]>
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 | ||
410cf6e6 | 85 | /* Contains canonical template parameter types. The vector is indexed by |
06d40de8 DG |
86 | the TEMPLATE_TYPE_IDX of the template parameter. Each element is a |
87 | TREE_LIST, whose TREE_VALUEs contain the canonical template | |
88 | parameters of various types and levels. */ | |
89 | static GTY(()) VEC(tree,gc) *canonical_template_parms; | |
90 | ||
830bfa74 MM |
91 | #define UNIFY_ALLOW_NONE 0 |
92 | #define UNIFY_ALLOW_MORE_CV_QUAL 1 | |
93 | #define UNIFY_ALLOW_LESS_CV_QUAL 2 | |
94 | #define UNIFY_ALLOW_DERIVED 4 | |
161c12b0 | 95 | #define UNIFY_ALLOW_INTEGER 8 |
028d1f20 | 96 | #define UNIFY_ALLOW_OUTER_LEVEL 16 |
62e4a758 NS |
97 | #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32 |
98 | #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64 | |
830bfa74 | 99 | |
3a978d72 NN |
100 | static void push_access_scope (tree); |
101 | static void pop_access_scope (tree); | |
acde59b4 MM |
102 | static bool resolve_overloaded_unification (tree, tree, tree, tree, |
103 | unification_kind_t, int); | |
3a978d72 | 104 | static int try_one_overload (tree, tree, tree, tree, tree, |
f23fb7f5 | 105 | unification_kind_t, int, bool); |
3a978d72 NN |
106 | static int unify (tree, tree, tree, tree, int); |
107 | static void add_pending_template (tree); | |
aa9d8196 VR |
108 | static int push_tinst_level (tree); |
109 | static void pop_tinst_level (void); | |
e2c3721c | 110 | static tree reopen_tinst_level (struct tinst_level *); |
3a978d72 | 111 | static tree tsubst_initializer_list (tree, tree); |
3a978d72 | 112 | static tree get_class_bindings (tree, tree, tree); |
3db45ab5 | 113 | static tree coerce_template_parms (tree, tree, tree, tsubst_flags_t, |
e7e93965 | 114 | bool, bool); |
3a978d72 NN |
115 | static void tsubst_enum (tree, tree, tree); |
116 | static tree add_to_template_args (tree, tree); | |
117 | static tree add_outermost_template_args (tree, tree); | |
118 | static bool check_instantiated_args (tree, tree, tsubst_flags_t); | |
8af2fec4 RY |
119 | static int maybe_adjust_types_for_deduction (unification_kind_t, tree*, tree*, |
120 | tree); | |
c166b898 ILT |
121 | static int type_unification_real (tree, tree, tree, const tree *, |
122 | unsigned int, int, unification_kind_t, int); | |
3a978d72 | 123 | static void note_template_header (int); |
b6ab6892 | 124 | static tree convert_nontype_argument_function (tree, tree); |
3a978d72 NN |
125 | static tree convert_nontype_argument (tree, tree); |
126 | static tree convert_template_argument (tree, tree, tree, | |
127 | tsubst_flags_t, int, tree); | |
0c58f841 | 128 | static int for_each_template_parm (tree, tree_fn_t, void*, |
c095a4f8 | 129 | struct pointer_set_t*, bool); |
5d80a306 | 130 | static tree expand_template_argument_pack (tree); |
3a978d72 | 131 | static tree build_template_parm_index (int, int, int, tree, tree); |
ae95e46e | 132 | static bool inline_needs_template_parms (tree); |
3a978d72 | 133 | static void push_inline_template_parms_recursive (tree, int); |
3a978d72 | 134 | static tree retrieve_local_specialization (tree); |
3a978d72 | 135 | static void register_local_specialization (tree, tree); |
ef3b7b17 | 136 | static tree reduce_template_parm_level (tree, tree, int, tree, tsubst_flags_t); |
3a978d72 NN |
137 | static int mark_template_parm (tree, void *); |
138 | static int template_parm_this_level_p (tree, void *); | |
139 | static tree tsubst_friend_function (tree, tree); | |
140 | static tree tsubst_friend_class (tree, tree); | |
141 | static int can_complete_type_without_circularity (tree); | |
a34d3336 | 142 | static tree get_bindings (tree, tree, tree, bool); |
3a978d72 NN |
143 | static int template_decl_level (tree); |
144 | static int check_cv_quals_for_unify (int, tree, tree); | |
5d80a306 DG |
145 | static void template_parm_level_and_index (tree, int*, int*); |
146 | static int unify_pack_expansion (tree, tree, tree, tree, int, bool, bool); | |
a91db711 NS |
147 | static tree tsubst_template_arg (tree, tree, tsubst_flags_t, tree); |
148 | static tree tsubst_template_args (tree, tree, tsubst_flags_t, tree); | |
3a978d72 NN |
149 | static tree tsubst_template_parms (tree, tree, tsubst_flags_t); |
150 | static void regenerate_decl_from_template (tree, tree); | |
3a978d72 | 151 | static tree most_specialized_class (tree, tree); |
3a978d72 | 152 | static tree tsubst_aggr_type (tree, tree, tsubst_flags_t, tree, int); |
3a978d72 NN |
153 | static tree tsubst_arg_types (tree, tree, tsubst_flags_t, tree); |
154 | static tree tsubst_function_type (tree, tree, tsubst_flags_t, tree); | |
9b910171 | 155 | static bool check_specialization_scope (void); |
3a978d72 NN |
156 | static tree process_partial_specialization (tree); |
157 | static void set_current_access_from_decl (tree); | |
3a978d72 | 158 | static tree get_template_base (tree, tree, tree, tree); |
3a978d72 NN |
159 | static tree try_class_unification (tree, tree, tree, tree); |
160 | static int coerce_template_template_parms (tree, tree, tsubst_flags_t, | |
161 | tree, tree); | |
4cf36211 | 162 | static bool template_template_parm_bindings_ok_p (tree, tree); |
3a978d72 NN |
163 | static int template_args_equal (tree, tree); |
164 | static void tsubst_default_arguments (tree); | |
165 | static tree for_each_template_parm_r (tree *, int *, void *); | |
166 | static tree copy_default_args_to_explicit_spec_1 (tree, tree); | |
167 | static void copy_default_args_to_explicit_spec (tree); | |
168 | static int invalid_nontype_parm_type_p (tree, tsubst_flags_t); | |
a723baf1 | 169 | static int eq_local_specializations (const void *, const void *); |
5d80a306 | 170 | static bool dependent_template_arg_p (tree); |
06d40de8 | 171 | static bool any_template_arguments_need_structural_equality_p (tree); |
5552b43c | 172 | static bool dependent_type_p_r (tree); |
015c2c66 | 173 | static tree tsubst_expr (tree, tree, tsubst_flags_t, tree, bool); |
14d22dd6 | 174 | static tree tsubst_copy (tree, tree, tsubst_flags_t, tree); |
5d80a306 | 175 | static tree tsubst_pack_expansion (tree, tree, tsubst_flags_t, tree); |
6afcfe0a | 176 | static tree tsubst_decl (tree, tree, tsubst_flags_t); |
d0940d56 | 177 | static void perform_typedefs_access_check (tree tmpl, tree targs); |
2eb25c98 | 178 | static void append_type_to_template_for_access_check_1 (tree, tree, tree); |
36a117a5 | 179 | |
2b59fc25 KL |
180 | /* Make the current scope suitable for access checking when we are |
181 | processing T. T can be FUNCTION_DECL for instantiated function | |
2b907f5c KL |
182 | template, or VAR_DECL for static member variable (need by |
183 | instantiate_decl). */ | |
2b59fc25 | 184 | |
8ce33230 | 185 | static void |
2b907f5c | 186 | push_access_scope (tree t) |
2b59fc25 | 187 | { |
50bc768d NS |
188 | gcc_assert (TREE_CODE (t) == FUNCTION_DECL |
189 | || TREE_CODE (t) == VAR_DECL); | |
2b59fc25 | 190 | |
23ff7e2d KL |
191 | if (DECL_FRIEND_CONTEXT (t)) |
192 | push_nested_class (DECL_FRIEND_CONTEXT (t)); | |
193 | else if (DECL_CLASS_SCOPE_P (t)) | |
2b907f5c | 194 | push_nested_class (DECL_CONTEXT (t)); |
0f399e5f KL |
195 | else |
196 | push_to_top_level (); | |
c8094d83 | 197 | |
2b907f5c | 198 | if (TREE_CODE (t) == FUNCTION_DECL) |
0f399e5f KL |
199 | { |
200 | saved_access_scope = tree_cons | |
201 | (NULL_TREE, current_function_decl, saved_access_scope); | |
202 | current_function_decl = t; | |
203 | } | |
2b59fc25 KL |
204 | } |
205 | ||
2b59fc25 KL |
206 | /* Restore the scope set up by push_access_scope. T is the node we |
207 | are processing. */ | |
208 | ||
8ce33230 | 209 | static void |
3a978d72 | 210 | pop_access_scope (tree t) |
2b59fc25 | 211 | { |
2b907f5c | 212 | if (TREE_CODE (t) == FUNCTION_DECL) |
2b59fc25 KL |
213 | { |
214 | current_function_decl = TREE_VALUE (saved_access_scope); | |
215 | saved_access_scope = TREE_CHAIN (saved_access_scope); | |
216 | } | |
0f399e5f | 217 | |
23ff7e2d | 218 | if (DECL_FRIEND_CONTEXT (t) || DECL_CLASS_SCOPE_P (t)) |
0f399e5f KL |
219 | pop_nested_class (); |
220 | else | |
221 | pop_from_top_level (); | |
2b59fc25 KL |
222 | } |
223 | ||
a723baf1 MM |
224 | /* Do any processing required when DECL (a member template |
225 | declaration) is finished. Returns the TEMPLATE_DECL corresponding | |
226 | to DECL, unless it is a specialization, in which case the DECL | |
227 | itself is returned. */ | |
e1467ff2 MM |
228 | |
229 | tree | |
3a978d72 | 230 | finish_member_template_decl (tree decl) |
e1467ff2 | 231 | { |
a723baf1 MM |
232 | if (decl == error_mark_node) |
233 | return error_mark_node; | |
234 | ||
50bc768d | 235 | gcc_assert (DECL_P (decl)); |
a723baf1 MM |
236 | |
237 | if (TREE_CODE (decl) == TYPE_DECL) | |
93cdc044 | 238 | { |
a723baf1 MM |
239 | tree type; |
240 | ||
241 | type = TREE_TYPE (decl); | |
92fab505 DG |
242 | if (type == error_mark_node) |
243 | return error_mark_node; | |
9e1e64ec | 244 | if (MAYBE_CLASS_TYPE_P (type) |
a723baf1 MM |
245 | && CLASSTYPE_TEMPLATE_INFO (type) |
246 | && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type)) | |
93cdc044 | 247 | { |
a723baf1 | 248 | tree tmpl = CLASSTYPE_TI_TEMPLATE (type); |
93cdc044 JM |
249 | check_member_template (tmpl); |
250 | return tmpl; | |
251 | } | |
8d019cef | 252 | return NULL_TREE; |
93cdc044 | 253 | } |
07c88314 | 254 | else if (TREE_CODE (decl) == FIELD_DECL) |
0f51ccfc | 255 | error ("data member %qD cannot be a member template", decl); |
a1da6cba | 256 | else if (DECL_TEMPLATE_INFO (decl)) |
e1467ff2 | 257 | { |
a1da6cba MM |
258 | if (!DECL_TEMPLATE_SPECIALIZATION (decl)) |
259 | { | |
260 | check_member_template (DECL_TI_TEMPLATE (decl)); | |
261 | return DECL_TI_TEMPLATE (decl); | |
262 | } | |
263 | else | |
264 | return decl; | |
c8094d83 | 265 | } |
a1da6cba | 266 | else |
0f51ccfc | 267 | error ("invalid member template declaration %qD", decl); |
e1467ff2 | 268 | |
a1da6cba | 269 | return error_mark_node; |
f84b4be9 | 270 | } |
e1467ff2 | 271 | |
91a77d68 JM |
272 | /* Return the template info node corresponding to T, whatever T is. */ |
273 | ||
274 | tree | |
275 | get_template_info (tree t) | |
276 | { | |
277 | tree tinfo = NULL_TREE; | |
278 | ||
279 | if (DECL_P (t) && DECL_LANG_SPECIFIC (t)) | |
280 | tinfo = DECL_TEMPLATE_INFO (t); | |
281 | ||
282 | if (!tinfo && TREE_CODE (t) == TYPE_DECL) | |
283 | t = TREE_TYPE (t); | |
284 | ||
285 | if (TAGGED_TYPE_P (t)) | |
286 | tinfo = TYPE_TEMPLATE_INFO (t); | |
287 | ||
288 | return tinfo; | |
289 | } | |
290 | ||
f84b4be9 | 291 | /* Returns the template nesting level of the indicated class TYPE. |
c8094d83 | 292 | |
f84b4be9 JM |
293 | For example, in: |
294 | template <class T> | |
295 | struct A | |
296 | { | |
297 | template <class U> | |
298 | struct B {}; | |
299 | }; | |
300 | ||
c8094d83 | 301 | A<T>::B<U> has depth two, while A<T> has depth one. |
39c01e4c | 302 | Both A<T>::B<int> and A<int>::B<U> have depth one, if |
260cd73f | 303 | they are instantiations, not specializations. |
39c01e4c MM |
304 | |
305 | This function is guaranteed to return 0 if passed NULL_TREE so | |
306 | that, for example, `template_class_depth (current_class_type)' is | |
307 | always safe. */ | |
f84b4be9 | 308 | |
260cd73f VR |
309 | int |
310 | template_class_depth (tree type) | |
f84b4be9 | 311 | { |
93cdc044 | 312 | int depth; |
f84b4be9 | 313 | |
c8094d83 | 314 | for (depth = 0; |
ed44da02 | 315 | type && TREE_CODE (type) != NAMESPACE_DECL; |
c8094d83 | 316 | type = (TREE_CODE (type) == FUNCTION_DECL) |
4f1c5b7d | 317 | ? CP_DECL_CONTEXT (type) : TYPE_CONTEXT (type)) |
ed44da02 | 318 | { |
91a77d68 JM |
319 | tree tinfo = get_template_info (type); |
320 | ||
321 | if (tinfo && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo)) | |
322 | && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo)))) | |
323 | ++depth; | |
ed44da02 | 324 | } |
f84b4be9 JM |
325 | |
326 | return depth; | |
e1467ff2 | 327 | } |
98c1c668 | 328 | |
ae95e46e PC |
329 | /* Subroutine of maybe_begin_member_template_processing. |
330 | Returns true if processing DECL needs us to push template parms. */ | |
cae40af6 | 331 | |
ae95e46e | 332 | static bool |
3a978d72 | 333 | inline_needs_template_parms (tree decl) |
cae40af6 JM |
334 | { |
335 | if (! DECL_TEMPLATE_INFO (decl)) | |
ae95e46e | 336 | return false; |
f84b4be9 | 337 | |
36a117a5 | 338 | return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl))) |
cae40af6 JM |
339 | > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl))); |
340 | } | |
341 | ||
342 | /* Subroutine of maybe_begin_member_template_processing. | |
343 | Push the template parms in PARMS, starting from LEVELS steps into the | |
344 | chain, and ending at the beginning, since template parms are listed | |
345 | innermost first. */ | |
346 | ||
347 | static void | |
3a978d72 | 348 | push_inline_template_parms_recursive (tree parmlist, int levels) |
cae40af6 JM |
349 | { |
350 | tree parms = TREE_VALUE (parmlist); | |
351 | int i; | |
352 | ||
353 | if (levels > 1) | |
354 | push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1); | |
786b5245 | 355 | |
98c1c668 | 356 | ++processing_template_decl; |
cae40af6 | 357 | current_template_parms |
4890c2f4 | 358 | = tree_cons (size_int (processing_template_decl), |
98c1c668 | 359 | parms, current_template_parms); |
cae40af6 JM |
360 | TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1; |
361 | ||
ac20c67a | 362 | begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec, |
0cbd7506 | 363 | NULL); |
c8094d83 | 364 | for (i = 0; i < TREE_VEC_LENGTH (parms); ++i) |
98c1c668 | 365 | { |
0f67a82f | 366 | tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i)); |
42b304f1 | 367 | |
0f67a82f LM |
368 | if (parm == error_mark_node) |
369 | continue; | |
42b304f1 | 370 | |
50bc768d | 371 | gcc_assert (DECL_P (parm)); |
cae40af6 | 372 | |
98c1c668 JM |
373 | switch (TREE_CODE (parm)) |
374 | { | |
786b5245 | 375 | case TYPE_DECL: |
73b0fce8 | 376 | case TEMPLATE_DECL: |
98c1c668 JM |
377 | pushdecl (parm); |
378 | break; | |
786b5245 MM |
379 | |
380 | case PARM_DECL: | |
381 | { | |
fc03edb3 MM |
382 | /* Make a CONST_DECL as is done in process_template_parm. |
383 | It is ugly that we recreate this here; the original | |
384 | version built in process_template_parm is no longer | |
385 | available. */ | |
c2255bc4 AH |
386 | tree decl = build_decl (DECL_SOURCE_LOCATION (parm), |
387 | CONST_DECL, DECL_NAME (parm), | |
786b5245 | 388 | TREE_TYPE (parm)); |
c727aa5e | 389 | DECL_ARTIFICIAL (decl) = 1; |
6de9cd9a | 390 | TREE_CONSTANT (decl) = 1; |
6de9cd9a | 391 | TREE_READONLY (decl) = 1; |
786b5245 | 392 | DECL_INITIAL (decl) = DECL_INITIAL (parm); |
cd9f6678 | 393 | SET_DECL_TEMPLATE_PARM_P (decl); |
786b5245 MM |
394 | pushdecl (decl); |
395 | } | |
cae40af6 | 396 | break; |
786b5245 | 397 | |
98c1c668 | 398 | default: |
315fb5db | 399 | gcc_unreachable (); |
98c1c668 JM |
400 | } |
401 | } | |
402 | } | |
403 | ||
cae40af6 JM |
404 | /* Restore the template parameter context for a member template or |
405 | a friend template defined in a class definition. */ | |
406 | ||
407 | void | |
3a978d72 | 408 | maybe_begin_member_template_processing (tree decl) |
cae40af6 JM |
409 | { |
410 | tree parms; | |
3dcaad8b | 411 | int levels = 0; |
cae40af6 | 412 | |
3dcaad8b MM |
413 | if (inline_needs_template_parms (decl)) |
414 | { | |
415 | parms = DECL_TEMPLATE_PARMS (most_general_template (decl)); | |
416 | levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl; | |
cae40af6 | 417 | |
3dcaad8b MM |
418 | if (DECL_TEMPLATE_SPECIALIZATION (decl)) |
419 | { | |
420 | --levels; | |
421 | parms = TREE_CHAIN (parms); | |
422 | } | |
cae40af6 | 423 | |
3dcaad8b | 424 | push_inline_template_parms_recursive (parms, levels); |
cae40af6 JM |
425 | } |
426 | ||
3dcaad8b MM |
427 | /* Remember how many levels of template parameters we pushed so that |
428 | we can pop them later. */ | |
53824026 | 429 | VEC_safe_push (int, heap, inline_parm_levels, levels); |
cae40af6 JM |
430 | } |
431 | ||
1875c2b7 | 432 | /* Undo the effects of maybe_begin_member_template_processing. */ |
98c1c668 | 433 | |
c8094d83 | 434 | void |
3a978d72 | 435 | maybe_end_member_template_processing (void) |
98c1c668 | 436 | { |
3dcaad8b | 437 | int i; |
53824026 | 438 | int last; |
3dcaad8b | 439 | |
53824026 | 440 | if (VEC_length (int, inline_parm_levels) == 0) |
98c1c668 JM |
441 | return; |
442 | ||
53824026 KH |
443 | last = VEC_pop (int, inline_parm_levels); |
444 | for (i = 0; i < last; ++i) | |
cae40af6 JM |
445 | { |
446 | --processing_template_decl; | |
447 | current_template_parms = TREE_CHAIN (current_template_parms); | |
448 | poplevel (0, 0, 0); | |
449 | } | |
98c1c668 JM |
450 | } |
451 | ||
36a117a5 | 452 | /* Return a new template argument vector which contains all of ARGS, |
f9a7ae04 | 453 | but has as its innermost set of arguments the EXTRA_ARGS. */ |
e6f1275f JM |
454 | |
455 | static tree | |
3a978d72 | 456 | add_to_template_args (tree args, tree extra_args) |
e6f1275f | 457 | { |
36a117a5 MM |
458 | tree new_args; |
459 | int extra_depth; | |
460 | int i; | |
461 | int j; | |
e6f1275f | 462 | |
36a117a5 | 463 | extra_depth = TMPL_ARGS_DEPTH (extra_args); |
f31c0a32 | 464 | new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth); |
e6f1275f | 465 | |
36a117a5 MM |
466 | for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i) |
467 | SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i)); | |
e6f1275f | 468 | |
36a117a5 MM |
469 | for (j = 1; j <= extra_depth; ++j, ++i) |
470 | SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j)); | |
c8094d83 | 471 | |
e6f1275f JM |
472 | return new_args; |
473 | } | |
474 | ||
36a117a5 MM |
475 | /* Like add_to_template_args, but only the outermost ARGS are added to |
476 | the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH | |
477 | (EXTRA_ARGS) levels are added. This function is used to combine | |
478 | the template arguments from a partial instantiation with the | |
479 | template arguments used to attain the full instantiation from the | |
480 | partial instantiation. */ | |
98c1c668 | 481 | |
4966381a | 482 | static tree |
3a978d72 | 483 | add_outermost_template_args (tree args, tree extra_args) |
98c1c668 JM |
484 | { |
485 | tree new_args; | |
486 | ||
e4a84209 MM |
487 | /* If there are more levels of EXTRA_ARGS than there are ARGS, |
488 | something very fishy is going on. */ | |
50bc768d | 489 | gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args)); |
e4a84209 MM |
490 | |
491 | /* If *all* the new arguments will be the EXTRA_ARGS, just return | |
492 | them. */ | |
493 | if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args)) | |
494 | return extra_args; | |
495 | ||
36a117a5 MM |
496 | /* For the moment, we make ARGS look like it contains fewer levels. */ |
497 | TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args); | |
c8094d83 | 498 | |
36a117a5 | 499 | new_args = add_to_template_args (args, extra_args); |
98c1c668 | 500 | |
36a117a5 MM |
501 | /* Now, we restore ARGS to its full dimensions. */ |
502 | TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args); | |
98c1c668 JM |
503 | |
504 | return new_args; | |
505 | } | |
5566b478 | 506 | |
f9a7ae04 MM |
507 | /* Return the N levels of innermost template arguments from the ARGS. */ |
508 | ||
509 | tree | |
3a978d72 | 510 | get_innermost_template_args (tree args, int n) |
f9a7ae04 MM |
511 | { |
512 | tree new_args; | |
513 | int extra_levels; | |
514 | int i; | |
515 | ||
50bc768d | 516 | gcc_assert (n >= 0); |
f9a7ae04 MM |
517 | |
518 | /* If N is 1, just return the innermost set of template arguments. */ | |
519 | if (n == 1) | |
520 | return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args)); | |
c8094d83 | 521 | |
f9a7ae04 MM |
522 | /* If we're not removing anything, just return the arguments we were |
523 | given. */ | |
524 | extra_levels = TMPL_ARGS_DEPTH (args) - n; | |
50bc768d | 525 | gcc_assert (extra_levels >= 0); |
f9a7ae04 MM |
526 | if (extra_levels == 0) |
527 | return args; | |
528 | ||
529 | /* Make a new set of arguments, not containing the outer arguments. */ | |
530 | new_args = make_tree_vec (n); | |
531 | for (i = 1; i <= n; ++i) | |
c8094d83 | 532 | SET_TMPL_ARGS_LEVEL (new_args, i, |
f9a7ae04 MM |
533 | TMPL_ARGS_LEVEL (args, i + extra_levels)); |
534 | ||
535 | return new_args; | |
536 | } | |
537 | ||
dc28490d JM |
538 | /* The inverse of get_innermost_template_args: Return all but the innermost |
539 | EXTRA_LEVELS levels of template arguments from the ARGS. */ | |
540 | ||
541 | static tree | |
542 | strip_innermost_template_args (tree args, int extra_levels) | |
543 | { | |
544 | tree new_args; | |
545 | int n = TMPL_ARGS_DEPTH (args) - extra_levels; | |
546 | int i; | |
547 | ||
548 | gcc_assert (n >= 0); | |
549 | ||
550 | /* If N is 1, just return the outermost set of template arguments. */ | |
551 | if (n == 1) | |
552 | return TMPL_ARGS_LEVEL (args, 1); | |
553 | ||
554 | /* If we're not removing anything, just return the arguments we were | |
555 | given. */ | |
556 | gcc_assert (extra_levels >= 0); | |
557 | if (extra_levels == 0) | |
558 | return args; | |
559 | ||
560 | /* Make a new set of arguments, not containing the inner arguments. */ | |
561 | new_args = make_tree_vec (n); | |
562 | for (i = 1; i <= n; ++i) | |
563 | SET_TMPL_ARGS_LEVEL (new_args, i, | |
564 | TMPL_ARGS_LEVEL (args, i)); | |
565 | ||
566 | return new_args; | |
567 | } | |
568 | ||
5566b478 MS |
569 | /* We've got a template header coming up; push to a new level for storing |
570 | the parms. */ | |
8d08fdba | 571 | |
8d08fdba | 572 | void |
3a978d72 | 573 | begin_template_parm_list (void) |
8d08fdba | 574 | { |
6757edfe MM |
575 | /* We use a non-tag-transparent scope here, which causes pushtag to |
576 | put tags in this scope, rather than in the enclosing class or | |
577 | namespace scope. This is the right thing, since we want | |
578 | TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a | |
579 | global template class, push_template_decl handles putting the | |
580 | TEMPLATE_DECL into top-level scope. For a nested template class, | |
581 | e.g.: | |
582 | ||
583 | template <class T> struct S1 { | |
0cbd7506 | 584 | template <class T> struct S2 {}; |
6757edfe MM |
585 | }; |
586 | ||
587 | pushtag contains special code to call pushdecl_with_scope on the | |
588 | TEMPLATE_DECL for S2. */ | |
ac20c67a | 589 | begin_scope (sk_template_parms, NULL); |
5156628f | 590 | ++processing_template_decl; |
67ffc812 | 591 | ++processing_template_parmlist; |
386b8a85 JM |
592 | note_template_header (0); |
593 | } | |
594 | ||
6c30752f | 595 | /* This routine is called when a specialization is declared. If it is |
9b910171 LM |
596 | invalid to declare a specialization here, an error is reported and |
597 | false is returned, otherwise this routine will return true. */ | |
6c30752f | 598 | |
9b910171 | 599 | static bool |
3a978d72 | 600 | check_specialization_scope (void) |
6c30752f MM |
601 | { |
602 | tree scope = current_scope (); | |
3ddfb0e6 | 603 | |
c8094d83 MS |
604 | /* [temp.expl.spec] |
605 | ||
6c30752f MM |
606 | An explicit specialization shall be declared in the namespace of |
607 | which the template is a member, or, for member templates, in the | |
608 | namespace of which the enclosing class or enclosing class | |
609 | template is a member. An explicit specialization of a member | |
610 | function, member class or static data member of a class template | |
611 | shall be declared in the namespace of which the class template | |
612 | is a member. */ | |
613 | if (scope && TREE_CODE (scope) != NAMESPACE_DECL) | |
9b910171 LM |
614 | { |
615 | error ("explicit specialization in non-namespace scope %qD", scope); | |
616 | return false; | |
617 | } | |
3ddfb0e6 | 618 | |
c8094d83 | 619 | /* [temp.expl.spec] |
6c30752f MM |
620 | |
621 | In an explicit specialization declaration for a member of a class | |
622 | template or a member template that appears in namespace scope, | |
623 | the member template and some of its enclosing class templates may | |
624 | remain unspecialized, except that the declaration shall not | |
625 | explicitly specialize a class member template if its enclosing | |
626 | class templates are not explicitly specialized as well. */ | |
c8094d83 | 627 | if (current_template_parms) |
9b910171 LM |
628 | { |
629 | error ("enclosing class templates are not explicitly specialized"); | |
630 | return false; | |
631 | } | |
632 | ||
633 | return true; | |
6c30752f MM |
634 | } |
635 | ||
c6002625 | 636 | /* We've just seen template <>. */ |
386b8a85 | 637 | |
9b910171 | 638 | bool |
3a978d72 | 639 | begin_specialization (void) |
386b8a85 | 640 | { |
ac20c67a | 641 | begin_scope (sk_template_spec, NULL); |
386b8a85 | 642 | note_template_header (1); |
9b910171 | 643 | return check_specialization_scope (); |
386b8a85 JM |
644 | } |
645 | ||
dc957d14 | 646 | /* Called at then end of processing a declaration preceded by |
386b8a85 JM |
647 | template<>. */ |
648 | ||
c8094d83 | 649 | void |
3a978d72 | 650 | end_specialization (void) |
386b8a85 | 651 | { |
74b846e0 | 652 | finish_scope (); |
386b8a85 JM |
653 | reset_specialization (); |
654 | } | |
655 | ||
386b8a85 | 656 | /* Any template <>'s that we have seen thus far are not referring to a |
c6002625 | 657 | function specialization. */ |
386b8a85 JM |
658 | |
659 | void | |
3a978d72 | 660 | reset_specialization (void) |
386b8a85 JM |
661 | { |
662 | processing_specialization = 0; | |
663 | template_header_count = 0; | |
664 | } | |
665 | ||
838dfd8a | 666 | /* We've just seen a template header. If SPECIALIZATION is nonzero, |
386b8a85 JM |
667 | it was of the form template <>. */ |
668 | ||
c8094d83 | 669 | static void |
3a978d72 | 670 | note_template_header (int specialization) |
386b8a85 JM |
671 | { |
672 | processing_specialization = specialization; | |
673 | template_header_count++; | |
674 | } | |
675 | ||
75650646 | 676 | /* We're beginning an explicit instantiation. */ |
386b8a85 | 677 | |
75650646 | 678 | void |
3a978d72 | 679 | begin_explicit_instantiation (void) |
386b8a85 | 680 | { |
50bc768d | 681 | gcc_assert (!processing_explicit_instantiation); |
a723baf1 | 682 | processing_explicit_instantiation = true; |
75650646 | 683 | } |
386b8a85 | 684 | |
386b8a85 | 685 | |
75650646 | 686 | void |
3a978d72 | 687 | end_explicit_instantiation (void) |
75650646 | 688 | { |
50bc768d | 689 | gcc_assert (processing_explicit_instantiation); |
a723baf1 | 690 | processing_explicit_instantiation = false; |
75650646 | 691 | } |
386b8a85 | 692 | |
13a44ee0 | 693 | /* An explicit specialization or partial specialization TMPL is being |
b1cc95ce MM |
694 | declared. Check that the namespace in which the specialization is |
695 | occurring is permissible. Returns false iff it is invalid to | |
696 | specialize TMPL in the current namespace. */ | |
c8094d83 | 697 | |
b1cc95ce MM |
698 | static bool |
699 | check_specialization_namespace (tree tmpl) | |
700 | { | |
701 | tree tpl_ns = decl_namespace_context (tmpl); | |
702 | ||
703 | /* [tmpl.expl.spec] | |
c8094d83 | 704 | |
b1cc95ce MM |
705 | An explicit specialization shall be declared in the namespace of |
706 | which the template is a member, or, for member templates, in the | |
707 | namespace of which the enclosing class or enclosing class | |
708 | template is a member. An explicit specialization of a member | |
709 | function, member class or static data member of a class template | |
710 | shall be declared in the namespace of which the class template is | |
711 | a member. */ | |
712 | if (is_associated_namespace (current_namespace, tpl_ns)) | |
713 | /* Same or super-using namespace. */ | |
714 | return true; | |
715 | else | |
716 | { | |
cbe5f3b3 MLI |
717 | permerror (input_location, "specialization of %qD in different namespace", tmpl); |
718 | permerror (input_location, " from definition of %q+#D", tmpl); | |
b1cc95ce MM |
719 | return false; |
720 | } | |
721 | } | |
722 | ||
9c12301f MM |
723 | /* SPEC is an explicit instantiation. Check that it is valid to |
724 | perform this explicit instantiation in the current namespace. */ | |
725 | ||
726 | static void | |
727 | check_explicit_instantiation_namespace (tree spec) | |
728 | { | |
729 | tree ns; | |
730 | ||
731 | /* DR 275: An explicit instantiation shall appear in an enclosing | |
3db45ab5 | 732 | namespace of its template. */ |
9c12301f MM |
733 | ns = decl_namespace_context (spec); |
734 | if (!is_ancestor (current_namespace, ns)) | |
cbe5f3b3 | 735 | permerror (input_location, "explicit instantiation of %qD in namespace %qD " |
37ec60ed JW |
736 | "(which does not enclose namespace %qD)", |
737 | spec, current_namespace, ns); | |
9c12301f MM |
738 | } |
739 | ||
36a117a5 MM |
740 | /* The TYPE is being declared. If it is a template type, that means it |
741 | is a partial specialization. Do appropriate error-checking. */ | |
742 | ||
9ce387d9 | 743 | tree |
3a978d72 | 744 | maybe_process_partial_specialization (tree type) |
36a117a5 | 745 | { |
c9043d03 VR |
746 | tree context; |
747 | ||
748 | if (type == error_mark_node) | |
9ce387d9 VR |
749 | return error_mark_node; |
750 | ||
751 | if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM) | |
752 | { | |
753 | error ("name of class shadows template template parameter %qD", | |
754 | TYPE_NAME (type)); | |
755 | return error_mark_node; | |
756 | } | |
c9043d03 VR |
757 | |
758 | context = TYPE_CONTEXT (type); | |
6e049fcd | 759 | |
a723baf1 | 760 | if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type)) |
36a117a5 | 761 | { |
6e049fcd KL |
762 | /* This is for ordinary explicit specialization and partial |
763 | specialization of a template class such as: | |
764 | ||
765 | template <> class C<int>; | |
766 | ||
767 | or: | |
768 | ||
769 | template <class T> class C<T*>; | |
770 | ||
771 | Make sure that `C<int>' and `C<T*>' are implicit instantiations. */ | |
772 | ||
36a117a5 | 773 | if (CLASSTYPE_IMPLICIT_INSTANTIATION (type) |
d0f062fb | 774 | && !COMPLETE_TYPE_P (type)) |
36a117a5 | 775 | { |
b1cc95ce | 776 | check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (type)); |
370af2d5 | 777 | SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type); |
36a117a5 | 778 | if (processing_template_decl) |
c5285718 PC |
779 | { |
780 | if (push_template_decl (TYPE_MAIN_DECL (type)) | |
781 | == error_mark_node) | |
782 | return error_mark_node; | |
783 | } | |
36a117a5 MM |
784 | } |
785 | else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type)) | |
0f51ccfc | 786 | error ("specialization of %qT after instantiation", type); |
36a117a5 | 787 | } |
6e049fcd KL |
788 | else if (CLASS_TYPE_P (type) |
789 | && !CLASSTYPE_USE_TEMPLATE (type) | |
790 | && CLASSTYPE_TEMPLATE_INFO (type) | |
791 | && context && CLASS_TYPE_P (context) | |
792 | && CLASSTYPE_TEMPLATE_INFO (context)) | |
793 | { | |
794 | /* This is for an explicit specialization of member class | |
795 | template according to [temp.expl.spec/18]: | |
796 | ||
797 | template <> template <class U> class C<int>::D; | |
798 | ||
799 | The context `C<int>' must be an implicit instantiation. | |
800 | Otherwise this is just a member class template declared | |
801 | earlier like: | |
802 | ||
803 | template <> class C<int> { template <class U> class D; }; | |
804 | template <> template <class U> class C<int>::D; | |
805 | ||
806 | In the first case, `C<int>::D' is a specialization of `C<T>::D' | |
807 | while in the second case, `C<int>::D' is a primary template | |
808 | and `C<T>::D' may not exist. */ | |
809 | ||
810 | if (CLASSTYPE_IMPLICIT_INSTANTIATION (context) | |
811 | && !COMPLETE_TYPE_P (type)) | |
812 | { | |
813 | tree t; | |
814 | ||
815 | if (current_namespace | |
816 | != decl_namespace_context (CLASSTYPE_TI_TEMPLATE (type))) | |
817 | { | |
cbe5f3b3 MLI |
818 | permerror (input_location, "specializing %q#T in different namespace", type); |
819 | permerror (input_location, " from definition of %q+#D", | |
37ec60ed | 820 | CLASSTYPE_TI_TEMPLATE (type)); |
6e049fcd KL |
821 | } |
822 | ||
823 | /* Check for invalid specialization after instantiation: | |
824 | ||
825 | template <> template <> class C<int>::D<int>; | |
826 | template <> template <class U> class C<int>::D; */ | |
827 | ||
828 | for (t = DECL_TEMPLATE_INSTANTIATIONS | |
829 | (most_general_template (CLASSTYPE_TI_TEMPLATE (type))); | |
830 | t; t = TREE_CHAIN (t)) | |
831 | if (TREE_VALUE (t) != type | |
832 | && TYPE_CONTEXT (TREE_VALUE (t)) == context) | |
0f51ccfc | 833 | error ("specialization %qT after instantiation %qT", |
6e049fcd KL |
834 | type, TREE_VALUE (t)); |
835 | ||
836 | /* Mark TYPE as a specialization. And as a result, we only | |
837 | have one level of template argument for the innermost | |
838 | class template. */ | |
839 | SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type); | |
840 | CLASSTYPE_TI_ARGS (type) | |
841 | = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)); | |
842 | } | |
843 | } | |
20496fa2 | 844 | else if (processing_specialization) |
9ce387d9 VR |
845 | { |
846 | error ("explicit specialization of non-template %qT", type); | |
847 | return error_mark_node; | |
848 | } | |
849 | ||
850 | return type; | |
36a117a5 MM |
851 | } |
852 | ||
ee81147e | 853 | /* Returns nonzero if we can optimize the retrieval of specializations |
c7222c02 MM |
854 | for TMPL, a TEMPLATE_DECL. In particular, for such a template, we |
855 | do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */ | |
856 | ||
c8094d83 | 857 | static inline bool |
c7222c02 MM |
858 | optimize_specialization_lookup_p (tree tmpl) |
859 | { | |
860 | return (DECL_FUNCTION_TEMPLATE_P (tmpl) | |
861 | && DECL_CLASS_SCOPE_P (tmpl) | |
862 | /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template | |
863 | parameter. */ | |
864 | && CLASS_TYPE_P (DECL_CONTEXT (tmpl)) | |
1ca939e5 MM |
865 | /* The optimized lookup depends on the fact that the |
866 | template arguments for the member function template apply | |
867 | purely to the containing class, which is not true if the | |
868 | containing class is an explicit or partial | |
869 | specialization. */ | |
870 | && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl)) | |
c7222c02 MM |
871 | && !DECL_MEMBER_TEMPLATE_P (tmpl) |
872 | && !DECL_CONV_FN_P (tmpl) | |
873 | /* It is possible to have a template that is not a member | |
874 | template and is not a member of a template class: | |
c8094d83 MS |
875 | |
876 | template <typename T> | |
c7222c02 | 877 | struct S { friend A::f(); }; |
c8094d83 | 878 | |
c7222c02 MM |
879 | Here, the friend function is a template, but the context does |
880 | not have template information. The optimized lookup relies | |
881 | on having ARGS be the template arguments for both the class | |
882 | and the function template. */ | |
883 | && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl))); | |
884 | } | |
885 | ||
75650646 MM |
886 | /* Retrieve the specialization (in the sense of [temp.spec] - a |
887 | specialization is either an instantiation or an explicit | |
888 | specialization) of TMPL for the given template ARGS. If there is | |
889 | no such specialization, return NULL_TREE. The ARGS are a vector of | |
890 | arguments, or a vector of vectors of arguments, in the case of | |
c8094d83 | 891 | templates with more than one level of parameters. |
c7222c02 MM |
892 | |
893 | If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true, | |
894 | then we search for a partial specialization matching ARGS. This | |
895 | parameter is ignored if TMPL is not a class template. */ | |
c8094d83 | 896 | |
75650646 | 897 | static tree |
c8094d83 | 898 | retrieve_specialization (tree tmpl, tree args, |
c7222c02 | 899 | bool class_specializations_p) |
75650646 | 900 | { |
2d8ba2c7 LM |
901 | if (args == error_mark_node) |
902 | return NULL_TREE; | |
903 | ||
50bc768d | 904 | gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL); |
75650646 | 905 | |
36a117a5 MM |
906 | /* There should be as many levels of arguments as there are |
907 | levels of parameters. */ | |
c8094d83 | 908 | gcc_assert (TMPL_ARGS_DEPTH (args) |
50bc768d | 909 | == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl))); |
c8094d83 | 910 | |
c7222c02 MM |
911 | if (optimize_specialization_lookup_p (tmpl)) |
912 | { | |
913 | tree class_template; | |
914 | tree class_specialization; | |
d4e6fecb | 915 | VEC(tree,gc) *methods; |
c7222c02 MM |
916 | tree fns; |
917 | int idx; | |
918 | ||
919 | /* The template arguments actually apply to the containing | |
920 | class. Find the class specialization with those | |
921 | arguments. */ | |
922 | class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl)); | |
c8094d83 | 923 | class_specialization |
c7222c02 MM |
924 | = retrieve_specialization (class_template, args, |
925 | /*class_specializations_p=*/false); | |
926 | if (!class_specialization) | |
927 | return NULL_TREE; | |
928 | /* Now, find the appropriate entry in the CLASSTYPE_METHOD_VEC | |
929 | for the specialization. */ | |
930 | idx = class_method_index_for_fn (class_specialization, tmpl); | |
931 | if (idx == -1) | |
932 | return NULL_TREE; | |
933 | /* Iterate through the methods with the indicated name, looking | |
934 | for the one that has an instance of TMPL. */ | |
935 | methods = CLASSTYPE_METHOD_VEC (class_specialization); | |
936 | for (fns = VEC_index (tree, methods, idx); fns; fns = OVL_NEXT (fns)) | |
937 | { | |
938 | tree fn = OVL_CURRENT (fns); | |
939 | if (DECL_TEMPLATE_INFO (fn) && DECL_TI_TEMPLATE (fn) == tmpl) | |
940 | return fn; | |
941 | } | |
942 | return NULL_TREE; | |
943 | } | |
944 | else | |
945 | { | |
946 | tree *sp; | |
947 | tree *head; | |
948 | ||
949 | /* Class templates store their instantiations on the | |
950 | DECL_TEMPLATE_INSTANTIATIONS list; other templates use the | |
951 | DECL_TEMPLATE_SPECIALIZATIONS list. */ | |
952 | if (!class_specializations_p | |
56d0c6e3 | 953 | && TREE_CODE (DECL_TEMPLATE_RESULT (tmpl)) == TYPE_DECL |
d0940d56 | 954 | && !is_typedef_decl (DECL_TEMPLATE_RESULT (tmpl)) |
56d0c6e3 | 955 | && TAGGED_TYPE_P (TREE_TYPE (tmpl))) |
c7222c02 MM |
956 | sp = &DECL_TEMPLATE_INSTANTIATIONS (tmpl); |
957 | else | |
958 | sp = &DECL_TEMPLATE_SPECIALIZATIONS (tmpl); | |
959 | head = sp; | |
960 | /* Iterate through the list until we find a matching template. */ | |
961 | while (*sp != NULL_TREE) | |
962 | { | |
963 | tree spec = *sp; | |
c8094d83 | 964 | |
c7222c02 MM |
965 | if (comp_template_args (TREE_PURPOSE (spec), args)) |
966 | { | |
967 | /* Use the move-to-front heuristic to speed up future | |
c8094d83 | 968 | searches. */ |
c7222c02 MM |
969 | if (spec != *head) |
970 | { | |
971 | *sp = TREE_CHAIN (*sp); | |
972 | TREE_CHAIN (spec) = *head; | |
973 | *head = spec; | |
974 | } | |
975 | return TREE_VALUE (spec); | |
976 | } | |
977 | sp = &TREE_CHAIN (spec); | |
978 | } | |
979 | } | |
75650646 MM |
980 | |
981 | return NULL_TREE; | |
386b8a85 JM |
982 | } |
983 | ||
dc957d14 | 984 | /* Like retrieve_specialization, but for local declarations. */ |
9188c363 MM |
985 | |
986 | static tree | |
3a978d72 | 987 | retrieve_local_specialization (tree tmpl) |
9188c363 | 988 | { |
29b0d1fd JM |
989 | tree spec; |
990 | ||
991 | if (local_specializations == NULL) | |
992 | return NULL_TREE; | |
993 | ||
994 | spec = (tree) htab_find_with_hash (local_specializations, tmpl, | |
995 | htab_hash_pointer (tmpl)); | |
a723baf1 | 996 | return spec ? TREE_PURPOSE (spec) : NULL_TREE; |
9188c363 MM |
997 | } |
998 | ||
838dfd8a | 999 | /* Returns nonzero iff DECL is a specialization of TMPL. */ |
6757edfe MM |
1000 | |
1001 | int | |
3a978d72 | 1002 | is_specialization_of (tree decl, tree tmpl) |
6757edfe MM |
1003 | { |
1004 | tree t; | |
1005 | ||
1006 | if (TREE_CODE (decl) == FUNCTION_DECL) | |
1007 | { | |
c8094d83 | 1008 | for (t = decl; |
6757edfe MM |
1009 | t != NULL_TREE; |
1010 | t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE) | |
1011 | if (t == tmpl) | |
1012 | return 1; | |
1013 | } | |
c8094d83 | 1014 | else |
6757edfe | 1015 | { |
50bc768d | 1016 | gcc_assert (TREE_CODE (decl) == TYPE_DECL); |
6757edfe MM |
1017 | |
1018 | for (t = TREE_TYPE (decl); | |
1019 | t != NULL_TREE; | |
1020 | t = CLASSTYPE_USE_TEMPLATE (t) | |
1021 | ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE) | |
9edc3913 | 1022 | if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl))) |
6757edfe | 1023 | return 1; |
c8094d83 | 1024 | } |
386b8a85 | 1025 | |
6757edfe MM |
1026 | return 0; |
1027 | } | |
75650646 | 1028 | |
d43f603d | 1029 | /* Returns nonzero iff DECL is a specialization of friend declaration |
be93747e | 1030 | FRIEND_DECL according to [temp.friend]. */ |
d43f603d KL |
1031 | |
1032 | bool | |
be93747e | 1033 | is_specialization_of_friend (tree decl, tree friend_decl) |
d43f603d KL |
1034 | { |
1035 | bool need_template = true; | |
1036 | int template_depth; | |
1037 | ||
b939a023 KL |
1038 | gcc_assert (TREE_CODE (decl) == FUNCTION_DECL |
1039 | || TREE_CODE (decl) == TYPE_DECL); | |
d43f603d | 1040 | |
be93747e | 1041 | /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function |
d43f603d KL |
1042 | of a template class, we want to check if DECL is a specialization |
1043 | if this. */ | |
be93747e KG |
1044 | if (TREE_CODE (friend_decl) == FUNCTION_DECL |
1045 | && DECL_TEMPLATE_INFO (friend_decl) | |
1046 | && !DECL_USE_TEMPLATE (friend_decl)) | |
d43f603d | 1047 | { |
b939a023 | 1048 | /* We want a TEMPLATE_DECL for `is_specialization_of'. */ |
be93747e | 1049 | friend_decl = DECL_TI_TEMPLATE (friend_decl); |
d43f603d KL |
1050 | need_template = false; |
1051 | } | |
be93747e KG |
1052 | else if (TREE_CODE (friend_decl) == TEMPLATE_DECL |
1053 | && !PRIMARY_TEMPLATE_P (friend_decl)) | |
b939a023 | 1054 | need_template = false; |
d43f603d KL |
1055 | |
1056 | /* There is nothing to do if this is not a template friend. */ | |
be93747e | 1057 | if (TREE_CODE (friend_decl) != TEMPLATE_DECL) |
b939a023 | 1058 | return false; |
d43f603d | 1059 | |
be93747e | 1060 | if (is_specialization_of (decl, friend_decl)) |
b939a023 | 1061 | return true; |
d43f603d KL |
1062 | |
1063 | /* [temp.friend/6] | |
1064 | A member of a class template may be declared to be a friend of a | |
1065 | non-template class. In this case, the corresponding member of | |
1066 | every specialization of the class template is a friend of the | |
1067 | class granting friendship. | |
c8094d83 | 1068 | |
d43f603d KL |
1069 | For example, given a template friend declaration |
1070 | ||
1071 | template <class T> friend void A<T>::f(); | |
1072 | ||
1073 | the member function below is considered a friend | |
1074 | ||
1075 | template <> struct A<int> { | |
1076 | void f(); | |
1077 | }; | |
1078 | ||
1079 | For this type of template friend, TEMPLATE_DEPTH below will be | |
5c425df5 | 1080 | nonzero. To determine if DECL is a friend of FRIEND, we first |
d43f603d KL |
1081 | check if the enclosing class is a specialization of another. */ |
1082 | ||
be93747e | 1083 | template_depth = template_class_depth (DECL_CONTEXT (friend_decl)); |
d43f603d KL |
1084 | if (template_depth |
1085 | && DECL_CLASS_SCOPE_P (decl) | |
c8094d83 | 1086 | && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)), |
be93747e | 1087 | CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl)))) |
d43f603d KL |
1088 | { |
1089 | /* Next, we check the members themselves. In order to handle | |
be93747e | 1090 | a few tricky cases, such as when FRIEND_DECL's are |
d43f603d KL |
1091 | |
1092 | template <class T> friend void A<T>::g(T t); | |
1093 | template <class T> template <T t> friend void A<T>::h(); | |
1094 | ||
b939a023 KL |
1095 | and DECL's are |
1096 | ||
1097 | void A<int>::g(int); | |
1098 | template <int> void A<int>::h(); | |
1099 | ||
1100 | we need to figure out ARGS, the template arguments from | |
1101 | the context of DECL. This is required for template substitution | |
1102 | of `T' in the function parameter of `g' and template parameter | |
1103 | of `h' in the above examples. Here ARGS corresponds to `int'. */ | |
d43f603d KL |
1104 | |
1105 | tree context = DECL_CONTEXT (decl); | |
1106 | tree args = NULL_TREE; | |
1107 | int current_depth = 0; | |
b939a023 | 1108 | |
d43f603d KL |
1109 | while (current_depth < template_depth) |
1110 | { | |
1111 | if (CLASSTYPE_TEMPLATE_INFO (context)) | |
1112 | { | |
1113 | if (current_depth == 0) | |
1114 | args = TYPE_TI_ARGS (context); | |
1115 | else | |
1116 | args = add_to_template_args (TYPE_TI_ARGS (context), args); | |
1117 | current_depth++; | |
1118 | } | |
1119 | context = TYPE_CONTEXT (context); | |
1120 | } | |
1121 | ||
1122 | if (TREE_CODE (decl) == FUNCTION_DECL) | |
1123 | { | |
1124 | bool is_template; | |
1125 | tree friend_type; | |
1126 | tree decl_type; | |
1127 | tree friend_args_type; | |
1128 | tree decl_args_type; | |
1129 | ||
be93747e | 1130 | /* Make sure that both DECL and FRIEND_DECL are templates or |
d43f603d KL |
1131 | non-templates. */ |
1132 | is_template = DECL_TEMPLATE_INFO (decl) | |
1133 | && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)); | |
1134 | if (need_template ^ is_template) | |
b939a023 | 1135 | return false; |
d43f603d KL |
1136 | else if (is_template) |
1137 | { | |
da1d7781 | 1138 | /* If both are templates, check template parameter list. */ |
d43f603d | 1139 | tree friend_parms |
be93747e | 1140 | = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl), |
d43f603d KL |
1141 | args, tf_none); |
1142 | if (!comp_template_parms | |
1143 | (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)), | |
1144 | friend_parms)) | |
b939a023 | 1145 | return false; |
d43f603d KL |
1146 | |
1147 | decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl)); | |
1148 | } | |
1149 | else | |
1150 | decl_type = TREE_TYPE (decl); | |
1151 | ||
be93747e | 1152 | friend_type = tsubst_function_type (TREE_TYPE (friend_decl), args, |
d43f603d KL |
1153 | tf_none, NULL_TREE); |
1154 | if (friend_type == error_mark_node) | |
b939a023 | 1155 | return false; |
d43f603d KL |
1156 | |
1157 | /* Check if return types match. */ | |
1158 | if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type))) | |
b939a023 | 1159 | return false; |
d43f603d KL |
1160 | |
1161 | /* Check if function parameter types match, ignoring the | |
1162 | `this' parameter. */ | |
1163 | friend_args_type = TYPE_ARG_TYPES (friend_type); | |
1164 | decl_args_type = TYPE_ARG_TYPES (decl_type); | |
be93747e | 1165 | if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl)) |
d43f603d KL |
1166 | friend_args_type = TREE_CHAIN (friend_args_type); |
1167 | if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)) | |
1168 | decl_args_type = TREE_CHAIN (decl_args_type); | |
b939a023 KL |
1169 | |
1170 | return compparms (decl_args_type, friend_args_type); | |
1171 | } | |
1172 | else | |
1173 | { | |
1174 | /* DECL is a TYPE_DECL */ | |
1175 | bool is_template; | |
1176 | tree decl_type = TREE_TYPE (decl); | |
1177 | ||
be93747e | 1178 | /* Make sure that both DECL and FRIEND_DECL are templates or |
b939a023 KL |
1179 | non-templates. */ |
1180 | is_template | |
1181 | = CLASSTYPE_TEMPLATE_INFO (decl_type) | |
1182 | && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type)); | |
1183 | ||
1184 | if (need_template ^ is_template) | |
1185 | return false; | |
1186 | else if (is_template) | |
1187 | { | |
1188 | tree friend_parms; | |
1189 | /* If both are templates, check the name of the two | |
1190 | TEMPLATE_DECL's first because is_friend didn't. */ | |
1191 | if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type)) | |
be93747e | 1192 | != DECL_NAME (friend_decl)) |
b939a023 KL |
1193 | return false; |
1194 | ||
1195 | /* Now check template parameter list. */ | |
1196 | friend_parms | |
be93747e | 1197 | = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl), |
b939a023 KL |
1198 | args, tf_none); |
1199 | return comp_template_parms | |
1200 | (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)), | |
1201 | friend_parms); | |
1202 | } | |
1203 | else | |
1204 | return (DECL_NAME (decl) | |
be93747e | 1205 | == DECL_NAME (friend_decl)); |
d43f603d KL |
1206 | } |
1207 | } | |
b939a023 | 1208 | return false; |
d43f603d KL |
1209 | } |
1210 | ||
75650646 | 1211 | /* Register the specialization SPEC as a specialization of TMPL with |
d63d5d0c ILT |
1212 | the indicated ARGS. IS_FRIEND indicates whether the specialization |
1213 | is actually just a friend declaration. Returns SPEC, or an | |
1214 | equivalent prior declaration, if available. */ | |
75650646 | 1215 | |
36a117a5 | 1216 | static tree |
d63d5d0c | 1217 | register_specialization (tree spec, tree tmpl, tree args, bool is_friend) |
75650646 | 1218 | { |
c7222c02 | 1219 | tree fn; |
75650646 | 1220 | |
50bc768d | 1221 | gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL); |
75650646 | 1222 | |
c8094d83 | 1223 | if (TREE_CODE (spec) == FUNCTION_DECL |
36a117a5 MM |
1224 | && uses_template_parms (DECL_TI_ARGS (spec))) |
1225 | /* This is the FUNCTION_DECL for a partial instantiation. Don't | |
1226 | register it; we want the corresponding TEMPLATE_DECL instead. | |
1227 | We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than | |
1228 | the more obvious `uses_template_parms (spec)' to avoid problems | |
1229 | with default function arguments. In particular, given | |
1230 | something like this: | |
1231 | ||
0cbd7506 | 1232 | template <class T> void f(T t1, T t = T()) |
36a117a5 MM |
1233 | |
1234 | the default argument expression is not substituted for in an | |
1235 | instantiation unless and until it is actually needed. */ | |
1236 | return spec; | |
28ba38e3 | 1237 | |
c8094d83 | 1238 | fn = retrieve_specialization (tmpl, args, |
c7222c02 MM |
1239 | /*class_specializations_p=*/false); |
1240 | /* We can sometimes try to re-register a specialization that we've | |
1241 | already got. In particular, regenerate_decl_from_template calls | |
1242 | duplicate_decls which will update the specialization list. But, | |
1243 | we'll still get called again here anyhow. It's more convenient | |
1244 | to simply allow this than to try to prevent it. */ | |
1245 | if (fn == spec) | |
1246 | return spec; | |
1247 | else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec)) | |
22e9174f | 1248 | { |
c7222c02 | 1249 | if (DECL_TEMPLATE_INSTANTIATION (fn)) |
22e9174f | 1250 | { |
c8094d83 | 1251 | if (TREE_USED (fn) |
c7222c02 | 1252 | || DECL_EXPLICIT_INSTANTIATION (fn)) |
22e9174f | 1253 | { |
c7222c02 MM |
1254 | error ("specialization of %qD after instantiation", |
1255 | fn); | |
99f4234a | 1256 | return error_mark_node; |
22e9174f | 1257 | } |
c7222c02 | 1258 | else |
b1cc95ce | 1259 | { |
a39041fa | 1260 | tree clone; |
c7222c02 MM |
1261 | /* This situation should occur only if the first |
1262 | specialization is an implicit instantiation, the | |
1263 | second is an explicit specialization, and the | |
1264 | implicit instantiation has not yet been used. That | |
1265 | situation can occur if we have implicitly | |
1266 | instantiated a member function and then specialized | |
1267 | it later. | |
c8094d83 | 1268 | |
c7222c02 MM |
1269 | We can also wind up here if a friend declaration that |
1270 | looked like an instantiation turns out to be a | |
1271 | specialization: | |
c8094d83 | 1272 | |
c7222c02 MM |
1273 | template <class T> void foo(T); |
1274 | class S { friend void foo<>(int) }; | |
c8094d83 MS |
1275 | template <> void foo(int); |
1276 | ||
c7222c02 MM |
1277 | We transform the existing DECL in place so that any |
1278 | pointers to it become pointers to the updated | |
1279 | declaration. | |
1280 | ||
1281 | If there was a definition for the template, but not | |
1282 | for the specialization, we want this to look as if | |
1283 | there were no definition, and vice versa. */ | |
1284 | DECL_INITIAL (fn) = NULL_TREE; | |
d63d5d0c | 1285 | duplicate_decls (spec, fn, is_friend); |
a39041fa | 1286 | /* The call to duplicate_decls will have applied |
3db45ab5 | 1287 | [temp.expl.spec]: |
a39041fa | 1288 | |
3db45ab5 | 1289 | An explicit specialization of a function template |
a39041fa | 1290 | is inline only if it is explicitly declared to be, |
bcf51da2 | 1291 | and independently of whether its function template |
a39041fa MM |
1292 | is. |
1293 | ||
1294 | to the primary function; now copy the inline bits to | |
3db45ab5 | 1295 | the various clones. */ |
a39041fa | 1296 | FOR_EACH_CLONE (clone, fn) |
99355518 JH |
1297 | DECL_DECLARED_INLINE_P (clone) |
1298 | = DECL_DECLARED_INLINE_P (fn); | |
9c12301f | 1299 | check_specialization_namespace (fn); |
c8094d83 | 1300 | |
b1cc95ce MM |
1301 | return fn; |
1302 | } | |
22e9174f | 1303 | } |
c7222c02 MM |
1304 | else if (DECL_TEMPLATE_SPECIALIZATION (fn)) |
1305 | { | |
d63d5d0c | 1306 | if (!duplicate_decls (spec, fn, is_friend) && DECL_INITIAL (spec)) |
c7222c02 MM |
1307 | /* Dup decl failed, but this is a new definition. Set the |
1308 | line number so any errors match this new | |
1309 | definition. */ | |
1310 | DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec); | |
c8094d83 | 1311 | |
c7222c02 MM |
1312 | return fn; |
1313 | } | |
b1cc95ce MM |
1314 | } |
1315 | ||
1316 | /* A specialization must be declared in the same namespace as the | |
1317 | template it is specializing. */ | |
1318 | if (DECL_TEMPLATE_SPECIALIZATION (spec) | |
1319 | && !check_specialization_namespace (tmpl)) | |
db10c6ec | 1320 | DECL_CONTEXT (spec) = DECL_CONTEXT (tmpl); |
75650646 | 1321 | |
c7222c02 MM |
1322 | if (!optimize_specialization_lookup_p (tmpl)) |
1323 | DECL_TEMPLATE_SPECIALIZATIONS (tmpl) | |
1324 | = tree_cons (args, spec, DECL_TEMPLATE_SPECIALIZATIONS (tmpl)); | |
36a117a5 MM |
1325 | |
1326 | return spec; | |
1327 | } | |
1328 | ||
1329 | /* Unregister the specialization SPEC as a specialization of TMPL. | |
b3445994 MM |
1330 | Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true |
1331 | if the SPEC was listed as a specialization of TMPL. */ | |
36a117a5 | 1332 | |
b3445994 MM |
1333 | bool |
1334 | reregister_specialization (tree spec, tree tmpl, tree new_spec) | |
36a117a5 MM |
1335 | { |
1336 | tree* s; | |
1337 | ||
1338 | for (s = &DECL_TEMPLATE_SPECIALIZATIONS (tmpl); | |
1339 | *s != NULL_TREE; | |
1340 | s = &TREE_CHAIN (*s)) | |
1341 | if (TREE_VALUE (*s) == spec) | |
1342 | { | |
b3445994 MM |
1343 | if (!new_spec) |
1344 | *s = TREE_CHAIN (*s); | |
1345 | else | |
a3d87771 | 1346 | TREE_VALUE (*s) = new_spec; |
36a117a5 MM |
1347 | return 1; |
1348 | } | |
1349 | ||
1350 | return 0; | |
75650646 MM |
1351 | } |
1352 | ||
a723baf1 MM |
1353 | /* Compare an entry in the local specializations hash table P1 (which |
1354 | is really a pointer to a TREE_LIST) with P2 (which is really a | |
1355 | DECL). */ | |
1356 | ||
1357 | static int | |
1358 | eq_local_specializations (const void *p1, const void *p2) | |
1359 | { | |
741ac903 | 1360 | return TREE_VALUE ((const_tree) p1) == (const_tree) p2; |
a723baf1 MM |
1361 | } |
1362 | ||
69f794a7 MM |
1363 | /* Hash P1, an entry in the local specializations table. */ |
1364 | ||
1365 | static hashval_t | |
1366 | hash_local_specialization (const void* p1) | |
1367 | { | |
741ac903 | 1368 | return htab_hash_pointer (TREE_VALUE ((const_tree) p1)); |
69f794a7 MM |
1369 | } |
1370 | ||
6dfbb909 MM |
1371 | /* Like register_specialization, but for local declarations. We are |
1372 | registering SPEC, an instantiation of TMPL. */ | |
9188c363 | 1373 | |
414ea4aa | 1374 | static void |
3a978d72 | 1375 | register_local_specialization (tree spec, tree tmpl) |
9188c363 | 1376 | { |
6dfbb909 MM |
1377 | void **slot; |
1378 | ||
c8094d83 | 1379 | slot = htab_find_slot_with_hash (local_specializations, tmpl, |
69f794a7 | 1380 | htab_hash_pointer (tmpl), INSERT); |
a723baf1 | 1381 | *slot = build_tree_list (spec, tmpl); |
9188c363 MM |
1382 | } |
1383 | ||
9ba7a2f2 MM |
1384 | /* TYPE is a class type. Returns true if TYPE is an explicitly |
1385 | specialized class. */ | |
1386 | ||
1387 | bool | |
1388 | explicit_class_specialization_p (tree type) | |
1389 | { | |
1390 | if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type)) | |
1391 | return false; | |
1392 | return !uses_template_parms (CLASSTYPE_TI_ARGS (type)); | |
1393 | } | |
1394 | ||
e1467ff2 MM |
1395 | /* Print the list of candidate FNS in an error message. */ |
1396 | ||
104bf76a | 1397 | void |
3a978d72 | 1398 | print_candidates (tree fns) |
e1467ff2 MM |
1399 | { |
1400 | tree fn; | |
1401 | ||
d8e178a0 | 1402 | const char *str = "candidates are:"; |
e1467ff2 MM |
1403 | |
1404 | for (fn = fns; fn != NULL_TREE; fn = TREE_CHAIN (fn)) | |
1405 | { | |
d6479fe7 MM |
1406 | tree f; |
1407 | ||
1408 | for (f = TREE_VALUE (fn); f; f = OVL_NEXT (f)) | |
dee15844 | 1409 | error ("%s %+#D", str, OVL_CURRENT (f)); |
e1467ff2 MM |
1410 | str = " "; |
1411 | } | |
1412 | } | |
1413 | ||
75650646 | 1414 | /* Returns the template (one of the functions given by TEMPLATE_ID) |
e1467ff2 | 1415 | which can be specialized to match the indicated DECL with the |
03017874 MM |
1416 | explicit template args given in TEMPLATE_ID. The DECL may be |
1417 | NULL_TREE if none is available. In that case, the functions in | |
1418 | TEMPLATE_ID are non-members. | |
1419 | ||
838dfd8a | 1420 | If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a |
03017874 MM |
1421 | specialization of a member template. |
1422 | ||
5fe7b654 GB |
1423 | The TEMPLATE_COUNT is the number of references to qualifying |
1424 | template classes that appeared in the name of the function. See | |
1425 | check_explicit_specialization for a more accurate description. | |
1426 | ||
29a1da1c MM |
1427 | TSK indicates what kind of template declaration (if any) is being |
1428 | declared. TSK_TEMPLATE indicates that the declaration given by | |
1429 | DECL, though a FUNCTION_DECL, has template parameters, and is | |
1430 | therefore a template function. | |
1431 | ||
03017874 MM |
1432 | The template args (those explicitly specified and those deduced) |
1433 | are output in a newly created vector *TARGS_OUT. | |
1434 | ||
1435 | If it is impossible to determine the result, an error message is | |
bf8f3f93 | 1436 | issued. The error_mark_node is returned to indicate failure. */ |
75650646 | 1437 | |
e9659ab0 | 1438 | static tree |
c8094d83 | 1439 | determine_specialization (tree template_id, |
0cbd7506 MS |
1440 | tree decl, |
1441 | tree* targs_out, | |
5fe7b654 | 1442 | int need_member_template, |
29a1da1c MM |
1443 | int template_count, |
1444 | tmpl_spec_kind tsk) | |
386b8a85 | 1445 | { |
03017874 MM |
1446 | tree fns; |
1447 | tree targs; | |
1448 | tree explicit_targs; | |
1449 | tree candidates = NULL_TREE; | |
7ca383e6 MM |
1450 | /* A TREE_LIST of templates of which DECL may be a specialization. |
1451 | The TREE_VALUE of each node is a TEMPLATE_DECL. The | |
1452 | corresponding TREE_PURPOSE is the set of template arguments that, | |
1453 | when used to instantiate the template, would produce a function | |
1454 | with the signature of DECL. */ | |
03017874 | 1455 | tree templates = NULL_TREE; |
5fe7b654 GB |
1456 | int header_count; |
1457 | struct cp_binding_level *b; | |
386b8a85 | 1458 | |
e1467ff2 MM |
1459 | *targs_out = NULL_TREE; |
1460 | ||
728da672 | 1461 | if (template_id == error_mark_node || decl == error_mark_node) |
f2e48b67 BK |
1462 | return error_mark_node; |
1463 | ||
1464 | fns = TREE_OPERAND (template_id, 0); | |
03017874 | 1465 | explicit_targs = TREE_OPERAND (template_id, 1); |
f2e48b67 | 1466 | |
27fafc8d JM |
1467 | if (fns == error_mark_node) |
1468 | return error_mark_node; | |
1469 | ||
c6002625 | 1470 | /* Check for baselinks. */ |
91e490ab | 1471 | if (BASELINK_P (fns)) |
da15dae6 | 1472 | fns = BASELINK_FUNCTIONS (fns); |
386b8a85 | 1473 | |
91e490ab MM |
1474 | if (!is_overloaded_fn (fns)) |
1475 | { | |
0f51ccfc | 1476 | error ("%qD is not a function template", fns); |
91e490ab MM |
1477 | return error_mark_node; |
1478 | } | |
1479 | ||
5fe7b654 GB |
1480 | /* Count the number of template headers specified for this |
1481 | specialization. */ | |
1482 | header_count = 0; | |
c8094d83 | 1483 | for (b = current_binding_level; |
98f99d7f | 1484 | b->kind == sk_template_parms; |
5fe7b654 GB |
1485 | b = b->level_chain) |
1486 | ++header_count; | |
1487 | ||
2c73f9f5 | 1488 | for (; fns; fns = OVL_NEXT (fns)) |
386b8a85 | 1489 | { |
9dfce8fd | 1490 | tree fn = OVL_CURRENT (fns); |
03017874 MM |
1491 | |
1492 | if (TREE_CODE (fn) == TEMPLATE_DECL) | |
d955f6ea KL |
1493 | { |
1494 | tree decl_arg_types; | |
0da99d4e | 1495 | tree fn_arg_types; |
d955f6ea | 1496 | |
5fe7b654 GB |
1497 | /* In case of explicit specialization, we need to check if |
1498 | the number of template headers appearing in the specialization | |
1499 | is correct. This is usually done in check_explicit_specialization, | |
1500 | but the check done there cannot be exhaustive when specializing | |
1501 | member functions. Consider the following code: | |
1502 | ||
1503 | template <> void A<int>::f(int); | |
1504 | template <> template <> void A<int>::f(int); | |
1505 | ||
1506 | Assuming that A<int> is not itself an explicit specialization | |
1507 | already, the first line specializes "f" which is a non-template | |
1508 | member function, whilst the second line specializes "f" which | |
1509 | is a template member function. So both lines are syntactically | |
1510 | correct, and check_explicit_specialization does not reject | |
1511 | them. | |
c8094d83 | 1512 | |
5fe7b654 GB |
1513 | Here, we can do better, as we are matching the specialization |
1514 | against the declarations. We count the number of template | |
1515 | headers, and we check if they match TEMPLATE_COUNT + 1 | |
1516 | (TEMPLATE_COUNT is the number of qualifying template classes, | |
1517 | plus there must be another header for the member template | |
1518 | itself). | |
c8094d83 | 1519 | |
5fe7b654 GB |
1520 | Notice that if header_count is zero, this is not a |
1521 | specialization but rather a template instantiation, so there | |
1522 | is no check we can perform here. */ | |
1523 | if (header_count && header_count != template_count + 1) | |
1524 | continue; | |
1525 | ||
98f99d7f MM |
1526 | /* Check that the number of template arguments at the |
1527 | innermost level for DECL is the same as for FN. */ | |
1528 | if (current_binding_level->kind == sk_template_parms | |
1529 | && !current_binding_level->explicit_spec_p | |
1530 | && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn)) | |
3db45ab5 | 1531 | != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS |
916b63c3 | 1532 | (current_template_parms)))) |
98f99d7f | 1533 | continue; |
c8094d83 | 1534 | |
ba139ba8 MM |
1535 | /* DECL might be a specialization of FN. */ |
1536 | decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl)); | |
1537 | fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn)); | |
1538 | ||
1539 | /* For a non-static member function, we need to make sure | |
1540 | that the const qualification is the same. Since | |
1541 | get_bindings does not try to merge the "this" parameter, | |
1542 | we must do the comparison explicitly. */ | |
1543 | if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn) | |
1544 | && !same_type_p (TREE_VALUE (fn_arg_types), | |
1545 | TREE_VALUE (decl_arg_types))) | |
1546 | continue; | |
1547 | ||
1548 | /* Skip the "this" parameter and, for constructors of | |
1549 | classes with virtual bases, the VTT parameter. A | |
1550 | full specialization of a constructor will have a VTT | |
1551 | parameter, but a template never will. */ | |
1552 | decl_arg_types | |
1553 | = skip_artificial_parms_for (decl, decl_arg_types); | |
1554 | fn_arg_types | |
1555 | = skip_artificial_parms_for (fn, fn_arg_types); | |
1556 | ||
1557 | /* Check that the number of function parameters matches. | |
1558 | For example, | |
1559 | template <class T> void f(int i = 0); | |
1560 | template <> void f<int>(); | |
1561 | The specialization f<int> is invalid but is not caught | |
1562 | by get_bindings below. */ | |
1563 | if (list_length (fn_arg_types) != list_length (decl_arg_types)) | |
1564 | continue; | |
1565 | ||
29a1da1c MM |
1566 | /* Function templates cannot be specializations; there are |
1567 | no partial specializations of functions. Therefore, if | |
1568 | the type of DECL does not match FN, there is no | |
1569 | match. */ | |
54062fc0 PC |
1570 | if (tsk == tsk_template) |
1571 | { | |
1572 | if (compparms (fn_arg_types, decl_arg_types)) | |
1573 | candidates = tree_cons (NULL_TREE, fn, candidates); | |
1574 | continue; | |
1575 | } | |
29a1da1c | 1576 | |
d955f6ea KL |
1577 | /* See whether this function might be a specialization of this |
1578 | template. */ | |
a34d3336 | 1579 | targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true); |
d955f6ea KL |
1580 | |
1581 | if (!targs) | |
1582 | /* We cannot deduce template arguments that when used to | |
1583 | specialize TMPL will produce DECL. */ | |
1584 | continue; | |
1585 | ||
1586 | /* Save this template, and the arguments deduced. */ | |
1587 | templates = tree_cons (targs, fn, templates); | |
1588 | } | |
03017874 MM |
1589 | else if (need_member_template) |
1590 | /* FN is an ordinary member function, and we need a | |
1591 | specialization of a member template. */ | |
d955f6ea | 1592 | ; |
03017874 MM |
1593 | else if (TREE_CODE (fn) != FUNCTION_DECL) |
1594 | /* We can get IDENTIFIER_NODEs here in certain erroneous | |
1595 | cases. */ | |
d955f6ea | 1596 | ; |
03017874 MM |
1597 | else if (!DECL_FUNCTION_MEMBER_P (fn)) |
1598 | /* This is just an ordinary non-member function. Nothing can | |
1599 | be a specialization of that. */ | |
d955f6ea | 1600 | ; |
3b82c249 KL |
1601 | else if (DECL_ARTIFICIAL (fn)) |
1602 | /* Cannot specialize functions that are created implicitly. */ | |
d955f6ea | 1603 | ; |
75650646 | 1604 | else |
03017874 MM |
1605 | { |
1606 | tree decl_arg_types; | |
386b8a85 | 1607 | |
03017874 MM |
1608 | /* This is an ordinary member function. However, since |
1609 | we're here, we can assume it's enclosing class is a | |
1610 | template class. For example, | |
c8094d83 | 1611 | |
03017874 MM |
1612 | template <typename T> struct S { void f(); }; |
1613 | template <> void S<int>::f() {} | |
1614 | ||
1615 | Here, S<int>::f is a non-template, but S<int> is a | |
1616 | template class. If FN has the same type as DECL, we | |
1617 | might be in business. */ | |
f5d47abd KL |
1618 | |
1619 | if (!DECL_TEMPLATE_INFO (fn)) | |
1620 | /* Its enclosing class is an explicit specialization | |
1621 | of a template class. This is not a candidate. */ | |
1622 | continue; | |
1623 | ||
03017874 MM |
1624 | if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)), |
1625 | TREE_TYPE (TREE_TYPE (fn)))) | |
1626 | /* The return types differ. */ | |
1627 | continue; | |
1628 | ||
1629 | /* Adjust the type of DECL in case FN is a static member. */ | |
1630 | decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl)); | |
c8094d83 | 1631 | if (DECL_STATIC_FUNCTION_P (fn) |
03017874 MM |
1632 | && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)) |
1633 | decl_arg_types = TREE_CHAIN (decl_arg_types); | |
1634 | ||
c8094d83 | 1635 | if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)), |
03017874 MM |
1636 | decl_arg_types)) |
1637 | /* They match! */ | |
1638 | candidates = tree_cons (NULL_TREE, fn, candidates); | |
03017874 | 1639 | } |
386b8a85 | 1640 | } |
03017874 | 1641 | |
bf8f3f93 | 1642 | if (templates && TREE_CHAIN (templates)) |
386b8a85 | 1643 | { |
03017874 | 1644 | /* We have: |
c8094d83 | 1645 | |
03017874 MM |
1646 | [temp.expl.spec] |
1647 | ||
1648 | It is possible for a specialization with a given function | |
1649 | signature to be instantiated from more than one function | |
1650 | template. In such cases, explicit specification of the | |
1651 | template arguments must be used to uniquely identify the | |
1652 | function template specialization being specialized. | |
1653 | ||
1654 | Note that here, there's no suggestion that we're supposed to | |
1655 | determine which of the candidate templates is most | |
1656 | specialized. However, we, also have: | |
1657 | ||
1658 | [temp.func.order] | |
1659 | ||
1660 | Partial ordering of overloaded function template | |
1661 | declarations is used in the following contexts to select | |
1662 | the function template to which a function template | |
c8094d83 | 1663 | specialization refers: |
03017874 | 1664 | |
0cbd7506 | 1665 | -- when an explicit specialization refers to a function |
c8094d83 | 1666 | template. |
03017874 MM |
1667 | |
1668 | So, we do use the partial ordering rules, at least for now. | |
0e339752 | 1669 | This extension can only serve to make invalid programs valid, |
03017874 MM |
1670 | so it's safe. And, there is strong anecdotal evidence that |
1671 | the committee intended the partial ordering rules to apply; | |
3b426391 | 1672 | the EDG front end has that behavior, and John Spicer claims |
03017874 MM |
1673 | that the committee simply forgot to delete the wording in |
1674 | [temp.expl.spec]. */ | |
7ca383e6 MM |
1675 | tree tmpl = most_specialized_instantiation (templates); |
1676 | if (tmpl != error_mark_node) | |
1677 | { | |
1678 | templates = tmpl; | |
1679 | TREE_CHAIN (templates) = NULL_TREE; | |
1680 | } | |
e1467ff2 MM |
1681 | } |
1682 | ||
03017874 | 1683 | if (templates == NULL_TREE && candidates == NULL_TREE) |
e1467ff2 | 1684 | { |
dee15844 JM |
1685 | error ("template-id %qD for %q+D does not match any template " |
1686 | "declaration", template_id, decl); | |
03017874 | 1687 | return error_mark_node; |
386b8a85 | 1688 | } |
03017874 | 1689 | else if ((templates && TREE_CHAIN (templates)) |
bf8f3f93 MM |
1690 | || (candidates && TREE_CHAIN (candidates)) |
1691 | || (templates && candidates)) | |
386b8a85 | 1692 | { |
dee15844 JM |
1693 | error ("ambiguous template specialization %qD for %q+D", |
1694 | template_id, decl); | |
bf8f3f93 MM |
1695 | chainon (candidates, templates); |
1696 | print_candidates (candidates); | |
03017874 | 1697 | return error_mark_node; |
386b8a85 JM |
1698 | } |
1699 | ||
c6002625 | 1700 | /* We have one, and exactly one, match. */ |
03017874 MM |
1701 | if (candidates) |
1702 | { | |
29a1da1c MM |
1703 | tree fn = TREE_VALUE (candidates); |
1704 | /* DECL is a re-declaration of a template function. */ | |
1705 | if (TREE_CODE (fn) == TEMPLATE_DECL) | |
1706 | return fn; | |
03017874 MM |
1707 | /* It was a specialization of an ordinary member function in a |
1708 | template class. */ | |
29a1da1c MM |
1709 | *targs_out = copy_node (DECL_TI_ARGS (fn)); |
1710 | return DECL_TI_TEMPLATE (fn); | |
03017874 MM |
1711 | } |
1712 | ||
1713 | /* It was a specialization of a template. */ | |
17aec3eb | 1714 | targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates))); |
03017874 MM |
1715 | if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs)) |
1716 | { | |
1717 | *targs_out = copy_node (targs); | |
c8094d83 | 1718 | SET_TMPL_ARGS_LEVEL (*targs_out, |
03017874 MM |
1719 | TMPL_ARGS_DEPTH (*targs_out), |
1720 | TREE_PURPOSE (templates)); | |
1721 | } | |
1722 | else | |
1723 | *targs_out = TREE_PURPOSE (templates); | |
e1467ff2 | 1724 | return TREE_VALUE (templates); |
8d08fdba | 1725 | } |
8afa707f MM |
1726 | |
1727 | /* Returns a chain of parameter types, exactly like the SPEC_TYPES, | |
1728 | but with the default argument values filled in from those in the | |
1729 | TMPL_TYPES. */ | |
c8094d83 | 1730 | |
8afa707f | 1731 | static tree |
3a978d72 NN |
1732 | copy_default_args_to_explicit_spec_1 (tree spec_types, |
1733 | tree tmpl_types) | |
8afa707f MM |
1734 | { |
1735 | tree new_spec_types; | |
1736 | ||
1737 | if (!spec_types) | |
1738 | return NULL_TREE; | |
1739 | ||
1740 | if (spec_types == void_list_node) | |
1741 | return void_list_node; | |
1742 | ||
1743 | /* Substitute into the rest of the list. */ | |
1744 | new_spec_types = | |
1745 | copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types), | |
1746 | TREE_CHAIN (tmpl_types)); | |
c8094d83 | 1747 | |
8afa707f MM |
1748 | /* Add the default argument for this parameter. */ |
1749 | return hash_tree_cons (TREE_PURPOSE (tmpl_types), | |
1750 | TREE_VALUE (spec_types), | |
1751 | new_spec_types); | |
1752 | } | |
1753 | ||
1754 | /* DECL is an explicit specialization. Replicate default arguments | |
1755 | from the template it specializes. (That way, code like: | |
1756 | ||
1757 | template <class T> void f(T = 3); | |
1758 | template <> void f(double); | |
c8094d83 | 1759 | void g () { f (); } |
8afa707f MM |
1760 | |
1761 | works, as required.) An alternative approach would be to look up | |
1762 | the correct default arguments at the call-site, but this approach | |
1763 | is consistent with how implicit instantiations are handled. */ | |
1764 | ||
1765 | static void | |
3a978d72 | 1766 | copy_default_args_to_explicit_spec (tree decl) |
8afa707f MM |
1767 | { |
1768 | tree tmpl; | |
1769 | tree spec_types; | |
1770 | tree tmpl_types; | |
1771 | tree new_spec_types; | |
1772 | tree old_type; | |
1773 | tree new_type; | |
1774 | tree t; | |
08c2df0f NS |
1775 | tree object_type = NULL_TREE; |
1776 | tree in_charge = NULL_TREE; | |
e0fff4b3 | 1777 | tree vtt = NULL_TREE; |
8afa707f MM |
1778 | |
1779 | /* See if there's anything we need to do. */ | |
1780 | tmpl = DECL_TI_TEMPLATE (decl); | |
1781 | tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl))); | |
1782 | for (t = tmpl_types; t; t = TREE_CHAIN (t)) | |
1783 | if (TREE_PURPOSE (t)) | |
1784 | break; | |
1785 | if (!t) | |
1786 | return; | |
1787 | ||
1788 | old_type = TREE_TYPE (decl); | |
1789 | spec_types = TYPE_ARG_TYPES (old_type); | |
c8094d83 | 1790 | |
8afa707f MM |
1791 | if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)) |
1792 | { | |
08c2df0f | 1793 | /* Remove the this pointer, but remember the object's type for |
0cbd7506 | 1794 | CV quals. */ |
08c2df0f | 1795 | object_type = TREE_TYPE (TREE_VALUE (spec_types)); |
8afa707f MM |
1796 | spec_types = TREE_CHAIN (spec_types); |
1797 | tmpl_types = TREE_CHAIN (tmpl_types); | |
c8094d83 | 1798 | |
8afa707f | 1799 | if (DECL_HAS_IN_CHARGE_PARM_P (decl)) |
0cbd7506 MS |
1800 | { |
1801 | /* DECL may contain more parameters than TMPL due to the extra | |
1802 | in-charge parameter in constructors and destructors. */ | |
1803 | in_charge = spec_types; | |
08c2df0f NS |
1804 | spec_types = TREE_CHAIN (spec_types); |
1805 | } | |
e0fff4b3 JM |
1806 | if (DECL_HAS_VTT_PARM_P (decl)) |
1807 | { | |
1808 | vtt = spec_types; | |
1809 | spec_types = TREE_CHAIN (spec_types); | |
1810 | } | |
8afa707f MM |
1811 | } |
1812 | ||
1813 | /* Compute the merged default arguments. */ | |
c8094d83 | 1814 | new_spec_types = |
8afa707f MM |
1815 | copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types); |
1816 | ||
08c2df0f NS |
1817 | /* Compute the new FUNCTION_TYPE. */ |
1818 | if (object_type) | |
8afa707f | 1819 | { |
e0fff4b3 | 1820 | if (vtt) |
0cbd7506 MS |
1821 | new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt), |
1822 | TREE_VALUE (vtt), | |
1823 | new_spec_types); | |
e0fff4b3 | 1824 | |
08c2df0f | 1825 | if (in_charge) |
0cbd7506 MS |
1826 | /* Put the in-charge parameter back. */ |
1827 | new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge), | |
1828 | TREE_VALUE (in_charge), | |
1829 | new_spec_types); | |
08c2df0f | 1830 | |
43dc123f MM |
1831 | new_type = build_method_type_directly (object_type, |
1832 | TREE_TYPE (old_type), | |
1833 | new_spec_types); | |
8afa707f | 1834 | } |
8afa707f MM |
1835 | else |
1836 | new_type = build_function_type (TREE_TYPE (old_type), | |
08c2df0f | 1837 | new_spec_types); |
e9525111 MM |
1838 | new_type = cp_build_type_attribute_variant (new_type, |
1839 | TYPE_ATTRIBUTES (old_type)); | |
8afa707f MM |
1840 | new_type = build_exception_variant (new_type, |
1841 | TYPE_RAISES_EXCEPTIONS (old_type)); | |
1842 | TREE_TYPE (decl) = new_type; | |
1843 | } | |
1844 | ||
386b8a85 | 1845 | /* Check to see if the function just declared, as indicated in |
75650646 MM |
1846 | DECLARATOR, and in DECL, is a specialization of a function |
1847 | template. We may also discover that the declaration is an explicit | |
1848 | instantiation at this point. | |
1849 | ||
e1467ff2 | 1850 | Returns DECL, or an equivalent declaration that should be used |
03017874 MM |
1851 | instead if all goes well. Issues an error message if something is |
1852 | amiss. Returns error_mark_node if the error is not easily | |
1853 | recoverable. | |
c8094d83 MS |
1854 | |
1855 | FLAGS is a bitmask consisting of the following flags: | |
75650646 | 1856 | |
75650646 MM |
1857 | 2: The function has a definition. |
1858 | 4: The function is a friend. | |
75650646 MM |
1859 | |
1860 | The TEMPLATE_COUNT is the number of references to qualifying | |
1861 | template classes that appeared in the name of the function. For | |
1862 | example, in | |
1863 | ||
1864 | template <class T> struct S { void f(); }; | |
1865 | void S<int>::f(); | |
c8094d83 | 1866 | |
75650646 MM |
1867 | the TEMPLATE_COUNT would be 1. However, explicitly specialized |
1868 | classes are not counted in the TEMPLATE_COUNT, so that in | |
1869 | ||
1870 | template <class T> struct S {}; | |
1871 | template <> struct S<int> { void f(); } | |
36a117a5 | 1872 | template <> void S<int>::f(); |
75650646 MM |
1873 | |
1874 | the TEMPLATE_COUNT would be 0. (Note that this declaration is | |
0e339752 | 1875 | invalid; there should be no template <>.) |
75650646 MM |
1876 | |
1877 | If the function is a specialization, it is marked as such via | |
1878 | DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO | |
c8094d83 | 1879 | is set up correctly, and it is added to the list of specializations |
75650646 | 1880 | for that template. */ |
386b8a85 | 1881 | |
e1467ff2 | 1882 | tree |
c8094d83 | 1883 | check_explicit_specialization (tree declarator, |
0cbd7506 MS |
1884 | tree decl, |
1885 | int template_count, | |
1886 | int flags) | |
386b8a85 | 1887 | { |
75650646 MM |
1888 | int have_def = flags & 2; |
1889 | int is_friend = flags & 4; | |
1890 | int specialization = 0; | |
e1467ff2 | 1891 | int explicit_instantiation = 0; |
fd4de5ff | 1892 | int member_specialization = 0; |
75650646 MM |
1893 | tree ctype = DECL_CLASS_CONTEXT (decl); |
1894 | tree dname = DECL_NAME (decl); | |
74b846e0 | 1895 | tmpl_spec_kind tsk; |
386b8a85 | 1896 | |
f65b7de3 GB |
1897 | if (is_friend) |
1898 | { | |
1899 | if (!processing_specialization) | |
1900 | tsk = tsk_none; | |
1901 | else | |
1902 | tsk = tsk_excessive_parms; | |
1903 | } | |
1904 | else | |
1905 | tsk = current_tmpl_spec_kind (template_count); | |
75650646 | 1906 | |
74b846e0 MM |
1907 | switch (tsk) |
1908 | { | |
1909 | case tsk_none: | |
c8094d83 | 1910 | if (processing_specialization) |
75650646 | 1911 | { |
fd4de5ff MM |
1912 | specialization = 1; |
1913 | SET_DECL_TEMPLATE_SPECIALIZATION (decl); | |
75650646 | 1914 | } |
74b846e0 | 1915 | else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR) |
386b8a85 | 1916 | { |
74b846e0 MM |
1917 | if (is_friend) |
1918 | /* This could be something like: | |
75650646 | 1919 | |
74b846e0 MM |
1920 | template <class T> void f(T); |
1921 | class S { friend void f<>(int); } */ | |
1922 | specialization = 1; | |
1923 | else | |
1924 | { | |
1925 | /* This case handles bogus declarations like template <> | |
1926 | template <class T> void f<int>(); */ | |
1927 | ||
0f51ccfc | 1928 | error ("template-id %qD in declaration of primary template", |
0cbd7506 | 1929 | declarator); |
74b846e0 MM |
1930 | return decl; |
1931 | } | |
1932 | } | |
1933 | break; | |
1934 | ||
1935 | case tsk_invalid_member_spec: | |
1936 | /* The error has already been reported in | |
1937 | check_specialization_scope. */ | |
1938 | return error_mark_node; | |
1939 | ||
1940 | case tsk_invalid_expl_inst: | |
33bd39a2 | 1941 | error ("template parameter list used in explicit instantiation"); |
74b846e0 MM |
1942 | |
1943 | /* Fall through. */ | |
1944 | ||
1945 | case tsk_expl_inst: | |
fd4de5ff | 1946 | if (have_def) |
33bd39a2 | 1947 | error ("definition provided for explicit instantiation"); |
c8094d83 | 1948 | |
fd4de5ff | 1949 | explicit_instantiation = 1; |
74b846e0 | 1950 | break; |
fd4de5ff | 1951 | |
74b846e0 | 1952 | case tsk_excessive_parms: |
f03adc6b GB |
1953 | case tsk_insufficient_parms: |
1954 | if (tsk == tsk_excessive_parms) | |
0cbd7506 | 1955 | error ("too many template parameter lists in declaration of %qD", |
f03adc6b GB |
1956 | decl); |
1957 | else if (template_header_count) | |
0f51ccfc | 1958 | error("too few template parameter lists in declaration of %qD", decl); |
f03adc6b | 1959 | else |
0f51ccfc | 1960 | error("explicit specialization of %qD must be introduced by " |
9e637a26 | 1961 | "%<template <>%>", decl); |
75650646 | 1962 | |
74b846e0 MM |
1963 | /* Fall through. */ |
1964 | case tsk_expl_spec: | |
1965 | SET_DECL_TEMPLATE_SPECIALIZATION (decl); | |
1966 | if (ctype) | |
1967 | member_specialization = 1; | |
1968 | else | |
1969 | specialization = 1; | |
1970 | break; | |
74b846e0 MM |
1971 | |
1972 | case tsk_template: | |
1973 | if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR) | |
c6f2ed0d | 1974 | { |
fd4de5ff MM |
1975 | /* This case handles bogus declarations like template <> |
1976 | template <class T> void f<int>(); */ | |
75650646 | 1977 | |
655dc6ee | 1978 | if (uses_template_parms (declarator)) |
0f51ccfc | 1979 | error ("function template partial specialization %qD " |
e1e93ad8 | 1980 | "is not allowed", declarator); |
655dc6ee | 1981 | else |
0f51ccfc | 1982 | error ("template-id %qD in declaration of primary template", |
0cbd7506 | 1983 | declarator); |
fd4de5ff | 1984 | return decl; |
386b8a85 | 1985 | } |
74b846e0 MM |
1986 | |
1987 | if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype)) | |
1988 | /* This is a specialization of a member template, without | |
1989 | specialization the containing class. Something like: | |
1990 | ||
1991 | template <class T> struct S { | |
c8094d83 | 1992 | template <class U> void f (U); |
0cbd7506 | 1993 | }; |
74b846e0 | 1994 | template <> template <class U> void S<int>::f(U) {} |
c8094d83 | 1995 | |
74b846e0 MM |
1996 | That's a specialization -- but of the entire template. */ |
1997 | specialization = 1; | |
1998 | break; | |
1999 | ||
2000 | default: | |
315fb5db | 2001 | gcc_unreachable (); |
75650646 | 2002 | } |
386b8a85 | 2003 | |
670960ac JM |
2004 | if (specialization || member_specialization) |
2005 | { | |
2006 | tree t = TYPE_ARG_TYPES (TREE_TYPE (decl)); | |
2007 | for (; t; t = TREE_CHAIN (t)) | |
2008 | if (TREE_PURPOSE (t)) | |
2009 | { | |
cbe5f3b3 MLI |
2010 | permerror (input_location, |
2011 | "default argument specified in explicit specialization"); | |
670960ac JM |
2012 | break; |
2013 | } | |
2014 | } | |
2015 | ||
e1467ff2 | 2016 | if (specialization || member_specialization || explicit_instantiation) |
75650646 MM |
2017 | { |
2018 | tree tmpl = NULL_TREE; | |
2019 | tree targs = NULL_TREE; | |
75650646 MM |
2020 | |
2021 | /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */ | |
386b8a85 JM |
2022 | if (TREE_CODE (declarator) != TEMPLATE_ID_EXPR) |
2023 | { | |
2024 | tree fns; | |
2025 | ||
50bc768d | 2026 | gcc_assert (TREE_CODE (declarator) == IDENTIFIER_NODE); |
2f54a1db | 2027 | if (ctype) |
386b8a85 | 2028 | fns = dname; |
2f54a1db GB |
2029 | else |
2030 | { | |
2031 | /* If there is no class context, the explicit instantiation | |
0cbd7506 | 2032 | must be at namespace scope. */ |
50bc768d | 2033 | gcc_assert (DECL_NAMESPACE_SCOPE_P (decl)); |
2f54a1db GB |
2034 | |
2035 | /* Find the namespace binding, using the declaration | |
0cbd7506 | 2036 | context. */ |
ddf74938 | 2037 | fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname, |
664a90c0 | 2038 | false, true); |
97dc8e5b | 2039 | if (fns == error_mark_node || !is_overloaded_fn (fns)) |
4230cec2 NS |
2040 | { |
2041 | error ("%qD is not a template function", dname); | |
2042 | fns = error_mark_node; | |
2043 | } | |
664a90c0 JM |
2044 | else |
2045 | { | |
2046 | tree fn = OVL_CURRENT (fns); | |
ddf74938 | 2047 | if (!is_associated_namespace (CP_DECL_CONTEXT (decl), |
08d295c5 JM |
2048 | CP_DECL_CONTEXT (fn))) |
2049 | error ("%qD is not declared in %qD", | |
2050 | decl, current_namespace); | |
664a90c0 | 2051 | } |
2f54a1db | 2052 | } |
386b8a85 | 2053 | |
2f54a1db | 2054 | declarator = lookup_template_function (fns, NULL_TREE); |
386b8a85 JM |
2055 | } |
2056 | ||
f2e48b67 BK |
2057 | if (declarator == error_mark_node) |
2058 | return error_mark_node; | |
2059 | ||
75650646 MM |
2060 | if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype)) |
2061 | { | |
8ec2ac34 | 2062 | if (!explicit_instantiation) |
0e339752 | 2063 | /* A specialization in class scope. This is invalid, |
6c30752f MM |
2064 | but the error will already have been flagged by |
2065 | check_specialization_scope. */ | |
2066 | return error_mark_node; | |
8ec2ac34 | 2067 | else |
b370501f | 2068 | { |
0e339752 | 2069 | /* It's not valid to write an explicit instantiation in |
b370501f | 2070 | class scope, e.g.: |
8ec2ac34 | 2071 | |
0cbd7506 | 2072 | class C { template void f(); } |
8ec2ac34 | 2073 | |
b370501f KG |
2074 | This case is caught by the parser. However, on |
2075 | something like: | |
c8094d83 | 2076 | |
b370501f | 2077 | template class C { void f(); }; |
8ec2ac34 | 2078 | |
0e339752 | 2079 | (which is invalid) we can get here. The error will be |
b370501f KG |
2080 | issued later. */ |
2081 | ; | |
2082 | } | |
8ec2ac34 | 2083 | |
e1467ff2 | 2084 | return decl; |
75650646 | 2085 | } |
c8094d83 | 2086 | else if (ctype != NULL_TREE |
75650646 MM |
2087 | && (TREE_CODE (TREE_OPERAND (declarator, 0)) == |
2088 | IDENTIFIER_NODE)) | |
386b8a85 | 2089 | { |
75650646 MM |
2090 | /* Find the list of functions in ctype that have the same |
2091 | name as the declared function. */ | |
2092 | tree name = TREE_OPERAND (declarator, 0); | |
03017874 MM |
2093 | tree fns = NULL_TREE; |
2094 | int idx; | |
2095 | ||
8ba658ee | 2096 | if (constructor_name_p (name, ctype)) |
386b8a85 | 2097 | { |
75650646 | 2098 | int is_constructor = DECL_CONSTRUCTOR_P (decl); |
c8094d83 | 2099 | |
0fcedd9c | 2100 | if (is_constructor ? !TYPE_HAS_USER_CONSTRUCTOR (ctype) |
9f4faeae | 2101 | : !CLASSTYPE_DESTRUCTORS (ctype)) |
75650646 MM |
2102 | { |
2103 | /* From [temp.expl.spec]: | |
c8094d83 | 2104 | |
75650646 MM |
2105 | If such an explicit specialization for the member |
2106 | of a class template names an implicitly-declared | |
2107 | special member function (clause _special_), the | |
c8094d83 | 2108 | program is ill-formed. |
e1467ff2 MM |
2109 | |
2110 | Similar language is found in [temp.explicit]. */ | |
33bd39a2 | 2111 | error ("specialization of implicitly-declared special member function"); |
03017874 | 2112 | return error_mark_node; |
75650646 | 2113 | } |
386b8a85 | 2114 | |
42da2fd8 | 2115 | name = is_constructor ? ctor_identifier : dtor_identifier; |
75650646 | 2116 | } |
42da2fd8 | 2117 | |
421844e7 | 2118 | if (!DECL_CONV_FN_P (decl)) |
03017874 MM |
2119 | { |
2120 | idx = lookup_fnfields_1 (ctype, name); | |
2121 | if (idx >= 0) | |
aaaa46d2 | 2122 | fns = VEC_index (tree, CLASSTYPE_METHOD_VEC (ctype), idx); |
03017874 MM |
2123 | } |
2124 | else | |
2125 | { | |
d4e6fecb | 2126 | VEC(tree,gc) *methods; |
aaaa46d2 | 2127 | tree ovl; |
03017874 MM |
2128 | |
2129 | /* For a type-conversion operator, we cannot do a | |
2130 | name-based lookup. We might be looking for `operator | |
2131 | int' which will be a specialization of `operator T'. | |
2132 | So, we find *all* the conversion operators, and then | |
2133 | select from them. */ | |
2134 | fns = NULL_TREE; | |
2135 | ||
2136 | methods = CLASSTYPE_METHOD_VEC (ctype); | |
2137 | if (methods) | |
5dd236e2 | 2138 | for (idx = CLASSTYPE_FIRST_CONVERSION_SLOT; |
9ba5ff0f | 2139 | VEC_iterate (tree, methods, idx, ovl); |
aaaa46d2 | 2140 | ++idx) |
03017874 | 2141 | { |
aaaa46d2 | 2142 | if (!DECL_CONV_FN_P (OVL_CURRENT (ovl))) |
03017874 MM |
2143 | /* There are no more conversion functions. */ |
2144 | break; | |
2145 | ||
2146 | /* Glue all these conversion functions together | |
2147 | with those we already have. */ | |
2148 | for (; ovl; ovl = OVL_NEXT (ovl)) | |
2149 | fns = ovl_cons (OVL_CURRENT (ovl), fns); | |
2150 | } | |
2151 | } | |
c8094d83 MS |
2152 | |
2153 | if (fns == NULL_TREE) | |
386b8a85 | 2154 | { |
0f51ccfc | 2155 | error ("no member function %qD declared in %qT", name, ctype); |
03017874 | 2156 | return error_mark_node; |
386b8a85 JM |
2157 | } |
2158 | else | |
2159 | TREE_OPERAND (declarator, 0) = fns; | |
2160 | } | |
c8094d83 | 2161 | |
e1467ff2 MM |
2162 | /* Figure out what exactly is being specialized at this point. |
2163 | Note that for an explicit instantiation, even one for a | |
38e01259 | 2164 | member function, we cannot tell apriori whether the |
e1467ff2 | 2165 | instantiation is for a member template, or just a member |
36a117a5 MM |
2166 | function of a template class. Even if a member template is |
2167 | being instantiated, the member template arguments may be | |
2168 | elided if they can be deduced from the rest of the | |
2169 | declaration. */ | |
e1467ff2 | 2170 | tmpl = determine_specialization (declarator, decl, |
c8094d83 | 2171 | &targs, |
5fe7b654 | 2172 | member_specialization, |
29a1da1c MM |
2173 | template_count, |
2174 | tsk); | |
c8094d83 | 2175 | |
03017874 MM |
2176 | if (!tmpl || tmpl == error_mark_node) |
2177 | /* We couldn't figure out what this declaration was | |
2178 | specializing. */ | |
2179 | return error_mark_node; | |
2180 | else | |
386b8a85 | 2181 | { |
25aab5d0 | 2182 | tree gen_tmpl = most_general_template (tmpl); |
36a117a5 | 2183 | |
e1467ff2 MM |
2184 | if (explicit_instantiation) |
2185 | { | |
03d0f4af | 2186 | /* We don't set DECL_EXPLICIT_INSTANTIATION here; that |
c8094d83 | 2187 | is done by do_decl_instantiation later. */ |
25aab5d0 MM |
2188 | |
2189 | int arg_depth = TMPL_ARGS_DEPTH (targs); | |
2190 | int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)); | |
2191 | ||
2192 | if (arg_depth > parm_depth) | |
2193 | { | |
2194 | /* If TMPL is not the most general template (for | |
2195 | example, if TMPL is a friend template that is | |
2196 | injected into namespace scope), then there will | |
dc957d14 | 2197 | be too many levels of TARGS. Remove some of them |
25aab5d0 MM |
2198 | here. */ |
2199 | int i; | |
2200 | tree new_targs; | |
2201 | ||
f31c0a32 | 2202 | new_targs = make_tree_vec (parm_depth); |
25aab5d0 MM |
2203 | for (i = arg_depth - parm_depth; i < arg_depth; ++i) |
2204 | TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth)) | |
2205 | = TREE_VEC_ELT (targs, i); | |
2206 | targs = new_targs; | |
2207 | } | |
c8094d83 | 2208 | |
3e4a3562 | 2209 | return instantiate_template (tmpl, targs, tf_error); |
e1467ff2 | 2210 | } |
74b846e0 | 2211 | |
6c07f448 KL |
2212 | /* If we thought that the DECL was a member function, but it |
2213 | turns out to be specializing a static member function, | |
4546865e | 2214 | make DECL a static member function as well. */ |
6c07f448 KL |
2215 | if (DECL_STATIC_FUNCTION_P (tmpl) |
2216 | && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)) | |
4546865e | 2217 | revert_static_member_fn (decl); |
6c07f448 | 2218 | |
f9a7ae04 | 2219 | /* If this is a specialization of a member template of a |
29a1da1c MM |
2220 | template class, we want to return the TEMPLATE_DECL, not |
2221 | the specialization of it. */ | |
74b846e0 MM |
2222 | if (tsk == tsk_template) |
2223 | { | |
448545cb | 2224 | tree result = DECL_TEMPLATE_RESULT (tmpl); |
74b846e0 | 2225 | SET_DECL_TEMPLATE_SPECIALIZATION (tmpl); |
448545cb | 2226 | DECL_INITIAL (result) = NULL_TREE; |
b190f239 NS |
2227 | if (have_def) |
2228 | { | |
448545cb | 2229 | tree parm; |
f31686a3 | 2230 | DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl); |
448545cb | 2231 | DECL_SOURCE_LOCATION (result) |
f31686a3 | 2232 | = DECL_SOURCE_LOCATION (decl); |
08167d1c AO |
2233 | /* We want to use the argument list specified in the |
2234 | definition, not in the original declaration. */ | |
448545cb JM |
2235 | DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl); |
2236 | for (parm = DECL_ARGUMENTS (result); parm; | |
2237 | parm = TREE_CHAIN (parm)) | |
2238 | DECL_CONTEXT (parm) = result; | |
b190f239 | 2239 | } |
74b846e0 MM |
2240 | return tmpl; |
2241 | } | |
2242 | ||
36a117a5 | 2243 | /* Set up the DECL_TEMPLATE_INFO for DECL. */ |
e1b3e07d | 2244 | DECL_TEMPLATE_INFO (decl) = tree_cons (tmpl, targs, NULL_TREE); |
36a117a5 | 2245 | |
8afa707f MM |
2246 | /* Inherit default function arguments from the template |
2247 | DECL is specializing. */ | |
2248 | copy_default_args_to_explicit_spec (decl); | |
2249 | ||
c750255c MM |
2250 | /* This specialization has the same protection as the |
2251 | template it specializes. */ | |
2252 | TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl); | |
2253 | TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl); | |
b9e75696 | 2254 | |
736b8100 SB |
2255 | /* 7.1.1-1 [dcl.stc] |
2256 | ||
2257 | A storage-class-specifier shall not be specified in an | |
2258 | explicit specialization... | |
2259 | ||
2260 | The parser rejects these, so unless action is taken here, | |
2261 | explicit function specializations will always appear with | |
2262 | global linkage. | |
2263 | ||
2264 | The action recommended by the C++ CWG in response to C++ | |
2265 | defect report 605 is to make the storage class and linkage | |
2266 | of the explicit specialization match the templated function: | |
2267 | ||
2268 | http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605 | |
2269 | */ | |
2270 | if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl)) | |
2271 | { | |
2272 | tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl); | |
2273 | gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL); | |
2274 | ||
d732e98f | 2275 | /* This specialization has the same linkage and visibility as |
736b8100 SB |
2276 | the function template it specializes. */ |
2277 | TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func); | |
92d0af97 JJ |
2278 | if (! TREE_PUBLIC (decl)) |
2279 | { | |
2280 | DECL_INTERFACE_KNOWN (decl) = 1; | |
2281 | DECL_NOT_REALLY_EXTERN (decl) = 1; | |
2282 | } | |
736b8100 SB |
2283 | DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func); |
2284 | if (DECL_VISIBILITY_SPECIFIED (tmpl_func)) | |
2285 | { | |
2286 | DECL_VISIBILITY_SPECIFIED (decl) = 1; | |
2287 | DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func); | |
2288 | } | |
2289 | } | |
2290 | ||
9c12301f MM |
2291 | /* If DECL is a friend declaration, declared using an |
2292 | unqualified name, the namespace associated with DECL may | |
2293 | have been set incorrectly. For example, in: | |
3db45ab5 MS |
2294 | |
2295 | template <typename T> void f(T); | |
2296 | namespace N { | |
2297 | struct S { friend void f<int>(int); } | |
2298 | } | |
2299 | ||
2300 | we will have set the DECL_CONTEXT for the friend | |
2301 | declaration to N, rather than to the global namespace. */ | |
9c12301f MM |
2302 | if (DECL_NAMESPACE_SCOPE_P (decl)) |
2303 | DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl); | |
8d039470 | 2304 | |
386b8a85 | 2305 | if (is_friend && !have_def) |
36a117a5 MM |
2306 | /* This is not really a declaration of a specialization. |
2307 | It's just the name of an instantiation. But, it's not | |
2308 | a request for an instantiation, either. */ | |
fbf1c34b | 2309 | SET_DECL_IMPLICIT_INSTANTIATION (decl); |
08511114 KL |
2310 | else if (DECL_CONSTRUCTOR_P (decl) || DECL_DESTRUCTOR_P (decl)) |
2311 | /* This is indeed a specialization. In case of constructors | |
2312 | and destructors, we need in-charge and not-in-charge | |
2313 | versions in V3 ABI. */ | |
2314 | clone_function_decl (decl, /*update_method_vec_p=*/0); | |
75650646 | 2315 | |
36a117a5 MM |
2316 | /* Register this specialization so that we can find it |
2317 | again. */ | |
d63d5d0c | 2318 | decl = register_specialization (decl, gen_tmpl, targs, is_friend); |
386b8a85 JM |
2319 | } |
2320 | } | |
c8094d83 | 2321 | |
e1467ff2 | 2322 | return decl; |
386b8a85 | 2323 | } |
75650646 | 2324 | |
75650646 MM |
2325 | /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template |
2326 | parameters. These are represented in the same format used for | |
2327 | DECL_TEMPLATE_PARMS. */ | |
2328 | ||
50bc768d | 2329 | int |
58f9752a | 2330 | comp_template_parms (const_tree parms1, const_tree parms2) |
75650646 | 2331 | { |
58f9752a KG |
2332 | const_tree p1; |
2333 | const_tree p2; | |
75650646 MM |
2334 | |
2335 | if (parms1 == parms2) | |
2336 | return 1; | |
2337 | ||
c8094d83 | 2338 | for (p1 = parms1, p2 = parms2; |
75650646 MM |
2339 | p1 != NULL_TREE && p2 != NULL_TREE; |
2340 | p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2)) | |
2341 | { | |
2342 | tree t1 = TREE_VALUE (p1); | |
2343 | tree t2 = TREE_VALUE (p2); | |
2344 | int i; | |
2345 | ||
50bc768d NS |
2346 | gcc_assert (TREE_CODE (t1) == TREE_VEC); |
2347 | gcc_assert (TREE_CODE (t2) == TREE_VEC); | |
75650646 MM |
2348 | |
2349 | if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2)) | |
2350 | return 0; | |
2351 | ||
c8094d83 | 2352 | for (i = 0; i < TREE_VEC_LENGTH (t2); ++i) |
75650646 | 2353 | { |
0f67a82f LM |
2354 | tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i)); |
2355 | tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i)); | |
42b304f1 | 2356 | |
0f67a82f LM |
2357 | /* If either of the template parameters are invalid, assume |
2358 | they match for the sake of error recovery. */ | |
2359 | if (parm1 == error_mark_node || parm2 == error_mark_node) | |
2360 | return 1; | |
75650646 MM |
2361 | |
2362 | if (TREE_CODE (parm1) != TREE_CODE (parm2)) | |
2363 | return 0; | |
2364 | ||
5d80a306 DG |
2365 | if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM |
2366 | && (TEMPLATE_TYPE_PARAMETER_PACK (parm1) | |
2367 | == TEMPLATE_TYPE_PARAMETER_PACK (parm2))) | |
75650646 | 2368 | continue; |
3bfdc719 | 2369 | else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2))) |
75650646 MM |
2370 | return 0; |
2371 | } | |
2372 | } | |
2373 | ||
2374 | if ((p1 != NULL_TREE) != (p2 != NULL_TREE)) | |
2375 | /* One set of parameters has more parameters lists than the | |
2376 | other. */ | |
2377 | return 0; | |
2378 | ||
2379 | return 1; | |
2380 | } | |
2381 | ||
5d80a306 DG |
2382 | /* Determine whether PARM is a parameter pack. */ |
2383 | bool | |
58f9752a | 2384 | template_parameter_pack_p (const_tree parm) |
5d80a306 DG |
2385 | { |
2386 | /* Determine if we have a non-type template parameter pack. */ | |
2387 | if (TREE_CODE (parm) == PARM_DECL) | |
2388 | return (DECL_TEMPLATE_PARM_P (parm) | |
2389 | && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))); | |
2390 | ||
2391 | /* If this is a list of template parameters, we could get a | |
2392 | TYPE_DECL or a TEMPLATE_DECL. */ | |
2393 | if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL) | |
2394 | parm = TREE_TYPE (parm); | |
2395 | ||
2396 | return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM | |
2397 | || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM) | |
2398 | && TEMPLATE_TYPE_PARAMETER_PACK (parm)); | |
2399 | } | |
2400 | ||
5d80a306 DG |
2401 | /* Determine whether ARGS describes a variadic template args list, |
2402 | i.e., one that is terminated by a template argument pack. */ | |
2403 | static bool | |
2404 | template_args_variadic_p (tree args) | |
2405 | { | |
2406 | int nargs; | |
2407 | tree last_parm; | |
2408 | ||
2409 | if (args == NULL_TREE) | |
2410 | return false; | |
2411 | ||
2412 | args = INNERMOST_TEMPLATE_ARGS (args); | |
2413 | nargs = TREE_VEC_LENGTH (args); | |
2414 | ||
2415 | if (nargs == 0) | |
2416 | return false; | |
2417 | ||
2418 | last_parm = TREE_VEC_ELT (args, nargs - 1); | |
2419 | ||
2420 | return ARGUMENT_PACK_P (last_parm); | |
2421 | } | |
2422 | ||
2423 | /* Generate a new name for the parameter pack name NAME (an | |
2424 | IDENTIFIER_NODE) that incorporates its */ | |
2425 | static tree | |
2426 | make_ith_pack_parameter_name (tree name, int i) | |
2427 | { | |
2428 | /* Munge the name to include the parameter index. */ | |
2429 | char numbuf[128]; | |
2430 | char* newname; | |
2431 | ||
2432 | sprintf(numbuf, "%i", i); | |
2433 | newname = (char*)alloca (IDENTIFIER_LENGTH (name) + strlen(numbuf) + 2); | |
2434 | sprintf(newname, "%s#%i", IDENTIFIER_POINTER (name), i); | |
2435 | return get_identifier (newname); | |
2436 | } | |
2437 | ||
1ad8aeeb | 2438 | /* Structure used to track the progress of find_parameter_packs_r. */ |
5d80a306 DG |
2439 | struct find_parameter_pack_data |
2440 | { | |
1ad8aeeb DG |
2441 | /* TREE_LIST that will contain all of the parameter packs found by |
2442 | the traversal. */ | |
5d80a306 | 2443 | tree* parameter_packs; |
1ad8aeeb DG |
2444 | |
2445 | /* Set of AST nodes that have been visited by the traversal. */ | |
5d80a306 DG |
2446 | struct pointer_set_t *visited; |
2447 | }; | |
2448 | ||
88b82314 | 2449 | /* Identifies all of the argument packs that occur in a template |
5d80a306 | 2450 | argument and appends them to the TREE_LIST inside DATA, which is a |
88b82314 | 2451 | find_parameter_pack_data structure. This is a subroutine of |
5d80a306 DG |
2452 | make_pack_expansion and uses_parameter_packs. */ |
2453 | static tree | |
2454 | find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data) | |
2455 | { | |
2456 | tree t = *tp; | |
2457 | struct find_parameter_pack_data* ppd = | |
2458 | (struct find_parameter_pack_data*)data; | |
1ad8aeeb | 2459 | bool parameter_pack_p = false; |
5d80a306 | 2460 | |
1ad8aeeb DG |
2461 | /* Identify whether this is a parameter pack or not. */ |
2462 | switch (TREE_CODE (t)) | |
2463 | { | |
2464 | case TEMPLATE_PARM_INDEX: | |
2465 | if (TEMPLATE_PARM_PARAMETER_PACK (t)) | |
2466 | parameter_pack_p = true; | |
2467 | break; | |
2468 | ||
2469 | case TEMPLATE_TYPE_PARM: | |
2470 | case TEMPLATE_TEMPLATE_PARM: | |
2471 | if (TEMPLATE_TYPE_PARAMETER_PACK (t)) | |
2472 | parameter_pack_p = true; | |
2473 | break; | |
2474 | ||
2475 | case PARM_DECL: | |
2476 | if (FUNCTION_PARAMETER_PACK_P (t)) | |
2477 | { | |
2478 | /* We don't want to walk into the type of a PARM_DECL, | |
2479 | because we don't want to see the type parameter pack. */ | |
2480 | *walk_subtrees = 0; | |
2481 | parameter_pack_p = true; | |
2482 | } | |
2483 | break; | |
2484 | ||
2485 | default: | |
2486 | /* Not a parameter pack. */ | |
2487 | break; | |
2488 | } | |
2489 | ||
2490 | if (parameter_pack_p) | |
2491 | { | |
2492 | /* Add this parameter pack to the list. */ | |
2493 | *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs); | |
1ad8aeeb | 2494 | } |
7b3e2d46 | 2495 | |
1ad8aeeb DG |
2496 | if (TYPE_P (t)) |
2497 | cp_walk_tree (&TYPE_CONTEXT (t), | |
92fab505 | 2498 | &find_parameter_packs_r, ppd, ppd->visited); |
1ad8aeeb | 2499 | |
5d80a306 DG |
2500 | /* This switch statement will return immediately if we don't find a |
2501 | parameter pack. */ | |
2502 | switch (TREE_CODE (t)) | |
2503 | { | |
2504 | case TEMPLATE_PARM_INDEX: | |
5d80a306 DG |
2505 | return NULL_TREE; |
2506 | ||
2507 | case BOUND_TEMPLATE_TEMPLATE_PARM: | |
1ad8aeeb DG |
2508 | /* Check the template itself. */ |
2509 | cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)), | |
92fab505 | 2510 | &find_parameter_packs_r, ppd, ppd->visited); |
5d80a306 | 2511 | /* Check the template arguments. */ |
14588106 | 2512 | cp_walk_tree (&TYPE_TI_ARGS (t), &find_parameter_packs_r, ppd, |
92fab505 | 2513 | ppd->visited); |
5d80a306 | 2514 | *walk_subtrees = 0; |
1ad8aeeb | 2515 | return NULL_TREE; |
5d80a306 DG |
2516 | |
2517 | case TEMPLATE_TYPE_PARM: | |
2518 | case TEMPLATE_TEMPLATE_PARM: | |
5d80a306 DG |
2519 | return NULL_TREE; |
2520 | ||
2521 | case PARM_DECL: | |
5d80a306 DG |
2522 | return NULL_TREE; |
2523 | ||
2524 | case RECORD_TYPE: | |
2525 | if (TYPE_PTRMEMFUNC_P (t)) | |
2526 | return NULL_TREE; | |
2527 | /* Fall through. */ | |
2528 | ||
2529 | case UNION_TYPE: | |
2530 | case ENUMERAL_TYPE: | |
2531 | if (TYPE_TEMPLATE_INFO (t)) | |
1ad8aeeb | 2532 | cp_walk_tree (&TREE_VALUE (TYPE_TEMPLATE_INFO (t)), |
92fab505 | 2533 | &find_parameter_packs_r, ppd, ppd->visited); |
5d80a306 DG |
2534 | |
2535 | *walk_subtrees = 0; | |
2536 | return NULL_TREE; | |
2537 | ||
2538 | case TEMPLATE_DECL: | |
1ad8aeeb | 2539 | cp_walk_tree (&TREE_TYPE (t), |
92fab505 | 2540 | &find_parameter_packs_r, ppd, ppd->visited); |
5d80a306 | 2541 | return NULL_TREE; |
e1a18c68 DG |
2542 | |
2543 | case TYPENAME_TYPE: | |
2544 | cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t), &find_parameter_packs_r, | |
92fab505 | 2545 | ppd, ppd->visited); |
e1a18c68 DG |
2546 | *walk_subtrees = 0; |
2547 | return NULL_TREE; | |
e1a18c68 | 2548 | |
5d80a306 DG |
2549 | case TYPE_PACK_EXPANSION: |
2550 | case EXPR_PACK_EXPANSION: | |
2551 | *walk_subtrees = 0; | |
2552 | return NULL_TREE; | |
2553 | ||
d393153e | 2554 | case INTEGER_TYPE: |
14588106 | 2555 | cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r, |
92fab505 | 2556 | ppd, ppd->visited); |
d393153e DG |
2557 | *walk_subtrees = 0; |
2558 | return NULL_TREE; | |
2559 | ||
4439d02f | 2560 | case IDENTIFIER_NODE: |
92fab505 DG |
2561 | cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd, |
2562 | ppd->visited); | |
4439d02f DG |
2563 | *walk_subtrees = 0; |
2564 | return NULL_TREE; | |
2565 | ||
5d80a306 DG |
2566 | default: |
2567 | return NULL_TREE; | |
2568 | } | |
5d80a306 DG |
2569 | |
2570 | return NULL_TREE; | |
2571 | } | |
2572 | ||
2573 | /* Determines if the expression or type T uses any parameter packs. */ | |
2574 | bool | |
2575 | uses_parameter_packs (tree t) | |
2576 | { | |
2577 | tree parameter_packs = NULL_TREE; | |
2578 | struct find_parameter_pack_data ppd; | |
2579 | ppd.parameter_packs = ¶meter_packs; | |
2580 | ppd.visited = pointer_set_create (); | |
92fab505 | 2581 | cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited); |
30bcc028 | 2582 | pointer_set_destroy (ppd.visited); |
5d80a306 DG |
2583 | return parameter_packs != NULL_TREE; |
2584 | } | |
2585 | ||
2586 | /* Turn ARG, which may be an expression, type, or a TREE_LIST | |
2587 | representation a base-class initializer into a parameter pack | |
2588 | expansion. If all goes well, the resulting node will be an | |
2589 | EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST, | |
2590 | respectively. */ | |
2591 | tree | |
2592 | make_pack_expansion (tree arg) | |
2593 | { | |
2594 | tree result; | |
2595 | tree parameter_packs = NULL_TREE; | |
2596 | bool for_types = false; | |
2597 | struct find_parameter_pack_data ppd; | |
2598 | ||
2599 | if (!arg || arg == error_mark_node) | |
2600 | return arg; | |
2601 | ||
2602 | if (TREE_CODE (arg) == TREE_LIST) | |
2603 | { | |
2604 | /* The only time we will see a TREE_LIST here is for a base | |
2605 | class initializer. In this case, the TREE_PURPOSE will be a | |
2606 | _TYPE node (representing the base class expansion we're | |
2607 | initializing) and the TREE_VALUE will be a TREE_LIST | |
2608 | containing the initialization arguments. | |
2609 | ||
2610 | The resulting expansion looks somewhat different from most | |
2611 | expansions. Rather than returning just one _EXPANSION, we | |
2612 | return a TREE_LIST whose TREE_PURPOSE is a | |
2613 | TYPE_PACK_EXPANSION containing the bases that will be | |
2614 | initialized. The TREE_VALUE will be identical to the | |
2615 | original TREE_VALUE, which is a list of arguments that will | |
2616 | be passed to each base. We do not introduce any new pack | |
2617 | expansion nodes into the TREE_VALUE (although it is possible | |
2618 | that some already exist), because the TREE_PURPOSE and | |
2619 | TREE_VALUE all need to be expanded together with the same | |
2620 | _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the | |
2621 | resulting TREE_PURPOSE will mention the parameter packs in | |
2622 | both the bases and the arguments to the bases. */ | |
2623 | tree purpose; | |
2624 | tree value; | |
2625 | tree parameter_packs = NULL_TREE; | |
2626 | ||
2627 | /* Determine which parameter packs will be used by the base | |
2628 | class expansion. */ | |
2629 | ppd.visited = pointer_set_create (); | |
2630 | ppd.parameter_packs = ¶meter_packs; | |
14588106 | 2631 | cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r, |
92fab505 | 2632 | &ppd, ppd.visited); |
5d80a306 DG |
2633 | |
2634 | if (parameter_packs == NULL_TREE) | |
2635 | { | |
2636 | error ("base initializer expansion %<%T%> contains no parameter packs", arg); | |
30bcc028 | 2637 | pointer_set_destroy (ppd.visited); |
5d80a306 DG |
2638 | return error_mark_node; |
2639 | } | |
2640 | ||
2641 | if (TREE_VALUE (arg) != void_type_node) | |
2642 | { | |
2643 | /* Collect the sets of parameter packs used in each of the | |
2644 | initialization arguments. */ | |
2645 | for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value)) | |
2646 | { | |
2647 | /* Determine which parameter packs will be expanded in this | |
2648 | argument. */ | |
14588106 | 2649 | cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r, |
92fab505 | 2650 | &ppd, ppd.visited); |
5d80a306 DG |
2651 | } |
2652 | } | |
2653 | ||
30bcc028 DG |
2654 | pointer_set_destroy (ppd.visited); |
2655 | ||
5d80a306 DG |
2656 | /* Create the pack expansion type for the base type. */ |
2657 | purpose = make_node (TYPE_PACK_EXPANSION); | |
2658 | SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg)); | |
2659 | PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs; | |
2660 | ||
2661 | /* Just use structural equality for these TYPE_PACK_EXPANSIONS; | |
2662 | they will rarely be compared to anything. */ | |
2663 | SET_TYPE_STRUCTURAL_EQUALITY (purpose); | |
2664 | ||
2665 | return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE); | |
2666 | } | |
2667 | ||
2668 | if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL) | |
2669 | for_types = true; | |
2670 | ||
2671 | /* Build the PACK_EXPANSION_* node. */ | |
2672 | result = make_node (for_types ? TYPE_PACK_EXPANSION : EXPR_PACK_EXPANSION); | |
2673 | SET_PACK_EXPANSION_PATTERN (result, arg); | |
2674 | if (TREE_CODE (result) == EXPR_PACK_EXPANSION) | |
2675 | { | |
2676 | /* Propagate type and const-expression information. */ | |
2677 | TREE_TYPE (result) = TREE_TYPE (arg); | |
2678 | TREE_CONSTANT (result) = TREE_CONSTANT (arg); | |
2679 | } | |
2680 | else | |
2681 | /* Just use structural equality for these TYPE_PACK_EXPANSIONS; | |
2682 | they will rarely be compared to anything. */ | |
2683 | SET_TYPE_STRUCTURAL_EQUALITY (result); | |
2684 | ||
2685 | /* Determine which parameter packs will be expanded. */ | |
2686 | ppd.parameter_packs = ¶meter_packs; | |
2687 | ppd.visited = pointer_set_create (); | |
92fab505 | 2688 | cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited); |
30bcc028 | 2689 | pointer_set_destroy (ppd.visited); |
5d80a306 DG |
2690 | |
2691 | /* Make sure we found some parameter packs. */ | |
2692 | if (parameter_packs == NULL_TREE) | |
2693 | { | |
2694 | if (TYPE_P (arg)) | |
2695 | error ("expansion pattern %<%T%> contains no argument packs", arg); | |
2696 | else | |
2697 | error ("expansion pattern %<%E%> contains no argument packs", arg); | |
2698 | return error_mark_node; | |
2699 | } | |
2700 | PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs; | |
2701 | ||
2702 | return result; | |
2703 | } | |
2704 | ||
2705 | /* Checks T for any "bare" parameter packs, which have not yet been | |
2706 | expanded, and issues an error if any are found. This operation can | |
2707 | only be done on full expressions or types (e.g., an expression | |
2708 | statement, "if" condition, etc.), because we could have expressions like: | |
2709 | ||
2710 | foo(f(g(h(args)))...) | |
2711 | ||
2712 | where "args" is a parameter pack. check_for_bare_parameter_packs | |
2713 | should not be called for the subexpressions args, h(args), | |
2714 | g(h(args)), or f(g(h(args))), because we would produce erroneous | |
b1d7b1c0 DG |
2715 | error messages. |
2716 | ||
4439d02f DG |
2717 | Returns TRUE and emits an error if there were bare parameter packs, |
2718 | returns FALSE otherwise. */ | |
b1d7b1c0 | 2719 | bool |
7b3e2d46 | 2720 | check_for_bare_parameter_packs (tree t) |
5d80a306 DG |
2721 | { |
2722 | tree parameter_packs = NULL_TREE; | |
2723 | struct find_parameter_pack_data ppd; | |
2724 | ||
7b3e2d46 | 2725 | if (!processing_template_decl || !t || t == error_mark_node) |
4439d02f | 2726 | return false; |
5d80a306 | 2727 | |
7b3e2d46 DG |
2728 | if (TREE_CODE (t) == TYPE_DECL) |
2729 | t = TREE_TYPE (t); | |
5d80a306 DG |
2730 | |
2731 | ppd.parameter_packs = ¶meter_packs; | |
2732 | ppd.visited = pointer_set_create (); | |
92fab505 | 2733 | cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited); |
30bcc028 | 2734 | pointer_set_destroy (ppd.visited); |
5d80a306 | 2735 | |
b1d7b1c0 DG |
2736 | if (parameter_packs) |
2737 | { | |
bcac2b89 | 2738 | error ("parameter packs not expanded with %<...%>:"); |
b1d7b1c0 DG |
2739 | while (parameter_packs) |
2740 | { | |
2741 | tree pack = TREE_VALUE (parameter_packs); | |
2742 | tree name = NULL_TREE; | |
2743 | ||
2744 | if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM | |
2745 | || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM) | |
2746 | name = TYPE_NAME (pack); | |
2747 | else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX) | |
2748 | name = DECL_NAME (TEMPLATE_PARM_DECL (pack)); | |
2749 | else | |
2750 | name = DECL_NAME (pack); | |
1ad8aeeb DG |
2751 | |
2752 | if (name) | |
1f5b3869 | 2753 | inform (input_location, " %qD", name); |
1ad8aeeb | 2754 | else |
1f5b3869 | 2755 | inform (input_location, " <anonymous>"); |
b1d7b1c0 DG |
2756 | |
2757 | parameter_packs = TREE_CHAIN (parameter_packs); | |
2758 | } | |
2759 | ||
4439d02f | 2760 | return true; |
b1d7b1c0 DG |
2761 | } |
2762 | ||
4439d02f | 2763 | return false; |
5d80a306 DG |
2764 | } |
2765 | ||
2766 | /* Expand any parameter packs that occur in the template arguments in | |
2767 | ARGS. */ | |
2768 | tree | |
2769 | expand_template_argument_pack (tree args) | |
2770 | { | |
2771 | tree result_args = NULL_TREE; | |
2772 | int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0; | |
2773 | int num_result_args = -1; | |
2774 | ||
2775 | /* First, determine if we need to expand anything, and the number of | |
2776 | slots we'll need. */ | |
2777 | for (in_arg = 0; in_arg < nargs; ++in_arg) | |
2778 | { | |
2779 | tree arg = TREE_VEC_ELT (args, in_arg); | |
2780 | if (ARGUMENT_PACK_P (arg)) | |
2781 | { | |
2782 | int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)); | |
2783 | if (num_result_args < 0) | |
2784 | num_result_args = in_arg + num_packed; | |
2785 | else | |
2786 | num_result_args += num_packed; | |
2787 | } | |
2788 | else | |
2789 | { | |
2790 | if (num_result_args >= 0) | |
2791 | num_result_args++; | |
2792 | } | |
2793 | } | |
2794 | ||
2795 | /* If no expansion is necessary, we're done. */ | |
2796 | if (num_result_args < 0) | |
2797 | return args; | |
2798 | ||
2799 | /* Expand arguments. */ | |
2800 | result_args = make_tree_vec (num_result_args); | |
2801 | for (in_arg = 0; in_arg < nargs; ++in_arg) | |
2802 | { | |
2803 | tree arg = TREE_VEC_ELT (args, in_arg); | |
2804 | if (ARGUMENT_PACK_P (arg)) | |
2805 | { | |
2806 | tree packed = ARGUMENT_PACK_ARGS (arg); | |
2807 | int i, num_packed = TREE_VEC_LENGTH (packed); | |
2808 | for (i = 0; i < num_packed; ++i, ++out_arg) | |
2809 | TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i); | |
2810 | } | |
2811 | else | |
2812 | { | |
2813 | TREE_VEC_ELT (result_args, out_arg) = arg; | |
2814 | ++out_arg; | |
2815 | } | |
2816 | } | |
2817 | ||
2818 | return result_args; | |
2819 | } | |
2820 | ||
157b0647 | 2821 | /* Checks if DECL shadows a template parameter. |
f3400fe2 JM |
2822 | |
2823 | [temp.local]: A template-parameter shall not be redeclared within its | |
157b0647 | 2824 | scope (including nested scopes). |
f3400fe2 | 2825 | |
157b0647 PC |
2826 | Emits an error and returns TRUE if the DECL shadows a parameter, |
2827 | returns FALSE otherwise. */ | |
2828 | ||
2829 | bool | |
3a978d72 | 2830 | check_template_shadow (tree decl) |
f3400fe2 | 2831 | { |
8f032717 MM |
2832 | tree olddecl; |
2833 | ||
b5d9b9ab MM |
2834 | /* If we're not in a template, we can't possibly shadow a template |
2835 | parameter. */ | |
2836 | if (!current_template_parms) | |
157b0647 | 2837 | return true; |
b5d9b9ab MM |
2838 | |
2839 | /* Figure out what we're shadowing. */ | |
8f032717 MM |
2840 | if (TREE_CODE (decl) == OVERLOAD) |
2841 | decl = OVL_CURRENT (decl); | |
90ea9897 | 2842 | olddecl = innermost_non_namespace_value (DECL_NAME (decl)); |
f3400fe2 | 2843 | |
b5d9b9ab MM |
2844 | /* If there's no previous binding for this name, we're not shadowing |
2845 | anything, let alone a template parameter. */ | |
2846 | if (!olddecl) | |
157b0647 | 2847 | return true; |
b5d9b9ab MM |
2848 | |
2849 | /* If we're not shadowing a template parameter, we're done. Note | |
2850 | that OLDDECL might be an OVERLOAD (or perhaps even an | |
2851 | ERROR_MARK), so we can't just blithely assume it to be a _DECL | |
2852 | node. */ | |
2f939d94 | 2853 | if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl)) |
157b0647 | 2854 | return true; |
b5d9b9ab MM |
2855 | |
2856 | /* We check for decl != olddecl to avoid bogus errors for using a | |
2857 | name inside a class. We check TPFI to avoid duplicate errors for | |
2858 | inline member templates. */ | |
c8094d83 | 2859 | if (decl == olddecl |
b5d9b9ab | 2860 | || TEMPLATE_PARMS_FOR_INLINE (current_template_parms)) |
157b0647 | 2861 | return true; |
b5d9b9ab | 2862 | |
dee15844 JM |
2863 | error ("declaration of %q+#D", decl); |
2864 | error (" shadows template parm %q+#D", olddecl); | |
157b0647 | 2865 | return false; |
f3400fe2 | 2866 | } |
22a7be53 | 2867 | |
f3400fe2 | 2868 | /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL, |
f84b4be9 JM |
2869 | ORIG_LEVEL, DECL, and TYPE. */ |
2870 | ||
2871 | static tree | |
c8094d83 | 2872 | build_template_parm_index (int index, |
0cbd7506 MS |
2873 | int level, |
2874 | int orig_level, | |
2875 | tree decl, | |
2876 | tree type) | |
f84b4be9 JM |
2877 | { |
2878 | tree t = make_node (TEMPLATE_PARM_INDEX); | |
2879 | TEMPLATE_PARM_IDX (t) = index; | |
2880 | TEMPLATE_PARM_LEVEL (t) = level; | |
2881 | TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level; | |
2882 | TEMPLATE_PARM_DECL (t) = decl; | |
2883 | TREE_TYPE (t) = type; | |
3e4a3562 NS |
2884 | TREE_CONSTANT (t) = TREE_CONSTANT (decl); |
2885 | TREE_READONLY (t) = TREE_READONLY (decl); | |
f84b4be9 JM |
2886 | |
2887 | return t; | |
2888 | } | |
2889 | ||
06d40de8 | 2890 | /* Find the canonical type parameter for the given template type |
c3e188fc KH |
2891 | parameter. Returns the canonical type parameter, which may be TYPE |
2892 | if no such parameter existed. */ | |
06d40de8 DG |
2893 | static tree |
2894 | canonical_type_parameter (tree type) | |
2895 | { | |
2896 | tree list; | |
2897 | int idx = TEMPLATE_TYPE_IDX (type); | |
2898 | if (!canonical_template_parms) | |
2899 | canonical_template_parms = VEC_alloc (tree, gc, idx+1); | |
2900 | ||
2901 | while (VEC_length (tree, canonical_template_parms) <= (unsigned)idx) | |
2902 | VEC_safe_push (tree, gc, canonical_template_parms, NULL_TREE); | |
2903 | ||
2904 | list = VEC_index (tree, canonical_template_parms, idx); | |
96d84882 | 2905 | while (list && !comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL)) |
06d40de8 DG |
2906 | list = TREE_CHAIN (list); |
2907 | ||
2908 | if (list) | |
2909 | return TREE_VALUE (list); | |
2910 | else | |
2911 | { | |
2912 | VEC_replace(tree, canonical_template_parms, idx, | |
2913 | tree_cons (NULL_TREE, type, | |
2914 | VEC_index (tree, canonical_template_parms, idx))); | |
2915 | return type; | |
2916 | } | |
2917 | } | |
2918 | ||
f84b4be9 | 2919 | /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose |
93cdc044 | 2920 | TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a |
f84b4be9 JM |
2921 | TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a |
2922 | new one is created. */ | |
2923 | ||
c8094d83 | 2924 | static tree |
ef3b7b17 JM |
2925 | reduce_template_parm_level (tree index, tree type, int levels, tree args, |
2926 | tsubst_flags_t complain) | |
f84b4be9 JM |
2927 | { |
2928 | if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE | |
2929 | || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index)) | |
93cdc044 | 2930 | != TEMPLATE_PARM_LEVEL (index) - levels)) |
f84b4be9 | 2931 | { |
3e4a3562 NS |
2932 | tree orig_decl = TEMPLATE_PARM_DECL (index); |
2933 | tree decl, t; | |
c8094d83 | 2934 | |
c2255bc4 AH |
2935 | decl = build_decl (DECL_SOURCE_LOCATION (orig_decl), |
2936 | TREE_CODE (orig_decl), DECL_NAME (orig_decl), type); | |
3e4a3562 NS |
2937 | TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl); |
2938 | TREE_READONLY (decl) = TREE_READONLY (orig_decl); | |
2939 | DECL_ARTIFICIAL (decl) = 1; | |
2940 | SET_DECL_TEMPLATE_PARM_P (decl); | |
c8094d83 | 2941 | |
3e4a3562 | 2942 | t = build_template_parm_index (TEMPLATE_PARM_IDX (index), |
93cdc044 | 2943 | TEMPLATE_PARM_LEVEL (index) - levels, |
f84b4be9 JM |
2944 | TEMPLATE_PARM_ORIG_LEVEL (index), |
2945 | decl, type); | |
2946 | TEMPLATE_PARM_DESCENDANTS (index) = t; | |
5d80a306 DG |
2947 | TEMPLATE_PARM_PARAMETER_PACK (t) |
2948 | = TEMPLATE_PARM_PARAMETER_PACK (index); | |
cae40af6 | 2949 | |
820cc88f | 2950 | /* Template template parameters need this. */ |
ef3b7b17 JM |
2951 | if (TREE_CODE (decl) == TEMPLATE_DECL) |
2952 | DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms | |
2953 | (DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index)), | |
2954 | args, complain); | |
f84b4be9 JM |
2955 | } |
2956 | ||
2957 | return TEMPLATE_PARM_DESCENDANTS (index); | |
2958 | } | |
2959 | ||
a14de1af | 2960 | /* Process information from new template parameter PARM and append it to the |
058b15c1 | 2961 | LIST being built. This new parameter is a non-type parameter iff |
5d80a306 | 2962 | IS_NON_TYPE is true. This new parameter is a parameter |
c2255bc4 AH |
2963 | pack iff IS_PARAMETER_PACK is true. The location of PARM is in |
2964 | PARM_LOC. */ | |
e92cc029 | 2965 | |
8d08fdba | 2966 | tree |
c2255bc4 | 2967 | process_template_parm (tree list, location_t parm_loc, tree parm, bool is_non_type, |
5d80a306 | 2968 | bool is_parameter_pack) |
8d08fdba | 2969 | { |
8d08fdba | 2970 | tree decl = 0; |
a292b002 | 2971 | tree defval; |
0f67a82f | 2972 | tree err_parm_list; |
d47e3adf | 2973 | int idx = 0; |
f84b4be9 | 2974 | |
50bc768d | 2975 | gcc_assert (TREE_CODE (parm) == TREE_LIST); |
a292b002 | 2976 | defval = TREE_PURPOSE (parm); |
5566b478 MS |
2977 | |
2978 | if (list) | |
2979 | { | |
d47e3adf LM |
2980 | tree p = tree_last (list); |
2981 | ||
0f67a82f | 2982 | if (p && TREE_VALUE (p) != error_mark_node) |
d47e3adf LM |
2983 | { |
2984 | p = TREE_VALUE (p); | |
2985 | if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL) | |
2986 | idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p)); | |
2987 | else | |
2988 | idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p)); | |
2989 | } | |
5566b478 | 2990 | |
5566b478 MS |
2991 | ++idx; |
2992 | } | |
2993 | else | |
2994 | idx = 0; | |
2995 | ||
058b15c1 | 2996 | if (is_non_type) |
8d08fdba | 2997 | { |
058b15c1 MM |
2998 | parm = TREE_VALUE (parm); |
2999 | ||
833aa4c4 | 3000 | SET_DECL_TEMPLATE_PARM_P (parm); |
d490621d | 3001 | |
620188c9 | 3002 | if (TREE_TYPE (parm) == error_mark_node) |
0f67a82f LM |
3003 | { |
3004 | err_parm_list = build_tree_list (defval, parm); | |
3005 | TREE_VALUE (err_parm_list) = error_mark_node; | |
3006 | return chainon (list, err_parm_list); | |
3007 | } | |
620188c9 VR |
3008 | else |
3009 | { | |
3010 | /* [temp.param] | |
d490621d | 3011 | |
620188c9 VR |
3012 | The top-level cv-qualifiers on the template-parameter are |
3013 | ignored when determining its type. */ | |
3014 | TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm)); | |
3015 | if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1)) | |
0f67a82f LM |
3016 | { |
3017 | err_parm_list = build_tree_list (defval, parm); | |
3018 | TREE_VALUE (err_parm_list) = error_mark_node; | |
3019 | return chainon (list, err_parm_list); | |
3020 | } | |
5d80a306 DG |
3021 | |
3022 | if (uses_parameter_packs (TREE_TYPE (parm)) && !is_parameter_pack) | |
3023 | { | |
3024 | /* This template parameter is not a parameter pack, but it | |
3025 | should be. Complain about "bare" parameter packs. */ | |
7b3e2d46 | 3026 | check_for_bare_parameter_packs (TREE_TYPE (parm)); |
5d80a306 DG |
3027 | |
3028 | /* Recover by calling this a parameter pack. */ | |
3029 | is_parameter_pack = true; | |
3030 | } | |
620188c9 | 3031 | } |
d490621d | 3032 | |
8d08fdba | 3033 | /* A template parameter is not modifiable. */ |
6de9cd9a | 3034 | TREE_CONSTANT (parm) = 1; |
6de9cd9a | 3035 | TREE_READONLY (parm) = 1; |
c2255bc4 AH |
3036 | decl = build_decl (parm_loc, |
3037 | CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm)); | |
6de9cd9a | 3038 | TREE_CONSTANT (decl) = 1; |
6de9cd9a | 3039 | TREE_READONLY (decl) = 1; |
c8094d83 | 3040 | DECL_INITIAL (parm) = DECL_INITIAL (decl) |
f84b4be9 JM |
3041 | = build_template_parm_index (idx, processing_template_decl, |
3042 | processing_template_decl, | |
3043 | decl, TREE_TYPE (parm)); | |
5d80a306 DG |
3044 | |
3045 | TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)) | |
3046 | = is_parameter_pack; | |
8d08fdba MS |
3047 | } |
3048 | else | |
3049 | { | |
73b0fce8 | 3050 | tree t; |
058b15c1 | 3051 | parm = TREE_VALUE (TREE_VALUE (parm)); |
c8094d83 | 3052 | |
73b0fce8 KL |
3053 | if (parm && TREE_CODE (parm) == TEMPLATE_DECL) |
3054 | { | |
9e1e64ec | 3055 | t = cxx_make_type (TEMPLATE_TEMPLATE_PARM); |
c8094d83 | 3056 | /* This is for distinguishing between real templates and template |
73b0fce8 KL |
3057 | template parameters */ |
3058 | TREE_TYPE (parm) = t; | |
3059 | TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t; | |
3060 | decl = parm; | |
3061 | } | |
3062 | else | |
3063 | { | |
9e1e64ec | 3064 | t = cxx_make_type (TEMPLATE_TYPE_PARM); |
f4f206f4 | 3065 | /* parm is either IDENTIFIER_NODE or NULL_TREE. */ |
c2255bc4 AH |
3066 | decl = build_decl (parm_loc, |
3067 | TYPE_DECL, parm, t); | |
73b0fce8 | 3068 | } |
c8094d83 | 3069 | |
d2e5ee5c MS |
3070 | TYPE_NAME (t) = decl; |
3071 | TYPE_STUB_DECL (t) = decl; | |
a292b002 | 3072 | parm = decl; |
f84b4be9 | 3073 | TEMPLATE_TYPE_PARM_INDEX (t) |
c8094d83 | 3074 | = build_template_parm_index (idx, processing_template_decl, |
f84b4be9 JM |
3075 | processing_template_decl, |
3076 | decl, TREE_TYPE (parm)); | |
5d80a306 | 3077 | TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack; |
06d40de8 | 3078 | TYPE_CANONICAL (t) = canonical_type_parameter (t); |
8d08fdba | 3079 | } |
c727aa5e | 3080 | DECL_ARTIFICIAL (decl) = 1; |
cd9f6678 | 3081 | SET_DECL_TEMPLATE_PARM_P (decl); |
8d08fdba | 3082 | pushdecl (decl); |
a292b002 | 3083 | parm = build_tree_list (defval, parm); |
8d08fdba MS |
3084 | return chainon (list, parm); |
3085 | } | |
3086 | ||
3087 | /* The end of a template parameter list has been reached. Process the | |
3088 | tree list into a parameter vector, converting each parameter into a more | |
3089 | useful form. Type parameters are saved as IDENTIFIER_NODEs, and others | |
3090 | as PARM_DECLs. */ | |
3091 | ||
3092 | tree | |
3a978d72 | 3093 | end_template_parm_list (tree parms) |
8d08fdba | 3094 | { |
5566b478 | 3095 | int nparms; |
9471d3e2 | 3096 | tree parm, next; |
5566b478 MS |
3097 | tree saved_parmlist = make_tree_vec (list_length (parms)); |
3098 | ||
5566b478 | 3099 | current_template_parms |
4890c2f4 | 3100 | = tree_cons (size_int (processing_template_decl), |
5566b478 | 3101 | saved_parmlist, current_template_parms); |
8d08fdba | 3102 | |
9471d3e2 NS |
3103 | for (parm = parms, nparms = 0; parm; parm = next, nparms++) |
3104 | { | |
3105 | next = TREE_CHAIN (parm); | |
3106 | TREE_VEC_ELT (saved_parmlist, nparms) = parm; | |
3107 | TREE_CHAIN (parm) = NULL_TREE; | |
3108 | } | |
a292b002 | 3109 | |
67ffc812 MM |
3110 | --processing_template_parmlist; |
3111 | ||
8d08fdba MS |
3112 | return saved_parmlist; |
3113 | } | |
3114 | ||
5566b478 MS |
3115 | /* end_template_decl is called after a template declaration is seen. */ |
3116 | ||
8d08fdba | 3117 | void |
3a978d72 | 3118 | end_template_decl (void) |
8d08fdba | 3119 | { |
386b8a85 JM |
3120 | reset_specialization (); |
3121 | ||
5156628f | 3122 | if (! processing_template_decl) |
73aad9b9 JM |
3123 | return; |
3124 | ||
5566b478 | 3125 | /* This matches the pushlevel in begin_template_parm_list. */ |
74b846e0 | 3126 | finish_scope (); |
8d08fdba | 3127 | |
5566b478 MS |
3128 | --processing_template_decl; |
3129 | current_template_parms = TREE_CHAIN (current_template_parms); | |
5566b478 | 3130 | } |
8d08fdba | 3131 | |
03c17ccd MM |
3132 | /* Within the declaration of a template, return all levels of template |
3133 | parameters that apply. The template parameters are represented as | |
3134 | a TREE_VEC, in the form documented in cp-tree.h for template | |
3135 | arguments. */ | |
9a3b49ac | 3136 | |
b5791fdc | 3137 | static tree |
3a978d72 | 3138 | current_template_args (void) |
5566b478 | 3139 | { |
36a117a5 | 3140 | tree header; |
b370501f | 3141 | tree args = NULL_TREE; |
36a117a5 | 3142 | int length = TMPL_PARMS_DEPTH (current_template_parms); |
98c1c668 JM |
3143 | int l = length; |
3144 | ||
36a117a5 MM |
3145 | /* If there is only one level of template parameters, we do not |
3146 | create a TREE_VEC of TREE_VECs. Instead, we return a single | |
3147 | TREE_VEC containing the arguments. */ | |
3148 | if (length > 1) | |
3149 | args = make_tree_vec (length); | |
3150 | ||
3151 | for (header = current_template_parms; header; header = TREE_CHAIN (header)) | |
8d08fdba | 3152 | { |
5566b478 | 3153 | tree a = copy_node (TREE_VALUE (header)); |
36a117a5 MM |
3154 | int i; |
3155 | ||
5566b478 | 3156 | TREE_TYPE (a) = NULL_TREE; |
36a117a5 | 3157 | for (i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i) |
5566b478 | 3158 | { |
98c1c668 JM |
3159 | tree t = TREE_VEC_ELT (a, i); |
3160 | ||
36a117a5 | 3161 | /* T will be a list if we are called from within a |
98c1c668 JM |
3162 | begin/end_template_parm_list pair, but a vector directly |
3163 | if within a begin/end_member_template_processing pair. */ | |
c8094d83 | 3164 | if (TREE_CODE (t) == TREE_LIST) |
98c1c668 JM |
3165 | { |
3166 | t = TREE_VALUE (t); | |
c8094d83 | 3167 | |
0b93f014 | 3168 | if (!error_operand_p (t)) |
0f67a82f LM |
3169 | { |
3170 | if (TREE_CODE (t) == TYPE_DECL | |
3171 | || TREE_CODE (t) == TEMPLATE_DECL) | |
5d80a306 DG |
3172 | { |
3173 | t = TREE_TYPE (t); | |
3174 | ||
3175 | if (TEMPLATE_TYPE_PARAMETER_PACK (t)) | |
3176 | { | |
3177 | /* Turn this argument into a TYPE_ARGUMENT_PACK | |
3178 | with a single element, which expands T. */ | |
3179 | tree vec = make_tree_vec (1); | |
3180 | TREE_VEC_ELT (vec, 0) = make_pack_expansion (t); | |
3181 | ||
3182 | t = make_node (TYPE_ARGUMENT_PACK); | |
3183 | SET_ARGUMENT_PACK_ARGS (t, vec); | |
3184 | } | |
3185 | } | |
3186 | else | |
3187 | { | |
3188 | t = DECL_INITIAL (t); | |
3189 | ||
3190 | if (TEMPLATE_PARM_PARAMETER_PACK (t)) | |
3191 | { | |
3192 | /* Turn this argument into a NONTYPE_ARGUMENT_PACK | |
3193 | with a single element, which expands T. */ | |
3194 | tree vec = make_tree_vec (1); | |
3195 | tree type = TREE_TYPE (TEMPLATE_PARM_DECL (t)); | |
3196 | TREE_VEC_ELT (vec, 0) = make_pack_expansion (t); | |
3197 | ||
3198 | t = make_node (NONTYPE_ARGUMENT_PACK); | |
3199 | SET_ARGUMENT_PACK_ARGS (t, vec); | |
3200 | TREE_TYPE (t) = type; | |
3201 | } | |
3202 | } | |
bf98d3b6 | 3203 | TREE_VEC_ELT (a, i) = t; |
5d80a306 | 3204 | } |
98c1c668 | 3205 | } |
5566b478 | 3206 | } |
36a117a5 MM |
3207 | |
3208 | if (length > 1) | |
3209 | TREE_VEC_ELT (args, --l) = a; | |
3210 | else | |
3211 | args = a; | |
8d08fdba MS |
3212 | } |
3213 | ||
9a3b49ac MS |
3214 | return args; |
3215 | } | |
75650646 | 3216 | |
e1467ff2 | 3217 | /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated |
c7222c02 | 3218 | template PARMS. If MEMBER_TEMPLATE_P is true, the new template is |
ee81147e | 3219 | a member template. Used by push_template_decl below. */ |
e1467ff2 | 3220 | |
75650646 | 3221 | static tree |
c7222c02 | 3222 | build_template_decl (tree decl, tree parms, bool member_template_p) |
75650646 MM |
3223 | { |
3224 | tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE); | |
3225 | DECL_TEMPLATE_PARMS (tmpl) = parms; | |
3226 | DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl); | |
c7222c02 | 3227 | DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p; |
75650646 MM |
3228 | if (DECL_LANG_SPECIFIC (decl)) |
3229 | { | |
8f032717 MM |
3230 | DECL_STATIC_FUNCTION_P (tmpl) = DECL_STATIC_FUNCTION_P (decl); |
3231 | DECL_CONSTRUCTOR_P (tmpl) = DECL_CONSTRUCTOR_P (decl); | |
c8460010 | 3232 | DECL_DESTRUCTOR_P (tmpl) = DECL_DESTRUCTOR_P (decl); |
11f98788 | 3233 | DECL_NONCONVERTING_P (tmpl) = DECL_NONCONVERTING_P (decl); |
1f6e1acc AS |
3234 | DECL_ASSIGNMENT_OPERATOR_P (tmpl) = DECL_ASSIGNMENT_OPERATOR_P (decl); |
3235 | if (DECL_OVERLOADED_OPERATOR_P (decl)) | |
c8094d83 | 3236 | SET_OVERLOADED_OPERATOR_CODE (tmpl, |
1f6e1acc | 3237 | DECL_OVERLOADED_OPERATOR_P (decl)); |
75650646 MM |
3238 | } |
3239 | ||
3240 | return tmpl; | |
3241 | } | |
3242 | ||
050367a3 MM |
3243 | struct template_parm_data |
3244 | { | |
6c30752f MM |
3245 | /* The level of the template parameters we are currently |
3246 | processing. */ | |
050367a3 | 3247 | int level; |
6c30752f MM |
3248 | |
3249 | /* The index of the specialization argument we are currently | |
3250 | processing. */ | |
3251 | int current_arg; | |
3252 | ||
3253 | /* An array whose size is the number of template parameters. The | |
838dfd8a | 3254 | elements are nonzero if the parameter has been used in any one |
6c30752f | 3255 | of the arguments processed so far. */ |
050367a3 | 3256 | int* parms; |
6c30752f MM |
3257 | |
3258 | /* An array whose size is the number of template arguments. The | |
838dfd8a | 3259 | elements are nonzero if the argument makes use of template |
6c30752f MM |
3260 | parameters of this level. */ |
3261 | int* arg_uses_template_parms; | |
050367a3 MM |
3262 | }; |
3263 | ||
3264 | /* Subroutine of push_template_decl used to see if each template | |
3265 | parameter in a partial specialization is used in the explicit | |
3266 | argument list. If T is of the LEVEL given in DATA (which is | |
3267 | treated as a template_parm_data*), then DATA->PARMS is marked | |
3268 | appropriately. */ | |
3269 | ||
3270 | static int | |
3a978d72 | 3271 | mark_template_parm (tree t, void* data) |
050367a3 MM |
3272 | { |
3273 | int level; | |
3274 | int idx; | |
3275 | struct template_parm_data* tpd = (struct template_parm_data*) data; | |
3276 | ||
3277 | if (TREE_CODE (t) == TEMPLATE_PARM_INDEX) | |
3278 | { | |
3279 | level = TEMPLATE_PARM_LEVEL (t); | |
3280 | idx = TEMPLATE_PARM_IDX (t); | |
3281 | } | |
3282 | else | |
3283 | { | |
3284 | level = TEMPLATE_TYPE_LEVEL (t); | |
3285 | idx = TEMPLATE_TYPE_IDX (t); | |
3286 | } | |
3287 | ||
3288 | if (level == tpd->level) | |
6c30752f MM |
3289 | { |
3290 | tpd->parms[idx] = 1; | |
3291 | tpd->arg_uses_template_parms[tpd->current_arg] = 1; | |
3292 | } | |
050367a3 MM |
3293 | |
3294 | /* Return zero so that for_each_template_parm will continue the | |
3295 | traversal of the tree; we want to mark *every* template parm. */ | |
3296 | return 0; | |
3297 | } | |
3298 | ||
6c30752f MM |
3299 | /* Process the partial specialization DECL. */ |
3300 | ||
e9659ab0 | 3301 | static tree |
3a978d72 | 3302 | process_partial_specialization (tree decl) |
6c30752f MM |
3303 | { |
3304 | tree type = TREE_TYPE (decl); | |
3305 | tree maintmpl = CLASSTYPE_TI_TEMPLATE (type); | |
3306 | tree specargs = CLASSTYPE_TI_ARGS (type); | |
f9a7ae04 | 3307 | tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs); |
6c30752f | 3308 | tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl); |
b17c40be | 3309 | tree inner_parms; |
6c30752f | 3310 | int nargs = TREE_VEC_LENGTH (inner_args); |
b17c40be | 3311 | int ntparms; |
6c30752f MM |
3312 | int i; |
3313 | int did_error_intro = 0; | |
6c30752f MM |
3314 | struct template_parm_data tpd; |
3315 | struct template_parm_data tpd2; | |
3316 | ||
b17c40be PC |
3317 | gcc_assert (current_template_parms); |
3318 | ||
3319 | inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms); | |
3320 | ntparms = TREE_VEC_LENGTH (inner_parms); | |
3321 | ||
6c30752f MM |
3322 | /* We check that each of the template parameters given in the |
3323 | partial specialization is used in the argument list to the | |
3324 | specialization. For example: | |
3325 | ||
3326 | template <class T> struct S; | |
3327 | template <class T> struct S<T*>; | |
3328 | ||
3329 | The second declaration is OK because `T*' uses the template | |
3330 | parameter T, whereas | |
3331 | ||
3332 | template <class T> struct S<int>; | |
3333 | ||
3334 | is no good. Even trickier is: | |
3335 | ||
3336 | template <class T> | |
3337 | struct S1 | |
3338 | { | |
3339 | template <class U> | |
3340 | struct S2; | |
3341 | template <class U> | |
3342 | struct S2<T>; | |
3343 | }; | |
3344 | ||
0e339752 | 3345 | The S2<T> declaration is actually invalid; it is a |
c8094d83 | 3346 | full-specialization. Of course, |
6c30752f MM |
3347 | |
3348 | template <class U> | |
3349 | struct S2<T (*)(U)>; | |
3350 | ||
3351 | or some such would have been OK. */ | |
3352 | tpd.level = TMPL_PARMS_DEPTH (current_template_parms); | |
67f5655f | 3353 | tpd.parms = (int *) alloca (sizeof (int) * ntparms); |
fad205ff | 3354 | memset (tpd.parms, 0, sizeof (int) * ntparms); |
6c30752f | 3355 | |
67f5655f | 3356 | tpd.arg_uses_template_parms = (int *) alloca (sizeof (int) * nargs); |
fad205ff | 3357 | memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs); |
6c30752f MM |
3358 | for (i = 0; i < nargs; ++i) |
3359 | { | |
3360 | tpd.current_arg = i; | |
3361 | for_each_template_parm (TREE_VEC_ELT (inner_args, i), | |
3362 | &mark_template_parm, | |
4f2c9d7e | 3363 | &tpd, |
c095a4f8 DG |
3364 | NULL, |
3365 | /*include_nondeduced_p=*/false); | |
6c30752f MM |
3366 | } |
3367 | for (i = 0; i < ntparms; ++i) | |
3368 | if (tpd.parms[i] == 0) | |
3369 | { | |
3370 | /* One of the template parms was not used in the | |
f9a7ae04 | 3371 | specialization. */ |
6c30752f MM |
3372 | if (!did_error_intro) |
3373 | { | |
33bd39a2 | 3374 | error ("template parameters not used in partial specialization:"); |
6c30752f MM |
3375 | did_error_intro = 1; |
3376 | } | |
3377 | ||
0f51ccfc | 3378 | error (" %qD", TREE_VALUE (TREE_VEC_ELT (inner_parms, i))); |
6c30752f MM |
3379 | } |
3380 | ||
3381 | /* [temp.class.spec] | |
3382 | ||
3383 | The argument list of the specialization shall not be identical to | |
3384 | the implicit argument list of the primary template. */ | |
c8094d83 MS |
3385 | if (comp_template_args |
3386 | (inner_args, | |
f9a7ae04 MM |
3387 | INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE |
3388 | (maintmpl))))) | |
0f51ccfc | 3389 | error ("partial specialization %qT does not specialize any template arguments", type); |
6c30752f MM |
3390 | |
3391 | /* [temp.class.spec] | |
3392 | ||
3393 | A partially specialized non-type argument expression shall not | |
3394 | involve template parameters of the partial specialization except | |
3395 | when the argument expression is a simple identifier. | |
3396 | ||
3397 | The type of a template parameter corresponding to a specialized | |
3398 | non-type argument shall not be dependent on a parameter of the | |
5d80a306 DG |
3399 | specialization. |
3400 | ||
3401 | Also, we verify that pack expansions only occur at the | |
3402 | end of the argument list. */ | |
50bc768d | 3403 | gcc_assert (nargs == DECL_NTPARMS (maintmpl)); |
6c30752f MM |
3404 | tpd2.parms = 0; |
3405 | for (i = 0; i < nargs; ++i) | |
3406 | { | |
5d80a306 | 3407 | tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i)); |
6c30752f | 3408 | tree arg = TREE_VEC_ELT (inner_args, i); |
5d80a306 DG |
3409 | tree packed_args = NULL_TREE; |
3410 | int j, len = 1; | |
6c30752f | 3411 | |
5d80a306 DG |
3412 | if (ARGUMENT_PACK_P (arg)) |
3413 | { | |
3414 | /* Extract the arguments from the argument pack. We'll be | |
3415 | iterating over these in the following loop. */ | |
3416 | packed_args = ARGUMENT_PACK_ARGS (arg); | |
3417 | len = TREE_VEC_LENGTH (packed_args); | |
3418 | } | |
3419 | ||
3420 | for (j = 0; j < len; j++) | |
3421 | { | |
3422 | if (packed_args) | |
3423 | /* Get the Jth argument in the parameter pack. */ | |
3424 | arg = TREE_VEC_ELT (packed_args, j); | |
3425 | ||
3426 | if (PACK_EXPANSION_P (arg)) | |
3427 | { | |
3428 | /* Pack expansions must come at the end of the | |
3429 | argument list. */ | |
3430 | if ((packed_args && j < len - 1) | |
3431 | || (!packed_args && i < nargs - 1)) | |
3432 | { | |
3433 | if (TREE_CODE (arg) == EXPR_PACK_EXPANSION) | |
3434 | error ("parameter pack argument %qE must be at the end of the template argument list", arg); | |
3435 | else | |
1ad8aeeb DG |
3436 | error ("parameter pack argument %qT must be at the end of the template argument list", arg); |
3437 | ||
3438 | if (packed_args) | |
3439 | TREE_VEC_ELT (packed_args, j) = error_mark_node; | |
5d80a306 DG |
3440 | } |
3441 | } | |
3442 | ||
3443 | if (TREE_CODE (arg) == EXPR_PACK_EXPANSION) | |
3444 | /* We only care about the pattern. */ | |
3445 | arg = PACK_EXPANSION_PATTERN (arg); | |
3446 | ||
3447 | if (/* These first two lines are the `non-type' bit. */ | |
3448 | !TYPE_P (arg) | |
3449 | && TREE_CODE (arg) != TEMPLATE_DECL | |
3450 | /* This next line is the `argument expression is not just a | |
3451 | simple identifier' condition and also the `specialized | |
3452 | non-type argument' bit. */ | |
3453 | && TREE_CODE (arg) != TEMPLATE_PARM_INDEX) | |
3454 | { | |
3455 | if ((!packed_args && tpd.arg_uses_template_parms[i]) | |
3456 | || (packed_args && uses_template_parms (arg))) | |
3457 | error ("template argument %qE involves template parameter(s)", | |
3458 | arg); | |
3459 | else | |
3460 | { | |
3461 | /* Look at the corresponding template parameter, | |
3462 | marking which template parameters its type depends | |
3463 | upon. */ | |
3464 | tree type = TREE_TYPE (parm); | |
3465 | ||
3466 | if (!tpd2.parms) | |
3467 | { | |
3468 | /* We haven't yet initialized TPD2. Do so now. */ | |
3469 | tpd2.arg_uses_template_parms | |
3470 | = (int *) alloca (sizeof (int) * nargs); | |
3471 | /* The number of parameters here is the number in the | |
3472 | main template, which, as checked in the assertion | |
3473 | above, is NARGS. */ | |
3474 | tpd2.parms = (int *) alloca (sizeof (int) * nargs); | |
3475 | tpd2.level = | |
3476 | TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl)); | |
3477 | } | |
3478 | ||
3479 | /* Mark the template parameters. But this time, we're | |
3480 | looking for the template parameters of the main | |
3481 | template, not in the specialization. */ | |
3482 | tpd2.current_arg = i; | |
3483 | tpd2.arg_uses_template_parms[i] = 0; | |
3484 | memset (tpd2.parms, 0, sizeof (int) * nargs); | |
3485 | for_each_template_parm (type, | |
3486 | &mark_template_parm, | |
3487 | &tpd2, | |
c095a4f8 DG |
3488 | NULL, |
3489 | /*include_nondeduced_p=*/false); | |
5d80a306 DG |
3490 | |
3491 | if (tpd2.arg_uses_template_parms [i]) | |
3492 | { | |
3493 | /* The type depended on some template parameters. | |
3494 | If they are fully specialized in the | |
3495 | specialization, that's OK. */ | |
3496 | int j; | |
3497 | for (j = 0; j < nargs; ++j) | |
3498 | if (tpd2.parms[j] != 0 | |
3499 | && tpd.arg_uses_template_parms [j]) | |
3500 | { | |
3501 | error ("type %qT of template argument %qE depends " | |
3502 | "on template parameter(s)", | |
3503 | type, | |
3504 | arg); | |
3505 | break; | |
3506 | } | |
3507 | } | |
3508 | } | |
3509 | } | |
3510 | } | |
6c30752f MM |
3511 | } |
3512 | ||
c8094d83 | 3513 | if (retrieve_specialization (maintmpl, specargs, |
c7222c02 | 3514 | /*class_specializations_p=*/true)) |
6c30752f MM |
3515 | /* We've already got this specialization. */ |
3516 | return decl; | |
3517 | ||
d8b64f80 | 3518 | DECL_TEMPLATE_SPECIALIZATIONS (maintmpl) |
916b63c3 | 3519 | = tree_cons (specargs, inner_parms, |
b1d7b1c0 | 3520 | DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)); |
6c30752f MM |
3521 | TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type; |
3522 | return decl; | |
3523 | } | |
3524 | ||
85d85234 DG |
3525 | /* Check that a template declaration's use of default arguments and |
3526 | parameter packs is not invalid. Here, PARMS are the template | |
3527 | parameters. IS_PRIMARY is nonzero if DECL is the thing declared by | |
3528 | a primary template. IS_PARTIAL is nonzero if DECL is a partial | |
3529 | specialization. | |
9b7dd5e8 | 3530 | |
6ba89f8e | 3531 | |
9b7dd5e8 DG |
3532 | IS_FRIEND_DECL is nonzero if DECL is a friend function template |
3533 | declaration (but not a definition); 1 indicates a declaration, 2 | |
3534 | indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are | |
3535 | emitted for extraneous default arguments. | |
3536 | ||
3537 | Returns TRUE if there were no errors found, FALSE otherwise. */ | |
3538 | ||
3539 | bool | |
3540 | check_default_tmpl_args (tree decl, tree parms, int is_primary, | |
3541 | int is_partial, int is_friend_decl) | |
6ba89f8e | 3542 | { |
d8e178a0 | 3543 | const char *msg; |
66191c20 MM |
3544 | int last_level_to_check; |
3545 | tree parm_level; | |
9b7dd5e8 | 3546 | bool no_errors = true; |
6ba89f8e | 3547 | |
c8094d83 | 3548 | /* [temp.param] |
6ba89f8e MM |
3549 | |
3550 | A default template-argument shall not be specified in a | |
3551 | function template declaration or a function template definition, nor | |
3552 | in the template-parameter-list of the definition of a member of a | |
3553 | class template. */ | |
3554 | ||
4f1c5b7d | 3555 | if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL) |
e0942dcd MM |
3556 | /* You can't have a function template declaration in a local |
3557 | scope, nor you can you define a member of a class template in a | |
3558 | local scope. */ | |
9b7dd5e8 | 3559 | return true; |
e0942dcd | 3560 | |
6ba89f8e MM |
3561 | if (current_class_type |
3562 | && !TYPE_BEING_DEFINED (current_class_type) | |
3d7de1fa | 3563 | && DECL_LANG_SPECIFIC (decl) |
5937a6f9 MM |
3564 | /* If this is either a friend defined in the scope of the class |
3565 | or a member function. */ | |
6df5158a NS |
3566 | && (DECL_FUNCTION_MEMBER_P (decl) |
3567 | ? same_type_p (DECL_CONTEXT (decl), current_class_type) | |
3568 | : DECL_FRIEND_CONTEXT (decl) | |
3569 | ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type) | |
3570 | : false) | |
5937a6f9 MM |
3571 | /* And, if it was a member function, it really was defined in |
3572 | the scope of the class. */ | |
6df5158a NS |
3573 | && (!DECL_FUNCTION_MEMBER_P (decl) |
3574 | || DECL_INITIALIZED_IN_CLASS_P (decl))) | |
6ba89f8e | 3575 | /* We already checked these parameters when the template was |
5937a6f9 MM |
3576 | declared, so there's no need to do it again now. This function |
3577 | was defined in class scope, but we're processing it's body now | |
3578 | that the class is complete. */ | |
9b7dd5e8 | 3579 | return true; |
c8094d83 | 3580 | |
9b7dd5e8 DG |
3581 | /* Core issue 226 (C++0x only): the following only applies to class |
3582 | templates. */ | |
c1ae8be5 | 3583 | if ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL) |
66191c20 | 3584 | { |
9b7dd5e8 | 3585 | /* [temp.param] |
66191c20 | 3586 | |
9b7dd5e8 DG |
3587 | If a template-parameter has a default template-argument, all |
3588 | subsequent template-parameters shall have a default | |
3589 | template-argument supplied. */ | |
3590 | for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level)) | |
3591 | { | |
3592 | tree inner_parms = TREE_VALUE (parm_level); | |
3593 | int ntparms = TREE_VEC_LENGTH (inner_parms); | |
3594 | int seen_def_arg_p = 0; | |
3595 | int i; | |
42b304f1 | 3596 | |
9b7dd5e8 DG |
3597 | for (i = 0; i < ntparms; ++i) |
3598 | { | |
3599 | tree parm = TREE_VEC_ELT (inner_parms, i); | |
42b304f1 | 3600 | |
9b7dd5e8 DG |
3601 | if (parm == error_mark_node) |
3602 | continue; | |
3603 | ||
3604 | if (TREE_PURPOSE (parm)) | |
3605 | seen_def_arg_p = 1; | |
ffaf1e05 JM |
3606 | else if (seen_def_arg_p |
3607 | && !template_parameter_pack_p (TREE_VALUE (parm))) | |
9b7dd5e8 DG |
3608 | { |
3609 | error ("no default argument for %qD", TREE_VALUE (parm)); | |
3610 | /* For better subsequent error-recovery, we indicate that | |
3611 | there should have been a default argument. */ | |
3612 | TREE_PURPOSE (parm) = error_mark_node; | |
3613 | no_errors = false; | |
3614 | } | |
85d85234 DG |
3615 | else if (is_primary |
3616 | && !is_partial | |
3617 | && !is_friend_decl | |
ffaf1e05 JM |
3618 | /* Don't complain about an enclosing partial |
3619 | specialization. */ | |
3620 | && parm_level == parms | |
85d85234 DG |
3621 | && TREE_CODE (decl) == TYPE_DECL |
3622 | && i < ntparms - 1 | |
3623 | && template_parameter_pack_p (TREE_VALUE (parm))) | |
3624 | { | |
3625 | /* A primary class template can only have one | |
3626 | parameter pack, at the end of the template | |
3627 | parameter list. */ | |
3628 | ||
3629 | if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL) | |
3630 | error ("parameter pack %qE must be at the end of the" | |
3631 | " template parameter list", TREE_VALUE (parm)); | |
3632 | else | |
3633 | error ("parameter pack %qT must be at the end of the" | |
3634 | " template parameter list", | |
3635 | TREE_TYPE (TREE_VALUE (parm))); | |
3636 | ||
3637 | TREE_VALUE (TREE_VEC_ELT (inner_parms, i)) | |
3638 | = error_mark_node; | |
3639 | no_errors = false; | |
3640 | } | |
9b7dd5e8 DG |
3641 | } |
3642 | } | |
66191c20 MM |
3643 | } |
3644 | ||
c1ae8be5 | 3645 | if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL) |
9b7dd5e8 DG |
3646 | || is_partial |
3647 | || !is_primary | |
3648 | || is_friend_decl) | |
6ba89f8e MM |
3649 | /* For an ordinary class template, default template arguments are |
3650 | allowed at the innermost level, e.g.: | |
0cbd7506 | 3651 | template <class T = int> |
6ba89f8e MM |
3652 | struct S {}; |
3653 | but, in a partial specialization, they're not allowed even | |
3654 | there, as we have in [temp.class.spec]: | |
c8094d83 | 3655 | |
6ba89f8e | 3656 | The template parameter list of a specialization shall not |
c8094d83 | 3657 | contain default template argument values. |
6ba89f8e | 3658 | |
9b7dd5e8 DG |
3659 | So, for a partial specialization, or for a function template |
3660 | (in C++98/C++03), we look at all of them. */ | |
6ba89f8e MM |
3661 | ; |
3662 | else | |
3663 | /* But, for a primary class template that is not a partial | |
3664 | specialization we look at all template parameters except the | |
3665 | innermost ones. */ | |
3666 | parms = TREE_CHAIN (parms); | |
3667 | ||
3668 | /* Figure out what error message to issue. */ | |
9b7dd5e8 DG |
3669 | if (is_friend_decl == 2) |
3670 | msg = "default template arguments may not be used in function template friend re-declaration"; | |
3671 | else if (is_friend_decl) | |
3672 | msg = "default template arguments may not be used in function template friend declarations"; | |
c1ae8be5 | 3673 | else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98)) |
8653a2c3 | 3674 | msg = "default template arguments may not be used in function templates"; |
6ba89f8e | 3675 | else if (is_partial) |
8653a2c3 | 3676 | msg = "default template arguments may not be used in partial specializations"; |
6ba89f8e | 3677 | else |
0f51ccfc | 3678 | msg = "default argument for template parameter for class enclosing %qD"; |
6ba89f8e MM |
3679 | |
3680 | if (current_class_type && TYPE_BEING_DEFINED (current_class_type)) | |
3681 | /* If we're inside a class definition, there's no need to | |
104bf76a | 3682 | examine the parameters to the class itself. On the one |
6ba89f8e | 3683 | hand, they will be checked when the class is defined, and, |
0e339752 | 3684 | on the other, default arguments are valid in things like: |
0cbd7506 MS |
3685 | template <class T = double> |
3686 | struct S { template <class U> void f(U); }; | |
6ba89f8e MM |
3687 | Here the default argument for `S' has no bearing on the |
3688 | declaration of `f'. */ | |
3689 | last_level_to_check = template_class_depth (current_class_type) + 1; | |
3690 | else | |
3691 | /* Check everything. */ | |
3692 | last_level_to_check = 0; | |
3693 | ||
c8094d83 MS |
3694 | for (parm_level = parms; |
3695 | parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check; | |
66191c20 | 3696 | parm_level = TREE_CHAIN (parm_level)) |
6ba89f8e | 3697 | { |
66191c20 MM |
3698 | tree inner_parms = TREE_VALUE (parm_level); |
3699 | int i; | |
3700 | int ntparms; | |
6ba89f8e MM |
3701 | |
3702 | ntparms = TREE_VEC_LENGTH (inner_parms); | |
c8094d83 | 3703 | for (i = 0; i < ntparms; ++i) |
42b304f1 LM |
3704 | { |
3705 | if (TREE_VEC_ELT (inner_parms, i) == error_mark_node) | |
3706 | continue; | |
6ba89f8e | 3707 | |
42b304f1 LM |
3708 | if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i))) |
3709 | { | |
3710 | if (msg) | |
3711 | { | |
9b7dd5e8 DG |
3712 | no_errors = false; |
3713 | if (is_friend_decl == 2) | |
3714 | return no_errors; | |
3715 | ||
42b304f1 LM |
3716 | error (msg, decl); |
3717 | msg = 0; | |
3718 | } | |
3719 | ||
3720 | /* Clear out the default argument so that we are not | |
3721 | confused later. */ | |
3722 | TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE; | |
3723 | } | |
3724 | } | |
6ba89f8e MM |
3725 | |
3726 | /* At this point, if we're still interested in issuing messages, | |
3727 | they must apply to classes surrounding the object declared. */ | |
3728 | if (msg) | |
c8094d83 | 3729 | msg = "default argument for template parameter for class enclosing %qD"; |
6ba89f8e | 3730 | } |
9b7dd5e8 DG |
3731 | |
3732 | return no_errors; | |
6ba89f8e MM |
3733 | } |
3734 | ||
5dd236e2 NS |
3735 | /* Worker for push_template_decl_real, called via |
3736 | for_each_template_parm. DATA is really an int, indicating the | |
3737 | level of the parameters we are interested in. If T is a template | |
838dfd8a | 3738 | parameter of that level, return nonzero. */ |
5dd236e2 NS |
3739 | |
3740 | static int | |
3a978d72 | 3741 | template_parm_this_level_p (tree t, void* data) |
5dd236e2 | 3742 | { |
6e04241f | 3743 | int this_level = *(int *)data; |
5dd236e2 NS |
3744 | int level; |
3745 | ||
3746 | if (TREE_CODE (t) == TEMPLATE_PARM_INDEX) | |
3747 | level = TEMPLATE_PARM_LEVEL (t); | |
3748 | else | |
3749 | level = TEMPLATE_TYPE_LEVEL (t); | |
3750 | return level == this_level; | |
3751 | } | |
3752 | ||
3ac3d9ea | 3753 | /* Creates a TEMPLATE_DECL for the indicated DECL using the template |
f84b4be9 JM |
3754 | parameters given by current_template_args, or reuses a |
3755 | previously existing one, if appropriate. Returns the DECL, or an | |
c8094d83 | 3756 | equivalent one, if it is replaced via a call to duplicate_decls. |
6757edfe | 3757 | |
d63d5d0c | 3758 | If IS_FRIEND is true, DECL is a friend declaration. */ |
3ac3d9ea MM |
3759 | |
3760 | tree | |
d63d5d0c | 3761 | push_template_decl_real (tree decl, bool is_friend) |
9a3b49ac MS |
3762 | { |
3763 | tree tmpl; | |
f84b4be9 | 3764 | tree args; |
9a3b49ac | 3765 | tree info; |
f84b4be9 JM |
3766 | tree ctx; |
3767 | int primary; | |
6ba89f8e | 3768 | int is_partial; |
cfe507be | 3769 | int new_template_p = 0; |
c7222c02 MM |
3770 | /* True if the template is a member template, in the sense of |
3771 | [temp.mem]. */ | |
3772 | bool member_template_p = false; | |
6ba89f8e | 3773 | |
b17c40be PC |
3774 | if (decl == error_mark_node || !current_template_parms) |
3775 | return error_mark_node; | |
c0694c4b | 3776 | |
6ba89f8e | 3777 | /* See if this is a partial specialization. */ |
9188c363 | 3778 | is_partial = (DECL_IMPLICIT_TYPEDEF_P (decl) |
6ba89f8e | 3779 | && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE |
370af2d5 | 3780 | && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl))); |
6757edfe | 3781 | |
d63d5d0c ILT |
3782 | if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl)) |
3783 | is_friend = true; | |
f84b4be9 JM |
3784 | |
3785 | if (is_friend) | |
3786 | /* For a friend, we want the context of the friend function, not | |
3787 | the type of which it is a friend. */ | |
3788 | ctx = DECL_CONTEXT (decl); | |
4f1c5b7d MM |
3789 | else if (CP_DECL_CONTEXT (decl) |
3790 | && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL) | |
f84b4be9 JM |
3791 | /* In the case of a virtual function, we want the class in which |
3792 | it is defined. */ | |
4f1c5b7d | 3793 | ctx = CP_DECL_CONTEXT (decl); |
f84b4be9 | 3794 | else |
dc957d14 | 3795 | /* Otherwise, if we're currently defining some class, the DECL |
f84b4be9 | 3796 | is assumed to be a member of the class. */ |
9188c363 | 3797 | ctx = current_scope (); |
f84b4be9 | 3798 | |
2c73f9f5 ML |
3799 | if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL) |
3800 | ctx = NULL_TREE; | |
3801 | ||
3802 | if (!DECL_CONTEXT (decl)) | |
cb0dbb9a | 3803 | DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace); |
2c73f9f5 | 3804 | |
6ba89f8e | 3805 | /* See if this is a primary template. */ |
c9cbfca6 JM |
3806 | if (is_friend && ctx) |
3807 | /* A friend template that specifies a class context, i.e. | |
3808 | template <typename T> friend void A<T>::f(); | |
3809 | is not primary. */ | |
3810 | primary = 0; | |
3811 | else | |
3812 | primary = template_parm_scope_p (); | |
9a3b49ac | 3813 | |
83566abf JM |
3814 | if (primary) |
3815 | { | |
c7222c02 MM |
3816 | if (DECL_CLASS_SCOPE_P (decl)) |
3817 | member_template_p = true; | |
2f1b1731 MM |
3818 | if (TREE_CODE (decl) == TYPE_DECL |
3819 | && ANON_AGGRNAME_P (DECL_NAME (decl))) | |
80d7287f PC |
3820 | { |
3821 | error ("template class without a name"); | |
3822 | return error_mark_node; | |
3823 | } | |
717a7d5d | 3824 | else if (TREE_CODE (decl) == FUNCTION_DECL) |
4b0d3cbe | 3825 | { |
717a7d5d MM |
3826 | if (DECL_DESTRUCTOR_P (decl)) |
3827 | { | |
3828 | /* [temp.mem] | |
c8094d83 | 3829 | |
0cbd7506 | 3830 | A destructor shall not be a member template. */ |
0f51ccfc | 3831 | error ("destructor %qD declared as member template", decl); |
717a7d5d MM |
3832 | return error_mark_node; |
3833 | } | |
3834 | if (NEW_DELETE_OPNAME_P (DECL_NAME (decl)) | |
3835 | && (!TYPE_ARG_TYPES (TREE_TYPE (decl)) | |
3836 | || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node | |
3837 | || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl))) | |
3838 | || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl)))) | |
3839 | == void_list_node))) | |
3840 | { | |
c8094d83 | 3841 | /* [basic.stc.dynamic.allocation] |
717a7d5d | 3842 | |
0cbd7506 | 3843 | An allocation function can be a function |
717a7d5d MM |
3844 | template. ... Template allocation functions shall |
3845 | have two or more parameters. */ | |
0f51ccfc | 3846 | error ("invalid template declaration of %qD", decl); |
7c60008e | 3847 | return error_mark_node; |
717a7d5d | 3848 | } |
4b0d3cbe | 3849 | } |
8ca4bf25 MM |
3850 | else if (DECL_IMPLICIT_TYPEDEF_P (decl) |
3851 | && CLASS_TYPE_P (TREE_TYPE (decl))) | |
2aaf816d JM |
3852 | /* OK */; |
3853 | else | |
11325dcd | 3854 | { |
0f51ccfc | 3855 | error ("template declaration of %q#D", decl); |
11325dcd KL |
3856 | return error_mark_node; |
3857 | } | |
83566abf JM |
3858 | } |
3859 | ||
6ba89f8e MM |
3860 | /* Check to see that the rules regarding the use of default |
3861 | arguments are not being violated. */ | |
c8094d83 | 3862 | check_default_tmpl_args (decl, current_template_parms, |
9b7dd5e8 | 3863 | primary, is_partial, /*is_friend_decl=*/0); |
73aad9b9 | 3864 | |
5d80a306 DG |
3865 | /* Ensure that there are no parameter packs in the type of this |
3866 | declaration that have not been expanded. */ | |
b1d7b1c0 DG |
3867 | if (TREE_CODE (decl) == FUNCTION_DECL) |
3868 | { | |
3869 | /* Check each of the arguments individually to see if there are | |
3870 | any bare parameter packs. */ | |
3871 | tree type = TREE_TYPE (decl); | |
3872 | tree arg = DECL_ARGUMENTS (decl); | |
3873 | tree argtype = TYPE_ARG_TYPES (type); | |
3874 | ||
3875 | while (arg && argtype) | |
3876 | { | |
3877 | if (!FUNCTION_PARAMETER_PACK_P (arg) | |
7b3e2d46 | 3878 | && check_for_bare_parameter_packs (TREE_TYPE (arg))) |
b1d7b1c0 DG |
3879 | { |
3880 | /* This is a PARM_DECL that contains unexpanded parameter | |
3881 | packs. We have already complained about this in the | |
3882 | check_for_bare_parameter_packs call, so just replace | |
3883 | these types with ERROR_MARK_NODE. */ | |
3884 | TREE_TYPE (arg) = error_mark_node; | |
3885 | TREE_VALUE (argtype) = error_mark_node; | |
3886 | } | |
3887 | ||
3888 | arg = TREE_CHAIN (arg); | |
3889 | argtype = TREE_CHAIN (argtype); | |
3890 | } | |
3891 | ||
3892 | /* Check for bare parameter packs in the return type and the | |
3893 | exception specifiers. */ | |
7b3e2d46 | 3894 | if (check_for_bare_parameter_packs (TREE_TYPE (type))) |
046e4071 JJ |
3895 | /* Errors were already issued, set return type to int |
3896 | as the frontend doesn't expect error_mark_node as | |
3897 | the return type. */ | |
3898 | TREE_TYPE (type) = integer_type_node; | |
7b3e2d46 DG |
3899 | if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type))) |
3900 | TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE; | |
b1d7b1c0 | 3901 | } |
7b3e2d46 | 3902 | else if (check_for_bare_parameter_packs (TREE_TYPE (decl))) |
92fab505 DG |
3903 | { |
3904 | TREE_TYPE (decl) = error_mark_node; | |
3905 | return error_mark_node; | |
3906 | } | |
5d80a306 | 3907 | |
6ba89f8e MM |
3908 | if (is_partial) |
3909 | return process_partial_specialization (decl); | |
d32789d8 | 3910 | |
9a3b49ac MS |
3911 | args = current_template_args (); |
3912 | ||
c8094d83 | 3913 | if (!ctx |
f84b4be9 | 3914 | || TREE_CODE (ctx) == FUNCTION_DECL |
a723baf1 | 3915 | || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx)) |
f84b4be9 | 3916 | || (is_friend && !DECL_TEMPLATE_INFO (decl))) |
8d08fdba | 3917 | { |
75650646 | 3918 | if (DECL_LANG_SPECIFIC (decl) |
f84b4be9 JM |
3919 | && DECL_TEMPLATE_INFO (decl) |
3920 | && DECL_TI_TEMPLATE (decl)) | |
3921 | tmpl = DECL_TI_TEMPLATE (decl); | |
1c10870d AS |
3922 | /* If DECL is a TYPE_DECL for a class-template, then there won't |
3923 | be DECL_LANG_SPECIFIC. The information equivalent to | |
3924 | DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */ | |
c8094d83 | 3925 | else if (DECL_IMPLICIT_TYPEDEF_P (decl) |
1c10870d AS |
3926 | && TYPE_TEMPLATE_INFO (TREE_TYPE (decl)) |
3927 | && TYPE_TI_TEMPLATE (TREE_TYPE (decl))) | |
3928 | { | |
3929 | /* Since a template declaration already existed for this | |
3930 | class-type, we must be redeclaring it here. Make sure | |
0e339752 | 3931 | that the redeclaration is valid. */ |
1c10870d AS |
3932 | redeclare_class_template (TREE_TYPE (decl), |
3933 | current_template_parms); | |
3934 | /* We don't need to create a new TEMPLATE_DECL; just use the | |
3935 | one we already had. */ | |
3936 | tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl)); | |
3937 | } | |
f84b4be9 | 3938 | else |
786b5245 | 3939 | { |
c7222c02 MM |
3940 | tmpl = build_template_decl (decl, current_template_parms, |
3941 | member_template_p); | |
cfe507be MM |
3942 | new_template_p = 1; |
3943 | ||
f84b4be9 JM |
3944 | if (DECL_LANG_SPECIFIC (decl) |
3945 | && DECL_TEMPLATE_SPECIALIZATION (decl)) | |
3946 | { | |
3947 | /* A specialization of a member template of a template | |
c6002625 | 3948 | class. */ |
f84b4be9 JM |
3949 | SET_DECL_TEMPLATE_SPECIALIZATION (tmpl); |
3950 | DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl); | |
3951 | DECL_TEMPLATE_INFO (decl) = NULL_TREE; | |
3952 | } | |
786b5245 | 3953 | } |
8d08fdba MS |
3954 | } |
3955 | else | |
3956 | { | |
e1a5ccf7 | 3957 | tree a, t, current, parms; |
ba4f4e5d | 3958 | int i; |
91a77d68 | 3959 | tree tinfo = get_template_info (decl); |
6633d636 | 3960 | |
91a77d68 | 3961 | if (!tinfo) |
c91a56d2 | 3962 | { |
0f51ccfc | 3963 | error ("template definition of non-template %q#D", decl); |
3fe82414 | 3964 | return error_mark_node; |
c91a56d2 | 3965 | } |
91a77d68 JM |
3966 | |
3967 | tmpl = TI_TEMPLATE (tinfo); | |
c8094d83 | 3968 | |
c353b8e3 | 3969 | if (DECL_FUNCTION_TEMPLATE_P (tmpl) |
c8094d83 | 3970 | && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl) |
c353b8e3 | 3971 | && DECL_TEMPLATE_SPECIALIZATION (decl) |
c7222c02 | 3972 | && DECL_MEMBER_TEMPLATE_P (tmpl)) |
98c1c668 | 3973 | { |
e1a5ccf7 JM |
3974 | tree new_tmpl; |
3975 | ||
3976 | /* The declaration is a specialization of a member | |
3977 | template, declared outside the class. Therefore, the | |
3978 | innermost template arguments will be NULL, so we | |
3979 | replace them with the arguments determined by the | |
3980 | earlier call to check_explicit_specialization. */ | |
3981 | args = DECL_TI_ARGS (decl); | |
3982 | ||
c8094d83 | 3983 | new_tmpl |
c7222c02 MM |
3984 | = build_template_decl (decl, current_template_parms, |
3985 | member_template_p); | |
e1a5ccf7 JM |
3986 | DECL_TEMPLATE_RESULT (new_tmpl) = decl; |
3987 | TREE_TYPE (new_tmpl) = TREE_TYPE (decl); | |
3988 | DECL_TI_TEMPLATE (decl) = new_tmpl; | |
3989 | SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl); | |
c8094d83 | 3990 | DECL_TEMPLATE_INFO (new_tmpl) |
e1b3e07d | 3991 | = tree_cons (tmpl, args, NULL_TREE); |
e1a5ccf7 | 3992 | |
c8094d83 MS |
3993 | register_specialization (new_tmpl, |
3994 | most_general_template (tmpl), | |
d63d5d0c ILT |
3995 | args, |
3996 | is_friend); | |
e1a5ccf7 | 3997 | return decl; |
98c1c668 | 3998 | } |
98c1c668 | 3999 | |
e1a5ccf7 | 4000 | /* Make sure the template headers we got make sense. */ |
6633d636 | 4001 | |
e1a5ccf7 JM |
4002 | parms = DECL_TEMPLATE_PARMS (tmpl); |
4003 | i = TMPL_PARMS_DEPTH (parms); | |
4004 | if (TMPL_ARGS_DEPTH (args) != i) | |
98c1c668 | 4005 | { |
0f51ccfc | 4006 | error ("expected %d levels of template parms for %q#D, got %d", |
0cbd7506 | 4007 | i, decl, TMPL_ARGS_DEPTH (args)); |
98c1c668 | 4008 | } |
e1a5ccf7 JM |
4009 | else |
4010 | for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms)) | |
4011 | { | |
4012 | a = TMPL_ARGS_LEVEL (args, i); | |
4013 | t = INNERMOST_TEMPLATE_PARMS (parms); | |
4014 | ||
4015 | if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a)) | |
4016 | { | |
4017 | if (current == decl) | |
0f51ccfc | 4018 | error ("got %d template parameters for %q#D", |
0cbd7506 | 4019 | TREE_VEC_LENGTH (a), decl); |
e1a5ccf7 | 4020 | else |
0f51ccfc | 4021 | error ("got %d template parameters for %q#T", |
0cbd7506 | 4022 | TREE_VEC_LENGTH (a), current); |
33bd39a2 | 4023 | error (" but %d required", TREE_VEC_LENGTH (t)); |
f1cc0515 | 4024 | return error_mark_node; |
e1a5ccf7 | 4025 | } |
98c1c668 | 4026 | |
e1a5ccf7 JM |
4027 | if (current == decl) |
4028 | current = ctx; | |
4029 | else | |
ba75df2b PC |
4030 | current = (TYPE_P (current) |
4031 | ? TYPE_CONTEXT (current) | |
4032 | : DECL_CONTEXT (current)); | |
e1a5ccf7 | 4033 | } |
91a77d68 JM |
4034 | |
4035 | /* Check that the parms are used in the appropriate qualifying scopes | |
4036 | in the declarator. */ | |
4037 | if (!comp_template_args | |
4038 | (TI_ARGS (tinfo), | |
4039 | TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl))))) | |
4040 | { | |
4041 | error ("\ | |
4042 | template arguments to %qD do not match original template %qD", | |
4043 | decl, DECL_TEMPLATE_RESULT (tmpl)); | |
4044 | if (!uses_template_parms (TI_ARGS (tinfo))) | |
1f5b3869 | 4045 | inform (input_location, "use template<> for an explicit specialization"); |
91a77d68 JM |
4046 | /* Avoid crash in import_export_decl. */ |
4047 | DECL_INTERFACE_KNOWN (decl) = 1; | |
4048 | return error_mark_node; | |
4049 | } | |
5566b478 | 4050 | } |
8d08fdba | 4051 | |
5566b478 MS |
4052 | DECL_TEMPLATE_RESULT (tmpl) = decl; |
4053 | TREE_TYPE (tmpl) = TREE_TYPE (decl); | |
8d08fdba | 4054 | |
36a117a5 MM |
4055 | /* Push template declarations for global functions and types. Note |
4056 | that we do not try to push a global template friend declared in a | |
4057 | template class; such a thing may well depend on the template | |
39c01e4c | 4058 | parameters of the class. */ |
c8094d83 | 4059 | if (new_template_p && !ctx |
36a117a5 | 4060 | && !(is_friend && template_class_depth (current_class_type) > 0)) |
c6f9f83b | 4061 | { |
d63d5d0c | 4062 | tmpl = pushdecl_namespace_level (tmpl, is_friend); |
c6f9f83b KL |
4063 | if (tmpl == error_mark_node) |
4064 | return error_mark_node; | |
bd3d082e KL |
4065 | |
4066 | /* Hide template friend classes that haven't been declared yet. */ | |
4067 | if (is_friend && TREE_CODE (decl) == TYPE_DECL) | |
4068 | { | |
4069 | DECL_ANTICIPATED (tmpl) = 1; | |
4070 | DECL_FRIEND_P (tmpl) = 1; | |
4071 | } | |
c6f9f83b | 4072 | } |
8d08fdba | 4073 | |
5566b478 | 4074 | if (primary) |
5dd236e2 | 4075 | { |
ef3b7b17 JM |
4076 | tree parms = DECL_TEMPLATE_PARMS (tmpl); |
4077 | int i; | |
4078 | ||
5dd236e2 NS |
4079 | DECL_PRIMARY_TEMPLATE (tmpl) = tmpl; |
4080 | if (DECL_CONV_FN_P (tmpl)) | |
4081 | { | |
ef3b7b17 | 4082 | int depth = TMPL_PARMS_DEPTH (parms); |
6e04241f | 4083 | |
5dd236e2 NS |
4084 | /* It is a conversion operator. See if the type converted to |
4085 | depends on innermost template operands. */ | |
c8094d83 | 4086 | |
d43f603d KL |
4087 | if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl)), |
4088 | depth)) | |
5dd236e2 NS |
4089 | DECL_TEMPLATE_CONV_FN_P (tmpl) = 1; |
4090 | } | |
ef3b7b17 JM |
4091 | |
4092 | /* Give template template parms a DECL_CONTEXT of the template | |
4093 | for which they are a parameter. */ | |
4094 | parms = INNERMOST_TEMPLATE_PARMS (parms); | |
4095 | for (i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i) | |
4096 | { | |
4097 | tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i)); | |
4098 | if (TREE_CODE (parm) == TEMPLATE_DECL) | |
4099 | DECL_CONTEXT (parm) = tmpl; | |
4100 | } | |
5dd236e2 | 4101 | } |
5566b478 | 4102 | |
a692ad2e | 4103 | /* The DECL_TI_ARGS of DECL contains full set of arguments referring |
28ba38e3 KL |
4104 | back to its most general template. If TMPL is a specialization, |
4105 | ARGS may only have the innermost set of arguments. Add the missing | |
4106 | argument levels if necessary. */ | |
4107 | if (DECL_TEMPLATE_INFO (tmpl)) | |
4108 | args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args); | |
4109 | ||
e1b3e07d | 4110 | info = tree_cons (tmpl, args, NULL_TREE); |
5566b478 | 4111 | |
9188c363 | 4112 | if (DECL_IMPLICIT_TYPEDEF_P (decl)) |
a0ad3539 | 4113 | SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info); |
2aaf816d | 4114 | else if (DECL_LANG_SPECIFIC (decl)) |
5566b478 | 4115 | DECL_TEMPLATE_INFO (decl) = info; |
3ac3d9ea MM |
4116 | |
4117 | return DECL_TEMPLATE_RESULT (tmpl); | |
8d08fdba MS |
4118 | } |
4119 | ||
6757edfe | 4120 | tree |
3a978d72 | 4121 | push_template_decl (tree decl) |
6757edfe | 4122 | { |
d63d5d0c | 4123 | return push_template_decl_real (decl, false); |
6757edfe MM |
4124 | } |
4125 | ||
4126 | /* Called when a class template TYPE is redeclared with the indicated | |
4127 | template PARMS, e.g.: | |
7fe6899f MM |
4128 | |
4129 | template <class T> struct S; | |
4130 | template <class T> struct S {}; */ | |
4131 | ||
60feef2c | 4132 | bool |
3a978d72 | 4133 | redeclare_class_template (tree type, tree parms) |
7fe6899f | 4134 | { |
3d7de1fa | 4135 | tree tmpl; |
6757edfe | 4136 | tree tmpl_parms; |
7fe6899f MM |
4137 | int i; |
4138 | ||
3d7de1fa MM |
4139 | if (!TYPE_TEMPLATE_INFO (type)) |
4140 | { | |
0f51ccfc | 4141 | error ("%qT is not a template type", type); |
60feef2c | 4142 | return false; |
3d7de1fa MM |
4143 | } |
4144 | ||
4145 | tmpl = TYPE_TI_TEMPLATE (type); | |
7fe6899f MM |
4146 | if (!PRIMARY_TEMPLATE_P (tmpl)) |
4147 | /* The type is nested in some template class. Nothing to worry | |
4148 | about here; there are no new template parameters for the nested | |
4149 | type. */ | |
60feef2c | 4150 | return true; |
7fe6899f | 4151 | |
ee921ddf MM |
4152 | if (!parms) |
4153 | { | |
4154 | error ("template specifiers not specified in declaration of %qD", | |
4155 | tmpl); | |
60feef2c | 4156 | return false; |
ee921ddf MM |
4157 | } |
4158 | ||
6757edfe MM |
4159 | parms = INNERMOST_TEMPLATE_PARMS (parms); |
4160 | tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl); | |
4161 | ||
7fe6899f MM |
4162 | if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms)) |
4163 | { | |
b131ad7c MLI |
4164 | error ("redeclared with %d template parameter(s)", |
4165 | TREE_VEC_LENGTH (parms)); | |
1f5b3869 | 4166 | inform (input_location, "previous declaration %q+D used %d template parameter(s)", |
b131ad7c | 4167 | tmpl, TREE_VEC_LENGTH (tmpl_parms)); |
60feef2c | 4168 | return false; |
7fe6899f MM |
4169 | } |
4170 | ||
4171 | for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i) | |
4172 | { | |
2d8ba2c7 LM |
4173 | tree tmpl_parm; |
4174 | tree parm; | |
4175 | tree tmpl_default; | |
4176 | tree parm_default; | |
4177 | ||
4178 | if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node | |
4179 | || TREE_VEC_ELT (parms, i) == error_mark_node) | |
4180 | continue; | |
4181 | ||
4182 | tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i)); | |
4183 | parm = TREE_VALUE (TREE_VEC_ELT (parms, i)); | |
4184 | tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)); | |
4185 | parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i)); | |
7fe6899f | 4186 | |
2649701f KL |
4187 | /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or |
4188 | TEMPLATE_DECL. */ | |
0f67a82f | 4189 | if (tmpl_parm != error_mark_node |
a022041e DG |
4190 | && (TREE_CODE (tmpl_parm) != TREE_CODE (parm) |
4191 | || (TREE_CODE (tmpl_parm) != TYPE_DECL | |
4192 | && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm))) | |
4193 | || (TREE_CODE (tmpl_parm) != PARM_DECL | |
4194 | && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm)) | |
4195 | != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))) | |
4196 | || (TREE_CODE (tmpl_parm) == PARM_DECL | |
4197 | && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm)) | |
4198 | != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))) | |
7fe6899f | 4199 | { |
dee15844 | 4200 | error ("template parameter %q+#D", tmpl_parm); |
0f51ccfc | 4201 | error ("redeclared here as %q#D", parm); |
60feef2c | 4202 | return false; |
7fe6899f MM |
4203 | } |
4204 | ||
4205 | if (tmpl_default != NULL_TREE && parm_default != NULL_TREE) | |
4206 | { | |
4207 | /* We have in [temp.param]: | |
4208 | ||
4209 | A template-parameter may not be given default arguments | |
4210 | by two different declarations in the same scope. */ | |
0f51ccfc | 4211 | error ("redefinition of default argument for %q#D", parm); |
1f5b3869 | 4212 | inform (input_location, "%Joriginal definition appeared here", tmpl_parm); |
60feef2c | 4213 | return false; |
7fe6899f MM |
4214 | } |
4215 | ||
4216 | if (parm_default != NULL_TREE) | |
4217 | /* Update the previous template parameters (which are the ones | |
4218 | that will really count) with the new default value. */ | |
4219 | TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default; | |
7ce74c5e MM |
4220 | else if (tmpl_default != NULL_TREE) |
4221 | /* Update the new parameters, too; they'll be used as the | |
4222 | parameters for any members. */ | |
4223 | TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default; | |
7fe6899f | 4224 | } |
60feef2c LM |
4225 | |
4226 | return true; | |
7fe6899f | 4227 | } |
75650646 | 4228 | |
9baa27a9 MM |
4229 | /* Simplify EXPR if it is a non-dependent expression. Returns the |
4230 | (possibly simplified) expression. */ | |
4231 | ||
4232 | tree | |
4233 | fold_non_dependent_expr (tree expr) | |
4234 | { | |
d4a200d3 SM |
4235 | if (expr == NULL_TREE) |
4236 | return NULL_TREE; | |
4237 | ||
9baa27a9 MM |
4238 | /* If we're in a template, but EXPR isn't value dependent, simplify |
4239 | it. We're supposed to treat: | |
c8094d83 | 4240 | |
9baa27a9 MM |
4241 | template <typename T> void f(T[1 + 1]); |
4242 | template <typename T> void f(T[2]); | |
c8094d83 | 4243 | |
9baa27a9 MM |
4244 | as two declarations of the same function, for example. */ |
4245 | if (processing_template_decl | |
4246 | && !type_dependent_expression_p (expr) | |
7416ab02 | 4247 | && !value_dependent_expression_p (expr)) |
9baa27a9 MM |
4248 | { |
4249 | HOST_WIDE_INT saved_processing_template_decl; | |
4250 | ||
4251 | saved_processing_template_decl = processing_template_decl; | |
4252 | processing_template_decl = 0; | |
4253 | expr = tsubst_copy_and_build (expr, | |
4254 | /*args=*/NULL_TREE, | |
4255 | tf_error, | |
4256 | /*in_decl=*/NULL_TREE, | |
015c2c66 MM |
4257 | /*function_p=*/false, |
4258 | /*integral_constant_expression_p=*/true); | |
9baa27a9 MM |
4259 | processing_template_decl = saved_processing_template_decl; |
4260 | } | |
4261 | return expr; | |
4262 | } | |
4263 | ||
b6ab6892 GB |
4264 | /* EXPR is an expression which is used in a constant-expression context. |
4265 | For instance, it could be a VAR_DECL with a constant initializer. | |
39a13be5 | 4266 | Extract the innermost constant expression. |
c8094d83 | 4267 | |
8a784e4a NS |
4268 | This is basically a more powerful version of |
4269 | integral_constant_value, which can be used also in templates where | |
4270 | initializers can maintain a syntactic rather than semantic form | |
4271 | (even if they are non-dependent, for access-checking purposes). */ | |
b6ab6892 | 4272 | |
993acaec | 4273 | static tree |
b6ab6892 GB |
4274 | fold_decl_constant_value (tree expr) |
4275 | { | |
4ef69b83 GB |
4276 | tree const_expr = expr; |
4277 | do | |
b6ab6892 | 4278 | { |
b6ab6892 | 4279 | expr = fold_non_dependent_expr (const_expr); |
4ef69b83 | 4280 | const_expr = integral_constant_value (expr); |
b6ab6892 | 4281 | } |
4ef69b83 | 4282 | while (expr != const_expr); |
b6ab6892 | 4283 | |
8a784e4a | 4284 | return expr; |
b6ab6892 GB |
4285 | } |
4286 | ||
4287 | /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which | |
4288 | must be a function or a pointer-to-function type, as specified | |
4289 | in [temp.arg.nontype]: disambiguate EXPR if it is an overload set, | |
4290 | and check that the resulting function has external linkage. */ | |
4291 | ||
4292 | static tree | |
4293 | convert_nontype_argument_function (tree type, tree expr) | |
4294 | { | |
4295 | tree fns = expr; | |
4296 | tree fn, fn_no_ptr; | |
4297 | ||
4298 | fn = instantiate_type (type, fns, tf_none); | |
4299 | if (fn == error_mark_node) | |
4300 | return error_mark_node; | |
4301 | ||
4302 | fn_no_ptr = fn; | |
4303 | if (TREE_CODE (fn_no_ptr) == ADDR_EXPR) | |
4304 | fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0); | |
eff3a276 MM |
4305 | if (TREE_CODE (fn_no_ptr) == BASELINK) |
4306 | fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr); | |
4307 | ||
b6ab6892 GB |
4308 | /* [temp.arg.nontype]/1 |
4309 | ||
4310 | A template-argument for a non-type, non-template template-parameter | |
4311 | shall be one of: | |
4312 | [...] | |
4313 | -- the address of an object or function with external linkage. */ | |
4314 | if (!DECL_EXTERNAL_LINKAGE_P (fn_no_ptr)) | |
4315 | { | |
4316 | error ("%qE is not a valid template argument for type %qT " | |
4317 | "because function %qD has not external linkage", | |
4318 | expr, type, fn_no_ptr); | |
4319 | return NULL_TREE; | |
4320 | } | |
4321 | ||
4322 | return fn; | |
4323 | } | |
4324 | ||
75650646 MM |
4325 | /* Attempt to convert the non-type template parameter EXPR to the |
4326 | indicated TYPE. If the conversion is successful, return the | |
dc957d14 | 4327 | converted value. If the conversion is unsuccessful, return |
75650646 MM |
4328 | NULL_TREE if we issued an error message, or error_mark_node if we |
4329 | did not. We issue error messages for out-and-out bad template | |
4330 | parameters, but not simply because the conversion failed, since we | |
9baa27a9 | 4331 | might be just trying to do argument deduction. Both TYPE and EXPR |
b6ab6892 GB |
4332 | must be non-dependent. |
4333 | ||
4334 | The conversion follows the special rules described in | |
4335 | [temp.arg.nontype], and it is much more strict than an implicit | |
4336 | conversion. | |
4337 | ||
4338 | This function is called twice for each template argument (see | |
4339 | lookup_template_class for a more accurate description of this | |
4340 | problem). This means that we need to handle expressions which | |
4341 | are not valid in a C++ source, but can be created from the | |
4342 | first call (for instance, casts to perform conversions). These | |
4343 | hacks can go away after we fix the double coercion problem. */ | |
75650646 MM |
4344 | |
4345 | static tree | |
3a978d72 | 4346 | convert_nontype_argument (tree type, tree expr) |
75650646 | 4347 | { |
9baa27a9 MM |
4348 | tree expr_type; |
4349 | ||
b6ab6892 GB |
4350 | /* Detect immediately string literals as invalid non-type argument. |
4351 | This special-case is not needed for correctness (we would easily | |
4352 | catch this later), but only to provide better diagnostic for this | |
4353 | common user mistake. As suggested by DR 100, we do not mention | |
4354 | linkage issues in the diagnostic as this is not the point. */ | |
4355 | if (TREE_CODE (expr) == STRING_CST) | |
4356 | { | |
4357 | error ("%qE is not a valid template argument for type %qT " | |
4358 | "because string literals can never be used in this context", | |
4359 | expr, type); | |
4360 | return NULL_TREE; | |
4361 | } | |
4362 | ||
9baa27a9 MM |
4363 | /* If we are in a template, EXPR may be non-dependent, but still |
4364 | have a syntactic, rather than semantic, form. For example, EXPR | |
4365 | might be a SCOPE_REF, rather than the VAR_DECL to which the | |
4366 | SCOPE_REF refers. Preserving the qualifying scope is necessary | |
4367 | so that access checking can be performed when the template is | |
4368 | instantiated -- but here we need the resolved form so that we can | |
4369 | convert the argument. */ | |
4370 | expr = fold_non_dependent_expr (expr); | |
b166a559 VR |
4371 | if (error_operand_p (expr)) |
4372 | return error_mark_node; | |
9baa27a9 | 4373 | expr_type = TREE_TYPE (expr); |
75650646 | 4374 | |
b6ab6892 GB |
4375 | /* HACK: Due to double coercion, we can get a |
4376 | NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here, | |
4377 | which is the tree that we built on the first call (see | |
4378 | below when coercing to reference to object or to reference to | |
4379 | function). We just strip everything and get to the arg. | |
4380 | See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C | |
4381 | for examples. */ | |
4382 | if (TREE_CODE (expr) == NOP_EXPR) | |
75650646 | 4383 | { |
b6ab6892 | 4384 | if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type)) |
75650646 | 4385 | { |
b6ab6892 GB |
4386 | /* ??? Maybe we could use convert_from_reference here, but we |
4387 | would need to relax its constraints because the NOP_EXPR | |
4388 | could actually change the type to something more cv-qualified, | |
4389 | and this is not folded by convert_from_reference. */ | |
4390 | tree addr = TREE_OPERAND (expr, 0); | |
4391 | gcc_assert (TREE_CODE (expr_type) == REFERENCE_TYPE); | |
4392 | gcc_assert (TREE_CODE (addr) == ADDR_EXPR); | |
4393 | gcc_assert (TREE_CODE (TREE_TYPE (addr)) == POINTER_TYPE); | |
4394 | gcc_assert (same_type_ignoring_top_level_qualifiers_p | |
4395 | (TREE_TYPE (expr_type), | |
4396 | TREE_TYPE (TREE_TYPE (addr)))); | |
4397 | ||
4398 | expr = TREE_OPERAND (addr, 0); | |
4399 | expr_type = TREE_TYPE (expr); | |
75650646 MM |
4400 | } |
4401 | ||
b6ab6892 GB |
4402 | /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the |
4403 | parameter is a pointer to object, through decay and | |
4404 | qualification conversion. Let's strip everything. */ | |
4405 | else if (TYPE_PTROBV_P (type)) | |
75650646 | 4406 | { |
b6ab6892 GB |
4407 | STRIP_NOPS (expr); |
4408 | gcc_assert (TREE_CODE (expr) == ADDR_EXPR); | |
4409 | gcc_assert (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE); | |
4410 | /* Skip the ADDR_EXPR only if it is part of the decay for | |
4411 | an array. Otherwise, it is part of the original argument | |
4412 | in the source code. */ | |
4413 | if (TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == ARRAY_TYPE) | |
4414 | expr = TREE_OPERAND (expr, 0); | |
4415 | expr_type = TREE_TYPE (expr); | |
f3400fe2 | 4416 | } |
75650646 | 4417 | } |
c61dce3a | 4418 | |
b6ab6892 | 4419 | /* [temp.arg.nontype]/5, bullet 1 |
75650646 | 4420 | |
b6ab6892 GB |
4421 | For a non-type template-parameter of integral or enumeration type, |
4422 | integral promotions (_conv.prom_) and integral conversions | |
4423 | (_conv.integral_) are applied. */ | |
550a799d | 4424 | if (INTEGRAL_OR_ENUMERATION_TYPE_P (type)) |
75650646 | 4425 | { |
550a799d | 4426 | if (!INTEGRAL_OR_ENUMERATION_TYPE_P (expr_type)) |
75650646 | 4427 | return error_mark_node; |
fddabb2c | 4428 | |
b6ab6892 GB |
4429 | expr = fold_decl_constant_value (expr); |
4430 | /* Notice that there are constant expressions like '4 % 0' which | |
4431 | do not fold into integer constants. */ | |
db02b6b9 | 4432 | if (TREE_CODE (expr) != INTEGER_CST) |
b6ab6892 GB |
4433 | { |
4434 | error ("%qE is not a valid template argument for type %qT " | |
4435 | "because it is a non-constant expression", expr, type); | |
4436 | return NULL_TREE; | |
4437 | } | |
75650646 | 4438 | |
b6ab6892 GB |
4439 | /* At this point, an implicit conversion does what we want, |
4440 | because we already know that the expression is of integral | |
4441 | type. */ | |
4442 | expr = ocp_convert (type, expr, CONV_IMPLICIT, LOOKUP_PROTECT); | |
4443 | if (expr == error_mark_node) | |
4444 | return error_mark_node; | |
75650646 | 4445 | |
b6ab6892 GB |
4446 | /* Conversion was allowed: fold it to a bare integer constant. */ |
4447 | expr = fold (expr); | |
4448 | } | |
4449 | /* [temp.arg.nontype]/5, bullet 2 | |
75650646 | 4450 | |
b6ab6892 GB |
4451 | For a non-type template-parameter of type pointer to object, |
4452 | qualification conversions (_conv.qual_) and the array-to-pointer | |
4453 | conversion (_conv.array_) are applied. */ | |
4454 | else if (TYPE_PTROBV_P (type)) | |
4455 | { | |
4456 | /* [temp.arg.nontype]/1 (TC1 version, DR 49): | |
75650646 | 4457 | |
b6ab6892 GB |
4458 | A template-argument for a non-type, non-template template-parameter |
4459 | shall be one of: [...] | |
75650646 | 4460 | |
b6ab6892 GB |
4461 | -- the name of a non-type template-parameter; |
4462 | -- the address of an object or function with external linkage, [...] | |
4463 | expressed as "& id-expression" where the & is optional if the name | |
4464 | refers to a function or array, or if the corresponding | |
4465 | template-parameter is a reference. | |
c8094d83 | 4466 | |
b6ab6892 GB |
4467 | Here, we do not care about functions, as they are invalid anyway |
4468 | for a parameter of type pointer-to-object. */ | |
1082fd10 MM |
4469 | |
4470 | if (DECL_P (expr) && DECL_TEMPLATE_PARM_P (expr)) | |
4471 | /* Non-type template parameters are OK. */ | |
4472 | ; | |
4473 | else if (TREE_CODE (expr) != ADDR_EXPR | |
4474 | && TREE_CODE (expr_type) != ARRAY_TYPE) | |
4475 | { | |
4476 | if (TREE_CODE (expr) == VAR_DECL) | |
4477 | { | |
4478 | error ("%qD is not a valid template argument " | |
4479 | "because %qD is a variable, not the address of " | |
4480 | "a variable", | |
4481 | expr, expr); | |
4482 | return NULL_TREE; | |
4483 | } | |
4484 | /* Other values, like integer constants, might be valid | |
4485 | non-type arguments of some other type. */ | |
4486 | return error_mark_node; | |
4487 | } | |
4488 | else | |
4489 | { | |
4490 | tree decl; | |
4491 | ||
4492 | decl = ((TREE_CODE (expr) == ADDR_EXPR) | |
4493 | ? TREE_OPERAND (expr, 0) : expr); | |
4494 | if (TREE_CODE (decl) != VAR_DECL) | |
4495 | { | |
4496 | error ("%qE is not a valid template argument of type %qT " | |
4497 | "because %qE is not a variable", | |
4498 | expr, type, decl); | |
4499 | return NULL_TREE; | |
4500 | } | |
4501 | else if (!DECL_EXTERNAL_LINKAGE_P (decl)) | |
4502 | { | |
4503 | error ("%qE is not a valid template argument of type %qT " | |
4504 | "because %qD does not have external linkage", | |
4505 | expr, type, decl); | |
4506 | return NULL_TREE; | |
4507 | } | |
4508 | } | |
b6ab6892 GB |
4509 | |
4510 | expr = decay_conversion (expr); | |
4511 | if (expr == error_mark_node) | |
4512 | return error_mark_node; | |
75650646 | 4513 | |
b6ab6892 GB |
4514 | expr = perform_qualification_conversions (type, expr); |
4515 | if (expr == error_mark_node) | |
4516 | return error_mark_node; | |
b6ab6892 GB |
4517 | } |
4518 | /* [temp.arg.nontype]/5, bullet 3 | |
4519 | ||
4520 | For a non-type template-parameter of type reference to object, no | |
4521 | conversions apply. The type referred to by the reference may be more | |
4522 | cv-qualified than the (otherwise identical) type of the | |
4523 | template-argument. The template-parameter is bound directly to the | |
4524 | template-argument, which must be an lvalue. */ | |
4525 | else if (TYPE_REF_OBJ_P (type)) | |
4526 | { | |
4527 | if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type), | |
4528 | expr_type)) | |
4529 | return error_mark_node; | |
75650646 | 4530 | |
b6ab6892 GB |
4531 | if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type)) |
4532 | { | |
4533 | error ("%qE is not a valid template argument for type %qT " | |
4534 | "because of conflicts in cv-qualification", expr, type); | |
4535 | return NULL_TREE; | |
4536 | } | |
c8094d83 | 4537 | |
b6ab6892 GB |
4538 | if (!real_lvalue_p (expr)) |
4539 | { | |
4540 | error ("%qE is not a valid template argument for type %qT " | |
944fd207 | 4541 | "because it is not an lvalue", expr, type); |
b6ab6892 GB |
4542 | return NULL_TREE; |
4543 | } | |
e6e174e5 | 4544 | |
b6ab6892 | 4545 | /* [temp.arg.nontype]/1 |
75650646 | 4546 | |
b6ab6892 GB |
4547 | A template-argument for a non-type, non-template template-parameter |
4548 | shall be one of: [...] | |
75650646 | 4549 | |
03fd3f84 | 4550 | -- the address of an object or function with external linkage. */ |
b6ab6892 GB |
4551 | if (!DECL_EXTERNAL_LINKAGE_P (expr)) |
4552 | { | |
4553 | error ("%qE is not a valid template argument for type %qT " | |
4554 | "because object %qD has not external linkage", | |
4555 | expr, type, expr); | |
4556 | return NULL_TREE; | |
4557 | } | |
0dc09a61 | 4558 | |
b6ab6892 GB |
4559 | expr = build_nop (type, build_address (expr)); |
4560 | } | |
4561 | /* [temp.arg.nontype]/5, bullet 4 | |
4562 | ||
4563 | For a non-type template-parameter of type pointer to function, only | |
4564 | the function-to-pointer conversion (_conv.func_) is applied. If the | |
4565 | template-argument represents a set of overloaded functions (or a | |
4566 | pointer to such), the matching function is selected from the set | |
4567 | (_over.over_). */ | |
4568 | else if (TYPE_PTRFN_P (type)) | |
4569 | { | |
4570 | /* If the argument is a template-id, we might not have enough | |
2c164de6 | 4571 | context information to decay the pointer. */ |
b6ab6892 GB |
4572 | if (!type_unknown_p (expr_type)) |
4573 | { | |
4574 | expr = decay_conversion (expr); | |
4575 | if (expr == error_mark_node) | |
4576 | return error_mark_node; | |
4577 | } | |
75650646 | 4578 | |
b6ab6892 GB |
4579 | expr = convert_nontype_argument_function (type, expr); |
4580 | if (!expr || expr == error_mark_node) | |
4581 | return expr; | |
e350dbbd PB |
4582 | |
4583 | if (TREE_CODE (expr) != ADDR_EXPR) | |
4584 | { | |
4585 | error ("%qE is not a valid template argument for type %qT", expr, type); | |
4586 | error ("it must be the address of a function with external linkage"); | |
4587 | return NULL_TREE; | |
4588 | } | |
b6ab6892 GB |
4589 | } |
4590 | /* [temp.arg.nontype]/5, bullet 5 | |
75650646 | 4591 | |
b6ab6892 GB |
4592 | For a non-type template-parameter of type reference to function, no |
4593 | conversions apply. If the template-argument represents a set of | |
4594 | overloaded functions, the matching function is selected from the set | |
4595 | (_over.over_). */ | |
4596 | else if (TYPE_REFFN_P (type)) | |
4597 | { | |
4598 | if (TREE_CODE (expr) == ADDR_EXPR) | |
4599 | { | |
4600 | error ("%qE is not a valid template argument for type %qT " | |
4601 | "because it is a pointer", expr, type); | |
1f5b3869 | 4602 | inform (input_location, "try using %qE instead", TREE_OPERAND (expr, 0)); |
b6ab6892 GB |
4603 | return NULL_TREE; |
4604 | } | |
75650646 | 4605 | |
b6ab6892 GB |
4606 | expr = convert_nontype_argument_function (TREE_TYPE (type), expr); |
4607 | if (!expr || expr == error_mark_node) | |
4608 | return expr; | |
75650646 | 4609 | |
7866705a | 4610 | expr = build_nop (type, build_address (expr)); |
b6ab6892 GB |
4611 | } |
4612 | /* [temp.arg.nontype]/5, bullet 6 | |
75650646 | 4613 | |
b6ab6892 GB |
4614 | For a non-type template-parameter of type pointer to member function, |
4615 | no conversions apply. If the template-argument represents a set of | |
4616 | overloaded member functions, the matching member function is selected | |
4617 | from the set (_over.over_). */ | |
4618 | else if (TYPE_PTRMEMFUNC_P (type)) | |
4619 | { | |
4620 | expr = instantiate_type (type, expr, tf_none); | |
4621 | if (expr == error_mark_node) | |
4622 | return error_mark_node; | |
75650646 | 4623 | |
b6ab6892 GB |
4624 | /* There is no way to disable standard conversions in |
4625 | resolve_address_of_overloaded_function (called by | |
4626 | instantiate_type). It is possible that the call succeeded by | |
4627 | converting &B::I to &D::I (where B is a base of D), so we need | |
4628 | to reject this conversion here. | |
75650646 | 4629 | |
b6ab6892 GB |
4630 | Actually, even if there was a way to disable standard conversions, |
4631 | it would still be better to reject them here so that we can | |
4632 | provide a superior diagnostic. */ | |
4633 | if (!same_type_p (TREE_TYPE (expr), type)) | |
4634 | { | |
4635 | /* Make sure we are just one standard conversion off. */ | |
4636 | gcc_assert (can_convert (type, TREE_TYPE (expr))); | |
4637 | error ("%qE is not a valid template argument for type %qT " | |
4638 | "because it is of type %qT", expr, type, | |
4639 | TREE_TYPE (expr)); | |
1f5b3869 | 4640 | inform (input_location, "standard conversions are not allowed in this context"); |
b6ab6892 GB |
4641 | return NULL_TREE; |
4642 | } | |
4643 | } | |
4644 | /* [temp.arg.nontype]/5, bullet 7 | |
59e7a901 | 4645 | |
b6ab6892 GB |
4646 | For a non-type template-parameter of type pointer to data member, |
4647 | qualification conversions (_conv.qual_) are applied. */ | |
4648 | else if (TYPE_PTRMEM_P (type)) | |
4649 | { | |
4650 | expr = perform_qualification_conversions (type, expr); | |
4651 | if (expr == error_mark_node) | |
75650646 | 4652 | return expr; |
75650646 | 4653 | } |
b6ab6892 GB |
4654 | /* A template non-type parameter must be one of the above. */ |
4655 | else | |
4656 | gcc_unreachable (); | |
75650646 | 4657 | |
b6ab6892 GB |
4658 | /* Sanity check: did we actually convert the argument to the |
4659 | right type? */ | |
4660 | gcc_assert (same_type_p (type, TREE_TYPE (expr))); | |
4661 | return expr; | |
75650646 MM |
4662 | } |
4663 | ||
a0a5f30f DG |
4664 | /* Subroutine of coerce_template_template_parms, which returns 1 if |
4665 | PARM_PARM and ARG_PARM match using the rule for the template | |
4666 | parameters of template template parameters. Both PARM and ARG are | |
4667 | template parameters; the rest of the arguments are the same as for | |
4668 | coerce_template_template_parms. | |
4669 | */ | |
4670 | static int | |
4671 | coerce_template_template_parm (tree parm, | |
4672 | tree arg, | |
4673 | tsubst_flags_t complain, | |
4674 | tree in_decl, | |
4675 | tree outer_args) | |
4676 | { | |
4677 | if (arg == NULL_TREE || arg == error_mark_node | |
4678 | || parm == NULL_TREE || parm == error_mark_node) | |
4679 | return 0; | |
4680 | ||
4681 | if (TREE_CODE (arg) != TREE_CODE (parm)) | |
4682 | return 0; | |
4683 | ||
4684 | switch (TREE_CODE (parm)) | |
4685 | { | |
4686 | case TEMPLATE_DECL: | |
4687 | /* We encounter instantiations of templates like | |
4688 | template <template <template <class> class> class TT> | |
4689 | class C; */ | |
4690 | { | |
4691 | tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm); | |
4692 | tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg); | |
4693 | ||
4694 | if (!coerce_template_template_parms | |
4695 | (parmparm, argparm, complain, in_decl, outer_args)) | |
4696 | return 0; | |
4697 | } | |
4698 | /* Fall through. */ | |
4699 | ||
4700 | case TYPE_DECL: | |
4701 | if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg)) | |
4702 | && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))) | |
4703 | /* Argument is a parameter pack but parameter is not. */ | |
4704 | return 0; | |
4705 | break; | |
4706 | ||
4707 | case PARM_DECL: | |
4708 | /* The tsubst call is used to handle cases such as | |
4709 | ||
4710 | template <int> class C {}; | |
4711 | template <class T, template <T> class TT> class D {}; | |
4712 | D<int, C> d; | |
4713 | ||
4714 | i.e. the parameter list of TT depends on earlier parameters. */ | |
8320114a | 4715 | if (!uses_template_parms (TREE_TYPE (arg)) |
a0a5f30f DG |
4716 | && !same_type_p |
4717 | (tsubst (TREE_TYPE (parm), outer_args, complain, in_decl), | |
4718 | TREE_TYPE (arg))) | |
4719 | return 0; | |
4720 | ||
4721 | if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg)) | |
4722 | && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))) | |
4723 | /* Argument is a parameter pack but parameter is not. */ | |
4724 | return 0; | |
4725 | ||
4726 | break; | |
4727 | ||
4728 | default: | |
4729 | gcc_unreachable (); | |
4730 | } | |
4731 | ||
4732 | return 1; | |
4733 | } | |
4734 | ||
b6ab6892 | 4735 | |
c8094d83 MS |
4736 | /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for |
4737 | template template parameters. Both PARM_PARMS and ARG_PARMS are | |
4738 | vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL | |
744fac59 | 4739 | or PARM_DECL. |
c8094d83 | 4740 | |
744fac59 | 4741 | Consider the example: |
e7e93965 MM |
4742 | template <class T> class A; |
4743 | template<template <class U> class TT> class B; | |
744fac59 | 4744 | |
e7e93965 MM |
4745 | For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are |
4746 | the parameters to A, and OUTER_ARGS contains A. */ | |
744fac59 KL |
4747 | |
4748 | static int | |
c8094d83 | 4749 | coerce_template_template_parms (tree parm_parms, |
0cbd7506 MS |
4750 | tree arg_parms, |
4751 | tsubst_flags_t complain, | |
3a978d72 | 4752 | tree in_decl, |
0cbd7506 | 4753 | tree outer_args) |
744fac59 KL |
4754 | { |
4755 | int nparms, nargs, i; | |
4756 | tree parm, arg; | |
a0a5f30f | 4757 | int variadic_p = 0; |
744fac59 | 4758 | |
50bc768d NS |
4759 | gcc_assert (TREE_CODE (parm_parms) == TREE_VEC); |
4760 | gcc_assert (TREE_CODE (arg_parms) == TREE_VEC); | |
744fac59 KL |
4761 | |
4762 | nparms = TREE_VEC_LENGTH (parm_parms); | |
4763 | nargs = TREE_VEC_LENGTH (arg_parms); | |
4764 | ||
a0a5f30f DG |
4765 | /* Determine whether we have a parameter pack at the end of the |
4766 | template template parameter's template parameter list. */ | |
4767 | if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node) | |
4768 | { | |
4769 | parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1)); | |
4770 | ||
8c8d4a46 L |
4771 | if (parm == error_mark_node) |
4772 | return 0; | |
4773 | ||
a0a5f30f DG |
4774 | switch (TREE_CODE (parm)) |
4775 | { | |
4776 | case TEMPLATE_DECL: | |
4777 | case TYPE_DECL: | |
4778 | if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))) | |
4779 | variadic_p = 1; | |
4780 | break; | |
4781 | ||
4782 | case PARM_DECL: | |
4783 | if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))) | |
4784 | variadic_p = 1; | |
4785 | break; | |
4786 | ||
4787 | default: | |
4788 | gcc_unreachable (); | |
4789 | } | |
4790 | } | |
4791 | ||
4792 | if (nargs != nparms | |
4793 | && !(variadic_p && nargs >= nparms - 1)) | |
744fac59 KL |
4794 | return 0; |
4795 | ||
a0a5f30f DG |
4796 | /* Check all of the template parameters except the parameter pack at |
4797 | the end (if any). */ | |
4798 | for (i = 0; i < nparms - variadic_p; ++i) | |
744fac59 | 4799 | { |
2d8ba2c7 LM |
4800 | if (TREE_VEC_ELT (parm_parms, i) == error_mark_node |
4801 | || TREE_VEC_ELT (arg_parms, i) == error_mark_node) | |
42b304f1 LM |
4802 | continue; |
4803 | ||
744fac59 KL |
4804 | parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i)); |
4805 | arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i)); | |
4806 | ||
a0a5f30f DG |
4807 | if (!coerce_template_template_parm (parm, arg, complain, in_decl, |
4808 | outer_args)) | |
744fac59 KL |
4809 | return 0; |
4810 | ||
a0a5f30f | 4811 | } |
00bdb87f | 4812 | |
a0a5f30f DG |
4813 | if (variadic_p) |
4814 | { | |
4815 | /* Check each of the template parameters in the template | |
4816 | argument against the template parameter pack at the end of | |
4817 | the template template parameter. */ | |
4818 | if (TREE_VEC_ELT (parm_parms, i) == error_mark_node) | |
4819 | return 0; | |
5d80a306 | 4820 | |
a0a5f30f | 4821 | parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i)); |
c8094d83 | 4822 | |
a0a5f30f DG |
4823 | for (; i < nargs; ++i) |
4824 | { | |
4825 | if (TREE_VEC_ELT (arg_parms, i) == error_mark_node) | |
4826 | continue; | |
4827 | ||
4828 | arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i)); | |
4829 | ||
4830 | if (!coerce_template_template_parm (parm, arg, complain, in_decl, | |
4831 | outer_args)) | |
4832 | return 0; | |
4833 | } | |
744fac59 | 4834 | } |
a0a5f30f | 4835 | |
744fac59 KL |
4836 | return 1; |
4837 | } | |
4838 | ||
4cf36211 DG |
4839 | /* Verifies that the deduced template arguments (in TARGS) for the |
4840 | template template parameters (in TPARMS) represent valid bindings, | |
4841 | by comparing the template parameter list of each template argument | |
4842 | to the template parameter list of its corresponding template | |
4843 | template parameter, in accordance with DR150. This | |
4844 | routine can only be called after all template arguments have been | |
4845 | deduced. It will return TRUE if all of the template template | |
4846 | parameter bindings are okay, FALSE otherwise. */ | |
4847 | bool | |
4848 | template_template_parm_bindings_ok_p (tree tparms, tree targs) | |
4849 | { | |
4850 | int i, ntparms = TREE_VEC_LENGTH (tparms); | |
ee34d21a JM |
4851 | bool ret = true; |
4852 | ||
4853 | /* We're dealing with template parms in this process. */ | |
4854 | ++processing_template_decl; | |
4cf36211 DG |
4855 | |
4856 | targs = INNERMOST_TEMPLATE_ARGS (targs); | |
4857 | ||
4858 | for (i = 0; i < ntparms; ++i) | |
4859 | { | |
4860 | tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i)); | |
4861 | tree targ = TREE_VEC_ELT (targs, i); | |
4862 | ||
4863 | if (TREE_CODE (tparm) == TEMPLATE_DECL && targ) | |
4864 | { | |
4865 | tree packed_args = NULL_TREE; | |
4866 | int idx, len = 1; | |
4867 | ||
4868 | if (ARGUMENT_PACK_P (targ)) | |
4869 | { | |
4870 | /* Look inside the argument pack. */ | |
4871 | packed_args = ARGUMENT_PACK_ARGS (targ); | |
4872 | len = TREE_VEC_LENGTH (packed_args); | |
4873 | } | |
4874 | ||
4875 | for (idx = 0; idx < len; ++idx) | |
4876 | { | |
4877 | tree targ_parms = NULL_TREE; | |
4878 | ||
4879 | if (packed_args) | |
4880 | /* Extract the next argument from the argument | |
4881 | pack. */ | |
4882 | targ = TREE_VEC_ELT (packed_args, idx); | |
4883 | ||
4884 | if (PACK_EXPANSION_P (targ)) | |
4885 | /* Look at the pattern of the pack expansion. */ | |
4886 | targ = PACK_EXPANSION_PATTERN (targ); | |
4887 | ||
4888 | /* Extract the template parameters from the template | |
4889 | argument. */ | |
4890 | if (TREE_CODE (targ) == TEMPLATE_DECL) | |
4891 | targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ); | |
4892 | else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM) | |
4893 | targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ)); | |
4894 | ||
4895 | /* Verify that we can coerce the template template | |
4896 | parameters from the template argument to the template | |
4897 | parameter. This requires an exact match. */ | |
4898 | if (targ_parms | |
4899 | && !coerce_template_template_parms | |
4900 | (DECL_INNERMOST_TEMPLATE_PARMS (tparm), | |
4901 | targ_parms, | |
4902 | tf_none, | |
4903 | tparm, | |
4904 | targs)) | |
ee34d21a JM |
4905 | { |
4906 | ret = false; | |
4907 | goto out; | |
4908 | } | |
4cf36211 DG |
4909 | } |
4910 | } | |
4911 | } | |
4912 | ||
ee34d21a JM |
4913 | out: |
4914 | ||
4915 | --processing_template_decl; | |
4916 | return ret; | |
4cf36211 DG |
4917 | } |
4918 | ||
8b5b8b7c MM |
4919 | /* Convert the indicated template ARG as necessary to match the |
4920 | indicated template PARM. Returns the converted ARG, or | |
c2ea3a40 NS |
4921 | error_mark_node if the conversion was unsuccessful. Error and |
4922 | warning messages are issued under control of COMPLAIN. This | |
4923 | conversion is for the Ith parameter in the parameter list. ARGS is | |
4924 | the full set of template arguments deduced so far. */ | |
8b5b8b7c MM |
4925 | |
4926 | static tree | |
c8094d83 | 4927 | convert_template_argument (tree parm, |
0cbd7506 MS |
4928 | tree arg, |
4929 | tree args, | |
4930 | tsubst_flags_t complain, | |
4931 | int i, | |
4932 | tree in_decl) | |
8b5b8b7c | 4933 | { |
88b82314 | 4934 | tree orig_arg; |
8b5b8b7c | 4935 | tree val; |
8b5b8b7c | 4936 | int is_type, requires_type, is_tmpl_type, requires_tmpl_type; |
c8094d83 | 4937 | |
c8094d83 | 4938 | if (TREE_CODE (arg) == TREE_LIST |
a5ac359a | 4939 | && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF) |
c8094d83 | 4940 | { |
8b5b8b7c MM |
4941 | /* The template argument was the name of some |
4942 | member function. That's usually | |
0e339752 | 4943 | invalid, but static members are OK. In any |
8b5b8b7c MM |
4944 | case, grab the underlying fields/functions |
4945 | and issue an error later if required. */ | |
88b82314 | 4946 | orig_arg = TREE_VALUE (arg); |
8b5b8b7c MM |
4947 | TREE_TYPE (arg) = unknown_type_node; |
4948 | } | |
4949 | ||
88b82314 DG |
4950 | orig_arg = arg; |
4951 | ||
8b5b8b7c MM |
4952 | requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL; |
4953 | requires_type = (TREE_CODE (parm) == TYPE_DECL | |
4954 | || requires_tmpl_type); | |
4955 | ||
d732e98f | 4956 | /* When determining whether an argument pack expansion is a template, |
5d80a306 | 4957 | look at the pattern. */ |
88b82314 DG |
4958 | if (TREE_CODE (arg) == TYPE_PACK_EXPANSION) |
4959 | arg = PACK_EXPANSION_PATTERN (arg); | |
5d80a306 DG |
4960 | |
4961 | is_tmpl_type = | |
88b82314 DG |
4962 | ((TREE_CODE (arg) == TEMPLATE_DECL |
4963 | && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL) | |
4964 | || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM | |
4965 | || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE); | |
c8094d83 | 4966 | |
b8c6534b KL |
4967 | if (is_tmpl_type |
4968 | && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM | |
4969 | || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)) | |
8b5b8b7c | 4970 | arg = TYPE_STUB_DECL (arg); |
8b5b8b7c | 4971 | |
2f939d94 | 4972 | is_type = TYPE_P (arg) || is_tmpl_type; |
8b5b8b7c MM |
4973 | |
4974 | if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF | |
4975 | && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM) | |
4976 | { | |
cbe5f3b3 | 4977 | permerror (input_location, "to refer to a type member of a template parameter, " |
37ec60ed | 4978 | "use %<typename %E%>", orig_arg); |
c8094d83 | 4979 | |
88b82314 DG |
4980 | orig_arg = make_typename_type (TREE_OPERAND (arg, 0), |
4981 | TREE_OPERAND (arg, 1), | |
4982 | typename_type, | |
4983 | complain & tf_error); | |
4984 | arg = orig_arg; | |
8b5b8b7c MM |
4985 | is_type = 1; |
4986 | } | |
4987 | if (is_type != requires_type) | |
4988 | { | |
4989 | if (in_decl) | |
4990 | { | |
c2ea3a40 | 4991 | if (complain & tf_error) |
8b5b8b7c | 4992 | { |
0f51ccfc | 4993 | error ("type/value mismatch at argument %d in template " |
0cbd7506 MS |
4994 | "parameter list for %qD", |
4995 | i + 1, in_decl); | |
8b5b8b7c | 4996 | if (is_type) |
0f51ccfc | 4997 | error (" expected a constant of type %qT, got %qT", |
0cbd7506 | 4998 | TREE_TYPE (parm), |
1ad8aeeb | 4999 | (DECL_P (arg) ? DECL_NAME (arg) : orig_arg)); |
d12a7283 | 5000 | else if (requires_tmpl_type) |
88b82314 | 5001 | error (" expected a class template, got %qE", orig_arg); |
8b5b8b7c | 5002 | else |
88b82314 | 5003 | error (" expected a type, got %qE", orig_arg); |
8b5b8b7c MM |
5004 | } |
5005 | } | |
5006 | return error_mark_node; | |
5007 | } | |
5008 | if (is_tmpl_type ^ requires_tmpl_type) | |
5009 | { | |
c2ea3a40 | 5010 | if (in_decl && (complain & tf_error)) |
8b5b8b7c | 5011 | { |
0f51ccfc | 5012 | error ("type/value mismatch at argument %d in template " |
0cbd7506 MS |
5013 | "parameter list for %qD", |
5014 | i + 1, in_decl); | |
8b5b8b7c | 5015 | if (is_tmpl_type) |
0f51ccfc | 5016 | error (" expected a type, got %qT", DECL_NAME (arg)); |
8b5b8b7c | 5017 | else |
88b82314 | 5018 | error (" expected a class template, got %qT", orig_arg); |
8b5b8b7c MM |
5019 | } |
5020 | return error_mark_node; | |
5021 | } | |
c8094d83 | 5022 | |
8b5b8b7c MM |
5023 | if (is_type) |
5024 | { | |
5025 | if (requires_tmpl_type) | |
5026 | { | |
b8c6534b KL |
5027 | if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE) |
5028 | /* The number of argument required is not known yet. | |
5029 | Just accept it for now. */ | |
5030 | val = TREE_TYPE (arg); | |
8b5b8b7c MM |
5031 | else |
5032 | { | |
b8c6534b | 5033 | tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm); |
5d80a306 DG |
5034 | tree argparm; |
5035 | ||
88b82314 | 5036 | argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg); |
b8c6534b KL |
5037 | |
5038 | if (coerce_template_template_parms (parmparm, argparm, | |
5039 | complain, in_decl, | |
6150b602 | 5040 | args)) |
8b5b8b7c | 5041 | { |
88b82314 | 5042 | val = orig_arg; |
c8094d83 MS |
5043 | |
5044 | /* TEMPLATE_TEMPLATE_PARM node is preferred over | |
b8c6534b | 5045 | TEMPLATE_DECL. */ |
5d80a306 DG |
5046 | if (val != error_mark_node) |
5047 | { | |
5048 | if (DECL_TEMPLATE_TEMPLATE_PARM_P (val)) | |
5049 | val = TREE_TYPE (val); | |
5050 | else if (TREE_CODE (val) == TYPE_PACK_EXPANSION | |
88b82314 | 5051 | && DECL_TEMPLATE_TEMPLATE_PARM_P (arg)) |
5d80a306 | 5052 | { |
88b82314 | 5053 | val = TREE_TYPE (arg); |
5d80a306 DG |
5054 | val = make_pack_expansion (val); |
5055 | } | |
5056 | } | |
8b5b8b7c | 5057 | } |
b8c6534b KL |
5058 | else |
5059 | { | |
c2ea3a40 | 5060 | if (in_decl && (complain & tf_error)) |
b8c6534b | 5061 | { |
0f51ccfc | 5062 | error ("type/value mismatch at argument %d in " |
0cbd7506 MS |
5063 | "template parameter list for %qD", |
5064 | i + 1, in_decl); | |
0f51ccfc | 5065 | error (" expected a template of type %qD, got %qD", |
88b82314 | 5066 | parm, orig_arg); |
b8c6534b | 5067 | } |
c8094d83 | 5068 | |
b8c6534b KL |
5069 | val = error_mark_node; |
5070 | } | |
8b5b8b7c MM |
5071 | } |
5072 | } | |
5073 | else | |
88b82314 | 5074 | val = orig_arg; |
685e39c2 MM |
5075 | /* We only form one instance of each template specialization. |
5076 | Therefore, if we use a non-canonical variant (i.e., a | |
3db45ab5 | 5077 | typedef), any future messages referring to the type will use |
685e39c2 MM |
5078 | the typedef, which is confusing if those future uses do not |
5079 | themselves also use the typedef. */ | |
5080 | if (TYPE_P (val)) | |
cd41d410 | 5081 | val = strip_typedefs (val); |
8b5b8b7c MM |
5082 | } |
5083 | else | |
5084 | { | |
4393e105 | 5085 | tree t = tsubst (TREE_TYPE (parm), args, complain, in_decl); |
8b5b8b7c | 5086 | |
11b810f1 | 5087 | if (invalid_nontype_parm_type_p (t, complain)) |
0cbd7506 | 5088 | return error_mark_node; |
c8094d83 | 5089 | |
ffaf1e05 JM |
5090 | if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg)) |
5091 | { | |
5092 | if (same_type_p (t, TREE_TYPE (orig_arg))) | |
5093 | val = orig_arg; | |
5094 | else | |
5095 | { | |
5096 | /* Not sure if this is reachable, but it doesn't hurt | |
5097 | to be robust. */ | |
5098 | error ("type mismatch in nontype parameter pack"); | |
5099 | val = error_mark_node; | |
5100 | } | |
5101 | } | |
5102 | else if (!uses_template_parms (orig_arg) && !uses_template_parms (t)) | |
8b5b8b7c MM |
5103 | /* We used to call digest_init here. However, digest_init |
5104 | will report errors, which we don't want when complain | |
5105 | is zero. More importantly, digest_init will try too | |
5106 | hard to convert things: for example, `0' should not be | |
5107 | converted to pointer type at this point according to | |
5108 | the standard. Accepting this is not merely an | |
5109 | extension, since deciding whether or not these | |
5110 | conversions can occur is part of determining which | |
dc957d14 | 5111 | function template to call, or whether a given explicit |
0e339752 | 5112 | argument specification is valid. */ |
88b82314 | 5113 | val = convert_nontype_argument (t, orig_arg); |
8b5b8b7c | 5114 | else |
88b82314 | 5115 | val = orig_arg; |
8b5b8b7c MM |
5116 | |
5117 | if (val == NULL_TREE) | |
5118 | val = error_mark_node; | |
c2ea3a40 | 5119 | else if (val == error_mark_node && (complain & tf_error)) |
88b82314 | 5120 | error ("could not convert template argument %qE to %qT", orig_arg, t); |
8b5b8b7c MM |
5121 | } |
5122 | ||
5123 | return val; | |
5124 | } | |
5125 | ||
b1d7b1c0 DG |
5126 | /* Coerces the remaining template arguments in INNER_ARGS (from |
5127 | ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS. | |
5128 | Returns the coerced argument pack. PARM_IDX is the position of this | |
5129 | parameter in the template parameter list. ARGS is the original | |
5130 | template argument list. */ | |
5131 | static tree | |
5132 | coerce_template_parameter_pack (tree parms, | |
5133 | int parm_idx, | |
5134 | tree args, | |
5135 | tree inner_args, | |
5136 | int arg_idx, | |
5137 | tree new_args, | |
5138 | int* lost, | |
5139 | tree in_decl, | |
5140 | tsubst_flags_t complain) | |
5141 | { | |
5142 | tree parm = TREE_VEC_ELT (parms, parm_idx); | |
5143 | int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0; | |
5144 | tree packed_args; | |
5145 | tree argument_pack; | |
5146 | tree packed_types = NULL_TREE; | |
5147 | ||
5148 | if (arg_idx > nargs) | |
5149 | arg_idx = nargs; | |
5150 | ||
5151 | packed_args = make_tree_vec (nargs - arg_idx); | |
5152 | ||
5153 | if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL | |
5154 | && uses_parameter_packs (TREE_TYPE (TREE_VALUE (parm)))) | |
5155 | { | |
5156 | /* When the template parameter is a non-type template | |
5157 | parameter pack whose type uses parameter packs, we need | |
5158 | to look at each of the template arguments | |
5159 | separately. Build a vector of the types for these | |
5160 | non-type template parameters in PACKED_TYPES. */ | |
5161 | tree expansion | |
5162 | = make_pack_expansion (TREE_TYPE (TREE_VALUE (parm))); | |
5163 | packed_types = tsubst_pack_expansion (expansion, args, | |
5164 | complain, in_decl); | |
5165 | ||
5166 | if (packed_types == error_mark_node) | |
5167 | return error_mark_node; | |
5168 | ||
5169 | /* Check that we have the right number of arguments. */ | |
5170 | if (arg_idx < nargs | |
5171 | && !PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)) | |
5172 | && nargs - arg_idx != TREE_VEC_LENGTH (packed_types)) | |
5173 | { | |
5174 | int needed_parms | |
5175 | = TREE_VEC_LENGTH (parms) - 1 + TREE_VEC_LENGTH (packed_types); | |
5176 | error ("wrong number of template arguments (%d, should be %d)", | |
5177 | nargs, needed_parms); | |
5178 | return error_mark_node; | |
5179 | } | |
5180 | ||
5181 | /* If we aren't able to check the actual arguments now | |
5182 | (because they haven't been expanded yet), we can at least | |
5183 | verify that all of the types used for the non-type | |
5184 | template parameter pack are, in fact, valid for non-type | |
5185 | template parameters. */ | |
5186 | if (arg_idx < nargs | |
5187 | && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx))) | |
5188 | { | |
5189 | int j, len = TREE_VEC_LENGTH (packed_types); | |
5190 | for (j = 0; j < len; ++j) | |
5191 | { | |
5192 | tree t = TREE_VEC_ELT (packed_types, j); | |
5193 | if (invalid_nontype_parm_type_p (t, complain)) | |
5194 | return error_mark_node; | |
5195 | } | |
5196 | } | |
5197 | } | |
5198 | ||
5199 | /* Convert the remaining arguments, which will be a part of the | |
5200 | parameter pack "parm". */ | |
5201 | for (; arg_idx < nargs; ++arg_idx) | |
5202 | { | |
5203 | tree arg = TREE_VEC_ELT (inner_args, arg_idx); | |
5204 | tree actual_parm = TREE_VALUE (parm); | |
5205 | ||
5206 | if (packed_types && !PACK_EXPANSION_P (arg)) | |
5207 | { | |
5208 | /* When we have a vector of types (corresponding to the | |
5209 | non-type template parameter pack that uses parameter | |
5210 | packs in its type, as mention above), and the | |
5211 | argument is not an expansion (which expands to a | |
5212 | currently unknown number of arguments), clone the | |
5213 | parm and give it the next type in PACKED_TYPES. */ | |
5214 | actual_parm = copy_node (actual_parm); | |
5215 | TREE_TYPE (actual_parm) = | |
5216 | TREE_VEC_ELT (packed_types, arg_idx - parm_idx); | |
5217 | } | |
5218 | ||
c343d5a7 PC |
5219 | if (arg != error_mark_node) |
5220 | arg = convert_template_argument (actual_parm, | |
5221 | arg, new_args, complain, parm_idx, | |
5222 | in_decl); | |
b1d7b1c0 DG |
5223 | if (arg == error_mark_node) |
5224 | (*lost)++; | |
5225 | TREE_VEC_ELT (packed_args, arg_idx - parm_idx) = arg; | |
5226 | } | |
5227 | ||
5228 | if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL | |
5229 | || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL) | |
5230 | argument_pack = make_node (TYPE_ARGUMENT_PACK); | |
5231 | else | |
5232 | { | |
5233 | argument_pack = make_node (NONTYPE_ARGUMENT_PACK); | |
88b82314 | 5234 | TREE_TYPE (argument_pack) |
cdcae745 | 5235 | = tsubst (TREE_TYPE (TREE_VALUE (parm)), new_args, complain, in_decl); |
b1d7b1c0 DG |
5236 | TREE_CONSTANT (argument_pack) = 1; |
5237 | } | |
5238 | ||
5239 | SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args); | |
5240 | return argument_pack; | |
5241 | } | |
5242 | ||
8b5b8b7c MM |
5243 | /* Convert all template arguments to their appropriate types, and |
5244 | return a vector containing the innermost resulting template | |
c2ea3a40 | 5245 | arguments. If any error occurs, return error_mark_node. Error and |
3e4a3562 | 5246 | warning messages are issued under control of COMPLAIN. |
75650646 | 5247 | |
e7e93965 | 5248 | If REQUIRE_ALL_ARGS is false, argument deduction will be performed |
84dc00e8 | 5249 | for arguments not specified in ARGS. Otherwise, if |
e7e93965 MM |
5250 | USE_DEFAULT_ARGS is true, default arguments will be used to fill in |
5251 | unspecified arguments. If REQUIRE_ALL_ARGS is true, but | |
5252 | USE_DEFAULT_ARGS is false, then all arguments must be specified in | |
5253 | ARGS. */ | |
c8094d83 | 5254 | |
8d08fdba | 5255 | static tree |
c8094d83 | 5256 | coerce_template_parms (tree parms, |
0cbd7506 MS |
5257 | tree args, |
5258 | tree in_decl, | |
3a978d72 | 5259 | tsubst_flags_t complain, |
e7e93965 MM |
5260 | bool require_all_args, |
5261 | bool use_default_args) | |
8d08fdba | 5262 | { |
b1d7b1c0 | 5263 | int nparms, nargs, parm_idx, arg_idx, lost = 0; |
e4a84209 | 5264 | tree inner_args; |
8b5b8b7c MM |
5265 | tree new_args; |
5266 | tree new_inner_args; | |
a2c5ed87 | 5267 | bool saved_skip_evaluation; |
a292b002 | 5268 | |
5d80a306 DG |
5269 | /* When used as a boolean value, indicates whether this is a |
5270 | variadic template parameter list. Since it's an int, we can also | |
5271 | subtract it from nparms to get the number of non-variadic | |
5272 | parameters. */ | |
b1d7b1c0 | 5273 | int variadic_p = 0; |
5d80a306 | 5274 | |
a292b002 MS |
5275 | nparms = TREE_VEC_LENGTH (parms); |
5276 | ||
b1d7b1c0 DG |
5277 | /* Determine if there are any parameter packs. */ |
5278 | for (parm_idx = 0; parm_idx < nparms; ++parm_idx) | |
5279 | { | |
5280 | tree tparm = TREE_VALUE (TREE_VEC_ELT (parms, parm_idx)); | |
5281 | if (template_parameter_pack_p (tparm)) | |
ffaf1e05 | 5282 | ++variadic_p; |
b1d7b1c0 DG |
5283 | } |
5284 | ||
ffaf1e05 JM |
5285 | inner_args = INNERMOST_TEMPLATE_ARGS (args); |
5286 | /* If there are 0 or 1 parameter packs, we need to expand any argument | |
5287 | packs so that we can deduce a parameter pack from some non-packed args | |
5288 | followed by an argument pack, as in variadic85.C. If there are more | |
5289 | than that, we need to leave argument packs intact so the arguments are | |
5290 | assigned to the right parameter packs. This should only happen when | |
5291 | dealing with a nested class inside a partial specialization of a class | |
5292 | template, as in variadic92.C. */ | |
5293 | if (variadic_p <= 1) | |
5294 | inner_args = expand_template_argument_pack (inner_args); | |
5295 | ||
5296 | nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0; | |
5297 | if ((nargs > nparms && !variadic_p) | |
5d80a306 | 5298 | || (nargs < nparms - variadic_p |
e7e93965 MM |
5299 | && require_all_args |
5300 | && (!use_default_args | |
42b304f1 LM |
5301 | || (TREE_VEC_ELT (parms, nargs) != error_mark_node |
5302 | && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs)))))) | |
8d08fdba | 5303 | { |
c8094d83 | 5304 | if (complain & tf_error) |
75650646 | 5305 | { |
5d80a306 DG |
5306 | const char *or_more = ""; |
5307 | if (variadic_p) | |
5308 | { | |
5309 | or_more = " or more"; | |
5310 | --nparms; | |
5311 | } | |
5312 | ||
5313 | error ("wrong number of template arguments (%d, should be %d%s)", | |
5314 | nargs, nparms, or_more); | |
c8094d83 | 5315 | |
75650646 | 5316 | if (in_decl) |
dee15844 | 5317 | error ("provided for %q+D", in_decl); |
75650646 MM |
5318 | } |
5319 | ||
8d08fdba MS |
5320 | return error_mark_node; |
5321 | } | |
5322 | ||
a2c5ed87 MM |
5323 | /* We need to evaluate the template arguments, even though this |
5324 | template-id may be nested within a "sizeof". */ | |
5325 | saved_skip_evaluation = skip_evaluation; | |
5326 | skip_evaluation = false; | |
f31c0a32 | 5327 | new_inner_args = make_tree_vec (nparms); |
8b5b8b7c | 5328 | new_args = add_outermost_template_args (args, new_inner_args); |
b1d7b1c0 | 5329 | for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++) |
8d08fdba | 5330 | { |
8b5b8b7c MM |
5331 | tree arg; |
5332 | tree parm; | |
e4a84209 | 5333 | |
8b5b8b7c | 5334 | /* Get the Ith template parameter. */ |
b1d7b1c0 | 5335 | parm = TREE_VEC_ELT (parms, parm_idx); |
42b304f1 LM |
5336 | |
5337 | if (parm == error_mark_node) | |
2d8ba2c7 | 5338 | { |
b1d7b1c0 | 5339 | TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node; |
42b304f1 | 5340 | continue; |
2d8ba2c7 | 5341 | } |
75650646 | 5342 | |
b1d7b1c0 | 5343 | /* Calculate the next argument. */ |
ffaf1e05 JM |
5344 | if (arg_idx < nargs) |
5345 | arg = TREE_VEC_ELT (inner_args, arg_idx); | |
5346 | else | |
5347 | arg = NULL_TREE; | |
5348 | ||
5349 | if (template_parameter_pack_p (TREE_VALUE (parm)) | |
5350 | && !(arg && ARGUMENT_PACK_P (arg))) | |
5d80a306 | 5351 | { |
ffaf1e05 JM |
5352 | /* All remaining arguments will be placed in the |
5353 | template parameter pack PARM. */ | |
5354 | arg = coerce_template_parameter_pack (parms, parm_idx, args, | |
5355 | inner_args, arg_idx, | |
5356 | new_args, &lost, | |
5357 | in_decl, complain); | |
5358 | ||
b1d7b1c0 DG |
5359 | /* Store this argument. */ |
5360 | if (arg == error_mark_node) | |
5361 | lost++; | |
5362 | TREE_VEC_ELT (new_inner_args, parm_idx) = arg; | |
5363 | ||
ffaf1e05 JM |
5364 | /* We are done with all of the arguments. */ |
5365 | arg_idx = nargs; | |
5366 | ||
b1d7b1c0 DG |
5367 | continue; |
5368 | } | |
ffaf1e05 JM |
5369 | else if (arg) |
5370 | { | |
5371 | if (PACK_EXPANSION_P (arg)) | |
5d80a306 | 5372 | { |
4cf36211 DG |
5373 | if (complain & tf_error) |
5374 | { | |
ffaf1e05 JM |
5375 | /* FIXME this restriction was removed by N2555; see |
5376 | bug 35722. */ | |
4cf36211 DG |
5377 | /* If ARG is a pack expansion, but PARM is not a |
5378 | template parameter pack (if it were, we would have | |
5379 | handled it above), we're trying to expand into a | |
5380 | fixed-length argument list. */ | |
5381 | if (TREE_CODE (arg) == EXPR_PACK_EXPANSION) | |
ffaf1e05 | 5382 | sorry ("cannot expand %<%E%> into a fixed-length " |
4cf36211 DG |
5383 | "argument list", arg); |
5384 | else | |
ffaf1e05 | 5385 | sorry ("cannot expand %<%T%> into a fixed-length " |
4cf36211 DG |
5386 | "argument list", arg); |
5387 | } | |
c343d5a7 | 5388 | return error_mark_node; |
5d80a306 DG |
5389 | } |
5390 | } | |
e7e93965 | 5391 | else if (require_all_args) |
b1d7b1c0 DG |
5392 | /* There must be a default arg in this case. */ |
5393 | arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args, | |
5394 | complain, in_decl); | |
ffd49b19 NS |
5395 | else |
5396 | break; | |
c8094d83 | 5397 | |
ffd49b19 | 5398 | if (arg == error_mark_node) |
e34b0922 KL |
5399 | { |
5400 | if (complain & tf_error) | |
b1d7b1c0 | 5401 | error ("template argument %d is invalid", arg_idx + 1); |
e34b0922 | 5402 | } |
b1d7b1c0 DG |
5403 | else if (!arg) |
5404 | /* This only occurs if there was an error in the template | |
5405 | parameter list itself (which we would already have | |
5406 | reported) that we are trying to recover from, e.g., a class | |
5407 | template with a parameter list such as | |
5408 | template<typename..., typename>. */ | |
5409 | return error_mark_node; | |
c8094d83 MS |
5410 | else |
5411 | arg = convert_template_argument (TREE_VALUE (parm), | |
b1d7b1c0 DG |
5412 | arg, new_args, complain, |
5413 | parm_idx, in_decl); | |
c8094d83 | 5414 | |
8b5b8b7c | 5415 | if (arg == error_mark_node) |
8d08fdba | 5416 | lost++; |
b1d7b1c0 | 5417 | TREE_VEC_ELT (new_inner_args, arg_idx) = arg; |
8d08fdba | 5418 | } |
a2c5ed87 | 5419 | skip_evaluation = saved_skip_evaluation; |
8b5b8b7c | 5420 | |
8d08fdba MS |
5421 | if (lost) |
5422 | return error_mark_node; | |
8b5b8b7c MM |
5423 | |
5424 | return new_inner_args; | |
8d08fdba MS |
5425 | } |
5426 | ||
34016c81 JM |
5427 | /* Returns 1 if template args OT and NT are equivalent. */ |
5428 | ||
d8e178a0 | 5429 | static int |
3a978d72 | 5430 | template_args_equal (tree ot, tree nt) |
34016c81 JM |
5431 | { |
5432 | if (nt == ot) | |
5433 | return 1; | |
74601d7c | 5434 | |
34016c81 JM |
5435 | if (TREE_CODE (nt) == TREE_VEC) |
5436 | /* For member templates */ | |
74601d7c | 5437 | return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt); |
5d80a306 DG |
5438 | else if (PACK_EXPANSION_P (ot)) |
5439 | return PACK_EXPANSION_P (nt) | |
5440 | && template_args_equal (PACK_EXPANSION_PATTERN (ot), | |
5441 | PACK_EXPANSION_PATTERN (nt)); | |
ffaf1e05 JM |
5442 | else if (ARGUMENT_PACK_P (ot)) |
5443 | { | |
5444 | int i, len; | |
5445 | tree opack, npack; | |
5446 | ||
5447 | if (!ARGUMENT_PACK_P (nt)) | |
5448 | return 0; | |
5449 | ||
5450 | opack = ARGUMENT_PACK_ARGS (ot); | |
5451 | npack = ARGUMENT_PACK_ARGS (nt); | |
5452 | len = TREE_VEC_LENGTH (opack); | |
5453 | if (TREE_VEC_LENGTH (npack) != len) | |
5454 | return 0; | |
5455 | for (i = 0; i < len; ++i) | |
5456 | if (!template_args_equal (TREE_VEC_ELT (opack, i), | |
5457 | TREE_VEC_ELT (npack, i))) | |
5458 | return 0; | |
5459 | return 1; | |
5460 | } | |
74601d7c | 5461 | else if (TYPE_P (nt)) |
660845bf | 5462 | return TYPE_P (ot) && same_type_p (ot, nt); |
74601d7c KL |
5463 | else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot)) |
5464 | return 0; | |
34016c81 | 5465 | else |
c8a209ca | 5466 | return cp_tree_equal (ot, nt); |
34016c81 JM |
5467 | } |
5468 | ||
5469 | /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets | |
f84b4be9 JM |
5470 | of template arguments. Returns 0 otherwise. */ |
5471 | ||
6757edfe | 5472 | int |
3a978d72 | 5473 | comp_template_args (tree oldargs, tree newargs) |
5566b478 MS |
5474 | { |
5475 | int i; | |
5476 | ||
386b8a85 JM |
5477 | if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs)) |
5478 | return 0; | |
5479 | ||
5566b478 MS |
5480 | for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i) |
5481 | { | |
5482 | tree nt = TREE_VEC_ELT (newargs, i); | |
5483 | tree ot = TREE_VEC_ELT (oldargs, i); | |
5484 | ||
34016c81 | 5485 | if (! template_args_equal (ot, nt)) |
61a127b3 | 5486 | return 0; |
5566b478 MS |
5487 | } |
5488 | return 1; | |
5489 | } | |
5490 | ||
5566b478 | 5491 | static void |
3a978d72 | 5492 | add_pending_template (tree d) |
5566b478 | 5493 | { |
3ae18eaf JM |
5494 | tree ti = (TYPE_P (d) |
5495 | ? CLASSTYPE_TEMPLATE_INFO (d) | |
5496 | : DECL_TEMPLATE_INFO (d)); | |
e2c3721c | 5497 | struct pending_template *pt; |
3ae18eaf | 5498 | int level; |
e92cc029 | 5499 | |
824b9a4c | 5500 | if (TI_PENDING_TEMPLATE_FLAG (ti)) |
5566b478 MS |
5501 | return; |
5502 | ||
3ae18eaf JM |
5503 | /* We are called both from instantiate_decl, where we've already had a |
5504 | tinst_level pushed, and instantiate_template, where we haven't. | |
5505 | Compensate. */ | |
e2c3721c | 5506 | level = !current_tinst_level || current_tinst_level->decl != d; |
3ae18eaf JM |
5507 | |
5508 | if (level) | |
5509 | push_tinst_level (d); | |
5510 | ||
e2c3721c PB |
5511 | pt = GGC_NEW (struct pending_template); |
5512 | pt->next = NULL; | |
5513 | pt->tinst = current_tinst_level; | |
46ccf50a | 5514 | if (last_pending_template) |
e2c3721c | 5515 | last_pending_template->next = pt; |
46ccf50a JM |
5516 | else |
5517 | pending_templates = pt; | |
5518 | ||
5519 | last_pending_template = pt; | |
5520 | ||
824b9a4c | 5521 | TI_PENDING_TEMPLATE_FLAG (ti) = 1; |
3ae18eaf JM |
5522 | |
5523 | if (level) | |
5524 | pop_tinst_level (); | |
5566b478 MS |
5525 | } |
5526 | ||
386b8a85 | 5527 | |
4ba126e4 MM |
5528 | /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and |
5529 | ARGLIST. Valid choices for FNS are given in the cp-tree.def | |
5530 | documentation for TEMPLATE_ID_EXPR. */ | |
386b8a85 JM |
5531 | |
5532 | tree | |
3a978d72 | 5533 | lookup_template_function (tree fns, tree arglist) |
386b8a85 | 5534 | { |
2c73f9f5 | 5535 | tree type; |
050367a3 | 5536 | |
4ba126e4 MM |
5537 | if (fns == error_mark_node || arglist == error_mark_node) |
5538 | return error_mark_node; | |
5539 | ||
50bc768d | 5540 | gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC); |
4230cec2 NS |
5541 | gcc_assert (fns && (is_overloaded_fn (fns) |
5542 | || TREE_CODE (fns) == IDENTIFIER_NODE)); | |
4ba126e4 | 5543 | |
50ad9642 MM |
5544 | if (BASELINK_P (fns)) |
5545 | { | |
f293ce4b RS |
5546 | BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR, |
5547 | unknown_type_node, | |
5548 | BASELINK_FUNCTIONS (fns), | |
5549 | arglist); | |
50ad9642 MM |
5550 | return fns; |
5551 | } | |
5552 | ||
2c73f9f5 ML |
5553 | type = TREE_TYPE (fns); |
5554 | if (TREE_CODE (fns) == OVERLOAD || !type) | |
5555 | type = unknown_type_node; | |
c8094d83 | 5556 | |
f293ce4b | 5557 | return build2 (TEMPLATE_ID_EXPR, type, fns, arglist); |
386b8a85 JM |
5558 | } |
5559 | ||
a2b60a0e MM |
5560 | /* Within the scope of a template class S<T>, the name S gets bound |
5561 | (in build_self_reference) to a TYPE_DECL for the class, not a | |
5562 | TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type, | |
5563 | or one of its enclosing classes, and that type is a template, | |
5564 | return the associated TEMPLATE_DECL. Otherwise, the original | |
5565 | DECL is returned. */ | |
5566 | ||
a723baf1 | 5567 | tree |
3a978d72 | 5568 | maybe_get_template_decl_from_type_decl (tree decl) |
a2b60a0e MM |
5569 | { |
5570 | return (decl != NULL_TREE | |
c8094d83 | 5571 | && TREE_CODE (decl) == TYPE_DECL |
a2b60a0e | 5572 | && DECL_ARTIFICIAL (decl) |
511b60ff | 5573 | && CLASS_TYPE_P (TREE_TYPE (decl)) |
c8094d83 | 5574 | && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl))) |
a2b60a0e MM |
5575 | ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl; |
5576 | } | |
386b8a85 | 5577 | |
8d08fdba MS |
5578 | /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of |
5579 | parameters, find the desired type. | |
5580 | ||
5581 | D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments. | |
8d08fdba MS |
5582 | |
5583 | IN_DECL, if non-NULL, is the template declaration we are trying to | |
c8094d83 | 5584 | instantiate. |
75650646 | 5585 | |
838dfd8a | 5586 | If ENTERING_SCOPE is nonzero, we are about to enter the scope of |
36a117a5 | 5587 | the class we are looking up. |
c8094d83 | 5588 | |
c2ea3a40 | 5589 | Issue error and warning messages under control of COMPLAIN. |
36a117a5 | 5590 | |
75650646 MM |
5591 | If the template class is really a local class in a template |
5592 | function, then the FUNCTION_CONTEXT is the function in which it is | |
c8094d83 | 5593 | being instantiated. |
b6ab6892 GB |
5594 | |
5595 | ??? Note that this function is currently called *twice* for each | |
5596 | template-id: the first time from the parser, while creating the | |
5597 | incomplete type (finish_template_type), and the second type during the | |
5598 | real instantiation (instantiate_template_class). This is surely something | |
5599 | that we want to avoid. It also causes some problems with argument | |
5600 | coercion (see convert_nontype_argument for more information on this). */ | |
e92cc029 | 5601 | |
8d08fdba | 5602 | tree |
c8094d83 | 5603 | lookup_template_class (tree d1, |
0cbd7506 MS |
5604 | tree arglist, |
5605 | tree in_decl, | |
5606 | tree context, | |
5607 | int entering_scope, | |
5608 | tsubst_flags_t complain) | |
8d08fdba | 5609 | { |
be93747e | 5610 | tree templ = NULL_TREE, parmlist; |
dbfe2124 | 5611 | tree t; |
c8094d83 | 5612 | |
fd295cb2 | 5613 | timevar_push (TV_NAME_LOOKUP); |
c8094d83 | 5614 | |
5566b478 MS |
5615 | if (TREE_CODE (d1) == IDENTIFIER_NODE) |
5616 | { | |
90ea9897 MM |
5617 | tree value = innermost_non_namespace_value (d1); |
5618 | if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value)) | |
be93747e | 5619 | templ = value; |
73b0fce8 KL |
5620 | else |
5621 | { | |
2c73f9f5 ML |
5622 | if (context) |
5623 | push_decl_namespace (context); | |
be93747e KG |
5624 | templ = lookup_name (d1); |
5625 | templ = maybe_get_template_decl_from_type_decl (templ); | |
2c73f9f5 ML |
5626 | if (context) |
5627 | pop_decl_namespace (); | |
73b0fce8 | 5628 | } |
be93747e KG |
5629 | if (templ) |
5630 | context = DECL_CONTEXT (templ); | |
5566b478 | 5631 | } |
9e1e64ec | 5632 | else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1))) |
c91a56d2 | 5633 | { |
802dbc34 JM |
5634 | tree type = TREE_TYPE (d1); |
5635 | ||
5636 | /* If we are declaring a constructor, say A<T>::A<T>, we will get | |
5637 | an implicit typename for the second A. Deal with it. */ | |
5638 | if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type)) | |
5639 | type = TREE_TYPE (type); | |
c8094d83 | 5640 | |
802dbc34 | 5641 | if (CLASSTYPE_TEMPLATE_INFO (type)) |
f3400fe2 | 5642 | { |
be93747e KG |
5643 | templ = CLASSTYPE_TI_TEMPLATE (type); |
5644 | d1 = DECL_NAME (templ); | |
f3400fe2 | 5645 | } |
c91a56d2 | 5646 | } |
c8094d83 | 5647 | else if (TREE_CODE (d1) == ENUMERAL_TYPE |
9e1e64ec | 5648 | || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1))) |
5566b478 | 5649 | { |
be93747e KG |
5650 | templ = TYPE_TI_TEMPLATE (d1); |
5651 | d1 = DECL_NAME (templ); | |
5566b478 | 5652 | } |
93cdc044 | 5653 | else if (TREE_CODE (d1) == TEMPLATE_DECL |
b55276c8 | 5654 | && DECL_TEMPLATE_RESULT (d1) |
17aec3eb | 5655 | && TREE_CODE (DECL_TEMPLATE_RESULT (d1)) == TYPE_DECL) |
93cdc044 | 5656 | { |
be93747e KG |
5657 | templ = d1; |
5658 | d1 = DECL_NAME (templ); | |
5659 | context = DECL_CONTEXT (templ); | |
93cdc044 | 5660 | } |
8d08fdba | 5661 | |
90ea9897 | 5662 | /* Issue an error message if we didn't find a template. */ |
be93747e | 5663 | if (! templ) |
f3400fe2 | 5664 | { |
c2ea3a40 | 5665 | if (complain & tf_error) |
0cbd7506 | 5666 | error ("%qT is not a template", d1); |
fd295cb2 | 5667 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node); |
f3400fe2 | 5668 | } |
2c73f9f5 | 5669 | |
be93747e | 5670 | if (TREE_CODE (templ) != TEMPLATE_DECL |
0cbd7506 | 5671 | /* Make sure it's a user visible template, if it was named by |
42eaed49 | 5672 | the user. */ |
be93747e KG |
5673 | || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ) |
5674 | && !PRIMARY_TEMPLATE_P (templ))) | |
8d08fdba | 5675 | { |
c2ea3a40 | 5676 | if (complain & tf_error) |
0cbd7506 MS |
5677 | { |
5678 | error ("non-template type %qT used as a template", d1); | |
5679 | if (in_decl) | |
dee15844 | 5680 | error ("for template declaration %q+D", in_decl); |
f9c244b8 | 5681 | } |
fd295cb2 | 5682 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node); |
8d08fdba | 5683 | } |
8d08fdba | 5684 | |
42eaed49 | 5685 | complain &= ~tf_user; |
c8094d83 | 5686 | |
be93747e | 5687 | if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ)) |
73b0fce8 KL |
5688 | { |
5689 | /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store | |
0cbd7506 | 5690 | template arguments */ |
73b0fce8 | 5691 | |
1899c3a4 | 5692 | tree parm; |
73b0fce8 | 5693 | tree arglist2; |
ef3b7b17 | 5694 | tree outer; |
73b0fce8 | 5695 | |
be93747e | 5696 | parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ); |
73b0fce8 | 5697 | |
4f96ff63 KL |
5698 | /* Consider an example where a template template parameter declared as |
5699 | ||
5700 | template <class T, class U = std::allocator<T> > class TT | |
5701 | ||
c8094d83 MS |
5702 | The template parameter level of T and U are one level larger than |
5703 | of TT. To proper process the default argument of U, say when an | |
4f96ff63 | 5704 | instantiation `TT<int>' is seen, we need to build the full |
342cea95 KL |
5705 | arguments containing {int} as the innermost level. Outer levels, |
5706 | available when not appearing as default template argument, can be | |
ef3b7b17 | 5707 | obtained from the arguments of the enclosing template. |
4f96ff63 | 5708 | |
342cea95 KL |
5709 | Suppose that TT is later substituted with std::vector. The above |
5710 | instantiation is `TT<int, std::allocator<T> >' with TT at | |
5711 | level 1, and T at level 2, while the template arguments at level 1 | |
5712 | becomes {std::vector} and the inner level 2 is {int}. */ | |
5713 | ||
be93747e | 5714 | outer = DECL_CONTEXT (templ); |
ef3b7b17 JM |
5715 | if (outer) |
5716 | outer = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer))); | |
5717 | else if (current_template_parms) | |
5718 | /* This is an argument of the current template, so we haven't set | |
5719 | DECL_CONTEXT yet. */ | |
5720 | outer = current_template_args (); | |
5721 | ||
5722 | if (outer) | |
5723 | arglist = add_to_template_args (outer, arglist); | |
4f96ff63 | 5724 | |
be93747e | 5725 | arglist2 = coerce_template_parms (parmlist, arglist, templ, |
3db45ab5 | 5726 | complain, |
e7e93965 MM |
5727 | /*require_all_args=*/true, |
5728 | /*use_default_args=*/true); | |
3e4a3562 | 5729 | if (arglist2 == error_mark_node |
544aef8c | 5730 | || (!uses_template_parms (arglist2) |
be93747e | 5731 | && check_instantiated_args (templ, arglist2, complain))) |
0cbd7506 | 5732 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node); |
73b0fce8 | 5733 | |
be93747e | 5734 | parm = bind_template_template_parm (TREE_TYPE (templ), arglist2); |
fd295cb2 | 5735 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, parm); |
73b0fce8 | 5736 | } |
c8094d83 | 5737 | else |
8d08fdba | 5738 | { |
be93747e | 5739 | tree template_type = TREE_TYPE (templ); |
7ac7b28f | 5740 | tree gen_tmpl; |
36a117a5 MM |
5741 | tree type_decl; |
5742 | tree found = NULL_TREE; | |
5743 | int arg_depth; | |
5744 | int parm_depth; | |
dbfe2124 | 5745 | int is_partial_instantiation; |
830bfa74 | 5746 | |
be93747e | 5747 | gen_tmpl = most_general_template (templ); |
7ac7b28f | 5748 | parmlist = DECL_TEMPLATE_PARMS (gen_tmpl); |
36a117a5 MM |
5749 | parm_depth = TMPL_PARMS_DEPTH (parmlist); |
5750 | arg_depth = TMPL_ARGS_DEPTH (arglist); | |
5751 | ||
5752 | if (arg_depth == 1 && parm_depth > 1) | |
5753 | { | |
39c01e4c | 5754 | /* We've been given an incomplete set of template arguments. |
36a117a5 MM |
5755 | For example, given: |
5756 | ||
5757 | template <class T> struct S1 { | |
0cbd7506 | 5758 | template <class U> struct S2 {}; |
36a117a5 | 5759 | template <class U> struct S2<U*> {}; |
0cbd7506 | 5760 | }; |
c8094d83 | 5761 | |
36a117a5 MM |
5762 | we will be called with an ARGLIST of `U*', but the |
5763 | TEMPLATE will be `template <class T> template | |
5764 | <class U> struct S1<T>::S2'. We must fill in the missing | |
5765 | arguments. */ | |
c8094d83 | 5766 | arglist |
be93747e | 5767 | = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (templ)), |
7ac7b28f | 5768 | arglist); |
36a117a5 MM |
5769 | arg_depth = TMPL_ARGS_DEPTH (arglist); |
5770 | } | |
5566b478 | 5771 | |
41f5d4b1 | 5772 | /* Now we should have enough arguments. */ |
50bc768d | 5773 | gcc_assert (parm_depth == arg_depth); |
c8094d83 | 5774 | |
7ac7b28f MM |
5775 | /* From here on, we're only interested in the most general |
5776 | template. */ | |
be93747e | 5777 | templ = gen_tmpl; |
7ac7b28f | 5778 | |
36a117a5 MM |
5779 | /* Calculate the BOUND_ARGS. These will be the args that are |
5780 | actually tsubst'd into the definition to create the | |
5781 | instantiation. */ | |
5782 | if (parm_depth > 1) | |
830bfa74 MM |
5783 | { |
5784 | /* We have multiple levels of arguments to coerce, at once. */ | |
830bfa74 | 5785 | int i; |
e4a84209 | 5786 | int saved_depth = TMPL_ARGS_DEPTH (arglist); |
36a117a5 | 5787 | |
f31c0a32 | 5788 | tree bound_args = make_tree_vec (parm_depth); |
c8094d83 | 5789 | |
e4a84209 | 5790 | for (i = saved_depth, |
be93747e | 5791 | t = DECL_TEMPLATE_PARMS (templ); |
e4a84209 | 5792 | i > 0 && t != NULL_TREE; |
830bfa74 | 5793 | --i, t = TREE_CHAIN (t)) |
e4a84209 | 5794 | { |
ee3071ef | 5795 | tree a = coerce_template_parms (TREE_VALUE (t), |
be93747e | 5796 | arglist, templ, |
3db45ab5 | 5797 | complain, |
e7e93965 MM |
5798 | /*require_all_args=*/true, |
5799 | /*use_default_args=*/true); | |
88e98cfe KL |
5800 | |
5801 | /* Don't process further if one of the levels fails. */ | |
5802 | if (a == error_mark_node) | |
5803 | { | |
5804 | /* Restore the ARGLIST to its full size. */ | |
5805 | TREE_VEC_LENGTH (arglist) = saved_depth; | |
5806 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node); | |
5807 | } | |
c8094d83 | 5808 | |
e4a84209 MM |
5809 | SET_TMPL_ARGS_LEVEL (bound_args, i, a); |
5810 | ||
5811 | /* We temporarily reduce the length of the ARGLIST so | |
5812 | that coerce_template_parms will see only the arguments | |
5813 | corresponding to the template parameters it is | |
5814 | examining. */ | |
5815 | TREE_VEC_LENGTH (arglist)--; | |
5816 | } | |
5817 | ||
5818 | /* Restore the ARGLIST to its full size. */ | |
5819 | TREE_VEC_LENGTH (arglist) = saved_depth; | |
5820 | ||
36a117a5 | 5821 | arglist = bound_args; |
830bfa74 MM |
5822 | } |
5823 | else | |
36a117a5 MM |
5824 | arglist |
5825 | = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist), | |
f9a7ae04 | 5826 | INNERMOST_TEMPLATE_ARGS (arglist), |
be93747e | 5827 | templ, |
3db45ab5 | 5828 | complain, |
e7e93965 MM |
5829 | /*require_all_args=*/true, |
5830 | /*use_default_args=*/true); | |
36a117a5 | 5831 | |
c353b8e3 | 5832 | if (arglist == error_mark_node) |
36a117a5 | 5833 | /* We were unable to bind the arguments. */ |
fd295cb2 | 5834 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node); |
5566b478 | 5835 | |
36a117a5 MM |
5836 | /* In the scope of a template class, explicit references to the |
5837 | template class refer to the type of the template, not any | |
5838 | instantiation of it. For example, in: | |
c8094d83 | 5839 | |
36a117a5 MM |
5840 | template <class T> class C { void f(C<T>); } |
5841 | ||
5842 | the `C<T>' is just the same as `C'. Outside of the | |
5843 | class, however, such a reference is an instantiation. */ | |
ed44da02 | 5844 | if (comp_template_args (TYPE_TI_ARGS (template_type), |
36a117a5 MM |
5845 | arglist)) |
5846 | { | |
5847 | found = template_type; | |
c8094d83 | 5848 | |
be93747e | 5849 | if (!entering_scope && PRIMARY_TEMPLATE_P (templ)) |
5566b478 | 5850 | { |
36a117a5 | 5851 | tree ctx; |
c8094d83 MS |
5852 | |
5853 | for (ctx = current_class_type; | |
5f04800c KL |
5854 | ctx && TREE_CODE (ctx) != NAMESPACE_DECL; |
5855 | ctx = (TYPE_P (ctx) | |
5856 | ? TYPE_CONTEXT (ctx) | |
5857 | : DECL_CONTEXT (ctx))) | |
5858 | if (TYPE_P (ctx) && same_type_p (ctx, template_type)) | |
5859 | goto found_ctx; | |
c8094d83 | 5860 | |
6df5158a NS |
5861 | /* We're not in the scope of the class, so the |
5862 | TEMPLATE_TYPE is not the type we want after all. */ | |
5863 | found = NULL_TREE; | |
5864 | found_ctx:; | |
5566b478 MS |
5865 | } |
5866 | } | |
36a117a5 | 5867 | if (found) |
0cbd7506 | 5868 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, found); |
414ea4aa | 5869 | |
c7222c02 | 5870 | /* If we already have this specialization, return it. */ |
be93747e | 5871 | found = retrieve_specialization (templ, arglist, |
c7222c02 MM |
5872 | /*class_specializations_p=*/false); |
5873 | if (found) | |
5874 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, found); | |
5566b478 | 5875 | |
dbfe2124 | 5876 | /* This type is a "partial instantiation" if any of the template |
ab097535 | 5877 | arguments still involve template parameters. Note that we set |
486e4077 MM |
5878 | IS_PARTIAL_INSTANTIATION for partial specializations as |
5879 | well. */ | |
dbfe2124 MM |
5880 | is_partial_instantiation = uses_template_parms (arglist); |
5881 | ||
c353b8e3 MM |
5882 | /* If the deduced arguments are invalid, then the binding |
5883 | failed. */ | |
5884 | if (!is_partial_instantiation | |
be93747e | 5885 | && check_instantiated_args (templ, |
c353b8e3 MM |
5886 | INNERMOST_TEMPLATE_ARGS (arglist), |
5887 | complain)) | |
5888 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node); | |
c8094d83 MS |
5889 | |
5890 | if (!is_partial_instantiation | |
be93747e KG |
5891 | && !PRIMARY_TEMPLATE_P (templ) |
5892 | && TREE_CODE (CP_DECL_CONTEXT (templ)) == NAMESPACE_DECL) | |
3ebc5c52 | 5893 | { |
be93747e KG |
5894 | found = xref_tag_from_type (TREE_TYPE (templ), |
5895 | DECL_NAME (templ), | |
29ef83de | 5896 | /*tag_scope=*/ts_global); |
fd295cb2 | 5897 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, found); |
3ebc5c52 | 5898 | } |
c8094d83 | 5899 | |
be93747e | 5900 | context = tsubst (DECL_CONTEXT (templ), arglist, |
c2ea3a40 | 5901 | complain, in_decl); |
95ee998c MM |
5902 | if (!context) |
5903 | context = global_namespace; | |
5904 | ||
36a117a5 | 5905 | /* Create the type. */ |
ed44da02 MM |
5906 | if (TREE_CODE (template_type) == ENUMERAL_TYPE) |
5907 | { | |
dbfe2124 | 5908 | if (!is_partial_instantiation) |
92777ce4 NS |
5909 | { |
5910 | set_current_access_from_decl (TYPE_NAME (template_type)); | |
adf2edec DG |
5911 | t = start_enum (TYPE_IDENTIFIER (template_type), |
5912 | tsubst (ENUM_UNDERLYING_TYPE (template_type), | |
5913 | arglist, complain, in_decl), | |
5914 | SCOPED_ENUM_P (template_type)); | |
92777ce4 | 5915 | } |
ed44da02 | 5916 | else |
adf2edec DG |
5917 | { |
5918 | /* We don't want to call start_enum for this type, since | |
5919 | the values for the enumeration constants may involve | |
5920 | template parameters. And, no one should be interested | |
5921 | in the enumeration constants for such a type. */ | |
5922 | t = make_node (ENUMERAL_TYPE); | |
5923 | SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type)); | |
5924 | } | |
ed44da02 MM |
5925 | } |
5926 | else | |
5927 | { | |
9e1e64ec | 5928 | t = make_class_type (TREE_CODE (template_type)); |
c8094d83 | 5929 | CLASSTYPE_DECLARED_CLASS (t) |
ed44da02 | 5930 | = CLASSTYPE_DECLARED_CLASS (template_type); |
ed44da02 | 5931 | SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t); |
f668f160 | 5932 | TYPE_FOR_JAVA (t) = TYPE_FOR_JAVA (template_type); |
ae673f14 JM |
5933 | |
5934 | /* A local class. Make sure the decl gets registered properly. */ | |
5935 | if (context == current_function_decl) | |
be93747e | 5936 | pushtag (DECL_NAME (templ), t, /*tag_scope=*/ts_current); |
06d40de8 DG |
5937 | |
5938 | if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist)) | |
5939 | /* This instantiation is another name for the primary | |
5940 | template type. Set the TYPE_CANONICAL field | |
5941 | appropriately. */ | |
5942 | TYPE_CANONICAL (t) = template_type; | |
5943 | else if (any_template_arguments_need_structural_equality_p (arglist)) | |
5944 | /* Some of the template arguments require structural | |
5945 | equality testing, so this template class requires | |
5946 | structural equality testing. */ | |
5947 | SET_TYPE_STRUCTURAL_EQUALITY (t); | |
ed44da02 MM |
5948 | } |
5949 | ||
ae673f14 JM |
5950 | /* If we called start_enum or pushtag above, this information |
5951 | will already be set up. */ | |
ed44da02 MM |
5952 | if (!TYPE_NAME (t)) |
5953 | { | |
5954 | TYPE_CONTEXT (t) = FROB_CONTEXT (context); | |
c8094d83 | 5955 | |
be93747e | 5956 | type_decl = create_implicit_typedef (DECL_NAME (templ), t); |
ed44da02 | 5957 | DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t); |
9188c363 | 5958 | TYPE_STUB_DECL (t) = type_decl; |
c8094d83 | 5959 | DECL_SOURCE_LOCATION (type_decl) |
f31686a3 | 5960 | = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type)); |
ed44da02 MM |
5961 | } |
5962 | else | |
5963 | type_decl = TYPE_NAME (t); | |
36a117a5 | 5964 | |
cab7a9a3 KL |
5965 | TREE_PRIVATE (type_decl) |
5966 | = TREE_PRIVATE (TYPE_STUB_DECL (template_type)); | |
5967 | TREE_PROTECTED (type_decl) | |
5968 | = TREE_PROTECTED (TYPE_STUB_DECL (template_type)); | |
8d039470 MS |
5969 | if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type)) |
5970 | { | |
5971 | DECL_VISIBILITY_SPECIFIED (type_decl) = 1; | |
5972 | DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type); | |
5973 | } | |
cab7a9a3 | 5974 | |
9fbf56f7 MM |
5975 | /* Set up the template information. We have to figure out which |
5976 | template is the immediate parent if this is a full | |
5977 | instantiation. */ | |
5978 | if (parm_depth == 1 || is_partial_instantiation | |
be93747e | 5979 | || !PRIMARY_TEMPLATE_P (templ)) |
9fbf56f7 | 5980 | /* This case is easy; there are no member templates involved. */ |
be93747e | 5981 | found = templ; |
9fbf56f7 MM |
5982 | else |
5983 | { | |
ab097535 NS |
5984 | /* This is a full instantiation of a member template. Look |
5985 | for a partial instantiation of which this is an instance. */ | |
9fbf56f7 | 5986 | |
be93747e | 5987 | for (found = DECL_TEMPLATE_INSTANTIATIONS (templ); |
9fbf56f7 MM |
5988 | found; found = TREE_CHAIN (found)) |
5989 | { | |
5990 | int success; | |
5991 | tree tmpl = CLASSTYPE_TI_TEMPLATE (TREE_VALUE (found)); | |
5992 | ||
5993 | /* We only want partial instantiations, here, not | |
5994 | specializations or full instantiations. */ | |
370af2d5 | 5995 | if (CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_VALUE (found)) |
9fbf56f7 MM |
5996 | || !uses_template_parms (TREE_VALUE (found))) |
5997 | continue; | |
5998 | ||
5999 | /* Temporarily reduce by one the number of levels in the | |
6000 | ARGLIST and in FOUND so as to avoid comparing the | |
6001 | last set of arguments. */ | |
6002 | TREE_VEC_LENGTH (arglist)--; | |
6003 | TREE_VEC_LENGTH (TREE_PURPOSE (found)) --; | |
6004 | ||
6005 | /* See if the arguments match. If they do, then TMPL is | |
6006 | the partial instantiation we want. */ | |
6007 | success = comp_template_args (TREE_PURPOSE (found), arglist); | |
6008 | ||
6009 | /* Restore the argument vectors to their full size. */ | |
6010 | TREE_VEC_LENGTH (arglist)++; | |
6011 | TREE_VEC_LENGTH (TREE_PURPOSE (found))++; | |
6012 | ||
6013 | if (success) | |
6014 | { | |
6015 | found = tmpl; | |
6016 | break; | |
6017 | } | |
6018 | } | |
6019 | ||
6020 | if (!found) | |
ab097535 NS |
6021 | { |
6022 | /* There was no partial instantiation. This happens | |
0cbd7506 MS |
6023 | where C<T> is a member template of A<T> and it's used |
6024 | in something like | |
c8094d83 | 6025 | |
0cbd7506 MS |
6026 | template <typename T> struct B { A<T>::C<int> m; }; |
6027 | B<float>; | |
c8094d83 | 6028 | |
0cbd7506 MS |
6029 | Create the partial instantiation. |
6030 | */ | |
6031 | TREE_VEC_LENGTH (arglist)--; | |
be93747e | 6032 | found = tsubst (templ, arglist, complain, NULL_TREE); |
0cbd7506 MS |
6033 | TREE_VEC_LENGTH (arglist)++; |
6034 | } | |
9fbf56f7 MM |
6035 | } |
6036 | ||
c8094d83 | 6037 | SET_TYPE_TEMPLATE_INFO (t, tree_cons (found, arglist, NULL_TREE)); |
be93747e | 6038 | DECL_TEMPLATE_INSTANTIATIONS (templ) |
c8094d83 | 6039 | = tree_cons (arglist, t, |
be93747e | 6040 | DECL_TEMPLATE_INSTANTIATIONS (templ)); |
dbfe2124 | 6041 | |
c8094d83 | 6042 | if (TREE_CODE (t) == ENUMERAL_TYPE |
dbfe2124 | 6043 | && !is_partial_instantiation) |
61a127b3 MM |
6044 | /* Now that the type has been registered on the instantiations |
6045 | list, we set up the enumerators. Because the enumeration | |
6046 | constants may involve the enumeration type itself, we make | |
6047 | sure to register the type first, and then create the | |
6048 | constants. That way, doing tsubst_expr for the enumeration | |
6049 | constants won't result in recursive calls here; we'll find | |
6050 | the instantiation and exit above. */ | |
6051 | tsubst_enum (template_type, t, arglist); | |
dbfe2124 | 6052 | |
7813d14c | 6053 | if (is_partial_instantiation) |
077e7015 MM |
6054 | /* If the type makes use of template parameters, the |
6055 | code that generates debugging information will crash. */ | |
6056 | DECL_IGNORED_P (TYPE_STUB_DECL (t)) = 1; | |
8d08fdba | 6057 | |
b9e75696 JM |
6058 | /* Possibly limit visibility based on template args. */ |
6059 | TREE_PUBLIC (type_decl) = 1; | |
6060 | determine_visibility (type_decl); | |
6061 | ||
fd295cb2 | 6062 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t); |
36a117a5 | 6063 | } |
fd295cb2 | 6064 | timevar_pop (TV_NAME_LOOKUP); |
8d08fdba MS |
6065 | } |
6066 | \f | |
c8094d83 | 6067 | struct pair_fn_data |
8d08fdba | 6068 | { |
8dfaeb63 MM |
6069 | tree_fn_t fn; |
6070 | void *data; | |
c095a4f8 DG |
6071 | /* True when we should also visit template parameters that occur in |
6072 | non-deduced contexts. */ | |
6073 | bool include_nondeduced_p; | |
0c58f841 | 6074 | struct pointer_set_t *visited; |
8dfaeb63 MM |
6075 | }; |
6076 | ||
6077 | /* Called from for_each_template_parm via walk_tree. */ | |
581d38d0 | 6078 | |
8dfaeb63 | 6079 | static tree |
350fae66 | 6080 | for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d) |
8dfaeb63 MM |
6081 | { |
6082 | tree t = *tp; | |
6083 | struct pair_fn_data *pfd = (struct pair_fn_data *) d; | |
6084 | tree_fn_t fn = pfd->fn; | |
6085 | void *data = pfd->data; | |
4f2c9d7e | 6086 | |
2f939d94 | 6087 | if (TYPE_P (t) |
c095a4f8 DG |
6088 | && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE) |
6089 | && for_each_template_parm (TYPE_CONTEXT (t), fn, data, pfd->visited, | |
6090 | pfd->include_nondeduced_p)) | |
8dfaeb63 | 6091 | return error_mark_node; |
581d38d0 | 6092 | |
8d08fdba MS |
6093 | switch (TREE_CODE (t)) |
6094 | { | |
8d08fdba | 6095 | case RECORD_TYPE: |
9076e292 | 6096 | if (TYPE_PTRMEMFUNC_P (t)) |
8dfaeb63 | 6097 | break; |
ed44da02 MM |
6098 | /* Fall through. */ |
6099 | ||
8d08fdba | 6100 | case UNION_TYPE: |
ed44da02 | 6101 | case ENUMERAL_TYPE: |
8dfaeb63 MM |
6102 | if (!TYPE_TEMPLATE_INFO (t)) |
6103 | *walk_subtrees = 0; | |
6104 | else if (for_each_template_parm (TREE_VALUE (TYPE_TEMPLATE_INFO (t)), | |
c095a4f8 DG |
6105 | fn, data, pfd->visited, |
6106 | pfd->include_nondeduced_p)) | |
8dfaeb63 MM |
6107 | return error_mark_node; |
6108 | break; | |
6109 | ||
8f6e6bf3 EB |
6110 | case INTEGER_TYPE: |
6111 | if (for_each_template_parm (TYPE_MIN_VALUE (t), | |
c095a4f8 DG |
6112 | fn, data, pfd->visited, |
6113 | pfd->include_nondeduced_p) | |
8f6e6bf3 | 6114 | || for_each_template_parm (TYPE_MAX_VALUE (t), |
c095a4f8 DG |
6115 | fn, data, pfd->visited, |
6116 | pfd->include_nondeduced_p)) | |
8f6e6bf3 EB |
6117 | return error_mark_node; |
6118 | break; | |
6119 | ||
588c2d1c | 6120 | case METHOD_TYPE: |
8dfaeb63 MM |
6121 | /* Since we're not going to walk subtrees, we have to do this |
6122 | explicitly here. */ | |
4f2c9d7e | 6123 | if (for_each_template_parm (TYPE_METHOD_BASETYPE (t), fn, data, |
c095a4f8 | 6124 | pfd->visited, pfd->include_nondeduced_p)) |
8dfaeb63 | 6125 | return error_mark_node; |
4890c2f4 | 6126 | /* Fall through. */ |
588c2d1c MM |
6127 | |
6128 | case FUNCTION_TYPE: | |
8dfaeb63 | 6129 | /* Check the return type. */ |
c095a4f8 DG |
6130 | if (for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited, |
6131 | pfd->include_nondeduced_p)) | |
8dfaeb63 MM |
6132 | return error_mark_node; |
6133 | ||
588c2d1c MM |
6134 | /* Check the parameter types. Since default arguments are not |
6135 | instantiated until they are needed, the TYPE_ARG_TYPES may | |
6136 | contain expressions that involve template parameters. But, | |
6137 | no-one should be looking at them yet. And, once they're | |
6138 | instantiated, they don't contain template parameters, so | |
6139 | there's no point in looking at them then, either. */ | |
6140 | { | |
6141 | tree parm; | |
6142 | ||
6143 | for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm)) | |
4f2c9d7e | 6144 | if (for_each_template_parm (TREE_VALUE (parm), fn, data, |
c095a4f8 | 6145 | pfd->visited, pfd->include_nondeduced_p)) |
8dfaeb63 | 6146 | return error_mark_node; |
5566b478 | 6147 | |
8dfaeb63 MM |
6148 | /* Since we've already handled the TYPE_ARG_TYPES, we don't |
6149 | want walk_tree walking into them itself. */ | |
6150 | *walk_subtrees = 0; | |
6151 | } | |
6152 | break; | |
3ac3d9ea | 6153 | |
a723baf1 | 6154 | case TYPEOF_TYPE: |
c095a4f8 DG |
6155 | if (pfd->include_nondeduced_p |
6156 | && for_each_template_parm (TYPE_FIELDS (t), fn, data, | |
6157 | pfd->visited, | |
6158 | pfd->include_nondeduced_p)) | |
a723baf1 MM |
6159 | return error_mark_node; |
6160 | break; | |
6161 | ||
8d08fdba | 6162 | case FUNCTION_DECL: |
5566b478 | 6163 | case VAR_DECL: |
5566b478 | 6164 | if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t) |
4f2c9d7e | 6165 | && for_each_template_parm (DECL_TI_ARGS (t), fn, data, |
c095a4f8 | 6166 | pfd->visited, pfd->include_nondeduced_p)) |
8dfaeb63 MM |
6167 | return error_mark_node; |
6168 | /* Fall through. */ | |
6169 | ||
8d08fdba | 6170 | case PARM_DECL: |
a723baf1 MM |
6171 | case CONST_DECL: |
6172 | if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t) | |
6173 | && for_each_template_parm (DECL_INITIAL (t), fn, data, | |
c095a4f8 | 6174 | pfd->visited, pfd->include_nondeduced_p)) |
a723baf1 | 6175 | return error_mark_node; |
c8094d83 | 6176 | if (DECL_CONTEXT (t) |
c095a4f8 | 6177 | && pfd->include_nondeduced_p |
4f2c9d7e | 6178 | && for_each_template_parm (DECL_CONTEXT (t), fn, data, |
c095a4f8 | 6179 | pfd->visited, pfd->include_nondeduced_p)) |
8dfaeb63 MM |
6180 | return error_mark_node; |
6181 | break; | |
8d08fdba | 6182 | |
a1281f45 | 6183 | case BOUND_TEMPLATE_TEMPLATE_PARM: |
744fac59 | 6184 | /* Record template parameters such as `T' inside `TT<T>'. */ |
c095a4f8 DG |
6185 | if (for_each_template_parm (TYPE_TI_ARGS (t), fn, data, pfd->visited, |
6186 | pfd->include_nondeduced_p)) | |
8dfaeb63 MM |
6187 | return error_mark_node; |
6188 | /* Fall through. */ | |
6189 | ||
a1281f45 | 6190 | case TEMPLATE_TEMPLATE_PARM: |
744fac59 | 6191 | case TEMPLATE_TYPE_PARM: |
f84b4be9 | 6192 | case TEMPLATE_PARM_INDEX: |
8dfaeb63 MM |
6193 | if (fn && (*fn)(t, data)) |
6194 | return error_mark_node; | |
6195 | else if (!fn) | |
6196 | return error_mark_node; | |
6197 | break; | |
5156628f | 6198 | |
8dfaeb63 | 6199 | case TEMPLATE_DECL: |
f4f206f4 | 6200 | /* A template template parameter is encountered. */ |
8dfaeb63 | 6201 | if (DECL_TEMPLATE_TEMPLATE_PARM_P (t) |
c095a4f8 DG |
6202 | && for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited, |
6203 | pfd->include_nondeduced_p)) | |
8dfaeb63 | 6204 | return error_mark_node; |
db5ae43f | 6205 | |
8dfaeb63 MM |
6206 | /* Already substituted template template parameter */ |
6207 | *walk_subtrees = 0; | |
6208 | break; | |
b8865407 | 6209 | |
4699c561 | 6210 | case TYPENAME_TYPE: |
c8094d83 | 6211 | if (!fn |
4f2c9d7e | 6212 | || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t), fn, |
c095a4f8 DG |
6213 | data, pfd->visited, |
6214 | pfd->include_nondeduced_p)) | |
8dfaeb63 MM |
6215 | return error_mark_node; |
6216 | break; | |
4699c561 | 6217 | |
8dfaeb63 MM |
6218 | case CONSTRUCTOR: |
6219 | if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t)) | |
c095a4f8 | 6220 | && pfd->include_nondeduced_p |
8dfaeb63 | 6221 | && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE |
4f2c9d7e | 6222 | (TREE_TYPE (t)), fn, data, |
c095a4f8 | 6223 | pfd->visited, pfd->include_nondeduced_p)) |
8dfaeb63 MM |
6224 | return error_mark_node; |
6225 | break; | |
c8094d83 | 6226 | |
b8865407 MM |
6227 | case INDIRECT_REF: |
6228 | case COMPONENT_REF: | |
4699c561 | 6229 | /* If there's no type, then this thing must be some expression |
b8865407 | 6230 | involving template parameters. */ |
4699c561 | 6231 | if (!fn && !TREE_TYPE (t)) |
8dfaeb63 MM |
6232 | return error_mark_node; |
6233 | break; | |
b8865407 | 6234 | |
42976354 BK |
6235 | case MODOP_EXPR: |
6236 | case CAST_EXPR: | |
6237 | case REINTERPRET_CAST_EXPR: | |
6238 | case CONST_CAST_EXPR: | |
6239 | case STATIC_CAST_EXPR: | |
6240 | case DYNAMIC_CAST_EXPR: | |
42976354 BK |
6241 | case ARROW_EXPR: |
6242 | case DOTSTAR_EXPR: | |
6243 | case TYPEID_EXPR: | |
40242ccf | 6244 | case PSEUDO_DTOR_EXPR: |
b8865407 | 6245 | if (!fn) |
8dfaeb63 MM |
6246 | return error_mark_node; |
6247 | break; | |
abff8e06 | 6248 | |
8d08fdba | 6249 | default: |
8dfaeb63 | 6250 | break; |
8d08fdba | 6251 | } |
8dfaeb63 MM |
6252 | |
6253 | /* We didn't find any template parameters we liked. */ | |
6254 | return NULL_TREE; | |
6255 | } | |
6256 | ||
c8094d83 MS |
6257 | /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM, |
6258 | BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T, | |
a1281f45 | 6259 | call FN with the parameter and the DATA. |
838dfd8a | 6260 | If FN returns nonzero, the iteration is terminated, and |
8dfaeb63 | 6261 | for_each_template_parm returns 1. Otherwise, the iteration |
838dfd8a | 6262 | continues. If FN never returns a nonzero value, the value |
8dfaeb63 | 6263 | returned by for_each_template_parm is 0. If FN is NULL, it is |
c095a4f8 DG |
6264 | considered to be the function which always returns 1. |
6265 | ||
6266 | If INCLUDE_NONDEDUCED_P, then this routine will also visit template | |
6267 | parameters that occur in non-deduced contexts. When false, only | |
6268 | visits those template parameters that can be deduced. */ | |
8dfaeb63 MM |
6269 | |
6270 | static int | |
0c58f841 | 6271 | for_each_template_parm (tree t, tree_fn_t fn, void* data, |
c095a4f8 DG |
6272 | struct pointer_set_t *visited, |
6273 | bool include_nondeduced_p) | |
8dfaeb63 MM |
6274 | { |
6275 | struct pair_fn_data pfd; | |
ad2ae3b2 | 6276 | int result; |
8dfaeb63 MM |
6277 | |
6278 | /* Set up. */ | |
6279 | pfd.fn = fn; | |
6280 | pfd.data = data; | |
c095a4f8 | 6281 | pfd.include_nondeduced_p = include_nondeduced_p; |
8dfaeb63 | 6282 | |
4890c2f4 MM |
6283 | /* Walk the tree. (Conceptually, we would like to walk without |
6284 | duplicates, but for_each_template_parm_r recursively calls | |
6285 | for_each_template_parm, so we would need to reorganize a fair | |
4f2c9d7e MM |
6286 | bit to use walk_tree_without_duplicates, so we keep our own |
6287 | visited list.) */ | |
6288 | if (visited) | |
6289 | pfd.visited = visited; | |
6290 | else | |
0c58f841 | 6291 | pfd.visited = pointer_set_create (); |
14588106 RG |
6292 | result = cp_walk_tree (&t, |
6293 | for_each_template_parm_r, | |
6294 | &pfd, | |
6295 | pfd.visited) != NULL_TREE; | |
ad2ae3b2 MM |
6296 | |
6297 | /* Clean up. */ | |
6298 | if (!visited) | |
0c58f841 MA |
6299 | { |
6300 | pointer_set_destroy (pfd.visited); | |
6301 | pfd.visited = 0; | |
6302 | } | |
ad2ae3b2 MM |
6303 | |
6304 | return result; | |
8d08fdba MS |
6305 | } |
6306 | ||
d43f603d KL |
6307 | /* Returns true if T depends on any template parameter. */ |
6308 | ||
050367a3 | 6309 | int |
3a978d72 | 6310 | uses_template_parms (tree t) |
050367a3 | 6311 | { |
c353b8e3 MM |
6312 | bool dependent_p; |
6313 | int saved_processing_template_decl; | |
6314 | ||
6315 | saved_processing_template_decl = processing_template_decl; | |
6316 | if (!saved_processing_template_decl) | |
6317 | processing_template_decl = 1; | |
6318 | if (TYPE_P (t)) | |
6319 | dependent_p = dependent_type_p (t); | |
6320 | else if (TREE_CODE (t) == TREE_VEC) | |
6321 | dependent_p = any_dependent_template_arguments_p (t); | |
6322 | else if (TREE_CODE (t) == TREE_LIST) | |
6323 | dependent_p = (uses_template_parms (TREE_VALUE (t)) | |
6324 | || uses_template_parms (TREE_CHAIN (t))); | |
3ce5fa4f NS |
6325 | else if (TREE_CODE (t) == TYPE_DECL) |
6326 | dependent_p = dependent_type_p (TREE_TYPE (t)); | |
c8094d83 MS |
6327 | else if (DECL_P (t) |
6328 | || EXPR_P (t) | |
c353b8e3 MM |
6329 | || TREE_CODE (t) == TEMPLATE_PARM_INDEX |
6330 | || TREE_CODE (t) == OVERLOAD | |
6331 | || TREE_CODE (t) == BASELINK | |
b207d6e2 | 6332 | || TREE_CODE (t) == IDENTIFIER_NODE |
cb68ec50 | 6333 | || TREE_CODE (t) == TRAIT_EXPR |
c1b93f2b | 6334 | || TREE_CODE (t) == CONSTRUCTOR |
6615c446 | 6335 | || CONSTANT_CLASS_P (t)) |
c353b8e3 MM |
6336 | dependent_p = (type_dependent_expression_p (t) |
6337 | || value_dependent_expression_p (t)); | |
315fb5db NS |
6338 | else |
6339 | { | |
6340 | gcc_assert (t == error_mark_node); | |
6341 | dependent_p = false; | |
6342 | } | |
c8094d83 | 6343 | |
c353b8e3 MM |
6344 | processing_template_decl = saved_processing_template_decl; |
6345 | ||
6346 | return dependent_p; | |
050367a3 MM |
6347 | } |
6348 | ||
d43f603d KL |
6349 | /* Returns true if T depends on any template parameter with level LEVEL. */ |
6350 | ||
6351 | int | |
6352 | uses_template_parms_level (tree t, int level) | |
6353 | { | |
c095a4f8 DG |
6354 | return for_each_template_parm (t, template_parm_this_level_p, &level, NULL, |
6355 | /*include_nondeduced_p=*/true); | |
d43f603d KL |
6356 | } |
6357 | ||
27fafc8d | 6358 | static int tinst_depth; |
e9f32eb5 | 6359 | extern int max_tinst_depth; |
5566b478 | 6360 | #ifdef GATHER_STATISTICS |
27fafc8d | 6361 | int depth_reached; |
5566b478 | 6362 | #endif |
8dfaeb63 MM |
6363 | static int tinst_level_tick; |
6364 | static int last_template_error_tick; | |
8d08fdba | 6365 | |
3ae18eaf JM |
6366 | /* We're starting to instantiate D; record the template instantiation context |
6367 | for diagnostics and to restore it later. */ | |
6368 | ||
aa9d8196 | 6369 | static int |
3a978d72 | 6370 | push_tinst_level (tree d) |
8d08fdba | 6371 | { |
be93747e | 6372 | struct tinst_level *new_level; |
8d08fdba | 6373 | |
7215f9a0 MS |
6374 | if (tinst_depth >= max_tinst_depth) |
6375 | { | |
8adf5b5e JM |
6376 | /* If the instantiation in question still has unbound template parms, |
6377 | we don't really care if we can't instantiate it, so just return. | |
0cbd7506 | 6378 | This happens with base instantiation for implicit `typename'. */ |
8adf5b5e JM |
6379 | if (uses_template_parms (d)) |
6380 | return 0; | |
6381 | ||
1139b3d8 | 6382 | last_template_error_tick = tinst_level_tick; |
0f51ccfc | 6383 | error ("template instantiation depth exceeds maximum of %d (use " |
0cbd7506 | 6384 | "-ftemplate-depth-NN to increase the maximum) instantiating %qD", |
b4f4233d | 6385 | max_tinst_depth, d); |
5566b478 | 6386 | |
cb753e49 | 6387 | print_instantiation_context (); |
5566b478 | 6388 | |
7215f9a0 MS |
6389 | return 0; |
6390 | } | |
6391 | ||
be93747e KG |
6392 | new_level = GGC_NEW (struct tinst_level); |
6393 | new_level->decl = d; | |
6394 | new_level->locus = input_location; | |
6395 | new_level->in_system_header_p = in_system_header; | |
6396 | new_level->next = current_tinst_level; | |
6397 | current_tinst_level = new_level; | |
5566b478 | 6398 | |
7215f9a0 | 6399 | ++tinst_depth; |
5566b478 MS |
6400 | #ifdef GATHER_STATISTICS |
6401 | if (tinst_depth > depth_reached) | |
6402 | depth_reached = tinst_depth; | |
6403 | #endif | |
6404 | ||
27fafc8d | 6405 | ++tinst_level_tick; |
7215f9a0 | 6406 | return 1; |
8d08fdba MS |
6407 | } |
6408 | ||
3ae18eaf JM |
6409 | /* We're done instantiating this template; return to the instantiation |
6410 | context. */ | |
6411 | ||
aa9d8196 | 6412 | static void |
3a978d72 | 6413 | pop_tinst_level (void) |
8d08fdba | 6414 | { |
ae58fa02 MM |
6415 | /* Restore the filename and line number stashed away when we started |
6416 | this instantiation. */ | |
e2c3721c | 6417 | input_location = current_tinst_level->locus; |
e2c3721c | 6418 | current_tinst_level = current_tinst_level->next; |
7215f9a0 | 6419 | --tinst_depth; |
27fafc8d | 6420 | ++tinst_level_tick; |
8d08fdba MS |
6421 | } |
6422 | ||
3ae18eaf JM |
6423 | /* We're instantiating a deferred template; restore the template |
6424 | instantiation context in which the instantiation was requested, which | |
e2c3721c | 6425 | is one step out from LEVEL. Return the corresponding DECL or TYPE. */ |
3ae18eaf | 6426 | |
e2c3721c PB |
6427 | static tree |
6428 | reopen_tinst_level (struct tinst_level *level) | |
3ae18eaf | 6429 | { |
e2c3721c | 6430 | struct tinst_level *t; |
3ae18eaf JM |
6431 | |
6432 | tinst_depth = 0; | |
e2c3721c | 6433 | for (t = level; t; t = t->next) |
3ae18eaf JM |
6434 | ++tinst_depth; |
6435 | ||
6436 | current_tinst_level = level; | |
6437 | pop_tinst_level (); | |
e2c3721c | 6438 | return level->decl; |
3ae18eaf JM |
6439 | } |
6440 | ||
61172206 JM |
6441 | /* Returns the TINST_LEVEL which gives the original instantiation |
6442 | context. */ | |
6443 | ||
e2c3721c | 6444 | struct tinst_level * |
61172206 JM |
6445 | outermost_tinst_level (void) |
6446 | { | |
e2c3721c PB |
6447 | struct tinst_level *level = current_tinst_level; |
6448 | if (level) | |
6449 | while (level->next) | |
6450 | level = level->next; | |
6451 | return level; | |
61172206 JM |
6452 | } |
6453 | ||
172a4594 DS |
6454 | /* Returns TRUE if PARM is a parameter of the template TEMPL. */ |
6455 | ||
6456 | bool | |
6457 | parameter_of_template_p (tree parm, tree templ) | |
6458 | { | |
6459 | tree parms; | |
6460 | int i; | |
6461 | ||
6462 | if (!parm || !templ) | |
6463 | return false; | |
6464 | ||
6465 | gcc_assert (DECL_TEMPLATE_PARM_P (parm)); | |
6466 | gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL); | |
6467 | ||
6468 | parms = DECL_TEMPLATE_PARMS (templ); | |
6469 | parms = INNERMOST_TEMPLATE_PARMS (parms); | |
6470 | ||
6471 | for (i = 0; i < TREE_VEC_LENGTH (parms); ++i) | |
6472 | if (parm == TREE_VALUE (TREE_VEC_ELT (parms, i))) | |
6473 | return true; | |
6474 | ||
6475 | return false; | |
6476 | } | |
6477 | ||
f84b4be9 JM |
6478 | /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the |
6479 | vector of template arguments, as for tsubst. | |
6480 | ||
dc957d14 | 6481 | Returns an appropriate tsubst'd friend declaration. */ |
f84b4be9 JM |
6482 | |
6483 | static tree | |
3a978d72 | 6484 | tsubst_friend_function (tree decl, tree args) |
f84b4be9 JM |
6485 | { |
6486 | tree new_friend; | |
27fafc8d | 6487 | |
c8094d83 | 6488 | if (TREE_CODE (decl) == FUNCTION_DECL |
f84b4be9 JM |
6489 | && DECL_TEMPLATE_INSTANTIATION (decl) |
6490 | && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL) | |
6491 | /* This was a friend declared with an explicit template | |
6492 | argument list, e.g.: | |
c8094d83 | 6493 | |
f84b4be9 | 6494 | friend void f<>(T); |
c8094d83 | 6495 | |
f84b4be9 JM |
6496 | to indicate that f was a template instantiation, not a new |
6497 | function declaration. Now, we have to figure out what | |
6498 | instantiation of what template. */ | |
6499 | { | |
76e57b45 | 6500 | tree template_id, arglist, fns; |
f84b4be9 JM |
6501 | tree new_args; |
6502 | tree tmpl; | |
ed2fa432 | 6503 | tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type)); |
c8094d83 | 6504 | |
76e57b45 | 6505 | /* Friend functions are looked up in the containing namespace scope. |
0cbd7506 | 6506 | We must enter that scope, to avoid finding member functions of the |
39a13be5 | 6507 | current class with same name. */ |
76e57b45 NS |
6508 | push_nested_namespace (ns); |
6509 | fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args, | |
015c2c66 MM |
6510 | tf_warning_or_error, NULL_TREE, |
6511 | /*integral_constant_expression_p=*/false); | |
76e57b45 NS |
6512 | pop_nested_namespace (ns); |
6513 | arglist = tsubst (DECL_TI_ARGS (decl), args, | |
23fca1f5 | 6514 | tf_warning_or_error, NULL_TREE); |
76e57b45 | 6515 | template_id = lookup_template_function (fns, arglist); |
c8094d83 | 6516 | |
23fca1f5 | 6517 | new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE); |
36a117a5 | 6518 | tmpl = determine_specialization (template_id, new_friend, |
c8094d83 | 6519 | &new_args, |
5fe7b654 | 6520 | /*need_member_template=*/0, |
29a1da1c MM |
6521 | TREE_VEC_LENGTH (args), |
6522 | tsk_none); | |
6e432b31 | 6523 | return instantiate_template (tmpl, new_args, tf_error); |
f84b4be9 | 6524 | } |
36a117a5 | 6525 | |
23fca1f5 | 6526 | new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE); |
c8094d83 | 6527 | |
36a117a5 | 6528 | /* The NEW_FRIEND will look like an instantiation, to the |
f84b4be9 JM |
6529 | compiler, but is not an instantiation from the point of view of |
6530 | the language. For example, we might have had: | |
c8094d83 | 6531 | |
f84b4be9 JM |
6532 | template <class T> struct S { |
6533 | template <class U> friend void f(T, U); | |
6534 | }; | |
c8094d83 | 6535 | |
f84b4be9 JM |
6536 | Then, in S<int>, template <class U> void f(int, U) is not an |
6537 | instantiation of anything. */ | |
ac2b3222 AP |
6538 | if (new_friend == error_mark_node) |
6539 | return error_mark_node; | |
c8094d83 | 6540 | |
f84b4be9 JM |
6541 | DECL_USE_TEMPLATE (new_friend) = 0; |
6542 | if (TREE_CODE (decl) == TEMPLATE_DECL) | |
655dc6ee JM |
6543 | { |
6544 | DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0; | |
6545 | DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend)) | |
6546 | = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl)); | |
6547 | } | |
36a117a5 | 6548 | |
92643fea MM |
6549 | /* The mangled name for the NEW_FRIEND is incorrect. The function |
6550 | is not a template instantiation and should not be mangled like | |
6551 | one. Therefore, we forget the mangling here; we'll recompute it | |
6552 | later if we need it. */ | |
36a117a5 MM |
6553 | if (TREE_CODE (new_friend) != TEMPLATE_DECL) |
6554 | { | |
19e7881c | 6555 | SET_DECL_RTL (new_friend, NULL_RTX); |
92643fea | 6556 | SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE); |
36a117a5 | 6557 | } |
c8094d83 | 6558 | |
6eb3bb27 | 6559 | if (DECL_NAMESPACE_SCOPE_P (new_friend)) |
f84b4be9 | 6560 | { |
36a117a5 | 6561 | tree old_decl; |
fbf1c34b MM |
6562 | tree new_friend_template_info; |
6563 | tree new_friend_result_template_info; | |
92da7074 | 6564 | tree ns; |
fbf1c34b MM |
6565 | int new_friend_is_defn; |
6566 | ||
6567 | /* We must save some information from NEW_FRIEND before calling | |
6568 | duplicate decls since that function will free NEW_FRIEND if | |
6569 | possible. */ | |
6570 | new_friend_template_info = DECL_TEMPLATE_INFO (new_friend); | |
5bd61841 | 6571 | new_friend_is_defn = |
c8094d83 | 6572 | (DECL_INITIAL (DECL_TEMPLATE_RESULT |
5bd61841 MM |
6573 | (template_for_substitution (new_friend))) |
6574 | != NULL_TREE); | |
f84b4be9 | 6575 | if (TREE_CODE (new_friend) == TEMPLATE_DECL) |
fbf1c34b MM |
6576 | { |
6577 | /* This declaration is a `primary' template. */ | |
6578 | DECL_PRIMARY_TEMPLATE (new_friend) = new_friend; | |
c8094d83 | 6579 | |
fbf1c34b | 6580 | new_friend_result_template_info |
17aec3eb | 6581 | = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend)); |
fbf1c34b MM |
6582 | } |
6583 | else | |
5bd61841 | 6584 | new_friend_result_template_info = NULL_TREE; |
36a117a5 | 6585 | |
b01e6d2b JM |
6586 | /* Make the init_value nonzero so pushdecl knows this is a defn. */ |
6587 | if (new_friend_is_defn) | |
6588 | DECL_INITIAL (new_friend) = error_mark_node; | |
6589 | ||
1c227897 MM |
6590 | /* Inside pushdecl_namespace_level, we will push into the |
6591 | current namespace. However, the friend function should go | |
c6002625 | 6592 | into the namespace of the template. */ |
92da7074 ML |
6593 | ns = decl_namespace_context (new_friend); |
6594 | push_nested_namespace (ns); | |
d63d5d0c | 6595 | old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true); |
92da7074 | 6596 | pop_nested_namespace (ns); |
36a117a5 | 6597 | |
0014c247 VR |
6598 | if (old_decl == error_mark_node) |
6599 | return error_mark_node; | |
6600 | ||
36a117a5 MM |
6601 | if (old_decl != new_friend) |
6602 | { | |
6603 | /* This new friend declaration matched an existing | |
6604 | declaration. For example, given: | |
6605 | ||
6606 | template <class T> void f(T); | |
c8094d83 MS |
6607 | template <class U> class C { |
6608 | template <class T> friend void f(T) {} | |
36a117a5 MM |
6609 | }; |
6610 | ||
6611 | the friend declaration actually provides the definition | |
6612 | of `f', once C has been instantiated for some type. So, | |
6613 | old_decl will be the out-of-class template declaration, | |
6614 | while new_friend is the in-class definition. | |
6615 | ||
6616 | But, if `f' was called before this point, the | |
6617 | instantiation of `f' will have DECL_TI_ARGS corresponding | |
6618 | to `T' but not to `U', references to which might appear | |
6619 | in the definition of `f'. Previously, the most general | |
6620 | template for an instantiation of `f' was the out-of-class | |
6621 | version; now it is the in-class version. Therefore, we | |
6622 | run through all specialization of `f', adding to their | |
6623 | DECL_TI_ARGS appropriately. In particular, they need a | |
6624 | new set of outer arguments, corresponding to the | |
c8094d83 | 6625 | arguments for this class instantiation. |
36a117a5 MM |
6626 | |
6627 | The same situation can arise with something like this: | |
6628 | ||
6629 | friend void f(int); | |
c8094d83 | 6630 | template <class T> class C { |
0cbd7506 MS |
6631 | friend void f(T) {} |
6632 | }; | |
36a117a5 MM |
6633 | |
6634 | when `C<int>' is instantiated. Now, `f(int)' is defined | |
6635 | in the class. */ | |
6636 | ||
fbf1c34b MM |
6637 | if (!new_friend_is_defn) |
6638 | /* On the other hand, if the in-class declaration does | |
6639 | *not* provide a definition, then we don't want to alter | |
6640 | existing definitions. We can just leave everything | |
6641 | alone. */ | |
36a117a5 | 6642 | ; |
fbf1c34b | 6643 | else |
36a117a5 | 6644 | { |
fbf1c34b MM |
6645 | /* Overwrite whatever template info was there before, if |
6646 | any, with the new template information pertaining to | |
6647 | the declaration. */ | |
6648 | DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info; | |
6649 | ||
6650 | if (TREE_CODE (old_decl) != TEMPLATE_DECL) | |
8d83f792 MM |
6651 | reregister_specialization (new_friend, |
6652 | most_general_template (old_decl), | |
6653 | old_decl); | |
c8094d83 | 6654 | else |
36a117a5 | 6655 | { |
fbf1c34b MM |
6656 | tree t; |
6657 | tree new_friend_args; | |
6658 | ||
c8094d83 | 6659 | DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl)) |
fbf1c34b | 6660 | = new_friend_result_template_info; |
c8094d83 | 6661 | |
fbf1c34b | 6662 | new_friend_args = TI_ARGS (new_friend_template_info); |
c8094d83 | 6663 | for (t = DECL_TEMPLATE_SPECIALIZATIONS (old_decl); |
fbf1c34b MM |
6664 | t != NULL_TREE; |
6665 | t = TREE_CHAIN (t)) | |
6666 | { | |
6667 | tree spec = TREE_VALUE (t); | |
c8094d83 MS |
6668 | |
6669 | DECL_TI_ARGS (spec) | |
fbf1c34b MM |
6670 | = add_outermost_template_args (new_friend_args, |
6671 | DECL_TI_ARGS (spec)); | |
fbf1c34b MM |
6672 | } |
6673 | ||
6674 | /* Now, since specializations are always supposed to | |
6675 | hang off of the most general template, we must move | |
6676 | them. */ | |
6677 | t = most_general_template (old_decl); | |
6678 | if (t != old_decl) | |
6679 | { | |
6680 | DECL_TEMPLATE_SPECIALIZATIONS (t) | |
6681 | = chainon (DECL_TEMPLATE_SPECIALIZATIONS (t), | |
6682 | DECL_TEMPLATE_SPECIALIZATIONS (old_decl)); | |
6683 | DECL_TEMPLATE_SPECIALIZATIONS (old_decl) = NULL_TREE; | |
6684 | } | |
36a117a5 MM |
6685 | } |
6686 | } | |
6687 | ||
6688 | /* The information from NEW_FRIEND has been merged into OLD_DECL | |
6689 | by duplicate_decls. */ | |
6690 | new_friend = old_decl; | |
6691 | } | |
f84b4be9 | 6692 | } |
6e432b31 | 6693 | else |
f84b4be9 | 6694 | { |
6e432b31 KL |
6695 | tree context = DECL_CONTEXT (new_friend); |
6696 | bool dependent_p; | |
6697 | ||
6698 | /* In the code | |
6699 | template <class T> class C { | |
6700 | template <class U> friend void C1<U>::f (); // case 1 | |
6701 | friend void C2<T>::f (); // case 2 | |
6702 | }; | |
6703 | we only need to make sure CONTEXT is a complete type for | |
6704 | case 2. To distinguish between the two cases, we note that | |
6705 | CONTEXT of case 1 remains dependent type after tsubst while | |
6706 | this isn't true for case 2. */ | |
6707 | ++processing_template_decl; | |
6708 | dependent_p = dependent_type_p (context); | |
6709 | --processing_template_decl; | |
6710 | ||
6711 | if (!dependent_p | |
6712 | && !complete_type_or_else (context, NULL_TREE)) | |
6713 | return error_mark_node; | |
6714 | ||
6715 | if (COMPLETE_TYPE_P (context)) | |
6716 | { | |
6717 | /* Check to see that the declaration is really present, and, | |
6718 | possibly obtain an improved declaration. */ | |
6719 | tree fn = check_classfn (context, | |
6720 | new_friend, NULL_TREE); | |
6721 | ||
684939ce | 6722 | if (fn) |
6e432b31 KL |
6723 | new_friend = fn; |
6724 | } | |
f84b4be9 JM |
6725 | } |
6726 | ||
6727 | return new_friend; | |
6728 | } | |
6729 | ||
1aed5355 MM |
6730 | /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of |
6731 | template arguments, as for tsubst. | |
6757edfe | 6732 | |
dc957d14 | 6733 | Returns an appropriate tsubst'd friend type or error_mark_node on |
4b054b80 | 6734 | failure. */ |
6757edfe MM |
6735 | |
6736 | static tree | |
3a978d72 | 6737 | tsubst_friend_class (tree friend_tmpl, tree args) |
6757edfe | 6738 | { |
1aed5355 | 6739 | tree friend_type; |
25aab5d0 | 6740 | tree tmpl; |
3e0ec82f | 6741 | tree context; |
6757edfe | 6742 | |
3e0ec82f MM |
6743 | context = DECL_CONTEXT (friend_tmpl); |
6744 | ||
6745 | if (context) | |
77adef84 | 6746 | { |
3e0ec82f MM |
6747 | if (TREE_CODE (context) == NAMESPACE_DECL) |
6748 | push_nested_namespace (context); | |
6749 | else | |
c8094d83 | 6750 | push_nested_class (tsubst (context, args, tf_none, NULL_TREE)); |
3e0ec82f | 6751 | } |
25aab5d0 | 6752 | |
105d72c5 MM |
6753 | /* Look for a class template declaration. We look for hidden names |
6754 | because two friend declarations of the same template are the | |
6755 | same. For example, in: | |
6756 | ||
6757 | struct A { | |
6758 | template <typename> friend class F; | |
6759 | }; | |
6760 | template <typename> struct B { | |
6761 | template <typename> friend class F; | |
6762 | }; | |
6763 | ||
6764 | both F templates are the same. */ | |
6765 | tmpl = lookup_name_real (DECL_NAME (friend_tmpl), 0, 0, | |
6766 | /*block_p=*/true, 0, | |
6767 | LOOKUP_COMPLAIN | LOOKUP_HIDDEN); | |
25aab5d0 | 6768 | |
3e0ec82f MM |
6769 | /* But, if we don't find one, it might be because we're in a |
6770 | situation like this: | |
77adef84 | 6771 | |
3e0ec82f MM |
6772 | template <class T> |
6773 | struct S { | |
6774 | template <class U> | |
6775 | friend struct S; | |
6776 | }; | |
6777 | ||
6778 | Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL | |
6779 | for `S<int>', not the TEMPLATE_DECL. */ | |
6780 | if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl)) | |
6781 | { | |
10e6657a | 6782 | tmpl = lookup_name_prefer_type (DECL_NAME (friend_tmpl), 1); |
3e0ec82f | 6783 | tmpl = maybe_get_template_decl_from_type_decl (tmpl); |
25aab5d0 | 6784 | } |
6757edfe | 6785 | |
25aab5d0 | 6786 | if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl)) |
6757edfe MM |
6787 | { |
6788 | /* The friend template has already been declared. Just | |
36a117a5 MM |
6789 | check to see that the declarations match, and install any new |
6790 | default parameters. We must tsubst the default parameters, | |
6791 | of course. We only need the innermost template parameters | |
6792 | because that is all that redeclare_class_template will look | |
6793 | at. */ | |
3e0ec82f MM |
6794 | if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl)) |
6795 | > TMPL_ARGS_DEPTH (args)) | |
6796 | { | |
6797 | tree parms; | |
b131ad7c | 6798 | location_t saved_input_location; |
3e0ec82f | 6799 | parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl), |
23fca1f5 | 6800 | args, tf_warning_or_error); |
b131ad7c MLI |
6801 | |
6802 | saved_input_location = input_location; | |
6803 | input_location = DECL_SOURCE_LOCATION (friend_tmpl); | |
3e0ec82f | 6804 | redeclare_class_template (TREE_TYPE (tmpl), parms); |
b131ad7c MLI |
6805 | input_location = saved_input_location; |
6806 | ||
3e0ec82f MM |
6807 | } |
6808 | ||
6757edfe MM |
6809 | friend_type = TREE_TYPE (tmpl); |
6810 | } | |
6811 | else | |
6812 | { | |
6813 | /* The friend template has not already been declared. In this | |
6814 | case, the instantiation of the template class will cause the | |
6815 | injection of this template into the global scope. */ | |
23fca1f5 | 6816 | tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE); |
dca56f77 VR |
6817 | if (tmpl == error_mark_node) |
6818 | return error_mark_node; | |
6757edfe MM |
6819 | |
6820 | /* The new TMPL is not an instantiation of anything, so we | |
0cbd7506 | 6821 | forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for |
6757edfe MM |
6822 | the new type because that is supposed to be the corresponding |
6823 | template decl, i.e., TMPL. */ | |
6824 | DECL_USE_TEMPLATE (tmpl) = 0; | |
6825 | DECL_TEMPLATE_INFO (tmpl) = NULL_TREE; | |
6826 | CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0; | |
68c78847 KL |
6827 | CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)) |
6828 | = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))); | |
6757edfe MM |
6829 | |
6830 | /* Inject this template into the global scope. */ | |
d63d5d0c | 6831 | friend_type = TREE_TYPE (pushdecl_top_level_maybe_friend (tmpl, true)); |
6757edfe MM |
6832 | } |
6833 | ||
c8094d83 | 6834 | if (context) |
3e0ec82f MM |
6835 | { |
6836 | if (TREE_CODE (context) == NAMESPACE_DECL) | |
6837 | pop_nested_namespace (context); | |
6838 | else | |
6839 | pop_nested_class (); | |
6840 | } | |
6841 | ||
6757edfe MM |
6842 | return friend_type; |
6843 | } | |
f84b4be9 | 6844 | |
17f29637 KL |
6845 | /* Returns zero if TYPE cannot be completed later due to circularity. |
6846 | Otherwise returns one. */ | |
6847 | ||
d5372501 | 6848 | static int |
3a978d72 | 6849 | can_complete_type_without_circularity (tree type) |
17f29637 KL |
6850 | { |
6851 | if (type == NULL_TREE || type == error_mark_node) | |
6852 | return 0; | |
6853 | else if (COMPLETE_TYPE_P (type)) | |
6854 | return 1; | |
6855 | else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type)) | |
6856 | return can_complete_type_without_circularity (TREE_TYPE (type)); | |
8c6ab2db NS |
6857 | else if (CLASS_TYPE_P (type) |
6858 | && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type))) | |
17f29637 KL |
6859 | return 0; |
6860 | else | |
6861 | return 1; | |
6862 | } | |
6863 | ||
4f4141ff JM |
6864 | /* Apply any attributes which had to be deferred until instantiation |
6865 | time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes; | |
6866 | ARGS, COMPLAIN, IN_DECL are as tsubst. */ | |
6867 | ||
6868 | static void | |
6869 | apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags, | |
6870 | tree args, tsubst_flags_t complain, tree in_decl) | |
6871 | { | |
5818c8e4 | 6872 | tree last_dep = NULL_TREE; |
4f4141ff | 6873 | tree t; |
5818c8e4 JM |
6874 | tree *p; |
6875 | ||
6876 | for (t = attributes; t; t = TREE_CHAIN (t)) | |
6877 | if (ATTR_IS_DEPENDENT (t)) | |
6878 | { | |
6879 | last_dep = t; | |
6880 | attributes = copy_list (attributes); | |
6881 | break; | |
6882 | } | |
4f4141ff JM |
6883 | |
6884 | if (DECL_P (*decl_p)) | |
823e5f7f JJ |
6885 | { |
6886 | if (TREE_TYPE (*decl_p) == error_mark_node) | |
6887 | return; | |
6888 | p = &DECL_ATTRIBUTES (*decl_p); | |
6889 | } | |
4f4141ff | 6890 | else |
5818c8e4 | 6891 | p = &TYPE_ATTRIBUTES (*decl_p); |
4f4141ff | 6892 | |
5818c8e4 JM |
6893 | if (last_dep) |
6894 | { | |
6895 | tree late_attrs = NULL_TREE; | |
6896 | tree *q = &late_attrs; | |
6897 | ||
6898 | for (*p = attributes; *p; ) | |
6899 | { | |
6900 | t = *p; | |
6901 | if (ATTR_IS_DEPENDENT (t)) | |
6902 | { | |
6903 | *p = TREE_CHAIN (t); | |
6904 | TREE_CHAIN (t) = NULL_TREE; | |
ff2f1c5f JJ |
6905 | /* If the first attribute argument is an identifier, don't |
6906 | pass it through tsubst. Attributes like mode, format, | |
6907 | cleanup and several target specific attributes expect it | |
6908 | unmodified. */ | |
6909 | if (TREE_VALUE (t) | |
6910 | && TREE_CODE (TREE_VALUE (t)) == TREE_LIST | |
6911 | && TREE_VALUE (TREE_VALUE (t)) | |
6912 | && (TREE_CODE (TREE_VALUE (TREE_VALUE (t))) | |
6913 | == IDENTIFIER_NODE)) | |
6914 | { | |
6915 | tree chain | |
6916 | = tsubst_expr (TREE_CHAIN (TREE_VALUE (t)), args, complain, | |
6917 | in_decl, | |
6918 | /*integral_constant_expression_p=*/false); | |
6919 | if (chain != TREE_CHAIN (TREE_VALUE (t))) | |
6920 | TREE_VALUE (t) | |
6921 | = tree_cons (NULL_TREE, TREE_VALUE (TREE_VALUE (t)), | |
6922 | chain); | |
6923 | } | |
6924 | else | |
6925 | TREE_VALUE (t) | |
6926 | = tsubst_expr (TREE_VALUE (t), args, complain, in_decl, | |
6927 | /*integral_constant_expression_p=*/false); | |
5818c8e4 JM |
6928 | *q = t; |
6929 | q = &TREE_CHAIN (t); | |
6930 | } | |
6931 | else | |
6932 | p = &TREE_CHAIN (t); | |
6933 | } | |
4f4141ff | 6934 | |
5818c8e4 JM |
6935 | cplus_decl_attributes (decl_p, late_attrs, attr_flags); |
6936 | } | |
4f4141ff JM |
6937 | } |
6938 | ||
d0940d56 DS |
6939 | /* Perform (or defer) access check for typedefs that were referenced |
6940 | from within the template TMPL code. | |
6941 | This is a subroutine of instantiate_template and instantiate_class_template. | |
6942 | TMPL is the template to consider and TARGS is the list of arguments of | |
6943 | that template. */ | |
6944 | ||
6945 | static void | |
6946 | perform_typedefs_access_check (tree tmpl, tree targs) | |
6947 | { | |
6948 | tree t; | |
6949 | ||
2eb25c98 DS |
6950 | if (!tmpl |
6951 | || (TREE_CODE (tmpl) != RECORD_TYPE | |
6952 | && TREE_CODE (tmpl) != FUNCTION_DECL)) | |
d0940d56 DS |
6953 | return; |
6954 | ||
2eb25c98 | 6955 | for (t = get_types_needing_access_check (tmpl); t; t = TREE_CHAIN (t)) |
d0940d56 DS |
6956 | { |
6957 | tree type_decl = TREE_PURPOSE (t); | |
6958 | tree type_scope = TREE_VALUE (t); | |
6959 | ||
6960 | if (!type_decl || !type_scope || !CLASS_TYPE_P (type_scope)) | |
6961 | continue; | |
6962 | ||
6963 | if (uses_template_parms (type_decl)) | |
6964 | type_decl = tsubst (type_decl, targs, tf_error, NULL_TREE); | |
6965 | if (uses_template_parms (type_scope)) | |
6966 | type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE); | |
6967 | ||
2eb25c98 DS |
6968 | perform_or_defer_access_check (TYPE_BINFO (type_scope), |
6969 | type_decl, type_decl); | |
d0940d56 DS |
6970 | } |
6971 | } | |
6972 | ||
8d08fdba | 6973 | tree |
3a978d72 | 6974 | instantiate_class_template (tree type) |
8d08fdba | 6975 | { |
be93747e | 6976 | tree templ, args, pattern, t, member; |
36a117a5 | 6977 | tree typedecl; |
dbbf88d1 | 6978 | tree pbinfo; |
cad7e87b | 6979 | tree base_list; |
c8094d83 | 6980 | |
5566b478 | 6981 | if (type == error_mark_node) |
8d08fdba MS |
6982 | return error_mark_node; |
6983 | ||
c8094d83 | 6984 | if (TYPE_BEING_DEFINED (type) |
ca099ac8 MM |
6985 | || COMPLETE_TYPE_P (type) |
6986 | || dependent_type_p (type)) | |
5566b478 MS |
6987 | return type; |
6988 | ||
6bdb985f | 6989 | /* Figure out which template is being instantiated. */ |
be93747e KG |
6990 | templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type)); |
6991 | gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL); | |
73aad9b9 | 6992 | |
4c571114 MM |
6993 | /* Determine what specialization of the original template to |
6994 | instantiate. */ | |
be93747e | 6995 | t = most_specialized_class (type, templ); |
8fbc5ae7 | 6996 | if (t == error_mark_node) |
73aad9b9 | 6997 | { |
8fbc5ae7 MM |
6998 | TYPE_BEING_DEFINED (type) = 1; |
6999 | return error_mark_node; | |
73aad9b9 | 7000 | } |
916b63c3 MM |
7001 | else if (t) |
7002 | { | |
7003 | /* This TYPE is actually an instantiation of a partial | |
7004 | specialization. We replace the innermost set of ARGS with | |
7005 | the arguments appropriate for substitution. For example, | |
7006 | given: | |
3db45ab5 MS |
7007 | |
7008 | template <class T> struct S {}; | |
916b63c3 | 7009 | template <class T> struct S<T*> {}; |
6bdb985f | 7010 | |
916b63c3 | 7011 | and supposing that we are instantiating S<int*>, ARGS will |
3db45ab5 | 7012 | presently be {int*} -- but we need {int}. */ |
916b63c3 MM |
7013 | pattern = TREE_TYPE (t); |
7014 | args = TREE_PURPOSE (t); | |
7015 | } | |
73aad9b9 | 7016 | else |
916b63c3 | 7017 | { |
be93747e | 7018 | pattern = TREE_TYPE (templ); |
916b63c3 MM |
7019 | args = CLASSTYPE_TI_ARGS (type); |
7020 | } | |
5566b478 | 7021 | |
4c571114 MM |
7022 | /* If the template we're instantiating is incomplete, then clearly |
7023 | there's nothing we can do. */ | |
d0f062fb | 7024 | if (!COMPLETE_TYPE_P (pattern)) |
f31c0a32 | 7025 | return type; |
5566b478 | 7026 | |
4c571114 MM |
7027 | /* If we've recursively instantiated too many templates, stop. */ |
7028 | if (! push_tinst_level (type)) | |
f31c0a32 | 7029 | return type; |
4c571114 MM |
7030 | |
7031 | /* Now we're really doing the instantiation. Mark the type as in | |
7032 | the process of being defined. */ | |
7033 | TYPE_BEING_DEFINED (type) = 1; | |
7034 | ||
78757caa KL |
7035 | /* We may be in the middle of deferred access check. Disable |
7036 | it now. */ | |
7037 | push_deferring_access_checks (dk_no_deferred); | |
7038 | ||
c353b8e3 | 7039 | push_to_top_level (); |
4c571114 | 7040 | |
7813d14c | 7041 | SET_CLASSTYPE_INTERFACE_UNKNOWN (type); |
8d08fdba | 7042 | |
2eb25c98 DS |
7043 | /* Set the input location to the most specialized template definition. |
7044 | This is needed if tsubsting causes an error. */ | |
7045 | typedecl = TYPE_MAIN_DECL (pattern); | |
12af7ba3 | 7046 | input_location = DECL_SOURCE_LOCATION (typedecl); |
68ea098a | 7047 | |
0fcedd9c | 7048 | TYPE_HAS_USER_CONSTRUCTOR (type) = TYPE_HAS_USER_CONSTRUCTOR (pattern); |
834c6dff MM |
7049 | TYPE_HAS_NEW_OPERATOR (type) = TYPE_HAS_NEW_OPERATOR (pattern); |
7050 | TYPE_HAS_ARRAY_NEW_OPERATOR (type) = TYPE_HAS_ARRAY_NEW_OPERATOR (pattern); | |
f7da6097 | 7051 | TYPE_GETS_DELETE (type) = TYPE_GETS_DELETE (pattern); |
f7da6097 MS |
7052 | TYPE_HAS_ASSIGN_REF (type) = TYPE_HAS_ASSIGN_REF (pattern); |
7053 | TYPE_HAS_CONST_ASSIGN_REF (type) = TYPE_HAS_CONST_ASSIGN_REF (pattern); | |
f7da6097 MS |
7054 | TYPE_HAS_INIT_REF (type) = TYPE_HAS_INIT_REF (pattern); |
7055 | TYPE_HAS_CONST_INIT_REF (type) = TYPE_HAS_CONST_INIT_REF (pattern); | |
f7da6097 MS |
7056 | TYPE_HAS_DEFAULT_CONSTRUCTOR (type) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern); |
7057 | TYPE_HAS_CONVERSION (type) = TYPE_HAS_CONVERSION (pattern); | |
f7da6097 MS |
7058 | TYPE_PACKED (type) = TYPE_PACKED (pattern); |
7059 | TYPE_ALIGN (type) = TYPE_ALIGN (pattern); | |
11cf4d18 | 7060 | TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern); |
eff71ab0 | 7061 | TYPE_FOR_JAVA (type) = TYPE_FOR_JAVA (pattern); /* For libjava's JArray<T> */ |
6bdb8141 JM |
7062 | if (ANON_AGGR_TYPE_P (pattern)) |
7063 | SET_ANON_AGGR_TYPE_P (type); | |
8d039470 MS |
7064 | if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern)) |
7065 | { | |
7066 | CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1; | |
7067 | CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern); | |
7068 | } | |
f7da6097 | 7069 | |
dbbf88d1 | 7070 | pbinfo = TYPE_BINFO (pattern); |
1456deaf | 7071 | |
315fb5db NS |
7072 | /* We should never instantiate a nested class before its enclosing |
7073 | class; we need to look up the nested class by name before we can | |
7074 | instantiate it, and that lookup should instantiate the enclosing | |
7075 | class. */ | |
7076 | gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern)) | |
7077 | || COMPLETE_TYPE_P (TYPE_CONTEXT (type)) | |
7078 | || TYPE_BEING_DEFINED (TYPE_CONTEXT (type))); | |
1456deaf | 7079 | |
cad7e87b | 7080 | base_list = NULL_TREE; |
fa743e8c | 7081 | if (BINFO_N_BASE_BINFOS (pbinfo)) |
3fd71a52 | 7082 | { |
fa743e8c | 7083 | tree pbase_binfo; |
a2507277 | 7084 | tree context = TYPE_CONTEXT (type); |
4514aa8c | 7085 | tree pushed_scope; |
3fd71a52 | 7086 | int i; |
5566b478 | 7087 | |
a2507277 NS |
7088 | /* We must enter the scope containing the type, as that is where |
7089 | the accessibility of types named in dependent bases are | |
7090 | looked up from. */ | |
4514aa8c | 7091 | pushed_scope = push_scope (context ? context : global_namespace); |
c8094d83 | 7092 | |
3fd71a52 MM |
7093 | /* Substitute into each of the bases to determine the actual |
7094 | basetypes. */ | |
fa743e8c | 7095 | for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++) |
3fd71a52 MM |
7096 | { |
7097 | tree base; | |
fa743e8c | 7098 | tree access = BINFO_BASE_ACCESS (pbinfo, i); |
5d80a306 DG |
7099 | tree expanded_bases = NULL_TREE; |
7100 | int idx, len = 1; | |
7101 | ||
7102 | if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo))) | |
7103 | { | |
7104 | expanded_bases = | |
7105 | tsubst_pack_expansion (BINFO_TYPE (pbase_binfo), | |
7106 | args, tf_error, NULL_TREE); | |
7107 | if (expanded_bases == error_mark_node) | |
7108 | continue; | |
7109 | ||
7110 | len = TREE_VEC_LENGTH (expanded_bases); | |
7111 | } | |
7112 | ||
7113 | for (idx = 0; idx < len; idx++) | |
7114 | { | |
7115 | if (expanded_bases) | |
7116 | /* Extract the already-expanded base class. */ | |
7117 | base = TREE_VEC_ELT (expanded_bases, idx); | |
7118 | else | |
7119 | /* Substitute to figure out the base class. */ | |
7120 | base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error, | |
7121 | NULL_TREE); | |
7122 | ||
7123 | if (base == error_mark_node) | |
7124 | continue; | |
7125 | ||
7126 | base_list = tree_cons (access, base, base_list); | |
7127 | if (BINFO_VIRTUAL_P (pbase_binfo)) | |
7128 | TREE_TYPE (base_list) = integer_type_node; | |
7129 | } | |
3fd71a52 | 7130 | } |
dfbcd65a | 7131 | |
3fd71a52 MM |
7132 | /* The list is now in reverse order; correct that. */ |
7133 | base_list = nreverse (base_list); | |
7134 | ||
4514aa8c NS |
7135 | if (pushed_scope) |
7136 | pop_scope (pushed_scope); | |
3fd71a52 | 7137 | } |
cad7e87b NS |
7138 | /* Now call xref_basetypes to set up all the base-class |
7139 | information. */ | |
7140 | xref_basetypes (type, base_list); | |
7141 | ||
4f4141ff JM |
7142 | apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern), |
7143 | (int) ATTR_FLAG_TYPE_IN_PLACE, | |
7144 | args, tf_error, NULL_TREE); | |
5566b478 | 7145 | |
b74a0560 MM |
7146 | /* Now that our base classes are set up, enter the scope of the |
7147 | class, so that name lookups into base classes, etc. will work | |
dc957d14 | 7148 | correctly. This is precisely analogous to what we do in |
b74a0560 | 7149 | begin_class_definition when defining an ordinary non-template |
56d0c6e3 JM |
7150 | class, except we also need to push the enclosing classes. */ |
7151 | push_nested_class (type); | |
b74a0560 | 7152 | |
7088fca9 | 7153 | /* Now members are processed in the order of declaration. */ |
8c6ab2db NS |
7154 | for (member = CLASSTYPE_DECL_LIST (pattern); |
7155 | member; member = TREE_CHAIN (member)) | |
8d08fdba | 7156 | { |
7088fca9 | 7157 | tree t = TREE_VALUE (member); |
5566b478 | 7158 | |
7088fca9 | 7159 | if (TREE_PURPOSE (member)) |
ed44da02 | 7160 | { |
7088fca9 KL |
7161 | if (TYPE_P (t)) |
7162 | { | |
5e0c54e5 | 7163 | /* Build new CLASSTYPE_NESTED_UTDS. */ |
8d08fdba | 7164 | |
7088fca9 | 7165 | tree newtag; |
883a2bff MM |
7166 | bool class_template_p; |
7167 | ||
2678bae8 VR |
7168 | class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE |
7169 | && TYPE_LANG_SPECIFIC (t) | |
7170 | && CLASSTYPE_IS_TEMPLATE (t)); | |
883a2bff | 7171 | /* If the member is a class template, then -- even after |
6c745393 | 7172 | substitution -- there may be dependent types in the |
883a2bff MM |
7173 | template argument list for the class. We increment |
7174 | PROCESSING_TEMPLATE_DECL so that dependent_type_p, as | |
7175 | that function will assume that no types are dependent | |
7176 | when outside of a template. */ | |
7177 | if (class_template_p) | |
7178 | ++processing_template_decl; | |
2678bae8 | 7179 | newtag = tsubst (t, args, tf_error, NULL_TREE); |
883a2bff MM |
7180 | if (class_template_p) |
7181 | --processing_template_decl; | |
2620d095 KL |
7182 | if (newtag == error_mark_node) |
7183 | continue; | |
7184 | ||
7088fca9 KL |
7185 | if (TREE_CODE (newtag) != ENUMERAL_TYPE) |
7186 | { | |
2678bae8 VR |
7187 | tree name = TYPE_IDENTIFIER (t); |
7188 | ||
883a2bff | 7189 | if (class_template_p) |
7088fca9 KL |
7190 | /* Unfortunately, lookup_template_class sets |
7191 | CLASSTYPE_IMPLICIT_INSTANTIATION for a partial | |
3afd2e20 NS |
7192 | instantiation (i.e., for the type of a member |
7193 | template class nested within a template class.) | |
7194 | This behavior is required for | |
7195 | maybe_process_partial_specialization to work | |
7196 | correctly, but is not accurate in this case; | |
7197 | the TAG is not an instantiation of anything. | |
7198 | (The corresponding TEMPLATE_DECL is an | |
7199 | instantiation, but the TYPE is not.) */ | |
7088fca9 KL |
7200 | CLASSTYPE_USE_TEMPLATE (newtag) = 0; |
7201 | ||
7202 | /* Now, we call pushtag to put this NEWTAG into the scope of | |
7203 | TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid | |
7204 | pushtag calling push_template_decl. We don't have to do | |
7205 | this for enums because it will already have been done in | |
7206 | tsubst_enum. */ | |
7207 | if (name) | |
7208 | SET_IDENTIFIER_TYPE_VALUE (name, newtag); | |
bd3d082e | 7209 | pushtag (name, newtag, /*tag_scope=*/ts_current); |
7088fca9 KL |
7210 | } |
7211 | } | |
c8094d83 | 7212 | else if (TREE_CODE (t) == FUNCTION_DECL |
7088fca9 KL |
7213 | || DECL_FUNCTION_TEMPLATE_P (t)) |
7214 | { | |
7215 | /* Build new TYPE_METHODS. */ | |
fecafe5e | 7216 | tree r; |
c8094d83 | 7217 | |
fecafe5e | 7218 | if (TREE_CODE (t) == TEMPLATE_DECL) |
9579624e | 7219 | ++processing_template_decl; |
fecafe5e NS |
7220 | r = tsubst (t, args, tf_error, NULL_TREE); |
7221 | if (TREE_CODE (t) == TEMPLATE_DECL) | |
9579624e | 7222 | --processing_template_decl; |
7088fca9 | 7223 | set_current_access_from_decl (r); |
7088fca9 KL |
7224 | finish_member_declaration (r); |
7225 | } | |
7226 | else | |
7227 | { | |
7228 | /* Build new TYPE_FIELDS. */ | |
55a3debe DG |
7229 | if (TREE_CODE (t) == STATIC_ASSERT) |
7230 | { | |
7231 | tree condition = | |
7232 | tsubst_expr (STATIC_ASSERT_CONDITION (t), args, | |
7233 | tf_warning_or_error, NULL_TREE, | |
7234 | /*integral_constant_expression_p=*/true); | |
7235 | finish_static_assert (condition, | |
7236 | STATIC_ASSERT_MESSAGE (t), | |
7237 | STATIC_ASSERT_SOURCE_LOCATION (t), | |
7238 | /*member_p=*/true); | |
7239 | } | |
7240 | else if (TREE_CODE (t) != CONST_DECL) | |
7088fca9 KL |
7241 | { |
7242 | tree r; | |
fa8d6e85 | 7243 | |
39a13be5 | 7244 | /* The file and line for this declaration, to |
d479d37f NS |
7245 | assist in error message reporting. Since we |
7246 | called push_tinst_level above, we don't need to | |
7247 | restore these. */ | |
f31686a3 | 7248 | input_location = DECL_SOURCE_LOCATION (t); |
fa8d6e85 | 7249 | |
fb5ce3c9 | 7250 | if (TREE_CODE (t) == TEMPLATE_DECL) |
9579624e | 7251 | ++processing_template_decl; |
23fca1f5 | 7252 | r = tsubst (t, args, tf_warning_or_error, NULL_TREE); |
fb5ce3c9 | 7253 | if (TREE_CODE (t) == TEMPLATE_DECL) |
9579624e | 7254 | --processing_template_decl; |
7088fca9 KL |
7255 | if (TREE_CODE (r) == VAR_DECL) |
7256 | { | |
b794e321 MM |
7257 | /* In [temp.inst]: |
7258 | ||
7259 | [t]he initialization (and any associated | |
7260 | side-effects) of a static data member does | |
7261 | not occur unless the static data member is | |
7262 | itself used in a way that requires the | |
7263 | definition of the static data member to | |
3db45ab5 | 7264 | exist. |
b794e321 MM |
7265 | |
7266 | Therefore, we do not substitute into the | |
3db45ab5 MS |
7267 | initialized for the static data member here. */ |
7268 | finish_static_data_member_decl | |
7269 | (r, | |
7270 | /*init=*/NULL_TREE, | |
d174af6c | 7271 | /*init_const_expr_p=*/false, |
3db45ab5 | 7272 | /*asmspec_tree=*/NULL_TREE, |
b794e321 | 7273 | /*flags=*/0); |
7088fca9 KL |
7274 | if (DECL_INITIALIZED_IN_CLASS_P (r)) |
7275 | check_static_variable_definition (r, TREE_TYPE (r)); | |
7276 | } | |
7277 | else if (TREE_CODE (r) == FIELD_DECL) | |
7278 | { | |
7279 | /* Determine whether R has a valid type and can be | |
7280 | completed later. If R is invalid, then it is | |
7281 | replaced by error_mark_node so that it will not be | |
7282 | added to TYPE_FIELDS. */ | |
7283 | tree rtype = TREE_TYPE (r); | |
7284 | if (can_complete_type_without_circularity (rtype)) | |
7285 | complete_type (rtype); | |
7286 | ||
7287 | if (!COMPLETE_TYPE_P (rtype)) | |
7288 | { | |
7289 | cxx_incomplete_type_error (r, rtype); | |
0cbd7506 | 7290 | r = error_mark_node; |
7088fca9 KL |
7291 | } |
7292 | } | |
5566b478 | 7293 | |
7088fca9 KL |
7294 | /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE, |
7295 | such a thing will already have been added to the field | |
7296 | list by tsubst_enum in finish_member_declaration in the | |
5e0c54e5 | 7297 | CLASSTYPE_NESTED_UTDS case above. */ |
7088fca9 KL |
7298 | if (!(TREE_CODE (r) == TYPE_DECL |
7299 | && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE | |
43b26a01 | 7300 | && DECL_ARTIFICIAL (r))) |
7088fca9 KL |
7301 | { |
7302 | set_current_access_from_decl (r); | |
7303 | finish_member_declaration (r); | |
7304 | } | |
0cbd7506 | 7305 | } |
7088fca9 | 7306 | } |
61fc8c9e | 7307 | } |
7088fca9 KL |
7308 | else |
7309 | { | |
7310 | if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)) | |
7311 | { | |
7312 | /* Build new CLASSTYPE_FRIEND_CLASSES. */ | |
7313 | ||
7314 | tree friend_type = t; | |
b939a023 | 7315 | bool adjust_processing_template_decl = false; |
1aed5355 | 7316 | |
7088fca9 | 7317 | if (TREE_CODE (friend_type) == TEMPLATE_DECL) |
b939a023 | 7318 | { |
5a24482e | 7319 | /* template <class T> friend class C; */ |
b939a023 | 7320 | friend_type = tsubst_friend_class (friend_type, args); |
0cbd7506 | 7321 | adjust_processing_template_decl = true; |
b939a023 KL |
7322 | } |
7323 | else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE) | |
7324 | { | |
5a24482e | 7325 | /* template <class T> friend class C::D; */ |
b939a023 | 7326 | friend_type = tsubst (friend_type, args, |
23fca1f5 | 7327 | tf_warning_or_error, NULL_TREE); |
b939a023 KL |
7328 | if (TREE_CODE (friend_type) == TEMPLATE_DECL) |
7329 | friend_type = TREE_TYPE (friend_type); | |
0cbd7506 | 7330 | adjust_processing_template_decl = true; |
b939a023 KL |
7331 | } |
7332 | else if (TREE_CODE (friend_type) == TYPENAME_TYPE) | |
7333 | { | |
5a24482e KL |
7334 | /* This could be either |
7335 | ||
7336 | friend class T::C; | |
7337 | ||
7338 | when dependent_type_p is false or | |
7339 | ||
7340 | template <class U> friend class T::C; | |
7341 | ||
7342 | otherwise. */ | |
b939a023 | 7343 | friend_type = tsubst (friend_type, args, |
23fca1f5 | 7344 | tf_warning_or_error, NULL_TREE); |
b939a023 KL |
7345 | /* Bump processing_template_decl for correct |
7346 | dependent_type_p calculation. */ | |
7347 | ++processing_template_decl; | |
7348 | if (dependent_type_p (friend_type)) | |
7349 | adjust_processing_template_decl = true; | |
7350 | --processing_template_decl; | |
7351 | } | |
5a24482e KL |
7352 | else if (!CLASSTYPE_USE_TEMPLATE (friend_type) |
7353 | && hidden_name_p (TYPE_NAME (friend_type))) | |
7088fca9 | 7354 | { |
5a24482e KL |
7355 | /* friend class C; |
7356 | ||
7357 | where C hasn't been declared yet. Let's lookup name | |
7358 | from namespace scope directly, bypassing any name that | |
7359 | come from dependent base class. */ | |
7088fca9 KL |
7360 | tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type)); |
7361 | ||
7362 | /* The call to xref_tag_from_type does injection for friend | |
7363 | classes. */ | |
7364 | push_nested_namespace (ns); | |
c8094d83 MS |
7365 | friend_type = |
7366 | xref_tag_from_type (friend_type, NULL_TREE, | |
5a24482e | 7367 | /*tag_scope=*/ts_current); |
7088fca9 KL |
7368 | pop_nested_namespace (ns); |
7369 | } | |
5a24482e KL |
7370 | else if (uses_template_parms (friend_type)) |
7371 | /* friend class C<T>; */ | |
7372 | friend_type = tsubst (friend_type, args, | |
23fca1f5 | 7373 | tf_warning_or_error, NULL_TREE); |
5a24482e KL |
7374 | /* Otherwise it's |
7375 | ||
7376 | friend class C; | |
7377 | ||
7378 | where C is already declared or | |
7379 | ||
7380 | friend class C<int>; | |
7381 | ||
0cbd7506 | 7382 | We don't have to do anything in these cases. */ |
1aed5355 | 7383 | |
b939a023 | 7384 | if (adjust_processing_template_decl) |
7088fca9 KL |
7385 | /* Trick make_friend_class into realizing that the friend |
7386 | we're adding is a template, not an ordinary class. It's | |
7387 | important that we use make_friend_class since it will | |
7388 | perform some error-checking and output cross-reference | |
7389 | information. */ | |
7390 | ++processing_template_decl; | |
fc378698 | 7391 | |
b939a023 | 7392 | if (friend_type != error_mark_node) |
0cbd7506 | 7393 | make_friend_class (type, friend_type, /*complain=*/false); |
fc378698 | 7394 | |
b939a023 | 7395 | if (adjust_processing_template_decl) |
7088fca9 KL |
7396 | --processing_template_decl; |
7397 | } | |
7398 | else | |
9579624e KL |
7399 | { |
7400 | /* Build new DECL_FRIENDLIST. */ | |
7401 | tree r; | |
7402 | ||
39a13be5 | 7403 | /* The file and line for this declaration, to |
6e432b31 KL |
7404 | assist in error message reporting. Since we |
7405 | called push_tinst_level above, we don't need to | |
7406 | restore these. */ | |
7407 | input_location = DECL_SOURCE_LOCATION (t); | |
7408 | ||
9579624e | 7409 | if (TREE_CODE (t) == TEMPLATE_DECL) |
cad7e87b NS |
7410 | { |
7411 | ++processing_template_decl; | |
7412 | push_deferring_access_checks (dk_no_check); | |
7413 | } | |
c8094d83 | 7414 | |
9579624e | 7415 | r = tsubst_friend_function (t, args); |
9579624e | 7416 | add_friend (type, r, /*complain=*/false); |
cad7e87b NS |
7417 | if (TREE_CODE (t) == TEMPLATE_DECL) |
7418 | { | |
7419 | pop_deferring_access_checks (); | |
7420 | --processing_template_decl; | |
7421 | } | |
9579624e | 7422 | } |
7088fca9 KL |
7423 | } |
7424 | } | |
5566b478 | 7425 | |
61a127b3 MM |
7426 | /* Set the file and line number information to whatever is given for |
7427 | the class itself. This puts error messages involving generated | |
7428 | implicit functions at a predictable point, and the same point | |
7429 | that would be used for non-template classes. */ | |
f31686a3 | 7430 | input_location = DECL_SOURCE_LOCATION (typedecl); |
6de9cd9a | 7431 | |
61a127b3 | 7432 | unreverse_member_declarations (type); |
9f33663b | 7433 | finish_struct_1 (type); |
5524676d | 7434 | TYPE_BEING_DEFINED (type) = 0; |
8d08fdba | 7435 | |
9188c363 MM |
7436 | /* Now that the class is complete, instantiate default arguments for |
7437 | any member functions. We don't do this earlier because the | |
7438 | default arguments may reference members of the class. */ | |
be93747e | 7439 | if (!PRIMARY_TEMPLATE_P (templ)) |
9188c363 | 7440 | for (t = TYPE_METHODS (type); t; t = TREE_CHAIN (t)) |
c8094d83 | 7441 | if (TREE_CODE (t) == FUNCTION_DECL |
dc957d14 | 7442 | /* Implicitly generated member functions will not have template |
9188c363 MM |
7443 | information; they are not instantiations, but instead are |
7444 | created "fresh" for each instantiation. */ | |
7445 | && DECL_TEMPLATE_INFO (t)) | |
7446 | tsubst_default_arguments (t); | |
7447 | ||
d0940d56 DS |
7448 | /* Some typedefs referenced from within the template code need to be access |
7449 | checked at template instantiation time, i.e now. These types were | |
7450 | added to the template at parsing time. Let's get those and perform | |
2eb25c98 DS |
7451 | the access checks then. */ |
7452 | perform_typedefs_access_check (pattern, args); | |
d0940d56 | 7453 | perform_deferred_access_checks (); |
56d0c6e3 | 7454 | pop_nested_class (); |
5566b478 | 7455 | pop_from_top_level (); |
78757caa | 7456 | pop_deferring_access_checks (); |
5566b478 MS |
7457 | pop_tinst_level (); |
7458 | ||
4684cd27 MM |
7459 | /* The vtable for a template class can be emitted in any translation |
7460 | unit in which the class is instantiated. When there is no key | |
7461 | method, however, finish_struct_1 will already have added TYPE to | |
7462 | the keyed_classes list. */ | |
7463 | if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type)) | |
9aad8f83 MA |
7464 | keyed_classes = tree_cons (NULL_TREE, type, keyed_classes); |
7465 | ||
5566b478 | 7466 | return type; |
8d08fdba MS |
7467 | } |
7468 | ||
00d3396f | 7469 | static tree |
a91db711 | 7470 | tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl) |
00d3396f | 7471 | { |
a91db711 | 7472 | tree r; |
c8094d83 | 7473 | |
a91db711 NS |
7474 | if (!t) |
7475 | r = t; | |
7476 | else if (TYPE_P (t)) | |
7477 | r = tsubst (t, args, complain, in_decl); | |
7478 | else | |
00d3396f | 7479 | { |
015c2c66 MM |
7480 | r = tsubst_expr (t, args, complain, in_decl, |
7481 | /*integral_constant_expression_p=*/true); | |
6f25cb35 | 7482 | r = fold_non_dependent_expr (r); |
bd83b409 | 7483 | } |
5d80a306 DG |
7484 | return r; |
7485 | } | |
7486 | ||
6afcfe0a JM |
7487 | /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a |
7488 | NONTYPE_ARGUMENT_PACK. */ | |
7489 | ||
7490 | static tree | |
7491 | make_fnparm_pack (tree spec_parm) | |
7492 | { | |
7493 | /* Collect all of the extra "packed" parameters into an | |
7494 | argument pack. */ | |
7495 | tree parmvec; | |
7496 | tree parmtypevec; | |
7497 | tree argpack = make_node (NONTYPE_ARGUMENT_PACK); | |
7498 | tree argtypepack = make_node (TYPE_ARGUMENT_PACK); | |
7499 | int i, len = list_length (spec_parm); | |
7500 | ||
7501 | /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */ | |
7502 | parmvec = make_tree_vec (len); | |
7503 | parmtypevec = make_tree_vec (len); | |
7504 | for (i = 0; i < len; i++, spec_parm = TREE_CHAIN (spec_parm)) | |
7505 | { | |
7506 | TREE_VEC_ELT (parmvec, i) = spec_parm; | |
7507 | TREE_VEC_ELT (parmtypevec, i) = TREE_TYPE (spec_parm); | |
7508 | } | |
7509 | ||
7510 | /* Build the argument packs. */ | |
7511 | SET_ARGUMENT_PACK_ARGS (argpack, parmvec); | |
7512 | SET_ARGUMENT_PACK_ARGS (argtypepack, parmtypevec); | |
7513 | TREE_TYPE (argpack) = argtypepack; | |
7514 | ||
7515 | return argpack; | |
7516 | } | |
7517 | ||
5d80a306 DG |
7518 | /* Substitute ARGS into T, which is an pack expansion |
7519 | (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a | |
7520 | TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node | |
7521 | (if only a partial substitution could be performed) or | |
7522 | ERROR_MARK_NODE if there was an error. */ | |
7523 | tree | |
7524 | tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain, | |
7525 | tree in_decl) | |
7526 | { | |
7527 | tree pattern; | |
7528 | tree pack, packs = NULL_TREE, unsubstituted_packs = NULL_TREE; | |
7529 | tree first_arg_pack; int i, len = -1; | |
7530 | tree result; | |
7531 | int incomplete = 0; | |
6afcfe0a | 7532 | bool very_local_specializations = false; |
5d80a306 DG |
7533 | |
7534 | gcc_assert (PACK_EXPANSION_P (t)); | |
7535 | pattern = PACK_EXPANSION_PATTERN (t); | |
7536 | ||
7537 | /* Determine the argument packs that will instantiate the parameter | |
7538 | packs used in the expansion expression. While we're at it, | |
7539 | compute the number of arguments to be expanded and make sure it | |
7540 | is consistent. */ | |
7541 | for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack; | |
7542 | pack = TREE_CHAIN (pack)) | |
7543 | { | |
7544 | tree parm_pack = TREE_VALUE (pack); | |
7545 | tree arg_pack = NULL_TREE; | |
7546 | tree orig_arg = NULL_TREE; | |
7547 | ||
7548 | if (TREE_CODE (parm_pack) == PARM_DECL) | |
6afcfe0a JM |
7549 | { |
7550 | arg_pack = retrieve_local_specialization (parm_pack); | |
7551 | if (arg_pack == NULL_TREE) | |
7552 | { | |
7553 | /* This can happen for a parameter name used later in a function | |
7554 | declaration (such as in a late-specified return type). Just | |
7555 | make a dummy decl, since it's only used for its type. */ | |
7556 | gcc_assert (skip_evaluation); | |
7557 | arg_pack = tsubst_decl (parm_pack, args, complain); | |
7558 | arg_pack = make_fnparm_pack (arg_pack); | |
7559 | } | |
7560 | } | |
5d80a306 DG |
7561 | else |
7562 | { | |
7563 | int level, idx, levels; | |
7564 | template_parm_level_and_index (parm_pack, &level, &idx); | |
7565 | ||
7566 | levels = TMPL_ARGS_DEPTH (args); | |
7567 | if (level <= levels) | |
7568 | arg_pack = TMPL_ARG (args, level, idx); | |
7569 | } | |
7570 | ||
7571 | orig_arg = arg_pack; | |
7572 | if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT) | |
7573 | arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack); | |
7574 | ||
d393153e DG |
7575 | if (arg_pack && !ARGUMENT_PACK_P (arg_pack)) |
7576 | /* This can only happen if we forget to expand an argument | |
7577 | pack somewhere else. Just return an error, silently. */ | |
7578 | { | |
7579 | result = make_tree_vec (1); | |
7580 | TREE_VEC_ELT (result, 0) = error_mark_node; | |
7581 | return result; | |
7582 | } | |
7583 | ||
88b82314 DG |
7584 | if (arg_pack |
7585 | && TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack)) == 1 | |
7586 | && PACK_EXPANSION_P (TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg_pack), 0))) | |
7587 | { | |
7588 | tree expansion = TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg_pack), 0); | |
7589 | tree pattern = PACK_EXPANSION_PATTERN (expansion); | |
7590 | if ((TYPE_P (pattern) && same_type_p (pattern, parm_pack)) | |
7591 | || (!TYPE_P (pattern) && cp_tree_equal (parm_pack, pattern))) | |
7592 | /* The argument pack that the parameter maps to is just an | |
7593 | expansion of the parameter itself, such as one would | |
7594 | find in the implicit typedef of a class inside the | |
7595 | class itself. Consider this parameter "unsubstituted", | |
7596 | so that we will maintain the outer pack expansion. */ | |
7597 | arg_pack = NULL_TREE; | |
7598 | } | |
7599 | ||
5d80a306 DG |
7600 | if (arg_pack) |
7601 | { | |
7602 | int my_len = | |
7603 | TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack)); | |
7604 | ||
7605 | /* It's all-or-nothing with incomplete argument packs. */ | |
7606 | if (incomplete && !ARGUMENT_PACK_INCOMPLETE_P (arg_pack)) | |
7607 | return error_mark_node; | |
7608 | ||
7609 | if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack)) | |
7610 | incomplete = 1; | |
7611 | ||
7612 | if (len < 0) | |
7613 | { | |
7614 | len = my_len; | |
7615 | first_arg_pack = arg_pack; | |
7616 | } | |
7617 | else if (len != my_len) | |
7618 | { | |
7619 | if (TREE_CODE (t) == TYPE_PACK_EXPANSION) | |
7620 | error ("mismatched argument pack lengths while expanding " | |
7621 | "%<%T%>", | |
7622 | pattern); | |
7623 | else | |
7624 | error ("mismatched argument pack lengths while expanding " | |
7625 | "%<%E%>", | |
7626 | pattern); | |
7627 | return error_mark_node; | |
7628 | } | |
7629 | ||
7630 | /* Keep track of the parameter packs and their corresponding | |
7631 | argument packs. */ | |
7632 | packs = tree_cons (parm_pack, arg_pack, packs); | |
7633 | TREE_TYPE (packs) = orig_arg; | |
7634 | } | |
7635 | else | |
7636 | /* We can't substitute for this parameter pack. */ | |
7637 | unsubstituted_packs = tree_cons (TREE_PURPOSE (pack), | |
7638 | TREE_VALUE (pack), | |
7639 | unsubstituted_packs); | |
7640 | } | |
7641 | ||
7642 | /* We cannot expand this expansion expression, because we don't have | |
7643 | all of the argument packs we need. Substitute into the pattern | |
7644 | and return a PACK_EXPANSION_*. The caller will need to deal with | |
7645 | that. */ | |
7646 | if (unsubstituted_packs) | |
7647 | return make_pack_expansion (tsubst (pattern, args, complain, | |
7648 | in_decl)); | |
7649 | ||
7650 | /* We could not find any argument packs that work. */ | |
7651 | if (len < 0) | |
7652 | return error_mark_node; | |
7653 | ||
6afcfe0a JM |
7654 | if (!local_specializations) |
7655 | { | |
7656 | /* We're in a late-specified return type, so we don't have a local | |
7657 | specializations table. Create one for doing this expansion. */ | |
7658 | very_local_specializations = true; | |
7659 | local_specializations = htab_create (37, | |
7660 | hash_local_specialization, | |
7661 | eq_local_specializations, | |
7662 | NULL); | |
7663 | } | |
7664 | ||
5d80a306 DG |
7665 | /* For each argument in each argument pack, substitute into the |
7666 | pattern. */ | |
7667 | result = make_tree_vec (len + incomplete); | |
7668 | for (i = 0; i < len + incomplete; ++i) | |
7669 | { | |
7670 | /* For parameter pack, change the substitution of the parameter | |
7671 | pack to the ith argument in its argument pack, then expand | |
7672 | the pattern. */ | |
7673 | for (pack = packs; pack; pack = TREE_CHAIN (pack)) | |
7674 | { | |
7675 | tree parm = TREE_PURPOSE (pack); | |
7676 | ||
7677 | if (TREE_CODE (parm) == PARM_DECL) | |
7678 | { | |
7679 | /* Select the Ith argument from the pack. */ | |
7680 | tree arg = make_node (ARGUMENT_PACK_SELECT); | |
7681 | ARGUMENT_PACK_SELECT_FROM_PACK (arg) = TREE_VALUE (pack); | |
7682 | ARGUMENT_PACK_SELECT_INDEX (arg) = i; | |
7683 | mark_used (parm); | |
7684 | register_local_specialization (arg, parm); | |
7685 | } | |
7686 | else | |
7687 | { | |
7688 | tree value = parm; | |
7689 | int idx, level; | |
7690 | template_parm_level_and_index (parm, &level, &idx); | |
7691 | ||
7692 | if (i < len) | |
7693 | { | |
7694 | /* Select the Ith argument from the pack. */ | |
7695 | value = make_node (ARGUMENT_PACK_SELECT); | |
7696 | ARGUMENT_PACK_SELECT_FROM_PACK (value) = TREE_VALUE (pack); | |
7697 | ARGUMENT_PACK_SELECT_INDEX (value) = i; | |
7698 | } | |
7699 | ||
7700 | /* Update the corresponding argument. */ | |
7701 | TMPL_ARG (args, level, idx) = value; | |
7702 | } | |
7703 | } | |
7704 | ||
7705 | /* Substitute into the PATTERN with the altered arguments. */ | |
7706 | if (TREE_CODE (t) == EXPR_PACK_EXPANSION) | |
7707 | TREE_VEC_ELT (result, i) = | |
7708 | tsubst_expr (pattern, args, complain, in_decl, | |
7709 | /*integral_constant_expression_p=*/false); | |
7710 | else | |
7711 | TREE_VEC_ELT (result, i) = tsubst (pattern, args, complain, in_decl); | |
7712 | ||
7713 | if (i == len) | |
7714 | /* When we have incomplete argument packs, the last "expanded" | |
7715 | result is itself a pack expansion, which allows us | |
7716 | to deduce more arguments. */ | |
7717 | TREE_VEC_ELT (result, i) = | |
7718 | make_pack_expansion (TREE_VEC_ELT (result, i)); | |
7719 | ||
7720 | if (TREE_VEC_ELT (result, i) == error_mark_node) | |
7721 | { | |
7722 | result = error_mark_node; | |
7723 | break; | |
7724 | } | |
7725 | } | |
6afcfe0a | 7726 | |
5d80a306 DG |
7727 | /* Update ARGS to restore the substitution from parameter packs to |
7728 | their argument packs. */ | |
7729 | for (pack = packs; pack; pack = TREE_CHAIN (pack)) | |
7730 | { | |
7731 | tree parm = TREE_PURPOSE (pack); | |
7732 | ||
7733 | if (TREE_CODE (parm) == PARM_DECL) | |
7734 | register_local_specialization (TREE_TYPE (pack), parm); | |
7735 | else | |
7736 | { | |
7737 | int idx, level; | |
7738 | template_parm_level_and_index (parm, &level, &idx); | |
7739 | ||
7740 | /* Update the corresponding argument. */ | |
7741 | if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args)) | |
7742 | TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) = | |
7743 | TREE_TYPE (pack); | |
7744 | else | |
7745 | TREE_VEC_ELT (args, idx) = TREE_TYPE (pack); | |
7746 | } | |
7747 | } | |
7748 | ||
6afcfe0a JM |
7749 | if (very_local_specializations) |
7750 | { | |
7751 | htab_delete (local_specializations); | |
7752 | local_specializations = NULL; | |
7753 | } | |
7754 | ||
5d80a306 | 7755 | return result; |
bd83b409 NS |
7756 | } |
7757 | ||
a91db711 | 7758 | /* Substitute ARGS into the vector or list of template arguments T. */ |
830bfa74 | 7759 | |
e9659ab0 | 7760 | static tree |
a91db711 | 7761 | tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl) |
830bfa74 | 7762 | { |
5d80a306 | 7763 | tree orig_t = t; |
bf12d54d | 7764 | int len = TREE_VEC_LENGTH (t); |
5d80a306 | 7765 | int need_new = 0, i, expanded_len_adjust = 0, out; |
67f5655f | 7766 | tree *elts = (tree *) alloca (len * sizeof (tree)); |
c8094d83 | 7767 | |
830bfa74 MM |
7768 | for (i = 0; i < len; i++) |
7769 | { | |
bf12d54d NS |
7770 | tree orig_arg = TREE_VEC_ELT (t, i); |
7771 | tree new_arg; | |
a91db711 | 7772 | |
bf12d54d NS |
7773 | if (TREE_CODE (orig_arg) == TREE_VEC) |
7774 | new_arg = tsubst_template_args (orig_arg, args, complain, in_decl); | |
5d80a306 DG |
7775 | else if (PACK_EXPANSION_P (orig_arg)) |
7776 | { | |
7777 | /* Substitute into an expansion expression. */ | |
7778 | new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl); | |
7779 | ||
7780 | if (TREE_CODE (new_arg) == TREE_VEC) | |
7781 | /* Add to the expanded length adjustment the number of | |
7782 | expanded arguments. We subtract one from this | |
7783 | measurement, because the argument pack expression | |
7784 | itself is already counted as 1 in | |
7785 | LEN. EXPANDED_LEN_ADJUST can actually be negative, if | |
7786 | the argument pack is empty. */ | |
7787 | expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1; | |
7788 | } | |
7789 | else if (ARGUMENT_PACK_P (orig_arg)) | |
7790 | { | |
7791 | /* Substitute into each of the arguments. */ | |
7792 | new_arg = make_node (TREE_CODE (orig_arg)); | |
7793 | ||
7794 | SET_ARGUMENT_PACK_ARGS ( | |
7795 | new_arg, | |
7796 | tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg), | |
7797 | args, complain, in_decl)); | |
7798 | ||
7799 | if (ARGUMENT_PACK_ARGS (new_arg) == error_mark_node) | |
7800 | new_arg = error_mark_node; | |
7801 | ||
7802 | if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK) { | |
7803 | TREE_TYPE (new_arg) = tsubst (TREE_TYPE (orig_arg), args, | |
7804 | complain, in_decl); | |
7805 | TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg); | |
7806 | ||
7807 | if (TREE_TYPE (new_arg) == error_mark_node) | |
7808 | new_arg = error_mark_node; | |
7809 | } | |
7810 | } | |
830bfa74 | 7811 | else |
a91db711 | 7812 | new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl); |
c8094d83 | 7813 | |
a91db711 | 7814 | if (new_arg == error_mark_node) |
08e72a19 JM |
7815 | return error_mark_node; |
7816 | ||
a91db711 NS |
7817 | elts[i] = new_arg; |
7818 | if (new_arg != orig_arg) | |
830bfa74 MM |
7819 | need_new = 1; |
7820 | } | |
c8094d83 | 7821 | |
830bfa74 MM |
7822 | if (!need_new) |
7823 | return t; | |
a91db711 | 7824 | |
5d80a306 DG |
7825 | /* Make space for the expanded arguments coming from template |
7826 | argument packs. */ | |
7827 | t = make_tree_vec (len + expanded_len_adjust); | |
7828 | for (i = 0, out = 0; i < len; i++) | |
7829 | { | |
7830 | if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i)) | |
7831 | || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i))) | |
7832 | && TREE_CODE (elts[i]) == TREE_VEC) | |
7833 | { | |
7834 | int idx; | |
7835 | ||
7836 | /* Now expand the template argument pack "in place". */ | |
7837 | for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++) | |
7838 | TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx); | |
7839 | } | |
7840 | else | |
7841 | { | |
7842 | TREE_VEC_ELT (t, out) = elts[i]; | |
7843 | out++; | |
7844 | } | |
7845 | } | |
c8094d83 | 7846 | |
830bfa74 MM |
7847 | return t; |
7848 | } | |
7849 | ||
36a117a5 MM |
7850 | /* Return the result of substituting ARGS into the template parameters |
7851 | given by PARMS. If there are m levels of ARGS and m + n levels of | |
7852 | PARMS, then the result will contain n levels of PARMS. For | |
7853 | example, if PARMS is `template <class T> template <class U> | |
7854 | template <T*, U, class V>' and ARGS is {{int}, {double}} then the | |
7855 | result will be `template <int*, double, class V>'. */ | |
7856 | ||
e9659ab0 | 7857 | static tree |
3a978d72 | 7858 | tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain) |
36a117a5 | 7859 | { |
f71f87f9 MM |
7860 | tree r = NULL_TREE; |
7861 | tree* new_parms; | |
36a117a5 | 7862 | |
8ca4bf25 MM |
7863 | /* When substituting into a template, we must set |
7864 | PROCESSING_TEMPLATE_DECL as the template parameters may be | |
7865 | dependent if they are based on one-another, and the dependency | |
7866 | predicates are short-circuit outside of templates. */ | |
7867 | ++processing_template_decl; | |
7868 | ||
36a117a5 MM |
7869 | for (new_parms = &r; |
7870 | TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args); | |
7871 | new_parms = &(TREE_CHAIN (*new_parms)), | |
7872 | parms = TREE_CHAIN (parms)) | |
7873 | { | |
c8094d83 | 7874 | tree new_vec = |
36a117a5 MM |
7875 | make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms))); |
7876 | int i; | |
c8094d83 | 7877 | |
36a117a5 MM |
7878 | for (i = 0; i < TREE_VEC_LENGTH (new_vec); ++i) |
7879 | { | |
42b304f1 LM |
7880 | tree tuple; |
7881 | tree default_value; | |
7882 | tree parm_decl; | |
7883 | ||
7884 | if (parms == error_mark_node) | |
7885 | continue; | |
7886 | ||
7887 | tuple = TREE_VEC_ELT (TREE_VALUE (parms), i); | |
7888 | ||
7889 | if (tuple == error_mark_node) | |
7890 | continue; | |
7891 | ||
7892 | default_value = TREE_PURPOSE (tuple); | |
7893 | parm_decl = TREE_VALUE (tuple); | |
833aa4c4 NS |
7894 | |
7895 | parm_decl = tsubst (parm_decl, args, complain, NULL_TREE); | |
a207780f VR |
7896 | if (TREE_CODE (parm_decl) == PARM_DECL |
7897 | && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain)) | |
7898 | parm_decl = error_mark_node; | |
a91db711 NS |
7899 | default_value = tsubst_template_arg (default_value, args, |
7900 | complain, NULL_TREE); | |
c8094d83 | 7901 | |
a91db711 | 7902 | tuple = build_tree_list (default_value, parm_decl); |
833aa4c4 | 7903 | TREE_VEC_ELT (new_vec, i) = tuple; |
36a117a5 | 7904 | } |
c8094d83 MS |
7905 | |
7906 | *new_parms = | |
7907 | tree_cons (size_int (TMPL_PARMS_DEPTH (parms) | |
4890c2f4 | 7908 | - TMPL_ARGS_DEPTH (args)), |
36a117a5 MM |
7909 | new_vec, NULL_TREE); |
7910 | } | |
7911 | ||
8ca4bf25 MM |
7912 | --processing_template_decl; |
7913 | ||
36a117a5 MM |
7914 | return r; |
7915 | } | |
7916 | ||
ed44da02 MM |
7917 | /* Substitute the ARGS into the indicated aggregate (or enumeration) |
7918 | type T. If T is not an aggregate or enumeration type, it is | |
7919 | handled as if by tsubst. IN_DECL is as for tsubst. If | |
838dfd8a | 7920 | ENTERING_SCOPE is nonzero, T is the context for a template which |
dc957d14 | 7921 | we are presently tsubst'ing. Return the substituted value. */ |
36a117a5 | 7922 | |
e9659ab0 | 7923 | static tree |
c8094d83 | 7924 | tsubst_aggr_type (tree t, |
0cbd7506 MS |
7925 | tree args, |
7926 | tsubst_flags_t complain, | |
7927 | tree in_decl, | |
7928 | int entering_scope) | |
36a117a5 MM |
7929 | { |
7930 | if (t == NULL_TREE) | |
7931 | return NULL_TREE; | |
7932 | ||
7933 | switch (TREE_CODE (t)) | |
7934 | { | |
7935 | case RECORD_TYPE: | |
7936 | if (TYPE_PTRMEMFUNC_P (t)) | |
46c895ac | 7937 | return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl); |
36a117a5 | 7938 | |
f4f206f4 | 7939 | /* Else fall through. */ |
ed44da02 | 7940 | case ENUMERAL_TYPE: |
36a117a5 | 7941 | case UNION_TYPE: |
82390eb6 | 7942 | if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t)) |
36a117a5 MM |
7943 | { |
7944 | tree argvec; | |
7945 | tree context; | |
7946 | tree r; | |
3489ea0c MM |
7947 | bool saved_skip_evaluation; |
7948 | ||
7949 | /* In "sizeof(X<I>)" we need to evaluate "I". */ | |
7950 | saved_skip_evaluation = skip_evaluation; | |
7951 | skip_evaluation = false; | |
36a117a5 MM |
7952 | |
7953 | /* First, determine the context for the type we are looking | |
7954 | up. */ | |
4f7847ca NS |
7955 | context = TYPE_CONTEXT (t); |
7956 | if (context) | |
29b0d1fd JM |
7957 | { |
7958 | context = tsubst_aggr_type (context, args, complain, | |
7959 | in_decl, /*entering_scope=*/1); | |
7960 | /* If context is a nested class inside a class template, | |
7961 | it may still need to be instantiated (c++/33959). */ | |
7962 | if (TYPE_P (context)) | |
7963 | context = complete_type (context); | |
7964 | } | |
36a117a5 MM |
7965 | |
7966 | /* Then, figure out what arguments are appropriate for the | |
7967 | type we are trying to find. For example, given: | |
7968 | ||
7969 | template <class T> struct S; | |
7970 | template <class T, class U> void f(T, U) { S<U> su; } | |
7971 | ||
7972 | and supposing that we are instantiating f<int, double>, | |
7973 | then our ARGS will be {int, double}, but, when looking up | |
7974 | S we only want {double}. */ | |
a91db711 NS |
7975 | argvec = tsubst_template_args (TYPE_TI_ARGS (t), args, |
7976 | complain, in_decl); | |
08e72a19 | 7977 | if (argvec == error_mark_node) |
3489ea0c MM |
7978 | r = error_mark_node; |
7979 | else | |
7980 | { | |
7981 | r = lookup_template_class (t, argvec, in_decl, context, | |
7982 | entering_scope, complain); | |
7983 | r = cp_build_qualified_type_real (r, TYPE_QUALS (t), complain); | |
7984 | } | |
3db45ab5 | 7985 | |
3489ea0c | 7986 | skip_evaluation = saved_skip_evaluation; |
36a117a5 | 7987 | |
3489ea0c | 7988 | return r; |
36a117a5 | 7989 | } |
c8094d83 | 7990 | else |
36a117a5 MM |
7991 | /* This is not a template type, so there's nothing to do. */ |
7992 | return t; | |
7993 | ||
7994 | default: | |
4393e105 | 7995 | return tsubst (t, args, complain, in_decl); |
36a117a5 MM |
7996 | } |
7997 | } | |
7998 | ||
9188c363 MM |
7999 | /* Substitute into the default argument ARG (a default argument for |
8000 | FN), which has the indicated TYPE. */ | |
8001 | ||
8002 | tree | |
3a978d72 | 8003 | tsubst_default_argument (tree fn, tree type, tree arg) |
9188c363 | 8004 | { |
2436b51f MM |
8005 | tree saved_class_ptr = NULL_TREE; |
8006 | tree saved_class_ref = NULL_TREE; | |
8007 | ||
9188c363 MM |
8008 | /* This default argument came from a template. Instantiate the |
8009 | default argument here, not in tsubst. In the case of | |
c8094d83 MS |
8010 | something like: |
8011 | ||
9188c363 MM |
8012 | template <class T> |
8013 | struct S { | |
8014 | static T t(); | |
8015 | void f(T = t()); | |
8016 | }; | |
c8094d83 | 8017 | |
9188c363 | 8018 | we must be careful to do name lookup in the scope of S<T>, |
d5a10cf0 | 8019 | rather than in the current class. */ |
2b59fc25 | 8020 | push_access_scope (fn); |
2436b51f MM |
8021 | /* The "this" pointer is not valid in a default argument. */ |
8022 | if (cfun) | |
8023 | { | |
8024 | saved_class_ptr = current_class_ptr; | |
8025 | cp_function_chain->x_current_class_ptr = NULL_TREE; | |
8026 | saved_class_ref = current_class_ref; | |
8027 | cp_function_chain->x_current_class_ref = NULL_TREE; | |
8028 | } | |
9188c363 | 8029 | |
d5a10cf0 | 8030 | push_deferring_access_checks(dk_no_deferred); |
5a8613b2 MM |
8031 | /* The default argument expression may cause implicitly defined |
8032 | member functions to be synthesized, which will result in garbage | |
8033 | collection. We must treat this situation as if we were within | |
8034 | the body of function so as to avoid collecting live data on the | |
8035 | stack. */ | |
8036 | ++function_depth; | |
c2ea3a40 | 8037 | arg = tsubst_expr (arg, DECL_TI_ARGS (fn), |
015c2c66 MM |
8038 | tf_warning_or_error, NULL_TREE, |
8039 | /*integral_constant_expression_p=*/false); | |
5a8613b2 | 8040 | --function_depth; |
d5a10cf0 MM |
8041 | pop_deferring_access_checks(); |
8042 | ||
2436b51f MM |
8043 | /* Restore the "this" pointer. */ |
8044 | if (cfun) | |
8045 | { | |
8046 | cp_function_chain->x_current_class_ptr = saved_class_ptr; | |
8047 | cp_function_chain->x_current_class_ref = saved_class_ref; | |
8048 | } | |
8049 | ||
2b59fc25 | 8050 | pop_access_scope (fn); |
9188c363 MM |
8051 | |
8052 | /* Make sure the default argument is reasonable. */ | |
8053 | arg = check_default_argument (type, arg); | |
8054 | ||
8055 | return arg; | |
8056 | } | |
8057 | ||
8058 | /* Substitute into all the default arguments for FN. */ | |
8059 | ||
8060 | static void | |
3a978d72 | 8061 | tsubst_default_arguments (tree fn) |
9188c363 MM |
8062 | { |
8063 | tree arg; | |
8064 | tree tmpl_args; | |
8065 | ||
8066 | tmpl_args = DECL_TI_ARGS (fn); | |
8067 | ||
8068 | /* If this function is not yet instantiated, we certainly don't need | |
8069 | its default arguments. */ | |
8070 | if (uses_template_parms (tmpl_args)) | |
8071 | return; | |
8072 | ||
c8094d83 MS |
8073 | for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn)); |
8074 | arg; | |
9188c363 MM |
8075 | arg = TREE_CHAIN (arg)) |
8076 | if (TREE_PURPOSE (arg)) | |
c8094d83 | 8077 | TREE_PURPOSE (arg) = tsubst_default_argument (fn, |
9188c363 MM |
8078 | TREE_VALUE (arg), |
8079 | TREE_PURPOSE (arg)); | |
8080 | } | |
8081 | ||
fc6a28d7 MM |
8082 | /* Substitute the ARGS into the T, which is a _DECL. Return the |
8083 | result of the substitution. Issue error and warning messages under | |
8084 | control of COMPLAIN. */ | |
00d3396f | 8085 | |
e9659ab0 | 8086 | static tree |
fc6a28d7 | 8087 | tsubst_decl (tree t, tree args, tsubst_flags_t complain) |
8d08fdba | 8088 | { |
82a98427 | 8089 | location_t saved_loc; |
b370501f | 8090 | tree r = NULL_TREE; |
4b2811e9 | 8091 | tree in_decl = t; |
830bfa74 | 8092 | |
ae58fa02 | 8093 | /* Set the filename and linenumber to improve error-reporting. */ |
82a98427 | 8094 | saved_loc = input_location; |
f31686a3 | 8095 | input_location = DECL_SOURCE_LOCATION (t); |
b7484fbe | 8096 | |
8d08fdba MS |
8097 | switch (TREE_CODE (t)) |
8098 | { | |
98c1c668 JM |
8099 | case TEMPLATE_DECL: |
8100 | { | |
28e42b7e KL |
8101 | /* We can get here when processing a member function template, |
8102 | member class template, and template template parameter of | |
8103 | a template class. */ | |
98c1c668 | 8104 | tree decl = DECL_TEMPLATE_RESULT (t); |
386b8a85 | 8105 | tree spec; |
28e42b7e KL |
8106 | tree tmpl_args; |
8107 | tree full_args; | |
98c1c668 | 8108 | |
28e42b7e | 8109 | if (DECL_TEMPLATE_TEMPLATE_PARM_P (t)) |
db2767b6 | 8110 | { |
28e42b7e KL |
8111 | /* Template template parameter is treated here. */ |
8112 | tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl); | |
8113 | if (new_type == error_mark_node) | |
8114 | return error_mark_node; | |
36a117a5 | 8115 | |
28e42b7e KL |
8116 | r = copy_decl (t); |
8117 | TREE_CHAIN (r) = NULL_TREE; | |
8118 | TREE_TYPE (r) = new_type; | |
8119 | DECL_TEMPLATE_RESULT (r) | |
c2255bc4 AH |
8120 | = build_decl (DECL_SOURCE_LOCATION (decl), |
8121 | TYPE_DECL, DECL_NAME (decl), new_type); | |
c8094d83 | 8122 | DECL_TEMPLATE_PARMS (r) |
28e42b7e KL |
8123 | = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args, |
8124 | complain); | |
8125 | TYPE_NAME (new_type) = r; | |
8126 | break; | |
8127 | } | |
36a117a5 | 8128 | |
28e42b7e KL |
8129 | /* We might already have an instance of this template. |
8130 | The ARGS are for the surrounding class type, so the | |
8131 | full args contain the tsubst'd args for the context, | |
8132 | plus the innermost args from the template decl. */ | |
c8094d83 | 8133 | tmpl_args = DECL_CLASS_TEMPLATE_P (t) |
28e42b7e KL |
8134 | ? CLASSTYPE_TI_ARGS (TREE_TYPE (t)) |
8135 | : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t)); | |
8ca4bf25 MM |
8136 | /* Because this is a template, the arguments will still be |
8137 | dependent, even after substitution. If | |
8138 | PROCESSING_TEMPLATE_DECL is not set, the dependency | |
8139 | predicates will short-circuit. */ | |
8140 | ++processing_template_decl; | |
28e42b7e KL |
8141 | full_args = tsubst_template_args (tmpl_args, args, |
8142 | complain, in_decl); | |
8ca4bf25 | 8143 | --processing_template_decl; |
bf2f7328 VR |
8144 | if (full_args == error_mark_node) |
8145 | return error_mark_node; | |
28e42b7e KL |
8146 | |
8147 | /* tsubst_template_args doesn't copy the vector if | |
8148 | nothing changed. But, *something* should have | |
8149 | changed. */ | |
8150 | gcc_assert (full_args != tmpl_args); | |
8151 | ||
8152 | spec = retrieve_specialization (t, full_args, | |
8153 | /*class_specializations_p=*/true); | |
8154 | if (spec != NULL_TREE) | |
8155 | { | |
8156 | r = spec; | |
8157 | break; | |
db2767b6 | 8158 | } |
98c1c668 JM |
8159 | |
8160 | /* Make a new template decl. It will be similar to the | |
c8094d83 | 8161 | original, but will record the current template arguments. |
98c1c668 JM |
8162 | We also create a new function declaration, which is just |
8163 | like the old one, but points to this new template, rather | |
8164 | than the old one. */ | |
0acf7199 | 8165 | r = copy_decl (t); |
50bc768d | 8166 | gcc_assert (DECL_LANG_SPECIFIC (r) != 0); |
ae58fa02 | 8167 | TREE_CHAIN (r) = NULL_TREE; |
db2767b6 | 8168 | |
ae58fa02 | 8169 | DECL_TEMPLATE_INFO (r) = build_tree_list (t, args); |
93cdc044 JM |
8170 | |
8171 | if (TREE_CODE (decl) == TYPE_DECL) | |
8172 | { | |
8ca4bf25 MM |
8173 | tree new_type; |
8174 | ++processing_template_decl; | |
8175 | new_type = tsubst (TREE_TYPE (t), args, complain, in_decl); | |
3db45ab5 | 8176 | --processing_template_decl; |
2620d095 KL |
8177 | if (new_type == error_mark_node) |
8178 | return error_mark_node; | |
8179 | ||
ae58fa02 MM |
8180 | TREE_TYPE (r) = new_type; |
8181 | CLASSTYPE_TI_TEMPLATE (new_type) = r; | |
17aec3eb | 8182 | DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type); |
ae58fa02 | 8183 | DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type); |
8ca4bf25 | 8184 | DECL_CONTEXT (r) = TYPE_CONTEXT (new_type); |
93cdc044 JM |
8185 | } |
8186 | else | |
8187 | { | |
8ca4bf25 MM |
8188 | tree new_decl; |
8189 | ++processing_template_decl; | |
8190 | new_decl = tsubst (decl, args, complain, in_decl); | |
8191 | --processing_template_decl; | |
caec1dc0 KL |
8192 | if (new_decl == error_mark_node) |
8193 | return error_mark_node; | |
17aec3eb RK |
8194 | |
8195 | DECL_TEMPLATE_RESULT (r) = new_decl; | |
ae58fa02 MM |
8196 | DECL_TI_TEMPLATE (new_decl) = r; |
8197 | TREE_TYPE (r) = TREE_TYPE (new_decl); | |
8198 | DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl); | |
3db45ab5 | 8199 | DECL_CONTEXT (r) = DECL_CONTEXT (new_decl); |
93cdc044 JM |
8200 | } |
8201 | ||
ae58fa02 MM |
8202 | SET_DECL_IMPLICIT_INSTANTIATION (r); |
8203 | DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE; | |
8204 | DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE; | |
98c1c668 JM |
8205 | |
8206 | /* The template parameters for this new template are all the | |
8207 | template parameters for the old template, except the | |
c6002625 | 8208 | outermost level of parameters. */ |
c8094d83 | 8209 | DECL_TEMPLATE_PARMS (r) |
4393e105 | 8210 | = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args, |
c2ea3a40 | 8211 | complain); |
98c1c668 | 8212 | |
93cdc044 | 8213 | if (PRIMARY_TEMPLATE_P (t)) |
ae58fa02 | 8214 | DECL_PRIMARY_TEMPLATE (r) = r; |
93cdc044 | 8215 | |
8c6ab2db NS |
8216 | if (TREE_CODE (decl) != TYPE_DECL) |
8217 | /* Record this non-type partial instantiation. */ | |
c8094d83 | 8218 | register_specialization (r, t, |
d63d5d0c ILT |
8219 | DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)), |
8220 | false); | |
98c1c668 | 8221 | } |
ae58fa02 | 8222 | break; |
8d08fdba MS |
8223 | |
8224 | case FUNCTION_DECL: | |
8225 | { | |
386b8a85 | 8226 | tree ctx; |
87603ed0 | 8227 | tree argvec = NULL_TREE; |
cf38f48a | 8228 | tree *friends; |
36a117a5 | 8229 | tree gen_tmpl; |
fc6a28d7 | 8230 | tree type; |
5566b478 | 8231 | int member; |
d8c4447d MM |
8232 | int args_depth; |
8233 | int parms_depth; | |
5566b478 | 8234 | |
36a117a5 | 8235 | /* Nobody should be tsubst'ing into non-template functions. */ |
50bc768d | 8236 | gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE); |
36a117a5 MM |
8237 | |
8238 | if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL) | |
8239 | { | |
8240 | tree spec; | |
00cf3e31 MM |
8241 | bool dependent_p; |
8242 | ||
8243 | /* If T is not dependent, just return it. We have to | |
8244 | increment PROCESSING_TEMPLATE_DECL because | |
8245 | value_dependent_expression_p assumes that nothing is | |
8246 | dependent when PROCESSING_TEMPLATE_DECL is zero. */ | |
8247 | ++processing_template_decl; | |
8248 | dependent_p = value_dependent_expression_p (t); | |
8249 | --processing_template_decl; | |
8250 | if (!dependent_p) | |
8251 | return t; | |
36a117a5 MM |
8252 | |
8253 | /* Calculate the most general template of which R is a | |
8254 | specialization, and the complete set of arguments used to | |
8255 | specialize R. */ | |
8256 | gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t)); | |
c8094d83 | 8257 | argvec = tsubst_template_args (DECL_TI_ARGS |
a91db711 | 8258 | (DECL_TEMPLATE_RESULT (gen_tmpl)), |
c8094d83 | 8259 | args, complain, in_decl); |
36a117a5 MM |
8260 | |
8261 | /* Check to see if we already have this specialization. */ | |
c7222c02 MM |
8262 | spec = retrieve_specialization (gen_tmpl, argvec, |
8263 | /*class_specializations_p=*/false); | |
7ddedda4 | 8264 | |
36a117a5 | 8265 | if (spec) |
ae58fa02 MM |
8266 | { |
8267 | r = spec; | |
8268 | break; | |
8269 | } | |
d8c4447d | 8270 | |
f9a7ae04 MM |
8271 | /* We can see more levels of arguments than parameters if |
8272 | there was a specialization of a member template, like | |
8273 | this: | |
8274 | ||
0cbd7506 | 8275 | template <class T> struct S { template <class U> void f(); } |
c8094d83 | 8276 | template <> template <class U> void S<int>::f(U); |
f9a7ae04 | 8277 | |
dc957d14 | 8278 | Here, we'll be substituting into the specialization, |
f9a7ae04 MM |
8279 | because that's where we can find the code we actually |
8280 | want to generate, but we'll have enough arguments for | |
c8094d83 | 8281 | the most general template. |
f9a7ae04 MM |
8282 | |
8283 | We also deal with the peculiar case: | |
d8c4447d | 8284 | |
c8094d83 | 8285 | template <class T> struct S { |
d8c4447d MM |
8286 | template <class U> friend void f(); |
8287 | }; | |
74b846e0 | 8288 | template <class U> void f() {} |
d8c4447d MM |
8289 | template S<int>; |
8290 | template void f<double>(); | |
8291 | ||
8292 | Here, the ARGS for the instantiation of will be {int, | |
8293 | double}. But, we only need as many ARGS as there are | |
8294 | levels of template parameters in CODE_PATTERN. We are | |
8295 | careful not to get fooled into reducing the ARGS in | |
8296 | situations like: | |
8297 | ||
8298 | template <class T> struct S { template <class U> void f(U); } | |
8299 | template <class T> template <> void S<T>::f(int) {} | |
8300 | ||
8301 | which we can spot because the pattern will be a | |
8302 | specialization in this case. */ | |
8303 | args_depth = TMPL_ARGS_DEPTH (args); | |
c8094d83 MS |
8304 | parms_depth = |
8305 | TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t))); | |
d8c4447d MM |
8306 | if (args_depth > parms_depth |
8307 | && !DECL_TEMPLATE_SPECIALIZATION (t)) | |
f9a7ae04 | 8308 | args = get_innermost_template_args (args, parms_depth); |
36a117a5 MM |
8309 | } |
8310 | else | |
8311 | { | |
8312 | /* This special case arises when we have something like this: | |
8313 | ||
0cbd7506 | 8314 | template <class T> struct S { |
c8094d83 | 8315 | friend void f<int>(int, double); |
36a117a5 MM |
8316 | }; |
8317 | ||
8318 | Here, the DECL_TI_TEMPLATE for the friend declaration | |
10b1d5e7 MM |
8319 | will be an IDENTIFIER_NODE. We are being called from |
8320 | tsubst_friend_function, and we want only to create a | |
8321 | new decl (R) with appropriate types so that we can call | |
8322 | determine_specialization. */ | |
36a117a5 MM |
8323 | gen_tmpl = NULL_TREE; |
8324 | } | |
8325 | ||
6eb3bb27 | 8326 | if (DECL_CLASS_SCOPE_P (t)) |
8d08fdba | 8327 | { |
5566b478 MS |
8328 | if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t))) |
8329 | member = 2; | |
8330 | else | |
8331 | member = 1; | |
c8094d83 | 8332 | ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, |
8c6ab2db | 8333 | complain, t, /*entering_scope=*/1); |
5566b478 MS |
8334 | } |
8335 | else | |
8336 | { | |
8337 | member = 0; | |
4f1c5b7d | 8338 | ctx = DECL_CONTEXT (t); |
5566b478 | 8339 | } |
fc6a28d7 | 8340 | type = tsubst (TREE_TYPE (t), args, complain, in_decl); |
b3da7bb1 MM |
8341 | if (type == error_mark_node) |
8342 | return error_mark_node; | |
8d08fdba | 8343 | |
5566b478 | 8344 | /* We do NOT check for matching decls pushed separately at this |
0cbd7506 MS |
8345 | point, as they may not represent instantiations of this |
8346 | template, and in any case are considered separate under the | |
8347 | discrete model. */ | |
0acf7199 | 8348 | r = copy_decl (t); |
e1467ff2 | 8349 | DECL_USE_TEMPLATE (r) = 0; |
5566b478 | 8350 | TREE_TYPE (r) = type; |
92643fea MM |
8351 | /* Clear out the mangled name and RTL for the instantiation. */ |
8352 | SET_DECL_ASSEMBLER_NAME (r, NULL_TREE); | |
8353 | SET_DECL_RTL (r, NULL_RTX); | |
9b26c96e JM |
8354 | /* Leave DECL_INITIAL set on deleted instantiations. */ |
8355 | if (!DECL_DELETED_FN (r)) | |
8356 | DECL_INITIAL (r) = NULL_TREE; | |
4f1c5b7d | 8357 | DECL_CONTEXT (r) = ctx; |
5566b478 | 8358 | |
c8094d83 | 8359 | if (member && DECL_CONV_FN_P (r)) |
1f84ec23 MM |
8360 | /* Type-conversion operator. Reconstruct the name, in |
8361 | case it's the name of one of the template's parameters. */ | |
8362 | DECL_NAME (r) = mangle_conv_op_name_for_type (TREE_TYPE (type)); | |
5566b478 | 8363 | |
4393e105 | 8364 | DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args, |
c2ea3a40 | 8365 | complain, t); |
477f6664 | 8366 | DECL_RESULT (r) = NULL_TREE; |
711734a9 JM |
8367 | |
8368 | TREE_STATIC (r) = 0; | |
8369 | TREE_PUBLIC (r) = TREE_PUBLIC (t); | |
8370 | DECL_EXTERNAL (r) = 1; | |
4684cd27 MM |
8371 | /* If this is an instantiation of a function with internal |
8372 | linkage, we already know what object file linkage will be | |
8373 | assigned to the instantiation. */ | |
8374 | DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r); | |
711734a9 JM |
8375 | DECL_DEFER_OUTPUT (r) = 0; |
8376 | TREE_CHAIN (r) = NULL_TREE; | |
8377 | DECL_PENDING_INLINE_INFO (r) = 0; | |
59026e79 | 8378 | DECL_PENDING_INLINE_P (r) = 0; |
655dc6ee | 8379 | DECL_SAVED_TREE (r) = NULL_TREE; |
29d7f409 | 8380 | DECL_STRUCT_FUNCTION (r) = NULL; |
711734a9 | 8381 | TREE_USED (r) = 0; |
db9b2174 MM |
8382 | if (DECL_CLONED_FUNCTION (r)) |
8383 | { | |
8384 | DECL_CLONED_FUNCTION (r) = tsubst (DECL_CLONED_FUNCTION (t), | |
c2ea3a40 | 8385 | args, complain, t); |
db9b2174 MM |
8386 | TREE_CHAIN (r) = TREE_CHAIN (DECL_CLONED_FUNCTION (r)); |
8387 | TREE_CHAIN (DECL_CLONED_FUNCTION (r)) = r; | |
8388 | } | |
711734a9 | 8389 | |
92643fea MM |
8390 | /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do |
8391 | this in the special friend case mentioned above where | |
8392 | GEN_TMPL is NULL. */ | |
36a117a5 | 8393 | if (gen_tmpl) |
386b8a85 | 8394 | { |
c8094d83 | 8395 | DECL_TEMPLATE_INFO (r) |
e1b3e07d | 8396 | = tree_cons (gen_tmpl, argvec, NULL_TREE); |
36a117a5 | 8397 | SET_DECL_IMPLICIT_INSTANTIATION (r); |
d63d5d0c | 8398 | register_specialization (r, gen_tmpl, argvec, false); |
36a117a5 | 8399 | |
9188c363 MM |
8400 | /* We're not supposed to instantiate default arguments |
8401 | until they are called, for a template. But, for a | |
8402 | declaration like: | |
8403 | ||
0cbd7506 MS |
8404 | template <class T> void f () |
8405 | { extern void g(int i = T()); } | |
c8094d83 | 8406 | |
9188c363 MM |
8407 | we should do the substitution when the template is |
8408 | instantiated. We handle the member function case in | |
8409 | instantiate_class_template since the default arguments | |
8410 | might refer to other members of the class. */ | |
8411 | if (!member | |
8412 | && !PRIMARY_TEMPLATE_P (gen_tmpl) | |
8413 | && !uses_template_parms (argvec)) | |
8414 | tsubst_default_arguments (r); | |
386b8a85 | 8415 | } |
c43e95f8 MM |
8416 | else |
8417 | DECL_TEMPLATE_INFO (r) = NULL_TREE; | |
f181d4ae | 8418 | |
cf38f48a MM |
8419 | /* Copy the list of befriending classes. */ |
8420 | for (friends = &DECL_BEFRIENDING_CLASSES (r); | |
8421 | *friends; | |
c8094d83 | 8422 | friends = &TREE_CHAIN (*friends)) |
cf38f48a MM |
8423 | { |
8424 | *friends = copy_node (*friends); | |
8425 | TREE_VALUE (*friends) = tsubst (TREE_VALUE (*friends), | |
c2ea3a40 | 8426 | args, complain, |
cf38f48a MM |
8427 | in_decl); |
8428 | } | |
8429 | ||
212e7048 | 8430 | if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r)) |
f181d4ae MM |
8431 | { |
8432 | maybe_retrofit_in_chrg (r); | |
212e7048 MM |
8433 | if (DECL_CONSTRUCTOR_P (r)) |
8434 | grok_ctor_properties (ctx, r); | |
2be678ff JM |
8435 | /* If this is an instantiation of a member template, clone it. |
8436 | If it isn't, that'll be handled by | |
8437 | clone_constructors_and_destructors. */ | |
5e818b93 | 8438 | if (PRIMARY_TEMPLATE_P (gen_tmpl)) |
271e6f02 | 8439 | clone_function_decl (r, /*update_method_vec_p=*/0); |
f181d4ae | 8440 | } |
398cd199 VR |
8441 | else if (IDENTIFIER_OPNAME_P (DECL_NAME (r)) |
8442 | && !grok_op_properties (r, (complain & tf_error) != 0)) | |
8443 | return error_mark_node; | |
c8b2e872 MM |
8444 | |
8445 | if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t)) | |
8446 | SET_DECL_FRIEND_CONTEXT (r, | |
8447 | tsubst (DECL_FRIEND_CONTEXT (t), | |
8448 | args, complain, in_decl)); | |
b9e75696 JM |
8449 | |
8450 | /* Possibly limit visibility based on template args. */ | |
8451 | DECL_VISIBILITY (r) = VISIBILITY_DEFAULT; | |
b70f0f48 JM |
8452 | if (DECL_VISIBILITY_SPECIFIED (t)) |
8453 | { | |
8454 | DECL_VISIBILITY_SPECIFIED (r) = 0; | |
8455 | DECL_ATTRIBUTES (r) | |
8456 | = remove_attribute ("visibility", DECL_ATTRIBUTES (r)); | |
8457 | } | |
b9e75696 | 8458 | determine_visibility (r); |
4f4141ff JM |
8459 | |
8460 | apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0, | |
8461 | args, complain, in_decl); | |
8d08fdba | 8462 | } |
ae58fa02 | 8463 | break; |
8d08fdba MS |
8464 | |
8465 | case PARM_DECL: | |
8466 | { | |
5d80a306 DG |
8467 | tree type = NULL_TREE; |
8468 | int i, len = 1; | |
8469 | tree expanded_types = NULL_TREE; | |
8470 | tree prev_r = NULL_TREE; | |
8471 | tree first_r = NULL_TREE; | |
1b8899d1 | 8472 | |
5d80a306 DG |
8473 | if (FUNCTION_PARAMETER_PACK_P (t)) |
8474 | { | |
8475 | /* If there is a local specialization that isn't a | |
8476 | parameter pack, it means that we're doing a "simple" | |
8477 | substitution from inside tsubst_pack_expansion. Just | |
a757b5c5 | 8478 | return the local specialization (which will be a single |
5d80a306 | 8479 | parm). */ |
29b0d1fd | 8480 | tree spec = retrieve_local_specialization (t); |
5d80a306 DG |
8481 | if (spec |
8482 | && TREE_CODE (spec) == PARM_DECL | |
8483 | && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION) | |
8484 | return spec; | |
8485 | ||
8486 | /* Expand the TYPE_PACK_EXPANSION that provides the types for | |
8487 | the parameters in this function parameter pack. */ | |
8488 | expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args, | |
8489 | complain, in_decl); | |
8490 | if (TREE_CODE (expanded_types) == TREE_VEC) | |
8491 | { | |
8492 | len = TREE_VEC_LENGTH (expanded_types); | |
8493 | ||
8494 | /* Zero-length parameter packs are boring. Just substitute | |
8495 | into the chain. */ | |
8496 | if (len == 0) | |
8497 | return tsubst (TREE_CHAIN (t), args, complain, | |
8498 | TREE_CHAIN (t)); | |
8499 | } | |
8500 | else | |
8501 | { | |
8502 | /* All we did was update the type. Make a note of that. */ | |
8503 | type = expanded_types; | |
8504 | expanded_types = NULL_TREE; | |
8505 | } | |
8506 | } | |
db2767b6 | 8507 | |
5d80a306 DG |
8508 | /* Loop through all of the parameter's we'll build. When T is |
8509 | a function parameter pack, LEN is the number of expanded | |
8510 | types in EXPANDED_TYPES; otherwise, LEN is 1. */ | |
8511 | r = NULL_TREE; | |
8512 | for (i = 0; i < len; ++i) | |
8513 | { | |
8514 | prev_r = r; | |
8515 | r = copy_node (t); | |
8516 | if (DECL_TEMPLATE_PARM_P (t)) | |
8517 | SET_DECL_TEMPLATE_PARM_P (r); | |
8518 | ||
8519 | if (expanded_types) | |
8520 | /* We're on the Ith parameter of the function parameter | |
8521 | pack. */ | |
8522 | { | |
8523 | /* Get the Ith type. */ | |
8524 | type = TREE_VEC_ELT (expanded_types, i); | |
8525 | ||
8526 | if (DECL_NAME (r)) | |
8527 | /* Rename the parameter to include the index. */ | |
8528 | DECL_NAME (r) = | |
8529 | make_ith_pack_parameter_name (DECL_NAME (r), i); | |
8530 | } | |
8531 | else if (!type) | |
8532 | /* We're dealing with a normal parameter. */ | |
8533 | type = tsubst (TREE_TYPE (t), args, complain, in_decl); | |
8534 | ||
8535 | type = type_decays_to (type); | |
8536 | TREE_TYPE (r) = type; | |
8537 | cp_apply_type_quals_to_decl (cp_type_quals (type), r); | |
8538 | ||
8539 | if (DECL_INITIAL (r)) | |
8540 | { | |
8541 | if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX) | |
8542 | DECL_INITIAL (r) = TREE_TYPE (r); | |
8543 | else | |
8544 | DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args, | |
8545 | complain, in_decl); | |
8546 | } | |
8547 | ||
8548 | DECL_CONTEXT (r) = NULL_TREE; | |
8549 | ||
8550 | if (!DECL_TEMPLATE_PARM_P (r)) | |
8551 | DECL_ARG_TYPE (r) = type_passed_as (type); | |
8552 | ||
4f4141ff JM |
8553 | apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0, |
8554 | args, complain, in_decl); | |
8555 | ||
5d80a306 DG |
8556 | /* Keep track of the first new parameter we |
8557 | generate. That's what will be returned to the | |
8558 | caller. */ | |
8559 | if (!first_r) | |
8560 | first_r = r; | |
8561 | ||
8562 | /* Build a proper chain of parameters when substituting | |
8563 | into a function parameter pack. */ | |
8564 | if (prev_r) | |
8565 | TREE_CHAIN (prev_r) = r; | |
8566 | } | |
8e51619a | 8567 | |
8d08fdba | 8568 | if (TREE_CHAIN (t)) |
4393e105 | 8569 | TREE_CHAIN (r) = tsubst (TREE_CHAIN (t), args, |
c2ea3a40 | 8570 | complain, TREE_CHAIN (t)); |
5d80a306 DG |
8571 | |
8572 | /* FIRST_R contains the start of the chain we've built. */ | |
8573 | r = first_r; | |
8d08fdba | 8574 | } |
ae58fa02 | 8575 | break; |
8d08fdba | 8576 | |
5566b478 MS |
8577 | case FIELD_DECL: |
8578 | { | |
fc6a28d7 MM |
8579 | tree type; |
8580 | ||
0acf7199 | 8581 | r = copy_decl (t); |
fc6a28d7 MM |
8582 | type = tsubst (TREE_TYPE (t), args, complain, in_decl); |
8583 | if (type == error_mark_node) | |
8584 | return error_mark_node; | |
1b8899d1 | 8585 | TREE_TYPE (r) = type; |
9804209d | 8586 | cp_apply_type_quals_to_decl (cp_type_quals (type), r); |
1b8899d1 | 8587 | |
015c2c66 MM |
8588 | /* DECL_INITIAL gives the number of bits in a bit-field. */ |
8589 | DECL_INITIAL (r) | |
8590 | = tsubst_expr (DECL_INITIAL (t), args, | |
8591 | complain, in_decl, | |
8592 | /*integral_constant_expression_p=*/true); | |
1b8899d1 MM |
8593 | /* We don't have to set DECL_CONTEXT here; it is set by |
8594 | finish_member_declaration. */ | |
5566b478 | 8595 | TREE_CHAIN (r) = NULL_TREE; |
c8094d83 | 8596 | if (VOID_TYPE_P (type)) |
dee15844 | 8597 | error ("instantiation of %q+D as type %qT", r, type); |
4f4141ff JM |
8598 | |
8599 | apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0, | |
8600 | args, complain, in_decl); | |
5566b478 | 8601 | } |
ae58fa02 | 8602 | break; |
5566b478 MS |
8603 | |
8604 | case USING_DECL: | |
98ed9dae NS |
8605 | /* We reach here only for member using decls. */ |
8606 | if (DECL_DEPENDENT_P (t)) | |
8607 | { | |
8608 | r = do_class_using_decl | |
8609 | (tsubst_copy (USING_DECL_SCOPE (t), args, complain, in_decl), | |
8610 | tsubst_copy (DECL_NAME (t), args, complain, in_decl)); | |
8611 | if (!r) | |
8612 | r = error_mark_node; | |
db8470f6 JM |
8613 | else |
8614 | { | |
8615 | TREE_PROTECTED (r) = TREE_PROTECTED (t); | |
8616 | TREE_PRIVATE (r) = TREE_PRIVATE (t); | |
8617 | } | |
98ed9dae NS |
8618 | } |
8619 | else | |
8620 | { | |
8621 | r = copy_node (t); | |
8622 | TREE_CHAIN (r) = NULL_TREE; | |
8623 | } | |
ae58fa02 | 8624 | break; |
5566b478 | 8625 | |
9188c363 | 8626 | case TYPE_DECL: |
5566b478 MS |
8627 | case VAR_DECL: |
8628 | { | |
1cea0434 KG |
8629 | tree argvec = NULL_TREE; |
8630 | tree gen_tmpl = NULL_TREE; | |
36a117a5 | 8631 | tree spec; |
1cea0434 | 8632 | tree tmpl = NULL_TREE; |
9188c363 | 8633 | tree ctx; |
fc6a28d7 | 8634 | tree type = NULL_TREE; |
f604fc3b | 8635 | bool local_p; |
9188c363 | 8636 | |
56d0c6e3 | 8637 | if (TREE_CODE (t) == TYPE_DECL |
a7f6bc8c | 8638 | && t == TYPE_MAIN_DECL (TREE_TYPE (t))) |
fc6a28d7 | 8639 | { |
a7f6bc8c JM |
8640 | /* If this is the canonical decl, we don't have to |
8641 | mess with instantiations, and often we can't (for | |
8642 | typename, template type parms and such). Note that | |
8643 | TYPE_NAME is not correct for the above test if | |
8644 | we've copied the type for a typedef. */ | |
fc6a28d7 | 8645 | type = tsubst (TREE_TYPE (t), args, complain, in_decl); |
c343d5a7 PC |
8646 | if (type == error_mark_node) |
8647 | return error_mark_node; | |
a7f6bc8c JM |
8648 | r = TYPE_NAME (type); |
8649 | break; | |
fc6a28d7 | 8650 | } |
c8094d83 | 8651 | |
f604fc3b MM |
8652 | /* Check to see if we already have the specialization we |
8653 | need. */ | |
8654 | spec = NULL_TREE; | |
8655 | if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t)) | |
8656 | { | |
8657 | /* T is a static data member or namespace-scope entity. | |
8658 | We have to substitute into namespace-scope variables | |
8659 | (even though such entities are never templates) because | |
8660 | of cases like: | |
8661 | ||
8662 | template <class T> void f() { extern T t; } | |
8663 | ||
8664 | where the entity referenced is not known until | |
8665 | instantiation time. */ | |
8666 | local_p = false; | |
8667 | ctx = DECL_CONTEXT (t); | |
8668 | if (DECL_CLASS_SCOPE_P (t)) | |
8669 | { | |
8670 | ctx = tsubst_aggr_type (ctx, args, | |
8671 | complain, | |
8672 | in_decl, /*entering_scope=*/1); | |
8673 | /* If CTX is unchanged, then T is in fact the | |
8674 | specialization we want. That situation occurs when | |
8675 | referencing a static data member within in its own | |
8676 | class. We can use pointer equality, rather than | |
8677 | same_type_p, because DECL_CONTEXT is always | |
8678 | canonical. */ | |
8679 | if (ctx == DECL_CONTEXT (t)) | |
8680 | spec = t; | |
8681 | } | |
5566b478 | 8682 | |
f604fc3b MM |
8683 | if (!spec) |
8684 | { | |
8685 | tmpl = DECL_TI_TEMPLATE (t); | |
8686 | gen_tmpl = most_general_template (tmpl); | |
8687 | argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl); | |
8688 | spec = (retrieve_specialization | |
8689 | (gen_tmpl, argvec, | |
8690 | /*class_specializations_p=*/false)); | |
8691 | } | |
8692 | } | |
9188c363 | 8693 | else |
6dfbb909 | 8694 | { |
f604fc3b MM |
8695 | /* A local variable. */ |
8696 | local_p = true; | |
6dfbb909 MM |
8697 | /* Subsequent calls to pushdecl will fill this in. */ |
8698 | ctx = NULL_TREE; | |
f604fc3b | 8699 | spec = retrieve_local_specialization (t); |
6dfbb909 | 8700 | } |
f604fc3b MM |
8701 | /* If we already have the specialization we need, there is |
8702 | nothing more to do. */ | |
36a117a5 | 8703 | if (spec) |
ae58fa02 MM |
8704 | { |
8705 | r = spec; | |
8706 | break; | |
8707 | } | |
5566b478 | 8708 | |
f604fc3b | 8709 | /* Create a new node for the specialization we need. */ |
0acf7199 | 8710 | r = copy_decl (t); |
56d0c6e3 JM |
8711 | if (type == NULL_TREE) |
8712 | type = tsubst (TREE_TYPE (t), args, complain, in_decl); | |
edebf865 | 8713 | if (TREE_CODE (r) == VAR_DECL) |
39703eb9 | 8714 | { |
8b0a8500 MM |
8715 | /* Even if the original location is out of scope, the |
8716 | newly substituted one is not. */ | |
8717 | DECL_DEAD_FOR_LOCAL (r) = 0; | |
8718 | DECL_INITIALIZED_P (r) = 0; | |
8719 | DECL_TEMPLATE_INSTANTIATED (r) = 0; | |
fc6a28d7 MM |
8720 | if (type == error_mark_node) |
8721 | return error_mark_node; | |
2c05d05e MM |
8722 | if (TREE_CODE (type) == FUNCTION_TYPE) |
8723 | { | |
8724 | /* It may seem that this case cannot occur, since: | |
8725 | ||
8726 | typedef void f(); | |
8727 | void g() { f x; } | |
8728 | ||
8729 | declares a function, not a variable. However: | |
8730 | ||
8731 | typedef void f(); | |
8732 | template <typename T> void g() { T t; } | |
8733 | template void g<f>(); | |
8734 | ||
8735 | is an attempt to declare a variable with function | |
8736 | type. */ | |
8737 | error ("variable %qD has function type", | |
8738 | /* R is not yet sufficiently initialized, so we | |
8739 | just use its name. */ | |
8740 | DECL_NAME (r)); | |
8741 | return error_mark_node; | |
8742 | } | |
39703eb9 MM |
8743 | type = complete_type (type); |
8744 | DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) | |
8745 | = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t); | |
62e19030 | 8746 | type = check_var_type (DECL_NAME (r), type); |
56b4ea3d RH |
8747 | |
8748 | if (DECL_HAS_VALUE_EXPR_P (t)) | |
8749 | { | |
8750 | tree ve = DECL_VALUE_EXPR (t); | |
015c2c66 MM |
8751 | ve = tsubst_expr (ve, args, complain, in_decl, |
8752 | /*constant_expression_p=*/false); | |
3db45ab5 | 8753 | SET_DECL_VALUE_EXPR (r, ve); |
56b4ea3d | 8754 | } |
39703eb9 | 8755 | } |
a3d87771 MM |
8756 | else if (DECL_SELF_REFERENCE_P (t)) |
8757 | SET_DECL_SELF_REFERENCE_P (r); | |
01f4137f | 8758 | TREE_TYPE (r) = type; |
9804209d | 8759 | cp_apply_type_quals_to_decl (cp_type_quals (type), r); |
5566b478 | 8760 | DECL_CONTEXT (r) = ctx; |
92643fea MM |
8761 | /* Clear out the mangled name and RTL for the instantiation. */ |
8762 | SET_DECL_ASSEMBLER_NAME (r, NULL_TREE); | |
820cc88f DB |
8763 | if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL)) |
8764 | SET_DECL_RTL (r, NULL_RTX); | |
8b0a8500 MM |
8765 | /* The initializer must not be expanded until it is required; |
8766 | see [temp.inst]. */ | |
d11ad92e | 8767 | DECL_INITIAL (r) = NULL_TREE; |
820cc88f DB |
8768 | if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL)) |
8769 | SET_DECL_RTL (r, NULL_RTX); | |
06ceef4e | 8770 | DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0; |
b9e75696 JM |
8771 | if (TREE_CODE (r) == VAR_DECL) |
8772 | { | |
8773 | /* Possibly limit visibility based on template args. */ | |
8774 | DECL_VISIBILITY (r) = VISIBILITY_DEFAULT; | |
b70f0f48 JM |
8775 | if (DECL_VISIBILITY_SPECIFIED (t)) |
8776 | { | |
8777 | DECL_VISIBILITY_SPECIFIED (r) = 0; | |
8778 | DECL_ATTRIBUTES (r) | |
8779 | = remove_attribute ("visibility", DECL_ATTRIBUTES (r)); | |
8780 | } | |
b9e75696 JM |
8781 | determine_visibility (r); |
8782 | } | |
5044ab0e JM |
8783 | /* Preserve a typedef that names a type. */ |
8784 | else if (TREE_CODE (r) == TYPE_DECL | |
8785 | && DECL_ORIGINAL_TYPE (t) | |
8786 | && type != error_mark_node) | |
8787 | { | |
8788 | DECL_ORIGINAL_TYPE (r) = tsubst (DECL_ORIGINAL_TYPE (t), | |
8789 | args, complain, in_decl); | |
8790 | TREE_TYPE (r) = type = build_variant_type_copy (type); | |
8791 | TYPE_NAME (type) = r; | |
8792 | } | |
5566b478 | 8793 | |
6dfbb909 MM |
8794 | if (!local_p) |
8795 | { | |
8796 | /* A static data member declaration is always marked | |
8797 | external when it is declared in-class, even if an | |
8798 | initializer is present. We mimic the non-template | |
8799 | processing here. */ | |
8800 | DECL_EXTERNAL (r) = 1; | |
fa8d6e85 | 8801 | |
d63d5d0c | 8802 | register_specialization (r, gen_tmpl, argvec, false); |
6dfbb909 MM |
8803 | DECL_TEMPLATE_INFO (r) = tree_cons (tmpl, argvec, NULL_TREE); |
8804 | SET_DECL_IMPLICIT_INSTANTIATION (r); | |
8805 | } | |
9188c363 | 8806 | else |
6dfbb909 | 8807 | register_local_specialization (r, t); |
5566b478 | 8808 | |
5566b478 | 8809 | TREE_CHAIN (r) = NULL_TREE; |
4f4141ff | 8810 | |
5044ab0e JM |
8811 | apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), |
8812 | (int) ATTR_FLAG_TYPE_IN_PLACE, | |
4f4141ff | 8813 | args, complain, in_decl); |
edebf865 | 8814 | layout_decl (r, 0); |
5566b478 | 8815 | } |
ae58fa02 | 8816 | break; |
5566b478 | 8817 | |
ae58fa02 | 8818 | default: |
315fb5db | 8819 | gcc_unreachable (); |
c8094d83 | 8820 | } |
ae58fa02 MM |
8821 | |
8822 | /* Restore the file and line information. */ | |
82a98427 | 8823 | input_location = saved_loc; |
ae58fa02 MM |
8824 | |
8825 | return r; | |
8826 | } | |
8827 | ||
34cd5ae7 | 8828 | /* Substitute into the ARG_TYPES of a function type. */ |
cabc336a | 8829 | |
e9659ab0 | 8830 | static tree |
c8094d83 | 8831 | tsubst_arg_types (tree arg_types, |
0cbd7506 MS |
8832 | tree args, |
8833 | tsubst_flags_t complain, | |
8834 | tree in_decl) | |
cabc336a MM |
8835 | { |
8836 | tree remaining_arg_types; | |
5d80a306 DG |
8837 | tree type = NULL_TREE; |
8838 | int i = 1; | |
8839 | tree expanded_args = NULL_TREE; | |
5e97d404 | 8840 | tree default_arg; |
cabc336a MM |
8841 | |
8842 | if (!arg_types || arg_types == void_list_node) | |
8843 | return arg_types; | |
c8094d83 | 8844 | |
cabc336a | 8845 | remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types), |
4393e105 MM |
8846 | args, complain, in_decl); |
8847 | if (remaining_arg_types == error_mark_node) | |
8848 | return error_mark_node; | |
8849 | ||
5d80a306 | 8850 | if (PACK_EXPANSION_P (TREE_VALUE (arg_types))) |
4b2811e9 | 8851 | { |
5d80a306 DG |
8852 | /* For a pack expansion, perform substitution on the |
8853 | entire expression. Later on, we'll handle the arguments | |
8854 | one-by-one. */ | |
8855 | expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types), | |
8856 | args, complain, in_decl); | |
cabc336a | 8857 | |
5d80a306 DG |
8858 | if (TREE_CODE (expanded_args) == TREE_VEC) |
8859 | /* So that we'll spin through the parameters, one by one. */ | |
8860 | i = TREE_VEC_LENGTH (expanded_args); | |
8861 | else | |
8862 | { | |
8863 | /* We only partially substituted into the parameter | |
8864 | pack. Our type is TYPE_PACK_EXPANSION. */ | |
8865 | type = expanded_args; | |
8866 | expanded_args = NULL_TREE; | |
8867 | } | |
8868 | } | |
cabc336a | 8869 | |
5d80a306 DG |
8870 | while (i > 0) { |
8871 | --i; | |
8872 | ||
8873 | if (expanded_args) | |
8874 | type = TREE_VEC_ELT (expanded_args, i); | |
8875 | else if (!type) | |
8876 | type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl); | |
c8094d83 | 8877 | |
5d80a306 DG |
8878 | if (type == error_mark_node) |
8879 | return error_mark_node; | |
8880 | if (VOID_TYPE_P (type)) | |
8881 | { | |
8882 | if (complain & tf_error) | |
8883 | { | |
8884 | error ("invalid parameter type %qT", type); | |
8885 | if (in_decl) | |
8886 | error ("in declaration %q+D", in_decl); | |
8887 | } | |
8888 | return error_mark_node; | |
5e97d404 | 8889 | } |
5d80a306 DG |
8890 | |
8891 | /* Do array-to-pointer, function-to-pointer conversion, and ignore | |
8892 | top-level qualifiers as required. */ | |
8893 | type = TYPE_MAIN_VARIANT (type_decays_to (type)); | |
c8094d83 | 8894 | |
5d80a306 DG |
8895 | /* We do not substitute into default arguments here. The standard |
8896 | mandates that they be instantiated only when needed, which is | |
8897 | done in build_over_call. */ | |
8898 | default_arg = TREE_PURPOSE (arg_types); | |
8899 | ||
8900 | if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG) | |
8901 | { | |
8902 | /* We've instantiated a template before its default arguments | |
8903 | have been parsed. This can happen for a nested template | |
8904 | class, and is not an error unless we require the default | |
8905 | argument in a call of this function. */ | |
8906 | remaining_arg_types = | |
8907 | tree_cons (default_arg, type, remaining_arg_types); | |
8908 | VEC_safe_push (tree, gc, DEFARG_INSTANTIATIONS (default_arg), | |
8909 | remaining_arg_types); | |
8910 | } | |
8911 | else | |
8912 | remaining_arg_types = | |
8913 | hash_tree_cons (default_arg, type, remaining_arg_types); | |
8914 | } | |
8915 | ||
8916 | return remaining_arg_types; | |
cabc336a MM |
8917 | } |
8918 | ||
4393e105 MM |
8919 | /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does |
8920 | *not* handle the exception-specification for FNTYPE, because the | |
8921 | initial substitution of explicitly provided template parameters | |
8922 | during argument deduction forbids substitution into the | |
8923 | exception-specification: | |
8924 | ||
8925 | [temp.deduct] | |
8926 | ||
8927 | All references in the function type of the function template to the | |
8928 | corresponding template parameters are replaced by the specified tem- | |
8929 | plate argument values. If a substitution in a template parameter or | |
8930 | in the function type of the function template results in an invalid | |
8931 | type, type deduction fails. [Note: The equivalent substitution in | |
8932 | exception specifications is done only when the function is instanti- | |
8933 | ated, at which point a program is ill-formed if the substitution | |
8934 | results in an invalid type.] */ | |
8935 | ||
8936 | static tree | |
c8094d83 | 8937 | tsubst_function_type (tree t, |
0cbd7506 MS |
8938 | tree args, |
8939 | tsubst_flags_t complain, | |
8940 | tree in_decl) | |
4393e105 MM |
8941 | { |
8942 | tree return_type; | |
8943 | tree arg_types; | |
8944 | tree fntype; | |
8945 | ||
8dd3f57a | 8946 | /* The TYPE_CONTEXT is not used for function/method types. */ |
50bc768d | 8947 | gcc_assert (TYPE_CONTEXT (t) == NULL_TREE); |
4393e105 | 8948 | |
46c895ac | 8949 | /* Substitute the return type. */ |
4393e105 MM |
8950 | return_type = tsubst (TREE_TYPE (t), args, complain, in_decl); |
8951 | if (return_type == error_mark_node) | |
8952 | return error_mark_node; | |
6e2993bf MM |
8953 | /* The standard does not presently indicate that creation of a |
8954 | function type with an invalid return type is a deduction failure. | |
270af55d | 8955 | However, that is clearly analogous to creating an array of "void" |
c8094d83 | 8956 | or a reference to a reference. This is core issue #486. */ |
6e2993bf MM |
8957 | if (TREE_CODE (return_type) == ARRAY_TYPE |
8958 | || TREE_CODE (return_type) == FUNCTION_TYPE) | |
8959 | { | |
8960 | if (complain & tf_error) | |
8961 | { | |
8962 | if (TREE_CODE (return_type) == ARRAY_TYPE) | |
8963 | error ("function returning an array"); | |
8964 | else | |
8965 | error ("function returning a function"); | |
8966 | } | |
8967 | return error_mark_node; | |
8968 | } | |
4393e105 | 8969 | |
34cd5ae7 | 8970 | /* Substitute the argument types. */ |
4393e105 | 8971 | arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, |
c8094d83 | 8972 | complain, in_decl); |
4393e105 MM |
8973 | if (arg_types == error_mark_node) |
8974 | return error_mark_node; | |
c8094d83 | 8975 | |
4393e105 MM |
8976 | /* Construct a new type node and return it. */ |
8977 | if (TREE_CODE (t) == FUNCTION_TYPE) | |
8978 | fntype = build_function_type (return_type, arg_types); | |
8979 | else | |
8980 | { | |
8981 | tree r = TREE_TYPE (TREE_VALUE (arg_types)); | |
9e1e64ec | 8982 | if (! MAYBE_CLASS_TYPE_P (r)) |
4393e105 MM |
8983 | { |
8984 | /* [temp.deduct] | |
c8094d83 | 8985 | |
4393e105 MM |
8986 | Type deduction may fail for any of the following |
8987 | reasons: | |
c8094d83 | 8988 | |
4393e105 MM |
8989 | -- Attempting to create "pointer to member of T" when T |
8990 | is not a class type. */ | |
c2ea3a40 | 8991 | if (complain & tf_error) |
0f51ccfc | 8992 | error ("creating pointer to member function of non-class type %qT", |
4393e105 MM |
8993 | r); |
8994 | return error_mark_node; | |
8995 | } | |
c8094d83 MS |
8996 | |
8997 | fntype = build_method_type_directly (r, return_type, | |
43dc123f | 8998 | TREE_CHAIN (arg_types)); |
4393e105 | 8999 | } |
c2ea3a40 | 9000 | fntype = cp_build_qualified_type_real (fntype, TYPE_QUALS (t), complain); |
e9525111 | 9001 | fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t)); |
c8094d83 MS |
9002 | |
9003 | return fntype; | |
4393e105 MM |
9004 | } |
9005 | ||
c7222c02 MM |
9006 | /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template |
9007 | ARGS into that specification, and return the substituted | |
9008 | specification. If there is no specification, return NULL_TREE. */ | |
9009 | ||
9010 | static tree | |
c8094d83 MS |
9011 | tsubst_exception_specification (tree fntype, |
9012 | tree args, | |
c7222c02 MM |
9013 | tsubst_flags_t complain, |
9014 | tree in_decl) | |
9015 | { | |
9016 | tree specs; | |
9017 | tree new_specs; | |
9018 | ||
9019 | specs = TYPE_RAISES_EXCEPTIONS (fntype); | |
9020 | new_specs = NULL_TREE; | |
9021 | if (specs) | |
9022 | { | |
9023 | if (! TREE_VALUE (specs)) | |
9024 | new_specs = specs; | |
9025 | else | |
9026 | while (specs) | |
9027 | { | |
9028 | tree spec; | |
5d80a306 DG |
9029 | int i, len = 1; |
9030 | tree expanded_specs = NULL_TREE; | |
9031 | ||
9032 | if (PACK_EXPANSION_P (TREE_VALUE (specs))) | |
9033 | { | |
9034 | /* Expand the pack expansion type. */ | |
9035 | expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs), | |
9036 | args, complain, | |
9037 | in_decl); | |
2f93f02c DG |
9038 | |
9039 | if (expanded_specs == error_mark_node) | |
9040 | return error_mark_node; | |
9041 | else if (TREE_CODE (expanded_specs) == TREE_VEC) | |
9042 | len = TREE_VEC_LENGTH (expanded_specs); | |
9043 | else | |
9044 | { | |
9045 | /* We're substituting into a member template, so | |
9046 | we got a TYPE_PACK_EXPANSION back. Add that | |
9047 | expansion and move on. */ | |
9048 | gcc_assert (TREE_CODE (expanded_specs) | |
9049 | == TYPE_PACK_EXPANSION); | |
9050 | new_specs = add_exception_specifier (new_specs, | |
9051 | expanded_specs, | |
9052 | complain); | |
9053 | specs = TREE_CHAIN (specs); | |
9054 | continue; | |
9055 | } | |
5d80a306 DG |
9056 | } |
9057 | ||
9058 | for (i = 0; i < len; ++i) | |
9059 | { | |
9060 | if (expanded_specs) | |
9061 | spec = TREE_VEC_ELT (expanded_specs, i); | |
9062 | else | |
9063 | spec = tsubst (TREE_VALUE (specs), args, complain, in_decl); | |
9064 | if (spec == error_mark_node) | |
9065 | return spec; | |
9066 | new_specs = add_exception_specifier (new_specs, spec, | |
9067 | complain); | |
9068 | } | |
9069 | ||
9070 | specs = TREE_CHAIN (specs); | |
c7222c02 MM |
9071 | } |
9072 | } | |
9073 | return new_specs; | |
9074 | } | |
9075 | ||
4393e105 MM |
9076 | /* Take the tree structure T and replace template parameters used |
9077 | therein with the argument vector ARGS. IN_DECL is an associated | |
9078 | decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE. | |
c2ea3a40 NS |
9079 | Issue error and warning messages under control of COMPLAIN. Note |
9080 | that we must be relatively non-tolerant of extensions here, in | |
9081 | order to preserve conformance; if we allow substitutions that | |
9082 | should not be allowed, we may allow argument deductions that should | |
9083 | not succeed, and therefore report ambiguous overload situations | |
9084 | where there are none. In theory, we could allow the substitution, | |
9085 | but indicate that it should have failed, and allow our caller to | |
9086 | make sure that the right thing happens, but we don't try to do this | |
9087 | yet. | |
4393e105 MM |
9088 | |
9089 | This function is used for dealing with types, decls and the like; | |
9090 | for expressions, use tsubst_expr or tsubst_copy. */ | |
ae58fa02 | 9091 | |
d5c8be27 | 9092 | tree |
3a978d72 | 9093 | tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl) |
ae58fa02 | 9094 | { |
0ecfe0b4 | 9095 | tree type, r; |
ae58fa02 MM |
9096 | |
9097 | if (t == NULL_TREE || t == error_mark_node | |
9098 | || t == integer_type_node | |
9099 | || t == void_type_node | |
9100 | || t == char_type_node | |
539599c1 | 9101 | || t == unknown_type_node |
ae58fa02 MM |
9102 | || TREE_CODE (t) == NAMESPACE_DECL) |
9103 | return t; | |
9104 | ||
fc6a28d7 MM |
9105 | if (DECL_P (t)) |
9106 | return tsubst_decl (t, args, complain); | |
9107 | ||
5fa0e853 SE |
9108 | if (args == NULL_TREE) |
9109 | return t; | |
9110 | ||
ae58fa02 MM |
9111 | if (TREE_CODE (t) == IDENTIFIER_NODE) |
9112 | type = IDENTIFIER_TYPE_VALUE (t); | |
9113 | else | |
9114 | type = TREE_TYPE (t); | |
399dedb9 | 9115 | |
50bc768d | 9116 | gcc_assert (type != unknown_type_node); |
ae58fa02 | 9117 | |
56d0c6e3 | 9118 | /* Reuse typedefs. We need to do this to handle dependent attributes, |
a7f6bc8c | 9119 | such as attribute aligned. */ |
5044ab0e JM |
9120 | if (TYPE_P (t) |
9121 | && TYPE_NAME (t) | |
a7f6bc8c | 9122 | && TYPE_NAME (t) != TYPE_MAIN_DECL (t)) |
5044ab0e JM |
9123 | { |
9124 | tree decl = TYPE_NAME (t); | |
56d0c6e3 | 9125 | |
a7f6bc8c | 9126 | if (DECL_CLASS_SCOPE_P (decl) |
02e52ae5 JM |
9127 | && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl)) |
9128 | && uses_template_parms (DECL_CONTEXT (decl))) | |
56d0c6e3 JM |
9129 | { |
9130 | tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl)); | |
9131 | tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl); | |
9132 | r = retrieve_specialization (tmpl, gen_args, false); | |
9133 | } | |
29b0d1fd | 9134 | else if (DECL_FUNCTION_SCOPE_P (decl) |
02e52ae5 JM |
9135 | && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl)) |
9136 | && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl)))) | |
56d0c6e3 JM |
9137 | r = retrieve_local_specialization (decl); |
9138 | else | |
29b0d1fd JM |
9139 | /* The typedef is from a non-template context. */ |
9140 | return t; | |
9141 | ||
56d0c6e3 JM |
9142 | if (r) |
9143 | { | |
9144 | r = TREE_TYPE (r); | |
9145 | r = cp_build_qualified_type_real | |
9146 | (r, cp_type_quals (t) | cp_type_quals (r), | |
9147 | complain | tf_ignore_bad_quals); | |
9148 | return r; | |
9149 | } | |
9150 | /* Else we must be instantiating the typedef, so fall through. */ | |
5044ab0e JM |
9151 | } |
9152 | ||
fc6a28d7 | 9153 | if (type |
ae58fa02 | 9154 | && TREE_CODE (t) != TYPENAME_TYPE |
4393e105 MM |
9155 | && TREE_CODE (t) != IDENTIFIER_NODE |
9156 | && TREE_CODE (t) != FUNCTION_TYPE | |
9157 | && TREE_CODE (t) != METHOD_TYPE) | |
9158 | type = tsubst (type, args, complain, in_decl); | |
9159 | if (type == error_mark_node) | |
9160 | return error_mark_node; | |
ae58fa02 | 9161 | |
ae58fa02 MM |
9162 | switch (TREE_CODE (t)) |
9163 | { | |
9164 | case RECORD_TYPE: | |
9165 | case UNION_TYPE: | |
9166 | case ENUMERAL_TYPE: | |
4393e105 MM |
9167 | return tsubst_aggr_type (t, args, complain, in_decl, |
9168 | /*entering_scope=*/0); | |
ae58fa02 MM |
9169 | |
9170 | case ERROR_MARK: | |
9171 | case IDENTIFIER_NODE: | |
ae58fa02 MM |
9172 | case VOID_TYPE: |
9173 | case REAL_TYPE: | |
9174 | case COMPLEX_TYPE: | |
c00996a3 | 9175 | case VECTOR_TYPE: |
ae58fa02 MM |
9176 | case BOOLEAN_TYPE: |
9177 | case INTEGER_CST: | |
9178 | case REAL_CST: | |
9179 | case STRING_CST: | |
9180 | return t; | |
9181 | ||
9182 | case INTEGER_TYPE: | |
9183 | if (t == integer_type_node) | |
9184 | return t; | |
9185 | ||
9186 | if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST | |
9187 | && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST) | |
9188 | return t; | |
d2e5ee5c | 9189 | |
5566b478 | 9190 | { |
ddce3528 | 9191 | tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0); |
7ddedda4 | 9192 | |
6ee3ffe8 MM |
9193 | max = tsubst_expr (omax, args, complain, in_decl, |
9194 | /*integral_constant_expression_p=*/false); | |
5bbca039 JJ |
9195 | |
9196 | /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if | |
9197 | needed. */ | |
9198 | if (TREE_CODE (max) == NOP_EXPR | |
9199 | && TREE_SIDE_EFFECTS (omax) | |
9200 | && !TREE_TYPE (max)) | |
9201 | TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0)); | |
9202 | ||
4ef69b83 | 9203 | max = fold_decl_constant_value (max); |
8dd3f57a | 9204 | |
c1165535 JM |
9205 | /* If we're in a partial instantiation, preserve the magic NOP_EXPR |
9206 | with TREE_SIDE_EFFECTS that indicates this is not an integral | |
9207 | constant expression. */ | |
9208 | if (processing_template_decl | |
9209 | && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR) | |
9210 | { | |
9211 | gcc_assert (TREE_CODE (max) == NOP_EXPR); | |
9212 | TREE_SIDE_EFFECTS (max) = 1; | |
9213 | } | |
9214 | ||
6a279e92 JJ |
9215 | if (TREE_CODE (max) != INTEGER_CST |
9216 | && !at_function_scope_p () | |
c1165535 | 9217 | && !TREE_SIDE_EFFECTS (max) |
6a279e92 | 9218 | && !value_dependent_expression_p (max)) |
eff3a276 MM |
9219 | { |
9220 | if (complain & tf_error) | |
9221 | error ("array bound is not an integer constant"); | |
9222 | return error_mark_node; | |
9223 | } | |
9224 | ||
95cd6f6f | 9225 | /* [temp.deduct] |
4393e105 | 9226 | |
95cd6f6f JC |
9227 | Type deduction may fail for any of the following |
9228 | reasons: | |
4393e105 | 9229 | |
95cd6f6f JC |
9230 | Attempting to create an array with a size that is |
9231 | zero or negative. */ | |
9232 | if (integer_zerop (max) && !(complain & tf_error)) | |
9233 | /* We must fail if performing argument deduction (as | |
9234 | indicated by the state of complain), so that | |
9235 | another substitution can be found. */ | |
9236 | return error_mark_node; | |
95cd6f6f JC |
9237 | else if (TREE_CODE (max) == INTEGER_CST |
9238 | && INT_CST_LT (max, integer_zero_node)) | |
9239 | { | |
c2ea3a40 | 9240 | if (complain & tf_error) |
95cd6f6f | 9241 | error ("creating array with negative size (%qE)", max); |
4393e105 MM |
9242 | |
9243 | return error_mark_node; | |
0ecfe0b4 JM |
9244 | } |
9245 | ||
c95cd22e | 9246 | return compute_array_index_type (NULL_TREE, max); |
ae58fa02 MM |
9247 | } |
9248 | ||
9249 | case TEMPLATE_TYPE_PARM: | |
9250 | case TEMPLATE_TEMPLATE_PARM: | |
a1281f45 | 9251 | case BOUND_TEMPLATE_TEMPLATE_PARM: |
ae58fa02 MM |
9252 | case TEMPLATE_PARM_INDEX: |
9253 | { | |
9254 | int idx; | |
9255 | int level; | |
9256 | int levels; | |
315fb5db | 9257 | tree arg = NULL_TREE; |
0ecfe0b4 JM |
9258 | |
9259 | r = NULL_TREE; | |
ae58fa02 | 9260 | |
315fb5db | 9261 | gcc_assert (TREE_VEC_LENGTH (args) > 0); |
f4205442 | 9262 | template_parm_level_and_index (t, &level, &idx); |
ae58fa02 | 9263 | |
315fb5db NS |
9264 | levels = TMPL_ARGS_DEPTH (args); |
9265 | if (level <= levels) | |
5d80a306 DG |
9266 | { |
9267 | arg = TMPL_ARG (args, level, idx); | |
9268 | ||
9269 | if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT) | |
9270 | /* See through ARGUMENT_PACK_SELECT arguments. */ | |
9271 | arg = ARGUMENT_PACK_SELECT_ARG (arg); | |
9272 | } | |
ae58fa02 | 9273 | |
315fb5db NS |
9274 | if (arg == error_mark_node) |
9275 | return error_mark_node; | |
9276 | else if (arg != NULL_TREE) | |
9277 | { | |
5d80a306 DG |
9278 | if (ARGUMENT_PACK_P (arg)) |
9279 | /* If ARG is an argument pack, we don't actually want to | |
9280 | perform a substitution here, because substitutions | |
9281 | for argument packs are only done | |
9282 | element-by-element. We can get to this point when | |
9283 | substituting the type of a non-type template | |
9284 | parameter pack, when that type actually contains | |
9285 | template parameter packs from an outer template, e.g., | |
9286 | ||
9287 | template<typename... Types> struct A { | |
9288 | template<Types... Values> struct B { }; | |
9289 | }; */ | |
9290 | return t; | |
9291 | ||
315fb5db | 9292 | if (TREE_CODE (t) == TEMPLATE_TYPE_PARM) |
ae58fa02 | 9293 | { |
9180c238 | 9294 | int quals; |
315fb5db | 9295 | gcc_assert (TYPE_P (arg)); |
9180c238 JM |
9296 | |
9297 | /* cv-quals from the template are discarded when | |
9298 | substituting in a function or reference type. */ | |
9299 | if (TREE_CODE (arg) == FUNCTION_TYPE | |
9300 | || TREE_CODE (arg) == METHOD_TYPE | |
9301 | || TREE_CODE (arg) == REFERENCE_TYPE) | |
9302 | quals = cp_type_quals (arg); | |
9303 | else | |
9304 | quals = cp_type_quals (arg) | cp_type_quals (t); | |
9305 | ||
315fb5db | 9306 | return cp_build_qualified_type_real |
9180c238 | 9307 | (arg, quals, complain | tf_ignore_bad_quals); |
315fb5db NS |
9308 | } |
9309 | else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM) | |
9310 | { | |
9311 | /* We are processing a type constructed from a | |
9312 | template template parameter. */ | |
9313 | tree argvec = tsubst (TYPE_TI_ARGS (t), | |
9314 | args, complain, in_decl); | |
9315 | if (argvec == error_mark_node) | |
9316 | return error_mark_node; | |
c8094d83 | 9317 | |
315fb5db NS |
9318 | /* We can get a TEMPLATE_TEMPLATE_PARM here when we |
9319 | are resolving nested-types in the signature of a | |
9320 | member function templates. Otherwise ARG is a | |
9321 | TEMPLATE_DECL and is the real template to be | |
9322 | instantiated. */ | |
9323 | if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM) | |
9324 | arg = TYPE_NAME (arg); | |
c8094d83 MS |
9325 | |
9326 | r = lookup_template_class (arg, | |
9327 | argvec, in_decl, | |
315fb5db NS |
9328 | DECL_CONTEXT (arg), |
9329 | /*entering_scope=*/0, | |
9330 | complain); | |
9331 | return cp_build_qualified_type_real | |
9332 | (r, TYPE_QUALS (t), complain); | |
ae58fa02 | 9333 | } |
315fb5db NS |
9334 | else |
9335 | /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */ | |
9336 | return arg; | |
ae58fa02 MM |
9337 | } |
9338 | ||
9339 | if (level == 1) | |
9340 | /* This can happen during the attempted tsubst'ing in | |
9341 | unify. This means that we don't yet have any information | |
9342 | about the template parameter in question. */ | |
9343 | return t; | |
9344 | ||
9345 | /* If we get here, we must have been looking at a parm for a | |
9346 | more deeply nested template. Make a new version of this | |
9347 | template parameter, but with a lower level. */ | |
9348 | switch (TREE_CODE (t)) | |
9349 | { | |
9350 | case TEMPLATE_TYPE_PARM: | |
9351 | case TEMPLATE_TEMPLATE_PARM: | |
a1281f45 | 9352 | case BOUND_TEMPLATE_TEMPLATE_PARM: |
89d684bb | 9353 | if (cp_type_quals (t)) |
ae58fa02 | 9354 | { |
9ccf6541 | 9355 | r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl); |
0cbd7506 MS |
9356 | r = cp_build_qualified_type_real |
9357 | (r, cp_type_quals (t), | |
4f2b0fb2 NS |
9358 | complain | (TREE_CODE (t) == TEMPLATE_TYPE_PARM |
9359 | ? tf_ignore_bad_quals : 0)); | |
9ccf6541 MM |
9360 | } |
9361 | else | |
9362 | { | |
11e74ea6 | 9363 | r = copy_type (t); |
9ccf6541 MM |
9364 | TEMPLATE_TYPE_PARM_INDEX (r) |
9365 | = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t), | |
ef3b7b17 | 9366 | r, levels, args, complain); |
9ccf6541 MM |
9367 | TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r); |
9368 | TYPE_MAIN_VARIANT (r) = r; | |
9369 | TYPE_POINTER_TO (r) = NULL_TREE; | |
9370 | TYPE_REFERENCE_TO (r) = NULL_TREE; | |
9371 | ||
06d40de8 DG |
9372 | if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM) |
9373 | /* We have reduced the level of the template | |
9374 | template parameter, but not the levels of its | |
9375 | template parameters, so canonical_type_parameter | |
9376 | will not be able to find the canonical template | |
9377 | template parameter for this level. Thus, we | |
9378 | require structural equality checking to compare | |
9379 | TEMPLATE_TEMPLATE_PARMs. */ | |
9380 | SET_TYPE_STRUCTURAL_EQUALITY (r); | |
3d761c46 DG |
9381 | else if (TYPE_STRUCTURAL_EQUALITY_P (t)) |
9382 | SET_TYPE_STRUCTURAL_EQUALITY (r); | |
06d40de8 DG |
9383 | else |
9384 | TYPE_CANONICAL (r) = canonical_type_parameter (r); | |
9385 | ||
a1281f45 | 9386 | if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM) |
9ccf6541 MM |
9387 | { |
9388 | tree argvec = tsubst (TYPE_TI_ARGS (t), args, | |
c8094d83 | 9389 | complain, in_decl); |
9ccf6541 MM |
9390 | if (argvec == error_mark_node) |
9391 | return error_mark_node; | |
4393e105 | 9392 | |
9ccf6541 MM |
9393 | TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r) |
9394 | = tree_cons (TYPE_TI_TEMPLATE (t), argvec, NULL_TREE); | |
9395 | } | |
ae58fa02 MM |
9396 | } |
9397 | break; | |
9398 | ||
9399 | case TEMPLATE_PARM_INDEX: | |
ef3b7b17 | 9400 | r = reduce_template_parm_level (t, type, levels, args, complain); |
ae58fa02 | 9401 | break; |
c8094d83 | 9402 | |
ae58fa02 | 9403 | default: |
315fb5db | 9404 | gcc_unreachable (); |
ae58fa02 MM |
9405 | } |
9406 | ||
5566b478 | 9407 | return r; |
ae58fa02 | 9408 | } |
5566b478 | 9409 | |
8d08fdba MS |
9410 | case TREE_LIST: |
9411 | { | |
058b15c1 | 9412 | tree purpose, value, chain; |
8d08fdba MS |
9413 | |
9414 | if (t == void_list_node) | |
9415 | return t; | |
9416 | ||
8d08fdba MS |
9417 | purpose = TREE_PURPOSE (t); |
9418 | if (purpose) | |
4393e105 MM |
9419 | { |
9420 | purpose = tsubst (purpose, args, complain, in_decl); | |
9421 | if (purpose == error_mark_node) | |
9422 | return error_mark_node; | |
9423 | } | |
8d08fdba MS |
9424 | value = TREE_VALUE (t); |
9425 | if (value) | |
4393e105 MM |
9426 | { |
9427 | value = tsubst (value, args, complain, in_decl); | |
9428 | if (value == error_mark_node) | |
9429 | return error_mark_node; | |
9430 | } | |
8d08fdba MS |
9431 | chain = TREE_CHAIN (t); |
9432 | if (chain && chain != void_type_node) | |
4393e105 MM |
9433 | { |
9434 | chain = tsubst (chain, args, complain, in_decl); | |
9435 | if (chain == error_mark_node) | |
9436 | return error_mark_node; | |
9437 | } | |
8d08fdba MS |
9438 | if (purpose == TREE_PURPOSE (t) |
9439 | && value == TREE_VALUE (t) | |
9440 | && chain == TREE_CHAIN (t)) | |
9441 | return t; | |
058b15c1 | 9442 | return hash_tree_cons (purpose, value, chain); |
8d08fdba | 9443 | } |
c8094d83 | 9444 | |
95b4aca6 | 9445 | case TREE_BINFO: |
bd7eccc4 | 9446 | /* We should never be tsubsting a binfo. */ |
315fb5db | 9447 | gcc_unreachable (); |
85b71cf2 | 9448 | |
95b4aca6 NS |
9449 | case TREE_VEC: |
9450 | /* A vector of template arguments. */ | |
50bc768d | 9451 | gcc_assert (!type); |
a91db711 | 9452 | return tsubst_template_args (t, args, complain, in_decl); |
8d08fdba | 9453 | |
8d08fdba MS |
9454 | case POINTER_TYPE: |
9455 | case REFERENCE_TYPE: | |
9456 | { | |
8d08fdba | 9457 | enum tree_code code; |
79a7c7fa | 9458 | |
46c895ac | 9459 | if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE) |
8d08fdba MS |
9460 | return t; |
9461 | ||
9462 | code = TREE_CODE (t); | |
4393e105 MM |
9463 | |
9464 | ||
9465 | /* [temp.deduct] | |
c8094d83 | 9466 | |
4393e105 | 9467 | Type deduction may fail for any of the following |
c8094d83 | 9468 | reasons: |
4393e105 MM |
9469 | |
9470 | -- Attempting to create a pointer to reference type. | |
9471 | -- Attempting to create a reference to a reference type or | |
8af2fec4 RY |
9472 | a reference to void. |
9473 | ||
9474 | Core issue 106 says that creating a reference to a reference | |
9475 | during instantiation is no longer a cause for failure. We | |
9476 | only enforce this check in strict C++98 mode. */ | |
9477 | if ((TREE_CODE (type) == REFERENCE_TYPE | |
c1ae8be5 | 9478 | && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE)) |
0ecfe0b4 | 9479 | || (code == REFERENCE_TYPE && TREE_CODE (type) == VOID_TYPE)) |
79a7c7fa | 9480 | { |
82a98427 | 9481 | static location_t last_loc; |
79a7c7fa JM |
9482 | |
9483 | /* We keep track of the last time we issued this error | |
9484 | message to avoid spewing a ton of messages during a | |
9485 | single bad template instantiation. */ | |
c2ea3a40 | 9486 | if (complain & tf_error |
2d593c86 | 9487 | && last_loc != input_location) |
79a7c7fa | 9488 | { |
0ecfe0b4 | 9489 | if (TREE_CODE (type) == VOID_TYPE) |
33bd39a2 | 9490 | error ("forming reference to void"); |
0ecfe0b4 | 9491 | else |
0f51ccfc | 9492 | error ("forming %s to reference type %qT", |
0cbd7506 MS |
9493 | (code == POINTER_TYPE) ? "pointer" : "reference", |
9494 | type); | |
82a98427 | 9495 | last_loc = input_location; |
79a7c7fa JM |
9496 | } |
9497 | ||
4393e105 | 9498 | return error_mark_node; |
79a7c7fa JM |
9499 | } |
9500 | else if (code == POINTER_TYPE) | |
46c895ac NS |
9501 | { |
9502 | r = build_pointer_type (type); | |
9503 | if (TREE_CODE (type) == METHOD_TYPE) | |
9504 | r = build_ptrmemfunc_type (r); | |
9505 | } | |
8af2fec4 RY |
9506 | else if (TREE_CODE (type) == REFERENCE_TYPE) |
9507 | /* In C++0x, during template argument substitution, when there is an | |
9508 | attempt to create a reference to a reference type, reference | |
9509 | collapsing is applied as described in [14.3.1/4 temp.arg.type]: | |
9510 | ||
9511 | "If a template-argument for a template-parameter T names a type | |
9512 | that is a reference to a type A, an attempt to create the type | |
9513 | 'lvalue reference to cv T' creates the type 'lvalue reference to | |
9514 | A,' while an attempt to create the type type rvalue reference to | |
9515 | cv T' creates the type T" | |
9516 | */ | |
9517 | r = cp_build_reference_type | |
9518 | (TREE_TYPE (type), | |
9519 | TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type)); | |
8d08fdba | 9520 | else |
8af2fec4 | 9521 | r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t)); |
adecb3f4 | 9522 | r = cp_build_qualified_type_real (r, TYPE_QUALS (t), complain); |
79a7c7fa | 9523 | |
a5f1c5f8 NS |
9524 | if (r != error_mark_node) |
9525 | /* Will this ever be needed for TYPE_..._TO values? */ | |
9526 | layout_type (r); | |
c8094d83 | 9527 | |
8d08fdba MS |
9528 | return r; |
9529 | } | |
a4443a08 | 9530 | case OFFSET_TYPE: |
0ecfe0b4 | 9531 | { |
4393e105 | 9532 | r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl); |
9e1e64ec | 9533 | if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r)) |
4393e105 MM |
9534 | { |
9535 | /* [temp.deduct] | |
9536 | ||
9537 | Type deduction may fail for any of the following | |
9538 | reasons: | |
c8094d83 | 9539 | |
4393e105 | 9540 | -- Attempting to create "pointer to member of T" when T |
0cbd7506 | 9541 | is not a class type. */ |
c2ea3a40 | 9542 | if (complain & tf_error) |
0f51ccfc | 9543 | error ("creating pointer to member of non-class type %qT", r); |
4393e105 MM |
9544 | return error_mark_node; |
9545 | } | |
46c895ac NS |
9546 | if (TREE_CODE (type) == REFERENCE_TYPE) |
9547 | { | |
4f09be91 | 9548 | if (complain & tf_error) |
0f51ccfc | 9549 | error ("creating pointer to member reference type %qT", type); |
cb6d4a9f VR |
9550 | return error_mark_node; |
9551 | } | |
9552 | if (TREE_CODE (type) == VOID_TYPE) | |
9553 | { | |
9554 | if (complain & tf_error) | |
9555 | error ("creating pointer to member of type void"); | |
46c895ac NS |
9556 | return error_mark_node; |
9557 | } | |
50bc768d | 9558 | gcc_assert (TREE_CODE (type) != METHOD_TYPE); |
46c895ac | 9559 | if (TREE_CODE (type) == FUNCTION_TYPE) |
a5ac359a | 9560 | { |
0cbd7506 MS |
9561 | /* The type of the implicit object parameter gets its |
9562 | cv-qualifiers from the FUNCTION_TYPE. */ | |
a5ac359a | 9563 | tree method_type; |
0cbd7506 MS |
9564 | tree this_type = cp_build_qualified_type (TYPE_MAIN_VARIANT (r), |
9565 | cp_type_quals (type)); | |
9566 | tree memptr; | |
9567 | method_type = build_method_type_directly (this_type, | |
43dc123f MM |
9568 | TREE_TYPE (type), |
9569 | TYPE_ARG_TYPES (type)); | |
0cbd7506 MS |
9570 | memptr = build_ptrmemfunc_type (build_pointer_type (method_type)); |
9571 | return cp_build_qualified_type_real (memptr, cp_type_quals (t), | |
9572 | complain); | |
a5ac359a | 9573 | } |
46c895ac | 9574 | else |
b7a78333 MM |
9575 | return cp_build_qualified_type_real (build_ptrmem_type (r, type), |
9576 | TYPE_QUALS (t), | |
9577 | complain); | |
0ecfe0b4 | 9578 | } |
8d08fdba MS |
9579 | case FUNCTION_TYPE: |
9580 | case METHOD_TYPE: | |
9581 | { | |
c11b6f21 | 9582 | tree fntype; |
c7222c02 | 9583 | tree specs; |
4393e105 MM |
9584 | fntype = tsubst_function_type (t, args, complain, in_decl); |
9585 | if (fntype == error_mark_node) | |
9586 | return error_mark_node; | |
cabc336a | 9587 | |
34cd5ae7 | 9588 | /* Substitute the exception specification. */ |
c8094d83 | 9589 | specs = tsubst_exception_specification (t, args, complain, |
c7222c02 | 9590 | in_decl); |
9f6206d9 VR |
9591 | if (specs == error_mark_node) |
9592 | return error_mark_node; | |
c7222c02 MM |
9593 | if (specs) |
9594 | fntype = build_exception_variant (fntype, specs); | |
c11b6f21 | 9595 | return fntype; |
8d08fdba MS |
9596 | } |
9597 | case ARRAY_TYPE: | |
9598 | { | |
4393e105 MM |
9599 | tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl); |
9600 | if (domain == error_mark_node) | |
9601 | return error_mark_node; | |
9602 | ||
9603 | /* As an optimization, we avoid regenerating the array type if | |
9604 | it will obviously be the same as T. */ | |
8d08fdba MS |
9605 | if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t)) |
9606 | return t; | |
0ecfe0b4 | 9607 | |
c8094d83 | 9608 | /* These checks should match the ones in grokdeclarator. |
4393e105 | 9609 | |
c8094d83 MS |
9610 | [temp.deduct] |
9611 | ||
9612 | The deduction may fail for any of the following reasons: | |
4393e105 MM |
9613 | |
9614 | -- Attempting to create an array with an element type that | |
c8094d83 | 9615 | is void, a function type, or a reference type, or [DR337] |
cfb91b67 | 9616 | an abstract class type. */ |
c8094d83 | 9617 | if (TREE_CODE (type) == VOID_TYPE |
4393e105 MM |
9618 | || TREE_CODE (type) == FUNCTION_TYPE |
9619 | || TREE_CODE (type) == REFERENCE_TYPE) | |
0ecfe0b4 | 9620 | { |
c2ea3a40 | 9621 | if (complain & tf_error) |
0f51ccfc | 9622 | error ("creating array of %qT", type); |
4393e105 | 9623 | return error_mark_node; |
0ecfe0b4 | 9624 | } |
cfb91b67 GB |
9625 | if (CLASS_TYPE_P (type) && CLASSTYPE_PURE_VIRTUALS (type)) |
9626 | { | |
9627 | if (complain & tf_error) | |
c8094d83 | 9628 | error ("creating array of %qT, which is an abstract class type", |
cfb91b67 | 9629 | type); |
c8094d83 | 9630 | return error_mark_node; |
cfb91b67 | 9631 | } |
0ecfe0b4 | 9632 | |
8d08fdba | 9633 | r = build_cplus_array_type (type, domain); |
5044ab0e JM |
9634 | |
9635 | if (TYPE_USER_ALIGN (t)) | |
9636 | { | |
9637 | TYPE_ALIGN (r) = TYPE_ALIGN (t); | |
9638 | TYPE_USER_ALIGN (r) = 1; | |
9639 | } | |
9640 | ||
8d08fdba MS |
9641 | return r; |
9642 | } | |
9643 | ||
8d08fdba | 9644 | case PLUS_EXPR: |
5566b478 | 9645 | case MINUS_EXPR: |
4393e105 | 9646 | { |
c2ea3a40 NS |
9647 | tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl); |
9648 | tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl); | |
4393e105 MM |
9649 | |
9650 | if (e1 == error_mark_node || e2 == error_mark_node) | |
9651 | return error_mark_node; | |
9652 | ||
7866705a | 9653 | return fold_build2 (TREE_CODE (t), TREE_TYPE (t), e1, e2); |
4393e105 | 9654 | } |
8d08fdba MS |
9655 | |
9656 | case NEGATE_EXPR: | |
9657 | case NOP_EXPR: | |
4393e105 | 9658 | { |
c2ea3a40 | 9659 | tree e = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl); |
4393e105 MM |
9660 | if (e == error_mark_node) |
9661 | return error_mark_node; | |
9662 | ||
7866705a | 9663 | return fold_build1 (TREE_CODE (t), TREE_TYPE (t), e); |
4393e105 | 9664 | } |
8d08fdba | 9665 | |
5566b478 MS |
9666 | case TYPENAME_TYPE: |
9667 | { | |
4393e105 MM |
9668 | tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain, |
9669 | in_decl, /*entering_scope=*/1); | |
9670 | tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args, | |
c8094d83 | 9671 | complain, in_decl); |
4393e105 MM |
9672 | |
9673 | if (ctx == error_mark_node || f == error_mark_node) | |
9674 | return error_mark_node; | |
ae58fa02 | 9675 | |
9e1e64ec | 9676 | if (!MAYBE_CLASS_TYPE_P (ctx)) |
bf8f3f93 | 9677 | { |
c2ea3a40 | 9678 | if (complain & tf_error) |
0f51ccfc | 9679 | error ("%qT is not a class, struct, or union type", ctx); |
bf8f3f93 MM |
9680 | return error_mark_node; |
9681 | } | |
9682 | else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx)) | |
9683 | { | |
9684 | /* Normally, make_typename_type does not require that the CTX | |
9685 | have complete type in order to allow things like: | |
c8094d83 | 9686 | |
0cbd7506 | 9687 | template <class T> struct S { typename S<T>::X Y; }; |
ae58fa02 | 9688 | |
bf8f3f93 MM |
9689 | But, such constructs have already been resolved by this |
9690 | point, so here CTX really should have complete type, unless | |
9691 | it's a partial instantiation. */ | |
4393e105 | 9692 | ctx = complete_type (ctx); |
d0f062fb | 9693 | if (!COMPLETE_TYPE_P (ctx)) |
4393e105 | 9694 | { |
c2ea3a40 | 9695 | if (complain & tf_error) |
7a228918 | 9696 | cxx_incomplete_type_error (NULL_TREE, ctx); |
4393e105 MM |
9697 | return error_mark_node; |
9698 | } | |
9699 | } | |
ae58fa02 | 9700 | |
fc6a28d7 | 9701 | f = make_typename_type (ctx, f, typename_type, |
4f2b0fb2 | 9702 | (complain & tf_error) | tf_keep_type_decl); |
f0bcd168 MM |
9703 | if (f == error_mark_node) |
9704 | return f; | |
0cbd7506 MS |
9705 | if (TREE_CODE (f) == TYPE_DECL) |
9706 | { | |
4f2b0fb2 | 9707 | complain |= tf_ignore_bad_quals; |
0cbd7506 MS |
9708 | f = TREE_TYPE (f); |
9709 | } | |
c8094d83 | 9710 | |
fc6a28d7 MM |
9711 | if (TREE_CODE (f) != TYPENAME_TYPE) |
9712 | { | |
9713 | if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE) | |
c8094d83 | 9714 | error ("%qT resolves to %qT, which is not an enumeration type", |
fc6a28d7 MM |
9715 | t, f); |
9716 | else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f)) | |
c8094d83 | 9717 | error ("%qT resolves to %qT, which is is not a class type", |
fc6a28d7 MM |
9718 | t, f); |
9719 | } | |
9720 | ||
0cbd7506 MS |
9721 | return cp_build_qualified_type_real |
9722 | (f, cp_type_quals (f) | cp_type_quals (t), complain); | |
5566b478 | 9723 | } |
c8094d83 | 9724 | |
b8c6534b KL |
9725 | case UNBOUND_CLASS_TEMPLATE: |
9726 | { | |
9727 | tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain, | |
9728 | in_decl, /*entering_scope=*/1); | |
9729 | tree name = TYPE_IDENTIFIER (t); | |
b939a023 | 9730 | tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t)); |
b8c6534b KL |
9731 | |
9732 | if (ctx == error_mark_node || name == error_mark_node) | |
9733 | return error_mark_node; | |
9734 | ||
b939a023 KL |
9735 | if (parm_list) |
9736 | parm_list = tsubst_template_parms (parm_list, args, complain); | |
9737 | return make_unbound_class_template (ctx, name, parm_list, complain); | |
b8c6534b KL |
9738 | } |
9739 | ||
5566b478 | 9740 | case INDIRECT_REF: |
5566b478 | 9741 | case ADDR_EXPR: |
058b15c1 | 9742 | case CALL_EXPR: |
315fb5db | 9743 | gcc_unreachable (); |
5566b478 MS |
9744 | |
9745 | case ARRAY_REF: | |
4393e105 | 9746 | { |
c2ea3a40 | 9747 | tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl); |
015c2c66 MM |
9748 | tree e2 = tsubst_expr (TREE_OPERAND (t, 1), args, complain, in_decl, |
9749 | /*integral_constant_expression_p=*/false); | |
4393e105 MM |
9750 | if (e1 == error_mark_node || e2 == error_mark_node) |
9751 | return error_mark_node; | |
9752 | ||
44de5aeb | 9753 | return build_nt (ARRAY_REF, e1, e2, NULL_TREE, NULL_TREE); |
4393e105 | 9754 | } |
5566b478 | 9755 | |
fc378698 | 9756 | case SCOPE_REF: |
4393e105 | 9757 | { |
c2ea3a40 | 9758 | tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl); |
4393e105 MM |
9759 | tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl); |
9760 | if (e1 == error_mark_node || e2 == error_mark_node) | |
9761 | return error_mark_node; | |
9762 | ||
02ed62dd MM |
9763 | return build_qualified_name (/*type=*/NULL_TREE, |
9764 | e1, e2, QUALIFIED_NAME_IS_TEMPLATE (t)); | |
4393e105 | 9765 | } |
fc378698 | 9766 | |
b894fc05 | 9767 | case TYPEOF_TYPE: |
4393e105 | 9768 | { |
b830b74c | 9769 | tree type; |
4393e105 | 9770 | |
015c2c66 MM |
9771 | type = finish_typeof (tsubst_expr |
9772 | (TYPEOF_TYPE_EXPR (t), args, | |
9773 | complain, in_decl, | |
9774 | /*integral_constant_expression_p=*/false)); | |
b830b74c | 9775 | return cp_build_qualified_type_real (type, |
dce50630 | 9776 | cp_type_quals (t) |
b830b74c | 9777 | | cp_type_quals (type), |
dce50630 | 9778 | complain); |
4393e105 | 9779 | } |
b894fc05 | 9780 | |
3ad6a8e1 DG |
9781 | case DECLTYPE_TYPE: |
9782 | { | |
9783 | tree type; | |
9784 | ||
b344d949 JM |
9785 | ++skip_evaluation; |
9786 | ||
9787 | type = tsubst_expr (DECLTYPE_TYPE_EXPR (t), args, | |
9788 | complain, in_decl, | |
9789 | /*integral_constant_expression_p=*/false); | |
9790 | ||
9791 | --skip_evaluation; | |
9792 | ||
9793 | type = | |
9794 | finish_decltype_type (type, | |
3ad6a8e1 DG |
9795 | DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t)); |
9796 | return cp_build_qualified_type_real (type, | |
9797 | cp_type_quals (t) | |
9798 | | cp_type_quals (type), | |
9799 | complain); | |
9800 | } | |
9801 | ||
5d80a306 DG |
9802 | case TYPE_ARGUMENT_PACK: |
9803 | case NONTYPE_ARGUMENT_PACK: | |
9804 | { | |
9805 | tree r = make_node (TREE_CODE (t)); | |
9806 | tree packed_out = | |
9807 | tsubst_template_args (ARGUMENT_PACK_ARGS (t), | |
9808 | args, | |
9809 | complain, | |
9810 | in_decl); | |
9811 | SET_ARGUMENT_PACK_ARGS (r, packed_out); | |
9812 | ||
9813 | /* For template nontype argument packs, also substitute into | |
9814 | the type. */ | |
9815 | if (TREE_CODE (t) == NONTYPE_ARGUMENT_PACK) | |
9816 | TREE_TYPE (r) = tsubst (TREE_TYPE (t), args, complain, in_decl); | |
9817 | ||
9818 | return r; | |
9819 | } | |
9820 | break; | |
9821 | ||
8d08fdba | 9822 | default: |
9e637a26 | 9823 | sorry ("use of %qs in template", |
8d08fdba MS |
9824 | tree_code_name [(int) TREE_CODE (t)]); |
9825 | return error_mark_node; | |
9826 | } | |
9827 | } | |
9828 | ||
ee76b931 MM |
9829 | /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the |
9830 | type of the expression on the left-hand side of the "." or "->" | |
9831 | operator. */ | |
9832 | ||
9833 | static tree | |
9834 | tsubst_baselink (tree baselink, tree object_type, | |
9835 | tree args, tsubst_flags_t complain, tree in_decl) | |
9836 | { | |
9837 | tree name; | |
9838 | tree qualifying_scope; | |
9839 | tree fns; | |
6f67f000 | 9840 | tree optype; |
ee76b931 MM |
9841 | tree template_args = 0; |
9842 | bool template_id_p = false; | |
9843 | ||
51a203d9 MM |
9844 | /* A baselink indicates a function from a base class. Both the |
9845 | BASELINK_ACCESS_BINFO and the base class referenced may | |
9846 | indicate bases of the template class, rather than the | |
9847 | instantiated class. In addition, lookups that were not | |
9848 | ambiguous before may be ambiguous now. Therefore, we perform | |
9849 | the lookup again. */ | |
ee76b931 | 9850 | qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink)); |
51a203d9 MM |
9851 | qualifying_scope = tsubst (qualifying_scope, args, |
9852 | complain, in_decl); | |
ee76b931 | 9853 | fns = BASELINK_FUNCTIONS (baselink); |
6f67f000 | 9854 | optype = BASELINK_OPTYPE (baselink); |
ee76b931 MM |
9855 | if (TREE_CODE (fns) == TEMPLATE_ID_EXPR) |
9856 | { | |
9857 | template_id_p = true; | |
9858 | template_args = TREE_OPERAND (fns, 1); | |
9859 | fns = TREE_OPERAND (fns, 0); | |
bf12d54d NS |
9860 | if (template_args) |
9861 | template_args = tsubst_template_args (template_args, args, | |
9862 | complain, in_decl); | |
ee76b931 MM |
9863 | } |
9864 | name = DECL_NAME (get_first_fn (fns)); | |
9865 | baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1); | |
c8094d83 | 9866 | |
9b60dfd7 MM |
9867 | /* If lookup found a single function, mark it as used at this |
9868 | point. (If it lookup found multiple functions the one selected | |
9869 | later by overload resolution will be marked as used at that | |
9870 | point.) */ | |
9871 | if (BASELINK_P (baselink)) | |
9872 | fns = BASELINK_FUNCTIONS (baselink); | |
9873 | if (!template_id_p && !really_overloaded_fn (fns)) | |
9874 | mark_used (OVL_CURRENT (fns)); | |
9875 | ||
9876 | /* Add back the template arguments, if present. */ | |
ee76b931 | 9877 | if (BASELINK_P (baselink) && template_id_p) |
c8094d83 | 9878 | BASELINK_FUNCTIONS (baselink) |
ee76b931 MM |
9879 | = build_nt (TEMPLATE_ID_EXPR, |
9880 | BASELINK_FUNCTIONS (baselink), | |
9881 | template_args); | |
6f67f000 MM |
9882 | /* Update the conversion operator type. */ |
9883 | BASELINK_OPTYPE (baselink) | |
9884 | = tsubst (optype, args, complain, in_decl); | |
9b60dfd7 | 9885 | |
ee76b931 MM |
9886 | if (!object_type) |
9887 | object_type = current_class_type; | |
c8094d83 | 9888 | return adjust_result_of_qualified_name_lookup (baselink, |
ee76b931 MM |
9889 | qualifying_scope, |
9890 | object_type); | |
9891 | } | |
9892 | ||
9893 | /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is | |
9894 | true if the qualified-id will be a postfix-expression in-and-of | |
9895 | itself; false if more of the postfix-expression follows the | |
9896 | QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand | |
9897 | of "&". */ | |
9898 | ||
9899 | static tree | |
c8094d83 | 9900 | tsubst_qualified_id (tree qualified_id, tree args, |
ee76b931 MM |
9901 | tsubst_flags_t complain, tree in_decl, |
9902 | bool done, bool address_p) | |
9903 | { | |
9904 | tree expr; | |
9905 | tree scope; | |
9906 | tree name; | |
9907 | bool is_template; | |
9908 | tree template_args; | |
9909 | ||
50bc768d | 9910 | gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF); |
ee76b931 | 9911 | |
ee76b931 MM |
9912 | /* Figure out what name to look up. */ |
9913 | name = TREE_OPERAND (qualified_id, 1); | |
9914 | if (TREE_CODE (name) == TEMPLATE_ID_EXPR) | |
9915 | { | |
9916 | is_template = true; | |
bf12d54d NS |
9917 | template_args = TREE_OPERAND (name, 1); |
9918 | if (template_args) | |
9919 | template_args = tsubst_template_args (template_args, args, | |
9920 | complain, in_decl); | |
ee76b931 MM |
9921 | name = TREE_OPERAND (name, 0); |
9922 | } | |
9923 | else | |
9924 | { | |
9925 | is_template = false; | |
9926 | template_args = NULL_TREE; | |
9927 | } | |
9928 | ||
6eeba0cc MM |
9929 | /* Substitute into the qualifying scope. When there are no ARGS, we |
9930 | are just trying to simplify a non-dependent expression. In that | |
9931 | case the qualifying scope may be dependent, and, in any case, | |
9932 | substituting will not help. */ | |
9933 | scope = TREE_OPERAND (qualified_id, 0); | |
9934 | if (args) | |
9935 | { | |
9936 | scope = tsubst (scope, args, complain, in_decl); | |
9937 | expr = tsubst_copy (name, args, complain, in_decl); | |
9938 | } | |
9939 | else | |
9940 | expr = name; | |
10b1d5e7 | 9941 | |
ab73670a | 9942 | if (dependent_type_p (scope)) |
7e361ae6 JM |
9943 | { |
9944 | tree type = NULL_TREE; | |
9945 | if (DECL_P (expr) && !dependent_scope_p (scope)) | |
9946 | type = TREE_TYPE (expr); | |
9947 | return build_qualified_name (type, scope, expr, | |
9948 | QUALIFIED_NAME_IS_TEMPLATE (qualified_id)); | |
9949 | } | |
c8094d83 | 9950 | |
5e08432e | 9951 | if (!BASELINK_P (name) && !DECL_P (expr)) |
12483c9f | 9952 | { |
8ca4bf25 | 9953 | if (TREE_CODE (expr) == BIT_NOT_EXPR) |
7e361ae6 JM |
9954 | { |
9955 | /* A BIT_NOT_EXPR is used to represent a destructor. */ | |
9956 | if (!check_dtor_name (scope, TREE_OPERAND (expr, 0))) | |
9957 | { | |
9958 | error ("qualifying type %qT does not match destructor name ~%qT", | |
9959 | scope, TREE_OPERAND (expr, 0)); | |
9960 | expr = error_mark_node; | |
9961 | } | |
9962 | else | |
9963 | expr = lookup_qualified_name (scope, complete_dtor_identifier, | |
9964 | /*is_type_p=*/0, false); | |
9965 | } | |
8ca4bf25 MM |
9966 | else |
9967 | expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false); | |
12483c9f NS |
9968 | if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL |
9969 | ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL) | |
9970 | { | |
9971 | if (complain & tf_error) | |
b2693faf | 9972 | { |
0f51ccfc | 9973 | error ("dependent-name %qE is parsed as a non-type, but " |
b2693faf | 9974 | "instantiation yields a type", qualified_id); |
1f5b3869 | 9975 | inform (input_location, "say %<typename %E%> if a type is meant", qualified_id); |
b2693faf | 9976 | } |
12483c9f NS |
9977 | return error_mark_node; |
9978 | } | |
9979 | } | |
c8094d83 | 9980 | |
279b8466 | 9981 | if (DECL_P (expr)) |
8f78f01f MM |
9982 | { |
9983 | check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE, | |
9984 | scope); | |
9985 | /* Remember that there was a reference to this entity. */ | |
9986 | mark_used (expr); | |
9987 | } | |
9988 | ||
9989 | if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST) | |
9990 | { | |
9991 | if (complain & tf_error) | |
c8094d83 | 9992 | qualified_name_lookup_error (scope, |
8f78f01f | 9993 | TREE_OPERAND (qualified_id, 1), |
2b7a3abf | 9994 | expr, input_location); |
8f78f01f MM |
9995 | return error_mark_node; |
9996 | } | |
ee76b931 MM |
9997 | |
9998 | if (is_template) | |
10b1d5e7 | 9999 | expr = lookup_template_function (expr, template_args); |
ee76b931 | 10000 | |
22038b2c | 10001 | if (expr == error_mark_node && complain & tf_error) |
8f78f01f | 10002 | qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1), |
2b7a3abf | 10003 | expr, input_location); |
22038b2c | 10004 | else if (TYPE_P (scope)) |
ee76b931 | 10005 | { |
c8094d83 | 10006 | expr = (adjust_result_of_qualified_name_lookup |
ee76b931 | 10007 | (expr, scope, current_class_type)); |
3db45ab5 | 10008 | expr = (finish_qualified_id_expr |
02ed62dd MM |
10009 | (scope, expr, done, address_p, |
10010 | QUALIFIED_NAME_IS_TEMPLATE (qualified_id), | |
10011 | /*template_arg_p=*/false)); | |
ee76b931 | 10012 | } |
c8094d83 | 10013 | |
015c2c66 MM |
10014 | /* Expressions do not generally have reference type. */ |
10015 | if (TREE_CODE (expr) != SCOPE_REF | |
10016 | /* However, if we're about to form a pointer-to-member, we just | |
10017 | want the referenced member referenced. */ | |
10018 | && TREE_CODE (expr) != OFFSET_REF) | |
aec5ba60 | 10019 | expr = convert_from_reference (expr); |
ee76b931 MM |
10020 | |
10021 | return expr; | |
10022 | } | |
10023 | ||
00d3396f JM |
10024 | /* Like tsubst, but deals with expressions. This function just replaces |
10025 | template parms; to finish processing the resultant expression, use | |
10026 | tsubst_expr. */ | |
10027 | ||
14d22dd6 | 10028 | static tree |
3a978d72 | 10029 | tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl) |
5566b478 MS |
10030 | { |
10031 | enum tree_code code; | |
8452b1d3 | 10032 | tree r; |
8d08fdba | 10033 | |
b77ba909 | 10034 | if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE) |
5566b478 MS |
10035 | return t; |
10036 | ||
10037 | code = TREE_CODE (t); | |
b7484fbe | 10038 | |
5566b478 MS |
10039 | switch (code) |
10040 | { | |
10041 | case PARM_DECL: | |
a723baf1 | 10042 | r = retrieve_local_specialization (t); |
b344d949 JM |
10043 | |
10044 | if (r == NULL) | |
10045 | { | |
c7cb9f42 | 10046 | tree c; |
b344d949 | 10047 | /* This can happen for a parameter name used later in a function |
448545cb JM |
10048 | declaration (such as in a late-specified return type). Just |
10049 | make a dummy decl, since it's only used for its type. */ | |
10050 | gcc_assert (skip_evaluation); | |
c7cb9f42 DS |
10051 | /* We copy T because want to tsubst the PARM_DECL only, |
10052 | not the following PARM_DECLs that are chained to T. */ | |
10053 | c = copy_node (t); | |
10054 | r = tsubst_decl (c, args, complain); | |
448545cb JM |
10055 | /* Give it the template pattern as its context; its true context |
10056 | hasn't been instantiated yet and this is good enough for | |
10057 | mangling. */ | |
10058 | DECL_CONTEXT (r) = DECL_CONTEXT (t); | |
b344d949 JM |
10059 | } |
10060 | ||
5d80a306 DG |
10061 | if (TREE_CODE (r) == ARGUMENT_PACK_SELECT) |
10062 | r = ARGUMENT_PACK_SELECT_ARG (r); | |
c0694c4b | 10063 | mark_used (r); |
a723baf1 | 10064 | return r; |
5566b478 MS |
10065 | |
10066 | case CONST_DECL: | |
ed44da02 MM |
10067 | { |
10068 | tree enum_type; | |
10069 | tree v; | |
10070 | ||
a723baf1 MM |
10071 | if (DECL_TEMPLATE_PARM_P (t)) |
10072 | return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl); | |
7b6d72fc MM |
10073 | /* There is no need to substitute into namespace-scope |
10074 | enumerators. */ | |
10075 | if (DECL_NAMESPACE_SCOPE_P (t)) | |
ed44da02 | 10076 | return t; |
d5a10cf0 MM |
10077 | /* If ARGS is NULL, then T is known to be non-dependent. */ |
10078 | if (args == NULL_TREE) | |
8a784e4a | 10079 | return integral_constant_value (t); |
ed44da02 MM |
10080 | |
10081 | /* Unfortunately, we cannot just call lookup_name here. | |
9188c363 | 10082 | Consider: |
c8094d83 | 10083 | |
9188c363 MM |
10084 | template <int I> int f() { |
10085 | enum E { a = I }; | |
10086 | struct S { void g() { E e = a; } }; | |
10087 | }; | |
c8094d83 | 10088 | |
9188c363 MM |
10089 | When we instantiate f<7>::S::g(), say, lookup_name is not |
10090 | clever enough to find f<7>::a. */ | |
c8094d83 MS |
10091 | enum_type |
10092 | = tsubst_aggr_type (TREE_TYPE (t), args, complain, in_decl, | |
ed44da02 MM |
10093 | /*entering_scope=*/0); |
10094 | ||
c8094d83 MS |
10095 | for (v = TYPE_VALUES (enum_type); |
10096 | v != NULL_TREE; | |
ed44da02 MM |
10097 | v = TREE_CHAIN (v)) |
10098 | if (TREE_PURPOSE (v) == DECL_NAME (t)) | |
10099 | return TREE_VALUE (v); | |
10100 | ||
10101 | /* We didn't find the name. That should never happen; if | |
10102 | name-lookup found it during preliminary parsing, we | |
10103 | should find it again here during instantiation. */ | |
315fb5db | 10104 | gcc_unreachable (); |
ed44da02 | 10105 | } |
db04386f | 10106 | return t; |
ed44da02 | 10107 | |
5566b478 MS |
10108 | case FIELD_DECL: |
10109 | if (DECL_CONTEXT (t)) | |
10110 | { | |
0978790f | 10111 | tree ctx; |
0978790f | 10112 | |
4393e105 | 10113 | ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl, |
36a117a5 | 10114 | /*entering_scope=*/1); |
0978790f | 10115 | if (ctx != DECL_CONTEXT (t)) |
bad1f462 KL |
10116 | { |
10117 | tree r = lookup_field (ctx, DECL_NAME (t), 0, false); | |
10118 | if (!r) | |
10119 | { | |
10120 | if (complain & tf_error) | |
10121 | error ("using invalid field %qD", t); | |
10122 | return error_mark_node; | |
10123 | } | |
10124 | return r; | |
10125 | } | |
5566b478 | 10126 | } |
c8094d83 | 10127 | |
5566b478 MS |
10128 | return t; |
10129 | ||
10130 | case VAR_DECL: | |
10131 | case FUNCTION_DECL: | |
a723baf1 MM |
10132 | if ((DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)) |
10133 | || local_variable_p (t)) | |
4393e105 | 10134 | t = tsubst (t, args, complain, in_decl); |
5566b478 MS |
10135 | mark_used (t); |
10136 | return t; | |
10137 | ||
a723baf1 | 10138 | case BASELINK: |
ee76b931 | 10139 | return tsubst_baselink (t, current_class_type, args, complain, in_decl); |
a723baf1 | 10140 | |
98c1c668 | 10141 | case TEMPLATE_DECL: |
a723baf1 | 10142 | if (DECL_TEMPLATE_TEMPLATE_PARM_P (t)) |
c8094d83 | 10143 | return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)), |
a723baf1 | 10144 | args, complain, in_decl); |
c7222c02 | 10145 | else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t)) |
4393e105 | 10146 | return tsubst (t, args, complain, in_decl); |
fcea7401 KL |
10147 | else if (DECL_CLASS_SCOPE_P (t) |
10148 | && uses_template_parms (DECL_CONTEXT (t))) | |
10149 | { | |
10150 | /* Template template argument like the following example need | |
10151 | special treatment: | |
10152 | ||
10153 | template <template <class> class TT> struct C {}; | |
10154 | template <class T> struct D { | |
10155 | template <class U> struct E {}; | |
0cbd7506 | 10156 | C<E> c; // #1 |
fcea7401 KL |
10157 | }; |
10158 | D<int> d; // #2 | |
10159 | ||
10160 | We are processing the template argument `E' in #1 for | |
10161 | the template instantiation #2. Originally, `E' is a | |
10162 | TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we | |
10163 | have to substitute this with one having context `D<int>'. */ | |
10164 | ||
10165 | tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl); | |
10166 | return lookup_field (context, DECL_NAME(t), 0, false); | |
10167 | } | |
98c1c668 | 10168 | else |
fcea7401 | 10169 | /* Ordinary template template argument. */ |
98c1c668 JM |
10170 | return t; |
10171 | ||
5566b478 MS |
10172 | case CAST_EXPR: |
10173 | case REINTERPRET_CAST_EXPR: | |
e92cc029 MS |
10174 | case CONST_CAST_EXPR: |
10175 | case STATIC_CAST_EXPR: | |
10176 | case DYNAMIC_CAST_EXPR: | |
51924768 | 10177 | case NOP_EXPR: |
5566b478 | 10178 | return build1 |
4393e105 MM |
10179 | (code, tsubst (TREE_TYPE (t), args, complain, in_decl), |
10180 | tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl)); | |
5566b478 | 10181 | |
5d80a306 DG |
10182 | case SIZEOF_EXPR: |
10183 | if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))) | |
10184 | { | |
10185 | /* We only want to compute the number of arguments. */ | |
10186 | tree expanded = tsubst_pack_expansion (TREE_OPERAND (t, 0), args, | |
10187 | complain, in_decl); | |
e68c63e3 | 10188 | int len = 0; |
063cc99c DG |
10189 | |
10190 | if (TREE_CODE (expanded) == TREE_VEC) | |
10191 | len = TREE_VEC_LENGTH (expanded); | |
10192 | ||
4745e4eb JJ |
10193 | if (expanded == error_mark_node) |
10194 | return error_mark_node; | |
063cc99c DG |
10195 | else if (PACK_EXPANSION_P (expanded) |
10196 | || (TREE_CODE (expanded) == TREE_VEC | |
10197 | && len > 0 | |
10198 | && PACK_EXPANSION_P (TREE_VEC_ELT (expanded, len-1)))) | |
10199 | { | |
10200 | if (TREE_CODE (expanded) == TREE_VEC) | |
10201 | expanded = TREE_VEC_ELT (expanded, len - 1); | |
10202 | ||
10203 | if (TYPE_P (expanded)) | |
10204 | return cxx_sizeof_or_alignof_type (expanded, SIZEOF_EXPR, | |
10205 | complain & tf_error); | |
10206 | else | |
10207 | return cxx_sizeof_or_alignof_expr (expanded, SIZEOF_EXPR, | |
10208 | complain & tf_error); | |
10209 | } | |
10210 | else | |
10211 | return build_int_cst (size_type_node, len); | |
5d80a306 DG |
10212 | } |
10213 | /* Fall through */ | |
10214 | ||
5566b478 | 10215 | case INDIRECT_REF: |
5566b478 MS |
10216 | case NEGATE_EXPR: |
10217 | case TRUTH_NOT_EXPR: | |
b87692e5 | 10218 | case BIT_NOT_EXPR: |
5566b478 | 10219 | case ADDR_EXPR: |
392e3d51 | 10220 | case UNARY_PLUS_EXPR: /* Unary + */ |
abff8e06 | 10221 | case ALIGNOF_EXPR: |
5566b478 | 10222 | case ARROW_EXPR: |
fc378698 | 10223 | case THROW_EXPR: |
5156628f | 10224 | case TYPEID_EXPR: |
f5733617 SS |
10225 | case REALPART_EXPR: |
10226 | case IMAGPART_EXPR: | |
5566b478 | 10227 | return build1 |
6a629cac | 10228 | (code, tsubst (TREE_TYPE (t), args, complain, in_decl), |
4393e105 | 10229 | tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl)); |
5566b478 | 10230 | |
a723baf1 MM |
10231 | case COMPONENT_REF: |
10232 | { | |
10233 | tree object; | |
10234 | tree name; | |
10235 | ||
10236 | object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl); | |
10237 | name = TREE_OPERAND (t, 1); | |
c8094d83 | 10238 | if (TREE_CODE (name) == BIT_NOT_EXPR) |
a723baf1 MM |
10239 | { |
10240 | name = tsubst_copy (TREE_OPERAND (name, 0), args, | |
10241 | complain, in_decl); | |
10242 | name = build1 (BIT_NOT_EXPR, NULL_TREE, name); | |
10243 | } | |
10244 | else if (TREE_CODE (name) == SCOPE_REF | |
10245 | && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR) | |
10246 | { | |
10247 | tree base = tsubst_copy (TREE_OPERAND (name, 0), args, | |
10248 | complain, in_decl); | |
10249 | name = TREE_OPERAND (name, 1); | |
10250 | name = tsubst_copy (TREE_OPERAND (name, 0), args, | |
10251 | complain, in_decl); | |
10252 | name = build1 (BIT_NOT_EXPR, NULL_TREE, name); | |
02ed62dd | 10253 | name = build_qualified_name (/*type=*/NULL_TREE, |
3db45ab5 | 10254 | base, name, |
02ed62dd | 10255 | /*template_p=*/false); |
a723baf1 | 10256 | } |
ee76b931 | 10257 | else if (TREE_CODE (name) == BASELINK) |
c8094d83 MS |
10258 | name = tsubst_baselink (name, |
10259 | non_reference (TREE_TYPE (object)), | |
10260 | args, complain, | |
ee76b931 | 10261 | in_decl); |
a723baf1 | 10262 | else |
ee76b931 | 10263 | name = tsubst_copy (name, args, complain, in_decl); |
44de5aeb | 10264 | return build_nt (COMPONENT_REF, object, name, NULL_TREE); |
a723baf1 MM |
10265 | } |
10266 | ||
5566b478 MS |
10267 | case PLUS_EXPR: |
10268 | case MINUS_EXPR: | |
10269 | case MULT_EXPR: | |
10270 | case TRUNC_DIV_EXPR: | |
10271 | case CEIL_DIV_EXPR: | |
10272 | case FLOOR_DIV_EXPR: | |
10273 | case ROUND_DIV_EXPR: | |
10274 | case EXACT_DIV_EXPR: | |
10275 | case BIT_AND_EXPR: | |
5566b478 MS |
10276 | case BIT_IOR_EXPR: |
10277 | case BIT_XOR_EXPR: | |
10278 | case TRUNC_MOD_EXPR: | |
10279 | case FLOOR_MOD_EXPR: | |
10280 | case TRUTH_ANDIF_EXPR: | |
10281 | case TRUTH_ORIF_EXPR: | |
10282 | case TRUTH_AND_EXPR: | |
10283 | case TRUTH_OR_EXPR: | |
10284 | case RSHIFT_EXPR: | |
10285 | case LSHIFT_EXPR: | |
10286 | case RROTATE_EXPR: | |
10287 | case LROTATE_EXPR: | |
10288 | case EQ_EXPR: | |
10289 | case NE_EXPR: | |
10290 | case MAX_EXPR: | |
10291 | case MIN_EXPR: | |
10292 | case LE_EXPR: | |
10293 | case GE_EXPR: | |
10294 | case LT_EXPR: | |
10295 | case GT_EXPR: | |
5566b478 | 10296 | case COMPOUND_EXPR: |
5566b478 MS |
10297 | case DOTSTAR_EXPR: |
10298 | case MEMBER_REF: | |
519c9806 MM |
10299 | case PREDECREMENT_EXPR: |
10300 | case PREINCREMENT_EXPR: | |
10301 | case POSTDECREMENT_EXPR: | |
10302 | case POSTINCREMENT_EXPR: | |
5566b478 | 10303 | return build_nt |
4393e105 MM |
10304 | (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl), |
10305 | tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl)); | |
5566b478 | 10306 | |
02ed62dd MM |
10307 | case SCOPE_REF: |
10308 | return build_qualified_name (/*type=*/NULL_TREE, | |
10309 | tsubst_copy (TREE_OPERAND (t, 0), | |
10310 | args, complain, in_decl), | |
10311 | tsubst_copy (TREE_OPERAND (t, 1), | |
10312 | args, complain, in_decl), | |
10313 | QUALIFIED_NAME_IS_TEMPLATE (t)); | |
10314 | ||
d8987adb NS |
10315 | case ARRAY_REF: |
10316 | return build_nt | |
10317 | (ARRAY_REF, | |
10318 | tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl), | |
10319 | tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl), | |
10320 | NULL_TREE, NULL_TREE); | |
10321 | ||
5566b478 | 10322 | case CALL_EXPR: |
5039610b SL |
10323 | { |
10324 | int n = VL_EXP_OPERAND_LENGTH (t); | |
10325 | tree result = build_vl_exp (CALL_EXPR, n); | |
10326 | int i; | |
10327 | for (i = 0; i < n; i++) | |
10328 | TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args, | |
10329 | complain, in_decl); | |
10330 | return result; | |
10331 | } | |
5566b478 | 10332 | |
5566b478 MS |
10333 | case COND_EXPR: |
10334 | case MODOP_EXPR: | |
40242ccf | 10335 | case PSEUDO_DTOR_EXPR: |
67da3287 | 10336 | { |
8452b1d3 | 10337 | r = build_nt |
4393e105 MM |
10338 | (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl), |
10339 | tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl), | |
10340 | tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl)); | |
e4c2c34b | 10341 | TREE_NO_WARNING (r) = TREE_NO_WARNING (t); |
67da3287 MM |
10342 | return r; |
10343 | } | |
5566b478 MS |
10344 | |
10345 | case NEW_EXPR: | |
10346 | { | |
8452b1d3 | 10347 | r = build_nt |
4393e105 MM |
10348 | (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl), |
10349 | tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl), | |
10350 | tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl)); | |
5566b478 MS |
10351 | NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t); |
10352 | return r; | |
10353 | } | |
10354 | ||
10355 | case DELETE_EXPR: | |
10356 | { | |
8452b1d3 | 10357 | r = build_nt |
4393e105 MM |
10358 | (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl), |
10359 | tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl)); | |
5566b478 MS |
10360 | DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t); |
10361 | DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t); | |
10362 | return r; | |
10363 | } | |
10364 | ||
386b8a85 JM |
10365 | case TEMPLATE_ID_EXPR: |
10366 | { | |
0cbd7506 | 10367 | /* Substituted template arguments */ |
a91db711 NS |
10368 | tree fn = TREE_OPERAND (t, 0); |
10369 | tree targs = TREE_OPERAND (t, 1); | |
856216bb | 10370 | |
a91db711 | 10371 | fn = tsubst_copy (fn, args, complain, in_decl); |
bf12d54d NS |
10372 | if (targs) |
10373 | targs = tsubst_template_args (targs, args, complain, in_decl); | |
c8094d83 | 10374 | |
a91db711 | 10375 | return lookup_template_function (fn, targs); |
386b8a85 JM |
10376 | } |
10377 | ||
5566b478 MS |
10378 | case TREE_LIST: |
10379 | { | |
10380 | tree purpose, value, chain; | |
10381 | ||
10382 | if (t == void_list_node) | |
10383 | return t; | |
10384 | ||
10385 | purpose = TREE_PURPOSE (t); | |
10386 | if (purpose) | |
4393e105 | 10387 | purpose = tsubst_copy (purpose, args, complain, in_decl); |
5566b478 MS |
10388 | value = TREE_VALUE (t); |
10389 | if (value) | |
4393e105 | 10390 | value = tsubst_copy (value, args, complain, in_decl); |
5566b478 MS |
10391 | chain = TREE_CHAIN (t); |
10392 | if (chain && chain != void_type_node) | |
4393e105 | 10393 | chain = tsubst_copy (chain, args, complain, in_decl); |
5566b478 MS |
10394 | if (purpose == TREE_PURPOSE (t) |
10395 | && value == TREE_VALUE (t) | |
10396 | && chain == TREE_CHAIN (t)) | |
10397 | return t; | |
10398 | return tree_cons (purpose, value, chain); | |
10399 | } | |
10400 | ||
10401 | case RECORD_TYPE: | |
10402 | case UNION_TYPE: | |
10403 | case ENUMERAL_TYPE: | |
10404 | case INTEGER_TYPE: | |
10405 | case TEMPLATE_TYPE_PARM: | |
73b0fce8 | 10406 | case TEMPLATE_TEMPLATE_PARM: |
a1281f45 | 10407 | case BOUND_TEMPLATE_TEMPLATE_PARM: |
f84b4be9 | 10408 | case TEMPLATE_PARM_INDEX: |
5566b478 MS |
10409 | case POINTER_TYPE: |
10410 | case REFERENCE_TYPE: | |
10411 | case OFFSET_TYPE: | |
10412 | case FUNCTION_TYPE: | |
10413 | case METHOD_TYPE: | |
10414 | case ARRAY_TYPE: | |
10415 | case TYPENAME_TYPE: | |
b8c6534b | 10416 | case UNBOUND_CLASS_TEMPLATE: |
2a2b1d56 | 10417 | case TYPEOF_TYPE: |
3ad6a8e1 | 10418 | case DECLTYPE_TYPE: |
f84b4be9 | 10419 | case TYPE_DECL: |
4393e105 | 10420 | return tsubst (t, args, complain, in_decl); |
5566b478 | 10421 | |
e92cc029 | 10422 | case IDENTIFIER_NODE: |
421844e7 | 10423 | if (IDENTIFIER_TYPENAME_P (t)) |
1f6e1acc AS |
10424 | { |
10425 | tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl); | |
1f84ec23 | 10426 | return mangle_conv_op_name_for_type (new_type); |
1f6e1acc | 10427 | } |
e92cc029 MS |
10428 | else |
10429 | return t; | |
10430 | ||
5156628f | 10431 | case CONSTRUCTOR: |
4038c495 GB |
10432 | /* This is handled by tsubst_copy_and_build. */ |
10433 | gcc_unreachable (); | |
5156628f | 10434 | |
371534a9 | 10435 | case VA_ARG_EXPR: |
ea333e1c | 10436 | return build_x_va_arg (tsubst_copy (TREE_OPERAND (t, 0), args, complain, |
c2ea3a40 NS |
10437 | in_decl), |
10438 | tsubst (TREE_TYPE (t), args, complain, in_decl)); | |
f9817201 | 10439 | |
543a0daa RH |
10440 | case CLEANUP_POINT_EXPR: |
10441 | /* We shouldn't have built any of these during initial template | |
10442 | generation. Instead, they should be built during instantiation | |
10443 | in response to the saved STMT_IS_FULL_EXPR_P setting. */ | |
315fb5db | 10444 | gcc_unreachable (); |
543a0daa | 10445 | |
7eab6e7b MM |
10446 | case OFFSET_REF: |
10447 | mark_used (TREE_OPERAND (t, 1)); | |
10448 | return t; | |
10449 | ||
5d80a306 DG |
10450 | case EXPR_PACK_EXPANSION: |
10451 | error ("invalid use of pack expansion expression"); | |
10452 | return error_mark_node; | |
10453 | ||
10454 | case NONTYPE_ARGUMENT_PACK: | |
10455 | error ("use %<...%> to expand argument pack"); | |
10456 | return error_mark_node; | |
10457 | ||
5566b478 MS |
10458 | default: |
10459 | return t; | |
10460 | } | |
10461 | } | |
10462 | ||
1799e5d5 RH |
10463 | /* Like tsubst_copy, but specifically for OpenMP clauses. */ |
10464 | ||
10465 | static tree | |
10466 | tsubst_omp_clauses (tree clauses, tree args, tsubst_flags_t complain, | |
10467 | tree in_decl) | |
10468 | { | |
10469 | tree new_clauses = NULL, nc, oc; | |
10470 | ||
10471 | for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc)) | |
10472 | { | |
10473 | nc = copy_node (oc); | |
10474 | OMP_CLAUSE_CHAIN (nc) = new_clauses; | |
10475 | new_clauses = nc; | |
10476 | ||
10477 | switch (OMP_CLAUSE_CODE (nc)) | |
10478 | { | |
a68ab351 JJ |
10479 | case OMP_CLAUSE_LASTPRIVATE: |
10480 | if (OMP_CLAUSE_LASTPRIVATE_STMT (oc)) | |
10481 | { | |
10482 | OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list (); | |
10483 | tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain, | |
10484 | in_decl, /*integral_constant_expression_p=*/false); | |
10485 | OMP_CLAUSE_LASTPRIVATE_STMT (nc) | |
10486 | = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc)); | |
10487 | } | |
10488 | /* FALLTHRU */ | |
1799e5d5 RH |
10489 | case OMP_CLAUSE_PRIVATE: |
10490 | case OMP_CLAUSE_SHARED: | |
10491 | case OMP_CLAUSE_FIRSTPRIVATE: | |
1799e5d5 RH |
10492 | case OMP_CLAUSE_REDUCTION: |
10493 | case OMP_CLAUSE_COPYIN: | |
10494 | case OMP_CLAUSE_COPYPRIVATE: | |
10495 | case OMP_CLAUSE_IF: | |
10496 | case OMP_CLAUSE_NUM_THREADS: | |
10497 | case OMP_CLAUSE_SCHEDULE: | |
a68ab351 | 10498 | case OMP_CLAUSE_COLLAPSE: |
1799e5d5 | 10499 | OMP_CLAUSE_OPERAND (nc, 0) |
015c2c66 MM |
10500 | = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain, |
10501 | in_decl, /*integral_constant_expression_p=*/false); | |
1799e5d5 RH |
10502 | break; |
10503 | case OMP_CLAUSE_NOWAIT: | |
10504 | case OMP_CLAUSE_ORDERED: | |
10505 | case OMP_CLAUSE_DEFAULT: | |
a68ab351 | 10506 | case OMP_CLAUSE_UNTIED: |
1799e5d5 RH |
10507 | break; |
10508 | default: | |
10509 | gcc_unreachable (); | |
10510 | } | |
10511 | } | |
10512 | ||
10513 | return finish_omp_clauses (nreverse (new_clauses)); | |
10514 | } | |
10515 | ||
f74dcfb7 JJ |
10516 | /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */ |
10517 | ||
10518 | static tree | |
10519 | tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain, | |
10520 | tree in_decl) | |
10521 | { | |
10522 | #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl) | |
10523 | ||
10524 | tree purpose, value, chain; | |
10525 | ||
10526 | if (t == NULL) | |
10527 | return t; | |
10528 | ||
10529 | if (TREE_CODE (t) != TREE_LIST) | |
10530 | return tsubst_copy_and_build (t, args, complain, in_decl, | |
015c2c66 MM |
10531 | /*function_p=*/false, |
10532 | /*integral_constant_expression_p=*/false); | |
f74dcfb7 JJ |
10533 | |
10534 | if (t == void_list_node) | |
10535 | return t; | |
10536 | ||
10537 | purpose = TREE_PURPOSE (t); | |
10538 | if (purpose) | |
10539 | purpose = RECUR (purpose); | |
10540 | value = TREE_VALUE (t); | |
10541 | if (value) | |
10542 | value = RECUR (value); | |
10543 | chain = TREE_CHAIN (t); | |
10544 | if (chain && chain != void_type_node) | |
10545 | chain = RECUR (chain); | |
10546 | return tree_cons (purpose, value, chain); | |
10547 | #undef RECUR | |
10548 | } | |
10549 | ||
a68ab351 JJ |
10550 | /* Substitute one OMP_FOR iterator. */ |
10551 | ||
10552 | static void | |
10553 | tsubst_omp_for_iterator (tree t, int i, tree declv, tree initv, | |
10554 | tree condv, tree incrv, tree *clauses, | |
10555 | tree args, tsubst_flags_t complain, tree in_decl, | |
10556 | bool integral_constant_expression_p) | |
10557 | { | |
10558 | #define RECUR(NODE) \ | |
10559 | tsubst_expr ((NODE), args, complain, in_decl, \ | |
10560 | integral_constant_expression_p) | |
a2d4cdc9 | 10561 | tree decl, init, cond, incr, auto_node; |
a68ab351 JJ |
10562 | |
10563 | init = TREE_VEC_ELT (OMP_FOR_INIT (t), i); | |
10564 | gcc_assert (TREE_CODE (init) == MODIFY_EXPR); | |
10565 | decl = RECUR (TREE_OPERAND (init, 0)); | |
10566 | init = TREE_OPERAND (init, 1); | |
a2d4cdc9 JJ |
10567 | auto_node = type_uses_auto (TREE_TYPE (decl)); |
10568 | if (auto_node && init) | |
10569 | { | |
10570 | tree init_expr = init; | |
a2d4cdc9 JJ |
10571 | if (TREE_CODE (init_expr) == DECL_EXPR) |
10572 | init_expr = DECL_INITIAL (DECL_EXPR_DECL (init_expr)); | |
749db154 | 10573 | init_expr = RECUR (init_expr); |
a2d4cdc9 JJ |
10574 | TREE_TYPE (decl) |
10575 | = do_auto_deduction (TREE_TYPE (decl), init_expr, auto_node); | |
a2d4cdc9 | 10576 | } |
a68ab351 JJ |
10577 | gcc_assert (!type_dependent_expression_p (decl)); |
10578 | ||
10579 | if (!CLASS_TYPE_P (TREE_TYPE (decl))) | |
10580 | { | |
10581 | cond = RECUR (TREE_VEC_ELT (OMP_FOR_COND (t), i)); | |
10582 | incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i); | |
10583 | if (TREE_CODE (incr) == MODIFY_EXPR) | |
10584 | incr = build_x_modify_expr (RECUR (TREE_OPERAND (incr, 0)), NOP_EXPR, | |
10585 | RECUR (TREE_OPERAND (incr, 1)), | |
10586 | complain); | |
10587 | else | |
10588 | incr = RECUR (incr); | |
10589 | TREE_VEC_ELT (declv, i) = decl; | |
10590 | TREE_VEC_ELT (initv, i) = init; | |
10591 | TREE_VEC_ELT (condv, i) = cond; | |
10592 | TREE_VEC_ELT (incrv, i) = incr; | |
10593 | return; | |
10594 | } | |
10595 | ||
10596 | if (init && TREE_CODE (init) != DECL_EXPR) | |
10597 | { | |
10598 | tree c; | |
10599 | for (c = *clauses; c ; c = OMP_CLAUSE_CHAIN (c)) | |
10600 | { | |
10601 | if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE | |
10602 | || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE) | |
10603 | && OMP_CLAUSE_DECL (c) == decl) | |
10604 | break; | |
10605 | else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE | |
10606 | && OMP_CLAUSE_DECL (c) == decl) | |
10607 | error ("iteration variable %qD should not be firstprivate", decl); | |
10608 | else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION | |
10609 | && OMP_CLAUSE_DECL (c) == decl) | |
10610 | error ("iteration variable %qD should not be reduction", decl); | |
10611 | } | |
10612 | if (c == NULL) | |
10613 | { | |
c2255bc4 | 10614 | c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE); |
a68ab351 JJ |
10615 | OMP_CLAUSE_DECL (c) = decl; |
10616 | c = finish_omp_clauses (c); | |
10617 | if (c) | |
10618 | { | |
10619 | OMP_CLAUSE_CHAIN (c) = *clauses; | |
10620 | *clauses = c; | |
10621 | } | |
10622 | } | |
10623 | } | |
10624 | cond = TREE_VEC_ELT (OMP_FOR_COND (t), i); | |
10625 | if (COMPARISON_CLASS_P (cond)) | |
10626 | cond = build2 (TREE_CODE (cond), boolean_type_node, | |
10627 | RECUR (TREE_OPERAND (cond, 0)), | |
10628 | RECUR (TREE_OPERAND (cond, 1))); | |
10629 | else | |
10630 | cond = RECUR (cond); | |
10631 | incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i); | |
10632 | switch (TREE_CODE (incr)) | |
10633 | { | |
10634 | case PREINCREMENT_EXPR: | |
10635 | case PREDECREMENT_EXPR: | |
10636 | case POSTINCREMENT_EXPR: | |
10637 | case POSTDECREMENT_EXPR: | |
10638 | incr = build2 (TREE_CODE (incr), TREE_TYPE (decl), | |
10639 | RECUR (TREE_OPERAND (incr, 0)), NULL_TREE); | |
10640 | break; | |
10641 | case MODIFY_EXPR: | |
10642 | if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR | |
10643 | || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR) | |
10644 | { | |
10645 | tree rhs = TREE_OPERAND (incr, 1); | |
10646 | incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), | |
10647 | RECUR (TREE_OPERAND (incr, 0)), | |
10648 | build2 (TREE_CODE (rhs), TREE_TYPE (decl), | |
10649 | RECUR (TREE_OPERAND (rhs, 0)), | |
10650 | RECUR (TREE_OPERAND (rhs, 1)))); | |
10651 | } | |
10652 | else | |
10653 | incr = RECUR (incr); | |
10654 | break; | |
10655 | case MODOP_EXPR: | |
10656 | if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR | |
10657 | || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR) | |
10658 | { | |
10659 | tree lhs = RECUR (TREE_OPERAND (incr, 0)); | |
10660 | incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs, | |
10661 | build2 (TREE_CODE (TREE_OPERAND (incr, 1)), | |
10662 | TREE_TYPE (decl), lhs, | |
10663 | RECUR (TREE_OPERAND (incr, 2)))); | |
10664 | } | |
10665 | else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR | |
10666 | && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR | |
10667 | || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR))) | |
10668 | { | |
10669 | tree rhs = TREE_OPERAND (incr, 2); | |
10670 | incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), | |
10671 | RECUR (TREE_OPERAND (incr, 0)), | |
10672 | build2 (TREE_CODE (rhs), TREE_TYPE (decl), | |
10673 | RECUR (TREE_OPERAND (rhs, 0)), | |
10674 | RECUR (TREE_OPERAND (rhs, 1)))); | |
10675 | } | |
10676 | else | |
10677 | incr = RECUR (incr); | |
10678 | break; | |
10679 | default: | |
10680 | incr = RECUR (incr); | |
10681 | break; | |
10682 | } | |
10683 | ||
10684 | TREE_VEC_ELT (declv, i) = decl; | |
10685 | TREE_VEC_ELT (initv, i) = init; | |
10686 | TREE_VEC_ELT (condv, i) = cond; | |
10687 | TREE_VEC_ELT (incrv, i) = incr; | |
10688 | #undef RECUR | |
10689 | } | |
10690 | ||
cc23546e JO |
10691 | /* Like tsubst_copy for expressions, etc. but also does semantic |
10692 | processing. */ | |
00d3396f | 10693 | |
14d22dd6 | 10694 | static tree |
015c2c66 MM |
10695 | tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl, |
10696 | bool integral_constant_expression_p) | |
5566b478 | 10697 | { |
015c2c66 MM |
10698 | #define RECUR(NODE) \ |
10699 | tsubst_expr ((NODE), args, complain, in_decl, \ | |
10700 | integral_constant_expression_p) | |
10701 | ||
fd10dd09 | 10702 | tree stmt, tmp; |
558475f0 | 10703 | |
5566b478 MS |
10704 | if (t == NULL_TREE || t == error_mark_node) |
10705 | return t; | |
10706 | ||
93409b8c PB |
10707 | if (EXPR_HAS_LOCATION (t)) |
10708 | input_location = EXPR_LOCATION (t); | |
7c34ced1 RH |
10709 | if (STATEMENT_CODE_P (TREE_CODE (t))) |
10710 | current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t); | |
10711 | ||
5566b478 | 10712 | switch (TREE_CODE (t)) |
8d08fdba | 10713 | { |
325c3691 RH |
10714 | case STATEMENT_LIST: |
10715 | { | |
10716 | tree_stmt_iterator i; | |
10717 | for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i)) | |
015c2c66 | 10718 | RECUR (tsi_stmt (i)); |
325c3691 RH |
10719 | break; |
10720 | } | |
10721 | ||
558475f0 | 10722 | case CTOR_INITIALIZER: |
c8094d83 | 10723 | finish_mem_initializers (tsubst_initializer_list |
2282d28d MM |
10724 | (TREE_OPERAND (t, 0), args)); |
10725 | break; | |
558475f0 | 10726 | |
5088b058 | 10727 | case RETURN_EXPR: |
015c2c66 | 10728 | finish_return_stmt (RECUR (TREE_OPERAND (t, 0))); |
5566b478 MS |
10729 | break; |
10730 | ||
10731 | case EXPR_STMT: | |
015c2c66 | 10732 | tmp = RECUR (EXPR_STMT_EXPR (t)); |
7c34ced1 RH |
10733 | if (EXPR_STMT_STMT_EXPR_RESULT (t)) |
10734 | finish_stmt_expr_expr (tmp, cur_stmt_expr); | |
10735 | else | |
10736 | finish_expr_stmt (tmp); | |
10737 | break; | |
5566b478 | 10738 | |
9da99f7d | 10739 | case USING_STMT: |
015c2c66 | 10740 | do_using_directive (RECUR (USING_STMT_NAMESPACE (t))); |
9da99f7d | 10741 | break; |
c8094d83 | 10742 | |
350fae66 | 10743 | case DECL_EXPR: |
5566b478 | 10744 | { |
e0942dcd MM |
10745 | tree decl; |
10746 | tree init; | |
5566b478 | 10747 | |
350fae66 | 10748 | decl = DECL_EXPR_DECL (t); |
acef433b MM |
10749 | if (TREE_CODE (decl) == LABEL_DECL) |
10750 | finish_label_decl (DECL_NAME (decl)); | |
fbfe8c9e NS |
10751 | else if (TREE_CODE (decl) == USING_DECL) |
10752 | { | |
98ed9dae | 10753 | tree scope = USING_DECL_SCOPE (decl); |
fbfe8c9e | 10754 | tree name = DECL_NAME (decl); |
22038b2c | 10755 | tree decl; |
c8094d83 | 10756 | |
015c2c66 | 10757 | scope = RECUR (scope); |
22038b2c | 10758 | decl = lookup_qualified_name (scope, name, |
12483c9f NS |
10759 | /*is_type_p=*/false, |
10760 | /*complain=*/false); | |
8f78f01f | 10761 | if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST) |
2b7a3abf | 10762 | qualified_name_lookup_error (scope, name, decl, input_location); |
22038b2c | 10763 | else |
ed5f054f | 10764 | do_local_using_decl (decl, scope, name); |
fbfe8c9e | 10765 | } |
acef433b MM |
10766 | else |
10767 | { | |
10768 | init = DECL_INITIAL (decl); | |
10769 | decl = tsubst (decl, args, complain, in_decl); | |
ce2e5191 NS |
10770 | if (decl != error_mark_node) |
10771 | { | |
0cbd7506 MS |
10772 | /* By marking the declaration as instantiated, we avoid |
10773 | trying to instantiate it. Since instantiate_decl can't | |
10774 | handle local variables, and since we've already done | |
10775 | all that needs to be done, that's the right thing to | |
10776 | do. */ | |
10777 | if (TREE_CODE (decl) == VAR_DECL) | |
10778 | DECL_TEMPLATE_INSTANTIATED (decl) = 1; | |
edebf865 MM |
10779 | if (TREE_CODE (decl) == VAR_DECL |
10780 | && ANON_AGGR_TYPE_P (TREE_TYPE (decl))) | |
10781 | /* Anonymous aggregates are a special case. */ | |
10782 | finish_anon_union (decl); | |
c8094d83 | 10783 | else |
ed2fa432 | 10784 | { |
edebf865 | 10785 | maybe_push_decl (decl); |
39703eb9 MM |
10786 | if (TREE_CODE (decl) == VAR_DECL |
10787 | && DECL_PRETTY_FUNCTION_P (decl)) | |
edebf865 MM |
10788 | { |
10789 | /* For __PRETTY_FUNCTION__ we have to adjust the | |
10790 | initializer. */ | |
10791 | const char *const name | |
10792 | = cxx_printable_name (current_function_decl, 2); | |
bb885938 | 10793 | init = cp_fname_init (name, &TREE_TYPE (decl)); |
edebf865 MM |
10794 | } |
10795 | else | |
268127ce DG |
10796 | { |
10797 | tree t = RECUR (init); | |
10798 | ||
10799 | if (init && !t) | |
10800 | /* If we had an initializer but it | |
10801 | instantiated to nothing, | |
10802 | value-initialize the object. This will | |
10803 | only occur when the initializer was a | |
10804 | pack expansion where the parameter packs | |
10805 | used in that expansion were of length | |
10806 | zero. */ | |
9dbd4406 | 10807 | init = build_value_init (TREE_TYPE (decl)); |
268127ce DG |
10808 | else |
10809 | init = t; | |
10810 | } | |
10811 | ||
bbbbb16a | 10812 | finish_decl (decl, init, NULL_TREE, NULL_TREE); |
ed2fa432 | 10813 | } |
ce2e5191 | 10814 | } |
acef433b | 10815 | } |
fd10dd09 | 10816 | |
350fae66 | 10817 | /* A DECL_EXPR can also be used as an expression, in the condition |
325c3691 RH |
10818 | clause of an if/for/while construct. */ |
10819 | return decl; | |
5566b478 | 10820 | } |
8d08fdba | 10821 | |
5566b478 | 10822 | case FOR_STMT: |
7c34ced1 | 10823 | stmt = begin_for_stmt (); |
015c2c66 | 10824 | RECUR (FOR_INIT_STMT (t)); |
7c34ced1 | 10825 | finish_for_init_stmt (stmt); |
015c2c66 | 10826 | tmp = RECUR (FOR_COND (t)); |
7c34ced1 | 10827 | finish_for_cond (tmp, stmt); |
015c2c66 | 10828 | tmp = RECUR (FOR_EXPR (t)); |
7c34ced1 | 10829 | finish_for_expr (tmp, stmt); |
015c2c66 | 10830 | RECUR (FOR_BODY (t)); |
7c34ced1 | 10831 | finish_for_stmt (stmt); |
5566b478 | 10832 | break; |
8d08fdba | 10833 | |
5566b478 | 10834 | case WHILE_STMT: |
7c34ced1 | 10835 | stmt = begin_while_stmt (); |
015c2c66 | 10836 | tmp = RECUR (WHILE_COND (t)); |
7c34ced1 | 10837 | finish_while_stmt_cond (tmp, stmt); |
015c2c66 | 10838 | RECUR (WHILE_BODY (t)); |
7c34ced1 | 10839 | finish_while_stmt (stmt); |
5566b478 | 10840 | break; |
8d08fdba | 10841 | |
5566b478 | 10842 | case DO_STMT: |
7c34ced1 | 10843 | stmt = begin_do_stmt (); |
015c2c66 | 10844 | RECUR (DO_BODY (t)); |
7c34ced1 | 10845 | finish_do_body (stmt); |
015c2c66 | 10846 | tmp = RECUR (DO_COND (t)); |
7c34ced1 | 10847 | finish_do_stmt (tmp, stmt); |
5566b478 | 10848 | break; |
a0a33927 | 10849 | |
5566b478 | 10850 | case IF_STMT: |
7c34ced1 | 10851 | stmt = begin_if_stmt (); |
015c2c66 | 10852 | tmp = RECUR (IF_COND (t)); |
7c34ced1 | 10853 | finish_if_stmt_cond (tmp, stmt); |
015c2c66 | 10854 | RECUR (THEN_CLAUSE (t)); |
7c34ced1 | 10855 | finish_then_clause (stmt); |
8d08fdba | 10856 | |
7c34ced1 RH |
10857 | if (ELSE_CLAUSE (t)) |
10858 | { | |
10859 | begin_else_clause (stmt); | |
015c2c66 | 10860 | RECUR (ELSE_CLAUSE (t)); |
7c34ced1 RH |
10861 | finish_else_clause (stmt); |
10862 | } | |
10863 | ||
10864 | finish_if_stmt (stmt); | |
5566b478 | 10865 | break; |
8d08fdba | 10866 | |
5882f0f3 | 10867 | case BIND_EXPR: |
7c34ced1 RH |
10868 | if (BIND_EXPR_BODY_BLOCK (t)) |
10869 | stmt = begin_function_body (); | |
10870 | else | |
10871 | stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t) | |
10872 | ? BCS_TRY_BLOCK : 0); | |
ade3dc07 | 10873 | |
015c2c66 | 10874 | RECUR (BIND_EXPR_BODY (t)); |
ade3dc07 | 10875 | |
7c34ced1 RH |
10876 | if (BIND_EXPR_BODY_BLOCK (t)) |
10877 | finish_function_body (stmt); | |
10878 | else | |
10879 | finish_compound_stmt (stmt); | |
5566b478 | 10880 | break; |
8d08fdba | 10881 | |
5566b478 | 10882 | case BREAK_STMT: |
ad321293 | 10883 | finish_break_stmt (); |
5566b478 | 10884 | break; |
8d08fdba | 10885 | |
6467930b | 10886 | case CONTINUE_STMT: |
ad321293 | 10887 | finish_continue_stmt (); |
6467930b MS |
10888 | break; |
10889 | ||
5566b478 | 10890 | case SWITCH_STMT: |
7c34ced1 | 10891 | stmt = begin_switch_stmt (); |
015c2c66 | 10892 | tmp = RECUR (SWITCH_STMT_COND (t)); |
7c34ced1 | 10893 | finish_switch_cond (tmp, stmt); |
015c2c66 | 10894 | RECUR (SWITCH_STMT_BODY (t)); |
7c34ced1 | 10895 | finish_switch_stmt (stmt); |
5566b478 MS |
10896 | break; |
10897 | ||
8c161995 | 10898 | case CASE_LABEL_EXPR: |
c2255bc4 AH |
10899 | finish_case_label (EXPR_LOCATION (t), |
10900 | RECUR (CASE_LOW (t)), | |
015c2c66 | 10901 | RECUR (CASE_HIGH (t))); |
5566b478 MS |
10902 | break; |
10903 | ||
9e14e18f | 10904 | case LABEL_EXPR: |
5bca4e80 ILT |
10905 | { |
10906 | tree decl = LABEL_EXPR_LABEL (t); | |
10907 | tree label; | |
10908 | ||
10909 | label = finish_label_stmt (DECL_NAME (decl)); | |
10910 | if (DECL_ATTRIBUTES (decl) != NULL_TREE) | |
10911 | cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0); | |
10912 | } | |
5566b478 MS |
10913 | break; |
10914 | ||
9e14e18f | 10915 | case GOTO_EXPR: |
fd10dd09 JM |
10916 | tmp = GOTO_DESTINATION (t); |
10917 | if (TREE_CODE (tmp) != LABEL_DECL) | |
aa09da44 MM |
10918 | /* Computed goto's must be tsubst'd into. On the other hand, |
10919 | non-computed gotos must not be; the identifier in question | |
10920 | will have no binding. */ | |
015c2c66 | 10921 | tmp = RECUR (tmp); |
3fa56191 | 10922 | else |
fd10dd09 JM |
10923 | tmp = DECL_NAME (tmp); |
10924 | finish_goto_stmt (tmp); | |
ad321293 MM |
10925 | break; |
10926 | ||
e130a54b | 10927 | case ASM_EXPR: |
c87978aa | 10928 | tmp = finish_asm_stmt |
6de9cd9a | 10929 | (ASM_VOLATILE_P (t), |
015c2c66 | 10930 | RECUR (ASM_STRING (t)), |
f74dcfb7 JJ |
10931 | tsubst_copy_asm_operands (ASM_OUTPUTS (t), args, complain, in_decl), |
10932 | tsubst_copy_asm_operands (ASM_INPUTS (t), args, complain, in_decl), | |
10933 | tsubst_copy_asm_operands (ASM_CLOBBERS (t), args, complain, in_decl)); | |
131263fa AP |
10934 | { |
10935 | tree asm_expr = tmp; | |
10936 | if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR) | |
10937 | asm_expr = TREE_OPERAND (asm_expr, 0); | |
10938 | ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t); | |
10939 | } | |
5566b478 | 10940 | break; |
faf5394a MS |
10941 | |
10942 | case TRY_BLOCK: | |
f1dedc31 | 10943 | if (CLEANUP_P (t)) |
62409b39 | 10944 | { |
57b52417 | 10945 | stmt = begin_try_block (); |
015c2c66 | 10946 | RECUR (TRY_STMTS (t)); |
62409b39 | 10947 | finish_cleanup_try_block (stmt); |
015c2c66 | 10948 | finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt); |
62409b39 | 10949 | } |
f1dedc31 MM |
10950 | else |
10951 | { | |
eaf6fb90 MM |
10952 | tree compound_stmt = NULL_TREE; |
10953 | ||
62409b39 | 10954 | if (FN_TRY_BLOCK_P (t)) |
eaf6fb90 | 10955 | stmt = begin_function_try_block (&compound_stmt); |
62409b39 MM |
10956 | else |
10957 | stmt = begin_try_block (); | |
10958 | ||
015c2c66 | 10959 | RECUR (TRY_STMTS (t)); |
62409b39 MM |
10960 | |
10961 | if (FN_TRY_BLOCK_P (t)) | |
10962 | finish_function_try_block (stmt); | |
10963 | else | |
10964 | finish_try_block (stmt); | |
10965 | ||
015c2c66 | 10966 | RECUR (TRY_HANDLERS (t)); |
84df082b | 10967 | if (FN_TRY_BLOCK_P (t)) |
eaf6fb90 | 10968 | finish_function_handler_sequence (stmt, compound_stmt); |
84df082b MM |
10969 | else |
10970 | finish_handler_sequence (stmt); | |
f1dedc31 | 10971 | } |
faf5394a | 10972 | break; |
c8094d83 | 10973 | |
faf5394a | 10974 | case HANDLER: |
b35d4555 | 10975 | { |
3a2419a7 | 10976 | tree decl = HANDLER_PARMS (t); |
b35d4555 | 10977 | |
3a2419a7 | 10978 | if (decl) |
b35d4555 | 10979 | { |
b35d4555 | 10980 | decl = tsubst (decl, args, complain, in_decl); |
f8191e64 MM |
10981 | /* Prevent instantiate_decl from trying to instantiate |
10982 | this variable. We've already done all that needs to be | |
10983 | done. */ | |
3a2419a7 VR |
10984 | if (decl != error_mark_node) |
10985 | DECL_TEMPLATE_INSTANTIATED (decl) = 1; | |
b35d4555 | 10986 | } |
3a2419a7 | 10987 | stmt = begin_handler (); |
1a6025b4 | 10988 | finish_handler_parms (decl, stmt); |
015c2c66 | 10989 | RECUR (HANDLER_BODY (t)); |
1a6025b4 | 10990 | finish_handler (stmt); |
b35d4555 | 10991 | } |
faf5394a MS |
10992 | break; |
10993 | ||
b87692e5 | 10994 | case TAG_DEFN: |
fd10dd09 | 10995 | tsubst (TREE_TYPE (t), args, complain, NULL_TREE); |
b87692e5 | 10996 | break; |
55a3debe DG |
10997 | |
10998 | case STATIC_ASSERT: | |
10999 | { | |
11000 | tree condition = | |
11001 | tsubst_expr (STATIC_ASSERT_CONDITION (t), | |
11002 | args, | |
11003 | complain, in_decl, | |
11004 | /*integral_constant_expression_p=*/true); | |
11005 | finish_static_assert (condition, | |
11006 | STATIC_ASSERT_MESSAGE (t), | |
11007 | STATIC_ASSERT_SOURCE_LOCATION (t), | |
11008 | /*member_p=*/false); | |
11009 | } | |
11010 | break; | |
b87692e5 | 11011 | |
1799e5d5 RH |
11012 | case OMP_PARALLEL: |
11013 | tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), | |
11014 | args, complain, in_decl); | |
11015 | stmt = begin_omp_parallel (); | |
015c2c66 | 11016 | RECUR (OMP_PARALLEL_BODY (t)); |
761041be JJ |
11017 | OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt)) |
11018 | = OMP_PARALLEL_COMBINED (t); | |
1799e5d5 RH |
11019 | break; |
11020 | ||
a68ab351 JJ |
11021 | case OMP_TASK: |
11022 | tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), | |
11023 | args, complain, in_decl); | |
11024 | stmt = begin_omp_task (); | |
11025 | RECUR (OMP_TASK_BODY (t)); | |
11026 | finish_omp_task (tmp, stmt); | |
11027 | break; | |
11028 | ||
1799e5d5 RH |
11029 | case OMP_FOR: |
11030 | { | |
a68ab351 JJ |
11031 | tree clauses, body, pre_body; |
11032 | tree declv, initv, condv, incrv; | |
11033 | int i; | |
1799e5d5 RH |
11034 | |
11035 | clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), | |
11036 | args, complain, in_decl); | |
a68ab351 JJ |
11037 | declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t))); |
11038 | initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t))); | |
11039 | condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t))); | |
11040 | incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t))); | |
11041 | ||
11042 | for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++) | |
11043 | tsubst_omp_for_iterator (t, i, declv, initv, condv, incrv, | |
11044 | &clauses, args, complain, in_decl, | |
11045 | integral_constant_expression_p); | |
1799e5d5 RH |
11046 | |
11047 | stmt = begin_omp_structured_block (); | |
11048 | ||
a68ab351 JJ |
11049 | for (i = 0; i < TREE_VEC_LENGTH (initv); i++) |
11050 | if (TREE_VEC_ELT (initv, i) == NULL | |
11051 | || TREE_CODE (TREE_VEC_ELT (initv, i)) != DECL_EXPR) | |
11052 | TREE_VEC_ELT (initv, i) = RECUR (TREE_VEC_ELT (initv, i)); | |
11053 | else if (CLASS_TYPE_P (TREE_TYPE (TREE_VEC_ELT (initv, i)))) | |
11054 | { | |
11055 | tree init = RECUR (TREE_VEC_ELT (initv, i)); | |
11056 | gcc_assert (init == TREE_VEC_ELT (declv, i)); | |
11057 | TREE_VEC_ELT (initv, i) = NULL_TREE; | |
11058 | } | |
11059 | else | |
11060 | { | |
11061 | tree decl_expr = TREE_VEC_ELT (initv, i); | |
11062 | tree init = DECL_INITIAL (DECL_EXPR_DECL (decl_expr)); | |
11063 | gcc_assert (init != NULL); | |
11064 | TREE_VEC_ELT (initv, i) = RECUR (init); | |
11065 | DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL; | |
11066 | RECUR (decl_expr); | |
11067 | DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init; | |
11068 | } | |
11069 | ||
1799e5d5 | 11070 | pre_body = push_stmt_list (); |
015c2c66 | 11071 | RECUR (OMP_FOR_PRE_BODY (t)); |
1799e5d5 RH |
11072 | pre_body = pop_stmt_list (pre_body); |
11073 | ||
11074 | body = push_stmt_list (); | |
015c2c66 | 11075 | RECUR (OMP_FOR_BODY (t)); |
1799e5d5 RH |
11076 | body = pop_stmt_list (body); |
11077 | ||
a68ab351 JJ |
11078 | t = finish_omp_for (EXPR_LOCATION (t), declv, initv, condv, incrv, |
11079 | body, pre_body, clauses); | |
1799e5d5 RH |
11080 | |
11081 | add_stmt (finish_omp_structured_block (stmt)); | |
11082 | } | |
11083 | break; | |
11084 | ||
11085 | case OMP_SECTIONS: | |
11086 | case OMP_SINGLE: | |
11087 | tmp = tsubst_omp_clauses (OMP_CLAUSES (t), args, complain, in_decl); | |
11088 | stmt = push_stmt_list (); | |
015c2c66 | 11089 | RECUR (OMP_BODY (t)); |
1799e5d5 RH |
11090 | stmt = pop_stmt_list (stmt); |
11091 | ||
11092 | t = copy_node (t); | |
11093 | OMP_BODY (t) = stmt; | |
11094 | OMP_CLAUSES (t) = tmp; | |
11095 | add_stmt (t); | |
11096 | break; | |
11097 | ||
11098 | case OMP_SECTION: | |
11099 | case OMP_CRITICAL: | |
11100 | case OMP_MASTER: | |
11101 | case OMP_ORDERED: | |
11102 | stmt = push_stmt_list (); | |
015c2c66 | 11103 | RECUR (OMP_BODY (t)); |
1799e5d5 RH |
11104 | stmt = pop_stmt_list (stmt); |
11105 | ||
11106 | t = copy_node (t); | |
11107 | OMP_BODY (t) = stmt; | |
11108 | add_stmt (t); | |
11109 | break; | |
11110 | ||
11111 | case OMP_ATOMIC: | |
239371f9 JJ |
11112 | gcc_assert (OMP_ATOMIC_DEPENDENT_P (t)); |
11113 | { | |
11114 | tree op1 = TREE_OPERAND (t, 1); | |
11115 | tree lhs = RECUR (TREE_OPERAND (op1, 0)); | |
11116 | tree rhs = RECUR (TREE_OPERAND (op1, 1)); | |
11117 | finish_omp_atomic (TREE_CODE (op1), lhs, rhs); | |
11118 | } | |
1799e5d5 RH |
11119 | break; |
11120 | ||
5d80a306 DG |
11121 | case EXPR_PACK_EXPANSION: |
11122 | error ("invalid use of pack expansion expression"); | |
11123 | return error_mark_node; | |
11124 | ||
11125 | case NONTYPE_ARGUMENT_PACK: | |
11126 | error ("use %<...%> to expand argument pack"); | |
11127 | return error_mark_node; | |
11128 | ||
5566b478 | 11129 | default: |
315fb5db | 11130 | gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t))); |
c8094d83 | 11131 | |
315fb5db | 11132 | return tsubst_copy_and_build (t, args, complain, in_decl, |
015c2c66 MM |
11133 | /*function_p=*/false, |
11134 | integral_constant_expression_p); | |
5566b478 | 11135 | } |
fd10dd09 | 11136 | |
325c3691 | 11137 | return NULL_TREE; |
015c2c66 | 11138 | #undef RECUR |
8d08fdba MS |
11139 | } |
11140 | ||
b3445994 MM |
11141 | /* T is a postfix-expression that is not being used in a function |
11142 | call. Return the substituted version of T. */ | |
11143 | ||
11144 | static tree | |
c8094d83 | 11145 | tsubst_non_call_postfix_expression (tree t, tree args, |
b3445994 MM |
11146 | tsubst_flags_t complain, |
11147 | tree in_decl) | |
11148 | { | |
11149 | if (TREE_CODE (t) == SCOPE_REF) | |
11150 | t = tsubst_qualified_id (t, args, complain, in_decl, | |
11151 | /*done=*/false, /*address_p=*/false); | |
11152 | else | |
11153 | t = tsubst_copy_and_build (t, args, complain, in_decl, | |
015c2c66 MM |
11154 | /*function_p=*/false, |
11155 | /*integral_constant_expression_p=*/false); | |
b3445994 MM |
11156 | |
11157 | return t; | |
11158 | } | |
11159 | ||
cc23546e | 11160 | /* Like tsubst but deals with expressions and performs semantic |
b3445994 | 11161 | analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */ |
cc23546e | 11162 | |
ee76b931 | 11163 | tree |
c8094d83 | 11164 | tsubst_copy_and_build (tree t, |
0cbd7506 MS |
11165 | tree args, |
11166 | tsubst_flags_t complain, | |
11167 | tree in_decl, | |
015c2c66 MM |
11168 | bool function_p, |
11169 | bool integral_constant_expression_p) | |
cc23546e | 11170 | { |
015c2c66 MM |
11171 | #define RECUR(NODE) \ |
11172 | tsubst_copy_and_build (NODE, args, complain, in_decl, \ | |
11173 | /*function_p=*/false, \ | |
11174 | integral_constant_expression_p) | |
b3445994 | 11175 | |
ee76b931 MM |
11176 | tree op1; |
11177 | ||
cc23546e JO |
11178 | if (t == NULL_TREE || t == error_mark_node) |
11179 | return t; | |
11180 | ||
11181 | switch (TREE_CODE (t)) | |
11182 | { | |
399dedb9 NS |
11183 | case USING_DECL: |
11184 | t = DECL_NAME (t); | |
852dcbdd | 11185 | /* Fall through. */ |
b3445994 | 11186 | case IDENTIFIER_NODE: |
cc23546e | 11187 | { |
b3445994 | 11188 | tree decl; |
b3445994 | 11189 | cp_id_kind idk; |
67c03833 | 11190 | bool non_integral_constant_expression_p; |
b3445994 MM |
11191 | const char *error_msg; |
11192 | ||
b3445994 | 11193 | if (IDENTIFIER_TYPENAME_P (t)) |
cc23546e | 11194 | { |
b3445994 MM |
11195 | tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl); |
11196 | t = mangle_conv_op_name_for_type (new_type); | |
cc23546e | 11197 | } |
b3445994 MM |
11198 | |
11199 | /* Look up the name. */ | |
10e6657a | 11200 | decl = lookup_name (t); |
b3445994 MM |
11201 | |
11202 | /* By convention, expressions use ERROR_MARK_NODE to indicate | |
11203 | failure, not NULL_TREE. */ | |
11204 | if (decl == NULL_TREE) | |
11205 | decl = error_mark_node; | |
11206 | ||
10b1d5e7 | 11207 | decl = finish_id_expression (t, decl, NULL_TREE, |
b3445994 | 11208 | &idk, |
015c2c66 | 11209 | integral_constant_expression_p, |
67c03833 JM |
11210 | /*allow_non_integral_constant_expression_p=*/false, |
11211 | &non_integral_constant_expression_p, | |
02ed62dd MM |
11212 | /*template_p=*/false, |
11213 | /*done=*/true, | |
11214 | /*address_p=*/false, | |
11215 | /*template_arg_p=*/false, | |
2b7a3abf DS |
11216 | &error_msg, |
11217 | input_location); | |
b3445994 MM |
11218 | if (error_msg) |
11219 | error (error_msg); | |
11220 | if (!function_p && TREE_CODE (decl) == IDENTIFIER_NODE) | |
11221 | decl = unqualified_name_lookup_error (decl); | |
11222 | return decl; | |
cc23546e JO |
11223 | } |
11224 | ||
11225 | case TEMPLATE_ID_EXPR: | |
11226 | { | |
11227 | tree object; | |
be93747e | 11228 | tree templ = RECUR (TREE_OPERAND (t, 0)); |
bf12d54d NS |
11229 | tree targs = TREE_OPERAND (t, 1); |
11230 | ||
11231 | if (targs) | |
11232 | targs = tsubst_template_args (targs, args, complain, in_decl); | |
c8094d83 | 11233 | |
be93747e | 11234 | if (TREE_CODE (templ) == COMPONENT_REF) |
cc23546e | 11235 | { |
be93747e KG |
11236 | object = TREE_OPERAND (templ, 0); |
11237 | templ = TREE_OPERAND (templ, 1); | |
cc23546e JO |
11238 | } |
11239 | else | |
11240 | object = NULL_TREE; | |
be93747e | 11241 | templ = lookup_template_function (templ, targs); |
c8094d83 | 11242 | |
cc23546e | 11243 | if (object) |
be93747e KG |
11244 | return build3 (COMPONENT_REF, TREE_TYPE (templ), |
11245 | object, templ, NULL_TREE); | |
cc23546e | 11246 | else |
be93747e | 11247 | return baselink_for_fns (templ); |
cc23546e JO |
11248 | } |
11249 | ||
11250 | case INDIRECT_REF: | |
db24eb1f NS |
11251 | { |
11252 | tree r = RECUR (TREE_OPERAND (t, 0)); | |
11253 | ||
11254 | if (REFERENCE_REF_P (t)) | |
11255 | { | |
e8c66fe0 NS |
11256 | /* A type conversion to reference type will be enclosed in |
11257 | such an indirect ref, but the substitution of the cast | |
11258 | will have also added such an indirect ref. */ | |
11259 | if (TREE_CODE (TREE_TYPE (r)) == REFERENCE_TYPE) | |
11260 | r = convert_from_reference (r); | |
db24eb1f NS |
11261 | } |
11262 | else | |
5ade1ed2 | 11263 | r = build_x_indirect_ref (r, "unary *", complain); |
db24eb1f NS |
11264 | return r; |
11265 | } | |
cc23546e | 11266 | |
0da99d4e GB |
11267 | case NOP_EXPR: |
11268 | return build_nop | |
11269 | (tsubst (TREE_TYPE (t), args, complain, in_decl), | |
11270 | RECUR (TREE_OPERAND (t, 0))); | |
11271 | ||
cc23546e | 11272 | case CAST_EXPR: |
cc23546e | 11273 | case REINTERPRET_CAST_EXPR: |
cc23546e | 11274 | case CONST_CAST_EXPR: |
cc23546e | 11275 | case DYNAMIC_CAST_EXPR: |
cc23546e | 11276 | case STATIC_CAST_EXPR: |
015c2c66 MM |
11277 | { |
11278 | tree type; | |
11279 | tree op; | |
11280 | ||
11281 | type = tsubst (TREE_TYPE (t), args, complain, in_decl); | |
11282 | if (integral_constant_expression_p | |
11283 | && !cast_valid_in_integral_constant_expression_p (type)) | |
11284 | { | |
5ade1ed2 DG |
11285 | if (complain & tf_error) |
11286 | error ("a cast to a type other than an integral or " | |
11287 | "enumeration type cannot appear in a constant-expression"); | |
015c2c66 MM |
11288 | return error_mark_node; |
11289 | } | |
11290 | ||
11291 | op = RECUR (TREE_OPERAND (t, 0)); | |
11292 | ||
11293 | switch (TREE_CODE (t)) | |
11294 | { | |
11295 | case CAST_EXPR: | |
5ade1ed2 | 11296 | return build_functional_cast (type, op, complain); |
015c2c66 | 11297 | case REINTERPRET_CAST_EXPR: |
5ade1ed2 | 11298 | return build_reinterpret_cast (type, op, complain); |
015c2c66 | 11299 | case CONST_CAST_EXPR: |
5ade1ed2 | 11300 | return build_const_cast (type, op, complain); |
015c2c66 | 11301 | case DYNAMIC_CAST_EXPR: |
5ade1ed2 | 11302 | return build_dynamic_cast (type, op, complain); |
015c2c66 | 11303 | case STATIC_CAST_EXPR: |
5ade1ed2 | 11304 | return build_static_cast (type, op, complain); |
015c2c66 MM |
11305 | default: |
11306 | gcc_unreachable (); | |
11307 | } | |
11308 | } | |
cc23546e | 11309 | |
cc23546e JO |
11310 | case POSTDECREMENT_EXPR: |
11311 | case POSTINCREMENT_EXPR: | |
b3445994 MM |
11312 | op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0), |
11313 | args, complain, in_decl); | |
5ade1ed2 | 11314 | return build_x_unary_op (TREE_CODE (t), op1, complain); |
ee76b931 MM |
11315 | |
11316 | case PREDECREMENT_EXPR: | |
11317 | case PREINCREMENT_EXPR: | |
cc23546e JO |
11318 | case NEGATE_EXPR: |
11319 | case BIT_NOT_EXPR: | |
cc23546e | 11320 | case ABS_EXPR: |
d17811fd | 11321 | case TRUTH_NOT_EXPR: |
392e3d51 | 11322 | case UNARY_PLUS_EXPR: /* Unary + */ |
d17811fd MM |
11323 | case REALPART_EXPR: |
11324 | case IMAGPART_EXPR: | |
5ade1ed2 DG |
11325 | return build_x_unary_op (TREE_CODE (t), RECUR (TREE_OPERAND (t, 0)), |
11326 | complain); | |
cc23546e | 11327 | |
cc23546e | 11328 | case ADDR_EXPR: |
ee76b931 MM |
11329 | op1 = TREE_OPERAND (t, 0); |
11330 | if (TREE_CODE (op1) == SCOPE_REF) | |
c8094d83 | 11331 | op1 = tsubst_qualified_id (op1, args, complain, in_decl, |
ee76b931 MM |
11332 | /*done=*/true, /*address_p=*/true); |
11333 | else | |
c8094d83 | 11334 | op1 = tsubst_non_call_postfix_expression (op1, args, complain, |
b3445994 | 11335 | in_decl); |
fc2b8477 | 11336 | if (TREE_CODE (op1) == LABEL_DECL) |
6a3799eb AH |
11337 | return finish_label_address_expr (DECL_NAME (op1), |
11338 | EXPR_LOCATION (op1)); | |
5ade1ed2 | 11339 | return build_x_unary_op (ADDR_EXPR, op1, complain); |
ee76b931 | 11340 | |
cc23546e JO |
11341 | case PLUS_EXPR: |
11342 | case MINUS_EXPR: | |
11343 | case MULT_EXPR: | |
11344 | case TRUNC_DIV_EXPR: | |
11345 | case CEIL_DIV_EXPR: | |
11346 | case FLOOR_DIV_EXPR: | |
11347 | case ROUND_DIV_EXPR: | |
11348 | case EXACT_DIV_EXPR: | |
11349 | case BIT_AND_EXPR: | |
cc23546e JO |
11350 | case BIT_IOR_EXPR: |
11351 | case BIT_XOR_EXPR: | |
11352 | case TRUNC_MOD_EXPR: | |
11353 | case FLOOR_MOD_EXPR: | |
11354 | case TRUTH_ANDIF_EXPR: | |
11355 | case TRUTH_ORIF_EXPR: | |
11356 | case TRUTH_AND_EXPR: | |
11357 | case TRUTH_OR_EXPR: | |
11358 | case RSHIFT_EXPR: | |
11359 | case LSHIFT_EXPR: | |
11360 | case RROTATE_EXPR: | |
11361 | case LROTATE_EXPR: | |
11362 | case EQ_EXPR: | |
11363 | case NE_EXPR: | |
11364 | case MAX_EXPR: | |
11365 | case MIN_EXPR: | |
11366 | case LE_EXPR: | |
11367 | case GE_EXPR: | |
11368 | case LT_EXPR: | |
11369 | case GT_EXPR: | |
11370 | case MEMBER_REF: | |
b3445994 | 11371 | case DOTSTAR_EXPR: |
cc23546e | 11372 | return build_x_binary_op |
c8094d83 | 11373 | (TREE_CODE (t), |
b3445994 | 11374 | RECUR (TREE_OPERAND (t, 0)), |
2a67bec2 ILT |
11375 | (TREE_NO_WARNING (TREE_OPERAND (t, 0)) |
11376 | ? ERROR_MARK | |
11377 | : TREE_CODE (TREE_OPERAND (t, 0))), | |
ec835fb2 | 11378 | RECUR (TREE_OPERAND (t, 1)), |
2a67bec2 ILT |
11379 | (TREE_NO_WARNING (TREE_OPERAND (t, 1)) |
11380 | ? ERROR_MARK | |
11381 | : TREE_CODE (TREE_OPERAND (t, 1))), | |
5ade1ed2 DG |
11382 | /*overloaded_p=*/NULL, |
11383 | complain); | |
cc23546e JO |
11384 | |
11385 | case SCOPE_REF: | |
ee76b931 MM |
11386 | return tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true, |
11387 | /*address_p=*/false); | |
cc23546e | 11388 | case ARRAY_REF: |
b3445994 MM |
11389 | op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0), |
11390 | args, complain, in_decl); | |
493e377c | 11391 | return build_x_array_ref (op1, RECUR (TREE_OPERAND (t, 1)), complain); |
c8094d83 | 11392 | |
cc23546e | 11393 | case SIZEOF_EXPR: |
5d80a306 | 11394 | if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))) |
063cc99c | 11395 | return tsubst_copy (t, args, complain, in_decl); |
5d80a306 DG |
11396 | /* Fall through */ |
11397 | ||
cc23546e | 11398 | case ALIGNOF_EXPR: |
d17811fd MM |
11399 | op1 = TREE_OPERAND (t, 0); |
11400 | if (!args) | |
11401 | { | |
11402 | /* When there are no ARGS, we are trying to evaluate a | |
11403 | non-dependent expression from the parser. Trying to do | |
11404 | the substitutions may not work. */ | |
11405 | if (!TYPE_P (op1)) | |
11406 | op1 = TREE_TYPE (op1); | |
11407 | } | |
11408 | else | |
11409 | { | |
11410 | ++skip_evaluation; | |
015c2c66 MM |
11411 | op1 = tsubst_copy_and_build (op1, args, complain, in_decl, |
11412 | /*function_p=*/false, | |
11413 | /*integral_constant_expression_p=*/false); | |
d17811fd MM |
11414 | --skip_evaluation; |
11415 | } | |
7a18b933 | 11416 | if (TYPE_P (op1)) |
5ade1ed2 DG |
11417 | return cxx_sizeof_or_alignof_type (op1, TREE_CODE (t), |
11418 | complain & tf_error); | |
d17811fd | 11419 | else |
5ade1ed2 DG |
11420 | return cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t), |
11421 | complain & tf_error); | |
cc23546e JO |
11422 | |
11423 | case MODOP_EXPR: | |
e4c2c34b JM |
11424 | { |
11425 | tree r = build_x_modify_expr | |
11426 | (RECUR (TREE_OPERAND (t, 0)), | |
11427 | TREE_CODE (TREE_OPERAND (t, 1)), | |
5ade1ed2 DG |
11428 | RECUR (TREE_OPERAND (t, 2)), |
11429 | complain); | |
bcf9a914 JM |
11430 | /* TREE_NO_WARNING must be set if either the expression was |
11431 | parenthesized or it uses an operator such as >>= rather | |
11432 | than plain assignment. In the former case, it was already | |
11433 | set and must be copied. In the latter case, | |
11434 | build_x_modify_expr sets it and it must not be reset | |
11435 | here. */ | |
11436 | if (TREE_NO_WARNING (t)) | |
11437 | TREE_NO_WARNING (r) = TREE_NO_WARNING (t); | |
e4c2c34b JM |
11438 | return r; |
11439 | } | |
cc23546e JO |
11440 | |
11441 | case ARROW_EXPR: | |
b3445994 MM |
11442 | op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0), |
11443 | args, complain, in_decl); | |
ee76b931 MM |
11444 | /* Remember that there was a reference to this entity. */ |
11445 | if (DECL_P (op1)) | |
11446 | mark_used (op1); | |
11447 | return build_x_arrow (op1); | |
cc23546e JO |
11448 | |
11449 | case NEW_EXPR: | |
268127ce | 11450 | { |
c166b898 | 11451 | tree placement = RECUR (TREE_OPERAND (t, 0)); |
268127ce | 11452 | tree init = RECUR (TREE_OPERAND (t, 3)); |
c166b898 ILT |
11453 | VEC(tree,gc) *placement_vec; |
11454 | VEC(tree,gc) *init_vec; | |
11455 | tree ret; | |
268127ce | 11456 | |
c166b898 ILT |
11457 | if (placement == NULL_TREE) |
11458 | placement_vec = NULL; | |
11459 | else | |
11460 | { | |
11461 | placement_vec = make_tree_vector (); | |
11462 | for (; placement != NULL_TREE; placement = TREE_CHAIN (placement)) | |
11463 | VEC_safe_push (tree, gc, placement_vec, TREE_VALUE (placement)); | |
11464 | } | |
268127ce | 11465 | |
c166b898 ILT |
11466 | /* If there was an initializer in the original tree, but it |
11467 | instantiated to an empty list, then we should pass a | |
11468 | non-NULL empty vector to tell build_new that it was an | |
11469 | empty initializer() rather than no initializer. This can | |
11470 | only happen when the initializer is a pack expansion whose | |
11471 | parameter packs are of length zero. */ | |
11472 | if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE) | |
11473 | init_vec = NULL; | |
11474 | else | |
11475 | { | |
11476 | init_vec = make_tree_vector (); | |
11477 | if (init == void_zero_node) | |
11478 | gcc_assert (init_vec != NULL); | |
11479 | else | |
11480 | { | |
11481 | for (; init != NULL_TREE; init = TREE_CHAIN (init)) | |
11482 | VEC_safe_push (tree, gc, init_vec, TREE_VALUE (init)); | |
11483 | } | |
11484 | } | |
11485 | ||
11486 | ret = build_new (&placement_vec, | |
11487 | RECUR (TREE_OPERAND (t, 1)), | |
11488 | RECUR (TREE_OPERAND (t, 2)), | |
11489 | &init_vec, | |
11490 | NEW_EXPR_USE_GLOBAL (t), | |
11491 | complain); | |
11492 | ||
11493 | if (placement_vec != NULL) | |
11494 | release_tree_vector (placement_vec); | |
11495 | if (init_vec != NULL) | |
11496 | release_tree_vector (init_vec); | |
11497 | ||
11498 | return ret; | |
268127ce | 11499 | } |
cc23546e JO |
11500 | |
11501 | case DELETE_EXPR: | |
11502 | return delete_sanity | |
b3445994 MM |
11503 | (RECUR (TREE_OPERAND (t, 0)), |
11504 | RECUR (TREE_OPERAND (t, 1)), | |
cc23546e JO |
11505 | DELETE_EXPR_USE_VEC (t), |
11506 | DELETE_EXPR_USE_GLOBAL (t)); | |
11507 | ||
11508 | case COMPOUND_EXPR: | |
b3445994 | 11509 | return build_x_compound_expr (RECUR (TREE_OPERAND (t, 0)), |
5ade1ed2 DG |
11510 | RECUR (TREE_OPERAND (t, 1)), |
11511 | complain); | |
cc23546e | 11512 | |
ee76b931 | 11513 | case CALL_EXPR: |
cc23546e | 11514 | { |
ee76b931 | 11515 | tree function; |
c166b898 ILT |
11516 | VEC(tree,gc) *call_args; |
11517 | unsigned int nargs, i; | |
ee76b931 | 11518 | bool qualified_p; |
ee935db4 | 11519 | bool koenig_p; |
c166b898 | 11520 | tree ret; |
ee76b931 | 11521 | |
5039610b | 11522 | function = CALL_EXPR_FN (t); |
6d80c4b9 MM |
11523 | /* When we parsed the expression, we determined whether or |
11524 | not Koenig lookup should be performed. */ | |
11525 | koenig_p = KOENIG_LOOKUP_P (t); | |
ee76b931 | 11526 | if (TREE_CODE (function) == SCOPE_REF) |
cc23546e | 11527 | { |
ee76b931 MM |
11528 | qualified_p = true; |
11529 | function = tsubst_qualified_id (function, args, complain, in_decl, | |
c8094d83 | 11530 | /*done=*/false, |
ee76b931 | 11531 | /*address_p=*/false); |
cc23546e | 11532 | } |
ee76b931 | 11533 | else |
cc23546e | 11534 | { |
61e71a9e NS |
11535 | if (TREE_CODE (function) == COMPONENT_REF) |
11536 | { | |
11537 | tree op = TREE_OPERAND (function, 1); | |
11538 | ||
11539 | qualified_p = (TREE_CODE (op) == SCOPE_REF | |
11540 | || (BASELINK_P (op) | |
11541 | && BASELINK_QUALIFIED_P (op))); | |
11542 | } | |
11543 | else | |
11544 | qualified_p = false; | |
3db45ab5 | 11545 | |
c8094d83 | 11546 | function = tsubst_copy_and_build (function, args, complain, |
b3445994 | 11547 | in_decl, |
015c2c66 MM |
11548 | !qualified_p, |
11549 | integral_constant_expression_p); | |
11550 | ||
6d80c4b9 MM |
11551 | if (BASELINK_P (function)) |
11552 | qualified_p = true; | |
cc23546e | 11553 | } |
cc23546e | 11554 | |
c166b898 ILT |
11555 | nargs = call_expr_nargs (t); |
11556 | call_args = make_tree_vector (); | |
11557 | for (i = 0; i < nargs; ++i) | |
11558 | { | |
11559 | tree arg = CALL_EXPR_ARG (t, i); | |
11560 | ||
11561 | if (!PACK_EXPANSION_P (arg)) | |
11562 | VEC_safe_push (tree, gc, call_args, | |
11563 | RECUR (CALL_EXPR_ARG (t, i))); | |
11564 | else | |
11565 | { | |
11566 | /* Expand the pack expansion and push each entry onto | |
11567 | CALL_ARGS. */ | |
11568 | arg = tsubst_pack_expansion (arg, args, complain, in_decl); | |
11569 | if (TREE_CODE (arg) == TREE_VEC) | |
11570 | { | |
11571 | unsigned int len, j; | |
11572 | ||
11573 | len = TREE_VEC_LENGTH (arg); | |
11574 | for (j = 0; j < len; ++j) | |
11575 | { | |
11576 | tree value = TREE_VEC_ELT (arg, j); | |
11577 | if (value != NULL_TREE) | |
11578 | value = convert_from_reference (value); | |
11579 | VEC_safe_push (tree, gc, call_args, value); | |
11580 | } | |
11581 | } | |
11582 | else | |
11583 | { | |
11584 | /* A partial substitution. Add one entry. */ | |
11585 | VEC_safe_push (tree, gc, call_args, arg); | |
11586 | } | |
11587 | } | |
11588 | } | |
676e33ca MM |
11589 | |
11590 | /* We do not perform argument-dependent lookup if normal | |
11591 | lookup finds a non-function, in accordance with the | |
11592 | expected resolution of DR 218. */ | |
ee935db4 | 11593 | if (koenig_p |
44370687 MM |
11594 | && ((is_overloaded_fn (function) |
11595 | /* If lookup found a member function, the Koenig lookup is | |
11596 | not appropriate, even if an unqualified-name was used | |
11597 | to denote the function. */ | |
11598 | && !DECL_FUNCTION_MEMBER_P (get_first_fn (function))) | |
5094a795 JM |
11599 | || TREE_CODE (function) == IDENTIFIER_NODE) |
11600 | /* Only do this when substitution turns a dependent call | |
11601 | into a non-dependent call. */ | |
11602 | && type_dependent_expression_p_push (t) | |
11603 | && !any_type_dependent_arguments_p (call_args)) | |
6d80c4b9 MM |
11604 | function = perform_koenig_lookup (function, call_args); |
11605 | ||
11606 | if (TREE_CODE (function) == IDENTIFIER_NODE) | |
b3445994 | 11607 | { |
6d80c4b9 | 11608 | unqualified_name_lookup_error (function); |
c166b898 | 11609 | release_tree_vector (call_args); |
6d80c4b9 | 11610 | return error_mark_node; |
b3445994 MM |
11611 | } |
11612 | ||
11613 | /* Remember that there was a reference to this entity. */ | |
11614 | if (DECL_P (function)) | |
11615 | mark_used (function); | |
11616 | ||
d17811fd | 11617 | if (TREE_CODE (function) == OFFSET_REF) |
c166b898 ILT |
11618 | ret = build_offset_ref_call_from_tree (function, &call_args); |
11619 | else if (TREE_CODE (function) == COMPONENT_REF) | |
9f880ef9 MM |
11620 | { |
11621 | if (!BASELINK_P (TREE_OPERAND (function, 1))) | |
c166b898 | 11622 | ret = finish_call_expr (function, &call_args, |
9f880ef9 | 11623 | /*disallow_virtual=*/false, |
5ade1ed2 DG |
11624 | /*koenig_p=*/false, |
11625 | complain); | |
9f880ef9 | 11626 | else |
c166b898 | 11627 | ret = (build_new_method_call |
9f880ef9 MM |
11628 | (TREE_OPERAND (function, 0), |
11629 | TREE_OPERAND (function, 1), | |
c166b898 | 11630 | &call_args, NULL_TREE, |
63c9a190 | 11631 | qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL, |
5ade1ed2 DG |
11632 | /*fn_p=*/NULL, |
11633 | complain)); | |
9f880ef9 | 11634 | } |
c166b898 ILT |
11635 | else |
11636 | ret = finish_call_expr (function, &call_args, | |
11637 | /*disallow_virtual=*/qualified_p, | |
11638 | koenig_p, | |
11639 | complain); | |
11640 | ||
11641 | release_tree_vector (call_args); | |
11642 | ||
11643 | return ret; | |
cc23546e JO |
11644 | } |
11645 | ||
11646 | case COND_EXPR: | |
11647 | return build_x_conditional_expr | |
b3445994 MM |
11648 | (RECUR (TREE_OPERAND (t, 0)), |
11649 | RECUR (TREE_OPERAND (t, 1)), | |
5ade1ed2 DG |
11650 | RECUR (TREE_OPERAND (t, 2)), |
11651 | complain); | |
cc23546e JO |
11652 | |
11653 | case PSEUDO_DTOR_EXPR: | |
c8094d83 | 11654 | return finish_pseudo_destructor_expr |
b3445994 MM |
11655 | (RECUR (TREE_OPERAND (t, 0)), |
11656 | RECUR (TREE_OPERAND (t, 1)), | |
11657 | RECUR (TREE_OPERAND (t, 2))); | |
cc23546e JO |
11658 | |
11659 | case TREE_LIST: | |
11660 | { | |
11661 | tree purpose, value, chain; | |
11662 | ||
11663 | if (t == void_list_node) | |
11664 | return t; | |
11665 | ||
5d80a306 DG |
11666 | if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t))) |
11667 | || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t)))) | |
11668 | { | |
11669 | /* We have pack expansions, so expand those and | |
11670 | create a new list out of it. */ | |
11671 | tree purposevec = NULL_TREE; | |
11672 | tree valuevec = NULL_TREE; | |
11673 | tree chain; | |
11674 | int i, len = -1; | |
11675 | ||
11676 | /* Expand the argument expressions. */ | |
11677 | if (TREE_PURPOSE (t)) | |
11678 | purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args, | |
11679 | complain, in_decl); | |
11680 | if (TREE_VALUE (t)) | |
11681 | valuevec = tsubst_pack_expansion (TREE_VALUE (t), args, | |
11682 | complain, in_decl); | |
11683 | ||
11684 | /* Build the rest of the list. */ | |
11685 | chain = TREE_CHAIN (t); | |
11686 | if (chain && chain != void_type_node) | |
11687 | chain = RECUR (chain); | |
11688 | ||
11689 | /* Determine the number of arguments. */ | |
11690 | if (purposevec && TREE_CODE (purposevec) == TREE_VEC) | |
11691 | { | |
11692 | len = TREE_VEC_LENGTH (purposevec); | |
11693 | gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec)); | |
11694 | } | |
11695 | else if (TREE_CODE (valuevec) == TREE_VEC) | |
11696 | len = TREE_VEC_LENGTH (valuevec); | |
11697 | else | |
11698 | { | |
11699 | /* Since we only performed a partial substitution into | |
11700 | the argument pack, we only return a single list | |
11701 | node. */ | |
11702 | if (purposevec == TREE_PURPOSE (t) | |
11703 | && valuevec == TREE_VALUE (t) | |
11704 | && chain == TREE_CHAIN (t)) | |
11705 | return t; | |
11706 | ||
11707 | return tree_cons (purposevec, valuevec, chain); | |
11708 | } | |
11709 | ||
11710 | /* Convert the argument vectors into a TREE_LIST */ | |
11711 | i = len; | |
11712 | while (i > 0) | |
11713 | { | |
11714 | /* Grab the Ith values. */ | |
11715 | i--; | |
11716 | purpose = purposevec ? TREE_VEC_ELT (purposevec, i) | |
11717 | : NULL_TREE; | |
11718 | value | |
11719 | = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i)) | |
11720 | : NULL_TREE; | |
11721 | ||
11722 | /* Build the list (backwards). */ | |
11723 | chain = tree_cons (purpose, value, chain); | |
11724 | } | |
11725 | ||
11726 | return chain; | |
11727 | } | |
11728 | ||
cc23546e JO |
11729 | purpose = TREE_PURPOSE (t); |
11730 | if (purpose) | |
b3445994 | 11731 | purpose = RECUR (purpose); |
cc23546e JO |
11732 | value = TREE_VALUE (t); |
11733 | if (value) | |
b3445994 | 11734 | value = RECUR (value); |
cc23546e JO |
11735 | chain = TREE_CHAIN (t); |
11736 | if (chain && chain != void_type_node) | |
b3445994 | 11737 | chain = RECUR (chain); |
cc23546e JO |
11738 | if (purpose == TREE_PURPOSE (t) |
11739 | && value == TREE_VALUE (t) | |
11740 | && chain == TREE_CHAIN (t)) | |
11741 | return t; | |
11742 | return tree_cons (purpose, value, chain); | |
11743 | } | |
11744 | ||
11745 | case COMPONENT_REF: | |
11746 | { | |
ee76b931 | 11747 | tree object; |
3897c0aa | 11748 | tree object_type; |
ee76b931 MM |
11749 | tree member; |
11750 | ||
b3445994 MM |
11751 | object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0), |
11752 | args, complain, in_decl); | |
ee76b931 MM |
11753 | /* Remember that there was a reference to this entity. */ |
11754 | if (DECL_P (object)) | |
11755 | mark_used (object); | |
3897c0aa | 11756 | object_type = TREE_TYPE (object); |
ee76b931 MM |
11757 | |
11758 | member = TREE_OPERAND (t, 1); | |
11759 | if (BASELINK_P (member)) | |
c8094d83 | 11760 | member = tsubst_baselink (member, |
ee76b931 MM |
11761 | non_reference (TREE_TYPE (object)), |
11762 | args, complain, in_decl); | |
11763 | else | |
11764 | member = tsubst_copy (member, args, complain, in_decl); | |
bad1f462 KL |
11765 | if (member == error_mark_node) |
11766 | return error_mark_node; | |
3897c0aa MM |
11767 | |
11768 | if (object_type && !CLASS_TYPE_P (object_type)) | |
cc23546e | 11769 | { |
3d9b2eb6 JJ |
11770 | if (SCALAR_TYPE_P (object_type)) |
11771 | { | |
11772 | tree s = NULL_TREE; | |
11773 | tree dtor = member; | |
11774 | ||
11775 | if (TREE_CODE (dtor) == SCOPE_REF) | |
11776 | { | |
11777 | s = TREE_OPERAND (dtor, 0); | |
11778 | dtor = TREE_OPERAND (dtor, 1); | |
11779 | } | |
11780 | if (TREE_CODE (dtor) == BIT_NOT_EXPR) | |
11781 | { | |
11782 | dtor = TREE_OPERAND (dtor, 0); | |
11783 | if (TYPE_P (dtor)) | |
11784 | return finish_pseudo_destructor_expr (object, s, dtor); | |
11785 | } | |
11786 | } | |
cc23546e JO |
11787 | } |
11788 | else if (TREE_CODE (member) == SCOPE_REF | |
11789 | && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR) | |
11790 | { | |
11791 | tree tmpl; | |
11792 | tree args; | |
c8094d83 | 11793 | |
cc23546e JO |
11794 | /* Lookup the template functions now that we know what the |
11795 | scope is. */ | |
11796 | tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0); | |
11797 | args = TREE_OPERAND (TREE_OPERAND (member, 1), 1); | |
c8094d83 | 11798 | member = lookup_qualified_name (TREE_OPERAND (member, 0), tmpl, |
12483c9f NS |
11799 | /*is_type_p=*/false, |
11800 | /*complain=*/false); | |
cc23546e | 11801 | if (BASELINK_P (member)) |
44370687 | 11802 | { |
c8094d83 | 11803 | BASELINK_FUNCTIONS (member) |
44370687 MM |
11804 | = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member), |
11805 | args); | |
c8094d83 MS |
11806 | member = (adjust_result_of_qualified_name_lookup |
11807 | (member, BINFO_TYPE (BASELINK_BINFO (member)), | |
3897c0aa | 11808 | object_type)); |
44370687 | 11809 | } |
cc23546e JO |
11810 | else |
11811 | { | |
2b7a3abf DS |
11812 | qualified_name_lookup_error (object_type, tmpl, member, |
11813 | input_location); | |
cc23546e JO |
11814 | return error_mark_node; |
11815 | } | |
11816 | } | |
2436b51f MM |
11817 | else if (TREE_CODE (member) == SCOPE_REF |
11818 | && !CLASS_TYPE_P (TREE_OPERAND (member, 0)) | |
11819 | && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL) | |
11820 | { | |
11821 | if (complain & tf_error) | |
11822 | { | |
11823 | if (TYPE_P (TREE_OPERAND (member, 0))) | |
c8094d83 | 11824 | error ("%qT is not a class or namespace", |
2436b51f MM |
11825 | TREE_OPERAND (member, 0)); |
11826 | else | |
c8094d83 | 11827 | error ("%qD is not a class or namespace", |
2436b51f MM |
11828 | TREE_OPERAND (member, 0)); |
11829 | } | |
11830 | return error_mark_node; | |
11831 | } | |
a3f10e50 NS |
11832 | else if (TREE_CODE (member) == FIELD_DECL) |
11833 | return finish_non_static_data_member (member, object, NULL_TREE); | |
cc23546e | 11834 | |
02ed62dd | 11835 | return finish_class_member_access_expr (object, member, |
5ade1ed2 DG |
11836 | /*template_p=*/false, |
11837 | complain); | |
cc23546e JO |
11838 | } |
11839 | ||
11840 | case THROW_EXPR: | |
11841 | return build_throw | |
b3445994 | 11842 | (RECUR (TREE_OPERAND (t, 0))); |
cc23546e JO |
11843 | |
11844 | case CONSTRUCTOR: | |
11845 | { | |
4038c495 GB |
11846 | VEC(constructor_elt,gc) *n; |
11847 | constructor_elt *ce; | |
11848 | unsigned HOST_WIDE_INT idx; | |
cc23546e | 11849 | tree type = tsubst (TREE_TYPE (t), args, complain, in_decl); |
4038c495 | 11850 | bool process_index_p; |
5d80a306 DG |
11851 | int newlen; |
11852 | bool need_copy_p = false; | |
09357846 | 11853 | tree r; |
cc23546e | 11854 | |
a97728cf VR |
11855 | if (type == error_mark_node) |
11856 | return error_mark_node; | |
11857 | ||
cc23546e JO |
11858 | /* digest_init will do the wrong thing if we let it. */ |
11859 | if (type && TYPE_PTRMEMFUNC_P (type)) | |
11860 | return t; | |
11861 | ||
4038c495 | 11862 | /* We do not want to process the index of aggregate |
cc23546e JO |
11863 | initializers as they are identifier nodes which will be |
11864 | looked up by digest_init. */ | |
9e1e64ec | 11865 | process_index_p = !(type && MAYBE_CLASS_TYPE_P (type)); |
c8094d83 | 11866 | |
4038c495 | 11867 | n = VEC_copy (constructor_elt, gc, CONSTRUCTOR_ELTS (t)); |
5d80a306 | 11868 | newlen = VEC_length (constructor_elt, n); |
4038c495 GB |
11869 | for (idx = 0; VEC_iterate (constructor_elt, n, idx, ce); idx++) |
11870 | { | |
11871 | if (ce->index && process_index_p) | |
11872 | ce->index = RECUR (ce->index); | |
5d80a306 DG |
11873 | |
11874 | if (PACK_EXPANSION_P (ce->value)) | |
11875 | { | |
11876 | /* Substitute into the pack expansion. */ | |
11877 | ce->value = tsubst_pack_expansion (ce->value, args, complain, | |
11878 | in_decl); | |
11879 | ||
4745e4eb JJ |
11880 | if (ce->value == error_mark_node) |
11881 | ; | |
11882 | else if (TREE_VEC_LENGTH (ce->value) == 1) | |
5d80a306 DG |
11883 | /* Just move the argument into place. */ |
11884 | ce->value = TREE_VEC_ELT (ce->value, 0); | |
11885 | else | |
11886 | { | |
11887 | /* Update the length of the final CONSTRUCTOR | |
11888 | arguments vector, and note that we will need to | |
11889 | copy.*/ | |
11890 | newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1; | |
11891 | need_copy_p = true; | |
11892 | } | |
11893 | } | |
11894 | else | |
11895 | ce->value = RECUR (ce->value); | |
cc23546e | 11896 | } |
c8094d83 | 11897 | |
5d80a306 DG |
11898 | if (need_copy_p) |
11899 | { | |
11900 | VEC(constructor_elt,gc) *old_n = n; | |
11901 | ||
11902 | n = VEC_alloc (constructor_elt, gc, newlen); | |
11903 | for (idx = 0; VEC_iterate (constructor_elt, old_n, idx, ce); | |
11904 | idx++) | |
11905 | { | |
11906 | if (TREE_CODE (ce->value) == TREE_VEC) | |
11907 | { | |
11908 | int i, len = TREE_VEC_LENGTH (ce->value); | |
11909 | for (i = 0; i < len; ++i) | |
11910 | CONSTRUCTOR_APPEND_ELT (n, 0, | |
11911 | TREE_VEC_ELT (ce->value, i)); | |
11912 | } | |
11913 | else | |
11914 | CONSTRUCTOR_APPEND_ELT (n, 0, ce->value); | |
11915 | } | |
11916 | } | |
11917 | ||
09357846 | 11918 | r = build_constructor (init_list_type_node, n); |
27fb09b7 | 11919 | CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t); |
09357846 | 11920 | |
05b22df9 | 11921 | if (TREE_HAS_CONSTRUCTOR (t)) |
09357846 | 11922 | return finish_compound_literal (type, r); |
cc23546e | 11923 | |
09357846 | 11924 | return r; |
cc23546e JO |
11925 | } |
11926 | ||
11927 | case TYPEID_EXPR: | |
11928 | { | |
b3445994 | 11929 | tree operand_0 = RECUR (TREE_OPERAND (t, 0)); |
cc23546e JO |
11930 | if (TYPE_P (operand_0)) |
11931 | return get_typeid (operand_0); | |
11932 | return build_typeid (operand_0); | |
11933 | } | |
11934 | ||
cc23546e | 11935 | case VAR_DECL: |
db24eb1f NS |
11936 | if (!args) |
11937 | return t; | |
11938 | /* Fall through */ | |
c8094d83 | 11939 | |
db24eb1f NS |
11940 | case PARM_DECL: |
11941 | { | |
11942 | tree r = tsubst_copy (t, args, complain, in_decl); | |
c8094d83 | 11943 | |
db24eb1f NS |
11944 | if (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE) |
11945 | /* If the original type was a reference, we'll be wrapped in | |
11946 | the appropriate INDIRECT_REF. */ | |
11947 | r = convert_from_reference (r); | |
11948 | return r; | |
11949 | } | |
cc23546e JO |
11950 | |
11951 | case VA_ARG_EXPR: | |
b3445994 | 11952 | return build_x_va_arg (RECUR (TREE_OPERAND (t, 0)), |
c8094d83 | 11953 | tsubst_copy (TREE_TYPE (t), args, complain, |
b3445994 | 11954 | in_decl)); |
cc23546e | 11955 | |
4bceb077 | 11956 | case OFFSETOF_EXPR: |
c291f8b1 | 11957 | return finish_offsetof (RECUR (TREE_OPERAND (t, 0))); |
4bceb077 | 11958 | |
cb68ec50 PC |
11959 | case TRAIT_EXPR: |
11960 | { | |
11961 | tree type1 = tsubst_copy (TRAIT_EXPR_TYPE1 (t), args, | |
11962 | complain, in_decl); | |
11963 | ||
11964 | tree type2 = TRAIT_EXPR_TYPE2 (t); | |
11965 | if (type2) | |
11966 | type2 = tsubst_copy (type2, args, complain, in_decl); | |
11967 | ||
11968 | return finish_trait_expr (TRAIT_EXPR_KIND (t), type1, type2); | |
11969 | } | |
11970 | ||
0fe0caa6 RH |
11971 | case STMT_EXPR: |
11972 | { | |
11973 | tree old_stmt_expr = cur_stmt_expr; | |
11974 | tree stmt_expr = begin_stmt_expr (); | |
11975 | ||
11976 | cur_stmt_expr = stmt_expr; | |
015c2c66 MM |
11977 | tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl, |
11978 | integral_constant_expression_p); | |
0fe0caa6 RH |
11979 | stmt_expr = finish_stmt_expr (stmt_expr, false); |
11980 | cur_stmt_expr = old_stmt_expr; | |
11981 | ||
11982 | return stmt_expr; | |
11983 | } | |
11984 | ||
a5201a91 MM |
11985 | case CONST_DECL: |
11986 | t = tsubst_copy (t, args, complain, in_decl); | |
11987 | /* As in finish_id_expression, we resolve enumeration constants | |
11988 | to their underlying values. */ | |
11989 | if (TREE_CODE (t) == CONST_DECL) | |
6193b8b7 DJ |
11990 | { |
11991 | used_types_insert (TREE_TYPE (t)); | |
11992 | return DECL_INITIAL (t); | |
11993 | } | |
a5201a91 MM |
11994 | return t; |
11995 | ||
cc23546e | 11996 | default: |
e58a9aa1 ZL |
11997 | /* Handle Objective-C++ constructs, if appropriate. */ |
11998 | { | |
11999 | tree subst | |
12000 | = objcp_tsubst_copy_and_build (t, args, complain, | |
12001 | in_decl, /*function_p=*/false); | |
12002 | if (subst) | |
12003 | return subst; | |
12004 | } | |
cc23546e JO |
12005 | return tsubst_copy (t, args, complain, in_decl); |
12006 | } | |
b3445994 MM |
12007 | |
12008 | #undef RECUR | |
cc23546e JO |
12009 | } |
12010 | ||
3e4a3562 NS |
12011 | /* Verify that the instantiated ARGS are valid. For type arguments, |
12012 | make sure that the type's linkage is ok. For non-type arguments, | |
34cd5ae7 | 12013 | make sure they are constants if they are integral or enumerations. |
9bcb9aae | 12014 | Emit an error under control of COMPLAIN, and return TRUE on error. */ |
3e4a3562 NS |
12015 | |
12016 | static bool | |
ffaf1e05 | 12017 | check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain) |
3e4a3562 | 12018 | { |
ffaf1e05 JM |
12019 | if (ARGUMENT_PACK_P (t)) |
12020 | { | |
12021 | tree vec = ARGUMENT_PACK_ARGS (t); | |
12022 | int len = TREE_VEC_LENGTH (vec); | |
12023 | bool result = false; | |
12024 | int i; | |
3e4a3562 | 12025 | |
ffaf1e05 JM |
12026 | for (i = 0; i < len; ++i) |
12027 | if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain)) | |
12028 | result = true; | |
12029 | return result; | |
12030 | } | |
12031 | else if (TYPE_P (t)) | |
3e4a3562 | 12032 | { |
ffaf1e05 JM |
12033 | /* [basic.link]: A name with no linkage (notably, the name |
12034 | of a class or enumeration declared in a local scope) | |
12035 | shall not be used to declare an entity with linkage. | |
12036 | This implies that names with no linkage cannot be used as | |
12037 | template arguments. */ | |
12038 | tree nt = no_linkage_check (t, /*relaxed_p=*/false); | |
c8094d83 | 12039 | |
ffaf1e05 | 12040 | if (nt) |
3e4a3562 | 12041 | { |
ffaf1e05 JM |
12042 | /* DR 488 makes use of a type with no linkage cause |
12043 | type deduction to fail. */ | |
12044 | if (complain & tf_error) | |
3e4a3562 | 12045 | { |
ffaf1e05 JM |
12046 | if (TYPE_ANONYMOUS_P (nt)) |
12047 | error ("%qT is/uses anonymous type", t); | |
12048 | else | |
12049 | error ("template argument for %qD uses local type %qT", | |
12050 | tmpl, t); | |
3e4a3562 | 12051 | } |
ffaf1e05 | 12052 | return true; |
3e4a3562 | 12053 | } |
ffaf1e05 JM |
12054 | /* In order to avoid all sorts of complications, we do not |
12055 | allow variably-modified types as template arguments. */ | |
12056 | else if (variably_modified_type_p (t, NULL_TREE)) | |
3e4a3562 NS |
12057 | { |
12058 | if (complain & tf_error) | |
ffaf1e05 JM |
12059 | error ("%qT is a variably modified type", t); |
12060 | return true; | |
3e4a3562 NS |
12061 | } |
12062 | } | |
ffaf1e05 JM |
12063 | /* A non-type argument of integral or enumerated type must be a |
12064 | constant. */ | |
12065 | else if (TREE_TYPE (t) | |
12066 | && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t)) | |
12067 | && !TREE_CONSTANT (t)) | |
12068 | { | |
12069 | if (complain & tf_error) | |
12070 | error ("integral expression %qE is not constant", t); | |
12071 | return true; | |
12072 | } | |
12073 | return false; | |
12074 | } | |
12075 | ||
12076 | static bool | |
12077 | check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain) | |
12078 | { | |
12079 | int ix, len = DECL_NTPARMS (tmpl); | |
12080 | bool result = false; | |
12081 | ||
12082 | for (ix = 0; ix != len; ix++) | |
12083 | { | |
12084 | if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain)) | |
12085 | result = true; | |
12086 | } | |
2010cdcd | 12087 | if (result && (complain & tf_error)) |
0f51ccfc | 12088 | error (" trying to instantiate %qD", tmpl); |
3e4a3562 NS |
12089 | return result; |
12090 | } | |
12091 | ||
6ba89f8e | 12092 | /* Instantiate the indicated variable or function template TMPL with |
36a117a5 MM |
12093 | the template arguments in TARG_PTR. */ |
12094 | ||
5566b478 | 12095 | tree |
3a978d72 | 12096 | instantiate_template (tree tmpl, tree targ_ptr, tsubst_flags_t complain) |
8d08fdba | 12097 | { |
5566b478 | 12098 | tree fndecl; |
36a117a5 MM |
12099 | tree gen_tmpl; |
12100 | tree spec; | |
a95799ec | 12101 | HOST_WIDE_INT saved_processing_template_decl; |
5566b478 | 12102 | |
27fafc8d JM |
12103 | if (tmpl == error_mark_node) |
12104 | return error_mark_node; | |
12105 | ||
50bc768d | 12106 | gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL); |
386b8a85 | 12107 | |
db9b2174 MM |
12108 | /* If this function is a clone, handle it specially. */ |
12109 | if (DECL_CLONED_FUNCTION_P (tmpl)) | |
3ad97789 | 12110 | { |
a30f62e0 | 12111 | tree spec; |
3ad97789 | 12112 | tree clone; |
c8094d83 | 12113 | |
a30f62e0 MM |
12114 | spec = instantiate_template (DECL_CLONED_FUNCTION (tmpl), targ_ptr, |
12115 | complain); | |
12116 | if (spec == error_mark_node) | |
12117 | return error_mark_node; | |
12118 | ||
c6002625 | 12119 | /* Look for the clone. */ |
4684cd27 | 12120 | FOR_EACH_CLONE (clone, spec) |
3ad97789 NS |
12121 | if (DECL_NAME (clone) == DECL_NAME (tmpl)) |
12122 | return clone; | |
12123 | /* We should always have found the clone by now. */ | |
315fb5db | 12124 | gcc_unreachable (); |
3ad97789 NS |
12125 | return NULL_TREE; |
12126 | } | |
c8094d83 | 12127 | |
36a117a5 | 12128 | /* Check to see if we already have this specialization. */ |
c8094d83 | 12129 | spec = retrieve_specialization (tmpl, targ_ptr, |
c7222c02 | 12130 | /*class_specializations_p=*/false); |
36a117a5 MM |
12131 | if (spec != NULL_TREE) |
12132 | return spec; | |
12133 | ||
f9a7ae04 MM |
12134 | gen_tmpl = most_general_template (tmpl); |
12135 | if (tmpl != gen_tmpl) | |
386b8a85 | 12136 | { |
36a117a5 MM |
12137 | /* The TMPL is a partial instantiation. To get a full set of |
12138 | arguments we must add the arguments used to perform the | |
12139 | partial instantiation. */ | |
12140 | targ_ptr = add_outermost_template_args (DECL_TI_ARGS (tmpl), | |
12141 | targ_ptr); | |
36a117a5 MM |
12142 | |
12143 | /* Check to see if we already have this specialization. */ | |
c7222c02 MM |
12144 | spec = retrieve_specialization (gen_tmpl, targ_ptr, |
12145 | /*class_specializations_p=*/false); | |
75650646 MM |
12146 | if (spec != NULL_TREE) |
12147 | return spec; | |
386b8a85 JM |
12148 | } |
12149 | ||
3e4a3562 NS |
12150 | if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr), |
12151 | complain)) | |
12152 | return error_mark_node; | |
c8094d83 | 12153 | |
2b907f5c KL |
12154 | /* We are building a FUNCTION_DECL, during which the access of its |
12155 | parameters and return types have to be checked. However this | |
12156 | FUNCTION_DECL which is the desired context for access checking | |
12157 | is not built yet. We solve this chicken-and-egg problem by | |
12158 | deferring all checks until we have the FUNCTION_DECL. */ | |
12159 | push_deferring_access_checks (dk_deferred); | |
5c74d5b0 | 12160 | |
a95799ec MM |
12161 | /* Although PROCESSING_TEMPLATE_DECL may be true at this point |
12162 | (because, for example, we have encountered a non-dependent | |
a1d08991 MM |
12163 | function call in the body of a template function and must now |
12164 | determine which of several overloaded functions will be called), | |
12165 | within the instantiation itself we are not processing a | |
12166 | template. */ | |
a95799ec MM |
12167 | saved_processing_template_decl = processing_template_decl; |
12168 | processing_template_decl = 0; | |
12169 | /* Substitute template parameters to obtain the specialization. */ | |
17aec3eb | 12170 | fndecl = tsubst (DECL_TEMPLATE_RESULT (gen_tmpl), |
3e4a3562 | 12171 | targ_ptr, complain, gen_tmpl); |
a95799ec | 12172 | processing_template_decl = saved_processing_template_decl; |
6b6b60af MM |
12173 | if (fndecl == error_mark_node) |
12174 | return error_mark_node; | |
5c74d5b0 | 12175 | |
2b907f5c KL |
12176 | /* Now we know the specialization, compute access previously |
12177 | deferred. */ | |
12178 | push_access_scope (fndecl); | |
d0940d56 DS |
12179 | |
12180 | /* Some typedefs referenced from within the template code need to be access | |
12181 | checked at template instantiation time, i.e now. These types were | |
12182 | added to the template at parsing time. Let's get those and perfom | |
12183 | the acces checks then. */ | |
2eb25c98 | 12184 | perform_typedefs_access_check (DECL_TEMPLATE_RESULT (tmpl), targ_ptr); |
2b907f5c KL |
12185 | perform_deferred_access_checks (); |
12186 | pop_access_scope (fndecl); | |
12187 | pop_deferring_access_checks (); | |
5c74d5b0 | 12188 | |
36a117a5 MM |
12189 | /* The DECL_TI_TEMPLATE should always be the immediate parent |
12190 | template, not the most general template. */ | |
12191 | DECL_TI_TEMPLATE (fndecl) = tmpl; | |
8d08fdba | 12192 | |
94350948 | 12193 | /* If we've just instantiated the main entry point for a function, |
3ad97789 NS |
12194 | instantiate all the alternate entry points as well. We do this |
12195 | by cloning the instantiation of the main entry point, not by | |
12196 | instantiating the template clones. */ | |
12197 | if (TREE_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (TREE_CHAIN (gen_tmpl))) | |
12198 | clone_function_decl (fndecl, /*update_method_vec_p=*/0); | |
94350948 | 12199 | |
5566b478 | 12200 | return fndecl; |
8d08fdba | 12201 | } |
5566b478 | 12202 | |
c166b898 ILT |
12203 | /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with |
12204 | NARGS elements of the arguments that are being used when calling | |
12205 | it. TARGS is a vector into which the deduced template arguments | |
12206 | are placed. | |
8d08fdba MS |
12207 | |
12208 | Return zero for success, 2 for an incomplete match that doesn't resolve | |
12209 | all the types, and 1 for complete failure. An error message will be | |
12210 | printed only for an incomplete match. | |
12211 | ||
e5214479 JM |
12212 | If FN is a conversion operator, or we are trying to produce a specific |
12213 | specialization, RETURN_TYPE is the return type desired. | |
9f54c803 MM |
12214 | |
12215 | The EXPLICIT_TARGS are explicit template arguments provided via a | |
12216 | template-id. | |
6467930b | 12217 | |
830bfa74 MM |
12218 | The parameter STRICT is one of: |
12219 | ||
c8094d83 | 12220 | DEDUCE_CALL: |
830bfa74 MM |
12221 | We are deducing arguments for a function call, as in |
12222 | [temp.deduct.call]. | |
12223 | ||
12224 | DEDUCE_CONV: | |
c8094d83 | 12225 | We are deducing arguments for a conversion function, as in |
830bfa74 MM |
12226 | [temp.deduct.conv]. |
12227 | ||
12228 | DEDUCE_EXACT: | |
62e4a758 NS |
12229 | We are deducing arguments when doing an explicit instantiation |
12230 | as in [temp.explicit], when determining an explicit specialization | |
12231 | as in [temp.expl.spec], or when taking the address of a function | |
a34d3336 | 12232 | template, as in [temp.deduct.funcaddr]. */ |
8d08fdba MS |
12233 | |
12234 | int | |
c8094d83 | 12235 | fn_type_unification (tree fn, |
0cbd7506 MS |
12236 | tree explicit_targs, |
12237 | tree targs, | |
c166b898 ILT |
12238 | const tree *args, |
12239 | unsigned int nargs, | |
0cbd7506 | 12240 | tree return_type, |
30f86ec3 FJ |
12241 | unification_kind_t strict, |
12242 | int flags) | |
386b8a85 | 12243 | { |
4393e105 MM |
12244 | tree parms; |
12245 | tree fntype; | |
adecb3f4 | 12246 | int result; |
5d80a306 | 12247 | bool incomplete_argument_packs_p = false; |
386b8a85 | 12248 | |
50bc768d | 12249 | gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL); |
2b59fc25 | 12250 | |
4393e105 MM |
12251 | fntype = TREE_TYPE (fn); |
12252 | if (explicit_targs) | |
75650646 | 12253 | { |
4393e105 | 12254 | /* [temp.deduct] |
c8094d83 | 12255 | |
4393e105 MM |
12256 | The specified template arguments must match the template |
12257 | parameters in kind (i.e., type, nontype, template), and there | |
12258 | must not be more arguments than there are parameters; | |
12259 | otherwise type deduction fails. | |
12260 | ||
12261 | Nontype arguments must match the types of the corresponding | |
12262 | nontype template parameters, or must be convertible to the | |
12263 | types of the corresponding nontype parameters as specified in | |
12264 | _temp.arg.nontype_, otherwise type deduction fails. | |
12265 | ||
12266 | All references in the function type of the function template | |
12267 | to the corresponding template parameters are replaced by the | |
12268 | specified template argument values. If a substitution in a | |
12269 | template parameter or in the function type of the function | |
12270 | template results in an invalid type, type deduction fails. */ | |
5d80a306 DG |
12271 | tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn); |
12272 | int i, len = TREE_VEC_LENGTH (tparms); | |
4393e105 | 12273 | tree converted_args; |
5d80a306 | 12274 | bool incomplete = false; |
75650646 | 12275 | |
c8a7ed43 AO |
12276 | if (explicit_targs == error_mark_node) |
12277 | return 1; | |
12278 | ||
4393e105 | 12279 | converted_args |
5d80a306 | 12280 | = (coerce_template_parms (tparms, explicit_targs, NULL_TREE, tf_none, |
e7e93965 MM |
12281 | /*require_all_args=*/false, |
12282 | /*use_default_args=*/false)); | |
4393e105 | 12283 | if (converted_args == error_mark_node) |
75650646 | 12284 | return 1; |
386b8a85 | 12285 | |
ffd49b19 | 12286 | /* Substitute the explicit args into the function type. This is |
0cbd7506 MS |
12287 | necessary so that, for instance, explicitly declared function |
12288 | arguments can match null pointed constants. If we were given | |
12289 | an incomplete set of explicit args, we must not do semantic | |
12290 | processing during substitution as we could create partial | |
12291 | instantiations. */ | |
5d80a306 DG |
12292 | for (i = 0; i < len; i++) |
12293 | { | |
12294 | tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i)); | |
12295 | bool parameter_pack = false; | |
12296 | ||
12297 | /* Dig out the actual parm. */ | |
12298 | if (TREE_CODE (parm) == TYPE_DECL | |
12299 | || TREE_CODE (parm) == TEMPLATE_DECL) | |
12300 | { | |
12301 | parm = TREE_TYPE (parm); | |
12302 | parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm); | |
12303 | } | |
12304 | else if (TREE_CODE (parm) == PARM_DECL) | |
12305 | { | |
12306 | parm = DECL_INITIAL (parm); | |
12307 | parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm); | |
12308 | } | |
12309 | ||
12310 | if (parameter_pack) | |
12311 | { | |
12312 | int level, idx; | |
12313 | tree targ; | |
12314 | template_parm_level_and_index (parm, &level, &idx); | |
12315 | ||
12316 | /* Mark the argument pack as "incomplete". We could | |
12317 | still deduce more arguments during unification. */ | |
12318 | targ = TMPL_ARG (converted_args, level, idx); | |
b1d7b1c0 DG |
12319 | if (targ) |
12320 | { | |
12321 | ARGUMENT_PACK_INCOMPLETE_P(targ) = 1; | |
12322 | ARGUMENT_PACK_EXPLICIT_ARGS (targ) | |
12323 | = ARGUMENT_PACK_ARGS (targ); | |
12324 | } | |
5d80a306 DG |
12325 | |
12326 | /* We have some incomplete argument packs. */ | |
12327 | incomplete_argument_packs_p = true; | |
12328 | } | |
12329 | } | |
12330 | ||
12331 | if (incomplete_argument_packs_p) | |
12332 | /* Any substitution is guaranteed to be incomplete if there | |
12333 | are incomplete argument packs, because we can still deduce | |
12334 | more arguments. */ | |
12335 | incomplete = 1; | |
12336 | else | |
12337 | incomplete = NUM_TMPL_ARGS (explicit_targs) != NUM_TMPL_ARGS (targs); | |
12338 | ||
ffd49b19 | 12339 | processing_template_decl += incomplete; |
c2ea3a40 | 12340 | fntype = tsubst (fntype, converted_args, tf_none, NULL_TREE); |
ffd49b19 | 12341 | processing_template_decl -= incomplete; |
c8094d83 | 12342 | |
4393e105 MM |
12343 | if (fntype == error_mark_node) |
12344 | return 1; | |
050367a3 | 12345 | |
4393e105 | 12346 | /* Place the explicitly specified arguments in TARGS. */ |
ffd49b19 | 12347 | for (i = NUM_TMPL_ARGS (converted_args); i--;) |
4393e105 | 12348 | TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (converted_args, i); |
75650646 | 12349 | } |
c8094d83 | 12350 | |
e5214479 | 12351 | /* Never do unification on the 'this' parameter. */ |
ba139ba8 | 12352 | parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype)); |
c8094d83 | 12353 | |
8d3631f8 NS |
12354 | if (return_type) |
12355 | { | |
c166b898 ILT |
12356 | tree *new_args; |
12357 | ||
8d3631f8 | 12358 | parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms); |
c166b898 ILT |
12359 | new_args = XALLOCAVEC (tree, nargs + 1); |
12360 | new_args[0] = return_type; | |
12361 | memcpy (new_args + 1, args, nargs * sizeof (tree)); | |
12362 | args = new_args; | |
12363 | ++nargs; | |
4393e105 MM |
12364 | } |
12365 | ||
4393e105 MM |
12366 | /* We allow incomplete unification without an error message here |
12367 | because the standard doesn't seem to explicitly prohibit it. Our | |
12368 | callers must be ready to deal with unification failures in any | |
12369 | event. */ | |
c8094d83 | 12370 | result = type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn), |
c166b898 | 12371 | targs, parms, args, nargs, /*subr=*/0, |
30f86ec3 | 12372 | strict, flags); |
adecb3f4 | 12373 | |
5d80a306 DG |
12374 | if (result == 0 && incomplete_argument_packs_p) |
12375 | { | |
12376 | int i, len = NUM_TMPL_ARGS (targs); | |
12377 | ||
12378 | /* Clear the "incomplete" flags on all argument packs. */ | |
12379 | for (i = 0; i < len; i++) | |
12380 | { | |
12381 | tree arg = TREE_VEC_ELT (targs, i); | |
12382 | if (ARGUMENT_PACK_P (arg)) | |
12383 | { | |
12384 | ARGUMENT_PACK_INCOMPLETE_P (arg) = 0; | |
12385 | ARGUMENT_PACK_EXPLICIT_ARGS (arg) = NULL_TREE; | |
12386 | } | |
12387 | } | |
12388 | } | |
12389 | ||
4cf36211 DG |
12390 | /* Now that we have bindings for all of the template arguments, |
12391 | ensure that the arguments deduced for the template template | |
12392 | parameters have compatible template parameter lists. We cannot | |
12393 | check this property before we have deduced all template | |
12394 | arguments, because the template parameter types of a template | |
12395 | template parameter might depend on prior template parameters | |
12396 | deduced after the template template parameter. The following | |
12397 | ill-formed example illustrates this issue: | |
12398 | ||
12399 | template<typename T, template<T> class C> void f(C<5>, T); | |
12400 | ||
12401 | template<int N> struct X {}; | |
12402 | ||
12403 | void g() { | |
12404 | f(X<5>(), 5l); // error: template argument deduction fails | |
12405 | } | |
12406 | ||
12407 | The template parameter list of 'C' depends on the template type | |
12408 | parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to | |
12409 | 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the | |
12410 | time that we deduce 'C'. */ | |
12411 | if (result == 0 | |
12412 | && !template_template_parm_bindings_ok_p | |
12413 | (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs)) | |
12414 | return 1; | |
12415 | ||
c8094d83 | 12416 | if (result == 0) |
adecb3f4 | 12417 | /* All is well so far. Now, check: |
c8094d83 MS |
12418 | |
12419 | [temp.deduct] | |
12420 | ||
adecb3f4 MM |
12421 | When all template arguments have been deduced, all uses of |
12422 | template parameters in nondeduced contexts are replaced with | |
12423 | the corresponding deduced argument values. If the | |
12424 | substitution results in an invalid type, as described above, | |
12425 | type deduction fails. */ | |
463ecaca JM |
12426 | { |
12427 | tree substed = tsubst (TREE_TYPE (fn), targs, tf_none, NULL_TREE); | |
12428 | if (substed == error_mark_node) | |
12429 | return 1; | |
12430 | ||
12431 | /* If we're looking for an exact match, check that what we got | |
12432 | is indeed an exact match. It might not be if some template | |
12433 | parameters are used in non-deduced contexts. */ | |
12434 | if (strict == DEDUCE_EXACT) | |
12435 | { | |
c166b898 ILT |
12436 | unsigned int i; |
12437 | ||
463ecaca JM |
12438 | tree sarg |
12439 | = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (substed)); | |
463ecaca JM |
12440 | if (return_type) |
12441 | sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg); | |
c166b898 ILT |
12442 | for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg)) |
12443 | if (!same_type_p (args[i], TREE_VALUE (sarg))) | |
463ecaca JM |
12444 | return 1; |
12445 | } | |
12446 | } | |
adecb3f4 MM |
12447 | |
12448 | return result; | |
830bfa74 MM |
12449 | } |
12450 | ||
12451 | /* Adjust types before performing type deduction, as described in | |
12452 | [temp.deduct.call] and [temp.deduct.conv]. The rules in these two | |
4c7d0dff MM |
12453 | sections are symmetric. PARM is the type of a function parameter |
12454 | or the return type of the conversion function. ARG is the type of | |
12455 | the argument passed to the call, or the type of the value | |
8af2fec4 RY |
12456 | initialized with the result of the conversion function. |
12457 | ARG_EXPR is the original argument expression, which may be null. */ | |
386b8a85 | 12458 | |
62e4a758 | 12459 | static int |
c8094d83 | 12460 | maybe_adjust_types_for_deduction (unification_kind_t strict, |
0cbd7506 | 12461 | tree* parm, |
8af2fec4 RY |
12462 | tree* arg, |
12463 | tree arg_expr) | |
830bfa74 | 12464 | { |
62e4a758 | 12465 | int result = 0; |
c8094d83 | 12466 | |
830bfa74 MM |
12467 | switch (strict) |
12468 | { | |
12469 | case DEDUCE_CALL: | |
12470 | break; | |
12471 | ||
12472 | case DEDUCE_CONV: | |
12473 | { | |
4c7d0dff MM |
12474 | /* Swap PARM and ARG throughout the remainder of this |
12475 | function; the handling is precisely symmetric since PARM | |
12476 | will initialize ARG rather than vice versa. */ | |
830bfa74 MM |
12477 | tree* temp = parm; |
12478 | parm = arg; | |
12479 | arg = temp; | |
12480 | break; | |
12481 | } | |
12482 | ||
12483 | case DEDUCE_EXACT: | |
12484 | /* There is nothing to do in this case. */ | |
62e4a758 | 12485 | return 0; |
830bfa74 MM |
12486 | |
12487 | default: | |
315fb5db | 12488 | gcc_unreachable (); |
830bfa74 MM |
12489 | } |
12490 | ||
12491 | if (TREE_CODE (*parm) != REFERENCE_TYPE) | |
12492 | { | |
12493 | /* [temp.deduct.call] | |
c8094d83 | 12494 | |
830bfa74 | 12495 | If P is not a reference type: |
c8094d83 | 12496 | |
830bfa74 MM |
12497 | --If A is an array type, the pointer type produced by the |
12498 | array-to-pointer standard conversion (_conv.array_) is | |
12499 | used in place of A for type deduction; otherwise, | |
c8094d83 | 12500 | |
830bfa74 MM |
12501 | --If A is a function type, the pointer type produced by |
12502 | the function-to-pointer standard conversion | |
12503 | (_conv.func_) is used in place of A for type deduction; | |
12504 | otherwise, | |
c8094d83 | 12505 | |
830bfa74 MM |
12506 | --If A is a cv-qualified type, the top level |
12507 | cv-qualifiers of A's type are ignored for type | |
12508 | deduction. */ | |
12509 | if (TREE_CODE (*arg) == ARRAY_TYPE) | |
12510 | *arg = build_pointer_type (TREE_TYPE (*arg)); | |
d8f8dca1 | 12511 | else if (TREE_CODE (*arg) == FUNCTION_TYPE) |
830bfa74 MM |
12512 | *arg = build_pointer_type (*arg); |
12513 | else | |
12514 | *arg = TYPE_MAIN_VARIANT (*arg); | |
12515 | } | |
c8094d83 | 12516 | |
8af2fec4 RY |
12517 | /* From C++0x [14.8.2.1/3 temp.deduct.call] (after DR606), "If P is |
12518 | of the form T&&, where T is a template parameter, and the argument | |
12519 | is an lvalue, T is deduced as A& */ | |
12520 | if (TREE_CODE (*parm) == REFERENCE_TYPE | |
12521 | && TYPE_REF_IS_RVALUE (*parm) | |
12522 | && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM | |
12523 | && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED | |
12524 | && arg_expr && real_lvalue_p (arg_expr)) | |
12525 | *arg = build_reference_type (*arg); | |
12526 | ||
830bfa74 | 12527 | /* [temp.deduct.call] |
c8094d83 | 12528 | |
830bfa74 MM |
12529 | If P is a cv-qualified type, the top level cv-qualifiers |
12530 | of P's type are ignored for type deduction. If P is a | |
12531 | reference type, the type referred to by P is used for | |
12532 | type deduction. */ | |
12533 | *parm = TYPE_MAIN_VARIANT (*parm); | |
12534 | if (TREE_CODE (*parm) == REFERENCE_TYPE) | |
62e4a758 NS |
12535 | { |
12536 | *parm = TREE_TYPE (*parm); | |
12537 | result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL; | |
12538 | } | |
1c82cc90 NS |
12539 | |
12540 | /* DR 322. For conversion deduction, remove a reference type on parm | |
12541 | too (which has been swapped into ARG). */ | |
12542 | if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE) | |
12543 | *arg = TREE_TYPE (*arg); | |
c8094d83 | 12544 | |
62e4a758 | 12545 | return result; |
386b8a85 JM |
12546 | } |
12547 | ||
e5214479 | 12548 | /* Most parms like fn_type_unification. |
9f54c803 MM |
12549 | |
12550 | If SUBR is 1, we're being called recursively (to unify the | |
12551 | arguments of a function or method parameter of a function | |
38d18b1a | 12552 | template). */ |
386b8a85 | 12553 | |
4966381a | 12554 | static int |
c8094d83 | 12555 | type_unification_real (tree tparms, |
0cbd7506 MS |
12556 | tree targs, |
12557 | tree xparms, | |
c166b898 ILT |
12558 | const tree *xargs, |
12559 | unsigned int xnargs, | |
0cbd7506 | 12560 | int subr, |
30f86ec3 FJ |
12561 | unification_kind_t strict, |
12562 | int flags) | |
8d08fdba | 12563 | { |
8af2fec4 | 12564 | tree parm, arg, arg_expr; |
8d08fdba MS |
12565 | int i; |
12566 | int ntparms = TREE_VEC_LENGTH (tparms); | |
830bfa74 | 12567 | int sub_strict; |
bd0d5d4a | 12568 | int saw_undeduced = 0; |
c166b898 ILT |
12569 | tree parms; |
12570 | const tree *args; | |
12571 | unsigned int nargs; | |
12572 | unsigned int ia; | |
8d08fdba | 12573 | |
50bc768d NS |
12574 | gcc_assert (TREE_CODE (tparms) == TREE_VEC); |
12575 | gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST); | |
50bc768d | 12576 | gcc_assert (ntparms > 0); |
8d08fdba | 12577 | |
830bfa74 MM |
12578 | switch (strict) |
12579 | { | |
12580 | case DEDUCE_CALL: | |
028d1f20 | 12581 | sub_strict = (UNIFY_ALLOW_OUTER_LEVEL | UNIFY_ALLOW_MORE_CV_QUAL |
0cbd7506 | 12582 | | UNIFY_ALLOW_DERIVED); |
830bfa74 | 12583 | break; |
c8094d83 | 12584 | |
830bfa74 MM |
12585 | case DEDUCE_CONV: |
12586 | sub_strict = UNIFY_ALLOW_LESS_CV_QUAL; | |
12587 | break; | |
12588 | ||
12589 | case DEDUCE_EXACT: | |
12590 | sub_strict = UNIFY_ALLOW_NONE; | |
12591 | break; | |
c8094d83 | 12592 | |
830bfa74 | 12593 | default: |
315fb5db | 12594 | gcc_unreachable (); |
830bfa74 MM |
12595 | } |
12596 | ||
bd0d5d4a JM |
12597 | again: |
12598 | parms = xparms; | |
12599 | args = xargs; | |
c166b898 | 12600 | nargs = xnargs; |
bd0d5d4a | 12601 | |
c166b898 | 12602 | ia = 0; |
a34d3336 | 12603 | while (parms && parms != void_list_node |
c166b898 | 12604 | && ia < nargs) |
8d08fdba | 12605 | { |
5d80a306 DG |
12606 | if (TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION) |
12607 | break; | |
12608 | ||
8d08fdba MS |
12609 | parm = TREE_VALUE (parms); |
12610 | parms = TREE_CHAIN (parms); | |
c166b898 ILT |
12611 | arg = args[ia]; |
12612 | ++ia; | |
8af2fec4 | 12613 | arg_expr = NULL; |
8d08fdba MS |
12614 | |
12615 | if (arg == error_mark_node) | |
12616 | return 1; | |
12617 | if (arg == unknown_type_node) | |
34016c81 JM |
12618 | /* We can't deduce anything from this, but we might get all the |
12619 | template args from other function args. */ | |
12620 | continue; | |
b7484fbe | 12621 | |
03e70705 JM |
12622 | /* Conversions will be performed on a function argument that |
12623 | corresponds with a function parameter that contains only | |
12624 | non-deducible template parameters and explicitly specified | |
12625 | template parameters. */ | |
c353b8e3 | 12626 | if (!uses_template_parms (parm)) |
b7484fbe | 12627 | { |
03e70705 JM |
12628 | tree type; |
12629 | ||
2f939d94 | 12630 | if (!TYPE_P (arg)) |
03e70705 JM |
12631 | type = TREE_TYPE (arg); |
12632 | else | |
c353b8e3 | 12633 | type = arg; |
03e70705 | 12634 | |
fad86f7a | 12635 | if (same_type_p (parm, type)) |
343c89cd | 12636 | continue; |
fad86f7a | 12637 | if (strict != DEDUCE_EXACT |
3db45ab5 | 12638 | && can_convert_arg (parm, type, TYPE_P (arg) ? NULL_TREE : arg, |
30f86ec3 | 12639 | flags)) |
fad86f7a | 12640 | continue; |
3db45ab5 | 12641 | |
b7484fbe MS |
12642 | return 1; |
12643 | } | |
c8094d83 | 12644 | |
2f939d94 | 12645 | if (!TYPE_P (arg)) |
8d08fdba | 12646 | { |
50bc768d | 12647 | gcc_assert (TREE_TYPE (arg) != NULL_TREE); |
34016c81 | 12648 | if (type_unknown_p (arg)) |
28cbf42c | 12649 | { |
acde59b4 | 12650 | /* [temp.deduct.type] |
34016c81 | 12651 | |
acde59b4 MM |
12652 | A template-argument can be deduced from a pointer to |
12653 | function or pointer to member function argument if | |
12654 | the set of overloaded functions does not contain | |
12655 | function templates and at most one of a set of | |
12656 | overloaded functions provides a unique match. */ | |
34016c81 | 12657 | if (resolve_overloaded_unification |
acde59b4 MM |
12658 | (tparms, targs, parm, arg, strict, sub_strict)) |
12659 | continue; | |
12660 | ||
12661 | return 1; | |
28cbf42c | 12662 | } |
8af2fec4 | 12663 | arg_expr = arg; |
f9aa54d3 | 12664 | arg = unlowered_expr_type (arg); |
08476342 NS |
12665 | if (arg == error_mark_node) |
12666 | return 1; | |
8d08fdba | 12667 | } |
c8094d83 | 12668 | |
62e4a758 | 12669 | { |
0cbd7506 | 12670 | int arg_strict = sub_strict; |
c8094d83 | 12671 | |
0cbd7506 | 12672 | if (!subr) |
8af2fec4 RY |
12673 | arg_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, |
12674 | arg_expr); | |
4393e105 | 12675 | |
09357846 JM |
12676 | if (arg == init_list_type_node && arg_expr) |
12677 | arg = arg_expr; | |
0cbd7506 MS |
12678 | if (unify (tparms, targs, parm, arg, arg_strict)) |
12679 | return 1; | |
62e4a758 | 12680 | } |
8d08fdba | 12681 | } |
c8094d83 | 12682 | |
5d80a306 DG |
12683 | |
12684 | if (parms | |
12685 | && parms != void_list_node | |
12686 | && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION) | |
12687 | { | |
12688 | /* Unify the remaining arguments with the pack expansion type. */ | |
12689 | tree argvec; | |
12690 | tree parmvec = make_tree_vec (1); | |
5d80a306 | 12691 | |
5d80a306 | 12692 | /* Allocate a TREE_VEC and copy in all of the arguments */ |
c166b898 ILT |
12693 | argvec = make_tree_vec (nargs - ia); |
12694 | for (i = 0; ia < nargs; ++ia, ++i) | |
12695 | TREE_VEC_ELT (argvec, i) = args[ia]; | |
5d80a306 DG |
12696 | |
12697 | /* Copy the parameter into parmvec. */ | |
12698 | TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms); | |
12699 | if (unify_pack_expansion (tparms, targs, parmvec, argvec, strict, | |
12700 | /*call_args_p=*/true, /*subr=*/subr)) | |
12701 | return 1; | |
12702 | ||
12703 | /* Advance to the end of the list of parameters. */ | |
12704 | parms = TREE_CHAIN (parms); | |
12705 | } | |
12706 | ||
8d08fdba MS |
12707 | /* Fail if we've reached the end of the parm list, and more args |
12708 | are present, and the parm list isn't variadic. */ | |
c166b898 | 12709 | if (ia < nargs && parms == void_list_node) |
8d08fdba | 12710 | return 1; |
f4f206f4 | 12711 | /* Fail if parms are left and they don't have default values. */ |
a34d3336 | 12712 | if (parms && parms != void_list_node |
8d08fdba MS |
12713 | && TREE_PURPOSE (parms) == NULL_TREE) |
12714 | return 1; | |
bd0d5d4a | 12715 | |
8d08fdba MS |
12716 | if (!subr) |
12717 | for (i = 0; i < ntparms; i++) | |
a34d3336 | 12718 | if (!TREE_VEC_ELT (targs, i)) |
8d08fdba | 12719 | { |
2d8ba2c7 LM |
12720 | tree tparm; |
12721 | ||
12722 | if (TREE_VEC_ELT (tparms, i) == error_mark_node) | |
12723 | continue; | |
12724 | ||
12725 | tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i)); | |
bd0d5d4a JM |
12726 | |
12727 | /* If this is an undeduced nontype parameter that depends on | |
12728 | a type parameter, try another pass; its type may have been | |
12729 | deduced from a later argument than the one from which | |
12730 | this parameter can be deduced. */ | |
12731 | if (TREE_CODE (tparm) == PARM_DECL | |
12732 | && uses_template_parms (TREE_TYPE (tparm)) | |
12733 | && !saw_undeduced++) | |
12734 | goto again; | |
12735 | ||
9b7dd5e8 DG |
12736 | /* Core issue #226 (C++0x) [temp.deduct]: |
12737 | ||
12738 | If a template argument has not been deduced, its | |
12739 | default template argument, if any, is used. | |
12740 | ||
c1ae8be5 SM |
12741 | When we are in C++98 mode, TREE_PURPOSE will either |
12742 | be NULL_TREE or ERROR_MARK_NODE, so we do not need | |
12743 | to explicitly check cxx_dialect here. */ | |
9b7dd5e8 DG |
12744 | if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i))) |
12745 | { | |
12746 | tree arg = tsubst (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)), | |
12747 | targs, tf_none, NULL_TREE); | |
12748 | if (arg == error_mark_node) | |
12749 | return 1; | |
12750 | else | |
12751 | { | |
12752 | TREE_VEC_ELT (targs, i) = arg; | |
12753 | continue; | |
12754 | } | |
12755 | } | |
12756 | ||
b1d7b1c0 DG |
12757 | /* If the type parameter is a parameter pack, then it will |
12758 | be deduced to an empty parameter pack. */ | |
12759 | if (template_parameter_pack_p (tparm)) | |
12760 | { | |
12761 | tree arg; | |
12762 | ||
12763 | if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX) | |
12764 | { | |
12765 | arg = make_node (NONTYPE_ARGUMENT_PACK); | |
12766 | TREE_TYPE (arg) = TREE_TYPE (TEMPLATE_PARM_DECL (tparm)); | |
12767 | TREE_CONSTANT (arg) = 1; | |
12768 | } | |
12769 | else | |
12770 | arg = make_node (TYPE_ARGUMENT_PACK); | |
12771 | ||
12772 | SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0)); | |
12773 | ||
12774 | TREE_VEC_ELT (targs, i) = arg; | |
12775 | continue; | |
12776 | } | |
12777 | ||
8d08fdba MS |
12778 | return 2; |
12779 | } | |
c8094d83 | 12780 | |
8d08fdba MS |
12781 | return 0; |
12782 | } | |
12783 | ||
acde59b4 MM |
12784 | /* Subroutine of type_unification_real. Args are like the variables |
12785 | at the call site. ARG is an overloaded function (or template-id); | |
12786 | we try deducing template args from each of the overloads, and if | |
12787 | only one succeeds, we go with that. Modifies TARGS and returns | |
12788 | true on success. */ | |
34016c81 | 12789 | |
acde59b4 | 12790 | static bool |
c8094d83 | 12791 | resolve_overloaded_unification (tree tparms, |
0cbd7506 MS |
12792 | tree targs, |
12793 | tree parm, | |
12794 | tree arg, | |
12795 | unification_kind_t strict, | |
3a978d72 | 12796 | int sub_strict) |
34016c81 JM |
12797 | { |
12798 | tree tempargs = copy_node (targs); | |
12799 | int good = 0; | |
e04c614e | 12800 | tree goodfn = NULL_TREE; |
f23fb7f5 | 12801 | bool addr_p; |
34016c81 JM |
12802 | |
12803 | if (TREE_CODE (arg) == ADDR_EXPR) | |
f23fb7f5 MM |
12804 | { |
12805 | arg = TREE_OPERAND (arg, 0); | |
12806 | addr_p = true; | |
12807 | } | |
12808 | else | |
12809 | addr_p = false; | |
9f3d9e46 | 12810 | |
d8f8dca1 MM |
12811 | if (TREE_CODE (arg) == COMPONENT_REF) |
12812 | /* Handle `&x' where `x' is some static or non-static member | |
12813 | function name. */ | |
12814 | arg = TREE_OPERAND (arg, 1); | |
12815 | ||
05e0b2f4 JM |
12816 | if (TREE_CODE (arg) == OFFSET_REF) |
12817 | arg = TREE_OPERAND (arg, 1); | |
12818 | ||
9f3d9e46 | 12819 | /* Strip baselink information. */ |
50ad9642 MM |
12820 | if (BASELINK_P (arg)) |
12821 | arg = BASELINK_FUNCTIONS (arg); | |
9f3d9e46 | 12822 | |
34016c81 JM |
12823 | if (TREE_CODE (arg) == TEMPLATE_ID_EXPR) |
12824 | { | |
12825 | /* If we got some explicit template args, we need to plug them into | |
12826 | the affected templates before we try to unify, in case the | |
12827 | explicit args will completely resolve the templates in question. */ | |
12828 | ||
12829 | tree expl_subargs = TREE_OPERAND (arg, 1); | |
12830 | arg = TREE_OPERAND (arg, 0); | |
12831 | ||
12832 | for (; arg; arg = OVL_NEXT (arg)) | |
12833 | { | |
12834 | tree fn = OVL_CURRENT (arg); | |
12835 | tree subargs, elem; | |
12836 | ||
12837 | if (TREE_CODE (fn) != TEMPLATE_DECL) | |
12838 | continue; | |
12839 | ||
cc85b4c5 | 12840 | ++processing_template_decl; |
a34d3336 NS |
12841 | subargs = get_bindings (fn, DECL_TEMPLATE_RESULT (fn), |
12842 | expl_subargs, /*check_ret=*/false); | |
34016c81 JM |
12843 | if (subargs) |
12844 | { | |
c2ea3a40 | 12845 | elem = tsubst (TREE_TYPE (fn), subargs, tf_none, NULL_TREE); |
e04c614e JM |
12846 | if (try_one_overload (tparms, targs, tempargs, parm, |
12847 | elem, strict, sub_strict, addr_p) | |
12848 | && (!goodfn || !decls_match (goodfn, elem))) | |
12849 | { | |
12850 | goodfn = elem; | |
12851 | ++good; | |
12852 | } | |
34016c81 | 12853 | } |
cc85b4c5 | 12854 | --processing_template_decl; |
34016c81 JM |
12855 | } |
12856 | } | |
acde59b4 MM |
12857 | else if (TREE_CODE (arg) != OVERLOAD |
12858 | && TREE_CODE (arg) != FUNCTION_DECL) | |
12859 | /* If ARG is, for example, "(0, &f)" then its type will be unknown | |
12860 | -- but the deduction does not succeed because the expression is | |
12861 | not just the function on its own. */ | |
12862 | return false; | |
315fb5db | 12863 | else |
acde59b4 | 12864 | for (; arg; arg = OVL_NEXT (arg)) |
e04c614e JM |
12865 | if (try_one_overload (tparms, targs, tempargs, parm, |
12866 | TREE_TYPE (OVL_CURRENT (arg)), | |
12867 | strict, sub_strict, addr_p) | |
12868 | && (!goodfn || !decls_match (goodfn, OVL_CURRENT (arg)))) | |
12869 | { | |
12870 | goodfn = OVL_CURRENT (arg); | |
12871 | ++good; | |
12872 | } | |
34016c81 JM |
12873 | |
12874 | /* [temp.deduct.type] A template-argument can be deduced from a pointer | |
12875 | to function or pointer to member function argument if the set of | |
12876 | overloaded functions does not contain function templates and at most | |
12877 | one of a set of overloaded functions provides a unique match. | |
12878 | ||
12879 | So if we found multiple possibilities, we return success but don't | |
12880 | deduce anything. */ | |
12881 | ||
12882 | if (good == 1) | |
12883 | { | |
12884 | int i = TREE_VEC_LENGTH (targs); | |
12885 | for (; i--; ) | |
12886 | if (TREE_VEC_ELT (tempargs, i)) | |
12887 | TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (tempargs, i); | |
12888 | } | |
12889 | if (good) | |
acde59b4 | 12890 | return true; |
34016c81 | 12891 | |
acde59b4 | 12892 | return false; |
34016c81 JM |
12893 | } |
12894 | ||
12895 | /* Subroutine of resolve_overloaded_unification; does deduction for a single | |
12896 | overload. Fills TARGS with any deduced arguments, or error_mark_node if | |
12897 | different overloads deduce different arguments for a given parm. | |
f23fb7f5 MM |
12898 | ADDR_P is true if the expression for which deduction is being |
12899 | performed was of the form "& fn" rather than simply "fn". | |
12900 | ||
34016c81 JM |
12901 | Returns 1 on success. */ |
12902 | ||
12903 | static int | |
3a978d72 | 12904 | try_one_overload (tree tparms, |
0cbd7506 MS |
12905 | tree orig_targs, |
12906 | tree targs, | |
12907 | tree parm, | |
12908 | tree arg, | |
12909 | unification_kind_t strict, | |
f23fb7f5 MM |
12910 | int sub_strict, |
12911 | bool addr_p) | |
34016c81 JM |
12912 | { |
12913 | int nargs; | |
12914 | tree tempargs; | |
12915 | int i; | |
12916 | ||
12917 | /* [temp.deduct.type] A template-argument can be deduced from a pointer | |
12918 | to function or pointer to member function argument if the set of | |
12919 | overloaded functions does not contain function templates and at most | |
12920 | one of a set of overloaded functions provides a unique match. | |
12921 | ||
12922 | So if this is a template, just return success. */ | |
12923 | ||
12924 | if (uses_template_parms (arg)) | |
12925 | return 1; | |
12926 | ||
f23fb7f5 MM |
12927 | if (TREE_CODE (arg) == METHOD_TYPE) |
12928 | arg = build_ptrmemfunc_type (build_pointer_type (arg)); | |
12929 | else if (addr_p) | |
12930 | arg = build_pointer_type (arg); | |
12931 | ||
8af2fec4 | 12932 | sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL); |
34016c81 JM |
12933 | |
12934 | /* We don't copy orig_targs for this because if we have already deduced | |
12935 | some template args from previous args, unify would complain when we | |
12936 | try to deduce a template parameter for the same argument, even though | |
12937 | there isn't really a conflict. */ | |
12938 | nargs = TREE_VEC_LENGTH (targs); | |
f31c0a32 | 12939 | tempargs = make_tree_vec (nargs); |
34016c81 | 12940 | |
4393e105 | 12941 | if (unify (tparms, tempargs, parm, arg, sub_strict) != 0) |
34016c81 JM |
12942 | return 0; |
12943 | ||
12944 | /* First make sure we didn't deduce anything that conflicts with | |
e97e5263 | 12945 | explicitly specified args. */ |
34016c81 JM |
12946 | for (i = nargs; i--; ) |
12947 | { | |
12948 | tree elt = TREE_VEC_ELT (tempargs, i); | |
e97e5263 | 12949 | tree oldelt = TREE_VEC_ELT (orig_targs, i); |
34016c81 | 12950 | |
a34d3336 NS |
12951 | if (!elt) |
12952 | /*NOP*/; | |
34016c81 | 12953 | else if (uses_template_parms (elt)) |
a34d3336 NS |
12954 | /* Since we're unifying against ourselves, we will fill in |
12955 | template args used in the function parm list with our own | |
12956 | template parms. Discard them. */ | |
12957 | TREE_VEC_ELT (tempargs, i) = NULL_TREE; | |
12958 | else if (oldelt && !template_args_equal (oldelt, elt)) | |
34016c81 JM |
12959 | return 0; |
12960 | } | |
12961 | ||
12962 | for (i = nargs; i--; ) | |
12963 | { | |
12964 | tree elt = TREE_VEC_ELT (tempargs, i); | |
12965 | ||
12966 | if (elt) | |
12967 | TREE_VEC_ELT (targs, i) = elt; | |
12968 | } | |
12969 | ||
12970 | return 1; | |
12971 | } | |
12972 | ||
4393e105 MM |
12973 | /* PARM is a template class (perhaps with unbound template |
12974 | parameters). ARG is a fully instantiated type. If ARG can be | |
12975 | bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and | |
12976 | TARGS are as for unify. */ | |
fcfb9f96 MM |
12977 | |
12978 | static tree | |
3a978d72 | 12979 | try_class_unification (tree tparms, tree targs, tree parm, tree arg) |
4393e105 | 12980 | { |
4393e105 MM |
12981 | tree copy_of_targs; |
12982 | ||
12983 | if (!CLASSTYPE_TEMPLATE_INFO (arg) | |
c8094d83 | 12984 | || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg)) |
68361a03 | 12985 | != most_general_template (CLASSTYPE_TI_TEMPLATE (parm)))) |
4393e105 MM |
12986 | return NULL_TREE; |
12987 | ||
12988 | /* We need to make a new template argument vector for the call to | |
12989 | unify. If we used TARGS, we'd clutter it up with the result of | |
12990 | the attempted unification, even if this class didn't work out. | |
12991 | We also don't want to commit ourselves to all the unifications | |
12992 | we've already done, since unification is supposed to be done on | |
12993 | an argument-by-argument basis. In other words, consider the | |
12994 | following pathological case: | |
12995 | ||
12996 | template <int I, int J, int K> | |
12997 | struct S {}; | |
c8094d83 | 12998 | |
4393e105 MM |
12999 | template <int I, int J> |
13000 | struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {}; | |
c8094d83 | 13001 | |
4393e105 MM |
13002 | template <int I, int J, int K> |
13003 | void f(S<I, J, K>, S<I, I, I>); | |
c8094d83 | 13004 | |
4393e105 | 13005 | void g() { |
0cbd7506 MS |
13006 | S<0, 0, 0> s0; |
13007 | S<0, 1, 2> s2; | |
c8094d83 | 13008 | |
0cbd7506 | 13009 | f(s0, s2); |
4393e105 MM |
13010 | } |
13011 | ||
13012 | Now, by the time we consider the unification involving `s2', we | |
13013 | already know that we must have `f<0, 0, 0>'. But, even though | |
0e339752 | 13014 | `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid |
4393e105 MM |
13015 | because there are two ways to unify base classes of S<0, 1, 2> |
13016 | with S<I, I, I>. If we kept the already deduced knowledge, we | |
13017 | would reject the possibility I=1. */ | |
f31c0a32 | 13018 | copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs)); |
c8094d83 | 13019 | |
4393e105 | 13020 | /* If unification failed, we're done. */ |
74601d7c KL |
13021 | if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm), |
13022 | CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE)) | |
4393e105 | 13023 | return NULL_TREE; |
74601d7c KL |
13024 | |
13025 | return arg; | |
4393e105 MM |
13026 | } |
13027 | ||
a3a0fc7f NS |
13028 | /* Given a template type PARM and a class type ARG, find the unique |
13029 | base type in ARG that is an instance of PARM. We do not examine | |
13030 | ARG itself; only its base-classes. If there is not exactly one | |
13031 | appropriate base class, return NULL_TREE. PARM may be the type of | |
13032 | a partial specialization, as well as a plain template type. Used | |
13033 | by unify. */ | |
4393e105 MM |
13034 | |
13035 | static tree | |
a3a0fc7f | 13036 | get_template_base (tree tparms, tree targs, tree parm, tree arg) |
fcfb9f96 | 13037 | { |
a3a0fc7f NS |
13038 | tree rval = NULL_TREE; |
13039 | tree binfo; | |
13040 | ||
9e1e64ec | 13041 | gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg))); |
c8094d83 | 13042 | |
a3a0fc7f NS |
13043 | binfo = TYPE_BINFO (complete_type (arg)); |
13044 | if (!binfo) | |
13045 | /* The type could not be completed. */ | |
13046 | return NULL_TREE; | |
fcfb9f96 | 13047 | |
a3a0fc7f NS |
13048 | /* Walk in inheritance graph order. The search order is not |
13049 | important, and this avoids multiple walks of virtual bases. */ | |
13050 | for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo)) | |
fcfb9f96 | 13051 | { |
a3a0fc7f | 13052 | tree r = try_class_unification (tparms, targs, parm, BINFO_TYPE (binfo)); |
fcfb9f96 | 13053 | |
8d83768f NS |
13054 | if (r) |
13055 | { | |
13056 | /* If there is more than one satisfactory baseclass, then: | |
fcfb9f96 | 13057 | |
8d83768f | 13058 | [temp.deduct.call] |
fcfb9f96 | 13059 | |
8d83768f NS |
13060 | If they yield more than one possible deduced A, the type |
13061 | deduction fails. | |
4393e105 | 13062 | |
8d83768f | 13063 | applies. */ |
a3a0fc7f NS |
13064 | if (rval && !same_type_p (r, rval)) |
13065 | return NULL_TREE; | |
c8094d83 | 13066 | |
a3a0fc7f | 13067 | rval = r; |
8d83768f | 13068 | } |
fcfb9f96 MM |
13069 | } |
13070 | ||
a3a0fc7f | 13071 | return rval; |
fcfb9f96 MM |
13072 | } |
13073 | ||
db2767b6 MM |
13074 | /* Returns the level of DECL, which declares a template parameter. */ |
13075 | ||
e9659ab0 | 13076 | static int |
3a978d72 | 13077 | template_decl_level (tree decl) |
db2767b6 MM |
13078 | { |
13079 | switch (TREE_CODE (decl)) | |
13080 | { | |
13081 | case TYPE_DECL: | |
13082 | case TEMPLATE_DECL: | |
13083 | return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl)); | |
13084 | ||
13085 | case PARM_DECL: | |
13086 | return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl)); | |
13087 | ||
13088 | default: | |
315fb5db | 13089 | gcc_unreachable (); |
db2767b6 | 13090 | } |
315fb5db | 13091 | return 0; |
db2767b6 MM |
13092 | } |
13093 | ||
830bfa74 MM |
13094 | /* Decide whether ARG can be unified with PARM, considering only the |
13095 | cv-qualifiers of each type, given STRICT as documented for unify. | |
324f9dfb | 13096 | Returns nonzero iff the unification is OK on that basis. */ |
e92cc029 | 13097 | |
e9659ab0 | 13098 | static int |
3a978d72 | 13099 | check_cv_quals_for_unify (int strict, tree arg, tree parm) |
830bfa74 | 13100 | { |
4f2b0fb2 NS |
13101 | int arg_quals = cp_type_quals (arg); |
13102 | int parm_quals = cp_type_quals (parm); | |
13103 | ||
355f774d NS |
13104 | if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM |
13105 | && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL)) | |
4f2b0fb2 | 13106 | { |
2e9ceb77 | 13107 | /* Although a CVR qualifier is ignored when being applied to a |
0cbd7506 MS |
13108 | substituted template parameter ([8.3.2]/1 for example), that |
13109 | does not apply during deduction [14.8.2.4]/1, (even though | |
13110 | that is not explicitly mentioned, [14.8.2.4]/9 indicates | |
13111 | this). Except when we're allowing additional CV qualifiers | |
13112 | at the outer level [14.8.2.1]/3,1st bullet. */ | |
2e9ceb77 NS |
13113 | if ((TREE_CODE (arg) == REFERENCE_TYPE |
13114 | || TREE_CODE (arg) == FUNCTION_TYPE | |
13115 | || TREE_CODE (arg) == METHOD_TYPE) | |
13116 | && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE))) | |
13117 | return 0; | |
13118 | ||
13119 | if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM) | |
13120 | && (parm_quals & TYPE_QUAL_RESTRICT)) | |
13121 | return 0; | |
4f2b0fb2 | 13122 | } |
2e9ceb77 | 13123 | |
62e4a758 | 13124 | if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL)) |
4f2b0fb2 | 13125 | && (arg_quals & parm_quals) != parm_quals) |
ef637255 MM |
13126 | return 0; |
13127 | ||
62e4a758 | 13128 | if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL)) |
4f2b0fb2 | 13129 | && (parm_quals & arg_quals) != arg_quals) |
ef637255 MM |
13130 | return 0; |
13131 | ||
ef637255 | 13132 | return 1; |
830bfa74 MM |
13133 | } |
13134 | ||
5d80a306 DG |
13135 | /* Determines the LEVEL and INDEX for the template parameter PARM. */ |
13136 | void | |
13137 | template_parm_level_and_index (tree parm, int* level, int* index) | |
13138 | { | |
13139 | if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM | |
13140 | || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM | |
13141 | || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM) | |
13142 | { | |
13143 | *index = TEMPLATE_TYPE_IDX (parm); | |
13144 | *level = TEMPLATE_TYPE_LEVEL (parm); | |
13145 | } | |
13146 | else | |
13147 | { | |
13148 | *index = TEMPLATE_PARM_IDX (parm); | |
13149 | *level = TEMPLATE_PARM_LEVEL (parm); | |
13150 | } | |
13151 | } | |
13152 | ||
13153 | /* Unifies the remaining arguments in PACKED_ARGS with the pack | |
13154 | expansion at the end of PACKED_PARMS. Returns 0 if the type | |
13155 | deduction succeeds, 1 otherwise. STRICT is the same as in | |
13156 | unify. CALL_ARGS_P is true iff PACKED_ARGS is actually a function | |
13157 | call argument list. We'll need to adjust the arguments to make them | |
13158 | types. SUBR tells us if this is from a recursive call to | |
13159 | type_unification_real. */ | |
13160 | int | |
13161 | unify_pack_expansion (tree tparms, tree targs, tree packed_parms, | |
13162 | tree packed_args, int strict, bool call_args_p, | |
13163 | bool subr) | |
13164 | { | |
13165 | tree parm | |
13166 | = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1); | |
13167 | tree pattern = PACK_EXPANSION_PATTERN (parm); | |
13168 | tree pack, packs = NULL_TREE; | |
13169 | int i, start = TREE_VEC_LENGTH (packed_parms) - 1; | |
13170 | int len = TREE_VEC_LENGTH (packed_args); | |
13171 | ||
13172 | /* Determine the parameter packs we will be deducing from the | |
13173 | pattern, and record their current deductions. */ | |
13174 | for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm); | |
13175 | pack; pack = TREE_CHAIN (pack)) | |
13176 | { | |
13177 | tree parm_pack = TREE_VALUE (pack); | |
13178 | int idx, level; | |
13179 | ||
13180 | /* Determine the index and level of this parameter pack. */ | |
13181 | template_parm_level_and_index (parm_pack, &level, &idx); | |
13182 | ||
13183 | /* Keep track of the parameter packs and their corresponding | |
13184 | argument packs. */ | |
13185 | packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs); | |
13186 | TREE_TYPE (packs) = make_tree_vec (len - start); | |
13187 | } | |
13188 | ||
13189 | /* Loop through all of the arguments that have not yet been | |
13190 | unified and unify each with the pattern. */ | |
13191 | for (i = start; i < len; i++) | |
13192 | { | |
13193 | tree parm = pattern; | |
13194 | ||
13195 | /* For each parameter pack, clear out the deduced value so that | |
13196 | we can deduce it again. */ | |
13197 | for (pack = packs; pack; pack = TREE_CHAIN (pack)) | |
13198 | { | |
13199 | int idx, level; | |
13200 | template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx); | |
13201 | ||
13202 | TMPL_ARG (targs, level, idx) = NULL_TREE; | |
13203 | } | |
13204 | ||
13205 | /* Unify the pattern with the current argument. */ | |
13206 | { | |
13207 | tree arg = TREE_VEC_ELT (packed_args, i); | |
2afad0f6 | 13208 | tree arg_expr = NULL_TREE; |
5d80a306 DG |
13209 | int arg_strict = strict; |
13210 | bool skip_arg_p = false; | |
13211 | ||
13212 | if (call_args_p) | |
13213 | { | |
13214 | int sub_strict; | |
13215 | ||
13216 | /* This mirrors what we do in type_unification_real. */ | |
13217 | switch (strict) | |
13218 | { | |
13219 | case DEDUCE_CALL: | |
13220 | sub_strict = (UNIFY_ALLOW_OUTER_LEVEL | |
13221 | | UNIFY_ALLOW_MORE_CV_QUAL | |
13222 | | UNIFY_ALLOW_DERIVED); | |
13223 | break; | |
13224 | ||
13225 | case DEDUCE_CONV: | |
13226 | sub_strict = UNIFY_ALLOW_LESS_CV_QUAL; | |
13227 | break; | |
13228 | ||
13229 | case DEDUCE_EXACT: | |
13230 | sub_strict = UNIFY_ALLOW_NONE; | |
13231 | break; | |
13232 | ||
13233 | default: | |
13234 | gcc_unreachable (); | |
13235 | } | |
13236 | ||
13237 | if (!TYPE_P (arg)) | |
13238 | { | |
13239 | gcc_assert (TREE_TYPE (arg) != NULL_TREE); | |
13240 | if (type_unknown_p (arg)) | |
13241 | { | |
13242 | /* [temp.deduct.type] A template-argument can be | |
13243 | deduced from a pointer to function or pointer | |
13244 | to member function argument if the set of | |
13245 | overloaded functions does not contain function | |
13246 | templates and at most one of a set of | |
13247 | overloaded functions provides a unique | |
13248 | match. */ | |
13249 | ||
13250 | if (resolve_overloaded_unification | |
a916d97f ILT |
13251 | (tparms, targs, parm, arg, |
13252 | (unification_kind_t) strict, | |
13253 | sub_strict) | |
5d80a306 DG |
13254 | != 0) |
13255 | return 1; | |
13256 | skip_arg_p = true; | |
13257 | } | |
13258 | ||
13259 | if (!skip_arg_p) | |
13260 | { | |
2afad0f6 DG |
13261 | arg_expr = arg; |
13262 | arg = unlowered_expr_type (arg); | |
5d80a306 DG |
13263 | if (arg == error_mark_node) |
13264 | return 1; | |
13265 | } | |
13266 | } | |
13267 | ||
13268 | arg_strict = sub_strict; | |
13269 | ||
13270 | if (!subr) | |
13271 | arg_strict |= | |
a916d97f ILT |
13272 | maybe_adjust_types_for_deduction ((unification_kind_t) strict, |
13273 | &parm, &arg, arg_expr); | |
5d80a306 DG |
13274 | } |
13275 | ||
13276 | if (!skip_arg_p) | |
13277 | { | |
13278 | if (unify (tparms, targs, parm, arg, arg_strict)) | |
13279 | return 1; | |
13280 | } | |
13281 | } | |
13282 | ||
13283 | /* For each parameter pack, collect the deduced value. */ | |
13284 | for (pack = packs; pack; pack = TREE_CHAIN (pack)) | |
13285 | { | |
13286 | int idx, level; | |
13287 | template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx); | |
13288 | ||
13289 | TREE_VEC_ELT (TREE_TYPE (pack), i - start) = | |
13290 | TMPL_ARG (targs, level, idx); | |
13291 | } | |
13292 | } | |
13293 | ||
13294 | /* Verify that the results of unification with the parameter packs | |
13295 | produce results consistent with what we've seen before, and make | |
13296 | the deduced argument packs available. */ | |
13297 | for (pack = packs; pack; pack = TREE_CHAIN (pack)) | |
13298 | { | |
13299 | tree old_pack = TREE_VALUE (pack); | |
13300 | tree new_args = TREE_TYPE (pack); | |
db60ff18 DG |
13301 | int i, len = TREE_VEC_LENGTH (new_args); |
13302 | bool nondeduced_p = false; | |
13303 | ||
13304 | /* If NEW_ARGS contains any NULL_TREE entries, we didn't | |
13305 | actually deduce anything. */ | |
13306 | for (i = 0; i < len && !nondeduced_p; ++i) | |
13307 | if (TREE_VEC_ELT (new_args, i) == NULL_TREE) | |
13308 | nondeduced_p = true; | |
13309 | if (nondeduced_p) | |
13310 | continue; | |
5d80a306 DG |
13311 | |
13312 | if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack)) | |
13313 | { | |
13314 | /* Prepend the explicit arguments onto NEW_ARGS. */ | |
13315 | tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack); | |
13316 | tree old_args = new_args; | |
13317 | int i, explicit_len = TREE_VEC_LENGTH (explicit_args); | |
13318 | int len = explicit_len + TREE_VEC_LENGTH (old_args); | |
13319 | ||
13320 | /* Copy the explicit arguments. */ | |
13321 | new_args = make_tree_vec (len); | |
13322 | for (i = 0; i < explicit_len; i++) | |
13323 | TREE_VEC_ELT (new_args, i) = TREE_VEC_ELT (explicit_args, i); | |
13324 | ||
13325 | /* Copy the deduced arguments. */ | |
13326 | for (; i < len; i++) | |
13327 | TREE_VEC_ELT (new_args, i) = | |
13328 | TREE_VEC_ELT (old_args, i - explicit_len); | |
13329 | } | |
13330 | ||
13331 | if (!old_pack) | |
13332 | { | |
13333 | tree result; | |
13334 | int idx, level; | |
13335 | ||
13336 | template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx); | |
13337 | ||
13338 | /* Build the deduced *_ARGUMENT_PACK. */ | |
13339 | if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX) | |
13340 | { | |
13341 | result = make_node (NONTYPE_ARGUMENT_PACK); | |
13342 | TREE_TYPE (result) = | |
13343 | TREE_TYPE (TEMPLATE_PARM_DECL (TREE_PURPOSE (pack))); | |
13344 | TREE_CONSTANT (result) = 1; | |
13345 | } | |
13346 | else | |
13347 | result = make_node (TYPE_ARGUMENT_PACK); | |
13348 | ||
13349 | SET_ARGUMENT_PACK_ARGS (result, new_args); | |
13350 | ||
13351 | /* Note the deduced argument packs for this parameter | |
13352 | pack. */ | |
13353 | TMPL_ARG (targs, level, idx) = result; | |
13354 | } | |
13355 | else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack) | |
13356 | && (ARGUMENT_PACK_ARGS (old_pack) | |
13357 | == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack))) | |
13358 | { | |
13359 | /* We only had the explicitly-provided arguments before, but | |
13360 | now we have a complete set of arguments. */ | |
13361 | int idx, level; | |
13362 | tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack); | |
13363 | template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx); | |
13364 | ||
13365 | /* Keep the original deduced argument pack. */ | |
13366 | TMPL_ARG (targs, level, idx) = old_pack; | |
13367 | ||
13368 | SET_ARGUMENT_PACK_ARGS (old_pack, new_args); | |
13369 | ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1; | |
13370 | ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args; | |
13371 | } | |
13372 | else if (!comp_template_args (ARGUMENT_PACK_ARGS (old_pack), | |
13373 | new_args)) | |
13374 | /* Inconsistent unification of this parameter pack. */ | |
13375 | return 1; | |
13376 | else | |
13377 | { | |
13378 | int idx, level; | |
13379 | ||
13380 | template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx); | |
13381 | ||
13382 | /* Keep the original deduced argument pack. */ | |
13383 | TMPL_ARG (targs, level, idx) = old_pack; | |
13384 | } | |
13385 | } | |
13386 | ||
13387 | return 0; | |
13388 | } | |
13389 | ||
916b63c3 MM |
13390 | /* Deduce the value of template parameters. TPARMS is the (innermost) |
13391 | set of template parameters to a template. TARGS is the bindings | |
13392 | for those template parameters, as determined thus far; TARGS may | |
13393 | include template arguments for outer levels of template parameters | |
13394 | as well. PARM is a parameter to a template function, or a | |
13395 | subcomponent of that parameter; ARG is the corresponding argument. | |
13396 | This function attempts to match PARM with ARG in a manner | |
13397 | consistent with the existing assignments in TARGS. If more values | |
13398 | are deduced, then TARGS is updated. | |
13399 | ||
13400 | Returns 0 if the type deduction succeeds, 1 otherwise. The | |
13401 | parameter STRICT is a bitwise or of the following flags: | |
830bfa74 MM |
13402 | |
13403 | UNIFY_ALLOW_NONE: | |
13404 | Require an exact match between PARM and ARG. | |
13405 | UNIFY_ALLOW_MORE_CV_QUAL: | |
028d1f20 NS |
13406 | Allow the deduced ARG to be more cv-qualified (by qualification |
13407 | conversion) than ARG. | |
830bfa74 MM |
13408 | UNIFY_ALLOW_LESS_CV_QUAL: |
13409 | Allow the deduced ARG to be less cv-qualified than ARG. | |
13410 | UNIFY_ALLOW_DERIVED: | |
13411 | Allow the deduced ARG to be a template base class of ARG, | |
13412 | or a pointer to a template base class of the type pointed to by | |
161c12b0 JM |
13413 | ARG. |
13414 | UNIFY_ALLOW_INTEGER: | |
13415 | Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX | |
c8094d83 | 13416 | case for more information. |
028d1f20 NS |
13417 | UNIFY_ALLOW_OUTER_LEVEL: |
13418 | This is the outermost level of a deduction. Used to determine validity | |
13419 | of qualification conversions. A valid qualification conversion must | |
13420 | have const qualified pointers leading up to the inner type which | |
13421 | requires additional CV quals, except at the outer level, where const | |
13422 | is not required [conv.qual]. It would be normal to set this flag in | |
62e4a758 NS |
13423 | addition to setting UNIFY_ALLOW_MORE_CV_QUAL. |
13424 | UNIFY_ALLOW_OUTER_MORE_CV_QUAL: | |
13425 | This is the outermost level of a deduction, and PARM can be more CV | |
13426 | qualified at this point. | |
13427 | UNIFY_ALLOW_OUTER_LESS_CV_QUAL: | |
13428 | This is the outermost level of a deduction, and PARM can be less CV | |
8baddbf1 | 13429 | qualified at this point. */ |
830bfa74 | 13430 | |
e9659ab0 | 13431 | static int |
3a978d72 | 13432 | unify (tree tparms, tree targs, tree parm, tree arg, int strict) |
8d08fdba MS |
13433 | { |
13434 | int idx; | |
050367a3 | 13435 | tree targ; |
db2767b6 | 13436 | tree tparm; |
028d1f20 | 13437 | int strict_in = strict; |
8d08fdba MS |
13438 | |
13439 | /* I don't think this will do the right thing with respect to types. | |
13440 | But the only case I've seen it in so far has been array bounds, where | |
13441 | signedness is the only information lost, and I think that will be | |
13442 | okay. */ | |
13443 | while (TREE_CODE (parm) == NOP_EXPR) | |
13444 | parm = TREE_OPERAND (parm, 0); | |
13445 | ||
13446 | if (arg == error_mark_node) | |
13447 | return 1; | |
09357846 JM |
13448 | if (arg == unknown_type_node |
13449 | || arg == init_list_type_node) | |
34016c81 JM |
13450 | /* We can't deduce anything from this, but we might get all the |
13451 | template args from other function args. */ | |
13452 | return 0; | |
13453 | ||
db2767b6 | 13454 | /* If PARM uses template parameters, then we can't bail out here, |
6bdb985f | 13455 | even if ARG == PARM, since we won't record unifications for the |
db2767b6 MM |
13456 | template parameters. We might need them if we're trying to |
13457 | figure out which of two things is more specialized. */ | |
13458 | if (arg == parm && !uses_template_parms (parm)) | |
8d08fdba MS |
13459 | return 0; |
13460 | ||
09357846 JM |
13461 | /* Handle init lists early, so the rest of the function can assume |
13462 | we're dealing with a type. */ | |
13463 | if (BRACE_ENCLOSED_INITIALIZER_P (arg)) | |
13464 | { | |
13465 | tree elt, elttype; | |
13466 | unsigned i; | |
13467 | ||
13468 | if (!is_std_init_list (parm)) | |
13469 | /* We can only deduce from an initializer list argument if the | |
13470 | parameter is std::initializer_list; otherwise this is a | |
13471 | non-deduced context. */ | |
13472 | return 0; | |
13473 | ||
13474 | elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0); | |
13475 | ||
13476 | FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt) | |
13477 | { | |
2604de9d | 13478 | int elt_strict = strict; |
09357846 | 13479 | if (!BRACE_ENCLOSED_INITIALIZER_P (elt)) |
2604de9d JM |
13480 | { |
13481 | tree type = TREE_TYPE (elt); | |
13482 | /* It should only be possible to get here for a call. */ | |
13483 | gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL); | |
13484 | elt_strict |= maybe_adjust_types_for_deduction | |
13485 | (DEDUCE_CALL, &elttype, &type, elt); | |
13486 | elt = type; | |
13487 | } | |
13488 | ||
13489 | if (unify (tparms, targs, elttype, elt, elt_strict)) | |
09357846 JM |
13490 | return 1; |
13491 | } | |
13492 | return 0; | |
13493 | } | |
13494 | ||
830bfa74 MM |
13495 | /* Immediately reject some pairs that won't unify because of |
13496 | cv-qualification mismatches. */ | |
13497 | if (TREE_CODE (arg) == TREE_CODE (parm) | |
2f939d94 | 13498 | && TYPE_P (arg) |
d0ab7624 | 13499 | /* It is the elements of the array which hold the cv quals of an array |
0cbd7506 MS |
13500 | type, and the elements might be template type parms. We'll check |
13501 | when we recurse. */ | |
d0ab7624 | 13502 | && TREE_CODE (arg) != ARRAY_TYPE |
830bfa74 MM |
13503 | /* We check the cv-qualifiers when unifying with template type |
13504 | parameters below. We want to allow ARG `const T' to unify with | |
13505 | PARM `T' for example, when computing which of two templates | |
13506 | is more specialized, for example. */ | |
13507 | && TREE_CODE (arg) != TEMPLATE_TYPE_PARM | |
028d1f20 | 13508 | && !check_cv_quals_for_unify (strict_in, arg, parm)) |
49432171 JM |
13509 | return 1; |
13510 | ||
028d1f20 | 13511 | if (!(strict & UNIFY_ALLOW_OUTER_LEVEL) |
3ea099f1 | 13512 | && TYPE_P (parm) && !CP_TYPE_CONST_P (parm)) |
028d1f20 NS |
13513 | strict &= ~UNIFY_ALLOW_MORE_CV_QUAL; |
13514 | strict &= ~UNIFY_ALLOW_OUTER_LEVEL; | |
13515 | strict &= ~UNIFY_ALLOW_DERIVED; | |
62e4a758 NS |
13516 | strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL; |
13517 | strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL; | |
c8094d83 | 13518 | |
8d08fdba MS |
13519 | switch (TREE_CODE (parm)) |
13520 | { | |
2ca340ae | 13521 | case TYPENAME_TYPE: |
fccef71e | 13522 | case SCOPE_REF: |
b8c6534b | 13523 | case UNBOUND_CLASS_TEMPLATE: |
2ca340ae JM |
13524 | /* In a type which contains a nested-name-specifier, template |
13525 | argument values cannot be deduced for template parameters used | |
13526 | within the nested-name-specifier. */ | |
13527 | return 0; | |
13528 | ||
8d08fdba | 13529 | case TEMPLATE_TYPE_PARM: |
73b0fce8 | 13530 | case TEMPLATE_TEMPLATE_PARM: |
a1281f45 | 13531 | case BOUND_TEMPLATE_TEMPLATE_PARM: |
db2767b6 | 13532 | tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0)); |
833be229 PC |
13533 | if (tparm == error_mark_node) |
13534 | return 1; | |
db2767b6 MM |
13535 | |
13536 | if (TEMPLATE_TYPE_LEVEL (parm) | |
13537 | != template_decl_level (tparm)) | |
13538 | /* The PARM is not one we're trying to unify. Just check | |
13539 | to see if it matches ARG. */ | |
13540 | return (TREE_CODE (arg) == TREE_CODE (parm) | |
3bfdc719 | 13541 | && same_type_p (parm, arg)) ? 0 : 1; |
73b0fce8 | 13542 | idx = TEMPLATE_TYPE_IDX (parm); |
916b63c3 | 13543 | targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx); |
db2767b6 | 13544 | tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx)); |
050367a3 | 13545 | |
73b0fce8 | 13546 | /* Check for mixed types and values. */ |
db2767b6 MM |
13547 | if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM |
13548 | && TREE_CODE (tparm) != TYPE_DECL) | |
c8094d83 | 13549 | || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM |
db2767b6 | 13550 | && TREE_CODE (tparm) != TEMPLATE_DECL)) |
73b0fce8 KL |
13551 | return 1; |
13552 | ||
a1281f45 | 13553 | if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM) |
73b0fce8 | 13554 | { |
b429fdf0 KL |
13555 | /* ARG must be constructed from a template class or a template |
13556 | template parameter. */ | |
13557 | if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM | |
4e95268d | 13558 | && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg)) |
a1281f45 | 13559 | return 1; |
73b0fce8 | 13560 | |
a1281f45 | 13561 | { |
a1281f45 | 13562 | tree parmvec = TYPE_TI_ARGS (parm); |
6df91b00 | 13563 | tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg)); |
a0a5f30f | 13564 | tree parm_parms |
063cc99c | 13565 | = DECL_INNERMOST_TEMPLATE_PARMS |
a0a5f30f | 13566 | (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (parm)); |
4cf36211 DG |
13567 | int i, len; |
13568 | int parm_variadic_p = 0; | |
73b0fce8 | 13569 | |
e7e93965 MM |
13570 | /* The resolution to DR150 makes clear that default |
13571 | arguments for an N-argument may not be used to bind T | |
13572 | to a template template parameter with fewer than N | |
13573 | parameters. It is not safe to permit the binding of | |
13574 | default arguments as an extension, as that may change | |
13575 | the meaning of a conforming program. Consider: | |
13576 | ||
13577 | struct Dense { static const unsigned int dim = 1; }; | |
13578 | ||
13579 | template <template <typename> class View, | |
13580 | typename Block> | |
13581 | void operator+(float, View<Block> const&); | |
13582 | ||
3db45ab5 MS |
13583 | template <typename Block, |
13584 | unsigned int Dim = Block::dim> | |
e7e93965 MM |
13585 | struct Lvalue_proxy { operator float() const; }; |
13586 | ||
13587 | void | |
13588 | test_1d (void) { | |
13589 | Lvalue_proxy<Dense> p; | |
13590 | float b; | |
13591 | b + p; | |
13592 | } | |
a1281f45 | 13593 | |
e7e93965 MM |
13594 | Here, if Lvalue_proxy is permitted to bind to View, then |
13595 | the global operator+ will be used; if they are not, the | |
3db45ab5 | 13596 | Lvalue_proxy will be converted to float. */ |
a0a5f30f DG |
13597 | if (coerce_template_parms (parm_parms, |
13598 | argvec, | |
e7e93965 MM |
13599 | TYPE_TI_TEMPLATE (parm), |
13600 | tf_none, | |
13601 | /*require_all_args=*/true, | |
13602 | /*use_default_args=*/false) | |
0cbd7506 | 13603 | == error_mark_node) |
a1281f45 | 13604 | return 1; |
c8094d83 MS |
13605 | |
13606 | /* Deduce arguments T, i from TT<T> or TT<i>. | |
a1281f45 KL |
13607 | We check each element of PARMVEC and ARGVEC individually |
13608 | rather than the whole TREE_VEC since they can have | |
13609 | different number of elements. */ | |
6b9b6b15 | 13610 | |
4cf36211 DG |
13611 | parmvec = expand_template_argument_pack (parmvec); |
13612 | argvec = expand_template_argument_pack (argvec); | |
13613 | ||
13614 | len = TREE_VEC_LENGTH (parmvec); | |
13615 | ||
13616 | /* Check if the parameters end in a pack, making them | |
13617 | variadic. */ | |
13618 | if (len > 0 | |
13619 | && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1))) | |
13620 | parm_variadic_p = 1; | |
13621 | ||
13622 | if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p) | |
13623 | return 1; | |
13624 | ||
13625 | for (i = 0; i < len - parm_variadic_p; ++i) | |
a1281f45 | 13626 | { |
0cbd7506 | 13627 | if (unify (tparms, targs, |
c8094d83 MS |
13628 | TREE_VEC_ELT (parmvec, i), |
13629 | TREE_VEC_ELT (argvec, i), | |
a1281f45 KL |
13630 | UNIFY_ALLOW_NONE)) |
13631 | return 1; | |
73b0fce8 | 13632 | } |
4cf36211 DG |
13633 | |
13634 | if (parm_variadic_p | |
13635 | && unify_pack_expansion (tparms, targs, | |
13636 | parmvec, argvec, | |
13637 | UNIFY_ALLOW_NONE, | |
13638 | /*call_args_p=*/false, | |
13639 | /*subr=*/false)) | |
13640 | return 1; | |
a1281f45 | 13641 | } |
b429fdf0 | 13642 | arg = TYPE_TI_TEMPLATE (arg); |
a1281f45 KL |
13643 | |
13644 | /* Fall through to deduce template name. */ | |
13645 | } | |
13646 | ||
13647 | if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM | |
13648 | || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM) | |
13649 | { | |
13650 | /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */ | |
13651 | ||
13652 | /* Simple cases: Value already set, does match or doesn't. */ | |
13653 | if (targ != NULL_TREE && template_args_equal (targ, arg)) | |
13654 | return 0; | |
13655 | else if (targ) | |
13656 | return 1; | |
db2767b6 MM |
13657 | } |
13658 | else | |
13659 | { | |
830bfa74 MM |
13660 | /* If PARM is `const T' and ARG is only `int', we don't have |
13661 | a match unless we are allowing additional qualification. | |
13662 | If ARG is `const int' and PARM is just `T' that's OK; | |
13663 | that binds `const int' to `T'. */ | |
c8094d83 | 13664 | if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL, |
830bfa74 | 13665 | arg, parm)) |
db2767b6 MM |
13666 | return 1; |
13667 | ||
830bfa74 MM |
13668 | /* Consider the case where ARG is `const volatile int' and |
13669 | PARM is `const T'. Then, T should be `volatile int'. */ | |
c2ea3a40 NS |
13670 | arg = cp_build_qualified_type_real |
13671 | (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none); | |
adecb3f4 MM |
13672 | if (arg == error_mark_node) |
13673 | return 1; | |
73b0fce8 | 13674 | |
a1281f45 KL |
13675 | /* Simple cases: Value already set, does match or doesn't. */ |
13676 | if (targ != NULL_TREE && same_type_p (targ, arg)) | |
13677 | return 0; | |
13678 | else if (targ) | |
13679 | return 1; | |
61cd552e | 13680 | |
94fc547c MM |
13681 | /* Make sure that ARG is not a variable-sized array. (Note |
13682 | that were talking about variable-sized arrays (like | |
13683 | `int[n]'), rather than arrays of unknown size (like | |
13684 | `int[]').) We'll get very confused by such a type since | |
13685 | the bound of the array will not be computable in an | |
13686 | instantiation. Besides, such types are not allowed in | |
13687 | ISO C++, so we can do as we please here. */ | |
5377d5ba | 13688 | if (variably_modified_type_p (arg, NULL_TREE)) |
94fc547c | 13689 | return 1; |
a4d674fc JM |
13690 | |
13691 | /* Strip typedefs as in convert_template_argument. */ | |
cd41d410 | 13692 | arg = strip_typedefs (arg); |
94fc547c | 13693 | } |
61cd552e | 13694 | |
5d80a306 DG |
13695 | /* If ARG is a parameter pack or an expansion, we cannot unify |
13696 | against it unless PARM is also a parameter pack. */ | |
13697 | if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg)) | |
13698 | && !template_parameter_pack_p (parm)) | |
13699 | return 1; | |
13700 | ||
111a28c2 DS |
13701 | /* If the argument deduction results is a METHOD_TYPE, |
13702 | then there is a problem. | |
13703 | METHOD_TYPE doesn't map to any real C++ type the result of | |
13704 | the deduction can not be of that type. */ | |
13705 | if (TREE_CODE (arg) == METHOD_TYPE) | |
13706 | return 1; | |
13707 | ||
916b63c3 | 13708 | TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg; |
73b0fce8 KL |
13709 | return 0; |
13710 | ||
f84b4be9 | 13711 | case TEMPLATE_PARM_INDEX: |
db2767b6 | 13712 | tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0)); |
3e9ac7e5 VR |
13713 | if (tparm == error_mark_node) |
13714 | return 1; | |
db2767b6 | 13715 | |
c8094d83 | 13716 | if (TEMPLATE_PARM_LEVEL (parm) |
db2767b6 MM |
13717 | != template_decl_level (tparm)) |
13718 | /* The PARM is not one we're trying to unify. Just check | |
13719 | to see if it matches ARG. */ | |
c8a209ca NS |
13720 | return !(TREE_CODE (arg) == TREE_CODE (parm) |
13721 | && cp_tree_equal (parm, arg)); | |
db2767b6 | 13722 | |
f84b4be9 | 13723 | idx = TEMPLATE_PARM_IDX (parm); |
916b63c3 | 13724 | targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx); |
db2767b6 | 13725 | |
050367a3 | 13726 | if (targ) |
c8a209ca | 13727 | return !cp_tree_equal (targ, arg); |
8d08fdba | 13728 | |
161c12b0 JM |
13729 | /* [temp.deduct.type] If, in the declaration of a function template |
13730 | with a non-type template-parameter, the non-type | |
13731 | template-parameter is used in an expression in the function | |
13732 | parameter-list and, if the corresponding template-argument is | |
13733 | deduced, the template-argument type shall match the type of the | |
13734 | template-parameter exactly, except that a template-argument | |
c8094d83 | 13735 | deduced from an array bound may be of any integral type. |
d7c4edd0 | 13736 | The non-type parameter might use already deduced type parameters. */ |
bd0d5d4a | 13737 | tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE); |
e2005c8d KL |
13738 | if (!TREE_TYPE (arg)) |
13739 | /* Template-parameter dependent expression. Just accept it for now. | |
13740 | It will later be processed in convert_template_argument. */ | |
13741 | ; | |
13742 | else if (same_type_p (TREE_TYPE (arg), tparm)) | |
13743 | /* OK */; | |
161c12b0 | 13744 | else if ((strict & UNIFY_ALLOW_INTEGER) |
bd0d5d4a JM |
13745 | && (TREE_CODE (tparm) == INTEGER_TYPE |
13746 | || TREE_CODE (tparm) == BOOLEAN_TYPE)) | |
8baddbf1 MM |
13747 | /* Convert the ARG to the type of PARM; the deduced non-type |
13748 | template argument must exactly match the types of the | |
13749 | corresponding parameter. */ | |
ed7284dd | 13750 | arg = fold (build_nop (tparm, arg)); |
bd0d5d4a JM |
13751 | else if (uses_template_parms (tparm)) |
13752 | /* We haven't deduced the type of this parameter yet. Try again | |
13753 | later. */ | |
13754 | return 0; | |
161c12b0 JM |
13755 | else |
13756 | return 1; | |
13757 | ||
5d80a306 DG |
13758 | /* If ARG is a parameter pack or an expansion, we cannot unify |
13759 | against it unless PARM is also a parameter pack. */ | |
13760 | if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg)) | |
13761 | && !TEMPLATE_PARM_PARAMETER_PACK (parm)) | |
13762 | return 1; | |
13763 | ||
916b63c3 | 13764 | TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg; |
8d08fdba MS |
13765 | return 0; |
13766 | ||
28e8f3a0 GB |
13767 | case PTRMEM_CST: |
13768 | { | |
0cbd7506 MS |
13769 | /* A pointer-to-member constant can be unified only with |
13770 | another constant. */ | |
28e8f3a0 | 13771 | if (TREE_CODE (arg) != PTRMEM_CST) |
0cbd7506 | 13772 | return 1; |
28e8f3a0 GB |
13773 | |
13774 | /* Just unify the class member. It would be useless (and possibly | |
0cbd7506 MS |
13775 | wrong, depending on the strict flags) to unify also |
13776 | PTRMEM_CST_CLASS, because we want to be sure that both parm and | |
13777 | arg refer to the same variable, even if through different | |
13778 | classes. For instance: | |
28e8f3a0 | 13779 | |
0cbd7506 MS |
13780 | struct A { int x; }; |
13781 | struct B : A { }; | |
28e8f3a0 | 13782 | |
0cbd7506 | 13783 | Unification of &A::x and &B::x must succeed. */ |
28e8f3a0 | 13784 | return unify (tparms, targs, PTRMEM_CST_MEMBER (parm), |
0cbd7506 | 13785 | PTRMEM_CST_MEMBER (arg), strict); |
28e8f3a0 GB |
13786 | } |
13787 | ||
8d08fdba | 13788 | case POINTER_TYPE: |
830bfa74 | 13789 | { |
830bfa74 MM |
13790 | if (TREE_CODE (arg) != POINTER_TYPE) |
13791 | return 1; | |
c8094d83 | 13792 | |
830bfa74 MM |
13793 | /* [temp.deduct.call] |
13794 | ||
13795 | A can be another pointer or pointer to member type that can | |
13796 | be converted to the deduced A via a qualification | |
13797 | conversion (_conv.qual_). | |
13798 | ||
13799 | We pass down STRICT here rather than UNIFY_ALLOW_NONE. | |
13800 | This will allow for additional cv-qualification of the | |
028d1f20 | 13801 | pointed-to types if appropriate. */ |
c8094d83 | 13802 | |
028d1f20 | 13803 | if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE) |
830bfa74 MM |
13804 | /* The derived-to-base conversion only persists through one |
13805 | level of pointers. */ | |
028d1f20 | 13806 | strict |= (strict_in & UNIFY_ALLOW_DERIVED); |
830bfa74 | 13807 | |
c8094d83 | 13808 | return unify (tparms, targs, TREE_TYPE (parm), |
028d1f20 | 13809 | TREE_TYPE (arg), strict); |
830bfa74 | 13810 | } |
8d08fdba MS |
13811 | |
13812 | case REFERENCE_TYPE: | |
830bfa74 MM |
13813 | if (TREE_CODE (arg) != REFERENCE_TYPE) |
13814 | return 1; | |
13815 | return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg), | |
028d1f20 | 13816 | strict & UNIFY_ALLOW_MORE_CV_QUAL); |
8d08fdba MS |
13817 | |
13818 | case ARRAY_TYPE: | |
13819 | if (TREE_CODE (arg) != ARRAY_TYPE) | |
13820 | return 1; | |
3042d5be MM |
13821 | if ((TYPE_DOMAIN (parm) == NULL_TREE) |
13822 | != (TYPE_DOMAIN (arg) == NULL_TREE)) | |
13823 | return 1; | |
8baddbf1 MM |
13824 | if (TYPE_DOMAIN (parm) != NULL_TREE) |
13825 | { | |
13826 | tree parm_max; | |
13827 | tree arg_max; | |
6ee3ffe8 MM |
13828 | bool parm_cst; |
13829 | bool arg_cst; | |
8baddbf1 MM |
13830 | |
13831 | /* Our representation of array types uses "N - 1" as the | |
13832 | TYPE_MAX_VALUE for an array with "N" elements, if "N" is | |
6ee3ffe8 MM |
13833 | not an integer constant. We cannot unify arbitrarily |
13834 | complex expressions, so we eliminate the MINUS_EXPRs | |
13835 | here. */ | |
13836 | parm_max = TYPE_MAX_VALUE (TYPE_DOMAIN (parm)); | |
13837 | parm_cst = TREE_CODE (parm_max) == INTEGER_CST; | |
13838 | if (!parm_cst) | |
8baddbf1 | 13839 | { |
6ee3ffe8 | 13840 | gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR); |
8baddbf1 MM |
13841 | parm_max = TREE_OPERAND (parm_max, 0); |
13842 | } | |
6ee3ffe8 MM |
13843 | arg_max = TYPE_MAX_VALUE (TYPE_DOMAIN (arg)); |
13844 | arg_cst = TREE_CODE (arg_max) == INTEGER_CST; | |
13845 | if (!arg_cst) | |
13846 | { | |
13847 | /* The ARG_MAX may not be a simple MINUS_EXPR, if we are | |
13848 | trying to unify the type of a variable with the type | |
13849 | of a template parameter. For example: | |
13850 | ||
13851 | template <unsigned int N> | |
13852 | void f (char (&) [N]); | |
13853 | int g(); | |
13854 | void h(int i) { | |
13855 | char a[g(i)]; | |
13856 | f(a); | |
13857 | } | |
13858 | ||
13859 | Here, the type of the ARG will be "int [g(i)]", and | |
13860 | may be a SAVE_EXPR, etc. */ | |
13861 | if (TREE_CODE (arg_max) != MINUS_EXPR) | |
13862 | return 1; | |
13863 | arg_max = TREE_OPERAND (arg_max, 0); | |
13864 | } | |
13865 | ||
13866 | /* If only one of the bounds used a MINUS_EXPR, compensate | |
13867 | by adding one to the other bound. */ | |
13868 | if (parm_cst && !arg_cst) | |
13869 | parm_max = fold_build2 (PLUS_EXPR, | |
13870 | integer_type_node, | |
13871 | parm_max, | |
13872 | integer_one_node); | |
13873 | else if (arg_cst && !parm_cst) | |
13874 | arg_max = fold_build2 (PLUS_EXPR, | |
13875 | integer_type_node, | |
13876 | arg_max, | |
13877 | integer_one_node); | |
8baddbf1 MM |
13878 | |
13879 | if (unify (tparms, targs, parm_max, arg_max, UNIFY_ALLOW_INTEGER)) | |
13880 | return 1; | |
13881 | } | |
830bfa74 | 13882 | return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg), |
712467a4 | 13883 | strict & UNIFY_ALLOW_MORE_CV_QUAL); |
8d08fdba MS |
13884 | |
13885 | case REAL_TYPE: | |
37c46b43 | 13886 | case COMPLEX_TYPE: |
c00996a3 | 13887 | case VECTOR_TYPE: |
8d08fdba | 13888 | case INTEGER_TYPE: |
42976354 | 13889 | case BOOLEAN_TYPE: |
3590f0a6 | 13890 | case ENUMERAL_TYPE: |
5ad5a526 | 13891 | case VOID_TYPE: |
f376e137 MS |
13892 | if (TREE_CODE (arg) != TREE_CODE (parm)) |
13893 | return 1; | |
c8094d83 | 13894 | |
9edc3913 | 13895 | /* We have already checked cv-qualification at the top of the |
514a1f18 | 13896 | function. */ |
8baddbf1 | 13897 | if (!same_type_ignoring_top_level_qualifiers_p (arg, parm)) |
ca79f85d JM |
13898 | return 1; |
13899 | ||
8d08fdba MS |
13900 | /* As far as unification is concerned, this wins. Later checks |
13901 | will invalidate it if necessary. */ | |
13902 | return 0; | |
13903 | ||
13904 | /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */ | |
bd6dd845 | 13905 | /* Type INTEGER_CST can come from ordinary constant template args. */ |
8d08fdba | 13906 | case INTEGER_CST: |
bd6dd845 MS |
13907 | while (TREE_CODE (arg) == NOP_EXPR) |
13908 | arg = TREE_OPERAND (arg, 0); | |
13909 | ||
8d08fdba MS |
13910 | if (TREE_CODE (arg) != INTEGER_CST) |
13911 | return 1; | |
13912 | return !tree_int_cst_equal (parm, arg); | |
13913 | ||
8d08fdba MS |
13914 | case TREE_VEC: |
13915 | { | |
13916 | int i; | |
13917 | if (TREE_CODE (arg) != TREE_VEC) | |
13918 | return 1; | |
13919 | if (TREE_VEC_LENGTH (parm) != TREE_VEC_LENGTH (arg)) | |
13920 | return 1; | |
0dc09a61 | 13921 | for (i = 0; i < TREE_VEC_LENGTH (parm); ++i) |
830bfa74 | 13922 | if (unify (tparms, targs, |
8d08fdba | 13923 | TREE_VEC_ELT (parm, i), TREE_VEC_ELT (arg, i), |
4393e105 | 13924 | UNIFY_ALLOW_NONE)) |
8d08fdba MS |
13925 | return 1; |
13926 | return 0; | |
13927 | } | |
13928 | ||
8d08fdba | 13929 | case RECORD_TYPE: |
f181d4ae | 13930 | case UNION_TYPE: |
f181d4ae | 13931 | if (TREE_CODE (arg) != TREE_CODE (parm)) |
a4443a08 | 13932 | return 1; |
c8094d83 | 13933 | |
a7a64a77 MM |
13934 | if (TYPE_PTRMEMFUNC_P (parm)) |
13935 | { | |
13936 | if (!TYPE_PTRMEMFUNC_P (arg)) | |
13937 | return 1; | |
13938 | ||
c8094d83 | 13939 | return unify (tparms, targs, |
a7a64a77 MM |
13940 | TYPE_PTRMEMFUNC_FN_TYPE (parm), |
13941 | TYPE_PTRMEMFUNC_FN_TYPE (arg), | |
13942 | strict); | |
13943 | } | |
13944 | ||
5db698f6 | 13945 | if (CLASSTYPE_TEMPLATE_INFO (parm)) |
5566b478 | 13946 | { |
6467930b | 13947 | tree t = NULL_TREE; |
4393e105 | 13948 | |
028d1f20 | 13949 | if (strict_in & UNIFY_ALLOW_DERIVED) |
4393e105 MM |
13950 | { |
13951 | /* First, we try to unify the PARM and ARG directly. */ | |
13952 | t = try_class_unification (tparms, targs, | |
13953 | parm, arg); | |
13954 | ||
13955 | if (!t) | |
13956 | { | |
13957 | /* Fallback to the special case allowed in | |
13958 | [temp.deduct.call]: | |
c8094d83 | 13959 | |
4393e105 MM |
13960 | If P is a class, and P has the form |
13961 | template-id, then A can be a derived class of | |
13962 | the deduced A. Likewise, if P is a pointer to | |
13963 | a class of the form template-id, A can be a | |
13964 | pointer to a derived class pointed to by the | |
13965 | deduced A. */ | |
8d83768f | 13966 | t = get_template_base (tparms, targs, parm, arg); |
4393e105 | 13967 | |
8d83768f | 13968 | if (!t) |
4393e105 MM |
13969 | return 1; |
13970 | } | |
13971 | } | |
c8094d83 MS |
13972 | else if (CLASSTYPE_TEMPLATE_INFO (arg) |
13973 | && (CLASSTYPE_TI_TEMPLATE (parm) | |
9fbf56f7 | 13974 | == CLASSTYPE_TI_TEMPLATE (arg))) |
6df47b06 MM |
13975 | /* Perhaps PARM is something like S<U> and ARG is S<int>. |
13976 | Then, we should unify `int' and `U'. */ | |
6467930b | 13977 | t = arg; |
4393e105 | 13978 | else |
dc957d14 | 13979 | /* There's no chance of unification succeeding. */ |
5566b478 | 13980 | return 1; |
6467930b | 13981 | |
830bfa74 | 13982 | return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm), |
4393e105 | 13983 | CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE); |
5566b478 | 13984 | } |
9edc3913 | 13985 | else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg)) |
5566b478 | 13986 | return 1; |
a4443a08 | 13987 | return 0; |
8d08fdba MS |
13988 | |
13989 | case METHOD_TYPE: | |
8d08fdba | 13990 | case FUNCTION_TYPE: |
c166b898 ILT |
13991 | { |
13992 | unsigned int nargs; | |
13993 | tree *args; | |
13994 | tree a; | |
13995 | unsigned int i; | |
830bfa74 | 13996 | |
c166b898 ILT |
13997 | if (TREE_CODE (arg) != TREE_CODE (parm)) |
13998 | return 1; | |
38d18b1a | 13999 | |
c166b898 ILT |
14000 | /* CV qualifications for methods can never be deduced, they must |
14001 | match exactly. We need to check them explicitly here, | |
14002 | because type_unification_real treats them as any other | |
14003 | cv-qualified parameter. */ | |
14004 | if (TREE_CODE (parm) == METHOD_TYPE | |
14005 | && (!check_cv_quals_for_unify | |
14006 | (UNIFY_ALLOW_NONE, | |
14007 | TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (arg))), | |
14008 | TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (parm)))))) | |
14009 | return 1; | |
14010 | ||
14011 | if (unify (tparms, targs, TREE_TYPE (parm), | |
14012 | TREE_TYPE (arg), UNIFY_ALLOW_NONE)) | |
14013 | return 1; | |
14014 | ||
14015 | nargs = list_length (TYPE_ARG_TYPES (arg)); | |
14016 | args = XALLOCAVEC (tree, nargs); | |
14017 | for (a = TYPE_ARG_TYPES (arg), i = 0; | |
14018 | a != NULL_TREE && a != void_list_node; | |
14019 | a = TREE_CHAIN (a), ++i) | |
14020 | args[i] = TREE_VALUE (a); | |
14021 | nargs = i; | |
14022 | ||
14023 | return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm), | |
14024 | args, nargs, 1, DEDUCE_EXACT, | |
14025 | LOOKUP_NORMAL); | |
14026 | } | |
a4443a08 MS |
14027 | |
14028 | case OFFSET_TYPE: | |
9804209d | 14029 | /* Unify a pointer to member with a pointer to member function, which |
0cbd7506 | 14030 | deduces the type of the member as a function type. */ |
9804209d | 14031 | if (TYPE_PTRMEMFUNC_P (arg)) |
0cbd7506 MS |
14032 | { |
14033 | tree method_type; | |
14034 | tree fntype; | |
14035 | cp_cv_quals cv_quals; | |
14036 | ||
14037 | /* Check top-level cv qualifiers */ | |
14038 | if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm)) | |
14039 | return 1; | |
14040 | ||
14041 | if (unify (tparms, targs, TYPE_OFFSET_BASETYPE (parm), | |
14042 | TYPE_PTRMEMFUNC_OBJECT_TYPE (arg), UNIFY_ALLOW_NONE)) | |
14043 | return 1; | |
14044 | ||
14045 | /* Determine the type of the function we are unifying against. */ | |
14046 | method_type = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (arg)); | |
14047 | fntype = | |
14048 | build_function_type (TREE_TYPE (method_type), | |
14049 | TREE_CHAIN (TYPE_ARG_TYPES (method_type))); | |
14050 | ||
14051 | /* Extract the cv-qualifiers of the member function from the | |
14052 | implicit object parameter and place them on the function | |
14053 | type to be restored later. */ | |
14054 | cv_quals = | |
14055 | cp_type_quals(TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (method_type)))); | |
14056 | fntype = build_qualified_type (fntype, cv_quals); | |
14057 | return unify (tparms, targs, TREE_TYPE (parm), fntype, strict); | |
14058 | } | |
9804209d | 14059 | |
a4443a08 MS |
14060 | if (TREE_CODE (arg) != OFFSET_TYPE) |
14061 | return 1; | |
830bfa74 | 14062 | if (unify (tparms, targs, TYPE_OFFSET_BASETYPE (parm), |
4393e105 | 14063 | TYPE_OFFSET_BASETYPE (arg), UNIFY_ALLOW_NONE)) |
a4443a08 | 14064 | return 1; |
830bfa74 | 14065 | return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg), |
4393e105 | 14066 | strict); |
a4443a08 | 14067 | |
f62dbf03 | 14068 | case CONST_DECL: |
a723baf1 MM |
14069 | if (DECL_TEMPLATE_PARM_P (parm)) |
14070 | return unify (tparms, targs, DECL_INITIAL (parm), arg, strict); | |
c8094d83 | 14071 | if (arg != integral_constant_value (parm)) |
f62dbf03 JM |
14072 | return 1; |
14073 | return 0; | |
14074 | ||
28e8f3a0 | 14075 | case FIELD_DECL: |
027905b4 KL |
14076 | case TEMPLATE_DECL: |
14077 | /* Matched cases are handled by the ARG == PARM test above. */ | |
14078 | return 1; | |
14079 | ||
5d80a306 DG |
14080 | case TYPE_ARGUMENT_PACK: |
14081 | case NONTYPE_ARGUMENT_PACK: | |
14082 | { | |
14083 | tree packed_parms = ARGUMENT_PACK_ARGS (parm); | |
14084 | tree packed_args = ARGUMENT_PACK_ARGS (arg); | |
14085 | int i, len = TREE_VEC_LENGTH (packed_parms); | |
14086 | int argslen = TREE_VEC_LENGTH (packed_args); | |
14087 | int parm_variadic_p = 0; | |
14088 | ||
db60ff18 DG |
14089 | for (i = 0; i < len; ++i) |
14090 | { | |
14091 | if (PACK_EXPANSION_P (TREE_VEC_ELT (packed_parms, i))) | |
14092 | { | |
14093 | if (i == len - 1) | |
14094 | /* We can unify against something with a trailing | |
14095 | parameter pack. */ | |
14096 | parm_variadic_p = 1; | |
14097 | else | |
14098 | /* Since there is something following the pack | |
14099 | expansion, we cannot unify this template argument | |
14100 | list. */ | |
14101 | return 0; | |
14102 | } | |
14103 | } | |
14104 | ||
5d80a306 DG |
14105 | |
14106 | /* If we don't have enough arguments to satisfy the parameters | |
14107 | (not counting the pack expression at the end), or we have | |
14108 | too many arguments for a parameter list that doesn't end in | |
14109 | a pack expression, we can't unify. */ | |
14110 | if (argslen < (len - parm_variadic_p) | |
14111 | || (argslen > len && !parm_variadic_p)) | |
14112 | return 1; | |
14113 | ||
14114 | /* Unify all of the parameters that precede the (optional) | |
14115 | pack expression. */ | |
14116 | for (i = 0; i < len - parm_variadic_p; ++i) | |
14117 | { | |
14118 | if (unify (tparms, targs, TREE_VEC_ELT (packed_parms, i), | |
14119 | TREE_VEC_ELT (packed_args, i), strict)) | |
14120 | return 1; | |
14121 | } | |
14122 | ||
14123 | if (parm_variadic_p) | |
14124 | return unify_pack_expansion (tparms, targs, | |
14125 | packed_parms, packed_args, | |
14126 | strict, /*call_args_p=*/false, | |
14127 | /*subr=*/false); | |
14128 | return 0; | |
14129 | } | |
14130 | ||
14131 | break; | |
14132 | ||
3ad6a8e1 DG |
14133 | case TYPEOF_TYPE: |
14134 | case DECLTYPE_TYPE: | |
14135 | /* Cannot deduce anything from TYPEOF_TYPE or DECLTYPE_TYPE | |
14136 | nodes. */ | |
14137 | return 0; | |
14138 | ||
1ad8aeeb DG |
14139 | case ERROR_MARK: |
14140 | /* Unification fails if we hit an error node. */ | |
14141 | return 1; | |
14142 | ||
8d08fdba | 14143 | default: |
6615c446 | 14144 | gcc_assert (EXPR_P (parm)); |
c8094d83 | 14145 | |
98ddffc1 | 14146 | /* We must be looking at an expression. This can happen with |
c8094d83 MS |
14147 | something like: |
14148 | ||
98ddffc1 NS |
14149 | template <int I> |
14150 | void foo(S<I>, S<I + 2>); | |
050367a3 | 14151 | |
98ddffc1 | 14152 | This is a "nondeduced context": |
050367a3 | 14153 | |
98ddffc1 | 14154 | [deduct.type] |
c8094d83 | 14155 | |
98ddffc1 | 14156 | The nondeduced contexts are: |
050367a3 | 14157 | |
98ddffc1 NS |
14158 | --A type that is a template-id in which one or more of |
14159 | the template-arguments is an expression that references | |
c8094d83 | 14160 | a template-parameter. |
050367a3 | 14161 | |
98ddffc1 NS |
14162 | In these cases, we assume deduction succeeded, but don't |
14163 | actually infer any unifications. */ | |
74601d7c | 14164 | |
98ddffc1 NS |
14165 | if (!uses_template_parms (parm) |
14166 | && !template_args_equal (parm, arg)) | |
14167 | return 1; | |
14168 | else | |
14169 | return 0; | |
8d08fdba MS |
14170 | } |
14171 | } | |
8d08fdba | 14172 | \f |
4684cd27 MM |
14173 | /* Note that DECL can be defined in this translation unit, if |
14174 | required. */ | |
14175 | ||
14176 | static void | |
14177 | mark_definable (tree decl) | |
14178 | { | |
14179 | tree clone; | |
14180 | DECL_NOT_REALLY_EXTERN (decl) = 1; | |
14181 | FOR_EACH_CLONE (clone, decl) | |
14182 | DECL_NOT_REALLY_EXTERN (clone) = 1; | |
14183 | } | |
14184 | ||
03d0f4af | 14185 | /* Called if RESULT is explicitly instantiated, or is a member of an |
4684cd27 | 14186 | explicitly instantiated class. */ |
03d0f4af | 14187 | |
faae18ab | 14188 | void |
3a978d72 | 14189 | mark_decl_instantiated (tree result, int extern_p) |
faae18ab | 14190 | { |
415c974c | 14191 | SET_DECL_EXPLICIT_INSTANTIATION (result); |
3ae18eaf | 14192 | |
1f6f0cb6 MM |
14193 | /* If this entity has already been written out, it's too late to |
14194 | make any modifications. */ | |
14195 | if (TREE_ASM_WRITTEN (result)) | |
14196 | return; | |
14197 | ||
14198 | if (TREE_CODE (result) != FUNCTION_DECL) | |
14199 | /* The TREE_PUBLIC flag for function declarations will have been | |
14200 | set correctly by tsubst. */ | |
14201 | TREE_PUBLIC (result) = 1; | |
14202 | ||
346eeb15 JM |
14203 | /* This might have been set by an earlier implicit instantiation. */ |
14204 | DECL_COMDAT (result) = 0; | |
14205 | ||
4684cd27 MM |
14206 | if (extern_p) |
14207 | DECL_NOT_REALLY_EXTERN (result) = 0; | |
14208 | else | |
faae18ab | 14209 | { |
4684cd27 | 14210 | mark_definable (result); |
1a408d07 JM |
14211 | /* Always make artificials weak. */ |
14212 | if (DECL_ARTIFICIAL (result) && flag_weak) | |
14213 | comdat_linkage (result); | |
a7d87521 JM |
14214 | /* For WIN32 we also want to put explicit instantiations in |
14215 | linkonce sections. */ | |
1a408d07 | 14216 | else if (TREE_PUBLIC (result)) |
b385c841 | 14217 | maybe_make_one_only (result); |
faae18ab | 14218 | } |
c8094d83 | 14219 | |
4684cd27 MM |
14220 | /* If EXTERN_P, then this function will not be emitted -- unless |
14221 | followed by an explicit instantiation, at which point its linkage | |
14222 | will be adjusted. If !EXTERN_P, then this function will be | |
14223 | emitted here. In neither circumstance do we want | |
14224 | import_export_decl to adjust the linkage. */ | |
c8094d83 | 14225 | DECL_INTERFACE_KNOWN (result) = 1; |
faae18ab MS |
14226 | } |
14227 | ||
e5214479 | 14228 | /* Given two function templates PAT1 and PAT2, return: |
6467930b MS |
14229 | |
14230 | 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order]. | |
14231 | -1 if PAT2 is more specialized than PAT1. | |
e5214479 JM |
14232 | 0 if neither is more specialized. |
14233 | ||
dda04398 NS |
14234 | LEN indicates the number of parameters we should consider |
14235 | (defaulted parameters should not be considered). | |
14236 | ||
14237 | The 1998 std underspecified function template partial ordering, and | |
14238 | DR214 addresses the issue. We take pairs of arguments, one from | |
c51940a2 | 14239 | each of the templates, and deduce them against each other. One of |
dda04398 NS |
14240 | the templates will be more specialized if all the *other* |
14241 | template's arguments deduce against its arguments and at least one | |
14242 | of its arguments *does* *not* deduce against the other template's | |
14243 | corresponding argument. Deduction is done as for class templates. | |
14244 | The arguments used in deduction have reference and top level cv | |
14245 | qualifiers removed. Iff both arguments were originally reference | |
14246 | types *and* deduction succeeds in both directions, the template | |
14247 | with the more cv-qualified argument wins for that pairing (if | |
14248 | neither is more cv-qualified, they both are equal). Unlike regular | |
14249 | deduction, after all the arguments have been deduced in this way, | |
14250 | we do *not* verify the deduced template argument values can be | |
14251 | substituted into non-deduced contexts, nor do we have to verify | |
14252 | that all template arguments have been deduced. */ | |
c8094d83 | 14253 | |
6467930b | 14254 | int |
dda04398 NS |
14255 | more_specialized_fn (tree pat1, tree pat2, int len) |
14256 | { | |
14257 | tree decl1 = DECL_TEMPLATE_RESULT (pat1); | |
14258 | tree decl2 = DECL_TEMPLATE_RESULT (pat2); | |
14259 | tree targs1 = make_tree_vec (DECL_NTPARMS (pat1)); | |
14260 | tree targs2 = make_tree_vec (DECL_NTPARMS (pat2)); | |
14261 | tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1); | |
14262 | tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2); | |
14263 | tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1)); | |
14264 | tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2)); | |
14265 | int better1 = 0; | |
14266 | int better2 = 0; | |
3db45ab5 | 14267 | |
48884537 NS |
14268 | /* Remove the this parameter from non-static member functions. If |
14269 | one is a non-static member function and the other is not a static | |
14270 | member function, remove the first parameter from that function | |
14271 | also. This situation occurs for operator functions where we | |
14272 | locate both a member function (with this pointer) and non-member | |
14273 | operator (with explicit first operand). */ | |
dda04398 | 14274 | if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1)) |
48884537 NS |
14275 | { |
14276 | len--; /* LEN is the number of significant arguments for DECL1 */ | |
14277 | args1 = TREE_CHAIN (args1); | |
14278 | if (!DECL_STATIC_FUNCTION_P (decl2)) | |
14279 | args2 = TREE_CHAIN (args2); | |
14280 | } | |
14281 | else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2)) | |
14282 | { | |
14283 | args2 = TREE_CHAIN (args2); | |
14284 | if (!DECL_STATIC_FUNCTION_P (decl1)) | |
14285 | { | |
14286 | len--; | |
14287 | args1 = TREE_CHAIN (args1); | |
14288 | } | |
14289 | } | |
3db45ab5 | 14290 | |
ee307009 NS |
14291 | /* If only one is a conversion operator, they are unordered. */ |
14292 | if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2)) | |
14293 | return 0; | |
c8094d83 | 14294 | |
dda04398 NS |
14295 | /* Consider the return type for a conversion function */ |
14296 | if (DECL_CONV_FN_P (decl1)) | |
14297 | { | |
dda04398 NS |
14298 | args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1); |
14299 | args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2); | |
14300 | len++; | |
14301 | } | |
c8094d83 | 14302 | |
dda04398 | 14303 | processing_template_decl++; |
c8094d83 | 14304 | |
56b1a554 JJ |
14305 | while (len-- |
14306 | /* Stop when an ellipsis is seen. */ | |
14307 | && args1 != NULL_TREE && args2 != NULL_TREE) | |
dda04398 NS |
14308 | { |
14309 | tree arg1 = TREE_VALUE (args1); | |
14310 | tree arg2 = TREE_VALUE (args2); | |
14311 | int deduce1, deduce2; | |
14312 | int quals1 = -1; | |
14313 | int quals2 = -1; | |
6467930b | 14314 | |
5d80a306 DG |
14315 | if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION |
14316 | && TREE_CODE (arg2) == TYPE_PACK_EXPANSION) | |
14317 | { | |
14318 | /* When both arguments are pack expansions, we need only | |
14319 | unify the patterns themselves. */ | |
14320 | arg1 = PACK_EXPANSION_PATTERN (arg1); | |
14321 | arg2 = PACK_EXPANSION_PATTERN (arg2); | |
14322 | ||
14323 | /* This is the last comparison we need to do. */ | |
14324 | len = 0; | |
14325 | } | |
14326 | ||
dda04398 NS |
14327 | if (TREE_CODE (arg1) == REFERENCE_TYPE) |
14328 | { | |
14329 | arg1 = TREE_TYPE (arg1); | |
14330 | quals1 = cp_type_quals (arg1); | |
14331 | } | |
c8094d83 | 14332 | |
dda04398 NS |
14333 | if (TREE_CODE (arg2) == REFERENCE_TYPE) |
14334 | { | |
14335 | arg2 = TREE_TYPE (arg2); | |
14336 | quals2 = cp_type_quals (arg2); | |
14337 | } | |
6467930b | 14338 | |
dda04398 NS |
14339 | if ((quals1 < 0) != (quals2 < 0)) |
14340 | { | |
14341 | /* Only of the args is a reference, see if we should apply | |
14342 | array/function pointer decay to it. This is not part of | |
14343 | DR214, but is, IMHO, consistent with the deduction rules | |
14344 | for the function call itself, and with our earlier | |
14345 | implementation of the underspecified partial ordering | |
14346 | rules. (nathan). */ | |
14347 | if (quals1 >= 0) | |
14348 | { | |
14349 | switch (TREE_CODE (arg1)) | |
14350 | { | |
14351 | case ARRAY_TYPE: | |
14352 | arg1 = TREE_TYPE (arg1); | |
14353 | /* FALLTHROUGH. */ | |
14354 | case FUNCTION_TYPE: | |
14355 | arg1 = build_pointer_type (arg1); | |
14356 | break; | |
c8094d83 | 14357 | |
dda04398 NS |
14358 | default: |
14359 | break; | |
14360 | } | |
14361 | } | |
14362 | else | |
14363 | { | |
14364 | switch (TREE_CODE (arg2)) | |
14365 | { | |
14366 | case ARRAY_TYPE: | |
14367 | arg2 = TREE_TYPE (arg2); | |
14368 | /* FALLTHROUGH. */ | |
14369 | case FUNCTION_TYPE: | |
14370 | arg2 = build_pointer_type (arg2); | |
14371 | break; | |
c8094d83 | 14372 | |
dda04398 NS |
14373 | default: |
14374 | break; | |
14375 | } | |
14376 | } | |
14377 | } | |
c8094d83 | 14378 | |
dda04398 NS |
14379 | arg1 = TYPE_MAIN_VARIANT (arg1); |
14380 | arg2 = TYPE_MAIN_VARIANT (arg2); | |
c8094d83 | 14381 | |
5d80a306 DG |
14382 | if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION) |
14383 | { | |
b1d7b1c0 | 14384 | int i, len2 = list_length (args2); |
5d80a306 DG |
14385 | tree parmvec = make_tree_vec (1); |
14386 | tree argvec = make_tree_vec (len2); | |
14387 | tree ta = args2; | |
14388 | ||
14389 | /* Setup the parameter vector, which contains only ARG1. */ | |
14390 | TREE_VEC_ELT (parmvec, 0) = arg1; | |
14391 | ||
14392 | /* Setup the argument vector, which contains the remaining | |
14393 | arguments. */ | |
14394 | for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta)) | |
14395 | TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta); | |
14396 | ||
14397 | deduce1 = !unify_pack_expansion (tparms1, targs1, parmvec, | |
14398 | argvec, UNIFY_ALLOW_NONE, | |
14399 | /*call_args_p=*/false, | |
14400 | /*subr=*/0); | |
14401 | ||
14402 | /* We cannot deduce in the other direction, because ARG1 is | |
14403 | a pack expansion but ARG2 is not. */ | |
14404 | deduce2 = 0; | |
14405 | } | |
14406 | else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION) | |
14407 | { | |
b1d7b1c0 | 14408 | int i, len1 = list_length (args1); |
5d80a306 DG |
14409 | tree parmvec = make_tree_vec (1); |
14410 | tree argvec = make_tree_vec (len1); | |
14411 | tree ta = args1; | |
14412 | ||
14413 | /* Setup the parameter vector, which contains only ARG1. */ | |
14414 | TREE_VEC_ELT (parmvec, 0) = arg2; | |
14415 | ||
14416 | /* Setup the argument vector, which contains the remaining | |
14417 | arguments. */ | |
14418 | for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta)) | |
14419 | TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta); | |
14420 | ||
14421 | deduce2 = !unify_pack_expansion (tparms2, targs2, parmvec, | |
14422 | argvec, UNIFY_ALLOW_NONE, | |
14423 | /*call_args_p=*/false, | |
14424 | /*subr=*/0); | |
14425 | ||
14426 | /* We cannot deduce in the other direction, because ARG2 is | |
14427 | a pack expansion but ARG1 is not.*/ | |
14428 | deduce1 = 0; | |
14429 | } | |
14430 | ||
14431 | else | |
14432 | { | |
14433 | /* The normal case, where neither argument is a pack | |
14434 | expansion. */ | |
14435 | deduce1 = !unify (tparms1, targs1, arg1, arg2, UNIFY_ALLOW_NONE); | |
14436 | deduce2 = !unify (tparms2, targs2, arg2, arg1, UNIFY_ALLOW_NONE); | |
14437 | } | |
dda04398 NS |
14438 | |
14439 | if (!deduce1) | |
14440 | better2 = -1; | |
14441 | if (!deduce2) | |
14442 | better1 = -1; | |
14443 | if (better1 < 0 && better2 < 0) | |
14444 | /* We've failed to deduce something in either direction. | |
14445 | These must be unordered. */ | |
14446 | break; | |
c8094d83 | 14447 | |
dda04398 NS |
14448 | if (deduce1 && deduce2 && quals1 >= 0 && quals2 >= 0) |
14449 | { | |
14450 | /* Deduces in both directions, see if quals can | |
14451 | disambiguate. Pretend the worse one failed to deduce. */ | |
14452 | if ((quals1 & quals2) == quals2) | |
14453 | deduce1 = 0; | |
14454 | if ((quals1 & quals2) == quals1) | |
14455 | deduce2 = 0; | |
14456 | } | |
14457 | if (deduce1 && !deduce2 && !better2) | |
14458 | better2 = 1; | |
14459 | if (deduce2 && !deduce1 && !better1) | |
14460 | better1 = 1; | |
c8094d83 | 14461 | |
5d80a306 DG |
14462 | if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION |
14463 | || TREE_CODE (arg2) == TYPE_PACK_EXPANSION) | |
14464 | /* We have already processed all of the arguments in our | |
14465 | handing of the pack expansion type. */ | |
14466 | len = 0; | |
14467 | ||
dda04398 NS |
14468 | args1 = TREE_CHAIN (args1); |
14469 | args2 = TREE_CHAIN (args2); | |
14470 | } | |
14471 | ||
14472 | processing_template_decl--; | |
14473 | ||
5d80a306 DG |
14474 | /* All things being equal, if the next argument is a pack expansion |
14475 | for one function but not for the other, prefer the | |
14476 | non-variadic function. */ | |
14477 | if ((better1 > 0) - (better2 > 0) == 0 | |
14478 | && args1 && TREE_VALUE (args1) | |
14479 | && args2 && TREE_VALUE (args2)) | |
14480 | { | |
14481 | if (TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION) | |
14482 | return TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION ? 0 : -1; | |
14483 | else if (TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION) | |
14484 | return 1; | |
14485 | } | |
14486 | ||
dda04398 | 14487 | return (better1 > 0) - (better2 > 0); |
73aad9b9 | 14488 | } |
6467930b | 14489 | |
916b63c3 | 14490 | /* Determine which of two partial specializations is more specialized. |
6467930b | 14491 | |
916b63c3 MM |
14492 | PAT1 is a TREE_LIST whose TREE_TYPE is the _TYPE node corresponding |
14493 | to the first partial specialization. The TREE_VALUE is the | |
14494 | innermost set of template parameters for the partial | |
14495 | specialization. PAT2 is similar, but for the second template. | |
14496 | ||
14497 | Return 1 if the first partial specialization is more specialized; | |
14498 | -1 if the second is more specialized; 0 if neither is more | |
14499 | specialized. | |
55ece1b3 | 14500 | |
916b63c3 | 14501 | See [temp.class.order] for information about determining which of |
3db45ab5 | 14502 | two templates is more specialized. */ |
c8094d83 | 14503 | |
b5791fdc | 14504 | static int |
916b63c3 | 14505 | more_specialized_class (tree pat1, tree pat2) |
73aad9b9 JM |
14506 | { |
14507 | tree targs; | |
916b63c3 | 14508 | tree tmpl1, tmpl2; |
73aad9b9 | 14509 | int winner = 0; |
5d80a306 | 14510 | bool any_deductions = false; |
73aad9b9 | 14511 | |
916b63c3 MM |
14512 | tmpl1 = TREE_TYPE (pat1); |
14513 | tmpl2 = TREE_TYPE (pat2); | |
14514 | ||
c8094d83 | 14515 | /* Just like what happens for functions, if we are ordering between |
baa49730 GB |
14516 | different class template specializations, we may encounter dependent |
14517 | types in the arguments, and we need our dependency check functions | |
14518 | to behave correctly. */ | |
14519 | ++processing_template_decl; | |
3db45ab5 | 14520 | targs = get_class_bindings (TREE_VALUE (pat1), |
916b63c3 MM |
14521 | CLASSTYPE_TI_ARGS (tmpl1), |
14522 | CLASSTYPE_TI_ARGS (tmpl2)); | |
73aad9b9 | 14523 | if (targs) |
5d80a306 DG |
14524 | { |
14525 | --winner; | |
14526 | any_deductions = true; | |
14527 | } | |
73aad9b9 | 14528 | |
3db45ab5 | 14529 | targs = get_class_bindings (TREE_VALUE (pat2), |
916b63c3 MM |
14530 | CLASSTYPE_TI_ARGS (tmpl2), |
14531 | CLASSTYPE_TI_ARGS (tmpl1)); | |
73aad9b9 | 14532 | if (targs) |
5d80a306 DG |
14533 | { |
14534 | ++winner; | |
14535 | any_deductions = true; | |
14536 | } | |
baa49730 | 14537 | --processing_template_decl; |
6467930b | 14538 | |
5d80a306 DG |
14539 | /* In the case of a tie where at least one of the class templates |
14540 | has a parameter pack at the end, the template with the most | |
14541 | non-packed parameters wins. */ | |
14542 | if (winner == 0 | |
14543 | && any_deductions | |
14544 | && (template_args_variadic_p (TREE_PURPOSE (pat1)) | |
14545 | || template_args_variadic_p (TREE_PURPOSE (pat2)))) | |
14546 | { | |
14547 | tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1)); | |
14548 | tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2)); | |
14549 | int len1 = TREE_VEC_LENGTH (args1); | |
14550 | int len2 = TREE_VEC_LENGTH (args2); | |
14551 | ||
14552 | /* We don't count the pack expansion at the end. */ | |
14553 | if (template_args_variadic_p (TREE_PURPOSE (pat1))) | |
14554 | --len1; | |
14555 | if (template_args_variadic_p (TREE_PURPOSE (pat2))) | |
14556 | --len2; | |
14557 | ||
14558 | if (len1 > len2) | |
14559 | return 1; | |
14560 | else if (len1 < len2) | |
14561 | return -1; | |
14562 | } | |
14563 | ||
6467930b MS |
14564 | return winner; |
14565 | } | |
73aad9b9 JM |
14566 | |
14567 | /* Return the template arguments that will produce the function signature | |
e1467ff2 | 14568 | DECL from the function template FN, with the explicit template |
a34d3336 | 14569 | arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must |
4393e105 | 14570 | also match. Return NULL_TREE if no satisfactory arguments could be |
a34d3336 | 14571 | found. */ |
c8094d83 | 14572 | |
76b9a14d | 14573 | static tree |
a34d3336 | 14574 | get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype) |
73aad9b9 | 14575 | { |
98c1c668 | 14576 | int ntparms = DECL_NTPARMS (fn); |
f31c0a32 | 14577 | tree targs = make_tree_vec (ntparms); |
4393e105 | 14578 | tree decl_type; |
03017874 | 14579 | tree decl_arg_types; |
c166b898 ILT |
14580 | tree *args; |
14581 | unsigned int nargs, ix; | |
14582 | tree arg; | |
98c1c668 | 14583 | |
4393e105 MM |
14584 | /* Substitute the explicit template arguments into the type of DECL. |
14585 | The call to fn_type_unification will handle substitution into the | |
14586 | FN. */ | |
14587 | decl_type = TREE_TYPE (decl); | |
14588 | if (explicit_args && uses_template_parms (decl_type)) | |
14589 | { | |
14590 | tree tmpl; | |
14591 | tree converted_args; | |
14592 | ||
14593 | if (DECL_TEMPLATE_INFO (decl)) | |
14594 | tmpl = DECL_TI_TEMPLATE (decl); | |
14595 | else | |
0e339752 | 14596 | /* We can get here for some invalid specializations. */ |
4393e105 MM |
14597 | return NULL_TREE; |
14598 | ||
14599 | converted_args | |
e7e93965 MM |
14600 | = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl), |
14601 | explicit_args, NULL_TREE, | |
3db45ab5 | 14602 | tf_none, |
e7e93965 MM |
14603 | /*require_all_args=*/false, |
14604 | /*use_default_args=*/false); | |
4393e105 MM |
14605 | if (converted_args == error_mark_node) |
14606 | return NULL_TREE; | |
c8094d83 MS |
14607 | |
14608 | decl_type = tsubst (decl_type, converted_args, tf_none, NULL_TREE); | |
4393e105 MM |
14609 | if (decl_type == error_mark_node) |
14610 | return NULL_TREE; | |
14611 | } | |
14612 | ||
e5214479 | 14613 | /* Never do unification on the 'this' parameter. */ |
ba139ba8 MM |
14614 | decl_arg_types = skip_artificial_parms_for (decl, |
14615 | TYPE_ARG_TYPES (decl_type)); | |
d7684f2d | 14616 | |
c166b898 ILT |
14617 | nargs = list_length (decl_arg_types); |
14618 | args = XALLOCAVEC (tree, nargs); | |
14619 | for (arg = decl_arg_types, ix = 0; | |
14620 | arg != NULL_TREE && arg != void_list_node; | |
14621 | arg = TREE_CHAIN (arg), ++ix) | |
14622 | args[ix] = TREE_VALUE (arg); | |
14623 | ||
c8094d83 | 14624 | if (fn_type_unification (fn, explicit_args, targs, |
c166b898 | 14625 | args, ix, |
8d3631f8 | 14626 | (check_rettype || DECL_CONV_FN_P (fn) |
0cbd7506 | 14627 | ? TREE_TYPE (decl_type) : NULL_TREE), |
30f86ec3 | 14628 | DEDUCE_EXACT, LOOKUP_NORMAL)) |
76b9a14d JM |
14629 | return NULL_TREE; |
14630 | ||
76b9a14d JM |
14631 | return targs; |
14632 | } | |
14633 | ||
36a117a5 MM |
14634 | /* Return the innermost template arguments that, when applied to a |
14635 | template specialization whose innermost template parameters are | |
d0940d56 | 14636 | TPARMS, and whose specialization arguments are SPEC_ARGS, yield the |
c8094d83 | 14637 | ARGS. |
36a117a5 MM |
14638 | |
14639 | For example, suppose we have: | |
14640 | ||
14641 | template <class T, class U> struct S {}; | |
14642 | template <class T> struct S<T*, int> {}; | |
14643 | ||
14644 | Then, suppose we want to get `S<double*, int>'. The TPARMS will be | |
916b63c3 | 14645 | {T}, the SPEC_ARGS will be {T*, int} and the ARGS will be {double*, |
36a117a5 MM |
14646 | int}. The resulting vector will be {double}, indicating that `T' |
14647 | is bound to `double'. */ | |
14648 | ||
bd6dd845 | 14649 | static tree |
916b63c3 | 14650 | get_class_bindings (tree tparms, tree spec_args, tree args) |
73aad9b9 | 14651 | { |
3b3ba9f0 | 14652 | int i, ntparms = TREE_VEC_LENGTH (tparms); |
916b63c3 MM |
14653 | tree deduced_args; |
14654 | tree innermost_deduced_args; | |
73aad9b9 | 14655 | |
916b63c3 MM |
14656 | innermost_deduced_args = make_tree_vec (ntparms); |
14657 | if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args)) | |
14658 | { | |
14659 | deduced_args = copy_node (args); | |
14660 | SET_TMPL_ARGS_LEVEL (deduced_args, | |
14661 | TMPL_ARGS_DEPTH (deduced_args), | |
14662 | innermost_deduced_args); | |
14663 | } | |
14664 | else | |
3db45ab5 | 14665 | deduced_args = innermost_deduced_args; |
916b63c3 | 14666 | |
3db45ab5 MS |
14667 | if (unify (tparms, deduced_args, |
14668 | INNERMOST_TEMPLATE_ARGS (spec_args), | |
14669 | INNERMOST_TEMPLATE_ARGS (args), | |
0cbd7506 | 14670 | UNIFY_ALLOW_NONE)) |
fcfb9f96 | 14671 | return NULL_TREE; |
73aad9b9 JM |
14672 | |
14673 | for (i = 0; i < ntparms; ++i) | |
916b63c3 | 14674 | if (! TREE_VEC_ELT (innermost_deduced_args, i)) |
73aad9b9 JM |
14675 | return NULL_TREE; |
14676 | ||
916b63c3 MM |
14677 | /* Verify that nondeduced template arguments agree with the type |
14678 | obtained from argument deduction. | |
3db45ab5 | 14679 | |
916b63c3 MM |
14680 | For example: |
14681 | ||
14682 | struct A { typedef int X; }; | |
14683 | template <class T, class U> struct C {}; | |
14684 | template <class T> struct C<T, typename T::X> {}; | |
14685 | ||
14686 | Then with the instantiation `C<A, int>', we can deduce that | |
14687 | `T' is `A' but unify () does not check whether `typename T::X' | |
14688 | is `int'. */ | |
14689 | spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE); | |
14690 | if (spec_args == error_mark_node | |
14691 | /* We only need to check the innermost arguments; the other | |
14692 | arguments will always agree. */ | |
14693 | || !comp_template_args (INNERMOST_TEMPLATE_ARGS (spec_args), | |
14694 | INNERMOST_TEMPLATE_ARGS (args))) | |
74601d7c KL |
14695 | return NULL_TREE; |
14696 | ||
4cf36211 DG |
14697 | /* Now that we have bindings for all of the template arguments, |
14698 | ensure that the arguments deduced for the template template | |
14699 | parameters have compatible template parameter lists. See the use | |
14700 | of template_template_parm_bindings_ok_p in fn_type_unification | |
14701 | for more information. */ | |
14702 | if (!template_template_parm_bindings_ok_p (tparms, deduced_args)) | |
14703 | return NULL_TREE; | |
14704 | ||
916b63c3 | 14705 | return deduced_args; |
73aad9b9 JM |
14706 | } |
14707 | ||
7ca383e6 MM |
14708 | /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL. |
14709 | Return the TREE_LIST node with the most specialized template, if | |
14710 | any. If there is no most specialized template, the error_mark_node | |
14711 | is returned. | |
14712 | ||
14713 | Note that this function does not look at, or modify, the | |
14714 | TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node | |
14715 | returned is one of the elements of INSTANTIATIONS, callers may | |
14716 | store information in the TREE_PURPOSE or TREE_TYPE of the nodes, | |
14717 | and retrieve it from the value returned. */ | |
73aad9b9 JM |
14718 | |
14719 | tree | |
7ca383e6 | 14720 | most_specialized_instantiation (tree templates) |
73aad9b9 | 14721 | { |
104bf76a | 14722 | tree fn, champ; |
73aad9b9 | 14723 | |
dda04398 | 14724 | ++processing_template_decl; |
c8094d83 | 14725 | |
7ca383e6 MM |
14726 | champ = templates; |
14727 | for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn)) | |
73aad9b9 | 14728 | { |
dda04398 | 14729 | int fate = 0; |
c8094d83 | 14730 | |
a34d3336 NS |
14731 | if (get_bindings (TREE_VALUE (champ), |
14732 | DECL_TEMPLATE_RESULT (TREE_VALUE (fn)), | |
14733 | NULL_TREE, /*check_ret=*/false)) | |
dda04398 NS |
14734 | fate--; |
14735 | ||
a34d3336 NS |
14736 | if (get_bindings (TREE_VALUE (fn), |
14737 | DECL_TEMPLATE_RESULT (TREE_VALUE (champ)), | |
14738 | NULL_TREE, /*check_ret=*/false)) | |
dda04398 | 14739 | fate++; |
c8094d83 | 14740 | |
8ca4bf25 MM |
14741 | if (fate == -1) |
14742 | champ = fn; | |
14743 | else if (!fate) | |
73aad9b9 | 14744 | { |
8ca4bf25 MM |
14745 | /* Equally specialized, move to next function. If there |
14746 | is no next function, nothing's most specialized. */ | |
14747 | fn = TREE_CHAIN (fn); | |
7ca383e6 | 14748 | champ = fn; |
8ca4bf25 MM |
14749 | if (!fn) |
14750 | break; | |
73aad9b9 JM |
14751 | } |
14752 | } | |
c8094d83 | 14753 | |
dda04398 NS |
14754 | if (champ) |
14755 | /* Now verify that champ is better than everything earlier in the | |
14756 | instantiation list. */ | |
7ca383e6 | 14757 | for (fn = templates; fn != champ; fn = TREE_CHAIN (fn)) |
a34d3336 NS |
14758 | if (get_bindings (TREE_VALUE (champ), |
14759 | DECL_TEMPLATE_RESULT (TREE_VALUE (fn)), | |
14760 | NULL_TREE, /*check_ret=*/false) | |
14761 | || !get_bindings (TREE_VALUE (fn), | |
14762 | DECL_TEMPLATE_RESULT (TREE_VALUE (champ)), | |
14763 | NULL_TREE, /*check_ret=*/false)) | |
dda04398 NS |
14764 | { |
14765 | champ = NULL_TREE; | |
14766 | break; | |
14767 | } | |
c8094d83 | 14768 | |
dda04398 | 14769 | processing_template_decl--; |
c8094d83 | 14770 | |
dda04398 NS |
14771 | if (!champ) |
14772 | return error_mark_node; | |
73aad9b9 | 14773 | |
7ca383e6 | 14774 | return champ; |
73aad9b9 JM |
14775 | } |
14776 | ||
36a117a5 | 14777 | /* If DECL is a specialization of some template, return the most |
f9a7ae04 MM |
14778 | general such template. Otherwise, returns NULL_TREE. |
14779 | ||
14780 | For example, given: | |
36a117a5 MM |
14781 | |
14782 | template <class T> struct S { template <class U> void f(U); }; | |
14783 | ||
14784 | if TMPL is `template <class U> void S<int>::f(U)' this will return | |
14785 | the full template. This function will not trace past partial | |
14786 | specializations, however. For example, given in addition: | |
14787 | ||
14788 | template <class T> struct S<T*> { template <class U> void f(U); }; | |
14789 | ||
14790 | if TMPL is `template <class U> void S<int*>::f(U)' this will return | |
14791 | `template <class T> template <class U> S<T*>::f(U)'. */ | |
73aad9b9 | 14792 | |
612c671a | 14793 | tree |
3a978d72 | 14794 | most_general_template (tree decl) |
73aad9b9 | 14795 | { |
f9a7ae04 MM |
14796 | /* If DECL is a FUNCTION_DECL, find the TEMPLATE_DECL of which it is |
14797 | an immediate specialization. */ | |
14798 | if (TREE_CODE (decl) == FUNCTION_DECL) | |
14799 | { | |
14800 | if (DECL_TEMPLATE_INFO (decl)) { | |
14801 | decl = DECL_TI_TEMPLATE (decl); | |
14802 | ||
14803 | /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE for a | |
14804 | template friend. */ | |
14805 | if (TREE_CODE (decl) != TEMPLATE_DECL) | |
14806 | return NULL_TREE; | |
14807 | } else | |
14808 | return NULL_TREE; | |
14809 | } | |
14810 | ||
14811 | /* Look for more and more general templates. */ | |
14812 | while (DECL_TEMPLATE_INFO (decl)) | |
14813 | { | |
10b1d5e7 MM |
14814 | /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases. |
14815 | (See cp-tree.h for details.) */ | |
f9a7ae04 MM |
14816 | if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL) |
14817 | break; | |
14818 | ||
6e049fcd KL |
14819 | if (CLASS_TYPE_P (TREE_TYPE (decl)) |
14820 | && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl))) | |
14821 | break; | |
14822 | ||
f9a7ae04 MM |
14823 | /* Stop if we run into an explicitly specialized class template. */ |
14824 | if (!DECL_NAMESPACE_SCOPE_P (decl) | |
14825 | && DECL_CONTEXT (decl) | |
14826 | && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl))) | |
14827 | break; | |
14828 | ||
14829 | decl = DECL_TI_TEMPLATE (decl); | |
14830 | } | |
36a117a5 MM |
14831 | |
14832 | return decl; | |
14833 | } | |
14834 | ||
916b63c3 MM |
14835 | /* Return the most specialized of the class template partial |
14836 | specializations of TMPL which can produce TYPE, a specialization of | |
14837 | TMPL. The value returned is actually a TREE_LIST; the TREE_TYPE is | |
14838 | a _TYPE node corresponding to the partial specialization, while the | |
14839 | TREE_PURPOSE is the set of template arguments that must be | |
14840 | substituted into the TREE_TYPE in order to generate TYPE. | |
14841 | ||
14842 | If the choice of partial specialization is ambiguous, a diagnostic | |
14843 | is issued, and the error_mark_node is returned. If there are no | |
14844 | partial specializations of TMPL matching TYPE, then NULL_TREE is | |
14845 | returned. */ | |
36a117a5 | 14846 | |
e9659ab0 | 14847 | static tree |
916b63c3 | 14848 | most_specialized_class (tree type, tree tmpl) |
36a117a5 MM |
14849 | { |
14850 | tree list = NULL_TREE; | |
14851 | tree t; | |
14852 | tree champ; | |
73aad9b9 | 14853 | int fate; |
916b63c3 MM |
14854 | bool ambiguous_p; |
14855 | tree args; | |
dc28490d | 14856 | tree outer_args = NULL_TREE; |
73aad9b9 | 14857 | |
36a117a5 | 14858 | tmpl = most_general_template (tmpl); |
916b63c3 | 14859 | args = CLASSTYPE_TI_ARGS (type); |
dc28490d JM |
14860 | |
14861 | /* For determining which partial specialization to use, only the | |
14862 | innermost args are interesting. */ | |
14863 | if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args)) | |
14864 | { | |
14865 | outer_args = strip_innermost_template_args (args, 1); | |
14866 | args = INNERMOST_TEMPLATE_ARGS (args); | |
14867 | } | |
14868 | ||
36a117a5 | 14869 | for (t = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); t; t = TREE_CHAIN (t)) |
73aad9b9 | 14870 | { |
916b63c3 MM |
14871 | tree partial_spec_args; |
14872 | tree spec_args; | |
dc28490d | 14873 | tree parms = TREE_VALUE (t); |
916b63c3 MM |
14874 | |
14875 | partial_spec_args = CLASSTYPE_TI_ARGS (TREE_TYPE (t)); | |
dc28490d JM |
14876 | if (outer_args) |
14877 | { | |
14878 | int i; | |
14879 | ||
fd452cef JM |
14880 | ++processing_template_decl; |
14881 | ||
dc28490d JM |
14882 | /* Discard the outer levels of args, and then substitute in the |
14883 | template args from the enclosing class. */ | |
14884 | partial_spec_args = INNERMOST_TEMPLATE_ARGS (partial_spec_args); | |
14885 | partial_spec_args = tsubst_template_args | |
14886 | (partial_spec_args, outer_args, tf_none, NULL_TREE); | |
14887 | ||
14888 | /* PARMS already refers to just the innermost parms, but the | |
14889 | template parms in partial_spec_args had their levels lowered | |
14890 | by tsubst, so we need to do the same for the parm list. We | |
14891 | can't just tsubst the TREE_VEC itself, as tsubst wants to | |
14892 | treat a TREE_VEC as an argument vector. */ | |
14893 | parms = copy_node (parms); | |
14894 | for (i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i) | |
14895 | TREE_VEC_ELT (parms, i) = | |
14896 | tsubst (TREE_VEC_ELT (parms, i), outer_args, tf_none, NULL_TREE); | |
fd452cef JM |
14897 | |
14898 | --processing_template_decl; | |
dc28490d JM |
14899 | } |
14900 | spec_args = get_class_bindings (parms, | |
3db45ab5 | 14901 | partial_spec_args, |
916b63c3 | 14902 | args); |
36a117a5 | 14903 | if (spec_args) |
73aad9b9 | 14904 | { |
dc28490d JM |
14905 | if (outer_args) |
14906 | spec_args = add_to_template_args (outer_args, spec_args); | |
916b63c3 | 14907 | list = tree_cons (spec_args, TREE_VALUE (t), list); |
73aad9b9 JM |
14908 | TREE_TYPE (list) = TREE_TYPE (t); |
14909 | } | |
14910 | } | |
14911 | ||
14912 | if (! list) | |
14913 | return NULL_TREE; | |
14914 | ||
916b63c3 | 14915 | ambiguous_p = false; |
73aad9b9 JM |
14916 | t = list; |
14917 | champ = t; | |
14918 | t = TREE_CHAIN (t); | |
14919 | for (; t; t = TREE_CHAIN (t)) | |
14920 | { | |
916b63c3 | 14921 | fate = more_specialized_class (champ, t); |
73aad9b9 JM |
14922 | if (fate == 1) |
14923 | ; | |
14924 | else | |
14925 | { | |
14926 | if (fate == 0) | |
14927 | { | |
14928 | t = TREE_CHAIN (t); | |
14929 | if (! t) | |
916b63c3 MM |
14930 | { |
14931 | ambiguous_p = true; | |
14932 | break; | |
14933 | } | |
73aad9b9 JM |
14934 | } |
14935 | champ = t; | |
14936 | } | |
14937 | } | |
14938 | ||
916b63c3 MM |
14939 | if (!ambiguous_p) |
14940 | for (t = list; t && t != champ; t = TREE_CHAIN (t)) | |
14941 | { | |
14942 | fate = more_specialized_class (champ, t); | |
14943 | if (fate != 1) | |
14944 | { | |
14945 | ambiguous_p = true; | |
14946 | break; | |
14947 | } | |
14948 | } | |
14949 | ||
14950 | if (ambiguous_p) | |
73aad9b9 | 14951 | { |
916b63c3 MM |
14952 | const char *str = "candidates are:"; |
14953 | error ("ambiguous class template instantiation for %q#T", type); | |
14954 | for (t = list; t; t = TREE_CHAIN (t)) | |
14955 | { | |
14956 | error ("%s %+#T", str, TREE_TYPE (t)); | |
14957 | str = " "; | |
14958 | } | |
14959 | return error_mark_node; | |
73aad9b9 JM |
14960 | } |
14961 | ||
14962 | return champ; | |
14963 | } | |
14964 | ||
eb8845be | 14965 | /* Explicitly instantiate DECL. */ |
e92cc029 | 14966 | |
8d08fdba | 14967 | void |
eb8845be | 14968 | do_decl_instantiation (tree decl, tree storage) |
8d08fdba | 14969 | { |
8d08fdba | 14970 | tree result = NULL_TREE; |
faae18ab | 14971 | int extern_p = 0; |
e8abc66f | 14972 | |
ad47b891 | 14973 | if (!decl || decl == error_mark_node) |
dc957d14 | 14974 | /* An error occurred, for which grokdeclarator has already issued |
3fa56191 MM |
14975 | an appropriate message. */ |
14976 | return; | |
14977 | else if (! DECL_LANG_SPECIFIC (decl)) | |
ec255269 | 14978 | { |
0f51ccfc | 14979 | error ("explicit instantiation of non-template %q#D", decl); |
ec255269 MS |
14980 | return; |
14981 | } | |
03d0f4af | 14982 | else if (TREE_CODE (decl) == VAR_DECL) |
6633d636 | 14983 | { |
03d0f4af MM |
14984 | /* There is an asymmetry here in the way VAR_DECLs and |
14985 | FUNCTION_DECLs are handled by grokdeclarator. In the case of | |
14986 | the latter, the DECL we get back will be marked as a | |
14987 | template instantiation, and the appropriate | |
14988 | DECL_TEMPLATE_INFO will be set up. This does not happen for | |
14989 | VAR_DECLs so we do the lookup here. Probably, grokdeclarator | |
14990 | should handle VAR_DECLs as it currently handles | |
14991 | FUNCTION_DECLs. */ | |
8259e4f5 PC |
14992 | if (!DECL_CLASS_SCOPE_P (decl)) |
14993 | { | |
14994 | error ("%qD is not a static data member of a class template", decl); | |
14995 | return; | |
14996 | } | |
86ac0575 | 14997 | result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false); |
e0860732 | 14998 | if (!result || TREE_CODE (result) != VAR_DECL) |
03d0f4af | 14999 | { |
0f51ccfc | 15000 | error ("no matching template for %qD found", decl); |
03d0f4af MM |
15001 | return; |
15002 | } | |
8ea6dfae SM |
15003 | if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl))) |
15004 | { | |
15005 | error ("type %qT for explicit instantiation %qD does not match " | |
15006 | "declared type %qT", TREE_TYPE (result), decl, | |
15007 | TREE_TYPE (decl)); | |
15008 | return; | |
15009 | } | |
6633d636 MS |
15010 | } |
15011 | else if (TREE_CODE (decl) != FUNCTION_DECL) | |
15012 | { | |
0f51ccfc | 15013 | error ("explicit instantiation of %q#D", decl); |
6633d636 MS |
15014 | return; |
15015 | } | |
03d0f4af MM |
15016 | else |
15017 | result = decl; | |
672476cb | 15018 | |
03d0f4af | 15019 | /* Check for various error cases. Note that if the explicit |
0e339752 | 15020 | instantiation is valid the RESULT will currently be marked as an |
03d0f4af MM |
15021 | *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set |
15022 | until we get here. */ | |
15023 | ||
15024 | if (DECL_TEMPLATE_SPECIALIZATION (result)) | |
672476cb | 15025 | { |
07782718 KL |
15026 | /* DR 259 [temp.spec]. |
15027 | ||
15028 | Both an explicit instantiation and a declaration of an explicit | |
15029 | specialization shall not appear in a program unless the explicit | |
15030 | instantiation follows a declaration of the explicit specialization. | |
03d0f4af | 15031 | |
07782718 KL |
15032 | For a given set of template parameters, if an explicit |
15033 | instantiation of a template appears after a declaration of an | |
15034 | explicit specialization for that template, the explicit | |
15035 | instantiation has no effect. */ | |
672476cb MM |
15036 | return; |
15037 | } | |
03d0f4af MM |
15038 | else if (DECL_EXPLICIT_INSTANTIATION (result)) |
15039 | { | |
15040 | /* [temp.spec] | |
98c1c668 | 15041 | |
03d0f4af | 15042 | No program shall explicitly instantiate any template more |
c8094d83 | 15043 | than once. |
03d0f4af | 15044 | |
4684cd27 MM |
15045 | We check DECL_NOT_REALLY_EXTERN so as not to complain when |
15046 | the first instantiation was `extern' and the second is not, | |
15047 | and EXTERN_P for the opposite case. */ | |
15048 | if (DECL_NOT_REALLY_EXTERN (result) && !extern_p) | |
cbe5f3b3 | 15049 | permerror (input_location, "duplicate explicit instantiation of %q#D", result); |
4684cd27 MM |
15050 | /* If an "extern" explicit instantiation follows an ordinary |
15051 | explicit instantiation, the template is instantiated. */ | |
15052 | if (extern_p) | |
03d0f4af MM |
15053 | return; |
15054 | } | |
15055 | else if (!DECL_IMPLICIT_INSTANTIATION (result)) | |
faae18ab | 15056 | { |
0f51ccfc | 15057 | error ("no matching template for %qD found", result); |
faae18ab MS |
15058 | return; |
15059 | } | |
03d0f4af | 15060 | else if (!DECL_TEMPLATE_INFO (result)) |
6633d636 | 15061 | { |
cbe5f3b3 | 15062 | permerror (input_location, "explicit instantiation of non-template %q#D", result); |
6633d636 MS |
15063 | return; |
15064 | } | |
15065 | ||
f0e01782 | 15066 | if (storage == NULL_TREE) |
00595019 | 15067 | ; |
faae18ab | 15068 | else if (storage == ridpointers[(int) RID_EXTERN]) |
03d0f4af | 15069 | { |
fcf73884 | 15070 | if (!in_system_header && (cxx_dialect == cxx98)) |
509c9d60 | 15071 | pedwarn (input_location, OPT_pedantic, |
fcf73884 | 15072 | "ISO C++ 1998 forbids the use of %<extern%> on explicit " |
0cbd7506 | 15073 | "instantiations"); |
03d0f4af MM |
15074 | extern_p = 1; |
15075 | } | |
f0e01782 | 15076 | else |
0f51ccfc | 15077 | error ("storage class %qD applied to template instantiation", storage); |
3db45ab5 | 15078 | |
9c12301f | 15079 | check_explicit_instantiation_namespace (result); |
5566b478 | 15080 | mark_decl_instantiated (result, extern_p); |
c91a56d2 | 15081 | if (! extern_p) |
3db45ab5 | 15082 | instantiate_decl (result, /*defer_ok=*/1, |
eba839f9 | 15083 | /*expl_inst_class_mem_p=*/false); |
7177d104 MS |
15084 | } |
15085 | ||
b5791fdc | 15086 | static void |
3a978d72 | 15087 | mark_class_instantiated (tree t, int extern_p) |
faae18ab MS |
15088 | { |
15089 | SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t); | |
eb773359 JM |
15090 | SET_CLASSTYPE_INTERFACE_KNOWN (t); |
15091 | CLASSTYPE_INTERFACE_ONLY (t) = extern_p; | |
faae18ab MS |
15092 | TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p; |
15093 | if (! extern_p) | |
15094 | { | |
15095 | CLASSTYPE_DEBUG_REQUESTED (t) = 1; | |
15096 | rest_of_type_compilation (t, 1); | |
15097 | } | |
c8094d83 | 15098 | } |
e8abc66f | 15099 | |
5e0c54e5 | 15100 | /* Called from do_type_instantiation through binding_table_foreach to |
9bcb9aae | 15101 | do recursive instantiation for the type bound in ENTRY. */ |
5e0c54e5 GDR |
15102 | static void |
15103 | bt_instantiate_type_proc (binding_entry entry, void *data) | |
15104 | { | |
15105 | tree storage = *(tree *) data; | |
15106 | ||
9e1e64ec | 15107 | if (MAYBE_CLASS_TYPE_P (entry->type) |
5e0c54e5 GDR |
15108 | && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type))) |
15109 | do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0); | |
15110 | } | |
15111 | ||
415c974c MA |
15112 | /* Called from do_type_instantiation to instantiate a member |
15113 | (a member function or a static member variable) of an | |
03fd3f84 | 15114 | explicitly instantiated class template. */ |
415c974c MA |
15115 | static void |
15116 | instantiate_class_member (tree decl, int extern_p) | |
15117 | { | |
15118 | mark_decl_instantiated (decl, extern_p); | |
415c974c | 15119 | if (! extern_p) |
3db45ab5 | 15120 | instantiate_decl (decl, /*defer_ok=*/1, |
eba839f9 | 15121 | /*expl_inst_class_mem_p=*/true); |
415c974c MA |
15122 | } |
15123 | ||
a1bcc528 JM |
15124 | /* Perform an explicit instantiation of template class T. STORAGE, if |
15125 | non-null, is the RID for extern, inline or static. COMPLAIN is | |
838dfd8a | 15126 | nonzero if this is called from the parser, zero if called recursively, |
a1bcc528 | 15127 | since the standard is unclear (as detailed below). */ |
c8094d83 | 15128 | |
7177d104 | 15129 | void |
3a978d72 | 15130 | do_type_instantiation (tree t, tree storage, tsubst_flags_t complain) |
7177d104 | 15131 | { |
e8abc66f MS |
15132 | int extern_p = 0; |
15133 | int nomem_p = 0; | |
5566b478 | 15134 | int static_p = 0; |
4746cf84 | 15135 | int previous_instantiation_extern_p = 0; |
5566b478 | 15136 | |
ca79f85d JM |
15137 | if (TREE_CODE (t) == TYPE_DECL) |
15138 | t = TREE_TYPE (t); | |
15139 | ||
7ddedda4 | 15140 | if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t)) |
ca79f85d | 15141 | { |
0f51ccfc | 15142 | error ("explicit instantiation of non-template type %qT", t); |
ca79f85d JM |
15143 | return; |
15144 | } | |
15145 | ||
5566b478 | 15146 | complete_type (t); |
7177d104 | 15147 | |
d0f062fb | 15148 | if (!COMPLETE_TYPE_P (t)) |
f0e01782 | 15149 | { |
c2ea3a40 | 15150 | if (complain & tf_error) |
0f51ccfc | 15151 | error ("explicit instantiation of %q#T before definition of template", |
0cbd7506 | 15152 | t); |
f0e01782 MS |
15153 | return; |
15154 | } | |
15155 | ||
03d0f4af | 15156 | if (storage != NULL_TREE) |
f0e01782 | 15157 | { |
fcf73884 | 15158 | if (!in_system_header) |
1ffe7b6e PC |
15159 | { |
15160 | if (storage == ridpointers[(int) RID_EXTERN]) | |
15161 | { | |
15162 | if (cxx_dialect == cxx98) | |
509c9d60 MLI |
15163 | pedwarn (input_location, OPT_pedantic, |
15164 | "ISO C++ 1998 forbids the use of %<extern%> on " | |
15165 | "explicit instantiations"); | |
1ffe7b6e PC |
15166 | } |
15167 | else | |
509c9d60 MLI |
15168 | pedwarn (input_location, OPT_pedantic, |
15169 | "ISO C++ forbids the use of %qE" | |
15170 | " on explicit instantiations", storage); | |
1ffe7b6e | 15171 | } |
03d0f4af MM |
15172 | |
15173 | if (storage == ridpointers[(int) RID_INLINE]) | |
15174 | nomem_p = 1; | |
15175 | else if (storage == ridpointers[(int) RID_EXTERN]) | |
15176 | extern_p = 1; | |
15177 | else if (storage == ridpointers[(int) RID_STATIC]) | |
15178 | static_p = 1; | |
15179 | else | |
15180 | { | |
0f51ccfc | 15181 | error ("storage class %qD applied to template instantiation", |
0cbd7506 | 15182 | storage); |
03d0f4af MM |
15183 | extern_p = 0; |
15184 | } | |
f0e01782 MS |
15185 | } |
15186 | ||
370af2d5 | 15187 | if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t)) |
03d0f4af | 15188 | { |
07782718 | 15189 | /* DR 259 [temp.spec]. |
a292b002 | 15190 | |
07782718 KL |
15191 | Both an explicit instantiation and a declaration of an explicit |
15192 | specialization shall not appear in a program unless the explicit | |
15193 | instantiation follows a declaration of the explicit specialization. | |
15194 | ||
15195 | For a given set of template parameters, if an explicit | |
15196 | instantiation of a template appears after a declaration of an | |
15197 | explicit specialization for that template, the explicit | |
15198 | instantiation has no effect. */ | |
03d0f4af MM |
15199 | return; |
15200 | } | |
15201 | else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t)) | |
44a8d0b3 | 15202 | { |
03d0f4af MM |
15203 | /* [temp.spec] |
15204 | ||
15205 | No program shall explicitly instantiate any template more | |
c8094d83 | 15206 | than once. |
03d0f4af | 15207 | |
0cbd7506 | 15208 | If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit |
4746cf84 | 15209 | instantiation was `extern'. If EXTERN_P then the second is. |
4684cd27 | 15210 | These cases are OK. */ |
4746cf84 MA |
15211 | previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t); |
15212 | ||
15213 | if (!previous_instantiation_extern_p && !extern_p | |
c2ea3a40 | 15214 | && (complain & tf_error)) |
cbe5f3b3 | 15215 | permerror (input_location, "duplicate explicit instantiation of %q#T", t); |
c8094d83 | 15216 | |
03d0f4af MM |
15217 | /* If we've already instantiated the template, just return now. */ |
15218 | if (!CLASSTYPE_INTERFACE_ONLY (t)) | |
15219 | return; | |
44a8d0b3 | 15220 | } |
e8abc66f | 15221 | |
9c12301f | 15222 | check_explicit_instantiation_namespace (TYPE_NAME (t)); |
03d0f4af | 15223 | mark_class_instantiated (t, extern_p); |
03d0f4af | 15224 | |
e8abc66f MS |
15225 | if (nomem_p) |
15226 | return; | |
15227 | ||
7177d104 | 15228 | { |
db5ae43f | 15229 | tree tmp; |
5566b478 | 15230 | |
03d0f4af MM |
15231 | /* In contrast to implicit instantiation, where only the |
15232 | declarations, and not the definitions, of members are | |
15233 | instantiated, we have here: | |
15234 | ||
0cbd7506 | 15235 | [temp.explicit] |
03d0f4af MM |
15236 | |
15237 | The explicit instantiation of a class template specialization | |
15238 | implies the instantiation of all of its members not | |
15239 | previously explicitly specialized in the translation unit | |
c8094d83 | 15240 | containing the explicit instantiation. |
03d0f4af MM |
15241 | |
15242 | Of course, we can't instantiate member template classes, since | |
15243 | we don't have any arguments for them. Note that the standard | |
dc957d14 | 15244 | is unclear on whether the instantiation of the members are |
415c974c | 15245 | *explicit* instantiations or not. However, the most natural |
03fd3f84 | 15246 | interpretation is that it should be an explicit instantiation. */ |
03d0f4af | 15247 | |
5566b478 MS |
15248 | if (! static_p) |
15249 | for (tmp = TYPE_METHODS (t); tmp; tmp = TREE_CHAIN (tmp)) | |
a7d87521 | 15250 | if (TREE_CODE (tmp) == FUNCTION_DECL |
1f109f0f | 15251 | && DECL_TEMPLATE_INSTANTIATION (tmp)) |
415c974c | 15252 | instantiate_class_member (tmp, extern_p); |
5566b478 MS |
15253 | |
15254 | for (tmp = TYPE_FIELDS (t); tmp; tmp = TREE_CHAIN (tmp)) | |
15255 | if (TREE_CODE (tmp) == VAR_DECL && DECL_TEMPLATE_INSTANTIATION (tmp)) | |
415c974c | 15256 | instantiate_class_member (tmp, extern_p); |
7177d104 | 15257 | |
5e0c54e5 GDR |
15258 | if (CLASSTYPE_NESTED_UTDS (t)) |
15259 | binding_table_foreach (CLASSTYPE_NESTED_UTDS (t), | |
0cbd7506 | 15260 | bt_instantiate_type_proc, &storage); |
a292b002 | 15261 | } |
8d08fdba | 15262 | } |
a28e3c7f | 15263 | |
36a117a5 MM |
15264 | /* Given a function DECL, which is a specialization of TMPL, modify |
15265 | DECL to be a re-instantiation of TMPL with the same template | |
15266 | arguments. TMPL should be the template into which tsubst'ing | |
15267 | should occur for DECL, not the most general template. | |
830bfa74 MM |
15268 | |
15269 | One reason for doing this is a scenario like this: | |
15270 | ||
15271 | template <class T> | |
15272 | void f(const T&, int i); | |
15273 | ||
15274 | void g() { f(3, 7); } | |
15275 | ||
15276 | template <class T> | |
15277 | void f(const T& t, const int i) { } | |
15278 | ||
15279 | Note that when the template is first instantiated, with | |
15280 | instantiate_template, the resulting DECL will have no name for the | |
15281 | first parameter, and the wrong type for the second. So, when we go | |
15282 | to instantiate the DECL, we regenerate it. */ | |
15283 | ||
e9659ab0 | 15284 | static void |
3a978d72 | 15285 | regenerate_decl_from_template (tree decl, tree tmpl) |
830bfa74 | 15286 | { |
f9a7ae04 MM |
15287 | /* The arguments used to instantiate DECL, from the most general |
15288 | template. */ | |
830bfa74 | 15289 | tree args; |
830bfa74 | 15290 | tree code_pattern; |
830bfa74 MM |
15291 | |
15292 | args = DECL_TI_ARGS (decl); | |
15293 | code_pattern = DECL_TEMPLATE_RESULT (tmpl); | |
15294 | ||
2b907f5c KL |
15295 | /* Make sure that we can see identifiers, and compute access |
15296 | correctly. */ | |
15297 | push_access_scope (decl); | |
15298 | ||
c7222c02 MM |
15299 | if (TREE_CODE (decl) == FUNCTION_DECL) |
15300 | { | |
15301 | tree decl_parm; | |
15302 | tree pattern_parm; | |
15303 | tree specs; | |
15304 | int args_depth; | |
15305 | int parms_depth; | |
c8094d83 | 15306 | |
c7222c02 | 15307 | args_depth = TMPL_ARGS_DEPTH (args); |
c8094d83 | 15308 | parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)); |
c7222c02 MM |
15309 | if (args_depth > parms_depth) |
15310 | args = get_innermost_template_args (args, parms_depth); | |
15311 | ||
15312 | specs = tsubst_exception_specification (TREE_TYPE (code_pattern), | |
15313 | args, tf_error, NULL_TREE); | |
15314 | if (specs) | |
15315 | TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl), | |
15316 | specs); | |
15317 | ||
15318 | /* Merge parameter declarations. */ | |
c8094d83 | 15319 | decl_parm = skip_artificial_parms_for (decl, |
c7222c02 | 15320 | DECL_ARGUMENTS (decl)); |
c8094d83 | 15321 | pattern_parm |
c7222c02 MM |
15322 | = skip_artificial_parms_for (code_pattern, |
15323 | DECL_ARGUMENTS (code_pattern)); | |
5d80a306 | 15324 | while (decl_parm && !FUNCTION_PARAMETER_PACK_P (pattern_parm)) |
c7222c02 MM |
15325 | { |
15326 | tree parm_type; | |
b17bba6d | 15327 | tree attributes; |
5d80a306 | 15328 | |
c7222c02 MM |
15329 | if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm)) |
15330 | DECL_NAME (decl_parm) = DECL_NAME (pattern_parm); | |
15331 | parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error, | |
15332 | NULL_TREE); | |
02bab9db | 15333 | parm_type = type_decays_to (parm_type); |
c7222c02 MM |
15334 | if (!same_type_p (TREE_TYPE (decl_parm), parm_type)) |
15335 | TREE_TYPE (decl_parm) = parm_type; | |
b17bba6d MM |
15336 | attributes = DECL_ATTRIBUTES (pattern_parm); |
15337 | if (DECL_ATTRIBUTES (decl_parm) != attributes) | |
15338 | { | |
15339 | DECL_ATTRIBUTES (decl_parm) = attributes; | |
15340 | cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0); | |
15341 | } | |
c7222c02 MM |
15342 | decl_parm = TREE_CHAIN (decl_parm); |
15343 | pattern_parm = TREE_CHAIN (pattern_parm); | |
15344 | } | |
5d80a306 DG |
15345 | /* Merge any parameters that match with the function parameter |
15346 | pack. */ | |
15347 | if (pattern_parm && FUNCTION_PARAMETER_PACK_P (pattern_parm)) | |
15348 | { | |
15349 | int i, len; | |
15350 | tree expanded_types; | |
15351 | /* Expand the TYPE_PACK_EXPANSION that provides the types for | |
15352 | the parameters in this function parameter pack. */ | |
15353 | expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm), | |
15354 | args, tf_error, NULL_TREE); | |
15355 | len = TREE_VEC_LENGTH (expanded_types); | |
15356 | for (i = 0; i < len; i++) | |
15357 | { | |
15358 | tree parm_type; | |
15359 | tree attributes; | |
15360 | ||
15361 | if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm)) | |
15362 | /* Rename the parameter to include the index. */ | |
15363 | DECL_NAME (decl_parm) = | |
15364 | make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i); | |
15365 | parm_type = TREE_VEC_ELT (expanded_types, i); | |
15366 | parm_type = type_decays_to (parm_type); | |
15367 | if (!same_type_p (TREE_TYPE (decl_parm), parm_type)) | |
15368 | TREE_TYPE (decl_parm) = parm_type; | |
15369 | attributes = DECL_ATTRIBUTES (pattern_parm); | |
15370 | if (DECL_ATTRIBUTES (decl_parm) != attributes) | |
15371 | { | |
15372 | DECL_ATTRIBUTES (decl_parm) = attributes; | |
15373 | cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0); | |
15374 | } | |
15375 | decl_parm = TREE_CHAIN (decl_parm); | |
15376 | } | |
15377 | } | |
c7222c02 MM |
15378 | /* Merge additional specifiers from the CODE_PATTERN. */ |
15379 | if (DECL_DECLARED_INLINE_P (code_pattern) | |
15380 | && !DECL_DECLARED_INLINE_P (decl)) | |
15381 | DECL_DECLARED_INLINE_P (decl) = 1; | |
c7222c02 MM |
15382 | } |
15383 | else if (TREE_CODE (decl) == VAR_DECL) | |
b794e321 MM |
15384 | DECL_INITIAL (decl) = |
15385 | tsubst_expr (DECL_INITIAL (code_pattern), args, | |
015c2c66 MM |
15386 | tf_error, DECL_TI_TEMPLATE (decl), |
15387 | /*integral_constant_expression_p=*/false); | |
c7222c02 MM |
15388 | else |
15389 | gcc_unreachable (); | |
36a117a5 | 15390 | |
2b59fc25 | 15391 | pop_access_scope (decl); |
830bfa74 MM |
15392 | } |
15393 | ||
a723baf1 MM |
15394 | /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be |
15395 | substituted to get DECL. */ | |
15396 | ||
d58b7c2d | 15397 | tree |
a723baf1 MM |
15398 | template_for_substitution (tree decl) |
15399 | { | |
15400 | tree tmpl = DECL_TI_TEMPLATE (decl); | |
15401 | ||
15402 | /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern | |
15403 | for the instantiation. This is not always the most general | |
15404 | template. Consider, for example: | |
15405 | ||
0cbd7506 | 15406 | template <class T> |
a723baf1 | 15407 | struct S { template <class U> void f(); |
0cbd7506 | 15408 | template <> void f<int>(); }; |
a723baf1 MM |
15409 | |
15410 | and an instantiation of S<double>::f<int>. We want TD to be the | |
15411 | specialization S<T>::f<int>, not the more general S<T>::f<U>. */ | |
15412 | while (/* An instantiation cannot have a definition, so we need a | |
15413 | more general template. */ | |
15414 | DECL_TEMPLATE_INSTANTIATION (tmpl) | |
15415 | /* We must also deal with friend templates. Given: | |
15416 | ||
c8094d83 | 15417 | template <class T> struct S { |
a723baf1 MM |
15418 | template <class U> friend void f() {}; |
15419 | }; | |
15420 | ||
15421 | S<int>::f<U> say, is not an instantiation of S<T>::f<U>, | |
15422 | so far as the language is concerned, but that's still | |
15423 | where we get the pattern for the instantiation from. On | |
15424 | other hand, if the definition comes outside the class, say: | |
15425 | ||
c8094d83 | 15426 | template <class T> struct S { |
a723baf1 MM |
15427 | template <class U> friend void f(); |
15428 | }; | |
15429 | template <class U> friend void f() {} | |
15430 | ||
15431 | we don't need to look any further. That's what the check for | |
15432 | DECL_INITIAL is for. */ | |
15433 | || (TREE_CODE (decl) == FUNCTION_DECL | |
15434 | && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl) | |
15435 | && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl)))) | |
15436 | { | |
15437 | /* The present template, TD, should not be a definition. If it | |
15438 | were a definition, we should be using it! Note that we | |
15439 | cannot restructure the loop to just keep going until we find | |
15440 | a template with a definition, since that might go too far if | |
15441 | a specialization was declared, but not defined. */ | |
50bc768d NS |
15442 | gcc_assert (TREE_CODE (decl) != VAR_DECL |
15443 | || DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (tmpl))); | |
c8094d83 | 15444 | |
a723baf1 MM |
15445 | /* Fetch the more general template. */ |
15446 | tmpl = DECL_TI_TEMPLATE (tmpl); | |
15447 | } | |
15448 | ||
15449 | return tmpl; | |
15450 | } | |
15451 | ||
16d53b64 | 15452 | /* Produce the definition of D, a _DECL generated from a template. If |
838dfd8a | 15453 | DEFER_OK is nonzero, then we don't have to actually do the |
415c974c MA |
15454 | instantiation now; we just have to do it sometime. Normally it is |
15455 | an error if this is an explicit instantiation but D is undefined. | |
eba839f9 MM |
15456 | EXPL_INST_CLASS_MEM_P is true iff D is a member of an |
15457 | explicitly instantiated class template. */ | |
f84b4be9 | 15458 | |
a28e3c7f | 15459 | tree |
3db45ab5 | 15460 | instantiate_decl (tree d, int defer_ok, |
eba839f9 | 15461 | bool expl_inst_class_mem_p) |
a28e3c7f | 15462 | { |
36a117a5 | 15463 | tree tmpl = DECL_TI_TEMPLATE (d); |
65f8b0fb MM |
15464 | tree gen_args; |
15465 | tree args; | |
830bfa74 | 15466 | tree td; |
36a117a5 MM |
15467 | tree code_pattern; |
15468 | tree spec; | |
15469 | tree gen_tmpl; | |
b794e321 | 15470 | bool pattern_defined; |
31a714f6 | 15471 | int need_push; |
82a98427 | 15472 | location_t saved_loc = input_location; |
f7e4e484 | 15473 | bool external_p; |
c8094d83 | 15474 | |
36a117a5 MM |
15475 | /* This function should only be used to instantiate templates for |
15476 | functions and static member variables. */ | |
50bc768d NS |
15477 | gcc_assert (TREE_CODE (d) == FUNCTION_DECL |
15478 | || TREE_CODE (d) == VAR_DECL); | |
36a117a5 | 15479 | |
cec24319 MM |
15480 | /* Variables are never deferred; if instantiation is required, they |
15481 | are instantiated right away. That allows for better code in the | |
15482 | case that an expression refers to the value of the variable -- | |
15483 | if the variable has a constant value the referring expression can | |
15484 | take advantage of that fact. */ | |
15485 | if (TREE_CODE (d) == VAR_DECL) | |
15486 | defer_ok = 0; | |
15487 | ||
db9b2174 MM |
15488 | /* Don't instantiate cloned functions. Instead, instantiate the |
15489 | functions they cloned. */ | |
15490 | if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d)) | |
15491 | d = DECL_CLONED_FUNCTION (d); | |
15492 | ||
fbf1c34b | 15493 | if (DECL_TEMPLATE_INSTANTIATED (d)) |
03d0f4af | 15494 | /* D has already been instantiated. It might seem reasonable to |
dc957d14 | 15495 | check whether or not D is an explicit instantiation, and, if so, |
03d0f4af MM |
15496 | stop here. But when an explicit instantiation is deferred |
15497 | until the end of the compilation, DECL_EXPLICIT_INSTANTIATION | |
15498 | is set, even though we still need to do the instantiation. */ | |
36a117a5 MM |
15499 | return d; |
15500 | ||
15501 | /* If we already have a specialization of this declaration, then | |
15502 | there's no reason to instantiate it. Note that | |
15503 | retrieve_specialization gives us both instantiations and | |
15504 | specializations, so we must explicitly check | |
15505 | DECL_TEMPLATE_SPECIALIZATION. */ | |
15506 | gen_tmpl = most_general_template (tmpl); | |
65f8b0fb | 15507 | gen_args = DECL_TI_ARGS (d); |
c7222c02 MM |
15508 | spec = retrieve_specialization (gen_tmpl, gen_args, |
15509 | /*class_specializations_p=*/false); | |
36a117a5 MM |
15510 | if (spec != NULL_TREE && DECL_TEMPLATE_SPECIALIZATION (spec)) |
15511 | return spec; | |
15512 | ||
15513 | /* This needs to happen before any tsubsting. */ | |
15514 | if (! push_tinst_level (d)) | |
15515 | return d; | |
15516 | ||
297a5329 JM |
15517 | timevar_push (TV_PARSE); |
15518 | ||
4d85e00e | 15519 | /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern |
a723baf1 MM |
15520 | for the instantiation. */ |
15521 | td = template_for_substitution (d); | |
fee23f54 | 15522 | code_pattern = DECL_TEMPLATE_RESULT (td); |
27bb8339 | 15523 | |
2d22db1f | 15524 | /* We should never be trying to instantiate a member of a class |
3db45ab5 | 15525 | template or partial specialization. */ |
2d22db1f | 15526 | gcc_assert (d != code_pattern); |
3db45ab5 | 15527 | |
76d3baad KL |
15528 | if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d)) |
15529 | || DECL_TEMPLATE_SPECIALIZATION (td)) | |
649fc72d NS |
15530 | /* In the case of a friend template whose definition is provided |
15531 | outside the class, we may have too many arguments. Drop the | |
76d3baad | 15532 | ones we don't need. The same is true for specializations. */ |
649fc72d NS |
15533 | args = get_innermost_template_args |
15534 | (gen_args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td))); | |
15535 | else | |
15536 | args = gen_args; | |
65f8b0fb | 15537 | |
5566b478 | 15538 | if (TREE_CODE (d) == FUNCTION_DECL) |
39c76b4f | 15539 | pattern_defined = (DECL_SAVED_TREE (code_pattern) != NULL_TREE); |
5566b478 | 15540 | else |
36a117a5 | 15541 | pattern_defined = ! DECL_IN_AGGR_P (code_pattern); |
b794e321 MM |
15542 | |
15543 | /* We may be in the middle of deferred access check. Disable it now. */ | |
15544 | push_deferring_access_checks (dk_no_deferred); | |
15545 | ||
4684cd27 MM |
15546 | /* Unless an explicit instantiation directive has already determined |
15547 | the linkage of D, remember that a definition is available for | |
15548 | this entity. */ | |
c8094d83 | 15549 | if (pattern_defined |
4684cd27 MM |
15550 | && !DECL_INTERFACE_KNOWN (d) |
15551 | && !DECL_NOT_REALLY_EXTERN (d)) | |
15552 | mark_definable (d); | |
de22184b | 15553 | |
f31686a3 | 15554 | input_location = DECL_SOURCE_LOCATION (d); |
de22184b | 15555 | |
eba839f9 MM |
15556 | /* If D is a member of an explicitly instantiated class template, |
15557 | and no definition is available, treat it like an implicit | |
3db45ab5 MS |
15558 | instantiation. */ |
15559 | if (!pattern_defined && expl_inst_class_mem_p | |
15560 | && DECL_EXPLICIT_INSTANTIATION (d)) | |
5566b478 | 15561 | { |
4684cd27 | 15562 | DECL_NOT_REALLY_EXTERN (d) = 0; |
eba839f9 | 15563 | DECL_INTERFACE_KNOWN (d) = 0; |
4684cd27 | 15564 | SET_DECL_IMPLICIT_INSTANTIATION (d); |
5566b478 MS |
15565 | } |
15566 | ||
4f2b0fb2 NS |
15567 | if (!defer_ok) |
15568 | { | |
15569 | /* Recheck the substitutions to obtain any warning messages | |
15570 | about ignoring cv qualifiers. */ | |
15571 | tree gen = DECL_TEMPLATE_RESULT (gen_tmpl); | |
15572 | tree type = TREE_TYPE (gen); | |
15573 | ||
0e902d98 | 15574 | /* Make sure that we can see identifiers, and compute access |
2b59fc25 KL |
15575 | correctly. D is already the target FUNCTION_DECL with the |
15576 | right context. */ | |
15577 | push_access_scope (d); | |
0e902d98 | 15578 | |
4f2b0fb2 NS |
15579 | if (TREE_CODE (gen) == FUNCTION_DECL) |
15580 | { | |
23fca1f5 | 15581 | tsubst (DECL_ARGUMENTS (gen), gen_args, tf_warning_or_error, d); |
5d80a306 DG |
15582 | tsubst_exception_specification (type, gen_args, tf_warning_or_error, |
15583 | d); | |
4f2b0fb2 NS |
15584 | /* Don't simply tsubst the function type, as that will give |
15585 | duplicate warnings about poor parameter qualifications. | |
15586 | The function arguments are the same as the decl_arguments | |
c6002625 | 15587 | without the top level cv qualifiers. */ |
4f2b0fb2 NS |
15588 | type = TREE_TYPE (type); |
15589 | } | |
23fca1f5 | 15590 | tsubst (type, gen_args, tf_warning_or_error, d); |
0e902d98 | 15591 | |
2b59fc25 | 15592 | pop_access_scope (d); |
4f2b0fb2 | 15593 | } |
c8094d83 | 15594 | |
f7e4e484 MM |
15595 | /* Check to see whether we know that this template will be |
15596 | instantiated in some other file, as with "extern template" | |
15597 | extension. */ | |
15598 | external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d)); | |
15599 | /* In general, we do not instantiate such templates... */ | |
15600 | if (external_p | |
15601 | /* ... but we instantiate inline functions so that we can inline | |
ccfbd880 RG |
15602 | them. An explicit instantiation declaration prohibits implicit |
15603 | instantiation of non-inline functions. With high levels of | |
15604 | optimization, we would normally inline non-inline functions | |
15605 | -- but we're not allowed to do that for "extern template" functions. | |
15606 | Therefore, we check DECL_DECLARED_INLINE_P, rather than | |
15607 | possibly_inlined_p. And ... */ | |
4847609e | 15608 | && ! (TREE_CODE (d) == FUNCTION_DECL |
ccfbd880 | 15609 | && DECL_DECLARED_INLINE_P (d)) |
f7e4e484 MM |
15610 | /* ... we instantiate static data members whose values are |
15611 | needed in integral constant expressions. */ | |
3db45ab5 | 15612 | && ! (TREE_CODE (d) == VAR_DECL |
f7e4e484 | 15613 | && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (d))) |
930cd796 | 15614 | goto out; |
16d53b64 | 15615 | /* Defer all other templates, unless we have been explicitly |
f7e4e484 MM |
15616 | forbidden from doing so. */ |
15617 | if (/* If there is no definition, we cannot instantiate the | |
15618 | template. */ | |
3db45ab5 | 15619 | ! pattern_defined |
f7e4e484 MM |
15620 | /* If it's OK to postpone instantiation, do so. */ |
15621 | || defer_ok | |
15622 | /* If this is a static data member that will be defined | |
15623 | elsewhere, we don't want to instantiate the entire data | |
15624 | member, but we do want to instantiate the initializer so that | |
15625 | we can substitute that elsewhere. */ | |
15626 | || (external_p && TREE_CODE (d) == VAR_DECL)) | |
16d53b64 | 15627 | { |
b794e321 MM |
15628 | /* The definition of the static data member is now required so |
15629 | we must substitute the initializer. */ | |
15630 | if (TREE_CODE (d) == VAR_DECL | |
3db45ab5 | 15631 | && !DECL_INITIAL (d) |
b794e321 MM |
15632 | && DECL_INITIAL (code_pattern)) |
15633 | { | |
4bff36d3 MM |
15634 | tree ns; |
15635 | tree init; | |
15636 | ||
15637 | ns = decl_namespace_context (d); | |
27a725e2 | 15638 | push_nested_namespace (ns); |
b794e321 | 15639 | push_nested_class (DECL_CONTEXT (d)); |
3db45ab5 | 15640 | init = tsubst_expr (DECL_INITIAL (code_pattern), |
4bff36d3 | 15641 | args, |
015c2c66 MM |
15642 | tf_warning_or_error, NULL_TREE, |
15643 | /*integral_constant_expression_p=*/false); | |
d174af6c MM |
15644 | cp_finish_decl (d, init, /*init_const_expr_p=*/false, |
15645 | /*asmspec_tree=*/NULL_TREE, | |
86414779 | 15646 | LOOKUP_ONLYCONVERTING); |
b794e321 | 15647 | pop_nested_class (); |
27a725e2 | 15648 | pop_nested_namespace (ns); |
b794e321 MM |
15649 | } |
15650 | ||
f7e4e484 MM |
15651 | /* We restore the source position here because it's used by |
15652 | add_pending_template. */ | |
82a98427 | 15653 | input_location = saved_loc; |
c27be9b9 | 15654 | |
c8094d83 | 15655 | if (at_eof && !pattern_defined |
da3933ba JM |
15656 | && DECL_EXPLICIT_INSTANTIATION (d) |
15657 | && DECL_NOT_REALLY_EXTERN (d)) | |
03d0f4af MM |
15658 | /* [temp.explicit] |
15659 | ||
15660 | The definition of a non-exported function template, a | |
15661 | non-exported member function template, or a non-exported | |
15662 | member function or static data member of a class template | |
15663 | shall be present in every translation unit in which it is | |
15664 | explicitly instantiated. */ | |
cbe5f3b3 MLI |
15665 | permerror (input_location, "explicit instantiation of %qD " |
15666 | "but no definition available", d); | |
03d0f4af | 15667 | |
f7e4e484 MM |
15668 | /* ??? Historically, we have instantiated inline functions, even |
15669 | when marked as "extern template". */ | |
15670 | if (!(external_p && TREE_CODE (d) == VAR_DECL)) | |
15671 | add_pending_template (d); | |
de22184b | 15672 | goto out; |
5566b478 | 15673 | } |
4684cd27 MM |
15674 | /* Tell the repository that D is available in this translation unit |
15675 | -- and see if it is supposed to be instantiated here. */ | |
15676 | if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d)) | |
15677 | { | |
15678 | /* In a PCH file, despite the fact that the repository hasn't | |
15679 | requested instantiation in the PCH it is still possible that | |
15680 | an instantiation will be required in a file that includes the | |
15681 | PCH. */ | |
15682 | if (pch_file) | |
15683 | add_pending_template (d); | |
15684 | /* Instantiate inline functions so that the inliner can do its | |
15685 | job, even though we'll not be emitting a copy of this | |
15686 | function. */ | |
4847609e | 15687 | if (!(TREE_CODE (d) == FUNCTION_DECL && possibly_inlined_p (d))) |
4684cd27 MM |
15688 | goto out; |
15689 | } | |
5566b478 | 15690 | |
6de9cd9a | 15691 | need_push = !cfun || !global_bindings_p (); |
31a714f6 MM |
15692 | if (need_push) |
15693 | push_to_top_level (); | |
414ea4aa | 15694 | |
66e0c440 KL |
15695 | /* Mark D as instantiated so that recursive calls to |
15696 | instantiate_decl do not try to instantiate it again. */ | |
15697 | DECL_TEMPLATE_INSTANTIATED (d) = 1; | |
15698 | ||
2b0a63a3 MM |
15699 | /* Regenerate the declaration in case the template has been modified |
15700 | by a subsequent redeclaration. */ | |
15701 | regenerate_decl_from_template (d, td); | |
4684cd27 | 15702 | |
120722ac | 15703 | /* We already set the file and line above. Reset them now in case |
6de9cd9a | 15704 | they changed as a result of calling regenerate_decl_from_template. */ |
f31686a3 | 15705 | input_location = DECL_SOURCE_LOCATION (d); |
5156628f | 15706 | |
5566b478 MS |
15707 | if (TREE_CODE (d) == VAR_DECL) |
15708 | { | |
e92fb501 MM |
15709 | tree init; |
15710 | ||
1d62c33e MM |
15711 | /* Clear out DECL_RTL; whatever was there before may not be right |
15712 | since we've reset the type of the declaration. */ | |
15713 | SET_DECL_RTL (d, NULL_RTX); | |
5566b478 | 15714 | DECL_IN_AGGR_P (d) = 0; |
ea56c40c | 15715 | |
e92fb501 MM |
15716 | /* The initializer is placed in DECL_INITIAL by |
15717 | regenerate_decl_from_template. Pull it out so that | |
15718 | finish_decl can process it. */ | |
15719 | init = DECL_INITIAL (d); | |
15720 | DECL_INITIAL (d) = NULL_TREE; | |
15721 | DECL_INITIALIZED_P (d) = 0; | |
15722 | ||
4684cd27 MM |
15723 | /* Clear DECL_EXTERNAL so that cp_finish_decl will process the |
15724 | initializer. That function will defer actual emission until | |
15725 | we have a chance to determine linkage. */ | |
15726 | DECL_EXTERNAL (d) = 0; | |
15727 | ||
73a8adb6 | 15728 | /* Enter the scope of D so that access-checking works correctly. */ |
4684cd27 | 15729 | push_nested_class (DECL_CONTEXT (d)); |
bbbbb16a | 15730 | finish_decl (d, init, NULL_TREE, NULL_TREE); |
73a8adb6 | 15731 | pop_nested_class (); |
5566b478 MS |
15732 | } |
15733 | else if (TREE_CODE (d) == FUNCTION_DECL) | |
15734 | { | |
6bbf1598 | 15735 | htab_t saved_local_specializations; |
a723baf1 MM |
15736 | tree subst_decl; |
15737 | tree tmpl_parm; | |
15738 | tree spec_parm; | |
6bbf1598 MM |
15739 | |
15740 | /* Save away the current list, in case we are instantiating one | |
15741 | template from within the body of another. */ | |
15742 | saved_local_specializations = local_specializations; | |
15743 | ||
6dfbb909 | 15744 | /* Set up the list of local specializations. */ |
c8094d83 | 15745 | local_specializations = htab_create (37, |
69f794a7 | 15746 | hash_local_specialization, |
a723baf1 | 15747 | eq_local_specializations, |
6dfbb909 MM |
15748 | NULL); |
15749 | ||
558475f0 | 15750 | /* Set up context. */ |
058b15c1 | 15751 | start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED); |
5566b478 | 15752 | |
a723baf1 MM |
15753 | /* Create substitution entries for the parameters. */ |
15754 | subst_decl = DECL_TEMPLATE_RESULT (template_for_substitution (d)); | |
15755 | tmpl_parm = DECL_ARGUMENTS (subst_decl); | |
15756 | spec_parm = DECL_ARGUMENTS (d); | |
15757 | if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d)) | |
15758 | { | |
15759 | register_local_specialization (spec_parm, tmpl_parm); | |
15760 | spec_parm = skip_artificial_parms_for (d, spec_parm); | |
15761 | tmpl_parm = skip_artificial_parms_for (subst_decl, tmpl_parm); | |
15762 | } | |
5d80a306 | 15763 | while (tmpl_parm && !FUNCTION_PARAMETER_PACK_P (tmpl_parm)) |
a723baf1 MM |
15764 | { |
15765 | register_local_specialization (spec_parm, tmpl_parm); | |
15766 | tmpl_parm = TREE_CHAIN (tmpl_parm); | |
15767 | spec_parm = TREE_CHAIN (spec_parm); | |
15768 | } | |
5d80a306 DG |
15769 | if (tmpl_parm && FUNCTION_PARAMETER_PACK_P (tmpl_parm)) |
15770 | { | |
5d80a306 DG |
15771 | /* Register the (value) argument pack as a specialization of |
15772 | TMPL_PARM, then move on. */ | |
6afcfe0a | 15773 | tree argpack = make_fnparm_pack (spec_parm); |
5d80a306 DG |
15774 | register_local_specialization (argpack, tmpl_parm); |
15775 | tmpl_parm = TREE_CHAIN (tmpl_parm); | |
6afcfe0a | 15776 | spec_parm = NULL_TREE; |
5d80a306 | 15777 | } |
50bc768d | 15778 | gcc_assert (!spec_parm); |
a723baf1 | 15779 | |
558475f0 MM |
15780 | /* Substitute into the body of the function. */ |
15781 | tsubst_expr (DECL_SAVED_TREE (code_pattern), args, | |
015c2c66 MM |
15782 | tf_warning_or_error, tmpl, |
15783 | /*integral_constant_expression_p=*/false); | |
558475f0 | 15784 | |
75407da3 SP |
15785 | /* Set the current input_location to the end of the function |
15786 | so that finish_function knows where we are. */ | |
15787 | input_location = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus; | |
15788 | ||
6dfbb909 MM |
15789 | /* We don't need the local specializations any more. */ |
15790 | htab_delete (local_specializations); | |
6bbf1598 | 15791 | local_specializations = saved_local_specializations; |
6dfbb909 | 15792 | |
4d6abc1c | 15793 | /* Finish the function. */ |
b2dd096b | 15794 | d = finish_function (0); |
8cd2462c | 15795 | expand_or_defer_fn (d); |
5566b478 MS |
15796 | } |
15797 | ||
971cbc14 MM |
15798 | /* We're not deferring instantiation any more. */ |
15799 | TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0; | |
15800 | ||
31a714f6 MM |
15801 | if (need_push) |
15802 | pop_from_top_level (); | |
414ea4aa | 15803 | |
de22184b | 15804 | out: |
82a98427 | 15805 | input_location = saved_loc; |
7d021397 | 15806 | pop_deferring_access_checks (); |
5566b478 | 15807 | pop_tinst_level (); |
a28e3c7f | 15808 | |
297a5329 JM |
15809 | timevar_pop (TV_PARSE); |
15810 | ||
a28e3c7f MS |
15811 | return d; |
15812 | } | |
5566b478 | 15813 | |
0aafb128 | 15814 | /* Run through the list of templates that we wish we could |
35046a54 KL |
15815 | instantiate, and instantiate any we can. RETRIES is the |
15816 | number of times we retry pending template instantiation. */ | |
0aafb128 | 15817 | |
35046a54 KL |
15818 | void |
15819 | instantiate_pending_templates (int retries) | |
0aafb128 | 15820 | { |
0aafb128 | 15821 | int reconsider; |
aad626f7 | 15822 | location_t saved_loc = input_location; |
35046a54 KL |
15823 | |
15824 | /* Instantiating templates may trigger vtable generation. This in turn | |
15825 | may require further template instantiations. We place a limit here | |
15826 | to avoid infinite loop. */ | |
15827 | if (pending_templates && retries >= max_tinst_depth) | |
15828 | { | |
e2c3721c | 15829 | tree decl = pending_templates->tinst->decl; |
96c993a8 | 15830 | |
dee15844 | 15831 | error ("template instantiation depth exceeds maximum of %d" |
96c993a8 JM |
15832 | " instantiating %q+D, possibly from virtual table generation" |
15833 | " (use -ftemplate-depth-NN to increase the maximum)", | |
15834 | max_tinst_depth, decl); | |
15835 | if (TREE_CODE (decl) == FUNCTION_DECL) | |
15836 | /* Pretend that we defined it. */ | |
15837 | DECL_INITIAL (decl) = error_mark_node; | |
35046a54 KL |
15838 | return; |
15839 | } | |
15840 | ||
c8094d83 | 15841 | do |
0aafb128 | 15842 | { |
e2c3721c PB |
15843 | struct pending_template **t = &pending_templates; |
15844 | struct pending_template *last = NULL; | |
0aafb128 | 15845 | reconsider = 0; |
0aafb128 MM |
15846 | while (*t) |
15847 | { | |
e2c3721c PB |
15848 | tree instantiation = reopen_tinst_level ((*t)->tinst); |
15849 | bool complete = false; | |
0aafb128 | 15850 | |
2f939d94 | 15851 | if (TYPE_P (instantiation)) |
0aafb128 MM |
15852 | { |
15853 | tree fn; | |
15854 | ||
d0f062fb | 15855 | if (!COMPLETE_TYPE_P (instantiation)) |
0aafb128 MM |
15856 | { |
15857 | instantiate_class_template (instantiation); | |
15858 | if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation)) | |
c8094d83 | 15859 | for (fn = TYPE_METHODS (instantiation); |
0aafb128 MM |
15860 | fn; |
15861 | fn = TREE_CHAIN (fn)) | |
15862 | if (! DECL_ARTIFICIAL (fn)) | |
3db45ab5 | 15863 | instantiate_decl (fn, |
eba839f9 MM |
15864 | /*defer_ok=*/0, |
15865 | /*expl_inst_class_mem_p=*/false); | |
d0f062fb | 15866 | if (COMPLETE_TYPE_P (instantiation)) |
35046a54 | 15867 | reconsider = 1; |
0aafb128 MM |
15868 | } |
15869 | ||
e2c3721c | 15870 | complete = COMPLETE_TYPE_P (instantiation); |
0aafb128 MM |
15871 | } |
15872 | else | |
15873 | { | |
16d53b64 | 15874 | if (!DECL_TEMPLATE_SPECIALIZATION (instantiation) |
0aafb128 MM |
15875 | && !DECL_TEMPLATE_INSTANTIATED (instantiation)) |
15876 | { | |
3db45ab5 | 15877 | instantiation |
eba839f9 MM |
15878 | = instantiate_decl (instantiation, |
15879 | /*defer_ok=*/0, | |
15880 | /*expl_inst_class_mem_p=*/false); | |
0aafb128 | 15881 | if (DECL_TEMPLATE_INSTANTIATED (instantiation)) |
35046a54 | 15882 | reconsider = 1; |
0aafb128 MM |
15883 | } |
15884 | ||
e2c3721c PB |
15885 | complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation) |
15886 | || DECL_TEMPLATE_INSTANTIATED (instantiation)); | |
15887 | } | |
15888 | ||
15889 | if (complete) | |
15890 | /* If INSTANTIATION has been instantiated, then we don't | |
15891 | need to consider it again in the future. */ | |
15892 | *t = (*t)->next; | |
15893 | else | |
15894 | { | |
15895 | last = *t; | |
15896 | t = &(*t)->next; | |
0aafb128 | 15897 | } |
84e5ca0f | 15898 | tinst_depth = 0; |
e2c3721c | 15899 | current_tinst_level = NULL; |
0aafb128 | 15900 | } |
46ccf50a | 15901 | last_pending_template = last; |
c8094d83 | 15902 | } |
0aafb128 MM |
15903 | while (reconsider); |
15904 | ||
aad626f7 | 15905 | input_location = saved_loc; |
0aafb128 MM |
15906 | } |
15907 | ||
fd74ca0b MM |
15908 | /* Substitute ARGVEC into T, which is a list of initializers for |
15909 | either base class or a non-static data member. The TREE_PURPOSEs | |
15910 | are DECLs, and the TREE_VALUEs are the initializer values. Used by | |
15911 | instantiate_decl. */ | |
4393e105 | 15912 | |
824b9a4c | 15913 | static tree |
3a978d72 | 15914 | tsubst_initializer_list (tree t, tree argvec) |
5566b478 | 15915 | { |
2282d28d | 15916 | tree inits = NULL_TREE; |
5566b478 MS |
15917 | |
15918 | for (; t; t = TREE_CHAIN (t)) | |
15919 | { | |
fd74ca0b MM |
15920 | tree decl; |
15921 | tree init; | |
5d80a306 DG |
15922 | tree expanded_bases = NULL_TREE; |
15923 | tree expanded_arguments = NULL_TREE; | |
15924 | int i, len = 1; | |
fd74ca0b | 15925 | |
5d80a306 DG |
15926 | if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION) |
15927 | { | |
15928 | tree expr; | |
15929 | tree arg; | |
15930 | ||
15931 | /* Expand the base class expansion type into separate base | |
15932 | classes. */ | |
15933 | expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec, | |
15934 | tf_warning_or_error, | |
15935 | NULL_TREE); | |
15936 | if (expanded_bases == error_mark_node) | |
15937 | continue; | |
15938 | ||
15939 | /* We'll be building separate TREE_LISTs of arguments for | |
15940 | each base. */ | |
15941 | len = TREE_VEC_LENGTH (expanded_bases); | |
15942 | expanded_arguments = make_tree_vec (len); | |
15943 | for (i = 0; i < len; i++) | |
15944 | TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE; | |
15945 | ||
15946 | /* Build a dummy EXPR_PACK_EXPANSION that will be used to | |
15947 | expand each argument in the TREE_VALUE of t. */ | |
15948 | expr = make_node (EXPR_PACK_EXPANSION); | |
15949 | PACK_EXPANSION_PARAMETER_PACKS (expr) = | |
15950 | PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t)); | |
15951 | ||
625b6d91 DG |
15952 | if (TREE_VALUE (t) == void_type_node) |
15953 | /* VOID_TYPE_NODE is used to indicate | |
15954 | value-initialization. */ | |
15955 | { | |
15956 | for (i = 0; i < len; i++) | |
15957 | TREE_VEC_ELT (expanded_arguments, i) = void_type_node; | |
15958 | } | |
15959 | else | |
15960 | { | |
15961 | /* Substitute parameter packs into each argument in the | |
15962 | TREE_LIST. */ | |
15963 | in_base_initializer = 1; | |
15964 | for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg)) | |
15965 | { | |
15966 | tree expanded_exprs; | |
15967 | ||
15968 | /* Expand the argument. */ | |
15969 | SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg)); | |
15970 | expanded_exprs | |
15971 | = tsubst_pack_expansion (expr, argvec, | |
15972 | tf_warning_or_error, | |
15973 | NULL_TREE); | |
aecaad79 PC |
15974 | if (expanded_exprs == error_mark_node) |
15975 | continue; | |
625b6d91 DG |
15976 | |
15977 | /* Prepend each of the expanded expressions to the | |
15978 | corresponding TREE_LIST in EXPANDED_ARGUMENTS. */ | |
15979 | for (i = 0; i < len; i++) | |
15980 | { | |
15981 | TREE_VEC_ELT (expanded_arguments, i) = | |
15982 | tree_cons (NULL_TREE, | |
15983 | TREE_VEC_ELT (expanded_exprs, i), | |
15984 | TREE_VEC_ELT (expanded_arguments, i)); | |
15985 | } | |
15986 | } | |
15987 | in_base_initializer = 0; | |
5d80a306 | 15988 | |
625b6d91 DG |
15989 | /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS, |
15990 | since we built them backwards. */ | |
15991 | for (i = 0; i < len; i++) | |
15992 | { | |
15993 | TREE_VEC_ELT (expanded_arguments, i) = | |
15994 | nreverse (TREE_VEC_ELT (expanded_arguments, i)); | |
15995 | } | |
15996 | } | |
5d80a306 | 15997 | } |
1f5a253a | 15998 | |
5d80a306 DG |
15999 | for (i = 0; i < len; ++i) |
16000 | { | |
16001 | if (expanded_bases) | |
16002 | { | |
16003 | decl = TREE_VEC_ELT (expanded_bases, i); | |
16004 | decl = expand_member_init (decl); | |
16005 | init = TREE_VEC_ELT (expanded_arguments, i); | |
16006 | } | |
16007 | else | |
16008 | { | |
16009 | decl = tsubst_copy (TREE_PURPOSE (t), argvec, | |
16010 | tf_warning_or_error, NULL_TREE); | |
16011 | ||
16012 | decl = expand_member_init (decl); | |
16013 | if (decl && !DECL_P (decl)) | |
16014 | in_base_initializer = 1; | |
16015 | ||
16016 | init = tsubst_expr (TREE_VALUE (t), argvec, | |
16017 | tf_warning_or_error, NULL_TREE, | |
16018 | /*integral_constant_expression_p=*/false); | |
16019 | in_base_initializer = 0; | |
16020 | } | |
16021 | ||
16022 | if (decl) | |
16023 | { | |
16024 | init = build_tree_list (decl, init); | |
16025 | TREE_CHAIN (init) = inits; | |
16026 | inits = init; | |
16027 | } | |
16028 | } | |
5566b478 | 16029 | } |
2282d28d | 16030 | return inits; |
5566b478 MS |
16031 | } |
16032 | ||
61a127b3 MM |
16033 | /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */ |
16034 | ||
16035 | static void | |
3a978d72 | 16036 | set_current_access_from_decl (tree decl) |
61a127b3 MM |
16037 | { |
16038 | if (TREE_PRIVATE (decl)) | |
16039 | current_access_specifier = access_private_node; | |
16040 | else if (TREE_PROTECTED (decl)) | |
16041 | current_access_specifier = access_protected_node; | |
16042 | else | |
16043 | current_access_specifier = access_public_node; | |
16044 | } | |
16045 | ||
dbfe2124 MM |
16046 | /* Instantiate an enumerated type. TAG is the template type, NEWTAG |
16047 | is the instantiation (which should have been created with | |
16048 | start_enum) and ARGS are the template arguments to use. */ | |
b87692e5 | 16049 | |
dbfe2124 | 16050 | static void |
3a978d72 | 16051 | tsubst_enum (tree tag, tree newtag, tree args) |
b87692e5 | 16052 | { |
dbfe2124 | 16053 | tree e; |
b87692e5 MS |
16054 | |
16055 | for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e)) | |
16056 | { | |
61a127b3 | 16057 | tree value; |
7b6d72fc MM |
16058 | tree decl; |
16059 | ||
16060 | decl = TREE_VALUE (e); | |
61a127b3 MM |
16061 | /* Note that in a template enum, the TREE_VALUE is the |
16062 | CONST_DECL, not the corresponding INTEGER_CST. */ | |
c8094d83 | 16063 | value = tsubst_expr (DECL_INITIAL (decl), |
015c2c66 MM |
16064 | args, tf_warning_or_error, NULL_TREE, |
16065 | /*integral_constant_expression_p=*/true); | |
61a127b3 MM |
16066 | |
16067 | /* Give this enumeration constant the correct access. */ | |
7b6d72fc | 16068 | set_current_access_from_decl (decl); |
61a127b3 MM |
16069 | |
16070 | /* Actually build the enumerator itself. */ | |
c8094d83 | 16071 | build_enumerator (DECL_NAME (decl), value, newtag); |
dbfe2124 | 16072 | } |
b3d5a58b | 16073 | |
219670f1 | 16074 | finish_enum (newtag); |
f31686a3 RH |
16075 | DECL_SOURCE_LOCATION (TYPE_NAME (newtag)) |
16076 | = DECL_SOURCE_LOCATION (TYPE_NAME (tag)); | |
b87692e5 | 16077 | } |
36a117a5 | 16078 | |
1f6e1acc AS |
16079 | /* DECL is a FUNCTION_DECL that is a template specialization. Return |
16080 | its type -- but without substituting the innermost set of template | |
16081 | arguments. So, innermost set of template parameters will appear in | |
5c74d5b0 | 16082 | the type. */ |
1f6e1acc | 16083 | |
c8094d83 | 16084 | tree |
3a978d72 | 16085 | get_mostly_instantiated_function_type (tree decl) |
1f6e1acc | 16086 | { |
1f6e1acc AS |
16087 | tree fn_type; |
16088 | tree tmpl; | |
16089 | tree targs; | |
16090 | tree tparms; | |
16091 | int parm_depth; | |
16092 | ||
16093 | tmpl = most_general_template (DECL_TI_TEMPLATE (decl)); | |
16094 | targs = DECL_TI_ARGS (decl); | |
16095 | tparms = DECL_TEMPLATE_PARMS (tmpl); | |
16096 | parm_depth = TMPL_PARMS_DEPTH (tparms); | |
16097 | ||
16098 | /* There should be as many levels of arguments as there are levels | |
16099 | of parameters. */ | |
50bc768d | 16100 | gcc_assert (parm_depth == TMPL_ARGS_DEPTH (targs)); |
1f6e1acc AS |
16101 | |
16102 | fn_type = TREE_TYPE (tmpl); | |
1f6e1acc AS |
16103 | |
16104 | if (parm_depth == 1) | |
16105 | /* No substitution is necessary. */ | |
16106 | ; | |
16107 | else | |
16108 | { | |
fae6e246 | 16109 | int i, save_access_control; |
1f6e1acc AS |
16110 | tree partial_args; |
16111 | ||
16112 | /* Replace the innermost level of the TARGS with NULL_TREEs to | |
dc957d14 | 16113 | let tsubst know not to substitute for those parameters. */ |
1f6e1acc AS |
16114 | partial_args = make_tree_vec (TREE_VEC_LENGTH (targs)); |
16115 | for (i = 1; i < TMPL_ARGS_DEPTH (targs); ++i) | |
16116 | SET_TMPL_ARGS_LEVEL (partial_args, i, | |
16117 | TMPL_ARGS_LEVEL (targs, i)); | |
16118 | SET_TMPL_ARGS_LEVEL (partial_args, | |
16119 | TMPL_ARGS_DEPTH (targs), | |
16120 | make_tree_vec (DECL_NTPARMS (tmpl))); | |
16121 | ||
fae6e246 RH |
16122 | /* Disable access control as this function is used only during |
16123 | name-mangling. */ | |
16124 | save_access_control = flag_access_control; | |
16125 | flag_access_control = 0; | |
5c74d5b0 | 16126 | |
9579624e | 16127 | ++processing_template_decl; |
1f6e1acc AS |
16128 | /* Now, do the (partial) substitution to figure out the |
16129 | appropriate function type. */ | |
c2ea3a40 | 16130 | fn_type = tsubst (fn_type, partial_args, tf_error, NULL_TREE); |
9579624e | 16131 | --processing_template_decl; |
1f6e1acc AS |
16132 | |
16133 | /* Substitute into the template parameters to obtain the real | |
16134 | innermost set of parameters. This step is important if the | |
16135 | innermost set of template parameters contains value | |
16136 | parameters whose types depend on outer template parameters. */ | |
16137 | TREE_VEC_LENGTH (partial_args)--; | |
c2ea3a40 | 16138 | tparms = tsubst_template_parms (tparms, partial_args, tf_error); |
1f6e1acc | 16139 | |
fae6e246 | 16140 | flag_access_control = save_access_control; |
5c74d5b0 | 16141 | } |
1f6e1acc AS |
16142 | |
16143 | return fn_type; | |
16144 | } | |
669ec2b4 | 16145 | |
cb753e49 | 16146 | /* Return truthvalue if we're processing a template different from |
dc957d14 | 16147 | the last one involved in diagnostics. */ |
cb753e49 | 16148 | int |
3a978d72 | 16149 | problematic_instantiation_changed (void) |
cb753e49 GDR |
16150 | { |
16151 | return last_template_error_tick != tinst_level_tick; | |
16152 | } | |
16153 | ||
16154 | /* Remember current template involved in diagnostics. */ | |
16155 | void | |
3a978d72 | 16156 | record_last_problematic_instantiation (void) |
cb753e49 GDR |
16157 | { |
16158 | last_template_error_tick = tinst_level_tick; | |
16159 | } | |
16160 | ||
e2c3721c | 16161 | struct tinst_level * |
3a978d72 | 16162 | current_instantiation (void) |
cb753e49 GDR |
16163 | { |
16164 | return current_tinst_level; | |
16165 | } | |
db3f4e4e NS |
16166 | |
16167 | /* [temp.param] Check that template non-type parm TYPE is of an allowable | |
838dfd8a | 16168 | type. Return zero for ok, nonzero for disallowed. Issue error and |
c2ea3a40 | 16169 | warning messages under control of COMPLAIN. */ |
db3f4e4e NS |
16170 | |
16171 | static int | |
3a978d72 | 16172 | invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain) |
db3f4e4e | 16173 | { |
550a799d | 16174 | if (INTEGRAL_OR_ENUMERATION_TYPE_P (type)) |
db3f4e4e NS |
16175 | return 0; |
16176 | else if (POINTER_TYPE_P (type)) | |
16177 | return 0; | |
a5ac359a | 16178 | else if (TYPE_PTR_TO_MEMBER_P (type)) |
db3f4e4e | 16179 | return 0; |
db3f4e4e NS |
16180 | else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM) |
16181 | return 0; | |
16182 | else if (TREE_CODE (type) == TYPENAME_TYPE) | |
16183 | return 0; | |
c8094d83 | 16184 | |
c2ea3a40 | 16185 | if (complain & tf_error) |
0f51ccfc | 16186 | error ("%q#T is not a valid type for a template constant parameter", type); |
db3f4e4e NS |
16187 | return 1; |
16188 | } | |
e2500fed | 16189 | |
5552b43c MM |
16190 | /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type]. |
16191 | Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/ | |
1fb3244a | 16192 | |
5552b43c MM |
16193 | static bool |
16194 | dependent_type_p_r (tree type) | |
1fb3244a MM |
16195 | { |
16196 | tree scope; | |
16197 | ||
1fb3244a MM |
16198 | /* [temp.dep.type] |
16199 | ||
16200 | A type is dependent if it is: | |
16201 | ||
6615c446 JO |
16202 | -- a template parameter. Template template parameters are types |
16203 | for us (since TYPE_P holds true for them) so we handle | |
16204 | them here. */ | |
c8094d83 | 16205 | if (TREE_CODE (type) == TEMPLATE_TYPE_PARM |
6bf92cb6 | 16206 | || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM) |
1fb3244a MM |
16207 | return true; |
16208 | /* -- a qualified-id with a nested-name-specifier which contains a | |
0cbd7506 | 16209 | class-name that names a dependent type or whose unqualified-id |
1fb3244a MM |
16210 | names a dependent type. */ |
16211 | if (TREE_CODE (type) == TYPENAME_TYPE) | |
16212 | return true; | |
16213 | /* -- a cv-qualified type where the cv-unqualified type is | |
0cbd7506 | 16214 | dependent. */ |
1fb3244a MM |
16215 | type = TYPE_MAIN_VARIANT (type); |
16216 | /* -- a compound type constructed from any dependent type. */ | |
a5ac359a | 16217 | if (TYPE_PTR_TO_MEMBER_P (type)) |
1fb3244a | 16218 | return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type)) |
c8094d83 | 16219 | || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE |
1fb3244a MM |
16220 | (type))); |
16221 | else if (TREE_CODE (type) == POINTER_TYPE | |
16222 | || TREE_CODE (type) == REFERENCE_TYPE) | |
16223 | return dependent_type_p (TREE_TYPE (type)); | |
16224 | else if (TREE_CODE (type) == FUNCTION_TYPE | |
16225 | || TREE_CODE (type) == METHOD_TYPE) | |
16226 | { | |
16227 | tree arg_type; | |
16228 | ||
16229 | if (dependent_type_p (TREE_TYPE (type))) | |
16230 | return true; | |
c8094d83 MS |
16231 | for (arg_type = TYPE_ARG_TYPES (type); |
16232 | arg_type; | |
1fb3244a MM |
16233 | arg_type = TREE_CHAIN (arg_type)) |
16234 | if (dependent_type_p (TREE_VALUE (arg_type))) | |
16235 | return true; | |
16236 | return false; | |
16237 | } | |
16238 | /* -- an array type constructed from any dependent type or whose | |
0cbd7506 | 16239 | size is specified by a constant expression that is |
1fb3244a MM |
16240 | value-dependent. */ |
16241 | if (TREE_CODE (type) == ARRAY_TYPE) | |
16242 | { | |
16243 | if (TYPE_DOMAIN (type) | |
82390eb6 | 16244 | && dependent_type_p (TYPE_DOMAIN (type))) |
1fb3244a MM |
16245 | return true; |
16246 | return dependent_type_p (TREE_TYPE (type)); | |
16247 | } | |
82390eb6 JM |
16248 | else if (TREE_CODE (type) == INTEGER_TYPE |
16249 | && !TREE_CONSTANT (TYPE_MAX_VALUE (type))) | |
16250 | { | |
16251 | /* If this is the TYPE_DOMAIN of an array type, consider it | |
c1165535 JM |
16252 | dependent. We already checked for value-dependence in |
16253 | compute_array_index_type. */ | |
16254 | return type_dependent_expression_p (TYPE_MAX_VALUE (type)); | |
82390eb6 | 16255 | } |
c8094d83 | 16256 | |
1fb3244a | 16257 | /* -- a template-id in which either the template name is a template |
86306a6b NS |
16258 | parameter ... */ |
16259 | if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM) | |
1fb3244a | 16260 | return true; |
86306a6b | 16261 | /* ... or any of the template arguments is a dependent type or |
04c06002 | 16262 | an expression that is type-dependent or value-dependent. */ |
86306a6b | 16263 | else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type) |
c8094d83 | 16264 | && (any_dependent_template_arguments_p |
7e99327d | 16265 | (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type))))) |
1fb3244a | 16266 | return true; |
c8094d83 | 16267 | |
3ad6a8e1 DG |
16268 | /* All TYPEOF_TYPEs and DECLTYPE_TYPEs are dependent; if the |
16269 | argument of the `typeof' expression is not type-dependent, then | |
16270 | it should already been have resolved. */ | |
16271 | if (TREE_CODE (type) == TYPEOF_TYPE | |
16272 | || TREE_CODE (type) == DECLTYPE_TYPE) | |
1fb3244a | 16273 | return true; |
c8094d83 | 16274 | |
5d80a306 DG |
16275 | /* A template argument pack is dependent if any of its packed |
16276 | arguments are. */ | |
16277 | if (TREE_CODE (type) == TYPE_ARGUMENT_PACK) | |
16278 | { | |
16279 | tree args = ARGUMENT_PACK_ARGS (type); | |
16280 | int i, len = TREE_VEC_LENGTH (args); | |
16281 | for (i = 0; i < len; ++i) | |
16282 | if (dependent_template_arg_p (TREE_VEC_ELT (args, i))) | |
16283 | return true; | |
16284 | } | |
16285 | ||
16286 | /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must | |
16287 | be template parameters. */ | |
16288 | if (TREE_CODE (type) == TYPE_PACK_EXPANSION) | |
16289 | return true; | |
16290 | ||
1fb3244a MM |
16291 | /* The standard does not specifically mention types that are local |
16292 | to template functions or local classes, but they should be | |
16293 | considered dependent too. For example: | |
16294 | ||
c8094d83 | 16295 | template <int I> void f() { |
0cbd7506 | 16296 | enum E { a = I }; |
1fb3244a MM |
16297 | S<sizeof (E)> s; |
16298 | } | |
16299 | ||
16300 | The size of `E' cannot be known until the value of `I' has been | |
16301 | determined. Therefore, `E' must be considered dependent. */ | |
16302 | scope = TYPE_CONTEXT (type); | |
16303 | if (scope && TYPE_P (scope)) | |
16304 | return dependent_type_p (scope); | |
16305 | else if (scope && TREE_CODE (scope) == FUNCTION_DECL) | |
16306 | return type_dependent_expression_p (scope); | |
16307 | ||
16308 | /* Other types are non-dependent. */ | |
16309 | return false; | |
16310 | } | |
16311 | ||
5552b43c MM |
16312 | /* Returns TRUE if TYPE is dependent, in the sense of |
16313 | [temp.dep.type]. */ | |
16314 | ||
16315 | bool | |
16316 | dependent_type_p (tree type) | |
16317 | { | |
16318 | /* If there are no template parameters in scope, then there can't be | |
16319 | any dependent types. */ | |
16320 | if (!processing_template_decl) | |
e7e93965 MM |
16321 | { |
16322 | /* If we are not processing a template, then nobody should be | |
16323 | providing us with a dependent type. */ | |
16324 | gcc_assert (type); | |
0257eee5 | 16325 | gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type)); |
e7e93965 MM |
16326 | return false; |
16327 | } | |
5552b43c MM |
16328 | |
16329 | /* If the type is NULL, we have not computed a type for the entity | |
16330 | in question; in that case, the type is dependent. */ | |
16331 | if (!type) | |
16332 | return true; | |
16333 | ||
16334 | /* Erroneous types can be considered non-dependent. */ | |
16335 | if (type == error_mark_node) | |
16336 | return false; | |
16337 | ||
16338 | /* If we have not already computed the appropriate value for TYPE, | |
16339 | do so now. */ | |
16340 | if (!TYPE_DEPENDENT_P_VALID (type)) | |
16341 | { | |
16342 | TYPE_DEPENDENT_P (type) = dependent_type_p_r (type); | |
16343 | TYPE_DEPENDENT_P_VALID (type) = 1; | |
16344 | } | |
16345 | ||
16346 | return TYPE_DEPENDENT_P (type); | |
16347 | } | |
16348 | ||
1cb801bc JM |
16349 | /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any |
16350 | lookup. In other words, a dependent type that is not the current | |
16351 | instantiation. */ | |
16352 | ||
16353 | bool | |
16354 | dependent_scope_p (tree scope) | |
16355 | { | |
b77ba909 JM |
16356 | return (scope && TYPE_P (scope) && dependent_type_p (scope) |
16357 | && !currently_open_class (scope)); | |
1cb801bc JM |
16358 | } |
16359 | ||
8d83f792 MM |
16360 | /* Returns TRUE if EXPRESSION is dependent, according to CRITERION. */ |
16361 | ||
16362 | static bool | |
16363 | dependent_scope_ref_p (tree expression, bool criterion (tree)) | |
16364 | { | |
16365 | tree scope; | |
16366 | tree name; | |
16367 | ||
50bc768d | 16368 | gcc_assert (TREE_CODE (expression) == SCOPE_REF); |
8d83f792 MM |
16369 | |
16370 | if (!TYPE_P (TREE_OPERAND (expression, 0))) | |
16371 | return true; | |
16372 | ||
16373 | scope = TREE_OPERAND (expression, 0); | |
16374 | name = TREE_OPERAND (expression, 1); | |
16375 | ||
16376 | /* [temp.dep.expr] | |
16377 | ||
16378 | An id-expression is type-dependent if it contains a | |
16379 | nested-name-specifier that contains a class-name that names a | |
16380 | dependent type. */ | |
1cb801bc | 16381 | /* The suggested resolution to Core Issue 224 implies that if the |
8d83f792 MM |
16382 | qualifying type is the current class, then we must peek |
16383 | inside it. */ | |
c8094d83 | 16384 | if (DECL_P (name) |
8d83f792 MM |
16385 | && currently_open_class (scope) |
16386 | && !criterion (name)) | |
16387 | return false; | |
16388 | if (dependent_type_p (scope)) | |
16389 | return true; | |
16390 | ||
16391 | return false; | |
16392 | } | |
16393 | ||
20929c7f | 16394 | /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of |
fe89d797 MM |
16395 | [temp.dep.constexpr]. EXPRESSION is already known to be a constant |
16396 | expression. */ | |
1fb3244a | 16397 | |
14d22dd6 | 16398 | bool |
1fb3244a MM |
16399 | value_dependent_expression_p (tree expression) |
16400 | { | |
16401 | if (!processing_template_decl) | |
16402 | return false; | |
16403 | ||
16404 | /* A name declared with a dependent type. */ | |
7416ab02 | 16405 | if (DECL_P (expression) && type_dependent_expression_p (expression)) |
1fb3244a | 16406 | return true; |
c8094d83 | 16407 | |
7416ab02 NS |
16408 | switch (TREE_CODE (expression)) |
16409 | { | |
16410 | case IDENTIFIER_NODE: | |
16411 | /* A name that has not been looked up -- must be dependent. */ | |
16412 | return true; | |
16413 | ||
16414 | case TEMPLATE_PARM_INDEX: | |
16415 | /* A non-type template parm. */ | |
16416 | return true; | |
16417 | ||
16418 | case CONST_DECL: | |
16419 | /* A non-type template parm. */ | |
16420 | if (DECL_TEMPLATE_PARM_P (expression)) | |
d17811fd | 16421 | return true; |
3387721b | 16422 | return value_dependent_expression_p (DECL_INITIAL (expression)); |
d36d5600 | 16423 | |
7416ab02 | 16424 | case VAR_DECL: |
c8094d83 | 16425 | /* A constant with integral or enumeration type and is initialized |
0cbd7506 | 16426 | with an expression that is value-dependent. */ |
7416ab02 NS |
16427 | if (DECL_INITIAL (expression) |
16428 | && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (expression)) | |
16429 | && value_dependent_expression_p (DECL_INITIAL (expression))) | |
16430 | return true; | |
16431 | return false; | |
16432 | ||
16433 | case DYNAMIC_CAST_EXPR: | |
16434 | case STATIC_CAST_EXPR: | |
16435 | case CONST_CAST_EXPR: | |
16436 | case REINTERPRET_CAST_EXPR: | |
16437 | case CAST_EXPR: | |
16438 | /* These expressions are value-dependent if the type to which | |
0cbd7506 MS |
16439 | the cast occurs is dependent or the expression being casted |
16440 | is value-dependent. */ | |
7416ab02 NS |
16441 | { |
16442 | tree type = TREE_TYPE (expression); | |
c8094d83 | 16443 | |
7416ab02 NS |
16444 | if (dependent_type_p (type)) |
16445 | return true; | |
c8094d83 | 16446 | |
7416ab02 NS |
16447 | /* A functional cast has a list of operands. */ |
16448 | expression = TREE_OPERAND (expression, 0); | |
16449 | if (!expression) | |
16450 | { | |
16451 | /* If there are no operands, it must be an expression such | |
16452 | as "int()". This should not happen for aggregate types | |
16453 | because it would form non-constant expressions. */ | |
16454 | gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type)); | |
c8094d83 | 16455 | |
7416ab02 NS |
16456 | return false; |
16457 | } | |
c8094d83 | 16458 | |
7416ab02 | 16459 | if (TREE_CODE (expression) == TREE_LIST) |
32a11c08 | 16460 | return any_value_dependent_elements_p (expression); |
c8094d83 | 16461 | |
d17811fd | 16462 | return value_dependent_expression_p (expression); |
7416ab02 | 16463 | } |
c8094d83 | 16464 | |
7416ab02 NS |
16465 | case SIZEOF_EXPR: |
16466 | case ALIGNOF_EXPR: | |
16467 | /* A `sizeof' expression is value-dependent if the operand is | |
5d80a306 | 16468 | type-dependent or is a pack expansion. */ |
d17811fd | 16469 | expression = TREE_OPERAND (expression, 0); |
5d80a306 DG |
16470 | if (PACK_EXPANSION_P (expression)) |
16471 | return true; | |
16472 | else if (TYPE_P (expression)) | |
d17811fd MM |
16473 | return dependent_type_p (expression); |
16474 | return type_dependent_expression_p (expression); | |
100d337a | 16475 | |
7416ab02 NS |
16476 | case SCOPE_REF: |
16477 | return dependent_scope_ref_p (expression, value_dependent_expression_p); | |
100d337a | 16478 | |
7416ab02 NS |
16479 | case COMPONENT_REF: |
16480 | return (value_dependent_expression_p (TREE_OPERAND (expression, 0)) | |
16481 | || value_dependent_expression_p (TREE_OPERAND (expression, 1))); | |
16482 | ||
16483 | case CALL_EXPR: | |
fe89d797 MM |
16484 | /* A CALL_EXPR may appear in a constant expression if it is a |
16485 | call to a builtin function, e.g., __builtin_constant_p. All | |
16486 | such calls are value-dependent. */ | |
16487 | return true; | |
7416ab02 | 16488 | |
5d80a306 DG |
16489 | case NONTYPE_ARGUMENT_PACK: |
16490 | /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument | |
16491 | is value-dependent. */ | |
16492 | { | |
16493 | tree values = ARGUMENT_PACK_ARGS (expression); | |
16494 | int i, len = TREE_VEC_LENGTH (values); | |
16495 | ||
16496 | for (i = 0; i < len; ++i) | |
16497 | if (value_dependent_expression_p (TREE_VEC_ELT (values, i))) | |
16498 | return true; | |
16499 | ||
16500 | return false; | |
16501 | } | |
16502 | ||
cb68ec50 PC |
16503 | case TRAIT_EXPR: |
16504 | { | |
16505 | tree type2 = TRAIT_EXPR_TYPE2 (expression); | |
16506 | return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression)) | |
16507 | || (type2 ? dependent_type_p (type2) : false)); | |
16508 | } | |
16509 | ||
f01ec37d SM |
16510 | case MODOP_EXPR: |
16511 | return ((value_dependent_expression_p (TREE_OPERAND (expression, 0))) | |
16512 | || (value_dependent_expression_p (TREE_OPERAND (expression, 2)))); | |
16513 | ||
7416ab02 NS |
16514 | default: |
16515 | /* A constant expression is value-dependent if any subexpression is | |
0cbd7506 | 16516 | value-dependent. */ |
1fb3244a MM |
16517 | switch (TREE_CODE_CLASS (TREE_CODE (expression))) |
16518 | { | |
7416ab02 | 16519 | case tcc_reference: |
6615c446 | 16520 | case tcc_unary: |
c8094d83 | 16521 | return (value_dependent_expression_p |
1fb3244a | 16522 | (TREE_OPERAND (expression, 0))); |
c8094d83 | 16523 | |
6615c446 JO |
16524 | case tcc_comparison: |
16525 | case tcc_binary: | |
c8094d83 | 16526 | return ((value_dependent_expression_p |
1fb3244a | 16527 | (TREE_OPERAND (expression, 0))) |
c8094d83 | 16528 | || (value_dependent_expression_p |
1fb3244a | 16529 | (TREE_OPERAND (expression, 1)))); |
c8094d83 | 16530 | |
6615c446 | 16531 | case tcc_expression: |
5039610b | 16532 | case tcc_vl_exp: |
1fb3244a MM |
16533 | { |
16534 | int i; | |
5039610b | 16535 | for (i = 0; i < TREE_OPERAND_LENGTH (expression); ++i) |
14d22dd6 MM |
16536 | /* In some cases, some of the operands may be missing. |
16537 | (For example, in the case of PREDECREMENT_EXPR, the | |
16538 | amount to increment by may be missing.) That doesn't | |
16539 | make the expression dependent. */ | |
16540 | if (TREE_OPERAND (expression, i) | |
16541 | && (value_dependent_expression_p | |
16542 | (TREE_OPERAND (expression, i)))) | |
1fb3244a MM |
16543 | return true; |
16544 | return false; | |
16545 | } | |
c8094d83 | 16546 | |
6615c446 | 16547 | default: |
7416ab02 | 16548 | break; |
1fb3244a MM |
16549 | } |
16550 | } | |
c8094d83 | 16551 | |
1fb3244a MM |
16552 | /* The expression is not value-dependent. */ |
16553 | return false; | |
16554 | } | |
16555 | ||
16556 | /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of | |
16557 | [temp.dep.expr]. */ | |
16558 | ||
16559 | bool | |
3a978d72 | 16560 | type_dependent_expression_p (tree expression) |
1fb3244a MM |
16561 | { |
16562 | if (!processing_template_decl) | |
16563 | return false; | |
16564 | ||
7efa3e22 NS |
16565 | if (expression == error_mark_node) |
16566 | return false; | |
10b1d5e7 MM |
16567 | |
16568 | /* An unresolved name is always dependent. */ | |
b01e6d2b JM |
16569 | if (TREE_CODE (expression) == IDENTIFIER_NODE |
16570 | || TREE_CODE (expression) == USING_DECL) | |
10b1d5e7 | 16571 | return true; |
c8094d83 | 16572 | |
1fb3244a MM |
16573 | /* Some expression forms are never type-dependent. */ |
16574 | if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR | |
16575 | || TREE_CODE (expression) == SIZEOF_EXPR | |
16576 | || TREE_CODE (expression) == ALIGNOF_EXPR | |
cb68ec50 | 16577 | || TREE_CODE (expression) == TRAIT_EXPR |
1fb3244a MM |
16578 | || TREE_CODE (expression) == TYPEID_EXPR |
16579 | || TREE_CODE (expression) == DELETE_EXPR | |
16580 | || TREE_CODE (expression) == VEC_DELETE_EXPR | |
16581 | || TREE_CODE (expression) == THROW_EXPR) | |
16582 | return false; | |
16583 | ||
16584 | /* The types of these expressions depends only on the type to which | |
16585 | the cast occurs. */ | |
16586 | if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR | |
16587 | || TREE_CODE (expression) == STATIC_CAST_EXPR | |
16588 | || TREE_CODE (expression) == CONST_CAST_EXPR | |
16589 | || TREE_CODE (expression) == REINTERPRET_CAST_EXPR | |
16590 | || TREE_CODE (expression) == CAST_EXPR) | |
16591 | return dependent_type_p (TREE_TYPE (expression)); | |
d17811fd | 16592 | |
1fb3244a MM |
16593 | /* The types of these expressions depends only on the type created |
16594 | by the expression. */ | |
d17811fd MM |
16595 | if (TREE_CODE (expression) == NEW_EXPR |
16596 | || TREE_CODE (expression) == VEC_NEW_EXPR) | |
09d2f85f KL |
16597 | { |
16598 | /* For NEW_EXPR tree nodes created inside a template, either | |
16599 | the object type itself or a TREE_LIST may appear as the | |
16600 | operand 1. */ | |
16601 | tree type = TREE_OPERAND (expression, 1); | |
16602 | if (TREE_CODE (type) == TREE_LIST) | |
16603 | /* This is an array type. We need to check array dimensions | |
16604 | as well. */ | |
16605 | return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type))) | |
16606 | || value_dependent_expression_p | |
16607 | (TREE_OPERAND (TREE_VALUE (type), 1)); | |
16608 | else | |
16609 | return dependent_type_p (type); | |
16610 | } | |
1fb3244a | 16611 | |
5a57f1b2 JM |
16612 | if (TREE_CODE (expression) == SCOPE_REF |
16613 | && dependent_scope_ref_p (expression, | |
16614 | type_dependent_expression_p)) | |
16615 | return true; | |
16616 | ||
16617 | if (TREE_CODE (expression) == FUNCTION_DECL | |
16618 | && DECL_LANG_SPECIFIC (expression) | |
16619 | && DECL_TEMPLATE_INFO (expression) | |
16620 | && (any_dependent_template_arguments_p | |
16621 | (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression))))) | |
16622 | return true; | |
16623 | ||
16624 | if (TREE_CODE (expression) == TEMPLATE_DECL | |
16625 | && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression)) | |
16626 | return false; | |
16627 | ||
c2acde1e JM |
16628 | if (TREE_CODE (expression) == STMT_EXPR) |
16629 | expression = stmt_expr_value_expr (expression); | |
16630 | ||
86a09a9e JM |
16631 | if (BRACE_ENCLOSED_INITIALIZER_P (expression)) |
16632 | { | |
16633 | tree elt; | |
16634 | unsigned i; | |
16635 | ||
16636 | FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt) | |
16637 | { | |
16638 | if (type_dependent_expression_p (elt)) | |
16639 | return true; | |
16640 | } | |
16641 | return false; | |
16642 | } | |
16643 | ||
d17811fd MM |
16644 | if (TREE_TYPE (expression) == unknown_type_node) |
16645 | { | |
16646 | if (TREE_CODE (expression) == ADDR_EXPR) | |
16647 | return type_dependent_expression_p (TREE_OPERAND (expression, 0)); | |
c353b8e3 MM |
16648 | if (TREE_CODE (expression) == COMPONENT_REF |
16649 | || TREE_CODE (expression) == OFFSET_REF) | |
6cb89308 NS |
16650 | { |
16651 | if (type_dependent_expression_p (TREE_OPERAND (expression, 0))) | |
16652 | return true; | |
16653 | expression = TREE_OPERAND (expression, 1); | |
16654 | if (TREE_CODE (expression) == IDENTIFIER_NODE) | |
16655 | return false; | |
16656 | } | |
3601f003 KL |
16657 | /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */ |
16658 | if (TREE_CODE (expression) == SCOPE_REF) | |
16659 | return false; | |
c8094d83 | 16660 | |
d17811fd MM |
16661 | if (TREE_CODE (expression) == BASELINK) |
16662 | expression = BASELINK_FUNCTIONS (expression); | |
c8094d83 | 16663 | |
d17811fd MM |
16664 | if (TREE_CODE (expression) == TEMPLATE_ID_EXPR) |
16665 | { | |
ee3071ef NS |
16666 | if (any_dependent_template_arguments_p |
16667 | (TREE_OPERAND (expression, 1))) | |
d17811fd MM |
16668 | return true; |
16669 | expression = TREE_OPERAND (expression, 0); | |
16670 | } | |
b207d6e2 MM |
16671 | gcc_assert (TREE_CODE (expression) == OVERLOAD |
16672 | || TREE_CODE (expression) == FUNCTION_DECL); | |
c8094d83 | 16673 | |
315fb5db | 16674 | while (expression) |
d17811fd | 16675 | { |
315fb5db NS |
16676 | if (type_dependent_expression_p (OVL_CURRENT (expression))) |
16677 | return true; | |
16678 | expression = OVL_NEXT (expression); | |
d17811fd | 16679 | } |
315fb5db | 16680 | return false; |
d17811fd | 16681 | } |
c8094d83 | 16682 | |
3ce5fa4f | 16683 | gcc_assert (TREE_CODE (expression) != TYPE_DECL); |
3db45ab5 | 16684 | |
1fb3244a MM |
16685 | return (dependent_type_p (TREE_TYPE (expression))); |
16686 | } | |
5094a795 JM |
16687 | |
16688 | /* Like type_dependent_expression_p, but it also works while not processing | |
16689 | a template definition, i.e. during substitution or mangling. */ | |
16690 | ||
16691 | bool | |
16692 | type_dependent_expression_p_push (tree expr) | |
16693 | { | |
16694 | bool b; | |
16695 | ++processing_template_decl; | |
16696 | b = type_dependent_expression_p (expr); | |
16697 | --processing_template_decl; | |
16698 | return b; | |
16699 | } | |
1fb3244a | 16700 | |
c166b898 | 16701 | /* Returns TRUE if ARGS contains a type-dependent expression. */ |
1fb3244a MM |
16702 | |
16703 | bool | |
c166b898 | 16704 | any_type_dependent_arguments_p (const VEC(tree,gc) *args) |
d17811fd | 16705 | { |
c166b898 ILT |
16706 | unsigned int i; |
16707 | tree arg; | |
7efa3e22 | 16708 | |
c166b898 ILT |
16709 | for (i = 0; VEC_iterate (tree, args, i, arg); ++i) |
16710 | { | |
7efa3e22 | 16711 | if (type_dependent_expression_p (arg)) |
d17811fd | 16712 | return true; |
d17811fd MM |
16713 | } |
16714 | return false; | |
16715 | } | |
16716 | ||
32a11c08 MM |
16717 | /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are |
16718 | expressions) contains any value-dependent expressions. */ | |
16719 | ||
16720 | bool | |
58f9752a | 16721 | any_value_dependent_elements_p (const_tree list) |
32a11c08 MM |
16722 | { |
16723 | for (; list; list = TREE_CHAIN (list)) | |
16724 | if (value_dependent_expression_p (TREE_VALUE (list))) | |
16725 | return true; | |
16726 | ||
16727 | return false; | |
16728 | } | |
16729 | ||
d17811fd MM |
16730 | /* Returns TRUE if the ARG (a template argument) is dependent. */ |
16731 | ||
5d80a306 | 16732 | bool |
1fb3244a MM |
16733 | dependent_template_arg_p (tree arg) |
16734 | { | |
16735 | if (!processing_template_decl) | |
16736 | return false; | |
16737 | ||
16738 | if (TREE_CODE (arg) == TEMPLATE_DECL | |
16739 | || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM) | |
16740 | return dependent_template_p (arg); | |
5d80a306 DG |
16741 | else if (ARGUMENT_PACK_P (arg)) |
16742 | { | |
16743 | tree args = ARGUMENT_PACK_ARGS (arg); | |
16744 | int i, len = TREE_VEC_LENGTH (args); | |
16745 | for (i = 0; i < len; ++i) | |
16746 | { | |
16747 | if (dependent_template_arg_p (TREE_VEC_ELT (args, i))) | |
16748 | return true; | |
16749 | } | |
16750 | ||
16751 | return false; | |
16752 | } | |
1fb3244a MM |
16753 | else if (TYPE_P (arg)) |
16754 | return dependent_type_p (arg); | |
16755 | else | |
16756 | return (type_dependent_expression_p (arg) | |
16757 | || value_dependent_expression_p (arg)); | |
16758 | } | |
16759 | ||
06d40de8 DG |
16760 | /* Returns true if ARGS (a collection of template arguments) contains |
16761 | any types that require structural equality testing. */ | |
16762 | ||
16763 | bool | |
16764 | any_template_arguments_need_structural_equality_p (tree args) | |
16765 | { | |
16766 | int i; | |
16767 | int j; | |
16768 | ||
16769 | if (!args) | |
16770 | return false; | |
16771 | if (args == error_mark_node) | |
16772 | return true; | |
16773 | ||
16774 | for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i) | |
16775 | { | |
16776 | tree level = TMPL_ARGS_LEVEL (args, i + 1); | |
16777 | for (j = 0; j < TREE_VEC_LENGTH (level); ++j) | |
16778 | { | |
16779 | tree arg = TREE_VEC_ELT (level, j); | |
5d80a306 DG |
16780 | tree packed_args = NULL_TREE; |
16781 | int k, len = 1; | |
16782 | ||
16783 | if (ARGUMENT_PACK_P (arg)) | |
16784 | { | |
16785 | /* Look inside the argument pack. */ | |
16786 | packed_args = ARGUMENT_PACK_ARGS (arg); | |
16787 | len = TREE_VEC_LENGTH (packed_args); | |
16788 | } | |
16789 | ||
16790 | for (k = 0; k < len; ++k) | |
16791 | { | |
16792 | if (packed_args) | |
16793 | arg = TREE_VEC_ELT (packed_args, k); | |
16794 | ||
16795 | if (error_operand_p (arg)) | |
16796 | return true; | |
16797 | else if (TREE_CODE (arg) == TEMPLATE_DECL | |
16798 | || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM) | |
16799 | continue; | |
16800 | else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg)) | |
16801 | return true; | |
16802 | else if (!TYPE_P (arg) && TREE_TYPE (arg) | |
16803 | && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg))) | |
16804 | return true; | |
16805 | } | |
06d40de8 DG |
16806 | } |
16807 | } | |
16808 | ||
16809 | return false; | |
16810 | } | |
16811 | ||
d17811fd MM |
16812 | /* Returns true if ARGS (a collection of template arguments) contains |
16813 | any dependent arguments. */ | |
1fb3244a | 16814 | |
d17811fd | 16815 | bool |
58f9752a | 16816 | any_dependent_template_arguments_p (const_tree args) |
1fb3244a | 16817 | { |
bf12d54d | 16818 | int i; |
c353b8e3 MM |
16819 | int j; |
16820 | ||
d17811fd MM |
16821 | if (!args) |
16822 | return false; | |
7e497d0c VR |
16823 | if (args == error_mark_node) |
16824 | return true; | |
d17811fd | 16825 | |
c353b8e3 MM |
16826 | for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i) |
16827 | { | |
58f9752a | 16828 | const_tree level = TMPL_ARGS_LEVEL (args, i + 1); |
c353b8e3 MM |
16829 | for (j = 0; j < TREE_VEC_LENGTH (level); ++j) |
16830 | if (dependent_template_arg_p (TREE_VEC_ELT (level, j))) | |
16831 | return true; | |
16832 | } | |
1fb3244a | 16833 | |
1fb3244a MM |
16834 | return false; |
16835 | } | |
16836 | ||
16837 | /* Returns TRUE if the template TMPL is dependent. */ | |
16838 | ||
16839 | bool | |
16840 | dependent_template_p (tree tmpl) | |
16841 | { | |
b95cc51a MM |
16842 | if (TREE_CODE (tmpl) == OVERLOAD) |
16843 | { | |
16844 | while (tmpl) | |
16845 | { | |
16846 | if (dependent_template_p (OVL_FUNCTION (tmpl))) | |
16847 | return true; | |
16848 | tmpl = OVL_CHAIN (tmpl); | |
16849 | } | |
16850 | return false; | |
16851 | } | |
16852 | ||
1fb3244a MM |
16853 | /* Template template parameters are dependent. */ |
16854 | if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl) | |
16855 | || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM) | |
16856 | return true; | |
27ab0504 | 16857 | /* So are names that have not been looked up. */ |
acccf788 MM |
16858 | if (TREE_CODE (tmpl) == SCOPE_REF |
16859 | || TREE_CODE (tmpl) == IDENTIFIER_NODE) | |
d17811fd | 16860 | return true; |
1fb3244a MM |
16861 | /* So are member templates of dependent classes. */ |
16862 | if (TYPE_P (CP_DECL_CONTEXT (tmpl))) | |
16863 | return dependent_type_p (DECL_CONTEXT (tmpl)); | |
16864 | return false; | |
16865 | } | |
16866 | ||
d17811fd MM |
16867 | /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */ |
16868 | ||
16869 | bool | |
16870 | dependent_template_id_p (tree tmpl, tree args) | |
16871 | { | |
16872 | return (dependent_template_p (tmpl) | |
16873 | || any_dependent_template_arguments_p (args)); | |
16874 | } | |
16875 | ||
a68ab351 JJ |
16876 | /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors |
16877 | is dependent. */ | |
16878 | ||
16879 | bool | |
16880 | dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv) | |
16881 | { | |
16882 | int i; | |
16883 | ||
16884 | if (!processing_template_decl) | |
16885 | return false; | |
16886 | ||
16887 | for (i = 0; i < TREE_VEC_LENGTH (declv); i++) | |
16888 | { | |
16889 | tree decl = TREE_VEC_ELT (declv, i); | |
16890 | tree init = TREE_VEC_ELT (initv, i); | |
16891 | tree cond = TREE_VEC_ELT (condv, i); | |
16892 | tree incr = TREE_VEC_ELT (incrv, i); | |
16893 | ||
16894 | if (type_dependent_expression_p (decl)) | |
16895 | return true; | |
16896 | ||
16897 | if (init && type_dependent_expression_p (init)) | |
16898 | return true; | |
16899 | ||
16900 | if (type_dependent_expression_p (cond)) | |
16901 | return true; | |
16902 | ||
16903 | if (COMPARISON_CLASS_P (cond) | |
16904 | && (type_dependent_expression_p (TREE_OPERAND (cond, 0)) | |
16905 | || type_dependent_expression_p (TREE_OPERAND (cond, 1)))) | |
16906 | return true; | |
16907 | ||
16908 | if (TREE_CODE (incr) == MODOP_EXPR) | |
16909 | { | |
16910 | if (type_dependent_expression_p (TREE_OPERAND (incr, 0)) | |
16911 | || type_dependent_expression_p (TREE_OPERAND (incr, 2))) | |
16912 | return true; | |
16913 | } | |
16914 | else if (type_dependent_expression_p (incr)) | |
16915 | return true; | |
16916 | else if (TREE_CODE (incr) == MODIFY_EXPR) | |
16917 | { | |
16918 | if (type_dependent_expression_p (TREE_OPERAND (incr, 0))) | |
16919 | return true; | |
16920 | else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1))) | |
16921 | { | |
16922 | tree t = TREE_OPERAND (incr, 1); | |
16923 | if (type_dependent_expression_p (TREE_OPERAND (t, 0)) | |
16924 | || type_dependent_expression_p (TREE_OPERAND (t, 1))) | |
16925 | return true; | |
16926 | } | |
16927 | } | |
16928 | } | |
16929 | ||
16930 | return false; | |
16931 | } | |
16932 | ||
14d22dd6 | 16933 | /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the |
4195a767 NS |
16934 | TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if |
16935 | no such TYPE can be found. Note that this function peers inside | |
16936 | uninstantiated templates and therefore should be used only in | |
16937 | extremely limited situations. ONLY_CURRENT_P restricts this | |
16938 | peering to the currently open classes hierarchy (which is required | |
16939 | when comparing types). */ | |
14d22dd6 MM |
16940 | |
16941 | tree | |
16942 | resolve_typename_type (tree type, bool only_current_p) | |
16943 | { | |
16944 | tree scope; | |
16945 | tree name; | |
16946 | tree decl; | |
16947 | int quals; | |
4514aa8c | 16948 | tree pushed_scope; |
4195a767 | 16949 | tree result; |
14d22dd6 | 16950 | |
50bc768d | 16951 | gcc_assert (TREE_CODE (type) == TYPENAME_TYPE); |
14d22dd6 MM |
16952 | |
16953 | scope = TYPE_CONTEXT (type); | |
d0940d56 DS |
16954 | /* Usually the non-qualified identifier of a TYPENAME_TYPE is |
16955 | TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of | |
16956 | a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing | |
16957 | the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified | |
16958 | identifier of the TYPENAME_TYPE anymore. | |
16959 | So by getting the TYPE_IDENTIFIER of the _main declaration_ of the | |
16960 | TYPENAME_TYPE instead, we avoid messing up with a possible | |
16961 | typedef variant case. */ | |
16962 | name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type)); | |
14d22dd6 MM |
16963 | |
16964 | /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve | |
16965 | it first before we can figure out what NAME refers to. */ | |
16966 | if (TREE_CODE (scope) == TYPENAME_TYPE) | |
16967 | scope = resolve_typename_type (scope, only_current_p); | |
16968 | /* If we don't know what SCOPE refers to, then we cannot resolve the | |
16969 | TYPENAME_TYPE. */ | |
4195a767 NS |
16970 | if (TREE_CODE (scope) == TYPENAME_TYPE) |
16971 | return type; | |
14d22dd6 MM |
16972 | /* If the SCOPE is a template type parameter, we have no way of |
16973 | resolving the name. */ | |
16974 | if (TREE_CODE (scope) == TEMPLATE_TYPE_PARM) | |
16975 | return type; | |
16976 | /* If the SCOPE is not the current instantiation, there's no reason | |
16977 | to look inside it. */ | |
16978 | if (only_current_p && !currently_open_class (scope)) | |
4195a767 | 16979 | return type; |
7a20d689 JM |
16980 | /* If SCOPE isn't the template itself, it will not have a valid |
16981 | TYPE_FIELDS list. */ | |
16982 | if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope))) | |
16983 | /* scope is either the template itself or a compatible instantiation | |
16984 | like X<T>, so look up the name in the original template. */ | |
16985 | scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope); | |
16986 | else | |
16987 | /* scope is a partial instantiation, so we can't do the lookup or we | |
16988 | will lose the template arguments. */ | |
16989 | return type; | |
14d22dd6 MM |
16990 | /* Enter the SCOPE so that name lookup will be resolved as if we |
16991 | were in the class definition. In particular, SCOPE will no | |
16992 | longer be considered a dependent type. */ | |
4514aa8c | 16993 | pushed_scope = push_scope (scope); |
14d22dd6 | 16994 | /* Look up the declaration. */ |
86ac0575 | 16995 | decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true); |
4195a767 NS |
16996 | |
16997 | result = NULL_TREE; | |
16998 | ||
14d22dd6 MM |
16999 | /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to |
17000 | find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */ | |
17001 | if (!decl) | |
4195a767 | 17002 | /*nop*/; |
14d22dd6 MM |
17003 | else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == IDENTIFIER_NODE |
17004 | && TREE_CODE (decl) == TYPE_DECL) | |
4195a767 NS |
17005 | { |
17006 | result = TREE_TYPE (decl); | |
17007 | if (result == error_mark_node) | |
17008 | result = NULL_TREE; | |
17009 | } | |
14d22dd6 MM |
17010 | else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == TEMPLATE_ID_EXPR |
17011 | && DECL_CLASS_TEMPLATE_P (decl)) | |
17012 | { | |
17013 | tree tmpl; | |
17014 | tree args; | |
17015 | /* Obtain the template and the arguments. */ | |
17016 | tmpl = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 0); | |
17017 | args = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 1); | |
17018 | /* Instantiate the template. */ | |
4195a767 NS |
17019 | result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE, |
17020 | /*entering_scope=*/0, | |
17021 | tf_error | tf_user); | |
17022 | if (result == error_mark_node) | |
17023 | result = NULL_TREE; | |
14d22dd6 | 17024 | } |
4195a767 | 17025 | |
14d22dd6 | 17026 | /* Leave the SCOPE. */ |
4514aa8c NS |
17027 | if (pushed_scope) |
17028 | pop_scope (pushed_scope); | |
14d22dd6 | 17029 | |
4195a767 NS |
17030 | /* If we failed to resolve it, return the original typename. */ |
17031 | if (!result) | |
17032 | return type; | |
17033 | ||
17034 | /* If lookup found a typename type, resolve that too. */ | |
17035 | if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result)) | |
17036 | { | |
17037 | /* Ill-formed programs can cause infinite recursion here, so we | |
17038 | must catch that. */ | |
17039 | TYPENAME_IS_RESOLVING_P (type) = 1; | |
17040 | result = resolve_typename_type (result, only_current_p); | |
17041 | TYPENAME_IS_RESOLVING_P (type) = 0; | |
17042 | } | |
17043 | ||
17044 | /* Qualify the resulting type. */ | |
17045 | quals = cp_type_quals (type); | |
17046 | if (quals) | |
17047 | result = cp_build_qualified_type (result, cp_type_quals (result) | quals); | |
17048 | ||
17049 | return result; | |
14d22dd6 MM |
17050 | } |
17051 | ||
d17811fd MM |
17052 | /* EXPR is an expression which is not type-dependent. Return a proxy |
17053 | for EXPR that can be used to compute the types of larger | |
17054 | expressions containing EXPR. */ | |
17055 | ||
17056 | tree | |
17057 | build_non_dependent_expr (tree expr) | |
17058 | { | |
0deb916c MM |
17059 | tree inner_expr; |
17060 | ||
c8094d83 | 17061 | /* Preserve null pointer constants so that the type of things like |
d17811fd MM |
17062 | "p == 0" where "p" is a pointer can be determined. */ |
17063 | if (null_ptr_cst_p (expr)) | |
17064 | return expr; | |
17065 | /* Preserve OVERLOADs; the functions must be available to resolve | |
17066 | types. */ | |
c497db75 | 17067 | inner_expr = expr; |
c2acde1e JM |
17068 | if (TREE_CODE (inner_expr) == STMT_EXPR) |
17069 | inner_expr = stmt_expr_value_expr (inner_expr); | |
c497db75 AO |
17070 | if (TREE_CODE (inner_expr) == ADDR_EXPR) |
17071 | inner_expr = TREE_OPERAND (inner_expr, 0); | |
17072 | if (TREE_CODE (inner_expr) == COMPONENT_REF) | |
17073 | inner_expr = TREE_OPERAND (inner_expr, 1); | |
2226e997 | 17074 | if (is_overloaded_fn (inner_expr) |
6439fffd | 17075 | || TREE_CODE (inner_expr) == OFFSET_REF) |
d17811fd | 17076 | return expr; |
5ae9ba3e MM |
17077 | /* There is no need to return a proxy for a variable. */ |
17078 | if (TREE_CODE (expr) == VAR_DECL) | |
17079 | return expr; | |
7433e6d4 MM |
17080 | /* Preserve string constants; conversions from string constants to |
17081 | "char *" are allowed, even though normally a "const char *" | |
17082 | cannot be used to initialize a "char *". */ | |
17083 | if (TREE_CODE (expr) == STRING_CST) | |
17084 | return expr; | |
b7c707d1 MM |
17085 | /* Preserve arithmetic constants, as an optimization -- there is no |
17086 | reason to create a new node. */ | |
17087 | if (TREE_CODE (expr) == INTEGER_CST || TREE_CODE (expr) == REAL_CST) | |
17088 | return expr; | |
9b7be7b5 MM |
17089 | /* Preserve THROW_EXPRs -- all throw-expressions have type "void". |
17090 | There is at least one place where we want to know that a | |
17091 | particular expression is a throw-expression: when checking a ?: | |
17092 | expression, there are special rules if the second or third | |
878cbb73 | 17093 | argument is a throw-expression. */ |
9b7be7b5 MM |
17094 | if (TREE_CODE (expr) == THROW_EXPR) |
17095 | return expr; | |
47d4c811 NS |
17096 | |
17097 | if (TREE_CODE (expr) == COND_EXPR) | |
f293ce4b RS |
17098 | return build3 (COND_EXPR, |
17099 | TREE_TYPE (expr), | |
17100 | TREE_OPERAND (expr, 0), | |
c8094d83 | 17101 | (TREE_OPERAND (expr, 1) |
f293ce4b RS |
17102 | ? build_non_dependent_expr (TREE_OPERAND (expr, 1)) |
17103 | : build_non_dependent_expr (TREE_OPERAND (expr, 0))), | |
17104 | build_non_dependent_expr (TREE_OPERAND (expr, 2))); | |
8e1daa34 NS |
17105 | if (TREE_CODE (expr) == COMPOUND_EXPR |
17106 | && !COMPOUND_EXPR_OVERLOADED (expr)) | |
f293ce4b RS |
17107 | return build2 (COMPOUND_EXPR, |
17108 | TREE_TYPE (expr), | |
17109 | TREE_OPERAND (expr, 0), | |
17110 | build_non_dependent_expr (TREE_OPERAND (expr, 1))); | |
c8094d83 | 17111 | |
fdeff563 NS |
17112 | /* If the type is unknown, it can't really be non-dependent */ |
17113 | gcc_assert (TREE_TYPE (expr) != unknown_type_node); | |
3db45ab5 | 17114 | |
c8094d83 | 17115 | /* Otherwise, build a NON_DEPENDENT_EXPR. |
d17811fd MM |
17116 | |
17117 | REFERENCE_TYPEs are not stripped for expressions in templates | |
17118 | because doing so would play havoc with mangling. Consider, for | |
17119 | example: | |
17120 | ||
c8094d83 | 17121 | template <typename T> void f<T& g>() { g(); } |
d17811fd MM |
17122 | |
17123 | In the body of "f", the expression for "g" will have | |
17124 | REFERENCE_TYPE, even though the standard says that it should | |
17125 | not. The reason is that we must preserve the syntactic form of | |
17126 | the expression so that mangling (say) "f<g>" inside the body of | |
17127 | "f" works out correctly. Therefore, the REFERENCE_TYPE is | |
17128 | stripped here. */ | |
018a5803 | 17129 | return build1 (NON_DEPENDENT_EXPR, non_reference (TREE_TYPE (expr)), expr); |
d17811fd MM |
17130 | } |
17131 | ||
c166b898 ILT |
17132 | /* ARGS is a vector of expressions as arguments to a function call. |
17133 | Replace the arguments with equivalent non-dependent expressions. | |
17134 | This modifies ARGS in place. */ | |
d17811fd | 17135 | |
c166b898 ILT |
17136 | void |
17137 | make_args_non_dependent (VEC(tree,gc) *args) | |
d17811fd | 17138 | { |
c166b898 ILT |
17139 | unsigned int ix; |
17140 | tree arg; | |
d17811fd | 17141 | |
c166b898 ILT |
17142 | for (ix = 0; VEC_iterate (tree, args, ix, arg); ++ix) |
17143 | { | |
17144 | tree newarg = build_non_dependent_expr (arg); | |
17145 | if (newarg != arg) | |
17146 | VEC_replace (tree, args, ix, newarg); | |
17147 | } | |
d17811fd MM |
17148 | } |
17149 | ||
86a09a9e JM |
17150 | /* Returns a type which represents 'auto'. We use a TEMPLATE_TYPE_PARM |
17151 | with a level one deeper than the actual template parms. */ | |
17152 | ||
17153 | tree | |
17154 | make_auto (void) | |
17155 | { | |
17156 | tree au; | |
17157 | ||
17158 | /* ??? Is it worth caching this for multiple autos at the same level? */ | |
17159 | au = cxx_make_type (TEMPLATE_TYPE_PARM); | |
c2255bc4 AH |
17160 | TYPE_NAME (au) = build_decl (BUILTINS_LOCATION, |
17161 | TYPE_DECL, get_identifier ("auto"), au); | |
86a09a9e JM |
17162 | TYPE_STUB_DECL (au) = TYPE_NAME (au); |
17163 | TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index | |
17164 | (0, processing_template_decl + 1, processing_template_decl + 1, | |
17165 | TYPE_NAME (au), NULL_TREE); | |
17166 | TYPE_CANONICAL (au) = canonical_type_parameter (au); | |
17167 | DECL_ARTIFICIAL (TYPE_NAME (au)) = 1; | |
17168 | SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au)); | |
17169 | ||
17170 | return au; | |
17171 | } | |
17172 | ||
17173 | /* Replace auto in TYPE with std::initializer_list<auto>. */ | |
17174 | ||
17175 | static tree | |
17176 | listify_autos (tree type, tree auto_node) | |
17177 | { | |
17178 | tree std_init_list = namespace_binding | |
17179 | (get_identifier ("initializer_list"), std_node); | |
17180 | tree argvec; | |
17181 | tree init_auto; | |
17182 | if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list)) | |
17183 | { | |
17184 | error ("deducing auto from brace-enclosed initializer list requires " | |
17185 | "#include <initializer_list>"); | |
17186 | return error_mark_node; | |
17187 | } | |
17188 | argvec = make_tree_vec (1); | |
17189 | TREE_VEC_ELT (argvec, 0) = auto_node; | |
17190 | init_auto = lookup_template_class (std_init_list, argvec, NULL_TREE, | |
17191 | NULL_TREE, 0, tf_warning_or_error); | |
17192 | ||
17193 | TREE_VEC_ELT (argvec, 0) = init_auto; | |
17194 | if (processing_template_decl) | |
17195 | argvec = add_to_template_args (current_template_args (), argvec); | |
17196 | return tsubst (type, argvec, tf_warning_or_error, NULL_TREE); | |
17197 | } | |
17198 | ||
17199 | /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced | |
17200 | from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */ | |
17201 | ||
17202 | tree | |
17203 | do_auto_deduction (tree type, tree init, tree auto_node) | |
17204 | { | |
c166b898 ILT |
17205 | tree parms, tparms, targs; |
17206 | tree args[1]; | |
86a09a9e JM |
17207 | int val; |
17208 | ||
17209 | /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto | |
17210 | with either a new invented type template parameter U or, if the | |
17211 | initializer is a braced-init-list (8.5.4), with | |
17212 | std::initializer_list<U>. */ | |
17213 | if (BRACE_ENCLOSED_INITIALIZER_P (init)) | |
17214 | type = listify_autos (type, auto_node); | |
17215 | ||
17216 | parms = build_tree_list (NULL_TREE, type); | |
c166b898 | 17217 | args[0] = init; |
86a09a9e JM |
17218 | tparms = make_tree_vec (1); |
17219 | targs = make_tree_vec (1); | |
17220 | TREE_VEC_ELT (tparms, 0) | |
17221 | = build_tree_list (NULL_TREE, TYPE_NAME (auto_node)); | |
c166b898 | 17222 | val = type_unification_real (tparms, targs, parms, args, 1, 0, |
86a09a9e JM |
17223 | DEDUCE_CALL, LOOKUP_NORMAL); |
17224 | if (val > 0) | |
17225 | { | |
17226 | error ("unable to deduce %qT from %qE", type, init); | |
17227 | return error_mark_node; | |
17228 | } | |
17229 | ||
17230 | if (processing_template_decl) | |
17231 | targs = add_to_template_args (current_template_args (), targs); | |
17232 | return tsubst (type, targs, tf_warning_or_error, NULL_TREE); | |
17233 | } | |
17234 | ||
f57ca1ea JM |
17235 | /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the |
17236 | result. */ | |
17237 | ||
17238 | tree | |
17239 | splice_late_return_type (tree type, tree late_return_type) | |
17240 | { | |
17241 | tree argvec; | |
17242 | ||
17243 | if (late_return_type == NULL_TREE) | |
17244 | return type; | |
17245 | argvec = make_tree_vec (1); | |
17246 | TREE_VEC_ELT (argvec, 0) = late_return_type; | |
17247 | if (processing_template_decl) | |
17248 | argvec = add_to_template_args (current_template_args (), argvec); | |
17249 | return tsubst (type, argvec, tf_warning_or_error, NULL_TREE); | |
17250 | } | |
17251 | ||
86a09a9e JM |
17252 | /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto'. */ |
17253 | ||
17254 | bool | |
17255 | is_auto (const_tree type) | |
17256 | { | |
17257 | if (TREE_CODE (type) == TEMPLATE_TYPE_PARM | |
17258 | && TYPE_IDENTIFIER (type) == get_identifier ("auto")) | |
17259 | return true; | |
17260 | else | |
17261 | return false; | |
17262 | } | |
17263 | ||
17264 | /* Returns true iff TYPE contains a use of 'auto'. Since auto can only | |
17265 | appear as a type-specifier for the declaration in question, we don't | |
17266 | have to look through the whole type. */ | |
17267 | ||
17268 | tree | |
17269 | type_uses_auto (tree type) | |
17270 | { | |
17271 | enum tree_code code; | |
17272 | if (is_auto (type)) | |
17273 | return type; | |
17274 | ||
17275 | code = TREE_CODE (type); | |
17276 | ||
17277 | if (code == POINTER_TYPE || code == REFERENCE_TYPE | |
17278 | || code == OFFSET_TYPE || code == FUNCTION_TYPE | |
17279 | || code == METHOD_TYPE || code == ARRAY_TYPE) | |
17280 | return type_uses_auto (TREE_TYPE (type)); | |
17281 | ||
17282 | if (TYPE_PTRMEMFUNC_P (type)) | |
17283 | return type_uses_auto (TREE_TYPE (TREE_TYPE | |
17284 | (TYPE_PTRMEMFUNC_FN_TYPE (type)))); | |
17285 | ||
17286 | return NULL_TREE; | |
17287 | } | |
17288 | ||
2eb25c98 DS |
17289 | /* For a given template T, return the list of typedefs referenced |
17290 | in T for which access check is needed at T instantiation time. | |
17291 | T is either a FUNCTION_DECL or a RECORD_TYPE. | |
17292 | Those typedefs were added to T by the function | |
17293 | append_type_to_template_for_access_check. */ | |
17294 | ||
17295 | tree | |
17296 | get_types_needing_access_check (tree t) | |
17297 | { | |
17298 | tree ti, result = NULL_TREE; | |
17299 | ||
17300 | if (!t || t == error_mark_node) | |
17301 | return t; | |
17302 | ||
17303 | if (!(ti = get_template_info (t))) | |
17304 | return NULL_TREE; | |
17305 | ||
17306 | if (TREE_CODE (t) == RECORD_TYPE || TREE_CODE (t) == FUNCTION_DECL) | |
17307 | { | |
17308 | if (!TI_TEMPLATE (ti)) | |
17309 | return NULL_TREE; | |
17310 | ||
17311 | result = TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti); | |
17312 | } | |
17313 | ||
17314 | return result; | |
17315 | } | |
17316 | ||
17317 | /* Append the typedef TYPE_DECL used in template T to a list of typedefs | |
17318 | tied to T. That list of typedefs will be access checked at | |
17319 | T instantiation time. | |
17320 | T is either a FUNCTION_DECL or a RECORD_TYPE. | |
17321 | TYPE_DECL is a TYPE_DECL node representing a typedef. | |
17322 | SCOPE is the scope through which TYPE_DECL is accessed. | |
17323 | ||
17324 | This function is a subroutine of | |
17325 | append_type_to_template_for_access_check. */ | |
17326 | ||
17327 | static void | |
17328 | append_type_to_template_for_access_check_1 (tree t, | |
17329 | tree type_decl, | |
17330 | tree scope) | |
17331 | { | |
17332 | tree ti; | |
17333 | ||
17334 | if (!t || t == error_mark_node) | |
17335 | return; | |
17336 | ||
17337 | gcc_assert ((TREE_CODE (t) == FUNCTION_DECL | |
17338 | || TREE_CODE (t) == RECORD_TYPE) | |
17339 | && type_decl | |
17340 | && TREE_CODE (type_decl) == TYPE_DECL | |
17341 | && scope); | |
17342 | ||
17343 | if (!(ti = get_template_info (t))) | |
17344 | return; | |
17345 | ||
17346 | gcc_assert (TI_TEMPLATE (ti)); | |
17347 | ||
17348 | TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti) = | |
17349 | tree_cons (type_decl, scope, TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti)); | |
17350 | } | |
17351 | ||
d0940d56 | 17352 | /* Append TYPE_DECL to the template TEMPL. |
2eb25c98 | 17353 | TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL. |
d0940d56 | 17354 | At TEMPL instanciation time, TYPE_DECL will be checked to see |
2eb25c98 DS |
17355 | if it can be accessed through SCOPE. |
17356 | ||
17357 | e.g. consider the following code snippet: | |
17358 | ||
17359 | class C | |
17360 | { | |
17361 | typedef int myint; | |
17362 | }; | |
17363 | ||
17364 | template<class U> struct S | |
17365 | { | |
17366 | C::myint mi; | |
17367 | }; | |
17368 | ||
17369 | S<char> s; | |
17370 | ||
17371 | At S<char> instantiation time, we need to check the access of C::myint | |
17372 | In other words, we need to check the access of the myint typedef through | |
17373 | the C scope. For that purpose, this function will add the myint typedef | |
17374 | and the scope C through which its being accessed to a list of typedefs | |
17375 | tied to the template S. That list will be walked at template instantiation | |
17376 | time and access check performed on each typedefs it contains. | |
17377 | Note that this particular code snippet should yield an error because | |
17378 | myint is private to C. */ | |
d0940d56 DS |
17379 | |
17380 | void | |
17381 | append_type_to_template_for_access_check (tree templ, | |
17382 | tree type_decl, | |
17383 | tree scope) | |
17384 | { | |
2eb25c98 | 17385 | tree node; |
d0940d56 | 17386 | |
2eb25c98 | 17387 | gcc_assert (type_decl && (TREE_CODE (type_decl) == TYPE_DECL)); |
d0940d56 | 17388 | |
2eb25c98 DS |
17389 | /* Make sure we don't append the type to the template twice. */ |
17390 | for (node = get_types_needing_access_check (templ); | |
d0940d56 DS |
17391 | node; |
17392 | node = TREE_CHAIN (node)) | |
17393 | { | |
17394 | tree decl = TREE_PURPOSE (node); | |
17395 | tree type_scope = TREE_VALUE (node); | |
17396 | ||
17397 | if (decl == type_decl && type_scope == scope) | |
17398 | return; | |
17399 | } | |
17400 | ||
2eb25c98 | 17401 | append_type_to_template_for_access_check_1 (templ, type_decl, scope); |
d0940d56 DS |
17402 | } |
17403 | ||
e2500fed | 17404 | #include "gt-cp-pt.h" |