]>
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, |
4514aa8c | 3 | 2001, 2002, 2003, 2004, 2005 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 MS |
10 | it under the terms of the GNU General Public License as published by |
11 | the Free Software Foundation; either version 2, or (at your option) | |
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 | |
f5adbb8d | 20 | along with GCC; see the file COPYING. If not, write to |
1788952f KC |
21 | the Free Software Foundation, 51 Franklin Street, Fifth Floor, |
22 | Boston, MA 02110-1301, USA. */ | |
8d08fdba MS |
23 | |
24 | /* Known bugs or deficiencies include: | |
e92cc029 | 25 | |
e92cc029 MS |
26 | all methods must be provided in header files; can't use a source |
27 | file that contains only the method templates and "just win". */ | |
8d08fdba MS |
28 | |
29 | #include "config.h" | |
8d052bc7 | 30 | #include "system.h" |
4977bab6 ZW |
31 | #include "coretypes.h" |
32 | #include "tm.h" | |
8d08fdba | 33 | #include "obstack.h" |
8d08fdba | 34 | #include "tree.h" |
0c58f841 | 35 | #include "pointer-set.h" |
8d08fdba | 36 | #include "flags.h" |
e58a9aa1 | 37 | #include "c-common.h" |
8d08fdba | 38 | #include "cp-tree.h" |
e58a9aa1 | 39 | #include "cp-objcp-common.h" |
25af8512 | 40 | #include "tree-inline.h" |
8d08fdba | 41 | #include "decl.h" |
e8abc66f | 42 | #include "output.h" |
49c249e1 | 43 | #include "except.h" |
54f92bfb | 44 | #include "toplev.h" |
3dcaad8b | 45 | #include "rtl.h" |
297a5329 | 46 | #include "timevar.h" |
325c3691 | 47 | #include "tree-iterator.h" |
53824026 | 48 | #include "vecprim.h" |
49c249e1 | 49 | |
050367a3 MM |
50 | /* The type of functions taking a tree, and some additional data, and |
51 | returning an int. */ | |
3a978d72 | 52 | typedef int (*tree_fn_t) (tree, void*); |
050367a3 | 53 | |
0aafb128 MM |
54 | /* The PENDING_TEMPLATES is a TREE_LIST of templates whose |
55 | instantiations have been deferred, either because their definitions | |
d78e771d ZW |
56 | were not yet available, or because we were putting off doing the work. |
57 | The TREE_PURPOSE of each entry is either a DECL (for a function or | |
58 | static data member), or a TYPE (for a class) indicating what we are | |
59 | hoping to instantiate. The TREE_VALUE is not used. */ | |
e2500fed | 60 | static GTY(()) tree pending_templates; |
13e73b29 | 61 | static GTY(()) tree last_pending_template; |
73aad9b9 | 62 | |
67ffc812 | 63 | int processing_template_parmlist; |
386b8a85 JM |
64 | static int template_header_count; |
65 | ||
e2500fed | 66 | static GTY(()) tree saved_trees; |
53824026 | 67 | static VEC(int,heap) *inline_parm_levels; |
75650646 | 68 | |
e2500fed | 69 | static GTY(()) tree current_tinst_level; |
3ae18eaf | 70 | |
2b59fc25 KL |
71 | static GTY(()) tree saved_access_scope; |
72 | ||
0fe0caa6 RH |
73 | /* Live only within one (recursive) call to tsubst_expr. We use |
74 | this to pass the statement expression node from the STMT_EXPR | |
75 | to the EXPR_STMT that is its result. */ | |
76 | static tree cur_stmt_expr; | |
77 | ||
6dfbb909 MM |
78 | /* A map from local variable declarations in the body of the template |
79 | presently being instantiated to the corresponding instantiated | |
80 | local variables. */ | |
81 | static htab_t local_specializations; | |
82 | ||
410cf6e6 | 83 | /* Contains canonical template parameter types. The vector is indexed by |
06d40de8 DG |
84 | the TEMPLATE_TYPE_IDX of the template parameter. Each element is a |
85 | TREE_LIST, whose TREE_VALUEs contain the canonical template | |
86 | parameters of various types and levels. */ | |
87 | static GTY(()) VEC(tree,gc) *canonical_template_parms; | |
88 | ||
830bfa74 MM |
89 | #define UNIFY_ALLOW_NONE 0 |
90 | #define UNIFY_ALLOW_MORE_CV_QUAL 1 | |
91 | #define UNIFY_ALLOW_LESS_CV_QUAL 2 | |
92 | #define UNIFY_ALLOW_DERIVED 4 | |
161c12b0 | 93 | #define UNIFY_ALLOW_INTEGER 8 |
028d1f20 | 94 | #define UNIFY_ALLOW_OUTER_LEVEL 16 |
62e4a758 NS |
95 | #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32 |
96 | #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64 | |
830bfa74 | 97 | |
3a978d72 NN |
98 | static void push_access_scope (tree); |
99 | static void pop_access_scope (tree); | |
100 | static int resolve_overloaded_unification (tree, tree, tree, tree, | |
101 | unification_kind_t, int); | |
102 | static int try_one_overload (tree, tree, tree, tree, tree, | |
f23fb7f5 | 103 | unification_kind_t, int, bool); |
3a978d72 NN |
104 | static int unify (tree, tree, tree, tree, int); |
105 | static void add_pending_template (tree); | |
aa9d8196 VR |
106 | static int push_tinst_level (tree); |
107 | static void pop_tinst_level (void); | |
3a978d72 | 108 | static void reopen_tinst_level (tree); |
3a978d72 | 109 | static tree tsubst_initializer_list (tree, tree); |
3a978d72 | 110 | static tree get_class_bindings (tree, tree, tree); |
3db45ab5 | 111 | static tree coerce_template_parms (tree, tree, tree, tsubst_flags_t, |
e7e93965 | 112 | bool, bool); |
3a978d72 NN |
113 | static void tsubst_enum (tree, tree, tree); |
114 | static tree add_to_template_args (tree, tree); | |
115 | static tree add_outermost_template_args (tree, tree); | |
116 | static bool check_instantiated_args (tree, tree, tsubst_flags_t); | |
c8094d83 | 117 | static int maybe_adjust_types_for_deduction (unification_kind_t, tree*, tree*); |
3a978d72 | 118 | static int type_unification_real (tree, tree, tree, tree, |
30f86ec3 | 119 | int, unification_kind_t, int); |
3a978d72 | 120 | static void note_template_header (int); |
b6ab6892 | 121 | static tree convert_nontype_argument_function (tree, tree); |
3a978d72 NN |
122 | static tree convert_nontype_argument (tree, tree); |
123 | static tree convert_template_argument (tree, tree, tree, | |
124 | tsubst_flags_t, int, tree); | |
0c58f841 MA |
125 | static int for_each_template_parm (tree, tree_fn_t, void*, |
126 | struct pointer_set_t*); | |
5d80a306 | 127 | static tree expand_template_argument_pack (tree); |
3a978d72 NN |
128 | static tree build_template_parm_index (int, int, int, tree, tree); |
129 | static int inline_needs_template_parms (tree); | |
130 | static void push_inline_template_parms_recursive (tree, int); | |
3a978d72 | 131 | static tree retrieve_local_specialization (tree); |
3a978d72 | 132 | static void register_local_specialization (tree, tree); |
3a978d72 | 133 | static tree reduce_template_parm_level (tree, tree, int); |
3a978d72 NN |
134 | static int mark_template_parm (tree, void *); |
135 | static int template_parm_this_level_p (tree, void *); | |
136 | static tree tsubst_friend_function (tree, tree); | |
137 | static tree tsubst_friend_class (tree, tree); | |
138 | static int can_complete_type_without_circularity (tree); | |
a34d3336 | 139 | static tree get_bindings (tree, tree, tree, bool); |
3a978d72 NN |
140 | static int template_decl_level (tree); |
141 | static int check_cv_quals_for_unify (int, tree, tree); | |
5d80a306 DG |
142 | static void template_parm_level_and_index (tree, int*, int*); |
143 | static int unify_pack_expansion (tree, tree, tree, tree, int, bool, bool); | |
a91db711 NS |
144 | static tree tsubst_template_arg (tree, tree, tsubst_flags_t, tree); |
145 | static tree tsubst_template_args (tree, tree, tsubst_flags_t, tree); | |
3a978d72 NN |
146 | static tree tsubst_template_parms (tree, tree, tsubst_flags_t); |
147 | static void regenerate_decl_from_template (tree, tree); | |
3a978d72 | 148 | static tree most_specialized_class (tree, tree); |
3a978d72 | 149 | static tree tsubst_aggr_type (tree, tree, tsubst_flags_t, tree, int); |
3a978d72 NN |
150 | static tree tsubst_arg_types (tree, tree, tsubst_flags_t, tree); |
151 | static tree tsubst_function_type (tree, tree, tsubst_flags_t, tree); | |
9b910171 | 152 | static bool check_specialization_scope (void); |
3a978d72 NN |
153 | static tree process_partial_specialization (tree); |
154 | static void set_current_access_from_decl (tree); | |
155 | static void check_default_tmpl_args (tree, tree, int, int); | |
3a978d72 | 156 | static tree get_template_base (tree, tree, tree, tree); |
3a978d72 NN |
157 | static tree try_class_unification (tree, tree, tree, tree); |
158 | static int coerce_template_template_parms (tree, tree, tsubst_flags_t, | |
159 | tree, tree); | |
3a978d72 NN |
160 | static int template_args_equal (tree, tree); |
161 | static void tsubst_default_arguments (tree); | |
162 | static tree for_each_template_parm_r (tree *, int *, void *); | |
163 | static tree copy_default_args_to_explicit_spec_1 (tree, tree); | |
164 | static void copy_default_args_to_explicit_spec (tree); | |
165 | static int invalid_nontype_parm_type_p (tree, tsubst_flags_t); | |
a723baf1 | 166 | static int eq_local_specializations (const void *, const void *); |
5d80a306 | 167 | static bool dependent_template_arg_p (tree); |
06d40de8 | 168 | static bool any_template_arguments_need_structural_equality_p (tree); |
5552b43c | 169 | static bool dependent_type_p_r (tree); |
14d22dd6 | 170 | static tree tsubst (tree, tree, tsubst_flags_t, tree); |
015c2c66 | 171 | static tree tsubst_expr (tree, tree, tsubst_flags_t, tree, bool); |
14d22dd6 | 172 | static tree tsubst_copy (tree, tree, tsubst_flags_t, tree); |
5d80a306 | 173 | static tree tsubst_pack_expansion (tree, tree, tsubst_flags_t, tree); |
36a117a5 | 174 | |
2b59fc25 KL |
175 | /* Make the current scope suitable for access checking when we are |
176 | processing T. T can be FUNCTION_DECL for instantiated function | |
2b907f5c KL |
177 | template, or VAR_DECL for static member variable (need by |
178 | instantiate_decl). */ | |
2b59fc25 | 179 | |
8ce33230 | 180 | static void |
2b907f5c | 181 | push_access_scope (tree t) |
2b59fc25 | 182 | { |
50bc768d NS |
183 | gcc_assert (TREE_CODE (t) == FUNCTION_DECL |
184 | || TREE_CODE (t) == VAR_DECL); | |
2b59fc25 | 185 | |
23ff7e2d KL |
186 | if (DECL_FRIEND_CONTEXT (t)) |
187 | push_nested_class (DECL_FRIEND_CONTEXT (t)); | |
188 | else if (DECL_CLASS_SCOPE_P (t)) | |
2b907f5c | 189 | push_nested_class (DECL_CONTEXT (t)); |
0f399e5f KL |
190 | else |
191 | push_to_top_level (); | |
c8094d83 | 192 | |
2b907f5c | 193 | if (TREE_CODE (t) == FUNCTION_DECL) |
0f399e5f KL |
194 | { |
195 | saved_access_scope = tree_cons | |
196 | (NULL_TREE, current_function_decl, saved_access_scope); | |
197 | current_function_decl = t; | |
198 | } | |
2b59fc25 KL |
199 | } |
200 | ||
2b59fc25 KL |
201 | /* Restore the scope set up by push_access_scope. T is the node we |
202 | are processing. */ | |
203 | ||
8ce33230 | 204 | static void |
3a978d72 | 205 | pop_access_scope (tree t) |
2b59fc25 | 206 | { |
2b907f5c | 207 | if (TREE_CODE (t) == FUNCTION_DECL) |
2b59fc25 KL |
208 | { |
209 | current_function_decl = TREE_VALUE (saved_access_scope); | |
210 | saved_access_scope = TREE_CHAIN (saved_access_scope); | |
211 | } | |
0f399e5f | 212 | |
23ff7e2d | 213 | if (DECL_FRIEND_CONTEXT (t) || DECL_CLASS_SCOPE_P (t)) |
0f399e5f KL |
214 | pop_nested_class (); |
215 | else | |
216 | pop_from_top_level (); | |
2b59fc25 KL |
217 | } |
218 | ||
a723baf1 MM |
219 | /* Do any processing required when DECL (a member template |
220 | declaration) is finished. Returns the TEMPLATE_DECL corresponding | |
221 | to DECL, unless it is a specialization, in which case the DECL | |
222 | itself is returned. */ | |
e1467ff2 MM |
223 | |
224 | tree | |
3a978d72 | 225 | finish_member_template_decl (tree decl) |
e1467ff2 | 226 | { |
a723baf1 MM |
227 | if (decl == error_mark_node) |
228 | return error_mark_node; | |
229 | ||
50bc768d | 230 | gcc_assert (DECL_P (decl)); |
a723baf1 MM |
231 | |
232 | if (TREE_CODE (decl) == TYPE_DECL) | |
93cdc044 | 233 | { |
a723baf1 MM |
234 | tree type; |
235 | ||
236 | type = TREE_TYPE (decl); | |
c8094d83 | 237 | if (IS_AGGR_TYPE (type) |
a723baf1 MM |
238 | && CLASSTYPE_TEMPLATE_INFO (type) |
239 | && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type)) | |
93cdc044 | 240 | { |
a723baf1 | 241 | tree tmpl = CLASSTYPE_TI_TEMPLATE (type); |
93cdc044 JM |
242 | check_member_template (tmpl); |
243 | return tmpl; | |
244 | } | |
8d019cef | 245 | return NULL_TREE; |
93cdc044 | 246 | } |
07c88314 | 247 | else if (TREE_CODE (decl) == FIELD_DECL) |
0f51ccfc | 248 | error ("data member %qD cannot be a member template", decl); |
a1da6cba | 249 | else if (DECL_TEMPLATE_INFO (decl)) |
e1467ff2 | 250 | { |
a1da6cba MM |
251 | if (!DECL_TEMPLATE_SPECIALIZATION (decl)) |
252 | { | |
253 | check_member_template (DECL_TI_TEMPLATE (decl)); | |
254 | return DECL_TI_TEMPLATE (decl); | |
255 | } | |
256 | else | |
257 | return decl; | |
c8094d83 | 258 | } |
a1da6cba | 259 | else |
0f51ccfc | 260 | error ("invalid member template declaration %qD", decl); |
e1467ff2 | 261 | |
a1da6cba | 262 | return error_mark_node; |
f84b4be9 | 263 | } |
e1467ff2 | 264 | |
f84b4be9 | 265 | /* Returns the template nesting level of the indicated class TYPE. |
c8094d83 | 266 | |
f84b4be9 JM |
267 | For example, in: |
268 | template <class T> | |
269 | struct A | |
270 | { | |
271 | template <class U> | |
272 | struct B {}; | |
273 | }; | |
274 | ||
c8094d83 | 275 | A<T>::B<U> has depth two, while A<T> has depth one. |
39c01e4c | 276 | Both A<T>::B<int> and A<int>::B<U> have depth one, if |
260cd73f | 277 | they are instantiations, not specializations. |
39c01e4c MM |
278 | |
279 | This function is guaranteed to return 0 if passed NULL_TREE so | |
280 | that, for example, `template_class_depth (current_class_type)' is | |
281 | always safe. */ | |
f84b4be9 | 282 | |
260cd73f VR |
283 | int |
284 | template_class_depth (tree type) | |
f84b4be9 | 285 | { |
93cdc044 | 286 | int depth; |
f84b4be9 | 287 | |
c8094d83 | 288 | for (depth = 0; |
ed44da02 | 289 | type && TREE_CODE (type) != NAMESPACE_DECL; |
c8094d83 | 290 | type = (TREE_CODE (type) == FUNCTION_DECL) |
4f1c5b7d | 291 | ? CP_DECL_CONTEXT (type) : TYPE_CONTEXT (type)) |
ed44da02 MM |
292 | { |
293 | if (TREE_CODE (type) != FUNCTION_DECL) | |
294 | { | |
295 | if (CLASSTYPE_TEMPLATE_INFO (type) | |
296 | && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)) | |
260cd73f | 297 | && uses_template_parms (CLASSTYPE_TI_ARGS (type))) |
ed44da02 MM |
298 | ++depth; |
299 | } | |
c8094d83 | 300 | else |
ed44da02 MM |
301 | { |
302 | if (DECL_TEMPLATE_INFO (type) | |
303 | && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (type)) | |
260cd73f | 304 | && uses_template_parms (DECL_TI_ARGS (type))) |
ed44da02 MM |
305 | ++depth; |
306 | } | |
307 | } | |
f84b4be9 JM |
308 | |
309 | return depth; | |
e1467ff2 | 310 | } |
98c1c668 | 311 | |
cae40af6 JM |
312 | /* Returns 1 if processing DECL as part of do_pending_inlines |
313 | needs us to push template parms. */ | |
314 | ||
315 | static int | |
3a978d72 | 316 | inline_needs_template_parms (tree decl) |
cae40af6 JM |
317 | { |
318 | if (! DECL_TEMPLATE_INFO (decl)) | |
319 | return 0; | |
f84b4be9 | 320 | |
36a117a5 | 321 | return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl))) |
cae40af6 JM |
322 | > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl))); |
323 | } | |
324 | ||
325 | /* Subroutine of maybe_begin_member_template_processing. | |
326 | Push the template parms in PARMS, starting from LEVELS steps into the | |
327 | chain, and ending at the beginning, since template parms are listed | |
328 | innermost first. */ | |
329 | ||
330 | static void | |
3a978d72 | 331 | push_inline_template_parms_recursive (tree parmlist, int levels) |
cae40af6 JM |
332 | { |
333 | tree parms = TREE_VALUE (parmlist); | |
334 | int i; | |
335 | ||
336 | if (levels > 1) | |
337 | push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1); | |
786b5245 | 338 | |
98c1c668 | 339 | ++processing_template_decl; |
cae40af6 | 340 | current_template_parms |
4890c2f4 | 341 | = tree_cons (size_int (processing_template_decl), |
98c1c668 | 342 | parms, current_template_parms); |
cae40af6 JM |
343 | TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1; |
344 | ||
ac20c67a | 345 | begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec, |
0cbd7506 | 346 | NULL); |
c8094d83 | 347 | for (i = 0; i < TREE_VEC_LENGTH (parms); ++i) |
98c1c668 | 348 | { |
0f67a82f | 349 | tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i)); |
42b304f1 | 350 | |
0f67a82f LM |
351 | if (parm == error_mark_node) |
352 | continue; | |
42b304f1 | 353 | |
50bc768d | 354 | gcc_assert (DECL_P (parm)); |
cae40af6 | 355 | |
98c1c668 JM |
356 | switch (TREE_CODE (parm)) |
357 | { | |
786b5245 | 358 | case TYPE_DECL: |
73b0fce8 | 359 | case TEMPLATE_DECL: |
98c1c668 JM |
360 | pushdecl (parm); |
361 | break; | |
786b5245 MM |
362 | |
363 | case PARM_DECL: | |
364 | { | |
fc03edb3 MM |
365 | /* Make a CONST_DECL as is done in process_template_parm. |
366 | It is ugly that we recreate this here; the original | |
367 | version built in process_template_parm is no longer | |
368 | available. */ | |
786b5245 MM |
369 | tree decl = build_decl (CONST_DECL, DECL_NAME (parm), |
370 | TREE_TYPE (parm)); | |
c727aa5e | 371 | DECL_ARTIFICIAL (decl) = 1; |
6de9cd9a DN |
372 | TREE_CONSTANT (decl) = 1; |
373 | TREE_INVARIANT (decl) = 1; | |
374 | TREE_READONLY (decl) = 1; | |
786b5245 | 375 | DECL_INITIAL (decl) = DECL_INITIAL (parm); |
cd9f6678 | 376 | SET_DECL_TEMPLATE_PARM_P (decl); |
786b5245 MM |
377 | pushdecl (decl); |
378 | } | |
cae40af6 | 379 | break; |
786b5245 | 380 | |
98c1c668 | 381 | default: |
315fb5db | 382 | gcc_unreachable (); |
98c1c668 JM |
383 | } |
384 | } | |
385 | } | |
386 | ||
cae40af6 JM |
387 | /* Restore the template parameter context for a member template or |
388 | a friend template defined in a class definition. */ | |
389 | ||
390 | void | |
3a978d72 | 391 | maybe_begin_member_template_processing (tree decl) |
cae40af6 JM |
392 | { |
393 | tree parms; | |
3dcaad8b | 394 | int levels = 0; |
cae40af6 | 395 | |
3dcaad8b MM |
396 | if (inline_needs_template_parms (decl)) |
397 | { | |
398 | parms = DECL_TEMPLATE_PARMS (most_general_template (decl)); | |
399 | levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl; | |
cae40af6 | 400 | |
3dcaad8b MM |
401 | if (DECL_TEMPLATE_SPECIALIZATION (decl)) |
402 | { | |
403 | --levels; | |
404 | parms = TREE_CHAIN (parms); | |
405 | } | |
cae40af6 | 406 | |
3dcaad8b | 407 | push_inline_template_parms_recursive (parms, levels); |
cae40af6 JM |
408 | } |
409 | ||
3dcaad8b MM |
410 | /* Remember how many levels of template parameters we pushed so that |
411 | we can pop them later. */ | |
53824026 | 412 | VEC_safe_push (int, heap, inline_parm_levels, levels); |
cae40af6 JM |
413 | } |
414 | ||
1875c2b7 | 415 | /* Undo the effects of maybe_begin_member_template_processing. */ |
98c1c668 | 416 | |
c8094d83 | 417 | void |
3a978d72 | 418 | maybe_end_member_template_processing (void) |
98c1c668 | 419 | { |
3dcaad8b | 420 | int i; |
53824026 | 421 | int last; |
3dcaad8b | 422 | |
53824026 | 423 | if (VEC_length (int, inline_parm_levels) == 0) |
98c1c668 JM |
424 | return; |
425 | ||
53824026 KH |
426 | last = VEC_pop (int, inline_parm_levels); |
427 | for (i = 0; i < last; ++i) | |
cae40af6 JM |
428 | { |
429 | --processing_template_decl; | |
430 | current_template_parms = TREE_CHAIN (current_template_parms); | |
431 | poplevel (0, 0, 0); | |
432 | } | |
98c1c668 JM |
433 | } |
434 | ||
36a117a5 | 435 | /* Return a new template argument vector which contains all of ARGS, |
f9a7ae04 | 436 | but has as its innermost set of arguments the EXTRA_ARGS. */ |
e6f1275f JM |
437 | |
438 | static tree | |
3a978d72 | 439 | add_to_template_args (tree args, tree extra_args) |
e6f1275f | 440 | { |
36a117a5 MM |
441 | tree new_args; |
442 | int extra_depth; | |
443 | int i; | |
444 | int j; | |
e6f1275f | 445 | |
36a117a5 | 446 | extra_depth = TMPL_ARGS_DEPTH (extra_args); |
f31c0a32 | 447 | new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth); |
e6f1275f | 448 | |
36a117a5 MM |
449 | for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i) |
450 | SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i)); | |
e6f1275f | 451 | |
36a117a5 MM |
452 | for (j = 1; j <= extra_depth; ++j, ++i) |
453 | SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j)); | |
c8094d83 | 454 | |
e6f1275f JM |
455 | return new_args; |
456 | } | |
457 | ||
36a117a5 MM |
458 | /* Like add_to_template_args, but only the outermost ARGS are added to |
459 | the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH | |
460 | (EXTRA_ARGS) levels are added. This function is used to combine | |
461 | the template arguments from a partial instantiation with the | |
462 | template arguments used to attain the full instantiation from the | |
463 | partial instantiation. */ | |
98c1c668 | 464 | |
4966381a | 465 | static tree |
3a978d72 | 466 | add_outermost_template_args (tree args, tree extra_args) |
98c1c668 JM |
467 | { |
468 | tree new_args; | |
469 | ||
e4a84209 MM |
470 | /* If there are more levels of EXTRA_ARGS than there are ARGS, |
471 | something very fishy is going on. */ | |
50bc768d | 472 | gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args)); |
e4a84209 MM |
473 | |
474 | /* If *all* the new arguments will be the EXTRA_ARGS, just return | |
475 | them. */ | |
476 | if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args)) | |
477 | return extra_args; | |
478 | ||
36a117a5 MM |
479 | /* For the moment, we make ARGS look like it contains fewer levels. */ |
480 | TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args); | |
c8094d83 | 481 | |
36a117a5 | 482 | new_args = add_to_template_args (args, extra_args); |
98c1c668 | 483 | |
36a117a5 MM |
484 | /* Now, we restore ARGS to its full dimensions. */ |
485 | TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args); | |
98c1c668 JM |
486 | |
487 | return new_args; | |
488 | } | |
5566b478 | 489 | |
f9a7ae04 MM |
490 | /* Return the N levels of innermost template arguments from the ARGS. */ |
491 | ||
492 | tree | |
3a978d72 | 493 | get_innermost_template_args (tree args, int n) |
f9a7ae04 MM |
494 | { |
495 | tree new_args; | |
496 | int extra_levels; | |
497 | int i; | |
498 | ||
50bc768d | 499 | gcc_assert (n >= 0); |
f9a7ae04 MM |
500 | |
501 | /* If N is 1, just return the innermost set of template arguments. */ | |
502 | if (n == 1) | |
503 | return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args)); | |
c8094d83 | 504 | |
f9a7ae04 MM |
505 | /* If we're not removing anything, just return the arguments we were |
506 | given. */ | |
507 | extra_levels = TMPL_ARGS_DEPTH (args) - n; | |
50bc768d | 508 | gcc_assert (extra_levels >= 0); |
f9a7ae04 MM |
509 | if (extra_levels == 0) |
510 | return args; | |
511 | ||
512 | /* Make a new set of arguments, not containing the outer arguments. */ | |
513 | new_args = make_tree_vec (n); | |
514 | for (i = 1; i <= n; ++i) | |
c8094d83 | 515 | SET_TMPL_ARGS_LEVEL (new_args, i, |
f9a7ae04 MM |
516 | TMPL_ARGS_LEVEL (args, i + extra_levels)); |
517 | ||
518 | return new_args; | |
519 | } | |
520 | ||
5566b478 MS |
521 | /* We've got a template header coming up; push to a new level for storing |
522 | the parms. */ | |
8d08fdba | 523 | |
8d08fdba | 524 | void |
3a978d72 | 525 | begin_template_parm_list (void) |
8d08fdba | 526 | { |
6757edfe MM |
527 | /* We use a non-tag-transparent scope here, which causes pushtag to |
528 | put tags in this scope, rather than in the enclosing class or | |
529 | namespace scope. This is the right thing, since we want | |
530 | TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a | |
531 | global template class, push_template_decl handles putting the | |
532 | TEMPLATE_DECL into top-level scope. For a nested template class, | |
533 | e.g.: | |
534 | ||
535 | template <class T> struct S1 { | |
0cbd7506 | 536 | template <class T> struct S2 {}; |
6757edfe MM |
537 | }; |
538 | ||
539 | pushtag contains special code to call pushdecl_with_scope on the | |
540 | TEMPLATE_DECL for S2. */ | |
ac20c67a | 541 | begin_scope (sk_template_parms, NULL); |
5156628f | 542 | ++processing_template_decl; |
67ffc812 | 543 | ++processing_template_parmlist; |
386b8a85 JM |
544 | note_template_header (0); |
545 | } | |
546 | ||
6c30752f | 547 | /* This routine is called when a specialization is declared. If it is |
9b910171 LM |
548 | invalid to declare a specialization here, an error is reported and |
549 | false is returned, otherwise this routine will return true. */ | |
6c30752f | 550 | |
9b910171 | 551 | static bool |
3a978d72 | 552 | check_specialization_scope (void) |
6c30752f MM |
553 | { |
554 | tree scope = current_scope (); | |
3ddfb0e6 | 555 | |
c8094d83 MS |
556 | /* [temp.expl.spec] |
557 | ||
6c30752f MM |
558 | An explicit specialization shall be declared in the namespace of |
559 | which the template is a member, or, for member templates, in the | |
560 | namespace of which the enclosing class or enclosing class | |
561 | template is a member. An explicit specialization of a member | |
562 | function, member class or static data member of a class template | |
563 | shall be declared in the namespace of which the class template | |
564 | is a member. */ | |
565 | if (scope && TREE_CODE (scope) != NAMESPACE_DECL) | |
9b910171 LM |
566 | { |
567 | error ("explicit specialization in non-namespace scope %qD", scope); | |
568 | return false; | |
569 | } | |
3ddfb0e6 | 570 | |
c8094d83 | 571 | /* [temp.expl.spec] |
6c30752f MM |
572 | |
573 | In an explicit specialization declaration for a member of a class | |
574 | template or a member template that appears in namespace scope, | |
575 | the member template and some of its enclosing class templates may | |
576 | remain unspecialized, except that the declaration shall not | |
577 | explicitly specialize a class member template if its enclosing | |
578 | class templates are not explicitly specialized as well. */ | |
c8094d83 | 579 | if (current_template_parms) |
9b910171 LM |
580 | { |
581 | error ("enclosing class templates are not explicitly specialized"); | |
582 | return false; | |
583 | } | |
584 | ||
585 | return true; | |
6c30752f MM |
586 | } |
587 | ||
c6002625 | 588 | /* We've just seen template <>. */ |
386b8a85 | 589 | |
9b910171 | 590 | bool |
3a978d72 | 591 | begin_specialization (void) |
386b8a85 | 592 | { |
ac20c67a | 593 | begin_scope (sk_template_spec, NULL); |
386b8a85 | 594 | note_template_header (1); |
9b910171 | 595 | return check_specialization_scope (); |
386b8a85 JM |
596 | } |
597 | ||
dc957d14 | 598 | /* Called at then end of processing a declaration preceded by |
386b8a85 JM |
599 | template<>. */ |
600 | ||
c8094d83 | 601 | void |
3a978d72 | 602 | end_specialization (void) |
386b8a85 | 603 | { |
74b846e0 | 604 | finish_scope (); |
386b8a85 JM |
605 | reset_specialization (); |
606 | } | |
607 | ||
386b8a85 | 608 | /* Any template <>'s that we have seen thus far are not referring to a |
c6002625 | 609 | function specialization. */ |
386b8a85 JM |
610 | |
611 | void | |
3a978d72 | 612 | reset_specialization (void) |
386b8a85 JM |
613 | { |
614 | processing_specialization = 0; | |
615 | template_header_count = 0; | |
616 | } | |
617 | ||
838dfd8a | 618 | /* We've just seen a template header. If SPECIALIZATION is nonzero, |
386b8a85 JM |
619 | it was of the form template <>. */ |
620 | ||
c8094d83 | 621 | static void |
3a978d72 | 622 | note_template_header (int specialization) |
386b8a85 JM |
623 | { |
624 | processing_specialization = specialization; | |
625 | template_header_count++; | |
626 | } | |
627 | ||
75650646 | 628 | /* We're beginning an explicit instantiation. */ |
386b8a85 | 629 | |
75650646 | 630 | void |
3a978d72 | 631 | begin_explicit_instantiation (void) |
386b8a85 | 632 | { |
50bc768d | 633 | gcc_assert (!processing_explicit_instantiation); |
a723baf1 | 634 | processing_explicit_instantiation = true; |
75650646 | 635 | } |
386b8a85 | 636 | |
386b8a85 | 637 | |
75650646 | 638 | void |
3a978d72 | 639 | end_explicit_instantiation (void) |
75650646 | 640 | { |
50bc768d | 641 | gcc_assert (processing_explicit_instantiation); |
a723baf1 | 642 | processing_explicit_instantiation = false; |
75650646 | 643 | } |
386b8a85 | 644 | |
13a44ee0 | 645 | /* An explicit specialization or partial specialization TMPL is being |
b1cc95ce MM |
646 | declared. Check that the namespace in which the specialization is |
647 | occurring is permissible. Returns false iff it is invalid to | |
648 | specialize TMPL in the current namespace. */ | |
c8094d83 | 649 | |
b1cc95ce MM |
650 | static bool |
651 | check_specialization_namespace (tree tmpl) | |
652 | { | |
653 | tree tpl_ns = decl_namespace_context (tmpl); | |
654 | ||
655 | /* [tmpl.expl.spec] | |
c8094d83 | 656 | |
b1cc95ce MM |
657 | An explicit specialization shall be declared in the namespace of |
658 | which the template is a member, or, for member templates, in the | |
659 | namespace of which the enclosing class or enclosing class | |
660 | template is a member. An explicit specialization of a member | |
661 | function, member class or static data member of a class template | |
662 | shall be declared in the namespace of which the class template is | |
663 | a member. */ | |
664 | if (is_associated_namespace (current_namespace, tpl_ns)) | |
665 | /* Same or super-using namespace. */ | |
666 | return true; | |
667 | else | |
668 | { | |
0f51ccfc | 669 | pedwarn ("specialization of %qD in different namespace", tmpl); |
dee15844 | 670 | pedwarn (" from definition of %q+#D", tmpl); |
b1cc95ce MM |
671 | return false; |
672 | } | |
673 | } | |
674 | ||
9c12301f MM |
675 | /* SPEC is an explicit instantiation. Check that it is valid to |
676 | perform this explicit instantiation in the current namespace. */ | |
677 | ||
678 | static void | |
679 | check_explicit_instantiation_namespace (tree spec) | |
680 | { | |
681 | tree ns; | |
682 | ||
683 | /* DR 275: An explicit instantiation shall appear in an enclosing | |
3db45ab5 | 684 | namespace of its template. */ |
9c12301f MM |
685 | ns = decl_namespace_context (spec); |
686 | if (!is_ancestor (current_namespace, ns)) | |
687 | pedwarn ("explicit instantiation of %qD in namespace %qD " | |
312ea236 | 688 | "(which does not enclose namespace %qD)", |
9c12301f MM |
689 | spec, current_namespace, ns); |
690 | } | |
691 | ||
36a117a5 MM |
692 | /* The TYPE is being declared. If it is a template type, that means it |
693 | is a partial specialization. Do appropriate error-checking. */ | |
694 | ||
9ce387d9 | 695 | tree |
3a978d72 | 696 | maybe_process_partial_specialization (tree type) |
36a117a5 | 697 | { |
c9043d03 VR |
698 | tree context; |
699 | ||
700 | if (type == error_mark_node) | |
9ce387d9 VR |
701 | return error_mark_node; |
702 | ||
703 | if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM) | |
704 | { | |
705 | error ("name of class shadows template template parameter %qD", | |
706 | TYPE_NAME (type)); | |
707 | return error_mark_node; | |
708 | } | |
c9043d03 VR |
709 | |
710 | context = TYPE_CONTEXT (type); | |
6e049fcd | 711 | |
a723baf1 | 712 | if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type)) |
36a117a5 | 713 | { |
6e049fcd KL |
714 | /* This is for ordinary explicit specialization and partial |
715 | specialization of a template class such as: | |
716 | ||
717 | template <> class C<int>; | |
718 | ||
719 | or: | |
720 | ||
721 | template <class T> class C<T*>; | |
722 | ||
723 | Make sure that `C<int>' and `C<T*>' are implicit instantiations. */ | |
724 | ||
36a117a5 | 725 | if (CLASSTYPE_IMPLICIT_INSTANTIATION (type) |
d0f062fb | 726 | && !COMPLETE_TYPE_P (type)) |
36a117a5 | 727 | { |
b1cc95ce | 728 | check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (type)); |
370af2d5 | 729 | SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type); |
36a117a5 MM |
730 | if (processing_template_decl) |
731 | push_template_decl (TYPE_MAIN_DECL (type)); | |
732 | } | |
733 | else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type)) | |
0f51ccfc | 734 | error ("specialization of %qT after instantiation", type); |
36a117a5 | 735 | } |
6e049fcd KL |
736 | else if (CLASS_TYPE_P (type) |
737 | && !CLASSTYPE_USE_TEMPLATE (type) | |
738 | && CLASSTYPE_TEMPLATE_INFO (type) | |
739 | && context && CLASS_TYPE_P (context) | |
740 | && CLASSTYPE_TEMPLATE_INFO (context)) | |
741 | { | |
742 | /* This is for an explicit specialization of member class | |
743 | template according to [temp.expl.spec/18]: | |
744 | ||
745 | template <> template <class U> class C<int>::D; | |
746 | ||
747 | The context `C<int>' must be an implicit instantiation. | |
748 | Otherwise this is just a member class template declared | |
749 | earlier like: | |
750 | ||
751 | template <> class C<int> { template <class U> class D; }; | |
752 | template <> template <class U> class C<int>::D; | |
753 | ||
754 | In the first case, `C<int>::D' is a specialization of `C<T>::D' | |
755 | while in the second case, `C<int>::D' is a primary template | |
756 | and `C<T>::D' may not exist. */ | |
757 | ||
758 | if (CLASSTYPE_IMPLICIT_INSTANTIATION (context) | |
759 | && !COMPLETE_TYPE_P (type)) | |
760 | { | |
761 | tree t; | |
762 | ||
763 | if (current_namespace | |
764 | != decl_namespace_context (CLASSTYPE_TI_TEMPLATE (type))) | |
765 | { | |
0f51ccfc | 766 | pedwarn ("specializing %q#T in different namespace", type); |
dee15844 JM |
767 | pedwarn (" from definition of %q+#D", |
768 | CLASSTYPE_TI_TEMPLATE (type)); | |
6e049fcd KL |
769 | } |
770 | ||
771 | /* Check for invalid specialization after instantiation: | |
772 | ||
773 | template <> template <> class C<int>::D<int>; | |
774 | template <> template <class U> class C<int>::D; */ | |
775 | ||
776 | for (t = DECL_TEMPLATE_INSTANTIATIONS | |
777 | (most_general_template (CLASSTYPE_TI_TEMPLATE (type))); | |
778 | t; t = TREE_CHAIN (t)) | |
779 | if (TREE_VALUE (t) != type | |
780 | && TYPE_CONTEXT (TREE_VALUE (t)) == context) | |
0f51ccfc | 781 | error ("specialization %qT after instantiation %qT", |
6e049fcd KL |
782 | type, TREE_VALUE (t)); |
783 | ||
784 | /* Mark TYPE as a specialization. And as a result, we only | |
785 | have one level of template argument for the innermost | |
786 | class template. */ | |
787 | SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type); | |
788 | CLASSTYPE_TI_ARGS (type) | |
789 | = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)); | |
790 | } | |
791 | } | |
20496fa2 | 792 | else if (processing_specialization) |
9ce387d9 VR |
793 | { |
794 | error ("explicit specialization of non-template %qT", type); | |
795 | return error_mark_node; | |
796 | } | |
797 | ||
798 | return type; | |
36a117a5 MM |
799 | } |
800 | ||
ee81147e | 801 | /* Returns nonzero if we can optimize the retrieval of specializations |
c7222c02 MM |
802 | for TMPL, a TEMPLATE_DECL. In particular, for such a template, we |
803 | do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */ | |
804 | ||
c8094d83 | 805 | static inline bool |
c7222c02 MM |
806 | optimize_specialization_lookup_p (tree tmpl) |
807 | { | |
808 | return (DECL_FUNCTION_TEMPLATE_P (tmpl) | |
809 | && DECL_CLASS_SCOPE_P (tmpl) | |
810 | /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template | |
811 | parameter. */ | |
812 | && CLASS_TYPE_P (DECL_CONTEXT (tmpl)) | |
1ca939e5 MM |
813 | /* The optimized lookup depends on the fact that the |
814 | template arguments for the member function template apply | |
815 | purely to the containing class, which is not true if the | |
816 | containing class is an explicit or partial | |
817 | specialization. */ | |
818 | && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl)) | |
c7222c02 MM |
819 | && !DECL_MEMBER_TEMPLATE_P (tmpl) |
820 | && !DECL_CONV_FN_P (tmpl) | |
821 | /* It is possible to have a template that is not a member | |
822 | template and is not a member of a template class: | |
c8094d83 MS |
823 | |
824 | template <typename T> | |
c7222c02 | 825 | struct S { friend A::f(); }; |
c8094d83 | 826 | |
c7222c02 MM |
827 | Here, the friend function is a template, but the context does |
828 | not have template information. The optimized lookup relies | |
829 | on having ARGS be the template arguments for both the class | |
830 | and the function template. */ | |
831 | && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl))); | |
832 | } | |
833 | ||
75650646 MM |
834 | /* Retrieve the specialization (in the sense of [temp.spec] - a |
835 | specialization is either an instantiation or an explicit | |
836 | specialization) of TMPL for the given template ARGS. If there is | |
837 | no such specialization, return NULL_TREE. The ARGS are a vector of | |
838 | arguments, or a vector of vectors of arguments, in the case of | |
c8094d83 | 839 | templates with more than one level of parameters. |
c7222c02 MM |
840 | |
841 | If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true, | |
842 | then we search for a partial specialization matching ARGS. This | |
843 | parameter is ignored if TMPL is not a class template. */ | |
c8094d83 | 844 | |
75650646 | 845 | static tree |
c8094d83 | 846 | retrieve_specialization (tree tmpl, tree args, |
c7222c02 | 847 | bool class_specializations_p) |
75650646 | 848 | { |
2d8ba2c7 LM |
849 | if (args == error_mark_node) |
850 | return NULL_TREE; | |
851 | ||
50bc768d | 852 | gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL); |
75650646 | 853 | |
36a117a5 MM |
854 | /* There should be as many levels of arguments as there are |
855 | levels of parameters. */ | |
c8094d83 | 856 | gcc_assert (TMPL_ARGS_DEPTH (args) |
50bc768d | 857 | == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl))); |
c8094d83 | 858 | |
c7222c02 MM |
859 | if (optimize_specialization_lookup_p (tmpl)) |
860 | { | |
861 | tree class_template; | |
862 | tree class_specialization; | |
d4e6fecb | 863 | VEC(tree,gc) *methods; |
c7222c02 MM |
864 | tree fns; |
865 | int idx; | |
866 | ||
867 | /* The template arguments actually apply to the containing | |
868 | class. Find the class specialization with those | |
869 | arguments. */ | |
870 | class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl)); | |
c8094d83 | 871 | class_specialization |
c7222c02 MM |
872 | = retrieve_specialization (class_template, args, |
873 | /*class_specializations_p=*/false); | |
874 | if (!class_specialization) | |
875 | return NULL_TREE; | |
876 | /* Now, find the appropriate entry in the CLASSTYPE_METHOD_VEC | |
877 | for the specialization. */ | |
878 | idx = class_method_index_for_fn (class_specialization, tmpl); | |
879 | if (idx == -1) | |
880 | return NULL_TREE; | |
881 | /* Iterate through the methods with the indicated name, looking | |
882 | for the one that has an instance of TMPL. */ | |
883 | methods = CLASSTYPE_METHOD_VEC (class_specialization); | |
884 | for (fns = VEC_index (tree, methods, idx); fns; fns = OVL_NEXT (fns)) | |
885 | { | |
886 | tree fn = OVL_CURRENT (fns); | |
887 | if (DECL_TEMPLATE_INFO (fn) && DECL_TI_TEMPLATE (fn) == tmpl) | |
888 | return fn; | |
889 | } | |
890 | return NULL_TREE; | |
891 | } | |
892 | else | |
893 | { | |
894 | tree *sp; | |
895 | tree *head; | |
896 | ||
897 | /* Class templates store their instantiations on the | |
898 | DECL_TEMPLATE_INSTANTIATIONS list; other templates use the | |
899 | DECL_TEMPLATE_SPECIALIZATIONS list. */ | |
900 | if (!class_specializations_p | |
901 | && TREE_CODE (DECL_TEMPLATE_RESULT (tmpl)) == TYPE_DECL) | |
902 | sp = &DECL_TEMPLATE_INSTANTIATIONS (tmpl); | |
903 | else | |
904 | sp = &DECL_TEMPLATE_SPECIALIZATIONS (tmpl); | |
905 | head = sp; | |
906 | /* Iterate through the list until we find a matching template. */ | |
907 | while (*sp != NULL_TREE) | |
908 | { | |
909 | tree spec = *sp; | |
c8094d83 | 910 | |
c7222c02 MM |
911 | if (comp_template_args (TREE_PURPOSE (spec), args)) |
912 | { | |
913 | /* Use the move-to-front heuristic to speed up future | |
c8094d83 | 914 | searches. */ |
c7222c02 MM |
915 | if (spec != *head) |
916 | { | |
917 | *sp = TREE_CHAIN (*sp); | |
918 | TREE_CHAIN (spec) = *head; | |
919 | *head = spec; | |
920 | } | |
921 | return TREE_VALUE (spec); | |
922 | } | |
923 | sp = &TREE_CHAIN (spec); | |
924 | } | |
925 | } | |
75650646 MM |
926 | |
927 | return NULL_TREE; | |
386b8a85 JM |
928 | } |
929 | ||
dc957d14 | 930 | /* Like retrieve_specialization, but for local declarations. */ |
9188c363 MM |
931 | |
932 | static tree | |
3a978d72 | 933 | retrieve_local_specialization (tree tmpl) |
9188c363 | 934 | { |
67f5655f | 935 | tree spec = (tree) htab_find_with_hash (local_specializations, tmpl, |
3db45ab5 | 936 | htab_hash_pointer (tmpl)); |
a723baf1 | 937 | return spec ? TREE_PURPOSE (spec) : NULL_TREE; |
9188c363 MM |
938 | } |
939 | ||
838dfd8a | 940 | /* Returns nonzero iff DECL is a specialization of TMPL. */ |
6757edfe MM |
941 | |
942 | int | |
3a978d72 | 943 | is_specialization_of (tree decl, tree tmpl) |
6757edfe MM |
944 | { |
945 | tree t; | |
946 | ||
947 | if (TREE_CODE (decl) == FUNCTION_DECL) | |
948 | { | |
c8094d83 | 949 | for (t = decl; |
6757edfe MM |
950 | t != NULL_TREE; |
951 | t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE) | |
952 | if (t == tmpl) | |
953 | return 1; | |
954 | } | |
c8094d83 | 955 | else |
6757edfe | 956 | { |
50bc768d | 957 | gcc_assert (TREE_CODE (decl) == TYPE_DECL); |
6757edfe MM |
958 | |
959 | for (t = TREE_TYPE (decl); | |
960 | t != NULL_TREE; | |
961 | t = CLASSTYPE_USE_TEMPLATE (t) | |
962 | ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE) | |
9edc3913 | 963 | if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl))) |
6757edfe | 964 | return 1; |
c8094d83 | 965 | } |
386b8a85 | 966 | |
6757edfe MM |
967 | return 0; |
968 | } | |
75650646 | 969 | |
d43f603d KL |
970 | /* Returns nonzero iff DECL is a specialization of friend declaration |
971 | FRIEND according to [temp.friend]. */ | |
972 | ||
973 | bool | |
974 | is_specialization_of_friend (tree decl, tree friend) | |
975 | { | |
976 | bool need_template = true; | |
977 | int template_depth; | |
978 | ||
b939a023 KL |
979 | gcc_assert (TREE_CODE (decl) == FUNCTION_DECL |
980 | || TREE_CODE (decl) == TYPE_DECL); | |
d43f603d KL |
981 | |
982 | /* For [temp.friend/6] when FRIEND is an ordinary member function | |
983 | of a template class, we want to check if DECL is a specialization | |
984 | if this. */ | |
985 | if (TREE_CODE (friend) == FUNCTION_DECL | |
986 | && DECL_TEMPLATE_INFO (friend) | |
987 | && !DECL_USE_TEMPLATE (friend)) | |
988 | { | |
b939a023 | 989 | /* We want a TEMPLATE_DECL for `is_specialization_of'. */ |
d43f603d KL |
990 | friend = DECL_TI_TEMPLATE (friend); |
991 | need_template = false; | |
992 | } | |
b939a023 KL |
993 | else if (TREE_CODE (friend) == TEMPLATE_DECL |
994 | && !PRIMARY_TEMPLATE_P (friend)) | |
995 | need_template = false; | |
d43f603d KL |
996 | |
997 | /* There is nothing to do if this is not a template friend. */ | |
998 | if (TREE_CODE (friend) != TEMPLATE_DECL) | |
b939a023 | 999 | return false; |
d43f603d KL |
1000 | |
1001 | if (is_specialization_of (decl, friend)) | |
b939a023 | 1002 | return true; |
d43f603d KL |
1003 | |
1004 | /* [temp.friend/6] | |
1005 | A member of a class template may be declared to be a friend of a | |
1006 | non-template class. In this case, the corresponding member of | |
1007 | every specialization of the class template is a friend of the | |
1008 | class granting friendship. | |
c8094d83 | 1009 | |
d43f603d KL |
1010 | For example, given a template friend declaration |
1011 | ||
1012 | template <class T> friend void A<T>::f(); | |
1013 | ||
1014 | the member function below is considered a friend | |
1015 | ||
1016 | template <> struct A<int> { | |
1017 | void f(); | |
1018 | }; | |
1019 | ||
1020 | For this type of template friend, TEMPLATE_DEPTH below will be | |
5c425df5 | 1021 | nonzero. To determine if DECL is a friend of FRIEND, we first |
d43f603d KL |
1022 | check if the enclosing class is a specialization of another. */ |
1023 | ||
1024 | template_depth = template_class_depth (DECL_CONTEXT (friend)); | |
1025 | if (template_depth | |
1026 | && DECL_CLASS_SCOPE_P (decl) | |
c8094d83 | 1027 | && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)), |
d43f603d KL |
1028 | CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend)))) |
1029 | { | |
1030 | /* Next, we check the members themselves. In order to handle | |
b939a023 | 1031 | a few tricky cases, such as when FRIEND's are |
d43f603d KL |
1032 | |
1033 | template <class T> friend void A<T>::g(T t); | |
1034 | template <class T> template <T t> friend void A<T>::h(); | |
1035 | ||
b939a023 KL |
1036 | and DECL's are |
1037 | ||
1038 | void A<int>::g(int); | |
1039 | template <int> void A<int>::h(); | |
1040 | ||
1041 | we need to figure out ARGS, the template arguments from | |
1042 | the context of DECL. This is required for template substitution | |
1043 | of `T' in the function parameter of `g' and template parameter | |
1044 | of `h' in the above examples. Here ARGS corresponds to `int'. */ | |
d43f603d KL |
1045 | |
1046 | tree context = DECL_CONTEXT (decl); | |
1047 | tree args = NULL_TREE; | |
1048 | int current_depth = 0; | |
b939a023 | 1049 | |
d43f603d KL |
1050 | while (current_depth < template_depth) |
1051 | { | |
1052 | if (CLASSTYPE_TEMPLATE_INFO (context)) | |
1053 | { | |
1054 | if (current_depth == 0) | |
1055 | args = TYPE_TI_ARGS (context); | |
1056 | else | |
1057 | args = add_to_template_args (TYPE_TI_ARGS (context), args); | |
1058 | current_depth++; | |
1059 | } | |
1060 | context = TYPE_CONTEXT (context); | |
1061 | } | |
1062 | ||
1063 | if (TREE_CODE (decl) == FUNCTION_DECL) | |
1064 | { | |
1065 | bool is_template; | |
1066 | tree friend_type; | |
1067 | tree decl_type; | |
1068 | tree friend_args_type; | |
1069 | tree decl_args_type; | |
1070 | ||
1071 | /* Make sure that both DECL and FRIEND are templates or | |
1072 | non-templates. */ | |
1073 | is_template = DECL_TEMPLATE_INFO (decl) | |
1074 | && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)); | |
1075 | if (need_template ^ is_template) | |
b939a023 | 1076 | return false; |
d43f603d KL |
1077 | else if (is_template) |
1078 | { | |
da1d7781 | 1079 | /* If both are templates, check template parameter list. */ |
d43f603d KL |
1080 | tree friend_parms |
1081 | = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend), | |
1082 | args, tf_none); | |
1083 | if (!comp_template_parms | |
1084 | (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)), | |
1085 | friend_parms)) | |
b939a023 | 1086 | return false; |
d43f603d KL |
1087 | |
1088 | decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl)); | |
1089 | } | |
1090 | else | |
1091 | decl_type = TREE_TYPE (decl); | |
1092 | ||
1093 | friend_type = tsubst_function_type (TREE_TYPE (friend), args, | |
1094 | tf_none, NULL_TREE); | |
1095 | if (friend_type == error_mark_node) | |
b939a023 | 1096 | return false; |
d43f603d KL |
1097 | |
1098 | /* Check if return types match. */ | |
1099 | if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type))) | |
b939a023 | 1100 | return false; |
d43f603d KL |
1101 | |
1102 | /* Check if function parameter types match, ignoring the | |
1103 | `this' parameter. */ | |
1104 | friend_args_type = TYPE_ARG_TYPES (friend_type); | |
1105 | decl_args_type = TYPE_ARG_TYPES (decl_type); | |
1106 | if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend)) | |
1107 | friend_args_type = TREE_CHAIN (friend_args_type); | |
1108 | if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)) | |
1109 | decl_args_type = TREE_CHAIN (decl_args_type); | |
b939a023 KL |
1110 | |
1111 | return compparms (decl_args_type, friend_args_type); | |
1112 | } | |
1113 | else | |
1114 | { | |
1115 | /* DECL is a TYPE_DECL */ | |
1116 | bool is_template; | |
1117 | tree decl_type = TREE_TYPE (decl); | |
1118 | ||
1119 | /* Make sure that both DECL and FRIEND are templates or | |
1120 | non-templates. */ | |
1121 | is_template | |
1122 | = CLASSTYPE_TEMPLATE_INFO (decl_type) | |
1123 | && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type)); | |
1124 | ||
1125 | if (need_template ^ is_template) | |
1126 | return false; | |
1127 | else if (is_template) | |
1128 | { | |
1129 | tree friend_parms; | |
1130 | /* If both are templates, check the name of the two | |
1131 | TEMPLATE_DECL's first because is_friend didn't. */ | |
1132 | if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type)) | |
1133 | != DECL_NAME (friend)) | |
1134 | return false; | |
1135 | ||
1136 | /* Now check template parameter list. */ | |
1137 | friend_parms | |
1138 | = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend), | |
1139 | args, tf_none); | |
1140 | return comp_template_parms | |
1141 | (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)), | |
1142 | friend_parms); | |
1143 | } | |
1144 | else | |
1145 | return (DECL_NAME (decl) | |
1146 | == DECL_NAME (friend)); | |
d43f603d KL |
1147 | } |
1148 | } | |
b939a023 | 1149 | return false; |
d43f603d KL |
1150 | } |
1151 | ||
75650646 | 1152 | /* Register the specialization SPEC as a specialization of TMPL with |
d63d5d0c ILT |
1153 | the indicated ARGS. IS_FRIEND indicates whether the specialization |
1154 | is actually just a friend declaration. Returns SPEC, or an | |
1155 | equivalent prior declaration, if available. */ | |
75650646 | 1156 | |
36a117a5 | 1157 | static tree |
d63d5d0c | 1158 | register_specialization (tree spec, tree tmpl, tree args, bool is_friend) |
75650646 | 1159 | { |
c7222c02 | 1160 | tree fn; |
75650646 | 1161 | |
50bc768d | 1162 | gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL); |
75650646 | 1163 | |
c8094d83 | 1164 | if (TREE_CODE (spec) == FUNCTION_DECL |
36a117a5 MM |
1165 | && uses_template_parms (DECL_TI_ARGS (spec))) |
1166 | /* This is the FUNCTION_DECL for a partial instantiation. Don't | |
1167 | register it; we want the corresponding TEMPLATE_DECL instead. | |
1168 | We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than | |
1169 | the more obvious `uses_template_parms (spec)' to avoid problems | |
1170 | with default function arguments. In particular, given | |
1171 | something like this: | |
1172 | ||
0cbd7506 | 1173 | template <class T> void f(T t1, T t = T()) |
36a117a5 MM |
1174 | |
1175 | the default argument expression is not substituted for in an | |
1176 | instantiation unless and until it is actually needed. */ | |
1177 | return spec; | |
28ba38e3 | 1178 | |
c8094d83 | 1179 | fn = retrieve_specialization (tmpl, args, |
c7222c02 MM |
1180 | /*class_specializations_p=*/false); |
1181 | /* We can sometimes try to re-register a specialization that we've | |
1182 | already got. In particular, regenerate_decl_from_template calls | |
1183 | duplicate_decls which will update the specialization list. But, | |
1184 | we'll still get called again here anyhow. It's more convenient | |
1185 | to simply allow this than to try to prevent it. */ | |
1186 | if (fn == spec) | |
1187 | return spec; | |
1188 | else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec)) | |
22e9174f | 1189 | { |
c7222c02 | 1190 | if (DECL_TEMPLATE_INSTANTIATION (fn)) |
22e9174f | 1191 | { |
c8094d83 | 1192 | if (TREE_USED (fn) |
c7222c02 | 1193 | || DECL_EXPLICIT_INSTANTIATION (fn)) |
22e9174f | 1194 | { |
c7222c02 MM |
1195 | error ("specialization of %qD after instantiation", |
1196 | fn); | |
99f4234a | 1197 | return error_mark_node; |
22e9174f | 1198 | } |
c7222c02 | 1199 | else |
b1cc95ce | 1200 | { |
a39041fa | 1201 | tree clone; |
c7222c02 MM |
1202 | /* This situation should occur only if the first |
1203 | specialization is an implicit instantiation, the | |
1204 | second is an explicit specialization, and the | |
1205 | implicit instantiation has not yet been used. That | |
1206 | situation can occur if we have implicitly | |
1207 | instantiated a member function and then specialized | |
1208 | it later. | |
c8094d83 | 1209 | |
c7222c02 MM |
1210 | We can also wind up here if a friend declaration that |
1211 | looked like an instantiation turns out to be a | |
1212 | specialization: | |
c8094d83 | 1213 | |
c7222c02 MM |
1214 | template <class T> void foo(T); |
1215 | class S { friend void foo<>(int) }; | |
c8094d83 MS |
1216 | template <> void foo(int); |
1217 | ||
c7222c02 MM |
1218 | We transform the existing DECL in place so that any |
1219 | pointers to it become pointers to the updated | |
1220 | declaration. | |
1221 | ||
1222 | If there was a definition for the template, but not | |
1223 | for the specialization, we want this to look as if | |
1224 | there were no definition, and vice versa. */ | |
1225 | DECL_INITIAL (fn) = NULL_TREE; | |
d63d5d0c | 1226 | duplicate_decls (spec, fn, is_friend); |
a39041fa | 1227 | /* The call to duplicate_decls will have applied |
3db45ab5 | 1228 | [temp.expl.spec]: |
a39041fa | 1229 | |
3db45ab5 | 1230 | An explicit specialization of a function template |
a39041fa | 1231 | is inline only if it is explicitly declared to be, |
bcf51da2 | 1232 | and independently of whether its function template |
a39041fa MM |
1233 | is. |
1234 | ||
1235 | to the primary function; now copy the inline bits to | |
3db45ab5 | 1236 | the various clones. */ |
a39041fa MM |
1237 | FOR_EACH_CLONE (clone, fn) |
1238 | { | |
1239 | DECL_DECLARED_INLINE_P (clone) | |
1240 | = DECL_DECLARED_INLINE_P (fn); | |
1241 | DECL_INLINE (clone) | |
1242 | = DECL_INLINE (fn); | |
1243 | } | |
9c12301f | 1244 | check_specialization_namespace (fn); |
c8094d83 | 1245 | |
b1cc95ce MM |
1246 | return fn; |
1247 | } | |
22e9174f | 1248 | } |
c7222c02 MM |
1249 | else if (DECL_TEMPLATE_SPECIALIZATION (fn)) |
1250 | { | |
d63d5d0c | 1251 | if (!duplicate_decls (spec, fn, is_friend) && DECL_INITIAL (spec)) |
c7222c02 MM |
1252 | /* Dup decl failed, but this is a new definition. Set the |
1253 | line number so any errors match this new | |
1254 | definition. */ | |
1255 | DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec); | |
c8094d83 | 1256 | |
c7222c02 MM |
1257 | return fn; |
1258 | } | |
b1cc95ce MM |
1259 | } |
1260 | ||
1261 | /* A specialization must be declared in the same namespace as the | |
1262 | template it is specializing. */ | |
1263 | if (DECL_TEMPLATE_SPECIALIZATION (spec) | |
1264 | && !check_specialization_namespace (tmpl)) | |
74b0d490 | 1265 | DECL_CONTEXT (spec) = FROB_CONTEXT (decl_namespace_context (tmpl)); |
75650646 | 1266 | |
c7222c02 MM |
1267 | if (!optimize_specialization_lookup_p (tmpl)) |
1268 | DECL_TEMPLATE_SPECIALIZATIONS (tmpl) | |
1269 | = tree_cons (args, spec, DECL_TEMPLATE_SPECIALIZATIONS (tmpl)); | |
36a117a5 MM |
1270 | |
1271 | return spec; | |
1272 | } | |
1273 | ||
1274 | /* Unregister the specialization SPEC as a specialization of TMPL. | |
b3445994 MM |
1275 | Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true |
1276 | if the SPEC was listed as a specialization of TMPL. */ | |
36a117a5 | 1277 | |
b3445994 MM |
1278 | bool |
1279 | reregister_specialization (tree spec, tree tmpl, tree new_spec) | |
36a117a5 MM |
1280 | { |
1281 | tree* s; | |
1282 | ||
1283 | for (s = &DECL_TEMPLATE_SPECIALIZATIONS (tmpl); | |
1284 | *s != NULL_TREE; | |
1285 | s = &TREE_CHAIN (*s)) | |
1286 | if (TREE_VALUE (*s) == spec) | |
1287 | { | |
b3445994 MM |
1288 | if (!new_spec) |
1289 | *s = TREE_CHAIN (*s); | |
1290 | else | |
a3d87771 | 1291 | TREE_VALUE (*s) = new_spec; |
36a117a5 MM |
1292 | return 1; |
1293 | } | |
1294 | ||
1295 | return 0; | |
75650646 MM |
1296 | } |
1297 | ||
a723baf1 MM |
1298 | /* Compare an entry in the local specializations hash table P1 (which |
1299 | is really a pointer to a TREE_LIST) with P2 (which is really a | |
1300 | DECL). */ | |
1301 | ||
1302 | static int | |
1303 | eq_local_specializations (const void *p1, const void *p2) | |
1304 | { | |
1305 | return TREE_VALUE ((tree) p1) == (tree) p2; | |
1306 | } | |
1307 | ||
69f794a7 MM |
1308 | /* Hash P1, an entry in the local specializations table. */ |
1309 | ||
1310 | static hashval_t | |
1311 | hash_local_specialization (const void* p1) | |
1312 | { | |
1313 | return htab_hash_pointer (TREE_VALUE ((tree) p1)); | |
1314 | } | |
1315 | ||
6dfbb909 MM |
1316 | /* Like register_specialization, but for local declarations. We are |
1317 | registering SPEC, an instantiation of TMPL. */ | |
9188c363 | 1318 | |
414ea4aa | 1319 | static void |
3a978d72 | 1320 | register_local_specialization (tree spec, tree tmpl) |
9188c363 | 1321 | { |
6dfbb909 MM |
1322 | void **slot; |
1323 | ||
c8094d83 | 1324 | slot = htab_find_slot_with_hash (local_specializations, tmpl, |
69f794a7 | 1325 | htab_hash_pointer (tmpl), INSERT); |
a723baf1 | 1326 | *slot = build_tree_list (spec, tmpl); |
9188c363 MM |
1327 | } |
1328 | ||
9ba7a2f2 MM |
1329 | /* TYPE is a class type. Returns true if TYPE is an explicitly |
1330 | specialized class. */ | |
1331 | ||
1332 | bool | |
1333 | explicit_class_specialization_p (tree type) | |
1334 | { | |
1335 | if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type)) | |
1336 | return false; | |
1337 | return !uses_template_parms (CLASSTYPE_TI_ARGS (type)); | |
1338 | } | |
1339 | ||
e1467ff2 MM |
1340 | /* Print the list of candidate FNS in an error message. */ |
1341 | ||
104bf76a | 1342 | void |
3a978d72 | 1343 | print_candidates (tree fns) |
e1467ff2 MM |
1344 | { |
1345 | tree fn; | |
1346 | ||
d8e178a0 | 1347 | const char *str = "candidates are:"; |
e1467ff2 MM |
1348 | |
1349 | for (fn = fns; fn != NULL_TREE; fn = TREE_CHAIN (fn)) | |
1350 | { | |
d6479fe7 MM |
1351 | tree f; |
1352 | ||
1353 | for (f = TREE_VALUE (fn); f; f = OVL_NEXT (f)) | |
dee15844 | 1354 | error ("%s %+#D", str, OVL_CURRENT (f)); |
e1467ff2 MM |
1355 | str = " "; |
1356 | } | |
1357 | } | |
1358 | ||
75650646 | 1359 | /* Returns the template (one of the functions given by TEMPLATE_ID) |
e1467ff2 | 1360 | which can be specialized to match the indicated DECL with the |
03017874 MM |
1361 | explicit template args given in TEMPLATE_ID. The DECL may be |
1362 | NULL_TREE if none is available. In that case, the functions in | |
1363 | TEMPLATE_ID are non-members. | |
1364 | ||
838dfd8a | 1365 | If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a |
03017874 MM |
1366 | specialization of a member template. |
1367 | ||
5fe7b654 GB |
1368 | The TEMPLATE_COUNT is the number of references to qualifying |
1369 | template classes that appeared in the name of the function. See | |
1370 | check_explicit_specialization for a more accurate description. | |
1371 | ||
29a1da1c MM |
1372 | TSK indicates what kind of template declaration (if any) is being |
1373 | declared. TSK_TEMPLATE indicates that the declaration given by | |
1374 | DECL, though a FUNCTION_DECL, has template parameters, and is | |
1375 | therefore a template function. | |
1376 | ||
03017874 MM |
1377 | The template args (those explicitly specified and those deduced) |
1378 | are output in a newly created vector *TARGS_OUT. | |
1379 | ||
1380 | If it is impossible to determine the result, an error message is | |
bf8f3f93 | 1381 | issued. The error_mark_node is returned to indicate failure. */ |
75650646 | 1382 | |
e9659ab0 | 1383 | static tree |
c8094d83 | 1384 | determine_specialization (tree template_id, |
0cbd7506 MS |
1385 | tree decl, |
1386 | tree* targs_out, | |
5fe7b654 | 1387 | int need_member_template, |
29a1da1c MM |
1388 | int template_count, |
1389 | tmpl_spec_kind tsk) | |
386b8a85 | 1390 | { |
03017874 MM |
1391 | tree fns; |
1392 | tree targs; | |
1393 | tree explicit_targs; | |
1394 | tree candidates = NULL_TREE; | |
7ca383e6 MM |
1395 | /* A TREE_LIST of templates of which DECL may be a specialization. |
1396 | The TREE_VALUE of each node is a TEMPLATE_DECL. The | |
1397 | corresponding TREE_PURPOSE is the set of template arguments that, | |
1398 | when used to instantiate the template, would produce a function | |
1399 | with the signature of DECL. */ | |
03017874 | 1400 | tree templates = NULL_TREE; |
5fe7b654 GB |
1401 | int header_count; |
1402 | struct cp_binding_level *b; | |
386b8a85 | 1403 | |
e1467ff2 MM |
1404 | *targs_out = NULL_TREE; |
1405 | ||
728da672 | 1406 | if (template_id == error_mark_node || decl == error_mark_node) |
f2e48b67 BK |
1407 | return error_mark_node; |
1408 | ||
1409 | fns = TREE_OPERAND (template_id, 0); | |
03017874 | 1410 | explicit_targs = TREE_OPERAND (template_id, 1); |
f2e48b67 | 1411 | |
27fafc8d JM |
1412 | if (fns == error_mark_node) |
1413 | return error_mark_node; | |
1414 | ||
c6002625 | 1415 | /* Check for baselinks. */ |
91e490ab | 1416 | if (BASELINK_P (fns)) |
da15dae6 | 1417 | fns = BASELINK_FUNCTIONS (fns); |
386b8a85 | 1418 | |
91e490ab MM |
1419 | if (!is_overloaded_fn (fns)) |
1420 | { | |
0f51ccfc | 1421 | error ("%qD is not a function template", fns); |
91e490ab MM |
1422 | return error_mark_node; |
1423 | } | |
1424 | ||
5fe7b654 GB |
1425 | /* Count the number of template headers specified for this |
1426 | specialization. */ | |
1427 | header_count = 0; | |
c8094d83 | 1428 | for (b = current_binding_level; |
98f99d7f | 1429 | b->kind == sk_template_parms; |
5fe7b654 GB |
1430 | b = b->level_chain) |
1431 | ++header_count; | |
1432 | ||
2c73f9f5 | 1433 | for (; fns; fns = OVL_NEXT (fns)) |
386b8a85 | 1434 | { |
9dfce8fd | 1435 | tree fn = OVL_CURRENT (fns); |
03017874 MM |
1436 | |
1437 | if (TREE_CODE (fn) == TEMPLATE_DECL) | |
d955f6ea KL |
1438 | { |
1439 | tree decl_arg_types; | |
0da99d4e | 1440 | tree fn_arg_types; |
d955f6ea | 1441 | |
5fe7b654 GB |
1442 | /* In case of explicit specialization, we need to check if |
1443 | the number of template headers appearing in the specialization | |
1444 | is correct. This is usually done in check_explicit_specialization, | |
1445 | but the check done there cannot be exhaustive when specializing | |
1446 | member functions. Consider the following code: | |
1447 | ||
1448 | template <> void A<int>::f(int); | |
1449 | template <> template <> void A<int>::f(int); | |
1450 | ||
1451 | Assuming that A<int> is not itself an explicit specialization | |
1452 | already, the first line specializes "f" which is a non-template | |
1453 | member function, whilst the second line specializes "f" which | |
1454 | is a template member function. So both lines are syntactically | |
1455 | correct, and check_explicit_specialization does not reject | |
1456 | them. | |
c8094d83 | 1457 | |
5fe7b654 GB |
1458 | Here, we can do better, as we are matching the specialization |
1459 | against the declarations. We count the number of template | |
1460 | headers, and we check if they match TEMPLATE_COUNT + 1 | |
1461 | (TEMPLATE_COUNT is the number of qualifying template classes, | |
1462 | plus there must be another header for the member template | |
1463 | itself). | |
c8094d83 | 1464 | |
5fe7b654 GB |
1465 | Notice that if header_count is zero, this is not a |
1466 | specialization but rather a template instantiation, so there | |
1467 | is no check we can perform here. */ | |
1468 | if (header_count && header_count != template_count + 1) | |
1469 | continue; | |
1470 | ||
98f99d7f MM |
1471 | /* Check that the number of template arguments at the |
1472 | innermost level for DECL is the same as for FN. */ | |
1473 | if (current_binding_level->kind == sk_template_parms | |
1474 | && !current_binding_level->explicit_spec_p | |
1475 | && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn)) | |
3db45ab5 | 1476 | != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS |
916b63c3 | 1477 | (current_template_parms)))) |
98f99d7f | 1478 | continue; |
c8094d83 | 1479 | |
ba139ba8 MM |
1480 | /* DECL might be a specialization of FN. */ |
1481 | decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl)); | |
1482 | fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn)); | |
1483 | ||
1484 | /* For a non-static member function, we need to make sure | |
1485 | that the const qualification is the same. Since | |
1486 | get_bindings does not try to merge the "this" parameter, | |
1487 | we must do the comparison explicitly. */ | |
1488 | if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn) | |
1489 | && !same_type_p (TREE_VALUE (fn_arg_types), | |
1490 | TREE_VALUE (decl_arg_types))) | |
1491 | continue; | |
1492 | ||
1493 | /* Skip the "this" parameter and, for constructors of | |
1494 | classes with virtual bases, the VTT parameter. A | |
1495 | full specialization of a constructor will have a VTT | |
1496 | parameter, but a template never will. */ | |
1497 | decl_arg_types | |
1498 | = skip_artificial_parms_for (decl, decl_arg_types); | |
1499 | fn_arg_types | |
1500 | = skip_artificial_parms_for (fn, fn_arg_types); | |
1501 | ||
1502 | /* Check that the number of function parameters matches. | |
1503 | For example, | |
1504 | template <class T> void f(int i = 0); | |
1505 | template <> void f<int>(); | |
1506 | The specialization f<int> is invalid but is not caught | |
1507 | by get_bindings below. */ | |
1508 | if (list_length (fn_arg_types) != list_length (decl_arg_types)) | |
1509 | continue; | |
1510 | ||
29a1da1c MM |
1511 | /* Function templates cannot be specializations; there are |
1512 | no partial specializations of functions. Therefore, if | |
1513 | the type of DECL does not match FN, there is no | |
1514 | match. */ | |
1515 | if (tsk == tsk_template) | |
1516 | { | |
ba139ba8 | 1517 | if (compparms (fn_arg_types, decl_arg_types)) |
29a1da1c MM |
1518 | candidates = tree_cons (NULL_TREE, fn, candidates); |
1519 | continue; | |
1520 | } | |
1521 | ||
d955f6ea KL |
1522 | /* See whether this function might be a specialization of this |
1523 | template. */ | |
a34d3336 | 1524 | targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true); |
d955f6ea KL |
1525 | |
1526 | if (!targs) | |
1527 | /* We cannot deduce template arguments that when used to | |
1528 | specialize TMPL will produce DECL. */ | |
1529 | continue; | |
1530 | ||
1531 | /* Save this template, and the arguments deduced. */ | |
1532 | templates = tree_cons (targs, fn, templates); | |
1533 | } | |
03017874 MM |
1534 | else if (need_member_template) |
1535 | /* FN is an ordinary member function, and we need a | |
1536 | specialization of a member template. */ | |
d955f6ea | 1537 | ; |
03017874 MM |
1538 | else if (TREE_CODE (fn) != FUNCTION_DECL) |
1539 | /* We can get IDENTIFIER_NODEs here in certain erroneous | |
1540 | cases. */ | |
d955f6ea | 1541 | ; |
03017874 MM |
1542 | else if (!DECL_FUNCTION_MEMBER_P (fn)) |
1543 | /* This is just an ordinary non-member function. Nothing can | |
1544 | be a specialization of that. */ | |
d955f6ea | 1545 | ; |
3b82c249 KL |
1546 | else if (DECL_ARTIFICIAL (fn)) |
1547 | /* Cannot specialize functions that are created implicitly. */ | |
d955f6ea | 1548 | ; |
75650646 | 1549 | else |
03017874 MM |
1550 | { |
1551 | tree decl_arg_types; | |
386b8a85 | 1552 | |
03017874 MM |
1553 | /* This is an ordinary member function. However, since |
1554 | we're here, we can assume it's enclosing class is a | |
1555 | template class. For example, | |
c8094d83 | 1556 | |
03017874 MM |
1557 | template <typename T> struct S { void f(); }; |
1558 | template <> void S<int>::f() {} | |
1559 | ||
1560 | Here, S<int>::f is a non-template, but S<int> is a | |
1561 | template class. If FN has the same type as DECL, we | |
1562 | might be in business. */ | |
f5d47abd KL |
1563 | |
1564 | if (!DECL_TEMPLATE_INFO (fn)) | |
1565 | /* Its enclosing class is an explicit specialization | |
1566 | of a template class. This is not a candidate. */ | |
1567 | continue; | |
1568 | ||
03017874 MM |
1569 | if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)), |
1570 | TREE_TYPE (TREE_TYPE (fn)))) | |
1571 | /* The return types differ. */ | |
1572 | continue; | |
1573 | ||
1574 | /* Adjust the type of DECL in case FN is a static member. */ | |
1575 | decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl)); | |
c8094d83 | 1576 | if (DECL_STATIC_FUNCTION_P (fn) |
03017874 MM |
1577 | && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)) |
1578 | decl_arg_types = TREE_CHAIN (decl_arg_types); | |
1579 | ||
c8094d83 | 1580 | if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)), |
03017874 MM |
1581 | decl_arg_types)) |
1582 | /* They match! */ | |
1583 | candidates = tree_cons (NULL_TREE, fn, candidates); | |
03017874 | 1584 | } |
386b8a85 | 1585 | } |
03017874 | 1586 | |
bf8f3f93 | 1587 | if (templates && TREE_CHAIN (templates)) |
386b8a85 | 1588 | { |
03017874 | 1589 | /* We have: |
c8094d83 | 1590 | |
03017874 MM |
1591 | [temp.expl.spec] |
1592 | ||
1593 | It is possible for a specialization with a given function | |
1594 | signature to be instantiated from more than one function | |
1595 | template. In such cases, explicit specification of the | |
1596 | template arguments must be used to uniquely identify the | |
1597 | function template specialization being specialized. | |
1598 | ||
1599 | Note that here, there's no suggestion that we're supposed to | |
1600 | determine which of the candidate templates is most | |
1601 | specialized. However, we, also have: | |
1602 | ||
1603 | [temp.func.order] | |
1604 | ||
1605 | Partial ordering of overloaded function template | |
1606 | declarations is used in the following contexts to select | |
1607 | the function template to which a function template | |
c8094d83 | 1608 | specialization refers: |
03017874 | 1609 | |
0cbd7506 | 1610 | -- when an explicit specialization refers to a function |
c8094d83 | 1611 | template. |
03017874 MM |
1612 | |
1613 | So, we do use the partial ordering rules, at least for now. | |
0e339752 | 1614 | This extension can only serve to make invalid programs valid, |
03017874 MM |
1615 | so it's safe. And, there is strong anecdotal evidence that |
1616 | the committee intended the partial ordering rules to apply; | |
3b426391 | 1617 | the EDG front end has that behavior, and John Spicer claims |
03017874 MM |
1618 | that the committee simply forgot to delete the wording in |
1619 | [temp.expl.spec]. */ | |
7ca383e6 MM |
1620 | tree tmpl = most_specialized_instantiation (templates); |
1621 | if (tmpl != error_mark_node) | |
1622 | { | |
1623 | templates = tmpl; | |
1624 | TREE_CHAIN (templates) = NULL_TREE; | |
1625 | } | |
e1467ff2 MM |
1626 | } |
1627 | ||
03017874 | 1628 | if (templates == NULL_TREE && candidates == NULL_TREE) |
e1467ff2 | 1629 | { |
dee15844 JM |
1630 | error ("template-id %qD for %q+D does not match any template " |
1631 | "declaration", template_id, decl); | |
03017874 | 1632 | return error_mark_node; |
386b8a85 | 1633 | } |
03017874 | 1634 | else if ((templates && TREE_CHAIN (templates)) |
bf8f3f93 MM |
1635 | || (candidates && TREE_CHAIN (candidates)) |
1636 | || (templates && candidates)) | |
386b8a85 | 1637 | { |
dee15844 JM |
1638 | error ("ambiguous template specialization %qD for %q+D", |
1639 | template_id, decl); | |
bf8f3f93 MM |
1640 | chainon (candidates, templates); |
1641 | print_candidates (candidates); | |
03017874 | 1642 | return error_mark_node; |
386b8a85 JM |
1643 | } |
1644 | ||
c6002625 | 1645 | /* We have one, and exactly one, match. */ |
03017874 MM |
1646 | if (candidates) |
1647 | { | |
29a1da1c MM |
1648 | tree fn = TREE_VALUE (candidates); |
1649 | /* DECL is a re-declaration of a template function. */ | |
1650 | if (TREE_CODE (fn) == TEMPLATE_DECL) | |
1651 | return fn; | |
03017874 MM |
1652 | /* It was a specialization of an ordinary member function in a |
1653 | template class. */ | |
29a1da1c MM |
1654 | *targs_out = copy_node (DECL_TI_ARGS (fn)); |
1655 | return DECL_TI_TEMPLATE (fn); | |
03017874 MM |
1656 | } |
1657 | ||
1658 | /* It was a specialization of a template. */ | |
17aec3eb | 1659 | targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates))); |
03017874 MM |
1660 | if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs)) |
1661 | { | |
1662 | *targs_out = copy_node (targs); | |
c8094d83 | 1663 | SET_TMPL_ARGS_LEVEL (*targs_out, |
03017874 MM |
1664 | TMPL_ARGS_DEPTH (*targs_out), |
1665 | TREE_PURPOSE (templates)); | |
1666 | } | |
1667 | else | |
1668 | *targs_out = TREE_PURPOSE (templates); | |
e1467ff2 | 1669 | return TREE_VALUE (templates); |
8d08fdba | 1670 | } |
8afa707f MM |
1671 | |
1672 | /* Returns a chain of parameter types, exactly like the SPEC_TYPES, | |
1673 | but with the default argument values filled in from those in the | |
1674 | TMPL_TYPES. */ | |
c8094d83 | 1675 | |
8afa707f | 1676 | static tree |
3a978d72 NN |
1677 | copy_default_args_to_explicit_spec_1 (tree spec_types, |
1678 | tree tmpl_types) | |
8afa707f MM |
1679 | { |
1680 | tree new_spec_types; | |
1681 | ||
1682 | if (!spec_types) | |
1683 | return NULL_TREE; | |
1684 | ||
1685 | if (spec_types == void_list_node) | |
1686 | return void_list_node; | |
1687 | ||
1688 | /* Substitute into the rest of the list. */ | |
1689 | new_spec_types = | |
1690 | copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types), | |
1691 | TREE_CHAIN (tmpl_types)); | |
c8094d83 | 1692 | |
8afa707f MM |
1693 | /* Add the default argument for this parameter. */ |
1694 | return hash_tree_cons (TREE_PURPOSE (tmpl_types), | |
1695 | TREE_VALUE (spec_types), | |
1696 | new_spec_types); | |
1697 | } | |
1698 | ||
1699 | /* DECL is an explicit specialization. Replicate default arguments | |
1700 | from the template it specializes. (That way, code like: | |
1701 | ||
1702 | template <class T> void f(T = 3); | |
1703 | template <> void f(double); | |
c8094d83 | 1704 | void g () { f (); } |
8afa707f MM |
1705 | |
1706 | works, as required.) An alternative approach would be to look up | |
1707 | the correct default arguments at the call-site, but this approach | |
1708 | is consistent with how implicit instantiations are handled. */ | |
1709 | ||
1710 | static void | |
3a978d72 | 1711 | copy_default_args_to_explicit_spec (tree decl) |
8afa707f MM |
1712 | { |
1713 | tree tmpl; | |
1714 | tree spec_types; | |
1715 | tree tmpl_types; | |
1716 | tree new_spec_types; | |
1717 | tree old_type; | |
1718 | tree new_type; | |
1719 | tree t; | |
08c2df0f NS |
1720 | tree object_type = NULL_TREE; |
1721 | tree in_charge = NULL_TREE; | |
e0fff4b3 | 1722 | tree vtt = NULL_TREE; |
8afa707f MM |
1723 | |
1724 | /* See if there's anything we need to do. */ | |
1725 | tmpl = DECL_TI_TEMPLATE (decl); | |
1726 | tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl))); | |
1727 | for (t = tmpl_types; t; t = TREE_CHAIN (t)) | |
1728 | if (TREE_PURPOSE (t)) | |
1729 | break; | |
1730 | if (!t) | |
1731 | return; | |
1732 | ||
1733 | old_type = TREE_TYPE (decl); | |
1734 | spec_types = TYPE_ARG_TYPES (old_type); | |
c8094d83 | 1735 | |
8afa707f MM |
1736 | if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)) |
1737 | { | |
08c2df0f | 1738 | /* Remove the this pointer, but remember the object's type for |
0cbd7506 | 1739 | CV quals. */ |
08c2df0f | 1740 | object_type = TREE_TYPE (TREE_VALUE (spec_types)); |
8afa707f MM |
1741 | spec_types = TREE_CHAIN (spec_types); |
1742 | tmpl_types = TREE_CHAIN (tmpl_types); | |
c8094d83 | 1743 | |
8afa707f | 1744 | if (DECL_HAS_IN_CHARGE_PARM_P (decl)) |
0cbd7506 MS |
1745 | { |
1746 | /* DECL may contain more parameters than TMPL due to the extra | |
1747 | in-charge parameter in constructors and destructors. */ | |
1748 | in_charge = spec_types; | |
08c2df0f NS |
1749 | spec_types = TREE_CHAIN (spec_types); |
1750 | } | |
e0fff4b3 JM |
1751 | if (DECL_HAS_VTT_PARM_P (decl)) |
1752 | { | |
1753 | vtt = spec_types; | |
1754 | spec_types = TREE_CHAIN (spec_types); | |
1755 | } | |
8afa707f MM |
1756 | } |
1757 | ||
1758 | /* Compute the merged default arguments. */ | |
c8094d83 | 1759 | new_spec_types = |
8afa707f MM |
1760 | copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types); |
1761 | ||
08c2df0f NS |
1762 | /* Compute the new FUNCTION_TYPE. */ |
1763 | if (object_type) | |
8afa707f | 1764 | { |
e0fff4b3 | 1765 | if (vtt) |
0cbd7506 MS |
1766 | new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt), |
1767 | TREE_VALUE (vtt), | |
1768 | new_spec_types); | |
e0fff4b3 | 1769 | |
08c2df0f | 1770 | if (in_charge) |
0cbd7506 MS |
1771 | /* Put the in-charge parameter back. */ |
1772 | new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge), | |
1773 | TREE_VALUE (in_charge), | |
1774 | new_spec_types); | |
08c2df0f | 1775 | |
43dc123f MM |
1776 | new_type = build_method_type_directly (object_type, |
1777 | TREE_TYPE (old_type), | |
1778 | new_spec_types); | |
8afa707f | 1779 | } |
8afa707f MM |
1780 | else |
1781 | new_type = build_function_type (TREE_TYPE (old_type), | |
08c2df0f | 1782 | new_spec_types); |
e9525111 MM |
1783 | new_type = cp_build_type_attribute_variant (new_type, |
1784 | TYPE_ATTRIBUTES (old_type)); | |
8afa707f MM |
1785 | new_type = build_exception_variant (new_type, |
1786 | TYPE_RAISES_EXCEPTIONS (old_type)); | |
1787 | TREE_TYPE (decl) = new_type; | |
1788 | } | |
1789 | ||
386b8a85 | 1790 | /* Check to see if the function just declared, as indicated in |
75650646 MM |
1791 | DECLARATOR, and in DECL, is a specialization of a function |
1792 | template. We may also discover that the declaration is an explicit | |
1793 | instantiation at this point. | |
1794 | ||
e1467ff2 | 1795 | Returns DECL, or an equivalent declaration that should be used |
03017874 MM |
1796 | instead if all goes well. Issues an error message if something is |
1797 | amiss. Returns error_mark_node if the error is not easily | |
1798 | recoverable. | |
c8094d83 MS |
1799 | |
1800 | FLAGS is a bitmask consisting of the following flags: | |
75650646 | 1801 | |
75650646 MM |
1802 | 2: The function has a definition. |
1803 | 4: The function is a friend. | |
75650646 MM |
1804 | |
1805 | The TEMPLATE_COUNT is the number of references to qualifying | |
1806 | template classes that appeared in the name of the function. For | |
1807 | example, in | |
1808 | ||
1809 | template <class T> struct S { void f(); }; | |
1810 | void S<int>::f(); | |
c8094d83 | 1811 | |
75650646 MM |
1812 | the TEMPLATE_COUNT would be 1. However, explicitly specialized |
1813 | classes are not counted in the TEMPLATE_COUNT, so that in | |
1814 | ||
1815 | template <class T> struct S {}; | |
1816 | template <> struct S<int> { void f(); } | |
36a117a5 | 1817 | template <> void S<int>::f(); |
75650646 MM |
1818 | |
1819 | the TEMPLATE_COUNT would be 0. (Note that this declaration is | |
0e339752 | 1820 | invalid; there should be no template <>.) |
75650646 MM |
1821 | |
1822 | If the function is a specialization, it is marked as such via | |
1823 | DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO | |
c8094d83 | 1824 | is set up correctly, and it is added to the list of specializations |
75650646 | 1825 | for that template. */ |
386b8a85 | 1826 | |
e1467ff2 | 1827 | tree |
c8094d83 | 1828 | check_explicit_specialization (tree declarator, |
0cbd7506 MS |
1829 | tree decl, |
1830 | int template_count, | |
1831 | int flags) | |
386b8a85 | 1832 | { |
75650646 MM |
1833 | int have_def = flags & 2; |
1834 | int is_friend = flags & 4; | |
1835 | int specialization = 0; | |
e1467ff2 | 1836 | int explicit_instantiation = 0; |
fd4de5ff | 1837 | int member_specialization = 0; |
75650646 MM |
1838 | tree ctype = DECL_CLASS_CONTEXT (decl); |
1839 | tree dname = DECL_NAME (decl); | |
74b846e0 | 1840 | tmpl_spec_kind tsk; |
386b8a85 | 1841 | |
f65b7de3 GB |
1842 | if (is_friend) |
1843 | { | |
1844 | if (!processing_specialization) | |
1845 | tsk = tsk_none; | |
1846 | else | |
1847 | tsk = tsk_excessive_parms; | |
1848 | } | |
1849 | else | |
1850 | tsk = current_tmpl_spec_kind (template_count); | |
75650646 | 1851 | |
74b846e0 MM |
1852 | switch (tsk) |
1853 | { | |
1854 | case tsk_none: | |
c8094d83 | 1855 | if (processing_specialization) |
75650646 | 1856 | { |
fd4de5ff MM |
1857 | specialization = 1; |
1858 | SET_DECL_TEMPLATE_SPECIALIZATION (decl); | |
75650646 | 1859 | } |
74b846e0 | 1860 | else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR) |
386b8a85 | 1861 | { |
74b846e0 MM |
1862 | if (is_friend) |
1863 | /* This could be something like: | |
75650646 | 1864 | |
74b846e0 MM |
1865 | template <class T> void f(T); |
1866 | class S { friend void f<>(int); } */ | |
1867 | specialization = 1; | |
1868 | else | |
1869 | { | |
1870 | /* This case handles bogus declarations like template <> | |
1871 | template <class T> void f<int>(); */ | |
1872 | ||
0f51ccfc | 1873 | error ("template-id %qD in declaration of primary template", |
0cbd7506 | 1874 | declarator); |
74b846e0 MM |
1875 | return decl; |
1876 | } | |
1877 | } | |
1878 | break; | |
1879 | ||
1880 | case tsk_invalid_member_spec: | |
1881 | /* The error has already been reported in | |
1882 | check_specialization_scope. */ | |
1883 | return error_mark_node; | |
1884 | ||
1885 | case tsk_invalid_expl_inst: | |
33bd39a2 | 1886 | error ("template parameter list used in explicit instantiation"); |
74b846e0 MM |
1887 | |
1888 | /* Fall through. */ | |
1889 | ||
1890 | case tsk_expl_inst: | |
fd4de5ff | 1891 | if (have_def) |
33bd39a2 | 1892 | error ("definition provided for explicit instantiation"); |
c8094d83 | 1893 | |
fd4de5ff | 1894 | explicit_instantiation = 1; |
74b846e0 | 1895 | break; |
fd4de5ff | 1896 | |
74b846e0 | 1897 | case tsk_excessive_parms: |
f03adc6b GB |
1898 | case tsk_insufficient_parms: |
1899 | if (tsk == tsk_excessive_parms) | |
0cbd7506 | 1900 | error ("too many template parameter lists in declaration of %qD", |
f03adc6b GB |
1901 | decl); |
1902 | else if (template_header_count) | |
0f51ccfc | 1903 | error("too few template parameter lists in declaration of %qD", decl); |
f03adc6b | 1904 | else |
0f51ccfc | 1905 | error("explicit specialization of %qD must be introduced by " |
9e637a26 | 1906 | "%<template <>%>", decl); |
75650646 | 1907 | |
74b846e0 MM |
1908 | /* Fall through. */ |
1909 | case tsk_expl_spec: | |
1910 | SET_DECL_TEMPLATE_SPECIALIZATION (decl); | |
1911 | if (ctype) | |
1912 | member_specialization = 1; | |
1913 | else | |
1914 | specialization = 1; | |
1915 | break; | |
74b846e0 MM |
1916 | |
1917 | case tsk_template: | |
1918 | if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR) | |
c6f2ed0d | 1919 | { |
fd4de5ff MM |
1920 | /* This case handles bogus declarations like template <> |
1921 | template <class T> void f<int>(); */ | |
75650646 | 1922 | |
655dc6ee | 1923 | if (uses_template_parms (declarator)) |
0f51ccfc | 1924 | error ("function template partial specialization %qD " |
e1e93ad8 | 1925 | "is not allowed", declarator); |
655dc6ee | 1926 | else |
0f51ccfc | 1927 | error ("template-id %qD in declaration of primary template", |
0cbd7506 | 1928 | declarator); |
fd4de5ff | 1929 | return decl; |
386b8a85 | 1930 | } |
74b846e0 MM |
1931 | |
1932 | if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype)) | |
1933 | /* This is a specialization of a member template, without | |
1934 | specialization the containing class. Something like: | |
1935 | ||
1936 | template <class T> struct S { | |
c8094d83 | 1937 | template <class U> void f (U); |
0cbd7506 | 1938 | }; |
74b846e0 | 1939 | template <> template <class U> void S<int>::f(U) {} |
c8094d83 | 1940 | |
74b846e0 MM |
1941 | That's a specialization -- but of the entire template. */ |
1942 | specialization = 1; | |
1943 | break; | |
1944 | ||
1945 | default: | |
315fb5db | 1946 | gcc_unreachable (); |
75650646 | 1947 | } |
386b8a85 | 1948 | |
670960ac JM |
1949 | if (specialization || member_specialization) |
1950 | { | |
1951 | tree t = TYPE_ARG_TYPES (TREE_TYPE (decl)); | |
1952 | for (; t; t = TREE_CHAIN (t)) | |
1953 | if (TREE_PURPOSE (t)) | |
1954 | { | |
33bd39a2 | 1955 | pedwarn |
8251199e | 1956 | ("default argument specified in explicit specialization"); |
670960ac JM |
1957 | break; |
1958 | } | |
1959 | } | |
1960 | ||
e1467ff2 | 1961 | if (specialization || member_specialization || explicit_instantiation) |
75650646 MM |
1962 | { |
1963 | tree tmpl = NULL_TREE; | |
1964 | tree targs = NULL_TREE; | |
75650646 MM |
1965 | |
1966 | /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */ | |
386b8a85 JM |
1967 | if (TREE_CODE (declarator) != TEMPLATE_ID_EXPR) |
1968 | { | |
1969 | tree fns; | |
1970 | ||
50bc768d | 1971 | gcc_assert (TREE_CODE (declarator) == IDENTIFIER_NODE); |
2f54a1db | 1972 | if (ctype) |
386b8a85 | 1973 | fns = dname; |
2f54a1db GB |
1974 | else |
1975 | { | |
1976 | /* If there is no class context, the explicit instantiation | |
0cbd7506 | 1977 | must be at namespace scope. */ |
50bc768d | 1978 | gcc_assert (DECL_NAMESPACE_SCOPE_P (decl)); |
2f54a1db GB |
1979 | |
1980 | /* Find the namespace binding, using the declaration | |
0cbd7506 | 1981 | context. */ |
ddf74938 | 1982 | fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname, |
664a90c0 | 1983 | false, true); |
4230cec2 NS |
1984 | if (!fns || !is_overloaded_fn (fns)) |
1985 | { | |
1986 | error ("%qD is not a template function", dname); | |
1987 | fns = error_mark_node; | |
1988 | } | |
664a90c0 JM |
1989 | else |
1990 | { | |
1991 | tree fn = OVL_CURRENT (fns); | |
ddf74938 | 1992 | if (!is_associated_namespace (CP_DECL_CONTEXT (decl), |
08d295c5 JM |
1993 | CP_DECL_CONTEXT (fn))) |
1994 | error ("%qD is not declared in %qD", | |
1995 | decl, current_namespace); | |
664a90c0 | 1996 | } |
2f54a1db | 1997 | } |
386b8a85 | 1998 | |
2f54a1db | 1999 | declarator = lookup_template_function (fns, NULL_TREE); |
386b8a85 JM |
2000 | } |
2001 | ||
f2e48b67 BK |
2002 | if (declarator == error_mark_node) |
2003 | return error_mark_node; | |
2004 | ||
75650646 MM |
2005 | if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype)) |
2006 | { | |
8ec2ac34 | 2007 | if (!explicit_instantiation) |
0e339752 | 2008 | /* A specialization in class scope. This is invalid, |
6c30752f MM |
2009 | but the error will already have been flagged by |
2010 | check_specialization_scope. */ | |
2011 | return error_mark_node; | |
8ec2ac34 | 2012 | else |
b370501f | 2013 | { |
0e339752 | 2014 | /* It's not valid to write an explicit instantiation in |
b370501f | 2015 | class scope, e.g.: |
8ec2ac34 | 2016 | |
0cbd7506 | 2017 | class C { template void f(); } |
8ec2ac34 | 2018 | |
b370501f KG |
2019 | This case is caught by the parser. However, on |
2020 | something like: | |
c8094d83 | 2021 | |
b370501f | 2022 | template class C { void f(); }; |
8ec2ac34 | 2023 | |
0e339752 | 2024 | (which is invalid) we can get here. The error will be |
b370501f KG |
2025 | issued later. */ |
2026 | ; | |
2027 | } | |
8ec2ac34 | 2028 | |
e1467ff2 | 2029 | return decl; |
75650646 | 2030 | } |
c8094d83 | 2031 | else if (ctype != NULL_TREE |
75650646 MM |
2032 | && (TREE_CODE (TREE_OPERAND (declarator, 0)) == |
2033 | IDENTIFIER_NODE)) | |
386b8a85 | 2034 | { |
75650646 MM |
2035 | /* Find the list of functions in ctype that have the same |
2036 | name as the declared function. */ | |
2037 | tree name = TREE_OPERAND (declarator, 0); | |
03017874 MM |
2038 | tree fns = NULL_TREE; |
2039 | int idx; | |
2040 | ||
8ba658ee | 2041 | if (constructor_name_p (name, ctype)) |
386b8a85 | 2042 | { |
75650646 | 2043 | int is_constructor = DECL_CONSTRUCTOR_P (decl); |
c8094d83 | 2044 | |
75650646 | 2045 | if (is_constructor ? !TYPE_HAS_CONSTRUCTOR (ctype) |
9f4faeae | 2046 | : !CLASSTYPE_DESTRUCTORS (ctype)) |
75650646 MM |
2047 | { |
2048 | /* From [temp.expl.spec]: | |
c8094d83 | 2049 | |
75650646 MM |
2050 | If such an explicit specialization for the member |
2051 | of a class template names an implicitly-declared | |
2052 | special member function (clause _special_), the | |
c8094d83 | 2053 | program is ill-formed. |
e1467ff2 MM |
2054 | |
2055 | Similar language is found in [temp.explicit]. */ | |
33bd39a2 | 2056 | error ("specialization of implicitly-declared special member function"); |
03017874 | 2057 | return error_mark_node; |
75650646 | 2058 | } |
386b8a85 | 2059 | |
42da2fd8 | 2060 | name = is_constructor ? ctor_identifier : dtor_identifier; |
75650646 | 2061 | } |
42da2fd8 | 2062 | |
421844e7 | 2063 | if (!DECL_CONV_FN_P (decl)) |
03017874 MM |
2064 | { |
2065 | idx = lookup_fnfields_1 (ctype, name); | |
2066 | if (idx >= 0) | |
aaaa46d2 | 2067 | fns = VEC_index (tree, CLASSTYPE_METHOD_VEC (ctype), idx); |
03017874 MM |
2068 | } |
2069 | else | |
2070 | { | |
d4e6fecb | 2071 | VEC(tree,gc) *methods; |
aaaa46d2 | 2072 | tree ovl; |
03017874 MM |
2073 | |
2074 | /* For a type-conversion operator, we cannot do a | |
2075 | name-based lookup. We might be looking for `operator | |
2076 | int' which will be a specialization of `operator T'. | |
2077 | So, we find *all* the conversion operators, and then | |
2078 | select from them. */ | |
2079 | fns = NULL_TREE; | |
2080 | ||
2081 | methods = CLASSTYPE_METHOD_VEC (ctype); | |
2082 | if (methods) | |
5dd236e2 | 2083 | for (idx = CLASSTYPE_FIRST_CONVERSION_SLOT; |
9ba5ff0f | 2084 | VEC_iterate (tree, methods, idx, ovl); |
aaaa46d2 | 2085 | ++idx) |
03017874 | 2086 | { |
aaaa46d2 | 2087 | if (!DECL_CONV_FN_P (OVL_CURRENT (ovl))) |
03017874 MM |
2088 | /* There are no more conversion functions. */ |
2089 | break; | |
2090 | ||
2091 | /* Glue all these conversion functions together | |
2092 | with those we already have. */ | |
2093 | for (; ovl; ovl = OVL_NEXT (ovl)) | |
2094 | fns = ovl_cons (OVL_CURRENT (ovl), fns); | |
2095 | } | |
2096 | } | |
c8094d83 MS |
2097 | |
2098 | if (fns == NULL_TREE) | |
386b8a85 | 2099 | { |
0f51ccfc | 2100 | error ("no member function %qD declared in %qT", name, ctype); |
03017874 | 2101 | return error_mark_node; |
386b8a85 JM |
2102 | } |
2103 | else | |
2104 | TREE_OPERAND (declarator, 0) = fns; | |
2105 | } | |
c8094d83 | 2106 | |
e1467ff2 MM |
2107 | /* Figure out what exactly is being specialized at this point. |
2108 | Note that for an explicit instantiation, even one for a | |
38e01259 | 2109 | member function, we cannot tell apriori whether the |
e1467ff2 | 2110 | instantiation is for a member template, or just a member |
36a117a5 MM |
2111 | function of a template class. Even if a member template is |
2112 | being instantiated, the member template arguments may be | |
2113 | elided if they can be deduced from the rest of the | |
2114 | declaration. */ | |
e1467ff2 | 2115 | tmpl = determine_specialization (declarator, decl, |
c8094d83 | 2116 | &targs, |
5fe7b654 | 2117 | member_specialization, |
29a1da1c MM |
2118 | template_count, |
2119 | tsk); | |
c8094d83 | 2120 | |
03017874 MM |
2121 | if (!tmpl || tmpl == error_mark_node) |
2122 | /* We couldn't figure out what this declaration was | |
2123 | specializing. */ | |
2124 | return error_mark_node; | |
2125 | else | |
386b8a85 | 2126 | { |
25aab5d0 | 2127 | tree gen_tmpl = most_general_template (tmpl); |
36a117a5 | 2128 | |
e1467ff2 MM |
2129 | if (explicit_instantiation) |
2130 | { | |
03d0f4af | 2131 | /* We don't set DECL_EXPLICIT_INSTANTIATION here; that |
c8094d83 | 2132 | is done by do_decl_instantiation later. */ |
25aab5d0 MM |
2133 | |
2134 | int arg_depth = TMPL_ARGS_DEPTH (targs); | |
2135 | int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)); | |
2136 | ||
2137 | if (arg_depth > parm_depth) | |
2138 | { | |
2139 | /* If TMPL is not the most general template (for | |
2140 | example, if TMPL is a friend template that is | |
2141 | injected into namespace scope), then there will | |
dc957d14 | 2142 | be too many levels of TARGS. Remove some of them |
25aab5d0 MM |
2143 | here. */ |
2144 | int i; | |
2145 | tree new_targs; | |
2146 | ||
f31c0a32 | 2147 | new_targs = make_tree_vec (parm_depth); |
25aab5d0 MM |
2148 | for (i = arg_depth - parm_depth; i < arg_depth; ++i) |
2149 | TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth)) | |
2150 | = TREE_VEC_ELT (targs, i); | |
2151 | targs = new_targs; | |
2152 | } | |
c8094d83 | 2153 | |
3e4a3562 | 2154 | return instantiate_template (tmpl, targs, tf_error); |
e1467ff2 | 2155 | } |
74b846e0 | 2156 | |
6c07f448 KL |
2157 | /* If we thought that the DECL was a member function, but it |
2158 | turns out to be specializing a static member function, | |
4546865e | 2159 | make DECL a static member function as well. */ |
6c07f448 KL |
2160 | if (DECL_STATIC_FUNCTION_P (tmpl) |
2161 | && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)) | |
4546865e | 2162 | revert_static_member_fn (decl); |
6c07f448 | 2163 | |
f9a7ae04 | 2164 | /* If this is a specialization of a member template of a |
29a1da1c MM |
2165 | template class, we want to return the TEMPLATE_DECL, not |
2166 | the specialization of it. */ | |
74b846e0 MM |
2167 | if (tsk == tsk_template) |
2168 | { | |
2169 | SET_DECL_TEMPLATE_SPECIALIZATION (tmpl); | |
f9a7ae04 | 2170 | DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl)) = NULL_TREE; |
b190f239 NS |
2171 | if (have_def) |
2172 | { | |
f31686a3 RH |
2173 | DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl); |
2174 | DECL_SOURCE_LOCATION (DECL_TEMPLATE_RESULT (tmpl)) | |
2175 | = DECL_SOURCE_LOCATION (decl); | |
08167d1c AO |
2176 | /* We want to use the argument list specified in the |
2177 | definition, not in the original declaration. */ | |
2178 | DECL_ARGUMENTS (DECL_TEMPLATE_RESULT (tmpl)) | |
2179 | = DECL_ARGUMENTS (decl); | |
b190f239 | 2180 | } |
74b846e0 MM |
2181 | return tmpl; |
2182 | } | |
2183 | ||
36a117a5 | 2184 | /* Set up the DECL_TEMPLATE_INFO for DECL. */ |
e1b3e07d | 2185 | DECL_TEMPLATE_INFO (decl) = tree_cons (tmpl, targs, NULL_TREE); |
36a117a5 | 2186 | |
8afa707f MM |
2187 | /* Inherit default function arguments from the template |
2188 | DECL is specializing. */ | |
2189 | copy_default_args_to_explicit_spec (decl); | |
2190 | ||
c750255c MM |
2191 | /* This specialization has the same protection as the |
2192 | template it specializes. */ | |
2193 | TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl); | |
2194 | TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl); | |
b9e75696 | 2195 | |
9c12301f MM |
2196 | /* If DECL is a friend declaration, declared using an |
2197 | unqualified name, the namespace associated with DECL may | |
2198 | have been set incorrectly. For example, in: | |
3db45ab5 MS |
2199 | |
2200 | template <typename T> void f(T); | |
2201 | namespace N { | |
2202 | struct S { friend void f<int>(int); } | |
2203 | } | |
2204 | ||
2205 | we will have set the DECL_CONTEXT for the friend | |
2206 | declaration to N, rather than to the global namespace. */ | |
9c12301f MM |
2207 | if (DECL_NAMESPACE_SCOPE_P (decl)) |
2208 | DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl); | |
8d039470 | 2209 | |
386b8a85 | 2210 | if (is_friend && !have_def) |
36a117a5 MM |
2211 | /* This is not really a declaration of a specialization. |
2212 | It's just the name of an instantiation. But, it's not | |
2213 | a request for an instantiation, either. */ | |
fbf1c34b | 2214 | SET_DECL_IMPLICIT_INSTANTIATION (decl); |
08511114 KL |
2215 | else if (DECL_CONSTRUCTOR_P (decl) || DECL_DESTRUCTOR_P (decl)) |
2216 | /* This is indeed a specialization. In case of constructors | |
2217 | and destructors, we need in-charge and not-in-charge | |
2218 | versions in V3 ABI. */ | |
2219 | clone_function_decl (decl, /*update_method_vec_p=*/0); | |
75650646 | 2220 | |
36a117a5 MM |
2221 | /* Register this specialization so that we can find it |
2222 | again. */ | |
d63d5d0c | 2223 | decl = register_specialization (decl, gen_tmpl, targs, is_friend); |
386b8a85 JM |
2224 | } |
2225 | } | |
c8094d83 | 2226 | |
e1467ff2 | 2227 | return decl; |
386b8a85 | 2228 | } |
75650646 | 2229 | |
75650646 MM |
2230 | /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template |
2231 | parameters. These are represented in the same format used for | |
2232 | DECL_TEMPLATE_PARMS. */ | |
2233 | ||
50bc768d NS |
2234 | int |
2235 | comp_template_parms (tree parms1, tree parms2) | |
75650646 MM |
2236 | { |
2237 | tree p1; | |
2238 | tree p2; | |
2239 | ||
2240 | if (parms1 == parms2) | |
2241 | return 1; | |
2242 | ||
c8094d83 | 2243 | for (p1 = parms1, p2 = parms2; |
75650646 MM |
2244 | p1 != NULL_TREE && p2 != NULL_TREE; |
2245 | p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2)) | |
2246 | { | |
2247 | tree t1 = TREE_VALUE (p1); | |
2248 | tree t2 = TREE_VALUE (p2); | |
2249 | int i; | |
2250 | ||
50bc768d NS |
2251 | gcc_assert (TREE_CODE (t1) == TREE_VEC); |
2252 | gcc_assert (TREE_CODE (t2) == TREE_VEC); | |
75650646 MM |
2253 | |
2254 | if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2)) | |
2255 | return 0; | |
2256 | ||
c8094d83 | 2257 | for (i = 0; i < TREE_VEC_LENGTH (t2); ++i) |
75650646 | 2258 | { |
0f67a82f LM |
2259 | tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i)); |
2260 | tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i)); | |
42b304f1 | 2261 | |
0f67a82f LM |
2262 | /* If either of the template parameters are invalid, assume |
2263 | they match for the sake of error recovery. */ | |
2264 | if (parm1 == error_mark_node || parm2 == error_mark_node) | |
2265 | return 1; | |
75650646 MM |
2266 | |
2267 | if (TREE_CODE (parm1) != TREE_CODE (parm2)) | |
2268 | return 0; | |
2269 | ||
5d80a306 DG |
2270 | if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM |
2271 | && (TEMPLATE_TYPE_PARAMETER_PACK (parm1) | |
2272 | == TEMPLATE_TYPE_PARAMETER_PACK (parm2))) | |
75650646 | 2273 | continue; |
3bfdc719 | 2274 | else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2))) |
75650646 MM |
2275 | return 0; |
2276 | } | |
2277 | } | |
2278 | ||
2279 | if ((p1 != NULL_TREE) != (p2 != NULL_TREE)) | |
2280 | /* One set of parameters has more parameters lists than the | |
2281 | other. */ | |
2282 | return 0; | |
2283 | ||
2284 | return 1; | |
2285 | } | |
2286 | ||
5d80a306 DG |
2287 | /* Determine whether PARM is a parameter pack. */ |
2288 | bool | |
2289 | template_parameter_pack_p (tree parm) | |
2290 | { | |
2291 | /* Determine if we have a non-type template parameter pack. */ | |
2292 | if (TREE_CODE (parm) == PARM_DECL) | |
2293 | return (DECL_TEMPLATE_PARM_P (parm) | |
2294 | && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))); | |
2295 | ||
2296 | /* If this is a list of template parameters, we could get a | |
2297 | TYPE_DECL or a TEMPLATE_DECL. */ | |
2298 | if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL) | |
2299 | parm = TREE_TYPE (parm); | |
2300 | ||
2301 | return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM | |
2302 | || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM) | |
2303 | && TEMPLATE_TYPE_PARAMETER_PACK (parm)); | |
2304 | } | |
2305 | ||
2306 | /* Determine whether PARMS describes a variadic template parameter | |
2307 | list, i.e., one that is terminated by a template parameter pack. */ | |
2308 | bool | |
2309 | template_parms_variadic_p (tree parms) | |
2310 | { | |
2311 | int nparms = TREE_VEC_LENGTH (parms); | |
2312 | tree last_parm = TREE_VALUE (TREE_VEC_ELT (parms, nparms - 1)); | |
2313 | ||
2314 | return template_parameter_pack_p (last_parm); | |
2315 | } | |
2316 | ||
2317 | /* Determine whether ARGS describes a variadic template args list, | |
2318 | i.e., one that is terminated by a template argument pack. */ | |
2319 | static bool | |
2320 | template_args_variadic_p (tree args) | |
2321 | { | |
2322 | int nargs; | |
2323 | tree last_parm; | |
2324 | ||
2325 | if (args == NULL_TREE) | |
2326 | return false; | |
2327 | ||
2328 | args = INNERMOST_TEMPLATE_ARGS (args); | |
2329 | nargs = TREE_VEC_LENGTH (args); | |
2330 | ||
2331 | if (nargs == 0) | |
2332 | return false; | |
2333 | ||
2334 | last_parm = TREE_VEC_ELT (args, nargs - 1); | |
2335 | ||
2336 | return ARGUMENT_PACK_P (last_parm); | |
2337 | } | |
2338 | ||
2339 | /* Generate a new name for the parameter pack name NAME (an | |
2340 | IDENTIFIER_NODE) that incorporates its */ | |
2341 | static tree | |
2342 | make_ith_pack_parameter_name (tree name, int i) | |
2343 | { | |
2344 | /* Munge the name to include the parameter index. */ | |
2345 | char numbuf[128]; | |
2346 | char* newname; | |
2347 | ||
2348 | sprintf(numbuf, "%i", i); | |
2349 | newname = (char*)alloca (IDENTIFIER_LENGTH (name) + strlen(numbuf) + 2); | |
2350 | sprintf(newname, "%s#%i", IDENTIFIER_POINTER (name), i); | |
2351 | return get_identifier (newname); | |
2352 | } | |
2353 | ||
2354 | /* Structure used to track the progress of find_parameter_pack_r. */ | |
2355 | struct find_parameter_pack_data | |
2356 | { | |
2357 | tree* parameter_packs; | |
2358 | struct pointer_set_t *visited; | |
2359 | }; | |
2360 | ||
2361 | /* Identifiers all of the argument packs that occur in a template | |
2362 | argument and appends them to the TREE_LIST inside DATA, which is a | |
2363 | find_parameter_pack_Data structure. This is a subroutine of | |
2364 | make_pack_expansion and uses_parameter_packs. */ | |
2365 | static tree | |
2366 | find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data) | |
2367 | { | |
2368 | tree t = *tp; | |
2369 | struct find_parameter_pack_data* ppd = | |
2370 | (struct find_parameter_pack_data*)data; | |
2371 | ||
2372 | if (TYPE_P (t)) | |
2373 | { | |
2374 | tree context = TYPE_CONTEXT (t); | |
2375 | walk_tree (&context, &find_parameter_packs_r, ppd, ppd->visited); | |
2376 | } | |
2377 | ||
2378 | /* This switch statement will return immediately if we don't find a | |
2379 | parameter pack. */ | |
2380 | switch (TREE_CODE (t)) | |
2381 | { | |
2382 | case TEMPLATE_PARM_INDEX: | |
2383 | if (TEMPLATE_PARM_PARAMETER_PACK (t)) | |
2384 | break; | |
2385 | return NULL_TREE; | |
2386 | ||
2387 | case BOUND_TEMPLATE_TEMPLATE_PARM: | |
2388 | /* Check the template arguments. */ | |
2389 | walk_tree (&TYPE_TI_ARGS (t), &find_parameter_packs_r, ppd, | |
2390 | ppd->visited); | |
2391 | ||
2392 | /* Dig out the underlying TEMPLATE_TEMPLATE_PARM. */ | |
2393 | t = TYPE_TI_TEMPLATE (t); | |
2394 | if (DECL_P (t) && TREE_TYPE (t)) | |
2395 | t = TREE_TYPE (t); | |
2396 | *walk_subtrees = 0; | |
2397 | ||
2398 | /* Fall through. */ | |
2399 | ||
2400 | case TEMPLATE_TYPE_PARM: | |
2401 | case TEMPLATE_TEMPLATE_PARM: | |
2402 | if (TEMPLATE_TYPE_PARAMETER_PACK (t)) | |
2403 | break; | |
2404 | return NULL_TREE; | |
2405 | ||
2406 | case PARM_DECL: | |
2407 | if (FUNCTION_PARAMETER_PACK_P (t)) | |
2408 | { | |
2409 | /* We don't want to walk into the type of a PARM_DECL, | |
2410 | because we don't want to see the type parameter pack.*/ | |
2411 | *walk_subtrees = 0; | |
2412 | break; | |
2413 | } | |
2414 | return NULL_TREE; | |
2415 | ||
2416 | case RECORD_TYPE: | |
2417 | if (TYPE_PTRMEMFUNC_P (t)) | |
2418 | return NULL_TREE; | |
2419 | /* Fall through. */ | |
2420 | ||
2421 | case UNION_TYPE: | |
2422 | case ENUMERAL_TYPE: | |
2423 | if (TYPE_TEMPLATE_INFO (t)) | |
2424 | { | |
2425 | tree args = TREE_VALUE (TYPE_TEMPLATE_INFO (t)); | |
2426 | walk_tree (&args, &find_parameter_packs_r, ppd, ppd->visited); | |
2427 | } | |
2428 | ||
2429 | *walk_subtrees = 0; | |
2430 | return NULL_TREE; | |
2431 | ||
2432 | case TEMPLATE_DECL: | |
2433 | if (DECL_TEMPLATE_TEMPLATE_PARM_P (t) | |
2434 | && TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (t))) | |
2435 | break; | |
2436 | ||
2437 | *walk_subtrees = 0; | |
2438 | return NULL_TREE; | |
2439 | ||
2440 | case TYPE_PACK_EXPANSION: | |
2441 | case EXPR_PACK_EXPANSION: | |
2442 | *walk_subtrees = 0; | |
2443 | return NULL_TREE; | |
2444 | ||
2445 | default: | |
2446 | return NULL_TREE; | |
2447 | } | |
2448 | ||
2449 | /* Add this parameter pack to the list. */ | |
2450 | *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs); | |
2451 | ||
2452 | return NULL_TREE; | |
2453 | } | |
2454 | ||
2455 | /* Determines if the expression or type T uses any parameter packs. */ | |
2456 | bool | |
2457 | uses_parameter_packs (tree t) | |
2458 | { | |
2459 | tree parameter_packs = NULL_TREE; | |
2460 | struct find_parameter_pack_data ppd; | |
2461 | ppd.parameter_packs = ¶meter_packs; | |
2462 | ppd.visited = pointer_set_create (); | |
2463 | walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited); | |
2464 | return parameter_packs != NULL_TREE; | |
2465 | } | |
2466 | ||
2467 | /* Turn ARG, which may be an expression, type, or a TREE_LIST | |
2468 | representation a base-class initializer into a parameter pack | |
2469 | expansion. If all goes well, the resulting node will be an | |
2470 | EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST, | |
2471 | respectively. */ | |
2472 | tree | |
2473 | make_pack_expansion (tree arg) | |
2474 | { | |
2475 | tree result; | |
2476 | tree parameter_packs = NULL_TREE; | |
2477 | bool for_types = false; | |
2478 | struct find_parameter_pack_data ppd; | |
2479 | ||
2480 | if (!arg || arg == error_mark_node) | |
2481 | return arg; | |
2482 | ||
2483 | if (TREE_CODE (arg) == TREE_LIST) | |
2484 | { | |
2485 | /* The only time we will see a TREE_LIST here is for a base | |
2486 | class initializer. In this case, the TREE_PURPOSE will be a | |
2487 | _TYPE node (representing the base class expansion we're | |
2488 | initializing) and the TREE_VALUE will be a TREE_LIST | |
2489 | containing the initialization arguments. | |
2490 | ||
2491 | The resulting expansion looks somewhat different from most | |
2492 | expansions. Rather than returning just one _EXPANSION, we | |
2493 | return a TREE_LIST whose TREE_PURPOSE is a | |
2494 | TYPE_PACK_EXPANSION containing the bases that will be | |
2495 | initialized. The TREE_VALUE will be identical to the | |
2496 | original TREE_VALUE, which is a list of arguments that will | |
2497 | be passed to each base. We do not introduce any new pack | |
2498 | expansion nodes into the TREE_VALUE (although it is possible | |
2499 | that some already exist), because the TREE_PURPOSE and | |
2500 | TREE_VALUE all need to be expanded together with the same | |
2501 | _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the | |
2502 | resulting TREE_PURPOSE will mention the parameter packs in | |
2503 | both the bases and the arguments to the bases. */ | |
2504 | tree purpose; | |
2505 | tree value; | |
2506 | tree parameter_packs = NULL_TREE; | |
2507 | ||
2508 | /* Determine which parameter packs will be used by the base | |
2509 | class expansion. */ | |
2510 | ppd.visited = pointer_set_create (); | |
2511 | ppd.parameter_packs = ¶meter_packs; | |
2512 | walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r, | |
2513 | &ppd, ppd.visited); | |
2514 | ||
2515 | if (parameter_packs == NULL_TREE) | |
2516 | { | |
2517 | error ("base initializer expansion %<%T%> contains no parameter packs", arg); | |
2518 | return error_mark_node; | |
2519 | } | |
2520 | ||
2521 | if (TREE_VALUE (arg) != void_type_node) | |
2522 | { | |
2523 | /* Collect the sets of parameter packs used in each of the | |
2524 | initialization arguments. */ | |
2525 | for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value)) | |
2526 | { | |
2527 | /* Determine which parameter packs will be expanded in this | |
2528 | argument. */ | |
2529 | walk_tree (&TREE_VALUE (value), &find_parameter_packs_r, | |
2530 | &ppd, ppd.visited); | |
2531 | } | |
2532 | } | |
2533 | ||
2534 | /* Create the pack expansion type for the base type. */ | |
2535 | purpose = make_node (TYPE_PACK_EXPANSION); | |
2536 | SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg)); | |
2537 | PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs; | |
2538 | ||
2539 | /* Just use structural equality for these TYPE_PACK_EXPANSIONS; | |
2540 | they will rarely be compared to anything. */ | |
2541 | SET_TYPE_STRUCTURAL_EQUALITY (purpose); | |
2542 | ||
2543 | return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE); | |
2544 | } | |
2545 | ||
2546 | if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL) | |
2547 | for_types = true; | |
2548 | ||
2549 | /* Build the PACK_EXPANSION_* node. */ | |
2550 | result = make_node (for_types ? TYPE_PACK_EXPANSION : EXPR_PACK_EXPANSION); | |
2551 | SET_PACK_EXPANSION_PATTERN (result, arg); | |
2552 | if (TREE_CODE (result) == EXPR_PACK_EXPANSION) | |
2553 | { | |
2554 | /* Propagate type and const-expression information. */ | |
2555 | TREE_TYPE (result) = TREE_TYPE (arg); | |
2556 | TREE_CONSTANT (result) = TREE_CONSTANT (arg); | |
2557 | } | |
2558 | else | |
2559 | /* Just use structural equality for these TYPE_PACK_EXPANSIONS; | |
2560 | they will rarely be compared to anything. */ | |
2561 | SET_TYPE_STRUCTURAL_EQUALITY (result); | |
2562 | ||
2563 | /* Determine which parameter packs will be expanded. */ | |
2564 | ppd.parameter_packs = ¶meter_packs; | |
2565 | ppd.visited = pointer_set_create (); | |
2566 | walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited); | |
2567 | ||
2568 | /* Make sure we found some parameter packs. */ | |
2569 | if (parameter_packs == NULL_TREE) | |
2570 | { | |
2571 | if (TYPE_P (arg)) | |
2572 | error ("expansion pattern %<%T%> contains no argument packs", arg); | |
2573 | else | |
2574 | error ("expansion pattern %<%E%> contains no argument packs", arg); | |
2575 | return error_mark_node; | |
2576 | } | |
2577 | PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs; | |
2578 | ||
2579 | return result; | |
2580 | } | |
2581 | ||
2582 | /* Checks T for any "bare" parameter packs, which have not yet been | |
2583 | expanded, and issues an error if any are found. This operation can | |
2584 | only be done on full expressions or types (e.g., an expression | |
2585 | statement, "if" condition, etc.), because we could have expressions like: | |
2586 | ||
2587 | foo(f(g(h(args)))...) | |
2588 | ||
2589 | where "args" is a parameter pack. check_for_bare_parameter_packs | |
2590 | should not be called for the subexpressions args, h(args), | |
2591 | g(h(args)), or f(g(h(args))), because we would produce erroneous | |
2592 | error messages. */ | |
2593 | void | |
2594 | check_for_bare_parameter_packs (tree t) | |
2595 | { | |
2596 | tree parameter_packs = NULL_TREE; | |
2597 | struct find_parameter_pack_data ppd; | |
2598 | ||
2599 | if (!processing_template_decl || !t || t == error_mark_node) | |
2600 | return; | |
2601 | ||
2602 | if (TREE_CODE (t) == TYPE_DECL) | |
2603 | t = TREE_TYPE (t); | |
2604 | ||
2605 | ppd.parameter_packs = ¶meter_packs; | |
2606 | ppd.visited = pointer_set_create (); | |
2607 | walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited); | |
2608 | ||
2609 | if (parameter_packs) { | |
2610 | error ("parameter packs not expanded with `...':"); | |
2611 | while (parameter_packs) | |
2612 | { | |
2613 | tree pack = TREE_VALUE (parameter_packs); | |
2614 | tree name = NULL_TREE; | |
2615 | ||
2616 | if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM | |
2617 | || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM) | |
2618 | name = TYPE_NAME (pack); | |
2619 | else | |
2620 | name = DECL_NAME (pack); | |
2621 | inform (" %qD", name); | |
2622 | ||
2623 | parameter_packs = TREE_CHAIN (parameter_packs); | |
2624 | } | |
2625 | } | |
2626 | } | |
2627 | ||
2628 | /* Expand any parameter packs that occur in the template arguments in | |
2629 | ARGS. */ | |
2630 | tree | |
2631 | expand_template_argument_pack (tree args) | |
2632 | { | |
2633 | tree result_args = NULL_TREE; | |
2634 | int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0; | |
2635 | int num_result_args = -1; | |
2636 | ||
2637 | /* First, determine if we need to expand anything, and the number of | |
2638 | slots we'll need. */ | |
2639 | for (in_arg = 0; in_arg < nargs; ++in_arg) | |
2640 | { | |
2641 | tree arg = TREE_VEC_ELT (args, in_arg); | |
2642 | if (ARGUMENT_PACK_P (arg)) | |
2643 | { | |
2644 | int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)); | |
2645 | if (num_result_args < 0) | |
2646 | num_result_args = in_arg + num_packed; | |
2647 | else | |
2648 | num_result_args += num_packed; | |
2649 | } | |
2650 | else | |
2651 | { | |
2652 | if (num_result_args >= 0) | |
2653 | num_result_args++; | |
2654 | } | |
2655 | } | |
2656 | ||
2657 | /* If no expansion is necessary, we're done. */ | |
2658 | if (num_result_args < 0) | |
2659 | return args; | |
2660 | ||
2661 | /* Expand arguments. */ | |
2662 | result_args = make_tree_vec (num_result_args); | |
2663 | for (in_arg = 0; in_arg < nargs; ++in_arg) | |
2664 | { | |
2665 | tree arg = TREE_VEC_ELT (args, in_arg); | |
2666 | if (ARGUMENT_PACK_P (arg)) | |
2667 | { | |
2668 | tree packed = ARGUMENT_PACK_ARGS (arg); | |
2669 | int i, num_packed = TREE_VEC_LENGTH (packed); | |
2670 | for (i = 0; i < num_packed; ++i, ++out_arg) | |
2671 | TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i); | |
2672 | } | |
2673 | else | |
2674 | { | |
2675 | TREE_VEC_ELT (result_args, out_arg) = arg; | |
2676 | ++out_arg; | |
2677 | } | |
2678 | } | |
2679 | ||
2680 | return result_args; | |
2681 | } | |
2682 | ||
f3400fe2 JM |
2683 | /* Complain if DECL shadows a template parameter. |
2684 | ||
2685 | [temp.local]: A template-parameter shall not be redeclared within its | |
2686 | scope (including nested scopes). */ | |
2687 | ||
2688 | void | |
3a978d72 | 2689 | check_template_shadow (tree decl) |
f3400fe2 | 2690 | { |
8f032717 MM |
2691 | tree olddecl; |
2692 | ||
b5d9b9ab MM |
2693 | /* If we're not in a template, we can't possibly shadow a template |
2694 | parameter. */ | |
2695 | if (!current_template_parms) | |
2696 | return; | |
2697 | ||
2698 | /* Figure out what we're shadowing. */ | |
8f032717 MM |
2699 | if (TREE_CODE (decl) == OVERLOAD) |
2700 | decl = OVL_CURRENT (decl); | |
90ea9897 | 2701 | olddecl = innermost_non_namespace_value (DECL_NAME (decl)); |
f3400fe2 | 2702 | |
b5d9b9ab MM |
2703 | /* If there's no previous binding for this name, we're not shadowing |
2704 | anything, let alone a template parameter. */ | |
2705 | if (!olddecl) | |
2706 | return; | |
2707 | ||
2708 | /* If we're not shadowing a template parameter, we're done. Note | |
2709 | that OLDDECL might be an OVERLOAD (or perhaps even an | |
2710 | ERROR_MARK), so we can't just blithely assume it to be a _DECL | |
2711 | node. */ | |
2f939d94 | 2712 | if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl)) |
b5d9b9ab MM |
2713 | return; |
2714 | ||
2715 | /* We check for decl != olddecl to avoid bogus errors for using a | |
2716 | name inside a class. We check TPFI to avoid duplicate errors for | |
2717 | inline member templates. */ | |
c8094d83 | 2718 | if (decl == olddecl |
b5d9b9ab MM |
2719 | || TEMPLATE_PARMS_FOR_INLINE (current_template_parms)) |
2720 | return; | |
2721 | ||
dee15844 JM |
2722 | error ("declaration of %q+#D", decl); |
2723 | error (" shadows template parm %q+#D", olddecl); | |
f3400fe2 | 2724 | } |
22a7be53 | 2725 | |
f3400fe2 | 2726 | /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL, |
f84b4be9 JM |
2727 | ORIG_LEVEL, DECL, and TYPE. */ |
2728 | ||
2729 | static tree | |
c8094d83 | 2730 | build_template_parm_index (int index, |
0cbd7506 MS |
2731 | int level, |
2732 | int orig_level, | |
2733 | tree decl, | |
2734 | tree type) | |
f84b4be9 JM |
2735 | { |
2736 | tree t = make_node (TEMPLATE_PARM_INDEX); | |
2737 | TEMPLATE_PARM_IDX (t) = index; | |
2738 | TEMPLATE_PARM_LEVEL (t) = level; | |
2739 | TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level; | |
2740 | TEMPLATE_PARM_DECL (t) = decl; | |
2741 | TREE_TYPE (t) = type; | |
3e4a3562 | 2742 | TREE_CONSTANT (t) = TREE_CONSTANT (decl); |
6de9cd9a | 2743 | TREE_INVARIANT (t) = TREE_INVARIANT (decl); |
3e4a3562 | 2744 | TREE_READONLY (t) = TREE_READONLY (decl); |
f84b4be9 JM |
2745 | |
2746 | return t; | |
2747 | } | |
2748 | ||
06d40de8 | 2749 | /* Find the canonical type parameter for the given template type |
c3e188fc KH |
2750 | parameter. Returns the canonical type parameter, which may be TYPE |
2751 | if no such parameter existed. */ | |
06d40de8 DG |
2752 | static tree |
2753 | canonical_type_parameter (tree type) | |
2754 | { | |
2755 | tree list; | |
2756 | int idx = TEMPLATE_TYPE_IDX (type); | |
2757 | if (!canonical_template_parms) | |
2758 | canonical_template_parms = VEC_alloc (tree, gc, idx+1); | |
2759 | ||
2760 | while (VEC_length (tree, canonical_template_parms) <= (unsigned)idx) | |
2761 | VEC_safe_push (tree, gc, canonical_template_parms, NULL_TREE); | |
2762 | ||
2763 | list = VEC_index (tree, canonical_template_parms, idx); | |
2764 | while (list && !comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL)) | |
2765 | list = TREE_CHAIN (list); | |
2766 | ||
2767 | if (list) | |
2768 | return TREE_VALUE (list); | |
2769 | else | |
2770 | { | |
2771 | VEC_replace(tree, canonical_template_parms, idx, | |
2772 | tree_cons (NULL_TREE, type, | |
2773 | VEC_index (tree, canonical_template_parms, idx))); | |
2774 | return type; | |
2775 | } | |
2776 | } | |
2777 | ||
f84b4be9 | 2778 | /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose |
93cdc044 | 2779 | TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a |
f84b4be9 JM |
2780 | TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a |
2781 | new one is created. */ | |
2782 | ||
c8094d83 | 2783 | static tree |
3a978d72 | 2784 | reduce_template_parm_level (tree index, tree type, int levels) |
f84b4be9 JM |
2785 | { |
2786 | if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE | |
2787 | || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index)) | |
93cdc044 | 2788 | != TEMPLATE_PARM_LEVEL (index) - levels)) |
f84b4be9 | 2789 | { |
3e4a3562 NS |
2790 | tree orig_decl = TEMPLATE_PARM_DECL (index); |
2791 | tree decl, t; | |
c8094d83 | 2792 | |
3e4a3562 NS |
2793 | decl = build_decl (TREE_CODE (orig_decl), DECL_NAME (orig_decl), type); |
2794 | TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl); | |
6de9cd9a | 2795 | TREE_INVARIANT (decl) = TREE_INVARIANT (orig_decl); |
3e4a3562 NS |
2796 | TREE_READONLY (decl) = TREE_READONLY (orig_decl); |
2797 | DECL_ARTIFICIAL (decl) = 1; | |
2798 | SET_DECL_TEMPLATE_PARM_P (decl); | |
c8094d83 | 2799 | |
3e4a3562 | 2800 | t = build_template_parm_index (TEMPLATE_PARM_IDX (index), |
93cdc044 | 2801 | TEMPLATE_PARM_LEVEL (index) - levels, |
f84b4be9 JM |
2802 | TEMPLATE_PARM_ORIG_LEVEL (index), |
2803 | decl, type); | |
2804 | TEMPLATE_PARM_DESCENDANTS (index) = t; | |
5d80a306 DG |
2805 | TEMPLATE_PARM_PARAMETER_PACK (t) |
2806 | = TEMPLATE_PARM_PARAMETER_PACK (index); | |
cae40af6 | 2807 | |
820cc88f DB |
2808 | /* Template template parameters need this. */ |
2809 | if (TREE_CODE (decl) != CONST_DECL) | |
2810 | DECL_TEMPLATE_PARMS (decl) | |
2811 | = DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index)); | |
f84b4be9 JM |
2812 | } |
2813 | ||
2814 | return TEMPLATE_PARM_DESCENDANTS (index); | |
2815 | } | |
2816 | ||
a14de1af | 2817 | /* Process information from new template parameter PARM and append it to the |
058b15c1 | 2818 | LIST being built. This new parameter is a non-type parameter iff |
5d80a306 DG |
2819 | IS_NON_TYPE is true. This new parameter is a parameter |
2820 | pack iff IS_PARAMETER_PACK is true. */ | |
e92cc029 | 2821 | |
8d08fdba | 2822 | tree |
5d80a306 DG |
2823 | process_template_parm (tree list, tree parm, bool is_non_type, |
2824 | bool is_parameter_pack) | |
8d08fdba | 2825 | { |
8d08fdba | 2826 | tree decl = 0; |
a292b002 | 2827 | tree defval; |
0f67a82f | 2828 | tree err_parm_list; |
d47e3adf | 2829 | int idx = 0; |
f84b4be9 | 2830 | |
50bc768d | 2831 | gcc_assert (TREE_CODE (parm) == TREE_LIST); |
a292b002 | 2832 | defval = TREE_PURPOSE (parm); |
5566b478 MS |
2833 | |
2834 | if (list) | |
2835 | { | |
d47e3adf LM |
2836 | tree p = tree_last (list); |
2837 | ||
0f67a82f | 2838 | if (p && TREE_VALUE (p) != error_mark_node) |
d47e3adf LM |
2839 | { |
2840 | p = TREE_VALUE (p); | |
2841 | if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL) | |
2842 | idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p)); | |
2843 | else | |
2844 | idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p)); | |
2845 | } | |
5566b478 | 2846 | |
5566b478 MS |
2847 | ++idx; |
2848 | } | |
2849 | else | |
2850 | idx = 0; | |
2851 | ||
058b15c1 | 2852 | if (is_non_type) |
8d08fdba | 2853 | { |
058b15c1 MM |
2854 | parm = TREE_VALUE (parm); |
2855 | ||
833aa4c4 | 2856 | SET_DECL_TEMPLATE_PARM_P (parm); |
d490621d | 2857 | |
620188c9 | 2858 | if (TREE_TYPE (parm) == error_mark_node) |
0f67a82f LM |
2859 | { |
2860 | err_parm_list = build_tree_list (defval, parm); | |
2861 | TREE_VALUE (err_parm_list) = error_mark_node; | |
2862 | return chainon (list, err_parm_list); | |
2863 | } | |
620188c9 VR |
2864 | else |
2865 | { | |
2866 | /* [temp.param] | |
d490621d | 2867 | |
620188c9 VR |
2868 | The top-level cv-qualifiers on the template-parameter are |
2869 | ignored when determining its type. */ | |
2870 | TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm)); | |
2871 | if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1)) | |
0f67a82f LM |
2872 | { |
2873 | err_parm_list = build_tree_list (defval, parm); | |
2874 | TREE_VALUE (err_parm_list) = error_mark_node; | |
2875 | return chainon (list, err_parm_list); | |
2876 | } | |
5d80a306 DG |
2877 | |
2878 | if (uses_parameter_packs (TREE_TYPE (parm)) && !is_parameter_pack) | |
2879 | { | |
2880 | /* This template parameter is not a parameter pack, but it | |
2881 | should be. Complain about "bare" parameter packs. */ | |
2882 | check_for_bare_parameter_packs (TREE_TYPE (parm)); | |
2883 | ||
2884 | /* Recover by calling this a parameter pack. */ | |
2885 | is_parameter_pack = true; | |
2886 | } | |
620188c9 | 2887 | } |
d490621d | 2888 | |
8d08fdba | 2889 | /* A template parameter is not modifiable. */ |
6de9cd9a DN |
2890 | TREE_CONSTANT (parm) = 1; |
2891 | TREE_INVARIANT (parm) = 1; | |
2892 | TREE_READONLY (parm) = 1; | |
8d08fdba | 2893 | decl = build_decl (CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm)); |
6de9cd9a DN |
2894 | TREE_CONSTANT (decl) = 1; |
2895 | TREE_INVARIANT (decl) = 1; | |
2896 | TREE_READONLY (decl) = 1; | |
c8094d83 | 2897 | DECL_INITIAL (parm) = DECL_INITIAL (decl) |
f84b4be9 JM |
2898 | = build_template_parm_index (idx, processing_template_decl, |
2899 | processing_template_decl, | |
2900 | decl, TREE_TYPE (parm)); | |
5d80a306 DG |
2901 | |
2902 | TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)) | |
2903 | = is_parameter_pack; | |
8d08fdba MS |
2904 | } |
2905 | else | |
2906 | { | |
73b0fce8 | 2907 | tree t; |
058b15c1 | 2908 | parm = TREE_VALUE (TREE_VALUE (parm)); |
c8094d83 | 2909 | |
73b0fce8 KL |
2910 | if (parm && TREE_CODE (parm) == TEMPLATE_DECL) |
2911 | { | |
33848bb0 | 2912 | t = make_aggr_type (TEMPLATE_TEMPLATE_PARM); |
c8094d83 | 2913 | /* This is for distinguishing between real templates and template |
73b0fce8 KL |
2914 | template parameters */ |
2915 | TREE_TYPE (parm) = t; | |
2916 | TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t; | |
2917 | decl = parm; | |
2918 | } | |
2919 | else | |
2920 | { | |
33848bb0 | 2921 | t = make_aggr_type (TEMPLATE_TYPE_PARM); |
f4f206f4 | 2922 | /* parm is either IDENTIFIER_NODE or NULL_TREE. */ |
73b0fce8 KL |
2923 | decl = build_decl (TYPE_DECL, parm, t); |
2924 | } | |
c8094d83 | 2925 | |
d2e5ee5c MS |
2926 | TYPE_NAME (t) = decl; |
2927 | TYPE_STUB_DECL (t) = decl; | |
a292b002 | 2928 | parm = decl; |
f84b4be9 | 2929 | TEMPLATE_TYPE_PARM_INDEX (t) |
c8094d83 | 2930 | = build_template_parm_index (idx, processing_template_decl, |
f84b4be9 JM |
2931 | processing_template_decl, |
2932 | decl, TREE_TYPE (parm)); | |
5d80a306 | 2933 | TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack; |
06d40de8 | 2934 | TYPE_CANONICAL (t) = canonical_type_parameter (t); |
8d08fdba | 2935 | } |
c727aa5e | 2936 | DECL_ARTIFICIAL (decl) = 1; |
cd9f6678 | 2937 | SET_DECL_TEMPLATE_PARM_P (decl); |
8d08fdba | 2938 | pushdecl (decl); |
a292b002 | 2939 | parm = build_tree_list (defval, parm); |
8d08fdba MS |
2940 | return chainon (list, parm); |
2941 | } | |
2942 | ||
2943 | /* The end of a template parameter list has been reached. Process the | |
2944 | tree list into a parameter vector, converting each parameter into a more | |
2945 | useful form. Type parameters are saved as IDENTIFIER_NODEs, and others | |
2946 | as PARM_DECLs. */ | |
2947 | ||
2948 | tree | |
3a978d72 | 2949 | end_template_parm_list (tree parms) |
8d08fdba | 2950 | { |
5566b478 | 2951 | int nparms; |
9471d3e2 | 2952 | tree parm, next; |
5566b478 MS |
2953 | tree saved_parmlist = make_tree_vec (list_length (parms)); |
2954 | ||
5566b478 | 2955 | current_template_parms |
4890c2f4 | 2956 | = tree_cons (size_int (processing_template_decl), |
5566b478 | 2957 | saved_parmlist, current_template_parms); |
8d08fdba | 2958 | |
9471d3e2 NS |
2959 | for (parm = parms, nparms = 0; parm; parm = next, nparms++) |
2960 | { | |
2961 | next = TREE_CHAIN (parm); | |
2962 | TREE_VEC_ELT (saved_parmlist, nparms) = parm; | |
2963 | TREE_CHAIN (parm) = NULL_TREE; | |
2964 | } | |
a292b002 | 2965 | |
67ffc812 MM |
2966 | --processing_template_parmlist; |
2967 | ||
8d08fdba MS |
2968 | return saved_parmlist; |
2969 | } | |
2970 | ||
5566b478 MS |
2971 | /* end_template_decl is called after a template declaration is seen. */ |
2972 | ||
8d08fdba | 2973 | void |
3a978d72 | 2974 | end_template_decl (void) |
8d08fdba | 2975 | { |
386b8a85 JM |
2976 | reset_specialization (); |
2977 | ||
5156628f | 2978 | if (! processing_template_decl) |
73aad9b9 JM |
2979 | return; |
2980 | ||
5566b478 | 2981 | /* This matches the pushlevel in begin_template_parm_list. */ |
74b846e0 | 2982 | finish_scope (); |
8d08fdba | 2983 | |
5566b478 MS |
2984 | --processing_template_decl; |
2985 | current_template_parms = TREE_CHAIN (current_template_parms); | |
5566b478 | 2986 | } |
8d08fdba | 2987 | |
36a117a5 MM |
2988 | /* Given a template argument vector containing the template PARMS. |
2989 | The innermost PARMS are given first. */ | |
9a3b49ac | 2990 | |
b5791fdc | 2991 | static tree |
3a978d72 | 2992 | current_template_args (void) |
5566b478 | 2993 | { |
36a117a5 | 2994 | tree header; |
b370501f | 2995 | tree args = NULL_TREE; |
36a117a5 | 2996 | int length = TMPL_PARMS_DEPTH (current_template_parms); |
98c1c668 JM |
2997 | int l = length; |
2998 | ||
36a117a5 MM |
2999 | /* If there is only one level of template parameters, we do not |
3000 | create a TREE_VEC of TREE_VECs. Instead, we return a single | |
3001 | TREE_VEC containing the arguments. */ | |
3002 | if (length > 1) | |
3003 | args = make_tree_vec (length); | |
3004 | ||
3005 | for (header = current_template_parms; header; header = TREE_CHAIN (header)) | |
8d08fdba | 3006 | { |
5566b478 | 3007 | tree a = copy_node (TREE_VALUE (header)); |
36a117a5 MM |
3008 | int i; |
3009 | ||
5566b478 | 3010 | TREE_TYPE (a) = NULL_TREE; |
36a117a5 | 3011 | for (i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i) |
5566b478 | 3012 | { |
98c1c668 JM |
3013 | tree t = TREE_VEC_ELT (a, i); |
3014 | ||
36a117a5 | 3015 | /* T will be a list if we are called from within a |
98c1c668 JM |
3016 | begin/end_template_parm_list pair, but a vector directly |
3017 | if within a begin/end_member_template_processing pair. */ | |
c8094d83 | 3018 | if (TREE_CODE (t) == TREE_LIST) |
98c1c668 JM |
3019 | { |
3020 | t = TREE_VALUE (t); | |
c8094d83 | 3021 | |
0f67a82f LM |
3022 | if (t != error_mark_node) |
3023 | { | |
3024 | if (TREE_CODE (t) == TYPE_DECL | |
3025 | || TREE_CODE (t) == TEMPLATE_DECL) | |
5d80a306 DG |
3026 | { |
3027 | t = TREE_TYPE (t); | |
3028 | ||
3029 | if (TEMPLATE_TYPE_PARAMETER_PACK (t)) | |
3030 | { | |
3031 | /* Turn this argument into a TYPE_ARGUMENT_PACK | |
3032 | with a single element, which expands T. */ | |
3033 | tree vec = make_tree_vec (1); | |
3034 | TREE_VEC_ELT (vec, 0) = make_pack_expansion (t); | |
3035 | ||
3036 | t = make_node (TYPE_ARGUMENT_PACK); | |
3037 | SET_ARGUMENT_PACK_ARGS (t, vec); | |
3038 | } | |
3039 | } | |
3040 | else | |
3041 | { | |
3042 | t = DECL_INITIAL (t); | |
3043 | ||
3044 | if (TEMPLATE_PARM_PARAMETER_PACK (t)) | |
3045 | { | |
3046 | /* Turn this argument into a NONTYPE_ARGUMENT_PACK | |
3047 | with a single element, which expands T. */ | |
3048 | tree vec = make_tree_vec (1); | |
3049 | tree type = TREE_TYPE (TEMPLATE_PARM_DECL (t)); | |
3050 | TREE_VEC_ELT (vec, 0) = make_pack_expansion (t); | |
3051 | ||
3052 | t = make_node (NONTYPE_ARGUMENT_PACK); | |
3053 | SET_ARGUMENT_PACK_ARGS (t, vec); | |
3054 | TREE_TYPE (t) = type; | |
3055 | } | |
3056 | } | |
3057 | } | |
36a117a5 | 3058 | TREE_VEC_ELT (a, i) = t; |
98c1c668 | 3059 | } |
5566b478 | 3060 | } |
36a117a5 MM |
3061 | |
3062 | if (length > 1) | |
3063 | TREE_VEC_ELT (args, --l) = a; | |
3064 | else | |
3065 | args = a; | |
8d08fdba MS |
3066 | } |
3067 | ||
9a3b49ac MS |
3068 | return args; |
3069 | } | |
75650646 | 3070 | |
e1467ff2 | 3071 | /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated |
c7222c02 | 3072 | template PARMS. If MEMBER_TEMPLATE_P is true, the new template is |
ee81147e | 3073 | a member template. Used by push_template_decl below. */ |
e1467ff2 | 3074 | |
75650646 | 3075 | static tree |
c7222c02 | 3076 | build_template_decl (tree decl, tree parms, bool member_template_p) |
75650646 MM |
3077 | { |
3078 | tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE); | |
3079 | DECL_TEMPLATE_PARMS (tmpl) = parms; | |
3080 | DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl); | |
c7222c02 | 3081 | DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p; |
75650646 MM |
3082 | if (DECL_LANG_SPECIFIC (decl)) |
3083 | { | |
8f032717 MM |
3084 | DECL_STATIC_FUNCTION_P (tmpl) = DECL_STATIC_FUNCTION_P (decl); |
3085 | DECL_CONSTRUCTOR_P (tmpl) = DECL_CONSTRUCTOR_P (decl); | |
c8460010 | 3086 | DECL_DESTRUCTOR_P (tmpl) = DECL_DESTRUCTOR_P (decl); |
11f98788 | 3087 | DECL_NONCONVERTING_P (tmpl) = DECL_NONCONVERTING_P (decl); |
1f6e1acc AS |
3088 | DECL_ASSIGNMENT_OPERATOR_P (tmpl) = DECL_ASSIGNMENT_OPERATOR_P (decl); |
3089 | if (DECL_OVERLOADED_OPERATOR_P (decl)) | |
c8094d83 | 3090 | SET_OVERLOADED_OPERATOR_CODE (tmpl, |
1f6e1acc | 3091 | DECL_OVERLOADED_OPERATOR_P (decl)); |
75650646 MM |
3092 | } |
3093 | ||
3094 | return tmpl; | |
3095 | } | |
3096 | ||
050367a3 MM |
3097 | struct template_parm_data |
3098 | { | |
6c30752f MM |
3099 | /* The level of the template parameters we are currently |
3100 | processing. */ | |
050367a3 | 3101 | int level; |
6c30752f MM |
3102 | |
3103 | /* The index of the specialization argument we are currently | |
3104 | processing. */ | |
3105 | int current_arg; | |
3106 | ||
3107 | /* An array whose size is the number of template parameters. The | |
838dfd8a | 3108 | elements are nonzero if the parameter has been used in any one |
6c30752f | 3109 | of the arguments processed so far. */ |
050367a3 | 3110 | int* parms; |
6c30752f MM |
3111 | |
3112 | /* An array whose size is the number of template arguments. The | |
838dfd8a | 3113 | elements are nonzero if the argument makes use of template |
6c30752f MM |
3114 | parameters of this level. */ |
3115 | int* arg_uses_template_parms; | |
050367a3 MM |
3116 | }; |
3117 | ||
3118 | /* Subroutine of push_template_decl used to see if each template | |
3119 | parameter in a partial specialization is used in the explicit | |
3120 | argument list. If T is of the LEVEL given in DATA (which is | |
3121 | treated as a template_parm_data*), then DATA->PARMS is marked | |
3122 | appropriately. */ | |
3123 | ||
3124 | static int | |
3a978d72 | 3125 | mark_template_parm (tree t, void* data) |
050367a3 MM |
3126 | { |
3127 | int level; | |
3128 | int idx; | |
3129 | struct template_parm_data* tpd = (struct template_parm_data*) data; | |
3130 | ||
3131 | if (TREE_CODE (t) == TEMPLATE_PARM_INDEX) | |
3132 | { | |
3133 | level = TEMPLATE_PARM_LEVEL (t); | |
3134 | idx = TEMPLATE_PARM_IDX (t); | |
3135 | } | |
3136 | else | |
3137 | { | |
3138 | level = TEMPLATE_TYPE_LEVEL (t); | |
3139 | idx = TEMPLATE_TYPE_IDX (t); | |
3140 | } | |
3141 | ||
3142 | if (level == tpd->level) | |
6c30752f MM |
3143 | { |
3144 | tpd->parms[idx] = 1; | |
3145 | tpd->arg_uses_template_parms[tpd->current_arg] = 1; | |
3146 | } | |
050367a3 MM |
3147 | |
3148 | /* Return zero so that for_each_template_parm will continue the | |
3149 | traversal of the tree; we want to mark *every* template parm. */ | |
3150 | return 0; | |
3151 | } | |
3152 | ||
6c30752f MM |
3153 | /* Process the partial specialization DECL. */ |
3154 | ||
e9659ab0 | 3155 | static tree |
3a978d72 | 3156 | process_partial_specialization (tree decl) |
6c30752f MM |
3157 | { |
3158 | tree type = TREE_TYPE (decl); | |
3159 | tree maintmpl = CLASSTYPE_TI_TEMPLATE (type); | |
3160 | tree specargs = CLASSTYPE_TI_ARGS (type); | |
f9a7ae04 | 3161 | tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs); |
6c30752f MM |
3162 | tree inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms); |
3163 | tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl); | |
3164 | int nargs = TREE_VEC_LENGTH (inner_args); | |
3165 | int ntparms = TREE_VEC_LENGTH (inner_parms); | |
3166 | int i; | |
3167 | int did_error_intro = 0; | |
6c30752f MM |
3168 | struct template_parm_data tpd; |
3169 | struct template_parm_data tpd2; | |
3170 | ||
6c30752f MM |
3171 | /* We check that each of the template parameters given in the |
3172 | partial specialization is used in the argument list to the | |
3173 | specialization. For example: | |
3174 | ||
3175 | template <class T> struct S; | |
3176 | template <class T> struct S<T*>; | |
3177 | ||
3178 | The second declaration is OK because `T*' uses the template | |
3179 | parameter T, whereas | |
3180 | ||
3181 | template <class T> struct S<int>; | |
3182 | ||
3183 | is no good. Even trickier is: | |
3184 | ||
3185 | template <class T> | |
3186 | struct S1 | |
3187 | { | |
3188 | template <class U> | |
3189 | struct S2; | |
3190 | template <class U> | |
3191 | struct S2<T>; | |
3192 | }; | |
3193 | ||
0e339752 | 3194 | The S2<T> declaration is actually invalid; it is a |
c8094d83 | 3195 | full-specialization. Of course, |
6c30752f MM |
3196 | |
3197 | template <class U> | |
3198 | struct S2<T (*)(U)>; | |
3199 | ||
3200 | or some such would have been OK. */ | |
3201 | tpd.level = TMPL_PARMS_DEPTH (current_template_parms); | |
67f5655f | 3202 | tpd.parms = (int *) alloca (sizeof (int) * ntparms); |
fad205ff | 3203 | memset (tpd.parms, 0, sizeof (int) * ntparms); |
6c30752f | 3204 | |
67f5655f | 3205 | tpd.arg_uses_template_parms = (int *) alloca (sizeof (int) * nargs); |
fad205ff | 3206 | memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs); |
6c30752f MM |
3207 | for (i = 0; i < nargs; ++i) |
3208 | { | |
3209 | tpd.current_arg = i; | |
3210 | for_each_template_parm (TREE_VEC_ELT (inner_args, i), | |
3211 | &mark_template_parm, | |
4f2c9d7e MM |
3212 | &tpd, |
3213 | NULL); | |
6c30752f MM |
3214 | } |
3215 | for (i = 0; i < ntparms; ++i) | |
3216 | if (tpd.parms[i] == 0) | |
3217 | { | |
3218 | /* One of the template parms was not used in the | |
f9a7ae04 | 3219 | specialization. */ |
6c30752f MM |
3220 | if (!did_error_intro) |
3221 | { | |
33bd39a2 | 3222 | error ("template parameters not used in partial specialization:"); |
6c30752f MM |
3223 | did_error_intro = 1; |
3224 | } | |
3225 | ||
0f51ccfc | 3226 | error (" %qD", TREE_VALUE (TREE_VEC_ELT (inner_parms, i))); |
6c30752f MM |
3227 | } |
3228 | ||
3229 | /* [temp.class.spec] | |
3230 | ||
3231 | The argument list of the specialization shall not be identical to | |
3232 | the implicit argument list of the primary template. */ | |
c8094d83 MS |
3233 | if (comp_template_args |
3234 | (inner_args, | |
f9a7ae04 MM |
3235 | INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE |
3236 | (maintmpl))))) | |
0f51ccfc | 3237 | error ("partial specialization %qT does not specialize any template arguments", type); |
6c30752f MM |
3238 | |
3239 | /* [temp.class.spec] | |
3240 | ||
3241 | A partially specialized non-type argument expression shall not | |
3242 | involve template parameters of the partial specialization except | |
3243 | when the argument expression is a simple identifier. | |
3244 | ||
3245 | The type of a template parameter corresponding to a specialized | |
3246 | non-type argument shall not be dependent on a parameter of the | |
5d80a306 DG |
3247 | specialization. |
3248 | ||
3249 | Also, we verify that pack expansions only occur at the | |
3250 | end of the argument list. */ | |
50bc768d | 3251 | gcc_assert (nargs == DECL_NTPARMS (maintmpl)); |
6c30752f MM |
3252 | tpd2.parms = 0; |
3253 | for (i = 0; i < nargs; ++i) | |
3254 | { | |
5d80a306 | 3255 | tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i)); |
6c30752f | 3256 | tree arg = TREE_VEC_ELT (inner_args, i); |
5d80a306 DG |
3257 | tree packed_args = NULL_TREE; |
3258 | int j, len = 1; | |
6c30752f | 3259 | |
5d80a306 DG |
3260 | if (ARGUMENT_PACK_P (arg)) |
3261 | { | |
3262 | /* Extract the arguments from the argument pack. We'll be | |
3263 | iterating over these in the following loop. */ | |
3264 | packed_args = ARGUMENT_PACK_ARGS (arg); | |
3265 | len = TREE_VEC_LENGTH (packed_args); | |
3266 | } | |
3267 | ||
3268 | for (j = 0; j < len; j++) | |
3269 | { | |
3270 | if (packed_args) | |
3271 | /* Get the Jth argument in the parameter pack. */ | |
3272 | arg = TREE_VEC_ELT (packed_args, j); | |
3273 | ||
3274 | if (PACK_EXPANSION_P (arg)) | |
3275 | { | |
3276 | /* Pack expansions must come at the end of the | |
3277 | argument list. */ | |
3278 | if ((packed_args && j < len - 1) | |
3279 | || (!packed_args && i < nargs - 1)) | |
3280 | { | |
3281 | if (TREE_CODE (arg) == EXPR_PACK_EXPANSION) | |
3282 | error ("parameter pack argument %qE must be at the end of the template argument list", arg); | |
3283 | else | |
3284 | error ("parameter pack argument %qT must be at the end of the template argument list", arg); | |
3285 | } | |
3286 | } | |
3287 | ||
3288 | if (TREE_CODE (arg) == EXPR_PACK_EXPANSION) | |
3289 | /* We only care about the pattern. */ | |
3290 | arg = PACK_EXPANSION_PATTERN (arg); | |
3291 | ||
3292 | if (/* These first two lines are the `non-type' bit. */ | |
3293 | !TYPE_P (arg) | |
3294 | && TREE_CODE (arg) != TEMPLATE_DECL | |
3295 | /* This next line is the `argument expression is not just a | |
3296 | simple identifier' condition and also the `specialized | |
3297 | non-type argument' bit. */ | |
3298 | && TREE_CODE (arg) != TEMPLATE_PARM_INDEX) | |
3299 | { | |
3300 | if ((!packed_args && tpd.arg_uses_template_parms[i]) | |
3301 | || (packed_args && uses_template_parms (arg))) | |
3302 | error ("template argument %qE involves template parameter(s)", | |
3303 | arg); | |
3304 | else | |
3305 | { | |
3306 | /* Look at the corresponding template parameter, | |
3307 | marking which template parameters its type depends | |
3308 | upon. */ | |
3309 | tree type = TREE_TYPE (parm); | |
3310 | ||
3311 | if (!tpd2.parms) | |
3312 | { | |
3313 | /* We haven't yet initialized TPD2. Do so now. */ | |
3314 | tpd2.arg_uses_template_parms | |
3315 | = (int *) alloca (sizeof (int) * nargs); | |
3316 | /* The number of parameters here is the number in the | |
3317 | main template, which, as checked in the assertion | |
3318 | above, is NARGS. */ | |
3319 | tpd2.parms = (int *) alloca (sizeof (int) * nargs); | |
3320 | tpd2.level = | |
3321 | TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl)); | |
3322 | } | |
3323 | ||
3324 | /* Mark the template parameters. But this time, we're | |
3325 | looking for the template parameters of the main | |
3326 | template, not in the specialization. */ | |
3327 | tpd2.current_arg = i; | |
3328 | tpd2.arg_uses_template_parms[i] = 0; | |
3329 | memset (tpd2.parms, 0, sizeof (int) * nargs); | |
3330 | for_each_template_parm (type, | |
3331 | &mark_template_parm, | |
3332 | &tpd2, | |
3333 | NULL); | |
3334 | ||
3335 | if (tpd2.arg_uses_template_parms [i]) | |
3336 | { | |
3337 | /* The type depended on some template parameters. | |
3338 | If they are fully specialized in the | |
3339 | specialization, that's OK. */ | |
3340 | int j; | |
3341 | for (j = 0; j < nargs; ++j) | |
3342 | if (tpd2.parms[j] != 0 | |
3343 | && tpd.arg_uses_template_parms [j]) | |
3344 | { | |
3345 | error ("type %qT of template argument %qE depends " | |
3346 | "on template parameter(s)", | |
3347 | type, | |
3348 | arg); | |
3349 | break; | |
3350 | } | |
3351 | } | |
3352 | } | |
3353 | } | |
3354 | } | |
6c30752f MM |
3355 | } |
3356 | ||
c8094d83 | 3357 | if (retrieve_specialization (maintmpl, specargs, |
c7222c02 | 3358 | /*class_specializations_p=*/true)) |
6c30752f MM |
3359 | /* We've already got this specialization. */ |
3360 | return decl; | |
3361 | ||
d8b64f80 | 3362 | DECL_TEMPLATE_SPECIALIZATIONS (maintmpl) |
916b63c3 | 3363 | = tree_cons (specargs, inner_parms, |
e1b3e07d | 3364 | DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)); |
6c30752f MM |
3365 | TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type; |
3366 | return decl; | |
3367 | } | |
3368 | ||
6ba89f8e MM |
3369 | /* Check that a template declaration's use of default arguments is not |
3370 | invalid. Here, PARMS are the template parameters. IS_PRIMARY is | |
838dfd8a KH |
3371 | nonzero if DECL is the thing declared by a primary template. |
3372 | IS_PARTIAL is nonzero if DECL is a partial specialization. */ | |
6ba89f8e MM |
3373 | |
3374 | static void | |
3a978d72 | 3375 | check_default_tmpl_args (tree decl, tree parms, int is_primary, int is_partial) |
6ba89f8e | 3376 | { |
d8e178a0 | 3377 | const char *msg; |
66191c20 MM |
3378 | int last_level_to_check; |
3379 | tree parm_level; | |
6ba89f8e | 3380 | |
c8094d83 | 3381 | /* [temp.param] |
6ba89f8e MM |
3382 | |
3383 | A default template-argument shall not be specified in a | |
3384 | function template declaration or a function template definition, nor | |
3385 | in the template-parameter-list of the definition of a member of a | |
3386 | class template. */ | |
3387 | ||
4f1c5b7d | 3388 | if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL) |
e0942dcd MM |
3389 | /* You can't have a function template declaration in a local |
3390 | scope, nor you can you define a member of a class template in a | |
3391 | local scope. */ | |
3392 | return; | |
3393 | ||
6ba89f8e MM |
3394 | if (current_class_type |
3395 | && !TYPE_BEING_DEFINED (current_class_type) | |
3d7de1fa | 3396 | && DECL_LANG_SPECIFIC (decl) |
5937a6f9 MM |
3397 | /* If this is either a friend defined in the scope of the class |
3398 | or a member function. */ | |
6df5158a NS |
3399 | && (DECL_FUNCTION_MEMBER_P (decl) |
3400 | ? same_type_p (DECL_CONTEXT (decl), current_class_type) | |
3401 | : DECL_FRIEND_CONTEXT (decl) | |
3402 | ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type) | |
3403 | : false) | |
5937a6f9 MM |
3404 | /* And, if it was a member function, it really was defined in |
3405 | the scope of the class. */ | |
6df5158a NS |
3406 | && (!DECL_FUNCTION_MEMBER_P (decl) |
3407 | || DECL_INITIALIZED_IN_CLASS_P (decl))) | |
6ba89f8e | 3408 | /* We already checked these parameters when the template was |
5937a6f9 MM |
3409 | declared, so there's no need to do it again now. This function |
3410 | was defined in class scope, but we're processing it's body now | |
3411 | that the class is complete. */ | |
6ba89f8e MM |
3412 | return; |
3413 | ||
66191c20 | 3414 | /* [temp.param] |
c8094d83 | 3415 | |
66191c20 MM |
3416 | If a template-parameter has a default template-argument, all |
3417 | subsequent template-parameters shall have a default | |
3418 | template-argument supplied. */ | |
3419 | for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level)) | |
3420 | { | |
3421 | tree inner_parms = TREE_VALUE (parm_level); | |
3422 | int ntparms = TREE_VEC_LENGTH (inner_parms); | |
c8094d83 | 3423 | int seen_def_arg_p = 0; |
66191c20 MM |
3424 | int i; |
3425 | ||
c8094d83 | 3426 | for (i = 0; i < ntparms; ++i) |
66191c20 MM |
3427 | { |
3428 | tree parm = TREE_VEC_ELT (inner_parms, i); | |
42b304f1 LM |
3429 | |
3430 | if (parm == error_mark_node) | |
3431 | continue; | |
3432 | ||
66191c20 MM |
3433 | if (TREE_PURPOSE (parm)) |
3434 | seen_def_arg_p = 1; | |
3435 | else if (seen_def_arg_p) | |
3436 | { | |
0f51ccfc | 3437 | error ("no default argument for %qD", TREE_VALUE (parm)); |
66191c20 MM |
3438 | /* For better subsequent error-recovery, we indicate that |
3439 | there should have been a default argument. */ | |
3440 | TREE_PURPOSE (parm) = error_mark_node; | |
3441 | } | |
3442 | } | |
3443 | } | |
3444 | ||
6ba89f8e MM |
3445 | if (TREE_CODE (decl) != TYPE_DECL || is_partial || !is_primary) |
3446 | /* For an ordinary class template, default template arguments are | |
3447 | allowed at the innermost level, e.g.: | |
0cbd7506 | 3448 | template <class T = int> |
6ba89f8e MM |
3449 | struct S {}; |
3450 | but, in a partial specialization, they're not allowed even | |
3451 | there, as we have in [temp.class.spec]: | |
c8094d83 | 3452 | |
6ba89f8e | 3453 | The template parameter list of a specialization shall not |
c8094d83 | 3454 | contain default template argument values. |
6ba89f8e MM |
3455 | |
3456 | So, for a partial specialization, or for a function template, | |
3457 | we look at all of them. */ | |
3458 | ; | |
3459 | else | |
3460 | /* But, for a primary class template that is not a partial | |
3461 | specialization we look at all template parameters except the | |
3462 | innermost ones. */ | |
3463 | parms = TREE_CHAIN (parms); | |
3464 | ||
3465 | /* Figure out what error message to issue. */ | |
3466 | if (TREE_CODE (decl) == FUNCTION_DECL) | |
8653a2c3 | 3467 | msg = "default template arguments may not be used in function templates"; |
6ba89f8e | 3468 | else if (is_partial) |
8653a2c3 | 3469 | msg = "default template arguments may not be used in partial specializations"; |
6ba89f8e | 3470 | else |
0f51ccfc | 3471 | msg = "default argument for template parameter for class enclosing %qD"; |
6ba89f8e MM |
3472 | |
3473 | if (current_class_type && TYPE_BEING_DEFINED (current_class_type)) | |
3474 | /* If we're inside a class definition, there's no need to | |
104bf76a | 3475 | examine the parameters to the class itself. On the one |
6ba89f8e | 3476 | hand, they will be checked when the class is defined, and, |
0e339752 | 3477 | on the other, default arguments are valid in things like: |
0cbd7506 MS |
3478 | template <class T = double> |
3479 | struct S { template <class U> void f(U); }; | |
6ba89f8e MM |
3480 | Here the default argument for `S' has no bearing on the |
3481 | declaration of `f'. */ | |
3482 | last_level_to_check = template_class_depth (current_class_type) + 1; | |
3483 | else | |
3484 | /* Check everything. */ | |
3485 | last_level_to_check = 0; | |
3486 | ||
c8094d83 MS |
3487 | for (parm_level = parms; |
3488 | parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check; | |
66191c20 | 3489 | parm_level = TREE_CHAIN (parm_level)) |
6ba89f8e | 3490 | { |
66191c20 MM |
3491 | tree inner_parms = TREE_VALUE (parm_level); |
3492 | int i; | |
3493 | int ntparms; | |
6ba89f8e MM |
3494 | |
3495 | ntparms = TREE_VEC_LENGTH (inner_parms); | |
c8094d83 | 3496 | for (i = 0; i < ntparms; ++i) |
42b304f1 LM |
3497 | { |
3498 | if (TREE_VEC_ELT (inner_parms, i) == error_mark_node) | |
3499 | continue; | |
6ba89f8e | 3500 | |
42b304f1 LM |
3501 | if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i))) |
3502 | { | |
3503 | if (msg) | |
3504 | { | |
3505 | error (msg, decl); | |
3506 | msg = 0; | |
3507 | } | |
3508 | ||
3509 | /* Clear out the default argument so that we are not | |
3510 | confused later. */ | |
3511 | TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE; | |
3512 | } | |
3513 | } | |
6ba89f8e MM |
3514 | |
3515 | /* At this point, if we're still interested in issuing messages, | |
3516 | they must apply to classes surrounding the object declared. */ | |
3517 | if (msg) | |
c8094d83 | 3518 | msg = "default argument for template parameter for class enclosing %qD"; |
6ba89f8e MM |
3519 | } |
3520 | } | |
3521 | ||
5dd236e2 NS |
3522 | /* Worker for push_template_decl_real, called via |
3523 | for_each_template_parm. DATA is really an int, indicating the | |
3524 | level of the parameters we are interested in. If T is a template | |
838dfd8a | 3525 | parameter of that level, return nonzero. */ |
5dd236e2 NS |
3526 | |
3527 | static int | |
3a978d72 | 3528 | template_parm_this_level_p (tree t, void* data) |
5dd236e2 | 3529 | { |
6e04241f | 3530 | int this_level = *(int *)data; |
5dd236e2 NS |
3531 | int level; |
3532 | ||
3533 | if (TREE_CODE (t) == TEMPLATE_PARM_INDEX) | |
3534 | level = TEMPLATE_PARM_LEVEL (t); | |
3535 | else | |
3536 | level = TEMPLATE_TYPE_LEVEL (t); | |
3537 | return level == this_level; | |
3538 | } | |
3539 | ||
3ac3d9ea | 3540 | /* Creates a TEMPLATE_DECL for the indicated DECL using the template |
f84b4be9 JM |
3541 | parameters given by current_template_args, or reuses a |
3542 | previously existing one, if appropriate. Returns the DECL, or an | |
c8094d83 | 3543 | equivalent one, if it is replaced via a call to duplicate_decls. |
6757edfe | 3544 | |
d63d5d0c | 3545 | If IS_FRIEND is true, DECL is a friend declaration. */ |
3ac3d9ea MM |
3546 | |
3547 | tree | |
d63d5d0c | 3548 | push_template_decl_real (tree decl, bool is_friend) |
9a3b49ac MS |
3549 | { |
3550 | tree tmpl; | |
f84b4be9 | 3551 | tree args; |
9a3b49ac | 3552 | tree info; |
f84b4be9 JM |
3553 | tree ctx; |
3554 | int primary; | |
6ba89f8e | 3555 | int is_partial; |
cfe507be | 3556 | int new_template_p = 0; |
c7222c02 MM |
3557 | /* True if the template is a member template, in the sense of |
3558 | [temp.mem]. */ | |
3559 | bool member_template_p = false; | |
6ba89f8e | 3560 | |
c0694c4b MM |
3561 | if (decl == error_mark_node) |
3562 | return decl; | |
3563 | ||
6ba89f8e | 3564 | /* See if this is a partial specialization. */ |
9188c363 | 3565 | is_partial = (DECL_IMPLICIT_TYPEDEF_P (decl) |
6ba89f8e | 3566 | && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE |
370af2d5 | 3567 | && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl))); |
6757edfe | 3568 | |
d63d5d0c ILT |
3569 | if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl)) |
3570 | is_friend = true; | |
f84b4be9 JM |
3571 | |
3572 | if (is_friend) | |
3573 | /* For a friend, we want the context of the friend function, not | |
3574 | the type of which it is a friend. */ | |
3575 | ctx = DECL_CONTEXT (decl); | |
4f1c5b7d MM |
3576 | else if (CP_DECL_CONTEXT (decl) |
3577 | && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL) | |
f84b4be9 JM |
3578 | /* In the case of a virtual function, we want the class in which |
3579 | it is defined. */ | |
4f1c5b7d | 3580 | ctx = CP_DECL_CONTEXT (decl); |
f84b4be9 | 3581 | else |
dc957d14 | 3582 | /* Otherwise, if we're currently defining some class, the DECL |
f84b4be9 | 3583 | is assumed to be a member of the class. */ |
9188c363 | 3584 | ctx = current_scope (); |
f84b4be9 | 3585 | |
2c73f9f5 ML |
3586 | if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL) |
3587 | ctx = NULL_TREE; | |
3588 | ||
3589 | if (!DECL_CONTEXT (decl)) | |
cb0dbb9a | 3590 | DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace); |
2c73f9f5 | 3591 | |
6ba89f8e | 3592 | /* See if this is a primary template. */ |
c9cbfca6 JM |
3593 | if (is_friend && ctx) |
3594 | /* A friend template that specifies a class context, i.e. | |
3595 | template <typename T> friend void A<T>::f(); | |
3596 | is not primary. */ | |
3597 | primary = 0; | |
3598 | else | |
3599 | primary = template_parm_scope_p (); | |
9a3b49ac | 3600 | |
83566abf JM |
3601 | if (primary) |
3602 | { | |
c7222c02 MM |
3603 | if (DECL_CLASS_SCOPE_P (decl)) |
3604 | member_template_p = true; | |
2f1b1731 MM |
3605 | if (TREE_CODE (decl) == TYPE_DECL |
3606 | && ANON_AGGRNAME_P (DECL_NAME (decl))) | |
33bd39a2 | 3607 | error ("template class without a name"); |
717a7d5d | 3608 | else if (TREE_CODE (decl) == FUNCTION_DECL) |
4b0d3cbe | 3609 | { |
717a7d5d MM |
3610 | if (DECL_DESTRUCTOR_P (decl)) |
3611 | { | |
3612 | /* [temp.mem] | |
c8094d83 | 3613 | |
0cbd7506 | 3614 | A destructor shall not be a member template. */ |
0f51ccfc | 3615 | error ("destructor %qD declared as member template", decl); |
717a7d5d MM |
3616 | return error_mark_node; |
3617 | } | |
3618 | if (NEW_DELETE_OPNAME_P (DECL_NAME (decl)) | |
3619 | && (!TYPE_ARG_TYPES (TREE_TYPE (decl)) | |
3620 | || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node | |
3621 | || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl))) | |
3622 | || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl)))) | |
3623 | == void_list_node))) | |
3624 | { | |
c8094d83 | 3625 | /* [basic.stc.dynamic.allocation] |
717a7d5d | 3626 | |
0cbd7506 | 3627 | An allocation function can be a function |
717a7d5d MM |
3628 | template. ... Template allocation functions shall |
3629 | have two or more parameters. */ | |
0f51ccfc | 3630 | error ("invalid template declaration of %qD", decl); |
7c60008e | 3631 | return error_mark_node; |
717a7d5d | 3632 | } |
4b0d3cbe | 3633 | } |
8ca4bf25 MM |
3634 | else if (DECL_IMPLICIT_TYPEDEF_P (decl) |
3635 | && CLASS_TYPE_P (TREE_TYPE (decl))) | |
2aaf816d JM |
3636 | /* OK */; |
3637 | else | |
11325dcd | 3638 | { |
0f51ccfc | 3639 | error ("template declaration of %q#D", decl); |
11325dcd KL |
3640 | return error_mark_node; |
3641 | } | |
83566abf JM |
3642 | } |
3643 | ||
6ba89f8e MM |
3644 | /* Check to see that the rules regarding the use of default |
3645 | arguments are not being violated. */ | |
c8094d83 | 3646 | check_default_tmpl_args (decl, current_template_parms, |
6ba89f8e | 3647 | primary, is_partial); |
73aad9b9 | 3648 | |
5d80a306 DG |
3649 | /* Ensure that there are no parameter packs in the type of this |
3650 | declaration that have not been expanded. */ | |
3651 | check_for_bare_parameter_packs (TREE_TYPE (decl)); | |
3652 | ||
6ba89f8e MM |
3653 | if (is_partial) |
3654 | return process_partial_specialization (decl); | |
d32789d8 | 3655 | |
5d80a306 DG |
3656 | /* A primary class template can only have one parameter pack, at the |
3657 | end of the template parameter list. */ | |
3658 | if (primary && TREE_CODE (decl) == TYPE_DECL) | |
3659 | { | |
3660 | tree inner_parms | |
3661 | = INNERMOST_TEMPLATE_PARMS (current_template_parms); | |
3662 | int i, len = TREE_VEC_LENGTH (inner_parms); | |
3663 | for (i = 0; i < len - 1; i++) | |
3664 | { | |
3665 | tree parm = TREE_VALUE (TREE_VEC_ELT (inner_parms, i)); | |
3666 | ||
3667 | if (template_parameter_pack_p (parm)) | |
3668 | { | |
3669 | if (TREE_CODE (parm) == PARM_DECL) | |
3670 | error ("parameter pack %qE must be at the end of the" | |
3671 | " template parameter list", parm); | |
3672 | else | |
3673 | error ("parameter pack %qT must be at the end of the" | |
3674 | " template parameter list", TREE_TYPE (parm)); | |
3675 | } | |
3676 | } | |
3677 | } | |
3678 | ||
9a3b49ac MS |
3679 | args = current_template_args (); |
3680 | ||
c8094d83 | 3681 | if (!ctx |
f84b4be9 | 3682 | || TREE_CODE (ctx) == FUNCTION_DECL |
a723baf1 | 3683 | || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx)) |
f84b4be9 | 3684 | || (is_friend && !DECL_TEMPLATE_INFO (decl))) |
8d08fdba | 3685 | { |
75650646 | 3686 | if (DECL_LANG_SPECIFIC (decl) |
f84b4be9 JM |
3687 | && DECL_TEMPLATE_INFO (decl) |
3688 | && DECL_TI_TEMPLATE (decl)) | |
3689 | tmpl = DECL_TI_TEMPLATE (decl); | |
1c10870d AS |
3690 | /* If DECL is a TYPE_DECL for a class-template, then there won't |
3691 | be DECL_LANG_SPECIFIC. The information equivalent to | |
3692 | DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */ | |
c8094d83 | 3693 | else if (DECL_IMPLICIT_TYPEDEF_P (decl) |
1c10870d AS |
3694 | && TYPE_TEMPLATE_INFO (TREE_TYPE (decl)) |
3695 | && TYPE_TI_TEMPLATE (TREE_TYPE (decl))) | |
3696 | { | |
3697 | /* Since a template declaration already existed for this | |
3698 | class-type, we must be redeclaring it here. Make sure | |
0e339752 | 3699 | that the redeclaration is valid. */ |
1c10870d AS |
3700 | redeclare_class_template (TREE_TYPE (decl), |
3701 | current_template_parms); | |
3702 | /* We don't need to create a new TEMPLATE_DECL; just use the | |
3703 | one we already had. */ | |
3704 | tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl)); | |
3705 | } | |
f84b4be9 | 3706 | else |
786b5245 | 3707 | { |
c7222c02 MM |
3708 | tmpl = build_template_decl (decl, current_template_parms, |
3709 | member_template_p); | |
cfe507be MM |
3710 | new_template_p = 1; |
3711 | ||
f84b4be9 JM |
3712 | if (DECL_LANG_SPECIFIC (decl) |
3713 | && DECL_TEMPLATE_SPECIALIZATION (decl)) | |
3714 | { | |
3715 | /* A specialization of a member template of a template | |
c6002625 | 3716 | class. */ |
f84b4be9 JM |
3717 | SET_DECL_TEMPLATE_SPECIALIZATION (tmpl); |
3718 | DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl); | |
3719 | DECL_TEMPLATE_INFO (decl) = NULL_TREE; | |
3720 | } | |
786b5245 | 3721 | } |
8d08fdba MS |
3722 | } |
3723 | else | |
3724 | { | |
e1a5ccf7 | 3725 | tree a, t, current, parms; |
ba4f4e5d | 3726 | int i; |
6633d636 | 3727 | |
6b9ab5cc MM |
3728 | if (TREE_CODE (decl) == TYPE_DECL) |
3729 | { | |
ed44da02 MM |
3730 | if ((IS_AGGR_TYPE_CODE (TREE_CODE (TREE_TYPE (decl))) |
3731 | || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE) | |
3732 | && TYPE_TEMPLATE_INFO (TREE_TYPE (decl)) | |
3733 | && TYPE_TI_TEMPLATE (TREE_TYPE (decl))) | |
3734 | tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl)); | |
6b9ab5cc MM |
3735 | else |
3736 | { | |
0f51ccfc | 3737 | error ("%qD does not declare a template type", decl); |
6b9ab5cc MM |
3738 | return decl; |
3739 | } | |
3740 | } | |
f3368a90 | 3741 | else if (!DECL_LANG_SPECIFIC (decl) || !DECL_TEMPLATE_INFO (decl)) |
c91a56d2 | 3742 | { |
0f51ccfc | 3743 | error ("template definition of non-template %q#D", decl); |
3ac3d9ea | 3744 | return decl; |
c91a56d2 | 3745 | } |
8d08fdba | 3746 | else |
5566b478 | 3747 | tmpl = DECL_TI_TEMPLATE (decl); |
c8094d83 | 3748 | |
c353b8e3 | 3749 | if (DECL_FUNCTION_TEMPLATE_P (tmpl) |
c8094d83 | 3750 | && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl) |
c353b8e3 | 3751 | && DECL_TEMPLATE_SPECIALIZATION (decl) |
c7222c02 | 3752 | && DECL_MEMBER_TEMPLATE_P (tmpl)) |
98c1c668 | 3753 | { |
e1a5ccf7 JM |
3754 | tree new_tmpl; |
3755 | ||
3756 | /* The declaration is a specialization of a member | |
3757 | template, declared outside the class. Therefore, the | |
3758 | innermost template arguments will be NULL, so we | |
3759 | replace them with the arguments determined by the | |
3760 | earlier call to check_explicit_specialization. */ | |
3761 | args = DECL_TI_ARGS (decl); | |
3762 | ||
c8094d83 | 3763 | new_tmpl |
c7222c02 MM |
3764 | = build_template_decl (decl, current_template_parms, |
3765 | member_template_p); | |
e1a5ccf7 JM |
3766 | DECL_TEMPLATE_RESULT (new_tmpl) = decl; |
3767 | TREE_TYPE (new_tmpl) = TREE_TYPE (decl); | |
3768 | DECL_TI_TEMPLATE (decl) = new_tmpl; | |
3769 | SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl); | |
c8094d83 | 3770 | DECL_TEMPLATE_INFO (new_tmpl) |
e1b3e07d | 3771 | = tree_cons (tmpl, args, NULL_TREE); |
e1a5ccf7 | 3772 | |
c8094d83 MS |
3773 | register_specialization (new_tmpl, |
3774 | most_general_template (tmpl), | |
d63d5d0c ILT |
3775 | args, |
3776 | is_friend); | |
e1a5ccf7 | 3777 | return decl; |
98c1c668 | 3778 | } |
98c1c668 | 3779 | |
e1a5ccf7 | 3780 | /* Make sure the template headers we got make sense. */ |
6633d636 | 3781 | |
e1a5ccf7 JM |
3782 | parms = DECL_TEMPLATE_PARMS (tmpl); |
3783 | i = TMPL_PARMS_DEPTH (parms); | |
3784 | if (TMPL_ARGS_DEPTH (args) != i) | |
98c1c668 | 3785 | { |
0f51ccfc | 3786 | error ("expected %d levels of template parms for %q#D, got %d", |
0cbd7506 | 3787 | i, decl, TMPL_ARGS_DEPTH (args)); |
98c1c668 | 3788 | } |
e1a5ccf7 JM |
3789 | else |
3790 | for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms)) | |
3791 | { | |
3792 | a = TMPL_ARGS_LEVEL (args, i); | |
3793 | t = INNERMOST_TEMPLATE_PARMS (parms); | |
3794 | ||
3795 | if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a)) | |
3796 | { | |
3797 | if (current == decl) | |
0f51ccfc | 3798 | error ("got %d template parameters for %q#D", |
0cbd7506 | 3799 | TREE_VEC_LENGTH (a), decl); |
e1a5ccf7 | 3800 | else |
0f51ccfc | 3801 | error ("got %d template parameters for %q#T", |
0cbd7506 | 3802 | TREE_VEC_LENGTH (a), current); |
33bd39a2 | 3803 | error (" but %d required", TREE_VEC_LENGTH (t)); |
f1cc0515 | 3804 | return error_mark_node; |
e1a5ccf7 | 3805 | } |
98c1c668 | 3806 | |
e1a5ccf7 | 3807 | /* Perhaps we should also check that the parms are used in the |
0cbd7506 | 3808 | appropriate qualifying scopes in the declarator? */ |
6633d636 | 3809 | |
e1a5ccf7 JM |
3810 | if (current == decl) |
3811 | current = ctx; | |
3812 | else | |
3813 | current = TYPE_CONTEXT (current); | |
3814 | } | |
5566b478 | 3815 | } |
8d08fdba | 3816 | |
5566b478 MS |
3817 | DECL_TEMPLATE_RESULT (tmpl) = decl; |
3818 | TREE_TYPE (tmpl) = TREE_TYPE (decl); | |
8d08fdba | 3819 | |
36a117a5 MM |
3820 | /* Push template declarations for global functions and types. Note |
3821 | that we do not try to push a global template friend declared in a | |
3822 | template class; such a thing may well depend on the template | |
39c01e4c | 3823 | parameters of the class. */ |
c8094d83 | 3824 | if (new_template_p && !ctx |
36a117a5 | 3825 | && !(is_friend && template_class_depth (current_class_type) > 0)) |
c6f9f83b | 3826 | { |
d63d5d0c | 3827 | tmpl = pushdecl_namespace_level (tmpl, is_friend); |
c6f9f83b KL |
3828 | if (tmpl == error_mark_node) |
3829 | return error_mark_node; | |
bd3d082e KL |
3830 | |
3831 | /* Hide template friend classes that haven't been declared yet. */ | |
3832 | if (is_friend && TREE_CODE (decl) == TYPE_DECL) | |
3833 | { | |
3834 | DECL_ANTICIPATED (tmpl) = 1; | |
3835 | DECL_FRIEND_P (tmpl) = 1; | |
3836 | } | |
c6f9f83b | 3837 | } |
8d08fdba | 3838 | |
5566b478 | 3839 | if (primary) |
5dd236e2 NS |
3840 | { |
3841 | DECL_PRIMARY_TEMPLATE (tmpl) = tmpl; | |
3842 | if (DECL_CONV_FN_P (tmpl)) | |
3843 | { | |
6e04241f GS |
3844 | int depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)); |
3845 | ||
5dd236e2 NS |
3846 | /* It is a conversion operator. See if the type converted to |
3847 | depends on innermost template operands. */ | |
c8094d83 | 3848 | |
d43f603d KL |
3849 | if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl)), |
3850 | depth)) | |
5dd236e2 NS |
3851 | DECL_TEMPLATE_CONV_FN_P (tmpl) = 1; |
3852 | } | |
3853 | } | |
5566b478 | 3854 | |
a692ad2e | 3855 | /* The DECL_TI_ARGS of DECL contains full set of arguments referring |
28ba38e3 KL |
3856 | back to its most general template. If TMPL is a specialization, |
3857 | ARGS may only have the innermost set of arguments. Add the missing | |
3858 | argument levels if necessary. */ | |
3859 | if (DECL_TEMPLATE_INFO (tmpl)) | |
3860 | args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args); | |
3861 | ||
e1b3e07d | 3862 | info = tree_cons (tmpl, args, NULL_TREE); |
5566b478 | 3863 | |
9188c363 | 3864 | if (DECL_IMPLICIT_TYPEDEF_P (decl)) |
a0ad3539 | 3865 | SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info); |
2aaf816d | 3866 | else if (DECL_LANG_SPECIFIC (decl)) |
5566b478 | 3867 | DECL_TEMPLATE_INFO (decl) = info; |
3ac3d9ea MM |
3868 | |
3869 | return DECL_TEMPLATE_RESULT (tmpl); | |
8d08fdba MS |
3870 | } |
3871 | ||
6757edfe | 3872 | tree |
3a978d72 | 3873 | push_template_decl (tree decl) |
6757edfe | 3874 | { |
d63d5d0c | 3875 | return push_template_decl_real (decl, false); |
6757edfe MM |
3876 | } |
3877 | ||
3878 | /* Called when a class template TYPE is redeclared with the indicated | |
3879 | template PARMS, e.g.: | |
7fe6899f MM |
3880 | |
3881 | template <class T> struct S; | |
3882 | template <class T> struct S {}; */ | |
3883 | ||
60feef2c | 3884 | bool |
3a978d72 | 3885 | redeclare_class_template (tree type, tree parms) |
7fe6899f | 3886 | { |
3d7de1fa | 3887 | tree tmpl; |
6757edfe | 3888 | tree tmpl_parms; |
7fe6899f MM |
3889 | int i; |
3890 | ||
3d7de1fa MM |
3891 | if (!TYPE_TEMPLATE_INFO (type)) |
3892 | { | |
0f51ccfc | 3893 | error ("%qT is not a template type", type); |
60feef2c | 3894 | return false; |
3d7de1fa MM |
3895 | } |
3896 | ||
3897 | tmpl = TYPE_TI_TEMPLATE (type); | |
7fe6899f MM |
3898 | if (!PRIMARY_TEMPLATE_P (tmpl)) |
3899 | /* The type is nested in some template class. Nothing to worry | |
3900 | about here; there are no new template parameters for the nested | |
3901 | type. */ | |
60feef2c | 3902 | return true; |
7fe6899f | 3903 | |
ee921ddf MM |
3904 | if (!parms) |
3905 | { | |
3906 | error ("template specifiers not specified in declaration of %qD", | |
3907 | tmpl); | |
60feef2c | 3908 | return false; |
ee921ddf MM |
3909 | } |
3910 | ||
6757edfe MM |
3911 | parms = INNERMOST_TEMPLATE_PARMS (parms); |
3912 | tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl); | |
3913 | ||
7fe6899f MM |
3914 | if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms)) |
3915 | { | |
dee15844 | 3916 | error ("previous declaration %q+D", tmpl); |
f49d8c52 | 3917 | error ("used %d template parameter(s) instead of %d", |
c8094d83 | 3918 | TREE_VEC_LENGTH (tmpl_parms), |
f49d8c52 | 3919 | TREE_VEC_LENGTH (parms)); |
60feef2c | 3920 | return false; |
7fe6899f MM |
3921 | } |
3922 | ||
3923 | for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i) | |
3924 | { | |
2d8ba2c7 LM |
3925 | tree tmpl_parm; |
3926 | tree parm; | |
3927 | tree tmpl_default; | |
3928 | tree parm_default; | |
3929 | ||
3930 | if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node | |
3931 | || TREE_VEC_ELT (parms, i) == error_mark_node) | |
3932 | continue; | |
3933 | ||
3934 | tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i)); | |
3935 | parm = TREE_VALUE (TREE_VEC_ELT (parms, i)); | |
3936 | tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)); | |
3937 | parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i)); | |
7fe6899f | 3938 | |
2649701f KL |
3939 | /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or |
3940 | TEMPLATE_DECL. */ | |
0f67a82f LM |
3941 | if (tmpl_parm != error_mark_node |
3942 | && (TREE_CODE (tmpl_parm) != TREE_CODE (parm) | |
3943 | || (TREE_CODE (tmpl_parm) != TYPE_DECL | |
3944 | && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm))))) | |
7fe6899f | 3945 | { |
dee15844 | 3946 | error ("template parameter %q+#D", tmpl_parm); |
0f51ccfc | 3947 | error ("redeclared here as %q#D", parm); |
60feef2c | 3948 | return false; |
7fe6899f MM |
3949 | } |
3950 | ||
3951 | if (tmpl_default != NULL_TREE && parm_default != NULL_TREE) | |
3952 | { | |
3953 | /* We have in [temp.param]: | |
3954 | ||
3955 | A template-parameter may not be given default arguments | |
3956 | by two different declarations in the same scope. */ | |
0f51ccfc | 3957 | error ("redefinition of default argument for %q#D", parm); |
ddd2d57e | 3958 | error ("%J original definition appeared here", tmpl_parm); |
60feef2c | 3959 | return false; |
7fe6899f MM |
3960 | } |
3961 | ||
3962 | if (parm_default != NULL_TREE) | |
3963 | /* Update the previous template parameters (which are the ones | |
3964 | that will really count) with the new default value. */ | |
3965 | TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default; | |
7ce74c5e MM |
3966 | else if (tmpl_default != NULL_TREE) |
3967 | /* Update the new parameters, too; they'll be used as the | |
3968 | parameters for any members. */ | |
3969 | TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default; | |
7fe6899f | 3970 | } |
60feef2c LM |
3971 | |
3972 | return true; | |
7fe6899f | 3973 | } |
75650646 | 3974 | |
9baa27a9 MM |
3975 | /* Simplify EXPR if it is a non-dependent expression. Returns the |
3976 | (possibly simplified) expression. */ | |
3977 | ||
3978 | tree | |
3979 | fold_non_dependent_expr (tree expr) | |
3980 | { | |
d4a200d3 SM |
3981 | if (expr == NULL_TREE) |
3982 | return NULL_TREE; | |
3983 | ||
9baa27a9 MM |
3984 | /* If we're in a template, but EXPR isn't value dependent, simplify |
3985 | it. We're supposed to treat: | |
c8094d83 | 3986 | |
9baa27a9 MM |
3987 | template <typename T> void f(T[1 + 1]); |
3988 | template <typename T> void f(T[2]); | |
c8094d83 | 3989 | |
9baa27a9 MM |
3990 | as two declarations of the same function, for example. */ |
3991 | if (processing_template_decl | |
3992 | && !type_dependent_expression_p (expr) | |
7416ab02 | 3993 | && !value_dependent_expression_p (expr)) |
9baa27a9 MM |
3994 | { |
3995 | HOST_WIDE_INT saved_processing_template_decl; | |
3996 | ||
3997 | saved_processing_template_decl = processing_template_decl; | |
3998 | processing_template_decl = 0; | |
3999 | expr = tsubst_copy_and_build (expr, | |
4000 | /*args=*/NULL_TREE, | |
4001 | tf_error, | |
4002 | /*in_decl=*/NULL_TREE, | |
015c2c66 MM |
4003 | /*function_p=*/false, |
4004 | /*integral_constant_expression_p=*/true); | |
9baa27a9 MM |
4005 | processing_template_decl = saved_processing_template_decl; |
4006 | } | |
4007 | return expr; | |
4008 | } | |
4009 | ||
b6ab6892 GB |
4010 | /* EXPR is an expression which is used in a constant-expression context. |
4011 | For instance, it could be a VAR_DECL with a constant initializer. | |
4012 | Extract the innest constant expression. | |
c8094d83 | 4013 | |
8a784e4a NS |
4014 | This is basically a more powerful version of |
4015 | integral_constant_value, which can be used also in templates where | |
4016 | initializers can maintain a syntactic rather than semantic form | |
4017 | (even if they are non-dependent, for access-checking purposes). */ | |
b6ab6892 | 4018 | |
993acaec | 4019 | static tree |
b6ab6892 GB |
4020 | fold_decl_constant_value (tree expr) |
4021 | { | |
4ef69b83 GB |
4022 | tree const_expr = expr; |
4023 | do | |
b6ab6892 | 4024 | { |
b6ab6892 | 4025 | expr = fold_non_dependent_expr (const_expr); |
4ef69b83 | 4026 | const_expr = integral_constant_value (expr); |
b6ab6892 | 4027 | } |
4ef69b83 | 4028 | while (expr != const_expr); |
b6ab6892 | 4029 | |
8a784e4a | 4030 | return expr; |
b6ab6892 GB |
4031 | } |
4032 | ||
4033 | /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which | |
4034 | must be a function or a pointer-to-function type, as specified | |
4035 | in [temp.arg.nontype]: disambiguate EXPR if it is an overload set, | |
4036 | and check that the resulting function has external linkage. */ | |
4037 | ||
4038 | static tree | |
4039 | convert_nontype_argument_function (tree type, tree expr) | |
4040 | { | |
4041 | tree fns = expr; | |
4042 | tree fn, fn_no_ptr; | |
4043 | ||
4044 | fn = instantiate_type (type, fns, tf_none); | |
4045 | if (fn == error_mark_node) | |
4046 | return error_mark_node; | |
4047 | ||
4048 | fn_no_ptr = fn; | |
4049 | if (TREE_CODE (fn_no_ptr) == ADDR_EXPR) | |
4050 | fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0); | |
eff3a276 MM |
4051 | if (TREE_CODE (fn_no_ptr) == BASELINK) |
4052 | fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr); | |
4053 | ||
b6ab6892 GB |
4054 | /* [temp.arg.nontype]/1 |
4055 | ||
4056 | A template-argument for a non-type, non-template template-parameter | |
4057 | shall be one of: | |
4058 | [...] | |
4059 | -- the address of an object or function with external linkage. */ | |
4060 | if (!DECL_EXTERNAL_LINKAGE_P (fn_no_ptr)) | |
4061 | { | |
4062 | error ("%qE is not a valid template argument for type %qT " | |
4063 | "because function %qD has not external linkage", | |
4064 | expr, type, fn_no_ptr); | |
4065 | return NULL_TREE; | |
4066 | } | |
4067 | ||
4068 | return fn; | |
4069 | } | |
4070 | ||
75650646 MM |
4071 | /* Attempt to convert the non-type template parameter EXPR to the |
4072 | indicated TYPE. If the conversion is successful, return the | |
dc957d14 | 4073 | converted value. If the conversion is unsuccessful, return |
75650646 MM |
4074 | NULL_TREE if we issued an error message, or error_mark_node if we |
4075 | did not. We issue error messages for out-and-out bad template | |
4076 | parameters, but not simply because the conversion failed, since we | |
9baa27a9 | 4077 | might be just trying to do argument deduction. Both TYPE and EXPR |
b6ab6892 GB |
4078 | must be non-dependent. |
4079 | ||
4080 | The conversion follows the special rules described in | |
4081 | [temp.arg.nontype], and it is much more strict than an implicit | |
4082 | conversion. | |
4083 | ||
4084 | This function is called twice for each template argument (see | |
4085 | lookup_template_class for a more accurate description of this | |
4086 | problem). This means that we need to handle expressions which | |
4087 | are not valid in a C++ source, but can be created from the | |
4088 | first call (for instance, casts to perform conversions). These | |
4089 | hacks can go away after we fix the double coercion problem. */ | |
75650646 MM |
4090 | |
4091 | static tree | |
3a978d72 | 4092 | convert_nontype_argument (tree type, tree expr) |
75650646 | 4093 | { |
9baa27a9 MM |
4094 | tree expr_type; |
4095 | ||
b6ab6892 GB |
4096 | /* Detect immediately string literals as invalid non-type argument. |
4097 | This special-case is not needed for correctness (we would easily | |
4098 | catch this later), but only to provide better diagnostic for this | |
4099 | common user mistake. As suggested by DR 100, we do not mention | |
4100 | linkage issues in the diagnostic as this is not the point. */ | |
4101 | if (TREE_CODE (expr) == STRING_CST) | |
4102 | { | |
4103 | error ("%qE is not a valid template argument for type %qT " | |
4104 | "because string literals can never be used in this context", | |
4105 | expr, type); | |
4106 | return NULL_TREE; | |
4107 | } | |
4108 | ||
9baa27a9 MM |
4109 | /* If we are in a template, EXPR may be non-dependent, but still |
4110 | have a syntactic, rather than semantic, form. For example, EXPR | |
4111 | might be a SCOPE_REF, rather than the VAR_DECL to which the | |
4112 | SCOPE_REF refers. Preserving the qualifying scope is necessary | |
4113 | so that access checking can be performed when the template is | |
4114 | instantiated -- but here we need the resolved form so that we can | |
4115 | convert the argument. */ | |
4116 | expr = fold_non_dependent_expr (expr); | |
b166a559 VR |
4117 | if (error_operand_p (expr)) |
4118 | return error_mark_node; | |
9baa27a9 | 4119 | expr_type = TREE_TYPE (expr); |
75650646 | 4120 | |
b6ab6892 GB |
4121 | /* HACK: Due to double coercion, we can get a |
4122 | NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here, | |
4123 | which is the tree that we built on the first call (see | |
4124 | below when coercing to reference to object or to reference to | |
4125 | function). We just strip everything and get to the arg. | |
4126 | See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C | |
4127 | for examples. */ | |
4128 | if (TREE_CODE (expr) == NOP_EXPR) | |
75650646 | 4129 | { |
b6ab6892 | 4130 | if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type)) |
75650646 | 4131 | { |
b6ab6892 GB |
4132 | /* ??? Maybe we could use convert_from_reference here, but we |
4133 | would need to relax its constraints because the NOP_EXPR | |
4134 | could actually change the type to something more cv-qualified, | |
4135 | and this is not folded by convert_from_reference. */ | |
4136 | tree addr = TREE_OPERAND (expr, 0); | |
4137 | gcc_assert (TREE_CODE (expr_type) == REFERENCE_TYPE); | |
4138 | gcc_assert (TREE_CODE (addr) == ADDR_EXPR); | |
4139 | gcc_assert (TREE_CODE (TREE_TYPE (addr)) == POINTER_TYPE); | |
4140 | gcc_assert (same_type_ignoring_top_level_qualifiers_p | |
4141 | (TREE_TYPE (expr_type), | |
4142 | TREE_TYPE (TREE_TYPE (addr)))); | |
4143 | ||
4144 | expr = TREE_OPERAND (addr, 0); | |
4145 | expr_type = TREE_TYPE (expr); | |
75650646 MM |
4146 | } |
4147 | ||
b6ab6892 GB |
4148 | /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the |
4149 | parameter is a pointer to object, through decay and | |
4150 | qualification conversion. Let's strip everything. */ | |
4151 | else if (TYPE_PTROBV_P (type)) | |
75650646 | 4152 | { |
b6ab6892 GB |
4153 | STRIP_NOPS (expr); |
4154 | gcc_assert (TREE_CODE (expr) == ADDR_EXPR); | |
4155 | gcc_assert (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE); | |
4156 | /* Skip the ADDR_EXPR only if it is part of the decay for | |
4157 | an array. Otherwise, it is part of the original argument | |
4158 | in the source code. */ | |
4159 | if (TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == ARRAY_TYPE) | |
4160 | expr = TREE_OPERAND (expr, 0); | |
4161 | expr_type = TREE_TYPE (expr); | |
f3400fe2 | 4162 | } |
75650646 | 4163 | } |
c61dce3a | 4164 | |
b6ab6892 | 4165 | /* [temp.arg.nontype]/5, bullet 1 |
75650646 | 4166 | |
b6ab6892 GB |
4167 | For a non-type template-parameter of integral or enumeration type, |
4168 | integral promotions (_conv.prom_) and integral conversions | |
4169 | (_conv.integral_) are applied. */ | |
4170 | if (INTEGRAL_TYPE_P (type)) | |
75650646 | 4171 | { |
75650646 MM |
4172 | if (!INTEGRAL_TYPE_P (expr_type)) |
4173 | return error_mark_node; | |
fddabb2c | 4174 | |
b6ab6892 GB |
4175 | expr = fold_decl_constant_value (expr); |
4176 | /* Notice that there are constant expressions like '4 % 0' which | |
4177 | do not fold into integer constants. */ | |
db02b6b9 | 4178 | if (TREE_CODE (expr) != INTEGER_CST) |
b6ab6892 GB |
4179 | { |
4180 | error ("%qE is not a valid template argument for type %qT " | |
4181 | "because it is a non-constant expression", expr, type); | |
4182 | return NULL_TREE; | |
4183 | } | |
75650646 | 4184 | |
b6ab6892 GB |
4185 | /* At this point, an implicit conversion does what we want, |
4186 | because we already know that the expression is of integral | |
4187 | type. */ | |
4188 | expr = ocp_convert (type, expr, CONV_IMPLICIT, LOOKUP_PROTECT); | |
4189 | if (expr == error_mark_node) | |
4190 | return error_mark_node; | |
75650646 | 4191 | |
b6ab6892 GB |
4192 | /* Conversion was allowed: fold it to a bare integer constant. */ |
4193 | expr = fold (expr); | |
4194 | } | |
4195 | /* [temp.arg.nontype]/5, bullet 2 | |
75650646 | 4196 | |
b6ab6892 GB |
4197 | For a non-type template-parameter of type pointer to object, |
4198 | qualification conversions (_conv.qual_) and the array-to-pointer | |
4199 | conversion (_conv.array_) are applied. */ | |
4200 | else if (TYPE_PTROBV_P (type)) | |
4201 | { | |
4202 | /* [temp.arg.nontype]/1 (TC1 version, DR 49): | |
75650646 | 4203 | |
b6ab6892 GB |
4204 | A template-argument for a non-type, non-template template-parameter |
4205 | shall be one of: [...] | |
75650646 | 4206 | |
b6ab6892 GB |
4207 | -- the name of a non-type template-parameter; |
4208 | -- the address of an object or function with external linkage, [...] | |
4209 | expressed as "& id-expression" where the & is optional if the name | |
4210 | refers to a function or array, or if the corresponding | |
4211 | template-parameter is a reference. | |
c8094d83 | 4212 | |
b6ab6892 GB |
4213 | Here, we do not care about functions, as they are invalid anyway |
4214 | for a parameter of type pointer-to-object. */ | |
1082fd10 MM |
4215 | |
4216 | if (DECL_P (expr) && DECL_TEMPLATE_PARM_P (expr)) | |
4217 | /* Non-type template parameters are OK. */ | |
4218 | ; | |
4219 | else if (TREE_CODE (expr) != ADDR_EXPR | |
4220 | && TREE_CODE (expr_type) != ARRAY_TYPE) | |
4221 | { | |
4222 | if (TREE_CODE (expr) == VAR_DECL) | |
4223 | { | |
4224 | error ("%qD is not a valid template argument " | |
4225 | "because %qD is a variable, not the address of " | |
4226 | "a variable", | |
4227 | expr, expr); | |
4228 | return NULL_TREE; | |
4229 | } | |
4230 | /* Other values, like integer constants, might be valid | |
4231 | non-type arguments of some other type. */ | |
4232 | return error_mark_node; | |
4233 | } | |
4234 | else | |
4235 | { | |
4236 | tree decl; | |
4237 | ||
4238 | decl = ((TREE_CODE (expr) == ADDR_EXPR) | |
4239 | ? TREE_OPERAND (expr, 0) : expr); | |
4240 | if (TREE_CODE (decl) != VAR_DECL) | |
4241 | { | |
4242 | error ("%qE is not a valid template argument of type %qT " | |
4243 | "because %qE is not a variable", | |
4244 | expr, type, decl); | |
4245 | return NULL_TREE; | |
4246 | } | |
4247 | else if (!DECL_EXTERNAL_LINKAGE_P (decl)) | |
4248 | { | |
4249 | error ("%qE is not a valid template argument of type %qT " | |
4250 | "because %qD does not have external linkage", | |
4251 | expr, type, decl); | |
4252 | return NULL_TREE; | |
4253 | } | |
4254 | } | |
b6ab6892 GB |
4255 | |
4256 | expr = decay_conversion (expr); | |
4257 | if (expr == error_mark_node) | |
4258 | return error_mark_node; | |
75650646 | 4259 | |
b6ab6892 GB |
4260 | expr = perform_qualification_conversions (type, expr); |
4261 | if (expr == error_mark_node) | |
4262 | return error_mark_node; | |
b6ab6892 GB |
4263 | } |
4264 | /* [temp.arg.nontype]/5, bullet 3 | |
4265 | ||
4266 | For a non-type template-parameter of type reference to object, no | |
4267 | conversions apply. The type referred to by the reference may be more | |
4268 | cv-qualified than the (otherwise identical) type of the | |
4269 | template-argument. The template-parameter is bound directly to the | |
4270 | template-argument, which must be an lvalue. */ | |
4271 | else if (TYPE_REF_OBJ_P (type)) | |
4272 | { | |
4273 | if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type), | |
4274 | expr_type)) | |
4275 | return error_mark_node; | |
75650646 | 4276 | |
b6ab6892 GB |
4277 | if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type)) |
4278 | { | |
4279 | error ("%qE is not a valid template argument for type %qT " | |
4280 | "because of conflicts in cv-qualification", expr, type); | |
4281 | return NULL_TREE; | |
4282 | } | |
c8094d83 | 4283 | |
b6ab6892 GB |
4284 | if (!real_lvalue_p (expr)) |
4285 | { | |
4286 | error ("%qE is not a valid template argument for type %qT " | |
944fd207 | 4287 | "because it is not an lvalue", expr, type); |
b6ab6892 GB |
4288 | return NULL_TREE; |
4289 | } | |
e6e174e5 | 4290 | |
b6ab6892 | 4291 | /* [temp.arg.nontype]/1 |
75650646 | 4292 | |
b6ab6892 GB |
4293 | A template-argument for a non-type, non-template template-parameter |
4294 | shall be one of: [...] | |
75650646 | 4295 | |
03fd3f84 | 4296 | -- the address of an object or function with external linkage. */ |
b6ab6892 GB |
4297 | if (!DECL_EXTERNAL_LINKAGE_P (expr)) |
4298 | { | |
4299 | error ("%qE is not a valid template argument for type %qT " | |
4300 | "because object %qD has not external linkage", | |
4301 | expr, type, expr); | |
4302 | return NULL_TREE; | |
4303 | } | |
0dc09a61 | 4304 | |
b6ab6892 GB |
4305 | expr = build_nop (type, build_address (expr)); |
4306 | } | |
4307 | /* [temp.arg.nontype]/5, bullet 4 | |
4308 | ||
4309 | For a non-type template-parameter of type pointer to function, only | |
4310 | the function-to-pointer conversion (_conv.func_) is applied. If the | |
4311 | template-argument represents a set of overloaded functions (or a | |
4312 | pointer to such), the matching function is selected from the set | |
4313 | (_over.over_). */ | |
4314 | else if (TYPE_PTRFN_P (type)) | |
4315 | { | |
4316 | /* If the argument is a template-id, we might not have enough | |
2c164de6 | 4317 | context information to decay the pointer. */ |
b6ab6892 GB |
4318 | if (!type_unknown_p (expr_type)) |
4319 | { | |
4320 | expr = decay_conversion (expr); | |
4321 | if (expr == error_mark_node) | |
4322 | return error_mark_node; | |
4323 | } | |
75650646 | 4324 | |
b6ab6892 GB |
4325 | expr = convert_nontype_argument_function (type, expr); |
4326 | if (!expr || expr == error_mark_node) | |
4327 | return expr; | |
4328 | } | |
4329 | /* [temp.arg.nontype]/5, bullet 5 | |
75650646 | 4330 | |
b6ab6892 GB |
4331 | For a non-type template-parameter of type reference to function, no |
4332 | conversions apply. If the template-argument represents a set of | |
4333 | overloaded functions, the matching function is selected from the set | |
4334 | (_over.over_). */ | |
4335 | else if (TYPE_REFFN_P (type)) | |
4336 | { | |
4337 | if (TREE_CODE (expr) == ADDR_EXPR) | |
4338 | { | |
4339 | error ("%qE is not a valid template argument for type %qT " | |
4340 | "because it is a pointer", expr, type); | |
4341 | inform ("try using %qE instead", TREE_OPERAND (expr, 0)); | |
4342 | return NULL_TREE; | |
4343 | } | |
75650646 | 4344 | |
b6ab6892 GB |
4345 | expr = convert_nontype_argument_function (TREE_TYPE (type), expr); |
4346 | if (!expr || expr == error_mark_node) | |
4347 | return expr; | |
75650646 | 4348 | |
7866705a | 4349 | expr = build_nop (type, build_address (expr)); |
b6ab6892 GB |
4350 | } |
4351 | /* [temp.arg.nontype]/5, bullet 6 | |
75650646 | 4352 | |
b6ab6892 GB |
4353 | For a non-type template-parameter of type pointer to member function, |
4354 | no conversions apply. If the template-argument represents a set of | |
4355 | overloaded member functions, the matching member function is selected | |
4356 | from the set (_over.over_). */ | |
4357 | else if (TYPE_PTRMEMFUNC_P (type)) | |
4358 | { | |
4359 | expr = instantiate_type (type, expr, tf_none); | |
4360 | if (expr == error_mark_node) | |
4361 | return error_mark_node; | |
75650646 | 4362 | |
b6ab6892 GB |
4363 | /* There is no way to disable standard conversions in |
4364 | resolve_address_of_overloaded_function (called by | |
4365 | instantiate_type). It is possible that the call succeeded by | |
4366 | converting &B::I to &D::I (where B is a base of D), so we need | |
4367 | to reject this conversion here. | |
75650646 | 4368 | |
b6ab6892 GB |
4369 | Actually, even if there was a way to disable standard conversions, |
4370 | it would still be better to reject them here so that we can | |
4371 | provide a superior diagnostic. */ | |
4372 | if (!same_type_p (TREE_TYPE (expr), type)) | |
4373 | { | |
4374 | /* Make sure we are just one standard conversion off. */ | |
4375 | gcc_assert (can_convert (type, TREE_TYPE (expr))); | |
4376 | error ("%qE is not a valid template argument for type %qT " | |
4377 | "because it is of type %qT", expr, type, | |
4378 | TREE_TYPE (expr)); | |
4379 | inform ("standard conversions are not allowed in this context"); | |
4380 | return NULL_TREE; | |
4381 | } | |
4382 | } | |
4383 | /* [temp.arg.nontype]/5, bullet 7 | |
59e7a901 | 4384 | |
b6ab6892 GB |
4385 | For a non-type template-parameter of type pointer to data member, |
4386 | qualification conversions (_conv.qual_) are applied. */ | |
4387 | else if (TYPE_PTRMEM_P (type)) | |
4388 | { | |
4389 | expr = perform_qualification_conversions (type, expr); | |
4390 | if (expr == error_mark_node) | |
75650646 | 4391 | return expr; |
75650646 | 4392 | } |
b6ab6892 GB |
4393 | /* A template non-type parameter must be one of the above. */ |
4394 | else | |
4395 | gcc_unreachable (); | |
75650646 | 4396 | |
b6ab6892 GB |
4397 | /* Sanity check: did we actually convert the argument to the |
4398 | right type? */ | |
4399 | gcc_assert (same_type_p (type, TREE_TYPE (expr))); | |
4400 | return expr; | |
75650646 MM |
4401 | } |
4402 | ||
b6ab6892 | 4403 | |
c8094d83 MS |
4404 | /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for |
4405 | template template parameters. Both PARM_PARMS and ARG_PARMS are | |
4406 | vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL | |
744fac59 | 4407 | or PARM_DECL. |
c8094d83 | 4408 | |
744fac59 | 4409 | Consider the example: |
e7e93965 MM |
4410 | template <class T> class A; |
4411 | template<template <class U> class TT> class B; | |
744fac59 | 4412 | |
e7e93965 MM |
4413 | For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are |
4414 | the parameters to A, and OUTER_ARGS contains A. */ | |
744fac59 KL |
4415 | |
4416 | static int | |
c8094d83 | 4417 | coerce_template_template_parms (tree parm_parms, |
0cbd7506 MS |
4418 | tree arg_parms, |
4419 | tsubst_flags_t complain, | |
3a978d72 | 4420 | tree in_decl, |
0cbd7506 | 4421 | tree outer_args) |
744fac59 KL |
4422 | { |
4423 | int nparms, nargs, i; | |
4424 | tree parm, arg; | |
4425 | ||
50bc768d NS |
4426 | gcc_assert (TREE_CODE (parm_parms) == TREE_VEC); |
4427 | gcc_assert (TREE_CODE (arg_parms) == TREE_VEC); | |
744fac59 KL |
4428 | |
4429 | nparms = TREE_VEC_LENGTH (parm_parms); | |
4430 | nargs = TREE_VEC_LENGTH (arg_parms); | |
4431 | ||
e7e93965 | 4432 | if (nargs != nparms) |
744fac59 KL |
4433 | return 0; |
4434 | ||
4435 | for (i = 0; i < nparms; ++i) | |
4436 | { | |
2d8ba2c7 LM |
4437 | if (TREE_VEC_ELT (parm_parms, i) == error_mark_node |
4438 | || TREE_VEC_ELT (arg_parms, i) == error_mark_node) | |
42b304f1 LM |
4439 | continue; |
4440 | ||
744fac59 KL |
4441 | parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i)); |
4442 | arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i)); | |
4443 | ||
4444 | if (arg == NULL_TREE || arg == error_mark_node | |
0cbd7506 | 4445 | || parm == NULL_TREE || parm == error_mark_node) |
744fac59 KL |
4446 | return 0; |
4447 | ||
4448 | if (TREE_CODE (arg) != TREE_CODE (parm)) | |
0cbd7506 | 4449 | return 0; |
744fac59 KL |
4450 | |
4451 | switch (TREE_CODE (parm)) | |
4452 | { | |
744fac59 KL |
4453 | case TEMPLATE_DECL: |
4454 | /* We encounter instantiations of templates like | |
4455 | template <template <template <class> class> class TT> | |
4456 | class C; */ | |
700466c2 JM |
4457 | { |
4458 | tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm); | |
4459 | tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg); | |
4460 | ||
8c6ab2db NS |
4461 | if (!coerce_template_template_parms |
4462 | (parmparm, argparm, complain, in_decl, outer_args)) | |
700466c2 JM |
4463 | return 0; |
4464 | } | |
5d80a306 DG |
4465 | /* Fall through. */ |
4466 | ||
4467 | case TYPE_DECL: | |
4468 | if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)) | |
4469 | != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))) | |
4470 | /* One is a parameter pack, the other is not. */ | |
4471 | return 0; | |
700466c2 | 4472 | break; |
744fac59 KL |
4473 | |
4474 | case PARM_DECL: | |
4475 | /* The tsubst call is used to handle cases such as | |
00bdb87f KL |
4476 | |
4477 | template <int> class C {}; | |
4478 | template <class T, template <T> class TT> class D {}; | |
4479 | D<int, C> d; | |
4480 | ||
744fac59 | 4481 | i.e. the parameter list of TT depends on earlier parameters. */ |
00bdb87f KL |
4482 | if (!dependent_type_p (TREE_TYPE (arg)) |
4483 | && !same_type_p | |
4484 | (tsubst (TREE_TYPE (parm), outer_args, complain, in_decl), | |
4485 | TREE_TYPE (arg))) | |
744fac59 | 4486 | return 0; |
5d80a306 DG |
4487 | |
4488 | if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)) | |
4489 | != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))) | |
4490 | /* One is a parameter pack, the other is not. */ | |
4491 | return 0; | |
744fac59 | 4492 | break; |
c8094d83 | 4493 | |
744fac59 | 4494 | default: |
315fb5db | 4495 | gcc_unreachable (); |
744fac59 KL |
4496 | } |
4497 | } | |
4498 | return 1; | |
4499 | } | |
4500 | ||
8b5b8b7c MM |
4501 | /* Convert the indicated template ARG as necessary to match the |
4502 | indicated template PARM. Returns the converted ARG, or | |
c2ea3a40 NS |
4503 | error_mark_node if the conversion was unsuccessful. Error and |
4504 | warning messages are issued under control of COMPLAIN. This | |
4505 | conversion is for the Ith parameter in the parameter list. ARGS is | |
4506 | the full set of template arguments deduced so far. */ | |
8b5b8b7c MM |
4507 | |
4508 | static tree | |
c8094d83 | 4509 | convert_template_argument (tree parm, |
0cbd7506 MS |
4510 | tree arg, |
4511 | tree args, | |
4512 | tsubst_flags_t complain, | |
4513 | int i, | |
4514 | tree in_decl) | |
8b5b8b7c MM |
4515 | { |
4516 | tree val; | |
8b5b8b7c | 4517 | int is_type, requires_type, is_tmpl_type, requires_tmpl_type; |
5d80a306 | 4518 | tree check_arg = arg; |
c8094d83 | 4519 | |
c8094d83 | 4520 | if (TREE_CODE (arg) == TREE_LIST |
a5ac359a | 4521 | && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF) |
c8094d83 | 4522 | { |
8b5b8b7c MM |
4523 | /* The template argument was the name of some |
4524 | member function. That's usually | |
0e339752 | 4525 | invalid, but static members are OK. In any |
8b5b8b7c MM |
4526 | case, grab the underlying fields/functions |
4527 | and issue an error later if required. */ | |
4528 | arg = TREE_VALUE (arg); | |
4529 | TREE_TYPE (arg) = unknown_type_node; | |
4530 | } | |
4531 | ||
4532 | requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL; | |
4533 | requires_type = (TREE_CODE (parm) == TYPE_DECL | |
4534 | || requires_tmpl_type); | |
4535 | ||
5d80a306 DG |
4536 | /* When determining whether a argument pack expansion is a template, |
4537 | look at the pattern. */ | |
4538 | if (TREE_CODE (check_arg) == TYPE_PACK_EXPANSION) | |
4539 | check_arg = PACK_EXPANSION_PATTERN (check_arg); | |
4540 | ||
4541 | is_tmpl_type = | |
4542 | ((TREE_CODE (check_arg) == TEMPLATE_DECL | |
4543 | && TREE_CODE (DECL_TEMPLATE_RESULT (check_arg)) == TYPE_DECL) | |
4544 | || TREE_CODE (check_arg) == TEMPLATE_TEMPLATE_PARM | |
4545 | || TREE_CODE (check_arg) == UNBOUND_CLASS_TEMPLATE); | |
c8094d83 | 4546 | |
b8c6534b KL |
4547 | if (is_tmpl_type |
4548 | && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM | |
4549 | || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)) | |
8b5b8b7c | 4550 | arg = TYPE_STUB_DECL (arg); |
8b5b8b7c | 4551 | |
2f939d94 | 4552 | is_type = TYPE_P (arg) || is_tmpl_type; |
8b5b8b7c MM |
4553 | |
4554 | if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF | |
4555 | && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM) | |
4556 | { | |
0f51ccfc | 4557 | pedwarn ("to refer to a type member of a template parameter, " |
0cbd7506 | 4558 | "use %<typename %E%>", arg); |
c8094d83 | 4559 | |
8b5b8b7c | 4560 | arg = make_typename_type (TREE_OPERAND (arg, 0), |
3baa501d | 4561 | TREE_OPERAND (arg, 1), |
fc6a28d7 | 4562 | typename_type, |
c2ea3a40 | 4563 | complain & tf_error); |
8b5b8b7c MM |
4564 | is_type = 1; |
4565 | } | |
4566 | if (is_type != requires_type) | |
4567 | { | |
4568 | if (in_decl) | |
4569 | { | |
c2ea3a40 | 4570 | if (complain & tf_error) |
8b5b8b7c | 4571 | { |
0f51ccfc | 4572 | error ("type/value mismatch at argument %d in template " |
0cbd7506 MS |
4573 | "parameter list for %qD", |
4574 | i + 1, in_decl); | |
8b5b8b7c | 4575 | if (is_type) |
0f51ccfc | 4576 | error (" expected a constant of type %qT, got %qT", |
0cbd7506 MS |
4577 | TREE_TYPE (parm), |
4578 | (is_tmpl_type ? DECL_NAME (arg) : arg)); | |
d12a7283 | 4579 | else if (requires_tmpl_type) |
0f51ccfc | 4580 | error (" expected a class template, got %qE", arg); |
8b5b8b7c | 4581 | else |
0f51ccfc | 4582 | error (" expected a type, got %qE", arg); |
8b5b8b7c MM |
4583 | } |
4584 | } | |
4585 | return error_mark_node; | |
4586 | } | |
4587 | if (is_tmpl_type ^ requires_tmpl_type) | |
4588 | { | |
c2ea3a40 | 4589 | if (in_decl && (complain & tf_error)) |
8b5b8b7c | 4590 | { |
0f51ccfc | 4591 | error ("type/value mismatch at argument %d in template " |
0cbd7506 MS |
4592 | "parameter list for %qD", |
4593 | i + 1, in_decl); | |
8b5b8b7c | 4594 | if (is_tmpl_type) |
0f51ccfc | 4595 | error (" expected a type, got %qT", DECL_NAME (arg)); |
8b5b8b7c | 4596 | else |
0f51ccfc | 4597 | error (" expected a class template, got %qT", arg); |
8b5b8b7c MM |
4598 | } |
4599 | return error_mark_node; | |
4600 | } | |
c8094d83 | 4601 | |
8b5b8b7c MM |
4602 | if (is_type) |
4603 | { | |
4604 | if (requires_tmpl_type) | |
4605 | { | |
b8c6534b KL |
4606 | if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE) |
4607 | /* The number of argument required is not known yet. | |
4608 | Just accept it for now. */ | |
4609 | val = TREE_TYPE (arg); | |
8b5b8b7c MM |
4610 | else |
4611 | { | |
b8c6534b | 4612 | tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm); |
5d80a306 DG |
4613 | tree argparm; |
4614 | ||
4615 | check_arg = arg; | |
4616 | /* When determining whether a pack expansion is a template, | |
4617 | look at the pattern. */ | |
4618 | if (TREE_CODE (check_arg) == TYPE_PACK_EXPANSION) | |
4619 | check_arg = PACK_EXPANSION_PATTERN (check_arg); | |
4620 | ||
4621 | argparm = DECL_INNERMOST_TEMPLATE_PARMS (check_arg); | |
b8c6534b KL |
4622 | |
4623 | if (coerce_template_template_parms (parmparm, argparm, | |
4624 | complain, in_decl, | |
6150b602 | 4625 | args)) |
8b5b8b7c | 4626 | { |
b8c6534b | 4627 | val = arg; |
c8094d83 MS |
4628 | |
4629 | /* TEMPLATE_TEMPLATE_PARM node is preferred over | |
b8c6534b | 4630 | TEMPLATE_DECL. */ |
5d80a306 DG |
4631 | if (val != error_mark_node) |
4632 | { | |
4633 | if (DECL_TEMPLATE_TEMPLATE_PARM_P (val)) | |
4634 | val = TREE_TYPE (val); | |
4635 | else if (TREE_CODE (val) == TYPE_PACK_EXPANSION | |
4636 | && DECL_TEMPLATE_TEMPLATE_PARM_P (check_arg)) | |
4637 | { | |
4638 | val = TREE_TYPE (check_arg); | |
4639 | val = make_pack_expansion (val); | |
4640 | } | |
4641 | } | |
8b5b8b7c | 4642 | } |
b8c6534b KL |
4643 | else |
4644 | { | |
c2ea3a40 | 4645 | if (in_decl && (complain & tf_error)) |
b8c6534b | 4646 | { |
0f51ccfc | 4647 | error ("type/value mismatch at argument %d in " |
0cbd7506 MS |
4648 | "template parameter list for %qD", |
4649 | i + 1, in_decl); | |
0f51ccfc | 4650 | error (" expected a template of type %qD, got %qD", |
0cbd7506 | 4651 | parm, arg); |
b8c6534b | 4652 | } |
c8094d83 | 4653 | |
b8c6534b KL |
4654 | val = error_mark_node; |
4655 | } | |
8b5b8b7c MM |
4656 | } |
4657 | } | |
4658 | else | |
058b15c1 | 4659 | val = arg; |
685e39c2 MM |
4660 | /* We only form one instance of each template specialization. |
4661 | Therefore, if we use a non-canonical variant (i.e., a | |
3db45ab5 | 4662 | typedef), any future messages referring to the type will use |
685e39c2 MM |
4663 | the typedef, which is confusing if those future uses do not |
4664 | themselves also use the typedef. */ | |
4665 | if (TYPE_P (val)) | |
4666 | val = canonical_type_variant (val); | |
8b5b8b7c MM |
4667 | } |
4668 | else | |
4669 | { | |
4393e105 | 4670 | tree t = tsubst (TREE_TYPE (parm), args, complain, in_decl); |
8b5b8b7c | 4671 | |
11b810f1 | 4672 | if (invalid_nontype_parm_type_p (t, complain)) |
0cbd7506 | 4673 | return error_mark_node; |
c8094d83 | 4674 | |
d768a589 | 4675 | if (!uses_template_parms (arg) && !uses_template_parms (t)) |
8b5b8b7c MM |
4676 | /* We used to call digest_init here. However, digest_init |
4677 | will report errors, which we don't want when complain | |
4678 | is zero. More importantly, digest_init will try too | |
4679 | hard to convert things: for example, `0' should not be | |
4680 | converted to pointer type at this point according to | |
4681 | the standard. Accepting this is not merely an | |
4682 | extension, since deciding whether or not these | |
4683 | conversions can occur is part of determining which | |
dc957d14 | 4684 | function template to call, or whether a given explicit |
0e339752 | 4685 | argument specification is valid. */ |
8b5b8b7c MM |
4686 | val = convert_nontype_argument (t, arg); |
4687 | else | |
4688 | val = arg; | |
4689 | ||
4690 | if (val == NULL_TREE) | |
4691 | val = error_mark_node; | |
c2ea3a40 | 4692 | else if (val == error_mark_node && (complain & tf_error)) |
0f51ccfc | 4693 | error ("could not convert template argument %qE to %qT", arg, t); |
8b5b8b7c MM |
4694 | } |
4695 | ||
4696 | return val; | |
4697 | } | |
4698 | ||
4699 | /* Convert all template arguments to their appropriate types, and | |
4700 | return a vector containing the innermost resulting template | |
c2ea3a40 | 4701 | arguments. If any error occurs, return error_mark_node. Error and |
3e4a3562 | 4702 | warning messages are issued under control of COMPLAIN. |
75650646 | 4703 | |
e7e93965 | 4704 | If REQUIRE_ALL_ARGS is false, argument deduction will be performed |
84dc00e8 | 4705 | for arguments not specified in ARGS. Otherwise, if |
e7e93965 MM |
4706 | USE_DEFAULT_ARGS is true, default arguments will be used to fill in |
4707 | unspecified arguments. If REQUIRE_ALL_ARGS is true, but | |
4708 | USE_DEFAULT_ARGS is false, then all arguments must be specified in | |
4709 | ARGS. */ | |
c8094d83 | 4710 | |
8d08fdba | 4711 | static tree |
c8094d83 | 4712 | coerce_template_parms (tree parms, |
0cbd7506 MS |
4713 | tree args, |
4714 | tree in_decl, | |
3a978d72 | 4715 | tsubst_flags_t complain, |
e7e93965 MM |
4716 | bool require_all_args, |
4717 | bool use_default_args) | |
8d08fdba | 4718 | { |
a292b002 | 4719 | int nparms, nargs, i, lost = 0; |
e4a84209 | 4720 | tree inner_args; |
8b5b8b7c MM |
4721 | tree new_args; |
4722 | tree new_inner_args; | |
a2c5ed87 | 4723 | bool saved_skip_evaluation; |
a292b002 | 4724 | |
5d80a306 DG |
4725 | /* When used as a boolean value, indicates whether this is a |
4726 | variadic template parameter list. Since it's an int, we can also | |
4727 | subtract it from nparms to get the number of non-variadic | |
4728 | parameters. */ | |
4729 | int variadic_p = template_parms_variadic_p (parms) ? 1 : 0; | |
4730 | ||
4731 | inner_args | |
4732 | = expand_template_argument_pack (INNERMOST_TEMPLATE_ARGS (args)); | |
4733 | ||
bf12d54d | 4734 | nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0; |
a292b002 MS |
4735 | nparms = TREE_VEC_LENGTH (parms); |
4736 | ||
5d80a306 DG |
4737 | if ((nargs > nparms - variadic_p && !variadic_p) |
4738 | || (nargs < nparms - variadic_p | |
e7e93965 MM |
4739 | && require_all_args |
4740 | && (!use_default_args | |
42b304f1 LM |
4741 | || (TREE_VEC_ELT (parms, nargs) != error_mark_node |
4742 | && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs)))))) | |
8d08fdba | 4743 | { |
c8094d83 | 4744 | if (complain & tf_error) |
75650646 | 4745 | { |
5d80a306 DG |
4746 | const char *or_more = ""; |
4747 | if (variadic_p) | |
4748 | { | |
4749 | or_more = " or more"; | |
4750 | --nparms; | |
4751 | } | |
4752 | ||
4753 | error ("wrong number of template arguments (%d, should be %d%s)", | |
4754 | nargs, nparms, or_more); | |
c8094d83 | 4755 | |
75650646 | 4756 | if (in_decl) |
dee15844 | 4757 | error ("provided for %q+D", in_decl); |
75650646 MM |
4758 | } |
4759 | ||
8d08fdba MS |
4760 | return error_mark_node; |
4761 | } | |
4762 | ||
a2c5ed87 MM |
4763 | /* We need to evaluate the template arguments, even though this |
4764 | template-id may be nested within a "sizeof". */ | |
4765 | saved_skip_evaluation = skip_evaluation; | |
4766 | skip_evaluation = false; | |
f31c0a32 | 4767 | new_inner_args = make_tree_vec (nparms); |
8b5b8b7c | 4768 | new_args = add_outermost_template_args (args, new_inner_args); |
5d80a306 | 4769 | for (i = 0; i < nparms - variadic_p; i++) |
8d08fdba | 4770 | { |
8b5b8b7c MM |
4771 | tree arg; |
4772 | tree parm; | |
e4a84209 | 4773 | |
8b5b8b7c MM |
4774 | /* Get the Ith template parameter. */ |
4775 | parm = TREE_VEC_ELT (parms, i); | |
42b304f1 LM |
4776 | |
4777 | if (parm == error_mark_node) | |
2d8ba2c7 LM |
4778 | { |
4779 | TREE_VEC_ELT (new_inner_args, i) = error_mark_node; | |
42b304f1 | 4780 | continue; |
2d8ba2c7 | 4781 | } |
75650646 | 4782 | |
8b5b8b7c | 4783 | /* Calculate the Ith argument. */ |
bf12d54d | 4784 | if (i < nargs) |
5d80a306 DG |
4785 | { |
4786 | arg = TREE_VEC_ELT (inner_args, i); | |
4787 | ||
4788 | if (PACK_EXPANSION_P (arg)) | |
4789 | { | |
4790 | /* If ARG is a pack expansion, then PARM must be | |
4791 | a template parameter pack. We can't expand into a | |
4792 | fixed-length argument list. */ | |
4793 | tree actual_parm = TREE_VALUE (parm); | |
4794 | bool parm_is_parameter_pack | |
4795 | = template_parameter_pack_p (actual_parm); | |
4796 | ||
4797 | if (!parm_is_parameter_pack) | |
4798 | { | |
4799 | if (TREE_CODE (arg) == EXPR_PACK_EXPANSION) | |
4800 | error ("cannot expand %<%E%> into a fixed-length " | |
4801 | "argument list", arg); | |
4802 | else | |
4803 | error ("cannot expand %<%T%> into a fixed-length " | |
4804 | "argument list", arg); | |
4805 | } | |
4806 | } | |
4807 | } | |
e7e93965 | 4808 | else if (require_all_args) |
04c06002 | 4809 | /* There must be a default arg in this case. */ |
a91db711 NS |
4810 | arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args, |
4811 | complain, in_decl); | |
ffd49b19 NS |
4812 | else |
4813 | break; | |
c8094d83 | 4814 | |
50bc768d | 4815 | gcc_assert (arg); |
ffd49b19 | 4816 | if (arg == error_mark_node) |
e34b0922 KL |
4817 | { |
4818 | if (complain & tf_error) | |
4819 | error ("template argument %d is invalid", i + 1); | |
4820 | } | |
c8094d83 MS |
4821 | else |
4822 | arg = convert_template_argument (TREE_VALUE (parm), | |
8b5b8b7c | 4823 | arg, new_args, complain, i, |
c8094d83 MS |
4824 | in_decl); |
4825 | ||
8b5b8b7c | 4826 | if (arg == error_mark_node) |
8d08fdba | 4827 | lost++; |
8b5b8b7c | 4828 | TREE_VEC_ELT (new_inner_args, i) = arg; |
8d08fdba | 4829 | } |
a2c5ed87 | 4830 | skip_evaluation = saved_skip_evaluation; |
8b5b8b7c | 4831 | |
5d80a306 DG |
4832 | if (variadic_p) |
4833 | { | |
4834 | int expected_len = nargs - nparms + 1; | |
4835 | tree parm = TREE_VEC_ELT (parms, nparms - 1); | |
4836 | tree packed_args; | |
4837 | tree argument_pack; | |
4838 | tree packed_types = NULL_TREE; | |
4839 | ||
4840 | packed_args = make_tree_vec (expected_len >= 0 ? expected_len : 0); | |
4841 | ||
4842 | if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL | |
4843 | && uses_parameter_packs (TREE_TYPE (TREE_VALUE (parm)))) | |
4844 | { | |
4845 | /* When the template parameter is a non-type template | |
4846 | parameter pack whose type uses parameter packs, we need | |
4847 | to look at each of the template arguments | |
4848 | separately. Build a vector of the types for these | |
4849 | non-type template parameters in PACKED_TYPES. */ | |
4850 | tree expansion | |
4851 | = make_pack_expansion (TREE_TYPE (TREE_VALUE (parm))); | |
4852 | packed_types = tsubst_pack_expansion (expansion, args, | |
4853 | complain, in_decl); | |
4854 | ||
4855 | if (packed_types == error_mark_node) | |
4856 | return error_mark_node; | |
4857 | ||
4858 | /* Check that we have the right number of arguments. */ | |
4859 | if (i < nargs | |
4860 | && !PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, i)) | |
4861 | && nargs - i != TREE_VEC_LENGTH (packed_types)) | |
4862 | { | |
4863 | error ("wrong number of template arguments (%d, should be %d)", | |
4864 | nargs, nparms - 1 + TREE_VEC_LENGTH (packed_types)); | |
4865 | return error_mark_node; | |
4866 | } | |
4867 | ||
4868 | /* If we aren't able to check the actual arguments now | |
4869 | (because they haven't been expanded yet), we can at least | |
4870 | verify that all of the types used for the non-type | |
4871 | template parameter pack are, in fact, valid for non-type | |
4872 | template parameters. */ | |
4873 | if (i < nargs && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, i))) | |
4874 | { | |
4875 | int j, len = TREE_VEC_LENGTH (packed_types); | |
4876 | for (j = 0; j < len; ++j) | |
4877 | { | |
4878 | tree t = TREE_VEC_ELT (packed_types, j); | |
4879 | if (invalid_nontype_parm_type_p (t, complain)) | |
4880 | return error_mark_node; | |
4881 | } | |
4882 | } | |
4883 | } | |
4884 | ||
4885 | /* Convert the remaining arguments, which will be a part of the | |
4886 | parameter pack "parm". */ | |
4887 | for (; i < nargs; ++i) | |
4888 | { | |
4889 | tree arg = TREE_VEC_ELT (inner_args, i); | |
4890 | tree actual_parm = TREE_VALUE (parm); | |
4891 | ||
4892 | if (packed_types && !PACK_EXPANSION_P (arg)) | |
4893 | { | |
4894 | /* When we have a vector of types (corresponding to the | |
4895 | non-type template parameter pack that uses parameter | |
4896 | packs in its type, as mention above), and the | |
4897 | argument is not an expansion (which expands to a | |
4898 | currently unknown number of arguments), clone the | |
4899 | parm and give it the next type in PACKED_TYPES. */ | |
4900 | actual_parm = copy_node (actual_parm); | |
4901 | TREE_TYPE (actual_parm) = | |
4902 | TREE_VEC_ELT (packed_types, i - nparms + 1); | |
4903 | } | |
4904 | ||
4905 | arg = convert_template_argument (actual_parm, | |
4906 | arg, new_args, complain, i, | |
4907 | in_decl); | |
4908 | if (arg == error_mark_node) | |
4909 | lost++; | |
4910 | TREE_VEC_ELT (packed_args, i - nparms + 1) = arg; | |
4911 | } | |
4912 | ||
4913 | if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL | |
4914 | || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL) | |
4915 | argument_pack = make_node (TYPE_ARGUMENT_PACK); | |
4916 | else | |
4917 | { | |
4918 | argument_pack = make_node (NONTYPE_ARGUMENT_PACK); | |
4919 | TREE_TYPE (argument_pack) = TREE_TYPE (TREE_VALUE (parm)); | |
4920 | TREE_CONSTANT (argument_pack) = 1; | |
4921 | } | |
4922 | ||
4923 | SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args); | |
4924 | TREE_VEC_ELT (new_inner_args, nparms - 1) = argument_pack; | |
4925 | } | |
4926 | ||
8d08fdba MS |
4927 | if (lost) |
4928 | return error_mark_node; | |
8b5b8b7c MM |
4929 | |
4930 | return new_inner_args; | |
8d08fdba MS |
4931 | } |
4932 | ||
34016c81 JM |
4933 | /* Returns 1 if template args OT and NT are equivalent. */ |
4934 | ||
d8e178a0 | 4935 | static int |
3a978d72 | 4936 | template_args_equal (tree ot, tree nt) |
34016c81 JM |
4937 | { |
4938 | if (nt == ot) | |
4939 | return 1; | |
74601d7c | 4940 | |
34016c81 JM |
4941 | if (TREE_CODE (nt) == TREE_VEC) |
4942 | /* For member templates */ | |
74601d7c | 4943 | return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt); |
5d80a306 DG |
4944 | else if (PACK_EXPANSION_P (ot)) |
4945 | return PACK_EXPANSION_P (nt) | |
4946 | && template_args_equal (PACK_EXPANSION_PATTERN (ot), | |
4947 | PACK_EXPANSION_PATTERN (nt)); | |
74601d7c | 4948 | else if (TYPE_P (nt)) |
660845bf | 4949 | return TYPE_P (ot) && same_type_p (ot, nt); |
74601d7c KL |
4950 | else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot)) |
4951 | return 0; | |
34016c81 | 4952 | else |
c8a209ca | 4953 | return cp_tree_equal (ot, nt); |
34016c81 JM |
4954 | } |
4955 | ||
4956 | /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets | |
f84b4be9 JM |
4957 | of template arguments. Returns 0 otherwise. */ |
4958 | ||
6757edfe | 4959 | int |
3a978d72 | 4960 | comp_template_args (tree oldargs, tree newargs) |
5566b478 MS |
4961 | { |
4962 | int i; | |
4963 | ||
5d80a306 DG |
4964 | oldargs = expand_template_argument_pack (oldargs); |
4965 | newargs = expand_template_argument_pack (newargs); | |
4966 | ||
386b8a85 JM |
4967 | if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs)) |
4968 | return 0; | |
4969 | ||
5566b478 MS |
4970 | for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i) |
4971 | { | |
4972 | tree nt = TREE_VEC_ELT (newargs, i); | |
4973 | tree ot = TREE_VEC_ELT (oldargs, i); | |
4974 | ||
34016c81 | 4975 | if (! template_args_equal (ot, nt)) |
61a127b3 | 4976 | return 0; |
5566b478 MS |
4977 | } |
4978 | return 1; | |
4979 | } | |
4980 | ||
5566b478 | 4981 | static void |
3a978d72 | 4982 | add_pending_template (tree d) |
5566b478 | 4983 | { |
3ae18eaf JM |
4984 | tree ti = (TYPE_P (d) |
4985 | ? CLASSTYPE_TEMPLATE_INFO (d) | |
4986 | : DECL_TEMPLATE_INFO (d)); | |
46ccf50a | 4987 | tree pt; |
3ae18eaf | 4988 | int level; |
e92cc029 | 4989 | |
824b9a4c | 4990 | if (TI_PENDING_TEMPLATE_FLAG (ti)) |
5566b478 MS |
4991 | return; |
4992 | ||
3ae18eaf JM |
4993 | /* We are called both from instantiate_decl, where we've already had a |
4994 | tinst_level pushed, and instantiate_template, where we haven't. | |
4995 | Compensate. */ | |
4996 | level = !(current_tinst_level && TINST_DECL (current_tinst_level) == d); | |
4997 | ||
4998 | if (level) | |
4999 | push_tinst_level (d); | |
5000 | ||
46ccf50a JM |
5001 | pt = tree_cons (current_tinst_level, d, NULL_TREE); |
5002 | if (last_pending_template) | |
5003 | TREE_CHAIN (last_pending_template) = pt; | |
5004 | else | |
5005 | pending_templates = pt; | |
5006 | ||
5007 | last_pending_template = pt; | |
5008 | ||
824b9a4c | 5009 | TI_PENDING_TEMPLATE_FLAG (ti) = 1; |
3ae18eaf JM |
5010 | |
5011 | if (level) | |
5012 | pop_tinst_level (); | |
5566b478 MS |
5013 | } |
5014 | ||
386b8a85 | 5015 | |
4ba126e4 MM |
5016 | /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and |
5017 | ARGLIST. Valid choices for FNS are given in the cp-tree.def | |
5018 | documentation for TEMPLATE_ID_EXPR. */ | |
386b8a85 JM |
5019 | |
5020 | tree | |
3a978d72 | 5021 | lookup_template_function (tree fns, tree arglist) |
386b8a85 | 5022 | { |
2c73f9f5 | 5023 | tree type; |
050367a3 | 5024 | |
4ba126e4 MM |
5025 | if (fns == error_mark_node || arglist == error_mark_node) |
5026 | return error_mark_node; | |
5027 | ||
50bc768d | 5028 | gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC); |
4230cec2 NS |
5029 | gcc_assert (fns && (is_overloaded_fn (fns) |
5030 | || TREE_CODE (fns) == IDENTIFIER_NODE)); | |
4ba126e4 | 5031 | |
50ad9642 MM |
5032 | if (BASELINK_P (fns)) |
5033 | { | |
f293ce4b RS |
5034 | BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR, |
5035 | unknown_type_node, | |
5036 | BASELINK_FUNCTIONS (fns), | |
5037 | arglist); | |
50ad9642 MM |
5038 | return fns; |
5039 | } | |
5040 | ||
2c73f9f5 ML |
5041 | type = TREE_TYPE (fns); |
5042 | if (TREE_CODE (fns) == OVERLOAD || !type) | |
5043 | type = unknown_type_node; | |
c8094d83 | 5044 | |
f293ce4b | 5045 | return build2 (TEMPLATE_ID_EXPR, type, fns, arglist); |
386b8a85 JM |
5046 | } |
5047 | ||
a2b60a0e MM |
5048 | /* Within the scope of a template class S<T>, the name S gets bound |
5049 | (in build_self_reference) to a TYPE_DECL for the class, not a | |
5050 | TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type, | |
5051 | or one of its enclosing classes, and that type is a template, | |
5052 | return the associated TEMPLATE_DECL. Otherwise, the original | |
5053 | DECL is returned. */ | |
5054 | ||
a723baf1 | 5055 | tree |
3a978d72 | 5056 | maybe_get_template_decl_from_type_decl (tree decl) |
a2b60a0e MM |
5057 | { |
5058 | return (decl != NULL_TREE | |
c8094d83 | 5059 | && TREE_CODE (decl) == TYPE_DECL |
a2b60a0e | 5060 | && DECL_ARTIFICIAL (decl) |
511b60ff | 5061 | && CLASS_TYPE_P (TREE_TYPE (decl)) |
c8094d83 | 5062 | && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl))) |
a2b60a0e MM |
5063 | ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl; |
5064 | } | |
386b8a85 | 5065 | |
8d08fdba MS |
5066 | /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of |
5067 | parameters, find the desired type. | |
5068 | ||
5069 | D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments. | |
8d08fdba MS |
5070 | |
5071 | IN_DECL, if non-NULL, is the template declaration we are trying to | |
c8094d83 | 5072 | instantiate. |
75650646 | 5073 | |
838dfd8a | 5074 | If ENTERING_SCOPE is nonzero, we are about to enter the scope of |
36a117a5 | 5075 | the class we are looking up. |
c8094d83 | 5076 | |
c2ea3a40 | 5077 | Issue error and warning messages under control of COMPLAIN. |
36a117a5 | 5078 | |
75650646 MM |
5079 | If the template class is really a local class in a template |
5080 | function, then the FUNCTION_CONTEXT is the function in which it is | |
c8094d83 | 5081 | being instantiated. |
b6ab6892 GB |
5082 | |
5083 | ??? Note that this function is currently called *twice* for each | |
5084 | template-id: the first time from the parser, while creating the | |
5085 | incomplete type (finish_template_type), and the second type during the | |
5086 | real instantiation (instantiate_template_class). This is surely something | |
5087 | that we want to avoid. It also causes some problems with argument | |
5088 | coercion (see convert_nontype_argument for more information on this). */ | |
e92cc029 | 5089 | |
8d08fdba | 5090 | tree |
c8094d83 | 5091 | lookup_template_class (tree d1, |
0cbd7506 MS |
5092 | tree arglist, |
5093 | tree in_decl, | |
5094 | tree context, | |
5095 | int entering_scope, | |
5096 | tsubst_flags_t complain) | |
8d08fdba | 5097 | { |
a703fb38 | 5098 | tree template = NULL_TREE, parmlist; |
dbfe2124 | 5099 | tree t; |
c8094d83 | 5100 | |
fd295cb2 | 5101 | timevar_push (TV_NAME_LOOKUP); |
c8094d83 | 5102 | |
5566b478 MS |
5103 | if (TREE_CODE (d1) == IDENTIFIER_NODE) |
5104 | { | |
90ea9897 MM |
5105 | tree value = innermost_non_namespace_value (d1); |
5106 | if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value)) | |
5107 | template = value; | |
73b0fce8 KL |
5108 | else |
5109 | { | |
2c73f9f5 ML |
5110 | if (context) |
5111 | push_decl_namespace (context); | |
10e6657a | 5112 | template = lookup_name (d1); |
3ebc5c52 | 5113 | template = maybe_get_template_decl_from_type_decl (template); |
2c73f9f5 ML |
5114 | if (context) |
5115 | pop_decl_namespace (); | |
73b0fce8 | 5116 | } |
8d019cef JM |
5117 | if (template) |
5118 | context = DECL_CONTEXT (template); | |
5566b478 | 5119 | } |
c91a56d2 MS |
5120 | else if (TREE_CODE (d1) == TYPE_DECL && IS_AGGR_TYPE (TREE_TYPE (d1))) |
5121 | { | |
802dbc34 JM |
5122 | tree type = TREE_TYPE (d1); |
5123 | ||
5124 | /* If we are declaring a constructor, say A<T>::A<T>, we will get | |
5125 | an implicit typename for the second A. Deal with it. */ | |
5126 | if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type)) | |
5127 | type = TREE_TYPE (type); | |
c8094d83 | 5128 | |
802dbc34 | 5129 | if (CLASSTYPE_TEMPLATE_INFO (type)) |
f3400fe2 | 5130 | { |
802dbc34 | 5131 | template = CLASSTYPE_TI_TEMPLATE (type); |
f3400fe2 JM |
5132 | d1 = DECL_NAME (template); |
5133 | } | |
c91a56d2 | 5134 | } |
c8094d83 | 5135 | else if (TREE_CODE (d1) == ENUMERAL_TYPE |
2f939d94 | 5136 | || (TYPE_P (d1) && IS_AGGR_TYPE (d1))) |
5566b478 | 5137 | { |
ed44da02 | 5138 | template = TYPE_TI_TEMPLATE (d1); |
5566b478 MS |
5139 | d1 = DECL_NAME (template); |
5140 | } | |
93cdc044 | 5141 | else if (TREE_CODE (d1) == TEMPLATE_DECL |
17aec3eb | 5142 | && TREE_CODE (DECL_TEMPLATE_RESULT (d1)) == TYPE_DECL) |
93cdc044 JM |
5143 | { |
5144 | template = d1; | |
5145 | d1 = DECL_NAME (template); | |
5146 | context = DECL_CONTEXT (template); | |
5147 | } | |
8d08fdba | 5148 | |
90ea9897 | 5149 | /* Issue an error message if we didn't find a template. */ |
8d08fdba | 5150 | if (! template) |
f3400fe2 | 5151 | { |
c2ea3a40 | 5152 | if (complain & tf_error) |
0cbd7506 | 5153 | error ("%qT is not a template", d1); |
fd295cb2 | 5154 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node); |
f3400fe2 | 5155 | } |
2c73f9f5 | 5156 | |
a87b4257 | 5157 | if (TREE_CODE (template) != TEMPLATE_DECL |
0cbd7506 | 5158 | /* Make sure it's a user visible template, if it was named by |
42eaed49 NS |
5159 | the user. */ |
5160 | || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (template) | |
5161 | && !PRIMARY_TEMPLATE_P (template))) | |
8d08fdba | 5162 | { |
c2ea3a40 | 5163 | if (complain & tf_error) |
0cbd7506 MS |
5164 | { |
5165 | error ("non-template type %qT used as a template", d1); | |
5166 | if (in_decl) | |
dee15844 | 5167 | error ("for template declaration %q+D", in_decl); |
f9c244b8 | 5168 | } |
fd295cb2 | 5169 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node); |
8d08fdba | 5170 | } |
8d08fdba | 5171 | |
42eaed49 | 5172 | complain &= ~tf_user; |
c8094d83 | 5173 | |
73b0fce8 KL |
5174 | if (DECL_TEMPLATE_TEMPLATE_PARM_P (template)) |
5175 | { | |
5176 | /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store | |
0cbd7506 | 5177 | template arguments */ |
73b0fce8 | 5178 | |
1899c3a4 | 5179 | tree parm; |
73b0fce8 KL |
5180 | tree arglist2; |
5181 | ||
73b0fce8 KL |
5182 | parmlist = DECL_INNERMOST_TEMPLATE_PARMS (template); |
5183 | ||
4f96ff63 KL |
5184 | /* Consider an example where a template template parameter declared as |
5185 | ||
5186 | template <class T, class U = std::allocator<T> > class TT | |
5187 | ||
c8094d83 MS |
5188 | The template parameter level of T and U are one level larger than |
5189 | of TT. To proper process the default argument of U, say when an | |
4f96ff63 | 5190 | instantiation `TT<int>' is seen, we need to build the full |
342cea95 KL |
5191 | arguments containing {int} as the innermost level. Outer levels, |
5192 | available when not appearing as default template argument, can be | |
5193 | obtained from `current_template_args ()'. | |
4f96ff63 | 5194 | |
342cea95 KL |
5195 | Suppose that TT is later substituted with std::vector. The above |
5196 | instantiation is `TT<int, std::allocator<T> >' with TT at | |
5197 | level 1, and T at level 2, while the template arguments at level 1 | |
5198 | becomes {std::vector} and the inner level 2 is {int}. */ | |
5199 | ||
5200 | if (current_template_parms) | |
4f96ff63 KL |
5201 | arglist = add_to_template_args (current_template_args (), arglist); |
5202 | ||
f9c244b8 | 5203 | arglist2 = coerce_template_parms (parmlist, arglist, template, |
3db45ab5 | 5204 | complain, |
e7e93965 MM |
5205 | /*require_all_args=*/true, |
5206 | /*use_default_args=*/true); | |
3e4a3562 | 5207 | if (arglist2 == error_mark_node |
544aef8c | 5208 | || (!uses_template_parms (arglist2) |
3e4a3562 | 5209 | && check_instantiated_args (template, arglist2, complain))) |
0cbd7506 | 5210 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node); |
73b0fce8 | 5211 | |
dac65501 | 5212 | parm = bind_template_template_parm (TREE_TYPE (template), arglist2); |
fd295cb2 | 5213 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, parm); |
73b0fce8 | 5214 | } |
c8094d83 | 5215 | else |
8d08fdba | 5216 | { |
36a117a5 | 5217 | tree template_type = TREE_TYPE (template); |
7ac7b28f | 5218 | tree gen_tmpl; |
36a117a5 MM |
5219 | tree type_decl; |
5220 | tree found = NULL_TREE; | |
5221 | int arg_depth; | |
5222 | int parm_depth; | |
dbfe2124 | 5223 | int is_partial_instantiation; |
830bfa74 | 5224 | |
7ac7b28f MM |
5225 | gen_tmpl = most_general_template (template); |
5226 | parmlist = DECL_TEMPLATE_PARMS (gen_tmpl); | |
36a117a5 MM |
5227 | parm_depth = TMPL_PARMS_DEPTH (parmlist); |
5228 | arg_depth = TMPL_ARGS_DEPTH (arglist); | |
5229 | ||
5230 | if (arg_depth == 1 && parm_depth > 1) | |
5231 | { | |
39c01e4c | 5232 | /* We've been given an incomplete set of template arguments. |
36a117a5 MM |
5233 | For example, given: |
5234 | ||
5235 | template <class T> struct S1 { | |
0cbd7506 | 5236 | template <class U> struct S2 {}; |
36a117a5 | 5237 | template <class U> struct S2<U*> {}; |
0cbd7506 | 5238 | }; |
c8094d83 | 5239 | |
36a117a5 MM |
5240 | we will be called with an ARGLIST of `U*', but the |
5241 | TEMPLATE will be `template <class T> template | |
5242 | <class U> struct S1<T>::S2'. We must fill in the missing | |
5243 | arguments. */ | |
c8094d83 | 5244 | arglist |
7ac7b28f MM |
5245 | = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (template)), |
5246 | arglist); | |
36a117a5 MM |
5247 | arg_depth = TMPL_ARGS_DEPTH (arglist); |
5248 | } | |
5566b478 | 5249 | |
41f5d4b1 | 5250 | /* Now we should have enough arguments. */ |
50bc768d | 5251 | gcc_assert (parm_depth == arg_depth); |
c8094d83 | 5252 | |
7ac7b28f MM |
5253 | /* From here on, we're only interested in the most general |
5254 | template. */ | |
5255 | template = gen_tmpl; | |
5256 | ||
36a117a5 MM |
5257 | /* Calculate the BOUND_ARGS. These will be the args that are |
5258 | actually tsubst'd into the definition to create the | |
5259 | instantiation. */ | |
5260 | if (parm_depth > 1) | |
830bfa74 MM |
5261 | { |
5262 | /* We have multiple levels of arguments to coerce, at once. */ | |
830bfa74 | 5263 | int i; |
e4a84209 | 5264 | int saved_depth = TMPL_ARGS_DEPTH (arglist); |
36a117a5 | 5265 | |
f31c0a32 | 5266 | tree bound_args = make_tree_vec (parm_depth); |
c8094d83 | 5267 | |
e4a84209 | 5268 | for (i = saved_depth, |
c8094d83 | 5269 | t = DECL_TEMPLATE_PARMS (template); |
e4a84209 | 5270 | i > 0 && t != NULL_TREE; |
830bfa74 | 5271 | --i, t = TREE_CHAIN (t)) |
e4a84209 | 5272 | { |
ee3071ef NS |
5273 | tree a = coerce_template_parms (TREE_VALUE (t), |
5274 | arglist, template, | |
3db45ab5 | 5275 | complain, |
e7e93965 MM |
5276 | /*require_all_args=*/true, |
5277 | /*use_default_args=*/true); | |
88e98cfe KL |
5278 | |
5279 | /* Don't process further if one of the levels fails. */ | |
5280 | if (a == error_mark_node) | |
5281 | { | |
5282 | /* Restore the ARGLIST to its full size. */ | |
5283 | TREE_VEC_LENGTH (arglist) = saved_depth; | |
5284 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node); | |
5285 | } | |
c8094d83 | 5286 | |
e4a84209 MM |
5287 | SET_TMPL_ARGS_LEVEL (bound_args, i, a); |
5288 | ||
5289 | /* We temporarily reduce the length of the ARGLIST so | |
5290 | that coerce_template_parms will see only the arguments | |
5291 | corresponding to the template parameters it is | |
5292 | examining. */ | |
5293 | TREE_VEC_LENGTH (arglist)--; | |
5294 | } | |
5295 | ||
5296 | /* Restore the ARGLIST to its full size. */ | |
5297 | TREE_VEC_LENGTH (arglist) = saved_depth; | |
5298 | ||
36a117a5 | 5299 | arglist = bound_args; |
830bfa74 MM |
5300 | } |
5301 | else | |
36a117a5 MM |
5302 | arglist |
5303 | = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist), | |
f9a7ae04 | 5304 | INNERMOST_TEMPLATE_ARGS (arglist), |
f9c244b8 | 5305 | template, |
3db45ab5 | 5306 | complain, |
e7e93965 MM |
5307 | /*require_all_args=*/true, |
5308 | /*use_default_args=*/true); | |
36a117a5 | 5309 | |
c353b8e3 | 5310 | if (arglist == error_mark_node) |
36a117a5 | 5311 | /* We were unable to bind the arguments. */ |
fd295cb2 | 5312 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node); |
5566b478 | 5313 | |
36a117a5 MM |
5314 | /* In the scope of a template class, explicit references to the |
5315 | template class refer to the type of the template, not any | |
5316 | instantiation of it. For example, in: | |
c8094d83 | 5317 | |
36a117a5 MM |
5318 | template <class T> class C { void f(C<T>); } |
5319 | ||
5320 | the `C<T>' is just the same as `C'. Outside of the | |
5321 | class, however, such a reference is an instantiation. */ | |
ed44da02 | 5322 | if (comp_template_args (TYPE_TI_ARGS (template_type), |
36a117a5 MM |
5323 | arglist)) |
5324 | { | |
5325 | found = template_type; | |
c8094d83 | 5326 | |
36a117a5 | 5327 | if (!entering_scope && PRIMARY_TEMPLATE_P (template)) |
5566b478 | 5328 | { |
36a117a5 | 5329 | tree ctx; |
c8094d83 MS |
5330 | |
5331 | for (ctx = current_class_type; | |
5f04800c KL |
5332 | ctx && TREE_CODE (ctx) != NAMESPACE_DECL; |
5333 | ctx = (TYPE_P (ctx) | |
5334 | ? TYPE_CONTEXT (ctx) | |
5335 | : DECL_CONTEXT (ctx))) | |
5336 | if (TYPE_P (ctx) && same_type_p (ctx, template_type)) | |
5337 | goto found_ctx; | |
c8094d83 | 5338 | |
6df5158a NS |
5339 | /* We're not in the scope of the class, so the |
5340 | TEMPLATE_TYPE is not the type we want after all. */ | |
5341 | found = NULL_TREE; | |
5342 | found_ctx:; | |
5566b478 MS |
5343 | } |
5344 | } | |
36a117a5 | 5345 | if (found) |
0cbd7506 | 5346 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, found); |
414ea4aa | 5347 | |
c7222c02 MM |
5348 | /* If we already have this specialization, return it. */ |
5349 | found = retrieve_specialization (template, arglist, | |
5350 | /*class_specializations_p=*/false); | |
5351 | if (found) | |
5352 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, found); | |
5566b478 | 5353 | |
dbfe2124 | 5354 | /* This type is a "partial instantiation" if any of the template |
ab097535 | 5355 | arguments still involve template parameters. Note that we set |
486e4077 MM |
5356 | IS_PARTIAL_INSTANTIATION for partial specializations as |
5357 | well. */ | |
dbfe2124 MM |
5358 | is_partial_instantiation = uses_template_parms (arglist); |
5359 | ||
c353b8e3 MM |
5360 | /* If the deduced arguments are invalid, then the binding |
5361 | failed. */ | |
5362 | if (!is_partial_instantiation | |
5363 | && check_instantiated_args (template, | |
5364 | INNERMOST_TEMPLATE_ARGS (arglist), | |
5365 | complain)) | |
5366 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node); | |
c8094d83 MS |
5367 | |
5368 | if (!is_partial_instantiation | |
3ebc5c52 MM |
5369 | && !PRIMARY_TEMPLATE_P (template) |
5370 | && TREE_CODE (CP_DECL_CONTEXT (template)) == NAMESPACE_DECL) | |
5371 | { | |
3ebc5c52 MM |
5372 | found = xref_tag_from_type (TREE_TYPE (template), |
5373 | DECL_NAME (template), | |
29ef83de | 5374 | /*tag_scope=*/ts_global); |
fd295cb2 | 5375 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, found); |
3ebc5c52 | 5376 | } |
c8094d83 | 5377 | |
95ee998c | 5378 | context = tsubst (DECL_CONTEXT (template), arglist, |
c2ea3a40 | 5379 | complain, in_decl); |
95ee998c MM |
5380 | if (!context) |
5381 | context = global_namespace; | |
5382 | ||
36a117a5 | 5383 | /* Create the type. */ |
ed44da02 MM |
5384 | if (TREE_CODE (template_type) == ENUMERAL_TYPE) |
5385 | { | |
dbfe2124 | 5386 | if (!is_partial_instantiation) |
92777ce4 NS |
5387 | { |
5388 | set_current_access_from_decl (TYPE_NAME (template_type)); | |
5389 | t = start_enum (TYPE_IDENTIFIER (template_type)); | |
5390 | } | |
ed44da02 | 5391 | else |
dbfe2124 | 5392 | /* We don't want to call start_enum for this type, since |
ed44da02 MM |
5393 | the values for the enumeration constants may involve |
5394 | template parameters. And, no one should be interested | |
5395 | in the enumeration constants for such a type. */ | |
5396 | t = make_node (ENUMERAL_TYPE); | |
5397 | } | |
5398 | else | |
5399 | { | |
33848bb0 | 5400 | t = make_aggr_type (TREE_CODE (template_type)); |
c8094d83 | 5401 | CLASSTYPE_DECLARED_CLASS (t) |
ed44da02 | 5402 | = CLASSTYPE_DECLARED_CLASS (template_type); |
ed44da02 | 5403 | SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t); |
f668f160 | 5404 | TYPE_FOR_JAVA (t) = TYPE_FOR_JAVA (template_type); |
ae673f14 JM |
5405 | |
5406 | /* A local class. Make sure the decl gets registered properly. */ | |
5407 | if (context == current_function_decl) | |
bd3d082e | 5408 | pushtag (DECL_NAME (template), t, /*tag_scope=*/ts_current); |
06d40de8 DG |
5409 | |
5410 | if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist)) | |
5411 | /* This instantiation is another name for the primary | |
5412 | template type. Set the TYPE_CANONICAL field | |
5413 | appropriately. */ | |
5414 | TYPE_CANONICAL (t) = template_type; | |
5415 | else if (any_template_arguments_need_structural_equality_p (arglist)) | |
5416 | /* Some of the template arguments require structural | |
5417 | equality testing, so this template class requires | |
5418 | structural equality testing. */ | |
5419 | SET_TYPE_STRUCTURAL_EQUALITY (t); | |
ed44da02 MM |
5420 | } |
5421 | ||
ae673f14 JM |
5422 | /* If we called start_enum or pushtag above, this information |
5423 | will already be set up. */ | |
ed44da02 MM |
5424 | if (!TYPE_NAME (t)) |
5425 | { | |
5426 | TYPE_CONTEXT (t) = FROB_CONTEXT (context); | |
c8094d83 | 5427 | |
9188c363 | 5428 | type_decl = create_implicit_typedef (DECL_NAME (template), t); |
ed44da02 | 5429 | DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t); |
9188c363 | 5430 | TYPE_STUB_DECL (t) = type_decl; |
c8094d83 | 5431 | DECL_SOURCE_LOCATION (type_decl) |
f31686a3 | 5432 | = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type)); |
ed44da02 MM |
5433 | } |
5434 | else | |
5435 | type_decl = TYPE_NAME (t); | |
36a117a5 | 5436 | |
cab7a9a3 KL |
5437 | TREE_PRIVATE (type_decl) |
5438 | = TREE_PRIVATE (TYPE_STUB_DECL (template_type)); | |
5439 | TREE_PROTECTED (type_decl) | |
5440 | = TREE_PROTECTED (TYPE_STUB_DECL (template_type)); | |
12af7ba3 MM |
5441 | DECL_IN_SYSTEM_HEADER (type_decl) |
5442 | = DECL_IN_SYSTEM_HEADER (template); | |
8d039470 MS |
5443 | if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type)) |
5444 | { | |
5445 | DECL_VISIBILITY_SPECIFIED (type_decl) = 1; | |
5446 | DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type); | |
5447 | } | |
cab7a9a3 | 5448 | |
9fbf56f7 MM |
5449 | /* Set up the template information. We have to figure out which |
5450 | template is the immediate parent if this is a full | |
5451 | instantiation. */ | |
5452 | if (parm_depth == 1 || is_partial_instantiation | |
5453 | || !PRIMARY_TEMPLATE_P (template)) | |
5454 | /* This case is easy; there are no member templates involved. */ | |
5455 | found = template; | |
5456 | else | |
5457 | { | |
ab097535 NS |
5458 | /* This is a full instantiation of a member template. Look |
5459 | for a partial instantiation of which this is an instance. */ | |
9fbf56f7 MM |
5460 | |
5461 | for (found = DECL_TEMPLATE_INSTANTIATIONS (template); | |
5462 | found; found = TREE_CHAIN (found)) | |
5463 | { | |
5464 | int success; | |
5465 | tree tmpl = CLASSTYPE_TI_TEMPLATE (TREE_VALUE (found)); | |
5466 | ||
5467 | /* We only want partial instantiations, here, not | |
5468 | specializations or full instantiations. */ | |
370af2d5 | 5469 | if (CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_VALUE (found)) |
9fbf56f7 MM |
5470 | || !uses_template_parms (TREE_VALUE (found))) |
5471 | continue; | |
5472 | ||
5473 | /* Temporarily reduce by one the number of levels in the | |
5474 | ARGLIST and in FOUND so as to avoid comparing the | |
5475 | last set of arguments. */ | |
5476 | TREE_VEC_LENGTH (arglist)--; | |
5477 | TREE_VEC_LENGTH (TREE_PURPOSE (found)) --; | |
5478 | ||
5479 | /* See if the arguments match. If they do, then TMPL is | |
5480 | the partial instantiation we want. */ | |
5481 | success = comp_template_args (TREE_PURPOSE (found), arglist); | |
5482 | ||
5483 | /* Restore the argument vectors to their full size. */ | |
5484 | TREE_VEC_LENGTH (arglist)++; | |
5485 | TREE_VEC_LENGTH (TREE_PURPOSE (found))++; | |
5486 | ||
5487 | if (success) | |
5488 | { | |
5489 | found = tmpl; | |
5490 | break; | |
5491 | } | |
5492 | } | |
5493 | ||
5494 | if (!found) | |
ab097535 NS |
5495 | { |
5496 | /* There was no partial instantiation. This happens | |
0cbd7506 MS |
5497 | where C<T> is a member template of A<T> and it's used |
5498 | in something like | |
c8094d83 | 5499 | |
0cbd7506 MS |
5500 | template <typename T> struct B { A<T>::C<int> m; }; |
5501 | B<float>; | |
c8094d83 | 5502 | |
0cbd7506 MS |
5503 | Create the partial instantiation. |
5504 | */ | |
5505 | TREE_VEC_LENGTH (arglist)--; | |
5506 | found = tsubst (template, arglist, complain, NULL_TREE); | |
5507 | TREE_VEC_LENGTH (arglist)++; | |
5508 | } | |
9fbf56f7 MM |
5509 | } |
5510 | ||
c8094d83 MS |
5511 | SET_TYPE_TEMPLATE_INFO (t, tree_cons (found, arglist, NULL_TREE)); |
5512 | DECL_TEMPLATE_INSTANTIATIONS (template) | |
5513 | = tree_cons (arglist, t, | |
dbfe2124 MM |
5514 | DECL_TEMPLATE_INSTANTIATIONS (template)); |
5515 | ||
c8094d83 | 5516 | if (TREE_CODE (t) == ENUMERAL_TYPE |
dbfe2124 | 5517 | && !is_partial_instantiation) |
61a127b3 MM |
5518 | /* Now that the type has been registered on the instantiations |
5519 | list, we set up the enumerators. Because the enumeration | |
5520 | constants may involve the enumeration type itself, we make | |
5521 | sure to register the type first, and then create the | |
5522 | constants. That way, doing tsubst_expr for the enumeration | |
5523 | constants won't result in recursive calls here; we'll find | |
5524 | the instantiation and exit above. */ | |
5525 | tsubst_enum (template_type, t, arglist); | |
dbfe2124 | 5526 | |
7813d14c | 5527 | if (is_partial_instantiation) |
077e7015 MM |
5528 | /* If the type makes use of template parameters, the |
5529 | code that generates debugging information will crash. */ | |
5530 | DECL_IGNORED_P (TYPE_STUB_DECL (t)) = 1; | |
8d08fdba | 5531 | |
b9e75696 JM |
5532 | /* Possibly limit visibility based on template args. */ |
5533 | TREE_PUBLIC (type_decl) = 1; | |
5534 | determine_visibility (type_decl); | |
5535 | ||
fd295cb2 | 5536 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t); |
36a117a5 | 5537 | } |
fd295cb2 | 5538 | timevar_pop (TV_NAME_LOOKUP); |
8d08fdba MS |
5539 | } |
5540 | \f | |
c8094d83 | 5541 | struct pair_fn_data |
8d08fdba | 5542 | { |
8dfaeb63 MM |
5543 | tree_fn_t fn; |
5544 | void *data; | |
0c58f841 | 5545 | struct pointer_set_t *visited; |
8dfaeb63 MM |
5546 | }; |
5547 | ||
5548 | /* Called from for_each_template_parm via walk_tree. */ | |
581d38d0 | 5549 | |
8dfaeb63 | 5550 | static tree |
350fae66 | 5551 | for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d) |
8dfaeb63 MM |
5552 | { |
5553 | tree t = *tp; | |
5554 | struct pair_fn_data *pfd = (struct pair_fn_data *) d; | |
5555 | tree_fn_t fn = pfd->fn; | |
5556 | void *data = pfd->data; | |
4f2c9d7e | 5557 | |
2f939d94 | 5558 | if (TYPE_P (t) |
4f2c9d7e | 5559 | && for_each_template_parm (TYPE_CONTEXT (t), fn, data, pfd->visited)) |
8dfaeb63 | 5560 | return error_mark_node; |
581d38d0 | 5561 | |
8d08fdba MS |
5562 | switch (TREE_CODE (t)) |
5563 | { | |
8d08fdba | 5564 | case RECORD_TYPE: |
9076e292 | 5565 | if (TYPE_PTRMEMFUNC_P (t)) |
8dfaeb63 | 5566 | break; |
ed44da02 MM |
5567 | /* Fall through. */ |
5568 | ||
8d08fdba | 5569 | case UNION_TYPE: |
ed44da02 | 5570 | case ENUMERAL_TYPE: |
8dfaeb63 MM |
5571 | if (!TYPE_TEMPLATE_INFO (t)) |
5572 | *walk_subtrees = 0; | |
5573 | else if (for_each_template_parm (TREE_VALUE (TYPE_TEMPLATE_INFO (t)), | |
4f2c9d7e | 5574 | fn, data, pfd->visited)) |
8dfaeb63 MM |
5575 | return error_mark_node; |
5576 | break; | |
5577 | ||
8f6e6bf3 EB |
5578 | case INTEGER_TYPE: |
5579 | if (for_each_template_parm (TYPE_MIN_VALUE (t), | |
5580 | fn, data, pfd->visited) | |
5581 | || for_each_template_parm (TYPE_MAX_VALUE (t), | |
5582 | fn, data, pfd->visited)) | |
5583 | return error_mark_node; | |
5584 | break; | |
5585 | ||
588c2d1c | 5586 | case METHOD_TYPE: |
8dfaeb63 MM |
5587 | /* Since we're not going to walk subtrees, we have to do this |
5588 | explicitly here. */ | |
4f2c9d7e MM |
5589 | if (for_each_template_parm (TYPE_METHOD_BASETYPE (t), fn, data, |
5590 | pfd->visited)) | |
8dfaeb63 | 5591 | return error_mark_node; |
4890c2f4 | 5592 | /* Fall through. */ |
588c2d1c MM |
5593 | |
5594 | case FUNCTION_TYPE: | |
8dfaeb63 | 5595 | /* Check the return type. */ |
4f2c9d7e | 5596 | if (for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited)) |
8dfaeb63 MM |
5597 | return error_mark_node; |
5598 | ||
588c2d1c MM |
5599 | /* Check the parameter types. Since default arguments are not |
5600 | instantiated until they are needed, the TYPE_ARG_TYPES may | |
5601 | contain expressions that involve template parameters. But, | |
5602 | no-one should be looking at them yet. And, once they're | |
5603 | instantiated, they don't contain template parameters, so | |
5604 | there's no point in looking at them then, either. */ | |
5605 | { | |
5606 | tree parm; | |
5607 | ||
5608 | for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm)) | |
4f2c9d7e MM |
5609 | if (for_each_template_parm (TREE_VALUE (parm), fn, data, |
5610 | pfd->visited)) | |
8dfaeb63 | 5611 | return error_mark_node; |
5566b478 | 5612 | |
8dfaeb63 MM |
5613 | /* Since we've already handled the TYPE_ARG_TYPES, we don't |
5614 | want walk_tree walking into them itself. */ | |
5615 | *walk_subtrees = 0; | |
5616 | } | |
5617 | break; | |
3ac3d9ea | 5618 | |
a723baf1 | 5619 | case TYPEOF_TYPE: |
c8094d83 | 5620 | if (for_each_template_parm (TYPE_FIELDS (t), fn, data, |
a723baf1 MM |
5621 | pfd->visited)) |
5622 | return error_mark_node; | |
5623 | break; | |
5624 | ||
8d08fdba | 5625 | case FUNCTION_DECL: |
5566b478 | 5626 | case VAR_DECL: |
5566b478 | 5627 | if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t) |
4f2c9d7e MM |
5628 | && for_each_template_parm (DECL_TI_ARGS (t), fn, data, |
5629 | pfd->visited)) | |
8dfaeb63 MM |
5630 | return error_mark_node; |
5631 | /* Fall through. */ | |
5632 | ||
8d08fdba | 5633 | case PARM_DECL: |
a723baf1 MM |
5634 | case CONST_DECL: |
5635 | if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t) | |
5636 | && for_each_template_parm (DECL_INITIAL (t), fn, data, | |
5637 | pfd->visited)) | |
5638 | return error_mark_node; | |
c8094d83 | 5639 | if (DECL_CONTEXT (t) |
4f2c9d7e MM |
5640 | && for_each_template_parm (DECL_CONTEXT (t), fn, data, |
5641 | pfd->visited)) | |
8dfaeb63 MM |
5642 | return error_mark_node; |
5643 | break; | |
8d08fdba | 5644 | |
a1281f45 | 5645 | case BOUND_TEMPLATE_TEMPLATE_PARM: |
744fac59 | 5646 | /* Record template parameters such as `T' inside `TT<T>'. */ |
4f2c9d7e | 5647 | if (for_each_template_parm (TYPE_TI_ARGS (t), fn, data, pfd->visited)) |
8dfaeb63 MM |
5648 | return error_mark_node; |
5649 | /* Fall through. */ | |
5650 | ||
a1281f45 | 5651 | case TEMPLATE_TEMPLATE_PARM: |
744fac59 | 5652 | case TEMPLATE_TYPE_PARM: |
f84b4be9 | 5653 | case TEMPLATE_PARM_INDEX: |
8dfaeb63 MM |
5654 | if (fn && (*fn)(t, data)) |
5655 | return error_mark_node; | |
5656 | else if (!fn) | |
5657 | return error_mark_node; | |
5658 | break; | |
5156628f | 5659 | |
8dfaeb63 | 5660 | case TEMPLATE_DECL: |
f4f206f4 | 5661 | /* A template template parameter is encountered. */ |
8dfaeb63 | 5662 | if (DECL_TEMPLATE_TEMPLATE_PARM_P (t) |
4f2c9d7e | 5663 | && for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited)) |
8dfaeb63 | 5664 | return error_mark_node; |
db5ae43f | 5665 | |
8dfaeb63 MM |
5666 | /* Already substituted template template parameter */ |
5667 | *walk_subtrees = 0; | |
5668 | break; | |
b8865407 | 5669 | |
4699c561 | 5670 | case TYPENAME_TYPE: |
c8094d83 | 5671 | if (!fn |
4f2c9d7e MM |
5672 | || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t), fn, |
5673 | data, pfd->visited)) | |
8dfaeb63 MM |
5674 | return error_mark_node; |
5675 | break; | |
4699c561 | 5676 | |
8dfaeb63 MM |
5677 | case CONSTRUCTOR: |
5678 | if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t)) | |
5679 | && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE | |
4f2c9d7e MM |
5680 | (TREE_TYPE (t)), fn, data, |
5681 | pfd->visited)) | |
8dfaeb63 MM |
5682 | return error_mark_node; |
5683 | break; | |
c8094d83 | 5684 | |
b8865407 MM |
5685 | case INDIRECT_REF: |
5686 | case COMPONENT_REF: | |
4699c561 | 5687 | /* If there's no type, then this thing must be some expression |
b8865407 | 5688 | involving template parameters. */ |
4699c561 | 5689 | if (!fn && !TREE_TYPE (t)) |
8dfaeb63 MM |
5690 | return error_mark_node; |
5691 | break; | |
b8865407 | 5692 | |
42976354 BK |
5693 | case MODOP_EXPR: |
5694 | case CAST_EXPR: | |
5695 | case REINTERPRET_CAST_EXPR: | |
5696 | case CONST_CAST_EXPR: | |
5697 | case STATIC_CAST_EXPR: | |
5698 | case DYNAMIC_CAST_EXPR: | |
42976354 BK |
5699 | case ARROW_EXPR: |
5700 | case DOTSTAR_EXPR: | |
5701 | case TYPEID_EXPR: | |
40242ccf | 5702 | case PSEUDO_DTOR_EXPR: |
b8865407 | 5703 | if (!fn) |
8dfaeb63 MM |
5704 | return error_mark_node; |
5705 | break; | |
abff8e06 | 5706 | |
8d08fdba | 5707 | default: |
8dfaeb63 | 5708 | break; |
8d08fdba | 5709 | } |
8dfaeb63 MM |
5710 | |
5711 | /* We didn't find any template parameters we liked. */ | |
5712 | return NULL_TREE; | |
5713 | } | |
5714 | ||
c8094d83 MS |
5715 | /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM, |
5716 | BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T, | |
a1281f45 | 5717 | call FN with the parameter and the DATA. |
838dfd8a | 5718 | If FN returns nonzero, the iteration is terminated, and |
8dfaeb63 | 5719 | for_each_template_parm returns 1. Otherwise, the iteration |
838dfd8a | 5720 | continues. If FN never returns a nonzero value, the value |
8dfaeb63 MM |
5721 | returned by for_each_template_parm is 0. If FN is NULL, it is |
5722 | considered to be the function which always returns 1. */ | |
5723 | ||
5724 | static int | |
0c58f841 MA |
5725 | for_each_template_parm (tree t, tree_fn_t fn, void* data, |
5726 | struct pointer_set_t *visited) | |
8dfaeb63 MM |
5727 | { |
5728 | struct pair_fn_data pfd; | |
ad2ae3b2 | 5729 | int result; |
8dfaeb63 MM |
5730 | |
5731 | /* Set up. */ | |
5732 | pfd.fn = fn; | |
5733 | pfd.data = data; | |
5734 | ||
4890c2f4 MM |
5735 | /* Walk the tree. (Conceptually, we would like to walk without |
5736 | duplicates, but for_each_template_parm_r recursively calls | |
5737 | for_each_template_parm, so we would need to reorganize a fair | |
4f2c9d7e MM |
5738 | bit to use walk_tree_without_duplicates, so we keep our own |
5739 | visited list.) */ | |
5740 | if (visited) | |
5741 | pfd.visited = visited; | |
5742 | else | |
0c58f841 | 5743 | pfd.visited = pointer_set_create (); |
c8094d83 MS |
5744 | result = walk_tree (&t, |
5745 | for_each_template_parm_r, | |
ad2ae3b2 | 5746 | &pfd, |
c1e39976 | 5747 | pfd.visited) != NULL_TREE; |
ad2ae3b2 MM |
5748 | |
5749 | /* Clean up. */ | |
5750 | if (!visited) | |
0c58f841 MA |
5751 | { |
5752 | pointer_set_destroy (pfd.visited); | |
5753 | pfd.visited = 0; | |
5754 | } | |
ad2ae3b2 MM |
5755 | |
5756 | return result; | |
8d08fdba MS |
5757 | } |
5758 | ||
d43f603d KL |
5759 | /* Returns true if T depends on any template parameter. */ |
5760 | ||
050367a3 | 5761 | int |
3a978d72 | 5762 | uses_template_parms (tree t) |
050367a3 | 5763 | { |
c353b8e3 MM |
5764 | bool dependent_p; |
5765 | int saved_processing_template_decl; | |
5766 | ||
5767 | saved_processing_template_decl = processing_template_decl; | |
5768 | if (!saved_processing_template_decl) | |
5769 | processing_template_decl = 1; | |
5770 | if (TYPE_P (t)) | |
5771 | dependent_p = dependent_type_p (t); | |
5772 | else if (TREE_CODE (t) == TREE_VEC) | |
5773 | dependent_p = any_dependent_template_arguments_p (t); | |
5774 | else if (TREE_CODE (t) == TREE_LIST) | |
5775 | dependent_p = (uses_template_parms (TREE_VALUE (t)) | |
5776 | || uses_template_parms (TREE_CHAIN (t))); | |
3ce5fa4f NS |
5777 | else if (TREE_CODE (t) == TYPE_DECL) |
5778 | dependent_p = dependent_type_p (TREE_TYPE (t)); | |
c8094d83 MS |
5779 | else if (DECL_P (t) |
5780 | || EXPR_P (t) | |
c353b8e3 MM |
5781 | || TREE_CODE (t) == TEMPLATE_PARM_INDEX |
5782 | || TREE_CODE (t) == OVERLOAD | |
5783 | || TREE_CODE (t) == BASELINK | |
b207d6e2 | 5784 | || TREE_CODE (t) == IDENTIFIER_NODE |
6615c446 | 5785 | || CONSTANT_CLASS_P (t)) |
c353b8e3 MM |
5786 | dependent_p = (type_dependent_expression_p (t) |
5787 | || value_dependent_expression_p (t)); | |
315fb5db NS |
5788 | else |
5789 | { | |
5790 | gcc_assert (t == error_mark_node); | |
5791 | dependent_p = false; | |
5792 | } | |
c8094d83 | 5793 | |
c353b8e3 MM |
5794 | processing_template_decl = saved_processing_template_decl; |
5795 | ||
5796 | return dependent_p; | |
050367a3 MM |
5797 | } |
5798 | ||
d43f603d KL |
5799 | /* Returns true if T depends on any template parameter with level LEVEL. */ |
5800 | ||
5801 | int | |
5802 | uses_template_parms_level (tree t, int level) | |
5803 | { | |
5804 | return for_each_template_parm (t, template_parm_this_level_p, &level, NULL); | |
5805 | } | |
5806 | ||
27fafc8d | 5807 | static int tinst_depth; |
e9f32eb5 | 5808 | extern int max_tinst_depth; |
5566b478 | 5809 | #ifdef GATHER_STATISTICS |
27fafc8d | 5810 | int depth_reached; |
5566b478 | 5811 | #endif |
8dfaeb63 MM |
5812 | static int tinst_level_tick; |
5813 | static int last_template_error_tick; | |
8d08fdba | 5814 | |
3ae18eaf JM |
5815 | /* We're starting to instantiate D; record the template instantiation context |
5816 | for diagnostics and to restore it later. */ | |
5817 | ||
aa9d8196 | 5818 | static int |
3a978d72 | 5819 | push_tinst_level (tree d) |
8d08fdba | 5820 | { |
3ae18eaf | 5821 | tree new; |
8d08fdba | 5822 | |
7215f9a0 MS |
5823 | if (tinst_depth >= max_tinst_depth) |
5824 | { | |
8adf5b5e JM |
5825 | /* If the instantiation in question still has unbound template parms, |
5826 | we don't really care if we can't instantiate it, so just return. | |
0cbd7506 | 5827 | This happens with base instantiation for implicit `typename'. */ |
8adf5b5e JM |
5828 | if (uses_template_parms (d)) |
5829 | return 0; | |
5830 | ||
1139b3d8 | 5831 | last_template_error_tick = tinst_level_tick; |
0f51ccfc | 5832 | error ("template instantiation depth exceeds maximum of %d (use " |
0cbd7506 | 5833 | "-ftemplate-depth-NN to increase the maximum) instantiating %qD", |
b4f4233d | 5834 | max_tinst_depth, d); |
5566b478 | 5835 | |
cb753e49 | 5836 | print_instantiation_context (); |
5566b478 | 5837 | |
7215f9a0 MS |
5838 | return 0; |
5839 | } | |
5840 | ||
12af7ba3 MM |
5841 | new = make_node (TINST_LEVEL); |
5842 | TINST_DECL (new) = d; | |
5843 | TINST_LOCATION (new) = input_location; | |
5844 | TINST_IN_SYSTEM_HEADER_P (new) = in_system_header; | |
3ae18eaf | 5845 | TREE_CHAIN (new) = current_tinst_level; |
8d08fdba | 5846 | current_tinst_level = new; |
5566b478 | 5847 | |
7215f9a0 | 5848 | ++tinst_depth; |
5566b478 MS |
5849 | #ifdef GATHER_STATISTICS |
5850 | if (tinst_depth > depth_reached) | |
5851 | depth_reached = tinst_depth; | |
5852 | #endif | |
5853 | ||
27fafc8d | 5854 | ++tinst_level_tick; |
7215f9a0 | 5855 | return 1; |
8d08fdba MS |
5856 | } |
5857 | ||
3ae18eaf JM |
5858 | /* We're done instantiating this template; return to the instantiation |
5859 | context. */ | |
5860 | ||
aa9d8196 | 5861 | static void |
3a978d72 | 5862 | pop_tinst_level (void) |
8d08fdba | 5863 | { |
3ae18eaf | 5864 | tree old = current_tinst_level; |
8d08fdba | 5865 | |
ae58fa02 MM |
5866 | /* Restore the filename and line number stashed away when we started |
5867 | this instantiation. */ | |
406d77a4 | 5868 | input_location = TINST_LOCATION (old); |
12af7ba3 | 5869 | in_system_header = TINST_IN_SYSTEM_HEADER_P (old); |
3ae18eaf | 5870 | current_tinst_level = TREE_CHAIN (old); |
7215f9a0 | 5871 | --tinst_depth; |
27fafc8d | 5872 | ++tinst_level_tick; |
8d08fdba MS |
5873 | } |
5874 | ||
3ae18eaf JM |
5875 | /* We're instantiating a deferred template; restore the template |
5876 | instantiation context in which the instantiation was requested, which | |
5877 | is one step out from LEVEL. */ | |
5878 | ||
5879 | static void | |
3a978d72 | 5880 | reopen_tinst_level (tree level) |
3ae18eaf JM |
5881 | { |
5882 | tree t; | |
5883 | ||
5884 | tinst_depth = 0; | |
5885 | for (t = level; t; t = TREE_CHAIN (t)) | |
5886 | ++tinst_depth; | |
5887 | ||
5888 | current_tinst_level = level; | |
5889 | pop_tinst_level (); | |
5890 | } | |
5891 | ||
f84b4be9 JM |
5892 | /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the |
5893 | vector of template arguments, as for tsubst. | |
5894 | ||
dc957d14 | 5895 | Returns an appropriate tsubst'd friend declaration. */ |
f84b4be9 JM |
5896 | |
5897 | static tree | |
3a978d72 | 5898 | tsubst_friend_function (tree decl, tree args) |
f84b4be9 JM |
5899 | { |
5900 | tree new_friend; | |
27fafc8d | 5901 | |
c8094d83 | 5902 | if (TREE_CODE (decl) == FUNCTION_DECL |
f84b4be9 JM |
5903 | && DECL_TEMPLATE_INSTANTIATION (decl) |
5904 | && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL) | |
5905 | /* This was a friend declared with an explicit template | |
5906 | argument list, e.g.: | |
c8094d83 | 5907 | |
f84b4be9 | 5908 | friend void f<>(T); |
c8094d83 | 5909 | |
f84b4be9 JM |
5910 | to indicate that f was a template instantiation, not a new |
5911 | function declaration. Now, we have to figure out what | |
5912 | instantiation of what template. */ | |
5913 | { | |
76e57b45 | 5914 | tree template_id, arglist, fns; |
f84b4be9 JM |
5915 | tree new_args; |
5916 | tree tmpl; | |
ed2fa432 | 5917 | tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type)); |
c8094d83 | 5918 | |
76e57b45 | 5919 | /* Friend functions are looked up in the containing namespace scope. |
0cbd7506 MS |
5920 | We must enter that scope, to avoid finding member functions of the |
5921 | current cless with same name. */ | |
76e57b45 NS |
5922 | push_nested_namespace (ns); |
5923 | fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args, | |
015c2c66 MM |
5924 | tf_warning_or_error, NULL_TREE, |
5925 | /*integral_constant_expression_p=*/false); | |
76e57b45 NS |
5926 | pop_nested_namespace (ns); |
5927 | arglist = tsubst (DECL_TI_ARGS (decl), args, | |
23fca1f5 | 5928 | tf_warning_or_error, NULL_TREE); |
76e57b45 | 5929 | template_id = lookup_template_function (fns, arglist); |
c8094d83 | 5930 | |
23fca1f5 | 5931 | new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE); |
36a117a5 | 5932 | tmpl = determine_specialization (template_id, new_friend, |
c8094d83 | 5933 | &new_args, |
5fe7b654 | 5934 | /*need_member_template=*/0, |
29a1da1c MM |
5935 | TREE_VEC_LENGTH (args), |
5936 | tsk_none); | |
6e432b31 | 5937 | return instantiate_template (tmpl, new_args, tf_error); |
f84b4be9 | 5938 | } |
36a117a5 | 5939 | |
23fca1f5 | 5940 | new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE); |
c8094d83 | 5941 | |
36a117a5 | 5942 | /* The NEW_FRIEND will look like an instantiation, to the |
f84b4be9 JM |
5943 | compiler, but is not an instantiation from the point of view of |
5944 | the language. For example, we might have had: | |
c8094d83 | 5945 | |
f84b4be9 JM |
5946 | template <class T> struct S { |
5947 | template <class U> friend void f(T, U); | |
5948 | }; | |
c8094d83 | 5949 | |
f84b4be9 JM |
5950 | Then, in S<int>, template <class U> void f(int, U) is not an |
5951 | instantiation of anything. */ | |
ac2b3222 AP |
5952 | if (new_friend == error_mark_node) |
5953 | return error_mark_node; | |
c8094d83 | 5954 | |
f84b4be9 JM |
5955 | DECL_USE_TEMPLATE (new_friend) = 0; |
5956 | if (TREE_CODE (decl) == TEMPLATE_DECL) | |
655dc6ee JM |
5957 | { |
5958 | DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0; | |
5959 | DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend)) | |
5960 | = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl)); | |
5961 | } | |
36a117a5 | 5962 | |
92643fea MM |
5963 | /* The mangled name for the NEW_FRIEND is incorrect. The function |
5964 | is not a template instantiation and should not be mangled like | |
5965 | one. Therefore, we forget the mangling here; we'll recompute it | |
5966 | later if we need it. */ | |
36a117a5 MM |
5967 | if (TREE_CODE (new_friend) != TEMPLATE_DECL) |
5968 | { | |
19e7881c | 5969 | SET_DECL_RTL (new_friend, NULL_RTX); |
92643fea | 5970 | SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE); |
36a117a5 | 5971 | } |
c8094d83 | 5972 | |
6eb3bb27 | 5973 | if (DECL_NAMESPACE_SCOPE_P (new_friend)) |
f84b4be9 | 5974 | { |
36a117a5 | 5975 | tree old_decl; |
fbf1c34b MM |
5976 | tree new_friend_template_info; |
5977 | tree new_friend_result_template_info; | |
92da7074 | 5978 | tree ns; |
fbf1c34b MM |
5979 | int new_friend_is_defn; |
5980 | ||
5981 | /* We must save some information from NEW_FRIEND before calling | |
5982 | duplicate decls since that function will free NEW_FRIEND if | |
5983 | possible. */ | |
5984 | new_friend_template_info = DECL_TEMPLATE_INFO (new_friend); | |
5bd61841 | 5985 | new_friend_is_defn = |
c8094d83 | 5986 | (DECL_INITIAL (DECL_TEMPLATE_RESULT |
5bd61841 MM |
5987 | (template_for_substitution (new_friend))) |
5988 | != NULL_TREE); | |
f84b4be9 | 5989 | if (TREE_CODE (new_friend) == TEMPLATE_DECL) |
fbf1c34b MM |
5990 | { |
5991 | /* This declaration is a `primary' template. */ | |
5992 | DECL_PRIMARY_TEMPLATE (new_friend) = new_friend; | |
c8094d83 | 5993 | |
fbf1c34b | 5994 | new_friend_result_template_info |
17aec3eb | 5995 | = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend)); |
fbf1c34b MM |
5996 | } |
5997 | else | |
5bd61841 | 5998 | new_friend_result_template_info = NULL_TREE; |
36a117a5 | 5999 | |
b01e6d2b JM |
6000 | /* Make the init_value nonzero so pushdecl knows this is a defn. */ |
6001 | if (new_friend_is_defn) | |
6002 | DECL_INITIAL (new_friend) = error_mark_node; | |
6003 | ||
1c227897 MM |
6004 | /* Inside pushdecl_namespace_level, we will push into the |
6005 | current namespace. However, the friend function should go | |
c6002625 | 6006 | into the namespace of the template. */ |
92da7074 ML |
6007 | ns = decl_namespace_context (new_friend); |
6008 | push_nested_namespace (ns); | |
d63d5d0c | 6009 | old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true); |
92da7074 | 6010 | pop_nested_namespace (ns); |
36a117a5 | 6011 | |
0014c247 VR |
6012 | if (old_decl == error_mark_node) |
6013 | return error_mark_node; | |
6014 | ||
36a117a5 MM |
6015 | if (old_decl != new_friend) |
6016 | { | |
6017 | /* This new friend declaration matched an existing | |
6018 | declaration. For example, given: | |
6019 | ||
6020 | template <class T> void f(T); | |
c8094d83 MS |
6021 | template <class U> class C { |
6022 | template <class T> friend void f(T) {} | |
36a117a5 MM |
6023 | }; |
6024 | ||
6025 | the friend declaration actually provides the definition | |
6026 | of `f', once C has been instantiated for some type. So, | |
6027 | old_decl will be the out-of-class template declaration, | |
6028 | while new_friend is the in-class definition. | |
6029 | ||
6030 | But, if `f' was called before this point, the | |
6031 | instantiation of `f' will have DECL_TI_ARGS corresponding | |
6032 | to `T' but not to `U', references to which might appear | |
6033 | in the definition of `f'. Previously, the most general | |
6034 | template for an instantiation of `f' was the out-of-class | |
6035 | version; now it is the in-class version. Therefore, we | |
6036 | run through all specialization of `f', adding to their | |
6037 | DECL_TI_ARGS appropriately. In particular, they need a | |
6038 | new set of outer arguments, corresponding to the | |
c8094d83 | 6039 | arguments for this class instantiation. |
36a117a5 MM |
6040 | |
6041 | The same situation can arise with something like this: | |
6042 | ||
6043 | friend void f(int); | |
c8094d83 | 6044 | template <class T> class C { |
0cbd7506 MS |
6045 | friend void f(T) {} |
6046 | }; | |
36a117a5 MM |
6047 | |
6048 | when `C<int>' is instantiated. Now, `f(int)' is defined | |
6049 | in the class. */ | |
6050 | ||
fbf1c34b MM |
6051 | if (!new_friend_is_defn) |
6052 | /* On the other hand, if the in-class declaration does | |
6053 | *not* provide a definition, then we don't want to alter | |
6054 | existing definitions. We can just leave everything | |
6055 | alone. */ | |
36a117a5 | 6056 | ; |
fbf1c34b | 6057 | else |
36a117a5 | 6058 | { |
fbf1c34b MM |
6059 | /* Overwrite whatever template info was there before, if |
6060 | any, with the new template information pertaining to | |
6061 | the declaration. */ | |
6062 | DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info; | |
6063 | ||
6064 | if (TREE_CODE (old_decl) != TEMPLATE_DECL) | |
8d83f792 MM |
6065 | reregister_specialization (new_friend, |
6066 | most_general_template (old_decl), | |
6067 | old_decl); | |
c8094d83 | 6068 | else |
36a117a5 | 6069 | { |
fbf1c34b MM |
6070 | tree t; |
6071 | tree new_friend_args; | |
6072 | ||
c8094d83 | 6073 | DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl)) |
fbf1c34b | 6074 | = new_friend_result_template_info; |
c8094d83 | 6075 | |
fbf1c34b | 6076 | new_friend_args = TI_ARGS (new_friend_template_info); |
c8094d83 | 6077 | for (t = DECL_TEMPLATE_SPECIALIZATIONS (old_decl); |
fbf1c34b MM |
6078 | t != NULL_TREE; |
6079 | t = TREE_CHAIN (t)) | |
6080 | { | |
6081 | tree spec = TREE_VALUE (t); | |
c8094d83 MS |
6082 | |
6083 | DECL_TI_ARGS (spec) | |
fbf1c34b MM |
6084 | = add_outermost_template_args (new_friend_args, |
6085 | DECL_TI_ARGS (spec)); | |
fbf1c34b MM |
6086 | } |
6087 | ||
6088 | /* Now, since specializations are always supposed to | |
6089 | hang off of the most general template, we must move | |
6090 | them. */ | |
6091 | t = most_general_template (old_decl); | |
6092 | if (t != old_decl) | |
6093 | { | |
6094 | DECL_TEMPLATE_SPECIALIZATIONS (t) | |
6095 | = chainon (DECL_TEMPLATE_SPECIALIZATIONS (t), | |
6096 | DECL_TEMPLATE_SPECIALIZATIONS (old_decl)); | |
6097 | DECL_TEMPLATE_SPECIALIZATIONS (old_decl) = NULL_TREE; | |
6098 | } | |
36a117a5 MM |
6099 | } |
6100 | } | |
6101 | ||
6102 | /* The information from NEW_FRIEND has been merged into OLD_DECL | |
6103 | by duplicate_decls. */ | |
6104 | new_friend = old_decl; | |
6105 | } | |
f84b4be9 | 6106 | } |
6e432b31 | 6107 | else |
f84b4be9 | 6108 | { |
6e432b31 KL |
6109 | tree context = DECL_CONTEXT (new_friend); |
6110 | bool dependent_p; | |
6111 | ||
6112 | /* In the code | |
6113 | template <class T> class C { | |
6114 | template <class U> friend void C1<U>::f (); // case 1 | |
6115 | friend void C2<T>::f (); // case 2 | |
6116 | }; | |
6117 | we only need to make sure CONTEXT is a complete type for | |
6118 | case 2. To distinguish between the two cases, we note that | |
6119 | CONTEXT of case 1 remains dependent type after tsubst while | |
6120 | this isn't true for case 2. */ | |
6121 | ++processing_template_decl; | |
6122 | dependent_p = dependent_type_p (context); | |
6123 | --processing_template_decl; | |
6124 | ||
6125 | if (!dependent_p | |
6126 | && !complete_type_or_else (context, NULL_TREE)) | |
6127 | return error_mark_node; | |
6128 | ||
6129 | if (COMPLETE_TYPE_P (context)) | |
6130 | { | |
6131 | /* Check to see that the declaration is really present, and, | |
6132 | possibly obtain an improved declaration. */ | |
6133 | tree fn = check_classfn (context, | |
6134 | new_friend, NULL_TREE); | |
6135 | ||
6136 | if (fn) | |
6137 | new_friend = fn; | |
6138 | } | |
f84b4be9 JM |
6139 | } |
6140 | ||
6141 | return new_friend; | |
6142 | } | |
6143 | ||
1aed5355 MM |
6144 | /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of |
6145 | template arguments, as for tsubst. | |
6757edfe | 6146 | |
dc957d14 | 6147 | Returns an appropriate tsubst'd friend type or error_mark_node on |
4b054b80 | 6148 | failure. */ |
6757edfe MM |
6149 | |
6150 | static tree | |
3a978d72 | 6151 | tsubst_friend_class (tree friend_tmpl, tree args) |
6757edfe | 6152 | { |
1aed5355 | 6153 | tree friend_type; |
25aab5d0 | 6154 | tree tmpl; |
3e0ec82f | 6155 | tree context; |
6757edfe | 6156 | |
3e0ec82f MM |
6157 | context = DECL_CONTEXT (friend_tmpl); |
6158 | ||
6159 | if (context) | |
77adef84 | 6160 | { |
3e0ec82f MM |
6161 | if (TREE_CODE (context) == NAMESPACE_DECL) |
6162 | push_nested_namespace (context); | |
6163 | else | |
c8094d83 | 6164 | push_nested_class (tsubst (context, args, tf_none, NULL_TREE)); |
3e0ec82f | 6165 | } |
25aab5d0 | 6166 | |
105d72c5 MM |
6167 | /* Look for a class template declaration. We look for hidden names |
6168 | because two friend declarations of the same template are the | |
6169 | same. For example, in: | |
6170 | ||
6171 | struct A { | |
6172 | template <typename> friend class F; | |
6173 | }; | |
6174 | template <typename> struct B { | |
6175 | template <typename> friend class F; | |
6176 | }; | |
6177 | ||
6178 | both F templates are the same. */ | |
6179 | tmpl = lookup_name_real (DECL_NAME (friend_tmpl), 0, 0, | |
6180 | /*block_p=*/true, 0, | |
6181 | LOOKUP_COMPLAIN | LOOKUP_HIDDEN); | |
25aab5d0 | 6182 | |
3e0ec82f MM |
6183 | /* But, if we don't find one, it might be because we're in a |
6184 | situation like this: | |
77adef84 | 6185 | |
3e0ec82f MM |
6186 | template <class T> |
6187 | struct S { | |
6188 | template <class U> | |
6189 | friend struct S; | |
6190 | }; | |
6191 | ||
6192 | Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL | |
6193 | for `S<int>', not the TEMPLATE_DECL. */ | |
6194 | if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl)) | |
6195 | { | |
10e6657a | 6196 | tmpl = lookup_name_prefer_type (DECL_NAME (friend_tmpl), 1); |
3e0ec82f | 6197 | tmpl = maybe_get_template_decl_from_type_decl (tmpl); |
25aab5d0 | 6198 | } |
6757edfe | 6199 | |
25aab5d0 | 6200 | if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl)) |
6757edfe MM |
6201 | { |
6202 | /* The friend template has already been declared. Just | |
36a117a5 MM |
6203 | check to see that the declarations match, and install any new |
6204 | default parameters. We must tsubst the default parameters, | |
6205 | of course. We only need the innermost template parameters | |
6206 | because that is all that redeclare_class_template will look | |
6207 | at. */ | |
3e0ec82f MM |
6208 | if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl)) |
6209 | > TMPL_ARGS_DEPTH (args)) | |
6210 | { | |
6211 | tree parms; | |
6212 | parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl), | |
23fca1f5 | 6213 | args, tf_warning_or_error); |
3e0ec82f MM |
6214 | redeclare_class_template (TREE_TYPE (tmpl), parms); |
6215 | } | |
6216 | ||
6757edfe MM |
6217 | friend_type = TREE_TYPE (tmpl); |
6218 | } | |
6219 | else | |
6220 | { | |
6221 | /* The friend template has not already been declared. In this | |
6222 | case, the instantiation of the template class will cause the | |
6223 | injection of this template into the global scope. */ | |
23fca1f5 | 6224 | tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE); |
dca56f77 VR |
6225 | if (tmpl == error_mark_node) |
6226 | return error_mark_node; | |
6757edfe MM |
6227 | |
6228 | /* The new TMPL is not an instantiation of anything, so we | |
0cbd7506 | 6229 | forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for |
6757edfe MM |
6230 | the new type because that is supposed to be the corresponding |
6231 | template decl, i.e., TMPL. */ | |
6232 | DECL_USE_TEMPLATE (tmpl) = 0; | |
6233 | DECL_TEMPLATE_INFO (tmpl) = NULL_TREE; | |
6234 | CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0; | |
68c78847 KL |
6235 | CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)) |
6236 | = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))); | |
6757edfe MM |
6237 | |
6238 | /* Inject this template into the global scope. */ | |
d63d5d0c | 6239 | friend_type = TREE_TYPE (pushdecl_top_level_maybe_friend (tmpl, true)); |
6757edfe MM |
6240 | } |
6241 | ||
c8094d83 | 6242 | if (context) |
3e0ec82f MM |
6243 | { |
6244 | if (TREE_CODE (context) == NAMESPACE_DECL) | |
6245 | pop_nested_namespace (context); | |
6246 | else | |
6247 | pop_nested_class (); | |
6248 | } | |
6249 | ||
6757edfe MM |
6250 | return friend_type; |
6251 | } | |
f84b4be9 | 6252 | |
17f29637 KL |
6253 | /* Returns zero if TYPE cannot be completed later due to circularity. |
6254 | Otherwise returns one. */ | |
6255 | ||
d5372501 | 6256 | static int |
3a978d72 | 6257 | can_complete_type_without_circularity (tree type) |
17f29637 KL |
6258 | { |
6259 | if (type == NULL_TREE || type == error_mark_node) | |
6260 | return 0; | |
6261 | else if (COMPLETE_TYPE_P (type)) | |
6262 | return 1; | |
6263 | else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type)) | |
6264 | return can_complete_type_without_circularity (TREE_TYPE (type)); | |
8c6ab2db NS |
6265 | else if (CLASS_TYPE_P (type) |
6266 | && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type))) | |
17f29637 KL |
6267 | return 0; |
6268 | else | |
6269 | return 1; | |
6270 | } | |
6271 | ||
8d08fdba | 6272 | tree |
3a978d72 | 6273 | instantiate_class_template (tree type) |
8d08fdba | 6274 | { |
7088fca9 | 6275 | tree template, args, pattern, t, member; |
36a117a5 | 6276 | tree typedecl; |
dbbf88d1 | 6277 | tree pbinfo; |
cad7e87b | 6278 | tree base_list; |
c8094d83 | 6279 | |
5566b478 | 6280 | if (type == error_mark_node) |
8d08fdba MS |
6281 | return error_mark_node; |
6282 | ||
c8094d83 | 6283 | if (TYPE_BEING_DEFINED (type) |
ca099ac8 MM |
6284 | || COMPLETE_TYPE_P (type) |
6285 | || dependent_type_p (type)) | |
5566b478 MS |
6286 | return type; |
6287 | ||
6bdb985f | 6288 | /* Figure out which template is being instantiated. */ |
36a117a5 | 6289 | template = most_general_template (CLASSTYPE_TI_TEMPLATE (type)); |
50bc768d | 6290 | gcc_assert (TREE_CODE (template) == TEMPLATE_DECL); |
73aad9b9 | 6291 | |
4c571114 MM |
6292 | /* Determine what specialization of the original template to |
6293 | instantiate. */ | |
916b63c3 | 6294 | t = most_specialized_class (type, template); |
8fbc5ae7 | 6295 | if (t == error_mark_node) |
73aad9b9 | 6296 | { |
8fbc5ae7 MM |
6297 | TYPE_BEING_DEFINED (type) = 1; |
6298 | return error_mark_node; | |
73aad9b9 | 6299 | } |
916b63c3 MM |
6300 | else if (t) |
6301 | { | |
6302 | /* This TYPE is actually an instantiation of a partial | |
6303 | specialization. We replace the innermost set of ARGS with | |
6304 | the arguments appropriate for substitution. For example, | |
6305 | given: | |
3db45ab5 MS |
6306 | |
6307 | template <class T> struct S {}; | |
916b63c3 | 6308 | template <class T> struct S<T*> {}; |
6bdb985f | 6309 | |
916b63c3 | 6310 | and supposing that we are instantiating S<int*>, ARGS will |
3db45ab5 | 6311 | presently be {int*} -- but we need {int}. */ |
916b63c3 MM |
6312 | pattern = TREE_TYPE (t); |
6313 | args = TREE_PURPOSE (t); | |
6314 | } | |
73aad9b9 | 6315 | else |
916b63c3 MM |
6316 | { |
6317 | pattern = TREE_TYPE (template); | |
6318 | args = CLASSTYPE_TI_ARGS (type); | |
6319 | } | |
5566b478 | 6320 | |
4c571114 MM |
6321 | /* If the template we're instantiating is incomplete, then clearly |
6322 | there's nothing we can do. */ | |
d0f062fb | 6323 | if (!COMPLETE_TYPE_P (pattern)) |
f31c0a32 | 6324 | return type; |
5566b478 | 6325 | |
4c571114 MM |
6326 | /* If we've recursively instantiated too many templates, stop. */ |
6327 | if (! push_tinst_level (type)) | |
f31c0a32 | 6328 | return type; |
4c571114 MM |
6329 | |
6330 | /* Now we're really doing the instantiation. Mark the type as in | |
6331 | the process of being defined. */ | |
6332 | TYPE_BEING_DEFINED (type) = 1; | |
6333 | ||
78757caa KL |
6334 | /* We may be in the middle of deferred access check. Disable |
6335 | it now. */ | |
6336 | push_deferring_access_checks (dk_no_deferred); | |
6337 | ||
c353b8e3 | 6338 | push_to_top_level (); |
4c571114 | 6339 | |
7813d14c | 6340 | SET_CLASSTYPE_INTERFACE_UNKNOWN (type); |
8d08fdba | 6341 | |
68ea098a NS |
6342 | /* Set the input location to the template definition. This is needed |
6343 | if tsubsting causes an error. */ | |
12af7ba3 MM |
6344 | typedecl = TYPE_MAIN_DECL (type); |
6345 | input_location = DECL_SOURCE_LOCATION (typedecl); | |
6346 | in_system_header = DECL_IN_SYSTEM_HEADER (typedecl); | |
68ea098a | 6347 | |
f7da6097 | 6348 | TYPE_HAS_CONSTRUCTOR (type) = TYPE_HAS_CONSTRUCTOR (pattern); |
834c6dff MM |
6349 | TYPE_HAS_NEW_OPERATOR (type) = TYPE_HAS_NEW_OPERATOR (pattern); |
6350 | TYPE_HAS_ARRAY_NEW_OPERATOR (type) = TYPE_HAS_ARRAY_NEW_OPERATOR (pattern); | |
f7da6097 | 6351 | TYPE_GETS_DELETE (type) = TYPE_GETS_DELETE (pattern); |
f7da6097 MS |
6352 | TYPE_HAS_ASSIGN_REF (type) = TYPE_HAS_ASSIGN_REF (pattern); |
6353 | TYPE_HAS_CONST_ASSIGN_REF (type) = TYPE_HAS_CONST_ASSIGN_REF (pattern); | |
f7da6097 MS |
6354 | TYPE_HAS_INIT_REF (type) = TYPE_HAS_INIT_REF (pattern); |
6355 | TYPE_HAS_CONST_INIT_REF (type) = TYPE_HAS_CONST_INIT_REF (pattern); | |
f7da6097 MS |
6356 | TYPE_HAS_DEFAULT_CONSTRUCTOR (type) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern); |
6357 | TYPE_HAS_CONVERSION (type) = TYPE_HAS_CONVERSION (pattern); | |
f7da6097 MS |
6358 | TYPE_PACKED (type) = TYPE_PACKED (pattern); |
6359 | TYPE_ALIGN (type) = TYPE_ALIGN (pattern); | |
11cf4d18 | 6360 | TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern); |
eff71ab0 | 6361 | TYPE_FOR_JAVA (type) = TYPE_FOR_JAVA (pattern); /* For libjava's JArray<T> */ |
6bdb8141 JM |
6362 | if (ANON_AGGR_TYPE_P (pattern)) |
6363 | SET_ANON_AGGR_TYPE_P (type); | |
8d039470 MS |
6364 | if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern)) |
6365 | { | |
6366 | CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1; | |
6367 | CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern); | |
6368 | } | |
f7da6097 | 6369 | |
dbbf88d1 | 6370 | pbinfo = TYPE_BINFO (pattern); |
1456deaf | 6371 | |
315fb5db NS |
6372 | /* We should never instantiate a nested class before its enclosing |
6373 | class; we need to look up the nested class by name before we can | |
6374 | instantiate it, and that lookup should instantiate the enclosing | |
6375 | class. */ | |
6376 | gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern)) | |
6377 | || COMPLETE_TYPE_P (TYPE_CONTEXT (type)) | |
6378 | || TYPE_BEING_DEFINED (TYPE_CONTEXT (type))); | |
1456deaf | 6379 | |
cad7e87b | 6380 | base_list = NULL_TREE; |
fa743e8c | 6381 | if (BINFO_N_BASE_BINFOS (pbinfo)) |
3fd71a52 | 6382 | { |
fa743e8c | 6383 | tree pbase_binfo; |
a2507277 | 6384 | tree context = TYPE_CONTEXT (type); |
4514aa8c | 6385 | tree pushed_scope; |
3fd71a52 | 6386 | int i; |
5566b478 | 6387 | |
a2507277 NS |
6388 | /* We must enter the scope containing the type, as that is where |
6389 | the accessibility of types named in dependent bases are | |
6390 | looked up from. */ | |
4514aa8c | 6391 | pushed_scope = push_scope (context ? context : global_namespace); |
c8094d83 | 6392 | |
3fd71a52 MM |
6393 | /* Substitute into each of the bases to determine the actual |
6394 | basetypes. */ | |
fa743e8c | 6395 | for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++) |
3fd71a52 MM |
6396 | { |
6397 | tree base; | |
fa743e8c | 6398 | tree access = BINFO_BASE_ACCESS (pbinfo, i); |
5d80a306 DG |
6399 | tree expanded_bases = NULL_TREE; |
6400 | int idx, len = 1; | |
6401 | ||
6402 | if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo))) | |
6403 | { | |
6404 | expanded_bases = | |
6405 | tsubst_pack_expansion (BINFO_TYPE (pbase_binfo), | |
6406 | args, tf_error, NULL_TREE); | |
6407 | if (expanded_bases == error_mark_node) | |
6408 | continue; | |
6409 | ||
6410 | len = TREE_VEC_LENGTH (expanded_bases); | |
6411 | } | |
6412 | ||
6413 | for (idx = 0; idx < len; idx++) | |
6414 | { | |
6415 | if (expanded_bases) | |
6416 | /* Extract the already-expanded base class. */ | |
6417 | base = TREE_VEC_ELT (expanded_bases, idx); | |
6418 | else | |
6419 | /* Substitute to figure out the base class. */ | |
6420 | base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error, | |
6421 | NULL_TREE); | |
6422 | ||
6423 | if (base == error_mark_node) | |
6424 | continue; | |
6425 | ||
6426 | base_list = tree_cons (access, base, base_list); | |
6427 | if (BINFO_VIRTUAL_P (pbase_binfo)) | |
6428 | TREE_TYPE (base_list) = integer_type_node; | |
6429 | } | |
3fd71a52 | 6430 | } |
dfbcd65a | 6431 | |
3fd71a52 MM |
6432 | /* The list is now in reverse order; correct that. */ |
6433 | base_list = nreverse (base_list); | |
6434 | ||
4514aa8c NS |
6435 | if (pushed_scope) |
6436 | pop_scope (pushed_scope); | |
3fd71a52 | 6437 | } |
cad7e87b NS |
6438 | /* Now call xref_basetypes to set up all the base-class |
6439 | information. */ | |
6440 | xref_basetypes (type, base_list); | |
6441 | ||
5566b478 | 6442 | |
b74a0560 MM |
6443 | /* Now that our base classes are set up, enter the scope of the |
6444 | class, so that name lookups into base classes, etc. will work | |
dc957d14 | 6445 | correctly. This is precisely analogous to what we do in |
b74a0560 MM |
6446 | begin_class_definition when defining an ordinary non-template |
6447 | class. */ | |
29370796 | 6448 | pushclass (type); |
b74a0560 | 6449 | |
7088fca9 | 6450 | /* Now members are processed in the order of declaration. */ |
8c6ab2db NS |
6451 | for (member = CLASSTYPE_DECL_LIST (pattern); |
6452 | member; member = TREE_CHAIN (member)) | |
8d08fdba | 6453 | { |
7088fca9 | 6454 | tree t = TREE_VALUE (member); |
5566b478 | 6455 | |
7088fca9 | 6456 | if (TREE_PURPOSE (member)) |
ed44da02 | 6457 | { |
7088fca9 KL |
6458 | if (TYPE_P (t)) |
6459 | { | |
5e0c54e5 | 6460 | /* Build new CLASSTYPE_NESTED_UTDS. */ |
8d08fdba | 6461 | |
7088fca9 | 6462 | tree newtag; |
883a2bff MM |
6463 | bool class_template_p; |
6464 | ||
2678bae8 VR |
6465 | class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE |
6466 | && TYPE_LANG_SPECIFIC (t) | |
6467 | && CLASSTYPE_IS_TEMPLATE (t)); | |
883a2bff | 6468 | /* If the member is a class template, then -- even after |
6c745393 | 6469 | substitution -- there may be dependent types in the |
883a2bff MM |
6470 | template argument list for the class. We increment |
6471 | PROCESSING_TEMPLATE_DECL so that dependent_type_p, as | |
6472 | that function will assume that no types are dependent | |
6473 | when outside of a template. */ | |
6474 | if (class_template_p) | |
6475 | ++processing_template_decl; | |
2678bae8 | 6476 | newtag = tsubst (t, args, tf_error, NULL_TREE); |
883a2bff MM |
6477 | if (class_template_p) |
6478 | --processing_template_decl; | |
2620d095 KL |
6479 | if (newtag == error_mark_node) |
6480 | continue; | |
6481 | ||
7088fca9 KL |
6482 | if (TREE_CODE (newtag) != ENUMERAL_TYPE) |
6483 | { | |
2678bae8 VR |
6484 | tree name = TYPE_IDENTIFIER (t); |
6485 | ||
883a2bff | 6486 | if (class_template_p) |
7088fca9 KL |
6487 | /* Unfortunately, lookup_template_class sets |
6488 | CLASSTYPE_IMPLICIT_INSTANTIATION for a partial | |
3afd2e20 NS |
6489 | instantiation (i.e., for the type of a member |
6490 | template class nested within a template class.) | |
6491 | This behavior is required for | |
6492 | maybe_process_partial_specialization to work | |
6493 | correctly, but is not accurate in this case; | |
6494 | the TAG is not an instantiation of anything. | |
6495 | (The corresponding TEMPLATE_DECL is an | |
6496 | instantiation, but the TYPE is not.) */ | |
7088fca9 KL |
6497 | CLASSTYPE_USE_TEMPLATE (newtag) = 0; |
6498 | ||
6499 | /* Now, we call pushtag to put this NEWTAG into the scope of | |
6500 | TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid | |
6501 | pushtag calling push_template_decl. We don't have to do | |
6502 | this for enums because it will already have been done in | |
6503 | tsubst_enum. */ | |
6504 | if (name) | |
6505 | SET_IDENTIFIER_TYPE_VALUE (name, newtag); | |
bd3d082e | 6506 | pushtag (name, newtag, /*tag_scope=*/ts_current); |
7088fca9 KL |
6507 | } |
6508 | } | |
c8094d83 | 6509 | else if (TREE_CODE (t) == FUNCTION_DECL |
7088fca9 KL |
6510 | || DECL_FUNCTION_TEMPLATE_P (t)) |
6511 | { | |
6512 | /* Build new TYPE_METHODS. */ | |
fecafe5e | 6513 | tree r; |
c8094d83 | 6514 | |
fecafe5e | 6515 | if (TREE_CODE (t) == TEMPLATE_DECL) |
9579624e | 6516 | ++processing_template_decl; |
fecafe5e NS |
6517 | r = tsubst (t, args, tf_error, NULL_TREE); |
6518 | if (TREE_CODE (t) == TEMPLATE_DECL) | |
9579624e | 6519 | --processing_template_decl; |
7088fca9 | 6520 | set_current_access_from_decl (r); |
7088fca9 KL |
6521 | finish_member_declaration (r); |
6522 | } | |
6523 | else | |
6524 | { | |
6525 | /* Build new TYPE_FIELDS. */ | |
55a3debe DG |
6526 | if (TREE_CODE (t) == STATIC_ASSERT) |
6527 | { | |
6528 | tree condition = | |
6529 | tsubst_expr (STATIC_ASSERT_CONDITION (t), args, | |
6530 | tf_warning_or_error, NULL_TREE, | |
6531 | /*integral_constant_expression_p=*/true); | |
6532 | finish_static_assert (condition, | |
6533 | STATIC_ASSERT_MESSAGE (t), | |
6534 | STATIC_ASSERT_SOURCE_LOCATION (t), | |
6535 | /*member_p=*/true); | |
6536 | } | |
6537 | else if (TREE_CODE (t) != CONST_DECL) | |
7088fca9 KL |
6538 | { |
6539 | tree r; | |
fa8d6e85 | 6540 | |
d479d37f NS |
6541 | /* The the file and line for this declaration, to |
6542 | assist in error message reporting. Since we | |
6543 | called push_tinst_level above, we don't need to | |
6544 | restore these. */ | |
f31686a3 | 6545 | input_location = DECL_SOURCE_LOCATION (t); |
fa8d6e85 | 6546 | |
fb5ce3c9 | 6547 | if (TREE_CODE (t) == TEMPLATE_DECL) |
9579624e | 6548 | ++processing_template_decl; |
23fca1f5 | 6549 | r = tsubst (t, args, tf_warning_or_error, NULL_TREE); |
fb5ce3c9 | 6550 | if (TREE_CODE (t) == TEMPLATE_DECL) |
9579624e | 6551 | --processing_template_decl; |
7088fca9 KL |
6552 | if (TREE_CODE (r) == VAR_DECL) |
6553 | { | |
b794e321 MM |
6554 | /* In [temp.inst]: |
6555 | ||
6556 | [t]he initialization (and any associated | |
6557 | side-effects) of a static data member does | |
6558 | not occur unless the static data member is | |
6559 | itself used in a way that requires the | |
6560 | definition of the static data member to | |
3db45ab5 | 6561 | exist. |
b794e321 MM |
6562 | |
6563 | Therefore, we do not substitute into the | |
3db45ab5 MS |
6564 | initialized for the static data member here. */ |
6565 | finish_static_data_member_decl | |
6566 | (r, | |
6567 | /*init=*/NULL_TREE, | |
d174af6c | 6568 | /*init_const_expr_p=*/false, |
3db45ab5 | 6569 | /*asmspec_tree=*/NULL_TREE, |
b794e321 | 6570 | /*flags=*/0); |
7088fca9 KL |
6571 | if (DECL_INITIALIZED_IN_CLASS_P (r)) |
6572 | check_static_variable_definition (r, TREE_TYPE (r)); | |
6573 | } | |
6574 | else if (TREE_CODE (r) == FIELD_DECL) | |
6575 | { | |
6576 | /* Determine whether R has a valid type and can be | |
6577 | completed later. If R is invalid, then it is | |
6578 | replaced by error_mark_node so that it will not be | |
6579 | added to TYPE_FIELDS. */ | |
6580 | tree rtype = TREE_TYPE (r); | |
6581 | if (can_complete_type_without_circularity (rtype)) | |
6582 | complete_type (rtype); | |
6583 | ||
6584 | if (!COMPLETE_TYPE_P (rtype)) | |
6585 | { | |
6586 | cxx_incomplete_type_error (r, rtype); | |
0cbd7506 | 6587 | r = error_mark_node; |
7088fca9 KL |
6588 | } |
6589 | } | |
5566b478 | 6590 | |
7088fca9 KL |
6591 | /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE, |
6592 | such a thing will already have been added to the field | |
6593 | list by tsubst_enum in finish_member_declaration in the | |
5e0c54e5 | 6594 | CLASSTYPE_NESTED_UTDS case above. */ |
7088fca9 KL |
6595 | if (!(TREE_CODE (r) == TYPE_DECL |
6596 | && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE | |
43b26a01 | 6597 | && DECL_ARTIFICIAL (r))) |
7088fca9 KL |
6598 | { |
6599 | set_current_access_from_decl (r); | |
6600 | finish_member_declaration (r); | |
6601 | } | |
0cbd7506 | 6602 | } |
7088fca9 | 6603 | } |
61fc8c9e | 6604 | } |
7088fca9 KL |
6605 | else |
6606 | { | |
6607 | if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)) | |
6608 | { | |
6609 | /* Build new CLASSTYPE_FRIEND_CLASSES. */ | |
6610 | ||
6611 | tree friend_type = t; | |
b939a023 | 6612 | bool adjust_processing_template_decl = false; |
1aed5355 | 6613 | |
7088fca9 | 6614 | if (TREE_CODE (friend_type) == TEMPLATE_DECL) |
b939a023 | 6615 | { |
5a24482e | 6616 | /* template <class T> friend class C; */ |
b939a023 | 6617 | friend_type = tsubst_friend_class (friend_type, args); |
0cbd7506 | 6618 | adjust_processing_template_decl = true; |
b939a023 KL |
6619 | } |
6620 | else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE) | |
6621 | { | |
5a24482e | 6622 | /* template <class T> friend class C::D; */ |
b939a023 | 6623 | friend_type = tsubst (friend_type, args, |
23fca1f5 | 6624 | tf_warning_or_error, NULL_TREE); |
b939a023 KL |
6625 | if (TREE_CODE (friend_type) == TEMPLATE_DECL) |
6626 | friend_type = TREE_TYPE (friend_type); | |
0cbd7506 | 6627 | adjust_processing_template_decl = true; |
b939a023 KL |
6628 | } |
6629 | else if (TREE_CODE (friend_type) == TYPENAME_TYPE) | |
6630 | { | |
5a24482e KL |
6631 | /* This could be either |
6632 | ||
6633 | friend class T::C; | |
6634 | ||
6635 | when dependent_type_p is false or | |
6636 | ||
6637 | template <class U> friend class T::C; | |
6638 | ||
6639 | otherwise. */ | |
b939a023 | 6640 | friend_type = tsubst (friend_type, args, |
23fca1f5 | 6641 | tf_warning_or_error, NULL_TREE); |
b939a023 KL |
6642 | /* Bump processing_template_decl for correct |
6643 | dependent_type_p calculation. */ | |
6644 | ++processing_template_decl; | |
6645 | if (dependent_type_p (friend_type)) | |
6646 | adjust_processing_template_decl = true; | |
6647 | --processing_template_decl; | |
6648 | } | |
5a24482e KL |
6649 | else if (!CLASSTYPE_USE_TEMPLATE (friend_type) |
6650 | && hidden_name_p (TYPE_NAME (friend_type))) | |
7088fca9 | 6651 | { |
5a24482e KL |
6652 | /* friend class C; |
6653 | ||
6654 | where C hasn't been declared yet. Let's lookup name | |
6655 | from namespace scope directly, bypassing any name that | |
6656 | come from dependent base class. */ | |
7088fca9 KL |
6657 | tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type)); |
6658 | ||
6659 | /* The call to xref_tag_from_type does injection for friend | |
6660 | classes. */ | |
6661 | push_nested_namespace (ns); | |
c8094d83 MS |
6662 | friend_type = |
6663 | xref_tag_from_type (friend_type, NULL_TREE, | |
5a24482e | 6664 | /*tag_scope=*/ts_current); |
7088fca9 KL |
6665 | pop_nested_namespace (ns); |
6666 | } | |
5a24482e KL |
6667 | else if (uses_template_parms (friend_type)) |
6668 | /* friend class C<T>; */ | |
6669 | friend_type = tsubst (friend_type, args, | |
23fca1f5 | 6670 | tf_warning_or_error, NULL_TREE); |
5a24482e KL |
6671 | /* Otherwise it's |
6672 | ||
6673 | friend class C; | |
6674 | ||
6675 | where C is already declared or | |
6676 | ||
6677 | friend class C<int>; | |
6678 | ||
0cbd7506 | 6679 | We don't have to do anything in these cases. */ |
1aed5355 | 6680 | |
b939a023 | 6681 | if (adjust_processing_template_decl) |
7088fca9 KL |
6682 | /* Trick make_friend_class into realizing that the friend |
6683 | we're adding is a template, not an ordinary class. It's | |
6684 | important that we use make_friend_class since it will | |
6685 | perform some error-checking and output cross-reference | |
6686 | information. */ | |
6687 | ++processing_template_decl; | |
fc378698 | 6688 | |
b939a023 | 6689 | if (friend_type != error_mark_node) |
0cbd7506 | 6690 | make_friend_class (type, friend_type, /*complain=*/false); |
fc378698 | 6691 | |
b939a023 | 6692 | if (adjust_processing_template_decl) |
7088fca9 KL |
6693 | --processing_template_decl; |
6694 | } | |
6695 | else | |
9579624e KL |
6696 | { |
6697 | /* Build new DECL_FRIENDLIST. */ | |
6698 | tree r; | |
6699 | ||
6e432b31 KL |
6700 | /* The the file and line for this declaration, to |
6701 | assist in error message reporting. Since we | |
6702 | called push_tinst_level above, we don't need to | |
6703 | restore these. */ | |
6704 | input_location = DECL_SOURCE_LOCATION (t); | |
6705 | ||
9579624e | 6706 | if (TREE_CODE (t) == TEMPLATE_DECL) |
cad7e87b NS |
6707 | { |
6708 | ++processing_template_decl; | |
6709 | push_deferring_access_checks (dk_no_check); | |
6710 | } | |
c8094d83 | 6711 | |
9579624e | 6712 | r = tsubst_friend_function (t, args); |
9579624e | 6713 | add_friend (type, r, /*complain=*/false); |
cad7e87b NS |
6714 | if (TREE_CODE (t) == TEMPLATE_DECL) |
6715 | { | |
6716 | pop_deferring_access_checks (); | |
6717 | --processing_template_decl; | |
6718 | } | |
9579624e | 6719 | } |
7088fca9 KL |
6720 | } |
6721 | } | |
5566b478 | 6722 | |
61a127b3 MM |
6723 | /* Set the file and line number information to whatever is given for |
6724 | the class itself. This puts error messages involving generated | |
6725 | implicit functions at a predictable point, and the same point | |
6726 | that would be used for non-template classes. */ | |
f31686a3 | 6727 | input_location = DECL_SOURCE_LOCATION (typedecl); |
6de9cd9a | 6728 | |
61a127b3 | 6729 | unreverse_member_declarations (type); |
9f33663b | 6730 | finish_struct_1 (type); |
5524676d | 6731 | TYPE_BEING_DEFINED (type) = 0; |
8d08fdba | 6732 | |
9188c363 MM |
6733 | /* Now that the class is complete, instantiate default arguments for |
6734 | any member functions. We don't do this earlier because the | |
6735 | default arguments may reference members of the class. */ | |
6736 | if (!PRIMARY_TEMPLATE_P (template)) | |
6737 | for (t = TYPE_METHODS (type); t; t = TREE_CHAIN (t)) | |
c8094d83 | 6738 | if (TREE_CODE (t) == FUNCTION_DECL |
dc957d14 | 6739 | /* Implicitly generated member functions will not have template |
9188c363 MM |
6740 | information; they are not instantiations, but instead are |
6741 | created "fresh" for each instantiation. */ | |
6742 | && DECL_TEMPLATE_INFO (t)) | |
6743 | tsubst_default_arguments (t); | |
6744 | ||
b74a0560 | 6745 | popclass (); |
5566b478 | 6746 | pop_from_top_level (); |
78757caa | 6747 | pop_deferring_access_checks (); |
5566b478 MS |
6748 | pop_tinst_level (); |
6749 | ||
4684cd27 MM |
6750 | /* The vtable for a template class can be emitted in any translation |
6751 | unit in which the class is instantiated. When there is no key | |
6752 | method, however, finish_struct_1 will already have added TYPE to | |
6753 | the keyed_classes list. */ | |
6754 | if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type)) | |
9aad8f83 MA |
6755 | keyed_classes = tree_cons (NULL_TREE, type, keyed_classes); |
6756 | ||
5566b478 | 6757 | return type; |
8d08fdba MS |
6758 | } |
6759 | ||
00d3396f | 6760 | static tree |
a91db711 | 6761 | tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl) |
00d3396f | 6762 | { |
a91db711 | 6763 | tree r; |
c8094d83 | 6764 | |
a91db711 NS |
6765 | if (!t) |
6766 | r = t; | |
6767 | else if (TYPE_P (t)) | |
6768 | r = tsubst (t, args, complain, in_decl); | |
6769 | else | |
00d3396f | 6770 | { |
015c2c66 MM |
6771 | r = tsubst_expr (t, args, complain, in_decl, |
6772 | /*integral_constant_expression_p=*/true); | |
6f25cb35 | 6773 | r = fold_non_dependent_expr (r); |
bd83b409 | 6774 | } |
5d80a306 DG |
6775 | return r; |
6776 | } | |
6777 | ||
6778 | /* Substitute ARGS into T, which is an pack expansion | |
6779 | (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a | |
6780 | TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node | |
6781 | (if only a partial substitution could be performed) or | |
6782 | ERROR_MARK_NODE if there was an error. */ | |
6783 | tree | |
6784 | tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain, | |
6785 | tree in_decl) | |
6786 | { | |
6787 | tree pattern; | |
6788 | tree pack, packs = NULL_TREE, unsubstituted_packs = NULL_TREE; | |
6789 | tree first_arg_pack; int i, len = -1; | |
6790 | tree result; | |
6791 | int incomplete = 0; | |
6792 | ||
6793 | gcc_assert (PACK_EXPANSION_P (t)); | |
6794 | pattern = PACK_EXPANSION_PATTERN (t); | |
6795 | ||
6796 | /* Determine the argument packs that will instantiate the parameter | |
6797 | packs used in the expansion expression. While we're at it, | |
6798 | compute the number of arguments to be expanded and make sure it | |
6799 | is consistent. */ | |
6800 | for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack; | |
6801 | pack = TREE_CHAIN (pack)) | |
6802 | { | |
6803 | tree parm_pack = TREE_VALUE (pack); | |
6804 | tree arg_pack = NULL_TREE; | |
6805 | tree orig_arg = NULL_TREE; | |
6806 | ||
6807 | if (TREE_CODE (parm_pack) == PARM_DECL) | |
6808 | { | |
6809 | if (local_specializations) | |
6810 | arg_pack = retrieve_local_specialization (parm_pack); | |
6811 | } | |
6812 | else | |
6813 | { | |
6814 | int level, idx, levels; | |
6815 | template_parm_level_and_index (parm_pack, &level, &idx); | |
6816 | ||
6817 | levels = TMPL_ARGS_DEPTH (args); | |
6818 | if (level <= levels) | |
6819 | arg_pack = TMPL_ARG (args, level, idx); | |
6820 | } | |
6821 | ||
6822 | orig_arg = arg_pack; | |
6823 | if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT) | |
6824 | arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack); | |
6825 | ||
6826 | if (arg_pack) | |
6827 | { | |
6828 | int my_len = | |
6829 | TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack)); | |
6830 | ||
6831 | /* It's all-or-nothing with incomplete argument packs. */ | |
6832 | if (incomplete && !ARGUMENT_PACK_INCOMPLETE_P (arg_pack)) | |
6833 | return error_mark_node; | |
6834 | ||
6835 | if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack)) | |
6836 | incomplete = 1; | |
6837 | ||
6838 | if (len < 0) | |
6839 | { | |
6840 | len = my_len; | |
6841 | first_arg_pack = arg_pack; | |
6842 | } | |
6843 | else if (len != my_len) | |
6844 | { | |
6845 | if (TREE_CODE (t) == TYPE_PACK_EXPANSION) | |
6846 | error ("mismatched argument pack lengths while expanding " | |
6847 | "%<%T%>", | |
6848 | pattern); | |
6849 | else | |
6850 | error ("mismatched argument pack lengths while expanding " | |
6851 | "%<%E%>", | |
6852 | pattern); | |
6853 | return error_mark_node; | |
6854 | } | |
6855 | ||
6856 | /* Keep track of the parameter packs and their corresponding | |
6857 | argument packs. */ | |
6858 | packs = tree_cons (parm_pack, arg_pack, packs); | |
6859 | TREE_TYPE (packs) = orig_arg; | |
6860 | } | |
6861 | else | |
6862 | /* We can't substitute for this parameter pack. */ | |
6863 | unsubstituted_packs = tree_cons (TREE_PURPOSE (pack), | |
6864 | TREE_VALUE (pack), | |
6865 | unsubstituted_packs); | |
6866 | } | |
6867 | ||
6868 | /* We cannot expand this expansion expression, because we don't have | |
6869 | all of the argument packs we need. Substitute into the pattern | |
6870 | and return a PACK_EXPANSION_*. The caller will need to deal with | |
6871 | that. */ | |
6872 | if (unsubstituted_packs) | |
6873 | return make_pack_expansion (tsubst (pattern, args, complain, | |
6874 | in_decl)); | |
6875 | ||
6876 | /* We could not find any argument packs that work. */ | |
6877 | if (len < 0) | |
6878 | return error_mark_node; | |
6879 | ||
6880 | /* For each argument in each argument pack, substitute into the | |
6881 | pattern. */ | |
6882 | result = make_tree_vec (len + incomplete); | |
6883 | for (i = 0; i < len + incomplete; ++i) | |
6884 | { | |
6885 | /* For parameter pack, change the substitution of the parameter | |
6886 | pack to the ith argument in its argument pack, then expand | |
6887 | the pattern. */ | |
6888 | for (pack = packs; pack; pack = TREE_CHAIN (pack)) | |
6889 | { | |
6890 | tree parm = TREE_PURPOSE (pack); | |
6891 | ||
6892 | if (TREE_CODE (parm) == PARM_DECL) | |
6893 | { | |
6894 | /* Select the Ith argument from the pack. */ | |
6895 | tree arg = make_node (ARGUMENT_PACK_SELECT); | |
6896 | ARGUMENT_PACK_SELECT_FROM_PACK (arg) = TREE_VALUE (pack); | |
6897 | ARGUMENT_PACK_SELECT_INDEX (arg) = i; | |
6898 | mark_used (parm); | |
6899 | register_local_specialization (arg, parm); | |
6900 | } | |
6901 | else | |
6902 | { | |
6903 | tree value = parm; | |
6904 | int idx, level; | |
6905 | template_parm_level_and_index (parm, &level, &idx); | |
6906 | ||
6907 | if (i < len) | |
6908 | { | |
6909 | /* Select the Ith argument from the pack. */ | |
6910 | value = make_node (ARGUMENT_PACK_SELECT); | |
6911 | ARGUMENT_PACK_SELECT_FROM_PACK (value) = TREE_VALUE (pack); | |
6912 | ARGUMENT_PACK_SELECT_INDEX (value) = i; | |
6913 | } | |
6914 | ||
6915 | /* Update the corresponding argument. */ | |
6916 | TMPL_ARG (args, level, idx) = value; | |
6917 | } | |
6918 | } | |
6919 | ||
6920 | /* Substitute into the PATTERN with the altered arguments. */ | |
6921 | if (TREE_CODE (t) == EXPR_PACK_EXPANSION) | |
6922 | TREE_VEC_ELT (result, i) = | |
6923 | tsubst_expr (pattern, args, complain, in_decl, | |
6924 | /*integral_constant_expression_p=*/false); | |
6925 | else | |
6926 | TREE_VEC_ELT (result, i) = tsubst (pattern, args, complain, in_decl); | |
6927 | ||
6928 | if (i == len) | |
6929 | /* When we have incomplete argument packs, the last "expanded" | |
6930 | result is itself a pack expansion, which allows us | |
6931 | to deduce more arguments. */ | |
6932 | TREE_VEC_ELT (result, i) = | |
6933 | make_pack_expansion (TREE_VEC_ELT (result, i)); | |
6934 | ||
6935 | if (TREE_VEC_ELT (result, i) == error_mark_node) | |
6936 | { | |
6937 | result = error_mark_node; | |
6938 | break; | |
6939 | } | |
6940 | } | |
6941 | ||
6942 | /* Update ARGS to restore the substitution from parameter packs to | |
6943 | their argument packs. */ | |
6944 | for (pack = packs; pack; pack = TREE_CHAIN (pack)) | |
6945 | { | |
6946 | tree parm = TREE_PURPOSE (pack); | |
6947 | ||
6948 | if (TREE_CODE (parm) == PARM_DECL) | |
6949 | register_local_specialization (TREE_TYPE (pack), parm); | |
6950 | else | |
6951 | { | |
6952 | int idx, level; | |
6953 | template_parm_level_and_index (parm, &level, &idx); | |
6954 | ||
6955 | /* Update the corresponding argument. */ | |
6956 | if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args)) | |
6957 | TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) = | |
6958 | TREE_TYPE (pack); | |
6959 | else | |
6960 | TREE_VEC_ELT (args, idx) = TREE_TYPE (pack); | |
6961 | } | |
6962 | } | |
6963 | ||
6964 | return result; | |
bd83b409 NS |
6965 | } |
6966 | ||
a91db711 | 6967 | /* Substitute ARGS into the vector or list of template arguments T. */ |
830bfa74 | 6968 | |
e9659ab0 | 6969 | static tree |
a91db711 | 6970 | tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl) |
830bfa74 | 6971 | { |
5d80a306 | 6972 | tree orig_t = t; |
bf12d54d | 6973 | int len = TREE_VEC_LENGTH (t); |
5d80a306 | 6974 | int need_new = 0, i, expanded_len_adjust = 0, out; |
67f5655f | 6975 | tree *elts = (tree *) alloca (len * sizeof (tree)); |
c8094d83 | 6976 | |
830bfa74 MM |
6977 | for (i = 0; i < len; i++) |
6978 | { | |
bf12d54d NS |
6979 | tree orig_arg = TREE_VEC_ELT (t, i); |
6980 | tree new_arg; | |
a91db711 | 6981 | |
bf12d54d NS |
6982 | if (TREE_CODE (orig_arg) == TREE_VEC) |
6983 | new_arg = tsubst_template_args (orig_arg, args, complain, in_decl); | |
5d80a306 DG |
6984 | else if (PACK_EXPANSION_P (orig_arg)) |
6985 | { | |
6986 | /* Substitute into an expansion expression. */ | |
6987 | new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl); | |
6988 | ||
6989 | if (TREE_CODE (new_arg) == TREE_VEC) | |
6990 | /* Add to the expanded length adjustment the number of | |
6991 | expanded arguments. We subtract one from this | |
6992 | measurement, because the argument pack expression | |
6993 | itself is already counted as 1 in | |
6994 | LEN. EXPANDED_LEN_ADJUST can actually be negative, if | |
6995 | the argument pack is empty. */ | |
6996 | expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1; | |
6997 | } | |
6998 | else if (ARGUMENT_PACK_P (orig_arg)) | |
6999 | { | |
7000 | /* Substitute into each of the arguments. */ | |
7001 | new_arg = make_node (TREE_CODE (orig_arg)); | |
7002 | ||
7003 | SET_ARGUMENT_PACK_ARGS ( | |
7004 | new_arg, | |
7005 | tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg), | |
7006 | args, complain, in_decl)); | |
7007 | ||
7008 | if (ARGUMENT_PACK_ARGS (new_arg) == error_mark_node) | |
7009 | new_arg = error_mark_node; | |
7010 | ||
7011 | if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK) { | |
7012 | TREE_TYPE (new_arg) = tsubst (TREE_TYPE (orig_arg), args, | |
7013 | complain, in_decl); | |
7014 | TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg); | |
7015 | ||
7016 | if (TREE_TYPE (new_arg) == error_mark_node) | |
7017 | new_arg = error_mark_node; | |
7018 | } | |
7019 | } | |
830bfa74 | 7020 | else |
a91db711 | 7021 | new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl); |
c8094d83 | 7022 | |
a91db711 | 7023 | if (new_arg == error_mark_node) |
08e72a19 JM |
7024 | return error_mark_node; |
7025 | ||
a91db711 NS |
7026 | elts[i] = new_arg; |
7027 | if (new_arg != orig_arg) | |
830bfa74 MM |
7028 | need_new = 1; |
7029 | } | |
c8094d83 | 7030 | |
830bfa74 MM |
7031 | if (!need_new) |
7032 | return t; | |
a91db711 | 7033 | |
5d80a306 DG |
7034 | /* Make space for the expanded arguments coming from template |
7035 | argument packs. */ | |
7036 | t = make_tree_vec (len + expanded_len_adjust); | |
7037 | for (i = 0, out = 0; i < len; i++) | |
7038 | { | |
7039 | if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i)) | |
7040 | || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i))) | |
7041 | && TREE_CODE (elts[i]) == TREE_VEC) | |
7042 | { | |
7043 | int idx; | |
7044 | ||
7045 | /* Now expand the template argument pack "in place". */ | |
7046 | for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++) | |
7047 | TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx); | |
7048 | } | |
7049 | else | |
7050 | { | |
7051 | TREE_VEC_ELT (t, out) = elts[i]; | |
7052 | out++; | |
7053 | } | |
7054 | } | |
c8094d83 | 7055 | |
830bfa74 MM |
7056 | return t; |
7057 | } | |
7058 | ||
36a117a5 MM |
7059 | /* Return the result of substituting ARGS into the template parameters |
7060 | given by PARMS. If there are m levels of ARGS and m + n levels of | |
7061 | PARMS, then the result will contain n levels of PARMS. For | |
7062 | example, if PARMS is `template <class T> template <class U> | |
7063 | template <T*, U, class V>' and ARGS is {{int}, {double}} then the | |
7064 | result will be `template <int*, double, class V>'. */ | |
7065 | ||
e9659ab0 | 7066 | static tree |
3a978d72 | 7067 | tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain) |
36a117a5 | 7068 | { |
f71f87f9 MM |
7069 | tree r = NULL_TREE; |
7070 | tree* new_parms; | |
36a117a5 | 7071 | |
8ca4bf25 MM |
7072 | /* When substituting into a template, we must set |
7073 | PROCESSING_TEMPLATE_DECL as the template parameters may be | |
7074 | dependent if they are based on one-another, and the dependency | |
7075 | predicates are short-circuit outside of templates. */ | |
7076 | ++processing_template_decl; | |
7077 | ||
36a117a5 MM |
7078 | for (new_parms = &r; |
7079 | TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args); | |
7080 | new_parms = &(TREE_CHAIN (*new_parms)), | |
7081 | parms = TREE_CHAIN (parms)) | |
7082 | { | |
c8094d83 | 7083 | tree new_vec = |
36a117a5 MM |
7084 | make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms))); |
7085 | int i; | |
c8094d83 | 7086 | |
36a117a5 MM |
7087 | for (i = 0; i < TREE_VEC_LENGTH (new_vec); ++i) |
7088 | { | |
42b304f1 LM |
7089 | tree tuple; |
7090 | tree default_value; | |
7091 | tree parm_decl; | |
7092 | ||
7093 | if (parms == error_mark_node) | |
7094 | continue; | |
7095 | ||
7096 | tuple = TREE_VEC_ELT (TREE_VALUE (parms), i); | |
7097 | ||
7098 | if (tuple == error_mark_node) | |
7099 | continue; | |
7100 | ||
7101 | default_value = TREE_PURPOSE (tuple); | |
7102 | parm_decl = TREE_VALUE (tuple); | |
833aa4c4 NS |
7103 | |
7104 | parm_decl = tsubst (parm_decl, args, complain, NULL_TREE); | |
a207780f VR |
7105 | if (TREE_CODE (parm_decl) == PARM_DECL |
7106 | && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain)) | |
7107 | parm_decl = error_mark_node; | |
a91db711 NS |
7108 | default_value = tsubst_template_arg (default_value, args, |
7109 | complain, NULL_TREE); | |
c8094d83 | 7110 | |
a91db711 | 7111 | tuple = build_tree_list (default_value, parm_decl); |
833aa4c4 | 7112 | TREE_VEC_ELT (new_vec, i) = tuple; |
36a117a5 | 7113 | } |
c8094d83 MS |
7114 | |
7115 | *new_parms = | |
7116 | tree_cons (size_int (TMPL_PARMS_DEPTH (parms) | |
4890c2f4 | 7117 | - TMPL_ARGS_DEPTH (args)), |
36a117a5 MM |
7118 | new_vec, NULL_TREE); |
7119 | } | |
7120 | ||
8ca4bf25 MM |
7121 | --processing_template_decl; |
7122 | ||
36a117a5 MM |
7123 | return r; |
7124 | } | |
7125 | ||
ed44da02 MM |
7126 | /* Substitute the ARGS into the indicated aggregate (or enumeration) |
7127 | type T. If T is not an aggregate or enumeration type, it is | |
7128 | handled as if by tsubst. IN_DECL is as for tsubst. If | |
838dfd8a | 7129 | ENTERING_SCOPE is nonzero, T is the context for a template which |
dc957d14 | 7130 | we are presently tsubst'ing. Return the substituted value. */ |
36a117a5 | 7131 | |
e9659ab0 | 7132 | static tree |
c8094d83 | 7133 | tsubst_aggr_type (tree t, |
0cbd7506 MS |
7134 | tree args, |
7135 | tsubst_flags_t complain, | |
7136 | tree in_decl, | |
7137 | int entering_scope) | |
36a117a5 MM |
7138 | { |
7139 | if (t == NULL_TREE) | |
7140 | return NULL_TREE; | |
7141 | ||
7142 | switch (TREE_CODE (t)) | |
7143 | { | |
7144 | case RECORD_TYPE: | |
7145 | if (TYPE_PTRMEMFUNC_P (t)) | |
46c895ac | 7146 | return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl); |
36a117a5 | 7147 | |
f4f206f4 | 7148 | /* Else fall through. */ |
ed44da02 | 7149 | case ENUMERAL_TYPE: |
36a117a5 | 7150 | case UNION_TYPE: |
5db698f6 | 7151 | if (TYPE_TEMPLATE_INFO (t)) |
36a117a5 MM |
7152 | { |
7153 | tree argvec; | |
7154 | tree context; | |
7155 | tree r; | |
3489ea0c MM |
7156 | bool saved_skip_evaluation; |
7157 | ||
7158 | /* In "sizeof(X<I>)" we need to evaluate "I". */ | |
7159 | saved_skip_evaluation = skip_evaluation; | |
7160 | skip_evaluation = false; | |
36a117a5 MM |
7161 | |
7162 | /* First, determine the context for the type we are looking | |
7163 | up. */ | |
4f7847ca NS |
7164 | context = TYPE_CONTEXT (t); |
7165 | if (context) | |
7166 | context = tsubst_aggr_type (context, args, complain, | |
36a117a5 | 7167 | in_decl, /*entering_scope=*/1); |
36a117a5 MM |
7168 | |
7169 | /* Then, figure out what arguments are appropriate for the | |
7170 | type we are trying to find. For example, given: | |
7171 | ||
7172 | template <class T> struct S; | |
7173 | template <class T, class U> void f(T, U) { S<U> su; } | |
7174 | ||
7175 | and supposing that we are instantiating f<int, double>, | |
7176 | then our ARGS will be {int, double}, but, when looking up | |
7177 | S we only want {double}. */ | |
a91db711 NS |
7178 | argvec = tsubst_template_args (TYPE_TI_ARGS (t), args, |
7179 | complain, in_decl); | |
08e72a19 | 7180 | if (argvec == error_mark_node) |
3489ea0c MM |
7181 | r = error_mark_node; |
7182 | else | |
7183 | { | |
7184 | r = lookup_template_class (t, argvec, in_decl, context, | |
7185 | entering_scope, complain); | |
7186 | r = cp_build_qualified_type_real (r, TYPE_QUALS (t), complain); | |
7187 | } | |
3db45ab5 | 7188 | |
3489ea0c | 7189 | skip_evaluation = saved_skip_evaluation; |
36a117a5 | 7190 | |
3489ea0c | 7191 | return r; |
36a117a5 | 7192 | } |
c8094d83 | 7193 | else |
36a117a5 MM |
7194 | /* This is not a template type, so there's nothing to do. */ |
7195 | return t; | |
7196 | ||
7197 | default: | |
4393e105 | 7198 | return tsubst (t, args, complain, in_decl); |
36a117a5 MM |
7199 | } |
7200 | } | |
7201 | ||
9188c363 MM |
7202 | /* Substitute into the default argument ARG (a default argument for |
7203 | FN), which has the indicated TYPE. */ | |
7204 | ||
7205 | tree | |
3a978d72 | 7206 | tsubst_default_argument (tree fn, tree type, tree arg) |
9188c363 | 7207 | { |
2436b51f MM |
7208 | tree saved_class_ptr = NULL_TREE; |
7209 | tree saved_class_ref = NULL_TREE; | |
7210 | ||
9188c363 MM |
7211 | /* This default argument came from a template. Instantiate the |
7212 | default argument here, not in tsubst. In the case of | |
c8094d83 MS |
7213 | something like: |
7214 | ||
9188c363 MM |
7215 | template <class T> |
7216 | struct S { | |
7217 | static T t(); | |
7218 | void f(T = t()); | |
7219 | }; | |
c8094d83 | 7220 | |
9188c363 | 7221 | we must be careful to do name lookup in the scope of S<T>, |
d5a10cf0 | 7222 | rather than in the current class. */ |
2b59fc25 | 7223 | push_access_scope (fn); |
2436b51f MM |
7224 | /* The "this" pointer is not valid in a default argument. */ |
7225 | if (cfun) | |
7226 | { | |
7227 | saved_class_ptr = current_class_ptr; | |
7228 | cp_function_chain->x_current_class_ptr = NULL_TREE; | |
7229 | saved_class_ref = current_class_ref; | |
7230 | cp_function_chain->x_current_class_ref = NULL_TREE; | |
7231 | } | |
9188c363 | 7232 | |
d5a10cf0 | 7233 | push_deferring_access_checks(dk_no_deferred); |
5a8613b2 MM |
7234 | /* The default argument expression may cause implicitly defined |
7235 | member functions to be synthesized, which will result in garbage | |
7236 | collection. We must treat this situation as if we were within | |
7237 | the body of function so as to avoid collecting live data on the | |
7238 | stack. */ | |
7239 | ++function_depth; | |
c2ea3a40 | 7240 | arg = tsubst_expr (arg, DECL_TI_ARGS (fn), |
015c2c66 MM |
7241 | tf_warning_or_error, NULL_TREE, |
7242 | /*integral_constant_expression_p=*/false); | |
5a8613b2 | 7243 | --function_depth; |
d5a10cf0 MM |
7244 | pop_deferring_access_checks(); |
7245 | ||
2436b51f MM |
7246 | /* Restore the "this" pointer. */ |
7247 | if (cfun) | |
7248 | { | |
7249 | cp_function_chain->x_current_class_ptr = saved_class_ptr; | |
7250 | cp_function_chain->x_current_class_ref = saved_class_ref; | |
7251 | } | |
7252 | ||
2b59fc25 | 7253 | pop_access_scope (fn); |
9188c363 MM |
7254 | |
7255 | /* Make sure the default argument is reasonable. */ | |
7256 | arg = check_default_argument (type, arg); | |
7257 | ||
7258 | return arg; | |
7259 | } | |
7260 | ||
7261 | /* Substitute into all the default arguments for FN. */ | |
7262 | ||
7263 | static void | |
3a978d72 | 7264 | tsubst_default_arguments (tree fn) |
9188c363 MM |
7265 | { |
7266 | tree arg; | |
7267 | tree tmpl_args; | |
7268 | ||
7269 | tmpl_args = DECL_TI_ARGS (fn); | |
7270 | ||
7271 | /* If this function is not yet instantiated, we certainly don't need | |
7272 | its default arguments. */ | |
7273 | if (uses_template_parms (tmpl_args)) | |
7274 | return; | |
7275 | ||
c8094d83 MS |
7276 | for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn)); |
7277 | arg; | |
9188c363 MM |
7278 | arg = TREE_CHAIN (arg)) |
7279 | if (TREE_PURPOSE (arg)) | |
c8094d83 | 7280 | TREE_PURPOSE (arg) = tsubst_default_argument (fn, |
9188c363 MM |
7281 | TREE_VALUE (arg), |
7282 | TREE_PURPOSE (arg)); | |
7283 | } | |
7284 | ||
fc6a28d7 MM |
7285 | /* Substitute the ARGS into the T, which is a _DECL. Return the |
7286 | result of the substitution. Issue error and warning messages under | |
7287 | control of COMPLAIN. */ | |
00d3396f | 7288 | |
e9659ab0 | 7289 | static tree |
fc6a28d7 | 7290 | tsubst_decl (tree t, tree args, tsubst_flags_t complain) |
8d08fdba | 7291 | { |
82a98427 | 7292 | location_t saved_loc; |
b370501f | 7293 | tree r = NULL_TREE; |
4b2811e9 | 7294 | tree in_decl = t; |
830bfa74 | 7295 | |
ae58fa02 | 7296 | /* Set the filename and linenumber to improve error-reporting. */ |
82a98427 | 7297 | saved_loc = input_location; |
f31686a3 | 7298 | input_location = DECL_SOURCE_LOCATION (t); |
b7484fbe | 7299 | |
8d08fdba MS |
7300 | switch (TREE_CODE (t)) |
7301 | { | |
98c1c668 JM |
7302 | case TEMPLATE_DECL: |
7303 | { | |
28e42b7e KL |
7304 | /* We can get here when processing a member function template, |
7305 | member class template, and template template parameter of | |
7306 | a template class. */ | |
98c1c668 | 7307 | tree decl = DECL_TEMPLATE_RESULT (t); |
386b8a85 | 7308 | tree spec; |
28e42b7e KL |
7309 | tree tmpl_args; |
7310 | tree full_args; | |
98c1c668 | 7311 | |
28e42b7e | 7312 | if (DECL_TEMPLATE_TEMPLATE_PARM_P (t)) |
db2767b6 | 7313 | { |
28e42b7e KL |
7314 | /* Template template parameter is treated here. */ |
7315 | tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl); | |
7316 | if (new_type == error_mark_node) | |
7317 | return error_mark_node; | |
36a117a5 | 7318 | |
28e42b7e KL |
7319 | r = copy_decl (t); |
7320 | TREE_CHAIN (r) = NULL_TREE; | |
7321 | TREE_TYPE (r) = new_type; | |
7322 | DECL_TEMPLATE_RESULT (r) | |
7323 | = build_decl (TYPE_DECL, DECL_NAME (decl), new_type); | |
c8094d83 | 7324 | DECL_TEMPLATE_PARMS (r) |
28e42b7e KL |
7325 | = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args, |
7326 | complain); | |
7327 | TYPE_NAME (new_type) = r; | |
7328 | break; | |
7329 | } | |
36a117a5 | 7330 | |
28e42b7e KL |
7331 | /* We might already have an instance of this template. |
7332 | The ARGS are for the surrounding class type, so the | |
7333 | full args contain the tsubst'd args for the context, | |
7334 | plus the innermost args from the template decl. */ | |
c8094d83 | 7335 | tmpl_args = DECL_CLASS_TEMPLATE_P (t) |
28e42b7e KL |
7336 | ? CLASSTYPE_TI_ARGS (TREE_TYPE (t)) |
7337 | : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t)); | |
8ca4bf25 MM |
7338 | /* Because this is a template, the arguments will still be |
7339 | dependent, even after substitution. If | |
7340 | PROCESSING_TEMPLATE_DECL is not set, the dependency | |
7341 | predicates will short-circuit. */ | |
7342 | ++processing_template_decl; | |
28e42b7e KL |
7343 | full_args = tsubst_template_args (tmpl_args, args, |
7344 | complain, in_decl); | |
8ca4bf25 | 7345 | --processing_template_decl; |
bf2f7328 VR |
7346 | if (full_args == error_mark_node) |
7347 | return error_mark_node; | |
28e42b7e KL |
7348 | |
7349 | /* tsubst_template_args doesn't copy the vector if | |
7350 | nothing changed. But, *something* should have | |
7351 | changed. */ | |
7352 | gcc_assert (full_args != tmpl_args); | |
7353 | ||
7354 | spec = retrieve_specialization (t, full_args, | |
7355 | /*class_specializations_p=*/true); | |
7356 | if (spec != NULL_TREE) | |
7357 | { | |
7358 | r = spec; | |
7359 | break; | |
db2767b6 | 7360 | } |
98c1c668 JM |
7361 | |
7362 | /* Make a new template decl. It will be similar to the | |
c8094d83 | 7363 | original, but will record the current template arguments. |
98c1c668 JM |
7364 | We also create a new function declaration, which is just |
7365 | like the old one, but points to this new template, rather | |
7366 | than the old one. */ | |
0acf7199 | 7367 | r = copy_decl (t); |
50bc768d | 7368 | gcc_assert (DECL_LANG_SPECIFIC (r) != 0); |
ae58fa02 | 7369 | TREE_CHAIN (r) = NULL_TREE; |
db2767b6 | 7370 | |
ae58fa02 | 7371 | DECL_TEMPLATE_INFO (r) = build_tree_list (t, args); |
93cdc044 JM |
7372 | |
7373 | if (TREE_CODE (decl) == TYPE_DECL) | |
7374 | { | |
8ca4bf25 MM |
7375 | tree new_type; |
7376 | ++processing_template_decl; | |
7377 | new_type = tsubst (TREE_TYPE (t), args, complain, in_decl); | |
3db45ab5 | 7378 | --processing_template_decl; |
2620d095 KL |
7379 | if (new_type == error_mark_node) |
7380 | return error_mark_node; | |
7381 | ||
ae58fa02 MM |
7382 | TREE_TYPE (r) = new_type; |
7383 | CLASSTYPE_TI_TEMPLATE (new_type) = r; | |
17aec3eb | 7384 | DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type); |
ae58fa02 | 7385 | DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type); |
8ca4bf25 | 7386 | DECL_CONTEXT (r) = TYPE_CONTEXT (new_type); |
93cdc044 JM |
7387 | } |
7388 | else | |
7389 | { | |
8ca4bf25 MM |
7390 | tree new_decl; |
7391 | ++processing_template_decl; | |
7392 | new_decl = tsubst (decl, args, complain, in_decl); | |
7393 | --processing_template_decl; | |
caec1dc0 KL |
7394 | if (new_decl == error_mark_node) |
7395 | return error_mark_node; | |
17aec3eb RK |
7396 | |
7397 | DECL_TEMPLATE_RESULT (r) = new_decl; | |
ae58fa02 MM |
7398 | DECL_TI_TEMPLATE (new_decl) = r; |
7399 | TREE_TYPE (r) = TREE_TYPE (new_decl); | |
7400 | DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl); | |
3db45ab5 | 7401 | DECL_CONTEXT (r) = DECL_CONTEXT (new_decl); |
93cdc044 JM |
7402 | } |
7403 | ||
ae58fa02 MM |
7404 | SET_DECL_IMPLICIT_INSTANTIATION (r); |
7405 | DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE; | |
7406 | DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE; | |
98c1c668 JM |
7407 | |
7408 | /* The template parameters for this new template are all the | |
7409 | template parameters for the old template, except the | |
c6002625 | 7410 | outermost level of parameters. */ |
c8094d83 | 7411 | DECL_TEMPLATE_PARMS (r) |
4393e105 | 7412 | = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args, |
c2ea3a40 | 7413 | complain); |
98c1c668 | 7414 | |
93cdc044 | 7415 | if (PRIMARY_TEMPLATE_P (t)) |
ae58fa02 | 7416 | DECL_PRIMARY_TEMPLATE (r) = r; |
93cdc044 | 7417 | |
8c6ab2db NS |
7418 | if (TREE_CODE (decl) != TYPE_DECL) |
7419 | /* Record this non-type partial instantiation. */ | |
c8094d83 | 7420 | register_specialization (r, t, |
d63d5d0c ILT |
7421 | DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)), |
7422 | false); | |
98c1c668 | 7423 | } |
ae58fa02 | 7424 | break; |
8d08fdba MS |
7425 | |
7426 | case FUNCTION_DECL: | |
7427 | { | |
386b8a85 | 7428 | tree ctx; |
87603ed0 | 7429 | tree argvec = NULL_TREE; |
cf38f48a | 7430 | tree *friends; |
36a117a5 | 7431 | tree gen_tmpl; |
fc6a28d7 | 7432 | tree type; |
5566b478 | 7433 | int member; |
d8c4447d MM |
7434 | int args_depth; |
7435 | int parms_depth; | |
5566b478 | 7436 | |
36a117a5 | 7437 | /* Nobody should be tsubst'ing into non-template functions. */ |
50bc768d | 7438 | gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE); |
36a117a5 MM |
7439 | |
7440 | if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL) | |
7441 | { | |
7442 | tree spec; | |
00cf3e31 MM |
7443 | bool dependent_p; |
7444 | ||
7445 | /* If T is not dependent, just return it. We have to | |
7446 | increment PROCESSING_TEMPLATE_DECL because | |
7447 | value_dependent_expression_p assumes that nothing is | |
7448 | dependent when PROCESSING_TEMPLATE_DECL is zero. */ | |
7449 | ++processing_template_decl; | |
7450 | dependent_p = value_dependent_expression_p (t); | |
7451 | --processing_template_decl; | |
7452 | if (!dependent_p) | |
7453 | return t; | |
36a117a5 MM |
7454 | |
7455 | /* Calculate the most general template of which R is a | |
7456 | specialization, and the complete set of arguments used to | |
7457 | specialize R. */ | |
7458 | gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t)); | |
c8094d83 | 7459 | argvec = tsubst_template_args (DECL_TI_ARGS |
a91db711 | 7460 | (DECL_TEMPLATE_RESULT (gen_tmpl)), |
c8094d83 | 7461 | args, complain, in_decl); |
36a117a5 MM |
7462 | |
7463 | /* Check to see if we already have this specialization. */ | |
c7222c02 MM |
7464 | spec = retrieve_specialization (gen_tmpl, argvec, |
7465 | /*class_specializations_p=*/false); | |
7ddedda4 | 7466 | |
36a117a5 | 7467 | if (spec) |
ae58fa02 MM |
7468 | { |
7469 | r = spec; | |
7470 | break; | |
7471 | } | |
d8c4447d | 7472 | |
f9a7ae04 MM |
7473 | /* We can see more levels of arguments than parameters if |
7474 | there was a specialization of a member template, like | |
7475 | this: | |
7476 | ||
0cbd7506 | 7477 | template <class T> struct S { template <class U> void f(); } |
c8094d83 | 7478 | template <> template <class U> void S<int>::f(U); |
f9a7ae04 | 7479 | |
dc957d14 | 7480 | Here, we'll be substituting into the specialization, |
f9a7ae04 MM |
7481 | because that's where we can find the code we actually |
7482 | want to generate, but we'll have enough arguments for | |
c8094d83 | 7483 | the most general template. |
f9a7ae04 MM |
7484 | |
7485 | We also deal with the peculiar case: | |
d8c4447d | 7486 | |
c8094d83 | 7487 | template <class T> struct S { |
d8c4447d MM |
7488 | template <class U> friend void f(); |
7489 | }; | |
74b846e0 | 7490 | template <class U> void f() {} |
d8c4447d MM |
7491 | template S<int>; |
7492 | template void f<double>(); | |
7493 | ||
7494 | Here, the ARGS for the instantiation of will be {int, | |
7495 | double}. But, we only need as many ARGS as there are | |
7496 | levels of template parameters in CODE_PATTERN. We are | |
7497 | careful not to get fooled into reducing the ARGS in | |
7498 | situations like: | |
7499 | ||
7500 | template <class T> struct S { template <class U> void f(U); } | |
7501 | template <class T> template <> void S<T>::f(int) {} | |
7502 | ||
7503 | which we can spot because the pattern will be a | |
7504 | specialization in this case. */ | |
7505 | args_depth = TMPL_ARGS_DEPTH (args); | |
c8094d83 MS |
7506 | parms_depth = |
7507 | TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t))); | |
d8c4447d MM |
7508 | if (args_depth > parms_depth |
7509 | && !DECL_TEMPLATE_SPECIALIZATION (t)) | |
f9a7ae04 | 7510 | args = get_innermost_template_args (args, parms_depth); |
36a117a5 MM |
7511 | } |
7512 | else | |
7513 | { | |
7514 | /* This special case arises when we have something like this: | |
7515 | ||
0cbd7506 | 7516 | template <class T> struct S { |
c8094d83 | 7517 | friend void f<int>(int, double); |
36a117a5 MM |
7518 | }; |
7519 | ||
7520 | Here, the DECL_TI_TEMPLATE for the friend declaration | |
10b1d5e7 MM |
7521 | will be an IDENTIFIER_NODE. We are being called from |
7522 | tsubst_friend_function, and we want only to create a | |
7523 | new decl (R) with appropriate types so that we can call | |
7524 | determine_specialization. */ | |
36a117a5 MM |
7525 | gen_tmpl = NULL_TREE; |
7526 | } | |
7527 | ||
6eb3bb27 | 7528 | if (DECL_CLASS_SCOPE_P (t)) |
8d08fdba | 7529 | { |
5566b478 MS |
7530 | if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t))) |
7531 | member = 2; | |
7532 | else | |
7533 | member = 1; | |
c8094d83 | 7534 | ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, |
8c6ab2db | 7535 | complain, t, /*entering_scope=*/1); |
5566b478 MS |
7536 | } |
7537 | else | |
7538 | { | |
7539 | member = 0; | |
4f1c5b7d | 7540 | ctx = DECL_CONTEXT (t); |
5566b478 | 7541 | } |
fc6a28d7 | 7542 | type = tsubst (TREE_TYPE (t), args, complain, in_decl); |
b3da7bb1 MM |
7543 | if (type == error_mark_node) |
7544 | return error_mark_node; | |
8d08fdba | 7545 | |
5566b478 | 7546 | /* We do NOT check for matching decls pushed separately at this |
0cbd7506 MS |
7547 | point, as they may not represent instantiations of this |
7548 | template, and in any case are considered separate under the | |
7549 | discrete model. */ | |
0acf7199 | 7550 | r = copy_decl (t); |
e1467ff2 | 7551 | DECL_USE_TEMPLATE (r) = 0; |
5566b478 | 7552 | TREE_TYPE (r) = type; |
92643fea MM |
7553 | /* Clear out the mangled name and RTL for the instantiation. */ |
7554 | SET_DECL_ASSEMBLER_NAME (r, NULL_TREE); | |
7555 | SET_DECL_RTL (r, NULL_RTX); | |
5bd61841 | 7556 | DECL_INITIAL (r) = NULL_TREE; |
4f1c5b7d | 7557 | DECL_CONTEXT (r) = ctx; |
5566b478 | 7558 | |
c8094d83 | 7559 | if (member && DECL_CONV_FN_P (r)) |
1f84ec23 MM |
7560 | /* Type-conversion operator. Reconstruct the name, in |
7561 | case it's the name of one of the template's parameters. */ | |
7562 | DECL_NAME (r) = mangle_conv_op_name_for_type (TREE_TYPE (type)); | |
5566b478 | 7563 | |
4393e105 | 7564 | DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args, |
c2ea3a40 | 7565 | complain, t); |
477f6664 | 7566 | DECL_RESULT (r) = NULL_TREE; |
711734a9 JM |
7567 | |
7568 | TREE_STATIC (r) = 0; | |
7569 | TREE_PUBLIC (r) = TREE_PUBLIC (t); | |
7570 | DECL_EXTERNAL (r) = 1; | |
4684cd27 MM |
7571 | /* If this is an instantiation of a function with internal |
7572 | linkage, we already know what object file linkage will be | |
7573 | assigned to the instantiation. */ | |
7574 | DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r); | |
711734a9 JM |
7575 | DECL_DEFER_OUTPUT (r) = 0; |
7576 | TREE_CHAIN (r) = NULL_TREE; | |
7577 | DECL_PENDING_INLINE_INFO (r) = 0; | |
59026e79 | 7578 | DECL_PENDING_INLINE_P (r) = 0; |
655dc6ee | 7579 | DECL_SAVED_TREE (r) = NULL_TREE; |
711734a9 | 7580 | TREE_USED (r) = 0; |
db9b2174 MM |
7581 | if (DECL_CLONED_FUNCTION (r)) |
7582 | { | |
7583 | DECL_CLONED_FUNCTION (r) = tsubst (DECL_CLONED_FUNCTION (t), | |
c2ea3a40 | 7584 | args, complain, t); |
db9b2174 MM |
7585 | TREE_CHAIN (r) = TREE_CHAIN (DECL_CLONED_FUNCTION (r)); |
7586 | TREE_CHAIN (DECL_CLONED_FUNCTION (r)) = r; | |
7587 | } | |
711734a9 | 7588 | |
92643fea MM |
7589 | /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do |
7590 | this in the special friend case mentioned above where | |
7591 | GEN_TMPL is NULL. */ | |
36a117a5 | 7592 | if (gen_tmpl) |
386b8a85 | 7593 | { |
c8094d83 | 7594 | DECL_TEMPLATE_INFO (r) |
e1b3e07d | 7595 | = tree_cons (gen_tmpl, argvec, NULL_TREE); |
36a117a5 | 7596 | SET_DECL_IMPLICIT_INSTANTIATION (r); |
d63d5d0c | 7597 | register_specialization (r, gen_tmpl, argvec, false); |
36a117a5 | 7598 | |
9188c363 MM |
7599 | /* We're not supposed to instantiate default arguments |
7600 | until they are called, for a template. But, for a | |
7601 | declaration like: | |
7602 | ||
0cbd7506 MS |
7603 | template <class T> void f () |
7604 | { extern void g(int i = T()); } | |
c8094d83 | 7605 | |
9188c363 MM |
7606 | we should do the substitution when the template is |
7607 | instantiated. We handle the member function case in | |
7608 | instantiate_class_template since the default arguments | |
7609 | might refer to other members of the class. */ | |
7610 | if (!member | |
7611 | && !PRIMARY_TEMPLATE_P (gen_tmpl) | |
7612 | && !uses_template_parms (argvec)) | |
7613 | tsubst_default_arguments (r); | |
386b8a85 | 7614 | } |
c43e95f8 MM |
7615 | else |
7616 | DECL_TEMPLATE_INFO (r) = NULL_TREE; | |
f181d4ae | 7617 | |
cf38f48a MM |
7618 | /* Copy the list of befriending classes. */ |
7619 | for (friends = &DECL_BEFRIENDING_CLASSES (r); | |
7620 | *friends; | |
c8094d83 | 7621 | friends = &TREE_CHAIN (*friends)) |
cf38f48a MM |
7622 | { |
7623 | *friends = copy_node (*friends); | |
7624 | TREE_VALUE (*friends) = tsubst (TREE_VALUE (*friends), | |
c2ea3a40 | 7625 | args, complain, |
cf38f48a MM |
7626 | in_decl); |
7627 | } | |
7628 | ||
212e7048 | 7629 | if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r)) |
f181d4ae MM |
7630 | { |
7631 | maybe_retrofit_in_chrg (r); | |
212e7048 MM |
7632 | if (DECL_CONSTRUCTOR_P (r)) |
7633 | grok_ctor_properties (ctx, r); | |
2be678ff JM |
7634 | /* If this is an instantiation of a member template, clone it. |
7635 | If it isn't, that'll be handled by | |
7636 | clone_constructors_and_destructors. */ | |
5e818b93 | 7637 | if (PRIMARY_TEMPLATE_P (gen_tmpl)) |
271e6f02 | 7638 | clone_function_decl (r, /*update_method_vec_p=*/0); |
f181d4ae | 7639 | } |
398cd199 VR |
7640 | else if (IDENTIFIER_OPNAME_P (DECL_NAME (r)) |
7641 | && !grok_op_properties (r, (complain & tf_error) != 0)) | |
7642 | return error_mark_node; | |
c8b2e872 MM |
7643 | |
7644 | if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t)) | |
7645 | SET_DECL_FRIEND_CONTEXT (r, | |
7646 | tsubst (DECL_FRIEND_CONTEXT (t), | |
7647 | args, complain, in_decl)); | |
b9e75696 JM |
7648 | |
7649 | /* Possibly limit visibility based on template args. */ | |
7650 | DECL_VISIBILITY (r) = VISIBILITY_DEFAULT; | |
b70f0f48 JM |
7651 | if (DECL_VISIBILITY_SPECIFIED (t)) |
7652 | { | |
7653 | DECL_VISIBILITY_SPECIFIED (r) = 0; | |
7654 | DECL_ATTRIBUTES (r) | |
7655 | = remove_attribute ("visibility", DECL_ATTRIBUTES (r)); | |
7656 | } | |
b9e75696 | 7657 | determine_visibility (r); |
8d08fdba | 7658 | } |
ae58fa02 | 7659 | break; |
8d08fdba MS |
7660 | |
7661 | case PARM_DECL: | |
7662 | { | |
5d80a306 DG |
7663 | tree type = NULL_TREE; |
7664 | int i, len = 1; | |
7665 | tree expanded_types = NULL_TREE; | |
7666 | tree prev_r = NULL_TREE; | |
7667 | tree first_r = NULL_TREE; | |
1b8899d1 | 7668 | |
5d80a306 DG |
7669 | if (FUNCTION_PARAMETER_PACK_P (t)) |
7670 | { | |
7671 | /* If there is a local specialization that isn't a | |
7672 | parameter pack, it means that we're doing a "simple" | |
7673 | substitution from inside tsubst_pack_expansion. Just | |
7674 | return the local specialiation (which will be a single | |
7675 | parm). */ | |
7676 | tree spec = NULL_TREE; | |
7677 | if (local_specializations) | |
7678 | spec = retrieve_local_specialization (t); | |
7679 | if (spec | |
7680 | && TREE_CODE (spec) == PARM_DECL | |
7681 | && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION) | |
7682 | return spec; | |
7683 | ||
7684 | /* Expand the TYPE_PACK_EXPANSION that provides the types for | |
7685 | the parameters in this function parameter pack. */ | |
7686 | expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args, | |
7687 | complain, in_decl); | |
7688 | if (TREE_CODE (expanded_types) == TREE_VEC) | |
7689 | { | |
7690 | len = TREE_VEC_LENGTH (expanded_types); | |
7691 | ||
7692 | /* Zero-length parameter packs are boring. Just substitute | |
7693 | into the chain. */ | |
7694 | if (len == 0) | |
7695 | return tsubst (TREE_CHAIN (t), args, complain, | |
7696 | TREE_CHAIN (t)); | |
7697 | } | |
7698 | else | |
7699 | { | |
7700 | /* All we did was update the type. Make a note of that. */ | |
7701 | type = expanded_types; | |
7702 | expanded_types = NULL_TREE; | |
7703 | } | |
7704 | } | |
db2767b6 | 7705 | |
5d80a306 DG |
7706 | /* Loop through all of the parameter's we'll build. When T is |
7707 | a function parameter pack, LEN is the number of expanded | |
7708 | types in EXPANDED_TYPES; otherwise, LEN is 1. */ | |
7709 | r = NULL_TREE; | |
7710 | for (i = 0; i < len; ++i) | |
7711 | { | |
7712 | prev_r = r; | |
7713 | r = copy_node (t); | |
7714 | if (DECL_TEMPLATE_PARM_P (t)) | |
7715 | SET_DECL_TEMPLATE_PARM_P (r); | |
7716 | ||
7717 | if (expanded_types) | |
7718 | /* We're on the Ith parameter of the function parameter | |
7719 | pack. */ | |
7720 | { | |
7721 | /* Get the Ith type. */ | |
7722 | type = TREE_VEC_ELT (expanded_types, i); | |
7723 | ||
7724 | if (DECL_NAME (r)) | |
7725 | /* Rename the parameter to include the index. */ | |
7726 | DECL_NAME (r) = | |
7727 | make_ith_pack_parameter_name (DECL_NAME (r), i); | |
7728 | } | |
7729 | else if (!type) | |
7730 | /* We're dealing with a normal parameter. */ | |
7731 | type = tsubst (TREE_TYPE (t), args, complain, in_decl); | |
7732 | ||
7733 | type = type_decays_to (type); | |
7734 | TREE_TYPE (r) = type; | |
7735 | cp_apply_type_quals_to_decl (cp_type_quals (type), r); | |
7736 | ||
7737 | if (DECL_INITIAL (r)) | |
7738 | { | |
7739 | if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX) | |
7740 | DECL_INITIAL (r) = TREE_TYPE (r); | |
7741 | else | |
7742 | DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args, | |
7743 | complain, in_decl); | |
7744 | } | |
7745 | ||
7746 | DECL_CONTEXT (r) = NULL_TREE; | |
7747 | ||
7748 | if (!DECL_TEMPLATE_PARM_P (r)) | |
7749 | DECL_ARG_TYPE (r) = type_passed_as (type); | |
7750 | ||
7751 | /* Keep track of the first new parameter we | |
7752 | generate. That's what will be returned to the | |
7753 | caller. */ | |
7754 | if (!first_r) | |
7755 | first_r = r; | |
7756 | ||
7757 | /* Build a proper chain of parameters when substituting | |
7758 | into a function parameter pack. */ | |
7759 | if (prev_r) | |
7760 | TREE_CHAIN (prev_r) = r; | |
7761 | } | |
8e51619a | 7762 | |
8d08fdba | 7763 | if (TREE_CHAIN (t)) |
4393e105 | 7764 | TREE_CHAIN (r) = tsubst (TREE_CHAIN (t), args, |
c2ea3a40 | 7765 | complain, TREE_CHAIN (t)); |
5d80a306 DG |
7766 | |
7767 | /* FIRST_R contains the start of the chain we've built. */ | |
7768 | r = first_r; | |
8d08fdba | 7769 | } |
ae58fa02 | 7770 | break; |
8d08fdba | 7771 | |
5566b478 MS |
7772 | case FIELD_DECL: |
7773 | { | |
fc6a28d7 MM |
7774 | tree type; |
7775 | ||
0acf7199 | 7776 | r = copy_decl (t); |
fc6a28d7 MM |
7777 | type = tsubst (TREE_TYPE (t), args, complain, in_decl); |
7778 | if (type == error_mark_node) | |
7779 | return error_mark_node; | |
1b8899d1 | 7780 | TREE_TYPE (r) = type; |
9804209d | 7781 | cp_apply_type_quals_to_decl (cp_type_quals (type), r); |
1b8899d1 | 7782 | |
015c2c66 MM |
7783 | /* DECL_INITIAL gives the number of bits in a bit-field. */ |
7784 | DECL_INITIAL (r) | |
7785 | = tsubst_expr (DECL_INITIAL (t), args, | |
7786 | complain, in_decl, | |
7787 | /*integral_constant_expression_p=*/true); | |
1b8899d1 MM |
7788 | /* We don't have to set DECL_CONTEXT here; it is set by |
7789 | finish_member_declaration. */ | |
5566b478 | 7790 | TREE_CHAIN (r) = NULL_TREE; |
c8094d83 | 7791 | if (VOID_TYPE_P (type)) |
dee15844 | 7792 | error ("instantiation of %q+D as type %qT", r, type); |
5566b478 | 7793 | } |
ae58fa02 | 7794 | break; |
5566b478 MS |
7795 | |
7796 | case USING_DECL: | |
98ed9dae NS |
7797 | /* We reach here only for member using decls. */ |
7798 | if (DECL_DEPENDENT_P (t)) | |
7799 | { | |
7800 | r = do_class_using_decl | |
7801 | (tsubst_copy (USING_DECL_SCOPE (t), args, complain, in_decl), | |
7802 | tsubst_copy (DECL_NAME (t), args, complain, in_decl)); | |
7803 | if (!r) | |
7804 | r = error_mark_node; | |
7805 | } | |
7806 | else | |
7807 | { | |
7808 | r = copy_node (t); | |
7809 | TREE_CHAIN (r) = NULL_TREE; | |
7810 | } | |
ae58fa02 | 7811 | break; |
5566b478 | 7812 | |
9188c363 | 7813 | case TYPE_DECL: |
5566b478 MS |
7814 | case VAR_DECL: |
7815 | { | |
1cea0434 KG |
7816 | tree argvec = NULL_TREE; |
7817 | tree gen_tmpl = NULL_TREE; | |
36a117a5 | 7818 | tree spec; |
1cea0434 | 7819 | tree tmpl = NULL_TREE; |
9188c363 | 7820 | tree ctx; |
fc6a28d7 | 7821 | tree type = NULL_TREE; |
f604fc3b | 7822 | bool local_p; |
9188c363 | 7823 | |
fc6a28d7 MM |
7824 | if (TREE_CODE (t) == TYPE_DECL) |
7825 | { | |
7826 | type = tsubst (TREE_TYPE (t), args, complain, in_decl); | |
7827 | if (TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM | |
7828 | || t == TYPE_MAIN_DECL (TREE_TYPE (t))) | |
7829 | { | |
7830 | /* If this is the canonical decl, we don't have to | |
7831 | mess with instantiations, and often we can't (for | |
7832 | typename, template type parms and such). Note that | |
7833 | TYPE_NAME is not correct for the above test if | |
7834 | we've copied the type for a typedef. */ | |
7835 | r = TYPE_NAME (type); | |
7836 | break; | |
7837 | } | |
7838 | } | |
c8094d83 | 7839 | |
f604fc3b MM |
7840 | /* Check to see if we already have the specialization we |
7841 | need. */ | |
7842 | spec = NULL_TREE; | |
7843 | if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t)) | |
7844 | { | |
7845 | /* T is a static data member or namespace-scope entity. | |
7846 | We have to substitute into namespace-scope variables | |
7847 | (even though such entities are never templates) because | |
7848 | of cases like: | |
7849 | ||
7850 | template <class T> void f() { extern T t; } | |
7851 | ||
7852 | where the entity referenced is not known until | |
7853 | instantiation time. */ | |
7854 | local_p = false; | |
7855 | ctx = DECL_CONTEXT (t); | |
7856 | if (DECL_CLASS_SCOPE_P (t)) | |
7857 | { | |
7858 | ctx = tsubst_aggr_type (ctx, args, | |
7859 | complain, | |
7860 | in_decl, /*entering_scope=*/1); | |
7861 | /* If CTX is unchanged, then T is in fact the | |
7862 | specialization we want. That situation occurs when | |
7863 | referencing a static data member within in its own | |
7864 | class. We can use pointer equality, rather than | |
7865 | same_type_p, because DECL_CONTEXT is always | |
7866 | canonical. */ | |
7867 | if (ctx == DECL_CONTEXT (t)) | |
7868 | spec = t; | |
7869 | } | |
5566b478 | 7870 | |
f604fc3b MM |
7871 | if (!spec) |
7872 | { | |
7873 | tmpl = DECL_TI_TEMPLATE (t); | |
7874 | gen_tmpl = most_general_template (tmpl); | |
7875 | argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl); | |
7876 | spec = (retrieve_specialization | |
7877 | (gen_tmpl, argvec, | |
7878 | /*class_specializations_p=*/false)); | |
7879 | } | |
7880 | } | |
9188c363 | 7881 | else |
6dfbb909 | 7882 | { |
f604fc3b MM |
7883 | /* A local variable. */ |
7884 | local_p = true; | |
6dfbb909 MM |
7885 | /* Subsequent calls to pushdecl will fill this in. */ |
7886 | ctx = NULL_TREE; | |
f604fc3b | 7887 | spec = retrieve_local_specialization (t); |
6dfbb909 | 7888 | } |
f604fc3b MM |
7889 | /* If we already have the specialization we need, there is |
7890 | nothing more to do. */ | |
36a117a5 | 7891 | if (spec) |
ae58fa02 MM |
7892 | { |
7893 | r = spec; | |
7894 | break; | |
7895 | } | |
5566b478 | 7896 | |
f604fc3b | 7897 | /* Create a new node for the specialization we need. */ |
0acf7199 | 7898 | r = copy_decl (t); |
edebf865 | 7899 | if (TREE_CODE (r) == VAR_DECL) |
39703eb9 | 7900 | { |
8b0a8500 MM |
7901 | /* Even if the original location is out of scope, the |
7902 | newly substituted one is not. */ | |
7903 | DECL_DEAD_FOR_LOCAL (r) = 0; | |
7904 | DECL_INITIALIZED_P (r) = 0; | |
7905 | DECL_TEMPLATE_INSTANTIATED (r) = 0; | |
fc6a28d7 MM |
7906 | type = tsubst (TREE_TYPE (t), args, complain, in_decl); |
7907 | if (type == error_mark_node) | |
7908 | return error_mark_node; | |
2c05d05e MM |
7909 | if (TREE_CODE (type) == FUNCTION_TYPE) |
7910 | { | |
7911 | /* It may seem that this case cannot occur, since: | |
7912 | ||
7913 | typedef void f(); | |
7914 | void g() { f x; } | |
7915 | ||
7916 | declares a function, not a variable. However: | |
7917 | ||
7918 | typedef void f(); | |
7919 | template <typename T> void g() { T t; } | |
7920 | template void g<f>(); | |
7921 | ||
7922 | is an attempt to declare a variable with function | |
7923 | type. */ | |
7924 | error ("variable %qD has function type", | |
7925 | /* R is not yet sufficiently initialized, so we | |
7926 | just use its name. */ | |
7927 | DECL_NAME (r)); | |
7928 | return error_mark_node; | |
7929 | } | |
39703eb9 MM |
7930 | type = complete_type (type); |
7931 | DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) | |
7932 | = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t); | |
62e19030 | 7933 | type = check_var_type (DECL_NAME (r), type); |
56b4ea3d RH |
7934 | |
7935 | if (DECL_HAS_VALUE_EXPR_P (t)) | |
7936 | { | |
7937 | tree ve = DECL_VALUE_EXPR (t); | |
015c2c66 MM |
7938 | ve = tsubst_expr (ve, args, complain, in_decl, |
7939 | /*constant_expression_p=*/false); | |
3db45ab5 | 7940 | SET_DECL_VALUE_EXPR (r, ve); |
56b4ea3d | 7941 | } |
39703eb9 | 7942 | } |
a3d87771 MM |
7943 | else if (DECL_SELF_REFERENCE_P (t)) |
7944 | SET_DECL_SELF_REFERENCE_P (r); | |
01f4137f | 7945 | TREE_TYPE (r) = type; |
9804209d | 7946 | cp_apply_type_quals_to_decl (cp_type_quals (type), r); |
5566b478 | 7947 | DECL_CONTEXT (r) = ctx; |
92643fea MM |
7948 | /* Clear out the mangled name and RTL for the instantiation. */ |
7949 | SET_DECL_ASSEMBLER_NAME (r, NULL_TREE); | |
820cc88f DB |
7950 | if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL)) |
7951 | SET_DECL_RTL (r, NULL_RTX); | |
8b0a8500 MM |
7952 | /* The initializer must not be expanded until it is required; |
7953 | see [temp.inst]. */ | |
d11ad92e | 7954 | DECL_INITIAL (r) = NULL_TREE; |
820cc88f DB |
7955 | if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL)) |
7956 | SET_DECL_RTL (r, NULL_RTX); | |
06ceef4e | 7957 | DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0; |
b9e75696 JM |
7958 | if (TREE_CODE (r) == VAR_DECL) |
7959 | { | |
7960 | /* Possibly limit visibility based on template args. */ | |
7961 | DECL_VISIBILITY (r) = VISIBILITY_DEFAULT; | |
b70f0f48 JM |
7962 | if (DECL_VISIBILITY_SPECIFIED (t)) |
7963 | { | |
7964 | DECL_VISIBILITY_SPECIFIED (r) = 0; | |
7965 | DECL_ATTRIBUTES (r) | |
7966 | = remove_attribute ("visibility", DECL_ATTRIBUTES (r)); | |
7967 | } | |
b9e75696 JM |
7968 | determine_visibility (r); |
7969 | } | |
5566b478 | 7970 | |
6dfbb909 MM |
7971 | if (!local_p) |
7972 | { | |
7973 | /* A static data member declaration is always marked | |
7974 | external when it is declared in-class, even if an | |
7975 | initializer is present. We mimic the non-template | |
7976 | processing here. */ | |
7977 | DECL_EXTERNAL (r) = 1; | |
fa8d6e85 | 7978 | |
d63d5d0c | 7979 | register_specialization (r, gen_tmpl, argvec, false); |
6dfbb909 MM |
7980 | DECL_TEMPLATE_INFO (r) = tree_cons (tmpl, argvec, NULL_TREE); |
7981 | SET_DECL_IMPLICIT_INSTANTIATION (r); | |
7982 | } | |
9188c363 | 7983 | else |
6dfbb909 | 7984 | register_local_specialization (r, t); |
5566b478 | 7985 | |
5566b478 | 7986 | TREE_CHAIN (r) = NULL_TREE; |
edebf865 | 7987 | layout_decl (r, 0); |
5566b478 | 7988 | } |
ae58fa02 | 7989 | break; |
5566b478 | 7990 | |
ae58fa02 | 7991 | default: |
315fb5db | 7992 | gcc_unreachable (); |
c8094d83 | 7993 | } |
ae58fa02 MM |
7994 | |
7995 | /* Restore the file and line information. */ | |
82a98427 | 7996 | input_location = saved_loc; |
ae58fa02 MM |
7997 | |
7998 | return r; | |
7999 | } | |
8000 | ||
34cd5ae7 | 8001 | /* Substitute into the ARG_TYPES of a function type. */ |
cabc336a | 8002 | |
e9659ab0 | 8003 | static tree |
c8094d83 | 8004 | tsubst_arg_types (tree arg_types, |
0cbd7506 MS |
8005 | tree args, |
8006 | tsubst_flags_t complain, | |
8007 | tree in_decl) | |
cabc336a MM |
8008 | { |
8009 | tree remaining_arg_types; | |
5d80a306 DG |
8010 | tree type = NULL_TREE; |
8011 | int i = 1; | |
8012 | tree expanded_args = NULL_TREE; | |
5e97d404 | 8013 | tree default_arg; |
cabc336a MM |
8014 | |
8015 | if (!arg_types || arg_types == void_list_node) | |
8016 | return arg_types; | |
c8094d83 | 8017 | |
cabc336a | 8018 | remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types), |
4393e105 MM |
8019 | args, complain, in_decl); |
8020 | if (remaining_arg_types == error_mark_node) | |
8021 | return error_mark_node; | |
8022 | ||
5d80a306 | 8023 | if (PACK_EXPANSION_P (TREE_VALUE (arg_types))) |
4b2811e9 | 8024 | { |
5d80a306 DG |
8025 | /* For a pack expansion, perform substitution on the |
8026 | entire expression. Later on, we'll handle the arguments | |
8027 | one-by-one. */ | |
8028 | expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types), | |
8029 | args, complain, in_decl); | |
cabc336a | 8030 | |
5d80a306 DG |
8031 | if (TREE_CODE (expanded_args) == TREE_VEC) |
8032 | /* So that we'll spin through the parameters, one by one. */ | |
8033 | i = TREE_VEC_LENGTH (expanded_args); | |
8034 | else | |
8035 | { | |
8036 | /* We only partially substituted into the parameter | |
8037 | pack. Our type is TYPE_PACK_EXPANSION. */ | |
8038 | type = expanded_args; | |
8039 | expanded_args = NULL_TREE; | |
8040 | } | |
8041 | } | |
cabc336a | 8042 | |
5d80a306 DG |
8043 | while (i > 0) { |
8044 | --i; | |
8045 | ||
8046 | if (expanded_args) | |
8047 | type = TREE_VEC_ELT (expanded_args, i); | |
8048 | else if (!type) | |
8049 | type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl); | |
c8094d83 | 8050 | |
5d80a306 DG |
8051 | if (type == error_mark_node) |
8052 | return error_mark_node; | |
8053 | if (VOID_TYPE_P (type)) | |
8054 | { | |
8055 | if (complain & tf_error) | |
8056 | { | |
8057 | error ("invalid parameter type %qT", type); | |
8058 | if (in_decl) | |
8059 | error ("in declaration %q+D", in_decl); | |
8060 | } | |
8061 | return error_mark_node; | |
5e97d404 | 8062 | } |
5d80a306 DG |
8063 | |
8064 | /* Do array-to-pointer, function-to-pointer conversion, and ignore | |
8065 | top-level qualifiers as required. */ | |
8066 | type = TYPE_MAIN_VARIANT (type_decays_to (type)); | |
c8094d83 | 8067 | |
5d80a306 DG |
8068 | /* We do not substitute into default arguments here. The standard |
8069 | mandates that they be instantiated only when needed, which is | |
8070 | done in build_over_call. */ | |
8071 | default_arg = TREE_PURPOSE (arg_types); | |
8072 | ||
8073 | if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG) | |
8074 | { | |
8075 | /* We've instantiated a template before its default arguments | |
8076 | have been parsed. This can happen for a nested template | |
8077 | class, and is not an error unless we require the default | |
8078 | argument in a call of this function. */ | |
8079 | remaining_arg_types = | |
8080 | tree_cons (default_arg, type, remaining_arg_types); | |
8081 | VEC_safe_push (tree, gc, DEFARG_INSTANTIATIONS (default_arg), | |
8082 | remaining_arg_types); | |
8083 | } | |
8084 | else | |
8085 | remaining_arg_types = | |
8086 | hash_tree_cons (default_arg, type, remaining_arg_types); | |
8087 | } | |
8088 | ||
8089 | return remaining_arg_types; | |
cabc336a MM |
8090 | } |
8091 | ||
4393e105 MM |
8092 | /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does |
8093 | *not* handle the exception-specification for FNTYPE, because the | |
8094 | initial substitution of explicitly provided template parameters | |
8095 | during argument deduction forbids substitution into the | |
8096 | exception-specification: | |
8097 | ||
8098 | [temp.deduct] | |
8099 | ||
8100 | All references in the function type of the function template to the | |
8101 | corresponding template parameters are replaced by the specified tem- | |
8102 | plate argument values. If a substitution in a template parameter or | |
8103 | in the function type of the function template results in an invalid | |
8104 | type, type deduction fails. [Note: The equivalent substitution in | |
8105 | exception specifications is done only when the function is instanti- | |
8106 | ated, at which point a program is ill-formed if the substitution | |
8107 | results in an invalid type.] */ | |
8108 | ||
8109 | static tree | |
c8094d83 | 8110 | tsubst_function_type (tree t, |
0cbd7506 MS |
8111 | tree args, |
8112 | tsubst_flags_t complain, | |
8113 | tree in_decl) | |
4393e105 MM |
8114 | { |
8115 | tree return_type; | |
8116 | tree arg_types; | |
8117 | tree fntype; | |
8118 | ||
8dd3f57a | 8119 | /* The TYPE_CONTEXT is not used for function/method types. */ |
50bc768d | 8120 | gcc_assert (TYPE_CONTEXT (t) == NULL_TREE); |
4393e105 | 8121 | |
46c895ac | 8122 | /* Substitute the return type. */ |
4393e105 MM |
8123 | return_type = tsubst (TREE_TYPE (t), args, complain, in_decl); |
8124 | if (return_type == error_mark_node) | |
8125 | return error_mark_node; | |
6e2993bf MM |
8126 | /* The standard does not presently indicate that creation of a |
8127 | function type with an invalid return type is a deduction failure. | |
270af55d | 8128 | However, that is clearly analogous to creating an array of "void" |
c8094d83 | 8129 | or a reference to a reference. This is core issue #486. */ |
6e2993bf MM |
8130 | if (TREE_CODE (return_type) == ARRAY_TYPE |
8131 | || TREE_CODE (return_type) == FUNCTION_TYPE) | |
8132 | { | |
8133 | if (complain & tf_error) | |
8134 | { | |
8135 | if (TREE_CODE (return_type) == ARRAY_TYPE) | |
8136 | error ("function returning an array"); | |
8137 | else | |
8138 | error ("function returning a function"); | |
8139 | } | |
8140 | return error_mark_node; | |
8141 | } | |
4393e105 | 8142 | |
34cd5ae7 | 8143 | /* Substitute the argument types. */ |
4393e105 | 8144 | arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, |
c8094d83 | 8145 | complain, in_decl); |
4393e105 MM |
8146 | if (arg_types == error_mark_node) |
8147 | return error_mark_node; | |
c8094d83 | 8148 | |
1891dec4 DM |
8149 | if (TYPE_QUALS (return_type) != TYPE_UNQUALIFIED |
8150 | && in_decl != NULL_TREE | |
8151 | && !TREE_NO_WARNING (in_decl) | |
8152 | && (SCALAR_TYPE_P (return_type) || VOID_TYPE_P (return_type))) | |
8153 | warning (OPT_Wreturn_type, | |
8154 | "type qualifiers ignored on function return type"); | |
8155 | ||
4393e105 MM |
8156 | /* Construct a new type node and return it. */ |
8157 | if (TREE_CODE (t) == FUNCTION_TYPE) | |
8158 | fntype = build_function_type (return_type, arg_types); | |
8159 | else | |
8160 | { | |
8161 | tree r = TREE_TYPE (TREE_VALUE (arg_types)); | |
8162 | if (! IS_AGGR_TYPE (r)) | |
8163 | { | |
8164 | /* [temp.deduct] | |
c8094d83 | 8165 | |
4393e105 MM |
8166 | Type deduction may fail for any of the following |
8167 | reasons: | |
c8094d83 | 8168 | |
4393e105 MM |
8169 | -- Attempting to create "pointer to member of T" when T |
8170 | is not a class type. */ | |
c2ea3a40 | 8171 | if (complain & tf_error) |
0f51ccfc | 8172 | error ("creating pointer to member function of non-class type %qT", |
4393e105 MM |
8173 | r); |
8174 | return error_mark_node; | |
8175 | } | |
c8094d83 MS |
8176 | |
8177 | fntype = build_method_type_directly (r, return_type, | |
43dc123f | 8178 | TREE_CHAIN (arg_types)); |
4393e105 | 8179 | } |
c2ea3a40 | 8180 | fntype = cp_build_qualified_type_real (fntype, TYPE_QUALS (t), complain); |
e9525111 | 8181 | fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t)); |
c8094d83 MS |
8182 | |
8183 | return fntype; | |
4393e105 MM |
8184 | } |
8185 | ||
c7222c02 MM |
8186 | /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template |
8187 | ARGS into that specification, and return the substituted | |
8188 | specification. If there is no specification, return NULL_TREE. */ | |
8189 | ||
8190 | static tree | |
c8094d83 MS |
8191 | tsubst_exception_specification (tree fntype, |
8192 | tree args, | |
c7222c02 MM |
8193 | tsubst_flags_t complain, |
8194 | tree in_decl) | |
8195 | { | |
8196 | tree specs; | |
8197 | tree new_specs; | |
8198 | ||
8199 | specs = TYPE_RAISES_EXCEPTIONS (fntype); | |
8200 | new_specs = NULL_TREE; | |
8201 | if (specs) | |
8202 | { | |
8203 | if (! TREE_VALUE (specs)) | |
8204 | new_specs = specs; | |
8205 | else | |
8206 | while (specs) | |
8207 | { | |
8208 | tree spec; | |
5d80a306 DG |
8209 | int i, len = 1; |
8210 | tree expanded_specs = NULL_TREE; | |
8211 | ||
8212 | if (PACK_EXPANSION_P (TREE_VALUE (specs))) | |
8213 | { | |
8214 | /* Expand the pack expansion type. */ | |
8215 | expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs), | |
8216 | args, complain, | |
8217 | in_decl); | |
8218 | len = TREE_VEC_LENGTH (expanded_specs); | |
8219 | } | |
8220 | ||
8221 | for (i = 0; i < len; ++i) | |
8222 | { | |
8223 | if (expanded_specs) | |
8224 | spec = TREE_VEC_ELT (expanded_specs, i); | |
8225 | else | |
8226 | spec = tsubst (TREE_VALUE (specs), args, complain, in_decl); | |
8227 | if (spec == error_mark_node) | |
8228 | return spec; | |
8229 | new_specs = add_exception_specifier (new_specs, spec, | |
8230 | complain); | |
8231 | } | |
8232 | ||
8233 | specs = TREE_CHAIN (specs); | |
c7222c02 MM |
8234 | } |
8235 | } | |
8236 | return new_specs; | |
8237 | } | |
8238 | ||
4393e105 MM |
8239 | /* Take the tree structure T and replace template parameters used |
8240 | therein with the argument vector ARGS. IN_DECL is an associated | |
8241 | decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE. | |
c2ea3a40 NS |
8242 | Issue error and warning messages under control of COMPLAIN. Note |
8243 | that we must be relatively non-tolerant of extensions here, in | |
8244 | order to preserve conformance; if we allow substitutions that | |
8245 | should not be allowed, we may allow argument deductions that should | |
8246 | not succeed, and therefore report ambiguous overload situations | |
8247 | where there are none. In theory, we could allow the substitution, | |
8248 | but indicate that it should have failed, and allow our caller to | |
8249 | make sure that the right thing happens, but we don't try to do this | |
8250 | yet. | |
4393e105 MM |
8251 | |
8252 | This function is used for dealing with types, decls and the like; | |
8253 | for expressions, use tsubst_expr or tsubst_copy. */ | |
ae58fa02 | 8254 | |
14d22dd6 | 8255 | static tree |
3a978d72 | 8256 | tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl) |
ae58fa02 | 8257 | { |
0ecfe0b4 | 8258 | tree type, r; |
ae58fa02 MM |
8259 | |
8260 | if (t == NULL_TREE || t == error_mark_node | |
8261 | || t == integer_type_node | |
8262 | || t == void_type_node | |
8263 | || t == char_type_node | |
539599c1 | 8264 | || t == unknown_type_node |
ae58fa02 MM |
8265 | || TREE_CODE (t) == NAMESPACE_DECL) |
8266 | return t; | |
8267 | ||
fc6a28d7 MM |
8268 | if (DECL_P (t)) |
8269 | return tsubst_decl (t, args, complain); | |
8270 | ||
ae58fa02 MM |
8271 | if (TREE_CODE (t) == IDENTIFIER_NODE) |
8272 | type = IDENTIFIER_TYPE_VALUE (t); | |
8273 | else | |
8274 | type = TREE_TYPE (t); | |
399dedb9 | 8275 | |
50bc768d | 8276 | gcc_assert (type != unknown_type_node); |
ae58fa02 | 8277 | |
fc6a28d7 | 8278 | if (type |
ae58fa02 | 8279 | && TREE_CODE (t) != TYPENAME_TYPE |
4393e105 MM |
8280 | && TREE_CODE (t) != IDENTIFIER_NODE |
8281 | && TREE_CODE (t) != FUNCTION_TYPE | |
8282 | && TREE_CODE (t) != METHOD_TYPE) | |
8283 | type = tsubst (type, args, complain, in_decl); | |
8284 | if (type == error_mark_node) | |
8285 | return error_mark_node; | |
ae58fa02 | 8286 | |
ae58fa02 MM |
8287 | switch (TREE_CODE (t)) |
8288 | { | |
8289 | case RECORD_TYPE: | |
8290 | case UNION_TYPE: | |
8291 | case ENUMERAL_TYPE: | |
4393e105 MM |
8292 | return tsubst_aggr_type (t, args, complain, in_decl, |
8293 | /*entering_scope=*/0); | |
ae58fa02 MM |
8294 | |
8295 | case ERROR_MARK: | |
8296 | case IDENTIFIER_NODE: | |
ae58fa02 MM |
8297 | case VOID_TYPE: |
8298 | case REAL_TYPE: | |
8299 | case COMPLEX_TYPE: | |
c00996a3 | 8300 | case VECTOR_TYPE: |
ae58fa02 MM |
8301 | case BOOLEAN_TYPE: |
8302 | case INTEGER_CST: | |
8303 | case REAL_CST: | |
8304 | case STRING_CST: | |
8305 | return t; | |
8306 | ||
8307 | case INTEGER_TYPE: | |
8308 | if (t == integer_type_node) | |
8309 | return t; | |
8310 | ||
8311 | if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST | |
8312 | && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST) | |
8313 | return t; | |
d2e5ee5c | 8314 | |
5566b478 | 8315 | { |
ddce3528 | 8316 | tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0); |
7ddedda4 | 8317 | |
6ee3ffe8 MM |
8318 | max = tsubst_expr (omax, args, complain, in_decl, |
8319 | /*integral_constant_expression_p=*/false); | |
4ef69b83 | 8320 | max = fold_decl_constant_value (max); |
8dd3f57a | 8321 | |
eff3a276 MM |
8322 | if (TREE_CODE (max) != INTEGER_CST |
8323 | && TREE_CODE (max) != TEMPLATE_PARM_INDEX | |
8324 | && !at_function_scope_p ()) | |
8325 | { | |
8326 | if (complain & tf_error) | |
8327 | error ("array bound is not an integer constant"); | |
8328 | return error_mark_node; | |
8329 | } | |
8330 | ||
95cd6f6f | 8331 | /* [temp.deduct] |
4393e105 | 8332 | |
95cd6f6f JC |
8333 | Type deduction may fail for any of the following |
8334 | reasons: | |
4393e105 | 8335 | |
95cd6f6f JC |
8336 | Attempting to create an array with a size that is |
8337 | zero or negative. */ | |
8338 | if (integer_zerop (max) && !(complain & tf_error)) | |
8339 | /* We must fail if performing argument deduction (as | |
8340 | indicated by the state of complain), so that | |
8341 | another substitution can be found. */ | |
8342 | return error_mark_node; | |
95cd6f6f JC |
8343 | else if (TREE_CODE (max) == INTEGER_CST |
8344 | && INT_CST_LT (max, integer_zero_node)) | |
8345 | { | |
c2ea3a40 | 8346 | if (complain & tf_error) |
95cd6f6f | 8347 | error ("creating array with negative size (%qE)", max); |
4393e105 MM |
8348 | |
8349 | return error_mark_node; | |
0ecfe0b4 JM |
8350 | } |
8351 | ||
c95cd22e | 8352 | return compute_array_index_type (NULL_TREE, max); |
ae58fa02 MM |
8353 | } |
8354 | ||
8355 | case TEMPLATE_TYPE_PARM: | |
8356 | case TEMPLATE_TEMPLATE_PARM: | |
a1281f45 | 8357 | case BOUND_TEMPLATE_TEMPLATE_PARM: |
ae58fa02 MM |
8358 | case TEMPLATE_PARM_INDEX: |
8359 | { | |
8360 | int idx; | |
8361 | int level; | |
8362 | int levels; | |
315fb5db | 8363 | tree arg = NULL_TREE; |
0ecfe0b4 JM |
8364 | |
8365 | r = NULL_TREE; | |
ae58fa02 | 8366 | |
315fb5db | 8367 | gcc_assert (TREE_VEC_LENGTH (args) > 0); |
ae58fa02 | 8368 | if (TREE_CODE (t) == TEMPLATE_TYPE_PARM |
a1281f45 KL |
8369 | || TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM |
8370 | || TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM) | |
ae58fa02 MM |
8371 | { |
8372 | idx = TEMPLATE_TYPE_IDX (t); | |
8373 | level = TEMPLATE_TYPE_LEVEL (t); | |
8374 | } | |
8375 | else | |
8376 | { | |
8377 | idx = TEMPLATE_PARM_IDX (t); | |
8378 | level = TEMPLATE_PARM_LEVEL (t); | |
8379 | } | |
8380 | ||
315fb5db NS |
8381 | levels = TMPL_ARGS_DEPTH (args); |
8382 | if (level <= levels) | |
5d80a306 DG |
8383 | { |
8384 | arg = TMPL_ARG (args, level, idx); | |
8385 | ||
8386 | if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT) | |
8387 | /* See through ARGUMENT_PACK_SELECT arguments. */ | |
8388 | arg = ARGUMENT_PACK_SELECT_ARG (arg); | |
8389 | } | |
ae58fa02 | 8390 | |
315fb5db NS |
8391 | if (arg == error_mark_node) |
8392 | return error_mark_node; | |
8393 | else if (arg != NULL_TREE) | |
8394 | { | |
5d80a306 DG |
8395 | if (ARGUMENT_PACK_P (arg)) |
8396 | /* If ARG is an argument pack, we don't actually want to | |
8397 | perform a substitution here, because substitutions | |
8398 | for argument packs are only done | |
8399 | element-by-element. We can get to this point when | |
8400 | substituting the type of a non-type template | |
8401 | parameter pack, when that type actually contains | |
8402 | template parameter packs from an outer template, e.g., | |
8403 | ||
8404 | template<typename... Types> struct A { | |
8405 | template<Types... Values> struct B { }; | |
8406 | }; */ | |
8407 | return t; | |
8408 | ||
315fb5db | 8409 | if (TREE_CODE (t) == TEMPLATE_TYPE_PARM) |
ae58fa02 | 8410 | { |
9180c238 | 8411 | int quals; |
315fb5db | 8412 | gcc_assert (TYPE_P (arg)); |
9180c238 JM |
8413 | |
8414 | /* cv-quals from the template are discarded when | |
8415 | substituting in a function or reference type. */ | |
8416 | if (TREE_CODE (arg) == FUNCTION_TYPE | |
8417 | || TREE_CODE (arg) == METHOD_TYPE | |
8418 | || TREE_CODE (arg) == REFERENCE_TYPE) | |
8419 | quals = cp_type_quals (arg); | |
8420 | else | |
8421 | quals = cp_type_quals (arg) | cp_type_quals (t); | |
8422 | ||
315fb5db | 8423 | return cp_build_qualified_type_real |
9180c238 | 8424 | (arg, quals, complain | tf_ignore_bad_quals); |
315fb5db NS |
8425 | } |
8426 | else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM) | |
8427 | { | |
8428 | /* We are processing a type constructed from a | |
8429 | template template parameter. */ | |
8430 | tree argvec = tsubst (TYPE_TI_ARGS (t), | |
8431 | args, complain, in_decl); | |
8432 | if (argvec == error_mark_node) | |
8433 | return error_mark_node; | |
c8094d83 | 8434 | |
315fb5db NS |
8435 | /* We can get a TEMPLATE_TEMPLATE_PARM here when we |
8436 | are resolving nested-types in the signature of a | |
8437 | member function templates. Otherwise ARG is a | |
8438 | TEMPLATE_DECL and is the real template to be | |
8439 | instantiated. */ | |
8440 | if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM) | |
8441 | arg = TYPE_NAME (arg); | |
c8094d83 MS |
8442 | |
8443 | r = lookup_template_class (arg, | |
8444 | argvec, in_decl, | |
315fb5db NS |
8445 | DECL_CONTEXT (arg), |
8446 | /*entering_scope=*/0, | |
8447 | complain); | |
8448 | return cp_build_qualified_type_real | |
8449 | (r, TYPE_QUALS (t), complain); | |
ae58fa02 | 8450 | } |
315fb5db NS |
8451 | else |
8452 | /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */ | |
8453 | return arg; | |
ae58fa02 MM |
8454 | } |
8455 | ||
8456 | if (level == 1) | |
8457 | /* This can happen during the attempted tsubst'ing in | |
8458 | unify. This means that we don't yet have any information | |
8459 | about the template parameter in question. */ | |
8460 | return t; | |
8461 | ||
8462 | /* If we get here, we must have been looking at a parm for a | |
8463 | more deeply nested template. Make a new version of this | |
8464 | template parameter, but with a lower level. */ | |
8465 | switch (TREE_CODE (t)) | |
8466 | { | |
8467 | case TEMPLATE_TYPE_PARM: | |
8468 | case TEMPLATE_TEMPLATE_PARM: | |
a1281f45 | 8469 | case BOUND_TEMPLATE_TEMPLATE_PARM: |
89d684bb | 8470 | if (cp_type_quals (t)) |
ae58fa02 | 8471 | { |
9ccf6541 | 8472 | r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl); |
0cbd7506 MS |
8473 | r = cp_build_qualified_type_real |
8474 | (r, cp_type_quals (t), | |
4f2b0fb2 NS |
8475 | complain | (TREE_CODE (t) == TEMPLATE_TYPE_PARM |
8476 | ? tf_ignore_bad_quals : 0)); | |
9ccf6541 MM |
8477 | } |
8478 | else | |
8479 | { | |
11e74ea6 | 8480 | r = copy_type (t); |
9ccf6541 MM |
8481 | TEMPLATE_TYPE_PARM_INDEX (r) |
8482 | = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t), | |
8483 | r, levels); | |
8484 | TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r); | |
8485 | TYPE_MAIN_VARIANT (r) = r; | |
8486 | TYPE_POINTER_TO (r) = NULL_TREE; | |
8487 | TYPE_REFERENCE_TO (r) = NULL_TREE; | |
8488 | ||
06d40de8 DG |
8489 | if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM) |
8490 | /* We have reduced the level of the template | |
8491 | template parameter, but not the levels of its | |
8492 | template parameters, so canonical_type_parameter | |
8493 | will not be able to find the canonical template | |
8494 | template parameter for this level. Thus, we | |
8495 | require structural equality checking to compare | |
8496 | TEMPLATE_TEMPLATE_PARMs. */ | |
8497 | SET_TYPE_STRUCTURAL_EQUALITY (r); | |
3d761c46 DG |
8498 | else if (TYPE_STRUCTURAL_EQUALITY_P (t)) |
8499 | SET_TYPE_STRUCTURAL_EQUALITY (r); | |
06d40de8 DG |
8500 | else |
8501 | TYPE_CANONICAL (r) = canonical_type_parameter (r); | |
8502 | ||
a1281f45 | 8503 | if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM) |
9ccf6541 MM |
8504 | { |
8505 | tree argvec = tsubst (TYPE_TI_ARGS (t), args, | |
c8094d83 | 8506 | complain, in_decl); |
9ccf6541 MM |
8507 | if (argvec == error_mark_node) |
8508 | return error_mark_node; | |
4393e105 | 8509 | |
9ccf6541 MM |
8510 | TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r) |
8511 | = tree_cons (TYPE_TI_TEMPLATE (t), argvec, NULL_TREE); | |
8512 | } | |
ae58fa02 MM |
8513 | } |
8514 | break; | |
8515 | ||
8516 | case TEMPLATE_PARM_INDEX: | |
8517 | r = reduce_template_parm_level (t, type, levels); | |
8518 | break; | |
c8094d83 | 8519 | |
ae58fa02 | 8520 | default: |
315fb5db | 8521 | gcc_unreachable (); |
ae58fa02 MM |
8522 | } |
8523 | ||
5566b478 | 8524 | return r; |
ae58fa02 | 8525 | } |
5566b478 | 8526 | |
8d08fdba MS |
8527 | case TREE_LIST: |
8528 | { | |
058b15c1 | 8529 | tree purpose, value, chain; |
8d08fdba MS |
8530 | |
8531 | if (t == void_list_node) | |
8532 | return t; | |
8533 | ||
8d08fdba MS |
8534 | purpose = TREE_PURPOSE (t); |
8535 | if (purpose) | |
4393e105 MM |
8536 | { |
8537 | purpose = tsubst (purpose, args, complain, in_decl); | |
8538 | if (purpose == error_mark_node) | |
8539 | return error_mark_node; | |
8540 | } | |
8d08fdba MS |
8541 | value = TREE_VALUE (t); |
8542 | if (value) | |
4393e105 MM |
8543 | { |
8544 | value = tsubst (value, args, complain, in_decl); | |
8545 | if (value == error_mark_node) | |
8546 | return error_mark_node; | |
8547 | } | |
8d08fdba MS |
8548 | chain = TREE_CHAIN (t); |
8549 | if (chain && chain != void_type_node) | |
4393e105 MM |
8550 | { |
8551 | chain = tsubst (chain, args, complain, in_decl); | |
8552 | if (chain == error_mark_node) | |
8553 | return error_mark_node; | |
8554 | } | |
8d08fdba MS |
8555 | if (purpose == TREE_PURPOSE (t) |
8556 | && value == TREE_VALUE (t) | |
8557 | && chain == TREE_CHAIN (t)) | |
8558 | return t; | |
058b15c1 | 8559 | return hash_tree_cons (purpose, value, chain); |
8d08fdba | 8560 | } |
c8094d83 | 8561 | |
95b4aca6 | 8562 | case TREE_BINFO: |
bd7eccc4 | 8563 | /* We should never be tsubsting a binfo. */ |
315fb5db | 8564 | gcc_unreachable (); |
85b71cf2 | 8565 | |
95b4aca6 NS |
8566 | case TREE_VEC: |
8567 | /* A vector of template arguments. */ | |
50bc768d | 8568 | gcc_assert (!type); |
a91db711 | 8569 | return tsubst_template_args (t, args, complain, in_decl); |
8d08fdba | 8570 | |
8d08fdba MS |
8571 | case POINTER_TYPE: |
8572 | case REFERENCE_TYPE: | |
8573 | { | |
8d08fdba | 8574 | enum tree_code code; |
79a7c7fa | 8575 | |
46c895ac | 8576 | if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE) |
8d08fdba MS |
8577 | return t; |
8578 | ||
8579 | code = TREE_CODE (t); | |
4393e105 MM |
8580 | |
8581 | ||
8582 | /* [temp.deduct] | |
c8094d83 | 8583 | |
4393e105 | 8584 | Type deduction may fail for any of the following |
c8094d83 | 8585 | reasons: |
4393e105 MM |
8586 | |
8587 | -- Attempting to create a pointer to reference type. | |
8588 | -- Attempting to create a reference to a reference type or | |
8589 | a reference to void. */ | |
0ecfe0b4 JM |
8590 | if (TREE_CODE (type) == REFERENCE_TYPE |
8591 | || (code == REFERENCE_TYPE && TREE_CODE (type) == VOID_TYPE)) | |
79a7c7fa | 8592 | { |
82a98427 | 8593 | static location_t last_loc; |
79a7c7fa JM |
8594 | |
8595 | /* We keep track of the last time we issued this error | |
8596 | message to avoid spewing a ton of messages during a | |
8597 | single bad template instantiation. */ | |
c2ea3a40 | 8598 | if (complain & tf_error |
93409b8c | 8599 | #ifdef USE_MAPPED_LOCATION |
9b60dfd7 | 8600 | && last_loc != input_location |
93409b8c | 8601 | #else |
82a98427 | 8602 | && (last_loc.line != input_line |
9b60dfd7 | 8603 | || last_loc.file != input_filename) |
93409b8c | 8604 | #endif |
9b60dfd7 | 8605 | ) |
79a7c7fa | 8606 | { |
0ecfe0b4 | 8607 | if (TREE_CODE (type) == VOID_TYPE) |
33bd39a2 | 8608 | error ("forming reference to void"); |
0ecfe0b4 | 8609 | else |
0f51ccfc | 8610 | error ("forming %s to reference type %qT", |
0cbd7506 MS |
8611 | (code == POINTER_TYPE) ? "pointer" : "reference", |
8612 | type); | |
82a98427 | 8613 | last_loc = input_location; |
79a7c7fa JM |
8614 | } |
8615 | ||
4393e105 | 8616 | return error_mark_node; |
79a7c7fa JM |
8617 | } |
8618 | else if (code == POINTER_TYPE) | |
46c895ac NS |
8619 | { |
8620 | r = build_pointer_type (type); | |
8621 | if (TREE_CODE (type) == METHOD_TYPE) | |
8622 | r = build_ptrmemfunc_type (r); | |
8623 | } | |
8d08fdba MS |
8624 | else |
8625 | r = build_reference_type (type); | |
adecb3f4 | 8626 | r = cp_build_qualified_type_real (r, TYPE_QUALS (t), complain); |
79a7c7fa | 8627 | |
a5f1c5f8 NS |
8628 | if (r != error_mark_node) |
8629 | /* Will this ever be needed for TYPE_..._TO values? */ | |
8630 | layout_type (r); | |
c8094d83 | 8631 | |
8d08fdba MS |
8632 | return r; |
8633 | } | |
a4443a08 | 8634 | case OFFSET_TYPE: |
0ecfe0b4 | 8635 | { |
4393e105 MM |
8636 | r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl); |
8637 | if (r == error_mark_node || !IS_AGGR_TYPE (r)) | |
8638 | { | |
8639 | /* [temp.deduct] | |
8640 | ||
8641 | Type deduction may fail for any of the following | |
8642 | reasons: | |
c8094d83 | 8643 | |
4393e105 | 8644 | -- Attempting to create "pointer to member of T" when T |
0cbd7506 | 8645 | is not a class type. */ |
c2ea3a40 | 8646 | if (complain & tf_error) |
0f51ccfc | 8647 | error ("creating pointer to member of non-class type %qT", r); |
4393e105 MM |
8648 | return error_mark_node; |
8649 | } | |
46c895ac NS |
8650 | if (TREE_CODE (type) == REFERENCE_TYPE) |
8651 | { | |
4f09be91 | 8652 | if (complain & tf_error) |
0f51ccfc | 8653 | error ("creating pointer to member reference type %qT", type); |
cb6d4a9f VR |
8654 | return error_mark_node; |
8655 | } | |
8656 | if (TREE_CODE (type) == VOID_TYPE) | |
8657 | { | |
8658 | if (complain & tf_error) | |
8659 | error ("creating pointer to member of type void"); | |
46c895ac NS |
8660 | return error_mark_node; |
8661 | } | |
50bc768d | 8662 | gcc_assert (TREE_CODE (type) != METHOD_TYPE); |
46c895ac | 8663 | if (TREE_CODE (type) == FUNCTION_TYPE) |
a5ac359a | 8664 | { |
0cbd7506 MS |
8665 | /* The type of the implicit object parameter gets its |
8666 | cv-qualifiers from the FUNCTION_TYPE. */ | |
a5ac359a | 8667 | tree method_type; |
0cbd7506 MS |
8668 | tree this_type = cp_build_qualified_type (TYPE_MAIN_VARIANT (r), |
8669 | cp_type_quals (type)); | |
8670 | tree memptr; | |
8671 | method_type = build_method_type_directly (this_type, | |
43dc123f MM |
8672 | TREE_TYPE (type), |
8673 | TYPE_ARG_TYPES (type)); | |
0cbd7506 MS |
8674 | memptr = build_ptrmemfunc_type (build_pointer_type (method_type)); |
8675 | return cp_build_qualified_type_real (memptr, cp_type_quals (t), | |
8676 | complain); | |
a5ac359a | 8677 | } |
46c895ac | 8678 | else |
b7a78333 MM |
8679 | return cp_build_qualified_type_real (build_ptrmem_type (r, type), |
8680 | TYPE_QUALS (t), | |
8681 | complain); | |
0ecfe0b4 | 8682 | } |
8d08fdba MS |
8683 | case FUNCTION_TYPE: |
8684 | case METHOD_TYPE: | |
8685 | { | |
c11b6f21 | 8686 | tree fntype; |
c7222c02 | 8687 | tree specs; |
4393e105 MM |
8688 | fntype = tsubst_function_type (t, args, complain, in_decl); |
8689 | if (fntype == error_mark_node) | |
8690 | return error_mark_node; | |
cabc336a | 8691 | |
34cd5ae7 | 8692 | /* Substitute the exception specification. */ |
c8094d83 | 8693 | specs = tsubst_exception_specification (t, args, complain, |
c7222c02 | 8694 | in_decl); |
9f6206d9 VR |
8695 | if (specs == error_mark_node) |
8696 | return error_mark_node; | |
c7222c02 MM |
8697 | if (specs) |
8698 | fntype = build_exception_variant (fntype, specs); | |
c11b6f21 | 8699 | return fntype; |
8d08fdba MS |
8700 | } |
8701 | case ARRAY_TYPE: | |
8702 | { | |
4393e105 MM |
8703 | tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl); |
8704 | if (domain == error_mark_node) | |
8705 | return error_mark_node; | |
8706 | ||
8707 | /* As an optimization, we avoid regenerating the array type if | |
8708 | it will obviously be the same as T. */ | |
8d08fdba MS |
8709 | if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t)) |
8710 | return t; | |
0ecfe0b4 | 8711 | |
c8094d83 | 8712 | /* These checks should match the ones in grokdeclarator. |
4393e105 | 8713 | |
c8094d83 MS |
8714 | [temp.deduct] |
8715 | ||
8716 | The deduction may fail for any of the following reasons: | |
4393e105 MM |
8717 | |
8718 | -- Attempting to create an array with an element type that | |
c8094d83 | 8719 | is void, a function type, or a reference type, or [DR337] |
cfb91b67 | 8720 | an abstract class type. */ |
c8094d83 | 8721 | if (TREE_CODE (type) == VOID_TYPE |
4393e105 MM |
8722 | || TREE_CODE (type) == FUNCTION_TYPE |
8723 | || TREE_CODE (type) == REFERENCE_TYPE) | |
0ecfe0b4 | 8724 | { |
c2ea3a40 | 8725 | if (complain & tf_error) |
0f51ccfc | 8726 | error ("creating array of %qT", type); |
4393e105 | 8727 | return error_mark_node; |
0ecfe0b4 | 8728 | } |
cfb91b67 GB |
8729 | if (CLASS_TYPE_P (type) && CLASSTYPE_PURE_VIRTUALS (type)) |
8730 | { | |
8731 | if (complain & tf_error) | |
c8094d83 | 8732 | error ("creating array of %qT, which is an abstract class type", |
cfb91b67 | 8733 | type); |
c8094d83 | 8734 | return error_mark_node; |
cfb91b67 | 8735 | } |
0ecfe0b4 | 8736 | |
8d08fdba MS |
8737 | r = build_cplus_array_type (type, domain); |
8738 | return r; | |
8739 | } | |
8740 | ||
8d08fdba | 8741 | case PLUS_EXPR: |
5566b478 | 8742 | case MINUS_EXPR: |
4393e105 | 8743 | { |
c2ea3a40 NS |
8744 | tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl); |
8745 | tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl); | |
4393e105 MM |
8746 | |
8747 | if (e1 == error_mark_node || e2 == error_mark_node) | |
8748 | return error_mark_node; | |
8749 | ||
7866705a | 8750 | return fold_build2 (TREE_CODE (t), TREE_TYPE (t), e1, e2); |
4393e105 | 8751 | } |
8d08fdba MS |
8752 | |
8753 | case NEGATE_EXPR: | |
8754 | case NOP_EXPR: | |
4393e105 | 8755 | { |
c2ea3a40 | 8756 | tree e = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl); |
4393e105 MM |
8757 | if (e == error_mark_node) |
8758 | return error_mark_node; | |
8759 | ||
7866705a | 8760 | return fold_build1 (TREE_CODE (t), TREE_TYPE (t), e); |
4393e105 | 8761 | } |
8d08fdba | 8762 | |
5566b478 MS |
8763 | case TYPENAME_TYPE: |
8764 | { | |
4393e105 MM |
8765 | tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain, |
8766 | in_decl, /*entering_scope=*/1); | |
8767 | tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args, | |
c8094d83 | 8768 | complain, in_decl); |
4393e105 MM |
8769 | |
8770 | if (ctx == error_mark_node || f == error_mark_node) | |
8771 | return error_mark_node; | |
ae58fa02 | 8772 | |
bf8f3f93 MM |
8773 | if (!IS_AGGR_TYPE (ctx)) |
8774 | { | |
c2ea3a40 | 8775 | if (complain & tf_error) |
0f51ccfc | 8776 | error ("%qT is not a class, struct, or union type", ctx); |
bf8f3f93 MM |
8777 | return error_mark_node; |
8778 | } | |
8779 | else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx)) | |
8780 | { | |
8781 | /* Normally, make_typename_type does not require that the CTX | |
8782 | have complete type in order to allow things like: | |
c8094d83 | 8783 | |
0cbd7506 | 8784 | template <class T> struct S { typename S<T>::X Y; }; |
ae58fa02 | 8785 | |
bf8f3f93 MM |
8786 | But, such constructs have already been resolved by this |
8787 | point, so here CTX really should have complete type, unless | |
8788 | it's a partial instantiation. */ | |
4393e105 | 8789 | ctx = complete_type (ctx); |
d0f062fb | 8790 | if (!COMPLETE_TYPE_P (ctx)) |
4393e105 | 8791 | { |
c2ea3a40 | 8792 | if (complain & tf_error) |
7a228918 | 8793 | cxx_incomplete_type_error (NULL_TREE, ctx); |
4393e105 MM |
8794 | return error_mark_node; |
8795 | } | |
8796 | } | |
ae58fa02 | 8797 | |
fc6a28d7 | 8798 | f = make_typename_type (ctx, f, typename_type, |
4f2b0fb2 | 8799 | (complain & tf_error) | tf_keep_type_decl); |
f0bcd168 MM |
8800 | if (f == error_mark_node) |
8801 | return f; | |
0cbd7506 MS |
8802 | if (TREE_CODE (f) == TYPE_DECL) |
8803 | { | |
4f2b0fb2 | 8804 | complain |= tf_ignore_bad_quals; |
0cbd7506 MS |
8805 | f = TREE_TYPE (f); |
8806 | } | |
c8094d83 | 8807 | |
fc6a28d7 MM |
8808 | if (TREE_CODE (f) != TYPENAME_TYPE) |
8809 | { | |
8810 | if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE) | |
c8094d83 | 8811 | error ("%qT resolves to %qT, which is not an enumeration type", |
fc6a28d7 MM |
8812 | t, f); |
8813 | else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f)) | |
c8094d83 | 8814 | error ("%qT resolves to %qT, which is is not a class type", |
fc6a28d7 MM |
8815 | t, f); |
8816 | } | |
8817 | ||
0cbd7506 MS |
8818 | return cp_build_qualified_type_real |
8819 | (f, cp_type_quals (f) | cp_type_quals (t), complain); | |
5566b478 | 8820 | } |
c8094d83 | 8821 | |
b8c6534b KL |
8822 | case UNBOUND_CLASS_TEMPLATE: |
8823 | { | |
8824 | tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain, | |
8825 | in_decl, /*entering_scope=*/1); | |
8826 | tree name = TYPE_IDENTIFIER (t); | |
b939a023 | 8827 | tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t)); |
b8c6534b KL |
8828 | |
8829 | if (ctx == error_mark_node || name == error_mark_node) | |
8830 | return error_mark_node; | |
8831 | ||
b939a023 KL |
8832 | if (parm_list) |
8833 | parm_list = tsubst_template_parms (parm_list, args, complain); | |
8834 | return make_unbound_class_template (ctx, name, parm_list, complain); | |
b8c6534b KL |
8835 | } |
8836 | ||
5566b478 | 8837 | case INDIRECT_REF: |
5566b478 | 8838 | case ADDR_EXPR: |
058b15c1 | 8839 | case CALL_EXPR: |
315fb5db | 8840 | gcc_unreachable (); |
5566b478 MS |
8841 | |
8842 | case ARRAY_REF: | |
4393e105 | 8843 | { |
c2ea3a40 | 8844 | tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl); |
015c2c66 MM |
8845 | tree e2 = tsubst_expr (TREE_OPERAND (t, 1), args, complain, in_decl, |
8846 | /*integral_constant_expression_p=*/false); | |
4393e105 MM |
8847 | if (e1 == error_mark_node || e2 == error_mark_node) |
8848 | return error_mark_node; | |
8849 | ||
44de5aeb | 8850 | return build_nt (ARRAY_REF, e1, e2, NULL_TREE, NULL_TREE); |
4393e105 | 8851 | } |
5566b478 | 8852 | |
fc378698 | 8853 | case SCOPE_REF: |
4393e105 | 8854 | { |
c2ea3a40 | 8855 | tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl); |
4393e105 MM |
8856 | tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl); |
8857 | if (e1 == error_mark_node || e2 == error_mark_node) | |
8858 | return error_mark_node; | |
8859 | ||
02ed62dd MM |
8860 | return build_qualified_name (/*type=*/NULL_TREE, |
8861 | e1, e2, QUALIFIED_NAME_IS_TEMPLATE (t)); | |
4393e105 | 8862 | } |
fc378698 | 8863 | |
b894fc05 | 8864 | case TYPEOF_TYPE: |
4393e105 | 8865 | { |
b830b74c | 8866 | tree type; |
4393e105 | 8867 | |
015c2c66 MM |
8868 | type = finish_typeof (tsubst_expr |
8869 | (TYPEOF_TYPE_EXPR (t), args, | |
8870 | complain, in_decl, | |
8871 | /*integral_constant_expression_p=*/false)); | |
b830b74c | 8872 | return cp_build_qualified_type_real (type, |
dce50630 | 8873 | cp_type_quals (t) |
b830b74c | 8874 | | cp_type_quals (type), |
dce50630 | 8875 | complain); |
4393e105 | 8876 | } |
b894fc05 | 8877 | |
5d80a306 DG |
8878 | case TYPE_ARGUMENT_PACK: |
8879 | case NONTYPE_ARGUMENT_PACK: | |
8880 | { | |
8881 | tree r = make_node (TREE_CODE (t)); | |
8882 | tree packed_out = | |
8883 | tsubst_template_args (ARGUMENT_PACK_ARGS (t), | |
8884 | args, | |
8885 | complain, | |
8886 | in_decl); | |
8887 | SET_ARGUMENT_PACK_ARGS (r, packed_out); | |
8888 | ||
8889 | /* For template nontype argument packs, also substitute into | |
8890 | the type. */ | |
8891 | if (TREE_CODE (t) == NONTYPE_ARGUMENT_PACK) | |
8892 | TREE_TYPE (r) = tsubst (TREE_TYPE (t), args, complain, in_decl); | |
8893 | ||
8894 | return r; | |
8895 | } | |
8896 | break; | |
8897 | ||
8d08fdba | 8898 | default: |
9e637a26 | 8899 | sorry ("use of %qs in template", |
8d08fdba MS |
8900 | tree_code_name [(int) TREE_CODE (t)]); |
8901 | return error_mark_node; | |
8902 | } | |
8903 | } | |
8904 | ||
ee76b931 MM |
8905 | /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the |
8906 | type of the expression on the left-hand side of the "." or "->" | |
8907 | operator. */ | |
8908 | ||
8909 | static tree | |
8910 | tsubst_baselink (tree baselink, tree object_type, | |
8911 | tree args, tsubst_flags_t complain, tree in_decl) | |
8912 | { | |
8913 | tree name; | |
8914 | tree qualifying_scope; | |
8915 | tree fns; | |
6f67f000 | 8916 | tree optype; |
ee76b931 MM |
8917 | tree template_args = 0; |
8918 | bool template_id_p = false; | |
8919 | ||
51a203d9 MM |
8920 | /* A baselink indicates a function from a base class. Both the |
8921 | BASELINK_ACCESS_BINFO and the base class referenced may | |
8922 | indicate bases of the template class, rather than the | |
8923 | instantiated class. In addition, lookups that were not | |
8924 | ambiguous before may be ambiguous now. Therefore, we perform | |
8925 | the lookup again. */ | |
ee76b931 | 8926 | qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink)); |
51a203d9 MM |
8927 | qualifying_scope = tsubst (qualifying_scope, args, |
8928 | complain, in_decl); | |
ee76b931 | 8929 | fns = BASELINK_FUNCTIONS (baselink); |
6f67f000 | 8930 | optype = BASELINK_OPTYPE (baselink); |
ee76b931 MM |
8931 | if (TREE_CODE (fns) == TEMPLATE_ID_EXPR) |
8932 | { | |
8933 | template_id_p = true; | |
8934 | template_args = TREE_OPERAND (fns, 1); | |
8935 | fns = TREE_OPERAND (fns, 0); | |
bf12d54d NS |
8936 | if (template_args) |
8937 | template_args = tsubst_template_args (template_args, args, | |
8938 | complain, in_decl); | |
ee76b931 MM |
8939 | } |
8940 | name = DECL_NAME (get_first_fn (fns)); | |
8941 | baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1); | |
c8094d83 | 8942 | |
9b60dfd7 MM |
8943 | /* If lookup found a single function, mark it as used at this |
8944 | point. (If it lookup found multiple functions the one selected | |
8945 | later by overload resolution will be marked as used at that | |
8946 | point.) */ | |
8947 | if (BASELINK_P (baselink)) | |
8948 | fns = BASELINK_FUNCTIONS (baselink); | |
8949 | if (!template_id_p && !really_overloaded_fn (fns)) | |
8950 | mark_used (OVL_CURRENT (fns)); | |
8951 | ||
8952 | /* Add back the template arguments, if present. */ | |
ee76b931 | 8953 | if (BASELINK_P (baselink) && template_id_p) |
c8094d83 | 8954 | BASELINK_FUNCTIONS (baselink) |
ee76b931 MM |
8955 | = build_nt (TEMPLATE_ID_EXPR, |
8956 | BASELINK_FUNCTIONS (baselink), | |
8957 | template_args); | |
6f67f000 MM |
8958 | /* Update the conversion operator type. */ |
8959 | BASELINK_OPTYPE (baselink) | |
8960 | = tsubst (optype, args, complain, in_decl); | |
9b60dfd7 | 8961 | |
ee76b931 MM |
8962 | if (!object_type) |
8963 | object_type = current_class_type; | |
c8094d83 | 8964 | return adjust_result_of_qualified_name_lookup (baselink, |
ee76b931 MM |
8965 | qualifying_scope, |
8966 | object_type); | |
8967 | } | |
8968 | ||
8969 | /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is | |
8970 | true if the qualified-id will be a postfix-expression in-and-of | |
8971 | itself; false if more of the postfix-expression follows the | |
8972 | QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand | |
8973 | of "&". */ | |
8974 | ||
8975 | static tree | |
c8094d83 | 8976 | tsubst_qualified_id (tree qualified_id, tree args, |
ee76b931 MM |
8977 | tsubst_flags_t complain, tree in_decl, |
8978 | bool done, bool address_p) | |
8979 | { | |
8980 | tree expr; | |
8981 | tree scope; | |
8982 | tree name; | |
8983 | bool is_template; | |
8984 | tree template_args; | |
8985 | ||
50bc768d | 8986 | gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF); |
ee76b931 | 8987 | |
ee76b931 MM |
8988 | /* Figure out what name to look up. */ |
8989 | name = TREE_OPERAND (qualified_id, 1); | |
8990 | if (TREE_CODE (name) == TEMPLATE_ID_EXPR) | |
8991 | { | |
8992 | is_template = true; | |
bf12d54d NS |
8993 | template_args = TREE_OPERAND (name, 1); |
8994 | if (template_args) | |
8995 | template_args = tsubst_template_args (template_args, args, | |
8996 | complain, in_decl); | |
ee76b931 MM |
8997 | name = TREE_OPERAND (name, 0); |
8998 | } | |
8999 | else | |
9000 | { | |
9001 | is_template = false; | |
9002 | template_args = NULL_TREE; | |
9003 | } | |
9004 | ||
6eeba0cc MM |
9005 | /* Substitute into the qualifying scope. When there are no ARGS, we |
9006 | are just trying to simplify a non-dependent expression. In that | |
9007 | case the qualifying scope may be dependent, and, in any case, | |
9008 | substituting will not help. */ | |
9009 | scope = TREE_OPERAND (qualified_id, 0); | |
9010 | if (args) | |
9011 | { | |
9012 | scope = tsubst (scope, args, complain, in_decl); | |
9013 | expr = tsubst_copy (name, args, complain, in_decl); | |
9014 | } | |
9015 | else | |
9016 | expr = name; | |
10b1d5e7 | 9017 | |
ab73670a | 9018 | if (dependent_type_p (scope)) |
3db45ab5 MS |
9019 | return build_qualified_name (/*type=*/NULL_TREE, |
9020 | scope, expr, | |
02ed62dd | 9021 | QUALIFIED_NAME_IS_TEMPLATE (qualified_id)); |
c8094d83 | 9022 | |
5e08432e | 9023 | if (!BASELINK_P (name) && !DECL_P (expr)) |
12483c9f | 9024 | { |
8ca4bf25 MM |
9025 | if (TREE_CODE (expr) == BIT_NOT_EXPR) |
9026 | /* If this were actually a destructor call, it would have been | |
9027 | parsed as such by the parser. */ | |
9028 | expr = error_mark_node; | |
9029 | else | |
9030 | expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false); | |
12483c9f NS |
9031 | if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL |
9032 | ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL) | |
9033 | { | |
9034 | if (complain & tf_error) | |
b2693faf | 9035 | { |
0f51ccfc | 9036 | error ("dependent-name %qE is parsed as a non-type, but " |
b2693faf | 9037 | "instantiation yields a type", qualified_id); |
0f51ccfc | 9038 | inform ("say %<typename %E%> if a type is meant", qualified_id); |
b2693faf | 9039 | } |
12483c9f NS |
9040 | return error_mark_node; |
9041 | } | |
9042 | } | |
c8094d83 | 9043 | |
279b8466 | 9044 | if (DECL_P (expr)) |
8f78f01f MM |
9045 | { |
9046 | check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE, | |
9047 | scope); | |
9048 | /* Remember that there was a reference to this entity. */ | |
9049 | mark_used (expr); | |
9050 | } | |
9051 | ||
9052 | if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST) | |
9053 | { | |
9054 | if (complain & tf_error) | |
c8094d83 | 9055 | qualified_name_lookup_error (scope, |
8f78f01f MM |
9056 | TREE_OPERAND (qualified_id, 1), |
9057 | expr); | |
9058 | return error_mark_node; | |
9059 | } | |
ee76b931 MM |
9060 | |
9061 | if (is_template) | |
10b1d5e7 | 9062 | expr = lookup_template_function (expr, template_args); |
ee76b931 | 9063 | |
22038b2c | 9064 | if (expr == error_mark_node && complain & tf_error) |
8f78f01f MM |
9065 | qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1), |
9066 | expr); | |
22038b2c | 9067 | else if (TYPE_P (scope)) |
ee76b931 | 9068 | { |
c8094d83 | 9069 | expr = (adjust_result_of_qualified_name_lookup |
ee76b931 | 9070 | (expr, scope, current_class_type)); |
3db45ab5 | 9071 | expr = (finish_qualified_id_expr |
02ed62dd MM |
9072 | (scope, expr, done, address_p, |
9073 | QUALIFIED_NAME_IS_TEMPLATE (qualified_id), | |
9074 | /*template_arg_p=*/false)); | |
ee76b931 | 9075 | } |
c8094d83 | 9076 | |
015c2c66 MM |
9077 | /* Expressions do not generally have reference type. */ |
9078 | if (TREE_CODE (expr) != SCOPE_REF | |
9079 | /* However, if we're about to form a pointer-to-member, we just | |
9080 | want the referenced member referenced. */ | |
9081 | && TREE_CODE (expr) != OFFSET_REF) | |
aec5ba60 | 9082 | expr = convert_from_reference (expr); |
ee76b931 MM |
9083 | |
9084 | return expr; | |
9085 | } | |
9086 | ||
00d3396f JM |
9087 | /* Like tsubst, but deals with expressions. This function just replaces |
9088 | template parms; to finish processing the resultant expression, use | |
9089 | tsubst_expr. */ | |
9090 | ||
14d22dd6 | 9091 | static tree |
3a978d72 | 9092 | tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl) |
5566b478 MS |
9093 | { |
9094 | enum tree_code code; | |
8452b1d3 | 9095 | tree r; |
8d08fdba | 9096 | |
5566b478 MS |
9097 | if (t == NULL_TREE || t == error_mark_node) |
9098 | return t; | |
9099 | ||
9100 | code = TREE_CODE (t); | |
b7484fbe | 9101 | |
5566b478 MS |
9102 | switch (code) |
9103 | { | |
9104 | case PARM_DECL: | |
a723baf1 | 9105 | r = retrieve_local_specialization (t); |
50bc768d | 9106 | gcc_assert (r != NULL); |
5d80a306 DG |
9107 | if (TREE_CODE (r) == ARGUMENT_PACK_SELECT) |
9108 | r = ARGUMENT_PACK_SELECT_ARG (r); | |
c0694c4b | 9109 | mark_used (r); |
a723baf1 | 9110 | return r; |
5566b478 MS |
9111 | |
9112 | case CONST_DECL: | |
ed44da02 MM |
9113 | { |
9114 | tree enum_type; | |
9115 | tree v; | |
9116 | ||
a723baf1 MM |
9117 | if (DECL_TEMPLATE_PARM_P (t)) |
9118 | return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl); | |
7b6d72fc MM |
9119 | /* There is no need to substitute into namespace-scope |
9120 | enumerators. */ | |
9121 | if (DECL_NAMESPACE_SCOPE_P (t)) | |
ed44da02 | 9122 | return t; |
d5a10cf0 MM |
9123 | /* If ARGS is NULL, then T is known to be non-dependent. */ |
9124 | if (args == NULL_TREE) | |
8a784e4a | 9125 | return integral_constant_value (t); |
ed44da02 MM |
9126 | |
9127 | /* Unfortunately, we cannot just call lookup_name here. | |
9188c363 | 9128 | Consider: |
c8094d83 | 9129 | |
9188c363 MM |
9130 | template <int I> int f() { |
9131 | enum E { a = I }; | |
9132 | struct S { void g() { E e = a; } }; | |
9133 | }; | |
c8094d83 | 9134 | |
9188c363 MM |
9135 | When we instantiate f<7>::S::g(), say, lookup_name is not |
9136 | clever enough to find f<7>::a. */ | |
c8094d83 MS |
9137 | enum_type |
9138 | = tsubst_aggr_type (TREE_TYPE (t), args, complain, in_decl, | |
ed44da02 MM |
9139 | /*entering_scope=*/0); |
9140 | ||
c8094d83 MS |
9141 | for (v = TYPE_VALUES (enum_type); |
9142 | v != NULL_TREE; | |
ed44da02 MM |
9143 | v = TREE_CHAIN (v)) |
9144 | if (TREE_PURPOSE (v) == DECL_NAME (t)) | |
9145 | return TREE_VALUE (v); | |
9146 | ||
9147 | /* We didn't find the name. That should never happen; if | |
9148 | name-lookup found it during preliminary parsing, we | |
9149 | should find it again here during instantiation. */ | |
315fb5db | 9150 | gcc_unreachable (); |
ed44da02 | 9151 | } |
db04386f | 9152 | return t; |
ed44da02 | 9153 | |
5566b478 MS |
9154 | case FIELD_DECL: |
9155 | if (DECL_CONTEXT (t)) | |
9156 | { | |
0978790f | 9157 | tree ctx; |
0978790f | 9158 | |
4393e105 | 9159 | ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl, |
36a117a5 | 9160 | /*entering_scope=*/1); |
0978790f | 9161 | if (ctx != DECL_CONTEXT (t)) |
bad1f462 KL |
9162 | { |
9163 | tree r = lookup_field (ctx, DECL_NAME (t), 0, false); | |
9164 | if (!r) | |
9165 | { | |
9166 | if (complain & tf_error) | |
9167 | error ("using invalid field %qD", t); | |
9168 | return error_mark_node; | |
9169 | } | |
9170 | return r; | |
9171 | } | |
5566b478 | 9172 | } |
c8094d83 | 9173 | |
5566b478 MS |
9174 | return t; |
9175 | ||
9176 | case VAR_DECL: | |
9177 | case FUNCTION_DECL: | |
a723baf1 MM |
9178 | if ((DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)) |
9179 | || local_variable_p (t)) | |
4393e105 | 9180 | t = tsubst (t, args, complain, in_decl); |
5566b478 MS |
9181 | mark_used (t); |
9182 | return t; | |
9183 | ||
a723baf1 | 9184 | case BASELINK: |
ee76b931 | 9185 | return tsubst_baselink (t, current_class_type, args, complain, in_decl); |
a723baf1 | 9186 | |
98c1c668 | 9187 | case TEMPLATE_DECL: |
a723baf1 | 9188 | if (DECL_TEMPLATE_TEMPLATE_PARM_P (t)) |
c8094d83 | 9189 | return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)), |
a723baf1 | 9190 | args, complain, in_decl); |
c7222c02 | 9191 | else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t)) |
4393e105 | 9192 | return tsubst (t, args, complain, in_decl); |
fcea7401 KL |
9193 | else if (DECL_CLASS_SCOPE_P (t) |
9194 | && uses_template_parms (DECL_CONTEXT (t))) | |
9195 | { | |
9196 | /* Template template argument like the following example need | |
9197 | special treatment: | |
9198 | ||
9199 | template <template <class> class TT> struct C {}; | |
9200 | template <class T> struct D { | |
9201 | template <class U> struct E {}; | |
0cbd7506 | 9202 | C<E> c; // #1 |
fcea7401 KL |
9203 | }; |
9204 | D<int> d; // #2 | |
9205 | ||
9206 | We are processing the template argument `E' in #1 for | |
9207 | the template instantiation #2. Originally, `E' is a | |
9208 | TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we | |
9209 | have to substitute this with one having context `D<int>'. */ | |
9210 | ||
9211 | tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl); | |
9212 | return lookup_field (context, DECL_NAME(t), 0, false); | |
9213 | } | |
98c1c668 | 9214 | else |
fcea7401 | 9215 | /* Ordinary template template argument. */ |
98c1c668 JM |
9216 | return t; |
9217 | ||
5566b478 MS |
9218 | case CAST_EXPR: |
9219 | case REINTERPRET_CAST_EXPR: | |
e92cc029 MS |
9220 | case CONST_CAST_EXPR: |
9221 | case STATIC_CAST_EXPR: | |
9222 | case DYNAMIC_CAST_EXPR: | |
51924768 | 9223 | case NOP_EXPR: |
5566b478 | 9224 | return build1 |
4393e105 MM |
9225 | (code, tsubst (TREE_TYPE (t), args, complain, in_decl), |
9226 | tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl)); | |
5566b478 | 9227 | |
5d80a306 DG |
9228 | case SIZEOF_EXPR: |
9229 | if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))) | |
9230 | { | |
9231 | /* We only want to compute the number of arguments. */ | |
9232 | tree expanded = tsubst_pack_expansion (TREE_OPERAND (t, 0), args, | |
9233 | complain, in_decl); | |
9234 | return build_int_cst (size_type_node, TREE_VEC_LENGTH (expanded)); | |
9235 | } | |
9236 | /* Fall through */ | |
9237 | ||
5566b478 | 9238 | case INDIRECT_REF: |
5566b478 MS |
9239 | case NEGATE_EXPR: |
9240 | case TRUTH_NOT_EXPR: | |
b87692e5 | 9241 | case BIT_NOT_EXPR: |
5566b478 | 9242 | case ADDR_EXPR: |
392e3d51 | 9243 | case UNARY_PLUS_EXPR: /* Unary + */ |
abff8e06 | 9244 | case ALIGNOF_EXPR: |
5566b478 | 9245 | case ARROW_EXPR: |
fc378698 | 9246 | case THROW_EXPR: |
5156628f | 9247 | case TYPEID_EXPR: |
f5733617 SS |
9248 | case REALPART_EXPR: |
9249 | case IMAGPART_EXPR: | |
5566b478 | 9250 | return build1 |
6a629cac | 9251 | (code, tsubst (TREE_TYPE (t), args, complain, in_decl), |
4393e105 | 9252 | tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl)); |
5566b478 | 9253 | |
a723baf1 MM |
9254 | case COMPONENT_REF: |
9255 | { | |
9256 | tree object; | |
9257 | tree name; | |
9258 | ||
9259 | object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl); | |
9260 | name = TREE_OPERAND (t, 1); | |
c8094d83 | 9261 | if (TREE_CODE (name) == BIT_NOT_EXPR) |
a723baf1 MM |
9262 | { |
9263 | name = tsubst_copy (TREE_OPERAND (name, 0), args, | |
9264 | complain, in_decl); | |
9265 | name = build1 (BIT_NOT_EXPR, NULL_TREE, name); | |
9266 | } | |
9267 | else if (TREE_CODE (name) == SCOPE_REF | |
9268 | && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR) | |
9269 | { | |
9270 | tree base = tsubst_copy (TREE_OPERAND (name, 0), args, | |
9271 | complain, in_decl); | |
9272 | name = TREE_OPERAND (name, 1); | |
9273 | name = tsubst_copy (TREE_OPERAND (name, 0), args, | |
9274 | complain, in_decl); | |
9275 | name = build1 (BIT_NOT_EXPR, NULL_TREE, name); | |
02ed62dd | 9276 | name = build_qualified_name (/*type=*/NULL_TREE, |
3db45ab5 | 9277 | base, name, |
02ed62dd | 9278 | /*template_p=*/false); |
a723baf1 | 9279 | } |
ee76b931 | 9280 | else if (TREE_CODE (name) == BASELINK) |
c8094d83 MS |
9281 | name = tsubst_baselink (name, |
9282 | non_reference (TREE_TYPE (object)), | |
9283 | args, complain, | |
ee76b931 | 9284 | in_decl); |
a723baf1 | 9285 | else |
ee76b931 | 9286 | name = tsubst_copy (name, args, complain, in_decl); |
44de5aeb | 9287 | return build_nt (COMPONENT_REF, object, name, NULL_TREE); |
a723baf1 MM |
9288 | } |
9289 | ||
5566b478 MS |
9290 | case PLUS_EXPR: |
9291 | case MINUS_EXPR: | |
9292 | case MULT_EXPR: | |
9293 | case TRUNC_DIV_EXPR: | |
9294 | case CEIL_DIV_EXPR: | |
9295 | case FLOOR_DIV_EXPR: | |
9296 | case ROUND_DIV_EXPR: | |
9297 | case EXACT_DIV_EXPR: | |
9298 | case BIT_AND_EXPR: | |
5566b478 MS |
9299 | case BIT_IOR_EXPR: |
9300 | case BIT_XOR_EXPR: | |
9301 | case TRUNC_MOD_EXPR: | |
9302 | case FLOOR_MOD_EXPR: | |
9303 | case TRUTH_ANDIF_EXPR: | |
9304 | case TRUTH_ORIF_EXPR: | |
9305 | case TRUTH_AND_EXPR: | |
9306 | case TRUTH_OR_EXPR: | |
9307 | case RSHIFT_EXPR: | |
9308 | case LSHIFT_EXPR: | |
9309 | case RROTATE_EXPR: | |
9310 | case LROTATE_EXPR: | |
9311 | case EQ_EXPR: | |
9312 | case NE_EXPR: | |
9313 | case MAX_EXPR: | |
9314 | case MIN_EXPR: | |
9315 | case LE_EXPR: | |
9316 | case GE_EXPR: | |
9317 | case LT_EXPR: | |
9318 | case GT_EXPR: | |
5566b478 | 9319 | case COMPOUND_EXPR: |
5566b478 MS |
9320 | case DOTSTAR_EXPR: |
9321 | case MEMBER_REF: | |
519c9806 MM |
9322 | case PREDECREMENT_EXPR: |
9323 | case PREINCREMENT_EXPR: | |
9324 | case POSTDECREMENT_EXPR: | |
9325 | case POSTINCREMENT_EXPR: | |
5566b478 | 9326 | return build_nt |
4393e105 MM |
9327 | (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl), |
9328 | tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl)); | |
5566b478 | 9329 | |
02ed62dd MM |
9330 | case SCOPE_REF: |
9331 | return build_qualified_name (/*type=*/NULL_TREE, | |
9332 | tsubst_copy (TREE_OPERAND (t, 0), | |
9333 | args, complain, in_decl), | |
9334 | tsubst_copy (TREE_OPERAND (t, 1), | |
9335 | args, complain, in_decl), | |
9336 | QUALIFIED_NAME_IS_TEMPLATE (t)); | |
9337 | ||
d8987adb NS |
9338 | case ARRAY_REF: |
9339 | return build_nt | |
9340 | (ARRAY_REF, | |
9341 | tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl), | |
9342 | tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl), | |
9343 | NULL_TREE, NULL_TREE); | |
9344 | ||
5566b478 | 9345 | case CALL_EXPR: |
5039610b SL |
9346 | { |
9347 | int n = VL_EXP_OPERAND_LENGTH (t); | |
9348 | tree result = build_vl_exp (CALL_EXPR, n); | |
9349 | int i; | |
9350 | for (i = 0; i < n; i++) | |
9351 | TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args, | |
9352 | complain, in_decl); | |
9353 | return result; | |
9354 | } | |
5566b478 | 9355 | |
5566b478 MS |
9356 | case COND_EXPR: |
9357 | case MODOP_EXPR: | |
40242ccf | 9358 | case PSEUDO_DTOR_EXPR: |
67da3287 | 9359 | { |
8452b1d3 | 9360 | r = build_nt |
4393e105 MM |
9361 | (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl), |
9362 | tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl), | |
9363 | tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl)); | |
e4c2c34b | 9364 | TREE_NO_WARNING (r) = TREE_NO_WARNING (t); |
67da3287 MM |
9365 | return r; |
9366 | } | |
5566b478 MS |
9367 | |
9368 | case NEW_EXPR: | |
9369 | { | |
8452b1d3 | 9370 | r = build_nt |
4393e105 MM |
9371 | (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl), |
9372 | tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl), | |
9373 | tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl)); | |
5566b478 MS |
9374 | NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t); |
9375 | return r; | |
9376 | } | |
9377 | ||
9378 | case DELETE_EXPR: | |
9379 | { | |
8452b1d3 | 9380 | r = build_nt |
4393e105 MM |
9381 | (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl), |
9382 | tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl)); | |
5566b478 MS |
9383 | DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t); |
9384 | DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t); | |
9385 | return r; | |
9386 | } | |
9387 | ||
386b8a85 JM |
9388 | case TEMPLATE_ID_EXPR: |
9389 | { | |
0cbd7506 | 9390 | /* Substituted template arguments */ |
a91db711 NS |
9391 | tree fn = TREE_OPERAND (t, 0); |
9392 | tree targs = TREE_OPERAND (t, 1); | |
856216bb | 9393 | |
a91db711 | 9394 | fn = tsubst_copy (fn, args, complain, in_decl); |
bf12d54d NS |
9395 | if (targs) |
9396 | targs = tsubst_template_args (targs, args, complain, in_decl); | |
c8094d83 | 9397 | |
a91db711 | 9398 | return lookup_template_function (fn, targs); |
386b8a85 JM |
9399 | } |
9400 | ||
5566b478 MS |
9401 | case TREE_LIST: |
9402 | { | |
9403 | tree purpose, value, chain; | |
9404 | ||
9405 | if (t == void_list_node) | |
9406 | return t; | |
9407 | ||
9408 | purpose = TREE_PURPOSE (t); | |
9409 | if (purpose) | |
4393e105 | 9410 | purpose = tsubst_copy (purpose, args, complain, in_decl); |
5566b478 MS |
9411 | value = TREE_VALUE (t); |
9412 | if (value) | |
4393e105 | 9413 | value = tsubst_copy (value, args, complain, in_decl); |
5566b478 MS |
9414 | chain = TREE_CHAIN (t); |
9415 | if (chain && chain != void_type_node) | |
4393e105 | 9416 | chain = tsubst_copy (chain, args, complain, in_decl); |
5566b478 MS |
9417 | if (purpose == TREE_PURPOSE (t) |
9418 | && value == TREE_VALUE (t) | |
9419 | && chain == TREE_CHAIN (t)) | |
9420 | return t; | |
9421 | return tree_cons (purpose, value, chain); | |
9422 | } | |
9423 | ||
9424 | case RECORD_TYPE: | |
9425 | case UNION_TYPE: | |
9426 | case ENUMERAL_TYPE: | |
9427 | case INTEGER_TYPE: | |
9428 | case TEMPLATE_TYPE_PARM: | |
73b0fce8 | 9429 | case TEMPLATE_TEMPLATE_PARM: |
a1281f45 | 9430 | case BOUND_TEMPLATE_TEMPLATE_PARM: |
f84b4be9 | 9431 | case TEMPLATE_PARM_INDEX: |
5566b478 MS |
9432 | case POINTER_TYPE: |
9433 | case REFERENCE_TYPE: | |
9434 | case OFFSET_TYPE: | |
9435 | case FUNCTION_TYPE: | |
9436 | case METHOD_TYPE: | |
9437 | case ARRAY_TYPE: | |
9438 | case TYPENAME_TYPE: | |
b8c6534b | 9439 | case UNBOUND_CLASS_TEMPLATE: |
2a2b1d56 | 9440 | case TYPEOF_TYPE: |
f84b4be9 | 9441 | case TYPE_DECL: |
4393e105 | 9442 | return tsubst (t, args, complain, in_decl); |
5566b478 | 9443 | |
e92cc029 | 9444 | case IDENTIFIER_NODE: |
421844e7 | 9445 | if (IDENTIFIER_TYPENAME_P (t)) |
1f6e1acc AS |
9446 | { |
9447 | tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl); | |
1f84ec23 | 9448 | return mangle_conv_op_name_for_type (new_type); |
1f6e1acc | 9449 | } |
e92cc029 MS |
9450 | else |
9451 | return t; | |
9452 | ||
5156628f | 9453 | case CONSTRUCTOR: |
4038c495 GB |
9454 | /* This is handled by tsubst_copy_and_build. */ |
9455 | gcc_unreachable (); | |
5156628f | 9456 | |
371534a9 | 9457 | case VA_ARG_EXPR: |
ea333e1c | 9458 | return build_x_va_arg (tsubst_copy (TREE_OPERAND (t, 0), args, complain, |
c2ea3a40 NS |
9459 | in_decl), |
9460 | tsubst (TREE_TYPE (t), args, complain, in_decl)); | |
f9817201 | 9461 | |
543a0daa RH |
9462 | case CLEANUP_POINT_EXPR: |
9463 | /* We shouldn't have built any of these during initial template | |
9464 | generation. Instead, they should be built during instantiation | |
9465 | in response to the saved STMT_IS_FULL_EXPR_P setting. */ | |
315fb5db | 9466 | gcc_unreachable (); |
543a0daa | 9467 | |
7eab6e7b MM |
9468 | case OFFSET_REF: |
9469 | mark_used (TREE_OPERAND (t, 1)); | |
9470 | return t; | |
9471 | ||
5d80a306 DG |
9472 | case EXPR_PACK_EXPANSION: |
9473 | error ("invalid use of pack expansion expression"); | |
9474 | return error_mark_node; | |
9475 | ||
9476 | case NONTYPE_ARGUMENT_PACK: | |
9477 | error ("use %<...%> to expand argument pack"); | |
9478 | return error_mark_node; | |
9479 | ||
5566b478 MS |
9480 | default: |
9481 | return t; | |
9482 | } | |
9483 | } | |
9484 | ||
1799e5d5 RH |
9485 | /* Like tsubst_copy, but specifically for OpenMP clauses. */ |
9486 | ||
9487 | static tree | |
9488 | tsubst_omp_clauses (tree clauses, tree args, tsubst_flags_t complain, | |
9489 | tree in_decl) | |
9490 | { | |
9491 | tree new_clauses = NULL, nc, oc; | |
9492 | ||
9493 | for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc)) | |
9494 | { | |
9495 | nc = copy_node (oc); | |
9496 | OMP_CLAUSE_CHAIN (nc) = new_clauses; | |
9497 | new_clauses = nc; | |
9498 | ||
9499 | switch (OMP_CLAUSE_CODE (nc)) | |
9500 | { | |
9501 | case OMP_CLAUSE_PRIVATE: | |
9502 | case OMP_CLAUSE_SHARED: | |
9503 | case OMP_CLAUSE_FIRSTPRIVATE: | |
9504 | case OMP_CLAUSE_LASTPRIVATE: | |
9505 | case OMP_CLAUSE_REDUCTION: | |
9506 | case OMP_CLAUSE_COPYIN: | |
9507 | case OMP_CLAUSE_COPYPRIVATE: | |
9508 | case OMP_CLAUSE_IF: | |
9509 | case OMP_CLAUSE_NUM_THREADS: | |
9510 | case OMP_CLAUSE_SCHEDULE: | |
9511 | OMP_CLAUSE_OPERAND (nc, 0) | |
015c2c66 MM |
9512 | = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain, |
9513 | in_decl, /*integral_constant_expression_p=*/false); | |
1799e5d5 RH |
9514 | break; |
9515 | case OMP_CLAUSE_NOWAIT: | |
9516 | case OMP_CLAUSE_ORDERED: | |
9517 | case OMP_CLAUSE_DEFAULT: | |
9518 | break; | |
9519 | default: | |
9520 | gcc_unreachable (); | |
9521 | } | |
9522 | } | |
9523 | ||
9524 | return finish_omp_clauses (nreverse (new_clauses)); | |
9525 | } | |
9526 | ||
f74dcfb7 JJ |
9527 | /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */ |
9528 | ||
9529 | static tree | |
9530 | tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain, | |
9531 | tree in_decl) | |
9532 | { | |
9533 | #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl) | |
9534 | ||
9535 | tree purpose, value, chain; | |
9536 | ||
9537 | if (t == NULL) | |
9538 | return t; | |
9539 | ||
9540 | if (TREE_CODE (t) != TREE_LIST) | |
9541 | return tsubst_copy_and_build (t, args, complain, in_decl, | |
015c2c66 MM |
9542 | /*function_p=*/false, |
9543 | /*integral_constant_expression_p=*/false); | |
f74dcfb7 JJ |
9544 | |
9545 | if (t == void_list_node) | |
9546 | return t; | |
9547 | ||
9548 | purpose = TREE_PURPOSE (t); | |
9549 | if (purpose) | |
9550 | purpose = RECUR (purpose); | |
9551 | value = TREE_VALUE (t); | |
9552 | if (value) | |
9553 | value = RECUR (value); | |
9554 | chain = TREE_CHAIN (t); | |
9555 | if (chain && chain != void_type_node) | |
9556 | chain = RECUR (chain); | |
9557 | return tree_cons (purpose, value, chain); | |
9558 | #undef RECUR | |
9559 | } | |
9560 | ||
cc23546e JO |
9561 | /* Like tsubst_copy for expressions, etc. but also does semantic |
9562 | processing. */ | |
00d3396f | 9563 | |
14d22dd6 | 9564 | static tree |
015c2c66 MM |
9565 | tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl, |
9566 | bool integral_constant_expression_p) | |
5566b478 | 9567 | { |
015c2c66 MM |
9568 | #define RECUR(NODE) \ |
9569 | tsubst_expr ((NODE), args, complain, in_decl, \ | |
9570 | integral_constant_expression_p) | |
9571 | ||
fd10dd09 | 9572 | tree stmt, tmp; |
558475f0 | 9573 | |
5566b478 MS |
9574 | if (t == NULL_TREE || t == error_mark_node) |
9575 | return t; | |
9576 | ||
93409b8c PB |
9577 | if (EXPR_HAS_LOCATION (t)) |
9578 | input_location = EXPR_LOCATION (t); | |
7c34ced1 RH |
9579 | if (STATEMENT_CODE_P (TREE_CODE (t))) |
9580 | current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t); | |
9581 | ||
5566b478 | 9582 | switch (TREE_CODE (t)) |
8d08fdba | 9583 | { |
325c3691 RH |
9584 | case STATEMENT_LIST: |
9585 | { | |
9586 | tree_stmt_iterator i; | |
9587 | for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i)) | |
015c2c66 | 9588 | RECUR (tsi_stmt (i)); |
325c3691 RH |
9589 | break; |
9590 | } | |
9591 | ||
558475f0 | 9592 | case CTOR_INITIALIZER: |
c8094d83 | 9593 | finish_mem_initializers (tsubst_initializer_list |
2282d28d MM |
9594 | (TREE_OPERAND (t, 0), args)); |
9595 | break; | |
558475f0 | 9596 | |
5088b058 | 9597 | case RETURN_EXPR: |
015c2c66 | 9598 | finish_return_stmt (RECUR (TREE_OPERAND (t, 0))); |
5566b478 MS |
9599 | break; |
9600 | ||
9601 | case EXPR_STMT: | |
015c2c66 | 9602 | tmp = RECUR (EXPR_STMT_EXPR (t)); |
7c34ced1 RH |
9603 | if (EXPR_STMT_STMT_EXPR_RESULT (t)) |
9604 | finish_stmt_expr_expr (tmp, cur_stmt_expr); | |
9605 | else | |
9606 | finish_expr_stmt (tmp); | |
9607 | break; | |
5566b478 | 9608 | |
9da99f7d | 9609 | case USING_STMT: |
015c2c66 | 9610 | do_using_directive (RECUR (USING_STMT_NAMESPACE (t))); |
9da99f7d | 9611 | break; |
c8094d83 | 9612 | |
350fae66 | 9613 | case DECL_EXPR: |
5566b478 | 9614 | { |
e0942dcd MM |
9615 | tree decl; |
9616 | tree init; | |
5566b478 | 9617 | |
350fae66 | 9618 | decl = DECL_EXPR_DECL (t); |
acef433b MM |
9619 | if (TREE_CODE (decl) == LABEL_DECL) |
9620 | finish_label_decl (DECL_NAME (decl)); | |
fbfe8c9e NS |
9621 | else if (TREE_CODE (decl) == USING_DECL) |
9622 | { | |
98ed9dae | 9623 | tree scope = USING_DECL_SCOPE (decl); |
fbfe8c9e | 9624 | tree name = DECL_NAME (decl); |
22038b2c | 9625 | tree decl; |
c8094d83 | 9626 | |
015c2c66 | 9627 | scope = RECUR (scope); |
22038b2c | 9628 | decl = lookup_qualified_name (scope, name, |
12483c9f NS |
9629 | /*is_type_p=*/false, |
9630 | /*complain=*/false); | |
8f78f01f MM |
9631 | if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST) |
9632 | qualified_name_lookup_error (scope, name, decl); | |
22038b2c | 9633 | else |
ed5f054f | 9634 | do_local_using_decl (decl, scope, name); |
fbfe8c9e | 9635 | } |
acef433b MM |
9636 | else |
9637 | { | |
9638 | init = DECL_INITIAL (decl); | |
9639 | decl = tsubst (decl, args, complain, in_decl); | |
ce2e5191 NS |
9640 | if (decl != error_mark_node) |
9641 | { | |
0cbd7506 MS |
9642 | /* By marking the declaration as instantiated, we avoid |
9643 | trying to instantiate it. Since instantiate_decl can't | |
9644 | handle local variables, and since we've already done | |
9645 | all that needs to be done, that's the right thing to | |
9646 | do. */ | |
9647 | if (TREE_CODE (decl) == VAR_DECL) | |
9648 | DECL_TEMPLATE_INSTANTIATED (decl) = 1; | |
edebf865 MM |
9649 | if (TREE_CODE (decl) == VAR_DECL |
9650 | && ANON_AGGR_TYPE_P (TREE_TYPE (decl))) | |
9651 | /* Anonymous aggregates are a special case. */ | |
9652 | finish_anon_union (decl); | |
c8094d83 | 9653 | else |
ed2fa432 | 9654 | { |
edebf865 | 9655 | maybe_push_decl (decl); |
39703eb9 MM |
9656 | if (TREE_CODE (decl) == VAR_DECL |
9657 | && DECL_PRETTY_FUNCTION_P (decl)) | |
edebf865 MM |
9658 | { |
9659 | /* For __PRETTY_FUNCTION__ we have to adjust the | |
9660 | initializer. */ | |
9661 | const char *const name | |
9662 | = cxx_printable_name (current_function_decl, 2); | |
bb885938 | 9663 | init = cp_fname_init (name, &TREE_TYPE (decl)); |
edebf865 MM |
9664 | } |
9665 | else | |
015c2c66 | 9666 | init = RECUR (init); |
d174af6c | 9667 | finish_decl (decl, init, NULL_TREE); |
ed2fa432 | 9668 | } |
ce2e5191 | 9669 | } |
acef433b | 9670 | } |
fd10dd09 | 9671 | |
350fae66 | 9672 | /* A DECL_EXPR can also be used as an expression, in the condition |
325c3691 RH |
9673 | clause of an if/for/while construct. */ |
9674 | return decl; | |
5566b478 | 9675 | } |
8d08fdba | 9676 | |
5566b478 | 9677 | case FOR_STMT: |
7c34ced1 | 9678 | stmt = begin_for_stmt (); |
015c2c66 | 9679 | RECUR (FOR_INIT_STMT (t)); |
7c34ced1 | 9680 | finish_for_init_stmt (stmt); |
015c2c66 | 9681 | tmp = RECUR (FOR_COND (t)); |
7c34ced1 | 9682 | finish_for_cond (tmp, stmt); |
015c2c66 | 9683 | tmp = RECUR (FOR_EXPR (t)); |
7c34ced1 | 9684 | finish_for_expr (tmp, stmt); |
015c2c66 | 9685 | RECUR (FOR_BODY (t)); |
7c34ced1 | 9686 | finish_for_stmt (stmt); |
5566b478 | 9687 | break; |
8d08fdba | 9688 | |
5566b478 | 9689 | case WHILE_STMT: |
7c34ced1 | 9690 | stmt = begin_while_stmt (); |
015c2c66 | 9691 | tmp = RECUR (WHILE_COND (t)); |
7c34ced1 | 9692 | finish_while_stmt_cond (tmp, stmt); |
015c2c66 | 9693 | RECUR (WHILE_BODY (t)); |
7c34ced1 | 9694 | finish_while_stmt (stmt); |
5566b478 | 9695 | break; |
8d08fdba | 9696 | |
5566b478 | 9697 | case DO_STMT: |
7c34ced1 | 9698 | stmt = begin_do_stmt (); |
015c2c66 | 9699 | RECUR (DO_BODY (t)); |
7c34ced1 | 9700 | finish_do_body (stmt); |
015c2c66 | 9701 | tmp = RECUR (DO_COND (t)); |
7c34ced1 | 9702 | finish_do_stmt (tmp, stmt); |
5566b478 | 9703 | break; |
a0a33927 | 9704 | |
5566b478 | 9705 | case IF_STMT: |
7c34ced1 | 9706 | stmt = begin_if_stmt (); |
015c2c66 | 9707 | tmp = RECUR (IF_COND (t)); |
7c34ced1 | 9708 | finish_if_stmt_cond (tmp, stmt); |
015c2c66 | 9709 | RECUR (THEN_CLAUSE (t)); |
7c34ced1 | 9710 | finish_then_clause (stmt); |
8d08fdba | 9711 | |
7c34ced1 RH |
9712 | if (ELSE_CLAUSE (t)) |
9713 | { | |
9714 | begin_else_clause (stmt); | |
015c2c66 | 9715 | RECUR (ELSE_CLAUSE (t)); |
7c34ced1 RH |
9716 | finish_else_clause (stmt); |
9717 | } | |
9718 | ||
9719 | finish_if_stmt (stmt); | |
5566b478 | 9720 | break; |
8d08fdba | 9721 | |
5882f0f3 | 9722 | case BIND_EXPR: |
7c34ced1 RH |
9723 | if (BIND_EXPR_BODY_BLOCK (t)) |
9724 | stmt = begin_function_body (); | |
9725 | else | |
9726 | stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t) | |
9727 | ? BCS_TRY_BLOCK : 0); | |
ade3dc07 | 9728 | |
015c2c66 | 9729 | RECUR (BIND_EXPR_BODY (t)); |
ade3dc07 | 9730 | |
7c34ced1 RH |
9731 | if (BIND_EXPR_BODY_BLOCK (t)) |
9732 | finish_function_body (stmt); | |
9733 | else | |
9734 | finish_compound_stmt (stmt); | |
5566b478 | 9735 | break; |
8d08fdba | 9736 | |
5566b478 | 9737 | case BREAK_STMT: |
ad321293 | 9738 | finish_break_stmt (); |
5566b478 | 9739 | break; |
8d08fdba | 9740 | |
6467930b | 9741 | case CONTINUE_STMT: |
ad321293 | 9742 | finish_continue_stmt (); |
6467930b MS |
9743 | break; |
9744 | ||
5566b478 | 9745 | case SWITCH_STMT: |
7c34ced1 | 9746 | stmt = begin_switch_stmt (); |
015c2c66 | 9747 | tmp = RECUR (SWITCH_STMT_COND (t)); |
7c34ced1 | 9748 | finish_switch_cond (tmp, stmt); |
015c2c66 | 9749 | RECUR (SWITCH_STMT_BODY (t)); |
7c34ced1 | 9750 | finish_switch_stmt (stmt); |
5566b478 MS |
9751 | break; |
9752 | ||
8c161995 | 9753 | case CASE_LABEL_EXPR: |
015c2c66 MM |
9754 | finish_case_label (RECUR (CASE_LOW (t)), |
9755 | RECUR (CASE_HIGH (t))); | |
5566b478 MS |
9756 | break; |
9757 | ||
9e14e18f | 9758 | case LABEL_EXPR: |
9e14e18f | 9759 | finish_label_stmt (DECL_NAME (LABEL_EXPR_LABEL (t))); |
5566b478 MS |
9760 | break; |
9761 | ||
9e14e18f | 9762 | case GOTO_EXPR: |
fd10dd09 JM |
9763 | tmp = GOTO_DESTINATION (t); |
9764 | if (TREE_CODE (tmp) != LABEL_DECL) | |
aa09da44 MM |
9765 | /* Computed goto's must be tsubst'd into. On the other hand, |
9766 | non-computed gotos must not be; the identifier in question | |
9767 | will have no binding. */ | |
015c2c66 | 9768 | tmp = RECUR (tmp); |
3fa56191 | 9769 | else |
fd10dd09 JM |
9770 | tmp = DECL_NAME (tmp); |
9771 | finish_goto_stmt (tmp); | |
ad321293 MM |
9772 | break; |
9773 | ||
e130a54b | 9774 | case ASM_EXPR: |
c87978aa | 9775 | tmp = finish_asm_stmt |
6de9cd9a | 9776 | (ASM_VOLATILE_P (t), |
015c2c66 | 9777 | RECUR (ASM_STRING (t)), |
f74dcfb7 JJ |
9778 | tsubst_copy_asm_operands (ASM_OUTPUTS (t), args, complain, in_decl), |
9779 | tsubst_copy_asm_operands (ASM_INPUTS (t), args, complain, in_decl), | |
9780 | tsubst_copy_asm_operands (ASM_CLOBBERS (t), args, complain, in_decl)); | |
131263fa AP |
9781 | { |
9782 | tree asm_expr = tmp; | |
9783 | if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR) | |
9784 | asm_expr = TREE_OPERAND (asm_expr, 0); | |
9785 | ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t); | |
9786 | } | |
5566b478 | 9787 | break; |
faf5394a MS |
9788 | |
9789 | case TRY_BLOCK: | |
f1dedc31 | 9790 | if (CLEANUP_P (t)) |
62409b39 | 9791 | { |
57b52417 | 9792 | stmt = begin_try_block (); |
015c2c66 | 9793 | RECUR (TRY_STMTS (t)); |
62409b39 | 9794 | finish_cleanup_try_block (stmt); |
015c2c66 | 9795 | finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt); |
62409b39 | 9796 | } |
f1dedc31 MM |
9797 | else |
9798 | { | |
eaf6fb90 MM |
9799 | tree compound_stmt = NULL_TREE; |
9800 | ||
62409b39 | 9801 | if (FN_TRY_BLOCK_P (t)) |
eaf6fb90 | 9802 | stmt = begin_function_try_block (&compound_stmt); |
62409b39 MM |
9803 | else |
9804 | stmt = begin_try_block (); | |
9805 | ||
015c2c66 | 9806 | RECUR (TRY_STMTS (t)); |
62409b39 MM |
9807 | |
9808 | if (FN_TRY_BLOCK_P (t)) | |
9809 | finish_function_try_block (stmt); | |
9810 | else | |
9811 | finish_try_block (stmt); | |
9812 | ||
015c2c66 | 9813 | RECUR (TRY_HANDLERS (t)); |
84df082b | 9814 | if (FN_TRY_BLOCK_P (t)) |
eaf6fb90 | 9815 | finish_function_handler_sequence (stmt, compound_stmt); |
84df082b MM |
9816 | else |
9817 | finish_handler_sequence (stmt); | |
f1dedc31 | 9818 | } |
faf5394a | 9819 | break; |
c8094d83 | 9820 | |
faf5394a | 9821 | case HANDLER: |
b35d4555 | 9822 | { |
3a2419a7 | 9823 | tree decl = HANDLER_PARMS (t); |
b35d4555 | 9824 | |
3a2419a7 | 9825 | if (decl) |
b35d4555 | 9826 | { |
b35d4555 | 9827 | decl = tsubst (decl, args, complain, in_decl); |
f8191e64 MM |
9828 | /* Prevent instantiate_decl from trying to instantiate |
9829 | this variable. We've already done all that needs to be | |
9830 | done. */ | |
3a2419a7 VR |
9831 | if (decl != error_mark_node) |
9832 | DECL_TEMPLATE_INSTANTIATED (decl) = 1; | |
b35d4555 | 9833 | } |
3a2419a7 | 9834 | stmt = begin_handler (); |
1a6025b4 | 9835 | finish_handler_parms (decl, stmt); |
015c2c66 | 9836 | RECUR (HANDLER_BODY (t)); |
1a6025b4 | 9837 | finish_handler (stmt); |
b35d4555 | 9838 | } |
faf5394a MS |
9839 | break; |
9840 | ||
b87692e5 | 9841 | case TAG_DEFN: |
fd10dd09 | 9842 | tsubst (TREE_TYPE (t), args, complain, NULL_TREE); |
b87692e5 | 9843 | break; |
55a3debe DG |
9844 | |
9845 | case STATIC_ASSERT: | |
9846 | { | |
9847 | tree condition = | |
9848 | tsubst_expr (STATIC_ASSERT_CONDITION (t), | |
9849 | args, | |
9850 | complain, in_decl, | |
9851 | /*integral_constant_expression_p=*/true); | |
9852 | finish_static_assert (condition, | |
9853 | STATIC_ASSERT_MESSAGE (t), | |
9854 | STATIC_ASSERT_SOURCE_LOCATION (t), | |
9855 | /*member_p=*/false); | |
9856 | } | |
9857 | break; | |
b87692e5 | 9858 | |
1799e5d5 RH |
9859 | case OMP_PARALLEL: |
9860 | tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), | |
9861 | args, complain, in_decl); | |
9862 | stmt = begin_omp_parallel (); | |
015c2c66 | 9863 | RECUR (OMP_PARALLEL_BODY (t)); |
761041be JJ |
9864 | OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt)) |
9865 | = OMP_PARALLEL_COMBINED (t); | |
1799e5d5 RH |
9866 | break; |
9867 | ||
9868 | case OMP_FOR: | |
9869 | { | |
9870 | tree clauses, decl, init, cond, incr, body, pre_body; | |
9871 | ||
9872 | clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), | |
9873 | args, complain, in_decl); | |
9874 | init = OMP_FOR_INIT (t); | |
9875 | gcc_assert (TREE_CODE (init) == MODIFY_EXPR); | |
015c2c66 MM |
9876 | decl = RECUR (TREE_OPERAND (init, 0)); |
9877 | init = RECUR (TREE_OPERAND (init, 1)); | |
9878 | cond = RECUR (OMP_FOR_COND (t)); | |
9879 | incr = RECUR (OMP_FOR_INCR (t)); | |
1799e5d5 RH |
9880 | |
9881 | stmt = begin_omp_structured_block (); | |
9882 | ||
9883 | pre_body = push_stmt_list (); | |
015c2c66 | 9884 | RECUR (OMP_FOR_PRE_BODY (t)); |
1799e5d5 RH |
9885 | pre_body = pop_stmt_list (pre_body); |
9886 | ||
9887 | body = push_stmt_list (); | |
015c2c66 | 9888 | RECUR (OMP_FOR_BODY (t)); |
1799e5d5 RH |
9889 | body = pop_stmt_list (body); |
9890 | ||
9891 | t = finish_omp_for (EXPR_LOCATION (t), decl, init, cond, incr, body, | |
9892 | pre_body); | |
9893 | if (t) | |
9894 | OMP_FOR_CLAUSES (t) = clauses; | |
9895 | ||
9896 | add_stmt (finish_omp_structured_block (stmt)); | |
9897 | } | |
9898 | break; | |
9899 | ||
9900 | case OMP_SECTIONS: | |
9901 | case OMP_SINGLE: | |
9902 | tmp = tsubst_omp_clauses (OMP_CLAUSES (t), args, complain, in_decl); | |
9903 | stmt = push_stmt_list (); | |
015c2c66 | 9904 | RECUR (OMP_BODY (t)); |
1799e5d5 RH |
9905 | stmt = pop_stmt_list (stmt); |
9906 | ||
9907 | t = copy_node (t); | |
9908 | OMP_BODY (t) = stmt; | |
9909 | OMP_CLAUSES (t) = tmp; | |
9910 | add_stmt (t); | |
9911 | break; | |
9912 | ||
9913 | case OMP_SECTION: | |
9914 | case OMP_CRITICAL: | |
9915 | case OMP_MASTER: | |
9916 | case OMP_ORDERED: | |
9917 | stmt = push_stmt_list (); | |
015c2c66 | 9918 | RECUR (OMP_BODY (t)); |
1799e5d5 RH |
9919 | stmt = pop_stmt_list (stmt); |
9920 | ||
9921 | t = copy_node (t); | |
9922 | OMP_BODY (t) = stmt; | |
9923 | add_stmt (t); | |
9924 | break; | |
9925 | ||
9926 | case OMP_ATOMIC: | |
4fe70b31 PB |
9927 | if (OMP_ATOMIC_DEPENDENT_P (t)) |
9928 | { | |
9929 | tree op1 = TREE_OPERAND (t, 1); | |
9930 | tree lhs = RECUR (TREE_OPERAND (op1, 0)); | |
9931 | tree rhs = RECUR (TREE_OPERAND (op1, 1)); | |
9932 | finish_omp_atomic (TREE_CODE (op1), lhs, rhs); | |
9933 | } | |
1799e5d5 RH |
9934 | break; |
9935 | ||
5d80a306 DG |
9936 | case EXPR_PACK_EXPANSION: |
9937 | error ("invalid use of pack expansion expression"); | |
9938 | return error_mark_node; | |
9939 | ||
9940 | case NONTYPE_ARGUMENT_PACK: | |
9941 | error ("use %<...%> to expand argument pack"); | |
9942 | return error_mark_node; | |
9943 | ||
5566b478 | 9944 | default: |
315fb5db | 9945 | gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t))); |
c8094d83 | 9946 | |
315fb5db | 9947 | return tsubst_copy_and_build (t, args, complain, in_decl, |
015c2c66 MM |
9948 | /*function_p=*/false, |
9949 | integral_constant_expression_p); | |
5566b478 | 9950 | } |
fd10dd09 | 9951 | |
325c3691 | 9952 | return NULL_TREE; |
015c2c66 | 9953 | #undef RECUR |
8d08fdba MS |
9954 | } |
9955 | ||
b3445994 MM |
9956 | /* T is a postfix-expression that is not being used in a function |
9957 | call. Return the substituted version of T. */ | |
9958 | ||
9959 | static tree | |
c8094d83 | 9960 | tsubst_non_call_postfix_expression (tree t, tree args, |
b3445994 MM |
9961 | tsubst_flags_t complain, |
9962 | tree in_decl) | |
9963 | { | |
9964 | if (TREE_CODE (t) == SCOPE_REF) | |
9965 | t = tsubst_qualified_id (t, args, complain, in_decl, | |
9966 | /*done=*/false, /*address_p=*/false); | |
9967 | else | |
9968 | t = tsubst_copy_and_build (t, args, complain, in_decl, | |
015c2c66 MM |
9969 | /*function_p=*/false, |
9970 | /*integral_constant_expression_p=*/false); | |
b3445994 MM |
9971 | |
9972 | return t; | |
9973 | } | |
9974 | ||
cc23546e | 9975 | /* Like tsubst but deals with expressions and performs semantic |
b3445994 | 9976 | analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */ |
cc23546e | 9977 | |
ee76b931 | 9978 | tree |
c8094d83 | 9979 | tsubst_copy_and_build (tree t, |
0cbd7506 MS |
9980 | tree args, |
9981 | tsubst_flags_t complain, | |
9982 | tree in_decl, | |
015c2c66 MM |
9983 | bool function_p, |
9984 | bool integral_constant_expression_p) | |
cc23546e | 9985 | { |
015c2c66 MM |
9986 | #define RECUR(NODE) \ |
9987 | tsubst_copy_and_build (NODE, args, complain, in_decl, \ | |
9988 | /*function_p=*/false, \ | |
9989 | integral_constant_expression_p) | |
b3445994 | 9990 | |
ee76b931 MM |
9991 | tree op1; |
9992 | ||
cc23546e JO |
9993 | if (t == NULL_TREE || t == error_mark_node) |
9994 | return t; | |
9995 | ||
9996 | switch (TREE_CODE (t)) | |
9997 | { | |
399dedb9 NS |
9998 | case USING_DECL: |
9999 | t = DECL_NAME (t); | |
852dcbdd | 10000 | /* Fall through. */ |
b3445994 | 10001 | case IDENTIFIER_NODE: |
cc23546e | 10002 | { |
b3445994 | 10003 | tree decl; |
b3445994 | 10004 | cp_id_kind idk; |
67c03833 | 10005 | bool non_integral_constant_expression_p; |
b3445994 MM |
10006 | const char *error_msg; |
10007 | ||
b3445994 | 10008 | if (IDENTIFIER_TYPENAME_P (t)) |
cc23546e | 10009 | { |
b3445994 MM |
10010 | tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl); |
10011 | t = mangle_conv_op_name_for_type (new_type); | |
cc23546e | 10012 | } |
b3445994 MM |
10013 | |
10014 | /* Look up the name. */ | |
10e6657a | 10015 | decl = lookup_name (t); |
b3445994 MM |
10016 | |
10017 | /* By convention, expressions use ERROR_MARK_NODE to indicate | |
10018 | failure, not NULL_TREE. */ | |
10019 | if (decl == NULL_TREE) | |
10020 | decl = error_mark_node; | |
10021 | ||
10b1d5e7 | 10022 | decl = finish_id_expression (t, decl, NULL_TREE, |
b3445994 | 10023 | &idk, |
015c2c66 | 10024 | integral_constant_expression_p, |
67c03833 JM |
10025 | /*allow_non_integral_constant_expression_p=*/false, |
10026 | &non_integral_constant_expression_p, | |
02ed62dd MM |
10027 | /*template_p=*/false, |
10028 | /*done=*/true, | |
10029 | /*address_p=*/false, | |
10030 | /*template_arg_p=*/false, | |
b3445994 MM |
10031 | &error_msg); |
10032 | if (error_msg) | |
10033 | error (error_msg); | |
10034 | if (!function_p && TREE_CODE (decl) == IDENTIFIER_NODE) | |
10035 | decl = unqualified_name_lookup_error (decl); | |
10036 | return decl; | |
cc23546e JO |
10037 | } |
10038 | ||
10039 | case TEMPLATE_ID_EXPR: | |
10040 | { | |
10041 | tree object; | |
b3445994 | 10042 | tree template = RECUR (TREE_OPERAND (t, 0)); |
bf12d54d NS |
10043 | tree targs = TREE_OPERAND (t, 1); |
10044 | ||
10045 | if (targs) | |
10046 | targs = tsubst_template_args (targs, args, complain, in_decl); | |
c8094d83 | 10047 | |
cc23546e JO |
10048 | if (TREE_CODE (template) == COMPONENT_REF) |
10049 | { | |
10050 | object = TREE_OPERAND (template, 0); | |
10051 | template = TREE_OPERAND (template, 1); | |
10052 | } | |
10053 | else | |
10054 | object = NULL_TREE; | |
bd83b409 | 10055 | template = lookup_template_function (template, targs); |
c8094d83 | 10056 | |
cc23546e | 10057 | if (object) |
c8094d83 | 10058 | return build3 (COMPONENT_REF, TREE_TYPE (template), |
f293ce4b | 10059 | object, template, NULL_TREE); |
cc23546e | 10060 | else |
eff3a276 | 10061 | return baselink_for_fns (template); |
cc23546e JO |
10062 | } |
10063 | ||
10064 | case INDIRECT_REF: | |
db24eb1f NS |
10065 | { |
10066 | tree r = RECUR (TREE_OPERAND (t, 0)); | |
10067 | ||
10068 | if (REFERENCE_REF_P (t)) | |
10069 | { | |
e8c66fe0 NS |
10070 | /* A type conversion to reference type will be enclosed in |
10071 | such an indirect ref, but the substitution of the cast | |
10072 | will have also added such an indirect ref. */ | |
10073 | if (TREE_CODE (TREE_TYPE (r)) == REFERENCE_TYPE) | |
10074 | r = convert_from_reference (r); | |
db24eb1f NS |
10075 | } |
10076 | else | |
10077 | r = build_x_indirect_ref (r, "unary *"); | |
10078 | return r; | |
10079 | } | |
cc23546e | 10080 | |
0da99d4e GB |
10081 | case NOP_EXPR: |
10082 | return build_nop | |
10083 | (tsubst (TREE_TYPE (t), args, complain, in_decl), | |
10084 | RECUR (TREE_OPERAND (t, 0))); | |
10085 | ||
cc23546e | 10086 | case CAST_EXPR: |
cc23546e | 10087 | case REINTERPRET_CAST_EXPR: |
cc23546e | 10088 | case CONST_CAST_EXPR: |
cc23546e | 10089 | case DYNAMIC_CAST_EXPR: |
cc23546e | 10090 | case STATIC_CAST_EXPR: |
015c2c66 MM |
10091 | { |
10092 | tree type; | |
10093 | tree op; | |
10094 | ||
10095 | type = tsubst (TREE_TYPE (t), args, complain, in_decl); | |
10096 | if (integral_constant_expression_p | |
10097 | && !cast_valid_in_integral_constant_expression_p (type)) | |
10098 | { | |
10099 | error ("a cast to a type other than an integral or " | |
10100 | "enumeration type cannot appear in a constant-expression"); | |
10101 | return error_mark_node; | |
10102 | } | |
10103 | ||
10104 | op = RECUR (TREE_OPERAND (t, 0)); | |
10105 | ||
10106 | switch (TREE_CODE (t)) | |
10107 | { | |
10108 | case CAST_EXPR: | |
10109 | return build_functional_cast (type, op); | |
10110 | case REINTERPRET_CAST_EXPR: | |
10111 | return build_reinterpret_cast (type, op); | |
10112 | case CONST_CAST_EXPR: | |
10113 | return build_const_cast (type, op); | |
10114 | case DYNAMIC_CAST_EXPR: | |
10115 | return build_dynamic_cast (type, op); | |
10116 | case STATIC_CAST_EXPR: | |
10117 | return build_static_cast (type, op); | |
10118 | default: | |
10119 | gcc_unreachable (); | |
10120 | } | |
10121 | } | |
cc23546e | 10122 | |
cc23546e JO |
10123 | case POSTDECREMENT_EXPR: |
10124 | case POSTINCREMENT_EXPR: | |
b3445994 MM |
10125 | op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0), |
10126 | args, complain, in_decl); | |
ee76b931 MM |
10127 | return build_x_unary_op (TREE_CODE (t), op1); |
10128 | ||
10129 | case PREDECREMENT_EXPR: | |
10130 | case PREINCREMENT_EXPR: | |
cc23546e JO |
10131 | case NEGATE_EXPR: |
10132 | case BIT_NOT_EXPR: | |
cc23546e | 10133 | case ABS_EXPR: |
d17811fd | 10134 | case TRUTH_NOT_EXPR: |
392e3d51 | 10135 | case UNARY_PLUS_EXPR: /* Unary + */ |
d17811fd MM |
10136 | case REALPART_EXPR: |
10137 | case IMAGPART_EXPR: | |
b3445994 | 10138 | return build_x_unary_op (TREE_CODE (t), RECUR (TREE_OPERAND (t, 0))); |
cc23546e | 10139 | |
cc23546e | 10140 | case ADDR_EXPR: |
ee76b931 MM |
10141 | op1 = TREE_OPERAND (t, 0); |
10142 | if (TREE_CODE (op1) == SCOPE_REF) | |
c8094d83 | 10143 | op1 = tsubst_qualified_id (op1, args, complain, in_decl, |
ee76b931 MM |
10144 | /*done=*/true, /*address_p=*/true); |
10145 | else | |
c8094d83 | 10146 | op1 = tsubst_non_call_postfix_expression (op1, args, complain, |
b3445994 | 10147 | in_decl); |
fc2b8477 MM |
10148 | if (TREE_CODE (op1) == LABEL_DECL) |
10149 | return finish_label_address_expr (DECL_NAME (op1)); | |
ee76b931 MM |
10150 | return build_x_unary_op (ADDR_EXPR, op1); |
10151 | ||
cc23546e JO |
10152 | case PLUS_EXPR: |
10153 | case MINUS_EXPR: | |
10154 | case MULT_EXPR: | |
10155 | case TRUNC_DIV_EXPR: | |
10156 | case CEIL_DIV_EXPR: | |
10157 | case FLOOR_DIV_EXPR: | |
10158 | case ROUND_DIV_EXPR: | |
10159 | case EXACT_DIV_EXPR: | |
10160 | case BIT_AND_EXPR: | |
cc23546e JO |
10161 | case BIT_IOR_EXPR: |
10162 | case BIT_XOR_EXPR: | |
10163 | case TRUNC_MOD_EXPR: | |
10164 | case FLOOR_MOD_EXPR: | |
10165 | case TRUTH_ANDIF_EXPR: | |
10166 | case TRUTH_ORIF_EXPR: | |
10167 | case TRUTH_AND_EXPR: | |
10168 | case TRUTH_OR_EXPR: | |
10169 | case RSHIFT_EXPR: | |
10170 | case LSHIFT_EXPR: | |
10171 | case RROTATE_EXPR: | |
10172 | case LROTATE_EXPR: | |
10173 | case EQ_EXPR: | |
10174 | case NE_EXPR: | |
10175 | case MAX_EXPR: | |
10176 | case MIN_EXPR: | |
10177 | case LE_EXPR: | |
10178 | case GE_EXPR: | |
10179 | case LT_EXPR: | |
10180 | case GT_EXPR: | |
10181 | case MEMBER_REF: | |
b3445994 | 10182 | case DOTSTAR_EXPR: |
cc23546e | 10183 | return build_x_binary_op |
c8094d83 | 10184 | (TREE_CODE (t), |
b3445994 | 10185 | RECUR (TREE_OPERAND (t, 0)), |
2a67bec2 ILT |
10186 | (TREE_NO_WARNING (TREE_OPERAND (t, 0)) |
10187 | ? ERROR_MARK | |
10188 | : TREE_CODE (TREE_OPERAND (t, 0))), | |
ec835fb2 | 10189 | RECUR (TREE_OPERAND (t, 1)), |
2a67bec2 ILT |
10190 | (TREE_NO_WARNING (TREE_OPERAND (t, 1)) |
10191 | ? ERROR_MARK | |
10192 | : TREE_CODE (TREE_OPERAND (t, 1))), | |
ec835fb2 | 10193 | /*overloaded_p=*/NULL); |
cc23546e JO |
10194 | |
10195 | case SCOPE_REF: | |
ee76b931 MM |
10196 | return tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true, |
10197 | /*address_p=*/false); | |
cc23546e | 10198 | case ARRAY_REF: |
b3445994 MM |
10199 | op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0), |
10200 | args, complain, in_decl); | |
2a67bec2 ILT |
10201 | return build_x_binary_op (ARRAY_REF, op1, |
10202 | (TREE_NO_WARNING (TREE_OPERAND (t, 0)) | |
10203 | ? ERROR_MARK | |
10204 | : TREE_CODE (TREE_OPERAND (t, 0))), | |
10205 | RECUR (TREE_OPERAND (t, 1)), | |
10206 | (TREE_NO_WARNING (TREE_OPERAND (t, 1)) | |
10207 | ? ERROR_MARK | |
10208 | : TREE_CODE (TREE_OPERAND (t, 1))), | |
d8987adb | 10209 | /*overloaded_p=*/NULL); |
c8094d83 | 10210 | |
cc23546e | 10211 | case SIZEOF_EXPR: |
5d80a306 DG |
10212 | if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))) |
10213 | { | |
10214 | /* We only want to compute the number of arguments. */ | |
10215 | tree expanded = tsubst_pack_expansion (TREE_OPERAND (t, 0), args, | |
10216 | complain, in_decl); | |
10217 | return build_int_cst (size_type_node, TREE_VEC_LENGTH (expanded)); | |
10218 | } | |
10219 | /* Fall through */ | |
10220 | ||
cc23546e | 10221 | case ALIGNOF_EXPR: |
d17811fd MM |
10222 | op1 = TREE_OPERAND (t, 0); |
10223 | if (!args) | |
10224 | { | |
10225 | /* When there are no ARGS, we are trying to evaluate a | |
10226 | non-dependent expression from the parser. Trying to do | |
10227 | the substitutions may not work. */ | |
10228 | if (!TYPE_P (op1)) | |
10229 | op1 = TREE_TYPE (op1); | |
10230 | } | |
10231 | else | |
10232 | { | |
10233 | ++skip_evaluation; | |
015c2c66 MM |
10234 | op1 = tsubst_copy_and_build (op1, args, complain, in_decl, |
10235 | /*function_p=*/false, | |
10236 | /*integral_constant_expression_p=*/false); | |
d17811fd MM |
10237 | --skip_evaluation; |
10238 | } | |
7a18b933 NS |
10239 | if (TYPE_P (op1)) |
10240 | return cxx_sizeof_or_alignof_type (op1, TREE_CODE (t), true); | |
d17811fd | 10241 | else |
7a18b933 | 10242 | return cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t)); |
cc23546e JO |
10243 | |
10244 | case MODOP_EXPR: | |
e4c2c34b JM |
10245 | { |
10246 | tree r = build_x_modify_expr | |
10247 | (RECUR (TREE_OPERAND (t, 0)), | |
10248 | TREE_CODE (TREE_OPERAND (t, 1)), | |
10249 | RECUR (TREE_OPERAND (t, 2))); | |
bcf9a914 JM |
10250 | /* TREE_NO_WARNING must be set if either the expression was |
10251 | parenthesized or it uses an operator such as >>= rather | |
10252 | than plain assignment. In the former case, it was already | |
10253 | set and must be copied. In the latter case, | |
10254 | build_x_modify_expr sets it and it must not be reset | |
10255 | here. */ | |
10256 | if (TREE_NO_WARNING (t)) | |
10257 | TREE_NO_WARNING (r) = TREE_NO_WARNING (t); | |
e4c2c34b JM |
10258 | return r; |
10259 | } | |
cc23546e JO |
10260 | |
10261 | case ARROW_EXPR: | |
b3445994 MM |
10262 | op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0), |
10263 | args, complain, in_decl); | |
ee76b931 MM |
10264 | /* Remember that there was a reference to this entity. */ |
10265 | if (DECL_P (op1)) | |
10266 | mark_used (op1); | |
10267 | return build_x_arrow (op1); | |
cc23546e JO |
10268 | |
10269 | case NEW_EXPR: | |
10270 | return build_new | |
b3445994 MM |
10271 | (RECUR (TREE_OPERAND (t, 0)), |
10272 | RECUR (TREE_OPERAND (t, 1)), | |
10273 | RECUR (TREE_OPERAND (t, 2)), | |
058b15c1 | 10274 | RECUR (TREE_OPERAND (t, 3)), |
cc23546e JO |
10275 | NEW_EXPR_USE_GLOBAL (t)); |
10276 | ||
10277 | case DELETE_EXPR: | |
10278 | return delete_sanity | |
b3445994 MM |
10279 | (RECUR (TREE_OPERAND (t, 0)), |
10280 | RECUR (TREE_OPERAND (t, 1)), | |
cc23546e JO |
10281 | DELETE_EXPR_USE_VEC (t), |
10282 | DELETE_EXPR_USE_GLOBAL (t)); | |
10283 | ||
10284 | case COMPOUND_EXPR: | |
b3445994 MM |
10285 | return build_x_compound_expr (RECUR (TREE_OPERAND (t, 0)), |
10286 | RECUR (TREE_OPERAND (t, 1))); | |
cc23546e | 10287 | |
ee76b931 | 10288 | case CALL_EXPR: |
cc23546e | 10289 | { |
ee76b931 MM |
10290 | tree function; |
10291 | tree call_args; | |
ee76b931 | 10292 | bool qualified_p; |
ee935db4 | 10293 | bool koenig_p; |
ee76b931 | 10294 | |
5039610b | 10295 | function = CALL_EXPR_FN (t); |
6d80c4b9 MM |
10296 | /* When we parsed the expression, we determined whether or |
10297 | not Koenig lookup should be performed. */ | |
10298 | koenig_p = KOENIG_LOOKUP_P (t); | |
ee76b931 | 10299 | if (TREE_CODE (function) == SCOPE_REF) |
cc23546e | 10300 | { |
ee76b931 MM |
10301 | qualified_p = true; |
10302 | function = tsubst_qualified_id (function, args, complain, in_decl, | |
c8094d83 | 10303 | /*done=*/false, |
ee76b931 | 10304 | /*address_p=*/false); |
cc23546e | 10305 | } |
ee76b931 | 10306 | else |
cc23546e | 10307 | { |
61e71a9e NS |
10308 | if (TREE_CODE (function) == COMPONENT_REF) |
10309 | { | |
10310 | tree op = TREE_OPERAND (function, 1); | |
10311 | ||
10312 | qualified_p = (TREE_CODE (op) == SCOPE_REF | |
10313 | || (BASELINK_P (op) | |
10314 | && BASELINK_QUALIFIED_P (op))); | |
10315 | } | |
10316 | else | |
10317 | qualified_p = false; | |
3db45ab5 | 10318 | |
c8094d83 | 10319 | function = tsubst_copy_and_build (function, args, complain, |
b3445994 | 10320 | in_decl, |
015c2c66 MM |
10321 | !qualified_p, |
10322 | integral_constant_expression_p); | |
10323 | ||
6d80c4b9 MM |
10324 | if (BASELINK_P (function)) |
10325 | qualified_p = true; | |
cc23546e | 10326 | } |
cc23546e | 10327 | |
5039610b SL |
10328 | /* FIXME: Rewrite this so as not to construct an arglist. */ |
10329 | call_args = RECUR (CALL_EXPR_ARGS (t)); | |
676e33ca MM |
10330 | |
10331 | /* We do not perform argument-dependent lookup if normal | |
10332 | lookup finds a non-function, in accordance with the | |
10333 | expected resolution of DR 218. */ | |
ee935db4 | 10334 | if (koenig_p |
44370687 MM |
10335 | && ((is_overloaded_fn (function) |
10336 | /* If lookup found a member function, the Koenig lookup is | |
10337 | not appropriate, even if an unqualified-name was used | |
10338 | to denote the function. */ | |
10339 | && !DECL_FUNCTION_MEMBER_P (get_first_fn (function))) | |
b3445994 | 10340 | || TREE_CODE (function) == IDENTIFIER_NODE)) |
6d80c4b9 MM |
10341 | function = perform_koenig_lookup (function, call_args); |
10342 | ||
10343 | if (TREE_CODE (function) == IDENTIFIER_NODE) | |
b3445994 | 10344 | { |
6d80c4b9 MM |
10345 | unqualified_name_lookup_error (function); |
10346 | return error_mark_node; | |
b3445994 MM |
10347 | } |
10348 | ||
10349 | /* Remember that there was a reference to this entity. */ | |
10350 | if (DECL_P (function)) | |
10351 | mark_used (function); | |
10352 | ||
d17811fd MM |
10353 | if (TREE_CODE (function) == OFFSET_REF) |
10354 | return build_offset_ref_call_from_tree (function, call_args); | |
10355 | if (TREE_CODE (function) == COMPONENT_REF) | |
9f880ef9 MM |
10356 | { |
10357 | if (!BASELINK_P (TREE_OPERAND (function, 1))) | |
10358 | return finish_call_expr (function, call_args, | |
10359 | /*disallow_virtual=*/false, | |
10360 | /*koenig_p=*/false); | |
10361 | else | |
c8094d83 | 10362 | return (build_new_method_call |
9f880ef9 MM |
10363 | (TREE_OPERAND (function, 0), |
10364 | TREE_OPERAND (function, 1), | |
c8094d83 | 10365 | call_args, NULL_TREE, |
63c9a190 MM |
10366 | qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL, |
10367 | /*fn_p=*/NULL)); | |
9f880ef9 | 10368 | } |
c8094d83 | 10369 | return finish_call_expr (function, call_args, |
6d80c4b9 MM |
10370 | /*disallow_virtual=*/qualified_p, |
10371 | koenig_p); | |
cc23546e JO |
10372 | } |
10373 | ||
10374 | case COND_EXPR: | |
10375 | return build_x_conditional_expr | |
b3445994 MM |
10376 | (RECUR (TREE_OPERAND (t, 0)), |
10377 | RECUR (TREE_OPERAND (t, 1)), | |
10378 | RECUR (TREE_OPERAND (t, 2))); | |
cc23546e JO |
10379 | |
10380 | case PSEUDO_DTOR_EXPR: | |
c8094d83 | 10381 | return finish_pseudo_destructor_expr |
b3445994 MM |
10382 | (RECUR (TREE_OPERAND (t, 0)), |
10383 | RECUR (TREE_OPERAND (t, 1)), | |
10384 | RECUR (TREE_OPERAND (t, 2))); | |
cc23546e JO |
10385 | |
10386 | case TREE_LIST: | |
10387 | { | |
10388 | tree purpose, value, chain; | |
10389 | ||
10390 | if (t == void_list_node) | |
10391 | return t; | |
10392 | ||
5d80a306 DG |
10393 | if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t))) |
10394 | || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t)))) | |
10395 | { | |
10396 | /* We have pack expansions, so expand those and | |
10397 | create a new list out of it. */ | |
10398 | tree purposevec = NULL_TREE; | |
10399 | tree valuevec = NULL_TREE; | |
10400 | tree chain; | |
10401 | int i, len = -1; | |
10402 | ||
10403 | /* Expand the argument expressions. */ | |
10404 | if (TREE_PURPOSE (t)) | |
10405 | purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args, | |
10406 | complain, in_decl); | |
10407 | if (TREE_VALUE (t)) | |
10408 | valuevec = tsubst_pack_expansion (TREE_VALUE (t), args, | |
10409 | complain, in_decl); | |
10410 | ||
10411 | /* Build the rest of the list. */ | |
10412 | chain = TREE_CHAIN (t); | |
10413 | if (chain && chain != void_type_node) | |
10414 | chain = RECUR (chain); | |
10415 | ||
10416 | /* Determine the number of arguments. */ | |
10417 | if (purposevec && TREE_CODE (purposevec) == TREE_VEC) | |
10418 | { | |
10419 | len = TREE_VEC_LENGTH (purposevec); | |
10420 | gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec)); | |
10421 | } | |
10422 | else if (TREE_CODE (valuevec) == TREE_VEC) | |
10423 | len = TREE_VEC_LENGTH (valuevec); | |
10424 | else | |
10425 | { | |
10426 | /* Since we only performed a partial substitution into | |
10427 | the argument pack, we only return a single list | |
10428 | node. */ | |
10429 | if (purposevec == TREE_PURPOSE (t) | |
10430 | && valuevec == TREE_VALUE (t) | |
10431 | && chain == TREE_CHAIN (t)) | |
10432 | return t; | |
10433 | ||
10434 | return tree_cons (purposevec, valuevec, chain); | |
10435 | } | |
10436 | ||
10437 | /* Convert the argument vectors into a TREE_LIST */ | |
10438 | i = len; | |
10439 | while (i > 0) | |
10440 | { | |
10441 | /* Grab the Ith values. */ | |
10442 | i--; | |
10443 | purpose = purposevec ? TREE_VEC_ELT (purposevec, i) | |
10444 | : NULL_TREE; | |
10445 | value | |
10446 | = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i)) | |
10447 | : NULL_TREE; | |
10448 | ||
10449 | /* Build the list (backwards). */ | |
10450 | chain = tree_cons (purpose, value, chain); | |
10451 | } | |
10452 | ||
10453 | return chain; | |
10454 | } | |
10455 | ||
cc23546e JO |
10456 | purpose = TREE_PURPOSE (t); |
10457 | if (purpose) | |
b3445994 | 10458 | purpose = RECUR (purpose); |
cc23546e JO |
10459 | value = TREE_VALUE (t); |
10460 | if (value) | |
b3445994 | 10461 | value = RECUR (value); |
cc23546e JO |
10462 | chain = TREE_CHAIN (t); |
10463 | if (chain && chain != void_type_node) | |
b3445994 | 10464 | chain = RECUR (chain); |
cc23546e JO |
10465 | if (purpose == TREE_PURPOSE (t) |
10466 | && value == TREE_VALUE (t) | |
10467 | && chain == TREE_CHAIN (t)) | |
10468 | return t; | |
10469 | return tree_cons (purpose, value, chain); | |
10470 | } | |
10471 | ||
10472 | case COMPONENT_REF: | |
10473 | { | |
ee76b931 | 10474 | tree object; |
3897c0aa | 10475 | tree object_type; |
ee76b931 MM |
10476 | tree member; |
10477 | ||
b3445994 MM |
10478 | object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0), |
10479 | args, complain, in_decl); | |
ee76b931 MM |
10480 | /* Remember that there was a reference to this entity. */ |
10481 | if (DECL_P (object)) | |
10482 | mark_used (object); | |
3897c0aa | 10483 | object_type = TREE_TYPE (object); |
ee76b931 MM |
10484 | |
10485 | member = TREE_OPERAND (t, 1); | |
10486 | if (BASELINK_P (member)) | |
c8094d83 | 10487 | member = tsubst_baselink (member, |
ee76b931 MM |
10488 | non_reference (TREE_TYPE (object)), |
10489 | args, complain, in_decl); | |
10490 | else | |
10491 | member = tsubst_copy (member, args, complain, in_decl); | |
bad1f462 KL |
10492 | if (member == error_mark_node) |
10493 | return error_mark_node; | |
3897c0aa MM |
10494 | |
10495 | if (object_type && !CLASS_TYPE_P (object_type)) | |
cc23546e JO |
10496 | { |
10497 | if (TREE_CODE (member) == BIT_NOT_EXPR) | |
c8094d83 | 10498 | return finish_pseudo_destructor_expr (object, |
cc23546e | 10499 | NULL_TREE, |
3897c0aa | 10500 | object_type); |
cc23546e JO |
10501 | else if (TREE_CODE (member) == SCOPE_REF |
10502 | && (TREE_CODE (TREE_OPERAND (member, 1)) == BIT_NOT_EXPR)) | |
c8094d83 | 10503 | return finish_pseudo_destructor_expr (object, |
cc23546e | 10504 | object, |
3897c0aa | 10505 | object_type); |
cc23546e JO |
10506 | } |
10507 | else if (TREE_CODE (member) == SCOPE_REF | |
10508 | && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR) | |
10509 | { | |
10510 | tree tmpl; | |
10511 | tree args; | |
c8094d83 | 10512 | |
cc23546e JO |
10513 | /* Lookup the template functions now that we know what the |
10514 | scope is. */ | |
10515 | tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0); | |
10516 | args = TREE_OPERAND (TREE_OPERAND (member, 1), 1); | |
c8094d83 | 10517 | member = lookup_qualified_name (TREE_OPERAND (member, 0), tmpl, |
12483c9f NS |
10518 | /*is_type_p=*/false, |
10519 | /*complain=*/false); | |
cc23546e | 10520 | if (BASELINK_P (member)) |
44370687 | 10521 | { |
c8094d83 | 10522 | BASELINK_FUNCTIONS (member) |
44370687 MM |
10523 | = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member), |
10524 | args); | |
c8094d83 MS |
10525 | member = (adjust_result_of_qualified_name_lookup |
10526 | (member, BINFO_TYPE (BASELINK_BINFO (member)), | |
3897c0aa | 10527 | object_type)); |
44370687 | 10528 | } |
cc23546e JO |
10529 | else |
10530 | { | |
3897c0aa | 10531 | qualified_name_lookup_error (object_type, tmpl, member); |
cc23546e JO |
10532 | return error_mark_node; |
10533 | } | |
10534 | } | |
2436b51f MM |
10535 | else if (TREE_CODE (member) == SCOPE_REF |
10536 | && !CLASS_TYPE_P (TREE_OPERAND (member, 0)) | |
10537 | && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL) | |
10538 | { | |
10539 | if (complain & tf_error) | |
10540 | { | |
10541 | if (TYPE_P (TREE_OPERAND (member, 0))) | |
c8094d83 | 10542 | error ("%qT is not a class or namespace", |
2436b51f MM |
10543 | TREE_OPERAND (member, 0)); |
10544 | else | |
c8094d83 | 10545 | error ("%qD is not a class or namespace", |
2436b51f MM |
10546 | TREE_OPERAND (member, 0)); |
10547 | } | |
10548 | return error_mark_node; | |
10549 | } | |
a3f10e50 NS |
10550 | else if (TREE_CODE (member) == FIELD_DECL) |
10551 | return finish_non_static_data_member (member, object, NULL_TREE); | |
cc23546e | 10552 | |
02ed62dd MM |
10553 | return finish_class_member_access_expr (object, member, |
10554 | /*template_p=*/false); | |
cc23546e JO |
10555 | } |
10556 | ||
10557 | case THROW_EXPR: | |
10558 | return build_throw | |
b3445994 | 10559 | (RECUR (TREE_OPERAND (t, 0))); |
cc23546e JO |
10560 | |
10561 | case CONSTRUCTOR: | |
10562 | { | |
4038c495 GB |
10563 | VEC(constructor_elt,gc) *n; |
10564 | constructor_elt *ce; | |
10565 | unsigned HOST_WIDE_INT idx; | |
cc23546e | 10566 | tree type = tsubst (TREE_TYPE (t), args, complain, in_decl); |
4038c495 | 10567 | bool process_index_p; |
5d80a306 DG |
10568 | int newlen; |
10569 | bool need_copy_p = false; | |
cc23546e | 10570 | |
a97728cf VR |
10571 | if (type == error_mark_node) |
10572 | return error_mark_node; | |
10573 | ||
cc23546e JO |
10574 | /* digest_init will do the wrong thing if we let it. */ |
10575 | if (type && TYPE_PTRMEMFUNC_P (type)) | |
10576 | return t; | |
10577 | ||
4038c495 | 10578 | /* We do not want to process the index of aggregate |
cc23546e JO |
10579 | initializers as they are identifier nodes which will be |
10580 | looked up by digest_init. */ | |
4038c495 | 10581 | process_index_p = !(type && IS_AGGR_TYPE (type)); |
c8094d83 | 10582 | |
4038c495 | 10583 | n = VEC_copy (constructor_elt, gc, CONSTRUCTOR_ELTS (t)); |
5d80a306 | 10584 | newlen = VEC_length (constructor_elt, n); |
4038c495 GB |
10585 | for (idx = 0; VEC_iterate (constructor_elt, n, idx, ce); idx++) |
10586 | { | |
10587 | if (ce->index && process_index_p) | |
10588 | ce->index = RECUR (ce->index); | |
5d80a306 DG |
10589 | |
10590 | if (PACK_EXPANSION_P (ce->value)) | |
10591 | { | |
10592 | /* Substitute into the pack expansion. */ | |
10593 | ce->value = tsubst_pack_expansion (ce->value, args, complain, | |
10594 | in_decl); | |
10595 | ||
10596 | if (TREE_VEC_LENGTH (ce->value) == 1) | |
10597 | /* Just move the argument into place. */ | |
10598 | ce->value = TREE_VEC_ELT (ce->value, 0); | |
10599 | else | |
10600 | { | |
10601 | /* Update the length of the final CONSTRUCTOR | |
10602 | arguments vector, and note that we will need to | |
10603 | copy.*/ | |
10604 | newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1; | |
10605 | need_copy_p = true; | |
10606 | } | |
10607 | } | |
10608 | else | |
10609 | ce->value = RECUR (ce->value); | |
cc23546e | 10610 | } |
c8094d83 | 10611 | |
5d80a306 DG |
10612 | if (need_copy_p) |
10613 | { | |
10614 | VEC(constructor_elt,gc) *old_n = n; | |
10615 | ||
10616 | n = VEC_alloc (constructor_elt, gc, newlen); | |
10617 | for (idx = 0; VEC_iterate (constructor_elt, old_n, idx, ce); | |
10618 | idx++) | |
10619 | { | |
10620 | if (TREE_CODE (ce->value) == TREE_VEC) | |
10621 | { | |
10622 | int i, len = TREE_VEC_LENGTH (ce->value); | |
10623 | for (i = 0; i < len; ++i) | |
10624 | CONSTRUCTOR_APPEND_ELT (n, 0, | |
10625 | TREE_VEC_ELT (ce->value, i)); | |
10626 | } | |
10627 | else | |
10628 | CONSTRUCTOR_APPEND_ELT (n, 0, ce->value); | |
10629 | } | |
10630 | } | |
10631 | ||
05b22df9 MM |
10632 | if (TREE_HAS_CONSTRUCTOR (t)) |
10633 | return finish_compound_literal (type, n); | |
cc23546e | 10634 | |
05b22df9 | 10635 | return build_constructor (NULL_TREE, n); |
cc23546e JO |
10636 | } |
10637 | ||
10638 | case TYPEID_EXPR: | |
10639 | { | |
b3445994 | 10640 | tree operand_0 = RECUR (TREE_OPERAND (t, 0)); |
cc23546e JO |
10641 | if (TYPE_P (operand_0)) |
10642 | return get_typeid (operand_0); | |
10643 | return build_typeid (operand_0); | |
10644 | } | |
10645 | ||
cc23546e | 10646 | case VAR_DECL: |
db24eb1f NS |
10647 | if (!args) |
10648 | return t; | |
10649 | /* Fall through */ | |
c8094d83 | 10650 | |
db24eb1f NS |
10651 | case PARM_DECL: |
10652 | { | |
10653 | tree r = tsubst_copy (t, args, complain, in_decl); | |
c8094d83 | 10654 | |
db24eb1f NS |
10655 | if (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE) |
10656 | /* If the original type was a reference, we'll be wrapped in | |
10657 | the appropriate INDIRECT_REF. */ | |
10658 | r = convert_from_reference (r); | |
10659 | return r; | |
10660 | } | |
cc23546e JO |
10661 | |
10662 | case VA_ARG_EXPR: | |
b3445994 | 10663 | return build_x_va_arg (RECUR (TREE_OPERAND (t, 0)), |
c8094d83 | 10664 | tsubst_copy (TREE_TYPE (t), args, complain, |
b3445994 | 10665 | in_decl)); |
cc23546e | 10666 | |
4bceb077 | 10667 | case OFFSETOF_EXPR: |
c291f8b1 | 10668 | return finish_offsetof (RECUR (TREE_OPERAND (t, 0))); |
4bceb077 | 10669 | |
0fe0caa6 RH |
10670 | case STMT_EXPR: |
10671 | { | |
10672 | tree old_stmt_expr = cur_stmt_expr; | |
10673 | tree stmt_expr = begin_stmt_expr (); | |
10674 | ||
10675 | cur_stmt_expr = stmt_expr; | |
015c2c66 MM |
10676 | tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl, |
10677 | integral_constant_expression_p); | |
0fe0caa6 RH |
10678 | stmt_expr = finish_stmt_expr (stmt_expr, false); |
10679 | cur_stmt_expr = old_stmt_expr; | |
10680 | ||
10681 | return stmt_expr; | |
10682 | } | |
10683 | ||
a5201a91 MM |
10684 | case CONST_DECL: |
10685 | t = tsubst_copy (t, args, complain, in_decl); | |
10686 | /* As in finish_id_expression, we resolve enumeration constants | |
10687 | to their underlying values. */ | |
10688 | if (TREE_CODE (t) == CONST_DECL) | |
6193b8b7 DJ |
10689 | { |
10690 | used_types_insert (TREE_TYPE (t)); | |
10691 | return DECL_INITIAL (t); | |
10692 | } | |
a5201a91 MM |
10693 | return t; |
10694 | ||
cc23546e | 10695 | default: |
e58a9aa1 ZL |
10696 | /* Handle Objective-C++ constructs, if appropriate. */ |
10697 | { | |
10698 | tree subst | |
10699 | = objcp_tsubst_copy_and_build (t, args, complain, | |
10700 | in_decl, /*function_p=*/false); | |
10701 | if (subst) | |
10702 | return subst; | |
10703 | } | |
cc23546e JO |
10704 | return tsubst_copy (t, args, complain, in_decl); |
10705 | } | |
b3445994 MM |
10706 | |
10707 | #undef RECUR | |
cc23546e JO |
10708 | } |
10709 | ||
3e4a3562 NS |
10710 | /* Verify that the instantiated ARGS are valid. For type arguments, |
10711 | make sure that the type's linkage is ok. For non-type arguments, | |
34cd5ae7 | 10712 | make sure they are constants if they are integral or enumerations. |
9bcb9aae | 10713 | Emit an error under control of COMPLAIN, and return TRUE on error. */ |
3e4a3562 NS |
10714 | |
10715 | static bool | |
3a978d72 | 10716 | check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain) |
3e4a3562 NS |
10717 | { |
10718 | int ix, len = DECL_NTPARMS (tmpl); | |
10719 | bool result = false; | |
10720 | ||
10721 | for (ix = 0; ix != len; ix++) | |
10722 | { | |
10723 | tree t = TREE_VEC_ELT (args, ix); | |
c8094d83 | 10724 | |
3e4a3562 NS |
10725 | if (TYPE_P (t)) |
10726 | { | |
10727 | /* [basic.link]: A name with no linkage (notably, the name | |
10728 | of a class or enumeration declared in a local scope) | |
10729 | shall not be used to declare an entity with linkage. | |
10730 | This implies that names with no linkage cannot be used as | |
10731 | template arguments. */ | |
4684cd27 | 10732 | tree nt = no_linkage_check (t, /*relaxed_p=*/false); |
3e4a3562 NS |
10733 | |
10734 | if (nt) | |
10735 | { | |
cb807ba3 | 10736 | /* DR 488 makes use of a type with no linkage cause |
3db45ab5 | 10737 | type deduction to fail. */ |
2010cdcd MM |
10738 | if (complain & tf_error) |
10739 | { | |
10740 | if (TYPE_ANONYMOUS_P (nt)) | |
10741 | error ("%qT is/uses anonymous type", t); | |
10742 | else | |
9df540a8 | 10743 | error ("template argument for %qD uses local type %qT", |
3db45ab5 | 10744 | tmpl, t); |
2010cdcd | 10745 | } |
3e4a3562 NS |
10746 | result = true; |
10747 | } | |
10748 | /* In order to avoid all sorts of complications, we do not | |
10749 | allow variably-modified types as template arguments. */ | |
5377d5ba | 10750 | else if (variably_modified_type_p (t, NULL_TREE)) |
3e4a3562 NS |
10751 | { |
10752 | if (complain & tf_error) | |
0f51ccfc | 10753 | error ("%qT is a variably modified type", t); |
3e4a3562 NS |
10754 | result = true; |
10755 | } | |
10756 | } | |
10757 | /* A non-type argument of integral or enumerated type must be a | |
10758 | constant. */ | |
10759 | else if (TREE_TYPE (t) | |
10760 | && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t)) | |
10761 | && !TREE_CONSTANT (t)) | |
10762 | { | |
10763 | if (complain & tf_error) | |
0f51ccfc | 10764 | error ("integral expression %qE is not constant", t); |
3e4a3562 NS |
10765 | result = true; |
10766 | } | |
10767 | } | |
2010cdcd | 10768 | if (result && (complain & tf_error)) |
0f51ccfc | 10769 | error (" trying to instantiate %qD", tmpl); |
3e4a3562 NS |
10770 | return result; |
10771 | } | |
10772 | ||
6ba89f8e | 10773 | /* Instantiate the indicated variable or function template TMPL with |
36a117a5 MM |
10774 | the template arguments in TARG_PTR. */ |
10775 | ||
5566b478 | 10776 | tree |
3a978d72 | 10777 | instantiate_template (tree tmpl, tree targ_ptr, tsubst_flags_t complain) |
8d08fdba | 10778 | { |
5566b478 | 10779 | tree fndecl; |
36a117a5 MM |
10780 | tree gen_tmpl; |
10781 | tree spec; | |
a95799ec | 10782 | HOST_WIDE_INT saved_processing_template_decl; |
5566b478 | 10783 | |
27fafc8d JM |
10784 | if (tmpl == error_mark_node) |
10785 | return error_mark_node; | |
10786 | ||
50bc768d | 10787 | gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL); |
386b8a85 | 10788 | |
db9b2174 MM |
10789 | /* If this function is a clone, handle it specially. */ |
10790 | if (DECL_CLONED_FUNCTION_P (tmpl)) | |
3ad97789 | 10791 | { |
a30f62e0 | 10792 | tree spec; |
3ad97789 | 10793 | tree clone; |
c8094d83 | 10794 | |
a30f62e0 MM |
10795 | spec = instantiate_template (DECL_CLONED_FUNCTION (tmpl), targ_ptr, |
10796 | complain); | |
10797 | if (spec == error_mark_node) | |
10798 | return error_mark_node; | |
10799 | ||
c6002625 | 10800 | /* Look for the clone. */ |
4684cd27 | 10801 | FOR_EACH_CLONE (clone, spec) |
3ad97789 NS |
10802 | if (DECL_NAME (clone) == DECL_NAME (tmpl)) |
10803 | return clone; | |
10804 | /* We should always have found the clone by now. */ | |
315fb5db | 10805 | gcc_unreachable (); |
3ad97789 NS |
10806 | return NULL_TREE; |
10807 | } | |
c8094d83 | 10808 | |
36a117a5 | 10809 | /* Check to see if we already have this specialization. */ |
c8094d83 | 10810 | spec = retrieve_specialization (tmpl, targ_ptr, |
c7222c02 | 10811 | /*class_specializations_p=*/false); |
36a117a5 MM |
10812 | if (spec != NULL_TREE) |
10813 | return spec; | |
10814 | ||
f9a7ae04 MM |
10815 | gen_tmpl = most_general_template (tmpl); |
10816 | if (tmpl != gen_tmpl) | |
386b8a85 | 10817 | { |
36a117a5 MM |
10818 | /* The TMPL is a partial instantiation. To get a full set of |
10819 | arguments we must add the arguments used to perform the | |
10820 | partial instantiation. */ | |
10821 | targ_ptr = add_outermost_template_args (DECL_TI_ARGS (tmpl), | |
10822 | targ_ptr); | |
36a117a5 MM |
10823 | |
10824 | /* Check to see if we already have this specialization. */ | |
c7222c02 MM |
10825 | spec = retrieve_specialization (gen_tmpl, targ_ptr, |
10826 | /*class_specializations_p=*/false); | |
75650646 MM |
10827 | if (spec != NULL_TREE) |
10828 | return spec; | |
386b8a85 JM |
10829 | } |
10830 | ||
3e4a3562 NS |
10831 | if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr), |
10832 | complain)) | |
10833 | return error_mark_node; | |
c8094d83 | 10834 | |
2b907f5c KL |
10835 | /* We are building a FUNCTION_DECL, during which the access of its |
10836 | parameters and return types have to be checked. However this | |
10837 | FUNCTION_DECL which is the desired context for access checking | |
10838 | is not built yet. We solve this chicken-and-egg problem by | |
10839 | deferring all checks until we have the FUNCTION_DECL. */ | |
10840 | push_deferring_access_checks (dk_deferred); | |
5c74d5b0 | 10841 | |
a95799ec MM |
10842 | /* Although PROCESSING_TEMPLATE_DECL may be true at this point |
10843 | (because, for example, we have encountered a non-dependent | |
a1d08991 MM |
10844 | function call in the body of a template function and must now |
10845 | determine which of several overloaded functions will be called), | |
10846 | within the instantiation itself we are not processing a | |
10847 | template. */ | |
a95799ec MM |
10848 | saved_processing_template_decl = processing_template_decl; |
10849 | processing_template_decl = 0; | |
10850 | /* Substitute template parameters to obtain the specialization. */ | |
17aec3eb | 10851 | fndecl = tsubst (DECL_TEMPLATE_RESULT (gen_tmpl), |
3e4a3562 | 10852 | targ_ptr, complain, gen_tmpl); |
a95799ec | 10853 | processing_template_decl = saved_processing_template_decl; |
6b6b60af MM |
10854 | if (fndecl == error_mark_node) |
10855 | return error_mark_node; | |
5c74d5b0 | 10856 | |
2b907f5c KL |
10857 | /* Now we know the specialization, compute access previously |
10858 | deferred. */ | |
10859 | push_access_scope (fndecl); | |
10860 | perform_deferred_access_checks (); | |
10861 | pop_access_scope (fndecl); | |
10862 | pop_deferring_access_checks (); | |
5c74d5b0 | 10863 | |
36a117a5 MM |
10864 | /* The DECL_TI_TEMPLATE should always be the immediate parent |
10865 | template, not the most general template. */ | |
10866 | DECL_TI_TEMPLATE (fndecl) = tmpl; | |
8d08fdba | 10867 | |
94350948 | 10868 | /* If we've just instantiated the main entry point for a function, |
3ad97789 NS |
10869 | instantiate all the alternate entry points as well. We do this |
10870 | by cloning the instantiation of the main entry point, not by | |
10871 | instantiating the template clones. */ | |
10872 | if (TREE_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (TREE_CHAIN (gen_tmpl))) | |
10873 | clone_function_decl (fndecl, /*update_method_vec_p=*/0); | |
94350948 | 10874 | |
5566b478 | 10875 | return fndecl; |
8d08fdba | 10876 | } |
5566b478 | 10877 | |
4393e105 MM |
10878 | /* The FN is a TEMPLATE_DECL for a function. The ARGS are the |
10879 | arguments that are being used when calling it. TARGS is a vector | |
c8094d83 | 10880 | into which the deduced template arguments are placed. |
8d08fdba MS |
10881 | |
10882 | Return zero for success, 2 for an incomplete match that doesn't resolve | |
10883 | all the types, and 1 for complete failure. An error message will be | |
10884 | printed only for an incomplete match. | |
10885 | ||
e5214479 JM |
10886 | If FN is a conversion operator, or we are trying to produce a specific |
10887 | specialization, RETURN_TYPE is the return type desired. | |
9f54c803 MM |
10888 | |
10889 | The EXPLICIT_TARGS are explicit template arguments provided via a | |
10890 | template-id. | |
6467930b | 10891 | |
830bfa74 MM |
10892 | The parameter STRICT is one of: |
10893 | ||
c8094d83 | 10894 | DEDUCE_CALL: |
830bfa74 MM |
10895 | We are deducing arguments for a function call, as in |
10896 | [temp.deduct.call]. | |
10897 | ||
10898 | DEDUCE_CONV: | |
c8094d83 | 10899 | We are deducing arguments for a conversion function, as in |
830bfa74 MM |
10900 | [temp.deduct.conv]. |
10901 | ||
10902 | DEDUCE_EXACT: | |
62e4a758 NS |
10903 | We are deducing arguments when doing an explicit instantiation |
10904 | as in [temp.explicit], when determining an explicit specialization | |
10905 | as in [temp.expl.spec], or when taking the address of a function | |
a34d3336 | 10906 | template, as in [temp.deduct.funcaddr]. */ |
8d08fdba MS |
10907 | |
10908 | int | |
c8094d83 | 10909 | fn_type_unification (tree fn, |
0cbd7506 MS |
10910 | tree explicit_targs, |
10911 | tree targs, | |
10912 | tree args, | |
10913 | tree return_type, | |
30f86ec3 FJ |
10914 | unification_kind_t strict, |
10915 | int flags) | |
386b8a85 | 10916 | { |
4393e105 MM |
10917 | tree parms; |
10918 | tree fntype; | |
adecb3f4 | 10919 | int result; |
5d80a306 | 10920 | bool incomplete_argument_packs_p = false; |
386b8a85 | 10921 | |
50bc768d | 10922 | gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL); |
2b59fc25 | 10923 | |
4393e105 MM |
10924 | fntype = TREE_TYPE (fn); |
10925 | if (explicit_targs) | |
75650646 | 10926 | { |
4393e105 | 10927 | /* [temp.deduct] |
c8094d83 | 10928 | |
4393e105 MM |
10929 | The specified template arguments must match the template |
10930 | parameters in kind (i.e., type, nontype, template), and there | |
10931 | must not be more arguments than there are parameters; | |
10932 | otherwise type deduction fails. | |
10933 | ||
10934 | Nontype arguments must match the types of the corresponding | |
10935 | nontype template parameters, or must be convertible to the | |
10936 | types of the corresponding nontype parameters as specified in | |
10937 | _temp.arg.nontype_, otherwise type deduction fails. | |
10938 | ||
10939 | All references in the function type of the function template | |
10940 | to the corresponding template parameters are replaced by the | |
10941 | specified template argument values. If a substitution in a | |
10942 | template parameter or in the function type of the function | |
10943 | template results in an invalid type, type deduction fails. */ | |
5d80a306 DG |
10944 | tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn); |
10945 | int i, len = TREE_VEC_LENGTH (tparms); | |
4393e105 | 10946 | tree converted_args; |
5d80a306 | 10947 | bool incomplete = false; |
75650646 | 10948 | |
c8a7ed43 AO |
10949 | if (explicit_targs == error_mark_node) |
10950 | return 1; | |
10951 | ||
4393e105 | 10952 | converted_args |
5d80a306 | 10953 | = (coerce_template_parms (tparms, explicit_targs, NULL_TREE, tf_none, |
e7e93965 MM |
10954 | /*require_all_args=*/false, |
10955 | /*use_default_args=*/false)); | |
4393e105 | 10956 | if (converted_args == error_mark_node) |
75650646 | 10957 | return 1; |
386b8a85 | 10958 | |
ffd49b19 | 10959 | /* Substitute the explicit args into the function type. This is |
0cbd7506 MS |
10960 | necessary so that, for instance, explicitly declared function |
10961 | arguments can match null pointed constants. If we were given | |
10962 | an incomplete set of explicit args, we must not do semantic | |
10963 | processing during substitution as we could create partial | |
10964 | instantiations. */ | |
5d80a306 DG |
10965 | for (i = 0; i < len; i++) |
10966 | { | |
10967 | tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i)); | |
10968 | bool parameter_pack = false; | |
10969 | ||
10970 | /* Dig out the actual parm. */ | |
10971 | if (TREE_CODE (parm) == TYPE_DECL | |
10972 | || TREE_CODE (parm) == TEMPLATE_DECL) | |
10973 | { | |
10974 | parm = TREE_TYPE (parm); | |
10975 | parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm); | |
10976 | } | |
10977 | else if (TREE_CODE (parm) == PARM_DECL) | |
10978 | { | |
10979 | parm = DECL_INITIAL (parm); | |
10980 | parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm); | |
10981 | } | |
10982 | ||
10983 | if (parameter_pack) | |
10984 | { | |
10985 | int level, idx; | |
10986 | tree targ; | |
10987 | template_parm_level_and_index (parm, &level, &idx); | |
10988 | ||
10989 | /* Mark the argument pack as "incomplete". We could | |
10990 | still deduce more arguments during unification. */ | |
10991 | targ = TMPL_ARG (converted_args, level, idx); | |
10992 | ARGUMENT_PACK_INCOMPLETE_P(targ) = 1; | |
10993 | ARGUMENT_PACK_EXPLICIT_ARGS (targ) = ARGUMENT_PACK_ARGS (targ); | |
10994 | ||
10995 | /* We have some incomplete argument packs. */ | |
10996 | incomplete_argument_packs_p = true; | |
10997 | } | |
10998 | } | |
10999 | ||
11000 | if (incomplete_argument_packs_p) | |
11001 | /* Any substitution is guaranteed to be incomplete if there | |
11002 | are incomplete argument packs, because we can still deduce | |
11003 | more arguments. */ | |
11004 | incomplete = 1; | |
11005 | else | |
11006 | incomplete = NUM_TMPL_ARGS (explicit_targs) != NUM_TMPL_ARGS (targs); | |
11007 | ||
ffd49b19 | 11008 | processing_template_decl += incomplete; |
c2ea3a40 | 11009 | fntype = tsubst (fntype, converted_args, tf_none, NULL_TREE); |
ffd49b19 | 11010 | processing_template_decl -= incomplete; |
c8094d83 | 11011 | |
4393e105 MM |
11012 | if (fntype == error_mark_node) |
11013 | return 1; | |
050367a3 | 11014 | |
4393e105 | 11015 | /* Place the explicitly specified arguments in TARGS. */ |
ffd49b19 | 11016 | for (i = NUM_TMPL_ARGS (converted_args); i--;) |
4393e105 | 11017 | TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (converted_args, i); |
75650646 | 11018 | } |
c8094d83 | 11019 | |
e5214479 | 11020 | /* Never do unification on the 'this' parameter. */ |
ba139ba8 | 11021 | parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype)); |
c8094d83 | 11022 | |
8d3631f8 NS |
11023 | if (return_type) |
11024 | { | |
8d3631f8 NS |
11025 | parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms); |
11026 | args = tree_cons (NULL_TREE, return_type, args); | |
4393e105 MM |
11027 | } |
11028 | ||
4393e105 MM |
11029 | /* We allow incomplete unification without an error message here |
11030 | because the standard doesn't seem to explicitly prohibit it. Our | |
11031 | callers must be ready to deal with unification failures in any | |
11032 | event. */ | |
c8094d83 | 11033 | result = type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn), |
adecb3f4 | 11034 | targs, parms, args, /*subr=*/0, |
30f86ec3 | 11035 | strict, flags); |
adecb3f4 | 11036 | |
5d80a306 DG |
11037 | if (result == 0 && incomplete_argument_packs_p) |
11038 | { | |
11039 | int i, len = NUM_TMPL_ARGS (targs); | |
11040 | ||
11041 | /* Clear the "incomplete" flags on all argument packs. */ | |
11042 | for (i = 0; i < len; i++) | |
11043 | { | |
11044 | tree arg = TREE_VEC_ELT (targs, i); | |
11045 | if (ARGUMENT_PACK_P (arg)) | |
11046 | { | |
11047 | ARGUMENT_PACK_INCOMPLETE_P (arg) = 0; | |
11048 | ARGUMENT_PACK_EXPLICIT_ARGS (arg) = NULL_TREE; | |
11049 | } | |
11050 | } | |
11051 | } | |
11052 | ||
c8094d83 | 11053 | if (result == 0) |
adecb3f4 | 11054 | /* All is well so far. Now, check: |
c8094d83 MS |
11055 | |
11056 | [temp.deduct] | |
11057 | ||
adecb3f4 MM |
11058 | When all template arguments have been deduced, all uses of |
11059 | template parameters in nondeduced contexts are replaced with | |
11060 | the corresponding deduced argument values. If the | |
11061 | substitution results in an invalid type, as described above, | |
11062 | type deduction fails. */ | |
c2ea3a40 | 11063 | if (tsubst (TREE_TYPE (fn), targs, tf_none, NULL_TREE) |
adecb3f4 MM |
11064 | == error_mark_node) |
11065 | return 1; | |
11066 | ||
11067 | return result; | |
830bfa74 MM |
11068 | } |
11069 | ||
11070 | /* Adjust types before performing type deduction, as described in | |
11071 | [temp.deduct.call] and [temp.deduct.conv]. The rules in these two | |
4c7d0dff MM |
11072 | sections are symmetric. PARM is the type of a function parameter |
11073 | or the return type of the conversion function. ARG is the type of | |
11074 | the argument passed to the call, or the type of the value | |
dc957d14 | 11075 | initialized with the result of the conversion function. */ |
386b8a85 | 11076 | |
62e4a758 | 11077 | static int |
c8094d83 | 11078 | maybe_adjust_types_for_deduction (unification_kind_t strict, |
0cbd7506 MS |
11079 | tree* parm, |
11080 | tree* arg) | |
830bfa74 | 11081 | { |
62e4a758 | 11082 | int result = 0; |
c8094d83 | 11083 | |
830bfa74 MM |
11084 | switch (strict) |
11085 | { | |
11086 | case DEDUCE_CALL: | |
11087 | break; | |
11088 | ||
11089 | case DEDUCE_CONV: | |
11090 | { | |
4c7d0dff MM |
11091 | /* Swap PARM and ARG throughout the remainder of this |
11092 | function; the handling is precisely symmetric since PARM | |
11093 | will initialize ARG rather than vice versa. */ | |
830bfa74 MM |
11094 | tree* temp = parm; |
11095 | parm = arg; | |
11096 | arg = temp; | |
11097 | break; | |
11098 | } | |
11099 | ||
11100 | case DEDUCE_EXACT: | |
11101 | /* There is nothing to do in this case. */ | |
62e4a758 | 11102 | return 0; |
830bfa74 MM |
11103 | |
11104 | default: | |
315fb5db | 11105 | gcc_unreachable (); |
830bfa74 MM |
11106 | } |
11107 | ||
11108 | if (TREE_CODE (*parm) != REFERENCE_TYPE) | |
11109 | { | |
11110 | /* [temp.deduct.call] | |
c8094d83 | 11111 | |
830bfa74 | 11112 | If P is not a reference type: |
c8094d83 | 11113 | |
830bfa74 MM |
11114 | --If A is an array type, the pointer type produced by the |
11115 | array-to-pointer standard conversion (_conv.array_) is | |
11116 | used in place of A for type deduction; otherwise, | |
c8094d83 | 11117 | |
830bfa74 MM |
11118 | --If A is a function type, the pointer type produced by |
11119 | the function-to-pointer standard conversion | |
11120 | (_conv.func_) is used in place of A for type deduction; | |
11121 | otherwise, | |
c8094d83 | 11122 | |
830bfa74 MM |
11123 | --If A is a cv-qualified type, the top level |
11124 | cv-qualifiers of A's type are ignored for type | |
11125 | deduction. */ | |
11126 | if (TREE_CODE (*arg) == ARRAY_TYPE) | |
11127 | *arg = build_pointer_type (TREE_TYPE (*arg)); | |
d8f8dca1 | 11128 | else if (TREE_CODE (*arg) == FUNCTION_TYPE) |
830bfa74 MM |
11129 | *arg = build_pointer_type (*arg); |
11130 | else | |
11131 | *arg = TYPE_MAIN_VARIANT (*arg); | |
11132 | } | |
c8094d83 | 11133 | |
830bfa74 | 11134 | /* [temp.deduct.call] |
c8094d83 | 11135 | |
830bfa74 MM |
11136 | If P is a cv-qualified type, the top level cv-qualifiers |
11137 | of P's type are ignored for type deduction. If P is a | |
11138 | reference type, the type referred to by P is used for | |
11139 | type deduction. */ | |
11140 | *parm = TYPE_MAIN_VARIANT (*parm); | |
11141 | if (TREE_CODE (*parm) == REFERENCE_TYPE) | |
62e4a758 NS |
11142 | { |
11143 | *parm = TREE_TYPE (*parm); | |
11144 | result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL; | |
11145 | } | |
1c82cc90 NS |
11146 | |
11147 | /* DR 322. For conversion deduction, remove a reference type on parm | |
11148 | too (which has been swapped into ARG). */ | |
11149 | if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE) | |
11150 | *arg = TREE_TYPE (*arg); | |
c8094d83 | 11151 | |
62e4a758 | 11152 | return result; |
386b8a85 JM |
11153 | } |
11154 | ||
e5214479 | 11155 | /* Most parms like fn_type_unification. |
9f54c803 MM |
11156 | |
11157 | If SUBR is 1, we're being called recursively (to unify the | |
11158 | arguments of a function or method parameter of a function | |
38d18b1a | 11159 | template). */ |
386b8a85 | 11160 | |
4966381a | 11161 | static int |
c8094d83 | 11162 | type_unification_real (tree tparms, |
0cbd7506 MS |
11163 | tree targs, |
11164 | tree xparms, | |
11165 | tree xargs, | |
11166 | int subr, | |
30f86ec3 FJ |
11167 | unification_kind_t strict, |
11168 | int flags) | |
8d08fdba MS |
11169 | { |
11170 | tree parm, arg; | |
11171 | int i; | |
11172 | int ntparms = TREE_VEC_LENGTH (tparms); | |
830bfa74 | 11173 | int sub_strict; |
bd0d5d4a JM |
11174 | int saw_undeduced = 0; |
11175 | tree parms, args; | |
8d08fdba | 11176 | |
50bc768d NS |
11177 | gcc_assert (TREE_CODE (tparms) == TREE_VEC); |
11178 | gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST); | |
11179 | gcc_assert (!xargs || TREE_CODE (xargs) == TREE_LIST); | |
11180 | gcc_assert (ntparms > 0); | |
8d08fdba | 11181 | |
830bfa74 MM |
11182 | switch (strict) |
11183 | { | |
11184 | case DEDUCE_CALL: | |
028d1f20 | 11185 | sub_strict = (UNIFY_ALLOW_OUTER_LEVEL | UNIFY_ALLOW_MORE_CV_QUAL |
0cbd7506 | 11186 | | UNIFY_ALLOW_DERIVED); |
830bfa74 | 11187 | break; |
c8094d83 | 11188 | |
830bfa74 MM |
11189 | case DEDUCE_CONV: |
11190 | sub_strict = UNIFY_ALLOW_LESS_CV_QUAL; | |
11191 | break; | |
11192 | ||
11193 | case DEDUCE_EXACT: | |
11194 | sub_strict = UNIFY_ALLOW_NONE; | |
11195 | break; | |
c8094d83 | 11196 | |
830bfa74 | 11197 | default: |
315fb5db | 11198 | gcc_unreachable (); |
830bfa74 MM |
11199 | } |
11200 | ||
bd0d5d4a JM |
11201 | again: |
11202 | parms = xparms; | |
11203 | args = xargs; | |
bd0d5d4a | 11204 | |
a34d3336 NS |
11205 | while (parms && parms != void_list_node |
11206 | && args && args != void_list_node) | |
8d08fdba | 11207 | { |
5d80a306 DG |
11208 | if (TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION) |
11209 | break; | |
11210 | ||
8d08fdba MS |
11211 | parm = TREE_VALUE (parms); |
11212 | parms = TREE_CHAIN (parms); | |
11213 | arg = TREE_VALUE (args); | |
11214 | args = TREE_CHAIN (args); | |
11215 | ||
11216 | if (arg == error_mark_node) | |
11217 | return 1; | |
11218 | if (arg == unknown_type_node) | |
34016c81 JM |
11219 | /* We can't deduce anything from this, but we might get all the |
11220 | template args from other function args. */ | |
11221 | continue; | |
b7484fbe | 11222 | |
03e70705 JM |
11223 | /* Conversions will be performed on a function argument that |
11224 | corresponds with a function parameter that contains only | |
11225 | non-deducible template parameters and explicitly specified | |
11226 | template parameters. */ | |
c353b8e3 | 11227 | if (!uses_template_parms (parm)) |
b7484fbe | 11228 | { |
03e70705 JM |
11229 | tree type; |
11230 | ||
2f939d94 | 11231 | if (!TYPE_P (arg)) |
03e70705 JM |
11232 | type = TREE_TYPE (arg); |
11233 | else | |
c353b8e3 | 11234 | type = arg; |
03e70705 | 11235 | |
fad86f7a | 11236 | if (same_type_p (parm, type)) |
343c89cd | 11237 | continue; |
fad86f7a | 11238 | if (strict != DEDUCE_EXACT |
3db45ab5 | 11239 | && can_convert_arg (parm, type, TYPE_P (arg) ? NULL_TREE : arg, |
30f86ec3 | 11240 | flags)) |
fad86f7a | 11241 | continue; |
3db45ab5 | 11242 | |
b7484fbe MS |
11243 | return 1; |
11244 | } | |
c8094d83 | 11245 | |
2f939d94 | 11246 | if (!TYPE_P (arg)) |
8d08fdba | 11247 | { |
50bc768d | 11248 | gcc_assert (TREE_TYPE (arg) != NULL_TREE); |
34016c81 | 11249 | if (type_unknown_p (arg)) |
28cbf42c | 11250 | { |
34016c81 JM |
11251 | /* [temp.deduct.type] A template-argument can be deduced from |
11252 | a pointer to function or pointer to member function | |
11253 | argument if the set of overloaded functions does not | |
11254 | contain function templates and at most one of a set of | |
11255 | overloaded functions provides a unique match. */ | |
11256 | ||
11257 | if (resolve_overloaded_unification | |
4393e105 | 11258 | (tparms, targs, parm, arg, strict, sub_strict) |
34016c81 JM |
11259 | != 0) |
11260 | return 1; | |
11261 | continue; | |
28cbf42c | 11262 | } |
8d08fdba | 11263 | arg = TREE_TYPE (arg); |
08476342 NS |
11264 | if (arg == error_mark_node) |
11265 | return 1; | |
8d08fdba | 11266 | } |
c8094d83 | 11267 | |
62e4a758 | 11268 | { |
0cbd7506 | 11269 | int arg_strict = sub_strict; |
c8094d83 | 11270 | |
0cbd7506 | 11271 | if (!subr) |
62e4a758 | 11272 | arg_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg); |
4393e105 | 11273 | |
0cbd7506 MS |
11274 | if (unify (tparms, targs, parm, arg, arg_strict)) |
11275 | return 1; | |
62e4a758 | 11276 | } |
8d08fdba | 11277 | } |
c8094d83 | 11278 | |
5d80a306 DG |
11279 | |
11280 | if (parms | |
11281 | && parms != void_list_node | |
11282 | && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION) | |
11283 | { | |
11284 | /* Unify the remaining arguments with the pack expansion type. */ | |
11285 | tree argvec; | |
11286 | tree parmvec = make_tree_vec (1); | |
11287 | int len = 0; | |
11288 | tree t; | |
11289 | ||
11290 | /* Count the number of arguments that remain. */ | |
11291 | for (t = args; t && t != void_list_node; t = TREE_CHAIN (t)) | |
11292 | len++; | |
11293 | ||
11294 | /* Allocate a TREE_VEC and copy in all of the arguments */ | |
11295 | argvec = make_tree_vec (len); | |
11296 | for (i = 0; args && args != void_list_node; args = TREE_CHAIN (args)) | |
11297 | { | |
11298 | TREE_VEC_ELT (argvec, i) = TREE_VALUE (args); | |
11299 | ++i; | |
11300 | } | |
11301 | ||
11302 | /* Copy the parameter into parmvec. */ | |
11303 | TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms); | |
11304 | if (unify_pack_expansion (tparms, targs, parmvec, argvec, strict, | |
11305 | /*call_args_p=*/true, /*subr=*/subr)) | |
11306 | return 1; | |
11307 | ||
11308 | /* Advance to the end of the list of parameters. */ | |
11309 | parms = TREE_CHAIN (parms); | |
11310 | } | |
11311 | ||
8d08fdba MS |
11312 | /* Fail if we've reached the end of the parm list, and more args |
11313 | are present, and the parm list isn't variadic. */ | |
11314 | if (args && args != void_list_node && parms == void_list_node) | |
11315 | return 1; | |
f4f206f4 | 11316 | /* Fail if parms are left and they don't have default values. */ |
a34d3336 | 11317 | if (parms && parms != void_list_node |
8d08fdba MS |
11318 | && TREE_PURPOSE (parms) == NULL_TREE) |
11319 | return 1; | |
bd0d5d4a | 11320 | |
8d08fdba MS |
11321 | if (!subr) |
11322 | for (i = 0; i < ntparms; i++) | |
a34d3336 | 11323 | if (!TREE_VEC_ELT (targs, i)) |
8d08fdba | 11324 | { |
2d8ba2c7 LM |
11325 | tree tparm; |
11326 | ||
11327 | if (TREE_VEC_ELT (tparms, i) == error_mark_node) | |
11328 | continue; | |
11329 | ||
11330 | tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i)); | |
bd0d5d4a JM |
11331 | |
11332 | /* If this is an undeduced nontype parameter that depends on | |
11333 | a type parameter, try another pass; its type may have been | |
11334 | deduced from a later argument than the one from which | |
11335 | this parameter can be deduced. */ | |
11336 | if (TREE_CODE (tparm) == PARM_DECL | |
11337 | && uses_template_parms (TREE_TYPE (tparm)) | |
11338 | && !saw_undeduced++) | |
11339 | goto again; | |
11340 | ||
8d08fdba MS |
11341 | return 2; |
11342 | } | |
c8094d83 | 11343 | |
8d08fdba MS |
11344 | return 0; |
11345 | } | |
11346 | ||
34016c81 JM |
11347 | /* Subroutine of type_unification_real. Args are like the variables at the |
11348 | call site. ARG is an overloaded function (or template-id); we try | |
11349 | deducing template args from each of the overloads, and if only one | |
11350 | succeeds, we go with that. Modifies TARGS and returns 0 on success. */ | |
11351 | ||
11352 | static int | |
c8094d83 | 11353 | resolve_overloaded_unification (tree tparms, |
0cbd7506 MS |
11354 | tree targs, |
11355 | tree parm, | |
11356 | tree arg, | |
11357 | unification_kind_t strict, | |
3a978d72 | 11358 | int sub_strict) |
34016c81 JM |
11359 | { |
11360 | tree tempargs = copy_node (targs); | |
11361 | int good = 0; | |
f23fb7f5 | 11362 | bool addr_p; |
34016c81 JM |
11363 | |
11364 | if (TREE_CODE (arg) == ADDR_EXPR) | |
f23fb7f5 MM |
11365 | { |
11366 | arg = TREE_OPERAND (arg, 0); | |
11367 | addr_p = true; | |
11368 | } | |
11369 | else | |
11370 | addr_p = false; | |
9f3d9e46 | 11371 | |
d8f8dca1 MM |
11372 | if (TREE_CODE (arg) == COMPONENT_REF) |
11373 | /* Handle `&x' where `x' is some static or non-static member | |
11374 | function name. */ | |
11375 | arg = TREE_OPERAND (arg, 1); | |
11376 | ||
05e0b2f4 JM |
11377 | if (TREE_CODE (arg) == OFFSET_REF) |
11378 | arg = TREE_OPERAND (arg, 1); | |
11379 | ||
9f3d9e46 | 11380 | /* Strip baselink information. */ |
50ad9642 MM |
11381 | if (BASELINK_P (arg)) |
11382 | arg = BASELINK_FUNCTIONS (arg); | |
9f3d9e46 | 11383 | |
34016c81 JM |
11384 | if (TREE_CODE (arg) == TEMPLATE_ID_EXPR) |
11385 | { | |
11386 | /* If we got some explicit template args, we need to plug them into | |
11387 | the affected templates before we try to unify, in case the | |
11388 | explicit args will completely resolve the templates in question. */ | |
11389 | ||
11390 | tree expl_subargs = TREE_OPERAND (arg, 1); | |
11391 | arg = TREE_OPERAND (arg, 0); | |
11392 | ||
11393 | for (; arg; arg = OVL_NEXT (arg)) | |
11394 | { | |
11395 | tree fn = OVL_CURRENT (arg); | |
11396 | tree subargs, elem; | |
11397 | ||
11398 | if (TREE_CODE (fn) != TEMPLATE_DECL) | |
11399 | continue; | |
11400 | ||
a34d3336 NS |
11401 | subargs = get_bindings (fn, DECL_TEMPLATE_RESULT (fn), |
11402 | expl_subargs, /*check_ret=*/false); | |
34016c81 JM |
11403 | if (subargs) |
11404 | { | |
c2ea3a40 | 11405 | elem = tsubst (TREE_TYPE (fn), subargs, tf_none, NULL_TREE); |
c8094d83 | 11406 | good += try_one_overload (tparms, targs, tempargs, parm, |
f23fb7f5 | 11407 | elem, strict, sub_strict, addr_p); |
34016c81 JM |
11408 | } |
11409 | } | |
11410 | } | |
315fb5db | 11411 | else |
34016c81 | 11412 | { |
315fb5db NS |
11413 | gcc_assert (TREE_CODE (arg) == OVERLOAD |
11414 | || TREE_CODE (arg) == FUNCTION_DECL); | |
c8094d83 | 11415 | |
34016c81 | 11416 | for (; arg; arg = OVL_NEXT (arg)) |
f23fb7f5 MM |
11417 | good += try_one_overload (tparms, targs, tempargs, parm, |
11418 | TREE_TYPE (OVL_CURRENT (arg)), | |
11419 | strict, sub_strict, addr_p); | |
34016c81 | 11420 | } |
34016c81 JM |
11421 | |
11422 | /* [temp.deduct.type] A template-argument can be deduced from a pointer | |
11423 | to function or pointer to member function argument if the set of | |
11424 | overloaded functions does not contain function templates and at most | |
11425 | one of a set of overloaded functions provides a unique match. | |
11426 | ||
11427 | So if we found multiple possibilities, we return success but don't | |
11428 | deduce anything. */ | |
11429 | ||
11430 | if (good == 1) | |
11431 | { | |
11432 | int i = TREE_VEC_LENGTH (targs); | |
11433 | for (; i--; ) | |
11434 | if (TREE_VEC_ELT (tempargs, i)) | |
11435 | TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (tempargs, i); | |
11436 | } | |
11437 | if (good) | |
11438 | return 0; | |
11439 | ||
11440 | return 1; | |
11441 | } | |
11442 | ||
11443 | /* Subroutine of resolve_overloaded_unification; does deduction for a single | |
11444 | overload. Fills TARGS with any deduced arguments, or error_mark_node if | |
11445 | different overloads deduce different arguments for a given parm. | |
f23fb7f5 MM |
11446 | ADDR_P is true if the expression for which deduction is being |
11447 | performed was of the form "& fn" rather than simply "fn". | |
11448 | ||
34016c81 JM |
11449 | Returns 1 on success. */ |
11450 | ||
11451 | static int | |
3a978d72 | 11452 | try_one_overload (tree tparms, |
0cbd7506 MS |
11453 | tree orig_targs, |
11454 | tree targs, | |
11455 | tree parm, | |
11456 | tree arg, | |
11457 | unification_kind_t strict, | |
f23fb7f5 MM |
11458 | int sub_strict, |
11459 | bool addr_p) | |
34016c81 JM |
11460 | { |
11461 | int nargs; | |
11462 | tree tempargs; | |
11463 | int i; | |
11464 | ||
11465 | /* [temp.deduct.type] A template-argument can be deduced from a pointer | |
11466 | to function or pointer to member function argument if the set of | |
11467 | overloaded functions does not contain function templates and at most | |
11468 | one of a set of overloaded functions provides a unique match. | |
11469 | ||
11470 | So if this is a template, just return success. */ | |
11471 | ||
11472 | if (uses_template_parms (arg)) | |
11473 | return 1; | |
11474 | ||
f23fb7f5 MM |
11475 | if (TREE_CODE (arg) == METHOD_TYPE) |
11476 | arg = build_ptrmemfunc_type (build_pointer_type (arg)); | |
11477 | else if (addr_p) | |
11478 | arg = build_pointer_type (arg); | |
11479 | ||
62e4a758 | 11480 | sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg); |
34016c81 JM |
11481 | |
11482 | /* We don't copy orig_targs for this because if we have already deduced | |
11483 | some template args from previous args, unify would complain when we | |
11484 | try to deduce a template parameter for the same argument, even though | |
11485 | there isn't really a conflict. */ | |
11486 | nargs = TREE_VEC_LENGTH (targs); | |
f31c0a32 | 11487 | tempargs = make_tree_vec (nargs); |
34016c81 | 11488 | |
4393e105 | 11489 | if (unify (tparms, tempargs, parm, arg, sub_strict) != 0) |
34016c81 JM |
11490 | return 0; |
11491 | ||
11492 | /* First make sure we didn't deduce anything that conflicts with | |
e97e5263 | 11493 | explicitly specified args. */ |
34016c81 JM |
11494 | for (i = nargs; i--; ) |
11495 | { | |
11496 | tree elt = TREE_VEC_ELT (tempargs, i); | |
e97e5263 | 11497 | tree oldelt = TREE_VEC_ELT (orig_targs, i); |
34016c81 | 11498 | |
a34d3336 NS |
11499 | if (!elt) |
11500 | /*NOP*/; | |
34016c81 | 11501 | else if (uses_template_parms (elt)) |
a34d3336 NS |
11502 | /* Since we're unifying against ourselves, we will fill in |
11503 | template args used in the function parm list with our own | |
11504 | template parms. Discard them. */ | |
11505 | TREE_VEC_ELT (tempargs, i) = NULL_TREE; | |
11506 | else if (oldelt && !template_args_equal (oldelt, elt)) | |
34016c81 JM |
11507 | return 0; |
11508 | } | |
11509 | ||
11510 | for (i = nargs; i--; ) | |
11511 | { | |
11512 | tree elt = TREE_VEC_ELT (tempargs, i); | |
11513 | ||
11514 | if (elt) | |
11515 | TREE_VEC_ELT (targs, i) = elt; | |
11516 | } | |
11517 | ||
11518 | return 1; | |
11519 | } | |
11520 | ||
4393e105 MM |
11521 | /* PARM is a template class (perhaps with unbound template |
11522 | parameters). ARG is a fully instantiated type. If ARG can be | |
11523 | bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and | |
11524 | TARGS are as for unify. */ | |
fcfb9f96 MM |
11525 | |
11526 | static tree | |
3a978d72 | 11527 | try_class_unification (tree tparms, tree targs, tree parm, tree arg) |
4393e105 | 11528 | { |
4393e105 MM |
11529 | tree copy_of_targs; |
11530 | ||
11531 | if (!CLASSTYPE_TEMPLATE_INFO (arg) | |
c8094d83 | 11532 | || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg)) |
68361a03 | 11533 | != most_general_template (CLASSTYPE_TI_TEMPLATE (parm)))) |
4393e105 MM |
11534 | return NULL_TREE; |
11535 | ||
11536 | /* We need to make a new template argument vector for the call to | |
11537 | unify. If we used TARGS, we'd clutter it up with the result of | |
11538 | the attempted unification, even if this class didn't work out. | |
11539 | We also don't want to commit ourselves to all the unifications | |
11540 | we've already done, since unification is supposed to be done on | |
11541 | an argument-by-argument basis. In other words, consider the | |
11542 | following pathological case: | |
11543 | ||
11544 | template <int I, int J, int K> | |
11545 | struct S {}; | |
c8094d83 | 11546 | |
4393e105 MM |
11547 | template <int I, int J> |
11548 | struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {}; | |
c8094d83 | 11549 | |
4393e105 MM |
11550 | template <int I, int J, int K> |
11551 | void f(S<I, J, K>, S<I, I, I>); | |
c8094d83 | 11552 | |
4393e105 | 11553 | void g() { |
0cbd7506 MS |
11554 | S<0, 0, 0> s0; |
11555 | S<0, 1, 2> s2; | |
c8094d83 | 11556 | |
0cbd7506 | 11557 | f(s0, s2); |
4393e105 MM |
11558 | } |
11559 | ||
11560 | Now, by the time we consider the unification involving `s2', we | |
11561 | already know that we must have `f<0, 0, 0>'. But, even though | |
0e339752 | 11562 | `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid |
4393e105 MM |
11563 | because there are two ways to unify base classes of S<0, 1, 2> |
11564 | with S<I, I, I>. If we kept the already deduced knowledge, we | |
11565 | would reject the possibility I=1. */ | |
f31c0a32 | 11566 | copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs)); |
c8094d83 | 11567 | |
4393e105 | 11568 | /* If unification failed, we're done. */ |
74601d7c KL |
11569 | if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm), |
11570 | CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE)) | |
4393e105 | 11571 | return NULL_TREE; |
74601d7c KL |
11572 | |
11573 | return arg; | |
4393e105 MM |
11574 | } |
11575 | ||
a3a0fc7f NS |
11576 | /* Given a template type PARM and a class type ARG, find the unique |
11577 | base type in ARG that is an instance of PARM. We do not examine | |
11578 | ARG itself; only its base-classes. If there is not exactly one | |
11579 | appropriate base class, return NULL_TREE. PARM may be the type of | |
11580 | a partial specialization, as well as a plain template type. Used | |
11581 | by unify. */ | |
4393e105 MM |
11582 | |
11583 | static tree | |
a3a0fc7f | 11584 | get_template_base (tree tparms, tree targs, tree parm, tree arg) |
fcfb9f96 | 11585 | { |
a3a0fc7f NS |
11586 | tree rval = NULL_TREE; |
11587 | tree binfo; | |
11588 | ||
11589 | gcc_assert (IS_AGGR_TYPE_CODE (TREE_CODE (arg))); | |
c8094d83 | 11590 | |
a3a0fc7f NS |
11591 | binfo = TYPE_BINFO (complete_type (arg)); |
11592 | if (!binfo) | |
11593 | /* The type could not be completed. */ | |
11594 | return NULL_TREE; | |
fcfb9f96 | 11595 | |
a3a0fc7f NS |
11596 | /* Walk in inheritance graph order. The search order is not |
11597 | important, and this avoids multiple walks of virtual bases. */ | |
11598 | for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo)) | |
fcfb9f96 | 11599 | { |
a3a0fc7f | 11600 | tree r = try_class_unification (tparms, targs, parm, BINFO_TYPE (binfo)); |
fcfb9f96 | 11601 | |
8d83768f NS |
11602 | if (r) |
11603 | { | |
11604 | /* If there is more than one satisfactory baseclass, then: | |
fcfb9f96 | 11605 | |
8d83768f | 11606 | [temp.deduct.call] |
fcfb9f96 | 11607 | |
8d83768f NS |
11608 | If they yield more than one possible deduced A, the type |
11609 | deduction fails. | |
4393e105 | 11610 | |
8d83768f | 11611 | applies. */ |
a3a0fc7f NS |
11612 | if (rval && !same_type_p (r, rval)) |
11613 | return NULL_TREE; | |
c8094d83 | 11614 | |
a3a0fc7f | 11615 | rval = r; |
8d83768f | 11616 | } |
fcfb9f96 MM |
11617 | } |
11618 | ||
a3a0fc7f | 11619 | return rval; |
fcfb9f96 MM |
11620 | } |
11621 | ||
db2767b6 MM |
11622 | /* Returns the level of DECL, which declares a template parameter. */ |
11623 | ||
e9659ab0 | 11624 | static int |
3a978d72 | 11625 | template_decl_level (tree decl) |
db2767b6 MM |
11626 | { |
11627 | switch (TREE_CODE (decl)) | |
11628 | { | |
11629 | case TYPE_DECL: | |
11630 | case TEMPLATE_DECL: | |
11631 | return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl)); | |
11632 | ||
11633 | case PARM_DECL: | |
11634 | return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl)); | |
11635 | ||
11636 | default: | |
315fb5db | 11637 | gcc_unreachable (); |
db2767b6 | 11638 | } |
315fb5db | 11639 | return 0; |
db2767b6 MM |
11640 | } |
11641 | ||
830bfa74 MM |
11642 | /* Decide whether ARG can be unified with PARM, considering only the |
11643 | cv-qualifiers of each type, given STRICT as documented for unify. | |
324f9dfb | 11644 | Returns nonzero iff the unification is OK on that basis. */ |
e92cc029 | 11645 | |
e9659ab0 | 11646 | static int |
3a978d72 | 11647 | check_cv_quals_for_unify (int strict, tree arg, tree parm) |
830bfa74 | 11648 | { |
4f2b0fb2 NS |
11649 | int arg_quals = cp_type_quals (arg); |
11650 | int parm_quals = cp_type_quals (parm); | |
11651 | ||
355f774d NS |
11652 | if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM |
11653 | && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL)) | |
4f2b0fb2 | 11654 | { |
2e9ceb77 | 11655 | /* Although a CVR qualifier is ignored when being applied to a |
0cbd7506 MS |
11656 | substituted template parameter ([8.3.2]/1 for example), that |
11657 | does not apply during deduction [14.8.2.4]/1, (even though | |
11658 | that is not explicitly mentioned, [14.8.2.4]/9 indicates | |
11659 | this). Except when we're allowing additional CV qualifiers | |
11660 | at the outer level [14.8.2.1]/3,1st bullet. */ | |
2e9ceb77 NS |
11661 | if ((TREE_CODE (arg) == REFERENCE_TYPE |
11662 | || TREE_CODE (arg) == FUNCTION_TYPE | |
11663 | || TREE_CODE (arg) == METHOD_TYPE) | |
11664 | && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE))) | |
11665 | return 0; | |
11666 | ||
11667 | if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM) | |
11668 | && (parm_quals & TYPE_QUAL_RESTRICT)) | |
11669 | return 0; | |
4f2b0fb2 | 11670 | } |
2e9ceb77 | 11671 | |
62e4a758 | 11672 | if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL)) |
4f2b0fb2 | 11673 | && (arg_quals & parm_quals) != parm_quals) |
ef637255 MM |
11674 | return 0; |
11675 | ||
62e4a758 | 11676 | if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL)) |
4f2b0fb2 | 11677 | && (parm_quals & arg_quals) != arg_quals) |
ef637255 MM |
11678 | return 0; |
11679 | ||
ef637255 | 11680 | return 1; |
830bfa74 MM |
11681 | } |
11682 | ||
5d80a306 DG |
11683 | /* Determines the LEVEL and INDEX for the template parameter PARM. */ |
11684 | void | |
11685 | template_parm_level_and_index (tree parm, int* level, int* index) | |
11686 | { | |
11687 | if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM | |
11688 | || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM | |
11689 | || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM) | |
11690 | { | |
11691 | *index = TEMPLATE_TYPE_IDX (parm); | |
11692 | *level = TEMPLATE_TYPE_LEVEL (parm); | |
11693 | } | |
11694 | else | |
11695 | { | |
11696 | *index = TEMPLATE_PARM_IDX (parm); | |
11697 | *level = TEMPLATE_PARM_LEVEL (parm); | |
11698 | } | |
11699 | } | |
11700 | ||
11701 | /* Unifies the remaining arguments in PACKED_ARGS with the pack | |
11702 | expansion at the end of PACKED_PARMS. Returns 0 if the type | |
11703 | deduction succeeds, 1 otherwise. STRICT is the same as in | |
11704 | unify. CALL_ARGS_P is true iff PACKED_ARGS is actually a function | |
11705 | call argument list. We'll need to adjust the arguments to make them | |
11706 | types. SUBR tells us if this is from a recursive call to | |
11707 | type_unification_real. */ | |
11708 | int | |
11709 | unify_pack_expansion (tree tparms, tree targs, tree packed_parms, | |
11710 | tree packed_args, int strict, bool call_args_p, | |
11711 | bool subr) | |
11712 | { | |
11713 | tree parm | |
11714 | = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1); | |
11715 | tree pattern = PACK_EXPANSION_PATTERN (parm); | |
11716 | tree pack, packs = NULL_TREE; | |
11717 | int i, start = TREE_VEC_LENGTH (packed_parms) - 1; | |
11718 | int len = TREE_VEC_LENGTH (packed_args); | |
11719 | ||
11720 | /* Determine the parameter packs we will be deducing from the | |
11721 | pattern, and record their current deductions. */ | |
11722 | for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm); | |
11723 | pack; pack = TREE_CHAIN (pack)) | |
11724 | { | |
11725 | tree parm_pack = TREE_VALUE (pack); | |
11726 | int idx, level; | |
11727 | ||
11728 | /* Determine the index and level of this parameter pack. */ | |
11729 | template_parm_level_and_index (parm_pack, &level, &idx); | |
11730 | ||
11731 | /* Keep track of the parameter packs and their corresponding | |
11732 | argument packs. */ | |
11733 | packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs); | |
11734 | TREE_TYPE (packs) = make_tree_vec (len - start); | |
11735 | } | |
11736 | ||
11737 | /* Loop through all of the arguments that have not yet been | |
11738 | unified and unify each with the pattern. */ | |
11739 | for (i = start; i < len; i++) | |
11740 | { | |
11741 | tree parm = pattern; | |
11742 | ||
11743 | /* For each parameter pack, clear out the deduced value so that | |
11744 | we can deduce it again. */ | |
11745 | for (pack = packs; pack; pack = TREE_CHAIN (pack)) | |
11746 | { | |
11747 | int idx, level; | |
11748 | template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx); | |
11749 | ||
11750 | TMPL_ARG (targs, level, idx) = NULL_TREE; | |
11751 | } | |
11752 | ||
11753 | /* Unify the pattern with the current argument. */ | |
11754 | { | |
11755 | tree arg = TREE_VEC_ELT (packed_args, i); | |
11756 | int arg_strict = strict; | |
11757 | bool skip_arg_p = false; | |
11758 | ||
11759 | if (call_args_p) | |
11760 | { | |
11761 | int sub_strict; | |
11762 | ||
11763 | /* This mirrors what we do in type_unification_real. */ | |
11764 | switch (strict) | |
11765 | { | |
11766 | case DEDUCE_CALL: | |
11767 | sub_strict = (UNIFY_ALLOW_OUTER_LEVEL | |
11768 | | UNIFY_ALLOW_MORE_CV_QUAL | |
11769 | | UNIFY_ALLOW_DERIVED); | |
11770 | break; | |
11771 | ||
11772 | case DEDUCE_CONV: | |
11773 | sub_strict = UNIFY_ALLOW_LESS_CV_QUAL; | |
11774 | break; | |
11775 | ||
11776 | case DEDUCE_EXACT: | |
11777 | sub_strict = UNIFY_ALLOW_NONE; | |
11778 | break; | |
11779 | ||
11780 | default: | |
11781 | gcc_unreachable (); | |
11782 | } | |
11783 | ||
11784 | if (!TYPE_P (arg)) | |
11785 | { | |
11786 | gcc_assert (TREE_TYPE (arg) != NULL_TREE); | |
11787 | if (type_unknown_p (arg)) | |
11788 | { | |
11789 | /* [temp.deduct.type] A template-argument can be | |
11790 | deduced from a pointer to function or pointer | |
11791 | to member function argument if the set of | |
11792 | overloaded functions does not contain function | |
11793 | templates and at most one of a set of | |
11794 | overloaded functions provides a unique | |
11795 | match. */ | |
11796 | ||
11797 | if (resolve_overloaded_unification | |
11798 | (tparms, targs, parm, arg, strict, sub_strict) | |
11799 | != 0) | |
11800 | return 1; | |
11801 | skip_arg_p = true; | |
11802 | } | |
11803 | ||
11804 | if (!skip_arg_p) | |
11805 | { | |
11806 | arg = TREE_TYPE (arg); | |
11807 | if (arg == error_mark_node) | |
11808 | return 1; | |
11809 | } | |
11810 | } | |
11811 | ||
11812 | arg_strict = sub_strict; | |
11813 | ||
11814 | if (!subr) | |
11815 | arg_strict |= | |
11816 | maybe_adjust_types_for_deduction (strict, &parm, &arg); | |
11817 | } | |
11818 | ||
11819 | if (!skip_arg_p) | |
11820 | { | |
11821 | if (unify (tparms, targs, parm, arg, arg_strict)) | |
11822 | return 1; | |
11823 | } | |
11824 | } | |
11825 | ||
11826 | /* For each parameter pack, collect the deduced value. */ | |
11827 | for (pack = packs; pack; pack = TREE_CHAIN (pack)) | |
11828 | { | |
11829 | int idx, level; | |
11830 | template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx); | |
11831 | ||
11832 | TREE_VEC_ELT (TREE_TYPE (pack), i - start) = | |
11833 | TMPL_ARG (targs, level, idx); | |
11834 | } | |
11835 | } | |
11836 | ||
11837 | /* Verify that the results of unification with the parameter packs | |
11838 | produce results consistent with what we've seen before, and make | |
11839 | the deduced argument packs available. */ | |
11840 | for (pack = packs; pack; pack = TREE_CHAIN (pack)) | |
11841 | { | |
11842 | tree old_pack = TREE_VALUE (pack); | |
11843 | tree new_args = TREE_TYPE (pack); | |
11844 | ||
11845 | if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack)) | |
11846 | { | |
11847 | /* Prepend the explicit arguments onto NEW_ARGS. */ | |
11848 | tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack); | |
11849 | tree old_args = new_args; | |
11850 | int i, explicit_len = TREE_VEC_LENGTH (explicit_args); | |
11851 | int len = explicit_len + TREE_VEC_LENGTH (old_args); | |
11852 | ||
11853 | /* Copy the explicit arguments. */ | |
11854 | new_args = make_tree_vec (len); | |
11855 | for (i = 0; i < explicit_len; i++) | |
11856 | TREE_VEC_ELT (new_args, i) = TREE_VEC_ELT (explicit_args, i); | |
11857 | ||
11858 | /* Copy the deduced arguments. */ | |
11859 | for (; i < len; i++) | |
11860 | TREE_VEC_ELT (new_args, i) = | |
11861 | TREE_VEC_ELT (old_args, i - explicit_len); | |
11862 | } | |
11863 | ||
11864 | if (!old_pack) | |
11865 | { | |
11866 | tree result; | |
11867 | int idx, level; | |
11868 | ||
11869 | template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx); | |
11870 | ||
11871 | /* Build the deduced *_ARGUMENT_PACK. */ | |
11872 | if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX) | |
11873 | { | |
11874 | result = make_node (NONTYPE_ARGUMENT_PACK); | |
11875 | TREE_TYPE (result) = | |
11876 | TREE_TYPE (TEMPLATE_PARM_DECL (TREE_PURPOSE (pack))); | |
11877 | TREE_CONSTANT (result) = 1; | |
11878 | } | |
11879 | else | |
11880 | result = make_node (TYPE_ARGUMENT_PACK); | |
11881 | ||
11882 | SET_ARGUMENT_PACK_ARGS (result, new_args); | |
11883 | ||
11884 | /* Note the deduced argument packs for this parameter | |
11885 | pack. */ | |
11886 | TMPL_ARG (targs, level, idx) = result; | |
11887 | } | |
11888 | else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack) | |
11889 | && (ARGUMENT_PACK_ARGS (old_pack) | |
11890 | == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack))) | |
11891 | { | |
11892 | /* We only had the explicitly-provided arguments before, but | |
11893 | now we have a complete set of arguments. */ | |
11894 | int idx, level; | |
11895 | tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack); | |
11896 | template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx); | |
11897 | ||
11898 | /* Keep the original deduced argument pack. */ | |
11899 | TMPL_ARG (targs, level, idx) = old_pack; | |
11900 | ||
11901 | SET_ARGUMENT_PACK_ARGS (old_pack, new_args); | |
11902 | ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1; | |
11903 | ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args; | |
11904 | } | |
11905 | else if (!comp_template_args (ARGUMENT_PACK_ARGS (old_pack), | |
11906 | new_args)) | |
11907 | /* Inconsistent unification of this parameter pack. */ | |
11908 | return 1; | |
11909 | else | |
11910 | { | |
11911 | int idx, level; | |
11912 | ||
11913 | template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx); | |
11914 | ||
11915 | /* Keep the original deduced argument pack. */ | |
11916 | TMPL_ARG (targs, level, idx) = old_pack; | |
11917 | } | |
11918 | } | |
11919 | ||
11920 | return 0; | |
11921 | } | |
11922 | ||
916b63c3 MM |
11923 | /* Deduce the value of template parameters. TPARMS is the (innermost) |
11924 | set of template parameters to a template. TARGS is the bindings | |
11925 | for those template parameters, as determined thus far; TARGS may | |
11926 | include template arguments for outer levels of template parameters | |
11927 | as well. PARM is a parameter to a template function, or a | |
11928 | subcomponent of that parameter; ARG is the corresponding argument. | |
11929 | This function attempts to match PARM with ARG in a manner | |
11930 | consistent with the existing assignments in TARGS. If more values | |
11931 | are deduced, then TARGS is updated. | |
11932 | ||
11933 | Returns 0 if the type deduction succeeds, 1 otherwise. The | |
11934 | parameter STRICT is a bitwise or of the following flags: | |
830bfa74 MM |
11935 | |
11936 | UNIFY_ALLOW_NONE: | |
11937 | Require an exact match between PARM and ARG. | |
11938 | UNIFY_ALLOW_MORE_CV_QUAL: | |
028d1f20 NS |
11939 | Allow the deduced ARG to be more cv-qualified (by qualification |
11940 | conversion) than ARG. | |
830bfa74 MM |
11941 | UNIFY_ALLOW_LESS_CV_QUAL: |
11942 | Allow the deduced ARG to be less cv-qualified than ARG. | |
11943 | UNIFY_ALLOW_DERIVED: | |
11944 | Allow the deduced ARG to be a template base class of ARG, | |
11945 | or a pointer to a template base class of the type pointed to by | |
161c12b0 JM |
11946 | ARG. |
11947 | UNIFY_ALLOW_INTEGER: | |
11948 | Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX | |
c8094d83 | 11949 | case for more information. |
028d1f20 NS |
11950 | UNIFY_ALLOW_OUTER_LEVEL: |
11951 | This is the outermost level of a deduction. Used to determine validity | |
11952 | of qualification conversions. A valid qualification conversion must | |
11953 | have const qualified pointers leading up to the inner type which | |
11954 | requires additional CV quals, except at the outer level, where const | |
11955 | is not required [conv.qual]. It would be normal to set this flag in | |
62e4a758 NS |
11956 | addition to setting UNIFY_ALLOW_MORE_CV_QUAL. |
11957 | UNIFY_ALLOW_OUTER_MORE_CV_QUAL: | |
11958 | This is the outermost level of a deduction, and PARM can be more CV | |
11959 | qualified at this point. | |
11960 | UNIFY_ALLOW_OUTER_LESS_CV_QUAL: | |
11961 | This is the outermost level of a deduction, and PARM can be less CV | |
8baddbf1 | 11962 | qualified at this point. */ |
830bfa74 | 11963 | |
e9659ab0 | 11964 | static int |
3a978d72 | 11965 | unify (tree tparms, tree targs, tree parm, tree arg, int strict) |
8d08fdba MS |
11966 | { |
11967 | int idx; | |
050367a3 | 11968 | tree targ; |
db2767b6 | 11969 | tree tparm; |
028d1f20 | 11970 | int strict_in = strict; |
8d08fdba MS |
11971 | |
11972 | /* I don't think this will do the right thing with respect to types. | |
11973 | But the only case I've seen it in so far has been array bounds, where | |
11974 | signedness is the only information lost, and I think that will be | |
11975 | okay. */ | |
11976 | while (TREE_CODE (parm) == NOP_EXPR) | |
11977 | parm = TREE_OPERAND (parm, 0); | |
11978 | ||
11979 | if (arg == error_mark_node) | |
11980 | return 1; | |
11981 | if (arg == unknown_type_node) | |
34016c81 JM |
11982 | /* We can't deduce anything from this, but we might get all the |
11983 | template args from other function args. */ | |
11984 | return 0; | |
11985 | ||
db2767b6 | 11986 | /* If PARM uses template parameters, then we can't bail out here, |
6bdb985f | 11987 | even if ARG == PARM, since we won't record unifications for the |
db2767b6 MM |
11988 | template parameters. We might need them if we're trying to |
11989 | figure out which of two things is more specialized. */ | |
11990 | if (arg == parm && !uses_template_parms (parm)) | |
8d08fdba MS |
11991 | return 0; |
11992 | ||
830bfa74 MM |
11993 | /* Immediately reject some pairs that won't unify because of |
11994 | cv-qualification mismatches. */ | |
11995 | if (TREE_CODE (arg) == TREE_CODE (parm) | |
2f939d94 | 11996 | && TYPE_P (arg) |
d0ab7624 | 11997 | /* It is the elements of the array which hold the cv quals of an array |
0cbd7506 MS |
11998 | type, and the elements might be template type parms. We'll check |
11999 | when we recurse. */ | |
d0ab7624 | 12000 | && TREE_CODE (arg) != ARRAY_TYPE |
830bfa74 MM |
12001 | /* We check the cv-qualifiers when unifying with template type |
12002 | parameters below. We want to allow ARG `const T' to unify with | |
12003 | PARM `T' for example, when computing which of two templates | |
12004 | is more specialized, for example. */ | |
12005 | && TREE_CODE (arg) != TEMPLATE_TYPE_PARM | |
028d1f20 | 12006 | && !check_cv_quals_for_unify (strict_in, arg, parm)) |
49432171 JM |
12007 | return 1; |
12008 | ||
028d1f20 | 12009 | if (!(strict & UNIFY_ALLOW_OUTER_LEVEL) |
3ea099f1 | 12010 | && TYPE_P (parm) && !CP_TYPE_CONST_P (parm)) |
028d1f20 NS |
12011 | strict &= ~UNIFY_ALLOW_MORE_CV_QUAL; |
12012 | strict &= ~UNIFY_ALLOW_OUTER_LEVEL; | |
12013 | strict &= ~UNIFY_ALLOW_DERIVED; | |
62e4a758 NS |
12014 | strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL; |
12015 | strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL; | |
c8094d83 | 12016 | |
8d08fdba MS |
12017 | switch (TREE_CODE (parm)) |
12018 | { | |
2ca340ae | 12019 | case TYPENAME_TYPE: |
fccef71e | 12020 | case SCOPE_REF: |
b8c6534b | 12021 | case UNBOUND_CLASS_TEMPLATE: |
2ca340ae JM |
12022 | /* In a type which contains a nested-name-specifier, template |
12023 | argument values cannot be deduced for template parameters used | |
12024 | within the nested-name-specifier. */ | |
12025 | return 0; | |
12026 | ||
8d08fdba | 12027 | case TEMPLATE_TYPE_PARM: |
73b0fce8 | 12028 | case TEMPLATE_TEMPLATE_PARM: |
a1281f45 | 12029 | case BOUND_TEMPLATE_TEMPLATE_PARM: |
db2767b6 MM |
12030 | tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0)); |
12031 | ||
12032 | if (TEMPLATE_TYPE_LEVEL (parm) | |
12033 | != template_decl_level (tparm)) | |
12034 | /* The PARM is not one we're trying to unify. Just check | |
12035 | to see if it matches ARG. */ | |
12036 | return (TREE_CODE (arg) == TREE_CODE (parm) | |
3bfdc719 | 12037 | && same_type_p (parm, arg)) ? 0 : 1; |
73b0fce8 | 12038 | idx = TEMPLATE_TYPE_IDX (parm); |
916b63c3 | 12039 | targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx); |
db2767b6 | 12040 | tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx)); |
050367a3 | 12041 | |
73b0fce8 | 12042 | /* Check for mixed types and values. */ |
db2767b6 MM |
12043 | if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM |
12044 | && TREE_CODE (tparm) != TYPE_DECL) | |
c8094d83 | 12045 | || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM |
db2767b6 | 12046 | && TREE_CODE (tparm) != TEMPLATE_DECL)) |
73b0fce8 KL |
12047 | return 1; |
12048 | ||
a1281f45 | 12049 | if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM) |
73b0fce8 | 12050 | { |
b429fdf0 KL |
12051 | /* ARG must be constructed from a template class or a template |
12052 | template parameter. */ | |
12053 | if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM | |
4e95268d | 12054 | && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg)) |
a1281f45 | 12055 | return 1; |
73b0fce8 | 12056 | |
a1281f45 | 12057 | { |
a1281f45 | 12058 | tree parmvec = TYPE_TI_ARGS (parm); |
6df91b00 | 12059 | tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg)); |
a1281f45 | 12060 | tree argtmplvec |
b429fdf0 | 12061 | = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_TI_TEMPLATE (arg)); |
a1281f45 | 12062 | int i; |
73b0fce8 | 12063 | |
e7e93965 MM |
12064 | /* The resolution to DR150 makes clear that default |
12065 | arguments for an N-argument may not be used to bind T | |
12066 | to a template template parameter with fewer than N | |
12067 | parameters. It is not safe to permit the binding of | |
12068 | default arguments as an extension, as that may change | |
12069 | the meaning of a conforming program. Consider: | |
12070 | ||
12071 | struct Dense { static const unsigned int dim = 1; }; | |
12072 | ||
12073 | template <template <typename> class View, | |
12074 | typename Block> | |
12075 | void operator+(float, View<Block> const&); | |
12076 | ||
3db45ab5 MS |
12077 | template <typename Block, |
12078 | unsigned int Dim = Block::dim> | |
e7e93965 MM |
12079 | struct Lvalue_proxy { operator float() const; }; |
12080 | ||
12081 | void | |
12082 | test_1d (void) { | |
12083 | Lvalue_proxy<Dense> p; | |
12084 | float b; | |
12085 | b + p; | |
12086 | } | |
a1281f45 | 12087 | |
e7e93965 MM |
12088 | Here, if Lvalue_proxy is permitted to bind to View, then |
12089 | the global operator+ will be used; if they are not, the | |
3db45ab5 MS |
12090 | Lvalue_proxy will be converted to float. */ |
12091 | if (coerce_template_parms (argtmplvec, parmvec, | |
e7e93965 MM |
12092 | TYPE_TI_TEMPLATE (parm), |
12093 | tf_none, | |
12094 | /*require_all_args=*/true, | |
12095 | /*use_default_args=*/false) | |
0cbd7506 | 12096 | == error_mark_node) |
a1281f45 | 12097 | return 1; |
c8094d83 MS |
12098 | |
12099 | /* Deduce arguments T, i from TT<T> or TT<i>. | |
a1281f45 KL |
12100 | We check each element of PARMVEC and ARGVEC individually |
12101 | rather than the whole TREE_VEC since they can have | |
12102 | different number of elements. */ | |
6b9b6b15 | 12103 | |
a1281f45 KL |
12104 | for (i = 0; i < TREE_VEC_LENGTH (parmvec); ++i) |
12105 | { | |
0cbd7506 | 12106 | if (unify (tparms, targs, |
c8094d83 MS |
12107 | TREE_VEC_ELT (parmvec, i), |
12108 | TREE_VEC_ELT (argvec, i), | |
a1281f45 KL |
12109 | UNIFY_ALLOW_NONE)) |
12110 | return 1; | |
73b0fce8 | 12111 | } |
a1281f45 | 12112 | } |
b429fdf0 | 12113 | arg = TYPE_TI_TEMPLATE (arg); |
a1281f45 KL |
12114 | |
12115 | /* Fall through to deduce template name. */ | |
12116 | } | |
12117 | ||
12118 | if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM | |
12119 | || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM) | |
12120 | { | |
12121 | /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */ | |
12122 | ||
12123 | /* Simple cases: Value already set, does match or doesn't. */ | |
12124 | if (targ != NULL_TREE && template_args_equal (targ, arg)) | |
12125 | return 0; | |
12126 | else if (targ) | |
12127 | return 1; | |
db2767b6 MM |
12128 | } |
12129 | else | |
12130 | { | |
830bfa74 MM |
12131 | /* If PARM is `const T' and ARG is only `int', we don't have |
12132 | a match unless we are allowing additional qualification. | |
12133 | If ARG is `const int' and PARM is just `T' that's OK; | |
12134 | that binds `const int' to `T'. */ | |
c8094d83 | 12135 | if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL, |
830bfa74 | 12136 | arg, parm)) |
db2767b6 MM |
12137 | return 1; |
12138 | ||
830bfa74 MM |
12139 | /* Consider the case where ARG is `const volatile int' and |
12140 | PARM is `const T'. Then, T should be `volatile int'. */ | |
c2ea3a40 NS |
12141 | arg = cp_build_qualified_type_real |
12142 | (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none); | |
adecb3f4 MM |
12143 | if (arg == error_mark_node) |
12144 | return 1; | |
73b0fce8 | 12145 | |
a1281f45 KL |
12146 | /* Simple cases: Value already set, does match or doesn't. */ |
12147 | if (targ != NULL_TREE && same_type_p (targ, arg)) | |
12148 | return 0; | |
12149 | else if (targ) | |
12150 | return 1; | |
61cd552e | 12151 | |
94fc547c MM |
12152 | /* Make sure that ARG is not a variable-sized array. (Note |
12153 | that were talking about variable-sized arrays (like | |
12154 | `int[n]'), rather than arrays of unknown size (like | |
12155 | `int[]').) We'll get very confused by such a type since | |
12156 | the bound of the array will not be computable in an | |
12157 | instantiation. Besides, such types are not allowed in | |
12158 | ISO C++, so we can do as we please here. */ | |
5377d5ba | 12159 | if (variably_modified_type_p (arg, NULL_TREE)) |
94fc547c MM |
12160 | return 1; |
12161 | } | |
61cd552e | 12162 | |
5d80a306 DG |
12163 | /* If ARG is a parameter pack or an expansion, we cannot unify |
12164 | against it unless PARM is also a parameter pack. */ | |
12165 | if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg)) | |
12166 | && !template_parameter_pack_p (parm)) | |
12167 | return 1; | |
12168 | ||
916b63c3 | 12169 | TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg; |
73b0fce8 KL |
12170 | return 0; |
12171 | ||
f84b4be9 | 12172 | case TEMPLATE_PARM_INDEX: |
db2767b6 | 12173 | tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0)); |
3e9ac7e5 VR |
12174 | if (tparm == error_mark_node) |
12175 | return 1; | |
db2767b6 | 12176 | |
c8094d83 | 12177 | if (TEMPLATE_PARM_LEVEL (parm) |
db2767b6 MM |
12178 | != template_decl_level (tparm)) |
12179 | /* The PARM is not one we're trying to unify. Just check | |
12180 | to see if it matches ARG. */ | |
c8a209ca NS |
12181 | return !(TREE_CODE (arg) == TREE_CODE (parm) |
12182 | && cp_tree_equal (parm, arg)); | |
db2767b6 | 12183 | |
f84b4be9 | 12184 | idx = TEMPLATE_PARM_IDX (parm); |
916b63c3 | 12185 | targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx); |
db2767b6 | 12186 | |
050367a3 | 12187 | if (targ) |
c8a209ca | 12188 | return !cp_tree_equal (targ, arg); |
8d08fdba | 12189 | |
161c12b0 JM |
12190 | /* [temp.deduct.type] If, in the declaration of a function template |
12191 | with a non-type template-parameter, the non-type | |
12192 | template-parameter is used in an expression in the function | |
12193 | parameter-list and, if the corresponding template-argument is | |
12194 | deduced, the template-argument type shall match the type of the | |
12195 | template-parameter exactly, except that a template-argument | |
c8094d83 | 12196 | deduced from an array bound may be of any integral type. |
d7c4edd0 | 12197 | The non-type parameter might use already deduced type parameters. */ |
bd0d5d4a | 12198 | tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE); |
e2005c8d KL |
12199 | if (!TREE_TYPE (arg)) |
12200 | /* Template-parameter dependent expression. Just accept it for now. | |
12201 | It will later be processed in convert_template_argument. */ | |
12202 | ; | |
12203 | else if (same_type_p (TREE_TYPE (arg), tparm)) | |
12204 | /* OK */; | |
161c12b0 | 12205 | else if ((strict & UNIFY_ALLOW_INTEGER) |
bd0d5d4a JM |
12206 | && (TREE_CODE (tparm) == INTEGER_TYPE |
12207 | || TREE_CODE (tparm) == BOOLEAN_TYPE)) | |
8baddbf1 MM |
12208 | /* Convert the ARG to the type of PARM; the deduced non-type |
12209 | template argument must exactly match the types of the | |
12210 | corresponding parameter. */ | |
12211 | arg = fold (build_nop (TREE_TYPE (parm), arg)); | |
bd0d5d4a JM |
12212 | else if (uses_template_parms (tparm)) |
12213 | /* We haven't deduced the type of this parameter yet. Try again | |
12214 | later. */ | |
12215 | return 0; | |
161c12b0 JM |
12216 | else |
12217 | return 1; | |
12218 | ||
5d80a306 DG |
12219 | /* If ARG is a parameter pack or an expansion, we cannot unify |
12220 | against it unless PARM is also a parameter pack. */ | |
12221 | if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg)) | |
12222 | && !TEMPLATE_PARM_PARAMETER_PACK (parm)) | |
12223 | return 1; | |
12224 | ||
916b63c3 | 12225 | TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg; |
8d08fdba MS |
12226 | return 0; |
12227 | ||
28e8f3a0 GB |
12228 | case PTRMEM_CST: |
12229 | { | |
0cbd7506 MS |
12230 | /* A pointer-to-member constant can be unified only with |
12231 | another constant. */ | |
28e8f3a0 | 12232 | if (TREE_CODE (arg) != PTRMEM_CST) |
0cbd7506 | 12233 | return 1; |
28e8f3a0 GB |
12234 | |
12235 | /* Just unify the class member. It would be useless (and possibly | |
0cbd7506 MS |
12236 | wrong, depending on the strict flags) to unify also |
12237 | PTRMEM_CST_CLASS, because we want to be sure that both parm and | |
12238 | arg refer to the same variable, even if through different | |
12239 | classes. For instance: | |
28e8f3a0 | 12240 | |
0cbd7506 MS |
12241 | struct A { int x; }; |
12242 | struct B : A { }; | |
28e8f3a0 | 12243 | |
0cbd7506 | 12244 | Unification of &A::x and &B::x must succeed. */ |
28e8f3a0 | 12245 | return unify (tparms, targs, PTRMEM_CST_MEMBER (parm), |
0cbd7506 | 12246 | PTRMEM_CST_MEMBER (arg), strict); |
28e8f3a0 GB |
12247 | } |
12248 | ||
8d08fdba | 12249 | case POINTER_TYPE: |
830bfa74 | 12250 | { |
830bfa74 MM |
12251 | if (TREE_CODE (arg) != POINTER_TYPE) |
12252 | return 1; | |
c8094d83 | 12253 | |
830bfa74 MM |
12254 | /* [temp.deduct.call] |
12255 | ||
12256 | A can be another pointer or pointer to member type that can | |
12257 | be converted to the deduced A via a qualification | |
12258 | conversion (_conv.qual_). | |
12259 | ||
12260 | We pass down STRICT here rather than UNIFY_ALLOW_NONE. | |
12261 | This will allow for additional cv-qualification of the | |
028d1f20 | 12262 | pointed-to types if appropriate. */ |
c8094d83 | 12263 | |
028d1f20 | 12264 | if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE) |
830bfa74 MM |
12265 | /* The derived-to-base conversion only persists through one |
12266 | level of pointers. */ | |
028d1f20 | 12267 | strict |= (strict_in & UNIFY_ALLOW_DERIVED); |
830bfa74 | 12268 | |
c8094d83 | 12269 | return unify (tparms, targs, TREE_TYPE (parm), |
028d1f20 | 12270 | TREE_TYPE (arg), strict); |
830bfa74 | 12271 | } |
8d08fdba MS |
12272 | |
12273 | case REFERENCE_TYPE: | |
830bfa74 MM |
12274 | if (TREE_CODE (arg) != REFERENCE_TYPE) |
12275 | return 1; | |
12276 | return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg), | |
028d1f20 | 12277 | strict & UNIFY_ALLOW_MORE_CV_QUAL); |
8d08fdba MS |
12278 | |
12279 | case ARRAY_TYPE: | |
12280 | if (TREE_CODE (arg) != ARRAY_TYPE) | |
12281 | return 1; | |
3042d5be MM |
12282 | if ((TYPE_DOMAIN (parm) == NULL_TREE) |
12283 | != (TYPE_DOMAIN (arg) == NULL_TREE)) | |
12284 | return 1; | |
8baddbf1 MM |
12285 | if (TYPE_DOMAIN (parm) != NULL_TREE) |
12286 | { | |
12287 | tree parm_max; | |
12288 | tree arg_max; | |
6ee3ffe8 MM |
12289 | bool parm_cst; |
12290 | bool arg_cst; | |
8baddbf1 MM |
12291 | |
12292 | /* Our representation of array types uses "N - 1" as the | |
12293 | TYPE_MAX_VALUE for an array with "N" elements, if "N" is | |
6ee3ffe8 MM |
12294 | not an integer constant. We cannot unify arbitrarily |
12295 | complex expressions, so we eliminate the MINUS_EXPRs | |
12296 | here. */ | |
12297 | parm_max = TYPE_MAX_VALUE (TYPE_DOMAIN (parm)); | |
12298 | parm_cst = TREE_CODE (parm_max) == INTEGER_CST; | |
12299 | if (!parm_cst) | |
8baddbf1 | 12300 | { |
6ee3ffe8 | 12301 | gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR); |
8baddbf1 MM |
12302 | parm_max = TREE_OPERAND (parm_max, 0); |
12303 | } | |
6ee3ffe8 MM |
12304 | arg_max = TYPE_MAX_VALUE (TYPE_DOMAIN (arg)); |
12305 | arg_cst = TREE_CODE (arg_max) == INTEGER_CST; | |
12306 | if (!arg_cst) | |
12307 | { | |
12308 | /* The ARG_MAX may not be a simple MINUS_EXPR, if we are | |
12309 | trying to unify the type of a variable with the type | |
12310 | of a template parameter. For example: | |
12311 | ||
12312 | template <unsigned int N> | |
12313 | void f (char (&) [N]); | |
12314 | int g(); | |
12315 | void h(int i) { | |
12316 | char a[g(i)]; | |
12317 | f(a); | |
12318 | } | |
12319 | ||
12320 | Here, the type of the ARG will be "int [g(i)]", and | |
12321 | may be a SAVE_EXPR, etc. */ | |
12322 | if (TREE_CODE (arg_max) != MINUS_EXPR) | |
12323 | return 1; | |
12324 | arg_max = TREE_OPERAND (arg_max, 0); | |
12325 | } | |
12326 | ||
12327 | /* If only one of the bounds used a MINUS_EXPR, compensate | |
12328 | by adding one to the other bound. */ | |
12329 | if (parm_cst && !arg_cst) | |
12330 | parm_max = fold_build2 (PLUS_EXPR, | |
12331 | integer_type_node, | |
12332 | parm_max, | |
12333 | integer_one_node); | |
12334 | else if (arg_cst && !parm_cst) | |
12335 | arg_max = fold_build2 (PLUS_EXPR, | |
12336 | integer_type_node, | |
12337 | arg_max, | |
12338 | integer_one_node); | |
8baddbf1 MM |
12339 | |
12340 | if (unify (tparms, targs, parm_max, arg_max, UNIFY_ALLOW_INTEGER)) | |
12341 | return 1; | |
12342 | } | |
830bfa74 | 12343 | return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg), |
712467a4 | 12344 | strict & UNIFY_ALLOW_MORE_CV_QUAL); |
8d08fdba MS |
12345 | |
12346 | case REAL_TYPE: | |
37c46b43 | 12347 | case COMPLEX_TYPE: |
c00996a3 | 12348 | case VECTOR_TYPE: |
8d08fdba | 12349 | case INTEGER_TYPE: |
42976354 | 12350 | case BOOLEAN_TYPE: |
3590f0a6 | 12351 | case ENUMERAL_TYPE: |
5ad5a526 | 12352 | case VOID_TYPE: |
f376e137 MS |
12353 | if (TREE_CODE (arg) != TREE_CODE (parm)) |
12354 | return 1; | |
c8094d83 | 12355 | |
9edc3913 | 12356 | /* We have already checked cv-qualification at the top of the |
514a1f18 | 12357 | function. */ |
8baddbf1 | 12358 | if (!same_type_ignoring_top_level_qualifiers_p (arg, parm)) |
ca79f85d JM |
12359 | return 1; |
12360 | ||
8d08fdba MS |
12361 | /* As far as unification is concerned, this wins. Later checks |
12362 | will invalidate it if necessary. */ | |
12363 | return 0; | |
12364 | ||
12365 | /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */ | |
bd6dd845 | 12366 | /* Type INTEGER_CST can come from ordinary constant template args. */ |
8d08fdba | 12367 | case INTEGER_CST: |
bd6dd845 MS |
12368 | while (TREE_CODE (arg) == NOP_EXPR) |
12369 | arg = TREE_OPERAND (arg, 0); | |
12370 | ||
8d08fdba MS |
12371 | if (TREE_CODE (arg) != INTEGER_CST) |
12372 | return 1; | |
12373 | return !tree_int_cst_equal (parm, arg); | |
12374 | ||
8d08fdba MS |
12375 | case TREE_VEC: |
12376 | { | |
12377 | int i; | |
12378 | if (TREE_CODE (arg) != TREE_VEC) | |
12379 | return 1; | |
12380 | if (TREE_VEC_LENGTH (parm) != TREE_VEC_LENGTH (arg)) | |
12381 | return 1; | |
0dc09a61 | 12382 | for (i = 0; i < TREE_VEC_LENGTH (parm); ++i) |
830bfa74 | 12383 | if (unify (tparms, targs, |
8d08fdba | 12384 | TREE_VEC_ELT (parm, i), TREE_VEC_ELT (arg, i), |
4393e105 | 12385 | UNIFY_ALLOW_NONE)) |
8d08fdba MS |
12386 | return 1; |
12387 | return 0; | |
12388 | } | |
12389 | ||
8d08fdba | 12390 | case RECORD_TYPE: |
f181d4ae | 12391 | case UNION_TYPE: |
f181d4ae | 12392 | if (TREE_CODE (arg) != TREE_CODE (parm)) |
a4443a08 | 12393 | return 1; |
c8094d83 | 12394 | |
a7a64a77 MM |
12395 | if (TYPE_PTRMEMFUNC_P (parm)) |
12396 | { | |
12397 | if (!TYPE_PTRMEMFUNC_P (arg)) | |
12398 | return 1; | |
12399 | ||
c8094d83 | 12400 | return unify (tparms, targs, |
a7a64a77 MM |
12401 | TYPE_PTRMEMFUNC_FN_TYPE (parm), |
12402 | TYPE_PTRMEMFUNC_FN_TYPE (arg), | |
12403 | strict); | |
12404 | } | |
12405 | ||
5db698f6 | 12406 | if (CLASSTYPE_TEMPLATE_INFO (parm)) |
5566b478 | 12407 | { |
6467930b | 12408 | tree t = NULL_TREE; |
4393e105 | 12409 | |
028d1f20 | 12410 | if (strict_in & UNIFY_ALLOW_DERIVED) |
4393e105 MM |
12411 | { |
12412 | /* First, we try to unify the PARM and ARG directly. */ | |
12413 | t = try_class_unification (tparms, targs, | |
12414 | parm, arg); | |
12415 | ||
12416 | if (!t) | |
12417 | { | |
12418 | /* Fallback to the special case allowed in | |
12419 | [temp.deduct.call]: | |
c8094d83 | 12420 | |
4393e105 MM |
12421 | If P is a class, and P has the form |
12422 | template-id, then A can be a derived class of | |
12423 | the deduced A. Likewise, if P is a pointer to | |
12424 | a class of the form template-id, A can be a | |
12425 | pointer to a derived class pointed to by the | |
12426 | deduced A. */ | |
8d83768f | 12427 | t = get_template_base (tparms, targs, parm, arg); |
4393e105 | 12428 | |
8d83768f | 12429 | if (!t) |
4393e105 MM |
12430 | return 1; |
12431 | } | |
12432 | } | |
c8094d83 MS |
12433 | else if (CLASSTYPE_TEMPLATE_INFO (arg) |
12434 | && (CLASSTYPE_TI_TEMPLATE (parm) | |
9fbf56f7 | 12435 | == CLASSTYPE_TI_TEMPLATE (arg))) |
6df47b06 MM |
12436 | /* Perhaps PARM is something like S<U> and ARG is S<int>. |
12437 | Then, we should unify `int' and `U'. */ | |
6467930b | 12438 | t = arg; |
4393e105 | 12439 | else |
dc957d14 | 12440 | /* There's no chance of unification succeeding. */ |
5566b478 | 12441 | return 1; |
6467930b | 12442 | |
830bfa74 | 12443 | return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm), |
4393e105 | 12444 | CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE); |
5566b478 | 12445 | } |
9edc3913 | 12446 | else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg)) |
5566b478 | 12447 | return 1; |
a4443a08 | 12448 | return 0; |
8d08fdba MS |
12449 | |
12450 | case METHOD_TYPE: | |
8d08fdba | 12451 | case FUNCTION_TYPE: |
830bfa74 | 12452 | if (TREE_CODE (arg) != TREE_CODE (parm)) |
8d08fdba | 12453 | return 1; |
830bfa74 | 12454 | |
38d18b1a | 12455 | /* CV qualifications for methods can never be deduced, they must |
3db45ab5 MS |
12456 | match exactly. We need to check them explicitly here, |
12457 | because type_unification_real treats them as any other | |
12458 | cvqualified parameter. */ | |
38d18b1a NS |
12459 | if (TREE_CODE (parm) == METHOD_TYPE |
12460 | && (!check_cv_quals_for_unify | |
12461 | (UNIFY_ALLOW_NONE, | |
12462 | TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (arg))), | |
12463 | TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (parm)))))) | |
12464 | return 1; | |
12465 | ||
830bfa74 | 12466 | if (unify (tparms, targs, TREE_TYPE (parm), |
4393e105 | 12467 | TREE_TYPE (arg), UNIFY_ALLOW_NONE)) |
28cbf42c | 12468 | return 1; |
386b8a85 | 12469 | return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm), |
30f86ec3 FJ |
12470 | TYPE_ARG_TYPES (arg), 1, DEDUCE_EXACT, |
12471 | LOOKUP_NORMAL); | |
a4443a08 MS |
12472 | |
12473 | case OFFSET_TYPE: | |
9804209d | 12474 | /* Unify a pointer to member with a pointer to member function, which |
0cbd7506 | 12475 | deduces the type of the member as a function type. */ |
9804209d | 12476 | if (TYPE_PTRMEMFUNC_P (arg)) |
0cbd7506 MS |
12477 | { |
12478 | tree method_type; | |
12479 | tree fntype; | |
12480 | cp_cv_quals cv_quals; | |
12481 | ||
12482 | /* Check top-level cv qualifiers */ | |
12483 | if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm)) | |
12484 | return 1; | |
12485 | ||
12486 | if (unify (tparms, targs, TYPE_OFFSET_BASETYPE (parm), | |
12487 | TYPE_PTRMEMFUNC_OBJECT_TYPE (arg), UNIFY_ALLOW_NONE)) | |
12488 | return 1; | |
12489 | ||
12490 | /* Determine the type of the function we are unifying against. */ | |
12491 | method_type = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (arg)); | |
12492 | fntype = | |
12493 | build_function_type (TREE_TYPE (method_type), | |
12494 | TREE_CHAIN (TYPE_ARG_TYPES (method_type))); | |
12495 | ||
12496 | /* Extract the cv-qualifiers of the member function from the | |
12497 | implicit object parameter and place them on the function | |
12498 | type to be restored later. */ | |
12499 | cv_quals = | |
12500 | cp_type_quals(TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (method_type)))); | |
12501 | fntype = build_qualified_type (fntype, cv_quals); | |
12502 | return unify (tparms, targs, TREE_TYPE (parm), fntype, strict); | |
12503 | } | |
9804209d | 12504 | |
a4443a08 MS |
12505 | if (TREE_CODE (arg) != OFFSET_TYPE) |
12506 | return 1; | |
830bfa74 | 12507 | if (unify (tparms, targs, TYPE_OFFSET_BASETYPE (parm), |
4393e105 | 12508 | TYPE_OFFSET_BASETYPE (arg), UNIFY_ALLOW_NONE)) |
a4443a08 | 12509 | return 1; |
830bfa74 | 12510 | return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg), |
4393e105 | 12511 | strict); |
a4443a08 | 12512 | |
f62dbf03 | 12513 | case CONST_DECL: |
a723baf1 MM |
12514 | if (DECL_TEMPLATE_PARM_P (parm)) |
12515 | return unify (tparms, targs, DECL_INITIAL (parm), arg, strict); | |
c8094d83 | 12516 | if (arg != integral_constant_value (parm)) |
f62dbf03 JM |
12517 | return 1; |
12518 | return 0; | |
12519 | ||
28e8f3a0 | 12520 | case FIELD_DECL: |
027905b4 KL |
12521 | case TEMPLATE_DECL: |
12522 | /* Matched cases are handled by the ARG == PARM test above. */ | |
12523 | return 1; | |
12524 | ||
5d80a306 DG |
12525 | case TYPE_ARGUMENT_PACK: |
12526 | case NONTYPE_ARGUMENT_PACK: | |
12527 | { | |
12528 | tree packed_parms = ARGUMENT_PACK_ARGS (parm); | |
12529 | tree packed_args = ARGUMENT_PACK_ARGS (arg); | |
12530 | int i, len = TREE_VEC_LENGTH (packed_parms); | |
12531 | int argslen = TREE_VEC_LENGTH (packed_args); | |
12532 | int parm_variadic_p = 0; | |
12533 | ||
12534 | /* Check if the parameters end in a pack, making them variadic. */ | |
12535 | if (len > 0 | |
12536 | && PACK_EXPANSION_P (TREE_VEC_ELT (packed_parms, len - 1))) | |
12537 | parm_variadic_p = 1; | |
12538 | ||
12539 | /* If we don't have enough arguments to satisfy the parameters | |
12540 | (not counting the pack expression at the end), or we have | |
12541 | too many arguments for a parameter list that doesn't end in | |
12542 | a pack expression, we can't unify. */ | |
12543 | if (argslen < (len - parm_variadic_p) | |
12544 | || (argslen > len && !parm_variadic_p)) | |
12545 | return 1; | |
12546 | ||
12547 | /* Unify all of the parameters that precede the (optional) | |
12548 | pack expression. */ | |
12549 | for (i = 0; i < len - parm_variadic_p; ++i) | |
12550 | { | |
12551 | if (unify (tparms, targs, TREE_VEC_ELT (packed_parms, i), | |
12552 | TREE_VEC_ELT (packed_args, i), strict)) | |
12553 | return 1; | |
12554 | } | |
12555 | ||
12556 | if (parm_variadic_p) | |
12557 | return unify_pack_expansion (tparms, targs, | |
12558 | packed_parms, packed_args, | |
12559 | strict, /*call_args_p=*/false, | |
12560 | /*subr=*/false); | |
12561 | return 0; | |
12562 | } | |
12563 | ||
12564 | break; | |
12565 | ||
8d08fdba | 12566 | default: |
6615c446 | 12567 | gcc_assert (EXPR_P (parm)); |
c8094d83 | 12568 | |
98ddffc1 | 12569 | /* We must be looking at an expression. This can happen with |
c8094d83 MS |
12570 | something like: |
12571 | ||
98ddffc1 NS |
12572 | template <int I> |
12573 | void foo(S<I>, S<I + 2>); | |
050367a3 | 12574 | |
98ddffc1 | 12575 | This is a "nondeduced context": |
050367a3 | 12576 | |
98ddffc1 | 12577 | [deduct.type] |
c8094d83 | 12578 | |
98ddffc1 | 12579 | The nondeduced contexts are: |
050367a3 | 12580 | |
98ddffc1 NS |
12581 | --A type that is a template-id in which one or more of |
12582 | the template-arguments is an expression that references | |
c8094d83 | 12583 | a template-parameter. |
050367a3 | 12584 | |
98ddffc1 NS |
12585 | In these cases, we assume deduction succeeded, but don't |
12586 | actually infer any unifications. */ | |
74601d7c | 12587 | |
98ddffc1 NS |
12588 | if (!uses_template_parms (parm) |
12589 | && !template_args_equal (parm, arg)) | |
12590 | return 1; | |
12591 | else | |
12592 | return 0; | |
8d08fdba MS |
12593 | } |
12594 | } | |
8d08fdba | 12595 | \f |
4684cd27 MM |
12596 | /* Note that DECL can be defined in this translation unit, if |
12597 | required. */ | |
12598 | ||
12599 | static void | |
12600 | mark_definable (tree decl) | |
12601 | { | |
12602 | tree clone; | |
12603 | DECL_NOT_REALLY_EXTERN (decl) = 1; | |
12604 | FOR_EACH_CLONE (clone, decl) | |
12605 | DECL_NOT_REALLY_EXTERN (clone) = 1; | |
12606 | } | |
12607 | ||
03d0f4af | 12608 | /* Called if RESULT is explicitly instantiated, or is a member of an |
4684cd27 | 12609 | explicitly instantiated class. */ |
03d0f4af | 12610 | |
faae18ab | 12611 | void |
3a978d72 | 12612 | mark_decl_instantiated (tree result, int extern_p) |
faae18ab | 12613 | { |
415c974c | 12614 | SET_DECL_EXPLICIT_INSTANTIATION (result); |
3ae18eaf | 12615 | |
1f6f0cb6 MM |
12616 | /* If this entity has already been written out, it's too late to |
12617 | make any modifications. */ | |
12618 | if (TREE_ASM_WRITTEN (result)) | |
12619 | return; | |
12620 | ||
12621 | if (TREE_CODE (result) != FUNCTION_DECL) | |
12622 | /* The TREE_PUBLIC flag for function declarations will have been | |
12623 | set correctly by tsubst. */ | |
12624 | TREE_PUBLIC (result) = 1; | |
12625 | ||
346eeb15 JM |
12626 | /* This might have been set by an earlier implicit instantiation. */ |
12627 | DECL_COMDAT (result) = 0; | |
12628 | ||
4684cd27 MM |
12629 | if (extern_p) |
12630 | DECL_NOT_REALLY_EXTERN (result) = 0; | |
12631 | else | |
faae18ab | 12632 | { |
4684cd27 | 12633 | mark_definable (result); |
1a408d07 JM |
12634 | /* Always make artificials weak. */ |
12635 | if (DECL_ARTIFICIAL (result) && flag_weak) | |
12636 | comdat_linkage (result); | |
a7d87521 JM |
12637 | /* For WIN32 we also want to put explicit instantiations in |
12638 | linkonce sections. */ | |
1a408d07 | 12639 | else if (TREE_PUBLIC (result)) |
b385c841 | 12640 | maybe_make_one_only (result); |
faae18ab | 12641 | } |
c8094d83 | 12642 | |
4684cd27 MM |
12643 | /* If EXTERN_P, then this function will not be emitted -- unless |
12644 | followed by an explicit instantiation, at which point its linkage | |
12645 | will be adjusted. If !EXTERN_P, then this function will be | |
12646 | emitted here. In neither circumstance do we want | |
12647 | import_export_decl to adjust the linkage. */ | |
c8094d83 | 12648 | DECL_INTERFACE_KNOWN (result) = 1; |
faae18ab MS |
12649 | } |
12650 | ||
e5214479 | 12651 | /* Given two function templates PAT1 and PAT2, return: |
6467930b MS |
12652 | |
12653 | 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order]. | |
12654 | -1 if PAT2 is more specialized than PAT1. | |
e5214479 JM |
12655 | 0 if neither is more specialized. |
12656 | ||
dda04398 NS |
12657 | LEN indicates the number of parameters we should consider |
12658 | (defaulted parameters should not be considered). | |
12659 | ||
12660 | The 1998 std underspecified function template partial ordering, and | |
12661 | DR214 addresses the issue. We take pairs of arguments, one from | |
c51940a2 | 12662 | each of the templates, and deduce them against each other. One of |
dda04398 NS |
12663 | the templates will be more specialized if all the *other* |
12664 | template's arguments deduce against its arguments and at least one | |
12665 | of its arguments *does* *not* deduce against the other template's | |
12666 | corresponding argument. Deduction is done as for class templates. | |
12667 | The arguments used in deduction have reference and top level cv | |
12668 | qualifiers removed. Iff both arguments were originally reference | |
12669 | types *and* deduction succeeds in both directions, the template | |
12670 | with the more cv-qualified argument wins for that pairing (if | |
12671 | neither is more cv-qualified, they both are equal). Unlike regular | |
12672 | deduction, after all the arguments have been deduced in this way, | |
12673 | we do *not* verify the deduced template argument values can be | |
12674 | substituted into non-deduced contexts, nor do we have to verify | |
12675 | that all template arguments have been deduced. */ | |
c8094d83 | 12676 | |
6467930b | 12677 | int |
dda04398 NS |
12678 | more_specialized_fn (tree pat1, tree pat2, int len) |
12679 | { | |
12680 | tree decl1 = DECL_TEMPLATE_RESULT (pat1); | |
12681 | tree decl2 = DECL_TEMPLATE_RESULT (pat2); | |
12682 | tree targs1 = make_tree_vec (DECL_NTPARMS (pat1)); | |
12683 | tree targs2 = make_tree_vec (DECL_NTPARMS (pat2)); | |
12684 | tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1); | |
12685 | tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2); | |
12686 | tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1)); | |
12687 | tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2)); | |
12688 | int better1 = 0; | |
12689 | int better2 = 0; | |
3db45ab5 | 12690 | |
48884537 NS |
12691 | /* Remove the this parameter from non-static member functions. If |
12692 | one is a non-static member function and the other is not a static | |
12693 | member function, remove the first parameter from that function | |
12694 | also. This situation occurs for operator functions where we | |
12695 | locate both a member function (with this pointer) and non-member | |
12696 | operator (with explicit first operand). */ | |
dda04398 | 12697 | if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1)) |
48884537 NS |
12698 | { |
12699 | len--; /* LEN is the number of significant arguments for DECL1 */ | |
12700 | args1 = TREE_CHAIN (args1); | |
12701 | if (!DECL_STATIC_FUNCTION_P (decl2)) | |
12702 | args2 = TREE_CHAIN (args2); | |
12703 | } | |
12704 | else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2)) | |
12705 | { | |
12706 | args2 = TREE_CHAIN (args2); | |
12707 | if (!DECL_STATIC_FUNCTION_P (decl1)) | |
12708 | { | |
12709 | len--; | |
12710 | args1 = TREE_CHAIN (args1); | |
12711 | } | |
12712 | } | |
3db45ab5 | 12713 | |
ee307009 NS |
12714 | /* If only one is a conversion operator, they are unordered. */ |
12715 | if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2)) | |
12716 | return 0; | |
c8094d83 | 12717 | |
dda04398 NS |
12718 | /* Consider the return type for a conversion function */ |
12719 | if (DECL_CONV_FN_P (decl1)) | |
12720 | { | |
dda04398 NS |
12721 | args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1); |
12722 | args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2); | |
12723 | len++; | |
12724 | } | |
c8094d83 | 12725 | |
dda04398 | 12726 | processing_template_decl++; |
c8094d83 | 12727 | |
dda04398 NS |
12728 | while (len--) |
12729 | { | |
12730 | tree arg1 = TREE_VALUE (args1); | |
12731 | tree arg2 = TREE_VALUE (args2); | |
12732 | int deduce1, deduce2; | |
12733 | int quals1 = -1; | |
12734 | int quals2 = -1; | |
6467930b | 12735 | |
5d80a306 DG |
12736 | if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION |
12737 | && TREE_CODE (arg2) == TYPE_PACK_EXPANSION) | |
12738 | { | |
12739 | /* When both arguments are pack expansions, we need only | |
12740 | unify the patterns themselves. */ | |
12741 | arg1 = PACK_EXPANSION_PATTERN (arg1); | |
12742 | arg2 = PACK_EXPANSION_PATTERN (arg2); | |
12743 | ||
12744 | /* This is the last comparison we need to do. */ | |
12745 | len = 0; | |
12746 | } | |
12747 | ||
dda04398 NS |
12748 | if (TREE_CODE (arg1) == REFERENCE_TYPE) |
12749 | { | |
12750 | arg1 = TREE_TYPE (arg1); | |
12751 | quals1 = cp_type_quals (arg1); | |
12752 | } | |
c8094d83 | 12753 | |
dda04398 NS |
12754 | if (TREE_CODE (arg2) == REFERENCE_TYPE) |
12755 | { | |
12756 | arg2 = TREE_TYPE (arg2); | |
12757 | quals2 = cp_type_quals (arg2); | |
12758 | } | |
6467930b | 12759 | |
dda04398 NS |
12760 | if ((quals1 < 0) != (quals2 < 0)) |
12761 | { | |
12762 | /* Only of the args is a reference, see if we should apply | |
12763 | array/function pointer decay to it. This is not part of | |
12764 | DR214, but is, IMHO, consistent with the deduction rules | |
12765 | for the function call itself, and with our earlier | |
12766 | implementation of the underspecified partial ordering | |
12767 | rules. (nathan). */ | |
12768 | if (quals1 >= 0) | |
12769 | { | |
12770 | switch (TREE_CODE (arg1)) | |
12771 | { | |
12772 | case ARRAY_TYPE: | |
12773 | arg1 = TREE_TYPE (arg1); | |
12774 | /* FALLTHROUGH. */ | |
12775 | case FUNCTION_TYPE: | |
12776 | arg1 = build_pointer_type (arg1); | |
12777 | break; | |
c8094d83 | 12778 | |
dda04398 NS |
12779 | default: |
12780 | break; | |
12781 | } | |
12782 | } | |
12783 | else | |
12784 | { | |
12785 | switch (TREE_CODE (arg2)) | |
12786 | { | |
12787 | case ARRAY_TYPE: | |
12788 | arg2 = TREE_TYPE (arg2); | |
12789 | /* FALLTHROUGH. */ | |
12790 | case FUNCTION_TYPE: | |
12791 | arg2 = build_pointer_type (arg2); | |
12792 | break; | |
c8094d83 | 12793 | |
dda04398 NS |
12794 | default: |
12795 | break; | |
12796 | } | |
12797 | } | |
12798 | } | |
c8094d83 | 12799 | |
dda04398 NS |
12800 | arg1 = TYPE_MAIN_VARIANT (arg1); |
12801 | arg2 = TYPE_MAIN_VARIANT (arg2); | |
c8094d83 | 12802 | |
5d80a306 DG |
12803 | if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION) |
12804 | { | |
12805 | int i, len2 = len + 1; | |
12806 | tree parmvec = make_tree_vec (1); | |
12807 | tree argvec = make_tree_vec (len2); | |
12808 | tree ta = args2; | |
12809 | ||
12810 | /* Setup the parameter vector, which contains only ARG1. */ | |
12811 | TREE_VEC_ELT (parmvec, 0) = arg1; | |
12812 | ||
12813 | /* Setup the argument vector, which contains the remaining | |
12814 | arguments. */ | |
12815 | for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta)) | |
12816 | TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta); | |
12817 | ||
12818 | deduce1 = !unify_pack_expansion (tparms1, targs1, parmvec, | |
12819 | argvec, UNIFY_ALLOW_NONE, | |
12820 | /*call_args_p=*/false, | |
12821 | /*subr=*/0); | |
12822 | ||
12823 | /* We cannot deduce in the other direction, because ARG1 is | |
12824 | a pack expansion but ARG2 is not. */ | |
12825 | deduce2 = 0; | |
12826 | } | |
12827 | else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION) | |
12828 | { | |
12829 | int i, len1 = len + 1; | |
12830 | tree parmvec = make_tree_vec (1); | |
12831 | tree argvec = make_tree_vec (len1); | |
12832 | tree ta = args1; | |
12833 | ||
12834 | /* Setup the parameter vector, which contains only ARG1. */ | |
12835 | TREE_VEC_ELT (parmvec, 0) = arg2; | |
12836 | ||
12837 | /* Setup the argument vector, which contains the remaining | |
12838 | arguments. */ | |
12839 | for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta)) | |
12840 | TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta); | |
12841 | ||
12842 | deduce2 = !unify_pack_expansion (tparms2, targs2, parmvec, | |
12843 | argvec, UNIFY_ALLOW_NONE, | |
12844 | /*call_args_p=*/false, | |
12845 | /*subr=*/0); | |
12846 | ||
12847 | /* We cannot deduce in the other direction, because ARG2 is | |
12848 | a pack expansion but ARG1 is not.*/ | |
12849 | deduce1 = 0; | |
12850 | } | |
12851 | ||
12852 | else | |
12853 | { | |
12854 | /* The normal case, where neither argument is a pack | |
12855 | expansion. */ | |
12856 | deduce1 = !unify (tparms1, targs1, arg1, arg2, UNIFY_ALLOW_NONE); | |
12857 | deduce2 = !unify (tparms2, targs2, arg2, arg1, UNIFY_ALLOW_NONE); | |
12858 | } | |
dda04398 NS |
12859 | |
12860 | if (!deduce1) | |
12861 | better2 = -1; | |
12862 | if (!deduce2) | |
12863 | better1 = -1; | |
12864 | if (better1 < 0 && better2 < 0) | |
12865 | /* We've failed to deduce something in either direction. | |
12866 | These must be unordered. */ | |
12867 | break; | |
c8094d83 | 12868 | |
dda04398 NS |
12869 | if (deduce1 && deduce2 && quals1 >= 0 && quals2 >= 0) |
12870 | { | |
12871 | /* Deduces in both directions, see if quals can | |
12872 | disambiguate. Pretend the worse one failed to deduce. */ | |
12873 | if ((quals1 & quals2) == quals2) | |
12874 | deduce1 = 0; | |
12875 | if ((quals1 & quals2) == quals1) | |
12876 | deduce2 = 0; | |
12877 | } | |
12878 | if (deduce1 && !deduce2 && !better2) | |
12879 | better2 = 1; | |
12880 | if (deduce2 && !deduce1 && !better1) | |
12881 | better1 = 1; | |
c8094d83 | 12882 | |
5d80a306 DG |
12883 | if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION |
12884 | || TREE_CODE (arg2) == TYPE_PACK_EXPANSION) | |
12885 | /* We have already processed all of the arguments in our | |
12886 | handing of the pack expansion type. */ | |
12887 | len = 0; | |
12888 | ||
dda04398 NS |
12889 | args1 = TREE_CHAIN (args1); |
12890 | args2 = TREE_CHAIN (args2); | |
12891 | } | |
12892 | ||
12893 | processing_template_decl--; | |
12894 | ||
5d80a306 DG |
12895 | /* All things being equal, if the next argument is a pack expansion |
12896 | for one function but not for the other, prefer the | |
12897 | non-variadic function. */ | |
12898 | if ((better1 > 0) - (better2 > 0) == 0 | |
12899 | && args1 && TREE_VALUE (args1) | |
12900 | && args2 && TREE_VALUE (args2)) | |
12901 | { | |
12902 | if (TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION) | |
12903 | return TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION ? 0 : -1; | |
12904 | else if (TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION) | |
12905 | return 1; | |
12906 | } | |
12907 | ||
dda04398 | 12908 | return (better1 > 0) - (better2 > 0); |
73aad9b9 | 12909 | } |
6467930b | 12910 | |
916b63c3 | 12911 | /* Determine which of two partial specializations is more specialized. |
6467930b | 12912 | |
916b63c3 MM |
12913 | PAT1 is a TREE_LIST whose TREE_TYPE is the _TYPE node corresponding |
12914 | to the first partial specialization. The TREE_VALUE is the | |
12915 | innermost set of template parameters for the partial | |
12916 | specialization. PAT2 is similar, but for the second template. | |
12917 | ||
12918 | Return 1 if the first partial specialization is more specialized; | |
12919 | -1 if the second is more specialized; 0 if neither is more | |
12920 | specialized. | |
55ece1b3 | 12921 | |
916b63c3 | 12922 | See [temp.class.order] for information about determining which of |
3db45ab5 | 12923 | two templates is more specialized. */ |
c8094d83 | 12924 | |
b5791fdc | 12925 | static int |
916b63c3 | 12926 | more_specialized_class (tree pat1, tree pat2) |
73aad9b9 JM |
12927 | { |
12928 | tree targs; | |
916b63c3 | 12929 | tree tmpl1, tmpl2; |
73aad9b9 | 12930 | int winner = 0; |
5d80a306 | 12931 | bool any_deductions = false; |
73aad9b9 | 12932 | |
916b63c3 MM |
12933 | tmpl1 = TREE_TYPE (pat1); |
12934 | tmpl2 = TREE_TYPE (pat2); | |
12935 | ||
c8094d83 | 12936 | /* Just like what happens for functions, if we are ordering between |
baa49730 GB |
12937 | different class template specializations, we may encounter dependent |
12938 | types in the arguments, and we need our dependency check functions | |
12939 | to behave correctly. */ | |
12940 | ++processing_template_decl; | |
3db45ab5 | 12941 | targs = get_class_bindings (TREE_VALUE (pat1), |
916b63c3 MM |
12942 | CLASSTYPE_TI_ARGS (tmpl1), |
12943 | CLASSTYPE_TI_ARGS (tmpl2)); | |
73aad9b9 | 12944 | if (targs) |
5d80a306 DG |
12945 | { |
12946 | --winner; | |
12947 | any_deductions = true; | |
12948 | } | |
73aad9b9 | 12949 | |
3db45ab5 | 12950 | targs = get_class_bindings (TREE_VALUE (pat2), |
916b63c3 MM |
12951 | CLASSTYPE_TI_ARGS (tmpl2), |
12952 | CLASSTYPE_TI_ARGS (tmpl1)); | |
73aad9b9 | 12953 | if (targs) |
5d80a306 DG |
12954 | { |
12955 | ++winner; | |
12956 | any_deductions = true; | |
12957 | } | |
baa49730 | 12958 | --processing_template_decl; |
6467930b | 12959 | |
5d80a306 DG |
12960 | /* In the case of a tie where at least one of the class templates |
12961 | has a parameter pack at the end, the template with the most | |
12962 | non-packed parameters wins. */ | |
12963 | if (winner == 0 | |
12964 | && any_deductions | |
12965 | && (template_args_variadic_p (TREE_PURPOSE (pat1)) | |
12966 | || template_args_variadic_p (TREE_PURPOSE (pat2)))) | |
12967 | { | |
12968 | tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1)); | |
12969 | tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2)); | |
12970 | int len1 = TREE_VEC_LENGTH (args1); | |
12971 | int len2 = TREE_VEC_LENGTH (args2); | |
12972 | ||
12973 | /* We don't count the pack expansion at the end. */ | |
12974 | if (template_args_variadic_p (TREE_PURPOSE (pat1))) | |
12975 | --len1; | |
12976 | if (template_args_variadic_p (TREE_PURPOSE (pat2))) | |
12977 | --len2; | |
12978 | ||
12979 | if (len1 > len2) | |
12980 | return 1; | |
12981 | else if (len1 < len2) | |
12982 | return -1; | |
12983 | } | |
12984 | ||
6467930b MS |
12985 | return winner; |
12986 | } | |
73aad9b9 JM |
12987 | |
12988 | /* Return the template arguments that will produce the function signature | |
e1467ff2 | 12989 | DECL from the function template FN, with the explicit template |
a34d3336 | 12990 | arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must |
4393e105 | 12991 | also match. Return NULL_TREE if no satisfactory arguments could be |
a34d3336 | 12992 | found. */ |
c8094d83 | 12993 | |
76b9a14d | 12994 | static tree |
a34d3336 | 12995 | get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype) |
73aad9b9 | 12996 | { |
98c1c668 | 12997 | int ntparms = DECL_NTPARMS (fn); |
f31c0a32 | 12998 | tree targs = make_tree_vec (ntparms); |
4393e105 | 12999 | tree decl_type; |
03017874 | 13000 | tree decl_arg_types; |
98c1c668 | 13001 | |
4393e105 MM |
13002 | /* Substitute the explicit template arguments into the type of DECL. |
13003 | The call to fn_type_unification will handle substitution into the | |
13004 | FN. */ | |
13005 | decl_type = TREE_TYPE (decl); | |
13006 | if (explicit_args && uses_template_parms (decl_type)) | |
13007 | { | |
13008 | tree tmpl; | |
13009 | tree converted_args; | |
13010 | ||
13011 | if (DECL_TEMPLATE_INFO (decl)) | |
13012 | tmpl = DECL_TI_TEMPLATE (decl); | |
13013 | else | |
0e339752 | 13014 | /* We can get here for some invalid specializations. */ |
4393e105 MM |
13015 | return NULL_TREE; |
13016 | ||
13017 | converted_args | |
e7e93965 MM |
13018 | = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl), |
13019 | explicit_args, NULL_TREE, | |
3db45ab5 | 13020 | tf_none, |
e7e93965 MM |
13021 | /*require_all_args=*/false, |
13022 | /*use_default_args=*/false); | |
4393e105 MM |
13023 | if (converted_args == error_mark_node) |
13024 | return NULL_TREE; | |
c8094d83 MS |
13025 | |
13026 | decl_type = tsubst (decl_type, converted_args, tf_none, NULL_TREE); | |
4393e105 MM |
13027 | if (decl_type == error_mark_node) |
13028 | return NULL_TREE; | |
13029 | } | |
13030 | ||
e5214479 | 13031 | /* Never do unification on the 'this' parameter. */ |
ba139ba8 MM |
13032 | decl_arg_types = skip_artificial_parms_for (decl, |
13033 | TYPE_ARG_TYPES (decl_type)); | |
d7684f2d | 13034 | |
c8094d83 | 13035 | if (fn_type_unification (fn, explicit_args, targs, |
03017874 | 13036 | decl_arg_types, |
8d3631f8 | 13037 | (check_rettype || DECL_CONV_FN_P (fn) |
0cbd7506 | 13038 | ? TREE_TYPE (decl_type) : NULL_TREE), |
30f86ec3 | 13039 | DEDUCE_EXACT, LOOKUP_NORMAL)) |
76b9a14d JM |
13040 | return NULL_TREE; |
13041 | ||
76b9a14d JM |
13042 | return targs; |
13043 | } | |
13044 | ||
36a117a5 MM |
13045 | /* Return the innermost template arguments that, when applied to a |
13046 | template specialization whose innermost template parameters are | |
9471d3e2 | 13047 | TPARMS, and whose specialization arguments are PARMS, yield the |
c8094d83 | 13048 | ARGS. |
36a117a5 MM |
13049 | |
13050 | For example, suppose we have: | |
13051 | ||
13052 | template <class T, class U> struct S {}; | |
13053 | template <class T> struct S<T*, int> {}; | |
13054 | ||
13055 | Then, suppose we want to get `S<double*, int>'. The TPARMS will be | |
916b63c3 | 13056 | {T}, the SPEC_ARGS will be {T*, int} and the ARGS will be {double*, |
36a117a5 MM |
13057 | int}. The resulting vector will be {double}, indicating that `T' |
13058 | is bound to `double'. */ | |
13059 | ||
bd6dd845 | 13060 | static tree |
916b63c3 | 13061 | get_class_bindings (tree tparms, tree spec_args, tree args) |
73aad9b9 | 13062 | { |
3b3ba9f0 | 13063 | int i, ntparms = TREE_VEC_LENGTH (tparms); |
916b63c3 MM |
13064 | tree deduced_args; |
13065 | tree innermost_deduced_args; | |
73aad9b9 | 13066 | |
916b63c3 MM |
13067 | innermost_deduced_args = make_tree_vec (ntparms); |
13068 | if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args)) | |
13069 | { | |
13070 | deduced_args = copy_node (args); | |
13071 | SET_TMPL_ARGS_LEVEL (deduced_args, | |
13072 | TMPL_ARGS_DEPTH (deduced_args), | |
13073 | innermost_deduced_args); | |
13074 | } | |
13075 | else | |
3db45ab5 | 13076 | deduced_args = innermost_deduced_args; |
916b63c3 | 13077 | |
3db45ab5 MS |
13078 | if (unify (tparms, deduced_args, |
13079 | INNERMOST_TEMPLATE_ARGS (spec_args), | |
13080 | INNERMOST_TEMPLATE_ARGS (args), | |
0cbd7506 | 13081 | UNIFY_ALLOW_NONE)) |
fcfb9f96 | 13082 | return NULL_TREE; |
73aad9b9 JM |
13083 | |
13084 | for (i = 0; i < ntparms; ++i) | |
916b63c3 | 13085 | if (! TREE_VEC_ELT (innermost_deduced_args, i)) |
73aad9b9 JM |
13086 | return NULL_TREE; |
13087 | ||
916b63c3 MM |
13088 | /* Verify that nondeduced template arguments agree with the type |
13089 | obtained from argument deduction. | |
3db45ab5 | 13090 | |
916b63c3 MM |
13091 | For example: |
13092 | ||
13093 | struct A { typedef int X; }; | |
13094 | template <class T, class U> struct C {}; | |
13095 | template <class T> struct C<T, typename T::X> {}; | |
13096 | ||
13097 | Then with the instantiation `C<A, int>', we can deduce that | |
13098 | `T' is `A' but unify () does not check whether `typename T::X' | |
13099 | is `int'. */ | |
13100 | spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE); | |
13101 | if (spec_args == error_mark_node | |
13102 | /* We only need to check the innermost arguments; the other | |
13103 | arguments will always agree. */ | |
13104 | || !comp_template_args (INNERMOST_TEMPLATE_ARGS (spec_args), | |
13105 | INNERMOST_TEMPLATE_ARGS (args))) | |
74601d7c KL |
13106 | return NULL_TREE; |
13107 | ||
916b63c3 | 13108 | return deduced_args; |
73aad9b9 JM |
13109 | } |
13110 | ||
7ca383e6 MM |
13111 | /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL. |
13112 | Return the TREE_LIST node with the most specialized template, if | |
13113 | any. If there is no most specialized template, the error_mark_node | |
13114 | is returned. | |
13115 | ||
13116 | Note that this function does not look at, or modify, the | |
13117 | TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node | |
13118 | returned is one of the elements of INSTANTIATIONS, callers may | |
13119 | store information in the TREE_PURPOSE or TREE_TYPE of the nodes, | |
13120 | and retrieve it from the value returned. */ | |
73aad9b9 JM |
13121 | |
13122 | tree | |
7ca383e6 | 13123 | most_specialized_instantiation (tree templates) |
73aad9b9 | 13124 | { |
104bf76a | 13125 | tree fn, champ; |
73aad9b9 | 13126 | |
dda04398 | 13127 | ++processing_template_decl; |
c8094d83 | 13128 | |
7ca383e6 MM |
13129 | champ = templates; |
13130 | for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn)) | |
73aad9b9 | 13131 | { |
dda04398 | 13132 | int fate = 0; |
c8094d83 | 13133 | |
a34d3336 NS |
13134 | if (get_bindings (TREE_VALUE (champ), |
13135 | DECL_TEMPLATE_RESULT (TREE_VALUE (fn)), | |
13136 | NULL_TREE, /*check_ret=*/false)) | |
dda04398 NS |
13137 | fate--; |
13138 | ||
a34d3336 NS |
13139 | if (get_bindings (TREE_VALUE (fn), |
13140 | DECL_TEMPLATE_RESULT (TREE_VALUE (champ)), | |
13141 | NULL_TREE, /*check_ret=*/false)) | |
dda04398 | 13142 | fate++; |
c8094d83 | 13143 | |
8ca4bf25 MM |
13144 | if (fate == -1) |
13145 | champ = fn; | |
13146 | else if (!fate) | |
73aad9b9 | 13147 | { |
8ca4bf25 MM |
13148 | /* Equally specialized, move to next function. If there |
13149 | is no next function, nothing's most specialized. */ | |
13150 | fn = TREE_CHAIN (fn); | |
7ca383e6 | 13151 | champ = fn; |
8ca4bf25 MM |
13152 | if (!fn) |
13153 | break; | |
73aad9b9 JM |
13154 | } |
13155 | } | |
c8094d83 | 13156 | |
dda04398 NS |
13157 | if (champ) |
13158 | /* Now verify that champ is better than everything earlier in the | |
13159 | instantiation list. */ | |
7ca383e6 | 13160 | for (fn = templates; fn != champ; fn = TREE_CHAIN (fn)) |
a34d3336 NS |
13161 | if (get_bindings (TREE_VALUE (champ), |
13162 | DECL_TEMPLATE_RESULT (TREE_VALUE (fn)), | |
13163 | NULL_TREE, /*check_ret=*/false) | |
13164 | || !get_bindings (TREE_VALUE (fn), | |
13165 | DECL_TEMPLATE_RESULT (TREE_VALUE (champ)), | |
13166 | NULL_TREE, /*check_ret=*/false)) | |
dda04398 NS |
13167 | { |
13168 | champ = NULL_TREE; | |
13169 | break; | |
13170 | } | |
c8094d83 | 13171 | |
dda04398 | 13172 | processing_template_decl--; |
c8094d83 | 13173 | |
dda04398 NS |
13174 | if (!champ) |
13175 | return error_mark_node; | |
73aad9b9 | 13176 | |
7ca383e6 | 13177 | return champ; |
73aad9b9 JM |
13178 | } |
13179 | ||
36a117a5 | 13180 | /* If DECL is a specialization of some template, return the most |
f9a7ae04 MM |
13181 | general such template. Otherwise, returns NULL_TREE. |
13182 | ||
13183 | For example, given: | |
36a117a5 MM |
13184 | |
13185 | template <class T> struct S { template <class U> void f(U); }; | |
13186 | ||
13187 | if TMPL is `template <class U> void S<int>::f(U)' this will return | |
13188 | the full template. This function will not trace past partial | |
13189 | specializations, however. For example, given in addition: | |
13190 | ||
13191 | template <class T> struct S<T*> { template <class U> void f(U); }; | |
13192 | ||
13193 | if TMPL is `template <class U> void S<int*>::f(U)' this will return | |
13194 | `template <class T> template <class U> S<T*>::f(U)'. */ | |
73aad9b9 | 13195 | |
612c671a | 13196 | tree |
3a978d72 | 13197 | most_general_template (tree decl) |
73aad9b9 | 13198 | { |
f9a7ae04 MM |
13199 | /* If DECL is a FUNCTION_DECL, find the TEMPLATE_DECL of which it is |
13200 | an immediate specialization. */ | |
13201 | if (TREE_CODE (decl) == FUNCTION_DECL) | |
13202 | { | |
13203 | if (DECL_TEMPLATE_INFO (decl)) { | |
13204 | decl = DECL_TI_TEMPLATE (decl); | |
13205 | ||
13206 | /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE for a | |
13207 | template friend. */ | |
13208 | if (TREE_CODE (decl) != TEMPLATE_DECL) | |
13209 | return NULL_TREE; | |
13210 | } else | |
13211 | return NULL_TREE; | |
13212 | } | |
13213 | ||
13214 | /* Look for more and more general templates. */ | |
13215 | while (DECL_TEMPLATE_INFO (decl)) | |
13216 | { | |
10b1d5e7 MM |
13217 | /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases. |
13218 | (See cp-tree.h for details.) */ | |
f9a7ae04 MM |
13219 | if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL) |
13220 | break; | |
13221 | ||
6e049fcd KL |
13222 | if (CLASS_TYPE_P (TREE_TYPE (decl)) |
13223 | && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl))) | |
13224 | break; | |
13225 | ||
f9a7ae04 MM |
13226 | /* Stop if we run into an explicitly specialized class template. */ |
13227 | if (!DECL_NAMESPACE_SCOPE_P (decl) | |
13228 | && DECL_CONTEXT (decl) | |
13229 | && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl))) | |
13230 | break; | |
13231 | ||
13232 | decl = DECL_TI_TEMPLATE (decl); | |
13233 | } | |
36a117a5 MM |
13234 | |
13235 | return decl; | |
13236 | } | |
13237 | ||
916b63c3 MM |
13238 | /* Return the most specialized of the class template partial |
13239 | specializations of TMPL which can produce TYPE, a specialization of | |
13240 | TMPL. The value returned is actually a TREE_LIST; the TREE_TYPE is | |
13241 | a _TYPE node corresponding to the partial specialization, while the | |
13242 | TREE_PURPOSE is the set of template arguments that must be | |
13243 | substituted into the TREE_TYPE in order to generate TYPE. | |
13244 | ||
13245 | If the choice of partial specialization is ambiguous, a diagnostic | |
13246 | is issued, and the error_mark_node is returned. If there are no | |
13247 | partial specializations of TMPL matching TYPE, then NULL_TREE is | |
13248 | returned. */ | |
36a117a5 | 13249 | |
e9659ab0 | 13250 | static tree |
916b63c3 | 13251 | most_specialized_class (tree type, tree tmpl) |
36a117a5 MM |
13252 | { |
13253 | tree list = NULL_TREE; | |
13254 | tree t; | |
13255 | tree champ; | |
73aad9b9 | 13256 | int fate; |
916b63c3 MM |
13257 | bool ambiguous_p; |
13258 | tree args; | |
73aad9b9 | 13259 | |
36a117a5 | 13260 | tmpl = most_general_template (tmpl); |
916b63c3 | 13261 | args = CLASSTYPE_TI_ARGS (type); |
36a117a5 | 13262 | for (t = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); t; t = TREE_CHAIN (t)) |
73aad9b9 | 13263 | { |
916b63c3 MM |
13264 | tree partial_spec_args; |
13265 | tree spec_args; | |
13266 | ||
13267 | partial_spec_args = CLASSTYPE_TI_ARGS (TREE_TYPE (t)); | |
3db45ab5 MS |
13268 | spec_args = get_class_bindings (TREE_VALUE (t), |
13269 | partial_spec_args, | |
916b63c3 | 13270 | args); |
36a117a5 | 13271 | if (spec_args) |
73aad9b9 | 13272 | { |
916b63c3 | 13273 | list = tree_cons (spec_args, TREE_VALUE (t), list); |
73aad9b9 JM |
13274 | TREE_TYPE (list) = TREE_TYPE (t); |
13275 | } | |
13276 | } | |
13277 | ||
13278 | if (! list) | |
13279 | return NULL_TREE; | |
13280 | ||
916b63c3 | 13281 | ambiguous_p = false; |
73aad9b9 JM |
13282 | t = list; |
13283 | champ = t; | |
13284 | t = TREE_CHAIN (t); | |
13285 | for (; t; t = TREE_CHAIN (t)) | |
13286 | { | |
916b63c3 | 13287 | fate = more_specialized_class (champ, t); |
73aad9b9 JM |
13288 | if (fate == 1) |
13289 | ; | |
13290 | else | |
13291 | { | |
13292 | if (fate == 0) | |
13293 | { | |
13294 | t = TREE_CHAIN (t); | |
13295 | if (! t) | |
916b63c3 MM |
13296 | { |
13297 | ambiguous_p = true; | |
13298 | break; | |
13299 | } | |
73aad9b9 JM |
13300 | } |
13301 | champ = t; | |
13302 | } | |
13303 | } | |
13304 | ||
916b63c3 MM |
13305 | if (!ambiguous_p) |
13306 | for (t = list; t && t != champ; t = TREE_CHAIN (t)) | |
13307 | { | |
13308 | fate = more_specialized_class (champ, t); | |
13309 | if (fate != 1) | |
13310 | { | |
13311 | ambiguous_p = true; | |
13312 | break; | |
13313 | } | |
13314 | } | |
13315 | ||
13316 | if (ambiguous_p) | |
73aad9b9 | 13317 | { |
916b63c3 MM |
13318 | const char *str = "candidates are:"; |
13319 | error ("ambiguous class template instantiation for %q#T", type); | |
13320 | for (t = list; t; t = TREE_CHAIN (t)) | |
13321 | { | |
13322 | error ("%s %+#T", str, TREE_TYPE (t)); | |
13323 | str = " "; | |
13324 | } | |
13325 | return error_mark_node; | |
73aad9b9 JM |
13326 | } |
13327 | ||
13328 | return champ; | |
13329 | } | |
13330 | ||
eb8845be | 13331 | /* Explicitly instantiate DECL. */ |
e92cc029 | 13332 | |
8d08fdba | 13333 | void |
eb8845be | 13334 | do_decl_instantiation (tree decl, tree storage) |
8d08fdba | 13335 | { |
8d08fdba | 13336 | tree result = NULL_TREE; |
faae18ab | 13337 | int extern_p = 0; |
e8abc66f | 13338 | |
ad47b891 | 13339 | if (!decl || decl == error_mark_node) |
dc957d14 | 13340 | /* An error occurred, for which grokdeclarator has already issued |
3fa56191 MM |
13341 | an appropriate message. */ |
13342 | return; | |
13343 | else if (! DECL_LANG_SPECIFIC (decl)) | |
ec255269 | 13344 | { |
0f51ccfc | 13345 | error ("explicit instantiation of non-template %q#D", decl); |
ec255269 MS |
13346 | return; |
13347 | } | |
03d0f4af | 13348 | else if (TREE_CODE (decl) == VAR_DECL) |
6633d636 | 13349 | { |
03d0f4af MM |
13350 | /* There is an asymmetry here in the way VAR_DECLs and |
13351 | FUNCTION_DECLs are handled by grokdeclarator. In the case of | |
13352 | the latter, the DECL we get back will be marked as a | |
13353 | template instantiation, and the appropriate | |
13354 | DECL_TEMPLATE_INFO will be set up. This does not happen for | |
13355 | VAR_DECLs so we do the lookup here. Probably, grokdeclarator | |
13356 | should handle VAR_DECLs as it currently handles | |
13357 | FUNCTION_DECLs. */ | |
86ac0575 | 13358 | result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false); |
e0860732 | 13359 | if (!result || TREE_CODE (result) != VAR_DECL) |
03d0f4af | 13360 | { |
0f51ccfc | 13361 | error ("no matching template for %qD found", decl); |
03d0f4af MM |
13362 | return; |
13363 | } | |
8ea6dfae SM |
13364 | if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl))) |
13365 | { | |
13366 | error ("type %qT for explicit instantiation %qD does not match " | |
13367 | "declared type %qT", TREE_TYPE (result), decl, | |
13368 | TREE_TYPE (decl)); | |
13369 | return; | |
13370 | } | |
6633d636 MS |
13371 | } |
13372 | else if (TREE_CODE (decl) != FUNCTION_DECL) | |
13373 | { | |
0f51ccfc | 13374 | error ("explicit instantiation of %q#D", decl); |
6633d636 MS |
13375 | return; |
13376 | } | |
03d0f4af MM |
13377 | else |
13378 | result = decl; | |
672476cb | 13379 | |
03d0f4af | 13380 | /* Check for various error cases. Note that if the explicit |
0e339752 | 13381 | instantiation is valid the RESULT will currently be marked as an |
03d0f4af MM |
13382 | *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set |
13383 | until we get here. */ | |
13384 | ||
13385 | if (DECL_TEMPLATE_SPECIALIZATION (result)) | |
672476cb | 13386 | { |
07782718 KL |
13387 | /* DR 259 [temp.spec]. |
13388 | ||
13389 | Both an explicit instantiation and a declaration of an explicit | |
13390 | specialization shall not appear in a program unless the explicit | |
13391 | instantiation follows a declaration of the explicit specialization. | |
03d0f4af | 13392 | |
07782718 KL |
13393 | For a given set of template parameters, if an explicit |
13394 | instantiation of a template appears after a declaration of an | |
13395 | explicit specialization for that template, the explicit | |
13396 | instantiation has no effect. */ | |
672476cb MM |
13397 | return; |
13398 | } | |
03d0f4af MM |
13399 | else if (DECL_EXPLICIT_INSTANTIATION (result)) |
13400 | { | |
13401 | /* [temp.spec] | |
98c1c668 | 13402 | |
03d0f4af | 13403 | No program shall explicitly instantiate any template more |
c8094d83 | 13404 | than once. |
03d0f4af | 13405 | |
4684cd27 MM |
13406 | We check DECL_NOT_REALLY_EXTERN so as not to complain when |
13407 | the first instantiation was `extern' and the second is not, | |
13408 | and EXTERN_P for the opposite case. */ | |
13409 | if (DECL_NOT_REALLY_EXTERN (result) && !extern_p) | |
0f51ccfc | 13410 | pedwarn ("duplicate explicit instantiation of %q#D", result); |
4684cd27 MM |
13411 | /* If an "extern" explicit instantiation follows an ordinary |
13412 | explicit instantiation, the template is instantiated. */ | |
13413 | if (extern_p) | |
03d0f4af MM |
13414 | return; |
13415 | } | |
13416 | else if (!DECL_IMPLICIT_INSTANTIATION (result)) | |
faae18ab | 13417 | { |
0f51ccfc | 13418 | error ("no matching template for %qD found", result); |
faae18ab MS |
13419 | return; |
13420 | } | |
03d0f4af | 13421 | else if (!DECL_TEMPLATE_INFO (result)) |
6633d636 | 13422 | { |
0f51ccfc | 13423 | pedwarn ("explicit instantiation of non-template %q#D", result); |
6633d636 MS |
13424 | return; |
13425 | } | |
13426 | ||
f0e01782 | 13427 | if (storage == NULL_TREE) |
00595019 | 13428 | ; |
faae18ab | 13429 | else if (storage == ridpointers[(int) RID_EXTERN]) |
03d0f4af | 13430 | { |
c02f5e29 | 13431 | if (pedantic && !in_system_header) |
0f51ccfc | 13432 | pedwarn ("ISO C++ forbids the use of %<extern%> on explicit " |
0cbd7506 | 13433 | "instantiations"); |
03d0f4af MM |
13434 | extern_p = 1; |
13435 | } | |
f0e01782 | 13436 | else |
0f51ccfc | 13437 | error ("storage class %qD applied to template instantiation", storage); |
3db45ab5 | 13438 | |
9c12301f | 13439 | check_explicit_instantiation_namespace (result); |
5566b478 | 13440 | mark_decl_instantiated (result, extern_p); |
c91a56d2 | 13441 | if (! extern_p) |
3db45ab5 | 13442 | instantiate_decl (result, /*defer_ok=*/1, |
eba839f9 | 13443 | /*expl_inst_class_mem_p=*/false); |
7177d104 MS |
13444 | } |
13445 | ||
b5791fdc | 13446 | static void |
3a978d72 | 13447 | mark_class_instantiated (tree t, int extern_p) |
faae18ab MS |
13448 | { |
13449 | SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t); | |
eb773359 JM |
13450 | SET_CLASSTYPE_INTERFACE_KNOWN (t); |
13451 | CLASSTYPE_INTERFACE_ONLY (t) = extern_p; | |
faae18ab MS |
13452 | TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p; |
13453 | if (! extern_p) | |
13454 | { | |
13455 | CLASSTYPE_DEBUG_REQUESTED (t) = 1; | |
13456 | rest_of_type_compilation (t, 1); | |
13457 | } | |
c8094d83 | 13458 | } |
e8abc66f | 13459 | |
5e0c54e5 | 13460 | /* Called from do_type_instantiation through binding_table_foreach to |
9bcb9aae | 13461 | do recursive instantiation for the type bound in ENTRY. */ |
5e0c54e5 GDR |
13462 | static void |
13463 | bt_instantiate_type_proc (binding_entry entry, void *data) | |
13464 | { | |
13465 | tree storage = *(tree *) data; | |
13466 | ||
13467 | if (IS_AGGR_TYPE (entry->type) | |
13468 | && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type))) | |
13469 | do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0); | |
13470 | } | |
13471 | ||
415c974c MA |
13472 | /* Called from do_type_instantiation to instantiate a member |
13473 | (a member function or a static member variable) of an | |
03fd3f84 | 13474 | explicitly instantiated class template. */ |
415c974c MA |
13475 | static void |
13476 | instantiate_class_member (tree decl, int extern_p) | |
13477 | { | |
13478 | mark_decl_instantiated (decl, extern_p); | |
415c974c | 13479 | if (! extern_p) |
3db45ab5 | 13480 | instantiate_decl (decl, /*defer_ok=*/1, |
eba839f9 | 13481 | /*expl_inst_class_mem_p=*/true); |
415c974c MA |
13482 | } |
13483 | ||
a1bcc528 JM |
13484 | /* Perform an explicit instantiation of template class T. STORAGE, if |
13485 | non-null, is the RID for extern, inline or static. COMPLAIN is | |
838dfd8a | 13486 | nonzero if this is called from the parser, zero if called recursively, |
a1bcc528 | 13487 | since the standard is unclear (as detailed below). */ |
c8094d83 | 13488 | |
7177d104 | 13489 | void |
3a978d72 | 13490 | do_type_instantiation (tree t, tree storage, tsubst_flags_t complain) |
7177d104 | 13491 | { |
e8abc66f MS |
13492 | int extern_p = 0; |
13493 | int nomem_p = 0; | |
5566b478 | 13494 | int static_p = 0; |
4746cf84 | 13495 | int previous_instantiation_extern_p = 0; |
5566b478 | 13496 | |
ca79f85d JM |
13497 | if (TREE_CODE (t) == TYPE_DECL) |
13498 | t = TREE_TYPE (t); | |
13499 | ||
7ddedda4 | 13500 | if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t)) |
ca79f85d | 13501 | { |
0f51ccfc | 13502 | error ("explicit instantiation of non-template type %qT", t); |
ca79f85d JM |
13503 | return; |
13504 | } | |
13505 | ||
5566b478 | 13506 | complete_type (t); |
7177d104 | 13507 | |
d0f062fb | 13508 | if (!COMPLETE_TYPE_P (t)) |
f0e01782 | 13509 | { |
c2ea3a40 | 13510 | if (complain & tf_error) |
0f51ccfc | 13511 | error ("explicit instantiation of %q#T before definition of template", |
0cbd7506 | 13512 | t); |
f0e01782 MS |
13513 | return; |
13514 | } | |
13515 | ||
03d0f4af | 13516 | if (storage != NULL_TREE) |
f0e01782 | 13517 | { |
c02f5e29 | 13518 | if (pedantic && !in_system_header) |
c8094d83 | 13519 | pedwarn("ISO C++ forbids the use of %qE on explicit instantiations", |
0cbd7506 | 13520 | storage); |
03d0f4af MM |
13521 | |
13522 | if (storage == ridpointers[(int) RID_INLINE]) | |
13523 | nomem_p = 1; | |
13524 | else if (storage == ridpointers[(int) RID_EXTERN]) | |
13525 | extern_p = 1; | |
13526 | else if (storage == ridpointers[(int) RID_STATIC]) | |
13527 | static_p = 1; | |
13528 | else | |
13529 | { | |
0f51ccfc | 13530 | error ("storage class %qD applied to template instantiation", |
0cbd7506 | 13531 | storage); |
03d0f4af MM |
13532 | extern_p = 0; |
13533 | } | |
f0e01782 MS |
13534 | } |
13535 | ||
370af2d5 | 13536 | if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t)) |
03d0f4af | 13537 | { |
07782718 | 13538 | /* DR 259 [temp.spec]. |
a292b002 | 13539 | |
07782718 KL |
13540 | Both an explicit instantiation and a declaration of an explicit |
13541 | specialization shall not appear in a program unless the explicit | |
13542 | instantiation follows a declaration of the explicit specialization. | |
13543 | ||
13544 | For a given set of template parameters, if an explicit | |
13545 | instantiation of a template appears after a declaration of an | |
13546 | explicit specialization for that template, the explicit | |
13547 | instantiation has no effect. */ | |
03d0f4af MM |
13548 | return; |
13549 | } | |
13550 | else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t)) | |
44a8d0b3 | 13551 | { |
03d0f4af MM |
13552 | /* [temp.spec] |
13553 | ||
13554 | No program shall explicitly instantiate any template more | |
c8094d83 | 13555 | than once. |
03d0f4af | 13556 | |
0cbd7506 | 13557 | If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit |
4746cf84 | 13558 | instantiation was `extern'. If EXTERN_P then the second is. |
4684cd27 | 13559 | These cases are OK. */ |
4746cf84 MA |
13560 | previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t); |
13561 | ||
13562 | if (!previous_instantiation_extern_p && !extern_p | |
c2ea3a40 | 13563 | && (complain & tf_error)) |
0f51ccfc | 13564 | pedwarn ("duplicate explicit instantiation of %q#T", t); |
c8094d83 | 13565 | |
03d0f4af MM |
13566 | /* If we've already instantiated the template, just return now. */ |
13567 | if (!CLASSTYPE_INTERFACE_ONLY (t)) | |
13568 | return; | |
44a8d0b3 | 13569 | } |
e8abc66f | 13570 | |
9c12301f | 13571 | check_explicit_instantiation_namespace (TYPE_NAME (t)); |
03d0f4af | 13572 | mark_class_instantiated (t, extern_p); |
03d0f4af | 13573 | |
e8abc66f MS |
13574 | if (nomem_p) |
13575 | return; | |
13576 | ||
7177d104 | 13577 | { |
db5ae43f | 13578 | tree tmp; |
5566b478 | 13579 | |
03d0f4af MM |
13580 | /* In contrast to implicit instantiation, where only the |
13581 | declarations, and not the definitions, of members are | |
13582 | instantiated, we have here: | |
13583 | ||
0cbd7506 | 13584 | [temp.explicit] |
03d0f4af MM |
13585 | |
13586 | The explicit instantiation of a class template specialization | |
13587 | implies the instantiation of all of its members not | |
13588 | previously explicitly specialized in the translation unit | |
c8094d83 | 13589 | containing the explicit instantiation. |
03d0f4af MM |
13590 | |
13591 | Of course, we can't instantiate member template classes, since | |
13592 | we don't have any arguments for them. Note that the standard | |
dc957d14 | 13593 | is unclear on whether the instantiation of the members are |
415c974c | 13594 | *explicit* instantiations or not. However, the most natural |
03fd3f84 | 13595 | interpretation is that it should be an explicit instantiation. */ |
03d0f4af | 13596 | |
5566b478 MS |
13597 | if (! static_p) |
13598 | for (tmp = TYPE_METHODS (t); tmp; tmp = TREE_CHAIN (tmp)) | |
a7d87521 | 13599 | if (TREE_CODE (tmp) == FUNCTION_DECL |
1f109f0f | 13600 | && DECL_TEMPLATE_INSTANTIATION (tmp)) |
415c974c | 13601 | instantiate_class_member (tmp, extern_p); |
5566b478 MS |
13602 | |
13603 | for (tmp = TYPE_FIELDS (t); tmp; tmp = TREE_CHAIN (tmp)) | |
13604 | if (TREE_CODE (tmp) == VAR_DECL && DECL_TEMPLATE_INSTANTIATION (tmp)) | |
415c974c | 13605 | instantiate_class_member (tmp, extern_p); |
7177d104 | 13606 | |
5e0c54e5 GDR |
13607 | if (CLASSTYPE_NESTED_UTDS (t)) |
13608 | binding_table_foreach (CLASSTYPE_NESTED_UTDS (t), | |
0cbd7506 | 13609 | bt_instantiate_type_proc, &storage); |
a292b002 | 13610 | } |
8d08fdba | 13611 | } |
a28e3c7f | 13612 | |
36a117a5 MM |
13613 | /* Given a function DECL, which is a specialization of TMPL, modify |
13614 | DECL to be a re-instantiation of TMPL with the same template | |
13615 | arguments. TMPL should be the template into which tsubst'ing | |
13616 | should occur for DECL, not the most general template. | |
830bfa74 MM |
13617 | |
13618 | One reason for doing this is a scenario like this: | |
13619 | ||
13620 | template <class T> | |
13621 | void f(const T&, int i); | |
13622 | ||
13623 | void g() { f(3, 7); } | |
13624 | ||
13625 | template <class T> | |
13626 | void f(const T& t, const int i) { } | |
13627 | ||
13628 | Note that when the template is first instantiated, with | |
13629 | instantiate_template, the resulting DECL will have no name for the | |
13630 | first parameter, and the wrong type for the second. So, when we go | |
13631 | to instantiate the DECL, we regenerate it. */ | |
13632 | ||
e9659ab0 | 13633 | static void |
3a978d72 | 13634 | regenerate_decl_from_template (tree decl, tree tmpl) |
830bfa74 | 13635 | { |
f9a7ae04 MM |
13636 | /* The arguments used to instantiate DECL, from the most general |
13637 | template. */ | |
830bfa74 | 13638 | tree args; |
830bfa74 | 13639 | tree code_pattern; |
830bfa74 MM |
13640 | |
13641 | args = DECL_TI_ARGS (decl); | |
13642 | code_pattern = DECL_TEMPLATE_RESULT (tmpl); | |
13643 | ||
2b907f5c KL |
13644 | /* Make sure that we can see identifiers, and compute access |
13645 | correctly. */ | |
13646 | push_access_scope (decl); | |
13647 | ||
c7222c02 MM |
13648 | if (TREE_CODE (decl) == FUNCTION_DECL) |
13649 | { | |
13650 | tree decl_parm; | |
13651 | tree pattern_parm; | |
13652 | tree specs; | |
13653 | int args_depth; | |
13654 | int parms_depth; | |
c8094d83 | 13655 | |
c7222c02 | 13656 | args_depth = TMPL_ARGS_DEPTH (args); |
c8094d83 | 13657 | parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)); |
c7222c02 MM |
13658 | if (args_depth > parms_depth) |
13659 | args = get_innermost_template_args (args, parms_depth); | |
13660 | ||
13661 | specs = tsubst_exception_specification (TREE_TYPE (code_pattern), | |
13662 | args, tf_error, NULL_TREE); | |
13663 | if (specs) | |
13664 | TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl), | |
13665 | specs); | |
13666 | ||
13667 | /* Merge parameter declarations. */ | |
c8094d83 | 13668 | decl_parm = skip_artificial_parms_for (decl, |
c7222c02 | 13669 | DECL_ARGUMENTS (decl)); |
c8094d83 | 13670 | pattern_parm |
c7222c02 MM |
13671 | = skip_artificial_parms_for (code_pattern, |
13672 | DECL_ARGUMENTS (code_pattern)); | |
5d80a306 | 13673 | while (decl_parm && !FUNCTION_PARAMETER_PACK_P (pattern_parm)) |
c7222c02 MM |
13674 | { |
13675 | tree parm_type; | |
b17bba6d | 13676 | tree attributes; |
5d80a306 | 13677 | |
c7222c02 MM |
13678 | if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm)) |
13679 | DECL_NAME (decl_parm) = DECL_NAME (pattern_parm); | |
13680 | parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error, | |
13681 | NULL_TREE); | |
02bab9db | 13682 | parm_type = type_decays_to (parm_type); |
c7222c02 MM |
13683 | if (!same_type_p (TREE_TYPE (decl_parm), parm_type)) |
13684 | TREE_TYPE (decl_parm) = parm_type; | |
b17bba6d MM |
13685 | attributes = DECL_ATTRIBUTES (pattern_parm); |
13686 | if (DECL_ATTRIBUTES (decl_parm) != attributes) | |
13687 | { | |
13688 | DECL_ATTRIBUTES (decl_parm) = attributes; | |
13689 | cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0); | |
13690 | } | |
c7222c02 MM |
13691 | decl_parm = TREE_CHAIN (decl_parm); |
13692 | pattern_parm = TREE_CHAIN (pattern_parm); | |
13693 | } | |
5d80a306 DG |
13694 | /* Merge any parameters that match with the function parameter |
13695 | pack. */ | |
13696 | if (pattern_parm && FUNCTION_PARAMETER_PACK_P (pattern_parm)) | |
13697 | { | |
13698 | int i, len; | |
13699 | tree expanded_types; | |
13700 | /* Expand the TYPE_PACK_EXPANSION that provides the types for | |
13701 | the parameters in this function parameter pack. */ | |
13702 | expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm), | |
13703 | args, tf_error, NULL_TREE); | |
13704 | len = TREE_VEC_LENGTH (expanded_types); | |
13705 | for (i = 0; i < len; i++) | |
13706 | { | |
13707 | tree parm_type; | |
13708 | tree attributes; | |
13709 | ||
13710 | if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm)) | |
13711 | /* Rename the parameter to include the index. */ | |
13712 | DECL_NAME (decl_parm) = | |
13713 | make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i); | |
13714 | parm_type = TREE_VEC_ELT (expanded_types, i); | |
13715 | parm_type = type_decays_to (parm_type); | |
13716 | if (!same_type_p (TREE_TYPE (decl_parm), parm_type)) | |
13717 | TREE_TYPE (decl_parm) = parm_type; | |
13718 | attributes = DECL_ATTRIBUTES (pattern_parm); | |
13719 | if (DECL_ATTRIBUTES (decl_parm) != attributes) | |
13720 | { | |
13721 | DECL_ATTRIBUTES (decl_parm) = attributes; | |
13722 | cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0); | |
13723 | } | |
13724 | decl_parm = TREE_CHAIN (decl_parm); | |
13725 | } | |
13726 | } | |
c7222c02 MM |
13727 | /* Merge additional specifiers from the CODE_PATTERN. */ |
13728 | if (DECL_DECLARED_INLINE_P (code_pattern) | |
13729 | && !DECL_DECLARED_INLINE_P (decl)) | |
13730 | DECL_DECLARED_INLINE_P (decl) = 1; | |
13731 | if (DECL_INLINE (code_pattern) && !DECL_INLINE (decl)) | |
13732 | DECL_INLINE (decl) = 1; | |
13733 | } | |
13734 | else if (TREE_CODE (decl) == VAR_DECL) | |
b794e321 MM |
13735 | DECL_INITIAL (decl) = |
13736 | tsubst_expr (DECL_INITIAL (code_pattern), args, | |
015c2c66 MM |
13737 | tf_error, DECL_TI_TEMPLATE (decl), |
13738 | /*integral_constant_expression_p=*/false); | |
c7222c02 MM |
13739 | else |
13740 | gcc_unreachable (); | |
36a117a5 | 13741 | |
2b59fc25 | 13742 | pop_access_scope (decl); |
830bfa74 MM |
13743 | } |
13744 | ||
a723baf1 MM |
13745 | /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be |
13746 | substituted to get DECL. */ | |
13747 | ||
d58b7c2d | 13748 | tree |
a723baf1 MM |
13749 | template_for_substitution (tree decl) |
13750 | { | |
13751 | tree tmpl = DECL_TI_TEMPLATE (decl); | |
13752 | ||
13753 | /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern | |
13754 | for the instantiation. This is not always the most general | |
13755 | template. Consider, for example: | |
13756 | ||
0cbd7506 | 13757 | template <class T> |
a723baf1 | 13758 | struct S { template <class U> void f(); |
0cbd7506 | 13759 | template <> void f<int>(); }; |
a723baf1 MM |
13760 | |
13761 | and an instantiation of S<double>::f<int>. We want TD to be the | |
13762 | specialization S<T>::f<int>, not the more general S<T>::f<U>. */ | |
13763 | while (/* An instantiation cannot have a definition, so we need a | |
13764 | more general template. */ | |
13765 | DECL_TEMPLATE_INSTANTIATION (tmpl) | |
13766 | /* We must also deal with friend templates. Given: | |
13767 | ||
c8094d83 | 13768 | template <class T> struct S { |
a723baf1 MM |
13769 | template <class U> friend void f() {}; |
13770 | }; | |
13771 | ||
13772 | S<int>::f<U> say, is not an instantiation of S<T>::f<U>, | |
13773 | so far as the language is concerned, but that's still | |
13774 | where we get the pattern for the instantiation from. On | |
13775 | other hand, if the definition comes outside the class, say: | |
13776 | ||
c8094d83 | 13777 | template <class T> struct S { |
a723baf1 MM |
13778 | template <class U> friend void f(); |
13779 | }; | |
13780 | template <class U> friend void f() {} | |
13781 | ||
13782 | we don't need to look any further. That's what the check for | |
13783 | DECL_INITIAL is for. */ | |
13784 | || (TREE_CODE (decl) == FUNCTION_DECL | |
13785 | && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl) | |
13786 | && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl)))) | |
13787 | { | |
13788 | /* The present template, TD, should not be a definition. If it | |
13789 | were a definition, we should be using it! Note that we | |
13790 | cannot restructure the loop to just keep going until we find | |
13791 | a template with a definition, since that might go too far if | |
13792 | a specialization was declared, but not defined. */ | |
50bc768d NS |
13793 | gcc_assert (TREE_CODE (decl) != VAR_DECL |
13794 | || DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (tmpl))); | |
c8094d83 | 13795 | |
a723baf1 MM |
13796 | /* Fetch the more general template. */ |
13797 | tmpl = DECL_TI_TEMPLATE (tmpl); | |
13798 | } | |
13799 | ||
13800 | return tmpl; | |
13801 | } | |
13802 | ||
16d53b64 | 13803 | /* Produce the definition of D, a _DECL generated from a template. If |
838dfd8a | 13804 | DEFER_OK is nonzero, then we don't have to actually do the |
415c974c MA |
13805 | instantiation now; we just have to do it sometime. Normally it is |
13806 | an error if this is an explicit instantiation but D is undefined. | |
eba839f9 MM |
13807 | EXPL_INST_CLASS_MEM_P is true iff D is a member of an |
13808 | explicitly instantiated class template. */ | |
f84b4be9 | 13809 | |
a28e3c7f | 13810 | tree |
3db45ab5 | 13811 | instantiate_decl (tree d, int defer_ok, |
eba839f9 | 13812 | bool expl_inst_class_mem_p) |
a28e3c7f | 13813 | { |
36a117a5 | 13814 | tree tmpl = DECL_TI_TEMPLATE (d); |
65f8b0fb MM |
13815 | tree gen_args; |
13816 | tree args; | |
830bfa74 | 13817 | tree td; |
36a117a5 MM |
13818 | tree code_pattern; |
13819 | tree spec; | |
13820 | tree gen_tmpl; | |
b794e321 | 13821 | bool pattern_defined; |
31a714f6 | 13822 | int need_push; |
82a98427 | 13823 | location_t saved_loc = input_location; |
f7e4e484 | 13824 | bool external_p; |
c8094d83 | 13825 | |
36a117a5 MM |
13826 | /* This function should only be used to instantiate templates for |
13827 | functions and static member variables. */ | |
50bc768d NS |
13828 | gcc_assert (TREE_CODE (d) == FUNCTION_DECL |
13829 | || TREE_CODE (d) == VAR_DECL); | |
36a117a5 | 13830 | |
cec24319 MM |
13831 | /* Variables are never deferred; if instantiation is required, they |
13832 | are instantiated right away. That allows for better code in the | |
13833 | case that an expression refers to the value of the variable -- | |
13834 | if the variable has a constant value the referring expression can | |
13835 | take advantage of that fact. */ | |
13836 | if (TREE_CODE (d) == VAR_DECL) | |
13837 | defer_ok = 0; | |
13838 | ||
db9b2174 MM |
13839 | /* Don't instantiate cloned functions. Instead, instantiate the |
13840 | functions they cloned. */ | |
13841 | if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d)) | |
13842 | d = DECL_CLONED_FUNCTION (d); | |
13843 | ||
fbf1c34b | 13844 | if (DECL_TEMPLATE_INSTANTIATED (d)) |
03d0f4af | 13845 | /* D has already been instantiated. It might seem reasonable to |
dc957d14 | 13846 | check whether or not D is an explicit instantiation, and, if so, |
03d0f4af MM |
13847 | stop here. But when an explicit instantiation is deferred |
13848 | until the end of the compilation, DECL_EXPLICIT_INSTANTIATION | |
13849 | is set, even though we still need to do the instantiation. */ | |
36a117a5 MM |
13850 | return d; |
13851 | ||
13852 | /* If we already have a specialization of this declaration, then | |
13853 | there's no reason to instantiate it. Note that | |
13854 | retrieve_specialization gives us both instantiations and | |
13855 | specializations, so we must explicitly check | |
13856 | DECL_TEMPLATE_SPECIALIZATION. */ | |
13857 | gen_tmpl = most_general_template (tmpl); | |
65f8b0fb | 13858 | gen_args = DECL_TI_ARGS (d); |
c7222c02 MM |
13859 | spec = retrieve_specialization (gen_tmpl, gen_args, |
13860 | /*class_specializations_p=*/false); | |
36a117a5 MM |
13861 | if (spec != NULL_TREE && DECL_TEMPLATE_SPECIALIZATION (spec)) |
13862 | return spec; | |
13863 | ||
13864 | /* This needs to happen before any tsubsting. */ | |
13865 | if (! push_tinst_level (d)) | |
13866 | return d; | |
13867 | ||
297a5329 JM |
13868 | timevar_push (TV_PARSE); |
13869 | ||
4d85e00e | 13870 | /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern |
a723baf1 MM |
13871 | for the instantiation. */ |
13872 | td = template_for_substitution (d); | |
fee23f54 | 13873 | code_pattern = DECL_TEMPLATE_RESULT (td); |
27bb8339 | 13874 | |
2d22db1f | 13875 | /* We should never be trying to instantiate a member of a class |
3db45ab5 | 13876 | template or partial specialization. */ |
2d22db1f | 13877 | gcc_assert (d != code_pattern); |
3db45ab5 | 13878 | |
76d3baad KL |
13879 | if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d)) |
13880 | || DECL_TEMPLATE_SPECIALIZATION (td)) | |
649fc72d NS |
13881 | /* In the case of a friend template whose definition is provided |
13882 | outside the class, we may have too many arguments. Drop the | |
76d3baad | 13883 | ones we don't need. The same is true for specializations. */ |
649fc72d NS |
13884 | args = get_innermost_template_args |
13885 | (gen_args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td))); | |
13886 | else | |
13887 | args = gen_args; | |
65f8b0fb | 13888 | |
5566b478 | 13889 | if (TREE_CODE (d) == FUNCTION_DECL) |
39c76b4f | 13890 | pattern_defined = (DECL_SAVED_TREE (code_pattern) != NULL_TREE); |
5566b478 | 13891 | else |
36a117a5 | 13892 | pattern_defined = ! DECL_IN_AGGR_P (code_pattern); |
b794e321 MM |
13893 | |
13894 | /* We may be in the middle of deferred access check. Disable it now. */ | |
13895 | push_deferring_access_checks (dk_no_deferred); | |
13896 | ||
4684cd27 MM |
13897 | /* Unless an explicit instantiation directive has already determined |
13898 | the linkage of D, remember that a definition is available for | |
13899 | this entity. */ | |
c8094d83 | 13900 | if (pattern_defined |
4684cd27 MM |
13901 | && !DECL_INTERFACE_KNOWN (d) |
13902 | && !DECL_NOT_REALLY_EXTERN (d)) | |
13903 | mark_definable (d); | |
de22184b | 13904 | |
f31686a3 | 13905 | input_location = DECL_SOURCE_LOCATION (d); |
de22184b | 13906 | |
eba839f9 MM |
13907 | /* If D is a member of an explicitly instantiated class template, |
13908 | and no definition is available, treat it like an implicit | |
3db45ab5 MS |
13909 | instantiation. */ |
13910 | if (!pattern_defined && expl_inst_class_mem_p | |
13911 | && DECL_EXPLICIT_INSTANTIATION (d)) | |
5566b478 | 13912 | { |
4684cd27 | 13913 | DECL_NOT_REALLY_EXTERN (d) = 0; |
eba839f9 | 13914 | DECL_INTERFACE_KNOWN (d) = 0; |
4684cd27 | 13915 | SET_DECL_IMPLICIT_INSTANTIATION (d); |
5566b478 MS |
13916 | } |
13917 | ||
4f2b0fb2 NS |
13918 | if (!defer_ok) |
13919 | { | |
13920 | /* Recheck the substitutions to obtain any warning messages | |
13921 | about ignoring cv qualifiers. */ | |
13922 | tree gen = DECL_TEMPLATE_RESULT (gen_tmpl); | |
13923 | tree type = TREE_TYPE (gen); | |
13924 | ||
0e902d98 | 13925 | /* Make sure that we can see identifiers, and compute access |
2b59fc25 KL |
13926 | correctly. D is already the target FUNCTION_DECL with the |
13927 | right context. */ | |
13928 | push_access_scope (d); | |
0e902d98 | 13929 | |
4f2b0fb2 NS |
13930 | if (TREE_CODE (gen) == FUNCTION_DECL) |
13931 | { | |
23fca1f5 | 13932 | tsubst (DECL_ARGUMENTS (gen), gen_args, tf_warning_or_error, d); |
5d80a306 DG |
13933 | tsubst_exception_specification (type, gen_args, tf_warning_or_error, |
13934 | d); | |
4f2b0fb2 NS |
13935 | /* Don't simply tsubst the function type, as that will give |
13936 | duplicate warnings about poor parameter qualifications. | |
13937 | The function arguments are the same as the decl_arguments | |
c6002625 | 13938 | without the top level cv qualifiers. */ |
4f2b0fb2 NS |
13939 | type = TREE_TYPE (type); |
13940 | } | |
23fca1f5 | 13941 | tsubst (type, gen_args, tf_warning_or_error, d); |
0e902d98 | 13942 | |
2b59fc25 | 13943 | pop_access_scope (d); |
4f2b0fb2 | 13944 | } |
c8094d83 | 13945 | |
f7e4e484 MM |
13946 | /* Check to see whether we know that this template will be |
13947 | instantiated in some other file, as with "extern template" | |
13948 | extension. */ | |
13949 | external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d)); | |
13950 | /* In general, we do not instantiate such templates... */ | |
13951 | if (external_p | |
13952 | /* ... but we instantiate inline functions so that we can inline | |
13953 | them and ... */ | |
13954 | && ! (TREE_CODE (d) == FUNCTION_DECL && DECL_INLINE (d)) | |
13955 | /* ... we instantiate static data members whose values are | |
13956 | needed in integral constant expressions. */ | |
3db45ab5 | 13957 | && ! (TREE_CODE (d) == VAR_DECL |
f7e4e484 | 13958 | && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (d))) |
930cd796 | 13959 | goto out; |
16d53b64 | 13960 | /* Defer all other templates, unless we have been explicitly |
f7e4e484 MM |
13961 | forbidden from doing so. */ |
13962 | if (/* If there is no definition, we cannot instantiate the | |
13963 | template. */ | |
3db45ab5 | 13964 | ! pattern_defined |
f7e4e484 MM |
13965 | /* If it's OK to postpone instantiation, do so. */ |
13966 | || defer_ok | |
13967 | /* If this is a static data member that will be defined | |
13968 | elsewhere, we don't want to instantiate the entire data | |
13969 | member, but we do want to instantiate the initializer so that | |
13970 | we can substitute that elsewhere. */ | |
13971 | || (external_p && TREE_CODE (d) == VAR_DECL)) | |
16d53b64 | 13972 | { |
b794e321 MM |
13973 | /* The definition of the static data member is now required so |
13974 | we must substitute the initializer. */ | |
13975 | if (TREE_CODE (d) == VAR_DECL | |
3db45ab5 | 13976 | && !DECL_INITIAL (d) |
b794e321 MM |
13977 | && DECL_INITIAL (code_pattern)) |
13978 | { | |
4bff36d3 MM |
13979 | tree ns; |
13980 | tree init; | |
13981 | ||
13982 | ns = decl_namespace_context (d); | |
27a725e2 | 13983 | push_nested_namespace (ns); |
b794e321 | 13984 | push_nested_class (DECL_CONTEXT (d)); |
3db45ab5 | 13985 | init = tsubst_expr (DECL_INITIAL (code_pattern), |
4bff36d3 | 13986 | args, |
015c2c66 MM |
13987 | tf_warning_or_error, NULL_TREE, |
13988 | /*integral_constant_expression_p=*/false); | |
d174af6c MM |
13989 | cp_finish_decl (d, init, /*init_const_expr_p=*/false, |
13990 | /*asmspec_tree=*/NULL_TREE, | |
86414779 | 13991 | LOOKUP_ONLYCONVERTING); |
b794e321 | 13992 | pop_nested_class (); |
27a725e2 | 13993 | pop_nested_namespace (ns); |
b794e321 MM |
13994 | } |
13995 | ||
f7e4e484 MM |
13996 | /* We restore the source position here because it's used by |
13997 | add_pending_template. */ | |
82a98427 | 13998 | input_location = saved_loc; |
c27be9b9 | 13999 | |
c8094d83 | 14000 | if (at_eof && !pattern_defined |
03d0f4af MM |
14001 | && DECL_EXPLICIT_INSTANTIATION (d)) |
14002 | /* [temp.explicit] | |
14003 | ||
14004 | The definition of a non-exported function template, a | |
14005 | non-exported member function template, or a non-exported | |
14006 | member function or static data member of a class template | |
14007 | shall be present in every translation unit in which it is | |
14008 | explicitly instantiated. */ | |
33bd39a2 | 14009 | pedwarn |
0f51ccfc | 14010 | ("explicit instantiation of %qD but no definition available", d); |
03d0f4af | 14011 | |
f7e4e484 MM |
14012 | /* ??? Historically, we have instantiated inline functions, even |
14013 | when marked as "extern template". */ | |
14014 | if (!(external_p && TREE_CODE (d) == VAR_DECL)) | |
14015 | add_pending_template (d); | |
de22184b | 14016 | goto out; |
5566b478 | 14017 | } |
4684cd27 MM |
14018 | /* Tell the repository that D is available in this translation unit |
14019 | -- and see if it is supposed to be instantiated here. */ | |
14020 | if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d)) | |
14021 | { | |
14022 | /* In a PCH file, despite the fact that the repository hasn't | |
14023 | requested instantiation in the PCH it is still possible that | |
14024 | an instantiation will be required in a file that includes the | |
14025 | PCH. */ | |
14026 | if (pch_file) | |
14027 | add_pending_template (d); | |
14028 | /* Instantiate inline functions so that the inliner can do its | |
14029 | job, even though we'll not be emitting a copy of this | |
14030 | function. */ | |
c2a124b2 | 14031 | if (!(TREE_CODE (d) == FUNCTION_DECL |
c8094d83 | 14032 | && flag_inline_trees |
c2a124b2 | 14033 | && DECL_DECLARED_INLINE_P (d))) |
4684cd27 MM |
14034 | goto out; |
14035 | } | |
5566b478 | 14036 | |
6de9cd9a | 14037 | need_push = !cfun || !global_bindings_p (); |
31a714f6 MM |
14038 | if (need_push) |
14039 | push_to_top_level (); | |
414ea4aa | 14040 | |
66e0c440 KL |
14041 | /* Mark D as instantiated so that recursive calls to |
14042 | instantiate_decl do not try to instantiate it again. */ | |
14043 | DECL_TEMPLATE_INSTANTIATED (d) = 1; | |
14044 | ||
2b0a63a3 MM |
14045 | /* Regenerate the declaration in case the template has been modified |
14046 | by a subsequent redeclaration. */ | |
14047 | regenerate_decl_from_template (d, td); | |
4684cd27 | 14048 | |
120722ac | 14049 | /* We already set the file and line above. Reset them now in case |
6de9cd9a | 14050 | they changed as a result of calling regenerate_decl_from_template. */ |
f31686a3 | 14051 | input_location = DECL_SOURCE_LOCATION (d); |
5156628f | 14052 | |
5566b478 MS |
14053 | if (TREE_CODE (d) == VAR_DECL) |
14054 | { | |
e92fb501 MM |
14055 | tree init; |
14056 | ||
1d62c33e MM |
14057 | /* Clear out DECL_RTL; whatever was there before may not be right |
14058 | since we've reset the type of the declaration. */ | |
14059 | SET_DECL_RTL (d, NULL_RTX); | |
5566b478 | 14060 | DECL_IN_AGGR_P (d) = 0; |
ea56c40c | 14061 | |
e92fb501 MM |
14062 | /* The initializer is placed in DECL_INITIAL by |
14063 | regenerate_decl_from_template. Pull it out so that | |
14064 | finish_decl can process it. */ | |
14065 | init = DECL_INITIAL (d); | |
14066 | DECL_INITIAL (d) = NULL_TREE; | |
14067 | DECL_INITIALIZED_P (d) = 0; | |
14068 | ||
4684cd27 MM |
14069 | /* Clear DECL_EXTERNAL so that cp_finish_decl will process the |
14070 | initializer. That function will defer actual emission until | |
14071 | we have a chance to determine linkage. */ | |
14072 | DECL_EXTERNAL (d) = 0; | |
14073 | ||
73a8adb6 | 14074 | /* Enter the scope of D so that access-checking works correctly. */ |
4684cd27 | 14075 | push_nested_class (DECL_CONTEXT (d)); |
e92fb501 | 14076 | finish_decl (d, init, NULL_TREE); |
73a8adb6 | 14077 | pop_nested_class (); |
5566b478 MS |
14078 | } |
14079 | else if (TREE_CODE (d) == FUNCTION_DECL) | |
14080 | { | |
6bbf1598 | 14081 | htab_t saved_local_specializations; |
a723baf1 MM |
14082 | tree subst_decl; |
14083 | tree tmpl_parm; | |
14084 | tree spec_parm; | |
6bbf1598 MM |
14085 | |
14086 | /* Save away the current list, in case we are instantiating one | |
14087 | template from within the body of another. */ | |
14088 | saved_local_specializations = local_specializations; | |
14089 | ||
6dfbb909 | 14090 | /* Set up the list of local specializations. */ |
c8094d83 | 14091 | local_specializations = htab_create (37, |
69f794a7 | 14092 | hash_local_specialization, |
a723baf1 | 14093 | eq_local_specializations, |
6dfbb909 MM |
14094 | NULL); |
14095 | ||
558475f0 | 14096 | /* Set up context. */ |
058b15c1 | 14097 | start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED); |
5566b478 | 14098 | |
a723baf1 MM |
14099 | /* Create substitution entries for the parameters. */ |
14100 | subst_decl = DECL_TEMPLATE_RESULT (template_for_substitution (d)); | |
14101 | tmpl_parm = DECL_ARGUMENTS (subst_decl); | |
14102 | spec_parm = DECL_ARGUMENTS (d); | |
14103 | if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d)) | |
14104 | { | |
14105 | register_local_specialization (spec_parm, tmpl_parm); | |
14106 | spec_parm = skip_artificial_parms_for (d, spec_parm); | |
14107 | tmpl_parm = skip_artificial_parms_for (subst_decl, tmpl_parm); | |
14108 | } | |
5d80a306 | 14109 | while (tmpl_parm && !FUNCTION_PARAMETER_PACK_P (tmpl_parm)) |
a723baf1 MM |
14110 | { |
14111 | register_local_specialization (spec_parm, tmpl_parm); | |
14112 | tmpl_parm = TREE_CHAIN (tmpl_parm); | |
14113 | spec_parm = TREE_CHAIN (spec_parm); | |
14114 | } | |
5d80a306 DG |
14115 | if (tmpl_parm && FUNCTION_PARAMETER_PACK_P (tmpl_parm)) |
14116 | { | |
14117 | /* Collect all of the extra "packed" parameters into an | |
14118 | argument pack. */ | |
14119 | tree parmvec; | |
14120 | tree parmtypevec; | |
14121 | tree argpack = make_node (NONTYPE_ARGUMENT_PACK); | |
14122 | tree argtypepack = make_node (TYPE_ARGUMENT_PACK); | |
14123 | int i, len = 0; | |
14124 | tree t; | |
14125 | ||
14126 | /* Count how many parameters remain. */ | |
14127 | for (t = spec_parm; t; t = TREE_CHAIN (t)) | |
14128 | len++; | |
14129 | ||
14130 | /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */ | |
14131 | parmvec = make_tree_vec (len); | |
14132 | parmtypevec = make_tree_vec (len); | |
14133 | for(i = 0; i < len; i++, spec_parm = TREE_CHAIN (spec_parm)) | |
14134 | { | |
14135 | TREE_VEC_ELT (parmvec, i) = spec_parm; | |
14136 | TREE_VEC_ELT (parmtypevec, i) = TREE_TYPE (spec_parm); | |
14137 | } | |
14138 | ||
14139 | /* Build the argument packs. */ | |
14140 | SET_ARGUMENT_PACK_ARGS (argpack, parmvec); | |
14141 | SET_ARGUMENT_PACK_ARGS (argtypepack, parmtypevec); | |
14142 | TREE_TYPE (argpack) = argtypepack; | |
14143 | ||
14144 | /* Register the (value) argument pack as a specialization of | |
14145 | TMPL_PARM, then move on. */ | |
14146 | register_local_specialization (argpack, tmpl_parm); | |
14147 | tmpl_parm = TREE_CHAIN (tmpl_parm); | |
14148 | } | |
50bc768d | 14149 | gcc_assert (!spec_parm); |
a723baf1 | 14150 | |
558475f0 MM |
14151 | /* Substitute into the body of the function. */ |
14152 | tsubst_expr (DECL_SAVED_TREE (code_pattern), args, | |
015c2c66 MM |
14153 | tf_warning_or_error, tmpl, |
14154 | /*integral_constant_expression_p=*/false); | |
558475f0 | 14155 | |
6dfbb909 MM |
14156 | /* We don't need the local specializations any more. */ |
14157 | htab_delete (local_specializations); | |
6bbf1598 | 14158 | local_specializations = saved_local_specializations; |
6dfbb909 | 14159 | |
4d6abc1c | 14160 | /* Finish the function. */ |
b2dd096b | 14161 | d = finish_function (0); |
8cd2462c | 14162 | expand_or_defer_fn (d); |
5566b478 MS |
14163 | } |
14164 | ||
971cbc14 MM |
14165 | /* We're not deferring instantiation any more. */ |
14166 | TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0; | |
14167 | ||
31a714f6 MM |
14168 | if (need_push) |
14169 | pop_from_top_level (); | |
414ea4aa | 14170 | |
de22184b | 14171 | out: |
82a98427 | 14172 | input_location = saved_loc; |
7d021397 | 14173 | pop_deferring_access_checks (); |
5566b478 | 14174 | pop_tinst_level (); |
a28e3c7f | 14175 | |
297a5329 JM |
14176 | timevar_pop (TV_PARSE); |
14177 | ||
a28e3c7f MS |
14178 | return d; |
14179 | } | |
5566b478 | 14180 | |
0aafb128 | 14181 | /* Run through the list of templates that we wish we could |
35046a54 KL |
14182 | instantiate, and instantiate any we can. RETRIES is the |
14183 | number of times we retry pending template instantiation. */ | |
0aafb128 | 14184 | |
35046a54 KL |
14185 | void |
14186 | instantiate_pending_templates (int retries) | |
0aafb128 MM |
14187 | { |
14188 | tree *t; | |
46ccf50a | 14189 | tree last = NULL_TREE; |
0aafb128 | 14190 | int reconsider; |
aad626f7 | 14191 | location_t saved_loc = input_location; |
12af7ba3 | 14192 | int saved_in_system_header = in_system_header; |
35046a54 KL |
14193 | |
14194 | /* Instantiating templates may trigger vtable generation. This in turn | |
14195 | may require further template instantiations. We place a limit here | |
14196 | to avoid infinite loop. */ | |
14197 | if (pending_templates && retries >= max_tinst_depth) | |
14198 | { | |
96c993a8 JM |
14199 | tree decl = TREE_VALUE (pending_templates); |
14200 | ||
dee15844 | 14201 | error ("template instantiation depth exceeds maximum of %d" |
96c993a8 JM |
14202 | " instantiating %q+D, possibly from virtual table generation" |
14203 | " (use -ftemplate-depth-NN to increase the maximum)", | |
14204 | max_tinst_depth, decl); | |
14205 | if (TREE_CODE (decl) == FUNCTION_DECL) | |
14206 | /* Pretend that we defined it. */ | |
14207 | DECL_INITIAL (decl) = error_mark_node; | |
35046a54 KL |
14208 | return; |
14209 | } | |
14210 | ||
c8094d83 | 14211 | do |
0aafb128 MM |
14212 | { |
14213 | reconsider = 0; | |
14214 | ||
14215 | t = &pending_templates; | |
14216 | while (*t) | |
14217 | { | |
0aafb128 MM |
14218 | tree instantiation = TREE_VALUE (*t); |
14219 | ||
3ae18eaf | 14220 | reopen_tinst_level (TREE_PURPOSE (*t)); |
0aafb128 | 14221 | |
2f939d94 | 14222 | if (TYPE_P (instantiation)) |
0aafb128 MM |
14223 | { |
14224 | tree fn; | |
14225 | ||
d0f062fb | 14226 | if (!COMPLETE_TYPE_P (instantiation)) |
0aafb128 MM |
14227 | { |
14228 | instantiate_class_template (instantiation); | |
14229 | if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation)) | |
c8094d83 | 14230 | for (fn = TYPE_METHODS (instantiation); |
0aafb128 MM |
14231 | fn; |
14232 | fn = TREE_CHAIN (fn)) | |
14233 | if (! DECL_ARTIFICIAL (fn)) | |
3db45ab5 | 14234 | instantiate_decl (fn, |
eba839f9 MM |
14235 | /*defer_ok=*/0, |
14236 | /*expl_inst_class_mem_p=*/false); | |
d0f062fb | 14237 | if (COMPLETE_TYPE_P (instantiation)) |
35046a54 | 14238 | reconsider = 1; |
0aafb128 MM |
14239 | } |
14240 | ||
d0f062fb | 14241 | if (COMPLETE_TYPE_P (instantiation)) |
0aafb128 MM |
14242 | /* If INSTANTIATION has been instantiated, then we don't |
14243 | need to consider it again in the future. */ | |
14244 | *t = TREE_CHAIN (*t); | |
46ccf50a JM |
14245 | else |
14246 | { | |
14247 | last = *t; | |
14248 | t = &TREE_CHAIN (*t); | |
14249 | } | |
0aafb128 MM |
14250 | } |
14251 | else | |
14252 | { | |
16d53b64 | 14253 | if (!DECL_TEMPLATE_SPECIALIZATION (instantiation) |
0aafb128 MM |
14254 | && !DECL_TEMPLATE_INSTANTIATED (instantiation)) |
14255 | { | |
3db45ab5 | 14256 | instantiation |
eba839f9 MM |
14257 | = instantiate_decl (instantiation, |
14258 | /*defer_ok=*/0, | |
14259 | /*expl_inst_class_mem_p=*/false); | |
0aafb128 | 14260 | if (DECL_TEMPLATE_INSTANTIATED (instantiation)) |
35046a54 | 14261 | reconsider = 1; |
0aafb128 MM |
14262 | } |
14263 | ||
16d53b64 | 14264 | if (DECL_TEMPLATE_SPECIALIZATION (instantiation) |
0aafb128 MM |
14265 | || DECL_TEMPLATE_INSTANTIATED (instantiation)) |
14266 | /* If INSTANTIATION has been instantiated, then we don't | |
14267 | need to consider it again in the future. */ | |
14268 | *t = TREE_CHAIN (*t); | |
46ccf50a JM |
14269 | else |
14270 | { | |
14271 | last = *t; | |
14272 | t = &TREE_CHAIN (*t); | |
14273 | } | |
0aafb128 | 14274 | } |
84e5ca0f NS |
14275 | tinst_depth = 0; |
14276 | current_tinst_level = NULL_TREE; | |
0aafb128 | 14277 | } |
46ccf50a | 14278 | last_pending_template = last; |
c8094d83 | 14279 | } |
0aafb128 MM |
14280 | while (reconsider); |
14281 | ||
aad626f7 | 14282 | input_location = saved_loc; |
12af7ba3 | 14283 | in_system_header = saved_in_system_header; |
0aafb128 MM |
14284 | } |
14285 | ||
fd74ca0b MM |
14286 | /* Substitute ARGVEC into T, which is a list of initializers for |
14287 | either base class or a non-static data member. The TREE_PURPOSEs | |
14288 | are DECLs, and the TREE_VALUEs are the initializer values. Used by | |
14289 | instantiate_decl. */ | |
4393e105 | 14290 | |
824b9a4c | 14291 | static tree |
3a978d72 | 14292 | tsubst_initializer_list (tree t, tree argvec) |
5566b478 | 14293 | { |
2282d28d | 14294 | tree inits = NULL_TREE; |
5566b478 MS |
14295 | |
14296 | for (; t; t = TREE_CHAIN (t)) | |
14297 | { | |
fd74ca0b MM |
14298 | tree decl; |
14299 | tree init; | |
5d80a306 DG |
14300 | tree expanded_bases = NULL_TREE; |
14301 | tree expanded_arguments = NULL_TREE; | |
14302 | int i, len = 1; | |
fd74ca0b | 14303 | |
5d80a306 DG |
14304 | if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION) |
14305 | { | |
14306 | tree expr; | |
14307 | tree arg; | |
14308 | ||
14309 | /* Expand the base class expansion type into separate base | |
14310 | classes. */ | |
14311 | expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec, | |
14312 | tf_warning_or_error, | |
14313 | NULL_TREE); | |
14314 | if (expanded_bases == error_mark_node) | |
14315 | continue; | |
14316 | ||
14317 | /* We'll be building separate TREE_LISTs of arguments for | |
14318 | each base. */ | |
14319 | len = TREE_VEC_LENGTH (expanded_bases); | |
14320 | expanded_arguments = make_tree_vec (len); | |
14321 | for (i = 0; i < len; i++) | |
14322 | TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE; | |
14323 | ||
14324 | /* Build a dummy EXPR_PACK_EXPANSION that will be used to | |
14325 | expand each argument in the TREE_VALUE of t. */ | |
14326 | expr = make_node (EXPR_PACK_EXPANSION); | |
14327 | PACK_EXPANSION_PARAMETER_PACKS (expr) = | |
14328 | PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t)); | |
14329 | ||
14330 | /* Substitute parameter packs into each argument in the | |
14331 | TREE_LIST. */ | |
14332 | in_base_initializer = 1; | |
14333 | for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg)) | |
14334 | { | |
14335 | tree expanded_exprs; | |
14336 | ||
14337 | /* Expand the argument. */ | |
14338 | SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg)); | |
14339 | expanded_exprs = tsubst_pack_expansion (expr, argvec, | |
14340 | tf_warning_or_error, | |
14341 | NULL_TREE); | |
14342 | ||
14343 | /* Prepend each of the expanded expressions to the | |
14344 | corresponding TREE_LIST in EXPANDED_ARGUMENTS. */ | |
14345 | for (i = 0; i < len; i++) | |
14346 | { | |
14347 | TREE_VEC_ELT (expanded_arguments, i) = | |
14348 | tree_cons (NULL_TREE, TREE_VEC_ELT (expanded_exprs, i), | |
14349 | TREE_VEC_ELT (expanded_arguments, i)); | |
14350 | } | |
14351 | } | |
14352 | in_base_initializer = 0; | |
14353 | ||
14354 | /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS, | |
14355 | since we built them backwards. */ | |
14356 | for (i = 0; i < len; i++) | |
14357 | { | |
14358 | TREE_VEC_ELT (expanded_arguments, i) = | |
14359 | nreverse (TREE_VEC_ELT (expanded_arguments, i)); | |
14360 | } | |
14361 | } | |
1f5a253a | 14362 | |
5d80a306 DG |
14363 | for (i = 0; i < len; ++i) |
14364 | { | |
14365 | if (expanded_bases) | |
14366 | { | |
14367 | decl = TREE_VEC_ELT (expanded_bases, i); | |
14368 | decl = expand_member_init (decl); | |
14369 | init = TREE_VEC_ELT (expanded_arguments, i); | |
14370 | } | |
14371 | else | |
14372 | { | |
14373 | decl = tsubst_copy (TREE_PURPOSE (t), argvec, | |
14374 | tf_warning_or_error, NULL_TREE); | |
14375 | ||
14376 | decl = expand_member_init (decl); | |
14377 | if (decl && !DECL_P (decl)) | |
14378 | in_base_initializer = 1; | |
14379 | ||
14380 | init = tsubst_expr (TREE_VALUE (t), argvec, | |
14381 | tf_warning_or_error, NULL_TREE, | |
14382 | /*integral_constant_expression_p=*/false); | |
14383 | in_base_initializer = 0; | |
14384 | } | |
14385 | ||
14386 | if (decl) | |
14387 | { | |
14388 | init = build_tree_list (decl, init); | |
14389 | TREE_CHAIN (init) = inits; | |
14390 | inits = init; | |
14391 | } | |
14392 | } | |
5566b478 | 14393 | } |
2282d28d | 14394 | return inits; |
5566b478 MS |
14395 | } |
14396 | ||
61a127b3 MM |
14397 | /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */ |
14398 | ||
14399 | static void | |
3a978d72 | 14400 | set_current_access_from_decl (tree decl) |
61a127b3 MM |
14401 | { |
14402 | if (TREE_PRIVATE (decl)) | |
14403 | current_access_specifier = access_private_node; | |
14404 | else if (TREE_PROTECTED (decl)) | |
14405 | current_access_specifier = access_protected_node; | |
14406 | else | |
14407 | current_access_specifier = access_public_node; | |
14408 | } | |
14409 | ||
dbfe2124 MM |
14410 | /* Instantiate an enumerated type. TAG is the template type, NEWTAG |
14411 | is the instantiation (which should have been created with | |
14412 | start_enum) and ARGS are the template arguments to use. */ | |
b87692e5 | 14413 | |
dbfe2124 | 14414 | static void |
3a978d72 | 14415 | tsubst_enum (tree tag, tree newtag, tree args) |
b87692e5 | 14416 | { |
dbfe2124 | 14417 | tree e; |
b87692e5 MS |
14418 | |
14419 | for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e)) | |
14420 | { | |
61a127b3 | 14421 | tree value; |
7b6d72fc MM |
14422 | tree decl; |
14423 | ||
14424 | decl = TREE_VALUE (e); | |
61a127b3 MM |
14425 | /* Note that in a template enum, the TREE_VALUE is the |
14426 | CONST_DECL, not the corresponding INTEGER_CST. */ | |
c8094d83 | 14427 | value = tsubst_expr (DECL_INITIAL (decl), |
015c2c66 MM |
14428 | args, tf_warning_or_error, NULL_TREE, |
14429 | /*integral_constant_expression_p=*/true); | |
61a127b3 MM |
14430 | |
14431 | /* Give this enumeration constant the correct access. */ | |
7b6d72fc | 14432 | set_current_access_from_decl (decl); |
61a127b3 MM |
14433 | |
14434 | /* Actually build the enumerator itself. */ | |
c8094d83 | 14435 | build_enumerator (DECL_NAME (decl), value, newtag); |
dbfe2124 | 14436 | } |
b3d5a58b | 14437 | |
219670f1 | 14438 | finish_enum (newtag); |
f31686a3 RH |
14439 | DECL_SOURCE_LOCATION (TYPE_NAME (newtag)) |
14440 | = DECL_SOURCE_LOCATION (TYPE_NAME (tag)); | |
b87692e5 | 14441 | } |
36a117a5 | 14442 | |
1f6e1acc AS |
14443 | /* DECL is a FUNCTION_DECL that is a template specialization. Return |
14444 | its type -- but without substituting the innermost set of template | |
14445 | arguments. So, innermost set of template parameters will appear in | |
5c74d5b0 | 14446 | the type. */ |
1f6e1acc | 14447 | |
c8094d83 | 14448 | tree |
3a978d72 | 14449 | get_mostly_instantiated_function_type (tree decl) |
1f6e1acc | 14450 | { |
1f6e1acc AS |
14451 | tree fn_type; |
14452 | tree tmpl; | |
14453 | tree targs; | |
14454 | tree tparms; | |
14455 | int parm_depth; | |
14456 | ||
14457 | tmpl = most_general_template (DECL_TI_TEMPLATE (decl)); | |
14458 | targs = DECL_TI_ARGS (decl); | |
14459 | tparms = DECL_TEMPLATE_PARMS (tmpl); | |
14460 | parm_depth = TMPL_PARMS_DEPTH (tparms); | |
14461 | ||
14462 | /* There should be as many levels of arguments as there are levels | |
14463 | of parameters. */ | |
50bc768d | 14464 | gcc_assert (parm_depth == TMPL_ARGS_DEPTH (targs)); |
1f6e1acc AS |
14465 | |
14466 | fn_type = TREE_TYPE (tmpl); | |
1f6e1acc AS |
14467 | |
14468 | if (parm_depth == 1) | |
14469 | /* No substitution is necessary. */ | |
14470 | ; | |
14471 | else | |
14472 | { | |
fae6e246 | 14473 | int i, save_access_control; |
1f6e1acc AS |
14474 | tree partial_args; |
14475 | ||
14476 | /* Replace the innermost level of the TARGS with NULL_TREEs to | |
dc957d14 | 14477 | let tsubst know not to substitute for those parameters. */ |
1f6e1acc AS |
14478 | partial_args = make_tree_vec (TREE_VEC_LENGTH (targs)); |
14479 | for (i = 1; i < TMPL_ARGS_DEPTH (targs); ++i) | |
14480 | SET_TMPL_ARGS_LEVEL (partial_args, i, | |
14481 | TMPL_ARGS_LEVEL (targs, i)); | |
14482 | SET_TMPL_ARGS_LEVEL (partial_args, | |
14483 | TMPL_ARGS_DEPTH (targs), | |
14484 | make_tree_vec (DECL_NTPARMS (tmpl))); | |
14485 | ||
fae6e246 RH |
14486 | /* Disable access control as this function is used only during |
14487 | name-mangling. */ | |
14488 | save_access_control = flag_access_control; | |
14489 | flag_access_control = 0; | |
5c74d5b0 | 14490 | |
9579624e | 14491 | ++processing_template_decl; |
1f6e1acc AS |
14492 | /* Now, do the (partial) substitution to figure out the |
14493 | appropriate function type. */ | |
c2ea3a40 | 14494 | fn_type = tsubst (fn_type, partial_args, tf_error, NULL_TREE); |
9579624e | 14495 | --processing_template_decl; |
1f6e1acc AS |
14496 | |
14497 | /* Substitute into the template parameters to obtain the real | |
14498 | innermost set of parameters. This step is important if the | |
14499 | innermost set of template parameters contains value | |
14500 | parameters whose types depend on outer template parameters. */ | |
14501 | TREE_VEC_LENGTH (partial_args)--; | |
c2ea3a40 | 14502 | tparms = tsubst_template_parms (tparms, partial_args, tf_error); |
1f6e1acc | 14503 | |
fae6e246 | 14504 | flag_access_control = save_access_control; |
5c74d5b0 | 14505 | } |
1f6e1acc AS |
14506 | |
14507 | return fn_type; | |
14508 | } | |
669ec2b4 | 14509 | |
cb753e49 | 14510 | /* Return truthvalue if we're processing a template different from |
dc957d14 | 14511 | the last one involved in diagnostics. */ |
cb753e49 | 14512 | int |
3a978d72 | 14513 | problematic_instantiation_changed (void) |
cb753e49 GDR |
14514 | { |
14515 | return last_template_error_tick != tinst_level_tick; | |
14516 | } | |
14517 | ||
14518 | /* Remember current template involved in diagnostics. */ | |
14519 | void | |
3a978d72 | 14520 | record_last_problematic_instantiation (void) |
cb753e49 GDR |
14521 | { |
14522 | last_template_error_tick = tinst_level_tick; | |
14523 | } | |
14524 | ||
14525 | tree | |
3a978d72 | 14526 | current_instantiation (void) |
cb753e49 GDR |
14527 | { |
14528 | return current_tinst_level; | |
14529 | } | |
db3f4e4e NS |
14530 | |
14531 | /* [temp.param] Check that template non-type parm TYPE is of an allowable | |
838dfd8a | 14532 | type. Return zero for ok, nonzero for disallowed. Issue error and |
c2ea3a40 | 14533 | warning messages under control of COMPLAIN. */ |
db3f4e4e NS |
14534 | |
14535 | static int | |
3a978d72 | 14536 | invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain) |
db3f4e4e NS |
14537 | { |
14538 | if (INTEGRAL_TYPE_P (type)) | |
14539 | return 0; | |
14540 | else if (POINTER_TYPE_P (type)) | |
14541 | return 0; | |
a5ac359a | 14542 | else if (TYPE_PTR_TO_MEMBER_P (type)) |
db3f4e4e | 14543 | return 0; |
db3f4e4e NS |
14544 | else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM) |
14545 | return 0; | |
14546 | else if (TREE_CODE (type) == TYPENAME_TYPE) | |
14547 | return 0; | |
c8094d83 | 14548 | |
c2ea3a40 | 14549 | if (complain & tf_error) |
0f51ccfc | 14550 | error ("%q#T is not a valid type for a template constant parameter", type); |
db3f4e4e NS |
14551 | return 1; |
14552 | } | |
e2500fed | 14553 | |
5552b43c MM |
14554 | /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type]. |
14555 | Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/ | |
1fb3244a | 14556 | |
5552b43c MM |
14557 | static bool |
14558 | dependent_type_p_r (tree type) | |
1fb3244a MM |
14559 | { |
14560 | tree scope; | |
14561 | ||
1fb3244a MM |
14562 | /* [temp.dep.type] |
14563 | ||
14564 | A type is dependent if it is: | |
14565 | ||
6615c446 JO |
14566 | -- a template parameter. Template template parameters are types |
14567 | for us (since TYPE_P holds true for them) so we handle | |
14568 | them here. */ | |
c8094d83 | 14569 | if (TREE_CODE (type) == TEMPLATE_TYPE_PARM |
6bf92cb6 | 14570 | || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM) |
1fb3244a MM |
14571 | return true; |
14572 | /* -- a qualified-id with a nested-name-specifier which contains a | |
0cbd7506 | 14573 | class-name that names a dependent type or whose unqualified-id |
1fb3244a MM |
14574 | names a dependent type. */ |
14575 | if (TREE_CODE (type) == TYPENAME_TYPE) | |
14576 | return true; | |
14577 | /* -- a cv-qualified type where the cv-unqualified type is | |
0cbd7506 | 14578 | dependent. */ |
1fb3244a MM |
14579 | type = TYPE_MAIN_VARIANT (type); |
14580 | /* -- a compound type constructed from any dependent type. */ | |
a5ac359a | 14581 | if (TYPE_PTR_TO_MEMBER_P (type)) |
1fb3244a | 14582 | return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type)) |
c8094d83 | 14583 | || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE |
1fb3244a MM |
14584 | (type))); |
14585 | else if (TREE_CODE (type) == POINTER_TYPE | |
14586 | || TREE_CODE (type) == REFERENCE_TYPE) | |
14587 | return dependent_type_p (TREE_TYPE (type)); | |
14588 | else if (TREE_CODE (type) == FUNCTION_TYPE | |
14589 | || TREE_CODE (type) == METHOD_TYPE) | |
14590 | { | |
14591 | tree arg_type; | |
14592 | ||
14593 | if (dependent_type_p (TREE_TYPE (type))) | |
14594 | return true; | |
c8094d83 MS |
14595 | for (arg_type = TYPE_ARG_TYPES (type); |
14596 | arg_type; | |
1fb3244a MM |
14597 | arg_type = TREE_CHAIN (arg_type)) |
14598 | if (dependent_type_p (TREE_VALUE (arg_type))) | |
14599 | return true; | |
14600 | return false; | |
14601 | } | |
14602 | /* -- an array type constructed from any dependent type or whose | |
0cbd7506 | 14603 | size is specified by a constant expression that is |
1fb3244a MM |
14604 | value-dependent. */ |
14605 | if (TREE_CODE (type) == ARRAY_TYPE) | |
14606 | { | |
14607 | if (TYPE_DOMAIN (type) | |
c8094d83 | 14608 | && ((value_dependent_expression_p |
1fb3244a MM |
14609 | (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))) |
14610 | || (type_dependent_expression_p | |
14611 | (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))))) | |
14612 | return true; | |
14613 | return dependent_type_p (TREE_TYPE (type)); | |
14614 | } | |
c8094d83 | 14615 | |
1fb3244a | 14616 | /* -- a template-id in which either the template name is a template |
86306a6b NS |
14617 | parameter ... */ |
14618 | if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM) | |
1fb3244a | 14619 | return true; |
86306a6b | 14620 | /* ... or any of the template arguments is a dependent type or |
04c06002 | 14621 | an expression that is type-dependent or value-dependent. */ |
86306a6b | 14622 | else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type) |
c8094d83 | 14623 | && (any_dependent_template_arguments_p |
7e99327d | 14624 | (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type))))) |
1fb3244a | 14625 | return true; |
c8094d83 | 14626 | |
1fb3244a MM |
14627 | /* All TYPEOF_TYPEs are dependent; if the argument of the `typeof' |
14628 | expression is not type-dependent, then it should already been | |
14629 | have resolved. */ | |
14630 | if (TREE_CODE (type) == TYPEOF_TYPE) | |
14631 | return true; | |
c8094d83 | 14632 | |
5d80a306 DG |
14633 | /* A template argument pack is dependent if any of its packed |
14634 | arguments are. */ | |
14635 | if (TREE_CODE (type) == TYPE_ARGUMENT_PACK) | |
14636 | { | |
14637 | tree args = ARGUMENT_PACK_ARGS (type); | |
14638 | int i, len = TREE_VEC_LENGTH (args); | |
14639 | for (i = 0; i < len; ++i) | |
14640 | if (dependent_template_arg_p (TREE_VEC_ELT (args, i))) | |
14641 | return true; | |
14642 | } | |
14643 | ||
14644 | /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must | |
14645 | be template parameters. */ | |
14646 | if (TREE_CODE (type) == TYPE_PACK_EXPANSION) | |
14647 | return true; | |
14648 | ||
1fb3244a MM |
14649 | /* The standard does not specifically mention types that are local |
14650 | to template functions or local classes, but they should be | |
14651 | considered dependent too. For example: | |
14652 | ||
c8094d83 | 14653 | template <int I> void f() { |
0cbd7506 | 14654 | enum E { a = I }; |
1fb3244a MM |
14655 | S<sizeof (E)> s; |
14656 | } | |
14657 | ||
14658 | The size of `E' cannot be known until the value of `I' has been | |
14659 | determined. Therefore, `E' must be considered dependent. */ | |
14660 | scope = TYPE_CONTEXT (type); | |
14661 | if (scope && TYPE_P (scope)) | |
14662 | return dependent_type_p (scope); | |
14663 | else if (scope && TREE_CODE (scope) == FUNCTION_DECL) | |
14664 | return type_dependent_expression_p (scope); | |
14665 | ||
14666 | /* Other types are non-dependent. */ | |
14667 | return false; | |
14668 | } | |
14669 | ||
5552b43c MM |
14670 | /* Returns TRUE if TYPE is dependent, in the sense of |
14671 | [temp.dep.type]. */ | |
14672 | ||
14673 | bool | |
14674 | dependent_type_p (tree type) | |
14675 | { | |
14676 | /* If there are no template parameters in scope, then there can't be | |
14677 | any dependent types. */ | |
14678 | if (!processing_template_decl) | |
e7e93965 MM |
14679 | { |
14680 | /* If we are not processing a template, then nobody should be | |
14681 | providing us with a dependent type. */ | |
14682 | gcc_assert (type); | |
14683 | gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM); | |
14684 | return false; | |
14685 | } | |
5552b43c MM |
14686 | |
14687 | /* If the type is NULL, we have not computed a type for the entity | |
14688 | in question; in that case, the type is dependent. */ | |
14689 | if (!type) | |
14690 | return true; | |
14691 | ||
14692 | /* Erroneous types can be considered non-dependent. */ | |
14693 | if (type == error_mark_node) | |
14694 | return false; | |
14695 | ||
14696 | /* If we have not already computed the appropriate value for TYPE, | |
14697 | do so now. */ | |
14698 | if (!TYPE_DEPENDENT_P_VALID (type)) | |
14699 | { | |
14700 | TYPE_DEPENDENT_P (type) = dependent_type_p_r (type); | |
14701 | TYPE_DEPENDENT_P_VALID (type) = 1; | |
14702 | } | |
14703 | ||
14704 | return TYPE_DEPENDENT_P (type); | |
14705 | } | |
14706 | ||
8d83f792 MM |
14707 | /* Returns TRUE if EXPRESSION is dependent, according to CRITERION. */ |
14708 | ||
14709 | static bool | |
14710 | dependent_scope_ref_p (tree expression, bool criterion (tree)) | |
14711 | { | |
14712 | tree scope; | |
14713 | tree name; | |
14714 | ||
50bc768d | 14715 | gcc_assert (TREE_CODE (expression) == SCOPE_REF); |
8d83f792 MM |
14716 | |
14717 | if (!TYPE_P (TREE_OPERAND (expression, 0))) | |
14718 | return true; | |
14719 | ||
14720 | scope = TREE_OPERAND (expression, 0); | |
14721 | name = TREE_OPERAND (expression, 1); | |
14722 | ||
14723 | /* [temp.dep.expr] | |
14724 | ||
14725 | An id-expression is type-dependent if it contains a | |
14726 | nested-name-specifier that contains a class-name that names a | |
14727 | dependent type. */ | |
14728 | /* The suggested resolution to Core Issue 2 implies that if the | |
14729 | qualifying type is the current class, then we must peek | |
14730 | inside it. */ | |
c8094d83 | 14731 | if (DECL_P (name) |
8d83f792 MM |
14732 | && currently_open_class (scope) |
14733 | && !criterion (name)) | |
14734 | return false; | |
14735 | if (dependent_type_p (scope)) | |
14736 | return true; | |
14737 | ||
14738 | return false; | |
14739 | } | |
14740 | ||
20929c7f | 14741 | /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of |
fe89d797 MM |
14742 | [temp.dep.constexpr]. EXPRESSION is already known to be a constant |
14743 | expression. */ | |
1fb3244a | 14744 | |
14d22dd6 | 14745 | bool |
1fb3244a MM |
14746 | value_dependent_expression_p (tree expression) |
14747 | { | |
14748 | if (!processing_template_decl) | |
14749 | return false; | |
14750 | ||
14751 | /* A name declared with a dependent type. */ | |
7416ab02 | 14752 | if (DECL_P (expression) && type_dependent_expression_p (expression)) |
1fb3244a | 14753 | return true; |
c8094d83 | 14754 | |
7416ab02 NS |
14755 | switch (TREE_CODE (expression)) |
14756 | { | |
14757 | case IDENTIFIER_NODE: | |
14758 | /* A name that has not been looked up -- must be dependent. */ | |
14759 | return true; | |
14760 | ||
14761 | case TEMPLATE_PARM_INDEX: | |
14762 | /* A non-type template parm. */ | |
14763 | return true; | |
14764 | ||
14765 | case CONST_DECL: | |
14766 | /* A non-type template parm. */ | |
14767 | if (DECL_TEMPLATE_PARM_P (expression)) | |
d17811fd | 14768 | return true; |
7416ab02 | 14769 | return false; |
d36d5600 | 14770 | |
7416ab02 | 14771 | case VAR_DECL: |
c8094d83 | 14772 | /* A constant with integral or enumeration type and is initialized |
0cbd7506 | 14773 | with an expression that is value-dependent. */ |
7416ab02 NS |
14774 | if (DECL_INITIAL (expression) |
14775 | && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (expression)) | |
14776 | && value_dependent_expression_p (DECL_INITIAL (expression))) | |
14777 | return true; | |
14778 | return false; | |
14779 | ||
14780 | case DYNAMIC_CAST_EXPR: | |
14781 | case STATIC_CAST_EXPR: | |
14782 | case CONST_CAST_EXPR: | |
14783 | case REINTERPRET_CAST_EXPR: | |
14784 | case CAST_EXPR: | |
14785 | /* These expressions are value-dependent if the type to which | |
0cbd7506 MS |
14786 | the cast occurs is dependent or the expression being casted |
14787 | is value-dependent. */ | |
7416ab02 NS |
14788 | { |
14789 | tree type = TREE_TYPE (expression); | |
c8094d83 | 14790 | |
7416ab02 NS |
14791 | if (dependent_type_p (type)) |
14792 | return true; | |
c8094d83 | 14793 | |
7416ab02 NS |
14794 | /* A functional cast has a list of operands. */ |
14795 | expression = TREE_OPERAND (expression, 0); | |
14796 | if (!expression) | |
14797 | { | |
14798 | /* If there are no operands, it must be an expression such | |
14799 | as "int()". This should not happen for aggregate types | |
14800 | because it would form non-constant expressions. */ | |
14801 | gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type)); | |
c8094d83 | 14802 | |
7416ab02 NS |
14803 | return false; |
14804 | } | |
c8094d83 | 14805 | |
7416ab02 NS |
14806 | if (TREE_CODE (expression) == TREE_LIST) |
14807 | { | |
14808 | for (; expression; expression = TREE_CHAIN (expression)) | |
d17811fd MM |
14809 | if (value_dependent_expression_p (TREE_VALUE (expression))) |
14810 | return true; | |
7416ab02 NS |
14811 | return false; |
14812 | } | |
c8094d83 | 14813 | |
d17811fd | 14814 | return value_dependent_expression_p (expression); |
7416ab02 | 14815 | } |
c8094d83 | 14816 | |
7416ab02 NS |
14817 | case SIZEOF_EXPR: |
14818 | case ALIGNOF_EXPR: | |
14819 | /* A `sizeof' expression is value-dependent if the operand is | |
5d80a306 | 14820 | type-dependent or is a pack expansion. */ |
d17811fd | 14821 | expression = TREE_OPERAND (expression, 0); |
5d80a306 DG |
14822 | if (PACK_EXPANSION_P (expression)) |
14823 | return true; | |
14824 | else if (TYPE_P (expression)) | |
d17811fd MM |
14825 | return dependent_type_p (expression); |
14826 | return type_dependent_expression_p (expression); | |
100d337a | 14827 | |
7416ab02 NS |
14828 | case SCOPE_REF: |
14829 | return dependent_scope_ref_p (expression, value_dependent_expression_p); | |
100d337a | 14830 | |
7416ab02 NS |
14831 | case COMPONENT_REF: |
14832 | return (value_dependent_expression_p (TREE_OPERAND (expression, 0)) | |
14833 | || value_dependent_expression_p (TREE_OPERAND (expression, 1))); | |
14834 | ||
14835 | case CALL_EXPR: | |
fe89d797 MM |
14836 | /* A CALL_EXPR may appear in a constant expression if it is a |
14837 | call to a builtin function, e.g., __builtin_constant_p. All | |
14838 | such calls are value-dependent. */ | |
14839 | return true; | |
7416ab02 | 14840 | |
5d80a306 DG |
14841 | case NONTYPE_ARGUMENT_PACK: |
14842 | /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument | |
14843 | is value-dependent. */ | |
14844 | { | |
14845 | tree values = ARGUMENT_PACK_ARGS (expression); | |
14846 | int i, len = TREE_VEC_LENGTH (values); | |
14847 | ||
14848 | for (i = 0; i < len; ++i) | |
14849 | if (value_dependent_expression_p (TREE_VEC_ELT (values, i))) | |
14850 | return true; | |
14851 | ||
14852 | return false; | |
14853 | } | |
14854 | ||
7416ab02 NS |
14855 | default: |
14856 | /* A constant expression is value-dependent if any subexpression is | |
0cbd7506 | 14857 | value-dependent. */ |
1fb3244a MM |
14858 | switch (TREE_CODE_CLASS (TREE_CODE (expression))) |
14859 | { | |
7416ab02 | 14860 | case tcc_reference: |
6615c446 | 14861 | case tcc_unary: |
c8094d83 | 14862 | return (value_dependent_expression_p |
1fb3244a | 14863 | (TREE_OPERAND (expression, 0))); |
c8094d83 | 14864 | |
6615c446 JO |
14865 | case tcc_comparison: |
14866 | case tcc_binary: | |
c8094d83 | 14867 | return ((value_dependent_expression_p |
1fb3244a | 14868 | (TREE_OPERAND (expression, 0))) |
c8094d83 | 14869 | || (value_dependent_expression_p |
1fb3244a | 14870 | (TREE_OPERAND (expression, 1)))); |
c8094d83 | 14871 | |
6615c446 | 14872 | case tcc_expression: |
5039610b | 14873 | case tcc_vl_exp: |
1fb3244a MM |
14874 | { |
14875 | int i; | |
5039610b | 14876 | for (i = 0; i < TREE_OPERAND_LENGTH (expression); ++i) |
14d22dd6 MM |
14877 | /* In some cases, some of the operands may be missing. |
14878 | (For example, in the case of PREDECREMENT_EXPR, the | |
14879 | amount to increment by may be missing.) That doesn't | |
14880 | make the expression dependent. */ | |
14881 | if (TREE_OPERAND (expression, i) | |
14882 | && (value_dependent_expression_p | |
14883 | (TREE_OPERAND (expression, i)))) | |
1fb3244a MM |
14884 | return true; |
14885 | return false; | |
14886 | } | |
c8094d83 | 14887 | |
6615c446 | 14888 | default: |
7416ab02 | 14889 | break; |
1fb3244a MM |
14890 | } |
14891 | } | |
c8094d83 | 14892 | |
1fb3244a MM |
14893 | /* The expression is not value-dependent. */ |
14894 | return false; | |
14895 | } | |
14896 | ||
14897 | /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of | |
14898 | [temp.dep.expr]. */ | |
14899 | ||
14900 | bool | |
3a978d72 | 14901 | type_dependent_expression_p (tree expression) |
1fb3244a MM |
14902 | { |
14903 | if (!processing_template_decl) | |
14904 | return false; | |
14905 | ||
7efa3e22 NS |
14906 | if (expression == error_mark_node) |
14907 | return false; | |
10b1d5e7 MM |
14908 | |
14909 | /* An unresolved name is always dependent. */ | |
b01e6d2b JM |
14910 | if (TREE_CODE (expression) == IDENTIFIER_NODE |
14911 | || TREE_CODE (expression) == USING_DECL) | |
10b1d5e7 | 14912 | return true; |
c8094d83 | 14913 | |
1fb3244a MM |
14914 | /* Some expression forms are never type-dependent. */ |
14915 | if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR | |
14916 | || TREE_CODE (expression) == SIZEOF_EXPR | |
14917 | || TREE_CODE (expression) == ALIGNOF_EXPR | |
14918 | || TREE_CODE (expression) == TYPEID_EXPR | |
14919 | || TREE_CODE (expression) == DELETE_EXPR | |
14920 | || TREE_CODE (expression) == VEC_DELETE_EXPR | |
14921 | || TREE_CODE (expression) == THROW_EXPR) | |
14922 | return false; | |
14923 | ||
14924 | /* The types of these expressions depends only on the type to which | |
14925 | the cast occurs. */ | |
14926 | if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR | |
14927 | || TREE_CODE (expression) == STATIC_CAST_EXPR | |
14928 | || TREE_CODE (expression) == CONST_CAST_EXPR | |
14929 | || TREE_CODE (expression) == REINTERPRET_CAST_EXPR | |
14930 | || TREE_CODE (expression) == CAST_EXPR) | |
14931 | return dependent_type_p (TREE_TYPE (expression)); | |
d17811fd | 14932 | |
1fb3244a MM |
14933 | /* The types of these expressions depends only on the type created |
14934 | by the expression. */ | |
d17811fd MM |
14935 | if (TREE_CODE (expression) == NEW_EXPR |
14936 | || TREE_CODE (expression) == VEC_NEW_EXPR) | |
09d2f85f KL |
14937 | { |
14938 | /* For NEW_EXPR tree nodes created inside a template, either | |
14939 | the object type itself or a TREE_LIST may appear as the | |
14940 | operand 1. */ | |
14941 | tree type = TREE_OPERAND (expression, 1); | |
14942 | if (TREE_CODE (type) == TREE_LIST) | |
14943 | /* This is an array type. We need to check array dimensions | |
14944 | as well. */ | |
14945 | return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type))) | |
14946 | || value_dependent_expression_p | |
14947 | (TREE_OPERAND (TREE_VALUE (type), 1)); | |
14948 | else | |
14949 | return dependent_type_p (type); | |
14950 | } | |
1fb3244a | 14951 | |
5a57f1b2 JM |
14952 | if (TREE_CODE (expression) == SCOPE_REF |
14953 | && dependent_scope_ref_p (expression, | |
14954 | type_dependent_expression_p)) | |
14955 | return true; | |
14956 | ||
14957 | if (TREE_CODE (expression) == FUNCTION_DECL | |
14958 | && DECL_LANG_SPECIFIC (expression) | |
14959 | && DECL_TEMPLATE_INFO (expression) | |
14960 | && (any_dependent_template_arguments_p | |
14961 | (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression))))) | |
14962 | return true; | |
14963 | ||
14964 | if (TREE_CODE (expression) == TEMPLATE_DECL | |
14965 | && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression)) | |
14966 | return false; | |
14967 | ||
d17811fd MM |
14968 | if (TREE_TYPE (expression) == unknown_type_node) |
14969 | { | |
14970 | if (TREE_CODE (expression) == ADDR_EXPR) | |
14971 | return type_dependent_expression_p (TREE_OPERAND (expression, 0)); | |
c353b8e3 MM |
14972 | if (TREE_CODE (expression) == COMPONENT_REF |
14973 | || TREE_CODE (expression) == OFFSET_REF) | |
6cb89308 NS |
14974 | { |
14975 | if (type_dependent_expression_p (TREE_OPERAND (expression, 0))) | |
14976 | return true; | |
14977 | expression = TREE_OPERAND (expression, 1); | |
14978 | if (TREE_CODE (expression) == IDENTIFIER_NODE) | |
14979 | return false; | |
14980 | } | |
3601f003 KL |
14981 | /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */ |
14982 | if (TREE_CODE (expression) == SCOPE_REF) | |
14983 | return false; | |
c8094d83 | 14984 | |
d17811fd MM |
14985 | if (TREE_CODE (expression) == BASELINK) |
14986 | expression = BASELINK_FUNCTIONS (expression); | |
c8094d83 | 14987 | |
d17811fd MM |
14988 | if (TREE_CODE (expression) == TEMPLATE_ID_EXPR) |
14989 | { | |
ee3071ef NS |
14990 | if (any_dependent_template_arguments_p |
14991 | (TREE_OPERAND (expression, 1))) | |
d17811fd MM |
14992 | return true; |
14993 | expression = TREE_OPERAND (expression, 0); | |
14994 | } | |
b207d6e2 MM |
14995 | gcc_assert (TREE_CODE (expression) == OVERLOAD |
14996 | || TREE_CODE (expression) == FUNCTION_DECL); | |
c8094d83 | 14997 | |
315fb5db | 14998 | while (expression) |
d17811fd | 14999 | { |
315fb5db NS |
15000 | if (type_dependent_expression_p (OVL_CURRENT (expression))) |
15001 | return true; | |
15002 | expression = OVL_NEXT (expression); | |
d17811fd | 15003 | } |
315fb5db | 15004 | return false; |
d17811fd | 15005 | } |
c8094d83 | 15006 | |
3ce5fa4f | 15007 | gcc_assert (TREE_CODE (expression) != TYPE_DECL); |
3db45ab5 | 15008 | |
1fb3244a MM |
15009 | return (dependent_type_p (TREE_TYPE (expression))); |
15010 | } | |
15011 | ||
d17811fd MM |
15012 | /* Returns TRUE if ARGS (a TREE_LIST of arguments to a function call) |
15013 | contains a type-dependent expression. */ | |
1fb3244a MM |
15014 | |
15015 | bool | |
d17811fd MM |
15016 | any_type_dependent_arguments_p (tree args) |
15017 | { | |
15018 | while (args) | |
15019 | { | |
7efa3e22 NS |
15020 | tree arg = TREE_VALUE (args); |
15021 | ||
15022 | if (type_dependent_expression_p (arg)) | |
d17811fd MM |
15023 | return true; |
15024 | args = TREE_CHAIN (args); | |
15025 | } | |
15026 | return false; | |
15027 | } | |
15028 | ||
15029 | /* Returns TRUE if the ARG (a template argument) is dependent. */ | |
15030 | ||
5d80a306 | 15031 | bool |
1fb3244a MM |
15032 | dependent_template_arg_p (tree arg) |
15033 | { | |
15034 | if (!processing_template_decl) | |
15035 | return false; | |
15036 | ||
15037 | if (TREE_CODE (arg) == TEMPLATE_DECL | |
15038 | || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM) | |
15039 | return dependent_template_p (arg); | |
5d80a306 DG |
15040 | else if (ARGUMENT_PACK_P (arg)) |
15041 | { | |
15042 | tree args = ARGUMENT_PACK_ARGS (arg); | |
15043 | int i, len = TREE_VEC_LENGTH (args); | |
15044 | for (i = 0; i < len; ++i) | |
15045 | { | |
15046 | if (dependent_template_arg_p (TREE_VEC_ELT (args, i))) | |
15047 | return true; | |
15048 | } | |
15049 | ||
15050 | return false; | |
15051 | } | |
1fb3244a MM |
15052 | else if (TYPE_P (arg)) |
15053 | return dependent_type_p (arg); | |
15054 | else | |
15055 | return (type_dependent_expression_p (arg) | |
15056 | || value_dependent_expression_p (arg)); | |
15057 | } | |
15058 | ||
06d40de8 DG |
15059 | /* Returns true if ARGS (a collection of template arguments) contains |
15060 | any types that require structural equality testing. */ | |
15061 | ||
15062 | bool | |
15063 | any_template_arguments_need_structural_equality_p (tree args) | |
15064 | { | |
15065 | int i; | |
15066 | int j; | |
15067 | ||
15068 | if (!args) | |
15069 | return false; | |
15070 | if (args == error_mark_node) | |
15071 | return true; | |
15072 | ||
15073 | for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i) | |
15074 | { | |
15075 | tree level = TMPL_ARGS_LEVEL (args, i + 1); | |
15076 | for (j = 0; j < TREE_VEC_LENGTH (level); ++j) | |
15077 | { | |
15078 | tree arg = TREE_VEC_ELT (level, j); | |
5d80a306 DG |
15079 | tree packed_args = NULL_TREE; |
15080 | int k, len = 1; | |
15081 | ||
15082 | if (ARGUMENT_PACK_P (arg)) | |
15083 | { | |
15084 | /* Look inside the argument pack. */ | |
15085 | packed_args = ARGUMENT_PACK_ARGS (arg); | |
15086 | len = TREE_VEC_LENGTH (packed_args); | |
15087 | } | |
15088 | ||
15089 | for (k = 0; k < len; ++k) | |
15090 | { | |
15091 | if (packed_args) | |
15092 | arg = TREE_VEC_ELT (packed_args, k); | |
15093 | ||
15094 | if (error_operand_p (arg)) | |
15095 | return true; | |
15096 | else if (TREE_CODE (arg) == TEMPLATE_DECL | |
15097 | || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM) | |
15098 | continue; | |
15099 | else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg)) | |
15100 | return true; | |
15101 | else if (!TYPE_P (arg) && TREE_TYPE (arg) | |
15102 | && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg))) | |
15103 | return true; | |
15104 | } | |
06d40de8 DG |
15105 | } |
15106 | } | |
15107 | ||
15108 | return false; | |
15109 | } | |
15110 | ||
d17811fd MM |
15111 | /* Returns true if ARGS (a collection of template arguments) contains |
15112 | any dependent arguments. */ | |
1fb3244a | 15113 | |
d17811fd MM |
15114 | bool |
15115 | any_dependent_template_arguments_p (tree args) | |
1fb3244a | 15116 | { |
bf12d54d | 15117 | int i; |
c353b8e3 MM |
15118 | int j; |
15119 | ||
d17811fd MM |
15120 | if (!args) |
15121 | return false; | |
7e497d0c VR |
15122 | if (args == error_mark_node) |
15123 | return true; | |
d17811fd | 15124 | |
c353b8e3 MM |
15125 | for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i) |
15126 | { | |
15127 | tree level = TMPL_ARGS_LEVEL (args, i + 1); | |
15128 | for (j = 0; j < TREE_VEC_LENGTH (level); ++j) | |
15129 | if (dependent_template_arg_p (TREE_VEC_ELT (level, j))) | |
15130 | return true; | |
15131 | } | |
1fb3244a | 15132 | |
1fb3244a MM |
15133 | return false; |
15134 | } | |
15135 | ||
15136 | /* Returns TRUE if the template TMPL is dependent. */ | |
15137 | ||
15138 | bool | |
15139 | dependent_template_p (tree tmpl) | |
15140 | { | |
b95cc51a MM |
15141 | if (TREE_CODE (tmpl) == OVERLOAD) |
15142 | { | |
15143 | while (tmpl) | |
15144 | { | |
15145 | if (dependent_template_p (OVL_FUNCTION (tmpl))) | |
15146 | return true; | |
15147 | tmpl = OVL_CHAIN (tmpl); | |
15148 | } | |
15149 | return false; | |
15150 | } | |
15151 | ||
1fb3244a MM |
15152 | /* Template template parameters are dependent. */ |
15153 | if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl) | |
15154 | || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM) | |
15155 | return true; | |
27ab0504 | 15156 | /* So are names that have not been looked up. */ |
acccf788 MM |
15157 | if (TREE_CODE (tmpl) == SCOPE_REF |
15158 | || TREE_CODE (tmpl) == IDENTIFIER_NODE) | |
d17811fd | 15159 | return true; |
1fb3244a MM |
15160 | /* So are member templates of dependent classes. */ |
15161 | if (TYPE_P (CP_DECL_CONTEXT (tmpl))) | |
15162 | return dependent_type_p (DECL_CONTEXT (tmpl)); | |
15163 | return false; | |
15164 | } | |
15165 | ||
d17811fd MM |
15166 | /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */ |
15167 | ||
15168 | bool | |
15169 | dependent_template_id_p (tree tmpl, tree args) | |
15170 | { | |
15171 | return (dependent_template_p (tmpl) | |
15172 | || any_dependent_template_arguments_p (args)); | |
15173 | } | |
15174 | ||
14d22dd6 MM |
15175 | /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the |
15176 | TYPENAME_TYPE corresponds. Returns ERROR_MARK_NODE if no such TYPE | |
15177 | can be found. Note that this function peers inside uninstantiated | |
15178 | templates and therefore should be used only in extremely limited | |
dda04398 | 15179 | situations. ONLY_CURRENT_P restricts this peering to the currently |
c51940a2 | 15180 | open classes hierarchy (which is required when comparing types). */ |
14d22dd6 MM |
15181 | |
15182 | tree | |
15183 | resolve_typename_type (tree type, bool only_current_p) | |
15184 | { | |
15185 | tree scope; | |
15186 | tree name; | |
15187 | tree decl; | |
15188 | int quals; | |
4514aa8c | 15189 | tree pushed_scope; |
14d22dd6 | 15190 | |
50bc768d | 15191 | gcc_assert (TREE_CODE (type) == TYPENAME_TYPE); |
14d22dd6 MM |
15192 | |
15193 | scope = TYPE_CONTEXT (type); | |
15194 | name = TYPE_IDENTIFIER (type); | |
15195 | ||
15196 | /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve | |
15197 | it first before we can figure out what NAME refers to. */ | |
15198 | if (TREE_CODE (scope) == TYPENAME_TYPE) | |
15199 | scope = resolve_typename_type (scope, only_current_p); | |
15200 | /* If we don't know what SCOPE refers to, then we cannot resolve the | |
15201 | TYPENAME_TYPE. */ | |
15202 | if (scope == error_mark_node || TREE_CODE (scope) == TYPENAME_TYPE) | |
15203 | return error_mark_node; | |
15204 | /* If the SCOPE is a template type parameter, we have no way of | |
15205 | resolving the name. */ | |
15206 | if (TREE_CODE (scope) == TEMPLATE_TYPE_PARM) | |
15207 | return type; | |
15208 | /* If the SCOPE is not the current instantiation, there's no reason | |
15209 | to look inside it. */ | |
15210 | if (only_current_p && !currently_open_class (scope)) | |
15211 | return error_mark_node; | |
ca099ac8 MM |
15212 | /* If SCOPE is a partial instantiation, it will not have a valid |
15213 | TYPE_FIELDS list, so use the original template. */ | |
353b4fc0 | 15214 | scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope); |
14d22dd6 MM |
15215 | /* Enter the SCOPE so that name lookup will be resolved as if we |
15216 | were in the class definition. In particular, SCOPE will no | |
15217 | longer be considered a dependent type. */ | |
4514aa8c | 15218 | pushed_scope = push_scope (scope); |
14d22dd6 | 15219 | /* Look up the declaration. */ |
86ac0575 | 15220 | decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true); |
14d22dd6 MM |
15221 | /* Obtain the set of qualifiers applied to the TYPE. */ |
15222 | quals = cp_type_quals (type); | |
15223 | /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to | |
15224 | find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */ | |
15225 | if (!decl) | |
15226 | type = error_mark_node; | |
15227 | else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == IDENTIFIER_NODE | |
15228 | && TREE_CODE (decl) == TYPE_DECL) | |
15229 | type = TREE_TYPE (decl); | |
15230 | else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == TEMPLATE_ID_EXPR | |
15231 | && DECL_CLASS_TEMPLATE_P (decl)) | |
15232 | { | |
15233 | tree tmpl; | |
15234 | tree args; | |
15235 | /* Obtain the template and the arguments. */ | |
15236 | tmpl = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 0); | |
15237 | args = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 1); | |
15238 | /* Instantiate the template. */ | |
15239 | type = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE, | |
42eaed49 | 15240 | /*entering_scope=*/0, tf_error | tf_user); |
14d22dd6 MM |
15241 | } |
15242 | else | |
15243 | type = error_mark_node; | |
15244 | /* Qualify the resulting type. */ | |
15245 | if (type != error_mark_node && quals) | |
15246 | type = cp_build_qualified_type (type, quals); | |
15247 | /* Leave the SCOPE. */ | |
4514aa8c NS |
15248 | if (pushed_scope) |
15249 | pop_scope (pushed_scope); | |
14d22dd6 MM |
15250 | |
15251 | return type; | |
15252 | } | |
15253 | ||
d17811fd MM |
15254 | /* EXPR is an expression which is not type-dependent. Return a proxy |
15255 | for EXPR that can be used to compute the types of larger | |
15256 | expressions containing EXPR. */ | |
15257 | ||
15258 | tree | |
15259 | build_non_dependent_expr (tree expr) | |
15260 | { | |
0deb916c MM |
15261 | tree inner_expr; |
15262 | ||
c8094d83 | 15263 | /* Preserve null pointer constants so that the type of things like |
d17811fd MM |
15264 | "p == 0" where "p" is a pointer can be determined. */ |
15265 | if (null_ptr_cst_p (expr)) | |
15266 | return expr; | |
15267 | /* Preserve OVERLOADs; the functions must be available to resolve | |
15268 | types. */ | |
c497db75 AO |
15269 | inner_expr = expr; |
15270 | if (TREE_CODE (inner_expr) == ADDR_EXPR) | |
15271 | inner_expr = TREE_OPERAND (inner_expr, 0); | |
15272 | if (TREE_CODE (inner_expr) == COMPONENT_REF) | |
15273 | inner_expr = TREE_OPERAND (inner_expr, 1); | |
2226e997 | 15274 | if (is_overloaded_fn (inner_expr) |
6439fffd | 15275 | || TREE_CODE (inner_expr) == OFFSET_REF) |
d17811fd | 15276 | return expr; |
5ae9ba3e MM |
15277 | /* There is no need to return a proxy for a variable. */ |
15278 | if (TREE_CODE (expr) == VAR_DECL) | |
15279 | return expr; | |
7433e6d4 MM |
15280 | /* Preserve string constants; conversions from string constants to |
15281 | "char *" are allowed, even though normally a "const char *" | |
15282 | cannot be used to initialize a "char *". */ | |
15283 | if (TREE_CODE (expr) == STRING_CST) | |
15284 | return expr; | |
b7c707d1 MM |
15285 | /* Preserve arithmetic constants, as an optimization -- there is no |
15286 | reason to create a new node. */ | |
15287 | if (TREE_CODE (expr) == INTEGER_CST || TREE_CODE (expr) == REAL_CST) | |
15288 | return expr; | |
9b7be7b5 MM |
15289 | /* Preserve THROW_EXPRs -- all throw-expressions have type "void". |
15290 | There is at least one place where we want to know that a | |
15291 | particular expression is a throw-expression: when checking a ?: | |
15292 | expression, there are special rules if the second or third | |
878cbb73 | 15293 | argument is a throw-expression. */ |
9b7be7b5 MM |
15294 | if (TREE_CODE (expr) == THROW_EXPR) |
15295 | return expr; | |
47d4c811 NS |
15296 | |
15297 | if (TREE_CODE (expr) == COND_EXPR) | |
f293ce4b RS |
15298 | return build3 (COND_EXPR, |
15299 | TREE_TYPE (expr), | |
15300 | TREE_OPERAND (expr, 0), | |
c8094d83 | 15301 | (TREE_OPERAND (expr, 1) |
f293ce4b RS |
15302 | ? build_non_dependent_expr (TREE_OPERAND (expr, 1)) |
15303 | : build_non_dependent_expr (TREE_OPERAND (expr, 0))), | |
15304 | build_non_dependent_expr (TREE_OPERAND (expr, 2))); | |
8e1daa34 NS |
15305 | if (TREE_CODE (expr) == COMPOUND_EXPR |
15306 | && !COMPOUND_EXPR_OVERLOADED (expr)) | |
f293ce4b RS |
15307 | return build2 (COMPOUND_EXPR, |
15308 | TREE_TYPE (expr), | |
15309 | TREE_OPERAND (expr, 0), | |
15310 | build_non_dependent_expr (TREE_OPERAND (expr, 1))); | |
c8094d83 | 15311 | |
fdeff563 NS |
15312 | /* If the type is unknown, it can't really be non-dependent */ |
15313 | gcc_assert (TREE_TYPE (expr) != unknown_type_node); | |
3db45ab5 | 15314 | |
c8094d83 | 15315 | /* Otherwise, build a NON_DEPENDENT_EXPR. |
d17811fd MM |
15316 | |
15317 | REFERENCE_TYPEs are not stripped for expressions in templates | |
15318 | because doing so would play havoc with mangling. Consider, for | |
15319 | example: | |
15320 | ||
c8094d83 | 15321 | template <typename T> void f<T& g>() { g(); } |
d17811fd MM |
15322 | |
15323 | In the body of "f", the expression for "g" will have | |
15324 | REFERENCE_TYPE, even though the standard says that it should | |
15325 | not. The reason is that we must preserve the syntactic form of | |
15326 | the expression so that mangling (say) "f<g>" inside the body of | |
15327 | "f" works out correctly. Therefore, the REFERENCE_TYPE is | |
15328 | stripped here. */ | |
018a5803 | 15329 | return build1 (NON_DEPENDENT_EXPR, non_reference (TREE_TYPE (expr)), expr); |
d17811fd MM |
15330 | } |
15331 | ||
15332 | /* ARGS is a TREE_LIST of expressions as arguments to a function call. | |
15333 | Return a new TREE_LIST with the various arguments replaced with | |
15334 | equivalent non-dependent expressions. */ | |
15335 | ||
15336 | tree | |
15337 | build_non_dependent_args (tree args) | |
15338 | { | |
15339 | tree a; | |
15340 | tree new_args; | |
15341 | ||
15342 | new_args = NULL_TREE; | |
15343 | for (a = args; a; a = TREE_CHAIN (a)) | |
c8094d83 | 15344 | new_args = tree_cons (NULL_TREE, |
d17811fd MM |
15345 | build_non_dependent_expr (TREE_VALUE (a)), |
15346 | new_args); | |
15347 | return nreverse (new_args); | |
15348 | } | |
15349 | ||
e2500fed | 15350 | #include "gt-cp-pt.h" |