]>
Commit | Line | Data |
---|---|---|
8d08fdba | 1 | /* Handle parameterized types (templates) for GNU C++. |
3febd123 | 2 | Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, |
e77f031d | 3 | 2001, 2002, 2003, 2004, 2005, 2007 Free Software Foundation, Inc. |
8d08fdba | 4 | Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing. |
fc378698 | 5 | Rewritten by Jason Merrill (jason@cygnus.com). |
8d08fdba | 6 | |
f5adbb8d | 7 | This file is part of GCC. |
8d08fdba | 8 | |
f5adbb8d | 9 | GCC is free software; you can redistribute it and/or modify |
8d08fdba | 10 | it under the terms of the GNU General Public License as published by |
e77f031d | 11 | the Free Software Foundation; either version 3, or (at your option) |
8d08fdba MS |
12 | any later version. |
13 | ||
f5adbb8d | 14 | GCC is distributed in the hope that it will be useful, |
8d08fdba MS |
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | GNU General Public License for more details. | |
18 | ||
19 | You should have received a copy of the GNU General Public License | |
e77f031d NC |
20 | along with GCC; see the file COPYING3. If not see |
21 | <http://www.gnu.org/licenses/>. */ | |
8d08fdba MS |
22 | |
23 | /* Known bugs or deficiencies include: | |
e92cc029 | 24 | |
e92cc029 MS |
25 | all methods must be provided in header files; can't use a source |
26 | file that contains only the method templates and "just win". */ | |
8d08fdba MS |
27 | |
28 | #include "config.h" | |
8d052bc7 | 29 | #include "system.h" |
4977bab6 ZW |
30 | #include "coretypes.h" |
31 | #include "tm.h" | |
8d08fdba | 32 | #include "obstack.h" |
8d08fdba | 33 | #include "tree.h" |
0c58f841 | 34 | #include "pointer-set.h" |
8d08fdba | 35 | #include "flags.h" |
e58a9aa1 | 36 | #include "c-common.h" |
8d08fdba | 37 | #include "cp-tree.h" |
e58a9aa1 | 38 | #include "cp-objcp-common.h" |
25af8512 | 39 | #include "tree-inline.h" |
8d08fdba | 40 | #include "decl.h" |
e8abc66f | 41 | #include "output.h" |
49c249e1 | 42 | #include "except.h" |
54f92bfb | 43 | #include "toplev.h" |
3dcaad8b | 44 | #include "rtl.h" |
297a5329 | 45 | #include "timevar.h" |
325c3691 | 46 | #include "tree-iterator.h" |
53824026 | 47 | #include "vecprim.h" |
49c249e1 | 48 | |
050367a3 MM |
49 | /* The type of functions taking a tree, and some additional data, and |
50 | returning an int. */ | |
3a978d72 | 51 | typedef int (*tree_fn_t) (tree, void*); |
050367a3 | 52 | |
0aafb128 MM |
53 | /* The PENDING_TEMPLATES is a TREE_LIST of templates whose |
54 | instantiations have been deferred, either because their definitions | |
e2c3721c PB |
55 | were not yet available, or because we were putting off doing the work. */ |
56 | struct pending_template GTY (()) { | |
57 | struct pending_template *next; | |
58 | struct tinst_level *tinst; | |
59 | }; | |
60 | ||
61 | static GTY(()) struct pending_template *pending_templates; | |
62 | static GTY(()) struct pending_template *last_pending_template; | |
73aad9b9 | 63 | |
67ffc812 | 64 | int processing_template_parmlist; |
386b8a85 JM |
65 | static int template_header_count; |
66 | ||
e2500fed | 67 | static GTY(()) tree saved_trees; |
53824026 | 68 | static VEC(int,heap) *inline_parm_levels; |
75650646 | 69 | |
e2c3721c | 70 | static GTY(()) struct tinst_level *current_tinst_level; |
3ae18eaf | 71 | |
2b59fc25 KL |
72 | static GTY(()) tree saved_access_scope; |
73 | ||
0fe0caa6 RH |
74 | /* Live only within one (recursive) call to tsubst_expr. We use |
75 | this to pass the statement expression node from the STMT_EXPR | |
76 | to the EXPR_STMT that is its result. */ | |
77 | static tree cur_stmt_expr; | |
78 | ||
6dfbb909 MM |
79 | /* A map from local variable declarations in the body of the template |
80 | presently being instantiated to the corresponding instantiated | |
81 | local variables. */ | |
82 | static htab_t local_specializations; | |
83 | ||
410cf6e6 | 84 | /* Contains canonical template parameter types. The vector is indexed by |
06d40de8 DG |
85 | the TEMPLATE_TYPE_IDX of the template parameter. Each element is a |
86 | TREE_LIST, whose TREE_VALUEs contain the canonical template | |
87 | parameters of various types and levels. */ | |
88 | static GTY(()) VEC(tree,gc) *canonical_template_parms; | |
89 | ||
830bfa74 MM |
90 | #define UNIFY_ALLOW_NONE 0 |
91 | #define UNIFY_ALLOW_MORE_CV_QUAL 1 | |
92 | #define UNIFY_ALLOW_LESS_CV_QUAL 2 | |
93 | #define UNIFY_ALLOW_DERIVED 4 | |
161c12b0 | 94 | #define UNIFY_ALLOW_INTEGER 8 |
028d1f20 | 95 | #define UNIFY_ALLOW_OUTER_LEVEL 16 |
62e4a758 NS |
96 | #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32 |
97 | #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64 | |
830bfa74 | 98 | |
3a978d72 NN |
99 | static void push_access_scope (tree); |
100 | static void pop_access_scope (tree); | |
acde59b4 MM |
101 | static bool resolve_overloaded_unification (tree, tree, tree, tree, |
102 | unification_kind_t, int); | |
3a978d72 | 103 | static int try_one_overload (tree, tree, tree, tree, tree, |
f23fb7f5 | 104 | unification_kind_t, int, bool); |
3a978d72 NN |
105 | static int unify (tree, tree, tree, tree, int); |
106 | static void add_pending_template (tree); | |
aa9d8196 VR |
107 | static int push_tinst_level (tree); |
108 | static void pop_tinst_level (void); | |
e2c3721c | 109 | static tree reopen_tinst_level (struct tinst_level *); |
3a978d72 | 110 | static tree tsubst_initializer_list (tree, tree); |
3a978d72 | 111 | static tree get_class_bindings (tree, tree, tree); |
3db45ab5 | 112 | static tree coerce_template_parms (tree, tree, tree, tsubst_flags_t, |
e7e93965 | 113 | bool, bool); |
3a978d72 NN |
114 | static void tsubst_enum (tree, tree, tree); |
115 | static tree add_to_template_args (tree, tree); | |
116 | static tree add_outermost_template_args (tree, tree); | |
117 | static bool check_instantiated_args (tree, tree, tsubst_flags_t); | |
8af2fec4 RY |
118 | static int maybe_adjust_types_for_deduction (unification_kind_t, tree*, tree*, |
119 | tree); | |
3a978d72 | 120 | static int type_unification_real (tree, tree, tree, tree, |
30f86ec3 | 121 | int, unification_kind_t, int); |
3a978d72 | 122 | static void note_template_header (int); |
b6ab6892 | 123 | static tree convert_nontype_argument_function (tree, tree); |
3a978d72 NN |
124 | static tree convert_nontype_argument (tree, tree); |
125 | static tree convert_template_argument (tree, tree, tree, | |
126 | tsubst_flags_t, int, tree); | |
0c58f841 MA |
127 | static int for_each_template_parm (tree, tree_fn_t, void*, |
128 | struct pointer_set_t*); | |
5d80a306 | 129 | static tree expand_template_argument_pack (tree); |
3a978d72 | 130 | static tree build_template_parm_index (int, int, int, tree, tree); |
ae95e46e | 131 | static bool inline_needs_template_parms (tree); |
3a978d72 | 132 | static void push_inline_template_parms_recursive (tree, int); |
3a978d72 | 133 | static tree retrieve_local_specialization (tree); |
3a978d72 | 134 | static void register_local_specialization (tree, tree); |
ef3b7b17 | 135 | static tree reduce_template_parm_level (tree, tree, int, tree, tsubst_flags_t); |
3a978d72 NN |
136 | static int mark_template_parm (tree, void *); |
137 | static int template_parm_this_level_p (tree, void *); | |
138 | static tree tsubst_friend_function (tree, tree); | |
139 | static tree tsubst_friend_class (tree, tree); | |
140 | static int can_complete_type_without_circularity (tree); | |
a34d3336 | 141 | static tree get_bindings (tree, tree, tree, bool); |
3a978d72 NN |
142 | static int template_decl_level (tree); |
143 | static int check_cv_quals_for_unify (int, tree, tree); | |
5d80a306 DG |
144 | static void template_parm_level_and_index (tree, int*, int*); |
145 | static int unify_pack_expansion (tree, tree, tree, tree, int, bool, bool); | |
a91db711 NS |
146 | static tree tsubst_template_arg (tree, tree, tsubst_flags_t, tree); |
147 | static tree tsubst_template_args (tree, tree, tsubst_flags_t, tree); | |
3a978d72 NN |
148 | static tree tsubst_template_parms (tree, tree, tsubst_flags_t); |
149 | static void regenerate_decl_from_template (tree, tree); | |
3a978d72 | 150 | static tree most_specialized_class (tree, tree); |
3a978d72 | 151 | static tree tsubst_aggr_type (tree, tree, tsubst_flags_t, tree, int); |
3a978d72 NN |
152 | static tree tsubst_arg_types (tree, tree, tsubst_flags_t, tree); |
153 | static tree tsubst_function_type (tree, tree, tsubst_flags_t, tree); | |
9b910171 | 154 | static bool check_specialization_scope (void); |
3a978d72 NN |
155 | static tree process_partial_specialization (tree); |
156 | static void set_current_access_from_decl (tree); | |
3a978d72 | 157 | static tree get_template_base (tree, tree, tree, tree); |
3a978d72 NN |
158 | static tree try_class_unification (tree, tree, tree, tree); |
159 | static int coerce_template_template_parms (tree, tree, tsubst_flags_t, | |
160 | tree, tree); | |
3a978d72 NN |
161 | static int template_args_equal (tree, tree); |
162 | static void tsubst_default_arguments (tree); | |
163 | static tree for_each_template_parm_r (tree *, int *, void *); | |
164 | static tree copy_default_args_to_explicit_spec_1 (tree, tree); | |
165 | static void copy_default_args_to_explicit_spec (tree); | |
166 | static int invalid_nontype_parm_type_p (tree, tsubst_flags_t); | |
a723baf1 | 167 | static int eq_local_specializations (const void *, const void *); |
5d80a306 | 168 | static bool dependent_template_arg_p (tree); |
06d40de8 | 169 | static bool any_template_arguments_need_structural_equality_p (tree); |
5552b43c | 170 | static bool dependent_type_p_r (tree); |
14d22dd6 | 171 | static tree tsubst (tree, tree, tsubst_flags_t, tree); |
015c2c66 | 172 | static tree tsubst_expr (tree, tree, tsubst_flags_t, tree, bool); |
14d22dd6 | 173 | static tree tsubst_copy (tree, tree, tsubst_flags_t, tree); |
5d80a306 | 174 | static tree tsubst_pack_expansion (tree, tree, tsubst_flags_t, tree); |
36a117a5 | 175 | |
2b59fc25 KL |
176 | /* Make the current scope suitable for access checking when we are |
177 | processing T. T can be FUNCTION_DECL for instantiated function | |
2b907f5c KL |
178 | template, or VAR_DECL for static member variable (need by |
179 | instantiate_decl). */ | |
2b59fc25 | 180 | |
8ce33230 | 181 | static void |
2b907f5c | 182 | push_access_scope (tree t) |
2b59fc25 | 183 | { |
50bc768d NS |
184 | gcc_assert (TREE_CODE (t) == FUNCTION_DECL |
185 | || TREE_CODE (t) == VAR_DECL); | |
2b59fc25 | 186 | |
23ff7e2d KL |
187 | if (DECL_FRIEND_CONTEXT (t)) |
188 | push_nested_class (DECL_FRIEND_CONTEXT (t)); | |
189 | else if (DECL_CLASS_SCOPE_P (t)) | |
2b907f5c | 190 | push_nested_class (DECL_CONTEXT (t)); |
0f399e5f KL |
191 | else |
192 | push_to_top_level (); | |
c8094d83 | 193 | |
2b907f5c | 194 | if (TREE_CODE (t) == FUNCTION_DECL) |
0f399e5f KL |
195 | { |
196 | saved_access_scope = tree_cons | |
197 | (NULL_TREE, current_function_decl, saved_access_scope); | |
198 | current_function_decl = t; | |
199 | } | |
2b59fc25 KL |
200 | } |
201 | ||
2b59fc25 KL |
202 | /* Restore the scope set up by push_access_scope. T is the node we |
203 | are processing. */ | |
204 | ||
8ce33230 | 205 | static void |
3a978d72 | 206 | pop_access_scope (tree t) |
2b59fc25 | 207 | { |
2b907f5c | 208 | if (TREE_CODE (t) == FUNCTION_DECL) |
2b59fc25 KL |
209 | { |
210 | current_function_decl = TREE_VALUE (saved_access_scope); | |
211 | saved_access_scope = TREE_CHAIN (saved_access_scope); | |
212 | } | |
0f399e5f | 213 | |
23ff7e2d | 214 | if (DECL_FRIEND_CONTEXT (t) || DECL_CLASS_SCOPE_P (t)) |
0f399e5f KL |
215 | pop_nested_class (); |
216 | else | |
217 | pop_from_top_level (); | |
2b59fc25 KL |
218 | } |
219 | ||
a723baf1 MM |
220 | /* Do any processing required when DECL (a member template |
221 | declaration) is finished. Returns the TEMPLATE_DECL corresponding | |
222 | to DECL, unless it is a specialization, in which case the DECL | |
223 | itself is returned. */ | |
e1467ff2 MM |
224 | |
225 | tree | |
3a978d72 | 226 | finish_member_template_decl (tree decl) |
e1467ff2 | 227 | { |
a723baf1 MM |
228 | if (decl == error_mark_node) |
229 | return error_mark_node; | |
230 | ||
50bc768d | 231 | gcc_assert (DECL_P (decl)); |
a723baf1 MM |
232 | |
233 | if (TREE_CODE (decl) == TYPE_DECL) | |
93cdc044 | 234 | { |
a723baf1 MM |
235 | tree type; |
236 | ||
237 | type = TREE_TYPE (decl); | |
c8094d83 | 238 | if (IS_AGGR_TYPE (type) |
a723baf1 MM |
239 | && CLASSTYPE_TEMPLATE_INFO (type) |
240 | && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type)) | |
93cdc044 | 241 | { |
a723baf1 | 242 | tree tmpl = CLASSTYPE_TI_TEMPLATE (type); |
93cdc044 JM |
243 | check_member_template (tmpl); |
244 | return tmpl; | |
245 | } | |
8d019cef | 246 | return NULL_TREE; |
93cdc044 | 247 | } |
07c88314 | 248 | else if (TREE_CODE (decl) == FIELD_DECL) |
0f51ccfc | 249 | error ("data member %qD cannot be a member template", decl); |
a1da6cba | 250 | else if (DECL_TEMPLATE_INFO (decl)) |
e1467ff2 | 251 | { |
a1da6cba MM |
252 | if (!DECL_TEMPLATE_SPECIALIZATION (decl)) |
253 | { | |
254 | check_member_template (DECL_TI_TEMPLATE (decl)); | |
255 | return DECL_TI_TEMPLATE (decl); | |
256 | } | |
257 | else | |
258 | return decl; | |
c8094d83 | 259 | } |
a1da6cba | 260 | else |
0f51ccfc | 261 | error ("invalid member template declaration %qD", decl); |
e1467ff2 | 262 | |
a1da6cba | 263 | return error_mark_node; |
f84b4be9 | 264 | } |
e1467ff2 | 265 | |
91a77d68 JM |
266 | /* Return the template info node corresponding to T, whatever T is. */ |
267 | ||
268 | tree | |
269 | get_template_info (tree t) | |
270 | { | |
271 | tree tinfo = NULL_TREE; | |
272 | ||
273 | if (DECL_P (t) && DECL_LANG_SPECIFIC (t)) | |
274 | tinfo = DECL_TEMPLATE_INFO (t); | |
275 | ||
276 | if (!tinfo && TREE_CODE (t) == TYPE_DECL) | |
277 | t = TREE_TYPE (t); | |
278 | ||
279 | if (TAGGED_TYPE_P (t)) | |
280 | tinfo = TYPE_TEMPLATE_INFO (t); | |
281 | ||
282 | return tinfo; | |
283 | } | |
284 | ||
f84b4be9 | 285 | /* Returns the template nesting level of the indicated class TYPE. |
c8094d83 | 286 | |
f84b4be9 JM |
287 | For example, in: |
288 | template <class T> | |
289 | struct A | |
290 | { | |
291 | template <class U> | |
292 | struct B {}; | |
293 | }; | |
294 | ||
c8094d83 | 295 | A<T>::B<U> has depth two, while A<T> has depth one. |
39c01e4c | 296 | Both A<T>::B<int> and A<int>::B<U> have depth one, if |
260cd73f | 297 | they are instantiations, not specializations. |
39c01e4c MM |
298 | |
299 | This function is guaranteed to return 0 if passed NULL_TREE so | |
300 | that, for example, `template_class_depth (current_class_type)' is | |
301 | always safe. */ | |
f84b4be9 | 302 | |
260cd73f VR |
303 | int |
304 | template_class_depth (tree type) | |
f84b4be9 | 305 | { |
93cdc044 | 306 | int depth; |
f84b4be9 | 307 | |
c8094d83 | 308 | for (depth = 0; |
ed44da02 | 309 | type && TREE_CODE (type) != NAMESPACE_DECL; |
c8094d83 | 310 | type = (TREE_CODE (type) == FUNCTION_DECL) |
4f1c5b7d | 311 | ? CP_DECL_CONTEXT (type) : TYPE_CONTEXT (type)) |
ed44da02 | 312 | { |
91a77d68 JM |
313 | tree tinfo = get_template_info (type); |
314 | ||
315 | if (tinfo && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo)) | |
316 | && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo)))) | |
317 | ++depth; | |
ed44da02 | 318 | } |
f84b4be9 JM |
319 | |
320 | return depth; | |
e1467ff2 | 321 | } |
98c1c668 | 322 | |
ae95e46e PC |
323 | /* Subroutine of maybe_begin_member_template_processing. |
324 | Returns true if processing DECL needs us to push template parms. */ | |
cae40af6 | 325 | |
ae95e46e | 326 | static bool |
3a978d72 | 327 | inline_needs_template_parms (tree decl) |
cae40af6 JM |
328 | { |
329 | if (! DECL_TEMPLATE_INFO (decl)) | |
ae95e46e | 330 | return false; |
f84b4be9 | 331 | |
36a117a5 | 332 | return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl))) |
cae40af6 JM |
333 | > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl))); |
334 | } | |
335 | ||
336 | /* Subroutine of maybe_begin_member_template_processing. | |
337 | Push the template parms in PARMS, starting from LEVELS steps into the | |
338 | chain, and ending at the beginning, since template parms are listed | |
339 | innermost first. */ | |
340 | ||
341 | static void | |
3a978d72 | 342 | push_inline_template_parms_recursive (tree parmlist, int levels) |
cae40af6 JM |
343 | { |
344 | tree parms = TREE_VALUE (parmlist); | |
345 | int i; | |
346 | ||
347 | if (levels > 1) | |
348 | push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1); | |
786b5245 | 349 | |
98c1c668 | 350 | ++processing_template_decl; |
cae40af6 | 351 | current_template_parms |
4890c2f4 | 352 | = tree_cons (size_int (processing_template_decl), |
98c1c668 | 353 | parms, current_template_parms); |
cae40af6 JM |
354 | TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1; |
355 | ||
ac20c67a | 356 | begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec, |
0cbd7506 | 357 | NULL); |
c8094d83 | 358 | for (i = 0; i < TREE_VEC_LENGTH (parms); ++i) |
98c1c668 | 359 | { |
0f67a82f | 360 | tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i)); |
42b304f1 | 361 | |
0f67a82f LM |
362 | if (parm == error_mark_node) |
363 | continue; | |
42b304f1 | 364 | |
50bc768d | 365 | gcc_assert (DECL_P (parm)); |
cae40af6 | 366 | |
98c1c668 JM |
367 | switch (TREE_CODE (parm)) |
368 | { | |
786b5245 | 369 | case TYPE_DECL: |
73b0fce8 | 370 | case TEMPLATE_DECL: |
98c1c668 JM |
371 | pushdecl (parm); |
372 | break; | |
786b5245 MM |
373 | |
374 | case PARM_DECL: | |
375 | { | |
fc03edb3 MM |
376 | /* Make a CONST_DECL as is done in process_template_parm. |
377 | It is ugly that we recreate this here; the original | |
378 | version built in process_template_parm is no longer | |
379 | available. */ | |
786b5245 MM |
380 | tree decl = build_decl (CONST_DECL, DECL_NAME (parm), |
381 | TREE_TYPE (parm)); | |
c727aa5e | 382 | DECL_ARTIFICIAL (decl) = 1; |
6de9cd9a DN |
383 | TREE_CONSTANT (decl) = 1; |
384 | TREE_INVARIANT (decl) = 1; | |
385 | TREE_READONLY (decl) = 1; | |
786b5245 | 386 | DECL_INITIAL (decl) = DECL_INITIAL (parm); |
cd9f6678 | 387 | SET_DECL_TEMPLATE_PARM_P (decl); |
786b5245 MM |
388 | pushdecl (decl); |
389 | } | |
cae40af6 | 390 | break; |
786b5245 | 391 | |
98c1c668 | 392 | default: |
315fb5db | 393 | gcc_unreachable (); |
98c1c668 JM |
394 | } |
395 | } | |
396 | } | |
397 | ||
cae40af6 JM |
398 | /* Restore the template parameter context for a member template or |
399 | a friend template defined in a class definition. */ | |
400 | ||
401 | void | |
3a978d72 | 402 | maybe_begin_member_template_processing (tree decl) |
cae40af6 JM |
403 | { |
404 | tree parms; | |
3dcaad8b | 405 | int levels = 0; |
cae40af6 | 406 | |
3dcaad8b MM |
407 | if (inline_needs_template_parms (decl)) |
408 | { | |
409 | parms = DECL_TEMPLATE_PARMS (most_general_template (decl)); | |
410 | levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl; | |
cae40af6 | 411 | |
3dcaad8b MM |
412 | if (DECL_TEMPLATE_SPECIALIZATION (decl)) |
413 | { | |
414 | --levels; | |
415 | parms = TREE_CHAIN (parms); | |
416 | } | |
cae40af6 | 417 | |
3dcaad8b | 418 | push_inline_template_parms_recursive (parms, levels); |
cae40af6 JM |
419 | } |
420 | ||
3dcaad8b MM |
421 | /* Remember how many levels of template parameters we pushed so that |
422 | we can pop them later. */ | |
53824026 | 423 | VEC_safe_push (int, heap, inline_parm_levels, levels); |
cae40af6 JM |
424 | } |
425 | ||
1875c2b7 | 426 | /* Undo the effects of maybe_begin_member_template_processing. */ |
98c1c668 | 427 | |
c8094d83 | 428 | void |
3a978d72 | 429 | maybe_end_member_template_processing (void) |
98c1c668 | 430 | { |
3dcaad8b | 431 | int i; |
53824026 | 432 | int last; |
3dcaad8b | 433 | |
53824026 | 434 | if (VEC_length (int, inline_parm_levels) == 0) |
98c1c668 JM |
435 | return; |
436 | ||
53824026 KH |
437 | last = VEC_pop (int, inline_parm_levels); |
438 | for (i = 0; i < last; ++i) | |
cae40af6 JM |
439 | { |
440 | --processing_template_decl; | |
441 | current_template_parms = TREE_CHAIN (current_template_parms); | |
442 | poplevel (0, 0, 0); | |
443 | } | |
98c1c668 JM |
444 | } |
445 | ||
36a117a5 | 446 | /* Return a new template argument vector which contains all of ARGS, |
f9a7ae04 | 447 | but has as its innermost set of arguments the EXTRA_ARGS. */ |
e6f1275f JM |
448 | |
449 | static tree | |
3a978d72 | 450 | add_to_template_args (tree args, tree extra_args) |
e6f1275f | 451 | { |
36a117a5 MM |
452 | tree new_args; |
453 | int extra_depth; | |
454 | int i; | |
455 | int j; | |
e6f1275f | 456 | |
36a117a5 | 457 | extra_depth = TMPL_ARGS_DEPTH (extra_args); |
f31c0a32 | 458 | new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth); |
e6f1275f | 459 | |
36a117a5 MM |
460 | for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i) |
461 | SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i)); | |
e6f1275f | 462 | |
36a117a5 MM |
463 | for (j = 1; j <= extra_depth; ++j, ++i) |
464 | SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j)); | |
c8094d83 | 465 | |
e6f1275f JM |
466 | return new_args; |
467 | } | |
468 | ||
36a117a5 MM |
469 | /* Like add_to_template_args, but only the outermost ARGS are added to |
470 | the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH | |
471 | (EXTRA_ARGS) levels are added. This function is used to combine | |
472 | the template arguments from a partial instantiation with the | |
473 | template arguments used to attain the full instantiation from the | |
474 | partial instantiation. */ | |
98c1c668 | 475 | |
4966381a | 476 | static tree |
3a978d72 | 477 | add_outermost_template_args (tree args, tree extra_args) |
98c1c668 JM |
478 | { |
479 | tree new_args; | |
480 | ||
e4a84209 MM |
481 | /* If there are more levels of EXTRA_ARGS than there are ARGS, |
482 | something very fishy is going on. */ | |
50bc768d | 483 | gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args)); |
e4a84209 MM |
484 | |
485 | /* If *all* the new arguments will be the EXTRA_ARGS, just return | |
486 | them. */ | |
487 | if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args)) | |
488 | return extra_args; | |
489 | ||
36a117a5 MM |
490 | /* For the moment, we make ARGS look like it contains fewer levels. */ |
491 | TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args); | |
c8094d83 | 492 | |
36a117a5 | 493 | new_args = add_to_template_args (args, extra_args); |
98c1c668 | 494 | |
36a117a5 MM |
495 | /* Now, we restore ARGS to its full dimensions. */ |
496 | TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args); | |
98c1c668 JM |
497 | |
498 | return new_args; | |
499 | } | |
5566b478 | 500 | |
f9a7ae04 MM |
501 | /* Return the N levels of innermost template arguments from the ARGS. */ |
502 | ||
503 | tree | |
3a978d72 | 504 | get_innermost_template_args (tree args, int n) |
f9a7ae04 MM |
505 | { |
506 | tree new_args; | |
507 | int extra_levels; | |
508 | int i; | |
509 | ||
50bc768d | 510 | gcc_assert (n >= 0); |
f9a7ae04 MM |
511 | |
512 | /* If N is 1, just return the innermost set of template arguments. */ | |
513 | if (n == 1) | |
514 | return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args)); | |
c8094d83 | 515 | |
f9a7ae04 MM |
516 | /* If we're not removing anything, just return the arguments we were |
517 | given. */ | |
518 | extra_levels = TMPL_ARGS_DEPTH (args) - n; | |
50bc768d | 519 | gcc_assert (extra_levels >= 0); |
f9a7ae04 MM |
520 | if (extra_levels == 0) |
521 | return args; | |
522 | ||
523 | /* Make a new set of arguments, not containing the outer arguments. */ | |
524 | new_args = make_tree_vec (n); | |
525 | for (i = 1; i <= n; ++i) | |
c8094d83 | 526 | SET_TMPL_ARGS_LEVEL (new_args, i, |
f9a7ae04 MM |
527 | TMPL_ARGS_LEVEL (args, i + extra_levels)); |
528 | ||
529 | return new_args; | |
530 | } | |
531 | ||
dc28490d JM |
532 | /* The inverse of get_innermost_template_args: Return all but the innermost |
533 | EXTRA_LEVELS levels of template arguments from the ARGS. */ | |
534 | ||
535 | static tree | |
536 | strip_innermost_template_args (tree args, int extra_levels) | |
537 | { | |
538 | tree new_args; | |
539 | int n = TMPL_ARGS_DEPTH (args) - extra_levels; | |
540 | int i; | |
541 | ||
542 | gcc_assert (n >= 0); | |
543 | ||
544 | /* If N is 1, just return the outermost set of template arguments. */ | |
545 | if (n == 1) | |
546 | return TMPL_ARGS_LEVEL (args, 1); | |
547 | ||
548 | /* If we're not removing anything, just return the arguments we were | |
549 | given. */ | |
550 | gcc_assert (extra_levels >= 0); | |
551 | if (extra_levels == 0) | |
552 | return args; | |
553 | ||
554 | /* Make a new set of arguments, not containing the inner arguments. */ | |
555 | new_args = make_tree_vec (n); | |
556 | for (i = 1; i <= n; ++i) | |
557 | SET_TMPL_ARGS_LEVEL (new_args, i, | |
558 | TMPL_ARGS_LEVEL (args, i)); | |
559 | ||
560 | return new_args; | |
561 | } | |
562 | ||
5566b478 MS |
563 | /* We've got a template header coming up; push to a new level for storing |
564 | the parms. */ | |
8d08fdba | 565 | |
8d08fdba | 566 | void |
3a978d72 | 567 | begin_template_parm_list (void) |
8d08fdba | 568 | { |
6757edfe MM |
569 | /* We use a non-tag-transparent scope here, which causes pushtag to |
570 | put tags in this scope, rather than in the enclosing class or | |
571 | namespace scope. This is the right thing, since we want | |
572 | TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a | |
573 | global template class, push_template_decl handles putting the | |
574 | TEMPLATE_DECL into top-level scope. For a nested template class, | |
575 | e.g.: | |
576 | ||
577 | template <class T> struct S1 { | |
0cbd7506 | 578 | template <class T> struct S2 {}; |
6757edfe MM |
579 | }; |
580 | ||
581 | pushtag contains special code to call pushdecl_with_scope on the | |
582 | TEMPLATE_DECL for S2. */ | |
ac20c67a | 583 | begin_scope (sk_template_parms, NULL); |
5156628f | 584 | ++processing_template_decl; |
67ffc812 | 585 | ++processing_template_parmlist; |
386b8a85 JM |
586 | note_template_header (0); |
587 | } | |
588 | ||
6c30752f | 589 | /* This routine is called when a specialization is declared. If it is |
9b910171 LM |
590 | invalid to declare a specialization here, an error is reported and |
591 | false is returned, otherwise this routine will return true. */ | |
6c30752f | 592 | |
9b910171 | 593 | static bool |
3a978d72 | 594 | check_specialization_scope (void) |
6c30752f MM |
595 | { |
596 | tree scope = current_scope (); | |
3ddfb0e6 | 597 | |
c8094d83 MS |
598 | /* [temp.expl.spec] |
599 | ||
6c30752f MM |
600 | An explicit specialization shall be declared in the namespace of |
601 | which the template is a member, or, for member templates, in the | |
602 | namespace of which the enclosing class or enclosing class | |
603 | template is a member. An explicit specialization of a member | |
604 | function, member class or static data member of a class template | |
605 | shall be declared in the namespace of which the class template | |
606 | is a member. */ | |
607 | if (scope && TREE_CODE (scope) != NAMESPACE_DECL) | |
9b910171 LM |
608 | { |
609 | error ("explicit specialization in non-namespace scope %qD", scope); | |
610 | return false; | |
611 | } | |
3ddfb0e6 | 612 | |
c8094d83 | 613 | /* [temp.expl.spec] |
6c30752f MM |
614 | |
615 | In an explicit specialization declaration for a member of a class | |
616 | template or a member template that appears in namespace scope, | |
617 | the member template and some of its enclosing class templates may | |
618 | remain unspecialized, except that the declaration shall not | |
619 | explicitly specialize a class member template if its enclosing | |
620 | class templates are not explicitly specialized as well. */ | |
c8094d83 | 621 | if (current_template_parms) |
9b910171 LM |
622 | { |
623 | error ("enclosing class templates are not explicitly specialized"); | |
624 | return false; | |
625 | } | |
626 | ||
627 | return true; | |
6c30752f MM |
628 | } |
629 | ||
c6002625 | 630 | /* We've just seen template <>. */ |
386b8a85 | 631 | |
9b910171 | 632 | bool |
3a978d72 | 633 | begin_specialization (void) |
386b8a85 | 634 | { |
ac20c67a | 635 | begin_scope (sk_template_spec, NULL); |
386b8a85 | 636 | note_template_header (1); |
9b910171 | 637 | return check_specialization_scope (); |
386b8a85 JM |
638 | } |
639 | ||
dc957d14 | 640 | /* Called at then end of processing a declaration preceded by |
386b8a85 JM |
641 | template<>. */ |
642 | ||
c8094d83 | 643 | void |
3a978d72 | 644 | end_specialization (void) |
386b8a85 | 645 | { |
74b846e0 | 646 | finish_scope (); |
386b8a85 JM |
647 | reset_specialization (); |
648 | } | |
649 | ||
386b8a85 | 650 | /* Any template <>'s that we have seen thus far are not referring to a |
c6002625 | 651 | function specialization. */ |
386b8a85 JM |
652 | |
653 | void | |
3a978d72 | 654 | reset_specialization (void) |
386b8a85 JM |
655 | { |
656 | processing_specialization = 0; | |
657 | template_header_count = 0; | |
658 | } | |
659 | ||
838dfd8a | 660 | /* We've just seen a template header. If SPECIALIZATION is nonzero, |
386b8a85 JM |
661 | it was of the form template <>. */ |
662 | ||
c8094d83 | 663 | static void |
3a978d72 | 664 | note_template_header (int specialization) |
386b8a85 JM |
665 | { |
666 | processing_specialization = specialization; | |
667 | template_header_count++; | |
668 | } | |
669 | ||
75650646 | 670 | /* We're beginning an explicit instantiation. */ |
386b8a85 | 671 | |
75650646 | 672 | void |
3a978d72 | 673 | begin_explicit_instantiation (void) |
386b8a85 | 674 | { |
50bc768d | 675 | gcc_assert (!processing_explicit_instantiation); |
a723baf1 | 676 | processing_explicit_instantiation = true; |
75650646 | 677 | } |
386b8a85 | 678 | |
386b8a85 | 679 | |
75650646 | 680 | void |
3a978d72 | 681 | end_explicit_instantiation (void) |
75650646 | 682 | { |
50bc768d | 683 | gcc_assert (processing_explicit_instantiation); |
a723baf1 | 684 | processing_explicit_instantiation = false; |
75650646 | 685 | } |
386b8a85 | 686 | |
13a44ee0 | 687 | /* An explicit specialization or partial specialization TMPL is being |
b1cc95ce MM |
688 | declared. Check that the namespace in which the specialization is |
689 | occurring is permissible. Returns false iff it is invalid to | |
690 | specialize TMPL in the current namespace. */ | |
c8094d83 | 691 | |
b1cc95ce MM |
692 | static bool |
693 | check_specialization_namespace (tree tmpl) | |
694 | { | |
695 | tree tpl_ns = decl_namespace_context (tmpl); | |
696 | ||
697 | /* [tmpl.expl.spec] | |
c8094d83 | 698 | |
b1cc95ce MM |
699 | An explicit specialization shall be declared in the namespace of |
700 | which the template is a member, or, for member templates, in the | |
701 | namespace of which the enclosing class or enclosing class | |
702 | template is a member. An explicit specialization of a member | |
703 | function, member class or static data member of a class template | |
704 | shall be declared in the namespace of which the class template is | |
705 | a member. */ | |
706 | if (is_associated_namespace (current_namespace, tpl_ns)) | |
707 | /* Same or super-using namespace. */ | |
708 | return true; | |
709 | else | |
710 | { | |
0f51ccfc | 711 | pedwarn ("specialization of %qD in different namespace", tmpl); |
dee15844 | 712 | pedwarn (" from definition of %q+#D", tmpl); |
b1cc95ce MM |
713 | return false; |
714 | } | |
715 | } | |
716 | ||
9c12301f MM |
717 | /* SPEC is an explicit instantiation. Check that it is valid to |
718 | perform this explicit instantiation in the current namespace. */ | |
719 | ||
720 | static void | |
721 | check_explicit_instantiation_namespace (tree spec) | |
722 | { | |
723 | tree ns; | |
724 | ||
725 | /* DR 275: An explicit instantiation shall appear in an enclosing | |
3db45ab5 | 726 | namespace of its template. */ |
9c12301f MM |
727 | ns = decl_namespace_context (spec); |
728 | if (!is_ancestor (current_namespace, ns)) | |
729 | pedwarn ("explicit instantiation of %qD in namespace %qD " | |
312ea236 | 730 | "(which does not enclose namespace %qD)", |
9c12301f MM |
731 | spec, current_namespace, ns); |
732 | } | |
733 | ||
36a117a5 MM |
734 | /* The TYPE is being declared. If it is a template type, that means it |
735 | is a partial specialization. Do appropriate error-checking. */ | |
736 | ||
9ce387d9 | 737 | tree |
3a978d72 | 738 | maybe_process_partial_specialization (tree type) |
36a117a5 | 739 | { |
c9043d03 VR |
740 | tree context; |
741 | ||
742 | if (type == error_mark_node) | |
9ce387d9 VR |
743 | return error_mark_node; |
744 | ||
745 | if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM) | |
746 | { | |
747 | error ("name of class shadows template template parameter %qD", | |
748 | TYPE_NAME (type)); | |
749 | return error_mark_node; | |
750 | } | |
c9043d03 VR |
751 | |
752 | context = TYPE_CONTEXT (type); | |
6e049fcd | 753 | |
a723baf1 | 754 | if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type)) |
36a117a5 | 755 | { |
6e049fcd KL |
756 | /* This is for ordinary explicit specialization and partial |
757 | specialization of a template class such as: | |
758 | ||
759 | template <> class C<int>; | |
760 | ||
761 | or: | |
762 | ||
763 | template <class T> class C<T*>; | |
764 | ||
765 | Make sure that `C<int>' and `C<T*>' are implicit instantiations. */ | |
766 | ||
36a117a5 | 767 | if (CLASSTYPE_IMPLICIT_INSTANTIATION (type) |
d0f062fb | 768 | && !COMPLETE_TYPE_P (type)) |
36a117a5 | 769 | { |
b1cc95ce | 770 | check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (type)); |
370af2d5 | 771 | SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type); |
36a117a5 MM |
772 | if (processing_template_decl) |
773 | push_template_decl (TYPE_MAIN_DECL (type)); | |
774 | } | |
775 | else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type)) | |
0f51ccfc | 776 | error ("specialization of %qT after instantiation", type); |
36a117a5 | 777 | } |
6e049fcd KL |
778 | else if (CLASS_TYPE_P (type) |
779 | && !CLASSTYPE_USE_TEMPLATE (type) | |
780 | && CLASSTYPE_TEMPLATE_INFO (type) | |
781 | && context && CLASS_TYPE_P (context) | |
782 | && CLASSTYPE_TEMPLATE_INFO (context)) | |
783 | { | |
784 | /* This is for an explicit specialization of member class | |
785 | template according to [temp.expl.spec/18]: | |
786 | ||
787 | template <> template <class U> class C<int>::D; | |
788 | ||
789 | The context `C<int>' must be an implicit instantiation. | |
790 | Otherwise this is just a member class template declared | |
791 | earlier like: | |
792 | ||
793 | template <> class C<int> { template <class U> class D; }; | |
794 | template <> template <class U> class C<int>::D; | |
795 | ||
796 | In the first case, `C<int>::D' is a specialization of `C<T>::D' | |
797 | while in the second case, `C<int>::D' is a primary template | |
798 | and `C<T>::D' may not exist. */ | |
799 | ||
800 | if (CLASSTYPE_IMPLICIT_INSTANTIATION (context) | |
801 | && !COMPLETE_TYPE_P (type)) | |
802 | { | |
803 | tree t; | |
804 | ||
805 | if (current_namespace | |
806 | != decl_namespace_context (CLASSTYPE_TI_TEMPLATE (type))) | |
807 | { | |
0f51ccfc | 808 | pedwarn ("specializing %q#T in different namespace", type); |
dee15844 JM |
809 | pedwarn (" from definition of %q+#D", |
810 | CLASSTYPE_TI_TEMPLATE (type)); | |
6e049fcd KL |
811 | } |
812 | ||
813 | /* Check for invalid specialization after instantiation: | |
814 | ||
815 | template <> template <> class C<int>::D<int>; | |
816 | template <> template <class U> class C<int>::D; */ | |
817 | ||
818 | for (t = DECL_TEMPLATE_INSTANTIATIONS | |
819 | (most_general_template (CLASSTYPE_TI_TEMPLATE (type))); | |
820 | t; t = TREE_CHAIN (t)) | |
821 | if (TREE_VALUE (t) != type | |
822 | && TYPE_CONTEXT (TREE_VALUE (t)) == context) | |
0f51ccfc | 823 | error ("specialization %qT after instantiation %qT", |
6e049fcd KL |
824 | type, TREE_VALUE (t)); |
825 | ||
826 | /* Mark TYPE as a specialization. And as a result, we only | |
827 | have one level of template argument for the innermost | |
828 | class template. */ | |
829 | SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type); | |
830 | CLASSTYPE_TI_ARGS (type) | |
831 | = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)); | |
832 | } | |
833 | } | |
20496fa2 | 834 | else if (processing_specialization) |
9ce387d9 VR |
835 | { |
836 | error ("explicit specialization of non-template %qT", type); | |
837 | return error_mark_node; | |
838 | } | |
839 | ||
840 | return type; | |
36a117a5 MM |
841 | } |
842 | ||
ee81147e | 843 | /* Returns nonzero if we can optimize the retrieval of specializations |
c7222c02 MM |
844 | for TMPL, a TEMPLATE_DECL. In particular, for such a template, we |
845 | do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */ | |
846 | ||
c8094d83 | 847 | static inline bool |
c7222c02 MM |
848 | optimize_specialization_lookup_p (tree tmpl) |
849 | { | |
850 | return (DECL_FUNCTION_TEMPLATE_P (tmpl) | |
851 | && DECL_CLASS_SCOPE_P (tmpl) | |
852 | /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template | |
853 | parameter. */ | |
854 | && CLASS_TYPE_P (DECL_CONTEXT (tmpl)) | |
1ca939e5 MM |
855 | /* The optimized lookup depends on the fact that the |
856 | template arguments for the member function template apply | |
857 | purely to the containing class, which is not true if the | |
858 | containing class is an explicit or partial | |
859 | specialization. */ | |
860 | && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl)) | |
c7222c02 MM |
861 | && !DECL_MEMBER_TEMPLATE_P (tmpl) |
862 | && !DECL_CONV_FN_P (tmpl) | |
863 | /* It is possible to have a template that is not a member | |
864 | template and is not a member of a template class: | |
c8094d83 MS |
865 | |
866 | template <typename T> | |
c7222c02 | 867 | struct S { friend A::f(); }; |
c8094d83 | 868 | |
c7222c02 MM |
869 | Here, the friend function is a template, but the context does |
870 | not have template information. The optimized lookup relies | |
871 | on having ARGS be the template arguments for both the class | |
872 | and the function template. */ | |
873 | && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl))); | |
874 | } | |
875 | ||
75650646 MM |
876 | /* Retrieve the specialization (in the sense of [temp.spec] - a |
877 | specialization is either an instantiation or an explicit | |
878 | specialization) of TMPL for the given template ARGS. If there is | |
879 | no such specialization, return NULL_TREE. The ARGS are a vector of | |
880 | arguments, or a vector of vectors of arguments, in the case of | |
c8094d83 | 881 | templates with more than one level of parameters. |
c7222c02 MM |
882 | |
883 | If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true, | |
884 | then we search for a partial specialization matching ARGS. This | |
885 | parameter is ignored if TMPL is not a class template. */ | |
c8094d83 | 886 | |
75650646 | 887 | static tree |
c8094d83 | 888 | retrieve_specialization (tree tmpl, tree args, |
c7222c02 | 889 | bool class_specializations_p) |
75650646 | 890 | { |
2d8ba2c7 LM |
891 | if (args == error_mark_node) |
892 | return NULL_TREE; | |
893 | ||
50bc768d | 894 | gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL); |
75650646 | 895 | |
36a117a5 MM |
896 | /* There should be as many levels of arguments as there are |
897 | levels of parameters. */ | |
c8094d83 | 898 | gcc_assert (TMPL_ARGS_DEPTH (args) |
50bc768d | 899 | == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl))); |
c8094d83 | 900 | |
c7222c02 MM |
901 | if (optimize_specialization_lookup_p (tmpl)) |
902 | { | |
903 | tree class_template; | |
904 | tree class_specialization; | |
d4e6fecb | 905 | VEC(tree,gc) *methods; |
c7222c02 MM |
906 | tree fns; |
907 | int idx; | |
908 | ||
909 | /* The template arguments actually apply to the containing | |
910 | class. Find the class specialization with those | |
911 | arguments. */ | |
912 | class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl)); | |
c8094d83 | 913 | class_specialization |
c7222c02 MM |
914 | = retrieve_specialization (class_template, args, |
915 | /*class_specializations_p=*/false); | |
916 | if (!class_specialization) | |
917 | return NULL_TREE; | |
918 | /* Now, find the appropriate entry in the CLASSTYPE_METHOD_VEC | |
919 | for the specialization. */ | |
920 | idx = class_method_index_for_fn (class_specialization, tmpl); | |
921 | if (idx == -1) | |
922 | return NULL_TREE; | |
923 | /* Iterate through the methods with the indicated name, looking | |
924 | for the one that has an instance of TMPL. */ | |
925 | methods = CLASSTYPE_METHOD_VEC (class_specialization); | |
926 | for (fns = VEC_index (tree, methods, idx); fns; fns = OVL_NEXT (fns)) | |
927 | { | |
928 | tree fn = OVL_CURRENT (fns); | |
929 | if (DECL_TEMPLATE_INFO (fn) && DECL_TI_TEMPLATE (fn) == tmpl) | |
930 | return fn; | |
931 | } | |
932 | return NULL_TREE; | |
933 | } | |
934 | else | |
935 | { | |
936 | tree *sp; | |
937 | tree *head; | |
938 | ||
939 | /* Class templates store their instantiations on the | |
940 | DECL_TEMPLATE_INSTANTIATIONS list; other templates use the | |
941 | DECL_TEMPLATE_SPECIALIZATIONS list. */ | |
942 | if (!class_specializations_p | |
56d0c6e3 JM |
943 | && TREE_CODE (DECL_TEMPLATE_RESULT (tmpl)) == TYPE_DECL |
944 | && TAGGED_TYPE_P (TREE_TYPE (tmpl))) | |
c7222c02 MM |
945 | sp = &DECL_TEMPLATE_INSTANTIATIONS (tmpl); |
946 | else | |
947 | sp = &DECL_TEMPLATE_SPECIALIZATIONS (tmpl); | |
948 | head = sp; | |
949 | /* Iterate through the list until we find a matching template. */ | |
950 | while (*sp != NULL_TREE) | |
951 | { | |
952 | tree spec = *sp; | |
c8094d83 | 953 | |
c7222c02 MM |
954 | if (comp_template_args (TREE_PURPOSE (spec), args)) |
955 | { | |
956 | /* Use the move-to-front heuristic to speed up future | |
c8094d83 | 957 | searches. */ |
c7222c02 MM |
958 | if (spec != *head) |
959 | { | |
960 | *sp = TREE_CHAIN (*sp); | |
961 | TREE_CHAIN (spec) = *head; | |
962 | *head = spec; | |
963 | } | |
964 | return TREE_VALUE (spec); | |
965 | } | |
966 | sp = &TREE_CHAIN (spec); | |
967 | } | |
968 | } | |
75650646 MM |
969 | |
970 | return NULL_TREE; | |
386b8a85 JM |
971 | } |
972 | ||
dc957d14 | 973 | /* Like retrieve_specialization, but for local declarations. */ |
9188c363 MM |
974 | |
975 | static tree | |
3a978d72 | 976 | retrieve_local_specialization (tree tmpl) |
9188c363 | 977 | { |
67f5655f | 978 | tree spec = (tree) htab_find_with_hash (local_specializations, tmpl, |
3db45ab5 | 979 | htab_hash_pointer (tmpl)); |
a723baf1 | 980 | return spec ? TREE_PURPOSE (spec) : NULL_TREE; |
9188c363 MM |
981 | } |
982 | ||
838dfd8a | 983 | /* Returns nonzero iff DECL is a specialization of TMPL. */ |
6757edfe MM |
984 | |
985 | int | |
3a978d72 | 986 | is_specialization_of (tree decl, tree tmpl) |
6757edfe MM |
987 | { |
988 | tree t; | |
989 | ||
990 | if (TREE_CODE (decl) == FUNCTION_DECL) | |
991 | { | |
c8094d83 | 992 | for (t = decl; |
6757edfe MM |
993 | t != NULL_TREE; |
994 | t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE) | |
995 | if (t == tmpl) | |
996 | return 1; | |
997 | } | |
c8094d83 | 998 | else |
6757edfe | 999 | { |
50bc768d | 1000 | gcc_assert (TREE_CODE (decl) == TYPE_DECL); |
6757edfe MM |
1001 | |
1002 | for (t = TREE_TYPE (decl); | |
1003 | t != NULL_TREE; | |
1004 | t = CLASSTYPE_USE_TEMPLATE (t) | |
1005 | ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE) | |
9edc3913 | 1006 | if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl))) |
6757edfe | 1007 | return 1; |
c8094d83 | 1008 | } |
386b8a85 | 1009 | |
6757edfe MM |
1010 | return 0; |
1011 | } | |
75650646 | 1012 | |
d43f603d KL |
1013 | /* Returns nonzero iff DECL is a specialization of friend declaration |
1014 | FRIEND according to [temp.friend]. */ | |
1015 | ||
1016 | bool | |
1017 | is_specialization_of_friend (tree decl, tree friend) | |
1018 | { | |
1019 | bool need_template = true; | |
1020 | int template_depth; | |
1021 | ||
b939a023 KL |
1022 | gcc_assert (TREE_CODE (decl) == FUNCTION_DECL |
1023 | || TREE_CODE (decl) == TYPE_DECL); | |
d43f603d KL |
1024 | |
1025 | /* For [temp.friend/6] when FRIEND is an ordinary member function | |
1026 | of a template class, we want to check if DECL is a specialization | |
1027 | if this. */ | |
1028 | if (TREE_CODE (friend) == FUNCTION_DECL | |
1029 | && DECL_TEMPLATE_INFO (friend) | |
1030 | && !DECL_USE_TEMPLATE (friend)) | |
1031 | { | |
b939a023 | 1032 | /* We want a TEMPLATE_DECL for `is_specialization_of'. */ |
d43f603d KL |
1033 | friend = DECL_TI_TEMPLATE (friend); |
1034 | need_template = false; | |
1035 | } | |
b939a023 KL |
1036 | else if (TREE_CODE (friend) == TEMPLATE_DECL |
1037 | && !PRIMARY_TEMPLATE_P (friend)) | |
1038 | need_template = false; | |
d43f603d KL |
1039 | |
1040 | /* There is nothing to do if this is not a template friend. */ | |
1041 | if (TREE_CODE (friend) != TEMPLATE_DECL) | |
b939a023 | 1042 | return false; |
d43f603d KL |
1043 | |
1044 | if (is_specialization_of (decl, friend)) | |
b939a023 | 1045 | return true; |
d43f603d KL |
1046 | |
1047 | /* [temp.friend/6] | |
1048 | A member of a class template may be declared to be a friend of a | |
1049 | non-template class. In this case, the corresponding member of | |
1050 | every specialization of the class template is a friend of the | |
1051 | class granting friendship. | |
c8094d83 | 1052 | |
d43f603d KL |
1053 | For example, given a template friend declaration |
1054 | ||
1055 | template <class T> friend void A<T>::f(); | |
1056 | ||
1057 | the member function below is considered a friend | |
1058 | ||
1059 | template <> struct A<int> { | |
1060 | void f(); | |
1061 | }; | |
1062 | ||
1063 | For this type of template friend, TEMPLATE_DEPTH below will be | |
5c425df5 | 1064 | nonzero. To determine if DECL is a friend of FRIEND, we first |
d43f603d KL |
1065 | check if the enclosing class is a specialization of another. */ |
1066 | ||
1067 | template_depth = template_class_depth (DECL_CONTEXT (friend)); | |
1068 | if (template_depth | |
1069 | && DECL_CLASS_SCOPE_P (decl) | |
c8094d83 | 1070 | && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)), |
d43f603d KL |
1071 | CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend)))) |
1072 | { | |
1073 | /* Next, we check the members themselves. In order to handle | |
b939a023 | 1074 | a few tricky cases, such as when FRIEND's are |
d43f603d KL |
1075 | |
1076 | template <class T> friend void A<T>::g(T t); | |
1077 | template <class T> template <T t> friend void A<T>::h(); | |
1078 | ||
b939a023 KL |
1079 | and DECL's are |
1080 | ||
1081 | void A<int>::g(int); | |
1082 | template <int> void A<int>::h(); | |
1083 | ||
1084 | we need to figure out ARGS, the template arguments from | |
1085 | the context of DECL. This is required for template substitution | |
1086 | of `T' in the function parameter of `g' and template parameter | |
1087 | of `h' in the above examples. Here ARGS corresponds to `int'. */ | |
d43f603d KL |
1088 | |
1089 | tree context = DECL_CONTEXT (decl); | |
1090 | tree args = NULL_TREE; | |
1091 | int current_depth = 0; | |
b939a023 | 1092 | |
d43f603d KL |
1093 | while (current_depth < template_depth) |
1094 | { | |
1095 | if (CLASSTYPE_TEMPLATE_INFO (context)) | |
1096 | { | |
1097 | if (current_depth == 0) | |
1098 | args = TYPE_TI_ARGS (context); | |
1099 | else | |
1100 | args = add_to_template_args (TYPE_TI_ARGS (context), args); | |
1101 | current_depth++; | |
1102 | } | |
1103 | context = TYPE_CONTEXT (context); | |
1104 | } | |
1105 | ||
1106 | if (TREE_CODE (decl) == FUNCTION_DECL) | |
1107 | { | |
1108 | bool is_template; | |
1109 | tree friend_type; | |
1110 | tree decl_type; | |
1111 | tree friend_args_type; | |
1112 | tree decl_args_type; | |
1113 | ||
1114 | /* Make sure that both DECL and FRIEND are templates or | |
1115 | non-templates. */ | |
1116 | is_template = DECL_TEMPLATE_INFO (decl) | |
1117 | && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)); | |
1118 | if (need_template ^ is_template) | |
b939a023 | 1119 | return false; |
d43f603d KL |
1120 | else if (is_template) |
1121 | { | |
da1d7781 | 1122 | /* If both are templates, check template parameter list. */ |
d43f603d KL |
1123 | tree friend_parms |
1124 | = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend), | |
1125 | args, tf_none); | |
1126 | if (!comp_template_parms | |
1127 | (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)), | |
1128 | friend_parms)) | |
b939a023 | 1129 | return false; |
d43f603d KL |
1130 | |
1131 | decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl)); | |
1132 | } | |
1133 | else | |
1134 | decl_type = TREE_TYPE (decl); | |
1135 | ||
1136 | friend_type = tsubst_function_type (TREE_TYPE (friend), args, | |
1137 | tf_none, NULL_TREE); | |
1138 | if (friend_type == error_mark_node) | |
b939a023 | 1139 | return false; |
d43f603d KL |
1140 | |
1141 | /* Check if return types match. */ | |
1142 | if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type))) | |
b939a023 | 1143 | return false; |
d43f603d KL |
1144 | |
1145 | /* Check if function parameter types match, ignoring the | |
1146 | `this' parameter. */ | |
1147 | friend_args_type = TYPE_ARG_TYPES (friend_type); | |
1148 | decl_args_type = TYPE_ARG_TYPES (decl_type); | |
1149 | if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend)) | |
1150 | friend_args_type = TREE_CHAIN (friend_args_type); | |
1151 | if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)) | |
1152 | decl_args_type = TREE_CHAIN (decl_args_type); | |
b939a023 KL |
1153 | |
1154 | return compparms (decl_args_type, friend_args_type); | |
1155 | } | |
1156 | else | |
1157 | { | |
1158 | /* DECL is a TYPE_DECL */ | |
1159 | bool is_template; | |
1160 | tree decl_type = TREE_TYPE (decl); | |
1161 | ||
1162 | /* Make sure that both DECL and FRIEND are templates or | |
1163 | non-templates. */ | |
1164 | is_template | |
1165 | = CLASSTYPE_TEMPLATE_INFO (decl_type) | |
1166 | && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type)); | |
1167 | ||
1168 | if (need_template ^ is_template) | |
1169 | return false; | |
1170 | else if (is_template) | |
1171 | { | |
1172 | tree friend_parms; | |
1173 | /* If both are templates, check the name of the two | |
1174 | TEMPLATE_DECL's first because is_friend didn't. */ | |
1175 | if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type)) | |
1176 | != DECL_NAME (friend)) | |
1177 | return false; | |
1178 | ||
1179 | /* Now check template parameter list. */ | |
1180 | friend_parms | |
1181 | = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend), | |
1182 | args, tf_none); | |
1183 | return comp_template_parms | |
1184 | (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)), | |
1185 | friend_parms); | |
1186 | } | |
1187 | else | |
1188 | return (DECL_NAME (decl) | |
1189 | == DECL_NAME (friend)); | |
d43f603d KL |
1190 | } |
1191 | } | |
b939a023 | 1192 | return false; |
d43f603d KL |
1193 | } |
1194 | ||
75650646 | 1195 | /* Register the specialization SPEC as a specialization of TMPL with |
d63d5d0c ILT |
1196 | the indicated ARGS. IS_FRIEND indicates whether the specialization |
1197 | is actually just a friend declaration. Returns SPEC, or an | |
1198 | equivalent prior declaration, if available. */ | |
75650646 | 1199 | |
36a117a5 | 1200 | static tree |
d63d5d0c | 1201 | register_specialization (tree spec, tree tmpl, tree args, bool is_friend) |
75650646 | 1202 | { |
c7222c02 | 1203 | tree fn; |
75650646 | 1204 | |
50bc768d | 1205 | gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL); |
75650646 | 1206 | |
c8094d83 | 1207 | if (TREE_CODE (spec) == FUNCTION_DECL |
36a117a5 MM |
1208 | && uses_template_parms (DECL_TI_ARGS (spec))) |
1209 | /* This is the FUNCTION_DECL for a partial instantiation. Don't | |
1210 | register it; we want the corresponding TEMPLATE_DECL instead. | |
1211 | We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than | |
1212 | the more obvious `uses_template_parms (spec)' to avoid problems | |
1213 | with default function arguments. In particular, given | |
1214 | something like this: | |
1215 | ||
0cbd7506 | 1216 | template <class T> void f(T t1, T t = T()) |
36a117a5 MM |
1217 | |
1218 | the default argument expression is not substituted for in an | |
1219 | instantiation unless and until it is actually needed. */ | |
1220 | return spec; | |
28ba38e3 | 1221 | |
c8094d83 | 1222 | fn = retrieve_specialization (tmpl, args, |
c7222c02 MM |
1223 | /*class_specializations_p=*/false); |
1224 | /* We can sometimes try to re-register a specialization that we've | |
1225 | already got. In particular, regenerate_decl_from_template calls | |
1226 | duplicate_decls which will update the specialization list. But, | |
1227 | we'll still get called again here anyhow. It's more convenient | |
1228 | to simply allow this than to try to prevent it. */ | |
1229 | if (fn == spec) | |
1230 | return spec; | |
1231 | else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec)) | |
22e9174f | 1232 | { |
c7222c02 | 1233 | if (DECL_TEMPLATE_INSTANTIATION (fn)) |
22e9174f | 1234 | { |
c8094d83 | 1235 | if (TREE_USED (fn) |
c7222c02 | 1236 | || DECL_EXPLICIT_INSTANTIATION (fn)) |
22e9174f | 1237 | { |
c7222c02 MM |
1238 | error ("specialization of %qD after instantiation", |
1239 | fn); | |
99f4234a | 1240 | return error_mark_node; |
22e9174f | 1241 | } |
c7222c02 | 1242 | else |
b1cc95ce | 1243 | { |
a39041fa | 1244 | tree clone; |
c7222c02 MM |
1245 | /* This situation should occur only if the first |
1246 | specialization is an implicit instantiation, the | |
1247 | second is an explicit specialization, and the | |
1248 | implicit instantiation has not yet been used. That | |
1249 | situation can occur if we have implicitly | |
1250 | instantiated a member function and then specialized | |
1251 | it later. | |
c8094d83 | 1252 | |
c7222c02 MM |
1253 | We can also wind up here if a friend declaration that |
1254 | looked like an instantiation turns out to be a | |
1255 | specialization: | |
c8094d83 | 1256 | |
c7222c02 MM |
1257 | template <class T> void foo(T); |
1258 | class S { friend void foo<>(int) }; | |
c8094d83 MS |
1259 | template <> void foo(int); |
1260 | ||
c7222c02 MM |
1261 | We transform the existing DECL in place so that any |
1262 | pointers to it become pointers to the updated | |
1263 | declaration. | |
1264 | ||
1265 | If there was a definition for the template, but not | |
1266 | for the specialization, we want this to look as if | |
1267 | there were no definition, and vice versa. */ | |
1268 | DECL_INITIAL (fn) = NULL_TREE; | |
d63d5d0c | 1269 | duplicate_decls (spec, fn, is_friend); |
a39041fa | 1270 | /* The call to duplicate_decls will have applied |
3db45ab5 | 1271 | [temp.expl.spec]: |
a39041fa | 1272 | |
3db45ab5 | 1273 | An explicit specialization of a function template |
a39041fa | 1274 | is inline only if it is explicitly declared to be, |
bcf51da2 | 1275 | and independently of whether its function template |
a39041fa MM |
1276 | is. |
1277 | ||
1278 | to the primary function; now copy the inline bits to | |
3db45ab5 | 1279 | the various clones. */ |
a39041fa MM |
1280 | FOR_EACH_CLONE (clone, fn) |
1281 | { | |
1282 | DECL_DECLARED_INLINE_P (clone) | |
1283 | = DECL_DECLARED_INLINE_P (fn); | |
1284 | DECL_INLINE (clone) | |
1285 | = DECL_INLINE (fn); | |
1286 | } | |
9c12301f | 1287 | check_specialization_namespace (fn); |
c8094d83 | 1288 | |
b1cc95ce MM |
1289 | return fn; |
1290 | } | |
22e9174f | 1291 | } |
c7222c02 MM |
1292 | else if (DECL_TEMPLATE_SPECIALIZATION (fn)) |
1293 | { | |
d63d5d0c | 1294 | if (!duplicate_decls (spec, fn, is_friend) && DECL_INITIAL (spec)) |
c7222c02 MM |
1295 | /* Dup decl failed, but this is a new definition. Set the |
1296 | line number so any errors match this new | |
1297 | definition. */ | |
1298 | DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec); | |
c8094d83 | 1299 | |
c7222c02 MM |
1300 | return fn; |
1301 | } | |
b1cc95ce MM |
1302 | } |
1303 | ||
1304 | /* A specialization must be declared in the same namespace as the | |
1305 | template it is specializing. */ | |
1306 | if (DECL_TEMPLATE_SPECIALIZATION (spec) | |
1307 | && !check_specialization_namespace (tmpl)) | |
74b0d490 | 1308 | DECL_CONTEXT (spec) = FROB_CONTEXT (decl_namespace_context (tmpl)); |
75650646 | 1309 | |
c7222c02 MM |
1310 | if (!optimize_specialization_lookup_p (tmpl)) |
1311 | DECL_TEMPLATE_SPECIALIZATIONS (tmpl) | |
1312 | = tree_cons (args, spec, DECL_TEMPLATE_SPECIALIZATIONS (tmpl)); | |
36a117a5 MM |
1313 | |
1314 | return spec; | |
1315 | } | |
1316 | ||
1317 | /* Unregister the specialization SPEC as a specialization of TMPL. | |
b3445994 MM |
1318 | Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true |
1319 | if the SPEC was listed as a specialization of TMPL. */ | |
36a117a5 | 1320 | |
b3445994 MM |
1321 | bool |
1322 | reregister_specialization (tree spec, tree tmpl, tree new_spec) | |
36a117a5 MM |
1323 | { |
1324 | tree* s; | |
1325 | ||
1326 | for (s = &DECL_TEMPLATE_SPECIALIZATIONS (tmpl); | |
1327 | *s != NULL_TREE; | |
1328 | s = &TREE_CHAIN (*s)) | |
1329 | if (TREE_VALUE (*s) == spec) | |
1330 | { | |
b3445994 MM |
1331 | if (!new_spec) |
1332 | *s = TREE_CHAIN (*s); | |
1333 | else | |
a3d87771 | 1334 | TREE_VALUE (*s) = new_spec; |
36a117a5 MM |
1335 | return 1; |
1336 | } | |
1337 | ||
1338 | return 0; | |
75650646 MM |
1339 | } |
1340 | ||
a723baf1 MM |
1341 | /* Compare an entry in the local specializations hash table P1 (which |
1342 | is really a pointer to a TREE_LIST) with P2 (which is really a | |
1343 | DECL). */ | |
1344 | ||
1345 | static int | |
1346 | eq_local_specializations (const void *p1, const void *p2) | |
1347 | { | |
741ac903 | 1348 | return TREE_VALUE ((const_tree) p1) == (const_tree) p2; |
a723baf1 MM |
1349 | } |
1350 | ||
69f794a7 MM |
1351 | /* Hash P1, an entry in the local specializations table. */ |
1352 | ||
1353 | static hashval_t | |
1354 | hash_local_specialization (const void* p1) | |
1355 | { | |
741ac903 | 1356 | return htab_hash_pointer (TREE_VALUE ((const_tree) p1)); |
69f794a7 MM |
1357 | } |
1358 | ||
6dfbb909 MM |
1359 | /* Like register_specialization, but for local declarations. We are |
1360 | registering SPEC, an instantiation of TMPL. */ | |
9188c363 | 1361 | |
414ea4aa | 1362 | static void |
3a978d72 | 1363 | register_local_specialization (tree spec, tree tmpl) |
9188c363 | 1364 | { |
6dfbb909 MM |
1365 | void **slot; |
1366 | ||
c8094d83 | 1367 | slot = htab_find_slot_with_hash (local_specializations, tmpl, |
69f794a7 | 1368 | htab_hash_pointer (tmpl), INSERT); |
a723baf1 | 1369 | *slot = build_tree_list (spec, tmpl); |
9188c363 MM |
1370 | } |
1371 | ||
9ba7a2f2 MM |
1372 | /* TYPE is a class type. Returns true if TYPE is an explicitly |
1373 | specialized class. */ | |
1374 | ||
1375 | bool | |
1376 | explicit_class_specialization_p (tree type) | |
1377 | { | |
1378 | if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type)) | |
1379 | return false; | |
1380 | return !uses_template_parms (CLASSTYPE_TI_ARGS (type)); | |
1381 | } | |
1382 | ||
e1467ff2 MM |
1383 | /* Print the list of candidate FNS in an error message. */ |
1384 | ||
104bf76a | 1385 | void |
3a978d72 | 1386 | print_candidates (tree fns) |
e1467ff2 MM |
1387 | { |
1388 | tree fn; | |
1389 | ||
d8e178a0 | 1390 | const char *str = "candidates are:"; |
e1467ff2 MM |
1391 | |
1392 | for (fn = fns; fn != NULL_TREE; fn = TREE_CHAIN (fn)) | |
1393 | { | |
d6479fe7 MM |
1394 | tree f; |
1395 | ||
1396 | for (f = TREE_VALUE (fn); f; f = OVL_NEXT (f)) | |
dee15844 | 1397 | error ("%s %+#D", str, OVL_CURRENT (f)); |
e1467ff2 MM |
1398 | str = " "; |
1399 | } | |
1400 | } | |
1401 | ||
75650646 | 1402 | /* Returns the template (one of the functions given by TEMPLATE_ID) |
e1467ff2 | 1403 | which can be specialized to match the indicated DECL with the |
03017874 MM |
1404 | explicit template args given in TEMPLATE_ID. The DECL may be |
1405 | NULL_TREE if none is available. In that case, the functions in | |
1406 | TEMPLATE_ID are non-members. | |
1407 | ||
838dfd8a | 1408 | If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a |
03017874 MM |
1409 | specialization of a member template. |
1410 | ||
5fe7b654 GB |
1411 | The TEMPLATE_COUNT is the number of references to qualifying |
1412 | template classes that appeared in the name of the function. See | |
1413 | check_explicit_specialization for a more accurate description. | |
1414 | ||
29a1da1c MM |
1415 | TSK indicates what kind of template declaration (if any) is being |
1416 | declared. TSK_TEMPLATE indicates that the declaration given by | |
1417 | DECL, though a FUNCTION_DECL, has template parameters, and is | |
1418 | therefore a template function. | |
1419 | ||
03017874 MM |
1420 | The template args (those explicitly specified and those deduced) |
1421 | are output in a newly created vector *TARGS_OUT. | |
1422 | ||
1423 | If it is impossible to determine the result, an error message is | |
bf8f3f93 | 1424 | issued. The error_mark_node is returned to indicate failure. */ |
75650646 | 1425 | |
e9659ab0 | 1426 | static tree |
c8094d83 | 1427 | determine_specialization (tree template_id, |
0cbd7506 MS |
1428 | tree decl, |
1429 | tree* targs_out, | |
5fe7b654 | 1430 | int need_member_template, |
29a1da1c MM |
1431 | int template_count, |
1432 | tmpl_spec_kind tsk) | |
386b8a85 | 1433 | { |
03017874 MM |
1434 | tree fns; |
1435 | tree targs; | |
1436 | tree explicit_targs; | |
1437 | tree candidates = NULL_TREE; | |
7ca383e6 MM |
1438 | /* A TREE_LIST of templates of which DECL may be a specialization. |
1439 | The TREE_VALUE of each node is a TEMPLATE_DECL. The | |
1440 | corresponding TREE_PURPOSE is the set of template arguments that, | |
1441 | when used to instantiate the template, would produce a function | |
1442 | with the signature of DECL. */ | |
03017874 | 1443 | tree templates = NULL_TREE; |
5fe7b654 GB |
1444 | int header_count; |
1445 | struct cp_binding_level *b; | |
386b8a85 | 1446 | |
e1467ff2 MM |
1447 | *targs_out = NULL_TREE; |
1448 | ||
728da672 | 1449 | if (template_id == error_mark_node || decl == error_mark_node) |
f2e48b67 BK |
1450 | return error_mark_node; |
1451 | ||
1452 | fns = TREE_OPERAND (template_id, 0); | |
03017874 | 1453 | explicit_targs = TREE_OPERAND (template_id, 1); |
f2e48b67 | 1454 | |
27fafc8d JM |
1455 | if (fns == error_mark_node) |
1456 | return error_mark_node; | |
1457 | ||
c6002625 | 1458 | /* Check for baselinks. */ |
91e490ab | 1459 | if (BASELINK_P (fns)) |
da15dae6 | 1460 | fns = BASELINK_FUNCTIONS (fns); |
386b8a85 | 1461 | |
91e490ab MM |
1462 | if (!is_overloaded_fn (fns)) |
1463 | { | |
0f51ccfc | 1464 | error ("%qD is not a function template", fns); |
91e490ab MM |
1465 | return error_mark_node; |
1466 | } | |
1467 | ||
5fe7b654 GB |
1468 | /* Count the number of template headers specified for this |
1469 | specialization. */ | |
1470 | header_count = 0; | |
c8094d83 | 1471 | for (b = current_binding_level; |
98f99d7f | 1472 | b->kind == sk_template_parms; |
5fe7b654 GB |
1473 | b = b->level_chain) |
1474 | ++header_count; | |
1475 | ||
2c73f9f5 | 1476 | for (; fns; fns = OVL_NEXT (fns)) |
386b8a85 | 1477 | { |
9dfce8fd | 1478 | tree fn = OVL_CURRENT (fns); |
03017874 MM |
1479 | |
1480 | if (TREE_CODE (fn) == TEMPLATE_DECL) | |
d955f6ea KL |
1481 | { |
1482 | tree decl_arg_types; | |
0da99d4e | 1483 | tree fn_arg_types; |
d955f6ea | 1484 | |
5fe7b654 GB |
1485 | /* In case of explicit specialization, we need to check if |
1486 | the number of template headers appearing in the specialization | |
1487 | is correct. This is usually done in check_explicit_specialization, | |
1488 | but the check done there cannot be exhaustive when specializing | |
1489 | member functions. Consider the following code: | |
1490 | ||
1491 | template <> void A<int>::f(int); | |
1492 | template <> template <> void A<int>::f(int); | |
1493 | ||
1494 | Assuming that A<int> is not itself an explicit specialization | |
1495 | already, the first line specializes "f" which is a non-template | |
1496 | member function, whilst the second line specializes "f" which | |
1497 | is a template member function. So both lines are syntactically | |
1498 | correct, and check_explicit_specialization does not reject | |
1499 | them. | |
c8094d83 | 1500 | |
5fe7b654 GB |
1501 | Here, we can do better, as we are matching the specialization |
1502 | against the declarations. We count the number of template | |
1503 | headers, and we check if they match TEMPLATE_COUNT + 1 | |
1504 | (TEMPLATE_COUNT is the number of qualifying template classes, | |
1505 | plus there must be another header for the member template | |
1506 | itself). | |
c8094d83 | 1507 | |
5fe7b654 GB |
1508 | Notice that if header_count is zero, this is not a |
1509 | specialization but rather a template instantiation, so there | |
1510 | is no check we can perform here. */ | |
1511 | if (header_count && header_count != template_count + 1) | |
1512 | continue; | |
1513 | ||
98f99d7f MM |
1514 | /* Check that the number of template arguments at the |
1515 | innermost level for DECL is the same as for FN. */ | |
1516 | if (current_binding_level->kind == sk_template_parms | |
1517 | && !current_binding_level->explicit_spec_p | |
1518 | && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn)) | |
3db45ab5 | 1519 | != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS |
916b63c3 | 1520 | (current_template_parms)))) |
98f99d7f | 1521 | continue; |
c8094d83 | 1522 | |
ba139ba8 MM |
1523 | /* DECL might be a specialization of FN. */ |
1524 | decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl)); | |
1525 | fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn)); | |
1526 | ||
1527 | /* For a non-static member function, we need to make sure | |
1528 | that the const qualification is the same. Since | |
1529 | get_bindings does not try to merge the "this" parameter, | |
1530 | we must do the comparison explicitly. */ | |
1531 | if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn) | |
1532 | && !same_type_p (TREE_VALUE (fn_arg_types), | |
1533 | TREE_VALUE (decl_arg_types))) | |
1534 | continue; | |
1535 | ||
1536 | /* Skip the "this" parameter and, for constructors of | |
1537 | classes with virtual bases, the VTT parameter. A | |
1538 | full specialization of a constructor will have a VTT | |
1539 | parameter, but a template never will. */ | |
1540 | decl_arg_types | |
1541 | = skip_artificial_parms_for (decl, decl_arg_types); | |
1542 | fn_arg_types | |
1543 | = skip_artificial_parms_for (fn, fn_arg_types); | |
1544 | ||
1545 | /* Check that the number of function parameters matches. | |
1546 | For example, | |
1547 | template <class T> void f(int i = 0); | |
1548 | template <> void f<int>(); | |
1549 | The specialization f<int> is invalid but is not caught | |
1550 | by get_bindings below. */ | |
1551 | if (list_length (fn_arg_types) != list_length (decl_arg_types)) | |
1552 | continue; | |
1553 | ||
29a1da1c MM |
1554 | /* Function templates cannot be specializations; there are |
1555 | no partial specializations of functions. Therefore, if | |
1556 | the type of DECL does not match FN, there is no | |
1557 | match. */ | |
1558 | if (tsk == tsk_template) | |
1559 | { | |
ba139ba8 | 1560 | if (compparms (fn_arg_types, decl_arg_types)) |
29a1da1c MM |
1561 | candidates = tree_cons (NULL_TREE, fn, candidates); |
1562 | continue; | |
1563 | } | |
1564 | ||
d955f6ea KL |
1565 | /* See whether this function might be a specialization of this |
1566 | template. */ | |
a34d3336 | 1567 | targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true); |
d955f6ea KL |
1568 | |
1569 | if (!targs) | |
1570 | /* We cannot deduce template arguments that when used to | |
1571 | specialize TMPL will produce DECL. */ | |
1572 | continue; | |
1573 | ||
1574 | /* Save this template, and the arguments deduced. */ | |
1575 | templates = tree_cons (targs, fn, templates); | |
1576 | } | |
03017874 MM |
1577 | else if (need_member_template) |
1578 | /* FN is an ordinary member function, and we need a | |
1579 | specialization of a member template. */ | |
d955f6ea | 1580 | ; |
03017874 MM |
1581 | else if (TREE_CODE (fn) != FUNCTION_DECL) |
1582 | /* We can get IDENTIFIER_NODEs here in certain erroneous | |
1583 | cases. */ | |
d955f6ea | 1584 | ; |
03017874 MM |
1585 | else if (!DECL_FUNCTION_MEMBER_P (fn)) |
1586 | /* This is just an ordinary non-member function. Nothing can | |
1587 | be a specialization of that. */ | |
d955f6ea | 1588 | ; |
3b82c249 KL |
1589 | else if (DECL_ARTIFICIAL (fn)) |
1590 | /* Cannot specialize functions that are created implicitly. */ | |
d955f6ea | 1591 | ; |
75650646 | 1592 | else |
03017874 MM |
1593 | { |
1594 | tree decl_arg_types; | |
386b8a85 | 1595 | |
03017874 MM |
1596 | /* This is an ordinary member function. However, since |
1597 | we're here, we can assume it's enclosing class is a | |
1598 | template class. For example, | |
c8094d83 | 1599 | |
03017874 MM |
1600 | template <typename T> struct S { void f(); }; |
1601 | template <> void S<int>::f() {} | |
1602 | ||
1603 | Here, S<int>::f is a non-template, but S<int> is a | |
1604 | template class. If FN has the same type as DECL, we | |
1605 | might be in business. */ | |
f5d47abd KL |
1606 | |
1607 | if (!DECL_TEMPLATE_INFO (fn)) | |
1608 | /* Its enclosing class is an explicit specialization | |
1609 | of a template class. This is not a candidate. */ | |
1610 | continue; | |
1611 | ||
03017874 MM |
1612 | if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)), |
1613 | TREE_TYPE (TREE_TYPE (fn)))) | |
1614 | /* The return types differ. */ | |
1615 | continue; | |
1616 | ||
1617 | /* Adjust the type of DECL in case FN is a static member. */ | |
1618 | decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl)); | |
c8094d83 | 1619 | if (DECL_STATIC_FUNCTION_P (fn) |
03017874 MM |
1620 | && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)) |
1621 | decl_arg_types = TREE_CHAIN (decl_arg_types); | |
1622 | ||
c8094d83 | 1623 | if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)), |
03017874 MM |
1624 | decl_arg_types)) |
1625 | /* They match! */ | |
1626 | candidates = tree_cons (NULL_TREE, fn, candidates); | |
03017874 | 1627 | } |
386b8a85 | 1628 | } |
03017874 | 1629 | |
bf8f3f93 | 1630 | if (templates && TREE_CHAIN (templates)) |
386b8a85 | 1631 | { |
03017874 | 1632 | /* We have: |
c8094d83 | 1633 | |
03017874 MM |
1634 | [temp.expl.spec] |
1635 | ||
1636 | It is possible for a specialization with a given function | |
1637 | signature to be instantiated from more than one function | |
1638 | template. In such cases, explicit specification of the | |
1639 | template arguments must be used to uniquely identify the | |
1640 | function template specialization being specialized. | |
1641 | ||
1642 | Note that here, there's no suggestion that we're supposed to | |
1643 | determine which of the candidate templates is most | |
1644 | specialized. However, we, also have: | |
1645 | ||
1646 | [temp.func.order] | |
1647 | ||
1648 | Partial ordering of overloaded function template | |
1649 | declarations is used in the following contexts to select | |
1650 | the function template to which a function template | |
c8094d83 | 1651 | specialization refers: |
03017874 | 1652 | |
0cbd7506 | 1653 | -- when an explicit specialization refers to a function |
c8094d83 | 1654 | template. |
03017874 MM |
1655 | |
1656 | So, we do use the partial ordering rules, at least for now. | |
0e339752 | 1657 | This extension can only serve to make invalid programs valid, |
03017874 MM |
1658 | so it's safe. And, there is strong anecdotal evidence that |
1659 | the committee intended the partial ordering rules to apply; | |
3b426391 | 1660 | the EDG front end has that behavior, and John Spicer claims |
03017874 MM |
1661 | that the committee simply forgot to delete the wording in |
1662 | [temp.expl.spec]. */ | |
7ca383e6 MM |
1663 | tree tmpl = most_specialized_instantiation (templates); |
1664 | if (tmpl != error_mark_node) | |
1665 | { | |
1666 | templates = tmpl; | |
1667 | TREE_CHAIN (templates) = NULL_TREE; | |
1668 | } | |
e1467ff2 MM |
1669 | } |
1670 | ||
03017874 | 1671 | if (templates == NULL_TREE && candidates == NULL_TREE) |
e1467ff2 | 1672 | { |
dee15844 JM |
1673 | error ("template-id %qD for %q+D does not match any template " |
1674 | "declaration", template_id, decl); | |
03017874 | 1675 | return error_mark_node; |
386b8a85 | 1676 | } |
03017874 | 1677 | else if ((templates && TREE_CHAIN (templates)) |
bf8f3f93 MM |
1678 | || (candidates && TREE_CHAIN (candidates)) |
1679 | || (templates && candidates)) | |
386b8a85 | 1680 | { |
dee15844 JM |
1681 | error ("ambiguous template specialization %qD for %q+D", |
1682 | template_id, decl); | |
bf8f3f93 MM |
1683 | chainon (candidates, templates); |
1684 | print_candidates (candidates); | |
03017874 | 1685 | return error_mark_node; |
386b8a85 JM |
1686 | } |
1687 | ||
c6002625 | 1688 | /* We have one, and exactly one, match. */ |
03017874 MM |
1689 | if (candidates) |
1690 | { | |
29a1da1c MM |
1691 | tree fn = TREE_VALUE (candidates); |
1692 | /* DECL is a re-declaration of a template function. */ | |
1693 | if (TREE_CODE (fn) == TEMPLATE_DECL) | |
1694 | return fn; | |
03017874 MM |
1695 | /* It was a specialization of an ordinary member function in a |
1696 | template class. */ | |
29a1da1c MM |
1697 | *targs_out = copy_node (DECL_TI_ARGS (fn)); |
1698 | return DECL_TI_TEMPLATE (fn); | |
03017874 MM |
1699 | } |
1700 | ||
1701 | /* It was a specialization of a template. */ | |
17aec3eb | 1702 | targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates))); |
03017874 MM |
1703 | if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs)) |
1704 | { | |
1705 | *targs_out = copy_node (targs); | |
c8094d83 | 1706 | SET_TMPL_ARGS_LEVEL (*targs_out, |
03017874 MM |
1707 | TMPL_ARGS_DEPTH (*targs_out), |
1708 | TREE_PURPOSE (templates)); | |
1709 | } | |
1710 | else | |
1711 | *targs_out = TREE_PURPOSE (templates); | |
e1467ff2 | 1712 | return TREE_VALUE (templates); |
8d08fdba | 1713 | } |
8afa707f MM |
1714 | |
1715 | /* Returns a chain of parameter types, exactly like the SPEC_TYPES, | |
1716 | but with the default argument values filled in from those in the | |
1717 | TMPL_TYPES. */ | |
c8094d83 | 1718 | |
8afa707f | 1719 | static tree |
3a978d72 NN |
1720 | copy_default_args_to_explicit_spec_1 (tree spec_types, |
1721 | tree tmpl_types) | |
8afa707f MM |
1722 | { |
1723 | tree new_spec_types; | |
1724 | ||
1725 | if (!spec_types) | |
1726 | return NULL_TREE; | |
1727 | ||
1728 | if (spec_types == void_list_node) | |
1729 | return void_list_node; | |
1730 | ||
1731 | /* Substitute into the rest of the list. */ | |
1732 | new_spec_types = | |
1733 | copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types), | |
1734 | TREE_CHAIN (tmpl_types)); | |
c8094d83 | 1735 | |
8afa707f MM |
1736 | /* Add the default argument for this parameter. */ |
1737 | return hash_tree_cons (TREE_PURPOSE (tmpl_types), | |
1738 | TREE_VALUE (spec_types), | |
1739 | new_spec_types); | |
1740 | } | |
1741 | ||
1742 | /* DECL is an explicit specialization. Replicate default arguments | |
1743 | from the template it specializes. (That way, code like: | |
1744 | ||
1745 | template <class T> void f(T = 3); | |
1746 | template <> void f(double); | |
c8094d83 | 1747 | void g () { f (); } |
8afa707f MM |
1748 | |
1749 | works, as required.) An alternative approach would be to look up | |
1750 | the correct default arguments at the call-site, but this approach | |
1751 | is consistent with how implicit instantiations are handled. */ | |
1752 | ||
1753 | static void | |
3a978d72 | 1754 | copy_default_args_to_explicit_spec (tree decl) |
8afa707f MM |
1755 | { |
1756 | tree tmpl; | |
1757 | tree spec_types; | |
1758 | tree tmpl_types; | |
1759 | tree new_spec_types; | |
1760 | tree old_type; | |
1761 | tree new_type; | |
1762 | tree t; | |
08c2df0f NS |
1763 | tree object_type = NULL_TREE; |
1764 | tree in_charge = NULL_TREE; | |
e0fff4b3 | 1765 | tree vtt = NULL_TREE; |
8afa707f MM |
1766 | |
1767 | /* See if there's anything we need to do. */ | |
1768 | tmpl = DECL_TI_TEMPLATE (decl); | |
1769 | tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl))); | |
1770 | for (t = tmpl_types; t; t = TREE_CHAIN (t)) | |
1771 | if (TREE_PURPOSE (t)) | |
1772 | break; | |
1773 | if (!t) | |
1774 | return; | |
1775 | ||
1776 | old_type = TREE_TYPE (decl); | |
1777 | spec_types = TYPE_ARG_TYPES (old_type); | |
c8094d83 | 1778 | |
8afa707f MM |
1779 | if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)) |
1780 | { | |
08c2df0f | 1781 | /* Remove the this pointer, but remember the object's type for |
0cbd7506 | 1782 | CV quals. */ |
08c2df0f | 1783 | object_type = TREE_TYPE (TREE_VALUE (spec_types)); |
8afa707f MM |
1784 | spec_types = TREE_CHAIN (spec_types); |
1785 | tmpl_types = TREE_CHAIN (tmpl_types); | |
c8094d83 | 1786 | |
8afa707f | 1787 | if (DECL_HAS_IN_CHARGE_PARM_P (decl)) |
0cbd7506 MS |
1788 | { |
1789 | /* DECL may contain more parameters than TMPL due to the extra | |
1790 | in-charge parameter in constructors and destructors. */ | |
1791 | in_charge = spec_types; | |
08c2df0f NS |
1792 | spec_types = TREE_CHAIN (spec_types); |
1793 | } | |
e0fff4b3 JM |
1794 | if (DECL_HAS_VTT_PARM_P (decl)) |
1795 | { | |
1796 | vtt = spec_types; | |
1797 | spec_types = TREE_CHAIN (spec_types); | |
1798 | } | |
8afa707f MM |
1799 | } |
1800 | ||
1801 | /* Compute the merged default arguments. */ | |
c8094d83 | 1802 | new_spec_types = |
8afa707f MM |
1803 | copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types); |
1804 | ||
08c2df0f NS |
1805 | /* Compute the new FUNCTION_TYPE. */ |
1806 | if (object_type) | |
8afa707f | 1807 | { |
e0fff4b3 | 1808 | if (vtt) |
0cbd7506 MS |
1809 | new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt), |
1810 | TREE_VALUE (vtt), | |
1811 | new_spec_types); | |
e0fff4b3 | 1812 | |
08c2df0f | 1813 | if (in_charge) |
0cbd7506 MS |
1814 | /* Put the in-charge parameter back. */ |
1815 | new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge), | |
1816 | TREE_VALUE (in_charge), | |
1817 | new_spec_types); | |
08c2df0f | 1818 | |
43dc123f MM |
1819 | new_type = build_method_type_directly (object_type, |
1820 | TREE_TYPE (old_type), | |
1821 | new_spec_types); | |
8afa707f | 1822 | } |
8afa707f MM |
1823 | else |
1824 | new_type = build_function_type (TREE_TYPE (old_type), | |
08c2df0f | 1825 | new_spec_types); |
e9525111 MM |
1826 | new_type = cp_build_type_attribute_variant (new_type, |
1827 | TYPE_ATTRIBUTES (old_type)); | |
8afa707f MM |
1828 | new_type = build_exception_variant (new_type, |
1829 | TYPE_RAISES_EXCEPTIONS (old_type)); | |
1830 | TREE_TYPE (decl) = new_type; | |
1831 | } | |
1832 | ||
386b8a85 | 1833 | /* Check to see if the function just declared, as indicated in |
75650646 MM |
1834 | DECLARATOR, and in DECL, is a specialization of a function |
1835 | template. We may also discover that the declaration is an explicit | |
1836 | instantiation at this point. | |
1837 | ||
e1467ff2 | 1838 | Returns DECL, or an equivalent declaration that should be used |
03017874 MM |
1839 | instead if all goes well. Issues an error message if something is |
1840 | amiss. Returns error_mark_node if the error is not easily | |
1841 | recoverable. | |
c8094d83 MS |
1842 | |
1843 | FLAGS is a bitmask consisting of the following flags: | |
75650646 | 1844 | |
75650646 MM |
1845 | 2: The function has a definition. |
1846 | 4: The function is a friend. | |
75650646 MM |
1847 | |
1848 | The TEMPLATE_COUNT is the number of references to qualifying | |
1849 | template classes that appeared in the name of the function. For | |
1850 | example, in | |
1851 | ||
1852 | template <class T> struct S { void f(); }; | |
1853 | void S<int>::f(); | |
c8094d83 | 1854 | |
75650646 MM |
1855 | the TEMPLATE_COUNT would be 1. However, explicitly specialized |
1856 | classes are not counted in the TEMPLATE_COUNT, so that in | |
1857 | ||
1858 | template <class T> struct S {}; | |
1859 | template <> struct S<int> { void f(); } | |
36a117a5 | 1860 | template <> void S<int>::f(); |
75650646 MM |
1861 | |
1862 | the TEMPLATE_COUNT would be 0. (Note that this declaration is | |
0e339752 | 1863 | invalid; there should be no template <>.) |
75650646 MM |
1864 | |
1865 | If the function is a specialization, it is marked as such via | |
1866 | DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO | |
c8094d83 | 1867 | is set up correctly, and it is added to the list of specializations |
75650646 | 1868 | for that template. */ |
386b8a85 | 1869 | |
e1467ff2 | 1870 | tree |
c8094d83 | 1871 | check_explicit_specialization (tree declarator, |
0cbd7506 MS |
1872 | tree decl, |
1873 | int template_count, | |
1874 | int flags) | |
386b8a85 | 1875 | { |
75650646 MM |
1876 | int have_def = flags & 2; |
1877 | int is_friend = flags & 4; | |
1878 | int specialization = 0; | |
e1467ff2 | 1879 | int explicit_instantiation = 0; |
fd4de5ff | 1880 | int member_specialization = 0; |
75650646 MM |
1881 | tree ctype = DECL_CLASS_CONTEXT (decl); |
1882 | tree dname = DECL_NAME (decl); | |
74b846e0 | 1883 | tmpl_spec_kind tsk; |
386b8a85 | 1884 | |
f65b7de3 GB |
1885 | if (is_friend) |
1886 | { | |
1887 | if (!processing_specialization) | |
1888 | tsk = tsk_none; | |
1889 | else | |
1890 | tsk = tsk_excessive_parms; | |
1891 | } | |
1892 | else | |
1893 | tsk = current_tmpl_spec_kind (template_count); | |
75650646 | 1894 | |
74b846e0 MM |
1895 | switch (tsk) |
1896 | { | |
1897 | case tsk_none: | |
c8094d83 | 1898 | if (processing_specialization) |
75650646 | 1899 | { |
fd4de5ff MM |
1900 | specialization = 1; |
1901 | SET_DECL_TEMPLATE_SPECIALIZATION (decl); | |
75650646 | 1902 | } |
74b846e0 | 1903 | else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR) |
386b8a85 | 1904 | { |
74b846e0 MM |
1905 | if (is_friend) |
1906 | /* This could be something like: | |
75650646 | 1907 | |
74b846e0 MM |
1908 | template <class T> void f(T); |
1909 | class S { friend void f<>(int); } */ | |
1910 | specialization = 1; | |
1911 | else | |
1912 | { | |
1913 | /* This case handles bogus declarations like template <> | |
1914 | template <class T> void f<int>(); */ | |
1915 | ||
0f51ccfc | 1916 | error ("template-id %qD in declaration of primary template", |
0cbd7506 | 1917 | declarator); |
74b846e0 MM |
1918 | return decl; |
1919 | } | |
1920 | } | |
1921 | break; | |
1922 | ||
1923 | case tsk_invalid_member_spec: | |
1924 | /* The error has already been reported in | |
1925 | check_specialization_scope. */ | |
1926 | return error_mark_node; | |
1927 | ||
1928 | case tsk_invalid_expl_inst: | |
33bd39a2 | 1929 | error ("template parameter list used in explicit instantiation"); |
74b846e0 MM |
1930 | |
1931 | /* Fall through. */ | |
1932 | ||
1933 | case tsk_expl_inst: | |
fd4de5ff | 1934 | if (have_def) |
33bd39a2 | 1935 | error ("definition provided for explicit instantiation"); |
c8094d83 | 1936 | |
fd4de5ff | 1937 | explicit_instantiation = 1; |
74b846e0 | 1938 | break; |
fd4de5ff | 1939 | |
74b846e0 | 1940 | case tsk_excessive_parms: |
f03adc6b GB |
1941 | case tsk_insufficient_parms: |
1942 | if (tsk == tsk_excessive_parms) | |
0cbd7506 | 1943 | error ("too many template parameter lists in declaration of %qD", |
f03adc6b GB |
1944 | decl); |
1945 | else if (template_header_count) | |
0f51ccfc | 1946 | error("too few template parameter lists in declaration of %qD", decl); |
f03adc6b | 1947 | else |
0f51ccfc | 1948 | error("explicit specialization of %qD must be introduced by " |
9e637a26 | 1949 | "%<template <>%>", decl); |
75650646 | 1950 | |
74b846e0 MM |
1951 | /* Fall through. */ |
1952 | case tsk_expl_spec: | |
1953 | SET_DECL_TEMPLATE_SPECIALIZATION (decl); | |
1954 | if (ctype) | |
1955 | member_specialization = 1; | |
1956 | else | |
1957 | specialization = 1; | |
1958 | break; | |
74b846e0 MM |
1959 | |
1960 | case tsk_template: | |
1961 | if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR) | |
c6f2ed0d | 1962 | { |
fd4de5ff MM |
1963 | /* This case handles bogus declarations like template <> |
1964 | template <class T> void f<int>(); */ | |
75650646 | 1965 | |
655dc6ee | 1966 | if (uses_template_parms (declarator)) |
0f51ccfc | 1967 | error ("function template partial specialization %qD " |
e1e93ad8 | 1968 | "is not allowed", declarator); |
655dc6ee | 1969 | else |
0f51ccfc | 1970 | error ("template-id %qD in declaration of primary template", |
0cbd7506 | 1971 | declarator); |
fd4de5ff | 1972 | return decl; |
386b8a85 | 1973 | } |
74b846e0 MM |
1974 | |
1975 | if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype)) | |
1976 | /* This is a specialization of a member template, without | |
1977 | specialization the containing class. Something like: | |
1978 | ||
1979 | template <class T> struct S { | |
c8094d83 | 1980 | template <class U> void f (U); |
0cbd7506 | 1981 | }; |
74b846e0 | 1982 | template <> template <class U> void S<int>::f(U) {} |
c8094d83 | 1983 | |
74b846e0 MM |
1984 | That's a specialization -- but of the entire template. */ |
1985 | specialization = 1; | |
1986 | break; | |
1987 | ||
1988 | default: | |
315fb5db | 1989 | gcc_unreachable (); |
75650646 | 1990 | } |
386b8a85 | 1991 | |
670960ac JM |
1992 | if (specialization || member_specialization) |
1993 | { | |
1994 | tree t = TYPE_ARG_TYPES (TREE_TYPE (decl)); | |
1995 | for (; t; t = TREE_CHAIN (t)) | |
1996 | if (TREE_PURPOSE (t)) | |
1997 | { | |
33bd39a2 | 1998 | pedwarn |
8251199e | 1999 | ("default argument specified in explicit specialization"); |
670960ac JM |
2000 | break; |
2001 | } | |
2002 | } | |
2003 | ||
e1467ff2 | 2004 | if (specialization || member_specialization || explicit_instantiation) |
75650646 MM |
2005 | { |
2006 | tree tmpl = NULL_TREE; | |
2007 | tree targs = NULL_TREE; | |
75650646 MM |
2008 | |
2009 | /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */ | |
386b8a85 JM |
2010 | if (TREE_CODE (declarator) != TEMPLATE_ID_EXPR) |
2011 | { | |
2012 | tree fns; | |
2013 | ||
50bc768d | 2014 | gcc_assert (TREE_CODE (declarator) == IDENTIFIER_NODE); |
2f54a1db | 2015 | if (ctype) |
386b8a85 | 2016 | fns = dname; |
2f54a1db GB |
2017 | else |
2018 | { | |
2019 | /* If there is no class context, the explicit instantiation | |
0cbd7506 | 2020 | must be at namespace scope. */ |
50bc768d | 2021 | gcc_assert (DECL_NAMESPACE_SCOPE_P (decl)); |
2f54a1db GB |
2022 | |
2023 | /* Find the namespace binding, using the declaration | |
0cbd7506 | 2024 | context. */ |
ddf74938 | 2025 | fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname, |
664a90c0 | 2026 | false, true); |
97dc8e5b | 2027 | if (fns == error_mark_node || !is_overloaded_fn (fns)) |
4230cec2 NS |
2028 | { |
2029 | error ("%qD is not a template function", dname); | |
2030 | fns = error_mark_node; | |
2031 | } | |
664a90c0 JM |
2032 | else |
2033 | { | |
2034 | tree fn = OVL_CURRENT (fns); | |
ddf74938 | 2035 | if (!is_associated_namespace (CP_DECL_CONTEXT (decl), |
08d295c5 JM |
2036 | CP_DECL_CONTEXT (fn))) |
2037 | error ("%qD is not declared in %qD", | |
2038 | decl, current_namespace); | |
664a90c0 | 2039 | } |
2f54a1db | 2040 | } |
386b8a85 | 2041 | |
2f54a1db | 2042 | declarator = lookup_template_function (fns, NULL_TREE); |
386b8a85 JM |
2043 | } |
2044 | ||
f2e48b67 BK |
2045 | if (declarator == error_mark_node) |
2046 | return error_mark_node; | |
2047 | ||
75650646 MM |
2048 | if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype)) |
2049 | { | |
8ec2ac34 | 2050 | if (!explicit_instantiation) |
0e339752 | 2051 | /* A specialization in class scope. This is invalid, |
6c30752f MM |
2052 | but the error will already have been flagged by |
2053 | check_specialization_scope. */ | |
2054 | return error_mark_node; | |
8ec2ac34 | 2055 | else |
b370501f | 2056 | { |
0e339752 | 2057 | /* It's not valid to write an explicit instantiation in |
b370501f | 2058 | class scope, e.g.: |
8ec2ac34 | 2059 | |
0cbd7506 | 2060 | class C { template void f(); } |
8ec2ac34 | 2061 | |
b370501f KG |
2062 | This case is caught by the parser. However, on |
2063 | something like: | |
c8094d83 | 2064 | |
b370501f | 2065 | template class C { void f(); }; |
8ec2ac34 | 2066 | |
0e339752 | 2067 | (which is invalid) we can get here. The error will be |
b370501f KG |
2068 | issued later. */ |
2069 | ; | |
2070 | } | |
8ec2ac34 | 2071 | |
e1467ff2 | 2072 | return decl; |
75650646 | 2073 | } |
c8094d83 | 2074 | else if (ctype != NULL_TREE |
75650646 MM |
2075 | && (TREE_CODE (TREE_OPERAND (declarator, 0)) == |
2076 | IDENTIFIER_NODE)) | |
386b8a85 | 2077 | { |
75650646 MM |
2078 | /* Find the list of functions in ctype that have the same |
2079 | name as the declared function. */ | |
2080 | tree name = TREE_OPERAND (declarator, 0); | |
03017874 MM |
2081 | tree fns = NULL_TREE; |
2082 | int idx; | |
2083 | ||
8ba658ee | 2084 | if (constructor_name_p (name, ctype)) |
386b8a85 | 2085 | { |
75650646 | 2086 | int is_constructor = DECL_CONSTRUCTOR_P (decl); |
c8094d83 | 2087 | |
75650646 | 2088 | if (is_constructor ? !TYPE_HAS_CONSTRUCTOR (ctype) |
9f4faeae | 2089 | : !CLASSTYPE_DESTRUCTORS (ctype)) |
75650646 MM |
2090 | { |
2091 | /* From [temp.expl.spec]: | |
c8094d83 | 2092 | |
75650646 MM |
2093 | If such an explicit specialization for the member |
2094 | of a class template names an implicitly-declared | |
2095 | special member function (clause _special_), the | |
c8094d83 | 2096 | program is ill-formed. |
e1467ff2 MM |
2097 | |
2098 | Similar language is found in [temp.explicit]. */ | |
33bd39a2 | 2099 | error ("specialization of implicitly-declared special member function"); |
03017874 | 2100 | return error_mark_node; |
75650646 | 2101 | } |
386b8a85 | 2102 | |
42da2fd8 | 2103 | name = is_constructor ? ctor_identifier : dtor_identifier; |
75650646 | 2104 | } |
42da2fd8 | 2105 | |
421844e7 | 2106 | if (!DECL_CONV_FN_P (decl)) |
03017874 MM |
2107 | { |
2108 | idx = lookup_fnfields_1 (ctype, name); | |
2109 | if (idx >= 0) | |
aaaa46d2 | 2110 | fns = VEC_index (tree, CLASSTYPE_METHOD_VEC (ctype), idx); |
03017874 MM |
2111 | } |
2112 | else | |
2113 | { | |
d4e6fecb | 2114 | VEC(tree,gc) *methods; |
aaaa46d2 | 2115 | tree ovl; |
03017874 MM |
2116 | |
2117 | /* For a type-conversion operator, we cannot do a | |
2118 | name-based lookup. We might be looking for `operator | |
2119 | int' which will be a specialization of `operator T'. | |
2120 | So, we find *all* the conversion operators, and then | |
2121 | select from them. */ | |
2122 | fns = NULL_TREE; | |
2123 | ||
2124 | methods = CLASSTYPE_METHOD_VEC (ctype); | |
2125 | if (methods) | |
5dd236e2 | 2126 | for (idx = CLASSTYPE_FIRST_CONVERSION_SLOT; |
9ba5ff0f | 2127 | VEC_iterate (tree, methods, idx, ovl); |
aaaa46d2 | 2128 | ++idx) |
03017874 | 2129 | { |
aaaa46d2 | 2130 | if (!DECL_CONV_FN_P (OVL_CURRENT (ovl))) |
03017874 MM |
2131 | /* There are no more conversion functions. */ |
2132 | break; | |
2133 | ||
2134 | /* Glue all these conversion functions together | |
2135 | with those we already have. */ | |
2136 | for (; ovl; ovl = OVL_NEXT (ovl)) | |
2137 | fns = ovl_cons (OVL_CURRENT (ovl), fns); | |
2138 | } | |
2139 | } | |
c8094d83 MS |
2140 | |
2141 | if (fns == NULL_TREE) | |
386b8a85 | 2142 | { |
0f51ccfc | 2143 | error ("no member function %qD declared in %qT", name, ctype); |
03017874 | 2144 | return error_mark_node; |
386b8a85 JM |
2145 | } |
2146 | else | |
2147 | TREE_OPERAND (declarator, 0) = fns; | |
2148 | } | |
c8094d83 | 2149 | |
e1467ff2 MM |
2150 | /* Figure out what exactly is being specialized at this point. |
2151 | Note that for an explicit instantiation, even one for a | |
38e01259 | 2152 | member function, we cannot tell apriori whether the |
e1467ff2 | 2153 | instantiation is for a member template, or just a member |
36a117a5 MM |
2154 | function of a template class. Even if a member template is |
2155 | being instantiated, the member template arguments may be | |
2156 | elided if they can be deduced from the rest of the | |
2157 | declaration. */ | |
e1467ff2 | 2158 | tmpl = determine_specialization (declarator, decl, |
c8094d83 | 2159 | &targs, |
5fe7b654 | 2160 | member_specialization, |
29a1da1c MM |
2161 | template_count, |
2162 | tsk); | |
c8094d83 | 2163 | |
03017874 MM |
2164 | if (!tmpl || tmpl == error_mark_node) |
2165 | /* We couldn't figure out what this declaration was | |
2166 | specializing. */ | |
2167 | return error_mark_node; | |
2168 | else | |
386b8a85 | 2169 | { |
25aab5d0 | 2170 | tree gen_tmpl = most_general_template (tmpl); |
36a117a5 | 2171 | |
e1467ff2 MM |
2172 | if (explicit_instantiation) |
2173 | { | |
03d0f4af | 2174 | /* We don't set DECL_EXPLICIT_INSTANTIATION here; that |
c8094d83 | 2175 | is done by do_decl_instantiation later. */ |
25aab5d0 MM |
2176 | |
2177 | int arg_depth = TMPL_ARGS_DEPTH (targs); | |
2178 | int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)); | |
2179 | ||
2180 | if (arg_depth > parm_depth) | |
2181 | { | |
2182 | /* If TMPL is not the most general template (for | |
2183 | example, if TMPL is a friend template that is | |
2184 | injected into namespace scope), then there will | |
dc957d14 | 2185 | be too many levels of TARGS. Remove some of them |
25aab5d0 MM |
2186 | here. */ |
2187 | int i; | |
2188 | tree new_targs; | |
2189 | ||
f31c0a32 | 2190 | new_targs = make_tree_vec (parm_depth); |
25aab5d0 MM |
2191 | for (i = arg_depth - parm_depth; i < arg_depth; ++i) |
2192 | TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth)) | |
2193 | = TREE_VEC_ELT (targs, i); | |
2194 | targs = new_targs; | |
2195 | } | |
c8094d83 | 2196 | |
3e4a3562 | 2197 | return instantiate_template (tmpl, targs, tf_error); |
e1467ff2 | 2198 | } |
74b846e0 | 2199 | |
6c07f448 KL |
2200 | /* If we thought that the DECL was a member function, but it |
2201 | turns out to be specializing a static member function, | |
4546865e | 2202 | make DECL a static member function as well. */ |
6c07f448 KL |
2203 | if (DECL_STATIC_FUNCTION_P (tmpl) |
2204 | && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)) | |
4546865e | 2205 | revert_static_member_fn (decl); |
6c07f448 | 2206 | |
f9a7ae04 | 2207 | /* If this is a specialization of a member template of a |
29a1da1c MM |
2208 | template class, we want to return the TEMPLATE_DECL, not |
2209 | the specialization of it. */ | |
74b846e0 MM |
2210 | if (tsk == tsk_template) |
2211 | { | |
2212 | SET_DECL_TEMPLATE_SPECIALIZATION (tmpl); | |
f9a7ae04 | 2213 | DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl)) = NULL_TREE; |
b190f239 NS |
2214 | if (have_def) |
2215 | { | |
f31686a3 RH |
2216 | DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl); |
2217 | DECL_SOURCE_LOCATION (DECL_TEMPLATE_RESULT (tmpl)) | |
2218 | = DECL_SOURCE_LOCATION (decl); | |
08167d1c AO |
2219 | /* We want to use the argument list specified in the |
2220 | definition, not in the original declaration. */ | |
2221 | DECL_ARGUMENTS (DECL_TEMPLATE_RESULT (tmpl)) | |
2222 | = DECL_ARGUMENTS (decl); | |
b190f239 | 2223 | } |
74b846e0 MM |
2224 | return tmpl; |
2225 | } | |
2226 | ||
36a117a5 | 2227 | /* Set up the DECL_TEMPLATE_INFO for DECL. */ |
e1b3e07d | 2228 | DECL_TEMPLATE_INFO (decl) = tree_cons (tmpl, targs, NULL_TREE); |
36a117a5 | 2229 | |
8afa707f MM |
2230 | /* Inherit default function arguments from the template |
2231 | DECL is specializing. */ | |
2232 | copy_default_args_to_explicit_spec (decl); | |
2233 | ||
c750255c MM |
2234 | /* This specialization has the same protection as the |
2235 | template it specializes. */ | |
2236 | TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl); | |
2237 | TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl); | |
b9e75696 | 2238 | |
736b8100 SB |
2239 | /* 7.1.1-1 [dcl.stc] |
2240 | ||
2241 | A storage-class-specifier shall not be specified in an | |
2242 | explicit specialization... | |
2243 | ||
2244 | The parser rejects these, so unless action is taken here, | |
2245 | explicit function specializations will always appear with | |
2246 | global linkage. | |
2247 | ||
2248 | The action recommended by the C++ CWG in response to C++ | |
2249 | defect report 605 is to make the storage class and linkage | |
2250 | of the explicit specialization match the templated function: | |
2251 | ||
2252 | http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605 | |
2253 | */ | |
2254 | if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl)) | |
2255 | { | |
2256 | tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl); | |
2257 | gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL); | |
2258 | ||
d732e98f | 2259 | /* This specialization has the same linkage and visibility as |
736b8100 SB |
2260 | the function template it specializes. */ |
2261 | TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func); | |
92d0af97 JJ |
2262 | if (! TREE_PUBLIC (decl)) |
2263 | { | |
2264 | DECL_INTERFACE_KNOWN (decl) = 1; | |
2265 | DECL_NOT_REALLY_EXTERN (decl) = 1; | |
2266 | } | |
736b8100 SB |
2267 | DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func); |
2268 | if (DECL_VISIBILITY_SPECIFIED (tmpl_func)) | |
2269 | { | |
2270 | DECL_VISIBILITY_SPECIFIED (decl) = 1; | |
2271 | DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func); | |
2272 | } | |
2273 | } | |
2274 | ||
9c12301f MM |
2275 | /* If DECL is a friend declaration, declared using an |
2276 | unqualified name, the namespace associated with DECL may | |
2277 | have been set incorrectly. For example, in: | |
3db45ab5 MS |
2278 | |
2279 | template <typename T> void f(T); | |
2280 | namespace N { | |
2281 | struct S { friend void f<int>(int); } | |
2282 | } | |
2283 | ||
2284 | we will have set the DECL_CONTEXT for the friend | |
2285 | declaration to N, rather than to the global namespace. */ | |
9c12301f MM |
2286 | if (DECL_NAMESPACE_SCOPE_P (decl)) |
2287 | DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl); | |
8d039470 | 2288 | |
386b8a85 | 2289 | if (is_friend && !have_def) |
36a117a5 MM |
2290 | /* This is not really a declaration of a specialization. |
2291 | It's just the name of an instantiation. But, it's not | |
2292 | a request for an instantiation, either. */ | |
fbf1c34b | 2293 | SET_DECL_IMPLICIT_INSTANTIATION (decl); |
08511114 KL |
2294 | else if (DECL_CONSTRUCTOR_P (decl) || DECL_DESTRUCTOR_P (decl)) |
2295 | /* This is indeed a specialization. In case of constructors | |
2296 | and destructors, we need in-charge and not-in-charge | |
2297 | versions in V3 ABI. */ | |
2298 | clone_function_decl (decl, /*update_method_vec_p=*/0); | |
75650646 | 2299 | |
36a117a5 MM |
2300 | /* Register this specialization so that we can find it |
2301 | again. */ | |
d63d5d0c | 2302 | decl = register_specialization (decl, gen_tmpl, targs, is_friend); |
386b8a85 JM |
2303 | } |
2304 | } | |
c8094d83 | 2305 | |
e1467ff2 | 2306 | return decl; |
386b8a85 | 2307 | } |
75650646 | 2308 | |
75650646 MM |
2309 | /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template |
2310 | parameters. These are represented in the same format used for | |
2311 | DECL_TEMPLATE_PARMS. */ | |
2312 | ||
50bc768d | 2313 | int |
58f9752a | 2314 | comp_template_parms (const_tree parms1, const_tree parms2) |
75650646 | 2315 | { |
58f9752a KG |
2316 | const_tree p1; |
2317 | const_tree p2; | |
75650646 MM |
2318 | |
2319 | if (parms1 == parms2) | |
2320 | return 1; | |
2321 | ||
c8094d83 | 2322 | for (p1 = parms1, p2 = parms2; |
75650646 MM |
2323 | p1 != NULL_TREE && p2 != NULL_TREE; |
2324 | p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2)) | |
2325 | { | |
2326 | tree t1 = TREE_VALUE (p1); | |
2327 | tree t2 = TREE_VALUE (p2); | |
2328 | int i; | |
2329 | ||
50bc768d NS |
2330 | gcc_assert (TREE_CODE (t1) == TREE_VEC); |
2331 | gcc_assert (TREE_CODE (t2) == TREE_VEC); | |
75650646 MM |
2332 | |
2333 | if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2)) | |
2334 | return 0; | |
2335 | ||
c8094d83 | 2336 | for (i = 0; i < TREE_VEC_LENGTH (t2); ++i) |
75650646 | 2337 | { |
0f67a82f LM |
2338 | tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i)); |
2339 | tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i)); | |
42b304f1 | 2340 | |
0f67a82f LM |
2341 | /* If either of the template parameters are invalid, assume |
2342 | they match for the sake of error recovery. */ | |
2343 | if (parm1 == error_mark_node || parm2 == error_mark_node) | |
2344 | return 1; | |
75650646 MM |
2345 | |
2346 | if (TREE_CODE (parm1) != TREE_CODE (parm2)) | |
2347 | return 0; | |
2348 | ||
5d80a306 DG |
2349 | if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM |
2350 | && (TEMPLATE_TYPE_PARAMETER_PACK (parm1) | |
2351 | == TEMPLATE_TYPE_PARAMETER_PACK (parm2))) | |
75650646 | 2352 | continue; |
3bfdc719 | 2353 | else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2))) |
75650646 MM |
2354 | return 0; |
2355 | } | |
2356 | } | |
2357 | ||
2358 | if ((p1 != NULL_TREE) != (p2 != NULL_TREE)) | |
2359 | /* One set of parameters has more parameters lists than the | |
2360 | other. */ | |
2361 | return 0; | |
2362 | ||
2363 | return 1; | |
2364 | } | |
2365 | ||
5d80a306 DG |
2366 | /* Determine whether PARM is a parameter pack. */ |
2367 | bool | |
58f9752a | 2368 | template_parameter_pack_p (const_tree parm) |
5d80a306 DG |
2369 | { |
2370 | /* Determine if we have a non-type template parameter pack. */ | |
2371 | if (TREE_CODE (parm) == PARM_DECL) | |
2372 | return (DECL_TEMPLATE_PARM_P (parm) | |
2373 | && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))); | |
2374 | ||
2375 | /* If this is a list of template parameters, we could get a | |
2376 | TYPE_DECL or a TEMPLATE_DECL. */ | |
2377 | if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL) | |
2378 | parm = TREE_TYPE (parm); | |
2379 | ||
2380 | return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM | |
2381 | || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM) | |
2382 | && TEMPLATE_TYPE_PARAMETER_PACK (parm)); | |
2383 | } | |
2384 | ||
5d80a306 DG |
2385 | /* Determine whether ARGS describes a variadic template args list, |
2386 | i.e., one that is terminated by a template argument pack. */ | |
2387 | static bool | |
2388 | template_args_variadic_p (tree args) | |
2389 | { | |
2390 | int nargs; | |
2391 | tree last_parm; | |
2392 | ||
2393 | if (args == NULL_TREE) | |
2394 | return false; | |
2395 | ||
2396 | args = INNERMOST_TEMPLATE_ARGS (args); | |
2397 | nargs = TREE_VEC_LENGTH (args); | |
2398 | ||
2399 | if (nargs == 0) | |
2400 | return false; | |
2401 | ||
2402 | last_parm = TREE_VEC_ELT (args, nargs - 1); | |
2403 | ||
2404 | return ARGUMENT_PACK_P (last_parm); | |
2405 | } | |
2406 | ||
2407 | /* Generate a new name for the parameter pack name NAME (an | |
2408 | IDENTIFIER_NODE) that incorporates its */ | |
2409 | static tree | |
2410 | make_ith_pack_parameter_name (tree name, int i) | |
2411 | { | |
2412 | /* Munge the name to include the parameter index. */ | |
2413 | char numbuf[128]; | |
2414 | char* newname; | |
2415 | ||
2416 | sprintf(numbuf, "%i", i); | |
2417 | newname = (char*)alloca (IDENTIFIER_LENGTH (name) + strlen(numbuf) + 2); | |
2418 | sprintf(newname, "%s#%i", IDENTIFIER_POINTER (name), i); | |
2419 | return get_identifier (newname); | |
2420 | } | |
2421 | ||
1ad8aeeb | 2422 | /* Structure used to track the progress of find_parameter_packs_r. */ |
5d80a306 DG |
2423 | struct find_parameter_pack_data |
2424 | { | |
1ad8aeeb DG |
2425 | /* TREE_LIST that will contain all of the parameter packs found by |
2426 | the traversal. */ | |
5d80a306 | 2427 | tree* parameter_packs; |
1ad8aeeb DG |
2428 | |
2429 | /* Set of AST nodes that have been visited by the traversal. */ | |
5d80a306 | 2430 | struct pointer_set_t *visited; |
1ad8aeeb DG |
2431 | |
2432 | /* Whether we should replace parameter packs with | |
2433 | ERROR_MARK_NODE. Used by check_for_bare_parameter_packs. */ | |
2434 | bool set_packs_to_error; | |
5d80a306 DG |
2435 | }; |
2436 | ||
88b82314 | 2437 | /* Identifies all of the argument packs that occur in a template |
5d80a306 | 2438 | argument and appends them to the TREE_LIST inside DATA, which is a |
88b82314 | 2439 | find_parameter_pack_data structure. This is a subroutine of |
5d80a306 DG |
2440 | make_pack_expansion and uses_parameter_packs. */ |
2441 | static tree | |
2442 | find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data) | |
2443 | { | |
2444 | tree t = *tp; | |
2445 | struct find_parameter_pack_data* ppd = | |
2446 | (struct find_parameter_pack_data*)data; | |
1ad8aeeb | 2447 | bool parameter_pack_p = false; |
5d80a306 | 2448 | |
1ad8aeeb DG |
2449 | /* Don't visit nodes twice, except when we're clearing out parameter |
2450 | packs. */ | |
2451 | if (pointer_set_contains (ppd->visited, *tp)) | |
5d80a306 | 2452 | { |
1ad8aeeb DG |
2453 | *walk_subtrees = 0; |
2454 | return NULL_TREE; | |
5d80a306 DG |
2455 | } |
2456 | ||
1ad8aeeb DG |
2457 | /* Identify whether this is a parameter pack or not. */ |
2458 | switch (TREE_CODE (t)) | |
2459 | { | |
2460 | case TEMPLATE_PARM_INDEX: | |
2461 | if (TEMPLATE_PARM_PARAMETER_PACK (t)) | |
2462 | parameter_pack_p = true; | |
2463 | break; | |
2464 | ||
2465 | case TEMPLATE_TYPE_PARM: | |
2466 | case TEMPLATE_TEMPLATE_PARM: | |
2467 | if (TEMPLATE_TYPE_PARAMETER_PACK (t)) | |
2468 | parameter_pack_p = true; | |
2469 | break; | |
2470 | ||
2471 | case PARM_DECL: | |
2472 | if (FUNCTION_PARAMETER_PACK_P (t)) | |
2473 | { | |
2474 | /* We don't want to walk into the type of a PARM_DECL, | |
2475 | because we don't want to see the type parameter pack. */ | |
2476 | *walk_subtrees = 0; | |
2477 | parameter_pack_p = true; | |
2478 | } | |
2479 | break; | |
2480 | ||
2481 | default: | |
2482 | /* Not a parameter pack. */ | |
2483 | break; | |
2484 | } | |
2485 | ||
2486 | if (parameter_pack_p) | |
2487 | { | |
2488 | /* Add this parameter pack to the list. */ | |
2489 | *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs); | |
2490 | ||
2491 | if (ppd->set_packs_to_error) | |
2492 | /* The caller requested that we set the parameter packs to | |
2493 | ERROR_MARK_NODE so that they will not trip up the compiler | |
2494 | later. The caller is responsible for emitting an error. */ | |
2495 | *tp = error_mark_node; | |
2496 | else | |
2497 | /* Make sure we do not visit this node again. */ | |
2498 | pointer_set_insert (ppd->visited, *tp); | |
2499 | } | |
2500 | else | |
2501 | /* Make sure we do not visit this node again. */ | |
2502 | pointer_set_insert (ppd->visited, *tp); | |
2503 | ||
2504 | if (TYPE_P (t)) | |
2505 | cp_walk_tree (&TYPE_CONTEXT (t), | |
2506 | &find_parameter_packs_r, ppd, NULL); | |
2507 | ||
5d80a306 DG |
2508 | /* This switch statement will return immediately if we don't find a |
2509 | parameter pack. */ | |
2510 | switch (TREE_CODE (t)) | |
2511 | { | |
2512 | case TEMPLATE_PARM_INDEX: | |
5d80a306 DG |
2513 | return NULL_TREE; |
2514 | ||
2515 | case BOUND_TEMPLATE_TEMPLATE_PARM: | |
1ad8aeeb DG |
2516 | /* Check the template itself. */ |
2517 | cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)), | |
2518 | &find_parameter_packs_r, ppd, NULL); | |
5d80a306 | 2519 | /* Check the template arguments. */ |
14588106 | 2520 | cp_walk_tree (&TYPE_TI_ARGS (t), &find_parameter_packs_r, ppd, |
1ad8aeeb | 2521 | NULL); |
5d80a306 | 2522 | *walk_subtrees = 0; |
1ad8aeeb | 2523 | return NULL_TREE; |
5d80a306 DG |
2524 | |
2525 | case TEMPLATE_TYPE_PARM: | |
2526 | case TEMPLATE_TEMPLATE_PARM: | |
5d80a306 DG |
2527 | return NULL_TREE; |
2528 | ||
2529 | case PARM_DECL: | |
5d80a306 DG |
2530 | return NULL_TREE; |
2531 | ||
2532 | case RECORD_TYPE: | |
2533 | if (TYPE_PTRMEMFUNC_P (t)) | |
2534 | return NULL_TREE; | |
2535 | /* Fall through. */ | |
2536 | ||
2537 | case UNION_TYPE: | |
2538 | case ENUMERAL_TYPE: | |
2539 | if (TYPE_TEMPLATE_INFO (t)) | |
1ad8aeeb DG |
2540 | cp_walk_tree (&TREE_VALUE (TYPE_TEMPLATE_INFO (t)), |
2541 | &find_parameter_packs_r, ppd, NULL); | |
5d80a306 DG |
2542 | |
2543 | *walk_subtrees = 0; | |
2544 | return NULL_TREE; | |
2545 | ||
2546 | case TEMPLATE_DECL: | |
1ad8aeeb DG |
2547 | cp_walk_tree (&TREE_TYPE (t), |
2548 | &find_parameter_packs_r, ppd, NULL); | |
5d80a306 | 2549 | return NULL_TREE; |
e1a18c68 DG |
2550 | |
2551 | case TYPENAME_TYPE: | |
2552 | cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t), &find_parameter_packs_r, | |
1ad8aeeb | 2553 | ppd, NULL); |
e1a18c68 DG |
2554 | *walk_subtrees = 0; |
2555 | return NULL_TREE; | |
2556 | ||
2557 | ||
5d80a306 DG |
2558 | case TYPE_PACK_EXPANSION: |
2559 | case EXPR_PACK_EXPANSION: | |
2560 | *walk_subtrees = 0; | |
2561 | return NULL_TREE; | |
2562 | ||
d393153e | 2563 | case INTEGER_TYPE: |
14588106 | 2564 | cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r, |
1ad8aeeb | 2565 | ppd, NULL); |
d393153e DG |
2566 | *walk_subtrees = 0; |
2567 | return NULL_TREE; | |
2568 | ||
5d80a306 DG |
2569 | default: |
2570 | return NULL_TREE; | |
2571 | } | |
5d80a306 DG |
2572 | |
2573 | return NULL_TREE; | |
2574 | } | |
2575 | ||
2576 | /* Determines if the expression or type T uses any parameter packs. */ | |
2577 | bool | |
2578 | uses_parameter_packs (tree t) | |
2579 | { | |
2580 | tree parameter_packs = NULL_TREE; | |
2581 | struct find_parameter_pack_data ppd; | |
2582 | ppd.parameter_packs = ¶meter_packs; | |
2583 | ppd.visited = pointer_set_create (); | |
1ad8aeeb DG |
2584 | ppd.set_packs_to_error = false; |
2585 | cp_walk_tree (&t, &find_parameter_packs_r, &ppd, NULL); | |
30bcc028 | 2586 | pointer_set_destroy (ppd.visited); |
5d80a306 DG |
2587 | return parameter_packs != NULL_TREE; |
2588 | } | |
2589 | ||
2590 | /* Turn ARG, which may be an expression, type, or a TREE_LIST | |
2591 | representation a base-class initializer into a parameter pack | |
2592 | expansion. If all goes well, the resulting node will be an | |
2593 | EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST, | |
2594 | respectively. */ | |
2595 | tree | |
2596 | make_pack_expansion (tree arg) | |
2597 | { | |
2598 | tree result; | |
2599 | tree parameter_packs = NULL_TREE; | |
2600 | bool for_types = false; | |
2601 | struct find_parameter_pack_data ppd; | |
2602 | ||
1ad8aeeb DG |
2603 | ppd.set_packs_to_error = false; |
2604 | ||
5d80a306 DG |
2605 | if (!arg || arg == error_mark_node) |
2606 | return arg; | |
2607 | ||
2608 | if (TREE_CODE (arg) == TREE_LIST) | |
2609 | { | |
2610 | /* The only time we will see a TREE_LIST here is for a base | |
2611 | class initializer. In this case, the TREE_PURPOSE will be a | |
2612 | _TYPE node (representing the base class expansion we're | |
2613 | initializing) and the TREE_VALUE will be a TREE_LIST | |
2614 | containing the initialization arguments. | |
2615 | ||
2616 | The resulting expansion looks somewhat different from most | |
2617 | expansions. Rather than returning just one _EXPANSION, we | |
2618 | return a TREE_LIST whose TREE_PURPOSE is a | |
2619 | TYPE_PACK_EXPANSION containing the bases that will be | |
2620 | initialized. The TREE_VALUE will be identical to the | |
2621 | original TREE_VALUE, which is a list of arguments that will | |
2622 | be passed to each base. We do not introduce any new pack | |
2623 | expansion nodes into the TREE_VALUE (although it is possible | |
2624 | that some already exist), because the TREE_PURPOSE and | |
2625 | TREE_VALUE all need to be expanded together with the same | |
2626 | _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the | |
2627 | resulting TREE_PURPOSE will mention the parameter packs in | |
2628 | both the bases and the arguments to the bases. */ | |
2629 | tree purpose; | |
2630 | tree value; | |
2631 | tree parameter_packs = NULL_TREE; | |
2632 | ||
2633 | /* Determine which parameter packs will be used by the base | |
2634 | class expansion. */ | |
2635 | ppd.visited = pointer_set_create (); | |
2636 | ppd.parameter_packs = ¶meter_packs; | |
14588106 | 2637 | cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r, |
1ad8aeeb | 2638 | &ppd, NULL); |
5d80a306 DG |
2639 | |
2640 | if (parameter_packs == NULL_TREE) | |
2641 | { | |
2642 | error ("base initializer expansion %<%T%> contains no parameter packs", arg); | |
30bcc028 | 2643 | pointer_set_destroy (ppd.visited); |
5d80a306 DG |
2644 | return error_mark_node; |
2645 | } | |
2646 | ||
2647 | if (TREE_VALUE (arg) != void_type_node) | |
2648 | { | |
2649 | /* Collect the sets of parameter packs used in each of the | |
2650 | initialization arguments. */ | |
2651 | for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value)) | |
2652 | { | |
2653 | /* Determine which parameter packs will be expanded in this | |
2654 | argument. */ | |
14588106 | 2655 | cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r, |
1ad8aeeb | 2656 | &ppd, NULL); |
5d80a306 DG |
2657 | } |
2658 | } | |
2659 | ||
30bcc028 DG |
2660 | pointer_set_destroy (ppd.visited); |
2661 | ||
5d80a306 DG |
2662 | /* Create the pack expansion type for the base type. */ |
2663 | purpose = make_node (TYPE_PACK_EXPANSION); | |
2664 | SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg)); | |
2665 | PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs; | |
2666 | ||
2667 | /* Just use structural equality for these TYPE_PACK_EXPANSIONS; | |
2668 | they will rarely be compared to anything. */ | |
2669 | SET_TYPE_STRUCTURAL_EQUALITY (purpose); | |
2670 | ||
2671 | return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE); | |
2672 | } | |
2673 | ||
2674 | if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL) | |
2675 | for_types = true; | |
2676 | ||
2677 | /* Build the PACK_EXPANSION_* node. */ | |
2678 | result = make_node (for_types ? TYPE_PACK_EXPANSION : EXPR_PACK_EXPANSION); | |
2679 | SET_PACK_EXPANSION_PATTERN (result, arg); | |
2680 | if (TREE_CODE (result) == EXPR_PACK_EXPANSION) | |
2681 | { | |
2682 | /* Propagate type and const-expression information. */ | |
2683 | TREE_TYPE (result) = TREE_TYPE (arg); | |
2684 | TREE_CONSTANT (result) = TREE_CONSTANT (arg); | |
2685 | } | |
2686 | else | |
2687 | /* Just use structural equality for these TYPE_PACK_EXPANSIONS; | |
2688 | they will rarely be compared to anything. */ | |
2689 | SET_TYPE_STRUCTURAL_EQUALITY (result); | |
2690 | ||
2691 | /* Determine which parameter packs will be expanded. */ | |
2692 | ppd.parameter_packs = ¶meter_packs; | |
2693 | ppd.visited = pointer_set_create (); | |
1ad8aeeb | 2694 | cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, NULL); |
30bcc028 | 2695 | pointer_set_destroy (ppd.visited); |
5d80a306 DG |
2696 | |
2697 | /* Make sure we found some parameter packs. */ | |
2698 | if (parameter_packs == NULL_TREE) | |
2699 | { | |
2700 | if (TYPE_P (arg)) | |
2701 | error ("expansion pattern %<%T%> contains no argument packs", arg); | |
2702 | else | |
2703 | error ("expansion pattern %<%E%> contains no argument packs", arg); | |
2704 | return error_mark_node; | |
2705 | } | |
2706 | PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs; | |
2707 | ||
2708 | return result; | |
2709 | } | |
2710 | ||
2711 | /* Checks T for any "bare" parameter packs, which have not yet been | |
2712 | expanded, and issues an error if any are found. This operation can | |
2713 | only be done on full expressions or types (e.g., an expression | |
2714 | statement, "if" condition, etc.), because we could have expressions like: | |
2715 | ||
2716 | foo(f(g(h(args)))...) | |
2717 | ||
2718 | where "args" is a parameter pack. check_for_bare_parameter_packs | |
2719 | should not be called for the subexpressions args, h(args), | |
2720 | g(h(args)), or f(g(h(args))), because we would produce erroneous | |
b1d7b1c0 DG |
2721 | error messages. |
2722 | ||
2723 | Returns TRUE if there were no bare parameter packs, returns FALSE | |
2724 | (and emits an error) if there were bare parameter packs.*/ | |
2725 | bool | |
1ad8aeeb | 2726 | check_for_bare_parameter_packs (tree* t) |
5d80a306 DG |
2727 | { |
2728 | tree parameter_packs = NULL_TREE; | |
2729 | struct find_parameter_pack_data ppd; | |
2730 | ||
1ad8aeeb | 2731 | if (!processing_template_decl || !t || !*t || *t == error_mark_node) |
b1d7b1c0 | 2732 | return true; |
5d80a306 | 2733 | |
1ad8aeeb DG |
2734 | if (TREE_CODE (*t) == TYPE_DECL) |
2735 | t = &TREE_TYPE (*t); | |
5d80a306 DG |
2736 | |
2737 | ppd.parameter_packs = ¶meter_packs; | |
2738 | ppd.visited = pointer_set_create (); | |
1ad8aeeb DG |
2739 | ppd.set_packs_to_error = false; |
2740 | cp_walk_tree (t, &find_parameter_packs_r, &ppd, NULL); | |
30bcc028 | 2741 | pointer_set_destroy (ppd.visited); |
5d80a306 | 2742 | |
b1d7b1c0 DG |
2743 | if (parameter_packs) |
2744 | { | |
2745 | error ("parameter packs not expanded with `...':"); | |
2746 | while (parameter_packs) | |
2747 | { | |
2748 | tree pack = TREE_VALUE (parameter_packs); | |
2749 | tree name = NULL_TREE; | |
2750 | ||
2751 | if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM | |
2752 | || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM) | |
2753 | name = TYPE_NAME (pack); | |
2754 | else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX) | |
2755 | name = DECL_NAME (TEMPLATE_PARM_DECL (pack)); | |
2756 | else | |
2757 | name = DECL_NAME (pack); | |
1ad8aeeb DG |
2758 | |
2759 | if (name) | |
2760 | inform (" %qD", name); | |
2761 | else | |
2762 | inform (" <anonymous>"); | |
b1d7b1c0 DG |
2763 | |
2764 | parameter_packs = TREE_CHAIN (parameter_packs); | |
2765 | } | |
2766 | ||
1ad8aeeb DG |
2767 | /* Clean up any references to these parameter packs within the |
2768 | tree. */ | |
2769 | ppd.parameter_packs = ¶meter_packs; | |
2770 | ppd.visited = pointer_set_create (); | |
2771 | ppd.set_packs_to_error = true; | |
2772 | cp_walk_tree (t, &find_parameter_packs_r, &ppd, NULL); | |
2773 | pointer_set_destroy (ppd.visited); | |
2774 | ||
b1d7b1c0 DG |
2775 | return false; |
2776 | } | |
2777 | ||
2778 | return true; | |
5d80a306 DG |
2779 | } |
2780 | ||
2781 | /* Expand any parameter packs that occur in the template arguments in | |
2782 | ARGS. */ | |
2783 | tree | |
2784 | expand_template_argument_pack (tree args) | |
2785 | { | |
2786 | tree result_args = NULL_TREE; | |
2787 | int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0; | |
2788 | int num_result_args = -1; | |
2789 | ||
2790 | /* First, determine if we need to expand anything, and the number of | |
2791 | slots we'll need. */ | |
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 | int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)); | |
2798 | if (num_result_args < 0) | |
2799 | num_result_args = in_arg + num_packed; | |
2800 | else | |
2801 | num_result_args += num_packed; | |
2802 | } | |
2803 | else | |
2804 | { | |
2805 | if (num_result_args >= 0) | |
2806 | num_result_args++; | |
2807 | } | |
2808 | } | |
2809 | ||
2810 | /* If no expansion is necessary, we're done. */ | |
2811 | if (num_result_args < 0) | |
2812 | return args; | |
2813 | ||
2814 | /* Expand arguments. */ | |
2815 | result_args = make_tree_vec (num_result_args); | |
2816 | for (in_arg = 0; in_arg < nargs; ++in_arg) | |
2817 | { | |
2818 | tree arg = TREE_VEC_ELT (args, in_arg); | |
2819 | if (ARGUMENT_PACK_P (arg)) | |
2820 | { | |
2821 | tree packed = ARGUMENT_PACK_ARGS (arg); | |
2822 | int i, num_packed = TREE_VEC_LENGTH (packed); | |
2823 | for (i = 0; i < num_packed; ++i, ++out_arg) | |
2824 | TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i); | |
2825 | } | |
2826 | else | |
2827 | { | |
2828 | TREE_VEC_ELT (result_args, out_arg) = arg; | |
2829 | ++out_arg; | |
2830 | } | |
2831 | } | |
2832 | ||
2833 | return result_args; | |
2834 | } | |
2835 | ||
f3400fe2 JM |
2836 | /* Complain if DECL shadows a template parameter. |
2837 | ||
2838 | [temp.local]: A template-parameter shall not be redeclared within its | |
2839 | scope (including nested scopes). */ | |
2840 | ||
2841 | void | |
3a978d72 | 2842 | check_template_shadow (tree decl) |
f3400fe2 | 2843 | { |
8f032717 MM |
2844 | tree olddecl; |
2845 | ||
b5d9b9ab MM |
2846 | /* If we're not in a template, we can't possibly shadow a template |
2847 | parameter. */ | |
2848 | if (!current_template_parms) | |
2849 | return; | |
2850 | ||
2851 | /* Figure out what we're shadowing. */ | |
8f032717 MM |
2852 | if (TREE_CODE (decl) == OVERLOAD) |
2853 | decl = OVL_CURRENT (decl); | |
90ea9897 | 2854 | olddecl = innermost_non_namespace_value (DECL_NAME (decl)); |
f3400fe2 | 2855 | |
b5d9b9ab MM |
2856 | /* If there's no previous binding for this name, we're not shadowing |
2857 | anything, let alone a template parameter. */ | |
2858 | if (!olddecl) | |
2859 | return; | |
2860 | ||
2861 | /* If we're not shadowing a template parameter, we're done. Note | |
2862 | that OLDDECL might be an OVERLOAD (or perhaps even an | |
2863 | ERROR_MARK), so we can't just blithely assume it to be a _DECL | |
2864 | node. */ | |
2f939d94 | 2865 | if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl)) |
b5d9b9ab MM |
2866 | return; |
2867 | ||
2868 | /* We check for decl != olddecl to avoid bogus errors for using a | |
2869 | name inside a class. We check TPFI to avoid duplicate errors for | |
2870 | inline member templates. */ | |
c8094d83 | 2871 | if (decl == olddecl |
b5d9b9ab MM |
2872 | || TEMPLATE_PARMS_FOR_INLINE (current_template_parms)) |
2873 | return; | |
2874 | ||
dee15844 JM |
2875 | error ("declaration of %q+#D", decl); |
2876 | error (" shadows template parm %q+#D", olddecl); | |
f3400fe2 | 2877 | } |
22a7be53 | 2878 | |
f3400fe2 | 2879 | /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL, |
f84b4be9 JM |
2880 | ORIG_LEVEL, DECL, and TYPE. */ |
2881 | ||
2882 | static tree | |
c8094d83 | 2883 | build_template_parm_index (int index, |
0cbd7506 MS |
2884 | int level, |
2885 | int orig_level, | |
2886 | tree decl, | |
2887 | tree type) | |
f84b4be9 JM |
2888 | { |
2889 | tree t = make_node (TEMPLATE_PARM_INDEX); | |
2890 | TEMPLATE_PARM_IDX (t) = index; | |
2891 | TEMPLATE_PARM_LEVEL (t) = level; | |
2892 | TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level; | |
2893 | TEMPLATE_PARM_DECL (t) = decl; | |
2894 | TREE_TYPE (t) = type; | |
3e4a3562 | 2895 | TREE_CONSTANT (t) = TREE_CONSTANT (decl); |
6de9cd9a | 2896 | TREE_INVARIANT (t) = TREE_INVARIANT (decl); |
3e4a3562 | 2897 | TREE_READONLY (t) = TREE_READONLY (decl); |
f84b4be9 JM |
2898 | |
2899 | return t; | |
2900 | } | |
2901 | ||
06d40de8 | 2902 | /* Find the canonical type parameter for the given template type |
c3e188fc KH |
2903 | parameter. Returns the canonical type parameter, which may be TYPE |
2904 | if no such parameter existed. */ | |
06d40de8 DG |
2905 | static tree |
2906 | canonical_type_parameter (tree type) | |
2907 | { | |
2908 | tree list; | |
2909 | int idx = TEMPLATE_TYPE_IDX (type); | |
2910 | if (!canonical_template_parms) | |
2911 | canonical_template_parms = VEC_alloc (tree, gc, idx+1); | |
2912 | ||
2913 | while (VEC_length (tree, canonical_template_parms) <= (unsigned)idx) | |
2914 | VEC_safe_push (tree, gc, canonical_template_parms, NULL_TREE); | |
2915 | ||
2916 | list = VEC_index (tree, canonical_template_parms, idx); | |
2917 | while (list && !comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL)) | |
2918 | list = TREE_CHAIN (list); | |
2919 | ||
2920 | if (list) | |
2921 | return TREE_VALUE (list); | |
2922 | else | |
2923 | { | |
2924 | VEC_replace(tree, canonical_template_parms, idx, | |
2925 | tree_cons (NULL_TREE, type, | |
2926 | VEC_index (tree, canonical_template_parms, idx))); | |
2927 | return type; | |
2928 | } | |
2929 | } | |
2930 | ||
f84b4be9 | 2931 | /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose |
93cdc044 | 2932 | TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a |
f84b4be9 JM |
2933 | TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a |
2934 | new one is created. */ | |
2935 | ||
c8094d83 | 2936 | static tree |
ef3b7b17 JM |
2937 | reduce_template_parm_level (tree index, tree type, int levels, tree args, |
2938 | tsubst_flags_t complain) | |
f84b4be9 JM |
2939 | { |
2940 | if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE | |
2941 | || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index)) | |
93cdc044 | 2942 | != TEMPLATE_PARM_LEVEL (index) - levels)) |
f84b4be9 | 2943 | { |
3e4a3562 NS |
2944 | tree orig_decl = TEMPLATE_PARM_DECL (index); |
2945 | tree decl, t; | |
c8094d83 | 2946 | |
3e4a3562 NS |
2947 | decl = build_decl (TREE_CODE (orig_decl), DECL_NAME (orig_decl), type); |
2948 | TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl); | |
6de9cd9a | 2949 | TREE_INVARIANT (decl) = TREE_INVARIANT (orig_decl); |
3e4a3562 NS |
2950 | TREE_READONLY (decl) = TREE_READONLY (orig_decl); |
2951 | DECL_ARTIFICIAL (decl) = 1; | |
2952 | SET_DECL_TEMPLATE_PARM_P (decl); | |
c8094d83 | 2953 | |
3e4a3562 | 2954 | t = build_template_parm_index (TEMPLATE_PARM_IDX (index), |
93cdc044 | 2955 | TEMPLATE_PARM_LEVEL (index) - levels, |
f84b4be9 JM |
2956 | TEMPLATE_PARM_ORIG_LEVEL (index), |
2957 | decl, type); | |
2958 | TEMPLATE_PARM_DESCENDANTS (index) = t; | |
5d80a306 DG |
2959 | TEMPLATE_PARM_PARAMETER_PACK (t) |
2960 | = TEMPLATE_PARM_PARAMETER_PACK (index); | |
cae40af6 | 2961 | |
820cc88f | 2962 | /* Template template parameters need this. */ |
ef3b7b17 JM |
2963 | if (TREE_CODE (decl) == TEMPLATE_DECL) |
2964 | DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms | |
2965 | (DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index)), | |
2966 | args, complain); | |
f84b4be9 JM |
2967 | } |
2968 | ||
2969 | return TEMPLATE_PARM_DESCENDANTS (index); | |
2970 | } | |
2971 | ||
a14de1af | 2972 | /* Process information from new template parameter PARM and append it to the |
058b15c1 | 2973 | LIST being built. This new parameter is a non-type parameter iff |
5d80a306 DG |
2974 | IS_NON_TYPE is true. This new parameter is a parameter |
2975 | pack iff IS_PARAMETER_PACK is true. */ | |
e92cc029 | 2976 | |
8d08fdba | 2977 | tree |
5d80a306 DG |
2978 | process_template_parm (tree list, tree parm, bool is_non_type, |
2979 | bool is_parameter_pack) | |
8d08fdba | 2980 | { |
8d08fdba | 2981 | tree decl = 0; |
a292b002 | 2982 | tree defval; |
0f67a82f | 2983 | tree err_parm_list; |
d47e3adf | 2984 | int idx = 0; |
f84b4be9 | 2985 | |
50bc768d | 2986 | gcc_assert (TREE_CODE (parm) == TREE_LIST); |
a292b002 | 2987 | defval = TREE_PURPOSE (parm); |
5566b478 MS |
2988 | |
2989 | if (list) | |
2990 | { | |
d47e3adf LM |
2991 | tree p = tree_last (list); |
2992 | ||
0f67a82f | 2993 | if (p && TREE_VALUE (p) != error_mark_node) |
d47e3adf LM |
2994 | { |
2995 | p = TREE_VALUE (p); | |
2996 | if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL) | |
2997 | idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p)); | |
2998 | else | |
2999 | idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p)); | |
3000 | } | |
5566b478 | 3001 | |
5566b478 MS |
3002 | ++idx; |
3003 | } | |
3004 | else | |
3005 | idx = 0; | |
3006 | ||
058b15c1 | 3007 | if (is_non_type) |
8d08fdba | 3008 | { |
058b15c1 MM |
3009 | parm = TREE_VALUE (parm); |
3010 | ||
833aa4c4 | 3011 | SET_DECL_TEMPLATE_PARM_P (parm); |
d490621d | 3012 | |
620188c9 | 3013 | if (TREE_TYPE (parm) == error_mark_node) |
0f67a82f LM |
3014 | { |
3015 | err_parm_list = build_tree_list (defval, parm); | |
3016 | TREE_VALUE (err_parm_list) = error_mark_node; | |
3017 | return chainon (list, err_parm_list); | |
3018 | } | |
620188c9 VR |
3019 | else |
3020 | { | |
3021 | /* [temp.param] | |
d490621d | 3022 | |
620188c9 VR |
3023 | The top-level cv-qualifiers on the template-parameter are |
3024 | ignored when determining its type. */ | |
3025 | TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm)); | |
3026 | if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1)) | |
0f67a82f LM |
3027 | { |
3028 | err_parm_list = build_tree_list (defval, parm); | |
3029 | TREE_VALUE (err_parm_list) = error_mark_node; | |
3030 | return chainon (list, err_parm_list); | |
3031 | } | |
5d80a306 DG |
3032 | |
3033 | if (uses_parameter_packs (TREE_TYPE (parm)) && !is_parameter_pack) | |
3034 | { | |
3035 | /* This template parameter is not a parameter pack, but it | |
3036 | should be. Complain about "bare" parameter packs. */ | |
1ad8aeeb | 3037 | check_for_bare_parameter_packs (&TREE_TYPE (parm)); |
5d80a306 DG |
3038 | |
3039 | /* Recover by calling this a parameter pack. */ | |
3040 | is_parameter_pack = true; | |
3041 | } | |
620188c9 | 3042 | } |
d490621d | 3043 | |
8d08fdba | 3044 | /* A template parameter is not modifiable. */ |
6de9cd9a DN |
3045 | TREE_CONSTANT (parm) = 1; |
3046 | TREE_INVARIANT (parm) = 1; | |
3047 | TREE_READONLY (parm) = 1; | |
8d08fdba | 3048 | decl = build_decl (CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm)); |
6de9cd9a DN |
3049 | TREE_CONSTANT (decl) = 1; |
3050 | TREE_INVARIANT (decl) = 1; | |
3051 | TREE_READONLY (decl) = 1; | |
c8094d83 | 3052 | DECL_INITIAL (parm) = DECL_INITIAL (decl) |
f84b4be9 JM |
3053 | = build_template_parm_index (idx, processing_template_decl, |
3054 | processing_template_decl, | |
3055 | decl, TREE_TYPE (parm)); | |
5d80a306 DG |
3056 | |
3057 | TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)) | |
3058 | = is_parameter_pack; | |
8d08fdba MS |
3059 | } |
3060 | else | |
3061 | { | |
73b0fce8 | 3062 | tree t; |
058b15c1 | 3063 | parm = TREE_VALUE (TREE_VALUE (parm)); |
c8094d83 | 3064 | |
73b0fce8 KL |
3065 | if (parm && TREE_CODE (parm) == TEMPLATE_DECL) |
3066 | { | |
33848bb0 | 3067 | t = make_aggr_type (TEMPLATE_TEMPLATE_PARM); |
c8094d83 | 3068 | /* This is for distinguishing between real templates and template |
73b0fce8 KL |
3069 | template parameters */ |
3070 | TREE_TYPE (parm) = t; | |
3071 | TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t; | |
3072 | decl = parm; | |
3073 | } | |
3074 | else | |
3075 | { | |
33848bb0 | 3076 | t = make_aggr_type (TEMPLATE_TYPE_PARM); |
f4f206f4 | 3077 | /* parm is either IDENTIFIER_NODE or NULL_TREE. */ |
73b0fce8 KL |
3078 | decl = build_decl (TYPE_DECL, parm, t); |
3079 | } | |
c8094d83 | 3080 | |
d2e5ee5c MS |
3081 | TYPE_NAME (t) = decl; |
3082 | TYPE_STUB_DECL (t) = decl; | |
a292b002 | 3083 | parm = decl; |
f84b4be9 | 3084 | TEMPLATE_TYPE_PARM_INDEX (t) |
c8094d83 | 3085 | = build_template_parm_index (idx, processing_template_decl, |
f84b4be9 JM |
3086 | processing_template_decl, |
3087 | decl, TREE_TYPE (parm)); | |
5d80a306 | 3088 | TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack; |
06d40de8 | 3089 | TYPE_CANONICAL (t) = canonical_type_parameter (t); |
8d08fdba | 3090 | } |
c727aa5e | 3091 | DECL_ARTIFICIAL (decl) = 1; |
cd9f6678 | 3092 | SET_DECL_TEMPLATE_PARM_P (decl); |
8d08fdba | 3093 | pushdecl (decl); |
a292b002 | 3094 | parm = build_tree_list (defval, parm); |
8d08fdba MS |
3095 | return chainon (list, parm); |
3096 | } | |
3097 | ||
3098 | /* The end of a template parameter list has been reached. Process the | |
3099 | tree list into a parameter vector, converting each parameter into a more | |
3100 | useful form. Type parameters are saved as IDENTIFIER_NODEs, and others | |
3101 | as PARM_DECLs. */ | |
3102 | ||
3103 | tree | |
3a978d72 | 3104 | end_template_parm_list (tree parms) |
8d08fdba | 3105 | { |
5566b478 | 3106 | int nparms; |
9471d3e2 | 3107 | tree parm, next; |
5566b478 MS |
3108 | tree saved_parmlist = make_tree_vec (list_length (parms)); |
3109 | ||
5566b478 | 3110 | current_template_parms |
4890c2f4 | 3111 | = tree_cons (size_int (processing_template_decl), |
5566b478 | 3112 | saved_parmlist, current_template_parms); |
8d08fdba | 3113 | |
9471d3e2 NS |
3114 | for (parm = parms, nparms = 0; parm; parm = next, nparms++) |
3115 | { | |
3116 | next = TREE_CHAIN (parm); | |
3117 | TREE_VEC_ELT (saved_parmlist, nparms) = parm; | |
3118 | TREE_CHAIN (parm) = NULL_TREE; | |
3119 | } | |
a292b002 | 3120 | |
67ffc812 MM |
3121 | --processing_template_parmlist; |
3122 | ||
8d08fdba MS |
3123 | return saved_parmlist; |
3124 | } | |
3125 | ||
5566b478 MS |
3126 | /* end_template_decl is called after a template declaration is seen. */ |
3127 | ||
8d08fdba | 3128 | void |
3a978d72 | 3129 | end_template_decl (void) |
8d08fdba | 3130 | { |
386b8a85 JM |
3131 | reset_specialization (); |
3132 | ||
5156628f | 3133 | if (! processing_template_decl) |
73aad9b9 JM |
3134 | return; |
3135 | ||
5566b478 | 3136 | /* This matches the pushlevel in begin_template_parm_list. */ |
74b846e0 | 3137 | finish_scope (); |
8d08fdba | 3138 | |
5566b478 MS |
3139 | --processing_template_decl; |
3140 | current_template_parms = TREE_CHAIN (current_template_parms); | |
5566b478 | 3141 | } |
8d08fdba | 3142 | |
03c17ccd MM |
3143 | /* Within the declaration of a template, return all levels of template |
3144 | parameters that apply. The template parameters are represented as | |
3145 | a TREE_VEC, in the form documented in cp-tree.h for template | |
3146 | arguments. */ | |
9a3b49ac | 3147 | |
b5791fdc | 3148 | static tree |
3a978d72 | 3149 | current_template_args (void) |
5566b478 | 3150 | { |
36a117a5 | 3151 | tree header; |
b370501f | 3152 | tree args = NULL_TREE; |
36a117a5 | 3153 | int length = TMPL_PARMS_DEPTH (current_template_parms); |
98c1c668 JM |
3154 | int l = length; |
3155 | ||
36a117a5 MM |
3156 | /* If there is only one level of template parameters, we do not |
3157 | create a TREE_VEC of TREE_VECs. Instead, we return a single | |
3158 | TREE_VEC containing the arguments. */ | |
3159 | if (length > 1) | |
3160 | args = make_tree_vec (length); | |
3161 | ||
3162 | for (header = current_template_parms; header; header = TREE_CHAIN (header)) | |
8d08fdba | 3163 | { |
5566b478 | 3164 | tree a = copy_node (TREE_VALUE (header)); |
36a117a5 MM |
3165 | int i; |
3166 | ||
5566b478 | 3167 | TREE_TYPE (a) = NULL_TREE; |
36a117a5 | 3168 | for (i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i) |
5566b478 | 3169 | { |
98c1c668 JM |
3170 | tree t = TREE_VEC_ELT (a, i); |
3171 | ||
36a117a5 | 3172 | /* T will be a list if we are called from within a |
98c1c668 JM |
3173 | begin/end_template_parm_list pair, but a vector directly |
3174 | if within a begin/end_member_template_processing pair. */ | |
c8094d83 | 3175 | if (TREE_CODE (t) == TREE_LIST) |
98c1c668 JM |
3176 | { |
3177 | t = TREE_VALUE (t); | |
c8094d83 | 3178 | |
0f67a82f LM |
3179 | if (t != error_mark_node) |
3180 | { | |
3181 | if (TREE_CODE (t) == TYPE_DECL | |
3182 | || TREE_CODE (t) == TEMPLATE_DECL) | |
5d80a306 DG |
3183 | { |
3184 | t = TREE_TYPE (t); | |
3185 | ||
3186 | if (TEMPLATE_TYPE_PARAMETER_PACK (t)) | |
3187 | { | |
3188 | /* Turn this argument into a TYPE_ARGUMENT_PACK | |
3189 | with a single element, which expands T. */ | |
3190 | tree vec = make_tree_vec (1); | |
3191 | TREE_VEC_ELT (vec, 0) = make_pack_expansion (t); | |
3192 | ||
3193 | t = make_node (TYPE_ARGUMENT_PACK); | |
3194 | SET_ARGUMENT_PACK_ARGS (t, vec); | |
3195 | } | |
3196 | } | |
3197 | else | |
3198 | { | |
3199 | t = DECL_INITIAL (t); | |
3200 | ||
3201 | if (TEMPLATE_PARM_PARAMETER_PACK (t)) | |
3202 | { | |
3203 | /* Turn this argument into a NONTYPE_ARGUMENT_PACK | |
3204 | with a single element, which expands T. */ | |
3205 | tree vec = make_tree_vec (1); | |
3206 | tree type = TREE_TYPE (TEMPLATE_PARM_DECL (t)); | |
3207 | TREE_VEC_ELT (vec, 0) = make_pack_expansion (t); | |
3208 | ||
3209 | t = make_node (NONTYPE_ARGUMENT_PACK); | |
3210 | SET_ARGUMENT_PACK_ARGS (t, vec); | |
3211 | TREE_TYPE (t) = type; | |
3212 | } | |
3213 | } | |
bf98d3b6 | 3214 | TREE_VEC_ELT (a, i) = t; |
5d80a306 | 3215 | } |
98c1c668 | 3216 | } |
5566b478 | 3217 | } |
36a117a5 MM |
3218 | |
3219 | if (length > 1) | |
3220 | TREE_VEC_ELT (args, --l) = a; | |
3221 | else | |
3222 | args = a; | |
8d08fdba MS |
3223 | } |
3224 | ||
9a3b49ac MS |
3225 | return args; |
3226 | } | |
75650646 | 3227 | |
e1467ff2 | 3228 | /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated |
c7222c02 | 3229 | template PARMS. If MEMBER_TEMPLATE_P is true, the new template is |
ee81147e | 3230 | a member template. Used by push_template_decl below. */ |
e1467ff2 | 3231 | |
75650646 | 3232 | static tree |
c7222c02 | 3233 | build_template_decl (tree decl, tree parms, bool member_template_p) |
75650646 MM |
3234 | { |
3235 | tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE); | |
3236 | DECL_TEMPLATE_PARMS (tmpl) = parms; | |
3237 | DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl); | |
c7222c02 | 3238 | DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p; |
75650646 MM |
3239 | if (DECL_LANG_SPECIFIC (decl)) |
3240 | { | |
8f032717 MM |
3241 | DECL_STATIC_FUNCTION_P (tmpl) = DECL_STATIC_FUNCTION_P (decl); |
3242 | DECL_CONSTRUCTOR_P (tmpl) = DECL_CONSTRUCTOR_P (decl); | |
c8460010 | 3243 | DECL_DESTRUCTOR_P (tmpl) = DECL_DESTRUCTOR_P (decl); |
11f98788 | 3244 | DECL_NONCONVERTING_P (tmpl) = DECL_NONCONVERTING_P (decl); |
1f6e1acc AS |
3245 | DECL_ASSIGNMENT_OPERATOR_P (tmpl) = DECL_ASSIGNMENT_OPERATOR_P (decl); |
3246 | if (DECL_OVERLOADED_OPERATOR_P (decl)) | |
c8094d83 | 3247 | SET_OVERLOADED_OPERATOR_CODE (tmpl, |
1f6e1acc | 3248 | DECL_OVERLOADED_OPERATOR_P (decl)); |
75650646 MM |
3249 | } |
3250 | ||
3251 | return tmpl; | |
3252 | } | |
3253 | ||
050367a3 MM |
3254 | struct template_parm_data |
3255 | { | |
6c30752f MM |
3256 | /* The level of the template parameters we are currently |
3257 | processing. */ | |
050367a3 | 3258 | int level; |
6c30752f MM |
3259 | |
3260 | /* The index of the specialization argument we are currently | |
3261 | processing. */ | |
3262 | int current_arg; | |
3263 | ||
3264 | /* An array whose size is the number of template parameters. The | |
838dfd8a | 3265 | elements are nonzero if the parameter has been used in any one |
6c30752f | 3266 | of the arguments processed so far. */ |
050367a3 | 3267 | int* parms; |
6c30752f MM |
3268 | |
3269 | /* An array whose size is the number of template arguments. The | |
838dfd8a | 3270 | elements are nonzero if the argument makes use of template |
6c30752f MM |
3271 | parameters of this level. */ |
3272 | int* arg_uses_template_parms; | |
050367a3 MM |
3273 | }; |
3274 | ||
3275 | /* Subroutine of push_template_decl used to see if each template | |
3276 | parameter in a partial specialization is used in the explicit | |
3277 | argument list. If T is of the LEVEL given in DATA (which is | |
3278 | treated as a template_parm_data*), then DATA->PARMS is marked | |
3279 | appropriately. */ | |
3280 | ||
3281 | static int | |
3a978d72 | 3282 | mark_template_parm (tree t, void* data) |
050367a3 MM |
3283 | { |
3284 | int level; | |
3285 | int idx; | |
3286 | struct template_parm_data* tpd = (struct template_parm_data*) data; | |
3287 | ||
3288 | if (TREE_CODE (t) == TEMPLATE_PARM_INDEX) | |
3289 | { | |
3290 | level = TEMPLATE_PARM_LEVEL (t); | |
3291 | idx = TEMPLATE_PARM_IDX (t); | |
3292 | } | |
3293 | else | |
3294 | { | |
3295 | level = TEMPLATE_TYPE_LEVEL (t); | |
3296 | idx = TEMPLATE_TYPE_IDX (t); | |
3297 | } | |
3298 | ||
3299 | if (level == tpd->level) | |
6c30752f MM |
3300 | { |
3301 | tpd->parms[idx] = 1; | |
3302 | tpd->arg_uses_template_parms[tpd->current_arg] = 1; | |
3303 | } | |
050367a3 MM |
3304 | |
3305 | /* Return zero so that for_each_template_parm will continue the | |
3306 | traversal of the tree; we want to mark *every* template parm. */ | |
3307 | return 0; | |
3308 | } | |
3309 | ||
6c30752f MM |
3310 | /* Process the partial specialization DECL. */ |
3311 | ||
e9659ab0 | 3312 | static tree |
3a978d72 | 3313 | process_partial_specialization (tree decl) |
6c30752f MM |
3314 | { |
3315 | tree type = TREE_TYPE (decl); | |
3316 | tree maintmpl = CLASSTYPE_TI_TEMPLATE (type); | |
3317 | tree specargs = CLASSTYPE_TI_ARGS (type); | |
f9a7ae04 | 3318 | tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs); |
6c30752f MM |
3319 | tree inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms); |
3320 | tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl); | |
3321 | int nargs = TREE_VEC_LENGTH (inner_args); | |
3322 | int ntparms = TREE_VEC_LENGTH (inner_parms); | |
3323 | int i; | |
3324 | int did_error_intro = 0; | |
6c30752f MM |
3325 | struct template_parm_data tpd; |
3326 | struct template_parm_data tpd2; | |
3327 | ||
6c30752f MM |
3328 | /* We check that each of the template parameters given in the |
3329 | partial specialization is used in the argument list to the | |
3330 | specialization. For example: | |
3331 | ||
3332 | template <class T> struct S; | |
3333 | template <class T> struct S<T*>; | |
3334 | ||
3335 | The second declaration is OK because `T*' uses the template | |
3336 | parameter T, whereas | |
3337 | ||
3338 | template <class T> struct S<int>; | |
3339 | ||
3340 | is no good. Even trickier is: | |
3341 | ||
3342 | template <class T> | |
3343 | struct S1 | |
3344 | { | |
3345 | template <class U> | |
3346 | struct S2; | |
3347 | template <class U> | |
3348 | struct S2<T>; | |
3349 | }; | |
3350 | ||
0e339752 | 3351 | The S2<T> declaration is actually invalid; it is a |
c8094d83 | 3352 | full-specialization. Of course, |
6c30752f MM |
3353 | |
3354 | template <class U> | |
3355 | struct S2<T (*)(U)>; | |
3356 | ||
3357 | or some such would have been OK. */ | |
3358 | tpd.level = TMPL_PARMS_DEPTH (current_template_parms); | |
67f5655f | 3359 | tpd.parms = (int *) alloca (sizeof (int) * ntparms); |
fad205ff | 3360 | memset (tpd.parms, 0, sizeof (int) * ntparms); |
6c30752f | 3361 | |
67f5655f | 3362 | tpd.arg_uses_template_parms = (int *) alloca (sizeof (int) * nargs); |
fad205ff | 3363 | memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs); |
6c30752f MM |
3364 | for (i = 0; i < nargs; ++i) |
3365 | { | |
3366 | tpd.current_arg = i; | |
3367 | for_each_template_parm (TREE_VEC_ELT (inner_args, i), | |
3368 | &mark_template_parm, | |
4f2c9d7e MM |
3369 | &tpd, |
3370 | NULL); | |
6c30752f MM |
3371 | } |
3372 | for (i = 0; i < ntparms; ++i) | |
3373 | if (tpd.parms[i] == 0) | |
3374 | { | |
3375 | /* One of the template parms was not used in the | |
f9a7ae04 | 3376 | specialization. */ |
6c30752f MM |
3377 | if (!did_error_intro) |
3378 | { | |
33bd39a2 | 3379 | error ("template parameters not used in partial specialization:"); |
6c30752f MM |
3380 | did_error_intro = 1; |
3381 | } | |
3382 | ||
0f51ccfc | 3383 | error (" %qD", TREE_VALUE (TREE_VEC_ELT (inner_parms, i))); |
6c30752f MM |
3384 | } |
3385 | ||
3386 | /* [temp.class.spec] | |
3387 | ||
3388 | The argument list of the specialization shall not be identical to | |
3389 | the implicit argument list of the primary template. */ | |
c8094d83 MS |
3390 | if (comp_template_args |
3391 | (inner_args, | |
f9a7ae04 MM |
3392 | INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE |
3393 | (maintmpl))))) | |
0f51ccfc | 3394 | error ("partial specialization %qT does not specialize any template arguments", type); |
6c30752f MM |
3395 | |
3396 | /* [temp.class.spec] | |
3397 | ||
3398 | A partially specialized non-type argument expression shall not | |
3399 | involve template parameters of the partial specialization except | |
3400 | when the argument expression is a simple identifier. | |
3401 | ||
3402 | The type of a template parameter corresponding to a specialized | |
3403 | non-type argument shall not be dependent on a parameter of the | |
5d80a306 DG |
3404 | specialization. |
3405 | ||
3406 | Also, we verify that pack expansions only occur at the | |
3407 | end of the argument list. */ | |
50bc768d | 3408 | gcc_assert (nargs == DECL_NTPARMS (maintmpl)); |
6c30752f MM |
3409 | tpd2.parms = 0; |
3410 | for (i = 0; i < nargs; ++i) | |
3411 | { | |
5d80a306 | 3412 | tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i)); |
6c30752f | 3413 | tree arg = TREE_VEC_ELT (inner_args, i); |
5d80a306 DG |
3414 | tree packed_args = NULL_TREE; |
3415 | int j, len = 1; | |
6c30752f | 3416 | |
5d80a306 DG |
3417 | if (ARGUMENT_PACK_P (arg)) |
3418 | { | |
3419 | /* Extract the arguments from the argument pack. We'll be | |
3420 | iterating over these in the following loop. */ | |
3421 | packed_args = ARGUMENT_PACK_ARGS (arg); | |
3422 | len = TREE_VEC_LENGTH (packed_args); | |
3423 | } | |
3424 | ||
3425 | for (j = 0; j < len; j++) | |
3426 | { | |
3427 | if (packed_args) | |
3428 | /* Get the Jth argument in the parameter pack. */ | |
3429 | arg = TREE_VEC_ELT (packed_args, j); | |
3430 | ||
3431 | if (PACK_EXPANSION_P (arg)) | |
3432 | { | |
3433 | /* Pack expansions must come at the end of the | |
3434 | argument list. */ | |
3435 | if ((packed_args && j < len - 1) | |
3436 | || (!packed_args && i < nargs - 1)) | |
3437 | { | |
3438 | if (TREE_CODE (arg) == EXPR_PACK_EXPANSION) | |
3439 | error ("parameter pack argument %qE must be at the end of the template argument list", arg); | |
3440 | else | |
1ad8aeeb DG |
3441 | error ("parameter pack argument %qT must be at the end of the template argument list", arg); |
3442 | ||
3443 | if (packed_args) | |
3444 | TREE_VEC_ELT (packed_args, j) = error_mark_node; | |
5d80a306 DG |
3445 | } |
3446 | } | |
3447 | ||
3448 | if (TREE_CODE (arg) == EXPR_PACK_EXPANSION) | |
3449 | /* We only care about the pattern. */ | |
3450 | arg = PACK_EXPANSION_PATTERN (arg); | |
3451 | ||
3452 | if (/* These first two lines are the `non-type' bit. */ | |
3453 | !TYPE_P (arg) | |
3454 | && TREE_CODE (arg) != TEMPLATE_DECL | |
3455 | /* This next line is the `argument expression is not just a | |
3456 | simple identifier' condition and also the `specialized | |
3457 | non-type argument' bit. */ | |
3458 | && TREE_CODE (arg) != TEMPLATE_PARM_INDEX) | |
3459 | { | |
3460 | if ((!packed_args && tpd.arg_uses_template_parms[i]) | |
3461 | || (packed_args && uses_template_parms (arg))) | |
3462 | error ("template argument %qE involves template parameter(s)", | |
3463 | arg); | |
3464 | else | |
3465 | { | |
3466 | /* Look at the corresponding template parameter, | |
3467 | marking which template parameters its type depends | |
3468 | upon. */ | |
3469 | tree type = TREE_TYPE (parm); | |
3470 | ||
3471 | if (!tpd2.parms) | |
3472 | { | |
3473 | /* We haven't yet initialized TPD2. Do so now. */ | |
3474 | tpd2.arg_uses_template_parms | |
3475 | = (int *) alloca (sizeof (int) * nargs); | |
3476 | /* The number of parameters here is the number in the | |
3477 | main template, which, as checked in the assertion | |
3478 | above, is NARGS. */ | |
3479 | tpd2.parms = (int *) alloca (sizeof (int) * nargs); | |
3480 | tpd2.level = | |
3481 | TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl)); | |
3482 | } | |
3483 | ||
3484 | /* Mark the template parameters. But this time, we're | |
3485 | looking for the template parameters of the main | |
3486 | template, not in the specialization. */ | |
3487 | tpd2.current_arg = i; | |
3488 | tpd2.arg_uses_template_parms[i] = 0; | |
3489 | memset (tpd2.parms, 0, sizeof (int) * nargs); | |
3490 | for_each_template_parm (type, | |
3491 | &mark_template_parm, | |
3492 | &tpd2, | |
3493 | NULL); | |
3494 | ||
3495 | if (tpd2.arg_uses_template_parms [i]) | |
3496 | { | |
3497 | /* The type depended on some template parameters. | |
3498 | If they are fully specialized in the | |
3499 | specialization, that's OK. */ | |
3500 | int j; | |
3501 | for (j = 0; j < nargs; ++j) | |
3502 | if (tpd2.parms[j] != 0 | |
3503 | && tpd.arg_uses_template_parms [j]) | |
3504 | { | |
3505 | error ("type %qT of template argument %qE depends " | |
3506 | "on template parameter(s)", | |
3507 | type, | |
3508 | arg); | |
3509 | break; | |
3510 | } | |
3511 | } | |
3512 | } | |
3513 | } | |
3514 | } | |
6c30752f MM |
3515 | } |
3516 | ||
c8094d83 | 3517 | if (retrieve_specialization (maintmpl, specargs, |
c7222c02 | 3518 | /*class_specializations_p=*/true)) |
6c30752f MM |
3519 | /* We've already got this specialization. */ |
3520 | return decl; | |
3521 | ||
d8b64f80 | 3522 | DECL_TEMPLATE_SPECIALIZATIONS (maintmpl) |
916b63c3 | 3523 | = tree_cons (specargs, inner_parms, |
b1d7b1c0 | 3524 | DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)); |
6c30752f MM |
3525 | TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type; |
3526 | return decl; | |
3527 | } | |
3528 | ||
6ba89f8e MM |
3529 | /* Check that a template declaration's use of default arguments is not |
3530 | invalid. Here, PARMS are the template parameters. IS_PRIMARY is | |
838dfd8a | 3531 | nonzero if DECL is the thing declared by a primary template. |
9b7dd5e8 DG |
3532 | IS_PARTIAL is nonzero if DECL is a partial specialization. |
3533 | ||
6ba89f8e | 3534 | |
9b7dd5e8 DG |
3535 | IS_FRIEND_DECL is nonzero if DECL is a friend function template |
3536 | declaration (but not a definition); 1 indicates a declaration, 2 | |
3537 | indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are | |
3538 | emitted for extraneous default arguments. | |
3539 | ||
3540 | Returns TRUE if there were no errors found, FALSE otherwise. */ | |
3541 | ||
3542 | bool | |
3543 | check_default_tmpl_args (tree decl, tree parms, int is_primary, | |
3544 | int is_partial, int is_friend_decl) | |
6ba89f8e | 3545 | { |
d8e178a0 | 3546 | const char *msg; |
66191c20 MM |
3547 | int last_level_to_check; |
3548 | tree parm_level; | |
9b7dd5e8 | 3549 | bool no_errors = true; |
6ba89f8e | 3550 | |
c8094d83 | 3551 | /* [temp.param] |
6ba89f8e MM |
3552 | |
3553 | A default template-argument shall not be specified in a | |
3554 | function template declaration or a function template definition, nor | |
3555 | in the template-parameter-list of the definition of a member of a | |
3556 | class template. */ | |
3557 | ||
4f1c5b7d | 3558 | if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL) |
e0942dcd MM |
3559 | /* You can't have a function template declaration in a local |
3560 | scope, nor you can you define a member of a class template in a | |
3561 | local scope. */ | |
9b7dd5e8 | 3562 | return true; |
e0942dcd | 3563 | |
6ba89f8e MM |
3564 | if (current_class_type |
3565 | && !TYPE_BEING_DEFINED (current_class_type) | |
3d7de1fa | 3566 | && DECL_LANG_SPECIFIC (decl) |
5937a6f9 MM |
3567 | /* If this is either a friend defined in the scope of the class |
3568 | or a member function. */ | |
6df5158a NS |
3569 | && (DECL_FUNCTION_MEMBER_P (decl) |
3570 | ? same_type_p (DECL_CONTEXT (decl), current_class_type) | |
3571 | : DECL_FRIEND_CONTEXT (decl) | |
3572 | ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type) | |
3573 | : false) | |
5937a6f9 MM |
3574 | /* And, if it was a member function, it really was defined in |
3575 | the scope of the class. */ | |
6df5158a NS |
3576 | && (!DECL_FUNCTION_MEMBER_P (decl) |
3577 | || DECL_INITIALIZED_IN_CLASS_P (decl))) | |
6ba89f8e | 3578 | /* We already checked these parameters when the template was |
5937a6f9 MM |
3579 | declared, so there's no need to do it again now. This function |
3580 | was defined in class scope, but we're processing it's body now | |
3581 | that the class is complete. */ | |
9b7dd5e8 | 3582 | return true; |
c8094d83 | 3583 | |
9b7dd5e8 DG |
3584 | /* Core issue 226 (C++0x only): the following only applies to class |
3585 | templates. */ | |
c1ae8be5 | 3586 | if ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL) |
66191c20 | 3587 | { |
9b7dd5e8 | 3588 | /* [temp.param] |
66191c20 | 3589 | |
9b7dd5e8 DG |
3590 | If a template-parameter has a default template-argument, all |
3591 | subsequent template-parameters shall have a default | |
3592 | template-argument supplied. */ | |
3593 | for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level)) | |
3594 | { | |
3595 | tree inner_parms = TREE_VALUE (parm_level); | |
3596 | int ntparms = TREE_VEC_LENGTH (inner_parms); | |
3597 | int seen_def_arg_p = 0; | |
3598 | int i; | |
42b304f1 | 3599 | |
9b7dd5e8 DG |
3600 | for (i = 0; i < ntparms; ++i) |
3601 | { | |
3602 | tree parm = TREE_VEC_ELT (inner_parms, i); | |
42b304f1 | 3603 | |
9b7dd5e8 DG |
3604 | if (parm == error_mark_node) |
3605 | continue; | |
3606 | ||
3607 | if (TREE_PURPOSE (parm)) | |
3608 | seen_def_arg_p = 1; | |
3609 | else if (seen_def_arg_p) | |
3610 | { | |
3611 | error ("no default argument for %qD", TREE_VALUE (parm)); | |
3612 | /* For better subsequent error-recovery, we indicate that | |
3613 | there should have been a default argument. */ | |
3614 | TREE_PURPOSE (parm) = error_mark_node; | |
3615 | no_errors = false; | |
3616 | } | |
3617 | } | |
3618 | } | |
66191c20 MM |
3619 | } |
3620 | ||
c1ae8be5 | 3621 | if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL) |
9b7dd5e8 DG |
3622 | || is_partial |
3623 | || !is_primary | |
3624 | || is_friend_decl) | |
6ba89f8e MM |
3625 | /* For an ordinary class template, default template arguments are |
3626 | allowed at the innermost level, e.g.: | |
0cbd7506 | 3627 | template <class T = int> |
6ba89f8e MM |
3628 | struct S {}; |
3629 | but, in a partial specialization, they're not allowed even | |
3630 | there, as we have in [temp.class.spec]: | |
c8094d83 | 3631 | |
6ba89f8e | 3632 | The template parameter list of a specialization shall not |
c8094d83 | 3633 | contain default template argument values. |
6ba89f8e | 3634 | |
9b7dd5e8 DG |
3635 | So, for a partial specialization, or for a function template |
3636 | (in C++98/C++03), we look at all of them. */ | |
6ba89f8e MM |
3637 | ; |
3638 | else | |
3639 | /* But, for a primary class template that is not a partial | |
3640 | specialization we look at all template parameters except the | |
3641 | innermost ones. */ | |
3642 | parms = TREE_CHAIN (parms); | |
3643 | ||
3644 | /* Figure out what error message to issue. */ | |
9b7dd5e8 DG |
3645 | if (is_friend_decl == 2) |
3646 | msg = "default template arguments may not be used in function template friend re-declaration"; | |
3647 | else if (is_friend_decl) | |
3648 | msg = "default template arguments may not be used in function template friend declarations"; | |
c1ae8be5 | 3649 | else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98)) |
8653a2c3 | 3650 | msg = "default template arguments may not be used in function templates"; |
6ba89f8e | 3651 | else if (is_partial) |
8653a2c3 | 3652 | msg = "default template arguments may not be used in partial specializations"; |
6ba89f8e | 3653 | else |
0f51ccfc | 3654 | msg = "default argument for template parameter for class enclosing %qD"; |
6ba89f8e MM |
3655 | |
3656 | if (current_class_type && TYPE_BEING_DEFINED (current_class_type)) | |
3657 | /* If we're inside a class definition, there's no need to | |
104bf76a | 3658 | examine the parameters to the class itself. On the one |
6ba89f8e | 3659 | hand, they will be checked when the class is defined, and, |
0e339752 | 3660 | on the other, default arguments are valid in things like: |
0cbd7506 MS |
3661 | template <class T = double> |
3662 | struct S { template <class U> void f(U); }; | |
6ba89f8e MM |
3663 | Here the default argument for `S' has no bearing on the |
3664 | declaration of `f'. */ | |
3665 | last_level_to_check = template_class_depth (current_class_type) + 1; | |
3666 | else | |
3667 | /* Check everything. */ | |
3668 | last_level_to_check = 0; | |
3669 | ||
c8094d83 MS |
3670 | for (parm_level = parms; |
3671 | parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check; | |
66191c20 | 3672 | parm_level = TREE_CHAIN (parm_level)) |
6ba89f8e | 3673 | { |
66191c20 MM |
3674 | tree inner_parms = TREE_VALUE (parm_level); |
3675 | int i; | |
3676 | int ntparms; | |
6ba89f8e MM |
3677 | |
3678 | ntparms = TREE_VEC_LENGTH (inner_parms); | |
c8094d83 | 3679 | for (i = 0; i < ntparms; ++i) |
42b304f1 LM |
3680 | { |
3681 | if (TREE_VEC_ELT (inner_parms, i) == error_mark_node) | |
3682 | continue; | |
6ba89f8e | 3683 | |
42b304f1 LM |
3684 | if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i))) |
3685 | { | |
3686 | if (msg) | |
3687 | { | |
9b7dd5e8 DG |
3688 | no_errors = false; |
3689 | if (is_friend_decl == 2) | |
3690 | return no_errors; | |
3691 | ||
42b304f1 LM |
3692 | error (msg, decl); |
3693 | msg = 0; | |
3694 | } | |
3695 | ||
3696 | /* Clear out the default argument so that we are not | |
3697 | confused later. */ | |
3698 | TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE; | |
3699 | } | |
3700 | } | |
6ba89f8e MM |
3701 | |
3702 | /* At this point, if we're still interested in issuing messages, | |
3703 | they must apply to classes surrounding the object declared. */ | |
3704 | if (msg) | |
c8094d83 | 3705 | msg = "default argument for template parameter for class enclosing %qD"; |
6ba89f8e | 3706 | } |
9b7dd5e8 DG |
3707 | |
3708 | return no_errors; | |
6ba89f8e MM |
3709 | } |
3710 | ||
5dd236e2 NS |
3711 | /* Worker for push_template_decl_real, called via |
3712 | for_each_template_parm. DATA is really an int, indicating the | |
3713 | level of the parameters we are interested in. If T is a template | |
838dfd8a | 3714 | parameter of that level, return nonzero. */ |
5dd236e2 NS |
3715 | |
3716 | static int | |
3a978d72 | 3717 | template_parm_this_level_p (tree t, void* data) |
5dd236e2 | 3718 | { |
6e04241f | 3719 | int this_level = *(int *)data; |
5dd236e2 NS |
3720 | int level; |
3721 | ||
3722 | if (TREE_CODE (t) == TEMPLATE_PARM_INDEX) | |
3723 | level = TEMPLATE_PARM_LEVEL (t); | |
3724 | else | |
3725 | level = TEMPLATE_TYPE_LEVEL (t); | |
3726 | return level == this_level; | |
3727 | } | |
3728 | ||
3ac3d9ea | 3729 | /* Creates a TEMPLATE_DECL for the indicated DECL using the template |
f84b4be9 JM |
3730 | parameters given by current_template_args, or reuses a |
3731 | previously existing one, if appropriate. Returns the DECL, or an | |
c8094d83 | 3732 | equivalent one, if it is replaced via a call to duplicate_decls. |
6757edfe | 3733 | |
d63d5d0c | 3734 | If IS_FRIEND is true, DECL is a friend declaration. */ |
3ac3d9ea MM |
3735 | |
3736 | tree | |
d63d5d0c | 3737 | push_template_decl_real (tree decl, bool is_friend) |
9a3b49ac MS |
3738 | { |
3739 | tree tmpl; | |
f84b4be9 | 3740 | tree args; |
9a3b49ac | 3741 | tree info; |
f84b4be9 JM |
3742 | tree ctx; |
3743 | int primary; | |
6ba89f8e | 3744 | int is_partial; |
cfe507be | 3745 | int new_template_p = 0; |
c7222c02 MM |
3746 | /* True if the template is a member template, in the sense of |
3747 | [temp.mem]. */ | |
3748 | bool member_template_p = false; | |
6ba89f8e | 3749 | |
c0694c4b MM |
3750 | if (decl == error_mark_node) |
3751 | return decl; | |
3752 | ||
6ba89f8e | 3753 | /* See if this is a partial specialization. */ |
9188c363 | 3754 | is_partial = (DECL_IMPLICIT_TYPEDEF_P (decl) |
6ba89f8e | 3755 | && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE |
370af2d5 | 3756 | && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl))); |
6757edfe | 3757 | |
d63d5d0c ILT |
3758 | if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl)) |
3759 | is_friend = true; | |
f84b4be9 JM |
3760 | |
3761 | if (is_friend) | |
3762 | /* For a friend, we want the context of the friend function, not | |
3763 | the type of which it is a friend. */ | |
3764 | ctx = DECL_CONTEXT (decl); | |
4f1c5b7d MM |
3765 | else if (CP_DECL_CONTEXT (decl) |
3766 | && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL) | |
f84b4be9 JM |
3767 | /* In the case of a virtual function, we want the class in which |
3768 | it is defined. */ | |
4f1c5b7d | 3769 | ctx = CP_DECL_CONTEXT (decl); |
f84b4be9 | 3770 | else |
dc957d14 | 3771 | /* Otherwise, if we're currently defining some class, the DECL |
f84b4be9 | 3772 | is assumed to be a member of the class. */ |
9188c363 | 3773 | ctx = current_scope (); |
f84b4be9 | 3774 | |
2c73f9f5 ML |
3775 | if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL) |
3776 | ctx = NULL_TREE; | |
3777 | ||
3778 | if (!DECL_CONTEXT (decl)) | |
cb0dbb9a | 3779 | DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace); |
2c73f9f5 | 3780 | |
6ba89f8e | 3781 | /* See if this is a primary template. */ |
c9cbfca6 JM |
3782 | if (is_friend && ctx) |
3783 | /* A friend template that specifies a class context, i.e. | |
3784 | template <typename T> friend void A<T>::f(); | |
3785 | is not primary. */ | |
3786 | primary = 0; | |
3787 | else | |
3788 | primary = template_parm_scope_p (); | |
9a3b49ac | 3789 | |
83566abf JM |
3790 | if (primary) |
3791 | { | |
c7222c02 MM |
3792 | if (DECL_CLASS_SCOPE_P (decl)) |
3793 | member_template_p = true; | |
2f1b1731 MM |
3794 | if (TREE_CODE (decl) == TYPE_DECL |
3795 | && ANON_AGGRNAME_P (DECL_NAME (decl))) | |
33bd39a2 | 3796 | error ("template class without a name"); |
717a7d5d | 3797 | else if (TREE_CODE (decl) == FUNCTION_DECL) |
4b0d3cbe | 3798 | { |
717a7d5d MM |
3799 | if (DECL_DESTRUCTOR_P (decl)) |
3800 | { | |
3801 | /* [temp.mem] | |
c8094d83 | 3802 | |
0cbd7506 | 3803 | A destructor shall not be a member template. */ |
0f51ccfc | 3804 | error ("destructor %qD declared as member template", decl); |
717a7d5d MM |
3805 | return error_mark_node; |
3806 | } | |
3807 | if (NEW_DELETE_OPNAME_P (DECL_NAME (decl)) | |
3808 | && (!TYPE_ARG_TYPES (TREE_TYPE (decl)) | |
3809 | || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node | |
3810 | || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl))) | |
3811 | || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl)))) | |
3812 | == void_list_node))) | |
3813 | { | |
c8094d83 | 3814 | /* [basic.stc.dynamic.allocation] |
717a7d5d | 3815 | |
0cbd7506 | 3816 | An allocation function can be a function |
717a7d5d MM |
3817 | template. ... Template allocation functions shall |
3818 | have two or more parameters. */ | |
0f51ccfc | 3819 | error ("invalid template declaration of %qD", decl); |
7c60008e | 3820 | return error_mark_node; |
717a7d5d | 3821 | } |
4b0d3cbe | 3822 | } |
8ca4bf25 MM |
3823 | else if (DECL_IMPLICIT_TYPEDEF_P (decl) |
3824 | && CLASS_TYPE_P (TREE_TYPE (decl))) | |
2aaf816d JM |
3825 | /* OK */; |
3826 | else | |
11325dcd | 3827 | { |
0f51ccfc | 3828 | error ("template declaration of %q#D", decl); |
11325dcd KL |
3829 | return error_mark_node; |
3830 | } | |
83566abf JM |
3831 | } |
3832 | ||
6ba89f8e MM |
3833 | /* Check to see that the rules regarding the use of default |
3834 | arguments are not being violated. */ | |
c8094d83 | 3835 | check_default_tmpl_args (decl, current_template_parms, |
9b7dd5e8 | 3836 | primary, is_partial, /*is_friend_decl=*/0); |
73aad9b9 | 3837 | |
5d80a306 DG |
3838 | /* Ensure that there are no parameter packs in the type of this |
3839 | declaration that have not been expanded. */ | |
b1d7b1c0 DG |
3840 | if (TREE_CODE (decl) == FUNCTION_DECL) |
3841 | { | |
3842 | /* Check each of the arguments individually to see if there are | |
3843 | any bare parameter packs. */ | |
3844 | tree type = TREE_TYPE (decl); | |
3845 | tree arg = DECL_ARGUMENTS (decl); | |
3846 | tree argtype = TYPE_ARG_TYPES (type); | |
3847 | ||
3848 | while (arg && argtype) | |
3849 | { | |
3850 | if (!FUNCTION_PARAMETER_PACK_P (arg) | |
1ad8aeeb | 3851 | && !check_for_bare_parameter_packs (&TREE_TYPE (arg))) |
b1d7b1c0 DG |
3852 | { |
3853 | /* This is a PARM_DECL that contains unexpanded parameter | |
3854 | packs. We have already complained about this in the | |
3855 | check_for_bare_parameter_packs call, so just replace | |
3856 | these types with ERROR_MARK_NODE. */ | |
3857 | TREE_TYPE (arg) = error_mark_node; | |
3858 | TREE_VALUE (argtype) = error_mark_node; | |
3859 | } | |
3860 | ||
3861 | arg = TREE_CHAIN (arg); | |
3862 | argtype = TREE_CHAIN (argtype); | |
3863 | } | |
3864 | ||
3865 | /* Check for bare parameter packs in the return type and the | |
3866 | exception specifiers. */ | |
1ad8aeeb DG |
3867 | check_for_bare_parameter_packs (&TREE_TYPE (type)); |
3868 | check_for_bare_parameter_packs (&TYPE_RAISES_EXCEPTIONS (type)); | |
b1d7b1c0 DG |
3869 | } |
3870 | else | |
1ad8aeeb | 3871 | check_for_bare_parameter_packs (&TREE_TYPE (decl)); |
5d80a306 | 3872 | |
6ba89f8e MM |
3873 | if (is_partial) |
3874 | return process_partial_specialization (decl); | |
d32789d8 | 3875 | |
5d80a306 DG |
3876 | /* A primary class template can only have one parameter pack, at the |
3877 | end of the template parameter list. */ | |
3878 | if (primary && TREE_CODE (decl) == TYPE_DECL) | |
3879 | { | |
3880 | tree inner_parms | |
3881 | = INNERMOST_TEMPLATE_PARMS (current_template_parms); | |
3882 | int i, len = TREE_VEC_LENGTH (inner_parms); | |
3883 | for (i = 0; i < len - 1; i++) | |
3884 | { | |
3885 | tree parm = TREE_VALUE (TREE_VEC_ELT (inner_parms, i)); | |
3886 | ||
3887 | if (template_parameter_pack_p (parm)) | |
3888 | { | |
3889 | if (TREE_CODE (parm) == PARM_DECL) | |
3890 | error ("parameter pack %qE must be at the end of the" | |
3891 | " template parameter list", parm); | |
3892 | else | |
3893 | error ("parameter pack %qT must be at the end of the" | |
3894 | " template parameter list", TREE_TYPE (parm)); | |
1ad8aeeb DG |
3895 | |
3896 | TREE_VALUE (TREE_VEC_ELT (inner_parms, i)) = error_mark_node; | |
5d80a306 DG |
3897 | } |
3898 | } | |
3899 | } | |
3900 | ||
9a3b49ac MS |
3901 | args = current_template_args (); |
3902 | ||
c8094d83 | 3903 | if (!ctx |
f84b4be9 | 3904 | || TREE_CODE (ctx) == FUNCTION_DECL |
a723baf1 | 3905 | || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx)) |
f84b4be9 | 3906 | || (is_friend && !DECL_TEMPLATE_INFO (decl))) |
8d08fdba | 3907 | { |
75650646 | 3908 | if (DECL_LANG_SPECIFIC (decl) |
f84b4be9 JM |
3909 | && DECL_TEMPLATE_INFO (decl) |
3910 | && DECL_TI_TEMPLATE (decl)) | |
3911 | tmpl = DECL_TI_TEMPLATE (decl); | |
1c10870d AS |
3912 | /* If DECL is a TYPE_DECL for a class-template, then there won't |
3913 | be DECL_LANG_SPECIFIC. The information equivalent to | |
3914 | DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */ | |
c8094d83 | 3915 | else if (DECL_IMPLICIT_TYPEDEF_P (decl) |
1c10870d AS |
3916 | && TYPE_TEMPLATE_INFO (TREE_TYPE (decl)) |
3917 | && TYPE_TI_TEMPLATE (TREE_TYPE (decl))) | |
3918 | { | |
3919 | /* Since a template declaration already existed for this | |
3920 | class-type, we must be redeclaring it here. Make sure | |
0e339752 | 3921 | that the redeclaration is valid. */ |
1c10870d AS |
3922 | redeclare_class_template (TREE_TYPE (decl), |
3923 | current_template_parms); | |
3924 | /* We don't need to create a new TEMPLATE_DECL; just use the | |
3925 | one we already had. */ | |
3926 | tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl)); | |
3927 | } | |
f84b4be9 | 3928 | else |
786b5245 | 3929 | { |
c7222c02 MM |
3930 | tmpl = build_template_decl (decl, current_template_parms, |
3931 | member_template_p); | |
cfe507be MM |
3932 | new_template_p = 1; |
3933 | ||
f84b4be9 JM |
3934 | if (DECL_LANG_SPECIFIC (decl) |
3935 | && DECL_TEMPLATE_SPECIALIZATION (decl)) | |
3936 | { | |
3937 | /* A specialization of a member template of a template | |
c6002625 | 3938 | class. */ |
f84b4be9 JM |
3939 | SET_DECL_TEMPLATE_SPECIALIZATION (tmpl); |
3940 | DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl); | |
3941 | DECL_TEMPLATE_INFO (decl) = NULL_TREE; | |
3942 | } | |
786b5245 | 3943 | } |
8d08fdba MS |
3944 | } |
3945 | else | |
3946 | { | |
e1a5ccf7 | 3947 | tree a, t, current, parms; |
ba4f4e5d | 3948 | int i; |
91a77d68 | 3949 | tree tinfo = get_template_info (decl); |
6633d636 | 3950 | |
91a77d68 | 3951 | if (!tinfo) |
c91a56d2 | 3952 | { |
0f51ccfc | 3953 | error ("template definition of non-template %q#D", decl); |
3ac3d9ea | 3954 | return decl; |
c91a56d2 | 3955 | } |
91a77d68 JM |
3956 | |
3957 | tmpl = TI_TEMPLATE (tinfo); | |
c8094d83 | 3958 | |
c353b8e3 | 3959 | if (DECL_FUNCTION_TEMPLATE_P (tmpl) |
c8094d83 | 3960 | && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl) |
c353b8e3 | 3961 | && DECL_TEMPLATE_SPECIALIZATION (decl) |
c7222c02 | 3962 | && DECL_MEMBER_TEMPLATE_P (tmpl)) |
98c1c668 | 3963 | { |
e1a5ccf7 JM |
3964 | tree new_tmpl; |
3965 | ||
3966 | /* The declaration is a specialization of a member | |
3967 | template, declared outside the class. Therefore, the | |
3968 | innermost template arguments will be NULL, so we | |
3969 | replace them with the arguments determined by the | |
3970 | earlier call to check_explicit_specialization. */ | |
3971 | args = DECL_TI_ARGS (decl); | |
3972 | ||
c8094d83 | 3973 | new_tmpl |
c7222c02 MM |
3974 | = build_template_decl (decl, current_template_parms, |
3975 | member_template_p); | |
e1a5ccf7 JM |
3976 | DECL_TEMPLATE_RESULT (new_tmpl) = decl; |
3977 | TREE_TYPE (new_tmpl) = TREE_TYPE (decl); | |
3978 | DECL_TI_TEMPLATE (decl) = new_tmpl; | |
3979 | SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl); | |
c8094d83 | 3980 | DECL_TEMPLATE_INFO (new_tmpl) |
e1b3e07d | 3981 | = tree_cons (tmpl, args, NULL_TREE); |
e1a5ccf7 | 3982 | |
c8094d83 MS |
3983 | register_specialization (new_tmpl, |
3984 | most_general_template (tmpl), | |
d63d5d0c ILT |
3985 | args, |
3986 | is_friend); | |
e1a5ccf7 | 3987 | return decl; |
98c1c668 | 3988 | } |
98c1c668 | 3989 | |
e1a5ccf7 | 3990 | /* Make sure the template headers we got make sense. */ |
6633d636 | 3991 | |
e1a5ccf7 JM |
3992 | parms = DECL_TEMPLATE_PARMS (tmpl); |
3993 | i = TMPL_PARMS_DEPTH (parms); | |
3994 | if (TMPL_ARGS_DEPTH (args) != i) | |
98c1c668 | 3995 | { |
0f51ccfc | 3996 | error ("expected %d levels of template parms for %q#D, got %d", |
0cbd7506 | 3997 | i, decl, TMPL_ARGS_DEPTH (args)); |
98c1c668 | 3998 | } |
e1a5ccf7 JM |
3999 | else |
4000 | for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms)) | |
4001 | { | |
4002 | a = TMPL_ARGS_LEVEL (args, i); | |
4003 | t = INNERMOST_TEMPLATE_PARMS (parms); | |
4004 | ||
4005 | if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a)) | |
4006 | { | |
4007 | if (current == decl) | |
0f51ccfc | 4008 | error ("got %d template parameters for %q#D", |
0cbd7506 | 4009 | TREE_VEC_LENGTH (a), decl); |
e1a5ccf7 | 4010 | else |
0f51ccfc | 4011 | error ("got %d template parameters for %q#T", |
0cbd7506 | 4012 | TREE_VEC_LENGTH (a), current); |
33bd39a2 | 4013 | error (" but %d required", TREE_VEC_LENGTH (t)); |
f1cc0515 | 4014 | return error_mark_node; |
e1a5ccf7 | 4015 | } |
98c1c668 | 4016 | |
e1a5ccf7 JM |
4017 | if (current == decl) |
4018 | current = ctx; | |
4019 | else | |
ba75df2b PC |
4020 | current = (TYPE_P (current) |
4021 | ? TYPE_CONTEXT (current) | |
4022 | : DECL_CONTEXT (current)); | |
e1a5ccf7 | 4023 | } |
91a77d68 JM |
4024 | |
4025 | /* Check that the parms are used in the appropriate qualifying scopes | |
4026 | in the declarator. */ | |
4027 | if (!comp_template_args | |
4028 | (TI_ARGS (tinfo), | |
4029 | TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl))))) | |
4030 | { | |
4031 | error ("\ | |
4032 | template arguments to %qD do not match original template %qD", | |
4033 | decl, DECL_TEMPLATE_RESULT (tmpl)); | |
4034 | if (!uses_template_parms (TI_ARGS (tinfo))) | |
4035 | inform ("use template<> for an explicit specialization"); | |
4036 | /* Avoid crash in import_export_decl. */ | |
4037 | DECL_INTERFACE_KNOWN (decl) = 1; | |
4038 | return error_mark_node; | |
4039 | } | |
5566b478 | 4040 | } |
8d08fdba | 4041 | |
5566b478 MS |
4042 | DECL_TEMPLATE_RESULT (tmpl) = decl; |
4043 | TREE_TYPE (tmpl) = TREE_TYPE (decl); | |
8d08fdba | 4044 | |
36a117a5 MM |
4045 | /* Push template declarations for global functions and types. Note |
4046 | that we do not try to push a global template friend declared in a | |
4047 | template class; such a thing may well depend on the template | |
39c01e4c | 4048 | parameters of the class. */ |
c8094d83 | 4049 | if (new_template_p && !ctx |
36a117a5 | 4050 | && !(is_friend && template_class_depth (current_class_type) > 0)) |
c6f9f83b | 4051 | { |
d63d5d0c | 4052 | tmpl = pushdecl_namespace_level (tmpl, is_friend); |
c6f9f83b KL |
4053 | if (tmpl == error_mark_node) |
4054 | return error_mark_node; | |
bd3d082e KL |
4055 | |
4056 | /* Hide template friend classes that haven't been declared yet. */ | |
4057 | if (is_friend && TREE_CODE (decl) == TYPE_DECL) | |
4058 | { | |
4059 | DECL_ANTICIPATED (tmpl) = 1; | |
4060 | DECL_FRIEND_P (tmpl) = 1; | |
4061 | } | |
c6f9f83b | 4062 | } |
8d08fdba | 4063 | |
5566b478 | 4064 | if (primary) |
5dd236e2 | 4065 | { |
ef3b7b17 JM |
4066 | tree parms = DECL_TEMPLATE_PARMS (tmpl); |
4067 | int i; | |
4068 | ||
5dd236e2 NS |
4069 | DECL_PRIMARY_TEMPLATE (tmpl) = tmpl; |
4070 | if (DECL_CONV_FN_P (tmpl)) | |
4071 | { | |
ef3b7b17 | 4072 | int depth = TMPL_PARMS_DEPTH (parms); |
6e04241f | 4073 | |
5dd236e2 NS |
4074 | /* It is a conversion operator. See if the type converted to |
4075 | depends on innermost template operands. */ | |
c8094d83 | 4076 | |
d43f603d KL |
4077 | if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl)), |
4078 | depth)) | |
5dd236e2 NS |
4079 | DECL_TEMPLATE_CONV_FN_P (tmpl) = 1; |
4080 | } | |
ef3b7b17 JM |
4081 | |
4082 | /* Give template template parms a DECL_CONTEXT of the template | |
4083 | for which they are a parameter. */ | |
4084 | parms = INNERMOST_TEMPLATE_PARMS (parms); | |
4085 | for (i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i) | |
4086 | { | |
4087 | tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i)); | |
4088 | if (TREE_CODE (parm) == TEMPLATE_DECL) | |
4089 | DECL_CONTEXT (parm) = tmpl; | |
4090 | } | |
5dd236e2 | 4091 | } |
5566b478 | 4092 | |
a692ad2e | 4093 | /* The DECL_TI_ARGS of DECL contains full set of arguments referring |
28ba38e3 KL |
4094 | back to its most general template. If TMPL is a specialization, |
4095 | ARGS may only have the innermost set of arguments. Add the missing | |
4096 | argument levels if necessary. */ | |
4097 | if (DECL_TEMPLATE_INFO (tmpl)) | |
4098 | args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args); | |
4099 | ||
e1b3e07d | 4100 | info = tree_cons (tmpl, args, NULL_TREE); |
5566b478 | 4101 | |
9188c363 | 4102 | if (DECL_IMPLICIT_TYPEDEF_P (decl)) |
a0ad3539 | 4103 | SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info); |
2aaf816d | 4104 | else if (DECL_LANG_SPECIFIC (decl)) |
5566b478 | 4105 | DECL_TEMPLATE_INFO (decl) = info; |
3ac3d9ea MM |
4106 | |
4107 | return DECL_TEMPLATE_RESULT (tmpl); | |
8d08fdba MS |
4108 | } |
4109 | ||
6757edfe | 4110 | tree |
3a978d72 | 4111 | push_template_decl (tree decl) |
6757edfe | 4112 | { |
d63d5d0c | 4113 | return push_template_decl_real (decl, false); |
6757edfe MM |
4114 | } |
4115 | ||
4116 | /* Called when a class template TYPE is redeclared with the indicated | |
4117 | template PARMS, e.g.: | |
7fe6899f MM |
4118 | |
4119 | template <class T> struct S; | |
4120 | template <class T> struct S {}; */ | |
4121 | ||
60feef2c | 4122 | bool |
3a978d72 | 4123 | redeclare_class_template (tree type, tree parms) |
7fe6899f | 4124 | { |
3d7de1fa | 4125 | tree tmpl; |
6757edfe | 4126 | tree tmpl_parms; |
7fe6899f MM |
4127 | int i; |
4128 | ||
3d7de1fa MM |
4129 | if (!TYPE_TEMPLATE_INFO (type)) |
4130 | { | |
0f51ccfc | 4131 | error ("%qT is not a template type", type); |
60feef2c | 4132 | return false; |
3d7de1fa MM |
4133 | } |
4134 | ||
4135 | tmpl = TYPE_TI_TEMPLATE (type); | |
7fe6899f MM |
4136 | if (!PRIMARY_TEMPLATE_P (tmpl)) |
4137 | /* The type is nested in some template class. Nothing to worry | |
4138 | about here; there are no new template parameters for the nested | |
4139 | type. */ | |
60feef2c | 4140 | return true; |
7fe6899f | 4141 | |
ee921ddf MM |
4142 | if (!parms) |
4143 | { | |
4144 | error ("template specifiers not specified in declaration of %qD", | |
4145 | tmpl); | |
60feef2c | 4146 | return false; |
ee921ddf MM |
4147 | } |
4148 | ||
6757edfe MM |
4149 | parms = INNERMOST_TEMPLATE_PARMS (parms); |
4150 | tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl); | |
4151 | ||
7fe6899f MM |
4152 | if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms)) |
4153 | { | |
b131ad7c MLI |
4154 | error ("redeclared with %d template parameter(s)", |
4155 | TREE_VEC_LENGTH (parms)); | |
4156 | inform ("previous declaration %q+D used %d template parameter(s)", | |
4157 | tmpl, TREE_VEC_LENGTH (tmpl_parms)); | |
60feef2c | 4158 | return false; |
7fe6899f MM |
4159 | } |
4160 | ||
4161 | for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i) | |
4162 | { | |
2d8ba2c7 LM |
4163 | tree tmpl_parm; |
4164 | tree parm; | |
4165 | tree tmpl_default; | |
4166 | tree parm_default; | |
4167 | ||
4168 | if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node | |
4169 | || TREE_VEC_ELT (parms, i) == error_mark_node) | |
4170 | continue; | |
4171 | ||
4172 | tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i)); | |
4173 | parm = TREE_VALUE (TREE_VEC_ELT (parms, i)); | |
4174 | tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)); | |
4175 | parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i)); | |
7fe6899f | 4176 | |
2649701f KL |
4177 | /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or |
4178 | TEMPLATE_DECL. */ | |
0f67a82f LM |
4179 | if (tmpl_parm != error_mark_node |
4180 | && (TREE_CODE (tmpl_parm) != TREE_CODE (parm) | |
4181 | || (TREE_CODE (tmpl_parm) != TYPE_DECL | |
4182 | && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm))))) | |
7fe6899f | 4183 | { |
dee15844 | 4184 | error ("template parameter %q+#D", tmpl_parm); |
0f51ccfc | 4185 | error ("redeclared here as %q#D", parm); |
60feef2c | 4186 | return false; |
7fe6899f MM |
4187 | } |
4188 | ||
4189 | if (tmpl_default != NULL_TREE && parm_default != NULL_TREE) | |
4190 | { | |
4191 | /* We have in [temp.param]: | |
4192 | ||
4193 | A template-parameter may not be given default arguments | |
4194 | by two different declarations in the same scope. */ | |
0f51ccfc | 4195 | error ("redefinition of default argument for %q#D", parm); |
b131ad7c | 4196 | inform ("%Joriginal definition appeared here", tmpl_parm); |
60feef2c | 4197 | return false; |
7fe6899f MM |
4198 | } |
4199 | ||
4200 | if (parm_default != NULL_TREE) | |
4201 | /* Update the previous template parameters (which are the ones | |
4202 | that will really count) with the new default value. */ | |
4203 | TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default; | |
7ce74c5e MM |
4204 | else if (tmpl_default != NULL_TREE) |
4205 | /* Update the new parameters, too; they'll be used as the | |
4206 | parameters for any members. */ | |
4207 | TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default; | |
7fe6899f | 4208 | } |
60feef2c LM |
4209 | |
4210 | return true; | |
7fe6899f | 4211 | } |
75650646 | 4212 | |
9baa27a9 MM |
4213 | /* Simplify EXPR if it is a non-dependent expression. Returns the |
4214 | (possibly simplified) expression. */ | |
4215 | ||
4216 | tree | |
4217 | fold_non_dependent_expr (tree expr) | |
4218 | { | |
d4a200d3 SM |
4219 | if (expr == NULL_TREE) |
4220 | return NULL_TREE; | |
4221 | ||
9baa27a9 MM |
4222 | /* If we're in a template, but EXPR isn't value dependent, simplify |
4223 | it. We're supposed to treat: | |
c8094d83 | 4224 | |
9baa27a9 MM |
4225 | template <typename T> void f(T[1 + 1]); |
4226 | template <typename T> void f(T[2]); | |
c8094d83 | 4227 | |
9baa27a9 MM |
4228 | as two declarations of the same function, for example. */ |
4229 | if (processing_template_decl | |
4230 | && !type_dependent_expression_p (expr) | |
7416ab02 | 4231 | && !value_dependent_expression_p (expr)) |
9baa27a9 MM |
4232 | { |
4233 | HOST_WIDE_INT saved_processing_template_decl; | |
4234 | ||
4235 | saved_processing_template_decl = processing_template_decl; | |
4236 | processing_template_decl = 0; | |
4237 | expr = tsubst_copy_and_build (expr, | |
4238 | /*args=*/NULL_TREE, | |
4239 | tf_error, | |
4240 | /*in_decl=*/NULL_TREE, | |
015c2c66 MM |
4241 | /*function_p=*/false, |
4242 | /*integral_constant_expression_p=*/true); | |
9baa27a9 MM |
4243 | processing_template_decl = saved_processing_template_decl; |
4244 | } | |
4245 | return expr; | |
4246 | } | |
4247 | ||
b6ab6892 GB |
4248 | /* EXPR is an expression which is used in a constant-expression context. |
4249 | For instance, it could be a VAR_DECL with a constant initializer. | |
4250 | Extract the innest constant expression. | |
c8094d83 | 4251 | |
8a784e4a NS |
4252 | This is basically a more powerful version of |
4253 | integral_constant_value, which can be used also in templates where | |
4254 | initializers can maintain a syntactic rather than semantic form | |
4255 | (even if they are non-dependent, for access-checking purposes). */ | |
b6ab6892 | 4256 | |
993acaec | 4257 | static tree |
b6ab6892 GB |
4258 | fold_decl_constant_value (tree expr) |
4259 | { | |
4ef69b83 GB |
4260 | tree const_expr = expr; |
4261 | do | |
b6ab6892 | 4262 | { |
b6ab6892 | 4263 | expr = fold_non_dependent_expr (const_expr); |
4ef69b83 | 4264 | const_expr = integral_constant_value (expr); |
b6ab6892 | 4265 | } |
4ef69b83 | 4266 | while (expr != const_expr); |
b6ab6892 | 4267 | |
8a784e4a | 4268 | return expr; |
b6ab6892 GB |
4269 | } |
4270 | ||
4271 | /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which | |
4272 | must be a function or a pointer-to-function type, as specified | |
4273 | in [temp.arg.nontype]: disambiguate EXPR if it is an overload set, | |
4274 | and check that the resulting function has external linkage. */ | |
4275 | ||
4276 | static tree | |
4277 | convert_nontype_argument_function (tree type, tree expr) | |
4278 | { | |
4279 | tree fns = expr; | |
4280 | tree fn, fn_no_ptr; | |
4281 | ||
4282 | fn = instantiate_type (type, fns, tf_none); | |
4283 | if (fn == error_mark_node) | |
4284 | return error_mark_node; | |
4285 | ||
4286 | fn_no_ptr = fn; | |
4287 | if (TREE_CODE (fn_no_ptr) == ADDR_EXPR) | |
4288 | fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0); | |
eff3a276 MM |
4289 | if (TREE_CODE (fn_no_ptr) == BASELINK) |
4290 | fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr); | |
4291 | ||
b6ab6892 GB |
4292 | /* [temp.arg.nontype]/1 |
4293 | ||
4294 | A template-argument for a non-type, non-template template-parameter | |
4295 | shall be one of: | |
4296 | [...] | |
4297 | -- the address of an object or function with external linkage. */ | |
4298 | if (!DECL_EXTERNAL_LINKAGE_P (fn_no_ptr)) | |
4299 | { | |
4300 | error ("%qE is not a valid template argument for type %qT " | |
4301 | "because function %qD has not external linkage", | |
4302 | expr, type, fn_no_ptr); | |
4303 | return NULL_TREE; | |
4304 | } | |
4305 | ||
4306 | return fn; | |
4307 | } | |
4308 | ||
75650646 MM |
4309 | /* Attempt to convert the non-type template parameter EXPR to the |
4310 | indicated TYPE. If the conversion is successful, return the | |
dc957d14 | 4311 | converted value. If the conversion is unsuccessful, return |
75650646 MM |
4312 | NULL_TREE if we issued an error message, or error_mark_node if we |
4313 | did not. We issue error messages for out-and-out bad template | |
4314 | parameters, but not simply because the conversion failed, since we | |
9baa27a9 | 4315 | might be just trying to do argument deduction. Both TYPE and EXPR |
b6ab6892 GB |
4316 | must be non-dependent. |
4317 | ||
4318 | The conversion follows the special rules described in | |
4319 | [temp.arg.nontype], and it is much more strict than an implicit | |
4320 | conversion. | |
4321 | ||
4322 | This function is called twice for each template argument (see | |
4323 | lookup_template_class for a more accurate description of this | |
4324 | problem). This means that we need to handle expressions which | |
4325 | are not valid in a C++ source, but can be created from the | |
4326 | first call (for instance, casts to perform conversions). These | |
4327 | hacks can go away after we fix the double coercion problem. */ | |
75650646 MM |
4328 | |
4329 | static tree | |
3a978d72 | 4330 | convert_nontype_argument (tree type, tree expr) |
75650646 | 4331 | { |
9baa27a9 MM |
4332 | tree expr_type; |
4333 | ||
b6ab6892 GB |
4334 | /* Detect immediately string literals as invalid non-type argument. |
4335 | This special-case is not needed for correctness (we would easily | |
4336 | catch this later), but only to provide better diagnostic for this | |
4337 | common user mistake. As suggested by DR 100, we do not mention | |
4338 | linkage issues in the diagnostic as this is not the point. */ | |
4339 | if (TREE_CODE (expr) == STRING_CST) | |
4340 | { | |
4341 | error ("%qE is not a valid template argument for type %qT " | |
4342 | "because string literals can never be used in this context", | |
4343 | expr, type); | |
4344 | return NULL_TREE; | |
4345 | } | |
4346 | ||
9baa27a9 MM |
4347 | /* If we are in a template, EXPR may be non-dependent, but still |
4348 | have a syntactic, rather than semantic, form. For example, EXPR | |
4349 | might be a SCOPE_REF, rather than the VAR_DECL to which the | |
4350 | SCOPE_REF refers. Preserving the qualifying scope is necessary | |
4351 | so that access checking can be performed when the template is | |
4352 | instantiated -- but here we need the resolved form so that we can | |
4353 | convert the argument. */ | |
4354 | expr = fold_non_dependent_expr (expr); | |
b166a559 VR |
4355 | if (error_operand_p (expr)) |
4356 | return error_mark_node; | |
9baa27a9 | 4357 | expr_type = TREE_TYPE (expr); |
75650646 | 4358 | |
b6ab6892 GB |
4359 | /* HACK: Due to double coercion, we can get a |
4360 | NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here, | |
4361 | which is the tree that we built on the first call (see | |
4362 | below when coercing to reference to object or to reference to | |
4363 | function). We just strip everything and get to the arg. | |
4364 | See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C | |
4365 | for examples. */ | |
4366 | if (TREE_CODE (expr) == NOP_EXPR) | |
75650646 | 4367 | { |
b6ab6892 | 4368 | if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type)) |
75650646 | 4369 | { |
b6ab6892 GB |
4370 | /* ??? Maybe we could use convert_from_reference here, but we |
4371 | would need to relax its constraints because the NOP_EXPR | |
4372 | could actually change the type to something more cv-qualified, | |
4373 | and this is not folded by convert_from_reference. */ | |
4374 | tree addr = TREE_OPERAND (expr, 0); | |
4375 | gcc_assert (TREE_CODE (expr_type) == REFERENCE_TYPE); | |
4376 | gcc_assert (TREE_CODE (addr) == ADDR_EXPR); | |
4377 | gcc_assert (TREE_CODE (TREE_TYPE (addr)) == POINTER_TYPE); | |
4378 | gcc_assert (same_type_ignoring_top_level_qualifiers_p | |
4379 | (TREE_TYPE (expr_type), | |
4380 | TREE_TYPE (TREE_TYPE (addr)))); | |
4381 | ||
4382 | expr = TREE_OPERAND (addr, 0); | |
4383 | expr_type = TREE_TYPE (expr); | |
75650646 MM |
4384 | } |
4385 | ||
b6ab6892 GB |
4386 | /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the |
4387 | parameter is a pointer to object, through decay and | |
4388 | qualification conversion. Let's strip everything. */ | |
4389 | else if (TYPE_PTROBV_P (type)) | |
75650646 | 4390 | { |
b6ab6892 GB |
4391 | STRIP_NOPS (expr); |
4392 | gcc_assert (TREE_CODE (expr) == ADDR_EXPR); | |
4393 | gcc_assert (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE); | |
4394 | /* Skip the ADDR_EXPR only if it is part of the decay for | |
4395 | an array. Otherwise, it is part of the original argument | |
4396 | in the source code. */ | |
4397 | if (TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == ARRAY_TYPE) | |
4398 | expr = TREE_OPERAND (expr, 0); | |
4399 | expr_type = TREE_TYPE (expr); | |
f3400fe2 | 4400 | } |
75650646 | 4401 | } |
c61dce3a | 4402 | |
b6ab6892 | 4403 | /* [temp.arg.nontype]/5, bullet 1 |
75650646 | 4404 | |
b6ab6892 GB |
4405 | For a non-type template-parameter of integral or enumeration type, |
4406 | integral promotions (_conv.prom_) and integral conversions | |
4407 | (_conv.integral_) are applied. */ | |
4408 | if (INTEGRAL_TYPE_P (type)) | |
75650646 | 4409 | { |
75650646 MM |
4410 | if (!INTEGRAL_TYPE_P (expr_type)) |
4411 | return error_mark_node; | |
fddabb2c | 4412 | |
b6ab6892 GB |
4413 | expr = fold_decl_constant_value (expr); |
4414 | /* Notice that there are constant expressions like '4 % 0' which | |
4415 | do not fold into integer constants. */ | |
db02b6b9 | 4416 | if (TREE_CODE (expr) != INTEGER_CST) |
b6ab6892 GB |
4417 | { |
4418 | error ("%qE is not a valid template argument for type %qT " | |
4419 | "because it is a non-constant expression", expr, type); | |
4420 | return NULL_TREE; | |
4421 | } | |
75650646 | 4422 | |
b6ab6892 GB |
4423 | /* At this point, an implicit conversion does what we want, |
4424 | because we already know that the expression is of integral | |
4425 | type. */ | |
4426 | expr = ocp_convert (type, expr, CONV_IMPLICIT, LOOKUP_PROTECT); | |
4427 | if (expr == error_mark_node) | |
4428 | return error_mark_node; | |
75650646 | 4429 | |
b6ab6892 GB |
4430 | /* Conversion was allowed: fold it to a bare integer constant. */ |
4431 | expr = fold (expr); | |
4432 | } | |
4433 | /* [temp.arg.nontype]/5, bullet 2 | |
75650646 | 4434 | |
b6ab6892 GB |
4435 | For a non-type template-parameter of type pointer to object, |
4436 | qualification conversions (_conv.qual_) and the array-to-pointer | |
4437 | conversion (_conv.array_) are applied. */ | |
4438 | else if (TYPE_PTROBV_P (type)) | |
4439 | { | |
4440 | /* [temp.arg.nontype]/1 (TC1 version, DR 49): | |
75650646 | 4441 | |
b6ab6892 GB |
4442 | A template-argument for a non-type, non-template template-parameter |
4443 | shall be one of: [...] | |
75650646 | 4444 | |
b6ab6892 GB |
4445 | -- the name of a non-type template-parameter; |
4446 | -- the address of an object or function with external linkage, [...] | |
4447 | expressed as "& id-expression" where the & is optional if the name | |
4448 | refers to a function or array, or if the corresponding | |
4449 | template-parameter is a reference. | |
c8094d83 | 4450 | |
b6ab6892 GB |
4451 | Here, we do not care about functions, as they are invalid anyway |
4452 | for a parameter of type pointer-to-object. */ | |
1082fd10 MM |
4453 | |
4454 | if (DECL_P (expr) && DECL_TEMPLATE_PARM_P (expr)) | |
4455 | /* Non-type template parameters are OK. */ | |
4456 | ; | |
4457 | else if (TREE_CODE (expr) != ADDR_EXPR | |
4458 | && TREE_CODE (expr_type) != ARRAY_TYPE) | |
4459 | { | |
4460 | if (TREE_CODE (expr) == VAR_DECL) | |
4461 | { | |
4462 | error ("%qD is not a valid template argument " | |
4463 | "because %qD is a variable, not the address of " | |
4464 | "a variable", | |
4465 | expr, expr); | |
4466 | return NULL_TREE; | |
4467 | } | |
4468 | /* Other values, like integer constants, might be valid | |
4469 | non-type arguments of some other type. */ | |
4470 | return error_mark_node; | |
4471 | } | |
4472 | else | |
4473 | { | |
4474 | tree decl; | |
4475 | ||
4476 | decl = ((TREE_CODE (expr) == ADDR_EXPR) | |
4477 | ? TREE_OPERAND (expr, 0) : expr); | |
4478 | if (TREE_CODE (decl) != VAR_DECL) | |
4479 | { | |
4480 | error ("%qE is not a valid template argument of type %qT " | |
4481 | "because %qE is not a variable", | |
4482 | expr, type, decl); | |
4483 | return NULL_TREE; | |
4484 | } | |
4485 | else if (!DECL_EXTERNAL_LINKAGE_P (decl)) | |
4486 | { | |
4487 | error ("%qE is not a valid template argument of type %qT " | |
4488 | "because %qD does not have external linkage", | |
4489 | expr, type, decl); | |
4490 | return NULL_TREE; | |
4491 | } | |
4492 | } | |
b6ab6892 GB |
4493 | |
4494 | expr = decay_conversion (expr); | |
4495 | if (expr == error_mark_node) | |
4496 | return error_mark_node; | |
75650646 | 4497 | |
b6ab6892 GB |
4498 | expr = perform_qualification_conversions (type, expr); |
4499 | if (expr == error_mark_node) | |
4500 | return error_mark_node; | |
b6ab6892 GB |
4501 | } |
4502 | /* [temp.arg.nontype]/5, bullet 3 | |
4503 | ||
4504 | For a non-type template-parameter of type reference to object, no | |
4505 | conversions apply. The type referred to by the reference may be more | |
4506 | cv-qualified than the (otherwise identical) type of the | |
4507 | template-argument. The template-parameter is bound directly to the | |
4508 | template-argument, which must be an lvalue. */ | |
4509 | else if (TYPE_REF_OBJ_P (type)) | |
4510 | { | |
4511 | if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type), | |
4512 | expr_type)) | |
4513 | return error_mark_node; | |
75650646 | 4514 | |
b6ab6892 GB |
4515 | if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type)) |
4516 | { | |
4517 | error ("%qE is not a valid template argument for type %qT " | |
4518 | "because of conflicts in cv-qualification", expr, type); | |
4519 | return NULL_TREE; | |
4520 | } | |
c8094d83 | 4521 | |
b6ab6892 GB |
4522 | if (!real_lvalue_p (expr)) |
4523 | { | |
4524 | error ("%qE is not a valid template argument for type %qT " | |
944fd207 | 4525 | "because it is not an lvalue", expr, type); |
b6ab6892 GB |
4526 | return NULL_TREE; |
4527 | } | |
e6e174e5 | 4528 | |
b6ab6892 | 4529 | /* [temp.arg.nontype]/1 |
75650646 | 4530 | |
b6ab6892 GB |
4531 | A template-argument for a non-type, non-template template-parameter |
4532 | shall be one of: [...] | |
75650646 | 4533 | |
03fd3f84 | 4534 | -- the address of an object or function with external linkage. */ |
b6ab6892 GB |
4535 | if (!DECL_EXTERNAL_LINKAGE_P (expr)) |
4536 | { | |
4537 | error ("%qE is not a valid template argument for type %qT " | |
4538 | "because object %qD has not external linkage", | |
4539 | expr, type, expr); | |
4540 | return NULL_TREE; | |
4541 | } | |
0dc09a61 | 4542 | |
b6ab6892 GB |
4543 | expr = build_nop (type, build_address (expr)); |
4544 | } | |
4545 | /* [temp.arg.nontype]/5, bullet 4 | |
4546 | ||
4547 | For a non-type template-parameter of type pointer to function, only | |
4548 | the function-to-pointer conversion (_conv.func_) is applied. If the | |
4549 | template-argument represents a set of overloaded functions (or a | |
4550 | pointer to such), the matching function is selected from the set | |
4551 | (_over.over_). */ | |
4552 | else if (TYPE_PTRFN_P (type)) | |
4553 | { | |
4554 | /* If the argument is a template-id, we might not have enough | |
2c164de6 | 4555 | context information to decay the pointer. */ |
b6ab6892 GB |
4556 | if (!type_unknown_p (expr_type)) |
4557 | { | |
4558 | expr = decay_conversion (expr); | |
4559 | if (expr == error_mark_node) | |
4560 | return error_mark_node; | |
4561 | } | |
75650646 | 4562 | |
b6ab6892 GB |
4563 | expr = convert_nontype_argument_function (type, expr); |
4564 | if (!expr || expr == error_mark_node) | |
4565 | return expr; | |
4566 | } | |
4567 | /* [temp.arg.nontype]/5, bullet 5 | |
75650646 | 4568 | |
b6ab6892 GB |
4569 | For a non-type template-parameter of type reference to function, no |
4570 | conversions apply. If the template-argument represents a set of | |
4571 | overloaded functions, the matching function is selected from the set | |
4572 | (_over.over_). */ | |
4573 | else if (TYPE_REFFN_P (type)) | |
4574 | { | |
4575 | if (TREE_CODE (expr) == ADDR_EXPR) | |
4576 | { | |
4577 | error ("%qE is not a valid template argument for type %qT " | |
4578 | "because it is a pointer", expr, type); | |
4579 | inform ("try using %qE instead", TREE_OPERAND (expr, 0)); | |
4580 | return NULL_TREE; | |
4581 | } | |
75650646 | 4582 | |
b6ab6892 GB |
4583 | expr = convert_nontype_argument_function (TREE_TYPE (type), expr); |
4584 | if (!expr || expr == error_mark_node) | |
4585 | return expr; | |
75650646 | 4586 | |
7866705a | 4587 | expr = build_nop (type, build_address (expr)); |
b6ab6892 GB |
4588 | } |
4589 | /* [temp.arg.nontype]/5, bullet 6 | |
75650646 | 4590 | |
b6ab6892 GB |
4591 | For a non-type template-parameter of type pointer to member function, |
4592 | no conversions apply. If the template-argument represents a set of | |
4593 | overloaded member functions, the matching member function is selected | |
4594 | from the set (_over.over_). */ | |
4595 | else if (TYPE_PTRMEMFUNC_P (type)) | |
4596 | { | |
4597 | expr = instantiate_type (type, expr, tf_none); | |
4598 | if (expr == error_mark_node) | |
4599 | return error_mark_node; | |
75650646 | 4600 | |
b6ab6892 GB |
4601 | /* There is no way to disable standard conversions in |
4602 | resolve_address_of_overloaded_function (called by | |
4603 | instantiate_type). It is possible that the call succeeded by | |
4604 | converting &B::I to &D::I (where B is a base of D), so we need | |
4605 | to reject this conversion here. | |
75650646 | 4606 | |
b6ab6892 GB |
4607 | Actually, even if there was a way to disable standard conversions, |
4608 | it would still be better to reject them here so that we can | |
4609 | provide a superior diagnostic. */ | |
4610 | if (!same_type_p (TREE_TYPE (expr), type)) | |
4611 | { | |
4612 | /* Make sure we are just one standard conversion off. */ | |
4613 | gcc_assert (can_convert (type, TREE_TYPE (expr))); | |
4614 | error ("%qE is not a valid template argument for type %qT " | |
4615 | "because it is of type %qT", expr, type, | |
4616 | TREE_TYPE (expr)); | |
4617 | inform ("standard conversions are not allowed in this context"); | |
4618 | return NULL_TREE; | |
4619 | } | |
4620 | } | |
4621 | /* [temp.arg.nontype]/5, bullet 7 | |
59e7a901 | 4622 | |
b6ab6892 GB |
4623 | For a non-type template-parameter of type pointer to data member, |
4624 | qualification conversions (_conv.qual_) are applied. */ | |
4625 | else if (TYPE_PTRMEM_P (type)) | |
4626 | { | |
4627 | expr = perform_qualification_conversions (type, expr); | |
4628 | if (expr == error_mark_node) | |
75650646 | 4629 | return expr; |
75650646 | 4630 | } |
b6ab6892 GB |
4631 | /* A template non-type parameter must be one of the above. */ |
4632 | else | |
4633 | gcc_unreachable (); | |
75650646 | 4634 | |
b6ab6892 GB |
4635 | /* Sanity check: did we actually convert the argument to the |
4636 | right type? */ | |
4637 | gcc_assert (same_type_p (type, TREE_TYPE (expr))); | |
4638 | return expr; | |
75650646 MM |
4639 | } |
4640 | ||
b6ab6892 | 4641 | |
c8094d83 MS |
4642 | /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for |
4643 | template template parameters. Both PARM_PARMS and ARG_PARMS are | |
4644 | vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL | |
744fac59 | 4645 | or PARM_DECL. |
c8094d83 | 4646 | |
744fac59 | 4647 | Consider the example: |
e7e93965 MM |
4648 | template <class T> class A; |
4649 | template<template <class U> class TT> class B; | |
744fac59 | 4650 | |
e7e93965 MM |
4651 | For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are |
4652 | the parameters to A, and OUTER_ARGS contains A. */ | |
744fac59 KL |
4653 | |
4654 | static int | |
c8094d83 | 4655 | coerce_template_template_parms (tree parm_parms, |
0cbd7506 MS |
4656 | tree arg_parms, |
4657 | tsubst_flags_t complain, | |
3a978d72 | 4658 | tree in_decl, |
0cbd7506 | 4659 | tree outer_args) |
744fac59 KL |
4660 | { |
4661 | int nparms, nargs, i; | |
4662 | tree parm, arg; | |
4663 | ||
50bc768d NS |
4664 | gcc_assert (TREE_CODE (parm_parms) == TREE_VEC); |
4665 | gcc_assert (TREE_CODE (arg_parms) == TREE_VEC); | |
744fac59 KL |
4666 | |
4667 | nparms = TREE_VEC_LENGTH (parm_parms); | |
4668 | nargs = TREE_VEC_LENGTH (arg_parms); | |
4669 | ||
e7e93965 | 4670 | if (nargs != nparms) |
744fac59 KL |
4671 | return 0; |
4672 | ||
4673 | for (i = 0; i < nparms; ++i) | |
4674 | { | |
2d8ba2c7 LM |
4675 | if (TREE_VEC_ELT (parm_parms, i) == error_mark_node |
4676 | || TREE_VEC_ELT (arg_parms, i) == error_mark_node) | |
42b304f1 LM |
4677 | continue; |
4678 | ||
744fac59 KL |
4679 | parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i)); |
4680 | arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i)); | |
4681 | ||
4682 | if (arg == NULL_TREE || arg == error_mark_node | |
0cbd7506 | 4683 | || parm == NULL_TREE || parm == error_mark_node) |
744fac59 KL |
4684 | return 0; |
4685 | ||
4686 | if (TREE_CODE (arg) != TREE_CODE (parm)) | |
0cbd7506 | 4687 | return 0; |
744fac59 KL |
4688 | |
4689 | switch (TREE_CODE (parm)) | |
4690 | { | |
744fac59 KL |
4691 | case TEMPLATE_DECL: |
4692 | /* We encounter instantiations of templates like | |
4693 | template <template <template <class> class> class TT> | |
4694 | class C; */ | |
700466c2 JM |
4695 | { |
4696 | tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm); | |
4697 | tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg); | |
4698 | ||
8c6ab2db NS |
4699 | if (!coerce_template_template_parms |
4700 | (parmparm, argparm, complain, in_decl, outer_args)) | |
700466c2 JM |
4701 | return 0; |
4702 | } | |
5d80a306 DG |
4703 | /* Fall through. */ |
4704 | ||
4705 | case TYPE_DECL: | |
4706 | if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)) | |
4707 | != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))) | |
4708 | /* One is a parameter pack, the other is not. */ | |
4709 | return 0; | |
700466c2 | 4710 | break; |
744fac59 KL |
4711 | |
4712 | case PARM_DECL: | |
4713 | /* The tsubst call is used to handle cases such as | |
00bdb87f KL |
4714 | |
4715 | template <int> class C {}; | |
4716 | template <class T, template <T> class TT> class D {}; | |
4717 | D<int, C> d; | |
4718 | ||
744fac59 | 4719 | i.e. the parameter list of TT depends on earlier parameters. */ |
00bdb87f KL |
4720 | if (!dependent_type_p (TREE_TYPE (arg)) |
4721 | && !same_type_p | |
4722 | (tsubst (TREE_TYPE (parm), outer_args, complain, in_decl), | |
4723 | TREE_TYPE (arg))) | |
744fac59 | 4724 | return 0; |
5d80a306 DG |
4725 | |
4726 | if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)) | |
4727 | != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))) | |
4728 | /* One is a parameter pack, the other is not. */ | |
4729 | return 0; | |
744fac59 | 4730 | break; |
c8094d83 | 4731 | |
744fac59 | 4732 | default: |
315fb5db | 4733 | gcc_unreachable (); |
744fac59 KL |
4734 | } |
4735 | } | |
4736 | return 1; | |
4737 | } | |
4738 | ||
8b5b8b7c MM |
4739 | /* Convert the indicated template ARG as necessary to match the |
4740 | indicated template PARM. Returns the converted ARG, or | |
c2ea3a40 NS |
4741 | error_mark_node if the conversion was unsuccessful. Error and |
4742 | warning messages are issued under control of COMPLAIN. This | |
4743 | conversion is for the Ith parameter in the parameter list. ARGS is | |
4744 | the full set of template arguments deduced so far. */ | |
8b5b8b7c MM |
4745 | |
4746 | static tree | |
c8094d83 | 4747 | convert_template_argument (tree parm, |
0cbd7506 MS |
4748 | tree arg, |
4749 | tree args, | |
4750 | tsubst_flags_t complain, | |
4751 | int i, | |
4752 | tree in_decl) | |
8b5b8b7c | 4753 | { |
88b82314 | 4754 | tree orig_arg; |
8b5b8b7c | 4755 | tree val; |
8b5b8b7c | 4756 | int is_type, requires_type, is_tmpl_type, requires_tmpl_type; |
c8094d83 | 4757 | |
c8094d83 | 4758 | if (TREE_CODE (arg) == TREE_LIST |
a5ac359a | 4759 | && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF) |
c8094d83 | 4760 | { |
8b5b8b7c MM |
4761 | /* The template argument was the name of some |
4762 | member function. That's usually | |
0e339752 | 4763 | invalid, but static members are OK. In any |
8b5b8b7c MM |
4764 | case, grab the underlying fields/functions |
4765 | and issue an error later if required. */ | |
88b82314 | 4766 | orig_arg = TREE_VALUE (arg); |
8b5b8b7c MM |
4767 | TREE_TYPE (arg) = unknown_type_node; |
4768 | } | |
4769 | ||
88b82314 DG |
4770 | orig_arg = arg; |
4771 | ||
8b5b8b7c MM |
4772 | requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL; |
4773 | requires_type = (TREE_CODE (parm) == TYPE_DECL | |
4774 | || requires_tmpl_type); | |
4775 | ||
d732e98f | 4776 | /* When determining whether an argument pack expansion is a template, |
5d80a306 | 4777 | look at the pattern. */ |
88b82314 DG |
4778 | if (TREE_CODE (arg) == TYPE_PACK_EXPANSION) |
4779 | arg = PACK_EXPANSION_PATTERN (arg); | |
5d80a306 DG |
4780 | |
4781 | is_tmpl_type = | |
88b82314 DG |
4782 | ((TREE_CODE (arg) == TEMPLATE_DECL |
4783 | && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL) | |
4784 | || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM | |
4785 | || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE); | |
c8094d83 | 4786 | |
b8c6534b KL |
4787 | if (is_tmpl_type |
4788 | && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM | |
4789 | || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)) | |
8b5b8b7c | 4790 | arg = TYPE_STUB_DECL (arg); |
8b5b8b7c | 4791 | |
2f939d94 | 4792 | is_type = TYPE_P (arg) || is_tmpl_type; |
8b5b8b7c MM |
4793 | |
4794 | if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF | |
4795 | && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM) | |
4796 | { | |
0f51ccfc | 4797 | pedwarn ("to refer to a type member of a template parameter, " |
88b82314 | 4798 | "use %<typename %E%>", orig_arg); |
c8094d83 | 4799 | |
88b82314 DG |
4800 | orig_arg = make_typename_type (TREE_OPERAND (arg, 0), |
4801 | TREE_OPERAND (arg, 1), | |
4802 | typename_type, | |
4803 | complain & tf_error); | |
4804 | arg = orig_arg; | |
8b5b8b7c MM |
4805 | is_type = 1; |
4806 | } | |
4807 | if (is_type != requires_type) | |
4808 | { | |
4809 | if (in_decl) | |
4810 | { | |
c2ea3a40 | 4811 | if (complain & tf_error) |
8b5b8b7c | 4812 | { |
0f51ccfc | 4813 | error ("type/value mismatch at argument %d in template " |
0cbd7506 MS |
4814 | "parameter list for %qD", |
4815 | i + 1, in_decl); | |
8b5b8b7c | 4816 | if (is_type) |
0f51ccfc | 4817 | error (" expected a constant of type %qT, got %qT", |
0cbd7506 | 4818 | TREE_TYPE (parm), |
1ad8aeeb | 4819 | (DECL_P (arg) ? DECL_NAME (arg) : orig_arg)); |
d12a7283 | 4820 | else if (requires_tmpl_type) |
88b82314 | 4821 | error (" expected a class template, got %qE", orig_arg); |
8b5b8b7c | 4822 | else |
88b82314 | 4823 | error (" expected a type, got %qE", orig_arg); |
8b5b8b7c MM |
4824 | } |
4825 | } | |
4826 | return error_mark_node; | |
4827 | } | |
4828 | if (is_tmpl_type ^ requires_tmpl_type) | |
4829 | { | |
c2ea3a40 | 4830 | if (in_decl && (complain & tf_error)) |
8b5b8b7c | 4831 | { |
0f51ccfc | 4832 | error ("type/value mismatch at argument %d in template " |
0cbd7506 MS |
4833 | "parameter list for %qD", |
4834 | i + 1, in_decl); | |
8b5b8b7c | 4835 | if (is_tmpl_type) |
0f51ccfc | 4836 | error (" expected a type, got %qT", DECL_NAME (arg)); |
8b5b8b7c | 4837 | else |
88b82314 | 4838 | error (" expected a class template, got %qT", orig_arg); |
8b5b8b7c MM |
4839 | } |
4840 | return error_mark_node; | |
4841 | } | |
c8094d83 | 4842 | |
8b5b8b7c MM |
4843 | if (is_type) |
4844 | { | |
4845 | if (requires_tmpl_type) | |
4846 | { | |
b8c6534b KL |
4847 | if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE) |
4848 | /* The number of argument required is not known yet. | |
4849 | Just accept it for now. */ | |
4850 | val = TREE_TYPE (arg); | |
8b5b8b7c MM |
4851 | else |
4852 | { | |
b8c6534b | 4853 | tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm); |
5d80a306 DG |
4854 | tree argparm; |
4855 | ||
88b82314 | 4856 | argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg); |
b8c6534b KL |
4857 | |
4858 | if (coerce_template_template_parms (parmparm, argparm, | |
4859 | complain, in_decl, | |
6150b602 | 4860 | args)) |
8b5b8b7c | 4861 | { |
88b82314 | 4862 | val = orig_arg; |
c8094d83 MS |
4863 | |
4864 | /* TEMPLATE_TEMPLATE_PARM node is preferred over | |
b8c6534b | 4865 | TEMPLATE_DECL. */ |
5d80a306 DG |
4866 | if (val != error_mark_node) |
4867 | { | |
4868 | if (DECL_TEMPLATE_TEMPLATE_PARM_P (val)) | |
4869 | val = TREE_TYPE (val); | |
4870 | else if (TREE_CODE (val) == TYPE_PACK_EXPANSION | |
88b82314 | 4871 | && DECL_TEMPLATE_TEMPLATE_PARM_P (arg)) |
5d80a306 | 4872 | { |
88b82314 | 4873 | val = TREE_TYPE (arg); |
5d80a306 DG |
4874 | val = make_pack_expansion (val); |
4875 | } | |
4876 | } | |
8b5b8b7c | 4877 | } |
b8c6534b KL |
4878 | else |
4879 | { | |
c2ea3a40 | 4880 | if (in_decl && (complain & tf_error)) |
b8c6534b | 4881 | { |
0f51ccfc | 4882 | error ("type/value mismatch at argument %d in " |
0cbd7506 MS |
4883 | "template parameter list for %qD", |
4884 | i + 1, in_decl); | |
0f51ccfc | 4885 | error (" expected a template of type %qD, got %qD", |
88b82314 | 4886 | parm, orig_arg); |
b8c6534b | 4887 | } |
c8094d83 | 4888 | |
b8c6534b KL |
4889 | val = error_mark_node; |
4890 | } | |
8b5b8b7c MM |
4891 | } |
4892 | } | |
4893 | else | |
88b82314 | 4894 | val = orig_arg; |
685e39c2 MM |
4895 | /* We only form one instance of each template specialization. |
4896 | Therefore, if we use a non-canonical variant (i.e., a | |
3db45ab5 | 4897 | typedef), any future messages referring to the type will use |
685e39c2 MM |
4898 | the typedef, which is confusing if those future uses do not |
4899 | themselves also use the typedef. */ | |
4900 | if (TYPE_P (val)) | |
4901 | val = canonical_type_variant (val); | |
8b5b8b7c MM |
4902 | } |
4903 | else | |
4904 | { | |
4393e105 | 4905 | tree t = tsubst (TREE_TYPE (parm), args, complain, in_decl); |
8b5b8b7c | 4906 | |
11b810f1 | 4907 | if (invalid_nontype_parm_type_p (t, complain)) |
0cbd7506 | 4908 | return error_mark_node; |
c8094d83 | 4909 | |
88b82314 | 4910 | if (!uses_template_parms (orig_arg) && !uses_template_parms (t)) |
8b5b8b7c MM |
4911 | /* We used to call digest_init here. However, digest_init |
4912 | will report errors, which we don't want when complain | |
4913 | is zero. More importantly, digest_init will try too | |
4914 | hard to convert things: for example, `0' should not be | |
4915 | converted to pointer type at this point according to | |
4916 | the standard. Accepting this is not merely an | |
4917 | extension, since deciding whether or not these | |
4918 | conversions can occur is part of determining which | |
dc957d14 | 4919 | function template to call, or whether a given explicit |
0e339752 | 4920 | argument specification is valid. */ |
88b82314 | 4921 | val = convert_nontype_argument (t, orig_arg); |
8b5b8b7c | 4922 | else |
88b82314 | 4923 | val = orig_arg; |
8b5b8b7c MM |
4924 | |
4925 | if (val == NULL_TREE) | |
4926 | val = error_mark_node; | |
c2ea3a40 | 4927 | else if (val == error_mark_node && (complain & tf_error)) |
88b82314 | 4928 | error ("could not convert template argument %qE to %qT", orig_arg, t); |
8b5b8b7c MM |
4929 | } |
4930 | ||
4931 | return val; | |
4932 | } | |
4933 | ||
b1d7b1c0 DG |
4934 | /* Coerces the remaining template arguments in INNER_ARGS (from |
4935 | ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS. | |
4936 | Returns the coerced argument pack. PARM_IDX is the position of this | |
4937 | parameter in the template parameter list. ARGS is the original | |
4938 | template argument list. */ | |
4939 | static tree | |
4940 | coerce_template_parameter_pack (tree parms, | |
4941 | int parm_idx, | |
4942 | tree args, | |
4943 | tree inner_args, | |
4944 | int arg_idx, | |
4945 | tree new_args, | |
4946 | int* lost, | |
4947 | tree in_decl, | |
4948 | tsubst_flags_t complain) | |
4949 | { | |
4950 | tree parm = TREE_VEC_ELT (parms, parm_idx); | |
4951 | int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0; | |
4952 | tree packed_args; | |
4953 | tree argument_pack; | |
4954 | tree packed_types = NULL_TREE; | |
4955 | ||
4956 | if (arg_idx > nargs) | |
4957 | arg_idx = nargs; | |
4958 | ||
4959 | packed_args = make_tree_vec (nargs - arg_idx); | |
4960 | ||
4961 | if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL | |
4962 | && uses_parameter_packs (TREE_TYPE (TREE_VALUE (parm)))) | |
4963 | { | |
4964 | /* When the template parameter is a non-type template | |
4965 | parameter pack whose type uses parameter packs, we need | |
4966 | to look at each of the template arguments | |
4967 | separately. Build a vector of the types for these | |
4968 | non-type template parameters in PACKED_TYPES. */ | |
4969 | tree expansion | |
4970 | = make_pack_expansion (TREE_TYPE (TREE_VALUE (parm))); | |
4971 | packed_types = tsubst_pack_expansion (expansion, args, | |
4972 | complain, in_decl); | |
4973 | ||
4974 | if (packed_types == error_mark_node) | |
4975 | return error_mark_node; | |
4976 | ||
4977 | /* Check that we have the right number of arguments. */ | |
4978 | if (arg_idx < nargs | |
4979 | && !PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)) | |
4980 | && nargs - arg_idx != TREE_VEC_LENGTH (packed_types)) | |
4981 | { | |
4982 | int needed_parms | |
4983 | = TREE_VEC_LENGTH (parms) - 1 + TREE_VEC_LENGTH (packed_types); | |
4984 | error ("wrong number of template arguments (%d, should be %d)", | |
4985 | nargs, needed_parms); | |
4986 | return error_mark_node; | |
4987 | } | |
4988 | ||
4989 | /* If we aren't able to check the actual arguments now | |
4990 | (because they haven't been expanded yet), we can at least | |
4991 | verify that all of the types used for the non-type | |
4992 | template parameter pack are, in fact, valid for non-type | |
4993 | template parameters. */ | |
4994 | if (arg_idx < nargs | |
4995 | && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx))) | |
4996 | { | |
4997 | int j, len = TREE_VEC_LENGTH (packed_types); | |
4998 | for (j = 0; j < len; ++j) | |
4999 | { | |
5000 | tree t = TREE_VEC_ELT (packed_types, j); | |
5001 | if (invalid_nontype_parm_type_p (t, complain)) | |
5002 | return error_mark_node; | |
5003 | } | |
5004 | } | |
5005 | } | |
5006 | ||
5007 | /* Convert the remaining arguments, which will be a part of the | |
5008 | parameter pack "parm". */ | |
5009 | for (; arg_idx < nargs; ++arg_idx) | |
5010 | { | |
5011 | tree arg = TREE_VEC_ELT (inner_args, arg_idx); | |
5012 | tree actual_parm = TREE_VALUE (parm); | |
5013 | ||
5014 | if (packed_types && !PACK_EXPANSION_P (arg)) | |
5015 | { | |
5016 | /* When we have a vector of types (corresponding to the | |
5017 | non-type template parameter pack that uses parameter | |
5018 | packs in its type, as mention above), and the | |
5019 | argument is not an expansion (which expands to a | |
5020 | currently unknown number of arguments), clone the | |
5021 | parm and give it the next type in PACKED_TYPES. */ | |
5022 | actual_parm = copy_node (actual_parm); | |
5023 | TREE_TYPE (actual_parm) = | |
5024 | TREE_VEC_ELT (packed_types, arg_idx - parm_idx); | |
5025 | } | |
5026 | ||
c343d5a7 PC |
5027 | if (arg != error_mark_node) |
5028 | arg = convert_template_argument (actual_parm, | |
5029 | arg, new_args, complain, parm_idx, | |
5030 | in_decl); | |
b1d7b1c0 DG |
5031 | if (arg == error_mark_node) |
5032 | (*lost)++; | |
5033 | TREE_VEC_ELT (packed_args, arg_idx - parm_idx) = arg; | |
5034 | } | |
5035 | ||
5036 | if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL | |
5037 | || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL) | |
5038 | argument_pack = make_node (TYPE_ARGUMENT_PACK); | |
5039 | else | |
5040 | { | |
5041 | argument_pack = make_node (NONTYPE_ARGUMENT_PACK); | |
88b82314 DG |
5042 | TREE_TYPE (argument_pack) |
5043 | = tsubst (TREE_TYPE (TREE_VALUE (parm)), args, complain, in_decl); | |
b1d7b1c0 DG |
5044 | TREE_CONSTANT (argument_pack) = 1; |
5045 | } | |
5046 | ||
5047 | SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args); | |
5048 | return argument_pack; | |
5049 | } | |
5050 | ||
8b5b8b7c MM |
5051 | /* Convert all template arguments to their appropriate types, and |
5052 | return a vector containing the innermost resulting template | |
c2ea3a40 | 5053 | arguments. If any error occurs, return error_mark_node. Error and |
3e4a3562 | 5054 | warning messages are issued under control of COMPLAIN. |
75650646 | 5055 | |
e7e93965 | 5056 | If REQUIRE_ALL_ARGS is false, argument deduction will be performed |
84dc00e8 | 5057 | for arguments not specified in ARGS. Otherwise, if |
e7e93965 MM |
5058 | USE_DEFAULT_ARGS is true, default arguments will be used to fill in |
5059 | unspecified arguments. If REQUIRE_ALL_ARGS is true, but | |
5060 | USE_DEFAULT_ARGS is false, then all arguments must be specified in | |
5061 | ARGS. */ | |
c8094d83 | 5062 | |
8d08fdba | 5063 | static tree |
c8094d83 | 5064 | coerce_template_parms (tree parms, |
0cbd7506 MS |
5065 | tree args, |
5066 | tree in_decl, | |
3a978d72 | 5067 | tsubst_flags_t complain, |
e7e93965 MM |
5068 | bool require_all_args, |
5069 | bool use_default_args) | |
8d08fdba | 5070 | { |
b1d7b1c0 | 5071 | int nparms, nargs, parm_idx, arg_idx, lost = 0; |
e4a84209 | 5072 | tree inner_args; |
8b5b8b7c MM |
5073 | tree new_args; |
5074 | tree new_inner_args; | |
a2c5ed87 | 5075 | bool saved_skip_evaluation; |
a292b002 | 5076 | |
5d80a306 DG |
5077 | /* When used as a boolean value, indicates whether this is a |
5078 | variadic template parameter list. Since it's an int, we can also | |
5079 | subtract it from nparms to get the number of non-variadic | |
5080 | parameters. */ | |
b1d7b1c0 | 5081 | int variadic_p = 0; |
5d80a306 DG |
5082 | |
5083 | inner_args | |
5084 | = expand_template_argument_pack (INNERMOST_TEMPLATE_ARGS (args)); | |
5085 | ||
bf12d54d | 5086 | nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0; |
a292b002 MS |
5087 | nparms = TREE_VEC_LENGTH (parms); |
5088 | ||
b1d7b1c0 DG |
5089 | /* Determine if there are any parameter packs. */ |
5090 | for (parm_idx = 0; parm_idx < nparms; ++parm_idx) | |
5091 | { | |
5092 | tree tparm = TREE_VALUE (TREE_VEC_ELT (parms, parm_idx)); | |
5093 | if (template_parameter_pack_p (tparm)) | |
5094 | { | |
5095 | variadic_p = 1; | |
5096 | break; | |
5097 | } | |
5098 | } | |
5099 | ||
5d80a306 DG |
5100 | if ((nargs > nparms - variadic_p && !variadic_p) |
5101 | || (nargs < nparms - variadic_p | |
e7e93965 MM |
5102 | && require_all_args |
5103 | && (!use_default_args | |
42b304f1 LM |
5104 | || (TREE_VEC_ELT (parms, nargs) != error_mark_node |
5105 | && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs)))))) | |
8d08fdba | 5106 | { |
c8094d83 | 5107 | if (complain & tf_error) |
75650646 | 5108 | { |
5d80a306 DG |
5109 | const char *or_more = ""; |
5110 | if (variadic_p) | |
5111 | { | |
5112 | or_more = " or more"; | |
5113 | --nparms; | |
5114 | } | |
5115 | ||
5116 | error ("wrong number of template arguments (%d, should be %d%s)", | |
5117 | nargs, nparms, or_more); | |
c8094d83 | 5118 | |
75650646 | 5119 | if (in_decl) |
dee15844 | 5120 | error ("provided for %q+D", in_decl); |
75650646 MM |
5121 | } |
5122 | ||
8d08fdba MS |
5123 | return error_mark_node; |
5124 | } | |
5125 | ||
a2c5ed87 MM |
5126 | /* We need to evaluate the template arguments, even though this |
5127 | template-id may be nested within a "sizeof". */ | |
5128 | saved_skip_evaluation = skip_evaluation; | |
5129 | skip_evaluation = false; | |
f31c0a32 | 5130 | new_inner_args = make_tree_vec (nparms); |
8b5b8b7c | 5131 | new_args = add_outermost_template_args (args, new_inner_args); |
b1d7b1c0 | 5132 | for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++) |
8d08fdba | 5133 | { |
8b5b8b7c MM |
5134 | tree arg; |
5135 | tree parm; | |
e4a84209 | 5136 | |
8b5b8b7c | 5137 | /* Get the Ith template parameter. */ |
b1d7b1c0 | 5138 | parm = TREE_VEC_ELT (parms, parm_idx); |
42b304f1 LM |
5139 | |
5140 | if (parm == error_mark_node) | |
2d8ba2c7 | 5141 | { |
b1d7b1c0 | 5142 | TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node; |
42b304f1 | 5143 | continue; |
2d8ba2c7 | 5144 | } |
75650646 | 5145 | |
b1d7b1c0 DG |
5146 | /* Calculate the next argument. */ |
5147 | if (template_parameter_pack_p (TREE_VALUE (parm))) | |
5d80a306 | 5148 | { |
b1d7b1c0 DG |
5149 | /* All remaining arguments will be placed in the |
5150 | template parameter pack PARM. */ | |
5151 | arg = coerce_template_parameter_pack (parms, parm_idx, args, | |
5152 | inner_args, arg_idx, | |
5153 | new_args, &lost, | |
5154 | in_decl, complain); | |
5155 | ||
5156 | /* Store this argument. */ | |
5157 | if (arg == error_mark_node) | |
5158 | lost++; | |
5159 | TREE_VEC_ELT (new_inner_args, parm_idx) = arg; | |
5160 | ||
5161 | /* We are done with all of the arguments. */ | |
5162 | arg_idx = nargs; | |
5163 | ||
5164 | continue; | |
5165 | } | |
5166 | else if (arg_idx < nargs) | |
5167 | { | |
5168 | arg = TREE_VEC_ELT (inner_args, arg_idx); | |
5169 | ||
5170 | if (arg && PACK_EXPANSION_P (arg)) | |
5d80a306 | 5171 | { |
b1d7b1c0 DG |
5172 | /* If ARG is a pack expansion, but PARM is not a |
5173 | template parameter pack (if it were, we would have | |
5174 | handled it above), we're trying to expand into a | |
5d80a306 | 5175 | fixed-length argument list. */ |
b1d7b1c0 DG |
5176 | if (TREE_CODE (arg) == EXPR_PACK_EXPANSION) |
5177 | error ("cannot expand %<%E%> into a fixed-length " | |
5178 | "argument list", arg); | |
5179 | else | |
5180 | error ("cannot expand %<%T%> into a fixed-length " | |
5181 | "argument list", arg); | |
c343d5a7 | 5182 | return error_mark_node; |
5d80a306 DG |
5183 | } |
5184 | } | |
e7e93965 | 5185 | else if (require_all_args) |
b1d7b1c0 DG |
5186 | /* There must be a default arg in this case. */ |
5187 | arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args, | |
5188 | complain, in_decl); | |
ffd49b19 NS |
5189 | else |
5190 | break; | |
c8094d83 | 5191 | |
ffd49b19 | 5192 | if (arg == error_mark_node) |
e34b0922 KL |
5193 | { |
5194 | if (complain & tf_error) | |
b1d7b1c0 | 5195 | error ("template argument %d is invalid", arg_idx + 1); |
e34b0922 | 5196 | } |
b1d7b1c0 DG |
5197 | else if (!arg) |
5198 | /* This only occurs if there was an error in the template | |
5199 | parameter list itself (which we would already have | |
5200 | reported) that we are trying to recover from, e.g., a class | |
5201 | template with a parameter list such as | |
5202 | template<typename..., typename>. */ | |
5203 | return error_mark_node; | |
c8094d83 MS |
5204 | else |
5205 | arg = convert_template_argument (TREE_VALUE (parm), | |
b1d7b1c0 DG |
5206 | arg, new_args, complain, |
5207 | parm_idx, in_decl); | |
c8094d83 | 5208 | |
8b5b8b7c | 5209 | if (arg == error_mark_node) |
8d08fdba | 5210 | lost++; |
b1d7b1c0 | 5211 | TREE_VEC_ELT (new_inner_args, arg_idx) = arg; |
8d08fdba | 5212 | } |
a2c5ed87 | 5213 | skip_evaluation = saved_skip_evaluation; |
8b5b8b7c | 5214 | |
8d08fdba MS |
5215 | if (lost) |
5216 | return error_mark_node; | |
8b5b8b7c MM |
5217 | |
5218 | return new_inner_args; | |
8d08fdba MS |
5219 | } |
5220 | ||
34016c81 JM |
5221 | /* Returns 1 if template args OT and NT are equivalent. */ |
5222 | ||
d8e178a0 | 5223 | static int |
3a978d72 | 5224 | template_args_equal (tree ot, tree nt) |
34016c81 JM |
5225 | { |
5226 | if (nt == ot) | |
5227 | return 1; | |
74601d7c | 5228 | |
34016c81 JM |
5229 | if (TREE_CODE (nt) == TREE_VEC) |
5230 | /* For member templates */ | |
74601d7c | 5231 | return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt); |
5d80a306 DG |
5232 | else if (PACK_EXPANSION_P (ot)) |
5233 | return PACK_EXPANSION_P (nt) | |
5234 | && template_args_equal (PACK_EXPANSION_PATTERN (ot), | |
5235 | PACK_EXPANSION_PATTERN (nt)); | |
74601d7c | 5236 | else if (TYPE_P (nt)) |
660845bf | 5237 | return TYPE_P (ot) && same_type_p (ot, nt); |
74601d7c KL |
5238 | else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot)) |
5239 | return 0; | |
34016c81 | 5240 | else |
c8a209ca | 5241 | return cp_tree_equal (ot, nt); |
34016c81 JM |
5242 | } |
5243 | ||
5244 | /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets | |
f84b4be9 JM |
5245 | of template arguments. Returns 0 otherwise. */ |
5246 | ||
6757edfe | 5247 | int |
3a978d72 | 5248 | comp_template_args (tree oldargs, tree newargs) |
5566b478 MS |
5249 | { |
5250 | int i; | |
5251 | ||
5d80a306 DG |
5252 | oldargs = expand_template_argument_pack (oldargs); |
5253 | newargs = expand_template_argument_pack (newargs); | |
5254 | ||
386b8a85 JM |
5255 | if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs)) |
5256 | return 0; | |
5257 | ||
5566b478 MS |
5258 | for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i) |
5259 | { | |
5260 | tree nt = TREE_VEC_ELT (newargs, i); | |
5261 | tree ot = TREE_VEC_ELT (oldargs, i); | |
5262 | ||
34016c81 | 5263 | if (! template_args_equal (ot, nt)) |
61a127b3 | 5264 | return 0; |
5566b478 MS |
5265 | } |
5266 | return 1; | |
5267 | } | |
5268 | ||
5566b478 | 5269 | static void |
3a978d72 | 5270 | add_pending_template (tree d) |
5566b478 | 5271 | { |
3ae18eaf JM |
5272 | tree ti = (TYPE_P (d) |
5273 | ? CLASSTYPE_TEMPLATE_INFO (d) | |
5274 | : DECL_TEMPLATE_INFO (d)); | |
e2c3721c | 5275 | struct pending_template *pt; |
3ae18eaf | 5276 | int level; |
e92cc029 | 5277 | |
824b9a4c | 5278 | if (TI_PENDING_TEMPLATE_FLAG (ti)) |
5566b478 MS |
5279 | return; |
5280 | ||
3ae18eaf JM |
5281 | /* We are called both from instantiate_decl, where we've already had a |
5282 | tinst_level pushed, and instantiate_template, where we haven't. | |
5283 | Compensate. */ | |
e2c3721c | 5284 | level = !current_tinst_level || current_tinst_level->decl != d; |
3ae18eaf JM |
5285 | |
5286 | if (level) | |
5287 | push_tinst_level (d); | |
5288 | ||
e2c3721c PB |
5289 | pt = GGC_NEW (struct pending_template); |
5290 | pt->next = NULL; | |
5291 | pt->tinst = current_tinst_level; | |
46ccf50a | 5292 | if (last_pending_template) |
e2c3721c | 5293 | last_pending_template->next = pt; |
46ccf50a JM |
5294 | else |
5295 | pending_templates = pt; | |
5296 | ||
5297 | last_pending_template = pt; | |
5298 | ||
824b9a4c | 5299 | TI_PENDING_TEMPLATE_FLAG (ti) = 1; |
3ae18eaf JM |
5300 | |
5301 | if (level) | |
5302 | pop_tinst_level (); | |
5566b478 MS |
5303 | } |
5304 | ||
386b8a85 | 5305 | |
4ba126e4 MM |
5306 | /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and |
5307 | ARGLIST. Valid choices for FNS are given in the cp-tree.def | |
5308 | documentation for TEMPLATE_ID_EXPR. */ | |
386b8a85 JM |
5309 | |
5310 | tree | |
3a978d72 | 5311 | lookup_template_function (tree fns, tree arglist) |
386b8a85 | 5312 | { |
2c73f9f5 | 5313 | tree type; |
050367a3 | 5314 | |
4ba126e4 MM |
5315 | if (fns == error_mark_node || arglist == error_mark_node) |
5316 | return error_mark_node; | |
5317 | ||
50bc768d | 5318 | gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC); |
4230cec2 NS |
5319 | gcc_assert (fns && (is_overloaded_fn (fns) |
5320 | || TREE_CODE (fns) == IDENTIFIER_NODE)); | |
4ba126e4 | 5321 | |
50ad9642 MM |
5322 | if (BASELINK_P (fns)) |
5323 | { | |
f293ce4b RS |
5324 | BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR, |
5325 | unknown_type_node, | |
5326 | BASELINK_FUNCTIONS (fns), | |
5327 | arglist); | |
50ad9642 MM |
5328 | return fns; |
5329 | } | |
5330 | ||
2c73f9f5 ML |
5331 | type = TREE_TYPE (fns); |
5332 | if (TREE_CODE (fns) == OVERLOAD || !type) | |
5333 | type = unknown_type_node; | |
c8094d83 | 5334 | |
f293ce4b | 5335 | return build2 (TEMPLATE_ID_EXPR, type, fns, arglist); |
386b8a85 JM |
5336 | } |
5337 | ||
a2b60a0e MM |
5338 | /* Within the scope of a template class S<T>, the name S gets bound |
5339 | (in build_self_reference) to a TYPE_DECL for the class, not a | |
5340 | TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type, | |
5341 | or one of its enclosing classes, and that type is a template, | |
5342 | return the associated TEMPLATE_DECL. Otherwise, the original | |
5343 | DECL is returned. */ | |
5344 | ||
a723baf1 | 5345 | tree |
3a978d72 | 5346 | maybe_get_template_decl_from_type_decl (tree decl) |
a2b60a0e MM |
5347 | { |
5348 | return (decl != NULL_TREE | |
c8094d83 | 5349 | && TREE_CODE (decl) == TYPE_DECL |
a2b60a0e | 5350 | && DECL_ARTIFICIAL (decl) |
511b60ff | 5351 | && CLASS_TYPE_P (TREE_TYPE (decl)) |
c8094d83 | 5352 | && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl))) |
a2b60a0e MM |
5353 | ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl; |
5354 | } | |
386b8a85 | 5355 | |
8d08fdba MS |
5356 | /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of |
5357 | parameters, find the desired type. | |
5358 | ||
5359 | D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments. | |
8d08fdba MS |
5360 | |
5361 | IN_DECL, if non-NULL, is the template declaration we are trying to | |
c8094d83 | 5362 | instantiate. |
75650646 | 5363 | |
838dfd8a | 5364 | If ENTERING_SCOPE is nonzero, we are about to enter the scope of |
36a117a5 | 5365 | the class we are looking up. |
c8094d83 | 5366 | |
c2ea3a40 | 5367 | Issue error and warning messages under control of COMPLAIN. |
36a117a5 | 5368 | |
75650646 MM |
5369 | If the template class is really a local class in a template |
5370 | function, then the FUNCTION_CONTEXT is the function in which it is | |
c8094d83 | 5371 | being instantiated. |
b6ab6892 GB |
5372 | |
5373 | ??? Note that this function is currently called *twice* for each | |
5374 | template-id: the first time from the parser, while creating the | |
5375 | incomplete type (finish_template_type), and the second type during the | |
5376 | real instantiation (instantiate_template_class). This is surely something | |
5377 | that we want to avoid. It also causes some problems with argument | |
5378 | coercion (see convert_nontype_argument for more information on this). */ | |
e92cc029 | 5379 | |
8d08fdba | 5380 | tree |
c8094d83 | 5381 | lookup_template_class (tree d1, |
0cbd7506 MS |
5382 | tree arglist, |
5383 | tree in_decl, | |
5384 | tree context, | |
5385 | int entering_scope, | |
5386 | tsubst_flags_t complain) | |
8d08fdba | 5387 | { |
a703fb38 | 5388 | tree template = NULL_TREE, parmlist; |
dbfe2124 | 5389 | tree t; |
c8094d83 | 5390 | |
fd295cb2 | 5391 | timevar_push (TV_NAME_LOOKUP); |
c8094d83 | 5392 | |
5566b478 MS |
5393 | if (TREE_CODE (d1) == IDENTIFIER_NODE) |
5394 | { | |
90ea9897 MM |
5395 | tree value = innermost_non_namespace_value (d1); |
5396 | if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value)) | |
5397 | template = value; | |
73b0fce8 KL |
5398 | else |
5399 | { | |
2c73f9f5 ML |
5400 | if (context) |
5401 | push_decl_namespace (context); | |
10e6657a | 5402 | template = lookup_name (d1); |
3ebc5c52 | 5403 | template = maybe_get_template_decl_from_type_decl (template); |
2c73f9f5 ML |
5404 | if (context) |
5405 | pop_decl_namespace (); | |
73b0fce8 | 5406 | } |
8d019cef JM |
5407 | if (template) |
5408 | context = DECL_CONTEXT (template); | |
5566b478 | 5409 | } |
c91a56d2 MS |
5410 | else if (TREE_CODE (d1) == TYPE_DECL && IS_AGGR_TYPE (TREE_TYPE (d1))) |
5411 | { | |
802dbc34 JM |
5412 | tree type = TREE_TYPE (d1); |
5413 | ||
5414 | /* If we are declaring a constructor, say A<T>::A<T>, we will get | |
5415 | an implicit typename for the second A. Deal with it. */ | |
5416 | if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type)) | |
5417 | type = TREE_TYPE (type); | |
c8094d83 | 5418 | |
802dbc34 | 5419 | if (CLASSTYPE_TEMPLATE_INFO (type)) |
f3400fe2 | 5420 | { |
802dbc34 | 5421 | template = CLASSTYPE_TI_TEMPLATE (type); |
f3400fe2 JM |
5422 | d1 = DECL_NAME (template); |
5423 | } | |
c91a56d2 | 5424 | } |
c8094d83 | 5425 | else if (TREE_CODE (d1) == ENUMERAL_TYPE |
2f939d94 | 5426 | || (TYPE_P (d1) && IS_AGGR_TYPE (d1))) |
5566b478 | 5427 | { |
ed44da02 | 5428 | template = TYPE_TI_TEMPLATE (d1); |
5566b478 MS |
5429 | d1 = DECL_NAME (template); |
5430 | } | |
93cdc044 | 5431 | else if (TREE_CODE (d1) == TEMPLATE_DECL |
17aec3eb | 5432 | && TREE_CODE (DECL_TEMPLATE_RESULT (d1)) == TYPE_DECL) |
93cdc044 JM |
5433 | { |
5434 | template = d1; | |
5435 | d1 = DECL_NAME (template); | |
5436 | context = DECL_CONTEXT (template); | |
5437 | } | |
8d08fdba | 5438 | |
90ea9897 | 5439 | /* Issue an error message if we didn't find a template. */ |
8d08fdba | 5440 | if (! template) |
f3400fe2 | 5441 | { |
c2ea3a40 | 5442 | if (complain & tf_error) |
0cbd7506 | 5443 | error ("%qT is not a template", d1); |
fd295cb2 | 5444 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node); |
f3400fe2 | 5445 | } |
2c73f9f5 | 5446 | |
a87b4257 | 5447 | if (TREE_CODE (template) != TEMPLATE_DECL |
0cbd7506 | 5448 | /* Make sure it's a user visible template, if it was named by |
42eaed49 NS |
5449 | the user. */ |
5450 | || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (template) | |
5451 | && !PRIMARY_TEMPLATE_P (template))) | |
8d08fdba | 5452 | { |
c2ea3a40 | 5453 | if (complain & tf_error) |
0cbd7506 MS |
5454 | { |
5455 | error ("non-template type %qT used as a template", d1); | |
5456 | if (in_decl) | |
dee15844 | 5457 | error ("for template declaration %q+D", in_decl); |
f9c244b8 | 5458 | } |
fd295cb2 | 5459 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node); |
8d08fdba | 5460 | } |
8d08fdba | 5461 | |
42eaed49 | 5462 | complain &= ~tf_user; |
c8094d83 | 5463 | |
73b0fce8 KL |
5464 | if (DECL_TEMPLATE_TEMPLATE_PARM_P (template)) |
5465 | { | |
5466 | /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store | |
0cbd7506 | 5467 | template arguments */ |
73b0fce8 | 5468 | |
1899c3a4 | 5469 | tree parm; |
73b0fce8 | 5470 | tree arglist2; |
ef3b7b17 | 5471 | tree outer; |
73b0fce8 | 5472 | |
73b0fce8 KL |
5473 | parmlist = DECL_INNERMOST_TEMPLATE_PARMS (template); |
5474 | ||
4f96ff63 KL |
5475 | /* Consider an example where a template template parameter declared as |
5476 | ||
5477 | template <class T, class U = std::allocator<T> > class TT | |
5478 | ||
c8094d83 MS |
5479 | The template parameter level of T and U are one level larger than |
5480 | of TT. To proper process the default argument of U, say when an | |
4f96ff63 | 5481 | instantiation `TT<int>' is seen, we need to build the full |
342cea95 KL |
5482 | arguments containing {int} as the innermost level. Outer levels, |
5483 | available when not appearing as default template argument, can be | |
ef3b7b17 | 5484 | obtained from the arguments of the enclosing template. |
4f96ff63 | 5485 | |
342cea95 KL |
5486 | Suppose that TT is later substituted with std::vector. The above |
5487 | instantiation is `TT<int, std::allocator<T> >' with TT at | |
5488 | level 1, and T at level 2, while the template arguments at level 1 | |
5489 | becomes {std::vector} and the inner level 2 is {int}. */ | |
5490 | ||
ef3b7b17 JM |
5491 | outer = DECL_CONTEXT (template); |
5492 | if (outer) | |
5493 | outer = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer))); | |
5494 | else if (current_template_parms) | |
5495 | /* This is an argument of the current template, so we haven't set | |
5496 | DECL_CONTEXT yet. */ | |
5497 | outer = current_template_args (); | |
5498 | ||
5499 | if (outer) | |
5500 | arglist = add_to_template_args (outer, arglist); | |
4f96ff63 | 5501 | |
f9c244b8 | 5502 | arglist2 = coerce_template_parms (parmlist, arglist, template, |
3db45ab5 | 5503 | complain, |
e7e93965 MM |
5504 | /*require_all_args=*/true, |
5505 | /*use_default_args=*/true); | |
3e4a3562 | 5506 | if (arglist2 == error_mark_node |
544aef8c | 5507 | || (!uses_template_parms (arglist2) |
3e4a3562 | 5508 | && check_instantiated_args (template, arglist2, complain))) |
0cbd7506 | 5509 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node); |
73b0fce8 | 5510 | |
dac65501 | 5511 | parm = bind_template_template_parm (TREE_TYPE (template), arglist2); |
fd295cb2 | 5512 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, parm); |
73b0fce8 | 5513 | } |
c8094d83 | 5514 | else |
8d08fdba | 5515 | { |
36a117a5 | 5516 | tree template_type = TREE_TYPE (template); |
7ac7b28f | 5517 | tree gen_tmpl; |
36a117a5 MM |
5518 | tree type_decl; |
5519 | tree found = NULL_TREE; | |
5520 | int arg_depth; | |
5521 | int parm_depth; | |
dbfe2124 | 5522 | int is_partial_instantiation; |
830bfa74 | 5523 | |
7ac7b28f MM |
5524 | gen_tmpl = most_general_template (template); |
5525 | parmlist = DECL_TEMPLATE_PARMS (gen_tmpl); | |
36a117a5 MM |
5526 | parm_depth = TMPL_PARMS_DEPTH (parmlist); |
5527 | arg_depth = TMPL_ARGS_DEPTH (arglist); | |
5528 | ||
5529 | if (arg_depth == 1 && parm_depth > 1) | |
5530 | { | |
39c01e4c | 5531 | /* We've been given an incomplete set of template arguments. |
36a117a5 MM |
5532 | For example, given: |
5533 | ||
5534 | template <class T> struct S1 { | |
0cbd7506 | 5535 | template <class U> struct S2 {}; |
36a117a5 | 5536 | template <class U> struct S2<U*> {}; |
0cbd7506 | 5537 | }; |
c8094d83 | 5538 | |
36a117a5 MM |
5539 | we will be called with an ARGLIST of `U*', but the |
5540 | TEMPLATE will be `template <class T> template | |
5541 | <class U> struct S1<T>::S2'. We must fill in the missing | |
5542 | arguments. */ | |
c8094d83 | 5543 | arglist |
7ac7b28f MM |
5544 | = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (template)), |
5545 | arglist); | |
36a117a5 MM |
5546 | arg_depth = TMPL_ARGS_DEPTH (arglist); |
5547 | } | |
5566b478 | 5548 | |
41f5d4b1 | 5549 | /* Now we should have enough arguments. */ |
50bc768d | 5550 | gcc_assert (parm_depth == arg_depth); |
c8094d83 | 5551 | |
7ac7b28f MM |
5552 | /* From here on, we're only interested in the most general |
5553 | template. */ | |
5554 | template = gen_tmpl; | |
5555 | ||
36a117a5 MM |
5556 | /* Calculate the BOUND_ARGS. These will be the args that are |
5557 | actually tsubst'd into the definition to create the | |
5558 | instantiation. */ | |
5559 | if (parm_depth > 1) | |
830bfa74 MM |
5560 | { |
5561 | /* We have multiple levels of arguments to coerce, at once. */ | |
830bfa74 | 5562 | int i; |
e4a84209 | 5563 | int saved_depth = TMPL_ARGS_DEPTH (arglist); |
36a117a5 | 5564 | |
f31c0a32 | 5565 | tree bound_args = make_tree_vec (parm_depth); |
c8094d83 | 5566 | |
e4a84209 | 5567 | for (i = saved_depth, |
c8094d83 | 5568 | t = DECL_TEMPLATE_PARMS (template); |
e4a84209 | 5569 | i > 0 && t != NULL_TREE; |
830bfa74 | 5570 | --i, t = TREE_CHAIN (t)) |
e4a84209 | 5571 | { |
ee3071ef NS |
5572 | tree a = coerce_template_parms (TREE_VALUE (t), |
5573 | arglist, template, | |
3db45ab5 | 5574 | complain, |
e7e93965 MM |
5575 | /*require_all_args=*/true, |
5576 | /*use_default_args=*/true); | |
88e98cfe KL |
5577 | |
5578 | /* Don't process further if one of the levels fails. */ | |
5579 | if (a == error_mark_node) | |
5580 | { | |
5581 | /* Restore the ARGLIST to its full size. */ | |
5582 | TREE_VEC_LENGTH (arglist) = saved_depth; | |
5583 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node); | |
5584 | } | |
c8094d83 | 5585 | |
e4a84209 MM |
5586 | SET_TMPL_ARGS_LEVEL (bound_args, i, a); |
5587 | ||
5588 | /* We temporarily reduce the length of the ARGLIST so | |
5589 | that coerce_template_parms will see only the arguments | |
5590 | corresponding to the template parameters it is | |
5591 | examining. */ | |
5592 | TREE_VEC_LENGTH (arglist)--; | |
5593 | } | |
5594 | ||
5595 | /* Restore the ARGLIST to its full size. */ | |
5596 | TREE_VEC_LENGTH (arglist) = saved_depth; | |
5597 | ||
36a117a5 | 5598 | arglist = bound_args; |
830bfa74 MM |
5599 | } |
5600 | else | |
36a117a5 MM |
5601 | arglist |
5602 | = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist), | |
f9a7ae04 | 5603 | INNERMOST_TEMPLATE_ARGS (arglist), |
f9c244b8 | 5604 | template, |
3db45ab5 | 5605 | complain, |
e7e93965 MM |
5606 | /*require_all_args=*/true, |
5607 | /*use_default_args=*/true); | |
36a117a5 | 5608 | |
c353b8e3 | 5609 | if (arglist == error_mark_node) |
36a117a5 | 5610 | /* We were unable to bind the arguments. */ |
fd295cb2 | 5611 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node); |
5566b478 | 5612 | |
36a117a5 MM |
5613 | /* In the scope of a template class, explicit references to the |
5614 | template class refer to the type of the template, not any | |
5615 | instantiation of it. For example, in: | |
c8094d83 | 5616 | |
36a117a5 MM |
5617 | template <class T> class C { void f(C<T>); } |
5618 | ||
5619 | the `C<T>' is just the same as `C'. Outside of the | |
5620 | class, however, such a reference is an instantiation. */ | |
ed44da02 | 5621 | if (comp_template_args (TYPE_TI_ARGS (template_type), |
36a117a5 MM |
5622 | arglist)) |
5623 | { | |
5624 | found = template_type; | |
c8094d83 | 5625 | |
36a117a5 | 5626 | if (!entering_scope && PRIMARY_TEMPLATE_P (template)) |
5566b478 | 5627 | { |
36a117a5 | 5628 | tree ctx; |
c8094d83 MS |
5629 | |
5630 | for (ctx = current_class_type; | |
5f04800c KL |
5631 | ctx && TREE_CODE (ctx) != NAMESPACE_DECL; |
5632 | ctx = (TYPE_P (ctx) | |
5633 | ? TYPE_CONTEXT (ctx) | |
5634 | : DECL_CONTEXT (ctx))) | |
5635 | if (TYPE_P (ctx) && same_type_p (ctx, template_type)) | |
5636 | goto found_ctx; | |
c8094d83 | 5637 | |
6df5158a NS |
5638 | /* We're not in the scope of the class, so the |
5639 | TEMPLATE_TYPE is not the type we want after all. */ | |
5640 | found = NULL_TREE; | |
5641 | found_ctx:; | |
5566b478 MS |
5642 | } |
5643 | } | |
36a117a5 | 5644 | if (found) |
0cbd7506 | 5645 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, found); |
414ea4aa | 5646 | |
c7222c02 MM |
5647 | /* If we already have this specialization, return it. */ |
5648 | found = retrieve_specialization (template, arglist, | |
5649 | /*class_specializations_p=*/false); | |
5650 | if (found) | |
5651 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, found); | |
5566b478 | 5652 | |
dbfe2124 | 5653 | /* This type is a "partial instantiation" if any of the template |
ab097535 | 5654 | arguments still involve template parameters. Note that we set |
486e4077 MM |
5655 | IS_PARTIAL_INSTANTIATION for partial specializations as |
5656 | well. */ | |
dbfe2124 MM |
5657 | is_partial_instantiation = uses_template_parms (arglist); |
5658 | ||
c353b8e3 MM |
5659 | /* If the deduced arguments are invalid, then the binding |
5660 | failed. */ | |
5661 | if (!is_partial_instantiation | |
5662 | && check_instantiated_args (template, | |
5663 | INNERMOST_TEMPLATE_ARGS (arglist), | |
5664 | complain)) | |
5665 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node); | |
c8094d83 MS |
5666 | |
5667 | if (!is_partial_instantiation | |
3ebc5c52 MM |
5668 | && !PRIMARY_TEMPLATE_P (template) |
5669 | && TREE_CODE (CP_DECL_CONTEXT (template)) == NAMESPACE_DECL) | |
5670 | { | |
3ebc5c52 MM |
5671 | found = xref_tag_from_type (TREE_TYPE (template), |
5672 | DECL_NAME (template), | |
29ef83de | 5673 | /*tag_scope=*/ts_global); |
fd295cb2 | 5674 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, found); |
3ebc5c52 | 5675 | } |
c8094d83 | 5676 | |
95ee998c | 5677 | context = tsubst (DECL_CONTEXT (template), arglist, |
c2ea3a40 | 5678 | complain, in_decl); |
95ee998c MM |
5679 | if (!context) |
5680 | context = global_namespace; | |
5681 | ||
36a117a5 | 5682 | /* Create the type. */ |
ed44da02 MM |
5683 | if (TREE_CODE (template_type) == ENUMERAL_TYPE) |
5684 | { | |
dbfe2124 | 5685 | if (!is_partial_instantiation) |
92777ce4 NS |
5686 | { |
5687 | set_current_access_from_decl (TYPE_NAME (template_type)); | |
5688 | t = start_enum (TYPE_IDENTIFIER (template_type)); | |
5689 | } | |
ed44da02 | 5690 | else |
dbfe2124 | 5691 | /* We don't want to call start_enum for this type, since |
ed44da02 MM |
5692 | the values for the enumeration constants may involve |
5693 | template parameters. And, no one should be interested | |
5694 | in the enumeration constants for such a type. */ | |
5695 | t = make_node (ENUMERAL_TYPE); | |
5696 | } | |
5697 | else | |
5698 | { | |
33848bb0 | 5699 | t = make_aggr_type (TREE_CODE (template_type)); |
c8094d83 | 5700 | CLASSTYPE_DECLARED_CLASS (t) |
ed44da02 | 5701 | = CLASSTYPE_DECLARED_CLASS (template_type); |
ed44da02 | 5702 | SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t); |
f668f160 | 5703 | TYPE_FOR_JAVA (t) = TYPE_FOR_JAVA (template_type); |
ae673f14 JM |
5704 | |
5705 | /* A local class. Make sure the decl gets registered properly. */ | |
5706 | if (context == current_function_decl) | |
bd3d082e | 5707 | pushtag (DECL_NAME (template), t, /*tag_scope=*/ts_current); |
06d40de8 DG |
5708 | |
5709 | if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist)) | |
5710 | /* This instantiation is another name for the primary | |
5711 | template type. Set the TYPE_CANONICAL field | |
5712 | appropriately. */ | |
5713 | TYPE_CANONICAL (t) = template_type; | |
5714 | else if (any_template_arguments_need_structural_equality_p (arglist)) | |
5715 | /* Some of the template arguments require structural | |
5716 | equality testing, so this template class requires | |
5717 | structural equality testing. */ | |
5718 | SET_TYPE_STRUCTURAL_EQUALITY (t); | |
ed44da02 MM |
5719 | } |
5720 | ||
ae673f14 JM |
5721 | /* If we called start_enum or pushtag above, this information |
5722 | will already be set up. */ | |
ed44da02 MM |
5723 | if (!TYPE_NAME (t)) |
5724 | { | |
5725 | TYPE_CONTEXT (t) = FROB_CONTEXT (context); | |
c8094d83 | 5726 | |
9188c363 | 5727 | type_decl = create_implicit_typedef (DECL_NAME (template), t); |
ed44da02 | 5728 | DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t); |
9188c363 | 5729 | TYPE_STUB_DECL (t) = type_decl; |
c8094d83 | 5730 | DECL_SOURCE_LOCATION (type_decl) |
f31686a3 | 5731 | = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type)); |
ed44da02 MM |
5732 | } |
5733 | else | |
5734 | type_decl = TYPE_NAME (t); | |
36a117a5 | 5735 | |
cab7a9a3 KL |
5736 | TREE_PRIVATE (type_decl) |
5737 | = TREE_PRIVATE (TYPE_STUB_DECL (template_type)); | |
5738 | TREE_PROTECTED (type_decl) | |
5739 | = TREE_PROTECTED (TYPE_STUB_DECL (template_type)); | |
12af7ba3 MM |
5740 | DECL_IN_SYSTEM_HEADER (type_decl) |
5741 | = DECL_IN_SYSTEM_HEADER (template); | |
8d039470 MS |
5742 | if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type)) |
5743 | { | |
5744 | DECL_VISIBILITY_SPECIFIED (type_decl) = 1; | |
5745 | DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type); | |
5746 | } | |
cab7a9a3 | 5747 | |
9fbf56f7 MM |
5748 | /* Set up the template information. We have to figure out which |
5749 | template is the immediate parent if this is a full | |
5750 | instantiation. */ | |
5751 | if (parm_depth == 1 || is_partial_instantiation | |
5752 | || !PRIMARY_TEMPLATE_P (template)) | |
5753 | /* This case is easy; there are no member templates involved. */ | |
5754 | found = template; | |
5755 | else | |
5756 | { | |
ab097535 NS |
5757 | /* This is a full instantiation of a member template. Look |
5758 | for a partial instantiation of which this is an instance. */ | |
9fbf56f7 MM |
5759 | |
5760 | for (found = DECL_TEMPLATE_INSTANTIATIONS (template); | |
5761 | found; found = TREE_CHAIN (found)) | |
5762 | { | |
5763 | int success; | |
5764 | tree tmpl = CLASSTYPE_TI_TEMPLATE (TREE_VALUE (found)); | |
5765 | ||
5766 | /* We only want partial instantiations, here, not | |
5767 | specializations or full instantiations. */ | |
370af2d5 | 5768 | if (CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_VALUE (found)) |
9fbf56f7 MM |
5769 | || !uses_template_parms (TREE_VALUE (found))) |
5770 | continue; | |
5771 | ||
5772 | /* Temporarily reduce by one the number of levels in the | |
5773 | ARGLIST and in FOUND so as to avoid comparing the | |
5774 | last set of arguments. */ | |
5775 | TREE_VEC_LENGTH (arglist)--; | |
5776 | TREE_VEC_LENGTH (TREE_PURPOSE (found)) --; | |
5777 | ||
5778 | /* See if the arguments match. If they do, then TMPL is | |
5779 | the partial instantiation we want. */ | |
5780 | success = comp_template_args (TREE_PURPOSE (found), arglist); | |
5781 | ||
5782 | /* Restore the argument vectors to their full size. */ | |
5783 | TREE_VEC_LENGTH (arglist)++; | |
5784 | TREE_VEC_LENGTH (TREE_PURPOSE (found))++; | |
5785 | ||
5786 | if (success) | |
5787 | { | |
5788 | found = tmpl; | |
5789 | break; | |
5790 | } | |
5791 | } | |
5792 | ||
5793 | if (!found) | |
ab097535 NS |
5794 | { |
5795 | /* There was no partial instantiation. This happens | |
0cbd7506 MS |
5796 | where C<T> is a member template of A<T> and it's used |
5797 | in something like | |
c8094d83 | 5798 | |
0cbd7506 MS |
5799 | template <typename T> struct B { A<T>::C<int> m; }; |
5800 | B<float>; | |
c8094d83 | 5801 | |
0cbd7506 MS |
5802 | Create the partial instantiation. |
5803 | */ | |
5804 | TREE_VEC_LENGTH (arglist)--; | |
5805 | found = tsubst (template, arglist, complain, NULL_TREE); | |
5806 | TREE_VEC_LENGTH (arglist)++; | |
5807 | } | |
9fbf56f7 MM |
5808 | } |
5809 | ||
c8094d83 MS |
5810 | SET_TYPE_TEMPLATE_INFO (t, tree_cons (found, arglist, NULL_TREE)); |
5811 | DECL_TEMPLATE_INSTANTIATIONS (template) | |
5812 | = tree_cons (arglist, t, | |
dbfe2124 MM |
5813 | DECL_TEMPLATE_INSTANTIATIONS (template)); |
5814 | ||
c8094d83 | 5815 | if (TREE_CODE (t) == ENUMERAL_TYPE |
dbfe2124 | 5816 | && !is_partial_instantiation) |
61a127b3 MM |
5817 | /* Now that the type has been registered on the instantiations |
5818 | list, we set up the enumerators. Because the enumeration | |
5819 | constants may involve the enumeration type itself, we make | |
5820 | sure to register the type first, and then create the | |
5821 | constants. That way, doing tsubst_expr for the enumeration | |
5822 | constants won't result in recursive calls here; we'll find | |
5823 | the instantiation and exit above. */ | |
5824 | tsubst_enum (template_type, t, arglist); | |
dbfe2124 | 5825 | |
7813d14c | 5826 | if (is_partial_instantiation) |
077e7015 MM |
5827 | /* If the type makes use of template parameters, the |
5828 | code that generates debugging information will crash. */ | |
5829 | DECL_IGNORED_P (TYPE_STUB_DECL (t)) = 1; | |
8d08fdba | 5830 | |
b9e75696 JM |
5831 | /* Possibly limit visibility based on template args. */ |
5832 | TREE_PUBLIC (type_decl) = 1; | |
5833 | determine_visibility (type_decl); | |
5834 | ||
fd295cb2 | 5835 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t); |
36a117a5 | 5836 | } |
fd295cb2 | 5837 | timevar_pop (TV_NAME_LOOKUP); |
8d08fdba MS |
5838 | } |
5839 | \f | |
c8094d83 | 5840 | struct pair_fn_data |
8d08fdba | 5841 | { |
8dfaeb63 MM |
5842 | tree_fn_t fn; |
5843 | void *data; | |
0c58f841 | 5844 | struct pointer_set_t *visited; |
8dfaeb63 MM |
5845 | }; |
5846 | ||
5847 | /* Called from for_each_template_parm via walk_tree. */ | |
581d38d0 | 5848 | |
8dfaeb63 | 5849 | static tree |
350fae66 | 5850 | for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d) |
8dfaeb63 MM |
5851 | { |
5852 | tree t = *tp; | |
5853 | struct pair_fn_data *pfd = (struct pair_fn_data *) d; | |
5854 | tree_fn_t fn = pfd->fn; | |
5855 | void *data = pfd->data; | |
4f2c9d7e | 5856 | |
2f939d94 | 5857 | if (TYPE_P (t) |
4f2c9d7e | 5858 | && for_each_template_parm (TYPE_CONTEXT (t), fn, data, pfd->visited)) |
8dfaeb63 | 5859 | return error_mark_node; |
581d38d0 | 5860 | |
8d08fdba MS |
5861 | switch (TREE_CODE (t)) |
5862 | { | |
8d08fdba | 5863 | case RECORD_TYPE: |
9076e292 | 5864 | if (TYPE_PTRMEMFUNC_P (t)) |
8dfaeb63 | 5865 | break; |
ed44da02 MM |
5866 | /* Fall through. */ |
5867 | ||
8d08fdba | 5868 | case UNION_TYPE: |
ed44da02 | 5869 | case ENUMERAL_TYPE: |
8dfaeb63 MM |
5870 | if (!TYPE_TEMPLATE_INFO (t)) |
5871 | *walk_subtrees = 0; | |
5872 | else if (for_each_template_parm (TREE_VALUE (TYPE_TEMPLATE_INFO (t)), | |
4f2c9d7e | 5873 | fn, data, pfd->visited)) |
8dfaeb63 MM |
5874 | return error_mark_node; |
5875 | break; | |
5876 | ||
8f6e6bf3 EB |
5877 | case INTEGER_TYPE: |
5878 | if (for_each_template_parm (TYPE_MIN_VALUE (t), | |
5879 | fn, data, pfd->visited) | |
5880 | || for_each_template_parm (TYPE_MAX_VALUE (t), | |
5881 | fn, data, pfd->visited)) | |
5882 | return error_mark_node; | |
5883 | break; | |
5884 | ||
588c2d1c | 5885 | case METHOD_TYPE: |
8dfaeb63 MM |
5886 | /* Since we're not going to walk subtrees, we have to do this |
5887 | explicitly here. */ | |
4f2c9d7e MM |
5888 | if (for_each_template_parm (TYPE_METHOD_BASETYPE (t), fn, data, |
5889 | pfd->visited)) | |
8dfaeb63 | 5890 | return error_mark_node; |
4890c2f4 | 5891 | /* Fall through. */ |
588c2d1c MM |
5892 | |
5893 | case FUNCTION_TYPE: | |
8dfaeb63 | 5894 | /* Check the return type. */ |
4f2c9d7e | 5895 | if (for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited)) |
8dfaeb63 MM |
5896 | return error_mark_node; |
5897 | ||
588c2d1c MM |
5898 | /* Check the parameter types. Since default arguments are not |
5899 | instantiated until they are needed, the TYPE_ARG_TYPES may | |
5900 | contain expressions that involve template parameters. But, | |
5901 | no-one should be looking at them yet. And, once they're | |
5902 | instantiated, they don't contain template parameters, so | |
5903 | there's no point in looking at them then, either. */ | |
5904 | { | |
5905 | tree parm; | |
5906 | ||
5907 | for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm)) | |
4f2c9d7e MM |
5908 | if (for_each_template_parm (TREE_VALUE (parm), fn, data, |
5909 | pfd->visited)) | |
8dfaeb63 | 5910 | return error_mark_node; |
5566b478 | 5911 | |
8dfaeb63 MM |
5912 | /* Since we've already handled the TYPE_ARG_TYPES, we don't |
5913 | want walk_tree walking into them itself. */ | |
5914 | *walk_subtrees = 0; | |
5915 | } | |
5916 | break; | |
3ac3d9ea | 5917 | |
a723baf1 | 5918 | case TYPEOF_TYPE: |
c8094d83 | 5919 | if (for_each_template_parm (TYPE_FIELDS (t), fn, data, |
a723baf1 MM |
5920 | pfd->visited)) |
5921 | return error_mark_node; | |
5922 | break; | |
5923 | ||
8d08fdba | 5924 | case FUNCTION_DECL: |
5566b478 | 5925 | case VAR_DECL: |
5566b478 | 5926 | if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t) |
4f2c9d7e MM |
5927 | && for_each_template_parm (DECL_TI_ARGS (t), fn, data, |
5928 | pfd->visited)) | |
8dfaeb63 MM |
5929 | return error_mark_node; |
5930 | /* Fall through. */ | |
5931 | ||
8d08fdba | 5932 | case PARM_DECL: |
a723baf1 MM |
5933 | case CONST_DECL: |
5934 | if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t) | |
5935 | && for_each_template_parm (DECL_INITIAL (t), fn, data, | |
5936 | pfd->visited)) | |
5937 | return error_mark_node; | |
c8094d83 | 5938 | if (DECL_CONTEXT (t) |
4f2c9d7e MM |
5939 | && for_each_template_parm (DECL_CONTEXT (t), fn, data, |
5940 | pfd->visited)) | |
8dfaeb63 MM |
5941 | return error_mark_node; |
5942 | break; | |
8d08fdba | 5943 | |
a1281f45 | 5944 | case BOUND_TEMPLATE_TEMPLATE_PARM: |
744fac59 | 5945 | /* Record template parameters such as `T' inside `TT<T>'. */ |
4f2c9d7e | 5946 | if (for_each_template_parm (TYPE_TI_ARGS (t), fn, data, pfd->visited)) |
8dfaeb63 MM |
5947 | return error_mark_node; |
5948 | /* Fall through. */ | |
5949 | ||
a1281f45 | 5950 | case TEMPLATE_TEMPLATE_PARM: |
744fac59 | 5951 | case TEMPLATE_TYPE_PARM: |
f84b4be9 | 5952 | case TEMPLATE_PARM_INDEX: |
8dfaeb63 MM |
5953 | if (fn && (*fn)(t, data)) |
5954 | return error_mark_node; | |
5955 | else if (!fn) | |
5956 | return error_mark_node; | |
5957 | break; | |
5156628f | 5958 | |
8dfaeb63 | 5959 | case TEMPLATE_DECL: |
f4f206f4 | 5960 | /* A template template parameter is encountered. */ |
8dfaeb63 | 5961 | if (DECL_TEMPLATE_TEMPLATE_PARM_P (t) |
4f2c9d7e | 5962 | && for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited)) |
8dfaeb63 | 5963 | return error_mark_node; |
db5ae43f | 5964 | |
8dfaeb63 MM |
5965 | /* Already substituted template template parameter */ |
5966 | *walk_subtrees = 0; | |
5967 | break; | |
b8865407 | 5968 | |
4699c561 | 5969 | case TYPENAME_TYPE: |
c8094d83 | 5970 | if (!fn |
4f2c9d7e MM |
5971 | || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t), fn, |
5972 | data, pfd->visited)) | |
8dfaeb63 MM |
5973 | return error_mark_node; |
5974 | break; | |
4699c561 | 5975 | |
8dfaeb63 MM |
5976 | case CONSTRUCTOR: |
5977 | if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t)) | |
5978 | && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE | |
4f2c9d7e MM |
5979 | (TREE_TYPE (t)), fn, data, |
5980 | pfd->visited)) | |
8dfaeb63 MM |
5981 | return error_mark_node; |
5982 | break; | |
c8094d83 | 5983 | |
b8865407 MM |
5984 | case INDIRECT_REF: |
5985 | case COMPONENT_REF: | |
4699c561 | 5986 | /* If there's no type, then this thing must be some expression |
b8865407 | 5987 | involving template parameters. */ |
4699c561 | 5988 | if (!fn && !TREE_TYPE (t)) |
8dfaeb63 MM |
5989 | return error_mark_node; |
5990 | break; | |
b8865407 | 5991 | |
42976354 BK |
5992 | case MODOP_EXPR: |
5993 | case CAST_EXPR: | |
5994 | case REINTERPRET_CAST_EXPR: | |
5995 | case CONST_CAST_EXPR: | |
5996 | case STATIC_CAST_EXPR: | |
5997 | case DYNAMIC_CAST_EXPR: | |
42976354 BK |
5998 | case ARROW_EXPR: |
5999 | case DOTSTAR_EXPR: | |
6000 | case TYPEID_EXPR: | |
40242ccf | 6001 | case PSEUDO_DTOR_EXPR: |
b8865407 | 6002 | if (!fn) |
8dfaeb63 MM |
6003 | return error_mark_node; |
6004 | break; | |
abff8e06 | 6005 | |
8d08fdba | 6006 | default: |
8dfaeb63 | 6007 | break; |
8d08fdba | 6008 | } |
8dfaeb63 MM |
6009 | |
6010 | /* We didn't find any template parameters we liked. */ | |
6011 | return NULL_TREE; | |
6012 | } | |
6013 | ||
c8094d83 MS |
6014 | /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM, |
6015 | BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T, | |
a1281f45 | 6016 | call FN with the parameter and the DATA. |
838dfd8a | 6017 | If FN returns nonzero, the iteration is terminated, and |
8dfaeb63 | 6018 | for_each_template_parm returns 1. Otherwise, the iteration |
838dfd8a | 6019 | continues. If FN never returns a nonzero value, the value |
8dfaeb63 MM |
6020 | returned by for_each_template_parm is 0. If FN is NULL, it is |
6021 | considered to be the function which always returns 1. */ | |
6022 | ||
6023 | static int | |
0c58f841 MA |
6024 | for_each_template_parm (tree t, tree_fn_t fn, void* data, |
6025 | struct pointer_set_t *visited) | |
8dfaeb63 MM |
6026 | { |
6027 | struct pair_fn_data pfd; | |
ad2ae3b2 | 6028 | int result; |
8dfaeb63 MM |
6029 | |
6030 | /* Set up. */ | |
6031 | pfd.fn = fn; | |
6032 | pfd.data = data; | |
6033 | ||
4890c2f4 MM |
6034 | /* Walk the tree. (Conceptually, we would like to walk without |
6035 | duplicates, but for_each_template_parm_r recursively calls | |
6036 | for_each_template_parm, so we would need to reorganize a fair | |
4f2c9d7e MM |
6037 | bit to use walk_tree_without_duplicates, so we keep our own |
6038 | visited list.) */ | |
6039 | if (visited) | |
6040 | pfd.visited = visited; | |
6041 | else | |
0c58f841 | 6042 | pfd.visited = pointer_set_create (); |
14588106 RG |
6043 | result = cp_walk_tree (&t, |
6044 | for_each_template_parm_r, | |
6045 | &pfd, | |
6046 | pfd.visited) != NULL_TREE; | |
ad2ae3b2 MM |
6047 | |
6048 | /* Clean up. */ | |
6049 | if (!visited) | |
0c58f841 MA |
6050 | { |
6051 | pointer_set_destroy (pfd.visited); | |
6052 | pfd.visited = 0; | |
6053 | } | |
ad2ae3b2 MM |
6054 | |
6055 | return result; | |
8d08fdba MS |
6056 | } |
6057 | ||
d43f603d KL |
6058 | /* Returns true if T depends on any template parameter. */ |
6059 | ||
050367a3 | 6060 | int |
3a978d72 | 6061 | uses_template_parms (tree t) |
050367a3 | 6062 | { |
c353b8e3 MM |
6063 | bool dependent_p; |
6064 | int saved_processing_template_decl; | |
6065 | ||
6066 | saved_processing_template_decl = processing_template_decl; | |
6067 | if (!saved_processing_template_decl) | |
6068 | processing_template_decl = 1; | |
6069 | if (TYPE_P (t)) | |
6070 | dependent_p = dependent_type_p (t); | |
6071 | else if (TREE_CODE (t) == TREE_VEC) | |
6072 | dependent_p = any_dependent_template_arguments_p (t); | |
6073 | else if (TREE_CODE (t) == TREE_LIST) | |
6074 | dependent_p = (uses_template_parms (TREE_VALUE (t)) | |
6075 | || uses_template_parms (TREE_CHAIN (t))); | |
3ce5fa4f NS |
6076 | else if (TREE_CODE (t) == TYPE_DECL) |
6077 | dependent_p = dependent_type_p (TREE_TYPE (t)); | |
c8094d83 MS |
6078 | else if (DECL_P (t) |
6079 | || EXPR_P (t) | |
c353b8e3 MM |
6080 | || TREE_CODE (t) == TEMPLATE_PARM_INDEX |
6081 | || TREE_CODE (t) == OVERLOAD | |
6082 | || TREE_CODE (t) == BASELINK | |
b207d6e2 | 6083 | || TREE_CODE (t) == IDENTIFIER_NODE |
cb68ec50 | 6084 | || TREE_CODE (t) == TRAIT_EXPR |
6615c446 | 6085 | || CONSTANT_CLASS_P (t)) |
c353b8e3 MM |
6086 | dependent_p = (type_dependent_expression_p (t) |
6087 | || value_dependent_expression_p (t)); | |
315fb5db NS |
6088 | else |
6089 | { | |
6090 | gcc_assert (t == error_mark_node); | |
6091 | dependent_p = false; | |
6092 | } | |
c8094d83 | 6093 | |
c353b8e3 MM |
6094 | processing_template_decl = saved_processing_template_decl; |
6095 | ||
6096 | return dependent_p; | |
050367a3 MM |
6097 | } |
6098 | ||
d43f603d KL |
6099 | /* Returns true if T depends on any template parameter with level LEVEL. */ |
6100 | ||
6101 | int | |
6102 | uses_template_parms_level (tree t, int level) | |
6103 | { | |
6104 | return for_each_template_parm (t, template_parm_this_level_p, &level, NULL); | |
6105 | } | |
6106 | ||
27fafc8d | 6107 | static int tinst_depth; |
e9f32eb5 | 6108 | extern int max_tinst_depth; |
5566b478 | 6109 | #ifdef GATHER_STATISTICS |
27fafc8d | 6110 | int depth_reached; |
5566b478 | 6111 | #endif |
8dfaeb63 MM |
6112 | static int tinst_level_tick; |
6113 | static int last_template_error_tick; | |
8d08fdba | 6114 | |
3ae18eaf JM |
6115 | /* We're starting to instantiate D; record the template instantiation context |
6116 | for diagnostics and to restore it later. */ | |
6117 | ||
aa9d8196 | 6118 | static int |
3a978d72 | 6119 | push_tinst_level (tree d) |
8d08fdba | 6120 | { |
e2c3721c | 6121 | struct tinst_level *new; |
8d08fdba | 6122 | |
7215f9a0 MS |
6123 | if (tinst_depth >= max_tinst_depth) |
6124 | { | |
8adf5b5e JM |
6125 | /* If the instantiation in question still has unbound template parms, |
6126 | we don't really care if we can't instantiate it, so just return. | |
0cbd7506 | 6127 | This happens with base instantiation for implicit `typename'. */ |
8adf5b5e JM |
6128 | if (uses_template_parms (d)) |
6129 | return 0; | |
6130 | ||
1139b3d8 | 6131 | last_template_error_tick = tinst_level_tick; |
0f51ccfc | 6132 | error ("template instantiation depth exceeds maximum of %d (use " |
0cbd7506 | 6133 | "-ftemplate-depth-NN to increase the maximum) instantiating %qD", |
b4f4233d | 6134 | max_tinst_depth, d); |
5566b478 | 6135 | |
cb753e49 | 6136 | print_instantiation_context (); |
5566b478 | 6137 | |
7215f9a0 MS |
6138 | return 0; |
6139 | } | |
6140 | ||
e2c3721c PB |
6141 | new = GGC_NEW (struct tinst_level); |
6142 | new->decl = d; | |
6143 | new->locus = input_location; | |
6144 | new->in_system_header_p = in_system_header; | |
6145 | new->next = current_tinst_level; | |
8d08fdba | 6146 | current_tinst_level = new; |
5566b478 | 6147 | |
7215f9a0 | 6148 | ++tinst_depth; |
5566b478 MS |
6149 | #ifdef GATHER_STATISTICS |
6150 | if (tinst_depth > depth_reached) | |
6151 | depth_reached = tinst_depth; | |
6152 | #endif | |
6153 | ||
27fafc8d | 6154 | ++tinst_level_tick; |
7215f9a0 | 6155 | return 1; |
8d08fdba MS |
6156 | } |
6157 | ||
3ae18eaf JM |
6158 | /* We're done instantiating this template; return to the instantiation |
6159 | context. */ | |
6160 | ||
aa9d8196 | 6161 | static void |
3a978d72 | 6162 | pop_tinst_level (void) |
8d08fdba | 6163 | { |
ae58fa02 MM |
6164 | /* Restore the filename and line number stashed away when we started |
6165 | this instantiation. */ | |
e2c3721c PB |
6166 | input_location = current_tinst_level->locus; |
6167 | in_system_header = current_tinst_level->in_system_header_p; | |
6168 | current_tinst_level = current_tinst_level->next; | |
7215f9a0 | 6169 | --tinst_depth; |
27fafc8d | 6170 | ++tinst_level_tick; |
8d08fdba MS |
6171 | } |
6172 | ||
3ae18eaf JM |
6173 | /* We're instantiating a deferred template; restore the template |
6174 | instantiation context in which the instantiation was requested, which | |
e2c3721c | 6175 | is one step out from LEVEL. Return the corresponding DECL or TYPE. */ |
3ae18eaf | 6176 | |
e2c3721c PB |
6177 | static tree |
6178 | reopen_tinst_level (struct tinst_level *level) | |
3ae18eaf | 6179 | { |
e2c3721c | 6180 | struct tinst_level *t; |
3ae18eaf JM |
6181 | |
6182 | tinst_depth = 0; | |
e2c3721c | 6183 | for (t = level; t; t = t->next) |
3ae18eaf JM |
6184 | ++tinst_depth; |
6185 | ||
6186 | current_tinst_level = level; | |
6187 | pop_tinst_level (); | |
e2c3721c | 6188 | return level->decl; |
3ae18eaf JM |
6189 | } |
6190 | ||
61172206 JM |
6191 | /* Returns the TINST_LEVEL which gives the original instantiation |
6192 | context. */ | |
6193 | ||
e2c3721c | 6194 | struct tinst_level * |
61172206 JM |
6195 | outermost_tinst_level (void) |
6196 | { | |
e2c3721c PB |
6197 | struct tinst_level *level = current_tinst_level; |
6198 | if (level) | |
6199 | while (level->next) | |
6200 | level = level->next; | |
6201 | return level; | |
61172206 JM |
6202 | } |
6203 | ||
f84b4be9 JM |
6204 | /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the |
6205 | vector of template arguments, as for tsubst. | |
6206 | ||
dc957d14 | 6207 | Returns an appropriate tsubst'd friend declaration. */ |
f84b4be9 JM |
6208 | |
6209 | static tree | |
3a978d72 | 6210 | tsubst_friend_function (tree decl, tree args) |
f84b4be9 JM |
6211 | { |
6212 | tree new_friend; | |
27fafc8d | 6213 | |
c8094d83 | 6214 | if (TREE_CODE (decl) == FUNCTION_DECL |
f84b4be9 JM |
6215 | && DECL_TEMPLATE_INSTANTIATION (decl) |
6216 | && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL) | |
6217 | /* This was a friend declared with an explicit template | |
6218 | argument list, e.g.: | |
c8094d83 | 6219 | |
f84b4be9 | 6220 | friend void f<>(T); |
c8094d83 | 6221 | |
f84b4be9 JM |
6222 | to indicate that f was a template instantiation, not a new |
6223 | function declaration. Now, we have to figure out what | |
6224 | instantiation of what template. */ | |
6225 | { | |
76e57b45 | 6226 | tree template_id, arglist, fns; |
f84b4be9 JM |
6227 | tree new_args; |
6228 | tree tmpl; | |
ed2fa432 | 6229 | tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type)); |
c8094d83 | 6230 | |
76e57b45 | 6231 | /* Friend functions are looked up in the containing namespace scope. |
0cbd7506 MS |
6232 | We must enter that scope, to avoid finding member functions of the |
6233 | current cless with same name. */ | |
76e57b45 NS |
6234 | push_nested_namespace (ns); |
6235 | fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args, | |
015c2c66 MM |
6236 | tf_warning_or_error, NULL_TREE, |
6237 | /*integral_constant_expression_p=*/false); | |
76e57b45 NS |
6238 | pop_nested_namespace (ns); |
6239 | arglist = tsubst (DECL_TI_ARGS (decl), args, | |
23fca1f5 | 6240 | tf_warning_or_error, NULL_TREE); |
76e57b45 | 6241 | template_id = lookup_template_function (fns, arglist); |
c8094d83 | 6242 | |
23fca1f5 | 6243 | new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE); |
36a117a5 | 6244 | tmpl = determine_specialization (template_id, new_friend, |
c8094d83 | 6245 | &new_args, |
5fe7b654 | 6246 | /*need_member_template=*/0, |
29a1da1c MM |
6247 | TREE_VEC_LENGTH (args), |
6248 | tsk_none); | |
6e432b31 | 6249 | return instantiate_template (tmpl, new_args, tf_error); |
f84b4be9 | 6250 | } |
36a117a5 | 6251 | |
23fca1f5 | 6252 | new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE); |
c8094d83 | 6253 | |
36a117a5 | 6254 | /* The NEW_FRIEND will look like an instantiation, to the |
f84b4be9 JM |
6255 | compiler, but is not an instantiation from the point of view of |
6256 | the language. For example, we might have had: | |
c8094d83 | 6257 | |
f84b4be9 JM |
6258 | template <class T> struct S { |
6259 | template <class U> friend void f(T, U); | |
6260 | }; | |
c8094d83 | 6261 | |
f84b4be9 JM |
6262 | Then, in S<int>, template <class U> void f(int, U) is not an |
6263 | instantiation of anything. */ | |
ac2b3222 AP |
6264 | if (new_friend == error_mark_node) |
6265 | return error_mark_node; | |
c8094d83 | 6266 | |
f84b4be9 JM |
6267 | DECL_USE_TEMPLATE (new_friend) = 0; |
6268 | if (TREE_CODE (decl) == TEMPLATE_DECL) | |
655dc6ee JM |
6269 | { |
6270 | DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0; | |
6271 | DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend)) | |
6272 | = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl)); | |
6273 | } | |
36a117a5 | 6274 | |
92643fea MM |
6275 | /* The mangled name for the NEW_FRIEND is incorrect. The function |
6276 | is not a template instantiation and should not be mangled like | |
6277 | one. Therefore, we forget the mangling here; we'll recompute it | |
6278 | later if we need it. */ | |
36a117a5 MM |
6279 | if (TREE_CODE (new_friend) != TEMPLATE_DECL) |
6280 | { | |
19e7881c | 6281 | SET_DECL_RTL (new_friend, NULL_RTX); |
92643fea | 6282 | SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE); |
36a117a5 | 6283 | } |
c8094d83 | 6284 | |
6eb3bb27 | 6285 | if (DECL_NAMESPACE_SCOPE_P (new_friend)) |
f84b4be9 | 6286 | { |
36a117a5 | 6287 | tree old_decl; |
fbf1c34b MM |
6288 | tree new_friend_template_info; |
6289 | tree new_friend_result_template_info; | |
92da7074 | 6290 | tree ns; |
fbf1c34b MM |
6291 | int new_friend_is_defn; |
6292 | ||
6293 | /* We must save some information from NEW_FRIEND before calling | |
6294 | duplicate decls since that function will free NEW_FRIEND if | |
6295 | possible. */ | |
6296 | new_friend_template_info = DECL_TEMPLATE_INFO (new_friend); | |
5bd61841 | 6297 | new_friend_is_defn = |
c8094d83 | 6298 | (DECL_INITIAL (DECL_TEMPLATE_RESULT |
5bd61841 MM |
6299 | (template_for_substitution (new_friend))) |
6300 | != NULL_TREE); | |
f84b4be9 | 6301 | if (TREE_CODE (new_friend) == TEMPLATE_DECL) |
fbf1c34b MM |
6302 | { |
6303 | /* This declaration is a `primary' template. */ | |
6304 | DECL_PRIMARY_TEMPLATE (new_friend) = new_friend; | |
c8094d83 | 6305 | |
fbf1c34b | 6306 | new_friend_result_template_info |
17aec3eb | 6307 | = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend)); |
fbf1c34b MM |
6308 | } |
6309 | else | |
5bd61841 | 6310 | new_friend_result_template_info = NULL_TREE; |
36a117a5 | 6311 | |
b01e6d2b JM |
6312 | /* Make the init_value nonzero so pushdecl knows this is a defn. */ |
6313 | if (new_friend_is_defn) | |
6314 | DECL_INITIAL (new_friend) = error_mark_node; | |
6315 | ||
1c227897 MM |
6316 | /* Inside pushdecl_namespace_level, we will push into the |
6317 | current namespace. However, the friend function should go | |
c6002625 | 6318 | into the namespace of the template. */ |
92da7074 ML |
6319 | ns = decl_namespace_context (new_friend); |
6320 | push_nested_namespace (ns); | |
d63d5d0c | 6321 | old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true); |
92da7074 | 6322 | pop_nested_namespace (ns); |
36a117a5 | 6323 | |
0014c247 VR |
6324 | if (old_decl == error_mark_node) |
6325 | return error_mark_node; | |
6326 | ||
36a117a5 MM |
6327 | if (old_decl != new_friend) |
6328 | { | |
6329 | /* This new friend declaration matched an existing | |
6330 | declaration. For example, given: | |
6331 | ||
6332 | template <class T> void f(T); | |
c8094d83 MS |
6333 | template <class U> class C { |
6334 | template <class T> friend void f(T) {} | |
36a117a5 MM |
6335 | }; |
6336 | ||
6337 | the friend declaration actually provides the definition | |
6338 | of `f', once C has been instantiated for some type. So, | |
6339 | old_decl will be the out-of-class template declaration, | |
6340 | while new_friend is the in-class definition. | |
6341 | ||
6342 | But, if `f' was called before this point, the | |
6343 | instantiation of `f' will have DECL_TI_ARGS corresponding | |
6344 | to `T' but not to `U', references to which might appear | |
6345 | in the definition of `f'. Previously, the most general | |
6346 | template for an instantiation of `f' was the out-of-class | |
6347 | version; now it is the in-class version. Therefore, we | |
6348 | run through all specialization of `f', adding to their | |
6349 | DECL_TI_ARGS appropriately. In particular, they need a | |
6350 | new set of outer arguments, corresponding to the | |
c8094d83 | 6351 | arguments for this class instantiation. |
36a117a5 MM |
6352 | |
6353 | The same situation can arise with something like this: | |
6354 | ||
6355 | friend void f(int); | |
c8094d83 | 6356 | template <class T> class C { |
0cbd7506 MS |
6357 | friend void f(T) {} |
6358 | }; | |
36a117a5 MM |
6359 | |
6360 | when `C<int>' is instantiated. Now, `f(int)' is defined | |
6361 | in the class. */ | |
6362 | ||
fbf1c34b MM |
6363 | if (!new_friend_is_defn) |
6364 | /* On the other hand, if the in-class declaration does | |
6365 | *not* provide a definition, then we don't want to alter | |
6366 | existing definitions. We can just leave everything | |
6367 | alone. */ | |
36a117a5 | 6368 | ; |
fbf1c34b | 6369 | else |
36a117a5 | 6370 | { |
fbf1c34b MM |
6371 | /* Overwrite whatever template info was there before, if |
6372 | any, with the new template information pertaining to | |
6373 | the declaration. */ | |
6374 | DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info; | |
6375 | ||
6376 | if (TREE_CODE (old_decl) != TEMPLATE_DECL) | |
8d83f792 MM |
6377 | reregister_specialization (new_friend, |
6378 | most_general_template (old_decl), | |
6379 | old_decl); | |
c8094d83 | 6380 | else |
36a117a5 | 6381 | { |
fbf1c34b MM |
6382 | tree t; |
6383 | tree new_friend_args; | |
6384 | ||
c8094d83 | 6385 | DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl)) |
fbf1c34b | 6386 | = new_friend_result_template_info; |
c8094d83 | 6387 | |
fbf1c34b | 6388 | new_friend_args = TI_ARGS (new_friend_template_info); |
c8094d83 | 6389 | for (t = DECL_TEMPLATE_SPECIALIZATIONS (old_decl); |
fbf1c34b MM |
6390 | t != NULL_TREE; |
6391 | t = TREE_CHAIN (t)) | |
6392 | { | |
6393 | tree spec = TREE_VALUE (t); | |
c8094d83 MS |
6394 | |
6395 | DECL_TI_ARGS (spec) | |
fbf1c34b MM |
6396 | = add_outermost_template_args (new_friend_args, |
6397 | DECL_TI_ARGS (spec)); | |
fbf1c34b MM |
6398 | } |
6399 | ||
6400 | /* Now, since specializations are always supposed to | |
6401 | hang off of the most general template, we must move | |
6402 | them. */ | |
6403 | t = most_general_template (old_decl); | |
6404 | if (t != old_decl) | |
6405 | { | |
6406 | DECL_TEMPLATE_SPECIALIZATIONS (t) | |
6407 | = chainon (DECL_TEMPLATE_SPECIALIZATIONS (t), | |
6408 | DECL_TEMPLATE_SPECIALIZATIONS (old_decl)); | |
6409 | DECL_TEMPLATE_SPECIALIZATIONS (old_decl) = NULL_TREE; | |
6410 | } | |
36a117a5 MM |
6411 | } |
6412 | } | |
6413 | ||
6414 | /* The information from NEW_FRIEND has been merged into OLD_DECL | |
6415 | by duplicate_decls. */ | |
6416 | new_friend = old_decl; | |
6417 | } | |
f84b4be9 | 6418 | } |
6e432b31 | 6419 | else |
f84b4be9 | 6420 | { |
6e432b31 KL |
6421 | tree context = DECL_CONTEXT (new_friend); |
6422 | bool dependent_p; | |
6423 | ||
6424 | /* In the code | |
6425 | template <class T> class C { | |
6426 | template <class U> friend void C1<U>::f (); // case 1 | |
6427 | friend void C2<T>::f (); // case 2 | |
6428 | }; | |
6429 | we only need to make sure CONTEXT is a complete type for | |
6430 | case 2. To distinguish between the two cases, we note that | |
6431 | CONTEXT of case 1 remains dependent type after tsubst while | |
6432 | this isn't true for case 2. */ | |
6433 | ++processing_template_decl; | |
6434 | dependent_p = dependent_type_p (context); | |
6435 | --processing_template_decl; | |
6436 | ||
6437 | if (!dependent_p | |
6438 | && !complete_type_or_else (context, NULL_TREE)) | |
6439 | return error_mark_node; | |
6440 | ||
6441 | if (COMPLETE_TYPE_P (context)) | |
6442 | { | |
6443 | /* Check to see that the declaration is really present, and, | |
6444 | possibly obtain an improved declaration. */ | |
6445 | tree fn = check_classfn (context, | |
6446 | new_friend, NULL_TREE); | |
6447 | ||
684939ce | 6448 | if (fn) |
6e432b31 KL |
6449 | new_friend = fn; |
6450 | } | |
f84b4be9 JM |
6451 | } |
6452 | ||
6453 | return new_friend; | |
6454 | } | |
6455 | ||
1aed5355 MM |
6456 | /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of |
6457 | template arguments, as for tsubst. | |
6757edfe | 6458 | |
dc957d14 | 6459 | Returns an appropriate tsubst'd friend type or error_mark_node on |
4b054b80 | 6460 | failure. */ |
6757edfe MM |
6461 | |
6462 | static tree | |
3a978d72 | 6463 | tsubst_friend_class (tree friend_tmpl, tree args) |
6757edfe | 6464 | { |
1aed5355 | 6465 | tree friend_type; |
25aab5d0 | 6466 | tree tmpl; |
3e0ec82f | 6467 | tree context; |
6757edfe | 6468 | |
3e0ec82f MM |
6469 | context = DECL_CONTEXT (friend_tmpl); |
6470 | ||
6471 | if (context) | |
77adef84 | 6472 | { |
3e0ec82f MM |
6473 | if (TREE_CODE (context) == NAMESPACE_DECL) |
6474 | push_nested_namespace (context); | |
6475 | else | |
c8094d83 | 6476 | push_nested_class (tsubst (context, args, tf_none, NULL_TREE)); |
3e0ec82f | 6477 | } |
25aab5d0 | 6478 | |
105d72c5 MM |
6479 | /* Look for a class template declaration. We look for hidden names |
6480 | because two friend declarations of the same template are the | |
6481 | same. For example, in: | |
6482 | ||
6483 | struct A { | |
6484 | template <typename> friend class F; | |
6485 | }; | |
6486 | template <typename> struct B { | |
6487 | template <typename> friend class F; | |
6488 | }; | |
6489 | ||
6490 | both F templates are the same. */ | |
6491 | tmpl = lookup_name_real (DECL_NAME (friend_tmpl), 0, 0, | |
6492 | /*block_p=*/true, 0, | |
6493 | LOOKUP_COMPLAIN | LOOKUP_HIDDEN); | |
25aab5d0 | 6494 | |
3e0ec82f MM |
6495 | /* But, if we don't find one, it might be because we're in a |
6496 | situation like this: | |
77adef84 | 6497 | |
3e0ec82f MM |
6498 | template <class T> |
6499 | struct S { | |
6500 | template <class U> | |
6501 | friend struct S; | |
6502 | }; | |
6503 | ||
6504 | Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL | |
6505 | for `S<int>', not the TEMPLATE_DECL. */ | |
6506 | if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl)) | |
6507 | { | |
10e6657a | 6508 | tmpl = lookup_name_prefer_type (DECL_NAME (friend_tmpl), 1); |
3e0ec82f | 6509 | tmpl = maybe_get_template_decl_from_type_decl (tmpl); |
25aab5d0 | 6510 | } |
6757edfe | 6511 | |
25aab5d0 | 6512 | if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl)) |
6757edfe MM |
6513 | { |
6514 | /* The friend template has already been declared. Just | |
36a117a5 MM |
6515 | check to see that the declarations match, and install any new |
6516 | default parameters. We must tsubst the default parameters, | |
6517 | of course. We only need the innermost template parameters | |
6518 | because that is all that redeclare_class_template will look | |
6519 | at. */ | |
3e0ec82f MM |
6520 | if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl)) |
6521 | > TMPL_ARGS_DEPTH (args)) | |
6522 | { | |
6523 | tree parms; | |
b131ad7c | 6524 | location_t saved_input_location; |
3e0ec82f | 6525 | parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl), |
23fca1f5 | 6526 | args, tf_warning_or_error); |
b131ad7c MLI |
6527 | |
6528 | saved_input_location = input_location; | |
6529 | input_location = DECL_SOURCE_LOCATION (friend_tmpl); | |
3e0ec82f | 6530 | redeclare_class_template (TREE_TYPE (tmpl), parms); |
b131ad7c MLI |
6531 | input_location = saved_input_location; |
6532 | ||
3e0ec82f MM |
6533 | } |
6534 | ||
6757edfe MM |
6535 | friend_type = TREE_TYPE (tmpl); |
6536 | } | |
6537 | else | |
6538 | { | |
6539 | /* The friend template has not already been declared. In this | |
6540 | case, the instantiation of the template class will cause the | |
6541 | injection of this template into the global scope. */ | |
23fca1f5 | 6542 | tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE); |
dca56f77 VR |
6543 | if (tmpl == error_mark_node) |
6544 | return error_mark_node; | |
6757edfe MM |
6545 | |
6546 | /* The new TMPL is not an instantiation of anything, so we | |
0cbd7506 | 6547 | forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for |
6757edfe MM |
6548 | the new type because that is supposed to be the corresponding |
6549 | template decl, i.e., TMPL. */ | |
6550 | DECL_USE_TEMPLATE (tmpl) = 0; | |
6551 | DECL_TEMPLATE_INFO (tmpl) = NULL_TREE; | |
6552 | CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0; | |
68c78847 KL |
6553 | CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)) |
6554 | = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))); | |
6757edfe MM |
6555 | |
6556 | /* Inject this template into the global scope. */ | |
d63d5d0c | 6557 | friend_type = TREE_TYPE (pushdecl_top_level_maybe_friend (tmpl, true)); |
6757edfe MM |
6558 | } |
6559 | ||
c8094d83 | 6560 | if (context) |
3e0ec82f MM |
6561 | { |
6562 | if (TREE_CODE (context) == NAMESPACE_DECL) | |
6563 | pop_nested_namespace (context); | |
6564 | else | |
6565 | pop_nested_class (); | |
6566 | } | |
6567 | ||
6757edfe MM |
6568 | return friend_type; |
6569 | } | |
f84b4be9 | 6570 | |
17f29637 KL |
6571 | /* Returns zero if TYPE cannot be completed later due to circularity. |
6572 | Otherwise returns one. */ | |
6573 | ||
d5372501 | 6574 | static int |
3a978d72 | 6575 | can_complete_type_without_circularity (tree type) |
17f29637 KL |
6576 | { |
6577 | if (type == NULL_TREE || type == error_mark_node) | |
6578 | return 0; | |
6579 | else if (COMPLETE_TYPE_P (type)) | |
6580 | return 1; | |
6581 | else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type)) | |
6582 | return can_complete_type_without_circularity (TREE_TYPE (type)); | |
8c6ab2db NS |
6583 | else if (CLASS_TYPE_P (type) |
6584 | && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type))) | |
17f29637 KL |
6585 | return 0; |
6586 | else | |
6587 | return 1; | |
6588 | } | |
6589 | ||
4f4141ff JM |
6590 | /* Apply any attributes which had to be deferred until instantiation |
6591 | time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes; | |
6592 | ARGS, COMPLAIN, IN_DECL are as tsubst. */ | |
6593 | ||
6594 | static void | |
6595 | apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags, | |
6596 | tree args, tsubst_flags_t complain, tree in_decl) | |
6597 | { | |
5818c8e4 | 6598 | tree last_dep = NULL_TREE; |
4f4141ff | 6599 | tree t; |
5818c8e4 JM |
6600 | tree *p; |
6601 | ||
6602 | for (t = attributes; t; t = TREE_CHAIN (t)) | |
6603 | if (ATTR_IS_DEPENDENT (t)) | |
6604 | { | |
6605 | last_dep = t; | |
6606 | attributes = copy_list (attributes); | |
6607 | break; | |
6608 | } | |
4f4141ff JM |
6609 | |
6610 | if (DECL_P (*decl_p)) | |
5818c8e4 | 6611 | p = &DECL_ATTRIBUTES (*decl_p); |
4f4141ff | 6612 | else |
5818c8e4 | 6613 | p = &TYPE_ATTRIBUTES (*decl_p); |
4f4141ff | 6614 | |
5818c8e4 JM |
6615 | if (last_dep) |
6616 | { | |
6617 | tree late_attrs = NULL_TREE; | |
6618 | tree *q = &late_attrs; | |
6619 | ||
6620 | for (*p = attributes; *p; ) | |
6621 | { | |
6622 | t = *p; | |
6623 | if (ATTR_IS_DEPENDENT (t)) | |
6624 | { | |
6625 | *p = TREE_CHAIN (t); | |
6626 | TREE_CHAIN (t) = NULL_TREE; | |
6627 | TREE_VALUE (t) | |
6628 | = tsubst_expr (TREE_VALUE (t), args, complain, in_decl, | |
6629 | /*integral_constant_expression_p=*/false); | |
6630 | *q = t; | |
6631 | q = &TREE_CHAIN (t); | |
6632 | } | |
6633 | else | |
6634 | p = &TREE_CHAIN (t); | |
6635 | } | |
4f4141ff | 6636 | |
5818c8e4 JM |
6637 | cplus_decl_attributes (decl_p, late_attrs, attr_flags); |
6638 | } | |
4f4141ff JM |
6639 | } |
6640 | ||
8d08fdba | 6641 | tree |
3a978d72 | 6642 | instantiate_class_template (tree type) |
8d08fdba | 6643 | { |
7088fca9 | 6644 | tree template, args, pattern, t, member; |
36a117a5 | 6645 | tree typedecl; |
dbbf88d1 | 6646 | tree pbinfo; |
cad7e87b | 6647 | tree base_list; |
c8094d83 | 6648 | |
5566b478 | 6649 | if (type == error_mark_node) |
8d08fdba MS |
6650 | return error_mark_node; |
6651 | ||
c8094d83 | 6652 | if (TYPE_BEING_DEFINED (type) |
ca099ac8 MM |
6653 | || COMPLETE_TYPE_P (type) |
6654 | || dependent_type_p (type)) | |
5566b478 MS |
6655 | return type; |
6656 | ||
6bdb985f | 6657 | /* Figure out which template is being instantiated. */ |
36a117a5 | 6658 | template = most_general_template (CLASSTYPE_TI_TEMPLATE (type)); |
50bc768d | 6659 | gcc_assert (TREE_CODE (template) == TEMPLATE_DECL); |
73aad9b9 | 6660 | |
4c571114 MM |
6661 | /* Determine what specialization of the original template to |
6662 | instantiate. */ | |
916b63c3 | 6663 | t = most_specialized_class (type, template); |
8fbc5ae7 | 6664 | if (t == error_mark_node) |
73aad9b9 | 6665 | { |
8fbc5ae7 MM |
6666 | TYPE_BEING_DEFINED (type) = 1; |
6667 | return error_mark_node; | |
73aad9b9 | 6668 | } |
916b63c3 MM |
6669 | else if (t) |
6670 | { | |
6671 | /* This TYPE is actually an instantiation of a partial | |
6672 | specialization. We replace the innermost set of ARGS with | |
6673 | the arguments appropriate for substitution. For example, | |
6674 | given: | |
3db45ab5 MS |
6675 | |
6676 | template <class T> struct S {}; | |
916b63c3 | 6677 | template <class T> struct S<T*> {}; |
6bdb985f | 6678 | |
916b63c3 | 6679 | and supposing that we are instantiating S<int*>, ARGS will |
3db45ab5 | 6680 | presently be {int*} -- but we need {int}. */ |
916b63c3 MM |
6681 | pattern = TREE_TYPE (t); |
6682 | args = TREE_PURPOSE (t); | |
6683 | } | |
73aad9b9 | 6684 | else |
916b63c3 MM |
6685 | { |
6686 | pattern = TREE_TYPE (template); | |
6687 | args = CLASSTYPE_TI_ARGS (type); | |
6688 | } | |
5566b478 | 6689 | |
4c571114 MM |
6690 | /* If the template we're instantiating is incomplete, then clearly |
6691 | there's nothing we can do. */ | |
d0f062fb | 6692 | if (!COMPLETE_TYPE_P (pattern)) |
f31c0a32 | 6693 | return type; |
5566b478 | 6694 | |
4c571114 MM |
6695 | /* If we've recursively instantiated too many templates, stop. */ |
6696 | if (! push_tinst_level (type)) | |
f31c0a32 | 6697 | return type; |
4c571114 MM |
6698 | |
6699 | /* Now we're really doing the instantiation. Mark the type as in | |
6700 | the process of being defined. */ | |
6701 | TYPE_BEING_DEFINED (type) = 1; | |
6702 | ||
78757caa KL |
6703 | /* We may be in the middle of deferred access check. Disable |
6704 | it now. */ | |
6705 | push_deferring_access_checks (dk_no_deferred); | |
6706 | ||
c353b8e3 | 6707 | push_to_top_level (); |
4c571114 | 6708 | |
7813d14c | 6709 | SET_CLASSTYPE_INTERFACE_UNKNOWN (type); |
8d08fdba | 6710 | |
68ea098a NS |
6711 | /* Set the input location to the template definition. This is needed |
6712 | if tsubsting causes an error. */ | |
12af7ba3 MM |
6713 | typedecl = TYPE_MAIN_DECL (type); |
6714 | input_location = DECL_SOURCE_LOCATION (typedecl); | |
6715 | in_system_header = DECL_IN_SYSTEM_HEADER (typedecl); | |
68ea098a | 6716 | |
f7da6097 | 6717 | TYPE_HAS_CONSTRUCTOR (type) = TYPE_HAS_CONSTRUCTOR (pattern); |
834c6dff MM |
6718 | TYPE_HAS_NEW_OPERATOR (type) = TYPE_HAS_NEW_OPERATOR (pattern); |
6719 | TYPE_HAS_ARRAY_NEW_OPERATOR (type) = TYPE_HAS_ARRAY_NEW_OPERATOR (pattern); | |
f7da6097 | 6720 | TYPE_GETS_DELETE (type) = TYPE_GETS_DELETE (pattern); |
f7da6097 MS |
6721 | TYPE_HAS_ASSIGN_REF (type) = TYPE_HAS_ASSIGN_REF (pattern); |
6722 | TYPE_HAS_CONST_ASSIGN_REF (type) = TYPE_HAS_CONST_ASSIGN_REF (pattern); | |
f7da6097 MS |
6723 | TYPE_HAS_INIT_REF (type) = TYPE_HAS_INIT_REF (pattern); |
6724 | TYPE_HAS_CONST_INIT_REF (type) = TYPE_HAS_CONST_INIT_REF (pattern); | |
f7da6097 MS |
6725 | TYPE_HAS_DEFAULT_CONSTRUCTOR (type) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern); |
6726 | TYPE_HAS_CONVERSION (type) = TYPE_HAS_CONVERSION (pattern); | |
f7da6097 MS |
6727 | TYPE_PACKED (type) = TYPE_PACKED (pattern); |
6728 | TYPE_ALIGN (type) = TYPE_ALIGN (pattern); | |
11cf4d18 | 6729 | TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern); |
eff71ab0 | 6730 | TYPE_FOR_JAVA (type) = TYPE_FOR_JAVA (pattern); /* For libjava's JArray<T> */ |
6bdb8141 JM |
6731 | if (ANON_AGGR_TYPE_P (pattern)) |
6732 | SET_ANON_AGGR_TYPE_P (type); | |
8d039470 MS |
6733 | if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern)) |
6734 | { | |
6735 | CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1; | |
6736 | CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern); | |
6737 | } | |
f7da6097 | 6738 | |
dbbf88d1 | 6739 | pbinfo = TYPE_BINFO (pattern); |
1456deaf | 6740 | |
315fb5db NS |
6741 | /* We should never instantiate a nested class before its enclosing |
6742 | class; we need to look up the nested class by name before we can | |
6743 | instantiate it, and that lookup should instantiate the enclosing | |
6744 | class. */ | |
6745 | gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern)) | |
6746 | || COMPLETE_TYPE_P (TYPE_CONTEXT (type)) | |
6747 | || TYPE_BEING_DEFINED (TYPE_CONTEXT (type))); | |
1456deaf | 6748 | |
cad7e87b | 6749 | base_list = NULL_TREE; |
fa743e8c | 6750 | if (BINFO_N_BASE_BINFOS (pbinfo)) |
3fd71a52 | 6751 | { |
fa743e8c | 6752 | tree pbase_binfo; |
a2507277 | 6753 | tree context = TYPE_CONTEXT (type); |
4514aa8c | 6754 | tree pushed_scope; |
3fd71a52 | 6755 | int i; |
5566b478 | 6756 | |
a2507277 NS |
6757 | /* We must enter the scope containing the type, as that is where |
6758 | the accessibility of types named in dependent bases are | |
6759 | looked up from. */ | |
4514aa8c | 6760 | pushed_scope = push_scope (context ? context : global_namespace); |
c8094d83 | 6761 | |
3fd71a52 MM |
6762 | /* Substitute into each of the bases to determine the actual |
6763 | basetypes. */ | |
fa743e8c | 6764 | for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++) |
3fd71a52 MM |
6765 | { |
6766 | tree base; | |
fa743e8c | 6767 | tree access = BINFO_BASE_ACCESS (pbinfo, i); |
5d80a306 DG |
6768 | tree expanded_bases = NULL_TREE; |
6769 | int idx, len = 1; | |
6770 | ||
6771 | if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo))) | |
6772 | { | |
6773 | expanded_bases = | |
6774 | tsubst_pack_expansion (BINFO_TYPE (pbase_binfo), | |
6775 | args, tf_error, NULL_TREE); | |
6776 | if (expanded_bases == error_mark_node) | |
6777 | continue; | |
6778 | ||
6779 | len = TREE_VEC_LENGTH (expanded_bases); | |
6780 | } | |
6781 | ||
6782 | for (idx = 0; idx < len; idx++) | |
6783 | { | |
6784 | if (expanded_bases) | |
6785 | /* Extract the already-expanded base class. */ | |
6786 | base = TREE_VEC_ELT (expanded_bases, idx); | |
6787 | else | |
6788 | /* Substitute to figure out the base class. */ | |
6789 | base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error, | |
6790 | NULL_TREE); | |
6791 | ||
6792 | if (base == error_mark_node) | |
6793 | continue; | |
6794 | ||
6795 | base_list = tree_cons (access, base, base_list); | |
6796 | if (BINFO_VIRTUAL_P (pbase_binfo)) | |
6797 | TREE_TYPE (base_list) = integer_type_node; | |
6798 | } | |
3fd71a52 | 6799 | } |
dfbcd65a | 6800 | |
3fd71a52 MM |
6801 | /* The list is now in reverse order; correct that. */ |
6802 | base_list = nreverse (base_list); | |
6803 | ||
4514aa8c NS |
6804 | if (pushed_scope) |
6805 | pop_scope (pushed_scope); | |
3fd71a52 | 6806 | } |
cad7e87b NS |
6807 | /* Now call xref_basetypes to set up all the base-class |
6808 | information. */ | |
6809 | xref_basetypes (type, base_list); | |
6810 | ||
4f4141ff JM |
6811 | apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern), |
6812 | (int) ATTR_FLAG_TYPE_IN_PLACE, | |
6813 | args, tf_error, NULL_TREE); | |
5566b478 | 6814 | |
b74a0560 MM |
6815 | /* Now that our base classes are set up, enter the scope of the |
6816 | class, so that name lookups into base classes, etc. will work | |
dc957d14 | 6817 | correctly. This is precisely analogous to what we do in |
b74a0560 | 6818 | begin_class_definition when defining an ordinary non-template |
56d0c6e3 JM |
6819 | class, except we also need to push the enclosing classes. */ |
6820 | push_nested_class (type); | |
b74a0560 | 6821 | |
7088fca9 | 6822 | /* Now members are processed in the order of declaration. */ |
8c6ab2db NS |
6823 | for (member = CLASSTYPE_DECL_LIST (pattern); |
6824 | member; member = TREE_CHAIN (member)) | |
8d08fdba | 6825 | { |
7088fca9 | 6826 | tree t = TREE_VALUE (member); |
5566b478 | 6827 | |
7088fca9 | 6828 | if (TREE_PURPOSE (member)) |
ed44da02 | 6829 | { |
7088fca9 KL |
6830 | if (TYPE_P (t)) |
6831 | { | |
5e0c54e5 | 6832 | /* Build new CLASSTYPE_NESTED_UTDS. */ |
8d08fdba | 6833 | |
7088fca9 | 6834 | tree newtag; |
883a2bff MM |
6835 | bool class_template_p; |
6836 | ||
2678bae8 VR |
6837 | class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE |
6838 | && TYPE_LANG_SPECIFIC (t) | |
6839 | && CLASSTYPE_IS_TEMPLATE (t)); | |
883a2bff | 6840 | /* If the member is a class template, then -- even after |
6c745393 | 6841 | substitution -- there may be dependent types in the |
883a2bff MM |
6842 | template argument list for the class. We increment |
6843 | PROCESSING_TEMPLATE_DECL so that dependent_type_p, as | |
6844 | that function will assume that no types are dependent | |
6845 | when outside of a template. */ | |
6846 | if (class_template_p) | |
6847 | ++processing_template_decl; | |
2678bae8 | 6848 | newtag = tsubst (t, args, tf_error, NULL_TREE); |
883a2bff MM |
6849 | if (class_template_p) |
6850 | --processing_template_decl; | |
2620d095 KL |
6851 | if (newtag == error_mark_node) |
6852 | continue; | |
6853 | ||
7088fca9 KL |
6854 | if (TREE_CODE (newtag) != ENUMERAL_TYPE) |
6855 | { | |
2678bae8 VR |
6856 | tree name = TYPE_IDENTIFIER (t); |
6857 | ||
883a2bff | 6858 | if (class_template_p) |
7088fca9 KL |
6859 | /* Unfortunately, lookup_template_class sets |
6860 | CLASSTYPE_IMPLICIT_INSTANTIATION for a partial | |
3afd2e20 NS |
6861 | instantiation (i.e., for the type of a member |
6862 | template class nested within a template class.) | |
6863 | This behavior is required for | |
6864 | maybe_process_partial_specialization to work | |
6865 | correctly, but is not accurate in this case; | |
6866 | the TAG is not an instantiation of anything. | |
6867 | (The corresponding TEMPLATE_DECL is an | |
6868 | instantiation, but the TYPE is not.) */ | |
7088fca9 KL |
6869 | CLASSTYPE_USE_TEMPLATE (newtag) = 0; |
6870 | ||
6871 | /* Now, we call pushtag to put this NEWTAG into the scope of | |
6872 | TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid | |
6873 | pushtag calling push_template_decl. We don't have to do | |
6874 | this for enums because it will already have been done in | |
6875 | tsubst_enum. */ | |
6876 | if (name) | |
6877 | SET_IDENTIFIER_TYPE_VALUE (name, newtag); | |
bd3d082e | 6878 | pushtag (name, newtag, /*tag_scope=*/ts_current); |
7088fca9 KL |
6879 | } |
6880 | } | |
c8094d83 | 6881 | else if (TREE_CODE (t) == FUNCTION_DECL |
7088fca9 KL |
6882 | || DECL_FUNCTION_TEMPLATE_P (t)) |
6883 | { | |
6884 | /* Build new TYPE_METHODS. */ | |
fecafe5e | 6885 | tree r; |
c8094d83 | 6886 | |
fecafe5e | 6887 | if (TREE_CODE (t) == TEMPLATE_DECL) |
9579624e | 6888 | ++processing_template_decl; |
fecafe5e NS |
6889 | r = tsubst (t, args, tf_error, NULL_TREE); |
6890 | if (TREE_CODE (t) == TEMPLATE_DECL) | |
9579624e | 6891 | --processing_template_decl; |
7088fca9 | 6892 | set_current_access_from_decl (r); |
7088fca9 KL |
6893 | finish_member_declaration (r); |
6894 | } | |
6895 | else | |
6896 | { | |
6897 | /* Build new TYPE_FIELDS. */ | |
55a3debe DG |
6898 | if (TREE_CODE (t) == STATIC_ASSERT) |
6899 | { | |
6900 | tree condition = | |
6901 | tsubst_expr (STATIC_ASSERT_CONDITION (t), args, | |
6902 | tf_warning_or_error, NULL_TREE, | |
6903 | /*integral_constant_expression_p=*/true); | |
6904 | finish_static_assert (condition, | |
6905 | STATIC_ASSERT_MESSAGE (t), | |
6906 | STATIC_ASSERT_SOURCE_LOCATION (t), | |
6907 | /*member_p=*/true); | |
6908 | } | |
6909 | else if (TREE_CODE (t) != CONST_DECL) | |
7088fca9 KL |
6910 | { |
6911 | tree r; | |
fa8d6e85 | 6912 | |
d479d37f NS |
6913 | /* The the file and line for this declaration, to |
6914 | assist in error message reporting. Since we | |
6915 | called push_tinst_level above, we don't need to | |
6916 | restore these. */ | |
f31686a3 | 6917 | input_location = DECL_SOURCE_LOCATION (t); |
fa8d6e85 | 6918 | |
fb5ce3c9 | 6919 | if (TREE_CODE (t) == TEMPLATE_DECL) |
9579624e | 6920 | ++processing_template_decl; |
23fca1f5 | 6921 | r = tsubst (t, args, tf_warning_or_error, NULL_TREE); |
fb5ce3c9 | 6922 | if (TREE_CODE (t) == TEMPLATE_DECL) |
9579624e | 6923 | --processing_template_decl; |
7088fca9 KL |
6924 | if (TREE_CODE (r) == VAR_DECL) |
6925 | { | |
b794e321 MM |
6926 | /* In [temp.inst]: |
6927 | ||
6928 | [t]he initialization (and any associated | |
6929 | side-effects) of a static data member does | |
6930 | not occur unless the static data member is | |
6931 | itself used in a way that requires the | |
6932 | definition of the static data member to | |
3db45ab5 | 6933 | exist. |
b794e321 MM |
6934 | |
6935 | Therefore, we do not substitute into the | |
3db45ab5 MS |
6936 | initialized for the static data member here. */ |
6937 | finish_static_data_member_decl | |
6938 | (r, | |
6939 | /*init=*/NULL_TREE, | |
d174af6c | 6940 | /*init_const_expr_p=*/false, |
3db45ab5 | 6941 | /*asmspec_tree=*/NULL_TREE, |
b794e321 | 6942 | /*flags=*/0); |
7088fca9 KL |
6943 | if (DECL_INITIALIZED_IN_CLASS_P (r)) |
6944 | check_static_variable_definition (r, TREE_TYPE (r)); | |
6945 | } | |
6946 | else if (TREE_CODE (r) == FIELD_DECL) | |
6947 | { | |
6948 | /* Determine whether R has a valid type and can be | |
6949 | completed later. If R is invalid, then it is | |
6950 | replaced by error_mark_node so that it will not be | |
6951 | added to TYPE_FIELDS. */ | |
6952 | tree rtype = TREE_TYPE (r); | |
6953 | if (can_complete_type_without_circularity (rtype)) | |
6954 | complete_type (rtype); | |
6955 | ||
6956 | if (!COMPLETE_TYPE_P (rtype)) | |
6957 | { | |
6958 | cxx_incomplete_type_error (r, rtype); | |
0cbd7506 | 6959 | r = error_mark_node; |
7088fca9 KL |
6960 | } |
6961 | } | |
5566b478 | 6962 | |
7088fca9 KL |
6963 | /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE, |
6964 | such a thing will already have been added to the field | |
6965 | list by tsubst_enum in finish_member_declaration in the | |
5e0c54e5 | 6966 | CLASSTYPE_NESTED_UTDS case above. */ |
7088fca9 KL |
6967 | if (!(TREE_CODE (r) == TYPE_DECL |
6968 | && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE | |
43b26a01 | 6969 | && DECL_ARTIFICIAL (r))) |
7088fca9 KL |
6970 | { |
6971 | set_current_access_from_decl (r); | |
6972 | finish_member_declaration (r); | |
6973 | } | |
0cbd7506 | 6974 | } |
7088fca9 | 6975 | } |
61fc8c9e | 6976 | } |
7088fca9 KL |
6977 | else |
6978 | { | |
6979 | if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)) | |
6980 | { | |
6981 | /* Build new CLASSTYPE_FRIEND_CLASSES. */ | |
6982 | ||
6983 | tree friend_type = t; | |
b939a023 | 6984 | bool adjust_processing_template_decl = false; |
1aed5355 | 6985 | |
7088fca9 | 6986 | if (TREE_CODE (friend_type) == TEMPLATE_DECL) |
b939a023 | 6987 | { |
5a24482e | 6988 | /* template <class T> friend class C; */ |
b939a023 | 6989 | friend_type = tsubst_friend_class (friend_type, args); |
0cbd7506 | 6990 | adjust_processing_template_decl = true; |
b939a023 KL |
6991 | } |
6992 | else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE) | |
6993 | { | |
5a24482e | 6994 | /* template <class T> friend class C::D; */ |
b939a023 | 6995 | friend_type = tsubst (friend_type, args, |
23fca1f5 | 6996 | tf_warning_or_error, NULL_TREE); |
b939a023 KL |
6997 | if (TREE_CODE (friend_type) == TEMPLATE_DECL) |
6998 | friend_type = TREE_TYPE (friend_type); | |
0cbd7506 | 6999 | adjust_processing_template_decl = true; |
b939a023 KL |
7000 | } |
7001 | else if (TREE_CODE (friend_type) == TYPENAME_TYPE) | |
7002 | { | |
5a24482e KL |
7003 | /* This could be either |
7004 | ||
7005 | friend class T::C; | |
7006 | ||
7007 | when dependent_type_p is false or | |
7008 | ||
7009 | template <class U> friend class T::C; | |
7010 | ||
7011 | otherwise. */ | |
b939a023 | 7012 | friend_type = tsubst (friend_type, args, |
23fca1f5 | 7013 | tf_warning_or_error, NULL_TREE); |
b939a023 KL |
7014 | /* Bump processing_template_decl for correct |
7015 | dependent_type_p calculation. */ | |
7016 | ++processing_template_decl; | |
7017 | if (dependent_type_p (friend_type)) | |
7018 | adjust_processing_template_decl = true; | |
7019 | --processing_template_decl; | |
7020 | } | |
5a24482e KL |
7021 | else if (!CLASSTYPE_USE_TEMPLATE (friend_type) |
7022 | && hidden_name_p (TYPE_NAME (friend_type))) | |
7088fca9 | 7023 | { |
5a24482e KL |
7024 | /* friend class C; |
7025 | ||
7026 | where C hasn't been declared yet. Let's lookup name | |
7027 | from namespace scope directly, bypassing any name that | |
7028 | come from dependent base class. */ | |
7088fca9 KL |
7029 | tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type)); |
7030 | ||
7031 | /* The call to xref_tag_from_type does injection for friend | |
7032 | classes. */ | |
7033 | push_nested_namespace (ns); | |
c8094d83 MS |
7034 | friend_type = |
7035 | xref_tag_from_type (friend_type, NULL_TREE, | |
5a24482e | 7036 | /*tag_scope=*/ts_current); |
7088fca9 KL |
7037 | pop_nested_namespace (ns); |
7038 | } | |
5a24482e KL |
7039 | else if (uses_template_parms (friend_type)) |
7040 | /* friend class C<T>; */ | |
7041 | friend_type = tsubst (friend_type, args, | |
23fca1f5 | 7042 | tf_warning_or_error, NULL_TREE); |
5a24482e KL |
7043 | /* Otherwise it's |
7044 | ||
7045 | friend class C; | |
7046 | ||
7047 | where C is already declared or | |
7048 | ||
7049 | friend class C<int>; | |
7050 | ||
0cbd7506 | 7051 | We don't have to do anything in these cases. */ |
1aed5355 | 7052 | |
b939a023 | 7053 | if (adjust_processing_template_decl) |
7088fca9 KL |
7054 | /* Trick make_friend_class into realizing that the friend |
7055 | we're adding is a template, not an ordinary class. It's | |
7056 | important that we use make_friend_class since it will | |
7057 | perform some error-checking and output cross-reference | |
7058 | information. */ | |
7059 | ++processing_template_decl; | |
fc378698 | 7060 | |
b939a023 | 7061 | if (friend_type != error_mark_node) |
0cbd7506 | 7062 | make_friend_class (type, friend_type, /*complain=*/false); |
fc378698 | 7063 | |
b939a023 | 7064 | if (adjust_processing_template_decl) |
7088fca9 KL |
7065 | --processing_template_decl; |
7066 | } | |
7067 | else | |
9579624e KL |
7068 | { |
7069 | /* Build new DECL_FRIENDLIST. */ | |
7070 | tree r; | |
7071 | ||
6e432b31 KL |
7072 | /* The the file and line for this declaration, to |
7073 | assist in error message reporting. Since we | |
7074 | called push_tinst_level above, we don't need to | |
7075 | restore these. */ | |
7076 | input_location = DECL_SOURCE_LOCATION (t); | |
7077 | ||
9579624e | 7078 | if (TREE_CODE (t) == TEMPLATE_DECL) |
cad7e87b NS |
7079 | { |
7080 | ++processing_template_decl; | |
7081 | push_deferring_access_checks (dk_no_check); | |
7082 | } | |
c8094d83 | 7083 | |
9579624e | 7084 | r = tsubst_friend_function (t, args); |
9579624e | 7085 | add_friend (type, r, /*complain=*/false); |
cad7e87b NS |
7086 | if (TREE_CODE (t) == TEMPLATE_DECL) |
7087 | { | |
7088 | pop_deferring_access_checks (); | |
7089 | --processing_template_decl; | |
7090 | } | |
9579624e | 7091 | } |
7088fca9 KL |
7092 | } |
7093 | } | |
5566b478 | 7094 | |
61a127b3 MM |
7095 | /* Set the file and line number information to whatever is given for |
7096 | the class itself. This puts error messages involving generated | |
7097 | implicit functions at a predictable point, and the same point | |
7098 | that would be used for non-template classes. */ | |
f31686a3 | 7099 | input_location = DECL_SOURCE_LOCATION (typedecl); |
6de9cd9a | 7100 | |
61a127b3 | 7101 | unreverse_member_declarations (type); |
9f33663b | 7102 | finish_struct_1 (type); |
5524676d | 7103 | TYPE_BEING_DEFINED (type) = 0; |
8d08fdba | 7104 | |
9188c363 MM |
7105 | /* Now that the class is complete, instantiate default arguments for |
7106 | any member functions. We don't do this earlier because the | |
7107 | default arguments may reference members of the class. */ | |
7108 | if (!PRIMARY_TEMPLATE_P (template)) | |
7109 | for (t = TYPE_METHODS (type); t; t = TREE_CHAIN (t)) | |
c8094d83 | 7110 | if (TREE_CODE (t) == FUNCTION_DECL |
dc957d14 | 7111 | /* Implicitly generated member functions will not have template |
9188c363 MM |
7112 | information; they are not instantiations, but instead are |
7113 | created "fresh" for each instantiation. */ | |
7114 | && DECL_TEMPLATE_INFO (t)) | |
7115 | tsubst_default_arguments (t); | |
7116 | ||
56d0c6e3 | 7117 | pop_nested_class (); |
5566b478 | 7118 | pop_from_top_level (); |
78757caa | 7119 | pop_deferring_access_checks (); |
5566b478 MS |
7120 | pop_tinst_level (); |
7121 | ||
4684cd27 MM |
7122 | /* The vtable for a template class can be emitted in any translation |
7123 | unit in which the class is instantiated. When there is no key | |
7124 | method, however, finish_struct_1 will already have added TYPE to | |
7125 | the keyed_classes list. */ | |
7126 | if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type)) | |
9aad8f83 MA |
7127 | keyed_classes = tree_cons (NULL_TREE, type, keyed_classes); |
7128 | ||
5566b478 | 7129 | return type; |
8d08fdba MS |
7130 | } |
7131 | ||
00d3396f | 7132 | static tree |
a91db711 | 7133 | tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl) |
00d3396f | 7134 | { |
a91db711 | 7135 | tree r; |
c8094d83 | 7136 | |
a91db711 NS |
7137 | if (!t) |
7138 | r = t; | |
7139 | else if (TYPE_P (t)) | |
7140 | r = tsubst (t, args, complain, in_decl); | |
7141 | else | |
00d3396f | 7142 | { |
015c2c66 MM |
7143 | r = tsubst_expr (t, args, complain, in_decl, |
7144 | /*integral_constant_expression_p=*/true); | |
6f25cb35 | 7145 | r = fold_non_dependent_expr (r); |
bd83b409 | 7146 | } |
5d80a306 DG |
7147 | return r; |
7148 | } | |
7149 | ||
7150 | /* Substitute ARGS into T, which is an pack expansion | |
7151 | (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a | |
7152 | TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node | |
7153 | (if only a partial substitution could be performed) or | |
7154 | ERROR_MARK_NODE if there was an error. */ | |
7155 | tree | |
7156 | tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain, | |
7157 | tree in_decl) | |
7158 | { | |
7159 | tree pattern; | |
7160 | tree pack, packs = NULL_TREE, unsubstituted_packs = NULL_TREE; | |
7161 | tree first_arg_pack; int i, len = -1; | |
7162 | tree result; | |
7163 | int incomplete = 0; | |
7164 | ||
7165 | gcc_assert (PACK_EXPANSION_P (t)); | |
7166 | pattern = PACK_EXPANSION_PATTERN (t); | |
7167 | ||
7168 | /* Determine the argument packs that will instantiate the parameter | |
7169 | packs used in the expansion expression. While we're at it, | |
7170 | compute the number of arguments to be expanded and make sure it | |
7171 | is consistent. */ | |
7172 | for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack; | |
7173 | pack = TREE_CHAIN (pack)) | |
7174 | { | |
7175 | tree parm_pack = TREE_VALUE (pack); | |
7176 | tree arg_pack = NULL_TREE; | |
7177 | tree orig_arg = NULL_TREE; | |
7178 | ||
7179 | if (TREE_CODE (parm_pack) == PARM_DECL) | |
7180 | { | |
7181 | if (local_specializations) | |
7182 | arg_pack = retrieve_local_specialization (parm_pack); | |
7183 | } | |
7184 | else | |
7185 | { | |
7186 | int level, idx, levels; | |
7187 | template_parm_level_and_index (parm_pack, &level, &idx); | |
7188 | ||
7189 | levels = TMPL_ARGS_DEPTH (args); | |
7190 | if (level <= levels) | |
7191 | arg_pack = TMPL_ARG (args, level, idx); | |
7192 | } | |
7193 | ||
7194 | orig_arg = arg_pack; | |
7195 | if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT) | |
7196 | arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack); | |
7197 | ||
d393153e DG |
7198 | if (arg_pack && !ARGUMENT_PACK_P (arg_pack)) |
7199 | /* This can only happen if we forget to expand an argument | |
7200 | pack somewhere else. Just return an error, silently. */ | |
7201 | { | |
7202 | result = make_tree_vec (1); | |
7203 | TREE_VEC_ELT (result, 0) = error_mark_node; | |
7204 | return result; | |
7205 | } | |
7206 | ||
88b82314 DG |
7207 | if (arg_pack |
7208 | && TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack)) == 1 | |
7209 | && PACK_EXPANSION_P (TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg_pack), 0))) | |
7210 | { | |
7211 | tree expansion = TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg_pack), 0); | |
7212 | tree pattern = PACK_EXPANSION_PATTERN (expansion); | |
7213 | if ((TYPE_P (pattern) && same_type_p (pattern, parm_pack)) | |
7214 | || (!TYPE_P (pattern) && cp_tree_equal (parm_pack, pattern))) | |
7215 | /* The argument pack that the parameter maps to is just an | |
7216 | expansion of the parameter itself, such as one would | |
7217 | find in the implicit typedef of a class inside the | |
7218 | class itself. Consider this parameter "unsubstituted", | |
7219 | so that we will maintain the outer pack expansion. */ | |
7220 | arg_pack = NULL_TREE; | |
7221 | } | |
7222 | ||
5d80a306 DG |
7223 | if (arg_pack) |
7224 | { | |
7225 | int my_len = | |
7226 | TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack)); | |
7227 | ||
7228 | /* It's all-or-nothing with incomplete argument packs. */ | |
7229 | if (incomplete && !ARGUMENT_PACK_INCOMPLETE_P (arg_pack)) | |
7230 | return error_mark_node; | |
7231 | ||
7232 | if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack)) | |
7233 | incomplete = 1; | |
7234 | ||
7235 | if (len < 0) | |
7236 | { | |
7237 | len = my_len; | |
7238 | first_arg_pack = arg_pack; | |
7239 | } | |
7240 | else if (len != my_len) | |
7241 | { | |
7242 | if (TREE_CODE (t) == TYPE_PACK_EXPANSION) | |
7243 | error ("mismatched argument pack lengths while expanding " | |
7244 | "%<%T%>", | |
7245 | pattern); | |
7246 | else | |
7247 | error ("mismatched argument pack lengths while expanding " | |
7248 | "%<%E%>", | |
7249 | pattern); | |
7250 | return error_mark_node; | |
7251 | } | |
7252 | ||
7253 | /* Keep track of the parameter packs and their corresponding | |
7254 | argument packs. */ | |
7255 | packs = tree_cons (parm_pack, arg_pack, packs); | |
7256 | TREE_TYPE (packs) = orig_arg; | |
7257 | } | |
7258 | else | |
7259 | /* We can't substitute for this parameter pack. */ | |
7260 | unsubstituted_packs = tree_cons (TREE_PURPOSE (pack), | |
7261 | TREE_VALUE (pack), | |
7262 | unsubstituted_packs); | |
7263 | } | |
7264 | ||
7265 | /* We cannot expand this expansion expression, because we don't have | |
7266 | all of the argument packs we need. Substitute into the pattern | |
7267 | and return a PACK_EXPANSION_*. The caller will need to deal with | |
7268 | that. */ | |
7269 | if (unsubstituted_packs) | |
7270 | return make_pack_expansion (tsubst (pattern, args, complain, | |
7271 | in_decl)); | |
7272 | ||
7273 | /* We could not find any argument packs that work. */ | |
7274 | if (len < 0) | |
7275 | return error_mark_node; | |
7276 | ||
7277 | /* For each argument in each argument pack, substitute into the | |
7278 | pattern. */ | |
7279 | result = make_tree_vec (len + incomplete); | |
7280 | for (i = 0; i < len + incomplete; ++i) | |
7281 | { | |
7282 | /* For parameter pack, change the substitution of the parameter | |
7283 | pack to the ith argument in its argument pack, then expand | |
7284 | the pattern. */ | |
7285 | for (pack = packs; pack; pack = TREE_CHAIN (pack)) | |
7286 | { | |
7287 | tree parm = TREE_PURPOSE (pack); | |
7288 | ||
7289 | if (TREE_CODE (parm) == PARM_DECL) | |
7290 | { | |
7291 | /* Select the Ith argument from the pack. */ | |
7292 | tree arg = make_node (ARGUMENT_PACK_SELECT); | |
7293 | ARGUMENT_PACK_SELECT_FROM_PACK (arg) = TREE_VALUE (pack); | |
7294 | ARGUMENT_PACK_SELECT_INDEX (arg) = i; | |
7295 | mark_used (parm); | |
7296 | register_local_specialization (arg, parm); | |
7297 | } | |
7298 | else | |
7299 | { | |
7300 | tree value = parm; | |
7301 | int idx, level; | |
7302 | template_parm_level_and_index (parm, &level, &idx); | |
7303 | ||
7304 | if (i < len) | |
7305 | { | |
7306 | /* Select the Ith argument from the pack. */ | |
7307 | value = make_node (ARGUMENT_PACK_SELECT); | |
7308 | ARGUMENT_PACK_SELECT_FROM_PACK (value) = TREE_VALUE (pack); | |
7309 | ARGUMENT_PACK_SELECT_INDEX (value) = i; | |
7310 | } | |
7311 | ||
7312 | /* Update the corresponding argument. */ | |
7313 | TMPL_ARG (args, level, idx) = value; | |
7314 | } | |
7315 | } | |
7316 | ||
7317 | /* Substitute into the PATTERN with the altered arguments. */ | |
7318 | if (TREE_CODE (t) == EXPR_PACK_EXPANSION) | |
7319 | TREE_VEC_ELT (result, i) = | |
7320 | tsubst_expr (pattern, args, complain, in_decl, | |
7321 | /*integral_constant_expression_p=*/false); | |
7322 | else | |
7323 | TREE_VEC_ELT (result, i) = tsubst (pattern, args, complain, in_decl); | |
7324 | ||
7325 | if (i == len) | |
7326 | /* When we have incomplete argument packs, the last "expanded" | |
7327 | result is itself a pack expansion, which allows us | |
7328 | to deduce more arguments. */ | |
7329 | TREE_VEC_ELT (result, i) = | |
7330 | make_pack_expansion (TREE_VEC_ELT (result, i)); | |
7331 | ||
7332 | if (TREE_VEC_ELT (result, i) == error_mark_node) | |
7333 | { | |
7334 | result = error_mark_node; | |
7335 | break; | |
7336 | } | |
7337 | } | |
7338 | ||
7339 | /* Update ARGS to restore the substitution from parameter packs to | |
7340 | their argument packs. */ | |
7341 | for (pack = packs; pack; pack = TREE_CHAIN (pack)) | |
7342 | { | |
7343 | tree parm = TREE_PURPOSE (pack); | |
7344 | ||
7345 | if (TREE_CODE (parm) == PARM_DECL) | |
7346 | register_local_specialization (TREE_TYPE (pack), parm); | |
7347 | else | |
7348 | { | |
7349 | int idx, level; | |
7350 | template_parm_level_and_index (parm, &level, &idx); | |
7351 | ||
7352 | /* Update the corresponding argument. */ | |
7353 | if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args)) | |
7354 | TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) = | |
7355 | TREE_TYPE (pack); | |
7356 | else | |
7357 | TREE_VEC_ELT (args, idx) = TREE_TYPE (pack); | |
7358 | } | |
7359 | } | |
7360 | ||
7361 | return result; | |
bd83b409 NS |
7362 | } |
7363 | ||
a91db711 | 7364 | /* Substitute ARGS into the vector or list of template arguments T. */ |
830bfa74 | 7365 | |
e9659ab0 | 7366 | static tree |
a91db711 | 7367 | tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl) |
830bfa74 | 7368 | { |
5d80a306 | 7369 | tree orig_t = t; |
bf12d54d | 7370 | int len = TREE_VEC_LENGTH (t); |
5d80a306 | 7371 | int need_new = 0, i, expanded_len_adjust = 0, out; |
67f5655f | 7372 | tree *elts = (tree *) alloca (len * sizeof (tree)); |
c8094d83 | 7373 | |
830bfa74 MM |
7374 | for (i = 0; i < len; i++) |
7375 | { | |
bf12d54d NS |
7376 | tree orig_arg = TREE_VEC_ELT (t, i); |
7377 | tree new_arg; | |
a91db711 | 7378 | |
bf12d54d NS |
7379 | if (TREE_CODE (orig_arg) == TREE_VEC) |
7380 | new_arg = tsubst_template_args (orig_arg, args, complain, in_decl); | |
5d80a306 DG |
7381 | else if (PACK_EXPANSION_P (orig_arg)) |
7382 | { | |
7383 | /* Substitute into an expansion expression. */ | |
7384 | new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl); | |
7385 | ||
7386 | if (TREE_CODE (new_arg) == TREE_VEC) | |
7387 | /* Add to the expanded length adjustment the number of | |
7388 | expanded arguments. We subtract one from this | |
7389 | measurement, because the argument pack expression | |
7390 | itself is already counted as 1 in | |
7391 | LEN. EXPANDED_LEN_ADJUST can actually be negative, if | |
7392 | the argument pack is empty. */ | |
7393 | expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1; | |
7394 | } | |
7395 | else if (ARGUMENT_PACK_P (orig_arg)) | |
7396 | { | |
7397 | /* Substitute into each of the arguments. */ | |
7398 | new_arg = make_node (TREE_CODE (orig_arg)); | |
7399 | ||
7400 | SET_ARGUMENT_PACK_ARGS ( | |
7401 | new_arg, | |
7402 | tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg), | |
7403 | args, complain, in_decl)); | |
7404 | ||
7405 | if (ARGUMENT_PACK_ARGS (new_arg) == error_mark_node) | |
7406 | new_arg = error_mark_node; | |
7407 | ||
7408 | if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK) { | |
7409 | TREE_TYPE (new_arg) = tsubst (TREE_TYPE (orig_arg), args, | |
7410 | complain, in_decl); | |
7411 | TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg); | |
7412 | ||
7413 | if (TREE_TYPE (new_arg) == error_mark_node) | |
7414 | new_arg = error_mark_node; | |
7415 | } | |
7416 | } | |
830bfa74 | 7417 | else |
a91db711 | 7418 | new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl); |
c8094d83 | 7419 | |
a91db711 | 7420 | if (new_arg == error_mark_node) |
08e72a19 JM |
7421 | return error_mark_node; |
7422 | ||
a91db711 NS |
7423 | elts[i] = new_arg; |
7424 | if (new_arg != orig_arg) | |
830bfa74 MM |
7425 | need_new = 1; |
7426 | } | |
c8094d83 | 7427 | |
830bfa74 MM |
7428 | if (!need_new) |
7429 | return t; | |
a91db711 | 7430 | |
5d80a306 DG |
7431 | /* Make space for the expanded arguments coming from template |
7432 | argument packs. */ | |
7433 | t = make_tree_vec (len + expanded_len_adjust); | |
7434 | for (i = 0, out = 0; i < len; i++) | |
7435 | { | |
7436 | if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i)) | |
7437 | || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i))) | |
7438 | && TREE_CODE (elts[i]) == TREE_VEC) | |
7439 | { | |
7440 | int idx; | |
7441 | ||
7442 | /* Now expand the template argument pack "in place". */ | |
7443 | for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++) | |
7444 | TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx); | |
7445 | } | |
7446 | else | |
7447 | { | |
7448 | TREE_VEC_ELT (t, out) = elts[i]; | |
7449 | out++; | |
7450 | } | |
7451 | } | |
c8094d83 | 7452 | |
830bfa74 MM |
7453 | return t; |
7454 | } | |
7455 | ||
36a117a5 MM |
7456 | /* Return the result of substituting ARGS into the template parameters |
7457 | given by PARMS. If there are m levels of ARGS and m + n levels of | |
7458 | PARMS, then the result will contain n levels of PARMS. For | |
7459 | example, if PARMS is `template <class T> template <class U> | |
7460 | template <T*, U, class V>' and ARGS is {{int}, {double}} then the | |
7461 | result will be `template <int*, double, class V>'. */ | |
7462 | ||
e9659ab0 | 7463 | static tree |
3a978d72 | 7464 | tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain) |
36a117a5 | 7465 | { |
f71f87f9 MM |
7466 | tree r = NULL_TREE; |
7467 | tree* new_parms; | |
36a117a5 | 7468 | |
8ca4bf25 MM |
7469 | /* When substituting into a template, we must set |
7470 | PROCESSING_TEMPLATE_DECL as the template parameters may be | |
7471 | dependent if they are based on one-another, and the dependency | |
7472 | predicates are short-circuit outside of templates. */ | |
7473 | ++processing_template_decl; | |
7474 | ||
36a117a5 MM |
7475 | for (new_parms = &r; |
7476 | TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args); | |
7477 | new_parms = &(TREE_CHAIN (*new_parms)), | |
7478 | parms = TREE_CHAIN (parms)) | |
7479 | { | |
c8094d83 | 7480 | tree new_vec = |
36a117a5 MM |
7481 | make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms))); |
7482 | int i; | |
c8094d83 | 7483 | |
36a117a5 MM |
7484 | for (i = 0; i < TREE_VEC_LENGTH (new_vec); ++i) |
7485 | { | |
42b304f1 LM |
7486 | tree tuple; |
7487 | tree default_value; | |
7488 | tree parm_decl; | |
7489 | ||
7490 | if (parms == error_mark_node) | |
7491 | continue; | |
7492 | ||
7493 | tuple = TREE_VEC_ELT (TREE_VALUE (parms), i); | |
7494 | ||
7495 | if (tuple == error_mark_node) | |
7496 | continue; | |
7497 | ||
7498 | default_value = TREE_PURPOSE (tuple); | |
7499 | parm_decl = TREE_VALUE (tuple); | |
833aa4c4 NS |
7500 | |
7501 | parm_decl = tsubst (parm_decl, args, complain, NULL_TREE); | |
a207780f VR |
7502 | if (TREE_CODE (parm_decl) == PARM_DECL |
7503 | && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain)) | |
7504 | parm_decl = error_mark_node; | |
a91db711 NS |
7505 | default_value = tsubst_template_arg (default_value, args, |
7506 | complain, NULL_TREE); | |
c8094d83 | 7507 | |
a91db711 | 7508 | tuple = build_tree_list (default_value, parm_decl); |
833aa4c4 | 7509 | TREE_VEC_ELT (new_vec, i) = tuple; |
36a117a5 | 7510 | } |
c8094d83 MS |
7511 | |
7512 | *new_parms = | |
7513 | tree_cons (size_int (TMPL_PARMS_DEPTH (parms) | |
4890c2f4 | 7514 | - TMPL_ARGS_DEPTH (args)), |
36a117a5 MM |
7515 | new_vec, NULL_TREE); |
7516 | } | |
7517 | ||
8ca4bf25 MM |
7518 | --processing_template_decl; |
7519 | ||
36a117a5 MM |
7520 | return r; |
7521 | } | |
7522 | ||
ed44da02 MM |
7523 | /* Substitute the ARGS into the indicated aggregate (or enumeration) |
7524 | type T. If T is not an aggregate or enumeration type, it is | |
7525 | handled as if by tsubst. IN_DECL is as for tsubst. If | |
838dfd8a | 7526 | ENTERING_SCOPE is nonzero, T is the context for a template which |
dc957d14 | 7527 | we are presently tsubst'ing. Return the substituted value. */ |
36a117a5 | 7528 | |
e9659ab0 | 7529 | static tree |
c8094d83 | 7530 | tsubst_aggr_type (tree t, |
0cbd7506 MS |
7531 | tree args, |
7532 | tsubst_flags_t complain, | |
7533 | tree in_decl, | |
7534 | int entering_scope) | |
36a117a5 MM |
7535 | { |
7536 | if (t == NULL_TREE) | |
7537 | return NULL_TREE; | |
7538 | ||
7539 | switch (TREE_CODE (t)) | |
7540 | { | |
7541 | case RECORD_TYPE: | |
7542 | if (TYPE_PTRMEMFUNC_P (t)) | |
46c895ac | 7543 | return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl); |
36a117a5 | 7544 | |
f4f206f4 | 7545 | /* Else fall through. */ |
ed44da02 | 7546 | case ENUMERAL_TYPE: |
36a117a5 | 7547 | case UNION_TYPE: |
5db698f6 | 7548 | if (TYPE_TEMPLATE_INFO (t)) |
36a117a5 MM |
7549 | { |
7550 | tree argvec; | |
7551 | tree context; | |
7552 | tree r; | |
3489ea0c MM |
7553 | bool saved_skip_evaluation; |
7554 | ||
7555 | /* In "sizeof(X<I>)" we need to evaluate "I". */ | |
7556 | saved_skip_evaluation = skip_evaluation; | |
7557 | skip_evaluation = false; | |
36a117a5 MM |
7558 | |
7559 | /* First, determine the context for the type we are looking | |
7560 | up. */ | |
4f7847ca NS |
7561 | context = TYPE_CONTEXT (t); |
7562 | if (context) | |
7563 | context = tsubst_aggr_type (context, args, complain, | |
36a117a5 | 7564 | in_decl, /*entering_scope=*/1); |
36a117a5 MM |
7565 | |
7566 | /* Then, figure out what arguments are appropriate for the | |
7567 | type we are trying to find. For example, given: | |
7568 | ||
7569 | template <class T> struct S; | |
7570 | template <class T, class U> void f(T, U) { S<U> su; } | |
7571 | ||
7572 | and supposing that we are instantiating f<int, double>, | |
7573 | then our ARGS will be {int, double}, but, when looking up | |
7574 | S we only want {double}. */ | |
a91db711 NS |
7575 | argvec = tsubst_template_args (TYPE_TI_ARGS (t), args, |
7576 | complain, in_decl); | |
08e72a19 | 7577 | if (argvec == error_mark_node) |
3489ea0c MM |
7578 | r = error_mark_node; |
7579 | else | |
7580 | { | |
7581 | r = lookup_template_class (t, argvec, in_decl, context, | |
7582 | entering_scope, complain); | |
7583 | r = cp_build_qualified_type_real (r, TYPE_QUALS (t), complain); | |
7584 | } | |
3db45ab5 | 7585 | |
3489ea0c | 7586 | skip_evaluation = saved_skip_evaluation; |
36a117a5 | 7587 | |
3489ea0c | 7588 | return r; |
36a117a5 | 7589 | } |
c8094d83 | 7590 | else |
36a117a5 MM |
7591 | /* This is not a template type, so there's nothing to do. */ |
7592 | return t; | |
7593 | ||
7594 | default: | |
4393e105 | 7595 | return tsubst (t, args, complain, in_decl); |
36a117a5 MM |
7596 | } |
7597 | } | |
7598 | ||
9188c363 MM |
7599 | /* Substitute into the default argument ARG (a default argument for |
7600 | FN), which has the indicated TYPE. */ | |
7601 | ||
7602 | tree | |
3a978d72 | 7603 | tsubst_default_argument (tree fn, tree type, tree arg) |
9188c363 | 7604 | { |
2436b51f MM |
7605 | tree saved_class_ptr = NULL_TREE; |
7606 | tree saved_class_ref = NULL_TREE; | |
7607 | ||
9188c363 MM |
7608 | /* This default argument came from a template. Instantiate the |
7609 | default argument here, not in tsubst. In the case of | |
c8094d83 MS |
7610 | something like: |
7611 | ||
9188c363 MM |
7612 | template <class T> |
7613 | struct S { | |
7614 | static T t(); | |
7615 | void f(T = t()); | |
7616 | }; | |
c8094d83 | 7617 | |
9188c363 | 7618 | we must be careful to do name lookup in the scope of S<T>, |
d5a10cf0 | 7619 | rather than in the current class. */ |
2b59fc25 | 7620 | push_access_scope (fn); |
2436b51f MM |
7621 | /* The "this" pointer is not valid in a default argument. */ |
7622 | if (cfun) | |
7623 | { | |
7624 | saved_class_ptr = current_class_ptr; | |
7625 | cp_function_chain->x_current_class_ptr = NULL_TREE; | |
7626 | saved_class_ref = current_class_ref; | |
7627 | cp_function_chain->x_current_class_ref = NULL_TREE; | |
7628 | } | |
9188c363 | 7629 | |
d5a10cf0 | 7630 | push_deferring_access_checks(dk_no_deferred); |
5a8613b2 MM |
7631 | /* The default argument expression may cause implicitly defined |
7632 | member functions to be synthesized, which will result in garbage | |
7633 | collection. We must treat this situation as if we were within | |
7634 | the body of function so as to avoid collecting live data on the | |
7635 | stack. */ | |
7636 | ++function_depth; | |
c2ea3a40 | 7637 | arg = tsubst_expr (arg, DECL_TI_ARGS (fn), |
015c2c66 MM |
7638 | tf_warning_or_error, NULL_TREE, |
7639 | /*integral_constant_expression_p=*/false); | |
5a8613b2 | 7640 | --function_depth; |
d5a10cf0 MM |
7641 | pop_deferring_access_checks(); |
7642 | ||
2436b51f MM |
7643 | /* Restore the "this" pointer. */ |
7644 | if (cfun) | |
7645 | { | |
7646 | cp_function_chain->x_current_class_ptr = saved_class_ptr; | |
7647 | cp_function_chain->x_current_class_ref = saved_class_ref; | |
7648 | } | |
7649 | ||
2b59fc25 | 7650 | pop_access_scope (fn); |
9188c363 MM |
7651 | |
7652 | /* Make sure the default argument is reasonable. */ | |
7653 | arg = check_default_argument (type, arg); | |
7654 | ||
7655 | return arg; | |
7656 | } | |
7657 | ||
7658 | /* Substitute into all the default arguments for FN. */ | |
7659 | ||
7660 | static void | |
3a978d72 | 7661 | tsubst_default_arguments (tree fn) |
9188c363 MM |
7662 | { |
7663 | tree arg; | |
7664 | tree tmpl_args; | |
7665 | ||
7666 | tmpl_args = DECL_TI_ARGS (fn); | |
7667 | ||
7668 | /* If this function is not yet instantiated, we certainly don't need | |
7669 | its default arguments. */ | |
7670 | if (uses_template_parms (tmpl_args)) | |
7671 | return; | |
7672 | ||
c8094d83 MS |
7673 | for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn)); |
7674 | arg; | |
9188c363 MM |
7675 | arg = TREE_CHAIN (arg)) |
7676 | if (TREE_PURPOSE (arg)) | |
c8094d83 | 7677 | TREE_PURPOSE (arg) = tsubst_default_argument (fn, |
9188c363 MM |
7678 | TREE_VALUE (arg), |
7679 | TREE_PURPOSE (arg)); | |
7680 | } | |
7681 | ||
fc6a28d7 MM |
7682 | /* Substitute the ARGS into the T, which is a _DECL. Return the |
7683 | result of the substitution. Issue error and warning messages under | |
7684 | control of COMPLAIN. */ | |
00d3396f | 7685 | |
e9659ab0 | 7686 | static tree |
fc6a28d7 | 7687 | tsubst_decl (tree t, tree args, tsubst_flags_t complain) |
8d08fdba | 7688 | { |
82a98427 | 7689 | location_t saved_loc; |
b370501f | 7690 | tree r = NULL_TREE; |
4b2811e9 | 7691 | tree in_decl = t; |
830bfa74 | 7692 | |
ae58fa02 | 7693 | /* Set the filename and linenumber to improve error-reporting. */ |
82a98427 | 7694 | saved_loc = input_location; |
f31686a3 | 7695 | input_location = DECL_SOURCE_LOCATION (t); |
b7484fbe | 7696 | |
8d08fdba MS |
7697 | switch (TREE_CODE (t)) |
7698 | { | |
98c1c668 JM |
7699 | case TEMPLATE_DECL: |
7700 | { | |
28e42b7e KL |
7701 | /* We can get here when processing a member function template, |
7702 | member class template, and template template parameter of | |
7703 | a template class. */ | |
98c1c668 | 7704 | tree decl = DECL_TEMPLATE_RESULT (t); |
386b8a85 | 7705 | tree spec; |
28e42b7e KL |
7706 | tree tmpl_args; |
7707 | tree full_args; | |
98c1c668 | 7708 | |
28e42b7e | 7709 | if (DECL_TEMPLATE_TEMPLATE_PARM_P (t)) |
db2767b6 | 7710 | { |
28e42b7e KL |
7711 | /* Template template parameter is treated here. */ |
7712 | tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl); | |
7713 | if (new_type == error_mark_node) | |
7714 | return error_mark_node; | |
36a117a5 | 7715 | |
28e42b7e KL |
7716 | r = copy_decl (t); |
7717 | TREE_CHAIN (r) = NULL_TREE; | |
7718 | TREE_TYPE (r) = new_type; | |
7719 | DECL_TEMPLATE_RESULT (r) | |
7720 | = build_decl (TYPE_DECL, DECL_NAME (decl), new_type); | |
c8094d83 | 7721 | DECL_TEMPLATE_PARMS (r) |
28e42b7e KL |
7722 | = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args, |
7723 | complain); | |
7724 | TYPE_NAME (new_type) = r; | |
7725 | break; | |
7726 | } | |
36a117a5 | 7727 | |
28e42b7e KL |
7728 | /* We might already have an instance of this template. |
7729 | The ARGS are for the surrounding class type, so the | |
7730 | full args contain the tsubst'd args for the context, | |
7731 | plus the innermost args from the template decl. */ | |
c8094d83 | 7732 | tmpl_args = DECL_CLASS_TEMPLATE_P (t) |
28e42b7e KL |
7733 | ? CLASSTYPE_TI_ARGS (TREE_TYPE (t)) |
7734 | : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t)); | |
8ca4bf25 MM |
7735 | /* Because this is a template, the arguments will still be |
7736 | dependent, even after substitution. If | |
7737 | PROCESSING_TEMPLATE_DECL is not set, the dependency | |
7738 | predicates will short-circuit. */ | |
7739 | ++processing_template_decl; | |
28e42b7e KL |
7740 | full_args = tsubst_template_args (tmpl_args, args, |
7741 | complain, in_decl); | |
8ca4bf25 | 7742 | --processing_template_decl; |
bf2f7328 VR |
7743 | if (full_args == error_mark_node) |
7744 | return error_mark_node; | |
28e42b7e KL |
7745 | |
7746 | /* tsubst_template_args doesn't copy the vector if | |
7747 | nothing changed. But, *something* should have | |
7748 | changed. */ | |
7749 | gcc_assert (full_args != tmpl_args); | |
7750 | ||
7751 | spec = retrieve_specialization (t, full_args, | |
7752 | /*class_specializations_p=*/true); | |
7753 | if (spec != NULL_TREE) | |
7754 | { | |
7755 | r = spec; | |
7756 | break; | |
db2767b6 | 7757 | } |
98c1c668 JM |
7758 | |
7759 | /* Make a new template decl. It will be similar to the | |
c8094d83 | 7760 | original, but will record the current template arguments. |
98c1c668 JM |
7761 | We also create a new function declaration, which is just |
7762 | like the old one, but points to this new template, rather | |
7763 | than the old one. */ | |
0acf7199 | 7764 | r = copy_decl (t); |
50bc768d | 7765 | gcc_assert (DECL_LANG_SPECIFIC (r) != 0); |
ae58fa02 | 7766 | TREE_CHAIN (r) = NULL_TREE; |
db2767b6 | 7767 | |
ae58fa02 | 7768 | DECL_TEMPLATE_INFO (r) = build_tree_list (t, args); |
93cdc044 JM |
7769 | |
7770 | if (TREE_CODE (decl) == TYPE_DECL) | |
7771 | { | |
8ca4bf25 MM |
7772 | tree new_type; |
7773 | ++processing_template_decl; | |
7774 | new_type = tsubst (TREE_TYPE (t), args, complain, in_decl); | |
3db45ab5 | 7775 | --processing_template_decl; |
2620d095 KL |
7776 | if (new_type == error_mark_node) |
7777 | return error_mark_node; | |
7778 | ||
ae58fa02 MM |
7779 | TREE_TYPE (r) = new_type; |
7780 | CLASSTYPE_TI_TEMPLATE (new_type) = r; | |
17aec3eb | 7781 | DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type); |
ae58fa02 | 7782 | DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type); |
8ca4bf25 | 7783 | DECL_CONTEXT (r) = TYPE_CONTEXT (new_type); |
93cdc044 JM |
7784 | } |
7785 | else | |
7786 | { | |
8ca4bf25 MM |
7787 | tree new_decl; |
7788 | ++processing_template_decl; | |
7789 | new_decl = tsubst (decl, args, complain, in_decl); | |
7790 | --processing_template_decl; | |
caec1dc0 KL |
7791 | if (new_decl == error_mark_node) |
7792 | return error_mark_node; | |
17aec3eb RK |
7793 | |
7794 | DECL_TEMPLATE_RESULT (r) = new_decl; | |
ae58fa02 MM |
7795 | DECL_TI_TEMPLATE (new_decl) = r; |
7796 | TREE_TYPE (r) = TREE_TYPE (new_decl); | |
7797 | DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl); | |
3db45ab5 | 7798 | DECL_CONTEXT (r) = DECL_CONTEXT (new_decl); |
93cdc044 JM |
7799 | } |
7800 | ||
ae58fa02 MM |
7801 | SET_DECL_IMPLICIT_INSTANTIATION (r); |
7802 | DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE; | |
7803 | DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE; | |
98c1c668 JM |
7804 | |
7805 | /* The template parameters for this new template are all the | |
7806 | template parameters for the old template, except the | |
c6002625 | 7807 | outermost level of parameters. */ |
c8094d83 | 7808 | DECL_TEMPLATE_PARMS (r) |
4393e105 | 7809 | = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args, |
c2ea3a40 | 7810 | complain); |
98c1c668 | 7811 | |
93cdc044 | 7812 | if (PRIMARY_TEMPLATE_P (t)) |
ae58fa02 | 7813 | DECL_PRIMARY_TEMPLATE (r) = r; |
93cdc044 | 7814 | |
8c6ab2db NS |
7815 | if (TREE_CODE (decl) != TYPE_DECL) |
7816 | /* Record this non-type partial instantiation. */ | |
c8094d83 | 7817 | register_specialization (r, t, |
d63d5d0c ILT |
7818 | DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)), |
7819 | false); | |
98c1c668 | 7820 | } |
ae58fa02 | 7821 | break; |
8d08fdba MS |
7822 | |
7823 | case FUNCTION_DECL: | |
7824 | { | |
386b8a85 | 7825 | tree ctx; |
87603ed0 | 7826 | tree argvec = NULL_TREE; |
cf38f48a | 7827 | tree *friends; |
36a117a5 | 7828 | tree gen_tmpl; |
fc6a28d7 | 7829 | tree type; |
5566b478 | 7830 | int member; |
d8c4447d MM |
7831 | int args_depth; |
7832 | int parms_depth; | |
5566b478 | 7833 | |
36a117a5 | 7834 | /* Nobody should be tsubst'ing into non-template functions. */ |
50bc768d | 7835 | gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE); |
36a117a5 MM |
7836 | |
7837 | if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL) | |
7838 | { | |
7839 | tree spec; | |
00cf3e31 MM |
7840 | bool dependent_p; |
7841 | ||
7842 | /* If T is not dependent, just return it. We have to | |
7843 | increment PROCESSING_TEMPLATE_DECL because | |
7844 | value_dependent_expression_p assumes that nothing is | |
7845 | dependent when PROCESSING_TEMPLATE_DECL is zero. */ | |
7846 | ++processing_template_decl; | |
7847 | dependent_p = value_dependent_expression_p (t); | |
7848 | --processing_template_decl; | |
7849 | if (!dependent_p) | |
7850 | return t; | |
36a117a5 MM |
7851 | |
7852 | /* Calculate the most general template of which R is a | |
7853 | specialization, and the complete set of arguments used to | |
7854 | specialize R. */ | |
7855 | gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t)); | |
c8094d83 | 7856 | argvec = tsubst_template_args (DECL_TI_ARGS |
a91db711 | 7857 | (DECL_TEMPLATE_RESULT (gen_tmpl)), |
c8094d83 | 7858 | args, complain, in_decl); |
36a117a5 MM |
7859 | |
7860 | /* Check to see if we already have this specialization. */ | |
c7222c02 MM |
7861 | spec = retrieve_specialization (gen_tmpl, argvec, |
7862 | /*class_specializations_p=*/false); | |
7ddedda4 | 7863 | |
36a117a5 | 7864 | if (spec) |
ae58fa02 MM |
7865 | { |
7866 | r = spec; | |
7867 | break; | |
7868 | } | |
d8c4447d | 7869 | |
f9a7ae04 MM |
7870 | /* We can see more levels of arguments than parameters if |
7871 | there was a specialization of a member template, like | |
7872 | this: | |
7873 | ||
0cbd7506 | 7874 | template <class T> struct S { template <class U> void f(); } |
c8094d83 | 7875 | template <> template <class U> void S<int>::f(U); |
f9a7ae04 | 7876 | |
dc957d14 | 7877 | Here, we'll be substituting into the specialization, |
f9a7ae04 MM |
7878 | because that's where we can find the code we actually |
7879 | want to generate, but we'll have enough arguments for | |
c8094d83 | 7880 | the most general template. |
f9a7ae04 MM |
7881 | |
7882 | We also deal with the peculiar case: | |
d8c4447d | 7883 | |
c8094d83 | 7884 | template <class T> struct S { |
d8c4447d MM |
7885 | template <class U> friend void f(); |
7886 | }; | |
74b846e0 | 7887 | template <class U> void f() {} |
d8c4447d MM |
7888 | template S<int>; |
7889 | template void f<double>(); | |
7890 | ||
7891 | Here, the ARGS for the instantiation of will be {int, | |
7892 | double}. But, we only need as many ARGS as there are | |
7893 | levels of template parameters in CODE_PATTERN. We are | |
7894 | careful not to get fooled into reducing the ARGS in | |
7895 | situations like: | |
7896 | ||
7897 | template <class T> struct S { template <class U> void f(U); } | |
7898 | template <class T> template <> void S<T>::f(int) {} | |
7899 | ||
7900 | which we can spot because the pattern will be a | |
7901 | specialization in this case. */ | |
7902 | args_depth = TMPL_ARGS_DEPTH (args); | |
c8094d83 MS |
7903 | parms_depth = |
7904 | TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t))); | |
d8c4447d MM |
7905 | if (args_depth > parms_depth |
7906 | && !DECL_TEMPLATE_SPECIALIZATION (t)) | |
f9a7ae04 | 7907 | args = get_innermost_template_args (args, parms_depth); |
36a117a5 MM |
7908 | } |
7909 | else | |
7910 | { | |
7911 | /* This special case arises when we have something like this: | |
7912 | ||
0cbd7506 | 7913 | template <class T> struct S { |
c8094d83 | 7914 | friend void f<int>(int, double); |
36a117a5 MM |
7915 | }; |
7916 | ||
7917 | Here, the DECL_TI_TEMPLATE for the friend declaration | |
10b1d5e7 MM |
7918 | will be an IDENTIFIER_NODE. We are being called from |
7919 | tsubst_friend_function, and we want only to create a | |
7920 | new decl (R) with appropriate types so that we can call | |
7921 | determine_specialization. */ | |
36a117a5 MM |
7922 | gen_tmpl = NULL_TREE; |
7923 | } | |
7924 | ||
6eb3bb27 | 7925 | if (DECL_CLASS_SCOPE_P (t)) |
8d08fdba | 7926 | { |
5566b478 MS |
7927 | if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t))) |
7928 | member = 2; | |
7929 | else | |
7930 | member = 1; | |
c8094d83 | 7931 | ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, |
8c6ab2db | 7932 | complain, t, /*entering_scope=*/1); |
5566b478 MS |
7933 | } |
7934 | else | |
7935 | { | |
7936 | member = 0; | |
4f1c5b7d | 7937 | ctx = DECL_CONTEXT (t); |
5566b478 | 7938 | } |
fc6a28d7 | 7939 | type = tsubst (TREE_TYPE (t), args, complain, in_decl); |
b3da7bb1 MM |
7940 | if (type == error_mark_node) |
7941 | return error_mark_node; | |
8d08fdba | 7942 | |
5566b478 | 7943 | /* We do NOT check for matching decls pushed separately at this |
0cbd7506 MS |
7944 | point, as they may not represent instantiations of this |
7945 | template, and in any case are considered separate under the | |
7946 | discrete model. */ | |
0acf7199 | 7947 | r = copy_decl (t); |
e1467ff2 | 7948 | DECL_USE_TEMPLATE (r) = 0; |
5566b478 | 7949 | TREE_TYPE (r) = type; |
92643fea MM |
7950 | /* Clear out the mangled name and RTL for the instantiation. */ |
7951 | SET_DECL_ASSEMBLER_NAME (r, NULL_TREE); | |
7952 | SET_DECL_RTL (r, NULL_RTX); | |
5bd61841 | 7953 | DECL_INITIAL (r) = NULL_TREE; |
4f1c5b7d | 7954 | DECL_CONTEXT (r) = ctx; |
5566b478 | 7955 | |
c8094d83 | 7956 | if (member && DECL_CONV_FN_P (r)) |
1f84ec23 MM |
7957 | /* Type-conversion operator. Reconstruct the name, in |
7958 | case it's the name of one of the template's parameters. */ | |
7959 | DECL_NAME (r) = mangle_conv_op_name_for_type (TREE_TYPE (type)); | |
5566b478 | 7960 | |
4393e105 | 7961 | DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args, |
c2ea3a40 | 7962 | complain, t); |
477f6664 | 7963 | DECL_RESULT (r) = NULL_TREE; |
711734a9 JM |
7964 | |
7965 | TREE_STATIC (r) = 0; | |
7966 | TREE_PUBLIC (r) = TREE_PUBLIC (t); | |
7967 | DECL_EXTERNAL (r) = 1; | |
4684cd27 MM |
7968 | /* If this is an instantiation of a function with internal |
7969 | linkage, we already know what object file linkage will be | |
7970 | assigned to the instantiation. */ | |
7971 | DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r); | |
711734a9 JM |
7972 | DECL_DEFER_OUTPUT (r) = 0; |
7973 | TREE_CHAIN (r) = NULL_TREE; | |
7974 | DECL_PENDING_INLINE_INFO (r) = 0; | |
59026e79 | 7975 | DECL_PENDING_INLINE_P (r) = 0; |
655dc6ee | 7976 | DECL_SAVED_TREE (r) = NULL_TREE; |
711734a9 | 7977 | TREE_USED (r) = 0; |
db9b2174 MM |
7978 | if (DECL_CLONED_FUNCTION (r)) |
7979 | { | |
7980 | DECL_CLONED_FUNCTION (r) = tsubst (DECL_CLONED_FUNCTION (t), | |
c2ea3a40 | 7981 | args, complain, t); |
db9b2174 MM |
7982 | TREE_CHAIN (r) = TREE_CHAIN (DECL_CLONED_FUNCTION (r)); |
7983 | TREE_CHAIN (DECL_CLONED_FUNCTION (r)) = r; | |
7984 | } | |
711734a9 | 7985 | |
92643fea MM |
7986 | /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do |
7987 | this in the special friend case mentioned above where | |
7988 | GEN_TMPL is NULL. */ | |
36a117a5 | 7989 | if (gen_tmpl) |
386b8a85 | 7990 | { |
c8094d83 | 7991 | DECL_TEMPLATE_INFO (r) |
e1b3e07d | 7992 | = tree_cons (gen_tmpl, argvec, NULL_TREE); |
36a117a5 | 7993 | SET_DECL_IMPLICIT_INSTANTIATION (r); |
d63d5d0c | 7994 | register_specialization (r, gen_tmpl, argvec, false); |
36a117a5 | 7995 | |
9188c363 MM |
7996 | /* We're not supposed to instantiate default arguments |
7997 | until they are called, for a template. But, for a | |
7998 | declaration like: | |
7999 | ||
0cbd7506 MS |
8000 | template <class T> void f () |
8001 | { extern void g(int i = T()); } | |
c8094d83 | 8002 | |
9188c363 MM |
8003 | we should do the substitution when the template is |
8004 | instantiated. We handle the member function case in | |
8005 | instantiate_class_template since the default arguments | |
8006 | might refer to other members of the class. */ | |
8007 | if (!member | |
8008 | && !PRIMARY_TEMPLATE_P (gen_tmpl) | |
8009 | && !uses_template_parms (argvec)) | |
8010 | tsubst_default_arguments (r); | |
386b8a85 | 8011 | } |
c43e95f8 MM |
8012 | else |
8013 | DECL_TEMPLATE_INFO (r) = NULL_TREE; | |
f181d4ae | 8014 | |
cf38f48a MM |
8015 | /* Copy the list of befriending classes. */ |
8016 | for (friends = &DECL_BEFRIENDING_CLASSES (r); | |
8017 | *friends; | |
c8094d83 | 8018 | friends = &TREE_CHAIN (*friends)) |
cf38f48a MM |
8019 | { |
8020 | *friends = copy_node (*friends); | |
8021 | TREE_VALUE (*friends) = tsubst (TREE_VALUE (*friends), | |
c2ea3a40 | 8022 | args, complain, |
cf38f48a MM |
8023 | in_decl); |
8024 | } | |
8025 | ||
212e7048 | 8026 | if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r)) |
f181d4ae MM |
8027 | { |
8028 | maybe_retrofit_in_chrg (r); | |
212e7048 MM |
8029 | if (DECL_CONSTRUCTOR_P (r)) |
8030 | grok_ctor_properties (ctx, r); | |
2be678ff JM |
8031 | /* If this is an instantiation of a member template, clone it. |
8032 | If it isn't, that'll be handled by | |
8033 | clone_constructors_and_destructors. */ | |
5e818b93 | 8034 | if (PRIMARY_TEMPLATE_P (gen_tmpl)) |
271e6f02 | 8035 | clone_function_decl (r, /*update_method_vec_p=*/0); |
f181d4ae | 8036 | } |
398cd199 VR |
8037 | else if (IDENTIFIER_OPNAME_P (DECL_NAME (r)) |
8038 | && !grok_op_properties (r, (complain & tf_error) != 0)) | |
8039 | return error_mark_node; | |
c8b2e872 MM |
8040 | |
8041 | if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t)) | |
8042 | SET_DECL_FRIEND_CONTEXT (r, | |
8043 | tsubst (DECL_FRIEND_CONTEXT (t), | |
8044 | args, complain, in_decl)); | |
b9e75696 JM |
8045 | |
8046 | /* Possibly limit visibility based on template args. */ | |
8047 | DECL_VISIBILITY (r) = VISIBILITY_DEFAULT; | |
b70f0f48 JM |
8048 | if (DECL_VISIBILITY_SPECIFIED (t)) |
8049 | { | |
8050 | DECL_VISIBILITY_SPECIFIED (r) = 0; | |
8051 | DECL_ATTRIBUTES (r) | |
8052 | = remove_attribute ("visibility", DECL_ATTRIBUTES (r)); | |
8053 | } | |
b9e75696 | 8054 | determine_visibility (r); |
4f4141ff JM |
8055 | |
8056 | apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0, | |
8057 | args, complain, in_decl); | |
8d08fdba | 8058 | } |
ae58fa02 | 8059 | break; |
8d08fdba MS |
8060 | |
8061 | case PARM_DECL: | |
8062 | { | |
5d80a306 DG |
8063 | tree type = NULL_TREE; |
8064 | int i, len = 1; | |
8065 | tree expanded_types = NULL_TREE; | |
8066 | tree prev_r = NULL_TREE; | |
8067 | tree first_r = NULL_TREE; | |
1b8899d1 | 8068 | |
5d80a306 DG |
8069 | if (FUNCTION_PARAMETER_PACK_P (t)) |
8070 | { | |
8071 | /* If there is a local specialization that isn't a | |
8072 | parameter pack, it means that we're doing a "simple" | |
8073 | substitution from inside tsubst_pack_expansion. Just | |
a757b5c5 | 8074 | return the local specialization (which will be a single |
5d80a306 DG |
8075 | parm). */ |
8076 | tree spec = NULL_TREE; | |
8077 | if (local_specializations) | |
8078 | spec = retrieve_local_specialization (t); | |
8079 | if (spec | |
8080 | && TREE_CODE (spec) == PARM_DECL | |
8081 | && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION) | |
8082 | return spec; | |
8083 | ||
8084 | /* Expand the TYPE_PACK_EXPANSION that provides the types for | |
8085 | the parameters in this function parameter pack. */ | |
8086 | expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args, | |
8087 | complain, in_decl); | |
8088 | if (TREE_CODE (expanded_types) == TREE_VEC) | |
8089 | { | |
8090 | len = TREE_VEC_LENGTH (expanded_types); | |
8091 | ||
8092 | /* Zero-length parameter packs are boring. Just substitute | |
8093 | into the chain. */ | |
8094 | if (len == 0) | |
8095 | return tsubst (TREE_CHAIN (t), args, complain, | |
8096 | TREE_CHAIN (t)); | |
8097 | } | |
8098 | else | |
8099 | { | |
8100 | /* All we did was update the type. Make a note of that. */ | |
8101 | type = expanded_types; | |
8102 | expanded_types = NULL_TREE; | |
8103 | } | |
8104 | } | |
db2767b6 | 8105 | |
5d80a306 DG |
8106 | /* Loop through all of the parameter's we'll build. When T is |
8107 | a function parameter pack, LEN is the number of expanded | |
8108 | types in EXPANDED_TYPES; otherwise, LEN is 1. */ | |
8109 | r = NULL_TREE; | |
8110 | for (i = 0; i < len; ++i) | |
8111 | { | |
8112 | prev_r = r; | |
8113 | r = copy_node (t); | |
8114 | if (DECL_TEMPLATE_PARM_P (t)) | |
8115 | SET_DECL_TEMPLATE_PARM_P (r); | |
8116 | ||
8117 | if (expanded_types) | |
8118 | /* We're on the Ith parameter of the function parameter | |
8119 | pack. */ | |
8120 | { | |
8121 | /* Get the Ith type. */ | |
8122 | type = TREE_VEC_ELT (expanded_types, i); | |
8123 | ||
8124 | if (DECL_NAME (r)) | |
8125 | /* Rename the parameter to include the index. */ | |
8126 | DECL_NAME (r) = | |
8127 | make_ith_pack_parameter_name (DECL_NAME (r), i); | |
8128 | } | |
8129 | else if (!type) | |
8130 | /* We're dealing with a normal parameter. */ | |
8131 | type = tsubst (TREE_TYPE (t), args, complain, in_decl); | |
8132 | ||
8133 | type = type_decays_to (type); | |
8134 | TREE_TYPE (r) = type; | |
8135 | cp_apply_type_quals_to_decl (cp_type_quals (type), r); | |
8136 | ||
8137 | if (DECL_INITIAL (r)) | |
8138 | { | |
8139 | if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX) | |
8140 | DECL_INITIAL (r) = TREE_TYPE (r); | |
8141 | else | |
8142 | DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args, | |
8143 | complain, in_decl); | |
8144 | } | |
8145 | ||
8146 | DECL_CONTEXT (r) = NULL_TREE; | |
8147 | ||
8148 | if (!DECL_TEMPLATE_PARM_P (r)) | |
8149 | DECL_ARG_TYPE (r) = type_passed_as (type); | |
8150 | ||
4f4141ff JM |
8151 | apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0, |
8152 | args, complain, in_decl); | |
8153 | ||
5d80a306 DG |
8154 | /* Keep track of the first new parameter we |
8155 | generate. That's what will be returned to the | |
8156 | caller. */ | |
8157 | if (!first_r) | |
8158 | first_r = r; | |
8159 | ||
8160 | /* Build a proper chain of parameters when substituting | |
8161 | into a function parameter pack. */ | |
8162 | if (prev_r) | |
8163 | TREE_CHAIN (prev_r) = r; | |
8164 | } | |
8e51619a | 8165 | |
8d08fdba | 8166 | if (TREE_CHAIN (t)) |
4393e105 | 8167 | TREE_CHAIN (r) = tsubst (TREE_CHAIN (t), args, |
c2ea3a40 | 8168 | complain, TREE_CHAIN (t)); |
5d80a306 DG |
8169 | |
8170 | /* FIRST_R contains the start of the chain we've built. */ | |
8171 | r = first_r; | |
8d08fdba | 8172 | } |
ae58fa02 | 8173 | break; |
8d08fdba | 8174 | |
5566b478 MS |
8175 | case FIELD_DECL: |
8176 | { | |
fc6a28d7 MM |
8177 | tree type; |
8178 | ||
0acf7199 | 8179 | r = copy_decl (t); |
fc6a28d7 MM |
8180 | type = tsubst (TREE_TYPE (t), args, complain, in_decl); |
8181 | if (type == error_mark_node) | |
8182 | return error_mark_node; | |
1b8899d1 | 8183 | TREE_TYPE (r) = type; |
9804209d | 8184 | cp_apply_type_quals_to_decl (cp_type_quals (type), r); |
1b8899d1 | 8185 | |
015c2c66 MM |
8186 | /* DECL_INITIAL gives the number of bits in a bit-field. */ |
8187 | DECL_INITIAL (r) | |
8188 | = tsubst_expr (DECL_INITIAL (t), args, | |
8189 | complain, in_decl, | |
8190 | /*integral_constant_expression_p=*/true); | |
1b8899d1 MM |
8191 | /* We don't have to set DECL_CONTEXT here; it is set by |
8192 | finish_member_declaration. */ | |
5566b478 | 8193 | TREE_CHAIN (r) = NULL_TREE; |
c8094d83 | 8194 | if (VOID_TYPE_P (type)) |
dee15844 | 8195 | error ("instantiation of %q+D as type %qT", r, type); |
4f4141ff JM |
8196 | |
8197 | apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0, | |
8198 | args, complain, in_decl); | |
5566b478 | 8199 | } |
ae58fa02 | 8200 | break; |
5566b478 MS |
8201 | |
8202 | case USING_DECL: | |
98ed9dae NS |
8203 | /* We reach here only for member using decls. */ |
8204 | if (DECL_DEPENDENT_P (t)) | |
8205 | { | |
8206 | r = do_class_using_decl | |
8207 | (tsubst_copy (USING_DECL_SCOPE (t), args, complain, in_decl), | |
8208 | tsubst_copy (DECL_NAME (t), args, complain, in_decl)); | |
8209 | if (!r) | |
8210 | r = error_mark_node; | |
8211 | } | |
8212 | else | |
8213 | { | |
8214 | r = copy_node (t); | |
8215 | TREE_CHAIN (r) = NULL_TREE; | |
8216 | } | |
ae58fa02 | 8217 | break; |
5566b478 | 8218 | |
9188c363 | 8219 | case TYPE_DECL: |
5566b478 MS |
8220 | case VAR_DECL: |
8221 | { | |
1cea0434 KG |
8222 | tree argvec = NULL_TREE; |
8223 | tree gen_tmpl = NULL_TREE; | |
36a117a5 | 8224 | tree spec; |
1cea0434 | 8225 | tree tmpl = NULL_TREE; |
9188c363 | 8226 | tree ctx; |
fc6a28d7 | 8227 | tree type = NULL_TREE; |
f604fc3b | 8228 | bool local_p; |
9188c363 | 8229 | |
56d0c6e3 | 8230 | if (TREE_CODE (t) == TYPE_DECL |
a7f6bc8c | 8231 | && t == TYPE_MAIN_DECL (TREE_TYPE (t))) |
fc6a28d7 | 8232 | { |
a7f6bc8c JM |
8233 | /* If this is the canonical decl, we don't have to |
8234 | mess with instantiations, and often we can't (for | |
8235 | typename, template type parms and such). Note that | |
8236 | TYPE_NAME is not correct for the above test if | |
8237 | we've copied the type for a typedef. */ | |
fc6a28d7 | 8238 | type = tsubst (TREE_TYPE (t), args, complain, in_decl); |
c343d5a7 PC |
8239 | if (type == error_mark_node) |
8240 | return error_mark_node; | |
a7f6bc8c JM |
8241 | r = TYPE_NAME (type); |
8242 | break; | |
fc6a28d7 | 8243 | } |
c8094d83 | 8244 | |
f604fc3b MM |
8245 | /* Check to see if we already have the specialization we |
8246 | need. */ | |
8247 | spec = NULL_TREE; | |
8248 | if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t)) | |
8249 | { | |
8250 | /* T is a static data member or namespace-scope entity. | |
8251 | We have to substitute into namespace-scope variables | |
8252 | (even though such entities are never templates) because | |
8253 | of cases like: | |
8254 | ||
8255 | template <class T> void f() { extern T t; } | |
8256 | ||
8257 | where the entity referenced is not known until | |
8258 | instantiation time. */ | |
8259 | local_p = false; | |
8260 | ctx = DECL_CONTEXT (t); | |
8261 | if (DECL_CLASS_SCOPE_P (t)) | |
8262 | { | |
8263 | ctx = tsubst_aggr_type (ctx, args, | |
8264 | complain, | |
8265 | in_decl, /*entering_scope=*/1); | |
8266 | /* If CTX is unchanged, then T is in fact the | |
8267 | specialization we want. That situation occurs when | |
8268 | referencing a static data member within in its own | |
8269 | class. We can use pointer equality, rather than | |
8270 | same_type_p, because DECL_CONTEXT is always | |
8271 | canonical. */ | |
8272 | if (ctx == DECL_CONTEXT (t)) | |
8273 | spec = t; | |
8274 | } | |
5566b478 | 8275 | |
f604fc3b MM |
8276 | if (!spec) |
8277 | { | |
8278 | tmpl = DECL_TI_TEMPLATE (t); | |
8279 | gen_tmpl = most_general_template (tmpl); | |
8280 | argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl); | |
8281 | spec = (retrieve_specialization | |
8282 | (gen_tmpl, argvec, | |
8283 | /*class_specializations_p=*/false)); | |
8284 | } | |
8285 | } | |
9188c363 | 8286 | else |
6dfbb909 | 8287 | { |
f604fc3b MM |
8288 | /* A local variable. */ |
8289 | local_p = true; | |
6dfbb909 MM |
8290 | /* Subsequent calls to pushdecl will fill this in. */ |
8291 | ctx = NULL_TREE; | |
f604fc3b | 8292 | spec = retrieve_local_specialization (t); |
6dfbb909 | 8293 | } |
f604fc3b MM |
8294 | /* If we already have the specialization we need, there is |
8295 | nothing more to do. */ | |
36a117a5 | 8296 | if (spec) |
ae58fa02 MM |
8297 | { |
8298 | r = spec; | |
8299 | break; | |
8300 | } | |
5566b478 | 8301 | |
f604fc3b | 8302 | /* Create a new node for the specialization we need. */ |
0acf7199 | 8303 | r = copy_decl (t); |
56d0c6e3 JM |
8304 | if (type == NULL_TREE) |
8305 | type = tsubst (TREE_TYPE (t), args, complain, in_decl); | |
edebf865 | 8306 | if (TREE_CODE (r) == VAR_DECL) |
39703eb9 | 8307 | { |
8b0a8500 MM |
8308 | /* Even if the original location is out of scope, the |
8309 | newly substituted one is not. */ | |
8310 | DECL_DEAD_FOR_LOCAL (r) = 0; | |
8311 | DECL_INITIALIZED_P (r) = 0; | |
8312 | DECL_TEMPLATE_INSTANTIATED (r) = 0; | |
fc6a28d7 MM |
8313 | if (type == error_mark_node) |
8314 | return error_mark_node; | |
2c05d05e MM |
8315 | if (TREE_CODE (type) == FUNCTION_TYPE) |
8316 | { | |
8317 | /* It may seem that this case cannot occur, since: | |
8318 | ||
8319 | typedef void f(); | |
8320 | void g() { f x; } | |
8321 | ||
8322 | declares a function, not a variable. However: | |
8323 | ||
8324 | typedef void f(); | |
8325 | template <typename T> void g() { T t; } | |
8326 | template void g<f>(); | |
8327 | ||
8328 | is an attempt to declare a variable with function | |
8329 | type. */ | |
8330 | error ("variable %qD has function type", | |
8331 | /* R is not yet sufficiently initialized, so we | |
8332 | just use its name. */ | |
8333 | DECL_NAME (r)); | |
8334 | return error_mark_node; | |
8335 | } | |
39703eb9 MM |
8336 | type = complete_type (type); |
8337 | DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) | |
8338 | = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t); | |
62e19030 | 8339 | type = check_var_type (DECL_NAME (r), type); |
56b4ea3d RH |
8340 | |
8341 | if (DECL_HAS_VALUE_EXPR_P (t)) | |
8342 | { | |
8343 | tree ve = DECL_VALUE_EXPR (t); | |
015c2c66 MM |
8344 | ve = tsubst_expr (ve, args, complain, in_decl, |
8345 | /*constant_expression_p=*/false); | |
3db45ab5 | 8346 | SET_DECL_VALUE_EXPR (r, ve); |
56b4ea3d | 8347 | } |
39703eb9 | 8348 | } |
a3d87771 MM |
8349 | else if (DECL_SELF_REFERENCE_P (t)) |
8350 | SET_DECL_SELF_REFERENCE_P (r); | |
01f4137f | 8351 | TREE_TYPE (r) = type; |
9804209d | 8352 | cp_apply_type_quals_to_decl (cp_type_quals (type), r); |
5566b478 | 8353 | DECL_CONTEXT (r) = ctx; |
92643fea MM |
8354 | /* Clear out the mangled name and RTL for the instantiation. */ |
8355 | SET_DECL_ASSEMBLER_NAME (r, NULL_TREE); | |
820cc88f DB |
8356 | if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL)) |
8357 | SET_DECL_RTL (r, NULL_RTX); | |
8b0a8500 MM |
8358 | /* The initializer must not be expanded until it is required; |
8359 | see [temp.inst]. */ | |
d11ad92e | 8360 | DECL_INITIAL (r) = NULL_TREE; |
820cc88f DB |
8361 | if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL)) |
8362 | SET_DECL_RTL (r, NULL_RTX); | |
06ceef4e | 8363 | DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0; |
b9e75696 JM |
8364 | if (TREE_CODE (r) == VAR_DECL) |
8365 | { | |
8366 | /* Possibly limit visibility based on template args. */ | |
8367 | DECL_VISIBILITY (r) = VISIBILITY_DEFAULT; | |
b70f0f48 JM |
8368 | if (DECL_VISIBILITY_SPECIFIED (t)) |
8369 | { | |
8370 | DECL_VISIBILITY_SPECIFIED (r) = 0; | |
8371 | DECL_ATTRIBUTES (r) | |
8372 | = remove_attribute ("visibility", DECL_ATTRIBUTES (r)); | |
8373 | } | |
b9e75696 JM |
8374 | determine_visibility (r); |
8375 | } | |
5044ab0e JM |
8376 | /* Preserve a typedef that names a type. */ |
8377 | else if (TREE_CODE (r) == TYPE_DECL | |
8378 | && DECL_ORIGINAL_TYPE (t) | |
8379 | && type != error_mark_node) | |
8380 | { | |
8381 | DECL_ORIGINAL_TYPE (r) = tsubst (DECL_ORIGINAL_TYPE (t), | |
8382 | args, complain, in_decl); | |
8383 | TREE_TYPE (r) = type = build_variant_type_copy (type); | |
8384 | TYPE_NAME (type) = r; | |
8385 | } | |
5566b478 | 8386 | |
6dfbb909 MM |
8387 | if (!local_p) |
8388 | { | |
8389 | /* A static data member declaration is always marked | |
8390 | external when it is declared in-class, even if an | |
8391 | initializer is present. We mimic the non-template | |
8392 | processing here. */ | |
8393 | DECL_EXTERNAL (r) = 1; | |
fa8d6e85 | 8394 | |
d63d5d0c | 8395 | register_specialization (r, gen_tmpl, argvec, false); |
6dfbb909 MM |
8396 | DECL_TEMPLATE_INFO (r) = tree_cons (tmpl, argvec, NULL_TREE); |
8397 | SET_DECL_IMPLICIT_INSTANTIATION (r); | |
8398 | } | |
9188c363 | 8399 | else |
6dfbb909 | 8400 | register_local_specialization (r, t); |
5566b478 | 8401 | |
5566b478 | 8402 | TREE_CHAIN (r) = NULL_TREE; |
4f4141ff | 8403 | |
5044ab0e JM |
8404 | apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), |
8405 | (int) ATTR_FLAG_TYPE_IN_PLACE, | |
4f4141ff | 8406 | args, complain, in_decl); |
edebf865 | 8407 | layout_decl (r, 0); |
5566b478 | 8408 | } |
ae58fa02 | 8409 | break; |
5566b478 | 8410 | |
ae58fa02 | 8411 | default: |
315fb5db | 8412 | gcc_unreachable (); |
c8094d83 | 8413 | } |
ae58fa02 MM |
8414 | |
8415 | /* Restore the file and line information. */ | |
82a98427 | 8416 | input_location = saved_loc; |
ae58fa02 MM |
8417 | |
8418 | return r; | |
8419 | } | |
8420 | ||
34cd5ae7 | 8421 | /* Substitute into the ARG_TYPES of a function type. */ |
cabc336a | 8422 | |
e9659ab0 | 8423 | static tree |
c8094d83 | 8424 | tsubst_arg_types (tree arg_types, |
0cbd7506 MS |
8425 | tree args, |
8426 | tsubst_flags_t complain, | |
8427 | tree in_decl) | |
cabc336a MM |
8428 | { |
8429 | tree remaining_arg_types; | |
5d80a306 DG |
8430 | tree type = NULL_TREE; |
8431 | int i = 1; | |
8432 | tree expanded_args = NULL_TREE; | |
5e97d404 | 8433 | tree default_arg; |
cabc336a MM |
8434 | |
8435 | if (!arg_types || arg_types == void_list_node) | |
8436 | return arg_types; | |
c8094d83 | 8437 | |
cabc336a | 8438 | remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types), |
4393e105 MM |
8439 | args, complain, in_decl); |
8440 | if (remaining_arg_types == error_mark_node) | |
8441 | return error_mark_node; | |
8442 | ||
5d80a306 | 8443 | if (PACK_EXPANSION_P (TREE_VALUE (arg_types))) |
4b2811e9 | 8444 | { |
5d80a306 DG |
8445 | /* For a pack expansion, perform substitution on the |
8446 | entire expression. Later on, we'll handle the arguments | |
8447 | one-by-one. */ | |
8448 | expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types), | |
8449 | args, complain, in_decl); | |
cabc336a | 8450 | |
5d80a306 DG |
8451 | if (TREE_CODE (expanded_args) == TREE_VEC) |
8452 | /* So that we'll spin through the parameters, one by one. */ | |
8453 | i = TREE_VEC_LENGTH (expanded_args); | |
8454 | else | |
8455 | { | |
8456 | /* We only partially substituted into the parameter | |
8457 | pack. Our type is TYPE_PACK_EXPANSION. */ | |
8458 | type = expanded_args; | |
8459 | expanded_args = NULL_TREE; | |
8460 | } | |
8461 | } | |
cabc336a | 8462 | |
5d80a306 DG |
8463 | while (i > 0) { |
8464 | --i; | |
8465 | ||
8466 | if (expanded_args) | |
8467 | type = TREE_VEC_ELT (expanded_args, i); | |
8468 | else if (!type) | |
8469 | type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl); | |
c8094d83 | 8470 | |
5d80a306 DG |
8471 | if (type == error_mark_node) |
8472 | return error_mark_node; | |
8473 | if (VOID_TYPE_P (type)) | |
8474 | { | |
8475 | if (complain & tf_error) | |
8476 | { | |
8477 | error ("invalid parameter type %qT", type); | |
8478 | if (in_decl) | |
8479 | error ("in declaration %q+D", in_decl); | |
8480 | } | |
8481 | return error_mark_node; | |
5e97d404 | 8482 | } |
5d80a306 DG |
8483 | |
8484 | /* Do array-to-pointer, function-to-pointer conversion, and ignore | |
8485 | top-level qualifiers as required. */ | |
8486 | type = TYPE_MAIN_VARIANT (type_decays_to (type)); | |
c8094d83 | 8487 | |
5d80a306 DG |
8488 | /* We do not substitute into default arguments here. The standard |
8489 | mandates that they be instantiated only when needed, which is | |
8490 | done in build_over_call. */ | |
8491 | default_arg = TREE_PURPOSE (arg_types); | |
8492 | ||
8493 | if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG) | |
8494 | { | |
8495 | /* We've instantiated a template before its default arguments | |
8496 | have been parsed. This can happen for a nested template | |
8497 | class, and is not an error unless we require the default | |
8498 | argument in a call of this function. */ | |
8499 | remaining_arg_types = | |
8500 | tree_cons (default_arg, type, remaining_arg_types); | |
8501 | VEC_safe_push (tree, gc, DEFARG_INSTANTIATIONS (default_arg), | |
8502 | remaining_arg_types); | |
8503 | } | |
8504 | else | |
8505 | remaining_arg_types = | |
8506 | hash_tree_cons (default_arg, type, remaining_arg_types); | |
8507 | } | |
8508 | ||
8509 | return remaining_arg_types; | |
cabc336a MM |
8510 | } |
8511 | ||
4393e105 MM |
8512 | /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does |
8513 | *not* handle the exception-specification for FNTYPE, because the | |
8514 | initial substitution of explicitly provided template parameters | |
8515 | during argument deduction forbids substitution into the | |
8516 | exception-specification: | |
8517 | ||
8518 | [temp.deduct] | |
8519 | ||
8520 | All references in the function type of the function template to the | |
8521 | corresponding template parameters are replaced by the specified tem- | |
8522 | plate argument values. If a substitution in a template parameter or | |
8523 | in the function type of the function template results in an invalid | |
8524 | type, type deduction fails. [Note: The equivalent substitution in | |
8525 | exception specifications is done only when the function is instanti- | |
8526 | ated, at which point a program is ill-formed if the substitution | |
8527 | results in an invalid type.] */ | |
8528 | ||
8529 | static tree | |
c8094d83 | 8530 | tsubst_function_type (tree t, |
0cbd7506 MS |
8531 | tree args, |
8532 | tsubst_flags_t complain, | |
8533 | tree in_decl) | |
4393e105 MM |
8534 | { |
8535 | tree return_type; | |
8536 | tree arg_types; | |
8537 | tree fntype; | |
8538 | ||
8dd3f57a | 8539 | /* The TYPE_CONTEXT is not used for function/method types. */ |
50bc768d | 8540 | gcc_assert (TYPE_CONTEXT (t) == NULL_TREE); |
4393e105 | 8541 | |
46c895ac | 8542 | /* Substitute the return type. */ |
4393e105 MM |
8543 | return_type = tsubst (TREE_TYPE (t), args, complain, in_decl); |
8544 | if (return_type == error_mark_node) | |
8545 | return error_mark_node; | |
6e2993bf MM |
8546 | /* The standard does not presently indicate that creation of a |
8547 | function type with an invalid return type is a deduction failure. | |
270af55d | 8548 | However, that is clearly analogous to creating an array of "void" |
c8094d83 | 8549 | or a reference to a reference. This is core issue #486. */ |
6e2993bf MM |
8550 | if (TREE_CODE (return_type) == ARRAY_TYPE |
8551 | || TREE_CODE (return_type) == FUNCTION_TYPE) | |
8552 | { | |
8553 | if (complain & tf_error) | |
8554 | { | |
8555 | if (TREE_CODE (return_type) == ARRAY_TYPE) | |
8556 | error ("function returning an array"); | |
8557 | else | |
8558 | error ("function returning a function"); | |
8559 | } | |
8560 | return error_mark_node; | |
8561 | } | |
4393e105 | 8562 | |
34cd5ae7 | 8563 | /* Substitute the argument types. */ |
4393e105 | 8564 | arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, |
c8094d83 | 8565 | complain, in_decl); |
4393e105 MM |
8566 | if (arg_types == error_mark_node) |
8567 | return error_mark_node; | |
c8094d83 | 8568 | |
1891dec4 DM |
8569 | if (TYPE_QUALS (return_type) != TYPE_UNQUALIFIED |
8570 | && in_decl != NULL_TREE | |
8571 | && !TREE_NO_WARNING (in_decl) | |
8572 | && (SCALAR_TYPE_P (return_type) || VOID_TYPE_P (return_type))) | |
8573 | warning (OPT_Wreturn_type, | |
8574 | "type qualifiers ignored on function return type"); | |
8575 | ||
4393e105 MM |
8576 | /* Construct a new type node and return it. */ |
8577 | if (TREE_CODE (t) == FUNCTION_TYPE) | |
8578 | fntype = build_function_type (return_type, arg_types); | |
8579 | else | |
8580 | { | |
8581 | tree r = TREE_TYPE (TREE_VALUE (arg_types)); | |
8582 | if (! IS_AGGR_TYPE (r)) | |
8583 | { | |
8584 | /* [temp.deduct] | |
c8094d83 | 8585 | |
4393e105 MM |
8586 | Type deduction may fail for any of the following |
8587 | reasons: | |
c8094d83 | 8588 | |
4393e105 MM |
8589 | -- Attempting to create "pointer to member of T" when T |
8590 | is not a class type. */ | |
c2ea3a40 | 8591 | if (complain & tf_error) |
0f51ccfc | 8592 | error ("creating pointer to member function of non-class type %qT", |
4393e105 MM |
8593 | r); |
8594 | return error_mark_node; | |
8595 | } | |
c8094d83 MS |
8596 | |
8597 | fntype = build_method_type_directly (r, return_type, | |
43dc123f | 8598 | TREE_CHAIN (arg_types)); |
4393e105 | 8599 | } |
c2ea3a40 | 8600 | fntype = cp_build_qualified_type_real (fntype, TYPE_QUALS (t), complain); |
e9525111 | 8601 | fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t)); |
c8094d83 MS |
8602 | |
8603 | return fntype; | |
4393e105 MM |
8604 | } |
8605 | ||
c7222c02 MM |
8606 | /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template |
8607 | ARGS into that specification, and return the substituted | |
8608 | specification. If there is no specification, return NULL_TREE. */ | |
8609 | ||
8610 | static tree | |
c8094d83 MS |
8611 | tsubst_exception_specification (tree fntype, |
8612 | tree args, | |
c7222c02 MM |
8613 | tsubst_flags_t complain, |
8614 | tree in_decl) | |
8615 | { | |
8616 | tree specs; | |
8617 | tree new_specs; | |
8618 | ||
8619 | specs = TYPE_RAISES_EXCEPTIONS (fntype); | |
8620 | new_specs = NULL_TREE; | |
8621 | if (specs) | |
8622 | { | |
8623 | if (! TREE_VALUE (specs)) | |
8624 | new_specs = specs; | |
8625 | else | |
8626 | while (specs) | |
8627 | { | |
8628 | tree spec; | |
5d80a306 DG |
8629 | int i, len = 1; |
8630 | tree expanded_specs = NULL_TREE; | |
8631 | ||
8632 | if (PACK_EXPANSION_P (TREE_VALUE (specs))) | |
8633 | { | |
8634 | /* Expand the pack expansion type. */ | |
8635 | expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs), | |
8636 | args, complain, | |
8637 | in_decl); | |
8638 | len = TREE_VEC_LENGTH (expanded_specs); | |
8639 | } | |
8640 | ||
8641 | for (i = 0; i < len; ++i) | |
8642 | { | |
8643 | if (expanded_specs) | |
8644 | spec = TREE_VEC_ELT (expanded_specs, i); | |
8645 | else | |
8646 | spec = tsubst (TREE_VALUE (specs), args, complain, in_decl); | |
8647 | if (spec == error_mark_node) | |
8648 | return spec; | |
8649 | new_specs = add_exception_specifier (new_specs, spec, | |
8650 | complain); | |
8651 | } | |
8652 | ||
8653 | specs = TREE_CHAIN (specs); | |
c7222c02 MM |
8654 | } |
8655 | } | |
8656 | return new_specs; | |
8657 | } | |
8658 | ||
4393e105 MM |
8659 | /* Take the tree structure T and replace template parameters used |
8660 | therein with the argument vector ARGS. IN_DECL is an associated | |
8661 | decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE. | |
c2ea3a40 NS |
8662 | Issue error and warning messages under control of COMPLAIN. Note |
8663 | that we must be relatively non-tolerant of extensions here, in | |
8664 | order to preserve conformance; if we allow substitutions that | |
8665 | should not be allowed, we may allow argument deductions that should | |
8666 | not succeed, and therefore report ambiguous overload situations | |
8667 | where there are none. In theory, we could allow the substitution, | |
8668 | but indicate that it should have failed, and allow our caller to | |
8669 | make sure that the right thing happens, but we don't try to do this | |
8670 | yet. | |
4393e105 MM |
8671 | |
8672 | This function is used for dealing with types, decls and the like; | |
8673 | for expressions, use tsubst_expr or tsubst_copy. */ | |
ae58fa02 | 8674 | |
14d22dd6 | 8675 | static tree |
3a978d72 | 8676 | tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl) |
ae58fa02 | 8677 | { |
0ecfe0b4 | 8678 | tree type, r; |
ae58fa02 MM |
8679 | |
8680 | if (t == NULL_TREE || t == error_mark_node | |
8681 | || t == integer_type_node | |
8682 | || t == void_type_node | |
8683 | || t == char_type_node | |
539599c1 | 8684 | || t == unknown_type_node |
ae58fa02 MM |
8685 | || TREE_CODE (t) == NAMESPACE_DECL) |
8686 | return t; | |
8687 | ||
fc6a28d7 MM |
8688 | if (DECL_P (t)) |
8689 | return tsubst_decl (t, args, complain); | |
8690 | ||
ae58fa02 MM |
8691 | if (TREE_CODE (t) == IDENTIFIER_NODE) |
8692 | type = IDENTIFIER_TYPE_VALUE (t); | |
8693 | else | |
8694 | type = TREE_TYPE (t); | |
399dedb9 | 8695 | |
50bc768d | 8696 | gcc_assert (type != unknown_type_node); |
ae58fa02 | 8697 | |
56d0c6e3 | 8698 | /* Reuse typedefs. We need to do this to handle dependent attributes, |
a7f6bc8c | 8699 | such as attribute aligned. */ |
5044ab0e JM |
8700 | if (TYPE_P (t) |
8701 | && TYPE_NAME (t) | |
a7f6bc8c | 8702 | && TYPE_NAME (t) != TYPE_MAIN_DECL (t)) |
5044ab0e JM |
8703 | { |
8704 | tree decl = TYPE_NAME (t); | |
56d0c6e3 | 8705 | |
a7f6bc8c JM |
8706 | if (DECL_CLASS_SCOPE_P (decl) |
8707 | && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))) | |
56d0c6e3 JM |
8708 | { |
8709 | tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl)); | |
8710 | tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl); | |
8711 | r = retrieve_specialization (tmpl, gen_args, false); | |
8712 | } | |
8713 | else if (DECL_FUNCTION_SCOPE_P (decl)) | |
8714 | r = retrieve_local_specialization (decl); | |
8715 | else | |
8716 | r = NULL_TREE; | |
8717 | ||
8718 | if (r) | |
8719 | { | |
8720 | r = TREE_TYPE (r); | |
8721 | r = cp_build_qualified_type_real | |
8722 | (r, cp_type_quals (t) | cp_type_quals (r), | |
8723 | complain | tf_ignore_bad_quals); | |
8724 | return r; | |
8725 | } | |
8726 | /* Else we must be instantiating the typedef, so fall through. */ | |
5044ab0e JM |
8727 | } |
8728 | ||
fc6a28d7 | 8729 | if (type |
ae58fa02 | 8730 | && TREE_CODE (t) != TYPENAME_TYPE |
4393e105 MM |
8731 | && TREE_CODE (t) != IDENTIFIER_NODE |
8732 | && TREE_CODE (t) != FUNCTION_TYPE | |
8733 | && TREE_CODE (t) != METHOD_TYPE) | |
8734 | type = tsubst (type, args, complain, in_decl); | |
8735 | if (type == error_mark_node) | |
8736 | return error_mark_node; | |
ae58fa02 | 8737 | |
ae58fa02 MM |
8738 | switch (TREE_CODE (t)) |
8739 | { | |
8740 | case RECORD_TYPE: | |
8741 | case UNION_TYPE: | |
8742 | case ENUMERAL_TYPE: | |
4393e105 MM |
8743 | return tsubst_aggr_type (t, args, complain, in_decl, |
8744 | /*entering_scope=*/0); | |
ae58fa02 MM |
8745 | |
8746 | case ERROR_MARK: | |
8747 | case IDENTIFIER_NODE: | |
ae58fa02 MM |
8748 | case VOID_TYPE: |
8749 | case REAL_TYPE: | |
8750 | case COMPLEX_TYPE: | |
c00996a3 | 8751 | case VECTOR_TYPE: |
ae58fa02 MM |
8752 | case BOOLEAN_TYPE: |
8753 | case INTEGER_CST: | |
8754 | case REAL_CST: | |
8755 | case STRING_CST: | |
8756 | return t; | |
8757 | ||
8758 | case INTEGER_TYPE: | |
8759 | if (t == integer_type_node) | |
8760 | return t; | |
8761 | ||
8762 | if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST | |
8763 | && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST) | |
8764 | return t; | |
d2e5ee5c | 8765 | |
5566b478 | 8766 | { |
ddce3528 | 8767 | tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0); |
7ddedda4 | 8768 | |
6ee3ffe8 MM |
8769 | max = tsubst_expr (omax, args, complain, in_decl, |
8770 | /*integral_constant_expression_p=*/false); | |
4ef69b83 | 8771 | max = fold_decl_constant_value (max); |
8dd3f57a | 8772 | |
eff3a276 MM |
8773 | if (TREE_CODE (max) != INTEGER_CST |
8774 | && TREE_CODE (max) != TEMPLATE_PARM_INDEX | |
8775 | && !at_function_scope_p ()) | |
8776 | { | |
8777 | if (complain & tf_error) | |
8778 | error ("array bound is not an integer constant"); | |
8779 | return error_mark_node; | |
8780 | } | |
8781 | ||
95cd6f6f | 8782 | /* [temp.deduct] |
4393e105 | 8783 | |
95cd6f6f JC |
8784 | Type deduction may fail for any of the following |
8785 | reasons: | |
4393e105 | 8786 | |
95cd6f6f JC |
8787 | Attempting to create an array with a size that is |
8788 | zero or negative. */ | |
8789 | if (integer_zerop (max) && !(complain & tf_error)) | |
8790 | /* We must fail if performing argument deduction (as | |
8791 | indicated by the state of complain), so that | |
8792 | another substitution can be found. */ | |
8793 | return error_mark_node; | |
95cd6f6f JC |
8794 | else if (TREE_CODE (max) == INTEGER_CST |
8795 | && INT_CST_LT (max, integer_zero_node)) | |
8796 | { | |
c2ea3a40 | 8797 | if (complain & tf_error) |
95cd6f6f | 8798 | error ("creating array with negative size (%qE)", max); |
4393e105 MM |
8799 | |
8800 | return error_mark_node; | |
0ecfe0b4 JM |
8801 | } |
8802 | ||
c95cd22e | 8803 | return compute_array_index_type (NULL_TREE, max); |
ae58fa02 MM |
8804 | } |
8805 | ||
8806 | case TEMPLATE_TYPE_PARM: | |
8807 | case TEMPLATE_TEMPLATE_PARM: | |
a1281f45 | 8808 | case BOUND_TEMPLATE_TEMPLATE_PARM: |
ae58fa02 MM |
8809 | case TEMPLATE_PARM_INDEX: |
8810 | { | |
8811 | int idx; | |
8812 | int level; | |
8813 | int levels; | |
315fb5db | 8814 | tree arg = NULL_TREE; |
0ecfe0b4 JM |
8815 | |
8816 | r = NULL_TREE; | |
ae58fa02 | 8817 | |
315fb5db | 8818 | gcc_assert (TREE_VEC_LENGTH (args) > 0); |
f4205442 | 8819 | template_parm_level_and_index (t, &level, &idx); |
ae58fa02 | 8820 | |
315fb5db NS |
8821 | levels = TMPL_ARGS_DEPTH (args); |
8822 | if (level <= levels) | |
5d80a306 DG |
8823 | { |
8824 | arg = TMPL_ARG (args, level, idx); | |
8825 | ||
8826 | if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT) | |
8827 | /* See through ARGUMENT_PACK_SELECT arguments. */ | |
8828 | arg = ARGUMENT_PACK_SELECT_ARG (arg); | |
8829 | } | |
ae58fa02 | 8830 | |
315fb5db NS |
8831 | if (arg == error_mark_node) |
8832 | return error_mark_node; | |
8833 | else if (arg != NULL_TREE) | |
8834 | { | |
5d80a306 DG |
8835 | if (ARGUMENT_PACK_P (arg)) |
8836 | /* If ARG is an argument pack, we don't actually want to | |
8837 | perform a substitution here, because substitutions | |
8838 | for argument packs are only done | |
8839 | element-by-element. We can get to this point when | |
8840 | substituting the type of a non-type template | |
8841 | parameter pack, when that type actually contains | |
8842 | template parameter packs from an outer template, e.g., | |
8843 | ||
8844 | template<typename... Types> struct A { | |
8845 | template<Types... Values> struct B { }; | |
8846 | }; */ | |
8847 | return t; | |
8848 | ||
315fb5db | 8849 | if (TREE_CODE (t) == TEMPLATE_TYPE_PARM) |
ae58fa02 | 8850 | { |
9180c238 | 8851 | int quals; |
315fb5db | 8852 | gcc_assert (TYPE_P (arg)); |
9180c238 JM |
8853 | |
8854 | /* cv-quals from the template are discarded when | |
8855 | substituting in a function or reference type. */ | |
8856 | if (TREE_CODE (arg) == FUNCTION_TYPE | |
8857 | || TREE_CODE (arg) == METHOD_TYPE | |
8858 | || TREE_CODE (arg) == REFERENCE_TYPE) | |
8859 | quals = cp_type_quals (arg); | |
8860 | else | |
8861 | quals = cp_type_quals (arg) | cp_type_quals (t); | |
8862 | ||
315fb5db | 8863 | return cp_build_qualified_type_real |
9180c238 | 8864 | (arg, quals, complain | tf_ignore_bad_quals); |
315fb5db NS |
8865 | } |
8866 | else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM) | |
8867 | { | |
8868 | /* We are processing a type constructed from a | |
8869 | template template parameter. */ | |
8870 | tree argvec = tsubst (TYPE_TI_ARGS (t), | |
8871 | args, complain, in_decl); | |
8872 | if (argvec == error_mark_node) | |
8873 | return error_mark_node; | |
c8094d83 | 8874 | |
315fb5db NS |
8875 | /* We can get a TEMPLATE_TEMPLATE_PARM here when we |
8876 | are resolving nested-types in the signature of a | |
8877 | member function templates. Otherwise ARG is a | |
8878 | TEMPLATE_DECL and is the real template to be | |
8879 | instantiated. */ | |
8880 | if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM) | |
8881 | arg = TYPE_NAME (arg); | |
c8094d83 MS |
8882 | |
8883 | r = lookup_template_class (arg, | |
8884 | argvec, in_decl, | |
315fb5db NS |
8885 | DECL_CONTEXT (arg), |
8886 | /*entering_scope=*/0, | |
8887 | complain); | |
8888 | return cp_build_qualified_type_real | |
8889 | (r, TYPE_QUALS (t), complain); | |
ae58fa02 | 8890 | } |
315fb5db NS |
8891 | else |
8892 | /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */ | |
8893 | return arg; | |
ae58fa02 MM |
8894 | } |
8895 | ||
8896 | if (level == 1) | |
8897 | /* This can happen during the attempted tsubst'ing in | |
8898 | unify. This means that we don't yet have any information | |
8899 | about the template parameter in question. */ | |
8900 | return t; | |
8901 | ||
8902 | /* If we get here, we must have been looking at a parm for a | |
8903 | more deeply nested template. Make a new version of this | |
8904 | template parameter, but with a lower level. */ | |
8905 | switch (TREE_CODE (t)) | |
8906 | { | |
8907 | case TEMPLATE_TYPE_PARM: | |
8908 | case TEMPLATE_TEMPLATE_PARM: | |
a1281f45 | 8909 | case BOUND_TEMPLATE_TEMPLATE_PARM: |
89d684bb | 8910 | if (cp_type_quals (t)) |
ae58fa02 | 8911 | { |
9ccf6541 | 8912 | r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl); |
0cbd7506 MS |
8913 | r = cp_build_qualified_type_real |
8914 | (r, cp_type_quals (t), | |
4f2b0fb2 NS |
8915 | complain | (TREE_CODE (t) == TEMPLATE_TYPE_PARM |
8916 | ? tf_ignore_bad_quals : 0)); | |
9ccf6541 MM |
8917 | } |
8918 | else | |
8919 | { | |
11e74ea6 | 8920 | r = copy_type (t); |
9ccf6541 MM |
8921 | TEMPLATE_TYPE_PARM_INDEX (r) |
8922 | = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t), | |
ef3b7b17 | 8923 | r, levels, args, complain); |
9ccf6541 MM |
8924 | TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r); |
8925 | TYPE_MAIN_VARIANT (r) = r; | |
8926 | TYPE_POINTER_TO (r) = NULL_TREE; | |
8927 | TYPE_REFERENCE_TO (r) = NULL_TREE; | |
8928 | ||
06d40de8 DG |
8929 | if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM) |
8930 | /* We have reduced the level of the template | |
8931 | template parameter, but not the levels of its | |
8932 | template parameters, so canonical_type_parameter | |
8933 | will not be able to find the canonical template | |
8934 | template parameter for this level. Thus, we | |
8935 | require structural equality checking to compare | |
8936 | TEMPLATE_TEMPLATE_PARMs. */ | |
8937 | SET_TYPE_STRUCTURAL_EQUALITY (r); | |
3d761c46 DG |
8938 | else if (TYPE_STRUCTURAL_EQUALITY_P (t)) |
8939 | SET_TYPE_STRUCTURAL_EQUALITY (r); | |
06d40de8 DG |
8940 | else |
8941 | TYPE_CANONICAL (r) = canonical_type_parameter (r); | |
8942 | ||
a1281f45 | 8943 | if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM) |
9ccf6541 MM |
8944 | { |
8945 | tree argvec = tsubst (TYPE_TI_ARGS (t), args, | |
c8094d83 | 8946 | complain, in_decl); |
9ccf6541 MM |
8947 | if (argvec == error_mark_node) |
8948 | return error_mark_node; | |
4393e105 | 8949 | |
9ccf6541 MM |
8950 | TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r) |
8951 | = tree_cons (TYPE_TI_TEMPLATE (t), argvec, NULL_TREE); | |
8952 | } | |
ae58fa02 MM |
8953 | } |
8954 | break; | |
8955 | ||
8956 | case TEMPLATE_PARM_INDEX: | |
ef3b7b17 | 8957 | r = reduce_template_parm_level (t, type, levels, args, complain); |
ae58fa02 | 8958 | break; |
c8094d83 | 8959 | |
ae58fa02 | 8960 | default: |
315fb5db | 8961 | gcc_unreachable (); |
ae58fa02 MM |
8962 | } |
8963 | ||
5566b478 | 8964 | return r; |
ae58fa02 | 8965 | } |
5566b478 | 8966 | |
8d08fdba MS |
8967 | case TREE_LIST: |
8968 | { | |
058b15c1 | 8969 | tree purpose, value, chain; |
8d08fdba MS |
8970 | |
8971 | if (t == void_list_node) | |
8972 | return t; | |
8973 | ||
8d08fdba MS |
8974 | purpose = TREE_PURPOSE (t); |
8975 | if (purpose) | |
4393e105 MM |
8976 | { |
8977 | purpose = tsubst (purpose, args, complain, in_decl); | |
8978 | if (purpose == error_mark_node) | |
8979 | return error_mark_node; | |
8980 | } | |
8d08fdba MS |
8981 | value = TREE_VALUE (t); |
8982 | if (value) | |
4393e105 MM |
8983 | { |
8984 | value = tsubst (value, args, complain, in_decl); | |
8985 | if (value == error_mark_node) | |
8986 | return error_mark_node; | |
8987 | } | |
8d08fdba MS |
8988 | chain = TREE_CHAIN (t); |
8989 | if (chain && chain != void_type_node) | |
4393e105 MM |
8990 | { |
8991 | chain = tsubst (chain, args, complain, in_decl); | |
8992 | if (chain == error_mark_node) | |
8993 | return error_mark_node; | |
8994 | } | |
8d08fdba MS |
8995 | if (purpose == TREE_PURPOSE (t) |
8996 | && value == TREE_VALUE (t) | |
8997 | && chain == TREE_CHAIN (t)) | |
8998 | return t; | |
058b15c1 | 8999 | return hash_tree_cons (purpose, value, chain); |
8d08fdba | 9000 | } |
c8094d83 | 9001 | |
95b4aca6 | 9002 | case TREE_BINFO: |
bd7eccc4 | 9003 | /* We should never be tsubsting a binfo. */ |
315fb5db | 9004 | gcc_unreachable (); |
85b71cf2 | 9005 | |
95b4aca6 NS |
9006 | case TREE_VEC: |
9007 | /* A vector of template arguments. */ | |
50bc768d | 9008 | gcc_assert (!type); |
a91db711 | 9009 | return tsubst_template_args (t, args, complain, in_decl); |
8d08fdba | 9010 | |
8d08fdba MS |
9011 | case POINTER_TYPE: |
9012 | case REFERENCE_TYPE: | |
9013 | { | |
8d08fdba | 9014 | enum tree_code code; |
79a7c7fa | 9015 | |
46c895ac | 9016 | if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE) |
8d08fdba MS |
9017 | return t; |
9018 | ||
9019 | code = TREE_CODE (t); | |
4393e105 MM |
9020 | |
9021 | ||
9022 | /* [temp.deduct] | |
c8094d83 | 9023 | |
4393e105 | 9024 | Type deduction may fail for any of the following |
c8094d83 | 9025 | reasons: |
4393e105 MM |
9026 | |
9027 | -- Attempting to create a pointer to reference type. | |
9028 | -- Attempting to create a reference to a reference type or | |
8af2fec4 RY |
9029 | a reference to void. |
9030 | ||
9031 | Core issue 106 says that creating a reference to a reference | |
9032 | during instantiation is no longer a cause for failure. We | |
9033 | only enforce this check in strict C++98 mode. */ | |
9034 | if ((TREE_CODE (type) == REFERENCE_TYPE | |
c1ae8be5 | 9035 | && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE)) |
0ecfe0b4 | 9036 | || (code == REFERENCE_TYPE && TREE_CODE (type) == VOID_TYPE)) |
79a7c7fa | 9037 | { |
82a98427 | 9038 | static location_t last_loc; |
79a7c7fa JM |
9039 | |
9040 | /* We keep track of the last time we issued this error | |
9041 | message to avoid spewing a ton of messages during a | |
9042 | single bad template instantiation. */ | |
c2ea3a40 | 9043 | if (complain & tf_error |
93409b8c | 9044 | #ifdef USE_MAPPED_LOCATION |
9b60dfd7 | 9045 | && last_loc != input_location |
93409b8c | 9046 | #else |
82a98427 | 9047 | && (last_loc.line != input_line |
9b60dfd7 | 9048 | || last_loc.file != input_filename) |
93409b8c | 9049 | #endif |
9b60dfd7 | 9050 | ) |
79a7c7fa | 9051 | { |
0ecfe0b4 | 9052 | if (TREE_CODE (type) == VOID_TYPE) |
33bd39a2 | 9053 | error ("forming reference to void"); |
0ecfe0b4 | 9054 | else |
0f51ccfc | 9055 | error ("forming %s to reference type %qT", |
0cbd7506 MS |
9056 | (code == POINTER_TYPE) ? "pointer" : "reference", |
9057 | type); | |
82a98427 | 9058 | last_loc = input_location; |
79a7c7fa JM |
9059 | } |
9060 | ||
4393e105 | 9061 | return error_mark_node; |
79a7c7fa JM |
9062 | } |
9063 | else if (code == POINTER_TYPE) | |
46c895ac NS |
9064 | { |
9065 | r = build_pointer_type (type); | |
9066 | if (TREE_CODE (type) == METHOD_TYPE) | |
9067 | r = build_ptrmemfunc_type (r); | |
9068 | } | |
8af2fec4 RY |
9069 | else if (TREE_CODE (type) == REFERENCE_TYPE) |
9070 | /* In C++0x, during template argument substitution, when there is an | |
9071 | attempt to create a reference to a reference type, reference | |
9072 | collapsing is applied as described in [14.3.1/4 temp.arg.type]: | |
9073 | ||
9074 | "If a template-argument for a template-parameter T names a type | |
9075 | that is a reference to a type A, an attempt to create the type | |
9076 | 'lvalue reference to cv T' creates the type 'lvalue reference to | |
9077 | A,' while an attempt to create the type type rvalue reference to | |
9078 | cv T' creates the type T" | |
9079 | */ | |
9080 | r = cp_build_reference_type | |
9081 | (TREE_TYPE (type), | |
9082 | TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type)); | |
8d08fdba | 9083 | else |
8af2fec4 | 9084 | r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t)); |
adecb3f4 | 9085 | r = cp_build_qualified_type_real (r, TYPE_QUALS (t), complain); |
79a7c7fa | 9086 | |
a5f1c5f8 NS |
9087 | if (r != error_mark_node) |
9088 | /* Will this ever be needed for TYPE_..._TO values? */ | |
9089 | layout_type (r); | |
c8094d83 | 9090 | |
8d08fdba MS |
9091 | return r; |
9092 | } | |
a4443a08 | 9093 | case OFFSET_TYPE: |
0ecfe0b4 | 9094 | { |
4393e105 MM |
9095 | r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl); |
9096 | if (r == error_mark_node || !IS_AGGR_TYPE (r)) | |
9097 | { | |
9098 | /* [temp.deduct] | |
9099 | ||
9100 | Type deduction may fail for any of the following | |
9101 | reasons: | |
c8094d83 | 9102 | |
4393e105 | 9103 | -- Attempting to create "pointer to member of T" when T |
0cbd7506 | 9104 | is not a class type. */ |
c2ea3a40 | 9105 | if (complain & tf_error) |
0f51ccfc | 9106 | error ("creating pointer to member of non-class type %qT", r); |
4393e105 MM |
9107 | return error_mark_node; |
9108 | } | |
46c895ac NS |
9109 | if (TREE_CODE (type) == REFERENCE_TYPE) |
9110 | { | |
4f09be91 | 9111 | if (complain & tf_error) |
0f51ccfc | 9112 | error ("creating pointer to member reference type %qT", type); |
cb6d4a9f VR |
9113 | return error_mark_node; |
9114 | } | |
9115 | if (TREE_CODE (type) == VOID_TYPE) | |
9116 | { | |
9117 | if (complain & tf_error) | |
9118 | error ("creating pointer to member of type void"); | |
46c895ac NS |
9119 | return error_mark_node; |
9120 | } | |
50bc768d | 9121 | gcc_assert (TREE_CODE (type) != METHOD_TYPE); |
46c895ac | 9122 | if (TREE_CODE (type) == FUNCTION_TYPE) |
a5ac359a | 9123 | { |
0cbd7506 MS |
9124 | /* The type of the implicit object parameter gets its |
9125 | cv-qualifiers from the FUNCTION_TYPE. */ | |
a5ac359a | 9126 | tree method_type; |
0cbd7506 MS |
9127 | tree this_type = cp_build_qualified_type (TYPE_MAIN_VARIANT (r), |
9128 | cp_type_quals (type)); | |
9129 | tree memptr; | |
9130 | method_type = build_method_type_directly (this_type, | |
43dc123f MM |
9131 | TREE_TYPE (type), |
9132 | TYPE_ARG_TYPES (type)); | |
0cbd7506 MS |
9133 | memptr = build_ptrmemfunc_type (build_pointer_type (method_type)); |
9134 | return cp_build_qualified_type_real (memptr, cp_type_quals (t), | |
9135 | complain); | |
a5ac359a | 9136 | } |
46c895ac | 9137 | else |
b7a78333 MM |
9138 | return cp_build_qualified_type_real (build_ptrmem_type (r, type), |
9139 | TYPE_QUALS (t), | |
9140 | complain); | |
0ecfe0b4 | 9141 | } |
8d08fdba MS |
9142 | case FUNCTION_TYPE: |
9143 | case METHOD_TYPE: | |
9144 | { | |
c11b6f21 | 9145 | tree fntype; |
c7222c02 | 9146 | tree specs; |
4393e105 MM |
9147 | fntype = tsubst_function_type (t, args, complain, in_decl); |
9148 | if (fntype == error_mark_node) | |
9149 | return error_mark_node; | |
cabc336a | 9150 | |
34cd5ae7 | 9151 | /* Substitute the exception specification. */ |
c8094d83 | 9152 | specs = tsubst_exception_specification (t, args, complain, |
c7222c02 | 9153 | in_decl); |
9f6206d9 VR |
9154 | if (specs == error_mark_node) |
9155 | return error_mark_node; | |
c7222c02 MM |
9156 | if (specs) |
9157 | fntype = build_exception_variant (fntype, specs); | |
c11b6f21 | 9158 | return fntype; |
8d08fdba MS |
9159 | } |
9160 | case ARRAY_TYPE: | |
9161 | { | |
4393e105 MM |
9162 | tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl); |
9163 | if (domain == error_mark_node) | |
9164 | return error_mark_node; | |
9165 | ||
9166 | /* As an optimization, we avoid regenerating the array type if | |
9167 | it will obviously be the same as T. */ | |
8d08fdba MS |
9168 | if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t)) |
9169 | return t; | |
0ecfe0b4 | 9170 | |
c8094d83 | 9171 | /* These checks should match the ones in grokdeclarator. |
4393e105 | 9172 | |
c8094d83 MS |
9173 | [temp.deduct] |
9174 | ||
9175 | The deduction may fail for any of the following reasons: | |
4393e105 MM |
9176 | |
9177 | -- Attempting to create an array with an element type that | |
c8094d83 | 9178 | is void, a function type, or a reference type, or [DR337] |
cfb91b67 | 9179 | an abstract class type. */ |
c8094d83 | 9180 | if (TREE_CODE (type) == VOID_TYPE |
4393e105 MM |
9181 | || TREE_CODE (type) == FUNCTION_TYPE |
9182 | || TREE_CODE (type) == REFERENCE_TYPE) | |
0ecfe0b4 | 9183 | { |
c2ea3a40 | 9184 | if (complain & tf_error) |
0f51ccfc | 9185 | error ("creating array of %qT", type); |
4393e105 | 9186 | return error_mark_node; |
0ecfe0b4 | 9187 | } |
cfb91b67 GB |
9188 | if (CLASS_TYPE_P (type) && CLASSTYPE_PURE_VIRTUALS (type)) |
9189 | { | |
9190 | if (complain & tf_error) | |
c8094d83 | 9191 | error ("creating array of %qT, which is an abstract class type", |
cfb91b67 | 9192 | type); |
c8094d83 | 9193 | return error_mark_node; |
cfb91b67 | 9194 | } |
0ecfe0b4 | 9195 | |
8d08fdba | 9196 | r = build_cplus_array_type (type, domain); |
5044ab0e JM |
9197 | |
9198 | if (TYPE_USER_ALIGN (t)) | |
9199 | { | |
9200 | TYPE_ALIGN (r) = TYPE_ALIGN (t); | |
9201 | TYPE_USER_ALIGN (r) = 1; | |
9202 | } | |
9203 | ||
8d08fdba MS |
9204 | return r; |
9205 | } | |
9206 | ||
8d08fdba | 9207 | case PLUS_EXPR: |
5566b478 | 9208 | case MINUS_EXPR: |
4393e105 | 9209 | { |
c2ea3a40 NS |
9210 | tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl); |
9211 | tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl); | |
4393e105 MM |
9212 | |
9213 | if (e1 == error_mark_node || e2 == error_mark_node) | |
9214 | return error_mark_node; | |
9215 | ||
7866705a | 9216 | return fold_build2 (TREE_CODE (t), TREE_TYPE (t), e1, e2); |
4393e105 | 9217 | } |
8d08fdba MS |
9218 | |
9219 | case NEGATE_EXPR: | |
9220 | case NOP_EXPR: | |
4393e105 | 9221 | { |
c2ea3a40 | 9222 | tree e = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl); |
4393e105 MM |
9223 | if (e == error_mark_node) |
9224 | return error_mark_node; | |
9225 | ||
7866705a | 9226 | return fold_build1 (TREE_CODE (t), TREE_TYPE (t), e); |
4393e105 | 9227 | } |
8d08fdba | 9228 | |
5566b478 MS |
9229 | case TYPENAME_TYPE: |
9230 | { | |
4393e105 MM |
9231 | tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain, |
9232 | in_decl, /*entering_scope=*/1); | |
9233 | tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args, | |
c8094d83 | 9234 | complain, in_decl); |
4393e105 MM |
9235 | |
9236 | if (ctx == error_mark_node || f == error_mark_node) | |
9237 | return error_mark_node; | |
ae58fa02 | 9238 | |
bf8f3f93 MM |
9239 | if (!IS_AGGR_TYPE (ctx)) |
9240 | { | |
c2ea3a40 | 9241 | if (complain & tf_error) |
0f51ccfc | 9242 | error ("%qT is not a class, struct, or union type", ctx); |
bf8f3f93 MM |
9243 | return error_mark_node; |
9244 | } | |
9245 | else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx)) | |
9246 | { | |
9247 | /* Normally, make_typename_type does not require that the CTX | |
9248 | have complete type in order to allow things like: | |
c8094d83 | 9249 | |
0cbd7506 | 9250 | template <class T> struct S { typename S<T>::X Y; }; |
ae58fa02 | 9251 | |
bf8f3f93 MM |
9252 | But, such constructs have already been resolved by this |
9253 | point, so here CTX really should have complete type, unless | |
9254 | it's a partial instantiation. */ | |
4393e105 | 9255 | ctx = complete_type (ctx); |
d0f062fb | 9256 | if (!COMPLETE_TYPE_P (ctx)) |
4393e105 | 9257 | { |
c2ea3a40 | 9258 | if (complain & tf_error) |
7a228918 | 9259 | cxx_incomplete_type_error (NULL_TREE, ctx); |
4393e105 MM |
9260 | return error_mark_node; |
9261 | } | |
9262 | } | |
ae58fa02 | 9263 | |
fc6a28d7 | 9264 | f = make_typename_type (ctx, f, typename_type, |
4f2b0fb2 | 9265 | (complain & tf_error) | tf_keep_type_decl); |
f0bcd168 MM |
9266 | if (f == error_mark_node) |
9267 | return f; | |
0cbd7506 MS |
9268 | if (TREE_CODE (f) == TYPE_DECL) |
9269 | { | |
4f2b0fb2 | 9270 | complain |= tf_ignore_bad_quals; |
0cbd7506 MS |
9271 | f = TREE_TYPE (f); |
9272 | } | |
c8094d83 | 9273 | |
fc6a28d7 MM |
9274 | if (TREE_CODE (f) != TYPENAME_TYPE) |
9275 | { | |
9276 | if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE) | |
c8094d83 | 9277 | error ("%qT resolves to %qT, which is not an enumeration type", |
fc6a28d7 MM |
9278 | t, f); |
9279 | else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f)) | |
c8094d83 | 9280 | error ("%qT resolves to %qT, which is is not a class type", |
fc6a28d7 MM |
9281 | t, f); |
9282 | } | |
9283 | ||
0cbd7506 MS |
9284 | return cp_build_qualified_type_real |
9285 | (f, cp_type_quals (f) | cp_type_quals (t), complain); | |
5566b478 | 9286 | } |
c8094d83 | 9287 | |
b8c6534b KL |
9288 | case UNBOUND_CLASS_TEMPLATE: |
9289 | { | |
9290 | tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain, | |
9291 | in_decl, /*entering_scope=*/1); | |
9292 | tree name = TYPE_IDENTIFIER (t); | |
b939a023 | 9293 | tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t)); |
b8c6534b KL |
9294 | |
9295 | if (ctx == error_mark_node || name == error_mark_node) | |
9296 | return error_mark_node; | |
9297 | ||
b939a023 KL |
9298 | if (parm_list) |
9299 | parm_list = tsubst_template_parms (parm_list, args, complain); | |
9300 | return make_unbound_class_template (ctx, name, parm_list, complain); | |
b8c6534b KL |
9301 | } |
9302 | ||
5566b478 | 9303 | case INDIRECT_REF: |
5566b478 | 9304 | case ADDR_EXPR: |
058b15c1 | 9305 | case CALL_EXPR: |
315fb5db | 9306 | gcc_unreachable (); |
5566b478 MS |
9307 | |
9308 | case ARRAY_REF: | |
4393e105 | 9309 | { |
c2ea3a40 | 9310 | tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl); |
015c2c66 MM |
9311 | tree e2 = tsubst_expr (TREE_OPERAND (t, 1), args, complain, in_decl, |
9312 | /*integral_constant_expression_p=*/false); | |
4393e105 MM |
9313 | if (e1 == error_mark_node || e2 == error_mark_node) |
9314 | return error_mark_node; | |
9315 | ||
44de5aeb | 9316 | return build_nt (ARRAY_REF, e1, e2, NULL_TREE, NULL_TREE); |
4393e105 | 9317 | } |
5566b478 | 9318 | |
fc378698 | 9319 | case SCOPE_REF: |
4393e105 | 9320 | { |
c2ea3a40 | 9321 | tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl); |
4393e105 MM |
9322 | tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl); |
9323 | if (e1 == error_mark_node || e2 == error_mark_node) | |
9324 | return error_mark_node; | |
9325 | ||
02ed62dd MM |
9326 | return build_qualified_name (/*type=*/NULL_TREE, |
9327 | e1, e2, QUALIFIED_NAME_IS_TEMPLATE (t)); | |
4393e105 | 9328 | } |
fc378698 | 9329 | |
b894fc05 | 9330 | case TYPEOF_TYPE: |
4393e105 | 9331 | { |
b830b74c | 9332 | tree type; |
4393e105 | 9333 | |
015c2c66 MM |
9334 | type = finish_typeof (tsubst_expr |
9335 | (TYPEOF_TYPE_EXPR (t), args, | |
9336 | complain, in_decl, | |
9337 | /*integral_constant_expression_p=*/false)); | |
b830b74c | 9338 | return cp_build_qualified_type_real (type, |
dce50630 | 9339 | cp_type_quals (t) |
b830b74c | 9340 | | cp_type_quals (type), |
dce50630 | 9341 | complain); |
4393e105 | 9342 | } |
b894fc05 | 9343 | |
3ad6a8e1 DG |
9344 | case DECLTYPE_TYPE: |
9345 | { | |
9346 | tree type; | |
9347 | ||
9348 | type = | |
9349 | finish_decltype_type (tsubst_expr | |
9350 | (DECLTYPE_TYPE_EXPR (t), args, | |
9351 | complain, in_decl, | |
9352 | /*integral_constant_expression_p=*/false), | |
9353 | DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t)); | |
9354 | return cp_build_qualified_type_real (type, | |
9355 | cp_type_quals (t) | |
9356 | | cp_type_quals (type), | |
9357 | complain); | |
9358 | } | |
9359 | ||
5d80a306 DG |
9360 | case TYPE_ARGUMENT_PACK: |
9361 | case NONTYPE_ARGUMENT_PACK: | |
9362 | { | |
9363 | tree r = make_node (TREE_CODE (t)); | |
9364 | tree packed_out = | |
9365 | tsubst_template_args (ARGUMENT_PACK_ARGS (t), | |
9366 | args, | |
9367 | complain, | |
9368 | in_decl); | |
9369 | SET_ARGUMENT_PACK_ARGS (r, packed_out); | |
9370 | ||
9371 | /* For template nontype argument packs, also substitute into | |
9372 | the type. */ | |
9373 | if (TREE_CODE (t) == NONTYPE_ARGUMENT_PACK) | |
9374 | TREE_TYPE (r) = tsubst (TREE_TYPE (t), args, complain, in_decl); | |
9375 | ||
9376 | return r; | |
9377 | } | |
9378 | break; | |
9379 | ||
8d08fdba | 9380 | default: |
9e637a26 | 9381 | sorry ("use of %qs in template", |
8d08fdba MS |
9382 | tree_code_name [(int) TREE_CODE (t)]); |
9383 | return error_mark_node; | |
9384 | } | |
9385 | } | |
9386 | ||
ee76b931 MM |
9387 | /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the |
9388 | type of the expression on the left-hand side of the "." or "->" | |
9389 | operator. */ | |
9390 | ||
9391 | static tree | |
9392 | tsubst_baselink (tree baselink, tree object_type, | |
9393 | tree args, tsubst_flags_t complain, tree in_decl) | |
9394 | { | |
9395 | tree name; | |
9396 | tree qualifying_scope; | |
9397 | tree fns; | |
6f67f000 | 9398 | tree optype; |
ee76b931 MM |
9399 | tree template_args = 0; |
9400 | bool template_id_p = false; | |
9401 | ||
51a203d9 MM |
9402 | /* A baselink indicates a function from a base class. Both the |
9403 | BASELINK_ACCESS_BINFO and the base class referenced may | |
9404 | indicate bases of the template class, rather than the | |
9405 | instantiated class. In addition, lookups that were not | |
9406 | ambiguous before may be ambiguous now. Therefore, we perform | |
9407 | the lookup again. */ | |
ee76b931 | 9408 | qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink)); |
51a203d9 MM |
9409 | qualifying_scope = tsubst (qualifying_scope, args, |
9410 | complain, in_decl); | |
ee76b931 | 9411 | fns = BASELINK_FUNCTIONS (baselink); |
6f67f000 | 9412 | optype = BASELINK_OPTYPE (baselink); |
ee76b931 MM |
9413 | if (TREE_CODE (fns) == TEMPLATE_ID_EXPR) |
9414 | { | |
9415 | template_id_p = true; | |
9416 | template_args = TREE_OPERAND (fns, 1); | |
9417 | fns = TREE_OPERAND (fns, 0); | |
bf12d54d NS |
9418 | if (template_args) |
9419 | template_args = tsubst_template_args (template_args, args, | |
9420 | complain, in_decl); | |
ee76b931 MM |
9421 | } |
9422 | name = DECL_NAME (get_first_fn (fns)); | |
9423 | baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1); | |
c8094d83 | 9424 | |
9b60dfd7 MM |
9425 | /* If lookup found a single function, mark it as used at this |
9426 | point. (If it lookup found multiple functions the one selected | |
9427 | later by overload resolution will be marked as used at that | |
9428 | point.) */ | |
9429 | if (BASELINK_P (baselink)) | |
9430 | fns = BASELINK_FUNCTIONS (baselink); | |
9431 | if (!template_id_p && !really_overloaded_fn (fns)) | |
9432 | mark_used (OVL_CURRENT (fns)); | |
9433 | ||
9434 | /* Add back the template arguments, if present. */ | |
ee76b931 | 9435 | if (BASELINK_P (baselink) && template_id_p) |
c8094d83 | 9436 | BASELINK_FUNCTIONS (baselink) |
ee76b931 MM |
9437 | = build_nt (TEMPLATE_ID_EXPR, |
9438 | BASELINK_FUNCTIONS (baselink), | |
9439 | template_args); | |
6f67f000 MM |
9440 | /* Update the conversion operator type. */ |
9441 | BASELINK_OPTYPE (baselink) | |
9442 | = tsubst (optype, args, complain, in_decl); | |
9b60dfd7 | 9443 | |
ee76b931 MM |
9444 | if (!object_type) |
9445 | object_type = current_class_type; | |
c8094d83 | 9446 | return adjust_result_of_qualified_name_lookup (baselink, |
ee76b931 MM |
9447 | qualifying_scope, |
9448 | object_type); | |
9449 | } | |
9450 | ||
9451 | /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is | |
9452 | true if the qualified-id will be a postfix-expression in-and-of | |
9453 | itself; false if more of the postfix-expression follows the | |
9454 | QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand | |
9455 | of "&". */ | |
9456 | ||
9457 | static tree | |
c8094d83 | 9458 | tsubst_qualified_id (tree qualified_id, tree args, |
ee76b931 MM |
9459 | tsubst_flags_t complain, tree in_decl, |
9460 | bool done, bool address_p) | |
9461 | { | |
9462 | tree expr; | |
9463 | tree scope; | |
9464 | tree name; | |
9465 | bool is_template; | |
9466 | tree template_args; | |
9467 | ||
50bc768d | 9468 | gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF); |
ee76b931 | 9469 | |
ee76b931 MM |
9470 | /* Figure out what name to look up. */ |
9471 | name = TREE_OPERAND (qualified_id, 1); | |
9472 | if (TREE_CODE (name) == TEMPLATE_ID_EXPR) | |
9473 | { | |
9474 | is_template = true; | |
bf12d54d NS |
9475 | template_args = TREE_OPERAND (name, 1); |
9476 | if (template_args) | |
9477 | template_args = tsubst_template_args (template_args, args, | |
9478 | complain, in_decl); | |
ee76b931 MM |
9479 | name = TREE_OPERAND (name, 0); |
9480 | } | |
9481 | else | |
9482 | { | |
9483 | is_template = false; | |
9484 | template_args = NULL_TREE; | |
9485 | } | |
9486 | ||
6eeba0cc MM |
9487 | /* Substitute into the qualifying scope. When there are no ARGS, we |
9488 | are just trying to simplify a non-dependent expression. In that | |
9489 | case the qualifying scope may be dependent, and, in any case, | |
9490 | substituting will not help. */ | |
9491 | scope = TREE_OPERAND (qualified_id, 0); | |
9492 | if (args) | |
9493 | { | |
9494 | scope = tsubst (scope, args, complain, in_decl); | |
9495 | expr = tsubst_copy (name, args, complain, in_decl); | |
9496 | } | |
9497 | else | |
9498 | expr = name; | |
10b1d5e7 | 9499 | |
ab73670a | 9500 | if (dependent_type_p (scope)) |
3db45ab5 MS |
9501 | return build_qualified_name (/*type=*/NULL_TREE, |
9502 | scope, expr, | |
02ed62dd | 9503 | QUALIFIED_NAME_IS_TEMPLATE (qualified_id)); |
c8094d83 | 9504 | |
5e08432e | 9505 | if (!BASELINK_P (name) && !DECL_P (expr)) |
12483c9f | 9506 | { |
8ca4bf25 MM |
9507 | if (TREE_CODE (expr) == BIT_NOT_EXPR) |
9508 | /* If this were actually a destructor call, it would have been | |
9509 | parsed as such by the parser. */ | |
9510 | expr = error_mark_node; | |
9511 | else | |
9512 | expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false); | |
12483c9f NS |
9513 | if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL |
9514 | ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL) | |
9515 | { | |
9516 | if (complain & tf_error) | |
b2693faf | 9517 | { |
0f51ccfc | 9518 | error ("dependent-name %qE is parsed as a non-type, but " |
b2693faf | 9519 | "instantiation yields a type", qualified_id); |
0f51ccfc | 9520 | inform ("say %<typename %E%> if a type is meant", qualified_id); |
b2693faf | 9521 | } |
12483c9f NS |
9522 | return error_mark_node; |
9523 | } | |
9524 | } | |
c8094d83 | 9525 | |
279b8466 | 9526 | if (DECL_P (expr)) |
8f78f01f MM |
9527 | { |
9528 | check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE, | |
9529 | scope); | |
9530 | /* Remember that there was a reference to this entity. */ | |
9531 | mark_used (expr); | |
9532 | } | |
9533 | ||
9534 | if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST) | |
9535 | { | |
9536 | if (complain & tf_error) | |
c8094d83 | 9537 | qualified_name_lookup_error (scope, |
8f78f01f MM |
9538 | TREE_OPERAND (qualified_id, 1), |
9539 | expr); | |
9540 | return error_mark_node; | |
9541 | } | |
ee76b931 MM |
9542 | |
9543 | if (is_template) | |
10b1d5e7 | 9544 | expr = lookup_template_function (expr, template_args); |
ee76b931 | 9545 | |
22038b2c | 9546 | if (expr == error_mark_node && complain & tf_error) |
8f78f01f MM |
9547 | qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1), |
9548 | expr); | |
22038b2c | 9549 | else if (TYPE_P (scope)) |
ee76b931 | 9550 | { |
c8094d83 | 9551 | expr = (adjust_result_of_qualified_name_lookup |
ee76b931 | 9552 | (expr, scope, current_class_type)); |
3db45ab5 | 9553 | expr = (finish_qualified_id_expr |
02ed62dd MM |
9554 | (scope, expr, done, address_p, |
9555 | QUALIFIED_NAME_IS_TEMPLATE (qualified_id), | |
9556 | /*template_arg_p=*/false)); | |
ee76b931 | 9557 | } |
c8094d83 | 9558 | |
015c2c66 MM |
9559 | /* Expressions do not generally have reference type. */ |
9560 | if (TREE_CODE (expr) != SCOPE_REF | |
9561 | /* However, if we're about to form a pointer-to-member, we just | |
9562 | want the referenced member referenced. */ | |
9563 | && TREE_CODE (expr) != OFFSET_REF) | |
aec5ba60 | 9564 | expr = convert_from_reference (expr); |
ee76b931 MM |
9565 | |
9566 | return expr; | |
9567 | } | |
9568 | ||
00d3396f JM |
9569 | /* Like tsubst, but deals with expressions. This function just replaces |
9570 | template parms; to finish processing the resultant expression, use | |
9571 | tsubst_expr. */ | |
9572 | ||
14d22dd6 | 9573 | static tree |
3a978d72 | 9574 | tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl) |
5566b478 MS |
9575 | { |
9576 | enum tree_code code; | |
8452b1d3 | 9577 | tree r; |
8d08fdba | 9578 | |
5566b478 MS |
9579 | if (t == NULL_TREE || t == error_mark_node) |
9580 | return t; | |
9581 | ||
9582 | code = TREE_CODE (t); | |
b7484fbe | 9583 | |
5566b478 MS |
9584 | switch (code) |
9585 | { | |
9586 | case PARM_DECL: | |
a723baf1 | 9587 | r = retrieve_local_specialization (t); |
50bc768d | 9588 | gcc_assert (r != NULL); |
5d80a306 DG |
9589 | if (TREE_CODE (r) == ARGUMENT_PACK_SELECT) |
9590 | r = ARGUMENT_PACK_SELECT_ARG (r); | |
c0694c4b | 9591 | mark_used (r); |
a723baf1 | 9592 | return r; |
5566b478 MS |
9593 | |
9594 | case CONST_DECL: | |
ed44da02 MM |
9595 | { |
9596 | tree enum_type; | |
9597 | tree v; | |
9598 | ||
a723baf1 MM |
9599 | if (DECL_TEMPLATE_PARM_P (t)) |
9600 | return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl); | |
7b6d72fc MM |
9601 | /* There is no need to substitute into namespace-scope |
9602 | enumerators. */ | |
9603 | if (DECL_NAMESPACE_SCOPE_P (t)) | |
ed44da02 | 9604 | return t; |
d5a10cf0 MM |
9605 | /* If ARGS is NULL, then T is known to be non-dependent. */ |
9606 | if (args == NULL_TREE) | |
8a784e4a | 9607 | return integral_constant_value (t); |
ed44da02 MM |
9608 | |
9609 | /* Unfortunately, we cannot just call lookup_name here. | |
9188c363 | 9610 | Consider: |
c8094d83 | 9611 | |
9188c363 MM |
9612 | template <int I> int f() { |
9613 | enum E { a = I }; | |
9614 | struct S { void g() { E e = a; } }; | |
9615 | }; | |
c8094d83 | 9616 | |
9188c363 MM |
9617 | When we instantiate f<7>::S::g(), say, lookup_name is not |
9618 | clever enough to find f<7>::a. */ | |
c8094d83 MS |
9619 | enum_type |
9620 | = tsubst_aggr_type (TREE_TYPE (t), args, complain, in_decl, | |
ed44da02 MM |
9621 | /*entering_scope=*/0); |
9622 | ||
c8094d83 MS |
9623 | for (v = TYPE_VALUES (enum_type); |
9624 | v != NULL_TREE; | |
ed44da02 MM |
9625 | v = TREE_CHAIN (v)) |
9626 | if (TREE_PURPOSE (v) == DECL_NAME (t)) | |
9627 | return TREE_VALUE (v); | |
9628 | ||
9629 | /* We didn't find the name. That should never happen; if | |
9630 | name-lookup found it during preliminary parsing, we | |
9631 | should find it again here during instantiation. */ | |
315fb5db | 9632 | gcc_unreachable (); |
ed44da02 | 9633 | } |
db04386f | 9634 | return t; |
ed44da02 | 9635 | |
5566b478 MS |
9636 | case FIELD_DECL: |
9637 | if (DECL_CONTEXT (t)) | |
9638 | { | |
0978790f | 9639 | tree ctx; |
0978790f | 9640 | |
4393e105 | 9641 | ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl, |
36a117a5 | 9642 | /*entering_scope=*/1); |
0978790f | 9643 | if (ctx != DECL_CONTEXT (t)) |
bad1f462 KL |
9644 | { |
9645 | tree r = lookup_field (ctx, DECL_NAME (t), 0, false); | |
9646 | if (!r) | |
9647 | { | |
9648 | if (complain & tf_error) | |
9649 | error ("using invalid field %qD", t); | |
9650 | return error_mark_node; | |
9651 | } | |
9652 | return r; | |
9653 | } | |
5566b478 | 9654 | } |
c8094d83 | 9655 | |
5566b478 MS |
9656 | return t; |
9657 | ||
9658 | case VAR_DECL: | |
9659 | case FUNCTION_DECL: | |
a723baf1 MM |
9660 | if ((DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)) |
9661 | || local_variable_p (t)) | |
4393e105 | 9662 | t = tsubst (t, args, complain, in_decl); |
5566b478 MS |
9663 | mark_used (t); |
9664 | return t; | |
9665 | ||
a723baf1 | 9666 | case BASELINK: |
ee76b931 | 9667 | return tsubst_baselink (t, current_class_type, args, complain, in_decl); |
a723baf1 | 9668 | |
98c1c668 | 9669 | case TEMPLATE_DECL: |
a723baf1 | 9670 | if (DECL_TEMPLATE_TEMPLATE_PARM_P (t)) |
c8094d83 | 9671 | return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)), |
a723baf1 | 9672 | args, complain, in_decl); |
c7222c02 | 9673 | else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t)) |
4393e105 | 9674 | return tsubst (t, args, complain, in_decl); |
fcea7401 KL |
9675 | else if (DECL_CLASS_SCOPE_P (t) |
9676 | && uses_template_parms (DECL_CONTEXT (t))) | |
9677 | { | |
9678 | /* Template template argument like the following example need | |
9679 | special treatment: | |
9680 | ||
9681 | template <template <class> class TT> struct C {}; | |
9682 | template <class T> struct D { | |
9683 | template <class U> struct E {}; | |
0cbd7506 | 9684 | C<E> c; // #1 |
fcea7401 KL |
9685 | }; |
9686 | D<int> d; // #2 | |
9687 | ||
9688 | We are processing the template argument `E' in #1 for | |
9689 | the template instantiation #2. Originally, `E' is a | |
9690 | TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we | |
9691 | have to substitute this with one having context `D<int>'. */ | |
9692 | ||
9693 | tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl); | |
9694 | return lookup_field (context, DECL_NAME(t), 0, false); | |
9695 | } | |
98c1c668 | 9696 | else |
fcea7401 | 9697 | /* Ordinary template template argument. */ |
98c1c668 JM |
9698 | return t; |
9699 | ||
5566b478 MS |
9700 | case CAST_EXPR: |
9701 | case REINTERPRET_CAST_EXPR: | |
e92cc029 MS |
9702 | case CONST_CAST_EXPR: |
9703 | case STATIC_CAST_EXPR: | |
9704 | case DYNAMIC_CAST_EXPR: | |
51924768 | 9705 | case NOP_EXPR: |
5566b478 | 9706 | return build1 |
4393e105 MM |
9707 | (code, tsubst (TREE_TYPE (t), args, complain, in_decl), |
9708 | tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl)); | |
5566b478 | 9709 | |
5d80a306 DG |
9710 | case SIZEOF_EXPR: |
9711 | if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))) | |
9712 | { | |
9713 | /* We only want to compute the number of arguments. */ | |
9714 | tree expanded = tsubst_pack_expansion (TREE_OPERAND (t, 0), args, | |
9715 | complain, in_decl); | |
4745e4eb JJ |
9716 | if (expanded == error_mark_node) |
9717 | return error_mark_node; | |
5d80a306 DG |
9718 | return build_int_cst (size_type_node, TREE_VEC_LENGTH (expanded)); |
9719 | } | |
9720 | /* Fall through */ | |
9721 | ||
5566b478 | 9722 | case INDIRECT_REF: |
5566b478 MS |
9723 | case NEGATE_EXPR: |
9724 | case TRUTH_NOT_EXPR: | |
b87692e5 | 9725 | case BIT_NOT_EXPR: |
5566b478 | 9726 | case ADDR_EXPR: |
392e3d51 | 9727 | case UNARY_PLUS_EXPR: /* Unary + */ |
abff8e06 | 9728 | case ALIGNOF_EXPR: |
5566b478 | 9729 | case ARROW_EXPR: |
fc378698 | 9730 | case THROW_EXPR: |
5156628f | 9731 | case TYPEID_EXPR: |
f5733617 SS |
9732 | case REALPART_EXPR: |
9733 | case IMAGPART_EXPR: | |
5566b478 | 9734 | return build1 |
6a629cac | 9735 | (code, tsubst (TREE_TYPE (t), args, complain, in_decl), |
4393e105 | 9736 | tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl)); |
5566b478 | 9737 | |
a723baf1 MM |
9738 | case COMPONENT_REF: |
9739 | { | |
9740 | tree object; | |
9741 | tree name; | |
9742 | ||
9743 | object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl); | |
9744 | name = TREE_OPERAND (t, 1); | |
c8094d83 | 9745 | if (TREE_CODE (name) == BIT_NOT_EXPR) |
a723baf1 MM |
9746 | { |
9747 | name = tsubst_copy (TREE_OPERAND (name, 0), args, | |
9748 | complain, in_decl); | |
9749 | name = build1 (BIT_NOT_EXPR, NULL_TREE, name); | |
9750 | } | |
9751 | else if (TREE_CODE (name) == SCOPE_REF | |
9752 | && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR) | |
9753 | { | |
9754 | tree base = tsubst_copy (TREE_OPERAND (name, 0), args, | |
9755 | complain, in_decl); | |
9756 | name = TREE_OPERAND (name, 1); | |
9757 | name = tsubst_copy (TREE_OPERAND (name, 0), args, | |
9758 | complain, in_decl); | |
9759 | name = build1 (BIT_NOT_EXPR, NULL_TREE, name); | |
02ed62dd | 9760 | name = build_qualified_name (/*type=*/NULL_TREE, |
3db45ab5 | 9761 | base, name, |
02ed62dd | 9762 | /*template_p=*/false); |
a723baf1 | 9763 | } |
ee76b931 | 9764 | else if (TREE_CODE (name) == BASELINK) |
c8094d83 MS |
9765 | name = tsubst_baselink (name, |
9766 | non_reference (TREE_TYPE (object)), | |
9767 | args, complain, | |
ee76b931 | 9768 | in_decl); |
a723baf1 | 9769 | else |
ee76b931 | 9770 | name = tsubst_copy (name, args, complain, in_decl); |
44de5aeb | 9771 | return build_nt (COMPONENT_REF, object, name, NULL_TREE); |
a723baf1 MM |
9772 | } |
9773 | ||
5566b478 MS |
9774 | case PLUS_EXPR: |
9775 | case MINUS_EXPR: | |
9776 | case MULT_EXPR: | |
9777 | case TRUNC_DIV_EXPR: | |
9778 | case CEIL_DIV_EXPR: | |
9779 | case FLOOR_DIV_EXPR: | |
9780 | case ROUND_DIV_EXPR: | |
9781 | case EXACT_DIV_EXPR: | |
9782 | case BIT_AND_EXPR: | |
5566b478 MS |
9783 | case BIT_IOR_EXPR: |
9784 | case BIT_XOR_EXPR: | |
9785 | case TRUNC_MOD_EXPR: | |
9786 | case FLOOR_MOD_EXPR: | |
9787 | case TRUTH_ANDIF_EXPR: | |
9788 | case TRUTH_ORIF_EXPR: | |
9789 | case TRUTH_AND_EXPR: | |
9790 | case TRUTH_OR_EXPR: | |
9791 | case RSHIFT_EXPR: | |
9792 | case LSHIFT_EXPR: | |
9793 | case RROTATE_EXPR: | |
9794 | case LROTATE_EXPR: | |
9795 | case EQ_EXPR: | |
9796 | case NE_EXPR: | |
9797 | case MAX_EXPR: | |
9798 | case MIN_EXPR: | |
9799 | case LE_EXPR: | |
9800 | case GE_EXPR: | |
9801 | case LT_EXPR: | |
9802 | case GT_EXPR: | |
5566b478 | 9803 | case COMPOUND_EXPR: |
5566b478 MS |
9804 | case DOTSTAR_EXPR: |
9805 | case MEMBER_REF: | |
519c9806 MM |
9806 | case PREDECREMENT_EXPR: |
9807 | case PREINCREMENT_EXPR: | |
9808 | case POSTDECREMENT_EXPR: | |
9809 | case POSTINCREMENT_EXPR: | |
5566b478 | 9810 | return build_nt |
4393e105 MM |
9811 | (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl), |
9812 | tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl)); | |
5566b478 | 9813 | |
02ed62dd MM |
9814 | case SCOPE_REF: |
9815 | return build_qualified_name (/*type=*/NULL_TREE, | |
9816 | tsubst_copy (TREE_OPERAND (t, 0), | |
9817 | args, complain, in_decl), | |
9818 | tsubst_copy (TREE_OPERAND (t, 1), | |
9819 | args, complain, in_decl), | |
9820 | QUALIFIED_NAME_IS_TEMPLATE (t)); | |
9821 | ||
d8987adb NS |
9822 | case ARRAY_REF: |
9823 | return build_nt | |
9824 | (ARRAY_REF, | |
9825 | tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl), | |
9826 | tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl), | |
9827 | NULL_TREE, NULL_TREE); | |
9828 | ||
5566b478 | 9829 | case CALL_EXPR: |
5039610b SL |
9830 | { |
9831 | int n = VL_EXP_OPERAND_LENGTH (t); | |
9832 | tree result = build_vl_exp (CALL_EXPR, n); | |
9833 | int i; | |
9834 | for (i = 0; i < n; i++) | |
9835 | TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args, | |
9836 | complain, in_decl); | |
9837 | return result; | |
9838 | } | |
5566b478 | 9839 | |
5566b478 MS |
9840 | case COND_EXPR: |
9841 | case MODOP_EXPR: | |
40242ccf | 9842 | case PSEUDO_DTOR_EXPR: |
67da3287 | 9843 | { |
8452b1d3 | 9844 | r = build_nt |
4393e105 MM |
9845 | (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl), |
9846 | tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl), | |
9847 | tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl)); | |
e4c2c34b | 9848 | TREE_NO_WARNING (r) = TREE_NO_WARNING (t); |
67da3287 MM |
9849 | return r; |
9850 | } | |
5566b478 MS |
9851 | |
9852 | case NEW_EXPR: | |
9853 | { | |
8452b1d3 | 9854 | r = build_nt |
4393e105 MM |
9855 | (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl), |
9856 | tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl), | |
9857 | tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl)); | |
5566b478 MS |
9858 | NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t); |
9859 | return r; | |
9860 | } | |
9861 | ||
9862 | case DELETE_EXPR: | |
9863 | { | |
8452b1d3 | 9864 | r = build_nt |
4393e105 MM |
9865 | (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl), |
9866 | tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl)); | |
5566b478 MS |
9867 | DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t); |
9868 | DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t); | |
9869 | return r; | |
9870 | } | |
9871 | ||
386b8a85 JM |
9872 | case TEMPLATE_ID_EXPR: |
9873 | { | |
0cbd7506 | 9874 | /* Substituted template arguments */ |
a91db711 NS |
9875 | tree fn = TREE_OPERAND (t, 0); |
9876 | tree targs = TREE_OPERAND (t, 1); | |
856216bb | 9877 | |
a91db711 | 9878 | fn = tsubst_copy (fn, args, complain, in_decl); |
bf12d54d NS |
9879 | if (targs) |
9880 | targs = tsubst_template_args (targs, args, complain, in_decl); | |
c8094d83 | 9881 | |
a91db711 | 9882 | return lookup_template_function (fn, targs); |
386b8a85 JM |
9883 | } |
9884 | ||
5566b478 MS |
9885 | case TREE_LIST: |
9886 | { | |
9887 | tree purpose, value, chain; | |
9888 | ||
9889 | if (t == void_list_node) | |
9890 | return t; | |
9891 | ||
9892 | purpose = TREE_PURPOSE (t); | |
9893 | if (purpose) | |
4393e105 | 9894 | purpose = tsubst_copy (purpose, args, complain, in_decl); |
5566b478 MS |
9895 | value = TREE_VALUE (t); |
9896 | if (value) | |
4393e105 | 9897 | value = tsubst_copy (value, args, complain, in_decl); |
5566b478 MS |
9898 | chain = TREE_CHAIN (t); |
9899 | if (chain && chain != void_type_node) | |
4393e105 | 9900 | chain = tsubst_copy (chain, args, complain, in_decl); |
5566b478 MS |
9901 | if (purpose == TREE_PURPOSE (t) |
9902 | && value == TREE_VALUE (t) | |
9903 | && chain == TREE_CHAIN (t)) | |
9904 | return t; | |
9905 | return tree_cons (purpose, value, chain); | |
9906 | } | |
9907 | ||
9908 | case RECORD_TYPE: | |
9909 | case UNION_TYPE: | |
9910 | case ENUMERAL_TYPE: | |
9911 | case INTEGER_TYPE: | |
9912 | case TEMPLATE_TYPE_PARM: | |
73b0fce8 | 9913 | case TEMPLATE_TEMPLATE_PARM: |
a1281f45 | 9914 | case BOUND_TEMPLATE_TEMPLATE_PARM: |
f84b4be9 | 9915 | case TEMPLATE_PARM_INDEX: |
5566b478 MS |
9916 | case POINTER_TYPE: |
9917 | case REFERENCE_TYPE: | |
9918 | case OFFSET_TYPE: | |
9919 | case FUNCTION_TYPE: | |
9920 | case METHOD_TYPE: | |
9921 | case ARRAY_TYPE: | |
9922 | case TYPENAME_TYPE: | |
b8c6534b | 9923 | case UNBOUND_CLASS_TEMPLATE: |
2a2b1d56 | 9924 | case TYPEOF_TYPE: |
3ad6a8e1 | 9925 | case DECLTYPE_TYPE: |
f84b4be9 | 9926 | case TYPE_DECL: |
4393e105 | 9927 | return tsubst (t, args, complain, in_decl); |
5566b478 | 9928 | |
e92cc029 | 9929 | case IDENTIFIER_NODE: |
421844e7 | 9930 | if (IDENTIFIER_TYPENAME_P (t)) |
1f6e1acc AS |
9931 | { |
9932 | tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl); | |
1f84ec23 | 9933 | return mangle_conv_op_name_for_type (new_type); |
1f6e1acc | 9934 | } |
e92cc029 MS |
9935 | else |
9936 | return t; | |
9937 | ||
5156628f | 9938 | case CONSTRUCTOR: |
4038c495 GB |
9939 | /* This is handled by tsubst_copy_and_build. */ |
9940 | gcc_unreachable (); | |
5156628f | 9941 | |
371534a9 | 9942 | case VA_ARG_EXPR: |
ea333e1c | 9943 | return build_x_va_arg (tsubst_copy (TREE_OPERAND (t, 0), args, complain, |
c2ea3a40 NS |
9944 | in_decl), |
9945 | tsubst (TREE_TYPE (t), args, complain, in_decl)); | |
f9817201 | 9946 | |
543a0daa RH |
9947 | case CLEANUP_POINT_EXPR: |
9948 | /* We shouldn't have built any of these during initial template | |
9949 | generation. Instead, they should be built during instantiation | |
9950 | in response to the saved STMT_IS_FULL_EXPR_P setting. */ | |
315fb5db | 9951 | gcc_unreachable (); |
543a0daa | 9952 | |
7eab6e7b MM |
9953 | case OFFSET_REF: |
9954 | mark_used (TREE_OPERAND (t, 1)); | |
9955 | return t; | |
9956 | ||
5d80a306 DG |
9957 | case EXPR_PACK_EXPANSION: |
9958 | error ("invalid use of pack expansion expression"); | |
9959 | return error_mark_node; | |
9960 | ||
9961 | case NONTYPE_ARGUMENT_PACK: | |
9962 | error ("use %<...%> to expand argument pack"); | |
9963 | return error_mark_node; | |
9964 | ||
5566b478 MS |
9965 | default: |
9966 | return t; | |
9967 | } | |
9968 | } | |
9969 | ||
1799e5d5 RH |
9970 | /* Like tsubst_copy, but specifically for OpenMP clauses. */ |
9971 | ||
9972 | static tree | |
9973 | tsubst_omp_clauses (tree clauses, tree args, tsubst_flags_t complain, | |
9974 | tree in_decl) | |
9975 | { | |
9976 | tree new_clauses = NULL, nc, oc; | |
9977 | ||
9978 | for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc)) | |
9979 | { | |
9980 | nc = copy_node (oc); | |
9981 | OMP_CLAUSE_CHAIN (nc) = new_clauses; | |
9982 | new_clauses = nc; | |
9983 | ||
9984 | switch (OMP_CLAUSE_CODE (nc)) | |
9985 | { | |
9986 | case OMP_CLAUSE_PRIVATE: | |
9987 | case OMP_CLAUSE_SHARED: | |
9988 | case OMP_CLAUSE_FIRSTPRIVATE: | |
9989 | case OMP_CLAUSE_LASTPRIVATE: | |
9990 | case OMP_CLAUSE_REDUCTION: | |
9991 | case OMP_CLAUSE_COPYIN: | |
9992 | case OMP_CLAUSE_COPYPRIVATE: | |
9993 | case OMP_CLAUSE_IF: | |
9994 | case OMP_CLAUSE_NUM_THREADS: | |
9995 | case OMP_CLAUSE_SCHEDULE: | |
9996 | OMP_CLAUSE_OPERAND (nc, 0) | |
015c2c66 MM |
9997 | = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain, |
9998 | in_decl, /*integral_constant_expression_p=*/false); | |
1799e5d5 RH |
9999 | break; |
10000 | case OMP_CLAUSE_NOWAIT: | |
10001 | case OMP_CLAUSE_ORDERED: | |
10002 | case OMP_CLAUSE_DEFAULT: | |
10003 | break; | |
10004 | default: | |
10005 | gcc_unreachable (); | |
10006 | } | |
10007 | } | |
10008 | ||
10009 | return finish_omp_clauses (nreverse (new_clauses)); | |
10010 | } | |
10011 | ||
f74dcfb7 JJ |
10012 | /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */ |
10013 | ||
10014 | static tree | |
10015 | tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain, | |
10016 | tree in_decl) | |
10017 | { | |
10018 | #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl) | |
10019 | ||
10020 | tree purpose, value, chain; | |
10021 | ||
10022 | if (t == NULL) | |
10023 | return t; | |
10024 | ||
10025 | if (TREE_CODE (t) != TREE_LIST) | |
10026 | return tsubst_copy_and_build (t, args, complain, in_decl, | |
015c2c66 MM |
10027 | /*function_p=*/false, |
10028 | /*integral_constant_expression_p=*/false); | |
f74dcfb7 JJ |
10029 | |
10030 | if (t == void_list_node) | |
10031 | return t; | |
10032 | ||
10033 | purpose = TREE_PURPOSE (t); | |
10034 | if (purpose) | |
10035 | purpose = RECUR (purpose); | |
10036 | value = TREE_VALUE (t); | |
10037 | if (value) | |
10038 | value = RECUR (value); | |
10039 | chain = TREE_CHAIN (t); | |
10040 | if (chain && chain != void_type_node) | |
10041 | chain = RECUR (chain); | |
10042 | return tree_cons (purpose, value, chain); | |
10043 | #undef RECUR | |
10044 | } | |
10045 | ||
cc23546e JO |
10046 | /* Like tsubst_copy for expressions, etc. but also does semantic |
10047 | processing. */ | |
00d3396f | 10048 | |
14d22dd6 | 10049 | static tree |
015c2c66 MM |
10050 | tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl, |
10051 | bool integral_constant_expression_p) | |
5566b478 | 10052 | { |
015c2c66 MM |
10053 | #define RECUR(NODE) \ |
10054 | tsubst_expr ((NODE), args, complain, in_decl, \ | |
10055 | integral_constant_expression_p) | |
10056 | ||
fd10dd09 | 10057 | tree stmt, tmp; |
558475f0 | 10058 | |
5566b478 MS |
10059 | if (t == NULL_TREE || t == error_mark_node) |
10060 | return t; | |
10061 | ||
93409b8c PB |
10062 | if (EXPR_HAS_LOCATION (t)) |
10063 | input_location = EXPR_LOCATION (t); | |
7c34ced1 RH |
10064 | if (STATEMENT_CODE_P (TREE_CODE (t))) |
10065 | current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t); | |
10066 | ||
5566b478 | 10067 | switch (TREE_CODE (t)) |
8d08fdba | 10068 | { |
325c3691 RH |
10069 | case STATEMENT_LIST: |
10070 | { | |
10071 | tree_stmt_iterator i; | |
10072 | for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i)) | |
015c2c66 | 10073 | RECUR (tsi_stmt (i)); |
325c3691 RH |
10074 | break; |
10075 | } | |
10076 | ||
558475f0 | 10077 | case CTOR_INITIALIZER: |
c8094d83 | 10078 | finish_mem_initializers (tsubst_initializer_list |
2282d28d MM |
10079 | (TREE_OPERAND (t, 0), args)); |
10080 | break; | |
558475f0 | 10081 | |
5088b058 | 10082 | case RETURN_EXPR: |
015c2c66 | 10083 | finish_return_stmt (RECUR (TREE_OPERAND (t, 0))); |
5566b478 MS |
10084 | break; |
10085 | ||
10086 | case EXPR_STMT: | |
015c2c66 | 10087 | tmp = RECUR (EXPR_STMT_EXPR (t)); |
7c34ced1 RH |
10088 | if (EXPR_STMT_STMT_EXPR_RESULT (t)) |
10089 | finish_stmt_expr_expr (tmp, cur_stmt_expr); | |
10090 | else | |
10091 | finish_expr_stmt (tmp); | |
10092 | break; | |
5566b478 | 10093 | |
9da99f7d | 10094 | case USING_STMT: |
015c2c66 | 10095 | do_using_directive (RECUR (USING_STMT_NAMESPACE (t))); |
9da99f7d | 10096 | break; |
c8094d83 | 10097 | |
350fae66 | 10098 | case DECL_EXPR: |
5566b478 | 10099 | { |
e0942dcd MM |
10100 | tree decl; |
10101 | tree init; | |
5566b478 | 10102 | |
350fae66 | 10103 | decl = DECL_EXPR_DECL (t); |
acef433b MM |
10104 | if (TREE_CODE (decl) == LABEL_DECL) |
10105 | finish_label_decl (DECL_NAME (decl)); | |
fbfe8c9e NS |
10106 | else if (TREE_CODE (decl) == USING_DECL) |
10107 | { | |
98ed9dae | 10108 | tree scope = USING_DECL_SCOPE (decl); |
fbfe8c9e | 10109 | tree name = DECL_NAME (decl); |
22038b2c | 10110 | tree decl; |
c8094d83 | 10111 | |
015c2c66 | 10112 | scope = RECUR (scope); |
22038b2c | 10113 | decl = lookup_qualified_name (scope, name, |
12483c9f NS |
10114 | /*is_type_p=*/false, |
10115 | /*complain=*/false); | |
8f78f01f MM |
10116 | if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST) |
10117 | qualified_name_lookup_error (scope, name, decl); | |
22038b2c | 10118 | else |
ed5f054f | 10119 | do_local_using_decl (decl, scope, name); |
fbfe8c9e | 10120 | } |
acef433b MM |
10121 | else |
10122 | { | |
10123 | init = DECL_INITIAL (decl); | |
10124 | decl = tsubst (decl, args, complain, in_decl); | |
ce2e5191 NS |
10125 | if (decl != error_mark_node) |
10126 | { | |
0cbd7506 MS |
10127 | /* By marking the declaration as instantiated, we avoid |
10128 | trying to instantiate it. Since instantiate_decl can't | |
10129 | handle local variables, and since we've already done | |
10130 | all that needs to be done, that's the right thing to | |
10131 | do. */ | |
10132 | if (TREE_CODE (decl) == VAR_DECL) | |
10133 | DECL_TEMPLATE_INSTANTIATED (decl) = 1; | |
edebf865 MM |
10134 | if (TREE_CODE (decl) == VAR_DECL |
10135 | && ANON_AGGR_TYPE_P (TREE_TYPE (decl))) | |
10136 | /* Anonymous aggregates are a special case. */ | |
10137 | finish_anon_union (decl); | |
c8094d83 | 10138 | else |
ed2fa432 | 10139 | { |
edebf865 | 10140 | maybe_push_decl (decl); |
39703eb9 MM |
10141 | if (TREE_CODE (decl) == VAR_DECL |
10142 | && DECL_PRETTY_FUNCTION_P (decl)) | |
edebf865 MM |
10143 | { |
10144 | /* For __PRETTY_FUNCTION__ we have to adjust the | |
10145 | initializer. */ | |
10146 | const char *const name | |
10147 | = cxx_printable_name (current_function_decl, 2); | |
bb885938 | 10148 | init = cp_fname_init (name, &TREE_TYPE (decl)); |
edebf865 MM |
10149 | } |
10150 | else | |
268127ce DG |
10151 | { |
10152 | tree t = RECUR (init); | |
10153 | ||
10154 | if (init && !t) | |
10155 | /* If we had an initializer but it | |
10156 | instantiated to nothing, | |
10157 | value-initialize the object. This will | |
10158 | only occur when the initializer was a | |
10159 | pack expansion where the parameter packs | |
10160 | used in that expansion were of length | |
10161 | zero. */ | |
10162 | init = build_default_init (TREE_TYPE (decl), | |
10163 | NULL_TREE); | |
10164 | else | |
10165 | init = t; | |
10166 | } | |
10167 | ||
d174af6c | 10168 | finish_decl (decl, init, NULL_TREE); |
ed2fa432 | 10169 | } |
ce2e5191 | 10170 | } |
acef433b | 10171 | } |
fd10dd09 | 10172 | |
350fae66 | 10173 | /* A DECL_EXPR can also be used as an expression, in the condition |
325c3691 RH |
10174 | clause of an if/for/while construct. */ |
10175 | return decl; | |
5566b478 | 10176 | } |
8d08fdba | 10177 | |
5566b478 | 10178 | case FOR_STMT: |
7c34ced1 | 10179 | stmt = begin_for_stmt (); |
015c2c66 | 10180 | RECUR (FOR_INIT_STMT (t)); |
7c34ced1 | 10181 | finish_for_init_stmt (stmt); |
015c2c66 | 10182 | tmp = RECUR (FOR_COND (t)); |
7c34ced1 | 10183 | finish_for_cond (tmp, stmt); |
015c2c66 | 10184 | tmp = RECUR (FOR_EXPR (t)); |
7c34ced1 | 10185 | finish_for_expr (tmp, stmt); |
015c2c66 | 10186 | RECUR (FOR_BODY (t)); |
7c34ced1 | 10187 | finish_for_stmt (stmt); |
5566b478 | 10188 | break; |
8d08fdba | 10189 | |
5566b478 | 10190 | case WHILE_STMT: |
7c34ced1 | 10191 | stmt = begin_while_stmt (); |
015c2c66 | 10192 | tmp = RECUR (WHILE_COND (t)); |
7c34ced1 | 10193 | finish_while_stmt_cond (tmp, stmt); |
015c2c66 | 10194 | RECUR (WHILE_BODY (t)); |
7c34ced1 | 10195 | finish_while_stmt (stmt); |
5566b478 | 10196 | break; |
8d08fdba | 10197 | |
5566b478 | 10198 | case DO_STMT: |
7c34ced1 | 10199 | stmt = begin_do_stmt (); |
015c2c66 | 10200 | RECUR (DO_BODY (t)); |
7c34ced1 | 10201 | finish_do_body (stmt); |
015c2c66 | 10202 | tmp = RECUR (DO_COND (t)); |
7c34ced1 | 10203 | finish_do_stmt (tmp, stmt); |
5566b478 | 10204 | break; |
a0a33927 | 10205 | |
5566b478 | 10206 | case IF_STMT: |
7c34ced1 | 10207 | stmt = begin_if_stmt (); |
015c2c66 | 10208 | tmp = RECUR (IF_COND (t)); |
7c34ced1 | 10209 | finish_if_stmt_cond (tmp, stmt); |
015c2c66 | 10210 | RECUR (THEN_CLAUSE (t)); |
7c34ced1 | 10211 | finish_then_clause (stmt); |
8d08fdba | 10212 | |
7c34ced1 RH |
10213 | if (ELSE_CLAUSE (t)) |
10214 | { | |
10215 | begin_else_clause (stmt); | |
015c2c66 | 10216 | RECUR (ELSE_CLAUSE (t)); |
7c34ced1 RH |
10217 | finish_else_clause (stmt); |
10218 | } | |
10219 | ||
10220 | finish_if_stmt (stmt); | |
5566b478 | 10221 | break; |
8d08fdba | 10222 | |
5882f0f3 | 10223 | case BIND_EXPR: |
7c34ced1 RH |
10224 | if (BIND_EXPR_BODY_BLOCK (t)) |
10225 | stmt = begin_function_body (); | |
10226 | else | |
10227 | stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t) | |
10228 | ? BCS_TRY_BLOCK : 0); | |
ade3dc07 | 10229 | |
015c2c66 | 10230 | RECUR (BIND_EXPR_BODY (t)); |
ade3dc07 | 10231 | |
7c34ced1 RH |
10232 | if (BIND_EXPR_BODY_BLOCK (t)) |
10233 | finish_function_body (stmt); | |
10234 | else | |
10235 | finish_compound_stmt (stmt); | |
5566b478 | 10236 | break; |
8d08fdba | 10237 | |
5566b478 | 10238 | case BREAK_STMT: |
ad321293 | 10239 | finish_break_stmt (); |
5566b478 | 10240 | break; |
8d08fdba | 10241 | |
6467930b | 10242 | case CONTINUE_STMT: |
ad321293 | 10243 | finish_continue_stmt (); |
6467930b MS |
10244 | break; |
10245 | ||
5566b478 | 10246 | case SWITCH_STMT: |
7c34ced1 | 10247 | stmt = begin_switch_stmt (); |
015c2c66 | 10248 | tmp = RECUR (SWITCH_STMT_COND (t)); |
7c34ced1 | 10249 | finish_switch_cond (tmp, stmt); |
015c2c66 | 10250 | RECUR (SWITCH_STMT_BODY (t)); |
7c34ced1 | 10251 | finish_switch_stmt (stmt); |
5566b478 MS |
10252 | break; |
10253 | ||
8c161995 | 10254 | case CASE_LABEL_EXPR: |
015c2c66 MM |
10255 | finish_case_label (RECUR (CASE_LOW (t)), |
10256 | RECUR (CASE_HIGH (t))); | |
5566b478 MS |
10257 | break; |
10258 | ||
9e14e18f | 10259 | case LABEL_EXPR: |
9e14e18f | 10260 | finish_label_stmt (DECL_NAME (LABEL_EXPR_LABEL (t))); |
5566b478 MS |
10261 | break; |
10262 | ||
9e14e18f | 10263 | case GOTO_EXPR: |
fd10dd09 JM |
10264 | tmp = GOTO_DESTINATION (t); |
10265 | if (TREE_CODE (tmp) != LABEL_DECL) | |
aa09da44 MM |
10266 | /* Computed goto's must be tsubst'd into. On the other hand, |
10267 | non-computed gotos must not be; the identifier in question | |
10268 | will have no binding. */ | |
015c2c66 | 10269 | tmp = RECUR (tmp); |
3fa56191 | 10270 | else |
fd10dd09 JM |
10271 | tmp = DECL_NAME (tmp); |
10272 | finish_goto_stmt (tmp); | |
ad321293 MM |
10273 | break; |
10274 | ||
e130a54b | 10275 | case ASM_EXPR: |
c87978aa | 10276 | tmp = finish_asm_stmt |
6de9cd9a | 10277 | (ASM_VOLATILE_P (t), |
015c2c66 | 10278 | RECUR (ASM_STRING (t)), |
f74dcfb7 JJ |
10279 | tsubst_copy_asm_operands (ASM_OUTPUTS (t), args, complain, in_decl), |
10280 | tsubst_copy_asm_operands (ASM_INPUTS (t), args, complain, in_decl), | |
10281 | tsubst_copy_asm_operands (ASM_CLOBBERS (t), args, complain, in_decl)); | |
131263fa AP |
10282 | { |
10283 | tree asm_expr = tmp; | |
10284 | if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR) | |
10285 | asm_expr = TREE_OPERAND (asm_expr, 0); | |
10286 | ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t); | |
10287 | } | |
5566b478 | 10288 | break; |
faf5394a MS |
10289 | |
10290 | case TRY_BLOCK: | |
f1dedc31 | 10291 | if (CLEANUP_P (t)) |
62409b39 | 10292 | { |
57b52417 | 10293 | stmt = begin_try_block (); |
015c2c66 | 10294 | RECUR (TRY_STMTS (t)); |
62409b39 | 10295 | finish_cleanup_try_block (stmt); |
015c2c66 | 10296 | finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt); |
62409b39 | 10297 | } |
f1dedc31 MM |
10298 | else |
10299 | { | |
eaf6fb90 MM |
10300 | tree compound_stmt = NULL_TREE; |
10301 | ||
62409b39 | 10302 | if (FN_TRY_BLOCK_P (t)) |
eaf6fb90 | 10303 | stmt = begin_function_try_block (&compound_stmt); |
62409b39 MM |
10304 | else |
10305 | stmt = begin_try_block (); | |
10306 | ||
015c2c66 | 10307 | RECUR (TRY_STMTS (t)); |
62409b39 MM |
10308 | |
10309 | if (FN_TRY_BLOCK_P (t)) | |
10310 | finish_function_try_block (stmt); | |
10311 | else | |
10312 | finish_try_block (stmt); | |
10313 | ||
015c2c66 | 10314 | RECUR (TRY_HANDLERS (t)); |
84df082b | 10315 | if (FN_TRY_BLOCK_P (t)) |
eaf6fb90 | 10316 | finish_function_handler_sequence (stmt, compound_stmt); |
84df082b MM |
10317 | else |
10318 | finish_handler_sequence (stmt); | |
f1dedc31 | 10319 | } |
faf5394a | 10320 | break; |
c8094d83 | 10321 | |
faf5394a | 10322 | case HANDLER: |
b35d4555 | 10323 | { |
3a2419a7 | 10324 | tree decl = HANDLER_PARMS (t); |
b35d4555 | 10325 | |
3a2419a7 | 10326 | if (decl) |
b35d4555 | 10327 | { |
b35d4555 | 10328 | decl = tsubst (decl, args, complain, in_decl); |
f8191e64 MM |
10329 | /* Prevent instantiate_decl from trying to instantiate |
10330 | this variable. We've already done all that needs to be | |
10331 | done. */ | |
3a2419a7 VR |
10332 | if (decl != error_mark_node) |
10333 | DECL_TEMPLATE_INSTANTIATED (decl) = 1; | |
b35d4555 | 10334 | } |
3a2419a7 | 10335 | stmt = begin_handler (); |
1a6025b4 | 10336 | finish_handler_parms (decl, stmt); |
015c2c66 | 10337 | RECUR (HANDLER_BODY (t)); |
1a6025b4 | 10338 | finish_handler (stmt); |
b35d4555 | 10339 | } |
faf5394a MS |
10340 | break; |
10341 | ||
b87692e5 | 10342 | case TAG_DEFN: |
fd10dd09 | 10343 | tsubst (TREE_TYPE (t), args, complain, NULL_TREE); |
b87692e5 | 10344 | break; |
55a3debe DG |
10345 | |
10346 | case STATIC_ASSERT: | |
10347 | { | |
10348 | tree condition = | |
10349 | tsubst_expr (STATIC_ASSERT_CONDITION (t), | |
10350 | args, | |
10351 | complain, in_decl, | |
10352 | /*integral_constant_expression_p=*/true); | |
10353 | finish_static_assert (condition, | |
10354 | STATIC_ASSERT_MESSAGE (t), | |
10355 | STATIC_ASSERT_SOURCE_LOCATION (t), | |
10356 | /*member_p=*/false); | |
10357 | } | |
10358 | break; | |
b87692e5 | 10359 | |
1799e5d5 RH |
10360 | case OMP_PARALLEL: |
10361 | tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), | |
10362 | args, complain, in_decl); | |
10363 | stmt = begin_omp_parallel (); | |
015c2c66 | 10364 | RECUR (OMP_PARALLEL_BODY (t)); |
761041be JJ |
10365 | OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt)) |
10366 | = OMP_PARALLEL_COMBINED (t); | |
1799e5d5 RH |
10367 | break; |
10368 | ||
10369 | case OMP_FOR: | |
10370 | { | |
10371 | tree clauses, decl, init, cond, incr, body, pre_body; | |
10372 | ||
10373 | clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), | |
10374 | args, complain, in_decl); | |
10375 | init = OMP_FOR_INIT (t); | |
10376 | gcc_assert (TREE_CODE (init) == MODIFY_EXPR); | |
015c2c66 MM |
10377 | decl = RECUR (TREE_OPERAND (init, 0)); |
10378 | init = RECUR (TREE_OPERAND (init, 1)); | |
10379 | cond = RECUR (OMP_FOR_COND (t)); | |
10380 | incr = RECUR (OMP_FOR_INCR (t)); | |
1799e5d5 RH |
10381 | |
10382 | stmt = begin_omp_structured_block (); | |
10383 | ||
10384 | pre_body = push_stmt_list (); | |
015c2c66 | 10385 | RECUR (OMP_FOR_PRE_BODY (t)); |
1799e5d5 RH |
10386 | pre_body = pop_stmt_list (pre_body); |
10387 | ||
10388 | body = push_stmt_list (); | |
015c2c66 | 10389 | RECUR (OMP_FOR_BODY (t)); |
1799e5d5 RH |
10390 | body = pop_stmt_list (body); |
10391 | ||
10392 | t = finish_omp_for (EXPR_LOCATION (t), decl, init, cond, incr, body, | |
10393 | pre_body); | |
10394 | if (t) | |
10395 | OMP_FOR_CLAUSES (t) = clauses; | |
10396 | ||
10397 | add_stmt (finish_omp_structured_block (stmt)); | |
10398 | } | |
10399 | break; | |
10400 | ||
10401 | case OMP_SECTIONS: | |
10402 | case OMP_SINGLE: | |
10403 | tmp = tsubst_omp_clauses (OMP_CLAUSES (t), args, complain, in_decl); | |
10404 | stmt = push_stmt_list (); | |
015c2c66 | 10405 | RECUR (OMP_BODY (t)); |
1799e5d5 RH |
10406 | stmt = pop_stmt_list (stmt); |
10407 | ||
10408 | t = copy_node (t); | |
10409 | OMP_BODY (t) = stmt; | |
10410 | OMP_CLAUSES (t) = tmp; | |
10411 | add_stmt (t); | |
10412 | break; | |
10413 | ||
10414 | case OMP_SECTION: | |
10415 | case OMP_CRITICAL: | |
10416 | case OMP_MASTER: | |
10417 | case OMP_ORDERED: | |
10418 | stmt = push_stmt_list (); | |
015c2c66 | 10419 | RECUR (OMP_BODY (t)); |
1799e5d5 RH |
10420 | stmt = pop_stmt_list (stmt); |
10421 | ||
10422 | t = copy_node (t); | |
10423 | OMP_BODY (t) = stmt; | |
10424 | add_stmt (t); | |
10425 | break; | |
10426 | ||
10427 | case OMP_ATOMIC: | |
239371f9 JJ |
10428 | gcc_assert (OMP_ATOMIC_DEPENDENT_P (t)); |
10429 | { | |
10430 | tree op1 = TREE_OPERAND (t, 1); | |
10431 | tree lhs = RECUR (TREE_OPERAND (op1, 0)); | |
10432 | tree rhs = RECUR (TREE_OPERAND (op1, 1)); | |
10433 | finish_omp_atomic (TREE_CODE (op1), lhs, rhs); | |
10434 | } | |
1799e5d5 RH |
10435 | break; |
10436 | ||
5d80a306 DG |
10437 | case EXPR_PACK_EXPANSION: |
10438 | error ("invalid use of pack expansion expression"); | |
10439 | return error_mark_node; | |
10440 | ||
10441 | case NONTYPE_ARGUMENT_PACK: | |
10442 | error ("use %<...%> to expand argument pack"); | |
10443 | return error_mark_node; | |
10444 | ||
5566b478 | 10445 | default: |
315fb5db | 10446 | gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t))); |
c8094d83 | 10447 | |
315fb5db | 10448 | return tsubst_copy_and_build (t, args, complain, in_decl, |
015c2c66 MM |
10449 | /*function_p=*/false, |
10450 | integral_constant_expression_p); | |
5566b478 | 10451 | } |
fd10dd09 | 10452 | |
325c3691 | 10453 | return NULL_TREE; |
015c2c66 | 10454 | #undef RECUR |
8d08fdba MS |
10455 | } |
10456 | ||
b3445994 MM |
10457 | /* T is a postfix-expression that is not being used in a function |
10458 | call. Return the substituted version of T. */ | |
10459 | ||
10460 | static tree | |
c8094d83 | 10461 | tsubst_non_call_postfix_expression (tree t, tree args, |
b3445994 MM |
10462 | tsubst_flags_t complain, |
10463 | tree in_decl) | |
10464 | { | |
10465 | if (TREE_CODE (t) == SCOPE_REF) | |
10466 | t = tsubst_qualified_id (t, args, complain, in_decl, | |
10467 | /*done=*/false, /*address_p=*/false); | |
10468 | else | |
10469 | t = tsubst_copy_and_build (t, args, complain, in_decl, | |
015c2c66 MM |
10470 | /*function_p=*/false, |
10471 | /*integral_constant_expression_p=*/false); | |
b3445994 MM |
10472 | |
10473 | return t; | |
10474 | } | |
10475 | ||
cc23546e | 10476 | /* Like tsubst but deals with expressions and performs semantic |
b3445994 | 10477 | analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */ |
cc23546e | 10478 | |
ee76b931 | 10479 | tree |
c8094d83 | 10480 | tsubst_copy_and_build (tree t, |
0cbd7506 MS |
10481 | tree args, |
10482 | tsubst_flags_t complain, | |
10483 | tree in_decl, | |
015c2c66 MM |
10484 | bool function_p, |
10485 | bool integral_constant_expression_p) | |
cc23546e | 10486 | { |
015c2c66 MM |
10487 | #define RECUR(NODE) \ |
10488 | tsubst_copy_and_build (NODE, args, complain, in_decl, \ | |
10489 | /*function_p=*/false, \ | |
10490 | integral_constant_expression_p) | |
b3445994 | 10491 | |
ee76b931 MM |
10492 | tree op1; |
10493 | ||
cc23546e JO |
10494 | if (t == NULL_TREE || t == error_mark_node) |
10495 | return t; | |
10496 | ||
10497 | switch (TREE_CODE (t)) | |
10498 | { | |
399dedb9 NS |
10499 | case USING_DECL: |
10500 | t = DECL_NAME (t); | |
852dcbdd | 10501 | /* Fall through. */ |
b3445994 | 10502 | case IDENTIFIER_NODE: |
cc23546e | 10503 | { |
b3445994 | 10504 | tree decl; |
b3445994 | 10505 | cp_id_kind idk; |
67c03833 | 10506 | bool non_integral_constant_expression_p; |
b3445994 MM |
10507 | const char *error_msg; |
10508 | ||
b3445994 | 10509 | if (IDENTIFIER_TYPENAME_P (t)) |
cc23546e | 10510 | { |
b3445994 MM |
10511 | tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl); |
10512 | t = mangle_conv_op_name_for_type (new_type); | |
cc23546e | 10513 | } |
b3445994 MM |
10514 | |
10515 | /* Look up the name. */ | |
10e6657a | 10516 | decl = lookup_name (t); |
b3445994 MM |
10517 | |
10518 | /* By convention, expressions use ERROR_MARK_NODE to indicate | |
10519 | failure, not NULL_TREE. */ | |
10520 | if (decl == NULL_TREE) | |
10521 | decl = error_mark_node; | |
10522 | ||
10b1d5e7 | 10523 | decl = finish_id_expression (t, decl, NULL_TREE, |
b3445994 | 10524 | &idk, |
015c2c66 | 10525 | integral_constant_expression_p, |
67c03833 JM |
10526 | /*allow_non_integral_constant_expression_p=*/false, |
10527 | &non_integral_constant_expression_p, | |
02ed62dd MM |
10528 | /*template_p=*/false, |
10529 | /*done=*/true, | |
10530 | /*address_p=*/false, | |
10531 | /*template_arg_p=*/false, | |
b3445994 MM |
10532 | &error_msg); |
10533 | if (error_msg) | |
10534 | error (error_msg); | |
10535 | if (!function_p && TREE_CODE (decl) == IDENTIFIER_NODE) | |
10536 | decl = unqualified_name_lookup_error (decl); | |
10537 | return decl; | |
cc23546e JO |
10538 | } |
10539 | ||
10540 | case TEMPLATE_ID_EXPR: | |
10541 | { | |
10542 | tree object; | |
b3445994 | 10543 | tree template = RECUR (TREE_OPERAND (t, 0)); |
bf12d54d NS |
10544 | tree targs = TREE_OPERAND (t, 1); |
10545 | ||
10546 | if (targs) | |
10547 | targs = tsubst_template_args (targs, args, complain, in_decl); | |
c8094d83 | 10548 | |
cc23546e JO |
10549 | if (TREE_CODE (template) == COMPONENT_REF) |
10550 | { | |
10551 | object = TREE_OPERAND (template, 0); | |
10552 | template = TREE_OPERAND (template, 1); | |
10553 | } | |
10554 | else | |
10555 | object = NULL_TREE; | |
bd83b409 | 10556 | template = lookup_template_function (template, targs); |
c8094d83 | 10557 | |
cc23546e | 10558 | if (object) |
c8094d83 | 10559 | return build3 (COMPONENT_REF, TREE_TYPE (template), |
f293ce4b | 10560 | object, template, NULL_TREE); |
cc23546e | 10561 | else |
eff3a276 | 10562 | return baselink_for_fns (template); |
cc23546e JO |
10563 | } |
10564 | ||
10565 | case INDIRECT_REF: | |
db24eb1f NS |
10566 | { |
10567 | tree r = RECUR (TREE_OPERAND (t, 0)); | |
10568 | ||
10569 | if (REFERENCE_REF_P (t)) | |
10570 | { | |
e8c66fe0 NS |
10571 | /* A type conversion to reference type will be enclosed in |
10572 | such an indirect ref, but the substitution of the cast | |
10573 | will have also added such an indirect ref. */ | |
10574 | if (TREE_CODE (TREE_TYPE (r)) == REFERENCE_TYPE) | |
10575 | r = convert_from_reference (r); | |
db24eb1f NS |
10576 | } |
10577 | else | |
10578 | r = build_x_indirect_ref (r, "unary *"); | |
10579 | return r; | |
10580 | } | |
cc23546e | 10581 | |
0da99d4e GB |
10582 | case NOP_EXPR: |
10583 | return build_nop | |
10584 | (tsubst (TREE_TYPE (t), args, complain, in_decl), | |
10585 | RECUR (TREE_OPERAND (t, 0))); | |
10586 | ||
cc23546e | 10587 | case CAST_EXPR: |
cc23546e | 10588 | case REINTERPRET_CAST_EXPR: |
cc23546e | 10589 | case CONST_CAST_EXPR: |
cc23546e | 10590 | case DYNAMIC_CAST_EXPR: |
cc23546e | 10591 | case STATIC_CAST_EXPR: |
015c2c66 MM |
10592 | { |
10593 | tree type; | |
10594 | tree op; | |
10595 | ||
10596 | type = tsubst (TREE_TYPE (t), args, complain, in_decl); | |
10597 | if (integral_constant_expression_p | |
10598 | && !cast_valid_in_integral_constant_expression_p (type)) | |
10599 | { | |
10600 | error ("a cast to a type other than an integral or " | |
10601 | "enumeration type cannot appear in a constant-expression"); | |
10602 | return error_mark_node; | |
10603 | } | |
10604 | ||
10605 | op = RECUR (TREE_OPERAND (t, 0)); | |
10606 | ||
10607 | switch (TREE_CODE (t)) | |
10608 | { | |
10609 | case CAST_EXPR: | |
10610 | return build_functional_cast (type, op); | |
10611 | case REINTERPRET_CAST_EXPR: | |
10612 | return build_reinterpret_cast (type, op); | |
10613 | case CONST_CAST_EXPR: | |
10614 | return build_const_cast (type, op); | |
10615 | case DYNAMIC_CAST_EXPR: | |
10616 | return build_dynamic_cast (type, op); | |
10617 | case STATIC_CAST_EXPR: | |
10618 | return build_static_cast (type, op); | |
10619 | default: | |
10620 | gcc_unreachable (); | |
10621 | } | |
10622 | } | |
cc23546e | 10623 | |
cc23546e JO |
10624 | case POSTDECREMENT_EXPR: |
10625 | case POSTINCREMENT_EXPR: | |
b3445994 MM |
10626 | op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0), |
10627 | args, complain, in_decl); | |
ee76b931 MM |
10628 | return build_x_unary_op (TREE_CODE (t), op1); |
10629 | ||
10630 | case PREDECREMENT_EXPR: | |
10631 | case PREINCREMENT_EXPR: | |
cc23546e JO |
10632 | case NEGATE_EXPR: |
10633 | case BIT_NOT_EXPR: | |
cc23546e | 10634 | case ABS_EXPR: |
d17811fd | 10635 | case TRUTH_NOT_EXPR: |
392e3d51 | 10636 | case UNARY_PLUS_EXPR: /* Unary + */ |
d17811fd MM |
10637 | case REALPART_EXPR: |
10638 | case IMAGPART_EXPR: | |
b3445994 | 10639 | return build_x_unary_op (TREE_CODE (t), RECUR (TREE_OPERAND (t, 0))); |
cc23546e | 10640 | |
cc23546e | 10641 | case ADDR_EXPR: |
ee76b931 MM |
10642 | op1 = TREE_OPERAND (t, 0); |
10643 | if (TREE_CODE (op1) == SCOPE_REF) | |
c8094d83 | 10644 | op1 = tsubst_qualified_id (op1, args, complain, in_decl, |
ee76b931 MM |
10645 | /*done=*/true, /*address_p=*/true); |
10646 | else | |
c8094d83 | 10647 | op1 = tsubst_non_call_postfix_expression (op1, args, complain, |
b3445994 | 10648 | in_decl); |
fc2b8477 MM |
10649 | if (TREE_CODE (op1) == LABEL_DECL) |
10650 | return finish_label_address_expr (DECL_NAME (op1)); | |
ee76b931 MM |
10651 | return build_x_unary_op (ADDR_EXPR, op1); |
10652 | ||
cc23546e JO |
10653 | case PLUS_EXPR: |
10654 | case MINUS_EXPR: | |
10655 | case MULT_EXPR: | |
10656 | case TRUNC_DIV_EXPR: | |
10657 | case CEIL_DIV_EXPR: | |
10658 | case FLOOR_DIV_EXPR: | |
10659 | case ROUND_DIV_EXPR: | |
10660 | case EXACT_DIV_EXPR: | |
10661 | case BIT_AND_EXPR: | |
cc23546e JO |
10662 | case BIT_IOR_EXPR: |
10663 | case BIT_XOR_EXPR: | |
10664 | case TRUNC_MOD_EXPR: | |
10665 | case FLOOR_MOD_EXPR: | |
10666 | case TRUTH_ANDIF_EXPR: | |
10667 | case TRUTH_ORIF_EXPR: | |
10668 | case TRUTH_AND_EXPR: | |
10669 | case TRUTH_OR_EXPR: | |
10670 | case RSHIFT_EXPR: | |
10671 | case LSHIFT_EXPR: | |
10672 | case RROTATE_EXPR: | |
10673 | case LROTATE_EXPR: | |
10674 | case EQ_EXPR: | |
10675 | case NE_EXPR: | |
10676 | case MAX_EXPR: | |
10677 | case MIN_EXPR: | |
10678 | case LE_EXPR: | |
10679 | case GE_EXPR: | |
10680 | case LT_EXPR: | |
10681 | case GT_EXPR: | |
10682 | case MEMBER_REF: | |
b3445994 | 10683 | case DOTSTAR_EXPR: |
cc23546e | 10684 | return build_x_binary_op |
c8094d83 | 10685 | (TREE_CODE (t), |
b3445994 | 10686 | RECUR (TREE_OPERAND (t, 0)), |
2a67bec2 ILT |
10687 | (TREE_NO_WARNING (TREE_OPERAND (t, 0)) |
10688 | ? ERROR_MARK | |
10689 | : TREE_CODE (TREE_OPERAND (t, 0))), | |
ec835fb2 | 10690 | RECUR (TREE_OPERAND (t, 1)), |
2a67bec2 ILT |
10691 | (TREE_NO_WARNING (TREE_OPERAND (t, 1)) |
10692 | ? ERROR_MARK | |
10693 | : TREE_CODE (TREE_OPERAND (t, 1))), | |
ec835fb2 | 10694 | /*overloaded_p=*/NULL); |
cc23546e JO |
10695 | |
10696 | case SCOPE_REF: | |
ee76b931 MM |
10697 | return tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true, |
10698 | /*address_p=*/false); | |
cc23546e | 10699 | case ARRAY_REF: |
b3445994 MM |
10700 | op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0), |
10701 | args, complain, in_decl); | |
2a67bec2 ILT |
10702 | return build_x_binary_op (ARRAY_REF, op1, |
10703 | (TREE_NO_WARNING (TREE_OPERAND (t, 0)) | |
10704 | ? ERROR_MARK | |
10705 | : TREE_CODE (TREE_OPERAND (t, 0))), | |
10706 | RECUR (TREE_OPERAND (t, 1)), | |
10707 | (TREE_NO_WARNING (TREE_OPERAND (t, 1)) | |
10708 | ? ERROR_MARK | |
10709 | : TREE_CODE (TREE_OPERAND (t, 1))), | |
d8987adb | 10710 | /*overloaded_p=*/NULL); |
c8094d83 | 10711 | |
cc23546e | 10712 | case SIZEOF_EXPR: |
5d80a306 DG |
10713 | if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))) |
10714 | { | |
10715 | /* We only want to compute the number of arguments. */ | |
10716 | tree expanded = tsubst_pack_expansion (TREE_OPERAND (t, 0), args, | |
10717 | complain, in_decl); | |
4745e4eb JJ |
10718 | if (expanded == error_mark_node) |
10719 | return error_mark_node; | |
5d80a306 DG |
10720 | return build_int_cst (size_type_node, TREE_VEC_LENGTH (expanded)); |
10721 | } | |
10722 | /* Fall through */ | |
10723 | ||
cc23546e | 10724 | case ALIGNOF_EXPR: |
d17811fd MM |
10725 | op1 = TREE_OPERAND (t, 0); |
10726 | if (!args) | |
10727 | { | |
10728 | /* When there are no ARGS, we are trying to evaluate a | |
10729 | non-dependent expression from the parser. Trying to do | |
10730 | the substitutions may not work. */ | |
10731 | if (!TYPE_P (op1)) | |
10732 | op1 = TREE_TYPE (op1); | |
10733 | } | |
10734 | else | |
10735 | { | |
10736 | ++skip_evaluation; | |
015c2c66 MM |
10737 | op1 = tsubst_copy_and_build (op1, args, complain, in_decl, |
10738 | /*function_p=*/false, | |
10739 | /*integral_constant_expression_p=*/false); | |
d17811fd MM |
10740 | --skip_evaluation; |
10741 | } | |
7a18b933 NS |
10742 | if (TYPE_P (op1)) |
10743 | return cxx_sizeof_or_alignof_type (op1, TREE_CODE (t), true); | |
d17811fd | 10744 | else |
7a18b933 | 10745 | return cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t)); |
cc23546e JO |
10746 | |
10747 | case MODOP_EXPR: | |
e4c2c34b JM |
10748 | { |
10749 | tree r = build_x_modify_expr | |
10750 | (RECUR (TREE_OPERAND (t, 0)), | |
10751 | TREE_CODE (TREE_OPERAND (t, 1)), | |
10752 | RECUR (TREE_OPERAND (t, 2))); | |
bcf9a914 JM |
10753 | /* TREE_NO_WARNING must be set if either the expression was |
10754 | parenthesized or it uses an operator such as >>= rather | |
10755 | than plain assignment. In the former case, it was already | |
10756 | set and must be copied. In the latter case, | |
10757 | build_x_modify_expr sets it and it must not be reset | |
10758 | here. */ | |
10759 | if (TREE_NO_WARNING (t)) | |
10760 | TREE_NO_WARNING (r) = TREE_NO_WARNING (t); | |
e4c2c34b JM |
10761 | return r; |
10762 | } | |
cc23546e JO |
10763 | |
10764 | case ARROW_EXPR: | |
b3445994 MM |
10765 | op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0), |
10766 | args, complain, in_decl); | |
ee76b931 MM |
10767 | /* Remember that there was a reference to this entity. */ |
10768 | if (DECL_P (op1)) | |
10769 | mark_used (op1); | |
10770 | return build_x_arrow (op1); | |
cc23546e JO |
10771 | |
10772 | case NEW_EXPR: | |
268127ce DG |
10773 | { |
10774 | tree init = RECUR (TREE_OPERAND (t, 3)); | |
10775 | ||
10776 | if (TREE_OPERAND (t, 3) && !init) | |
10777 | /* If there was an initializer in the the original tree, but | |
10778 | it instantiated to an empty list, then we should pass on | |
10779 | VOID_ZERO_NODE to tell build_new that it was an empty | |
10780 | initializer () rather than no initializer. This can only | |
10781 | happen when the initializer is a pack expansion whose | |
10782 | parameter packs are of length zero. */ | |
10783 | init = void_zero_node; | |
10784 | ||
10785 | return build_new | |
10786 | (RECUR (TREE_OPERAND (t, 0)), | |
10787 | RECUR (TREE_OPERAND (t, 1)), | |
10788 | RECUR (TREE_OPERAND (t, 2)), | |
10789 | init, | |
10790 | NEW_EXPR_USE_GLOBAL (t)); | |
10791 | } | |
cc23546e JO |
10792 | |
10793 | case DELETE_EXPR: | |
10794 | return delete_sanity | |
b3445994 MM |
10795 | (RECUR (TREE_OPERAND (t, 0)), |
10796 | RECUR (TREE_OPERAND (t, 1)), | |
cc23546e JO |
10797 | DELETE_EXPR_USE_VEC (t), |
10798 | DELETE_EXPR_USE_GLOBAL (t)); | |
10799 | ||
10800 | case COMPOUND_EXPR: | |
b3445994 MM |
10801 | return build_x_compound_expr (RECUR (TREE_OPERAND (t, 0)), |
10802 | RECUR (TREE_OPERAND (t, 1))); | |
cc23546e | 10803 | |
ee76b931 | 10804 | case CALL_EXPR: |
cc23546e | 10805 | { |
ee76b931 MM |
10806 | tree function; |
10807 | tree call_args; | |
ee76b931 | 10808 | bool qualified_p; |
ee935db4 | 10809 | bool koenig_p; |
ee76b931 | 10810 | |
5039610b | 10811 | function = CALL_EXPR_FN (t); |
6d80c4b9 MM |
10812 | /* When we parsed the expression, we determined whether or |
10813 | not Koenig lookup should be performed. */ | |
10814 | koenig_p = KOENIG_LOOKUP_P (t); | |
ee76b931 | 10815 | if (TREE_CODE (function) == SCOPE_REF) |
cc23546e | 10816 | { |
ee76b931 MM |
10817 | qualified_p = true; |
10818 | function = tsubst_qualified_id (function, args, complain, in_decl, | |
c8094d83 | 10819 | /*done=*/false, |
ee76b931 | 10820 | /*address_p=*/false); |
cc23546e | 10821 | } |
ee76b931 | 10822 | else |
cc23546e | 10823 | { |
61e71a9e NS |
10824 | if (TREE_CODE (function) == COMPONENT_REF) |
10825 | { | |
10826 | tree op = TREE_OPERAND (function, 1); | |
10827 | ||
10828 | qualified_p = (TREE_CODE (op) == SCOPE_REF | |
10829 | || (BASELINK_P (op) | |
10830 | && BASELINK_QUALIFIED_P (op))); | |
10831 | } | |
10832 | else | |
10833 | qualified_p = false; | |
3db45ab5 | 10834 | |
c8094d83 | 10835 | function = tsubst_copy_and_build (function, args, complain, |
b3445994 | 10836 | in_decl, |
015c2c66 MM |
10837 | !qualified_p, |
10838 | integral_constant_expression_p); | |
10839 | ||
6d80c4b9 MM |
10840 | if (BASELINK_P (function)) |
10841 | qualified_p = true; | |
cc23546e | 10842 | } |
cc23546e | 10843 | |
5039610b SL |
10844 | /* FIXME: Rewrite this so as not to construct an arglist. */ |
10845 | call_args = RECUR (CALL_EXPR_ARGS (t)); | |
676e33ca MM |
10846 | |
10847 | /* We do not perform argument-dependent lookup if normal | |
10848 | lookup finds a non-function, in accordance with the | |
10849 | expected resolution of DR 218. */ | |
ee935db4 | 10850 | if (koenig_p |
44370687 MM |
10851 | && ((is_overloaded_fn (function) |
10852 | /* If lookup found a member function, the Koenig lookup is | |
10853 | not appropriate, even if an unqualified-name was used | |
10854 | to denote the function. */ | |
10855 | && !DECL_FUNCTION_MEMBER_P (get_first_fn (function))) | |
b3445994 | 10856 | || TREE_CODE (function) == IDENTIFIER_NODE)) |
6d80c4b9 MM |
10857 | function = perform_koenig_lookup (function, call_args); |
10858 | ||
10859 | if (TREE_CODE (function) == IDENTIFIER_NODE) | |
b3445994 | 10860 | { |
6d80c4b9 MM |
10861 | unqualified_name_lookup_error (function); |
10862 | return error_mark_node; | |
b3445994 MM |
10863 | } |
10864 | ||
10865 | /* Remember that there was a reference to this entity. */ | |
10866 | if (DECL_P (function)) | |
10867 | mark_used (function); | |
10868 | ||
d17811fd MM |
10869 | if (TREE_CODE (function) == OFFSET_REF) |
10870 | return build_offset_ref_call_from_tree (function, call_args); | |
10871 | if (TREE_CODE (function) == COMPONENT_REF) | |
9f880ef9 MM |
10872 | { |
10873 | if (!BASELINK_P (TREE_OPERAND (function, 1))) | |
10874 | return finish_call_expr (function, call_args, | |
10875 | /*disallow_virtual=*/false, | |
10876 | /*koenig_p=*/false); | |
10877 | else | |
c8094d83 | 10878 | return (build_new_method_call |
9f880ef9 MM |
10879 | (TREE_OPERAND (function, 0), |
10880 | TREE_OPERAND (function, 1), | |
c8094d83 | 10881 | call_args, NULL_TREE, |
63c9a190 MM |
10882 | qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL, |
10883 | /*fn_p=*/NULL)); | |
9f880ef9 | 10884 | } |
c8094d83 | 10885 | return finish_call_expr (function, call_args, |
6d80c4b9 MM |
10886 | /*disallow_virtual=*/qualified_p, |
10887 | koenig_p); | |
cc23546e JO |
10888 | } |
10889 | ||
10890 | case COND_EXPR: | |
10891 | return build_x_conditional_expr | |
b3445994 MM |
10892 | (RECUR (TREE_OPERAND (t, 0)), |
10893 | RECUR (TREE_OPERAND (t, 1)), | |
10894 | RECUR (TREE_OPERAND (t, 2))); | |
cc23546e JO |
10895 | |
10896 | case PSEUDO_DTOR_EXPR: | |
c8094d83 | 10897 | return finish_pseudo_destructor_expr |
b3445994 MM |
10898 | (RECUR (TREE_OPERAND (t, 0)), |
10899 | RECUR (TREE_OPERAND (t, 1)), | |
10900 | RECUR (TREE_OPERAND (t, 2))); | |
cc23546e JO |
10901 | |
10902 | case TREE_LIST: | |
10903 | { | |
10904 | tree purpose, value, chain; | |
10905 | ||
10906 | if (t == void_list_node) | |
10907 | return t; | |
10908 | ||
5d80a306 DG |
10909 | if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t))) |
10910 | || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t)))) | |
10911 | { | |
10912 | /* We have pack expansions, so expand those and | |
10913 | create a new list out of it. */ | |
10914 | tree purposevec = NULL_TREE; | |
10915 | tree valuevec = NULL_TREE; | |
10916 | tree chain; | |
10917 | int i, len = -1; | |
10918 | ||
10919 | /* Expand the argument expressions. */ | |
10920 | if (TREE_PURPOSE (t)) | |
10921 | purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args, | |
10922 | complain, in_decl); | |
10923 | if (TREE_VALUE (t)) | |
10924 | valuevec = tsubst_pack_expansion (TREE_VALUE (t), args, | |
10925 | complain, in_decl); | |
10926 | ||
10927 | /* Build the rest of the list. */ | |
10928 | chain = TREE_CHAIN (t); | |
10929 | if (chain && chain != void_type_node) | |
10930 | chain = RECUR (chain); | |
10931 | ||
10932 | /* Determine the number of arguments. */ | |
10933 | if (purposevec && TREE_CODE (purposevec) == TREE_VEC) | |
10934 | { | |
10935 | len = TREE_VEC_LENGTH (purposevec); | |
10936 | gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec)); | |
10937 | } | |
10938 | else if (TREE_CODE (valuevec) == TREE_VEC) | |
10939 | len = TREE_VEC_LENGTH (valuevec); | |
10940 | else | |
10941 | { | |
10942 | /* Since we only performed a partial substitution into | |
10943 | the argument pack, we only return a single list | |
10944 | node. */ | |
10945 | if (purposevec == TREE_PURPOSE (t) | |
10946 | && valuevec == TREE_VALUE (t) | |
10947 | && chain == TREE_CHAIN (t)) | |
10948 | return t; | |
10949 | ||
10950 | return tree_cons (purposevec, valuevec, chain); | |
10951 | } | |
10952 | ||
10953 | /* Convert the argument vectors into a TREE_LIST */ | |
10954 | i = len; | |
10955 | while (i > 0) | |
10956 | { | |
10957 | /* Grab the Ith values. */ | |
10958 | i--; | |
10959 | purpose = purposevec ? TREE_VEC_ELT (purposevec, i) | |
10960 | : NULL_TREE; | |
10961 | value | |
10962 | = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i)) | |
10963 | : NULL_TREE; | |
10964 | ||
10965 | /* Build the list (backwards). */ | |
10966 | chain = tree_cons (purpose, value, chain); | |
10967 | } | |
10968 | ||
10969 | return chain; | |
10970 | } | |
10971 | ||
cc23546e JO |
10972 | purpose = TREE_PURPOSE (t); |
10973 | if (purpose) | |
b3445994 | 10974 | purpose = RECUR (purpose); |
cc23546e JO |
10975 | value = TREE_VALUE (t); |
10976 | if (value) | |
b3445994 | 10977 | value = RECUR (value); |
cc23546e JO |
10978 | chain = TREE_CHAIN (t); |
10979 | if (chain && chain != void_type_node) | |
b3445994 | 10980 | chain = RECUR (chain); |
cc23546e JO |
10981 | if (purpose == TREE_PURPOSE (t) |
10982 | && value == TREE_VALUE (t) | |
10983 | && chain == TREE_CHAIN (t)) | |
10984 | return t; | |
10985 | return tree_cons (purpose, value, chain); | |
10986 | } | |
10987 | ||
10988 | case COMPONENT_REF: | |
10989 | { | |
ee76b931 | 10990 | tree object; |
3897c0aa | 10991 | tree object_type; |
ee76b931 MM |
10992 | tree member; |
10993 | ||
b3445994 MM |
10994 | object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0), |
10995 | args, complain, in_decl); | |
ee76b931 MM |
10996 | /* Remember that there was a reference to this entity. */ |
10997 | if (DECL_P (object)) | |
10998 | mark_used (object); | |
3897c0aa | 10999 | object_type = TREE_TYPE (object); |
ee76b931 MM |
11000 | |
11001 | member = TREE_OPERAND (t, 1); | |
11002 | if (BASELINK_P (member)) | |
c8094d83 | 11003 | member = tsubst_baselink (member, |
ee76b931 MM |
11004 | non_reference (TREE_TYPE (object)), |
11005 | args, complain, in_decl); | |
11006 | else | |
11007 | member = tsubst_copy (member, args, complain, in_decl); | |
bad1f462 KL |
11008 | if (member == error_mark_node) |
11009 | return error_mark_node; | |
3897c0aa MM |
11010 | |
11011 | if (object_type && !CLASS_TYPE_P (object_type)) | |
cc23546e | 11012 | { |
3d9b2eb6 JJ |
11013 | if (SCALAR_TYPE_P (object_type)) |
11014 | { | |
11015 | tree s = NULL_TREE; | |
11016 | tree dtor = member; | |
11017 | ||
11018 | if (TREE_CODE (dtor) == SCOPE_REF) | |
11019 | { | |
11020 | s = TREE_OPERAND (dtor, 0); | |
11021 | dtor = TREE_OPERAND (dtor, 1); | |
11022 | } | |
11023 | if (TREE_CODE (dtor) == BIT_NOT_EXPR) | |
11024 | { | |
11025 | dtor = TREE_OPERAND (dtor, 0); | |
11026 | if (TYPE_P (dtor)) | |
11027 | return finish_pseudo_destructor_expr (object, s, dtor); | |
11028 | } | |
11029 | } | |
cc23546e JO |
11030 | } |
11031 | else if (TREE_CODE (member) == SCOPE_REF | |
11032 | && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR) | |
11033 | { | |
11034 | tree tmpl; | |
11035 | tree args; | |
c8094d83 | 11036 | |
cc23546e JO |
11037 | /* Lookup the template functions now that we know what the |
11038 | scope is. */ | |
11039 | tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0); | |
11040 | args = TREE_OPERAND (TREE_OPERAND (member, 1), 1); | |
c8094d83 | 11041 | member = lookup_qualified_name (TREE_OPERAND (member, 0), tmpl, |
12483c9f NS |
11042 | /*is_type_p=*/false, |
11043 | /*complain=*/false); | |
cc23546e | 11044 | if (BASELINK_P (member)) |
44370687 | 11045 | { |
c8094d83 | 11046 | BASELINK_FUNCTIONS (member) |
44370687 MM |
11047 | = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member), |
11048 | args); | |
c8094d83 MS |
11049 | member = (adjust_result_of_qualified_name_lookup |
11050 | (member, BINFO_TYPE (BASELINK_BINFO (member)), | |
3897c0aa | 11051 | object_type)); |
44370687 | 11052 | } |
cc23546e JO |
11053 | else |
11054 | { | |
3897c0aa | 11055 | qualified_name_lookup_error (object_type, tmpl, member); |
cc23546e JO |
11056 | return error_mark_node; |
11057 | } | |
11058 | } | |
2436b51f MM |
11059 | else if (TREE_CODE (member) == SCOPE_REF |
11060 | && !CLASS_TYPE_P (TREE_OPERAND (member, 0)) | |
11061 | && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL) | |
11062 | { | |
11063 | if (complain & tf_error) | |
11064 | { | |
11065 | if (TYPE_P (TREE_OPERAND (member, 0))) | |
c8094d83 | 11066 | error ("%qT is not a class or namespace", |
2436b51f MM |
11067 | TREE_OPERAND (member, 0)); |
11068 | else | |
c8094d83 | 11069 | error ("%qD is not a class or namespace", |
2436b51f MM |
11070 | TREE_OPERAND (member, 0)); |
11071 | } | |
11072 | return error_mark_node; | |
11073 | } | |
a3f10e50 NS |
11074 | else if (TREE_CODE (member) == FIELD_DECL) |
11075 | return finish_non_static_data_member (member, object, NULL_TREE); | |
cc23546e | 11076 | |
02ed62dd MM |
11077 | return finish_class_member_access_expr (object, member, |
11078 | /*template_p=*/false); | |
cc23546e JO |
11079 | } |
11080 | ||
11081 | case THROW_EXPR: | |
11082 | return build_throw | |
b3445994 | 11083 | (RECUR (TREE_OPERAND (t, 0))); |
cc23546e JO |
11084 | |
11085 | case CONSTRUCTOR: | |
11086 | { | |
4038c495 GB |
11087 | VEC(constructor_elt,gc) *n; |
11088 | constructor_elt *ce; | |
11089 | unsigned HOST_WIDE_INT idx; | |
cc23546e | 11090 | tree type = tsubst (TREE_TYPE (t), args, complain, in_decl); |
4038c495 | 11091 | bool process_index_p; |
5d80a306 DG |
11092 | int newlen; |
11093 | bool need_copy_p = false; | |
cc23546e | 11094 | |
a97728cf VR |
11095 | if (type == error_mark_node) |
11096 | return error_mark_node; | |
11097 | ||
cc23546e JO |
11098 | /* digest_init will do the wrong thing if we let it. */ |
11099 | if (type && TYPE_PTRMEMFUNC_P (type)) | |
11100 | return t; | |
11101 | ||
4038c495 | 11102 | /* We do not want to process the index of aggregate |
cc23546e JO |
11103 | initializers as they are identifier nodes which will be |
11104 | looked up by digest_init. */ | |
4038c495 | 11105 | process_index_p = !(type && IS_AGGR_TYPE (type)); |
c8094d83 | 11106 | |
4038c495 | 11107 | n = VEC_copy (constructor_elt, gc, CONSTRUCTOR_ELTS (t)); |
5d80a306 | 11108 | newlen = VEC_length (constructor_elt, n); |
4038c495 GB |
11109 | for (idx = 0; VEC_iterate (constructor_elt, n, idx, ce); idx++) |
11110 | { | |
11111 | if (ce->index && process_index_p) | |
11112 | ce->index = RECUR (ce->index); | |
5d80a306 DG |
11113 | |
11114 | if (PACK_EXPANSION_P (ce->value)) | |
11115 | { | |
11116 | /* Substitute into the pack expansion. */ | |
11117 | ce->value = tsubst_pack_expansion (ce->value, args, complain, | |
11118 | in_decl); | |
11119 | ||
4745e4eb JJ |
11120 | if (ce->value == error_mark_node) |
11121 | ; | |
11122 | else if (TREE_VEC_LENGTH (ce->value) == 1) | |
5d80a306 DG |
11123 | /* Just move the argument into place. */ |
11124 | ce->value = TREE_VEC_ELT (ce->value, 0); | |
11125 | else | |
11126 | { | |
11127 | /* Update the length of the final CONSTRUCTOR | |
11128 | arguments vector, and note that we will need to | |
11129 | copy.*/ | |
11130 | newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1; | |
11131 | need_copy_p = true; | |
11132 | } | |
11133 | } | |
11134 | else | |
11135 | ce->value = RECUR (ce->value); | |
cc23546e | 11136 | } |
c8094d83 | 11137 | |
5d80a306 DG |
11138 | if (need_copy_p) |
11139 | { | |
11140 | VEC(constructor_elt,gc) *old_n = n; | |
11141 | ||
11142 | n = VEC_alloc (constructor_elt, gc, newlen); | |
11143 | for (idx = 0; VEC_iterate (constructor_elt, old_n, idx, ce); | |
11144 | idx++) | |
11145 | { | |
11146 | if (TREE_CODE (ce->value) == TREE_VEC) | |
11147 | { | |
11148 | int i, len = TREE_VEC_LENGTH (ce->value); | |
11149 | for (i = 0; i < len; ++i) | |
11150 | CONSTRUCTOR_APPEND_ELT (n, 0, | |
11151 | TREE_VEC_ELT (ce->value, i)); | |
11152 | } | |
11153 | else | |
11154 | CONSTRUCTOR_APPEND_ELT (n, 0, ce->value); | |
11155 | } | |
11156 | } | |
11157 | ||
05b22df9 MM |
11158 | if (TREE_HAS_CONSTRUCTOR (t)) |
11159 | return finish_compound_literal (type, n); | |
cc23546e | 11160 | |
05b22df9 | 11161 | return build_constructor (NULL_TREE, n); |
cc23546e JO |
11162 | } |
11163 | ||
11164 | case TYPEID_EXPR: | |
11165 | { | |
b3445994 | 11166 | tree operand_0 = RECUR (TREE_OPERAND (t, 0)); |
cc23546e JO |
11167 | if (TYPE_P (operand_0)) |
11168 | return get_typeid (operand_0); | |
11169 | return build_typeid (operand_0); | |
11170 | } | |
11171 | ||
cc23546e | 11172 | case VAR_DECL: |
db24eb1f NS |
11173 | if (!args) |
11174 | return t; | |
11175 | /* Fall through */ | |
c8094d83 | 11176 | |
db24eb1f NS |
11177 | case PARM_DECL: |
11178 | { | |
11179 | tree r = tsubst_copy (t, args, complain, in_decl); | |
c8094d83 | 11180 | |
db24eb1f NS |
11181 | if (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE) |
11182 | /* If the original type was a reference, we'll be wrapped in | |
11183 | the appropriate INDIRECT_REF. */ | |
11184 | r = convert_from_reference (r); | |
11185 | return r; | |
11186 | } | |
cc23546e JO |
11187 | |
11188 | case VA_ARG_EXPR: | |
b3445994 | 11189 | return build_x_va_arg (RECUR (TREE_OPERAND (t, 0)), |
c8094d83 | 11190 | tsubst_copy (TREE_TYPE (t), args, complain, |
b3445994 | 11191 | in_decl)); |
cc23546e | 11192 | |
4bceb077 | 11193 | case OFFSETOF_EXPR: |
c291f8b1 | 11194 | return finish_offsetof (RECUR (TREE_OPERAND (t, 0))); |
4bceb077 | 11195 | |
cb68ec50 PC |
11196 | case TRAIT_EXPR: |
11197 | { | |
11198 | tree type1 = tsubst_copy (TRAIT_EXPR_TYPE1 (t), args, | |
11199 | complain, in_decl); | |
11200 | ||
11201 | tree type2 = TRAIT_EXPR_TYPE2 (t); | |
11202 | if (type2) | |
11203 | type2 = tsubst_copy (type2, args, complain, in_decl); | |
11204 | ||
11205 | return finish_trait_expr (TRAIT_EXPR_KIND (t), type1, type2); | |
11206 | } | |
11207 | ||
0fe0caa6 RH |
11208 | case STMT_EXPR: |
11209 | { | |
11210 | tree old_stmt_expr = cur_stmt_expr; | |
11211 | tree stmt_expr = begin_stmt_expr (); | |
11212 | ||
11213 | cur_stmt_expr = stmt_expr; | |
015c2c66 MM |
11214 | tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl, |
11215 | integral_constant_expression_p); | |
0fe0caa6 RH |
11216 | stmt_expr = finish_stmt_expr (stmt_expr, false); |
11217 | cur_stmt_expr = old_stmt_expr; | |
11218 | ||
11219 | return stmt_expr; | |
11220 | } | |
11221 | ||
a5201a91 MM |
11222 | case CONST_DECL: |
11223 | t = tsubst_copy (t, args, complain, in_decl); | |
11224 | /* As in finish_id_expression, we resolve enumeration constants | |
11225 | to their underlying values. */ | |
11226 | if (TREE_CODE (t) == CONST_DECL) | |
6193b8b7 DJ |
11227 | { |
11228 | used_types_insert (TREE_TYPE (t)); | |
11229 | return DECL_INITIAL (t); | |
11230 | } | |
a5201a91 MM |
11231 | return t; |
11232 | ||
cc23546e | 11233 | default: |
e58a9aa1 ZL |
11234 | /* Handle Objective-C++ constructs, if appropriate. */ |
11235 | { | |
11236 | tree subst | |
11237 | = objcp_tsubst_copy_and_build (t, args, complain, | |
11238 | in_decl, /*function_p=*/false); | |
11239 | if (subst) | |
11240 | return subst; | |
11241 | } | |
cc23546e JO |
11242 | return tsubst_copy (t, args, complain, in_decl); |
11243 | } | |
b3445994 MM |
11244 | |
11245 | #undef RECUR | |
cc23546e JO |
11246 | } |
11247 | ||
3e4a3562 NS |
11248 | /* Verify that the instantiated ARGS are valid. For type arguments, |
11249 | make sure that the type's linkage is ok. For non-type arguments, | |
34cd5ae7 | 11250 | make sure they are constants if they are integral or enumerations. |
9bcb9aae | 11251 | Emit an error under control of COMPLAIN, and return TRUE on error. */ |
3e4a3562 NS |
11252 | |
11253 | static bool | |
3a978d72 | 11254 | check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain) |
3e4a3562 NS |
11255 | { |
11256 | int ix, len = DECL_NTPARMS (tmpl); | |
11257 | bool result = false; | |
11258 | ||
11259 | for (ix = 0; ix != len; ix++) | |
11260 | { | |
11261 | tree t = TREE_VEC_ELT (args, ix); | |
c8094d83 | 11262 | |
3e4a3562 NS |
11263 | if (TYPE_P (t)) |
11264 | { | |
11265 | /* [basic.link]: A name with no linkage (notably, the name | |
11266 | of a class or enumeration declared in a local scope) | |
11267 | shall not be used to declare an entity with linkage. | |
11268 | This implies that names with no linkage cannot be used as | |
11269 | template arguments. */ | |
4684cd27 | 11270 | tree nt = no_linkage_check (t, /*relaxed_p=*/false); |
3e4a3562 NS |
11271 | |
11272 | if (nt) | |
11273 | { | |
cb807ba3 | 11274 | /* DR 488 makes use of a type with no linkage cause |
3db45ab5 | 11275 | type deduction to fail. */ |
2010cdcd MM |
11276 | if (complain & tf_error) |
11277 | { | |
11278 | if (TYPE_ANONYMOUS_P (nt)) | |
11279 | error ("%qT is/uses anonymous type", t); | |
11280 | else | |
9df540a8 | 11281 | error ("template argument for %qD uses local type %qT", |
3db45ab5 | 11282 | tmpl, t); |
2010cdcd | 11283 | } |
3e4a3562 NS |
11284 | result = true; |
11285 | } | |
11286 | /* In order to avoid all sorts of complications, we do not | |
11287 | allow variably-modified types as template arguments. */ | |
5377d5ba | 11288 | else if (variably_modified_type_p (t, NULL_TREE)) |
3e4a3562 NS |
11289 | { |
11290 | if (complain & tf_error) | |
0f51ccfc | 11291 | error ("%qT is a variably modified type", t); |
3e4a3562 NS |
11292 | result = true; |
11293 | } | |
11294 | } | |
11295 | /* A non-type argument of integral or enumerated type must be a | |
11296 | constant. */ | |
11297 | else if (TREE_TYPE (t) | |
11298 | && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t)) | |
11299 | && !TREE_CONSTANT (t)) | |
11300 | { | |
11301 | if (complain & tf_error) | |
0f51ccfc | 11302 | error ("integral expression %qE is not constant", t); |
3e4a3562 NS |
11303 | result = true; |
11304 | } | |
11305 | } | |
2010cdcd | 11306 | if (result && (complain & tf_error)) |
0f51ccfc | 11307 | error (" trying to instantiate %qD", tmpl); |
3e4a3562 NS |
11308 | return result; |
11309 | } | |
11310 | ||
6ba89f8e | 11311 | /* Instantiate the indicated variable or function template TMPL with |
36a117a5 MM |
11312 | the template arguments in TARG_PTR. */ |
11313 | ||
5566b478 | 11314 | tree |
3a978d72 | 11315 | instantiate_template (tree tmpl, tree targ_ptr, tsubst_flags_t complain) |
8d08fdba | 11316 | { |
5566b478 | 11317 | tree fndecl; |
36a117a5 MM |
11318 | tree gen_tmpl; |
11319 | tree spec; | |
a95799ec | 11320 | HOST_WIDE_INT saved_processing_template_decl; |
5566b478 | 11321 | |
27fafc8d JM |
11322 | if (tmpl == error_mark_node) |
11323 | return error_mark_node; | |
11324 | ||
50bc768d | 11325 | gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL); |
386b8a85 | 11326 | |
db9b2174 MM |
11327 | /* If this function is a clone, handle it specially. */ |
11328 | if (DECL_CLONED_FUNCTION_P (tmpl)) | |
3ad97789 | 11329 | { |
a30f62e0 | 11330 | tree spec; |
3ad97789 | 11331 | tree clone; |
c8094d83 | 11332 | |
a30f62e0 MM |
11333 | spec = instantiate_template (DECL_CLONED_FUNCTION (tmpl), targ_ptr, |
11334 | complain); | |
11335 | if (spec == error_mark_node) | |
11336 | return error_mark_node; | |
11337 | ||
c6002625 | 11338 | /* Look for the clone. */ |
4684cd27 | 11339 | FOR_EACH_CLONE (clone, spec) |
3ad97789 NS |
11340 | if (DECL_NAME (clone) == DECL_NAME (tmpl)) |
11341 | return clone; | |
11342 | /* We should always have found the clone by now. */ | |
315fb5db | 11343 | gcc_unreachable (); |
3ad97789 NS |
11344 | return NULL_TREE; |
11345 | } | |
c8094d83 | 11346 | |
36a117a5 | 11347 | /* Check to see if we already have this specialization. */ |
c8094d83 | 11348 | spec = retrieve_specialization (tmpl, targ_ptr, |
c7222c02 | 11349 | /*class_specializations_p=*/false); |
36a117a5 MM |
11350 | if (spec != NULL_TREE) |
11351 | return spec; | |
11352 | ||
f9a7ae04 MM |
11353 | gen_tmpl = most_general_template (tmpl); |
11354 | if (tmpl != gen_tmpl) | |
386b8a85 | 11355 | { |
36a117a5 MM |
11356 | /* The TMPL is a partial instantiation. To get a full set of |
11357 | arguments we must add the arguments used to perform the | |
11358 | partial instantiation. */ | |
11359 | targ_ptr = add_outermost_template_args (DECL_TI_ARGS (tmpl), | |
11360 | targ_ptr); | |
36a117a5 MM |
11361 | |
11362 | /* Check to see if we already have this specialization. */ | |
c7222c02 MM |
11363 | spec = retrieve_specialization (gen_tmpl, targ_ptr, |
11364 | /*class_specializations_p=*/false); | |
75650646 MM |
11365 | if (spec != NULL_TREE) |
11366 | return spec; | |
386b8a85 JM |
11367 | } |
11368 | ||
3e4a3562 NS |
11369 | if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr), |
11370 | complain)) | |
11371 | return error_mark_node; | |
c8094d83 | 11372 | |
2b907f5c KL |
11373 | /* We are building a FUNCTION_DECL, during which the access of its |
11374 | parameters and return types have to be checked. However this | |
11375 | FUNCTION_DECL which is the desired context for access checking | |
11376 | is not built yet. We solve this chicken-and-egg problem by | |
11377 | deferring all checks until we have the FUNCTION_DECL. */ | |
11378 | push_deferring_access_checks (dk_deferred); | |
5c74d5b0 | 11379 | |
a95799ec MM |
11380 | /* Although PROCESSING_TEMPLATE_DECL may be true at this point |
11381 | (because, for example, we have encountered a non-dependent | |
a1d08991 MM |
11382 | function call in the body of a template function and must now |
11383 | determine which of several overloaded functions will be called), | |
11384 | within the instantiation itself we are not processing a | |
11385 | template. */ | |
a95799ec MM |
11386 | saved_processing_template_decl = processing_template_decl; |
11387 | processing_template_decl = 0; | |
11388 | /* Substitute template parameters to obtain the specialization. */ | |
17aec3eb | 11389 | fndecl = tsubst (DECL_TEMPLATE_RESULT (gen_tmpl), |
3e4a3562 | 11390 | targ_ptr, complain, gen_tmpl); |
a95799ec | 11391 | processing_template_decl = saved_processing_template_decl; |
6b6b60af MM |
11392 | if (fndecl == error_mark_node) |
11393 | return error_mark_node; | |
5c74d5b0 | 11394 | |
2b907f5c KL |
11395 | /* Now we know the specialization, compute access previously |
11396 | deferred. */ | |
11397 | push_access_scope (fndecl); | |
11398 | perform_deferred_access_checks (); | |
11399 | pop_access_scope (fndecl); | |
11400 | pop_deferring_access_checks (); | |
5c74d5b0 | 11401 | |
36a117a5 MM |
11402 | /* The DECL_TI_TEMPLATE should always be the immediate parent |
11403 | template, not the most general template. */ | |
11404 | DECL_TI_TEMPLATE (fndecl) = tmpl; | |
8d08fdba | 11405 | |
94350948 | 11406 | /* If we've just instantiated the main entry point for a function, |
3ad97789 NS |
11407 | instantiate all the alternate entry points as well. We do this |
11408 | by cloning the instantiation of the main entry point, not by | |
11409 | instantiating the template clones. */ | |
11410 | if (TREE_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (TREE_CHAIN (gen_tmpl))) | |
11411 | clone_function_decl (fndecl, /*update_method_vec_p=*/0); | |
94350948 | 11412 | |
5566b478 | 11413 | return fndecl; |
8d08fdba | 11414 | } |
5566b478 | 11415 | |
4393e105 MM |
11416 | /* The FN is a TEMPLATE_DECL for a function. The ARGS are the |
11417 | arguments that are being used when calling it. TARGS is a vector | |
c8094d83 | 11418 | into which the deduced template arguments are placed. |
8d08fdba MS |
11419 | |
11420 | Return zero for success, 2 for an incomplete match that doesn't resolve | |
11421 | all the types, and 1 for complete failure. An error message will be | |
11422 | printed only for an incomplete match. | |
11423 | ||
e5214479 JM |
11424 | If FN is a conversion operator, or we are trying to produce a specific |
11425 | specialization, RETURN_TYPE is the return type desired. | |
9f54c803 MM |
11426 | |
11427 | The EXPLICIT_TARGS are explicit template arguments provided via a | |
11428 | template-id. | |
6467930b | 11429 | |
830bfa74 MM |
11430 | The parameter STRICT is one of: |
11431 | ||
c8094d83 | 11432 | DEDUCE_CALL: |
830bfa74 MM |
11433 | We are deducing arguments for a function call, as in |
11434 | [temp.deduct.call]. | |
11435 | ||
11436 | DEDUCE_CONV: | |
c8094d83 | 11437 | We are deducing arguments for a conversion function, as in |
830bfa74 MM |
11438 | [temp.deduct.conv]. |
11439 | ||
11440 | DEDUCE_EXACT: | |
62e4a758 NS |
11441 | We are deducing arguments when doing an explicit instantiation |
11442 | as in [temp.explicit], when determining an explicit specialization | |
11443 | as in [temp.expl.spec], or when taking the address of a function | |
a34d3336 | 11444 | template, as in [temp.deduct.funcaddr]. */ |
8d08fdba MS |
11445 | |
11446 | int | |
c8094d83 | 11447 | fn_type_unification (tree fn, |
0cbd7506 MS |
11448 | tree explicit_targs, |
11449 | tree targs, | |
11450 | tree args, | |
11451 | tree return_type, | |
30f86ec3 FJ |
11452 | unification_kind_t strict, |
11453 | int flags) | |
386b8a85 | 11454 | { |
4393e105 MM |
11455 | tree parms; |
11456 | tree fntype; | |
adecb3f4 | 11457 | int result; |
5d80a306 | 11458 | bool incomplete_argument_packs_p = false; |
386b8a85 | 11459 | |
50bc768d | 11460 | gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL); |
2b59fc25 | 11461 | |
4393e105 MM |
11462 | fntype = TREE_TYPE (fn); |
11463 | if (explicit_targs) | |
75650646 | 11464 | { |
4393e105 | 11465 | /* [temp.deduct] |
c8094d83 | 11466 | |
4393e105 MM |
11467 | The specified template arguments must match the template |
11468 | parameters in kind (i.e., type, nontype, template), and there | |
11469 | must not be more arguments than there are parameters; | |
11470 | otherwise type deduction fails. | |
11471 | ||
11472 | Nontype arguments must match the types of the corresponding | |
11473 | nontype template parameters, or must be convertible to the | |
11474 | types of the corresponding nontype parameters as specified in | |
11475 | _temp.arg.nontype_, otherwise type deduction fails. | |
11476 | ||
11477 | All references in the function type of the function template | |
11478 | to the corresponding template parameters are replaced by the | |
11479 | specified template argument values. If a substitution in a | |
11480 | template parameter or in the function type of the function | |
11481 | template results in an invalid type, type deduction fails. */ | |
5d80a306 DG |
11482 | tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn); |
11483 | int i, len = TREE_VEC_LENGTH (tparms); | |
4393e105 | 11484 | tree converted_args; |
5d80a306 | 11485 | bool incomplete = false; |
75650646 | 11486 | |
c8a7ed43 AO |
11487 | if (explicit_targs == error_mark_node) |
11488 | return 1; | |
11489 | ||
4393e105 | 11490 | converted_args |
5d80a306 | 11491 | = (coerce_template_parms (tparms, explicit_targs, NULL_TREE, tf_none, |
e7e93965 MM |
11492 | /*require_all_args=*/false, |
11493 | /*use_default_args=*/false)); | |
4393e105 | 11494 | if (converted_args == error_mark_node) |
75650646 | 11495 | return 1; |
386b8a85 | 11496 | |
ffd49b19 | 11497 | /* Substitute the explicit args into the function type. This is |
0cbd7506 MS |
11498 | necessary so that, for instance, explicitly declared function |
11499 | arguments can match null pointed constants. If we were given | |
11500 | an incomplete set of explicit args, we must not do semantic | |
11501 | processing during substitution as we could create partial | |
11502 | instantiations. */ | |
5d80a306 DG |
11503 | for (i = 0; i < len; i++) |
11504 | { | |
11505 | tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i)); | |
11506 | bool parameter_pack = false; | |
11507 | ||
11508 | /* Dig out the actual parm. */ | |
11509 | if (TREE_CODE (parm) == TYPE_DECL | |
11510 | || TREE_CODE (parm) == TEMPLATE_DECL) | |
11511 | { | |
11512 | parm = TREE_TYPE (parm); | |
11513 | parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm); | |
11514 | } | |
11515 | else if (TREE_CODE (parm) == PARM_DECL) | |
11516 | { | |
11517 | parm = DECL_INITIAL (parm); | |
11518 | parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm); | |
11519 | } | |
11520 | ||
11521 | if (parameter_pack) | |
11522 | { | |
11523 | int level, idx; | |
11524 | tree targ; | |
11525 | template_parm_level_and_index (parm, &level, &idx); | |
11526 | ||
11527 | /* Mark the argument pack as "incomplete". We could | |
11528 | still deduce more arguments during unification. */ | |
11529 | targ = TMPL_ARG (converted_args, level, idx); | |
b1d7b1c0 DG |
11530 | if (targ) |
11531 | { | |
11532 | ARGUMENT_PACK_INCOMPLETE_P(targ) = 1; | |
11533 | ARGUMENT_PACK_EXPLICIT_ARGS (targ) | |
11534 | = ARGUMENT_PACK_ARGS (targ); | |
11535 | } | |
5d80a306 DG |
11536 | |
11537 | /* We have some incomplete argument packs. */ | |
11538 | incomplete_argument_packs_p = true; | |
11539 | } | |
11540 | } | |
11541 | ||
11542 | if (incomplete_argument_packs_p) | |
11543 | /* Any substitution is guaranteed to be incomplete if there | |
11544 | are incomplete argument packs, because we can still deduce | |
11545 | more arguments. */ | |
11546 | incomplete = 1; | |
11547 | else | |
11548 | incomplete = NUM_TMPL_ARGS (explicit_targs) != NUM_TMPL_ARGS (targs); | |
11549 | ||
ffd49b19 | 11550 | processing_template_decl += incomplete; |
c2ea3a40 | 11551 | fntype = tsubst (fntype, converted_args, tf_none, NULL_TREE); |
ffd49b19 | 11552 | processing_template_decl -= incomplete; |
c8094d83 | 11553 | |
4393e105 MM |
11554 | if (fntype == error_mark_node) |
11555 | return 1; | |
050367a3 | 11556 | |
4393e105 | 11557 | /* Place the explicitly specified arguments in TARGS. */ |
ffd49b19 | 11558 | for (i = NUM_TMPL_ARGS (converted_args); i--;) |
4393e105 | 11559 | TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (converted_args, i); |
75650646 | 11560 | } |
c8094d83 | 11561 | |
e5214479 | 11562 | /* Never do unification on the 'this' parameter. */ |
ba139ba8 | 11563 | parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype)); |
c8094d83 | 11564 | |
8d3631f8 NS |
11565 | if (return_type) |
11566 | { | |
8d3631f8 NS |
11567 | parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms); |
11568 | args = tree_cons (NULL_TREE, return_type, args); | |
4393e105 MM |
11569 | } |
11570 | ||
4393e105 MM |
11571 | /* We allow incomplete unification without an error message here |
11572 | because the standard doesn't seem to explicitly prohibit it. Our | |
11573 | callers must be ready to deal with unification failures in any | |
11574 | event. */ | |
c8094d83 | 11575 | result = type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn), |
adecb3f4 | 11576 | targs, parms, args, /*subr=*/0, |
30f86ec3 | 11577 | strict, flags); |
adecb3f4 | 11578 | |
5d80a306 DG |
11579 | if (result == 0 && incomplete_argument_packs_p) |
11580 | { | |
11581 | int i, len = NUM_TMPL_ARGS (targs); | |
11582 | ||
11583 | /* Clear the "incomplete" flags on all argument packs. */ | |
11584 | for (i = 0; i < len; i++) | |
11585 | { | |
11586 | tree arg = TREE_VEC_ELT (targs, i); | |
11587 | if (ARGUMENT_PACK_P (arg)) | |
11588 | { | |
11589 | ARGUMENT_PACK_INCOMPLETE_P (arg) = 0; | |
11590 | ARGUMENT_PACK_EXPLICIT_ARGS (arg) = NULL_TREE; | |
11591 | } | |
11592 | } | |
11593 | } | |
11594 | ||
c8094d83 | 11595 | if (result == 0) |
adecb3f4 | 11596 | /* All is well so far. Now, check: |
c8094d83 MS |
11597 | |
11598 | [temp.deduct] | |
11599 | ||
adecb3f4 MM |
11600 | When all template arguments have been deduced, all uses of |
11601 | template parameters in nondeduced contexts are replaced with | |
11602 | the corresponding deduced argument values. If the | |
11603 | substitution results in an invalid type, as described above, | |
11604 | type deduction fails. */ | |
c2ea3a40 | 11605 | if (tsubst (TREE_TYPE (fn), targs, tf_none, NULL_TREE) |
adecb3f4 MM |
11606 | == error_mark_node) |
11607 | return 1; | |
11608 | ||
11609 | return result; | |
830bfa74 MM |
11610 | } |
11611 | ||
11612 | /* Adjust types before performing type deduction, as described in | |
11613 | [temp.deduct.call] and [temp.deduct.conv]. The rules in these two | |
4c7d0dff MM |
11614 | sections are symmetric. PARM is the type of a function parameter |
11615 | or the return type of the conversion function. ARG is the type of | |
11616 | the argument passed to the call, or the type of the value | |
8af2fec4 RY |
11617 | initialized with the result of the conversion function. |
11618 | ARG_EXPR is the original argument expression, which may be null. */ | |
386b8a85 | 11619 | |
62e4a758 | 11620 | static int |
c8094d83 | 11621 | maybe_adjust_types_for_deduction (unification_kind_t strict, |
0cbd7506 | 11622 | tree* parm, |
8af2fec4 RY |
11623 | tree* arg, |
11624 | tree arg_expr) | |
830bfa74 | 11625 | { |
62e4a758 | 11626 | int result = 0; |
c8094d83 | 11627 | |
830bfa74 MM |
11628 | switch (strict) |
11629 | { | |
11630 | case DEDUCE_CALL: | |
11631 | break; | |
11632 | ||
11633 | case DEDUCE_CONV: | |
11634 | { | |
4c7d0dff MM |
11635 | /* Swap PARM and ARG throughout the remainder of this |
11636 | function; the handling is precisely symmetric since PARM | |
11637 | will initialize ARG rather than vice versa. */ | |
830bfa74 MM |
11638 | tree* temp = parm; |
11639 | parm = arg; | |
11640 | arg = temp; | |
11641 | break; | |
11642 | } | |
11643 | ||
11644 | case DEDUCE_EXACT: | |
11645 | /* There is nothing to do in this case. */ | |
62e4a758 | 11646 | return 0; |
830bfa74 MM |
11647 | |
11648 | default: | |
315fb5db | 11649 | gcc_unreachable (); |
830bfa74 MM |
11650 | } |
11651 | ||
11652 | if (TREE_CODE (*parm) != REFERENCE_TYPE) | |
11653 | { | |
11654 | /* [temp.deduct.call] | |
c8094d83 | 11655 | |
830bfa74 | 11656 | If P is not a reference type: |
c8094d83 | 11657 | |
830bfa74 MM |
11658 | --If A is an array type, the pointer type produced by the |
11659 | array-to-pointer standard conversion (_conv.array_) is | |
11660 | used in place of A for type deduction; otherwise, | |
c8094d83 | 11661 | |
830bfa74 MM |
11662 | --If A is a function type, the pointer type produced by |
11663 | the function-to-pointer standard conversion | |
11664 | (_conv.func_) is used in place of A for type deduction; | |
11665 | otherwise, | |
c8094d83 | 11666 | |
830bfa74 MM |
11667 | --If A is a cv-qualified type, the top level |
11668 | cv-qualifiers of A's type are ignored for type | |
11669 | deduction. */ | |
11670 | if (TREE_CODE (*arg) == ARRAY_TYPE) | |
11671 | *arg = build_pointer_type (TREE_TYPE (*arg)); | |
d8f8dca1 | 11672 | else if (TREE_CODE (*arg) == FUNCTION_TYPE) |
830bfa74 MM |
11673 | *arg = build_pointer_type (*arg); |
11674 | else | |
11675 | *arg = TYPE_MAIN_VARIANT (*arg); | |
11676 | } | |
c8094d83 | 11677 | |
8af2fec4 RY |
11678 | /* From C++0x [14.8.2.1/3 temp.deduct.call] (after DR606), "If P is |
11679 | of the form T&&, where T is a template parameter, and the argument | |
11680 | is an lvalue, T is deduced as A& */ | |
11681 | if (TREE_CODE (*parm) == REFERENCE_TYPE | |
11682 | && TYPE_REF_IS_RVALUE (*parm) | |
11683 | && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM | |
11684 | && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED | |
11685 | && arg_expr && real_lvalue_p (arg_expr)) | |
11686 | *arg = build_reference_type (*arg); | |
11687 | ||
830bfa74 | 11688 | /* [temp.deduct.call] |
c8094d83 | 11689 | |
830bfa74 MM |
11690 | If P is a cv-qualified type, the top level cv-qualifiers |
11691 | of P's type are ignored for type deduction. If P is a | |
11692 | reference type, the type referred to by P is used for | |
11693 | type deduction. */ | |
11694 | *parm = TYPE_MAIN_VARIANT (*parm); | |
11695 | if (TREE_CODE (*parm) == REFERENCE_TYPE) | |
62e4a758 NS |
11696 | { |
11697 | *parm = TREE_TYPE (*parm); | |
11698 | result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL; | |
11699 | } | |
1c82cc90 NS |
11700 | |
11701 | /* DR 322. For conversion deduction, remove a reference type on parm | |
11702 | too (which has been swapped into ARG). */ | |
11703 | if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE) | |
11704 | *arg = TREE_TYPE (*arg); | |
c8094d83 | 11705 | |
62e4a758 | 11706 | return result; |
386b8a85 JM |
11707 | } |
11708 | ||
e5214479 | 11709 | /* Most parms like fn_type_unification. |
9f54c803 MM |
11710 | |
11711 | If SUBR is 1, we're being called recursively (to unify the | |
11712 | arguments of a function or method parameter of a function | |
38d18b1a | 11713 | template). */ |
386b8a85 | 11714 | |
4966381a | 11715 | static int |
c8094d83 | 11716 | type_unification_real (tree tparms, |
0cbd7506 MS |
11717 | tree targs, |
11718 | tree xparms, | |
11719 | tree xargs, | |
11720 | int subr, | |
30f86ec3 FJ |
11721 | unification_kind_t strict, |
11722 | int flags) | |
8d08fdba | 11723 | { |
8af2fec4 | 11724 | tree parm, arg, arg_expr; |
8d08fdba MS |
11725 | int i; |
11726 | int ntparms = TREE_VEC_LENGTH (tparms); | |
830bfa74 | 11727 | int sub_strict; |
bd0d5d4a JM |
11728 | int saw_undeduced = 0; |
11729 | tree parms, args; | |
8d08fdba | 11730 | |
50bc768d NS |
11731 | gcc_assert (TREE_CODE (tparms) == TREE_VEC); |
11732 | gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST); | |
11733 | gcc_assert (!xargs || TREE_CODE (xargs) == TREE_LIST); | |
11734 | gcc_assert (ntparms > 0); | |
8d08fdba | 11735 | |
830bfa74 MM |
11736 | switch (strict) |
11737 | { | |
11738 | case DEDUCE_CALL: | |
028d1f20 | 11739 | sub_strict = (UNIFY_ALLOW_OUTER_LEVEL | UNIFY_ALLOW_MORE_CV_QUAL |
0cbd7506 | 11740 | | UNIFY_ALLOW_DERIVED); |
830bfa74 | 11741 | break; |
c8094d83 | 11742 | |
830bfa74 MM |
11743 | case DEDUCE_CONV: |
11744 | sub_strict = UNIFY_ALLOW_LESS_CV_QUAL; | |
11745 | break; | |
11746 | ||
11747 | case DEDUCE_EXACT: | |
11748 | sub_strict = UNIFY_ALLOW_NONE; | |
11749 | break; | |
c8094d83 | 11750 | |
830bfa74 | 11751 | default: |
315fb5db | 11752 | gcc_unreachable (); |
830bfa74 MM |
11753 | } |
11754 | ||
bd0d5d4a JM |
11755 | again: |
11756 | parms = xparms; | |
11757 | args = xargs; | |
bd0d5d4a | 11758 | |
a34d3336 NS |
11759 | while (parms && parms != void_list_node |
11760 | && args && args != void_list_node) | |
8d08fdba | 11761 | { |
5d80a306 DG |
11762 | if (TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION) |
11763 | break; | |
11764 | ||
8d08fdba MS |
11765 | parm = TREE_VALUE (parms); |
11766 | parms = TREE_CHAIN (parms); | |
11767 | arg = TREE_VALUE (args); | |
11768 | args = TREE_CHAIN (args); | |
8af2fec4 | 11769 | arg_expr = NULL; |
8d08fdba MS |
11770 | |
11771 | if (arg == error_mark_node) | |
11772 | return 1; | |
11773 | if (arg == unknown_type_node) | |
34016c81 JM |
11774 | /* We can't deduce anything from this, but we might get all the |
11775 | template args from other function args. */ | |
11776 | continue; | |
b7484fbe | 11777 | |
03e70705 JM |
11778 | /* Conversions will be performed on a function argument that |
11779 | corresponds with a function parameter that contains only | |
11780 | non-deducible template parameters and explicitly specified | |
11781 | template parameters. */ | |
c353b8e3 | 11782 | if (!uses_template_parms (parm)) |
b7484fbe | 11783 | { |
03e70705 JM |
11784 | tree type; |
11785 | ||
2f939d94 | 11786 | if (!TYPE_P (arg)) |
03e70705 JM |
11787 | type = TREE_TYPE (arg); |
11788 | else | |
c353b8e3 | 11789 | type = arg; |
03e70705 | 11790 | |
fad86f7a | 11791 | if (same_type_p (parm, type)) |
343c89cd | 11792 | continue; |
fad86f7a | 11793 | if (strict != DEDUCE_EXACT |
3db45ab5 | 11794 | && can_convert_arg (parm, type, TYPE_P (arg) ? NULL_TREE : arg, |
30f86ec3 | 11795 | flags)) |
fad86f7a | 11796 | continue; |
3db45ab5 | 11797 | |
b7484fbe MS |
11798 | return 1; |
11799 | } | |
c8094d83 | 11800 | |
2f939d94 | 11801 | if (!TYPE_P (arg)) |
8d08fdba | 11802 | { |
50bc768d | 11803 | gcc_assert (TREE_TYPE (arg) != NULL_TREE); |
34016c81 | 11804 | if (type_unknown_p (arg)) |
28cbf42c | 11805 | { |
acde59b4 | 11806 | /* [temp.deduct.type] |
34016c81 | 11807 | |
acde59b4 MM |
11808 | A template-argument can be deduced from a pointer to |
11809 | function or pointer to member function argument if | |
11810 | the set of overloaded functions does not contain | |
11811 | function templates and at most one of a set of | |
11812 | overloaded functions provides a unique match. */ | |
34016c81 | 11813 | if (resolve_overloaded_unification |
acde59b4 MM |
11814 | (tparms, targs, parm, arg, strict, sub_strict)) |
11815 | continue; | |
11816 | ||
11817 | return 1; | |
28cbf42c | 11818 | } |
8af2fec4 | 11819 | arg_expr = arg; |
f9aa54d3 | 11820 | arg = unlowered_expr_type (arg); |
08476342 NS |
11821 | if (arg == error_mark_node) |
11822 | return 1; | |
8d08fdba | 11823 | } |
c8094d83 | 11824 | |
62e4a758 | 11825 | { |
0cbd7506 | 11826 | int arg_strict = sub_strict; |
c8094d83 | 11827 | |
0cbd7506 | 11828 | if (!subr) |
8af2fec4 RY |
11829 | arg_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, |
11830 | arg_expr); | |
4393e105 | 11831 | |
0cbd7506 MS |
11832 | if (unify (tparms, targs, parm, arg, arg_strict)) |
11833 | return 1; | |
62e4a758 | 11834 | } |
8d08fdba | 11835 | } |
c8094d83 | 11836 | |
5d80a306 DG |
11837 | |
11838 | if (parms | |
11839 | && parms != void_list_node | |
11840 | && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION) | |
11841 | { | |
11842 | /* Unify the remaining arguments with the pack expansion type. */ | |
11843 | tree argvec; | |
11844 | tree parmvec = make_tree_vec (1); | |
11845 | int len = 0; | |
11846 | tree t; | |
11847 | ||
11848 | /* Count the number of arguments that remain. */ | |
11849 | for (t = args; t && t != void_list_node; t = TREE_CHAIN (t)) | |
11850 | len++; | |
11851 | ||
11852 | /* Allocate a TREE_VEC and copy in all of the arguments */ | |
11853 | argvec = make_tree_vec (len); | |
11854 | for (i = 0; args && args != void_list_node; args = TREE_CHAIN (args)) | |
11855 | { | |
11856 | TREE_VEC_ELT (argvec, i) = TREE_VALUE (args); | |
11857 | ++i; | |
11858 | } | |
11859 | ||
11860 | /* Copy the parameter into parmvec. */ | |
11861 | TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms); | |
11862 | if (unify_pack_expansion (tparms, targs, parmvec, argvec, strict, | |
11863 | /*call_args_p=*/true, /*subr=*/subr)) | |
11864 | return 1; | |
11865 | ||
11866 | /* Advance to the end of the list of parameters. */ | |
11867 | parms = TREE_CHAIN (parms); | |
11868 | } | |
11869 | ||
8d08fdba MS |
11870 | /* Fail if we've reached the end of the parm list, and more args |
11871 | are present, and the parm list isn't variadic. */ | |
11872 | if (args && args != void_list_node && parms == void_list_node) | |
11873 | return 1; | |
f4f206f4 | 11874 | /* Fail if parms are left and they don't have default values. */ |
a34d3336 | 11875 | if (parms && parms != void_list_node |
8d08fdba MS |
11876 | && TREE_PURPOSE (parms) == NULL_TREE) |
11877 | return 1; | |
bd0d5d4a | 11878 | |
8d08fdba MS |
11879 | if (!subr) |
11880 | for (i = 0; i < ntparms; i++) | |
a34d3336 | 11881 | if (!TREE_VEC_ELT (targs, i)) |
8d08fdba | 11882 | { |
2d8ba2c7 LM |
11883 | tree tparm; |
11884 | ||
11885 | if (TREE_VEC_ELT (tparms, i) == error_mark_node) | |
11886 | continue; | |
11887 | ||
11888 | tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i)); | |
bd0d5d4a JM |
11889 | |
11890 | /* If this is an undeduced nontype parameter that depends on | |
11891 | a type parameter, try another pass; its type may have been | |
11892 | deduced from a later argument than the one from which | |
11893 | this parameter can be deduced. */ | |
11894 | if (TREE_CODE (tparm) == PARM_DECL | |
11895 | && uses_template_parms (TREE_TYPE (tparm)) | |
11896 | && !saw_undeduced++) | |
11897 | goto again; | |
11898 | ||
9b7dd5e8 DG |
11899 | /* Core issue #226 (C++0x) [temp.deduct]: |
11900 | ||
11901 | If a template argument has not been deduced, its | |
11902 | default template argument, if any, is used. | |
11903 | ||
c1ae8be5 SM |
11904 | When we are in C++98 mode, TREE_PURPOSE will either |
11905 | be NULL_TREE or ERROR_MARK_NODE, so we do not need | |
11906 | to explicitly check cxx_dialect here. */ | |
9b7dd5e8 DG |
11907 | if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i))) |
11908 | { | |
11909 | tree arg = tsubst (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)), | |
11910 | targs, tf_none, NULL_TREE); | |
11911 | if (arg == error_mark_node) | |
11912 | return 1; | |
11913 | else | |
11914 | { | |
11915 | TREE_VEC_ELT (targs, i) = arg; | |
11916 | continue; | |
11917 | } | |
11918 | } | |
11919 | ||
b1d7b1c0 DG |
11920 | /* If the type parameter is a parameter pack, then it will |
11921 | be deduced to an empty parameter pack. */ | |
11922 | if (template_parameter_pack_p (tparm)) | |
11923 | { | |
11924 | tree arg; | |
11925 | ||
11926 | if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX) | |
11927 | { | |
11928 | arg = make_node (NONTYPE_ARGUMENT_PACK); | |
11929 | TREE_TYPE (arg) = TREE_TYPE (TEMPLATE_PARM_DECL (tparm)); | |
11930 | TREE_CONSTANT (arg) = 1; | |
11931 | } | |
11932 | else | |
11933 | arg = make_node (TYPE_ARGUMENT_PACK); | |
11934 | ||
11935 | SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0)); | |
11936 | ||
11937 | TREE_VEC_ELT (targs, i) = arg; | |
11938 | continue; | |
11939 | } | |
11940 | ||
8d08fdba MS |
11941 | return 2; |
11942 | } | |
c8094d83 | 11943 | |
8d08fdba MS |
11944 | return 0; |
11945 | } | |
11946 | ||
acde59b4 MM |
11947 | /* Subroutine of type_unification_real. Args are like the variables |
11948 | at the call site. ARG is an overloaded function (or template-id); | |
11949 | we try deducing template args from each of the overloads, and if | |
11950 | only one succeeds, we go with that. Modifies TARGS and returns | |
11951 | true on success. */ | |
34016c81 | 11952 | |
acde59b4 | 11953 | static bool |
c8094d83 | 11954 | resolve_overloaded_unification (tree tparms, |
0cbd7506 MS |
11955 | tree targs, |
11956 | tree parm, | |
11957 | tree arg, | |
11958 | unification_kind_t strict, | |
3a978d72 | 11959 | int sub_strict) |
34016c81 JM |
11960 | { |
11961 | tree tempargs = copy_node (targs); | |
11962 | int good = 0; | |
f23fb7f5 | 11963 | bool addr_p; |
34016c81 JM |
11964 | |
11965 | if (TREE_CODE (arg) == ADDR_EXPR) | |
f23fb7f5 MM |
11966 | { |
11967 | arg = TREE_OPERAND (arg, 0); | |
11968 | addr_p = true; | |
11969 | } | |
11970 | else | |
11971 | addr_p = false; | |
9f3d9e46 | 11972 | |
d8f8dca1 MM |
11973 | if (TREE_CODE (arg) == COMPONENT_REF) |
11974 | /* Handle `&x' where `x' is some static or non-static member | |
11975 | function name. */ | |
11976 | arg = TREE_OPERAND (arg, 1); | |
11977 | ||
05e0b2f4 JM |
11978 | if (TREE_CODE (arg) == OFFSET_REF) |
11979 | arg = TREE_OPERAND (arg, 1); | |
11980 | ||
9f3d9e46 | 11981 | /* Strip baselink information. */ |
50ad9642 MM |
11982 | if (BASELINK_P (arg)) |
11983 | arg = BASELINK_FUNCTIONS (arg); | |
9f3d9e46 | 11984 | |
34016c81 JM |
11985 | if (TREE_CODE (arg) == TEMPLATE_ID_EXPR) |
11986 | { | |
11987 | /* If we got some explicit template args, we need to plug them into | |
11988 | the affected templates before we try to unify, in case the | |
11989 | explicit args will completely resolve the templates in question. */ | |
11990 | ||
11991 | tree expl_subargs = TREE_OPERAND (arg, 1); | |
11992 | arg = TREE_OPERAND (arg, 0); | |
11993 | ||
11994 | for (; arg; arg = OVL_NEXT (arg)) | |
11995 | { | |
11996 | tree fn = OVL_CURRENT (arg); | |
11997 | tree subargs, elem; | |
11998 | ||
11999 | if (TREE_CODE (fn) != TEMPLATE_DECL) | |
12000 | continue; | |
12001 | ||
a34d3336 NS |
12002 | subargs = get_bindings (fn, DECL_TEMPLATE_RESULT (fn), |
12003 | expl_subargs, /*check_ret=*/false); | |
34016c81 JM |
12004 | if (subargs) |
12005 | { | |
c2ea3a40 | 12006 | elem = tsubst (TREE_TYPE (fn), subargs, tf_none, NULL_TREE); |
c8094d83 | 12007 | good += try_one_overload (tparms, targs, tempargs, parm, |
f23fb7f5 | 12008 | elem, strict, sub_strict, addr_p); |
34016c81 JM |
12009 | } |
12010 | } | |
12011 | } | |
acde59b4 MM |
12012 | else if (TREE_CODE (arg) != OVERLOAD |
12013 | && TREE_CODE (arg) != FUNCTION_DECL) | |
12014 | /* If ARG is, for example, "(0, &f)" then its type will be unknown | |
12015 | -- but the deduction does not succeed because the expression is | |
12016 | not just the function on its own. */ | |
12017 | return false; | |
315fb5db | 12018 | else |
acde59b4 MM |
12019 | for (; arg; arg = OVL_NEXT (arg)) |
12020 | good += try_one_overload (tparms, targs, tempargs, parm, | |
12021 | TREE_TYPE (OVL_CURRENT (arg)), | |
12022 | strict, sub_strict, addr_p); | |
34016c81 JM |
12023 | |
12024 | /* [temp.deduct.type] A template-argument can be deduced from a pointer | |
12025 | to function or pointer to member function argument if the set of | |
12026 | overloaded functions does not contain function templates and at most | |
12027 | one of a set of overloaded functions provides a unique match. | |
12028 | ||
12029 | So if we found multiple possibilities, we return success but don't | |
12030 | deduce anything. */ | |
12031 | ||
12032 | if (good == 1) | |
12033 | { | |
12034 | int i = TREE_VEC_LENGTH (targs); | |
12035 | for (; i--; ) | |
12036 | if (TREE_VEC_ELT (tempargs, i)) | |
12037 | TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (tempargs, i); | |
12038 | } | |
12039 | if (good) | |
acde59b4 | 12040 | return true; |
34016c81 | 12041 | |
acde59b4 | 12042 | return false; |
34016c81 JM |
12043 | } |
12044 | ||
12045 | /* Subroutine of resolve_overloaded_unification; does deduction for a single | |
12046 | overload. Fills TARGS with any deduced arguments, or error_mark_node if | |
12047 | different overloads deduce different arguments for a given parm. | |
f23fb7f5 MM |
12048 | ADDR_P is true if the expression for which deduction is being |
12049 | performed was of the form "& fn" rather than simply "fn". | |
12050 | ||
34016c81 JM |
12051 | Returns 1 on success. */ |
12052 | ||
12053 | static int | |
3a978d72 | 12054 | try_one_overload (tree tparms, |
0cbd7506 MS |
12055 | tree orig_targs, |
12056 | tree targs, | |
12057 | tree parm, | |
12058 | tree arg, | |
12059 | unification_kind_t strict, | |
f23fb7f5 MM |
12060 | int sub_strict, |
12061 | bool addr_p) | |
34016c81 JM |
12062 | { |
12063 | int nargs; | |
12064 | tree tempargs; | |
12065 | int i; | |
12066 | ||
12067 | /* [temp.deduct.type] A template-argument can be deduced from a pointer | |
12068 | to function or pointer to member function argument if the set of | |
12069 | overloaded functions does not contain function templates and at most | |
12070 | one of a set of overloaded functions provides a unique match. | |
12071 | ||
12072 | So if this is a template, just return success. */ | |
12073 | ||
12074 | if (uses_template_parms (arg)) | |
12075 | return 1; | |
12076 | ||
f23fb7f5 MM |
12077 | if (TREE_CODE (arg) == METHOD_TYPE) |
12078 | arg = build_ptrmemfunc_type (build_pointer_type (arg)); | |
12079 | else if (addr_p) | |
12080 | arg = build_pointer_type (arg); | |
12081 | ||
8af2fec4 | 12082 | sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL); |
34016c81 JM |
12083 | |
12084 | /* We don't copy orig_targs for this because if we have already deduced | |
12085 | some template args from previous args, unify would complain when we | |
12086 | try to deduce a template parameter for the same argument, even though | |
12087 | there isn't really a conflict. */ | |
12088 | nargs = TREE_VEC_LENGTH (targs); | |
f31c0a32 | 12089 | tempargs = make_tree_vec (nargs); |
34016c81 | 12090 | |
4393e105 | 12091 | if (unify (tparms, tempargs, parm, arg, sub_strict) != 0) |
34016c81 JM |
12092 | return 0; |
12093 | ||
12094 | /* First make sure we didn't deduce anything that conflicts with | |
e97e5263 | 12095 | explicitly specified args. */ |
34016c81 JM |
12096 | for (i = nargs; i--; ) |
12097 | { | |
12098 | tree elt = TREE_VEC_ELT (tempargs, i); | |
e97e5263 | 12099 | tree oldelt = TREE_VEC_ELT (orig_targs, i); |
34016c81 | 12100 | |
a34d3336 NS |
12101 | if (!elt) |
12102 | /*NOP*/; | |
34016c81 | 12103 | else if (uses_template_parms (elt)) |
a34d3336 NS |
12104 | /* Since we're unifying against ourselves, we will fill in |
12105 | template args used in the function parm list with our own | |
12106 | template parms. Discard them. */ | |
12107 | TREE_VEC_ELT (tempargs, i) = NULL_TREE; | |
12108 | else if (oldelt && !template_args_equal (oldelt, elt)) | |
34016c81 JM |
12109 | return 0; |
12110 | } | |
12111 | ||
12112 | for (i = nargs; i--; ) | |
12113 | { | |
12114 | tree elt = TREE_VEC_ELT (tempargs, i); | |
12115 | ||
12116 | if (elt) | |
12117 | TREE_VEC_ELT (targs, i) = elt; | |
12118 | } | |
12119 | ||
12120 | return 1; | |
12121 | } | |
12122 | ||
4393e105 MM |
12123 | /* PARM is a template class (perhaps with unbound template |
12124 | parameters). ARG is a fully instantiated type. If ARG can be | |
12125 | bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and | |
12126 | TARGS are as for unify. */ | |
fcfb9f96 MM |
12127 | |
12128 | static tree | |
3a978d72 | 12129 | try_class_unification (tree tparms, tree targs, tree parm, tree arg) |
4393e105 | 12130 | { |
4393e105 MM |
12131 | tree copy_of_targs; |
12132 | ||
12133 | if (!CLASSTYPE_TEMPLATE_INFO (arg) | |
c8094d83 | 12134 | || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg)) |
68361a03 | 12135 | != most_general_template (CLASSTYPE_TI_TEMPLATE (parm)))) |
4393e105 MM |
12136 | return NULL_TREE; |
12137 | ||
12138 | /* We need to make a new template argument vector for the call to | |
12139 | unify. If we used TARGS, we'd clutter it up with the result of | |
12140 | the attempted unification, even if this class didn't work out. | |
12141 | We also don't want to commit ourselves to all the unifications | |
12142 | we've already done, since unification is supposed to be done on | |
12143 | an argument-by-argument basis. In other words, consider the | |
12144 | following pathological case: | |
12145 | ||
12146 | template <int I, int J, int K> | |
12147 | struct S {}; | |
c8094d83 | 12148 | |
4393e105 MM |
12149 | template <int I, int J> |
12150 | struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {}; | |
c8094d83 | 12151 | |
4393e105 MM |
12152 | template <int I, int J, int K> |
12153 | void f(S<I, J, K>, S<I, I, I>); | |
c8094d83 | 12154 | |
4393e105 | 12155 | void g() { |
0cbd7506 MS |
12156 | S<0, 0, 0> s0; |
12157 | S<0, 1, 2> s2; | |
c8094d83 | 12158 | |
0cbd7506 | 12159 | f(s0, s2); |
4393e105 MM |
12160 | } |
12161 | ||
12162 | Now, by the time we consider the unification involving `s2', we | |
12163 | already know that we must have `f<0, 0, 0>'. But, even though | |
0e339752 | 12164 | `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid |
4393e105 MM |
12165 | because there are two ways to unify base classes of S<0, 1, 2> |
12166 | with S<I, I, I>. If we kept the already deduced knowledge, we | |
12167 | would reject the possibility I=1. */ | |
f31c0a32 | 12168 | copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs)); |
c8094d83 | 12169 | |
4393e105 | 12170 | /* If unification failed, we're done. */ |
74601d7c KL |
12171 | if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm), |
12172 | CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE)) | |
4393e105 | 12173 | return NULL_TREE; |
74601d7c KL |
12174 | |
12175 | return arg; | |
4393e105 MM |
12176 | } |
12177 | ||
a3a0fc7f NS |
12178 | /* Given a template type PARM and a class type ARG, find the unique |
12179 | base type in ARG that is an instance of PARM. We do not examine | |
12180 | ARG itself; only its base-classes. If there is not exactly one | |
12181 | appropriate base class, return NULL_TREE. PARM may be the type of | |
12182 | a partial specialization, as well as a plain template type. Used | |
12183 | by unify. */ | |
4393e105 MM |
12184 | |
12185 | static tree | |
a3a0fc7f | 12186 | get_template_base (tree tparms, tree targs, tree parm, tree arg) |
fcfb9f96 | 12187 | { |
a3a0fc7f NS |
12188 | tree rval = NULL_TREE; |
12189 | tree binfo; | |
12190 | ||
12191 | gcc_assert (IS_AGGR_TYPE_CODE (TREE_CODE (arg))); | |
c8094d83 | 12192 | |
a3a0fc7f NS |
12193 | binfo = TYPE_BINFO (complete_type (arg)); |
12194 | if (!binfo) | |
12195 | /* The type could not be completed. */ | |
12196 | return NULL_TREE; | |
fcfb9f96 | 12197 | |
a3a0fc7f NS |
12198 | /* Walk in inheritance graph order. The search order is not |
12199 | important, and this avoids multiple walks of virtual bases. */ | |
12200 | for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo)) | |
fcfb9f96 | 12201 | { |
a3a0fc7f | 12202 | tree r = try_class_unification (tparms, targs, parm, BINFO_TYPE (binfo)); |
fcfb9f96 | 12203 | |
8d83768f NS |
12204 | if (r) |
12205 | { | |
12206 | /* If there is more than one satisfactory baseclass, then: | |
fcfb9f96 | 12207 | |
8d83768f | 12208 | [temp.deduct.call] |
fcfb9f96 | 12209 | |
8d83768f NS |
12210 | If they yield more than one possible deduced A, the type |
12211 | deduction fails. | |
4393e105 | 12212 | |
8d83768f | 12213 | applies. */ |
a3a0fc7f NS |
12214 | if (rval && !same_type_p (r, rval)) |
12215 | return NULL_TREE; | |
c8094d83 | 12216 | |
a3a0fc7f | 12217 | rval = r; |
8d83768f | 12218 | } |
fcfb9f96 MM |
12219 | } |
12220 | ||
a3a0fc7f | 12221 | return rval; |
fcfb9f96 MM |
12222 | } |
12223 | ||
db2767b6 MM |
12224 | /* Returns the level of DECL, which declares a template parameter. */ |
12225 | ||
e9659ab0 | 12226 | static int |
3a978d72 | 12227 | template_decl_level (tree decl) |
db2767b6 MM |
12228 | { |
12229 | switch (TREE_CODE (decl)) | |
12230 | { | |
12231 | case TYPE_DECL: | |
12232 | case TEMPLATE_DECL: | |
12233 | return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl)); | |
12234 | ||
12235 | case PARM_DECL: | |
12236 | return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl)); | |
12237 | ||
12238 | default: | |
315fb5db | 12239 | gcc_unreachable (); |
db2767b6 | 12240 | } |
315fb5db | 12241 | return 0; |
db2767b6 MM |
12242 | } |
12243 | ||
830bfa74 MM |
12244 | /* Decide whether ARG can be unified with PARM, considering only the |
12245 | cv-qualifiers of each type, given STRICT as documented for unify. | |
324f9dfb | 12246 | Returns nonzero iff the unification is OK on that basis. */ |
e92cc029 | 12247 | |
e9659ab0 | 12248 | static int |
3a978d72 | 12249 | check_cv_quals_for_unify (int strict, tree arg, tree parm) |
830bfa74 | 12250 | { |
4f2b0fb2 NS |
12251 | int arg_quals = cp_type_quals (arg); |
12252 | int parm_quals = cp_type_quals (parm); | |
12253 | ||
355f774d NS |
12254 | if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM |
12255 | && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL)) | |
4f2b0fb2 | 12256 | { |
2e9ceb77 | 12257 | /* Although a CVR qualifier is ignored when being applied to a |
0cbd7506 MS |
12258 | substituted template parameter ([8.3.2]/1 for example), that |
12259 | does not apply during deduction [14.8.2.4]/1, (even though | |
12260 | that is not explicitly mentioned, [14.8.2.4]/9 indicates | |
12261 | this). Except when we're allowing additional CV qualifiers | |
12262 | at the outer level [14.8.2.1]/3,1st bullet. */ | |
2e9ceb77 NS |
12263 | if ((TREE_CODE (arg) == REFERENCE_TYPE |
12264 | || TREE_CODE (arg) == FUNCTION_TYPE | |
12265 | || TREE_CODE (arg) == METHOD_TYPE) | |
12266 | && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE))) | |
12267 | return 0; | |
12268 | ||
12269 | if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM) | |
12270 | && (parm_quals & TYPE_QUAL_RESTRICT)) | |
12271 | return 0; | |
4f2b0fb2 | 12272 | } |
2e9ceb77 | 12273 | |
62e4a758 | 12274 | if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL)) |
4f2b0fb2 | 12275 | && (arg_quals & parm_quals) != parm_quals) |
ef637255 MM |
12276 | return 0; |
12277 | ||
62e4a758 | 12278 | if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL)) |
4f2b0fb2 | 12279 | && (parm_quals & arg_quals) != arg_quals) |
ef637255 MM |
12280 | return 0; |
12281 | ||
ef637255 | 12282 | return 1; |
830bfa74 MM |
12283 | } |
12284 | ||
5d80a306 DG |
12285 | /* Determines the LEVEL and INDEX for the template parameter PARM. */ |
12286 | void | |
12287 | template_parm_level_and_index (tree parm, int* level, int* index) | |
12288 | { | |
12289 | if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM | |
12290 | || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM | |
12291 | || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM) | |
12292 | { | |
12293 | *index = TEMPLATE_TYPE_IDX (parm); | |
12294 | *level = TEMPLATE_TYPE_LEVEL (parm); | |
12295 | } | |
12296 | else | |
12297 | { | |
12298 | *index = TEMPLATE_PARM_IDX (parm); | |
12299 | *level = TEMPLATE_PARM_LEVEL (parm); | |
12300 | } | |
12301 | } | |
12302 | ||
12303 | /* Unifies the remaining arguments in PACKED_ARGS with the pack | |
12304 | expansion at the end of PACKED_PARMS. Returns 0 if the type | |
12305 | deduction succeeds, 1 otherwise. STRICT is the same as in | |
12306 | unify. CALL_ARGS_P is true iff PACKED_ARGS is actually a function | |
12307 | call argument list. We'll need to adjust the arguments to make them | |
12308 | types. SUBR tells us if this is from a recursive call to | |
12309 | type_unification_real. */ | |
12310 | int | |
12311 | unify_pack_expansion (tree tparms, tree targs, tree packed_parms, | |
12312 | tree packed_args, int strict, bool call_args_p, | |
12313 | bool subr) | |
12314 | { | |
12315 | tree parm | |
12316 | = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1); | |
12317 | tree pattern = PACK_EXPANSION_PATTERN (parm); | |
12318 | tree pack, packs = NULL_TREE; | |
12319 | int i, start = TREE_VEC_LENGTH (packed_parms) - 1; | |
12320 | int len = TREE_VEC_LENGTH (packed_args); | |
12321 | ||
12322 | /* Determine the parameter packs we will be deducing from the | |
12323 | pattern, and record their current deductions. */ | |
12324 | for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm); | |
12325 | pack; pack = TREE_CHAIN (pack)) | |
12326 | { | |
12327 | tree parm_pack = TREE_VALUE (pack); | |
12328 | int idx, level; | |
12329 | ||
12330 | /* Determine the index and level of this parameter pack. */ | |
12331 | template_parm_level_and_index (parm_pack, &level, &idx); | |
12332 | ||
12333 | /* Keep track of the parameter packs and their corresponding | |
12334 | argument packs. */ | |
12335 | packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs); | |
12336 | TREE_TYPE (packs) = make_tree_vec (len - start); | |
12337 | } | |
12338 | ||
12339 | /* Loop through all of the arguments that have not yet been | |
12340 | unified and unify each with the pattern. */ | |
12341 | for (i = start; i < len; i++) | |
12342 | { | |
12343 | tree parm = pattern; | |
12344 | ||
12345 | /* For each parameter pack, clear out the deduced value so that | |
12346 | we can deduce it again. */ | |
12347 | for (pack = packs; pack; pack = TREE_CHAIN (pack)) | |
12348 | { | |
12349 | int idx, level; | |
12350 | template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx); | |
12351 | ||
12352 | TMPL_ARG (targs, level, idx) = NULL_TREE; | |
12353 | } | |
12354 | ||
12355 | /* Unify the pattern with the current argument. */ | |
12356 | { | |
12357 | tree arg = TREE_VEC_ELT (packed_args, i); | |
2afad0f6 | 12358 | tree arg_expr = NULL_TREE; |
5d80a306 DG |
12359 | int arg_strict = strict; |
12360 | bool skip_arg_p = false; | |
12361 | ||
12362 | if (call_args_p) | |
12363 | { | |
12364 | int sub_strict; | |
12365 | ||
12366 | /* This mirrors what we do in type_unification_real. */ | |
12367 | switch (strict) | |
12368 | { | |
12369 | case DEDUCE_CALL: | |
12370 | sub_strict = (UNIFY_ALLOW_OUTER_LEVEL | |
12371 | | UNIFY_ALLOW_MORE_CV_QUAL | |
12372 | | UNIFY_ALLOW_DERIVED); | |
12373 | break; | |
12374 | ||
12375 | case DEDUCE_CONV: | |
12376 | sub_strict = UNIFY_ALLOW_LESS_CV_QUAL; | |
12377 | break; | |
12378 | ||
12379 | case DEDUCE_EXACT: | |
12380 | sub_strict = UNIFY_ALLOW_NONE; | |
12381 | break; | |
12382 | ||
12383 | default: | |
12384 | gcc_unreachable (); | |
12385 | } | |
12386 | ||
12387 | if (!TYPE_P (arg)) | |
12388 | { | |
12389 | gcc_assert (TREE_TYPE (arg) != NULL_TREE); | |
12390 | if (type_unknown_p (arg)) | |
12391 | { | |
12392 | /* [temp.deduct.type] A template-argument can be | |
12393 | deduced from a pointer to function or pointer | |
12394 | to member function argument if the set of | |
12395 | overloaded functions does not contain function | |
12396 | templates and at most one of a set of | |
12397 | overloaded functions provides a unique | |
12398 | match. */ | |
12399 | ||
12400 | if (resolve_overloaded_unification | |
12401 | (tparms, targs, parm, arg, strict, sub_strict) | |
12402 | != 0) | |
12403 | return 1; | |
12404 | skip_arg_p = true; | |
12405 | } | |
12406 | ||
12407 | if (!skip_arg_p) | |
12408 | { | |
2afad0f6 DG |
12409 | arg_expr = arg; |
12410 | arg = unlowered_expr_type (arg); | |
5d80a306 DG |
12411 | if (arg == error_mark_node) |
12412 | return 1; | |
12413 | } | |
12414 | } | |
12415 | ||
12416 | arg_strict = sub_strict; | |
12417 | ||
12418 | if (!subr) | |
12419 | arg_strict |= | |
2afad0f6 DG |
12420 | maybe_adjust_types_for_deduction (strict, &parm, &arg, |
12421 | arg_expr); | |
5d80a306 DG |
12422 | } |
12423 | ||
12424 | if (!skip_arg_p) | |
12425 | { | |
12426 | if (unify (tparms, targs, parm, arg, arg_strict)) | |
12427 | return 1; | |
12428 | } | |
12429 | } | |
12430 | ||
12431 | /* For each parameter pack, collect the deduced value. */ | |
12432 | for (pack = packs; pack; pack = TREE_CHAIN (pack)) | |
12433 | { | |
12434 | int idx, level; | |
12435 | template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx); | |
12436 | ||
12437 | TREE_VEC_ELT (TREE_TYPE (pack), i - start) = | |
12438 | TMPL_ARG (targs, level, idx); | |
12439 | } | |
12440 | } | |
12441 | ||
12442 | /* Verify that the results of unification with the parameter packs | |
12443 | produce results consistent with what we've seen before, and make | |
12444 | the deduced argument packs available. */ | |
12445 | for (pack = packs; pack; pack = TREE_CHAIN (pack)) | |
12446 | { | |
12447 | tree old_pack = TREE_VALUE (pack); | |
12448 | tree new_args = TREE_TYPE (pack); | |
12449 | ||
12450 | if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack)) | |
12451 | { | |
12452 | /* Prepend the explicit arguments onto NEW_ARGS. */ | |
12453 | tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack); | |
12454 | tree old_args = new_args; | |
12455 | int i, explicit_len = TREE_VEC_LENGTH (explicit_args); | |
12456 | int len = explicit_len + TREE_VEC_LENGTH (old_args); | |
12457 | ||
12458 | /* Copy the explicit arguments. */ | |
12459 | new_args = make_tree_vec (len); | |
12460 | for (i = 0; i < explicit_len; i++) | |
12461 | TREE_VEC_ELT (new_args, i) = TREE_VEC_ELT (explicit_args, i); | |
12462 | ||
12463 | /* Copy the deduced arguments. */ | |
12464 | for (; i < len; i++) | |
12465 | TREE_VEC_ELT (new_args, i) = | |
12466 | TREE_VEC_ELT (old_args, i - explicit_len); | |
12467 | } | |
12468 | ||
12469 | if (!old_pack) | |
12470 | { | |
12471 | tree result; | |
12472 | int idx, level; | |
12473 | ||
12474 | template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx); | |
12475 | ||
12476 | /* Build the deduced *_ARGUMENT_PACK. */ | |
12477 | if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX) | |
12478 | { | |
12479 | result = make_node (NONTYPE_ARGUMENT_PACK); | |
12480 | TREE_TYPE (result) = | |
12481 | TREE_TYPE (TEMPLATE_PARM_DECL (TREE_PURPOSE (pack))); | |
12482 | TREE_CONSTANT (result) = 1; | |
12483 | } | |
12484 | else | |
12485 | result = make_node (TYPE_ARGUMENT_PACK); | |
12486 | ||
12487 | SET_ARGUMENT_PACK_ARGS (result, new_args); | |
12488 | ||
12489 | /* Note the deduced argument packs for this parameter | |
12490 | pack. */ | |
12491 | TMPL_ARG (targs, level, idx) = result; | |
12492 | } | |
12493 | else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack) | |
12494 | && (ARGUMENT_PACK_ARGS (old_pack) | |
12495 | == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack))) | |
12496 | { | |
12497 | /* We only had the explicitly-provided arguments before, but | |
12498 | now we have a complete set of arguments. */ | |
12499 | int idx, level; | |
12500 | tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack); | |
12501 | template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx); | |
12502 | ||
12503 | /* Keep the original deduced argument pack. */ | |
12504 | TMPL_ARG (targs, level, idx) = old_pack; | |
12505 | ||
12506 | SET_ARGUMENT_PACK_ARGS (old_pack, new_args); | |
12507 | ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1; | |
12508 | ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args; | |
12509 | } | |
12510 | else if (!comp_template_args (ARGUMENT_PACK_ARGS (old_pack), | |
12511 | new_args)) | |
12512 | /* Inconsistent unification of this parameter pack. */ | |
12513 | return 1; | |
12514 | else | |
12515 | { | |
12516 | int idx, level; | |
12517 | ||
12518 | template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx); | |
12519 | ||
12520 | /* Keep the original deduced argument pack. */ | |
12521 | TMPL_ARG (targs, level, idx) = old_pack; | |
12522 | } | |
12523 | } | |
12524 | ||
12525 | return 0; | |
12526 | } | |
12527 | ||
916b63c3 MM |
12528 | /* Deduce the value of template parameters. TPARMS is the (innermost) |
12529 | set of template parameters to a template. TARGS is the bindings | |
12530 | for those template parameters, as determined thus far; TARGS may | |
12531 | include template arguments for outer levels of template parameters | |
12532 | as well. PARM is a parameter to a template function, or a | |
12533 | subcomponent of that parameter; ARG is the corresponding argument. | |
12534 | This function attempts to match PARM with ARG in a manner | |
12535 | consistent with the existing assignments in TARGS. If more values | |
12536 | are deduced, then TARGS is updated. | |
12537 | ||
12538 | Returns 0 if the type deduction succeeds, 1 otherwise. The | |
12539 | parameter STRICT is a bitwise or of the following flags: | |
830bfa74 MM |
12540 | |
12541 | UNIFY_ALLOW_NONE: | |
12542 | Require an exact match between PARM and ARG. | |
12543 | UNIFY_ALLOW_MORE_CV_QUAL: | |
028d1f20 NS |
12544 | Allow the deduced ARG to be more cv-qualified (by qualification |
12545 | conversion) than ARG. | |
830bfa74 MM |
12546 | UNIFY_ALLOW_LESS_CV_QUAL: |
12547 | Allow the deduced ARG to be less cv-qualified than ARG. | |
12548 | UNIFY_ALLOW_DERIVED: | |
12549 | Allow the deduced ARG to be a template base class of ARG, | |
12550 | or a pointer to a template base class of the type pointed to by | |
161c12b0 JM |
12551 | ARG. |
12552 | UNIFY_ALLOW_INTEGER: | |
12553 | Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX | |
c8094d83 | 12554 | case for more information. |
028d1f20 NS |
12555 | UNIFY_ALLOW_OUTER_LEVEL: |
12556 | This is the outermost level of a deduction. Used to determine validity | |
12557 | of qualification conversions. A valid qualification conversion must | |
12558 | have const qualified pointers leading up to the inner type which | |
12559 | requires additional CV quals, except at the outer level, where const | |
12560 | is not required [conv.qual]. It would be normal to set this flag in | |
62e4a758 NS |
12561 | addition to setting UNIFY_ALLOW_MORE_CV_QUAL. |
12562 | UNIFY_ALLOW_OUTER_MORE_CV_QUAL: | |
12563 | This is the outermost level of a deduction, and PARM can be more CV | |
12564 | qualified at this point. | |
12565 | UNIFY_ALLOW_OUTER_LESS_CV_QUAL: | |
12566 | This is the outermost level of a deduction, and PARM can be less CV | |
8baddbf1 | 12567 | qualified at this point. */ |
830bfa74 | 12568 | |
e9659ab0 | 12569 | static int |
3a978d72 | 12570 | unify (tree tparms, tree targs, tree parm, tree arg, int strict) |
8d08fdba MS |
12571 | { |
12572 | int idx; | |
050367a3 | 12573 | tree targ; |
db2767b6 | 12574 | tree tparm; |
028d1f20 | 12575 | int strict_in = strict; |
8d08fdba MS |
12576 | |
12577 | /* I don't think this will do the right thing with respect to types. | |
12578 | But the only case I've seen it in so far has been array bounds, where | |
12579 | signedness is the only information lost, and I think that will be | |
12580 | okay. */ | |
12581 | while (TREE_CODE (parm) == NOP_EXPR) | |
12582 | parm = TREE_OPERAND (parm, 0); | |
12583 | ||
12584 | if (arg == error_mark_node) | |
12585 | return 1; | |
12586 | if (arg == unknown_type_node) | |
34016c81 JM |
12587 | /* We can't deduce anything from this, but we might get all the |
12588 | template args from other function args. */ | |
12589 | return 0; | |
12590 | ||
db2767b6 | 12591 | /* If PARM uses template parameters, then we can't bail out here, |
6bdb985f | 12592 | even if ARG == PARM, since we won't record unifications for the |
db2767b6 MM |
12593 | template parameters. We might need them if we're trying to |
12594 | figure out which of two things is more specialized. */ | |
12595 | if (arg == parm && !uses_template_parms (parm)) | |
8d08fdba MS |
12596 | return 0; |
12597 | ||
830bfa74 MM |
12598 | /* Immediately reject some pairs that won't unify because of |
12599 | cv-qualification mismatches. */ | |
12600 | if (TREE_CODE (arg) == TREE_CODE (parm) | |
2f939d94 | 12601 | && TYPE_P (arg) |
d0ab7624 | 12602 | /* It is the elements of the array which hold the cv quals of an array |
0cbd7506 MS |
12603 | type, and the elements might be template type parms. We'll check |
12604 | when we recurse. */ | |
d0ab7624 | 12605 | && TREE_CODE (arg) != ARRAY_TYPE |
830bfa74 MM |
12606 | /* We check the cv-qualifiers when unifying with template type |
12607 | parameters below. We want to allow ARG `const T' to unify with | |
12608 | PARM `T' for example, when computing which of two templates | |
12609 | is more specialized, for example. */ | |
12610 | && TREE_CODE (arg) != TEMPLATE_TYPE_PARM | |
028d1f20 | 12611 | && !check_cv_quals_for_unify (strict_in, arg, parm)) |
49432171 JM |
12612 | return 1; |
12613 | ||
028d1f20 | 12614 | if (!(strict & UNIFY_ALLOW_OUTER_LEVEL) |
3ea099f1 | 12615 | && TYPE_P (parm) && !CP_TYPE_CONST_P (parm)) |
028d1f20 NS |
12616 | strict &= ~UNIFY_ALLOW_MORE_CV_QUAL; |
12617 | strict &= ~UNIFY_ALLOW_OUTER_LEVEL; | |
12618 | strict &= ~UNIFY_ALLOW_DERIVED; | |
62e4a758 NS |
12619 | strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL; |
12620 | strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL; | |
c8094d83 | 12621 | |
8d08fdba MS |
12622 | switch (TREE_CODE (parm)) |
12623 | { | |
2ca340ae | 12624 | case TYPENAME_TYPE: |
fccef71e | 12625 | case SCOPE_REF: |
b8c6534b | 12626 | case UNBOUND_CLASS_TEMPLATE: |
2ca340ae JM |
12627 | /* In a type which contains a nested-name-specifier, template |
12628 | argument values cannot be deduced for template parameters used | |
12629 | within the nested-name-specifier. */ | |
12630 | return 0; | |
12631 | ||
8d08fdba | 12632 | case TEMPLATE_TYPE_PARM: |
73b0fce8 | 12633 | case TEMPLATE_TEMPLATE_PARM: |
a1281f45 | 12634 | case BOUND_TEMPLATE_TEMPLATE_PARM: |
db2767b6 | 12635 | tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0)); |
833be229 PC |
12636 | if (tparm == error_mark_node) |
12637 | return 1; | |
db2767b6 MM |
12638 | |
12639 | if (TEMPLATE_TYPE_LEVEL (parm) | |
12640 | != template_decl_level (tparm)) | |
12641 | /* The PARM is not one we're trying to unify. Just check | |
12642 | to see if it matches ARG. */ | |
12643 | return (TREE_CODE (arg) == TREE_CODE (parm) | |
3bfdc719 | 12644 | && same_type_p (parm, arg)) ? 0 : 1; |
73b0fce8 | 12645 | idx = TEMPLATE_TYPE_IDX (parm); |
916b63c3 | 12646 | targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx); |
db2767b6 | 12647 | tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx)); |
050367a3 | 12648 | |
73b0fce8 | 12649 | /* Check for mixed types and values. */ |
db2767b6 MM |
12650 | if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM |
12651 | && TREE_CODE (tparm) != TYPE_DECL) | |
c8094d83 | 12652 | || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM |
db2767b6 | 12653 | && TREE_CODE (tparm) != TEMPLATE_DECL)) |
73b0fce8 KL |
12654 | return 1; |
12655 | ||
a1281f45 | 12656 | if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM) |
73b0fce8 | 12657 | { |
b429fdf0 KL |
12658 | /* ARG must be constructed from a template class or a template |
12659 | template parameter. */ | |
12660 | if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM | |
4e95268d | 12661 | && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg)) |
a1281f45 | 12662 | return 1; |
73b0fce8 | 12663 | |
a1281f45 | 12664 | { |
a1281f45 | 12665 | tree parmvec = TYPE_TI_ARGS (parm); |
6df91b00 | 12666 | tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg)); |
a1281f45 | 12667 | tree argtmplvec |
b429fdf0 | 12668 | = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_TI_TEMPLATE (arg)); |
a1281f45 | 12669 | int i; |
73b0fce8 | 12670 | |
e7e93965 MM |
12671 | /* The resolution to DR150 makes clear that default |
12672 | arguments for an N-argument may not be used to bind T | |
12673 | to a template template parameter with fewer than N | |
12674 | parameters. It is not safe to permit the binding of | |
12675 | default arguments as an extension, as that may change | |
12676 | the meaning of a conforming program. Consider: | |
12677 | ||
12678 | struct Dense { static const unsigned int dim = 1; }; | |
12679 | ||
12680 | template <template <typename> class View, | |
12681 | typename Block> | |
12682 | void operator+(float, View<Block> const&); | |
12683 | ||
3db45ab5 MS |
12684 | template <typename Block, |
12685 | unsigned int Dim = Block::dim> | |
e7e93965 MM |
12686 | struct Lvalue_proxy { operator float() const; }; |
12687 | ||
12688 | void | |
12689 | test_1d (void) { | |
12690 | Lvalue_proxy<Dense> p; | |
12691 | float b; | |
12692 | b + p; | |
12693 | } | |
a1281f45 | 12694 | |
e7e93965 MM |
12695 | Here, if Lvalue_proxy is permitted to bind to View, then |
12696 | the global operator+ will be used; if they are not, the | |
3db45ab5 MS |
12697 | Lvalue_proxy will be converted to float. */ |
12698 | if (coerce_template_parms (argtmplvec, parmvec, | |
e7e93965 MM |
12699 | TYPE_TI_TEMPLATE (parm), |
12700 | tf_none, | |
12701 | /*require_all_args=*/true, | |
12702 | /*use_default_args=*/false) | |
0cbd7506 | 12703 | == error_mark_node) |
a1281f45 | 12704 | return 1; |
c8094d83 MS |
12705 | |
12706 | /* Deduce arguments T, i from TT<T> or TT<i>. | |
a1281f45 KL |
12707 | We check each element of PARMVEC and ARGVEC individually |
12708 | rather than the whole TREE_VEC since they can have | |
12709 | different number of elements. */ | |
6b9b6b15 | 12710 | |
a1281f45 KL |
12711 | for (i = 0; i < TREE_VEC_LENGTH (parmvec); ++i) |
12712 | { | |
0cbd7506 | 12713 | if (unify (tparms, targs, |
c8094d83 MS |
12714 | TREE_VEC_ELT (parmvec, i), |
12715 | TREE_VEC_ELT (argvec, i), | |
a1281f45 KL |
12716 | UNIFY_ALLOW_NONE)) |
12717 | return 1; | |
73b0fce8 | 12718 | } |
a1281f45 | 12719 | } |
b429fdf0 | 12720 | arg = TYPE_TI_TEMPLATE (arg); |
a1281f45 KL |
12721 | |
12722 | /* Fall through to deduce template name. */ | |
12723 | } | |
12724 | ||
12725 | if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM | |
12726 | || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM) | |
12727 | { | |
12728 | /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */ | |
12729 | ||
12730 | /* Simple cases: Value already set, does match or doesn't. */ | |
12731 | if (targ != NULL_TREE && template_args_equal (targ, arg)) | |
12732 | return 0; | |
12733 | else if (targ) | |
12734 | return 1; | |
db2767b6 MM |
12735 | } |
12736 | else | |
12737 | { | |
830bfa74 MM |
12738 | /* If PARM is `const T' and ARG is only `int', we don't have |
12739 | a match unless we are allowing additional qualification. | |
12740 | If ARG is `const int' and PARM is just `T' that's OK; | |
12741 | that binds `const int' to `T'. */ | |
c8094d83 | 12742 | if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL, |
830bfa74 | 12743 | arg, parm)) |
db2767b6 MM |
12744 | return 1; |
12745 | ||
830bfa74 MM |
12746 | /* Consider the case where ARG is `const volatile int' and |
12747 | PARM is `const T'. Then, T should be `volatile int'. */ | |
c2ea3a40 NS |
12748 | arg = cp_build_qualified_type_real |
12749 | (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none); | |
adecb3f4 MM |
12750 | if (arg == error_mark_node) |
12751 | return 1; | |
73b0fce8 | 12752 | |
a1281f45 KL |
12753 | /* Simple cases: Value already set, does match or doesn't. */ |
12754 | if (targ != NULL_TREE && same_type_p (targ, arg)) | |
12755 | return 0; | |
12756 | else if (targ) | |
12757 | return 1; | |
61cd552e | 12758 | |
94fc547c MM |
12759 | /* Make sure that ARG is not a variable-sized array. (Note |
12760 | that were talking about variable-sized arrays (like | |
12761 | `int[n]'), rather than arrays of unknown size (like | |
12762 | `int[]').) We'll get very confused by such a type since | |
12763 | the bound of the array will not be computable in an | |
12764 | instantiation. Besides, such types are not allowed in | |
12765 | ISO C++, so we can do as we please here. */ | |
5377d5ba | 12766 | if (variably_modified_type_p (arg, NULL_TREE)) |
94fc547c MM |
12767 | return 1; |
12768 | } | |
61cd552e | 12769 | |
5d80a306 DG |
12770 | /* If ARG is a parameter pack or an expansion, we cannot unify |
12771 | against it unless PARM is also a parameter pack. */ | |
12772 | if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg)) | |
12773 | && !template_parameter_pack_p (parm)) | |
12774 | return 1; | |
12775 | ||
916b63c3 | 12776 | TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg; |
73b0fce8 KL |
12777 | return 0; |
12778 | ||
f84b4be9 | 12779 | case TEMPLATE_PARM_INDEX: |
db2767b6 | 12780 | tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0)); |
3e9ac7e5 VR |
12781 | if (tparm == error_mark_node) |
12782 | return 1; | |
db2767b6 | 12783 | |
c8094d83 | 12784 | if (TEMPLATE_PARM_LEVEL (parm) |
db2767b6 MM |
12785 | != template_decl_level (tparm)) |
12786 | /* The PARM is not one we're trying to unify. Just check | |
12787 | to see if it matches ARG. */ | |
c8a209ca NS |
12788 | return !(TREE_CODE (arg) == TREE_CODE (parm) |
12789 | && cp_tree_equal (parm, arg)); | |
db2767b6 | 12790 | |
f84b4be9 | 12791 | idx = TEMPLATE_PARM_IDX (parm); |
916b63c3 | 12792 | targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx); |
db2767b6 | 12793 | |
050367a3 | 12794 | if (targ) |
c8a209ca | 12795 | return !cp_tree_equal (targ, arg); |
8d08fdba | 12796 | |
161c12b0 JM |
12797 | /* [temp.deduct.type] If, in the declaration of a function template |
12798 | with a non-type template-parameter, the non-type | |
12799 | template-parameter is used in an expression in the function | |
12800 | parameter-list and, if the corresponding template-argument is | |
12801 | deduced, the template-argument type shall match the type of the | |
12802 | template-parameter exactly, except that a template-argument | |
c8094d83 | 12803 | deduced from an array bound may be of any integral type. |
d7c4edd0 | 12804 | The non-type parameter might use already deduced type parameters. */ |
bd0d5d4a | 12805 | tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE); |
e2005c8d KL |
12806 | if (!TREE_TYPE (arg)) |
12807 | /* Template-parameter dependent expression. Just accept it for now. | |
12808 | It will later be processed in convert_template_argument. */ | |
12809 | ; | |
12810 | else if (same_type_p (TREE_TYPE (arg), tparm)) | |
12811 | /* OK */; | |
161c12b0 | 12812 | else if ((strict & UNIFY_ALLOW_INTEGER) |
bd0d5d4a JM |
12813 | && (TREE_CODE (tparm) == INTEGER_TYPE |
12814 | || TREE_CODE (tparm) == BOOLEAN_TYPE)) | |
8baddbf1 MM |
12815 | /* Convert the ARG to the type of PARM; the deduced non-type |
12816 | template argument must exactly match the types of the | |
12817 | corresponding parameter. */ | |
12818 | arg = fold (build_nop (TREE_TYPE (parm), arg)); | |
bd0d5d4a JM |
12819 | else if (uses_template_parms (tparm)) |
12820 | /* We haven't deduced the type of this parameter yet. Try again | |
12821 | later. */ | |
12822 | return 0; | |
161c12b0 JM |
12823 | else |
12824 | return 1; | |
12825 | ||
5d80a306 DG |
12826 | /* If ARG is a parameter pack or an expansion, we cannot unify |
12827 | against it unless PARM is also a parameter pack. */ | |
12828 | if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg)) | |
12829 | && !TEMPLATE_PARM_PARAMETER_PACK (parm)) | |
12830 | return 1; | |
12831 | ||
916b63c3 | 12832 | TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg; |
8d08fdba MS |
12833 | return 0; |
12834 | ||
28e8f3a0 GB |
12835 | case PTRMEM_CST: |
12836 | { | |
0cbd7506 MS |
12837 | /* A pointer-to-member constant can be unified only with |
12838 | another constant. */ | |
28e8f3a0 | 12839 | if (TREE_CODE (arg) != PTRMEM_CST) |
0cbd7506 | 12840 | return 1; |
28e8f3a0 GB |
12841 | |
12842 | /* Just unify the class member. It would be useless (and possibly | |
0cbd7506 MS |
12843 | wrong, depending on the strict flags) to unify also |
12844 | PTRMEM_CST_CLASS, because we want to be sure that both parm and | |
12845 | arg refer to the same variable, even if through different | |
12846 | classes. For instance: | |
28e8f3a0 | 12847 | |
0cbd7506 MS |
12848 | struct A { int x; }; |
12849 | struct B : A { }; | |
28e8f3a0 | 12850 | |
0cbd7506 | 12851 | Unification of &A::x and &B::x must succeed. */ |
28e8f3a0 | 12852 | return unify (tparms, targs, PTRMEM_CST_MEMBER (parm), |
0cbd7506 | 12853 | PTRMEM_CST_MEMBER (arg), strict); |
28e8f3a0 GB |
12854 | } |
12855 | ||
8d08fdba | 12856 | case POINTER_TYPE: |
830bfa74 | 12857 | { |
830bfa74 MM |
12858 | if (TREE_CODE (arg) != POINTER_TYPE) |
12859 | return 1; | |
c8094d83 | 12860 | |
830bfa74 MM |
12861 | /* [temp.deduct.call] |
12862 | ||
12863 | A can be another pointer or pointer to member type that can | |
12864 | be converted to the deduced A via a qualification | |
12865 | conversion (_conv.qual_). | |
12866 | ||
12867 | We pass down STRICT here rather than UNIFY_ALLOW_NONE. | |
12868 | This will allow for additional cv-qualification of the | |
028d1f20 | 12869 | pointed-to types if appropriate. */ |
c8094d83 | 12870 | |
028d1f20 | 12871 | if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE) |
830bfa74 MM |
12872 | /* The derived-to-base conversion only persists through one |
12873 | level of pointers. */ | |
028d1f20 | 12874 | strict |= (strict_in & UNIFY_ALLOW_DERIVED); |
830bfa74 | 12875 | |
c8094d83 | 12876 | return unify (tparms, targs, TREE_TYPE (parm), |
028d1f20 | 12877 | TREE_TYPE (arg), strict); |
830bfa74 | 12878 | } |
8d08fdba MS |
12879 | |
12880 | case REFERENCE_TYPE: | |
830bfa74 MM |
12881 | if (TREE_CODE (arg) != REFERENCE_TYPE) |
12882 | return 1; | |
12883 | return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg), | |
028d1f20 | 12884 | strict & UNIFY_ALLOW_MORE_CV_QUAL); |
8d08fdba MS |
12885 | |
12886 | case ARRAY_TYPE: | |
12887 | if (TREE_CODE (arg) != ARRAY_TYPE) | |
12888 | return 1; | |
3042d5be MM |
12889 | if ((TYPE_DOMAIN (parm) == NULL_TREE) |
12890 | != (TYPE_DOMAIN (arg) == NULL_TREE)) | |
12891 | return 1; | |
8baddbf1 MM |
12892 | if (TYPE_DOMAIN (parm) != NULL_TREE) |
12893 | { | |
12894 | tree parm_max; | |
12895 | tree arg_max; | |
6ee3ffe8 MM |
12896 | bool parm_cst; |
12897 | bool arg_cst; | |
8baddbf1 MM |
12898 | |
12899 | /* Our representation of array types uses "N - 1" as the | |
12900 | TYPE_MAX_VALUE for an array with "N" elements, if "N" is | |
6ee3ffe8 MM |
12901 | not an integer constant. We cannot unify arbitrarily |
12902 | complex expressions, so we eliminate the MINUS_EXPRs | |
12903 | here. */ | |
12904 | parm_max = TYPE_MAX_VALUE (TYPE_DOMAIN (parm)); | |
12905 | parm_cst = TREE_CODE (parm_max) == INTEGER_CST; | |
12906 | if (!parm_cst) | |
8baddbf1 | 12907 | { |
6ee3ffe8 | 12908 | gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR); |
8baddbf1 MM |
12909 | parm_max = TREE_OPERAND (parm_max, 0); |
12910 | } | |
6ee3ffe8 MM |
12911 | arg_max = TYPE_MAX_VALUE (TYPE_DOMAIN (arg)); |
12912 | arg_cst = TREE_CODE (arg_max) == INTEGER_CST; | |
12913 | if (!arg_cst) | |
12914 | { | |
12915 | /* The ARG_MAX may not be a simple MINUS_EXPR, if we are | |
12916 | trying to unify the type of a variable with the type | |
12917 | of a template parameter. For example: | |
12918 | ||
12919 | template <unsigned int N> | |
12920 | void f (char (&) [N]); | |
12921 | int g(); | |
12922 | void h(int i) { | |
12923 | char a[g(i)]; | |
12924 | f(a); | |
12925 | } | |
12926 | ||
12927 | Here, the type of the ARG will be "int [g(i)]", and | |
12928 | may be a SAVE_EXPR, etc. */ | |
12929 | if (TREE_CODE (arg_max) != MINUS_EXPR) | |
12930 | return 1; | |
12931 | arg_max = TREE_OPERAND (arg_max, 0); | |
12932 | } | |
12933 | ||
12934 | /* If only one of the bounds used a MINUS_EXPR, compensate | |
12935 | by adding one to the other bound. */ | |
12936 | if (parm_cst && !arg_cst) | |
12937 | parm_max = fold_build2 (PLUS_EXPR, | |
12938 | integer_type_node, | |
12939 | parm_max, | |
12940 | integer_one_node); | |
12941 | else if (arg_cst && !parm_cst) | |
12942 | arg_max = fold_build2 (PLUS_EXPR, | |
12943 | integer_type_node, | |
12944 | arg_max, | |
12945 | integer_one_node); | |
8baddbf1 MM |
12946 | |
12947 | if (unify (tparms, targs, parm_max, arg_max, UNIFY_ALLOW_INTEGER)) | |
12948 | return 1; | |
12949 | } | |
830bfa74 | 12950 | return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg), |
712467a4 | 12951 | strict & UNIFY_ALLOW_MORE_CV_QUAL); |
8d08fdba MS |
12952 | |
12953 | case REAL_TYPE: | |
37c46b43 | 12954 | case COMPLEX_TYPE: |
c00996a3 | 12955 | case VECTOR_TYPE: |
8d08fdba | 12956 | case INTEGER_TYPE: |
42976354 | 12957 | case BOOLEAN_TYPE: |
3590f0a6 | 12958 | case ENUMERAL_TYPE: |
5ad5a526 | 12959 | case VOID_TYPE: |
f376e137 MS |
12960 | if (TREE_CODE (arg) != TREE_CODE (parm)) |
12961 | return 1; | |
c8094d83 | 12962 | |
9edc3913 | 12963 | /* We have already checked cv-qualification at the top of the |
514a1f18 | 12964 | function. */ |
8baddbf1 | 12965 | if (!same_type_ignoring_top_level_qualifiers_p (arg, parm)) |
ca79f85d JM |
12966 | return 1; |
12967 | ||
8d08fdba MS |
12968 | /* As far as unification is concerned, this wins. Later checks |
12969 | will invalidate it if necessary. */ | |
12970 | return 0; | |
12971 | ||
12972 | /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */ | |
bd6dd845 | 12973 | /* Type INTEGER_CST can come from ordinary constant template args. */ |
8d08fdba | 12974 | case INTEGER_CST: |
bd6dd845 MS |
12975 | while (TREE_CODE (arg) == NOP_EXPR) |
12976 | arg = TREE_OPERAND (arg, 0); | |
12977 | ||
8d08fdba MS |
12978 | if (TREE_CODE (arg) != INTEGER_CST) |
12979 | return 1; | |
12980 | return !tree_int_cst_equal (parm, arg); | |
12981 | ||
8d08fdba MS |
12982 | case TREE_VEC: |
12983 | { | |
12984 | int i; | |
12985 | if (TREE_CODE (arg) != TREE_VEC) | |
12986 | return 1; | |
12987 | if (TREE_VEC_LENGTH (parm) != TREE_VEC_LENGTH (arg)) | |
12988 | return 1; | |
0dc09a61 | 12989 | for (i = 0; i < TREE_VEC_LENGTH (parm); ++i) |
830bfa74 | 12990 | if (unify (tparms, targs, |
8d08fdba | 12991 | TREE_VEC_ELT (parm, i), TREE_VEC_ELT (arg, i), |
4393e105 | 12992 | UNIFY_ALLOW_NONE)) |
8d08fdba MS |
12993 | return 1; |
12994 | return 0; | |
12995 | } | |
12996 | ||
8d08fdba | 12997 | case RECORD_TYPE: |
f181d4ae | 12998 | case UNION_TYPE: |
f181d4ae | 12999 | if (TREE_CODE (arg) != TREE_CODE (parm)) |
a4443a08 | 13000 | return 1; |
c8094d83 | 13001 | |
a7a64a77 MM |
13002 | if (TYPE_PTRMEMFUNC_P (parm)) |
13003 | { | |
13004 | if (!TYPE_PTRMEMFUNC_P (arg)) | |
13005 | return 1; | |
13006 | ||
c8094d83 | 13007 | return unify (tparms, targs, |
a7a64a77 MM |
13008 | TYPE_PTRMEMFUNC_FN_TYPE (parm), |
13009 | TYPE_PTRMEMFUNC_FN_TYPE (arg), | |
13010 | strict); | |
13011 | } | |
13012 | ||
5db698f6 | 13013 | if (CLASSTYPE_TEMPLATE_INFO (parm)) |
5566b478 | 13014 | { |
6467930b | 13015 | tree t = NULL_TREE; |
4393e105 | 13016 | |
028d1f20 | 13017 | if (strict_in & UNIFY_ALLOW_DERIVED) |
4393e105 MM |
13018 | { |
13019 | /* First, we try to unify the PARM and ARG directly. */ | |
13020 | t = try_class_unification (tparms, targs, | |
13021 | parm, arg); | |
13022 | ||
13023 | if (!t) | |
13024 | { | |
13025 | /* Fallback to the special case allowed in | |
13026 | [temp.deduct.call]: | |
c8094d83 | 13027 | |
4393e105 MM |
13028 | If P is a class, and P has the form |
13029 | template-id, then A can be a derived class of | |
13030 | the deduced A. Likewise, if P is a pointer to | |
13031 | a class of the form template-id, A can be a | |
13032 | pointer to a derived class pointed to by the | |
13033 | deduced A. */ | |
8d83768f | 13034 | t = get_template_base (tparms, targs, parm, arg); |
4393e105 | 13035 | |
8d83768f | 13036 | if (!t) |
4393e105 MM |
13037 | return 1; |
13038 | } | |
13039 | } | |
c8094d83 MS |
13040 | else if (CLASSTYPE_TEMPLATE_INFO (arg) |
13041 | && (CLASSTYPE_TI_TEMPLATE (parm) | |
9fbf56f7 | 13042 | == CLASSTYPE_TI_TEMPLATE (arg))) |
6df47b06 MM |
13043 | /* Perhaps PARM is something like S<U> and ARG is S<int>. |
13044 | Then, we should unify `int' and `U'. */ | |
6467930b | 13045 | t = arg; |
4393e105 | 13046 | else |
dc957d14 | 13047 | /* There's no chance of unification succeeding. */ |
5566b478 | 13048 | return 1; |
6467930b | 13049 | |
830bfa74 | 13050 | return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm), |
4393e105 | 13051 | CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE); |
5566b478 | 13052 | } |
9edc3913 | 13053 | else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg)) |
5566b478 | 13054 | return 1; |
a4443a08 | 13055 | return 0; |
8d08fdba MS |
13056 | |
13057 | case METHOD_TYPE: | |
8d08fdba | 13058 | case FUNCTION_TYPE: |
830bfa74 | 13059 | if (TREE_CODE (arg) != TREE_CODE (parm)) |
8d08fdba | 13060 | return 1; |
830bfa74 | 13061 | |
38d18b1a | 13062 | /* CV qualifications for methods can never be deduced, they must |
3db45ab5 MS |
13063 | match exactly. We need to check them explicitly here, |
13064 | because type_unification_real treats them as any other | |
13065 | cvqualified parameter. */ | |
38d18b1a NS |
13066 | if (TREE_CODE (parm) == METHOD_TYPE |
13067 | && (!check_cv_quals_for_unify | |
13068 | (UNIFY_ALLOW_NONE, | |
13069 | TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (arg))), | |
13070 | TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (parm)))))) | |
13071 | return 1; | |
13072 | ||
830bfa74 | 13073 | if (unify (tparms, targs, TREE_TYPE (parm), |
4393e105 | 13074 | TREE_TYPE (arg), UNIFY_ALLOW_NONE)) |
28cbf42c | 13075 | return 1; |
386b8a85 | 13076 | return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm), |
30f86ec3 FJ |
13077 | TYPE_ARG_TYPES (arg), 1, DEDUCE_EXACT, |
13078 | LOOKUP_NORMAL); | |
a4443a08 MS |
13079 | |
13080 | case OFFSET_TYPE: | |
9804209d | 13081 | /* Unify a pointer to member with a pointer to member function, which |
0cbd7506 | 13082 | deduces the type of the member as a function type. */ |
9804209d | 13083 | if (TYPE_PTRMEMFUNC_P (arg)) |
0cbd7506 MS |
13084 | { |
13085 | tree method_type; | |
13086 | tree fntype; | |
13087 | cp_cv_quals cv_quals; | |
13088 | ||
13089 | /* Check top-level cv qualifiers */ | |
13090 | if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm)) | |
13091 | return 1; | |
13092 | ||
13093 | if (unify (tparms, targs, TYPE_OFFSET_BASETYPE (parm), | |
13094 | TYPE_PTRMEMFUNC_OBJECT_TYPE (arg), UNIFY_ALLOW_NONE)) | |
13095 | return 1; | |
13096 | ||
13097 | /* Determine the type of the function we are unifying against. */ | |
13098 | method_type = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (arg)); | |
13099 | fntype = | |
13100 | build_function_type (TREE_TYPE (method_type), | |
13101 | TREE_CHAIN (TYPE_ARG_TYPES (method_type))); | |
13102 | ||
13103 | /* Extract the cv-qualifiers of the member function from the | |
13104 | implicit object parameter and place them on the function | |
13105 | type to be restored later. */ | |
13106 | cv_quals = | |
13107 | cp_type_quals(TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (method_type)))); | |
13108 | fntype = build_qualified_type (fntype, cv_quals); | |
13109 | return unify (tparms, targs, TREE_TYPE (parm), fntype, strict); | |
13110 | } | |
9804209d | 13111 | |
a4443a08 MS |
13112 | if (TREE_CODE (arg) != OFFSET_TYPE) |
13113 | return 1; | |
830bfa74 | 13114 | if (unify (tparms, targs, TYPE_OFFSET_BASETYPE (parm), |
4393e105 | 13115 | TYPE_OFFSET_BASETYPE (arg), UNIFY_ALLOW_NONE)) |
a4443a08 | 13116 | return 1; |
830bfa74 | 13117 | return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg), |
4393e105 | 13118 | strict); |
a4443a08 | 13119 | |
f62dbf03 | 13120 | case CONST_DECL: |
a723baf1 MM |
13121 | if (DECL_TEMPLATE_PARM_P (parm)) |
13122 | return unify (tparms, targs, DECL_INITIAL (parm), arg, strict); | |
c8094d83 | 13123 | if (arg != integral_constant_value (parm)) |
f62dbf03 JM |
13124 | return 1; |
13125 | return 0; | |
13126 | ||
28e8f3a0 | 13127 | case FIELD_DECL: |
027905b4 KL |
13128 | case TEMPLATE_DECL: |
13129 | /* Matched cases are handled by the ARG == PARM test above. */ | |
13130 | return 1; | |
13131 | ||
5d80a306 DG |
13132 | case TYPE_ARGUMENT_PACK: |
13133 | case NONTYPE_ARGUMENT_PACK: | |
13134 | { | |
13135 | tree packed_parms = ARGUMENT_PACK_ARGS (parm); | |
13136 | tree packed_args = ARGUMENT_PACK_ARGS (arg); | |
13137 | int i, len = TREE_VEC_LENGTH (packed_parms); | |
13138 | int argslen = TREE_VEC_LENGTH (packed_args); | |
13139 | int parm_variadic_p = 0; | |
13140 | ||
13141 | /* Check if the parameters end in a pack, making them variadic. */ | |
13142 | if (len > 0 | |
13143 | && PACK_EXPANSION_P (TREE_VEC_ELT (packed_parms, len - 1))) | |
13144 | parm_variadic_p = 1; | |
13145 | ||
13146 | /* If we don't have enough arguments to satisfy the parameters | |
13147 | (not counting the pack expression at the end), or we have | |
13148 | too many arguments for a parameter list that doesn't end in | |
13149 | a pack expression, we can't unify. */ | |
13150 | if (argslen < (len - parm_variadic_p) | |
13151 | || (argslen > len && !parm_variadic_p)) | |
13152 | return 1; | |
13153 | ||
13154 | /* Unify all of the parameters that precede the (optional) | |
13155 | pack expression. */ | |
13156 | for (i = 0; i < len - parm_variadic_p; ++i) | |
13157 | { | |
13158 | if (unify (tparms, targs, TREE_VEC_ELT (packed_parms, i), | |
13159 | TREE_VEC_ELT (packed_args, i), strict)) | |
13160 | return 1; | |
13161 | } | |
13162 | ||
13163 | if (parm_variadic_p) | |
13164 | return unify_pack_expansion (tparms, targs, | |
13165 | packed_parms, packed_args, | |
13166 | strict, /*call_args_p=*/false, | |
13167 | /*subr=*/false); | |
13168 | return 0; | |
13169 | } | |
13170 | ||
13171 | break; | |
13172 | ||
3ad6a8e1 DG |
13173 | case TYPEOF_TYPE: |
13174 | case DECLTYPE_TYPE: | |
13175 | /* Cannot deduce anything from TYPEOF_TYPE or DECLTYPE_TYPE | |
13176 | nodes. */ | |
13177 | return 0; | |
13178 | ||
1ad8aeeb DG |
13179 | case ERROR_MARK: |
13180 | /* Unification fails if we hit an error node. */ | |
13181 | return 1; | |
13182 | ||
8d08fdba | 13183 | default: |
6615c446 | 13184 | gcc_assert (EXPR_P (parm)); |
c8094d83 | 13185 | |
98ddffc1 | 13186 | /* We must be looking at an expression. This can happen with |
c8094d83 MS |
13187 | something like: |
13188 | ||
98ddffc1 NS |
13189 | template <int I> |
13190 | void foo(S<I>, S<I + 2>); | |
050367a3 | 13191 | |
98ddffc1 | 13192 | This is a "nondeduced context": |
050367a3 | 13193 | |
98ddffc1 | 13194 | [deduct.type] |
c8094d83 | 13195 | |
98ddffc1 | 13196 | The nondeduced contexts are: |
050367a3 | 13197 | |
98ddffc1 NS |
13198 | --A type that is a template-id in which one or more of |
13199 | the template-arguments is an expression that references | |
c8094d83 | 13200 | a template-parameter. |
050367a3 | 13201 | |
98ddffc1 NS |
13202 | In these cases, we assume deduction succeeded, but don't |
13203 | actually infer any unifications. */ | |
74601d7c | 13204 | |
98ddffc1 NS |
13205 | if (!uses_template_parms (parm) |
13206 | && !template_args_equal (parm, arg)) | |
13207 | return 1; | |
13208 | else | |
13209 | return 0; | |
8d08fdba MS |
13210 | } |
13211 | } | |
8d08fdba | 13212 | \f |
4684cd27 MM |
13213 | /* Note that DECL can be defined in this translation unit, if |
13214 | required. */ | |
13215 | ||
13216 | static void | |
13217 | mark_definable (tree decl) | |
13218 | { | |
13219 | tree clone; | |
13220 | DECL_NOT_REALLY_EXTERN (decl) = 1; | |
13221 | FOR_EACH_CLONE (clone, decl) | |
13222 | DECL_NOT_REALLY_EXTERN (clone) = 1; | |
13223 | } | |
13224 | ||
03d0f4af | 13225 | /* Called if RESULT is explicitly instantiated, or is a member of an |
4684cd27 | 13226 | explicitly instantiated class. */ |
03d0f4af | 13227 | |
faae18ab | 13228 | void |
3a978d72 | 13229 | mark_decl_instantiated (tree result, int extern_p) |
faae18ab | 13230 | { |
415c974c | 13231 | SET_DECL_EXPLICIT_INSTANTIATION (result); |
3ae18eaf | 13232 | |
1f6f0cb6 MM |
13233 | /* If this entity has already been written out, it's too late to |
13234 | make any modifications. */ | |
13235 | if (TREE_ASM_WRITTEN (result)) | |
13236 | return; | |
13237 | ||
13238 | if (TREE_CODE (result) != FUNCTION_DECL) | |
13239 | /* The TREE_PUBLIC flag for function declarations will have been | |
13240 | set correctly by tsubst. */ | |
13241 | TREE_PUBLIC (result) = 1; | |
13242 | ||
346eeb15 JM |
13243 | /* This might have been set by an earlier implicit instantiation. */ |
13244 | DECL_COMDAT (result) = 0; | |
13245 | ||
4684cd27 MM |
13246 | if (extern_p) |
13247 | DECL_NOT_REALLY_EXTERN (result) = 0; | |
13248 | else | |
faae18ab | 13249 | { |
4684cd27 | 13250 | mark_definable (result); |
1a408d07 JM |
13251 | /* Always make artificials weak. */ |
13252 | if (DECL_ARTIFICIAL (result) && flag_weak) | |
13253 | comdat_linkage (result); | |
a7d87521 JM |
13254 | /* For WIN32 we also want to put explicit instantiations in |
13255 | linkonce sections. */ | |
1a408d07 | 13256 | else if (TREE_PUBLIC (result)) |
b385c841 | 13257 | maybe_make_one_only (result); |
faae18ab | 13258 | } |
c8094d83 | 13259 | |
4684cd27 MM |
13260 | /* If EXTERN_P, then this function will not be emitted -- unless |
13261 | followed by an explicit instantiation, at which point its linkage | |
13262 | will be adjusted. If !EXTERN_P, then this function will be | |
13263 | emitted here. In neither circumstance do we want | |
13264 | import_export_decl to adjust the linkage. */ | |
c8094d83 | 13265 | DECL_INTERFACE_KNOWN (result) = 1; |
faae18ab MS |
13266 | } |
13267 | ||
e5214479 | 13268 | /* Given two function templates PAT1 and PAT2, return: |
6467930b MS |
13269 | |
13270 | 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order]. | |
13271 | -1 if PAT2 is more specialized than PAT1. | |
e5214479 JM |
13272 | 0 if neither is more specialized. |
13273 | ||
dda04398 NS |
13274 | LEN indicates the number of parameters we should consider |
13275 | (defaulted parameters should not be considered). | |
13276 | ||
13277 | The 1998 std underspecified function template partial ordering, and | |
13278 | DR214 addresses the issue. We take pairs of arguments, one from | |
c51940a2 | 13279 | each of the templates, and deduce them against each other. One of |
dda04398 NS |
13280 | the templates will be more specialized if all the *other* |
13281 | template's arguments deduce against its arguments and at least one | |
13282 | of its arguments *does* *not* deduce against the other template's | |
13283 | corresponding argument. Deduction is done as for class templates. | |
13284 | The arguments used in deduction have reference and top level cv | |
13285 | qualifiers removed. Iff both arguments were originally reference | |
13286 | types *and* deduction succeeds in both directions, the template | |
13287 | with the more cv-qualified argument wins for that pairing (if | |
13288 | neither is more cv-qualified, they both are equal). Unlike regular | |
13289 | deduction, after all the arguments have been deduced in this way, | |
13290 | we do *not* verify the deduced template argument values can be | |
13291 | substituted into non-deduced contexts, nor do we have to verify | |
13292 | that all template arguments have been deduced. */ | |
c8094d83 | 13293 | |
6467930b | 13294 | int |
dda04398 NS |
13295 | more_specialized_fn (tree pat1, tree pat2, int len) |
13296 | { | |
13297 | tree decl1 = DECL_TEMPLATE_RESULT (pat1); | |
13298 | tree decl2 = DECL_TEMPLATE_RESULT (pat2); | |
13299 | tree targs1 = make_tree_vec (DECL_NTPARMS (pat1)); | |
13300 | tree targs2 = make_tree_vec (DECL_NTPARMS (pat2)); | |
13301 | tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1); | |
13302 | tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2); | |
13303 | tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1)); | |
13304 | tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2)); | |
13305 | int better1 = 0; | |
13306 | int better2 = 0; | |
3db45ab5 | 13307 | |
48884537 NS |
13308 | /* Remove the this parameter from non-static member functions. If |
13309 | one is a non-static member function and the other is not a static | |
13310 | member function, remove the first parameter from that function | |
13311 | also. This situation occurs for operator functions where we | |
13312 | locate both a member function (with this pointer) and non-member | |
13313 | operator (with explicit first operand). */ | |
dda04398 | 13314 | if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1)) |
48884537 NS |
13315 | { |
13316 | len--; /* LEN is the number of significant arguments for DECL1 */ | |
13317 | args1 = TREE_CHAIN (args1); | |
13318 | if (!DECL_STATIC_FUNCTION_P (decl2)) | |
13319 | args2 = TREE_CHAIN (args2); | |
13320 | } | |
13321 | else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2)) | |
13322 | { | |
13323 | args2 = TREE_CHAIN (args2); | |
13324 | if (!DECL_STATIC_FUNCTION_P (decl1)) | |
13325 | { | |
13326 | len--; | |
13327 | args1 = TREE_CHAIN (args1); | |
13328 | } | |
13329 | } | |
3db45ab5 | 13330 | |
ee307009 NS |
13331 | /* If only one is a conversion operator, they are unordered. */ |
13332 | if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2)) | |
13333 | return 0; | |
c8094d83 | 13334 | |
dda04398 NS |
13335 | /* Consider the return type for a conversion function */ |
13336 | if (DECL_CONV_FN_P (decl1)) | |
13337 | { | |
dda04398 NS |
13338 | args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1); |
13339 | args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2); | |
13340 | len++; | |
13341 | } | |
c8094d83 | 13342 | |
dda04398 | 13343 | processing_template_decl++; |
c8094d83 | 13344 | |
dda04398 NS |
13345 | while (len--) |
13346 | { | |
13347 | tree arg1 = TREE_VALUE (args1); | |
13348 | tree arg2 = TREE_VALUE (args2); | |
13349 | int deduce1, deduce2; | |
13350 | int quals1 = -1; | |
13351 | int quals2 = -1; | |
6467930b | 13352 | |
5d80a306 DG |
13353 | if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION |
13354 | && TREE_CODE (arg2) == TYPE_PACK_EXPANSION) | |
13355 | { | |
13356 | /* When both arguments are pack expansions, we need only | |
13357 | unify the patterns themselves. */ | |
13358 | arg1 = PACK_EXPANSION_PATTERN (arg1); | |
13359 | arg2 = PACK_EXPANSION_PATTERN (arg2); | |
13360 | ||
13361 | /* This is the last comparison we need to do. */ | |
13362 | len = 0; | |
13363 | } | |
13364 | ||
dda04398 NS |
13365 | if (TREE_CODE (arg1) == REFERENCE_TYPE) |
13366 | { | |
13367 | arg1 = TREE_TYPE (arg1); | |
13368 | quals1 = cp_type_quals (arg1); | |
13369 | } | |
c8094d83 | 13370 | |
dda04398 NS |
13371 | if (TREE_CODE (arg2) == REFERENCE_TYPE) |
13372 | { | |
13373 | arg2 = TREE_TYPE (arg2); | |
13374 | quals2 = cp_type_quals (arg2); | |
13375 | } | |
6467930b | 13376 | |
dda04398 NS |
13377 | if ((quals1 < 0) != (quals2 < 0)) |
13378 | { | |
13379 | /* Only of the args is a reference, see if we should apply | |
13380 | array/function pointer decay to it. This is not part of | |
13381 | DR214, but is, IMHO, consistent with the deduction rules | |
13382 | for the function call itself, and with our earlier | |
13383 | implementation of the underspecified partial ordering | |
13384 | rules. (nathan). */ | |
13385 | if (quals1 >= 0) | |
13386 | { | |
13387 | switch (TREE_CODE (arg1)) | |
13388 | { | |
13389 | case ARRAY_TYPE: | |
13390 | arg1 = TREE_TYPE (arg1); | |
13391 | /* FALLTHROUGH. */ | |
13392 | case FUNCTION_TYPE: | |
13393 | arg1 = build_pointer_type (arg1); | |
13394 | break; | |
c8094d83 | 13395 | |
dda04398 NS |
13396 | default: |
13397 | break; | |
13398 | } | |
13399 | } | |
13400 | else | |
13401 | { | |
13402 | switch (TREE_CODE (arg2)) | |
13403 | { | |
13404 | case ARRAY_TYPE: | |
13405 | arg2 = TREE_TYPE (arg2); | |
13406 | /* FALLTHROUGH. */ | |
13407 | case FUNCTION_TYPE: | |
13408 | arg2 = build_pointer_type (arg2); | |
13409 | break; | |
c8094d83 | 13410 | |
dda04398 NS |
13411 | default: |
13412 | break; | |
13413 | } | |
13414 | } | |
13415 | } | |
c8094d83 | 13416 | |
dda04398 NS |
13417 | arg1 = TYPE_MAIN_VARIANT (arg1); |
13418 | arg2 = TYPE_MAIN_VARIANT (arg2); | |
c8094d83 | 13419 | |
5d80a306 DG |
13420 | if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION) |
13421 | { | |
b1d7b1c0 | 13422 | int i, len2 = list_length (args2); |
5d80a306 DG |
13423 | tree parmvec = make_tree_vec (1); |
13424 | tree argvec = make_tree_vec (len2); | |
13425 | tree ta = args2; | |
13426 | ||
13427 | /* Setup the parameter vector, which contains only ARG1. */ | |
13428 | TREE_VEC_ELT (parmvec, 0) = arg1; | |
13429 | ||
13430 | /* Setup the argument vector, which contains the remaining | |
13431 | arguments. */ | |
13432 | for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta)) | |
13433 | TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta); | |
13434 | ||
13435 | deduce1 = !unify_pack_expansion (tparms1, targs1, parmvec, | |
13436 | argvec, UNIFY_ALLOW_NONE, | |
13437 | /*call_args_p=*/false, | |
13438 | /*subr=*/0); | |
13439 | ||
13440 | /* We cannot deduce in the other direction, because ARG1 is | |
13441 | a pack expansion but ARG2 is not. */ | |
13442 | deduce2 = 0; | |
13443 | } | |
13444 | else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION) | |
13445 | { | |
b1d7b1c0 | 13446 | int i, len1 = list_length (args1); |
5d80a306 DG |
13447 | tree parmvec = make_tree_vec (1); |
13448 | tree argvec = make_tree_vec (len1); | |
13449 | tree ta = args1; | |
13450 | ||
13451 | /* Setup the parameter vector, which contains only ARG1. */ | |
13452 | TREE_VEC_ELT (parmvec, 0) = arg2; | |
13453 | ||
13454 | /* Setup the argument vector, which contains the remaining | |
13455 | arguments. */ | |
13456 | for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta)) | |
13457 | TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta); | |
13458 | ||
13459 | deduce2 = !unify_pack_expansion (tparms2, targs2, parmvec, | |
13460 | argvec, UNIFY_ALLOW_NONE, | |
13461 | /*call_args_p=*/false, | |
13462 | /*subr=*/0); | |
13463 | ||
13464 | /* We cannot deduce in the other direction, because ARG2 is | |
13465 | a pack expansion but ARG1 is not.*/ | |
13466 | deduce1 = 0; | |
13467 | } | |
13468 | ||
13469 | else | |
13470 | { | |
13471 | /* The normal case, where neither argument is a pack | |
13472 | expansion. */ | |
13473 | deduce1 = !unify (tparms1, targs1, arg1, arg2, UNIFY_ALLOW_NONE); | |
13474 | deduce2 = !unify (tparms2, targs2, arg2, arg1, UNIFY_ALLOW_NONE); | |
13475 | } | |
dda04398 NS |
13476 | |
13477 | if (!deduce1) | |
13478 | better2 = -1; | |
13479 | if (!deduce2) | |
13480 | better1 = -1; | |
13481 | if (better1 < 0 && better2 < 0) | |
13482 | /* We've failed to deduce something in either direction. | |
13483 | These must be unordered. */ | |
13484 | break; | |
c8094d83 | 13485 | |
dda04398 NS |
13486 | if (deduce1 && deduce2 && quals1 >= 0 && quals2 >= 0) |
13487 | { | |
13488 | /* Deduces in both directions, see if quals can | |
13489 | disambiguate. Pretend the worse one failed to deduce. */ | |
13490 | if ((quals1 & quals2) == quals2) | |
13491 | deduce1 = 0; | |
13492 | if ((quals1 & quals2) == quals1) | |
13493 | deduce2 = 0; | |
13494 | } | |
13495 | if (deduce1 && !deduce2 && !better2) | |
13496 | better2 = 1; | |
13497 | if (deduce2 && !deduce1 && !better1) | |
13498 | better1 = 1; | |
c8094d83 | 13499 | |
5d80a306 DG |
13500 | if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION |
13501 | || TREE_CODE (arg2) == TYPE_PACK_EXPANSION) | |
13502 | /* We have already processed all of the arguments in our | |
13503 | handing of the pack expansion type. */ | |
13504 | len = 0; | |
13505 | ||
dda04398 NS |
13506 | args1 = TREE_CHAIN (args1); |
13507 | args2 = TREE_CHAIN (args2); | |
13508 | } | |
13509 | ||
13510 | processing_template_decl--; | |
13511 | ||
5d80a306 DG |
13512 | /* All things being equal, if the next argument is a pack expansion |
13513 | for one function but not for the other, prefer the | |
13514 | non-variadic function. */ | |
13515 | if ((better1 > 0) - (better2 > 0) == 0 | |
13516 | && args1 && TREE_VALUE (args1) | |
13517 | && args2 && TREE_VALUE (args2)) | |
13518 | { | |
13519 | if (TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION) | |
13520 | return TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION ? 0 : -1; | |
13521 | else if (TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION) | |
13522 | return 1; | |
13523 | } | |
13524 | ||
dda04398 | 13525 | return (better1 > 0) - (better2 > 0); |
73aad9b9 | 13526 | } |
6467930b | 13527 | |
916b63c3 | 13528 | /* Determine which of two partial specializations is more specialized. |
6467930b | 13529 | |
916b63c3 MM |
13530 | PAT1 is a TREE_LIST whose TREE_TYPE is the _TYPE node corresponding |
13531 | to the first partial specialization. The TREE_VALUE is the | |
13532 | innermost set of template parameters for the partial | |
13533 | specialization. PAT2 is similar, but for the second template. | |
13534 | ||
13535 | Return 1 if the first partial specialization is more specialized; | |
13536 | -1 if the second is more specialized; 0 if neither is more | |
13537 | specialized. | |
55ece1b3 | 13538 | |
916b63c3 | 13539 | See [temp.class.order] for information about determining which of |
3db45ab5 | 13540 | two templates is more specialized. */ |
c8094d83 | 13541 | |
b5791fdc | 13542 | static int |
916b63c3 | 13543 | more_specialized_class (tree pat1, tree pat2) |
73aad9b9 JM |
13544 | { |
13545 | tree targs; | |
916b63c3 | 13546 | tree tmpl1, tmpl2; |
73aad9b9 | 13547 | int winner = 0; |
5d80a306 | 13548 | bool any_deductions = false; |
73aad9b9 | 13549 | |
916b63c3 MM |
13550 | tmpl1 = TREE_TYPE (pat1); |
13551 | tmpl2 = TREE_TYPE (pat2); | |
13552 | ||
c8094d83 | 13553 | /* Just like what happens for functions, if we are ordering between |
baa49730 GB |
13554 | different class template specializations, we may encounter dependent |
13555 | types in the arguments, and we need our dependency check functions | |
13556 | to behave correctly. */ | |
13557 | ++processing_template_decl; | |
3db45ab5 | 13558 | targs = get_class_bindings (TREE_VALUE (pat1), |
916b63c3 MM |
13559 | CLASSTYPE_TI_ARGS (tmpl1), |
13560 | CLASSTYPE_TI_ARGS (tmpl2)); | |
73aad9b9 | 13561 | if (targs) |
5d80a306 DG |
13562 | { |
13563 | --winner; | |
13564 | any_deductions = true; | |
13565 | } | |
73aad9b9 | 13566 | |
3db45ab5 | 13567 | targs = get_class_bindings (TREE_VALUE (pat2), |
916b63c3 MM |
13568 | CLASSTYPE_TI_ARGS (tmpl2), |
13569 | CLASSTYPE_TI_ARGS (tmpl1)); | |
73aad9b9 | 13570 | if (targs) |
5d80a306 DG |
13571 | { |
13572 | ++winner; | |
13573 | any_deductions = true; | |
13574 | } | |
baa49730 | 13575 | --processing_template_decl; |
6467930b | 13576 | |
5d80a306 DG |
13577 | /* In the case of a tie where at least one of the class templates |
13578 | has a parameter pack at the end, the template with the most | |
13579 | non-packed parameters wins. */ | |
13580 | if (winner == 0 | |
13581 | && any_deductions | |
13582 | && (template_args_variadic_p (TREE_PURPOSE (pat1)) | |
13583 | || template_args_variadic_p (TREE_PURPOSE (pat2)))) | |
13584 | { | |
13585 | tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1)); | |
13586 | tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2)); | |
13587 | int len1 = TREE_VEC_LENGTH (args1); | |
13588 | int len2 = TREE_VEC_LENGTH (args2); | |
13589 | ||
13590 | /* We don't count the pack expansion at the end. */ | |
13591 | if (template_args_variadic_p (TREE_PURPOSE (pat1))) | |
13592 | --len1; | |
13593 | if (template_args_variadic_p (TREE_PURPOSE (pat2))) | |
13594 | --len2; | |
13595 | ||
13596 | if (len1 > len2) | |
13597 | return 1; | |
13598 | else if (len1 < len2) | |
13599 | return -1; | |
13600 | } | |
13601 | ||
6467930b MS |
13602 | return winner; |
13603 | } | |
73aad9b9 JM |
13604 | |
13605 | /* Return the template arguments that will produce the function signature | |
e1467ff2 | 13606 | DECL from the function template FN, with the explicit template |
a34d3336 | 13607 | arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must |
4393e105 | 13608 | also match. Return NULL_TREE if no satisfactory arguments could be |
a34d3336 | 13609 | found. */ |
c8094d83 | 13610 | |
76b9a14d | 13611 | static tree |
a34d3336 | 13612 | get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype) |
73aad9b9 | 13613 | { |
98c1c668 | 13614 | int ntparms = DECL_NTPARMS (fn); |
f31c0a32 | 13615 | tree targs = make_tree_vec (ntparms); |
4393e105 | 13616 | tree decl_type; |
03017874 | 13617 | tree decl_arg_types; |
98c1c668 | 13618 | |
4393e105 MM |
13619 | /* Substitute the explicit template arguments into the type of DECL. |
13620 | The call to fn_type_unification will handle substitution into the | |
13621 | FN. */ | |
13622 | decl_type = TREE_TYPE (decl); | |
13623 | if (explicit_args && uses_template_parms (decl_type)) | |
13624 | { | |
13625 | tree tmpl; | |
13626 | tree converted_args; | |
13627 | ||
13628 | if (DECL_TEMPLATE_INFO (decl)) | |
13629 | tmpl = DECL_TI_TEMPLATE (decl); | |
13630 | else | |
0e339752 | 13631 | /* We can get here for some invalid specializations. */ |
4393e105 MM |
13632 | return NULL_TREE; |
13633 | ||
13634 | converted_args | |
e7e93965 MM |
13635 | = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl), |
13636 | explicit_args, NULL_TREE, | |
3db45ab5 | 13637 | tf_none, |
e7e93965 MM |
13638 | /*require_all_args=*/false, |
13639 | /*use_default_args=*/false); | |
4393e105 MM |
13640 | if (converted_args == error_mark_node) |
13641 | return NULL_TREE; | |
c8094d83 MS |
13642 | |
13643 | decl_type = tsubst (decl_type, converted_args, tf_none, NULL_TREE); | |
4393e105 MM |
13644 | if (decl_type == error_mark_node) |
13645 | return NULL_TREE; | |
13646 | } | |
13647 | ||
e5214479 | 13648 | /* Never do unification on the 'this' parameter. */ |
ba139ba8 MM |
13649 | decl_arg_types = skip_artificial_parms_for (decl, |
13650 | TYPE_ARG_TYPES (decl_type)); | |
d7684f2d | 13651 | |
c8094d83 | 13652 | if (fn_type_unification (fn, explicit_args, targs, |
03017874 | 13653 | decl_arg_types, |
8d3631f8 | 13654 | (check_rettype || DECL_CONV_FN_P (fn) |
0cbd7506 | 13655 | ? TREE_TYPE (decl_type) : NULL_TREE), |
30f86ec3 | 13656 | DEDUCE_EXACT, LOOKUP_NORMAL)) |
76b9a14d JM |
13657 | return NULL_TREE; |
13658 | ||
76b9a14d JM |
13659 | return targs; |
13660 | } | |
13661 | ||
36a117a5 MM |
13662 | /* Return the innermost template arguments that, when applied to a |
13663 | template specialization whose innermost template parameters are | |
9471d3e2 | 13664 | TPARMS, and whose specialization arguments are PARMS, yield the |
c8094d83 | 13665 | ARGS. |
36a117a5 MM |
13666 | |
13667 | For example, suppose we have: | |
13668 | ||
13669 | template <class T, class U> struct S {}; | |
13670 | template <class T> struct S<T*, int> {}; | |
13671 | ||
13672 | Then, suppose we want to get `S<double*, int>'. The TPARMS will be | |
916b63c3 | 13673 | {T}, the SPEC_ARGS will be {T*, int} and the ARGS will be {double*, |
36a117a5 MM |
13674 | int}. The resulting vector will be {double}, indicating that `T' |
13675 | is bound to `double'. */ | |
13676 | ||
bd6dd845 | 13677 | static tree |
916b63c3 | 13678 | get_class_bindings (tree tparms, tree spec_args, tree args) |
73aad9b9 | 13679 | { |
3b3ba9f0 | 13680 | int i, ntparms = TREE_VEC_LENGTH (tparms); |
916b63c3 MM |
13681 | tree deduced_args; |
13682 | tree innermost_deduced_args; | |
73aad9b9 | 13683 | |
916b63c3 MM |
13684 | innermost_deduced_args = make_tree_vec (ntparms); |
13685 | if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args)) | |
13686 | { | |
13687 | deduced_args = copy_node (args); | |
13688 | SET_TMPL_ARGS_LEVEL (deduced_args, | |
13689 | TMPL_ARGS_DEPTH (deduced_args), | |
13690 | innermost_deduced_args); | |
13691 | } | |
13692 | else | |
3db45ab5 | 13693 | deduced_args = innermost_deduced_args; |
916b63c3 | 13694 | |
3db45ab5 MS |
13695 | if (unify (tparms, deduced_args, |
13696 | INNERMOST_TEMPLATE_ARGS (spec_args), | |
13697 | INNERMOST_TEMPLATE_ARGS (args), | |
0cbd7506 | 13698 | UNIFY_ALLOW_NONE)) |
fcfb9f96 | 13699 | return NULL_TREE; |
73aad9b9 JM |
13700 | |
13701 | for (i = 0; i < ntparms; ++i) | |
916b63c3 | 13702 | if (! TREE_VEC_ELT (innermost_deduced_args, i)) |
73aad9b9 JM |
13703 | return NULL_TREE; |
13704 | ||
916b63c3 MM |
13705 | /* Verify that nondeduced template arguments agree with the type |
13706 | obtained from argument deduction. | |
3db45ab5 | 13707 | |
916b63c3 MM |
13708 | For example: |
13709 | ||
13710 | struct A { typedef int X; }; | |
13711 | template <class T, class U> struct C {}; | |
13712 | template <class T> struct C<T, typename T::X> {}; | |
13713 | ||
13714 | Then with the instantiation `C<A, int>', we can deduce that | |
13715 | `T' is `A' but unify () does not check whether `typename T::X' | |
13716 | is `int'. */ | |
13717 | spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE); | |
13718 | if (spec_args == error_mark_node | |
13719 | /* We only need to check the innermost arguments; the other | |
13720 | arguments will always agree. */ | |
13721 | || !comp_template_args (INNERMOST_TEMPLATE_ARGS (spec_args), | |
13722 | INNERMOST_TEMPLATE_ARGS (args))) | |
74601d7c KL |
13723 | return NULL_TREE; |
13724 | ||
916b63c3 | 13725 | return deduced_args; |
73aad9b9 JM |
13726 | } |
13727 | ||
7ca383e6 MM |
13728 | /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL. |
13729 | Return the TREE_LIST node with the most specialized template, if | |
13730 | any. If there is no most specialized template, the error_mark_node | |
13731 | is returned. | |
13732 | ||
13733 | Note that this function does not look at, or modify, the | |
13734 | TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node | |
13735 | returned is one of the elements of INSTANTIATIONS, callers may | |
13736 | store information in the TREE_PURPOSE or TREE_TYPE of the nodes, | |
13737 | and retrieve it from the value returned. */ | |
73aad9b9 JM |
13738 | |
13739 | tree | |
7ca383e6 | 13740 | most_specialized_instantiation (tree templates) |
73aad9b9 | 13741 | { |
104bf76a | 13742 | tree fn, champ; |
73aad9b9 | 13743 | |
dda04398 | 13744 | ++processing_template_decl; |
c8094d83 | 13745 | |
7ca383e6 MM |
13746 | champ = templates; |
13747 | for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn)) | |
73aad9b9 | 13748 | { |
dda04398 | 13749 | int fate = 0; |
c8094d83 | 13750 | |
a34d3336 NS |
13751 | if (get_bindings (TREE_VALUE (champ), |
13752 | DECL_TEMPLATE_RESULT (TREE_VALUE (fn)), | |
13753 | NULL_TREE, /*check_ret=*/false)) | |
dda04398 NS |
13754 | fate--; |
13755 | ||
a34d3336 NS |
13756 | if (get_bindings (TREE_VALUE (fn), |
13757 | DECL_TEMPLATE_RESULT (TREE_VALUE (champ)), | |
13758 | NULL_TREE, /*check_ret=*/false)) | |
dda04398 | 13759 | fate++; |
c8094d83 | 13760 | |
8ca4bf25 MM |
13761 | if (fate == -1) |
13762 | champ = fn; | |
13763 | else if (!fate) | |
73aad9b9 | 13764 | { |
8ca4bf25 MM |
13765 | /* Equally specialized, move to next function. If there |
13766 | is no next function, nothing's most specialized. */ | |
13767 | fn = TREE_CHAIN (fn); | |
7ca383e6 | 13768 | champ = fn; |
8ca4bf25 MM |
13769 | if (!fn) |
13770 | break; | |
73aad9b9 JM |
13771 | } |
13772 | } | |
c8094d83 | 13773 | |
dda04398 NS |
13774 | if (champ) |
13775 | /* Now verify that champ is better than everything earlier in the | |
13776 | instantiation list. */ | |
7ca383e6 | 13777 | for (fn = templates; fn != champ; fn = TREE_CHAIN (fn)) |
a34d3336 NS |
13778 | if (get_bindings (TREE_VALUE (champ), |
13779 | DECL_TEMPLATE_RESULT (TREE_VALUE (fn)), | |
13780 | NULL_TREE, /*check_ret=*/false) | |
13781 | || !get_bindings (TREE_VALUE (fn), | |
13782 | DECL_TEMPLATE_RESULT (TREE_VALUE (champ)), | |
13783 | NULL_TREE, /*check_ret=*/false)) | |
dda04398 NS |
13784 | { |
13785 | champ = NULL_TREE; | |
13786 | break; | |
13787 | } | |
c8094d83 | 13788 | |
dda04398 | 13789 | processing_template_decl--; |
c8094d83 | 13790 | |
dda04398 NS |
13791 | if (!champ) |
13792 | return error_mark_node; | |
73aad9b9 | 13793 | |
7ca383e6 | 13794 | return champ; |
73aad9b9 JM |
13795 | } |
13796 | ||
36a117a5 | 13797 | /* If DECL is a specialization of some template, return the most |
f9a7ae04 MM |
13798 | general such template. Otherwise, returns NULL_TREE. |
13799 | ||
13800 | For example, given: | |
36a117a5 MM |
13801 | |
13802 | template <class T> struct S { template <class U> void f(U); }; | |
13803 | ||
13804 | if TMPL is `template <class U> void S<int>::f(U)' this will return | |
13805 | the full template. This function will not trace past partial | |
13806 | specializations, however. For example, given in addition: | |
13807 | ||
13808 | template <class T> struct S<T*> { template <class U> void f(U); }; | |
13809 | ||
13810 | if TMPL is `template <class U> void S<int*>::f(U)' this will return | |
13811 | `template <class T> template <class U> S<T*>::f(U)'. */ | |
73aad9b9 | 13812 | |
612c671a | 13813 | tree |
3a978d72 | 13814 | most_general_template (tree decl) |
73aad9b9 | 13815 | { |
f9a7ae04 MM |
13816 | /* If DECL is a FUNCTION_DECL, find the TEMPLATE_DECL of which it is |
13817 | an immediate specialization. */ | |
13818 | if (TREE_CODE (decl) == FUNCTION_DECL) | |
13819 | { | |
13820 | if (DECL_TEMPLATE_INFO (decl)) { | |
13821 | decl = DECL_TI_TEMPLATE (decl); | |
13822 | ||
13823 | /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE for a | |
13824 | template friend. */ | |
13825 | if (TREE_CODE (decl) != TEMPLATE_DECL) | |
13826 | return NULL_TREE; | |
13827 | } else | |
13828 | return NULL_TREE; | |
13829 | } | |
13830 | ||
13831 | /* Look for more and more general templates. */ | |
13832 | while (DECL_TEMPLATE_INFO (decl)) | |
13833 | { | |
10b1d5e7 MM |
13834 | /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases. |
13835 | (See cp-tree.h for details.) */ | |
f9a7ae04 MM |
13836 | if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL) |
13837 | break; | |
13838 | ||
6e049fcd KL |
13839 | if (CLASS_TYPE_P (TREE_TYPE (decl)) |
13840 | && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl))) | |
13841 | break; | |
13842 | ||
f9a7ae04 MM |
13843 | /* Stop if we run into an explicitly specialized class template. */ |
13844 | if (!DECL_NAMESPACE_SCOPE_P (decl) | |
13845 | && DECL_CONTEXT (decl) | |
13846 | && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl))) | |
13847 | break; | |
13848 | ||
13849 | decl = DECL_TI_TEMPLATE (decl); | |
13850 | } | |
36a117a5 MM |
13851 | |
13852 | return decl; | |
13853 | } | |
13854 | ||
916b63c3 MM |
13855 | /* Return the most specialized of the class template partial |
13856 | specializations of TMPL which can produce TYPE, a specialization of | |
13857 | TMPL. The value returned is actually a TREE_LIST; the TREE_TYPE is | |
13858 | a _TYPE node corresponding to the partial specialization, while the | |
13859 | TREE_PURPOSE is the set of template arguments that must be | |
13860 | substituted into the TREE_TYPE in order to generate TYPE. | |
13861 | ||
13862 | If the choice of partial specialization is ambiguous, a diagnostic | |
13863 | is issued, and the error_mark_node is returned. If there are no | |
13864 | partial specializations of TMPL matching TYPE, then NULL_TREE is | |
13865 | returned. */ | |
36a117a5 | 13866 | |
e9659ab0 | 13867 | static tree |
916b63c3 | 13868 | most_specialized_class (tree type, tree tmpl) |
36a117a5 MM |
13869 | { |
13870 | tree list = NULL_TREE; | |
13871 | tree t; | |
13872 | tree champ; | |
73aad9b9 | 13873 | int fate; |
916b63c3 MM |
13874 | bool ambiguous_p; |
13875 | tree args; | |
dc28490d | 13876 | tree outer_args = NULL_TREE; |
73aad9b9 | 13877 | |
36a117a5 | 13878 | tmpl = most_general_template (tmpl); |
916b63c3 | 13879 | args = CLASSTYPE_TI_ARGS (type); |
dc28490d JM |
13880 | |
13881 | /* For determining which partial specialization to use, only the | |
13882 | innermost args are interesting. */ | |
13883 | if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args)) | |
13884 | { | |
13885 | outer_args = strip_innermost_template_args (args, 1); | |
13886 | args = INNERMOST_TEMPLATE_ARGS (args); | |
13887 | } | |
13888 | ||
36a117a5 | 13889 | for (t = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); t; t = TREE_CHAIN (t)) |
73aad9b9 | 13890 | { |
916b63c3 MM |
13891 | tree partial_spec_args; |
13892 | tree spec_args; | |
dc28490d | 13893 | tree parms = TREE_VALUE (t); |
916b63c3 MM |
13894 | |
13895 | partial_spec_args = CLASSTYPE_TI_ARGS (TREE_TYPE (t)); | |
dc28490d JM |
13896 | if (outer_args) |
13897 | { | |
13898 | int i; | |
13899 | ||
fd452cef JM |
13900 | ++processing_template_decl; |
13901 | ||
dc28490d JM |
13902 | /* Discard the outer levels of args, and then substitute in the |
13903 | template args from the enclosing class. */ | |
13904 | partial_spec_args = INNERMOST_TEMPLATE_ARGS (partial_spec_args); | |
13905 | partial_spec_args = tsubst_template_args | |
13906 | (partial_spec_args, outer_args, tf_none, NULL_TREE); | |
13907 | ||
13908 | /* PARMS already refers to just the innermost parms, but the | |
13909 | template parms in partial_spec_args had their levels lowered | |
13910 | by tsubst, so we need to do the same for the parm list. We | |
13911 | can't just tsubst the TREE_VEC itself, as tsubst wants to | |
13912 | treat a TREE_VEC as an argument vector. */ | |
13913 | parms = copy_node (parms); | |
13914 | for (i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i) | |
13915 | TREE_VEC_ELT (parms, i) = | |
13916 | tsubst (TREE_VEC_ELT (parms, i), outer_args, tf_none, NULL_TREE); | |
fd452cef JM |
13917 | |
13918 | --processing_template_decl; | |
dc28490d JM |
13919 | } |
13920 | spec_args = get_class_bindings (parms, | |
3db45ab5 | 13921 | partial_spec_args, |
916b63c3 | 13922 | args); |
36a117a5 | 13923 | if (spec_args) |
73aad9b9 | 13924 | { |
dc28490d JM |
13925 | if (outer_args) |
13926 | spec_args = add_to_template_args (outer_args, spec_args); | |
916b63c3 | 13927 | list = tree_cons (spec_args, TREE_VALUE (t), list); |
73aad9b9 JM |
13928 | TREE_TYPE (list) = TREE_TYPE (t); |
13929 | } | |
13930 | } | |
13931 | ||
13932 | if (! list) | |
13933 | return NULL_TREE; | |
13934 | ||
916b63c3 | 13935 | ambiguous_p = false; |
73aad9b9 JM |
13936 | t = list; |
13937 | champ = t; | |
13938 | t = TREE_CHAIN (t); | |
13939 | for (; t; t = TREE_CHAIN (t)) | |
13940 | { | |
916b63c3 | 13941 | fate = more_specialized_class (champ, t); |
73aad9b9 JM |
13942 | if (fate == 1) |
13943 | ; | |
13944 | else | |
13945 | { | |
13946 | if (fate == 0) | |
13947 | { | |
13948 | t = TREE_CHAIN (t); | |
13949 | if (! t) | |
916b63c3 MM |
13950 | { |
13951 | ambiguous_p = true; | |
13952 | break; | |
13953 | } | |
73aad9b9 JM |
13954 | } |
13955 | champ = t; | |
13956 | } | |
13957 | } | |
13958 | ||
916b63c3 MM |
13959 | if (!ambiguous_p) |
13960 | for (t = list; t && t != champ; t = TREE_CHAIN (t)) | |
13961 | { | |
13962 | fate = more_specialized_class (champ, t); | |
13963 | if (fate != 1) | |
13964 | { | |
13965 | ambiguous_p = true; | |
13966 | break; | |
13967 | } | |
13968 | } | |
13969 | ||
13970 | if (ambiguous_p) | |
73aad9b9 | 13971 | { |
916b63c3 MM |
13972 | const char *str = "candidates are:"; |
13973 | error ("ambiguous class template instantiation for %q#T", type); | |
13974 | for (t = list; t; t = TREE_CHAIN (t)) | |
13975 | { | |
13976 | error ("%s %+#T", str, TREE_TYPE (t)); | |
13977 | str = " "; | |
13978 | } | |
13979 | return error_mark_node; | |
73aad9b9 JM |
13980 | } |
13981 | ||
13982 | return champ; | |
13983 | } | |
13984 | ||
eb8845be | 13985 | /* Explicitly instantiate DECL. */ |
e92cc029 | 13986 | |
8d08fdba | 13987 | void |
eb8845be | 13988 | do_decl_instantiation (tree decl, tree storage) |
8d08fdba | 13989 | { |
8d08fdba | 13990 | tree result = NULL_TREE; |
faae18ab | 13991 | int extern_p = 0; |
e8abc66f | 13992 | |
ad47b891 | 13993 | if (!decl || decl == error_mark_node) |
dc957d14 | 13994 | /* An error occurred, for which grokdeclarator has already issued |
3fa56191 MM |
13995 | an appropriate message. */ |
13996 | return; | |
13997 | else if (! DECL_LANG_SPECIFIC (decl)) | |
ec255269 | 13998 | { |
0f51ccfc | 13999 | error ("explicit instantiation of non-template %q#D", decl); |
ec255269 MS |
14000 | return; |
14001 | } | |
03d0f4af | 14002 | else if (TREE_CODE (decl) == VAR_DECL) |
6633d636 | 14003 | { |
03d0f4af MM |
14004 | /* There is an asymmetry here in the way VAR_DECLs and |
14005 | FUNCTION_DECLs are handled by grokdeclarator. In the case of | |
14006 | the latter, the DECL we get back will be marked as a | |
14007 | template instantiation, and the appropriate | |
14008 | DECL_TEMPLATE_INFO will be set up. This does not happen for | |
14009 | VAR_DECLs so we do the lookup here. Probably, grokdeclarator | |
14010 | should handle VAR_DECLs as it currently handles | |
14011 | FUNCTION_DECLs. */ | |
8259e4f5 PC |
14012 | if (!DECL_CLASS_SCOPE_P (decl)) |
14013 | { | |
14014 | error ("%qD is not a static data member of a class template", decl); | |
14015 | return; | |
14016 | } | |
86ac0575 | 14017 | result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false); |
e0860732 | 14018 | if (!result || TREE_CODE (result) != VAR_DECL) |
03d0f4af | 14019 | { |
0f51ccfc | 14020 | error ("no matching template for %qD found", decl); |
03d0f4af MM |
14021 | return; |
14022 | } | |
8ea6dfae SM |
14023 | if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl))) |
14024 | { | |
14025 | error ("type %qT for explicit instantiation %qD does not match " | |
14026 | "declared type %qT", TREE_TYPE (result), decl, | |
14027 | TREE_TYPE (decl)); | |
14028 | return; | |
14029 | } | |
6633d636 MS |
14030 | } |
14031 | else if (TREE_CODE (decl) != FUNCTION_DECL) | |
14032 | { | |
0f51ccfc | 14033 | error ("explicit instantiation of %q#D", decl); |
6633d636 MS |
14034 | return; |
14035 | } | |
03d0f4af MM |
14036 | else |
14037 | result = decl; | |
672476cb | 14038 | |
03d0f4af | 14039 | /* Check for various error cases. Note that if the explicit |
0e339752 | 14040 | instantiation is valid the RESULT will currently be marked as an |
03d0f4af MM |
14041 | *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set |
14042 | until we get here. */ | |
14043 | ||
14044 | if (DECL_TEMPLATE_SPECIALIZATION (result)) | |
672476cb | 14045 | { |
07782718 KL |
14046 | /* DR 259 [temp.spec]. |
14047 | ||
14048 | Both an explicit instantiation and a declaration of an explicit | |
14049 | specialization shall not appear in a program unless the explicit | |
14050 | instantiation follows a declaration of the explicit specialization. | |
03d0f4af | 14051 | |
07782718 KL |
14052 | For a given set of template parameters, if an explicit |
14053 | instantiation of a template appears after a declaration of an | |
14054 | explicit specialization for that template, the explicit | |
14055 | instantiation has no effect. */ | |
672476cb MM |
14056 | return; |
14057 | } | |
03d0f4af MM |
14058 | else if (DECL_EXPLICIT_INSTANTIATION (result)) |
14059 | { | |
14060 | /* [temp.spec] | |
98c1c668 | 14061 | |
03d0f4af | 14062 | No program shall explicitly instantiate any template more |
c8094d83 | 14063 | than once. |
03d0f4af | 14064 | |
4684cd27 MM |
14065 | We check DECL_NOT_REALLY_EXTERN so as not to complain when |
14066 | the first instantiation was `extern' and the second is not, | |
14067 | and EXTERN_P for the opposite case. */ | |
14068 | if (DECL_NOT_REALLY_EXTERN (result) && !extern_p) | |
0f51ccfc | 14069 | pedwarn ("duplicate explicit instantiation of %q#D", result); |
4684cd27 MM |
14070 | /* If an "extern" explicit instantiation follows an ordinary |
14071 | explicit instantiation, the template is instantiated. */ | |
14072 | if (extern_p) | |
03d0f4af MM |
14073 | return; |
14074 | } | |
14075 | else if (!DECL_IMPLICIT_INSTANTIATION (result)) | |
faae18ab | 14076 | { |
0f51ccfc | 14077 | error ("no matching template for %qD found", result); |
faae18ab MS |
14078 | return; |
14079 | } | |
03d0f4af | 14080 | else if (!DECL_TEMPLATE_INFO (result)) |
6633d636 | 14081 | { |
0f51ccfc | 14082 | pedwarn ("explicit instantiation of non-template %q#D", result); |
6633d636 MS |
14083 | return; |
14084 | } | |
14085 | ||
f0e01782 | 14086 | if (storage == NULL_TREE) |
00595019 | 14087 | ; |
faae18ab | 14088 | else if (storage == ridpointers[(int) RID_EXTERN]) |
03d0f4af | 14089 | { |
c02f5e29 | 14090 | if (pedantic && !in_system_header) |
0f51ccfc | 14091 | pedwarn ("ISO C++ forbids the use of %<extern%> on explicit " |
0cbd7506 | 14092 | "instantiations"); |
03d0f4af MM |
14093 | extern_p = 1; |
14094 | } | |
f0e01782 | 14095 | else |
0f51ccfc | 14096 | error ("storage class %qD applied to template instantiation", storage); |
3db45ab5 | 14097 | |
9c12301f | 14098 | check_explicit_instantiation_namespace (result); |
5566b478 | 14099 | mark_decl_instantiated (result, extern_p); |
c91a56d2 | 14100 | if (! extern_p) |
3db45ab5 | 14101 | instantiate_decl (result, /*defer_ok=*/1, |
eba839f9 | 14102 | /*expl_inst_class_mem_p=*/false); |
7177d104 MS |
14103 | } |
14104 | ||
b5791fdc | 14105 | static void |
3a978d72 | 14106 | mark_class_instantiated (tree t, int extern_p) |
faae18ab MS |
14107 | { |
14108 | SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t); | |
eb773359 JM |
14109 | SET_CLASSTYPE_INTERFACE_KNOWN (t); |
14110 | CLASSTYPE_INTERFACE_ONLY (t) = extern_p; | |
faae18ab MS |
14111 | TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p; |
14112 | if (! extern_p) | |
14113 | { | |
14114 | CLASSTYPE_DEBUG_REQUESTED (t) = 1; | |
14115 | rest_of_type_compilation (t, 1); | |
14116 | } | |
c8094d83 | 14117 | } |
e8abc66f | 14118 | |
5e0c54e5 | 14119 | /* Called from do_type_instantiation through binding_table_foreach to |
9bcb9aae | 14120 | do recursive instantiation for the type bound in ENTRY. */ |
5e0c54e5 GDR |
14121 | static void |
14122 | bt_instantiate_type_proc (binding_entry entry, void *data) | |
14123 | { | |
14124 | tree storage = *(tree *) data; | |
14125 | ||
14126 | if (IS_AGGR_TYPE (entry->type) | |
14127 | && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type))) | |
14128 | do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0); | |
14129 | } | |
14130 | ||
415c974c MA |
14131 | /* Called from do_type_instantiation to instantiate a member |
14132 | (a member function or a static member variable) of an | |
03fd3f84 | 14133 | explicitly instantiated class template. */ |
415c974c MA |
14134 | static void |
14135 | instantiate_class_member (tree decl, int extern_p) | |
14136 | { | |
14137 | mark_decl_instantiated (decl, extern_p); | |
415c974c | 14138 | if (! extern_p) |
3db45ab5 | 14139 | instantiate_decl (decl, /*defer_ok=*/1, |
eba839f9 | 14140 | /*expl_inst_class_mem_p=*/true); |
415c974c MA |
14141 | } |
14142 | ||
a1bcc528 JM |
14143 | /* Perform an explicit instantiation of template class T. STORAGE, if |
14144 | non-null, is the RID for extern, inline or static. COMPLAIN is | |
838dfd8a | 14145 | nonzero if this is called from the parser, zero if called recursively, |
a1bcc528 | 14146 | since the standard is unclear (as detailed below). */ |
c8094d83 | 14147 | |
7177d104 | 14148 | void |
3a978d72 | 14149 | do_type_instantiation (tree t, tree storage, tsubst_flags_t complain) |
7177d104 | 14150 | { |
e8abc66f MS |
14151 | int extern_p = 0; |
14152 | int nomem_p = 0; | |
5566b478 | 14153 | int static_p = 0; |
4746cf84 | 14154 | int previous_instantiation_extern_p = 0; |
5566b478 | 14155 | |
ca79f85d JM |
14156 | if (TREE_CODE (t) == TYPE_DECL) |
14157 | t = TREE_TYPE (t); | |
14158 | ||
7ddedda4 | 14159 | if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t)) |
ca79f85d | 14160 | { |
0f51ccfc | 14161 | error ("explicit instantiation of non-template type %qT", t); |
ca79f85d JM |
14162 | return; |
14163 | } | |
14164 | ||
5566b478 | 14165 | complete_type (t); |
7177d104 | 14166 | |
d0f062fb | 14167 | if (!COMPLETE_TYPE_P (t)) |
f0e01782 | 14168 | { |
c2ea3a40 | 14169 | if (complain & tf_error) |
0f51ccfc | 14170 | error ("explicit instantiation of %q#T before definition of template", |
0cbd7506 | 14171 | t); |
f0e01782 MS |
14172 | return; |
14173 | } | |
14174 | ||
03d0f4af | 14175 | if (storage != NULL_TREE) |
f0e01782 | 14176 | { |
c02f5e29 | 14177 | if (pedantic && !in_system_header) |
c8094d83 | 14178 | pedwarn("ISO C++ forbids the use of %qE on explicit instantiations", |
0cbd7506 | 14179 | storage); |
03d0f4af MM |
14180 | |
14181 | if (storage == ridpointers[(int) RID_INLINE]) | |
14182 | nomem_p = 1; | |
14183 | else if (storage == ridpointers[(int) RID_EXTERN]) | |
14184 | extern_p = 1; | |
14185 | else if (storage == ridpointers[(int) RID_STATIC]) | |
14186 | static_p = 1; | |
14187 | else | |
14188 | { | |
0f51ccfc | 14189 | error ("storage class %qD applied to template instantiation", |
0cbd7506 | 14190 | storage); |
03d0f4af MM |
14191 | extern_p = 0; |
14192 | } | |
f0e01782 MS |
14193 | } |
14194 | ||
370af2d5 | 14195 | if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t)) |
03d0f4af | 14196 | { |
07782718 | 14197 | /* DR 259 [temp.spec]. |
a292b002 | 14198 | |
07782718 KL |
14199 | Both an explicit instantiation and a declaration of an explicit |
14200 | specialization shall not appear in a program unless the explicit | |
14201 | instantiation follows a declaration of the explicit specialization. | |
14202 | ||
14203 | For a given set of template parameters, if an explicit | |
14204 | instantiation of a template appears after a declaration of an | |
14205 | explicit specialization for that template, the explicit | |
14206 | instantiation has no effect. */ | |
03d0f4af MM |
14207 | return; |
14208 | } | |
14209 | else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t)) | |
44a8d0b3 | 14210 | { |
03d0f4af MM |
14211 | /* [temp.spec] |
14212 | ||
14213 | No program shall explicitly instantiate any template more | |
c8094d83 | 14214 | than once. |
03d0f4af | 14215 | |
0cbd7506 | 14216 | If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit |
4746cf84 | 14217 | instantiation was `extern'. If EXTERN_P then the second is. |
4684cd27 | 14218 | These cases are OK. */ |
4746cf84 MA |
14219 | previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t); |
14220 | ||
14221 | if (!previous_instantiation_extern_p && !extern_p | |
c2ea3a40 | 14222 | && (complain & tf_error)) |
0f51ccfc | 14223 | pedwarn ("duplicate explicit instantiation of %q#T", t); |
c8094d83 | 14224 | |
03d0f4af MM |
14225 | /* If we've already instantiated the template, just return now. */ |
14226 | if (!CLASSTYPE_INTERFACE_ONLY (t)) | |
14227 | return; | |
44a8d0b3 | 14228 | } |
e8abc66f | 14229 | |
9c12301f | 14230 | check_explicit_instantiation_namespace (TYPE_NAME (t)); |
03d0f4af | 14231 | mark_class_instantiated (t, extern_p); |
03d0f4af | 14232 | |
e8abc66f MS |
14233 | if (nomem_p) |
14234 | return; | |
14235 | ||
7177d104 | 14236 | { |
db5ae43f | 14237 | tree tmp; |
5566b478 | 14238 | |
03d0f4af MM |
14239 | /* In contrast to implicit instantiation, where only the |
14240 | declarations, and not the definitions, of members are | |
14241 | instantiated, we have here: | |
14242 | ||
0cbd7506 | 14243 | [temp.explicit] |
03d0f4af MM |
14244 | |
14245 | The explicit instantiation of a class template specialization | |
14246 | implies the instantiation of all of its members not | |
14247 | previously explicitly specialized in the translation unit | |
c8094d83 | 14248 | containing the explicit instantiation. |
03d0f4af MM |
14249 | |
14250 | Of course, we can't instantiate member template classes, since | |
14251 | we don't have any arguments for them. Note that the standard | |
dc957d14 | 14252 | is unclear on whether the instantiation of the members are |
415c974c | 14253 | *explicit* instantiations or not. However, the most natural |
03fd3f84 | 14254 | interpretation is that it should be an explicit instantiation. */ |
03d0f4af | 14255 | |
5566b478 MS |
14256 | if (! static_p) |
14257 | for (tmp = TYPE_METHODS (t); tmp; tmp = TREE_CHAIN (tmp)) | |
a7d87521 | 14258 | if (TREE_CODE (tmp) == FUNCTION_DECL |
1f109f0f | 14259 | && DECL_TEMPLATE_INSTANTIATION (tmp)) |
415c974c | 14260 | instantiate_class_member (tmp, extern_p); |
5566b478 MS |
14261 | |
14262 | for (tmp = TYPE_FIELDS (t); tmp; tmp = TREE_CHAIN (tmp)) | |
14263 | if (TREE_CODE (tmp) == VAR_DECL && DECL_TEMPLATE_INSTANTIATION (tmp)) | |
415c974c | 14264 | instantiate_class_member (tmp, extern_p); |
7177d104 | 14265 | |
5e0c54e5 GDR |
14266 | if (CLASSTYPE_NESTED_UTDS (t)) |
14267 | binding_table_foreach (CLASSTYPE_NESTED_UTDS (t), | |
0cbd7506 | 14268 | bt_instantiate_type_proc, &storage); |
a292b002 | 14269 | } |
8d08fdba | 14270 | } |
a28e3c7f | 14271 | |
36a117a5 MM |
14272 | /* Given a function DECL, which is a specialization of TMPL, modify |
14273 | DECL to be a re-instantiation of TMPL with the same template | |
14274 | arguments. TMPL should be the template into which tsubst'ing | |
14275 | should occur for DECL, not the most general template. | |
830bfa74 MM |
14276 | |
14277 | One reason for doing this is a scenario like this: | |
14278 | ||
14279 | template <class T> | |
14280 | void f(const T&, int i); | |
14281 | ||
14282 | void g() { f(3, 7); } | |
14283 | ||
14284 | template <class T> | |
14285 | void f(const T& t, const int i) { } | |
14286 | ||
14287 | Note that when the template is first instantiated, with | |
14288 | instantiate_template, the resulting DECL will have no name for the | |
14289 | first parameter, and the wrong type for the second. So, when we go | |
14290 | to instantiate the DECL, we regenerate it. */ | |
14291 | ||
e9659ab0 | 14292 | static void |
3a978d72 | 14293 | regenerate_decl_from_template (tree decl, tree tmpl) |
830bfa74 | 14294 | { |
f9a7ae04 MM |
14295 | /* The arguments used to instantiate DECL, from the most general |
14296 | template. */ | |
830bfa74 | 14297 | tree args; |
830bfa74 | 14298 | tree code_pattern; |
830bfa74 MM |
14299 | |
14300 | args = DECL_TI_ARGS (decl); | |
14301 | code_pattern = DECL_TEMPLATE_RESULT (tmpl); | |
14302 | ||
2b907f5c KL |
14303 | /* Make sure that we can see identifiers, and compute access |
14304 | correctly. */ | |
14305 | push_access_scope (decl); | |
14306 | ||
c7222c02 MM |
14307 | if (TREE_CODE (decl) == FUNCTION_DECL) |
14308 | { | |
14309 | tree decl_parm; | |
14310 | tree pattern_parm; | |
14311 | tree specs; | |
14312 | int args_depth; | |
14313 | int parms_depth; | |
c8094d83 | 14314 | |
c7222c02 | 14315 | args_depth = TMPL_ARGS_DEPTH (args); |
c8094d83 | 14316 | parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)); |
c7222c02 MM |
14317 | if (args_depth > parms_depth) |
14318 | args = get_innermost_template_args (args, parms_depth); | |
14319 | ||
14320 | specs = tsubst_exception_specification (TREE_TYPE (code_pattern), | |
14321 | args, tf_error, NULL_TREE); | |
14322 | if (specs) | |
14323 | TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl), | |
14324 | specs); | |
14325 | ||
14326 | /* Merge parameter declarations. */ | |
c8094d83 | 14327 | decl_parm = skip_artificial_parms_for (decl, |
c7222c02 | 14328 | DECL_ARGUMENTS (decl)); |
c8094d83 | 14329 | pattern_parm |
c7222c02 MM |
14330 | = skip_artificial_parms_for (code_pattern, |
14331 | DECL_ARGUMENTS (code_pattern)); | |
5d80a306 | 14332 | while (decl_parm && !FUNCTION_PARAMETER_PACK_P (pattern_parm)) |
c7222c02 MM |
14333 | { |
14334 | tree parm_type; | |
b17bba6d | 14335 | tree attributes; |
5d80a306 | 14336 | |
c7222c02 MM |
14337 | if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm)) |
14338 | DECL_NAME (decl_parm) = DECL_NAME (pattern_parm); | |
14339 | parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error, | |
14340 | NULL_TREE); | |
02bab9db | 14341 | parm_type = type_decays_to (parm_type); |
c7222c02 MM |
14342 | if (!same_type_p (TREE_TYPE (decl_parm), parm_type)) |
14343 | TREE_TYPE (decl_parm) = parm_type; | |
b17bba6d MM |
14344 | attributes = DECL_ATTRIBUTES (pattern_parm); |
14345 | if (DECL_ATTRIBUTES (decl_parm) != attributes) | |
14346 | { | |
14347 | DECL_ATTRIBUTES (decl_parm) = attributes; | |
14348 | cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0); | |
14349 | } | |
c7222c02 MM |
14350 | decl_parm = TREE_CHAIN (decl_parm); |
14351 | pattern_parm = TREE_CHAIN (pattern_parm); | |
14352 | } | |
5d80a306 DG |
14353 | /* Merge any parameters that match with the function parameter |
14354 | pack. */ | |
14355 | if (pattern_parm && FUNCTION_PARAMETER_PACK_P (pattern_parm)) | |
14356 | { | |
14357 | int i, len; | |
14358 | tree expanded_types; | |
14359 | /* Expand the TYPE_PACK_EXPANSION that provides the types for | |
14360 | the parameters in this function parameter pack. */ | |
14361 | expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm), | |
14362 | args, tf_error, NULL_TREE); | |
14363 | len = TREE_VEC_LENGTH (expanded_types); | |
14364 | for (i = 0; i < len; i++) | |
14365 | { | |
14366 | tree parm_type; | |
14367 | tree attributes; | |
14368 | ||
14369 | if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm)) | |
14370 | /* Rename the parameter to include the index. */ | |
14371 | DECL_NAME (decl_parm) = | |
14372 | make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i); | |
14373 | parm_type = TREE_VEC_ELT (expanded_types, i); | |
14374 | parm_type = type_decays_to (parm_type); | |
14375 | if (!same_type_p (TREE_TYPE (decl_parm), parm_type)) | |
14376 | TREE_TYPE (decl_parm) = parm_type; | |
14377 | attributes = DECL_ATTRIBUTES (pattern_parm); | |
14378 | if (DECL_ATTRIBUTES (decl_parm) != attributes) | |
14379 | { | |
14380 | DECL_ATTRIBUTES (decl_parm) = attributes; | |
14381 | cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0); | |
14382 | } | |
14383 | decl_parm = TREE_CHAIN (decl_parm); | |
14384 | } | |
14385 | } | |
c7222c02 MM |
14386 | /* Merge additional specifiers from the CODE_PATTERN. */ |
14387 | if (DECL_DECLARED_INLINE_P (code_pattern) | |
14388 | && !DECL_DECLARED_INLINE_P (decl)) | |
14389 | DECL_DECLARED_INLINE_P (decl) = 1; | |
14390 | if (DECL_INLINE (code_pattern) && !DECL_INLINE (decl)) | |
14391 | DECL_INLINE (decl) = 1; | |
14392 | } | |
14393 | else if (TREE_CODE (decl) == VAR_DECL) | |
b794e321 MM |
14394 | DECL_INITIAL (decl) = |
14395 | tsubst_expr (DECL_INITIAL (code_pattern), args, | |
015c2c66 MM |
14396 | tf_error, DECL_TI_TEMPLATE (decl), |
14397 | /*integral_constant_expression_p=*/false); | |
c7222c02 MM |
14398 | else |
14399 | gcc_unreachable (); | |
36a117a5 | 14400 | |
2b59fc25 | 14401 | pop_access_scope (decl); |
830bfa74 MM |
14402 | } |
14403 | ||
a723baf1 MM |
14404 | /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be |
14405 | substituted to get DECL. */ | |
14406 | ||
d58b7c2d | 14407 | tree |
a723baf1 MM |
14408 | template_for_substitution (tree decl) |
14409 | { | |
14410 | tree tmpl = DECL_TI_TEMPLATE (decl); | |
14411 | ||
14412 | /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern | |
14413 | for the instantiation. This is not always the most general | |
14414 | template. Consider, for example: | |
14415 | ||
0cbd7506 | 14416 | template <class T> |
a723baf1 | 14417 | struct S { template <class U> void f(); |
0cbd7506 | 14418 | template <> void f<int>(); }; |
a723baf1 MM |
14419 | |
14420 | and an instantiation of S<double>::f<int>. We want TD to be the | |
14421 | specialization S<T>::f<int>, not the more general S<T>::f<U>. */ | |
14422 | while (/* An instantiation cannot have a definition, so we need a | |
14423 | more general template. */ | |
14424 | DECL_TEMPLATE_INSTANTIATION (tmpl) | |
14425 | /* We must also deal with friend templates. Given: | |
14426 | ||
c8094d83 | 14427 | template <class T> struct S { |
a723baf1 MM |
14428 | template <class U> friend void f() {}; |
14429 | }; | |
14430 | ||
14431 | S<int>::f<U> say, is not an instantiation of S<T>::f<U>, | |
14432 | so far as the language is concerned, but that's still | |
14433 | where we get the pattern for the instantiation from. On | |
14434 | other hand, if the definition comes outside the class, say: | |
14435 | ||
c8094d83 | 14436 | template <class T> struct S { |
a723baf1 MM |
14437 | template <class U> friend void f(); |
14438 | }; | |
14439 | template <class U> friend void f() {} | |
14440 | ||
14441 | we don't need to look any further. That's what the check for | |
14442 | DECL_INITIAL is for. */ | |
14443 | || (TREE_CODE (decl) == FUNCTION_DECL | |
14444 | && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl) | |
14445 | && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl)))) | |
14446 | { | |
14447 | /* The present template, TD, should not be a definition. If it | |
14448 | were a definition, we should be using it! Note that we | |
14449 | cannot restructure the loop to just keep going until we find | |
14450 | a template with a definition, since that might go too far if | |
14451 | a specialization was declared, but not defined. */ | |
50bc768d NS |
14452 | gcc_assert (TREE_CODE (decl) != VAR_DECL |
14453 | || DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (tmpl))); | |
c8094d83 | 14454 | |
a723baf1 MM |
14455 | /* Fetch the more general template. */ |
14456 | tmpl = DECL_TI_TEMPLATE (tmpl); | |
14457 | } | |
14458 | ||
14459 | return tmpl; | |
14460 | } | |
14461 | ||
16d53b64 | 14462 | /* Produce the definition of D, a _DECL generated from a template. If |
838dfd8a | 14463 | DEFER_OK is nonzero, then we don't have to actually do the |
415c974c MA |
14464 | instantiation now; we just have to do it sometime. Normally it is |
14465 | an error if this is an explicit instantiation but D is undefined. | |
eba839f9 MM |
14466 | EXPL_INST_CLASS_MEM_P is true iff D is a member of an |
14467 | explicitly instantiated class template. */ | |
f84b4be9 | 14468 | |
a28e3c7f | 14469 | tree |
3db45ab5 | 14470 | instantiate_decl (tree d, int defer_ok, |
eba839f9 | 14471 | bool expl_inst_class_mem_p) |
a28e3c7f | 14472 | { |
36a117a5 | 14473 | tree tmpl = DECL_TI_TEMPLATE (d); |
65f8b0fb MM |
14474 | tree gen_args; |
14475 | tree args; | |
830bfa74 | 14476 | tree td; |
36a117a5 MM |
14477 | tree code_pattern; |
14478 | tree spec; | |
14479 | tree gen_tmpl; | |
b794e321 | 14480 | bool pattern_defined; |
31a714f6 | 14481 | int need_push; |
82a98427 | 14482 | location_t saved_loc = input_location; |
912291ca | 14483 | int saved_in_system_header = in_system_header; |
f7e4e484 | 14484 | bool external_p; |
c8094d83 | 14485 | |
36a117a5 MM |
14486 | /* This function should only be used to instantiate templates for |
14487 | functions and static member variables. */ | |
50bc768d NS |
14488 | gcc_assert (TREE_CODE (d) == FUNCTION_DECL |
14489 | || TREE_CODE (d) == VAR_DECL); | |
36a117a5 | 14490 | |
cec24319 MM |
14491 | /* Variables are never deferred; if instantiation is required, they |
14492 | are instantiated right away. That allows for better code in the | |
14493 | case that an expression refers to the value of the variable -- | |
14494 | if the variable has a constant value the referring expression can | |
14495 | take advantage of that fact. */ | |
14496 | if (TREE_CODE (d) == VAR_DECL) | |
14497 | defer_ok = 0; | |
14498 | ||
db9b2174 MM |
14499 | /* Don't instantiate cloned functions. Instead, instantiate the |
14500 | functions they cloned. */ | |
14501 | if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d)) | |
14502 | d = DECL_CLONED_FUNCTION (d); | |
14503 | ||
fbf1c34b | 14504 | if (DECL_TEMPLATE_INSTANTIATED (d)) |
03d0f4af | 14505 | /* D has already been instantiated. It might seem reasonable to |
dc957d14 | 14506 | check whether or not D is an explicit instantiation, and, if so, |
03d0f4af MM |
14507 | stop here. But when an explicit instantiation is deferred |
14508 | until the end of the compilation, DECL_EXPLICIT_INSTANTIATION | |
14509 | is set, even though we still need to do the instantiation. */ | |
36a117a5 MM |
14510 | return d; |
14511 | ||
14512 | /* If we already have a specialization of this declaration, then | |
14513 | there's no reason to instantiate it. Note that | |
14514 | retrieve_specialization gives us both instantiations and | |
14515 | specializations, so we must explicitly check | |
14516 | DECL_TEMPLATE_SPECIALIZATION. */ | |
14517 | gen_tmpl = most_general_template (tmpl); | |
65f8b0fb | 14518 | gen_args = DECL_TI_ARGS (d); |
c7222c02 MM |
14519 | spec = retrieve_specialization (gen_tmpl, gen_args, |
14520 | /*class_specializations_p=*/false); | |
36a117a5 MM |
14521 | if (spec != NULL_TREE && DECL_TEMPLATE_SPECIALIZATION (spec)) |
14522 | return spec; | |
14523 | ||
14524 | /* This needs to happen before any tsubsting. */ | |
14525 | if (! push_tinst_level (d)) | |
14526 | return d; | |
14527 | ||
297a5329 JM |
14528 | timevar_push (TV_PARSE); |
14529 | ||
4d85e00e | 14530 | /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern |
a723baf1 MM |
14531 | for the instantiation. */ |
14532 | td = template_for_substitution (d); | |
fee23f54 | 14533 | code_pattern = DECL_TEMPLATE_RESULT (td); |
27bb8339 | 14534 | |
2d22db1f | 14535 | /* We should never be trying to instantiate a member of a class |
3db45ab5 | 14536 | template or partial specialization. */ |
2d22db1f | 14537 | gcc_assert (d != code_pattern); |
3db45ab5 | 14538 | |
76d3baad KL |
14539 | if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d)) |
14540 | || DECL_TEMPLATE_SPECIALIZATION (td)) | |
649fc72d NS |
14541 | /* In the case of a friend template whose definition is provided |
14542 | outside the class, we may have too many arguments. Drop the | |
76d3baad | 14543 | ones we don't need. The same is true for specializations. */ |
649fc72d NS |
14544 | args = get_innermost_template_args |
14545 | (gen_args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td))); | |
14546 | else | |
14547 | args = gen_args; | |
65f8b0fb | 14548 | |
5566b478 | 14549 | if (TREE_CODE (d) == FUNCTION_DECL) |
39c76b4f | 14550 | pattern_defined = (DECL_SAVED_TREE (code_pattern) != NULL_TREE); |
5566b478 | 14551 | else |
36a117a5 | 14552 | pattern_defined = ! DECL_IN_AGGR_P (code_pattern); |
b794e321 MM |
14553 | |
14554 | /* We may be in the middle of deferred access check. Disable it now. */ | |
14555 | push_deferring_access_checks (dk_no_deferred); | |
14556 | ||
4684cd27 MM |
14557 | /* Unless an explicit instantiation directive has already determined |
14558 | the linkage of D, remember that a definition is available for | |
14559 | this entity. */ | |
c8094d83 | 14560 | if (pattern_defined |
4684cd27 MM |
14561 | && !DECL_INTERFACE_KNOWN (d) |
14562 | && !DECL_NOT_REALLY_EXTERN (d)) | |
14563 | mark_definable (d); | |
de22184b | 14564 | |
f31686a3 | 14565 | input_location = DECL_SOURCE_LOCATION (d); |
912291ca | 14566 | in_system_header = DECL_IN_SYSTEM_HEADER (d); |
de22184b | 14567 | |
eba839f9 MM |
14568 | /* If D is a member of an explicitly instantiated class template, |
14569 | and no definition is available, treat it like an implicit | |
3db45ab5 MS |
14570 | instantiation. */ |
14571 | if (!pattern_defined && expl_inst_class_mem_p | |
14572 | && DECL_EXPLICIT_INSTANTIATION (d)) | |
5566b478 | 14573 | { |
4684cd27 | 14574 | DECL_NOT_REALLY_EXTERN (d) = 0; |
eba839f9 | 14575 | DECL_INTERFACE_KNOWN (d) = 0; |
4684cd27 | 14576 | SET_DECL_IMPLICIT_INSTANTIATION (d); |
5566b478 MS |
14577 | } |
14578 | ||
4f2b0fb2 NS |
14579 | if (!defer_ok) |
14580 | { | |
14581 | /* Recheck the substitutions to obtain any warning messages | |
14582 | about ignoring cv qualifiers. */ | |
14583 | tree gen = DECL_TEMPLATE_RESULT (gen_tmpl); | |
14584 | tree type = TREE_TYPE (gen); | |
14585 | ||
0e902d98 | 14586 | /* Make sure that we can see identifiers, and compute access |
2b59fc25 KL |
14587 | correctly. D is already the target FUNCTION_DECL with the |
14588 | right context. */ | |
14589 | push_access_scope (d); | |
0e902d98 | 14590 | |
4f2b0fb2 NS |
14591 | if (TREE_CODE (gen) == FUNCTION_DECL) |
14592 | { | |
23fca1f5 | 14593 | tsubst (DECL_ARGUMENTS (gen), gen_args, tf_warning_or_error, d); |
5d80a306 DG |
14594 | tsubst_exception_specification (type, gen_args, tf_warning_or_error, |
14595 | d); | |
4f2b0fb2 NS |
14596 | /* Don't simply tsubst the function type, as that will give |
14597 | duplicate warnings about poor parameter qualifications. | |
14598 | The function arguments are the same as the decl_arguments | |
c6002625 | 14599 | without the top level cv qualifiers. */ |
4f2b0fb2 NS |
14600 | type = TREE_TYPE (type); |
14601 | } | |
23fca1f5 | 14602 | tsubst (type, gen_args, tf_warning_or_error, d); |
0e902d98 | 14603 | |
2b59fc25 | 14604 | pop_access_scope (d); |
4f2b0fb2 | 14605 | } |
c8094d83 | 14606 | |
f7e4e484 MM |
14607 | /* Check to see whether we know that this template will be |
14608 | instantiated in some other file, as with "extern template" | |
14609 | extension. */ | |
14610 | external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d)); | |
14611 | /* In general, we do not instantiate such templates... */ | |
14612 | if (external_p | |
14613 | /* ... but we instantiate inline functions so that we can inline | |
14614 | them and ... */ | |
14615 | && ! (TREE_CODE (d) == FUNCTION_DECL && DECL_INLINE (d)) | |
14616 | /* ... we instantiate static data members whose values are | |
14617 | needed in integral constant expressions. */ | |
3db45ab5 | 14618 | && ! (TREE_CODE (d) == VAR_DECL |
f7e4e484 | 14619 | && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (d))) |
930cd796 | 14620 | goto out; |
16d53b64 | 14621 | /* Defer all other templates, unless we have been explicitly |
f7e4e484 MM |
14622 | forbidden from doing so. */ |
14623 | if (/* If there is no definition, we cannot instantiate the | |
14624 | template. */ | |
3db45ab5 | 14625 | ! pattern_defined |
f7e4e484 MM |
14626 | /* If it's OK to postpone instantiation, do so. */ |
14627 | || defer_ok | |
14628 | /* If this is a static data member that will be defined | |
14629 | elsewhere, we don't want to instantiate the entire data | |
14630 | member, but we do want to instantiate the initializer so that | |
14631 | we can substitute that elsewhere. */ | |
14632 | || (external_p && TREE_CODE (d) == VAR_DECL)) | |
16d53b64 | 14633 | { |
b794e321 MM |
14634 | /* The definition of the static data member is now required so |
14635 | we must substitute the initializer. */ | |
14636 | if (TREE_CODE (d) == VAR_DECL | |
3db45ab5 | 14637 | && !DECL_INITIAL (d) |
b794e321 MM |
14638 | && DECL_INITIAL (code_pattern)) |
14639 | { | |
4bff36d3 MM |
14640 | tree ns; |
14641 | tree init; | |
14642 | ||
14643 | ns = decl_namespace_context (d); | |
27a725e2 | 14644 | push_nested_namespace (ns); |
b794e321 | 14645 | push_nested_class (DECL_CONTEXT (d)); |
3db45ab5 | 14646 | init = tsubst_expr (DECL_INITIAL (code_pattern), |
4bff36d3 | 14647 | args, |
015c2c66 MM |
14648 | tf_warning_or_error, NULL_TREE, |
14649 | /*integral_constant_expression_p=*/false); | |
d174af6c MM |
14650 | cp_finish_decl (d, init, /*init_const_expr_p=*/false, |
14651 | /*asmspec_tree=*/NULL_TREE, | |
86414779 | 14652 | LOOKUP_ONLYCONVERTING); |
b794e321 | 14653 | pop_nested_class (); |
27a725e2 | 14654 | pop_nested_namespace (ns); |
b794e321 MM |
14655 | } |
14656 | ||
f7e4e484 MM |
14657 | /* We restore the source position here because it's used by |
14658 | add_pending_template. */ | |
82a98427 | 14659 | input_location = saved_loc; |
c27be9b9 | 14660 | |
c8094d83 | 14661 | if (at_eof && !pattern_defined |
03d0f4af MM |
14662 | && DECL_EXPLICIT_INSTANTIATION (d)) |
14663 | /* [temp.explicit] | |
14664 | ||
14665 | The definition of a non-exported function template, a | |
14666 | non-exported member function template, or a non-exported | |
14667 | member function or static data member of a class template | |
14668 | shall be present in every translation unit in which it is | |
14669 | explicitly instantiated. */ | |
33bd39a2 | 14670 | pedwarn |
0f51ccfc | 14671 | ("explicit instantiation of %qD but no definition available", d); |
03d0f4af | 14672 | |
f7e4e484 MM |
14673 | /* ??? Historically, we have instantiated inline functions, even |
14674 | when marked as "extern template". */ | |
14675 | if (!(external_p && TREE_CODE (d) == VAR_DECL)) | |
14676 | add_pending_template (d); | |
de22184b | 14677 | goto out; |
5566b478 | 14678 | } |
4684cd27 MM |
14679 | /* Tell the repository that D is available in this translation unit |
14680 | -- and see if it is supposed to be instantiated here. */ | |
14681 | if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d)) | |
14682 | { | |
14683 | /* In a PCH file, despite the fact that the repository hasn't | |
14684 | requested instantiation in the PCH it is still possible that | |
14685 | an instantiation will be required in a file that includes the | |
14686 | PCH. */ | |
14687 | if (pch_file) | |
14688 | add_pending_template (d); | |
14689 | /* Instantiate inline functions so that the inliner can do its | |
14690 | job, even though we'll not be emitting a copy of this | |
14691 | function. */ | |
c2a124b2 | 14692 | if (!(TREE_CODE (d) == FUNCTION_DECL |
c8094d83 | 14693 | && flag_inline_trees |
c2a124b2 | 14694 | && DECL_DECLARED_INLINE_P (d))) |
4684cd27 MM |
14695 | goto out; |
14696 | } | |
5566b478 | 14697 | |
6de9cd9a | 14698 | need_push = !cfun || !global_bindings_p (); |
31a714f6 MM |
14699 | if (need_push) |
14700 | push_to_top_level (); | |
414ea4aa | 14701 | |
66e0c440 KL |
14702 | /* Mark D as instantiated so that recursive calls to |
14703 | instantiate_decl do not try to instantiate it again. */ | |
14704 | DECL_TEMPLATE_INSTANTIATED (d) = 1; | |
14705 | ||
2b0a63a3 MM |
14706 | /* Regenerate the declaration in case the template has been modified |
14707 | by a subsequent redeclaration. */ | |
14708 | regenerate_decl_from_template (d, td); | |
4684cd27 | 14709 | |
120722ac | 14710 | /* We already set the file and line above. Reset them now in case |
6de9cd9a | 14711 | they changed as a result of calling regenerate_decl_from_template. */ |
f31686a3 | 14712 | input_location = DECL_SOURCE_LOCATION (d); |
5156628f | 14713 | |
5566b478 MS |
14714 | if (TREE_CODE (d) == VAR_DECL) |
14715 | { | |
e92fb501 MM |
14716 | tree init; |
14717 | ||
1d62c33e MM |
14718 | /* Clear out DECL_RTL; whatever was there before may not be right |
14719 | since we've reset the type of the declaration. */ | |
14720 | SET_DECL_RTL (d, NULL_RTX); | |
5566b478 | 14721 | DECL_IN_AGGR_P (d) = 0; |
ea56c40c | 14722 | |
e92fb501 MM |
14723 | /* The initializer is placed in DECL_INITIAL by |
14724 | regenerate_decl_from_template. Pull it out so that | |
14725 | finish_decl can process it. */ | |
14726 | init = DECL_INITIAL (d); | |
14727 | DECL_INITIAL (d) = NULL_TREE; | |
14728 | DECL_INITIALIZED_P (d) = 0; | |
14729 | ||
4684cd27 MM |
14730 | /* Clear DECL_EXTERNAL so that cp_finish_decl will process the |
14731 | initializer. That function will defer actual emission until | |
14732 | we have a chance to determine linkage. */ | |
14733 | DECL_EXTERNAL (d) = 0; | |
14734 | ||
73a8adb6 | 14735 | /* Enter the scope of D so that access-checking works correctly. */ |
4684cd27 | 14736 | push_nested_class (DECL_CONTEXT (d)); |
e92fb501 | 14737 | finish_decl (d, init, NULL_TREE); |
73a8adb6 | 14738 | pop_nested_class (); |
5566b478 MS |
14739 | } |
14740 | else if (TREE_CODE (d) == FUNCTION_DECL) | |
14741 | { | |
6bbf1598 | 14742 | htab_t saved_local_specializations; |
a723baf1 MM |
14743 | tree subst_decl; |
14744 | tree tmpl_parm; | |
14745 | tree spec_parm; | |
6bbf1598 MM |
14746 | |
14747 | /* Save away the current list, in case we are instantiating one | |
14748 | template from within the body of another. */ | |
14749 | saved_local_specializations = local_specializations; | |
14750 | ||
6dfbb909 | 14751 | /* Set up the list of local specializations. */ |
c8094d83 | 14752 | local_specializations = htab_create (37, |
69f794a7 | 14753 | hash_local_specialization, |
a723baf1 | 14754 | eq_local_specializations, |
6dfbb909 MM |
14755 | NULL); |
14756 | ||
558475f0 | 14757 | /* Set up context. */ |
058b15c1 | 14758 | start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED); |
5566b478 | 14759 | |
a723baf1 MM |
14760 | /* Create substitution entries for the parameters. */ |
14761 | subst_decl = DECL_TEMPLATE_RESULT (template_for_substitution (d)); | |
14762 | tmpl_parm = DECL_ARGUMENTS (subst_decl); | |
14763 | spec_parm = DECL_ARGUMENTS (d); | |
14764 | if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d)) | |
14765 | { | |
14766 | register_local_specialization (spec_parm, tmpl_parm); | |
14767 | spec_parm = skip_artificial_parms_for (d, spec_parm); | |
14768 | tmpl_parm = skip_artificial_parms_for (subst_decl, tmpl_parm); | |
14769 | } | |
5d80a306 | 14770 | while (tmpl_parm && !FUNCTION_PARAMETER_PACK_P (tmpl_parm)) |
a723baf1 MM |
14771 | { |
14772 | register_local_specialization (spec_parm, tmpl_parm); | |
14773 | tmpl_parm = TREE_CHAIN (tmpl_parm); | |
14774 | spec_parm = TREE_CHAIN (spec_parm); | |
14775 | } | |
5d80a306 DG |
14776 | if (tmpl_parm && FUNCTION_PARAMETER_PACK_P (tmpl_parm)) |
14777 | { | |
14778 | /* Collect all of the extra "packed" parameters into an | |
14779 | argument pack. */ | |
14780 | tree parmvec; | |
14781 | tree parmtypevec; | |
14782 | tree argpack = make_node (NONTYPE_ARGUMENT_PACK); | |
14783 | tree argtypepack = make_node (TYPE_ARGUMENT_PACK); | |
14784 | int i, len = 0; | |
14785 | tree t; | |
14786 | ||
14787 | /* Count how many parameters remain. */ | |
14788 | for (t = spec_parm; t; t = TREE_CHAIN (t)) | |
14789 | len++; | |
14790 | ||
14791 | /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */ | |
14792 | parmvec = make_tree_vec (len); | |
14793 | parmtypevec = make_tree_vec (len); | |
14794 | for(i = 0; i < len; i++, spec_parm = TREE_CHAIN (spec_parm)) | |
14795 | { | |
14796 | TREE_VEC_ELT (parmvec, i) = spec_parm; | |
14797 | TREE_VEC_ELT (parmtypevec, i) = TREE_TYPE (spec_parm); | |
14798 | } | |
14799 | ||
14800 | /* Build the argument packs. */ | |
14801 | SET_ARGUMENT_PACK_ARGS (argpack, parmvec); | |
14802 | SET_ARGUMENT_PACK_ARGS (argtypepack, parmtypevec); | |
14803 | TREE_TYPE (argpack) = argtypepack; | |
14804 | ||
14805 | /* Register the (value) argument pack as a specialization of | |
14806 | TMPL_PARM, then move on. */ | |
14807 | register_local_specialization (argpack, tmpl_parm); | |
14808 | tmpl_parm = TREE_CHAIN (tmpl_parm); | |
14809 | } | |
50bc768d | 14810 | gcc_assert (!spec_parm); |
a723baf1 | 14811 | |
558475f0 MM |
14812 | /* Substitute into the body of the function. */ |
14813 | tsubst_expr (DECL_SAVED_TREE (code_pattern), args, | |
015c2c66 MM |
14814 | tf_warning_or_error, tmpl, |
14815 | /*integral_constant_expression_p=*/false); | |
558475f0 | 14816 | |
75407da3 SP |
14817 | /* Set the current input_location to the end of the function |
14818 | so that finish_function knows where we are. */ | |
14819 | input_location = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus; | |
14820 | ||
6dfbb909 MM |
14821 | /* We don't need the local specializations any more. */ |
14822 | htab_delete (local_specializations); | |
6bbf1598 | 14823 | local_specializations = saved_local_specializations; |
6dfbb909 | 14824 | |
4d6abc1c | 14825 | /* Finish the function. */ |
b2dd096b | 14826 | d = finish_function (0); |
8cd2462c | 14827 | expand_or_defer_fn (d); |
5566b478 MS |
14828 | } |
14829 | ||
971cbc14 MM |
14830 | /* We're not deferring instantiation any more. */ |
14831 | TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0; | |
14832 | ||
31a714f6 MM |
14833 | if (need_push) |
14834 | pop_from_top_level (); | |
414ea4aa | 14835 | |
de22184b | 14836 | out: |
82a98427 | 14837 | input_location = saved_loc; |
912291ca | 14838 | in_system_header = saved_in_system_header; |
7d021397 | 14839 | pop_deferring_access_checks (); |
5566b478 | 14840 | pop_tinst_level (); |
a28e3c7f | 14841 | |
297a5329 JM |
14842 | timevar_pop (TV_PARSE); |
14843 | ||
a28e3c7f MS |
14844 | return d; |
14845 | } | |
5566b478 | 14846 | |
0aafb128 | 14847 | /* Run through the list of templates that we wish we could |
35046a54 KL |
14848 | instantiate, and instantiate any we can. RETRIES is the |
14849 | number of times we retry pending template instantiation. */ | |
0aafb128 | 14850 | |
35046a54 KL |
14851 | void |
14852 | instantiate_pending_templates (int retries) | |
0aafb128 | 14853 | { |
0aafb128 | 14854 | int reconsider; |
aad626f7 | 14855 | location_t saved_loc = input_location; |
12af7ba3 | 14856 | int saved_in_system_header = in_system_header; |
35046a54 KL |
14857 | |
14858 | /* Instantiating templates may trigger vtable generation. This in turn | |
14859 | may require further template instantiations. We place a limit here | |
14860 | to avoid infinite loop. */ | |
14861 | if (pending_templates && retries >= max_tinst_depth) | |
14862 | { | |
e2c3721c | 14863 | tree decl = pending_templates->tinst->decl; |
96c993a8 | 14864 | |
dee15844 | 14865 | error ("template instantiation depth exceeds maximum of %d" |
96c993a8 JM |
14866 | " instantiating %q+D, possibly from virtual table generation" |
14867 | " (use -ftemplate-depth-NN to increase the maximum)", | |
14868 | max_tinst_depth, decl); | |
14869 | if (TREE_CODE (decl) == FUNCTION_DECL) | |
14870 | /* Pretend that we defined it. */ | |
14871 | DECL_INITIAL (decl) = error_mark_node; | |
35046a54 KL |
14872 | return; |
14873 | } | |
14874 | ||
c8094d83 | 14875 | do |
0aafb128 | 14876 | { |
e2c3721c PB |
14877 | struct pending_template **t = &pending_templates; |
14878 | struct pending_template *last = NULL; | |
0aafb128 | 14879 | reconsider = 0; |
0aafb128 MM |
14880 | while (*t) |
14881 | { | |
e2c3721c PB |
14882 | tree instantiation = reopen_tinst_level ((*t)->tinst); |
14883 | bool complete = false; | |
0aafb128 | 14884 | |
2f939d94 | 14885 | if (TYPE_P (instantiation)) |
0aafb128 MM |
14886 | { |
14887 | tree fn; | |
14888 | ||
d0f062fb | 14889 | if (!COMPLETE_TYPE_P (instantiation)) |
0aafb128 MM |
14890 | { |
14891 | instantiate_class_template (instantiation); | |
14892 | if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation)) | |
c8094d83 | 14893 | for (fn = TYPE_METHODS (instantiation); |
0aafb128 MM |
14894 | fn; |
14895 | fn = TREE_CHAIN (fn)) | |
14896 | if (! DECL_ARTIFICIAL (fn)) | |
3db45ab5 | 14897 | instantiate_decl (fn, |
eba839f9 MM |
14898 | /*defer_ok=*/0, |
14899 | /*expl_inst_class_mem_p=*/false); | |
d0f062fb | 14900 | if (COMPLETE_TYPE_P (instantiation)) |
35046a54 | 14901 | reconsider = 1; |
0aafb128 MM |
14902 | } |
14903 | ||
e2c3721c | 14904 | complete = COMPLETE_TYPE_P (instantiation); |
0aafb128 MM |
14905 | } |
14906 | else | |
14907 | { | |
16d53b64 | 14908 | if (!DECL_TEMPLATE_SPECIALIZATION (instantiation) |
0aafb128 MM |
14909 | && !DECL_TEMPLATE_INSTANTIATED (instantiation)) |
14910 | { | |
3db45ab5 | 14911 | instantiation |
eba839f9 MM |
14912 | = instantiate_decl (instantiation, |
14913 | /*defer_ok=*/0, | |
14914 | /*expl_inst_class_mem_p=*/false); | |
0aafb128 | 14915 | if (DECL_TEMPLATE_INSTANTIATED (instantiation)) |
35046a54 | 14916 | reconsider = 1; |
0aafb128 MM |
14917 | } |
14918 | ||
e2c3721c PB |
14919 | complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation) |
14920 | || DECL_TEMPLATE_INSTANTIATED (instantiation)); | |
14921 | } | |
14922 | ||
14923 | if (complete) | |
14924 | /* If INSTANTIATION has been instantiated, then we don't | |
14925 | need to consider it again in the future. */ | |
14926 | *t = (*t)->next; | |
14927 | else | |
14928 | { | |
14929 | last = *t; | |
14930 | t = &(*t)->next; | |
0aafb128 | 14931 | } |
84e5ca0f | 14932 | tinst_depth = 0; |
e2c3721c | 14933 | current_tinst_level = NULL; |
0aafb128 | 14934 | } |
46ccf50a | 14935 | last_pending_template = last; |
c8094d83 | 14936 | } |
0aafb128 MM |
14937 | while (reconsider); |
14938 | ||
aad626f7 | 14939 | input_location = saved_loc; |
12af7ba3 | 14940 | in_system_header = saved_in_system_header; |
0aafb128 MM |
14941 | } |
14942 | ||
fd74ca0b MM |
14943 | /* Substitute ARGVEC into T, which is a list of initializers for |
14944 | either base class or a non-static data member. The TREE_PURPOSEs | |
14945 | are DECLs, and the TREE_VALUEs are the initializer values. Used by | |
14946 | instantiate_decl. */ | |
4393e105 | 14947 | |
824b9a4c | 14948 | static tree |
3a978d72 | 14949 | tsubst_initializer_list (tree t, tree argvec) |
5566b478 | 14950 | { |
2282d28d | 14951 | tree inits = NULL_TREE; |
5566b478 MS |
14952 | |
14953 | for (; t; t = TREE_CHAIN (t)) | |
14954 | { | |
fd74ca0b MM |
14955 | tree decl; |
14956 | tree init; | |
5d80a306 DG |
14957 | tree expanded_bases = NULL_TREE; |
14958 | tree expanded_arguments = NULL_TREE; | |
14959 | int i, len = 1; | |
fd74ca0b | 14960 | |
5d80a306 DG |
14961 | if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION) |
14962 | { | |
14963 | tree expr; | |
14964 | tree arg; | |
14965 | ||
14966 | /* Expand the base class expansion type into separate base | |
14967 | classes. */ | |
14968 | expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec, | |
14969 | tf_warning_or_error, | |
14970 | NULL_TREE); | |
14971 | if (expanded_bases == error_mark_node) | |
14972 | continue; | |
14973 | ||
14974 | /* We'll be building separate TREE_LISTs of arguments for | |
14975 | each base. */ | |
14976 | len = TREE_VEC_LENGTH (expanded_bases); | |
14977 | expanded_arguments = make_tree_vec (len); | |
14978 | for (i = 0; i < len; i++) | |
14979 | TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE; | |
14980 | ||
14981 | /* Build a dummy EXPR_PACK_EXPANSION that will be used to | |
14982 | expand each argument in the TREE_VALUE of t. */ | |
14983 | expr = make_node (EXPR_PACK_EXPANSION); | |
14984 | PACK_EXPANSION_PARAMETER_PACKS (expr) = | |
14985 | PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t)); | |
14986 | ||
14987 | /* Substitute parameter packs into each argument in the | |
14988 | TREE_LIST. */ | |
14989 | in_base_initializer = 1; | |
14990 | for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg)) | |
14991 | { | |
14992 | tree expanded_exprs; | |
14993 | ||
14994 | /* Expand the argument. */ | |
14995 | SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg)); | |
14996 | expanded_exprs = tsubst_pack_expansion (expr, argvec, | |
14997 | tf_warning_or_error, | |
14998 | NULL_TREE); | |
14999 | ||
15000 | /* Prepend each of the expanded expressions to the | |
15001 | corresponding TREE_LIST in EXPANDED_ARGUMENTS. */ | |
15002 | for (i = 0; i < len; i++) | |
15003 | { | |
15004 | TREE_VEC_ELT (expanded_arguments, i) = | |
15005 | tree_cons (NULL_TREE, TREE_VEC_ELT (expanded_exprs, i), | |
15006 | TREE_VEC_ELT (expanded_arguments, i)); | |
15007 | } | |
15008 | } | |
15009 | in_base_initializer = 0; | |
15010 | ||
15011 | /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS, | |
15012 | since we built them backwards. */ | |
15013 | for (i = 0; i < len; i++) | |
15014 | { | |
15015 | TREE_VEC_ELT (expanded_arguments, i) = | |
15016 | nreverse (TREE_VEC_ELT (expanded_arguments, i)); | |
15017 | } | |
15018 | } | |
1f5a253a | 15019 | |
5d80a306 DG |
15020 | for (i = 0; i < len; ++i) |
15021 | { | |
15022 | if (expanded_bases) | |
15023 | { | |
15024 | decl = TREE_VEC_ELT (expanded_bases, i); | |
15025 | decl = expand_member_init (decl); | |
15026 | init = TREE_VEC_ELT (expanded_arguments, i); | |
15027 | } | |
15028 | else | |
15029 | { | |
15030 | decl = tsubst_copy (TREE_PURPOSE (t), argvec, | |
15031 | tf_warning_or_error, NULL_TREE); | |
15032 | ||
15033 | decl = expand_member_init (decl); | |
15034 | if (decl && !DECL_P (decl)) | |
15035 | in_base_initializer = 1; | |
15036 | ||
15037 | init = tsubst_expr (TREE_VALUE (t), argvec, | |
15038 | tf_warning_or_error, NULL_TREE, | |
15039 | /*integral_constant_expression_p=*/false); | |
15040 | in_base_initializer = 0; | |
15041 | } | |
15042 | ||
15043 | if (decl) | |
15044 | { | |
15045 | init = build_tree_list (decl, init); | |
15046 | TREE_CHAIN (init) = inits; | |
15047 | inits = init; | |
15048 | } | |
15049 | } | |
5566b478 | 15050 | } |
2282d28d | 15051 | return inits; |
5566b478 MS |
15052 | } |
15053 | ||
61a127b3 MM |
15054 | /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */ |
15055 | ||
15056 | static void | |
3a978d72 | 15057 | set_current_access_from_decl (tree decl) |
61a127b3 MM |
15058 | { |
15059 | if (TREE_PRIVATE (decl)) | |
15060 | current_access_specifier = access_private_node; | |
15061 | else if (TREE_PROTECTED (decl)) | |
15062 | current_access_specifier = access_protected_node; | |
15063 | else | |
15064 | current_access_specifier = access_public_node; | |
15065 | } | |
15066 | ||
dbfe2124 MM |
15067 | /* Instantiate an enumerated type. TAG is the template type, NEWTAG |
15068 | is the instantiation (which should have been created with | |
15069 | start_enum) and ARGS are the template arguments to use. */ | |
b87692e5 | 15070 | |
dbfe2124 | 15071 | static void |
3a978d72 | 15072 | tsubst_enum (tree tag, tree newtag, tree args) |
b87692e5 | 15073 | { |
dbfe2124 | 15074 | tree e; |
b87692e5 MS |
15075 | |
15076 | for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e)) | |
15077 | { | |
61a127b3 | 15078 | tree value; |
7b6d72fc MM |
15079 | tree decl; |
15080 | ||
15081 | decl = TREE_VALUE (e); | |
61a127b3 MM |
15082 | /* Note that in a template enum, the TREE_VALUE is the |
15083 | CONST_DECL, not the corresponding INTEGER_CST. */ | |
c8094d83 | 15084 | value = tsubst_expr (DECL_INITIAL (decl), |
015c2c66 MM |
15085 | args, tf_warning_or_error, NULL_TREE, |
15086 | /*integral_constant_expression_p=*/true); | |
61a127b3 MM |
15087 | |
15088 | /* Give this enumeration constant the correct access. */ | |
7b6d72fc | 15089 | set_current_access_from_decl (decl); |
61a127b3 MM |
15090 | |
15091 | /* Actually build the enumerator itself. */ | |
c8094d83 | 15092 | build_enumerator (DECL_NAME (decl), value, newtag); |
dbfe2124 | 15093 | } |
b3d5a58b | 15094 | |
219670f1 | 15095 | finish_enum (newtag); |
f31686a3 RH |
15096 | DECL_SOURCE_LOCATION (TYPE_NAME (newtag)) |
15097 | = DECL_SOURCE_LOCATION (TYPE_NAME (tag)); | |
b87692e5 | 15098 | } |
36a117a5 | 15099 | |
1f6e1acc AS |
15100 | /* DECL is a FUNCTION_DECL that is a template specialization. Return |
15101 | its type -- but without substituting the innermost set of template | |
15102 | arguments. So, innermost set of template parameters will appear in | |
5c74d5b0 | 15103 | the type. */ |
1f6e1acc | 15104 | |
c8094d83 | 15105 | tree |
3a978d72 | 15106 | get_mostly_instantiated_function_type (tree decl) |
1f6e1acc | 15107 | { |
1f6e1acc AS |
15108 | tree fn_type; |
15109 | tree tmpl; | |
15110 | tree targs; | |
15111 | tree tparms; | |
15112 | int parm_depth; | |
15113 | ||
15114 | tmpl = most_general_template (DECL_TI_TEMPLATE (decl)); | |
15115 | targs = DECL_TI_ARGS (decl); | |
15116 | tparms = DECL_TEMPLATE_PARMS (tmpl); | |
15117 | parm_depth = TMPL_PARMS_DEPTH (tparms); | |
15118 | ||
15119 | /* There should be as many levels of arguments as there are levels | |
15120 | of parameters. */ | |
50bc768d | 15121 | gcc_assert (parm_depth == TMPL_ARGS_DEPTH (targs)); |
1f6e1acc AS |
15122 | |
15123 | fn_type = TREE_TYPE (tmpl); | |
1f6e1acc AS |
15124 | |
15125 | if (parm_depth == 1) | |
15126 | /* No substitution is necessary. */ | |
15127 | ; | |
15128 | else | |
15129 | { | |
fae6e246 | 15130 | int i, save_access_control; |
1f6e1acc AS |
15131 | tree partial_args; |
15132 | ||
15133 | /* Replace the innermost level of the TARGS with NULL_TREEs to | |
dc957d14 | 15134 | let tsubst know not to substitute for those parameters. */ |
1f6e1acc AS |
15135 | partial_args = make_tree_vec (TREE_VEC_LENGTH (targs)); |
15136 | for (i = 1; i < TMPL_ARGS_DEPTH (targs); ++i) | |
15137 | SET_TMPL_ARGS_LEVEL (partial_args, i, | |
15138 | TMPL_ARGS_LEVEL (targs, i)); | |
15139 | SET_TMPL_ARGS_LEVEL (partial_args, | |
15140 | TMPL_ARGS_DEPTH (targs), | |
15141 | make_tree_vec (DECL_NTPARMS (tmpl))); | |
15142 | ||
fae6e246 RH |
15143 | /* Disable access control as this function is used only during |
15144 | name-mangling. */ | |
15145 | save_access_control = flag_access_control; | |
15146 | flag_access_control = 0; | |
5c74d5b0 | 15147 | |
9579624e | 15148 | ++processing_template_decl; |
1f6e1acc AS |
15149 | /* Now, do the (partial) substitution to figure out the |
15150 | appropriate function type. */ | |
c2ea3a40 | 15151 | fn_type = tsubst (fn_type, partial_args, tf_error, NULL_TREE); |
9579624e | 15152 | --processing_template_decl; |
1f6e1acc AS |
15153 | |
15154 | /* Substitute into the template parameters to obtain the real | |
15155 | innermost set of parameters. This step is important if the | |
15156 | innermost set of template parameters contains value | |
15157 | parameters whose types depend on outer template parameters. */ | |
15158 | TREE_VEC_LENGTH (partial_args)--; | |
c2ea3a40 | 15159 | tparms = tsubst_template_parms (tparms, partial_args, tf_error); |
1f6e1acc | 15160 | |
fae6e246 | 15161 | flag_access_control = save_access_control; |
5c74d5b0 | 15162 | } |
1f6e1acc AS |
15163 | |
15164 | return fn_type; | |
15165 | } | |
669ec2b4 | 15166 | |
cb753e49 | 15167 | /* Return truthvalue if we're processing a template different from |
dc957d14 | 15168 | the last one involved in diagnostics. */ |
cb753e49 | 15169 | int |
3a978d72 | 15170 | problematic_instantiation_changed (void) |
cb753e49 GDR |
15171 | { |
15172 | return last_template_error_tick != tinst_level_tick; | |
15173 | } | |
15174 | ||
15175 | /* Remember current template involved in diagnostics. */ | |
15176 | void | |
3a978d72 | 15177 | record_last_problematic_instantiation (void) |
cb753e49 GDR |
15178 | { |
15179 | last_template_error_tick = tinst_level_tick; | |
15180 | } | |
15181 | ||
e2c3721c | 15182 | struct tinst_level * |
3a978d72 | 15183 | current_instantiation (void) |
cb753e49 GDR |
15184 | { |
15185 | return current_tinst_level; | |
15186 | } | |
db3f4e4e NS |
15187 | |
15188 | /* [temp.param] Check that template non-type parm TYPE is of an allowable | |
838dfd8a | 15189 | type. Return zero for ok, nonzero for disallowed. Issue error and |
c2ea3a40 | 15190 | warning messages under control of COMPLAIN. */ |
db3f4e4e NS |
15191 | |
15192 | static int | |
3a978d72 | 15193 | invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain) |
db3f4e4e NS |
15194 | { |
15195 | if (INTEGRAL_TYPE_P (type)) | |
15196 | return 0; | |
15197 | else if (POINTER_TYPE_P (type)) | |
15198 | return 0; | |
a5ac359a | 15199 | else if (TYPE_PTR_TO_MEMBER_P (type)) |
db3f4e4e | 15200 | return 0; |
db3f4e4e NS |
15201 | else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM) |
15202 | return 0; | |
15203 | else if (TREE_CODE (type) == TYPENAME_TYPE) | |
15204 | return 0; | |
c8094d83 | 15205 | |
c2ea3a40 | 15206 | if (complain & tf_error) |
0f51ccfc | 15207 | error ("%q#T is not a valid type for a template constant parameter", type); |
db3f4e4e NS |
15208 | return 1; |
15209 | } | |
e2500fed | 15210 | |
5552b43c MM |
15211 | /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type]. |
15212 | Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/ | |
1fb3244a | 15213 | |
5552b43c MM |
15214 | static bool |
15215 | dependent_type_p_r (tree type) | |
1fb3244a MM |
15216 | { |
15217 | tree scope; | |
15218 | ||
1fb3244a MM |
15219 | /* [temp.dep.type] |
15220 | ||
15221 | A type is dependent if it is: | |
15222 | ||
6615c446 JO |
15223 | -- a template parameter. Template template parameters are types |
15224 | for us (since TYPE_P holds true for them) so we handle | |
15225 | them here. */ | |
c8094d83 | 15226 | if (TREE_CODE (type) == TEMPLATE_TYPE_PARM |
6bf92cb6 | 15227 | || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM) |
1fb3244a MM |
15228 | return true; |
15229 | /* -- a qualified-id with a nested-name-specifier which contains a | |
0cbd7506 | 15230 | class-name that names a dependent type or whose unqualified-id |
1fb3244a MM |
15231 | names a dependent type. */ |
15232 | if (TREE_CODE (type) == TYPENAME_TYPE) | |
15233 | return true; | |
15234 | /* -- a cv-qualified type where the cv-unqualified type is | |
0cbd7506 | 15235 | dependent. */ |
1fb3244a MM |
15236 | type = TYPE_MAIN_VARIANT (type); |
15237 | /* -- a compound type constructed from any dependent type. */ | |
a5ac359a | 15238 | if (TYPE_PTR_TO_MEMBER_P (type)) |
1fb3244a | 15239 | return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type)) |
c8094d83 | 15240 | || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE |
1fb3244a MM |
15241 | (type))); |
15242 | else if (TREE_CODE (type) == POINTER_TYPE | |
15243 | || TREE_CODE (type) == REFERENCE_TYPE) | |
15244 | return dependent_type_p (TREE_TYPE (type)); | |
15245 | else if (TREE_CODE (type) == FUNCTION_TYPE | |
15246 | || TREE_CODE (type) == METHOD_TYPE) | |
15247 | { | |
15248 | tree arg_type; | |
15249 | ||
15250 | if (dependent_type_p (TREE_TYPE (type))) | |
15251 | return true; | |
c8094d83 MS |
15252 | for (arg_type = TYPE_ARG_TYPES (type); |
15253 | arg_type; | |
1fb3244a MM |
15254 | arg_type = TREE_CHAIN (arg_type)) |
15255 | if (dependent_type_p (TREE_VALUE (arg_type))) | |
15256 | return true; | |
15257 | return false; | |
15258 | } | |
15259 | /* -- an array type constructed from any dependent type or whose | |
0cbd7506 | 15260 | size is specified by a constant expression that is |
1fb3244a MM |
15261 | value-dependent. */ |
15262 | if (TREE_CODE (type) == ARRAY_TYPE) | |
15263 | { | |
15264 | if (TYPE_DOMAIN (type) | |
c8094d83 | 15265 | && ((value_dependent_expression_p |
1fb3244a MM |
15266 | (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))) |
15267 | || (type_dependent_expression_p | |
15268 | (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))))) | |
15269 | return true; | |
15270 | return dependent_type_p (TREE_TYPE (type)); | |
15271 | } | |
c8094d83 | 15272 | |
1fb3244a | 15273 | /* -- a template-id in which either the template name is a template |
86306a6b NS |
15274 | parameter ... */ |
15275 | if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM) | |
1fb3244a | 15276 | return true; |
86306a6b | 15277 | /* ... or any of the template arguments is a dependent type or |
04c06002 | 15278 | an expression that is type-dependent or value-dependent. */ |
86306a6b | 15279 | else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type) |
c8094d83 | 15280 | && (any_dependent_template_arguments_p |
7e99327d | 15281 | (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type))))) |
1fb3244a | 15282 | return true; |
c8094d83 | 15283 | |
3ad6a8e1 DG |
15284 | /* All TYPEOF_TYPEs and DECLTYPE_TYPEs are dependent; if the |
15285 | argument of the `typeof' expression is not type-dependent, then | |
15286 | it should already been have resolved. */ | |
15287 | if (TREE_CODE (type) == TYPEOF_TYPE | |
15288 | || TREE_CODE (type) == DECLTYPE_TYPE) | |
1fb3244a | 15289 | return true; |
c8094d83 | 15290 | |
5d80a306 DG |
15291 | /* A template argument pack is dependent if any of its packed |
15292 | arguments are. */ | |
15293 | if (TREE_CODE (type) == TYPE_ARGUMENT_PACK) | |
15294 | { | |
15295 | tree args = ARGUMENT_PACK_ARGS (type); | |
15296 | int i, len = TREE_VEC_LENGTH (args); | |
15297 | for (i = 0; i < len; ++i) | |
15298 | if (dependent_template_arg_p (TREE_VEC_ELT (args, i))) | |
15299 | return true; | |
15300 | } | |
15301 | ||
15302 | /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must | |
15303 | be template parameters. */ | |
15304 | if (TREE_CODE (type) == TYPE_PACK_EXPANSION) | |
15305 | return true; | |
15306 | ||
1fb3244a MM |
15307 | /* The standard does not specifically mention types that are local |
15308 | to template functions or local classes, but they should be | |
15309 | considered dependent too. For example: | |
15310 | ||
c8094d83 | 15311 | template <int I> void f() { |
0cbd7506 | 15312 | enum E { a = I }; |
1fb3244a MM |
15313 | S<sizeof (E)> s; |
15314 | } | |
15315 | ||
15316 | The size of `E' cannot be known until the value of `I' has been | |
15317 | determined. Therefore, `E' must be considered dependent. */ | |
15318 | scope = TYPE_CONTEXT (type); | |
15319 | if (scope && TYPE_P (scope)) | |
15320 | return dependent_type_p (scope); | |
15321 | else if (scope && TREE_CODE (scope) == FUNCTION_DECL) | |
15322 | return type_dependent_expression_p (scope); | |
15323 | ||
15324 | /* Other types are non-dependent. */ | |
15325 | return false; | |
15326 | } | |
15327 | ||
5552b43c MM |
15328 | /* Returns TRUE if TYPE is dependent, in the sense of |
15329 | [temp.dep.type]. */ | |
15330 | ||
15331 | bool | |
15332 | dependent_type_p (tree type) | |
15333 | { | |
15334 | /* If there are no template parameters in scope, then there can't be | |
15335 | any dependent types. */ | |
15336 | if (!processing_template_decl) | |
e7e93965 MM |
15337 | { |
15338 | /* If we are not processing a template, then nobody should be | |
15339 | providing us with a dependent type. */ | |
15340 | gcc_assert (type); | |
15341 | gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM); | |
15342 | return false; | |
15343 | } | |
5552b43c MM |
15344 | |
15345 | /* If the type is NULL, we have not computed a type for the entity | |
15346 | in question; in that case, the type is dependent. */ | |
15347 | if (!type) | |
15348 | return true; | |
15349 | ||
15350 | /* Erroneous types can be considered non-dependent. */ | |
15351 | if (type == error_mark_node) | |
15352 | return false; | |
15353 | ||
15354 | /* If we have not already computed the appropriate value for TYPE, | |
15355 | do so now. */ | |
15356 | if (!TYPE_DEPENDENT_P_VALID (type)) | |
15357 | { | |
15358 | TYPE_DEPENDENT_P (type) = dependent_type_p_r (type); | |
15359 | TYPE_DEPENDENT_P_VALID (type) = 1; | |
15360 | } | |
15361 | ||
15362 | return TYPE_DEPENDENT_P (type); | |
15363 | } | |
15364 | ||
8d83f792 MM |
15365 | /* Returns TRUE if EXPRESSION is dependent, according to CRITERION. */ |
15366 | ||
15367 | static bool | |
15368 | dependent_scope_ref_p (tree expression, bool criterion (tree)) | |
15369 | { | |
15370 | tree scope; | |
15371 | tree name; | |
15372 | ||
50bc768d | 15373 | gcc_assert (TREE_CODE (expression) == SCOPE_REF); |
8d83f792 MM |
15374 | |
15375 | if (!TYPE_P (TREE_OPERAND (expression, 0))) | |
15376 | return true; | |
15377 | ||
15378 | scope = TREE_OPERAND (expression, 0); | |
15379 | name = TREE_OPERAND (expression, 1); | |
15380 | ||
15381 | /* [temp.dep.expr] | |
15382 | ||
15383 | An id-expression is type-dependent if it contains a | |
15384 | nested-name-specifier that contains a class-name that names a | |
15385 | dependent type. */ | |
15386 | /* The suggested resolution to Core Issue 2 implies that if the | |
15387 | qualifying type is the current class, then we must peek | |
15388 | inside it. */ | |
c8094d83 | 15389 | if (DECL_P (name) |
8d83f792 MM |
15390 | && currently_open_class (scope) |
15391 | && !criterion (name)) | |
15392 | return false; | |
15393 | if (dependent_type_p (scope)) | |
15394 | return true; | |
15395 | ||
15396 | return false; | |
15397 | } | |
15398 | ||
20929c7f | 15399 | /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of |
fe89d797 MM |
15400 | [temp.dep.constexpr]. EXPRESSION is already known to be a constant |
15401 | expression. */ | |
1fb3244a | 15402 | |
14d22dd6 | 15403 | bool |
1fb3244a MM |
15404 | value_dependent_expression_p (tree expression) |
15405 | { | |
15406 | if (!processing_template_decl) | |
15407 | return false; | |
15408 | ||
15409 | /* A name declared with a dependent type. */ | |
7416ab02 | 15410 | if (DECL_P (expression) && type_dependent_expression_p (expression)) |
1fb3244a | 15411 | return true; |
c8094d83 | 15412 | |
7416ab02 NS |
15413 | switch (TREE_CODE (expression)) |
15414 | { | |
15415 | case IDENTIFIER_NODE: | |
15416 | /* A name that has not been looked up -- must be dependent. */ | |
15417 | return true; | |
15418 | ||
15419 | case TEMPLATE_PARM_INDEX: | |
15420 | /* A non-type template parm. */ | |
15421 | return true; | |
15422 | ||
15423 | case CONST_DECL: | |
15424 | /* A non-type template parm. */ | |
15425 | if (DECL_TEMPLATE_PARM_P (expression)) | |
d17811fd | 15426 | return true; |
7416ab02 | 15427 | return false; |
d36d5600 | 15428 | |
7416ab02 | 15429 | case VAR_DECL: |
c8094d83 | 15430 | /* A constant with integral or enumeration type and is initialized |
0cbd7506 | 15431 | with an expression that is value-dependent. */ |
7416ab02 NS |
15432 | if (DECL_INITIAL (expression) |
15433 | && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (expression)) | |
15434 | && value_dependent_expression_p (DECL_INITIAL (expression))) | |
15435 | return true; | |
15436 | return false; | |
15437 | ||
15438 | case DYNAMIC_CAST_EXPR: | |
15439 | case STATIC_CAST_EXPR: | |
15440 | case CONST_CAST_EXPR: | |
15441 | case REINTERPRET_CAST_EXPR: | |
15442 | case CAST_EXPR: | |
15443 | /* These expressions are value-dependent if the type to which | |
0cbd7506 MS |
15444 | the cast occurs is dependent or the expression being casted |
15445 | is value-dependent. */ | |
7416ab02 NS |
15446 | { |
15447 | tree type = TREE_TYPE (expression); | |
c8094d83 | 15448 | |
7416ab02 NS |
15449 | if (dependent_type_p (type)) |
15450 | return true; | |
c8094d83 | 15451 | |
7416ab02 NS |
15452 | /* A functional cast has a list of operands. */ |
15453 | expression = TREE_OPERAND (expression, 0); | |
15454 | if (!expression) | |
15455 | { | |
15456 | /* If there are no operands, it must be an expression such | |
15457 | as "int()". This should not happen for aggregate types | |
15458 | because it would form non-constant expressions. */ | |
15459 | gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type)); | |
c8094d83 | 15460 | |
7416ab02 NS |
15461 | return false; |
15462 | } | |
c8094d83 | 15463 | |
7416ab02 | 15464 | if (TREE_CODE (expression) == TREE_LIST) |
32a11c08 | 15465 | return any_value_dependent_elements_p (expression); |
c8094d83 | 15466 | |
d17811fd | 15467 | return value_dependent_expression_p (expression); |
7416ab02 | 15468 | } |
c8094d83 | 15469 | |
7416ab02 NS |
15470 | case SIZEOF_EXPR: |
15471 | case ALIGNOF_EXPR: | |
15472 | /* A `sizeof' expression is value-dependent if the operand is | |
5d80a306 | 15473 | type-dependent or is a pack expansion. */ |
d17811fd | 15474 | expression = TREE_OPERAND (expression, 0); |
5d80a306 DG |
15475 | if (PACK_EXPANSION_P (expression)) |
15476 | return true; | |
15477 | else if (TYPE_P (expression)) | |
d17811fd MM |
15478 | return dependent_type_p (expression); |
15479 | return type_dependent_expression_p (expression); | |
100d337a | 15480 | |
7416ab02 NS |
15481 | case SCOPE_REF: |
15482 | return dependent_scope_ref_p (expression, value_dependent_expression_p); | |
100d337a | 15483 | |
7416ab02 NS |
15484 | case COMPONENT_REF: |
15485 | return (value_dependent_expression_p (TREE_OPERAND (expression, 0)) | |
15486 | || value_dependent_expression_p (TREE_OPERAND (expression, 1))); | |
15487 | ||
15488 | case CALL_EXPR: | |
fe89d797 MM |
15489 | /* A CALL_EXPR may appear in a constant expression if it is a |
15490 | call to a builtin function, e.g., __builtin_constant_p. All | |
15491 | such calls are value-dependent. */ | |
15492 | return true; | |
7416ab02 | 15493 | |
5d80a306 DG |
15494 | case NONTYPE_ARGUMENT_PACK: |
15495 | /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument | |
15496 | is value-dependent. */ | |
15497 | { | |
15498 | tree values = ARGUMENT_PACK_ARGS (expression); | |
15499 | int i, len = TREE_VEC_LENGTH (values); | |
15500 | ||
15501 | for (i = 0; i < len; ++i) | |
15502 | if (value_dependent_expression_p (TREE_VEC_ELT (values, i))) | |
15503 | return true; | |
15504 | ||
15505 | return false; | |
15506 | } | |
15507 | ||
cb68ec50 PC |
15508 | case TRAIT_EXPR: |
15509 | { | |
15510 | tree type2 = TRAIT_EXPR_TYPE2 (expression); | |
15511 | return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression)) | |
15512 | || (type2 ? dependent_type_p (type2) : false)); | |
15513 | } | |
15514 | ||
f01ec37d SM |
15515 | case MODOP_EXPR: |
15516 | return ((value_dependent_expression_p (TREE_OPERAND (expression, 0))) | |
15517 | || (value_dependent_expression_p (TREE_OPERAND (expression, 2)))); | |
15518 | ||
7416ab02 NS |
15519 | default: |
15520 | /* A constant expression is value-dependent if any subexpression is | |
0cbd7506 | 15521 | value-dependent. */ |
1fb3244a MM |
15522 | switch (TREE_CODE_CLASS (TREE_CODE (expression))) |
15523 | { | |
7416ab02 | 15524 | case tcc_reference: |
6615c446 | 15525 | case tcc_unary: |
c8094d83 | 15526 | return (value_dependent_expression_p |
1fb3244a | 15527 | (TREE_OPERAND (expression, 0))); |
c8094d83 | 15528 | |
6615c446 JO |
15529 | case tcc_comparison: |
15530 | case tcc_binary: | |
c8094d83 | 15531 | return ((value_dependent_expression_p |
1fb3244a | 15532 | (TREE_OPERAND (expression, 0))) |
c8094d83 | 15533 | || (value_dependent_expression_p |
1fb3244a | 15534 | (TREE_OPERAND (expression, 1)))); |
c8094d83 | 15535 | |
6615c446 | 15536 | case tcc_expression: |
5039610b | 15537 | case tcc_vl_exp: |
1fb3244a MM |
15538 | { |
15539 | int i; | |
5039610b | 15540 | for (i = 0; i < TREE_OPERAND_LENGTH (expression); ++i) |
14d22dd6 MM |
15541 | /* In some cases, some of the operands may be missing. |
15542 | (For example, in the case of PREDECREMENT_EXPR, the | |
15543 | amount to increment by may be missing.) That doesn't | |
15544 | make the expression dependent. */ | |
15545 | if (TREE_OPERAND (expression, i) | |
15546 | && (value_dependent_expression_p | |
15547 | (TREE_OPERAND (expression, i)))) | |
1fb3244a MM |
15548 | return true; |
15549 | return false; | |
15550 | } | |
c8094d83 | 15551 | |
6615c446 | 15552 | default: |
7416ab02 | 15553 | break; |
1fb3244a MM |
15554 | } |
15555 | } | |
c8094d83 | 15556 | |
1fb3244a MM |
15557 | /* The expression is not value-dependent. */ |
15558 | return false; | |
15559 | } | |
15560 | ||
15561 | /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of | |
15562 | [temp.dep.expr]. */ | |
15563 | ||
15564 | bool | |
3a978d72 | 15565 | type_dependent_expression_p (tree expression) |
1fb3244a MM |
15566 | { |
15567 | if (!processing_template_decl) | |
15568 | return false; | |
15569 | ||
7efa3e22 NS |
15570 | if (expression == error_mark_node) |
15571 | return false; | |
10b1d5e7 MM |
15572 | |
15573 | /* An unresolved name is always dependent. */ | |
b01e6d2b JM |
15574 | if (TREE_CODE (expression) == IDENTIFIER_NODE |
15575 | || TREE_CODE (expression) == USING_DECL) | |
10b1d5e7 | 15576 | return true; |
c8094d83 | 15577 | |
1fb3244a MM |
15578 | /* Some expression forms are never type-dependent. */ |
15579 | if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR | |
15580 | || TREE_CODE (expression) == SIZEOF_EXPR | |
15581 | || TREE_CODE (expression) == ALIGNOF_EXPR | |
cb68ec50 | 15582 | || TREE_CODE (expression) == TRAIT_EXPR |
1fb3244a MM |
15583 | || TREE_CODE (expression) == TYPEID_EXPR |
15584 | || TREE_CODE (expression) == DELETE_EXPR | |
15585 | || TREE_CODE (expression) == VEC_DELETE_EXPR | |
15586 | || TREE_CODE (expression) == THROW_EXPR) | |
15587 | return false; | |
15588 | ||
15589 | /* The types of these expressions depends only on the type to which | |
15590 | the cast occurs. */ | |
15591 | if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR | |
15592 | || TREE_CODE (expression) == STATIC_CAST_EXPR | |
15593 | || TREE_CODE (expression) == CONST_CAST_EXPR | |
15594 | || TREE_CODE (expression) == REINTERPRET_CAST_EXPR | |
15595 | || TREE_CODE (expression) == CAST_EXPR) | |
15596 | return dependent_type_p (TREE_TYPE (expression)); | |
d17811fd | 15597 | |
1fb3244a MM |
15598 | /* The types of these expressions depends only on the type created |
15599 | by the expression. */ | |
d17811fd MM |
15600 | if (TREE_CODE (expression) == NEW_EXPR |
15601 | || TREE_CODE (expression) == VEC_NEW_EXPR) | |
09d2f85f KL |
15602 | { |
15603 | /* For NEW_EXPR tree nodes created inside a template, either | |
15604 | the object type itself or a TREE_LIST may appear as the | |
15605 | operand 1. */ | |
15606 | tree type = TREE_OPERAND (expression, 1); | |
15607 | if (TREE_CODE (type) == TREE_LIST) | |
15608 | /* This is an array type. We need to check array dimensions | |
15609 | as well. */ | |
15610 | return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type))) | |
15611 | || value_dependent_expression_p | |
15612 | (TREE_OPERAND (TREE_VALUE (type), 1)); | |
15613 | else | |
15614 | return dependent_type_p (type); | |
15615 | } | |
1fb3244a | 15616 | |
5a57f1b2 JM |
15617 | if (TREE_CODE (expression) == SCOPE_REF |
15618 | && dependent_scope_ref_p (expression, | |
15619 | type_dependent_expression_p)) | |
15620 | return true; | |
15621 | ||
15622 | if (TREE_CODE (expression) == FUNCTION_DECL | |
15623 | && DECL_LANG_SPECIFIC (expression) | |
15624 | && DECL_TEMPLATE_INFO (expression) | |
15625 | && (any_dependent_template_arguments_p | |
15626 | (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression))))) | |
15627 | return true; | |
15628 | ||
15629 | if (TREE_CODE (expression) == TEMPLATE_DECL | |
15630 | && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression)) | |
15631 | return false; | |
15632 | ||
c2acde1e JM |
15633 | if (TREE_CODE (expression) == STMT_EXPR) |
15634 | expression = stmt_expr_value_expr (expression); | |
15635 | ||
d17811fd MM |
15636 | if (TREE_TYPE (expression) == unknown_type_node) |
15637 | { | |
15638 | if (TREE_CODE (expression) == ADDR_EXPR) | |
15639 | return type_dependent_expression_p (TREE_OPERAND (expression, 0)); | |
c353b8e3 MM |
15640 | if (TREE_CODE (expression) == COMPONENT_REF |
15641 | || TREE_CODE (expression) == OFFSET_REF) | |
6cb89308 NS |
15642 | { |
15643 | if (type_dependent_expression_p (TREE_OPERAND (expression, 0))) | |
15644 | return true; | |
15645 | expression = TREE_OPERAND (expression, 1); | |
15646 | if (TREE_CODE (expression) == IDENTIFIER_NODE) | |
15647 | return false; | |
15648 | } | |
3601f003 KL |
15649 | /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */ |
15650 | if (TREE_CODE (expression) == SCOPE_REF) | |
15651 | return false; | |
c8094d83 | 15652 | |
d17811fd MM |
15653 | if (TREE_CODE (expression) == BASELINK) |
15654 | expression = BASELINK_FUNCTIONS (expression); | |
c8094d83 | 15655 | |
d17811fd MM |
15656 | if (TREE_CODE (expression) == TEMPLATE_ID_EXPR) |
15657 | { | |
ee3071ef NS |
15658 | if (any_dependent_template_arguments_p |
15659 | (TREE_OPERAND (expression, 1))) | |
d17811fd MM |
15660 | return true; |
15661 | expression = TREE_OPERAND (expression, 0); | |
15662 | } | |
b207d6e2 MM |
15663 | gcc_assert (TREE_CODE (expression) == OVERLOAD |
15664 | || TREE_CODE (expression) == FUNCTION_DECL); | |
c8094d83 | 15665 | |
315fb5db | 15666 | while (expression) |
d17811fd | 15667 | { |
315fb5db NS |
15668 | if (type_dependent_expression_p (OVL_CURRENT (expression))) |
15669 | return true; | |
15670 | expression = OVL_NEXT (expression); | |
d17811fd | 15671 | } |
315fb5db | 15672 | return false; |
d17811fd | 15673 | } |
c8094d83 | 15674 | |
3ce5fa4f | 15675 | gcc_assert (TREE_CODE (expression) != TYPE_DECL); |
3db45ab5 | 15676 | |
1fb3244a MM |
15677 | return (dependent_type_p (TREE_TYPE (expression))); |
15678 | } | |
15679 | ||
d17811fd MM |
15680 | /* Returns TRUE if ARGS (a TREE_LIST of arguments to a function call) |
15681 | contains a type-dependent expression. */ | |
1fb3244a MM |
15682 | |
15683 | bool | |
58f9752a | 15684 | any_type_dependent_arguments_p (const_tree args) |
d17811fd MM |
15685 | { |
15686 | while (args) | |
15687 | { | |
7efa3e22 NS |
15688 | tree arg = TREE_VALUE (args); |
15689 | ||
15690 | if (type_dependent_expression_p (arg)) | |
d17811fd MM |
15691 | return true; |
15692 | args = TREE_CHAIN (args); | |
15693 | } | |
15694 | return false; | |
15695 | } | |
15696 | ||
32a11c08 MM |
15697 | /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are |
15698 | expressions) contains any value-dependent expressions. */ | |
15699 | ||
15700 | bool | |
58f9752a | 15701 | any_value_dependent_elements_p (const_tree list) |
32a11c08 MM |
15702 | { |
15703 | for (; list; list = TREE_CHAIN (list)) | |
15704 | if (value_dependent_expression_p (TREE_VALUE (list))) | |
15705 | return true; | |
15706 | ||
15707 | return false; | |
15708 | } | |
15709 | ||
d17811fd MM |
15710 | /* Returns TRUE if the ARG (a template argument) is dependent. */ |
15711 | ||
5d80a306 | 15712 | bool |
1fb3244a MM |
15713 | dependent_template_arg_p (tree arg) |
15714 | { | |
15715 | if (!processing_template_decl) | |
15716 | return false; | |
15717 | ||
15718 | if (TREE_CODE (arg) == TEMPLATE_DECL | |
15719 | || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM) | |
15720 | return dependent_template_p (arg); | |
5d80a306 DG |
15721 | else if (ARGUMENT_PACK_P (arg)) |
15722 | { | |
15723 | tree args = ARGUMENT_PACK_ARGS (arg); | |
15724 | int i, len = TREE_VEC_LENGTH (args); | |
15725 | for (i = 0; i < len; ++i) | |
15726 | { | |
15727 | if (dependent_template_arg_p (TREE_VEC_ELT (args, i))) | |
15728 | return true; | |
15729 | } | |
15730 | ||
15731 | return false; | |
15732 | } | |
1fb3244a MM |
15733 | else if (TYPE_P (arg)) |
15734 | return dependent_type_p (arg); | |
15735 | else | |
15736 | return (type_dependent_expression_p (arg) | |
15737 | || value_dependent_expression_p (arg)); | |
15738 | } | |
15739 | ||
06d40de8 DG |
15740 | /* Returns true if ARGS (a collection of template arguments) contains |
15741 | any types that require structural equality testing. */ | |
15742 | ||
15743 | bool | |
15744 | any_template_arguments_need_structural_equality_p (tree args) | |
15745 | { | |
15746 | int i; | |
15747 | int j; | |
15748 | ||
15749 | if (!args) | |
15750 | return false; | |
15751 | if (args == error_mark_node) | |
15752 | return true; | |
15753 | ||
15754 | for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i) | |
15755 | { | |
15756 | tree level = TMPL_ARGS_LEVEL (args, i + 1); | |
15757 | for (j = 0; j < TREE_VEC_LENGTH (level); ++j) | |
15758 | { | |
15759 | tree arg = TREE_VEC_ELT (level, j); | |
5d80a306 DG |
15760 | tree packed_args = NULL_TREE; |
15761 | int k, len = 1; | |
15762 | ||
15763 | if (ARGUMENT_PACK_P (arg)) | |
15764 | { | |
15765 | /* Look inside the argument pack. */ | |
15766 | packed_args = ARGUMENT_PACK_ARGS (arg); | |
15767 | len = TREE_VEC_LENGTH (packed_args); | |
15768 | } | |
15769 | ||
15770 | for (k = 0; k < len; ++k) | |
15771 | { | |
15772 | if (packed_args) | |
15773 | arg = TREE_VEC_ELT (packed_args, k); | |
15774 | ||
15775 | if (error_operand_p (arg)) | |
15776 | return true; | |
15777 | else if (TREE_CODE (arg) == TEMPLATE_DECL | |
15778 | || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM) | |
15779 | continue; | |
15780 | else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg)) | |
15781 | return true; | |
15782 | else if (!TYPE_P (arg) && TREE_TYPE (arg) | |
15783 | && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg))) | |
15784 | return true; | |
15785 | } | |
06d40de8 DG |
15786 | } |
15787 | } | |
15788 | ||
15789 | return false; | |
15790 | } | |
15791 | ||
d17811fd MM |
15792 | /* Returns true if ARGS (a collection of template arguments) contains |
15793 | any dependent arguments. */ | |
1fb3244a | 15794 | |
d17811fd | 15795 | bool |
58f9752a | 15796 | any_dependent_template_arguments_p (const_tree args) |
1fb3244a | 15797 | { |
bf12d54d | 15798 | int i; |
c353b8e3 MM |
15799 | int j; |
15800 | ||
d17811fd MM |
15801 | if (!args) |
15802 | return false; | |
7e497d0c VR |
15803 | if (args == error_mark_node) |
15804 | return true; | |
d17811fd | 15805 | |
c353b8e3 MM |
15806 | for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i) |
15807 | { | |
58f9752a | 15808 | const_tree level = TMPL_ARGS_LEVEL (args, i + 1); |
c353b8e3 MM |
15809 | for (j = 0; j < TREE_VEC_LENGTH (level); ++j) |
15810 | if (dependent_template_arg_p (TREE_VEC_ELT (level, j))) | |
15811 | return true; | |
15812 | } | |
1fb3244a | 15813 | |
1fb3244a MM |
15814 | return false; |
15815 | } | |
15816 | ||
15817 | /* Returns TRUE if the template TMPL is dependent. */ | |
15818 | ||
15819 | bool | |
15820 | dependent_template_p (tree tmpl) | |
15821 | { | |
b95cc51a MM |
15822 | if (TREE_CODE (tmpl) == OVERLOAD) |
15823 | { | |
15824 | while (tmpl) | |
15825 | { | |
15826 | if (dependent_template_p (OVL_FUNCTION (tmpl))) | |
15827 | return true; | |
15828 | tmpl = OVL_CHAIN (tmpl); | |
15829 | } | |
15830 | return false; | |
15831 | } | |
15832 | ||
1fb3244a MM |
15833 | /* Template template parameters are dependent. */ |
15834 | if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl) | |
15835 | || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM) | |
15836 | return true; | |
27ab0504 | 15837 | /* So are names that have not been looked up. */ |
acccf788 MM |
15838 | if (TREE_CODE (tmpl) == SCOPE_REF |
15839 | || TREE_CODE (tmpl) == IDENTIFIER_NODE) | |
d17811fd | 15840 | return true; |
1fb3244a MM |
15841 | /* So are member templates of dependent classes. */ |
15842 | if (TYPE_P (CP_DECL_CONTEXT (tmpl))) | |
15843 | return dependent_type_p (DECL_CONTEXT (tmpl)); | |
15844 | return false; | |
15845 | } | |
15846 | ||
d17811fd MM |
15847 | /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */ |
15848 | ||
15849 | bool | |
15850 | dependent_template_id_p (tree tmpl, tree args) | |
15851 | { | |
15852 | return (dependent_template_p (tmpl) | |
15853 | || any_dependent_template_arguments_p (args)); | |
15854 | } | |
15855 | ||
14d22dd6 | 15856 | /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the |
4195a767 NS |
15857 | TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if |
15858 | no such TYPE can be found. Note that this function peers inside | |
15859 | uninstantiated templates and therefore should be used only in | |
15860 | extremely limited situations. ONLY_CURRENT_P restricts this | |
15861 | peering to the currently open classes hierarchy (which is required | |
15862 | when comparing types). */ | |
14d22dd6 MM |
15863 | |
15864 | tree | |
15865 | resolve_typename_type (tree type, bool only_current_p) | |
15866 | { | |
15867 | tree scope; | |
15868 | tree name; | |
15869 | tree decl; | |
15870 | int quals; | |
4514aa8c | 15871 | tree pushed_scope; |
4195a767 | 15872 | tree result; |
14d22dd6 | 15873 | |
50bc768d | 15874 | gcc_assert (TREE_CODE (type) == TYPENAME_TYPE); |
14d22dd6 MM |
15875 | |
15876 | scope = TYPE_CONTEXT (type); | |
15877 | name = TYPE_IDENTIFIER (type); | |
15878 | ||
15879 | /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve | |
15880 | it first before we can figure out what NAME refers to. */ | |
15881 | if (TREE_CODE (scope) == TYPENAME_TYPE) | |
15882 | scope = resolve_typename_type (scope, only_current_p); | |
15883 | /* If we don't know what SCOPE refers to, then we cannot resolve the | |
15884 | TYPENAME_TYPE. */ | |
4195a767 NS |
15885 | if (TREE_CODE (scope) == TYPENAME_TYPE) |
15886 | return type; | |
14d22dd6 MM |
15887 | /* If the SCOPE is a template type parameter, we have no way of |
15888 | resolving the name. */ | |
15889 | if (TREE_CODE (scope) == TEMPLATE_TYPE_PARM) | |
15890 | return type; | |
15891 | /* If the SCOPE is not the current instantiation, there's no reason | |
15892 | to look inside it. */ | |
15893 | if (only_current_p && !currently_open_class (scope)) | |
4195a767 | 15894 | return type; |
7a20d689 JM |
15895 | /* If SCOPE isn't the template itself, it will not have a valid |
15896 | TYPE_FIELDS list. */ | |
15897 | if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope))) | |
15898 | /* scope is either the template itself or a compatible instantiation | |
15899 | like X<T>, so look up the name in the original template. */ | |
15900 | scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope); | |
15901 | else | |
15902 | /* scope is a partial instantiation, so we can't do the lookup or we | |
15903 | will lose the template arguments. */ | |
15904 | return type; | |
14d22dd6 MM |
15905 | /* Enter the SCOPE so that name lookup will be resolved as if we |
15906 | were in the class definition. In particular, SCOPE will no | |
15907 | longer be considered a dependent type. */ | |
4514aa8c | 15908 | pushed_scope = push_scope (scope); |
14d22dd6 | 15909 | /* Look up the declaration. */ |
86ac0575 | 15910 | decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true); |
4195a767 NS |
15911 | |
15912 | result = NULL_TREE; | |
15913 | ||
14d22dd6 MM |
15914 | /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to |
15915 | find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */ | |
15916 | if (!decl) | |
4195a767 | 15917 | /*nop*/; |
14d22dd6 MM |
15918 | else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == IDENTIFIER_NODE |
15919 | && TREE_CODE (decl) == TYPE_DECL) | |
4195a767 NS |
15920 | { |
15921 | result = TREE_TYPE (decl); | |
15922 | if (result == error_mark_node) | |
15923 | result = NULL_TREE; | |
15924 | } | |
14d22dd6 MM |
15925 | else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == TEMPLATE_ID_EXPR |
15926 | && DECL_CLASS_TEMPLATE_P (decl)) | |
15927 | { | |
15928 | tree tmpl; | |
15929 | tree args; | |
15930 | /* Obtain the template and the arguments. */ | |
15931 | tmpl = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 0); | |
15932 | args = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 1); | |
15933 | /* Instantiate the template. */ | |
4195a767 NS |
15934 | result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE, |
15935 | /*entering_scope=*/0, | |
15936 | tf_error | tf_user); | |
15937 | if (result == error_mark_node) | |
15938 | result = NULL_TREE; | |
14d22dd6 | 15939 | } |
4195a767 | 15940 | |
14d22dd6 | 15941 | /* Leave the SCOPE. */ |
4514aa8c NS |
15942 | if (pushed_scope) |
15943 | pop_scope (pushed_scope); | |
14d22dd6 | 15944 | |
4195a767 NS |
15945 | /* If we failed to resolve it, return the original typename. */ |
15946 | if (!result) | |
15947 | return type; | |
15948 | ||
15949 | /* If lookup found a typename type, resolve that too. */ | |
15950 | if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result)) | |
15951 | { | |
15952 | /* Ill-formed programs can cause infinite recursion here, so we | |
15953 | must catch that. */ | |
15954 | TYPENAME_IS_RESOLVING_P (type) = 1; | |
15955 | result = resolve_typename_type (result, only_current_p); | |
15956 | TYPENAME_IS_RESOLVING_P (type) = 0; | |
15957 | } | |
15958 | ||
15959 | /* Qualify the resulting type. */ | |
15960 | quals = cp_type_quals (type); | |
15961 | if (quals) | |
15962 | result = cp_build_qualified_type (result, cp_type_quals (result) | quals); | |
15963 | ||
15964 | return result; | |
14d22dd6 MM |
15965 | } |
15966 | ||
d17811fd MM |
15967 | /* EXPR is an expression which is not type-dependent. Return a proxy |
15968 | for EXPR that can be used to compute the types of larger | |
15969 | expressions containing EXPR. */ | |
15970 | ||
15971 | tree | |
15972 | build_non_dependent_expr (tree expr) | |
15973 | { | |
0deb916c MM |
15974 | tree inner_expr; |
15975 | ||
c8094d83 | 15976 | /* Preserve null pointer constants so that the type of things like |
d17811fd MM |
15977 | "p == 0" where "p" is a pointer can be determined. */ |
15978 | if (null_ptr_cst_p (expr)) | |
15979 | return expr; | |
15980 | /* Preserve OVERLOADs; the functions must be available to resolve | |
15981 | types. */ | |
c497db75 | 15982 | inner_expr = expr; |
c2acde1e JM |
15983 | if (TREE_CODE (inner_expr) == STMT_EXPR) |
15984 | inner_expr = stmt_expr_value_expr (inner_expr); | |
c497db75 AO |
15985 | if (TREE_CODE (inner_expr) == ADDR_EXPR) |
15986 | inner_expr = TREE_OPERAND (inner_expr, 0); | |
15987 | if (TREE_CODE (inner_expr) == COMPONENT_REF) | |
15988 | inner_expr = TREE_OPERAND (inner_expr, 1); | |
2226e997 | 15989 | if (is_overloaded_fn (inner_expr) |
6439fffd | 15990 | || TREE_CODE (inner_expr) == OFFSET_REF) |
d17811fd | 15991 | return expr; |
5ae9ba3e MM |
15992 | /* There is no need to return a proxy for a variable. */ |
15993 | if (TREE_CODE (expr) == VAR_DECL) | |
15994 | return expr; | |
7433e6d4 MM |
15995 | /* Preserve string constants; conversions from string constants to |
15996 | "char *" are allowed, even though normally a "const char *" | |
15997 | cannot be used to initialize a "char *". */ | |
15998 | if (TREE_CODE (expr) == STRING_CST) | |
15999 | return expr; | |
b7c707d1 MM |
16000 | /* Preserve arithmetic constants, as an optimization -- there is no |
16001 | reason to create a new node. */ | |
16002 | if (TREE_CODE (expr) == INTEGER_CST || TREE_CODE (expr) == REAL_CST) | |
16003 | return expr; | |
9b7be7b5 MM |
16004 | /* Preserve THROW_EXPRs -- all throw-expressions have type "void". |
16005 | There is at least one place where we want to know that a | |
16006 | particular expression is a throw-expression: when checking a ?: | |
16007 | expression, there are special rules if the second or third | |
878cbb73 | 16008 | argument is a throw-expression. */ |
9b7be7b5 MM |
16009 | if (TREE_CODE (expr) == THROW_EXPR) |
16010 | return expr; | |
47d4c811 NS |
16011 | |
16012 | if (TREE_CODE (expr) == COND_EXPR) | |
f293ce4b RS |
16013 | return build3 (COND_EXPR, |
16014 | TREE_TYPE (expr), | |
16015 | TREE_OPERAND (expr, 0), | |
c8094d83 | 16016 | (TREE_OPERAND (expr, 1) |
f293ce4b RS |
16017 | ? build_non_dependent_expr (TREE_OPERAND (expr, 1)) |
16018 | : build_non_dependent_expr (TREE_OPERAND (expr, 0))), | |
16019 | build_non_dependent_expr (TREE_OPERAND (expr, 2))); | |
8e1daa34 NS |
16020 | if (TREE_CODE (expr) == COMPOUND_EXPR |
16021 | && !COMPOUND_EXPR_OVERLOADED (expr)) | |
f293ce4b RS |
16022 | return build2 (COMPOUND_EXPR, |
16023 | TREE_TYPE (expr), | |
16024 | TREE_OPERAND (expr, 0), | |
16025 | build_non_dependent_expr (TREE_OPERAND (expr, 1))); | |
c8094d83 | 16026 | |
fdeff563 NS |
16027 | /* If the type is unknown, it can't really be non-dependent */ |
16028 | gcc_assert (TREE_TYPE (expr) != unknown_type_node); | |
3db45ab5 | 16029 | |
c8094d83 | 16030 | /* Otherwise, build a NON_DEPENDENT_EXPR. |
d17811fd MM |
16031 | |
16032 | REFERENCE_TYPEs are not stripped for expressions in templates | |
16033 | because doing so would play havoc with mangling. Consider, for | |
16034 | example: | |
16035 | ||
c8094d83 | 16036 | template <typename T> void f<T& g>() { g(); } |
d17811fd MM |
16037 | |
16038 | In the body of "f", the expression for "g" will have | |
16039 | REFERENCE_TYPE, even though the standard says that it should | |
16040 | not. The reason is that we must preserve the syntactic form of | |
16041 | the expression so that mangling (say) "f<g>" inside the body of | |
16042 | "f" works out correctly. Therefore, the REFERENCE_TYPE is | |
16043 | stripped here. */ | |
018a5803 | 16044 | return build1 (NON_DEPENDENT_EXPR, non_reference (TREE_TYPE (expr)), expr); |
d17811fd MM |
16045 | } |
16046 | ||
16047 | /* ARGS is a TREE_LIST of expressions as arguments to a function call. | |
16048 | Return a new TREE_LIST with the various arguments replaced with | |
16049 | equivalent non-dependent expressions. */ | |
16050 | ||
16051 | tree | |
16052 | build_non_dependent_args (tree args) | |
16053 | { | |
16054 | tree a; | |
16055 | tree new_args; | |
16056 | ||
16057 | new_args = NULL_TREE; | |
16058 | for (a = args; a; a = TREE_CHAIN (a)) | |
c8094d83 | 16059 | new_args = tree_cons (NULL_TREE, |
d17811fd MM |
16060 | build_non_dependent_expr (TREE_VALUE (a)), |
16061 | new_args); | |
16062 | return nreverse (new_args); | |
16063 | } | |
16064 | ||
e2500fed | 16065 | #include "gt-cp-pt.h" |