]>
Commit | Line | Data |
---|---|---|
471086d6 | 1 | /* Handle parameterized types (templates) for GNU C++. |
c792eccc | 2 | Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, |
c05c4be3 | 3 | 2001, 2002, 2003, 2004, 2005, 2007, 2008 Free Software Foundation, Inc. |
471086d6 | 4 | Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing. |
d2a15a12 | 5 | Rewritten by Jason Merrill (jason@cygnus.com). |
471086d6 | 6 | |
6f0d25a6 | 7 | This file is part of GCC. |
471086d6 | 8 | |
6f0d25a6 | 9 | GCC is free software; you can redistribute it and/or modify |
471086d6 | 10 | it under the terms of the GNU General Public License as published by |
aa139c3f | 11 | the Free Software Foundation; either version 3, or (at your option) |
471086d6 | 12 | any later version. |
13 | ||
6f0d25a6 | 14 | GCC is distributed in the hope that it will be useful, |
471086d6 | 15 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | GNU General Public License for more details. | |
18 | ||
19 | You should have received a copy of the GNU General Public License | |
aa139c3f | 20 | along with GCC; see the file COPYING3. If not see |
21 | <http://www.gnu.org/licenses/>. */ | |
471086d6 | 22 | |
23 | /* Known bugs or deficiencies include: | |
96624a9e | 24 | |
96624a9e | 25 | all methods must be provided in header files; can't use a source |
26 | file that contains only the method templates and "just win". */ | |
471086d6 | 27 | |
28 | #include "config.h" | |
b3ef7553 | 29 | #include "system.h" |
805e22b2 | 30 | #include "coretypes.h" |
31 | #include "tm.h" | |
471086d6 | 32 | #include "obstack.h" |
471086d6 | 33 | #include "tree.h" |
c6224531 | 34 | #include "pointer-set.h" |
471086d6 | 35 | #include "flags.h" |
7a4e126b | 36 | #include "c-common.h" |
471086d6 | 37 | #include "cp-tree.h" |
7a4e126b | 38 | #include "cp-objcp-common.h" |
1e3b023c | 39 | #include "tree-inline.h" |
471086d6 | 40 | #include "decl.h" |
3f7d79e4 | 41 | #include "output.h" |
71ccdfff | 42 | #include "except.h" |
2a4e40b0 | 43 | #include "toplev.h" |
d226d13f | 44 | #include "rtl.h" |
3a37334e | 45 | #include "timevar.h" |
2363ef00 | 46 | #include "tree-iterator.h" |
e52a9dbc | 47 | #include "vecprim.h" |
71ccdfff | 48 | |
c906a2a7 | 49 | /* The type of functions taking a tree, and some additional data, and |
50 | returning an int. */ | |
807be5b4 | 51 | typedef int (*tree_fn_t) (tree, void*); |
c906a2a7 | 52 | |
cec1f6a6 | 53 | /* The PENDING_TEMPLATES is a TREE_LIST of templates whose |
54 | instantiations have been deferred, either because their definitions | |
c5dd8e06 | 55 | were not yet available, or because we were putting off doing the work. */ |
56 | struct pending_template GTY (()) { | |
57 | struct pending_template *next; | |
58 | struct tinst_level *tinst; | |
59 | }; | |
60 | ||
61 | static GTY(()) struct pending_template *pending_templates; | |
62 | static GTY(()) struct pending_template *last_pending_template; | |
d3ce0f51 | 63 | |
29926476 | 64 | int processing_template_parmlist; |
b1cfe2be | 65 | static int template_header_count; |
66 | ||
1f3233d1 | 67 | static GTY(()) tree saved_trees; |
e52a9dbc | 68 | static VEC(int,heap) *inline_parm_levels; |
668ae905 | 69 | |
c5dd8e06 | 70 | static GTY(()) struct tinst_level *current_tinst_level; |
04d89d04 | 71 | |
7a468236 | 72 | static GTY(()) tree saved_access_scope; |
73 | ||
0d42c4e1 | 74 | /* Live only within one (recursive) call to tsubst_expr. We use |
75 | this to pass the statement expression node from the STMT_EXPR | |
76 | to the EXPR_STMT that is its result. */ | |
77 | static tree cur_stmt_expr; | |
78 | ||
db1ad296 | 79 | /* A map from local variable declarations in the body of the template |
80 | presently being instantiated to the corresponding instantiated | |
81 | local variables. */ | |
82 | static htab_t local_specializations; | |
83 | ||
35796121 | 84 | /* Contains canonical template parameter types. The vector is indexed by |
6753bca0 | 85 | the TEMPLATE_TYPE_IDX of the template parameter. Each element is a |
86 | TREE_LIST, whose TREE_VALUEs contain the canonical template | |
87 | parameters of various types and levels. */ | |
88 | static GTY(()) VEC(tree,gc) *canonical_template_parms; | |
89 | ||
32b5f03e | 90 | #define UNIFY_ALLOW_NONE 0 |
91 | #define UNIFY_ALLOW_MORE_CV_QUAL 1 | |
92 | #define UNIFY_ALLOW_LESS_CV_QUAL 2 | |
93 | #define UNIFY_ALLOW_DERIVED 4 | |
acf0e4b3 | 94 | #define UNIFY_ALLOW_INTEGER 8 |
7e0d59b2 | 95 | #define UNIFY_ALLOW_OUTER_LEVEL 16 |
efa31275 | 96 | #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32 |
97 | #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64 | |
32b5f03e | 98 | |
807be5b4 | 99 | static void push_access_scope (tree); |
100 | static void pop_access_scope (tree); | |
9788fee1 | 101 | static bool resolve_overloaded_unification (tree, tree, tree, tree, |
102 | unification_kind_t, int); | |
807be5b4 | 103 | static int try_one_overload (tree, tree, tree, tree, tree, |
b44599c0 | 104 | unification_kind_t, int, bool); |
807be5b4 | 105 | static int unify (tree, tree, tree, tree, int); |
106 | static void add_pending_template (tree); | |
66b60ad2 | 107 | static int push_tinst_level (tree); |
108 | static void pop_tinst_level (void); | |
c5dd8e06 | 109 | static tree reopen_tinst_level (struct tinst_level *); |
807be5b4 | 110 | static tree tsubst_initializer_list (tree, tree); |
807be5b4 | 111 | static tree get_class_bindings (tree, tree, tree); |
074ab442 | 112 | static tree coerce_template_parms (tree, tree, tree, tsubst_flags_t, |
36653a30 | 113 | bool, bool); |
807be5b4 | 114 | static void tsubst_enum (tree, tree, tree); |
115 | static tree add_to_template_args (tree, tree); | |
116 | static tree add_outermost_template_args (tree, tree); | |
117 | static bool check_instantiated_args (tree, tree, tsubst_flags_t); | |
63949b38 | 118 | static int maybe_adjust_types_for_deduction (unification_kind_t, tree*, tree*, |
119 | tree); | |
807be5b4 | 120 | static int type_unification_real (tree, tree, tree, tree, |
db3bf8f6 | 121 | int, unification_kind_t, int); |
807be5b4 | 122 | static void note_template_header (int); |
b312a686 | 123 | static tree convert_nontype_argument_function (tree, tree); |
807be5b4 | 124 | static tree convert_nontype_argument (tree, tree); |
125 | static tree convert_template_argument (tree, tree, tree, | |
126 | tsubst_flags_t, int, tree); | |
c6224531 | 127 | static int for_each_template_parm (tree, tree_fn_t, void*, |
6510331e | 128 | struct pointer_set_t*, bool); |
d95d815d | 129 | static tree expand_template_argument_pack (tree); |
807be5b4 | 130 | static tree build_template_parm_index (int, int, int, tree, tree); |
9cf51856 | 131 | static bool inline_needs_template_parms (tree); |
807be5b4 | 132 | static void push_inline_template_parms_recursive (tree, int); |
807be5b4 | 133 | static tree retrieve_local_specialization (tree); |
807be5b4 | 134 | static void register_local_specialization (tree, tree); |
864f2267 | 135 | static tree reduce_template_parm_level (tree, tree, int, tree, tsubst_flags_t); |
807be5b4 | 136 | static int mark_template_parm (tree, void *); |
137 | static int template_parm_this_level_p (tree, void *); | |
138 | static tree tsubst_friend_function (tree, tree); | |
139 | static tree tsubst_friend_class (tree, tree); | |
140 | static int can_complete_type_without_circularity (tree); | |
022b46b4 | 141 | static tree get_bindings (tree, tree, tree, bool); |
807be5b4 | 142 | static int template_decl_level (tree); |
143 | static int check_cv_quals_for_unify (int, tree, tree); | |
d95d815d | 144 | static void template_parm_level_and_index (tree, int*, int*); |
145 | static int unify_pack_expansion (tree, tree, tree, tree, int, bool, bool); | |
3fa5de5b | 146 | static tree tsubst_template_arg (tree, tree, tsubst_flags_t, tree); |
147 | static tree tsubst_template_args (tree, tree, tsubst_flags_t, tree); | |
807be5b4 | 148 | static tree tsubst_template_parms (tree, tree, tsubst_flags_t); |
149 | static void regenerate_decl_from_template (tree, tree); | |
807be5b4 | 150 | static tree most_specialized_class (tree, tree); |
807be5b4 | 151 | static tree tsubst_aggr_type (tree, tree, tsubst_flags_t, tree, int); |
807be5b4 | 152 | static tree tsubst_arg_types (tree, tree, tsubst_flags_t, tree); |
153 | static tree tsubst_function_type (tree, tree, tsubst_flags_t, tree); | |
6d9bff9f | 154 | static bool check_specialization_scope (void); |
807be5b4 | 155 | static tree process_partial_specialization (tree); |
156 | static void set_current_access_from_decl (tree); | |
807be5b4 | 157 | static tree get_template_base (tree, tree, tree, tree); |
807be5b4 | 158 | static tree try_class_unification (tree, tree, tree, tree); |
159 | static int coerce_template_template_parms (tree, tree, tsubst_flags_t, | |
160 | tree, tree); | |
e5dab245 | 161 | static bool template_template_parm_bindings_ok_p (tree, tree); |
807be5b4 | 162 | static int template_args_equal (tree, tree); |
163 | static void tsubst_default_arguments (tree); | |
164 | static tree for_each_template_parm_r (tree *, int *, void *); | |
165 | static tree copy_default_args_to_explicit_spec_1 (tree, tree); | |
166 | static void copy_default_args_to_explicit_spec (tree); | |
167 | static int invalid_nontype_parm_type_p (tree, tsubst_flags_t); | |
0a3b29ad | 168 | static int eq_local_specializations (const void *, const void *); |
d95d815d | 169 | static bool dependent_template_arg_p (tree); |
6753bca0 | 170 | static bool any_template_arguments_need_structural_equality_p (tree); |
7f77d1f0 | 171 | static bool dependent_type_p_r (tree); |
5f6526e1 | 172 | static tree tsubst (tree, tree, tsubst_flags_t, tree); |
bde9ebf7 | 173 | static tree tsubst_expr (tree, tree, tsubst_flags_t, tree, bool); |
5f6526e1 | 174 | static tree tsubst_copy (tree, tree, tsubst_flags_t, tree); |
d95d815d | 175 | static tree tsubst_pack_expansion (tree, tree, tsubst_flags_t, tree); |
34197853 | 176 | |
7a468236 | 177 | /* Make the current scope suitable for access checking when we are |
178 | processing T. T can be FUNCTION_DECL for instantiated function | |
9014db85 | 179 | template, or VAR_DECL for static member variable (need by |
180 | instantiate_decl). */ | |
7a468236 | 181 | |
9140e457 | 182 | static void |
9014db85 | 183 | push_access_scope (tree t) |
7a468236 | 184 | { |
b4df430b | 185 | gcc_assert (TREE_CODE (t) == FUNCTION_DECL |
186 | || TREE_CODE (t) == VAR_DECL); | |
7a468236 | 187 | |
615c38af | 188 | if (DECL_FRIEND_CONTEXT (t)) |
189 | push_nested_class (DECL_FRIEND_CONTEXT (t)); | |
190 | else if (DECL_CLASS_SCOPE_P (t)) | |
9014db85 | 191 | push_nested_class (DECL_CONTEXT (t)); |
7917481e | 192 | else |
193 | push_to_top_level (); | |
9031d10b | 194 | |
9014db85 | 195 | if (TREE_CODE (t) == FUNCTION_DECL) |
7917481e | 196 | { |
197 | saved_access_scope = tree_cons | |
198 | (NULL_TREE, current_function_decl, saved_access_scope); | |
199 | current_function_decl = t; | |
200 | } | |
7a468236 | 201 | } |
202 | ||
7a468236 | 203 | /* Restore the scope set up by push_access_scope. T is the node we |
204 | are processing. */ | |
205 | ||
9140e457 | 206 | static void |
807be5b4 | 207 | pop_access_scope (tree t) |
7a468236 | 208 | { |
9014db85 | 209 | if (TREE_CODE (t) == FUNCTION_DECL) |
7a468236 | 210 | { |
211 | current_function_decl = TREE_VALUE (saved_access_scope); | |
212 | saved_access_scope = TREE_CHAIN (saved_access_scope); | |
213 | } | |
7917481e | 214 | |
615c38af | 215 | if (DECL_FRIEND_CONTEXT (t) || DECL_CLASS_SCOPE_P (t)) |
7917481e | 216 | pop_nested_class (); |
217 | else | |
218 | pop_from_top_level (); | |
7a468236 | 219 | } |
220 | ||
0a3b29ad | 221 | /* Do any processing required when DECL (a member template |
222 | declaration) is finished. Returns the TEMPLATE_DECL corresponding | |
223 | to DECL, unless it is a specialization, in which case the DECL | |
224 | itself is returned. */ | |
1146f179 | 225 | |
226 | tree | |
807be5b4 | 227 | finish_member_template_decl (tree decl) |
1146f179 | 228 | { |
0a3b29ad | 229 | if (decl == error_mark_node) |
230 | return error_mark_node; | |
231 | ||
b4df430b | 232 | gcc_assert (DECL_P (decl)); |
0a3b29ad | 233 | |
234 | if (TREE_CODE (decl) == TYPE_DECL) | |
1ac9b32b | 235 | { |
0a3b29ad | 236 | tree type; |
237 | ||
238 | type = TREE_TYPE (decl); | |
decf9b61 | 239 | if (type == error_mark_node) |
240 | return error_mark_node; | |
95397ff9 | 241 | if (MAYBE_CLASS_TYPE_P (type) |
0a3b29ad | 242 | && CLASSTYPE_TEMPLATE_INFO (type) |
243 | && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type)) | |
1ac9b32b | 244 | { |
0a3b29ad | 245 | tree tmpl = CLASSTYPE_TI_TEMPLATE (type); |
1ac9b32b | 246 | check_member_template (tmpl); |
247 | return tmpl; | |
248 | } | |
9ebe7529 | 249 | return NULL_TREE; |
1ac9b32b | 250 | } |
3d411d73 | 251 | else if (TREE_CODE (decl) == FIELD_DECL) |
39e7b4a4 | 252 | error ("data member %qD cannot be a member template", decl); |
29a50ff5 | 253 | else if (DECL_TEMPLATE_INFO (decl)) |
1146f179 | 254 | { |
29a50ff5 | 255 | if (!DECL_TEMPLATE_SPECIALIZATION (decl)) |
256 | { | |
257 | check_member_template (DECL_TI_TEMPLATE (decl)); | |
258 | return DECL_TI_TEMPLATE (decl); | |
259 | } | |
260 | else | |
261 | return decl; | |
9031d10b | 262 | } |
29a50ff5 | 263 | else |
39e7b4a4 | 264 | error ("invalid member template declaration %qD", decl); |
1146f179 | 265 | |
29a50ff5 | 266 | return error_mark_node; |
f3110581 | 267 | } |
1146f179 | 268 | |
cd4b38bb | 269 | /* Return the template info node corresponding to T, whatever T is. */ |
270 | ||
271 | tree | |
272 | get_template_info (tree t) | |
273 | { | |
274 | tree tinfo = NULL_TREE; | |
275 | ||
276 | if (DECL_P (t) && DECL_LANG_SPECIFIC (t)) | |
277 | tinfo = DECL_TEMPLATE_INFO (t); | |
278 | ||
279 | if (!tinfo && TREE_CODE (t) == TYPE_DECL) | |
280 | t = TREE_TYPE (t); | |
281 | ||
282 | if (TAGGED_TYPE_P (t)) | |
283 | tinfo = TYPE_TEMPLATE_INFO (t); | |
284 | ||
285 | return tinfo; | |
286 | } | |
287 | ||
f3110581 | 288 | /* Returns the template nesting level of the indicated class TYPE. |
9031d10b | 289 | |
f3110581 | 290 | For example, in: |
291 | template <class T> | |
292 | struct A | |
293 | { | |
294 | template <class U> | |
295 | struct B {}; | |
296 | }; | |
297 | ||
9031d10b | 298 | A<T>::B<U> has depth two, while A<T> has depth one. |
63e8563c | 299 | Both A<T>::B<int> and A<int>::B<U> have depth one, if |
58861e83 | 300 | they are instantiations, not specializations. |
63e8563c | 301 | |
302 | This function is guaranteed to return 0 if passed NULL_TREE so | |
303 | that, for example, `template_class_depth (current_class_type)' is | |
304 | always safe. */ | |
f3110581 | 305 | |
58861e83 | 306 | int |
307 | template_class_depth (tree type) | |
f3110581 | 308 | { |
1ac9b32b | 309 | int depth; |
f3110581 | 310 | |
9031d10b | 311 | for (depth = 0; |
11fa0698 | 312 | type && TREE_CODE (type) != NAMESPACE_DECL; |
9031d10b | 313 | type = (TREE_CODE (type) == FUNCTION_DECL) |
9ba4048d | 314 | ? CP_DECL_CONTEXT (type) : TYPE_CONTEXT (type)) |
11fa0698 | 315 | { |
cd4b38bb | 316 | tree tinfo = get_template_info (type); |
317 | ||
318 | if (tinfo && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo)) | |
319 | && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo)))) | |
320 | ++depth; | |
11fa0698 | 321 | } |
f3110581 | 322 | |
323 | return depth; | |
1146f179 | 324 | } |
64b4f183 | 325 | |
9cf51856 | 326 | /* Subroutine of maybe_begin_member_template_processing. |
327 | Returns true if processing DECL needs us to push template parms. */ | |
4a330908 | 328 | |
9cf51856 | 329 | static bool |
807be5b4 | 330 | inline_needs_template_parms (tree decl) |
4a330908 | 331 | { |
332 | if (! DECL_TEMPLATE_INFO (decl)) | |
9cf51856 | 333 | return false; |
f3110581 | 334 | |
34197853 | 335 | return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl))) |
4a330908 | 336 | > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl))); |
337 | } | |
338 | ||
339 | /* Subroutine of maybe_begin_member_template_processing. | |
340 | Push the template parms in PARMS, starting from LEVELS steps into the | |
341 | chain, and ending at the beginning, since template parms are listed | |
342 | innermost first. */ | |
343 | ||
344 | static void | |
807be5b4 | 345 | push_inline_template_parms_recursive (tree parmlist, int levels) |
4a330908 | 346 | { |
347 | tree parms = TREE_VALUE (parmlist); | |
348 | int i; | |
349 | ||
350 | if (levels > 1) | |
351 | push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1); | |
f5308315 | 352 | |
64b4f183 | 353 | ++processing_template_decl; |
4a330908 | 354 | current_template_parms |
180f098b | 355 | = tree_cons (size_int (processing_template_decl), |
64b4f183 | 356 | parms, current_template_parms); |
4a330908 | 357 | TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1; |
358 | ||
e880f232 | 359 | begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec, |
653e5405 | 360 | NULL); |
9031d10b | 361 | for (i = 0; i < TREE_VEC_LENGTH (parms); ++i) |
64b4f183 | 362 | { |
21bd02a9 | 363 | tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i)); |
508c7aa7 | 364 | |
21bd02a9 | 365 | if (parm == error_mark_node) |
366 | continue; | |
508c7aa7 | 367 | |
b4df430b | 368 | gcc_assert (DECL_P (parm)); |
4a330908 | 369 | |
64b4f183 | 370 | switch (TREE_CODE (parm)) |
371 | { | |
f5308315 | 372 | case TYPE_DECL: |
cd5dd2d8 | 373 | case TEMPLATE_DECL: |
64b4f183 | 374 | pushdecl (parm); |
375 | break; | |
f5308315 | 376 | |
377 | case PARM_DECL: | |
378 | { | |
ee7a9a7b | 379 | /* Make a CONST_DECL as is done in process_template_parm. |
380 | It is ugly that we recreate this here; the original | |
381 | version built in process_template_parm is no longer | |
382 | available. */ | |
f5308315 | 383 | tree decl = build_decl (CONST_DECL, DECL_NAME (parm), |
384 | TREE_TYPE (parm)); | |
ecdd3d84 | 385 | DECL_ARTIFICIAL (decl) = 1; |
4ee9c684 | 386 | TREE_CONSTANT (decl) = 1; |
4ee9c684 | 387 | TREE_READONLY (decl) = 1; |
f5308315 | 388 | DECL_INITIAL (decl) = DECL_INITIAL (parm); |
ce23987e | 389 | SET_DECL_TEMPLATE_PARM_P (decl); |
f5308315 | 390 | pushdecl (decl); |
391 | } | |
4a330908 | 392 | break; |
f5308315 | 393 | |
64b4f183 | 394 | default: |
2e3e31d2 | 395 | gcc_unreachable (); |
64b4f183 | 396 | } |
397 | } | |
398 | } | |
399 | ||
4a330908 | 400 | /* Restore the template parameter context for a member template or |
401 | a friend template defined in a class definition. */ | |
402 | ||
403 | void | |
807be5b4 | 404 | maybe_begin_member_template_processing (tree decl) |
4a330908 | 405 | { |
406 | tree parms; | |
d226d13f | 407 | int levels = 0; |
4a330908 | 408 | |
d226d13f | 409 | if (inline_needs_template_parms (decl)) |
410 | { | |
411 | parms = DECL_TEMPLATE_PARMS (most_general_template (decl)); | |
412 | levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl; | |
4a330908 | 413 | |
d226d13f | 414 | if (DECL_TEMPLATE_SPECIALIZATION (decl)) |
415 | { | |
416 | --levels; | |
417 | parms = TREE_CHAIN (parms); | |
418 | } | |
4a330908 | 419 | |
d226d13f | 420 | push_inline_template_parms_recursive (parms, levels); |
4a330908 | 421 | } |
422 | ||
d226d13f | 423 | /* Remember how many levels of template parameters we pushed so that |
424 | we can pop them later. */ | |
e52a9dbc | 425 | VEC_safe_push (int, heap, inline_parm_levels, levels); |
4a330908 | 426 | } |
427 | ||
16d30ad7 | 428 | /* Undo the effects of maybe_begin_member_template_processing. */ |
64b4f183 | 429 | |
9031d10b | 430 | void |
807be5b4 | 431 | maybe_end_member_template_processing (void) |
64b4f183 | 432 | { |
d226d13f | 433 | int i; |
e52a9dbc | 434 | int last; |
d226d13f | 435 | |
e52a9dbc | 436 | if (VEC_length (int, inline_parm_levels) == 0) |
64b4f183 | 437 | return; |
438 | ||
e52a9dbc | 439 | last = VEC_pop (int, inline_parm_levels); |
440 | for (i = 0; i < last; ++i) | |
4a330908 | 441 | { |
442 | --processing_template_decl; | |
443 | current_template_parms = TREE_CHAIN (current_template_parms); | |
444 | poplevel (0, 0, 0); | |
445 | } | |
64b4f183 | 446 | } |
447 | ||
34197853 | 448 | /* Return a new template argument vector which contains all of ARGS, |
a32fe883 | 449 | but has as its innermost set of arguments the EXTRA_ARGS. */ |
8610df0d | 450 | |
451 | static tree | |
807be5b4 | 452 | add_to_template_args (tree args, tree extra_args) |
8610df0d | 453 | { |
34197853 | 454 | tree new_args; |
455 | int extra_depth; | |
456 | int i; | |
457 | int j; | |
8610df0d | 458 | |
34197853 | 459 | extra_depth = TMPL_ARGS_DEPTH (extra_args); |
19c3b3a6 | 460 | new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth); |
8610df0d | 461 | |
34197853 | 462 | for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i) |
463 | SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i)); | |
8610df0d | 464 | |
34197853 | 465 | for (j = 1; j <= extra_depth; ++j, ++i) |
466 | SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j)); | |
9031d10b | 467 | |
8610df0d | 468 | return new_args; |
469 | } | |
470 | ||
34197853 | 471 | /* Like add_to_template_args, but only the outermost ARGS are added to |
472 | the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH | |
473 | (EXTRA_ARGS) levels are added. This function is used to combine | |
474 | the template arguments from a partial instantiation with the | |
475 | template arguments used to attain the full instantiation from the | |
476 | partial instantiation. */ | |
64b4f183 | 477 | |
b4841af8 | 478 | static tree |
807be5b4 | 479 | add_outermost_template_args (tree args, tree extra_args) |
64b4f183 | 480 | { |
481 | tree new_args; | |
482 | ||
26430721 | 483 | /* If there are more levels of EXTRA_ARGS than there are ARGS, |
484 | something very fishy is going on. */ | |
b4df430b | 485 | gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args)); |
26430721 | 486 | |
487 | /* If *all* the new arguments will be the EXTRA_ARGS, just return | |
488 | them. */ | |
489 | if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args)) | |
490 | return extra_args; | |
491 | ||
34197853 | 492 | /* For the moment, we make ARGS look like it contains fewer levels. */ |
493 | TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args); | |
9031d10b | 494 | |
34197853 | 495 | new_args = add_to_template_args (args, extra_args); |
64b4f183 | 496 | |
34197853 | 497 | /* Now, we restore ARGS to its full dimensions. */ |
498 | TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args); | |
64b4f183 | 499 | |
500 | return new_args; | |
501 | } | |
e857e9c7 | 502 | |
a32fe883 | 503 | /* Return the N levels of innermost template arguments from the ARGS. */ |
504 | ||
505 | tree | |
807be5b4 | 506 | get_innermost_template_args (tree args, int n) |
a32fe883 | 507 | { |
508 | tree new_args; | |
509 | int extra_levels; | |
510 | int i; | |
511 | ||
b4df430b | 512 | gcc_assert (n >= 0); |
a32fe883 | 513 | |
514 | /* If N is 1, just return the innermost set of template arguments. */ | |
515 | if (n == 1) | |
516 | return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args)); | |
9031d10b | 517 | |
a32fe883 | 518 | /* If we're not removing anything, just return the arguments we were |
519 | given. */ | |
520 | extra_levels = TMPL_ARGS_DEPTH (args) - n; | |
b4df430b | 521 | gcc_assert (extra_levels >= 0); |
a32fe883 | 522 | if (extra_levels == 0) |
523 | return args; | |
524 | ||
525 | /* Make a new set of arguments, not containing the outer arguments. */ | |
526 | new_args = make_tree_vec (n); | |
527 | for (i = 1; i <= n; ++i) | |
9031d10b | 528 | SET_TMPL_ARGS_LEVEL (new_args, i, |
a32fe883 | 529 | TMPL_ARGS_LEVEL (args, i + extra_levels)); |
530 | ||
531 | return new_args; | |
532 | } | |
533 | ||
73ab75d5 | 534 | /* The inverse of get_innermost_template_args: Return all but the innermost |
535 | EXTRA_LEVELS levels of template arguments from the ARGS. */ | |
536 | ||
537 | static tree | |
538 | strip_innermost_template_args (tree args, int extra_levels) | |
539 | { | |
540 | tree new_args; | |
541 | int n = TMPL_ARGS_DEPTH (args) - extra_levels; | |
542 | int i; | |
543 | ||
544 | gcc_assert (n >= 0); | |
545 | ||
546 | /* If N is 1, just return the outermost set of template arguments. */ | |
547 | if (n == 1) | |
548 | return TMPL_ARGS_LEVEL (args, 1); | |
549 | ||
550 | /* If we're not removing anything, just return the arguments we were | |
551 | given. */ | |
552 | gcc_assert (extra_levels >= 0); | |
553 | if (extra_levels == 0) | |
554 | return args; | |
555 | ||
556 | /* Make a new set of arguments, not containing the inner arguments. */ | |
557 | new_args = make_tree_vec (n); | |
558 | for (i = 1; i <= n; ++i) | |
559 | SET_TMPL_ARGS_LEVEL (new_args, i, | |
560 | TMPL_ARGS_LEVEL (args, i)); | |
561 | ||
562 | return new_args; | |
563 | } | |
564 | ||
e857e9c7 | 565 | /* We've got a template header coming up; push to a new level for storing |
566 | the parms. */ | |
471086d6 | 567 | |
471086d6 | 568 | void |
807be5b4 | 569 | begin_template_parm_list (void) |
471086d6 | 570 | { |
0f0d5a69 | 571 | /* We use a non-tag-transparent scope here, which causes pushtag to |
572 | put tags in this scope, rather than in the enclosing class or | |
573 | namespace scope. This is the right thing, since we want | |
574 | TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a | |
575 | global template class, push_template_decl handles putting the | |
576 | TEMPLATE_DECL into top-level scope. For a nested template class, | |
577 | e.g.: | |
578 | ||
579 | template <class T> struct S1 { | |
653e5405 | 580 | template <class T> struct S2 {}; |
0f0d5a69 | 581 | }; |
582 | ||
583 | pushtag contains special code to call pushdecl_with_scope on the | |
584 | TEMPLATE_DECL for S2. */ | |
e880f232 | 585 | begin_scope (sk_template_parms, NULL); |
3cc0b4b9 | 586 | ++processing_template_decl; |
29926476 | 587 | ++processing_template_parmlist; |
b1cfe2be | 588 | note_template_header (0); |
589 | } | |
590 | ||
1d42585d | 591 | /* This routine is called when a specialization is declared. If it is |
6d9bff9f | 592 | invalid to declare a specialization here, an error is reported and |
593 | false is returned, otherwise this routine will return true. */ | |
1d42585d | 594 | |
6d9bff9f | 595 | static bool |
807be5b4 | 596 | check_specialization_scope (void) |
1d42585d | 597 | { |
598 | tree scope = current_scope (); | |
76c1ba40 | 599 | |
9031d10b | 600 | /* [temp.expl.spec] |
601 | ||
1d42585d | 602 | An explicit specialization shall be declared in the namespace of |
603 | which the template is a member, or, for member templates, in the | |
604 | namespace of which the enclosing class or enclosing class | |
605 | template is a member. An explicit specialization of a member | |
606 | function, member class or static data member of a class template | |
607 | shall be declared in the namespace of which the class template | |
608 | is a member. */ | |
609 | if (scope && TREE_CODE (scope) != NAMESPACE_DECL) | |
6d9bff9f | 610 | { |
611 | error ("explicit specialization in non-namespace scope %qD", scope); | |
612 | return false; | |
613 | } | |
76c1ba40 | 614 | |
9031d10b | 615 | /* [temp.expl.spec] |
1d42585d | 616 | |
617 | In an explicit specialization declaration for a member of a class | |
618 | template or a member template that appears in namespace scope, | |
619 | the member template and some of its enclosing class templates may | |
620 | remain unspecialized, except that the declaration shall not | |
621 | explicitly specialize a class member template if its enclosing | |
622 | class templates are not explicitly specialized as well. */ | |
9031d10b | 623 | if (current_template_parms) |
6d9bff9f | 624 | { |
625 | error ("enclosing class templates are not explicitly specialized"); | |
626 | return false; | |
627 | } | |
628 | ||
629 | return true; | |
1d42585d | 630 | } |
631 | ||
47cd6605 | 632 | /* We've just seen template <>. */ |
b1cfe2be | 633 | |
6d9bff9f | 634 | bool |
807be5b4 | 635 | begin_specialization (void) |
b1cfe2be | 636 | { |
e880f232 | 637 | begin_scope (sk_template_spec, NULL); |
b1cfe2be | 638 | note_template_header (1); |
6d9bff9f | 639 | return check_specialization_scope (); |
b1cfe2be | 640 | } |
641 | ||
ec0eee8d | 642 | /* Called at then end of processing a declaration preceded by |
b1cfe2be | 643 | template<>. */ |
644 | ||
9031d10b | 645 | void |
807be5b4 | 646 | end_specialization (void) |
b1cfe2be | 647 | { |
3467e461 | 648 | finish_scope (); |
b1cfe2be | 649 | reset_specialization (); |
650 | } | |
651 | ||
b1cfe2be | 652 | /* Any template <>'s that we have seen thus far are not referring to a |
47cd6605 | 653 | function specialization. */ |
b1cfe2be | 654 | |
655 | void | |
807be5b4 | 656 | reset_specialization (void) |
b1cfe2be | 657 | { |
658 | processing_specialization = 0; | |
659 | template_header_count = 0; | |
660 | } | |
661 | ||
3160db1d | 662 | /* We've just seen a template header. If SPECIALIZATION is nonzero, |
b1cfe2be | 663 | it was of the form template <>. */ |
664 | ||
9031d10b | 665 | static void |
807be5b4 | 666 | note_template_header (int specialization) |
b1cfe2be | 667 | { |
668 | processing_specialization = specialization; | |
669 | template_header_count++; | |
670 | } | |
671 | ||
668ae905 | 672 | /* We're beginning an explicit instantiation. */ |
b1cfe2be | 673 | |
668ae905 | 674 | void |
807be5b4 | 675 | begin_explicit_instantiation (void) |
b1cfe2be | 676 | { |
b4df430b | 677 | gcc_assert (!processing_explicit_instantiation); |
0a3b29ad | 678 | processing_explicit_instantiation = true; |
668ae905 | 679 | } |
b1cfe2be | 680 | |
b1cfe2be | 681 | |
668ae905 | 682 | void |
807be5b4 | 683 | end_explicit_instantiation (void) |
668ae905 | 684 | { |
b4df430b | 685 | gcc_assert (processing_explicit_instantiation); |
0a3b29ad | 686 | processing_explicit_instantiation = false; |
668ae905 | 687 | } |
b1cfe2be | 688 | |
cd7043f9 | 689 | /* An explicit specialization or partial specialization TMPL is being |
e4e8c884 | 690 | declared. Check that the namespace in which the specialization is |
691 | occurring is permissible. Returns false iff it is invalid to | |
692 | specialize TMPL in the current namespace. */ | |
9031d10b | 693 | |
e4e8c884 | 694 | static bool |
695 | check_specialization_namespace (tree tmpl) | |
696 | { | |
697 | tree tpl_ns = decl_namespace_context (tmpl); | |
698 | ||
699 | /* [tmpl.expl.spec] | |
9031d10b | 700 | |
e4e8c884 | 701 | An explicit specialization shall be declared in the namespace of |
702 | which the template is a member, or, for member templates, in the | |
703 | namespace of which the enclosing class or enclosing class | |
704 | template is a member. An explicit specialization of a member | |
705 | function, member class or static data member of a class template | |
706 | shall be declared in the namespace of which the class template is | |
707 | a member. */ | |
708 | if (is_associated_namespace (current_namespace, tpl_ns)) | |
709 | /* Same or super-using namespace. */ | |
710 | return true; | |
711 | else | |
712 | { | |
39e7b4a4 | 713 | pedwarn ("specialization of %qD in different namespace", tmpl); |
3cf8b391 | 714 | pedwarn (" from definition of %q+#D", tmpl); |
e4e8c884 | 715 | return false; |
716 | } | |
717 | } | |
718 | ||
9ed9d9ec | 719 | /* SPEC is an explicit instantiation. Check that it is valid to |
720 | perform this explicit instantiation in the current namespace. */ | |
721 | ||
722 | static void | |
723 | check_explicit_instantiation_namespace (tree spec) | |
724 | { | |
725 | tree ns; | |
726 | ||
727 | /* DR 275: An explicit instantiation shall appear in an enclosing | |
074ab442 | 728 | namespace of its template. */ |
9ed9d9ec | 729 | ns = decl_namespace_context (spec); |
730 | if (!is_ancestor (current_namespace, ns)) | |
731 | pedwarn ("explicit instantiation of %qD in namespace %qD " | |
f629cc66 | 732 | "(which does not enclose namespace %qD)", |
9ed9d9ec | 733 | spec, current_namespace, ns); |
734 | } | |
735 | ||
34197853 | 736 | /* The TYPE is being declared. If it is a template type, that means it |
737 | is a partial specialization. Do appropriate error-checking. */ | |
738 | ||
2ce07779 | 739 | tree |
807be5b4 | 740 | maybe_process_partial_specialization (tree type) |
34197853 | 741 | { |
0965c843 | 742 | tree context; |
743 | ||
744 | if (type == error_mark_node) | |
2ce07779 | 745 | return error_mark_node; |
746 | ||
747 | if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM) | |
748 | { | |
749 | error ("name of class shadows template template parameter %qD", | |
750 | TYPE_NAME (type)); | |
751 | return error_mark_node; | |
752 | } | |
0965c843 | 753 | |
754 | context = TYPE_CONTEXT (type); | |
9ae1acf5 | 755 | |
0a3b29ad | 756 | if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type)) |
34197853 | 757 | { |
9ae1acf5 | 758 | /* This is for ordinary explicit specialization and partial |
759 | specialization of a template class such as: | |
760 | ||
761 | template <> class C<int>; | |
762 | ||
763 | or: | |
764 | ||
765 | template <class T> class C<T*>; | |
766 | ||
767 | Make sure that `C<int>' and `C<T*>' are implicit instantiations. */ | |
768 | ||
34197853 | 769 | if (CLASSTYPE_IMPLICIT_INSTANTIATION (type) |
4b72716d | 770 | && !COMPLETE_TYPE_P (type)) |
34197853 | 771 | { |
e4e8c884 | 772 | check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (type)); |
3a8f9e39 | 773 | SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type); |
34197853 | 774 | if (processing_template_decl) |
775 | push_template_decl (TYPE_MAIN_DECL (type)); | |
776 | } | |
777 | else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type)) | |
39e7b4a4 | 778 | error ("specialization of %qT after instantiation", type); |
34197853 | 779 | } |
9ae1acf5 | 780 | else if (CLASS_TYPE_P (type) |
781 | && !CLASSTYPE_USE_TEMPLATE (type) | |
782 | && CLASSTYPE_TEMPLATE_INFO (type) | |
783 | && context && CLASS_TYPE_P (context) | |
784 | && CLASSTYPE_TEMPLATE_INFO (context)) | |
785 | { | |
786 | /* This is for an explicit specialization of member class | |
787 | template according to [temp.expl.spec/18]: | |
788 | ||
789 | template <> template <class U> class C<int>::D; | |
790 | ||
791 | The context `C<int>' must be an implicit instantiation. | |
792 | Otherwise this is just a member class template declared | |
793 | earlier like: | |
794 | ||
795 | template <> class C<int> { template <class U> class D; }; | |
796 | template <> template <class U> class C<int>::D; | |
797 | ||
798 | In the first case, `C<int>::D' is a specialization of `C<T>::D' | |
799 | while in the second case, `C<int>::D' is a primary template | |
800 | and `C<T>::D' may not exist. */ | |
801 | ||
802 | if (CLASSTYPE_IMPLICIT_INSTANTIATION (context) | |
803 | && !COMPLETE_TYPE_P (type)) | |
804 | { | |
805 | tree t; | |
806 | ||
807 | if (current_namespace | |
808 | != decl_namespace_context (CLASSTYPE_TI_TEMPLATE (type))) | |
809 | { | |
39e7b4a4 | 810 | pedwarn ("specializing %q#T in different namespace", type); |
3cf8b391 | 811 | pedwarn (" from definition of %q+#D", |
812 | CLASSTYPE_TI_TEMPLATE (type)); | |
9ae1acf5 | 813 | } |
814 | ||
815 | /* Check for invalid specialization after instantiation: | |
816 | ||
817 | template <> template <> class C<int>::D<int>; | |
818 | template <> template <class U> class C<int>::D; */ | |
819 | ||
820 | for (t = DECL_TEMPLATE_INSTANTIATIONS | |
821 | (most_general_template (CLASSTYPE_TI_TEMPLATE (type))); | |
822 | t; t = TREE_CHAIN (t)) | |
823 | if (TREE_VALUE (t) != type | |
824 | && TYPE_CONTEXT (TREE_VALUE (t)) == context) | |
39e7b4a4 | 825 | error ("specialization %qT after instantiation %qT", |
9ae1acf5 | 826 | type, TREE_VALUE (t)); |
827 | ||
828 | /* Mark TYPE as a specialization. And as a result, we only | |
829 | have one level of template argument for the innermost | |
830 | class template. */ | |
831 | SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type); | |
832 | CLASSTYPE_TI_ARGS (type) | |
833 | = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)); | |
834 | } | |
835 | } | |
e0d8778b | 836 | else if (processing_specialization) |
2ce07779 | 837 | { |
838 | error ("explicit specialization of non-template %qT", type); | |
839 | return error_mark_node; | |
840 | } | |
841 | ||
842 | return type; | |
34197853 | 843 | } |
844 | ||
d68806e8 | 845 | /* Returns nonzero if we can optimize the retrieval of specializations |
38d89ee9 | 846 | for TMPL, a TEMPLATE_DECL. In particular, for such a template, we |
847 | do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */ | |
848 | ||
9031d10b | 849 | static inline bool |
38d89ee9 | 850 | optimize_specialization_lookup_p (tree tmpl) |
851 | { | |
852 | return (DECL_FUNCTION_TEMPLATE_P (tmpl) | |
853 | && DECL_CLASS_SCOPE_P (tmpl) | |
854 | /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template | |
855 | parameter. */ | |
856 | && CLASS_TYPE_P (DECL_CONTEXT (tmpl)) | |
b5fe3425 | 857 | /* The optimized lookup depends on the fact that the |
858 | template arguments for the member function template apply | |
859 | purely to the containing class, which is not true if the | |
860 | containing class is an explicit or partial | |
861 | specialization. */ | |
862 | && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl)) | |
38d89ee9 | 863 | && !DECL_MEMBER_TEMPLATE_P (tmpl) |
864 | && !DECL_CONV_FN_P (tmpl) | |
865 | /* It is possible to have a template that is not a member | |
866 | template and is not a member of a template class: | |
9031d10b | 867 | |
868 | template <typename T> | |
38d89ee9 | 869 | struct S { friend A::f(); }; |
9031d10b | 870 | |
38d89ee9 | 871 | Here, the friend function is a template, but the context does |
872 | not have template information. The optimized lookup relies | |
873 | on having ARGS be the template arguments for both the class | |
874 | and the function template. */ | |
875 | && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl))); | |
876 | } | |
877 | ||
668ae905 | 878 | /* Retrieve the specialization (in the sense of [temp.spec] - a |
879 | specialization is either an instantiation or an explicit | |
880 | specialization) of TMPL for the given template ARGS. If there is | |
881 | no such specialization, return NULL_TREE. The ARGS are a vector of | |
882 | arguments, or a vector of vectors of arguments, in the case of | |
9031d10b | 883 | templates with more than one level of parameters. |
38d89ee9 | 884 | |
885 | If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true, | |
886 | then we search for a partial specialization matching ARGS. This | |
887 | parameter is ignored if TMPL is not a class template. */ | |
9031d10b | 888 | |
668ae905 | 889 | static tree |
9031d10b | 890 | retrieve_specialization (tree tmpl, tree args, |
38d89ee9 | 891 | bool class_specializations_p) |
668ae905 | 892 | { |
6f32df61 | 893 | if (args == error_mark_node) |
894 | return NULL_TREE; | |
895 | ||
b4df430b | 896 | gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL); |
668ae905 | 897 | |
34197853 | 898 | /* There should be as many levels of arguments as there are |
899 | levels of parameters. */ | |
9031d10b | 900 | gcc_assert (TMPL_ARGS_DEPTH (args) |
b4df430b | 901 | == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl))); |
9031d10b | 902 | |
38d89ee9 | 903 | if (optimize_specialization_lookup_p (tmpl)) |
904 | { | |
905 | tree class_template; | |
906 | tree class_specialization; | |
046bfc77 | 907 | VEC(tree,gc) *methods; |
38d89ee9 | 908 | tree fns; |
909 | int idx; | |
910 | ||
911 | /* The template arguments actually apply to the containing | |
912 | class. Find the class specialization with those | |
913 | arguments. */ | |
914 | class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl)); | |
9031d10b | 915 | class_specialization |
38d89ee9 | 916 | = retrieve_specialization (class_template, args, |
917 | /*class_specializations_p=*/false); | |
918 | if (!class_specialization) | |
919 | return NULL_TREE; | |
920 | /* Now, find the appropriate entry in the CLASSTYPE_METHOD_VEC | |
921 | for the specialization. */ | |
922 | idx = class_method_index_for_fn (class_specialization, tmpl); | |
923 | if (idx == -1) | |
924 | return NULL_TREE; | |
925 | /* Iterate through the methods with the indicated name, looking | |
926 | for the one that has an instance of TMPL. */ | |
927 | methods = CLASSTYPE_METHOD_VEC (class_specialization); | |
928 | for (fns = VEC_index (tree, methods, idx); fns; fns = OVL_NEXT (fns)) | |
929 | { | |
930 | tree fn = OVL_CURRENT (fns); | |
931 | if (DECL_TEMPLATE_INFO (fn) && DECL_TI_TEMPLATE (fn) == tmpl) | |
932 | return fn; | |
933 | } | |
934 | return NULL_TREE; | |
935 | } | |
936 | else | |
937 | { | |
938 | tree *sp; | |
939 | tree *head; | |
940 | ||
941 | /* Class templates store their instantiations on the | |
942 | DECL_TEMPLATE_INSTANTIATIONS list; other templates use the | |
943 | DECL_TEMPLATE_SPECIALIZATIONS list. */ | |
944 | if (!class_specializations_p | |
d718e1bc | 945 | && TREE_CODE (DECL_TEMPLATE_RESULT (tmpl)) == TYPE_DECL |
946 | && TAGGED_TYPE_P (TREE_TYPE (tmpl))) | |
38d89ee9 | 947 | sp = &DECL_TEMPLATE_INSTANTIATIONS (tmpl); |
948 | else | |
949 | sp = &DECL_TEMPLATE_SPECIALIZATIONS (tmpl); | |
950 | head = sp; | |
951 | /* Iterate through the list until we find a matching template. */ | |
952 | while (*sp != NULL_TREE) | |
953 | { | |
954 | tree spec = *sp; | |
9031d10b | 955 | |
38d89ee9 | 956 | if (comp_template_args (TREE_PURPOSE (spec), args)) |
957 | { | |
958 | /* Use the move-to-front heuristic to speed up future | |
9031d10b | 959 | searches. */ |
38d89ee9 | 960 | if (spec != *head) |
961 | { | |
962 | *sp = TREE_CHAIN (*sp); | |
963 | TREE_CHAIN (spec) = *head; | |
964 | *head = spec; | |
965 | } | |
966 | return TREE_VALUE (spec); | |
967 | } | |
968 | sp = &TREE_CHAIN (spec); | |
969 | } | |
970 | } | |
668ae905 | 971 | |
972 | return NULL_TREE; | |
b1cfe2be | 973 | } |
974 | ||
ec0eee8d | 975 | /* Like retrieve_specialization, but for local declarations. */ |
70a658bd | 976 | |
977 | static tree | |
807be5b4 | 978 | retrieve_local_specialization (tree tmpl) |
70a658bd | 979 | { |
f8ee651e | 980 | tree spec; |
981 | ||
982 | if (local_specializations == NULL) | |
983 | return NULL_TREE; | |
984 | ||
985 | spec = (tree) htab_find_with_hash (local_specializations, tmpl, | |
986 | htab_hash_pointer (tmpl)); | |
0a3b29ad | 987 | return spec ? TREE_PURPOSE (spec) : NULL_TREE; |
70a658bd | 988 | } |
989 | ||
3160db1d | 990 | /* Returns nonzero iff DECL is a specialization of TMPL. */ |
0f0d5a69 | 991 | |
992 | int | |
807be5b4 | 993 | is_specialization_of (tree decl, tree tmpl) |
0f0d5a69 | 994 | { |
995 | tree t; | |
996 | ||
997 | if (TREE_CODE (decl) == FUNCTION_DECL) | |
998 | { | |
9031d10b | 999 | for (t = decl; |
0f0d5a69 | 1000 | t != NULL_TREE; |
1001 | t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE) | |
1002 | if (t == tmpl) | |
1003 | return 1; | |
1004 | } | |
9031d10b | 1005 | else |
0f0d5a69 | 1006 | { |
b4df430b | 1007 | gcc_assert (TREE_CODE (decl) == TYPE_DECL); |
0f0d5a69 | 1008 | |
1009 | for (t = TREE_TYPE (decl); | |
1010 | t != NULL_TREE; | |
1011 | t = CLASSTYPE_USE_TEMPLATE (t) | |
1012 | ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE) | |
1361fb16 | 1013 | if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl))) |
0f0d5a69 | 1014 | return 1; |
9031d10b | 1015 | } |
b1cfe2be | 1016 | |
0f0d5a69 | 1017 | return 0; |
1018 | } | |
668ae905 | 1019 | |
7bdfc61c | 1020 | /* Returns nonzero iff DECL is a specialization of friend declaration |
1021 | FRIEND according to [temp.friend]. */ | |
1022 | ||
1023 | bool | |
1024 | is_specialization_of_friend (tree decl, tree friend) | |
1025 | { | |
1026 | bool need_template = true; | |
1027 | int template_depth; | |
1028 | ||
f95fba26 | 1029 | gcc_assert (TREE_CODE (decl) == FUNCTION_DECL |
1030 | || TREE_CODE (decl) == TYPE_DECL); | |
7bdfc61c | 1031 | |
1032 | /* For [temp.friend/6] when FRIEND is an ordinary member function | |
1033 | of a template class, we want to check if DECL is a specialization | |
1034 | if this. */ | |
1035 | if (TREE_CODE (friend) == FUNCTION_DECL | |
1036 | && DECL_TEMPLATE_INFO (friend) | |
1037 | && !DECL_USE_TEMPLATE (friend)) | |
1038 | { | |
f95fba26 | 1039 | /* We want a TEMPLATE_DECL for `is_specialization_of'. */ |
7bdfc61c | 1040 | friend = DECL_TI_TEMPLATE (friend); |
1041 | need_template = false; | |
1042 | } | |
f95fba26 | 1043 | else if (TREE_CODE (friend) == TEMPLATE_DECL |
1044 | && !PRIMARY_TEMPLATE_P (friend)) | |
1045 | need_template = false; | |
7bdfc61c | 1046 | |
1047 | /* There is nothing to do if this is not a template friend. */ | |
1048 | if (TREE_CODE (friend) != TEMPLATE_DECL) | |
f95fba26 | 1049 | return false; |
7bdfc61c | 1050 | |
1051 | if (is_specialization_of (decl, friend)) | |
f95fba26 | 1052 | return true; |
7bdfc61c | 1053 | |
1054 | /* [temp.friend/6] | |
1055 | A member of a class template may be declared to be a friend of a | |
1056 | non-template class. In this case, the corresponding member of | |
1057 | every specialization of the class template is a friend of the | |
1058 | class granting friendship. | |
9031d10b | 1059 | |
7bdfc61c | 1060 | For example, given a template friend declaration |
1061 | ||
1062 | template <class T> friend void A<T>::f(); | |
1063 | ||
1064 | the member function below is considered a friend | |
1065 | ||
1066 | template <> struct A<int> { | |
1067 | void f(); | |
1068 | }; | |
1069 | ||
1070 | For this type of template friend, TEMPLATE_DEPTH below will be | |
e324a773 | 1071 | nonzero. To determine if DECL is a friend of FRIEND, we first |
7bdfc61c | 1072 | check if the enclosing class is a specialization of another. */ |
1073 | ||
1074 | template_depth = template_class_depth (DECL_CONTEXT (friend)); | |
1075 | if (template_depth | |
1076 | && DECL_CLASS_SCOPE_P (decl) | |
9031d10b | 1077 | && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)), |
7bdfc61c | 1078 | CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend)))) |
1079 | { | |
1080 | /* Next, we check the members themselves. In order to handle | |
f95fba26 | 1081 | a few tricky cases, such as when FRIEND's are |
7bdfc61c | 1082 | |
1083 | template <class T> friend void A<T>::g(T t); | |
1084 | template <class T> template <T t> friend void A<T>::h(); | |
1085 | ||
f95fba26 | 1086 | and DECL's are |
1087 | ||
1088 | void A<int>::g(int); | |
1089 | template <int> void A<int>::h(); | |
1090 | ||
1091 | we need to figure out ARGS, the template arguments from | |
1092 | the context of DECL. This is required for template substitution | |
1093 | of `T' in the function parameter of `g' and template parameter | |
1094 | of `h' in the above examples. Here ARGS corresponds to `int'. */ | |
7bdfc61c | 1095 | |
1096 | tree context = DECL_CONTEXT (decl); | |
1097 | tree args = NULL_TREE; | |
1098 | int current_depth = 0; | |
f95fba26 | 1099 | |
7bdfc61c | 1100 | while (current_depth < template_depth) |
1101 | { | |
1102 | if (CLASSTYPE_TEMPLATE_INFO (context)) | |
1103 | { | |
1104 | if (current_depth == 0) | |
1105 | args = TYPE_TI_ARGS (context); | |
1106 | else | |
1107 | args = add_to_template_args (TYPE_TI_ARGS (context), args); | |
1108 | current_depth++; | |
1109 | } | |
1110 | context = TYPE_CONTEXT (context); | |
1111 | } | |
1112 | ||
1113 | if (TREE_CODE (decl) == FUNCTION_DECL) | |
1114 | { | |
1115 | bool is_template; | |
1116 | tree friend_type; | |
1117 | tree decl_type; | |
1118 | tree friend_args_type; | |
1119 | tree decl_args_type; | |
1120 | ||
1121 | /* Make sure that both DECL and FRIEND are templates or | |
1122 | non-templates. */ | |
1123 | is_template = DECL_TEMPLATE_INFO (decl) | |
1124 | && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)); | |
1125 | if (need_template ^ is_template) | |
f95fba26 | 1126 | return false; |
7bdfc61c | 1127 | else if (is_template) |
1128 | { | |
a5268b2f | 1129 | /* If both are templates, check template parameter list. */ |
7bdfc61c | 1130 | tree friend_parms |
1131 | = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend), | |
1132 | args, tf_none); | |
1133 | if (!comp_template_parms | |
1134 | (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)), | |
1135 | friend_parms)) | |
f95fba26 | 1136 | return false; |
7bdfc61c | 1137 | |
1138 | decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl)); | |
1139 | } | |
1140 | else | |
1141 | decl_type = TREE_TYPE (decl); | |
1142 | ||
1143 | friend_type = tsubst_function_type (TREE_TYPE (friend), args, | |
1144 | tf_none, NULL_TREE); | |
1145 | if (friend_type == error_mark_node) | |
f95fba26 | 1146 | return false; |
7bdfc61c | 1147 | |
1148 | /* Check if return types match. */ | |
1149 | if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type))) | |
f95fba26 | 1150 | return false; |
7bdfc61c | 1151 | |
1152 | /* Check if function parameter types match, ignoring the | |
1153 | `this' parameter. */ | |
1154 | friend_args_type = TYPE_ARG_TYPES (friend_type); | |
1155 | decl_args_type = TYPE_ARG_TYPES (decl_type); | |
1156 | if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend)) | |
1157 | friend_args_type = TREE_CHAIN (friend_args_type); | |
1158 | if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)) | |
1159 | decl_args_type = TREE_CHAIN (decl_args_type); | |
f95fba26 | 1160 | |
1161 | return compparms (decl_args_type, friend_args_type); | |
1162 | } | |
1163 | else | |
1164 | { | |
1165 | /* DECL is a TYPE_DECL */ | |
1166 | bool is_template; | |
1167 | tree decl_type = TREE_TYPE (decl); | |
1168 | ||
1169 | /* Make sure that both DECL and FRIEND are templates or | |
1170 | non-templates. */ | |
1171 | is_template | |
1172 | = CLASSTYPE_TEMPLATE_INFO (decl_type) | |
1173 | && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type)); | |
1174 | ||
1175 | if (need_template ^ is_template) | |
1176 | return false; | |
1177 | else if (is_template) | |
1178 | { | |
1179 | tree friend_parms; | |
1180 | /* If both are templates, check the name of the two | |
1181 | TEMPLATE_DECL's first because is_friend didn't. */ | |
1182 | if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type)) | |
1183 | != DECL_NAME (friend)) | |
1184 | return false; | |
1185 | ||
1186 | /* Now check template parameter list. */ | |
1187 | friend_parms | |
1188 | = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend), | |
1189 | args, tf_none); | |
1190 | return comp_template_parms | |
1191 | (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)), | |
1192 | friend_parms); | |
1193 | } | |
1194 | else | |
1195 | return (DECL_NAME (decl) | |
1196 | == DECL_NAME (friend)); | |
7bdfc61c | 1197 | } |
1198 | } | |
f95fba26 | 1199 | return false; |
7bdfc61c | 1200 | } |
1201 | ||
668ae905 | 1202 | /* Register the specialization SPEC as a specialization of TMPL with |
c1d4295f | 1203 | the indicated ARGS. IS_FRIEND indicates whether the specialization |
1204 | is actually just a friend declaration. Returns SPEC, or an | |
1205 | equivalent prior declaration, if available. */ | |
668ae905 | 1206 | |
34197853 | 1207 | static tree |
c1d4295f | 1208 | register_specialization (tree spec, tree tmpl, tree args, bool is_friend) |
668ae905 | 1209 | { |
38d89ee9 | 1210 | tree fn; |
668ae905 | 1211 | |
b4df430b | 1212 | gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL); |
668ae905 | 1213 | |
9031d10b | 1214 | if (TREE_CODE (spec) == FUNCTION_DECL |
34197853 | 1215 | && uses_template_parms (DECL_TI_ARGS (spec))) |
1216 | /* This is the FUNCTION_DECL for a partial instantiation. Don't | |
1217 | register it; we want the corresponding TEMPLATE_DECL instead. | |
1218 | We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than | |
1219 | the more obvious `uses_template_parms (spec)' to avoid problems | |
1220 | with default function arguments. In particular, given | |
1221 | something like this: | |
1222 | ||
653e5405 | 1223 | template <class T> void f(T t1, T t = T()) |
34197853 | 1224 | |
1225 | the default argument expression is not substituted for in an | |
1226 | instantiation unless and until it is actually needed. */ | |
1227 | return spec; | |
a2a5f9d8 | 1228 | |
9031d10b | 1229 | fn = retrieve_specialization (tmpl, args, |
38d89ee9 | 1230 | /*class_specializations_p=*/false); |
1231 | /* We can sometimes try to re-register a specialization that we've | |
1232 | already got. In particular, regenerate_decl_from_template calls | |
1233 | duplicate_decls which will update the specialization list. But, | |
1234 | we'll still get called again here anyhow. It's more convenient | |
1235 | to simply allow this than to try to prevent it. */ | |
1236 | if (fn == spec) | |
1237 | return spec; | |
1238 | else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec)) | |
fc2d3974 | 1239 | { |
38d89ee9 | 1240 | if (DECL_TEMPLATE_INSTANTIATION (fn)) |
fc2d3974 | 1241 | { |
9031d10b | 1242 | if (TREE_USED (fn) |
38d89ee9 | 1243 | || DECL_EXPLICIT_INSTANTIATION (fn)) |
fc2d3974 | 1244 | { |
38d89ee9 | 1245 | error ("specialization of %qD after instantiation", |
1246 | fn); | |
23bf0363 | 1247 | return error_mark_node; |
fc2d3974 | 1248 | } |
38d89ee9 | 1249 | else |
e4e8c884 | 1250 | { |
cfdc832f | 1251 | tree clone; |
38d89ee9 | 1252 | /* This situation should occur only if the first |
1253 | specialization is an implicit instantiation, the | |
1254 | second is an explicit specialization, and the | |
1255 | implicit instantiation has not yet been used. That | |
1256 | situation can occur if we have implicitly | |
1257 | instantiated a member function and then specialized | |
1258 | it later. | |
9031d10b | 1259 | |
38d89ee9 | 1260 | We can also wind up here if a friend declaration that |
1261 | looked like an instantiation turns out to be a | |
1262 | specialization: | |
9031d10b | 1263 | |
38d89ee9 | 1264 | template <class T> void foo(T); |
1265 | class S { friend void foo<>(int) }; | |
9031d10b | 1266 | template <> void foo(int); |
1267 | ||
38d89ee9 | 1268 | We transform the existing DECL in place so that any |
1269 | pointers to it become pointers to the updated | |
1270 | declaration. | |
1271 | ||
1272 | If there was a definition for the template, but not | |
1273 | for the specialization, we want this to look as if | |
1274 | there were no definition, and vice versa. */ | |
1275 | DECL_INITIAL (fn) = NULL_TREE; | |
c1d4295f | 1276 | duplicate_decls (spec, fn, is_friend); |
cfdc832f | 1277 | /* The call to duplicate_decls will have applied |
074ab442 | 1278 | [temp.expl.spec]: |
cfdc832f | 1279 | |
074ab442 | 1280 | An explicit specialization of a function template |
cfdc832f | 1281 | is inline only if it is explicitly declared to be, |
149fdb98 | 1282 | and independently of whether its function template |
cfdc832f | 1283 | is. |
1284 | ||
1285 | to the primary function; now copy the inline bits to | |
074ab442 | 1286 | the various clones. */ |
cfdc832f | 1287 | FOR_EACH_CLONE (clone, fn) |
1288 | { | |
1289 | DECL_DECLARED_INLINE_P (clone) | |
1290 | = DECL_DECLARED_INLINE_P (fn); | |
1291 | DECL_INLINE (clone) | |
1292 | = DECL_INLINE (fn); | |
1293 | } | |
9ed9d9ec | 1294 | check_specialization_namespace (fn); |
9031d10b | 1295 | |
e4e8c884 | 1296 | return fn; |
1297 | } | |
fc2d3974 | 1298 | } |
38d89ee9 | 1299 | else if (DECL_TEMPLATE_SPECIALIZATION (fn)) |
1300 | { | |
c1d4295f | 1301 | if (!duplicate_decls (spec, fn, is_friend) && DECL_INITIAL (spec)) |
38d89ee9 | 1302 | /* Dup decl failed, but this is a new definition. Set the |
1303 | line number so any errors match this new | |
1304 | definition. */ | |
1305 | DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec); | |
9031d10b | 1306 | |
38d89ee9 | 1307 | return fn; |
1308 | } | |
e4e8c884 | 1309 | } |
1310 | ||
1311 | /* A specialization must be declared in the same namespace as the | |
1312 | template it is specializing. */ | |
1313 | if (DECL_TEMPLATE_SPECIALIZATION (spec) | |
1314 | && !check_specialization_namespace (tmpl)) | |
f0652ba9 | 1315 | DECL_CONTEXT (spec) = FROB_CONTEXT (decl_namespace_context (tmpl)); |
668ae905 | 1316 | |
38d89ee9 | 1317 | if (!optimize_specialization_lookup_p (tmpl)) |
1318 | DECL_TEMPLATE_SPECIALIZATIONS (tmpl) | |
1319 | = tree_cons (args, spec, DECL_TEMPLATE_SPECIALIZATIONS (tmpl)); | |
34197853 | 1320 | |
1321 | return spec; | |
1322 | } | |
1323 | ||
1324 | /* Unregister the specialization SPEC as a specialization of TMPL. | |
0886adbc | 1325 | Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true |
1326 | if the SPEC was listed as a specialization of TMPL. */ | |
34197853 | 1327 | |
0886adbc | 1328 | bool |
1329 | reregister_specialization (tree spec, tree tmpl, tree new_spec) | |
34197853 | 1330 | { |
1331 | tree* s; | |
1332 | ||
1333 | for (s = &DECL_TEMPLATE_SPECIALIZATIONS (tmpl); | |
1334 | *s != NULL_TREE; | |
1335 | s = &TREE_CHAIN (*s)) | |
1336 | if (TREE_VALUE (*s) == spec) | |
1337 | { | |
0886adbc | 1338 | if (!new_spec) |
1339 | *s = TREE_CHAIN (*s); | |
1340 | else | |
56e75739 | 1341 | TREE_VALUE (*s) = new_spec; |
34197853 | 1342 | return 1; |
1343 | } | |
1344 | ||
1345 | return 0; | |
668ae905 | 1346 | } |
1347 | ||
0a3b29ad | 1348 | /* Compare an entry in the local specializations hash table P1 (which |
1349 | is really a pointer to a TREE_LIST) with P2 (which is really a | |
1350 | DECL). */ | |
1351 | ||
1352 | static int | |
1353 | eq_local_specializations (const void *p1, const void *p2) | |
1354 | { | |
aae87fc3 | 1355 | return TREE_VALUE ((const_tree) p1) == (const_tree) p2; |
0a3b29ad | 1356 | } |
1357 | ||
cfa03699 | 1358 | /* Hash P1, an entry in the local specializations table. */ |
1359 | ||
1360 | static hashval_t | |
1361 | hash_local_specialization (const void* p1) | |
1362 | { | |
aae87fc3 | 1363 | return htab_hash_pointer (TREE_VALUE ((const_tree) p1)); |
cfa03699 | 1364 | } |
1365 | ||
db1ad296 | 1366 | /* Like register_specialization, but for local declarations. We are |
1367 | registering SPEC, an instantiation of TMPL. */ | |
70a658bd | 1368 | |
ca9f5265 | 1369 | static void |
807be5b4 | 1370 | register_local_specialization (tree spec, tree tmpl) |
70a658bd | 1371 | { |
db1ad296 | 1372 | void **slot; |
1373 | ||
9031d10b | 1374 | slot = htab_find_slot_with_hash (local_specializations, tmpl, |
cfa03699 | 1375 | htab_hash_pointer (tmpl), INSERT); |
0a3b29ad | 1376 | *slot = build_tree_list (spec, tmpl); |
70a658bd | 1377 | } |
1378 | ||
04ef83b7 | 1379 | /* TYPE is a class type. Returns true if TYPE is an explicitly |
1380 | specialized class. */ | |
1381 | ||
1382 | bool | |
1383 | explicit_class_specialization_p (tree type) | |
1384 | { | |
1385 | if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type)) | |
1386 | return false; | |
1387 | return !uses_template_parms (CLASSTYPE_TI_ARGS (type)); | |
1388 | } | |
1389 | ||
1146f179 | 1390 | /* Print the list of candidate FNS in an error message. */ |
1391 | ||
3effa7a7 | 1392 | void |
807be5b4 | 1393 | print_candidates (tree fns) |
1146f179 | 1394 | { |
1395 | tree fn; | |
1396 | ||
e1721763 | 1397 | const char *str = "candidates are:"; |
1146f179 | 1398 | |
1399 | for (fn = fns; fn != NULL_TREE; fn = TREE_CHAIN (fn)) | |
1400 | { | |
b90e9c68 | 1401 | tree f; |
1402 | ||
1403 | for (f = TREE_VALUE (fn); f; f = OVL_NEXT (f)) | |
3cf8b391 | 1404 | error ("%s %+#D", str, OVL_CURRENT (f)); |
1146f179 | 1405 | str = " "; |
1406 | } | |
1407 | } | |
1408 | ||
668ae905 | 1409 | /* Returns the template (one of the functions given by TEMPLATE_ID) |
1146f179 | 1410 | which can be specialized to match the indicated DECL with the |
88e449ea | 1411 | explicit template args given in TEMPLATE_ID. The DECL may be |
1412 | NULL_TREE if none is available. In that case, the functions in | |
1413 | TEMPLATE_ID are non-members. | |
1414 | ||
3160db1d | 1415 | If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a |
88e449ea | 1416 | specialization of a member template. |
1417 | ||
05a677c7 | 1418 | The TEMPLATE_COUNT is the number of references to qualifying |
1419 | template classes that appeared in the name of the function. See | |
1420 | check_explicit_specialization for a more accurate description. | |
1421 | ||
5e302b95 | 1422 | TSK indicates what kind of template declaration (if any) is being |
1423 | declared. TSK_TEMPLATE indicates that the declaration given by | |
1424 | DECL, though a FUNCTION_DECL, has template parameters, and is | |
1425 | therefore a template function. | |
1426 | ||
88e449ea | 1427 | The template args (those explicitly specified and those deduced) |
1428 | are output in a newly created vector *TARGS_OUT. | |
1429 | ||
1430 | If it is impossible to determine the result, an error message is | |
ca8f6b4d | 1431 | issued. The error_mark_node is returned to indicate failure. */ |
668ae905 | 1432 | |
863c3f96 | 1433 | static tree |
9031d10b | 1434 | determine_specialization (tree template_id, |
653e5405 | 1435 | tree decl, |
1436 | tree* targs_out, | |
05a677c7 | 1437 | int need_member_template, |
5e302b95 | 1438 | int template_count, |
1439 | tmpl_spec_kind tsk) | |
b1cfe2be | 1440 | { |
88e449ea | 1441 | tree fns; |
1442 | tree targs; | |
1443 | tree explicit_targs; | |
1444 | tree candidates = NULL_TREE; | |
d9db88e1 | 1445 | /* A TREE_LIST of templates of which DECL may be a specialization. |
1446 | The TREE_VALUE of each node is a TEMPLATE_DECL. The | |
1447 | corresponding TREE_PURPOSE is the set of template arguments that, | |
1448 | when used to instantiate the template, would produce a function | |
1449 | with the signature of DECL. */ | |
88e449ea | 1450 | tree templates = NULL_TREE; |
05a677c7 | 1451 | int header_count; |
1452 | struct cp_binding_level *b; | |
b1cfe2be | 1453 | |
1146f179 | 1454 | *targs_out = NULL_TREE; |
1455 | ||
f375ca81 | 1456 | if (template_id == error_mark_node || decl == error_mark_node) |
d2a8ac7d | 1457 | return error_mark_node; |
1458 | ||
1459 | fns = TREE_OPERAND (template_id, 0); | |
88e449ea | 1460 | explicit_targs = TREE_OPERAND (template_id, 1); |
d2a8ac7d | 1461 | |
063e90ea | 1462 | if (fns == error_mark_node) |
1463 | return error_mark_node; | |
1464 | ||
47cd6605 | 1465 | /* Check for baselinks. */ |
974baf38 | 1466 | if (BASELINK_P (fns)) |
8042247d | 1467 | fns = BASELINK_FUNCTIONS (fns); |
b1cfe2be | 1468 | |
974baf38 | 1469 | if (!is_overloaded_fn (fns)) |
1470 | { | |
39e7b4a4 | 1471 | error ("%qD is not a function template", fns); |
974baf38 | 1472 | return error_mark_node; |
1473 | } | |
1474 | ||
05a677c7 | 1475 | /* Count the number of template headers specified for this |
1476 | specialization. */ | |
1477 | header_count = 0; | |
9031d10b | 1478 | for (b = current_binding_level; |
3f42e1d4 | 1479 | b->kind == sk_template_parms; |
05a677c7 | 1480 | b = b->level_chain) |
1481 | ++header_count; | |
1482 | ||
8417823c | 1483 | for (; fns; fns = OVL_NEXT (fns)) |
b1cfe2be | 1484 | { |
92ce5851 | 1485 | tree fn = OVL_CURRENT (fns); |
88e449ea | 1486 | |
1487 | if (TREE_CODE (fn) == TEMPLATE_DECL) | |
d832c89b | 1488 | { |
1489 | tree decl_arg_types; | |
f352cc1d | 1490 | tree fn_arg_types; |
d832c89b | 1491 | |
05a677c7 | 1492 | /* In case of explicit specialization, we need to check if |
1493 | the number of template headers appearing in the specialization | |
1494 | is correct. This is usually done in check_explicit_specialization, | |
1495 | but the check done there cannot be exhaustive when specializing | |
1496 | member functions. Consider the following code: | |
1497 | ||
1498 | template <> void A<int>::f(int); | |
1499 | template <> template <> void A<int>::f(int); | |
1500 | ||
1501 | Assuming that A<int> is not itself an explicit specialization | |
1502 | already, the first line specializes "f" which is a non-template | |
1503 | member function, whilst the second line specializes "f" which | |
1504 | is a template member function. So both lines are syntactically | |
1505 | correct, and check_explicit_specialization does not reject | |
1506 | them. | |
9031d10b | 1507 | |
05a677c7 | 1508 | Here, we can do better, as we are matching the specialization |
1509 | against the declarations. We count the number of template | |
1510 | headers, and we check if they match TEMPLATE_COUNT + 1 | |
1511 | (TEMPLATE_COUNT is the number of qualifying template classes, | |
1512 | plus there must be another header for the member template | |
1513 | itself). | |
9031d10b | 1514 | |
05a677c7 | 1515 | Notice that if header_count is zero, this is not a |
1516 | specialization but rather a template instantiation, so there | |
1517 | is no check we can perform here. */ | |
1518 | if (header_count && header_count != template_count + 1) | |
1519 | continue; | |
1520 | ||
3f42e1d4 | 1521 | /* Check that the number of template arguments at the |
1522 | innermost level for DECL is the same as for FN. */ | |
1523 | if (current_binding_level->kind == sk_template_parms | |
1524 | && !current_binding_level->explicit_spec_p | |
1525 | && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn)) | |
074ab442 | 1526 | != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS |
aa32d258 | 1527 | (current_template_parms)))) |
3f42e1d4 | 1528 | continue; |
9031d10b | 1529 | |
14aa085c | 1530 | /* DECL might be a specialization of FN. */ |
1531 | decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl)); | |
1532 | fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn)); | |
1533 | ||
1534 | /* For a non-static member function, we need to make sure | |
1535 | that the const qualification is the same. Since | |
1536 | get_bindings does not try to merge the "this" parameter, | |
1537 | we must do the comparison explicitly. */ | |
1538 | if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn) | |
1539 | && !same_type_p (TREE_VALUE (fn_arg_types), | |
1540 | TREE_VALUE (decl_arg_types))) | |
1541 | continue; | |
1542 | ||
1543 | /* Skip the "this" parameter and, for constructors of | |
1544 | classes with virtual bases, the VTT parameter. A | |
1545 | full specialization of a constructor will have a VTT | |
1546 | parameter, but a template never will. */ | |
1547 | decl_arg_types | |
1548 | = skip_artificial_parms_for (decl, decl_arg_types); | |
1549 | fn_arg_types | |
1550 | = skip_artificial_parms_for (fn, fn_arg_types); | |
1551 | ||
1552 | /* Check that the number of function parameters matches. | |
1553 | For example, | |
1554 | template <class T> void f(int i = 0); | |
1555 | template <> void f<int>(); | |
1556 | The specialization f<int> is invalid but is not caught | |
1557 | by get_bindings below. */ | |
1558 | if (list_length (fn_arg_types) != list_length (decl_arg_types)) | |
1559 | continue; | |
1560 | ||
5e302b95 | 1561 | /* Function templates cannot be specializations; there are |
1562 | no partial specializations of functions. Therefore, if | |
1563 | the type of DECL does not match FN, there is no | |
1564 | match. */ | |
50d32ef1 | 1565 | if (tsk == tsk_template) |
1566 | { | |
1567 | if (compparms (fn_arg_types, decl_arg_types)) | |
1568 | candidates = tree_cons (NULL_TREE, fn, candidates); | |
1569 | continue; | |
1570 | } | |
5e302b95 | 1571 | |
d832c89b | 1572 | /* See whether this function might be a specialization of this |
1573 | template. */ | |
022b46b4 | 1574 | targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true); |
d832c89b | 1575 | |
1576 | if (!targs) | |
1577 | /* We cannot deduce template arguments that when used to | |
1578 | specialize TMPL will produce DECL. */ | |
1579 | continue; | |
1580 | ||
1581 | /* Save this template, and the arguments deduced. */ | |
1582 | templates = tree_cons (targs, fn, templates); | |
1583 | } | |
88e449ea | 1584 | else if (need_member_template) |
1585 | /* FN is an ordinary member function, and we need a | |
1586 | specialization of a member template. */ | |
d832c89b | 1587 | ; |
88e449ea | 1588 | else if (TREE_CODE (fn) != FUNCTION_DECL) |
1589 | /* We can get IDENTIFIER_NODEs here in certain erroneous | |
1590 | cases. */ | |
d832c89b | 1591 | ; |
88e449ea | 1592 | else if (!DECL_FUNCTION_MEMBER_P (fn)) |
1593 | /* This is just an ordinary non-member function. Nothing can | |
1594 | be a specialization of that. */ | |
d832c89b | 1595 | ; |
ee5f962c | 1596 | else if (DECL_ARTIFICIAL (fn)) |
1597 | /* Cannot specialize functions that are created implicitly. */ | |
d832c89b | 1598 | ; |
668ae905 | 1599 | else |
88e449ea | 1600 | { |
1601 | tree decl_arg_types; | |
b1cfe2be | 1602 | |
88e449ea | 1603 | /* This is an ordinary member function. However, since |
1604 | we're here, we can assume it's enclosing class is a | |
1605 | template class. For example, | |
9031d10b | 1606 | |
88e449ea | 1607 | template <typename T> struct S { void f(); }; |
1608 | template <> void S<int>::f() {} | |
1609 | ||
1610 | Here, S<int>::f is a non-template, but S<int> is a | |
1611 | template class. If FN has the same type as DECL, we | |
1612 | might be in business. */ | |
28fdf3f7 | 1613 | |
1614 | if (!DECL_TEMPLATE_INFO (fn)) | |
1615 | /* Its enclosing class is an explicit specialization | |
1616 | of a template class. This is not a candidate. */ | |
1617 | continue; | |
1618 | ||
88e449ea | 1619 | if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)), |
1620 | TREE_TYPE (TREE_TYPE (fn)))) | |
1621 | /* The return types differ. */ | |
1622 | continue; | |
1623 | ||
1624 | /* Adjust the type of DECL in case FN is a static member. */ | |
1625 | decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl)); | |
9031d10b | 1626 | if (DECL_STATIC_FUNCTION_P (fn) |
88e449ea | 1627 | && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)) |
1628 | decl_arg_types = TREE_CHAIN (decl_arg_types); | |
1629 | ||
9031d10b | 1630 | if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)), |
88e449ea | 1631 | decl_arg_types)) |
1632 | /* They match! */ | |
1633 | candidates = tree_cons (NULL_TREE, fn, candidates); | |
88e449ea | 1634 | } |
b1cfe2be | 1635 | } |
88e449ea | 1636 | |
ca8f6b4d | 1637 | if (templates && TREE_CHAIN (templates)) |
b1cfe2be | 1638 | { |
88e449ea | 1639 | /* We have: |
9031d10b | 1640 | |
88e449ea | 1641 | [temp.expl.spec] |
1642 | ||
1643 | It is possible for a specialization with a given function | |
1644 | signature to be instantiated from more than one function | |
1645 | template. In such cases, explicit specification of the | |
1646 | template arguments must be used to uniquely identify the | |
1647 | function template specialization being specialized. | |
1648 | ||
1649 | Note that here, there's no suggestion that we're supposed to | |
1650 | determine which of the candidate templates is most | |
1651 | specialized. However, we, also have: | |
1652 | ||
1653 | [temp.func.order] | |
1654 | ||
1655 | Partial ordering of overloaded function template | |
1656 | declarations is used in the following contexts to select | |
1657 | the function template to which a function template | |
9031d10b | 1658 | specialization refers: |
88e449ea | 1659 | |
653e5405 | 1660 | -- when an explicit specialization refers to a function |
9031d10b | 1661 | template. |
88e449ea | 1662 | |
1663 | So, we do use the partial ordering rules, at least for now. | |
6c0cc2cd | 1664 | This extension can only serve to make invalid programs valid, |
88e449ea | 1665 | so it's safe. And, there is strong anecdotal evidence that |
1666 | the committee intended the partial ordering rules to apply; | |
a17c2a3a | 1667 | the EDG front end has that behavior, and John Spicer claims |
88e449ea | 1668 | that the committee simply forgot to delete the wording in |
1669 | [temp.expl.spec]. */ | |
d9db88e1 | 1670 | tree tmpl = most_specialized_instantiation (templates); |
1671 | if (tmpl != error_mark_node) | |
1672 | { | |
1673 | templates = tmpl; | |
1674 | TREE_CHAIN (templates) = NULL_TREE; | |
1675 | } | |
1146f179 | 1676 | } |
1677 | ||
88e449ea | 1678 | if (templates == NULL_TREE && candidates == NULL_TREE) |
1146f179 | 1679 | { |
3cf8b391 | 1680 | error ("template-id %qD for %q+D does not match any template " |
1681 | "declaration", template_id, decl); | |
88e449ea | 1682 | return error_mark_node; |
b1cfe2be | 1683 | } |
88e449ea | 1684 | else if ((templates && TREE_CHAIN (templates)) |
ca8f6b4d | 1685 | || (candidates && TREE_CHAIN (candidates)) |
1686 | || (templates && candidates)) | |
b1cfe2be | 1687 | { |
3cf8b391 | 1688 | error ("ambiguous template specialization %qD for %q+D", |
1689 | template_id, decl); | |
ca8f6b4d | 1690 | chainon (candidates, templates); |
1691 | print_candidates (candidates); | |
88e449ea | 1692 | return error_mark_node; |
b1cfe2be | 1693 | } |
1694 | ||
47cd6605 | 1695 | /* We have one, and exactly one, match. */ |
88e449ea | 1696 | if (candidates) |
1697 | { | |
5e302b95 | 1698 | tree fn = TREE_VALUE (candidates); |
1699 | /* DECL is a re-declaration of a template function. */ | |
1700 | if (TREE_CODE (fn) == TEMPLATE_DECL) | |
1701 | return fn; | |
88e449ea | 1702 | /* It was a specialization of an ordinary member function in a |
1703 | template class. */ | |
5e302b95 | 1704 | *targs_out = copy_node (DECL_TI_ARGS (fn)); |
1705 | return DECL_TI_TEMPLATE (fn); | |
88e449ea | 1706 | } |
1707 | ||
1708 | /* It was a specialization of a template. */ | |
b8e0d419 | 1709 | targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates))); |
88e449ea | 1710 | if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs)) |
1711 | { | |
1712 | *targs_out = copy_node (targs); | |
9031d10b | 1713 | SET_TMPL_ARGS_LEVEL (*targs_out, |
88e449ea | 1714 | TMPL_ARGS_DEPTH (*targs_out), |
1715 | TREE_PURPOSE (templates)); | |
1716 | } | |
1717 | else | |
1718 | *targs_out = TREE_PURPOSE (templates); | |
1146f179 | 1719 | return TREE_VALUE (templates); |
471086d6 | 1720 | } |
fbe96aa1 | 1721 | |
1722 | /* Returns a chain of parameter types, exactly like the SPEC_TYPES, | |
1723 | but with the default argument values filled in from those in the | |
1724 | TMPL_TYPES. */ | |
9031d10b | 1725 | |
fbe96aa1 | 1726 | static tree |
807be5b4 | 1727 | copy_default_args_to_explicit_spec_1 (tree spec_types, |
1728 | tree tmpl_types) | |
fbe96aa1 | 1729 | { |
1730 | tree new_spec_types; | |
1731 | ||
1732 | if (!spec_types) | |
1733 | return NULL_TREE; | |
1734 | ||
1735 | if (spec_types == void_list_node) | |
1736 | return void_list_node; | |
1737 | ||
1738 | /* Substitute into the rest of the list. */ | |
1739 | new_spec_types = | |
1740 | copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types), | |
1741 | TREE_CHAIN (tmpl_types)); | |
9031d10b | 1742 | |
fbe96aa1 | 1743 | /* Add the default argument for this parameter. */ |
1744 | return hash_tree_cons (TREE_PURPOSE (tmpl_types), | |
1745 | TREE_VALUE (spec_types), | |
1746 | new_spec_types); | |
1747 | } | |
1748 | ||
1749 | /* DECL is an explicit specialization. Replicate default arguments | |
1750 | from the template it specializes. (That way, code like: | |
1751 | ||
1752 | template <class T> void f(T = 3); | |
1753 | template <> void f(double); | |
9031d10b | 1754 | void g () { f (); } |
fbe96aa1 | 1755 | |
1756 | works, as required.) An alternative approach would be to look up | |
1757 | the correct default arguments at the call-site, but this approach | |
1758 | is consistent with how implicit instantiations are handled. */ | |
1759 | ||
1760 | static void | |
807be5b4 | 1761 | copy_default_args_to_explicit_spec (tree decl) |
fbe96aa1 | 1762 | { |
1763 | tree tmpl; | |
1764 | tree spec_types; | |
1765 | tree tmpl_types; | |
1766 | tree new_spec_types; | |
1767 | tree old_type; | |
1768 | tree new_type; | |
1769 | tree t; | |
9039431e | 1770 | tree object_type = NULL_TREE; |
1771 | tree in_charge = NULL_TREE; | |
dcbeb3ef | 1772 | tree vtt = NULL_TREE; |
fbe96aa1 | 1773 | |
1774 | /* See if there's anything we need to do. */ | |
1775 | tmpl = DECL_TI_TEMPLATE (decl); | |
1776 | tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl))); | |
1777 | for (t = tmpl_types; t; t = TREE_CHAIN (t)) | |
1778 | if (TREE_PURPOSE (t)) | |
1779 | break; | |
1780 | if (!t) | |
1781 | return; | |
1782 | ||
1783 | old_type = TREE_TYPE (decl); | |
1784 | spec_types = TYPE_ARG_TYPES (old_type); | |
9031d10b | 1785 | |
fbe96aa1 | 1786 | if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)) |
1787 | { | |
9039431e | 1788 | /* Remove the this pointer, but remember the object's type for |
653e5405 | 1789 | CV quals. */ |
9039431e | 1790 | object_type = TREE_TYPE (TREE_VALUE (spec_types)); |
fbe96aa1 | 1791 | spec_types = TREE_CHAIN (spec_types); |
1792 | tmpl_types = TREE_CHAIN (tmpl_types); | |
9031d10b | 1793 | |
fbe96aa1 | 1794 | if (DECL_HAS_IN_CHARGE_PARM_P (decl)) |
653e5405 | 1795 | { |
1796 | /* DECL may contain more parameters than TMPL due to the extra | |
1797 | in-charge parameter in constructors and destructors. */ | |
1798 | in_charge = spec_types; | |
9039431e | 1799 | spec_types = TREE_CHAIN (spec_types); |
1800 | } | |
dcbeb3ef | 1801 | if (DECL_HAS_VTT_PARM_P (decl)) |
1802 | { | |
1803 | vtt = spec_types; | |
1804 | spec_types = TREE_CHAIN (spec_types); | |
1805 | } | |
fbe96aa1 | 1806 | } |
1807 | ||
1808 | /* Compute the merged default arguments. */ | |
9031d10b | 1809 | new_spec_types = |
fbe96aa1 | 1810 | copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types); |
1811 | ||
9039431e | 1812 | /* Compute the new FUNCTION_TYPE. */ |
1813 | if (object_type) | |
fbe96aa1 | 1814 | { |
dcbeb3ef | 1815 | if (vtt) |
653e5405 | 1816 | new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt), |
1817 | TREE_VALUE (vtt), | |
1818 | new_spec_types); | |
dcbeb3ef | 1819 | |
9039431e | 1820 | if (in_charge) |
653e5405 | 1821 | /* Put the in-charge parameter back. */ |
1822 | new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge), | |
1823 | TREE_VALUE (in_charge), | |
1824 | new_spec_types); | |
9039431e | 1825 | |
5bfb0742 | 1826 | new_type = build_method_type_directly (object_type, |
1827 | TREE_TYPE (old_type), | |
1828 | new_spec_types); | |
fbe96aa1 | 1829 | } |
fbe96aa1 | 1830 | else |
1831 | new_type = build_function_type (TREE_TYPE (old_type), | |
9039431e | 1832 | new_spec_types); |
b3beaf30 | 1833 | new_type = cp_build_type_attribute_variant (new_type, |
1834 | TYPE_ATTRIBUTES (old_type)); | |
fbe96aa1 | 1835 | new_type = build_exception_variant (new_type, |
1836 | TYPE_RAISES_EXCEPTIONS (old_type)); | |
1837 | TREE_TYPE (decl) = new_type; | |
1838 | } | |
1839 | ||
b1cfe2be | 1840 | /* Check to see if the function just declared, as indicated in |
668ae905 | 1841 | DECLARATOR, and in DECL, is a specialization of a function |
1842 | template. We may also discover that the declaration is an explicit | |
1843 | instantiation at this point. | |
1844 | ||
1146f179 | 1845 | Returns DECL, or an equivalent declaration that should be used |
88e449ea | 1846 | instead if all goes well. Issues an error message if something is |
1847 | amiss. Returns error_mark_node if the error is not easily | |
1848 | recoverable. | |
9031d10b | 1849 | |
1850 | FLAGS is a bitmask consisting of the following flags: | |
668ae905 | 1851 | |
668ae905 | 1852 | 2: The function has a definition. |
1853 | 4: The function is a friend. | |
668ae905 | 1854 | |
1855 | The TEMPLATE_COUNT is the number of references to qualifying | |
1856 | template classes that appeared in the name of the function. For | |
1857 | example, in | |
1858 | ||
1859 | template <class T> struct S { void f(); }; | |
1860 | void S<int>::f(); | |
9031d10b | 1861 | |
668ae905 | 1862 | the TEMPLATE_COUNT would be 1. However, explicitly specialized |
1863 | classes are not counted in the TEMPLATE_COUNT, so that in | |
1864 | ||
1865 | template <class T> struct S {}; | |
1866 | template <> struct S<int> { void f(); } | |
34197853 | 1867 | template <> void S<int>::f(); |
668ae905 | 1868 | |
1869 | the TEMPLATE_COUNT would be 0. (Note that this declaration is | |
6c0cc2cd | 1870 | invalid; there should be no template <>.) |
668ae905 | 1871 | |
1872 | If the function is a specialization, it is marked as such via | |
1873 | DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO | |
9031d10b | 1874 | is set up correctly, and it is added to the list of specializations |
668ae905 | 1875 | for that template. */ |
b1cfe2be | 1876 | |
1146f179 | 1877 | tree |
9031d10b | 1878 | check_explicit_specialization (tree declarator, |
653e5405 | 1879 | tree decl, |
1880 | int template_count, | |
1881 | int flags) | |
b1cfe2be | 1882 | { |
668ae905 | 1883 | int have_def = flags & 2; |
1884 | int is_friend = flags & 4; | |
1885 | int specialization = 0; | |
1146f179 | 1886 | int explicit_instantiation = 0; |
ae66148e | 1887 | int member_specialization = 0; |
668ae905 | 1888 | tree ctype = DECL_CLASS_CONTEXT (decl); |
1889 | tree dname = DECL_NAME (decl); | |
3467e461 | 1890 | tmpl_spec_kind tsk; |
b1cfe2be | 1891 | |
efc6b810 | 1892 | if (is_friend) |
1893 | { | |
1894 | if (!processing_specialization) | |
1895 | tsk = tsk_none; | |
1896 | else | |
1897 | tsk = tsk_excessive_parms; | |
1898 | } | |
1899 | else | |
1900 | tsk = current_tmpl_spec_kind (template_count); | |
668ae905 | 1901 | |
3467e461 | 1902 | switch (tsk) |
1903 | { | |
1904 | case tsk_none: | |
9031d10b | 1905 | if (processing_specialization) |
668ae905 | 1906 | { |
ae66148e | 1907 | specialization = 1; |
1908 | SET_DECL_TEMPLATE_SPECIALIZATION (decl); | |
668ae905 | 1909 | } |
3467e461 | 1910 | else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR) |
b1cfe2be | 1911 | { |
3467e461 | 1912 | if (is_friend) |
1913 | /* This could be something like: | |
668ae905 | 1914 | |
3467e461 | 1915 | template <class T> void f(T); |
1916 | class S { friend void f<>(int); } */ | |
1917 | specialization = 1; | |
1918 | else | |
1919 | { | |
1920 | /* This case handles bogus declarations like template <> | |
1921 | template <class T> void f<int>(); */ | |
1922 | ||
39e7b4a4 | 1923 | error ("template-id %qD in declaration of primary template", |
653e5405 | 1924 | declarator); |
3467e461 | 1925 | return decl; |
1926 | } | |
1927 | } | |
1928 | break; | |
1929 | ||
1930 | case tsk_invalid_member_spec: | |
1931 | /* The error has already been reported in | |
1932 | check_specialization_scope. */ | |
1933 | return error_mark_node; | |
1934 | ||
1935 | case tsk_invalid_expl_inst: | |
cf103c6c | 1936 | error ("template parameter list used in explicit instantiation"); |
3467e461 | 1937 | |
1938 | /* Fall through. */ | |
1939 | ||
1940 | case tsk_expl_inst: | |
ae66148e | 1941 | if (have_def) |
cf103c6c | 1942 | error ("definition provided for explicit instantiation"); |
9031d10b | 1943 | |
ae66148e | 1944 | explicit_instantiation = 1; |
3467e461 | 1945 | break; |
ae66148e | 1946 | |
3467e461 | 1947 | case tsk_excessive_parms: |
a312d54f | 1948 | case tsk_insufficient_parms: |
1949 | if (tsk == tsk_excessive_parms) | |
653e5405 | 1950 | error ("too many template parameter lists in declaration of %qD", |
a312d54f | 1951 | decl); |
1952 | else if (template_header_count) | |
39e7b4a4 | 1953 | error("too few template parameter lists in declaration of %qD", decl); |
a312d54f | 1954 | else |
39e7b4a4 | 1955 | error("explicit specialization of %qD must be introduced by " |
1e5fcbe2 | 1956 | "%<template <>%>", decl); |
668ae905 | 1957 | |
3467e461 | 1958 | /* Fall through. */ |
1959 | case tsk_expl_spec: | |
1960 | SET_DECL_TEMPLATE_SPECIALIZATION (decl); | |
1961 | if (ctype) | |
1962 | member_specialization = 1; | |
1963 | else | |
1964 | specialization = 1; | |
1965 | break; | |
3467e461 | 1966 | |
1967 | case tsk_template: | |
1968 | if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR) | |
000c4cfb | 1969 | { |
ae66148e | 1970 | /* This case handles bogus declarations like template <> |
1971 | template <class T> void f<int>(); */ | |
668ae905 | 1972 | |
b81be439 | 1973 | if (uses_template_parms (declarator)) |
39e7b4a4 | 1974 | error ("function template partial specialization %qD " |
58cf9bfa | 1975 | "is not allowed", declarator); |
b81be439 | 1976 | else |
39e7b4a4 | 1977 | error ("template-id %qD in declaration of primary template", |
653e5405 | 1978 | declarator); |
ae66148e | 1979 | return decl; |
b1cfe2be | 1980 | } |
3467e461 | 1981 | |
1982 | if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype)) | |
1983 | /* This is a specialization of a member template, without | |
1984 | specialization the containing class. Something like: | |
1985 | ||
1986 | template <class T> struct S { | |
9031d10b | 1987 | template <class U> void f (U); |
653e5405 | 1988 | }; |
3467e461 | 1989 | template <> template <class U> void S<int>::f(U) {} |
9031d10b | 1990 | |
3467e461 | 1991 | That's a specialization -- but of the entire template. */ |
1992 | specialization = 1; | |
1993 | break; | |
1994 | ||
1995 | default: | |
2e3e31d2 | 1996 | gcc_unreachable (); |
668ae905 | 1997 | } |
b1cfe2be | 1998 | |
a15ec2b8 | 1999 | if (specialization || member_specialization) |
2000 | { | |
2001 | tree t = TYPE_ARG_TYPES (TREE_TYPE (decl)); | |
2002 | for (; t; t = TREE_CHAIN (t)) | |
2003 | if (TREE_PURPOSE (t)) | |
2004 | { | |
cf103c6c | 2005 | pedwarn |
905d4035 | 2006 | ("default argument specified in explicit specialization"); |
a15ec2b8 | 2007 | break; |
2008 | } | |
2009 | } | |
2010 | ||
1146f179 | 2011 | if (specialization || member_specialization || explicit_instantiation) |
668ae905 | 2012 | { |
2013 | tree tmpl = NULL_TREE; | |
2014 | tree targs = NULL_TREE; | |
668ae905 | 2015 | |
2016 | /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */ | |
b1cfe2be | 2017 | if (TREE_CODE (declarator) != TEMPLATE_ID_EXPR) |
2018 | { | |
2019 | tree fns; | |
2020 | ||
b4df430b | 2021 | gcc_assert (TREE_CODE (declarator) == IDENTIFIER_NODE); |
797cb3b4 | 2022 | if (ctype) |
b1cfe2be | 2023 | fns = dname; |
797cb3b4 | 2024 | else |
2025 | { | |
2026 | /* If there is no class context, the explicit instantiation | |
653e5405 | 2027 | must be at namespace scope. */ |
b4df430b | 2028 | gcc_assert (DECL_NAMESPACE_SCOPE_P (decl)); |
797cb3b4 | 2029 | |
2030 | /* Find the namespace binding, using the declaration | |
653e5405 | 2031 | context. */ |
e69e338f | 2032 | fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname, |
ea4d28f3 | 2033 | false, true); |
786edca3 | 2034 | if (fns == error_mark_node || !is_overloaded_fn (fns)) |
42510d75 | 2035 | { |
2036 | error ("%qD is not a template function", dname); | |
2037 | fns = error_mark_node; | |
2038 | } | |
ea4d28f3 | 2039 | else |
2040 | { | |
2041 | tree fn = OVL_CURRENT (fns); | |
e69e338f | 2042 | if (!is_associated_namespace (CP_DECL_CONTEXT (decl), |
3a15b281 | 2043 | CP_DECL_CONTEXT (fn))) |
2044 | error ("%qD is not declared in %qD", | |
2045 | decl, current_namespace); | |
ea4d28f3 | 2046 | } |
797cb3b4 | 2047 | } |
b1cfe2be | 2048 | |
797cb3b4 | 2049 | declarator = lookup_template_function (fns, NULL_TREE); |
b1cfe2be | 2050 | } |
2051 | ||
d2a8ac7d | 2052 | if (declarator == error_mark_node) |
2053 | return error_mark_node; | |
2054 | ||
668ae905 | 2055 | if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype)) |
2056 | { | |
89e01b6b | 2057 | if (!explicit_instantiation) |
6c0cc2cd | 2058 | /* A specialization in class scope. This is invalid, |
1d42585d | 2059 | but the error will already have been flagged by |
2060 | check_specialization_scope. */ | |
2061 | return error_mark_node; | |
89e01b6b | 2062 | else |
6a44e72e | 2063 | { |
6c0cc2cd | 2064 | /* It's not valid to write an explicit instantiation in |
6a44e72e | 2065 | class scope, e.g.: |
89e01b6b | 2066 | |
653e5405 | 2067 | class C { template void f(); } |
89e01b6b | 2068 | |
6a44e72e | 2069 | This case is caught by the parser. However, on |
2070 | something like: | |
9031d10b | 2071 | |
6a44e72e | 2072 | template class C { void f(); }; |
89e01b6b | 2073 | |
6c0cc2cd | 2074 | (which is invalid) we can get here. The error will be |
6a44e72e | 2075 | issued later. */ |
2076 | ; | |
2077 | } | |
89e01b6b | 2078 | |
1146f179 | 2079 | return decl; |
668ae905 | 2080 | } |
9031d10b | 2081 | else if (ctype != NULL_TREE |
668ae905 | 2082 | && (TREE_CODE (TREE_OPERAND (declarator, 0)) == |
2083 | IDENTIFIER_NODE)) | |
b1cfe2be | 2084 | { |
668ae905 | 2085 | /* Find the list of functions in ctype that have the same |
2086 | name as the declared function. */ | |
2087 | tree name = TREE_OPERAND (declarator, 0); | |
88e449ea | 2088 | tree fns = NULL_TREE; |
2089 | int idx; | |
2090 | ||
4c487414 | 2091 | if (constructor_name_p (name, ctype)) |
b1cfe2be | 2092 | { |
668ae905 | 2093 | int is_constructor = DECL_CONSTRUCTOR_P (decl); |
9031d10b | 2094 | |
930e8175 | 2095 | if (is_constructor ? !TYPE_HAS_USER_CONSTRUCTOR (ctype) |
ed36f1cf | 2096 | : !CLASSTYPE_DESTRUCTORS (ctype)) |
668ae905 | 2097 | { |
2098 | /* From [temp.expl.spec]: | |
9031d10b | 2099 | |
668ae905 | 2100 | If such an explicit specialization for the member |
2101 | of a class template names an implicitly-declared | |
2102 | special member function (clause _special_), the | |
9031d10b | 2103 | program is ill-formed. |
1146f179 | 2104 | |
2105 | Similar language is found in [temp.explicit]. */ | |
cf103c6c | 2106 | error ("specialization of implicitly-declared special member function"); |
88e449ea | 2107 | return error_mark_node; |
668ae905 | 2108 | } |
b1cfe2be | 2109 | |
7c0fe510 | 2110 | name = is_constructor ? ctor_identifier : dtor_identifier; |
668ae905 | 2111 | } |
7c0fe510 | 2112 | |
f0edcca6 | 2113 | if (!DECL_CONV_FN_P (decl)) |
88e449ea | 2114 | { |
2115 | idx = lookup_fnfields_1 (ctype, name); | |
2116 | if (idx >= 0) | |
de5ab3f1 | 2117 | fns = VEC_index (tree, CLASSTYPE_METHOD_VEC (ctype), idx); |
88e449ea | 2118 | } |
2119 | else | |
2120 | { | |
046bfc77 | 2121 | VEC(tree,gc) *methods; |
de5ab3f1 | 2122 | tree ovl; |
88e449ea | 2123 | |
2124 | /* For a type-conversion operator, we cannot do a | |
2125 | name-based lookup. We might be looking for `operator | |
2126 | int' which will be a specialization of `operator T'. | |
2127 | So, we find *all* the conversion operators, and then | |
2128 | select from them. */ | |
2129 | fns = NULL_TREE; | |
2130 | ||
2131 | methods = CLASSTYPE_METHOD_VEC (ctype); | |
2132 | if (methods) | |
c0af458b | 2133 | for (idx = CLASSTYPE_FIRST_CONVERSION_SLOT; |
930bdacf | 2134 | VEC_iterate (tree, methods, idx, ovl); |
de5ab3f1 | 2135 | ++idx) |
88e449ea | 2136 | { |
de5ab3f1 | 2137 | if (!DECL_CONV_FN_P (OVL_CURRENT (ovl))) |
88e449ea | 2138 | /* There are no more conversion functions. */ |
2139 | break; | |
2140 | ||
2141 | /* Glue all these conversion functions together | |
2142 | with those we already have. */ | |
2143 | for (; ovl; ovl = OVL_NEXT (ovl)) | |
2144 | fns = ovl_cons (OVL_CURRENT (ovl), fns); | |
2145 | } | |
2146 | } | |
9031d10b | 2147 | |
2148 | if (fns == NULL_TREE) | |
b1cfe2be | 2149 | { |
39e7b4a4 | 2150 | error ("no member function %qD declared in %qT", name, ctype); |
88e449ea | 2151 | return error_mark_node; |
b1cfe2be | 2152 | } |
2153 | else | |
2154 | TREE_OPERAND (declarator, 0) = fns; | |
2155 | } | |
9031d10b | 2156 | |
1146f179 | 2157 | /* Figure out what exactly is being specialized at this point. |
2158 | Note that for an explicit instantiation, even one for a | |
3398e91d | 2159 | member function, we cannot tell apriori whether the |
1146f179 | 2160 | instantiation is for a member template, or just a member |
34197853 | 2161 | function of a template class. Even if a member template is |
2162 | being instantiated, the member template arguments may be | |
2163 | elided if they can be deduced from the rest of the | |
2164 | declaration. */ | |
1146f179 | 2165 | tmpl = determine_specialization (declarator, decl, |
9031d10b | 2166 | &targs, |
05a677c7 | 2167 | member_specialization, |
5e302b95 | 2168 | template_count, |
2169 | tsk); | |
9031d10b | 2170 | |
88e449ea | 2171 | if (!tmpl || tmpl == error_mark_node) |
2172 | /* We couldn't figure out what this declaration was | |
2173 | specializing. */ | |
2174 | return error_mark_node; | |
2175 | else | |
b1cfe2be | 2176 | { |
162ed5b9 | 2177 | tree gen_tmpl = most_general_template (tmpl); |
34197853 | 2178 | |
1146f179 | 2179 | if (explicit_instantiation) |
2180 | { | |
954885ed | 2181 | /* We don't set DECL_EXPLICIT_INSTANTIATION here; that |
9031d10b | 2182 | is done by do_decl_instantiation later. */ |
162ed5b9 | 2183 | |
2184 | int arg_depth = TMPL_ARGS_DEPTH (targs); | |
2185 | int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)); | |
2186 | ||
2187 | if (arg_depth > parm_depth) | |
2188 | { | |
2189 | /* If TMPL is not the most general template (for | |
2190 | example, if TMPL is a friend template that is | |
2191 | injected into namespace scope), then there will | |
ec0eee8d | 2192 | be too many levels of TARGS. Remove some of them |
162ed5b9 | 2193 | here. */ |
2194 | int i; | |
2195 | tree new_targs; | |
2196 | ||
19c3b3a6 | 2197 | new_targs = make_tree_vec (parm_depth); |
162ed5b9 | 2198 | for (i = arg_depth - parm_depth; i < arg_depth; ++i) |
2199 | TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth)) | |
2200 | = TREE_VEC_ELT (targs, i); | |
2201 | targs = new_targs; | |
2202 | } | |
9031d10b | 2203 | |
2bc53434 | 2204 | return instantiate_template (tmpl, targs, tf_error); |
1146f179 | 2205 | } |
3467e461 | 2206 | |
d0af9169 | 2207 | /* If we thought that the DECL was a member function, but it |
2208 | turns out to be specializing a static member function, | |
7ef14399 | 2209 | make DECL a static member function as well. */ |
d0af9169 | 2210 | if (DECL_STATIC_FUNCTION_P (tmpl) |
2211 | && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)) | |
7ef14399 | 2212 | revert_static_member_fn (decl); |
d0af9169 | 2213 | |
a32fe883 | 2214 | /* If this is a specialization of a member template of a |
5e302b95 | 2215 | template class, we want to return the TEMPLATE_DECL, not |
2216 | the specialization of it. */ | |
3467e461 | 2217 | if (tsk == tsk_template) |
2218 | { | |
2219 | SET_DECL_TEMPLATE_SPECIALIZATION (tmpl); | |
a32fe883 | 2220 | DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl)) = NULL_TREE; |
0933e909 | 2221 | if (have_def) |
2222 | { | |
346064d9 | 2223 | DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl); |
2224 | DECL_SOURCE_LOCATION (DECL_TEMPLATE_RESULT (tmpl)) | |
2225 | = DECL_SOURCE_LOCATION (decl); | |
419f16b0 | 2226 | /* We want to use the argument list specified in the |
2227 | definition, not in the original declaration. */ | |
2228 | DECL_ARGUMENTS (DECL_TEMPLATE_RESULT (tmpl)) | |
2229 | = DECL_ARGUMENTS (decl); | |
0933e909 | 2230 | } |
3467e461 | 2231 | return tmpl; |
2232 | } | |
2233 | ||
34197853 | 2234 | /* Set up the DECL_TEMPLATE_INFO for DECL. */ |
b0652a4f | 2235 | DECL_TEMPLATE_INFO (decl) = tree_cons (tmpl, targs, NULL_TREE); |
34197853 | 2236 | |
fbe96aa1 | 2237 | /* Inherit default function arguments from the template |
2238 | DECL is specializing. */ | |
2239 | copy_default_args_to_explicit_spec (decl); | |
2240 | ||
c319168d | 2241 | /* This specialization has the same protection as the |
2242 | template it specializes. */ | |
2243 | TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl); | |
2244 | TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl); | |
4a2849cb | 2245 | |
0c032b46 | 2246 | /* 7.1.1-1 [dcl.stc] |
2247 | ||
2248 | A storage-class-specifier shall not be specified in an | |
2249 | explicit specialization... | |
2250 | ||
2251 | The parser rejects these, so unless action is taken here, | |
2252 | explicit function specializations will always appear with | |
2253 | global linkage. | |
2254 | ||
2255 | The action recommended by the C++ CWG in response to C++ | |
2256 | defect report 605 is to make the storage class and linkage | |
2257 | of the explicit specialization match the templated function: | |
2258 | ||
2259 | http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605 | |
2260 | */ | |
2261 | if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl)) | |
2262 | { | |
2263 | tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl); | |
2264 | gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL); | |
2265 | ||
45cc50c5 | 2266 | /* This specialization has the same linkage and visibility as |
0c032b46 | 2267 | the function template it specializes. */ |
2268 | TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func); | |
566f872f | 2269 | if (! TREE_PUBLIC (decl)) |
2270 | { | |
2271 | DECL_INTERFACE_KNOWN (decl) = 1; | |
2272 | DECL_NOT_REALLY_EXTERN (decl) = 1; | |
2273 | } | |
0c032b46 | 2274 | DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func); |
2275 | if (DECL_VISIBILITY_SPECIFIED (tmpl_func)) | |
2276 | { | |
2277 | DECL_VISIBILITY_SPECIFIED (decl) = 1; | |
2278 | DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func); | |
2279 | } | |
2280 | } | |
2281 | ||
9ed9d9ec | 2282 | /* If DECL is a friend declaration, declared using an |
2283 | unqualified name, the namespace associated with DECL may | |
2284 | have been set incorrectly. For example, in: | |
074ab442 | 2285 | |
2286 | template <typename T> void f(T); | |
2287 | namespace N { | |
2288 | struct S { friend void f<int>(int); } | |
2289 | } | |
2290 | ||
2291 | we will have set the DECL_CONTEXT for the friend | |
2292 | declaration to N, rather than to the global namespace. */ | |
9ed9d9ec | 2293 | if (DECL_NAMESPACE_SCOPE_P (decl)) |
2294 | DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl); | |
66dfed4f | 2295 | |
b1cfe2be | 2296 | if (is_friend && !have_def) |
34197853 | 2297 | /* This is not really a declaration of a specialization. |
2298 | It's just the name of an instantiation. But, it's not | |
2299 | a request for an instantiation, either. */ | |
4012c20a | 2300 | SET_DECL_IMPLICIT_INSTANTIATION (decl); |
307e5cb7 | 2301 | else if (DECL_CONSTRUCTOR_P (decl) || DECL_DESTRUCTOR_P (decl)) |
2302 | /* This is indeed a specialization. In case of constructors | |
2303 | and destructors, we need in-charge and not-in-charge | |
2304 | versions in V3 ABI. */ | |
2305 | clone_function_decl (decl, /*update_method_vec_p=*/0); | |
668ae905 | 2306 | |
34197853 | 2307 | /* Register this specialization so that we can find it |
2308 | again. */ | |
c1d4295f | 2309 | decl = register_specialization (decl, gen_tmpl, targs, is_friend); |
b1cfe2be | 2310 | } |
2311 | } | |
9031d10b | 2312 | |
1146f179 | 2313 | return decl; |
b1cfe2be | 2314 | } |
668ae905 | 2315 | |
668ae905 | 2316 | /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template |
2317 | parameters. These are represented in the same format used for | |
2318 | DECL_TEMPLATE_PARMS. */ | |
2319 | ||
b4df430b | 2320 | int |
9f627b1a | 2321 | comp_template_parms (const_tree parms1, const_tree parms2) |
668ae905 | 2322 | { |
9f627b1a | 2323 | const_tree p1; |
2324 | const_tree p2; | |
668ae905 | 2325 | |
2326 | if (parms1 == parms2) | |
2327 | return 1; | |
2328 | ||
9031d10b | 2329 | for (p1 = parms1, p2 = parms2; |
668ae905 | 2330 | p1 != NULL_TREE && p2 != NULL_TREE; |
2331 | p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2)) | |
2332 | { | |
2333 | tree t1 = TREE_VALUE (p1); | |
2334 | tree t2 = TREE_VALUE (p2); | |
2335 | int i; | |
2336 | ||
b4df430b | 2337 | gcc_assert (TREE_CODE (t1) == TREE_VEC); |
2338 | gcc_assert (TREE_CODE (t2) == TREE_VEC); | |
668ae905 | 2339 | |
2340 | if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2)) | |
2341 | return 0; | |
2342 | ||
9031d10b | 2343 | for (i = 0; i < TREE_VEC_LENGTH (t2); ++i) |
668ae905 | 2344 | { |
21bd02a9 | 2345 | tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i)); |
2346 | tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i)); | |
508c7aa7 | 2347 | |
21bd02a9 | 2348 | /* If either of the template parameters are invalid, assume |
2349 | they match for the sake of error recovery. */ | |
2350 | if (parm1 == error_mark_node || parm2 == error_mark_node) | |
2351 | return 1; | |
668ae905 | 2352 | |
2353 | if (TREE_CODE (parm1) != TREE_CODE (parm2)) | |
2354 | return 0; | |
2355 | ||
d95d815d | 2356 | if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM |
2357 | && (TEMPLATE_TYPE_PARAMETER_PACK (parm1) | |
2358 | == TEMPLATE_TYPE_PARAMETER_PACK (parm2))) | |
668ae905 | 2359 | continue; |
daf9ff67 | 2360 | else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2))) |
668ae905 | 2361 | return 0; |
2362 | } | |
2363 | } | |
2364 | ||
2365 | if ((p1 != NULL_TREE) != (p2 != NULL_TREE)) | |
2366 | /* One set of parameters has more parameters lists than the | |
2367 | other. */ | |
2368 | return 0; | |
2369 | ||
2370 | return 1; | |
2371 | } | |
2372 | ||
d95d815d | 2373 | /* Determine whether PARM is a parameter pack. */ |
2374 | bool | |
9f627b1a | 2375 | template_parameter_pack_p (const_tree parm) |
d95d815d | 2376 | { |
2377 | /* Determine if we have a non-type template parameter pack. */ | |
2378 | if (TREE_CODE (parm) == PARM_DECL) | |
2379 | return (DECL_TEMPLATE_PARM_P (parm) | |
2380 | && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))); | |
2381 | ||
2382 | /* If this is a list of template parameters, we could get a | |
2383 | TYPE_DECL or a TEMPLATE_DECL. */ | |
2384 | if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL) | |
2385 | parm = TREE_TYPE (parm); | |
2386 | ||
2387 | return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM | |
2388 | || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM) | |
2389 | && TEMPLATE_TYPE_PARAMETER_PACK (parm)); | |
2390 | } | |
2391 | ||
d95d815d | 2392 | /* Determine whether ARGS describes a variadic template args list, |
2393 | i.e., one that is terminated by a template argument pack. */ | |
2394 | static bool | |
2395 | template_args_variadic_p (tree args) | |
2396 | { | |
2397 | int nargs; | |
2398 | tree last_parm; | |
2399 | ||
2400 | if (args == NULL_TREE) | |
2401 | return false; | |
2402 | ||
2403 | args = INNERMOST_TEMPLATE_ARGS (args); | |
2404 | nargs = TREE_VEC_LENGTH (args); | |
2405 | ||
2406 | if (nargs == 0) | |
2407 | return false; | |
2408 | ||
2409 | last_parm = TREE_VEC_ELT (args, nargs - 1); | |
2410 | ||
2411 | return ARGUMENT_PACK_P (last_parm); | |
2412 | } | |
2413 | ||
2414 | /* Generate a new name for the parameter pack name NAME (an | |
2415 | IDENTIFIER_NODE) that incorporates its */ | |
2416 | static tree | |
2417 | make_ith_pack_parameter_name (tree name, int i) | |
2418 | { | |
2419 | /* Munge the name to include the parameter index. */ | |
2420 | char numbuf[128]; | |
2421 | char* newname; | |
2422 | ||
2423 | sprintf(numbuf, "%i", i); | |
2424 | newname = (char*)alloca (IDENTIFIER_LENGTH (name) + strlen(numbuf) + 2); | |
2425 | sprintf(newname, "%s#%i", IDENTIFIER_POINTER (name), i); | |
2426 | return get_identifier (newname); | |
2427 | } | |
2428 | ||
4eaf1b43 | 2429 | /* Structure used to track the progress of find_parameter_packs_r. */ |
d95d815d | 2430 | struct find_parameter_pack_data |
2431 | { | |
4eaf1b43 | 2432 | /* TREE_LIST that will contain all of the parameter packs found by |
2433 | the traversal. */ | |
d95d815d | 2434 | tree* parameter_packs; |
4eaf1b43 | 2435 | |
2436 | /* Set of AST nodes that have been visited by the traversal. */ | |
d95d815d | 2437 | struct pointer_set_t *visited; |
2438 | }; | |
2439 | ||
08b4cffd | 2440 | /* Identifies all of the argument packs that occur in a template |
d95d815d | 2441 | argument and appends them to the TREE_LIST inside DATA, which is a |
08b4cffd | 2442 | find_parameter_pack_data structure. This is a subroutine of |
d95d815d | 2443 | make_pack_expansion and uses_parameter_packs. */ |
2444 | static tree | |
2445 | find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data) | |
2446 | { | |
2447 | tree t = *tp; | |
2448 | struct find_parameter_pack_data* ppd = | |
2449 | (struct find_parameter_pack_data*)data; | |
4eaf1b43 | 2450 | bool parameter_pack_p = false; |
d95d815d | 2451 | |
4eaf1b43 | 2452 | /* Identify whether this is a parameter pack or not. */ |
2453 | switch (TREE_CODE (t)) | |
2454 | { | |
2455 | case TEMPLATE_PARM_INDEX: | |
2456 | if (TEMPLATE_PARM_PARAMETER_PACK (t)) | |
2457 | parameter_pack_p = true; | |
2458 | break; | |
2459 | ||
2460 | case TEMPLATE_TYPE_PARM: | |
2461 | case TEMPLATE_TEMPLATE_PARM: | |
2462 | if (TEMPLATE_TYPE_PARAMETER_PACK (t)) | |
2463 | parameter_pack_p = true; | |
2464 | break; | |
2465 | ||
2466 | case PARM_DECL: | |
2467 | if (FUNCTION_PARAMETER_PACK_P (t)) | |
2468 | { | |
2469 | /* We don't want to walk into the type of a PARM_DECL, | |
2470 | because we don't want to see the type parameter pack. */ | |
2471 | *walk_subtrees = 0; | |
2472 | parameter_pack_p = true; | |
2473 | } | |
2474 | break; | |
2475 | ||
2476 | default: | |
2477 | /* Not a parameter pack. */ | |
2478 | break; | |
2479 | } | |
2480 | ||
2481 | if (parameter_pack_p) | |
2482 | { | |
2483 | /* Add this parameter pack to the list. */ | |
2484 | *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs); | |
4eaf1b43 | 2485 | } |
830a6615 | 2486 | |
4eaf1b43 | 2487 | if (TYPE_P (t)) |
2488 | cp_walk_tree (&TYPE_CONTEXT (t), | |
decf9b61 | 2489 | &find_parameter_packs_r, ppd, ppd->visited); |
4eaf1b43 | 2490 | |
d95d815d | 2491 | /* This switch statement will return immediately if we don't find a |
2492 | parameter pack. */ | |
2493 | switch (TREE_CODE (t)) | |
2494 | { | |
2495 | case TEMPLATE_PARM_INDEX: | |
d95d815d | 2496 | return NULL_TREE; |
2497 | ||
2498 | case BOUND_TEMPLATE_TEMPLATE_PARM: | |
4eaf1b43 | 2499 | /* Check the template itself. */ |
2500 | cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)), | |
decf9b61 | 2501 | &find_parameter_packs_r, ppd, ppd->visited); |
d95d815d | 2502 | /* Check the template arguments. */ |
20a8f962 | 2503 | cp_walk_tree (&TYPE_TI_ARGS (t), &find_parameter_packs_r, ppd, |
decf9b61 | 2504 | ppd->visited); |
d95d815d | 2505 | *walk_subtrees = 0; |
4eaf1b43 | 2506 | return NULL_TREE; |
d95d815d | 2507 | |
2508 | case TEMPLATE_TYPE_PARM: | |
2509 | case TEMPLATE_TEMPLATE_PARM: | |
d95d815d | 2510 | return NULL_TREE; |
2511 | ||
2512 | case PARM_DECL: | |
d95d815d | 2513 | return NULL_TREE; |
2514 | ||
2515 | case RECORD_TYPE: | |
2516 | if (TYPE_PTRMEMFUNC_P (t)) | |
2517 | return NULL_TREE; | |
2518 | /* Fall through. */ | |
2519 | ||
2520 | case UNION_TYPE: | |
2521 | case ENUMERAL_TYPE: | |
2522 | if (TYPE_TEMPLATE_INFO (t)) | |
4eaf1b43 | 2523 | cp_walk_tree (&TREE_VALUE (TYPE_TEMPLATE_INFO (t)), |
decf9b61 | 2524 | &find_parameter_packs_r, ppd, ppd->visited); |
d95d815d | 2525 | |
2526 | *walk_subtrees = 0; | |
2527 | return NULL_TREE; | |
2528 | ||
2529 | case TEMPLATE_DECL: | |
4eaf1b43 | 2530 | cp_walk_tree (&TREE_TYPE (t), |
decf9b61 | 2531 | &find_parameter_packs_r, ppd, ppd->visited); |
d95d815d | 2532 | return NULL_TREE; |
6c61a9b6 | 2533 | |
2534 | case TYPENAME_TYPE: | |
2535 | cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t), &find_parameter_packs_r, | |
decf9b61 | 2536 | ppd, ppd->visited); |
6c61a9b6 | 2537 | *walk_subtrees = 0; |
2538 | return NULL_TREE; | |
6c61a9b6 | 2539 | |
d95d815d | 2540 | case TYPE_PACK_EXPANSION: |
2541 | case EXPR_PACK_EXPANSION: | |
2542 | *walk_subtrees = 0; | |
2543 | return NULL_TREE; | |
2544 | ||
2aedc2ff | 2545 | case INTEGER_TYPE: |
20a8f962 | 2546 | cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r, |
decf9b61 | 2547 | ppd, ppd->visited); |
2aedc2ff | 2548 | *walk_subtrees = 0; |
2549 | return NULL_TREE; | |
2550 | ||
613687b1 | 2551 | case IDENTIFIER_NODE: |
decf9b61 | 2552 | cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd, |
2553 | ppd->visited); | |
613687b1 | 2554 | *walk_subtrees = 0; |
2555 | return NULL_TREE; | |
2556 | ||
d95d815d | 2557 | default: |
2558 | return NULL_TREE; | |
2559 | } | |
d95d815d | 2560 | |
2561 | return NULL_TREE; | |
2562 | } | |
2563 | ||
2564 | /* Determines if the expression or type T uses any parameter packs. */ | |
2565 | bool | |
2566 | uses_parameter_packs (tree t) | |
2567 | { | |
2568 | tree parameter_packs = NULL_TREE; | |
2569 | struct find_parameter_pack_data ppd; | |
2570 | ppd.parameter_packs = ¶meter_packs; | |
2571 | ppd.visited = pointer_set_create (); | |
decf9b61 | 2572 | cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited); |
855ed7a1 | 2573 | pointer_set_destroy (ppd.visited); |
d95d815d | 2574 | return parameter_packs != NULL_TREE; |
2575 | } | |
2576 | ||
2577 | /* Turn ARG, which may be an expression, type, or a TREE_LIST | |
2578 | representation a base-class initializer into a parameter pack | |
2579 | expansion. If all goes well, the resulting node will be an | |
2580 | EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST, | |
2581 | respectively. */ | |
2582 | tree | |
2583 | make_pack_expansion (tree arg) | |
2584 | { | |
2585 | tree result; | |
2586 | tree parameter_packs = NULL_TREE; | |
2587 | bool for_types = false; | |
2588 | struct find_parameter_pack_data ppd; | |
2589 | ||
2590 | if (!arg || arg == error_mark_node) | |
2591 | return arg; | |
2592 | ||
2593 | if (TREE_CODE (arg) == TREE_LIST) | |
2594 | { | |
2595 | /* The only time we will see a TREE_LIST here is for a base | |
2596 | class initializer. In this case, the TREE_PURPOSE will be a | |
2597 | _TYPE node (representing the base class expansion we're | |
2598 | initializing) and the TREE_VALUE will be a TREE_LIST | |
2599 | containing the initialization arguments. | |
2600 | ||
2601 | The resulting expansion looks somewhat different from most | |
2602 | expansions. Rather than returning just one _EXPANSION, we | |
2603 | return a TREE_LIST whose TREE_PURPOSE is a | |
2604 | TYPE_PACK_EXPANSION containing the bases that will be | |
2605 | initialized. The TREE_VALUE will be identical to the | |
2606 | original TREE_VALUE, which is a list of arguments that will | |
2607 | be passed to each base. We do not introduce any new pack | |
2608 | expansion nodes into the TREE_VALUE (although it is possible | |
2609 | that some already exist), because the TREE_PURPOSE and | |
2610 | TREE_VALUE all need to be expanded together with the same | |
2611 | _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the | |
2612 | resulting TREE_PURPOSE will mention the parameter packs in | |
2613 | both the bases and the arguments to the bases. */ | |
2614 | tree purpose; | |
2615 | tree value; | |
2616 | tree parameter_packs = NULL_TREE; | |
2617 | ||
2618 | /* Determine which parameter packs will be used by the base | |
2619 | class expansion. */ | |
2620 | ppd.visited = pointer_set_create (); | |
2621 | ppd.parameter_packs = ¶meter_packs; | |
20a8f962 | 2622 | cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r, |
decf9b61 | 2623 | &ppd, ppd.visited); |
d95d815d | 2624 | |
2625 | if (parameter_packs == NULL_TREE) | |
2626 | { | |
2627 | error ("base initializer expansion %<%T%> contains no parameter packs", arg); | |
855ed7a1 | 2628 | pointer_set_destroy (ppd.visited); |
d95d815d | 2629 | return error_mark_node; |
2630 | } | |
2631 | ||
2632 | if (TREE_VALUE (arg) != void_type_node) | |
2633 | { | |
2634 | /* Collect the sets of parameter packs used in each of the | |
2635 | initialization arguments. */ | |
2636 | for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value)) | |
2637 | { | |
2638 | /* Determine which parameter packs will be expanded in this | |
2639 | argument. */ | |
20a8f962 | 2640 | cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r, |
decf9b61 | 2641 | &ppd, ppd.visited); |
d95d815d | 2642 | } |
2643 | } | |
2644 | ||
855ed7a1 | 2645 | pointer_set_destroy (ppd.visited); |
2646 | ||
d95d815d | 2647 | /* Create the pack expansion type for the base type. */ |
2648 | purpose = make_node (TYPE_PACK_EXPANSION); | |
2649 | SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg)); | |
2650 | PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs; | |
2651 | ||
2652 | /* Just use structural equality for these TYPE_PACK_EXPANSIONS; | |
2653 | they will rarely be compared to anything. */ | |
2654 | SET_TYPE_STRUCTURAL_EQUALITY (purpose); | |
2655 | ||
2656 | return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE); | |
2657 | } | |
2658 | ||
2659 | if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL) | |
2660 | for_types = true; | |
2661 | ||
2662 | /* Build the PACK_EXPANSION_* node. */ | |
2663 | result = make_node (for_types ? TYPE_PACK_EXPANSION : EXPR_PACK_EXPANSION); | |
2664 | SET_PACK_EXPANSION_PATTERN (result, arg); | |
2665 | if (TREE_CODE (result) == EXPR_PACK_EXPANSION) | |
2666 | { | |
2667 | /* Propagate type and const-expression information. */ | |
2668 | TREE_TYPE (result) = TREE_TYPE (arg); | |
2669 | TREE_CONSTANT (result) = TREE_CONSTANT (arg); | |
2670 | } | |
2671 | else | |
2672 | /* Just use structural equality for these TYPE_PACK_EXPANSIONS; | |
2673 | they will rarely be compared to anything. */ | |
2674 | SET_TYPE_STRUCTURAL_EQUALITY (result); | |
2675 | ||
2676 | /* Determine which parameter packs will be expanded. */ | |
2677 | ppd.parameter_packs = ¶meter_packs; | |
2678 | ppd.visited = pointer_set_create (); | |
decf9b61 | 2679 | cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited); |
855ed7a1 | 2680 | pointer_set_destroy (ppd.visited); |
d95d815d | 2681 | |
2682 | /* Make sure we found some parameter packs. */ | |
2683 | if (parameter_packs == NULL_TREE) | |
2684 | { | |
2685 | if (TYPE_P (arg)) | |
2686 | error ("expansion pattern %<%T%> contains no argument packs", arg); | |
2687 | else | |
2688 | error ("expansion pattern %<%E%> contains no argument packs", arg); | |
2689 | return error_mark_node; | |
2690 | } | |
2691 | PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs; | |
2692 | ||
2693 | return result; | |
2694 | } | |
2695 | ||
2696 | /* Checks T for any "bare" parameter packs, which have not yet been | |
2697 | expanded, and issues an error if any are found. This operation can | |
2698 | only be done on full expressions or types (e.g., an expression | |
2699 | statement, "if" condition, etc.), because we could have expressions like: | |
2700 | ||
2701 | foo(f(g(h(args)))...) | |
2702 | ||
2703 | where "args" is a parameter pack. check_for_bare_parameter_packs | |
2704 | should not be called for the subexpressions args, h(args), | |
2705 | g(h(args)), or f(g(h(args))), because we would produce erroneous | |
a4f66688 | 2706 | error messages. |
2707 | ||
613687b1 | 2708 | Returns TRUE and emits an error if there were bare parameter packs, |
2709 | returns FALSE otherwise. */ | |
a4f66688 | 2710 | bool |
830a6615 | 2711 | check_for_bare_parameter_packs (tree t) |
d95d815d | 2712 | { |
2713 | tree parameter_packs = NULL_TREE; | |
2714 | struct find_parameter_pack_data ppd; | |
2715 | ||
830a6615 | 2716 | if (!processing_template_decl || !t || t == error_mark_node) |
613687b1 | 2717 | return false; |
d95d815d | 2718 | |
830a6615 | 2719 | if (TREE_CODE (t) == TYPE_DECL) |
2720 | t = TREE_TYPE (t); | |
d95d815d | 2721 | |
2722 | ppd.parameter_packs = ¶meter_packs; | |
2723 | ppd.visited = pointer_set_create (); | |
decf9b61 | 2724 | cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited); |
855ed7a1 | 2725 | pointer_set_destroy (ppd.visited); |
d95d815d | 2726 | |
a4f66688 | 2727 | if (parameter_packs) |
2728 | { | |
140beb32 | 2729 | error ("parameter packs not expanded with %<...%>:"); |
a4f66688 | 2730 | while (parameter_packs) |
2731 | { | |
2732 | tree pack = TREE_VALUE (parameter_packs); | |
2733 | tree name = NULL_TREE; | |
2734 | ||
2735 | if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM | |
2736 | || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM) | |
2737 | name = TYPE_NAME (pack); | |
2738 | else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX) | |
2739 | name = DECL_NAME (TEMPLATE_PARM_DECL (pack)); | |
2740 | else | |
2741 | name = DECL_NAME (pack); | |
4eaf1b43 | 2742 | |
2743 | if (name) | |
2744 | inform (" %qD", name); | |
2745 | else | |
2746 | inform (" <anonymous>"); | |
a4f66688 | 2747 | |
2748 | parameter_packs = TREE_CHAIN (parameter_packs); | |
2749 | } | |
2750 | ||
613687b1 | 2751 | return true; |
a4f66688 | 2752 | } |
2753 | ||
613687b1 | 2754 | return false; |
d95d815d | 2755 | } |
2756 | ||
2757 | /* Expand any parameter packs that occur in the template arguments in | |
2758 | ARGS. */ | |
2759 | tree | |
2760 | expand_template_argument_pack (tree args) | |
2761 | { | |
2762 | tree result_args = NULL_TREE; | |
2763 | int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0; | |
2764 | int num_result_args = -1; | |
2765 | ||
2766 | /* First, determine if we need to expand anything, and the number of | |
2767 | slots we'll need. */ | |
2768 | for (in_arg = 0; in_arg < nargs; ++in_arg) | |
2769 | { | |
2770 | tree arg = TREE_VEC_ELT (args, in_arg); | |
2771 | if (ARGUMENT_PACK_P (arg)) | |
2772 | { | |
2773 | int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)); | |
2774 | if (num_result_args < 0) | |
2775 | num_result_args = in_arg + num_packed; | |
2776 | else | |
2777 | num_result_args += num_packed; | |
2778 | } | |
2779 | else | |
2780 | { | |
2781 | if (num_result_args >= 0) | |
2782 | num_result_args++; | |
2783 | } | |
2784 | } | |
2785 | ||
2786 | /* If no expansion is necessary, we're done. */ | |
2787 | if (num_result_args < 0) | |
2788 | return args; | |
2789 | ||
2790 | /* Expand arguments. */ | |
2791 | result_args = make_tree_vec (num_result_args); | |
2792 | for (in_arg = 0; in_arg < nargs; ++in_arg) | |
2793 | { | |
2794 | tree arg = TREE_VEC_ELT (args, in_arg); | |
2795 | if (ARGUMENT_PACK_P (arg)) | |
2796 | { | |
2797 | tree packed = ARGUMENT_PACK_ARGS (arg); | |
2798 | int i, num_packed = TREE_VEC_LENGTH (packed); | |
2799 | for (i = 0; i < num_packed; ++i, ++out_arg) | |
2800 | TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i); | |
2801 | } | |
2802 | else | |
2803 | { | |
2804 | TREE_VEC_ELT (result_args, out_arg) = arg; | |
2805 | ++out_arg; | |
2806 | } | |
2807 | } | |
2808 | ||
2809 | return result_args; | |
2810 | } | |
2811 | ||
c24cced6 | 2812 | /* Complain if DECL shadows a template parameter. |
2813 | ||
2814 | [temp.local]: A template-parameter shall not be redeclared within its | |
2815 | scope (including nested scopes). */ | |
2816 | ||
2817 | void | |
807be5b4 | 2818 | check_template_shadow (tree decl) |
c24cced6 | 2819 | { |
1eaf178d | 2820 | tree olddecl; |
2821 | ||
cb712d5e | 2822 | /* If we're not in a template, we can't possibly shadow a template |
2823 | parameter. */ | |
2824 | if (!current_template_parms) | |
2825 | return; | |
2826 | ||
2827 | /* Figure out what we're shadowing. */ | |
1eaf178d | 2828 | if (TREE_CODE (decl) == OVERLOAD) |
2829 | decl = OVL_CURRENT (decl); | |
d09ae6d5 | 2830 | olddecl = innermost_non_namespace_value (DECL_NAME (decl)); |
c24cced6 | 2831 | |
cb712d5e | 2832 | /* If there's no previous binding for this name, we're not shadowing |
2833 | anything, let alone a template parameter. */ | |
2834 | if (!olddecl) | |
2835 | return; | |
2836 | ||
2837 | /* If we're not shadowing a template parameter, we're done. Note | |
2838 | that OLDDECL might be an OVERLOAD (or perhaps even an | |
2839 | ERROR_MARK), so we can't just blithely assume it to be a _DECL | |
2840 | node. */ | |
9308e976 | 2841 | if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl)) |
cb712d5e | 2842 | return; |
2843 | ||
2844 | /* We check for decl != olddecl to avoid bogus errors for using a | |
2845 | name inside a class. We check TPFI to avoid duplicate errors for | |
2846 | inline member templates. */ | |
9031d10b | 2847 | if (decl == olddecl |
cb712d5e | 2848 | || TEMPLATE_PARMS_FOR_INLINE (current_template_parms)) |
2849 | return; | |
2850 | ||
3cf8b391 | 2851 | error ("declaration of %q+#D", decl); |
2852 | error (" shadows template parm %q+#D", olddecl); | |
c24cced6 | 2853 | } |
ea6df0ef | 2854 | |
c24cced6 | 2855 | /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL, |
f3110581 | 2856 | ORIG_LEVEL, DECL, and TYPE. */ |
2857 | ||
2858 | static tree | |
9031d10b | 2859 | build_template_parm_index (int index, |
653e5405 | 2860 | int level, |
2861 | int orig_level, | |
2862 | tree decl, | |
2863 | tree type) | |
f3110581 | 2864 | { |
2865 | tree t = make_node (TEMPLATE_PARM_INDEX); | |
2866 | TEMPLATE_PARM_IDX (t) = index; | |
2867 | TEMPLATE_PARM_LEVEL (t) = level; | |
2868 | TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level; | |
2869 | TEMPLATE_PARM_DECL (t) = decl; | |
2870 | TREE_TYPE (t) = type; | |
2bc53434 | 2871 | TREE_CONSTANT (t) = TREE_CONSTANT (decl); |
2872 | TREE_READONLY (t) = TREE_READONLY (decl); | |
f3110581 | 2873 | |
2874 | return t; | |
2875 | } | |
2876 | ||
6753bca0 | 2877 | /* Find the canonical type parameter for the given template type |
dcf4cce5 | 2878 | parameter. Returns the canonical type parameter, which may be TYPE |
2879 | if no such parameter existed. */ | |
6753bca0 | 2880 | static tree |
2881 | canonical_type_parameter (tree type) | |
2882 | { | |
2883 | tree list; | |
2884 | int idx = TEMPLATE_TYPE_IDX (type); | |
2885 | if (!canonical_template_parms) | |
2886 | canonical_template_parms = VEC_alloc (tree, gc, idx+1); | |
2887 | ||
2888 | while (VEC_length (tree, canonical_template_parms) <= (unsigned)idx) | |
2889 | VEC_safe_push (tree, gc, canonical_template_parms, NULL_TREE); | |
2890 | ||
2891 | list = VEC_index (tree, canonical_template_parms, idx); | |
a09db423 | 2892 | while (list && !comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL)) |
6753bca0 | 2893 | list = TREE_CHAIN (list); |
2894 | ||
2895 | if (list) | |
2896 | return TREE_VALUE (list); | |
2897 | else | |
2898 | { | |
2899 | VEC_replace(tree, canonical_template_parms, idx, | |
2900 | tree_cons (NULL_TREE, type, | |
2901 | VEC_index (tree, canonical_template_parms, idx))); | |
2902 | return type; | |
2903 | } | |
2904 | } | |
2905 | ||
f3110581 | 2906 | /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose |
1ac9b32b | 2907 | TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a |
f3110581 | 2908 | TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a |
2909 | new one is created. */ | |
2910 | ||
9031d10b | 2911 | static tree |
864f2267 | 2912 | reduce_template_parm_level (tree index, tree type, int levels, tree args, |
2913 | tsubst_flags_t complain) | |
f3110581 | 2914 | { |
2915 | if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE | |
2916 | || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index)) | |
1ac9b32b | 2917 | != TEMPLATE_PARM_LEVEL (index) - levels)) |
f3110581 | 2918 | { |
2bc53434 | 2919 | tree orig_decl = TEMPLATE_PARM_DECL (index); |
2920 | tree decl, t; | |
9031d10b | 2921 | |
2bc53434 | 2922 | decl = build_decl (TREE_CODE (orig_decl), DECL_NAME (orig_decl), type); |
2923 | TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl); | |
2924 | TREE_READONLY (decl) = TREE_READONLY (orig_decl); | |
2925 | DECL_ARTIFICIAL (decl) = 1; | |
2926 | SET_DECL_TEMPLATE_PARM_P (decl); | |
9031d10b | 2927 | |
2bc53434 | 2928 | t = build_template_parm_index (TEMPLATE_PARM_IDX (index), |
1ac9b32b | 2929 | TEMPLATE_PARM_LEVEL (index) - levels, |
f3110581 | 2930 | TEMPLATE_PARM_ORIG_LEVEL (index), |
2931 | decl, type); | |
2932 | TEMPLATE_PARM_DESCENDANTS (index) = t; | |
d95d815d | 2933 | TEMPLATE_PARM_PARAMETER_PACK (t) |
2934 | = TEMPLATE_PARM_PARAMETER_PACK (index); | |
4a330908 | 2935 | |
5ded8c6f | 2936 | /* Template template parameters need this. */ |
864f2267 | 2937 | if (TREE_CODE (decl) == TEMPLATE_DECL) |
2938 | DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms | |
2939 | (DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index)), | |
2940 | args, complain); | |
f3110581 | 2941 | } |
2942 | ||
2943 | return TEMPLATE_PARM_DESCENDANTS (index); | |
2944 | } | |
2945 | ||
9b5f02dd | 2946 | /* Process information from new template parameter PARM and append it to the |
3046c0a3 | 2947 | LIST being built. This new parameter is a non-type parameter iff |
d95d815d | 2948 | IS_NON_TYPE is true. This new parameter is a parameter |
2949 | pack iff IS_PARAMETER_PACK is true. */ | |
96624a9e | 2950 | |
471086d6 | 2951 | tree |
d95d815d | 2952 | process_template_parm (tree list, tree parm, bool is_non_type, |
2953 | bool is_parameter_pack) | |
471086d6 | 2954 | { |
471086d6 | 2955 | tree decl = 0; |
f3ba5c6a | 2956 | tree defval; |
21bd02a9 | 2957 | tree err_parm_list; |
26f73e10 | 2958 | int idx = 0; |
f3110581 | 2959 | |
b4df430b | 2960 | gcc_assert (TREE_CODE (parm) == TREE_LIST); |
f3ba5c6a | 2961 | defval = TREE_PURPOSE (parm); |
e857e9c7 | 2962 | |
2963 | if (list) | |
2964 | { | |
26f73e10 | 2965 | tree p = tree_last (list); |
2966 | ||
21bd02a9 | 2967 | if (p && TREE_VALUE (p) != error_mark_node) |
26f73e10 | 2968 | { |
2969 | p = TREE_VALUE (p); | |
2970 | if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL) | |
2971 | idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p)); | |
2972 | else | |
2973 | idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p)); | |
2974 | } | |
e857e9c7 | 2975 | |
e857e9c7 | 2976 | ++idx; |
2977 | } | |
2978 | else | |
2979 | idx = 0; | |
2980 | ||
3046c0a3 | 2981 | if (is_non_type) |
471086d6 | 2982 | { |
3046c0a3 | 2983 | parm = TREE_VALUE (parm); |
2984 | ||
1f062702 | 2985 | SET_DECL_TEMPLATE_PARM_P (parm); |
f07be57a | 2986 | |
9b774a10 | 2987 | if (TREE_TYPE (parm) == error_mark_node) |
21bd02a9 | 2988 | { |
2989 | err_parm_list = build_tree_list (defval, parm); | |
2990 | TREE_VALUE (err_parm_list) = error_mark_node; | |
2991 | return chainon (list, err_parm_list); | |
2992 | } | |
9b774a10 | 2993 | else |
2994 | { | |
2995 | /* [temp.param] | |
f07be57a | 2996 | |
9b774a10 | 2997 | The top-level cv-qualifiers on the template-parameter are |
2998 | ignored when determining its type. */ | |
2999 | TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm)); | |
3000 | if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1)) | |
21bd02a9 | 3001 | { |
3002 | err_parm_list = build_tree_list (defval, parm); | |
3003 | TREE_VALUE (err_parm_list) = error_mark_node; | |
3004 | return chainon (list, err_parm_list); | |
3005 | } | |
d95d815d | 3006 | |
3007 | if (uses_parameter_packs (TREE_TYPE (parm)) && !is_parameter_pack) | |
3008 | { | |
3009 | /* This template parameter is not a parameter pack, but it | |
3010 | should be. Complain about "bare" parameter packs. */ | |
830a6615 | 3011 | check_for_bare_parameter_packs (TREE_TYPE (parm)); |
d95d815d | 3012 | |
3013 | /* Recover by calling this a parameter pack. */ | |
3014 | is_parameter_pack = true; | |
3015 | } | |
9b774a10 | 3016 | } |
f07be57a | 3017 | |
471086d6 | 3018 | /* A template parameter is not modifiable. */ |
4ee9c684 | 3019 | TREE_CONSTANT (parm) = 1; |
4ee9c684 | 3020 | TREE_READONLY (parm) = 1; |
471086d6 | 3021 | decl = build_decl (CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm)); |
4ee9c684 | 3022 | TREE_CONSTANT (decl) = 1; |
4ee9c684 | 3023 | TREE_READONLY (decl) = 1; |
9031d10b | 3024 | DECL_INITIAL (parm) = DECL_INITIAL (decl) |
f3110581 | 3025 | = build_template_parm_index (idx, processing_template_decl, |
3026 | processing_template_decl, | |
3027 | decl, TREE_TYPE (parm)); | |
d95d815d | 3028 | |
3029 | TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)) | |
3030 | = is_parameter_pack; | |
471086d6 | 3031 | } |
3032 | else | |
3033 | { | |
cd5dd2d8 | 3034 | tree t; |
3046c0a3 | 3035 | parm = TREE_VALUE (TREE_VALUE (parm)); |
9031d10b | 3036 | |
cd5dd2d8 | 3037 | if (parm && TREE_CODE (parm) == TEMPLATE_DECL) |
3038 | { | |
95397ff9 | 3039 | t = cxx_make_type (TEMPLATE_TEMPLATE_PARM); |
9031d10b | 3040 | /* This is for distinguishing between real templates and template |
cd5dd2d8 | 3041 | template parameters */ |
3042 | TREE_TYPE (parm) = t; | |
3043 | TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t; | |
3044 | decl = parm; | |
3045 | } | |
3046 | else | |
3047 | { | |
95397ff9 | 3048 | t = cxx_make_type (TEMPLATE_TYPE_PARM); |
331bc0ad | 3049 | /* parm is either IDENTIFIER_NODE or NULL_TREE. */ |
cd5dd2d8 | 3050 | decl = build_decl (TYPE_DECL, parm, t); |
3051 | } | |
9031d10b | 3052 | |
b0df6589 | 3053 | TYPE_NAME (t) = decl; |
3054 | TYPE_STUB_DECL (t) = decl; | |
f3ba5c6a | 3055 | parm = decl; |
f3110581 | 3056 | TEMPLATE_TYPE_PARM_INDEX (t) |
9031d10b | 3057 | = build_template_parm_index (idx, processing_template_decl, |
f3110581 | 3058 | processing_template_decl, |
3059 | decl, TREE_TYPE (parm)); | |
d95d815d | 3060 | TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack; |
6753bca0 | 3061 | TYPE_CANONICAL (t) = canonical_type_parameter (t); |
471086d6 | 3062 | } |
ecdd3d84 | 3063 | DECL_ARTIFICIAL (decl) = 1; |
ce23987e | 3064 | SET_DECL_TEMPLATE_PARM_P (decl); |
471086d6 | 3065 | pushdecl (decl); |
f3ba5c6a | 3066 | parm = build_tree_list (defval, parm); |
471086d6 | 3067 | return chainon (list, parm); |
3068 | } | |
3069 | ||
3070 | /* The end of a template parameter list has been reached. Process the | |
3071 | tree list into a parameter vector, converting each parameter into a more | |
3072 | useful form. Type parameters are saved as IDENTIFIER_NODEs, and others | |
3073 | as PARM_DECLs. */ | |
3074 | ||
3075 | tree | |
807be5b4 | 3076 | end_template_parm_list (tree parms) |
471086d6 | 3077 | { |
e857e9c7 | 3078 | int nparms; |
eff2dffd | 3079 | tree parm, next; |
e857e9c7 | 3080 | tree saved_parmlist = make_tree_vec (list_length (parms)); |
3081 | ||
e857e9c7 | 3082 | current_template_parms |
180f098b | 3083 | = tree_cons (size_int (processing_template_decl), |
e857e9c7 | 3084 | saved_parmlist, current_template_parms); |
471086d6 | 3085 | |
eff2dffd | 3086 | for (parm = parms, nparms = 0; parm; parm = next, nparms++) |
3087 | { | |
3088 | next = TREE_CHAIN (parm); | |
3089 | TREE_VEC_ELT (saved_parmlist, nparms) = parm; | |
3090 | TREE_CHAIN (parm) = NULL_TREE; | |
3091 | } | |
f3ba5c6a | 3092 | |
29926476 | 3093 | --processing_template_parmlist; |
3094 | ||
471086d6 | 3095 | return saved_parmlist; |
3096 | } | |
3097 | ||
e857e9c7 | 3098 | /* end_template_decl is called after a template declaration is seen. */ |
3099 | ||
471086d6 | 3100 | void |
807be5b4 | 3101 | end_template_decl (void) |
471086d6 | 3102 | { |
b1cfe2be | 3103 | reset_specialization (); |
3104 | ||
3cc0b4b9 | 3105 | if (! processing_template_decl) |
d3ce0f51 | 3106 | return; |
3107 | ||
e857e9c7 | 3108 | /* This matches the pushlevel in begin_template_parm_list. */ |
3467e461 | 3109 | finish_scope (); |
471086d6 | 3110 | |
e857e9c7 | 3111 | --processing_template_decl; |
3112 | current_template_parms = TREE_CHAIN (current_template_parms); | |
e857e9c7 | 3113 | } |
471086d6 | 3114 | |
df476b3b | 3115 | /* Within the declaration of a template, return all levels of template |
3116 | parameters that apply. The template parameters are represented as | |
3117 | a TREE_VEC, in the form documented in cp-tree.h for template | |
3118 | arguments. */ | |
38281c46 | 3119 | |
e75bce43 | 3120 | static tree |
807be5b4 | 3121 | current_template_args (void) |
e857e9c7 | 3122 | { |
34197853 | 3123 | tree header; |
6a44e72e | 3124 | tree args = NULL_TREE; |
34197853 | 3125 | int length = TMPL_PARMS_DEPTH (current_template_parms); |
64b4f183 | 3126 | int l = length; |
3127 | ||
34197853 | 3128 | /* If there is only one level of template parameters, we do not |
3129 | create a TREE_VEC of TREE_VECs. Instead, we return a single | |
3130 | TREE_VEC containing the arguments. */ | |
3131 | if (length > 1) | |
3132 | args = make_tree_vec (length); | |
3133 | ||
3134 | for (header = current_template_parms; header; header = TREE_CHAIN (header)) | |
471086d6 | 3135 | { |
e857e9c7 | 3136 | tree a = copy_node (TREE_VALUE (header)); |
34197853 | 3137 | int i; |
3138 | ||
e857e9c7 | 3139 | TREE_TYPE (a) = NULL_TREE; |
34197853 | 3140 | for (i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i) |
e857e9c7 | 3141 | { |
64b4f183 | 3142 | tree t = TREE_VEC_ELT (a, i); |
3143 | ||
34197853 | 3144 | /* T will be a list if we are called from within a |
64b4f183 | 3145 | begin/end_template_parm_list pair, but a vector directly |
3146 | if within a begin/end_member_template_processing pair. */ | |
9031d10b | 3147 | if (TREE_CODE (t) == TREE_LIST) |
64b4f183 | 3148 | { |
3149 | t = TREE_VALUE (t); | |
9031d10b | 3150 | |
d36c839d | 3151 | if (!error_operand_p (t)) |
21bd02a9 | 3152 | { |
3153 | if (TREE_CODE (t) == TYPE_DECL | |
3154 | || TREE_CODE (t) == TEMPLATE_DECL) | |
d95d815d | 3155 | { |
3156 | t = TREE_TYPE (t); | |
3157 | ||
3158 | if (TEMPLATE_TYPE_PARAMETER_PACK (t)) | |
3159 | { | |
3160 | /* Turn this argument into a TYPE_ARGUMENT_PACK | |
3161 | with a single element, which expands T. */ | |
3162 | tree vec = make_tree_vec (1); | |
3163 | TREE_VEC_ELT (vec, 0) = make_pack_expansion (t); | |
3164 | ||
3165 | t = make_node (TYPE_ARGUMENT_PACK); | |
3166 | SET_ARGUMENT_PACK_ARGS (t, vec); | |
3167 | } | |
3168 | } | |
3169 | else | |
3170 | { | |
3171 | t = DECL_INITIAL (t); | |
3172 | ||
3173 | if (TEMPLATE_PARM_PARAMETER_PACK (t)) | |
3174 | { | |
3175 | /* Turn this argument into a NONTYPE_ARGUMENT_PACK | |
3176 | with a single element, which expands T. */ | |
3177 | tree vec = make_tree_vec (1); | |
3178 | tree type = TREE_TYPE (TEMPLATE_PARM_DECL (t)); | |
3179 | TREE_VEC_ELT (vec, 0) = make_pack_expansion (t); | |
3180 | ||
3181 | t = make_node (NONTYPE_ARGUMENT_PACK); | |
3182 | SET_ARGUMENT_PACK_ARGS (t, vec); | |
3183 | TREE_TYPE (t) = type; | |
3184 | } | |
3185 | } | |
077320e1 | 3186 | TREE_VEC_ELT (a, i) = t; |
d95d815d | 3187 | } |
64b4f183 | 3188 | } |
e857e9c7 | 3189 | } |
34197853 | 3190 | |
3191 | if (length > 1) | |
3192 | TREE_VEC_ELT (args, --l) = a; | |
3193 | else | |
3194 | args = a; | |
471086d6 | 3195 | } |
3196 | ||
38281c46 | 3197 | return args; |
3198 | } | |
668ae905 | 3199 | |
1146f179 | 3200 | /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated |
38d89ee9 | 3201 | template PARMS. If MEMBER_TEMPLATE_P is true, the new template is |
d68806e8 | 3202 | a member template. Used by push_template_decl below. */ |
1146f179 | 3203 | |
668ae905 | 3204 | static tree |
38d89ee9 | 3205 | build_template_decl (tree decl, tree parms, bool member_template_p) |
668ae905 | 3206 | { |
3207 | tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE); | |
3208 | DECL_TEMPLATE_PARMS (tmpl) = parms; | |
3209 | DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl); | |
38d89ee9 | 3210 | DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p; |
668ae905 | 3211 | if (DECL_LANG_SPECIFIC (decl)) |
3212 | { | |
1eaf178d | 3213 | DECL_STATIC_FUNCTION_P (tmpl) = DECL_STATIC_FUNCTION_P (decl); |
3214 | DECL_CONSTRUCTOR_P (tmpl) = DECL_CONSTRUCTOR_P (decl); | |
cc3a9eb6 | 3215 | DECL_DESTRUCTOR_P (tmpl) = DECL_DESTRUCTOR_P (decl); |
3a8ec1e7 | 3216 | DECL_NONCONVERTING_P (tmpl) = DECL_NONCONVERTING_P (decl); |
98eaf693 | 3217 | DECL_ASSIGNMENT_OPERATOR_P (tmpl) = DECL_ASSIGNMENT_OPERATOR_P (decl); |
3218 | if (DECL_OVERLOADED_OPERATOR_P (decl)) | |
9031d10b | 3219 | SET_OVERLOADED_OPERATOR_CODE (tmpl, |
98eaf693 | 3220 | DECL_OVERLOADED_OPERATOR_P (decl)); |
668ae905 | 3221 | } |
3222 | ||
3223 | return tmpl; | |
3224 | } | |
3225 | ||
c906a2a7 | 3226 | struct template_parm_data |
3227 | { | |
1d42585d | 3228 | /* The level of the template parameters we are currently |
3229 | processing. */ | |
c906a2a7 | 3230 | int level; |
1d42585d | 3231 | |
3232 | /* The index of the specialization argument we are currently | |
3233 | processing. */ | |
3234 | int current_arg; | |
3235 | ||
3236 | /* An array whose size is the number of template parameters. The | |
3160db1d | 3237 | elements are nonzero if the parameter has been used in any one |
1d42585d | 3238 | of the arguments processed so far. */ |
c906a2a7 | 3239 | int* parms; |
1d42585d | 3240 | |
3241 | /* An array whose size is the number of template arguments. The | |
3160db1d | 3242 | elements are nonzero if the argument makes use of template |
1d42585d | 3243 | parameters of this level. */ |
3244 | int* arg_uses_template_parms; | |
c906a2a7 | 3245 | }; |
3246 | ||
3247 | /* Subroutine of push_template_decl used to see if each template | |
3248 | parameter in a partial specialization is used in the explicit | |
3249 | argument list. If T is of the LEVEL given in DATA (which is | |
3250 | treated as a template_parm_data*), then DATA->PARMS is marked | |
3251 | appropriately. */ | |
3252 | ||
3253 | static int | |
807be5b4 | 3254 | mark_template_parm (tree t, void* data) |
c906a2a7 | 3255 | { |
3256 | int level; | |
3257 | int idx; | |
3258 | struct template_parm_data* tpd = (struct template_parm_data*) data; | |
3259 | ||
3260 | if (TREE_CODE (t) == TEMPLATE_PARM_INDEX) | |
3261 | { | |
3262 | level = TEMPLATE_PARM_LEVEL (t); | |
3263 | idx = TEMPLATE_PARM_IDX (t); | |
3264 | } | |
3265 | else | |
3266 | { | |
3267 | level = TEMPLATE_TYPE_LEVEL (t); | |
3268 | idx = TEMPLATE_TYPE_IDX (t); | |
3269 | } | |
3270 | ||
3271 | if (level == tpd->level) | |
1d42585d | 3272 | { |
3273 | tpd->parms[idx] = 1; | |
3274 | tpd->arg_uses_template_parms[tpd->current_arg] = 1; | |
3275 | } | |
c906a2a7 | 3276 | |
3277 | /* Return zero so that for_each_template_parm will continue the | |
3278 | traversal of the tree; we want to mark *every* template parm. */ | |
3279 | return 0; | |
3280 | } | |
3281 | ||
1d42585d | 3282 | /* Process the partial specialization DECL. */ |
3283 | ||
863c3f96 | 3284 | static tree |
807be5b4 | 3285 | process_partial_specialization (tree decl) |
1d42585d | 3286 | { |
3287 | tree type = TREE_TYPE (decl); | |
3288 | tree maintmpl = CLASSTYPE_TI_TEMPLATE (type); | |
3289 | tree specargs = CLASSTYPE_TI_ARGS (type); | |
a32fe883 | 3290 | tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs); |
1d42585d | 3291 | tree inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms); |
3292 | tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl); | |
3293 | int nargs = TREE_VEC_LENGTH (inner_args); | |
3294 | int ntparms = TREE_VEC_LENGTH (inner_parms); | |
3295 | int i; | |
3296 | int did_error_intro = 0; | |
1d42585d | 3297 | struct template_parm_data tpd; |
3298 | struct template_parm_data tpd2; | |
3299 | ||
1d42585d | 3300 | /* We check that each of the template parameters given in the |
3301 | partial specialization is used in the argument list to the | |
3302 | specialization. For example: | |
3303 | ||
3304 | template <class T> struct S; | |
3305 | template <class T> struct S<T*>; | |
3306 | ||
3307 | The second declaration is OK because `T*' uses the template | |
3308 | parameter T, whereas | |
3309 | ||
3310 | template <class T> struct S<int>; | |
3311 | ||
3312 | is no good. Even trickier is: | |
3313 | ||
3314 | template <class T> | |
3315 | struct S1 | |
3316 | { | |
3317 | template <class U> | |
3318 | struct S2; | |
3319 | template <class U> | |
3320 | struct S2<T>; | |
3321 | }; | |
3322 | ||
6c0cc2cd | 3323 | The S2<T> declaration is actually invalid; it is a |
9031d10b | 3324 | full-specialization. Of course, |
1d42585d | 3325 | |
3326 | template <class U> | |
3327 | struct S2<T (*)(U)>; | |
3328 | ||
3329 | or some such would have been OK. */ | |
3330 | tpd.level = TMPL_PARMS_DEPTH (current_template_parms); | |
cc52f165 | 3331 | tpd.parms = (int *) alloca (sizeof (int) * ntparms); |
b9a7cc69 | 3332 | memset (tpd.parms, 0, sizeof (int) * ntparms); |
1d42585d | 3333 | |
cc52f165 | 3334 | tpd.arg_uses_template_parms = (int *) alloca (sizeof (int) * nargs); |
b9a7cc69 | 3335 | memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs); |
1d42585d | 3336 | for (i = 0; i < nargs; ++i) |
3337 | { | |
3338 | tpd.current_arg = i; | |
3339 | for_each_template_parm (TREE_VEC_ELT (inner_args, i), | |
3340 | &mark_template_parm, | |
6b8feb95 | 3341 | &tpd, |
6510331e | 3342 | NULL, |
3343 | /*include_nondeduced_p=*/false); | |
1d42585d | 3344 | } |
3345 | for (i = 0; i < ntparms; ++i) | |
3346 | if (tpd.parms[i] == 0) | |
3347 | { | |
3348 | /* One of the template parms was not used in the | |
a32fe883 | 3349 | specialization. */ |
1d42585d | 3350 | if (!did_error_intro) |
3351 | { | |
cf103c6c | 3352 | error ("template parameters not used in partial specialization:"); |
1d42585d | 3353 | did_error_intro = 1; |
3354 | } | |
3355 | ||
39e7b4a4 | 3356 | error (" %qD", TREE_VALUE (TREE_VEC_ELT (inner_parms, i))); |
1d42585d | 3357 | } |
3358 | ||
3359 | /* [temp.class.spec] | |
3360 | ||
3361 | The argument list of the specialization shall not be identical to | |
3362 | the implicit argument list of the primary template. */ | |
9031d10b | 3363 | if (comp_template_args |
3364 | (inner_args, | |
a32fe883 | 3365 | INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE |
3366 | (maintmpl))))) | |
39e7b4a4 | 3367 | error ("partial specialization %qT does not specialize any template arguments", type); |
1d42585d | 3368 | |
3369 | /* [temp.class.spec] | |
3370 | ||
3371 | A partially specialized non-type argument expression shall not | |
3372 | involve template parameters of the partial specialization except | |
3373 | when the argument expression is a simple identifier. | |
3374 | ||
3375 | The type of a template parameter corresponding to a specialized | |
3376 | non-type argument shall not be dependent on a parameter of the | |
d95d815d | 3377 | specialization. |
3378 | ||
3379 | Also, we verify that pack expansions only occur at the | |
3380 | end of the argument list. */ | |
b4df430b | 3381 | gcc_assert (nargs == DECL_NTPARMS (maintmpl)); |
1d42585d | 3382 | tpd2.parms = 0; |
3383 | for (i = 0; i < nargs; ++i) | |
3384 | { | |
d95d815d | 3385 | tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i)); |
1d42585d | 3386 | tree arg = TREE_VEC_ELT (inner_args, i); |
d95d815d | 3387 | tree packed_args = NULL_TREE; |
3388 | int j, len = 1; | |
1d42585d | 3389 | |
d95d815d | 3390 | if (ARGUMENT_PACK_P (arg)) |
3391 | { | |
3392 | /* Extract the arguments from the argument pack. We'll be | |
3393 | iterating over these in the following loop. */ | |
3394 | packed_args = ARGUMENT_PACK_ARGS (arg); | |
3395 | len = TREE_VEC_LENGTH (packed_args); | |
3396 | } | |
3397 | ||
3398 | for (j = 0; j < len; j++) | |
3399 | { | |
3400 | if (packed_args) | |
3401 | /* Get the Jth argument in the parameter pack. */ | |
3402 | arg = TREE_VEC_ELT (packed_args, j); | |
3403 | ||
3404 | if (PACK_EXPANSION_P (arg)) | |
3405 | { | |
3406 | /* Pack expansions must come at the end of the | |
3407 | argument list. */ | |
3408 | if ((packed_args && j < len - 1) | |
3409 | || (!packed_args && i < nargs - 1)) | |
3410 | { | |
3411 | if (TREE_CODE (arg) == EXPR_PACK_EXPANSION) | |
3412 | error ("parameter pack argument %qE must be at the end of the template argument list", arg); | |
3413 | else | |
4eaf1b43 | 3414 | error ("parameter pack argument %qT must be at the end of the template argument list", arg); |
3415 | ||
3416 | if (packed_args) | |
3417 | TREE_VEC_ELT (packed_args, j) = error_mark_node; | |
d95d815d | 3418 | } |
3419 | } | |
3420 | ||
3421 | if (TREE_CODE (arg) == EXPR_PACK_EXPANSION) | |
3422 | /* We only care about the pattern. */ | |
3423 | arg = PACK_EXPANSION_PATTERN (arg); | |
3424 | ||
3425 | if (/* These first two lines are the `non-type' bit. */ | |
3426 | !TYPE_P (arg) | |
3427 | && TREE_CODE (arg) != TEMPLATE_DECL | |
3428 | /* This next line is the `argument expression is not just a | |
3429 | simple identifier' condition and also the `specialized | |
3430 | non-type argument' bit. */ | |
3431 | && TREE_CODE (arg) != TEMPLATE_PARM_INDEX) | |
3432 | { | |
3433 | if ((!packed_args && tpd.arg_uses_template_parms[i]) | |
3434 | || (packed_args && uses_template_parms (arg))) | |
3435 | error ("template argument %qE involves template parameter(s)", | |
3436 | arg); | |
3437 | else | |
3438 | { | |
3439 | /* Look at the corresponding template parameter, | |
3440 | marking which template parameters its type depends | |
3441 | upon. */ | |
3442 | tree type = TREE_TYPE (parm); | |
3443 | ||
3444 | if (!tpd2.parms) | |
3445 | { | |
3446 | /* We haven't yet initialized TPD2. Do so now. */ | |
3447 | tpd2.arg_uses_template_parms | |
3448 | = (int *) alloca (sizeof (int) * nargs); | |
3449 | /* The number of parameters here is the number in the | |
3450 | main template, which, as checked in the assertion | |
3451 | above, is NARGS. */ | |
3452 | tpd2.parms = (int *) alloca (sizeof (int) * nargs); | |
3453 | tpd2.level = | |
3454 | TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl)); | |
3455 | } | |
3456 | ||
3457 | /* Mark the template parameters. But this time, we're | |
3458 | looking for the template parameters of the main | |
3459 | template, not in the specialization. */ | |
3460 | tpd2.current_arg = i; | |
3461 | tpd2.arg_uses_template_parms[i] = 0; | |
3462 | memset (tpd2.parms, 0, sizeof (int) * nargs); | |
3463 | for_each_template_parm (type, | |
3464 | &mark_template_parm, | |
3465 | &tpd2, | |
6510331e | 3466 | NULL, |
3467 | /*include_nondeduced_p=*/false); | |
d95d815d | 3468 | |
3469 | if (tpd2.arg_uses_template_parms [i]) | |
3470 | { | |
3471 | /* The type depended on some template parameters. | |
3472 | If they are fully specialized in the | |
3473 | specialization, that's OK. */ | |
3474 | int j; | |
3475 | for (j = 0; j < nargs; ++j) | |
3476 | if (tpd2.parms[j] != 0 | |
3477 | && tpd.arg_uses_template_parms [j]) | |
3478 | { | |
3479 | error ("type %qT of template argument %qE depends " | |
3480 | "on template parameter(s)", | |
3481 | type, | |
3482 | arg); | |
3483 | break; | |
3484 | } | |
3485 | } | |
3486 | } | |
3487 | } | |
3488 | } | |
1d42585d | 3489 | } |
3490 | ||
9031d10b | 3491 | if (retrieve_specialization (maintmpl, specargs, |
38d89ee9 | 3492 | /*class_specializations_p=*/true)) |
1d42585d | 3493 | /* We've already got this specialization. */ |
3494 | return decl; | |
3495 | ||
6392d297 | 3496 | DECL_TEMPLATE_SPECIALIZATIONS (maintmpl) |
aa32d258 | 3497 | = tree_cons (specargs, inner_parms, |
a4f66688 | 3498 | DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)); |
1d42585d | 3499 | TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type; |
3500 | return decl; | |
3501 | } | |
3502 | ||
faab44d2 | 3503 | /* Check that a template declaration's use of default arguments and |
3504 | parameter packs is not invalid. Here, PARMS are the template | |
3505 | parameters. IS_PRIMARY is nonzero if DECL is the thing declared by | |
3506 | a primary template. IS_PARTIAL is nonzero if DECL is a partial | |
3507 | specialization. | |
82d31768 | 3508 | |
df5066e2 | 3509 | |
82d31768 | 3510 | IS_FRIEND_DECL is nonzero if DECL is a friend function template |
3511 | declaration (but not a definition); 1 indicates a declaration, 2 | |
3512 | indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are | |
3513 | emitted for extraneous default arguments. | |
3514 | ||
3515 | Returns TRUE if there were no errors found, FALSE otherwise. */ | |
3516 | ||
3517 | bool | |
3518 | check_default_tmpl_args (tree decl, tree parms, int is_primary, | |
3519 | int is_partial, int is_friend_decl) | |
df5066e2 | 3520 | { |
e1721763 | 3521 | const char *msg; |
22971cf1 | 3522 | int last_level_to_check; |
3523 | tree parm_level; | |
82d31768 | 3524 | bool no_errors = true; |
df5066e2 | 3525 | |
9031d10b | 3526 | /* [temp.param] |
df5066e2 | 3527 | |
3528 | A default template-argument shall not be specified in a | |
3529 | function template declaration or a function template definition, nor | |
3530 | in the template-parameter-list of the definition of a member of a | |
3531 | class template. */ | |
3532 | ||
9ba4048d | 3533 | if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL) |
ad828bde | 3534 | /* You can't have a function template declaration in a local |
3535 | scope, nor you can you define a member of a class template in a | |
3536 | local scope. */ | |
82d31768 | 3537 | return true; |
ad828bde | 3538 | |
df5066e2 | 3539 | if (current_class_type |
3540 | && !TYPE_BEING_DEFINED (current_class_type) | |
6e4e8926 | 3541 | && DECL_LANG_SPECIFIC (decl) |
3dc151ad | 3542 | /* If this is either a friend defined in the scope of the class |
3543 | or a member function. */ | |
e4f430b5 | 3544 | && (DECL_FUNCTION_MEMBER_P (decl) |
3545 | ? same_type_p (DECL_CONTEXT (decl), current_class_type) | |
3546 | : DECL_FRIEND_CONTEXT (decl) | |
3547 | ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type) | |
3548 | : false) | |
3dc151ad | 3549 | /* And, if it was a member function, it really was defined in |
3550 | the scope of the class. */ | |
e4f430b5 | 3551 | && (!DECL_FUNCTION_MEMBER_P (decl) |
3552 | || DECL_INITIALIZED_IN_CLASS_P (decl))) | |
df5066e2 | 3553 | /* We already checked these parameters when the template was |
3dc151ad | 3554 | declared, so there's no need to do it again now. This function |
3555 | was defined in class scope, but we're processing it's body now | |
3556 | that the class is complete. */ | |
82d31768 | 3557 | return true; |
9031d10b | 3558 | |
82d31768 | 3559 | /* Core issue 226 (C++0x only): the following only applies to class |
3560 | templates. */ | |
6dcdb5de | 3561 | if ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL) |
22971cf1 | 3562 | { |
82d31768 | 3563 | /* [temp.param] |
22971cf1 | 3564 | |
82d31768 | 3565 | If a template-parameter has a default template-argument, all |
3566 | subsequent template-parameters shall have a default | |
3567 | template-argument supplied. */ | |
3568 | for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level)) | |
3569 | { | |
3570 | tree inner_parms = TREE_VALUE (parm_level); | |
3571 | int ntparms = TREE_VEC_LENGTH (inner_parms); | |
3572 | int seen_def_arg_p = 0; | |
3573 | int i; | |
508c7aa7 | 3574 | |
82d31768 | 3575 | for (i = 0; i < ntparms; ++i) |
3576 | { | |
3577 | tree parm = TREE_VEC_ELT (inner_parms, i); | |
508c7aa7 | 3578 | |
82d31768 | 3579 | if (parm == error_mark_node) |
3580 | continue; | |
3581 | ||
3582 | if (TREE_PURPOSE (parm)) | |
3583 | seen_def_arg_p = 1; | |
3584 | else if (seen_def_arg_p) | |
3585 | { | |
3586 | error ("no default argument for %qD", TREE_VALUE (parm)); | |
3587 | /* For better subsequent error-recovery, we indicate that | |
3588 | there should have been a default argument. */ | |
3589 | TREE_PURPOSE (parm) = error_mark_node; | |
3590 | no_errors = false; | |
3591 | } | |
faab44d2 | 3592 | else if (is_primary |
3593 | && !is_partial | |
3594 | && !is_friend_decl | |
3595 | && TREE_CODE (decl) == TYPE_DECL | |
3596 | && i < ntparms - 1 | |
3597 | && template_parameter_pack_p (TREE_VALUE (parm))) | |
3598 | { | |
3599 | /* A primary class template can only have one | |
3600 | parameter pack, at the end of the template | |
3601 | parameter list. */ | |
3602 | ||
3603 | if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL) | |
3604 | error ("parameter pack %qE must be at the end of the" | |
3605 | " template parameter list", TREE_VALUE (parm)); | |
3606 | else | |
3607 | error ("parameter pack %qT must be at the end of the" | |
3608 | " template parameter list", | |
3609 | TREE_TYPE (TREE_VALUE (parm))); | |
3610 | ||
3611 | TREE_VALUE (TREE_VEC_ELT (inner_parms, i)) | |
3612 | = error_mark_node; | |
3613 | no_errors = false; | |
3614 | } | |
82d31768 | 3615 | } |
3616 | } | |
22971cf1 | 3617 | } |
3618 | ||
6dcdb5de | 3619 | if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL) |
82d31768 | 3620 | || is_partial |
3621 | || !is_primary | |
3622 | || is_friend_decl) | |
df5066e2 | 3623 | /* For an ordinary class template, default template arguments are |
3624 | allowed at the innermost level, e.g.: | |
653e5405 | 3625 | template <class T = int> |
df5066e2 | 3626 | struct S {}; |
3627 | but, in a partial specialization, they're not allowed even | |
3628 | there, as we have in [temp.class.spec]: | |
9031d10b | 3629 | |
df5066e2 | 3630 | The template parameter list of a specialization shall not |
9031d10b | 3631 | contain default template argument values. |
df5066e2 | 3632 | |
82d31768 | 3633 | So, for a partial specialization, or for a function template |
3634 | (in C++98/C++03), we look at all of them. */ | |
df5066e2 | 3635 | ; |
3636 | else | |
3637 | /* But, for a primary class template that is not a partial | |
3638 | specialization we look at all template parameters except the | |
3639 | innermost ones. */ | |
3640 | parms = TREE_CHAIN (parms); | |
3641 | ||
3642 | /* Figure out what error message to issue. */ | |
82d31768 | 3643 | if (is_friend_decl == 2) |
3644 | msg = "default template arguments may not be used in function template friend re-declaration"; | |
3645 | else if (is_friend_decl) | |
3646 | msg = "default template arguments may not be used in function template friend declarations"; | |
6dcdb5de | 3647 | else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98)) |
8ca064d7 | 3648 | msg = "default template arguments may not be used in function templates"; |
df5066e2 | 3649 | else if (is_partial) |
8ca064d7 | 3650 | msg = "default template arguments may not be used in partial specializations"; |
df5066e2 | 3651 | else |
39e7b4a4 | 3652 | msg = "default argument for template parameter for class enclosing %qD"; |
df5066e2 | 3653 | |
3654 | if (current_class_type && TYPE_BEING_DEFINED (current_class_type)) | |
3655 | /* If we're inside a class definition, there's no need to | |
3effa7a7 | 3656 | examine the parameters to the class itself. On the one |
df5066e2 | 3657 | hand, they will be checked when the class is defined, and, |
6c0cc2cd | 3658 | on the other, default arguments are valid in things like: |
653e5405 | 3659 | template <class T = double> |
3660 | struct S { template <class U> void f(U); }; | |
df5066e2 | 3661 | Here the default argument for `S' has no bearing on the |
3662 | declaration of `f'. */ | |
3663 | last_level_to_check = template_class_depth (current_class_type) + 1; | |
3664 | else | |
3665 | /* Check everything. */ | |
3666 | last_level_to_check = 0; | |
3667 | ||
9031d10b | 3668 | for (parm_level = parms; |
3669 | parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check; | |
22971cf1 | 3670 | parm_level = TREE_CHAIN (parm_level)) |
df5066e2 | 3671 | { |
22971cf1 | 3672 | tree inner_parms = TREE_VALUE (parm_level); |
3673 | int i; | |
3674 | int ntparms; | |
df5066e2 | 3675 | |
3676 | ntparms = TREE_VEC_LENGTH (inner_parms); | |
9031d10b | 3677 | for (i = 0; i < ntparms; ++i) |
508c7aa7 | 3678 | { |
3679 | if (TREE_VEC_ELT (inner_parms, i) == error_mark_node) | |
3680 | continue; | |
df5066e2 | 3681 | |
508c7aa7 | 3682 | if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i))) |
3683 | { | |
3684 | if (msg) | |
3685 | { | |
82d31768 | 3686 | no_errors = false; |
3687 | if (is_friend_decl == 2) | |
3688 | return no_errors; | |
3689 | ||
508c7aa7 | 3690 | error (msg, decl); |
3691 | msg = 0; | |
3692 | } | |
3693 | ||
3694 | /* Clear out the default argument so that we are not | |
3695 | confused later. */ | |
3696 | TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE; | |
3697 | } | |
3698 | } | |
df5066e2 | 3699 | |
3700 | /* At this point, if we're still interested in issuing messages, | |
3701 | they must apply to classes surrounding the object declared. */ | |
3702 | if (msg) | |
9031d10b | 3703 | msg = "default argument for template parameter for class enclosing %qD"; |
df5066e2 | 3704 | } |
82d31768 | 3705 | |
3706 | return no_errors; | |
df5066e2 | 3707 | } |
3708 | ||
c0af458b | 3709 | /* Worker for push_template_decl_real, called via |
3710 | for_each_template_parm. DATA is really an int, indicating the | |
3711 | level of the parameters we are interested in. If T is a template | |
3160db1d | 3712 | parameter of that level, return nonzero. */ |
c0af458b | 3713 | |
3714 | static int | |
807be5b4 | 3715 | template_parm_this_level_p (tree t, void* data) |
c0af458b | 3716 | { |
bfb2681d | 3717 | int this_level = *(int *)data; |
c0af458b | 3718 | int level; |
3719 | ||
3720 | if (TREE_CODE (t) == TEMPLATE_PARM_INDEX) | |
3721 | level = TEMPLATE_PARM_LEVEL (t); | |
3722 | else | |
3723 | level = TEMPLATE_TYPE_LEVEL (t); | |
3724 | return level == this_level; | |
3725 | } | |
3726 | ||
1ca935fd | 3727 | /* Creates a TEMPLATE_DECL for the indicated DECL using the template |
f3110581 | 3728 | parameters given by current_template_args, or reuses a |
3729 | previously existing one, if appropriate. Returns the DECL, or an | |
9031d10b | 3730 | equivalent one, if it is replaced via a call to duplicate_decls. |
0f0d5a69 | 3731 | |
c1d4295f | 3732 | If IS_FRIEND is true, DECL is a friend declaration. */ |
1ca935fd | 3733 | |
3734 | tree | |
c1d4295f | 3735 | push_template_decl_real (tree decl, bool is_friend) |
38281c46 | 3736 | { |
3737 | tree tmpl; | |
f3110581 | 3738 | tree args; |
38281c46 | 3739 | tree info; |
f3110581 | 3740 | tree ctx; |
3741 | int primary; | |
df5066e2 | 3742 | int is_partial; |
900f01f2 | 3743 | int new_template_p = 0; |
38d89ee9 | 3744 | /* True if the template is a member template, in the sense of |
3745 | [temp.mem]. */ | |
3746 | bool member_template_p = false; | |
df5066e2 | 3747 | |
1940f978 | 3748 | if (decl == error_mark_node) |
3749 | return decl; | |
3750 | ||
df5066e2 | 3751 | /* See if this is a partial specialization. */ |
70a658bd | 3752 | is_partial = (DECL_IMPLICIT_TYPEDEF_P (decl) |
df5066e2 | 3753 | && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE |
3a8f9e39 | 3754 | && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl))); |
0f0d5a69 | 3755 | |
c1d4295f | 3756 | if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl)) |
3757 | is_friend = true; | |
f3110581 | 3758 | |
3759 | if (is_friend) | |
3760 | /* For a friend, we want the context of the friend function, not | |
3761 | the type of which it is a friend. */ | |
3762 | ctx = DECL_CONTEXT (decl); | |
9ba4048d | 3763 | else if (CP_DECL_CONTEXT (decl) |
3764 | && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL) | |
f3110581 | 3765 | /* In the case of a virtual function, we want the class in which |
3766 | it is defined. */ | |
9ba4048d | 3767 | ctx = CP_DECL_CONTEXT (decl); |
f3110581 | 3768 | else |
ec0eee8d | 3769 | /* Otherwise, if we're currently defining some class, the DECL |
f3110581 | 3770 | is assumed to be a member of the class. */ |
70a658bd | 3771 | ctx = current_scope (); |
f3110581 | 3772 | |
8417823c | 3773 | if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL) |
3774 | ctx = NULL_TREE; | |
3775 | ||
3776 | if (!DECL_CONTEXT (decl)) | |
d8396542 | 3777 | DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace); |
8417823c | 3778 | |
df5066e2 | 3779 | /* See if this is a primary template. */ |
7209d041 | 3780 | if (is_friend && ctx) |
3781 | /* A friend template that specifies a class context, i.e. | |
3782 | template <typename T> friend void A<T>::f(); | |
3783 | is not primary. */ | |
3784 | primary = 0; | |
3785 | else | |
3786 | primary = template_parm_scope_p (); | |
38281c46 | 3787 | |
1f3fed0b | 3788 | if (primary) |
3789 | { | |
38d89ee9 | 3790 | if (DECL_CLASS_SCOPE_P (decl)) |
3791 | member_template_p = true; | |
9f25cdd8 | 3792 | if (TREE_CODE (decl) == TYPE_DECL |
3793 | && ANON_AGGRNAME_P (DECL_NAME (decl))) | |
a4aaa624 | 3794 | { |
3795 | error ("template class without a name"); | |
3796 | return error_mark_node; | |
3797 | } | |
e8dd671e | 3798 | else if (TREE_CODE (decl) == FUNCTION_DECL) |
b7d1e8ea | 3799 | { |
e8dd671e | 3800 | if (DECL_DESTRUCTOR_P (decl)) |
3801 | { | |
3802 | /* [temp.mem] | |
9031d10b | 3803 | |
653e5405 | 3804 | A destructor shall not be a member template. */ |
39e7b4a4 | 3805 | error ("destructor %qD declared as member template", decl); |
e8dd671e | 3806 | return error_mark_node; |
3807 | } | |
3808 | if (NEW_DELETE_OPNAME_P (DECL_NAME (decl)) | |
3809 | && (!TYPE_ARG_TYPES (TREE_TYPE (decl)) | |
3810 | || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node | |
3811 | || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl))) | |
3812 | || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl)))) | |
3813 | == void_list_node))) | |
3814 | { | |
9031d10b | 3815 | /* [basic.stc.dynamic.allocation] |
e8dd671e | 3816 | |
653e5405 | 3817 | An allocation function can be a function |
e8dd671e | 3818 | template. ... Template allocation functions shall |
3819 | have two or more parameters. */ | |
39e7b4a4 | 3820 | error ("invalid template declaration of %qD", decl); |
1cdc4670 | 3821 | return error_mark_node; |
e8dd671e | 3822 | } |
b7d1e8ea | 3823 | } |
f3d3cc67 | 3824 | else if (DECL_IMPLICIT_TYPEDEF_P (decl) |
3825 | && CLASS_TYPE_P (TREE_TYPE (decl))) | |
f1866bd9 | 3826 | /* OK */; |
3827 | else | |
f3959165 | 3828 | { |
39e7b4a4 | 3829 | error ("template declaration of %q#D", decl); |
f3959165 | 3830 | return error_mark_node; |
3831 | } | |
1f3fed0b | 3832 | } |
3833 | ||
df5066e2 | 3834 | /* Check to see that the rules regarding the use of default |
3835 | arguments are not being violated. */ | |
9031d10b | 3836 | check_default_tmpl_args (decl, current_template_parms, |
82d31768 | 3837 | primary, is_partial, /*is_friend_decl=*/0); |
d3ce0f51 | 3838 | |
d95d815d | 3839 | /* Ensure that there are no parameter packs in the type of this |
3840 | declaration that have not been expanded. */ | |
a4f66688 | 3841 | if (TREE_CODE (decl) == FUNCTION_DECL) |
3842 | { | |
3843 | /* Check each of the arguments individually to see if there are | |
3844 | any bare parameter packs. */ | |
3845 | tree type = TREE_TYPE (decl); | |
3846 | tree arg = DECL_ARGUMENTS (decl); | |
3847 | tree argtype = TYPE_ARG_TYPES (type); | |
3848 | ||
3849 | while (arg && argtype) | |
3850 | { | |
3851 | if (!FUNCTION_PARAMETER_PACK_P (arg) | |
830a6615 | 3852 | && check_for_bare_parameter_packs (TREE_TYPE (arg))) |
a4f66688 | 3853 | { |
3854 | /* This is a PARM_DECL that contains unexpanded parameter | |
3855 | packs. We have already complained about this in the | |
3856 | check_for_bare_parameter_packs call, so just replace | |
3857 | these types with ERROR_MARK_NODE. */ | |
3858 | TREE_TYPE (arg) = error_mark_node; | |
3859 | TREE_VALUE (argtype) = error_mark_node; | |
3860 | } | |
3861 | ||
3862 | arg = TREE_CHAIN (arg); | |
3863 | argtype = TREE_CHAIN (argtype); | |
3864 | } | |
3865 | ||
3866 | /* Check for bare parameter packs in the return type and the | |
3867 | exception specifiers. */ | |
830a6615 | 3868 | if (check_for_bare_parameter_packs (TREE_TYPE (type))) |
b369581a | 3869 | /* Errors were already issued, set return type to int |
3870 | as the frontend doesn't expect error_mark_node as | |
3871 | the return type. */ | |
3872 | TREE_TYPE (type) = integer_type_node; | |
830a6615 | 3873 | if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type))) |
3874 | TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE; | |
a4f66688 | 3875 | } |
830a6615 | 3876 | else if (check_for_bare_parameter_packs (TREE_TYPE (decl))) |
decf9b61 | 3877 | { |
3878 | TREE_TYPE (decl) = error_mark_node; | |
3879 | return error_mark_node; | |
3880 | } | |
d95d815d | 3881 | |
df5066e2 | 3882 | if (is_partial) |
3883 | return process_partial_specialization (decl); | |
b6577671 | 3884 | |
38281c46 | 3885 | args = current_template_args (); |
3886 | ||
9031d10b | 3887 | if (!ctx |
f3110581 | 3888 | || TREE_CODE (ctx) == FUNCTION_DECL |
0a3b29ad | 3889 | || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx)) |
f3110581 | 3890 | || (is_friend && !DECL_TEMPLATE_INFO (decl))) |
471086d6 | 3891 | { |
668ae905 | 3892 | if (DECL_LANG_SPECIFIC (decl) |
f3110581 | 3893 | && DECL_TEMPLATE_INFO (decl) |
3894 | && DECL_TI_TEMPLATE (decl)) | |
3895 | tmpl = DECL_TI_TEMPLATE (decl); | |
44696e78 | 3896 | /* If DECL is a TYPE_DECL for a class-template, then there won't |
3897 | be DECL_LANG_SPECIFIC. The information equivalent to | |
3898 | DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */ | |
9031d10b | 3899 | else if (DECL_IMPLICIT_TYPEDEF_P (decl) |
44696e78 | 3900 | && TYPE_TEMPLATE_INFO (TREE_TYPE (decl)) |
3901 | && TYPE_TI_TEMPLATE (TREE_TYPE (decl))) | |
3902 | { | |
3903 | /* Since a template declaration already existed for this | |
3904 | class-type, we must be redeclaring it here. Make sure | |
6c0cc2cd | 3905 | that the redeclaration is valid. */ |
44696e78 | 3906 | redeclare_class_template (TREE_TYPE (decl), |
3907 | current_template_parms); | |
3908 | /* We don't need to create a new TEMPLATE_DECL; just use the | |
3909 | one we already had. */ | |
3910 | tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl)); | |
3911 | } | |
f3110581 | 3912 | else |
f5308315 | 3913 | { |
38d89ee9 | 3914 | tmpl = build_template_decl (decl, current_template_parms, |
3915 | member_template_p); | |
900f01f2 | 3916 | new_template_p = 1; |
3917 | ||
f3110581 | 3918 | if (DECL_LANG_SPECIFIC (decl) |
3919 | && DECL_TEMPLATE_SPECIALIZATION (decl)) | |
3920 | { | |
3921 | /* A specialization of a member template of a template | |
47cd6605 | 3922 | class. */ |
f3110581 | 3923 | SET_DECL_TEMPLATE_SPECIALIZATION (tmpl); |
3924 | DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl); | |
3925 | DECL_TEMPLATE_INFO (decl) = NULL_TREE; | |
3926 | } | |
f5308315 | 3927 | } |
471086d6 | 3928 | } |
3929 | else | |
3930 | { | |
d07f132c | 3931 | tree a, t, current, parms; |
47d76caf | 3932 | int i; |
cd4b38bb | 3933 | tree tinfo = get_template_info (decl); |
6686e84d | 3934 | |
cd4b38bb | 3935 | if (!tinfo) |
1e28ccf1 | 3936 | { |
39e7b4a4 | 3937 | error ("template definition of non-template %q#D", decl); |
c05c4be3 | 3938 | return error_mark_node; |
1e28ccf1 | 3939 | } |
cd4b38bb | 3940 | |
3941 | tmpl = TI_TEMPLATE (tinfo); | |
9031d10b | 3942 | |
7f233616 | 3943 | if (DECL_FUNCTION_TEMPLATE_P (tmpl) |
9031d10b | 3944 | && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl) |
7f233616 | 3945 | && DECL_TEMPLATE_SPECIALIZATION (decl) |
38d89ee9 | 3946 | && DECL_MEMBER_TEMPLATE_P (tmpl)) |
64b4f183 | 3947 | { |
d07f132c | 3948 | tree new_tmpl; |
3949 | ||
3950 | /* The declaration is a specialization of a member | |
3951 | template, declared outside the class. Therefore, the | |
3952 | innermost template arguments will be NULL, so we | |
3953 | replace them with the arguments determined by the | |
3954 | earlier call to check_explicit_specialization. */ | |
3955 | args = DECL_TI_ARGS (decl); | |
3956 | ||
9031d10b | 3957 | new_tmpl |
38d89ee9 | 3958 | = build_template_decl (decl, current_template_parms, |
3959 | member_template_p); | |
d07f132c | 3960 | DECL_TEMPLATE_RESULT (new_tmpl) = decl; |
3961 | TREE_TYPE (new_tmpl) = TREE_TYPE (decl); | |
3962 | DECL_TI_TEMPLATE (decl) = new_tmpl; | |
3963 | SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl); | |
9031d10b | 3964 | DECL_TEMPLATE_INFO (new_tmpl) |
b0652a4f | 3965 | = tree_cons (tmpl, args, NULL_TREE); |
d07f132c | 3966 | |
9031d10b | 3967 | register_specialization (new_tmpl, |
3968 | most_general_template (tmpl), | |
c1d4295f | 3969 | args, |
3970 | is_friend); | |
d07f132c | 3971 | return decl; |
64b4f183 | 3972 | } |
64b4f183 | 3973 | |
d07f132c | 3974 | /* Make sure the template headers we got make sense. */ |
6686e84d | 3975 | |
d07f132c | 3976 | parms = DECL_TEMPLATE_PARMS (tmpl); |
3977 | i = TMPL_PARMS_DEPTH (parms); | |
3978 | if (TMPL_ARGS_DEPTH (args) != i) | |
64b4f183 | 3979 | { |
39e7b4a4 | 3980 | error ("expected %d levels of template parms for %q#D, got %d", |
653e5405 | 3981 | i, decl, TMPL_ARGS_DEPTH (args)); |
64b4f183 | 3982 | } |
d07f132c | 3983 | else |
3984 | for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms)) | |
3985 | { | |
3986 | a = TMPL_ARGS_LEVEL (args, i); | |
3987 | t = INNERMOST_TEMPLATE_PARMS (parms); | |
3988 | ||
3989 | if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a)) | |
3990 | { | |
3991 | if (current == decl) | |
39e7b4a4 | 3992 | error ("got %d template parameters for %q#D", |
653e5405 | 3993 | TREE_VEC_LENGTH (a), decl); |
d07f132c | 3994 | else |
39e7b4a4 | 3995 | error ("got %d template parameters for %q#T", |
653e5405 | 3996 | TREE_VEC_LENGTH (a), current); |
cf103c6c | 3997 | error (" but %d required", TREE_VEC_LENGTH (t)); |
0bda1427 | 3998 | return error_mark_node; |
d07f132c | 3999 | } |
64b4f183 | 4000 | |
d07f132c | 4001 | if (current == decl) |
4002 | current = ctx; | |
4003 | else | |
0bea21db | 4004 | current = (TYPE_P (current) |
4005 | ? TYPE_CONTEXT (current) | |
4006 | : DECL_CONTEXT (current)); | |
d07f132c | 4007 | } |
cd4b38bb | 4008 | |
4009 | /* Check that the parms are used in the appropriate qualifying scopes | |
4010 | in the declarator. */ | |
4011 | if (!comp_template_args | |
4012 | (TI_ARGS (tinfo), | |
4013 | TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl))))) | |
4014 | { | |
4015 | error ("\ | |
4016 | template arguments to %qD do not match original template %qD", | |
4017 | decl, DECL_TEMPLATE_RESULT (tmpl)); | |
4018 | if (!uses_template_parms (TI_ARGS (tinfo))) | |
4019 | inform ("use template<> for an explicit specialization"); | |
4020 | /* Avoid crash in import_export_decl. */ | |
4021 | DECL_INTERFACE_KNOWN (decl) = 1; | |
4022 | return error_mark_node; | |
4023 | } | |
e857e9c7 | 4024 | } |
471086d6 | 4025 | |
e857e9c7 | 4026 | DECL_TEMPLATE_RESULT (tmpl) = decl; |
4027 | TREE_TYPE (tmpl) = TREE_TYPE (decl); | |
471086d6 | 4028 | |
34197853 | 4029 | /* Push template declarations for global functions and types. Note |
4030 | that we do not try to push a global template friend declared in a | |
4031 | template class; such a thing may well depend on the template | |
63e8563c | 4032 | parameters of the class. */ |
9031d10b | 4033 | if (new_template_p && !ctx |
34197853 | 4034 | && !(is_friend && template_class_depth (current_class_type) > 0)) |
8aedf238 | 4035 | { |
c1d4295f | 4036 | tmpl = pushdecl_namespace_level (tmpl, is_friend); |
8aedf238 | 4037 | if (tmpl == error_mark_node) |
4038 | return error_mark_node; | |
3f3fa556 | 4039 | |
4040 | /* Hide template friend classes that haven't been declared yet. */ | |
4041 | if (is_friend && TREE_CODE (decl) == TYPE_DECL) | |
4042 | { | |
4043 | DECL_ANTICIPATED (tmpl) = 1; | |
4044 | DECL_FRIEND_P (tmpl) = 1; | |
4045 | } | |
8aedf238 | 4046 | } |
471086d6 | 4047 | |
e857e9c7 | 4048 | if (primary) |
c0af458b | 4049 | { |
864f2267 | 4050 | tree parms = DECL_TEMPLATE_PARMS (tmpl); |
4051 | int i; | |
4052 | ||
c0af458b | 4053 | DECL_PRIMARY_TEMPLATE (tmpl) = tmpl; |
4054 | if (DECL_CONV_FN_P (tmpl)) | |
4055 | { | |
864f2267 | 4056 | int depth = TMPL_PARMS_DEPTH (parms); |
bfb2681d | 4057 | |
c0af458b | 4058 | /* It is a conversion operator. See if the type converted to |
4059 | depends on innermost template operands. */ | |
9031d10b | 4060 | |
7bdfc61c | 4061 | if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl)), |
4062 | depth)) | |
c0af458b | 4063 | DECL_TEMPLATE_CONV_FN_P (tmpl) = 1; |
4064 | } | |
864f2267 | 4065 | |
4066 | /* Give template template parms a DECL_CONTEXT of the template | |
4067 | for which they are a parameter. */ | |
4068 | parms = INNERMOST_TEMPLATE_PARMS (parms); | |
4069 | for (i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i) | |
4070 | { | |
4071 | tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i)); | |
4072 | if (TREE_CODE (parm) == TEMPLATE_DECL) | |
4073 | DECL_CONTEXT (parm) = tmpl; | |
4074 | } | |
c0af458b | 4075 | } |
e857e9c7 | 4076 | |
f5f73833 | 4077 | /* The DECL_TI_ARGS of DECL contains full set of arguments referring |
a2a5f9d8 | 4078 | back to its most general template. If TMPL is a specialization, |
4079 | ARGS may only have the innermost set of arguments. Add the missing | |
4080 | argument levels if necessary. */ | |
4081 | if (DECL_TEMPLATE_INFO (tmpl)) | |
4082 | args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args); | |
4083 | ||
b0652a4f | 4084 | info = tree_cons (tmpl, args, NULL_TREE); |
e857e9c7 | 4085 | |
70a658bd | 4086 | if (DECL_IMPLICIT_TYPEDEF_P (decl)) |
facb12b2 | 4087 | SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info); |
f1866bd9 | 4088 | else if (DECL_LANG_SPECIFIC (decl)) |
e857e9c7 | 4089 | DECL_TEMPLATE_INFO (decl) = info; |
1ca935fd | 4090 | |
4091 | return DECL_TEMPLATE_RESULT (tmpl); | |
471086d6 | 4092 | } |
4093 | ||
0f0d5a69 | 4094 | tree |
807be5b4 | 4095 | push_template_decl (tree decl) |
0f0d5a69 | 4096 | { |
c1d4295f | 4097 | return push_template_decl_real (decl, false); |
0f0d5a69 | 4098 | } |
4099 | ||
4100 | /* Called when a class template TYPE is redeclared with the indicated | |
4101 | template PARMS, e.g.: | |
55d6e079 | 4102 | |
4103 | template <class T> struct S; | |
4104 | template <class T> struct S {}; */ | |
4105 | ||
2ed0bcc0 | 4106 | bool |
807be5b4 | 4107 | redeclare_class_template (tree type, tree parms) |
55d6e079 | 4108 | { |
6e4e8926 | 4109 | tree tmpl; |
0f0d5a69 | 4110 | tree tmpl_parms; |
55d6e079 | 4111 | int i; |
4112 | ||
6e4e8926 | 4113 | if (!TYPE_TEMPLATE_INFO (type)) |
4114 | { | |
39e7b4a4 | 4115 | error ("%qT is not a template type", type); |
2ed0bcc0 | 4116 | return false; |
6e4e8926 | 4117 | } |
4118 | ||
4119 | tmpl = TYPE_TI_TEMPLATE (type); | |
55d6e079 | 4120 | if (!PRIMARY_TEMPLATE_P (tmpl)) |
4121 | /* The type is nested in some template class. Nothing to worry | |
4122 | about here; there are no new template parameters for the nested | |
4123 | type. */ | |
2ed0bcc0 | 4124 | return true; |
55d6e079 | 4125 | |
1ab5734f | 4126 | if (!parms) |
4127 | { | |
4128 | error ("template specifiers not specified in declaration of %qD", | |
4129 | tmpl); | |
2ed0bcc0 | 4130 | return false; |
1ab5734f | 4131 | } |
4132 | ||
0f0d5a69 | 4133 | parms = INNERMOST_TEMPLATE_PARMS (parms); |
4134 | tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl); | |
4135 | ||
55d6e079 | 4136 | if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms)) |
4137 | { | |
40db0c7c | 4138 | error ("redeclared with %d template parameter(s)", |
4139 | TREE_VEC_LENGTH (parms)); | |
4140 | inform ("previous declaration %q+D used %d template parameter(s)", | |
4141 | tmpl, TREE_VEC_LENGTH (tmpl_parms)); | |
2ed0bcc0 | 4142 | return false; |
55d6e079 | 4143 | } |
4144 | ||
4145 | for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i) | |
4146 | { | |
6f32df61 | 4147 | tree tmpl_parm; |
4148 | tree parm; | |
4149 | tree tmpl_default; | |
4150 | tree parm_default; | |
4151 | ||
4152 | if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node | |
4153 | || TREE_VEC_ELT (parms, i) == error_mark_node) | |
4154 | continue; | |
4155 | ||
4156 | tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i)); | |
4157 | parm = TREE_VALUE (TREE_VEC_ELT (parms, i)); | |
4158 | tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)); | |
4159 | parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i)); | |
55d6e079 | 4160 | |
fcbecc82 | 4161 | /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or |
4162 | TEMPLATE_DECL. */ | |
21bd02a9 | 4163 | if (tmpl_parm != error_mark_node |
7485db95 | 4164 | && (TREE_CODE (tmpl_parm) != TREE_CODE (parm) |
4165 | || (TREE_CODE (tmpl_parm) != TYPE_DECL | |
4166 | && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm))) | |
4167 | || (TREE_CODE (tmpl_parm) != PARM_DECL | |
4168 | && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm)) | |
4169 | != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))) | |
4170 | || (TREE_CODE (tmpl_parm) == PARM_DECL | |
4171 | && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm)) | |
4172 | != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))) | |
55d6e079 | 4173 | { |
3cf8b391 | 4174 | error ("template parameter %q+#D", tmpl_parm); |
39e7b4a4 | 4175 | error ("redeclared here as %q#D", parm); |
2ed0bcc0 | 4176 | return false; |
55d6e079 | 4177 | } |
4178 | ||
4179 | if (tmpl_default != NULL_TREE && parm_default != NULL_TREE) | |
4180 | { | |
4181 | /* We have in [temp.param]: | |
4182 | ||
4183 | A template-parameter may not be given default arguments | |
4184 | by two different declarations in the same scope. */ | |
39e7b4a4 | 4185 | error ("redefinition of default argument for %q#D", parm); |
40db0c7c | 4186 | inform ("%Joriginal definition appeared here", tmpl_parm); |
2ed0bcc0 | 4187 | return false; |
55d6e079 | 4188 | } |
4189 | ||
4190 | if (parm_default != NULL_TREE) | |
4191 | /* Update the previous template parameters (which are the ones | |
4192 | that will really count) with the new default value. */ | |
4193 | TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default; | |
b7452de5 | 4194 | else if (tmpl_default != NULL_TREE) |
4195 | /* Update the new parameters, too; they'll be used as the | |
4196 | parameters for any members. */ | |
4197 | TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default; | |
55d6e079 | 4198 | } |
2ed0bcc0 | 4199 | |
4200 | return true; | |
55d6e079 | 4201 | } |
668ae905 | 4202 | |
2250d32c | 4203 | /* Simplify EXPR if it is a non-dependent expression. Returns the |
4204 | (possibly simplified) expression. */ | |
4205 | ||
4206 | tree | |
4207 | fold_non_dependent_expr (tree expr) | |
4208 | { | |
950d58ce | 4209 | if (expr == NULL_TREE) |
4210 | return NULL_TREE; | |
4211 | ||
2250d32c | 4212 | /* If we're in a template, but EXPR isn't value dependent, simplify |
4213 | it. We're supposed to treat: | |
9031d10b | 4214 | |
2250d32c | 4215 | template <typename T> void f(T[1 + 1]); |
4216 | template <typename T> void f(T[2]); | |
9031d10b | 4217 | |
2250d32c | 4218 | as two declarations of the same function, for example. */ |
4219 | if (processing_template_decl | |
4220 | && !type_dependent_expression_p (expr) | |
165b0cb9 | 4221 | && !value_dependent_expression_p (expr)) |
2250d32c | 4222 | { |
4223 | HOST_WIDE_INT saved_processing_template_decl; | |
4224 | ||
4225 | saved_processing_template_decl = processing_template_decl; | |
4226 | processing_template_decl = 0; | |
4227 | expr = tsubst_copy_and_build (expr, | |
4228 | /*args=*/NULL_TREE, | |
4229 | tf_error, | |
4230 | /*in_decl=*/NULL_TREE, | |
bde9ebf7 | 4231 | /*function_p=*/false, |
4232 | /*integral_constant_expression_p=*/true); | |
2250d32c | 4233 | processing_template_decl = saved_processing_template_decl; |
4234 | } | |
4235 | return expr; | |
4236 | } | |
4237 | ||
b312a686 | 4238 | /* EXPR is an expression which is used in a constant-expression context. |
4239 | For instance, it could be a VAR_DECL with a constant initializer. | |
4240 | Extract the innest constant expression. | |
9031d10b | 4241 | |
13f0eb20 | 4242 | This is basically a more powerful version of |
4243 | integral_constant_value, which can be used also in templates where | |
4244 | initializers can maintain a syntactic rather than semantic form | |
4245 | (even if they are non-dependent, for access-checking purposes). */ | |
b312a686 | 4246 | |
6cf0bc8c | 4247 | static tree |
b312a686 | 4248 | fold_decl_constant_value (tree expr) |
4249 | { | |
2855a567 | 4250 | tree const_expr = expr; |
4251 | do | |
b312a686 | 4252 | { |
b312a686 | 4253 | expr = fold_non_dependent_expr (const_expr); |
2855a567 | 4254 | const_expr = integral_constant_value (expr); |
b312a686 | 4255 | } |
2855a567 | 4256 | while (expr != const_expr); |
b312a686 | 4257 | |
13f0eb20 | 4258 | return expr; |
b312a686 | 4259 | } |
4260 | ||
4261 | /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which | |
4262 | must be a function or a pointer-to-function type, as specified | |
4263 | in [temp.arg.nontype]: disambiguate EXPR if it is an overload set, | |
4264 | and check that the resulting function has external linkage. */ | |
4265 | ||
4266 | static tree | |
4267 | convert_nontype_argument_function (tree type, tree expr) | |
4268 | { | |
4269 | tree fns = expr; | |
4270 | tree fn, fn_no_ptr; | |
4271 | ||
4272 | fn = instantiate_type (type, fns, tf_none); | |
4273 | if (fn == error_mark_node) | |
4274 | return error_mark_node; | |
4275 | ||
4276 | fn_no_ptr = fn; | |
4277 | if (TREE_CODE (fn_no_ptr) == ADDR_EXPR) | |
4278 | fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0); | |
0e5cde0c | 4279 | if (TREE_CODE (fn_no_ptr) == BASELINK) |
4280 | fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr); | |
4281 | ||
b312a686 | 4282 | /* [temp.arg.nontype]/1 |
4283 | ||
4284 | A template-argument for a non-type, non-template template-parameter | |
4285 | shall be one of: | |
4286 | [...] | |
4287 | -- the address of an object or function with external linkage. */ | |
4288 | if (!DECL_EXTERNAL_LINKAGE_P (fn_no_ptr)) | |
4289 | { | |
4290 | error ("%qE is not a valid template argument for type %qT " | |
4291 | "because function %qD has not external linkage", | |
4292 | expr, type, fn_no_ptr); | |
4293 | return NULL_TREE; | |
4294 | } | |
4295 | ||
4296 | return fn; | |
4297 | } | |
4298 | ||
668ae905 | 4299 | /* Attempt to convert the non-type template parameter EXPR to the |
4300 | indicated TYPE. If the conversion is successful, return the | |
ec0eee8d | 4301 | converted value. If the conversion is unsuccessful, return |
668ae905 | 4302 | NULL_TREE if we issued an error message, or error_mark_node if we |
4303 | did not. We issue error messages for out-and-out bad template | |
4304 | parameters, but not simply because the conversion failed, since we | |
2250d32c | 4305 | might be just trying to do argument deduction. Both TYPE and EXPR |
b312a686 | 4306 | must be non-dependent. |
4307 | ||
4308 | The conversion follows the special rules described in | |
4309 | [temp.arg.nontype], and it is much more strict than an implicit | |
4310 | conversion. | |
4311 | ||
4312 | This function is called twice for each template argument (see | |
4313 | lookup_template_class for a more accurate description of this | |
4314 | problem). This means that we need to handle expressions which | |
4315 | are not valid in a C++ source, but can be created from the | |
4316 | first call (for instance, casts to perform conversions). These | |
4317 | hacks can go away after we fix the double coercion problem. */ | |
668ae905 | 4318 | |
4319 | static tree | |
807be5b4 | 4320 | convert_nontype_argument (tree type, tree expr) |
668ae905 | 4321 | { |
2250d32c | 4322 | tree expr_type; |
4323 | ||
b312a686 | 4324 | /* Detect immediately string literals as invalid non-type argument. |
4325 | This special-case is not needed for correctness (we would easily | |
4326 | catch this later), but only to provide better diagnostic for this | |
4327 | common user mistake. As suggested by DR 100, we do not mention | |
4328 | linkage issues in the diagnostic as this is not the point. */ | |
4329 | if (TREE_CODE (expr) == STRING_CST) | |
4330 | { | |
4331 | error ("%qE is not a valid template argument for type %qT " | |
4332 | "because string literals can never be used in this context", | |
4333 | expr, type); | |
4334 | return NULL_TREE; | |
4335 | } | |
4336 | ||
2250d32c | 4337 | /* If we are in a template, EXPR may be non-dependent, but still |
4338 | have a syntactic, rather than semantic, form. For example, EXPR | |
4339 | might be a SCOPE_REF, rather than the VAR_DECL to which the | |
4340 | SCOPE_REF refers. Preserving the qualifying scope is necessary | |
4341 | so that access checking can be performed when the template is | |
4342 | instantiated -- but here we need the resolved form so that we can | |
4343 | convert the argument. */ | |
4344 | expr = fold_non_dependent_expr (expr); | |
7b8ea6a3 | 4345 | if (error_operand_p (expr)) |
4346 | return error_mark_node; | |
2250d32c | 4347 | expr_type = TREE_TYPE (expr); |
668ae905 | 4348 | |
b312a686 | 4349 | /* HACK: Due to double coercion, we can get a |
4350 | NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here, | |
4351 | which is the tree that we built on the first call (see | |
4352 | below when coercing to reference to object or to reference to | |
4353 | function). We just strip everything and get to the arg. | |
4354 | See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C | |
4355 | for examples. */ | |
4356 | if (TREE_CODE (expr) == NOP_EXPR) | |
668ae905 | 4357 | { |
b312a686 | 4358 | if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type)) |
668ae905 | 4359 | { |
b312a686 | 4360 | /* ??? Maybe we could use convert_from_reference here, but we |
4361 | would need to relax its constraints because the NOP_EXPR | |
4362 | could actually change the type to something more cv-qualified, | |
4363 | and this is not folded by convert_from_reference. */ | |
4364 | tree addr = TREE_OPERAND (expr, 0); | |
4365 | gcc_assert (TREE_CODE (expr_type) == REFERENCE_TYPE); | |
4366 | gcc_assert (TREE_CODE (addr) == ADDR_EXPR); | |
4367 | gcc_assert (TREE_CODE (TREE_TYPE (addr)) == POINTER_TYPE); | |
4368 | gcc_assert (same_type_ignoring_top_level_qualifiers_p | |
4369 | (TREE_TYPE (expr_type), | |
4370 | TREE_TYPE (TREE_TYPE (addr)))); | |
4371 | ||
4372 | expr = TREE_OPERAND (addr, 0); | |
4373 | expr_type = TREE_TYPE (expr); | |
668ae905 | 4374 | } |
4375 | ||
b312a686 | 4376 | /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the |
4377 | parameter is a pointer to object, through decay and | |
4378 | qualification conversion. Let's strip everything. */ | |
4379 | else if (TYPE_PTROBV_P (type)) | |
668ae905 | 4380 | { |
b312a686 | 4381 | STRIP_NOPS (expr); |
4382 | gcc_assert (TREE_CODE (expr) == ADDR_EXPR); | |
4383 | gcc_assert (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE); | |
4384 | /* Skip the ADDR_EXPR only if it is part of the decay for | |
4385 | an array. Otherwise, it is part of the original argument | |
4386 | in the source code. */ | |
4387 | if (TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == ARRAY_TYPE) | |
4388 | expr = TREE_OPERAND (expr, 0); | |
4389 | expr_type = TREE_TYPE (expr); | |
c24cced6 | 4390 | } |
668ae905 | 4391 | } |
7e56f6c0 | 4392 | |
b312a686 | 4393 | /* [temp.arg.nontype]/5, bullet 1 |
668ae905 | 4394 | |
b312a686 | 4395 | For a non-type template-parameter of integral or enumeration type, |
4396 | integral promotions (_conv.prom_) and integral conversions | |
4397 | (_conv.integral_) are applied. */ | |
4398 | if (INTEGRAL_TYPE_P (type)) | |
668ae905 | 4399 | { |
668ae905 | 4400 | if (!INTEGRAL_TYPE_P (expr_type)) |
4401 | return error_mark_node; | |
f551ed05 | 4402 | |
b312a686 | 4403 | expr = fold_decl_constant_value (expr); |
4404 | /* Notice that there are constant expressions like '4 % 0' which | |
4405 | do not fold into integer constants. */ | |
eb402de4 | 4406 | if (TREE_CODE (expr) != INTEGER_CST) |
b312a686 | 4407 | { |
4408 | error ("%qE is not a valid template argument for type %qT " | |
4409 | "because it is a non-constant expression", expr, type); | |
4410 | return NULL_TREE; | |
4411 | } | |
668ae905 | 4412 | |
b312a686 | 4413 | /* At this point, an implicit conversion does what we want, |
4414 | because we already know that the expression is of integral | |
4415 | type. */ | |
4416 | expr = ocp_convert (type, expr, CONV_IMPLICIT, LOOKUP_PROTECT); | |
4417 | if (expr == error_mark_node) | |
4418 | return error_mark_node; | |
668ae905 | 4419 | |
b312a686 | 4420 | /* Conversion was allowed: fold it to a bare integer constant. */ |
4421 | expr = fold (expr); | |
4422 | } | |
4423 | /* [temp.arg.nontype]/5, bullet 2 | |
668ae905 | 4424 | |
b312a686 | 4425 | For a non-type template-parameter of type pointer to object, |
4426 | qualification conversions (_conv.qual_) and the array-to-pointer | |
4427 | conversion (_conv.array_) are applied. */ | |
4428 | else if (TYPE_PTROBV_P (type)) | |
4429 | { | |
4430 | /* [temp.arg.nontype]/1 (TC1 version, DR 49): | |
668ae905 | 4431 | |
b312a686 | 4432 | A template-argument for a non-type, non-template template-parameter |
4433 | shall be one of: [...] | |
668ae905 | 4434 | |
b312a686 | 4435 | -- the name of a non-type template-parameter; |
4436 | -- the address of an object or function with external linkage, [...] | |
4437 | expressed as "& id-expression" where the & is optional if the name | |
4438 | refers to a function or array, or if the corresponding | |
4439 | template-parameter is a reference. | |
9031d10b | 4440 | |
b312a686 | 4441 | Here, we do not care about functions, as they are invalid anyway |
4442 | for a parameter of type pointer-to-object. */ | |
33c1a964 | 4443 | |
4444 | if (DECL_P (expr) && DECL_TEMPLATE_PARM_P (expr)) | |
4445 | /* Non-type template parameters are OK. */ | |
4446 | ; | |
4447 | else if (TREE_CODE (expr) != ADDR_EXPR | |
4448 | && TREE_CODE (expr_type) != ARRAY_TYPE) | |
4449 | { | |
4450 | if (TREE_CODE (expr) == VAR_DECL) | |
4451 | { | |
4452 | error ("%qD is not a valid template argument " | |
4453 | "because %qD is a variable, not the address of " | |
4454 | "a variable", | |
4455 | expr, expr); | |
4456 | return NULL_TREE; | |
4457 | } | |
4458 | /* Other values, like integer constants, might be valid | |
4459 | non-type arguments of some other type. */ | |
4460 | return error_mark_node; | |
4461 | } | |
4462 | else | |
4463 | { | |
4464 | tree decl; | |
4465 | ||
4466 | decl = ((TREE_CODE (expr) == ADDR_EXPR) | |
4467 | ? TREE_OPERAND (expr, 0) : expr); | |
4468 | if (TREE_CODE (decl) != VAR_DECL) | |
4469 | { | |
4470 | error ("%qE is not a valid template argument of type %qT " | |
4471 | "because %qE is not a variable", | |
4472 | expr, type, decl); | |
4473 | return NULL_TREE; | |
4474 | } | |
4475 | else if (!DECL_EXTERNAL_LINKAGE_P (decl)) | |
4476 | { | |
4477 | error ("%qE is not a valid template argument of type %qT " | |
4478 | "because %qD does not have external linkage", | |
4479 | expr, type, decl); | |
4480 | return NULL_TREE; | |
4481 | } | |
4482 | } | |
b312a686 | 4483 | |
4484 | expr = decay_conversion (expr); | |
4485 | if (expr == error_mark_node) | |
4486 | return error_mark_node; | |
668ae905 | 4487 | |
b312a686 | 4488 | expr = perform_qualification_conversions (type, expr); |
4489 | if (expr == error_mark_node) | |
4490 | return error_mark_node; | |
b312a686 | 4491 | } |
4492 | /* [temp.arg.nontype]/5, bullet 3 | |
4493 | ||
4494 | For a non-type template-parameter of type reference to object, no | |
4495 | conversions apply. The type referred to by the reference may be more | |
4496 | cv-qualified than the (otherwise identical) type of the | |
4497 | template-argument. The template-parameter is bound directly to the | |
4498 | template-argument, which must be an lvalue. */ | |
4499 | else if (TYPE_REF_OBJ_P (type)) | |
4500 | { | |
4501 | if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type), | |
4502 | expr_type)) | |
4503 | return error_mark_node; | |
668ae905 | 4504 | |
b312a686 | 4505 | if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type)) |
4506 | { | |
4507 | error ("%qE is not a valid template argument for type %qT " | |
4508 | "because of conflicts in cv-qualification", expr, type); | |
4509 | return NULL_TREE; | |
4510 | } | |
9031d10b | 4511 | |
b312a686 | 4512 | if (!real_lvalue_p (expr)) |
4513 | { | |
4514 | error ("%qE is not a valid template argument for type %qT " | |
b2d321a6 | 4515 | "because it is not an lvalue", expr, type); |
b312a686 | 4516 | return NULL_TREE; |
4517 | } | |
cc4d0855 | 4518 | |
b312a686 | 4519 | /* [temp.arg.nontype]/1 |
668ae905 | 4520 | |
b312a686 | 4521 | A template-argument for a non-type, non-template template-parameter |
4522 | shall be one of: [...] | |
668ae905 | 4523 | |
93523877 | 4524 | -- the address of an object or function with external linkage. */ |
b312a686 | 4525 | if (!DECL_EXTERNAL_LINKAGE_P (expr)) |
4526 | { | |
4527 | error ("%qE is not a valid template argument for type %qT " | |
4528 | "because object %qD has not external linkage", | |
4529 | expr, type, expr); | |
4530 | return NULL_TREE; | |
4531 | } | |
148e86d7 | 4532 | |
b312a686 | 4533 | expr = build_nop (type, build_address (expr)); |
4534 | } | |
4535 | /* [temp.arg.nontype]/5, bullet 4 | |
4536 | ||
4537 | For a non-type template-parameter of type pointer to function, only | |
4538 | the function-to-pointer conversion (_conv.func_) is applied. If the | |
4539 | template-argument represents a set of overloaded functions (or a | |
4540 | pointer to such), the matching function is selected from the set | |
4541 | (_over.over_). */ | |
4542 | else if (TYPE_PTRFN_P (type)) | |
4543 | { | |
4544 | /* If the argument is a template-id, we might not have enough | |
e085933c | 4545 | context information to decay the pointer. */ |
b312a686 | 4546 | if (!type_unknown_p (expr_type)) |
4547 | { | |
4548 | expr = decay_conversion (expr); | |
4549 | if (expr == error_mark_node) | |
4550 | return error_mark_node; | |
4551 | } | |
668ae905 | 4552 | |
b312a686 | 4553 | expr = convert_nontype_argument_function (type, expr); |
4554 | if (!expr || expr == error_mark_node) | |
4555 | return expr; | |
4556 | } | |
4557 | /* [temp.arg.nontype]/5, bullet 5 | |
668ae905 | 4558 | |
b312a686 | 4559 | For a non-type template-parameter of type reference to function, no |
4560 | conversions apply. If the template-argument represents a set of | |
4561 | overloaded functions, the matching function is selected from the set | |
4562 | (_over.over_). */ | |
4563 | else if (TYPE_REFFN_P (type)) | |
4564 | { | |
4565 | if (TREE_CODE (expr) == ADDR_EXPR) | |
4566 | { | |
4567 | error ("%qE is not a valid template argument for type %qT " | |
4568 | "because it is a pointer", expr, type); | |
4569 | inform ("try using %qE instead", TREE_OPERAND (expr, 0)); | |
4570 | return NULL_TREE; | |
4571 | } | |
668ae905 | 4572 | |
b312a686 | 4573 | expr = convert_nontype_argument_function (TREE_TYPE (type), expr); |
4574 | if (!expr || expr == error_mark_node) | |
4575 | return expr; | |
668ae905 | 4576 | |
b7837065 | 4577 | expr = build_nop (type, build_address (expr)); |
b312a686 | 4578 | } |
4579 | /* [temp.arg.nontype]/5, bullet 6 | |
668ae905 | 4580 | |
b312a686 | 4581 | For a non-type template-parameter of type pointer to member function, |
4582 | no conversions apply. If the template-argument represents a set of | |
4583 | overloaded member functions, the matching member function is selected | |
4584 | from the set (_over.over_). */ | |
4585 | else if (TYPE_PTRMEMFUNC_P (type)) | |
4586 | { | |
4587 | expr = instantiate_type (type, expr, tf_none); | |
4588 | if (expr == error_mark_node) | |
4589 | return error_mark_node; | |
668ae905 | 4590 | |
b312a686 | 4591 | /* There is no way to disable standard conversions in |
4592 | resolve_address_of_overloaded_function (called by | |
4593 | instantiate_type). It is possible that the call succeeded by | |
4594 | converting &B::I to &D::I (where B is a base of D), so we need | |
4595 | to reject this conversion here. | |
668ae905 | 4596 | |
b312a686 | 4597 | Actually, even if there was a way to disable standard conversions, |
4598 | it would still be better to reject them here so that we can | |
4599 | provide a superior diagnostic. */ | |
4600 | if (!same_type_p (TREE_TYPE (expr), type)) | |
4601 | { | |
4602 | /* Make sure we are just one standard conversion off. */ | |
4603 | gcc_assert (can_convert (type, TREE_TYPE (expr))); | |
4604 | error ("%qE is not a valid template argument for type %qT " | |
4605 | "because it is of type %qT", expr, type, | |
4606 | TREE_TYPE (expr)); | |
4607 | inform ("standard conversions are not allowed in this context"); | |
4608 | return NULL_TREE; | |
4609 | } | |
4610 | } | |
4611 | /* [temp.arg.nontype]/5, bullet 7 | |
2d1f8ee9 | 4612 | |
b312a686 | 4613 | For a non-type template-parameter of type pointer to data member, |
4614 | qualification conversions (_conv.qual_) are applied. */ | |
4615 | else if (TYPE_PTRMEM_P (type)) | |
4616 | { | |
4617 | expr = perform_qualification_conversions (type, expr); | |
4618 | if (expr == error_mark_node) | |
668ae905 | 4619 | return expr; |
668ae905 | 4620 | } |
b312a686 | 4621 | /* A template non-type parameter must be one of the above. */ |
4622 | else | |
4623 | gcc_unreachable (); | |
668ae905 | 4624 | |
b312a686 | 4625 | /* Sanity check: did we actually convert the argument to the |
4626 | right type? */ | |
4627 | gcc_assert (same_type_p (type, TREE_TYPE (expr))); | |
4628 | return expr; | |
668ae905 | 4629 | } |
4630 | ||
2e1a9c49 | 4631 | /* Subroutine of coerce_template_template_parms, which returns 1 if |
4632 | PARM_PARM and ARG_PARM match using the rule for the template | |
4633 | parameters of template template parameters. Both PARM and ARG are | |
4634 | template parameters; the rest of the arguments are the same as for | |
4635 | coerce_template_template_parms. | |
4636 | */ | |
4637 | static int | |
4638 | coerce_template_template_parm (tree parm, | |
4639 | tree arg, | |
4640 | tsubst_flags_t complain, | |
4641 | tree in_decl, | |
4642 | tree outer_args) | |
4643 | { | |
4644 | if (arg == NULL_TREE || arg == error_mark_node | |
4645 | || parm == NULL_TREE || parm == error_mark_node) | |
4646 | return 0; | |
4647 | ||
4648 | if (TREE_CODE (arg) != TREE_CODE (parm)) | |
4649 | return 0; | |
4650 | ||
4651 | switch (TREE_CODE (parm)) | |
4652 | { | |
4653 | case TEMPLATE_DECL: | |
4654 | /* We encounter instantiations of templates like | |
4655 | template <template <template <class> class> class TT> | |
4656 | class C; */ | |
4657 | { | |
4658 | tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm); | |
4659 | tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg); | |
4660 | ||
4661 | if (!coerce_template_template_parms | |
4662 | (parmparm, argparm, complain, in_decl, outer_args)) | |
4663 | return 0; | |
4664 | } | |
4665 | /* Fall through. */ | |
4666 | ||
4667 | case TYPE_DECL: | |
4668 | if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg)) | |
4669 | && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))) | |
4670 | /* Argument is a parameter pack but parameter is not. */ | |
4671 | return 0; | |
4672 | break; | |
4673 | ||
4674 | case PARM_DECL: | |
4675 | /* The tsubst call is used to handle cases such as | |
4676 | ||
4677 | template <int> class C {}; | |
4678 | template <class T, template <T> class TT> class D {}; | |
4679 | D<int, C> d; | |
4680 | ||
4681 | i.e. the parameter list of TT depends on earlier parameters. */ | |
4682 | if (!dependent_type_p (TREE_TYPE (arg)) | |
4683 | && !same_type_p | |
4684 | (tsubst (TREE_TYPE (parm), outer_args, complain, in_decl), | |
4685 | TREE_TYPE (arg))) | |
4686 | return 0; | |
4687 | ||
4688 | if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg)) | |
4689 | && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))) | |
4690 | /* Argument is a parameter pack but parameter is not. */ | |
4691 | return 0; | |
4692 | ||
4693 | break; | |
4694 | ||
4695 | default: | |
4696 | gcc_unreachable (); | |
4697 | } | |
4698 | ||
4699 | return 1; | |
4700 | } | |
4701 | ||
b312a686 | 4702 | |
9031d10b | 4703 | /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for |
4704 | template template parameters. Both PARM_PARMS and ARG_PARMS are | |
4705 | vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL | |
9e5bd773 | 4706 | or PARM_DECL. |
9031d10b | 4707 | |
9e5bd773 | 4708 | Consider the example: |
36653a30 | 4709 | template <class T> class A; |
4710 | template<template <class U> class TT> class B; | |
9e5bd773 | 4711 | |
36653a30 | 4712 | For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are |
4713 | the parameters to A, and OUTER_ARGS contains A. */ | |
9e5bd773 | 4714 | |
4715 | static int | |
9031d10b | 4716 | coerce_template_template_parms (tree parm_parms, |
653e5405 | 4717 | tree arg_parms, |
4718 | tsubst_flags_t complain, | |
807be5b4 | 4719 | tree in_decl, |
653e5405 | 4720 | tree outer_args) |
9e5bd773 | 4721 | { |
4722 | int nparms, nargs, i; | |
4723 | tree parm, arg; | |
2e1a9c49 | 4724 | int variadic_p = 0; |
9e5bd773 | 4725 | |
b4df430b | 4726 | gcc_assert (TREE_CODE (parm_parms) == TREE_VEC); |
4727 | gcc_assert (TREE_CODE (arg_parms) == TREE_VEC); | |
9e5bd773 | 4728 | |
4729 | nparms = TREE_VEC_LENGTH (parm_parms); | |
4730 | nargs = TREE_VEC_LENGTH (arg_parms); | |
4731 | ||
2e1a9c49 | 4732 | /* Determine whether we have a parameter pack at the end of the |
4733 | template template parameter's template parameter list. */ | |
4734 | if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node) | |
4735 | { | |
4736 | parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1)); | |
4737 | ||
4738 | switch (TREE_CODE (parm)) | |
4739 | { | |
4740 | case TEMPLATE_DECL: | |
4741 | case TYPE_DECL: | |
4742 | if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))) | |
4743 | variadic_p = 1; | |
4744 | break; | |
4745 | ||
4746 | case PARM_DECL: | |
4747 | if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))) | |
4748 | variadic_p = 1; | |
4749 | break; | |
4750 | ||
4751 | default: | |
4752 | gcc_unreachable (); | |
4753 | } | |
4754 | } | |
4755 | ||
4756 | if (nargs != nparms | |
4757 | && !(variadic_p && nargs >= nparms - 1)) | |
9e5bd773 | 4758 | return 0; |
4759 | ||
2e1a9c49 | 4760 | /* Check all of the template parameters except the parameter pack at |
4761 | the end (if any). */ | |
4762 | for (i = 0; i < nparms - variadic_p; ++i) | |
9e5bd773 | 4763 | { |
6f32df61 | 4764 | if (TREE_VEC_ELT (parm_parms, i) == error_mark_node |
4765 | || TREE_VEC_ELT (arg_parms, i) == error_mark_node) | |
508c7aa7 | 4766 | continue; |
4767 | ||
9e5bd773 | 4768 | parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i)); |
4769 | arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i)); | |
4770 | ||
2e1a9c49 | 4771 | if (!coerce_template_template_parm (parm, arg, complain, in_decl, |
4772 | outer_args)) | |
9e5bd773 | 4773 | return 0; |
4774 | ||
2e1a9c49 | 4775 | } |
eafc7979 | 4776 | |
2e1a9c49 | 4777 | if (variadic_p) |
4778 | { | |
4779 | /* Check each of the template parameters in the template | |
4780 | argument against the template parameter pack at the end of | |
4781 | the template template parameter. */ | |
4782 | if (TREE_VEC_ELT (parm_parms, i) == error_mark_node) | |
4783 | return 0; | |
d95d815d | 4784 | |
2e1a9c49 | 4785 | parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i)); |
9031d10b | 4786 | |
2e1a9c49 | 4787 | for (; i < nargs; ++i) |
4788 | { | |
4789 | if (TREE_VEC_ELT (arg_parms, i) == error_mark_node) | |
4790 | continue; | |
4791 | ||
4792 | arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i)); | |
4793 | ||
4794 | if (!coerce_template_template_parm (parm, arg, complain, in_decl, | |
4795 | outer_args)) | |
4796 | return 0; | |
4797 | } | |
9e5bd773 | 4798 | } |
2e1a9c49 | 4799 | |
9e5bd773 | 4800 | return 1; |
4801 | } | |
4802 | ||
e5dab245 | 4803 | /* Verifies that the deduced template arguments (in TARGS) for the |
4804 | template template parameters (in TPARMS) represent valid bindings, | |
4805 | by comparing the template parameter list of each template argument | |
4806 | to the template parameter list of its corresponding template | |
4807 | template parameter, in accordance with DR150. This | |
4808 | routine can only be called after all template arguments have been | |
4809 | deduced. It will return TRUE if all of the template template | |
4810 | parameter bindings are okay, FALSE otherwise. */ | |
4811 | bool | |
4812 | template_template_parm_bindings_ok_p (tree tparms, tree targs) | |
4813 | { | |
4814 | int i, ntparms = TREE_VEC_LENGTH (tparms); | |
fe8bc099 | 4815 | bool ret = true; |
4816 | ||
4817 | /* We're dealing with template parms in this process. */ | |
4818 | ++processing_template_decl; | |
e5dab245 | 4819 | |
4820 | targs = INNERMOST_TEMPLATE_ARGS (targs); | |
4821 | ||
4822 | for (i = 0; i < ntparms; ++i) | |
4823 | { | |
4824 | tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i)); | |
4825 | tree targ = TREE_VEC_ELT (targs, i); | |
4826 | ||
4827 | if (TREE_CODE (tparm) == TEMPLATE_DECL && targ) | |
4828 | { | |
4829 | tree packed_args = NULL_TREE; | |
4830 | int idx, len = 1; | |
4831 | ||
4832 | if (ARGUMENT_PACK_P (targ)) | |
4833 | { | |
4834 | /* Look inside the argument pack. */ | |
4835 | packed_args = ARGUMENT_PACK_ARGS (targ); | |
4836 | len = TREE_VEC_LENGTH (packed_args); | |
4837 | } | |
4838 | ||
4839 | for (idx = 0; idx < len; ++idx) | |
4840 | { | |
4841 | tree targ_parms = NULL_TREE; | |
4842 | ||
4843 | if (packed_args) | |
4844 | /* Extract the next argument from the argument | |
4845 | pack. */ | |
4846 | targ = TREE_VEC_ELT (packed_args, idx); | |
4847 | ||
4848 | if (PACK_EXPANSION_P (targ)) | |
4849 | /* Look at the pattern of the pack expansion. */ | |
4850 | targ = PACK_EXPANSION_PATTERN (targ); | |
4851 | ||
4852 | /* Extract the template parameters from the template | |
4853 | argument. */ | |
4854 | if (TREE_CODE (targ) == TEMPLATE_DECL) | |
4855 | targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ); | |
4856 | else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM) | |
4857 | targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ)); | |
4858 | ||
4859 | /* Verify that we can coerce the template template | |
4860 | parameters from the template argument to the template | |
4861 | parameter. This requires an exact match. */ | |
4862 | if (targ_parms | |
4863 | && !coerce_template_template_parms | |
4864 | (DECL_INNERMOST_TEMPLATE_PARMS (tparm), | |
4865 | targ_parms, | |
4866 | tf_none, | |
4867 | tparm, | |
4868 | targs)) | |
fe8bc099 | 4869 | { |
4870 | ret = false; | |
4871 | goto out; | |
4872 | } | |
e5dab245 | 4873 | } |
4874 | } | |
4875 | } | |
4876 | ||
fe8bc099 | 4877 | out: |
4878 | ||
4879 | --processing_template_decl; | |
4880 | return ret; | |
e5dab245 | 4881 | } |
4882 | ||
f9a16168 | 4883 | /* Convert the indicated template ARG as necessary to match the |
4884 | indicated template PARM. Returns the converted ARG, or | |
4b471722 | 4885 | error_mark_node if the conversion was unsuccessful. Error and |
4886 | warning messages are issued under control of COMPLAIN. This | |
4887 | conversion is for the Ith parameter in the parameter list. ARGS is | |
4888 | the full set of template arguments deduced so far. */ | |
f9a16168 | 4889 | |
4890 | static tree | |
9031d10b | 4891 | convert_template_argument (tree parm, |
653e5405 | 4892 | tree arg, |
4893 | tree args, | |
4894 | tsubst_flags_t complain, | |
4895 | int i, | |
4896 | tree in_decl) | |
f9a16168 | 4897 | { |
08b4cffd | 4898 | tree orig_arg; |
f9a16168 | 4899 | tree val; |
f9a16168 | 4900 | int is_type, requires_type, is_tmpl_type, requires_tmpl_type; |
9031d10b | 4901 | |
9031d10b | 4902 | if (TREE_CODE (arg) == TREE_LIST |
1bc16cab | 4903 | && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF) |
9031d10b | 4904 | { |
f9a16168 | 4905 | /* The template argument was the name of some |
4906 | member function. That's usually | |
6c0cc2cd | 4907 | invalid, but static members are OK. In any |
f9a16168 | 4908 | case, grab the underlying fields/functions |
4909 | and issue an error later if required. */ | |
08b4cffd | 4910 | orig_arg = TREE_VALUE (arg); |
f9a16168 | 4911 | TREE_TYPE (arg) = unknown_type_node; |
4912 | } | |
4913 | ||
08b4cffd | 4914 | orig_arg = arg; |
4915 | ||
f9a16168 | 4916 | requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL; |
4917 | requires_type = (TREE_CODE (parm) == TYPE_DECL | |
4918 | || requires_tmpl_type); | |
4919 | ||
45cc50c5 | 4920 | /* When determining whether an argument pack expansion is a template, |
d95d815d | 4921 | look at the pattern. */ |
08b4cffd | 4922 | if (TREE_CODE (arg) == TYPE_PACK_EXPANSION) |
4923 | arg = PACK_EXPANSION_PATTERN (arg); | |
d95d815d | 4924 | |
4925 | is_tmpl_type = | |
08b4cffd | 4926 | ((TREE_CODE (arg) == TEMPLATE_DECL |
4927 | && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL) | |
4928 | || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM | |
4929 | || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE); | |
9031d10b | 4930 | |
47d727d4 | 4931 | if (is_tmpl_type |
4932 | && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM | |
4933 | || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)) | |
f9a16168 | 4934 | arg = TYPE_STUB_DECL (arg); |
f9a16168 | 4935 | |
9308e976 | 4936 | is_type = TYPE_P (arg) || is_tmpl_type; |
f9a16168 | 4937 | |
4938 | if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF | |
4939 | && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM) | |
4940 | { | |
39e7b4a4 | 4941 | pedwarn ("to refer to a type member of a template parameter, " |
08b4cffd | 4942 | "use %<typename %E%>", orig_arg); |
9031d10b | 4943 | |
08b4cffd | 4944 | orig_arg = make_typename_type (TREE_OPERAND (arg, 0), |
4945 | TREE_OPERAND (arg, 1), | |
4946 | typename_type, | |
4947 | complain & tf_error); | |
4948 | arg = orig_arg; | |
f9a16168 | 4949 | is_type = 1; |
4950 | } | |
4951 | if (is_type != requires_type) | |
4952 | { | |
4953 | if (in_decl) | |
4954 | { | |
4b471722 | 4955 | if (complain & tf_error) |
f9a16168 | 4956 | { |
39e7b4a4 | 4957 | error ("type/value mismatch at argument %d in template " |
653e5405 | 4958 | "parameter list for %qD", |
4959 | i + 1, in_decl); | |
f9a16168 | 4960 | if (is_type) |
39e7b4a4 | 4961 | error (" expected a constant of type %qT, got %qT", |
653e5405 | 4962 | TREE_TYPE (parm), |
4eaf1b43 | 4963 | (DECL_P (arg) ? DECL_NAME (arg) : orig_arg)); |
9452f399 | 4964 | else if (requires_tmpl_type) |
08b4cffd | 4965 | error (" expected a class template, got %qE", orig_arg); |
f9a16168 | 4966 | else |
08b4cffd | 4967 | error (" expected a type, got %qE", orig_arg); |
f9a16168 | 4968 | } |
4969 | } | |
4970 | return error_mark_node; | |
4971 | } | |
4972 | if (is_tmpl_type ^ requires_tmpl_type) | |
4973 | { | |
4b471722 | 4974 | if (in_decl && (complain & tf_error)) |
f9a16168 | 4975 | { |
39e7b4a4 | 4976 | error ("type/value mismatch at argument %d in template " |
653e5405 | 4977 | "parameter list for %qD", |
4978 | i + 1, in_decl); | |
f9a16168 | 4979 | if (is_tmpl_type) |
39e7b4a4 | 4980 | error (" expected a type, got %qT", DECL_NAME (arg)); |
f9a16168 | 4981 | else |
08b4cffd | 4982 | error (" expected a class template, got %qT", orig_arg); |
f9a16168 | 4983 | } |
4984 | return error_mark_node; | |
4985 | } | |
9031d10b | 4986 | |
f9a16168 | 4987 | if (is_type) |
4988 | { | |
4989 | if (requires_tmpl_type) | |
4990 | { | |
47d727d4 | 4991 | if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE) |
4992 | /* The number of argument required is not known yet. | |
4993 | Just accept it for now. */ | |
4994 | val = TREE_TYPE (arg); | |
f9a16168 | 4995 | else |
4996 | { | |
47d727d4 | 4997 | tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm); |
d95d815d | 4998 | tree argparm; |
4999 | ||
08b4cffd | 5000 | argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg); |
47d727d4 | 5001 | |
5002 | if (coerce_template_template_parms (parmparm, argparm, | |
5003 | complain, in_decl, | |
a589f796 | 5004 | args)) |
f9a16168 | 5005 | { |
08b4cffd | 5006 | val = orig_arg; |
9031d10b | 5007 | |
5008 | /* TEMPLATE_TEMPLATE_PARM node is preferred over | |
47d727d4 | 5009 | TEMPLATE_DECL. */ |
d95d815d | 5010 | if (val != error_mark_node) |
5011 | { | |
5012 | if (DECL_TEMPLATE_TEMPLATE_PARM_P (val)) | |
5013 | val = TREE_TYPE (val); | |
5014 | else if (TREE_CODE (val) == TYPE_PACK_EXPANSION | |
08b4cffd | 5015 | && DECL_TEMPLATE_TEMPLATE_PARM_P (arg)) |
d95d815d | 5016 | { |
08b4cffd | 5017 | val = TREE_TYPE (arg); |
d95d815d | 5018 | val = make_pack_expansion (val); |
5019 | } | |
5020 | } | |
f9a16168 | 5021 | } |
47d727d4 | 5022 | else |
5023 | { | |
4b471722 | 5024 | if (in_decl && (complain & tf_error)) |
47d727d4 | 5025 | { |
39e7b4a4 | 5026 | error ("type/value mismatch at argument %d in " |
653e5405 | 5027 | "template parameter list for %qD", |
5028 | i + 1, in_decl); | |
39e7b4a4 | 5029 | error (" expected a template of type %qD, got %qD", |
08b4cffd | 5030 | parm, orig_arg); |
47d727d4 | 5031 | } |
9031d10b | 5032 | |
47d727d4 | 5033 | val = error_mark_node; |
5034 | } | |
f9a16168 | 5035 | } |
5036 | } | |
5037 | else | |
08b4cffd | 5038 | val = orig_arg; |
089fac52 | 5039 | /* We only form one instance of each template specialization. |
5040 | Therefore, if we use a non-canonical variant (i.e., a | |
074ab442 | 5041 | typedef), any future messages referring to the type will use |
089fac52 | 5042 | the typedef, which is confusing if those future uses do not |
5043 | themselves also use the typedef. */ | |
5044 | if (TYPE_P (val)) | |
5045 | val = canonical_type_variant (val); | |
f9a16168 | 5046 | } |
5047 | else | |
5048 | { | |
6d158f4d | 5049 | tree t = tsubst (TREE_TYPE (parm), args, complain, in_decl); |
f9a16168 | 5050 | |
f92ec819 | 5051 | if (invalid_nontype_parm_type_p (t, complain)) |
653e5405 | 5052 | return error_mark_node; |
9031d10b | 5053 | |
08b4cffd | 5054 | if (!uses_template_parms (orig_arg) && !uses_template_parms (t)) |
f9a16168 | 5055 | /* We used to call digest_init here. However, digest_init |
5056 | will report errors, which we don't want when complain | |
5057 | is zero. More importantly, digest_init will try too | |
5058 | hard to convert things: for example, `0' should not be | |
5059 | converted to pointer type at this point according to | |
5060 | the standard. Accepting this is not merely an | |
5061 | extension, since deciding whether or not these | |
5062 | conversions can occur is part of determining which | |
ec0eee8d | 5063 | function template to call, or whether a given explicit |
6c0cc2cd | 5064 | argument specification is valid. */ |
08b4cffd | 5065 | val = convert_nontype_argument (t, orig_arg); |
f9a16168 | 5066 | else |
08b4cffd | 5067 | val = orig_arg; |
f9a16168 | 5068 | |
5069 | if (val == NULL_TREE) | |
5070 | val = error_mark_node; | |
4b471722 | 5071 | else if (val == error_mark_node && (complain & tf_error)) |
08b4cffd | 5072 | error ("could not convert template argument %qE to %qT", orig_arg, t); |
f9a16168 | 5073 | } |
5074 | ||
5075 | return val; | |
5076 | } | |
5077 | ||
a4f66688 | 5078 | /* Coerces the remaining template arguments in INNER_ARGS (from |
5079 | ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS. | |
5080 | Returns the coerced argument pack. PARM_IDX is the position of this | |
5081 | parameter in the template parameter list. ARGS is the original | |
5082 | template argument list. */ | |
5083 | static tree | |
5084 | coerce_template_parameter_pack (tree parms, | |
5085 | int parm_idx, | |
5086 | tree args, | |
5087 | tree inner_args, | |
5088 | int arg_idx, | |
5089 | tree new_args, | |
5090 | int* lost, | |
5091 | tree in_decl, | |
5092 | tsubst_flags_t complain) | |
5093 | { | |
5094 | tree parm = TREE_VEC_ELT (parms, parm_idx); | |
5095 | int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0; | |
5096 | tree packed_args; | |
5097 | tree argument_pack; | |
5098 | tree packed_types = NULL_TREE; | |
5099 | ||
5100 | if (arg_idx > nargs) | |
5101 | arg_idx = nargs; | |
5102 | ||
5103 | packed_args = make_tree_vec (nargs - arg_idx); | |
5104 | ||
5105 | if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL | |
5106 | && uses_parameter_packs (TREE_TYPE (TREE_VALUE (parm)))) | |
5107 | { | |
5108 | /* When the template parameter is a non-type template | |
5109 | parameter pack whose type uses parameter packs, we need | |
5110 | to look at each of the template arguments | |
5111 | separately. Build a vector of the types for these | |
5112 | non-type template parameters in PACKED_TYPES. */ | |
5113 | tree expansion | |
5114 | = make_pack_expansion (TREE_TYPE (TREE_VALUE (parm))); | |
5115 | packed_types = tsubst_pack_expansion (expansion, args, | |
5116 | complain, in_decl); | |
5117 | ||
5118 | if (packed_types == error_mark_node) | |
5119 | return error_mark_node; | |
5120 | ||
5121 | /* Check that we have the right number of arguments. */ | |
5122 | if (arg_idx < nargs | |
5123 | && !PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)) | |
5124 | && nargs - arg_idx != TREE_VEC_LENGTH (packed_types)) | |
5125 | { | |
5126 | int needed_parms | |
5127 | = TREE_VEC_LENGTH (parms) - 1 + TREE_VEC_LENGTH (packed_types); | |
5128 | error ("wrong number of template arguments (%d, should be %d)", | |
5129 | nargs, needed_parms); | |
5130 | return error_mark_node; | |
5131 | } | |
5132 | ||
5133 | /* If we aren't able to check the actual arguments now | |
5134 | (because they haven't been expanded yet), we can at least | |
5135 | verify that all of the types used for the non-type | |
5136 | template parameter pack are, in fact, valid for non-type | |
5137 | template parameters. */ | |
5138 | if (arg_idx < nargs | |
5139 | && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx))) | |
5140 | { | |
5141 | int j, len = TREE_VEC_LENGTH (packed_types); | |
5142 | for (j = 0; j < len; ++j) | |
5143 | { | |
5144 | tree t = TREE_VEC_ELT (packed_types, j); | |
5145 | if (invalid_nontype_parm_type_p (t, complain)) | |
5146 | return error_mark_node; | |
5147 | } | |
5148 | } | |
5149 | } | |
5150 | ||
5151 | /* Convert the remaining arguments, which will be a part of the | |
5152 | parameter pack "parm". */ | |
5153 | for (; arg_idx < nargs; ++arg_idx) | |
5154 | { | |
5155 | tree arg = TREE_VEC_ELT (inner_args, arg_idx); | |
5156 | tree actual_parm = TREE_VALUE (parm); | |
5157 | ||
5158 | if (packed_types && !PACK_EXPANSION_P (arg)) | |
5159 | { | |
5160 | /* When we have a vector of types (corresponding to the | |
5161 | non-type template parameter pack that uses parameter | |
5162 | packs in its type, as mention above), and the | |
5163 | argument is not an expansion (which expands to a | |
5164 | currently unknown number of arguments), clone the | |
5165 | parm and give it the next type in PACKED_TYPES. */ | |
5166 | actual_parm = copy_node (actual_parm); | |
5167 | TREE_TYPE (actual_parm) = | |
5168 | TREE_VEC_ELT (packed_types, arg_idx - parm_idx); | |
5169 | } | |
5170 | ||
70dcc792 | 5171 | if (arg != error_mark_node) |
5172 | arg = convert_template_argument (actual_parm, | |
5173 | arg, new_args, complain, parm_idx, | |
5174 | in_decl); | |
a4f66688 | 5175 | if (arg == error_mark_node) |
5176 | (*lost)++; | |
5177 | TREE_VEC_ELT (packed_args, arg_idx - parm_idx) = arg; | |
5178 | } | |
5179 | ||
5180 | if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL | |
5181 | || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL) | |
5182 | argument_pack = make_node (TYPE_ARGUMENT_PACK); | |
5183 | else | |
5184 | { | |
5185 | argument_pack = make_node (NONTYPE_ARGUMENT_PACK); | |
08b4cffd | 5186 | TREE_TYPE (argument_pack) |
41341abd | 5187 | = tsubst (TREE_TYPE (TREE_VALUE (parm)), new_args, complain, in_decl); |
a4f66688 | 5188 | TREE_CONSTANT (argument_pack) = 1; |
5189 | } | |
5190 | ||
5191 | SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args); | |
5192 | return argument_pack; | |
5193 | } | |
5194 | ||
f9a16168 | 5195 | /* Convert all template arguments to their appropriate types, and |
5196 | return a vector containing the innermost resulting template | |
4b471722 | 5197 | arguments. If any error occurs, return error_mark_node. Error and |
2bc53434 | 5198 | warning messages are issued under control of COMPLAIN. |
668ae905 | 5199 | |
36653a30 | 5200 | If REQUIRE_ALL_ARGS is false, argument deduction will be performed |
3b49899c | 5201 | for arguments not specified in ARGS. Otherwise, if |
36653a30 | 5202 | USE_DEFAULT_ARGS is true, default arguments will be used to fill in |
5203 | unspecified arguments. If REQUIRE_ALL_ARGS is true, but | |
5204 | USE_DEFAULT_ARGS is false, then all arguments must be specified in | |
5205 | ARGS. */ | |
9031d10b | 5206 | |
471086d6 | 5207 | static tree |
9031d10b | 5208 | coerce_template_parms (tree parms, |
653e5405 | 5209 | tree args, |
5210 | tree in_decl, | |
807be5b4 | 5211 | tsubst_flags_t complain, |
36653a30 | 5212 | bool require_all_args, |
5213 | bool use_default_args) | |
471086d6 | 5214 | { |
a4f66688 | 5215 | int nparms, nargs, parm_idx, arg_idx, lost = 0; |
26430721 | 5216 | tree inner_args; |
f9a16168 | 5217 | tree new_args; |
5218 | tree new_inner_args; | |
61ebd7a0 | 5219 | bool saved_skip_evaluation; |
f3ba5c6a | 5220 | |
d95d815d | 5221 | /* When used as a boolean value, indicates whether this is a |
5222 | variadic template parameter list. Since it's an int, we can also | |
5223 | subtract it from nparms to get the number of non-variadic | |
5224 | parameters. */ | |
a4f66688 | 5225 | int variadic_p = 0; |
d95d815d | 5226 | |
5227 | inner_args | |
5228 | = expand_template_argument_pack (INNERMOST_TEMPLATE_ARGS (args)); | |
5229 | ||
b5959ba9 | 5230 | nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0; |
f3ba5c6a | 5231 | nparms = TREE_VEC_LENGTH (parms); |
5232 | ||
a4f66688 | 5233 | /* Determine if there are any parameter packs. */ |
5234 | for (parm_idx = 0; parm_idx < nparms; ++parm_idx) | |
5235 | { | |
5236 | tree tparm = TREE_VALUE (TREE_VEC_ELT (parms, parm_idx)); | |
5237 | if (template_parameter_pack_p (tparm)) | |
5238 | { | |
5239 | variadic_p = 1; | |
5240 | break; | |
5241 | } | |
5242 | } | |
5243 | ||
d95d815d | 5244 | if ((nargs > nparms - variadic_p && !variadic_p) |
5245 | || (nargs < nparms - variadic_p | |
36653a30 | 5246 | && require_all_args |
5247 | && (!use_default_args | |
508c7aa7 | 5248 | || (TREE_VEC_ELT (parms, nargs) != error_mark_node |
5249 | && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs)))))) | |
471086d6 | 5250 | { |
9031d10b | 5251 | if (complain & tf_error) |
668ae905 | 5252 | { |
d95d815d | 5253 | const char *or_more = ""; |
5254 | if (variadic_p) | |
5255 | { | |
5256 | or_more = " or more"; | |
5257 | --nparms; | |
5258 | } | |
5259 | ||
5260 | error ("wrong number of template arguments (%d, should be %d%s)", | |
5261 | nargs, nparms, or_more); | |
9031d10b | 5262 | |
668ae905 | 5263 | if (in_decl) |
3cf8b391 | 5264 | error ("provided for %q+D", in_decl); |
668ae905 | 5265 | } |
5266 | ||
471086d6 | 5267 | return error_mark_node; |
5268 | } | |
5269 | ||
61ebd7a0 | 5270 | /* We need to evaluate the template arguments, even though this |
5271 | template-id may be nested within a "sizeof". */ | |
5272 | saved_skip_evaluation = skip_evaluation; | |
5273 | skip_evaluation = false; | |
19c3b3a6 | 5274 | new_inner_args = make_tree_vec (nparms); |
f9a16168 | 5275 | new_args = add_outermost_template_args (args, new_inner_args); |
a4f66688 | 5276 | for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++) |
471086d6 | 5277 | { |
f9a16168 | 5278 | tree arg; |
5279 | tree parm; | |
26430721 | 5280 | |
f9a16168 | 5281 | /* Get the Ith template parameter. */ |
a4f66688 | 5282 | parm = TREE_VEC_ELT (parms, parm_idx); |
508c7aa7 | 5283 | |
5284 | if (parm == error_mark_node) | |
6f32df61 | 5285 | { |
a4f66688 | 5286 | TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node; |
508c7aa7 | 5287 | continue; |
6f32df61 | 5288 | } |
668ae905 | 5289 | |
a4f66688 | 5290 | /* Calculate the next argument. */ |
5291 | if (template_parameter_pack_p (TREE_VALUE (parm))) | |
d95d815d | 5292 | { |
a4f66688 | 5293 | /* All remaining arguments will be placed in the |
5294 | template parameter pack PARM. */ | |
5295 | arg = coerce_template_parameter_pack (parms, parm_idx, args, | |
5296 | inner_args, arg_idx, | |
5297 | new_args, &lost, | |
5298 | in_decl, complain); | |
5299 | ||
5300 | /* Store this argument. */ | |
5301 | if (arg == error_mark_node) | |
5302 | lost++; | |
5303 | TREE_VEC_ELT (new_inner_args, parm_idx) = arg; | |
5304 | ||
5305 | /* We are done with all of the arguments. */ | |
5306 | arg_idx = nargs; | |
5307 | ||
5308 | continue; | |
5309 | } | |
5310 | else if (arg_idx < nargs) | |
5311 | { | |
5312 | arg = TREE_VEC_ELT (inner_args, arg_idx); | |
5313 | ||
5314 | if (arg && PACK_EXPANSION_P (arg)) | |
d95d815d | 5315 | { |
e5dab245 | 5316 | if (complain & tf_error) |
5317 | { | |
5318 | /* If ARG is a pack expansion, but PARM is not a | |
5319 | template parameter pack (if it were, we would have | |
5320 | handled it above), we're trying to expand into a | |
5321 | fixed-length argument list. */ | |
5322 | if (TREE_CODE (arg) == EXPR_PACK_EXPANSION) | |
5323 | error ("cannot expand %<%E%> into a fixed-length " | |
5324 | "argument list", arg); | |
5325 | else | |
5326 | error ("cannot expand %<%T%> into a fixed-length " | |
5327 | "argument list", arg); | |
5328 | } | |
70dcc792 | 5329 | return error_mark_node; |
d95d815d | 5330 | } |
5331 | } | |
36653a30 | 5332 | else if (require_all_args) |
a4f66688 | 5333 | /* There must be a default arg in this case. */ |
5334 | arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args, | |
5335 | complain, in_decl); | |
8b070b49 | 5336 | else |
5337 | break; | |
9031d10b | 5338 | |
8b070b49 | 5339 | if (arg == error_mark_node) |
bfc966a9 | 5340 | { |
5341 | if (complain & tf_error) | |
a4f66688 | 5342 | error ("template argument %d is invalid", arg_idx + 1); |
bfc966a9 | 5343 | } |
a4f66688 | 5344 | else if (!arg) |
5345 | /* This only occurs if there was an error in the template | |
5346 | parameter list itself (which we would already have | |
5347 | reported) that we are trying to recover from, e.g., a class | |
5348 | template with a parameter list such as | |
5349 | template<typename..., typename>. */ | |
5350 | return error_mark_node; | |
9031d10b | 5351 | else |
5352 | arg = convert_template_argument (TREE_VALUE (parm), | |
a4f66688 | 5353 | arg, new_args, complain, |
5354 | parm_idx, in_decl); | |
9031d10b | 5355 | |
f9a16168 | 5356 | if (arg == error_mark_node) |
471086d6 | 5357 | lost++; |
a4f66688 | 5358 | TREE_VEC_ELT (new_inner_args, arg_idx) = arg; |
471086d6 | 5359 | } |
61ebd7a0 | 5360 | skip_evaluation = saved_skip_evaluation; |
f9a16168 | 5361 | |
471086d6 | 5362 | if (lost) |
5363 | return error_mark_node; | |
f9a16168 | 5364 | |
5365 | return new_inner_args; | |
471086d6 | 5366 | } |
5367 | ||
eaf0f63a | 5368 | /* Returns 1 if template args OT and NT are equivalent. */ |
5369 | ||
e1721763 | 5370 | static int |
807be5b4 | 5371 | template_args_equal (tree ot, tree nt) |
eaf0f63a | 5372 | { |
5373 | if (nt == ot) | |
5374 | return 1; | |
25963226 | 5375 | |
eaf0f63a | 5376 | if (TREE_CODE (nt) == TREE_VEC) |
5377 | /* For member templates */ | |
25963226 | 5378 | return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt); |
d95d815d | 5379 | else if (PACK_EXPANSION_P (ot)) |
5380 | return PACK_EXPANSION_P (nt) | |
5381 | && template_args_equal (PACK_EXPANSION_PATTERN (ot), | |
5382 | PACK_EXPANSION_PATTERN (nt)); | |
25963226 | 5383 | else if (TYPE_P (nt)) |
aa796005 | 5384 | return TYPE_P (ot) && same_type_p (ot, nt); |
25963226 | 5385 | else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot)) |
5386 | return 0; | |
eaf0f63a | 5387 | else |
00952d10 | 5388 | return cp_tree_equal (ot, nt); |
eaf0f63a | 5389 | } |
5390 | ||
5391 | /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets | |
f3110581 | 5392 | of template arguments. Returns 0 otherwise. */ |
5393 | ||
0f0d5a69 | 5394 | int |
807be5b4 | 5395 | comp_template_args (tree oldargs, tree newargs) |
e857e9c7 | 5396 | { |
5397 | int i; | |
5398 | ||
d95d815d | 5399 | oldargs = expand_template_argument_pack (oldargs); |
5400 | newargs = expand_template_argument_pack (newargs); | |
5401 | ||
b1cfe2be | 5402 | if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs)) |
5403 | return 0; | |
5404 | ||
e857e9c7 | 5405 | for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i) |
5406 | { | |
5407 | tree nt = TREE_VEC_ELT (newargs, i); | |
5408 | tree ot = TREE_VEC_ELT (oldargs, i); | |
5409 | ||
eaf0f63a | 5410 | if (! template_args_equal (ot, nt)) |
0f2952a1 | 5411 | return 0; |
e857e9c7 | 5412 | } |
5413 | return 1; | |
5414 | } | |
5415 | ||
e857e9c7 | 5416 | static void |
807be5b4 | 5417 | add_pending_template (tree d) |
e857e9c7 | 5418 | { |
04d89d04 | 5419 | tree ti = (TYPE_P (d) |
5420 | ? CLASSTYPE_TEMPLATE_INFO (d) | |
5421 | : DECL_TEMPLATE_INFO (d)); | |
c5dd8e06 | 5422 | struct pending_template *pt; |
04d89d04 | 5423 | int level; |
96624a9e | 5424 | |
02d7f858 | 5425 | if (TI_PENDING_TEMPLATE_FLAG (ti)) |
e857e9c7 | 5426 | return; |
5427 | ||
04d89d04 | 5428 | /* We are called both from instantiate_decl, where we've already had a |
5429 | tinst_level pushed, and instantiate_template, where we haven't. | |
5430 | Compensate. */ | |
c5dd8e06 | 5431 | level = !current_tinst_level || current_tinst_level->decl != d; |
04d89d04 | 5432 | |
5433 | if (level) | |
5434 | push_tinst_level (d); | |
5435 | ||
c5dd8e06 | 5436 | pt = GGC_NEW (struct pending_template); |
5437 | pt->next = NULL; | |
5438 | pt->tinst = current_tinst_level; | |
b53db2b0 | 5439 | if (last_pending_template) |
c5dd8e06 | 5440 | last_pending_template->next = pt; |
b53db2b0 | 5441 | else |
5442 | pending_templates = pt; | |
5443 | ||
5444 | last_pending_template = pt; | |
5445 | ||
02d7f858 | 5446 | TI_PENDING_TEMPLATE_FLAG (ti) = 1; |
04d89d04 | 5447 | |
5448 | if (level) | |
5449 | pop_tinst_level (); | |
e857e9c7 | 5450 | } |
5451 | ||
b1cfe2be | 5452 | |
f70cb9e6 | 5453 | /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and |
5454 | ARGLIST. Valid choices for FNS are given in the cp-tree.def | |
5455 | documentation for TEMPLATE_ID_EXPR. */ | |
b1cfe2be | 5456 | |
5457 | tree | |
807be5b4 | 5458 | lookup_template_function (tree fns, tree arglist) |
b1cfe2be | 5459 | { |
8417823c | 5460 | tree type; |
c906a2a7 | 5461 | |
f70cb9e6 | 5462 | if (fns == error_mark_node || arglist == error_mark_node) |
5463 | return error_mark_node; | |
5464 | ||
b4df430b | 5465 | gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC); |
42510d75 | 5466 | gcc_assert (fns && (is_overloaded_fn (fns) |
5467 | || TREE_CODE (fns) == IDENTIFIER_NODE)); | |
f70cb9e6 | 5468 | |
4ac852cb | 5469 | if (BASELINK_P (fns)) |
5470 | { | |
831d52a2 | 5471 | BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR, |
5472 | unknown_type_node, | |
5473 | BASELINK_FUNCTIONS (fns), | |
5474 | arglist); | |
4ac852cb | 5475 | return fns; |
5476 | } | |
5477 | ||
8417823c | 5478 | type = TREE_TYPE (fns); |
5479 | if (TREE_CODE (fns) == OVERLOAD || !type) | |
5480 | type = unknown_type_node; | |
9031d10b | 5481 | |
831d52a2 | 5482 | return build2 (TEMPLATE_ID_EXPR, type, fns, arglist); |
b1cfe2be | 5483 | } |
5484 | ||
1e5f085c | 5485 | /* Within the scope of a template class S<T>, the name S gets bound |
5486 | (in build_self_reference) to a TYPE_DECL for the class, not a | |
5487 | TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type, | |
5488 | or one of its enclosing classes, and that type is a template, | |
5489 | return the associated TEMPLATE_DECL. Otherwise, the original | |
5490 | DECL is returned. */ | |
5491 | ||
0a3b29ad | 5492 | tree |
807be5b4 | 5493 | maybe_get_template_decl_from_type_decl (tree decl) |
1e5f085c | 5494 | { |
5495 | return (decl != NULL_TREE | |
9031d10b | 5496 | && TREE_CODE (decl) == TYPE_DECL |
1e5f085c | 5497 | && DECL_ARTIFICIAL (decl) |
1d0ce42a | 5498 | && CLASS_TYPE_P (TREE_TYPE (decl)) |
9031d10b | 5499 | && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl))) |
1e5f085c | 5500 | ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl; |
5501 | } | |
b1cfe2be | 5502 | |
471086d6 | 5503 | /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of |
5504 | parameters, find the desired type. | |
5505 | ||
5506 | D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments. | |
471086d6 | 5507 | |
5508 | IN_DECL, if non-NULL, is the template declaration we are trying to | |
9031d10b | 5509 | instantiate. |
668ae905 | 5510 | |
3160db1d | 5511 | If ENTERING_SCOPE is nonzero, we are about to enter the scope of |
34197853 | 5512 | the class we are looking up. |
9031d10b | 5513 | |
4b471722 | 5514 | Issue error and warning messages under control of COMPLAIN. |
34197853 | 5515 | |
668ae905 | 5516 | If the template class is really a local class in a template |
5517 | function, then the FUNCTION_CONTEXT is the function in which it is | |
9031d10b | 5518 | being instantiated. |
b312a686 | 5519 | |
5520 | ??? Note that this function is currently called *twice* for each | |
5521 | template-id: the first time from the parser, while creating the | |
5522 | incomplete type (finish_template_type), and the second type during the | |
5523 | real instantiation (instantiate_template_class). This is surely something | |
5524 | that we want to avoid. It also causes some problems with argument | |
5525 | coercion (see convert_nontype_argument for more information on this). */ | |
96624a9e | 5526 | |
471086d6 | 5527 | tree |
9031d10b | 5528 | lookup_template_class (tree d1, |
653e5405 | 5529 | tree arglist, |
5530 | tree in_decl, | |
5531 | tree context, | |
5532 | int entering_scope, | |
5533 | tsubst_flags_t complain) | |
471086d6 | 5534 | { |
034b484a | 5535 | tree template = NULL_TREE, parmlist; |
d91e3832 | 5536 | tree t; |
9031d10b | 5537 | |
a322b452 | 5538 | timevar_push (TV_NAME_LOOKUP); |
9031d10b | 5539 | |
e857e9c7 | 5540 | if (TREE_CODE (d1) == IDENTIFIER_NODE) |
5541 | { | |
d09ae6d5 | 5542 | tree value = innermost_non_namespace_value (d1); |
5543 | if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value)) | |
5544 | template = value; | |
cd5dd2d8 | 5545 | else |
5546 | { | |
8417823c | 5547 | if (context) |
5548 | push_decl_namespace (context); | |
d1c41717 | 5549 | template = lookup_name (d1); |
7af67ee8 | 5550 | template = maybe_get_template_decl_from_type_decl (template); |
8417823c | 5551 | if (context) |
5552 | pop_decl_namespace (); | |
cd5dd2d8 | 5553 | } |
9ebe7529 | 5554 | if (template) |
5555 | context = DECL_CONTEXT (template); | |
e857e9c7 | 5556 | } |
95397ff9 | 5557 | else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1))) |
1e28ccf1 | 5558 | { |
6e4a6ed9 | 5559 | tree type = TREE_TYPE (d1); |
5560 | ||
5561 | /* If we are declaring a constructor, say A<T>::A<T>, we will get | |
5562 | an implicit typename for the second A. Deal with it. */ | |
5563 | if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type)) | |
5564 | type = TREE_TYPE (type); | |
9031d10b | 5565 | |
6e4a6ed9 | 5566 | if (CLASSTYPE_TEMPLATE_INFO (type)) |
c24cced6 | 5567 | { |
6e4a6ed9 | 5568 | template = CLASSTYPE_TI_TEMPLATE (type); |
c24cced6 | 5569 | d1 = DECL_NAME (template); |
5570 | } | |
1e28ccf1 | 5571 | } |
9031d10b | 5572 | else if (TREE_CODE (d1) == ENUMERAL_TYPE |
95397ff9 | 5573 | || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1))) |
e857e9c7 | 5574 | { |
11fa0698 | 5575 | template = TYPE_TI_TEMPLATE (d1); |
e857e9c7 | 5576 | d1 = DECL_NAME (template); |
5577 | } | |
1ac9b32b | 5578 | else if (TREE_CODE (d1) == TEMPLATE_DECL |
b8e0d419 | 5579 | && TREE_CODE (DECL_TEMPLATE_RESULT (d1)) == TYPE_DECL) |
1ac9b32b | 5580 | { |
5581 | template = d1; | |
5582 | d1 = DECL_NAME (template); | |
5583 | context = DECL_CONTEXT (template); | |
5584 | } | |
471086d6 | 5585 | |
d09ae6d5 | 5586 | /* Issue an error message if we didn't find a template. */ |
471086d6 | 5587 | if (! template) |
c24cced6 | 5588 | { |
4b471722 | 5589 | if (complain & tf_error) |
653e5405 | 5590 | error ("%qT is not a template", d1); |
a322b452 | 5591 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node); |
c24cced6 | 5592 | } |
8417823c | 5593 | |
73a7dbdd | 5594 | if (TREE_CODE (template) != TEMPLATE_DECL |
653e5405 | 5595 | /* Make sure it's a user visible template, if it was named by |
b992fe70 | 5596 | the user. */ |
5597 | || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (template) | |
5598 | && !PRIMARY_TEMPLATE_P (template))) | |
471086d6 | 5599 | { |
4b471722 | 5600 | if (complain & tf_error) |
653e5405 | 5601 | { |
5602 | error ("non-template type %qT used as a template", d1); | |
5603 | if (in_decl) | |
3cf8b391 | 5604 | error ("for template declaration %q+D", in_decl); |
f8ce2bac | 5605 | } |
a322b452 | 5606 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node); |
471086d6 | 5607 | } |
471086d6 | 5608 | |
b992fe70 | 5609 | complain &= ~tf_user; |
9031d10b | 5610 | |
cd5dd2d8 | 5611 | if (DECL_TEMPLATE_TEMPLATE_PARM_P (template)) |
5612 | { | |
5613 | /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store | |
653e5405 | 5614 | template arguments */ |
cd5dd2d8 | 5615 | |
6d2b611f | 5616 | tree parm; |
cd5dd2d8 | 5617 | tree arglist2; |
864f2267 | 5618 | tree outer; |
cd5dd2d8 | 5619 | |
cd5dd2d8 | 5620 | parmlist = DECL_INNERMOST_TEMPLATE_PARMS (template); |
5621 | ||
977f01e1 | 5622 | /* Consider an example where a template template parameter declared as |
5623 | ||
5624 | template <class T, class U = std::allocator<T> > class TT | |
5625 | ||
9031d10b | 5626 | The template parameter level of T and U are one level larger than |
5627 | of TT. To proper process the default argument of U, say when an | |
977f01e1 | 5628 | instantiation `TT<int>' is seen, we need to build the full |
d7524814 | 5629 | arguments containing {int} as the innermost level. Outer levels, |
5630 | available when not appearing as default template argument, can be | |
864f2267 | 5631 | obtained from the arguments of the enclosing template. |
977f01e1 | 5632 | |
d7524814 | 5633 | Suppose that TT is later substituted with std::vector. The above |
5634 | instantiation is `TT<int, std::allocator<T> >' with TT at | |
5635 | level 1, and T at level 2, while the template arguments at level 1 | |
5636 | becomes {std::vector} and the inner level 2 is {int}. */ | |
5637 | ||
864f2267 | 5638 | outer = DECL_CONTEXT (template); |
5639 | if (outer) | |
5640 | outer = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer))); | |
5641 | else if (current_template_parms) | |
5642 | /* This is an argument of the current template, so we haven't set | |
5643 | DECL_CONTEXT yet. */ | |
5644 | outer = current_template_args (); | |
5645 | ||
5646 | if (outer) | |
5647 | arglist = add_to_template_args (outer, arglist); | |
977f01e1 | 5648 | |
f8ce2bac | 5649 | arglist2 = coerce_template_parms (parmlist, arglist, template, |
074ab442 | 5650 | complain, |
36653a30 | 5651 | /*require_all_args=*/true, |
5652 | /*use_default_args=*/true); | |
2bc53434 | 5653 | if (arglist2 == error_mark_node |
51e8ba24 | 5654 | || (!uses_template_parms (arglist2) |
2bc53434 | 5655 | && check_instantiated_args (template, arglist2, complain))) |
653e5405 | 5656 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node); |
cd5dd2d8 | 5657 | |
e01e9e5a | 5658 | parm = bind_template_template_parm (TREE_TYPE (template), arglist2); |
a322b452 | 5659 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, parm); |
cd5dd2d8 | 5660 | } |
9031d10b | 5661 | else |
471086d6 | 5662 | { |
34197853 | 5663 | tree template_type = TREE_TYPE (template); |
a0412b00 | 5664 | tree gen_tmpl; |
34197853 | 5665 | tree type_decl; |
5666 | tree found = NULL_TREE; | |
5667 | int arg_depth; | |
5668 | int parm_depth; | |
d91e3832 | 5669 | int is_partial_instantiation; |
32b5f03e | 5670 | |
a0412b00 | 5671 | gen_tmpl = most_general_template (template); |
5672 | parmlist = DECL_TEMPLATE_PARMS (gen_tmpl); | |
34197853 | 5673 | parm_depth = TMPL_PARMS_DEPTH (parmlist); |
5674 | arg_depth = TMPL_ARGS_DEPTH (arglist); | |
5675 | ||
5676 | if (arg_depth == 1 && parm_depth > 1) | |
5677 | { | |
63e8563c | 5678 | /* We've been given an incomplete set of template arguments. |
34197853 | 5679 | For example, given: |
5680 | ||
5681 | template <class T> struct S1 { | |
653e5405 | 5682 | template <class U> struct S2 {}; |
34197853 | 5683 | template <class U> struct S2<U*> {}; |
653e5405 | 5684 | }; |
9031d10b | 5685 | |
34197853 | 5686 | we will be called with an ARGLIST of `U*', but the |
5687 | TEMPLATE will be `template <class T> template | |
5688 | <class U> struct S1<T>::S2'. We must fill in the missing | |
5689 | arguments. */ | |
9031d10b | 5690 | arglist |
a0412b00 | 5691 | = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (template)), |
5692 | arglist); | |
34197853 | 5693 | arg_depth = TMPL_ARGS_DEPTH (arglist); |
5694 | } | |
e857e9c7 | 5695 | |
8bc57e28 | 5696 | /* Now we should have enough arguments. */ |
b4df430b | 5697 | gcc_assert (parm_depth == arg_depth); |
9031d10b | 5698 | |
a0412b00 | 5699 | /* From here on, we're only interested in the most general |
5700 | template. */ | |
5701 | template = gen_tmpl; | |
5702 | ||
34197853 | 5703 | /* Calculate the BOUND_ARGS. These will be the args that are |
5704 | actually tsubst'd into the definition to create the | |
5705 | instantiation. */ | |
5706 | if (parm_depth > 1) | |
32b5f03e | 5707 | { |
5708 | /* We have multiple levels of arguments to coerce, at once. */ | |
32b5f03e | 5709 | int i; |
26430721 | 5710 | int saved_depth = TMPL_ARGS_DEPTH (arglist); |
34197853 | 5711 | |
19c3b3a6 | 5712 | tree bound_args = make_tree_vec (parm_depth); |
9031d10b | 5713 | |
26430721 | 5714 | for (i = saved_depth, |
9031d10b | 5715 | t = DECL_TEMPLATE_PARMS (template); |
26430721 | 5716 | i > 0 && t != NULL_TREE; |
32b5f03e | 5717 | --i, t = TREE_CHAIN (t)) |
26430721 | 5718 | { |
8512e308 | 5719 | tree a = coerce_template_parms (TREE_VALUE (t), |
5720 | arglist, template, | |
074ab442 | 5721 | complain, |
36653a30 | 5722 | /*require_all_args=*/true, |
5723 | /*use_default_args=*/true); | |
fed48d89 | 5724 | |
5725 | /* Don't process further if one of the levels fails. */ | |
5726 | if (a == error_mark_node) | |
5727 | { | |
5728 | /* Restore the ARGLIST to its full size. */ | |
5729 | TREE_VEC_LENGTH (arglist) = saved_depth; | |
5730 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node); | |
5731 | } | |
9031d10b | 5732 | |
26430721 | 5733 | SET_TMPL_ARGS_LEVEL (bound_args, i, a); |
5734 | ||
5735 | /* We temporarily reduce the length of the ARGLIST so | |
5736 | that coerce_template_parms will see only the arguments | |
5737 | corresponding to the template parameters it is | |
5738 | examining. */ | |
5739 | TREE_VEC_LENGTH (arglist)--; | |
5740 | } | |
5741 | ||
5742 | /* Restore the ARGLIST to its full size. */ | |
5743 | TREE_VEC_LENGTH (arglist) = saved_depth; | |
5744 | ||
34197853 | 5745 | arglist = bound_args; |
32b5f03e | 5746 | } |
5747 | else | |
34197853 | 5748 | arglist |
5749 | = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist), | |
a32fe883 | 5750 | INNERMOST_TEMPLATE_ARGS (arglist), |
f8ce2bac | 5751 | template, |
074ab442 | 5752 | complain, |
36653a30 | 5753 | /*require_all_args=*/true, |
5754 | /*use_default_args=*/true); | |
34197853 | 5755 | |
7f233616 | 5756 | if (arglist == error_mark_node) |
34197853 | 5757 | /* We were unable to bind the arguments. */ |
a322b452 | 5758 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node); |
e857e9c7 | 5759 | |
34197853 | 5760 | /* In the scope of a template class, explicit references to the |
5761 | template class refer to the type of the template, not any | |
5762 | instantiation of it. For example, in: | |
9031d10b | 5763 | |
34197853 | 5764 | template <class T> class C { void f(C<T>); } |
5765 | ||
5766 | the `C<T>' is just the same as `C'. Outside of the | |
5767 | class, however, such a reference is an instantiation. */ | |
11fa0698 | 5768 | if (comp_template_args (TYPE_TI_ARGS (template_type), |
34197853 | 5769 | arglist)) |
5770 | { | |
5771 | found = template_type; | |
9031d10b | 5772 | |
34197853 | 5773 | if (!entering_scope && PRIMARY_TEMPLATE_P (template)) |
e857e9c7 | 5774 | { |
34197853 | 5775 | tree ctx; |
9031d10b | 5776 | |
5777 | for (ctx = current_class_type; | |
44e4cd44 | 5778 | ctx && TREE_CODE (ctx) != NAMESPACE_DECL; |
5779 | ctx = (TYPE_P (ctx) | |
5780 | ? TYPE_CONTEXT (ctx) | |
5781 | : DECL_CONTEXT (ctx))) | |
5782 | if (TYPE_P (ctx) && same_type_p (ctx, template_type)) | |
5783 | goto found_ctx; | |
9031d10b | 5784 | |
e4f430b5 | 5785 | /* We're not in the scope of the class, so the |
5786 | TEMPLATE_TYPE is not the type we want after all. */ | |
5787 | found = NULL_TREE; | |
5788 | found_ctx:; | |
e857e9c7 | 5789 | } |
5790 | } | |
34197853 | 5791 | if (found) |
653e5405 | 5792 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, found); |
ca9f5265 | 5793 | |
38d89ee9 | 5794 | /* If we already have this specialization, return it. */ |
5795 | found = retrieve_specialization (template, arglist, | |
5796 | /*class_specializations_p=*/false); | |
5797 | if (found) | |
5798 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, found); | |
e857e9c7 | 5799 | |
d91e3832 | 5800 | /* This type is a "partial instantiation" if any of the template |
5d5ece7c | 5801 | arguments still involve template parameters. Note that we set |
1ab0a87c | 5802 | IS_PARTIAL_INSTANTIATION for partial specializations as |
5803 | well. */ | |
d91e3832 | 5804 | is_partial_instantiation = uses_template_parms (arglist); |
5805 | ||
7f233616 | 5806 | /* If the deduced arguments are invalid, then the binding |
5807 | failed. */ | |
5808 | if (!is_partial_instantiation | |
5809 | && check_instantiated_args (template, | |
5810 | INNERMOST_TEMPLATE_ARGS (arglist), | |
5811 | complain)) | |
5812 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node); | |
9031d10b | 5813 | |
5814 | if (!is_partial_instantiation | |
7af67ee8 | 5815 | && !PRIMARY_TEMPLATE_P (template) |
5816 | && TREE_CODE (CP_DECL_CONTEXT (template)) == NAMESPACE_DECL) | |
5817 | { | |
7af67ee8 | 5818 | found = xref_tag_from_type (TREE_TYPE (template), |
5819 | DECL_NAME (template), | |
1fadf2c8 | 5820 | /*tag_scope=*/ts_global); |
a322b452 | 5821 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, found); |
7af67ee8 | 5822 | } |
9031d10b | 5823 | |
5ef286c9 | 5824 | context = tsubst (DECL_CONTEXT (template), arglist, |
4b471722 | 5825 | complain, in_decl); |
5ef286c9 | 5826 | if (!context) |
5827 | context = global_namespace; | |
5828 | ||
34197853 | 5829 | /* Create the type. */ |
11fa0698 | 5830 | if (TREE_CODE (template_type) == ENUMERAL_TYPE) |
5831 | { | |
d91e3832 | 5832 | if (!is_partial_instantiation) |
cdda1383 | 5833 | { |
5834 | set_current_access_from_decl (TYPE_NAME (template_type)); | |
5835 | t = start_enum (TYPE_IDENTIFIER (template_type)); | |
5836 | } | |
11fa0698 | 5837 | else |
d91e3832 | 5838 | /* We don't want to call start_enum for this type, since |
11fa0698 | 5839 | the values for the enumeration constants may involve |
5840 | template parameters. And, no one should be interested | |
5841 | in the enumeration constants for such a type. */ | |
5842 | t = make_node (ENUMERAL_TYPE); | |
5843 | } | |
5844 | else | |
5845 | { | |
95397ff9 | 5846 | t = make_class_type (TREE_CODE (template_type)); |
9031d10b | 5847 | CLASSTYPE_DECLARED_CLASS (t) |
11fa0698 | 5848 | = CLASSTYPE_DECLARED_CLASS (template_type); |
11fa0698 | 5849 | SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t); |
b5711a32 | 5850 | TYPE_FOR_JAVA (t) = TYPE_FOR_JAVA (template_type); |
b3908271 | 5851 | |
5852 | /* A local class. Make sure the decl gets registered properly. */ | |
5853 | if (context == current_function_decl) | |
3f3fa556 | 5854 | pushtag (DECL_NAME (template), t, /*tag_scope=*/ts_current); |
6753bca0 | 5855 | |
5856 | if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist)) | |
5857 | /* This instantiation is another name for the primary | |
5858 | template type. Set the TYPE_CANONICAL field | |
5859 | appropriately. */ | |
5860 | TYPE_CANONICAL (t) = template_type; | |
5861 | else if (any_template_arguments_need_structural_equality_p (arglist)) | |
5862 | /* Some of the template arguments require structural | |
5863 | equality testing, so this template class requires | |
5864 | structural equality testing. */ | |
5865 | SET_TYPE_STRUCTURAL_EQUALITY (t); | |
11fa0698 | 5866 | } |
5867 | ||
b3908271 | 5868 | /* If we called start_enum or pushtag above, this information |
5869 | will already be set up. */ | |
11fa0698 | 5870 | if (!TYPE_NAME (t)) |
5871 | { | |
5872 | TYPE_CONTEXT (t) = FROB_CONTEXT (context); | |
9031d10b | 5873 | |
70a658bd | 5874 | type_decl = create_implicit_typedef (DECL_NAME (template), t); |
11fa0698 | 5875 | DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t); |
70a658bd | 5876 | TYPE_STUB_DECL (t) = type_decl; |
9031d10b | 5877 | DECL_SOURCE_LOCATION (type_decl) |
346064d9 | 5878 | = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type)); |
11fa0698 | 5879 | } |
5880 | else | |
5881 | type_decl = TYPE_NAME (t); | |
34197853 | 5882 | |
0246130d | 5883 | TREE_PRIVATE (type_decl) |
5884 | = TREE_PRIVATE (TYPE_STUB_DECL (template_type)); | |
5885 | TREE_PROTECTED (type_decl) | |
5886 | = TREE_PROTECTED (TYPE_STUB_DECL (template_type)); | |
8f3b5fc9 | 5887 | DECL_IN_SYSTEM_HEADER (type_decl) |
5888 | = DECL_IN_SYSTEM_HEADER (template); | |
66dfed4f | 5889 | if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type)) |
5890 | { | |
5891 | DECL_VISIBILITY_SPECIFIED (type_decl) = 1; | |
5892 | DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type); | |
5893 | } | |
0246130d | 5894 | |
6f0e36c6 | 5895 | /* Set up the template information. We have to figure out which |
5896 | template is the immediate parent if this is a full | |
5897 | instantiation. */ | |
5898 | if (parm_depth == 1 || is_partial_instantiation | |
5899 | || !PRIMARY_TEMPLATE_P (template)) | |
5900 | /* This case is easy; there are no member templates involved. */ | |
5901 | found = template; | |
5902 | else | |
5903 | { | |
5d5ece7c | 5904 | /* This is a full instantiation of a member template. Look |
5905 | for a partial instantiation of which this is an instance. */ | |
6f0e36c6 | 5906 | |
5907 | for (found = DECL_TEMPLATE_INSTANTIATIONS (template); | |
5908 | found; found = TREE_CHAIN (found)) | |
5909 | { | |
5910 | int success; | |
5911 | tree tmpl = CLASSTYPE_TI_TEMPLATE (TREE_VALUE (found)); | |
5912 | ||
5913 | /* We only want partial instantiations, here, not | |
5914 | specializations or full instantiations. */ | |
3a8f9e39 | 5915 | if (CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_VALUE (found)) |
6f0e36c6 | 5916 | || !uses_template_parms (TREE_VALUE (found))) |
5917 | continue; | |
5918 | ||
5919 | /* Temporarily reduce by one the number of levels in the | |
5920 | ARGLIST and in FOUND so as to avoid comparing the | |
5921 | last set of arguments. */ | |
5922 | TREE_VEC_LENGTH (arglist)--; | |
5923 | TREE_VEC_LENGTH (TREE_PURPOSE (found)) --; | |
5924 | ||
5925 | /* See if the arguments match. If they do, then TMPL is | |
5926 | the partial instantiation we want. */ | |
5927 | success = comp_template_args (TREE_PURPOSE (found), arglist); | |
5928 | ||
5929 | /* Restore the argument vectors to their full size. */ | |
5930 | TREE_VEC_LENGTH (arglist)++; | |
5931 | TREE_VEC_LENGTH (TREE_PURPOSE (found))++; | |
5932 | ||
5933 | if (success) | |
5934 | { | |
5935 | found = tmpl; | |
5936 | break; | |
5937 | } | |
5938 | } | |
5939 | ||
5940 | if (!found) | |
5d5ece7c | 5941 | { |
5942 | /* There was no partial instantiation. This happens | |
653e5405 | 5943 | where C<T> is a member template of A<T> and it's used |
5944 | in something like | |
9031d10b | 5945 | |
653e5405 | 5946 | template <typename T> struct B { A<T>::C<int> m; }; |
5947 | B<float>; | |
9031d10b | 5948 | |
653e5405 | 5949 | Create the partial instantiation. |
5950 | */ | |
5951 | TREE_VEC_LENGTH (arglist)--; | |
5952 | found = tsubst (template, arglist, complain, NULL_TREE); | |
5953 | TREE_VEC_LENGTH (arglist)++; | |
5954 | } | |
6f0e36c6 | 5955 | } |
5956 | ||
9031d10b | 5957 | SET_TYPE_TEMPLATE_INFO (t, tree_cons (found, arglist, NULL_TREE)); |
5958 | DECL_TEMPLATE_INSTANTIATIONS (template) | |
5959 | = tree_cons (arglist, t, | |
d91e3832 | 5960 | DECL_TEMPLATE_INSTANTIATIONS (template)); |
5961 | ||
9031d10b | 5962 | if (TREE_CODE (t) == ENUMERAL_TYPE |
d91e3832 | 5963 | && !is_partial_instantiation) |
0f2952a1 | 5964 | /* Now that the type has been registered on the instantiations |
5965 | list, we set up the enumerators. Because the enumeration | |
5966 | constants may involve the enumeration type itself, we make | |
5967 | sure to register the type first, and then create the | |
5968 | constants. That way, doing tsubst_expr for the enumeration | |
5969 | constants won't result in recursive calls here; we'll find | |
5970 | the instantiation and exit above. */ | |
5971 | tsubst_enum (template_type, t, arglist); | |
d91e3832 | 5972 | |
c79f94a2 | 5973 | if (is_partial_instantiation) |
5a5a82ac | 5974 | /* If the type makes use of template parameters, the |
5975 | code that generates debugging information will crash. */ | |
5976 | DECL_IGNORED_P (TYPE_STUB_DECL (t)) = 1; | |
471086d6 | 5977 | |
4a2849cb | 5978 | /* Possibly limit visibility based on template args. */ |
5979 | TREE_PUBLIC (type_decl) = 1; | |
5980 | determine_visibility (type_decl); | |
5981 | ||
a322b452 | 5982 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t); |
34197853 | 5983 | } |
a322b452 | 5984 | timevar_pop (TV_NAME_LOOKUP); |
471086d6 | 5985 | } |
5986 | \f | |
9031d10b | 5987 | struct pair_fn_data |
471086d6 | 5988 | { |
fe61b73e | 5989 | tree_fn_t fn; |
5990 | void *data; | |
6510331e | 5991 | /* True when we should also visit template parameters that occur in |
5992 | non-deduced contexts. */ | |
5993 | bool include_nondeduced_p; | |
c6224531 | 5994 | struct pointer_set_t *visited; |
fe61b73e | 5995 | }; |
5996 | ||
5997 | /* Called from for_each_template_parm via walk_tree. */ | |
a734686b | 5998 | |
fe61b73e | 5999 | static tree |
7dd37241 | 6000 | for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d) |
fe61b73e | 6001 | { |
6002 | tree t = *tp; | |
6003 | struct pair_fn_data *pfd = (struct pair_fn_data *) d; | |
6004 | tree_fn_t fn = pfd->fn; | |
6005 | void *data = pfd->data; | |
6b8feb95 | 6006 | |
9308e976 | 6007 | if (TYPE_P (t) |
6510331e | 6008 | && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE) |
6009 | && for_each_template_parm (TYPE_CONTEXT (t), fn, data, pfd->visited, | |
6010 | pfd->include_nondeduced_p)) | |
fe61b73e | 6011 | return error_mark_node; |
a734686b | 6012 | |
471086d6 | 6013 | switch (TREE_CODE (t)) |
6014 | { | |
471086d6 | 6015 | case RECORD_TYPE: |
1459abb8 | 6016 | if (TYPE_PTRMEMFUNC_P (t)) |
fe61b73e | 6017 | break; |
11fa0698 | 6018 | /* Fall through. */ |
6019 | ||
471086d6 | 6020 | case UNION_TYPE: |
11fa0698 | 6021 | case ENUMERAL_TYPE: |
fe61b73e | 6022 | if (!TYPE_TEMPLATE_INFO (t)) |
6023 | *walk_subtrees = 0; | |
6024 | else if (for_each_template_parm (TREE_VALUE (TYPE_TEMPLATE_INFO (t)), | |
6510331e | 6025 | fn, data, pfd->visited, |
6026 | pfd->include_nondeduced_p)) | |
fe61b73e | 6027 | return error_mark_node; |
6028 | break; | |
6029 | ||
89c5cfdc | 6030 | case INTEGER_TYPE: |
6031 | if (for_each_template_parm (TYPE_MIN_VALUE (t), | |
6510331e | 6032 | fn, data, pfd->visited, |
6033 | pfd->include_nondeduced_p) | |
89c5cfdc | 6034 | || for_each_template_parm (TYPE_MAX_VALUE (t), |
6510331e | 6035 | fn, data, pfd->visited, |
6036 | pfd->include_nondeduced_p)) | |
89c5cfdc | 6037 | return error_mark_node; |
6038 | break; | |
6039 | ||
fdb1094f | 6040 | case METHOD_TYPE: |
fe61b73e | 6041 | /* Since we're not going to walk subtrees, we have to do this |
6042 | explicitly here. */ | |
6b8feb95 | 6043 | if (for_each_template_parm (TYPE_METHOD_BASETYPE (t), fn, data, |
6510331e | 6044 | pfd->visited, pfd->include_nondeduced_p)) |
fe61b73e | 6045 | return error_mark_node; |
180f098b | 6046 | /* Fall through. */ |
fdb1094f | 6047 | |
6048 | case FUNCTION_TYPE: | |
fe61b73e | 6049 | /* Check the return type. */ |
6510331e | 6050 | if (for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited, |
6051 | pfd->include_nondeduced_p)) | |
fe61b73e | 6052 | return error_mark_node; |
6053 | ||
fdb1094f | 6054 | /* Check the parameter types. Since default arguments are not |
6055 | instantiated until they are needed, the TYPE_ARG_TYPES may | |
6056 | contain expressions that involve template parameters. But, | |
6057 | no-one should be looking at them yet. And, once they're | |
6058 | instantiated, they don't contain template parameters, so | |
6059 | there's no point in looking at them then, either. */ | |
6060 | { | |
6061 | tree parm; | |
6062 | ||
6063 | for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm)) | |
6b8feb95 | 6064 | if (for_each_template_parm (TREE_VALUE (parm), fn, data, |
6510331e | 6065 | pfd->visited, pfd->include_nondeduced_p)) |
fe61b73e | 6066 | return error_mark_node; |
e857e9c7 | 6067 | |
fe61b73e | 6068 | /* Since we've already handled the TYPE_ARG_TYPES, we don't |
6069 | want walk_tree walking into them itself. */ | |
6070 | *walk_subtrees = 0; | |
6071 | } | |
6072 | break; | |
1ca935fd | 6073 | |
0a3b29ad | 6074 | case TYPEOF_TYPE: |
6510331e | 6075 | if (pfd->include_nondeduced_p |
6076 | && for_each_template_parm (TYPE_FIELDS (t), fn, data, | |
6077 | pfd->visited, | |
6078 | pfd->include_nondeduced_p)) | |
0a3b29ad | 6079 | return error_mark_node; |
6080 | break; | |
6081 | ||
471086d6 | 6082 | case FUNCTION_DECL: |
e857e9c7 | 6083 | case VAR_DECL: |
e857e9c7 | 6084 | if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t) |
6b8feb95 | 6085 | && for_each_template_parm (DECL_TI_ARGS (t), fn, data, |
6510331e | 6086 | pfd->visited, pfd->include_nondeduced_p)) |
fe61b73e | 6087 | return error_mark_node; |
6088 | /* Fall through. */ | |
6089 | ||
471086d6 | 6090 | case PARM_DECL: |
0a3b29ad | 6091 | case CONST_DECL: |
6092 | if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t) | |
6093 | && for_each_template_parm (DECL_INITIAL (t), fn, data, | |
6510331e | 6094 | pfd->visited, pfd->include_nondeduced_p)) |
0a3b29ad | 6095 | return error_mark_node; |
9031d10b | 6096 | if (DECL_CONTEXT (t) |
6510331e | 6097 | && pfd->include_nondeduced_p |
6b8feb95 | 6098 | && for_each_template_parm (DECL_CONTEXT (t), fn, data, |
6510331e | 6099 | pfd->visited, pfd->include_nondeduced_p)) |
fe61b73e | 6100 | return error_mark_node; |
6101 | break; | |
471086d6 | 6102 | |
1e93ca27 | 6103 | case BOUND_TEMPLATE_TEMPLATE_PARM: |
9e5bd773 | 6104 | /* Record template parameters such as `T' inside `TT<T>'. */ |
6510331e | 6105 | if (for_each_template_parm (TYPE_TI_ARGS (t), fn, data, pfd->visited, |
6106 | pfd->include_nondeduced_p)) | |
fe61b73e | 6107 | return error_mark_node; |
6108 | /* Fall through. */ | |
6109 | ||
1e93ca27 | 6110 | case TEMPLATE_TEMPLATE_PARM: |
9e5bd773 | 6111 | case TEMPLATE_TYPE_PARM: |
f3110581 | 6112 | case TEMPLATE_PARM_INDEX: |
fe61b73e | 6113 | if (fn && (*fn)(t, data)) |
6114 | return error_mark_node; | |
6115 | else if (!fn) | |
6116 | return error_mark_node; | |
6117 | break; | |
3cc0b4b9 | 6118 | |
fe61b73e | 6119 | case TEMPLATE_DECL: |
331bc0ad | 6120 | /* A template template parameter is encountered. */ |
fe61b73e | 6121 | if (DECL_TEMPLATE_TEMPLATE_PARM_P (t) |
6510331e | 6122 | && for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited, |
6123 | pfd->include_nondeduced_p)) | |
fe61b73e | 6124 | return error_mark_node; |
c25194fd | 6125 | |
fe61b73e | 6126 | /* Already substituted template template parameter */ |
6127 | *walk_subtrees = 0; | |
6128 | break; | |
f51b72ed | 6129 | |
49c2d56b | 6130 | case TYPENAME_TYPE: |
9031d10b | 6131 | if (!fn |
6b8feb95 | 6132 | || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t), fn, |
6510331e | 6133 | data, pfd->visited, |
6134 | pfd->include_nondeduced_p)) | |
fe61b73e | 6135 | return error_mark_node; |
6136 | break; | |
49c2d56b | 6137 | |
fe61b73e | 6138 | case CONSTRUCTOR: |
6139 | if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t)) | |
6510331e | 6140 | && pfd->include_nondeduced_p |
fe61b73e | 6141 | && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE |
6b8feb95 | 6142 | (TREE_TYPE (t)), fn, data, |
6510331e | 6143 | pfd->visited, pfd->include_nondeduced_p)) |
fe61b73e | 6144 | return error_mark_node; |
6145 | break; | |
9031d10b | 6146 | |
f51b72ed | 6147 | case INDIRECT_REF: |
6148 | case COMPONENT_REF: | |
49c2d56b | 6149 | /* If there's no type, then this thing must be some expression |
f51b72ed | 6150 | involving template parameters. */ |
49c2d56b | 6151 | if (!fn && !TREE_TYPE (t)) |
fe61b73e | 6152 | return error_mark_node; |
6153 | break; | |
f51b72ed | 6154 | |
03db3ebd | 6155 | case MODOP_EXPR: |
6156 | case CAST_EXPR: | |
6157 | case REINTERPRET_CAST_EXPR: | |
6158 | case CONST_CAST_EXPR: | |
6159 | case STATIC_CAST_EXPR: | |
6160 | case DYNAMIC_CAST_EXPR: | |
03db3ebd | 6161 | case ARROW_EXPR: |
6162 | case DOTSTAR_EXPR: | |
6163 | case TYPEID_EXPR: | |
8318ad7a | 6164 | case PSEUDO_DTOR_EXPR: |
f51b72ed | 6165 | if (!fn) |
fe61b73e | 6166 | return error_mark_node; |
6167 | break; | |
dd53deb4 | 6168 | |
471086d6 | 6169 | default: |
fe61b73e | 6170 | break; |
471086d6 | 6171 | } |
fe61b73e | 6172 | |
6173 | /* We didn't find any template parameters we liked. */ | |
6174 | return NULL_TREE; | |
6175 | } | |
6176 | ||
9031d10b | 6177 | /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM, |
6178 | BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T, | |
1e93ca27 | 6179 | call FN with the parameter and the DATA. |
3160db1d | 6180 | If FN returns nonzero, the iteration is terminated, and |
fe61b73e | 6181 | for_each_template_parm returns 1. Otherwise, the iteration |
3160db1d | 6182 | continues. If FN never returns a nonzero value, the value |
fe61b73e | 6183 | returned by for_each_template_parm is 0. If FN is NULL, it is |
6510331e | 6184 | considered to be the function which always returns 1. |
6185 | ||
6186 | If INCLUDE_NONDEDUCED_P, then this routine will also visit template | |
6187 | parameters that occur in non-deduced contexts. When false, only | |
6188 | visits those template parameters that can be deduced. */ | |
fe61b73e | 6189 | |
6190 | static int | |
c6224531 | 6191 | for_each_template_parm (tree t, tree_fn_t fn, void* data, |
6510331e | 6192 | struct pointer_set_t *visited, |
6193 | bool include_nondeduced_p) | |
fe61b73e | 6194 | { |
6195 | struct pair_fn_data pfd; | |
719ba3d9 | 6196 | int result; |
fe61b73e | 6197 | |
6198 | /* Set up. */ | |
6199 | pfd.fn = fn; | |
6200 | pfd.data = data; | |
6510331e | 6201 | pfd.include_nondeduced_p = include_nondeduced_p; |
fe61b73e | 6202 | |
180f098b | 6203 | /* Walk the tree. (Conceptually, we would like to walk without |
6204 | duplicates, but for_each_template_parm_r recursively calls | |
6205 | for_each_template_parm, so we would need to reorganize a fair | |
6b8feb95 | 6206 | bit to use walk_tree_without_duplicates, so we keep our own |
6207 | visited list.) */ | |
6208 | if (visited) | |
6209 | pfd.visited = visited; | |
6210 | else | |
c6224531 | 6211 | pfd.visited = pointer_set_create (); |
20a8f962 | 6212 | result = cp_walk_tree (&t, |
6213 | for_each_template_parm_r, | |
6214 | &pfd, | |
6215 | pfd.visited) != NULL_TREE; | |
719ba3d9 | 6216 | |
6217 | /* Clean up. */ | |
6218 | if (!visited) | |
c6224531 | 6219 | { |
6220 | pointer_set_destroy (pfd.visited); | |
6221 | pfd.visited = 0; | |
6222 | } | |
719ba3d9 | 6223 | |
6224 | return result; | |
471086d6 | 6225 | } |
6226 | ||
7bdfc61c | 6227 | /* Returns true if T depends on any template parameter. */ |
6228 | ||
c906a2a7 | 6229 | int |
807be5b4 | 6230 | uses_template_parms (tree t) |
c906a2a7 | 6231 | { |
7f233616 | 6232 | bool dependent_p; |
6233 | int saved_processing_template_decl; | |
6234 | ||
6235 | saved_processing_template_decl = processing_template_decl; | |
6236 | if (!saved_processing_template_decl) | |
6237 | processing_template_decl = 1; | |
6238 | if (TYPE_P (t)) | |
6239 | dependent_p = dependent_type_p (t); | |
6240 | else if (TREE_CODE (t) == TREE_VEC) | |
6241 | dependent_p = any_dependent_template_arguments_p (t); | |
6242 | else if (TREE_CODE (t) == TREE_LIST) | |
6243 | dependent_p = (uses_template_parms (TREE_VALUE (t)) | |
6244 | || uses_template_parms (TREE_CHAIN (t))); | |
673b95fd | 6245 | else if (TREE_CODE (t) == TYPE_DECL) |
6246 | dependent_p = dependent_type_p (TREE_TYPE (t)); | |
9031d10b | 6247 | else if (DECL_P (t) |
6248 | || EXPR_P (t) | |
7f233616 | 6249 | || TREE_CODE (t) == TEMPLATE_PARM_INDEX |
6250 | || TREE_CODE (t) == OVERLOAD | |
6251 | || TREE_CODE (t) == BASELINK | |
09e79bdc | 6252 | || TREE_CODE (t) == IDENTIFIER_NODE |
481451eb | 6253 | || TREE_CODE (t) == TRAIT_EXPR |
ce45a448 | 6254 | || CONSTANT_CLASS_P (t)) |
7f233616 | 6255 | dependent_p = (type_dependent_expression_p (t) |
6256 | || value_dependent_expression_p (t)); | |
2e3e31d2 | 6257 | else |
6258 | { | |
6259 | gcc_assert (t == error_mark_node); | |
6260 | dependent_p = false; | |
6261 | } | |
9031d10b | 6262 | |
7f233616 | 6263 | processing_template_decl = saved_processing_template_decl; |
6264 | ||
6265 | return dependent_p; | |
c906a2a7 | 6266 | } |
6267 | ||
7bdfc61c | 6268 | /* Returns true if T depends on any template parameter with level LEVEL. */ |
6269 | ||
6270 | int | |
6271 | uses_template_parms_level (tree t, int level) | |
6272 | { | |
6510331e | 6273 | return for_each_template_parm (t, template_parm_this_level_p, &level, NULL, |
6274 | /*include_nondeduced_p=*/true); | |
7bdfc61c | 6275 | } |
6276 | ||
063e90ea | 6277 | static int tinst_depth; |
d6d05907 | 6278 | extern int max_tinst_depth; |
e857e9c7 | 6279 | #ifdef GATHER_STATISTICS |
063e90ea | 6280 | int depth_reached; |
e857e9c7 | 6281 | #endif |
fe61b73e | 6282 | static int tinst_level_tick; |
6283 | static int last_template_error_tick; | |
471086d6 | 6284 | |
04d89d04 | 6285 | /* We're starting to instantiate D; record the template instantiation context |
6286 | for diagnostics and to restore it later. */ | |
6287 | ||
66b60ad2 | 6288 | static int |
807be5b4 | 6289 | push_tinst_level (tree d) |
471086d6 | 6290 | { |
c5dd8e06 | 6291 | struct tinst_level *new; |
471086d6 | 6292 | |
dd2b9e17 | 6293 | if (tinst_depth >= max_tinst_depth) |
6294 | { | |
6e44a93b | 6295 | /* If the instantiation in question still has unbound template parms, |
6296 | we don't really care if we can't instantiate it, so just return. | |
653e5405 | 6297 | This happens with base instantiation for implicit `typename'. */ |
6e44a93b | 6298 | if (uses_template_parms (d)) |
6299 | return 0; | |
6300 | ||
999c9c6f | 6301 | last_template_error_tick = tinst_level_tick; |
39e7b4a4 | 6302 | error ("template instantiation depth exceeds maximum of %d (use " |
653e5405 | 6303 | "-ftemplate-depth-NN to increase the maximum) instantiating %qD", |
e77c6394 | 6304 | max_tinst_depth, d); |
e857e9c7 | 6305 | |
b97edf7e | 6306 | print_instantiation_context (); |
e857e9c7 | 6307 | |
dd2b9e17 | 6308 | return 0; |
6309 | } | |
6310 | ||
c5dd8e06 | 6311 | new = GGC_NEW (struct tinst_level); |
6312 | new->decl = d; | |
6313 | new->locus = input_location; | |
6314 | new->in_system_header_p = in_system_header; | |
6315 | new->next = current_tinst_level; | |
471086d6 | 6316 | current_tinst_level = new; |
e857e9c7 | 6317 | |
dd2b9e17 | 6318 | ++tinst_depth; |
e857e9c7 | 6319 | #ifdef GATHER_STATISTICS |
6320 | if (tinst_depth > depth_reached) | |
6321 | depth_reached = tinst_depth; | |
6322 | #endif | |
6323 | ||
063e90ea | 6324 | ++tinst_level_tick; |
dd2b9e17 | 6325 | return 1; |
471086d6 | 6326 | } |
6327 | ||
04d89d04 | 6328 | /* We're done instantiating this template; return to the instantiation |
6329 | context. */ | |
6330 | ||
66b60ad2 | 6331 | static void |
807be5b4 | 6332 | pop_tinst_level (void) |
471086d6 | 6333 | { |
4bac4c68 | 6334 | /* Restore the filename and line number stashed away when we started |
6335 | this instantiation. */ | |
c5dd8e06 | 6336 | input_location = current_tinst_level->locus; |
6337 | in_system_header = current_tinst_level->in_system_header_p; | |
6338 | current_tinst_level = current_tinst_level->next; | |
dd2b9e17 | 6339 | --tinst_depth; |
063e90ea | 6340 | ++tinst_level_tick; |
471086d6 | 6341 | } |
6342 | ||
04d89d04 | 6343 | /* We're instantiating a deferred template; restore the template |
6344 | instantiation context in which the instantiation was requested, which | |
c5dd8e06 | 6345 | is one step out from LEVEL. Return the corresponding DECL or TYPE. */ |
04d89d04 | 6346 | |
c5dd8e06 | 6347 | static tree |
6348 | reopen_tinst_level (struct tinst_level *level) | |
04d89d04 | 6349 | { |
c5dd8e06 | 6350 | struct tinst_level *t; |
04d89d04 | 6351 | |
6352 | tinst_depth = 0; | |
c5dd8e06 | 6353 | for (t = level; t; t = t->next) |
04d89d04 | 6354 | ++tinst_depth; |
6355 | ||
6356 | current_tinst_level = level; | |
6357 | pop_tinst_level (); | |
c5dd8e06 | 6358 | return level->decl; |
04d89d04 | 6359 | } |
6360 | ||
96cc8e6a | 6361 | /* Returns the TINST_LEVEL which gives the original instantiation |
6362 | context. */ | |
6363 | ||
c5dd8e06 | 6364 | struct tinst_level * |
96cc8e6a | 6365 | outermost_tinst_level (void) |
6366 | { | |
c5dd8e06 | 6367 | struct tinst_level *level = current_tinst_level; |
6368 | if (level) | |
6369 | while (level->next) | |
6370 | level = level->next; | |
6371 | return level; | |
96cc8e6a | 6372 | } |
6373 | ||
f3110581 | 6374 | /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the |
6375 | vector of template arguments, as for tsubst. | |
6376 | ||
ec0eee8d | 6377 | Returns an appropriate tsubst'd friend declaration. */ |
f3110581 | 6378 | |
6379 | static tree | |
807be5b4 | 6380 | tsubst_friend_function (tree decl, tree args) |
f3110581 | 6381 | { |
6382 | tree new_friend; | |
063e90ea | 6383 | |
9031d10b | 6384 | if (TREE_CODE (decl) == FUNCTION_DECL |
f3110581 | 6385 | && DECL_TEMPLATE_INSTANTIATION (decl) |
6386 | && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL) | |
6387 | /* This was a friend declared with an explicit template | |
6388 | argument list, e.g.: | |
9031d10b | 6389 | |
f3110581 | 6390 | friend void f<>(T); |
9031d10b | 6391 | |
f3110581 | 6392 | to indicate that f was a template instantiation, not a new |
6393 | function declaration. Now, we have to figure out what | |
6394 | instantiation of what template. */ | |
6395 | { | |
178ecb0c | 6396 | tree template_id, arglist, fns; |
f3110581 | 6397 | tree new_args; |
6398 | tree tmpl; | |
9416e3c9 | 6399 | tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type)); |
9031d10b | 6400 | |
178ecb0c | 6401 | /* Friend functions are looked up in the containing namespace scope. |
653e5405 | 6402 | We must enter that scope, to avoid finding member functions of the |
6403 | current cless with same name. */ | |
178ecb0c | 6404 | push_nested_namespace (ns); |
6405 | fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args, | |
bde9ebf7 | 6406 | tf_warning_or_error, NULL_TREE, |
6407 | /*integral_constant_expression_p=*/false); | |
178ecb0c | 6408 | pop_nested_namespace (ns); |
6409 | arglist = tsubst (DECL_TI_ARGS (decl), args, | |
0fbca5e8 | 6410 | tf_warning_or_error, NULL_TREE); |
178ecb0c | 6411 | template_id = lookup_template_function (fns, arglist); |
9031d10b | 6412 | |
0fbca5e8 | 6413 | new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE); |
34197853 | 6414 | tmpl = determine_specialization (template_id, new_friend, |
9031d10b | 6415 | &new_args, |
05a677c7 | 6416 | /*need_member_template=*/0, |
5e302b95 | 6417 | TREE_VEC_LENGTH (args), |
6418 | tsk_none); | |
4c97972b | 6419 | return instantiate_template (tmpl, new_args, tf_error); |
f3110581 | 6420 | } |
34197853 | 6421 | |
0fbca5e8 | 6422 | new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE); |
9031d10b | 6423 | |
34197853 | 6424 | /* The NEW_FRIEND will look like an instantiation, to the |
f3110581 | 6425 | compiler, but is not an instantiation from the point of view of |
6426 | the language. For example, we might have had: | |
9031d10b | 6427 | |
f3110581 | 6428 | template <class T> struct S { |
6429 | template <class U> friend void f(T, U); | |
6430 | }; | |
9031d10b | 6431 | |
f3110581 | 6432 | Then, in S<int>, template <class U> void f(int, U) is not an |
6433 | instantiation of anything. */ | |
08a54795 | 6434 | if (new_friend == error_mark_node) |
6435 | return error_mark_node; | |
9031d10b | 6436 | |
f3110581 | 6437 | DECL_USE_TEMPLATE (new_friend) = 0; |
6438 | if (TREE_CODE (decl) == TEMPLATE_DECL) | |
b81be439 | 6439 | { |
6440 | DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0; | |
6441 | DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend)) | |
6442 | = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl)); | |
6443 | } | |
34197853 | 6444 | |
7e64c604 | 6445 | /* The mangled name for the NEW_FRIEND is incorrect. The function |
6446 | is not a template instantiation and should not be mangled like | |
6447 | one. Therefore, we forget the mangling here; we'll recompute it | |
6448 | later if we need it. */ | |
34197853 | 6449 | if (TREE_CODE (new_friend) != TEMPLATE_DECL) |
6450 | { | |
0e8e37b2 | 6451 | SET_DECL_RTL (new_friend, NULL_RTX); |
7e64c604 | 6452 | SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE); |
34197853 | 6453 | } |
9031d10b | 6454 | |
ddb39453 | 6455 | if (DECL_NAMESPACE_SCOPE_P (new_friend)) |
f3110581 | 6456 | { |
34197853 | 6457 | tree old_decl; |
4012c20a | 6458 | tree new_friend_template_info; |
6459 | tree new_friend_result_template_info; | |
83cf81a0 | 6460 | tree ns; |
4012c20a | 6461 | int new_friend_is_defn; |
6462 | ||
6463 | /* We must save some information from NEW_FRIEND before calling | |
6464 | duplicate decls since that function will free NEW_FRIEND if | |
6465 | possible. */ | |
6466 | new_friend_template_info = DECL_TEMPLATE_INFO (new_friend); | |
1611df57 | 6467 | new_friend_is_defn = |
9031d10b | 6468 | (DECL_INITIAL (DECL_TEMPLATE_RESULT |
1611df57 | 6469 | (template_for_substitution (new_friend))) |
6470 | != NULL_TREE); | |
f3110581 | 6471 | if (TREE_CODE (new_friend) == TEMPLATE_DECL) |
4012c20a | 6472 | { |
6473 | /* This declaration is a `primary' template. */ | |
6474 | DECL_PRIMARY_TEMPLATE (new_friend) = new_friend; | |
9031d10b | 6475 | |
4012c20a | 6476 | new_friend_result_template_info |
b8e0d419 | 6477 | = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend)); |
4012c20a | 6478 | } |
6479 | else | |
1611df57 | 6480 | new_friend_result_template_info = NULL_TREE; |
34197853 | 6481 | |
4a132a96 | 6482 | /* Make the init_value nonzero so pushdecl knows this is a defn. */ |
6483 | if (new_friend_is_defn) | |
6484 | DECL_INITIAL (new_friend) = error_mark_node; | |
6485 | ||
671116bf | 6486 | /* Inside pushdecl_namespace_level, we will push into the |
6487 | current namespace. However, the friend function should go | |
47cd6605 | 6488 | into the namespace of the template. */ |
83cf81a0 | 6489 | ns = decl_namespace_context (new_friend); |
6490 | push_nested_namespace (ns); | |
c1d4295f | 6491 | old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true); |
83cf81a0 | 6492 | pop_nested_namespace (ns); |
34197853 | 6493 | |
7ec3a70c | 6494 | if (old_decl == error_mark_node) |
6495 | return error_mark_node; | |
6496 | ||
34197853 | 6497 | if (old_decl != new_friend) |
6498 | { | |
6499 | /* This new friend declaration matched an existing | |
6500 | declaration. For example, given: | |
6501 | ||
6502 | template <class T> void f(T); | |
9031d10b | 6503 | template <class U> class C { |
6504 | template <class T> friend void f(T) {} | |
34197853 | 6505 | }; |
6506 | ||
6507 | the friend declaration actually provides the definition | |
6508 | of `f', once C has been instantiated for some type. So, | |
6509 | old_decl will be the out-of-class template declaration, | |
6510 | while new_friend is the in-class definition. | |
6511 | ||
6512 | But, if `f' was called before this point, the | |
6513 | instantiation of `f' will have DECL_TI_ARGS corresponding | |
6514 | to `T' but not to `U', references to which might appear | |
6515 | in the definition of `f'. Previously, the most general | |
6516 | template for an instantiation of `f' was the out-of-class | |
6517 | version; now it is the in-class version. Therefore, we | |
6518 | run through all specialization of `f', adding to their | |
6519 | DECL_TI_ARGS appropriately. In particular, they need a | |
6520 | new set of outer arguments, corresponding to the | |
9031d10b | 6521 | arguments for this class instantiation. |
34197853 | 6522 | |
6523 | The same situation can arise with something like this: | |
6524 | ||
6525 | friend void f(int); | |
9031d10b | 6526 | template <class T> class C { |
653e5405 | 6527 | friend void f(T) {} |
6528 | }; | |
34197853 | 6529 | |
6530 | when `C<int>' is instantiated. Now, `f(int)' is defined | |
6531 | in the class. */ | |
6532 | ||
4012c20a | 6533 | if (!new_friend_is_defn) |
6534 | /* On the other hand, if the in-class declaration does | |
6535 | *not* provide a definition, then we don't want to alter | |
6536 | existing definitions. We can just leave everything | |
6537 | alone. */ | |
34197853 | 6538 | ; |
4012c20a | 6539 | else |
34197853 | 6540 | { |
4012c20a | 6541 | /* Overwrite whatever template info was there before, if |
6542 | any, with the new template information pertaining to | |
6543 | the declaration. */ | |
6544 | DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info; | |
6545 | ||
6546 | if (TREE_CODE (old_decl) != TEMPLATE_DECL) | |
59b4cc29 | 6547 | reregister_specialization (new_friend, |
6548 | most_general_template (old_decl), | |
6549 | old_decl); | |
9031d10b | 6550 | else |
34197853 | 6551 | { |
4012c20a | 6552 | tree t; |
6553 | tree new_friend_args; | |
6554 | ||
9031d10b | 6555 | DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl)) |
4012c20a | 6556 | = new_friend_result_template_info; |
9031d10b | 6557 | |
4012c20a | 6558 | new_friend_args = TI_ARGS (new_friend_template_info); |
9031d10b | 6559 | for (t = DECL_TEMPLATE_SPECIALIZATIONS (old_decl); |
4012c20a | 6560 | t != NULL_TREE; |
6561 | t = TREE_CHAIN (t)) | |
6562 | { | |
6563 | tree spec = TREE_VALUE (t); | |
9031d10b | 6564 | |
6565 | DECL_TI_ARGS (spec) | |
4012c20a | 6566 | = add_outermost_template_args (new_friend_args, |
6567 | DECL_TI_ARGS (spec)); | |
4012c20a | 6568 | } |
6569 | ||
6570 | /* Now, since specializations are always supposed to | |
6571 | hang off of the most general template, we must move | |
6572 | them. */ | |
6573 | t = most_general_template (old_decl); | |
6574 | if (t != old_decl) | |
6575 | { | |
6576 | DECL_TEMPLATE_SPECIALIZATIONS (t) | |
6577 | = chainon (DECL_TEMPLATE_SPECIALIZATIONS (t), | |
6578 | DECL_TEMPLATE_SPECIALIZATIONS (old_decl)); | |
6579 | DECL_TEMPLATE_SPECIALIZATIONS (old_decl) = NULL_TREE; | |
6580 | } | |
34197853 | 6581 | } |
6582 | } | |
6583 | ||
6584 | /* The information from NEW_FRIEND has been merged into OLD_DECL | |
6585 | by duplicate_decls. */ | |
6586 | new_friend = old_decl; | |
6587 | } | |
f3110581 | 6588 | } |
4c97972b | 6589 | else |
f3110581 | 6590 | { |
4c97972b | 6591 | tree context = DECL_CONTEXT (new_friend); |
6592 | bool dependent_p; | |
6593 | ||
6594 | /* In the code | |
6595 | template <class T> class C { | |
6596 | template <class U> friend void C1<U>::f (); // case 1 | |
6597 | friend void C2<T>::f (); // case 2 | |
6598 | }; | |
6599 | we only need to make sure CONTEXT is a complete type for | |
6600 | case 2. To distinguish between the two cases, we note that | |
6601 | CONTEXT of case 1 remains dependent type after tsubst while | |
6602 | this isn't true for case 2. */ | |
6603 | ++processing_template_decl; | |
6604 | dependent_p = dependent_type_p (context); | |
6605 | --processing_template_decl; | |
6606 | ||
6607 | if (!dependent_p | |
6608 | && !complete_type_or_else (context, NULL_TREE)) | |
6609 | return error_mark_node; | |
6610 | ||
6611 | if (COMPLETE_TYPE_P (context)) | |
6612 | { | |
6613 | /* Check to see that the declaration is really present, and, | |
6614 | possibly obtain an improved declaration. */ | |
6615 | tree fn = check_classfn (context, | |
6616 | new_friend, NULL_TREE); | |
6617 | ||
a86a1d8b | 6618 | if (fn) |
4c97972b | 6619 | new_friend = fn; |
6620 | } | |
f3110581 | 6621 | } |
6622 | ||
6623 | return new_friend; | |
6624 | } | |
6625 | ||
f3906343 | 6626 | /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of |
6627 | template arguments, as for tsubst. | |
0f0d5a69 | 6628 | |
ec0eee8d | 6629 | Returns an appropriate tsubst'd friend type or error_mark_node on |
eccec853 | 6630 | failure. */ |
0f0d5a69 | 6631 | |
6632 | static tree | |
807be5b4 | 6633 | tsubst_friend_class (tree friend_tmpl, tree args) |
0f0d5a69 | 6634 | { |
f3906343 | 6635 | tree friend_type; |
162ed5b9 | 6636 | tree tmpl; |
652f5812 | 6637 | tree context; |
0f0d5a69 | 6638 | |
652f5812 | 6639 | context = DECL_CONTEXT (friend_tmpl); |
6640 | ||
6641 | if (context) | |
13ddc993 | 6642 | { |
652f5812 | 6643 | if (TREE_CODE (context) == NAMESPACE_DECL) |
6644 | push_nested_namespace (context); | |
6645 | else | |
9031d10b | 6646 | push_nested_class (tsubst (context, args, tf_none, NULL_TREE)); |
652f5812 | 6647 | } |
162ed5b9 | 6648 | |
1fb8fb56 | 6649 | /* Look for a class template declaration. We look for hidden names |
6650 | because two friend declarations of the same template are the | |
6651 | same. For example, in: | |
6652 | ||
6653 | struct A { | |
6654 | template <typename> friend class F; | |
6655 | }; | |
6656 | template <typename> struct B { | |
6657 | template <typename> friend class F; | |
6658 | }; | |
6659 | ||
6660 | both F templates are the same. */ | |
6661 | tmpl = lookup_name_real (DECL_NAME (friend_tmpl), 0, 0, | |
6662 | /*block_p=*/true, 0, | |
6663 | LOOKUP_COMPLAIN | LOOKUP_HIDDEN); | |
162ed5b9 | 6664 | |
652f5812 | 6665 | /* But, if we don't find one, it might be because we're in a |
6666 | situation like this: | |
13ddc993 | 6667 | |
652f5812 | 6668 | template <class T> |
6669 | struct S { | |
6670 | template <class U> | |
6671 | friend struct S; | |
6672 | }; | |
6673 | ||
6674 | Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL | |
6675 | for `S<int>', not the TEMPLATE_DECL. */ | |
6676 | if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl)) | |
6677 | { | |
d1c41717 | 6678 | tmpl = lookup_name_prefer_type (DECL_NAME (friend_tmpl), 1); |
652f5812 | 6679 | tmpl = maybe_get_template_decl_from_type_decl (tmpl); |
162ed5b9 | 6680 | } |
0f0d5a69 | 6681 | |
162ed5b9 | 6682 | if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl)) |
0f0d5a69 | 6683 | { |
6684 | /* The friend template has already been declared. Just | |
34197853 | 6685 | check to see that the declarations match, and install any new |
6686 | default parameters. We must tsubst the default parameters, | |
6687 | of course. We only need the innermost template parameters | |
6688 | because that is all that redeclare_class_template will look | |
6689 | at. */ | |
652f5812 | 6690 | if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl)) |
6691 | > TMPL_ARGS_DEPTH (args)) | |
6692 | { | |
6693 | tree parms; | |
40db0c7c | 6694 | location_t saved_input_location; |
652f5812 | 6695 | parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl), |
0fbca5e8 | 6696 | args, tf_warning_or_error); |
40db0c7c | 6697 | |
6698 | saved_input_location = input_location; | |
6699 | input_location = DECL_SOURCE_LOCATION (friend_tmpl); | |
652f5812 | 6700 | redeclare_class_template (TREE_TYPE (tmpl), parms); |
40db0c7c | 6701 | input_location = saved_input_location; |
6702 | ||
652f5812 | 6703 | } |
6704 | ||
0f0d5a69 | 6705 | friend_type = TREE_TYPE (tmpl); |
6706 | } | |
6707 | else | |
6708 | { | |
6709 | /* The friend template has not already been declared. In this | |
6710 | case, the instantiation of the template class will cause the | |
6711 | injection of this template into the global scope. */ | |
0fbca5e8 | 6712 | tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE); |
32c92b57 | 6713 | if (tmpl == error_mark_node) |
6714 | return error_mark_node; | |
0f0d5a69 | 6715 | |
6716 | /* The new TMPL is not an instantiation of anything, so we | |
653e5405 | 6717 | forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for |
0f0d5a69 | 6718 | the new type because that is supposed to be the corresponding |
6719 | template decl, i.e., TMPL. */ | |
6720 | DECL_USE_TEMPLATE (tmpl) = 0; | |
6721 | DECL_TEMPLATE_INFO (tmpl) = NULL_TREE; | |
6722 | CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0; | |
566cd7af | 6723 | CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)) |
6724 | = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))); | |
0f0d5a69 | 6725 | |
6726 | /* Inject this template into the global scope. */ | |
c1d4295f | 6727 | friend_type = TREE_TYPE (pushdecl_top_level_maybe_friend (tmpl, true)); |
0f0d5a69 | 6728 | } |
6729 | ||
9031d10b | 6730 | if (context) |
652f5812 | 6731 | { |
6732 | if (TREE_CODE (context) == NAMESPACE_DECL) | |
6733 | pop_nested_namespace (context); | |
6734 | else | |
6735 | pop_nested_class (); | |
6736 | } | |
6737 | ||
0f0d5a69 | 6738 | return friend_type; |
6739 | } | |
f3110581 | 6740 | |
e1c962d0 | 6741 | /* Returns zero if TYPE cannot be completed later due to circularity. |
6742 | Otherwise returns one. */ | |
6743 | ||
1b800449 | 6744 | static int |
807be5b4 | 6745 | can_complete_type_without_circularity (tree type) |
e1c962d0 | 6746 | { |
6747 | if (type == NULL_TREE || type == error_mark_node) | |
6748 | return 0; | |
6749 | else if (COMPLETE_TYPE_P (type)) | |
6750 | return 1; | |
6751 | else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type)) | |
6752 | return can_complete_type_without_circularity (TREE_TYPE (type)); | |
b9f26971 | 6753 | else if (CLASS_TYPE_P (type) |
6754 | && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type))) | |
e1c962d0 | 6755 | return 0; |
6756 | else | |
6757 | return 1; | |
6758 | } | |
6759 | ||
531be878 | 6760 | /* Apply any attributes which had to be deferred until instantiation |
6761 | time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes; | |
6762 | ARGS, COMPLAIN, IN_DECL are as tsubst. */ | |
6763 | ||
6764 | static void | |
6765 | apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags, | |
6766 | tree args, tsubst_flags_t complain, tree in_decl) | |
6767 | { | |
f00ae06e | 6768 | tree last_dep = NULL_TREE; |
531be878 | 6769 | tree t; |
f00ae06e | 6770 | tree *p; |
6771 | ||
6772 | for (t = attributes; t; t = TREE_CHAIN (t)) | |
6773 | if (ATTR_IS_DEPENDENT (t)) | |
6774 | { | |
6775 | last_dep = t; | |
6776 | attributes = copy_list (attributes); | |
6777 | break; | |
6778 | } | |
531be878 | 6779 | |
6780 | if (DECL_P (*decl_p)) | |
92208374 | 6781 | { |
6782 | if (TREE_TYPE (*decl_p) == error_mark_node) | |
6783 | return; | |
6784 | p = &DECL_ATTRIBUTES (*decl_p); | |
6785 | } | |
531be878 | 6786 | else |
f00ae06e | 6787 | p = &TYPE_ATTRIBUTES (*decl_p); |
531be878 | 6788 | |
f00ae06e | 6789 | if (last_dep) |
6790 | { | |
6791 | tree late_attrs = NULL_TREE; | |
6792 | tree *q = &late_attrs; | |
6793 | ||
6794 | for (*p = attributes; *p; ) | |
6795 | { | |
6796 | t = *p; | |
6797 | if (ATTR_IS_DEPENDENT (t)) | |
6798 | { | |
6799 | *p = TREE_CHAIN (t); | |
6800 | TREE_CHAIN (t) = NULL_TREE; | |
b8ba9d95 | 6801 | /* If the first attribute argument is an identifier, don't |
6802 | pass it through tsubst. Attributes like mode, format, | |
6803 | cleanup and several target specific attributes expect it | |
6804 | unmodified. */ | |
6805 | if (TREE_VALUE (t) | |
6806 | && TREE_CODE (TREE_VALUE (t)) == TREE_LIST | |
6807 | && TREE_VALUE (TREE_VALUE (t)) | |
6808 | && (TREE_CODE (TREE_VALUE (TREE_VALUE (t))) | |
6809 | == IDENTIFIER_NODE)) | |
6810 | { | |
6811 | tree chain | |
6812 | = tsubst_expr (TREE_CHAIN (TREE_VALUE (t)), args, complain, | |
6813 | in_decl, | |
6814 | /*integral_constant_expression_p=*/false); | |
6815 | if (chain != TREE_CHAIN (TREE_VALUE (t))) | |
6816 | TREE_VALUE (t) | |
6817 | = tree_cons (NULL_TREE, TREE_VALUE (TREE_VALUE (t)), | |
6818 | chain); | |
6819 | } | |
6820 | else | |
6821 | TREE_VALUE (t) | |
6822 | = tsubst_expr (TREE_VALUE (t), args, complain, in_decl, | |
6823 | /*integral_constant_expression_p=*/false); | |
f00ae06e | 6824 | *q = t; |
6825 | q = &TREE_CHAIN (t); | |
6826 | } | |
6827 | else | |
6828 | p = &TREE_CHAIN (t); | |
6829 | } | |
531be878 | 6830 | |
f00ae06e | 6831 | cplus_decl_attributes (decl_p, late_attrs, attr_flags); |
6832 | } | |
531be878 | 6833 | } |
6834 | ||
471086d6 | 6835 | tree |
807be5b4 | 6836 | instantiate_class_template (tree type) |
471086d6 | 6837 | { |
7a40f9b7 | 6838 | tree template, args, pattern, t, member; |
34197853 | 6839 | tree typedecl; |
95f3173a | 6840 | tree pbinfo; |
a6460bf1 | 6841 | tree base_list; |
9031d10b | 6842 | |
e857e9c7 | 6843 | if (type == error_mark_node) |
471086d6 | 6844 | return error_mark_node; |
6845 | ||
9031d10b | 6846 | if (TYPE_BEING_DEFINED (type) |
5c6faf71 | 6847 | || COMPLETE_TYPE_P (type) |
6848 | || dependent_type_p (type)) | |
e857e9c7 | 6849 | return type; |
6850 | ||
ae883b79 | 6851 | /* Figure out which template is being instantiated. */ |
34197853 | 6852 | template = most_general_template (CLASSTYPE_TI_TEMPLATE (type)); |
b4df430b | 6853 | gcc_assert (TREE_CODE (template) == TEMPLATE_DECL); |
d3ce0f51 | 6854 | |
fb868a5d | 6855 | /* Determine what specialization of the original template to |
6856 | instantiate. */ | |
aa32d258 | 6857 | t = most_specialized_class (type, template); |
954ad420 | 6858 | if (t == error_mark_node) |
d3ce0f51 | 6859 | { |
954ad420 | 6860 | TYPE_BEING_DEFINED (type) = 1; |
6861 | return error_mark_node; | |
d3ce0f51 | 6862 | } |
aa32d258 | 6863 | else if (t) |
6864 | { | |
6865 | /* This TYPE is actually an instantiation of a partial | |
6866 | specialization. We replace the innermost set of ARGS with | |
6867 | the arguments appropriate for substitution. For example, | |
6868 | given: | |
074ab442 | 6869 | |
6870 | template <class T> struct S {}; | |
aa32d258 | 6871 | template <class T> struct S<T*> {}; |
ae883b79 | 6872 | |
aa32d258 | 6873 | and supposing that we are instantiating S<int*>, ARGS will |
074ab442 | 6874 | presently be {int*} -- but we need {int}. */ |
aa32d258 | 6875 | pattern = TREE_TYPE (t); |
6876 | args = TREE_PURPOSE (t); | |
6877 | } | |
d3ce0f51 | 6878 | else |
aa32d258 | 6879 | { |
6880 | pattern = TREE_TYPE (template); | |
6881 | args = CLASSTYPE_TI_ARGS (type); | |
6882 | } | |
e857e9c7 | 6883 | |
fb868a5d | 6884 | /* If the template we're instantiating is incomplete, then clearly |
6885 | there's nothing we can do. */ | |
4b72716d | 6886 | if (!COMPLETE_TYPE_P (pattern)) |
19c3b3a6 | 6887 | return type; |
e857e9c7 | 6888 | |
fb868a5d | 6889 | /* If we've recursively instantiated too many templates, stop. */ |
6890 | if (! push_tinst_level (type)) | |
19c3b3a6 | 6891 | return type; |
fb868a5d | 6892 | |
6893 | /* Now we're really doing the instantiation. Mark the type as in | |
6894 | the process of being defined. */ | |
6895 | TYPE_BEING_DEFINED (type) = 1; | |
6896 | ||
4cab8273 | 6897 | /* We may be in the middle of deferred access check. Disable |
6898 | it now. */ | |
6899 | push_deferring_access_checks (dk_no_deferred); | |
6900 | ||
7f233616 | 6901 | push_to_top_level (); |
fb868a5d | 6902 | |
c79f94a2 | 6903 | SET_CLASSTYPE_INTERFACE_UNKNOWN (type); |
471086d6 | 6904 | |
821209e0 | 6905 | /* Set the input location to the template definition. This is needed |
6906 | if tsubsting causes an error. */ | |
8f3b5fc9 | 6907 | typedecl = TYPE_MAIN_DECL (type); |
6908 | input_location = DECL_SOURCE_LOCATION (typedecl); | |
6909 | in_system_header = DECL_IN_SYSTEM_HEADER (typedecl); | |
821209e0 | 6910 | |
930e8175 | 6911 | TYPE_HAS_USER_CONSTRUCTOR (type) = TYPE_HAS_USER_CONSTRUCTOR (pattern); |
89e923d8 | 6912 | TYPE_HAS_NEW_OPERATOR (type) = TYPE_HAS_NEW_OPERATOR (pattern); |
6913 | TYPE_HAS_ARRAY_NEW_OPERATOR (type) = TYPE_HAS_ARRAY_NEW_OPERATOR (pattern); | |
eaf26d83 | 6914 | TYPE_GETS_DELETE (type) = TYPE_GETS_DELETE (pattern); |
eaf26d83 | 6915 | TYPE_HAS_ASSIGN_REF (type) = TYPE_HAS_ASSIGN_REF (pattern); |
6916 | TYPE_HAS_CONST_ASSIGN_REF (type) = TYPE_HAS_CONST_ASSIGN_REF (pattern); | |
eaf26d83 | 6917 | TYPE_HAS_INIT_REF (type) = TYPE_HAS_INIT_REF (pattern); |
6918 | TYPE_HAS_CONST_INIT_REF (type) = TYPE_HAS_CONST_INIT_REF (pattern); | |
eaf26d83 | 6919 | TYPE_HAS_DEFAULT_CONSTRUCTOR (type) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern); |
6920 | TYPE_HAS_CONVERSION (type) = TYPE_HAS_CONVERSION (pattern); | |
eaf26d83 | 6921 | TYPE_PACKED (type) = TYPE_PACKED (pattern); |
6922 | TYPE_ALIGN (type) = TYPE_ALIGN (pattern); | |
aca14577 | 6923 | TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern); |
7d53e30d | 6924 | TYPE_FOR_JAVA (type) = TYPE_FOR_JAVA (pattern); /* For libjava's JArray<T> */ |
128e1d72 | 6925 | if (ANON_AGGR_TYPE_P (pattern)) |
6926 | SET_ANON_AGGR_TYPE_P (type); | |
66dfed4f | 6927 | if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern)) |
6928 | { | |
6929 | CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1; | |
6930 | CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern); | |
6931 | } | |
eaf26d83 | 6932 | |
95f3173a | 6933 | pbinfo = TYPE_BINFO (pattern); |
d7e38994 | 6934 | |
2e3e31d2 | 6935 | /* We should never instantiate a nested class before its enclosing |
6936 | class; we need to look up the nested class by name before we can | |
6937 | instantiate it, and that lookup should instantiate the enclosing | |
6938 | class. */ | |
6939 | gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern)) | |
6940 | || COMPLETE_TYPE_P (TYPE_CONTEXT (type)) | |
6941 | || TYPE_BEING_DEFINED (TYPE_CONTEXT (type))); | |
d7e38994 | 6942 | |
a6460bf1 | 6943 | base_list = NULL_TREE; |
f6cc6a08 | 6944 | if (BINFO_N_BASE_BINFOS (pbinfo)) |
d1aed66b | 6945 | { |
f6cc6a08 | 6946 | tree pbase_binfo; |
8de4bdf4 | 6947 | tree context = TYPE_CONTEXT (type); |
7f602bca | 6948 | tree pushed_scope; |
d1aed66b | 6949 | int i; |
e857e9c7 | 6950 | |
8de4bdf4 | 6951 | /* We must enter the scope containing the type, as that is where |
6952 | the accessibility of types named in dependent bases are | |
6953 | looked up from. */ | |
7f602bca | 6954 | pushed_scope = push_scope (context ? context : global_namespace); |
9031d10b | 6955 | |
d1aed66b | 6956 | /* Substitute into each of the bases to determine the actual |
6957 | basetypes. */ | |
f6cc6a08 | 6958 | for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++) |
d1aed66b | 6959 | { |
6960 | tree base; | |
f6cc6a08 | 6961 | tree access = BINFO_BASE_ACCESS (pbinfo, i); |
d95d815d | 6962 | tree expanded_bases = NULL_TREE; |
6963 | int idx, len = 1; | |
6964 | ||
6965 | if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo))) | |
6966 | { | |
6967 | expanded_bases = | |
6968 | tsubst_pack_expansion (BINFO_TYPE (pbase_binfo), | |
6969 | args, tf_error, NULL_TREE); | |
6970 | if (expanded_bases == error_mark_node) | |
6971 | continue; | |
6972 | ||
6973 | len = TREE_VEC_LENGTH (expanded_bases); | |
6974 | } | |
6975 | ||
6976 | for (idx = 0; idx < len; idx++) | |
6977 | { | |
6978 | if (expanded_bases) | |
6979 | /* Extract the already-expanded base class. */ | |
6980 | base = TREE_VEC_ELT (expanded_bases, idx); | |
6981 | else | |
6982 | /* Substitute to figure out the base class. */ | |
6983 | base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error, | |
6984 | NULL_TREE); | |
6985 | ||
6986 | if (base == error_mark_node) | |
6987 | continue; | |
6988 | ||
6989 | base_list = tree_cons (access, base, base_list); | |
6990 | if (BINFO_VIRTUAL_P (pbase_binfo)) | |
6991 | TREE_TYPE (base_list) = integer_type_node; | |
6992 | } | |
d1aed66b | 6993 | } |
79076e97 | 6994 | |
d1aed66b | 6995 | /* The list is now in reverse order; correct that. */ |
6996 | base_list = nreverse (base_list); | |
6997 | ||
7f602bca | 6998 | if (pushed_scope) |
6999 | pop_scope (pushed_scope); | |
d1aed66b | 7000 | } |
a6460bf1 | 7001 | /* Now call xref_basetypes to set up all the base-class |
7002 | information. */ | |
7003 | xref_basetypes (type, base_list); | |
7004 | ||
531be878 | 7005 | apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern), |
7006 | (int) ATTR_FLAG_TYPE_IN_PLACE, | |
7007 | args, tf_error, NULL_TREE); | |
e857e9c7 | 7008 | |
8fc54128 | 7009 | /* Now that our base classes are set up, enter the scope of the |
7010 | class, so that name lookups into base classes, etc. will work | |
ec0eee8d | 7011 | correctly. This is precisely analogous to what we do in |
8fc54128 | 7012 | begin_class_definition when defining an ordinary non-template |
d718e1bc | 7013 | class, except we also need to push the enclosing classes. */ |
7014 | push_nested_class (type); | |
8fc54128 | 7015 | |
7a40f9b7 | 7016 | /* Now members are processed in the order of declaration. */ |
b9f26971 | 7017 | for (member = CLASSTYPE_DECL_LIST (pattern); |
7018 | member; member = TREE_CHAIN (member)) | |
471086d6 | 7019 | { |
7a40f9b7 | 7020 | tree t = TREE_VALUE (member); |
e857e9c7 | 7021 | |
7a40f9b7 | 7022 | if (TREE_PURPOSE (member)) |
11fa0698 | 7023 | { |
7a40f9b7 | 7024 | if (TYPE_P (t)) |
7025 | { | |
af694375 | 7026 | /* Build new CLASSTYPE_NESTED_UTDS. */ |
471086d6 | 7027 | |
7a40f9b7 | 7028 | tree newtag; |
0cb38f3e | 7029 | bool class_template_p; |
7030 | ||
db729ece | 7031 | class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE |
7032 | && TYPE_LANG_SPECIFIC (t) | |
7033 | && CLASSTYPE_IS_TEMPLATE (t)); | |
0cb38f3e | 7034 | /* If the member is a class template, then -- even after |
071129a1 | 7035 | substitution -- there may be dependent types in the |
0cb38f3e | 7036 | template argument list for the class. We increment |
7037 | PROCESSING_TEMPLATE_DECL so that dependent_type_p, as | |
7038 | that function will assume that no types are dependent | |
7039 | when outside of a template. */ | |
7040 | if (class_template_p) | |
7041 | ++processing_template_decl; | |
db729ece | 7042 | newtag = tsubst (t, args, tf_error, NULL_TREE); |
0cb38f3e | 7043 | if (class_template_p) |
7044 | --processing_template_decl; | |
5d6080a0 | 7045 | if (newtag == error_mark_node) |
7046 | continue; | |
7047 | ||
7a40f9b7 | 7048 | if (TREE_CODE (newtag) != ENUMERAL_TYPE) |
7049 | { | |
db729ece | 7050 | tree name = TYPE_IDENTIFIER (t); |
7051 | ||
0cb38f3e | 7052 | if (class_template_p) |
7a40f9b7 | 7053 | /* Unfortunately, lookup_template_class sets |
7054 | CLASSTYPE_IMPLICIT_INSTANTIATION for a partial | |
a4ad28aa | 7055 | instantiation (i.e., for the type of a member |
7056 | template class nested within a template class.) | |
7057 | This behavior is required for | |
7058 | maybe_process_partial_specialization to work | |
7059 | correctly, but is not accurate in this case; | |
7060 | the TAG is not an instantiation of anything. | |
7061 | (The corresponding TEMPLATE_DECL is an | |
7062 | instantiation, but the TYPE is not.) */ | |
7a40f9b7 | 7063 | CLASSTYPE_USE_TEMPLATE (newtag) = 0; |
7064 | ||
7065 | /* Now, we call pushtag to put this NEWTAG into the scope of | |
7066 | TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid | |
7067 | pushtag calling push_template_decl. We don't have to do | |
7068 | this for enums because it will already have been done in | |
7069 | tsubst_enum. */ | |
7070 | if (name) | |
7071 | SET_IDENTIFIER_TYPE_VALUE (name, newtag); | |
3f3fa556 | 7072 | pushtag (name, newtag, /*tag_scope=*/ts_current); |
7a40f9b7 | 7073 | } |
7074 | } | |
9031d10b | 7075 | else if (TREE_CODE (t) == FUNCTION_DECL |
7a40f9b7 | 7076 | || DECL_FUNCTION_TEMPLATE_P (t)) |
7077 | { | |
7078 | /* Build new TYPE_METHODS. */ | |
796d0fa7 | 7079 | tree r; |
9031d10b | 7080 | |
796d0fa7 | 7081 | if (TREE_CODE (t) == TEMPLATE_DECL) |
c04baa53 | 7082 | ++processing_template_decl; |
796d0fa7 | 7083 | r = tsubst (t, args, tf_error, NULL_TREE); |
7084 | if (TREE_CODE (t) == TEMPLATE_DECL) | |
c04baa53 | 7085 | --processing_template_decl; |
7a40f9b7 | 7086 | set_current_access_from_decl (r); |
7a40f9b7 | 7087 | finish_member_declaration (r); |
7088 | } | |
7089 | else | |
7090 | { | |
7091 | /* Build new TYPE_FIELDS. */ | |
7a05c4b1 | 7092 | if (TREE_CODE (t) == STATIC_ASSERT) |
7093 | { | |
7094 | tree condition = | |
7095 | tsubst_expr (STATIC_ASSERT_CONDITION (t), args, | |
7096 | tf_warning_or_error, NULL_TREE, | |
7097 | /*integral_constant_expression_p=*/true); | |
7098 | finish_static_assert (condition, | |
7099 | STATIC_ASSERT_MESSAGE (t), | |
7100 | STATIC_ASSERT_SOURCE_LOCATION (t), | |
7101 | /*member_p=*/true); | |
7102 | } | |
7103 | else if (TREE_CODE (t) != CONST_DECL) | |
7a40f9b7 | 7104 | { |
7105 | tree r; | |
2789f91b | 7106 | |
fa70df70 | 7107 | /* The the file and line for this declaration, to |
7108 | assist in error message reporting. Since we | |
7109 | called push_tinst_level above, we don't need to | |
7110 | restore these. */ | |
346064d9 | 7111 | input_location = DECL_SOURCE_LOCATION (t); |
2789f91b | 7112 | |
e01414c4 | 7113 | if (TREE_CODE (t) == TEMPLATE_DECL) |
c04baa53 | 7114 | ++processing_template_decl; |
0fbca5e8 | 7115 | r = tsubst (t, args, tf_warning_or_error, NULL_TREE); |
e01414c4 | 7116 | if (TREE_CODE (t) == TEMPLATE_DECL) |
c04baa53 | 7117 | --processing_template_decl; |
7a40f9b7 | 7118 | if (TREE_CODE (r) == VAR_DECL) |
7119 | { | |
e6ef0e42 | 7120 | /* In [temp.inst]: |
7121 | ||
7122 | [t]he initialization (and any associated | |
7123 | side-effects) of a static data member does | |
7124 | not occur unless the static data member is | |
7125 | itself used in a way that requires the | |
7126 | definition of the static data member to | |
074ab442 | 7127 | exist. |
e6ef0e42 | 7128 | |
7129 | Therefore, we do not substitute into the | |
074ab442 | 7130 | initialized for the static data member here. */ |
7131 | finish_static_data_member_decl | |
7132 | (r, | |
7133 | /*init=*/NULL_TREE, | |
d91303a6 | 7134 | /*init_const_expr_p=*/false, |
074ab442 | 7135 | /*asmspec_tree=*/NULL_TREE, |
e6ef0e42 | 7136 | /*flags=*/0); |
7a40f9b7 | 7137 | if (DECL_INITIALIZED_IN_CLASS_P (r)) |
7138 | check_static_variable_definition (r, TREE_TYPE (r)); | |
7139 | } | |
7140 | else if (TREE_CODE (r) == FIELD_DECL) | |
7141 | { | |
7142 | /* Determine whether R has a valid type and can be | |
7143 | completed later. If R is invalid, then it is | |
7144 | replaced by error_mark_node so that it will not be | |
7145 | added to TYPE_FIELDS. */ | |
7146 | tree rtype = TREE_TYPE (r); | |
7147 | if (can_complete_type_without_circularity (rtype)) | |
7148 | complete_type (rtype); | |
7149 | ||
7150 | if (!COMPLETE_TYPE_P (rtype)) | |
7151 | { | |
7152 | cxx_incomplete_type_error (r, rtype); | |
653e5405 | 7153 | r = error_mark_node; |
7a40f9b7 | 7154 | } |
7155 | } | |
e857e9c7 | 7156 | |
7a40f9b7 | 7157 | /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE, |
7158 | such a thing will already have been added to the field | |
7159 | list by tsubst_enum in finish_member_declaration in the | |
af694375 | 7160 | CLASSTYPE_NESTED_UTDS case above. */ |
7a40f9b7 | 7161 | if (!(TREE_CODE (r) == TYPE_DECL |
7162 | && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE | |
9a9dd49f | 7163 | && DECL_ARTIFICIAL (r))) |
7a40f9b7 | 7164 | { |
7165 | set_current_access_from_decl (r); | |
7166 | finish_member_declaration (r); | |
7167 | } | |
653e5405 | 7168 | } |
7a40f9b7 | 7169 | } |
623cc2d2 | 7170 | } |
7a40f9b7 | 7171 | else |
7172 | { | |
7173 | if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)) | |
7174 | { | |
7175 | /* Build new CLASSTYPE_FRIEND_CLASSES. */ | |
7176 | ||
7177 | tree friend_type = t; | |
f95fba26 | 7178 | bool adjust_processing_template_decl = false; |
f3906343 | 7179 | |
7a40f9b7 | 7180 | if (TREE_CODE (friend_type) == TEMPLATE_DECL) |
f95fba26 | 7181 | { |
f4f11c20 | 7182 | /* template <class T> friend class C; */ |
f95fba26 | 7183 | friend_type = tsubst_friend_class (friend_type, args); |
653e5405 | 7184 | adjust_processing_template_decl = true; |
f95fba26 | 7185 | } |
7186 | else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE) | |
7187 | { | |
f4f11c20 | 7188 | /* template <class T> friend class C::D; */ |
f95fba26 | 7189 | friend_type = tsubst (friend_type, args, |
0fbca5e8 | 7190 | tf_warning_or_error, NULL_TREE); |
f95fba26 | 7191 | if (TREE_CODE (friend_type) == TEMPLATE_DECL) |
7192 | friend_type = TREE_TYPE (friend_type); | |
653e5405 | 7193 | adjust_processing_template_decl = true; |
f95fba26 | 7194 | } |
7195 | else if (TREE_CODE (friend_type) == TYPENAME_TYPE) | |
7196 | { | |
f4f11c20 | 7197 | /* This could be either |
7198 | ||
7199 | friend class T::C; | |
7200 | ||
7201 | when dependent_type_p is false or | |
7202 | ||
7203 | template <class U> friend class T::C; | |
7204 | ||
7205 | otherwise. */ | |
f95fba26 | 7206 | friend_type = tsubst (friend_type, args, |
0fbca5e8 | 7207 | tf_warning_or_error, NULL_TREE); |
f95fba26 | 7208 | /* Bump processing_template_decl for correct |
7209 | dependent_type_p calculation. */ | |
7210 | ++processing_template_decl; | |
7211 | if (dependent_type_p (friend_type)) | |
7212 | adjust_processing_template_decl = true; | |
7213 | --processing_template_decl; | |
7214 | } | |
f4f11c20 | 7215 | else if (!CLASSTYPE_USE_TEMPLATE (friend_type) |
7216 | && hidden_name_p (TYPE_NAME (friend_type))) | |
7a40f9b7 | 7217 | { |
f4f11c20 | 7218 | /* friend class C; |
7219 | ||
7220 | where C hasn't been declared yet. Let's lookup name | |
7221 | from namespace scope directly, bypassing any name that | |
7222 | come from dependent base class. */ | |
7a40f9b7 | 7223 | tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type)); |
7224 | ||
7225 | /* The call to xref_tag_from_type does injection for friend | |
7226 | classes. */ | |
7227 | push_nested_namespace (ns); | |
9031d10b | 7228 | friend_type = |
7229 | xref_tag_from_type (friend_type, NULL_TREE, | |
f4f11c20 | 7230 | /*tag_scope=*/ts_current); |
7a40f9b7 | 7231 | pop_nested_namespace (ns); |
7232 | } | |
f4f11c20 | 7233 | else if (uses_template_parms (friend_type)) |
7234 | /* friend class C<T>; */ | |
7235 | friend_type = tsubst (friend_type, args, | |
0fbca5e8 | 7236 | tf_warning_or_error, NULL_TREE); |
f4f11c20 | 7237 | /* Otherwise it's |
7238 | ||
7239 | friend class C; | |
7240 | ||
7241 | where C is already declared or | |
7242 | ||
7243 | friend class C<int>; | |
7244 | ||
653e5405 | 7245 | We don't have to do anything in these cases. */ |
f3906343 | 7246 | |
f95fba26 | 7247 | if (adjust_processing_template_decl) |
7a40f9b7 | 7248 | /* Trick make_friend_class into realizing that the friend |
7249 | we're adding is a template, not an ordinary class. It's | |
7250 | important that we use make_friend_class since it will | |
7251 | perform some error-checking and output cross-reference | |
7252 | information. */ | |
7253 | ++processing_template_decl; | |
d2a15a12 | 7254 | |
f95fba26 | 7255 | if (friend_type != error_mark_node) |
653e5405 | 7256 | make_friend_class (type, friend_type, /*complain=*/false); |
d2a15a12 | 7257 | |
f95fba26 | 7258 | if (adjust_processing_template_decl) |
7a40f9b7 | 7259 | --processing_template_decl; |
7260 | } | |
7261 | else | |
c04baa53 | 7262 | { |
7263 | /* Build new DECL_FRIENDLIST. */ | |
7264 | tree r; | |
7265 | ||
4c97972b | 7266 | /* The the file and line for this declaration, to |
7267 | assist in error message reporting. Since we | |
7268 | called push_tinst_level above, we don't need to | |
7269 | restore these. */ | |
7270 | input_location = DECL_SOURCE_LOCATION (t); | |
7271 | ||
c04baa53 | 7272 | if (TREE_CODE (t) == TEMPLATE_DECL) |
a6460bf1 | 7273 | { |
7274 | ++processing_template_decl; | |
7275 | push_deferring_access_checks (dk_no_check); | |
7276 | } | |
9031d10b | 7277 | |
c04baa53 | 7278 | r = tsubst_friend_function (t, args); |
c04baa53 | 7279 | add_friend (type, r, /*complain=*/false); |
a6460bf1 | 7280 | if (TREE_CODE (t) == TEMPLATE_DECL) |
7281 | { | |
7282 | pop_deferring_access_checks (); | |
7283 | --processing_template_decl; | |
7284 | } | |
c04baa53 | 7285 | } |
7a40f9b7 | 7286 | } |
7287 | } | |
e857e9c7 | 7288 | |
0f2952a1 | 7289 | /* Set the file and line number information to whatever is given for |
7290 | the class itself. This puts error messages involving generated | |
7291 | implicit functions at a predictable point, and the same point | |
7292 | that would be used for non-template classes. */ | |
346064d9 | 7293 | input_location = DECL_SOURCE_LOCATION (typedecl); |
4ee9c684 | 7294 | |
0f2952a1 | 7295 | unreverse_member_declarations (type); |
175a96e8 | 7296 | finish_struct_1 (type); |
d1c45f5d | 7297 | TYPE_BEING_DEFINED (type) = 0; |
471086d6 | 7298 | |
70a658bd | 7299 | /* Now that the class is complete, instantiate default arguments for |
7300 | any member functions. We don't do this earlier because the | |
7301 | default arguments may reference members of the class. */ | |
7302 | if (!PRIMARY_TEMPLATE_P (template)) | |
7303 | for (t = TYPE_METHODS (type); t; t = TREE_CHAIN (t)) | |
9031d10b | 7304 | if (TREE_CODE (t) == FUNCTION_DECL |
ec0eee8d | 7305 | /* Implicitly generated member functions will not have template |
70a658bd | 7306 | information; they are not instantiations, but instead are |
7307 | created "fresh" for each instantiation. */ | |
7308 | && DECL_TEMPLATE_INFO (t)) | |
7309 | tsubst_default_arguments (t); | |
7310 | ||
d718e1bc | 7311 | pop_nested_class (); |
e857e9c7 | 7312 | pop_from_top_level (); |
4cab8273 | 7313 | pop_deferring_access_checks (); |
e857e9c7 | 7314 | pop_tinst_level (); |
7315 | ||
caa6fdce | 7316 | /* The vtable for a template class can be emitted in any translation |
7317 | unit in which the class is instantiated. When there is no key | |
7318 | method, however, finish_struct_1 will already have added TYPE to | |
7319 | the keyed_classes list. */ | |
7320 | if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type)) | |
9bacae7e | 7321 | keyed_classes = tree_cons (NULL_TREE, type, keyed_classes); |
7322 | ||
e857e9c7 | 7323 | return type; |
471086d6 | 7324 | } |
7325 | ||
73645519 | 7326 | static tree |
3fa5de5b | 7327 | tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl) |
73645519 | 7328 | { |
3fa5de5b | 7329 | tree r; |
9031d10b | 7330 | |
3fa5de5b | 7331 | if (!t) |
7332 | r = t; | |
7333 | else if (TYPE_P (t)) | |
7334 | r = tsubst (t, args, complain, in_decl); | |
7335 | else | |
73645519 | 7336 | { |
bde9ebf7 | 7337 | r = tsubst_expr (t, args, complain, in_decl, |
7338 | /*integral_constant_expression_p=*/true); | |
3b087710 | 7339 | r = fold_non_dependent_expr (r); |
7aa978b3 | 7340 | } |
d95d815d | 7341 | return r; |
7342 | } | |
7343 | ||
7344 | /* Substitute ARGS into T, which is an pack expansion | |
7345 | (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a | |
7346 | TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node | |
7347 | (if only a partial substitution could be performed) or | |
7348 | ERROR_MARK_NODE if there was an error. */ | |
7349 | tree | |
7350 | tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain, | |
7351 | tree in_decl) | |
7352 | { | |
7353 | tree pattern; | |
7354 | tree pack, packs = NULL_TREE, unsubstituted_packs = NULL_TREE; | |
7355 | tree first_arg_pack; int i, len = -1; | |
7356 | tree result; | |
7357 | int incomplete = 0; | |
7358 | ||
7359 | gcc_assert (PACK_EXPANSION_P (t)); | |
7360 | pattern = PACK_EXPANSION_PATTERN (t); | |
7361 | ||
7362 | /* Determine the argument packs that will instantiate the parameter | |
7363 | packs used in the expansion expression. While we're at it, | |
7364 | compute the number of arguments to be expanded and make sure it | |
7365 | is consistent. */ | |
7366 | for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack; | |
7367 | pack = TREE_CHAIN (pack)) | |
7368 | { | |
7369 | tree parm_pack = TREE_VALUE (pack); | |
7370 | tree arg_pack = NULL_TREE; | |
7371 | tree orig_arg = NULL_TREE; | |
7372 | ||
7373 | if (TREE_CODE (parm_pack) == PARM_DECL) | |
f8ee651e | 7374 | arg_pack = retrieve_local_specialization (parm_pack); |
d95d815d | 7375 | else |
7376 | { | |
7377 | int level, idx, levels; | |
7378 | template_parm_level_and_index (parm_pack, &level, &idx); | |
7379 | ||
7380 | levels = TMPL_ARGS_DEPTH (args); | |
7381 | if (level <= levels) | |
7382 | arg_pack = TMPL_ARG (args, level, idx); | |
7383 | } | |
7384 | ||
7385 | orig_arg = arg_pack; | |
7386 | if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT) | |
7387 | arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack); | |
7388 | ||
2aedc2ff | 7389 | if (arg_pack && !ARGUMENT_PACK_P (arg_pack)) |
7390 | /* This can only happen if we forget to expand an argument | |
7391 | pack somewhere else. Just return an error, silently. */ | |
7392 | { | |
7393 | result = make_tree_vec (1); | |
7394 | TREE_VEC_ELT (result, 0) = error_mark_node; | |
7395 | return result; | |
7396 | } | |
7397 | ||
08b4cffd | 7398 | if (arg_pack |
7399 | && TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack)) == 1 | |
7400 | && PACK_EXPANSION_P (TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg_pack), 0))) | |
7401 | { | |
7402 | tree expansion = TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg_pack), 0); | |
7403 | tree pattern = PACK_EXPANSION_PATTERN (expansion); | |
7404 | if ((TYPE_P (pattern) && same_type_p (pattern, parm_pack)) | |
7405 | || (!TYPE_P (pattern) && cp_tree_equal (parm_pack, pattern))) | |
7406 | /* The argument pack that the parameter maps to is just an | |
7407 | expansion of the parameter itself, such as one would | |
7408 | find in the implicit typedef of a class inside the | |
7409 | class itself. Consider this parameter "unsubstituted", | |
7410 | so that we will maintain the outer pack expansion. */ | |
7411 | arg_pack = NULL_TREE; | |
7412 | } | |
7413 | ||
d95d815d | 7414 | if (arg_pack) |
7415 | { | |
7416 | int my_len = | |
7417 | TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack)); | |
7418 | ||
7419 | /* It's all-or-nothing with incomplete argument packs. */ | |
7420 | if (incomplete && !ARGUMENT_PACK_INCOMPLETE_P (arg_pack)) | |
7421 | return error_mark_node; | |
7422 | ||
7423 | if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack)) | |
7424 | incomplete = 1; | |
7425 | ||
7426 | if (len < 0) | |
7427 | { | |
7428 | len = my_len; | |
7429 | first_arg_pack = arg_pack; | |
7430 | } | |
7431 | else if (len != my_len) | |
7432 | { | |
7433 | if (TREE_CODE (t) == TYPE_PACK_EXPANSION) | |
7434 | error ("mismatched argument pack lengths while expanding " | |
7435 | "%<%T%>", | |
7436 | pattern); | |
7437 | else | |
7438 | error ("mismatched argument pack lengths while expanding " | |
7439 | "%<%E%>", | |
7440 | pattern); | |
7441 | return error_mark_node; | |
7442 | } | |
7443 | ||
7444 | /* Keep track of the parameter packs and their corresponding | |
7445 | argument packs. */ | |
7446 | packs = tree_cons (parm_pack, arg_pack, packs); | |
7447 | TREE_TYPE (packs) = orig_arg; | |
7448 | } | |
7449 | else | |
7450 | /* We can't substitute for this parameter pack. */ | |
7451 | unsubstituted_packs = tree_cons (TREE_PURPOSE (pack), | |
7452 | TREE_VALUE (pack), | |
7453 | unsubstituted_packs); | |
7454 | } | |
7455 | ||
7456 | /* We cannot expand this expansion expression, because we don't have | |
7457 | all of the argument packs we need. Substitute into the pattern | |
7458 | and return a PACK_EXPANSION_*. The caller will need to deal with | |
7459 | that. */ | |
7460 | if (unsubstituted_packs) | |
7461 | return make_pack_expansion (tsubst (pattern, args, complain, | |
7462 | in_decl)); | |
7463 | ||
7464 | /* We could not find any argument packs that work. */ | |
7465 | if (len < 0) | |
7466 | return error_mark_node; | |
7467 | ||
7468 | /* For each argument in each argument pack, substitute into the | |
7469 | pattern. */ | |
7470 | result = make_tree_vec (len + incomplete); | |
7471 | for (i = 0; i < len + incomplete; ++i) | |
7472 | { | |
7473 | /* For parameter pack, change the substitution of the parameter | |
7474 | pack to the ith argument in its argument pack, then expand | |
7475 | the pattern. */ | |
7476 | for (pack = packs; pack; pack = TREE_CHAIN (pack)) | |
7477 | { | |
7478 | tree parm = TREE_PURPOSE (pack); | |
7479 | ||
7480 | if (TREE_CODE (parm) == PARM_DECL) | |
7481 | { | |
7482 | /* Select the Ith argument from the pack. */ | |
7483 | tree arg = make_node (ARGUMENT_PACK_SELECT); | |
7484 | ARGUMENT_PACK_SELECT_FROM_PACK (arg) = TREE_VALUE (pack); | |
7485 | ARGUMENT_PACK_SELECT_INDEX (arg) = i; | |
7486 | mark_used (parm); | |
7487 | register_local_specialization (arg, parm); | |
7488 | } | |
7489 | else | |
7490 | { | |
7491 | tree value = parm; | |
7492 | int idx, level; | |
7493 | template_parm_level_and_index (parm, &level, &idx); | |
7494 | ||
7495 | if (i < len) | |
7496 | { | |
7497 | /* Select the Ith argument from the pack. */ | |
7498 | value = make_node (ARGUMENT_PACK_SELECT); | |
7499 | ARGUMENT_PACK_SELECT_FROM_PACK (value) = TREE_VALUE (pack); | |
7500 | ARGUMENT_PACK_SELECT_INDEX (value) = i; | |
7501 | } | |
7502 | ||
7503 | /* Update the corresponding argument. */ | |
7504 | TMPL_ARG (args, level, idx) = value; | |
7505 | } | |
7506 | } | |
7507 | ||
7508 | /* Substitute into the PATTERN with the altered arguments. */ | |
7509 | if (TREE_CODE (t) == EXPR_PACK_EXPANSION) | |
7510 | TREE_VEC_ELT (result, i) = | |
7511 | tsubst_expr (pattern, args, complain, in_decl, | |
7512 | /*integral_constant_expression_p=*/false); | |
7513 | else | |
7514 | TREE_VEC_ELT (result, i) = tsubst (pattern, args, complain, in_decl); | |
7515 | ||
7516 | if (i == len) | |
7517 | /* When we have incomplete argument packs, the last "expanded" | |
7518 | result is itself a pack expansion, which allows us | |
7519 | to deduce more arguments. */ | |
7520 | TREE_VEC_ELT (result, i) = | |
7521 | make_pack_expansion (TREE_VEC_ELT (result, i)); | |
7522 | ||
7523 | if (TREE_VEC_ELT (result, i) == error_mark_node) | |
7524 | { | |
7525 | result = error_mark_node; | |
7526 | break; | |
7527 | } | |
7528 | } | |
7529 | ||
7530 | /* Update ARGS to restore the substitution from parameter packs to | |
7531 | their argument packs. */ | |
7532 | for (pack = packs; pack; pack = TREE_CHAIN (pack)) | |
7533 | { | |
7534 | tree parm = TREE_PURPOSE (pack); | |
7535 | ||
7536 | if (TREE_CODE (parm) == PARM_DECL) | |
7537 | register_local_specialization (TREE_TYPE (pack), parm); | |
7538 | else | |
7539 | { | |
7540 | int idx, level; | |
7541 | template_parm_level_and_index (parm, &level, &idx); | |
7542 | ||
7543 | /* Update the corresponding argument. */ | |
7544 | if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args)) | |
7545 | TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) = | |
7546 | TREE_TYPE (pack); | |
7547 | else | |
7548 | TREE_VEC_ELT (args, idx) = TREE_TYPE (pack); | |
7549 | } | |
7550 | } | |
7551 | ||
7552 | return result; | |
7aa978b3 | 7553 | } |
7554 | ||
3fa5de5b | 7555 | /* Substitute ARGS into the vector or list of template arguments T. */ |
32b5f03e | 7556 | |
863c3f96 | 7557 | static tree |
3fa5de5b | 7558 | tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl) |
32b5f03e | 7559 | { |
d95d815d | 7560 | tree orig_t = t; |
b5959ba9 | 7561 | int len = TREE_VEC_LENGTH (t); |
d95d815d | 7562 | int need_new = 0, i, expanded_len_adjust = 0, out; |
cc52f165 | 7563 | tree *elts = (tree *) alloca (len * sizeof (tree)); |
9031d10b | 7564 | |
32b5f03e | 7565 | for (i = 0; i < len; i++) |
7566 | { | |
b5959ba9 | 7567 | tree orig_arg = TREE_VEC_ELT (t, i); |
7568 | tree new_arg; | |
3fa5de5b | 7569 | |
b5959ba9 | 7570 | if (TREE_CODE (orig_arg) == TREE_VEC) |
7571 | new_arg = tsubst_template_args (orig_arg, args, complain, in_decl); | |
d95d815d | 7572 | else if (PACK_EXPANSION_P (orig_arg)) |
7573 | { | |
7574 | /* Substitute into an expansion expression. */ | |
7575 | new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl); | |
7576 | ||
7577 | if (TREE_CODE (new_arg) == TREE_VEC) | |
7578 | /* Add to the expanded length adjustment the number of | |
7579 | expanded arguments. We subtract one from this | |
7580 | measurement, because the argument pack expression | |
7581 | itself is already counted as 1 in | |
7582 | LEN. EXPANDED_LEN_ADJUST can actually be negative, if | |
7583 | the argument pack is empty. */ | |
7584 | expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1; | |
7585 | } | |
7586 | else if (ARGUMENT_PACK_P (orig_arg)) | |
7587 | { | |
7588 | /* Substitute into each of the arguments. */ | |
7589 | new_arg = make_node (TREE_CODE (orig_arg)); | |
7590 | ||
7591 | SET_ARGUMENT_PACK_ARGS ( | |
7592 | new_arg, | |
7593 | tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg), | |
7594 | args, complain, in_decl)); | |
7595 | ||
7596 | if (ARGUMENT_PACK_ARGS (new_arg) == error_mark_node) | |
7597 | new_arg = error_mark_node; | |
7598 | ||
7599 | if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK) { | |
7600 | TREE_TYPE (new_arg) = tsubst (TREE_TYPE (orig_arg), args, | |
7601 | complain, in_decl); | |
7602 | TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg); | |
7603 | ||
7604 | if (TREE_TYPE (new_arg) == error_mark_node) | |
7605 | new_arg = error_mark_node; | |
7606 | } | |
7607 | } | |
32b5f03e | 7608 | else |
3fa5de5b | 7609 | new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl); |
9031d10b | 7610 | |
3fa5de5b | 7611 | if (new_arg == error_mark_node) |
0d4a9e94 | 7612 | return error_mark_node; |
7613 | ||
3fa5de5b | 7614 | elts[i] = new_arg; |
7615 | if (new_arg != orig_arg) | |
32b5f03e | 7616 | need_new = 1; |
7617 | } | |
9031d10b | 7618 | |
32b5f03e | 7619 | if (!need_new) |
7620 | return t; | |
3fa5de5b | 7621 | |
d95d815d | 7622 | /* Make space for the expanded arguments coming from template |
7623 | argument packs. */ | |
7624 | t = make_tree_vec (len + expanded_len_adjust); | |
7625 | for (i = 0, out = 0; i < len; i++) | |
7626 | { | |
7627 | if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i)) | |
7628 | || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i))) | |
7629 | && TREE_CODE (elts[i]) == TREE_VEC) | |
7630 | { | |
7631 | int idx; | |
7632 | ||
7633 | /* Now expand the template argument pack "in place". */ | |
7634 | for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++) | |
7635 | TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx); | |
7636 | } | |
7637 | else | |
7638 | { | |
7639 | TREE_VEC_ELT (t, out) = elts[i]; | |
7640 | out++; | |
7641 | } | |
7642 | } | |
9031d10b | 7643 | |
32b5f03e | 7644 | return t; |
7645 | } | |
7646 | ||
34197853 | 7647 | /* Return the result of substituting ARGS into the template parameters |
7648 | given by PARMS. If there are m levels of ARGS and m + n levels of | |
7649 | PARMS, then the result will contain n levels of PARMS. For | |
7650 | example, if PARMS is `template <class T> template <class U> | |
7651 | template <T*, U, class V>' and ARGS is {{int}, {double}} then the | |
7652 | result will be `template <int*, double, class V>'. */ | |
7653 | ||
863c3f96 | 7654 | static tree |
807be5b4 | 7655 | tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain) |
34197853 | 7656 | { |
ddea175e | 7657 | tree r = NULL_TREE; |
7658 | tree* new_parms; | |
34197853 | 7659 | |
f3d3cc67 | 7660 | /* When substituting into a template, we must set |
7661 | PROCESSING_TEMPLATE_DECL as the template parameters may be | |
7662 | dependent if they are based on one-another, and the dependency | |
7663 | predicates are short-circuit outside of templates. */ | |
7664 | ++processing_template_decl; | |
7665 | ||
34197853 | 7666 | for (new_parms = &r; |
7667 | TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args); | |
7668 | new_parms = &(TREE_CHAIN (*new_parms)), | |
7669 | parms = TREE_CHAIN (parms)) | |
7670 | { | |
9031d10b | 7671 | tree new_vec = |
34197853 | 7672 | make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms))); |
7673 | int i; | |
9031d10b | 7674 | |
34197853 | 7675 | for (i = 0; i < TREE_VEC_LENGTH (new_vec); ++i) |
7676 | { | |
508c7aa7 | 7677 | tree tuple; |
7678 | tree default_value; | |
7679 | tree parm_decl; | |
7680 | ||
7681 | if (parms == error_mark_node) | |
7682 | continue; | |
7683 | ||
7684 | tuple = TREE_VEC_ELT (TREE_VALUE (parms), i); | |
7685 | ||
7686 | if (tuple == error_mark_node) | |
7687 | continue; | |
7688 | ||
7689 | default_value = TREE_PURPOSE (tuple); | |
7690 | parm_decl = TREE_VALUE (tuple); | |
1f062702 | 7691 | |
7692 | parm_decl = tsubst (parm_decl, args, complain, NULL_TREE); | |
b67dd653 | 7693 | if (TREE_CODE (parm_decl) == PARM_DECL |
7694 | && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain)) | |
7695 | parm_decl = error_mark_node; | |
3fa5de5b | 7696 | default_value = tsubst_template_arg (default_value, args, |
7697 | complain, NULL_TREE); | |
9031d10b | 7698 | |
3fa5de5b | 7699 | tuple = build_tree_list (default_value, parm_decl); |
1f062702 | 7700 | TREE_VEC_ELT (new_vec, i) = tuple; |
34197853 | 7701 | } |
9031d10b | 7702 | |
7703 | *new_parms = | |
7704 | tree_cons (size_int (TMPL_PARMS_DEPTH (parms) | |
180f098b | 7705 | - TMPL_ARGS_DEPTH (args)), |
34197853 | 7706 | new_vec, NULL_TREE); |
7707 | } | |
7708 | ||
f3d3cc67 | 7709 | --processing_template_decl; |
7710 | ||
34197853 | 7711 | return r; |
7712 | } | |
7713 | ||
11fa0698 | 7714 | /* Substitute the ARGS into the indicated aggregate (or enumeration) |
7715 | type T. If T is not an aggregate or enumeration type, it is | |
7716 | handled as if by tsubst. IN_DECL is as for tsubst. If | |
3160db1d | 7717 | ENTERING_SCOPE is nonzero, T is the context for a template which |
ec0eee8d | 7718 | we are presently tsubst'ing. Return the substituted value. */ |
34197853 | 7719 | |
863c3f96 | 7720 | static tree |
9031d10b | 7721 | tsubst_aggr_type (tree t, |
653e5405 | 7722 | tree args, |
7723 | tsubst_flags_t complain, | |
7724 | tree in_decl, | |
7725 | int entering_scope) | |
34197853 | 7726 | { |
7727 | if (t == NULL_TREE) | |
7728 | return NULL_TREE; | |
7729 | ||
7730 | switch (TREE_CODE (t)) | |
7731 | { | |
7732 | case RECORD_TYPE: | |
7733 | if (TYPE_PTRMEMFUNC_P (t)) | |
1c4208ef | 7734 | return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl); |
34197853 | 7735 | |
331bc0ad | 7736 | /* Else fall through. */ |
11fa0698 | 7737 | case ENUMERAL_TYPE: |
34197853 | 7738 | case UNION_TYPE: |
510501ab | 7739 | if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t)) |
34197853 | 7740 | { |
7741 | tree argvec; | |
7742 | tree context; | |
7743 | tree r; | |
5c626400 | 7744 | bool saved_skip_evaluation; |
7745 | ||
7746 | /* In "sizeof(X<I>)" we need to evaluate "I". */ | |
7747 | saved_skip_evaluation = skip_evaluation; | |
7748 | skip_evaluation = false; | |
34197853 | 7749 | |
7750 | /* First, determine the context for the type we are looking | |
7751 | up. */ | |
7cbbfe66 | 7752 | context = TYPE_CONTEXT (t); |
7753 | if (context) | |
f8ee651e | 7754 | { |
7755 | context = tsubst_aggr_type (context, args, complain, | |
7756 | in_decl, /*entering_scope=*/1); | |
7757 | /* If context is a nested class inside a class template, | |
7758 | it may still need to be instantiated (c++/33959). */ | |
7759 | if (TYPE_P (context)) | |
7760 | context = complete_type (context); | |
7761 | } | |
34197853 | 7762 | |
7763 | /* Then, figure out what arguments are appropriate for the | |
7764 | type we are trying to find. For example, given: | |
7765 | ||
7766 | template <class T> struct S; | |
7767 | template <class T, class U> void f(T, U) { S<U> su; } | |
7768 | ||
7769 | and supposing that we are instantiating f<int, double>, | |
7770 | then our ARGS will be {int, double}, but, when looking up | |
7771 | S we only want {double}. */ | |
3fa5de5b | 7772 | argvec = tsubst_template_args (TYPE_TI_ARGS (t), args, |
7773 | complain, in_decl); | |
0d4a9e94 | 7774 | if (argvec == error_mark_node) |
5c626400 | 7775 | r = error_mark_node; |
7776 | else | |
7777 | { | |
7778 | r = lookup_template_class (t, argvec, in_decl, context, | |
7779 | entering_scope, complain); | |
7780 | r = cp_build_qualified_type_real (r, TYPE_QUALS (t), complain); | |
7781 | } | |
074ab442 | 7782 | |
5c626400 | 7783 | skip_evaluation = saved_skip_evaluation; |
34197853 | 7784 | |
5c626400 | 7785 | return r; |
34197853 | 7786 | } |
9031d10b | 7787 | else |
34197853 | 7788 | /* This is not a template type, so there's nothing to do. */ |
7789 | return t; | |
7790 | ||
7791 | default: | |
6d158f4d | 7792 | return tsubst (t, args, complain, in_decl); |
34197853 | 7793 | } |
7794 | } | |
7795 | ||
70a658bd | 7796 | /* Substitute into the default argument ARG (a default argument for |
7797 | FN), which has the indicated TYPE. */ | |
7798 | ||
7799 | tree | |
807be5b4 | 7800 | tsubst_default_argument (tree fn, tree type, tree arg) |
70a658bd | 7801 | { |
534d03ee | 7802 | tree saved_class_ptr = NULL_TREE; |
7803 | tree saved_class_ref = NULL_TREE; | |
7804 | ||
70a658bd | 7805 | /* This default argument came from a template. Instantiate the |
7806 | default argument here, not in tsubst. In the case of | |
9031d10b | 7807 | something like: |
7808 | ||
70a658bd | 7809 | template <class T> |
7810 | struct S { | |
7811 | static T t(); | |
7812 | void f(T = t()); | |
7813 | }; | |
9031d10b | 7814 | |
70a658bd | 7815 | we must be careful to do name lookup in the scope of S<T>, |
3ad0e8cc | 7816 | rather than in the current class. */ |
7a468236 | 7817 | push_access_scope (fn); |
534d03ee | 7818 | /* The "this" pointer is not valid in a default argument. */ |
7819 | if (cfun) | |
7820 | { | |
7821 | saved_class_ptr = current_class_ptr; | |
7822 | cp_function_chain->x_current_class_ptr = NULL_TREE; | |
7823 | saved_class_ref = current_class_ref; | |
7824 | cp_function_chain->x_current_class_ref = NULL_TREE; | |
7825 | } | |
70a658bd | 7826 | |
3ad0e8cc | 7827 | push_deferring_access_checks(dk_no_deferred); |
0a045a26 | 7828 | /* The default argument expression may cause implicitly defined |
7829 | member functions to be synthesized, which will result in garbage | |
7830 | collection. We must treat this situation as if we were within | |
7831 | the body of function so as to avoid collecting live data on the | |
7832 | stack. */ | |
7833 | ++function_depth; | |
4b471722 | 7834 | arg = tsubst_expr (arg, DECL_TI_ARGS (fn), |
bde9ebf7 | 7835 | tf_warning_or_error, NULL_TREE, |
7836 | /*integral_constant_expression_p=*/false); | |
0a045a26 | 7837 | --function_depth; |
3ad0e8cc | 7838 | pop_deferring_access_checks(); |
7839 | ||
534d03ee | 7840 | /* Restore the "this" pointer. */ |
7841 | if (cfun) | |
7842 | { | |
7843 | cp_function_chain->x_current_class_ptr = saved_class_ptr; | |
7844 | cp_function_chain->x_current_class_ref = saved_class_ref; | |
7845 | } | |
7846 | ||
7a468236 | 7847 | pop_access_scope (fn); |
70a658bd | 7848 | |
7849 | /* Make sure the default argument is reasonable. */ | |
7850 | arg = check_default_argument (type, arg); | |
7851 | ||
7852 | return arg; | |
7853 | } | |
7854 | ||
7855 | /* Substitute into all the default arguments for FN. */ | |
7856 | ||
7857 | static void | |
807be5b4 | 7858 | tsubst_default_arguments (tree fn) |
70a658bd | 7859 | { |
7860 | tree arg; | |
7861 | tree tmpl_args; | |
7862 | ||
7863 | tmpl_args = DECL_TI_ARGS (fn); | |
7864 | ||
7865 | /* If this function is not yet instantiated, we certainly don't need | |
7866 | its default arguments. */ | |
7867 | if (uses_template_parms (tmpl_args)) | |
7868 | return; | |
7869 | ||
9031d10b | 7870 | for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn)); |
7871 | arg; | |
70a658bd | 7872 | arg = TREE_CHAIN (arg)) |
7873 | if (TREE_PURPOSE (arg)) | |
9031d10b | 7874 | TREE_PURPOSE (arg) = tsubst_default_argument (fn, |
70a658bd | 7875 | TREE_VALUE (arg), |
7876 | TREE_PURPOSE (arg)); | |
7877 | } | |
7878 | ||
e2ae55f2 | 7879 | /* Substitute the ARGS into the T, which is a _DECL. Return the |
7880 | result of the substitution. Issue error and warning messages under | |
7881 | control of COMPLAIN. */ | |
73645519 | 7882 | |
863c3f96 | 7883 | static tree |
e2ae55f2 | 7884 | tsubst_decl (tree t, tree args, tsubst_flags_t complain) |
471086d6 | 7885 | { |
6a86d77b | 7886 | location_t saved_loc; |
6a44e72e | 7887 | tree r = NULL_TREE; |
01b0e634 | 7888 | tree in_decl = t; |
32b5f03e | 7889 | |
4bac4c68 | 7890 | /* Set the filename and linenumber to improve error-reporting. */ |
6a86d77b | 7891 | saved_loc = input_location; |
346064d9 | 7892 | input_location = DECL_SOURCE_LOCATION (t); |
1e66592c | 7893 | |
471086d6 | 7894 | switch (TREE_CODE (t)) |
7895 | { | |
64b4f183 | 7896 | case TEMPLATE_DECL: |
7897 | { | |
fe14c2ad | 7898 | /* We can get here when processing a member function template, |
7899 | member class template, and template template parameter of | |
7900 | a template class. */ | |
64b4f183 | 7901 | tree decl = DECL_TEMPLATE_RESULT (t); |
b1cfe2be | 7902 | tree spec; |
fe14c2ad | 7903 | tree tmpl_args; |
7904 | tree full_args; | |
64b4f183 | 7905 | |
fe14c2ad | 7906 | if (DECL_TEMPLATE_TEMPLATE_PARM_P (t)) |
f4797a74 | 7907 | { |
fe14c2ad | 7908 | /* Template template parameter is treated here. */ |
7909 | tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl); | |
7910 | if (new_type == error_mark_node) | |
7911 | return error_mark_node; | |
34197853 | 7912 | |
fe14c2ad | 7913 | r = copy_decl (t); |
7914 | TREE_CHAIN (r) = NULL_TREE; | |
7915 | TREE_TYPE (r) = new_type; | |
7916 | DECL_TEMPLATE_RESULT (r) | |
7917 | = build_decl (TYPE_DECL, DECL_NAME (decl), new_type); | |
9031d10b | 7918 | DECL_TEMPLATE_PARMS (r) |
fe14c2ad | 7919 | = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args, |
7920 | complain); | |
7921 | TYPE_NAME (new_type) = r; | |
7922 | break; | |
7923 | } | |
34197853 | 7924 | |
fe14c2ad | 7925 | /* We might already have an instance of this template. |
7926 | The ARGS are for the surrounding class type, so the | |
7927 | full args contain the tsubst'd args for the context, | |
7928 | plus the innermost args from the template decl. */ | |
9031d10b | 7929 | tmpl_args = DECL_CLASS_TEMPLATE_P (t) |
fe14c2ad | 7930 | ? CLASSTYPE_TI_ARGS (TREE_TYPE (t)) |
7931 | : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t)); | |
f3d3cc67 | 7932 | /* Because this is a template, the arguments will still be |
7933 | dependent, even after substitution. If | |
7934 | PROCESSING_TEMPLATE_DECL is not set, the dependency | |
7935 | predicates will short-circuit. */ | |
7936 | ++processing_template_decl; | |
fe14c2ad | 7937 | full_args = tsubst_template_args (tmpl_args, args, |
7938 | complain, in_decl); | |
f3d3cc67 | 7939 | --processing_template_decl; |
88178f71 | 7940 | if (full_args == error_mark_node) |
7941 | return error_mark_node; | |
fe14c2ad | 7942 | |
7943 | /* tsubst_template_args doesn't copy the vector if | |
7944 | nothing changed. But, *something* should have | |
7945 | changed. */ | |
7946 | gcc_assert (full_args != tmpl_args); | |
7947 | ||
7948 | spec = retrieve_specialization (t, full_args, | |
7949 | /*class_specializations_p=*/true); | |
7950 | if (spec != NULL_TREE) | |
7951 | { | |
7952 | r = spec; | |
7953 | break; | |
f4797a74 | 7954 | } |
64b4f183 | 7955 | |
7956 | /* Make a new template decl. It will be similar to the | |
9031d10b | 7957 | original, but will record the current template arguments. |
64b4f183 | 7958 | We also create a new function declaration, which is just |
7959 | like the old one, but points to this new template, rather | |
7960 | than the old one. */ | |
c771ec7a | 7961 | r = copy_decl (t); |
b4df430b | 7962 | gcc_assert (DECL_LANG_SPECIFIC (r) != 0); |
4bac4c68 | 7963 | TREE_CHAIN (r) = NULL_TREE; |
f4797a74 | 7964 | |
4bac4c68 | 7965 | DECL_TEMPLATE_INFO (r) = build_tree_list (t, args); |
1ac9b32b | 7966 | |
7967 | if (TREE_CODE (decl) == TYPE_DECL) | |
7968 | { | |
f3d3cc67 | 7969 | tree new_type; |
7970 | ++processing_template_decl; | |
7971 | new_type = tsubst (TREE_TYPE (t), args, complain, in_decl); | |
074ab442 | 7972 | --processing_template_decl; |
5d6080a0 | 7973 | if (new_type == error_mark_node) |
7974 | return error_mark_node; | |
7975 | ||
4bac4c68 | 7976 | TREE_TYPE (r) = new_type; |
7977 | CLASSTYPE_TI_TEMPLATE (new_type) = r; | |
b8e0d419 | 7978 | DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type); |
4bac4c68 | 7979 | DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type); |
f3d3cc67 | 7980 | DECL_CONTEXT (r) = TYPE_CONTEXT (new_type); |
1ac9b32b | 7981 | } |
7982 | else | |
7983 | { | |
f3d3cc67 | 7984 | tree new_decl; |
7985 | ++processing_template_decl; | |
7986 | new_decl = tsubst (decl, args, complain, in_decl); | |
7987 | --processing_template_decl; | |
687897ea | 7988 | if (new_decl == error_mark_node) |
7989 | return error_mark_node; | |
b8e0d419 | 7990 | |
7991 | DECL_TEMPLATE_RESULT (r) = new_decl; | |
4bac4c68 | 7992 | DECL_TI_TEMPLATE (new_decl) = r; |
7993 | TREE_TYPE (r) = TREE_TYPE (new_decl); | |
7994 | DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl); | |
074ab442 | 7995 | DECL_CONTEXT (r) = DECL_CONTEXT (new_decl); |
1ac9b32b | 7996 | } |
7997 | ||
4bac4c68 | 7998 | SET_DECL_IMPLICIT_INSTANTIATION (r); |
7999 | DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE; | |
8000 | DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE; | |
64b4f183 | 8001 | |
8002 | /* The template parameters for this new template are all the | |
8003 | template parameters for the old template, except the | |
47cd6605 | 8004 | outermost level of parameters. */ |
9031d10b | 8005 | DECL_TEMPLATE_PARMS (r) |
6d158f4d | 8006 | = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args, |
4b471722 | 8007 | complain); |
64b4f183 | 8008 | |
1ac9b32b | 8009 | if (PRIMARY_TEMPLATE_P (t)) |
4bac4c68 | 8010 | DECL_PRIMARY_TEMPLATE (r) = r; |
1ac9b32b | 8011 | |
b9f26971 | 8012 | if (TREE_CODE (decl) != TYPE_DECL) |
8013 | /* Record this non-type partial instantiation. */ | |
9031d10b | 8014 | register_specialization (r, t, |
c1d4295f | 8015 | DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)), |
8016 | false); | |
64b4f183 | 8017 | } |
4bac4c68 | 8018 | break; |
471086d6 | 8019 | |
8020 | case FUNCTION_DECL: | |
8021 | { | |
b1cfe2be | 8022 | tree ctx; |
8f8b0e45 | 8023 | tree argvec = NULL_TREE; |
c76d0e72 | 8024 | tree *friends; |
34197853 | 8025 | tree gen_tmpl; |
e2ae55f2 | 8026 | tree type; |
e857e9c7 | 8027 | int member; |
da710137 | 8028 | int args_depth; |
8029 | int parms_depth; | |
e857e9c7 | 8030 | |
34197853 | 8031 | /* Nobody should be tsubst'ing into non-template functions. */ |
b4df430b | 8032 | gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE); |
34197853 | 8033 | |
8034 | if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL) | |
8035 | { | |
8036 | tree spec; | |
f11469de | 8037 | bool dependent_p; |
8038 | ||
8039 | /* If T is not dependent, just return it. We have to | |
8040 | increment PROCESSING_TEMPLATE_DECL because | |
8041 | value_dependent_expression_p assumes that nothing is | |
8042 | dependent when PROCESSING_TEMPLATE_DECL is zero. */ | |
8043 | ++processing_template_decl; | |
8044 | dependent_p = value_dependent_expression_p (t); | |
8045 | --processing_template_decl; | |
8046 | if (!dependent_p) | |
8047 | return t; | |
34197853 | 8048 | |
8049 | /* Calculate the most general template of which R is a | |
8050 | specialization, and the complete set of arguments used to | |
8051 | specialize R. */ | |
8052 | gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t)); | |
9031d10b | 8053 | argvec = tsubst_template_args (DECL_TI_ARGS |
3fa5de5b | 8054 | (DECL_TEMPLATE_RESULT (gen_tmpl)), |
9031d10b | 8055 | args, complain, in_decl); |
34197853 | 8056 | |
8057 | /* Check to see if we already have this specialization. */ | |
38d89ee9 | 8058 | spec = retrieve_specialization (gen_tmpl, argvec, |
8059 | /*class_specializations_p=*/false); | |
e4e283ec | 8060 | |
34197853 | 8061 | if (spec) |
4bac4c68 | 8062 | { |
8063 | r = spec; | |
8064 | break; | |
8065 | } | |
da710137 | 8066 | |
a32fe883 | 8067 | /* We can see more levels of arguments than parameters if |
8068 | there was a specialization of a member template, like | |
8069 | this: | |
8070 | ||
653e5405 | 8071 | template <class T> struct S { template <class U> void f(); } |
9031d10b | 8072 | template <> template <class U> void S<int>::f(U); |
a32fe883 | 8073 | |
ec0eee8d | 8074 | Here, we'll be substituting into the specialization, |
a32fe883 | 8075 | because that's where we can find the code we actually |
8076 | want to generate, but we'll have enough arguments for | |
9031d10b | 8077 | the most general template. |
a32fe883 | 8078 | |
8079 | We also deal with the peculiar case: | |
da710137 | 8080 | |
9031d10b | 8081 | template <class T> struct S { |
da710137 | 8082 | template <class U> friend void f(); |
8083 | }; | |
3467e461 | 8084 | template <class U> void f() {} |
da710137 | 8085 | template S<int>; |
8086 | template void f<double>(); | |
8087 | ||
8088 | Here, the ARGS for the instantiation of will be {int, | |
8089 | double}. But, we only need as many ARGS as there are | |
8090 | levels of template parameters in CODE_PATTERN. We are | |
8091 | careful not to get fooled into reducing the ARGS in | |
8092 | situations like: | |
8093 | ||
8094 | template <class T> struct S { template <class U> void f(U); } | |
8095 | template <class T> template <> void S<T>::f(int) {} | |
8096 | ||
8097 | which we can spot because the pattern will be a | |
8098 | specialization in this case. */ | |
8099 | args_depth = TMPL_ARGS_DEPTH (args); | |
9031d10b | 8100 | parms_depth = |
8101 | TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t))); | |
da710137 | 8102 | if (args_depth > parms_depth |
8103 | && !DECL_TEMPLATE_SPECIALIZATION (t)) | |
a32fe883 | 8104 | args = get_innermost_template_args (args, parms_depth); |
34197853 | 8105 | } |
8106 | else | |
8107 | { | |
8108 | /* This special case arises when we have something like this: | |
8109 | ||
653e5405 | 8110 | template <class T> struct S { |
9031d10b | 8111 | friend void f<int>(int, double); |
34197853 | 8112 | }; |
8113 | ||
8114 | Here, the DECL_TI_TEMPLATE for the friend declaration | |
c08d51be | 8115 | will be an IDENTIFIER_NODE. We are being called from |
8116 | tsubst_friend_function, and we want only to create a | |
8117 | new decl (R) with appropriate types so that we can call | |
8118 | determine_specialization. */ | |
34197853 | 8119 | gen_tmpl = NULL_TREE; |
8120 | } | |
8121 | ||
ddb39453 | 8122 | if (DECL_CLASS_SCOPE_P (t)) |
471086d6 | 8123 | { |
e857e9c7 | 8124 | if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t))) |
8125 | member = 2; | |
8126 | else | |
8127 | member = 1; | |
9031d10b | 8128 | ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, |
b9f26971 | 8129 | complain, t, /*entering_scope=*/1); |
e857e9c7 | 8130 | } |
8131 | else | |
8132 | { | |
8133 | member = 0; | |
9ba4048d | 8134 | ctx = DECL_CONTEXT (t); |
e857e9c7 | 8135 | } |
e2ae55f2 | 8136 | type = tsubst (TREE_TYPE (t), args, complain, in_decl); |
0401d384 | 8137 | if (type == error_mark_node) |
8138 | return error_mark_node; | |
471086d6 | 8139 | |
e857e9c7 | 8140 | /* We do NOT check for matching decls pushed separately at this |
653e5405 | 8141 | point, as they may not represent instantiations of this |
8142 | template, and in any case are considered separate under the | |
8143 | discrete model. */ | |
c771ec7a | 8144 | r = copy_decl (t); |
1146f179 | 8145 | DECL_USE_TEMPLATE (r) = 0; |
e857e9c7 | 8146 | TREE_TYPE (r) = type; |
7e64c604 | 8147 | /* Clear out the mangled name and RTL for the instantiation. */ |
8148 | SET_DECL_ASSEMBLER_NAME (r, NULL_TREE); | |
8149 | SET_DECL_RTL (r, NULL_RTX); | |
1611df57 | 8150 | DECL_INITIAL (r) = NULL_TREE; |
9ba4048d | 8151 | DECL_CONTEXT (r) = ctx; |
e857e9c7 | 8152 | |
9031d10b | 8153 | if (member && DECL_CONV_FN_P (r)) |
606b494c | 8154 | /* Type-conversion operator. Reconstruct the name, in |
8155 | case it's the name of one of the template's parameters. */ | |
8156 | DECL_NAME (r) = mangle_conv_op_name_for_type (TREE_TYPE (type)); | |
e857e9c7 | 8157 | |
6d158f4d | 8158 | DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args, |
4b471722 | 8159 | complain, t); |
2a88c9e6 | 8160 | DECL_RESULT (r) = NULL_TREE; |
e46cb663 | 8161 | |
8162 | TREE_STATIC (r) = 0; | |
8163 | TREE_PUBLIC (r) = TREE_PUBLIC (t); | |
8164 | DECL_EXTERNAL (r) = 1; | |
caa6fdce | 8165 | /* If this is an instantiation of a function with internal |
8166 | linkage, we already know what object file linkage will be | |
8167 | assigned to the instantiation. */ | |
8168 | DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r); | |
e46cb663 | 8169 | DECL_DEFER_OUTPUT (r) = 0; |
8170 | TREE_CHAIN (r) = NULL_TREE; | |
8171 | DECL_PENDING_INLINE_INFO (r) = 0; | |
7f075a5e | 8172 | DECL_PENDING_INLINE_P (r) = 0; |
b81be439 | 8173 | DECL_SAVED_TREE (r) = NULL_TREE; |
e46cb663 | 8174 | TREE_USED (r) = 0; |
e55cba4c | 8175 | if (DECL_CLONED_FUNCTION (r)) |
8176 | { | |
8177 | DECL_CLONED_FUNCTION (r) = tsubst (DECL_CLONED_FUNCTION (t), | |
4b471722 | 8178 | args, complain, t); |
e55cba4c | 8179 | TREE_CHAIN (r) = TREE_CHAIN (DECL_CLONED_FUNCTION (r)); |
8180 | TREE_CHAIN (DECL_CLONED_FUNCTION (r)) = r; | |
8181 | } | |
e46cb663 | 8182 | |
7e64c604 | 8183 | /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do |
8184 | this in the special friend case mentioned above where | |
8185 | GEN_TMPL is NULL. */ | |
34197853 | 8186 | if (gen_tmpl) |
b1cfe2be | 8187 | { |
9031d10b | 8188 | DECL_TEMPLATE_INFO (r) |
b0652a4f | 8189 | = tree_cons (gen_tmpl, argvec, NULL_TREE); |
34197853 | 8190 | SET_DECL_IMPLICIT_INSTANTIATION (r); |
c1d4295f | 8191 | register_specialization (r, gen_tmpl, argvec, false); |
34197853 | 8192 | |
70a658bd | 8193 | /* We're not supposed to instantiate default arguments |
8194 | until they are called, for a template. But, for a | |
8195 | declaration like: | |
8196 | ||
653e5405 | 8197 | template <class T> void f () |
8198 | { extern void g(int i = T()); } | |
9031d10b | 8199 | |
70a658bd | 8200 | we should do the substitution when the template is |
8201 | instantiated. We handle the member function case in | |
8202 | instantiate_class_template since the default arguments | |
8203 | might refer to other members of the class. */ | |
8204 | if (!member | |
8205 | && !PRIMARY_TEMPLATE_P (gen_tmpl) | |
8206 | && !uses_template_parms (argvec)) | |
8207 | tsubst_default_arguments (r); | |
b1cfe2be | 8208 | } |
439abd1d | 8209 | else |
8210 | DECL_TEMPLATE_INFO (r) = NULL_TREE; | |
53137e6a | 8211 | |
c76d0e72 | 8212 | /* Copy the list of befriending classes. */ |
8213 | for (friends = &DECL_BEFRIENDING_CLASSES (r); | |
8214 | *friends; | |
9031d10b | 8215 | friends = &TREE_CHAIN (*friends)) |
c76d0e72 | 8216 | { |
8217 | *friends = copy_node (*friends); | |
8218 | TREE_VALUE (*friends) = tsubst (TREE_VALUE (*friends), | |
4b471722 | 8219 | args, complain, |
c76d0e72 | 8220 | in_decl); |
8221 | } | |
8222 | ||
e4d39a4e | 8223 | if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r)) |
53137e6a | 8224 | { |
8225 | maybe_retrofit_in_chrg (r); | |
e4d39a4e | 8226 | if (DECL_CONSTRUCTOR_P (r)) |
8227 | grok_ctor_properties (ctx, r); | |
332692ff | 8228 | /* If this is an instantiation of a member template, clone it. |
8229 | If it isn't, that'll be handled by | |
8230 | clone_constructors_and_destructors. */ | |
3eb89cd8 | 8231 | if (PRIMARY_TEMPLATE_P (gen_tmpl)) |
01779b5f | 8232 | clone_function_decl (r, /*update_method_vec_p=*/0); |
53137e6a | 8233 | } |
fcdd34ab | 8234 | else if (IDENTIFIER_OPNAME_P (DECL_NAME (r)) |
8235 | && !grok_op_properties (r, (complain & tf_error) != 0)) | |
8236 | return error_mark_node; | |
334f6ce1 | 8237 | |
8238 | if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t)) | |
8239 | SET_DECL_FRIEND_CONTEXT (r, | |
8240 | tsubst (DECL_FRIEND_CONTEXT (t), | |
8241 | args, complain, in_decl)); | |
4a2849cb | 8242 | |
8243 | /* Possibly limit visibility based on template args. */ | |
8244 | DECL_VISIBILITY (r) = VISIBILITY_DEFAULT; | |
6a47600c | 8245 | if (DECL_VISIBILITY_SPECIFIED (t)) |
8246 | { | |
8247 | DECL_VISIBILITY_SPECIFIED (r) = 0; | |
8248 | DECL_ATTRIBUTES (r) | |
8249 | = remove_attribute ("visibility", DECL_ATTRIBUTES (r)); | |
8250 | } | |
4a2849cb | 8251 | determine_visibility (r); |
531be878 | 8252 | |
8253 | apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0, | |
8254 | args, complain, in_decl); | |
471086d6 | 8255 | } |
4bac4c68 | 8256 | break; |
471086d6 | 8257 | |
8258 | case PARM_DECL: | |
8259 | { | |
d95d815d | 8260 | tree type = NULL_TREE; |
8261 | int i, len = 1; | |
8262 | tree expanded_types = NULL_TREE; | |
8263 | tree prev_r = NULL_TREE; | |
8264 | tree first_r = NULL_TREE; | |
a500a485 | 8265 | |
d95d815d | 8266 | if (FUNCTION_PARAMETER_PACK_P (t)) |
8267 | { | |
8268 | /* If there is a local specialization that isn't a | |
8269 | parameter pack, it means that we're doing a "simple" | |
8270 | substitution from inside tsubst_pack_expansion. Just | |
93b25def | 8271 | return the local specialization (which will be a single |
d95d815d | 8272 | parm). */ |
f8ee651e | 8273 | tree spec = retrieve_local_specialization (t); |
d95d815d | 8274 | if (spec |
8275 | && TREE_CODE (spec) == PARM_DECL | |
8276 | && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION) | |
8277 | return spec; | |
8278 | ||
8279 | /* Expand the TYPE_PACK_EXPANSION that provides the types for | |
8280 | the parameters in this function parameter pack. */ | |
8281 | expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args, | |
8282 | complain, in_decl); | |
8283 | if (TREE_CODE (expanded_types) == TREE_VEC) | |
8284 | { | |
8285 | len = TREE_VEC_LENGTH (expanded_types); | |
8286 | ||
8287 | /* Zero-length parameter packs are boring. Just substitute | |
8288 | into the chain. */ | |
8289 | if (len == 0) | |
8290 | return tsubst (TREE_CHAIN (t), args, complain, | |
8291 | TREE_CHAIN (t)); | |
8292 | } | |
8293 | else | |
8294 | { | |
8295 | /* All we did was update the type. Make a note of that. */ | |
8296 | type = expanded_types; | |
8297 | expanded_types = NULL_TREE; | |
8298 | } | |
8299 | } | |
f4797a74 | 8300 | |
d95d815d | 8301 | /* Loop through all of the parameter's we'll build. When T is |
8302 | a function parameter pack, LEN is the number of expanded | |
8303 | types in EXPANDED_TYPES; otherwise, LEN is 1. */ | |
8304 | r = NULL_TREE; | |
8305 | for (i = 0; i < len; ++i) | |
8306 | { | |
8307 | prev_r = r; | |
8308 | r = copy_node (t); | |
8309 | if (DECL_TEMPLATE_PARM_P (t)) | |
8310 | SET_DECL_TEMPLATE_PARM_P (r); | |
8311 | ||
8312 | if (expanded_types) | |
8313 | /* We're on the Ith parameter of the function parameter | |
8314 | pack. */ | |
8315 | { | |
8316 | /* Get the Ith type. */ | |
8317 | type = TREE_VEC_ELT (expanded_types, i); | |
8318 | ||
8319 | if (DECL_NAME (r)) | |
8320 | /* Rename the parameter to include the index. */ | |
8321 | DECL_NAME (r) = | |
8322 | make_ith_pack_parameter_name (DECL_NAME (r), i); | |
8323 | } | |
8324 | else if (!type) | |
8325 | /* We're dealing with a normal parameter. */ | |
8326 | type = tsubst (TREE_TYPE (t), args, complain, in_decl); | |
8327 | ||
8328 | type = type_decays_to (type); | |
8329 | TREE_TYPE (r) = type; | |
8330 | cp_apply_type_quals_to_decl (cp_type_quals (type), r); | |
8331 | ||
8332 | if (DECL_INITIAL (r)) | |
8333 | { | |
8334 | if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX) | |
8335 | DECL_INITIAL (r) = TREE_TYPE (r); | |
8336 | else | |
8337 | DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args, | |
8338 | complain, in_decl); | |
8339 | } | |
8340 | ||
8341 | DECL_CONTEXT (r) = NULL_TREE; | |
8342 | ||
8343 | if (!DECL_TEMPLATE_PARM_P (r)) | |
8344 | DECL_ARG_TYPE (r) = type_passed_as (type); | |
8345 | ||
531be878 | 8346 | apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0, |
8347 | args, complain, in_decl); | |
8348 | ||
d95d815d | 8349 | /* Keep track of the first new parameter we |
8350 | generate. That's what will be returned to the | |
8351 | caller. */ | |
8352 | if (!first_r) | |
8353 | first_r = r; | |
8354 | ||
8355 | /* Build a proper chain of parameters when substituting | |
8356 | into a function parameter pack. */ | |
8357 | if (prev_r) | |
8358 | TREE_CHAIN (prev_r) = r; | |
8359 | } | |
d145d8d5 | 8360 | |
471086d6 | 8361 | if (TREE_CHAIN (t)) |
6d158f4d | 8362 | TREE_CHAIN (r) = tsubst (TREE_CHAIN (t), args, |
4b471722 | 8363 | complain, TREE_CHAIN (t)); |
d95d815d | 8364 | |
8365 | /* FIRST_R contains the start of the chain we've built. */ | |
8366 | r = first_r; | |
471086d6 | 8367 | } |
4bac4c68 | 8368 | break; |
471086d6 | 8369 | |
e857e9c7 | 8370 | case FIELD_DECL: |
8371 | { | |
e2ae55f2 | 8372 | tree type; |
8373 | ||
c771ec7a | 8374 | r = copy_decl (t); |
e2ae55f2 | 8375 | type = tsubst (TREE_TYPE (t), args, complain, in_decl); |
8376 | if (type == error_mark_node) | |
8377 | return error_mark_node; | |
a500a485 | 8378 | TREE_TYPE (r) = type; |
11ef5c28 | 8379 | cp_apply_type_quals_to_decl (cp_type_quals (type), r); |
a500a485 | 8380 | |
bde9ebf7 | 8381 | /* DECL_INITIAL gives the number of bits in a bit-field. */ |
8382 | DECL_INITIAL (r) | |
8383 | = tsubst_expr (DECL_INITIAL (t), args, | |
8384 | complain, in_decl, | |
8385 | /*integral_constant_expression_p=*/true); | |
a500a485 | 8386 | /* We don't have to set DECL_CONTEXT here; it is set by |
8387 | finish_member_declaration. */ | |
e857e9c7 | 8388 | TREE_CHAIN (r) = NULL_TREE; |
9031d10b | 8389 | if (VOID_TYPE_P (type)) |
3cf8b391 | 8390 | error ("instantiation of %q+D as type %qT", r, type); |
531be878 | 8391 | |
8392 | apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0, | |
8393 | args, complain, in_decl); | |
e857e9c7 | 8394 | } |
4bac4c68 | 8395 | break; |
e857e9c7 | 8396 | |
8397 | case USING_DECL: | |
094fb0d8 | 8398 | /* We reach here only for member using decls. */ |
8399 | if (DECL_DEPENDENT_P (t)) | |
8400 | { | |
8401 | r = do_class_using_decl | |
8402 | (tsubst_copy (USING_DECL_SCOPE (t), args, complain, in_decl), | |
8403 | tsubst_copy (DECL_NAME (t), args, complain, in_decl)); | |
8404 | if (!r) | |
8405 | r = error_mark_node; | |
8406 | } | |
8407 | else | |
8408 | { | |
8409 | r = copy_node (t); | |
8410 | TREE_CHAIN (r) = NULL_TREE; | |
8411 | } | |
4bac4c68 | 8412 | break; |
e857e9c7 | 8413 | |
70a658bd | 8414 | case TYPE_DECL: |
e857e9c7 | 8415 | case VAR_DECL: |
8416 | { | |
e456e2ea | 8417 | tree argvec = NULL_TREE; |
8418 | tree gen_tmpl = NULL_TREE; | |
34197853 | 8419 | tree spec; |
e456e2ea | 8420 | tree tmpl = NULL_TREE; |
70a658bd | 8421 | tree ctx; |
e2ae55f2 | 8422 | tree type = NULL_TREE; |
59bfec98 | 8423 | bool local_p; |
70a658bd | 8424 | |
d718e1bc | 8425 | if (TREE_CODE (t) == TYPE_DECL |
2fdd6488 | 8426 | && t == TYPE_MAIN_DECL (TREE_TYPE (t))) |
e2ae55f2 | 8427 | { |
2fdd6488 | 8428 | /* If this is the canonical decl, we don't have to |
8429 | mess with instantiations, and often we can't (for | |
8430 | typename, template type parms and such). Note that | |
8431 | TYPE_NAME is not correct for the above test if | |
8432 | we've copied the type for a typedef. */ | |
e2ae55f2 | 8433 | type = tsubst (TREE_TYPE (t), args, complain, in_decl); |
70dcc792 | 8434 | if (type == error_mark_node) |
8435 | return error_mark_node; | |
2fdd6488 | 8436 | r = TYPE_NAME (type); |
8437 | break; | |
e2ae55f2 | 8438 | } |
9031d10b | 8439 | |
59bfec98 | 8440 | /* Check to see if we already have the specialization we |
8441 | need. */ | |
8442 | spec = NULL_TREE; | |
8443 | if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t)) | |
8444 | { | |
8445 | /* T is a static data member or namespace-scope entity. | |
8446 | We have to substitute into namespace-scope variables | |
8447 | (even though such entities are never templates) because | |
8448 | of cases like: | |
8449 | ||
8450 | template <class T> void f() { extern T t; } | |
8451 | ||
8452 | where the entity referenced is not known until | |
8453 | instantiation time. */ | |
8454 | local_p = false; | |
8455 | ctx = DECL_CONTEXT (t); | |
8456 | if (DECL_CLASS_SCOPE_P (t)) | |
8457 | { | |
8458 | ctx = tsubst_aggr_type (ctx, args, | |
8459 | complain, | |
8460 | in_decl, /*entering_scope=*/1); | |
8461 | /* If CTX is unchanged, then T is in fact the | |
8462 | specialization we want. That situation occurs when | |
8463 | referencing a static data member within in its own | |
8464 | class. We can use pointer equality, rather than | |
8465 | same_type_p, because DECL_CONTEXT is always | |
8466 | canonical. */ | |
8467 | if (ctx == DECL_CONTEXT (t)) | |
8468 | spec = t; | |
8469 | } | |
e857e9c7 | 8470 | |
59bfec98 | 8471 | if (!spec) |
8472 | { | |
8473 | tmpl = DECL_TI_TEMPLATE (t); | |
8474 | gen_tmpl = most_general_template (tmpl); | |
8475 | argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl); | |
8476 | spec = (retrieve_specialization | |
8477 | (gen_tmpl, argvec, | |
8478 | /*class_specializations_p=*/false)); | |
8479 | } | |
8480 | } | |
70a658bd | 8481 | else |
db1ad296 | 8482 | { |
59bfec98 | 8483 | /* A local variable. */ |
8484 | local_p = true; | |
db1ad296 | 8485 | /* Subsequent calls to pushdecl will fill this in. */ |
8486 | ctx = NULL_TREE; | |
59bfec98 | 8487 | spec = retrieve_local_specialization (t); |
db1ad296 | 8488 | } |
59bfec98 | 8489 | /* If we already have the specialization we need, there is |
8490 | nothing more to do. */ | |
34197853 | 8491 | if (spec) |
4bac4c68 | 8492 | { |
8493 | r = spec; | |
8494 | break; | |
8495 | } | |
e857e9c7 | 8496 | |
59bfec98 | 8497 | /* Create a new node for the specialization we need. */ |
c771ec7a | 8498 | r = copy_decl (t); |
d718e1bc | 8499 | if (type == NULL_TREE) |
8500 | type = tsubst (TREE_TYPE (t), args, complain, in_decl); | |
c8a606ae | 8501 | if (TREE_CODE (r) == VAR_DECL) |
878870b4 | 8502 | { |
03798958 | 8503 | /* Even if the original location is out of scope, the |
8504 | newly substituted one is not. */ | |
8505 | DECL_DEAD_FOR_LOCAL (r) = 0; | |
8506 | DECL_INITIALIZED_P (r) = 0; | |
8507 | DECL_TEMPLATE_INSTANTIATED (r) = 0; | |
e2ae55f2 | 8508 | if (type == error_mark_node) |
8509 | return error_mark_node; | |
729a26ca | 8510 | if (TREE_CODE (type) == FUNCTION_TYPE) |
8511 | { | |
8512 | /* It may seem that this case cannot occur, since: | |
8513 | ||
8514 | typedef void f(); | |
8515 | void g() { f x; } | |
8516 | ||
8517 | declares a function, not a variable. However: | |
8518 | ||
8519 | typedef void f(); | |
8520 | template <typename T> void g() { T t; } | |
8521 | template void g<f>(); | |
8522 | ||
8523 | is an attempt to declare a variable with function | |
8524 | type. */ | |
8525 | error ("variable %qD has function type", | |
8526 | /* R is not yet sufficiently initialized, so we | |
8527 | just use its name. */ | |
8528 | DECL_NAME (r)); | |
8529 | return error_mark_node; | |
8530 | } | |
878870b4 | 8531 | type = complete_type (type); |
8532 | DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) | |
8533 | = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t); | |
dc9d9310 | 8534 | type = check_var_type (DECL_NAME (r), type); |
36b92eb7 | 8535 | |
8536 | if (DECL_HAS_VALUE_EXPR_P (t)) | |
8537 | { | |
8538 | tree ve = DECL_VALUE_EXPR (t); | |
bde9ebf7 | 8539 | ve = tsubst_expr (ve, args, complain, in_decl, |
8540 | /*constant_expression_p=*/false); | |
074ab442 | 8541 | SET_DECL_VALUE_EXPR (r, ve); |
36b92eb7 | 8542 | } |
878870b4 | 8543 | } |
56e75739 | 8544 | else if (DECL_SELF_REFERENCE_P (t)) |
8545 | SET_DECL_SELF_REFERENCE_P (r); | |
205aec29 | 8546 | TREE_TYPE (r) = type; |
11ef5c28 | 8547 | cp_apply_type_quals_to_decl (cp_type_quals (type), r); |
e857e9c7 | 8548 | DECL_CONTEXT (r) = ctx; |
7e64c604 | 8549 | /* Clear out the mangled name and RTL for the instantiation. */ |
8550 | SET_DECL_ASSEMBLER_NAME (r, NULL_TREE); | |
5ded8c6f | 8551 | if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL)) |
8552 | SET_DECL_RTL (r, NULL_RTX); | |
03798958 | 8553 | /* The initializer must not be expanded until it is required; |
8554 | see [temp.inst]. */ | |
2739960c | 8555 | DECL_INITIAL (r) = NULL_TREE; |
5ded8c6f | 8556 | if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL)) |
8557 | SET_DECL_RTL (r, NULL_RTX); | |
b278476e | 8558 | DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0; |
4a2849cb | 8559 | if (TREE_CODE (r) == VAR_DECL) |
8560 | { | |
8561 | /* Possibly limit visibility based on template args. */ | |
8562 | DECL_VISIBILITY (r) = VISIBILITY_DEFAULT; | |
6a47600c | 8563 | if (DECL_VISIBILITY_SPECIFIED (t)) |
8564 | { | |
8565 | DECL_VISIBILITY_SPECIFIED (r) = 0; | |
8566 | DECL_ATTRIBUTES (r) | |
8567 | = remove_attribute ("visibility", DECL_ATTRIBUTES (r)); | |
8568 | } | |
4a2849cb | 8569 | determine_visibility (r); |
8570 | } | |
51d7cfb2 | 8571 | /* Preserve a typedef that names a type. */ |
8572 | else if (TREE_CODE (r) == TYPE_DECL | |
8573 | && DECL_ORIGINAL_TYPE (t) | |
8574 | && type != error_mark_node) | |
8575 | { | |
8576 | DECL_ORIGINAL_TYPE (r) = tsubst (DECL_ORIGINAL_TYPE (t), | |
8577 | args, complain, in_decl); | |
8578 | TREE_TYPE (r) = type = build_variant_type_copy (type); | |
8579 | TYPE_NAME (type) = r; | |
8580 | } | |
e857e9c7 | 8581 | |
db1ad296 | 8582 | if (!local_p) |
8583 | { | |
8584 | /* A static data member declaration is always marked | |
8585 | external when it is declared in-class, even if an | |
8586 | initializer is present. We mimic the non-template | |
8587 | processing here. */ | |
8588 | DECL_EXTERNAL (r) = 1; | |
2789f91b | 8589 | |
c1d4295f | 8590 | register_specialization (r, gen_tmpl, argvec, false); |
db1ad296 | 8591 | DECL_TEMPLATE_INFO (r) = tree_cons (tmpl, argvec, NULL_TREE); |
8592 | SET_DECL_IMPLICIT_INSTANTIATION (r); | |
8593 | } | |
70a658bd | 8594 | else |
db1ad296 | 8595 | register_local_specialization (r, t); |
e857e9c7 | 8596 | |
e857e9c7 | 8597 | TREE_CHAIN (r) = NULL_TREE; |
531be878 | 8598 | |
51d7cfb2 | 8599 | apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), |
8600 | (int) ATTR_FLAG_TYPE_IN_PLACE, | |
531be878 | 8601 | args, complain, in_decl); |
c8a606ae | 8602 | layout_decl (r, 0); |
e857e9c7 | 8603 | } |
4bac4c68 | 8604 | break; |
e857e9c7 | 8605 | |
4bac4c68 | 8606 | default: |
2e3e31d2 | 8607 | gcc_unreachable (); |
9031d10b | 8608 | } |
4bac4c68 | 8609 | |
8610 | /* Restore the file and line information. */ | |
6a86d77b | 8611 | input_location = saved_loc; |
4bac4c68 | 8612 | |
8613 | return r; | |
8614 | } | |
8615 | ||
755edffd | 8616 | /* Substitute into the ARG_TYPES of a function type. */ |
ae97983e | 8617 | |
863c3f96 | 8618 | static tree |
9031d10b | 8619 | tsubst_arg_types (tree arg_types, |
653e5405 | 8620 | tree args, |
8621 | tsubst_flags_t complain, | |
8622 | tree in_decl) | |
ae97983e | 8623 | { |
8624 | tree remaining_arg_types; | |
d95d815d | 8625 | tree type = NULL_TREE; |
8626 | int i = 1; | |
8627 | tree expanded_args = NULL_TREE; | |
29081c08 | 8628 | tree default_arg; |
ae97983e | 8629 | |
8630 | if (!arg_types || arg_types == void_list_node) | |
8631 | return arg_types; | |
9031d10b | 8632 | |
ae97983e | 8633 | remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types), |
6d158f4d | 8634 | args, complain, in_decl); |
8635 | if (remaining_arg_types == error_mark_node) | |
8636 | return error_mark_node; | |
8637 | ||
d95d815d | 8638 | if (PACK_EXPANSION_P (TREE_VALUE (arg_types))) |
01b0e634 | 8639 | { |
d95d815d | 8640 | /* For a pack expansion, perform substitution on the |
8641 | entire expression. Later on, we'll handle the arguments | |
8642 | one-by-one. */ | |
8643 | expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types), | |
8644 | args, complain, in_decl); | |
ae97983e | 8645 | |
d95d815d | 8646 | if (TREE_CODE (expanded_args) == TREE_VEC) |
8647 | /* So that we'll spin through the parameters, one by one. */ | |
8648 | i = TREE_VEC_LENGTH (expanded_args); | |
8649 | else | |
8650 | { | |
8651 | /* We only partially substituted into the parameter | |
8652 | pack. Our type is TYPE_PACK_EXPANSION. */ | |
8653 | type = expanded_args; | |
8654 | expanded_args = NULL_TREE; | |
8655 | } | |
8656 | } | |
ae97983e | 8657 | |
d95d815d | 8658 | while (i > 0) { |
8659 | --i; | |
8660 | ||
8661 | if (expanded_args) | |
8662 | type = TREE_VEC_ELT (expanded_args, i); | |
8663 | else if (!type) | |
8664 | type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl); | |
9031d10b | 8665 | |
d95d815d | 8666 | if (type == error_mark_node) |
8667 | return error_mark_node; | |
8668 | if (VOID_TYPE_P (type)) | |
8669 | { | |
8670 | if (complain & tf_error) | |
8671 | { | |
8672 | error ("invalid parameter type %qT", type); | |
8673 | if (in_decl) | |
8674 | error ("in declaration %q+D", in_decl); | |
8675 | } | |
8676 | return error_mark_node; | |
29081c08 | 8677 | } |
d95d815d | 8678 | |
8679 | /* Do array-to-pointer, function-to-pointer conversion, and ignore | |
8680 | top-level qualifiers as required. */ | |
8681 | type = TYPE_MAIN_VARIANT (type_decays_to (type)); | |
9031d10b | 8682 | |
d95d815d | 8683 | /* We do not substitute into default arguments here. The standard |
8684 | mandates that they be instantiated only when needed, which is | |
8685 | done in build_over_call. */ | |
8686 | default_arg = TREE_PURPOSE (arg_types); | |
8687 | ||
8688 | if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG) | |
8689 | { | |
8690 | /* We've instantiated a template before its default arguments | |
8691 | have been parsed. This can happen for a nested template | |
8692 | class, and is not an error unless we require the default | |
8693 | argument in a call of this function. */ | |
8694 | remaining_arg_types = | |
8695 | tree_cons (default_arg, type, remaining_arg_types); | |
8696 | VEC_safe_push (tree, gc, DEFARG_INSTANTIATIONS (default_arg), | |
8697 | remaining_arg_types); | |
8698 | } | |
8699 | else | |
8700 | remaining_arg_types = | |
8701 | hash_tree_cons (default_arg, type, remaining_arg_types); | |
8702 | } | |
8703 | ||
8704 | return remaining_arg_types; | |
ae97983e | 8705 | } |
8706 | ||
6d158f4d | 8707 | /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does |
8708 | *not* handle the exception-specification for FNTYPE, because the | |
8709 | initial substitution of explicitly provided template parameters | |
8710 | during argument deduction forbids substitution into the | |
8711 | exception-specification: | |
8712 | ||
8713 | [temp.deduct] | |
8714 | ||
8715 | All references in the function type of the function template to the | |
8716 | corresponding template parameters are replaced by the specified tem- | |
8717 | plate argument values. If a substitution in a template parameter or | |
8718 | in the function type of the function template results in an invalid | |
8719 | type, type deduction fails. [Note: The equivalent substitution in | |
8720 | exception specifications is done only when the function is instanti- | |
8721 | ated, at which point a program is ill-formed if the substitution | |
8722 | results in an invalid type.] */ | |
8723 | ||
8724 | static tree | |
9031d10b | 8725 | tsubst_function_type (tree t, |
653e5405 | 8726 | tree args, |
8727 | tsubst_flags_t complain, | |
8728 | tree in_decl) | |
6d158f4d | 8729 | { |
8730 | tree return_type; | |
8731 | tree arg_types; | |
8732 | tree fntype; | |
8733 | ||
a4caa97a | 8734 | /* The TYPE_CONTEXT is not used for function/method types. */ |
b4df430b | 8735 | gcc_assert (TYPE_CONTEXT (t) == NULL_TREE); |
6d158f4d | 8736 | |
1c4208ef | 8737 | /* Substitute the return type. */ |
6d158f4d | 8738 | return_type = tsubst (TREE_TYPE (t), args, complain, in_decl); |
8739 | if (return_type == error_mark_node) | |
8740 | return error_mark_node; | |
0836e2c9 | 8741 | /* The standard does not presently indicate that creation of a |
8742 | function type with an invalid return type is a deduction failure. | |
bcc16f59 | 8743 | However, that is clearly analogous to creating an array of "void" |
9031d10b | 8744 | or a reference to a reference. This is core issue #486. */ |
0836e2c9 | 8745 | if (TREE_CODE (return_type) == ARRAY_TYPE |
8746 | || TREE_CODE (return_type) == FUNCTION_TYPE) | |
8747 | { | |
8748 | if (complain & tf_error) | |
8749 | { | |
8750 | if (TREE_CODE (return_type) == ARRAY_TYPE) | |
8751 | error ("function returning an array"); | |
8752 | else | |
8753 | error ("function returning a function"); | |
8754 | } | |
8755 | return error_mark_node; | |
8756 | } | |
6d158f4d | 8757 | |
755edffd | 8758 | /* Substitute the argument types. */ |
6d158f4d | 8759 | arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, |
9031d10b | 8760 | complain, in_decl); |
6d158f4d | 8761 | if (arg_types == error_mark_node) |
8762 | return error_mark_node; | |
9031d10b | 8763 | |
e9ec06a1 | 8764 | if (TYPE_QUALS (return_type) != TYPE_UNQUALIFIED |
8765 | && in_decl != NULL_TREE | |
8766 | && !TREE_NO_WARNING (in_decl) | |
8767 | && (SCALAR_TYPE_P (return_type) || VOID_TYPE_P (return_type))) | |
36e37a87 | 8768 | warning (OPT_Wignored_qualifiers, |
e9ec06a1 | 8769 | "type qualifiers ignored on function return type"); |
8770 | ||
6d158f4d | 8771 | /* Construct a new type node and return it. */ |
8772 | if (TREE_CODE (t) == FUNCTION_TYPE) | |
8773 | fntype = build_function_type (return_type, arg_types); | |
8774 | else | |
8775 | { | |
8776 | tree r = TREE_TYPE (TREE_VALUE (arg_types)); | |
95397ff9 | 8777 | if (! MAYBE_CLASS_TYPE_P (r)) |
6d158f4d | 8778 | { |
8779 | /* [temp.deduct] | |
9031d10b | 8780 | |
6d158f4d | 8781 | Type deduction may fail for any of the following |
8782 | reasons: | |
9031d10b | 8783 | |
6d158f4d | 8784 | -- Attempting to create "pointer to member of T" when T |
8785 | is not a class type. */ | |
4b471722 | 8786 | if (complain & tf_error) |
39e7b4a4 | 8787 | error ("creating pointer to member function of non-class type %qT", |
6d158f4d | 8788 | r); |
8789 | return error_mark_node; | |
8790 | } | |
9031d10b | 8791 | |
8792 | fntype = build_method_type_directly (r, return_type, | |
5bfb0742 | 8793 | TREE_CHAIN (arg_types)); |
6d158f4d | 8794 | } |
4b471722 | 8795 | fntype = cp_build_qualified_type_real (fntype, TYPE_QUALS (t), complain); |
b3beaf30 | 8796 | fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t)); |
9031d10b | 8797 | |
8798 | return fntype; | |
6d158f4d | 8799 | } |
8800 | ||
38d89ee9 | 8801 | /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template |
8802 | ARGS into that specification, and return the substituted | |
8803 | specification. If there is no specification, return NULL_TREE. */ | |
8804 | ||
8805 | static tree | |
9031d10b | 8806 | tsubst_exception_specification (tree fntype, |
8807 | tree args, | |
38d89ee9 | 8808 | tsubst_flags_t complain, |
8809 | tree in_decl) | |
8810 | { | |
8811 | tree specs; | |
8812 | tree new_specs; | |
8813 | ||
8814 | specs = TYPE_RAISES_EXCEPTIONS (fntype); | |
8815 | new_specs = NULL_TREE; | |
8816 | if (specs) | |
8817 | { | |
8818 | if (! TREE_VALUE (specs)) | |
8819 | new_specs = specs; | |
8820 | else | |
8821 | while (specs) | |
8822 | { | |
8823 | tree spec; | |
d95d815d | 8824 | int i, len = 1; |
8825 | tree expanded_specs = NULL_TREE; | |
8826 | ||
8827 | if (PACK_EXPANSION_P (TREE_VALUE (specs))) | |
8828 | { | |
8829 | /* Expand the pack expansion type. */ | |
8830 | expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs), | |
8831 | args, complain, | |
8832 | in_decl); | |
2732dc9b | 8833 | |
8834 | if (expanded_specs == error_mark_node) | |
8835 | return error_mark_node; | |
8836 | else if (TREE_CODE (expanded_specs) == TREE_VEC) | |
8837 | len = TREE_VEC_LENGTH (expanded_specs); | |
8838 | else | |
8839 | { | |
8840 | /* We're substituting into a member template, so | |
8841 | we got a TYPE_PACK_EXPANSION back. Add that | |
8842 | expansion and move on. */ | |
8843 | gcc_assert (TREE_CODE (expanded_specs) | |
8844 | == TYPE_PACK_EXPANSION); | |
8845 | new_specs = add_exception_specifier (new_specs, | |
8846 | expanded_specs, | |
8847 | complain); | |
8848 | specs = TREE_CHAIN (specs); | |
8849 | continue; | |
8850 | } | |
d95d815d | 8851 | } |
8852 | ||
8853 | for (i = 0; i < len; ++i) | |
8854 | { | |
8855 | if (expanded_specs) | |
8856 | spec = TREE_VEC_ELT (expanded_specs, i); | |
8857 | else | |
8858 | spec = tsubst (TREE_VALUE (specs), args, complain, in_decl); | |
8859 | if (spec == error_mark_node) | |
8860 | return spec; | |
8861 | new_specs = add_exception_specifier (new_specs, spec, | |
8862 | complain); | |
8863 | } | |
8864 | ||
8865 | specs = TREE_CHAIN (specs); | |
38d89ee9 | 8866 | } |
8867 | } | |
8868 | return new_specs; | |
8869 | } | |
8870 | ||
6d158f4d | 8871 | /* Take the tree structure T and replace template parameters used |
8872 | therein with the argument vector ARGS. IN_DECL is an associated | |
8873 | decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE. | |
4b471722 | 8874 | Issue error and warning messages under control of COMPLAIN. Note |
8875 | that we must be relatively non-tolerant of extensions here, in | |
8876 | order to preserve conformance; if we allow substitutions that | |
8877 | should not be allowed, we may allow argument deductions that should | |
8878 | not succeed, and therefore report ambiguous overload situations | |
8879 | where there are none. In theory, we could allow the substitution, | |
8880 | but indicate that it should have failed, and allow our caller to | |
8881 | make sure that the right thing happens, but we don't try to do this | |
8882 | yet. | |
6d158f4d | 8883 | |
8884 | This function is used for dealing with types, decls and the like; | |
8885 | for expressions, use tsubst_expr or tsubst_copy. */ | |
4bac4c68 | 8886 | |
5f6526e1 | 8887 | static tree |
807be5b4 | 8888 | tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl) |
4bac4c68 | 8889 | { |
71debe1f | 8890 | tree type, r; |
4bac4c68 | 8891 | |
8892 | if (t == NULL_TREE || t == error_mark_node | |
8893 | || t == integer_type_node | |
8894 | || t == void_type_node | |
8895 | || t == char_type_node | |
af59b531 | 8896 | || t == unknown_type_node |
4bac4c68 | 8897 | || TREE_CODE (t) == NAMESPACE_DECL) |
8898 | return t; | |
8899 | ||
e2ae55f2 | 8900 | if (DECL_P (t)) |
8901 | return tsubst_decl (t, args, complain); | |
8902 | ||
4bac4c68 | 8903 | if (TREE_CODE (t) == IDENTIFIER_NODE) |
8904 | type = IDENTIFIER_TYPE_VALUE (t); | |
8905 | else | |
8906 | type = TREE_TYPE (t); | |
b7edeb23 | 8907 | |
b4df430b | 8908 | gcc_assert (type != unknown_type_node); |
4bac4c68 | 8909 | |
d718e1bc | 8910 | /* Reuse typedefs. We need to do this to handle dependent attributes, |
2fdd6488 | 8911 | such as attribute aligned. */ |
51d7cfb2 | 8912 | if (TYPE_P (t) |
8913 | && TYPE_NAME (t) | |
2fdd6488 | 8914 | && TYPE_NAME (t) != TYPE_MAIN_DECL (t)) |
51d7cfb2 | 8915 | { |
8916 | tree decl = TYPE_NAME (t); | |
d718e1bc | 8917 | |
2fdd6488 | 8918 | if (DECL_CLASS_SCOPE_P (decl) |
08037cca | 8919 | && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl)) |
8920 | && uses_template_parms (DECL_CONTEXT (decl))) | |
d718e1bc | 8921 | { |
8922 | tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl)); | |
8923 | tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl); | |
8924 | r = retrieve_specialization (tmpl, gen_args, false); | |
8925 | } | |
f8ee651e | 8926 | else if (DECL_FUNCTION_SCOPE_P (decl) |
08037cca | 8927 | && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl)) |
8928 | && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl)))) | |
d718e1bc | 8929 | r = retrieve_local_specialization (decl); |
8930 | else | |
f8ee651e | 8931 | /* The typedef is from a non-template context. */ |
8932 | return t; | |
8933 | ||
d718e1bc | 8934 | if (r) |
8935 | { | |
8936 | r = TREE_TYPE (r); | |
8937 | r = cp_build_qualified_type_real | |
8938 | (r, cp_type_quals (t) | cp_type_quals (r), | |
8939 | complain | tf_ignore_bad_quals); | |
8940 | return r; | |
8941 | } | |
8942 | /* Else we must be instantiating the typedef, so fall through. */ | |
51d7cfb2 | 8943 | } |
8944 | ||
e2ae55f2 | 8945 | if (type |
4bac4c68 | 8946 | && TREE_CODE (t) != TYPENAME_TYPE |
6d158f4d | 8947 | && TREE_CODE (t) != IDENTIFIER_NODE |
8948 | && TREE_CODE (t) != FUNCTION_TYPE | |
8949 | && TREE_CODE (t) != METHOD_TYPE) | |
8950 | type = tsubst (type, args, complain, in_decl); | |
8951 | if (type == error_mark_node) | |
8952 | return error_mark_node; | |
4bac4c68 | 8953 | |
4bac4c68 | 8954 | switch (TREE_CODE (t)) |
8955 | { | |
8956 | case RECORD_TYPE: | |
8957 | case UNION_TYPE: | |
8958 | case ENUMERAL_TYPE: | |
6d158f4d | 8959 | return tsubst_aggr_type (t, args, complain, in_decl, |
8960 | /*entering_scope=*/0); | |
4bac4c68 | 8961 | |
8962 | case ERROR_MARK: | |
8963 | case IDENTIFIER_NODE: | |
4bac4c68 | 8964 | case VOID_TYPE: |
8965 | case REAL_TYPE: | |
8966 | case COMPLEX_TYPE: | |
90a83261 | 8967 | case VECTOR_TYPE: |
4bac4c68 | 8968 | case BOOLEAN_TYPE: |
8969 | case INTEGER_CST: | |
8970 | case REAL_CST: | |
8971 | case STRING_CST: | |
8972 | return t; | |
8973 | ||
8974 | case INTEGER_TYPE: | |
8975 | if (t == integer_type_node) | |
8976 | return t; | |
8977 | ||
8978 | if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST | |
8979 | && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST) | |
8980 | return t; | |
b0df6589 | 8981 | |
e857e9c7 | 8982 | { |
7464c809 | 8983 | tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0); |
e4e283ec | 8984 | |
657e2cb1 | 8985 | max = tsubst_expr (omax, args, complain, in_decl, |
8986 | /*integral_constant_expression_p=*/false); | |
2855a567 | 8987 | max = fold_decl_constant_value (max); |
a4caa97a | 8988 | |
df017c59 | 8989 | if (TREE_CODE (max) != INTEGER_CST |
8990 | && !at_function_scope_p () | |
8991 | && !value_dependent_expression_p (max)) | |
0e5cde0c | 8992 | { |
8993 | if (complain & tf_error) | |
8994 | error ("array bound is not an integer constant"); | |
8995 | return error_mark_node; | |
8996 | } | |
8997 | ||
2fc907bd | 8998 | /* [temp.deduct] |
6d158f4d | 8999 | |
2fc907bd | 9000 | Type deduction may fail for any of the following |
9001 | reasons: | |
6d158f4d | 9002 | |
2fc907bd | 9003 | Attempting to create an array with a size that is |
9004 | zero or negative. */ | |
9005 | if (integer_zerop (max) && !(complain & tf_error)) | |
9006 | /* We must fail if performing argument deduction (as | |
9007 | indicated by the state of complain), so that | |
9008 | another substitution can be found. */ | |
9009 | return error_mark_node; | |
2fc907bd | 9010 | else if (TREE_CODE (max) == INTEGER_CST |
9011 | && INT_CST_LT (max, integer_zero_node)) | |
9012 | { | |
4b471722 | 9013 | if (complain & tf_error) |
2fc907bd | 9014 | error ("creating array with negative size (%qE)", max); |
6d158f4d | 9015 | |
9016 | return error_mark_node; | |
71debe1f | 9017 | } |
9018 | ||
949d8cc7 | 9019 | return compute_array_index_type (NULL_TREE, max); |
4bac4c68 | 9020 | } |
9021 | ||
9022 | case TEMPLATE_TYPE_PARM: | |
9023 | case TEMPLATE_TEMPLATE_PARM: | |
1e93ca27 | 9024 | case BOUND_TEMPLATE_TEMPLATE_PARM: |
4bac4c68 | 9025 | case TEMPLATE_PARM_INDEX: |
9026 | { | |
9027 | int idx; | |
9028 | int level; | |
9029 | int levels; | |
2e3e31d2 | 9030 | tree arg = NULL_TREE; |
71debe1f | 9031 | |
9032 | r = NULL_TREE; | |
4bac4c68 | 9033 | |
2e3e31d2 | 9034 | gcc_assert (TREE_VEC_LENGTH (args) > 0); |
2bb77459 | 9035 | template_parm_level_and_index (t, &level, &idx); |
4bac4c68 | 9036 | |
2e3e31d2 | 9037 | levels = TMPL_ARGS_DEPTH (args); |
9038 | if (level <= levels) | |
d95d815d | 9039 | { |
9040 | arg = TMPL_ARG (args, level, idx); | |
9041 | ||
9042 | if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT) | |
9043 | /* See through ARGUMENT_PACK_SELECT arguments. */ | |
9044 | arg = ARGUMENT_PACK_SELECT_ARG (arg); | |
9045 | } | |
4bac4c68 | 9046 | |
2e3e31d2 | 9047 | if (arg == error_mark_node) |
9048 | return error_mark_node; | |
9049 | else if (arg != NULL_TREE) | |
9050 | { | |
d95d815d | 9051 | if (ARGUMENT_PACK_P (arg)) |
9052 | /* If ARG is an argument pack, we don't actually want to | |
9053 | perform a substitution here, because substitutions | |
9054 | for argument packs are only done | |
9055 | element-by-element. We can get to this point when | |
9056 | substituting the type of a non-type template | |
9057 | parameter pack, when that type actually contains | |
9058 | template parameter packs from an outer template, e.g., | |
9059 | ||
9060 | template<typename... Types> struct A { | |
9061 | template<Types... Values> struct B { }; | |
9062 | }; */ | |
9063 | return t; | |
9064 | ||
2e3e31d2 | 9065 | if (TREE_CODE (t) == TEMPLATE_TYPE_PARM) |
4bac4c68 | 9066 | { |
f98a9d6f | 9067 | int quals; |
2e3e31d2 | 9068 | gcc_assert (TYPE_P (arg)); |
f98a9d6f | 9069 | |
9070 | /* cv-quals from the template are discarded when | |
9071 | substituting in a function or reference type. */ | |
9072 | if (TREE_CODE (arg) == FUNCTION_TYPE | |
9073 | || TREE_CODE (arg) == METHOD_TYPE | |
9074 | || TREE_CODE (arg) == REFERENCE_TYPE) | |
9075 | quals = cp_type_quals (arg); | |
9076 | else | |
9077 | quals = cp_type_quals (arg) | cp_type_quals (t); | |
9078 | ||
2e3e31d2 | 9079 | return cp_build_qualified_type_real |
f98a9d6f | 9080 | (arg, quals, complain | tf_ignore_bad_quals); |
2e3e31d2 | 9081 | } |
9082 | else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM) | |
9083 | { | |
9084 | /* We are processing a type constructed from a | |
9085 | template template parameter. */ | |
9086 | tree argvec = tsubst (TYPE_TI_ARGS (t), | |
9087 | args, complain, in_decl); | |
9088 | if (argvec == error_mark_node) | |
9089 | return error_mark_node; | |
9031d10b | 9090 | |
2e3e31d2 | 9091 | /* We can get a TEMPLATE_TEMPLATE_PARM here when we |
9092 | are resolving nested-types in the signature of a | |
9093 | member function templates. Otherwise ARG is a | |
9094 | TEMPLATE_DECL and is the real template to be | |
9095 | instantiated. */ | |
9096 | if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM) | |
9097 | arg = TYPE_NAME (arg); | |
9031d10b | 9098 | |
9099 | r = lookup_template_class (arg, | |
9100 | argvec, in_decl, | |
2e3e31d2 | 9101 | DECL_CONTEXT (arg), |
9102 | /*entering_scope=*/0, | |
9103 | complain); | |
9104 | return cp_build_qualified_type_real | |
9105 | (r, TYPE_QUALS (t), complain); | |
4bac4c68 | 9106 | } |
2e3e31d2 | 9107 | else |
9108 | /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */ | |
9109 | return arg; | |
4bac4c68 | 9110 | } |
9111 | ||
9112 | if (level == 1) | |
9113 | /* This can happen during the attempted tsubst'ing in | |
9114 | unify. This means that we don't yet have any information | |
9115 | about the template parameter in question. */ | |
9116 | return t; | |
9117 | ||
9118 | /* If we get here, we must have been looking at a parm for a | |
9119 | more deeply nested template. Make a new version of this | |
9120 | template parameter, but with a lower level. */ | |
9121 | switch (TREE_CODE (t)) | |
9122 | { | |
9123 | case TEMPLATE_TYPE_PARM: | |
9124 | case TEMPLATE_TEMPLATE_PARM: | |
1e93ca27 | 9125 | case BOUND_TEMPLATE_TEMPLATE_PARM: |
3119c950 | 9126 | if (cp_type_quals (t)) |
4bac4c68 | 9127 | { |
6c6e3d32 | 9128 | r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl); |
653e5405 | 9129 | r = cp_build_qualified_type_real |
9130 | (r, cp_type_quals (t), | |
9edbac04 | 9131 | complain | (TREE_CODE (t) == TEMPLATE_TYPE_PARM |
9132 | ? tf_ignore_bad_quals : 0)); | |
6c6e3d32 | 9133 | } |
9134 | else | |
9135 | { | |
1d36b416 | 9136 | r = copy_type (t); |
6c6e3d32 | 9137 | TEMPLATE_TYPE_PARM_INDEX (r) |
9138 | = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t), | |
864f2267 | 9139 | r, levels, args, complain); |
6c6e3d32 | 9140 | TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r); |
9141 | TYPE_MAIN_VARIANT (r) = r; | |
9142 | TYPE_POINTER_TO (r) = NULL_TREE; | |
9143 | TYPE_REFERENCE_TO (r) = NULL_TREE; | |
9144 | ||
6753bca0 | 9145 | if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM) |
9146 | /* We have reduced the level of the template | |
9147 | template parameter, but not the levels of its | |
9148 | template parameters, so canonical_type_parameter | |
9149 | will not be able to find the canonical template | |
9150 | template parameter for this level. Thus, we | |
9151 | require structural equality checking to compare | |
9152 | TEMPLATE_TEMPLATE_PARMs. */ | |
9153 | SET_TYPE_STRUCTURAL_EQUALITY (r); | |
11b0d70e | 9154 | else if (TYPE_STRUCTURAL_EQUALITY_P (t)) |
9155 | SET_TYPE_STRUCTURAL_EQUALITY (r); | |
6753bca0 | 9156 | else |
9157 | TYPE_CANONICAL (r) = canonical_type_parameter (r); | |
9158 | ||
1e93ca27 | 9159 | if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM) |
6c6e3d32 | 9160 | { |
9161 | tree argvec = tsubst (TYPE_TI_ARGS (t), args, | |
9031d10b | 9162 | complain, in_decl); |
6c6e3d32 | 9163 | if (argvec == error_mark_node) |
9164 | return error_mark_node; | |
6d158f4d | 9165 | |
6c6e3d32 | 9166 | TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r) |
9167 | = tree_cons (TYPE_TI_TEMPLATE (t), argvec, NULL_TREE); | |
9168 | } | |
4bac4c68 | 9169 | } |
9170 | break; | |
9171 | ||
9172 | case TEMPLATE_PARM_INDEX: | |
864f2267 | 9173 | r = reduce_template_parm_level (t, type, levels, args, complain); |
4bac4c68 | 9174 | break; |
9031d10b | 9175 | |
4bac4c68 | 9176 | default: |
2e3e31d2 | 9177 | gcc_unreachable (); |
4bac4c68 | 9178 | } |
9179 | ||
e857e9c7 | 9180 | return r; |
4bac4c68 | 9181 | } |
e857e9c7 | 9182 | |
471086d6 | 9183 | case TREE_LIST: |
9184 | { | |
3046c0a3 | 9185 | tree purpose, value, chain; |
471086d6 | 9186 | |
9187 | if (t == void_list_node) | |
9188 | return t; | |
9189 | ||
471086d6 | 9190 | purpose = TREE_PURPOSE (t); |
9191 | if (purpose) | |
6d158f4d | 9192 | { |
9193 | purpose = tsubst (purpose, args, complain, in_decl); | |
9194 | if (purpose == error_mark_node) | |
9195 | return error_mark_node; | |
9196 | } | |
471086d6 | 9197 | value = TREE_VALUE (t); |
9198 | if (value) | |
6d158f4d | 9199 | { |
9200 | value = tsubst (value, args, complain, in_decl); | |
9201 | if (value == error_mark_node) | |
9202 | return error_mark_node; | |
9203 | } | |
471086d6 | 9204 | chain = TREE_CHAIN (t); |
9205 | if (chain && chain != void_type_node) | |
6d158f4d | 9206 | { |
9207 | chain = tsubst (chain, args, complain, in_decl); | |
9208 | if (chain == error_mark_node) | |
9209 | return error_mark_node; | |
9210 | } | |
471086d6 | 9211 | if (purpose == TREE_PURPOSE (t) |
9212 | && value == TREE_VALUE (t) | |
9213 | && chain == TREE_CHAIN (t)) | |
9214 | return t; | |
3046c0a3 | 9215 | return hash_tree_cons (purpose, value, chain); |
471086d6 | 9216 | } |
9031d10b | 9217 | |
3cb98335 | 9218 | case TREE_BINFO: |
5e008ac7 | 9219 | /* We should never be tsubsting a binfo. */ |
2e3e31d2 | 9220 | gcc_unreachable (); |
7a52431f | 9221 | |
3cb98335 | 9222 | case TREE_VEC: |
9223 | /* A vector of template arguments. */ | |
b4df430b | 9224 | gcc_assert (!type); |
3fa5de5b | 9225 | return tsubst_template_args (t, args, complain, in_decl); |
471086d6 | 9226 | |
471086d6 | 9227 | case POINTER_TYPE: |
9228 | case REFERENCE_TYPE: | |
9229 | { | |
471086d6 | 9230 | enum tree_code code; |
d509e46d | 9231 | |
1c4208ef | 9232 | if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE) |
471086d6 | 9233 | return t; |
9234 | ||
9235 | code = TREE_CODE (t); | |
6d158f4d | 9236 | |
9237 | ||
9238 | /* [temp.deduct] | |
9031d10b | 9239 | |
6d158f4d | 9240 | Type deduction may fail for any of the following |
9031d10b | 9241 | reasons: |
6d158f4d | 9242 | |
9243 | -- Attempting to create a pointer to reference type. | |
9244 | -- Attempting to create a reference to a reference type or | |
63949b38 | 9245 | a reference to void. |
9246 | ||
9247 | Core issue 106 says that creating a reference to a reference | |
9248 | during instantiation is no longer a cause for failure. We | |
9249 | only enforce this check in strict C++98 mode. */ | |
9250 | if ((TREE_CODE (type) == REFERENCE_TYPE | |
6dcdb5de | 9251 | && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE)) |
71debe1f | 9252 | || (code == REFERENCE_TYPE && TREE_CODE (type) == VOID_TYPE)) |
d509e46d | 9253 | { |
6a86d77b | 9254 | static location_t last_loc; |
d509e46d | 9255 | |
9256 | /* We keep track of the last time we issued this error | |
9257 | message to avoid spewing a ton of messages during a | |
9258 | single bad template instantiation. */ | |
4b471722 | 9259 | if (complain & tf_error |
9c85a98a | 9260 | && last_loc != input_location) |
d509e46d | 9261 | { |
71debe1f | 9262 | if (TREE_CODE (type) == VOID_TYPE) |
cf103c6c | 9263 | error ("forming reference to void"); |
71debe1f | 9264 | else |
39e7b4a4 | 9265 | error ("forming %s to reference type %qT", |
653e5405 | 9266 | (code == POINTER_TYPE) ? "pointer" : "reference", |
9267 | type); | |
6a86d77b | 9268 | last_loc = input_location; |
d509e46d | 9269 | } |
9270 | ||
6d158f4d | 9271 | return error_mark_node; |
d509e46d | 9272 | } |
9273 | else if (code == POINTER_TYPE) | |
1c4208ef | 9274 | { |
9275 | r = build_pointer_type (type); | |
9276 | if (TREE_CODE (type) == METHOD_TYPE) | |
9277 | r = build_ptrmemfunc_type (r); | |
9278 | } | |
63949b38 | 9279 | else if (TREE_CODE (type) == REFERENCE_TYPE) |
9280 | /* In C++0x, during template argument substitution, when there is an | |
9281 | attempt to create a reference to a reference type, reference | |
9282 | collapsing is applied as described in [14.3.1/4 temp.arg.type]: | |
9283 | ||
9284 | "If a template-argument for a template-parameter T names a type | |
9285 | that is a reference to a type A, an attempt to create the type | |
9286 | 'lvalue reference to cv T' creates the type 'lvalue reference to | |
9287 | A,' while an attempt to create the type type rvalue reference to | |
9288 | cv T' creates the type T" | |
9289 | */ | |
9290 | r = cp_build_reference_type | |
9291 | (TREE_TYPE (type), | |
9292 | TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type)); | |
471086d6 | 9293 | else |
63949b38 | 9294 | r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t)); |
e9c0ac6a | 9295 | r = cp_build_qualified_type_real (r, TYPE_QUALS (t), complain); |
d509e46d | 9296 | |
3f63ad7d | 9297 | if (r != error_mark_node) |
9298 | /* Will this ever be needed for TYPE_..._TO values? */ | |
9299 | layout_type (r); | |
9031d10b | 9300 | |
471086d6 | 9301 | return r; |
9302 | } | |
d81e00a4 | 9303 | case OFFSET_TYPE: |
71debe1f | 9304 | { |
6d158f4d | 9305 | r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl); |
95397ff9 | 9306 | if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r)) |
6d158f4d | 9307 | { |
9308 | /* [temp.deduct] | |
9309 | ||
9310 | Type deduction may fail for any of the following | |
9311 | reasons: | |
9031d10b | 9312 | |
6d158f4d | 9313 | -- Attempting to create "pointer to member of T" when T |
653e5405 | 9314 | is not a class type. */ |
4b471722 | 9315 | if (complain & tf_error) |
39e7b4a4 | 9316 | error ("creating pointer to member of non-class type %qT", r); |
6d158f4d | 9317 | return error_mark_node; |
9318 | } | |
1c4208ef | 9319 | if (TREE_CODE (type) == REFERENCE_TYPE) |
9320 | { | |
b9a2c51c | 9321 | if (complain & tf_error) |
39e7b4a4 | 9322 | error ("creating pointer to member reference type %qT", type); |
f7d1c2ea | 9323 | return error_mark_node; |
9324 | } | |
9325 | if (TREE_CODE (type) == VOID_TYPE) | |
9326 | { | |
9327 | if (complain & tf_error) | |
9328 | error ("creating pointer to member of type void"); | |
1c4208ef | 9329 | return error_mark_node; |
9330 | } | |
b4df430b | 9331 | gcc_assert (TREE_CODE (type) != METHOD_TYPE); |
1c4208ef | 9332 | if (TREE_CODE (type) == FUNCTION_TYPE) |
1bc16cab | 9333 | { |
653e5405 | 9334 | /* The type of the implicit object parameter gets its |
9335 | cv-qualifiers from the FUNCTION_TYPE. */ | |
1bc16cab | 9336 | tree method_type; |
653e5405 | 9337 | tree this_type = cp_build_qualified_type (TYPE_MAIN_VARIANT (r), |
9338 | cp_type_quals (type)); | |
9339 | tree memptr; | |
9340 | method_type = build_method_type_directly (this_type, | |
5bfb0742 | 9341 | TREE_TYPE (type), |
9342 | TYPE_ARG_TYPES (type)); | |
653e5405 | 9343 | memptr = build_ptrmemfunc_type (build_pointer_type (method_type)); |
9344 | return cp_build_qualified_type_real (memptr, cp_type_quals (t), | |
9345 | complain); | |
1bc16cab | 9346 | } |
1c4208ef | 9347 | else |
149a7f80 | 9348 | return cp_build_qualified_type_real (build_ptrmem_type (r, type), |
9349 | TYPE_QUALS (t), | |
9350 | complain); | |
71debe1f | 9351 | } |
471086d6 | 9352 | case FUNCTION_TYPE: |
9353 | case METHOD_TYPE: | |
9354 | { | |
f9670f72 | 9355 | tree fntype; |
38d89ee9 | 9356 | tree specs; |
6d158f4d | 9357 | fntype = tsubst_function_type (t, args, complain, in_decl); |
9358 | if (fntype == error_mark_node) | |
9359 | return error_mark_node; | |
ae97983e | 9360 | |
755edffd | 9361 | /* Substitute the exception specification. */ |
9031d10b | 9362 | specs = tsubst_exception_specification (t, args, complain, |
38d89ee9 | 9363 | in_decl); |
0d820f37 | 9364 | if (specs == error_mark_node) |
9365 | return error_mark_node; | |
38d89ee9 | 9366 | if (specs) |
9367 | fntype = build_exception_variant (fntype, specs); | |
f9670f72 | 9368 | return fntype; |
471086d6 | 9369 | } |
9370 | case ARRAY_TYPE: | |
9371 | { | |
6d158f4d | 9372 | tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl); |
9373 | if (domain == error_mark_node) | |
9374 | return error_mark_node; | |
9375 | ||
9376 | /* As an optimization, we avoid regenerating the array type if | |
9377 | it will obviously be the same as T. */ | |
471086d6 | 9378 | if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t)) |
9379 | return t; | |
71debe1f | 9380 | |
9031d10b | 9381 | /* These checks should match the ones in grokdeclarator. |
6d158f4d | 9382 | |
9031d10b | 9383 | [temp.deduct] |
9384 | ||
9385 | The deduction may fail for any of the following reasons: | |
6d158f4d | 9386 | |
9387 | -- Attempting to create an array with an element type that | |
9031d10b | 9388 | is void, a function type, or a reference type, or [DR337] |
71817734 | 9389 | an abstract class type. */ |
9031d10b | 9390 | if (TREE_CODE (type) == VOID_TYPE |
6d158f4d | 9391 | || TREE_CODE (type) == FUNCTION_TYPE |
9392 | || TREE_CODE (type) == REFERENCE_TYPE) | |
71debe1f | 9393 | { |
4b471722 | 9394 | if (complain & tf_error) |
39e7b4a4 | 9395 | error ("creating array of %qT", type); |
6d158f4d | 9396 | return error_mark_node; |
71debe1f | 9397 | } |
71817734 | 9398 | if (CLASS_TYPE_P (type) && CLASSTYPE_PURE_VIRTUALS (type)) |
9399 | { | |
9400 | if (complain & tf_error) | |
9031d10b | 9401 | error ("creating array of %qT, which is an abstract class type", |
71817734 | 9402 | type); |
9031d10b | 9403 | return error_mark_node; |
71817734 | 9404 | } |
71debe1f | 9405 | |
471086d6 | 9406 | r = build_cplus_array_type (type, domain); |
51d7cfb2 | 9407 | |
9408 | if (TYPE_USER_ALIGN (t)) | |
9409 | { | |
9410 | TYPE_ALIGN (r) = TYPE_ALIGN (t); | |
9411 | TYPE_USER_ALIGN (r) = 1; | |
9412 | } | |
9413 | ||
471086d6 | 9414 | return r; |
9415 | } | |
9416 | ||
471086d6 | 9417 | case PLUS_EXPR: |
e857e9c7 | 9418 | case MINUS_EXPR: |
6d158f4d | 9419 | { |
4b471722 | 9420 | tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl); |
9421 | tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl); | |
6d158f4d | 9422 | |
9423 | if (e1 == error_mark_node || e2 == error_mark_node) | |
9424 | return error_mark_node; | |
9425 | ||
b7837065 | 9426 | return fold_build2 (TREE_CODE (t), TREE_TYPE (t), e1, e2); |
6d158f4d | 9427 | } |
471086d6 | 9428 | |
9429 | case NEGATE_EXPR: | |
9430 | case NOP_EXPR: | |
6d158f4d | 9431 | { |
4b471722 | 9432 | tree e = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl); |
6d158f4d | 9433 | if (e == error_mark_node) |
9434 | return error_mark_node; | |
9435 | ||
b7837065 | 9436 | return fold_build1 (TREE_CODE (t), TREE_TYPE (t), e); |
6d158f4d | 9437 | } |
471086d6 | 9438 | |
e857e9c7 | 9439 | case TYPENAME_TYPE: |
9440 | { | |
6d158f4d | 9441 | tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain, |
9442 | in_decl, /*entering_scope=*/1); | |
9443 | tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args, | |
9031d10b | 9444 | complain, in_decl); |
6d158f4d | 9445 | |
9446 | if (ctx == error_mark_node || f == error_mark_node) | |
9447 | return error_mark_node; | |
4bac4c68 | 9448 | |
95397ff9 | 9449 | if (!MAYBE_CLASS_TYPE_P (ctx)) |
ca8f6b4d | 9450 | { |
4b471722 | 9451 | if (complain & tf_error) |
39e7b4a4 | 9452 | error ("%qT is not a class, struct, or union type", ctx); |
ca8f6b4d | 9453 | return error_mark_node; |
9454 | } | |
9455 | else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx)) | |
9456 | { | |
9457 | /* Normally, make_typename_type does not require that the CTX | |
9458 | have complete type in order to allow things like: | |
9031d10b | 9459 | |
653e5405 | 9460 | template <class T> struct S { typename S<T>::X Y; }; |
4bac4c68 | 9461 | |
ca8f6b4d | 9462 | But, such constructs have already been resolved by this |
9463 | point, so here CTX really should have complete type, unless | |
9464 | it's a partial instantiation. */ | |
6d158f4d | 9465 | ctx = complete_type (ctx); |
4b72716d | 9466 | if (!COMPLETE_TYPE_P (ctx)) |
6d158f4d | 9467 | { |
4b471722 | 9468 | if (complain & tf_error) |
1dd25100 | 9469 | cxx_incomplete_type_error (NULL_TREE, ctx); |
6d158f4d | 9470 | return error_mark_node; |
9471 | } | |
9472 | } | |
4bac4c68 | 9473 | |
e2ae55f2 | 9474 | f = make_typename_type (ctx, f, typename_type, |
9edbac04 | 9475 | (complain & tf_error) | tf_keep_type_decl); |
a43b59f9 | 9476 | if (f == error_mark_node) |
9477 | return f; | |
653e5405 | 9478 | if (TREE_CODE (f) == TYPE_DECL) |
9479 | { | |
9edbac04 | 9480 | complain |= tf_ignore_bad_quals; |
653e5405 | 9481 | f = TREE_TYPE (f); |
9482 | } | |
9031d10b | 9483 | |
e2ae55f2 | 9484 | if (TREE_CODE (f) != TYPENAME_TYPE) |
9485 | { | |
9486 | if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE) | |
9031d10b | 9487 | error ("%qT resolves to %qT, which is not an enumeration type", |
e2ae55f2 | 9488 | t, f); |
9489 | else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f)) | |
9031d10b | 9490 | error ("%qT resolves to %qT, which is is not a class type", |
e2ae55f2 | 9491 | t, f); |
9492 | } | |
9493 | ||
653e5405 | 9494 | return cp_build_qualified_type_real |
9495 | (f, cp_type_quals (f) | cp_type_quals (t), complain); | |
e857e9c7 | 9496 | } |
9031d10b | 9497 | |
47d727d4 | 9498 | case UNBOUND_CLASS_TEMPLATE: |
9499 | { | |
9500 | tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain, | |
9501 | in_decl, /*entering_scope=*/1); | |
9502 | tree name = TYPE_IDENTIFIER (t); | |
f95fba26 | 9503 | tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t)); |
47d727d4 | 9504 | |
9505 | if (ctx == error_mark_node || name == error_mark_node) | |
9506 | return error_mark_node; | |
9507 | ||
f95fba26 | 9508 | if (parm_list) |
9509 | parm_list = tsubst_template_parms (parm_list, args, complain); | |
9510 | return make_unbound_class_template (ctx, name, parm_list, complain); | |
47d727d4 | 9511 | } |
9512 | ||
e857e9c7 | 9513 | case INDIRECT_REF: |
e857e9c7 | 9514 | case ADDR_EXPR: |
3046c0a3 | 9515 | case CALL_EXPR: |
2e3e31d2 | 9516 | gcc_unreachable (); |
e857e9c7 | 9517 | |
9518 | case ARRAY_REF: | |
6d158f4d | 9519 | { |
4b471722 | 9520 | tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl); |
bde9ebf7 | 9521 | tree e2 = tsubst_expr (TREE_OPERAND (t, 1), args, complain, in_decl, |
9522 | /*integral_constant_expression_p=*/false); | |
6d158f4d | 9523 | if (e1 == error_mark_node || e2 == error_mark_node) |
9524 | return error_mark_node; | |
9525 | ||
6374121b | 9526 | return build_nt (ARRAY_REF, e1, e2, NULL_TREE, NULL_TREE); |
6d158f4d | 9527 | } |
e857e9c7 | 9528 | |
d2a15a12 | 9529 | case SCOPE_REF: |
6d158f4d | 9530 | { |
4b471722 | 9531 | tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl); |
6d158f4d | 9532 | tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl); |
9533 | if (e1 == error_mark_node || e2 == error_mark_node) | |
9534 | return error_mark_node; | |
9535 | ||
fbb01da7 | 9536 | return build_qualified_name (/*type=*/NULL_TREE, |
9537 | e1, e2, QUALIFIED_NAME_IS_TEMPLATE (t)); | |
6d158f4d | 9538 | } |
d2a15a12 | 9539 | |
63e22e88 | 9540 | case TYPEOF_TYPE: |
6d158f4d | 9541 | { |
e960d39c | 9542 | tree type; |
6d158f4d | 9543 | |
bde9ebf7 | 9544 | type = finish_typeof (tsubst_expr |
9545 | (TYPEOF_TYPE_EXPR (t), args, | |
9546 | complain, in_decl, | |
9547 | /*integral_constant_expression_p=*/false)); | |
e960d39c | 9548 | return cp_build_qualified_type_real (type, |
c896502a | 9549 | cp_type_quals (t) |
e960d39c | 9550 | | cp_type_quals (type), |
c896502a | 9551 | complain); |
6d158f4d | 9552 | } |
63e22e88 | 9553 | |
34da8800 | 9554 | case DECLTYPE_TYPE: |
9555 | { | |
9556 | tree type; | |
9557 | ||
9558 | type = | |
9559 | finish_decltype_type (tsubst_expr | |
9560 | (DECLTYPE_TYPE_EXPR (t), args, | |
9561 | complain, in_decl, | |
9562 | /*integral_constant_expression_p=*/false), | |
9563 | DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t)); | |
9564 | return cp_build_qualified_type_real (type, | |
9565 | cp_type_quals (t) | |
9566 | | cp_type_quals (type), | |
9567 | complain); | |
9568 | } | |
9569 | ||
d95d815d | 9570 | case TYPE_ARGUMENT_PACK: |
9571 | case NONTYPE_ARGUMENT_PACK: | |
9572 | { | |
9573 | tree r = make_node (TREE_CODE (t)); | |
9574 | tree packed_out = | |
9575 | tsubst_template_args (ARGUMENT_PACK_ARGS (t), | |
9576 | args, | |
9577 | complain, | |
9578 | in_decl); | |
9579 | SET_ARGUMENT_PACK_ARGS (r, packed_out); | |
9580 | ||
9581 | /* For template nontype argument packs, also substitute into | |
9582 | the type. */ | |
9583 | if (TREE_CODE (t) == NONTYPE_ARGUMENT_PACK) | |
9584 | TREE_TYPE (r) = tsubst (TREE_TYPE (t), args, complain, in_decl); | |
9585 | ||
9586 | return r; | |
9587 | } | |
9588 | break; | |
9589 | ||
471086d6 | 9590 | default: |
1e5fcbe2 | 9591 | sorry ("use of %qs in template", |
471086d6 | 9592 | tree_code_name [(int) TREE_CODE (t)]); |
9593 | return error_mark_node; | |
9594 | } | |
9595 | } | |
9596 | ||
ef4534a3 | 9597 | /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the |
9598 | type of the expression on the left-hand side of the "." or "->" | |
9599 | operator. */ | |
9600 | ||
9601 | static tree | |
9602 | tsubst_baselink (tree baselink, tree object_type, | |
9603 | tree args, tsubst_flags_t complain, tree in_decl) | |
9604 | { | |
9605 | tree name; | |
9606 | tree qualifying_scope; | |
9607 | tree fns; | |
d85b6f37 | 9608 | tree optype; |
ef4534a3 | 9609 | tree template_args = 0; |
9610 | bool template_id_p = false; | |
9611 | ||
6b5d7622 | 9612 | /* A baselink indicates a function from a base class. Both the |
9613 | BASELINK_ACCESS_BINFO and the base class referenced may | |
9614 | indicate bases of the template class, rather than the | |
9615 | instantiated class. In addition, lookups that were not | |
9616 | ambiguous before may be ambiguous now. Therefore, we perform | |
9617 | the lookup again. */ | |
ef4534a3 | 9618 | qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink)); |
6b5d7622 | 9619 | qualifying_scope = tsubst (qualifying_scope, args, |
9620 | complain, in_decl); | |
ef4534a3 | 9621 | fns = BASELINK_FUNCTIONS (baselink); |
d85b6f37 | 9622 | optype = BASELINK_OPTYPE (baselink); |
ef4534a3 | 9623 | if (TREE_CODE (fns) == TEMPLATE_ID_EXPR) |
9624 | { | |
9625 | template_id_p = true; | |
9626 | template_args = TREE_OPERAND (fns, 1); | |
9627 | fns = TREE_OPERAND (fns, 0); | |
b5959ba9 | 9628 | if (template_args) |
9629 | template_args = tsubst_template_args (template_args, args, | |
9630 | complain, in_decl); | |
ef4534a3 | 9631 | } |
9632 | name = DECL_NAME (get_first_fn (fns)); | |
9633 | baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1); | |
9031d10b | 9634 | |
3771c712 | 9635 | /* If lookup found a single function, mark it as used at this |
9636 | point. (If it lookup found multiple functions the one selected | |
9637 | later by overload resolution will be marked as used at that | |
9638 | point.) */ | |
9639 | if (BASELINK_P (baselink)) | |
9640 | fns = BASELINK_FUNCTIONS (baselink); | |
9641 | if (!template_id_p && !really_overloaded_fn (fns)) | |
9642 | mark_used (OVL_CURRENT (fns)); | |
9643 | ||
9644 | /* Add back the template arguments, if present. */ | |
ef4534a3 | 9645 | if (BASELINK_P (baselink) && template_id_p) |
9031d10b | 9646 | BASELINK_FUNCTIONS (baselink) |
ef4534a3 | 9647 | = build_nt (TEMPLATE_ID_EXPR, |
9648 | BASELINK_FUNCTIONS (baselink), | |
9649 | template_args); | |
d85b6f37 | 9650 | /* Update the conversion operator type. */ |
9651 | BASELINK_OPTYPE (baselink) | |
9652 | = tsubst (optype, args, complain, in_decl); | |
3771c712 | 9653 | |
ef4534a3 | 9654 | if (!object_type) |
9655 | object_type = current_class_type; | |
9031d10b | 9656 | return adjust_result_of_qualified_name_lookup (baselink, |
ef4534a3 | 9657 | qualifying_scope, |
9658 | object_type); | |
9659 | } | |
9660 | ||
9661 | /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is | |
9662 | true if the qualified-id will be a postfix-expression in-and-of | |
9663 | itself; false if more of the postfix-expression follows the | |
9664 | QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand | |
9665 | of "&". */ | |
9666 | ||
9667 | static tree | |
9031d10b | 9668 | tsubst_qualified_id (tree qualified_id, tree args, |
ef4534a3 | 9669 | tsubst_flags_t complain, tree in_decl, |
9670 | bool done, bool address_p) | |
9671 | { | |
9672 | tree expr; | |
9673 | tree scope; | |
9674 | tree name; | |
9675 | bool is_template; | |
9676 | tree template_args; | |
9677 | ||
b4df430b | 9678 | gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF); |
ef4534a3 | 9679 | |
ef4534a3 | 9680 | /* Figure out what name to look up. */ |
9681 | name = TREE_OPERAND (qualified_id, 1); | |
9682 | if (TREE_CODE (name) == TEMPLATE_ID_EXPR) | |
9683 | { | |
9684 | is_template = true; | |
b5959ba9 | 9685 | template_args = TREE_OPERAND (name, 1); |
9686 | if (template_args) | |
9687 | template_args = tsubst_template_args (template_args, args, | |
9688 | complain, in_decl); | |
ef4534a3 | 9689 | name = TREE_OPERAND (name, 0); |
9690 | } | |
9691 | else | |
9692 | { | |
9693 | is_template = false; | |
9694 | template_args = NULL_TREE; | |
9695 | } | |
9696 | ||
877a501e | 9697 | /* Substitute into the qualifying scope. When there are no ARGS, we |
9698 | are just trying to simplify a non-dependent expression. In that | |
9699 | case the qualifying scope may be dependent, and, in any case, | |
9700 | substituting will not help. */ | |
9701 | scope = TREE_OPERAND (qualified_id, 0); | |
9702 | if (args) | |
9703 | { | |
9704 | scope = tsubst (scope, args, complain, in_decl); | |
9705 | expr = tsubst_copy (name, args, complain, in_decl); | |
9706 | } | |
9707 | else | |
9708 | expr = name; | |
c08d51be | 9709 | |
a8f49272 | 9710 | if (dependent_type_p (scope)) |
074ab442 | 9711 | return build_qualified_name (/*type=*/NULL_TREE, |
9712 | scope, expr, | |
fbb01da7 | 9713 | QUALIFIED_NAME_IS_TEMPLATE (qualified_id)); |
9031d10b | 9714 | |
2be3e2ee | 9715 | if (!BASELINK_P (name) && !DECL_P (expr)) |
c4cf31e6 | 9716 | { |
f3d3cc67 | 9717 | if (TREE_CODE (expr) == BIT_NOT_EXPR) |
9718 | /* If this were actually a destructor call, it would have been | |
9719 | parsed as such by the parser. */ | |
9720 | expr = error_mark_node; | |
9721 | else | |
9722 | expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false); | |
c4cf31e6 | 9723 | if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL |
9724 | ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL) | |
9725 | { | |
9726 | if (complain & tf_error) | |
4b288940 | 9727 | { |
39e7b4a4 | 9728 | error ("dependent-name %qE is parsed as a non-type, but " |
4b288940 | 9729 | "instantiation yields a type", qualified_id); |
39e7b4a4 | 9730 | inform ("say %<typename %E%> if a type is meant", qualified_id); |
4b288940 | 9731 | } |
c4cf31e6 | 9732 | return error_mark_node; |
9733 | } | |
9734 | } | |
9031d10b | 9735 | |
e351c854 | 9736 | if (DECL_P (expr)) |
2cdbcd51 | 9737 | { |
9738 | check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE, | |
9739 | scope); | |
9740 | /* Remember that there was a reference to this entity. */ | |
9741 | mark_used (expr); | |
9742 | } | |
9743 | ||
9744 | if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST) | |
9745 | { | |
9746 | if (complain & tf_error) | |
9031d10b | 9747 | qualified_name_lookup_error (scope, |
2cdbcd51 | 9748 | TREE_OPERAND (qualified_id, 1), |
9749 | expr); | |
9750 | return error_mark_node; | |
9751 | } | |
ef4534a3 | 9752 | |
9753 | if (is_template) | |
c08d51be | 9754 | expr = lookup_template_function (expr, template_args); |
ef4534a3 | 9755 | |
c75a55a8 | 9756 | if (expr == error_mark_node && complain & tf_error) |
2cdbcd51 | 9757 | qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1), |
9758 | expr); | |
c75a55a8 | 9759 | else if (TYPE_P (scope)) |
ef4534a3 | 9760 | { |
9031d10b | 9761 | expr = (adjust_result_of_qualified_name_lookup |
ef4534a3 | 9762 | (expr, scope, current_class_type)); |
074ab442 | 9763 | expr = (finish_qualified_id_expr |
fbb01da7 | 9764 | (scope, expr, done, address_p, |
9765 | QUALIFIED_NAME_IS_TEMPLATE (qualified_id), | |
9766 | /*template_arg_p=*/false)); | |
ef4534a3 | 9767 | } |
9031d10b | 9768 | |
bde9ebf7 | 9769 | /* Expressions do not generally have reference type. */ |
9770 | if (TREE_CODE (expr) != SCOPE_REF | |
9771 | /* However, if we're about to form a pointer-to-member, we just | |
9772 | want the referenced member referenced. */ | |
9773 | && TREE_CODE (expr) != OFFSET_REF) | |
f05b9c1f | 9774 | expr = convert_from_reference (expr); |
ef4534a3 | 9775 | |
9776 | return expr; | |
9777 | } | |
9778 | ||
73645519 | 9779 | /* Like tsubst, but deals with expressions. This function just replaces |
9780 | template parms; to finish processing the resultant expression, use | |
9781 | tsubst_expr. */ | |
9782 | ||
5f6526e1 | 9783 | static tree |
807be5b4 | 9784 | tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl) |
e857e9c7 | 9785 | { |
9786 | enum tree_code code; | |
9df97129 | 9787 | tree r; |
471086d6 | 9788 | |
e857e9c7 | 9789 | if (t == NULL_TREE || t == error_mark_node) |
9790 | return t; | |
9791 | ||
9792 | code = TREE_CODE (t); | |
1e66592c | 9793 | |
e857e9c7 | 9794 | switch (code) |
9795 | { | |
9796 | case PARM_DECL: | |
0a3b29ad | 9797 | r = retrieve_local_specialization (t); |
b4df430b | 9798 | gcc_assert (r != NULL); |
d95d815d | 9799 | if (TREE_CODE (r) == ARGUMENT_PACK_SELECT) |
9800 | r = ARGUMENT_PACK_SELECT_ARG (r); | |
1940f978 | 9801 | mark_used (r); |
0a3b29ad | 9802 | return r; |
e857e9c7 | 9803 | |
9804 | case CONST_DECL: | |
11fa0698 | 9805 | { |
9806 | tree enum_type; | |
9807 | tree v; | |
9808 | ||
0a3b29ad | 9809 | if (DECL_TEMPLATE_PARM_P (t)) |
9810 | return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl); | |
ee1ab431 | 9811 | /* There is no need to substitute into namespace-scope |
9812 | enumerators. */ | |
9813 | if (DECL_NAMESPACE_SCOPE_P (t)) | |
11fa0698 | 9814 | return t; |
3ad0e8cc | 9815 | /* If ARGS is NULL, then T is known to be non-dependent. */ |
9816 | if (args == NULL_TREE) | |
13f0eb20 | 9817 | return integral_constant_value (t); |
11fa0698 | 9818 | |
9819 | /* Unfortunately, we cannot just call lookup_name here. | |
70a658bd | 9820 | Consider: |
9031d10b | 9821 | |
70a658bd | 9822 | template <int I> int f() { |
9823 | enum E { a = I }; | |
9824 | struct S { void g() { E e = a; } }; | |
9825 | }; | |
9031d10b | 9826 | |
70a658bd | 9827 | When we instantiate f<7>::S::g(), say, lookup_name is not |
9828 | clever enough to find f<7>::a. */ | |
9031d10b | 9829 | enum_type |
9830 | = tsubst_aggr_type (TREE_TYPE (t), args, complain, in_decl, | |
11fa0698 | 9831 | /*entering_scope=*/0); |
9832 | ||
9031d10b | 9833 | for (v = TYPE_VALUES (enum_type); |
9834 | v != NULL_TREE; | |
11fa0698 | 9835 | v = TREE_CHAIN (v)) |
9836 | if (TREE_PURPOSE (v) == DECL_NAME (t)) | |
9837 | return TREE_VALUE (v); | |
9838 | ||
9839 | /* We didn't find the name. That should never happen; if | |
9840 | name-lookup found it during preliminary parsing, we | |
9841 | should find it again here during instantiation. */ | |
2e3e31d2 | 9842 | gcc_unreachable (); |
11fa0698 | 9843 | } |
3acec327 | 9844 | return t; |
11fa0698 | 9845 | |
e857e9c7 | 9846 | case FIELD_DECL: |
9847 | if (DECL_CONTEXT (t)) | |
9848 | { | |
808093fb | 9849 | tree ctx; |
808093fb | 9850 | |
6d158f4d | 9851 | ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl, |
34197853 | 9852 | /*entering_scope=*/1); |
808093fb | 9853 | if (ctx != DECL_CONTEXT (t)) |
9a4312bd | 9854 | { |
9855 | tree r = lookup_field (ctx, DECL_NAME (t), 0, false); | |
9856 | if (!r) | |
9857 | { | |
9858 | if (complain & tf_error) | |
9859 | error ("using invalid field %qD", t); | |
9860 | return error_mark_node; | |
9861 | } | |
9862 | return r; | |
9863 | } | |
e857e9c7 | 9864 | } |
9031d10b | 9865 | |
e857e9c7 | 9866 | return t; |
9867 | ||
9868 | case VAR_DECL: | |
9869 | case FUNCTION_DECL: | |
0a3b29ad | 9870 | if ((DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)) |
9871 | || local_variable_p (t)) | |
6d158f4d | 9872 | t = tsubst (t, args, complain, in_decl); |
e857e9c7 | 9873 | mark_used (t); |
9874 | return t; | |
9875 | ||
0a3b29ad | 9876 | case BASELINK: |
ef4534a3 | 9877 | return tsubst_baselink (t, current_class_type, args, complain, in_decl); |
0a3b29ad | 9878 | |
64b4f183 | 9879 | case TEMPLATE_DECL: |
0a3b29ad | 9880 | if (DECL_TEMPLATE_TEMPLATE_PARM_P (t)) |
9031d10b | 9881 | return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)), |
0a3b29ad | 9882 | args, complain, in_decl); |
38d89ee9 | 9883 | else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t)) |
6d158f4d | 9884 | return tsubst (t, args, complain, in_decl); |
107c6afb | 9885 | else if (DECL_CLASS_SCOPE_P (t) |
9886 | && uses_template_parms (DECL_CONTEXT (t))) | |
9887 | { | |
9888 | /* Template template argument like the following example need | |
9889 | special treatment: | |
9890 | ||
9891 | template <template <class> class TT> struct C {}; | |
9892 | template <class T> struct D { | |
9893 | template <class U> struct E {}; | |
653e5405 | 9894 | C<E> c; // #1 |
107c6afb | 9895 | }; |
9896 | D<int> d; // #2 | |
9897 | ||
9898 | We are processing the template argument `E' in #1 for | |
9899 | the template instantiation #2. Originally, `E' is a | |
9900 | TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we | |
9901 | have to substitute this with one having context `D<int>'. */ | |
9902 | ||
9903 | tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl); | |
9904 | return lookup_field (context, DECL_NAME(t), 0, false); | |
9905 | } | |
64b4f183 | 9906 | else |
107c6afb | 9907 | /* Ordinary template template argument. */ |
64b4f183 | 9908 | return t; |
9909 | ||
e857e9c7 | 9910 | case CAST_EXPR: |
9911 | case REINTERPRET_CAST_EXPR: | |
96624a9e | 9912 | case CONST_CAST_EXPR: |
9913 | case STATIC_CAST_EXPR: | |
9914 | case DYNAMIC_CAST_EXPR: | |
2917b86f | 9915 | case NOP_EXPR: |
e857e9c7 | 9916 | return build1 |
6d158f4d | 9917 | (code, tsubst (TREE_TYPE (t), args, complain, in_decl), |
9918 | tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl)); | |
e857e9c7 | 9919 | |
d95d815d | 9920 | case SIZEOF_EXPR: |
9921 | if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))) | |
9922 | { | |
9923 | /* We only want to compute the number of arguments. */ | |
9924 | tree expanded = tsubst_pack_expansion (TREE_OPERAND (t, 0), args, | |
9925 | complain, in_decl); | |
66c14530 | 9926 | int len = 0; |
83b01f73 | 9927 | |
9928 | if (TREE_CODE (expanded) == TREE_VEC) | |
9929 | len = TREE_VEC_LENGTH (expanded); | |
9930 | ||
1db95a0f | 9931 | if (expanded == error_mark_node) |
9932 | return error_mark_node; | |
83b01f73 | 9933 | else if (PACK_EXPANSION_P (expanded) |
9934 | || (TREE_CODE (expanded) == TREE_VEC | |
9935 | && len > 0 | |
9936 | && PACK_EXPANSION_P (TREE_VEC_ELT (expanded, len-1)))) | |
9937 | { | |
9938 | if (TREE_CODE (expanded) == TREE_VEC) | |
9939 | expanded = TREE_VEC_ELT (expanded, len - 1); | |
9940 | ||
9941 | if (TYPE_P (expanded)) | |
9942 | return cxx_sizeof_or_alignof_type (expanded, SIZEOF_EXPR, | |
9943 | complain & tf_error); | |
9944 | else | |
9945 | return cxx_sizeof_or_alignof_expr (expanded, SIZEOF_EXPR, | |
9946 | complain & tf_error); | |
9947 | } | |
9948 | else | |
9949 | return build_int_cst (size_type_node, len); | |
d95d815d | 9950 | } |
9951 | /* Fall through */ | |
9952 | ||
e857e9c7 | 9953 | case INDIRECT_REF: |
e857e9c7 | 9954 | case NEGATE_EXPR: |
9955 | case TRUTH_NOT_EXPR: | |
dc9c6fd7 | 9956 | case BIT_NOT_EXPR: |
e857e9c7 | 9957 | case ADDR_EXPR: |
97d541d5 | 9958 | case UNARY_PLUS_EXPR: /* Unary + */ |
dd53deb4 | 9959 | case ALIGNOF_EXPR: |
e857e9c7 | 9960 | case ARROW_EXPR: |
d2a15a12 | 9961 | case THROW_EXPR: |
3cc0b4b9 | 9962 | case TYPEID_EXPR: |
eb25810f | 9963 | case REALPART_EXPR: |
9964 | case IMAGPART_EXPR: | |
e857e9c7 | 9965 | return build1 |
a731c87f | 9966 | (code, tsubst (TREE_TYPE (t), args, complain, in_decl), |
6d158f4d | 9967 | tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl)); |
e857e9c7 | 9968 | |
0a3b29ad | 9969 | case COMPONENT_REF: |
9970 | { | |
9971 | tree object; | |
9972 | tree name; | |
9973 | ||
9974 | object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl); | |
9975 | name = TREE_OPERAND (t, 1); | |
9031d10b | 9976 | if (TREE_CODE (name) == BIT_NOT_EXPR) |
0a3b29ad | 9977 | { |
9978 | name = tsubst_copy (TREE_OPERAND (name, 0), args, | |
9979 | complain, in_decl); | |
9980 | name = build1 (BIT_NOT_EXPR, NULL_TREE, name); | |
9981 | } | |
9982 | else if (TREE_CODE (name) == SCOPE_REF | |
9983 | && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR) | |
9984 | { | |
9985 | tree base = tsubst_copy (TREE_OPERAND (name, 0), args, | |
9986 | complain, in_decl); | |
9987 | name = TREE_OPERAND (name, 1); | |
9988 | name = tsubst_copy (TREE_OPERAND (name, 0), args, | |
9989 | complain, in_decl); | |
9990 | name = build1 (BIT_NOT_EXPR, NULL_TREE, name); | |
fbb01da7 | 9991 | name = build_qualified_name (/*type=*/NULL_TREE, |
074ab442 | 9992 | base, name, |
fbb01da7 | 9993 | /*template_p=*/false); |
0a3b29ad | 9994 | } |
ef4534a3 | 9995 | else if (TREE_CODE (name) == BASELINK) |
9031d10b | 9996 | name = tsubst_baselink (name, |
9997 | non_reference (TREE_TYPE (object)), | |
9998 | args, complain, | |
ef4534a3 | 9999 | in_decl); |
0a3b29ad | 10000 | else |
ef4534a3 | 10001 | name = tsubst_copy (name, args, complain, in_decl); |
6374121b | 10002 | return build_nt (COMPONENT_REF, object, name, NULL_TREE); |
0a3b29ad | 10003 | } |
10004 | ||
e857e9c7 | 10005 | case PLUS_EXPR: |
10006 | case MINUS_EXPR: | |
10007 | case MULT_EXPR: | |
10008 | case TRUNC_DIV_EXPR: | |
10009 | case CEIL_DIV_EXPR: | |
10010 | case FLOOR_DIV_EXPR: | |
10011 | case ROUND_DIV_EXPR: | |
10012 | case EXACT_DIV_EXPR: | |
10013 | case BIT_AND_EXPR: | |
e857e9c7 | 10014 | case BIT_IOR_EXPR: |
10015 | case BIT_XOR_EXPR: | |
10016 | case TRUNC_MOD_EXPR: | |
10017 | case FLOOR_MOD_EXPR: | |
10018 | case TRUTH_ANDIF_EXPR: | |
10019 | case TRUTH_ORIF_EXPR: | |
10020 | case TRUTH_AND_EXPR: | |
10021 | case TRUTH_OR_EXPR: | |
10022 | case RSHIFT_EXPR: | |
10023 | case LSHIFT_EXPR: | |
10024 | case RROTATE_EXPR: | |
10025 | case LROTATE_EXPR: | |
10026 | case EQ_EXPR: | |
10027 | case NE_EXPR: | |
10028 | case MAX_EXPR: | |
10029 | case MIN_EXPR: | |
10030 | case LE_EXPR: | |
10031 | case GE_EXPR: | |
10032 | case LT_EXPR: | |
10033 | case GT_EXPR: | |
e857e9c7 | 10034 | case COMPOUND_EXPR: |
e857e9c7 | 10035 | case DOTSTAR_EXPR: |
10036 | case MEMBER_REF: | |
abca5032 | 10037 | case PREDECREMENT_EXPR: |
10038 | case PREINCREMENT_EXPR: | |
10039 | case POSTDECREMENT_EXPR: | |
10040 | case POSTINCREMENT_EXPR: | |
e857e9c7 | 10041 | return build_nt |
6d158f4d | 10042 | (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl), |
10043 | tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl)); | |
e857e9c7 | 10044 | |
fbb01da7 | 10045 | case SCOPE_REF: |
10046 | return build_qualified_name (/*type=*/NULL_TREE, | |
10047 | tsubst_copy (TREE_OPERAND (t, 0), | |
10048 | args, complain, in_decl), | |
10049 | tsubst_copy (TREE_OPERAND (t, 1), | |
10050 | args, complain, in_decl), | |
10051 | QUALIFIED_NAME_IS_TEMPLATE (t)); | |
10052 | ||
5fcd398b | 10053 | case ARRAY_REF: |
10054 | return build_nt | |
10055 | (ARRAY_REF, | |
10056 | tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl), | |
10057 | tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl), | |
10058 | NULL_TREE, NULL_TREE); | |
10059 | ||
e857e9c7 | 10060 | case CALL_EXPR: |
c2f47e15 | 10061 | { |
10062 | int n = VL_EXP_OPERAND_LENGTH (t); | |
10063 | tree result = build_vl_exp (CALL_EXPR, n); | |
10064 | int i; | |
10065 | for (i = 0; i < n; i++) | |
10066 | TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args, | |
10067 | complain, in_decl); | |
10068 | return result; | |
10069 | } | |
e857e9c7 | 10070 | |
e857e9c7 | 10071 | case COND_EXPR: |
10072 | case MODOP_EXPR: | |
8318ad7a | 10073 | case PSEUDO_DTOR_EXPR: |
c87151cf | 10074 | { |
9df97129 | 10075 | r = build_nt |
6d158f4d | 10076 | (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl), |
10077 | tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl), | |
10078 | tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl)); | |
ef1a5b81 | 10079 | TREE_NO_WARNING (r) = TREE_NO_WARNING (t); |
c87151cf | 10080 | return r; |
10081 | } | |
e857e9c7 | 10082 | |
10083 | case NEW_EXPR: | |
10084 | { | |
9df97129 | 10085 | r = build_nt |
6d158f4d | 10086 | (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl), |
10087 | tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl), | |
10088 | tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl)); | |
e857e9c7 | 10089 | NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t); |
10090 | return r; | |
10091 | } | |
10092 | ||
10093 | case DELETE_EXPR: | |
10094 | { | |
9df97129 | 10095 | r = build_nt |
6d158f4d | 10096 | (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl), |
10097 | tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl)); | |
e857e9c7 | 10098 | DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t); |
10099 | DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t); | |
10100 | return r; | |
10101 | } | |
10102 | ||
b1cfe2be | 10103 | case TEMPLATE_ID_EXPR: |
10104 | { | |
653e5405 | 10105 | /* Substituted template arguments */ |
3fa5de5b | 10106 | tree fn = TREE_OPERAND (t, 0); |
10107 | tree targs = TREE_OPERAND (t, 1); | |
8a5f292f | 10108 | |
3fa5de5b | 10109 | fn = tsubst_copy (fn, args, complain, in_decl); |
b5959ba9 | 10110 | if (targs) |
10111 | targs = tsubst_template_args (targs, args, complain, in_decl); | |
9031d10b | 10112 | |
3fa5de5b | 10113 | return lookup_template_function (fn, targs); |
b1cfe2be | 10114 | } |
10115 | ||
e857e9c7 | 10116 | case TREE_LIST: |
10117 | { | |
10118 | tree purpose, value, chain; | |
10119 | ||
10120 | if (t == void_list_node) | |
10121 | return t; | |
10122 | ||
10123 | purpose = TREE_PURPOSE (t); | |
10124 | if (purpose) | |
6d158f4d | 10125 | purpose = tsubst_copy (purpose, args, complain, in_decl); |
e857e9c7 | 10126 | value = TREE_VALUE (t); |
10127 | if (value) | |
6d158f4d | 10128 | value = tsubst_copy (value, args, complain, in_decl); |
e857e9c7 | 10129 | chain = TREE_CHAIN (t); |
10130 | if (chain && chain != void_type_node) | |
6d158f4d | 10131 | chain = tsubst_copy (chain, args, complain, in_decl); |
e857e9c7 | 10132 | if (purpose == TREE_PURPOSE (t) |
10133 | && value == TREE_VALUE (t) | |
10134 | && chain == TREE_CHAIN (t)) | |
10135 | return t; | |
10136 | return tree_cons (purpose, value, chain); | |
10137 | } | |
10138 | ||
10139 | case RECORD_TYPE: | |
10140 | case UNION_TYPE: | |
10141 | case ENUMERAL_TYPE: | |
10142 | case INTEGER_TYPE: | |
10143 | case TEMPLATE_TYPE_PARM: | |
cd5dd2d8 | 10144 | case TEMPLATE_TEMPLATE_PARM: |
1e93ca27 | 10145 | case BOUND_TEMPLATE_TEMPLATE_PARM: |
f3110581 | 10146 | case TEMPLATE_PARM_INDEX: |
e857e9c7 | 10147 | case POINTER_TYPE: |
10148 | case REFERENCE_TYPE: | |
10149 | case OFFSET_TYPE: | |
10150 | case FUNCTION_TYPE: | |
10151 | case METHOD_TYPE: | |
10152 | case ARRAY_TYPE: | |
10153 | case TYPENAME_TYPE: | |
47d727d4 | 10154 | case UNBOUND_CLASS_TEMPLATE: |
5143d60f | 10155 | case TYPEOF_TYPE: |
34da8800 | 10156 | case DECLTYPE_TYPE: |
f3110581 | 10157 | case TYPE_DECL: |
6d158f4d | 10158 | return tsubst (t, args, complain, in_decl); |
e857e9c7 | 10159 | |
96624a9e | 10160 | case IDENTIFIER_NODE: |
f0edcca6 | 10161 | if (IDENTIFIER_TYPENAME_P (t)) |
98eaf693 | 10162 | { |
10163 | tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl); | |
606b494c | 10164 | return mangle_conv_op_name_for_type (new_type); |
98eaf693 | 10165 | } |
96624a9e | 10166 | else |
10167 | return t; | |
10168 | ||
3cc0b4b9 | 10169 | case CONSTRUCTOR: |
c75b4594 | 10170 | /* This is handled by tsubst_copy_and_build. */ |
10171 | gcc_unreachable (); | |
3cc0b4b9 | 10172 | |
0407a27f | 10173 | case VA_ARG_EXPR: |
f64ede8c | 10174 | return build_x_va_arg (tsubst_copy (TREE_OPERAND (t, 0), args, complain, |
4b471722 | 10175 | in_decl), |
10176 | tsubst (TREE_TYPE (t), args, complain, in_decl)); | |
b0944a40 | 10177 | |
73d4090e | 10178 | case CLEANUP_POINT_EXPR: |
10179 | /* We shouldn't have built any of these during initial template | |
10180 | generation. Instead, they should be built during instantiation | |
10181 | in response to the saved STMT_IS_FULL_EXPR_P setting. */ | |
2e3e31d2 | 10182 | gcc_unreachable (); |
73d4090e | 10183 | |
0638c434 | 10184 | case OFFSET_REF: |
10185 | mark_used (TREE_OPERAND (t, 1)); | |
10186 | return t; | |
10187 | ||
d95d815d | 10188 | case EXPR_PACK_EXPANSION: |
10189 | error ("invalid use of pack expansion expression"); | |
10190 | return error_mark_node; | |
10191 | ||
10192 | case NONTYPE_ARGUMENT_PACK: | |
10193 | error ("use %<...%> to expand argument pack"); | |
10194 | return error_mark_node; | |
10195 | ||
e857e9c7 | 10196 | default: |
10197 | return t; | |
10198 | } | |
10199 | } | |
10200 | ||
8487df40 | 10201 | /* Like tsubst_copy, but specifically for OpenMP clauses. */ |
10202 | ||
10203 | static tree | |
10204 | tsubst_omp_clauses (tree clauses, tree args, tsubst_flags_t complain, | |
10205 | tree in_decl) | |
10206 | { | |
10207 | tree new_clauses = NULL, nc, oc; | |
10208 | ||
10209 | for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc)) | |
10210 | { | |
10211 | nc = copy_node (oc); | |
10212 | OMP_CLAUSE_CHAIN (nc) = new_clauses; | |
10213 | new_clauses = nc; | |
10214 | ||
10215 | switch (OMP_CLAUSE_CODE (nc)) | |
10216 | { | |
fd6481cf | 10217 | case OMP_CLAUSE_LASTPRIVATE: |
10218 | if (OMP_CLAUSE_LASTPRIVATE_STMT (oc)) | |
10219 | { | |
10220 | OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list (); | |
10221 | tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain, | |
10222 | in_decl, /*integral_constant_expression_p=*/false); | |
10223 | OMP_CLAUSE_LASTPRIVATE_STMT (nc) | |
10224 | = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc)); | |
10225 | } | |
10226 | /* FALLTHRU */ | |
8487df40 | 10227 | case OMP_CLAUSE_PRIVATE: |
10228 | case OMP_CLAUSE_SHARED: | |
10229 | case OMP_CLAUSE_FIRSTPRIVATE: | |
8487df40 | 10230 | case OMP_CLAUSE_REDUCTION: |
10231 | case OMP_CLAUSE_COPYIN: | |
10232 | case OMP_CLAUSE_COPYPRIVATE: | |
10233 | case OMP_CLAUSE_IF: | |
10234 | case OMP_CLAUSE_NUM_THREADS: | |
10235 | case OMP_CLAUSE_SCHEDULE: | |
fd6481cf | 10236 | case OMP_CLAUSE_COLLAPSE: |
8487df40 | 10237 | OMP_CLAUSE_OPERAND (nc, 0) |
bde9ebf7 | 10238 | = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain, |
10239 | in_decl, /*integral_constant_expression_p=*/false); | |
8487df40 | 10240 | break; |
10241 | case OMP_CLAUSE_NOWAIT: | |
10242 | case OMP_CLAUSE_ORDERED: | |
10243 | case OMP_CLAUSE_DEFAULT: | |
fd6481cf | 10244 | case OMP_CLAUSE_UNTIED: |
8487df40 | 10245 | break; |
10246 | default: | |
10247 | gcc_unreachable (); | |
10248 | } | |
10249 | } | |
10250 | ||
10251 | return finish_omp_clauses (nreverse (new_clauses)); | |
10252 | } | |
10253 | ||
997a0ce8 | 10254 | /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */ |
10255 | ||
10256 | static tree | |
10257 | tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain, | |
10258 | tree in_decl) | |
10259 | { | |
10260 | #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl) | |
10261 | ||
10262 | tree purpose, value, chain; | |
10263 | ||
10264 | if (t == NULL) | |
10265 | return t; | |
10266 | ||
10267 | if (TREE_CODE (t) != TREE_LIST) | |
10268 | return tsubst_copy_and_build (t, args, complain, in_decl, | |
bde9ebf7 | 10269 | /*function_p=*/false, |
10270 | /*integral_constant_expression_p=*/false); | |
997a0ce8 | 10271 | |
10272 | if (t == void_list_node) | |
10273 | return t; | |
10274 | ||
10275 | purpose = TREE_PURPOSE (t); | |
10276 | if (purpose) | |
10277 | purpose = RECUR (purpose); | |
10278 | value = TREE_VALUE (t); | |
10279 | if (value) | |
10280 | value = RECUR (value); | |
10281 | chain = TREE_CHAIN (t); | |
10282 | if (chain && chain != void_type_node) | |
10283 | chain = RECUR (chain); | |
10284 | return tree_cons (purpose, value, chain); | |
10285 | #undef RECUR | |
10286 | } | |
10287 | ||
fd6481cf | 10288 | /* Substitute one OMP_FOR iterator. */ |
10289 | ||
10290 | static void | |
10291 | tsubst_omp_for_iterator (tree t, int i, tree declv, tree initv, | |
10292 | tree condv, tree incrv, tree *clauses, | |
10293 | tree args, tsubst_flags_t complain, tree in_decl, | |
10294 | bool integral_constant_expression_p) | |
10295 | { | |
10296 | #define RECUR(NODE) \ | |
10297 | tsubst_expr ((NODE), args, complain, in_decl, \ | |
10298 | integral_constant_expression_p) | |
10299 | tree decl, init, cond, incr; | |
10300 | ||
10301 | init = TREE_VEC_ELT (OMP_FOR_INIT (t), i); | |
10302 | gcc_assert (TREE_CODE (init) == MODIFY_EXPR); | |
10303 | decl = RECUR (TREE_OPERAND (init, 0)); | |
10304 | init = TREE_OPERAND (init, 1); | |
10305 | gcc_assert (!type_dependent_expression_p (decl)); | |
10306 | ||
10307 | if (!CLASS_TYPE_P (TREE_TYPE (decl))) | |
10308 | { | |
10309 | cond = RECUR (TREE_VEC_ELT (OMP_FOR_COND (t), i)); | |
10310 | incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i); | |
10311 | if (TREE_CODE (incr) == MODIFY_EXPR) | |
10312 | incr = build_x_modify_expr (RECUR (TREE_OPERAND (incr, 0)), NOP_EXPR, | |
10313 | RECUR (TREE_OPERAND (incr, 1)), | |
10314 | complain); | |
10315 | else | |
10316 | incr = RECUR (incr); | |
10317 | TREE_VEC_ELT (declv, i) = decl; | |
10318 | TREE_VEC_ELT (initv, i) = init; | |
10319 | TREE_VEC_ELT (condv, i) = cond; | |
10320 | TREE_VEC_ELT (incrv, i) = incr; | |
10321 | return; | |
10322 | } | |
10323 | ||
10324 | if (init && TREE_CODE (init) != DECL_EXPR) | |
10325 | { | |
10326 | tree c; | |
10327 | for (c = *clauses; c ; c = OMP_CLAUSE_CHAIN (c)) | |
10328 | { | |
10329 | if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE | |
10330 | || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE) | |
10331 | && OMP_CLAUSE_DECL (c) == decl) | |
10332 | break; | |
10333 | else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE | |
10334 | && OMP_CLAUSE_DECL (c) == decl) | |
10335 | error ("iteration variable %qD should not be firstprivate", decl); | |
10336 | else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION | |
10337 | && OMP_CLAUSE_DECL (c) == decl) | |
10338 | error ("iteration variable %qD should not be reduction", decl); | |
10339 | } | |
10340 | if (c == NULL) | |
10341 | { | |
10342 | c = build_omp_clause (OMP_CLAUSE_PRIVATE); | |
10343 | OMP_CLAUSE_DECL (c) = decl; | |
10344 | c = finish_omp_clauses (c); | |
10345 | if (c) | |
10346 | { | |
10347 | OMP_CLAUSE_CHAIN (c) = *clauses; | |
10348 | *clauses = c; | |
10349 | } | |
10350 | } | |
10351 | } | |
10352 | cond = TREE_VEC_ELT (OMP_FOR_COND (t), i); | |
10353 | if (COMPARISON_CLASS_P (cond)) | |
10354 | cond = build2 (TREE_CODE (cond), boolean_type_node, | |
10355 | RECUR (TREE_OPERAND (cond, 0)), | |
10356 | RECUR (TREE_OPERAND (cond, 1))); | |
10357 | else | |
10358 | cond = RECUR (cond); | |
10359 | incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i); | |
10360 | switch (TREE_CODE (incr)) | |
10361 | { | |
10362 | case PREINCREMENT_EXPR: | |
10363 | case PREDECREMENT_EXPR: | |
10364 | case POSTINCREMENT_EXPR: | |
10365 | case POSTDECREMENT_EXPR: | |
10366 | incr = build2 (TREE_CODE (incr), TREE_TYPE (decl), | |
10367 | RECUR (TREE_OPERAND (incr, 0)), NULL_TREE); | |
10368 | break; | |
10369 | case MODIFY_EXPR: | |
10370 | if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR | |
10371 | || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR) | |
10372 | { | |
10373 | tree rhs = TREE_OPERAND (incr, 1); | |
10374 | incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), | |
10375 | RECUR (TREE_OPERAND (incr, 0)), | |
10376 | build2 (TREE_CODE (rhs), TREE_TYPE (decl), | |
10377 | RECUR (TREE_OPERAND (rhs, 0)), | |
10378 | RECUR (TREE_OPERAND (rhs, 1)))); | |
10379 | } | |
10380 | else | |
10381 | incr = RECUR (incr); | |
10382 | break; | |
10383 | case MODOP_EXPR: | |
10384 | if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR | |
10385 | || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR) | |
10386 | { | |
10387 | tree lhs = RECUR (TREE_OPERAND (incr, 0)); | |
10388 | incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs, | |
10389 | build2 (TREE_CODE (TREE_OPERAND (incr, 1)), | |
10390 | TREE_TYPE (decl), lhs, | |
10391 | RECUR (TREE_OPERAND (incr, 2)))); | |
10392 | } | |
10393 | else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR | |
10394 | && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR | |
10395 | || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR))) | |
10396 | { | |
10397 | tree rhs = TREE_OPERAND (incr, 2); | |
10398 | incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), | |
10399 | RECUR (TREE_OPERAND (incr, 0)), | |
10400 | build2 (TREE_CODE (rhs), TREE_TYPE (decl), | |
10401 | RECUR (TREE_OPERAND (rhs, 0)), | |
10402 | RECUR (TREE_OPERAND (rhs, 1)))); | |
10403 | } | |
10404 | else | |
10405 | incr = RECUR (incr); | |
10406 | break; | |
10407 | default: | |
10408 | incr = RECUR (incr); | |
10409 | break; | |
10410 | } | |
10411 | ||
10412 | TREE_VEC_ELT (declv, i) = decl; | |
10413 | TREE_VEC_ELT (initv, i) = init; | |
10414 | TREE_VEC_ELT (condv, i) = cond; | |
10415 | TREE_VEC_ELT (incrv, i) = incr; | |
10416 | #undef RECUR | |
10417 | } | |
10418 | ||
33643032 | 10419 | /* Like tsubst_copy for expressions, etc. but also does semantic |
10420 | processing. */ | |
73645519 | 10421 | |
5f6526e1 | 10422 | static tree |
bde9ebf7 | 10423 | tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl, |
10424 | bool integral_constant_expression_p) | |
e857e9c7 | 10425 | { |
bde9ebf7 | 10426 | #define RECUR(NODE) \ |
10427 | tsubst_expr ((NODE), args, complain, in_decl, \ | |
10428 | integral_constant_expression_p) | |
10429 | ||
7a37f969 | 10430 | tree stmt, tmp; |
019cf886 | 10431 | |
e857e9c7 | 10432 | if (t == NULL_TREE || t == error_mark_node) |
10433 | return t; | |
10434 | ||
357f7efa | 10435 | if (EXPR_HAS_LOCATION (t)) |
10436 | input_location = EXPR_LOCATION (t); | |
8495c0ca | 10437 | if (STATEMENT_CODE_P (TREE_CODE (t))) |
10438 | current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t); | |
10439 | ||
e857e9c7 | 10440 | switch (TREE_CODE (t)) |
471086d6 | 10441 | { |
2363ef00 | 10442 | case STATEMENT_LIST: |
10443 | { | |
10444 | tree_stmt_iterator i; | |
10445 | for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i)) | |
bde9ebf7 | 10446 | RECUR (tsi_stmt (i)); |
2363ef00 | 10447 | break; |
10448 | } | |
10449 | ||
019cf886 | 10450 | case CTOR_INITIALIZER: |
9031d10b | 10451 | finish_mem_initializers (tsubst_initializer_list |
6507cda8 | 10452 | (TREE_OPERAND (t, 0), args)); |
10453 | break; | |
019cf886 | 10454 | |
c857cd60 | 10455 | case RETURN_EXPR: |
bde9ebf7 | 10456 | finish_return_stmt (RECUR (TREE_OPERAND (t, 0))); |
e857e9c7 | 10457 | break; |
10458 | ||
10459 | case EXPR_STMT: | |
bde9ebf7 | 10460 | tmp = RECUR (EXPR_STMT_EXPR (t)); |
8495c0ca | 10461 | if (EXPR_STMT_STMT_EXPR_RESULT (t)) |
10462 | finish_stmt_expr_expr (tmp, cur_stmt_expr); | |
10463 | else | |
10464 | finish_expr_stmt (tmp); | |
10465 | break; | |
e857e9c7 | 10466 | |
632cab6c | 10467 | case USING_STMT: |
bde9ebf7 | 10468 | do_using_directive (RECUR (USING_STMT_NAMESPACE (t))); |
632cab6c | 10469 | break; |
9031d10b | 10470 | |
7dd37241 | 10471 | case DECL_EXPR: |
e857e9c7 | 10472 | { |
ad828bde | 10473 | tree decl; |
10474 | tree init; | |
e857e9c7 | 10475 | |
7dd37241 | 10476 | decl = DECL_EXPR_DECL (t); |
2d46e540 | 10477 | if (TREE_CODE (decl) == LABEL_DECL) |
10478 | finish_label_decl (DECL_NAME (decl)); | |
a233dea3 | 10479 | else if (TREE_CODE (decl) == USING_DECL) |
10480 | { | |
094fb0d8 | 10481 | tree scope = USING_DECL_SCOPE (decl); |
a233dea3 | 10482 | tree name = DECL_NAME (decl); |
c75a55a8 | 10483 | tree decl; |
9031d10b | 10484 | |
bde9ebf7 | 10485 | scope = RECUR (scope); |
c75a55a8 | 10486 | decl = lookup_qualified_name (scope, name, |
c4cf31e6 | 10487 | /*is_type_p=*/false, |
10488 | /*complain=*/false); | |
2cdbcd51 | 10489 | if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST) |
10490 | qualified_name_lookup_error (scope, name, decl); | |
c75a55a8 | 10491 | else |
2c47ecdb | 10492 | do_local_using_decl (decl, scope, name); |
a233dea3 | 10493 | } |
2d46e540 | 10494 | else |
10495 | { | |
10496 | init = DECL_INITIAL (decl); | |
10497 | decl = tsubst (decl, args, complain, in_decl); | |
6b057184 | 10498 | if (decl != error_mark_node) |
10499 | { | |
653e5405 | 10500 | /* By marking the declaration as instantiated, we avoid |
10501 | trying to instantiate it. Since instantiate_decl can't | |
10502 | handle local variables, and since we've already done | |
10503 | all that needs to be done, that's the right thing to | |
10504 | do. */ | |
10505 | if (TREE_CODE (decl) == VAR_DECL) | |
10506 | DECL_TEMPLATE_INSTANTIATED (decl) = 1; | |
c8a606ae | 10507 | if (TREE_CODE (decl) == VAR_DECL |
10508 | && ANON_AGGR_TYPE_P (TREE_TYPE (decl))) | |
10509 | /* Anonymous aggregates are a special case. */ | |
10510 | finish_anon_union (decl); | |
9031d10b | 10511 | else |
9416e3c9 | 10512 | { |
c8a606ae | 10513 | maybe_push_decl (decl); |
878870b4 | 10514 | if (TREE_CODE (decl) == VAR_DECL |
10515 | && DECL_PRETTY_FUNCTION_P (decl)) | |
c8a606ae | 10516 | { |
10517 | /* For __PRETTY_FUNCTION__ we have to adjust the | |
10518 | initializer. */ | |
10519 | const char *const name | |
10520 | = cxx_printable_name (current_function_decl, 2); | |
4880ab99 | 10521 | init = cp_fname_init (name, &TREE_TYPE (decl)); |
c8a606ae | 10522 | } |
10523 | else | |
2d7f50b9 | 10524 | { |
10525 | tree t = RECUR (init); | |
10526 | ||
10527 | if (init && !t) | |
10528 | /* If we had an initializer but it | |
10529 | instantiated to nothing, | |
10530 | value-initialize the object. This will | |
10531 | only occur when the initializer was a | |
10532 | pack expansion where the parameter packs | |
10533 | used in that expansion were of length | |
10534 | zero. */ | |
10535 | init = build_default_init (TREE_TYPE (decl), | |
10536 | NULL_TREE); | |
10537 | else | |
10538 | init = t; | |
10539 | } | |
10540 | ||
d91303a6 | 10541 | finish_decl (decl, init, NULL_TREE); |
9416e3c9 | 10542 | } |
6b057184 | 10543 | } |
2d46e540 | 10544 | } |
7a37f969 | 10545 | |
7dd37241 | 10546 | /* A DECL_EXPR can also be used as an expression, in the condition |
2363ef00 | 10547 | clause of an if/for/while construct. */ |
10548 | return decl; | |
e857e9c7 | 10549 | } |
471086d6 | 10550 | |
e857e9c7 | 10551 | case FOR_STMT: |
8495c0ca | 10552 | stmt = begin_for_stmt (); |
bde9ebf7 | 10553 | RECUR (FOR_INIT_STMT (t)); |
8495c0ca | 10554 | finish_for_init_stmt (stmt); |
bde9ebf7 | 10555 | tmp = RECUR (FOR_COND (t)); |
8495c0ca | 10556 | finish_for_cond (tmp, stmt); |
bde9ebf7 | 10557 | tmp = RECUR (FOR_EXPR (t)); |
8495c0ca | 10558 | finish_for_expr (tmp, stmt); |
bde9ebf7 | 10559 | RECUR (FOR_BODY (t)); |
8495c0ca | 10560 | finish_for_stmt (stmt); |
e857e9c7 | 10561 | break; |
471086d6 | 10562 | |
e857e9c7 | 10563 | case WHILE_STMT: |
8495c0ca | 10564 | stmt = begin_while_stmt (); |
bde9ebf7 | 10565 | tmp = RECUR (WHILE_COND (t)); |
8495c0ca | 10566 | finish_while_stmt_cond (tmp, stmt); |
bde9ebf7 | 10567 | RECUR (WHILE_BODY (t)); |
8495c0ca | 10568 | finish_while_stmt (stmt); |
e857e9c7 | 10569 | break; |
471086d6 | 10570 | |
e857e9c7 | 10571 | case DO_STMT: |
8495c0ca | 10572 | stmt = begin_do_stmt (); |
bde9ebf7 | 10573 | RECUR (DO_BODY (t)); |
8495c0ca | 10574 | finish_do_body (stmt); |
bde9ebf7 | 10575 | tmp = RECUR (DO_COND (t)); |
8495c0ca | 10576 | finish_do_stmt (tmp, stmt); |
e857e9c7 | 10577 | break; |
ddb9bca7 | 10578 | |
e857e9c7 | 10579 | case IF_STMT: |
8495c0ca | 10580 | stmt = begin_if_stmt (); |
bde9ebf7 | 10581 | tmp = RECUR (IF_COND (t)); |
8495c0ca | 10582 | finish_if_stmt_cond (tmp, stmt); |
bde9ebf7 | 10583 | RECUR (THEN_CLAUSE (t)); |
8495c0ca | 10584 | finish_then_clause (stmt); |
471086d6 | 10585 | |
8495c0ca | 10586 | if (ELSE_CLAUSE (t)) |
10587 | { | |
10588 | begin_else_clause (stmt); | |
bde9ebf7 | 10589 | RECUR (ELSE_CLAUSE (t)); |
8495c0ca | 10590 | finish_else_clause (stmt); |
10591 | } | |
10592 | ||
10593 | finish_if_stmt (stmt); | |
e857e9c7 | 10594 | break; |
471086d6 | 10595 | |
632f8185 | 10596 | case BIND_EXPR: |
8495c0ca | 10597 | if (BIND_EXPR_BODY_BLOCK (t)) |
10598 | stmt = begin_function_body (); | |
10599 | else | |
10600 | stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t) | |
10601 | ? BCS_TRY_BLOCK : 0); | |
52616263 | 10602 | |
bde9ebf7 | 10603 | RECUR (BIND_EXPR_BODY (t)); |
52616263 | 10604 | |
8495c0ca | 10605 | if (BIND_EXPR_BODY_BLOCK (t)) |
10606 | finish_function_body (stmt); | |
10607 | else | |
10608 | finish_compound_stmt (stmt); | |
e857e9c7 | 10609 | break; |
471086d6 | 10610 | |
e857e9c7 | 10611 | case BREAK_STMT: |
0090dad2 | 10612 | finish_break_stmt (); |
e857e9c7 | 10613 | break; |
471086d6 | 10614 | |
596c0ae6 | 10615 | case CONTINUE_STMT: |
0090dad2 | 10616 | finish_continue_stmt (); |
596c0ae6 | 10617 | break; |
10618 | ||
e857e9c7 | 10619 | case SWITCH_STMT: |
8495c0ca | 10620 | stmt = begin_switch_stmt (); |
bde9ebf7 | 10621 | tmp = RECUR (SWITCH_STMT_COND (t)); |
8495c0ca | 10622 | finish_switch_cond (tmp, stmt); |
bde9ebf7 | 10623 | RECUR (SWITCH_STMT_BODY (t)); |
8495c0ca | 10624 | finish_switch_stmt (stmt); |
e857e9c7 | 10625 | break; |
10626 | ||
eaae3b75 | 10627 | case CASE_LABEL_EXPR: |
bde9ebf7 | 10628 | finish_case_label (RECUR (CASE_LOW (t)), |
10629 | RECUR (CASE_HIGH (t))); | |
e857e9c7 | 10630 | break; |
10631 | ||
daf6dff5 | 10632 | case LABEL_EXPR: |
daf6dff5 | 10633 | finish_label_stmt (DECL_NAME (LABEL_EXPR_LABEL (t))); |
e857e9c7 | 10634 | break; |
10635 | ||
daf6dff5 | 10636 | case GOTO_EXPR: |
7a37f969 | 10637 | tmp = GOTO_DESTINATION (t); |
10638 | if (TREE_CODE (tmp) != LABEL_DECL) | |
b1dcc417 | 10639 | /* Computed goto's must be tsubst'd into. On the other hand, |
10640 | non-computed gotos must not be; the identifier in question | |
10641 | will have no binding. */ | |
bde9ebf7 | 10642 | tmp = RECUR (tmp); |
b0a59c16 | 10643 | else |
7a37f969 | 10644 | tmp = DECL_NAME (tmp); |
10645 | finish_goto_stmt (tmp); | |
0090dad2 | 10646 | break; |
10647 | ||
226c2452 | 10648 | case ASM_EXPR: |
311e42bc | 10649 | tmp = finish_asm_stmt |
4ee9c684 | 10650 | (ASM_VOLATILE_P (t), |
bde9ebf7 | 10651 | RECUR (ASM_STRING (t)), |
997a0ce8 | 10652 | tsubst_copy_asm_operands (ASM_OUTPUTS (t), args, complain, in_decl), |
10653 | tsubst_copy_asm_operands (ASM_INPUTS (t), args, complain, in_decl), | |
10654 | tsubst_copy_asm_operands (ASM_CLOBBERS (t), args, complain, in_decl)); | |
e4301c68 | 10655 | { |
10656 | tree asm_expr = tmp; | |
10657 | if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR) | |
10658 | asm_expr = TREE_OPERAND (asm_expr, 0); | |
10659 | ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t); | |
10660 | } | |
e857e9c7 | 10661 | break; |
860740a7 | 10662 | |
10663 | case TRY_BLOCK: | |
b48733fd | 10664 | if (CLEANUP_P (t)) |
0a8302dc | 10665 | { |
73b8a26b | 10666 | stmt = begin_try_block (); |
bde9ebf7 | 10667 | RECUR (TRY_STMTS (t)); |
0a8302dc | 10668 | finish_cleanup_try_block (stmt); |
bde9ebf7 | 10669 | finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt); |
0a8302dc | 10670 | } |
b48733fd | 10671 | else |
10672 | { | |
78f7169a | 10673 | tree compound_stmt = NULL_TREE; |
10674 | ||
0a8302dc | 10675 | if (FN_TRY_BLOCK_P (t)) |
78f7169a | 10676 | stmt = begin_function_try_block (&compound_stmt); |
0a8302dc | 10677 | else |
10678 | stmt = begin_try_block (); | |
10679 | ||
bde9ebf7 | 10680 | RECUR (TRY_STMTS (t)); |
0a8302dc | 10681 | |
10682 | if (FN_TRY_BLOCK_P (t)) | |
10683 | finish_function_try_block (stmt); | |
10684 | else | |
10685 | finish_try_block (stmt); | |
10686 | ||
bde9ebf7 | 10687 | RECUR (TRY_HANDLERS (t)); |
a3d5bae2 | 10688 | if (FN_TRY_BLOCK_P (t)) |
78f7169a | 10689 | finish_function_handler_sequence (stmt, compound_stmt); |
a3d5bae2 | 10690 | else |
10691 | finish_handler_sequence (stmt); | |
b48733fd | 10692 | } |
860740a7 | 10693 | break; |
9031d10b | 10694 | |
860740a7 | 10695 | case HANDLER: |
e0e489c4 | 10696 | { |
e25a65a1 | 10697 | tree decl = HANDLER_PARMS (t); |
e0e489c4 | 10698 | |
e25a65a1 | 10699 | if (decl) |
e0e489c4 | 10700 | { |
e0e489c4 | 10701 | decl = tsubst (decl, args, complain, in_decl); |
24bd1b1a | 10702 | /* Prevent instantiate_decl from trying to instantiate |
10703 | this variable. We've already done all that needs to be | |
10704 | done. */ | |
e25a65a1 | 10705 | if (decl != error_mark_node) |
10706 | DECL_TEMPLATE_INSTANTIATED (decl) = 1; | |
e0e489c4 | 10707 | } |
e25a65a1 | 10708 | stmt = begin_handler (); |
6993fb0a | 10709 | finish_handler_parms (decl, stmt); |
bde9ebf7 | 10710 | RECUR (HANDLER_BODY (t)); |
6993fb0a | 10711 | finish_handler (stmt); |
e0e489c4 | 10712 | } |
860740a7 | 10713 | break; |
10714 | ||
dc9c6fd7 | 10715 | case TAG_DEFN: |
7a37f969 | 10716 | tsubst (TREE_TYPE (t), args, complain, NULL_TREE); |
dc9c6fd7 | 10717 | break; |
7a05c4b1 | 10718 | |
10719 | case STATIC_ASSERT: | |
10720 | { | |
10721 | tree condition = | |
10722 | tsubst_expr (STATIC_ASSERT_CONDITION (t), | |
10723 | args, | |
10724 | complain, in_decl, | |
10725 | /*integral_constant_expression_p=*/true); | |
10726 | finish_static_assert (condition, | |
10727 | STATIC_ASSERT_MESSAGE (t), | |
10728 | STATIC_ASSERT_SOURCE_LOCATION (t), | |
10729 | /*member_p=*/false); | |
10730 | } | |
10731 | break; | |
dc9c6fd7 | 10732 | |
8487df40 | 10733 | case OMP_PARALLEL: |
10734 | tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), | |
10735 | args, complain, in_decl); | |
10736 | stmt = begin_omp_parallel (); | |
bde9ebf7 | 10737 | RECUR (OMP_PARALLEL_BODY (t)); |
87f7c31e | 10738 | OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt)) |
10739 | = OMP_PARALLEL_COMBINED (t); | |
8487df40 | 10740 | break; |
10741 | ||
fd6481cf | 10742 | case OMP_TASK: |
10743 | tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), | |
10744 | args, complain, in_decl); | |
10745 | stmt = begin_omp_task (); | |
10746 | RECUR (OMP_TASK_BODY (t)); | |
10747 | finish_omp_task (tmp, stmt); | |
10748 | break; | |
10749 | ||
8487df40 | 10750 | case OMP_FOR: |
10751 | { | |
fd6481cf | 10752 | tree clauses, body, pre_body; |
10753 | tree declv, initv, condv, incrv; | |
10754 | int i; | |
8487df40 | 10755 | |
10756 | clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), | |
10757 | args, complain, in_decl); | |
fd6481cf | 10758 | declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t))); |
10759 | initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t))); | |
10760 | condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t))); | |
10761 | incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t))); | |
10762 | ||
10763 | for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++) | |
10764 | tsubst_omp_for_iterator (t, i, declv, initv, condv, incrv, | |
10765 | &clauses, args, complain, in_decl, | |
10766 | integral_constant_expression_p); | |
8487df40 | 10767 | |
10768 | stmt = begin_omp_structured_block (); | |
10769 | ||
fd6481cf | 10770 | for (i = 0; i < TREE_VEC_LENGTH (initv); i++) |
10771 | if (TREE_VEC_ELT (initv, i) == NULL | |
10772 | || TREE_CODE (TREE_VEC_ELT (initv, i)) != DECL_EXPR) | |
10773 | TREE_VEC_ELT (initv, i) = RECUR (TREE_VEC_ELT (initv, i)); | |
10774 | else if (CLASS_TYPE_P (TREE_TYPE (TREE_VEC_ELT (initv, i)))) | |
10775 | { | |
10776 | tree init = RECUR (TREE_VEC_ELT (initv, i)); | |
10777 | gcc_assert (init == TREE_VEC_ELT (declv, i)); | |
10778 | TREE_VEC_ELT (initv, i) = NULL_TREE; | |
10779 | } | |
10780 | else | |
10781 | { | |
10782 | tree decl_expr = TREE_VEC_ELT (initv, i); | |
10783 | tree init = DECL_INITIAL (DECL_EXPR_DECL (decl_expr)); | |
10784 | gcc_assert (init != NULL); | |
10785 | TREE_VEC_ELT (initv, i) = RECUR (init); | |
10786 | DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL; | |
10787 | RECUR (decl_expr); | |
10788 | DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init; | |
10789 | } | |
10790 | ||
8487df40 | 10791 | pre_body = push_stmt_list (); |
bde9ebf7 | 10792 | RECUR (OMP_FOR_PRE_BODY (t)); |
8487df40 | 10793 | pre_body = pop_stmt_list (pre_body); |
10794 | ||
10795 | body = push_stmt_list (); | |
bde9ebf7 | 10796 | RECUR (OMP_FOR_BODY (t)); |
8487df40 | 10797 | body = pop_stmt_list (body); |
10798 | ||
fd6481cf | 10799 | t = finish_omp_for (EXPR_LOCATION (t), declv, initv, condv, incrv, |
10800 | body, pre_body, clauses); | |
8487df40 | 10801 | |
10802 | add_stmt (finish_omp_structured_block (stmt)); | |
10803 | } | |
10804 | break; | |
10805 | ||
10806 | case OMP_SECTIONS: | |
10807 | case OMP_SINGLE: | |
10808 | tmp = tsubst_omp_clauses (OMP_CLAUSES (t), args, complain, in_decl); | |
10809 | stmt = push_stmt_list (); | |
bde9ebf7 | 10810 | RECUR (OMP_BODY (t)); |
8487df40 | 10811 | stmt = pop_stmt_list (stmt); |
10812 | ||
10813 | t = copy_node (t); | |
10814 | OMP_BODY (t) = stmt; | |
10815 | OMP_CLAUSES (t) = tmp; | |
10816 | add_stmt (t); | |
10817 | break; | |
10818 | ||
10819 | case OMP_SECTION: | |
10820 | case OMP_CRITICAL: | |
10821 | case OMP_MASTER: | |
10822 | case OMP_ORDERED: | |
10823 | stmt = push_stmt_list (); | |
bde9ebf7 | 10824 | RECUR (OMP_BODY (t)); |
8487df40 | 10825 | stmt = pop_stmt_list (stmt); |
10826 | ||
10827 | t = copy_node (t); | |
10828 | OMP_BODY (t) = stmt; | |
10829 | add_stmt (t); | |
10830 | break; | |
10831 | ||
10832 | case OMP_ATOMIC: | |
221e68f2 | 10833 | gcc_assert (OMP_ATOMIC_DEPENDENT_P (t)); |
10834 | { | |
10835 | tree op1 = TREE_OPERAND (t, 1); | |
10836 | tree lhs = RECUR (TREE_OPERAND (op1, 0)); | |
10837 | tree rhs = RECUR (TREE_OPERAND (op1, 1)); | |
10838 | finish_omp_atomic (TREE_CODE (op1), lhs, rhs); | |
10839 | } | |
8487df40 | 10840 | break; |
10841 | ||
d95d815d | 10842 | case EXPR_PACK_EXPANSION: |
10843 | error ("invalid use of pack expansion expression"); | |
10844 | return error_mark_node; | |
10845 | ||
10846 | case NONTYPE_ARGUMENT_PACK: | |
10847 | error ("use %<...%> to expand argument pack"); | |
10848 | return error_mark_node; | |
10849 | ||
e857e9c7 | 10850 | default: |
2e3e31d2 | 10851 | gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t))); |
9031d10b | 10852 | |
2e3e31d2 | 10853 | return tsubst_copy_and_build (t, args, complain, in_decl, |
bde9ebf7 | 10854 | /*function_p=*/false, |
10855 | integral_constant_expression_p); | |
e857e9c7 | 10856 | } |
7a37f969 | 10857 | |
2363ef00 | 10858 | return NULL_TREE; |
bde9ebf7 | 10859 | #undef RECUR |
471086d6 | 10860 | } |
10861 | ||
0886adbc | 10862 | /* T is a postfix-expression that is not being used in a function |
10863 | call. Return the substituted version of T. */ | |
10864 | ||
10865 | static tree | |
9031d10b | 10866 | tsubst_non_call_postfix_expression (tree t, tree args, |
0886adbc | 10867 | tsubst_flags_t complain, |
10868 | tree in_decl) | |
10869 | { | |
10870 | if (TREE_CODE (t) == SCOPE_REF) | |
10871 | t = tsubst_qualified_id (t, args, complain, in_decl, | |
10872 | /*done=*/false, /*address_p=*/false); | |
10873 | else | |
10874 | t = tsubst_copy_and_build (t, args, complain, in_decl, | |
bde9ebf7 | 10875 | /*function_p=*/false, |
10876 | /*integral_constant_expression_p=*/false); | |
0886adbc | 10877 | |
10878 | return t; | |
10879 | } | |
10880 | ||
33643032 | 10881 | /* Like tsubst but deals with expressions and performs semantic |
0886adbc | 10882 | analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */ |
33643032 | 10883 | |
ef4534a3 | 10884 | tree |
9031d10b | 10885 | tsubst_copy_and_build (tree t, |
653e5405 | 10886 | tree args, |
10887 | tsubst_flags_t complain, | |
10888 | tree in_decl, | |
bde9ebf7 | 10889 | bool function_p, |
10890 | bool integral_constant_expression_p) | |
33643032 | 10891 | { |
bde9ebf7 | 10892 | #define RECUR(NODE) \ |
10893 | tsubst_copy_and_build (NODE, args, complain, in_decl, \ | |
10894 | /*function_p=*/false, \ | |
10895 | integral_constant_expression_p) | |
0886adbc | 10896 | |
ef4534a3 | 10897 | tree op1; |
10898 | ||
33643032 | 10899 | if (t == NULL_TREE || t == error_mark_node) |
10900 | return t; | |
10901 | ||
10902 | switch (TREE_CODE (t)) | |
10903 | { | |
b7edeb23 | 10904 | case USING_DECL: |
10905 | t = DECL_NAME (t); | |
0f3ccaa3 | 10906 | /* Fall through. */ |
0886adbc | 10907 | case IDENTIFIER_NODE: |
33643032 | 10908 | { |
0886adbc | 10909 | tree decl; |
0886adbc | 10910 | cp_id_kind idk; |
f47c1747 | 10911 | bool non_integral_constant_expression_p; |
0886adbc | 10912 | const char *error_msg; |
10913 | ||
0886adbc | 10914 | if (IDENTIFIER_TYPENAME_P (t)) |
33643032 | 10915 | { |
0886adbc | 10916 | tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl); |
10917 | t = mangle_conv_op_name_for_type (new_type); | |
33643032 | 10918 | } |
0886adbc | 10919 | |
10920 | /* Look up the name. */ | |
d1c41717 | 10921 | decl = lookup_name (t); |
0886adbc | 10922 | |
10923 | /* By convention, expressions use ERROR_MARK_NODE to indicate | |
10924 | failure, not NULL_TREE. */ | |
10925 | if (decl == NULL_TREE) | |
10926 | decl = error_mark_node; | |
10927 | ||
c08d51be | 10928 | decl = finish_id_expression (t, decl, NULL_TREE, |
0886adbc | 10929 | &idk, |
bde9ebf7 | 10930 | integral_constant_expression_p, |
f47c1747 | 10931 | /*allow_non_integral_constant_expression_p=*/false, |
10932 | &non_integral_constant_expression_p, | |
fbb01da7 | 10933 | /*template_p=*/false, |
10934 | /*done=*/true, | |
10935 | /*address_p=*/false, | |
10936 | /*template_arg_p=*/false, | |
0886adbc | 10937 | &error_msg); |
10938 | if (error_msg) | |
10939 | error (error_msg); | |
10940 | if (!function_p && TREE_CODE (decl) == IDENTIFIER_NODE) | |
10941 | decl = unqualified_name_lookup_error (decl); | |
10942 | return decl; | |
33643032 | 10943 | } |
10944 | ||
10945 | case TEMPLATE_ID_EXPR: | |
10946 | { | |
10947 | tree object; | |
0886adbc | 10948 | tree template = RECUR (TREE_OPERAND (t, 0)); |
b5959ba9 | 10949 | tree targs = TREE_OPERAND (t, 1); |
10950 | ||
10951 | if (targs) | |
10952 | targs = tsubst_template_args (targs, args, complain, in_decl); | |
9031d10b | 10953 | |
33643032 | 10954 | if (TREE_CODE (template) == COMPONENT_REF) |
10955 | { | |
10956 | object = TREE_OPERAND (template, 0); | |
10957 | template = TREE_OPERAND (template, 1); | |
10958 | } | |
10959 | else | |
10960 | object = NULL_TREE; | |
7aa978b3 | 10961 | template = lookup_template_function (template, targs); |
9031d10b | 10962 | |
33643032 | 10963 | if (object) |
9031d10b | 10964 | return build3 (COMPONENT_REF, TREE_TYPE (template), |
831d52a2 | 10965 | object, template, NULL_TREE); |
33643032 | 10966 | else |
0e5cde0c | 10967 | return baselink_for_fns (template); |
33643032 | 10968 | } |
10969 | ||
10970 | case INDIRECT_REF: | |
729f89ff | 10971 | { |
10972 | tree r = RECUR (TREE_OPERAND (t, 0)); | |
10973 | ||
10974 | if (REFERENCE_REF_P (t)) | |
10975 | { | |
34da5a16 | 10976 | /* A type conversion to reference type will be enclosed in |
10977 | such an indirect ref, but the substitution of the cast | |
10978 | will have also added such an indirect ref. */ | |
10979 | if (TREE_CODE (TREE_TYPE (r)) == REFERENCE_TYPE) | |
10980 | r = convert_from_reference (r); | |
729f89ff | 10981 | } |
10982 | else | |
ebd21de4 | 10983 | r = build_x_indirect_ref (r, "unary *", complain); |
729f89ff | 10984 | return r; |
10985 | } | |
33643032 | 10986 | |
f352cc1d | 10987 | case NOP_EXPR: |
10988 | return build_nop | |
10989 | (tsubst (TREE_TYPE (t), args, complain, in_decl), | |
10990 | RECUR (TREE_OPERAND (t, 0))); | |
10991 | ||
33643032 | 10992 | case CAST_EXPR: |
33643032 | 10993 | case REINTERPRET_CAST_EXPR: |
33643032 | 10994 | case CONST_CAST_EXPR: |
33643032 | 10995 | case DYNAMIC_CAST_EXPR: |
33643032 | 10996 | case STATIC_CAST_EXPR: |
bde9ebf7 | 10997 | { |
10998 | tree type; | |
10999 | tree op; | |
11000 | ||
11001 | type = tsubst (TREE_TYPE (t), args, complain, in_decl); | |
11002 | if (integral_constant_expression_p | |
11003 | && !cast_valid_in_integral_constant_expression_p (type)) | |
11004 | { | |
ebd21de4 | 11005 | if (complain & tf_error) |
11006 | error ("a cast to a type other than an integral or " | |
11007 | "enumeration type cannot appear in a constant-expression"); | |
bde9ebf7 | 11008 | return error_mark_node; |
11009 | } | |
11010 | ||
11011 | op = RECUR (TREE_OPERAND (t, 0)); | |
11012 | ||
11013 | switch (TREE_CODE (t)) | |
11014 | { | |
11015 | case CAST_EXPR: | |
ebd21de4 | 11016 | return build_functional_cast (type, op, complain); |
bde9ebf7 | 11017 | case REINTERPRET_CAST_EXPR: |
ebd21de4 | 11018 | return build_reinterpret_cast (type, op, complain); |
bde9ebf7 | 11019 | case CONST_CAST_EXPR: |
ebd21de4 | 11020 | return build_const_cast (type, op, complain); |
bde9ebf7 | 11021 | case DYNAMIC_CAST_EXPR: |
ebd21de4 | 11022 | return build_dynamic_cast (type, op, complain); |
bde9ebf7 | 11023 | case STATIC_CAST_EXPR: |
ebd21de4 | 11024 | return build_static_cast (type, op, complain); |
bde9ebf7 | 11025 | default: |
11026 | gcc_unreachable (); | |
11027 | } | |
11028 | } | |
33643032 | 11029 | |
33643032 | 11030 | case POSTDECREMENT_EXPR: |
11031 | case POSTINCREMENT_EXPR: | |
0886adbc | 11032 | op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0), |
11033 | args, complain, in_decl); | |
ebd21de4 | 11034 | return build_x_unary_op (TREE_CODE (t), op1, complain); |
ef4534a3 | 11035 | |
11036 | case PREDECREMENT_EXPR: | |
11037 | case PREINCREMENT_EXPR: | |
33643032 | 11038 | case NEGATE_EXPR: |
11039 | case BIT_NOT_EXPR: | |
33643032 | 11040 | case ABS_EXPR: |
13795292 | 11041 | case TRUTH_NOT_EXPR: |
97d541d5 | 11042 | case UNARY_PLUS_EXPR: /* Unary + */ |
13795292 | 11043 | case REALPART_EXPR: |
11044 | case IMAGPART_EXPR: | |
ebd21de4 | 11045 | return build_x_unary_op (TREE_CODE (t), RECUR (TREE_OPERAND (t, 0)), |
11046 | complain); | |
33643032 | 11047 | |
33643032 | 11048 | case ADDR_EXPR: |
ef4534a3 | 11049 | op1 = TREE_OPERAND (t, 0); |
11050 | if (TREE_CODE (op1) == SCOPE_REF) | |
9031d10b | 11051 | op1 = tsubst_qualified_id (op1, args, complain, in_decl, |
ef4534a3 | 11052 | /*done=*/true, /*address_p=*/true); |
11053 | else | |
9031d10b | 11054 | op1 = tsubst_non_call_postfix_expression (op1, args, complain, |
0886adbc | 11055 | in_decl); |
0eb00403 | 11056 | if (TREE_CODE (op1) == LABEL_DECL) |
11057 | return finish_label_address_expr (DECL_NAME (op1)); | |
ebd21de4 | 11058 | return build_x_unary_op (ADDR_EXPR, op1, complain); |
ef4534a3 | 11059 | |
33643032 | 11060 | case PLUS_EXPR: |
11061 | case MINUS_EXPR: | |
11062 | case MULT_EXPR: | |
11063 | case TRUNC_DIV_EXPR: | |
11064 | case CEIL_DIV_EXPR: | |
11065 | case FLOOR_DIV_EXPR: | |
11066 | case ROUND_DIV_EXPR: | |
11067 | case EXACT_DIV_EXPR: | |
11068 | case BIT_AND_EXPR: | |
33643032 | 11069 | case BIT_IOR_EXPR: |
11070 | case BIT_XOR_EXPR: | |
11071 | case TRUNC_MOD_EXPR: | |
11072 | case FLOOR_MOD_EXPR: | |
11073 | case TRUTH_ANDIF_EXPR: | |
11074 | case TRUTH_ORIF_EXPR: | |
11075 | case TRUTH_AND_EXPR: | |
11076 | case TRUTH_OR_EXPR: | |
11077 | case RSHIFT_EXPR: | |
11078 | case LSHIFT_EXPR: | |
11079 | case RROTATE_EXPR: | |
11080 | case LROTATE_EXPR: | |
11081 | case EQ_EXPR: | |
11082 | case NE_EXPR: | |
11083 | case MAX_EXPR: | |
11084 | case MIN_EXPR: | |
11085 | case LE_EXPR: | |
11086 | case GE_EXPR: | |
11087 | case LT_EXPR: | |
11088 | case GT_EXPR: | |
11089 | case MEMBER_REF: | |
0886adbc | 11090 | case DOTSTAR_EXPR: |
33643032 | 11091 | return build_x_binary_op |
9031d10b | 11092 | (TREE_CODE (t), |
0886adbc | 11093 | RECUR (TREE_OPERAND (t, 0)), |
e534436e | 11094 | (TREE_NO_WARNING (TREE_OPERAND (t, 0)) |
11095 | ? ERROR_MARK | |
11096 | : TREE_CODE (TREE_OPERAND (t, 0))), | |
6ee6550d | 11097 | RECUR (TREE_OPERAND (t, 1)), |
e534436e | 11098 | (TREE_NO_WARNING (TREE_OPERAND (t, 1)) |
11099 | ? ERROR_MARK | |
11100 | : TREE_CODE (TREE_OPERAND (t, 1))), | |
ebd21de4 | 11101 | /*overloaded_p=*/NULL, |
11102 | complain); | |
33643032 | 11103 | |
11104 | case SCOPE_REF: | |
ef4534a3 | 11105 | return tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true, |
11106 | /*address_p=*/false); | |
33643032 | 11107 | case ARRAY_REF: |
0886adbc | 11108 | op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0), |
11109 | args, complain, in_decl); | |
e534436e | 11110 | return build_x_binary_op (ARRAY_REF, op1, |
11111 | (TREE_NO_WARNING (TREE_OPERAND (t, 0)) | |
11112 | ? ERROR_MARK | |
11113 | : TREE_CODE (TREE_OPERAND (t, 0))), | |
11114 | RECUR (TREE_OPERAND (t, 1)), | |
11115 | (TREE_NO_WARNING (TREE_OPERAND (t, 1)) | |
11116 | ? ERROR_MARK | |
11117 | : TREE_CODE (TREE_OPERAND (t, 1))), | |
ebd21de4 | 11118 | /*overloaded_p=*/NULL, |
11119 | complain); | |
9031d10b | 11120 | |
33643032 | 11121 | case SIZEOF_EXPR: |
d95d815d | 11122 | if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))) |
83b01f73 | 11123 | return tsubst_copy (t, args, complain, in_decl); |
d95d815d | 11124 | /* Fall through */ |
11125 | ||
33643032 | 11126 | case ALIGNOF_EXPR: |
13795292 | 11127 | op1 = TREE_OPERAND (t, 0); |
11128 | if (!args) | |
11129 | { | |
11130 | /* When there are no ARGS, we are trying to evaluate a | |
11131 | non-dependent expression from the parser. Trying to do | |
11132 | the substitutions may not work. */ | |
11133 | if (!TYPE_P (op1)) | |
11134 | op1 = TREE_TYPE (op1); | |
11135 | } | |
11136 | else | |
11137 | { | |
11138 | ++skip_evaluation; | |
bde9ebf7 | 11139 | op1 = tsubst_copy_and_build (op1, args, complain, in_decl, |
11140 | /*function_p=*/false, | |
11141 | /*integral_constant_expression_p=*/false); | |
13795292 | 11142 | --skip_evaluation; |
11143 | } | |
e47f82ba | 11144 | if (TYPE_P (op1)) |
ebd21de4 | 11145 | return cxx_sizeof_or_alignof_type (op1, TREE_CODE (t), |
11146 | complain & tf_error); | |
13795292 | 11147 | else |
ebd21de4 | 11148 | return cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t), |
11149 | complain & tf_error); | |
33643032 | 11150 | |
11151 | case MODOP_EXPR: | |
ef1a5b81 | 11152 | { |
11153 | tree r = build_x_modify_expr | |
11154 | (RECUR (TREE_OPERAND (t, 0)), | |
11155 | TREE_CODE (TREE_OPERAND (t, 1)), | |
ebd21de4 | 11156 | RECUR (TREE_OPERAND (t, 2)), |
11157 | complain); | |
35ca1541 | 11158 | /* TREE_NO_WARNING must be set if either the expression was |
11159 | parenthesized or it uses an operator such as >>= rather | |
11160 | than plain assignment. In the former case, it was already | |
11161 | set and must be copied. In the latter case, | |
11162 | build_x_modify_expr sets it and it must not be reset | |
11163 | here. */ | |
11164 | if (TREE_NO_WARNING (t)) | |
11165 | TREE_NO_WARNING (r) = TREE_NO_WARNING (t); | |
ef1a5b81 | 11166 | return r; |
11167 | } | |
33643032 | 11168 | |
11169 | case ARROW_EXPR: | |
0886adbc | 11170 | op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0), |
11171 | args, complain, in_decl); | |
ef4534a3 | 11172 | /* Remember that there was a reference to this entity. */ |
11173 | if (DECL_P (op1)) | |
11174 | mark_used (op1); | |
11175 | return build_x_arrow (op1); | |
33643032 | 11176 | |
11177 | case NEW_EXPR: | |
2d7f50b9 | 11178 | { |
11179 | tree init = RECUR (TREE_OPERAND (t, 3)); | |
11180 | ||
11181 | if (TREE_OPERAND (t, 3) && !init) | |
11182 | /* If there was an initializer in the the original tree, but | |
11183 | it instantiated to an empty list, then we should pass on | |
11184 | VOID_ZERO_NODE to tell build_new that it was an empty | |
11185 | initializer () rather than no initializer. This can only | |
11186 | happen when the initializer is a pack expansion whose | |
11187 | parameter packs are of length zero. */ | |
11188 | init = void_zero_node; | |
11189 | ||
11190 | return build_new | |
11191 | (RECUR (TREE_OPERAND (t, 0)), | |
11192 | RECUR (TREE_OPERAND (t, 1)), | |
11193 | RECUR (TREE_OPERAND (t, 2)), | |
11194 | init, | |
ebd21de4 | 11195 | NEW_EXPR_USE_GLOBAL (t), |
11196 | complain); | |
2d7f50b9 | 11197 | } |
33643032 | 11198 | |
11199 | case DELETE_EXPR: | |
11200 | return delete_sanity | |
0886adbc | 11201 | (RECUR (TREE_OPERAND (t, 0)), |
11202 | RECUR (TREE_OPERAND (t, 1)), | |
33643032 | 11203 | DELETE_EXPR_USE_VEC (t), |
11204 | DELETE_EXPR_USE_GLOBAL (t)); | |
11205 | ||
11206 | case COMPOUND_EXPR: | |
0886adbc | 11207 | return build_x_compound_expr (RECUR (TREE_OPERAND (t, 0)), |
ebd21de4 | 11208 | RECUR (TREE_OPERAND (t, 1)), |
11209 | complain); | |
33643032 | 11210 | |
ef4534a3 | 11211 | case CALL_EXPR: |
33643032 | 11212 | { |
ef4534a3 | 11213 | tree function; |
11214 | tree call_args; | |
ef4534a3 | 11215 | bool qualified_p; |
c0b419db | 11216 | bool koenig_p; |
ef4534a3 | 11217 | |
c2f47e15 | 11218 | function = CALL_EXPR_FN (t); |
cbce34a5 | 11219 | /* When we parsed the expression, we determined whether or |
11220 | not Koenig lookup should be performed. */ | |
11221 | koenig_p = KOENIG_LOOKUP_P (t); | |
ef4534a3 | 11222 | if (TREE_CODE (function) == SCOPE_REF) |
33643032 | 11223 | { |
ef4534a3 | 11224 | qualified_p = true; |
11225 | function = tsubst_qualified_id (function, args, complain, in_decl, | |
9031d10b | 11226 | /*done=*/false, |
ef4534a3 | 11227 | /*address_p=*/false); |
33643032 | 11228 | } |
ef4534a3 | 11229 | else |
33643032 | 11230 | { |
7276a1d0 | 11231 | if (TREE_CODE (function) == COMPONENT_REF) |
11232 | { | |
11233 | tree op = TREE_OPERAND (function, 1); | |
11234 | ||
11235 | qualified_p = (TREE_CODE (op) == SCOPE_REF | |
11236 | || (BASELINK_P (op) | |
11237 | && BASELINK_QUALIFIED_P (op))); | |
11238 | } | |
11239 | else | |
11240 | qualified_p = false; | |
074ab442 | 11241 | |
9031d10b | 11242 | function = tsubst_copy_and_build (function, args, complain, |
0886adbc | 11243 | in_decl, |
bde9ebf7 | 11244 | !qualified_p, |
11245 | integral_constant_expression_p); | |
11246 | ||
cbce34a5 | 11247 | if (BASELINK_P (function)) |
11248 | qualified_p = true; | |
33643032 | 11249 | } |
33643032 | 11250 | |
c2f47e15 | 11251 | /* FIXME: Rewrite this so as not to construct an arglist. */ |
11252 | call_args = RECUR (CALL_EXPR_ARGS (t)); | |
3ed12242 | 11253 | |
11254 | /* We do not perform argument-dependent lookup if normal | |
11255 | lookup finds a non-function, in accordance with the | |
11256 | expected resolution of DR 218. */ | |
c0b419db | 11257 | if (koenig_p |
8b652e89 | 11258 | && ((is_overloaded_fn (function) |
11259 | /* If lookup found a member function, the Koenig lookup is | |
11260 | not appropriate, even if an unqualified-name was used | |
11261 | to denote the function. */ | |
11262 | && !DECL_FUNCTION_MEMBER_P (get_first_fn (function))) | |
0886adbc | 11263 | || TREE_CODE (function) == IDENTIFIER_NODE)) |
cbce34a5 | 11264 | function = perform_koenig_lookup (function, call_args); |
11265 | ||
11266 | if (TREE_CODE (function) == IDENTIFIER_NODE) | |
0886adbc | 11267 | { |
cbce34a5 | 11268 | unqualified_name_lookup_error (function); |
11269 | return error_mark_node; | |
0886adbc | 11270 | } |
11271 | ||
11272 | /* Remember that there was a reference to this entity. */ | |
11273 | if (DECL_P (function)) | |
11274 | mark_used (function); | |
11275 | ||
13795292 | 11276 | if (TREE_CODE (function) == OFFSET_REF) |
11277 | return build_offset_ref_call_from_tree (function, call_args); | |
11278 | if (TREE_CODE (function) == COMPONENT_REF) | |
3c33f9f3 | 11279 | { |
11280 | if (!BASELINK_P (TREE_OPERAND (function, 1))) | |
11281 | return finish_call_expr (function, call_args, | |
11282 | /*disallow_virtual=*/false, | |
ebd21de4 | 11283 | /*koenig_p=*/false, |
11284 | complain); | |
3c33f9f3 | 11285 | else |
9031d10b | 11286 | return (build_new_method_call |
3c33f9f3 | 11287 | (TREE_OPERAND (function, 0), |
11288 | TREE_OPERAND (function, 1), | |
9031d10b | 11289 | call_args, NULL_TREE, |
393f878f | 11290 | qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL, |
ebd21de4 | 11291 | /*fn_p=*/NULL, |
11292 | complain)); | |
3c33f9f3 | 11293 | } |
9031d10b | 11294 | return finish_call_expr (function, call_args, |
cbce34a5 | 11295 | /*disallow_virtual=*/qualified_p, |
ebd21de4 | 11296 | koenig_p, |
11297 | complain); | |
33643032 | 11298 | } |
11299 | ||
11300 | case COND_EXPR: | |
11301 | return build_x_conditional_expr | |
0886adbc | 11302 | (RECUR (TREE_OPERAND (t, 0)), |
11303 | RECUR (TREE_OPERAND (t, 1)), | |
ebd21de4 | 11304 | RECUR (TREE_OPERAND (t, 2)), |
11305 | complain); | |
33643032 | 11306 | |
11307 | case PSEUDO_DTOR_EXPR: | |
9031d10b | 11308 | return finish_pseudo_destructor_expr |
0886adbc | 11309 | (RECUR (TREE_OPERAND (t, 0)), |
11310 | RECUR (TREE_OPERAND (t, 1)), | |
11311 | RECUR (TREE_OPERAND (t, 2))); | |
33643032 | 11312 | |
11313 | case TREE_LIST: | |
11314 | { | |
11315 | tree purpose, value, chain; | |
11316 | ||
11317 | if (t == void_list_node) | |
11318 | return t; | |
11319 | ||
d95d815d | 11320 | if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t))) |
11321 | || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t)))) | |
11322 | { | |
11323 | /* We have pack expansions, so expand those and | |
11324 | create a new list out of it. */ | |
11325 | tree purposevec = NULL_TREE; | |
11326 | tree valuevec = NULL_TREE; | |
11327 | tree chain; | |
11328 | int i, len = -1; | |
11329 | ||
11330 | /* Expand the argument expressions. */ | |
11331 | if (TREE_PURPOSE (t)) | |
11332 | purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args, | |
11333 | complain, in_decl); | |
11334 | if (TREE_VALUE (t)) | |
11335 | valuevec = tsubst_pack_expansion (TREE_VALUE (t), args, | |
11336 | complain, in_decl); | |
11337 | ||
11338 | /* Build the rest of the list. */ | |
11339 | chain = TREE_CHAIN (t); | |
11340 | if (chain && chain != void_type_node) | |
11341 | chain = RECUR (chain); | |
11342 | ||
11343 | /* Determine the number of arguments. */ | |
11344 | if (purposevec && TREE_CODE (purposevec) == TREE_VEC) | |
11345 | { | |
11346 | len = TREE_VEC_LENGTH (purposevec); | |
11347 | gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec)); | |
11348 | } | |
11349 | else if (TREE_CODE (valuevec) == TREE_VEC) | |
11350 | len = TREE_VEC_LENGTH (valuevec); | |
11351 | else | |
11352 | { | |
11353 | /* Since we only performed a partial substitution into | |
11354 | the argument pack, we only return a single list | |
11355 | node. */ | |
11356 | if (purposevec == TREE_PURPOSE (t) | |
11357 | && valuevec == TREE_VALUE (t) | |
11358 | && chain == TREE_CHAIN (t)) | |
11359 | return t; | |
11360 | ||
11361 | return tree_cons (purposevec, valuevec, chain); | |
11362 | } | |
11363 | ||
11364 | /* Convert the argument vectors into a TREE_LIST */ | |
11365 | i = len; | |
11366 | while (i > 0) | |
11367 | { | |
11368 | /* Grab the Ith values. */ | |
11369 | i--; | |
11370 | purpose = purposevec ? TREE_VEC_ELT (purposevec, i) | |
11371 | : NULL_TREE; | |
11372 | value | |
11373 | = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i)) | |
11374 | : NULL_TREE; | |
11375 | ||
11376 | /* Build the list (backwards). */ | |
11377 | chain = tree_cons (purpose, value, chain); | |
11378 | } | |
11379 | ||
11380 | return chain; | |
11381 | } | |
11382 | ||
33643032 | 11383 | purpose = TREE_PURPOSE (t); |
11384 | if (purpose) | |
0886adbc | 11385 | purpose = RECUR (purpose); |
33643032 | 11386 | value = TREE_VALUE (t); |
11387 | if (value) | |
0886adbc | 11388 | value = RECUR (value); |
33643032 | 11389 | chain = TREE_CHAIN (t); |
11390 | if (chain && chain != void_type_node) | |
0886adbc | 11391 | chain = RECUR (chain); |
33643032 | 11392 | if (purpose == TREE_PURPOSE (t) |
11393 | && value == TREE_VALUE (t) | |
11394 | && chain == TREE_CHAIN (t)) | |
11395 | return t; | |
11396 | return tree_cons (purpose, value, chain); | |
11397 | } | |
11398 | ||
11399 | case COMPONENT_REF: | |
11400 | { | |
ef4534a3 | 11401 | tree object; |
d023a9aa | 11402 | tree object_type; |
ef4534a3 | 11403 | tree member; |
11404 | ||
0886adbc | 11405 | object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0), |
11406 | args, complain, in_decl); | |
ef4534a3 | 11407 | /* Remember that there was a reference to this entity. */ |
11408 | if (DECL_P (object)) | |
11409 | mark_used (object); | |
d023a9aa | 11410 | object_type = TREE_TYPE (object); |
ef4534a3 | 11411 | |
11412 | member = TREE_OPERAND (t, 1); | |
11413 | if (BASELINK_P (member)) | |
9031d10b | 11414 | member = tsubst_baselink (member, |
ef4534a3 | 11415 | non_reference (TREE_TYPE (object)), |
11416 | args, complain, in_decl); | |
11417 | else | |
11418 | member = tsubst_copy (member, args, complain, in_decl); | |
9a4312bd | 11419 | if (member == error_mark_node) |
11420 | return error_mark_node; | |
d023a9aa | 11421 | |
11422 | if (object_type && !CLASS_TYPE_P (object_type)) | |
33643032 | 11423 | { |
1c8dc672 | 11424 | if (SCALAR_TYPE_P (object_type)) |
11425 | { | |
11426 | tree s = NULL_TREE; | |
11427 | tree dtor = member; | |
11428 | ||
11429 | if (TREE_CODE (dtor) == SCOPE_REF) | |
11430 | { | |
11431 | s = TREE_OPERAND (dtor, 0); | |
11432 | dtor = TREE_OPERAND (dtor, 1); | |
11433 | } | |
11434 | if (TREE_CODE (dtor) == BIT_NOT_EXPR) | |
11435 | { | |
11436 | dtor = TREE_OPERAND (dtor, 0); | |
11437 | if (TYPE_P (dtor)) | |
11438 | return finish_pseudo_destructor_expr (object, s, dtor); | |
11439 | } | |
11440 | } | |
33643032 | 11441 | } |
11442 | else if (TREE_CODE (member) == SCOPE_REF | |
11443 | && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR) | |
11444 | { | |
11445 | tree tmpl; | |
11446 | tree args; | |
9031d10b | 11447 | |
33643032 | 11448 | /* Lookup the template functions now that we know what the |
11449 | scope is. */ | |
11450 | tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0); | |
11451 | args = TREE_OPERAND (TREE_OPERAND (member, 1), 1); | |
9031d10b | 11452 | member = lookup_qualified_name (TREE_OPERAND (member, 0), tmpl, |
c4cf31e6 | 11453 | /*is_type_p=*/false, |
11454 | /*complain=*/false); | |
33643032 | 11455 | if (BASELINK_P (member)) |
8b652e89 | 11456 | { |
9031d10b | 11457 | BASELINK_FUNCTIONS (member) |
8b652e89 | 11458 | = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member), |
11459 | args); | |
9031d10b | 11460 | member = (adjust_result_of_qualified_name_lookup |
11461 | (member, BINFO_TYPE (BASELINK_BINFO (member)), | |
d023a9aa | 11462 | object_type)); |
8b652e89 | 11463 | } |
33643032 | 11464 | else |
11465 | { | |
d023a9aa | 11466 | qualified_name_lookup_error (object_type, tmpl, member); |
33643032 | 11467 | return error_mark_node; |
11468 | } | |
11469 | } | |
534d03ee | 11470 | else if (TREE_CODE (member) == SCOPE_REF |
11471 | && !CLASS_TYPE_P (TREE_OPERAND (member, 0)) | |
11472 | && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL) | |
11473 | { | |
11474 | if (complain & tf_error) | |
11475 | { | |
11476 | if (TYPE_P (TREE_OPERAND (member, 0))) | |
9031d10b | 11477 | error ("%qT is not a class or namespace", |
534d03ee | 11478 | TREE_OPERAND (member, 0)); |
11479 | else | |
9031d10b | 11480 | error ("%qD is not a class or namespace", |
534d03ee | 11481 | TREE_OPERAND (member, 0)); |
11482 | } | |
11483 | return error_mark_node; | |
11484 | } | |
26d880e6 | 11485 | else if (TREE_CODE (member) == FIELD_DECL) |
11486 | return finish_non_static_data_member (member, object, NULL_TREE); | |
33643032 | 11487 | |
fbb01da7 | 11488 | return finish_class_member_access_expr (object, member, |
ebd21de4 | 11489 | /*template_p=*/false, |
11490 | complain); | |
33643032 | 11491 | } |
11492 | ||
11493 | case THROW_EXPR: | |
11494 | return build_throw | |
0886adbc | 11495 | (RECUR (TREE_OPERAND (t, 0))); |
33643032 | 11496 | |
11497 | case CONSTRUCTOR: | |
11498 | { | |
c75b4594 | 11499 | VEC(constructor_elt,gc) *n; |
11500 | constructor_elt *ce; | |
11501 | unsigned HOST_WIDE_INT idx; | |
33643032 | 11502 | tree type = tsubst (TREE_TYPE (t), args, complain, in_decl); |
c75b4594 | 11503 | bool process_index_p; |
d95d815d | 11504 | int newlen; |
11505 | bool need_copy_p = false; | |
33643032 | 11506 | |
8de7bc60 | 11507 | if (type == error_mark_node) |
11508 | return error_mark_node; | |
11509 | ||
33643032 | 11510 | /* digest_init will do the wrong thing if we let it. */ |
11511 | if (type && TYPE_PTRMEMFUNC_P (type)) | |
11512 | return t; | |
11513 | ||
c75b4594 | 11514 | /* We do not want to process the index of aggregate |
33643032 | 11515 | initializers as they are identifier nodes which will be |
11516 | looked up by digest_init. */ | |
95397ff9 | 11517 | process_index_p = !(type && MAYBE_CLASS_TYPE_P (type)); |
9031d10b | 11518 | |
c75b4594 | 11519 | n = VEC_copy (constructor_elt, gc, CONSTRUCTOR_ELTS (t)); |
d95d815d | 11520 | newlen = VEC_length (constructor_elt, n); |
c75b4594 | 11521 | for (idx = 0; VEC_iterate (constructor_elt, n, idx, ce); idx++) |
11522 | { | |
11523 | if (ce->index && process_index_p) | |
11524 | ce->index = RECUR (ce->index); | |
d95d815d | 11525 | |
11526 | if (PACK_EXPANSION_P (ce->value)) | |
11527 | { | |
11528 | /* Substitute into the pack expansion. */ | |
11529 | ce->value = tsubst_pack_expansion (ce->value, args, complain, | |
11530 | in_decl); | |
11531 | ||
1db95a0f | 11532 | if (ce->value == error_mark_node) |
11533 | ; | |
11534 | else if (TREE_VEC_LENGTH (ce->value) == 1) | |
d95d815d | 11535 | /* Just move the argument into place. */ |
11536 | ce->value = TREE_VEC_ELT (ce->value, 0); | |
11537 | else | |
11538 | { | |
11539 | /* Update the length of the final CONSTRUCTOR | |
11540 | arguments vector, and note that we will need to | |
11541 | copy.*/ | |
11542 | newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1; | |
11543 | need_copy_p = true; | |
11544 | } | |
11545 | } | |
11546 | else | |
11547 | ce->value = RECUR (ce->value); | |
33643032 | 11548 | } |
9031d10b | 11549 | |
d95d815d | 11550 | if (need_copy_p) |
11551 | { | |
11552 | VEC(constructor_elt,gc) *old_n = n; | |
11553 | ||
11554 | n = VEC_alloc (constructor_elt, gc, newlen); | |
11555 | for (idx = 0; VEC_iterate (constructor_elt, old_n, idx, ce); | |
11556 | idx++) | |
11557 | { | |
11558 | if (TREE_CODE (ce->value) == TREE_VEC) | |
11559 | { | |
11560 | int i, len = TREE_VEC_LENGTH (ce->value); | |
11561 | for (i = 0; i < len; ++i) | |
11562 | CONSTRUCTOR_APPEND_ELT (n, 0, | |
11563 | TREE_VEC_ELT (ce->value, i)); | |
11564 | } | |
11565 | else | |
11566 | CONSTRUCTOR_APPEND_ELT (n, 0, ce->value); | |
11567 | } | |
11568 | } | |
11569 | ||
1d8baa0e | 11570 | if (TREE_HAS_CONSTRUCTOR (t)) |
11571 | return finish_compound_literal (type, n); | |
33643032 | 11572 | |
1d8baa0e | 11573 | return build_constructor (NULL_TREE, n); |
33643032 | 11574 | } |
11575 | ||
11576 | case TYPEID_EXPR: | |
11577 | { | |
0886adbc | 11578 | tree operand_0 = RECUR (TREE_OPERAND (t, 0)); |
33643032 | 11579 | if (TYPE_P (operand_0)) |
11580 | return get_typeid (operand_0); | |
11581 | return build_typeid (operand_0); | |
11582 | } | |
11583 | ||
33643032 | 11584 | case VAR_DECL: |
729f89ff | 11585 | if (!args) |
11586 | return t; | |
11587 | /* Fall through */ | |
9031d10b | 11588 | |
729f89ff | 11589 | case PARM_DECL: |
11590 | { | |
11591 | tree r = tsubst_copy (t, args, complain, in_decl); | |
9031d10b | 11592 | |
729f89ff | 11593 | if (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE) |
11594 | /* If the original type was a reference, we'll be wrapped in | |
11595 | the appropriate INDIRECT_REF. */ | |
11596 | r = convert_from_reference (r); | |
11597 | return r; | |
11598 | } | |
33643032 | 11599 | |
11600 | case VA_ARG_EXPR: | |
0886adbc | 11601 | return build_x_va_arg (RECUR (TREE_OPERAND (t, 0)), |
9031d10b | 11602 | tsubst_copy (TREE_TYPE (t), args, complain, |
0886adbc | 11603 | in_decl)); |
33643032 | 11604 | |
0f248b08 | 11605 | case OFFSETOF_EXPR: |
bf75f33a | 11606 | return finish_offsetof (RECUR (TREE_OPERAND (t, 0))); |
0f248b08 | 11607 | |
481451eb | 11608 | case TRAIT_EXPR: |
11609 | { | |
11610 | tree type1 = tsubst_copy (TRAIT_EXPR_TYPE1 (t), args, | |
11611 | complain, in_decl); | |
11612 | ||
11613 | tree type2 = TRAIT_EXPR_TYPE2 (t); | |
11614 | if (type2) | |
11615 | type2 = tsubst_copy (type2, args, complain, in_decl); | |
11616 | ||
11617 | return finish_trait_expr (TRAIT_EXPR_KIND (t), type1, type2); | |
11618 | } | |
11619 | ||
0d42c4e1 | 11620 | case STMT_EXPR: |
11621 | { | |
11622 | tree old_stmt_expr = cur_stmt_expr; | |
11623 | tree stmt_expr = begin_stmt_expr (); | |
11624 | ||
11625 | cur_stmt_expr = stmt_expr; | |
bde9ebf7 | 11626 | tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl, |
11627 | integral_constant_expression_p); | |
0d42c4e1 | 11628 | stmt_expr = finish_stmt_expr (stmt_expr, false); |
11629 | cur_stmt_expr = old_stmt_expr; | |
11630 | ||
11631 | return stmt_expr; | |
11632 | } | |
11633 | ||
46f43a6b | 11634 | case CONST_DECL: |
11635 | t = tsubst_copy (t, args, complain, in_decl); | |
11636 | /* As in finish_id_expression, we resolve enumeration constants | |
11637 | to their underlying values. */ | |
11638 | if (TREE_CODE (t) == CONST_DECL) | |
89c30811 | 11639 | { |
11640 | used_types_insert (TREE_TYPE (t)); | |
11641 | return DECL_INITIAL (t); | |
11642 | } | |
46f43a6b | 11643 | return t; |
11644 | ||
33643032 | 11645 | default: |
7a4e126b | 11646 | /* Handle Objective-C++ constructs, if appropriate. */ |
11647 | { | |
11648 | tree subst | |
11649 | = objcp_tsubst_copy_and_build (t, args, complain, | |
11650 | in_decl, /*function_p=*/false); | |
11651 | if (subst) | |
11652 | return subst; | |
11653 | } | |
33643032 | 11654 | return tsubst_copy (t, args, complain, in_decl); |
11655 | } | |
0886adbc | 11656 | |
11657 | #undef RECUR | |
33643032 | 11658 | } |
11659 | ||
2bc53434 | 11660 | /* Verify that the instantiated ARGS are valid. For type arguments, |
11661 | make sure that the type's linkage is ok. For non-type arguments, | |
755edffd | 11662 | make sure they are constants if they are integral or enumerations. |
6beb3f76 | 11663 | Emit an error under control of COMPLAIN, and return TRUE on error. */ |
2bc53434 | 11664 | |
11665 | static bool | |
807be5b4 | 11666 | check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain) |
2bc53434 | 11667 | { |
11668 | int ix, len = DECL_NTPARMS (tmpl); | |
11669 | bool result = false; | |
11670 | ||
11671 | for (ix = 0; ix != len; ix++) | |
11672 | { | |
11673 | tree t = TREE_VEC_ELT (args, ix); | |
9031d10b | 11674 | |
2bc53434 | 11675 | if (TYPE_P (t)) |
11676 | { | |
11677 | /* [basic.link]: A name with no linkage (notably, the name | |
11678 | of a class or enumeration declared in a local scope) | |
11679 | shall not be used to declare an entity with linkage. | |
11680 | This implies that names with no linkage cannot be used as | |
11681 | template arguments. */ | |
caa6fdce | 11682 | tree nt = no_linkage_check (t, /*relaxed_p=*/false); |
2bc53434 | 11683 | |
11684 | if (nt) | |
11685 | { | |
159fae15 | 11686 | /* DR 488 makes use of a type with no linkage cause |
074ab442 | 11687 | type deduction to fail. */ |
0f35bd1d | 11688 | if (complain & tf_error) |
11689 | { | |
11690 | if (TYPE_ANONYMOUS_P (nt)) | |
11691 | error ("%qT is/uses anonymous type", t); | |
11692 | else | |
fef91456 | 11693 | error ("template argument for %qD uses local type %qT", |
074ab442 | 11694 | tmpl, t); |
0f35bd1d | 11695 | } |
2bc53434 | 11696 | result = true; |
11697 | } | |
11698 | /* In order to avoid all sorts of complications, we do not | |
11699 | allow variably-modified types as template arguments. */ | |
1f8a6ff8 | 11700 | else if (variably_modified_type_p (t, NULL_TREE)) |
2bc53434 | 11701 | { |
11702 | if (complain & tf_error) | |
39e7b4a4 | 11703 | error ("%qT is a variably modified type", t); |
2bc53434 | 11704 | result = true; |
11705 | } | |
11706 | } | |
11707 | /* A non-type argument of integral or enumerated type must be a | |
11708 | constant. */ | |
11709 | else if (TREE_TYPE (t) | |
11710 | && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t)) | |
11711 | && !TREE_CONSTANT (t)) | |
11712 | { | |
11713 | if (complain & tf_error) | |
39e7b4a4 | 11714 | error ("integral expression %qE is not constant", t); |
2bc53434 | 11715 | result = true; |
11716 | } | |
11717 | } | |
0f35bd1d | 11718 | if (result && (complain & tf_error)) |
39e7b4a4 | 11719 | error (" trying to instantiate %qD", tmpl); |
2bc53434 | 11720 | return result; |
11721 | } | |
11722 | ||
df5066e2 | 11723 | /* Instantiate the indicated variable or function template TMPL with |
34197853 | 11724 | the template arguments in TARG_PTR. */ |
11725 | ||
e857e9c7 | 11726 | tree |
807be5b4 | 11727 | instantiate_template (tree tmpl, tree targ_ptr, tsubst_flags_t complain) |
471086d6 | 11728 | { |
e857e9c7 | 11729 | tree fndecl; |
34197853 | 11730 | tree gen_tmpl; |
11731 | tree spec; | |
54daf078 | 11732 | HOST_WIDE_INT saved_processing_template_decl; |
e857e9c7 | 11733 | |
063e90ea | 11734 | if (tmpl == error_mark_node) |
11735 | return error_mark_node; | |
11736 | ||
b4df430b | 11737 | gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL); |
b1cfe2be | 11738 | |
e55cba4c | 11739 | /* If this function is a clone, handle it specially. */ |
11740 | if (DECL_CLONED_FUNCTION_P (tmpl)) | |
a91aa990 | 11741 | { |
028e46e7 | 11742 | tree spec; |
a91aa990 | 11743 | tree clone; |
9031d10b | 11744 | |
028e46e7 | 11745 | spec = instantiate_template (DECL_CLONED_FUNCTION (tmpl), targ_ptr, |
11746 | complain); | |
11747 | if (spec == error_mark_node) | |
11748 | return error_mark_node; | |
11749 | ||
47cd6605 | 11750 | /* Look for the clone. */ |
caa6fdce | 11751 | FOR_EACH_CLONE (clone, spec) |
a91aa990 | 11752 | if (DECL_NAME (clone) == DECL_NAME (tmpl)) |
11753 | return clone; | |
11754 | /* We should always have found the clone by now. */ | |
2e3e31d2 | 11755 | gcc_unreachable (); |
a91aa990 | 11756 | return NULL_TREE; |
11757 | } | |
9031d10b | 11758 | |
34197853 | 11759 | /* Check to see if we already have this specialization. */ |
9031d10b | 11760 | spec = retrieve_specialization (tmpl, targ_ptr, |
38d89ee9 | 11761 | /*class_specializations_p=*/false); |
34197853 | 11762 | if (spec != NULL_TREE) |
11763 | return spec; | |
11764 | ||
a32fe883 | 11765 | gen_tmpl = most_general_template (tmpl); |
11766 | if (tmpl != gen_tmpl) | |
b1cfe2be | 11767 | { |
34197853 | 11768 | /* The TMPL is a partial instantiation. To get a full set of |
11769 | arguments we must add the arguments used to perform the | |
11770 | partial instantiation. */ | |
11771 | targ_ptr = add_outermost_template_args (DECL_TI_ARGS (tmpl), | |
11772 | targ_ptr); | |
34197853 | 11773 | |
11774 | /* Check to see if we already have this specialization. */ | |
38d89ee9 | 11775 | spec = retrieve_specialization (gen_tmpl, targ_ptr, |
11776 | /*class_specializations_p=*/false); | |
668ae905 | 11777 | if (spec != NULL_TREE) |
11778 | return spec; | |
b1cfe2be | 11779 | } |
11780 | ||
2bc53434 | 11781 | if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr), |
11782 | complain)) | |
11783 | return error_mark_node; | |
9031d10b | 11784 | |
9014db85 | 11785 | /* We are building a FUNCTION_DECL, during which the access of its |
11786 | parameters and return types have to be checked. However this | |
11787 | FUNCTION_DECL which is the desired context for access checking | |
11788 | is not built yet. We solve this chicken-and-egg problem by | |
11789 | deferring all checks until we have the FUNCTION_DECL. */ | |
11790 | push_deferring_access_checks (dk_deferred); | |
ff207dc8 | 11791 | |
54daf078 | 11792 | /* Although PROCESSING_TEMPLATE_DECL may be true at this point |
11793 | (because, for example, we have encountered a non-dependent | |
21d0dc67 | 11794 | function call in the body of a template function and must now |
11795 | determine which of several overloaded functions will be called), | |
11796 | within the instantiation itself we are not processing a | |
11797 | template. */ | |
54daf078 | 11798 | saved_processing_template_decl = processing_template_decl; |
11799 | processing_template_decl = 0; | |
11800 | /* Substitute template parameters to obtain the specialization. */ | |
b8e0d419 | 11801 | fndecl = tsubst (DECL_TEMPLATE_RESULT (gen_tmpl), |
2bc53434 | 11802 | targ_ptr, complain, gen_tmpl); |
54daf078 | 11803 | processing_template_decl = saved_processing_template_decl; |
528bbcf5 | 11804 | if (fndecl == error_mark_node) |
11805 | return error_mark_node; | |
ff207dc8 | 11806 | |
9014db85 | 11807 | /* Now we know the specialization, compute access previously |
11808 | deferred. */ | |
11809 | push_access_scope (fndecl); | |
11810 | perform_deferred_access_checks (); | |
11811 | pop_access_scope (fndecl); | |
11812 | pop_deferring_access_checks (); | |
ff207dc8 | 11813 | |
34197853 | 11814 | /* The DECL_TI_TEMPLATE should always be the immediate parent |
11815 | template, not the most general template. */ | |
11816 | DECL_TI_TEMPLATE (fndecl) = tmpl; | |
471086d6 | 11817 | |
aa6b4383 | 11818 | /* If we've just instantiated the main entry point for a function, |
a91aa990 | 11819 | instantiate all the alternate entry points as well. We do this |
11820 | by cloning the instantiation of the main entry point, not by | |
11821 | instantiating the template clones. */ | |
11822 | if (TREE_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (TREE_CHAIN (gen_tmpl))) | |
11823 | clone_function_decl (fndecl, /*update_method_vec_p=*/0); | |
aa6b4383 | 11824 | |
e857e9c7 | 11825 | return fndecl; |
471086d6 | 11826 | } |
e857e9c7 | 11827 | |
6d158f4d | 11828 | /* The FN is a TEMPLATE_DECL for a function. The ARGS are the |
11829 | arguments that are being used when calling it. TARGS is a vector | |
9031d10b | 11830 | into which the deduced template arguments are placed. |
471086d6 | 11831 | |
11832 | Return zero for success, 2 for an incomplete match that doesn't resolve | |
11833 | all the types, and 1 for complete failure. An error message will be | |
11834 | printed only for an incomplete match. | |
11835 | ||
2db0e9b3 | 11836 | If FN is a conversion operator, or we are trying to produce a specific |
11837 | specialization, RETURN_TYPE is the return type desired. | |
a2569099 | 11838 | |
11839 | The EXPLICIT_TARGS are explicit template arguments provided via a | |
11840 | template-id. | |
596c0ae6 | 11841 | |
32b5f03e | 11842 | The parameter STRICT is one of: |
11843 | ||
9031d10b | 11844 | DEDUCE_CALL: |
32b5f03e | 11845 | We are deducing arguments for a function call, as in |
11846 | [temp.deduct.call]. | |
11847 | ||
11848 | DEDUCE_CONV: | |
9031d10b | 11849 | We are deducing arguments for a conversion function, as in |
32b5f03e | 11850 | [temp.deduct.conv]. |
11851 | ||
11852 | DEDUCE_EXACT: | |
efa31275 | 11853 | We are deducing arguments when doing an explicit instantiation |
11854 | as in [temp.explicit], when determining an explicit specialization | |
11855 | as in [temp.expl.spec], or when taking the address of a function | |
022b46b4 | 11856 | template, as in [temp.deduct.funcaddr]. */ |
471086d6 | 11857 | |
11858 | int | |
9031d10b | 11859 | fn_type_unification (tree fn, |
653e5405 | 11860 | tree explicit_targs, |
11861 | tree targs, | |
11862 | tree args, | |
11863 | tree return_type, | |
db3bf8f6 | 11864 | unification_kind_t strict, |
11865 | int flags) | |
b1cfe2be | 11866 | { |
6d158f4d | 11867 | tree parms; |
11868 | tree fntype; | |
e9c0ac6a | 11869 | int result; |
d95d815d | 11870 | bool incomplete_argument_packs_p = false; |
b1cfe2be | 11871 | |
b4df430b | 11872 | gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL); |
7a468236 | 11873 | |
6d158f4d | 11874 | fntype = TREE_TYPE (fn); |
11875 | if (explicit_targs) | |
668ae905 | 11876 | { |
6d158f4d | 11877 | /* [temp.deduct] |
9031d10b | 11878 | |
6d158f4d | 11879 | The specified template arguments must match the template |
11880 | parameters in kind (i.e., type, nontype, template), and there | |
11881 | must not be more arguments than there are parameters; | |
11882 | otherwise type deduction fails. | |
11883 | ||
11884 | Nontype arguments must match the types of the corresponding | |
11885 | nontype template parameters, or must be convertible to the | |
11886 | types of the corresponding nontype parameters as specified in | |
11887 | _temp.arg.nontype_, otherwise type deduction fails. | |
11888 | ||
11889 | All references in the function type of the function template | |
11890 | to the corresponding template parameters are replaced by the | |
11891 | specified template argument values. If a substitution in a | |
11892 | template parameter or in the function type of the function | |
11893 | template results in an invalid type, type deduction fails. */ | |
d95d815d | 11894 | tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn); |
11895 | int i, len = TREE_VEC_LENGTH (tparms); | |
6d158f4d | 11896 | tree converted_args; |
d95d815d | 11897 | bool incomplete = false; |
668ae905 | 11898 | |
67635103 | 11899 | if (explicit_targs == error_mark_node) |
11900 | return 1; | |
11901 | ||
6d158f4d | 11902 | converted_args |
d95d815d | 11903 | = (coerce_template_parms (tparms, explicit_targs, NULL_TREE, tf_none, |
36653a30 | 11904 | /*require_all_args=*/false, |
11905 | /*use_default_args=*/false)); | |
6d158f4d | 11906 | if (converted_args == error_mark_node) |
668ae905 | 11907 | return 1; |
b1cfe2be | 11908 | |
8b070b49 | 11909 | /* Substitute the explicit args into the function type. This is |
653e5405 | 11910 | necessary so that, for instance, explicitly declared function |
11911 | arguments can match null pointed constants. If we were given | |
11912 | an incomplete set of explicit args, we must not do semantic | |
11913 | processing during substitution as we could create partial | |
11914 | instantiations. */ | |
d95d815d | 11915 | for (i = 0; i < len; i++) |
11916 | { | |
11917 | tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i)); | |
11918 | bool parameter_pack = false; | |
11919 | ||
11920 | /* Dig out the actual parm. */ | |
11921 | if (TREE_CODE (parm) == TYPE_DECL | |
11922 | || TREE_CODE (parm) == TEMPLATE_DECL) | |
11923 | { | |
11924 | parm = TREE_TYPE (parm); | |
11925 | parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm); | |
11926 | } | |
11927 | else if (TREE_CODE (parm) == PARM_DECL) | |
11928 | { | |
11929 | parm = DECL_INITIAL (parm); | |
11930 | parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm); | |
11931 | } | |
11932 | ||
11933 | if (parameter_pack) | |
11934 | { | |
11935 | int level, idx; | |
11936 | tree targ; | |
11937 | template_parm_level_and_index (parm, &level, &idx); | |
11938 | ||
11939 | /* Mark the argument pack as "incomplete". We could | |
11940 | still deduce more arguments during unification. */ | |
11941 | targ = TMPL_ARG (converted_args, level, idx); | |
a4f66688 | 11942 | if (targ) |
11943 | { | |
11944 | ARGUMENT_PACK_INCOMPLETE_P(targ) = 1; | |
11945 | ARGUMENT_PACK_EXPLICIT_ARGS (targ) | |
11946 | = ARGUMENT_PACK_ARGS (targ); | |
11947 | } | |
d95d815d | 11948 | |
11949 | /* We have some incomplete argument packs. */ | |
11950 | incomplete_argument_packs_p = true; | |
11951 | } | |
11952 | } | |
11953 | ||
11954 | if (incomplete_argument_packs_p) | |
11955 | /* Any substitution is guaranteed to be incomplete if there | |
11956 | are incomplete argument packs, because we can still deduce | |
11957 | more arguments. */ | |
11958 | incomplete = 1; | |
11959 | else | |
11960 | incomplete = NUM_TMPL_ARGS (explicit_targs) != NUM_TMPL_ARGS (targs); | |
11961 | ||
8b070b49 | 11962 | processing_template_decl += incomplete; |
4b471722 | 11963 | fntype = tsubst (fntype, converted_args, tf_none, NULL_TREE); |
8b070b49 | 11964 | processing_template_decl -= incomplete; |
9031d10b | 11965 | |
6d158f4d | 11966 | if (fntype == error_mark_node) |
11967 | return 1; | |
c906a2a7 | 11968 | |
6d158f4d | 11969 | /* Place the explicitly specified arguments in TARGS. */ |
8b070b49 | 11970 | for (i = NUM_TMPL_ARGS (converted_args); i--;) |
6d158f4d | 11971 | TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (converted_args, i); |
668ae905 | 11972 | } |
9031d10b | 11973 | |
2db0e9b3 | 11974 | /* Never do unification on the 'this' parameter. */ |
14aa085c | 11975 | parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype)); |
9031d10b | 11976 | |
1b5e8868 | 11977 | if (return_type) |
11978 | { | |
1b5e8868 | 11979 | parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms); |
11980 | args = tree_cons (NULL_TREE, return_type, args); | |
6d158f4d | 11981 | } |
11982 | ||
6d158f4d | 11983 | /* We allow incomplete unification without an error message here |
11984 | because the standard doesn't seem to explicitly prohibit it. Our | |
11985 | callers must be ready to deal with unification failures in any | |
11986 | event. */ | |
9031d10b | 11987 | result = type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn), |
e9c0ac6a | 11988 | targs, parms, args, /*subr=*/0, |
db3bf8f6 | 11989 | strict, flags); |
e9c0ac6a | 11990 | |
d95d815d | 11991 | if (result == 0 && incomplete_argument_packs_p) |
11992 | { | |
11993 | int i, len = NUM_TMPL_ARGS (targs); | |
11994 | ||
11995 | /* Clear the "incomplete" flags on all argument packs. */ | |
11996 | for (i = 0; i < len; i++) | |
11997 | { | |
11998 | tree arg = TREE_VEC_ELT (targs, i); | |
11999 | if (ARGUMENT_PACK_P (arg)) | |
12000 | { | |
12001 | ARGUMENT_PACK_INCOMPLETE_P (arg) = 0; | |
12002 | ARGUMENT_PACK_EXPLICIT_ARGS (arg) = NULL_TREE; | |
12003 | } | |
12004 | } | |
12005 | } | |
12006 | ||
e5dab245 | 12007 | /* Now that we have bindings for all of the template arguments, |
12008 | ensure that the arguments deduced for the template template | |
12009 | parameters have compatible template parameter lists. We cannot | |
12010 | check this property before we have deduced all template | |
12011 | arguments, because the template parameter types of a template | |
12012 | template parameter might depend on prior template parameters | |
12013 | deduced after the template template parameter. The following | |
12014 | ill-formed example illustrates this issue: | |
12015 | ||
12016 | template<typename T, template<T> class C> void f(C<5>, T); | |
12017 | ||
12018 | template<int N> struct X {}; | |
12019 | ||
12020 | void g() { | |
12021 | f(X<5>(), 5l); // error: template argument deduction fails | |
12022 | } | |
12023 | ||
12024 | The template parameter list of 'C' depends on the template type | |
12025 | parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to | |
12026 | 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the | |
12027 | time that we deduce 'C'. */ | |
12028 | if (result == 0 | |
12029 | && !template_template_parm_bindings_ok_p | |
12030 | (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs)) | |
12031 | return 1; | |
12032 | ||
9031d10b | 12033 | if (result == 0) |
e9c0ac6a | 12034 | /* All is well so far. Now, check: |
9031d10b | 12035 | |
12036 | [temp.deduct] | |
12037 | ||
e9c0ac6a | 12038 | When all template arguments have been deduced, all uses of |
12039 | template parameters in nondeduced contexts are replaced with | |
12040 | the corresponding deduced argument values. If the | |
12041 | substitution results in an invalid type, as described above, | |
12042 | type deduction fails. */ | |
4b471722 | 12043 | if (tsubst (TREE_TYPE (fn), targs, tf_none, NULL_TREE) |
e9c0ac6a | 12044 | == error_mark_node) |
12045 | return 1; | |
12046 | ||
12047 | return result; | |
32b5f03e | 12048 | } |
12049 | ||
12050 | /* Adjust types before performing type deduction, as described in | |
12051 | [temp.deduct.call] and [temp.deduct.conv]. The rules in these two | |
006ef4f8 | 12052 | sections are symmetric. PARM is the type of a function parameter |
12053 | or the return type of the conversion function. ARG is the type of | |
12054 | the argument passed to the call, or the type of the value | |
63949b38 | 12055 | initialized with the result of the conversion function. |
12056 | ARG_EXPR is the original argument expression, which may be null. */ | |
b1cfe2be | 12057 | |
efa31275 | 12058 | static int |
9031d10b | 12059 | maybe_adjust_types_for_deduction (unification_kind_t strict, |
653e5405 | 12060 | tree* parm, |
63949b38 | 12061 | tree* arg, |
12062 | tree arg_expr) | |
32b5f03e | 12063 | { |
efa31275 | 12064 | int result = 0; |
9031d10b | 12065 | |
32b5f03e | 12066 | switch (strict) |
12067 | { | |
12068 | case DEDUCE_CALL: | |
12069 | break; | |
12070 | ||
12071 | case DEDUCE_CONV: | |
12072 | { | |
006ef4f8 | 12073 | /* Swap PARM and ARG throughout the remainder of this |
12074 | function; the handling is precisely symmetric since PARM | |
12075 | will initialize ARG rather than vice versa. */ | |
32b5f03e | 12076 | tree* temp = parm; |
12077 | parm = arg; | |
12078 | arg = temp; | |
12079 | break; | |
12080 | } | |
12081 | ||
12082 | case DEDUCE_EXACT: | |
12083 | /* There is nothing to do in this case. */ | |
efa31275 | 12084 | return 0; |
32b5f03e | 12085 | |
12086 | default: | |
2e3e31d2 | 12087 | gcc_unreachable (); |
32b5f03e | 12088 | } |
12089 | ||
12090 | if (TREE_CODE (*parm) != REFERENCE_TYPE) | |
12091 | { | |
12092 | /* [temp.deduct.call] | |
9031d10b | 12093 | |
32b5f03e | 12094 | If P is not a reference type: |
9031d10b | 12095 | |
32b5f03e | 12096 | --If A is an array type, the pointer type produced by the |
12097 | array-to-pointer standard conversion (_conv.array_) is | |
12098 | used in place of A for type deduction; otherwise, | |
9031d10b | 12099 | |
32b5f03e | 12100 | --If A is a function type, the pointer type produced by |
12101 | the function-to-pointer standard conversion | |
12102 | (_conv.func_) is used in place of A for type deduction; | |
12103 | otherwise, | |
9031d10b | 12104 | |
32b5f03e | 12105 | --If A is a cv-qualified type, the top level |
12106 | cv-qualifiers of A's type are ignored for type | |
12107 | deduction. */ | |
12108 | if (TREE_CODE (*arg) == ARRAY_TYPE) | |
12109 | *arg = build_pointer_type (TREE_TYPE (*arg)); | |
2b77484d | 12110 | else if (TREE_CODE (*arg) == FUNCTION_TYPE) |
32b5f03e | 12111 | *arg = build_pointer_type (*arg); |
12112 | else | |
12113 | *arg = TYPE_MAIN_VARIANT (*arg); | |
12114 | } | |
9031d10b | 12115 | |
63949b38 | 12116 | /* From C++0x [14.8.2.1/3 temp.deduct.call] (after DR606), "If P is |
12117 | of the form T&&, where T is a template parameter, and the argument | |
12118 | is an lvalue, T is deduced as A& */ | |
12119 | if (TREE_CODE (*parm) == REFERENCE_TYPE | |
12120 | && TYPE_REF_IS_RVALUE (*parm) | |
12121 | && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM | |
12122 | && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED | |
12123 | && arg_expr && real_lvalue_p (arg_expr)) | |
12124 | *arg = build_reference_type (*arg); | |
12125 | ||
32b5f03e | 12126 | /* [temp.deduct.call] |
9031d10b | 12127 | |
32b5f03e | 12128 | If P is a cv-qualified type, the top level cv-qualifiers |
12129 | of P's type are ignored for type deduction. If P is a | |
12130 | reference type, the type referred to by P is used for | |
12131 | type deduction. */ | |
12132 | *parm = TYPE_MAIN_VARIANT (*parm); | |
12133 | if (TREE_CODE (*parm) == REFERENCE_TYPE) | |
efa31275 | 12134 | { |
12135 | *parm = TREE_TYPE (*parm); | |
12136 | result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL; | |
12137 | } | |
a4c04854 | 12138 | |
12139 | /* DR 322. For conversion deduction, remove a reference type on parm | |
12140 | too (which has been swapped into ARG). */ | |
12141 | if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE) | |
12142 | *arg = TREE_TYPE (*arg); | |
9031d10b | 12143 | |
efa31275 | 12144 | return result; |
b1cfe2be | 12145 | } |
12146 | ||
2db0e9b3 | 12147 | /* Most parms like fn_type_unification. |
a2569099 | 12148 | |
12149 | If SUBR is 1, we're being called recursively (to unify the | |
12150 | arguments of a function or method parameter of a function | |
b31f4474 | 12151 | template). */ |
b1cfe2be | 12152 | |
b4841af8 | 12153 | static int |
9031d10b | 12154 | type_unification_real (tree tparms, |
653e5405 | 12155 | tree targs, |
12156 | tree xparms, | |
12157 | tree xargs, | |
12158 | int subr, | |
db3bf8f6 | 12159 | unification_kind_t strict, |
12160 | int flags) | |
471086d6 | 12161 | { |
63949b38 | 12162 | tree parm, arg, arg_expr; |
471086d6 | 12163 | int i; |
12164 | int ntparms = TREE_VEC_LENGTH (tparms); | |
32b5f03e | 12165 | int sub_strict; |
614cc70d | 12166 | int saw_undeduced = 0; |
12167 | tree parms, args; | |
471086d6 | 12168 | |
b4df430b | 12169 | gcc_assert (TREE_CODE (tparms) == TREE_VEC); |
12170 | gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST); | |
12171 | gcc_assert (!xargs || TREE_CODE (xargs) == TREE_LIST); | |
12172 | gcc_assert (ntparms > 0); | |
471086d6 | 12173 | |
32b5f03e | 12174 | switch (strict) |
12175 | { | |
12176 | case DEDUCE_CALL: | |
7e0d59b2 | 12177 | sub_strict = (UNIFY_ALLOW_OUTER_LEVEL | UNIFY_ALLOW_MORE_CV_QUAL |
653e5405 | 12178 | | UNIFY_ALLOW_DERIVED); |
32b5f03e | 12179 | break; |
9031d10b | 12180 | |
32b5f03e | 12181 | case DEDUCE_CONV: |
12182 | sub_strict = UNIFY_ALLOW_LESS_CV_QUAL; | |
12183 | break; | |
12184 | ||
12185 | case DEDUCE_EXACT: | |
12186 | sub_strict = UNIFY_ALLOW_NONE; | |
12187 | break; | |
9031d10b | 12188 | |
32b5f03e | 12189 | default: |
2e3e31d2 | 12190 | gcc_unreachable (); |
32b5f03e | 12191 | } |
12192 | ||
614cc70d | 12193 | again: |
12194 | parms = xparms; | |
12195 | args = xargs; | |
614cc70d | 12196 | |
022b46b4 | 12197 | while (parms && parms != void_list_node |
12198 | && args && args != void_list_node) | |
471086d6 | 12199 | { |
d95d815d | 12200 | if (TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION) |
12201 | break; | |
12202 | ||
471086d6 | 12203 | parm = TREE_VALUE (parms); |
12204 | parms = TREE_CHAIN (parms); | |
12205 | arg = TREE_VALUE (args); | |
12206 | args = TREE_CHAIN (args); | |
63949b38 | 12207 | arg_expr = NULL; |
471086d6 | 12208 | |
12209 | if (arg == error_mark_node) | |
12210 | return 1; | |
12211 | if (arg == unknown_type_node) | |
eaf0f63a | 12212 | /* We can't deduce anything from this, but we might get all the |
12213 | template args from other function args. */ | |
12214 | continue; | |
1e66592c | 12215 | |
d04cff64 | 12216 | /* Conversions will be performed on a function argument that |
12217 | corresponds with a function parameter that contains only | |
12218 | non-deducible template parameters and explicitly specified | |
12219 | template parameters. */ | |
7f233616 | 12220 | if (!uses_template_parms (parm)) |
1e66592c | 12221 | { |
d04cff64 | 12222 | tree type; |
12223 | ||
9308e976 | 12224 | if (!TYPE_P (arg)) |
d04cff64 | 12225 | type = TREE_TYPE (arg); |
12226 | else | |
7f233616 | 12227 | type = arg; |
d04cff64 | 12228 | |
4521a9fa | 12229 | if (same_type_p (parm, type)) |
8ddb3192 | 12230 | continue; |
4521a9fa | 12231 | if (strict != DEDUCE_EXACT |
074ab442 | 12232 | && can_convert_arg (parm, type, TYPE_P (arg) ? NULL_TREE : arg, |
db3bf8f6 | 12233 | flags)) |
4521a9fa | 12234 | continue; |
074ab442 | 12235 | |
1e66592c | 12236 | return 1; |
12237 | } | |
9031d10b | 12238 | |
9308e976 | 12239 | if (!TYPE_P (arg)) |
471086d6 | 12240 | { |
b4df430b | 12241 | gcc_assert (TREE_TYPE (arg) != NULL_TREE); |
eaf0f63a | 12242 | if (type_unknown_p (arg)) |
bea7d742 | 12243 | { |
9788fee1 | 12244 | /* [temp.deduct.type] |
eaf0f63a | 12245 | |
9788fee1 | 12246 | A template-argument can be deduced from a pointer to |
12247 | function or pointer to member function argument if | |
12248 | the set of overloaded functions does not contain | |
12249 | function templates and at most one of a set of | |
12250 | overloaded functions provides a unique match. */ | |
eaf0f63a | 12251 | if (resolve_overloaded_unification |
9788fee1 | 12252 | (tparms, targs, parm, arg, strict, sub_strict)) |
12253 | continue; | |
12254 | ||
12255 | return 1; | |
bea7d742 | 12256 | } |
63949b38 | 12257 | arg_expr = arg; |
154a4e70 | 12258 | arg = unlowered_expr_type (arg); |
2f0497a7 | 12259 | if (arg == error_mark_node) |
12260 | return 1; | |
471086d6 | 12261 | } |
9031d10b | 12262 | |
efa31275 | 12263 | { |
653e5405 | 12264 | int arg_strict = sub_strict; |
9031d10b | 12265 | |
653e5405 | 12266 | if (!subr) |
63949b38 | 12267 | arg_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, |
12268 | arg_expr); | |
6d158f4d | 12269 | |
653e5405 | 12270 | if (unify (tparms, targs, parm, arg, arg_strict)) |
12271 | return 1; | |
efa31275 | 12272 | } |
471086d6 | 12273 | } |
9031d10b | 12274 | |
d95d815d | 12275 | |
12276 | if (parms | |
12277 | && parms != void_list_node | |
12278 | && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION) | |
12279 | { | |
12280 | /* Unify the remaining arguments with the pack expansion type. */ | |
12281 | tree argvec; | |
12282 | tree parmvec = make_tree_vec (1); | |
12283 | int len = 0; | |
12284 | tree t; | |
12285 | ||
12286 | /* Count the number of arguments that remain. */ | |
12287 | for (t = args; t && t != void_list_node; t = TREE_CHAIN (t)) | |
12288 | len++; | |
12289 | ||
12290 | /* Allocate a TREE_VEC and copy in all of the arguments */ | |
12291 | argvec = make_tree_vec (len); | |
12292 | for (i = 0; args && args != void_list_node; args = TREE_CHAIN (args)) | |
12293 | { | |
12294 | TREE_VEC_ELT (argvec, i) = TREE_VALUE (args); | |
12295 | ++i; | |
12296 | } | |
12297 | ||
12298 | /* Copy the parameter into parmvec. */ | |
12299 | TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms); | |
12300 | if (unify_pack_expansion (tparms, targs, parmvec, argvec, strict, | |
12301 | /*call_args_p=*/true, /*subr=*/subr)) | |
12302 | return 1; | |
12303 | ||
12304 | /* Advance to the end of the list of parameters. */ | |
12305 | parms = TREE_CHAIN (parms); | |
12306 | } | |
12307 | ||
471086d6 | 12308 | /* Fail if we've reached the end of the parm list, and more args |
12309 | are present, and the parm list isn't variadic. */ | |
12310 | if (args && args != void_list_node && parms == void_list_node) | |
12311 | return 1; | |
331bc0ad | 12312 | /* Fail if parms are left and they don't have default values. */ |
022b46b4 | 12313 | if (parms && parms != void_list_node |
471086d6 | 12314 | && TREE_PURPOSE (parms) == NULL_TREE) |
12315 | return 1; | |
614cc70d | 12316 | |
471086d6 | 12317 | if (!subr) |
12318 | for (i = 0; i < ntparms; i++) | |
022b46b4 | 12319 | if (!TREE_VEC_ELT (targs, i)) |
471086d6 | 12320 | { |
6f32df61 | 12321 | tree tparm; |
12322 | ||
12323 | if (TREE_VEC_ELT (tparms, i) == error_mark_node) | |
12324 | continue; | |
12325 | ||
12326 | tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i)); | |
614cc70d | 12327 | |
12328 | /* If this is an undeduced nontype parameter that depends on | |
12329 | a type parameter, try another pass; its type may have been | |
12330 | deduced from a later argument than the one from which | |
12331 | this parameter can be deduced. */ | |
12332 | if (TREE_CODE (tparm) == PARM_DECL | |
12333 | && uses_template_parms (TREE_TYPE (tparm)) | |
12334 | && !saw_undeduced++) | |
12335 | goto again; | |
12336 | ||
82d31768 | 12337 | /* Core issue #226 (C++0x) [temp.deduct]: |
12338 | ||
12339 | If a template argument has not been deduced, its | |
12340 | default template argument, if any, is used. | |
12341 | ||
6dcdb5de | 12342 | When we are in C++98 mode, TREE_PURPOSE will either |
12343 | be NULL_TREE or ERROR_MARK_NODE, so we do not need | |
12344 | to explicitly check cxx_dialect here. */ | |
82d31768 | 12345 | if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i))) |
12346 | { | |
12347 | tree arg = tsubst (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)), | |
12348 | targs, tf_none, NULL_TREE); | |
12349 | if (arg == error_mark_node) | |
12350 | return 1; | |
12351 | else | |
12352 | { | |
12353 | TREE_VEC_ELT (targs, i) = arg; | |
12354 | continue; | |
12355 | } | |
12356 | } | |
12357 | ||
a4f66688 | 12358 | /* If the type parameter is a parameter pack, then it will |
12359 | be deduced to an empty parameter pack. */ | |
12360 | if (template_parameter_pack_p (tparm)) | |
12361 | { | |
12362 | tree arg; | |
12363 | ||
12364 | if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX) | |
12365 | { | |
12366 | arg = make_node (NONTYPE_ARGUMENT_PACK); | |
12367 | TREE_TYPE (arg) = TREE_TYPE (TEMPLATE_PARM_DECL (tparm)); | |
12368 | TREE_CONSTANT (arg) = 1; | |
12369 | } | |
12370 | else | |
12371 | arg = make_node (TYPE_ARGUMENT_PACK); | |
12372 | ||
12373 | SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0)); | |
12374 | ||
12375 | TREE_VEC_ELT (targs, i) = arg; | |
12376 | continue; | |
12377 | } | |
12378 | ||
471086d6 | 12379 | return 2; |
12380 | } | |
9031d10b | 12381 | |
471086d6 | 12382 | return 0; |
12383 | } | |
12384 | ||
9788fee1 | 12385 | /* Subroutine of type_unification_real. Args are like the variables |
12386 | at the call site. ARG is an overloaded function (or template-id); | |
12387 | we try deducing template args from each of the overloads, and if | |
12388 | only one succeeds, we go with that. Modifies TARGS and returns | |
12389 | true on success. */ | |
eaf0f63a | 12390 | |
9788fee1 | 12391 | static bool |
9031d10b | 12392 | resolve_overloaded_unification (tree tparms, |
653e5405 | 12393 | tree targs, |
12394 | tree parm, | |
12395 | tree arg, | |
12396 | unification_kind_t strict, | |
807be5b4 | 12397 | int sub_strict) |
eaf0f63a | 12398 | { |
12399 | tree tempargs = copy_node (targs); | |
12400 | int good = 0; | |
b44599c0 | 12401 | bool addr_p; |
eaf0f63a | 12402 | |
12403 | if (TREE_CODE (arg) == ADDR_EXPR) | |
b44599c0 | 12404 | { |
12405 | arg = TREE_OPERAND (arg, 0); | |
12406 | addr_p = true; | |
12407 | } | |
12408 | else | |
12409 | addr_p = false; | |
74467e75 | 12410 | |
2b77484d | 12411 | if (TREE_CODE (arg) == COMPONENT_REF) |
12412 | /* Handle `&x' where `x' is some static or non-static member | |
12413 | function name. */ | |
12414 | arg = TREE_OPERAND (arg, 1); | |
12415 | ||
42b9ec6a | 12416 | if (TREE_CODE (arg) == OFFSET_REF) |
12417 | arg = TREE_OPERAND (arg, 1); | |
12418 | ||
74467e75 | 12419 | /* Strip baselink information. */ |
4ac852cb | 12420 | if (BASELINK_P (arg)) |
12421 | arg = BASELINK_FUNCTIONS (arg); | |
74467e75 | 12422 | |
eaf0f63a | 12423 | if (TREE_CODE (arg) == TEMPLATE_ID_EXPR) |
12424 | { | |
12425 | /* If we got some explicit template args, we need to plug them into | |
12426 | the affected templates before we try to unify, in case the | |
12427 | explicit args will completely resolve the templates in question. */ | |
12428 | ||
12429 | tree expl_subargs = TREE_OPERAND (arg, 1); | |
12430 | arg = TREE_OPERAND (arg, 0); | |
12431 | ||
12432 | for (; arg; arg = OVL_NEXT (arg)) | |
12433 | { | |
12434 | tree fn = OVL_CURRENT (arg); | |
12435 | tree subargs, elem; | |
12436 | ||
12437 | if (TREE_CODE (fn) != TEMPLATE_DECL) | |
12438 | continue; | |
12439 | ||
f15749da | 12440 | ++processing_template_decl; |
022b46b4 | 12441 | subargs = get_bindings (fn, DECL_TEMPLATE_RESULT (fn), |
12442 | expl_subargs, /*check_ret=*/false); | |
eaf0f63a | 12443 | if (subargs) |
12444 | { | |
4b471722 | 12445 | elem = tsubst (TREE_TYPE (fn), subargs, tf_none, NULL_TREE); |
9031d10b | 12446 | good += try_one_overload (tparms, targs, tempargs, parm, |
b44599c0 | 12447 | elem, strict, sub_strict, addr_p); |
eaf0f63a | 12448 | } |
f15749da | 12449 | --processing_template_decl; |
eaf0f63a | 12450 | } |
12451 | } | |
9788fee1 | 12452 | else if (TREE_CODE (arg) != OVERLOAD |
12453 | && TREE_CODE (arg) != FUNCTION_DECL) | |
12454 | /* If ARG is, for example, "(0, &f)" then its type will be unknown | |
12455 | -- but the deduction does not succeed because the expression is | |
12456 | not just the function on its own. */ | |
12457 | return false; | |
2e3e31d2 | 12458 | else |
9788fee1 | 12459 | for (; arg; arg = OVL_NEXT (arg)) |
12460 | good += try_one_overload (tparms, targs, tempargs, parm, | |
12461 | TREE_TYPE (OVL_CURRENT (arg)), | |
12462 | strict, sub_strict, addr_p); | |
eaf0f63a | 12463 | |
12464 | /* [temp.deduct.type] A template-argument can be deduced from a pointer | |
12465 | to function or pointer to member function argument if the set of | |
12466 | overloaded functions does not contain function templates and at most | |
12467 | one of a set of overloaded functions provides a unique match. | |
12468 | ||
12469 | So if we found multiple possibilities, we return success but don't | |
12470 | deduce anything. */ | |
12471 | ||
12472 | if (good == 1) | |
12473 | { | |
12474 | int i = TREE_VEC_LENGTH (targs); | |
12475 | for (; i--; ) | |
12476 | if (TREE_VEC_ELT (tempargs, i)) | |
12477 | TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (tempargs, i); | |
12478 | } | |
12479 | if (good) | |
9788fee1 | 12480 | return true; |
eaf0f63a | 12481 | |
9788fee1 | 12482 | return false; |
eaf0f63a | 12483 | } |
12484 | ||
12485 | /* Subroutine of resolve_overloaded_unification; does deduction for a single | |
12486 | overload. Fills TARGS with any deduced arguments, or error_mark_node if | |
12487 | different overloads deduce different arguments for a given parm. | |
b44599c0 | 12488 | ADDR_P is true if the expression for which deduction is being |
12489 | performed was of the form "& fn" rather than simply "fn". | |
12490 | ||
eaf0f63a | 12491 | Returns 1 on success. */ |
12492 | ||
12493 | static int | |
807be5b4 | 12494 | try_one_overload (tree tparms, |
653e5405 | 12495 | tree orig_targs, |
12496 | tree targs, | |
12497 | tree parm, | |
12498 | tree arg, | |
12499 | unification_kind_t strict, | |
b44599c0 | 12500 | int sub_strict, |
12501 | bool addr_p) | |
eaf0f63a | 12502 | { |
12503 | int nargs; | |
12504 | tree tempargs; | |
12505 | int i; | |
12506 | ||
12507 | /* [temp.deduct.type] A template-argument can be deduced from a pointer | |
12508 | to function or pointer to member function argument if the set of | |
12509 | overloaded functions does not contain function templates and at most | |
12510 | one of a set of overloaded functions provides a unique match. | |
12511 | ||
12512 | So if this is a template, just return success. */ | |
12513 | ||
12514 | if (uses_template_parms (arg)) | |
12515 | return 1; | |
12516 | ||
b44599c0 | 12517 | if (TREE_CODE (arg) == METHOD_TYPE) |
12518 | arg = build_ptrmemfunc_type (build_pointer_type (arg)); | |
12519 | else if (addr_p) | |
12520 | arg = build_pointer_type (arg); | |
12521 | ||
63949b38 | 12522 | sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL); |
eaf0f63a | 12523 | |
12524 | /* We don't copy orig_targs for this because if we have already deduced | |
12525 | some template args from previous args, unify would complain when we | |
12526 | try to deduce a template parameter for the same argument, even though | |
12527 | there isn't really a conflict. */ | |
12528 | nargs = TREE_VEC_LENGTH (targs); | |
19c3b3a6 | 12529 | tempargs = make_tree_vec (nargs); |
eaf0f63a | 12530 | |
6d158f4d | 12531 | if (unify (tparms, tempargs, parm, arg, sub_strict) != 0) |
eaf0f63a | 12532 | return 0; |
12533 | ||
12534 | /* First make sure we didn't deduce anything that conflicts with | |
07f4da33 | 12535 | explicitly specified args. */ |
eaf0f63a | 12536 | for (i = nargs; i--; ) |
12537 | { | |
12538 | tree elt = TREE_VEC_ELT (tempargs, i); | |
07f4da33 | 12539 | tree oldelt = TREE_VEC_ELT (orig_targs, i); |
eaf0f63a | 12540 | |
022b46b4 | 12541 | if (!elt) |
12542 | /*NOP*/; | |
eaf0f63a | 12543 | else if (uses_template_parms (elt)) |
022b46b4 | 12544 | /* Since we're unifying against ourselves, we will fill in |
12545 | template args used in the function parm list with our own | |
12546 | template parms. Discard them. */ | |
12547 | TREE_VEC_ELT (tempargs, i) = NULL_TREE; | |
12548 | else if (oldelt && !template_args_equal (oldelt, elt)) | |
eaf0f63a | 12549 | return 0; |
12550 | } | |
12551 | ||
12552 | for (i = nargs; i--; ) | |
12553 | { | |
12554 | tree elt = TREE_VEC_ELT (tempargs, i); | |
12555 | ||
12556 | if (elt) | |
12557 | TREE_VEC_ELT (targs, i) = elt; | |
12558 | } | |
12559 | ||
12560 | return 1; | |
12561 | } | |
12562 | ||
6d158f4d | 12563 | /* PARM is a template class (perhaps with unbound template |
12564 | parameters). ARG is a fully instantiated type. If ARG can be | |
12565 | bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and | |
12566 | TARGS are as for unify. */ | |
34a99cba | 12567 | |
12568 | static tree | |
807be5b4 | 12569 | try_class_unification (tree tparms, tree targs, tree parm, tree arg) |
6d158f4d | 12570 | { |
6d158f4d | 12571 | tree copy_of_targs; |
12572 | ||
12573 | if (!CLASSTYPE_TEMPLATE_INFO (arg) | |
9031d10b | 12574 | || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg)) |
0dcef890 | 12575 | != most_general_template (CLASSTYPE_TI_TEMPLATE (parm)))) |
6d158f4d | 12576 | return NULL_TREE; |
12577 | ||
12578 | /* We need to make a new template argument vector for the call to | |
12579 | unify. If we used TARGS, we'd clutter it up with the result of | |
12580 | the attempted unification, even if this class didn't work out. | |
12581 | We also don't want to commit ourselves to all the unifications | |
12582 | we've already done, since unification is supposed to be done on | |
12583 | an argument-by-argument basis. In other words, consider the | |
12584 | following pathological case: | |
12585 | ||
12586 | template <int I, int J, int K> | |
12587 | struct S {}; | |
9031d10b | 12588 | |
6d158f4d | 12589 | template <int I, int J> |
12590 | struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {}; | |
9031d10b | 12591 | |
6d158f4d | 12592 | template <int I, int J, int K> |
12593 | void f(S<I, J, K>, S<I, I, I>); | |
9031d10b | 12594 | |
6d158f4d | 12595 | void g() { |
653e5405 | 12596 | S<0, 0, 0> s0; |
12597 | S<0, 1, 2> s2; | |
9031d10b | 12598 | |
653e5405 | 12599 | f(s0, s2); |
6d158f4d | 12600 | } |
12601 | ||
12602 | Now, by the time we consider the unification involving `s2', we | |
12603 | already know that we must have `f<0, 0, 0>'. But, even though | |
6c0cc2cd | 12604 | `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid |
6d158f4d | 12605 | because there are two ways to unify base classes of S<0, 1, 2> |
12606 | with S<I, I, I>. If we kept the already deduced knowledge, we | |
12607 | would reject the possibility I=1. */ | |
19c3b3a6 | 12608 | copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs)); |
9031d10b | 12609 | |
6d158f4d | 12610 | /* If unification failed, we're done. */ |
25963226 | 12611 | if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm), |
12612 | CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE)) | |
6d158f4d | 12613 | return NULL_TREE; |
25963226 | 12614 | |
12615 | return arg; | |
6d158f4d | 12616 | } |
12617 | ||
98dc77cd | 12618 | /* Given a template type PARM and a class type ARG, find the unique |
12619 | base type in ARG that is an instance of PARM. We do not examine | |
12620 | ARG itself; only its base-classes. If there is not exactly one | |
12621 | appropriate base class, return NULL_TREE. PARM may be the type of | |
12622 | a partial specialization, as well as a plain template type. Used | |
12623 | by unify. */ | |
6d158f4d | 12624 | |
12625 | static tree | |
98dc77cd | 12626 | get_template_base (tree tparms, tree targs, tree parm, tree arg) |
34a99cba | 12627 | { |
98dc77cd | 12628 | tree rval = NULL_TREE; |
12629 | tree binfo; | |
12630 | ||
95397ff9 | 12631 | gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg))); |
9031d10b | 12632 | |
98dc77cd | 12633 | binfo = TYPE_BINFO (complete_type (arg)); |
12634 | if (!binfo) | |
12635 | /* The type could not be completed. */ | |
12636 | return NULL_TREE; | |
34a99cba | 12637 | |
98dc77cd | 12638 | /* Walk in inheritance graph order. The search order is not |
12639 | important, and this avoids multiple walks of virtual bases. */ | |
12640 | for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo)) | |
34a99cba | 12641 | { |
98dc77cd | 12642 | tree r = try_class_unification (tparms, targs, parm, BINFO_TYPE (binfo)); |
34a99cba | 12643 | |
bce43c50 | 12644 | if (r) |
12645 | { | |
12646 | /* If there is more than one satisfactory baseclass, then: | |
34a99cba | 12647 | |
bce43c50 | 12648 | [temp.deduct.call] |
34a99cba | 12649 | |
bce43c50 | 12650 | If they yield more than one possible deduced A, the type |
12651 | deduction fails. | |
6d158f4d | 12652 | |
bce43c50 | 12653 | applies. */ |
98dc77cd | 12654 | if (rval && !same_type_p (r, rval)) |
12655 | return NULL_TREE; | |
9031d10b | 12656 | |
98dc77cd | 12657 | rval = r; |
bce43c50 | 12658 | } |
34a99cba | 12659 | } |
12660 | ||
98dc77cd | 12661 | return rval; |
34a99cba | 12662 | } |
12663 | ||
f4797a74 | 12664 | /* Returns the level of DECL, which declares a template parameter. */ |
12665 | ||
863c3f96 | 12666 | static int |
807be5b4 | 12667 | template_decl_level (tree decl) |
f4797a74 | 12668 | { |
12669 | switch (TREE_CODE (decl)) | |
12670 | { | |
12671 | case TYPE_DECL: | |
12672 | case TEMPLATE_DECL: | |
12673 | return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl)); | |
12674 | ||
12675 | case PARM_DECL: | |
12676 | return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl)); | |
12677 | ||
12678 | default: | |
2e3e31d2 | 12679 | gcc_unreachable (); |
f4797a74 | 12680 | } |
2e3e31d2 | 12681 | return 0; |
f4797a74 | 12682 | } |
12683 | ||
32b5f03e | 12684 | /* Decide whether ARG can be unified with PARM, considering only the |
12685 | cv-qualifiers of each type, given STRICT as documented for unify. | |
7677ebb9 | 12686 | Returns nonzero iff the unification is OK on that basis. */ |
96624a9e | 12687 | |
863c3f96 | 12688 | static int |
807be5b4 | 12689 | check_cv_quals_for_unify (int strict, tree arg, tree parm) |
32b5f03e | 12690 | { |
9edbac04 | 12691 | int arg_quals = cp_type_quals (arg); |
12692 | int parm_quals = cp_type_quals (parm); | |
12693 | ||
f8e3bda1 | 12694 | if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM |
12695 | && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL)) | |
9edbac04 | 12696 | { |
57561691 | 12697 | /* Although a CVR qualifier is ignored when being applied to a |
653e5405 | 12698 | substituted template parameter ([8.3.2]/1 for example), that |
12699 | does not apply during deduction [14.8.2.4]/1, (even though | |
12700 | that is not explicitly mentioned, [14.8.2.4]/9 indicates | |
12701 | this). Except when we're allowing additional CV qualifiers | |
12702 | at the outer level [14.8.2.1]/3,1st bullet. */ | |
57561691 | 12703 | if ((TREE_CODE (arg) == REFERENCE_TYPE |
12704 | || TREE_CODE (arg) == FUNCTION_TYPE | |
12705 | || TREE_CODE (arg) == METHOD_TYPE) | |
12706 | && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE))) | |
12707 | return 0; | |
12708 | ||
12709 | if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM) | |
12710 | && (parm_quals & TYPE_QUAL_RESTRICT)) | |
12711 | return 0; | |
9edbac04 | 12712 | } |
57561691 | 12713 | |
efa31275 | 12714 | if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL)) |
9edbac04 | 12715 | && (arg_quals & parm_quals) != parm_quals) |
12bd6b61 | 12716 | return 0; |
12717 | ||
efa31275 | 12718 | if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL)) |
9edbac04 | 12719 | && (parm_quals & arg_quals) != arg_quals) |
12bd6b61 | 12720 | return 0; |
12721 | ||
12bd6b61 | 12722 | return 1; |
32b5f03e | 12723 | } |
12724 | ||
d95d815d | 12725 | /* Determines the LEVEL and INDEX for the template parameter PARM. */ |
12726 | void | |
12727 | template_parm_level_and_index (tree parm, int* level, int* index) | |
12728 | { | |
12729 | if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM | |
12730 | || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM | |
12731 | || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM) | |
12732 | { | |
12733 | *index = TEMPLATE_TYPE_IDX (parm); | |
12734 | *level = TEMPLATE_TYPE_LEVEL (parm); | |
12735 | } | |
12736 | else | |
12737 | { | |
12738 | *index = TEMPLATE_PARM_IDX (parm); | |
12739 | *level = TEMPLATE_PARM_LEVEL (parm); | |
12740 | } | |
12741 | } | |
12742 | ||
12743 | /* Unifies the remaining arguments in PACKED_ARGS with the pack | |
12744 | expansion at the end of PACKED_PARMS. Returns 0 if the type | |
12745 | deduction succeeds, 1 otherwise. STRICT is the same as in | |
12746 | unify. CALL_ARGS_P is true iff PACKED_ARGS is actually a function | |
12747 | call argument list. We'll need to adjust the arguments to make them | |
12748 | types. SUBR tells us if this is from a recursive call to | |
12749 | type_unification_real. */ | |
12750 | int | |
12751 | unify_pack_expansion (tree tparms, tree targs, tree packed_parms, | |
12752 | tree packed_args, int strict, bool call_args_p, | |
12753 | bool subr) | |
12754 | { | |
12755 | tree parm | |
12756 | = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1); | |
12757 | tree pattern = PACK_EXPANSION_PATTERN (parm); | |
12758 | tree pack, packs = NULL_TREE; | |
12759 | int i, start = TREE_VEC_LENGTH (packed_parms) - 1; | |
12760 | int len = TREE_VEC_LENGTH (packed_args); | |
12761 | ||
12762 | /* Determine the parameter packs we will be deducing from the | |
12763 | pattern, and record their current deductions. */ | |
12764 | for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm); | |
12765 | pack; pack = TREE_CHAIN (pack)) | |
12766 | { | |
12767 | tree parm_pack = TREE_VALUE (pack); | |
12768 | int idx, level; | |
12769 | ||
12770 | /* Determine the index and level of this parameter pack. */ | |
12771 | template_parm_level_and_index (parm_pack, &level, &idx); | |
12772 | ||
12773 | /* Keep track of the parameter packs and their corresponding | |
12774 | argument packs. */ | |
12775 | packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs); | |
12776 | TREE_TYPE (packs) = make_tree_vec (len - start); | |
12777 | } | |
12778 | ||
12779 | /* Loop through all of the arguments that have not yet been | |
12780 | unified and unify each with the pattern. */ | |
12781 | for (i = start; i < len; i++) | |
12782 | { | |
12783 | tree parm = pattern; | |
12784 | ||
12785 | /* For each parameter pack, clear out the deduced value so that | |
12786 | we can deduce it again. */ | |
12787 | for (pack = packs; pack; pack = TREE_CHAIN (pack)) | |
12788 | { | |
12789 | int idx, level; | |
12790 | template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx); | |
12791 | ||
12792 | TMPL_ARG (targs, level, idx) = NULL_TREE; | |
12793 | } | |
12794 | ||
12795 | /* Unify the pattern with the current argument. */ | |
12796 | { | |
12797 | tree arg = TREE_VEC_ELT (packed_args, i); | |
0fba986b | 12798 | tree arg_expr = NULL_TREE; |
d95d815d | 12799 | int arg_strict = strict; |
12800 | bool skip_arg_p = false; | |
12801 | ||
12802 | if (call_args_p) | |
12803 | { | |
12804 | int sub_strict; | |
12805 | ||
12806 | /* This mirrors what we do in type_unification_real. */ | |
12807 | switch (strict) | |
12808 | { | |
12809 | case DEDUCE_CALL: | |
12810 | sub_strict = (UNIFY_ALLOW_OUTER_LEVEL | |
12811 | | UNIFY_ALLOW_MORE_CV_QUAL | |
12812 | | UNIFY_ALLOW_DERIVED); | |
12813 | break; | |
12814 | ||
12815 | case DEDUCE_CONV: | |
12816 | sub_strict = UNIFY_ALLOW_LESS_CV_QUAL; | |
12817 | break; | |
12818 | ||
12819 | case DEDUCE_EXACT: | |
12820 | sub_strict = UNIFY_ALLOW_NONE; | |
12821 | break; | |
12822 | ||
12823 | default: | |
12824 | gcc_unreachable (); | |
12825 | } | |
12826 | ||
12827 | if (!TYPE_P (arg)) | |
12828 | { | |
12829 | gcc_assert (TREE_TYPE (arg) != NULL_TREE); | |
12830 | if (type_unknown_p (arg)) | |
12831 | { | |
12832 | /* [temp.deduct.type] A template-argument can be | |
12833 | deduced from a pointer to function or pointer | |
12834 | to member function argument if the set of | |
12835 | overloaded functions does not contain function | |
12836 | templates and at most one of a set of | |
12837 | overloaded functions provides a unique | |
12838 | match. */ | |
12839 | ||
12840 | if (resolve_overloaded_unification | |
12841 | (tparms, targs, parm, arg, strict, sub_strict) | |
12842 | != 0) | |
12843 | return 1; | |
12844 | skip_arg_p = true; | |
12845 | } | |
12846 | ||
12847 | if (!skip_arg_p) | |
12848 | { | |
0fba986b | 12849 | arg_expr = arg; |
12850 | arg = unlowered_expr_type (arg); | |
d95d815d | 12851 | if (arg == error_mark_node) |
12852 | return 1; | |
12853 | } | |
12854 | } | |
12855 | ||
12856 | arg_strict = sub_strict; | |
12857 | ||
12858 | if (!subr) | |
12859 | arg_strict |= | |
0fba986b | 12860 | maybe_adjust_types_for_deduction (strict, &parm, &arg, |
12861 | arg_expr); | |
d95d815d | 12862 | } |
12863 | ||
12864 | if (!skip_arg_p) | |
12865 | { | |
12866 | if (unify (tparms, targs, parm, arg, arg_strict)) | |
12867 | return 1; | |
12868 | } | |
12869 | } | |
12870 | ||
12871 | /* For each parameter pack, collect the deduced value. */ | |
12872 | for (pack = packs; pack; pack = TREE_CHAIN (pack)) | |
12873 | { | |
12874 | int idx, level; | |
12875 | template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx); | |
12876 | ||
12877 | TREE_VEC_ELT (TREE_TYPE (pack), i - start) = | |
12878 | TMPL_ARG (targs, level, idx); | |
12879 | } | |
12880 | } | |
12881 | ||
12882 | /* Verify that the results of unification with the parameter packs | |
12883 | produce results consistent with what we've seen before, and make | |
12884 | the deduced argument packs available. */ | |
12885 | for (pack = packs; pack; pack = TREE_CHAIN (pack)) | |
12886 | { | |
12887 | tree old_pack = TREE_VALUE (pack); | |
12888 | tree new_args = TREE_TYPE (pack); | |
d1cc437c | 12889 | int i, len = TREE_VEC_LENGTH (new_args); |
12890 | bool nondeduced_p = false; | |
12891 | ||
12892 | /* If NEW_ARGS contains any NULL_TREE entries, we didn't | |
12893 | actually deduce anything. */ | |
12894 | for (i = 0; i < len && !nondeduced_p; ++i) | |
12895 | if (TREE_VEC_ELT (new_args, i) == NULL_TREE) | |
12896 | nondeduced_p = true; | |
12897 | if (nondeduced_p) | |
12898 | continue; | |
d95d815d | 12899 | |
12900 | if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack)) | |
12901 | { | |
12902 | /* Prepend the explicit arguments onto NEW_ARGS. */ | |
12903 | tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack); | |
12904 | tree old_args = new_args; | |
12905 | int i, explicit_len = TREE_VEC_LENGTH (explicit_args); | |
12906 | int len = explicit_len + TREE_VEC_LENGTH (old_args); | |
12907 | ||
12908 | /* Copy the explicit arguments. */ | |
12909 | new_args = make_tree_vec (len); | |
12910 | for (i = 0; i < explicit_len; i++) | |
12911 | TREE_VEC_ELT (new_args, i) = TREE_VEC_ELT (explicit_args, i); | |
12912 | ||
12913 | /* Copy the deduced arguments. */ | |
12914 | for (; i < len; i++) | |
12915 | TREE_VEC_ELT (new_args, i) = | |
12916 | TREE_VEC_ELT (old_args, i - explicit_len); | |
12917 | } | |
12918 | ||
12919 | if (!old_pack) | |
12920 | { | |
12921 | tree result; | |
12922 | int idx, level; | |
12923 | ||
12924 | template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx); | |
12925 | ||
12926 | /* Build the deduced *_ARGUMENT_PACK. */ | |
12927 | if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX) | |
12928 | { | |
12929 | result = make_node (NONTYPE_ARGUMENT_PACK); | |
12930 | TREE_TYPE (result) = | |
12931 | TREE_TYPE (TEMPLATE_PARM_DECL (TREE_PURPOSE (pack))); | |
12932 | TREE_CONSTANT (result) = 1; | |
12933 | } | |
12934 | else | |
12935 | result = make_node (TYPE_ARGUMENT_PACK); | |
12936 | ||
12937 | SET_ARGUMENT_PACK_ARGS (result, new_args); | |
12938 | ||
12939 | /* Note the deduced argument packs for this parameter | |
12940 | pack. */ | |
12941 | TMPL_ARG (targs, level, idx) = result; | |
12942 | } | |
12943 | else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack) | |
12944 | && (ARGUMENT_PACK_ARGS (old_pack) | |
12945 | == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack))) | |
12946 | { | |
12947 | /* We only had the explicitly-provided arguments before, but | |
12948 | now we have a complete set of arguments. */ | |
12949 | int idx, level; | |
12950 | tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack); | |
12951 | template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx); | |
12952 | ||
12953 | /* Keep the original deduced argument pack. */ | |
12954 | TMPL_ARG (targs, level, idx) = old_pack; | |
12955 | ||
12956 | SET_ARGUMENT_PACK_ARGS (old_pack, new_args); | |
12957 | ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1; | |
12958 | ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args; | |
12959 | } | |
12960 | else if (!comp_template_args (ARGUMENT_PACK_ARGS (old_pack), | |
12961 | new_args)) | |
12962 | /* Inconsistent unification of this parameter pack. */ | |
12963 | return 1; | |
12964 | else | |
12965 | { | |
12966 | int idx, level; | |
12967 | ||
12968 | template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx); | |
12969 | ||
12970 | /* Keep the original deduced argument pack. */ | |
12971 | TMPL_ARG (targs, level, idx) = old_pack; | |
12972 | } | |
12973 | } | |
12974 | ||
12975 | return 0; | |
12976 | } | |
12977 | ||
aa32d258 | 12978 | /* Deduce the value of template parameters. TPARMS is the (innermost) |
12979 | set of template parameters to a template. TARGS is the bindings | |
12980 | for those template parameters, as determined thus far; TARGS may | |
12981 | include template arguments for outer levels of template parameters | |
12982 | as well. PARM is a parameter to a template function, or a | |
12983 | subcomponent of that parameter; ARG is the corresponding argument. | |
12984 | This function attempts to match PARM with ARG in a manner | |
12985 | consistent with the existing assignments in TARGS. If more values | |
12986 | are deduced, then TARGS is updated. | |
12987 | ||
12988 | Returns 0 if the type deduction succeeds, 1 otherwise. The | |
12989 | parameter STRICT is a bitwise or of the following flags: | |
32b5f03e | 12990 | |
12991 | UNIFY_ALLOW_NONE: | |
12992 | Require an exact match between PARM and ARG. | |
12993 | UNIFY_ALLOW_MORE_CV_QUAL: | |
7e0d59b2 | 12994 | Allow the deduced ARG to be more cv-qualified (by qualification |
12995 | conversion) than ARG. | |
32b5f03e | 12996 | UNIFY_ALLOW_LESS_CV_QUAL: |
12997 | Allow the deduced ARG to be less cv-qualified than ARG. | |
12998 | UNIFY_ALLOW_DERIVED: | |
12999 | Allow the deduced ARG to be a template base class of ARG, | |
13000 | or a pointer to a template base class of the type pointed to by | |
acf0e4b3 | 13001 | ARG. |
13002 | UNIFY_ALLOW_INTEGER: | |
13003 | Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX | |
9031d10b | 13004 | case for more information. |
7e0d59b2 | 13005 | UNIFY_ALLOW_OUTER_LEVEL: |
13006 | This is the outermost level of a deduction. Used to determine validity | |
13007 | of qualification conversions. A valid qualification conversion must | |
13008 | have const qualified pointers leading up to the inner type which | |
13009 | requires additional CV quals, except at the outer level, where const | |
13010 | is not required [conv.qual]. It would be normal to set this flag in | |
efa31275 | 13011 | addition to setting UNIFY_ALLOW_MORE_CV_QUAL. |
13012 | UNIFY_ALLOW_OUTER_MORE_CV_QUAL: | |
13013 | This is the outermost level of a deduction, and PARM can be more CV | |
13014 | qualified at this point. | |
13015 | UNIFY_ALLOW_OUTER_LESS_CV_QUAL: | |
13016 | This is the outermost level of a deduction, and PARM can be less CV | |
cb47f9e7 | 13017 | qualified at this point. */ |
32b5f03e | 13018 | |
863c3f96 | 13019 | static int |
807be5b4 | 13020 | unify (tree tparms, tree targs, tree parm, tree arg, int strict) |
471086d6 | 13021 | { |
13022 | int idx; | |
c906a2a7 | 13023 | tree targ; |
f4797a74 | 13024 | tree tparm; |
7e0d59b2 | 13025 | int strict_in = strict; |
471086d6 | 13026 | |
13027 | /* I don't think this will do the right thing with respect to types. | |
13028 | But the only case I've seen it in so far has been array bounds, where | |
13029 | signedness is the only information lost, and I think that will be | |
13030 | okay. */ | |
13031 | while (TREE_CODE (parm) == NOP_EXPR) | |
13032 | parm = TREE_OPERAND (parm, 0); | |
13033 | ||
13034 | if (arg == error_mark_node) | |
13035 | return 1; | |
13036 | if (arg == unknown_type_node) | |
eaf0f63a | 13037 | /* We can't deduce anything from this, but we might get all the |
13038 | template args from other function args. */ | |
13039 | return 0; | |
13040 | ||
f4797a74 | 13041 | /* If PARM uses template parameters, then we can't bail out here, |
ae883b79 | 13042 | even if ARG == PARM, since we won't record unifications for the |
f4797a74 | 13043 | template parameters. We might need them if we're trying to |
13044 | figure out which of two things is more specialized. */ | |
13045 | if (arg == parm && !uses_template_parms (parm)) | |
471086d6 | 13046 | return 0; |
13047 | ||
32b5f03e | 13048 | /* Immediately reject some pairs that won't unify because of |
13049 | cv-qualification mismatches. */ | |
13050 | if (TREE_CODE (arg) == TREE_CODE (parm) | |
9308e976 | 13051 | && TYPE_P (arg) |
57d3a318 | 13052 | /* It is the elements of the array which hold the cv quals of an array |
653e5405 | 13053 | type, and the elements might be template type parms. We'll check |
13054 | when we recurse. */ | |
57d3a318 | 13055 | && TREE_CODE (arg) != ARRAY_TYPE |
32b5f03e | 13056 | /* We check the cv-qualifiers when unifying with template type |
13057 | parameters below. We want to allow ARG `const T' to unify with | |
13058 | PARM `T' for example, when computing which of two templates | |
13059 | is more specialized, for example. */ | |
13060 | && TREE_CODE (arg) != TEMPLATE_TYPE_PARM | |
7e0d59b2 | 13061 | && !check_cv_quals_for_unify (strict_in, arg, parm)) |
092a2ddf | 13062 | return 1; |
13063 | ||
7e0d59b2 | 13064 | if (!(strict & UNIFY_ALLOW_OUTER_LEVEL) |
8ef93a49 | 13065 | && TYPE_P (parm) && !CP_TYPE_CONST_P (parm)) |
7e0d59b2 | 13066 | strict &= ~UNIFY_ALLOW_MORE_CV_QUAL; |
13067 | strict &= ~UNIFY_ALLOW_OUTER_LEVEL; | |
13068 | strict &= ~UNIFY_ALLOW_DERIVED; | |
efa31275 | 13069 | strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL; |
13070 | strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL; | |
9031d10b | 13071 | |
471086d6 | 13072 | switch (TREE_CODE (parm)) |
13073 | { | |
fe2a3089 | 13074 | case TYPENAME_TYPE: |
d2b6885f | 13075 | case SCOPE_REF: |
47d727d4 | 13076 | case UNBOUND_CLASS_TEMPLATE: |
fe2a3089 | 13077 | /* In a type which contains a nested-name-specifier, template |
13078 | argument values cannot be deduced for template parameters used | |
13079 | within the nested-name-specifier. */ | |
13080 | return 0; | |
13081 | ||
471086d6 | 13082 | case TEMPLATE_TYPE_PARM: |
cd5dd2d8 | 13083 | case TEMPLATE_TEMPLATE_PARM: |
1e93ca27 | 13084 | case BOUND_TEMPLATE_TEMPLATE_PARM: |
f4797a74 | 13085 | tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0)); |
bb6a649f | 13086 | if (tparm == error_mark_node) |
13087 | return 1; | |
f4797a74 | 13088 | |
13089 | if (TEMPLATE_TYPE_LEVEL (parm) | |
13090 | != template_decl_level (tparm)) | |
13091 | /* The PARM is not one we're trying to unify. Just check | |
13092 | to see if it matches ARG. */ | |
13093 | return (TREE_CODE (arg) == TREE_CODE (parm) | |
daf9ff67 | 13094 | && same_type_p (parm, arg)) ? 0 : 1; |
cd5dd2d8 | 13095 | idx = TEMPLATE_TYPE_IDX (parm); |
aa32d258 | 13096 | targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx); |
f4797a74 | 13097 | tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx)); |
c906a2a7 | 13098 | |
cd5dd2d8 | 13099 | /* Check for mixed types and values. */ |
f4797a74 | 13100 | if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM |
13101 | && TREE_CODE (tparm) != TYPE_DECL) | |
9031d10b | 13102 | || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM |
f4797a74 | 13103 | && TREE_CODE (tparm) != TEMPLATE_DECL)) |
cd5dd2d8 | 13104 | return 1; |
13105 | ||
1e93ca27 | 13106 | if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM) |
cd5dd2d8 | 13107 | { |
05d303f3 | 13108 | /* ARG must be constructed from a template class or a template |
13109 | template parameter. */ | |
13110 | if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM | |
a0ae7f4b | 13111 | && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg)) |
1e93ca27 | 13112 | return 1; |
cd5dd2d8 | 13113 | |
1e93ca27 | 13114 | { |
1e93ca27 | 13115 | tree parmvec = TYPE_TI_ARGS (parm); |
7c0f0641 | 13116 | tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg)); |
2e1a9c49 | 13117 | tree parm_parms |
83b01f73 | 13118 | = DECL_INNERMOST_TEMPLATE_PARMS |
2e1a9c49 | 13119 | (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (parm)); |
e5dab245 | 13120 | int i, len; |
13121 | int parm_variadic_p = 0; | |
cd5dd2d8 | 13122 | |
36653a30 | 13123 | /* The resolution to DR150 makes clear that default |
13124 | arguments for an N-argument may not be used to bind T | |
13125 | to a template template parameter with fewer than N | |
13126 | parameters. It is not safe to permit the binding of | |
13127 | default arguments as an extension, as that may change | |
13128 | the meaning of a conforming program. Consider: | |
13129 | ||
13130 | struct Dense { static const unsigned int dim = 1; }; | |
13131 | ||
13132 | template <template <typename> class View, | |
13133 | typename Block> | |
13134 | void operator+(float, View<Block> const&); | |
13135 | ||
074ab442 | 13136 | template <typename Block, |
13137 | unsigned int Dim = Block::dim> | |
36653a30 | 13138 | struct Lvalue_proxy { operator float() const; }; |
13139 | ||
13140 | void | |
13141 | test_1d (void) { | |
13142 | Lvalue_proxy<Dense> p; | |
13143 | float b; | |
13144 | b + p; | |
13145 | } | |
1e93ca27 | 13146 | |
36653a30 | 13147 | Here, if Lvalue_proxy is permitted to bind to View, then |
13148 | the global operator+ will be used; if they are not, the | |
074ab442 | 13149 | Lvalue_proxy will be converted to float. */ |
2e1a9c49 | 13150 | if (coerce_template_parms (parm_parms, |
13151 | argvec, | |
36653a30 | 13152 | TYPE_TI_TEMPLATE (parm), |
13153 | tf_none, | |
13154 | /*require_all_args=*/true, | |
13155 | /*use_default_args=*/false) | |
653e5405 | 13156 | == error_mark_node) |
1e93ca27 | 13157 | return 1; |
9031d10b | 13158 | |
13159 | /* Deduce arguments T, i from TT<T> or TT<i>. | |
1e93ca27 | 13160 | We check each element of PARMVEC and ARGVEC individually |
13161 | rather than the whole TREE_VEC since they can have | |
13162 | different number of elements. */ | |
49df3f58 | 13163 | |
e5dab245 | 13164 | parmvec = expand_template_argument_pack (parmvec); |
13165 | argvec = expand_template_argument_pack (argvec); | |
13166 | ||
13167 | len = TREE_VEC_LENGTH (parmvec); | |
13168 | ||
13169 | /* Check if the parameters end in a pack, making them | |
13170 | variadic. */ | |
13171 | if (len > 0 | |
13172 | && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1))) | |
13173 | parm_variadic_p = 1; | |
13174 | ||
13175 | if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p) | |
13176 | return 1; | |
13177 | ||
13178 | for (i = 0; i < len - parm_variadic_p; ++i) | |
1e93ca27 | 13179 | { |
653e5405 | 13180 | if (unify (tparms, targs, |
9031d10b | 13181 | TREE_VEC_ELT (parmvec, i), |
13182 | TREE_VEC_ELT (argvec, i), | |
1e93ca27 | 13183 | UNIFY_ALLOW_NONE)) |
13184 | return 1; | |
cd5dd2d8 | 13185 | } |
e5dab245 | 13186 | |
13187 | if (parm_variadic_p | |
13188 | && unify_pack_expansion (tparms, targs, | |
13189 | parmvec, argvec, | |
13190 | UNIFY_ALLOW_NONE, | |
13191 | /*call_args_p=*/false, | |
13192 | /*subr=*/false)) | |
13193 | return 1; | |
1e93ca27 | 13194 | } |
05d303f3 | 13195 | arg = TYPE_TI_TEMPLATE (arg); |
1e93ca27 | 13196 | |
13197 | /* Fall through to deduce template name. */ | |
13198 | } | |
13199 | ||
13200 | if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM | |
13201 | || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM) | |
13202 | { | |
13203 | /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */ | |
13204 | ||
13205 | /* Simple cases: Value already set, does match or doesn't. */ | |
13206 | if (targ != NULL_TREE && template_args_equal (targ, arg)) | |
13207 | return 0; | |
13208 | else if (targ) | |
13209 | return 1; | |
f4797a74 | 13210 | } |
13211 | else | |
13212 | { | |
32b5f03e | 13213 | /* If PARM is `const T' and ARG is only `int', we don't have |
13214 | a match unless we are allowing additional qualification. | |
13215 | If ARG is `const int' and PARM is just `T' that's OK; | |
13216 | that binds `const int' to `T'. */ | |
9031d10b | 13217 | if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL, |
32b5f03e | 13218 | arg, parm)) |
f4797a74 | 13219 | return 1; |
13220 | ||
32b5f03e | 13221 | /* Consider the case where ARG is `const volatile int' and |
13222 | PARM is `const T'. Then, T should be `volatile int'. */ | |
4b471722 | 13223 | arg = cp_build_qualified_type_real |
13224 | (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none); | |
e9c0ac6a | 13225 | if (arg == error_mark_node) |
13226 | return 1; | |
cd5dd2d8 | 13227 | |
1e93ca27 | 13228 | /* Simple cases: Value already set, does match or doesn't. */ |
13229 | if (targ != NULL_TREE && same_type_p (targ, arg)) | |
13230 | return 0; | |
13231 | else if (targ) | |
13232 | return 1; | |
477f2174 | 13233 | |
e3479c27 | 13234 | /* Make sure that ARG is not a variable-sized array. (Note |
13235 | that were talking about variable-sized arrays (like | |
13236 | `int[n]'), rather than arrays of unknown size (like | |
13237 | `int[]').) We'll get very confused by such a type since | |
13238 | the bound of the array will not be computable in an | |
13239 | instantiation. Besides, such types are not allowed in | |
13240 | ISO C++, so we can do as we please here. */ | |
1f8a6ff8 | 13241 | if (variably_modified_type_p (arg, NULL_TREE)) |
e3479c27 | 13242 | return 1; |
13243 | } | |
477f2174 | 13244 | |
d95d815d | 13245 | /* If ARG is a parameter pack or an expansion, we cannot unify |
13246 | against it unless PARM is also a parameter pack. */ | |
13247 | if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg)) | |
13248 | && !template_parameter_pack_p (parm)) | |
13249 | return 1; | |
13250 | ||
aa32d258 | 13251 | TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg; |
cd5dd2d8 | 13252 | return 0; |
13253 | ||
f3110581 | 13254 | case TEMPLATE_PARM_INDEX: |
f4797a74 | 13255 | tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0)); |
475963b0 | 13256 | if (tparm == error_mark_node) |
13257 | return 1; | |
f4797a74 | 13258 | |
9031d10b | 13259 | if (TEMPLATE_PARM_LEVEL (parm) |
f4797a74 | 13260 | != template_decl_level (tparm)) |
13261 | /* The PARM is not one we're trying to unify. Just check | |
13262 | to see if it matches ARG. */ | |
00952d10 | 13263 | return !(TREE_CODE (arg) == TREE_CODE (parm) |
13264 | && cp_tree_equal (parm, arg)); | |
f4797a74 | 13265 | |
f3110581 | 13266 | idx = TEMPLATE_PARM_IDX (parm); |
aa32d258 | 13267 | targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx); |
f4797a74 | 13268 | |
c906a2a7 | 13269 | if (targ) |
00952d10 | 13270 | return !cp_tree_equal (targ, arg); |
471086d6 | 13271 | |
acf0e4b3 | 13272 | /* [temp.deduct.type] If, in the declaration of a function template |
13273 | with a non-type template-parameter, the non-type | |
13274 | template-parameter is used in an expression in the function | |
13275 | parameter-list and, if the corresponding template-argument is | |
13276 | deduced, the template-argument type shall match the type of the | |
13277 | template-parameter exactly, except that a template-argument | |
9031d10b | 13278 | deduced from an array bound may be of any integral type. |
acbb7c3a | 13279 | The non-type parameter might use already deduced type parameters. */ |
614cc70d | 13280 | tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE); |
66da0aec | 13281 | if (!TREE_TYPE (arg)) |
13282 | /* Template-parameter dependent expression. Just accept it for now. | |
13283 | It will later be processed in convert_template_argument. */ | |
13284 | ; | |
13285 | else if (same_type_p (TREE_TYPE (arg), tparm)) | |
13286 | /* OK */; | |
acf0e4b3 | 13287 | else if ((strict & UNIFY_ALLOW_INTEGER) |
614cc70d | 13288 | && (TREE_CODE (tparm) == INTEGER_TYPE |
13289 | || TREE_CODE (tparm) == BOOLEAN_TYPE)) | |
cb47f9e7 | 13290 | /* Convert the ARG to the type of PARM; the deduced non-type |
13291 | template argument must exactly match the types of the | |
13292 | corresponding parameter. */ | |
13293 | arg = fold (build_nop (TREE_TYPE (parm), arg)); | |
614cc70d | 13294 | else if (uses_template_parms (tparm)) |
13295 | /* We haven't deduced the type of this parameter yet. Try again | |
13296 | later. */ | |
13297 | return 0; | |
acf0e4b3 | 13298 | else |
13299 | return 1; | |
13300 | ||
d95d815d | 13301 | /* If ARG is a parameter pack or an expansion, we cannot unify |
13302 | against it unless PARM is also a parameter pack. */ | |
13303 | if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg)) | |
13304 | && !TEMPLATE_PARM_PARAMETER_PACK (parm)) | |
13305 | return 1; | |
13306 | ||
aa32d258 | 13307 | TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg; |
471086d6 | 13308 | return 0; |
13309 | ||
02ca53f2 | 13310 | case PTRMEM_CST: |
13311 | { | |
653e5405 | 13312 | /* A pointer-to-member constant can be unified only with |
13313 | another constant. */ | |
02ca53f2 | 13314 | if (TREE_CODE (arg) != PTRMEM_CST) |
653e5405 | 13315 | return 1; |
02ca53f2 | 13316 | |
13317 | /* Just unify the class member. It would be useless (and possibly | |
653e5405 | 13318 | wrong, depending on the strict flags) to unify also |
13319 | PTRMEM_CST_CLASS, because we want to be sure that both parm and | |
13320 | arg refer to the same variable, even if through different | |
13321 | classes. For instance: | |
02ca53f2 | 13322 | |
653e5405 | 13323 | struct A { int x; }; |
13324 | struct B : A { }; | |
02ca53f2 | 13325 | |
653e5405 | 13326 | Unification of &A::x and &B::x must succeed. */ |
02ca53f2 | 13327 | return unify (tparms, targs, PTRMEM_CST_MEMBER (parm), |
653e5405 | 13328 | PTRMEM_CST_MEMBER (arg), strict); |
02ca53f2 | 13329 | } |
13330 | ||
471086d6 | 13331 | case POINTER_TYPE: |
32b5f03e | 13332 | { |
32b5f03e | 13333 | if (TREE_CODE (arg) != POINTER_TYPE) |
13334 | return 1; | |
9031d10b | 13335 | |
32b5f03e | 13336 | /* [temp.deduct.call] |
13337 | ||
13338 | A can be another pointer or pointer to member type that can | |
13339 | be converted to the deduced A via a qualification | |
13340 | conversion (_conv.qual_). | |
13341 | ||
13342 | We pass down STRICT here rather than UNIFY_ALLOW_NONE. | |
13343 | This will allow for additional cv-qualification of the | |
7e0d59b2 | 13344 | pointed-to types if appropriate. */ |
9031d10b | 13345 | |
7e0d59b2 | 13346 | if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE) |
32b5f03e | 13347 | /* The derived-to-base conversion only persists through one |
13348 | level of pointers. */ | |
7e0d59b2 | 13349 | strict |= (strict_in & UNIFY_ALLOW_DERIVED); |
32b5f03e | 13350 | |
9031d10b | 13351 | return unify (tparms, targs, TREE_TYPE (parm), |
7e0d59b2 | 13352 | TREE_TYPE (arg), strict); |
32b5f03e | 13353 | } |
471086d6 | 13354 | |
13355 | case REFERENCE_TYPE: | |
32b5f03e | 13356 | if (TREE_CODE (arg) != REFERENCE_TYPE) |
13357 | return 1; | |
13358 | return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg), | |
7e0d59b2 | 13359 | strict & UNIFY_ALLOW_MORE_CV_QUAL); |
471086d6 | 13360 | |
13361 | case ARRAY_TYPE: | |
13362 | if (TREE_CODE (arg) != ARRAY_TYPE) | |
13363 | return 1; | |
8cecf372 | 13364 | if ((TYPE_DOMAIN (parm) == NULL_TREE) |
13365 | != (TYPE_DOMAIN (arg) == NULL_TREE)) | |
13366 | return 1; | |
cb47f9e7 | 13367 | if (TYPE_DOMAIN (parm) != NULL_TREE) |
13368 | { | |
13369 | tree parm_max; | |
13370 | tree arg_max; | |
657e2cb1 | 13371 | bool parm_cst; |
13372 | bool arg_cst; | |
cb47f9e7 | 13373 | |
13374 | /* Our representation of array types uses "N - 1" as the | |
13375 | TYPE_MAX_VALUE for an array with "N" elements, if "N" is | |
657e2cb1 | 13376 | not an integer constant. We cannot unify arbitrarily |
13377 | complex expressions, so we eliminate the MINUS_EXPRs | |
13378 | here. */ | |
13379 | parm_max = TYPE_MAX_VALUE (TYPE_DOMAIN (parm)); | |
13380 | parm_cst = TREE_CODE (parm_max) == INTEGER_CST; | |
13381 | if (!parm_cst) | |
cb47f9e7 | 13382 | { |
657e2cb1 | 13383 | gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR); |
cb47f9e7 | 13384 | parm_max = TREE_OPERAND (parm_max, 0); |
13385 | } | |
657e2cb1 | 13386 | arg_max = TYPE_MAX_VALUE (TYPE_DOMAIN (arg)); |
13387 | arg_cst = TREE_CODE (arg_max) == INTEGER_CST; | |
13388 | if (!arg_cst) | |
13389 | { | |
13390 | /* The ARG_MAX may not be a simple MINUS_EXPR, if we are | |
13391 | trying to unify the type of a variable with the type | |
13392 | of a template parameter. For example: | |
13393 | ||
13394 | template <unsigned int N> | |
13395 | void f (char (&) [N]); | |
13396 | int g(); | |
13397 | void h(int i) { | |
13398 | char a[g(i)]; | |
13399 | f(a); | |
13400 | } | |
13401 | ||
13402 | Here, the type of the ARG will be "int [g(i)]", and | |
13403 | may be a SAVE_EXPR, etc. */ | |
13404 | if (TREE_CODE (arg_max) != MINUS_EXPR) | |
13405 | return 1; | |
13406 | arg_max = TREE_OPERAND (arg_max, 0); | |
13407 | } | |
13408 | ||
13409 | /* If only one of the bounds used a MINUS_EXPR, compensate | |
13410 | by adding one to the other bound. */ | |
13411 | if (parm_cst && !arg_cst) | |
13412 | parm_max = fold_build2 (PLUS_EXPR, | |
13413 | integer_type_node, | |
13414 | parm_max, | |
13415 | integer_one_node); | |
13416 | else if (arg_cst && !parm_cst) | |
13417 | arg_max = fold_build2 (PLUS_EXPR, | |
13418 | integer_type_node, | |
13419 | arg_max, | |
13420 | integer_one_node); | |
cb47f9e7 | 13421 | |
13422 | if (unify (tparms, targs, parm_max, arg_max, UNIFY_ALLOW_INTEGER)) | |
13423 | return 1; | |
13424 | } | |
32b5f03e | 13425 | return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg), |
b39d755d | 13426 | strict & UNIFY_ALLOW_MORE_CV_QUAL); |
471086d6 | 13427 | |
13428 | case REAL_TYPE: | |
c4a8ac95 | 13429 | case COMPLEX_TYPE: |
90a83261 | 13430 | case VECTOR_TYPE: |
471086d6 | 13431 | case INTEGER_TYPE: |
03db3ebd | 13432 | case BOOLEAN_TYPE: |
a837f55e | 13433 | case ENUMERAL_TYPE: |
55d94afe | 13434 | case VOID_TYPE: |
c38086bd | 13435 | if (TREE_CODE (arg) != TREE_CODE (parm)) |
13436 | return 1; | |
9031d10b | 13437 | |
1361fb16 | 13438 | /* We have already checked cv-qualification at the top of the |
0df40fee | 13439 | function. */ |
cb47f9e7 | 13440 | if (!same_type_ignoring_top_level_qualifiers_p (arg, parm)) |
839e6096 | 13441 | return 1; |
13442 | ||
471086d6 | 13443 | /* As far as unification is concerned, this wins. Later checks |
13444 | will invalidate it if necessary. */ | |
13445 | return 0; | |
13446 | ||
13447 | /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */ | |
b465397d | 13448 | /* Type INTEGER_CST can come from ordinary constant template args. */ |
471086d6 | 13449 | case INTEGER_CST: |
b465397d | 13450 | while (TREE_CODE (arg) == NOP_EXPR) |
13451 | arg = TREE_OPERAND (arg, 0); | |
13452 | ||
471086d6 | 13453 | if (TREE_CODE (arg) != INTEGER_CST) |
13454 | return 1; | |
13455 | return !tree_int_cst_equal (parm, arg); | |
13456 | ||
471086d6 | 13457 | case TREE_VEC: |
13458 | { | |
13459 | int i; | |
13460 | if (TREE_CODE (arg) != TREE_VEC) | |
13461 | return 1; | |
13462 | if (TREE_VEC_LENGTH (parm) != TREE_VEC_LENGTH (arg)) | |
13463 | return 1; | |
148e86d7 | 13464 | for (i = 0; i < TREE_VEC_LENGTH (parm); ++i) |
32b5f03e | 13465 | if (unify (tparms, targs, |
471086d6 | 13466 | TREE_VEC_ELT (parm, i), TREE_VEC_ELT (arg, i), |
6d158f4d | 13467 | UNIFY_ALLOW_NONE)) |
471086d6 | 13468 | return 1; |
13469 | return 0; | |
13470 | } | |
13471 | ||
471086d6 | 13472 | case RECORD_TYPE: |
53137e6a | 13473 | case UNION_TYPE: |
53137e6a | 13474 | if (TREE_CODE (arg) != TREE_CODE (parm)) |
d81e00a4 | 13475 | return 1; |
9031d10b | 13476 | |
8c18e707 | 13477 | if (TYPE_PTRMEMFUNC_P (parm)) |
13478 | { | |
13479 | if (!TYPE_PTRMEMFUNC_P (arg)) | |
13480 | return 1; | |
13481 | ||
9031d10b | 13482 | return unify (tparms, targs, |
8c18e707 | 13483 | TYPE_PTRMEMFUNC_FN_TYPE (parm), |
13484 | TYPE_PTRMEMFUNC_FN_TYPE (arg), | |
13485 | strict); | |
13486 | } | |
13487 | ||
1b7a7ed1 | 13488 | if (CLASSTYPE_TEMPLATE_INFO (parm)) |
e857e9c7 | 13489 | { |
596c0ae6 | 13490 | tree t = NULL_TREE; |
6d158f4d | 13491 | |
7e0d59b2 | 13492 | if (strict_in & UNIFY_ALLOW_DERIVED) |
6d158f4d | 13493 | { |
13494 | /* First, we try to unify the PARM and ARG directly. */ | |
13495 | t = try_class_unification (tparms, targs, | |
13496 | parm, arg); | |
13497 | ||
13498 | if (!t) | |
13499 | { | |
13500 | /* Fallback to the special case allowed in | |
13501 | [temp.deduct.call]: | |
9031d10b | 13502 | |
6d158f4d | 13503 | If P is a class, and P has the form |
13504 | template-id, then A can be a derived class of | |
13505 | the deduced A. Likewise, if P is a pointer to | |
13506 | a class of the form template-id, A can be a | |
13507 | pointer to a derived class pointed to by the | |
13508 | deduced A. */ | |
bce43c50 | 13509 | t = get_template_base (tparms, targs, parm, arg); |
6d158f4d | 13510 | |
bce43c50 | 13511 | if (!t) |
6d158f4d | 13512 | return 1; |
13513 | } | |
13514 | } | |
9031d10b | 13515 | else if (CLASSTYPE_TEMPLATE_INFO (arg) |
13516 | && (CLASSTYPE_TI_TEMPLATE (parm) | |
6f0e36c6 | 13517 | == CLASSTYPE_TI_TEMPLATE (arg))) |
e129c98d | 13518 | /* Perhaps PARM is something like S<U> and ARG is S<int>. |
13519 | Then, we should unify `int' and `U'. */ | |
596c0ae6 | 13520 | t = arg; |
6d158f4d | 13521 | else |
ec0eee8d | 13522 | /* There's no chance of unification succeeding. */ |
e857e9c7 | 13523 | return 1; |
596c0ae6 | 13524 | |
32b5f03e | 13525 | return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm), |
6d158f4d | 13526 | CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE); |
e857e9c7 | 13527 | } |
1361fb16 | 13528 | else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg)) |
e857e9c7 | 13529 | return 1; |
d81e00a4 | 13530 | return 0; |
471086d6 | 13531 | |
13532 | case METHOD_TYPE: | |
471086d6 | 13533 | case FUNCTION_TYPE: |
32b5f03e | 13534 | if (TREE_CODE (arg) != TREE_CODE (parm)) |
471086d6 | 13535 | return 1; |
32b5f03e | 13536 | |
b31f4474 | 13537 | /* CV qualifications for methods can never be deduced, they must |
074ab442 | 13538 | match exactly. We need to check them explicitly here, |
13539 | because type_unification_real treats them as any other | |
13540 | cvqualified parameter. */ | |
b31f4474 | 13541 | if (TREE_CODE (parm) == METHOD_TYPE |
13542 | && (!check_cv_quals_for_unify | |
13543 | (UNIFY_ALLOW_NONE, | |
13544 | TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (arg))), | |
13545 | TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (parm)))))) | |
13546 | return 1; | |
13547 | ||
32b5f03e | 13548 | if (unify (tparms, targs, TREE_TYPE (parm), |
6d158f4d | 13549 | TREE_TYPE (arg), UNIFY_ALLOW_NONE)) |
bea7d742 | 13550 | return 1; |
b1cfe2be | 13551 | return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm), |
db3bf8f6 | 13552 | TYPE_ARG_TYPES (arg), 1, DEDUCE_EXACT, |
13553 | LOOKUP_NORMAL); | |
d81e00a4 | 13554 | |
13555 | case OFFSET_TYPE: | |
11ef5c28 | 13556 | /* Unify a pointer to member with a pointer to member function, which |
653e5405 | 13557 | deduces the type of the member as a function type. */ |
11ef5c28 | 13558 | if (TYPE_PTRMEMFUNC_P (arg)) |
653e5405 | 13559 | { |
13560 | tree method_type; | |
13561 | tree fntype; | |
13562 | cp_cv_quals cv_quals; | |
13563 | ||
13564 | /* Check top-level cv qualifiers */ | |
13565 | if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm)) | |
13566 | return 1; | |
13567 | ||
13568 | if (unify (tparms, targs, TYPE_OFFSET_BASETYPE (parm), | |
13569 | TYPE_PTRMEMFUNC_OBJECT_TYPE (arg), UNIFY_ALLOW_NONE)) | |
13570 | return 1; | |
13571 | ||
13572 | /* Determine the type of the function we are unifying against. */ | |
13573 | method_type = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (arg)); | |
13574 | fntype = | |
13575 | build_function_type (TREE_TYPE (method_type), | |
13576 | TREE_CHAIN (TYPE_ARG_TYPES (method_type))); | |
13577 | ||
13578 | /* Extract the cv-qualifiers of the member function from the | |
13579 | implicit object parameter and place them on the function | |
13580 | type to be restored later. */ | |
13581 | cv_quals = | |
13582 | cp_type_quals(TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (method_type)))); | |
13583 | fntype = build_qualified_type (fntype, cv_quals); | |
13584 | return unify (tparms, targs, TREE_TYPE (parm), fntype, strict); | |
13585 | } | |
11ef5c28 | 13586 | |
d81e00a4 | 13587 | if (TREE_CODE (arg) != OFFSET_TYPE) |
13588 | return 1; | |
32b5f03e | 13589 | if (unify (tparms, targs, TYPE_OFFSET_BASETYPE (parm), |
6d158f4d | 13590 | TYPE_OFFSET_BASETYPE (arg), UNIFY_ALLOW_NONE)) |
d81e00a4 | 13591 | return 1; |
32b5f03e | 13592 | return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg), |
6d158f4d | 13593 | strict); |
d81e00a4 | 13594 | |
c90e0199 | 13595 | case CONST_DECL: |
0a3b29ad | 13596 | if (DECL_TEMPLATE_PARM_P (parm)) |
13597 | return unify (tparms, targs, DECL_INITIAL (parm), arg, strict); | |
9031d10b | 13598 | if (arg != integral_constant_value (parm)) |
c90e0199 | 13599 | return 1; |
13600 | return 0; | |
13601 | ||
02ca53f2 | 13602 | case FIELD_DECL: |
ac25c32b | 13603 | case TEMPLATE_DECL: |
13604 | /* Matched cases are handled by the ARG == PARM test above. */ | |
13605 | return 1; | |
13606 | ||
d95d815d | 13607 | case TYPE_ARGUMENT_PACK: |
13608 | case NONTYPE_ARGUMENT_PACK: | |
13609 | { | |
13610 | tree packed_parms = ARGUMENT_PACK_ARGS (parm); | |
13611 | tree packed_args = ARGUMENT_PACK_ARGS (arg); | |
13612 | int i, len = TREE_VEC_LENGTH (packed_parms); | |
13613 | int argslen = TREE_VEC_LENGTH (packed_args); | |
13614 | int parm_variadic_p = 0; | |
13615 | ||
d1cc437c | 13616 | for (i = 0; i < len; ++i) |
13617 | { | |
13618 | if (PACK_EXPANSION_P (TREE_VEC_ELT (packed_parms, i))) | |
13619 | { | |
13620 | if (i == len - 1) | |
13621 | /* We can unify against something with a trailing | |
13622 | parameter pack. */ | |
13623 | parm_variadic_p = 1; | |
13624 | else | |
13625 | /* Since there is something following the pack | |
13626 | expansion, we cannot unify this template argument | |
13627 | list. */ | |
13628 | return 0; | |
13629 | } | |
13630 | } | |
13631 | ||
d95d815d | 13632 | |
13633 | /* If we don't have enough arguments to satisfy the parameters | |
13634 | (not counting the pack expression at the end), or we have | |
13635 | too many arguments for a parameter list that doesn't end in | |
13636 | a pack expression, we can't unify. */ | |
13637 | if (argslen < (len - parm_variadic_p) | |
13638 | || (argslen > len && !parm_variadic_p)) | |
13639 | return 1; | |
13640 | ||
13641 | /* Unify all of the parameters that precede the (optional) | |
13642 | pack expression. */ | |
13643 | for (i = 0; i < len - parm_variadic_p; ++i) | |
13644 | { | |
13645 | if (unify (tparms, targs, TREE_VEC_ELT (packed_parms, i), | |
13646 | TREE_VEC_ELT (packed_args, i), strict)) | |
13647 | return 1; | |
13648 | } | |
13649 | ||
13650 | if (parm_variadic_p) | |
13651 | return unify_pack_expansion (tparms, targs, | |
13652 | packed_parms, packed_args, | |
13653 | strict, /*call_args_p=*/false, | |
13654 | /*subr=*/false); | |
13655 | return 0; | |
13656 | } | |
13657 | ||
13658 | break; | |
13659 | ||
34da8800 | 13660 | case TYPEOF_TYPE: |
13661 | case DECLTYPE_TYPE: | |
13662 | /* Cannot deduce anything from TYPEOF_TYPE or DECLTYPE_TYPE | |
13663 | nodes. */ | |
13664 | return 0; | |
13665 | ||
4eaf1b43 | 13666 | case ERROR_MARK: |
13667 | /* Unification fails if we hit an error node. */ | |
13668 | return 1; | |
13669 | ||
471086d6 | 13670 | default: |
ce45a448 | 13671 | gcc_assert (EXPR_P (parm)); |
9031d10b | 13672 | |
0262ceed | 13673 | /* We must be looking at an expression. This can happen with |
9031d10b | 13674 | something like: |
13675 | ||
0262ceed | 13676 | template <int I> |
13677 | void foo(S<I>, S<I + 2>); | |
c906a2a7 | 13678 | |
0262ceed | 13679 | This is a "nondeduced context": |
c906a2a7 | 13680 | |
0262ceed | 13681 | [deduct.type] |
9031d10b | 13682 | |
0262ceed | 13683 | The nondeduced contexts are: |
c906a2a7 | 13684 | |
0262ceed | 13685 | --A type that is a template-id in which one or more of |
13686 | the template-arguments is an expression that references | |
9031d10b | 13687 | a template-parameter. |
c906a2a7 | 13688 | |
0262ceed | 13689 | In these cases, we assume deduction succeeded, but don't |
13690 | actually infer any unifications. */ | |
25963226 | 13691 | |
0262ceed | 13692 | if (!uses_template_parms (parm) |
13693 | && !template_args_equal (parm, arg)) | |
13694 | return 1; | |
13695 | else | |
13696 | return 0; | |
471086d6 | 13697 | } |
13698 | } | |
471086d6 | 13699 | \f |
caa6fdce | 13700 | /* Note that DECL can be defined in this translation unit, if |
13701 | required. */ | |
13702 | ||
13703 | static void | |
13704 | mark_definable (tree decl) | |
13705 | { | |
13706 | tree clone; | |
13707 | DECL_NOT_REALLY_EXTERN (decl) = 1; | |
13708 | FOR_EACH_CLONE (clone, decl) | |
13709 | DECL_NOT_REALLY_EXTERN (clone) = 1; | |
13710 | } | |
13711 | ||
954885ed | 13712 | /* Called if RESULT is explicitly instantiated, or is a member of an |
caa6fdce | 13713 | explicitly instantiated class. */ |
954885ed | 13714 | |
bb09dca5 | 13715 | void |
807be5b4 | 13716 | mark_decl_instantiated (tree result, int extern_p) |
bb09dca5 | 13717 | { |
e4959831 | 13718 | SET_DECL_EXPLICIT_INSTANTIATION (result); |
04d89d04 | 13719 | |
55293db3 | 13720 | /* If this entity has already been written out, it's too late to |
13721 | make any modifications. */ | |
13722 | if (TREE_ASM_WRITTEN (result)) | |
13723 | return; | |
13724 | ||
13725 | if (TREE_CODE (result) != FUNCTION_DECL) | |
13726 | /* The TREE_PUBLIC flag for function declarations will have been | |
13727 | set correctly by tsubst. */ | |
13728 | TREE_PUBLIC (result) = 1; | |
13729 | ||
4e80f1c4 | 13730 | /* This might have been set by an earlier implicit instantiation. */ |
13731 | DECL_COMDAT (result) = 0; | |
13732 | ||
caa6fdce | 13733 | if (extern_p) |
13734 | DECL_NOT_REALLY_EXTERN (result) = 0; | |
13735 | else | |
bb09dca5 | 13736 | { |
caa6fdce | 13737 | mark_definable (result); |
5c8b4cb9 | 13738 | /* Always make artificials weak. */ |
13739 | if (DECL_ARTIFICIAL (result) && flag_weak) | |
13740 | comdat_linkage (result); | |
db173e97 | 13741 | /* For WIN32 we also want to put explicit instantiations in |
13742 | linkonce sections. */ | |
5c8b4cb9 | 13743 | else if (TREE_PUBLIC (result)) |
347cbbaf | 13744 | maybe_make_one_only (result); |
bb09dca5 | 13745 | } |
9031d10b | 13746 | |
caa6fdce | 13747 | /* If EXTERN_P, then this function will not be emitted -- unless |
13748 | followed by an explicit instantiation, at which point its linkage | |
13749 | will be adjusted. If !EXTERN_P, then this function will be | |
13750 | emitted here. In neither circumstance do we want | |
13751 | import_export_decl to adjust the linkage. */ | |
9031d10b | 13752 | DECL_INTERFACE_KNOWN (result) = 1; |
bb09dca5 | 13753 | } |
13754 | ||
2db0e9b3 | 13755 | /* Given two function templates PAT1 and PAT2, return: |
596c0ae6 | 13756 | |
13757 | 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order]. | |
13758 | -1 if PAT2 is more specialized than PAT1. | |
2db0e9b3 | 13759 | 0 if neither is more specialized. |
13760 | ||
517ee39a | 13761 | LEN indicates the number of parameters we should consider |
13762 | (defaulted parameters should not be considered). | |
13763 | ||
13764 | The 1998 std underspecified function template partial ordering, and | |
13765 | DR214 addresses the issue. We take pairs of arguments, one from | |
eb6886e3 | 13766 | each of the templates, and deduce them against each other. One of |
517ee39a | 13767 | the templates will be more specialized if all the *other* |
13768 | template's arguments deduce against its arguments and at least one | |
13769 | of its arguments *does* *not* deduce against the other template's | |
13770 | corresponding argument. Deduction is done as for class templates. | |
13771 | The arguments used in deduction have reference and top level cv | |
13772 | qualifiers removed. Iff both arguments were originally reference | |
13773 | types *and* deduction succeeds in both directions, the template | |
13774 | with the more cv-qualified argument wins for that pairing (if | |
13775 | neither is more cv-qualified, they both are equal). Unlike regular | |
13776 | deduction, after all the arguments have been deduced in this way, | |
13777 | we do *not* verify the deduced template argument values can be | |
13778 | substituted into non-deduced contexts, nor do we have to verify | |
13779 | that all template arguments have been deduced. */ | |
9031d10b | 13780 | |
596c0ae6 | 13781 | int |
517ee39a | 13782 | more_specialized_fn (tree pat1, tree pat2, int len) |
13783 | { | |
13784 | tree decl1 = DECL_TEMPLATE_RESULT (pat1); | |
13785 | tree decl2 = DECL_TEMPLATE_RESULT (pat2); | |
13786 | tree targs1 = make_tree_vec (DECL_NTPARMS (pat1)); | |
13787 | tree targs2 = make_tree_vec (DECL_NTPARMS (pat2)); | |
13788 | tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1); | |
13789 | tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2); | |
13790 | tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1)); | |
13791 | tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2)); | |
13792 | int better1 = 0; | |
13793 | int better2 = 0; | |
074ab442 | 13794 | |
3d32803d | 13795 | /* Remove the this parameter from non-static member functions. If |
13796 | one is a non-static member function and the other is not a static | |
13797 | member function, remove the first parameter from that function | |
13798 | also. This situation occurs for operator functions where we | |
13799 | locate both a member function (with this pointer) and non-member | |
13800 | operator (with explicit first operand). */ | |
517ee39a | 13801 | if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1)) |
3d32803d | 13802 | { |
13803 | len--; /* LEN is the number of significant arguments for DECL1 */ | |
13804 | args1 = TREE_CHAIN (args1); | |
13805 | if (!DECL_STATIC_FUNCTION_P (decl2)) | |
13806 | args2 = TREE_CHAIN (args2); | |
13807 | } | |
13808 | else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2)) | |
13809 | { | |
13810 | args2 = TREE_CHAIN (args2); | |
13811 | if (!DECL_STATIC_FUNCTION_P (decl1)) | |
13812 | { | |
13813 | len--; | |
13814 | args1 = TREE_CHAIN (args1); | |
13815 | } | |
13816 | } | |
074ab442 | 13817 | |
1301b597 | 13818 | /* If only one is a conversion operator, they are unordered. */ |
13819 | if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2)) | |
13820 | return 0; | |
9031d10b | 13821 | |
517ee39a | 13822 | /* Consider the return type for a conversion function */ |
13823 | if (DECL_CONV_FN_P (decl1)) | |
13824 | { | |
517ee39a | 13825 | args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1); |
13826 | args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2); | |
13827 | len++; | |
13828 | } | |
9031d10b | 13829 | |
517ee39a | 13830 | processing_template_decl++; |
9031d10b | 13831 | |
c1f194ed | 13832 | while (len-- |
13833 | /* Stop when an ellipsis is seen. */ | |
13834 | && args1 != NULL_TREE && args2 != NULL_TREE) | |
517ee39a | 13835 | { |
13836 | tree arg1 = TREE_VALUE (args1); | |
13837 | tree arg2 = TREE_VALUE (args2); | |
13838 | int deduce1, deduce2; | |
13839 | int quals1 = -1; | |
13840 | int quals2 = -1; | |
596c0ae6 | 13841 | |
d95d815d | 13842 | if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION |
13843 | && TREE_CODE (arg2) == TYPE_PACK_EXPANSION) | |
13844 | { | |
13845 | /* When both arguments are pack expansions, we need only | |
13846 | unify the patterns themselves. */ | |
13847 | arg1 = PACK_EXPANSION_PATTERN (arg1); | |
13848 | arg2 = PACK_EXPANSION_PATTERN (arg2); | |
13849 | ||
13850 | /* This is the last comparison we need to do. */ | |
13851 | len = 0; | |
13852 | } | |
13853 | ||
517ee39a | 13854 | if (TREE_CODE (arg1) == REFERENCE_TYPE) |
13855 | { | |
13856 | arg1 = TREE_TYPE (arg1); | |
13857 | quals1 = cp_type_quals (arg1); | |
13858 | } | |
9031d10b | 13859 | |
517ee39a | 13860 | if (TREE_CODE (arg2) == REFERENCE_TYPE) |
13861 | { | |
13862 | arg2 = TREE_TYPE (arg2); | |
13863 | quals2 = cp_type_quals (arg2); | |
13864 | } | |
596c0ae6 | 13865 | |
517ee39a | 13866 | if ((quals1 < 0) != (quals2 < 0)) |
13867 | { | |
13868 | /* Only of the args is a reference, see if we should apply | |
13869 | array/function pointer decay to it. This is not part of | |
13870 | DR214, but is, IMHO, consistent with the deduction rules | |
13871 | for the function call itself, and with our earlier | |
13872 | implementation of the underspecified partial ordering | |
13873 | rules. (nathan). */ | |
13874 | if (quals1 >= 0) | |
13875 | { | |
13876 | switch (TREE_CODE (arg1)) | |
13877 | { | |
13878 | case ARRAY_TYPE: | |
13879 | arg1 = TREE_TYPE (arg1); | |
13880 | /* FALLTHROUGH. */ | |
13881 | case FUNCTION_TYPE: | |
13882 | arg1 = build_pointer_type (arg1); | |
13883 | break; | |
9031d10b | 13884 | |
517ee39a | 13885 | default: |
13886 | break; | |
13887 | } | |
13888 | } | |
13889 | else | |
13890 | { | |
13891 | switch (TREE_CODE (arg2)) | |
13892 | { | |
13893 | case ARRAY_TYPE: | |
13894 | arg2 = TREE_TYPE (arg2); | |
13895 | /* FALLTHROUGH. */ | |
13896 | case FUNCTION_TYPE: | |
13897 | arg2 = build_pointer_type (arg2); | |
13898 | break; | |
9031d10b | 13899 | |
517ee39a | 13900 | default: |
13901 | break; | |
13902 | } | |
13903 | } | |
13904 | } | |
9031d10b | 13905 | |
517ee39a | 13906 | arg1 = TYPE_MAIN_VARIANT (arg1); |
13907 | arg2 = TYPE_MAIN_VARIANT (arg2); | |
9031d10b | 13908 | |
d95d815d | 13909 | if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION) |
13910 | { | |
a4f66688 | 13911 | int i, len2 = list_length (args2); |
d95d815d | 13912 | tree parmvec = make_tree_vec (1); |
13913 | tree argvec = make_tree_vec (len2); | |
13914 | tree ta = args2; | |
13915 | ||
13916 | /* Setup the parameter vector, which contains only ARG1. */ | |
13917 | TREE_VEC_ELT (parmvec, 0) = arg1; | |
13918 | ||
13919 | /* Setup the argument vector, which contains the remaining | |
13920 | arguments. */ | |
13921 | for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta)) | |
13922 | TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta); | |
13923 | ||
13924 | deduce1 = !unify_pack_expansion (tparms1, targs1, parmvec, | |
13925 | argvec, UNIFY_ALLOW_NONE, | |
13926 | /*call_args_p=*/false, | |
13927 | /*subr=*/0); | |
13928 | ||
13929 | /* We cannot deduce in the other direction, because ARG1 is | |
13930 | a pack expansion but ARG2 is not. */ | |
13931 | deduce2 = 0; | |
13932 | } | |
13933 | else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION) | |
13934 | { | |
a4f66688 | 13935 | int i, len1 = list_length (args1); |
d95d815d | 13936 | tree parmvec = make_tree_vec (1); |
13937 | tree argvec = make_tree_vec (len1); | |
13938 | tree ta = args1; | |
13939 | ||
13940 | /* Setup the parameter vector, which contains only ARG1. */ | |
13941 | TREE_VEC_ELT (parmvec, 0) = arg2; | |
13942 | ||
13943 | /* Setup the argument vector, which contains the remaining | |
13944 | arguments. */ | |
13945 | for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta)) | |
13946 | TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta); | |
13947 | ||
13948 | deduce2 = !unify_pack_expansion (tparms2, targs2, parmvec, | |
13949 | argvec, UNIFY_ALLOW_NONE, | |
13950 | /*call_args_p=*/false, | |
13951 | /*subr=*/0); | |
13952 | ||
13953 | /* We cannot deduce in the other direction, because ARG2 is | |
13954 | a pack expansion but ARG1 is not.*/ | |
13955 | deduce1 = 0; | |
13956 | } | |
13957 | ||
13958 | else | |
13959 | { | |
13960 | /* The normal case, where neither argument is a pack | |
13961 | expansion. */ | |
13962 | deduce1 = !unify (tparms1, targs1, arg1, arg2, UNIFY_ALLOW_NONE); | |
13963 | deduce2 = !unify (tparms2, targs2, arg2, arg1, UNIFY_ALLOW_NONE); | |
13964 | } | |
517ee39a | 13965 | |
13966 | if (!deduce1) | |
13967 | better2 = -1; | |
13968 | if (!deduce2) | |
13969 | better1 = -1; | |
13970 | if (better1 < 0 && better2 < 0) | |
13971 | /* We've failed to deduce something in either direction. | |
13972 | These must be unordered. */ | |
13973 | break; | |
9031d10b | 13974 | |
517ee39a | 13975 | if (deduce1 && deduce2 && quals1 >= 0 && quals2 >= 0) |
13976 | { | |
13977 | /* Deduces in both directions, see if quals can | |
13978 | disambiguate. Pretend the worse one failed to deduce. */ | |
13979 | if ((quals1 & quals2) == quals2) | |
13980 | deduce1 = 0; | |
13981 | if ((quals1 & quals2) == quals1) | |
13982 | deduce2 = 0; | |
13983 | } | |
13984 | if (deduce1 && !deduce2 && !better2) | |
13985 | better2 = 1; | |
13986 | if (deduce2 && !deduce1 && !better1) | |
13987 | better1 = 1; | |
9031d10b | 13988 | |
d95d815d | 13989 | if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION |
13990 | || TREE_CODE (arg2) == TYPE_PACK_EXPANSION) | |
13991 | /* We have already processed all of the arguments in our | |
13992 | handing of the pack expansion type. */ | |
13993 | len = 0; | |
13994 | ||
517ee39a | 13995 | args1 = TREE_CHAIN (args1); |
13996 | args2 = TREE_CHAIN (args2); | |
13997 | } | |
13998 | ||
13999 | processing_template_decl--; | |
14000 | ||
d95d815d | 14001 | /* All things being equal, if the next argument is a pack expansion |
14002 | for one function but not for the other, prefer the | |
14003 | non-variadic function. */ | |
14004 | if ((better1 > 0) - (better2 > 0) == 0 | |
14005 | && args1 && TREE_VALUE (args1) | |
14006 | && args2 && TREE_VALUE (args2)) | |
14007 | { | |
14008 | if (TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION) | |
14009 | return TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION ? 0 : -1; | |
14010 | else if (TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION) | |
14011 | return 1; | |
14012 | } | |
14013 | ||
517ee39a | 14014 | return (better1 > 0) - (better2 > 0); |
d3ce0f51 | 14015 | } |
596c0ae6 | 14016 | |
aa32d258 | 14017 | /* Determine which of two partial specializations is more specialized. |
596c0ae6 | 14018 | |
aa32d258 | 14019 | PAT1 is a TREE_LIST whose TREE_TYPE is the _TYPE node corresponding |
14020 | to the first partial specialization. The TREE_VALUE is the | |
14021 | innermost set of template parameters for the partial | |
14022 | specialization. PAT2 is similar, but for the second template. | |
14023 | ||
14024 | Return 1 if the first partial specialization is more specialized; | |
14025 | -1 if the second is more specialized; 0 if neither is more | |
14026 | specialized. | |
ad3a6239 | 14027 | |
aa32d258 | 14028 | See [temp.class.order] for information about determining which of |
074ab442 | 14029 | two templates is more specialized. */ |
9031d10b | 14030 | |
e75bce43 | 14031 | static int |
aa32d258 | 14032 | more_specialized_class (tree pat1, tree pat2) |
d3ce0f51 | 14033 | { |
14034 | tree targs; | |
aa32d258 | 14035 | tree tmpl1, tmpl2; |
d3ce0f51 | 14036 | int winner = 0; |
d95d815d | 14037 | bool any_deductions = false; |
d3ce0f51 | 14038 | |
aa32d258 | 14039 | tmpl1 = TREE_TYPE (pat1); |
14040 | tmpl2 = TREE_TYPE (pat2); | |
14041 | ||
9031d10b | 14042 | /* Just like what happens for functions, if we are ordering between |
94affa87 | 14043 | different class template specializations, we may encounter dependent |
14044 | types in the arguments, and we need our dependency check functions | |
14045 | to behave correctly. */ | |
14046 | ++processing_template_decl; | |
074ab442 | 14047 | targs = get_class_bindings (TREE_VALUE (pat1), |
aa32d258 | 14048 | CLASSTYPE_TI_ARGS (tmpl1), |
14049 | CLASSTYPE_TI_ARGS (tmpl2)); | |
d3ce0f51 | 14050 | if (targs) |
d95d815d | 14051 | { |
14052 | --winner; | |
14053 | any_deductions = true; | |
14054 | } | |
d3ce0f51 | 14055 | |
074ab442 | 14056 | targs = get_class_bindings (TREE_VALUE (pat2), |
aa32d258 | 14057 | CLASSTYPE_TI_ARGS (tmpl2), |
14058 | CLASSTYPE_TI_ARGS (tmpl1)); | |
d3ce0f51 | 14059 | if (targs) |
d95d815d | 14060 | { |
14061 | ++winner; | |
14062 | any_deductions = true; | |
14063 | } | |
94affa87 | 14064 | --processing_template_decl; |
596c0ae6 | 14065 | |
d95d815d | 14066 | /* In the case of a tie where at least one of the class templates |
14067 | has a parameter pack at the end, the template with the most | |
14068 | non-packed parameters wins. */ | |
14069 | if (winner == 0 | |
14070 | && any_deductions | |
14071 | && (template_args_variadic_p (TREE_PURPOSE (pat1)) | |
14072 | || template_args_variadic_p (TREE_PURPOSE (pat2)))) | |
14073 | { | |
14074 | tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1)); | |
14075 | tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2)); | |
14076 | int len1 = TREE_VEC_LENGTH (args1); | |
14077 | int len2 = TREE_VEC_LENGTH (args2); | |
14078 | ||
14079 | /* We don't count the pack expansion at the end. */ | |
14080 | if (template_args_variadic_p (TREE_PURPOSE (pat1))) | |
14081 | --len1; | |
14082 | if (template_args_variadic_p (TREE_PURPOSE (pat2))) | |
14083 | --len2; | |
14084 | ||
14085 | if (len1 > len2) | |
14086 | return 1; | |
14087 | else if (len1 < len2) | |
14088 | return -1; | |
14089 | } | |
14090 | ||
596c0ae6 | 14091 | return winner; |
14092 | } | |
d3ce0f51 | 14093 | |
14094 | /* Return the template arguments that will produce the function signature | |
1146f179 | 14095 | DECL from the function template FN, with the explicit template |
022b46b4 | 14096 | arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must |
6d158f4d | 14097 | also match. Return NULL_TREE if no satisfactory arguments could be |
022b46b4 | 14098 | found. */ |
9031d10b | 14099 | |
8dcea779 | 14100 | static tree |
022b46b4 | 14101 | get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype) |
d3ce0f51 | 14102 | { |
64b4f183 | 14103 | int ntparms = DECL_NTPARMS (fn); |
19c3b3a6 | 14104 | tree targs = make_tree_vec (ntparms); |
6d158f4d | 14105 | tree decl_type; |
88e449ea | 14106 | tree decl_arg_types; |
64b4f183 | 14107 | |
6d158f4d | 14108 | /* Substitute the explicit template arguments into the type of DECL. |
14109 | The call to fn_type_unification will handle substitution into the | |
14110 | FN. */ | |
14111 | decl_type = TREE_TYPE (decl); | |
14112 | if (explicit_args && uses_template_parms (decl_type)) | |
14113 | { | |
14114 | tree tmpl; | |
14115 | tree converted_args; | |
14116 | ||
14117 | if (DECL_TEMPLATE_INFO (decl)) | |
14118 | tmpl = DECL_TI_TEMPLATE (decl); | |
14119 | else | |
6c0cc2cd | 14120 | /* We can get here for some invalid specializations. */ |
6d158f4d | 14121 | return NULL_TREE; |
14122 | ||
14123 | converted_args | |
36653a30 | 14124 | = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl), |
14125 | explicit_args, NULL_TREE, | |
074ab442 | 14126 | tf_none, |
36653a30 | 14127 | /*require_all_args=*/false, |
14128 | /*use_default_args=*/false); | |
6d158f4d | 14129 | if (converted_args == error_mark_node) |
14130 | return NULL_TREE; | |
9031d10b | 14131 | |
14132 | decl_type = tsubst (decl_type, converted_args, tf_none, NULL_TREE); | |
6d158f4d | 14133 | if (decl_type == error_mark_node) |
14134 | return NULL_TREE; | |
14135 | } | |
14136 | ||
2db0e9b3 | 14137 | /* Never do unification on the 'this' parameter. */ |
14aa085c | 14138 | decl_arg_types = skip_artificial_parms_for (decl, |
14139 | TYPE_ARG_TYPES (decl_type)); | |
54008b6b | 14140 | |
9031d10b | 14141 | if (fn_type_unification (fn, explicit_args, targs, |
88e449ea | 14142 | decl_arg_types, |
1b5e8868 | 14143 | (check_rettype || DECL_CONV_FN_P (fn) |
653e5405 | 14144 | ? TREE_TYPE (decl_type) : NULL_TREE), |
db3bf8f6 | 14145 | DEDUCE_EXACT, LOOKUP_NORMAL)) |
8dcea779 | 14146 | return NULL_TREE; |
14147 | ||
8dcea779 | 14148 | return targs; |
14149 | } | |
14150 | ||
34197853 | 14151 | /* Return the innermost template arguments that, when applied to a |
14152 | template specialization whose innermost template parameters are | |
eff2dffd | 14153 | TPARMS, and whose specialization arguments are PARMS, yield the |
9031d10b | 14154 | ARGS. |
34197853 | 14155 | |
14156 | For example, suppose we have: | |
14157 | ||
14158 | template <class T, class U> struct S {}; | |
14159 | template <class T> struct S<T*, int> {}; | |
14160 | ||
14161 | Then, suppose we want to get `S<double*, int>'. The TPARMS will be | |
aa32d258 | 14162 | {T}, the SPEC_ARGS will be {T*, int} and the ARGS will be {double*, |
34197853 | 14163 | int}. The resulting vector will be {double}, indicating that `T' |
14164 | is bound to `double'. */ | |
14165 | ||
b465397d | 14166 | static tree |
aa32d258 | 14167 | get_class_bindings (tree tparms, tree spec_args, tree args) |
d3ce0f51 | 14168 | { |
37e14c87 | 14169 | int i, ntparms = TREE_VEC_LENGTH (tparms); |
aa32d258 | 14170 | tree deduced_args; |
14171 | tree innermost_deduced_args; | |
d3ce0f51 | 14172 | |
aa32d258 | 14173 | innermost_deduced_args = make_tree_vec (ntparms); |
14174 | if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args)) | |
14175 | { | |
14176 | deduced_args = copy_node (args); | |
14177 | SET_TMPL_ARGS_LEVEL (deduced_args, | |
14178 | TMPL_ARGS_DEPTH (deduced_args), | |
14179 | innermost_deduced_args); | |
14180 | } | |
14181 | else | |
074ab442 | 14182 | deduced_args = innermost_deduced_args; |
aa32d258 | 14183 | |
074ab442 | 14184 | if (unify (tparms, deduced_args, |
14185 | INNERMOST_TEMPLATE_ARGS (spec_args), | |
14186 | INNERMOST_TEMPLATE_ARGS (args), | |
653e5405 | 14187 | UNIFY_ALLOW_NONE)) |
34a99cba | 14188 | return NULL_TREE; |
d3ce0f51 | 14189 | |
14190 | for (i = 0; i < ntparms; ++i) | |
aa32d258 | 14191 | if (! TREE_VEC_ELT (innermost_deduced_args, i)) |
d3ce0f51 | 14192 | return NULL_TREE; |
14193 | ||
aa32d258 | 14194 | /* Verify that nondeduced template arguments agree with the type |
14195 | obtained from argument deduction. | |
074ab442 | 14196 | |
aa32d258 | 14197 | For example: |
14198 | ||
14199 | struct A { typedef int X; }; | |
14200 | template <class T, class U> struct C {}; | |
14201 | template <class T> struct C<T, typename T::X> {}; | |
14202 | ||
14203 | Then with the instantiation `C<A, int>', we can deduce that | |
14204 | `T' is `A' but unify () does not check whether `typename T::X' | |
14205 | is `int'. */ | |
14206 | spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE); | |
14207 | if (spec_args == error_mark_node | |
14208 | /* We only need to check the innermost arguments; the other | |
14209 | arguments will always agree. */ | |
14210 | || !comp_template_args (INNERMOST_TEMPLATE_ARGS (spec_args), | |
14211 | INNERMOST_TEMPLATE_ARGS (args))) | |
25963226 | 14212 | return NULL_TREE; |
14213 | ||
e5dab245 | 14214 | /* Now that we have bindings for all of the template arguments, |
14215 | ensure that the arguments deduced for the template template | |
14216 | parameters have compatible template parameter lists. See the use | |
14217 | of template_template_parm_bindings_ok_p in fn_type_unification | |
14218 | for more information. */ | |
14219 | if (!template_template_parm_bindings_ok_p (tparms, deduced_args)) | |
14220 | return NULL_TREE; | |
14221 | ||
aa32d258 | 14222 | return deduced_args; |
d3ce0f51 | 14223 | } |
14224 | ||
d9db88e1 | 14225 | /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL. |
14226 | Return the TREE_LIST node with the most specialized template, if | |
14227 | any. If there is no most specialized template, the error_mark_node | |
14228 | is returned. | |
14229 | ||
14230 | Note that this function does not look at, or modify, the | |
14231 | TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node | |
14232 | returned is one of the elements of INSTANTIATIONS, callers may | |
14233 | store information in the TREE_PURPOSE or TREE_TYPE of the nodes, | |
14234 | and retrieve it from the value returned. */ | |
d3ce0f51 | 14235 | |
14236 | tree | |
d9db88e1 | 14237 | most_specialized_instantiation (tree templates) |
d3ce0f51 | 14238 | { |
3effa7a7 | 14239 | tree fn, champ; |
d3ce0f51 | 14240 | |
517ee39a | 14241 | ++processing_template_decl; |
9031d10b | 14242 | |
d9db88e1 | 14243 | champ = templates; |
14244 | for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn)) | |
d3ce0f51 | 14245 | { |
517ee39a | 14246 | int fate = 0; |
9031d10b | 14247 | |
022b46b4 | 14248 | if (get_bindings (TREE_VALUE (champ), |
14249 | DECL_TEMPLATE_RESULT (TREE_VALUE (fn)), | |
14250 | NULL_TREE, /*check_ret=*/false)) | |
517ee39a | 14251 | fate--; |
14252 | ||
022b46b4 | 14253 | if (get_bindings (TREE_VALUE (fn), |
14254 | DECL_TEMPLATE_RESULT (TREE_VALUE (champ)), | |
14255 | NULL_TREE, /*check_ret=*/false)) | |
517ee39a | 14256 | fate++; |
9031d10b | 14257 | |
f3d3cc67 | 14258 | if (fate == -1) |
14259 | champ = fn; | |
14260 | else if (!fate) | |
d3ce0f51 | 14261 | { |
f3d3cc67 | 14262 | /* Equally specialized, move to next function. If there |
14263 | is no next function, nothing's most specialized. */ | |
14264 | fn = TREE_CHAIN (fn); | |
d9db88e1 | 14265 | champ = fn; |
f3d3cc67 | 14266 | if (!fn) |
14267 | break; | |
d3ce0f51 | 14268 | } |
14269 | } | |
9031d10b | 14270 | |
517ee39a | 14271 | if (champ) |
14272 | /* Now verify that champ is better than everything earlier in the | |
14273 | instantiation list. */ | |
d9db88e1 | 14274 | for (fn = templates; fn != champ; fn = TREE_CHAIN (fn)) |
022b46b4 | 14275 | if (get_bindings (TREE_VALUE (champ), |
14276 | DECL_TEMPLATE_RESULT (TREE_VALUE (fn)), | |
14277 | NULL_TREE, /*check_ret=*/false) | |
14278 | || !get_bindings (TREE_VALUE (fn), | |
14279 | DECL_TEMPLATE_RESULT (TREE_VALUE (champ)), | |
14280 | NULL_TREE, /*check_ret=*/false)) | |
517ee39a | 14281 | { |
14282 | champ = NULL_TREE; | |
14283 | break; | |
14284 | } | |
9031d10b | 14285 | |
517ee39a | 14286 | processing_template_decl--; |
9031d10b | 14287 | |
517ee39a | 14288 | if (!champ) |
14289 | return error_mark_node; | |
d3ce0f51 | 14290 | |
d9db88e1 | 14291 | return champ; |
d3ce0f51 | 14292 | } |
14293 | ||
34197853 | 14294 | /* If DECL is a specialization of some template, return the most |
a32fe883 | 14295 | general such template. Otherwise, returns NULL_TREE. |
14296 | ||
14297 | For example, given: | |
34197853 | 14298 | |
14299 | template <class T> struct S { template <class U> void f(U); }; | |
14300 | ||
14301 | if TMPL is `template <class U> void S<int>::f(U)' this will return | |
14302 | the full template. This function will not trace past partial | |
14303 | specializations, however. For example, given in addition: | |
14304 | ||
14305 | template <class T> struct S<T*> { template <class U> void f(U); }; | |
14306 | ||
14307 | if TMPL is `template <class U> void S<int*>::f(U)' this will return | |
14308 | `template <class T> template <class U> S<T*>::f(U)'. */ | |
d3ce0f51 | 14309 | |
a2a627ce | 14310 | tree |
807be5b4 | 14311 | most_general_template (tree decl) |
d3ce0f51 | 14312 | { |
a32fe883 | 14313 | /* If DECL is a FUNCTION_DECL, find the TEMPLATE_DECL of which it is |
14314 | an immediate specialization. */ | |
14315 | if (TREE_CODE (decl) == FUNCTION_DECL) | |
14316 | { | |
14317 | if (DECL_TEMPLATE_INFO (decl)) { | |
14318 | decl = DECL_TI_TEMPLATE (decl); | |
14319 | ||
14320 | /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE for a | |
14321 | template friend. */ | |
14322 | if (TREE_CODE (decl) != TEMPLATE_DECL) | |
14323 | return NULL_TREE; | |
14324 | } else | |
14325 | return NULL_TREE; | |
14326 | } | |
14327 | ||
14328 | /* Look for more and more general templates. */ | |
14329 | while (DECL_TEMPLATE_INFO (decl)) | |
14330 | { | |
c08d51be | 14331 | /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases. |
14332 | (See cp-tree.h for details.) */ | |
a32fe883 | 14333 | if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL) |
14334 | break; | |
14335 | ||
9ae1acf5 | 14336 | if (CLASS_TYPE_P (TREE_TYPE (decl)) |
14337 | && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl))) | |
14338 | break; | |
14339 | ||
a32fe883 | 14340 | /* Stop if we run into an explicitly specialized class template. */ |
14341 | if (!DECL_NAMESPACE_SCOPE_P (decl) | |
14342 | && DECL_CONTEXT (decl) | |
14343 | && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl))) | |
14344 | break; | |
14345 | ||
14346 | decl = DECL_TI_TEMPLATE (decl); | |
14347 | } | |
34197853 | 14348 | |
14349 | return decl; | |
14350 | } | |
14351 | ||
aa32d258 | 14352 | /* Return the most specialized of the class template partial |
14353 | specializations of TMPL which can produce TYPE, a specialization of | |
14354 | TMPL. The value returned is actually a TREE_LIST; the TREE_TYPE is | |
14355 | a _TYPE node corresponding to the partial specialization, while the | |
14356 | TREE_PURPOSE is the set of template arguments that must be | |
14357 | substituted into the TREE_TYPE in order to generate TYPE. | |
14358 | ||
14359 | If the choice of partial specialization is ambiguous, a diagnostic | |
14360 | is issued, and the error_mark_node is returned. If there are no | |
14361 | partial specializations of TMPL matching TYPE, then NULL_TREE is | |
14362 | returned. */ | |
34197853 | 14363 | |
863c3f96 | 14364 | static tree |
aa32d258 | 14365 | most_specialized_class (tree type, tree tmpl) |
34197853 | 14366 | { |
14367 | tree list = NULL_TREE; | |
14368 | tree t; | |
14369 | tree champ; | |
d3ce0f51 | 14370 | int fate; |
aa32d258 | 14371 | bool ambiguous_p; |
14372 | tree args; | |
73ab75d5 | 14373 | tree outer_args = NULL_TREE; |
d3ce0f51 | 14374 | |
34197853 | 14375 | tmpl = most_general_template (tmpl); |
aa32d258 | 14376 | args = CLASSTYPE_TI_ARGS (type); |
73ab75d5 | 14377 | |
14378 | /* For determining which partial specialization to use, only the | |
14379 | innermost args are interesting. */ | |
14380 | if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args)) | |
14381 | { | |
14382 | outer_args = strip_innermost_template_args (args, 1); | |
14383 | args = INNERMOST_TEMPLATE_ARGS (args); | |
14384 | } | |
14385 | ||
34197853 | 14386 | for (t = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); t; t = TREE_CHAIN (t)) |
d3ce0f51 | 14387 | { |
aa32d258 | 14388 | tree partial_spec_args; |
14389 | tree spec_args; | |
73ab75d5 | 14390 | tree parms = TREE_VALUE (t); |
aa32d258 | 14391 | |
14392 | partial_spec_args = CLASSTYPE_TI_ARGS (TREE_TYPE (t)); | |
73ab75d5 | 14393 | if (outer_args) |
14394 | { | |
14395 | int i; | |
14396 | ||
a254d1ae | 14397 | ++processing_template_decl; |
14398 | ||
73ab75d5 | 14399 | /* Discard the outer levels of args, and then substitute in the |
14400 | template args from the enclosing class. */ | |
14401 | partial_spec_args = INNERMOST_TEMPLATE_ARGS (partial_spec_args); | |
14402 | partial_spec_args = tsubst_template_args | |
14403 | (partial_spec_args, outer_args, tf_none, NULL_TREE); | |
14404 | ||
14405 | /* PARMS already refers to just the innermost parms, but the | |
14406 | template parms in partial_spec_args had their levels lowered | |
14407 | by tsubst, so we need to do the same for the parm list. We | |
14408 | can't just tsubst the TREE_VEC itself, as tsubst wants to | |
14409 | treat a TREE_VEC as an argument vector. */ | |
14410 | parms = copy_node (parms); | |
14411 | for (i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i) | |
14412 | TREE_VEC_ELT (parms, i) = | |
14413 | tsubst (TREE_VEC_ELT (parms, i), outer_args, tf_none, NULL_TREE); | |
a254d1ae | 14414 | |
14415 | --processing_template_decl; | |
73ab75d5 | 14416 | } |
14417 | spec_args = get_class_bindings (parms, | |
074ab442 | 14418 | partial_spec_args, |
aa32d258 | 14419 | args); |
34197853 | 14420 | if (spec_args) |
d3ce0f51 | 14421 | { |
73ab75d5 | 14422 | if (outer_args) |
14423 | spec_args = add_to_template_args (outer_args, spec_args); | |
aa32d258 | 14424 | list = tree_cons (spec_args, TREE_VALUE (t), list); |
d3ce0f51 | 14425 | TREE_TYPE (list) = TREE_TYPE (t); |
14426 | } | |
14427 | } | |
14428 | ||
14429 | if (! list) | |
14430 | return NULL_TREE; | |
14431 | ||
aa32d258 | 14432 | ambiguous_p = false; |
d3ce0f51 | 14433 | t = list; |
14434 | champ = t; | |
14435 | t = TREE_CHAIN (t); | |
14436 | for (; t; t = TREE_CHAIN (t)) | |
14437 | { | |
aa32d258 | 14438 | fate = more_specialized_class (champ, t); |
d3ce0f51 | 14439 | if (fate == 1) |
14440 | ; | |
14441 | else | |
14442 | { | |
14443 | if (fate == 0) | |
14444 | { | |
14445 | t = TREE_CHAIN (t); | |
14446 | if (! t) | |
aa32d258 | 14447 | { |
14448 | ambiguous_p = true; | |
14449 | break; | |
14450 | } | |
d3ce0f51 | 14451 | } |
14452 | champ = t; | |
14453 | } | |
14454 | } | |
14455 | ||
aa32d258 | 14456 | if (!ambiguous_p) |
14457 | for (t = list; t && t != champ; t = TREE_CHAIN (t)) | |
14458 | { | |
14459 | fate = more_specialized_class (champ, t); | |
14460 | if (fate != 1) | |
14461 | { | |
14462 | ambiguous_p = true; | |
14463 | break; | |
14464 | } | |
14465 | } | |
14466 | ||
14467 | if (ambiguous_p) | |
d3ce0f51 | 14468 | { |
aa32d258 | 14469 | const char *str = "candidates are:"; |
14470 | error ("ambiguous class template instantiation for %q#T", type); | |
14471 | for (t = list; t; t = TREE_CHAIN (t)) | |
14472 | { | |
14473 | error ("%s %+#T", str, TREE_TYPE (t)); | |
14474 | str = " "; | |
14475 | } | |
14476 | return error_mark_node; | |
d3ce0f51 | 14477 | } |
14478 | ||
14479 | return champ; | |
14480 | } | |
14481 | ||
ab93b497 | 14482 | /* Explicitly instantiate DECL. */ |
96624a9e | 14483 | |
471086d6 | 14484 | void |
ab93b497 | 14485 | do_decl_instantiation (tree decl, tree storage) |
471086d6 | 14486 | { |
471086d6 | 14487 | tree result = NULL_TREE; |
bb09dca5 | 14488 | int extern_p = 0; |
3f7d79e4 | 14489 | |
4f480861 | 14490 | if (!decl || decl == error_mark_node) |
ec0eee8d | 14491 | /* An error occurred, for which grokdeclarator has already issued |
b0a59c16 | 14492 | an appropriate message. */ |
14493 | return; | |
14494 | else if (! DECL_LANG_SPECIFIC (decl)) | |
dbdf67c6 | 14495 | { |
39e7b4a4 | 14496 | error ("explicit instantiation of non-template %q#D", decl); |
dbdf67c6 | 14497 | return; |
14498 | } | |
954885ed | 14499 | else if (TREE_CODE (decl) == VAR_DECL) |
6686e84d | 14500 | { |
954885ed | 14501 | /* There is an asymmetry here in the way VAR_DECLs and |
14502 | FUNCTION_DECLs are handled by grokdeclarator. In the case of | |
14503 | the latter, the DECL we get back will be marked as a | |
14504 | template instantiation, and the appropriate | |
14505 | DECL_TEMPLATE_INFO will be set up. This does not happen for | |
14506 | VAR_DECLs so we do the lookup here. Probably, grokdeclarator | |
14507 | should handle VAR_DECLs as it currently handles | |
14508 | FUNCTION_DECLs. */ | |
e1c738bf | 14509 | if (!DECL_CLASS_SCOPE_P (decl)) |
14510 | { | |
14511 | error ("%qD is not a static data member of a class template", decl); | |
14512 | return; | |
14513 | } | |
b330805e | 14514 | result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false); |
cf91b86a | 14515 | if (!result || TREE_CODE (result) != VAR_DECL) |
954885ed | 14516 | { |
39e7b4a4 | 14517 | error ("no matching template for %qD found", decl); |
954885ed | 14518 | return; |
14519 | } | |
8ed472d2 | 14520 | if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl))) |
14521 | { | |
14522 | error ("type %qT for explicit instantiation %qD does not match " | |
14523 | "declared type %qT", TREE_TYPE (result), decl, | |
14524 | TREE_TYPE (decl)); | |
14525 | return; | |
14526 | } | |
6686e84d | 14527 | } |
14528 | else if (TREE_CODE (decl) != FUNCTION_DECL) | |
14529 | { | |
39e7b4a4 | 14530 | error ("explicit instantiation of %q#D", decl); |
6686e84d | 14531 | return; |
14532 | } | |
954885ed | 14533 | else |
14534 | result = decl; | |
3addf100 | 14535 | |
954885ed | 14536 | /* Check for various error cases. Note that if the explicit |
6c0cc2cd | 14537 | instantiation is valid the RESULT will currently be marked as an |
954885ed | 14538 | *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set |
14539 | until we get here. */ | |
14540 | ||
14541 | if (DECL_TEMPLATE_SPECIALIZATION (result)) | |
3addf100 | 14542 | { |
a8057507 | 14543 | /* DR 259 [temp.spec]. |
14544 | ||
14545 | Both an explicit instantiation and a declaration of an explicit | |
14546 | specialization shall not appear in a program unless the explicit | |
14547 | instantiation follows a declaration of the explicit specialization. | |
954885ed | 14548 | |
a8057507 | 14549 | For a given set of template parameters, if an explicit |
14550 | instantiation of a template appears after a declaration of an | |
14551 | explicit specialization for that template, the explicit | |
14552 | instantiation has no effect. */ | |
3addf100 | 14553 | return; |
14554 | } | |
954885ed | 14555 | else if (DECL_EXPLICIT_INSTANTIATION (result)) |
14556 | { | |
14557 | /* [temp.spec] | |
64b4f183 | 14558 | |
954885ed | 14559 | No program shall explicitly instantiate any template more |
9031d10b | 14560 | than once. |
954885ed | 14561 | |
caa6fdce | 14562 | We check DECL_NOT_REALLY_EXTERN so as not to complain when |
14563 | the first instantiation was `extern' and the second is not, | |
14564 | and EXTERN_P for the opposite case. */ | |
14565 | if (DECL_NOT_REALLY_EXTERN (result) && !extern_p) | |
39e7b4a4 | 14566 | pedwarn ("duplicate explicit instantiation of %q#D", result); |
caa6fdce | 14567 | /* If an "extern" explicit instantiation follows an ordinary |
14568 | explicit instantiation, the template is instantiated. */ | |
14569 | if (extern_p) | |
954885ed | 14570 | return; |
14571 | } | |
14572 | else if (!DECL_IMPLICIT_INSTANTIATION (result)) | |
bb09dca5 | 14573 | { |
39e7b4a4 | 14574 | error ("no matching template for %qD found", result); |
bb09dca5 | 14575 | return; |
14576 | } | |
954885ed | 14577 | else if (!DECL_TEMPLATE_INFO (result)) |
6686e84d | 14578 | { |
39e7b4a4 | 14579 | pedwarn ("explicit instantiation of non-template %q#D", result); |
6686e84d | 14580 | return; |
14581 | } | |
14582 | ||
617abf06 | 14583 | if (storage == NULL_TREE) |
6495357a | 14584 | ; |
bb09dca5 | 14585 | else if (storage == ridpointers[(int) RID_EXTERN]) |
954885ed | 14586 | { |
a28038a1 | 14587 | if (pedantic && !in_system_header) |
39e7b4a4 | 14588 | pedwarn ("ISO C++ forbids the use of %<extern%> on explicit " |
653e5405 | 14589 | "instantiations"); |
954885ed | 14590 | extern_p = 1; |
14591 | } | |
617abf06 | 14592 | else |
39e7b4a4 | 14593 | error ("storage class %qD applied to template instantiation", storage); |
074ab442 | 14594 | |
9ed9d9ec | 14595 | check_explicit_instantiation_namespace (result); |
e857e9c7 | 14596 | mark_decl_instantiated (result, extern_p); |
1e28ccf1 | 14597 | if (! extern_p) |
074ab442 | 14598 | instantiate_decl (result, /*defer_ok=*/1, |
2806ee49 | 14599 | /*expl_inst_class_mem_p=*/false); |
b0722fac | 14600 | } |
14601 | ||
e75bce43 | 14602 | static void |
807be5b4 | 14603 | mark_class_instantiated (tree t, int extern_p) |
bb09dca5 | 14604 | { |
14605 | SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t); | |
d05d1f5c | 14606 | SET_CLASSTYPE_INTERFACE_KNOWN (t); |
14607 | CLASSTYPE_INTERFACE_ONLY (t) = extern_p; | |
bb09dca5 | 14608 | TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p; |
14609 | if (! extern_p) | |
14610 | { | |
14611 | CLASSTYPE_DEBUG_REQUESTED (t) = 1; | |
14612 | rest_of_type_compilation (t, 1); | |
14613 | } | |
9031d10b | 14614 | } |
3f7d79e4 | 14615 | |
af694375 | 14616 | /* Called from do_type_instantiation through binding_table_foreach to |
6beb3f76 | 14617 | do recursive instantiation for the type bound in ENTRY. */ |
af694375 | 14618 | static void |
14619 | bt_instantiate_type_proc (binding_entry entry, void *data) | |
14620 | { | |
14621 | tree storage = *(tree *) data; | |
14622 | ||
95397ff9 | 14623 | if (MAYBE_CLASS_TYPE_P (entry->type) |
af694375 | 14624 | && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type))) |
14625 | do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0); | |
14626 | } | |
14627 | ||
e4959831 | 14628 | /* Called from do_type_instantiation to instantiate a member |
14629 | (a member function or a static member variable) of an | |
93523877 | 14630 | explicitly instantiated class template. */ |
e4959831 | 14631 | static void |
14632 | instantiate_class_member (tree decl, int extern_p) | |
14633 | { | |
14634 | mark_decl_instantiated (decl, extern_p); | |
e4959831 | 14635 | if (! extern_p) |
074ab442 | 14636 | instantiate_decl (decl, /*defer_ok=*/1, |
2806ee49 | 14637 | /*expl_inst_class_mem_p=*/true); |
e4959831 | 14638 | } |
14639 | ||
761cd2c4 | 14640 | /* Perform an explicit instantiation of template class T. STORAGE, if |
14641 | non-null, is the RID for extern, inline or static. COMPLAIN is | |
3160db1d | 14642 | nonzero if this is called from the parser, zero if called recursively, |
761cd2c4 | 14643 | since the standard is unclear (as detailed below). */ |
9031d10b | 14644 | |
b0722fac | 14645 | void |
807be5b4 | 14646 | do_type_instantiation (tree t, tree storage, tsubst_flags_t complain) |
b0722fac | 14647 | { |
3f7d79e4 | 14648 | int extern_p = 0; |
14649 | int nomem_p = 0; | |
e857e9c7 | 14650 | int static_p = 0; |
2f9fc8ef | 14651 | int previous_instantiation_extern_p = 0; |
e857e9c7 | 14652 | |
839e6096 | 14653 | if (TREE_CODE (t) == TYPE_DECL) |
14654 | t = TREE_TYPE (t); | |
14655 | ||
e4e283ec | 14656 | if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t)) |
839e6096 | 14657 | { |
39e7b4a4 | 14658 | error ("explicit instantiation of non-template type %qT", t); |
839e6096 | 14659 | return; |
14660 | } | |
14661 | ||
e857e9c7 | 14662 | complete_type (t); |
b0722fac | 14663 | |
4b72716d | 14664 | if (!COMPLETE_TYPE_P (t)) |
617abf06 | 14665 | { |
4b471722 | 14666 | if (complain & tf_error) |
39e7b4a4 | 14667 | error ("explicit instantiation of %q#T before definition of template", |
653e5405 | 14668 | t); |
617abf06 | 14669 | return; |
14670 | } | |
14671 | ||
954885ed | 14672 | if (storage != NULL_TREE) |
617abf06 | 14673 | { |
a28038a1 | 14674 | if (pedantic && !in_system_header) |
9031d10b | 14675 | pedwarn("ISO C++ forbids the use of %qE on explicit instantiations", |
653e5405 | 14676 | storage); |
954885ed | 14677 | |
14678 | if (storage == ridpointers[(int) RID_INLINE]) | |
14679 | nomem_p = 1; | |
14680 | else if (storage == ridpointers[(int) RID_EXTERN]) | |
14681 | extern_p = 1; | |
14682 | else if (storage == ridpointers[(int) RID_STATIC]) | |
14683 | static_p = 1; | |
14684 | else | |
14685 | { | |
39e7b4a4 | 14686 | error ("storage class %qD applied to template instantiation", |
653e5405 | 14687 | storage); |
954885ed | 14688 | extern_p = 0; |
14689 | } | |
617abf06 | 14690 | } |
14691 | ||
3a8f9e39 | 14692 | if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t)) |
954885ed | 14693 | { |
a8057507 | 14694 | /* DR 259 [temp.spec]. |
f3ba5c6a | 14695 | |
a8057507 | 14696 | Both an explicit instantiation and a declaration of an explicit |
14697 | specialization shall not appear in a program unless the explicit | |
14698 | instantiation follows a declaration of the explicit specialization. | |
14699 | ||
14700 | For a given set of template parameters, if an explicit | |
14701 | instantiation of a template appears after a declaration of an | |
14702 | explicit specialization for that template, the explicit | |
14703 | instantiation has no effect. */ | |
954885ed | 14704 | return; |
14705 | } | |
14706 | else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t)) | |
dc76caae | 14707 | { |
954885ed | 14708 | /* [temp.spec] |
14709 | ||
14710 | No program shall explicitly instantiate any template more | |
9031d10b | 14711 | than once. |
954885ed | 14712 | |
653e5405 | 14713 | If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit |
2f9fc8ef | 14714 | instantiation was `extern'. If EXTERN_P then the second is. |
caa6fdce | 14715 | These cases are OK. */ |
2f9fc8ef | 14716 | previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t); |
14717 | ||
14718 | if (!previous_instantiation_extern_p && !extern_p | |
4b471722 | 14719 | && (complain & tf_error)) |
39e7b4a4 | 14720 | pedwarn ("duplicate explicit instantiation of %q#T", t); |
9031d10b | 14721 | |
954885ed | 14722 | /* If we've already instantiated the template, just return now. */ |
14723 | if (!CLASSTYPE_INTERFACE_ONLY (t)) | |
14724 | return; | |
dc76caae | 14725 | } |
3f7d79e4 | 14726 | |
9ed9d9ec | 14727 | check_explicit_instantiation_namespace (TYPE_NAME (t)); |
954885ed | 14728 | mark_class_instantiated (t, extern_p); |
954885ed | 14729 | |
3f7d79e4 | 14730 | if (nomem_p) |
14731 | return; | |
14732 | ||
b0722fac | 14733 | { |
c25194fd | 14734 | tree tmp; |
e857e9c7 | 14735 | |
954885ed | 14736 | /* In contrast to implicit instantiation, where only the |
14737 | declarations, and not the definitions, of members are | |
14738 | instantiated, we have here: | |
14739 | ||
653e5405 | 14740 | [temp.explicit] |
954885ed | 14741 | |
14742 | The explicit instantiation of a class template specialization | |
14743 | implies the instantiation of all of its members not | |
14744 | previously explicitly specialized in the translation unit | |
9031d10b | 14745 | containing the explicit instantiation. |
954885ed | 14746 | |
14747 | Of course, we can't instantiate member template classes, since | |
14748 | we don't have any arguments for them. Note that the standard | |
ec0eee8d | 14749 | is unclear on whether the instantiation of the members are |
e4959831 | 14750 | *explicit* instantiations or not. However, the most natural |
93523877 | 14751 | interpretation is that it should be an explicit instantiation. */ |
954885ed | 14752 | |
e857e9c7 | 14753 | if (! static_p) |
14754 | for (tmp = TYPE_METHODS (t); tmp; tmp = TREE_CHAIN (tmp)) | |
db173e97 | 14755 | if (TREE_CODE (tmp) == FUNCTION_DECL |
c8559ab6 | 14756 | && DECL_TEMPLATE_INSTANTIATION (tmp)) |
e4959831 | 14757 | instantiate_class_member (tmp, extern_p); |
e857e9c7 | 14758 | |
14759 | for (tmp = TYPE_FIELDS (t); tmp; tmp = TREE_CHAIN (tmp)) | |
14760 | if (TREE_CODE (tmp) == VAR_DECL && DECL_TEMPLATE_INSTANTIATION (tmp)) | |
e4959831 | 14761 | instantiate_class_member (tmp, extern_p); |
b0722fac | 14762 | |
af694375 | 14763 | if (CLASSTYPE_NESTED_UTDS (t)) |
14764 | binding_table_foreach (CLASSTYPE_NESTED_UTDS (t), | |
653e5405 | 14765 | bt_instantiate_type_proc, &storage); |
f3ba5c6a | 14766 | } |
471086d6 | 14767 | } |
e581f478 | 14768 | |
34197853 | 14769 | /* Given a function DECL, which is a specialization of TMPL, modify |
14770 | DECL to be a re-instantiation of TMPL with the same template | |
14771 | arguments. TMPL should be the template into which tsubst'ing | |
14772 | should occur for DECL, not the most general template. | |
32b5f03e | 14773 | |
14774 | One reason for doing this is a scenario like this: | |
14775 | ||
14776 | template <class T> | |
14777 | void f(const T&, int i); | |
14778 | ||
14779 | void g() { f(3, 7); } | |
14780 | ||
14781 | template <class T> | |
14782 | void f(const T& t, const int i) { } | |
14783 | ||
14784 | Note that when the template is first instantiated, with | |
14785 | instantiate_template, the resulting DECL will have no name for the | |
14786 | first parameter, and the wrong type for the second. So, when we go | |
14787 | to instantiate the DECL, we regenerate it. */ | |
14788 | ||
863c3f96 | 14789 | static void |
807be5b4 | 14790 | regenerate_decl_from_template (tree decl, tree tmpl) |
32b5f03e | 14791 | { |
a32fe883 | 14792 | /* The arguments used to instantiate DECL, from the most general |
14793 | template. */ | |
32b5f03e | 14794 | tree args; |
32b5f03e | 14795 | tree code_pattern; |
32b5f03e | 14796 | |
14797 | args = DECL_TI_ARGS (decl); | |
14798 | code_pattern = DECL_TEMPLATE_RESULT (tmpl); | |
14799 | ||
9014db85 | 14800 | /* Make sure that we can see identifiers, and compute access |
14801 | correctly. */ | |
14802 | push_access_scope (decl); | |
14803 | ||
38d89ee9 | 14804 | if (TREE_CODE (decl) == FUNCTION_DECL) |
14805 | { | |
14806 | tree decl_parm; | |
14807 | tree pattern_parm; | |
14808 | tree specs; | |
14809 | int args_depth; | |
14810 | int parms_depth; | |
9031d10b | 14811 | |
38d89ee9 | 14812 | args_depth = TMPL_ARGS_DEPTH (args); |
9031d10b | 14813 | parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)); |
38d89ee9 | 14814 | if (args_depth > parms_depth) |
14815 | args = get_innermost_template_args (args, parms_depth); | |
14816 | ||
14817 | specs = tsubst_exception_specification (TREE_TYPE (code_pattern), | |
14818 | args, tf_error, NULL_TREE); | |
14819 | if (specs) | |
14820 | TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl), | |
14821 | specs); | |
14822 | ||
14823 | /* Merge parameter declarations. */ | |
9031d10b | 14824 | decl_parm = skip_artificial_parms_for (decl, |
38d89ee9 | 14825 | DECL_ARGUMENTS (decl)); |
9031d10b | 14826 | pattern_parm |
38d89ee9 | 14827 | = skip_artificial_parms_for (code_pattern, |
14828 | DECL_ARGUMENTS (code_pattern)); | |
d95d815d | 14829 | while (decl_parm && !FUNCTION_PARAMETER_PACK_P (pattern_parm)) |
38d89ee9 | 14830 | { |
14831 | tree parm_type; | |
577cba38 | 14832 | tree attributes; |
d95d815d | 14833 | |
38d89ee9 | 14834 | if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm)) |
14835 | DECL_NAME (decl_parm) = DECL_NAME (pattern_parm); | |
14836 | parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error, | |
14837 | NULL_TREE); | |
9e9dded5 | 14838 | parm_type = type_decays_to (parm_type); |
38d89ee9 | 14839 | if (!same_type_p (TREE_TYPE (decl_parm), parm_type)) |
14840 | TREE_TYPE (decl_parm) = parm_type; | |
577cba38 | 14841 | attributes = DECL_ATTRIBUTES (pattern_parm); |
14842 | if (DECL_ATTRIBUTES (decl_parm) != attributes) | |
14843 | { | |
14844 | DECL_ATTRIBUTES (decl_parm) = attributes; | |
14845 | cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0); | |
14846 | } | |
38d89ee9 | 14847 | decl_parm = TREE_CHAIN (decl_parm); |
14848 | pattern_parm = TREE_CHAIN (pattern_parm); | |
14849 | } | |
d95d815d | 14850 | /* Merge any parameters that match with the function parameter |
14851 | pack. */ | |
14852 | if (pattern_parm && FUNCTION_PARAMETER_PACK_P (pattern_parm)) | |
14853 | { | |
14854 | int i, len; | |
14855 | tree expanded_types; | |
14856 | /* Expand the TYPE_PACK_EXPANSION that provides the types for | |
14857 | the parameters in this function parameter pack. */ | |
14858 | expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm), | |
14859 | args, tf_error, NULL_TREE); | |
14860 | len = TREE_VEC_LENGTH (expanded_types); | |
14861 | for (i = 0; i < len; i++) | |
14862 | { | |
14863 | tree parm_type; | |
14864 | tree attributes; | |
14865 | ||
14866 | if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm)) | |
14867 | /* Rename the parameter to include the index. */ | |
14868 | DECL_NAME (decl_parm) = | |
14869 | make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i); | |
14870 | parm_type = TREE_VEC_ELT (expanded_types, i); | |
14871 | parm_type = type_decays_to (parm_type); | |
14872 | if (!same_type_p (TREE_TYPE (decl_parm), parm_type)) | |
14873 | TREE_TYPE (decl_parm) = parm_type; | |
14874 | attributes = DECL_ATTRIBUTES (pattern_parm); | |
14875 | if (DECL_ATTRIBUTES (decl_parm) != attributes) | |
14876 | { | |
14877 | DECL_ATTRIBUTES (decl_parm) = attributes; | |
14878 | cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0); | |
14879 | } | |
14880 | decl_parm = TREE_CHAIN (decl_parm); | |
14881 | } | |
14882 | } | |
38d89ee9 | 14883 | /* Merge additional specifiers from the CODE_PATTERN. */ |
14884 | if (DECL_DECLARED_INLINE_P (code_pattern) | |
14885 | && !DECL_DECLARED_INLINE_P (decl)) | |
14886 | DECL_DECLARED_INLINE_P (decl) = 1; | |
14887 | if (DECL_INLINE (code_pattern) && !DECL_INLINE (decl)) | |
14888 | DECL_INLINE (decl) = 1; | |
14889 | } | |
14890 | else if (TREE_CODE (decl) == VAR_DECL) | |
e6ef0e42 | 14891 | DECL_INITIAL (decl) = |
14892 | tsubst_expr (DECL_INITIAL (code_pattern), args, | |
bde9ebf7 | 14893 | tf_error, DECL_TI_TEMPLATE (decl), |
14894 | /*integral_constant_expression_p=*/false); | |
38d89ee9 | 14895 | else |
14896 | gcc_unreachable (); | |
34197853 | 14897 | |
7a468236 | 14898 | pop_access_scope (decl); |
32b5f03e | 14899 | } |
14900 | ||
0a3b29ad | 14901 | /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be |
14902 | substituted to get DECL. */ | |
14903 | ||
3c5d9204 | 14904 | tree |
0a3b29ad | 14905 | template_for_substitution (tree decl) |
14906 | { | |
14907 | tree tmpl = DECL_TI_TEMPLATE (decl); | |
14908 | ||
14909 | /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern | |
14910 | for the instantiation. This is not always the most general | |
14911 | template. Consider, for example: | |
14912 | ||
653e5405 | 14913 | template <class T> |
0a3b29ad | 14914 | struct S { template <class U> void f(); |
653e5405 | 14915 | template <> void f<int>(); }; |
0a3b29ad | 14916 | |
14917 | and an instantiation of S<double>::f<int>. We want TD to be the | |
14918 | specialization S<T>::f<int>, not the more general S<T>::f<U>. */ | |
14919 | while (/* An instantiation cannot have a definition, so we need a | |
14920 | more general template. */ | |
14921 | DECL_TEMPLATE_INSTANTIATION (tmpl) | |
14922 | /* We must also deal with friend templates. Given: | |
14923 | ||
9031d10b | 14924 | template <class T> struct S { |
0a3b29ad | 14925 | template <class U> friend void f() {}; |
14926 | }; | |
14927 | ||
14928 | S<int>::f<U> say, is not an instantiation of S<T>::f<U>, | |
14929 | so far as the language is concerned, but that's still | |
14930 | where we get the pattern for the instantiation from. On | |
14931 | other hand, if the definition comes outside the class, say: | |
14932 | ||
9031d10b | 14933 | template <class T> struct S { |
0a3b29ad | 14934 | template <class U> friend void f(); |
14935 | }; | |
14936 | template <class U> friend void f() {} | |
14937 | ||
14938 | we don't need to look any further. That's what the check for | |
14939 | DECL_INITIAL is for. */ | |
14940 | || (TREE_CODE (decl) == FUNCTION_DECL | |
14941 | && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl) | |
14942 | && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl)))) | |
14943 | { | |
14944 | /* The present template, TD, should not be a definition. If it | |
14945 | were a definition, we should be using it! Note that we | |
14946 | cannot restructure the loop to just keep going until we find | |
14947 | a template with a definition, since that might go too far if | |
14948 | a specialization was declared, but not defined. */ | |
b4df430b | 14949 | gcc_assert (TREE_CODE (decl) != VAR_DECL |
14950 | || DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (tmpl))); | |
9031d10b | 14951 | |
0a3b29ad | 14952 | /* Fetch the more general template. */ |
14953 | tmpl = DECL_TI_TEMPLATE (tmpl); | |
14954 | } | |
14955 | ||
14956 | return tmpl; | |
14957 | } | |
14958 | ||
a35958af | 14959 | /* Produce the definition of D, a _DECL generated from a template. If |
3160db1d | 14960 | DEFER_OK is nonzero, then we don't have to actually do the |
e4959831 | 14961 | instantiation now; we just have to do it sometime. Normally it is |
14962 | an error if this is an explicit instantiation but D is undefined. | |
2806ee49 | 14963 | EXPL_INST_CLASS_MEM_P is true iff D is a member of an |
14964 | explicitly instantiated class template. */ | |
f3110581 | 14965 | |
e581f478 | 14966 | tree |
074ab442 | 14967 | instantiate_decl (tree d, int defer_ok, |
2806ee49 | 14968 | bool expl_inst_class_mem_p) |
e581f478 | 14969 | { |
34197853 | 14970 | tree tmpl = DECL_TI_TEMPLATE (d); |
7466d33f | 14971 | tree gen_args; |
14972 | tree args; | |
32b5f03e | 14973 | tree td; |
34197853 | 14974 | tree code_pattern; |
14975 | tree spec; | |
14976 | tree gen_tmpl; | |
e6ef0e42 | 14977 | bool pattern_defined; |
170e6e3a | 14978 | int need_push; |
6a86d77b | 14979 | location_t saved_loc = input_location; |
fb1f9e09 | 14980 | int saved_in_system_header = in_system_header; |
1f9378db | 14981 | bool external_p; |
9031d10b | 14982 | |
34197853 | 14983 | /* This function should only be used to instantiate templates for |
14984 | functions and static member variables. */ | |
b4df430b | 14985 | gcc_assert (TREE_CODE (d) == FUNCTION_DECL |
14986 | || TREE_CODE (d) == VAR_DECL); | |
34197853 | 14987 | |
cd4dc755 | 14988 | /* Variables are never deferred; if instantiation is required, they |
14989 | are instantiated right away. That allows for better code in the | |
14990 | case that an expression refers to the value of the variable -- | |
14991 | if the variable has a constant value the referring expression can | |
14992 | take advantage of that fact. */ | |
14993 | if (TREE_CODE (d) == VAR_DECL) | |
14994 | defer_ok = 0; | |
14995 | ||
e55cba4c | 14996 | /* Don't instantiate cloned functions. Instead, instantiate the |
14997 | functions they cloned. */ | |
14998 | if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d)) | |
14999 | d = DECL_CLONED_FUNCTION (d); | |
15000 | ||
4012c20a | 15001 | if (DECL_TEMPLATE_INSTANTIATED (d)) |
954885ed | 15002 | /* D has already been instantiated. It might seem reasonable to |
ec0eee8d | 15003 | check whether or not D is an explicit instantiation, and, if so, |
954885ed | 15004 | stop here. But when an explicit instantiation is deferred |
15005 | until the end of the compilation, DECL_EXPLICIT_INSTANTIATION | |
15006 | is set, even though we still need to do the instantiation. */ | |
34197853 | 15007 | return d; |
15008 | ||
15009 | /* If we already have a specialization of this declaration, then | |
15010 | there's no reason to instantiate it. Note that | |
15011 | retrieve_specialization gives us both instantiations and | |
15012 | specializations, so we must explicitly check | |
15013 | DECL_TEMPLATE_SPECIALIZATION. */ | |
15014 | gen_tmpl = most_general_template (tmpl); | |
7466d33f | 15015 | gen_args = DECL_TI_ARGS (d); |
38d89ee9 | 15016 | spec = retrieve_specialization (gen_tmpl, gen_args, |
15017 | /*class_specializations_p=*/false); | |
34197853 | 15018 | if (spec != NULL_TREE && DECL_TEMPLATE_SPECIALIZATION (spec)) |
15019 | return spec; | |
15020 | ||
15021 | /* This needs to happen before any tsubsting. */ | |
15022 | if (! push_tinst_level (d)) | |
15023 | return d; | |
15024 | ||
3a37334e | 15025 | timevar_push (TV_PARSE); |
15026 | ||
a072266a | 15027 | /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern |
0a3b29ad | 15028 | for the instantiation. */ |
15029 | td = template_for_substitution (d); | |
c4b7de4c | 15030 | code_pattern = DECL_TEMPLATE_RESULT (td); |
a7ab4ece | 15031 | |
5c028b29 | 15032 | /* We should never be trying to instantiate a member of a class |
074ab442 | 15033 | template or partial specialization. */ |
5c028b29 | 15034 | gcc_assert (d != code_pattern); |
074ab442 | 15035 | |
0995757f | 15036 | if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d)) |
15037 | || DECL_TEMPLATE_SPECIALIZATION (td)) | |
7b827e01 | 15038 | /* In the case of a friend template whose definition is provided |
15039 | outside the class, we may have too many arguments. Drop the | |
0995757f | 15040 | ones we don't need. The same is true for specializations. */ |
7b827e01 | 15041 | args = get_innermost_template_args |
15042 | (gen_args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td))); | |
15043 | else | |
15044 | args = gen_args; | |
7466d33f | 15045 | |
e857e9c7 | 15046 | if (TREE_CODE (d) == FUNCTION_DECL) |
bfdf1186 | 15047 | pattern_defined = (DECL_SAVED_TREE (code_pattern) != NULL_TREE); |
e857e9c7 | 15048 | else |
34197853 | 15049 | pattern_defined = ! DECL_IN_AGGR_P (code_pattern); |
e6ef0e42 | 15050 | |
15051 | /* We may be in the middle of deferred access check. Disable it now. */ | |
15052 | push_deferring_access_checks (dk_no_deferred); | |
15053 | ||
caa6fdce | 15054 | /* Unless an explicit instantiation directive has already determined |
15055 | the linkage of D, remember that a definition is available for | |
15056 | this entity. */ | |
9031d10b | 15057 | if (pattern_defined |
caa6fdce | 15058 | && !DECL_INTERFACE_KNOWN (d) |
15059 | && !DECL_NOT_REALLY_EXTERN (d)) | |
15060 | mark_definable (d); | |
1adc02a5 | 15061 | |
346064d9 | 15062 | input_location = DECL_SOURCE_LOCATION (d); |
fb1f9e09 | 15063 | in_system_header = DECL_IN_SYSTEM_HEADER (d); |
1adc02a5 | 15064 | |
2806ee49 | 15065 | /* If D is a member of an explicitly instantiated class template, |
15066 | and no definition is available, treat it like an implicit | |
074ab442 | 15067 | instantiation. */ |
15068 | if (!pattern_defined && expl_inst_class_mem_p | |
15069 | && DECL_EXPLICIT_INSTANTIATION (d)) | |
e857e9c7 | 15070 | { |
caa6fdce | 15071 | DECL_NOT_REALLY_EXTERN (d) = 0; |
2806ee49 | 15072 | DECL_INTERFACE_KNOWN (d) = 0; |
caa6fdce | 15073 | SET_DECL_IMPLICIT_INSTANTIATION (d); |
e857e9c7 | 15074 | } |
15075 | ||
9edbac04 | 15076 | if (!defer_ok) |
15077 | { | |
15078 | /* Recheck the substitutions to obtain any warning messages | |
15079 | about ignoring cv qualifiers. */ | |
15080 | tree gen = DECL_TEMPLATE_RESULT (gen_tmpl); | |
15081 | tree type = TREE_TYPE (gen); | |
15082 | ||
d91f6775 | 15083 | /* Make sure that we can see identifiers, and compute access |
7a468236 | 15084 | correctly. D is already the target FUNCTION_DECL with the |
15085 | right context. */ | |
15086 | push_access_scope (d); | |
d91f6775 | 15087 | |
9edbac04 | 15088 | if (TREE_CODE (gen) == FUNCTION_DECL) |
15089 | { | |
0fbca5e8 | 15090 | tsubst (DECL_ARGUMENTS (gen), gen_args, tf_warning_or_error, d); |
d95d815d | 15091 | tsubst_exception_specification (type, gen_args, tf_warning_or_error, |
15092 | d); | |
9edbac04 | 15093 | /* Don't simply tsubst the function type, as that will give |
15094 | duplicate warnings about poor parameter qualifications. | |
15095 | The function arguments are the same as the decl_arguments | |
47cd6605 | 15096 | without the top level cv qualifiers. */ |
9edbac04 | 15097 | type = TREE_TYPE (type); |
15098 | } | |
0fbca5e8 | 15099 | tsubst (type, gen_args, tf_warning_or_error, d); |
d91f6775 | 15100 | |
7a468236 | 15101 | pop_access_scope (d); |
9edbac04 | 15102 | } |
9031d10b | 15103 | |
1f9378db | 15104 | /* Check to see whether we know that this template will be |
15105 | instantiated in some other file, as with "extern template" | |
15106 | extension. */ | |
15107 | external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d)); | |
15108 | /* In general, we do not instantiate such templates... */ | |
15109 | if (external_p | |
15110 | /* ... but we instantiate inline functions so that we can inline | |
15111 | them and ... */ | |
15112 | && ! (TREE_CODE (d) == FUNCTION_DECL && DECL_INLINE (d)) | |
15113 | /* ... we instantiate static data members whose values are | |
15114 | needed in integral constant expressions. */ | |
074ab442 | 15115 | && ! (TREE_CODE (d) == VAR_DECL |
1f9378db | 15116 | && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (d))) |
0d2ee68b | 15117 | goto out; |
a35958af | 15118 | /* Defer all other templates, unless we have been explicitly |
1f9378db | 15119 | forbidden from doing so. */ |
15120 | if (/* If there is no definition, we cannot instantiate the | |
15121 | template. */ | |
074ab442 | 15122 | ! pattern_defined |
1f9378db | 15123 | /* If it's OK to postpone instantiation, do so. */ |
15124 | || defer_ok | |
15125 | /* If this is a static data member that will be defined | |
15126 | elsewhere, we don't want to instantiate the entire data | |
15127 | member, but we do want to instantiate the initializer so that | |
15128 | we can substitute that elsewhere. */ | |
15129 | || (external_p && TREE_CODE (d) == VAR_DECL)) | |
a35958af | 15130 | { |
e6ef0e42 | 15131 | /* The definition of the static data member is now required so |
15132 | we must substitute the initializer. */ | |
15133 | if (TREE_CODE (d) == VAR_DECL | |
074ab442 | 15134 | && !DECL_INITIAL (d) |
e6ef0e42 | 15135 | && DECL_INITIAL (code_pattern)) |
15136 | { | |
6d8c84aa | 15137 | tree ns; |
15138 | tree init; | |
15139 | ||
15140 | ns = decl_namespace_context (d); | |
2590c937 | 15141 | push_nested_namespace (ns); |
e6ef0e42 | 15142 | push_nested_class (DECL_CONTEXT (d)); |
074ab442 | 15143 | init = tsubst_expr (DECL_INITIAL (code_pattern), |
6d8c84aa | 15144 | args, |
bde9ebf7 | 15145 | tf_warning_or_error, NULL_TREE, |
15146 | /*integral_constant_expression_p=*/false); | |
d91303a6 | 15147 | cp_finish_decl (d, init, /*init_const_expr_p=*/false, |
15148 | /*asmspec_tree=*/NULL_TREE, | |
0fe80e90 | 15149 | LOOKUP_ONLYCONVERTING); |
e6ef0e42 | 15150 | pop_nested_class (); |
2590c937 | 15151 | pop_nested_namespace (ns); |
e6ef0e42 | 15152 | } |
15153 | ||
1f9378db | 15154 | /* We restore the source position here because it's used by |
15155 | add_pending_template. */ | |
6a86d77b | 15156 | input_location = saved_loc; |
2eda5fcb | 15157 | |
9031d10b | 15158 | if (at_eof && !pattern_defined |
954885ed | 15159 | && DECL_EXPLICIT_INSTANTIATION (d)) |
15160 | /* [temp.explicit] | |
15161 | ||
15162 | The definition of a non-exported function template, a | |
15163 | non-exported member function template, or a non-exported | |
15164 | member function or static data member of a class template | |
15165 | shall be present in every translation unit in which it is | |
15166 | explicitly instantiated. */ | |
cf103c6c | 15167 | pedwarn |
39e7b4a4 | 15168 | ("explicit instantiation of %qD but no definition available", d); |
954885ed | 15169 | |
1f9378db | 15170 | /* ??? Historically, we have instantiated inline functions, even |
15171 | when marked as "extern template". */ | |
15172 | if (!(external_p && TREE_CODE (d) == VAR_DECL)) | |
15173 | add_pending_template (d); | |
1adc02a5 | 15174 | goto out; |
e857e9c7 | 15175 | } |
caa6fdce | 15176 | /* Tell the repository that D is available in this translation unit |
15177 | -- and see if it is supposed to be instantiated here. */ | |
15178 | if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d)) | |
15179 | { | |
15180 | /* In a PCH file, despite the fact that the repository hasn't | |
15181 | requested instantiation in the PCH it is still possible that | |
15182 | an instantiation will be required in a file that includes the | |
15183 | PCH. */ | |
15184 | if (pch_file) | |
15185 | add_pending_template (d); | |
15186 | /* Instantiate inline functions so that the inliner can do its | |
15187 | job, even though we'll not be emitting a copy of this | |
15188 | function. */ | |
837f1ad9 | 15189 | if (!(TREE_CODE (d) == FUNCTION_DECL |
9031d10b | 15190 | && flag_inline_trees |
837f1ad9 | 15191 | && DECL_DECLARED_INLINE_P (d))) |
caa6fdce | 15192 | goto out; |
15193 | } | |
e857e9c7 | 15194 | |
4ee9c684 | 15195 | need_push = !cfun || !global_bindings_p (); |
170e6e3a | 15196 | if (need_push) |
15197 | push_to_top_level (); | |
ca9f5265 | 15198 | |
9b08685a | 15199 | /* Mark D as instantiated so that recursive calls to |
15200 | instantiate_decl do not try to instantiate it again. */ | |
15201 | DECL_TEMPLATE_INSTANTIATED (d) = 1; | |
15202 | ||
2699dcee | 15203 | /* Regenerate the declaration in case the template has been modified |
15204 | by a subsequent redeclaration. */ | |
15205 | regenerate_decl_from_template (d, td); | |
caa6fdce | 15206 | |
4e83b7e0 | 15207 | /* We already set the file and line above. Reset them now in case |
4ee9c684 | 15208 | they changed as a result of calling regenerate_decl_from_template. */ |
346064d9 | 15209 | input_location = DECL_SOURCE_LOCATION (d); |
3cc0b4b9 | 15210 | |
e857e9c7 | 15211 | if (TREE_CODE (d) == VAR_DECL) |
15212 | { | |
788f3e3e | 15213 | tree init; |
15214 | ||
f6a31c6f | 15215 | /* Clear out DECL_RTL; whatever was there before may not be right |
15216 | since we've reset the type of the declaration. */ | |
15217 | SET_DECL_RTL (d, NULL_RTX); | |
e857e9c7 | 15218 | DECL_IN_AGGR_P (d) = 0; |
b8f254f0 | 15219 | |
788f3e3e | 15220 | /* The initializer is placed in DECL_INITIAL by |
15221 | regenerate_decl_from_template. Pull it out so that | |
15222 | finish_decl can process it. */ | |
15223 | init = DECL_INITIAL (d); | |
15224 | DECL_INITIAL (d) = NULL_TREE; | |
15225 | DECL_INITIALIZED_P (d) = 0; | |
15226 | ||
caa6fdce | 15227 | /* Clear DECL_EXTERNAL so that cp_finish_decl will process the |
15228 | initializer. That function will defer actual emission until | |
15229 | we have a chance to determine linkage. */ | |
15230 | DECL_EXTERNAL (d) = 0; | |
15231 | ||
91caa6ca | 15232 | /* Enter the scope of D so that access-checking works correctly. */ |
caa6fdce | 15233 | push_nested_class (DECL_CONTEXT (d)); |
788f3e3e | 15234 | finish_decl (d, init, NULL_TREE); |
91caa6ca | 15235 | pop_nested_class (); |
e857e9c7 | 15236 | } |
15237 | else if (TREE_CODE (d) == FUNCTION_DECL) | |
15238 | { | |
c87435c8 | 15239 | htab_t saved_local_specializations; |
0a3b29ad | 15240 | tree subst_decl; |
15241 | tree tmpl_parm; | |
15242 | tree spec_parm; | |
c87435c8 | 15243 | |
15244 | /* Save away the current list, in case we are instantiating one | |
15245 | template from within the body of another. */ | |
15246 | saved_local_specializations = local_specializations; | |
15247 | ||
db1ad296 | 15248 | /* Set up the list of local specializations. */ |
9031d10b | 15249 | local_specializations = htab_create (37, |
cfa03699 | 15250 | hash_local_specialization, |
0a3b29ad | 15251 | eq_local_specializations, |
db1ad296 | 15252 | NULL); |
15253 | ||
019cf886 | 15254 | /* Set up context. */ |
3046c0a3 | 15255 | start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED); |
e857e9c7 | 15256 | |
0a3b29ad | 15257 | /* Create substitution entries for the parameters. */ |
15258 | subst_decl = DECL_TEMPLATE_RESULT (template_for_substitution (d)); | |
15259 | tmpl_parm = DECL_ARGUMENTS (subst_decl); | |
15260 | spec_parm = DECL_ARGUMENTS (d); | |
15261 | if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d)) | |
15262 | { | |
15263 | register_local_specialization (spec_parm, tmpl_parm); | |
15264 | spec_parm = skip_artificial_parms_for (d, spec_parm); | |
15265 | tmpl_parm = skip_artificial_parms_for (subst_decl, tmpl_parm); | |
15266 | } | |
d95d815d | 15267 | while (tmpl_parm && !FUNCTION_PARAMETER_PACK_P (tmpl_parm)) |
0a3b29ad | 15268 | { |
15269 | register_local_specialization (spec_parm, tmpl_parm); | |
15270 | tmpl_parm = TREE_CHAIN (tmpl_parm); | |
15271 | spec_parm = TREE_CHAIN (spec_parm); | |
15272 | } | |
d95d815d | 15273 | if (tmpl_parm && FUNCTION_PARAMETER_PACK_P (tmpl_parm)) |
15274 | { | |
15275 | /* Collect all of the extra "packed" parameters into an | |
15276 | argument pack. */ | |
15277 | tree parmvec; | |
15278 | tree parmtypevec; | |
15279 | tree argpack = make_node (NONTYPE_ARGUMENT_PACK); | |
15280 | tree argtypepack = make_node (TYPE_ARGUMENT_PACK); | |
15281 | int i, len = 0; | |
15282 | tree t; | |
15283 | ||
15284 | /* Count how many parameters remain. */ | |
15285 | for (t = spec_parm; t; t = TREE_CHAIN (t)) | |
15286 | len++; | |
15287 | ||
15288 | /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */ | |
15289 | parmvec = make_tree_vec (len); | |
15290 | parmtypevec = make_tree_vec (len); | |
15291 | for(i = 0; i < len; i++, spec_parm = TREE_CHAIN (spec_parm)) | |
15292 | { | |
15293 | TREE_VEC_ELT (parmvec, i) = spec_parm; | |
15294 | TREE_VEC_ELT (parmtypevec, i) = TREE_TYPE (spec_parm); | |
15295 | } | |
15296 | ||
15297 | /* Build the argument packs. */ | |
15298 | SET_ARGUMENT_PACK_ARGS (argpack, parmvec); | |
15299 | SET_ARGUMENT_PACK_ARGS (argtypepack, parmtypevec); | |
15300 | TREE_TYPE (argpack) = argtypepack; | |
15301 | ||
15302 | /* Register the (value) argument pack as a specialization of | |
15303 | TMPL_PARM, then move on. */ | |
15304 | register_local_specialization (argpack, tmpl_parm); | |
15305 | tmpl_parm = TREE_CHAIN (tmpl_parm); | |
15306 | } | |
b4df430b | 15307 | gcc_assert (!spec_parm); |
0a3b29ad | 15308 | |
019cf886 | 15309 | /* Substitute into the body of the function. */ |
15310 | tsubst_expr (DECL_SAVED_TREE (code_pattern), args, | |
bde9ebf7 | 15311 | tf_warning_or_error, tmpl, |
15312 | /*integral_constant_expression_p=*/false); | |
019cf886 | 15313 | |
df464291 | 15314 | /* Set the current input_location to the end of the function |
15315 | so that finish_function knows where we are. */ | |
15316 | input_location = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus; | |
15317 | ||
db1ad296 | 15318 | /* We don't need the local specializations any more. */ |
15319 | htab_delete (local_specializations); | |
c87435c8 | 15320 | local_specializations = saved_local_specializations; |
db1ad296 | 15321 | |
07d01fbf | 15322 | /* Finish the function. */ |
95cedffb | 15323 | d = finish_function (0); |
6cb758f0 | 15324 | expand_or_defer_fn (d); |
e857e9c7 | 15325 | } |
15326 | ||
37794701 | 15327 | /* We're not deferring instantiation any more. */ |
15328 | TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0; | |
15329 | ||
170e6e3a | 15330 | if (need_push) |
15331 | pop_from_top_level (); | |
ca9f5265 | 15332 | |
1adc02a5 | 15333 | out: |
6a86d77b | 15334 | input_location = saved_loc; |
fb1f9e09 | 15335 | in_system_header = saved_in_system_header; |
d0693a37 | 15336 | pop_deferring_access_checks (); |
e857e9c7 | 15337 | pop_tinst_level (); |
e581f478 | 15338 | |
3a37334e | 15339 | timevar_pop (TV_PARSE); |
15340 | ||
e581f478 | 15341 | return d; |
15342 | } | |
e857e9c7 | 15343 | |
cec1f6a6 | 15344 | /* Run through the list of templates that we wish we could |
68efb5b8 | 15345 | instantiate, and instantiate any we can. RETRIES is the |
15346 | number of times we retry pending template instantiation. */ | |
cec1f6a6 | 15347 | |
68efb5b8 | 15348 | void |
15349 | instantiate_pending_templates (int retries) | |
cec1f6a6 | 15350 | { |
cec1f6a6 | 15351 | int reconsider; |
76c7f39d | 15352 | location_t saved_loc = input_location; |
8f3b5fc9 | 15353 | int saved_in_system_header = in_system_header; |
68efb5b8 | 15354 | |
15355 | /* Instantiating templates may trigger vtable generation. This in turn | |
15356 | may require further template instantiations. We place a limit here | |
15357 | to avoid infinite loop. */ | |
15358 | if (pending_templates && retries >= max_tinst_depth) | |
15359 | { | |
c5dd8e06 | 15360 | tree decl = pending_templates->tinst->decl; |
7946223e | 15361 | |
3cf8b391 | 15362 | error ("template instantiation depth exceeds maximum of %d" |
7946223e | 15363 | " instantiating %q+D, possibly from virtual table generation" |
15364 | " (use -ftemplate-depth-NN to increase the maximum)", | |
15365 | max_tinst_depth, decl); | |
15366 | if (TREE_CODE (decl) == FUNCTION_DECL) | |
15367 | /* Pretend that we defined it. */ | |
15368 | DECL_INITIAL (decl) = error_mark_node; | |
68efb5b8 | 15369 | return; |
15370 | } | |
15371 | ||
9031d10b | 15372 | do |
cec1f6a6 | 15373 | { |
c5dd8e06 | 15374 | struct pending_template **t = &pending_templates; |
15375 | struct pending_template *last = NULL; | |
cec1f6a6 | 15376 | reconsider = 0; |
cec1f6a6 | 15377 | while (*t) |
15378 | { | |
c5dd8e06 | 15379 | tree instantiation = reopen_tinst_level ((*t)->tinst); |
15380 | bool complete = false; | |
cec1f6a6 | 15381 | |
9308e976 | 15382 | if (TYPE_P (instantiation)) |
cec1f6a6 | 15383 | { |
15384 | tree fn; | |
15385 | ||
4b72716d | 15386 | if (!COMPLETE_TYPE_P (instantiation)) |
cec1f6a6 | 15387 | { |
15388 | instantiate_class_template (instantiation); | |
15389 | if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation)) | |
9031d10b | 15390 | for (fn = TYPE_METHODS (instantiation); |
cec1f6a6 | 15391 | fn; |
15392 | fn = TREE_CHAIN (fn)) | |
15393 | if (! DECL_ARTIFICIAL (fn)) | |
074ab442 | 15394 | instantiate_decl (fn, |
2806ee49 | 15395 | /*defer_ok=*/0, |
15396 | /*expl_inst_class_mem_p=*/false); | |
4b72716d | 15397 | if (COMPLETE_TYPE_P (instantiation)) |
68efb5b8 | 15398 | reconsider = 1; |
cec1f6a6 | 15399 | } |
15400 | ||
c5dd8e06 | 15401 | complete = COMPLETE_TYPE_P (instantiation); |
cec1f6a6 | 15402 | } |
15403 | else | |
15404 | { | |
a35958af | 15405 | if (!DECL_TEMPLATE_SPECIALIZATION (instantiation) |
cec1f6a6 | 15406 | && !DECL_TEMPLATE_INSTANTIATED (instantiation)) |
15407 | { | |
074ab442 | 15408 | instantiation |
2806ee49 | 15409 | = instantiate_decl (instantiation, |
15410 | /*defer_ok=*/0, | |
15411 | /*expl_inst_class_mem_p=*/false); | |
cec1f6a6 | 15412 | if (DECL_TEMPLATE_INSTANTIATED (instantiation)) |
68efb5b8 | 15413 | reconsider = 1; |
cec1f6a6 | 15414 | } |
15415 | ||
c5dd8e06 | 15416 | complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation) |
15417 | || DECL_TEMPLATE_INSTANTIATED (instantiation)); | |
15418 | } | |
15419 | ||
15420 | if (complete) | |
15421 | /* If INSTANTIATION has been instantiated, then we don't | |
15422 | need to consider it again in the future. */ | |
15423 | *t = (*t)->next; | |
15424 | else | |
15425 | { | |
15426 | last = *t; | |
15427 | t = &(*t)->next; | |
cec1f6a6 | 15428 | } |
1120283b | 15429 | tinst_depth = 0; |
c5dd8e06 | 15430 | current_tinst_level = NULL; |
cec1f6a6 | 15431 | } |
b53db2b0 | 15432 | last_pending_template = last; |
9031d10b | 15433 | } |
cec1f6a6 | 15434 | while (reconsider); |
15435 | ||
76c7f39d | 15436 | input_location = saved_loc; |
8f3b5fc9 | 15437 | in_system_header = saved_in_system_header; |
cec1f6a6 | 15438 | } |
15439 | ||
bc577f39 | 15440 | /* Substitute ARGVEC into T, which is a list of initializers for |
15441 | either base class or a non-static data member. The TREE_PURPOSEs | |
15442 | are DECLs, and the TREE_VALUEs are the initializer values. Used by | |
15443 | instantiate_decl. */ | |
6d158f4d | 15444 | |
02d7f858 | 15445 | static tree |
807be5b4 | 15446 | tsubst_initializer_list (tree t, tree argvec) |
e857e9c7 | 15447 | { |
6507cda8 | 15448 | tree inits = NULL_TREE; |
e857e9c7 | 15449 | |
15450 | for (; t; t = TREE_CHAIN (t)) | |
15451 | { | |
bc577f39 | 15452 | tree decl; |
15453 | tree init; | |
d95d815d | 15454 | tree expanded_bases = NULL_TREE; |
15455 | tree expanded_arguments = NULL_TREE; | |
15456 | int i, len = 1; | |
bc577f39 | 15457 | |
d95d815d | 15458 | if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION) |
15459 | { | |
15460 | tree expr; | |
15461 | tree arg; | |
15462 | ||
15463 | /* Expand the base class expansion type into separate base | |
15464 | classes. */ | |
15465 | expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec, | |
15466 | tf_warning_or_error, | |
15467 | NULL_TREE); | |
15468 | if (expanded_bases == error_mark_node) | |
15469 | continue; | |
15470 | ||
15471 | /* We'll be building separate TREE_LISTs of arguments for | |
15472 | each base. */ | |
15473 | len = TREE_VEC_LENGTH (expanded_bases); | |
15474 | expanded_arguments = make_tree_vec (len); | |
15475 | for (i = 0; i < len; i++) | |
15476 | TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE; | |
15477 | ||
15478 | /* Build a dummy EXPR_PACK_EXPANSION that will be used to | |
15479 | expand each argument in the TREE_VALUE of t. */ | |
15480 | expr = make_node (EXPR_PACK_EXPANSION); | |
15481 | PACK_EXPANSION_PARAMETER_PACKS (expr) = | |
15482 | PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t)); | |
15483 | ||
e729f00e | 15484 | if (TREE_VALUE (t) == void_type_node) |
15485 | /* VOID_TYPE_NODE is used to indicate | |
15486 | value-initialization. */ | |
15487 | { | |
15488 | for (i = 0; i < len; i++) | |
15489 | TREE_VEC_ELT (expanded_arguments, i) = void_type_node; | |
15490 | } | |
15491 | else | |
15492 | { | |
15493 | /* Substitute parameter packs into each argument in the | |
15494 | TREE_LIST. */ | |
15495 | in_base_initializer = 1; | |
15496 | for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg)) | |
15497 | { | |
15498 | tree expanded_exprs; | |
15499 | ||
15500 | /* Expand the argument. */ | |
15501 | SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg)); | |
15502 | expanded_exprs | |
15503 | = tsubst_pack_expansion (expr, argvec, | |
15504 | tf_warning_or_error, | |
15505 | NULL_TREE); | |
f7531ee0 | 15506 | if (expanded_exprs == error_mark_node) |
15507 | continue; | |
e729f00e | 15508 | |
15509 | /* Prepend each of the expanded expressions to the | |
15510 | corresponding TREE_LIST in EXPANDED_ARGUMENTS. */ | |
15511 | for (i = 0; i < len; i++) | |
15512 | { | |
15513 | TREE_VEC_ELT (expanded_arguments, i) = | |
15514 | tree_cons (NULL_TREE, | |
15515 | TREE_VEC_ELT (expanded_exprs, i), | |
15516 | TREE_VEC_ELT (expanded_arguments, i)); | |
15517 | } | |
15518 | } | |
15519 | in_base_initializer = 0; | |
d95d815d | 15520 | |
e729f00e | 15521 | /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS, |
15522 | since we built them backwards. */ | |
15523 | for (i = 0; i < len; i++) | |
15524 | { | |
15525 | TREE_VEC_ELT (expanded_arguments, i) = | |
15526 | nreverse (TREE_VEC_ELT (expanded_arguments, i)); | |
15527 | } | |
15528 | } | |
d95d815d | 15529 | } |
5f1653d2 | 15530 | |
d95d815d | 15531 | for (i = 0; i < len; ++i) |
15532 | { | |
15533 | if (expanded_bases) | |
15534 | { | |
15535 | decl = TREE_VEC_ELT (expanded_bases, i); | |
15536 | decl = expand_member_init (decl); | |
15537 | init = TREE_VEC_ELT (expanded_arguments, i); | |
15538 | } | |
15539 | else | |
15540 | { | |
15541 | decl = tsubst_copy (TREE_PURPOSE (t), argvec, | |
15542 | tf_warning_or_error, NULL_TREE); | |
15543 | ||
15544 | decl = expand_member_init (decl); | |
15545 | if (decl && !DECL_P (decl)) | |
15546 | in_base_initializer = 1; | |
15547 | ||
15548 | init = tsubst_expr (TREE_VALUE (t), argvec, | |
15549 | tf_warning_or_error, NULL_TREE, | |
15550 | /*integral_constant_expression_p=*/false); | |
15551 | in_base_initializer = 0; | |
15552 | } | |
15553 | ||
15554 | if (decl) | |
15555 | { | |
15556 | init = build_tree_list (decl, init); | |
15557 | TREE_CHAIN (init) = inits; | |
15558 | inits = init; | |
15559 | } | |
15560 | } | |
e857e9c7 | 15561 | } |
6507cda8 | 15562 | return inits; |
e857e9c7 | 15563 | } |
15564 | ||
0f2952a1 | 15565 | /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */ |
15566 | ||
15567 | static void | |
807be5b4 | 15568 | set_current_access_from_decl (tree decl) |
0f2952a1 | 15569 | { |
15570 | if (TREE_PRIVATE (decl)) | |
15571 | current_access_specifier = access_private_node; | |
15572 | else if (TREE_PROTECTED (decl)) | |
15573 | current_access_specifier = access_protected_node; | |
15574 | else | |
15575 | current_access_specifier = access_public_node; | |
15576 | } | |
15577 | ||
d91e3832 | 15578 | /* Instantiate an enumerated type. TAG is the template type, NEWTAG |
15579 | is the instantiation (which should have been created with | |
15580 | start_enum) and ARGS are the template arguments to use. */ | |
dc9c6fd7 | 15581 | |
d91e3832 | 15582 | static void |
807be5b4 | 15583 | tsubst_enum (tree tag, tree newtag, tree args) |
dc9c6fd7 | 15584 | { |
d91e3832 | 15585 | tree e; |
dc9c6fd7 | 15586 | |
15587 | for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e)) | |
15588 | { | |
0f2952a1 | 15589 | tree value; |
ee1ab431 | 15590 | tree decl; |
15591 | ||
15592 | decl = TREE_VALUE (e); | |
0f2952a1 | 15593 | /* Note that in a template enum, the TREE_VALUE is the |
15594 | CONST_DECL, not the corresponding INTEGER_CST. */ | |
9031d10b | 15595 | value = tsubst_expr (DECL_INITIAL (decl), |
bde9ebf7 | 15596 | args, tf_warning_or_error, NULL_TREE, |
15597 | /*integral_constant_expression_p=*/true); | |
0f2952a1 | 15598 | |
15599 | /* Give this enumeration constant the correct access. */ | |
ee1ab431 | 15600 | set_current_access_from_decl (decl); |
0f2952a1 | 15601 | |
15602 | /* Actually build the enumerator itself. */ | |
9031d10b | 15603 | build_enumerator (DECL_NAME (decl), value, newtag); |
d91e3832 | 15604 | } |
31da200b | 15605 | |
2768cacc | 15606 | finish_enum (newtag); |
346064d9 | 15607 | DECL_SOURCE_LOCATION (TYPE_NAME (newtag)) |
15608 | = DECL_SOURCE_LOCATION (TYPE_NAME (tag)); | |
dc9c6fd7 | 15609 | } |
34197853 | 15610 | |
98eaf693 | 15611 | /* DECL is a FUNCTION_DECL that is a template specialization. Return |
15612 | its type -- but without substituting the innermost set of template | |
15613 | arguments. So, innermost set of template parameters will appear in | |
ff207dc8 | 15614 | the type. */ |
98eaf693 | 15615 | |
9031d10b | 15616 | tree |
807be5b4 | 15617 | get_mostly_instantiated_function_type (tree decl) |
98eaf693 | 15618 | { |
98eaf693 | 15619 | tree fn_type; |
15620 | tree tmpl; | |
15621 | tree targs; | |
15622 | tree tparms; | |
15623 | int parm_depth; | |
15624 | ||
15625 | tmpl = most_general_template (DECL_TI_TEMPLATE (decl)); | |
15626 | targs = DECL_TI_ARGS (decl); | |
15627 | tparms = DECL_TEMPLATE_PARMS (tmpl); | |
15628 | parm_depth = TMPL_PARMS_DEPTH (tparms); | |
15629 | ||
15630 | /* There should be as many levels of arguments as there are levels | |
15631 | of parameters. */ | |
b4df430b | 15632 | gcc_assert (parm_depth == TMPL_ARGS_DEPTH (targs)); |
98eaf693 | 15633 | |
15634 | fn_type = TREE_TYPE (tmpl); | |
98eaf693 | 15635 | |
15636 | if (parm_depth == 1) | |
15637 | /* No substitution is necessary. */ | |
15638 | ; | |
15639 | else | |
15640 | { | |
5051fdd6 | 15641 | int i, save_access_control; |
98eaf693 | 15642 | tree partial_args; |
15643 | ||
15644 | /* Replace the innermost level of the TARGS with NULL_TREEs to | |
ec0eee8d | 15645 | let tsubst know not to substitute for those parameters. */ |
98eaf693 | 15646 | partial_args = make_tree_vec (TREE_VEC_LENGTH (targs)); |
15647 | for (i = 1; i < TMPL_ARGS_DEPTH (targs); ++i) | |
15648 | SET_TMPL_ARGS_LEVEL (partial_args, i, | |
15649 | TMPL_ARGS_LEVEL (targs, i)); | |
15650 | SET_TMPL_ARGS_LEVEL (partial_args, | |
15651 | TMPL_ARGS_DEPTH (targs), | |
15652 | make_tree_vec (DECL_NTPARMS (tmpl))); | |
15653 | ||
5051fdd6 | 15654 | /* Disable access control as this function is used only during |
15655 | name-mangling. */ | |
15656 | save_access_control = flag_access_control; | |
15657 | flag_access_control = 0; | |
ff207dc8 | 15658 | |
c04baa53 | 15659 | ++processing_template_decl; |
98eaf693 | 15660 | /* Now, do the (partial) substitution to figure out the |
15661 | appropriate function type. */ | |
4b471722 | 15662 | fn_type = tsubst (fn_type, partial_args, tf_error, NULL_TREE); |
c04baa53 | 15663 | --processing_template_decl; |
98eaf693 | 15664 | |
15665 | /* Substitute into the template parameters to obtain the real | |
15666 | innermost set of parameters. This step is important if the | |
15667 | innermost set of template parameters contains value | |
15668 | parameters whose types depend on outer template parameters. */ | |
15669 | TREE_VEC_LENGTH (partial_args)--; | |
4b471722 | 15670 | tparms = tsubst_template_parms (tparms, partial_args, tf_error); |
98eaf693 | 15671 | |
5051fdd6 | 15672 | flag_access_control = save_access_control; |
ff207dc8 | 15673 | } |
98eaf693 | 15674 | |
15675 | return fn_type; | |
15676 | } | |
442fd60a | 15677 | |
b97edf7e | 15678 | /* Return truthvalue if we're processing a template different from |
ec0eee8d | 15679 | the last one involved in diagnostics. */ |
b97edf7e | 15680 | int |
807be5b4 | 15681 | problematic_instantiation_changed (void) |
b97edf7e | 15682 | { |
15683 | return last_template_error_tick != tinst_level_tick; | |
15684 | } | |
15685 | ||
15686 | /* Remember current template involved in diagnostics. */ | |
15687 | void | |
807be5b4 | 15688 | record_last_problematic_instantiation (void) |
b97edf7e | 15689 | { |
15690 | last_template_error_tick = tinst_level_tick; | |
15691 | } | |
15692 | ||
c5dd8e06 | 15693 | struct tinst_level * |
807be5b4 | 15694 | current_instantiation (void) |
b97edf7e | 15695 | { |
15696 | return current_tinst_level; | |
15697 | } | |
e6144333 | 15698 | |
15699 | /* [temp.param] Check that template non-type parm TYPE is of an allowable | |
3160db1d | 15700 | type. Return zero for ok, nonzero for disallowed. Issue error and |
4b471722 | 15701 | warning messages under control of COMPLAIN. */ |
e6144333 | 15702 | |
15703 | static int | |
807be5b4 | 15704 | invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain) |
e6144333 | 15705 | { |
15706 | if (INTEGRAL_TYPE_P (type)) | |
15707 | return 0; | |
15708 | else if (POINTER_TYPE_P (type)) | |
15709 | return 0; | |
1bc16cab | 15710 | else if (TYPE_PTR_TO_MEMBER_P (type)) |
e6144333 | 15711 | return 0; |
e6144333 | 15712 | else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM) |
15713 | return 0; | |
15714 | else if (TREE_CODE (type) == TYPENAME_TYPE) | |
15715 | return 0; | |
9031d10b | 15716 | |
4b471722 | 15717 | if (complain & tf_error) |
39e7b4a4 | 15718 | error ("%q#T is not a valid type for a template constant parameter", type); |
e6144333 | 15719 | return 1; |
15720 | } | |
1f3233d1 | 15721 | |
7f77d1f0 | 15722 | /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type]. |
15723 | Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/ | |
7e9a6a16 | 15724 | |
7f77d1f0 | 15725 | static bool |
15726 | dependent_type_p_r (tree type) | |
7e9a6a16 | 15727 | { |
15728 | tree scope; | |
15729 | ||
7e9a6a16 | 15730 | /* [temp.dep.type] |
15731 | ||
15732 | A type is dependent if it is: | |
15733 | ||
ce45a448 | 15734 | -- a template parameter. Template template parameters are types |
15735 | for us (since TYPE_P holds true for them) so we handle | |
15736 | them here. */ | |
9031d10b | 15737 | if (TREE_CODE (type) == TEMPLATE_TYPE_PARM |
fefedfd1 | 15738 | || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM) |
7e9a6a16 | 15739 | return true; |
15740 | /* -- a qualified-id with a nested-name-specifier which contains a | |
653e5405 | 15741 | class-name that names a dependent type or whose unqualified-id |
7e9a6a16 | 15742 | names a dependent type. */ |
15743 | if (TREE_CODE (type) == TYPENAME_TYPE) | |
15744 | return true; | |
15745 | /* -- a cv-qualified type where the cv-unqualified type is | |
653e5405 | 15746 | dependent. */ |
7e9a6a16 | 15747 | type = TYPE_MAIN_VARIANT (type); |
15748 | /* -- a compound type constructed from any dependent type. */ | |
1bc16cab | 15749 | if (TYPE_PTR_TO_MEMBER_P (type)) |
7e9a6a16 | 15750 | return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type)) |
9031d10b | 15751 | || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE |
7e9a6a16 | 15752 | (type))); |
15753 | else if (TREE_CODE (type) == POINTER_TYPE | |
15754 | || TREE_CODE (type) == REFERENCE_TYPE) | |
15755 | return dependent_type_p (TREE_TYPE (type)); | |
15756 | else if (TREE_CODE (type) == FUNCTION_TYPE | |
15757 | || TREE_CODE (type) == METHOD_TYPE) | |
15758 | { | |
15759 | tree arg_type; | |
15760 | ||
15761 | if (dependent_type_p (TREE_TYPE (type))) | |
15762 | return true; | |
9031d10b | 15763 | for (arg_type = TYPE_ARG_TYPES (type); |
15764 | arg_type; | |
7e9a6a16 | 15765 | arg_type = TREE_CHAIN (arg_type)) |
15766 | if (dependent_type_p (TREE_VALUE (arg_type))) | |
15767 | return true; | |
15768 | return false; | |
15769 | } | |
15770 | /* -- an array type constructed from any dependent type or whose | |
653e5405 | 15771 | size is specified by a constant expression that is |
7e9a6a16 | 15772 | value-dependent. */ |
15773 | if (TREE_CODE (type) == ARRAY_TYPE) | |
15774 | { | |
15775 | if (TYPE_DOMAIN (type) | |
510501ab | 15776 | && dependent_type_p (TYPE_DOMAIN (type))) |
7e9a6a16 | 15777 | return true; |
15778 | return dependent_type_p (TREE_TYPE (type)); | |
15779 | } | |
510501ab | 15780 | else if (TREE_CODE (type) == INTEGER_TYPE |
15781 | && !TREE_CONSTANT (TYPE_MAX_VALUE (type))) | |
15782 | { | |
15783 | /* If this is the TYPE_DOMAIN of an array type, consider it | |
15784 | dependent. */ | |
15785 | return (value_dependent_expression_p (TYPE_MAX_VALUE (type)) | |
15786 | || type_dependent_expression_p (TYPE_MAX_VALUE (type))); | |
15787 | } | |
9031d10b | 15788 | |
7e9a6a16 | 15789 | /* -- a template-id in which either the template name is a template |
ae8dd64b | 15790 | parameter ... */ |
15791 | if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM) | |
7e9a6a16 | 15792 | return true; |
ae8dd64b | 15793 | /* ... or any of the template arguments is a dependent type or |
bd8962d5 | 15794 | an expression that is type-dependent or value-dependent. */ |
ae8dd64b | 15795 | else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type) |
9031d10b | 15796 | && (any_dependent_template_arguments_p |
d7d79557 | 15797 | (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type))))) |
7e9a6a16 | 15798 | return true; |
9031d10b | 15799 | |
34da8800 | 15800 | /* All TYPEOF_TYPEs and DECLTYPE_TYPEs are dependent; if the |
15801 | argument of the `typeof' expression is not type-dependent, then | |
15802 | it should already been have resolved. */ | |
15803 | if (TREE_CODE (type) == TYPEOF_TYPE | |
15804 | || TREE_CODE (type) == DECLTYPE_TYPE) | |
7e9a6a16 | 15805 | return true; |
9031d10b | 15806 | |
d95d815d | 15807 | /* A template argument pack is dependent if any of its packed |
15808 | arguments are. */ | |
15809 | if (TREE_CODE (type) == TYPE_ARGUMENT_PACK) | |
15810 | { | |
15811 | tree args = ARGUMENT_PACK_ARGS (type); | |
15812 | int i, len = TREE_VEC_LENGTH (args); | |
15813 | for (i = 0; i < len; ++i) | |
15814 | if (dependent_template_arg_p (TREE_VEC_ELT (args, i))) | |
15815 | return true; | |
15816 | } | |
15817 | ||
15818 | /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must | |
15819 | be template parameters. */ | |
15820 | if (TREE_CODE (type) == TYPE_PACK_EXPANSION) | |
15821 | return true; | |
15822 | ||
7e9a6a16 | 15823 | /* The standard does not specifically mention types that are local |
15824 | to template functions or local classes, but they should be | |
15825 | considered dependent too. For example: | |
15826 | ||
9031d10b | 15827 | template <int I> void f() { |
653e5405 | 15828 | enum E { a = I }; |
7e9a6a16 | 15829 | S<sizeof (E)> s; |
15830 | } | |
15831 | ||
15832 | The size of `E' cannot be known until the value of `I' has been | |
15833 | determined. Therefore, `E' must be considered dependent. */ | |
15834 | scope = TYPE_CONTEXT (type); | |
15835 | if (scope && TYPE_P (scope)) | |
15836 | return dependent_type_p (scope); | |
15837 | else if (scope && TREE_CODE (scope) == FUNCTION_DECL) | |
15838 | return type_dependent_expression_p (scope); | |
15839 | ||
15840 | /* Other types are non-dependent. */ | |
15841 | return false; | |
15842 | } | |
15843 | ||
7f77d1f0 | 15844 | /* Returns TRUE if TYPE is dependent, in the sense of |
15845 | [temp.dep.type]. */ | |
15846 | ||
15847 | bool | |
15848 | dependent_type_p (tree type) | |
15849 | { | |
15850 | /* If there are no template parameters in scope, then there can't be | |
15851 | any dependent types. */ | |
15852 | if (!processing_template_decl) | |
36653a30 | 15853 | { |
15854 | /* If we are not processing a template, then nobody should be | |
15855 | providing us with a dependent type. */ | |
15856 | gcc_assert (type); | |
15857 | gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM); | |
15858 | return false; | |
15859 | } | |
7f77d1f0 | 15860 | |
15861 | /* If the type is NULL, we have not computed a type for the entity | |
15862 | in question; in that case, the type is dependent. */ | |
15863 | if (!type) | |
15864 | return true; | |
15865 | ||
15866 | /* Erroneous types can be considered non-dependent. */ | |
15867 | if (type == error_mark_node) | |
15868 | return false; | |
15869 | ||
15870 | /* If we have not already computed the appropriate value for TYPE, | |
15871 | do so now. */ | |
15872 | if (!TYPE_DEPENDENT_P_VALID (type)) | |
15873 | { | |
15874 | TYPE_DEPENDENT_P (type) = dependent_type_p_r (type); | |
15875 | TYPE_DEPENDENT_P_VALID (type) = 1; | |
15876 | } | |
15877 | ||
15878 | return TYPE_DEPENDENT_P (type); | |
15879 | } | |
15880 | ||
59b4cc29 | 15881 | /* Returns TRUE if EXPRESSION is dependent, according to CRITERION. */ |
15882 | ||
15883 | static bool | |
15884 | dependent_scope_ref_p (tree expression, bool criterion (tree)) | |
15885 | { | |
15886 | tree scope; | |
15887 | tree name; | |
15888 | ||
b4df430b | 15889 | gcc_assert (TREE_CODE (expression) == SCOPE_REF); |
59b4cc29 | 15890 | |
15891 | if (!TYPE_P (TREE_OPERAND (expression, 0))) | |
15892 | return true; | |
15893 | ||
15894 | scope = TREE_OPERAND (expression, 0); | |
15895 | name = TREE_OPERAND (expression, 1); | |
15896 | ||
15897 | /* [temp.dep.expr] | |
15898 | ||
15899 | An id-expression is type-dependent if it contains a | |
15900 | nested-name-specifier that contains a class-name that names a | |
15901 | dependent type. */ | |
15902 | /* The suggested resolution to Core Issue 2 implies that if the | |
15903 | qualifying type is the current class, then we must peek | |
15904 | inside it. */ | |
9031d10b | 15905 | if (DECL_P (name) |
59b4cc29 | 15906 | && currently_open_class (scope) |
15907 | && !criterion (name)) | |
15908 | return false; | |
15909 | if (dependent_type_p (scope)) | |
15910 | return true; | |
15911 | ||
15912 | return false; | |
15913 | } | |
15914 | ||
5d20b8d6 | 15915 | /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of |
d377303f | 15916 | [temp.dep.constexpr]. EXPRESSION is already known to be a constant |
15917 | expression. */ | |
7e9a6a16 | 15918 | |
5f6526e1 | 15919 | bool |
7e9a6a16 | 15920 | value_dependent_expression_p (tree expression) |
15921 | { | |
15922 | if (!processing_template_decl) | |
15923 | return false; | |
15924 | ||
15925 | /* A name declared with a dependent type. */ | |
165b0cb9 | 15926 | if (DECL_P (expression) && type_dependent_expression_p (expression)) |
7e9a6a16 | 15927 | return true; |
9031d10b | 15928 | |
165b0cb9 | 15929 | switch (TREE_CODE (expression)) |
15930 | { | |
15931 | case IDENTIFIER_NODE: | |
15932 | /* A name that has not been looked up -- must be dependent. */ | |
15933 | return true; | |
15934 | ||
15935 | case TEMPLATE_PARM_INDEX: | |
15936 | /* A non-type template parm. */ | |
15937 | return true; | |
15938 | ||
15939 | case CONST_DECL: | |
15940 | /* A non-type template parm. */ | |
15941 | if (DECL_TEMPLATE_PARM_P (expression)) | |
13795292 | 15942 | return true; |
4838fa0b | 15943 | return value_dependent_expression_p (DECL_INITIAL (expression)); |
9ee4e816 | 15944 | |
165b0cb9 | 15945 | case VAR_DECL: |
9031d10b | 15946 | /* A constant with integral or enumeration type and is initialized |
653e5405 | 15947 | with an expression that is value-dependent. */ |
165b0cb9 | 15948 | if (DECL_INITIAL (expression) |
15949 | && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (expression)) | |
15950 | && value_dependent_expression_p (DECL_INITIAL (expression))) | |
15951 | return true; | |
15952 | return false; | |
15953 | ||
15954 | case DYNAMIC_CAST_EXPR: | |
15955 | case STATIC_CAST_EXPR: | |
15956 | case CONST_CAST_EXPR: | |
15957 | case REINTERPRET_CAST_EXPR: | |
15958 | case CAST_EXPR: | |
15959 | /* These expressions are value-dependent if the type to which | |
653e5405 | 15960 | the cast occurs is dependent or the expression being casted |
15961 | is value-dependent. */ | |
165b0cb9 | 15962 | { |
15963 | tree type = TREE_TYPE (expression); | |
9031d10b | 15964 | |
165b0cb9 | 15965 | if (dependent_type_p (type)) |
15966 | return true; | |
9031d10b | 15967 | |
165b0cb9 | 15968 | /* A functional cast has a list of operands. */ |
15969 | expression = TREE_OPERAND (expression, 0); | |
15970 | if (!expression) | |
15971 | { | |
15972 | /* If there are no operands, it must be an expression such | |
15973 | as "int()". This should not happen for aggregate types | |
15974 | because it would form non-constant expressions. */ | |
15975 | gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type)); | |
9031d10b | 15976 | |
165b0cb9 | 15977 | return false; |
15978 | } | |
9031d10b | 15979 | |
165b0cb9 | 15980 | if (TREE_CODE (expression) == TREE_LIST) |
62116ec3 | 15981 | return any_value_dependent_elements_p (expression); |
9031d10b | 15982 | |
13795292 | 15983 | return value_dependent_expression_p (expression); |
165b0cb9 | 15984 | } |
9031d10b | 15985 | |
165b0cb9 | 15986 | case SIZEOF_EXPR: |
15987 | case ALIGNOF_EXPR: | |
15988 | /* A `sizeof' expression is value-dependent if the operand is | |
d95d815d | 15989 | type-dependent or is a pack expansion. */ |
13795292 | 15990 | expression = TREE_OPERAND (expression, 0); |
d95d815d | 15991 | if (PACK_EXPANSION_P (expression)) |
15992 | return true; | |
15993 | else if (TYPE_P (expression)) | |
13795292 | 15994 | return dependent_type_p (expression); |
15995 | return type_dependent_expression_p (expression); | |
3a88b1db | 15996 | |
165b0cb9 | 15997 | case SCOPE_REF: |
15998 | return dependent_scope_ref_p (expression, value_dependent_expression_p); | |
3a88b1db | 15999 | |
165b0cb9 | 16000 | case COMPONENT_REF: |
16001 | return (value_dependent_expression_p (TREE_OPERAND (expression, 0)) | |
16002 | || value_dependent_expression_p (TREE_OPERAND (expression, 1))); | |
16003 | ||
16004 | case CALL_EXPR: | |
d377303f | 16005 | /* A CALL_EXPR may appear in a constant expression if it is a |
16006 | call to a builtin function, e.g., __builtin_constant_p. All | |
16007 | such calls are value-dependent. */ | |
16008 | return true; | |
165b0cb9 | 16009 | |
d95d815d | 16010 | case NONTYPE_ARGUMENT_PACK: |
16011 | /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument | |
16012 | is value-dependent. */ | |
16013 | { | |
16014 | tree values = ARGUMENT_PACK_ARGS (expression); | |
16015 | int i, len = TREE_VEC_LENGTH (values); | |
16016 | ||
16017 | for (i = 0; i < len; ++i) | |
16018 | if (value_dependent_expression_p (TREE_VEC_ELT (values, i))) | |
16019 | return true; | |
16020 | ||
16021 | return false; | |
16022 | } | |
16023 | ||
481451eb | 16024 | case TRAIT_EXPR: |
16025 | { | |
16026 | tree type2 = TRAIT_EXPR_TYPE2 (expression); | |
16027 | return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression)) | |
16028 | || (type2 ? dependent_type_p (type2) : false)); | |
16029 | } | |
16030 | ||
18bf4e9a | 16031 | case MODOP_EXPR: |
16032 | return ((value_dependent_expression_p (TREE_OPERAND (expression, 0))) | |
16033 | || (value_dependent_expression_p (TREE_OPERAND (expression, 2)))); | |
16034 | ||
165b0cb9 | 16035 | default: |
16036 | /* A constant expression is value-dependent if any subexpression is | |
653e5405 | 16037 | value-dependent. */ |
7e9a6a16 | 16038 | switch (TREE_CODE_CLASS (TREE_CODE (expression))) |
16039 | { | |
165b0cb9 | 16040 | case tcc_reference: |
ce45a448 | 16041 | case tcc_unary: |
9031d10b | 16042 | return (value_dependent_expression_p |
7e9a6a16 | 16043 | (TREE_OPERAND (expression, 0))); |
9031d10b | 16044 | |
ce45a448 | 16045 | case tcc_comparison: |
16046 | case tcc_binary: | |
9031d10b | 16047 | return ((value_dependent_expression_p |
7e9a6a16 | 16048 | (TREE_OPERAND (expression, 0))) |
9031d10b | 16049 | || (value_dependent_expression_p |
7e9a6a16 | 16050 | (TREE_OPERAND (expression, 1)))); |
9031d10b | 16051 | |
ce45a448 | 16052 | case tcc_expression: |
c2f47e15 | 16053 | case tcc_vl_exp: |
7e9a6a16 | 16054 | { |
16055 | int i; | |
c2f47e15 | 16056 | for (i = 0; i < TREE_OPERAND_LENGTH (expression); ++i) |
5f6526e1 | 16057 | /* In some cases, some of the operands may be missing. |
16058 | (For example, in the case of PREDECREMENT_EXPR, the | |
16059 | amount to increment by may be missing.) That doesn't | |
16060 | make the expression dependent. */ | |
16061 | if (TREE_OPERAND (expression, i) | |
16062 | && (value_dependent_expression_p | |
16063 | (TREE_OPERAND (expression, i)))) | |
7e9a6a16 | 16064 | return true; |
16065 | return false; | |
16066 | } | |
9031d10b | 16067 | |
ce45a448 | 16068 | default: |
165b0cb9 | 16069 | break; |
7e9a6a16 | 16070 | } |
16071 | } | |
9031d10b | 16072 | |
7e9a6a16 | 16073 | /* The expression is not value-dependent. */ |
16074 | return false; | |
16075 | } | |
16076 | ||
16077 | /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of | |
16078 | [temp.dep.expr]. */ | |
16079 | ||
16080 | bool | |
807be5b4 | 16081 | type_dependent_expression_p (tree expression) |
7e9a6a16 | 16082 | { |
16083 | if (!processing_template_decl) | |
16084 | return false; | |
16085 | ||
0986fa22 | 16086 | if (expression == error_mark_node) |
16087 | return false; | |
c08d51be | 16088 | |
16089 | /* An unresolved name is always dependent. */ | |
4a132a96 | 16090 | if (TREE_CODE (expression) == IDENTIFIER_NODE |
16091 | || TREE_CODE (expression) == USING_DECL) | |
c08d51be | 16092 | return true; |
9031d10b | 16093 | |
7e9a6a16 | 16094 | /* Some expression forms are never type-dependent. */ |
16095 | if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR | |
16096 | || TREE_CODE (expression) == SIZEOF_EXPR | |
16097 | || TREE_CODE (expression) == ALIGNOF_EXPR | |
481451eb | 16098 | || TREE_CODE (expression) == TRAIT_EXPR |
7e9a6a16 | 16099 | || TREE_CODE (expression) == TYPEID_EXPR |
16100 | || TREE_CODE (expression) == DELETE_EXPR | |
16101 | || TREE_CODE (expression) == VEC_DELETE_EXPR | |
16102 | || TREE_CODE (expression) == THROW_EXPR) | |
16103 | return false; | |
16104 | ||
16105 | /* The types of these expressions depends only on the type to which | |
16106 | the cast occurs. */ | |
16107 | if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR | |
16108 | || TREE_CODE (expression) == STATIC_CAST_EXPR | |
16109 | || TREE_CODE (expression) == CONST_CAST_EXPR | |
16110 | || TREE_CODE (expression) == REINTERPRET_CAST_EXPR | |
16111 | || TREE_CODE (expression) == CAST_EXPR) | |
16112 | return dependent_type_p (TREE_TYPE (expression)); | |
13795292 | 16113 | |
7e9a6a16 | 16114 | /* The types of these expressions depends only on the type created |
16115 | by the expression. */ | |
13795292 | 16116 | if (TREE_CODE (expression) == NEW_EXPR |
16117 | || TREE_CODE (expression) == VEC_NEW_EXPR) | |
d5766515 | 16118 | { |
16119 | /* For NEW_EXPR tree nodes created inside a template, either | |
16120 | the object type itself or a TREE_LIST may appear as the | |
16121 | operand 1. */ | |
16122 | tree type = TREE_OPERAND (expression, 1); | |
16123 | if (TREE_CODE (type) == TREE_LIST) | |
16124 | /* This is an array type. We need to check array dimensions | |
16125 | as well. */ | |
16126 | return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type))) | |
16127 | || value_dependent_expression_p | |
16128 | (TREE_OPERAND (TREE_VALUE (type), 1)); | |
16129 | else | |
16130 | return dependent_type_p (type); | |
16131 | } | |
7e9a6a16 | 16132 | |
d1842279 | 16133 | if (TREE_CODE (expression) == SCOPE_REF |
16134 | && dependent_scope_ref_p (expression, | |
16135 | type_dependent_expression_p)) | |
16136 | return true; | |
16137 | ||
16138 | if (TREE_CODE (expression) == FUNCTION_DECL | |
16139 | && DECL_LANG_SPECIFIC (expression) | |
16140 | && DECL_TEMPLATE_INFO (expression) | |
16141 | && (any_dependent_template_arguments_p | |
16142 | (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression))))) | |
16143 | return true; | |
16144 | ||
16145 | if (TREE_CODE (expression) == TEMPLATE_DECL | |
16146 | && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression)) | |
16147 | return false; | |
16148 | ||
ffc6c453 | 16149 | if (TREE_CODE (expression) == STMT_EXPR) |
16150 | expression = stmt_expr_value_expr (expression); | |
16151 | ||
13795292 | 16152 | if (TREE_TYPE (expression) == unknown_type_node) |
16153 | { | |
16154 | if (TREE_CODE (expression) == ADDR_EXPR) | |
16155 | return type_dependent_expression_p (TREE_OPERAND (expression, 0)); | |
7f233616 | 16156 | if (TREE_CODE (expression) == COMPONENT_REF |
16157 | || TREE_CODE (expression) == OFFSET_REF) | |
d1ff0e2d | 16158 | { |
16159 | if (type_dependent_expression_p (TREE_OPERAND (expression, 0))) | |
16160 | return true; | |
16161 | expression = TREE_OPERAND (expression, 1); | |
16162 | if (TREE_CODE (expression) == IDENTIFIER_NODE) | |
16163 | return false; | |
16164 | } | |
c3c0139b | 16165 | /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */ |
16166 | if (TREE_CODE (expression) == SCOPE_REF) | |
16167 | return false; | |
9031d10b | 16168 | |
13795292 | 16169 | if (TREE_CODE (expression) == BASELINK) |
16170 | expression = BASELINK_FUNCTIONS (expression); | |
9031d10b | 16171 | |
13795292 | 16172 | if (TREE_CODE (expression) == TEMPLATE_ID_EXPR) |
16173 | { | |
8512e308 | 16174 | if (any_dependent_template_arguments_p |
16175 | (TREE_OPERAND (expression, 1))) | |
13795292 | 16176 | return true; |
16177 | expression = TREE_OPERAND (expression, 0); | |
16178 | } | |
09e79bdc | 16179 | gcc_assert (TREE_CODE (expression) == OVERLOAD |
16180 | || TREE_CODE (expression) == FUNCTION_DECL); | |
9031d10b | 16181 | |
2e3e31d2 | 16182 | while (expression) |
13795292 | 16183 | { |
2e3e31d2 | 16184 | if (type_dependent_expression_p (OVL_CURRENT (expression))) |
16185 | return true; | |
16186 | expression = OVL_NEXT (expression); | |
13795292 | 16187 | } |
2e3e31d2 | 16188 | return false; |
13795292 | 16189 | } |
9031d10b | 16190 | |
673b95fd | 16191 | gcc_assert (TREE_CODE (expression) != TYPE_DECL); |
074ab442 | 16192 | |
7e9a6a16 | 16193 | return (dependent_type_p (TREE_TYPE (expression))); |
16194 | } | |
16195 | ||
13795292 | 16196 | /* Returns TRUE if ARGS (a TREE_LIST of arguments to a function call) |
16197 | contains a type-dependent expression. */ | |
7e9a6a16 | 16198 | |
16199 | bool | |
9f627b1a | 16200 | any_type_dependent_arguments_p (const_tree args) |
13795292 | 16201 | { |
16202 | while (args) | |
16203 | { | |
0986fa22 | 16204 | tree arg = TREE_VALUE (args); |
16205 | ||
16206 | if (type_dependent_expression_p (arg)) | |
13795292 | 16207 | return true; |
16208 | args = TREE_CHAIN (args); | |
16209 | } | |
16210 | return false; | |
16211 | } | |
16212 | ||
62116ec3 | 16213 | /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are |
16214 | expressions) contains any value-dependent expressions. */ | |
16215 | ||
16216 | bool | |
9f627b1a | 16217 | any_value_dependent_elements_p (const_tree list) |
62116ec3 | 16218 | { |
16219 | for (; list; list = TREE_CHAIN (list)) | |
16220 | if (value_dependent_expression_p (TREE_VALUE (list))) | |
16221 | return true; | |
16222 | ||
16223 | return false; | |
16224 | } | |
16225 | ||
13795292 | 16226 | /* Returns TRUE if the ARG (a template argument) is dependent. */ |
16227 | ||
d95d815d | 16228 | bool |
7e9a6a16 | 16229 | dependent_template_arg_p (tree arg) |
16230 | { | |
16231 | if (!processing_template_decl) | |
16232 | return false; | |
16233 | ||
16234 | if (TREE_CODE (arg) == TEMPLATE_DECL | |
16235 | || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM) | |
16236 | return dependent_template_p (arg); | |
d95d815d | 16237 | else if (ARGUMENT_PACK_P (arg)) |
16238 | { | |
16239 | tree args = ARGUMENT_PACK_ARGS (arg); | |
16240 | int i, len = TREE_VEC_LENGTH (args); | |
16241 | for (i = 0; i < len; ++i) | |
16242 | { | |
16243 | if (dependent_template_arg_p (TREE_VEC_ELT (args, i))) | |
16244 | return true; | |
16245 | } | |
16246 | ||
16247 | return false; | |
16248 | } | |
7e9a6a16 | 16249 | else if (TYPE_P (arg)) |
16250 | return dependent_type_p (arg); | |
16251 | else | |
16252 | return (type_dependent_expression_p (arg) | |
16253 | || value_dependent_expression_p (arg)); | |
16254 | } | |
16255 | ||
6753bca0 | 16256 | /* Returns true if ARGS (a collection of template arguments) contains |
16257 | any types that require structural equality testing. */ | |
16258 | ||
16259 | bool | |
16260 | any_template_arguments_need_structural_equality_p (tree args) | |
16261 | { | |
16262 | int i; | |
16263 | int j; | |
16264 | ||
16265 | if (!args) | |
16266 | return false; | |
16267 | if (args == error_mark_node) | |
16268 | return true; | |
16269 | ||
16270 | for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i) | |
16271 | { | |
16272 | tree level = TMPL_ARGS_LEVEL (args, i + 1); | |
16273 | for (j = 0; j < TREE_VEC_LENGTH (level); ++j) | |
16274 | { | |
16275 | tree arg = TREE_VEC_ELT (level, j); | |
d95d815d | 16276 | tree packed_args = NULL_TREE; |
16277 | int k, len = 1; | |
16278 | ||
16279 | if (ARGUMENT_PACK_P (arg)) | |
16280 | { | |
16281 | /* Look inside the argument pack. */ | |
16282 | packed_args = ARGUMENT_PACK_ARGS (arg); | |
16283 | len = TREE_VEC_LENGTH (packed_args); | |
16284 | } | |
16285 | ||
16286 | for (k = 0; k < len; ++k) | |
16287 | { | |
16288 | if (packed_args) | |
16289 | arg = TREE_VEC_ELT (packed_args, k); | |
16290 | ||
16291 | if (error_operand_p (arg)) | |
16292 | return true; | |
16293 | else if (TREE_CODE (arg) == TEMPLATE_DECL | |
16294 | || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM) | |
16295 | continue; | |
16296 | else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg)) | |
16297 | return true; | |
16298 | else if (!TYPE_P (arg) && TREE_TYPE (arg) | |
16299 | && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg))) | |
16300 | return true; | |
16301 | } | |
6753bca0 | 16302 | } |
16303 | } | |
16304 | ||
16305 | return false; | |
16306 | } | |
16307 | ||
13795292 | 16308 | /* Returns true if ARGS (a collection of template arguments) contains |
16309 | any dependent arguments. */ | |
7e9a6a16 | 16310 | |
13795292 | 16311 | bool |
9f627b1a | 16312 | any_dependent_template_arguments_p (const_tree args) |
7e9a6a16 | 16313 | { |
b5959ba9 | 16314 | int i; |
7f233616 | 16315 | int j; |
16316 | ||
13795292 | 16317 | if (!args) |
16318 | return false; | |
cd2ba456 | 16319 | if (args == error_mark_node) |
16320 | return true; | |
13795292 | 16321 | |
7f233616 | 16322 | for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i) |
16323 | { | |
9f627b1a | 16324 | const_tree level = TMPL_ARGS_LEVEL (args, i + 1); |
7f233616 | 16325 | for (j = 0; j < TREE_VEC_LENGTH (level); ++j) |
16326 | if (dependent_template_arg_p (TREE_VEC_ELT (level, j))) | |
16327 | return true; | |
16328 | } | |
7e9a6a16 | 16329 | |
7e9a6a16 | 16330 | return false; |
16331 | } | |
16332 | ||
16333 | /* Returns TRUE if the template TMPL is dependent. */ | |
16334 | ||
16335 | bool | |
16336 | dependent_template_p (tree tmpl) | |
16337 | { | |
12136637 | 16338 | if (TREE_CODE (tmpl) == OVERLOAD) |
16339 | { | |
16340 | while (tmpl) | |
16341 | { | |
16342 | if (dependent_template_p (OVL_FUNCTION (tmpl))) | |
16343 | return true; | |
16344 | tmpl = OVL_CHAIN (tmpl); | |
16345 | } | |
16346 | return false; | |
16347 | } | |
16348 | ||
7e9a6a16 | 16349 | /* Template template parameters are dependent. */ |
16350 | if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl) | |
16351 | || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM) | |
16352 | return true; | |
85633df2 | 16353 | /* So are names that have not been looked up. */ |
7322c29e | 16354 | if (TREE_CODE (tmpl) == SCOPE_REF |
16355 | || TREE_CODE (tmpl) == IDENTIFIER_NODE) | |
13795292 | 16356 | return true; |
7e9a6a16 | 16357 | /* So are member templates of dependent classes. */ |
16358 | if (TYPE_P (CP_DECL_CONTEXT (tmpl))) | |
16359 | return dependent_type_p (DECL_CONTEXT (tmpl)); | |
16360 | return false; | |
16361 | } | |
16362 | ||
13795292 | 16363 | /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */ |
16364 | ||
16365 | bool | |
16366 | dependent_template_id_p (tree tmpl, tree args) | |
16367 | { | |
16368 | return (dependent_template_p (tmpl) | |
16369 | || any_dependent_template_arguments_p (args)); | |
16370 | } | |
16371 | ||
fd6481cf | 16372 | /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors |
16373 | is dependent. */ | |
16374 | ||
16375 | bool | |
16376 | dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv) | |
16377 | { | |
16378 | int i; | |
16379 | ||
16380 | if (!processing_template_decl) | |
16381 | return false; | |
16382 | ||
16383 | for (i = 0; i < TREE_VEC_LENGTH (declv); i++) | |
16384 | { | |
16385 | tree decl = TREE_VEC_ELT (declv, i); | |
16386 | tree init = TREE_VEC_ELT (initv, i); | |
16387 | tree cond = TREE_VEC_ELT (condv, i); | |
16388 | tree incr = TREE_VEC_ELT (incrv, i); | |
16389 | ||
16390 | if (type_dependent_expression_p (decl)) | |
16391 | return true; | |
16392 | ||
16393 | if (init && type_dependent_expression_p (init)) | |
16394 | return true; | |
16395 | ||
16396 | if (type_dependent_expression_p (cond)) | |
16397 | return true; | |
16398 | ||
16399 | if (COMPARISON_CLASS_P (cond) | |
16400 | && (type_dependent_expression_p (TREE_OPERAND (cond, 0)) | |
16401 | || type_dependent_expression_p (TREE_OPERAND (cond, 1)))) | |
16402 | return true; | |
16403 | ||
16404 | if (TREE_CODE (incr) == MODOP_EXPR) | |
16405 | { | |
16406 | if (type_dependent_expression_p (TREE_OPERAND (incr, 0)) | |
16407 | || type_dependent_expression_p (TREE_OPERAND (incr, 2))) | |
16408 | return true; | |
16409 | } | |
16410 | else if (type_dependent_expression_p (incr)) | |
16411 | return true; | |
16412 | else if (TREE_CODE (incr) == MODIFY_EXPR) | |
16413 | { | |
16414 | if (type_dependent_expression_p (TREE_OPERAND (incr, 0))) | |
16415 | return true; | |
16416 | else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1))) | |
16417 | { | |
16418 | tree t = TREE_OPERAND (incr, 1); | |
16419 | if (type_dependent_expression_p (TREE_OPERAND (t, 0)) | |
16420 | || type_dependent_expression_p (TREE_OPERAND (t, 1))) | |
16421 | return true; | |
16422 | } | |
16423 | } | |
16424 | } | |
16425 | ||
16426 | return false; | |
16427 | } | |
16428 | ||
5f6526e1 | 16429 | /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the |
8826a863 | 16430 | TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if |
16431 | no such TYPE can be found. Note that this function peers inside | |
16432 | uninstantiated templates and therefore should be used only in | |
16433 | extremely limited situations. ONLY_CURRENT_P restricts this | |
16434 | peering to the currently open classes hierarchy (which is required | |
16435 | when comparing types). */ | |
5f6526e1 | 16436 | |
16437 | tree | |
16438 | resolve_typename_type (tree type, bool only_current_p) | |
16439 | { | |
16440 | tree scope; | |
16441 | tree name; | |
16442 | tree decl; | |
16443 | int quals; | |
7f602bca | 16444 | tree pushed_scope; |
8826a863 | 16445 | tree result; |
5f6526e1 | 16446 | |
b4df430b | 16447 | gcc_assert (TREE_CODE (type) == TYPENAME_TYPE); |
5f6526e1 | 16448 | |
16449 | scope = TYPE_CONTEXT (type); | |
16450 | name = TYPE_IDENTIFIER (type); | |
16451 | ||
16452 | /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve | |
16453 | it first before we can figure out what NAME refers to. */ | |
16454 | if (TREE_CODE (scope) == TYPENAME_TYPE) | |
16455 | scope = resolve_typename_type (scope, only_current_p); | |
16456 | /* If we don't know what SCOPE refers to, then we cannot resolve the | |
16457 | TYPENAME_TYPE. */ | |
8826a863 | 16458 | if (TREE_CODE (scope) == TYPENAME_TYPE) |
16459 | return type; | |
5f6526e1 | 16460 | /* If the SCOPE is a template type parameter, we have no way of |
16461 | resolving the name. */ | |
16462 | if (TREE_CODE (scope) == TEMPLATE_TYPE_PARM) | |
16463 | return type; | |
16464 | /* If the SCOPE is not the current instantiation, there's no reason | |
16465 | to look inside it. */ | |
16466 | if (only_current_p && !currently_open_class (scope)) | |
8826a863 | 16467 | return type; |
ad9ba1e5 | 16468 | /* If SCOPE isn't the template itself, it will not have a valid |
16469 | TYPE_FIELDS list. */ | |
16470 | if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope))) | |
16471 | /* scope is either the template itself or a compatible instantiation | |
16472 | like X<T>, so look up the name in the original template. */ | |
16473 | scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope); | |
16474 | else | |
16475 | /* scope is a partial instantiation, so we can't do the lookup or we | |
16476 | will lose the template arguments. */ | |
16477 | return type; | |
5f6526e1 | 16478 | /* Enter the SCOPE so that name lookup will be resolved as if we |
16479 | were in the class definition. In particular, SCOPE will no | |
16480 | longer be considered a dependent type. */ | |
7f602bca | 16481 | pushed_scope = push_scope (scope); |
5f6526e1 | 16482 | /* Look up the declaration. */ |
b330805e | 16483 | decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true); |
8826a863 | 16484 | |
16485 | result = NULL_TREE; | |
16486 | ||
5f6526e1 | 16487 | /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to |
16488 | find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */ | |
16489 | if (!decl) | |
8826a863 | 16490 | /*nop*/; |
5f6526e1 | 16491 | else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == IDENTIFIER_NODE |
16492 | && TREE_CODE (decl) == TYPE_DECL) | |
8826a863 | 16493 | { |
16494 | result = TREE_TYPE (decl); | |
16495 | if (result == error_mark_node) | |
16496 | result = NULL_TREE; | |
16497 | } | |
5f6526e1 | 16498 | else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == TEMPLATE_ID_EXPR |
16499 | && DECL_CLASS_TEMPLATE_P (decl)) | |
16500 | { | |
16501 | tree tmpl; | |
16502 | tree args; | |
16503 | /* Obtain the template and the arguments. */ | |
16504 | tmpl = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 0); | |
16505 | args = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 1); | |
16506 | /* Instantiate the template. */ | |
8826a863 | 16507 | result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE, |
16508 | /*entering_scope=*/0, | |
16509 | tf_error | tf_user); | |
16510 | if (result == error_mark_node) | |
16511 | result = NULL_TREE; | |
5f6526e1 | 16512 | } |
8826a863 | 16513 | |
5f6526e1 | 16514 | /* Leave the SCOPE. */ |
7f602bca | 16515 | if (pushed_scope) |
16516 | pop_scope (pushed_scope); | |
5f6526e1 | 16517 | |
8826a863 | 16518 | /* If we failed to resolve it, return the original typename. */ |
16519 | if (!result) | |
16520 | return type; | |
16521 | ||
16522 | /* If lookup found a typename type, resolve that too. */ | |
16523 | if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result)) | |
16524 | { | |
16525 | /* Ill-formed programs can cause infinite recursion here, so we | |
16526 | must catch that. */ | |
16527 | TYPENAME_IS_RESOLVING_P (type) = 1; | |
16528 | result = resolve_typename_type (result, only_current_p); | |
16529 | TYPENAME_IS_RESOLVING_P (type) = 0; | |
16530 | } | |
16531 | ||
16532 | /* Qualify the resulting type. */ | |
16533 | quals = cp_type_quals (type); | |
16534 | if (quals) | |
16535 | result = cp_build_qualified_type (result, cp_type_quals (result) | quals); | |
16536 | ||
16537 | return result; | |
5f6526e1 | 16538 | } |
16539 | ||
13795292 | 16540 | /* EXPR is an expression which is not type-dependent. Return a proxy |
16541 | for EXPR that can be used to compute the types of larger | |
16542 | expressions containing EXPR. */ | |
16543 | ||
16544 | tree | |
16545 | build_non_dependent_expr (tree expr) | |
16546 | { | |
df6f8740 | 16547 | tree inner_expr; |
16548 | ||
9031d10b | 16549 | /* Preserve null pointer constants so that the type of things like |
13795292 | 16550 | "p == 0" where "p" is a pointer can be determined. */ |
16551 | if (null_ptr_cst_p (expr)) | |
16552 | return expr; | |
16553 | /* Preserve OVERLOADs; the functions must be available to resolve | |
16554 | types. */ | |
2f9d63f9 | 16555 | inner_expr = expr; |
ffc6c453 | 16556 | if (TREE_CODE (inner_expr) == STMT_EXPR) |
16557 | inner_expr = stmt_expr_value_expr (inner_expr); | |
2f9d63f9 | 16558 | if (TREE_CODE (inner_expr) == ADDR_EXPR) |
16559 | inner_expr = TREE_OPERAND (inner_expr, 0); | |
16560 | if (TREE_CODE (inner_expr) == COMPONENT_REF) | |
16561 | inner_expr = TREE_OPERAND (inner_expr, 1); | |
96642f8a | 16562 | if (is_overloaded_fn (inner_expr) |
33d18491 | 16563 | || TREE_CODE (inner_expr) == OFFSET_REF) |
13795292 | 16564 | return expr; |
ab6bb714 | 16565 | /* There is no need to return a proxy for a variable. */ |
16566 | if (TREE_CODE (expr) == VAR_DECL) | |
16567 | return expr; | |
cf326312 | 16568 | /* Preserve string constants; conversions from string constants to |
16569 | "char *" are allowed, even though normally a "const char *" | |
16570 | cannot be used to initialize a "char *". */ | |
16571 | if (TREE_CODE (expr) == STRING_CST) | |
16572 | return expr; | |
cdbcf942 | 16573 | /* Preserve arithmetic constants, as an optimization -- there is no |
16574 | reason to create a new node. */ | |
16575 | if (TREE_CODE (expr) == INTEGER_CST || TREE_CODE (expr) == REAL_CST) | |
16576 | return expr; | |
0b1ece0b | 16577 | /* Preserve THROW_EXPRs -- all throw-expressions have type "void". |
16578 | There is at least one place where we want to know that a | |
16579 | particular expression is a throw-expression: when checking a ?: | |
16580 | expression, there are special rules if the second or third | |
415bd54e | 16581 | argument is a throw-expression. */ |
0b1ece0b | 16582 | if (TREE_CODE (expr) == THROW_EXPR) |
16583 | return expr; | |
11dfb79c | 16584 | |
16585 | if (TREE_CODE (expr) == COND_EXPR) | |
831d52a2 | 16586 | return build3 (COND_EXPR, |
16587 | TREE_TYPE (expr), | |
16588 | TREE_OPERAND (expr, 0), | |
9031d10b | 16589 | (TREE_OPERAND (expr, 1) |
831d52a2 | 16590 | ? build_non_dependent_expr (TREE_OPERAND (expr, 1)) |
16591 | : build_non_dependent_expr (TREE_OPERAND (expr, 0))), | |
16592 | build_non_dependent_expr (TREE_OPERAND (expr, 2))); | |
b6691ff5 | 16593 | if (TREE_CODE (expr) == COMPOUND_EXPR |
16594 | && !COMPOUND_EXPR_OVERLOADED (expr)) | |
831d52a2 | 16595 | return build2 (COMPOUND_EXPR, |
16596 | TREE_TYPE (expr), | |
16597 | TREE_OPERAND (expr, 0), | |
16598 | build_non_dependent_expr (TREE_OPERAND (expr, 1))); | |
9031d10b | 16599 | |
5d6d17b7 | 16600 | /* If the type is unknown, it can't really be non-dependent */ |
16601 | gcc_assert (TREE_TYPE (expr) != unknown_type_node); | |
074ab442 | 16602 | |
9031d10b | 16603 | /* Otherwise, build a NON_DEPENDENT_EXPR. |
13795292 | 16604 | |
16605 | REFERENCE_TYPEs are not stripped for expressions in templates | |
16606 | because doing so would play havoc with mangling. Consider, for | |
16607 | example: | |
16608 | ||
9031d10b | 16609 | template <typename T> void f<T& g>() { g(); } |
13795292 | 16610 | |
16611 | In the body of "f", the expression for "g" will have | |
16612 | REFERENCE_TYPE, even though the standard says that it should | |
16613 | not. The reason is that we must preserve the syntactic form of | |
16614 | the expression so that mangling (say) "f<g>" inside the body of | |
16615 | "f" works out correctly. Therefore, the REFERENCE_TYPE is | |
16616 | stripped here. */ | |
9b7c46f2 | 16617 | return build1 (NON_DEPENDENT_EXPR, non_reference (TREE_TYPE (expr)), expr); |
13795292 | 16618 | } |
16619 | ||
16620 | /* ARGS is a TREE_LIST of expressions as arguments to a function call. | |
16621 | Return a new TREE_LIST with the various arguments replaced with | |
16622 | equivalent non-dependent expressions. */ | |
16623 | ||
16624 | tree | |
16625 | build_non_dependent_args (tree args) | |
16626 | { | |
16627 | tree a; | |
16628 | tree new_args; | |
16629 | ||
16630 | new_args = NULL_TREE; | |
16631 | for (a = args; a; a = TREE_CHAIN (a)) | |
9031d10b | 16632 | new_args = tree_cons (NULL_TREE, |
13795292 | 16633 | build_non_dependent_expr (TREE_VALUE (a)), |
16634 | new_args); | |
16635 | return nreverse (new_args); | |
16636 | } | |
16637 | ||
1f3233d1 | 16638 | #include "gt-cp-pt.h" |