]>
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 |
e9fa0c7c RK |
21 | the Free Software Foundation, 59 Temple Place - Suite 330, |
22 | Boston, MA 02111-1307, 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 MS |
36 | #include "flags.h" |
37 | #include "cp-tree.h" | |
25af8512 | 38 | #include "tree-inline.h" |
8d08fdba | 39 | #include "decl.h" |
e8abc66f | 40 | #include "output.h" |
49c249e1 | 41 | #include "except.h" |
54f92bfb | 42 | #include "toplev.h" |
3dcaad8b | 43 | #include "rtl.h" |
297a5329 | 44 | #include "timevar.h" |
325c3691 | 45 | #include "tree-iterator.h" |
49c249e1 | 46 | |
050367a3 MM |
47 | /* The type of functions taking a tree, and some additional data, and |
48 | returning an int. */ | |
3a978d72 | 49 | typedef int (*tree_fn_t) (tree, void*); |
050367a3 | 50 | |
0aafb128 MM |
51 | /* The PENDING_TEMPLATES is a TREE_LIST of templates whose |
52 | instantiations have been deferred, either because their definitions | |
d78e771d ZW |
53 | were not yet available, or because we were putting off doing the work. |
54 | The TREE_PURPOSE of each entry is either a DECL (for a function or | |
55 | static data member), or a TYPE (for a class) indicating what we are | |
56 | hoping to instantiate. The TREE_VALUE is not used. */ | |
e2500fed | 57 | static GTY(()) tree pending_templates; |
13e73b29 | 58 | static GTY(()) tree last_pending_template; |
73aad9b9 | 59 | |
67ffc812 | 60 | int processing_template_parmlist; |
386b8a85 JM |
61 | static int template_header_count; |
62 | ||
e2500fed GK |
63 | static GTY(()) tree saved_trees; |
64 | static GTY(()) varray_type inline_parm_levels; | |
3dcaad8b | 65 | static size_t inline_parm_levels_used; |
75650646 | 66 | |
e2500fed | 67 | static GTY(()) tree current_tinst_level; |
3ae18eaf | 68 | |
2b59fc25 KL |
69 | static GTY(()) tree saved_access_scope; |
70 | ||
0fe0caa6 RH |
71 | /* Live only within one (recursive) call to tsubst_expr. We use |
72 | this to pass the statement expression node from the STMT_EXPR | |
73 | to the EXPR_STMT that is its result. */ | |
74 | static tree cur_stmt_expr; | |
75 | ||
6dfbb909 MM |
76 | /* A map from local variable declarations in the body of the template |
77 | presently being instantiated to the corresponding instantiated | |
78 | local variables. */ | |
79 | static htab_t local_specializations; | |
80 | ||
830bfa74 MM |
81 | #define UNIFY_ALLOW_NONE 0 |
82 | #define UNIFY_ALLOW_MORE_CV_QUAL 1 | |
83 | #define UNIFY_ALLOW_LESS_CV_QUAL 2 | |
84 | #define UNIFY_ALLOW_DERIVED 4 | |
161c12b0 | 85 | #define UNIFY_ALLOW_INTEGER 8 |
028d1f20 | 86 | #define UNIFY_ALLOW_OUTER_LEVEL 16 |
62e4a758 NS |
87 | #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32 |
88 | #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64 | |
d9f818d9 | 89 | #define UNIFY_ALLOW_MAX_CORRECTION 128 |
830bfa74 | 90 | |
3a978d72 NN |
91 | static void push_access_scope (tree); |
92 | static void pop_access_scope (tree); | |
93 | static int resolve_overloaded_unification (tree, tree, tree, tree, | |
94 | unification_kind_t, int); | |
95 | static int try_one_overload (tree, tree, tree, tree, tree, | |
f23fb7f5 | 96 | unification_kind_t, int, bool); |
3a978d72 NN |
97 | static int unify (tree, tree, tree, tree, int); |
98 | static void add_pending_template (tree); | |
99 | static void reopen_tinst_level (tree); | |
100 | static tree classtype_mangled_name (tree); | |
101 | static char* mangle_class_name_for_template (const char *, tree, tree); | |
102 | static tree tsubst_initializer_list (tree, tree); | |
3a978d72 NN |
103 | static tree get_class_bindings (tree, tree, tree); |
104 | static tree coerce_template_parms (tree, tree, tree, tsubst_flags_t, int); | |
105 | static void tsubst_enum (tree, tree, tree); | |
106 | static tree add_to_template_args (tree, tree); | |
107 | static tree add_outermost_template_args (tree, tree); | |
108 | static bool check_instantiated_args (tree, tree, tsubst_flags_t); | |
109 | static int maybe_adjust_types_for_deduction (unification_kind_t, tree*, tree*); | |
110 | static int type_unification_real (tree, tree, tree, tree, | |
111 | int, unification_kind_t, int, int); | |
112 | static void note_template_header (int); | |
b6ab6892 | 113 | static tree convert_nontype_argument_function (tree, tree); |
3a978d72 NN |
114 | static tree convert_nontype_argument (tree, tree); |
115 | static tree convert_template_argument (tree, tree, tree, | |
116 | tsubst_flags_t, int, tree); | |
117 | static tree get_bindings_overload (tree, tree, tree); | |
0c58f841 MA |
118 | static int for_each_template_parm (tree, tree_fn_t, void*, |
119 | struct pointer_set_t*); | |
3a978d72 NN |
120 | static tree build_template_parm_index (int, int, int, tree, tree); |
121 | static int inline_needs_template_parms (tree); | |
122 | static void push_inline_template_parms_recursive (tree, int); | |
3a978d72 | 123 | static tree retrieve_local_specialization (tree); |
3a978d72 | 124 | static void register_local_specialization (tree, tree); |
3a978d72 | 125 | static tree reduce_template_parm_level (tree, tree, int); |
3a978d72 NN |
126 | static int mark_template_parm (tree, void *); |
127 | static int template_parm_this_level_p (tree, void *); | |
128 | static tree tsubst_friend_function (tree, tree); | |
129 | static tree tsubst_friend_class (tree, tree); | |
130 | static int can_complete_type_without_circularity (tree); | |
10b1d5e7 | 131 | static tree get_bindings (tree, tree, tree); |
3a978d72 NN |
132 | static tree get_bindings_real (tree, tree, tree, int, int, int); |
133 | static int template_decl_level (tree); | |
134 | static int check_cv_quals_for_unify (int, tree, tree); | |
a91db711 NS |
135 | static tree tsubst_template_arg (tree, tree, tsubst_flags_t, tree); |
136 | static tree tsubst_template_args (tree, tree, tsubst_flags_t, tree); | |
3a978d72 NN |
137 | static tree tsubst_template_parms (tree, tree, tsubst_flags_t); |
138 | static void regenerate_decl_from_template (tree, tree); | |
139 | static tree most_specialized (tree, tree, tree); | |
140 | static tree most_specialized_class (tree, tree); | |
141 | static int template_class_depth_real (tree, int); | |
142 | static tree tsubst_aggr_type (tree, tree, tsubst_flags_t, tree, int); | |
3a978d72 NN |
143 | static tree tsubst_arg_types (tree, tree, tsubst_flags_t, tree); |
144 | static tree tsubst_function_type (tree, tree, tsubst_flags_t, tree); | |
145 | static void check_specialization_scope (void); | |
146 | static tree process_partial_specialization (tree); | |
147 | static void set_current_access_from_decl (tree); | |
148 | static void check_default_tmpl_args (tree, tree, int, int); | |
149 | static tree tsubst_call_declarator_parms (tree, tree, tsubst_flags_t, tree); | |
3a978d72 NN |
150 | static tree get_template_base (tree, tree, tree, tree); |
151 | static int verify_class_unification (tree, tree, tree); | |
152 | static tree try_class_unification (tree, tree, tree, tree); | |
153 | static int coerce_template_template_parms (tree, tree, tsubst_flags_t, | |
154 | tree, tree); | |
5fe7b654 | 155 | static tree determine_specialization (tree, tree, tree *, int, int); |
3a978d72 NN |
156 | static int template_args_equal (tree, tree); |
157 | static void tsubst_default_arguments (tree); | |
158 | static tree for_each_template_parm_r (tree *, int *, void *); | |
159 | static tree copy_default_args_to_explicit_spec_1 (tree, tree); | |
160 | static void copy_default_args_to_explicit_spec (tree); | |
161 | static int invalid_nontype_parm_type_p (tree, tsubst_flags_t); | |
a723baf1 | 162 | static int eq_local_specializations (const void *, const void *); |
5552b43c | 163 | static bool dependent_type_p_r (tree); |
14d22dd6 MM |
164 | static tree tsubst (tree, tree, tsubst_flags_t, tree); |
165 | static tree tsubst_expr (tree, tree, tsubst_flags_t, tree); | |
166 | static tree tsubst_copy (tree, tree, tsubst_flags_t, tree); | |
36a117a5 | 167 | |
2b59fc25 KL |
168 | /* Make the current scope suitable for access checking when we are |
169 | processing T. T can be FUNCTION_DECL for instantiated function | |
2b907f5c KL |
170 | template, or VAR_DECL for static member variable (need by |
171 | instantiate_decl). */ | |
2b59fc25 | 172 | |
8ce33230 | 173 | static void |
2b907f5c | 174 | push_access_scope (tree t) |
2b59fc25 | 175 | { |
50bc768d NS |
176 | gcc_assert (TREE_CODE (t) == FUNCTION_DECL |
177 | || TREE_CODE (t) == VAR_DECL); | |
2b59fc25 | 178 | |
23ff7e2d KL |
179 | if (DECL_FRIEND_CONTEXT (t)) |
180 | push_nested_class (DECL_FRIEND_CONTEXT (t)); | |
181 | else if (DECL_CLASS_SCOPE_P (t)) | |
2b907f5c | 182 | push_nested_class (DECL_CONTEXT (t)); |
0f399e5f KL |
183 | else |
184 | push_to_top_level (); | |
185 | ||
2b907f5c | 186 | if (TREE_CODE (t) == FUNCTION_DECL) |
0f399e5f KL |
187 | { |
188 | saved_access_scope = tree_cons | |
189 | (NULL_TREE, current_function_decl, saved_access_scope); | |
190 | current_function_decl = t; | |
191 | } | |
2b59fc25 KL |
192 | } |
193 | ||
2b59fc25 KL |
194 | /* Restore the scope set up by push_access_scope. T is the node we |
195 | are processing. */ | |
196 | ||
8ce33230 | 197 | static void |
3a978d72 | 198 | pop_access_scope (tree t) |
2b59fc25 | 199 | { |
2b907f5c | 200 | if (TREE_CODE (t) == FUNCTION_DECL) |
2b59fc25 KL |
201 | { |
202 | current_function_decl = TREE_VALUE (saved_access_scope); | |
203 | saved_access_scope = TREE_CHAIN (saved_access_scope); | |
204 | } | |
0f399e5f | 205 | |
23ff7e2d | 206 | if (DECL_FRIEND_CONTEXT (t) || DECL_CLASS_SCOPE_P (t)) |
0f399e5f KL |
207 | pop_nested_class (); |
208 | else | |
209 | pop_from_top_level (); | |
2b59fc25 KL |
210 | } |
211 | ||
a723baf1 MM |
212 | /* Do any processing required when DECL (a member template |
213 | declaration) is finished. Returns the TEMPLATE_DECL corresponding | |
214 | to DECL, unless it is a specialization, in which case the DECL | |
215 | itself is returned. */ | |
e1467ff2 MM |
216 | |
217 | tree | |
3a978d72 | 218 | finish_member_template_decl (tree decl) |
e1467ff2 | 219 | { |
a723baf1 MM |
220 | if (decl == error_mark_node) |
221 | return error_mark_node; | |
222 | ||
50bc768d | 223 | gcc_assert (DECL_P (decl)); |
a723baf1 MM |
224 | |
225 | if (TREE_CODE (decl) == TYPE_DECL) | |
93cdc044 | 226 | { |
a723baf1 MM |
227 | tree type; |
228 | ||
229 | type = TREE_TYPE (decl); | |
230 | if (IS_AGGR_TYPE (type) | |
231 | && CLASSTYPE_TEMPLATE_INFO (type) | |
232 | && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type)) | |
93cdc044 | 233 | { |
a723baf1 | 234 | tree tmpl = CLASSTYPE_TI_TEMPLATE (type); |
93cdc044 JM |
235 | check_member_template (tmpl); |
236 | return tmpl; | |
237 | } | |
8d019cef | 238 | return NULL_TREE; |
93cdc044 | 239 | } |
07c88314 | 240 | else if (TREE_CODE (decl) == FIELD_DECL) |
0f51ccfc | 241 | error ("data member %qD cannot be a member template", decl); |
a1da6cba | 242 | else if (DECL_TEMPLATE_INFO (decl)) |
e1467ff2 | 243 | { |
a1da6cba MM |
244 | if (!DECL_TEMPLATE_SPECIALIZATION (decl)) |
245 | { | |
246 | check_member_template (DECL_TI_TEMPLATE (decl)); | |
247 | return DECL_TI_TEMPLATE (decl); | |
248 | } | |
249 | else | |
250 | return decl; | |
251 | } | |
252 | else | |
0f51ccfc | 253 | error ("invalid member template declaration %qD", decl); |
e1467ff2 | 254 | |
a1da6cba | 255 | return error_mark_node; |
f84b4be9 | 256 | } |
e1467ff2 | 257 | |
f84b4be9 JM |
258 | /* Returns the template nesting level of the indicated class TYPE. |
259 | ||
260 | For example, in: | |
261 | template <class T> | |
262 | struct A | |
263 | { | |
264 | template <class U> | |
265 | struct B {}; | |
266 | }; | |
267 | ||
39c01e4c MM |
268 | A<T>::B<U> has depth two, while A<T> has depth one. |
269 | Both A<T>::B<int> and A<int>::B<U> have depth one, if | |
270 | COUNT_SPECIALIZATIONS is 0 or if they are instantiations, not | |
271 | specializations. | |
272 | ||
273 | This function is guaranteed to return 0 if passed NULL_TREE so | |
274 | that, for example, `template_class_depth (current_class_type)' is | |
275 | always safe. */ | |
f84b4be9 | 276 | |
e9659ab0 | 277 | static int |
3a978d72 | 278 | template_class_depth_real (tree type, int count_specializations) |
f84b4be9 | 279 | { |
93cdc044 | 280 | int depth; |
f84b4be9 | 281 | |
2c73f9f5 | 282 | for (depth = 0; |
ed44da02 MM |
283 | type && TREE_CODE (type) != NAMESPACE_DECL; |
284 | type = (TREE_CODE (type) == FUNCTION_DECL) | |
4f1c5b7d | 285 | ? CP_DECL_CONTEXT (type) : TYPE_CONTEXT (type)) |
ed44da02 MM |
286 | { |
287 | if (TREE_CODE (type) != FUNCTION_DECL) | |
288 | { | |
289 | if (CLASSTYPE_TEMPLATE_INFO (type) | |
290 | && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)) | |
291 | && ((count_specializations | |
370af2d5 | 292 | && CLASSTYPE_TEMPLATE_SPECIALIZATION (type)) |
ed44da02 MM |
293 | || uses_template_parms (CLASSTYPE_TI_ARGS (type)))) |
294 | ++depth; | |
295 | } | |
296 | else | |
297 | { | |
298 | if (DECL_TEMPLATE_INFO (type) | |
299 | && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (type)) | |
300 | && ((count_specializations | |
301 | && DECL_TEMPLATE_SPECIALIZATION (type)) | |
302 | || uses_template_parms (DECL_TI_ARGS (type)))) | |
303 | ++depth; | |
304 | } | |
305 | } | |
f84b4be9 JM |
306 | |
307 | return depth; | |
e1467ff2 | 308 | } |
98c1c668 | 309 | |
39c01e4c MM |
310 | /* Returns the template nesting level of the indicated class TYPE. |
311 | Like template_class_depth_real, but instantiations do not count in | |
312 | the depth. */ | |
313 | ||
314 | int | |
3a978d72 | 315 | template_class_depth (tree type) |
39c01e4c MM |
316 | { |
317 | return template_class_depth_real (type, /*count_specializations=*/0); | |
318 | } | |
319 | ||
cae40af6 JM |
320 | /* Returns 1 if processing DECL as part of do_pending_inlines |
321 | needs us to push template parms. */ | |
322 | ||
323 | static int | |
3a978d72 | 324 | inline_needs_template_parms (tree decl) |
cae40af6 JM |
325 | { |
326 | if (! DECL_TEMPLATE_INFO (decl)) | |
327 | return 0; | |
f84b4be9 | 328 | |
36a117a5 | 329 | return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl))) |
cae40af6 JM |
330 | > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl))); |
331 | } | |
332 | ||
333 | /* Subroutine of maybe_begin_member_template_processing. | |
334 | Push the template parms in PARMS, starting from LEVELS steps into the | |
335 | chain, and ending at the beginning, since template parms are listed | |
336 | innermost first. */ | |
337 | ||
338 | static void | |
3a978d72 | 339 | push_inline_template_parms_recursive (tree parmlist, int levels) |
cae40af6 JM |
340 | { |
341 | tree parms = TREE_VALUE (parmlist); | |
342 | int i; | |
343 | ||
344 | if (levels > 1) | |
345 | push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1); | |
786b5245 | 346 | |
98c1c668 | 347 | ++processing_template_decl; |
cae40af6 | 348 | current_template_parms |
4890c2f4 | 349 | = tree_cons (size_int (processing_template_decl), |
98c1c668 | 350 | parms, current_template_parms); |
cae40af6 JM |
351 | TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1; |
352 | ||
ac20c67a GDR |
353 | begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec, |
354 | NULL); | |
98c1c668 JM |
355 | for (i = 0; i < TREE_VEC_LENGTH (parms); ++i) |
356 | { | |
786b5245 | 357 | tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i)); |
50bc768d | 358 | gcc_assert (DECL_P (parm)); |
cae40af6 | 359 | |
98c1c668 JM |
360 | switch (TREE_CODE (parm)) |
361 | { | |
786b5245 | 362 | case TYPE_DECL: |
73b0fce8 | 363 | case TEMPLATE_DECL: |
98c1c668 JM |
364 | pushdecl (parm); |
365 | break; | |
786b5245 MM |
366 | |
367 | case PARM_DECL: | |
368 | { | |
fc03edb3 MM |
369 | /* Make a CONST_DECL as is done in process_template_parm. |
370 | It is ugly that we recreate this here; the original | |
371 | version built in process_template_parm is no longer | |
372 | available. */ | |
786b5245 MM |
373 | tree decl = build_decl (CONST_DECL, DECL_NAME (parm), |
374 | TREE_TYPE (parm)); | |
c727aa5e | 375 | DECL_ARTIFICIAL (decl) = 1; |
6de9cd9a DN |
376 | TREE_CONSTANT (decl) = 1; |
377 | TREE_INVARIANT (decl) = 1; | |
378 | TREE_READONLY (decl) = 1; | |
786b5245 | 379 | DECL_INITIAL (decl) = DECL_INITIAL (parm); |
cd9f6678 | 380 | SET_DECL_TEMPLATE_PARM_P (decl); |
786b5245 MM |
381 | pushdecl (decl); |
382 | } | |
cae40af6 | 383 | break; |
786b5245 | 384 | |
98c1c668 | 385 | default: |
315fb5db | 386 | gcc_unreachable (); |
98c1c668 JM |
387 | } |
388 | } | |
389 | } | |
390 | ||
cae40af6 JM |
391 | /* Restore the template parameter context for a member template or |
392 | a friend template defined in a class definition. */ | |
393 | ||
394 | void | |
3a978d72 | 395 | maybe_begin_member_template_processing (tree decl) |
cae40af6 JM |
396 | { |
397 | tree parms; | |
3dcaad8b | 398 | int levels = 0; |
cae40af6 | 399 | |
3dcaad8b MM |
400 | if (inline_needs_template_parms (decl)) |
401 | { | |
402 | parms = DECL_TEMPLATE_PARMS (most_general_template (decl)); | |
403 | levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl; | |
cae40af6 | 404 | |
3dcaad8b MM |
405 | if (DECL_TEMPLATE_SPECIALIZATION (decl)) |
406 | { | |
407 | --levels; | |
408 | parms = TREE_CHAIN (parms); | |
409 | } | |
cae40af6 | 410 | |
3dcaad8b | 411 | push_inline_template_parms_recursive (parms, levels); |
cae40af6 JM |
412 | } |
413 | ||
3dcaad8b MM |
414 | /* Remember how many levels of template parameters we pushed so that |
415 | we can pop them later. */ | |
416 | if (!inline_parm_levels) | |
417 | VARRAY_INT_INIT (inline_parm_levels, 4, "inline_parm_levels"); | |
418 | if (inline_parm_levels_used == inline_parm_levels->num_elements) | |
419 | VARRAY_GROW (inline_parm_levels, 2 * inline_parm_levels_used); | |
420 | VARRAY_INT (inline_parm_levels, inline_parm_levels_used) = levels; | |
421 | ++inline_parm_levels_used; | |
cae40af6 JM |
422 | } |
423 | ||
c6002625 | 424 | /* Undo the effects of begin_member_template_processing. */ |
98c1c668 JM |
425 | |
426 | void | |
3a978d72 | 427 | maybe_end_member_template_processing (void) |
98c1c668 | 428 | { |
3dcaad8b MM |
429 | int i; |
430 | ||
431 | if (!inline_parm_levels_used) | |
98c1c668 JM |
432 | return; |
433 | ||
3dcaad8b MM |
434 | --inline_parm_levels_used; |
435 | for (i = 0; | |
436 | i < VARRAY_INT (inline_parm_levels, inline_parm_levels_used); | |
437 | ++i) | |
cae40af6 JM |
438 | { |
439 | --processing_template_decl; | |
440 | current_template_parms = TREE_CHAIN (current_template_parms); | |
441 | poplevel (0, 0, 0); | |
442 | } | |
98c1c668 JM |
443 | } |
444 | ||
36a117a5 | 445 | /* Return a new template argument vector which contains all of ARGS, |
f9a7ae04 | 446 | but has as its innermost set of arguments the EXTRA_ARGS. */ |
e6f1275f JM |
447 | |
448 | static tree | |
3a978d72 | 449 | add_to_template_args (tree args, tree extra_args) |
e6f1275f | 450 | { |
36a117a5 MM |
451 | tree new_args; |
452 | int extra_depth; | |
453 | int i; | |
454 | int j; | |
e6f1275f | 455 | |
36a117a5 | 456 | extra_depth = TMPL_ARGS_DEPTH (extra_args); |
f31c0a32 | 457 | new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth); |
e6f1275f | 458 | |
36a117a5 MM |
459 | for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i) |
460 | SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i)); | |
e6f1275f | 461 | |
36a117a5 MM |
462 | for (j = 1; j <= extra_depth; ++j, ++i) |
463 | SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j)); | |
464 | ||
e6f1275f JM |
465 | return new_args; |
466 | } | |
467 | ||
36a117a5 MM |
468 | /* Like add_to_template_args, but only the outermost ARGS are added to |
469 | the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH | |
470 | (EXTRA_ARGS) levels are added. This function is used to combine | |
471 | the template arguments from a partial instantiation with the | |
472 | template arguments used to attain the full instantiation from the | |
473 | partial instantiation. */ | |
98c1c668 | 474 | |
4966381a | 475 | static tree |
3a978d72 | 476 | add_outermost_template_args (tree args, tree extra_args) |
98c1c668 JM |
477 | { |
478 | tree new_args; | |
479 | ||
e4a84209 MM |
480 | /* If there are more levels of EXTRA_ARGS than there are ARGS, |
481 | something very fishy is going on. */ | |
50bc768d | 482 | gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args)); |
e4a84209 MM |
483 | |
484 | /* If *all* the new arguments will be the EXTRA_ARGS, just return | |
485 | them. */ | |
486 | if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args)) | |
487 | return extra_args; | |
488 | ||
36a117a5 MM |
489 | /* For the moment, we make ARGS look like it contains fewer levels. */ |
490 | TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args); | |
491 | ||
492 | new_args = add_to_template_args (args, extra_args); | |
98c1c668 | 493 | |
36a117a5 MM |
494 | /* Now, we restore ARGS to its full dimensions. */ |
495 | TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args); | |
98c1c668 JM |
496 | |
497 | return new_args; | |
498 | } | |
5566b478 | 499 | |
f9a7ae04 MM |
500 | /* Return the N levels of innermost template arguments from the ARGS. */ |
501 | ||
502 | tree | |
3a978d72 | 503 | get_innermost_template_args (tree args, int n) |
f9a7ae04 MM |
504 | { |
505 | tree new_args; | |
506 | int extra_levels; | |
507 | int i; | |
508 | ||
50bc768d | 509 | gcc_assert (n >= 0); |
f9a7ae04 MM |
510 | |
511 | /* If N is 1, just return the innermost set of template arguments. */ | |
512 | if (n == 1) | |
513 | return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args)); | |
514 | ||
515 | /* If we're not removing anything, just return the arguments we were | |
516 | given. */ | |
517 | extra_levels = TMPL_ARGS_DEPTH (args) - n; | |
50bc768d | 518 | gcc_assert (extra_levels >= 0); |
f9a7ae04 MM |
519 | if (extra_levels == 0) |
520 | return args; | |
521 | ||
522 | /* Make a new set of arguments, not containing the outer arguments. */ | |
523 | new_args = make_tree_vec (n); | |
524 | for (i = 1; i <= n; ++i) | |
525 | SET_TMPL_ARGS_LEVEL (new_args, i, | |
526 | TMPL_ARGS_LEVEL (args, i + extra_levels)); | |
527 | ||
528 | return new_args; | |
529 | } | |
530 | ||
5566b478 MS |
531 | /* We've got a template header coming up; push to a new level for storing |
532 | the parms. */ | |
8d08fdba | 533 | |
8d08fdba | 534 | void |
3a978d72 | 535 | begin_template_parm_list (void) |
8d08fdba | 536 | { |
6757edfe MM |
537 | /* We use a non-tag-transparent scope here, which causes pushtag to |
538 | put tags in this scope, rather than in the enclosing class or | |
539 | namespace scope. This is the right thing, since we want | |
540 | TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a | |
541 | global template class, push_template_decl handles putting the | |
542 | TEMPLATE_DECL into top-level scope. For a nested template class, | |
543 | e.g.: | |
544 | ||
545 | template <class T> struct S1 { | |
546 | template <class T> struct S2 {}; | |
547 | }; | |
548 | ||
549 | pushtag contains special code to call pushdecl_with_scope on the | |
550 | TEMPLATE_DECL for S2. */ | |
ac20c67a | 551 | begin_scope (sk_template_parms, NULL); |
5156628f | 552 | ++processing_template_decl; |
67ffc812 | 553 | ++processing_template_parmlist; |
386b8a85 JM |
554 | note_template_header (0); |
555 | } | |
556 | ||
6c30752f | 557 | /* This routine is called when a specialization is declared. If it is |
0e339752 | 558 | invalid to declare a specialization here, an error is reported. */ |
6c30752f | 559 | |
e9659ab0 | 560 | static void |
3a978d72 | 561 | check_specialization_scope (void) |
6c30752f MM |
562 | { |
563 | tree scope = current_scope (); | |
3ddfb0e6 | 564 | |
6c30752f MM |
565 | /* [temp.expl.spec] |
566 | ||
567 | An explicit specialization shall be declared in the namespace of | |
568 | which the template is a member, or, for member templates, in the | |
569 | namespace of which the enclosing class or enclosing class | |
570 | template is a member. An explicit specialization of a member | |
571 | function, member class or static data member of a class template | |
572 | shall be declared in the namespace of which the class template | |
573 | is a member. */ | |
574 | if (scope && TREE_CODE (scope) != NAMESPACE_DECL) | |
0f51ccfc | 575 | error ("explicit specialization in non-namespace scope %qD", scope); |
3ddfb0e6 | 576 | |
6c30752f MM |
577 | /* [temp.expl.spec] |
578 | ||
579 | In an explicit specialization declaration for a member of a class | |
580 | template or a member template that appears in namespace scope, | |
581 | the member template and some of its enclosing class templates may | |
582 | remain unspecialized, except that the declaration shall not | |
583 | explicitly specialize a class member template if its enclosing | |
584 | class templates are not explicitly specialized as well. */ | |
585 | if (current_template_parms) | |
33bd39a2 | 586 | error ("enclosing class templates are not explicitly specialized"); |
6c30752f MM |
587 | } |
588 | ||
c6002625 | 589 | /* We've just seen template <>. */ |
386b8a85 JM |
590 | |
591 | void | |
3a978d72 | 592 | begin_specialization (void) |
386b8a85 | 593 | { |
ac20c67a | 594 | begin_scope (sk_template_spec, NULL); |
386b8a85 | 595 | note_template_header (1); |
6c30752f | 596 | check_specialization_scope (); |
386b8a85 JM |
597 | } |
598 | ||
dc957d14 | 599 | /* Called at then end of processing a declaration preceded by |
386b8a85 JM |
600 | template<>. */ |
601 | ||
602 | void | |
3a978d72 | 603 | end_specialization (void) |
386b8a85 | 604 | { |
74b846e0 | 605 | finish_scope (); |
386b8a85 JM |
606 | reset_specialization (); |
607 | } | |
608 | ||
386b8a85 | 609 | /* Any template <>'s that we have seen thus far are not referring to a |
c6002625 | 610 | function specialization. */ |
386b8a85 JM |
611 | |
612 | void | |
3a978d72 | 613 | reset_specialization (void) |
386b8a85 JM |
614 | { |
615 | processing_specialization = 0; | |
616 | template_header_count = 0; | |
617 | } | |
618 | ||
838dfd8a | 619 | /* We've just seen a template header. If SPECIALIZATION is nonzero, |
386b8a85 JM |
620 | it was of the form template <>. */ |
621 | ||
4966381a | 622 | static void |
3a978d72 | 623 | note_template_header (int specialization) |
386b8a85 JM |
624 | { |
625 | processing_specialization = specialization; | |
626 | template_header_count++; | |
627 | } | |
628 | ||
75650646 | 629 | /* We're beginning an explicit instantiation. */ |
386b8a85 | 630 | |
75650646 | 631 | void |
3a978d72 | 632 | begin_explicit_instantiation (void) |
386b8a85 | 633 | { |
50bc768d | 634 | gcc_assert (!processing_explicit_instantiation); |
a723baf1 | 635 | processing_explicit_instantiation = true; |
75650646 | 636 | } |
386b8a85 | 637 | |
386b8a85 | 638 | |
75650646 | 639 | void |
3a978d72 | 640 | end_explicit_instantiation (void) |
75650646 | 641 | { |
50bc768d | 642 | gcc_assert (processing_explicit_instantiation); |
a723baf1 | 643 | processing_explicit_instantiation = false; |
75650646 | 644 | } |
386b8a85 | 645 | |
b1cc95ce MM |
646 | /* A explicit specialization or partial specialization TMPL is being |
647 | declared. Check that the namespace in which the specialization is | |
648 | occurring is permissible. Returns false iff it is invalid to | |
649 | specialize TMPL in the current namespace. */ | |
650 | ||
651 | static bool | |
652 | check_specialization_namespace (tree tmpl) | |
653 | { | |
654 | tree tpl_ns = decl_namespace_context (tmpl); | |
655 | ||
656 | /* [tmpl.expl.spec] | |
657 | ||
658 | An explicit specialization shall be declared in the namespace of | |
659 | which the template is a member, or, for member templates, in the | |
660 | namespace of which the enclosing class or enclosing class | |
661 | template is a member. An explicit specialization of a member | |
662 | function, member class or static data member of a class template | |
663 | shall be declared in the namespace of which the class template is | |
664 | a member. */ | |
665 | if (is_associated_namespace (current_namespace, tpl_ns)) | |
666 | /* Same or super-using namespace. */ | |
667 | return true; | |
668 | else | |
669 | { | |
0f51ccfc GDR |
670 | pedwarn ("specialization of %qD in different namespace", tmpl); |
671 | cp_pedwarn_at (" from definition of %q#D", tmpl); | |
b1cc95ce MM |
672 | return false; |
673 | } | |
674 | } | |
675 | ||
36a117a5 MM |
676 | /* The TYPE is being declared. If it is a template type, that means it |
677 | is a partial specialization. Do appropriate error-checking. */ | |
678 | ||
679 | void | |
3a978d72 | 680 | maybe_process_partial_specialization (tree type) |
36a117a5 | 681 | { |
6e049fcd KL |
682 | /* TYPE maybe an ERROR_MARK_NODE. */ |
683 | tree context = TYPE_P (type) ? TYPE_CONTEXT (type) : NULL_TREE; | |
684 | ||
a723baf1 | 685 | if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type)) |
36a117a5 | 686 | { |
6e049fcd KL |
687 | /* This is for ordinary explicit specialization and partial |
688 | specialization of a template class such as: | |
689 | ||
690 | template <> class C<int>; | |
691 | ||
692 | or: | |
693 | ||
694 | template <class T> class C<T*>; | |
695 | ||
696 | Make sure that `C<int>' and `C<T*>' are implicit instantiations. */ | |
697 | ||
36a117a5 | 698 | if (CLASSTYPE_IMPLICIT_INSTANTIATION (type) |
d0f062fb | 699 | && !COMPLETE_TYPE_P (type)) |
36a117a5 | 700 | { |
b1cc95ce | 701 | check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (type)); |
370af2d5 | 702 | SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type); |
36a117a5 MM |
703 | if (processing_template_decl) |
704 | push_template_decl (TYPE_MAIN_DECL (type)); | |
705 | } | |
706 | else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type)) | |
0f51ccfc | 707 | error ("specialization of %qT after instantiation", type); |
36a117a5 | 708 | } |
6e049fcd KL |
709 | else if (CLASS_TYPE_P (type) |
710 | && !CLASSTYPE_USE_TEMPLATE (type) | |
711 | && CLASSTYPE_TEMPLATE_INFO (type) | |
712 | && context && CLASS_TYPE_P (context) | |
713 | && CLASSTYPE_TEMPLATE_INFO (context)) | |
714 | { | |
715 | /* This is for an explicit specialization of member class | |
716 | template according to [temp.expl.spec/18]: | |
717 | ||
718 | template <> template <class U> class C<int>::D; | |
719 | ||
720 | The context `C<int>' must be an implicit instantiation. | |
721 | Otherwise this is just a member class template declared | |
722 | earlier like: | |
723 | ||
724 | template <> class C<int> { template <class U> class D; }; | |
725 | template <> template <class U> class C<int>::D; | |
726 | ||
727 | In the first case, `C<int>::D' is a specialization of `C<T>::D' | |
728 | while in the second case, `C<int>::D' is a primary template | |
729 | and `C<T>::D' may not exist. */ | |
730 | ||
731 | if (CLASSTYPE_IMPLICIT_INSTANTIATION (context) | |
732 | && !COMPLETE_TYPE_P (type)) | |
733 | { | |
734 | tree t; | |
735 | ||
736 | if (current_namespace | |
737 | != decl_namespace_context (CLASSTYPE_TI_TEMPLATE (type))) | |
738 | { | |
0f51ccfc GDR |
739 | pedwarn ("specializing %q#T in different namespace", type); |
740 | cp_pedwarn_at (" from definition of %q#D", | |
6e049fcd KL |
741 | CLASSTYPE_TI_TEMPLATE (type)); |
742 | } | |
743 | ||
744 | /* Check for invalid specialization after instantiation: | |
745 | ||
746 | template <> template <> class C<int>::D<int>; | |
747 | template <> template <class U> class C<int>::D; */ | |
748 | ||
749 | for (t = DECL_TEMPLATE_INSTANTIATIONS | |
750 | (most_general_template (CLASSTYPE_TI_TEMPLATE (type))); | |
751 | t; t = TREE_CHAIN (t)) | |
752 | if (TREE_VALUE (t) != type | |
753 | && TYPE_CONTEXT (TREE_VALUE (t)) == context) | |
0f51ccfc | 754 | error ("specialization %qT after instantiation %qT", |
6e049fcd KL |
755 | type, TREE_VALUE (t)); |
756 | ||
757 | /* Mark TYPE as a specialization. And as a result, we only | |
758 | have one level of template argument for the innermost | |
759 | class template. */ | |
760 | SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type); | |
761 | CLASSTYPE_TI_ARGS (type) | |
762 | = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)); | |
763 | } | |
764 | } | |
20496fa2 | 765 | else if (processing_specialization) |
0f51ccfc | 766 | error ("explicit specialization of non-template %qT", type); |
36a117a5 MM |
767 | } |
768 | ||
ee81147e | 769 | /* Returns nonzero if we can optimize the retrieval of specializations |
c7222c02 MM |
770 | for TMPL, a TEMPLATE_DECL. In particular, for such a template, we |
771 | do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */ | |
772 | ||
773 | static inline bool | |
774 | optimize_specialization_lookup_p (tree tmpl) | |
775 | { | |
776 | return (DECL_FUNCTION_TEMPLATE_P (tmpl) | |
777 | && DECL_CLASS_SCOPE_P (tmpl) | |
778 | /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template | |
779 | parameter. */ | |
780 | && CLASS_TYPE_P (DECL_CONTEXT (tmpl)) | |
1ca939e5 MM |
781 | /* The optimized lookup depends on the fact that the |
782 | template arguments for the member function template apply | |
783 | purely to the containing class, which is not true if the | |
784 | containing class is an explicit or partial | |
785 | specialization. */ | |
786 | && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl)) | |
c7222c02 MM |
787 | && !DECL_MEMBER_TEMPLATE_P (tmpl) |
788 | && !DECL_CONV_FN_P (tmpl) | |
789 | /* It is possible to have a template that is not a member | |
790 | template and is not a member of a template class: | |
791 | ||
792 | template <typename T> | |
793 | struct S { friend A::f(); }; | |
794 | ||
795 | Here, the friend function is a template, but the context does | |
796 | not have template information. The optimized lookup relies | |
797 | on having ARGS be the template arguments for both the class | |
798 | and the function template. */ | |
799 | && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl))); | |
800 | } | |
801 | ||
75650646 MM |
802 | /* Retrieve the specialization (in the sense of [temp.spec] - a |
803 | specialization is either an instantiation or an explicit | |
804 | specialization) of TMPL for the given template ARGS. If there is | |
805 | no such specialization, return NULL_TREE. The ARGS are a vector of | |
806 | arguments, or a vector of vectors of arguments, in the case of | |
c7222c02 MM |
807 | templates with more than one level of parameters. |
808 | ||
809 | If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true, | |
810 | then we search for a partial specialization matching ARGS. This | |
811 | parameter is ignored if TMPL is not a class template. */ | |
75650646 MM |
812 | |
813 | static tree | |
c7222c02 MM |
814 | retrieve_specialization (tree tmpl, tree args, |
815 | bool class_specializations_p) | |
75650646 | 816 | { |
50bc768d | 817 | gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL); |
75650646 | 818 | |
36a117a5 MM |
819 | /* There should be as many levels of arguments as there are |
820 | levels of parameters. */ | |
50bc768d NS |
821 | gcc_assert (TMPL_ARGS_DEPTH (args) |
822 | == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl))); | |
c7222c02 MM |
823 | |
824 | if (optimize_specialization_lookup_p (tmpl)) | |
825 | { | |
826 | tree class_template; | |
827 | tree class_specialization; | |
828 | VEC(tree) *methods; | |
829 | tree fns; | |
830 | int idx; | |
831 | ||
832 | /* The template arguments actually apply to the containing | |
833 | class. Find the class specialization with those | |
834 | arguments. */ | |
835 | class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl)); | |
836 | class_specialization | |
837 | = retrieve_specialization (class_template, args, | |
838 | /*class_specializations_p=*/false); | |
839 | if (!class_specialization) | |
840 | return NULL_TREE; | |
841 | /* Now, find the appropriate entry in the CLASSTYPE_METHOD_VEC | |
842 | for the specialization. */ | |
843 | idx = class_method_index_for_fn (class_specialization, tmpl); | |
844 | if (idx == -1) | |
845 | return NULL_TREE; | |
846 | /* Iterate through the methods with the indicated name, looking | |
847 | for the one that has an instance of TMPL. */ | |
848 | methods = CLASSTYPE_METHOD_VEC (class_specialization); | |
849 | for (fns = VEC_index (tree, methods, idx); fns; fns = OVL_NEXT (fns)) | |
850 | { | |
851 | tree fn = OVL_CURRENT (fns); | |
852 | if (DECL_TEMPLATE_INFO (fn) && DECL_TI_TEMPLATE (fn) == tmpl) | |
853 | return fn; | |
854 | } | |
855 | return NULL_TREE; | |
856 | } | |
857 | else | |
858 | { | |
859 | tree *sp; | |
860 | tree *head; | |
861 | ||
862 | /* Class templates store their instantiations on the | |
863 | DECL_TEMPLATE_INSTANTIATIONS list; other templates use the | |
864 | DECL_TEMPLATE_SPECIALIZATIONS list. */ | |
865 | if (!class_specializations_p | |
866 | && TREE_CODE (DECL_TEMPLATE_RESULT (tmpl)) == TYPE_DECL) | |
867 | sp = &DECL_TEMPLATE_INSTANTIATIONS (tmpl); | |
868 | else | |
869 | sp = &DECL_TEMPLATE_SPECIALIZATIONS (tmpl); | |
870 | head = sp; | |
871 | /* Iterate through the list until we find a matching template. */ | |
872 | while (*sp != NULL_TREE) | |
873 | { | |
874 | tree spec = *sp; | |
875 | ||
876 | if (comp_template_args (TREE_PURPOSE (spec), args)) | |
877 | { | |
878 | /* Use the move-to-front heuristic to speed up future | |
879 | searches. */ | |
880 | if (spec != *head) | |
881 | { | |
882 | *sp = TREE_CHAIN (*sp); | |
883 | TREE_CHAIN (spec) = *head; | |
884 | *head = spec; | |
885 | } | |
886 | return TREE_VALUE (spec); | |
887 | } | |
888 | sp = &TREE_CHAIN (spec); | |
889 | } | |
890 | } | |
75650646 MM |
891 | |
892 | return NULL_TREE; | |
386b8a85 JM |
893 | } |
894 | ||
dc957d14 | 895 | /* Like retrieve_specialization, but for local declarations. */ |
9188c363 MM |
896 | |
897 | static tree | |
3a978d72 | 898 | retrieve_local_specialization (tree tmpl) |
9188c363 | 899 | { |
c68b0a84 KG |
900 | tree spec = htab_find_with_hash (local_specializations, tmpl, |
901 | htab_hash_pointer (tmpl)); | |
a723baf1 | 902 | return spec ? TREE_PURPOSE (spec) : NULL_TREE; |
9188c363 MM |
903 | } |
904 | ||
838dfd8a | 905 | /* Returns nonzero iff DECL is a specialization of TMPL. */ |
6757edfe MM |
906 | |
907 | int | |
3a978d72 | 908 | is_specialization_of (tree decl, tree tmpl) |
6757edfe MM |
909 | { |
910 | tree t; | |
911 | ||
912 | if (TREE_CODE (decl) == FUNCTION_DECL) | |
913 | { | |
914 | for (t = decl; | |
915 | t != NULL_TREE; | |
916 | t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE) | |
917 | if (t == tmpl) | |
918 | return 1; | |
919 | } | |
920 | else | |
921 | { | |
50bc768d | 922 | gcc_assert (TREE_CODE (decl) == TYPE_DECL); |
6757edfe MM |
923 | |
924 | for (t = TREE_TYPE (decl); | |
925 | t != NULL_TREE; | |
926 | t = CLASSTYPE_USE_TEMPLATE (t) | |
927 | ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE) | |
9edc3913 | 928 | if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl))) |
6757edfe MM |
929 | return 1; |
930 | } | |
386b8a85 | 931 | |
6757edfe MM |
932 | return 0; |
933 | } | |
75650646 | 934 | |
d43f603d KL |
935 | /* Returns nonzero iff DECL is a specialization of friend declaration |
936 | FRIEND according to [temp.friend]. */ | |
937 | ||
938 | bool | |
939 | is_specialization_of_friend (tree decl, tree friend) | |
940 | { | |
941 | bool need_template = true; | |
942 | int template_depth; | |
943 | ||
b939a023 KL |
944 | gcc_assert (TREE_CODE (decl) == FUNCTION_DECL |
945 | || TREE_CODE (decl) == TYPE_DECL); | |
d43f603d KL |
946 | |
947 | /* For [temp.friend/6] when FRIEND is an ordinary member function | |
948 | of a template class, we want to check if DECL is a specialization | |
949 | if this. */ | |
950 | if (TREE_CODE (friend) == FUNCTION_DECL | |
951 | && DECL_TEMPLATE_INFO (friend) | |
952 | && !DECL_USE_TEMPLATE (friend)) | |
953 | { | |
b939a023 | 954 | /* We want a TEMPLATE_DECL for `is_specialization_of'. */ |
d43f603d KL |
955 | friend = DECL_TI_TEMPLATE (friend); |
956 | need_template = false; | |
957 | } | |
b939a023 KL |
958 | else if (TREE_CODE (friend) == TEMPLATE_DECL |
959 | && !PRIMARY_TEMPLATE_P (friend)) | |
960 | need_template = false; | |
d43f603d KL |
961 | |
962 | /* There is nothing to do if this is not a template friend. */ | |
963 | if (TREE_CODE (friend) != TEMPLATE_DECL) | |
b939a023 | 964 | return false; |
d43f603d KL |
965 | |
966 | if (is_specialization_of (decl, friend)) | |
b939a023 | 967 | return true; |
d43f603d KL |
968 | |
969 | /* [temp.friend/6] | |
970 | A member of a class template may be declared to be a friend of a | |
971 | non-template class. In this case, the corresponding member of | |
972 | every specialization of the class template is a friend of the | |
973 | class granting friendship. | |
974 | ||
975 | For example, given a template friend declaration | |
976 | ||
977 | template <class T> friend void A<T>::f(); | |
978 | ||
979 | the member function below is considered a friend | |
980 | ||
981 | template <> struct A<int> { | |
982 | void f(); | |
983 | }; | |
984 | ||
985 | For this type of template friend, TEMPLATE_DEPTH below will be | |
5c425df5 | 986 | nonzero. To determine if DECL is a friend of FRIEND, we first |
d43f603d KL |
987 | check if the enclosing class is a specialization of another. */ |
988 | ||
989 | template_depth = template_class_depth (DECL_CONTEXT (friend)); | |
990 | if (template_depth | |
991 | && DECL_CLASS_SCOPE_P (decl) | |
992 | && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)), | |
993 | CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend)))) | |
994 | { | |
995 | /* Next, we check the members themselves. In order to handle | |
b939a023 | 996 | a few tricky cases, such as when FRIEND's are |
d43f603d KL |
997 | |
998 | template <class T> friend void A<T>::g(T t); | |
999 | template <class T> template <T t> friend void A<T>::h(); | |
1000 | ||
b939a023 KL |
1001 | and DECL's are |
1002 | ||
1003 | void A<int>::g(int); | |
1004 | template <int> void A<int>::h(); | |
1005 | ||
1006 | we need to figure out ARGS, the template arguments from | |
1007 | the context of DECL. This is required for template substitution | |
1008 | of `T' in the function parameter of `g' and template parameter | |
1009 | of `h' in the above examples. Here ARGS corresponds to `int'. */ | |
d43f603d KL |
1010 | |
1011 | tree context = DECL_CONTEXT (decl); | |
1012 | tree args = NULL_TREE; | |
1013 | int current_depth = 0; | |
b939a023 | 1014 | |
d43f603d KL |
1015 | while (current_depth < template_depth) |
1016 | { | |
1017 | if (CLASSTYPE_TEMPLATE_INFO (context)) | |
1018 | { | |
1019 | if (current_depth == 0) | |
1020 | args = TYPE_TI_ARGS (context); | |
1021 | else | |
1022 | args = add_to_template_args (TYPE_TI_ARGS (context), args); | |
1023 | current_depth++; | |
1024 | } | |
1025 | context = TYPE_CONTEXT (context); | |
1026 | } | |
1027 | ||
1028 | if (TREE_CODE (decl) == FUNCTION_DECL) | |
1029 | { | |
1030 | bool is_template; | |
1031 | tree friend_type; | |
1032 | tree decl_type; | |
1033 | tree friend_args_type; | |
1034 | tree decl_args_type; | |
1035 | ||
1036 | /* Make sure that both DECL and FRIEND are templates or | |
1037 | non-templates. */ | |
1038 | is_template = DECL_TEMPLATE_INFO (decl) | |
1039 | && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)); | |
1040 | if (need_template ^ is_template) | |
b939a023 | 1041 | return false; |
d43f603d KL |
1042 | else if (is_template) |
1043 | { | |
da1d7781 | 1044 | /* If both are templates, check template parameter list. */ |
d43f603d KL |
1045 | tree friend_parms |
1046 | = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend), | |
1047 | args, tf_none); | |
1048 | if (!comp_template_parms | |
1049 | (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)), | |
1050 | friend_parms)) | |
b939a023 | 1051 | return false; |
d43f603d KL |
1052 | |
1053 | decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl)); | |
1054 | } | |
1055 | else | |
1056 | decl_type = TREE_TYPE (decl); | |
1057 | ||
1058 | friend_type = tsubst_function_type (TREE_TYPE (friend), args, | |
1059 | tf_none, NULL_TREE); | |
1060 | if (friend_type == error_mark_node) | |
b939a023 | 1061 | return false; |
d43f603d KL |
1062 | |
1063 | /* Check if return types match. */ | |
1064 | if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type))) | |
b939a023 | 1065 | return false; |
d43f603d KL |
1066 | |
1067 | /* Check if function parameter types match, ignoring the | |
1068 | `this' parameter. */ | |
1069 | friend_args_type = TYPE_ARG_TYPES (friend_type); | |
1070 | decl_args_type = TYPE_ARG_TYPES (decl_type); | |
1071 | if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend)) | |
1072 | friend_args_type = TREE_CHAIN (friend_args_type); | |
1073 | if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)) | |
1074 | decl_args_type = TREE_CHAIN (decl_args_type); | |
b939a023 KL |
1075 | |
1076 | return compparms (decl_args_type, friend_args_type); | |
1077 | } | |
1078 | else | |
1079 | { | |
1080 | /* DECL is a TYPE_DECL */ | |
1081 | bool is_template; | |
1082 | tree decl_type = TREE_TYPE (decl); | |
1083 | ||
1084 | /* Make sure that both DECL and FRIEND are templates or | |
1085 | non-templates. */ | |
1086 | is_template | |
1087 | = CLASSTYPE_TEMPLATE_INFO (decl_type) | |
1088 | && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type)); | |
1089 | ||
1090 | if (need_template ^ is_template) | |
1091 | return false; | |
1092 | else if (is_template) | |
1093 | { | |
1094 | tree friend_parms; | |
1095 | /* If both are templates, check the name of the two | |
1096 | TEMPLATE_DECL's first because is_friend didn't. */ | |
1097 | if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type)) | |
1098 | != DECL_NAME (friend)) | |
1099 | return false; | |
1100 | ||
1101 | /* Now check template parameter list. */ | |
1102 | friend_parms | |
1103 | = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend), | |
1104 | args, tf_none); | |
1105 | return comp_template_parms | |
1106 | (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)), | |
1107 | friend_parms); | |
1108 | } | |
1109 | else | |
1110 | return (DECL_NAME (decl) | |
1111 | == DECL_NAME (friend)); | |
d43f603d KL |
1112 | } |
1113 | } | |
b939a023 | 1114 | return false; |
d43f603d KL |
1115 | } |
1116 | ||
75650646 | 1117 | /* Register the specialization SPEC as a specialization of TMPL with |
36a117a5 MM |
1118 | the indicated ARGS. Returns SPEC, or an equivalent prior |
1119 | declaration, if available. */ | |
75650646 | 1120 | |
36a117a5 | 1121 | static tree |
3a978d72 | 1122 | register_specialization (tree spec, tree tmpl, tree args) |
75650646 | 1123 | { |
c7222c02 | 1124 | tree fn; |
75650646 | 1125 | |
50bc768d | 1126 | gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL); |
75650646 | 1127 | |
36a117a5 MM |
1128 | if (TREE_CODE (spec) == FUNCTION_DECL |
1129 | && uses_template_parms (DECL_TI_ARGS (spec))) | |
1130 | /* This is the FUNCTION_DECL for a partial instantiation. Don't | |
1131 | register it; we want the corresponding TEMPLATE_DECL instead. | |
1132 | We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than | |
1133 | the more obvious `uses_template_parms (spec)' to avoid problems | |
1134 | with default function arguments. In particular, given | |
1135 | something like this: | |
1136 | ||
1137 | template <class T> void f(T t1, T t = T()) | |
1138 | ||
1139 | the default argument expression is not substituted for in an | |
1140 | instantiation unless and until it is actually needed. */ | |
1141 | return spec; | |
28ba38e3 | 1142 | |
36a117a5 MM |
1143 | /* There should be as many levels of arguments as there are |
1144 | levels of parameters. */ | |
50bc768d NS |
1145 | gcc_assert (TMPL_ARGS_DEPTH (args) |
1146 | == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl))); | |
36a117a5 | 1147 | |
c7222c02 MM |
1148 | fn = retrieve_specialization (tmpl, args, |
1149 | /*class_specializations_p=*/false); | |
1150 | /* We can sometimes try to re-register a specialization that we've | |
1151 | already got. In particular, regenerate_decl_from_template calls | |
1152 | duplicate_decls which will update the specialization list. But, | |
1153 | we'll still get called again here anyhow. It's more convenient | |
1154 | to simply allow this than to try to prevent it. */ | |
1155 | if (fn == spec) | |
1156 | return spec; | |
1157 | else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec)) | |
22e9174f | 1158 | { |
c7222c02 | 1159 | if (DECL_TEMPLATE_INSTANTIATION (fn)) |
22e9174f | 1160 | { |
c7222c02 MM |
1161 | if (TREE_USED (fn) |
1162 | || DECL_EXPLICIT_INSTANTIATION (fn)) | |
22e9174f | 1163 | { |
c7222c02 MM |
1164 | error ("specialization of %qD after instantiation", |
1165 | fn); | |
1166 | return spec; | |
22e9174f | 1167 | } |
c7222c02 | 1168 | else |
b1cc95ce | 1169 | { |
c7222c02 MM |
1170 | /* This situation should occur only if the first |
1171 | specialization is an implicit instantiation, the | |
1172 | second is an explicit specialization, and the | |
1173 | implicit instantiation has not yet been used. That | |
1174 | situation can occur if we have implicitly | |
1175 | instantiated a member function and then specialized | |
1176 | it later. | |
1177 | ||
1178 | We can also wind up here if a friend declaration that | |
1179 | looked like an instantiation turns out to be a | |
1180 | specialization: | |
1181 | ||
1182 | template <class T> void foo(T); | |
1183 | class S { friend void foo<>(int) }; | |
1184 | template <> void foo(int); | |
1185 | ||
1186 | We transform the existing DECL in place so that any | |
1187 | pointers to it become pointers to the updated | |
1188 | declaration. | |
1189 | ||
1190 | If there was a definition for the template, but not | |
1191 | for the specialization, we want this to look as if | |
1192 | there were no definition, and vice versa. */ | |
1193 | DECL_INITIAL (fn) = NULL_TREE; | |
1194 | duplicate_decls (spec, fn); | |
b1cc95ce MM |
1195 | |
1196 | return fn; | |
1197 | } | |
22e9174f | 1198 | } |
c7222c02 MM |
1199 | else if (DECL_TEMPLATE_SPECIALIZATION (fn)) |
1200 | { | |
1201 | if (!duplicate_decls (spec, fn) && DECL_INITIAL (spec)) | |
1202 | /* Dup decl failed, but this is a new definition. Set the | |
1203 | line number so any errors match this new | |
1204 | definition. */ | |
1205 | DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec); | |
1206 | ||
1207 | return fn; | |
1208 | } | |
b1cc95ce MM |
1209 | } |
1210 | ||
1211 | /* A specialization must be declared in the same namespace as the | |
1212 | template it is specializing. */ | |
1213 | if (DECL_TEMPLATE_SPECIALIZATION (spec) | |
1214 | && !check_specialization_namespace (tmpl)) | |
1215 | DECL_CONTEXT (spec) = decl_namespace_context (tmpl); | |
75650646 | 1216 | |
c7222c02 MM |
1217 | if (!optimize_specialization_lookup_p (tmpl)) |
1218 | DECL_TEMPLATE_SPECIALIZATIONS (tmpl) | |
1219 | = tree_cons (args, spec, DECL_TEMPLATE_SPECIALIZATIONS (tmpl)); | |
36a117a5 MM |
1220 | |
1221 | return spec; | |
1222 | } | |
1223 | ||
1224 | /* Unregister the specialization SPEC as a specialization of TMPL. | |
b3445994 MM |
1225 | Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true |
1226 | if the SPEC was listed as a specialization of TMPL. */ | |
36a117a5 | 1227 | |
b3445994 MM |
1228 | bool |
1229 | reregister_specialization (tree spec, tree tmpl, tree new_spec) | |
36a117a5 MM |
1230 | { |
1231 | tree* s; | |
1232 | ||
1233 | for (s = &DECL_TEMPLATE_SPECIALIZATIONS (tmpl); | |
1234 | *s != NULL_TREE; | |
1235 | s = &TREE_CHAIN (*s)) | |
1236 | if (TREE_VALUE (*s) == spec) | |
1237 | { | |
b3445994 MM |
1238 | if (!new_spec) |
1239 | *s = TREE_CHAIN (*s); | |
1240 | else | |
a3d87771 | 1241 | TREE_VALUE (*s) = new_spec; |
36a117a5 MM |
1242 | return 1; |
1243 | } | |
1244 | ||
1245 | return 0; | |
75650646 MM |
1246 | } |
1247 | ||
a723baf1 MM |
1248 | /* Compare an entry in the local specializations hash table P1 (which |
1249 | is really a pointer to a TREE_LIST) with P2 (which is really a | |
1250 | DECL). */ | |
1251 | ||
1252 | static int | |
1253 | eq_local_specializations (const void *p1, const void *p2) | |
1254 | { | |
1255 | return TREE_VALUE ((tree) p1) == (tree) p2; | |
1256 | } | |
1257 | ||
69f794a7 MM |
1258 | /* Hash P1, an entry in the local specializations table. */ |
1259 | ||
1260 | static hashval_t | |
1261 | hash_local_specialization (const void* p1) | |
1262 | { | |
1263 | return htab_hash_pointer (TREE_VALUE ((tree) p1)); | |
1264 | } | |
1265 | ||
6dfbb909 MM |
1266 | /* Like register_specialization, but for local declarations. We are |
1267 | registering SPEC, an instantiation of TMPL. */ | |
9188c363 | 1268 | |
414ea4aa | 1269 | static void |
3a978d72 | 1270 | register_local_specialization (tree spec, tree tmpl) |
9188c363 | 1271 | { |
6dfbb909 MM |
1272 | void **slot; |
1273 | ||
69f794a7 MM |
1274 | slot = htab_find_slot_with_hash (local_specializations, tmpl, |
1275 | htab_hash_pointer (tmpl), INSERT); | |
a723baf1 | 1276 | *slot = build_tree_list (spec, tmpl); |
9188c363 MM |
1277 | } |
1278 | ||
e1467ff2 MM |
1279 | /* Print the list of candidate FNS in an error message. */ |
1280 | ||
104bf76a | 1281 | void |
3a978d72 | 1282 | print_candidates (tree fns) |
e1467ff2 MM |
1283 | { |
1284 | tree fn; | |
1285 | ||
d8e178a0 | 1286 | const char *str = "candidates are:"; |
e1467ff2 MM |
1287 | |
1288 | for (fn = fns; fn != NULL_TREE; fn = TREE_CHAIN (fn)) | |
1289 | { | |
d6479fe7 MM |
1290 | tree f; |
1291 | ||
1292 | for (f = TREE_VALUE (fn); f; f = OVL_NEXT (f)) | |
1293 | cp_error_at ("%s %+#D", str, OVL_CURRENT (f)); | |
e1467ff2 MM |
1294 | str = " "; |
1295 | } | |
1296 | } | |
1297 | ||
75650646 | 1298 | /* Returns the template (one of the functions given by TEMPLATE_ID) |
e1467ff2 | 1299 | which can be specialized to match the indicated DECL with the |
03017874 MM |
1300 | explicit template args given in TEMPLATE_ID. The DECL may be |
1301 | NULL_TREE if none is available. In that case, the functions in | |
1302 | TEMPLATE_ID are non-members. | |
1303 | ||
838dfd8a | 1304 | If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a |
03017874 MM |
1305 | specialization of a member template. |
1306 | ||
5fe7b654 GB |
1307 | The TEMPLATE_COUNT is the number of references to qualifying |
1308 | template classes that appeared in the name of the function. See | |
1309 | check_explicit_specialization for a more accurate description. | |
1310 | ||
03017874 MM |
1311 | The template args (those explicitly specified and those deduced) |
1312 | are output in a newly created vector *TARGS_OUT. | |
1313 | ||
1314 | If it is impossible to determine the result, an error message is | |
bf8f3f93 | 1315 | issued. The error_mark_node is returned to indicate failure. */ |
75650646 | 1316 | |
e9659ab0 | 1317 | static tree |
3a978d72 NN |
1318 | determine_specialization (tree template_id, |
1319 | tree decl, | |
1320 | tree* targs_out, | |
5fe7b654 GB |
1321 | int need_member_template, |
1322 | int template_count) | |
386b8a85 | 1323 | { |
03017874 MM |
1324 | tree fns; |
1325 | tree targs; | |
1326 | tree explicit_targs; | |
1327 | tree candidates = NULL_TREE; | |
1328 | tree templates = NULL_TREE; | |
5fe7b654 GB |
1329 | int header_count; |
1330 | struct cp_binding_level *b; | |
386b8a85 | 1331 | |
e1467ff2 MM |
1332 | *targs_out = NULL_TREE; |
1333 | ||
f2e48b67 BK |
1334 | if (template_id == error_mark_node) |
1335 | return error_mark_node; | |
1336 | ||
1337 | fns = TREE_OPERAND (template_id, 0); | |
03017874 | 1338 | explicit_targs = TREE_OPERAND (template_id, 1); |
f2e48b67 | 1339 | |
27fafc8d JM |
1340 | if (fns == error_mark_node) |
1341 | return error_mark_node; | |
1342 | ||
c6002625 | 1343 | /* Check for baselinks. */ |
91e490ab | 1344 | if (BASELINK_P (fns)) |
da15dae6 | 1345 | fns = BASELINK_FUNCTIONS (fns); |
386b8a85 | 1346 | |
91e490ab MM |
1347 | if (!is_overloaded_fn (fns)) |
1348 | { | |
0f51ccfc | 1349 | error ("%qD is not a function template", fns); |
91e490ab MM |
1350 | return error_mark_node; |
1351 | } | |
1352 | ||
5fe7b654 GB |
1353 | /* Count the number of template headers specified for this |
1354 | specialization. */ | |
1355 | header_count = 0; | |
1356 | for (b = current_binding_level; | |
1357 | b->kind == sk_template_parms || b->kind == sk_template_spec; | |
1358 | b = b->level_chain) | |
1359 | ++header_count; | |
1360 | ||
2c73f9f5 | 1361 | for (; fns; fns = OVL_NEXT (fns)) |
386b8a85 | 1362 | { |
9dfce8fd | 1363 | tree fn = OVL_CURRENT (fns); |
03017874 MM |
1364 | |
1365 | if (TREE_CODE (fn) == TEMPLATE_DECL) | |
d955f6ea KL |
1366 | { |
1367 | tree decl_arg_types; | |
0da99d4e | 1368 | tree fn_arg_types; |
d955f6ea KL |
1369 | |
1370 | /* DECL might be a specialization of FN. */ | |
1371 | ||
1372 | /* Adjust the type of DECL in case FN is a static member. */ | |
1373 | decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl)); | |
1374 | if (DECL_STATIC_FUNCTION_P (fn) | |
1375 | && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)) | |
1376 | decl_arg_types = TREE_CHAIN (decl_arg_types); | |
1377 | ||
1378 | /* Check that the number of function parameters matches. | |
1379 | For example, | |
1380 | template <class T> void f(int i = 0); | |
1381 | template <> void f<int>(); | |
0e339752 | 1382 | The specialization f<int> is invalid but is not caught |
d955f6ea KL |
1383 | by get_bindings below. */ |
1384 | ||
0da99d4e GB |
1385 | fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn)); |
1386 | if (list_length (fn_arg_types) != list_length (decl_arg_types)) | |
1387 | continue; | |
1388 | ||
1389 | /* For a non-static member function, we need to make sure that | |
1390 | the const qualification is the same. This can be done by | |
1391 | checking the 'this' in the argument list. */ | |
1392 | if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn) | |
1393 | && !same_type_p (TREE_VALUE (fn_arg_types), | |
1394 | TREE_VALUE (decl_arg_types))) | |
d955f6ea KL |
1395 | continue; |
1396 | ||
5fe7b654 GB |
1397 | /* In case of explicit specialization, we need to check if |
1398 | the number of template headers appearing in the specialization | |
1399 | is correct. This is usually done in check_explicit_specialization, | |
1400 | but the check done there cannot be exhaustive when specializing | |
1401 | member functions. Consider the following code: | |
1402 | ||
1403 | template <> void A<int>::f(int); | |
1404 | template <> template <> void A<int>::f(int); | |
1405 | ||
1406 | Assuming that A<int> is not itself an explicit specialization | |
1407 | already, the first line specializes "f" which is a non-template | |
1408 | member function, whilst the second line specializes "f" which | |
1409 | is a template member function. So both lines are syntactically | |
1410 | correct, and check_explicit_specialization does not reject | |
1411 | them. | |
1412 | ||
1413 | Here, we can do better, as we are matching the specialization | |
1414 | against the declarations. We count the number of template | |
1415 | headers, and we check if they match TEMPLATE_COUNT + 1 | |
1416 | (TEMPLATE_COUNT is the number of qualifying template classes, | |
1417 | plus there must be another header for the member template | |
1418 | itself). | |
1419 | ||
1420 | Notice that if header_count is zero, this is not a | |
1421 | specialization but rather a template instantiation, so there | |
1422 | is no check we can perform here. */ | |
1423 | if (header_count && header_count != template_count + 1) | |
1424 | continue; | |
1425 | ||
d955f6ea KL |
1426 | /* See whether this function might be a specialization of this |
1427 | template. */ | |
1428 | targs = get_bindings (fn, decl, explicit_targs); | |
1429 | ||
1430 | if (!targs) | |
1431 | /* We cannot deduce template arguments that when used to | |
1432 | specialize TMPL will produce DECL. */ | |
1433 | continue; | |
1434 | ||
1435 | /* Save this template, and the arguments deduced. */ | |
1436 | templates = tree_cons (targs, fn, templates); | |
1437 | } | |
03017874 MM |
1438 | else if (need_member_template) |
1439 | /* FN is an ordinary member function, and we need a | |
1440 | specialization of a member template. */ | |
d955f6ea | 1441 | ; |
03017874 MM |
1442 | else if (TREE_CODE (fn) != FUNCTION_DECL) |
1443 | /* We can get IDENTIFIER_NODEs here in certain erroneous | |
1444 | cases. */ | |
d955f6ea | 1445 | ; |
03017874 MM |
1446 | else if (!DECL_FUNCTION_MEMBER_P (fn)) |
1447 | /* This is just an ordinary non-member function. Nothing can | |
1448 | be a specialization of that. */ | |
d955f6ea | 1449 | ; |
3b82c249 KL |
1450 | else if (DECL_ARTIFICIAL (fn)) |
1451 | /* Cannot specialize functions that are created implicitly. */ | |
d955f6ea | 1452 | ; |
75650646 | 1453 | else |
03017874 MM |
1454 | { |
1455 | tree decl_arg_types; | |
386b8a85 | 1456 | |
03017874 MM |
1457 | /* This is an ordinary member function. However, since |
1458 | we're here, we can assume it's enclosing class is a | |
1459 | template class. For example, | |
1460 | ||
1461 | template <typename T> struct S { void f(); }; | |
1462 | template <> void S<int>::f() {} | |
1463 | ||
1464 | Here, S<int>::f is a non-template, but S<int> is a | |
1465 | template class. If FN has the same type as DECL, we | |
1466 | might be in business. */ | |
f5d47abd KL |
1467 | |
1468 | if (!DECL_TEMPLATE_INFO (fn)) | |
1469 | /* Its enclosing class is an explicit specialization | |
1470 | of a template class. This is not a candidate. */ | |
1471 | continue; | |
1472 | ||
03017874 MM |
1473 | if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)), |
1474 | TREE_TYPE (TREE_TYPE (fn)))) | |
1475 | /* The return types differ. */ | |
1476 | continue; | |
1477 | ||
1478 | /* Adjust the type of DECL in case FN is a static member. */ | |
1479 | decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl)); | |
1480 | if (DECL_STATIC_FUNCTION_P (fn) | |
1481 | && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)) | |
1482 | decl_arg_types = TREE_CHAIN (decl_arg_types); | |
1483 | ||
1484 | if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)), | |
1485 | decl_arg_types)) | |
1486 | /* They match! */ | |
1487 | candidates = tree_cons (NULL_TREE, fn, candidates); | |
03017874 | 1488 | } |
386b8a85 | 1489 | } |
03017874 | 1490 | |
bf8f3f93 | 1491 | if (templates && TREE_CHAIN (templates)) |
386b8a85 | 1492 | { |
03017874 MM |
1493 | /* We have: |
1494 | ||
1495 | [temp.expl.spec] | |
1496 | ||
1497 | It is possible for a specialization with a given function | |
1498 | signature to be instantiated from more than one function | |
1499 | template. In such cases, explicit specification of the | |
1500 | template arguments must be used to uniquely identify the | |
1501 | function template specialization being specialized. | |
1502 | ||
1503 | Note that here, there's no suggestion that we're supposed to | |
1504 | determine which of the candidate templates is most | |
1505 | specialized. However, we, also have: | |
1506 | ||
1507 | [temp.func.order] | |
1508 | ||
1509 | Partial ordering of overloaded function template | |
1510 | declarations is used in the following contexts to select | |
1511 | the function template to which a function template | |
1512 | specialization refers: | |
1513 | ||
1514 | -- when an explicit specialization refers to a function | |
1515 | template. | |
1516 | ||
1517 | So, we do use the partial ordering rules, at least for now. | |
0e339752 | 1518 | This extension can only serve to make invalid programs valid, |
03017874 MM |
1519 | so it's safe. And, there is strong anecdotal evidence that |
1520 | the committee intended the partial ordering rules to apply; | |
1521 | the EDG front-end has that behavior, and John Spicer claims | |
1522 | that the committee simply forgot to delete the wording in | |
1523 | [temp.expl.spec]. */ | |
1524 | tree tmpl = most_specialized (templates, decl, explicit_targs); | |
1525 | if (tmpl && tmpl != error_mark_node) | |
1526 | { | |
1527 | targs = get_bindings (tmpl, decl, explicit_targs); | |
e1b3e07d | 1528 | templates = tree_cons (targs, tmpl, NULL_TREE); |
03017874 | 1529 | } |
e1467ff2 MM |
1530 | } |
1531 | ||
03017874 | 1532 | if (templates == NULL_TREE && candidates == NULL_TREE) |
e1467ff2 | 1533 | { |
0f51ccfc GDR |
1534 | cp_error_at ("template-id %qD for %q+D does not match any template " |
1535 | "declaration", | |
bf8f3f93 | 1536 | template_id, decl); |
03017874 | 1537 | return error_mark_node; |
386b8a85 | 1538 | } |
03017874 | 1539 | else if ((templates && TREE_CHAIN (templates)) |
bf8f3f93 MM |
1540 | || (candidates && TREE_CHAIN (candidates)) |
1541 | || (templates && candidates)) | |
386b8a85 | 1542 | { |
0f51ccfc | 1543 | cp_error_at ("ambiguous template specialization %qD for %q+D", |
bf8f3f93 MM |
1544 | template_id, decl); |
1545 | chainon (candidates, templates); | |
1546 | print_candidates (candidates); | |
03017874 | 1547 | return error_mark_node; |
386b8a85 JM |
1548 | } |
1549 | ||
c6002625 | 1550 | /* We have one, and exactly one, match. */ |
03017874 MM |
1551 | if (candidates) |
1552 | { | |
1553 | /* It was a specialization of an ordinary member function in a | |
1554 | template class. */ | |
1555 | *targs_out = copy_node (DECL_TI_ARGS (TREE_VALUE (candidates))); | |
1556 | return DECL_TI_TEMPLATE (TREE_VALUE (candidates)); | |
1557 | } | |
1558 | ||
1559 | /* It was a specialization of a template. */ | |
17aec3eb | 1560 | targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates))); |
03017874 MM |
1561 | if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs)) |
1562 | { | |
1563 | *targs_out = copy_node (targs); | |
1564 | SET_TMPL_ARGS_LEVEL (*targs_out, | |
1565 | TMPL_ARGS_DEPTH (*targs_out), | |
1566 | TREE_PURPOSE (templates)); | |
1567 | } | |
1568 | else | |
1569 | *targs_out = TREE_PURPOSE (templates); | |
e1467ff2 | 1570 | return TREE_VALUE (templates); |
8d08fdba | 1571 | } |
8afa707f MM |
1572 | |
1573 | /* Returns a chain of parameter types, exactly like the SPEC_TYPES, | |
1574 | but with the default argument values filled in from those in the | |
1575 | TMPL_TYPES. */ | |
6757edfe | 1576 | |
8afa707f | 1577 | static tree |
3a978d72 NN |
1578 | copy_default_args_to_explicit_spec_1 (tree spec_types, |
1579 | tree tmpl_types) | |
8afa707f MM |
1580 | { |
1581 | tree new_spec_types; | |
1582 | ||
1583 | if (!spec_types) | |
1584 | return NULL_TREE; | |
1585 | ||
1586 | if (spec_types == void_list_node) | |
1587 | return void_list_node; | |
1588 | ||
1589 | /* Substitute into the rest of the list. */ | |
1590 | new_spec_types = | |
1591 | copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types), | |
1592 | TREE_CHAIN (tmpl_types)); | |
1593 | ||
1594 | /* Add the default argument for this parameter. */ | |
1595 | return hash_tree_cons (TREE_PURPOSE (tmpl_types), | |
1596 | TREE_VALUE (spec_types), | |
1597 | new_spec_types); | |
1598 | } | |
1599 | ||
1600 | /* DECL is an explicit specialization. Replicate default arguments | |
1601 | from the template it specializes. (That way, code like: | |
1602 | ||
1603 | template <class T> void f(T = 3); | |
1604 | template <> void f(double); | |
1605 | void g () { f (); } | |
1606 | ||
1607 | works, as required.) An alternative approach would be to look up | |
1608 | the correct default arguments at the call-site, but this approach | |
1609 | is consistent with how implicit instantiations are handled. */ | |
1610 | ||
1611 | static void | |
3a978d72 | 1612 | copy_default_args_to_explicit_spec (tree decl) |
8afa707f MM |
1613 | { |
1614 | tree tmpl; | |
1615 | tree spec_types; | |
1616 | tree tmpl_types; | |
1617 | tree new_spec_types; | |
1618 | tree old_type; | |
1619 | tree new_type; | |
1620 | tree t; | |
08c2df0f NS |
1621 | tree object_type = NULL_TREE; |
1622 | tree in_charge = NULL_TREE; | |
e0fff4b3 | 1623 | tree vtt = NULL_TREE; |
8afa707f MM |
1624 | |
1625 | /* See if there's anything we need to do. */ | |
1626 | tmpl = DECL_TI_TEMPLATE (decl); | |
1627 | tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl))); | |
1628 | for (t = tmpl_types; t; t = TREE_CHAIN (t)) | |
1629 | if (TREE_PURPOSE (t)) | |
1630 | break; | |
1631 | if (!t) | |
1632 | return; | |
1633 | ||
1634 | old_type = TREE_TYPE (decl); | |
1635 | spec_types = TYPE_ARG_TYPES (old_type); | |
1636 | ||
8afa707f MM |
1637 | if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)) |
1638 | { | |
08c2df0f NS |
1639 | /* Remove the this pointer, but remember the object's type for |
1640 | CV quals. */ | |
1641 | object_type = TREE_TYPE (TREE_VALUE (spec_types)); | |
8afa707f MM |
1642 | spec_types = TREE_CHAIN (spec_types); |
1643 | tmpl_types = TREE_CHAIN (tmpl_types); | |
08c2df0f | 1644 | |
8afa707f | 1645 | if (DECL_HAS_IN_CHARGE_PARM_P (decl)) |
08c2df0f NS |
1646 | { |
1647 | /* DECL may contain more parameters than TMPL due to the extra | |
1648 | in-charge parameter in constructors and destructors. */ | |
1649 | in_charge = spec_types; | |
1650 | spec_types = TREE_CHAIN (spec_types); | |
1651 | } | |
e0fff4b3 JM |
1652 | if (DECL_HAS_VTT_PARM_P (decl)) |
1653 | { | |
1654 | vtt = spec_types; | |
1655 | spec_types = TREE_CHAIN (spec_types); | |
1656 | } | |
8afa707f MM |
1657 | } |
1658 | ||
1659 | /* Compute the merged default arguments. */ | |
1660 | new_spec_types = | |
1661 | copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types); | |
1662 | ||
08c2df0f NS |
1663 | /* Compute the new FUNCTION_TYPE. */ |
1664 | if (object_type) | |
8afa707f | 1665 | { |
e0fff4b3 JM |
1666 | if (vtt) |
1667 | new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt), | |
1668 | TREE_VALUE (vtt), | |
1669 | new_spec_types); | |
1670 | ||
08c2df0f NS |
1671 | if (in_charge) |
1672 | /* Put the in-charge parameter back. */ | |
1673 | new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge), | |
1674 | TREE_VALUE (in_charge), | |
1675 | new_spec_types); | |
1676 | ||
43dc123f MM |
1677 | new_type = build_method_type_directly (object_type, |
1678 | TREE_TYPE (old_type), | |
1679 | new_spec_types); | |
8afa707f | 1680 | } |
8afa707f MM |
1681 | else |
1682 | new_type = build_function_type (TREE_TYPE (old_type), | |
08c2df0f | 1683 | new_spec_types); |
e9525111 MM |
1684 | new_type = cp_build_type_attribute_variant (new_type, |
1685 | TYPE_ATTRIBUTES (old_type)); | |
8afa707f MM |
1686 | new_type = build_exception_variant (new_type, |
1687 | TYPE_RAISES_EXCEPTIONS (old_type)); | |
1688 | TREE_TYPE (decl) = new_type; | |
1689 | } | |
1690 | ||
386b8a85 | 1691 | /* Check to see if the function just declared, as indicated in |
75650646 MM |
1692 | DECLARATOR, and in DECL, is a specialization of a function |
1693 | template. We may also discover that the declaration is an explicit | |
1694 | instantiation at this point. | |
1695 | ||
e1467ff2 | 1696 | Returns DECL, or an equivalent declaration that should be used |
03017874 MM |
1697 | instead if all goes well. Issues an error message if something is |
1698 | amiss. Returns error_mark_node if the error is not easily | |
1699 | recoverable. | |
75650646 | 1700 | |
75650646 MM |
1701 | FLAGS is a bitmask consisting of the following flags: |
1702 | ||
75650646 MM |
1703 | 2: The function has a definition. |
1704 | 4: The function is a friend. | |
75650646 MM |
1705 | |
1706 | The TEMPLATE_COUNT is the number of references to qualifying | |
1707 | template classes that appeared in the name of the function. For | |
1708 | example, in | |
1709 | ||
1710 | template <class T> struct S { void f(); }; | |
1711 | void S<int>::f(); | |
1712 | ||
1713 | the TEMPLATE_COUNT would be 1. However, explicitly specialized | |
1714 | classes are not counted in the TEMPLATE_COUNT, so that in | |
1715 | ||
1716 | template <class T> struct S {}; | |
1717 | template <> struct S<int> { void f(); } | |
36a117a5 | 1718 | template <> void S<int>::f(); |
75650646 MM |
1719 | |
1720 | the TEMPLATE_COUNT would be 0. (Note that this declaration is | |
0e339752 | 1721 | invalid; there should be no template <>.) |
75650646 MM |
1722 | |
1723 | If the function is a specialization, it is marked as such via | |
1724 | DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO | |
1725 | is set up correctly, and it is added to the list of specializations | |
1726 | for that template. */ | |
386b8a85 | 1727 | |
e1467ff2 | 1728 | tree |
3a978d72 NN |
1729 | check_explicit_specialization (tree declarator, |
1730 | tree decl, | |
1731 | int template_count, | |
1732 | int flags) | |
386b8a85 | 1733 | { |
75650646 MM |
1734 | int have_def = flags & 2; |
1735 | int is_friend = flags & 4; | |
1736 | int specialization = 0; | |
e1467ff2 | 1737 | int explicit_instantiation = 0; |
fd4de5ff | 1738 | int member_specialization = 0; |
75650646 MM |
1739 | tree ctype = DECL_CLASS_CONTEXT (decl); |
1740 | tree dname = DECL_NAME (decl); | |
74b846e0 | 1741 | tmpl_spec_kind tsk; |
386b8a85 | 1742 | |
f65b7de3 GB |
1743 | if (is_friend) |
1744 | { | |
1745 | if (!processing_specialization) | |
1746 | tsk = tsk_none; | |
1747 | else | |
1748 | tsk = tsk_excessive_parms; | |
1749 | } | |
1750 | else | |
1751 | tsk = current_tmpl_spec_kind (template_count); | |
75650646 | 1752 | |
74b846e0 MM |
1753 | switch (tsk) |
1754 | { | |
1755 | case tsk_none: | |
1756 | if (processing_specialization) | |
75650646 | 1757 | { |
fd4de5ff MM |
1758 | specialization = 1; |
1759 | SET_DECL_TEMPLATE_SPECIALIZATION (decl); | |
75650646 | 1760 | } |
74b846e0 | 1761 | else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR) |
386b8a85 | 1762 | { |
74b846e0 MM |
1763 | if (is_friend) |
1764 | /* This could be something like: | |
75650646 | 1765 | |
74b846e0 MM |
1766 | template <class T> void f(T); |
1767 | class S { friend void f<>(int); } */ | |
1768 | specialization = 1; | |
1769 | else | |
1770 | { | |
1771 | /* This case handles bogus declarations like template <> | |
1772 | template <class T> void f<int>(); */ | |
1773 | ||
0f51ccfc GDR |
1774 | error ("template-id %qD in declaration of primary template", |
1775 | declarator); | |
74b846e0 MM |
1776 | return decl; |
1777 | } | |
1778 | } | |
1779 | break; | |
1780 | ||
1781 | case tsk_invalid_member_spec: | |
1782 | /* The error has already been reported in | |
1783 | check_specialization_scope. */ | |
1784 | return error_mark_node; | |
1785 | ||
1786 | case tsk_invalid_expl_inst: | |
33bd39a2 | 1787 | error ("template parameter list used in explicit instantiation"); |
74b846e0 MM |
1788 | |
1789 | /* Fall through. */ | |
1790 | ||
1791 | case tsk_expl_inst: | |
fd4de5ff | 1792 | if (have_def) |
33bd39a2 | 1793 | error ("definition provided for explicit instantiation"); |
74b846e0 | 1794 | |
fd4de5ff | 1795 | explicit_instantiation = 1; |
74b846e0 | 1796 | break; |
fd4de5ff | 1797 | |
74b846e0 | 1798 | case tsk_excessive_parms: |
f03adc6b GB |
1799 | case tsk_insufficient_parms: |
1800 | if (tsk == tsk_excessive_parms) | |
0f51ccfc | 1801 | error ("too many template parameter lists in declaration of %qD", |
f03adc6b GB |
1802 | decl); |
1803 | else if (template_header_count) | |
0f51ccfc | 1804 | error("too few template parameter lists in declaration of %qD", decl); |
f03adc6b | 1805 | else |
0f51ccfc | 1806 | error("explicit specialization of %qD must be introduced by " |
9e637a26 | 1807 | "%<template <>%>", decl); |
75650646 | 1808 | |
74b846e0 MM |
1809 | /* Fall through. */ |
1810 | case tsk_expl_spec: | |
1811 | SET_DECL_TEMPLATE_SPECIALIZATION (decl); | |
1812 | if (ctype) | |
1813 | member_specialization = 1; | |
1814 | else | |
1815 | specialization = 1; | |
1816 | break; | |
74b846e0 MM |
1817 | |
1818 | case tsk_template: | |
1819 | if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR) | |
c6f2ed0d | 1820 | { |
fd4de5ff MM |
1821 | /* This case handles bogus declarations like template <> |
1822 | template <class T> void f<int>(); */ | |
75650646 | 1823 | |
655dc6ee | 1824 | if (uses_template_parms (declarator)) |
0f51ccfc | 1825 | error ("function template partial specialization %qD " |
e1e93ad8 | 1826 | "is not allowed", declarator); |
655dc6ee | 1827 | else |
0f51ccfc GDR |
1828 | error ("template-id %qD in declaration of primary template", |
1829 | declarator); | |
fd4de5ff | 1830 | return decl; |
386b8a85 | 1831 | } |
74b846e0 MM |
1832 | |
1833 | if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype)) | |
1834 | /* This is a specialization of a member template, without | |
1835 | specialization the containing class. Something like: | |
1836 | ||
1837 | template <class T> struct S { | |
1838 | template <class U> void f (U); | |
1839 | }; | |
1840 | template <> template <class U> void S<int>::f(U) {} | |
1841 | ||
1842 | That's a specialization -- but of the entire template. */ | |
1843 | specialization = 1; | |
1844 | break; | |
1845 | ||
1846 | default: | |
315fb5db | 1847 | gcc_unreachable (); |
75650646 | 1848 | } |
386b8a85 | 1849 | |
670960ac JM |
1850 | if (specialization || member_specialization) |
1851 | { | |
1852 | tree t = TYPE_ARG_TYPES (TREE_TYPE (decl)); | |
1853 | for (; t; t = TREE_CHAIN (t)) | |
1854 | if (TREE_PURPOSE (t)) | |
1855 | { | |
33bd39a2 | 1856 | pedwarn |
8251199e | 1857 | ("default argument specified in explicit specialization"); |
670960ac JM |
1858 | break; |
1859 | } | |
f3400fe2 | 1860 | if (current_lang_name == lang_name_c) |
33bd39a2 | 1861 | error ("template specialization with C linkage"); |
670960ac JM |
1862 | } |
1863 | ||
e1467ff2 | 1864 | if (specialization || member_specialization || explicit_instantiation) |
75650646 MM |
1865 | { |
1866 | tree tmpl = NULL_TREE; | |
1867 | tree targs = NULL_TREE; | |
75650646 MM |
1868 | |
1869 | /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */ | |
386b8a85 JM |
1870 | if (TREE_CODE (declarator) != TEMPLATE_ID_EXPR) |
1871 | { | |
1872 | tree fns; | |
1873 | ||
50bc768d | 1874 | gcc_assert (TREE_CODE (declarator) == IDENTIFIER_NODE); |
2f54a1db | 1875 | if (ctype) |
386b8a85 | 1876 | fns = dname; |
2f54a1db GB |
1877 | else |
1878 | { | |
1879 | /* If there is no class context, the explicit instantiation | |
1880 | must be at namespace scope. */ | |
50bc768d | 1881 | gcc_assert (DECL_NAMESPACE_SCOPE_P (decl)); |
2f54a1db GB |
1882 | |
1883 | /* Find the namespace binding, using the declaration | |
1884 | context. */ | |
1885 | fns = namespace_binding (dname, CP_DECL_CONTEXT (decl)); | |
1886 | } | |
386b8a85 | 1887 | |
2f54a1db | 1888 | declarator = lookup_template_function (fns, NULL_TREE); |
386b8a85 JM |
1889 | } |
1890 | ||
f2e48b67 BK |
1891 | if (declarator == error_mark_node) |
1892 | return error_mark_node; | |
1893 | ||
75650646 MM |
1894 | if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype)) |
1895 | { | |
8ec2ac34 | 1896 | if (!explicit_instantiation) |
0e339752 | 1897 | /* A specialization in class scope. This is invalid, |
6c30752f MM |
1898 | but the error will already have been flagged by |
1899 | check_specialization_scope. */ | |
1900 | return error_mark_node; | |
8ec2ac34 | 1901 | else |
b370501f | 1902 | { |
0e339752 | 1903 | /* It's not valid to write an explicit instantiation in |
b370501f | 1904 | class scope, e.g.: |
8ec2ac34 | 1905 | |
b370501f | 1906 | class C { template void f(); } |
8ec2ac34 | 1907 | |
b370501f KG |
1908 | This case is caught by the parser. However, on |
1909 | something like: | |
8ec2ac34 | 1910 | |
b370501f | 1911 | template class C { void f(); }; |
8ec2ac34 | 1912 | |
0e339752 | 1913 | (which is invalid) we can get here. The error will be |
b370501f KG |
1914 | issued later. */ |
1915 | ; | |
1916 | } | |
8ec2ac34 | 1917 | |
e1467ff2 | 1918 | return decl; |
75650646 MM |
1919 | } |
1920 | else if (ctype != NULL_TREE | |
1921 | && (TREE_CODE (TREE_OPERAND (declarator, 0)) == | |
1922 | IDENTIFIER_NODE)) | |
386b8a85 | 1923 | { |
75650646 MM |
1924 | /* Find the list of functions in ctype that have the same |
1925 | name as the declared function. */ | |
1926 | tree name = TREE_OPERAND (declarator, 0); | |
03017874 MM |
1927 | tree fns = NULL_TREE; |
1928 | int idx; | |
1929 | ||
8ba658ee | 1930 | if (constructor_name_p (name, ctype)) |
386b8a85 | 1931 | { |
75650646 MM |
1932 | int is_constructor = DECL_CONSTRUCTOR_P (decl); |
1933 | ||
1934 | if (is_constructor ? !TYPE_HAS_CONSTRUCTOR (ctype) | |
1935 | : !TYPE_HAS_DESTRUCTOR (ctype)) | |
1936 | { | |
1937 | /* From [temp.expl.spec]: | |
e1467ff2 | 1938 | |
75650646 MM |
1939 | If such an explicit specialization for the member |
1940 | of a class template names an implicitly-declared | |
1941 | special member function (clause _special_), the | |
e1467ff2 MM |
1942 | program is ill-formed. |
1943 | ||
1944 | Similar language is found in [temp.explicit]. */ | |
33bd39a2 | 1945 | error ("specialization of implicitly-declared special member function"); |
03017874 | 1946 | return error_mark_node; |
75650646 | 1947 | } |
386b8a85 | 1948 | |
42da2fd8 | 1949 | name = is_constructor ? ctor_identifier : dtor_identifier; |
75650646 | 1950 | } |
42da2fd8 | 1951 | |
421844e7 | 1952 | if (!DECL_CONV_FN_P (decl)) |
03017874 MM |
1953 | { |
1954 | idx = lookup_fnfields_1 (ctype, name); | |
1955 | if (idx >= 0) | |
aaaa46d2 | 1956 | fns = VEC_index (tree, CLASSTYPE_METHOD_VEC (ctype), idx); |
03017874 MM |
1957 | } |
1958 | else | |
1959 | { | |
aaaa46d2 MM |
1960 | VEC(tree) *methods; |
1961 | tree ovl; | |
03017874 MM |
1962 | |
1963 | /* For a type-conversion operator, we cannot do a | |
1964 | name-based lookup. We might be looking for `operator | |
1965 | int' which will be a specialization of `operator T'. | |
1966 | So, we find *all* the conversion operators, and then | |
1967 | select from them. */ | |
1968 | fns = NULL_TREE; | |
1969 | ||
1970 | methods = CLASSTYPE_METHOD_VEC (ctype); | |
1971 | if (methods) | |
5dd236e2 | 1972 | for (idx = CLASSTYPE_FIRST_CONVERSION_SLOT; |
9ba5ff0f | 1973 | VEC_iterate (tree, methods, idx, ovl); |
aaaa46d2 | 1974 | ++idx) |
03017874 | 1975 | { |
aaaa46d2 | 1976 | if (!DECL_CONV_FN_P (OVL_CURRENT (ovl))) |
03017874 MM |
1977 | /* There are no more conversion functions. */ |
1978 | break; | |
1979 | ||
1980 | /* Glue all these conversion functions together | |
1981 | with those we already have. */ | |
1982 | for (; ovl; ovl = OVL_NEXT (ovl)) | |
1983 | fns = ovl_cons (OVL_CURRENT (ovl), fns); | |
1984 | } | |
1985 | } | |
1986 | ||
386b8a85 JM |
1987 | if (fns == NULL_TREE) |
1988 | { | |
0f51ccfc | 1989 | error ("no member function %qD declared in %qT", name, ctype); |
03017874 | 1990 | return error_mark_node; |
386b8a85 JM |
1991 | } |
1992 | else | |
1993 | TREE_OPERAND (declarator, 0) = fns; | |
1994 | } | |
75650646 | 1995 | |
e1467ff2 MM |
1996 | /* Figure out what exactly is being specialized at this point. |
1997 | Note that for an explicit instantiation, even one for a | |
38e01259 | 1998 | member function, we cannot tell apriori whether the |
e1467ff2 | 1999 | instantiation is for a member template, or just a member |
36a117a5 MM |
2000 | function of a template class. Even if a member template is |
2001 | being instantiated, the member template arguments may be | |
2002 | elided if they can be deduced from the rest of the | |
2003 | declaration. */ | |
e1467ff2 MM |
2004 | tmpl = determine_specialization (declarator, decl, |
2005 | &targs, | |
5fe7b654 GB |
2006 | member_specialization, |
2007 | template_count); | |
386b8a85 | 2008 | |
03017874 MM |
2009 | if (!tmpl || tmpl == error_mark_node) |
2010 | /* We couldn't figure out what this declaration was | |
2011 | specializing. */ | |
2012 | return error_mark_node; | |
2013 | else | |
386b8a85 | 2014 | { |
25aab5d0 | 2015 | tree gen_tmpl = most_general_template (tmpl); |
36a117a5 | 2016 | |
e1467ff2 MM |
2017 | if (explicit_instantiation) |
2018 | { | |
03d0f4af | 2019 | /* We don't set DECL_EXPLICIT_INSTANTIATION here; that |
25aab5d0 MM |
2020 | is done by do_decl_instantiation later. */ |
2021 | ||
2022 | int arg_depth = TMPL_ARGS_DEPTH (targs); | |
2023 | int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)); | |
2024 | ||
2025 | if (arg_depth > parm_depth) | |
2026 | { | |
2027 | /* If TMPL is not the most general template (for | |
2028 | example, if TMPL is a friend template that is | |
2029 | injected into namespace scope), then there will | |
dc957d14 | 2030 | be too many levels of TARGS. Remove some of them |
25aab5d0 MM |
2031 | here. */ |
2032 | int i; | |
2033 | tree new_targs; | |
2034 | ||
f31c0a32 | 2035 | new_targs = make_tree_vec (parm_depth); |
25aab5d0 MM |
2036 | for (i = arg_depth - parm_depth; i < arg_depth; ++i) |
2037 | TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth)) | |
2038 | = TREE_VEC_ELT (targs, i); | |
2039 | targs = new_targs; | |
2040 | } | |
2041 | ||
3e4a3562 | 2042 | return instantiate_template (tmpl, targs, tf_error); |
e1467ff2 | 2043 | } |
74b846e0 | 2044 | |
6c07f448 KL |
2045 | /* If we thought that the DECL was a member function, but it |
2046 | turns out to be specializing a static member function, | |
4546865e | 2047 | make DECL a static member function as well. */ |
6c07f448 KL |
2048 | if (DECL_STATIC_FUNCTION_P (tmpl) |
2049 | && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)) | |
4546865e | 2050 | revert_static_member_fn (decl); |
6c07f448 | 2051 | |
f9a7ae04 MM |
2052 | /* If this is a specialization of a member template of a |
2053 | template class. In we want to return the TEMPLATE_DECL, | |
2054 | not the specialization of it. */ | |
74b846e0 MM |
2055 | if (tsk == tsk_template) |
2056 | { | |
2057 | SET_DECL_TEMPLATE_SPECIALIZATION (tmpl); | |
f9a7ae04 | 2058 | DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl)) = NULL_TREE; |
b190f239 NS |
2059 | if (have_def) |
2060 | { | |
f31686a3 RH |
2061 | DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl); |
2062 | DECL_SOURCE_LOCATION (DECL_TEMPLATE_RESULT (tmpl)) | |
2063 | = DECL_SOURCE_LOCATION (decl); | |
08167d1c AO |
2064 | /* We want to use the argument list specified in the |
2065 | definition, not in the original declaration. */ | |
2066 | DECL_ARGUMENTS (DECL_TEMPLATE_RESULT (tmpl)) | |
2067 | = DECL_ARGUMENTS (decl); | |
b190f239 | 2068 | } |
74b846e0 MM |
2069 | return tmpl; |
2070 | } | |
2071 | ||
36a117a5 | 2072 | /* Set up the DECL_TEMPLATE_INFO for DECL. */ |
e1b3e07d | 2073 | DECL_TEMPLATE_INFO (decl) = tree_cons (tmpl, targs, NULL_TREE); |
36a117a5 | 2074 | |
8afa707f MM |
2075 | /* Inherit default function arguments from the template |
2076 | DECL is specializing. */ | |
2077 | copy_default_args_to_explicit_spec (decl); | |
2078 | ||
c750255c MM |
2079 | /* This specialization has the same protection as the |
2080 | template it specializes. */ | |
2081 | TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl); | |
2082 | TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl); | |
2083 | ||
386b8a85 | 2084 | if (is_friend && !have_def) |
36a117a5 MM |
2085 | /* This is not really a declaration of a specialization. |
2086 | It's just the name of an instantiation. But, it's not | |
2087 | a request for an instantiation, either. */ | |
fbf1c34b | 2088 | SET_DECL_IMPLICIT_INSTANTIATION (decl); |
08511114 KL |
2089 | else if (DECL_CONSTRUCTOR_P (decl) || DECL_DESTRUCTOR_P (decl)) |
2090 | /* This is indeed a specialization. In case of constructors | |
2091 | and destructors, we need in-charge and not-in-charge | |
2092 | versions in V3 ABI. */ | |
2093 | clone_function_decl (decl, /*update_method_vec_p=*/0); | |
75650646 | 2094 | |
36a117a5 MM |
2095 | /* Register this specialization so that we can find it |
2096 | again. */ | |
2097 | decl = register_specialization (decl, gen_tmpl, targs); | |
386b8a85 JM |
2098 | } |
2099 | } | |
75650646 | 2100 | |
e1467ff2 | 2101 | return decl; |
386b8a85 | 2102 | } |
75650646 | 2103 | |
39c01e4c MM |
2104 | /* TYPE is being declared. Verify that the use of template headers |
2105 | and such is reasonable. Issue error messages if not. */ | |
2106 | ||
2107 | void | |
3a978d72 | 2108 | maybe_check_template_type (tree type) |
39c01e4c MM |
2109 | { |
2110 | if (template_header_count) | |
2111 | { | |
2112 | /* We are in the scope of some `template <...>' header. */ | |
2113 | ||
2114 | int context_depth | |
2115 | = template_class_depth_real (TYPE_CONTEXT (type), | |
2116 | /*count_specializations=*/1); | |
2117 | ||
2118 | if (template_header_count <= context_depth) | |
2119 | /* This is OK; the template headers are for the context. We | |
2120 | are actually too lenient here; like | |
2121 | check_explicit_specialization we should consider the number | |
2122 | of template types included in the actual declaration. For | |
2123 | example, | |
2124 | ||
2125 | template <class T> struct S { | |
2126 | template <class U> template <class V> | |
2127 | struct I {}; | |
2128 | }; | |
2129 | ||
0e339752 | 2130 | is invalid, but: |
39c01e4c MM |
2131 | |
2132 | template <class T> struct S { | |
2133 | template <class U> struct I; | |
2134 | }; | |
2135 | ||
2136 | template <class T> template <class U. | |
2137 | struct S<T>::I {}; | |
2138 | ||
2139 | is not. */ | |
2140 | ; | |
2141 | else if (template_header_count > context_depth + 1) | |
2142 | /* There are two many template parameter lists. */ | |
0f51ccfc | 2143 | error ("too many template parameter lists in declaration of %qT", type); |
39c01e4c MM |
2144 | } |
2145 | } | |
2146 | ||
75650646 MM |
2147 | /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template |
2148 | parameters. These are represented in the same format used for | |
2149 | DECL_TEMPLATE_PARMS. */ | |
2150 | ||
50bc768d NS |
2151 | int |
2152 | comp_template_parms (tree parms1, tree parms2) | |
75650646 MM |
2153 | { |
2154 | tree p1; | |
2155 | tree p2; | |
2156 | ||
2157 | if (parms1 == parms2) | |
2158 | return 1; | |
2159 | ||
2160 | for (p1 = parms1, p2 = parms2; | |
2161 | p1 != NULL_TREE && p2 != NULL_TREE; | |
2162 | p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2)) | |
2163 | { | |
2164 | tree t1 = TREE_VALUE (p1); | |
2165 | tree t2 = TREE_VALUE (p2); | |
2166 | int i; | |
2167 | ||
50bc768d NS |
2168 | gcc_assert (TREE_CODE (t1) == TREE_VEC); |
2169 | gcc_assert (TREE_CODE (t2) == TREE_VEC); | |
75650646 MM |
2170 | |
2171 | if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2)) | |
2172 | return 0; | |
2173 | ||
2174 | for (i = 0; i < TREE_VEC_LENGTH (t2); ++i) | |
2175 | { | |
2176 | tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i)); | |
2177 | tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i)); | |
2178 | ||
2179 | if (TREE_CODE (parm1) != TREE_CODE (parm2)) | |
2180 | return 0; | |
2181 | ||
2182 | if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM) | |
2183 | continue; | |
3bfdc719 | 2184 | else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2))) |
75650646 MM |
2185 | return 0; |
2186 | } | |
2187 | } | |
2188 | ||
2189 | if ((p1 != NULL_TREE) != (p2 != NULL_TREE)) | |
2190 | /* One set of parameters has more parameters lists than the | |
2191 | other. */ | |
2192 | return 0; | |
2193 | ||
2194 | return 1; | |
2195 | } | |
2196 | ||
f3400fe2 JM |
2197 | /* Complain if DECL shadows a template parameter. |
2198 | ||
2199 | [temp.local]: A template-parameter shall not be redeclared within its | |
2200 | scope (including nested scopes). */ | |
2201 | ||
2202 | void | |
3a978d72 | 2203 | check_template_shadow (tree decl) |
f3400fe2 | 2204 | { |
8f032717 MM |
2205 | tree olddecl; |
2206 | ||
b5d9b9ab MM |
2207 | /* If we're not in a template, we can't possibly shadow a template |
2208 | parameter. */ | |
2209 | if (!current_template_parms) | |
2210 | return; | |
2211 | ||
2212 | /* Figure out what we're shadowing. */ | |
8f032717 MM |
2213 | if (TREE_CODE (decl) == OVERLOAD) |
2214 | decl = OVL_CURRENT (decl); | |
90ea9897 | 2215 | olddecl = innermost_non_namespace_value (DECL_NAME (decl)); |
f3400fe2 | 2216 | |
b5d9b9ab MM |
2217 | /* If there's no previous binding for this name, we're not shadowing |
2218 | anything, let alone a template parameter. */ | |
2219 | if (!olddecl) | |
2220 | return; | |
2221 | ||
2222 | /* If we're not shadowing a template parameter, we're done. Note | |
2223 | that OLDDECL might be an OVERLOAD (or perhaps even an | |
2224 | ERROR_MARK), so we can't just blithely assume it to be a _DECL | |
2225 | node. */ | |
2f939d94 | 2226 | if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl)) |
b5d9b9ab MM |
2227 | return; |
2228 | ||
2229 | /* We check for decl != olddecl to avoid bogus errors for using a | |
2230 | name inside a class. We check TPFI to avoid duplicate errors for | |
2231 | inline member templates. */ | |
2232 | if (decl == olddecl | |
2233 | || TEMPLATE_PARMS_FOR_INLINE (current_template_parms)) | |
2234 | return; | |
2235 | ||
0f51ccfc GDR |
2236 | cp_error_at ("declaration of %q#D", decl); |
2237 | cp_error_at (" shadows template parm %q#D", olddecl); | |
f3400fe2 | 2238 | } |
22a7be53 | 2239 | |
f3400fe2 | 2240 | /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL, |
f84b4be9 JM |
2241 | ORIG_LEVEL, DECL, and TYPE. */ |
2242 | ||
2243 | static tree | |
3a978d72 NN |
2244 | build_template_parm_index (int index, |
2245 | int level, | |
2246 | int orig_level, | |
2247 | tree decl, | |
2248 | tree type) | |
f84b4be9 JM |
2249 | { |
2250 | tree t = make_node (TEMPLATE_PARM_INDEX); | |
2251 | TEMPLATE_PARM_IDX (t) = index; | |
2252 | TEMPLATE_PARM_LEVEL (t) = level; | |
2253 | TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level; | |
2254 | TEMPLATE_PARM_DECL (t) = decl; | |
2255 | TREE_TYPE (t) = type; | |
3e4a3562 | 2256 | TREE_CONSTANT (t) = TREE_CONSTANT (decl); |
6de9cd9a | 2257 | TREE_INVARIANT (t) = TREE_INVARIANT (decl); |
3e4a3562 | 2258 | TREE_READONLY (t) = TREE_READONLY (decl); |
f84b4be9 JM |
2259 | |
2260 | return t; | |
2261 | } | |
2262 | ||
f84b4be9 | 2263 | /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose |
93cdc044 | 2264 | TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a |
f84b4be9 JM |
2265 | TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a |
2266 | new one is created. */ | |
2267 | ||
2268 | static tree | |
3a978d72 | 2269 | reduce_template_parm_level (tree index, tree type, int levels) |
f84b4be9 JM |
2270 | { |
2271 | if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE | |
2272 | || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index)) | |
93cdc044 | 2273 | != TEMPLATE_PARM_LEVEL (index) - levels)) |
f84b4be9 | 2274 | { |
3e4a3562 NS |
2275 | tree orig_decl = TEMPLATE_PARM_DECL (index); |
2276 | tree decl, t; | |
2277 | ||
2278 | decl = build_decl (TREE_CODE (orig_decl), DECL_NAME (orig_decl), type); | |
2279 | TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl); | |
6de9cd9a | 2280 | TREE_INVARIANT (decl) = TREE_INVARIANT (orig_decl); |
3e4a3562 NS |
2281 | TREE_READONLY (decl) = TREE_READONLY (orig_decl); |
2282 | DECL_ARTIFICIAL (decl) = 1; | |
2283 | SET_DECL_TEMPLATE_PARM_P (decl); | |
2284 | ||
2285 | t = build_template_parm_index (TEMPLATE_PARM_IDX (index), | |
93cdc044 | 2286 | TEMPLATE_PARM_LEVEL (index) - levels, |
f84b4be9 JM |
2287 | TEMPLATE_PARM_ORIG_LEVEL (index), |
2288 | decl, type); | |
2289 | TEMPLATE_PARM_DESCENDANTS (index) = t; | |
cae40af6 JM |
2290 | |
2291 | /* Template template parameters need this. */ | |
2292 | DECL_TEMPLATE_PARMS (decl) | |
2293 | = DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index)); | |
f84b4be9 JM |
2294 | } |
2295 | ||
2296 | return TEMPLATE_PARM_DESCENDANTS (index); | |
2297 | } | |
2298 | ||
8d08fdba | 2299 | /* Process information from new template parameter NEXT and append it to the |
058b15c1 MM |
2300 | LIST being built. This new parameter is a non-type parameter iff |
2301 | IS_NON_TYPE is true. */ | |
e92cc029 | 2302 | |
8d08fdba | 2303 | tree |
058b15c1 | 2304 | process_template_parm (tree list, tree next, bool is_non_type) |
8d08fdba MS |
2305 | { |
2306 | tree parm; | |
2307 | tree decl = 0; | |
a292b002 | 2308 | tree defval; |
058b15c1 | 2309 | int idx; |
f84b4be9 | 2310 | |
8d08fdba | 2311 | parm = next; |
50bc768d | 2312 | gcc_assert (TREE_CODE (parm) == TREE_LIST); |
a292b002 | 2313 | defval = TREE_PURPOSE (parm); |
5566b478 MS |
2314 | |
2315 | if (list) | |
2316 | { | |
2317 | tree p = TREE_VALUE (tree_last (list)); | |
2318 | ||
1899c3a4 | 2319 | if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL) |
5566b478 MS |
2320 | idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p)); |
2321 | else | |
f84b4be9 | 2322 | idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p)); |
5566b478 MS |
2323 | ++idx; |
2324 | } | |
2325 | else | |
2326 | idx = 0; | |
2327 | ||
058b15c1 | 2328 | if (is_non_type) |
8d08fdba | 2329 | { |
058b15c1 MM |
2330 | parm = TREE_VALUE (parm); |
2331 | ||
833aa4c4 | 2332 | SET_DECL_TEMPLATE_PARM_P (parm); |
d490621d MM |
2333 | |
2334 | /* [temp.param] | |
2335 | ||
2336 | The top-level cv-qualifiers on the template-parameter are | |
2337 | ignored when determining its type. */ | |
2338 | TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm)); | |
2339 | ||
8d08fdba | 2340 | /* A template parameter is not modifiable. */ |
6de9cd9a DN |
2341 | TREE_CONSTANT (parm) = 1; |
2342 | TREE_INVARIANT (parm) = 1; | |
2343 | TREE_READONLY (parm) = 1; | |
11b810f1 | 2344 | if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1)) |
db3f4e4e | 2345 | TREE_TYPE (parm) = void_type_node; |
8d08fdba | 2346 | decl = build_decl (CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm)); |
6de9cd9a DN |
2347 | TREE_CONSTANT (decl) = 1; |
2348 | TREE_INVARIANT (decl) = 1; | |
2349 | TREE_READONLY (decl) = 1; | |
f84b4be9 JM |
2350 | DECL_INITIAL (parm) = DECL_INITIAL (decl) |
2351 | = build_template_parm_index (idx, processing_template_decl, | |
2352 | processing_template_decl, | |
2353 | decl, TREE_TYPE (parm)); | |
8d08fdba MS |
2354 | } |
2355 | else | |
2356 | { | |
73b0fce8 | 2357 | tree t; |
058b15c1 | 2358 | parm = TREE_VALUE (TREE_VALUE (parm)); |
73b0fce8 KL |
2359 | |
2360 | if (parm && TREE_CODE (parm) == TEMPLATE_DECL) | |
2361 | { | |
33848bb0 | 2362 | t = make_aggr_type (TEMPLATE_TEMPLATE_PARM); |
73b0fce8 KL |
2363 | /* This is for distinguishing between real templates and template |
2364 | template parameters */ | |
2365 | TREE_TYPE (parm) = t; | |
2366 | TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t; | |
2367 | decl = parm; | |
2368 | } | |
2369 | else | |
2370 | { | |
33848bb0 | 2371 | t = make_aggr_type (TEMPLATE_TYPE_PARM); |
f4f206f4 | 2372 | /* parm is either IDENTIFIER_NODE or NULL_TREE. */ |
73b0fce8 KL |
2373 | decl = build_decl (TYPE_DECL, parm, t); |
2374 | } | |
2375 | ||
d2e5ee5c MS |
2376 | TYPE_NAME (t) = decl; |
2377 | TYPE_STUB_DECL (t) = decl; | |
a292b002 | 2378 | parm = decl; |
f84b4be9 JM |
2379 | TEMPLATE_TYPE_PARM_INDEX (t) |
2380 | = build_template_parm_index (idx, processing_template_decl, | |
2381 | processing_template_decl, | |
2382 | decl, TREE_TYPE (parm)); | |
8d08fdba | 2383 | } |
c727aa5e | 2384 | DECL_ARTIFICIAL (decl) = 1; |
cd9f6678 | 2385 | SET_DECL_TEMPLATE_PARM_P (decl); |
8d08fdba | 2386 | pushdecl (decl); |
a292b002 | 2387 | parm = build_tree_list (defval, parm); |
8d08fdba MS |
2388 | return chainon (list, parm); |
2389 | } | |
2390 | ||
2391 | /* The end of a template parameter list has been reached. Process the | |
2392 | tree list into a parameter vector, converting each parameter into a more | |
2393 | useful form. Type parameters are saved as IDENTIFIER_NODEs, and others | |
2394 | as PARM_DECLs. */ | |
2395 | ||
2396 | tree | |
3a978d72 | 2397 | end_template_parm_list (tree parms) |
8d08fdba | 2398 | { |
5566b478 | 2399 | int nparms; |
9471d3e2 | 2400 | tree parm, next; |
5566b478 MS |
2401 | tree saved_parmlist = make_tree_vec (list_length (parms)); |
2402 | ||
5566b478 | 2403 | current_template_parms |
4890c2f4 | 2404 | = tree_cons (size_int (processing_template_decl), |
5566b478 | 2405 | saved_parmlist, current_template_parms); |
8d08fdba | 2406 | |
9471d3e2 NS |
2407 | for (parm = parms, nparms = 0; parm; parm = next, nparms++) |
2408 | { | |
2409 | next = TREE_CHAIN (parm); | |
2410 | TREE_VEC_ELT (saved_parmlist, nparms) = parm; | |
2411 | TREE_CHAIN (parm) = NULL_TREE; | |
2412 | } | |
a292b002 | 2413 | |
67ffc812 MM |
2414 | --processing_template_parmlist; |
2415 | ||
8d08fdba MS |
2416 | return saved_parmlist; |
2417 | } | |
2418 | ||
5566b478 MS |
2419 | /* end_template_decl is called after a template declaration is seen. */ |
2420 | ||
8d08fdba | 2421 | void |
3a978d72 | 2422 | end_template_decl (void) |
8d08fdba | 2423 | { |
386b8a85 JM |
2424 | reset_specialization (); |
2425 | ||
5156628f | 2426 | if (! processing_template_decl) |
73aad9b9 JM |
2427 | return; |
2428 | ||
5566b478 | 2429 | /* This matches the pushlevel in begin_template_parm_list. */ |
74b846e0 | 2430 | finish_scope (); |
8d08fdba | 2431 | |
5566b478 MS |
2432 | --processing_template_decl; |
2433 | current_template_parms = TREE_CHAIN (current_template_parms); | |
5566b478 | 2434 | } |
8d08fdba | 2435 | |
36a117a5 MM |
2436 | /* Given a template argument vector containing the template PARMS. |
2437 | The innermost PARMS are given first. */ | |
9a3b49ac MS |
2438 | |
2439 | tree | |
3a978d72 | 2440 | current_template_args (void) |
5566b478 | 2441 | { |
36a117a5 | 2442 | tree header; |
b370501f | 2443 | tree args = NULL_TREE; |
36a117a5 | 2444 | int length = TMPL_PARMS_DEPTH (current_template_parms); |
98c1c668 JM |
2445 | int l = length; |
2446 | ||
36a117a5 MM |
2447 | /* If there is only one level of template parameters, we do not |
2448 | create a TREE_VEC of TREE_VECs. Instead, we return a single | |
2449 | TREE_VEC containing the arguments. */ | |
2450 | if (length > 1) | |
2451 | args = make_tree_vec (length); | |
2452 | ||
2453 | for (header = current_template_parms; header; header = TREE_CHAIN (header)) | |
8d08fdba | 2454 | { |
5566b478 | 2455 | tree a = copy_node (TREE_VALUE (header)); |
36a117a5 MM |
2456 | int i; |
2457 | ||
5566b478 | 2458 | TREE_TYPE (a) = NULL_TREE; |
36a117a5 | 2459 | for (i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i) |
5566b478 | 2460 | { |
98c1c668 JM |
2461 | tree t = TREE_VEC_ELT (a, i); |
2462 | ||
36a117a5 | 2463 | /* T will be a list if we are called from within a |
98c1c668 JM |
2464 | begin/end_template_parm_list pair, but a vector directly |
2465 | if within a begin/end_member_template_processing pair. */ | |
2466 | if (TREE_CODE (t) == TREE_LIST) | |
2467 | { | |
2468 | t = TREE_VALUE (t); | |
2469 | ||
73b0fce8 KL |
2470 | if (TREE_CODE (t) == TYPE_DECL |
2471 | || TREE_CODE (t) == TEMPLATE_DECL) | |
98c1c668 JM |
2472 | t = TREE_TYPE (t); |
2473 | else | |
2474 | t = DECL_INITIAL (t); | |
36a117a5 | 2475 | TREE_VEC_ELT (a, i) = t; |
98c1c668 | 2476 | } |
5566b478 | 2477 | } |
36a117a5 MM |
2478 | |
2479 | if (length > 1) | |
2480 | TREE_VEC_ELT (args, --l) = a; | |
2481 | else | |
2482 | args = a; | |
8d08fdba MS |
2483 | } |
2484 | ||
9a3b49ac MS |
2485 | return args; |
2486 | } | |
75650646 | 2487 | |
e1467ff2 | 2488 | /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated |
c7222c02 | 2489 | template PARMS. If MEMBER_TEMPLATE_P is true, the new template is |
ee81147e | 2490 | a member template. Used by push_template_decl below. */ |
e1467ff2 | 2491 | |
75650646 | 2492 | static tree |
c7222c02 | 2493 | build_template_decl (tree decl, tree parms, bool member_template_p) |
75650646 MM |
2494 | { |
2495 | tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE); | |
2496 | DECL_TEMPLATE_PARMS (tmpl) = parms; | |
2497 | DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl); | |
c7222c02 | 2498 | DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p; |
75650646 MM |
2499 | if (DECL_LANG_SPECIFIC (decl)) |
2500 | { | |
8f032717 MM |
2501 | DECL_STATIC_FUNCTION_P (tmpl) = DECL_STATIC_FUNCTION_P (decl); |
2502 | DECL_CONSTRUCTOR_P (tmpl) = DECL_CONSTRUCTOR_P (decl); | |
c8460010 | 2503 | DECL_DESTRUCTOR_P (tmpl) = DECL_DESTRUCTOR_P (decl); |
11f98788 | 2504 | DECL_NONCONVERTING_P (tmpl) = DECL_NONCONVERTING_P (decl); |
1f6e1acc AS |
2505 | DECL_ASSIGNMENT_OPERATOR_P (tmpl) = DECL_ASSIGNMENT_OPERATOR_P (decl); |
2506 | if (DECL_OVERLOADED_OPERATOR_P (decl)) | |
2507 | SET_OVERLOADED_OPERATOR_CODE (tmpl, | |
2508 | DECL_OVERLOADED_OPERATOR_P (decl)); | |
75650646 MM |
2509 | } |
2510 | ||
2511 | return tmpl; | |
2512 | } | |
2513 | ||
050367a3 MM |
2514 | struct template_parm_data |
2515 | { | |
6c30752f MM |
2516 | /* The level of the template parameters we are currently |
2517 | processing. */ | |
050367a3 | 2518 | int level; |
6c30752f MM |
2519 | |
2520 | /* The index of the specialization argument we are currently | |
2521 | processing. */ | |
2522 | int current_arg; | |
2523 | ||
2524 | /* An array whose size is the number of template parameters. The | |
838dfd8a | 2525 | elements are nonzero if the parameter has been used in any one |
6c30752f | 2526 | of the arguments processed so far. */ |
050367a3 | 2527 | int* parms; |
6c30752f MM |
2528 | |
2529 | /* An array whose size is the number of template arguments. The | |
838dfd8a | 2530 | elements are nonzero if the argument makes use of template |
6c30752f MM |
2531 | parameters of this level. */ |
2532 | int* arg_uses_template_parms; | |
050367a3 MM |
2533 | }; |
2534 | ||
2535 | /* Subroutine of push_template_decl used to see if each template | |
2536 | parameter in a partial specialization is used in the explicit | |
2537 | argument list. If T is of the LEVEL given in DATA (which is | |
2538 | treated as a template_parm_data*), then DATA->PARMS is marked | |
2539 | appropriately. */ | |
2540 | ||
2541 | static int | |
3a978d72 | 2542 | mark_template_parm (tree t, void* data) |
050367a3 MM |
2543 | { |
2544 | int level; | |
2545 | int idx; | |
2546 | struct template_parm_data* tpd = (struct template_parm_data*) data; | |
2547 | ||
2548 | if (TREE_CODE (t) == TEMPLATE_PARM_INDEX) | |
2549 | { | |
2550 | level = TEMPLATE_PARM_LEVEL (t); | |
2551 | idx = TEMPLATE_PARM_IDX (t); | |
2552 | } | |
2553 | else | |
2554 | { | |
2555 | level = TEMPLATE_TYPE_LEVEL (t); | |
2556 | idx = TEMPLATE_TYPE_IDX (t); | |
2557 | } | |
2558 | ||
2559 | if (level == tpd->level) | |
6c30752f MM |
2560 | { |
2561 | tpd->parms[idx] = 1; | |
2562 | tpd->arg_uses_template_parms[tpd->current_arg] = 1; | |
2563 | } | |
050367a3 MM |
2564 | |
2565 | /* Return zero so that for_each_template_parm will continue the | |
2566 | traversal of the tree; we want to mark *every* template parm. */ | |
2567 | return 0; | |
2568 | } | |
2569 | ||
6c30752f MM |
2570 | /* Process the partial specialization DECL. */ |
2571 | ||
e9659ab0 | 2572 | static tree |
3a978d72 | 2573 | process_partial_specialization (tree decl) |
6c30752f MM |
2574 | { |
2575 | tree type = TREE_TYPE (decl); | |
2576 | tree maintmpl = CLASSTYPE_TI_TEMPLATE (type); | |
2577 | tree specargs = CLASSTYPE_TI_ARGS (type); | |
f9a7ae04 | 2578 | tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs); |
6c30752f MM |
2579 | tree inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms); |
2580 | tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl); | |
2581 | int nargs = TREE_VEC_LENGTH (inner_args); | |
2582 | int ntparms = TREE_VEC_LENGTH (inner_parms); | |
2583 | int i; | |
2584 | int did_error_intro = 0; | |
6c30752f MM |
2585 | struct template_parm_data tpd; |
2586 | struct template_parm_data tpd2; | |
2587 | ||
6c30752f MM |
2588 | /* We check that each of the template parameters given in the |
2589 | partial specialization is used in the argument list to the | |
2590 | specialization. For example: | |
2591 | ||
2592 | template <class T> struct S; | |
2593 | template <class T> struct S<T*>; | |
2594 | ||
2595 | The second declaration is OK because `T*' uses the template | |
2596 | parameter T, whereas | |
2597 | ||
2598 | template <class T> struct S<int>; | |
2599 | ||
2600 | is no good. Even trickier is: | |
2601 | ||
2602 | template <class T> | |
2603 | struct S1 | |
2604 | { | |
2605 | template <class U> | |
2606 | struct S2; | |
2607 | template <class U> | |
2608 | struct S2<T>; | |
2609 | }; | |
2610 | ||
0e339752 | 2611 | The S2<T> declaration is actually invalid; it is a |
6c30752f MM |
2612 | full-specialization. Of course, |
2613 | ||
2614 | template <class U> | |
2615 | struct S2<T (*)(U)>; | |
2616 | ||
2617 | or some such would have been OK. */ | |
2618 | tpd.level = TMPL_PARMS_DEPTH (current_template_parms); | |
2619 | tpd.parms = alloca (sizeof (int) * ntparms); | |
fad205ff | 2620 | memset (tpd.parms, 0, sizeof (int) * ntparms); |
6c30752f MM |
2621 | |
2622 | tpd.arg_uses_template_parms = alloca (sizeof (int) * nargs); | |
fad205ff | 2623 | memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs); |
6c30752f MM |
2624 | for (i = 0; i < nargs; ++i) |
2625 | { | |
2626 | tpd.current_arg = i; | |
2627 | for_each_template_parm (TREE_VEC_ELT (inner_args, i), | |
2628 | &mark_template_parm, | |
4f2c9d7e MM |
2629 | &tpd, |
2630 | NULL); | |
6c30752f MM |
2631 | } |
2632 | for (i = 0; i < ntparms; ++i) | |
2633 | if (tpd.parms[i] == 0) | |
2634 | { | |
2635 | /* One of the template parms was not used in the | |
f9a7ae04 | 2636 | specialization. */ |
6c30752f MM |
2637 | if (!did_error_intro) |
2638 | { | |
33bd39a2 | 2639 | error ("template parameters not used in partial specialization:"); |
6c30752f MM |
2640 | did_error_intro = 1; |
2641 | } | |
2642 | ||
0f51ccfc | 2643 | error (" %qD", TREE_VALUE (TREE_VEC_ELT (inner_parms, i))); |
6c30752f MM |
2644 | } |
2645 | ||
2646 | /* [temp.class.spec] | |
2647 | ||
2648 | The argument list of the specialization shall not be identical to | |
2649 | the implicit argument list of the primary template. */ | |
f9a7ae04 MM |
2650 | if (comp_template_args |
2651 | (inner_args, | |
2652 | INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE | |
2653 | (maintmpl))))) | |
0f51ccfc | 2654 | error ("partial specialization %qT does not specialize any template arguments", type); |
6c30752f MM |
2655 | |
2656 | /* [temp.class.spec] | |
2657 | ||
2658 | A partially specialized non-type argument expression shall not | |
2659 | involve template parameters of the partial specialization except | |
2660 | when the argument expression is a simple identifier. | |
2661 | ||
2662 | The type of a template parameter corresponding to a specialized | |
2663 | non-type argument shall not be dependent on a parameter of the | |
2664 | specialization. */ | |
50bc768d | 2665 | gcc_assert (nargs == DECL_NTPARMS (maintmpl)); |
6c30752f MM |
2666 | tpd2.parms = 0; |
2667 | for (i = 0; i < nargs; ++i) | |
2668 | { | |
2669 | tree arg = TREE_VEC_ELT (inner_args, i); | |
2670 | if (/* These first two lines are the `non-type' bit. */ | |
2f939d94 | 2671 | !TYPE_P (arg) |
6c30752f MM |
2672 | && TREE_CODE (arg) != TEMPLATE_DECL |
2673 | /* This next line is the `argument expression is not just a | |
2674 | simple identifier' condition and also the `specialized | |
2675 | non-type argument' bit. */ | |
2676 | && TREE_CODE (arg) != TEMPLATE_PARM_INDEX) | |
2677 | { | |
2678 | if (tpd.arg_uses_template_parms[i]) | |
0f51ccfc | 2679 | error ("template argument %qE involves template parameter(s)", arg); |
6c30752f MM |
2680 | else |
2681 | { | |
2682 | /* Look at the corresponding template parameter, | |
2683 | marking which template parameters its type depends | |
2684 | upon. */ | |
2685 | tree type = | |
2686 | TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (main_inner_parms, | |
2687 | i))); | |
2688 | ||
2689 | if (!tpd2.parms) | |
2690 | { | |
2691 | /* We haven't yet initialized TPD2. Do so now. */ | |
2692 | tpd2.arg_uses_template_parms | |
c68b0a84 | 2693 | = alloca (sizeof (int) * nargs); |
104bf76a | 2694 | /* The number of parameters here is the number in the |
76a83782 RH |
2695 | main template, which, as checked in the assertion |
2696 | above, is NARGS. */ | |
c68b0a84 | 2697 | tpd2.parms = alloca (sizeof (int) * nargs); |
6c30752f MM |
2698 | tpd2.level = |
2699 | TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl)); | |
2700 | } | |
2701 | ||
104bf76a | 2702 | /* Mark the template parameters. But this time, we're |
6c30752f MM |
2703 | looking for the template parameters of the main |
2704 | template, not in the specialization. */ | |
2705 | tpd2.current_arg = i; | |
2706 | tpd2.arg_uses_template_parms[i] = 0; | |
fad205ff | 2707 | memset (tpd2.parms, 0, sizeof (int) * nargs); |
6c30752f MM |
2708 | for_each_template_parm (type, |
2709 | &mark_template_parm, | |
4f2c9d7e MM |
2710 | &tpd2, |
2711 | NULL); | |
6c30752f MM |
2712 | |
2713 | if (tpd2.arg_uses_template_parms [i]) | |
2714 | { | |
2715 | /* The type depended on some template parameters. | |
2716 | If they are fully specialized in the | |
2717 | specialization, that's OK. */ | |
2718 | int j; | |
2719 | for (j = 0; j < nargs; ++j) | |
2720 | if (tpd2.parms[j] != 0 | |
2721 | && tpd.arg_uses_template_parms [j]) | |
2722 | { | |
0f51ccfc GDR |
2723 | error ("type %qT of template argument %qE depends " |
2724 | "on template parameter(s)", | |
2725 | type, | |
2726 | arg); | |
6c30752f MM |
2727 | break; |
2728 | } | |
2729 | } | |
2730 | } | |
2731 | } | |
2732 | } | |
2733 | ||
c7222c02 MM |
2734 | if (retrieve_specialization (maintmpl, specargs, |
2735 | /*class_specializations_p=*/true)) | |
6c30752f MM |
2736 | /* We've already got this specialization. */ |
2737 | return decl; | |
2738 | ||
d8b64f80 | 2739 | DECL_TEMPLATE_SPECIALIZATIONS (maintmpl) |
e1b3e07d MM |
2740 | = tree_cons (inner_args, inner_parms, |
2741 | DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)); | |
6c30752f MM |
2742 | TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type; |
2743 | return decl; | |
2744 | } | |
2745 | ||
6ba89f8e MM |
2746 | /* Check that a template declaration's use of default arguments is not |
2747 | invalid. Here, PARMS are the template parameters. IS_PRIMARY is | |
838dfd8a KH |
2748 | nonzero if DECL is the thing declared by a primary template. |
2749 | IS_PARTIAL is nonzero if DECL is a partial specialization. */ | |
6ba89f8e MM |
2750 | |
2751 | static void | |
3a978d72 | 2752 | check_default_tmpl_args (tree decl, tree parms, int is_primary, int is_partial) |
6ba89f8e | 2753 | { |
d8e178a0 | 2754 | const char *msg; |
66191c20 MM |
2755 | int last_level_to_check; |
2756 | tree parm_level; | |
6ba89f8e MM |
2757 | |
2758 | /* [temp.param] | |
2759 | ||
2760 | A default template-argument shall not be specified in a | |
2761 | function template declaration or a function template definition, nor | |
2762 | in the template-parameter-list of the definition of a member of a | |
2763 | class template. */ | |
2764 | ||
4f1c5b7d | 2765 | if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL) |
e0942dcd MM |
2766 | /* You can't have a function template declaration in a local |
2767 | scope, nor you can you define a member of a class template in a | |
2768 | local scope. */ | |
2769 | return; | |
2770 | ||
6ba89f8e MM |
2771 | if (current_class_type |
2772 | && !TYPE_BEING_DEFINED (current_class_type) | |
3d7de1fa | 2773 | && DECL_LANG_SPECIFIC (decl) |
5937a6f9 MM |
2774 | /* If this is either a friend defined in the scope of the class |
2775 | or a member function. */ | |
6df5158a NS |
2776 | && (DECL_FUNCTION_MEMBER_P (decl) |
2777 | ? same_type_p (DECL_CONTEXT (decl), current_class_type) | |
2778 | : DECL_FRIEND_CONTEXT (decl) | |
2779 | ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type) | |
2780 | : false) | |
5937a6f9 MM |
2781 | /* And, if it was a member function, it really was defined in |
2782 | the scope of the class. */ | |
6df5158a NS |
2783 | && (!DECL_FUNCTION_MEMBER_P (decl) |
2784 | || DECL_INITIALIZED_IN_CLASS_P (decl))) | |
6ba89f8e | 2785 | /* We already checked these parameters when the template was |
5937a6f9 MM |
2786 | declared, so there's no need to do it again now. This function |
2787 | was defined in class scope, but we're processing it's body now | |
2788 | that the class is complete. */ | |
6ba89f8e MM |
2789 | return; |
2790 | ||
66191c20 MM |
2791 | /* [temp.param] |
2792 | ||
2793 | If a template-parameter has a default template-argument, all | |
2794 | subsequent template-parameters shall have a default | |
2795 | template-argument supplied. */ | |
2796 | for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level)) | |
2797 | { | |
2798 | tree inner_parms = TREE_VALUE (parm_level); | |
2799 | int ntparms = TREE_VEC_LENGTH (inner_parms); | |
2800 | int seen_def_arg_p = 0; | |
2801 | int i; | |
2802 | ||
2803 | for (i = 0; i < ntparms; ++i) | |
2804 | { | |
2805 | tree parm = TREE_VEC_ELT (inner_parms, i); | |
2806 | if (TREE_PURPOSE (parm)) | |
2807 | seen_def_arg_p = 1; | |
2808 | else if (seen_def_arg_p) | |
2809 | { | |
0f51ccfc | 2810 | error ("no default argument for %qD", TREE_VALUE (parm)); |
66191c20 MM |
2811 | /* For better subsequent error-recovery, we indicate that |
2812 | there should have been a default argument. */ | |
2813 | TREE_PURPOSE (parm) = error_mark_node; | |
2814 | } | |
2815 | } | |
2816 | } | |
2817 | ||
6ba89f8e MM |
2818 | if (TREE_CODE (decl) != TYPE_DECL || is_partial || !is_primary) |
2819 | /* For an ordinary class template, default template arguments are | |
2820 | allowed at the innermost level, e.g.: | |
2821 | template <class T = int> | |
2822 | struct S {}; | |
2823 | but, in a partial specialization, they're not allowed even | |
2824 | there, as we have in [temp.class.spec]: | |
2825 | ||
2826 | The template parameter list of a specialization shall not | |
2827 | contain default template argument values. | |
2828 | ||
2829 | So, for a partial specialization, or for a function template, | |
2830 | we look at all of them. */ | |
2831 | ; | |
2832 | else | |
2833 | /* But, for a primary class template that is not a partial | |
2834 | specialization we look at all template parameters except the | |
2835 | innermost ones. */ | |
2836 | parms = TREE_CHAIN (parms); | |
2837 | ||
2838 | /* Figure out what error message to issue. */ | |
2839 | if (TREE_CODE (decl) == FUNCTION_DECL) | |
8653a2c3 | 2840 | msg = "default template arguments may not be used in function templates"; |
6ba89f8e | 2841 | else if (is_partial) |
8653a2c3 | 2842 | msg = "default template arguments may not be used in partial specializations"; |
6ba89f8e | 2843 | else |
0f51ccfc | 2844 | msg = "default argument for template parameter for class enclosing %qD"; |
6ba89f8e MM |
2845 | |
2846 | if (current_class_type && TYPE_BEING_DEFINED (current_class_type)) | |
2847 | /* If we're inside a class definition, there's no need to | |
104bf76a | 2848 | examine the parameters to the class itself. On the one |
6ba89f8e | 2849 | hand, they will be checked when the class is defined, and, |
0e339752 | 2850 | on the other, default arguments are valid in things like: |
6ba89f8e MM |
2851 | template <class T = double> |
2852 | struct S { template <class U> void f(U); }; | |
2853 | Here the default argument for `S' has no bearing on the | |
2854 | declaration of `f'. */ | |
2855 | last_level_to_check = template_class_depth (current_class_type) + 1; | |
2856 | else | |
2857 | /* Check everything. */ | |
2858 | last_level_to_check = 0; | |
2859 | ||
66191c20 MM |
2860 | for (parm_level = parms; |
2861 | parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check; | |
2862 | parm_level = TREE_CHAIN (parm_level)) | |
6ba89f8e | 2863 | { |
66191c20 MM |
2864 | tree inner_parms = TREE_VALUE (parm_level); |
2865 | int i; | |
2866 | int ntparms; | |
6ba89f8e MM |
2867 | |
2868 | ntparms = TREE_VEC_LENGTH (inner_parms); | |
2869 | for (i = 0; i < ntparms; ++i) | |
2870 | if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i))) | |
2871 | { | |
2872 | if (msg) | |
2873 | { | |
33bd39a2 | 2874 | error (msg, decl); |
6ba89f8e MM |
2875 | msg = 0; |
2876 | } | |
2877 | ||
2878 | /* Clear out the default argument so that we are not | |
2879 | confused later. */ | |
2880 | TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE; | |
2881 | } | |
2882 | ||
2883 | /* At this point, if we're still interested in issuing messages, | |
2884 | they must apply to classes surrounding the object declared. */ | |
2885 | if (msg) | |
0f51ccfc | 2886 | msg = "default argument for template parameter for class enclosing %qD"; |
6ba89f8e MM |
2887 | } |
2888 | } | |
2889 | ||
5dd236e2 NS |
2890 | /* Worker for push_template_decl_real, called via |
2891 | for_each_template_parm. DATA is really an int, indicating the | |
2892 | level of the parameters we are interested in. If T is a template | |
838dfd8a | 2893 | parameter of that level, return nonzero. */ |
5dd236e2 NS |
2894 | |
2895 | static int | |
3a978d72 | 2896 | template_parm_this_level_p (tree t, void* data) |
5dd236e2 | 2897 | { |
6e04241f | 2898 | int this_level = *(int *)data; |
5dd236e2 NS |
2899 | int level; |
2900 | ||
2901 | if (TREE_CODE (t) == TEMPLATE_PARM_INDEX) | |
2902 | level = TEMPLATE_PARM_LEVEL (t); | |
2903 | else | |
2904 | level = TEMPLATE_TYPE_LEVEL (t); | |
2905 | return level == this_level; | |
2906 | } | |
2907 | ||
3ac3d9ea | 2908 | /* Creates a TEMPLATE_DECL for the indicated DECL using the template |
f84b4be9 JM |
2909 | parameters given by current_template_args, or reuses a |
2910 | previously existing one, if appropriate. Returns the DECL, or an | |
6757edfe MM |
2911 | equivalent one, if it is replaced via a call to duplicate_decls. |
2912 | ||
838dfd8a | 2913 | If IS_FRIEND is nonzero, DECL is a friend declaration. */ |
3ac3d9ea MM |
2914 | |
2915 | tree | |
3a978d72 | 2916 | push_template_decl_real (tree decl, int is_friend) |
9a3b49ac MS |
2917 | { |
2918 | tree tmpl; | |
f84b4be9 | 2919 | tree args; |
9a3b49ac | 2920 | tree info; |
f84b4be9 JM |
2921 | tree ctx; |
2922 | int primary; | |
6ba89f8e | 2923 | int is_partial; |
cfe507be | 2924 | int new_template_p = 0; |
c7222c02 MM |
2925 | /* True if the template is a member template, in the sense of |
2926 | [temp.mem]. */ | |
2927 | bool member_template_p = false; | |
6ba89f8e | 2928 | |
c0694c4b MM |
2929 | if (decl == error_mark_node) |
2930 | return decl; | |
2931 | ||
6ba89f8e | 2932 | /* See if this is a partial specialization. */ |
9188c363 | 2933 | is_partial = (DECL_IMPLICIT_TYPEDEF_P (decl) |
6ba89f8e | 2934 | && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE |
370af2d5 | 2935 | && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl))); |
6757edfe MM |
2936 | |
2937 | is_friend |= (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl)); | |
f84b4be9 JM |
2938 | |
2939 | if (is_friend) | |
2940 | /* For a friend, we want the context of the friend function, not | |
2941 | the type of which it is a friend. */ | |
2942 | ctx = DECL_CONTEXT (decl); | |
4f1c5b7d MM |
2943 | else if (CP_DECL_CONTEXT (decl) |
2944 | && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL) | |
f84b4be9 JM |
2945 | /* In the case of a virtual function, we want the class in which |
2946 | it is defined. */ | |
4f1c5b7d | 2947 | ctx = CP_DECL_CONTEXT (decl); |
f84b4be9 | 2948 | else |
dc957d14 | 2949 | /* Otherwise, if we're currently defining some class, the DECL |
f84b4be9 | 2950 | is assumed to be a member of the class. */ |
9188c363 | 2951 | ctx = current_scope (); |
f84b4be9 | 2952 | |
2c73f9f5 ML |
2953 | if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL) |
2954 | ctx = NULL_TREE; | |
2955 | ||
2956 | if (!DECL_CONTEXT (decl)) | |
cb0dbb9a | 2957 | DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace); |
2c73f9f5 | 2958 | |
6ba89f8e | 2959 | /* See if this is a primary template. */ |
74b846e0 | 2960 | primary = template_parm_scope_p (); |
9a3b49ac | 2961 | |
83566abf JM |
2962 | if (primary) |
2963 | { | |
c7222c02 MM |
2964 | if (DECL_CLASS_SCOPE_P (decl)) |
2965 | member_template_p = true; | |
83566abf | 2966 | if (current_lang_name == lang_name_c) |
33bd39a2 | 2967 | error ("template with C linkage"); |
4ce3d537 MM |
2968 | else if (TREE_CODE (decl) == TYPE_DECL |
2969 | && ANON_AGGRNAME_P (DECL_NAME (decl))) | |
33bd39a2 | 2970 | error ("template class without a name"); |
717a7d5d | 2971 | else if (TREE_CODE (decl) == FUNCTION_DECL) |
4b0d3cbe | 2972 | { |
717a7d5d MM |
2973 | if (DECL_DESTRUCTOR_P (decl)) |
2974 | { | |
2975 | /* [temp.mem] | |
2976 | ||
2977 | A destructor shall not be a member template. */ | |
0f51ccfc | 2978 | error ("destructor %qD declared as member template", decl); |
717a7d5d MM |
2979 | return error_mark_node; |
2980 | } | |
2981 | if (NEW_DELETE_OPNAME_P (DECL_NAME (decl)) | |
2982 | && (!TYPE_ARG_TYPES (TREE_TYPE (decl)) | |
2983 | || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node | |
2984 | || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl))) | |
2985 | || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl)))) | |
2986 | == void_list_node))) | |
2987 | { | |
2988 | /* [basic.stc.dynamic.allocation] | |
2989 | ||
2990 | An allocation function can be a function | |
2991 | template. ... Template allocation functions shall | |
2992 | have two or more parameters. */ | |
0f51ccfc | 2993 | error ("invalid template declaration of %qD", decl); |
717a7d5d MM |
2994 | return decl; |
2995 | } | |
4b0d3cbe | 2996 | } |
2aaf816d JM |
2997 | else if ((DECL_IMPLICIT_TYPEDEF_P (decl) |
2998 | && CLASS_TYPE_P (TREE_TYPE (decl))) | |
717a7d5d | 2999 | || (TREE_CODE (decl) == VAR_DECL && ctx && CLASS_TYPE_P (ctx))) |
2aaf816d JM |
3000 | /* OK */; |
3001 | else | |
11325dcd | 3002 | { |
0f51ccfc | 3003 | error ("template declaration of %q#D", decl); |
11325dcd KL |
3004 | return error_mark_node; |
3005 | } | |
83566abf JM |
3006 | } |
3007 | ||
6ba89f8e MM |
3008 | /* Check to see that the rules regarding the use of default |
3009 | arguments are not being violated. */ | |
3010 | check_default_tmpl_args (decl, current_template_parms, | |
3011 | primary, is_partial); | |
73aad9b9 | 3012 | |
6ba89f8e MM |
3013 | if (is_partial) |
3014 | return process_partial_specialization (decl); | |
d32789d8 | 3015 | |
9a3b49ac MS |
3016 | args = current_template_args (); |
3017 | ||
f84b4be9 JM |
3018 | if (!ctx |
3019 | || TREE_CODE (ctx) == FUNCTION_DECL | |
a723baf1 | 3020 | || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx)) |
f84b4be9 | 3021 | || (is_friend && !DECL_TEMPLATE_INFO (decl))) |
8d08fdba | 3022 | { |
75650646 | 3023 | if (DECL_LANG_SPECIFIC (decl) |
f84b4be9 JM |
3024 | && DECL_TEMPLATE_INFO (decl) |
3025 | && DECL_TI_TEMPLATE (decl)) | |
3026 | tmpl = DECL_TI_TEMPLATE (decl); | |
1c10870d AS |
3027 | /* If DECL is a TYPE_DECL for a class-template, then there won't |
3028 | be DECL_LANG_SPECIFIC. The information equivalent to | |
3029 | DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */ | |
3030 | else if (DECL_IMPLICIT_TYPEDEF_P (decl) | |
3031 | && TYPE_TEMPLATE_INFO (TREE_TYPE (decl)) | |
3032 | && TYPE_TI_TEMPLATE (TREE_TYPE (decl))) | |
3033 | { | |
3034 | /* Since a template declaration already existed for this | |
3035 | class-type, we must be redeclaring it here. Make sure | |
0e339752 | 3036 | that the redeclaration is valid. */ |
1c10870d AS |
3037 | redeclare_class_template (TREE_TYPE (decl), |
3038 | current_template_parms); | |
3039 | /* We don't need to create a new TEMPLATE_DECL; just use the | |
3040 | one we already had. */ | |
3041 | tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl)); | |
3042 | } | |
f84b4be9 | 3043 | else |
786b5245 | 3044 | { |
c7222c02 MM |
3045 | tmpl = build_template_decl (decl, current_template_parms, |
3046 | member_template_p); | |
cfe507be MM |
3047 | new_template_p = 1; |
3048 | ||
f84b4be9 JM |
3049 | if (DECL_LANG_SPECIFIC (decl) |
3050 | && DECL_TEMPLATE_SPECIALIZATION (decl)) | |
3051 | { | |
3052 | /* A specialization of a member template of a template | |
c6002625 | 3053 | class. */ |
f84b4be9 JM |
3054 | SET_DECL_TEMPLATE_SPECIALIZATION (tmpl); |
3055 | DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl); | |
3056 | DECL_TEMPLATE_INFO (decl) = NULL_TREE; | |
3057 | } | |
786b5245 | 3058 | } |
8d08fdba MS |
3059 | } |
3060 | else | |
3061 | { | |
e1a5ccf7 | 3062 | tree a, t, current, parms; |
ba4f4e5d | 3063 | int i; |
6633d636 | 3064 | |
6b9ab5cc MM |
3065 | if (TREE_CODE (decl) == TYPE_DECL) |
3066 | { | |
ed44da02 MM |
3067 | if ((IS_AGGR_TYPE_CODE (TREE_CODE (TREE_TYPE (decl))) |
3068 | || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE) | |
3069 | && TYPE_TEMPLATE_INFO (TREE_TYPE (decl)) | |
3070 | && TYPE_TI_TEMPLATE (TREE_TYPE (decl))) | |
3071 | tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl)); | |
6b9ab5cc MM |
3072 | else |
3073 | { | |
0f51ccfc | 3074 | error ("%qD does not declare a template type", decl); |
6b9ab5cc MM |
3075 | return decl; |
3076 | } | |
3077 | } | |
f3368a90 | 3078 | else if (!DECL_LANG_SPECIFIC (decl) || !DECL_TEMPLATE_INFO (decl)) |
c91a56d2 | 3079 | { |
0f51ccfc | 3080 | error ("template definition of non-template %q#D", decl); |
3ac3d9ea | 3081 | return decl; |
c91a56d2 | 3082 | } |
8d08fdba | 3083 | else |
5566b478 | 3084 | tmpl = DECL_TI_TEMPLATE (decl); |
98c1c668 | 3085 | |
c353b8e3 | 3086 | if (DECL_FUNCTION_TEMPLATE_P (tmpl) |
e1a5ccf7 | 3087 | && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl) |
c353b8e3 | 3088 | && DECL_TEMPLATE_SPECIALIZATION (decl) |
c7222c02 | 3089 | && DECL_MEMBER_TEMPLATE_P (tmpl)) |
98c1c668 | 3090 | { |
e1a5ccf7 JM |
3091 | tree new_tmpl; |
3092 | ||
3093 | /* The declaration is a specialization of a member | |
3094 | template, declared outside the class. Therefore, the | |
3095 | innermost template arguments will be NULL, so we | |
3096 | replace them with the arguments determined by the | |
3097 | earlier call to check_explicit_specialization. */ | |
3098 | args = DECL_TI_ARGS (decl); | |
3099 | ||
3100 | new_tmpl | |
c7222c02 MM |
3101 | = build_template_decl (decl, current_template_parms, |
3102 | member_template_p); | |
e1a5ccf7 JM |
3103 | DECL_TEMPLATE_RESULT (new_tmpl) = decl; |
3104 | TREE_TYPE (new_tmpl) = TREE_TYPE (decl); | |
3105 | DECL_TI_TEMPLATE (decl) = new_tmpl; | |
3106 | SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl); | |
e1b3e07d MM |
3107 | DECL_TEMPLATE_INFO (new_tmpl) |
3108 | = tree_cons (tmpl, args, NULL_TREE); | |
e1a5ccf7 | 3109 | |
f9a7ae04 MM |
3110 | register_specialization (new_tmpl, |
3111 | most_general_template (tmpl), | |
3112 | args); | |
e1a5ccf7 | 3113 | return decl; |
98c1c668 | 3114 | } |
98c1c668 | 3115 | |
e1a5ccf7 | 3116 | /* Make sure the template headers we got make sense. */ |
6633d636 | 3117 | |
e1a5ccf7 JM |
3118 | parms = DECL_TEMPLATE_PARMS (tmpl); |
3119 | i = TMPL_PARMS_DEPTH (parms); | |
3120 | if (TMPL_ARGS_DEPTH (args) != i) | |
98c1c668 | 3121 | { |
0f51ccfc GDR |
3122 | error ("expected %d levels of template parms for %q#D, got %d", |
3123 | i, decl, TMPL_ARGS_DEPTH (args)); | |
98c1c668 | 3124 | } |
e1a5ccf7 JM |
3125 | else |
3126 | for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms)) | |
3127 | { | |
3128 | a = TMPL_ARGS_LEVEL (args, i); | |
3129 | t = INNERMOST_TEMPLATE_PARMS (parms); | |
3130 | ||
3131 | if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a)) | |
3132 | { | |
3133 | if (current == decl) | |
0f51ccfc GDR |
3134 | error ("got %d template parameters for %q#D", |
3135 | TREE_VEC_LENGTH (a), decl); | |
e1a5ccf7 | 3136 | else |
0f51ccfc GDR |
3137 | error ("got %d template parameters for %q#T", |
3138 | TREE_VEC_LENGTH (a), current); | |
33bd39a2 | 3139 | error (" but %d required", TREE_VEC_LENGTH (t)); |
e1a5ccf7 | 3140 | } |
98c1c668 | 3141 | |
e1a5ccf7 JM |
3142 | /* Perhaps we should also check that the parms are used in the |
3143 | appropriate qualifying scopes in the declarator? */ | |
6633d636 | 3144 | |
e1a5ccf7 JM |
3145 | if (current == decl) |
3146 | current = ctx; | |
3147 | else | |
3148 | current = TYPE_CONTEXT (current); | |
3149 | } | |
5566b478 | 3150 | } |
8d08fdba | 3151 | |
5566b478 MS |
3152 | DECL_TEMPLATE_RESULT (tmpl) = decl; |
3153 | TREE_TYPE (tmpl) = TREE_TYPE (decl); | |
8d08fdba | 3154 | |
36a117a5 MM |
3155 | /* Push template declarations for global functions and types. Note |
3156 | that we do not try to push a global template friend declared in a | |
3157 | template class; such a thing may well depend on the template | |
39c01e4c | 3158 | parameters of the class. */ |
cfe507be | 3159 | if (new_template_p && !ctx |
36a117a5 | 3160 | && !(is_friend && template_class_depth (current_class_type) > 0)) |
c6f9f83b KL |
3161 | { |
3162 | tmpl = pushdecl_namespace_level (tmpl); | |
3163 | if (tmpl == error_mark_node) | |
3164 | return error_mark_node; | |
3165 | } | |
8d08fdba | 3166 | |
5566b478 | 3167 | if (primary) |
5dd236e2 NS |
3168 | { |
3169 | DECL_PRIMARY_TEMPLATE (tmpl) = tmpl; | |
3170 | if (DECL_CONV_FN_P (tmpl)) | |
3171 | { | |
6e04241f GS |
3172 | int depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)); |
3173 | ||
5dd236e2 NS |
3174 | /* It is a conversion operator. See if the type converted to |
3175 | depends on innermost template operands. */ | |
3176 | ||
d43f603d KL |
3177 | if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl)), |
3178 | depth)) | |
5dd236e2 NS |
3179 | DECL_TEMPLATE_CONV_FN_P (tmpl) = 1; |
3180 | } | |
3181 | } | |
5566b478 | 3182 | |
a692ad2e | 3183 | /* The DECL_TI_ARGS of DECL contains full set of arguments referring |
28ba38e3 KL |
3184 | back to its most general template. If TMPL is a specialization, |
3185 | ARGS may only have the innermost set of arguments. Add the missing | |
3186 | argument levels if necessary. */ | |
3187 | if (DECL_TEMPLATE_INFO (tmpl)) | |
3188 | args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args); | |
3189 | ||
e1b3e07d | 3190 | info = tree_cons (tmpl, args, NULL_TREE); |
5566b478 | 3191 | |
9188c363 | 3192 | if (DECL_IMPLICIT_TYPEDEF_P (decl)) |
8d08fdba | 3193 | { |
ed44da02 MM |
3194 | SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info); |
3195 | if ((!ctx || TREE_CODE (ctx) != FUNCTION_DECL) | |
b60ecc04 MM |
3196 | && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE |
3197 | /* Don't change the name if we've already set it up. */ | |
3198 | && !IDENTIFIER_TEMPLATE (DECL_NAME (decl))) | |
75650646 | 3199 | DECL_NAME (decl) = classtype_mangled_name (TREE_TYPE (decl)); |
51c184be | 3200 | } |
2aaf816d | 3201 | else if (DECL_LANG_SPECIFIC (decl)) |
5566b478 | 3202 | DECL_TEMPLATE_INFO (decl) = info; |
3ac3d9ea MM |
3203 | |
3204 | return DECL_TEMPLATE_RESULT (tmpl); | |
8d08fdba MS |
3205 | } |
3206 | ||
6757edfe | 3207 | tree |
3a978d72 | 3208 | push_template_decl (tree decl) |
6757edfe MM |
3209 | { |
3210 | return push_template_decl_real (decl, 0); | |
3211 | } | |
3212 | ||
3213 | /* Called when a class template TYPE is redeclared with the indicated | |
3214 | template PARMS, e.g.: | |
7fe6899f MM |
3215 | |
3216 | template <class T> struct S; | |
3217 | template <class T> struct S {}; */ | |
3218 | ||
3219 | void | |
3a978d72 | 3220 | redeclare_class_template (tree type, tree parms) |
7fe6899f | 3221 | { |
3d7de1fa | 3222 | tree tmpl; |
6757edfe | 3223 | tree tmpl_parms; |
7fe6899f MM |
3224 | int i; |
3225 | ||
3d7de1fa MM |
3226 | if (!TYPE_TEMPLATE_INFO (type)) |
3227 | { | |
0f51ccfc | 3228 | error ("%qT is not a template type", type); |
3d7de1fa MM |
3229 | return; |
3230 | } | |
3231 | ||
3232 | tmpl = TYPE_TI_TEMPLATE (type); | |
7fe6899f MM |
3233 | if (!PRIMARY_TEMPLATE_P (tmpl)) |
3234 | /* The type is nested in some template class. Nothing to worry | |
3235 | about here; there are no new template parameters for the nested | |
3236 | type. */ | |
3237 | return; | |
3238 | ||
ee921ddf MM |
3239 | if (!parms) |
3240 | { | |
3241 | error ("template specifiers not specified in declaration of %qD", | |
3242 | tmpl); | |
3243 | return; | |
3244 | } | |
3245 | ||
6757edfe MM |
3246 | parms = INNERMOST_TEMPLATE_PARMS (parms); |
3247 | tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl); | |
3248 | ||
7fe6899f MM |
3249 | if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms)) |
3250 | { | |
0f51ccfc | 3251 | cp_error_at ("previous declaration %qD", tmpl); |
33bd39a2 | 3252 | error ("used %d template parameter%s instead of %d", |
7fe6899f MM |
3253 | TREE_VEC_LENGTH (tmpl_parms), |
3254 | TREE_VEC_LENGTH (tmpl_parms) == 1 ? "" : "s", | |
3255 | TREE_VEC_LENGTH (parms)); | |
3256 | return; | |
3257 | } | |
3258 | ||
3259 | for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i) | |
3260 | { | |
3261 | tree tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i)); | |
3262 | tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i)); | |
3263 | tree tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)); | |
3264 | tree parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i)); | |
3265 | ||
2649701f KL |
3266 | /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or |
3267 | TEMPLATE_DECL. */ | |
3268 | if (TREE_CODE (tmpl_parm) != TREE_CODE (parm) | |
3269 | || (TREE_CODE (tmpl_parm) != TYPE_DECL | |
3270 | && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))) | |
7fe6899f | 3271 | { |
0f51ccfc GDR |
3272 | cp_error_at ("template parameter %q#D", tmpl_parm); |
3273 | error ("redeclared here as %q#D", parm); | |
7fe6899f MM |
3274 | return; |
3275 | } | |
3276 | ||
3277 | if (tmpl_default != NULL_TREE && parm_default != NULL_TREE) | |
3278 | { | |
3279 | /* We have in [temp.param]: | |
3280 | ||
3281 | A template-parameter may not be given default arguments | |
3282 | by two different declarations in the same scope. */ | |
0f51ccfc | 3283 | error ("redefinition of default argument for %q#D", parm); |
ddd2d57e | 3284 | error ("%J original definition appeared here", tmpl_parm); |
7fe6899f MM |
3285 | return; |
3286 | } | |
3287 | ||
3288 | if (parm_default != NULL_TREE) | |
3289 | /* Update the previous template parameters (which are the ones | |
3290 | that will really count) with the new default value. */ | |
3291 | TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default; | |
7ce74c5e MM |
3292 | else if (tmpl_default != NULL_TREE) |
3293 | /* Update the new parameters, too; they'll be used as the | |
3294 | parameters for any members. */ | |
3295 | TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default; | |
7fe6899f MM |
3296 | } |
3297 | } | |
75650646 | 3298 | |
9baa27a9 MM |
3299 | /* Simplify EXPR if it is a non-dependent expression. Returns the |
3300 | (possibly simplified) expression. */ | |
3301 | ||
3302 | tree | |
3303 | fold_non_dependent_expr (tree expr) | |
3304 | { | |
3305 | /* If we're in a template, but EXPR isn't value dependent, simplify | |
3306 | it. We're supposed to treat: | |
3307 | ||
3308 | template <typename T> void f(T[1 + 1]); | |
3309 | template <typename T> void f(T[2]); | |
3310 | ||
3311 | as two declarations of the same function, for example. */ | |
3312 | if (processing_template_decl | |
3313 | && !type_dependent_expression_p (expr) | |
3314 | && !value_dependent_expression_p (expr)) | |
3315 | { | |
3316 | HOST_WIDE_INT saved_processing_template_decl; | |
3317 | ||
3318 | saved_processing_template_decl = processing_template_decl; | |
3319 | processing_template_decl = 0; | |
3320 | expr = tsubst_copy_and_build (expr, | |
3321 | /*args=*/NULL_TREE, | |
3322 | tf_error, | |
3323 | /*in_decl=*/NULL_TREE, | |
3324 | /*function_p=*/false); | |
3325 | processing_template_decl = saved_processing_template_decl; | |
3326 | } | |
3327 | return expr; | |
3328 | } | |
3329 | ||
b6ab6892 GB |
3330 | /* EXPR is an expression which is used in a constant-expression context. |
3331 | For instance, it could be a VAR_DECL with a constant initializer. | |
3332 | Extract the innest constant expression. | |
3333 | ||
8a784e4a NS |
3334 | This is basically a more powerful version of |
3335 | integral_constant_value, which can be used also in templates where | |
3336 | initializers can maintain a syntactic rather than semantic form | |
3337 | (even if they are non-dependent, for access-checking purposes). */ | |
b6ab6892 GB |
3338 | |
3339 | tree | |
3340 | fold_decl_constant_value (tree expr) | |
3341 | { | |
4ef69b83 GB |
3342 | tree const_expr = expr; |
3343 | do | |
b6ab6892 | 3344 | { |
b6ab6892 | 3345 | expr = fold_non_dependent_expr (const_expr); |
4ef69b83 | 3346 | const_expr = integral_constant_value (expr); |
b6ab6892 | 3347 | } |
4ef69b83 | 3348 | while (expr != const_expr); |
b6ab6892 | 3349 | |
8a784e4a | 3350 | return expr; |
b6ab6892 GB |
3351 | } |
3352 | ||
3353 | /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which | |
3354 | must be a function or a pointer-to-function type, as specified | |
3355 | in [temp.arg.nontype]: disambiguate EXPR if it is an overload set, | |
3356 | and check that the resulting function has external linkage. */ | |
3357 | ||
3358 | static tree | |
3359 | convert_nontype_argument_function (tree type, tree expr) | |
3360 | { | |
3361 | tree fns = expr; | |
3362 | tree fn, fn_no_ptr; | |
3363 | ||
3364 | fn = instantiate_type (type, fns, tf_none); | |
3365 | if (fn == error_mark_node) | |
3366 | return error_mark_node; | |
3367 | ||
3368 | fn_no_ptr = fn; | |
3369 | if (TREE_CODE (fn_no_ptr) == ADDR_EXPR) | |
3370 | fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0); | |
3371 | ||
3372 | /* [temp.arg.nontype]/1 | |
3373 | ||
3374 | A template-argument for a non-type, non-template template-parameter | |
3375 | shall be one of: | |
3376 | [...] | |
3377 | -- the address of an object or function with external linkage. */ | |
3378 | if (!DECL_EXTERNAL_LINKAGE_P (fn_no_ptr)) | |
3379 | { | |
3380 | error ("%qE is not a valid template argument for type %qT " | |
3381 | "because function %qD has not external linkage", | |
3382 | expr, type, fn_no_ptr); | |
3383 | return NULL_TREE; | |
3384 | } | |
3385 | ||
3386 | return fn; | |
3387 | } | |
3388 | ||
75650646 MM |
3389 | /* Attempt to convert the non-type template parameter EXPR to the |
3390 | indicated TYPE. If the conversion is successful, return the | |
dc957d14 | 3391 | converted value. If the conversion is unsuccessful, return |
75650646 MM |
3392 | NULL_TREE if we issued an error message, or error_mark_node if we |
3393 | did not. We issue error messages for out-and-out bad template | |
3394 | parameters, but not simply because the conversion failed, since we | |
9baa27a9 | 3395 | might be just trying to do argument deduction. Both TYPE and EXPR |
b6ab6892 GB |
3396 | must be non-dependent. |
3397 | ||
3398 | The conversion follows the special rules described in | |
3399 | [temp.arg.nontype], and it is much more strict than an implicit | |
3400 | conversion. | |
3401 | ||
3402 | This function is called twice for each template argument (see | |
3403 | lookup_template_class for a more accurate description of this | |
3404 | problem). This means that we need to handle expressions which | |
3405 | are not valid in a C++ source, but can be created from the | |
3406 | first call (for instance, casts to perform conversions). These | |
3407 | hacks can go away after we fix the double coercion problem. */ | |
75650646 MM |
3408 | |
3409 | static tree | |
3a978d72 | 3410 | convert_nontype_argument (tree type, tree expr) |
75650646 | 3411 | { |
9baa27a9 MM |
3412 | tree expr_type; |
3413 | ||
b6ab6892 GB |
3414 | /* Detect immediately string literals as invalid non-type argument. |
3415 | This special-case is not needed for correctness (we would easily | |
3416 | catch this later), but only to provide better diagnostic for this | |
3417 | common user mistake. As suggested by DR 100, we do not mention | |
3418 | linkage issues in the diagnostic as this is not the point. */ | |
3419 | if (TREE_CODE (expr) == STRING_CST) | |
3420 | { | |
3421 | error ("%qE is not a valid template argument for type %qT " | |
3422 | "because string literals can never be used in this context", | |
3423 | expr, type); | |
3424 | return NULL_TREE; | |
3425 | } | |
3426 | ||
9baa27a9 MM |
3427 | /* If we are in a template, EXPR may be non-dependent, but still |
3428 | have a syntactic, rather than semantic, form. For example, EXPR | |
3429 | might be a SCOPE_REF, rather than the VAR_DECL to which the | |
3430 | SCOPE_REF refers. Preserving the qualifying scope is necessary | |
3431 | so that access checking can be performed when the template is | |
3432 | instantiated -- but here we need the resolved form so that we can | |
3433 | convert the argument. */ | |
3434 | expr = fold_non_dependent_expr (expr); | |
3435 | expr_type = TREE_TYPE (expr); | |
75650646 | 3436 | |
b6ab6892 GB |
3437 | /* HACK: Due to double coercion, we can get a |
3438 | NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here, | |
3439 | which is the tree that we built on the first call (see | |
3440 | below when coercing to reference to object or to reference to | |
3441 | function). We just strip everything and get to the arg. | |
3442 | See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C | |
3443 | for examples. */ | |
3444 | if (TREE_CODE (expr) == NOP_EXPR) | |
75650646 | 3445 | { |
b6ab6892 | 3446 | if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type)) |
75650646 | 3447 | { |
b6ab6892 GB |
3448 | /* ??? Maybe we could use convert_from_reference here, but we |
3449 | would need to relax its constraints because the NOP_EXPR | |
3450 | could actually change the type to something more cv-qualified, | |
3451 | and this is not folded by convert_from_reference. */ | |
3452 | tree addr = TREE_OPERAND (expr, 0); | |
3453 | gcc_assert (TREE_CODE (expr_type) == REFERENCE_TYPE); | |
3454 | gcc_assert (TREE_CODE (addr) == ADDR_EXPR); | |
3455 | gcc_assert (TREE_CODE (TREE_TYPE (addr)) == POINTER_TYPE); | |
3456 | gcc_assert (same_type_ignoring_top_level_qualifiers_p | |
3457 | (TREE_TYPE (expr_type), | |
3458 | TREE_TYPE (TREE_TYPE (addr)))); | |
3459 | ||
3460 | expr = TREE_OPERAND (addr, 0); | |
3461 | expr_type = TREE_TYPE (expr); | |
75650646 MM |
3462 | } |
3463 | ||
b6ab6892 GB |
3464 | /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the |
3465 | parameter is a pointer to object, through decay and | |
3466 | qualification conversion. Let's strip everything. */ | |
3467 | else if (TYPE_PTROBV_P (type)) | |
75650646 | 3468 | { |
b6ab6892 GB |
3469 | STRIP_NOPS (expr); |
3470 | gcc_assert (TREE_CODE (expr) == ADDR_EXPR); | |
3471 | gcc_assert (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE); | |
3472 | /* Skip the ADDR_EXPR only if it is part of the decay for | |
3473 | an array. Otherwise, it is part of the original argument | |
3474 | in the source code. */ | |
3475 | if (TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == ARRAY_TYPE) | |
3476 | expr = TREE_OPERAND (expr, 0); | |
3477 | expr_type = TREE_TYPE (expr); | |
f3400fe2 | 3478 | } |
75650646 | 3479 | } |
c61dce3a | 3480 | |
b6ab6892 | 3481 | /* [temp.arg.nontype]/5, bullet 1 |
75650646 | 3482 | |
b6ab6892 GB |
3483 | For a non-type template-parameter of integral or enumeration type, |
3484 | integral promotions (_conv.prom_) and integral conversions | |
3485 | (_conv.integral_) are applied. */ | |
3486 | if (INTEGRAL_TYPE_P (type)) | |
75650646 | 3487 | { |
75650646 MM |
3488 | if (!INTEGRAL_TYPE_P (expr_type)) |
3489 | return error_mark_node; | |
fddabb2c | 3490 | |
b6ab6892 GB |
3491 | expr = fold_decl_constant_value (expr); |
3492 | /* Notice that there are constant expressions like '4 % 0' which | |
3493 | do not fold into integer constants. */ | |
db02b6b9 | 3494 | if (TREE_CODE (expr) != INTEGER_CST) |
b6ab6892 GB |
3495 | { |
3496 | error ("%qE is not a valid template argument for type %qT " | |
3497 | "because it is a non-constant expression", expr, type); | |
3498 | return NULL_TREE; | |
3499 | } | |
75650646 | 3500 | |
b6ab6892 GB |
3501 | /* At this point, an implicit conversion does what we want, |
3502 | because we already know that the expression is of integral | |
3503 | type. */ | |
3504 | expr = ocp_convert (type, expr, CONV_IMPLICIT, LOOKUP_PROTECT); | |
3505 | if (expr == error_mark_node) | |
3506 | return error_mark_node; | |
75650646 | 3507 | |
b6ab6892 GB |
3508 | /* Conversion was allowed: fold it to a bare integer constant. */ |
3509 | expr = fold (expr); | |
3510 | } | |
3511 | /* [temp.arg.nontype]/5, bullet 2 | |
75650646 | 3512 | |
b6ab6892 GB |
3513 | For a non-type template-parameter of type pointer to object, |
3514 | qualification conversions (_conv.qual_) and the array-to-pointer | |
3515 | conversion (_conv.array_) are applied. */ | |
3516 | else if (TYPE_PTROBV_P (type)) | |
3517 | { | |
3518 | /* [temp.arg.nontype]/1 (TC1 version, DR 49): | |
75650646 | 3519 | |
b6ab6892 GB |
3520 | A template-argument for a non-type, non-template template-parameter |
3521 | shall be one of: [...] | |
75650646 | 3522 | |
b6ab6892 GB |
3523 | -- the name of a non-type template-parameter; |
3524 | -- the address of an object or function with external linkage, [...] | |
3525 | expressed as "& id-expression" where the & is optional if the name | |
3526 | refers to a function or array, or if the corresponding | |
3527 | template-parameter is a reference. | |
e1467ff2 | 3528 | |
b6ab6892 GB |
3529 | Here, we do not care about functions, as they are invalid anyway |
3530 | for a parameter of type pointer-to-object. */ | |
3531 | bool constant_address_p = | |
3532 | (TREE_CODE (expr) == ADDR_EXPR | |
3533 | || TREE_CODE (expr_type) == ARRAY_TYPE | |
3534 | || (DECL_P (expr) && DECL_TEMPLATE_PARM_P (expr))); | |
3535 | ||
3536 | expr = decay_conversion (expr); | |
3537 | if (expr == error_mark_node) | |
3538 | return error_mark_node; | |
75650646 | 3539 | |
b6ab6892 GB |
3540 | expr = perform_qualification_conversions (type, expr); |
3541 | if (expr == error_mark_node) | |
3542 | return error_mark_node; | |
0dc09a61 | 3543 | |
b6ab6892 GB |
3544 | if (!constant_address_p) |
3545 | { | |
3546 | error ("%qE is not a valid template argument for type %qT " | |
3547 | "because it is not a constant pointer", expr, type); | |
3548 | return NULL_TREE; | |
3549 | } | |
3550 | } | |
3551 | /* [temp.arg.nontype]/5, bullet 3 | |
3552 | ||
3553 | For a non-type template-parameter of type reference to object, no | |
3554 | conversions apply. The type referred to by the reference may be more | |
3555 | cv-qualified than the (otherwise identical) type of the | |
3556 | template-argument. The template-parameter is bound directly to the | |
3557 | template-argument, which must be an lvalue. */ | |
3558 | else if (TYPE_REF_OBJ_P (type)) | |
3559 | { | |
3560 | if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type), | |
3561 | expr_type)) | |
3562 | return error_mark_node; | |
75650646 | 3563 | |
b6ab6892 GB |
3564 | if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type)) |
3565 | { | |
3566 | error ("%qE is not a valid template argument for type %qT " | |
3567 | "because of conflicts in cv-qualification", expr, type); | |
3568 | return NULL_TREE; | |
3569 | } | |
3570 | ||
3571 | if (!real_lvalue_p (expr)) | |
3572 | { | |
3573 | error ("%qE is not a valid template argument for type %qT " | |
3574 | "because it is not a lvalue", expr, type); | |
3575 | return NULL_TREE; | |
3576 | } | |
e6e174e5 | 3577 | |
b6ab6892 | 3578 | /* [temp.arg.nontype]/1 |
75650646 | 3579 | |
b6ab6892 GB |
3580 | A template-argument for a non-type, non-template template-parameter |
3581 | shall be one of: [...] | |
75650646 | 3582 | |
03fd3f84 | 3583 | -- the address of an object or function with external linkage. */ |
b6ab6892 GB |
3584 | if (!DECL_EXTERNAL_LINKAGE_P (expr)) |
3585 | { | |
3586 | error ("%qE is not a valid template argument for type %qT " | |
3587 | "because object %qD has not external linkage", | |
3588 | expr, type, expr); | |
3589 | return NULL_TREE; | |
3590 | } | |
0dc09a61 | 3591 | |
b6ab6892 GB |
3592 | expr = build_nop (type, build_address (expr)); |
3593 | } | |
3594 | /* [temp.arg.nontype]/5, bullet 4 | |
3595 | ||
3596 | For a non-type template-parameter of type pointer to function, only | |
3597 | the function-to-pointer conversion (_conv.func_) is applied. If the | |
3598 | template-argument represents a set of overloaded functions (or a | |
3599 | pointer to such), the matching function is selected from the set | |
3600 | (_over.over_). */ | |
3601 | else if (TYPE_PTRFN_P (type)) | |
3602 | { | |
3603 | /* If the argument is a template-id, we might not have enough | |
3604 | context information to decay the pointer. | |
3605 | ??? Why static5.C requires decay and subst1.C works fine | |
3606 | even without it? */ | |
3607 | if (!type_unknown_p (expr_type)) | |
3608 | { | |
3609 | expr = decay_conversion (expr); | |
3610 | if (expr == error_mark_node) | |
3611 | return error_mark_node; | |
3612 | } | |
75650646 | 3613 | |
b6ab6892 GB |
3614 | expr = convert_nontype_argument_function (type, expr); |
3615 | if (!expr || expr == error_mark_node) | |
3616 | return expr; | |
3617 | } | |
3618 | /* [temp.arg.nontype]/5, bullet 5 | |
75650646 | 3619 | |
b6ab6892 GB |
3620 | For a non-type template-parameter of type reference to function, no |
3621 | conversions apply. If the template-argument represents a set of | |
3622 | overloaded functions, the matching function is selected from the set | |
3623 | (_over.over_). */ | |
3624 | else if (TYPE_REFFN_P (type)) | |
3625 | { | |
3626 | if (TREE_CODE (expr) == ADDR_EXPR) | |
3627 | { | |
3628 | error ("%qE is not a valid template argument for type %qT " | |
3629 | "because it is a pointer", expr, type); | |
3630 | inform ("try using %qE instead", TREE_OPERAND (expr, 0)); | |
3631 | return NULL_TREE; | |
3632 | } | |
75650646 | 3633 | |
b6ab6892 GB |
3634 | expr = convert_nontype_argument_function (TREE_TYPE (type), expr); |
3635 | if (!expr || expr == error_mark_node) | |
3636 | return expr; | |
75650646 | 3637 | |
b6ab6892 GB |
3638 | expr = build_nop(type, build_address (expr)); |
3639 | } | |
3640 | /* [temp.arg.nontype]/5, bullet 6 | |
75650646 | 3641 | |
b6ab6892 GB |
3642 | For a non-type template-parameter of type pointer to member function, |
3643 | no conversions apply. If the template-argument represents a set of | |
3644 | overloaded member functions, the matching member function is selected | |
3645 | from the set (_over.over_). */ | |
3646 | else if (TYPE_PTRMEMFUNC_P (type)) | |
3647 | { | |
3648 | expr = instantiate_type (type, expr, tf_none); | |
3649 | if (expr == error_mark_node) | |
3650 | return error_mark_node; | |
75650646 | 3651 | |
b6ab6892 GB |
3652 | /* There is no way to disable standard conversions in |
3653 | resolve_address_of_overloaded_function (called by | |
3654 | instantiate_type). It is possible that the call succeeded by | |
3655 | converting &B::I to &D::I (where B is a base of D), so we need | |
3656 | to reject this conversion here. | |
75650646 | 3657 | |
b6ab6892 GB |
3658 | Actually, even if there was a way to disable standard conversions, |
3659 | it would still be better to reject them here so that we can | |
3660 | provide a superior diagnostic. */ | |
3661 | if (!same_type_p (TREE_TYPE (expr), type)) | |
3662 | { | |
3663 | /* Make sure we are just one standard conversion off. */ | |
3664 | gcc_assert (can_convert (type, TREE_TYPE (expr))); | |
3665 | error ("%qE is not a valid template argument for type %qT " | |
3666 | "because it is of type %qT", expr, type, | |
3667 | TREE_TYPE (expr)); | |
3668 | inform ("standard conversions are not allowed in this context"); | |
3669 | return NULL_TREE; | |
3670 | } | |
3671 | } | |
3672 | /* [temp.arg.nontype]/5, bullet 7 | |
59e7a901 | 3673 | |
b6ab6892 GB |
3674 | For a non-type template-parameter of type pointer to data member, |
3675 | qualification conversions (_conv.qual_) are applied. */ | |
3676 | else if (TYPE_PTRMEM_P (type)) | |
3677 | { | |
3678 | expr = perform_qualification_conversions (type, expr); | |
3679 | if (expr == error_mark_node) | |
75650646 | 3680 | return expr; |
75650646 | 3681 | } |
b6ab6892 GB |
3682 | /* A template non-type parameter must be one of the above. */ |
3683 | else | |
3684 | gcc_unreachable (); | |
75650646 | 3685 | |
b6ab6892 GB |
3686 | /* Sanity check: did we actually convert the argument to the |
3687 | right type? */ | |
3688 | gcc_assert (same_type_p (type, TREE_TYPE (expr))); | |
3689 | return expr; | |
75650646 MM |
3690 | } |
3691 | ||
b6ab6892 | 3692 | |
744fac59 KL |
3693 | /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for |
3694 | template template parameters. Both PARM_PARMS and ARG_PARMS are | |
3695 | vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL | |
3696 | or PARM_DECL. | |
3697 | ||
3698 | ARG_PARMS may contain more parameters than PARM_PARMS. If this is | |
3699 | the case, then extra parameters must have default arguments. | |
3700 | ||
3701 | Consider the example: | |
3702 | template <class T, class Allocator = allocator> class vector; | |
3703 | template<template <class U> class TT> class C; | |
3704 | ||
3705 | C<vector> is a valid instantiation. PARM_PARMS for the above code | |
3706 | contains a TYPE_DECL (for U), ARG_PARMS contains two TYPE_DECLs (for | |
3707 | T and Allocator) and OUTER_ARGS contains the argument that is used to | |
3708 | substitute the TT parameter. */ | |
3709 | ||
3710 | static int | |
3a978d72 NN |
3711 | coerce_template_template_parms (tree parm_parms, |
3712 | tree arg_parms, | |
3713 | tsubst_flags_t complain, | |
3714 | tree in_decl, | |
3715 | tree outer_args) | |
744fac59 KL |
3716 | { |
3717 | int nparms, nargs, i; | |
3718 | tree parm, arg; | |
3719 | ||
50bc768d NS |
3720 | gcc_assert (TREE_CODE (parm_parms) == TREE_VEC); |
3721 | gcc_assert (TREE_CODE (arg_parms) == TREE_VEC); | |
744fac59 KL |
3722 | |
3723 | nparms = TREE_VEC_LENGTH (parm_parms); | |
3724 | nargs = TREE_VEC_LENGTH (arg_parms); | |
3725 | ||
3726 | /* The rule here is opposite of coerce_template_parms. */ | |
3727 | if (nargs < nparms | |
3728 | || (nargs > nparms | |
3729 | && TREE_PURPOSE (TREE_VEC_ELT (arg_parms, nparms)) == NULL_TREE)) | |
3730 | return 0; | |
3731 | ||
3732 | for (i = 0; i < nparms; ++i) | |
3733 | { | |
3734 | parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i)); | |
3735 | arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i)); | |
3736 | ||
3737 | if (arg == NULL_TREE || arg == error_mark_node | |
3738 | || parm == NULL_TREE || parm == error_mark_node) | |
3739 | return 0; | |
3740 | ||
3741 | if (TREE_CODE (arg) != TREE_CODE (parm)) | |
3742 | return 0; | |
3743 | ||
3744 | switch (TREE_CODE (parm)) | |
3745 | { | |
3746 | case TYPE_DECL: | |
3747 | break; | |
3748 | ||
3749 | case TEMPLATE_DECL: | |
3750 | /* We encounter instantiations of templates like | |
3751 | template <template <template <class> class> class TT> | |
3752 | class C; */ | |
700466c2 JM |
3753 | { |
3754 | tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm); | |
3755 | tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg); | |
3756 | ||
8c6ab2db NS |
3757 | if (!coerce_template_template_parms |
3758 | (parmparm, argparm, complain, in_decl, outer_args)) | |
700466c2 JM |
3759 | return 0; |
3760 | } | |
3761 | break; | |
744fac59 KL |
3762 | |
3763 | case PARM_DECL: | |
3764 | /* The tsubst call is used to handle cases such as | |
00bdb87f KL |
3765 | |
3766 | template <int> class C {}; | |
3767 | template <class T, template <T> class TT> class D {}; | |
3768 | D<int, C> d; | |
3769 | ||
744fac59 | 3770 | i.e. the parameter list of TT depends on earlier parameters. */ |
00bdb87f KL |
3771 | if (!dependent_type_p (TREE_TYPE (arg)) |
3772 | && !same_type_p | |
3773 | (tsubst (TREE_TYPE (parm), outer_args, complain, in_decl), | |
3774 | TREE_TYPE (arg))) | |
744fac59 KL |
3775 | return 0; |
3776 | break; | |
3777 | ||
3778 | default: | |
315fb5db | 3779 | gcc_unreachable (); |
744fac59 KL |
3780 | } |
3781 | } | |
3782 | return 1; | |
3783 | } | |
3784 | ||
8b5b8b7c MM |
3785 | /* Convert the indicated template ARG as necessary to match the |
3786 | indicated template PARM. Returns the converted ARG, or | |
c2ea3a40 NS |
3787 | error_mark_node if the conversion was unsuccessful. Error and |
3788 | warning messages are issued under control of COMPLAIN. This | |
3789 | conversion is for the Ith parameter in the parameter list. ARGS is | |
3790 | the full set of template arguments deduced so far. */ | |
8b5b8b7c MM |
3791 | |
3792 | static tree | |
3a978d72 NN |
3793 | convert_template_argument (tree parm, |
3794 | tree arg, | |
3795 | tree args, | |
3796 | tsubst_flags_t complain, | |
3797 | int i, | |
3798 | tree in_decl) | |
8b5b8b7c MM |
3799 | { |
3800 | tree val; | |
3801 | tree inner_args; | |
3802 | int is_type, requires_type, is_tmpl_type, requires_tmpl_type; | |
3803 | ||
f9a7ae04 | 3804 | inner_args = INNERMOST_TEMPLATE_ARGS (args); |
8b5b8b7c MM |
3805 | |
3806 | if (TREE_CODE (arg) == TREE_LIST | |
a5ac359a | 3807 | && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF) |
8b5b8b7c MM |
3808 | { |
3809 | /* The template argument was the name of some | |
3810 | member function. That's usually | |
0e339752 | 3811 | invalid, but static members are OK. In any |
8b5b8b7c MM |
3812 | case, grab the underlying fields/functions |
3813 | and issue an error later if required. */ | |
3814 | arg = TREE_VALUE (arg); | |
3815 | TREE_TYPE (arg) = unknown_type_node; | |
3816 | } | |
3817 | ||
3818 | requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL; | |
3819 | requires_type = (TREE_CODE (parm) == TYPE_DECL | |
3820 | || requires_tmpl_type); | |
3821 | ||
cbd63935 KL |
3822 | is_tmpl_type = ((TREE_CODE (arg) == TEMPLATE_DECL |
3823 | && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL) | |
3824 | || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM | |
3825 | || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE); | |
303d1c55 | 3826 | |
b8c6534b KL |
3827 | if (is_tmpl_type |
3828 | && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM | |
3829 | || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)) | |
8b5b8b7c | 3830 | arg = TYPE_STUB_DECL (arg); |
8b5b8b7c | 3831 | |
2f939d94 | 3832 | is_type = TYPE_P (arg) || is_tmpl_type; |
8b5b8b7c MM |
3833 | |
3834 | if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF | |
3835 | && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM) | |
3836 | { | |
0f51ccfc GDR |
3837 | pedwarn ("to refer to a type member of a template parameter, " |
3838 | "use %<typename %E%>", arg); | |
8b5b8b7c MM |
3839 | |
3840 | arg = make_typename_type (TREE_OPERAND (arg, 0), | |
3baa501d | 3841 | TREE_OPERAND (arg, 1), |
fc6a28d7 | 3842 | typename_type, |
c2ea3a40 | 3843 | complain & tf_error); |
8b5b8b7c MM |
3844 | is_type = 1; |
3845 | } | |
3846 | if (is_type != requires_type) | |
3847 | { | |
3848 | if (in_decl) | |
3849 | { | |
c2ea3a40 | 3850 | if (complain & tf_error) |
8b5b8b7c | 3851 | { |
0f51ccfc GDR |
3852 | error ("type/value mismatch at argument %d in template " |
3853 | "parameter list for %qD", | |
3854 | i + 1, in_decl); | |
8b5b8b7c | 3855 | if (is_type) |
0f51ccfc GDR |
3856 | error (" expected a constant of type %qT, got %qT", |
3857 | TREE_TYPE (parm), | |
3858 | (is_tmpl_type ? DECL_NAME (arg) : arg)); | |
d12a7283 | 3859 | else if (requires_tmpl_type) |
0f51ccfc | 3860 | error (" expected a class template, got %qE", arg); |
8b5b8b7c | 3861 | else |
0f51ccfc | 3862 | error (" expected a type, got %qE", arg); |
8b5b8b7c MM |
3863 | } |
3864 | } | |
3865 | return error_mark_node; | |
3866 | } | |
3867 | if (is_tmpl_type ^ requires_tmpl_type) | |
3868 | { | |
c2ea3a40 | 3869 | if (in_decl && (complain & tf_error)) |
8b5b8b7c | 3870 | { |
0f51ccfc GDR |
3871 | error ("type/value mismatch at argument %d in template " |
3872 | "parameter list for %qD", | |
3873 | i + 1, in_decl); | |
8b5b8b7c | 3874 | if (is_tmpl_type) |
0f51ccfc | 3875 | error (" expected a type, got %qT", DECL_NAME (arg)); |
8b5b8b7c | 3876 | else |
0f51ccfc | 3877 | error (" expected a class template, got %qT", arg); |
8b5b8b7c MM |
3878 | } |
3879 | return error_mark_node; | |
3880 | } | |
3881 | ||
3882 | if (is_type) | |
3883 | { | |
3884 | if (requires_tmpl_type) | |
3885 | { | |
b8c6534b KL |
3886 | if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE) |
3887 | /* The number of argument required is not known yet. | |
3888 | Just accept it for now. */ | |
3889 | val = TREE_TYPE (arg); | |
8b5b8b7c MM |
3890 | else |
3891 | { | |
b8c6534b KL |
3892 | tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm); |
3893 | tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg); | |
3894 | ||
3895 | if (coerce_template_template_parms (parmparm, argparm, | |
3896 | complain, in_decl, | |
3897 | inner_args)) | |
8b5b8b7c | 3898 | { |
b8c6534b KL |
3899 | val = arg; |
3900 | ||
3901 | /* TEMPLATE_TEMPLATE_PARM node is preferred over | |
3902 | TEMPLATE_DECL. */ | |
3903 | if (val != error_mark_node | |
3904 | && DECL_TEMPLATE_TEMPLATE_PARM_P (val)) | |
3905 | val = TREE_TYPE (val); | |
8b5b8b7c | 3906 | } |
b8c6534b KL |
3907 | else |
3908 | { | |
c2ea3a40 | 3909 | if (in_decl && (complain & tf_error)) |
b8c6534b | 3910 | { |
0f51ccfc GDR |
3911 | error ("type/value mismatch at argument %d in " |
3912 | "template parameter list for %qD", | |
3913 | i + 1, in_decl); | |
3914 | error (" expected a template of type %qD, got %qD", | |
3915 | parm, arg); | |
b8c6534b | 3916 | } |
8b5b8b7c | 3917 | |
b8c6534b KL |
3918 | val = error_mark_node; |
3919 | } | |
8b5b8b7c MM |
3920 | } |
3921 | } | |
3922 | else | |
058b15c1 | 3923 | val = arg; |
8b5b8b7c MM |
3924 | } |
3925 | else | |
3926 | { | |
4393e105 | 3927 | tree t = tsubst (TREE_TYPE (parm), args, complain, in_decl); |
8b5b8b7c | 3928 | |
11b810f1 | 3929 | if (invalid_nontype_parm_type_p (t, complain)) |
db3f4e4e NS |
3930 | return error_mark_node; |
3931 | ||
d768a589 | 3932 | if (!uses_template_parms (arg) && !uses_template_parms (t)) |
8b5b8b7c MM |
3933 | /* We used to call digest_init here. However, digest_init |
3934 | will report errors, which we don't want when complain | |
3935 | is zero. More importantly, digest_init will try too | |
3936 | hard to convert things: for example, `0' should not be | |
3937 | converted to pointer type at this point according to | |
3938 | the standard. Accepting this is not merely an | |
3939 | extension, since deciding whether or not these | |
3940 | conversions can occur is part of determining which | |
dc957d14 | 3941 | function template to call, or whether a given explicit |
0e339752 | 3942 | argument specification is valid. */ |
8b5b8b7c MM |
3943 | val = convert_nontype_argument (t, arg); |
3944 | else | |
3945 | val = arg; | |
3946 | ||
3947 | if (val == NULL_TREE) | |
3948 | val = error_mark_node; | |
c2ea3a40 | 3949 | else if (val == error_mark_node && (complain & tf_error)) |
0f51ccfc | 3950 | error ("could not convert template argument %qE to %qT", arg, t); |
8b5b8b7c MM |
3951 | } |
3952 | ||
3953 | return val; | |
3954 | } | |
3955 | ||
3956 | /* Convert all template arguments to their appropriate types, and | |
3957 | return a vector containing the innermost resulting template | |
c2ea3a40 | 3958 | arguments. If any error occurs, return error_mark_node. Error and |
3e4a3562 | 3959 | warning messages are issued under control of COMPLAIN. |
75650646 | 3960 | |
838dfd8a | 3961 | If REQUIRE_ALL_ARGUMENTS is nonzero, all arguments must be |
75650646 MM |
3962 | provided in ARGLIST, or else trailing parameters must have default |
3963 | values. If REQUIRE_ALL_ARGUMENTS is zero, we will attempt argument | |
6dfbb909 | 3964 | deduction for any unspecified trailing arguments. */ |
de575009 | 3965 | |
8d08fdba | 3966 | static tree |
3a978d72 NN |
3967 | coerce_template_parms (tree parms, |
3968 | tree args, | |
3969 | tree in_decl, | |
3970 | tsubst_flags_t complain, | |
3971 | int require_all_arguments) | |
8d08fdba | 3972 | { |
a292b002 | 3973 | int nparms, nargs, i, lost = 0; |
e4a84209 | 3974 | tree inner_args; |
8b5b8b7c MM |
3975 | tree new_args; |
3976 | tree new_inner_args; | |
a292b002 | 3977 | |
f9a7ae04 | 3978 | inner_args = INNERMOST_TEMPLATE_ARGS (args); |
bf12d54d | 3979 | nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0; |
a292b002 MS |
3980 | nparms = TREE_VEC_LENGTH (parms); |
3981 | ||
3982 | if (nargs > nparms | |
3983 | || (nargs < nparms | |
75650646 | 3984 | && require_all_arguments |
a292b002 | 3985 | && TREE_PURPOSE (TREE_VEC_ELT (parms, nargs)) == NULL_TREE)) |
8d08fdba | 3986 | { |
c2ea3a40 | 3987 | if (complain & tf_error) |
75650646 | 3988 | { |
33bd39a2 | 3989 | error ("wrong number of template arguments (%d, should be %d)", |
0f51ccfc | 3990 | nargs, nparms); |
75650646 MM |
3991 | |
3992 | if (in_decl) | |
0f51ccfc | 3993 | cp_error_at ("provided for %qD", in_decl); |
75650646 MM |
3994 | } |
3995 | ||
8d08fdba MS |
3996 | return error_mark_node; |
3997 | } | |
3998 | ||
f31c0a32 | 3999 | new_inner_args = make_tree_vec (nparms); |
8b5b8b7c MM |
4000 | new_args = add_outermost_template_args (args, new_inner_args); |
4001 | for (i = 0; i < nparms; i++) | |
8d08fdba | 4002 | { |
8b5b8b7c MM |
4003 | tree arg; |
4004 | tree parm; | |
e4a84209 | 4005 | |
8b5b8b7c MM |
4006 | /* Get the Ith template parameter. */ |
4007 | parm = TREE_VEC_ELT (parms, i); | |
75650646 | 4008 | |
8b5b8b7c | 4009 | /* Calculate the Ith argument. */ |
bf12d54d | 4010 | if (i < nargs) |
8b5b8b7c | 4011 | arg = TREE_VEC_ELT (inner_args, i); |
ffd49b19 | 4012 | else if (require_all_arguments) |
04c06002 | 4013 | /* There must be a default arg in this case. */ |
a91db711 NS |
4014 | arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args, |
4015 | complain, in_decl); | |
ffd49b19 NS |
4016 | else |
4017 | break; | |
4018 | ||
50bc768d | 4019 | gcc_assert (arg); |
ffd49b19 | 4020 | if (arg == error_mark_node) |
e34b0922 KL |
4021 | { |
4022 | if (complain & tf_error) | |
4023 | error ("template argument %d is invalid", i + 1); | |
4024 | } | |
8b5b8b7c MM |
4025 | else |
4026 | arg = convert_template_argument (TREE_VALUE (parm), | |
4027 | arg, new_args, complain, i, | |
4028 | in_decl); | |
4029 | ||
4030 | if (arg == error_mark_node) | |
8d08fdba | 4031 | lost++; |
8b5b8b7c | 4032 | TREE_VEC_ELT (new_inner_args, i) = arg; |
8d08fdba | 4033 | } |
8b5b8b7c | 4034 | |
8d08fdba MS |
4035 | if (lost) |
4036 | return error_mark_node; | |
8b5b8b7c MM |
4037 | |
4038 | return new_inner_args; | |
8d08fdba MS |
4039 | } |
4040 | ||
34016c81 JM |
4041 | /* Returns 1 if template args OT and NT are equivalent. */ |
4042 | ||
d8e178a0 | 4043 | static int |
3a978d72 | 4044 | template_args_equal (tree ot, tree nt) |
34016c81 JM |
4045 | { |
4046 | if (nt == ot) | |
4047 | return 1; | |
74601d7c | 4048 | |
34016c81 JM |
4049 | if (TREE_CODE (nt) == TREE_VEC) |
4050 | /* For member templates */ | |
74601d7c KL |
4051 | return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt); |
4052 | else if (TYPE_P (nt)) | |
4053 | return TYPE_P (ot) && same_type_p (ot, nt); | |
4054 | else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot)) | |
4055 | return 0; | |
34016c81 | 4056 | else |
c8a209ca | 4057 | return cp_tree_equal (ot, nt); |
34016c81 JM |
4058 | } |
4059 | ||
4060 | /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets | |
f84b4be9 JM |
4061 | of template arguments. Returns 0 otherwise. */ |
4062 | ||
6757edfe | 4063 | int |
3a978d72 | 4064 | comp_template_args (tree oldargs, tree newargs) |
5566b478 MS |
4065 | { |
4066 | int i; | |
4067 | ||
386b8a85 JM |
4068 | if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs)) |
4069 | return 0; | |
4070 | ||
5566b478 MS |
4071 | for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i) |
4072 | { | |
4073 | tree nt = TREE_VEC_ELT (newargs, i); | |
4074 | tree ot = TREE_VEC_ELT (oldargs, i); | |
4075 | ||
34016c81 | 4076 | if (! template_args_equal (ot, nt)) |
61a127b3 | 4077 | return 0; |
5566b478 MS |
4078 | } |
4079 | return 1; | |
4080 | } | |
4081 | ||
8d08fdba MS |
4082 | /* Given class template name and parameter list, produce a user-friendly name |
4083 | for the instantiation. */ | |
e92cc029 | 4084 | |
8d08fdba | 4085 | static char * |
3a978d72 | 4086 | mangle_class_name_for_template (const char* name, tree parms, tree arglist) |
8d08fdba MS |
4087 | { |
4088 | static struct obstack scratch_obstack; | |
4089 | static char *scratch_firstobj; | |
4090 | int i, nparms; | |
8d08fdba MS |
4091 | |
4092 | if (!scratch_firstobj) | |
fc378698 | 4093 | gcc_obstack_init (&scratch_obstack); |
8d08fdba MS |
4094 | else |
4095 | obstack_free (&scratch_obstack, scratch_firstobj); | |
fc378698 | 4096 | scratch_firstobj = obstack_alloc (&scratch_obstack, 1); |
8d08fdba | 4097 | |
18e314ad GS |
4098 | #define ccat(C) obstack_1grow (&scratch_obstack, (C)); |
4099 | #define cat(S) obstack_grow (&scratch_obstack, (S), strlen (S)) | |
8d08fdba MS |
4100 | |
4101 | cat (name); | |
4102 | ccat ('<'); | |
4103 | nparms = TREE_VEC_LENGTH (parms); | |
f9a7ae04 | 4104 | arglist = INNERMOST_TEMPLATE_ARGS (arglist); |
50bc768d | 4105 | gcc_assert (nparms == TREE_VEC_LENGTH (arglist)); |
8d08fdba MS |
4106 | for (i = 0; i < nparms; i++) |
4107 | { | |
a292b002 MS |
4108 | tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i)); |
4109 | tree arg = TREE_VEC_ELT (arglist, i); | |
8d08fdba MS |
4110 | |
4111 | if (i) | |
4112 | ccat (','); | |
4113 | ||
a292b002 | 4114 | if (TREE_CODE (parm) == TYPE_DECL) |
8d08fdba | 4115 | { |
761f0855 | 4116 | cat (type_as_string (arg, TFF_CHASE_TYPEDEF)); |
8d08fdba MS |
4117 | continue; |
4118 | } | |
73b0fce8 KL |
4119 | else if (TREE_CODE (parm) == TEMPLATE_DECL) |
4120 | { | |
4121 | if (TREE_CODE (arg) == TEMPLATE_DECL) | |
2c73f9f5 ML |
4122 | { |
4123 | /* Already substituted with real template. Just output | |
4124 | the template name here */ | |
cb0dbb9a | 4125 | tree context = DECL_CONTEXT (arg); |
ec4f972f AS |
4126 | if (context) |
4127 | { | |
4128 | /* The template may be defined in a namespace, or | |
4129 | may be a member template. */ | |
50bc768d NS |
4130 | gcc_assert (TREE_CODE (context) == NAMESPACE_DECL |
4131 | || CLASS_TYPE_P (context)); | |
4132 | cat (decl_as_string (DECL_CONTEXT (arg), | |
4133 | TFF_PLAIN_IDENTIFIER)); | |
4134 | cat ("::"); | |
2c73f9f5 ML |
4135 | } |
4136 | cat (IDENTIFIER_POINTER (DECL_NAME (arg))); | |
4137 | } | |
73b0fce8 | 4138 | else |
f4f206f4 | 4139 | /* Output the parameter declaration. */ |
761f0855 | 4140 | cat (type_as_string (arg, TFF_CHASE_TYPEDEF)); |
73b0fce8 KL |
4141 | continue; |
4142 | } | |
8d08fdba | 4143 | else |
50bc768d | 4144 | gcc_assert (TREE_CODE (parm) == PARM_DECL); |
8d08fdba | 4145 | |
8d08fdba MS |
4146 | /* No need to check arglist against parmlist here; we did that |
4147 | in coerce_template_parms, called from lookup_template_class. */ | |
761f0855 | 4148 | cat (expr_as_string (arg, TFF_PLAIN_IDENTIFIER)); |
8d08fdba MS |
4149 | } |
4150 | { | |
4151 | char *bufp = obstack_next_free (&scratch_obstack); | |
4152 | int offset = 0; | |
4153 | while (bufp[offset - 1] == ' ') | |
4154 | offset--; | |
4155 | obstack_blank_fast (&scratch_obstack, offset); | |
4156 | ||
4157 | /* B<C<char> >, not B<C<char>> */ | |
4158 | if (bufp[offset - 1] == '>') | |
4159 | ccat (' '); | |
4160 | } | |
4161 | ccat ('>'); | |
4162 | ccat ('\0'); | |
4163 | return (char *) obstack_base (&scratch_obstack); | |
8d08fdba MS |
4164 | } |
4165 | ||
bd6dd845 | 4166 | static tree |
3a978d72 | 4167 | classtype_mangled_name (tree t) |
5566b478 MS |
4168 | { |
4169 | if (CLASSTYPE_TEMPLATE_INFO (t) | |
36a117a5 MM |
4170 | /* Specializations have already had their names set up in |
4171 | lookup_template_class. */ | |
370af2d5 | 4172 | && !CLASSTYPE_TEMPLATE_SPECIALIZATION (t)) |
9fbf56f7 MM |
4173 | { |
4174 | tree tmpl = most_general_template (CLASSTYPE_TI_TEMPLATE (t)); | |
4175 | ||
36a117a5 MM |
4176 | /* For non-primary templates, the template parameters are |
4177 | implicit from their surrounding context. */ | |
9fbf56f7 MM |
4178 | if (PRIMARY_TEMPLATE_P (tmpl)) |
4179 | { | |
4180 | tree name = DECL_NAME (tmpl); | |
4181 | char *mangled_name = mangle_class_name_for_template | |
4182 | (IDENTIFIER_POINTER (name), | |
4183 | DECL_INNERMOST_TEMPLATE_PARMS (tmpl), | |
4184 | CLASSTYPE_TI_ARGS (t)); | |
4185 | tree id = get_identifier (mangled_name); | |
4186 | IDENTIFIER_TEMPLATE (id) = name; | |
4187 | return id; | |
4188 | } | |
5566b478 | 4189 | } |
9fbf56f7 MM |
4190 | |
4191 | return TYPE_IDENTIFIER (t); | |
5566b478 MS |
4192 | } |
4193 | ||
4194 | static void | |
3a978d72 | 4195 | add_pending_template (tree d) |
5566b478 | 4196 | { |
3ae18eaf JM |
4197 | tree ti = (TYPE_P (d) |
4198 | ? CLASSTYPE_TEMPLATE_INFO (d) | |
4199 | : DECL_TEMPLATE_INFO (d)); | |
46ccf50a | 4200 | tree pt; |
3ae18eaf | 4201 | int level; |
e92cc029 | 4202 | |
824b9a4c | 4203 | if (TI_PENDING_TEMPLATE_FLAG (ti)) |
5566b478 MS |
4204 | return; |
4205 | ||
3ae18eaf JM |
4206 | /* We are called both from instantiate_decl, where we've already had a |
4207 | tinst_level pushed, and instantiate_template, where we haven't. | |
4208 | Compensate. */ | |
4209 | level = !(current_tinst_level && TINST_DECL (current_tinst_level) == d); | |
4210 | ||
4211 | if (level) | |
4212 | push_tinst_level (d); | |
4213 | ||
46ccf50a JM |
4214 | pt = tree_cons (current_tinst_level, d, NULL_TREE); |
4215 | if (last_pending_template) | |
4216 | TREE_CHAIN (last_pending_template) = pt; | |
4217 | else | |
4218 | pending_templates = pt; | |
4219 | ||
4220 | last_pending_template = pt; | |
4221 | ||
824b9a4c | 4222 | TI_PENDING_TEMPLATE_FLAG (ti) = 1; |
3ae18eaf JM |
4223 | |
4224 | if (level) | |
4225 | pop_tinst_level (); | |
5566b478 MS |
4226 | } |
4227 | ||
386b8a85 | 4228 | |
4ba126e4 MM |
4229 | /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and |
4230 | ARGLIST. Valid choices for FNS are given in the cp-tree.def | |
4231 | documentation for TEMPLATE_ID_EXPR. */ | |
386b8a85 JM |
4232 | |
4233 | tree | |
3a978d72 | 4234 | lookup_template_function (tree fns, tree arglist) |
386b8a85 | 4235 | { |
2c73f9f5 | 4236 | tree type; |
050367a3 | 4237 | |
4ba126e4 MM |
4238 | if (fns == error_mark_node || arglist == error_mark_node) |
4239 | return error_mark_node; | |
4240 | ||
50bc768d | 4241 | gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC); |
ceeae2d1 MM |
4242 | if (fns == NULL_TREE |
4243 | || TREE_CODE (fns) == FUNCTION_DECL) | |
386b8a85 | 4244 | { |
33bd39a2 | 4245 | error ("non-template used as template"); |
386b8a85 JM |
4246 | return error_mark_node; |
4247 | } | |
4248 | ||
50bc768d NS |
4249 | gcc_assert (TREE_CODE (fns) == TEMPLATE_DECL |
4250 | || TREE_CODE (fns) == OVERLOAD | |
4251 | || BASELINK_P (fns) | |
4252 | || TREE_CODE (fns) == IDENTIFIER_NODE); | |
4ba126e4 | 4253 | |
50ad9642 MM |
4254 | if (BASELINK_P (fns)) |
4255 | { | |
f293ce4b RS |
4256 | BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR, |
4257 | unknown_type_node, | |
4258 | BASELINK_FUNCTIONS (fns), | |
4259 | arglist); | |
50ad9642 MM |
4260 | return fns; |
4261 | } | |
4262 | ||
2c73f9f5 ML |
4263 | type = TREE_TYPE (fns); |
4264 | if (TREE_CODE (fns) == OVERLOAD || !type) | |
4265 | type = unknown_type_node; | |
4ba126e4 | 4266 | |
f293ce4b | 4267 | return build2 (TEMPLATE_ID_EXPR, type, fns, arglist); |
386b8a85 JM |
4268 | } |
4269 | ||
a2b60a0e MM |
4270 | /* Within the scope of a template class S<T>, the name S gets bound |
4271 | (in build_self_reference) to a TYPE_DECL for the class, not a | |
4272 | TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type, | |
4273 | or one of its enclosing classes, and that type is a template, | |
4274 | return the associated TEMPLATE_DECL. Otherwise, the original | |
4275 | DECL is returned. */ | |
4276 | ||
a723baf1 | 4277 | tree |
3a978d72 | 4278 | maybe_get_template_decl_from_type_decl (tree decl) |
a2b60a0e MM |
4279 | { |
4280 | return (decl != NULL_TREE | |
4281 | && TREE_CODE (decl) == TYPE_DECL | |
4282 | && DECL_ARTIFICIAL (decl) | |
511b60ff | 4283 | && CLASS_TYPE_P (TREE_TYPE (decl)) |
a2b60a0e MM |
4284 | && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl))) |
4285 | ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl; | |
4286 | } | |
386b8a85 | 4287 | |
8d08fdba MS |
4288 | /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of |
4289 | parameters, find the desired type. | |
4290 | ||
4291 | D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments. | |
8d08fdba MS |
4292 | |
4293 | IN_DECL, if non-NULL, is the template declaration we are trying to | |
75650646 MM |
4294 | instantiate. |
4295 | ||
838dfd8a | 4296 | If ENTERING_SCOPE is nonzero, we are about to enter the scope of |
36a117a5 | 4297 | the class we are looking up. |
f9c244b8 | 4298 | |
c2ea3a40 | 4299 | Issue error and warning messages under control of COMPLAIN. |
36a117a5 | 4300 | |
75650646 MM |
4301 | If the template class is really a local class in a template |
4302 | function, then the FUNCTION_CONTEXT is the function in which it is | |
b6ab6892 GB |
4303 | being instantiated. |
4304 | ||
4305 | ??? Note that this function is currently called *twice* for each | |
4306 | template-id: the first time from the parser, while creating the | |
4307 | incomplete type (finish_template_type), and the second type during the | |
4308 | real instantiation (instantiate_template_class). This is surely something | |
4309 | that we want to avoid. It also causes some problems with argument | |
4310 | coercion (see convert_nontype_argument for more information on this). */ | |
e92cc029 | 4311 | |
8d08fdba | 4312 | tree |
3a978d72 NN |
4313 | lookup_template_class (tree d1, |
4314 | tree arglist, | |
4315 | tree in_decl, | |
4316 | tree context, | |
4317 | int entering_scope, | |
4318 | tsubst_flags_t complain) | |
8d08fdba | 4319 | { |
a703fb38 | 4320 | tree template = NULL_TREE, parmlist; |
dbfe2124 | 4321 | tree t; |
42eaed49 | 4322 | |
fd295cb2 | 4323 | timevar_push (TV_NAME_LOOKUP); |
42eaed49 | 4324 | |
5566b478 MS |
4325 | if (TREE_CODE (d1) == IDENTIFIER_NODE) |
4326 | { | |
90ea9897 MM |
4327 | tree value = innermost_non_namespace_value (d1); |
4328 | if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value)) | |
4329 | template = value; | |
73b0fce8 KL |
4330 | else |
4331 | { | |
2c73f9f5 ML |
4332 | if (context) |
4333 | push_decl_namespace (context); | |
3ebc5c52 MM |
4334 | template = lookup_name (d1, /*prefer_type=*/0); |
4335 | template = maybe_get_template_decl_from_type_decl (template); | |
2c73f9f5 ML |
4336 | if (context) |
4337 | pop_decl_namespace (); | |
73b0fce8 | 4338 | } |
8d019cef JM |
4339 | if (template) |
4340 | context = DECL_CONTEXT (template); | |
5566b478 | 4341 | } |
c91a56d2 MS |
4342 | else if (TREE_CODE (d1) == TYPE_DECL && IS_AGGR_TYPE (TREE_TYPE (d1))) |
4343 | { | |
802dbc34 JM |
4344 | tree type = TREE_TYPE (d1); |
4345 | ||
4346 | /* If we are declaring a constructor, say A<T>::A<T>, we will get | |
4347 | an implicit typename for the second A. Deal with it. */ | |
4348 | if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type)) | |
4349 | type = TREE_TYPE (type); | |
4350 | ||
4351 | if (CLASSTYPE_TEMPLATE_INFO (type)) | |
f3400fe2 | 4352 | { |
802dbc34 | 4353 | template = CLASSTYPE_TI_TEMPLATE (type); |
f3400fe2 JM |
4354 | d1 = DECL_NAME (template); |
4355 | } | |
c91a56d2 | 4356 | } |
ed44da02 | 4357 | else if (TREE_CODE (d1) == ENUMERAL_TYPE |
2f939d94 | 4358 | || (TYPE_P (d1) && IS_AGGR_TYPE (d1))) |
5566b478 | 4359 | { |
ed44da02 | 4360 | template = TYPE_TI_TEMPLATE (d1); |
5566b478 MS |
4361 | d1 = DECL_NAME (template); |
4362 | } | |
93cdc044 | 4363 | else if (TREE_CODE (d1) == TEMPLATE_DECL |
17aec3eb | 4364 | && TREE_CODE (DECL_TEMPLATE_RESULT (d1)) == TYPE_DECL) |
93cdc044 JM |
4365 | { |
4366 | template = d1; | |
4367 | d1 = DECL_NAME (template); | |
4368 | context = DECL_CONTEXT (template); | |
4369 | } | |
8d08fdba | 4370 | |
90ea9897 | 4371 | /* Issue an error message if we didn't find a template. */ |
8d08fdba | 4372 | if (! template) |
f3400fe2 | 4373 | { |
c2ea3a40 | 4374 | if (complain & tf_error) |
0f51ccfc | 4375 | error ("%qT is not a template", d1); |
fd295cb2 | 4376 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node); |
f3400fe2 | 4377 | } |
2c73f9f5 | 4378 | |
a87b4257 | 4379 | if (TREE_CODE (template) != TEMPLATE_DECL |
42eaed49 NS |
4380 | /* Make sure it's a user visible template, if it was named by |
4381 | the user. */ | |
4382 | || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (template) | |
4383 | && !PRIMARY_TEMPLATE_P (template))) | |
8d08fdba | 4384 | { |
c2ea3a40 | 4385 | if (complain & tf_error) |
f9c244b8 | 4386 | { |
0f51ccfc | 4387 | error ("non-template type %qT used as a template", d1); |
f9c244b8 | 4388 | if (in_decl) |
0f51ccfc | 4389 | cp_error_at ("for template declaration %qD", in_decl); |
f9c244b8 | 4390 | } |
fd295cb2 | 4391 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node); |
8d08fdba | 4392 | } |
8d08fdba | 4393 | |
42eaed49 NS |
4394 | complain &= ~tf_user; |
4395 | ||
73b0fce8 KL |
4396 | if (DECL_TEMPLATE_TEMPLATE_PARM_P (template)) |
4397 | { | |
4398 | /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store | |
4399 | template arguments */ | |
4400 | ||
1899c3a4 | 4401 | tree parm; |
73b0fce8 KL |
4402 | tree arglist2; |
4403 | ||
73b0fce8 KL |
4404 | parmlist = DECL_INNERMOST_TEMPLATE_PARMS (template); |
4405 | ||
4f96ff63 KL |
4406 | /* Consider an example where a template template parameter declared as |
4407 | ||
4408 | template <class T, class U = std::allocator<T> > class TT | |
4409 | ||
4410 | The template parameter level of T and U are one level larger than | |
4411 | of TT. To proper process the default argument of U, say when an | |
4412 | instantiation `TT<int>' is seen, we need to build the full | |
342cea95 KL |
4413 | arguments containing {int} as the innermost level. Outer levels, |
4414 | available when not appearing as default template argument, can be | |
4415 | obtained from `current_template_args ()'. | |
4f96ff63 | 4416 | |
342cea95 KL |
4417 | Suppose that TT is later substituted with std::vector. The above |
4418 | instantiation is `TT<int, std::allocator<T> >' with TT at | |
4419 | level 1, and T at level 2, while the template arguments at level 1 | |
4420 | becomes {std::vector} and the inner level 2 is {int}. */ | |
4421 | ||
4422 | if (current_template_parms) | |
4f96ff63 KL |
4423 | arglist = add_to_template_args (current_template_args (), arglist); |
4424 | ||
f9c244b8 NS |
4425 | arglist2 = coerce_template_parms (parmlist, arglist, template, |
4426 | complain, /*require_all_args=*/1); | |
3e4a3562 | 4427 | if (arglist2 == error_mark_node |
544aef8c | 4428 | || (!uses_template_parms (arglist2) |
3e4a3562 | 4429 | && check_instantiated_args (template, arglist2, complain))) |
fd295cb2 | 4430 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node); |
73b0fce8 | 4431 | |
dac65501 | 4432 | parm = bind_template_template_parm (TREE_TYPE (template), arglist2); |
fd295cb2 | 4433 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, parm); |
73b0fce8 | 4434 | } |
36a117a5 | 4435 | else |
8d08fdba | 4436 | { |
36a117a5 | 4437 | tree template_type = TREE_TYPE (template); |
7ac7b28f | 4438 | tree gen_tmpl; |
36a117a5 MM |
4439 | tree type_decl; |
4440 | tree found = NULL_TREE; | |
4441 | int arg_depth; | |
4442 | int parm_depth; | |
dbfe2124 | 4443 | int is_partial_instantiation; |
830bfa74 | 4444 | |
7ac7b28f MM |
4445 | gen_tmpl = most_general_template (template); |
4446 | parmlist = DECL_TEMPLATE_PARMS (gen_tmpl); | |
36a117a5 MM |
4447 | parm_depth = TMPL_PARMS_DEPTH (parmlist); |
4448 | arg_depth = TMPL_ARGS_DEPTH (arglist); | |
4449 | ||
4450 | if (arg_depth == 1 && parm_depth > 1) | |
4451 | { | |
39c01e4c | 4452 | /* We've been given an incomplete set of template arguments. |
36a117a5 MM |
4453 | For example, given: |
4454 | ||
4455 | template <class T> struct S1 { | |
4456 | template <class U> struct S2 {}; | |
4457 | template <class U> struct S2<U*> {}; | |
4458 | }; | |
4459 | ||
4460 | we will be called with an ARGLIST of `U*', but the | |
4461 | TEMPLATE will be `template <class T> template | |
4462 | <class U> struct S1<T>::S2'. We must fill in the missing | |
4463 | arguments. */ | |
7ac7b28f MM |
4464 | arglist |
4465 | = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (template)), | |
4466 | arglist); | |
36a117a5 MM |
4467 | arg_depth = TMPL_ARGS_DEPTH (arglist); |
4468 | } | |
5566b478 | 4469 | |
41f5d4b1 | 4470 | /* Now we should have enough arguments. */ |
50bc768d | 4471 | gcc_assert (parm_depth == arg_depth); |
36a117a5 | 4472 | |
7ac7b28f MM |
4473 | /* From here on, we're only interested in the most general |
4474 | template. */ | |
4475 | template = gen_tmpl; | |
4476 | ||
36a117a5 MM |
4477 | /* Calculate the BOUND_ARGS. These will be the args that are |
4478 | actually tsubst'd into the definition to create the | |
4479 | instantiation. */ | |
4480 | if (parm_depth > 1) | |
830bfa74 MM |
4481 | { |
4482 | /* We have multiple levels of arguments to coerce, at once. */ | |
830bfa74 | 4483 | int i; |
e4a84209 | 4484 | int saved_depth = TMPL_ARGS_DEPTH (arglist); |
36a117a5 | 4485 | |
f31c0a32 | 4486 | tree bound_args = make_tree_vec (parm_depth); |
830bfa74 | 4487 | |
e4a84209 | 4488 | for (i = saved_depth, |
830bfa74 | 4489 | t = DECL_TEMPLATE_PARMS (template); |
e4a84209 | 4490 | i > 0 && t != NULL_TREE; |
830bfa74 | 4491 | --i, t = TREE_CHAIN (t)) |
e4a84209 | 4492 | { |
ee3071ef NS |
4493 | tree a = coerce_template_parms (TREE_VALUE (t), |
4494 | arglist, template, | |
4495 | complain, /*require_all_args=*/1); | |
88e98cfe KL |
4496 | |
4497 | /* Don't process further if one of the levels fails. */ | |
4498 | if (a == error_mark_node) | |
4499 | { | |
4500 | /* Restore the ARGLIST to its full size. */ | |
4501 | TREE_VEC_LENGTH (arglist) = saved_depth; | |
4502 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node); | |
4503 | } | |
4504 | ||
e4a84209 MM |
4505 | SET_TMPL_ARGS_LEVEL (bound_args, i, a); |
4506 | ||
4507 | /* We temporarily reduce the length of the ARGLIST so | |
4508 | that coerce_template_parms will see only the arguments | |
4509 | corresponding to the template parameters it is | |
4510 | examining. */ | |
4511 | TREE_VEC_LENGTH (arglist)--; | |
4512 | } | |
4513 | ||
4514 | /* Restore the ARGLIST to its full size. */ | |
4515 | TREE_VEC_LENGTH (arglist) = saved_depth; | |
4516 | ||
36a117a5 | 4517 | arglist = bound_args; |
830bfa74 MM |
4518 | } |
4519 | else | |
36a117a5 MM |
4520 | arglist |
4521 | = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist), | |
f9a7ae04 | 4522 | INNERMOST_TEMPLATE_ARGS (arglist), |
f9c244b8 NS |
4523 | template, |
4524 | complain, /*require_all_args=*/1); | |
36a117a5 | 4525 | |
c353b8e3 | 4526 | if (arglist == error_mark_node) |
36a117a5 | 4527 | /* We were unable to bind the arguments. */ |
fd295cb2 | 4528 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node); |
5566b478 | 4529 | |
36a117a5 MM |
4530 | /* In the scope of a template class, explicit references to the |
4531 | template class refer to the type of the template, not any | |
4532 | instantiation of it. For example, in: | |
4533 | ||
4534 | template <class T> class C { void f(C<T>); } | |
4535 | ||
4536 | the `C<T>' is just the same as `C'. Outside of the | |
4537 | class, however, such a reference is an instantiation. */ | |
ed44da02 | 4538 | if (comp_template_args (TYPE_TI_ARGS (template_type), |
36a117a5 MM |
4539 | arglist)) |
4540 | { | |
4541 | found = template_type; | |
4542 | ||
4543 | if (!entering_scope && PRIMARY_TEMPLATE_P (template)) | |
5566b478 | 4544 | { |
36a117a5 MM |
4545 | tree ctx; |
4546 | ||
36a117a5 | 4547 | for (ctx = current_class_type; |
5f04800c KL |
4548 | ctx && TREE_CODE (ctx) != NAMESPACE_DECL; |
4549 | ctx = (TYPE_P (ctx) | |
4550 | ? TYPE_CONTEXT (ctx) | |
4551 | : DECL_CONTEXT (ctx))) | |
4552 | if (TYPE_P (ctx) && same_type_p (ctx, template_type)) | |
4553 | goto found_ctx; | |
36a117a5 | 4554 | |
6df5158a NS |
4555 | /* We're not in the scope of the class, so the |
4556 | TEMPLATE_TYPE is not the type we want after all. */ | |
4557 | found = NULL_TREE; | |
4558 | found_ctx:; | |
5566b478 MS |
4559 | } |
4560 | } | |
36a117a5 | 4561 | if (found) |
fd295cb2 | 4562 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, found); |
414ea4aa | 4563 | |
c7222c02 MM |
4564 | /* If we already have this specialization, return it. */ |
4565 | found = retrieve_specialization (template, arglist, | |
4566 | /*class_specializations_p=*/false); | |
4567 | if (found) | |
4568 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, found); | |
5566b478 | 4569 | |
dbfe2124 | 4570 | /* This type is a "partial instantiation" if any of the template |
ab097535 | 4571 | arguments still involve template parameters. Note that we set |
486e4077 MM |
4572 | IS_PARTIAL_INSTANTIATION for partial specializations as |
4573 | well. */ | |
dbfe2124 MM |
4574 | is_partial_instantiation = uses_template_parms (arglist); |
4575 | ||
c353b8e3 MM |
4576 | /* If the deduced arguments are invalid, then the binding |
4577 | failed. */ | |
4578 | if (!is_partial_instantiation | |
4579 | && check_instantiated_args (template, | |
4580 | INNERMOST_TEMPLATE_ARGS (arglist), | |
4581 | complain)) | |
4582 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node); | |
4583 | ||
3ebc5c52 MM |
4584 | if (!is_partial_instantiation |
4585 | && !PRIMARY_TEMPLATE_P (template) | |
4586 | && TREE_CODE (CP_DECL_CONTEXT (template)) == NAMESPACE_DECL) | |
4587 | { | |
3ebc5c52 MM |
4588 | found = xref_tag_from_type (TREE_TYPE (template), |
4589 | DECL_NAME (template), | |
29ef83de | 4590 | /*tag_scope=*/ts_global); |
fd295cb2 | 4591 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, found); |
3ebc5c52 | 4592 | } |
3ebc5c52 | 4593 | |
95ee998c | 4594 | context = tsubst (DECL_CONTEXT (template), arglist, |
c2ea3a40 | 4595 | complain, in_decl); |
95ee998c MM |
4596 | if (!context) |
4597 | context = global_namespace; | |
4598 | ||
36a117a5 | 4599 | /* Create the type. */ |
ed44da02 MM |
4600 | if (TREE_CODE (template_type) == ENUMERAL_TYPE) |
4601 | { | |
dbfe2124 | 4602 | if (!is_partial_instantiation) |
92777ce4 NS |
4603 | { |
4604 | set_current_access_from_decl (TYPE_NAME (template_type)); | |
4605 | t = start_enum (TYPE_IDENTIFIER (template_type)); | |
4606 | } | |
ed44da02 | 4607 | else |
dbfe2124 | 4608 | /* We don't want to call start_enum for this type, since |
ed44da02 MM |
4609 | the values for the enumeration constants may involve |
4610 | template parameters. And, no one should be interested | |
4611 | in the enumeration constants for such a type. */ | |
4612 | t = make_node (ENUMERAL_TYPE); | |
4613 | } | |
4614 | else | |
4615 | { | |
33848bb0 | 4616 | t = make_aggr_type (TREE_CODE (template_type)); |
ed44da02 MM |
4617 | CLASSTYPE_DECLARED_CLASS (t) |
4618 | = CLASSTYPE_DECLARED_CLASS (template_type); | |
ed44da02 | 4619 | SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t); |
f668f160 | 4620 | TYPE_FOR_JAVA (t) = TYPE_FOR_JAVA (template_type); |
ae673f14 JM |
4621 | |
4622 | /* A local class. Make sure the decl gets registered properly. */ | |
4623 | if (context == current_function_decl) | |
4624 | pushtag (DECL_NAME (template), t, 0); | |
ed44da02 MM |
4625 | } |
4626 | ||
ae673f14 JM |
4627 | /* If we called start_enum or pushtag above, this information |
4628 | will already be set up. */ | |
ed44da02 MM |
4629 | if (!TYPE_NAME (t)) |
4630 | { | |
4631 | TYPE_CONTEXT (t) = FROB_CONTEXT (context); | |
36a117a5 | 4632 | |
9188c363 | 4633 | type_decl = create_implicit_typedef (DECL_NAME (template), t); |
ed44da02 | 4634 | DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t); |
9188c363 | 4635 | TYPE_STUB_DECL (t) = type_decl; |
f31686a3 RH |
4636 | DECL_SOURCE_LOCATION (type_decl) |
4637 | = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type)); | |
ed44da02 MM |
4638 | } |
4639 | else | |
4640 | type_decl = TYPE_NAME (t); | |
36a117a5 | 4641 | |
cab7a9a3 KL |
4642 | TREE_PRIVATE (type_decl) |
4643 | = TREE_PRIVATE (TYPE_STUB_DECL (template_type)); | |
4644 | TREE_PROTECTED (type_decl) | |
4645 | = TREE_PROTECTED (TYPE_STUB_DECL (template_type)); | |
4646 | ||
9fbf56f7 MM |
4647 | /* Set up the template information. We have to figure out which |
4648 | template is the immediate parent if this is a full | |
4649 | instantiation. */ | |
4650 | if (parm_depth == 1 || is_partial_instantiation | |
4651 | || !PRIMARY_TEMPLATE_P (template)) | |
4652 | /* This case is easy; there are no member templates involved. */ | |
4653 | found = template; | |
4654 | else | |
4655 | { | |
ab097535 NS |
4656 | /* This is a full instantiation of a member template. Look |
4657 | for a partial instantiation of which this is an instance. */ | |
9fbf56f7 MM |
4658 | |
4659 | for (found = DECL_TEMPLATE_INSTANTIATIONS (template); | |
4660 | found; found = TREE_CHAIN (found)) | |
4661 | { | |
4662 | int success; | |
4663 | tree tmpl = CLASSTYPE_TI_TEMPLATE (TREE_VALUE (found)); | |
4664 | ||
4665 | /* We only want partial instantiations, here, not | |
4666 | specializations or full instantiations. */ | |
370af2d5 | 4667 | if (CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_VALUE (found)) |
9fbf56f7 MM |
4668 | || !uses_template_parms (TREE_VALUE (found))) |
4669 | continue; | |
4670 | ||
4671 | /* Temporarily reduce by one the number of levels in the | |
4672 | ARGLIST and in FOUND so as to avoid comparing the | |
4673 | last set of arguments. */ | |
4674 | TREE_VEC_LENGTH (arglist)--; | |
4675 | TREE_VEC_LENGTH (TREE_PURPOSE (found)) --; | |
4676 | ||
4677 | /* See if the arguments match. If they do, then TMPL is | |
4678 | the partial instantiation we want. */ | |
4679 | success = comp_template_args (TREE_PURPOSE (found), arglist); | |
4680 | ||
4681 | /* Restore the argument vectors to their full size. */ | |
4682 | TREE_VEC_LENGTH (arglist)++; | |
4683 | TREE_VEC_LENGTH (TREE_PURPOSE (found))++; | |
4684 | ||
4685 | if (success) | |
4686 | { | |
4687 | found = tmpl; | |
4688 | break; | |
4689 | } | |
4690 | } | |
4691 | ||
4692 | if (!found) | |
ab097535 NS |
4693 | { |
4694 | /* There was no partial instantiation. This happens | |
4695 | where C<T> is a member template of A<T> and it's used | |
4696 | in something like | |
4697 | ||
4698 | template <typename T> struct B { A<T>::C<int> m; }; | |
4699 | B<float>; | |
4700 | ||
4701 | Create the partial instantiation. | |
4702 | */ | |
4703 | TREE_VEC_LENGTH (arglist)--; | |
c2ea3a40 | 4704 | found = tsubst (template, arglist, complain, NULL_TREE); |
ab097535 | 4705 | TREE_VEC_LENGTH (arglist)++; |
ab097535 | 4706 | } |
9fbf56f7 MM |
4707 | } |
4708 | ||
ab097535 | 4709 | SET_TYPE_TEMPLATE_INFO (t, tree_cons (found, arglist, NULL_TREE)); |
dbfe2124 MM |
4710 | DECL_TEMPLATE_INSTANTIATIONS (template) |
4711 | = tree_cons (arglist, t, | |
4712 | DECL_TEMPLATE_INSTANTIATIONS (template)); | |
4713 | ||
4714 | if (TREE_CODE (t) == ENUMERAL_TYPE | |
4715 | && !is_partial_instantiation) | |
61a127b3 MM |
4716 | /* Now that the type has been registered on the instantiations |
4717 | list, we set up the enumerators. Because the enumeration | |
4718 | constants may involve the enumeration type itself, we make | |
4719 | sure to register the type first, and then create the | |
4720 | constants. That way, doing tsubst_expr for the enumeration | |
4721 | constants won't result in recursive calls here; we'll find | |
4722 | the instantiation and exit above. */ | |
4723 | tsubst_enum (template_type, t, arglist); | |
dbfe2124 | 4724 | |
36a117a5 MM |
4725 | /* Reset the name of the type, now that CLASSTYPE_TEMPLATE_INFO |
4726 | is set up. */ | |
ed44da02 MM |
4727 | if (TREE_CODE (t) != ENUMERAL_TYPE) |
4728 | DECL_NAME (type_decl) = classtype_mangled_name (t); | |
7813d14c | 4729 | if (is_partial_instantiation) |
077e7015 MM |
4730 | /* If the type makes use of template parameters, the |
4731 | code that generates debugging information will crash. */ | |
4732 | DECL_IGNORED_P (TYPE_STUB_DECL (t)) = 1; | |
8d08fdba | 4733 | |
fd295cb2 | 4734 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t); |
36a117a5 | 4735 | } |
fd295cb2 | 4736 | timevar_pop (TV_NAME_LOOKUP); |
8d08fdba MS |
4737 | } |
4738 | \f | |
8dfaeb63 | 4739 | struct pair_fn_data |
8d08fdba | 4740 | { |
8dfaeb63 MM |
4741 | tree_fn_t fn; |
4742 | void *data; | |
0c58f841 | 4743 | struct pointer_set_t *visited; |
8dfaeb63 MM |
4744 | }; |
4745 | ||
4746 | /* Called from for_each_template_parm via walk_tree. */ | |
581d38d0 | 4747 | |
8dfaeb63 | 4748 | static tree |
350fae66 | 4749 | for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d) |
8dfaeb63 MM |
4750 | { |
4751 | tree t = *tp; | |
4752 | struct pair_fn_data *pfd = (struct pair_fn_data *) d; | |
4753 | tree_fn_t fn = pfd->fn; | |
4754 | void *data = pfd->data; | |
4f2c9d7e | 4755 | |
2f939d94 | 4756 | if (TYPE_P (t) |
4f2c9d7e | 4757 | && for_each_template_parm (TYPE_CONTEXT (t), fn, data, pfd->visited)) |
8dfaeb63 | 4758 | return error_mark_node; |
581d38d0 | 4759 | |
8d08fdba MS |
4760 | switch (TREE_CODE (t)) |
4761 | { | |
8d08fdba | 4762 | case RECORD_TYPE: |
9076e292 | 4763 | if (TYPE_PTRMEMFUNC_P (t)) |
8dfaeb63 | 4764 | break; |
ed44da02 MM |
4765 | /* Fall through. */ |
4766 | ||
8d08fdba | 4767 | case UNION_TYPE: |
ed44da02 | 4768 | case ENUMERAL_TYPE: |
8dfaeb63 MM |
4769 | if (!TYPE_TEMPLATE_INFO (t)) |
4770 | *walk_subtrees = 0; | |
4771 | else if (for_each_template_parm (TREE_VALUE (TYPE_TEMPLATE_INFO (t)), | |
4f2c9d7e | 4772 | fn, data, pfd->visited)) |
8dfaeb63 MM |
4773 | return error_mark_node; |
4774 | break; | |
4775 | ||
588c2d1c | 4776 | case METHOD_TYPE: |
8dfaeb63 MM |
4777 | /* Since we're not going to walk subtrees, we have to do this |
4778 | explicitly here. */ | |
4f2c9d7e MM |
4779 | if (for_each_template_parm (TYPE_METHOD_BASETYPE (t), fn, data, |
4780 | pfd->visited)) | |
8dfaeb63 | 4781 | return error_mark_node; |
4890c2f4 | 4782 | /* Fall through. */ |
588c2d1c MM |
4783 | |
4784 | case FUNCTION_TYPE: | |
8dfaeb63 | 4785 | /* Check the return type. */ |
4f2c9d7e | 4786 | if (for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited)) |
8dfaeb63 MM |
4787 | return error_mark_node; |
4788 | ||
588c2d1c MM |
4789 | /* Check the parameter types. Since default arguments are not |
4790 | instantiated until they are needed, the TYPE_ARG_TYPES may | |
4791 | contain expressions that involve template parameters. But, | |
4792 | no-one should be looking at them yet. And, once they're | |
4793 | instantiated, they don't contain template parameters, so | |
4794 | there's no point in looking at them then, either. */ | |
4795 | { | |
4796 | tree parm; | |
4797 | ||
4798 | for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm)) | |
4f2c9d7e MM |
4799 | if (for_each_template_parm (TREE_VALUE (parm), fn, data, |
4800 | pfd->visited)) | |
8dfaeb63 | 4801 | return error_mark_node; |
5566b478 | 4802 | |
8dfaeb63 MM |
4803 | /* Since we've already handled the TYPE_ARG_TYPES, we don't |
4804 | want walk_tree walking into them itself. */ | |
4805 | *walk_subtrees = 0; | |
4806 | } | |
4807 | break; | |
3ac3d9ea | 4808 | |
a723baf1 MM |
4809 | case TYPEOF_TYPE: |
4810 | if (for_each_template_parm (TYPE_FIELDS (t), fn, data, | |
4811 | pfd->visited)) | |
4812 | return error_mark_node; | |
4813 | break; | |
4814 | ||
8d08fdba | 4815 | case FUNCTION_DECL: |
5566b478 | 4816 | case VAR_DECL: |
5566b478 | 4817 | if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t) |
4f2c9d7e MM |
4818 | && for_each_template_parm (DECL_TI_ARGS (t), fn, data, |
4819 | pfd->visited)) | |
8dfaeb63 MM |
4820 | return error_mark_node; |
4821 | /* Fall through. */ | |
4822 | ||
8d08fdba | 4823 | case PARM_DECL: |
a723baf1 MM |
4824 | case CONST_DECL: |
4825 | if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t) | |
4826 | && for_each_template_parm (DECL_INITIAL (t), fn, data, | |
4827 | pfd->visited)) | |
4828 | return error_mark_node; | |
050367a3 | 4829 | if (DECL_CONTEXT (t) |
4f2c9d7e MM |
4830 | && for_each_template_parm (DECL_CONTEXT (t), fn, data, |
4831 | pfd->visited)) | |
8dfaeb63 MM |
4832 | return error_mark_node; |
4833 | break; | |
8d08fdba | 4834 | |
a1281f45 | 4835 | case BOUND_TEMPLATE_TEMPLATE_PARM: |
744fac59 | 4836 | /* Record template parameters such as `T' inside `TT<T>'. */ |
4f2c9d7e | 4837 | if (for_each_template_parm (TYPE_TI_ARGS (t), fn, data, pfd->visited)) |
8dfaeb63 MM |
4838 | return error_mark_node; |
4839 | /* Fall through. */ | |
4840 | ||
a1281f45 | 4841 | case TEMPLATE_TEMPLATE_PARM: |
744fac59 | 4842 | case TEMPLATE_TYPE_PARM: |
f84b4be9 | 4843 | case TEMPLATE_PARM_INDEX: |
8dfaeb63 MM |
4844 | if (fn && (*fn)(t, data)) |
4845 | return error_mark_node; | |
4846 | else if (!fn) | |
4847 | return error_mark_node; | |
4848 | break; | |
5156628f | 4849 | |
8dfaeb63 | 4850 | case TEMPLATE_DECL: |
f4f206f4 | 4851 | /* A template template parameter is encountered. */ |
8dfaeb63 | 4852 | if (DECL_TEMPLATE_TEMPLATE_PARM_P (t) |
4f2c9d7e | 4853 | && for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited)) |
8dfaeb63 | 4854 | return error_mark_node; |
db5ae43f | 4855 | |
8dfaeb63 MM |
4856 | /* Already substituted template template parameter */ |
4857 | *walk_subtrees = 0; | |
4858 | break; | |
b8865407 | 4859 | |
4699c561 | 4860 | case TYPENAME_TYPE: |
4f2c9d7e MM |
4861 | if (!fn |
4862 | || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t), fn, | |
4863 | data, pfd->visited)) | |
8dfaeb63 MM |
4864 | return error_mark_node; |
4865 | break; | |
4699c561 | 4866 | |
8dfaeb63 MM |
4867 | case CONSTRUCTOR: |
4868 | if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t)) | |
4869 | && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE | |
4f2c9d7e MM |
4870 | (TREE_TYPE (t)), fn, data, |
4871 | pfd->visited)) | |
8dfaeb63 MM |
4872 | return error_mark_node; |
4873 | break; | |
4874 | ||
b8865407 MM |
4875 | case INDIRECT_REF: |
4876 | case COMPONENT_REF: | |
4699c561 | 4877 | /* If there's no type, then this thing must be some expression |
b8865407 | 4878 | involving template parameters. */ |
4699c561 | 4879 | if (!fn && !TREE_TYPE (t)) |
8dfaeb63 MM |
4880 | return error_mark_node; |
4881 | break; | |
b8865407 | 4882 | |
42976354 BK |
4883 | case MODOP_EXPR: |
4884 | case CAST_EXPR: | |
4885 | case REINTERPRET_CAST_EXPR: | |
4886 | case CONST_CAST_EXPR: | |
4887 | case STATIC_CAST_EXPR: | |
4888 | case DYNAMIC_CAST_EXPR: | |
42976354 BK |
4889 | case ARROW_EXPR: |
4890 | case DOTSTAR_EXPR: | |
4891 | case TYPEID_EXPR: | |
40242ccf | 4892 | case PSEUDO_DTOR_EXPR: |
b8865407 | 4893 | if (!fn) |
8dfaeb63 MM |
4894 | return error_mark_node; |
4895 | break; | |
abff8e06 | 4896 | |
bd9bb3d2 MM |
4897 | case BASELINK: |
4898 | /* If we do not handle this case specially, we end up walking | |
4899 | the BINFO hierarchy, which is circular, and therefore | |
4900 | confuses walk_tree. */ | |
4901 | *walk_subtrees = 0; | |
4f2c9d7e MM |
4902 | if (for_each_template_parm (BASELINK_FUNCTIONS (*tp), fn, data, |
4903 | pfd->visited)) | |
bd9bb3d2 MM |
4904 | return error_mark_node; |
4905 | break; | |
4906 | ||
8d08fdba | 4907 | default: |
8dfaeb63 | 4908 | break; |
8d08fdba | 4909 | } |
8dfaeb63 MM |
4910 | |
4911 | /* We didn't find any template parameters we liked. */ | |
4912 | return NULL_TREE; | |
4913 | } | |
4914 | ||
a1281f45 KL |
4915 | /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM, |
4916 | BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T, | |
4917 | call FN with the parameter and the DATA. | |
838dfd8a | 4918 | If FN returns nonzero, the iteration is terminated, and |
8dfaeb63 | 4919 | for_each_template_parm returns 1. Otherwise, the iteration |
838dfd8a | 4920 | continues. If FN never returns a nonzero value, the value |
8dfaeb63 MM |
4921 | returned by for_each_template_parm is 0. If FN is NULL, it is |
4922 | considered to be the function which always returns 1. */ | |
4923 | ||
4924 | static int | |
0c58f841 MA |
4925 | for_each_template_parm (tree t, tree_fn_t fn, void* data, |
4926 | struct pointer_set_t *visited) | |
8dfaeb63 MM |
4927 | { |
4928 | struct pair_fn_data pfd; | |
ad2ae3b2 | 4929 | int result; |
8dfaeb63 MM |
4930 | |
4931 | /* Set up. */ | |
4932 | pfd.fn = fn; | |
4933 | pfd.data = data; | |
4934 | ||
4890c2f4 MM |
4935 | /* Walk the tree. (Conceptually, we would like to walk without |
4936 | duplicates, but for_each_template_parm_r recursively calls | |
4937 | for_each_template_parm, so we would need to reorganize a fair | |
4f2c9d7e MM |
4938 | bit to use walk_tree_without_duplicates, so we keep our own |
4939 | visited list.) */ | |
4940 | if (visited) | |
4941 | pfd.visited = visited; | |
4942 | else | |
0c58f841 | 4943 | pfd.visited = pointer_set_create (); |
ad2ae3b2 MM |
4944 | result = walk_tree (&t, |
4945 | for_each_template_parm_r, | |
4946 | &pfd, | |
c1e39976 | 4947 | pfd.visited) != NULL_TREE; |
ad2ae3b2 MM |
4948 | |
4949 | /* Clean up. */ | |
4950 | if (!visited) | |
0c58f841 MA |
4951 | { |
4952 | pointer_set_destroy (pfd.visited); | |
4953 | pfd.visited = 0; | |
4954 | } | |
ad2ae3b2 MM |
4955 | |
4956 | return result; | |
8d08fdba MS |
4957 | } |
4958 | ||
d43f603d KL |
4959 | /* Returns true if T depends on any template parameter. */ |
4960 | ||
050367a3 | 4961 | int |
3a978d72 | 4962 | uses_template_parms (tree t) |
050367a3 | 4963 | { |
c353b8e3 MM |
4964 | bool dependent_p; |
4965 | int saved_processing_template_decl; | |
4966 | ||
4967 | saved_processing_template_decl = processing_template_decl; | |
4968 | if (!saved_processing_template_decl) | |
4969 | processing_template_decl = 1; | |
4970 | if (TYPE_P (t)) | |
4971 | dependent_p = dependent_type_p (t); | |
4972 | else if (TREE_CODE (t) == TREE_VEC) | |
4973 | dependent_p = any_dependent_template_arguments_p (t); | |
4974 | else if (TREE_CODE (t) == TREE_LIST) | |
4975 | dependent_p = (uses_template_parms (TREE_VALUE (t)) | |
4976 | || uses_template_parms (TREE_CHAIN (t))); | |
4977 | else if (DECL_P (t) | |
4978 | || EXPR_P (t) | |
4979 | || TREE_CODE (t) == TEMPLATE_PARM_INDEX | |
4980 | || TREE_CODE (t) == OVERLOAD | |
4981 | || TREE_CODE (t) == BASELINK | |
6615c446 | 4982 | || CONSTANT_CLASS_P (t)) |
c353b8e3 MM |
4983 | dependent_p = (type_dependent_expression_p (t) |
4984 | || value_dependent_expression_p (t)); | |
315fb5db NS |
4985 | else |
4986 | { | |
4987 | gcc_assert (t == error_mark_node); | |
4988 | dependent_p = false; | |
4989 | } | |
4990 | ||
c353b8e3 MM |
4991 | processing_template_decl = saved_processing_template_decl; |
4992 | ||
4993 | return dependent_p; | |
050367a3 MM |
4994 | } |
4995 | ||
d43f603d KL |
4996 | /* Returns true if T depends on any template parameter with level LEVEL. */ |
4997 | ||
4998 | int | |
4999 | uses_template_parms_level (tree t, int level) | |
5000 | { | |
5001 | return for_each_template_parm (t, template_parm_this_level_p, &level, NULL); | |
5002 | } | |
5003 | ||
27fafc8d | 5004 | static int tinst_depth; |
e9f32eb5 | 5005 | extern int max_tinst_depth; |
5566b478 | 5006 | #ifdef GATHER_STATISTICS |
27fafc8d | 5007 | int depth_reached; |
5566b478 | 5008 | #endif |
8dfaeb63 MM |
5009 | static int tinst_level_tick; |
5010 | static int last_template_error_tick; | |
8d08fdba | 5011 | |
3ae18eaf JM |
5012 | /* We're starting to instantiate D; record the template instantiation context |
5013 | for diagnostics and to restore it later. */ | |
5014 | ||
742a37d5 | 5015 | int |
3a978d72 | 5016 | push_tinst_level (tree d) |
8d08fdba | 5017 | { |
3ae18eaf | 5018 | tree new; |
8d08fdba | 5019 | |
7215f9a0 MS |
5020 | if (tinst_depth >= max_tinst_depth) |
5021 | { | |
8adf5b5e JM |
5022 | /* If the instantiation in question still has unbound template parms, |
5023 | we don't really care if we can't instantiate it, so just return. | |
5024 | This happens with base instantiation for implicit `typename'. */ | |
5025 | if (uses_template_parms (d)) | |
5026 | return 0; | |
5027 | ||
1139b3d8 | 5028 | last_template_error_tick = tinst_level_tick; |
0f51ccfc GDR |
5029 | error ("template instantiation depth exceeds maximum of %d (use " |
5030 | "-ftemplate-depth-NN to increase the maximum) instantiating %qD", | |
b4f4233d | 5031 | max_tinst_depth, d); |
5566b478 | 5032 | |
cb753e49 | 5033 | print_instantiation_context (); |
5566b478 | 5034 | |
7215f9a0 MS |
5035 | return 0; |
5036 | } | |
5037 | ||
406d77a4 | 5038 | new = make_tinst_level (d, input_location); |
3ae18eaf | 5039 | TREE_CHAIN (new) = current_tinst_level; |
8d08fdba | 5040 | current_tinst_level = new; |
5566b478 | 5041 | |
7215f9a0 | 5042 | ++tinst_depth; |
5566b478 MS |
5043 | #ifdef GATHER_STATISTICS |
5044 | if (tinst_depth > depth_reached) | |
5045 | depth_reached = tinst_depth; | |
5046 | #endif | |
5047 | ||
27fafc8d | 5048 | ++tinst_level_tick; |
7215f9a0 | 5049 | return 1; |
8d08fdba MS |
5050 | } |
5051 | ||
3ae18eaf JM |
5052 | /* We're done instantiating this template; return to the instantiation |
5053 | context. */ | |
5054 | ||
8d08fdba | 5055 | void |
3a978d72 | 5056 | pop_tinst_level (void) |
8d08fdba | 5057 | { |
3ae18eaf | 5058 | tree old = current_tinst_level; |
8d08fdba | 5059 | |
ae58fa02 MM |
5060 | /* Restore the filename and line number stashed away when we started |
5061 | this instantiation. */ | |
406d77a4 | 5062 | input_location = TINST_LOCATION (old); |
3ae18eaf | 5063 | current_tinst_level = TREE_CHAIN (old); |
7215f9a0 | 5064 | --tinst_depth; |
27fafc8d | 5065 | ++tinst_level_tick; |
8d08fdba MS |
5066 | } |
5067 | ||
3ae18eaf JM |
5068 | /* We're instantiating a deferred template; restore the template |
5069 | instantiation context in which the instantiation was requested, which | |
5070 | is one step out from LEVEL. */ | |
5071 | ||
5072 | static void | |
3a978d72 | 5073 | reopen_tinst_level (tree level) |
3ae18eaf JM |
5074 | { |
5075 | tree t; | |
5076 | ||
5077 | tinst_depth = 0; | |
5078 | for (t = level; t; t = TREE_CHAIN (t)) | |
5079 | ++tinst_depth; | |
5080 | ||
5081 | current_tinst_level = level; | |
5082 | pop_tinst_level (); | |
5083 | } | |
5084 | ||
f84b4be9 JM |
5085 | /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the |
5086 | vector of template arguments, as for tsubst. | |
5087 | ||
dc957d14 | 5088 | Returns an appropriate tsubst'd friend declaration. */ |
f84b4be9 JM |
5089 | |
5090 | static tree | |
3a978d72 | 5091 | tsubst_friend_function (tree decl, tree args) |
f84b4be9 JM |
5092 | { |
5093 | tree new_friend; | |
27fafc8d | 5094 | |
f84b4be9 JM |
5095 | if (TREE_CODE (decl) == FUNCTION_DECL |
5096 | && DECL_TEMPLATE_INSTANTIATION (decl) | |
5097 | && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL) | |
5098 | /* This was a friend declared with an explicit template | |
5099 | argument list, e.g.: | |
5100 | ||
5101 | friend void f<>(T); | |
5102 | ||
5103 | to indicate that f was a template instantiation, not a new | |
5104 | function declaration. Now, we have to figure out what | |
5105 | instantiation of what template. */ | |
5106 | { | |
76e57b45 | 5107 | tree template_id, arglist, fns; |
f84b4be9 JM |
5108 | tree new_args; |
5109 | tree tmpl; | |
ed2fa432 | 5110 | tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type)); |
76e57b45 NS |
5111 | |
5112 | /* Friend functions are looked up in the containing namespace scope. | |
5113 | We must enter that scope, to avoid finding member functions of the | |
5114 | current cless with same name. */ | |
5115 | push_nested_namespace (ns); | |
5116 | fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args, | |
c2ea3a40 | 5117 | tf_error | tf_warning, NULL_TREE); |
76e57b45 NS |
5118 | pop_nested_namespace (ns); |
5119 | arglist = tsubst (DECL_TI_ARGS (decl), args, | |
c2ea3a40 | 5120 | tf_error | tf_warning, NULL_TREE); |
76e57b45 NS |
5121 | template_id = lookup_template_function (fns, arglist); |
5122 | ||
c2ea3a40 | 5123 | new_friend = tsubst (decl, args, tf_error | tf_warning, NULL_TREE); |
36a117a5 MM |
5124 | tmpl = determine_specialization (template_id, new_friend, |
5125 | &new_args, | |
5fe7b654 GB |
5126 | /*need_member_template=*/0, |
5127 | TREE_VEC_LENGTH (args)); | |
6e432b31 | 5128 | return instantiate_template (tmpl, new_args, tf_error); |
f84b4be9 | 5129 | } |
36a117a5 | 5130 | |
c2ea3a40 | 5131 | new_friend = tsubst (decl, args, tf_error | tf_warning, NULL_TREE); |
f84b4be9 | 5132 | |
36a117a5 | 5133 | /* The NEW_FRIEND will look like an instantiation, to the |
f84b4be9 JM |
5134 | compiler, but is not an instantiation from the point of view of |
5135 | the language. For example, we might have had: | |
5136 | ||
5137 | template <class T> struct S { | |
5138 | template <class U> friend void f(T, U); | |
5139 | }; | |
5140 | ||
5141 | Then, in S<int>, template <class U> void f(int, U) is not an | |
5142 | instantiation of anything. */ | |
ac2b3222 AP |
5143 | if (new_friend == error_mark_node) |
5144 | return error_mark_node; | |
5145 | ||
f84b4be9 JM |
5146 | DECL_USE_TEMPLATE (new_friend) = 0; |
5147 | if (TREE_CODE (decl) == TEMPLATE_DECL) | |
655dc6ee JM |
5148 | { |
5149 | DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0; | |
5150 | DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend)) | |
5151 | = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl)); | |
5152 | } | |
36a117a5 | 5153 | |
92643fea MM |
5154 | /* The mangled name for the NEW_FRIEND is incorrect. The function |
5155 | is not a template instantiation and should not be mangled like | |
5156 | one. Therefore, we forget the mangling here; we'll recompute it | |
5157 | later if we need it. */ | |
36a117a5 MM |
5158 | if (TREE_CODE (new_friend) != TEMPLATE_DECL) |
5159 | { | |
19e7881c | 5160 | SET_DECL_RTL (new_friend, NULL_RTX); |
92643fea | 5161 | SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE); |
36a117a5 MM |
5162 | } |
5163 | ||
6eb3bb27 | 5164 | if (DECL_NAMESPACE_SCOPE_P (new_friend)) |
f84b4be9 | 5165 | { |
36a117a5 | 5166 | tree old_decl; |
fbf1c34b MM |
5167 | tree new_friend_template_info; |
5168 | tree new_friend_result_template_info; | |
92da7074 | 5169 | tree ns; |
fbf1c34b MM |
5170 | int new_friend_is_defn; |
5171 | ||
5172 | /* We must save some information from NEW_FRIEND before calling | |
5173 | duplicate decls since that function will free NEW_FRIEND if | |
5174 | possible. */ | |
5175 | new_friend_template_info = DECL_TEMPLATE_INFO (new_friend); | |
5bd61841 MM |
5176 | new_friend_is_defn = |
5177 | (DECL_INITIAL (DECL_TEMPLATE_RESULT | |
5178 | (template_for_substitution (new_friend))) | |
5179 | != NULL_TREE); | |
f84b4be9 | 5180 | if (TREE_CODE (new_friend) == TEMPLATE_DECL) |
fbf1c34b MM |
5181 | { |
5182 | /* This declaration is a `primary' template. */ | |
5183 | DECL_PRIMARY_TEMPLATE (new_friend) = new_friend; | |
5184 | ||
fbf1c34b | 5185 | new_friend_result_template_info |
17aec3eb | 5186 | = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend)); |
fbf1c34b MM |
5187 | } |
5188 | else | |
5bd61841 | 5189 | new_friend_result_template_info = NULL_TREE; |
36a117a5 | 5190 | |
1c227897 MM |
5191 | /* Inside pushdecl_namespace_level, we will push into the |
5192 | current namespace. However, the friend function should go | |
c6002625 | 5193 | into the namespace of the template. */ |
92da7074 ML |
5194 | ns = decl_namespace_context (new_friend); |
5195 | push_nested_namespace (ns); | |
36a117a5 | 5196 | old_decl = pushdecl_namespace_level (new_friend); |
92da7074 | 5197 | pop_nested_namespace (ns); |
36a117a5 MM |
5198 | |
5199 | if (old_decl != new_friend) | |
5200 | { | |
5201 | /* This new friend declaration matched an existing | |
5202 | declaration. For example, given: | |
5203 | ||
5204 | template <class T> void f(T); | |
5205 | template <class U> class C { | |
5206 | template <class T> friend void f(T) {} | |
5207 | }; | |
5208 | ||
5209 | the friend declaration actually provides the definition | |
5210 | of `f', once C has been instantiated for some type. So, | |
5211 | old_decl will be the out-of-class template declaration, | |
5212 | while new_friend is the in-class definition. | |
5213 | ||
5214 | But, if `f' was called before this point, the | |
5215 | instantiation of `f' will have DECL_TI_ARGS corresponding | |
5216 | to `T' but not to `U', references to which might appear | |
5217 | in the definition of `f'. Previously, the most general | |
5218 | template for an instantiation of `f' was the out-of-class | |
5219 | version; now it is the in-class version. Therefore, we | |
5220 | run through all specialization of `f', adding to their | |
5221 | DECL_TI_ARGS appropriately. In particular, they need a | |
5222 | new set of outer arguments, corresponding to the | |
5223 | arguments for this class instantiation. | |
5224 | ||
5225 | The same situation can arise with something like this: | |
5226 | ||
5227 | friend void f(int); | |
5228 | template <class T> class C { | |
5229 | friend void f(T) {} | |
5230 | }; | |
5231 | ||
5232 | when `C<int>' is instantiated. Now, `f(int)' is defined | |
5233 | in the class. */ | |
5234 | ||
fbf1c34b MM |
5235 | if (!new_friend_is_defn) |
5236 | /* On the other hand, if the in-class declaration does | |
5237 | *not* provide a definition, then we don't want to alter | |
5238 | existing definitions. We can just leave everything | |
5239 | alone. */ | |
36a117a5 | 5240 | ; |
fbf1c34b | 5241 | else |
36a117a5 | 5242 | { |
fbf1c34b MM |
5243 | /* Overwrite whatever template info was there before, if |
5244 | any, with the new template information pertaining to | |
5245 | the declaration. */ | |
5246 | DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info; | |
5247 | ||
5248 | if (TREE_CODE (old_decl) != TEMPLATE_DECL) | |
8d83f792 MM |
5249 | reregister_specialization (new_friend, |
5250 | most_general_template (old_decl), | |
5251 | old_decl); | |
fbf1c34b | 5252 | else |
36a117a5 | 5253 | { |
fbf1c34b MM |
5254 | tree t; |
5255 | tree new_friend_args; | |
5256 | ||
17aec3eb | 5257 | DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl)) |
fbf1c34b MM |
5258 | = new_friend_result_template_info; |
5259 | ||
5260 | new_friend_args = TI_ARGS (new_friend_template_info); | |
5261 | for (t = DECL_TEMPLATE_SPECIALIZATIONS (old_decl); | |
5262 | t != NULL_TREE; | |
5263 | t = TREE_CHAIN (t)) | |
5264 | { | |
5265 | tree spec = TREE_VALUE (t); | |
36a117a5 | 5266 | |
fbf1c34b MM |
5267 | DECL_TI_ARGS (spec) |
5268 | = add_outermost_template_args (new_friend_args, | |
5269 | DECL_TI_ARGS (spec)); | |
fbf1c34b MM |
5270 | } |
5271 | ||
5272 | /* Now, since specializations are always supposed to | |
5273 | hang off of the most general template, we must move | |
5274 | them. */ | |
5275 | t = most_general_template (old_decl); | |
5276 | if (t != old_decl) | |
5277 | { | |
5278 | DECL_TEMPLATE_SPECIALIZATIONS (t) | |
5279 | = chainon (DECL_TEMPLATE_SPECIALIZATIONS (t), | |
5280 | DECL_TEMPLATE_SPECIALIZATIONS (old_decl)); | |
5281 | DECL_TEMPLATE_SPECIALIZATIONS (old_decl) = NULL_TREE; | |
5282 | } | |
36a117a5 MM |
5283 | } |
5284 | } | |
5285 | ||
5286 | /* The information from NEW_FRIEND has been merged into OLD_DECL | |
5287 | by duplicate_decls. */ | |
5288 | new_friend = old_decl; | |
5289 | } | |
f84b4be9 | 5290 | } |
6e432b31 | 5291 | else |
f84b4be9 | 5292 | { |
6e432b31 KL |
5293 | tree context = DECL_CONTEXT (new_friend); |
5294 | bool dependent_p; | |
5295 | ||
5296 | /* In the code | |
5297 | template <class T> class C { | |
5298 | template <class U> friend void C1<U>::f (); // case 1 | |
5299 | friend void C2<T>::f (); // case 2 | |
5300 | }; | |
5301 | we only need to make sure CONTEXT is a complete type for | |
5302 | case 2. To distinguish between the two cases, we note that | |
5303 | CONTEXT of case 1 remains dependent type after tsubst while | |
5304 | this isn't true for case 2. */ | |
5305 | ++processing_template_decl; | |
5306 | dependent_p = dependent_type_p (context); | |
5307 | --processing_template_decl; | |
5308 | ||
5309 | if (!dependent_p | |
5310 | && !complete_type_or_else (context, NULL_TREE)) | |
5311 | return error_mark_node; | |
5312 | ||
5313 | if (COMPLETE_TYPE_P (context)) | |
5314 | { | |
5315 | /* Check to see that the declaration is really present, and, | |
5316 | possibly obtain an improved declaration. */ | |
5317 | tree fn = check_classfn (context, | |
5318 | new_friend, NULL_TREE); | |
5319 | ||
5320 | if (fn) | |
5321 | new_friend = fn; | |
5322 | } | |
f84b4be9 JM |
5323 | } |
5324 | ||
5325 | return new_friend; | |
5326 | } | |
5327 | ||
1aed5355 MM |
5328 | /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of |
5329 | template arguments, as for tsubst. | |
6757edfe | 5330 | |
dc957d14 | 5331 | Returns an appropriate tsubst'd friend type or error_mark_node on |
4b054b80 | 5332 | failure. */ |
6757edfe MM |
5333 | |
5334 | static tree | |
3a978d72 | 5335 | tsubst_friend_class (tree friend_tmpl, tree args) |
6757edfe | 5336 | { |
1aed5355 | 5337 | tree friend_type; |
25aab5d0 | 5338 | tree tmpl; |
3e0ec82f | 5339 | tree context; |
6757edfe | 5340 | |
3e0ec82f MM |
5341 | context = DECL_CONTEXT (friend_tmpl); |
5342 | ||
5343 | if (context) | |
77adef84 | 5344 | { |
3e0ec82f MM |
5345 | if (TREE_CODE (context) == NAMESPACE_DECL) |
5346 | push_nested_namespace (context); | |
5347 | else | |
14d22dd6 | 5348 | push_nested_class (tsubst (context, args, tf_none, NULL_TREE)); |
3e0ec82f | 5349 | } |
25aab5d0 | 5350 | |
3e0ec82f MM |
5351 | /* First, we look for a class template. */ |
5352 | tmpl = lookup_name (DECL_NAME (friend_tmpl), /*prefer_type=*/0); | |
25aab5d0 | 5353 | |
3e0ec82f MM |
5354 | /* But, if we don't find one, it might be because we're in a |
5355 | situation like this: | |
77adef84 | 5356 | |
3e0ec82f MM |
5357 | template <class T> |
5358 | struct S { | |
5359 | template <class U> | |
5360 | friend struct S; | |
5361 | }; | |
5362 | ||
5363 | Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL | |
5364 | for `S<int>', not the TEMPLATE_DECL. */ | |
5365 | if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl)) | |
5366 | { | |
5367 | tmpl = lookup_name (DECL_NAME (friend_tmpl), /*prefer_type=*/1); | |
5368 | tmpl = maybe_get_template_decl_from_type_decl (tmpl); | |
25aab5d0 | 5369 | } |
6757edfe | 5370 | |
25aab5d0 | 5371 | if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl)) |
6757edfe MM |
5372 | { |
5373 | /* The friend template has already been declared. Just | |
36a117a5 MM |
5374 | check to see that the declarations match, and install any new |
5375 | default parameters. We must tsubst the default parameters, | |
5376 | of course. We only need the innermost template parameters | |
5377 | because that is all that redeclare_class_template will look | |
5378 | at. */ | |
3e0ec82f MM |
5379 | if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl)) |
5380 | > TMPL_ARGS_DEPTH (args)) | |
5381 | { | |
5382 | tree parms; | |
5383 | parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl), | |
5384 | args, tf_error | tf_warning); | |
5385 | redeclare_class_template (TREE_TYPE (tmpl), parms); | |
5386 | } | |
5387 | ||
6757edfe MM |
5388 | friend_type = TREE_TYPE (tmpl); |
5389 | } | |
5390 | else | |
5391 | { | |
5392 | /* The friend template has not already been declared. In this | |
5393 | case, the instantiation of the template class will cause the | |
5394 | injection of this template into the global scope. */ | |
c2ea3a40 | 5395 | tmpl = tsubst (friend_tmpl, args, tf_error | tf_warning, NULL_TREE); |
6757edfe MM |
5396 | |
5397 | /* The new TMPL is not an instantiation of anything, so we | |
5398 | forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for | |
5399 | the new type because that is supposed to be the corresponding | |
5400 | template decl, i.e., TMPL. */ | |
5401 | DECL_USE_TEMPLATE (tmpl) = 0; | |
5402 | DECL_TEMPLATE_INFO (tmpl) = NULL_TREE; | |
5403 | CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0; | |
68c78847 KL |
5404 | CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)) |
5405 | = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))); | |
6757edfe MM |
5406 | |
5407 | /* Inject this template into the global scope. */ | |
5408 | friend_type = TREE_TYPE (pushdecl_top_level (tmpl)); | |
5409 | } | |
5410 | ||
3e0ec82f MM |
5411 | if (context) |
5412 | { | |
5413 | if (TREE_CODE (context) == NAMESPACE_DECL) | |
5414 | pop_nested_namespace (context); | |
5415 | else | |
5416 | pop_nested_class (); | |
5417 | } | |
5418 | ||
6757edfe MM |
5419 | return friend_type; |
5420 | } | |
f84b4be9 | 5421 | |
17f29637 KL |
5422 | /* Returns zero if TYPE cannot be completed later due to circularity. |
5423 | Otherwise returns one. */ | |
5424 | ||
d5372501 | 5425 | static int |
3a978d72 | 5426 | can_complete_type_without_circularity (tree type) |
17f29637 KL |
5427 | { |
5428 | if (type == NULL_TREE || type == error_mark_node) | |
5429 | return 0; | |
5430 | else if (COMPLETE_TYPE_P (type)) | |
5431 | return 1; | |
5432 | else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type)) | |
5433 | return can_complete_type_without_circularity (TREE_TYPE (type)); | |
8c6ab2db NS |
5434 | else if (CLASS_TYPE_P (type) |
5435 | && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type))) | |
17f29637 KL |
5436 | return 0; |
5437 | else | |
5438 | return 1; | |
5439 | } | |
5440 | ||
8d08fdba | 5441 | tree |
3a978d72 | 5442 | instantiate_class_template (tree type) |
8d08fdba | 5443 | { |
7088fca9 | 5444 | tree template, args, pattern, t, member; |
36a117a5 | 5445 | tree typedecl; |
dbbf88d1 | 5446 | tree pbinfo; |
cad7e87b | 5447 | tree base_list; |
dbbf88d1 | 5448 | |
5566b478 | 5449 | if (type == error_mark_node) |
8d08fdba MS |
5450 | return error_mark_node; |
5451 | ||
ca099ac8 MM |
5452 | if (TYPE_BEING_DEFINED (type) |
5453 | || COMPLETE_TYPE_P (type) | |
5454 | || dependent_type_p (type)) | |
5566b478 MS |
5455 | return type; |
5456 | ||
6bdb985f | 5457 | /* Figure out which template is being instantiated. */ |
36a117a5 | 5458 | template = most_general_template (CLASSTYPE_TI_TEMPLATE (type)); |
50bc768d | 5459 | gcc_assert (TREE_CODE (template) == TEMPLATE_DECL); |
73aad9b9 | 5460 | |
6bdb985f MM |
5461 | /* Figure out which arguments are being used to do the |
5462 | instantiation. */ | |
5463 | args = CLASSTYPE_TI_ARGS (type); | |
4c571114 MM |
5464 | |
5465 | /* Determine what specialization of the original template to | |
5466 | instantiate. */ | |
8fbc5ae7 MM |
5467 | t = most_specialized_class (template, args); |
5468 | if (t == error_mark_node) | |
73aad9b9 | 5469 | { |
8fbc5ae7 | 5470 | const char *str = "candidates are:"; |
0f51ccfc | 5471 | error ("ambiguous class template instantiation for %q#T", type); |
8fbc5ae7 MM |
5472 | for (t = DECL_TEMPLATE_SPECIALIZATIONS (template); t; |
5473 | t = TREE_CHAIN (t)) | |
73aad9b9 | 5474 | { |
8c6ab2db | 5475 | if (get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t), args)) |
73aad9b9 | 5476 | { |
8fbc5ae7 MM |
5477 | cp_error_at ("%s %+#T", str, TREE_TYPE (t)); |
5478 | str = " "; | |
73aad9b9 JM |
5479 | } |
5480 | } | |
8fbc5ae7 MM |
5481 | TYPE_BEING_DEFINED (type) = 1; |
5482 | return error_mark_node; | |
73aad9b9 | 5483 | } |
6bdb985f MM |
5484 | |
5485 | if (t) | |
73aad9b9 JM |
5486 | pattern = TREE_TYPE (t); |
5487 | else | |
5488 | pattern = TREE_TYPE (template); | |
5566b478 | 5489 | |
4c571114 MM |
5490 | /* If the template we're instantiating is incomplete, then clearly |
5491 | there's nothing we can do. */ | |
d0f062fb | 5492 | if (!COMPLETE_TYPE_P (pattern)) |
f31c0a32 | 5493 | return type; |
5566b478 | 5494 | |
4c571114 MM |
5495 | /* If we've recursively instantiated too many templates, stop. */ |
5496 | if (! push_tinst_level (type)) | |
f31c0a32 | 5497 | return type; |
4c571114 MM |
5498 | |
5499 | /* Now we're really doing the instantiation. Mark the type as in | |
5500 | the process of being defined. */ | |
5501 | TYPE_BEING_DEFINED (type) = 1; | |
5502 | ||
78757caa KL |
5503 | /* We may be in the middle of deferred access check. Disable |
5504 | it now. */ | |
5505 | push_deferring_access_checks (dk_no_deferred); | |
5506 | ||
c353b8e3 | 5507 | push_to_top_level (); |
4c571114 | 5508 | |
73aad9b9 | 5509 | if (t) |
36a117a5 | 5510 | { |
27631dae | 5511 | /* This TYPE is actually an instantiation of a partial |
36a117a5 MM |
5512 | specialization. We replace the innermost set of ARGS with |
5513 | the arguments appropriate for substitution. For example, | |
5514 | given: | |
5515 | ||
5516 | template <class T> struct S {}; | |
5517 | template <class T> struct S<T*> {}; | |
5518 | ||
5519 | and supposing that we are instantiating S<int*>, ARGS will | |
5520 | present be {int*} but we need {int}. */ | |
5521 | tree inner_args | |
5522 | = get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t), | |
5523 | args); | |
5524 | ||
5525 | /* If there were multiple levels in ARGS, replacing the | |
5526 | innermost level would alter CLASSTYPE_TI_ARGS, which we don't | |
5527 | want, so we make a copy first. */ | |
5528 | if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args)) | |
5529 | { | |
5530 | args = copy_node (args); | |
5531 | SET_TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args), inner_args); | |
5532 | } | |
5533 | else | |
5534 | args = inner_args; | |
5535 | } | |
8d019cef | 5536 | |
7813d14c | 5537 | SET_CLASSTYPE_INTERFACE_UNKNOWN (type); |
8d08fdba | 5538 | |
68ea098a NS |
5539 | /* Set the input location to the template definition. This is needed |
5540 | if tsubsting causes an error. */ | |
f31686a3 | 5541 | input_location = DECL_SOURCE_LOCATION (TYPE_NAME (pattern)); |
68ea098a | 5542 | |
f7da6097 MS |
5543 | TYPE_HAS_CONSTRUCTOR (type) = TYPE_HAS_CONSTRUCTOR (pattern); |
5544 | TYPE_HAS_DESTRUCTOR (type) = TYPE_HAS_DESTRUCTOR (pattern); | |
834c6dff MM |
5545 | TYPE_HAS_NEW_OPERATOR (type) = TYPE_HAS_NEW_OPERATOR (pattern); |
5546 | TYPE_HAS_ARRAY_NEW_OPERATOR (type) = TYPE_HAS_ARRAY_NEW_OPERATOR (pattern); | |
f7da6097 | 5547 | TYPE_GETS_DELETE (type) = TYPE_GETS_DELETE (pattern); |
f7da6097 MS |
5548 | TYPE_HAS_ASSIGN_REF (type) = TYPE_HAS_ASSIGN_REF (pattern); |
5549 | TYPE_HAS_CONST_ASSIGN_REF (type) = TYPE_HAS_CONST_ASSIGN_REF (pattern); | |
f7da6097 MS |
5550 | TYPE_HAS_INIT_REF (type) = TYPE_HAS_INIT_REF (pattern); |
5551 | TYPE_HAS_CONST_INIT_REF (type) = TYPE_HAS_CONST_INIT_REF (pattern); | |
f7da6097 MS |
5552 | TYPE_HAS_DEFAULT_CONSTRUCTOR (type) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern); |
5553 | TYPE_HAS_CONVERSION (type) = TYPE_HAS_CONVERSION (pattern); | |
f7da6097 MS |
5554 | TYPE_PACKED (type) = TYPE_PACKED (pattern); |
5555 | TYPE_ALIGN (type) = TYPE_ALIGN (pattern); | |
11cf4d18 | 5556 | TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern); |
eff71ab0 | 5557 | TYPE_FOR_JAVA (type) = TYPE_FOR_JAVA (pattern); /* For libjava's JArray<T> */ |
6bdb8141 JM |
5558 | if (ANON_AGGR_TYPE_P (pattern)) |
5559 | SET_ANON_AGGR_TYPE_P (type); | |
f7da6097 | 5560 | |
dbbf88d1 | 5561 | pbinfo = TYPE_BINFO (pattern); |
1456deaf | 5562 | |
315fb5db NS |
5563 | /* We should never instantiate a nested class before its enclosing |
5564 | class; we need to look up the nested class by name before we can | |
5565 | instantiate it, and that lookup should instantiate the enclosing | |
5566 | class. */ | |
5567 | gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern)) | |
5568 | || COMPLETE_TYPE_P (TYPE_CONTEXT (type)) | |
5569 | || TYPE_BEING_DEFINED (TYPE_CONTEXT (type))); | |
1456deaf | 5570 | |
cad7e87b | 5571 | base_list = NULL_TREE; |
fa743e8c | 5572 | if (BINFO_N_BASE_BINFOS (pbinfo)) |
3fd71a52 | 5573 | { |
fa743e8c | 5574 | tree pbase_binfo; |
a2507277 | 5575 | tree context = TYPE_CONTEXT (type); |
4514aa8c | 5576 | tree pushed_scope; |
3fd71a52 | 5577 | int i; |
5566b478 | 5578 | |
a2507277 NS |
5579 | /* We must enter the scope containing the type, as that is where |
5580 | the accessibility of types named in dependent bases are | |
5581 | looked up from. */ | |
4514aa8c | 5582 | pushed_scope = push_scope (context ? context : global_namespace); |
a2507277 | 5583 | |
3fd71a52 MM |
5584 | /* Substitute into each of the bases to determine the actual |
5585 | basetypes. */ | |
fa743e8c | 5586 | for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++) |
3fd71a52 MM |
5587 | { |
5588 | tree base; | |
fa743e8c | 5589 | tree access = BINFO_BASE_ACCESS (pbinfo, i); |
711734a9 | 5590 | |
dc957d14 | 5591 | /* Substitute to figure out the base class. */ |
fa743e8c | 5592 | base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error, NULL_TREE); |
3fd71a52 MM |
5593 | if (base == error_mark_node) |
5594 | continue; | |
dbbf88d1 | 5595 | |
3fd71a52 | 5596 | base_list = tree_cons (access, base, base_list); |
fa743e8c | 5597 | if (BINFO_VIRTUAL_P (pbase_binfo)) |
809e3e7f | 5598 | TREE_TYPE (base_list) = integer_type_node; |
3fd71a52 | 5599 | } |
dfbcd65a | 5600 | |
3fd71a52 MM |
5601 | /* The list is now in reverse order; correct that. */ |
5602 | base_list = nreverse (base_list); | |
5603 | ||
4514aa8c NS |
5604 | if (pushed_scope) |
5605 | pop_scope (pushed_scope); | |
3fd71a52 | 5606 | } |
cad7e87b NS |
5607 | /* Now call xref_basetypes to set up all the base-class |
5608 | information. */ | |
5609 | xref_basetypes (type, base_list); | |
5610 | ||
5566b478 | 5611 | |
b74a0560 MM |
5612 | /* Now that our base classes are set up, enter the scope of the |
5613 | class, so that name lookups into base classes, etc. will work | |
dc957d14 | 5614 | correctly. This is precisely analogous to what we do in |
b74a0560 MM |
5615 | begin_class_definition when defining an ordinary non-template |
5616 | class. */ | |
29370796 | 5617 | pushclass (type); |
b74a0560 | 5618 | |
7088fca9 | 5619 | /* Now members are processed in the order of declaration. */ |
8c6ab2db NS |
5620 | for (member = CLASSTYPE_DECL_LIST (pattern); |
5621 | member; member = TREE_CHAIN (member)) | |
8d08fdba | 5622 | { |
7088fca9 | 5623 | tree t = TREE_VALUE (member); |
5566b478 | 5624 | |
7088fca9 | 5625 | if (TREE_PURPOSE (member)) |
ed44da02 | 5626 | { |
7088fca9 KL |
5627 | if (TYPE_P (t)) |
5628 | { | |
5e0c54e5 | 5629 | /* Build new CLASSTYPE_NESTED_UTDS. */ |
8d08fdba | 5630 | |
7088fca9 KL |
5631 | tree tag = t; |
5632 | tree name = TYPE_IDENTIFIER (tag); | |
5633 | tree newtag; | |
883a2bff MM |
5634 | bool class_template_p; |
5635 | ||
5636 | class_template_p = (TREE_CODE (tag) != ENUMERAL_TYPE | |
5637 | && TYPE_LANG_SPECIFIC (tag) | |
5638 | && CLASSTYPE_IS_TEMPLATE (tag)); | |
5639 | /* If the member is a class template, then -- even after | |
6c745393 | 5640 | substitution -- there may be dependent types in the |
883a2bff MM |
5641 | template argument list for the class. We increment |
5642 | PROCESSING_TEMPLATE_DECL so that dependent_type_p, as | |
5643 | that function will assume that no types are dependent | |
5644 | when outside of a template. */ | |
5645 | if (class_template_p) | |
5646 | ++processing_template_decl; | |
7088fca9 | 5647 | newtag = tsubst (tag, args, tf_error, NULL_TREE); |
883a2bff MM |
5648 | if (class_template_p) |
5649 | --processing_template_decl; | |
2620d095 KL |
5650 | if (newtag == error_mark_node) |
5651 | continue; | |
5652 | ||
7088fca9 KL |
5653 | if (TREE_CODE (newtag) != ENUMERAL_TYPE) |
5654 | { | |
883a2bff | 5655 | if (class_template_p) |
7088fca9 KL |
5656 | /* Unfortunately, lookup_template_class sets |
5657 | CLASSTYPE_IMPLICIT_INSTANTIATION for a partial | |
3afd2e20 NS |
5658 | instantiation (i.e., for the type of a member |
5659 | template class nested within a template class.) | |
5660 | This behavior is required for | |
5661 | maybe_process_partial_specialization to work | |
5662 | correctly, but is not accurate in this case; | |
5663 | the TAG is not an instantiation of anything. | |
5664 | (The corresponding TEMPLATE_DECL is an | |
5665 | instantiation, but the TYPE is not.) */ | |
7088fca9 KL |
5666 | CLASSTYPE_USE_TEMPLATE (newtag) = 0; |
5667 | ||
5668 | /* Now, we call pushtag to put this NEWTAG into the scope of | |
5669 | TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid | |
5670 | pushtag calling push_template_decl. We don't have to do | |
5671 | this for enums because it will already have been done in | |
5672 | tsubst_enum. */ | |
5673 | if (name) | |
5674 | SET_IDENTIFIER_TYPE_VALUE (name, newtag); | |
5675 | pushtag (name, newtag, /*globalize=*/0); | |
5676 | } | |
5677 | } | |
5678 | else if (TREE_CODE (t) == FUNCTION_DECL | |
5679 | || DECL_FUNCTION_TEMPLATE_P (t)) | |
5680 | { | |
5681 | /* Build new TYPE_METHODS. */ | |
fecafe5e NS |
5682 | tree r; |
5683 | ||
5684 | if (TREE_CODE (t) == TEMPLATE_DECL) | |
9579624e | 5685 | ++processing_template_decl; |
fecafe5e NS |
5686 | r = tsubst (t, args, tf_error, NULL_TREE); |
5687 | if (TREE_CODE (t) == TEMPLATE_DECL) | |
9579624e | 5688 | --processing_template_decl; |
7088fca9 KL |
5689 | set_current_access_from_decl (r); |
5690 | grok_special_member_properties (r); | |
5691 | finish_member_declaration (r); | |
5692 | } | |
5693 | else | |
5694 | { | |
5695 | /* Build new TYPE_FIELDS. */ | |
fa8d6e85 | 5696 | |
7088fca9 KL |
5697 | if (TREE_CODE (t) != CONST_DECL) |
5698 | { | |
5699 | tree r; | |
fa8d6e85 | 5700 | |
d479d37f NS |
5701 | /* The the file and line for this declaration, to |
5702 | assist in error message reporting. Since we | |
5703 | called push_tinst_level above, we don't need to | |
5704 | restore these. */ | |
f31686a3 | 5705 | input_location = DECL_SOURCE_LOCATION (t); |
fa8d6e85 | 5706 | |
fb5ce3c9 | 5707 | if (TREE_CODE (t) == TEMPLATE_DECL) |
9579624e | 5708 | ++processing_template_decl; |
7088fca9 | 5709 | r = tsubst (t, args, tf_error | tf_warning, NULL_TREE); |
fb5ce3c9 | 5710 | if (TREE_CODE (t) == TEMPLATE_DECL) |
9579624e | 5711 | --processing_template_decl; |
7088fca9 KL |
5712 | if (TREE_CODE (r) == VAR_DECL) |
5713 | { | |
5714 | tree init; | |
17f29637 | 5715 | |
7088fca9 KL |
5716 | if (DECL_INITIALIZED_IN_CLASS_P (r)) |
5717 | init = tsubst_expr (DECL_INITIAL (t), args, | |
5718 | tf_error | tf_warning, NULL_TREE); | |
5719 | else | |
5720 | init = NULL_TREE; | |
8d08fdba | 5721 | |
8c6ab2db NS |
5722 | finish_static_data_member_decl |
5723 | (r, init, /*asmspec_tree=*/NULL_TREE, /*flags=*/0); | |
8d08fdba | 5724 | |
7088fca9 KL |
5725 | if (DECL_INITIALIZED_IN_CLASS_P (r)) |
5726 | check_static_variable_definition (r, TREE_TYPE (r)); | |
5727 | } | |
5728 | else if (TREE_CODE (r) == FIELD_DECL) | |
5729 | { | |
5730 | /* Determine whether R has a valid type and can be | |
5731 | completed later. If R is invalid, then it is | |
5732 | replaced by error_mark_node so that it will not be | |
5733 | added to TYPE_FIELDS. */ | |
5734 | tree rtype = TREE_TYPE (r); | |
5735 | if (can_complete_type_without_circularity (rtype)) | |
5736 | complete_type (rtype); | |
5737 | ||
5738 | if (!COMPLETE_TYPE_P (rtype)) | |
5739 | { | |
5740 | cxx_incomplete_type_error (r, rtype); | |
5741 | r = error_mark_node; | |
5742 | } | |
5743 | } | |
5566b478 | 5744 | |
7088fca9 KL |
5745 | /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE, |
5746 | such a thing will already have been added to the field | |
5747 | list by tsubst_enum in finish_member_declaration in the | |
5e0c54e5 | 5748 | CLASSTYPE_NESTED_UTDS case above. */ |
7088fca9 KL |
5749 | if (!(TREE_CODE (r) == TYPE_DECL |
5750 | && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE | |
43b26a01 | 5751 | && DECL_ARTIFICIAL (r))) |
7088fca9 KL |
5752 | { |
5753 | set_current_access_from_decl (r); | |
5754 | finish_member_declaration (r); | |
5755 | } | |
5756 | } | |
5757 | } | |
61fc8c9e | 5758 | } |
7088fca9 KL |
5759 | else |
5760 | { | |
5761 | if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)) | |
5762 | { | |
5763 | /* Build new CLASSTYPE_FRIEND_CLASSES. */ | |
5764 | ||
5765 | tree friend_type = t; | |
b939a023 | 5766 | bool adjust_processing_template_decl = false; |
1aed5355 | 5767 | |
7088fca9 | 5768 | if (TREE_CODE (friend_type) == TEMPLATE_DECL) |
b939a023 KL |
5769 | { |
5770 | friend_type = tsubst_friend_class (friend_type, args); | |
5771 | adjust_processing_template_decl = true; | |
5772 | } | |
5773 | else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE) | |
5774 | { | |
5775 | friend_type = tsubst (friend_type, args, | |
5776 | tf_error | tf_warning, NULL_TREE); | |
5777 | if (TREE_CODE (friend_type) == TEMPLATE_DECL) | |
5778 | friend_type = TREE_TYPE (friend_type); | |
5779 | adjust_processing_template_decl = true; | |
5780 | } | |
5781 | else if (TREE_CODE (friend_type) == TYPENAME_TYPE) | |
5782 | { | |
5783 | friend_type = tsubst (friend_type, args, | |
5784 | tf_error | tf_warning, NULL_TREE); | |
5785 | /* Bump processing_template_decl for correct | |
5786 | dependent_type_p calculation. */ | |
5787 | ++processing_template_decl; | |
5788 | if (dependent_type_p (friend_type)) | |
5789 | adjust_processing_template_decl = true; | |
5790 | --processing_template_decl; | |
5791 | } | |
7088fca9 | 5792 | else if (uses_template_parms (friend_type)) |
b939a023 KL |
5793 | friend_type = tsubst (friend_type, args, |
5794 | tf_error | tf_warning, NULL_TREE); | |
c4d0910c | 5795 | else if (CLASSTYPE_USE_TEMPLATE (friend_type)) |
b939a023 | 5796 | friend_type = friend_type; |
7088fca9 KL |
5797 | else |
5798 | { | |
5799 | tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type)); | |
5800 | ||
5801 | /* The call to xref_tag_from_type does injection for friend | |
5802 | classes. */ | |
5803 | push_nested_namespace (ns); | |
b939a023 | 5804 | friend_type = |
29ef83de KL |
5805 | xref_tag_from_type (friend_type, NULL_TREE, |
5806 | /*tag_scope=*/ts_global); | |
7088fca9 KL |
5807 | pop_nested_namespace (ns); |
5808 | } | |
1aed5355 | 5809 | |
b939a023 | 5810 | if (adjust_processing_template_decl) |
7088fca9 KL |
5811 | /* Trick make_friend_class into realizing that the friend |
5812 | we're adding is a template, not an ordinary class. It's | |
5813 | important that we use make_friend_class since it will | |
5814 | perform some error-checking and output cross-reference | |
5815 | information. */ | |
5816 | ++processing_template_decl; | |
fc378698 | 5817 | |
b939a023 KL |
5818 | if (friend_type != error_mark_node) |
5819 | make_friend_class (type, friend_type, /*complain=*/false); | |
fc378698 | 5820 | |
b939a023 | 5821 | if (adjust_processing_template_decl) |
7088fca9 KL |
5822 | --processing_template_decl; |
5823 | } | |
5824 | else | |
9579624e KL |
5825 | { |
5826 | /* Build new DECL_FRIENDLIST. */ | |
5827 | tree r; | |
5828 | ||
6e432b31 KL |
5829 | /* The the file and line for this declaration, to |
5830 | assist in error message reporting. Since we | |
5831 | called push_tinst_level above, we don't need to | |
5832 | restore these. */ | |
5833 | input_location = DECL_SOURCE_LOCATION (t); | |
5834 | ||
9579624e | 5835 | if (TREE_CODE (t) == TEMPLATE_DECL) |
cad7e87b NS |
5836 | { |
5837 | ++processing_template_decl; | |
5838 | push_deferring_access_checks (dk_no_check); | |
5839 | } | |
5840 | ||
9579624e | 5841 | r = tsubst_friend_function (t, args); |
9579624e | 5842 | add_friend (type, r, /*complain=*/false); |
cad7e87b NS |
5843 | if (TREE_CODE (t) == TEMPLATE_DECL) |
5844 | { | |
5845 | pop_deferring_access_checks (); | |
5846 | --processing_template_decl; | |
5847 | } | |
9579624e | 5848 | } |
7088fca9 KL |
5849 | } |
5850 | } | |
5566b478 | 5851 | |
61a127b3 MM |
5852 | /* Set the file and line number information to whatever is given for |
5853 | the class itself. This puts error messages involving generated | |
5854 | implicit functions at a predictable point, and the same point | |
5855 | that would be used for non-template classes. */ | |
7088fca9 | 5856 | typedecl = TYPE_MAIN_DECL (type); |
f31686a3 | 5857 | input_location = DECL_SOURCE_LOCATION (typedecl); |
6de9cd9a | 5858 | |
61a127b3 | 5859 | unreverse_member_declarations (type); |
9f33663b | 5860 | finish_struct_1 (type); |
5524676d | 5861 | TYPE_BEING_DEFINED (type) = 0; |
8d08fdba | 5862 | |
9188c363 MM |
5863 | /* Now that the class is complete, instantiate default arguments for |
5864 | any member functions. We don't do this earlier because the | |
5865 | default arguments may reference members of the class. */ | |
5866 | if (!PRIMARY_TEMPLATE_P (template)) | |
5867 | for (t = TYPE_METHODS (type); t; t = TREE_CHAIN (t)) | |
5868 | if (TREE_CODE (t) == FUNCTION_DECL | |
dc957d14 | 5869 | /* Implicitly generated member functions will not have template |
9188c363 MM |
5870 | information; they are not instantiations, but instead are |
5871 | created "fresh" for each instantiation. */ | |
5872 | && DECL_TEMPLATE_INFO (t)) | |
5873 | tsubst_default_arguments (t); | |
5874 | ||
b74a0560 | 5875 | popclass (); |
5566b478 | 5876 | pop_from_top_level (); |
78757caa | 5877 | pop_deferring_access_checks (); |
5566b478 MS |
5878 | pop_tinst_level (); |
5879 | ||
4684cd27 MM |
5880 | /* The vtable for a template class can be emitted in any translation |
5881 | unit in which the class is instantiated. When there is no key | |
5882 | method, however, finish_struct_1 will already have added TYPE to | |
5883 | the keyed_classes list. */ | |
5884 | if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type)) | |
9aad8f83 MA |
5885 | keyed_classes = tree_cons (NULL_TREE, type, keyed_classes); |
5886 | ||
5566b478 | 5887 | return type; |
8d08fdba MS |
5888 | } |
5889 | ||
00d3396f | 5890 | static tree |
a91db711 | 5891 | tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl) |
00d3396f | 5892 | { |
a91db711 NS |
5893 | tree r; |
5894 | ||
5895 | if (!t) | |
5896 | r = t; | |
5897 | else if (TYPE_P (t)) | |
5898 | r = tsubst (t, args, complain, in_decl); | |
5899 | else | |
00d3396f | 5900 | { |
a91db711 NS |
5901 | r = tsubst_expr (t, args, complain, in_decl); |
5902 | ||
5903 | if (!uses_template_parms (r)) | |
4d5f3fbd | 5904 | { |
a91db711 NS |
5905 | /* Sometimes, one of the args was an expression involving a |
5906 | template constant parameter, like N - 1. Now that we've | |
5907 | tsubst'd, we might have something like 2 - 1. This will | |
5908 | confuse lookup_template_class, so we do constant folding | |
5909 | here. We have to unset processing_template_decl, to fool | |
5910 | tsubst_copy_and_build() into building an actual tree. */ | |
5911 | ||
5912 | /* If the TREE_TYPE of ARG is not NULL_TREE, ARG is already | |
5913 | as simple as it's going to get, and trying to reprocess | |
5914 | the trees will break. Once tsubst_expr et al DTRT for | |
5915 | non-dependent exprs, this code can go away, as the type | |
5916 | will always be set. */ | |
5917 | if (!TREE_TYPE (r)) | |
5918 | { | |
5919 | int saved_processing_template_decl = processing_template_decl; | |
5920 | processing_template_decl = 0; | |
5921 | r = tsubst_copy_and_build (r, /*args=*/NULL_TREE, | |
5922 | tf_error, /*in_decl=*/NULL_TREE, | |
5923 | /*function_p=*/false); | |
5924 | processing_template_decl = saved_processing_template_decl; | |
5925 | } | |
5926 | r = fold (r); | |
4d5f3fbd | 5927 | } |
bd83b409 | 5928 | } |
a91db711 | 5929 | return r; |
bd83b409 NS |
5930 | } |
5931 | ||
a91db711 | 5932 | /* Substitute ARGS into the vector or list of template arguments T. */ |
830bfa74 | 5933 | |
e9659ab0 | 5934 | static tree |
a91db711 | 5935 | tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl) |
830bfa74 | 5936 | { |
bf12d54d | 5937 | int len = TREE_VEC_LENGTH (t); |
a91db711 | 5938 | int need_new = 0, i; |
c68b0a84 | 5939 | tree *elts = alloca (len * sizeof (tree)); |
830bfa74 | 5940 | |
830bfa74 MM |
5941 | for (i = 0; i < len; i++) |
5942 | { | |
bf12d54d NS |
5943 | tree orig_arg = TREE_VEC_ELT (t, i); |
5944 | tree new_arg; | |
a91db711 | 5945 | |
bf12d54d NS |
5946 | if (TREE_CODE (orig_arg) == TREE_VEC) |
5947 | new_arg = tsubst_template_args (orig_arg, args, complain, in_decl); | |
830bfa74 | 5948 | else |
a91db711 | 5949 | new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl); |
830bfa74 | 5950 | |
a91db711 | 5951 | if (new_arg == error_mark_node) |
08e72a19 JM |
5952 | return error_mark_node; |
5953 | ||
a91db711 NS |
5954 | elts[i] = new_arg; |
5955 | if (new_arg != orig_arg) | |
830bfa74 MM |
5956 | need_new = 1; |
5957 | } | |
5958 | ||
5959 | if (!need_new) | |
5960 | return t; | |
a91db711 | 5961 | |
bf12d54d NS |
5962 | t = make_tree_vec (len); |
5963 | for (i = 0; i < len; i++) | |
5964 | TREE_VEC_ELT (t, i) = elts[i]; | |
830bfa74 MM |
5965 | |
5966 | return t; | |
5967 | } | |
5968 | ||
36a117a5 MM |
5969 | /* Return the result of substituting ARGS into the template parameters |
5970 | given by PARMS. If there are m levels of ARGS and m + n levels of | |
5971 | PARMS, then the result will contain n levels of PARMS. For | |
5972 | example, if PARMS is `template <class T> template <class U> | |
5973 | template <T*, U, class V>' and ARGS is {{int}, {double}} then the | |
5974 | result will be `template <int*, double, class V>'. */ | |
5975 | ||
e9659ab0 | 5976 | static tree |
3a978d72 | 5977 | tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain) |
36a117a5 | 5978 | { |
f71f87f9 MM |
5979 | tree r = NULL_TREE; |
5980 | tree* new_parms; | |
36a117a5 MM |
5981 | |
5982 | for (new_parms = &r; | |
5983 | TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args); | |
5984 | new_parms = &(TREE_CHAIN (*new_parms)), | |
5985 | parms = TREE_CHAIN (parms)) | |
5986 | { | |
5987 | tree new_vec = | |
5988 | make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms))); | |
5989 | int i; | |
5990 | ||
5991 | for (i = 0; i < TREE_VEC_LENGTH (new_vec); ++i) | |
5992 | { | |
833aa4c4 NS |
5993 | tree tuple = TREE_VEC_ELT (TREE_VALUE (parms), i); |
5994 | tree default_value = TREE_PURPOSE (tuple); | |
5995 | tree parm_decl = TREE_VALUE (tuple); | |
5996 | ||
5997 | parm_decl = tsubst (parm_decl, args, complain, NULL_TREE); | |
a91db711 NS |
5998 | default_value = tsubst_template_arg (default_value, args, |
5999 | complain, NULL_TREE); | |
6000 | ||
6001 | tuple = build_tree_list (default_value, parm_decl); | |
833aa4c4 | 6002 | TREE_VEC_ELT (new_vec, i) = tuple; |
36a117a5 MM |
6003 | } |
6004 | ||
6005 | *new_parms = | |
4890c2f4 MM |
6006 | tree_cons (size_int (TMPL_PARMS_DEPTH (parms) |
6007 | - TMPL_ARGS_DEPTH (args)), | |
36a117a5 MM |
6008 | new_vec, NULL_TREE); |
6009 | } | |
6010 | ||
6011 | return r; | |
6012 | } | |
6013 | ||
ed44da02 MM |
6014 | /* Substitute the ARGS into the indicated aggregate (or enumeration) |
6015 | type T. If T is not an aggregate or enumeration type, it is | |
6016 | handled as if by tsubst. IN_DECL is as for tsubst. If | |
838dfd8a | 6017 | ENTERING_SCOPE is nonzero, T is the context for a template which |
dc957d14 | 6018 | we are presently tsubst'ing. Return the substituted value. */ |
36a117a5 | 6019 | |
e9659ab0 | 6020 | static tree |
3a978d72 NN |
6021 | tsubst_aggr_type (tree t, |
6022 | tree args, | |
6023 | tsubst_flags_t complain, | |
6024 | tree in_decl, | |
6025 | int entering_scope) | |
36a117a5 MM |
6026 | { |
6027 | if (t == NULL_TREE) | |
6028 | return NULL_TREE; | |
6029 | ||
6030 | switch (TREE_CODE (t)) | |
6031 | { | |
6032 | case RECORD_TYPE: | |
6033 | if (TYPE_PTRMEMFUNC_P (t)) | |
46c895ac | 6034 | return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl); |
36a117a5 | 6035 | |
f4f206f4 | 6036 | /* Else fall through. */ |
ed44da02 | 6037 | case ENUMERAL_TYPE: |
36a117a5 | 6038 | case UNION_TYPE: |
5db698f6 | 6039 | if (TYPE_TEMPLATE_INFO (t)) |
36a117a5 MM |
6040 | { |
6041 | tree argvec; | |
6042 | tree context; | |
6043 | tree r; | |
6044 | ||
6045 | /* First, determine the context for the type we are looking | |
6046 | up. */ | |
4f7847ca NS |
6047 | context = TYPE_CONTEXT (t); |
6048 | if (context) | |
6049 | context = tsubst_aggr_type (context, args, complain, | |
36a117a5 | 6050 | in_decl, /*entering_scope=*/1); |
36a117a5 MM |
6051 | |
6052 | /* Then, figure out what arguments are appropriate for the | |
6053 | type we are trying to find. For example, given: | |
6054 | ||
6055 | template <class T> struct S; | |
6056 | template <class T, class U> void f(T, U) { S<U> su; } | |
6057 | ||
6058 | and supposing that we are instantiating f<int, double>, | |
6059 | then our ARGS will be {int, double}, but, when looking up | |
6060 | S we only want {double}. */ | |
a91db711 NS |
6061 | argvec = tsubst_template_args (TYPE_TI_ARGS (t), args, |
6062 | complain, in_decl); | |
08e72a19 JM |
6063 | if (argvec == error_mark_node) |
6064 | return error_mark_node; | |
36a117a5 MM |
6065 | |
6066 | r = lookup_template_class (t, argvec, in_decl, context, | |
f9c244b8 | 6067 | entering_scope, complain); |
36a117a5 | 6068 | |
c2ea3a40 | 6069 | return cp_build_qualified_type_real (r, TYPE_QUALS (t), complain); |
36a117a5 MM |
6070 | } |
6071 | else | |
6072 | /* This is not a template type, so there's nothing to do. */ | |
6073 | return t; | |
6074 | ||
6075 | default: | |
4393e105 | 6076 | return tsubst (t, args, complain, in_decl); |
36a117a5 MM |
6077 | } |
6078 | } | |
6079 | ||
9188c363 MM |
6080 | /* Substitute into the default argument ARG (a default argument for |
6081 | FN), which has the indicated TYPE. */ | |
6082 | ||
6083 | tree | |
3a978d72 | 6084 | tsubst_default_argument (tree fn, tree type, tree arg) |
9188c363 | 6085 | { |
2436b51f MM |
6086 | tree saved_class_ptr = NULL_TREE; |
6087 | tree saved_class_ref = NULL_TREE; | |
6088 | ||
9188c363 MM |
6089 | /* This default argument came from a template. Instantiate the |
6090 | default argument here, not in tsubst. In the case of | |
6091 | something like: | |
6092 | ||
6093 | template <class T> | |
6094 | struct S { | |
6095 | static T t(); | |
6096 | void f(T = t()); | |
6097 | }; | |
6098 | ||
6099 | we must be careful to do name lookup in the scope of S<T>, | |
d5a10cf0 | 6100 | rather than in the current class. */ |
2b59fc25 | 6101 | push_access_scope (fn); |
c6671cbb MM |
6102 | /* The default argument expression should not be considered to be |
6103 | within the scope of FN. Since push_access_scope sets | |
6104 | current_function_decl, we must explicitly clear it here. */ | |
6105 | current_function_decl = NULL_TREE; | |
2436b51f MM |
6106 | /* The "this" pointer is not valid in a default argument. */ |
6107 | if (cfun) | |
6108 | { | |
6109 | saved_class_ptr = current_class_ptr; | |
6110 | cp_function_chain->x_current_class_ptr = NULL_TREE; | |
6111 | saved_class_ref = current_class_ref; | |
6112 | cp_function_chain->x_current_class_ref = NULL_TREE; | |
6113 | } | |
9188c363 | 6114 | |
d5a10cf0 | 6115 | push_deferring_access_checks(dk_no_deferred); |
c2ea3a40 NS |
6116 | arg = tsubst_expr (arg, DECL_TI_ARGS (fn), |
6117 | tf_error | tf_warning, NULL_TREE); | |
d5a10cf0 MM |
6118 | pop_deferring_access_checks(); |
6119 | ||
2436b51f MM |
6120 | /* Restore the "this" pointer. */ |
6121 | if (cfun) | |
6122 | { | |
6123 | cp_function_chain->x_current_class_ptr = saved_class_ptr; | |
6124 | cp_function_chain->x_current_class_ref = saved_class_ref; | |
6125 | } | |
6126 | ||
2b59fc25 | 6127 | pop_access_scope (fn); |
9188c363 MM |
6128 | |
6129 | /* Make sure the default argument is reasonable. */ | |
6130 | arg = check_default_argument (type, arg); | |
6131 | ||
6132 | return arg; | |
6133 | } | |
6134 | ||
6135 | /* Substitute into all the default arguments for FN. */ | |
6136 | ||
6137 | static void | |
3a978d72 | 6138 | tsubst_default_arguments (tree fn) |
9188c363 MM |
6139 | { |
6140 | tree arg; | |
6141 | tree tmpl_args; | |
6142 | ||
6143 | tmpl_args = DECL_TI_ARGS (fn); | |
6144 | ||
6145 | /* If this function is not yet instantiated, we certainly don't need | |
6146 | its default arguments. */ | |
6147 | if (uses_template_parms (tmpl_args)) | |
6148 | return; | |
6149 | ||
6150 | for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn)); | |
6151 | arg; | |
6152 | arg = TREE_CHAIN (arg)) | |
6153 | if (TREE_PURPOSE (arg)) | |
6154 | TREE_PURPOSE (arg) = tsubst_default_argument (fn, | |
6155 | TREE_VALUE (arg), | |
6156 | TREE_PURPOSE (arg)); | |
6157 | } | |
6158 | ||
fc6a28d7 MM |
6159 | /* Substitute the ARGS into the T, which is a _DECL. Return the |
6160 | result of the substitution. Issue error and warning messages under | |
6161 | control of COMPLAIN. */ | |
00d3396f | 6162 | |
e9659ab0 | 6163 | static tree |
fc6a28d7 | 6164 | tsubst_decl (tree t, tree args, tsubst_flags_t complain) |
8d08fdba | 6165 | { |
82a98427 | 6166 | location_t saved_loc; |
b370501f | 6167 | tree r = NULL_TREE; |
4b2811e9 | 6168 | tree in_decl = t; |
830bfa74 | 6169 | |
ae58fa02 | 6170 | /* Set the filename and linenumber to improve error-reporting. */ |
82a98427 | 6171 | saved_loc = input_location; |
f31686a3 | 6172 | input_location = DECL_SOURCE_LOCATION (t); |
b7484fbe | 6173 | |
8d08fdba MS |
6174 | switch (TREE_CODE (t)) |
6175 | { | |
98c1c668 JM |
6176 | case TEMPLATE_DECL: |
6177 | { | |
28e42b7e KL |
6178 | /* We can get here when processing a member function template, |
6179 | member class template, and template template parameter of | |
6180 | a template class. */ | |
98c1c668 | 6181 | tree decl = DECL_TEMPLATE_RESULT (t); |
386b8a85 | 6182 | tree spec; |
28e42b7e KL |
6183 | tree tmpl_args; |
6184 | tree full_args; | |
98c1c668 | 6185 | |
28e42b7e | 6186 | if (DECL_TEMPLATE_TEMPLATE_PARM_P (t)) |
db2767b6 | 6187 | { |
28e42b7e KL |
6188 | /* Template template parameter is treated here. */ |
6189 | tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl); | |
6190 | if (new_type == error_mark_node) | |
6191 | return error_mark_node; | |
36a117a5 | 6192 | |
28e42b7e KL |
6193 | r = copy_decl (t); |
6194 | TREE_CHAIN (r) = NULL_TREE; | |
6195 | TREE_TYPE (r) = new_type; | |
6196 | DECL_TEMPLATE_RESULT (r) | |
6197 | = build_decl (TYPE_DECL, DECL_NAME (decl), new_type); | |
6198 | DECL_TEMPLATE_PARMS (r) | |
6199 | = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args, | |
6200 | complain); | |
6201 | TYPE_NAME (new_type) = r; | |
6202 | break; | |
6203 | } | |
36a117a5 | 6204 | |
28e42b7e KL |
6205 | /* We might already have an instance of this template. |
6206 | The ARGS are for the surrounding class type, so the | |
6207 | full args contain the tsubst'd args for the context, | |
6208 | plus the innermost args from the template decl. */ | |
6209 | tmpl_args = DECL_CLASS_TEMPLATE_P (t) | |
6210 | ? CLASSTYPE_TI_ARGS (TREE_TYPE (t)) | |
6211 | : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t)); | |
6212 | full_args = tsubst_template_args (tmpl_args, args, | |
6213 | complain, in_decl); | |
6214 | ||
6215 | /* tsubst_template_args doesn't copy the vector if | |
6216 | nothing changed. But, *something* should have | |
6217 | changed. */ | |
6218 | gcc_assert (full_args != tmpl_args); | |
6219 | ||
6220 | spec = retrieve_specialization (t, full_args, | |
6221 | /*class_specializations_p=*/true); | |
6222 | if (spec != NULL_TREE) | |
6223 | { | |
6224 | r = spec; | |
6225 | break; | |
db2767b6 | 6226 | } |
98c1c668 JM |
6227 | |
6228 | /* Make a new template decl. It will be similar to the | |
6229 | original, but will record the current template arguments. | |
6230 | We also create a new function declaration, which is just | |
6231 | like the old one, but points to this new template, rather | |
6232 | than the old one. */ | |
0acf7199 | 6233 | r = copy_decl (t); |
50bc768d | 6234 | gcc_assert (DECL_LANG_SPECIFIC (r) != 0); |
ae58fa02 | 6235 | TREE_CHAIN (r) = NULL_TREE; |
db2767b6 | 6236 | |
ae58fa02 | 6237 | DECL_CONTEXT (r) |
4f1c5b7d | 6238 | = tsubst_aggr_type (DECL_CONTEXT (t), args, |
c2ea3a40 | 6239 | complain, in_decl, |
4393e105 | 6240 | /*entering_scope=*/1); |
ae58fa02 | 6241 | DECL_TEMPLATE_INFO (r) = build_tree_list (t, args); |
93cdc044 JM |
6242 | |
6243 | if (TREE_CODE (decl) == TYPE_DECL) | |
6244 | { | |
c2ea3a40 | 6245 | tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl); |
2620d095 KL |
6246 | if (new_type == error_mark_node) |
6247 | return error_mark_node; | |
6248 | ||
ae58fa02 MM |
6249 | TREE_TYPE (r) = new_type; |
6250 | CLASSTYPE_TI_TEMPLATE (new_type) = r; | |
17aec3eb | 6251 | DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type); |
ae58fa02 | 6252 | DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type); |
93cdc044 JM |
6253 | } |
6254 | else | |
6255 | { | |
c2ea3a40 | 6256 | tree new_decl = tsubst (decl, args, complain, in_decl); |
caec1dc0 KL |
6257 | if (new_decl == error_mark_node) |
6258 | return error_mark_node; | |
17aec3eb RK |
6259 | |
6260 | DECL_TEMPLATE_RESULT (r) = new_decl; | |
ae58fa02 MM |
6261 | DECL_TI_TEMPLATE (new_decl) = r; |
6262 | TREE_TYPE (r) = TREE_TYPE (new_decl); | |
6263 | DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl); | |
93cdc044 JM |
6264 | } |
6265 | ||
ae58fa02 MM |
6266 | SET_DECL_IMPLICIT_INSTANTIATION (r); |
6267 | DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE; | |
6268 | DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE; | |
98c1c668 JM |
6269 | |
6270 | /* The template parameters for this new template are all the | |
6271 | template parameters for the old template, except the | |
c6002625 | 6272 | outermost level of parameters. */ |
ae58fa02 | 6273 | DECL_TEMPLATE_PARMS (r) |
4393e105 | 6274 | = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args, |
c2ea3a40 | 6275 | complain); |
98c1c668 | 6276 | |
93cdc044 | 6277 | if (PRIMARY_TEMPLATE_P (t)) |
ae58fa02 | 6278 | DECL_PRIMARY_TEMPLATE (r) = r; |
93cdc044 | 6279 | |
8c6ab2db NS |
6280 | if (TREE_CODE (decl) != TYPE_DECL) |
6281 | /* Record this non-type partial instantiation. */ | |
6282 | register_specialization (r, t, | |
6283 | DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r))); | |
98c1c668 | 6284 | } |
ae58fa02 | 6285 | break; |
8d08fdba MS |
6286 | |
6287 | case FUNCTION_DECL: | |
6288 | { | |
386b8a85 | 6289 | tree ctx; |
87603ed0 | 6290 | tree argvec = NULL_TREE; |
cf38f48a | 6291 | tree *friends; |
36a117a5 | 6292 | tree gen_tmpl; |
fc6a28d7 | 6293 | tree type; |
5566b478 | 6294 | int member; |
d8c4447d MM |
6295 | int args_depth; |
6296 | int parms_depth; | |
5566b478 | 6297 | |
36a117a5 | 6298 | /* Nobody should be tsubst'ing into non-template functions. */ |
50bc768d | 6299 | gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE); |
36a117a5 MM |
6300 | |
6301 | if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL) | |
6302 | { | |
6303 | tree spec; | |
00cf3e31 MM |
6304 | bool dependent_p; |
6305 | ||
6306 | /* If T is not dependent, just return it. We have to | |
6307 | increment PROCESSING_TEMPLATE_DECL because | |
6308 | value_dependent_expression_p assumes that nothing is | |
6309 | dependent when PROCESSING_TEMPLATE_DECL is zero. */ | |
6310 | ++processing_template_decl; | |
6311 | dependent_p = value_dependent_expression_p (t); | |
6312 | --processing_template_decl; | |
6313 | if (!dependent_p) | |
6314 | return t; | |
36a117a5 MM |
6315 | |
6316 | /* Calculate the most general template of which R is a | |
6317 | specialization, and the complete set of arguments used to | |
6318 | specialize R. */ | |
6319 | gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t)); | |
a91db711 NS |
6320 | argvec = tsubst_template_args (DECL_TI_ARGS |
6321 | (DECL_TEMPLATE_RESULT (gen_tmpl)), | |
6322 | args, complain, in_decl); | |
36a117a5 MM |
6323 | |
6324 | /* Check to see if we already have this specialization. */ | |
c7222c02 MM |
6325 | spec = retrieve_specialization (gen_tmpl, argvec, |
6326 | /*class_specializations_p=*/false); | |
7ddedda4 | 6327 | |
36a117a5 | 6328 | if (spec) |
ae58fa02 MM |
6329 | { |
6330 | r = spec; | |
6331 | break; | |
6332 | } | |
d8c4447d | 6333 | |
f9a7ae04 MM |
6334 | /* We can see more levels of arguments than parameters if |
6335 | there was a specialization of a member template, like | |
6336 | this: | |
6337 | ||
6338 | template <class T> struct S { template <class U> void f(); } | |
6339 | template <> template <class U> void S<int>::f(U); | |
6340 | ||
dc957d14 | 6341 | Here, we'll be substituting into the specialization, |
f9a7ae04 MM |
6342 | because that's where we can find the code we actually |
6343 | want to generate, but we'll have enough arguments for | |
6344 | the most general template. | |
6345 | ||
6346 | We also deal with the peculiar case: | |
d8c4447d MM |
6347 | |
6348 | template <class T> struct S { | |
6349 | template <class U> friend void f(); | |
6350 | }; | |
74b846e0 | 6351 | template <class U> void f() {} |
d8c4447d MM |
6352 | template S<int>; |
6353 | template void f<double>(); | |
6354 | ||
6355 | Here, the ARGS for the instantiation of will be {int, | |
6356 | double}. But, we only need as many ARGS as there are | |
6357 | levels of template parameters in CODE_PATTERN. We are | |
6358 | careful not to get fooled into reducing the ARGS in | |
6359 | situations like: | |
6360 | ||
6361 | template <class T> struct S { template <class U> void f(U); } | |
6362 | template <class T> template <> void S<T>::f(int) {} | |
6363 | ||
6364 | which we can spot because the pattern will be a | |
6365 | specialization in this case. */ | |
6366 | args_depth = TMPL_ARGS_DEPTH (args); | |
6367 | parms_depth = | |
6368 | TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t))); | |
6369 | if (args_depth > parms_depth | |
6370 | && !DECL_TEMPLATE_SPECIALIZATION (t)) | |
f9a7ae04 | 6371 | args = get_innermost_template_args (args, parms_depth); |
36a117a5 MM |
6372 | } |
6373 | else | |
6374 | { | |
6375 | /* This special case arises when we have something like this: | |
6376 | ||
6377 | template <class T> struct S { | |
6378 | friend void f<int>(int, double); | |
6379 | }; | |
6380 | ||
6381 | Here, the DECL_TI_TEMPLATE for the friend declaration | |
10b1d5e7 MM |
6382 | will be an IDENTIFIER_NODE. We are being called from |
6383 | tsubst_friend_function, and we want only to create a | |
6384 | new decl (R) with appropriate types so that we can call | |
6385 | determine_specialization. */ | |
36a117a5 MM |
6386 | gen_tmpl = NULL_TREE; |
6387 | } | |
6388 | ||
6eb3bb27 | 6389 | if (DECL_CLASS_SCOPE_P (t)) |
8d08fdba | 6390 | { |
5566b478 MS |
6391 | if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t))) |
6392 | member = 2; | |
6393 | else | |
6394 | member = 1; | |
4f1c5b7d | 6395 | ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, |
8c6ab2db | 6396 | complain, t, /*entering_scope=*/1); |
5566b478 MS |
6397 | } |
6398 | else | |
6399 | { | |
6400 | member = 0; | |
4f1c5b7d | 6401 | ctx = DECL_CONTEXT (t); |
5566b478 | 6402 | } |
fc6a28d7 | 6403 | type = tsubst (TREE_TYPE (t), args, complain, in_decl); |
b3da7bb1 MM |
6404 | if (type == error_mark_node) |
6405 | return error_mark_node; | |
8d08fdba | 6406 | |
5566b478 MS |
6407 | /* We do NOT check for matching decls pushed separately at this |
6408 | point, as they may not represent instantiations of this | |
6409 | template, and in any case are considered separate under the | |
0a7394bc | 6410 | discrete model. */ |
0acf7199 | 6411 | r = copy_decl (t); |
e1467ff2 | 6412 | DECL_USE_TEMPLATE (r) = 0; |
5566b478 | 6413 | TREE_TYPE (r) = type; |
92643fea MM |
6414 | /* Clear out the mangled name and RTL for the instantiation. */ |
6415 | SET_DECL_ASSEMBLER_NAME (r, NULL_TREE); | |
6416 | SET_DECL_RTL (r, NULL_RTX); | |
5bd61841 | 6417 | DECL_INITIAL (r) = NULL_TREE; |
4f1c5b7d | 6418 | DECL_CONTEXT (r) = ctx; |
5566b478 | 6419 | |
1f6e1acc | 6420 | if (member && DECL_CONV_FN_P (r)) |
1f84ec23 MM |
6421 | /* Type-conversion operator. Reconstruct the name, in |
6422 | case it's the name of one of the template's parameters. */ | |
6423 | DECL_NAME (r) = mangle_conv_op_name_for_type (TREE_TYPE (type)); | |
5566b478 | 6424 | |
4393e105 | 6425 | DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args, |
c2ea3a40 | 6426 | complain, t); |
477f6664 | 6427 | DECL_RESULT (r) = NULL_TREE; |
711734a9 JM |
6428 | |
6429 | TREE_STATIC (r) = 0; | |
6430 | TREE_PUBLIC (r) = TREE_PUBLIC (t); | |
6431 | DECL_EXTERNAL (r) = 1; | |
4684cd27 MM |
6432 | /* If this is an instantiation of a function with internal |
6433 | linkage, we already know what object file linkage will be | |
6434 | assigned to the instantiation. */ | |
6435 | DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r); | |
711734a9 JM |
6436 | DECL_DEFER_OUTPUT (r) = 0; |
6437 | TREE_CHAIN (r) = NULL_TREE; | |
6438 | DECL_PENDING_INLINE_INFO (r) = 0; | |
59026e79 | 6439 | DECL_PENDING_INLINE_P (r) = 0; |
655dc6ee | 6440 | DECL_SAVED_TREE (r) = NULL_TREE; |
711734a9 | 6441 | TREE_USED (r) = 0; |
db9b2174 MM |
6442 | if (DECL_CLONED_FUNCTION (r)) |
6443 | { | |
6444 | DECL_CLONED_FUNCTION (r) = tsubst (DECL_CLONED_FUNCTION (t), | |
c2ea3a40 | 6445 | args, complain, t); |
db9b2174 MM |
6446 | TREE_CHAIN (r) = TREE_CHAIN (DECL_CLONED_FUNCTION (r)); |
6447 | TREE_CHAIN (DECL_CLONED_FUNCTION (r)) = r; | |
6448 | } | |
711734a9 | 6449 | |
92643fea MM |
6450 | /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do |
6451 | this in the special friend case mentioned above where | |
6452 | GEN_TMPL is NULL. */ | |
36a117a5 | 6453 | if (gen_tmpl) |
386b8a85 | 6454 | { |
36a117a5 | 6455 | DECL_TEMPLATE_INFO (r) |
e1b3e07d | 6456 | = tree_cons (gen_tmpl, argvec, NULL_TREE); |
36a117a5 MM |
6457 | SET_DECL_IMPLICIT_INSTANTIATION (r); |
6458 | register_specialization (r, gen_tmpl, argvec); | |
6459 | ||
9188c363 MM |
6460 | /* We're not supposed to instantiate default arguments |
6461 | until they are called, for a template. But, for a | |
6462 | declaration like: | |
6463 | ||
6464 | template <class T> void f () | |
6465 | { extern void g(int i = T()); } | |
6466 | ||
6467 | we should do the substitution when the template is | |
6468 | instantiated. We handle the member function case in | |
6469 | instantiate_class_template since the default arguments | |
6470 | might refer to other members of the class. */ | |
6471 | if (!member | |
6472 | && !PRIMARY_TEMPLATE_P (gen_tmpl) | |
6473 | && !uses_template_parms (argvec)) | |
6474 | tsubst_default_arguments (r); | |
386b8a85 | 6475 | } |
f181d4ae | 6476 | |
cf38f48a MM |
6477 | /* Copy the list of befriending classes. */ |
6478 | for (friends = &DECL_BEFRIENDING_CLASSES (r); | |
6479 | *friends; | |
6480 | friends = &TREE_CHAIN (*friends)) | |
6481 | { | |
6482 | *friends = copy_node (*friends); | |
6483 | TREE_VALUE (*friends) = tsubst (TREE_VALUE (*friends), | |
c2ea3a40 | 6484 | args, complain, |
cf38f48a MM |
6485 | in_decl); |
6486 | } | |
6487 | ||
212e7048 | 6488 | if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r)) |
f181d4ae MM |
6489 | { |
6490 | maybe_retrofit_in_chrg (r); | |
212e7048 MM |
6491 | if (DECL_CONSTRUCTOR_P (r)) |
6492 | grok_ctor_properties (ctx, r); | |
2be678ff JM |
6493 | /* If this is an instantiation of a member template, clone it. |
6494 | If it isn't, that'll be handled by | |
6495 | clone_constructors_and_destructors. */ | |
5e818b93 | 6496 | if (PRIMARY_TEMPLATE_P (gen_tmpl)) |
271e6f02 | 6497 | clone_function_decl (r, /*update_method_vec_p=*/0); |
f181d4ae | 6498 | } |
596ea4e5 | 6499 | else if (IDENTIFIER_OPNAME_P (DECL_NAME (r))) |
4b0d3cbe MM |
6500 | grok_op_properties (r, DECL_FRIEND_P (r), |
6501 | (complain & tf_error) != 0); | |
c8b2e872 MM |
6502 | |
6503 | if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t)) | |
6504 | SET_DECL_FRIEND_CONTEXT (r, | |
6505 | tsubst (DECL_FRIEND_CONTEXT (t), | |
6506 | args, complain, in_decl)); | |
8d08fdba | 6507 | } |
ae58fa02 | 6508 | break; |
8d08fdba MS |
6509 | |
6510 | case PARM_DECL: | |
6511 | { | |
fc6a28d7 MM |
6512 | tree type; |
6513 | ||
ae58fa02 | 6514 | r = copy_node (t); |
833aa4c4 NS |
6515 | if (DECL_TEMPLATE_PARM_P (t)) |
6516 | SET_DECL_TEMPLATE_PARM_P (r); | |
8e51619a | 6517 | |
fc6a28d7 | 6518 | type = tsubst (TREE_TYPE (t), args, complain, in_decl); |
5566b478 | 6519 | TREE_TYPE (r) = type; |
89d684bb | 6520 | c_apply_type_quals_to_decl (cp_type_quals (type), r); |
1b8899d1 | 6521 | |
560ad596 MM |
6522 | if (DECL_INITIAL (r)) |
6523 | { | |
6524 | if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX) | |
6525 | DECL_INITIAL (r) = TREE_TYPE (r); | |
6526 | else | |
6527 | DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args, | |
6528 | complain, in_decl); | |
6529 | } | |
db2767b6 | 6530 | |
5566b478 | 6531 | DECL_CONTEXT (r) = NULL_TREE; |
8e51619a JM |
6532 | |
6533 | if (!DECL_TEMPLATE_PARM_P (r)) | |
6534 | DECL_ARG_TYPE (r) = type_passed_as (type); | |
8d08fdba | 6535 | if (TREE_CHAIN (t)) |
4393e105 | 6536 | TREE_CHAIN (r) = tsubst (TREE_CHAIN (t), args, |
c2ea3a40 | 6537 | complain, TREE_CHAIN (t)); |
8d08fdba | 6538 | } |
ae58fa02 | 6539 | break; |
8d08fdba | 6540 | |
5566b478 MS |
6541 | case FIELD_DECL: |
6542 | { | |
fc6a28d7 MM |
6543 | tree type; |
6544 | ||
0acf7199 | 6545 | r = copy_decl (t); |
fc6a28d7 MM |
6546 | type = tsubst (TREE_TYPE (t), args, complain, in_decl); |
6547 | if (type == error_mark_node) | |
6548 | return error_mark_node; | |
1b8899d1 | 6549 | TREE_TYPE (r) = type; |
89d684bb | 6550 | c_apply_type_quals_to_decl (cp_type_quals (type), r); |
1b8899d1 MM |
6551 | |
6552 | /* We don't have to set DECL_CONTEXT here; it is set by | |
6553 | finish_member_declaration. */ | |
4393e105 | 6554 | DECL_INITIAL (r) = tsubst_expr (DECL_INITIAL (t), args, |
c2ea3a40 | 6555 | complain, in_decl); |
5566b478 | 6556 | TREE_CHAIN (r) = NULL_TREE; |
dfbb4f34 | 6557 | if (VOID_TYPE_P (type)) |
0f51ccfc | 6558 | cp_error_at ("instantiation of %qD as type %qT", r, type); |
5566b478 | 6559 | } |
ae58fa02 | 6560 | break; |
5566b478 MS |
6561 | |
6562 | case USING_DECL: | |
6563 | { | |
ae58fa02 | 6564 | r = copy_node (t); |
fd9aef9d NS |
6565 | /* It is not a dependent using decl any more. */ |
6566 | TREE_TYPE (r) = void_type_node; | |
5566b478 | 6567 | DECL_INITIAL (r) |
c2ea3a40 | 6568 | = tsubst_copy (DECL_INITIAL (t), args, complain, in_decl); |
53400b9e GB |
6569 | DECL_NAME (r) |
6570 | = tsubst_copy (DECL_NAME (t), args, complain, in_decl); | |
5566b478 | 6571 | TREE_CHAIN (r) = NULL_TREE; |
5566b478 | 6572 | } |
ae58fa02 | 6573 | break; |
5566b478 | 6574 | |
9188c363 | 6575 | case TYPE_DECL: |
5566b478 MS |
6576 | case VAR_DECL: |
6577 | { | |
1cea0434 KG |
6578 | tree argvec = NULL_TREE; |
6579 | tree gen_tmpl = NULL_TREE; | |
36a117a5 | 6580 | tree spec; |
1cea0434 | 6581 | tree tmpl = NULL_TREE; |
9188c363 | 6582 | tree ctx; |
fc6a28d7 | 6583 | tree type = NULL_TREE; |
6dfbb909 | 6584 | int local_p; |
9188c363 | 6585 | |
fc6a28d7 MM |
6586 | if (TREE_CODE (t) == TYPE_DECL) |
6587 | { | |
6588 | type = tsubst (TREE_TYPE (t), args, complain, in_decl); | |
6589 | if (TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM | |
6590 | || t == TYPE_MAIN_DECL (TREE_TYPE (t))) | |
6591 | { | |
6592 | /* If this is the canonical decl, we don't have to | |
6593 | mess with instantiations, and often we can't (for | |
6594 | typename, template type parms and such). Note that | |
6595 | TYPE_NAME is not correct for the above test if | |
6596 | we've copied the type for a typedef. */ | |
6597 | r = TYPE_NAME (type); | |
6598 | break; | |
6599 | } | |
6600 | } | |
6601 | ||
6dfbb909 MM |
6602 | /* Assume this is a non-local variable. */ |
6603 | local_p = 0; | |
5566b478 | 6604 | |
de96bf57 | 6605 | if (TYPE_P (CP_DECL_CONTEXT (t))) |
9188c363 | 6606 | ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, |
c2ea3a40 | 6607 | complain, |
9188c363 | 6608 | in_decl, /*entering_scope=*/1); |
2ba9c47e MM |
6609 | else if (DECL_NAMESPACE_SCOPE_P (t)) |
6610 | ctx = DECL_CONTEXT (t); | |
9188c363 | 6611 | else |
6dfbb909 MM |
6612 | { |
6613 | /* Subsequent calls to pushdecl will fill this in. */ | |
6614 | ctx = NULL_TREE; | |
2ba9c47e | 6615 | local_p = 1; |
6dfbb909 | 6616 | } |
9188c363 | 6617 | |
36a117a5 | 6618 | /* Check to see if we already have this specialization. */ |
6dfbb909 MM |
6619 | if (!local_p) |
6620 | { | |
6621 | tmpl = DECL_TI_TEMPLATE (t); | |
6622 | gen_tmpl = most_general_template (tmpl); | |
c2ea3a40 | 6623 | argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl); |
c7222c02 MM |
6624 | spec = retrieve_specialization (gen_tmpl, argvec, |
6625 | /*class_specializations_p=*/false); | |
6dfbb909 | 6626 | } |
9188c363 | 6627 | else |
6dfbb909 | 6628 | spec = retrieve_local_specialization (t); |
9188c363 | 6629 | |
36a117a5 | 6630 | if (spec) |
ae58fa02 MM |
6631 | { |
6632 | r = spec; | |
6633 | break; | |
6634 | } | |
5566b478 | 6635 | |
0acf7199 | 6636 | r = copy_decl (t); |
edebf865 | 6637 | if (TREE_CODE (r) == VAR_DECL) |
39703eb9 | 6638 | { |
fc6a28d7 MM |
6639 | type = tsubst (TREE_TYPE (t), args, complain, in_decl); |
6640 | if (type == error_mark_node) | |
6641 | return error_mark_node; | |
39703eb9 MM |
6642 | type = complete_type (type); |
6643 | DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) | |
6644 | = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t); | |
62e19030 | 6645 | type = check_var_type (DECL_NAME (r), type); |
39703eb9 | 6646 | } |
a3d87771 MM |
6647 | else if (DECL_SELF_REFERENCE_P (t)) |
6648 | SET_DECL_SELF_REFERENCE_P (r); | |
01f4137f | 6649 | TREE_TYPE (r) = type; |
89d684bb | 6650 | c_apply_type_quals_to_decl (cp_type_quals (type), r); |
5566b478 | 6651 | DECL_CONTEXT (r) = ctx; |
92643fea MM |
6652 | /* Clear out the mangled name and RTL for the instantiation. */ |
6653 | SET_DECL_ASSEMBLER_NAME (r, NULL_TREE); | |
6654 | SET_DECL_RTL (r, NULL_RTX); | |
d11ad92e MS |
6655 | |
6656 | /* Don't try to expand the initializer until someone tries to use | |
6657 | this variable; otherwise we run into circular dependencies. */ | |
6658 | DECL_INITIAL (r) = NULL_TREE; | |
19e7881c | 6659 | SET_DECL_RTL (r, NULL_RTX); |
06ceef4e | 6660 | DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0; |
5566b478 | 6661 | |
e0942dcd MM |
6662 | /* Even if the original location is out of scope, the newly |
6663 | substituted one is not. */ | |
6664 | if (TREE_CODE (r) == VAR_DECL) | |
17bbb839 MM |
6665 | { |
6666 | DECL_DEAD_FOR_LOCAL (r) = 0; | |
6667 | DECL_INITIALIZED_P (r) = 0; | |
6668 | } | |
e0942dcd | 6669 | |
6dfbb909 MM |
6670 | if (!local_p) |
6671 | { | |
6672 | /* A static data member declaration is always marked | |
6673 | external when it is declared in-class, even if an | |
6674 | initializer is present. We mimic the non-template | |
6675 | processing here. */ | |
6676 | DECL_EXTERNAL (r) = 1; | |
fa8d6e85 | 6677 | |
6dfbb909 MM |
6678 | register_specialization (r, gen_tmpl, argvec); |
6679 | DECL_TEMPLATE_INFO (r) = tree_cons (tmpl, argvec, NULL_TREE); | |
6680 | SET_DECL_IMPLICIT_INSTANTIATION (r); | |
6681 | } | |
9188c363 | 6682 | else |
6dfbb909 | 6683 | register_local_specialization (r, t); |
5566b478 | 6684 | |
5566b478 | 6685 | TREE_CHAIN (r) = NULL_TREE; |
edebf865 | 6686 | layout_decl (r, 0); |
5566b478 | 6687 | } |
ae58fa02 | 6688 | break; |
5566b478 | 6689 | |
ae58fa02 | 6690 | default: |
315fb5db | 6691 | gcc_unreachable (); |
ae58fa02 MM |
6692 | } |
6693 | ||
6694 | /* Restore the file and line information. */ | |
82a98427 | 6695 | input_location = saved_loc; |
ae58fa02 MM |
6696 | |
6697 | return r; | |
6698 | } | |
6699 | ||
34cd5ae7 | 6700 | /* Substitute into the ARG_TYPES of a function type. */ |
cabc336a | 6701 | |
e9659ab0 | 6702 | static tree |
3a978d72 NN |
6703 | tsubst_arg_types (tree arg_types, |
6704 | tree args, | |
6705 | tsubst_flags_t complain, | |
6706 | tree in_decl) | |
cabc336a MM |
6707 | { |
6708 | tree remaining_arg_types; | |
cabc336a MM |
6709 | tree type; |
6710 | ||
6711 | if (!arg_types || arg_types == void_list_node) | |
6712 | return arg_types; | |
6713 | ||
6714 | remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types), | |
4393e105 MM |
6715 | args, complain, in_decl); |
6716 | if (remaining_arg_types == error_mark_node) | |
6717 | return error_mark_node; | |
6718 | ||
6719 | type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl); | |
6720 | if (type == error_mark_node) | |
6721 | return error_mark_node; | |
4b2811e9 NS |
6722 | if (VOID_TYPE_P (type)) |
6723 | { | |
c2ea3a40 | 6724 | if (complain & tf_error) |
4b2811e9 | 6725 | { |
0f51ccfc | 6726 | error ("invalid parameter type %qT", type); |
4b2811e9 | 6727 | if (in_decl) |
0f51ccfc | 6728 | cp_error_at ("in declaration %qD", in_decl); |
4b2811e9 NS |
6729 | } |
6730 | return error_mark_node; | |
6731 | } | |
cabc336a | 6732 | |
4393e105 MM |
6733 | /* Do array-to-pointer, function-to-pointer conversion, and ignore |
6734 | top-level qualifiers as required. */ | |
6735 | type = TYPE_MAIN_VARIANT (type_decays_to (type)); | |
cabc336a MM |
6736 | |
6737 | /* Note that we do not substitute into default arguments here. The | |
6738 | standard mandates that they be instantiated only when needed, | |
6739 | which is done in build_over_call. */ | |
51632249 JM |
6740 | return hash_tree_cons (TREE_PURPOSE (arg_types), type, |
6741 | remaining_arg_types); | |
cabc336a MM |
6742 | |
6743 | } | |
6744 | ||
4393e105 MM |
6745 | /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does |
6746 | *not* handle the exception-specification for FNTYPE, because the | |
6747 | initial substitution of explicitly provided template parameters | |
6748 | during argument deduction forbids substitution into the | |
6749 | exception-specification: | |
6750 | ||
6751 | [temp.deduct] | |
6752 | ||
6753 | All references in the function type of the function template to the | |
6754 | corresponding template parameters are replaced by the specified tem- | |
6755 | plate argument values. If a substitution in a template parameter or | |
6756 | in the function type of the function template results in an invalid | |
6757 | type, type deduction fails. [Note: The equivalent substitution in | |
6758 | exception specifications is done only when the function is instanti- | |
6759 | ated, at which point a program is ill-formed if the substitution | |
6760 | results in an invalid type.] */ | |
6761 | ||
6762 | static tree | |
3a978d72 NN |
6763 | tsubst_function_type (tree t, |
6764 | tree args, | |
6765 | tsubst_flags_t complain, | |
6766 | tree in_decl) | |
4393e105 MM |
6767 | { |
6768 | tree return_type; | |
6769 | tree arg_types; | |
6770 | tree fntype; | |
6771 | ||
8dd3f57a | 6772 | /* The TYPE_CONTEXT is not used for function/method types. */ |
50bc768d | 6773 | gcc_assert (TYPE_CONTEXT (t) == NULL_TREE); |
4393e105 | 6774 | |
46c895ac | 6775 | /* Substitute the return type. */ |
4393e105 MM |
6776 | return_type = tsubst (TREE_TYPE (t), args, complain, in_decl); |
6777 | if (return_type == error_mark_node) | |
6778 | return error_mark_node; | |
6e2993bf MM |
6779 | /* The standard does not presently indicate that creation of a |
6780 | function type with an invalid return type is a deduction failure. | |
270af55d | 6781 | However, that is clearly analogous to creating an array of "void" |
6e2993bf MM |
6782 | or a reference to a reference. This is core issue #486. */ |
6783 | if (TREE_CODE (return_type) == ARRAY_TYPE | |
6784 | || TREE_CODE (return_type) == FUNCTION_TYPE) | |
6785 | { | |
6786 | if (complain & tf_error) | |
6787 | { | |
6788 | if (TREE_CODE (return_type) == ARRAY_TYPE) | |
6789 | error ("function returning an array"); | |
6790 | else | |
6791 | error ("function returning a function"); | |
6792 | } | |
6793 | return error_mark_node; | |
6794 | } | |
4393e105 | 6795 | |
34cd5ae7 | 6796 | /* Substitute the argument types. */ |
4393e105 MM |
6797 | arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, |
6798 | complain, in_decl); | |
6799 | if (arg_types == error_mark_node) | |
6800 | return error_mark_node; | |
6801 | ||
6802 | /* Construct a new type node and return it. */ | |
6803 | if (TREE_CODE (t) == FUNCTION_TYPE) | |
6804 | fntype = build_function_type (return_type, arg_types); | |
6805 | else | |
6806 | { | |
6807 | tree r = TREE_TYPE (TREE_VALUE (arg_types)); | |
6808 | if (! IS_AGGR_TYPE (r)) | |
6809 | { | |
6810 | /* [temp.deduct] | |
6811 | ||
6812 | Type deduction may fail for any of the following | |
6813 | reasons: | |
6814 | ||
6815 | -- Attempting to create "pointer to member of T" when T | |
6816 | is not a class type. */ | |
c2ea3a40 | 6817 | if (complain & tf_error) |
0f51ccfc | 6818 | error ("creating pointer to member function of non-class type %qT", |
4393e105 MM |
6819 | r); |
6820 | return error_mark_node; | |
6821 | } | |
6822 | ||
43dc123f MM |
6823 | fntype = build_method_type_directly (r, return_type, |
6824 | TREE_CHAIN (arg_types)); | |
4393e105 | 6825 | } |
c2ea3a40 | 6826 | fntype = cp_build_qualified_type_real (fntype, TYPE_QUALS (t), complain); |
e9525111 | 6827 | fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t)); |
4393e105 MM |
6828 | |
6829 | return fntype; | |
6830 | } | |
6831 | ||
c7222c02 MM |
6832 | /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template |
6833 | ARGS into that specification, and return the substituted | |
6834 | specification. If there is no specification, return NULL_TREE. */ | |
6835 | ||
6836 | static tree | |
6837 | tsubst_exception_specification (tree fntype, | |
6838 | tree args, | |
6839 | tsubst_flags_t complain, | |
6840 | tree in_decl) | |
6841 | { | |
6842 | tree specs; | |
6843 | tree new_specs; | |
6844 | ||
6845 | specs = TYPE_RAISES_EXCEPTIONS (fntype); | |
6846 | new_specs = NULL_TREE; | |
6847 | if (specs) | |
6848 | { | |
6849 | if (! TREE_VALUE (specs)) | |
6850 | new_specs = specs; | |
6851 | else | |
6852 | while (specs) | |
6853 | { | |
6854 | tree spec; | |
6855 | spec = tsubst (TREE_VALUE (specs), args, complain, in_decl); | |
6856 | if (spec == error_mark_node) | |
6857 | return spec; | |
6858 | new_specs = add_exception_specifier (new_specs, spec, complain); | |
6859 | specs = TREE_CHAIN (specs); | |
6860 | } | |
6861 | } | |
6862 | return new_specs; | |
6863 | } | |
6864 | ||
297e73d8 MM |
6865 | /* Substitute into the PARMS of a call-declarator. */ |
6866 | ||
e9659ab0 | 6867 | static tree |
3a978d72 NN |
6868 | tsubst_call_declarator_parms (tree parms, |
6869 | tree args, | |
6870 | tsubst_flags_t complain, | |
6871 | tree in_decl) | |
297e73d8 MM |
6872 | { |
6873 | tree new_parms; | |
6874 | tree type; | |
6875 | tree defarg; | |
6876 | ||
6877 | if (!parms || parms == void_list_node) | |
6878 | return parms; | |
6879 | ||
6880 | new_parms = tsubst_call_declarator_parms (TREE_CHAIN (parms), | |
4393e105 | 6881 | args, complain, in_decl); |
297e73d8 MM |
6882 | |
6883 | /* Figure out the type of this parameter. */ | |
4393e105 | 6884 | type = tsubst (TREE_VALUE (parms), args, complain, in_decl); |
297e73d8 MM |
6885 | |
6886 | /* Figure out the default argument as well. Note that we use | |
4393e105 MM |
6887 | tsubst_expr since the default argument is really an expression. */ |
6888 | defarg = tsubst_expr (TREE_PURPOSE (parms), args, complain, in_decl); | |
297e73d8 MM |
6889 | |
6890 | /* Chain this parameter on to the front of those we have already | |
6891 | processed. We don't use hash_tree_cons because that function | |
6892 | doesn't check TREE_PARMLIST. */ | |
6893 | new_parms = tree_cons (defarg, type, new_parms); | |
6894 | ||
297e73d8 MM |
6895 | return new_parms; |
6896 | } | |
6897 | ||
4393e105 MM |
6898 | /* Take the tree structure T and replace template parameters used |
6899 | therein with the argument vector ARGS. IN_DECL is an associated | |
6900 | decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE. | |
c2ea3a40 NS |
6901 | Issue error and warning messages under control of COMPLAIN. Note |
6902 | that we must be relatively non-tolerant of extensions here, in | |
6903 | order to preserve conformance; if we allow substitutions that | |
6904 | should not be allowed, we may allow argument deductions that should | |
6905 | not succeed, and therefore report ambiguous overload situations | |
6906 | where there are none. In theory, we could allow the substitution, | |
6907 | but indicate that it should have failed, and allow our caller to | |
6908 | make sure that the right thing happens, but we don't try to do this | |
6909 | yet. | |
4393e105 MM |
6910 | |
6911 | This function is used for dealing with types, decls and the like; | |
6912 | for expressions, use tsubst_expr or tsubst_copy. */ | |
ae58fa02 | 6913 | |
14d22dd6 | 6914 | static tree |
3a978d72 | 6915 | tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl) |
ae58fa02 | 6916 | { |
0ecfe0b4 | 6917 | tree type, r; |
ae58fa02 MM |
6918 | |
6919 | if (t == NULL_TREE || t == error_mark_node | |
6920 | || t == integer_type_node | |
6921 | || t == void_type_node | |
6922 | || t == char_type_node | |
539599c1 | 6923 | || t == unknown_type_node |
ae58fa02 MM |
6924 | || TREE_CODE (t) == NAMESPACE_DECL) |
6925 | return t; | |
6926 | ||
fc6a28d7 MM |
6927 | if (DECL_P (t)) |
6928 | return tsubst_decl (t, args, complain); | |
6929 | ||
ae58fa02 MM |
6930 | if (TREE_CODE (t) == IDENTIFIER_NODE) |
6931 | type = IDENTIFIER_TYPE_VALUE (t); | |
6932 | else | |
6933 | type = TREE_TYPE (t); | |
399dedb9 | 6934 | |
50bc768d | 6935 | gcc_assert (type != unknown_type_node); |
ae58fa02 | 6936 | |
fc6a28d7 | 6937 | if (type |
ae58fa02 | 6938 | && TREE_CODE (t) != TYPENAME_TYPE |
4393e105 MM |
6939 | && TREE_CODE (t) != IDENTIFIER_NODE |
6940 | && TREE_CODE (t) != FUNCTION_TYPE | |
6941 | && TREE_CODE (t) != METHOD_TYPE) | |
6942 | type = tsubst (type, args, complain, in_decl); | |
6943 | if (type == error_mark_node) | |
6944 | return error_mark_node; | |
ae58fa02 | 6945 | |
ae58fa02 MM |
6946 | switch (TREE_CODE (t)) |
6947 | { | |
6948 | case RECORD_TYPE: | |
6949 | case UNION_TYPE: | |
6950 | case ENUMERAL_TYPE: | |
4393e105 MM |
6951 | return tsubst_aggr_type (t, args, complain, in_decl, |
6952 | /*entering_scope=*/0); | |
ae58fa02 MM |
6953 | |
6954 | case ERROR_MARK: | |
6955 | case IDENTIFIER_NODE: | |
ae58fa02 MM |
6956 | case VOID_TYPE: |
6957 | case REAL_TYPE: | |
6958 | case COMPLEX_TYPE: | |
c00996a3 | 6959 | case VECTOR_TYPE: |
ae58fa02 MM |
6960 | case BOOLEAN_TYPE: |
6961 | case INTEGER_CST: | |
6962 | case REAL_CST: | |
6963 | case STRING_CST: | |
6964 | return t; | |
6965 | ||
6966 | case INTEGER_TYPE: | |
6967 | if (t == integer_type_node) | |
6968 | return t; | |
6969 | ||
6970 | if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST | |
6971 | && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST) | |
6972 | return t; | |
d2e5ee5c | 6973 | |
5566b478 | 6974 | { |
ddce3528 | 6975 | tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0); |
7ddedda4 | 6976 | |
a91db711 NS |
6977 | /* The array dimension behaves like a non-type template arg, |
6978 | in that we want to fold it as much as possible. */ | |
6979 | max = tsubst_template_arg (omax, args, complain, in_decl); | |
4ef69b83 | 6980 | max = fold_decl_constant_value (max); |
8dd3f57a | 6981 | |
ddce3528 JM |
6982 | if (integer_zerop (omax)) |
6983 | { | |
6984 | /* Still allow an explicit array of size zero. */ | |
6985 | if (pedantic) | |
6986 | pedwarn ("creating array with size zero"); | |
6987 | } | |
07c88314 MM |
6988 | else if (integer_zerop (max) |
6989 | || (TREE_CODE (max) == INTEGER_CST | |
6990 | && INT_CST_LT (max, integer_zero_node))) | |
0ecfe0b4 | 6991 | { |
4393e105 MM |
6992 | /* [temp.deduct] |
6993 | ||
6994 | Type deduction may fail for any of the following | |
6995 | reasons: | |
6996 | ||
bf8f3f93 MM |
6997 | Attempting to create an array with a size that is |
6998 | zero or negative. */ | |
c2ea3a40 | 6999 | if (complain & tf_error) |
0f51ccfc | 7000 | error ("creating array with size zero (%qE)", max); |
4393e105 MM |
7001 | |
7002 | return error_mark_node; | |
0ecfe0b4 JM |
7003 | } |
7004 | ||
c95cd22e | 7005 | return compute_array_index_type (NULL_TREE, max); |
ae58fa02 MM |
7006 | } |
7007 | ||
7008 | case TEMPLATE_TYPE_PARM: | |
7009 | case TEMPLATE_TEMPLATE_PARM: | |
a1281f45 | 7010 | case BOUND_TEMPLATE_TEMPLATE_PARM: |
ae58fa02 MM |
7011 | case TEMPLATE_PARM_INDEX: |
7012 | { | |
7013 | int idx; | |
7014 | int level; | |
7015 | int levels; | |
315fb5db | 7016 | tree arg = NULL_TREE; |
0ecfe0b4 JM |
7017 | |
7018 | r = NULL_TREE; | |
ae58fa02 | 7019 | |
315fb5db | 7020 | gcc_assert (TREE_VEC_LENGTH (args) > 0); |
ae58fa02 | 7021 | if (TREE_CODE (t) == TEMPLATE_TYPE_PARM |
a1281f45 KL |
7022 | || TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM |
7023 | || TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM) | |
ae58fa02 MM |
7024 | { |
7025 | idx = TEMPLATE_TYPE_IDX (t); | |
7026 | level = TEMPLATE_TYPE_LEVEL (t); | |
7027 | } | |
7028 | else | |
7029 | { | |
7030 | idx = TEMPLATE_PARM_IDX (t); | |
7031 | level = TEMPLATE_PARM_LEVEL (t); | |
7032 | } | |
7033 | ||
315fb5db NS |
7034 | levels = TMPL_ARGS_DEPTH (args); |
7035 | if (level <= levels) | |
7036 | arg = TMPL_ARG (args, level, idx); | |
ae58fa02 | 7037 | |
315fb5db NS |
7038 | if (arg == error_mark_node) |
7039 | return error_mark_node; | |
7040 | else if (arg != NULL_TREE) | |
7041 | { | |
7042 | if (TREE_CODE (t) == TEMPLATE_TYPE_PARM) | |
ae58fa02 | 7043 | { |
315fb5db NS |
7044 | gcc_assert (TYPE_P (arg)); |
7045 | return cp_build_qualified_type_real | |
7046 | (arg, cp_type_quals (arg) | cp_type_quals (t), | |
7047 | complain | tf_ignore_bad_quals); | |
7048 | } | |
7049 | else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM) | |
7050 | { | |
7051 | /* We are processing a type constructed from a | |
7052 | template template parameter. */ | |
7053 | tree argvec = tsubst (TYPE_TI_ARGS (t), | |
7054 | args, complain, in_decl); | |
7055 | if (argvec == error_mark_node) | |
7056 | return error_mark_node; | |
4393e105 | 7057 | |
315fb5db NS |
7058 | /* We can get a TEMPLATE_TEMPLATE_PARM here when we |
7059 | are resolving nested-types in the signature of a | |
7060 | member function templates. Otherwise ARG is a | |
7061 | TEMPLATE_DECL and is the real template to be | |
7062 | instantiated. */ | |
7063 | if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM) | |
7064 | arg = TYPE_NAME (arg); | |
7065 | ||
7066 | r = lookup_template_class (arg, | |
7067 | argvec, in_decl, | |
7068 | DECL_CONTEXT (arg), | |
7069 | /*entering_scope=*/0, | |
7070 | complain); | |
7071 | return cp_build_qualified_type_real | |
7072 | (r, TYPE_QUALS (t), complain); | |
ae58fa02 | 7073 | } |
315fb5db NS |
7074 | else |
7075 | /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */ | |
7076 | return arg; | |
ae58fa02 MM |
7077 | } |
7078 | ||
7079 | if (level == 1) | |
7080 | /* This can happen during the attempted tsubst'ing in | |
7081 | unify. This means that we don't yet have any information | |
7082 | about the template parameter in question. */ | |
7083 | return t; | |
7084 | ||
7085 | /* If we get here, we must have been looking at a parm for a | |
7086 | more deeply nested template. Make a new version of this | |
7087 | template parameter, but with a lower level. */ | |
7088 | switch (TREE_CODE (t)) | |
7089 | { | |
7090 | case TEMPLATE_TYPE_PARM: | |
7091 | case TEMPLATE_TEMPLATE_PARM: | |
a1281f45 | 7092 | case BOUND_TEMPLATE_TEMPLATE_PARM: |
89d684bb | 7093 | if (cp_type_quals (t)) |
ae58fa02 | 7094 | { |
9ccf6541 | 7095 | r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl); |
4f2b0fb2 NS |
7096 | r = cp_build_qualified_type_real |
7097 | (r, cp_type_quals (t), | |
7098 | complain | (TREE_CODE (t) == TEMPLATE_TYPE_PARM | |
7099 | ? tf_ignore_bad_quals : 0)); | |
9ccf6541 MM |
7100 | } |
7101 | else | |
7102 | { | |
11e74ea6 | 7103 | r = copy_type (t); |
9ccf6541 MM |
7104 | TEMPLATE_TYPE_PARM_INDEX (r) |
7105 | = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t), | |
7106 | r, levels); | |
7107 | TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r); | |
7108 | TYPE_MAIN_VARIANT (r) = r; | |
7109 | TYPE_POINTER_TO (r) = NULL_TREE; | |
7110 | TYPE_REFERENCE_TO (r) = NULL_TREE; | |
7111 | ||
a1281f45 | 7112 | if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM) |
9ccf6541 MM |
7113 | { |
7114 | tree argvec = tsubst (TYPE_TI_ARGS (t), args, | |
7115 | complain, in_decl); | |
7116 | if (argvec == error_mark_node) | |
7117 | return error_mark_node; | |
4393e105 | 7118 | |
9ccf6541 MM |
7119 | TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r) |
7120 | = tree_cons (TYPE_TI_TEMPLATE (t), argvec, NULL_TREE); | |
7121 | } | |
ae58fa02 MM |
7122 | } |
7123 | break; | |
7124 | ||
7125 | case TEMPLATE_PARM_INDEX: | |
7126 | r = reduce_template_parm_level (t, type, levels); | |
7127 | break; | |
7128 | ||
7129 | default: | |
315fb5db | 7130 | gcc_unreachable (); |
ae58fa02 MM |
7131 | } |
7132 | ||
5566b478 | 7133 | return r; |
ae58fa02 | 7134 | } |
5566b478 | 7135 | |
8d08fdba MS |
7136 | case TREE_LIST: |
7137 | { | |
058b15c1 | 7138 | tree purpose, value, chain; |
8d08fdba MS |
7139 | |
7140 | if (t == void_list_node) | |
7141 | return t; | |
7142 | ||
8d08fdba MS |
7143 | purpose = TREE_PURPOSE (t); |
7144 | if (purpose) | |
4393e105 MM |
7145 | { |
7146 | purpose = tsubst (purpose, args, complain, in_decl); | |
7147 | if (purpose == error_mark_node) | |
7148 | return error_mark_node; | |
7149 | } | |
8d08fdba MS |
7150 | value = TREE_VALUE (t); |
7151 | if (value) | |
4393e105 MM |
7152 | { |
7153 | value = tsubst (value, args, complain, in_decl); | |
7154 | if (value == error_mark_node) | |
7155 | return error_mark_node; | |
7156 | } | |
8d08fdba MS |
7157 | chain = TREE_CHAIN (t); |
7158 | if (chain && chain != void_type_node) | |
4393e105 MM |
7159 | { |
7160 | chain = tsubst (chain, args, complain, in_decl); | |
7161 | if (chain == error_mark_node) | |
7162 | return error_mark_node; | |
7163 | } | |
8d08fdba MS |
7164 | if (purpose == TREE_PURPOSE (t) |
7165 | && value == TREE_VALUE (t) | |
7166 | && chain == TREE_CHAIN (t)) | |
7167 | return t; | |
058b15c1 | 7168 | return hash_tree_cons (purpose, value, chain); |
8d08fdba | 7169 | } |
95b4aca6 NS |
7170 | |
7171 | case TREE_BINFO: | |
bd7eccc4 | 7172 | /* We should never be tsubsting a binfo. */ |
315fb5db | 7173 | gcc_unreachable (); |
85b71cf2 | 7174 | |
95b4aca6 NS |
7175 | case TREE_VEC: |
7176 | /* A vector of template arguments. */ | |
50bc768d | 7177 | gcc_assert (!type); |
a91db711 | 7178 | return tsubst_template_args (t, args, complain, in_decl); |
8d08fdba | 7179 | |
8d08fdba MS |
7180 | case POINTER_TYPE: |
7181 | case REFERENCE_TYPE: | |
7182 | { | |
8d08fdba | 7183 | enum tree_code code; |
79a7c7fa | 7184 | |
46c895ac | 7185 | if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE) |
8d08fdba MS |
7186 | return t; |
7187 | ||
7188 | code = TREE_CODE (t); | |
4393e105 MM |
7189 | |
7190 | ||
7191 | /* [temp.deduct] | |
7192 | ||
7193 | Type deduction may fail for any of the following | |
7194 | reasons: | |
7195 | ||
7196 | -- Attempting to create a pointer to reference type. | |
7197 | -- Attempting to create a reference to a reference type or | |
7198 | a reference to void. */ | |
0ecfe0b4 JM |
7199 | if (TREE_CODE (type) == REFERENCE_TYPE |
7200 | || (code == REFERENCE_TYPE && TREE_CODE (type) == VOID_TYPE)) | |
79a7c7fa | 7201 | { |
82a98427 | 7202 | static location_t last_loc; |
79a7c7fa JM |
7203 | |
7204 | /* We keep track of the last time we issued this error | |
7205 | message to avoid spewing a ton of messages during a | |
7206 | single bad template instantiation. */ | |
c2ea3a40 | 7207 | if (complain & tf_error |
93409b8c | 7208 | #ifdef USE_MAPPED_LOCATION |
9b60dfd7 | 7209 | && last_loc != input_location |
93409b8c | 7210 | #else |
82a98427 | 7211 | && (last_loc.line != input_line |
9b60dfd7 | 7212 | || last_loc.file != input_filename) |
93409b8c | 7213 | #endif |
9b60dfd7 | 7214 | ) |
79a7c7fa | 7215 | { |
0ecfe0b4 | 7216 | if (TREE_CODE (type) == VOID_TYPE) |
33bd39a2 | 7217 | error ("forming reference to void"); |
0ecfe0b4 | 7218 | else |
0f51ccfc GDR |
7219 | error ("forming %s to reference type %qT", |
7220 | (code == POINTER_TYPE) ? "pointer" : "reference", | |
7221 | type); | |
82a98427 | 7222 | last_loc = input_location; |
79a7c7fa JM |
7223 | } |
7224 | ||
4393e105 | 7225 | return error_mark_node; |
79a7c7fa JM |
7226 | } |
7227 | else if (code == POINTER_TYPE) | |
46c895ac NS |
7228 | { |
7229 | r = build_pointer_type (type); | |
7230 | if (TREE_CODE (type) == METHOD_TYPE) | |
7231 | r = build_ptrmemfunc_type (r); | |
7232 | } | |
8d08fdba MS |
7233 | else |
7234 | r = build_reference_type (type); | |
adecb3f4 | 7235 | r = cp_build_qualified_type_real (r, TYPE_QUALS (t), complain); |
79a7c7fa | 7236 | |
a5f1c5f8 NS |
7237 | if (r != error_mark_node) |
7238 | /* Will this ever be needed for TYPE_..._TO values? */ | |
7239 | layout_type (r); | |
7240 | ||
8d08fdba MS |
7241 | return r; |
7242 | } | |
a4443a08 | 7243 | case OFFSET_TYPE: |
0ecfe0b4 | 7244 | { |
4393e105 MM |
7245 | r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl); |
7246 | if (r == error_mark_node || !IS_AGGR_TYPE (r)) | |
7247 | { | |
7248 | /* [temp.deduct] | |
7249 | ||
7250 | Type deduction may fail for any of the following | |
7251 | reasons: | |
7252 | ||
7253 | -- Attempting to create "pointer to member of T" when T | |
7254 | is not a class type. */ | |
c2ea3a40 | 7255 | if (complain & tf_error) |
0f51ccfc | 7256 | error ("creating pointer to member of non-class type %qT", r); |
4393e105 MM |
7257 | return error_mark_node; |
7258 | } | |
46c895ac NS |
7259 | if (TREE_CODE (type) == REFERENCE_TYPE) |
7260 | { | |
4f09be91 | 7261 | if (complain & tf_error) |
0f51ccfc | 7262 | error ("creating pointer to member reference type %qT", type); |
46c895ac NS |
7263 | |
7264 | return error_mark_node; | |
7265 | } | |
50bc768d | 7266 | gcc_assert (TREE_CODE (type) != METHOD_TYPE); |
46c895ac | 7267 | if (TREE_CODE (type) == FUNCTION_TYPE) |
a5ac359a MM |
7268 | { |
7269 | /* This is really a method type. The cv qualifiers of the | |
7270 | this pointer should _not_ be determined by the cv | |
7271 | qualifiers of the class type. They should be held | |
7272 | somewhere in the FUNCTION_TYPE, but we don't do that at | |
7273 | the moment. Consider | |
7274 | typedef void (Func) () const; | |
7275 | ||
7276 | template <typename T1> void Foo (Func T1::*); | |
7277 | ||
7278 | */ | |
7279 | tree method_type; | |
7280 | ||
43dc123f MM |
7281 | method_type = build_method_type_directly (TYPE_MAIN_VARIANT (r), |
7282 | TREE_TYPE (type), | |
7283 | TYPE_ARG_TYPES (type)); | |
a5ac359a MM |
7284 | return build_ptrmemfunc_type (build_pointer_type (method_type)); |
7285 | } | |
46c895ac | 7286 | else |
b7a78333 MM |
7287 | return cp_build_qualified_type_real (build_ptrmem_type (r, type), |
7288 | TYPE_QUALS (t), | |
7289 | complain); | |
0ecfe0b4 | 7290 | } |
8d08fdba MS |
7291 | case FUNCTION_TYPE: |
7292 | case METHOD_TYPE: | |
7293 | { | |
c11b6f21 | 7294 | tree fntype; |
c7222c02 | 7295 | tree specs; |
4393e105 MM |
7296 | fntype = tsubst_function_type (t, args, complain, in_decl); |
7297 | if (fntype == error_mark_node) | |
7298 | return error_mark_node; | |
cabc336a | 7299 | |
34cd5ae7 | 7300 | /* Substitute the exception specification. */ |
c7222c02 MM |
7301 | specs = tsubst_exception_specification (t, args, complain, |
7302 | in_decl); | |
7303 | if (specs) | |
7304 | fntype = build_exception_variant (fntype, specs); | |
c11b6f21 | 7305 | return fntype; |
8d08fdba MS |
7306 | } |
7307 | case ARRAY_TYPE: | |
7308 | { | |
4393e105 MM |
7309 | tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl); |
7310 | if (domain == error_mark_node) | |
7311 | return error_mark_node; | |
7312 | ||
7313 | /* As an optimization, we avoid regenerating the array type if | |
7314 | it will obviously be the same as T. */ | |
8d08fdba MS |
7315 | if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t)) |
7316 | return t; | |
0ecfe0b4 | 7317 | |
4393e105 MM |
7318 | /* These checks should match the ones in grokdeclarator. |
7319 | ||
7320 | [temp.deduct] | |
7321 | ||
7322 | The deduction may fail for any of the following reasons: | |
7323 | ||
7324 | -- Attempting to create an array with an element type that | |
cfb91b67 GB |
7325 | is void, a function type, or a reference type, or [DR337] |
7326 | an abstract class type. */ | |
4393e105 MM |
7327 | if (TREE_CODE (type) == VOID_TYPE |
7328 | || TREE_CODE (type) == FUNCTION_TYPE | |
7329 | || TREE_CODE (type) == REFERENCE_TYPE) | |
0ecfe0b4 | 7330 | { |
c2ea3a40 | 7331 | if (complain & tf_error) |
0f51ccfc | 7332 | error ("creating array of %qT", type); |
4393e105 | 7333 | return error_mark_node; |
0ecfe0b4 | 7334 | } |
cfb91b67 GB |
7335 | if (CLASS_TYPE_P (type) && CLASSTYPE_PURE_VIRTUALS (type)) |
7336 | { | |
7337 | if (complain & tf_error) | |
0f51ccfc | 7338 | error ("creating array of %qT, which is an abstract class type", |
cfb91b67 GB |
7339 | type); |
7340 | return error_mark_node; | |
7341 | } | |
0ecfe0b4 | 7342 | |
8d08fdba MS |
7343 | r = build_cplus_array_type (type, domain); |
7344 | return r; | |
7345 | } | |
7346 | ||
8d08fdba | 7347 | case PLUS_EXPR: |
5566b478 | 7348 | case MINUS_EXPR: |
4393e105 | 7349 | { |
c2ea3a40 NS |
7350 | tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl); |
7351 | tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl); | |
4393e105 MM |
7352 | |
7353 | if (e1 == error_mark_node || e2 == error_mark_node) | |
7354 | return error_mark_node; | |
7355 | ||
f293ce4b | 7356 | return fold (build2 (TREE_CODE (t), TREE_TYPE (t), e1, e2)); |
4393e105 | 7357 | } |
8d08fdba MS |
7358 | |
7359 | case NEGATE_EXPR: | |
7360 | case NOP_EXPR: | |
4393e105 | 7361 | { |
c2ea3a40 | 7362 | tree e = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl); |
4393e105 MM |
7363 | if (e == error_mark_node) |
7364 | return error_mark_node; | |
7365 | ||
f293ce4b | 7366 | return fold (build1 (TREE_CODE (t), TREE_TYPE (t), e)); |
4393e105 | 7367 | } |
8d08fdba | 7368 | |
5566b478 MS |
7369 | case TYPENAME_TYPE: |
7370 | { | |
4393e105 MM |
7371 | tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain, |
7372 | in_decl, /*entering_scope=*/1); | |
7373 | tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args, | |
7374 | complain, in_decl); | |
7375 | ||
7376 | if (ctx == error_mark_node || f == error_mark_node) | |
7377 | return error_mark_node; | |
ae58fa02 | 7378 | |
bf8f3f93 MM |
7379 | if (!IS_AGGR_TYPE (ctx)) |
7380 | { | |
c2ea3a40 | 7381 | if (complain & tf_error) |
0f51ccfc | 7382 | error ("%qT is not a class, struct, or union type", ctx); |
bf8f3f93 MM |
7383 | return error_mark_node; |
7384 | } | |
7385 | else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx)) | |
7386 | { | |
7387 | /* Normally, make_typename_type does not require that the CTX | |
7388 | have complete type in order to allow things like: | |
ae58fa02 | 7389 | |
bf8f3f93 | 7390 | template <class T> struct S { typename S<T>::X Y; }; |
ae58fa02 | 7391 | |
bf8f3f93 MM |
7392 | But, such constructs have already been resolved by this |
7393 | point, so here CTX really should have complete type, unless | |
7394 | it's a partial instantiation. */ | |
4393e105 | 7395 | ctx = complete_type (ctx); |
d0f062fb | 7396 | if (!COMPLETE_TYPE_P (ctx)) |
4393e105 | 7397 | { |
c2ea3a40 | 7398 | if (complain & tf_error) |
7a228918 | 7399 | cxx_incomplete_type_error (NULL_TREE, ctx); |
4393e105 MM |
7400 | return error_mark_node; |
7401 | } | |
7402 | } | |
ae58fa02 | 7403 | |
fc6a28d7 | 7404 | f = make_typename_type (ctx, f, typename_type, |
4f2b0fb2 | 7405 | (complain & tf_error) | tf_keep_type_decl); |
f0bcd168 MM |
7406 | if (f == error_mark_node) |
7407 | return f; | |
4f2b0fb2 NS |
7408 | if (TREE_CODE (f) == TYPE_DECL) |
7409 | { | |
7410 | complain |= tf_ignore_bad_quals; | |
7411 | f = TREE_TYPE (f); | |
7412 | } | |
7413 | ||
fc6a28d7 MM |
7414 | if (TREE_CODE (f) != TYPENAME_TYPE) |
7415 | { | |
7416 | if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE) | |
7417 | error ("%qT resolves to %qT, which is not an enumeration type", | |
7418 | t, f); | |
7419 | else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f)) | |
7420 | error ("%qT resolves to %qT, which is is not a class type", | |
7421 | t, f); | |
7422 | } | |
7423 | ||
4f2b0fb2 NS |
7424 | return cp_build_qualified_type_real |
7425 | (f, cp_type_quals (f) | cp_type_quals (t), complain); | |
5566b478 | 7426 | } |
c2ea3a40 | 7427 | |
b8c6534b KL |
7428 | case UNBOUND_CLASS_TEMPLATE: |
7429 | { | |
7430 | tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain, | |
7431 | in_decl, /*entering_scope=*/1); | |
7432 | tree name = TYPE_IDENTIFIER (t); | |
b939a023 | 7433 | tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t)); |
b8c6534b KL |
7434 | |
7435 | if (ctx == error_mark_node || name == error_mark_node) | |
7436 | return error_mark_node; | |
7437 | ||
b939a023 KL |
7438 | if (parm_list) |
7439 | parm_list = tsubst_template_parms (parm_list, args, complain); | |
7440 | return make_unbound_class_template (ctx, name, parm_list, complain); | |
b8c6534b KL |
7441 | } |
7442 | ||
5566b478 | 7443 | case INDIRECT_REF: |
5566b478 | 7444 | case ADDR_EXPR: |
058b15c1 | 7445 | case CALL_EXPR: |
315fb5db | 7446 | gcc_unreachable (); |
5566b478 MS |
7447 | |
7448 | case ARRAY_REF: | |
4393e105 | 7449 | { |
c2ea3a40 NS |
7450 | tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl); |
7451 | tree e2 = tsubst_expr (TREE_OPERAND (t, 1), args, complain, in_decl); | |
4393e105 MM |
7452 | if (e1 == error_mark_node || e2 == error_mark_node) |
7453 | return error_mark_node; | |
7454 | ||
44de5aeb | 7455 | return build_nt (ARRAY_REF, e1, e2, NULL_TREE, NULL_TREE); |
4393e105 | 7456 | } |
5566b478 | 7457 | |
fc378698 | 7458 | case SCOPE_REF: |
4393e105 | 7459 | { |
c2ea3a40 | 7460 | tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl); |
4393e105 MM |
7461 | tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl); |
7462 | if (e1 == error_mark_node || e2 == error_mark_node) | |
7463 | return error_mark_node; | |
7464 | ||
718b8ea5 | 7465 | return build_nt (TREE_CODE (t), e1, e2); |
4393e105 | 7466 | } |
fc378698 | 7467 | |
b894fc05 | 7468 | case TYPEOF_TYPE: |
4393e105 | 7469 | { |
b830b74c | 7470 | tree type; |
4393e105 | 7471 | |
eb34af89 RK |
7472 | type = finish_typeof (tsubst_expr (TYPEOF_TYPE_EXPR (t), args, |
7473 | complain, in_decl)); | |
b830b74c | 7474 | return cp_build_qualified_type_real (type, |
dce50630 | 7475 | cp_type_quals (t) |
b830b74c | 7476 | | cp_type_quals (type), |
dce50630 | 7477 | complain); |
4393e105 | 7478 | } |
b894fc05 | 7479 | |
8d08fdba | 7480 | default: |
9e637a26 | 7481 | sorry ("use of %qs in template", |
8d08fdba MS |
7482 | tree_code_name [(int) TREE_CODE (t)]); |
7483 | return error_mark_node; | |
7484 | } | |
7485 | } | |
7486 | ||
ee76b931 MM |
7487 | /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the |
7488 | type of the expression on the left-hand side of the "." or "->" | |
7489 | operator. */ | |
7490 | ||
7491 | static tree | |
7492 | tsubst_baselink (tree baselink, tree object_type, | |
7493 | tree args, tsubst_flags_t complain, tree in_decl) | |
7494 | { | |
7495 | tree name; | |
7496 | tree qualifying_scope; | |
7497 | tree fns; | |
7498 | tree template_args = 0; | |
7499 | bool template_id_p = false; | |
7500 | ||
7501 | /* A baselink indicates a function from a base class. The | |
7502 | BASELINK_ACCESS_BINFO and BASELINK_BINFO are going to have | |
7503 | non-dependent types; otherwise, the lookup could not have | |
7504 | succeeded. However, they may indicate bases of the template | |
7505 | class, rather than the instantiated class. | |
7506 | ||
7507 | In addition, lookups that were not ambiguous before may be | |
04c06002 | 7508 | ambiguous now. Therefore, we perform the lookup again. */ |
ee76b931 MM |
7509 | qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink)); |
7510 | fns = BASELINK_FUNCTIONS (baselink); | |
7511 | if (TREE_CODE (fns) == TEMPLATE_ID_EXPR) | |
7512 | { | |
7513 | template_id_p = true; | |
7514 | template_args = TREE_OPERAND (fns, 1); | |
7515 | fns = TREE_OPERAND (fns, 0); | |
bf12d54d NS |
7516 | if (template_args) |
7517 | template_args = tsubst_template_args (template_args, args, | |
7518 | complain, in_decl); | |
ee76b931 MM |
7519 | } |
7520 | name = DECL_NAME (get_first_fn (fns)); | |
7521 | baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1); | |
9b60dfd7 MM |
7522 | |
7523 | /* If lookup found a single function, mark it as used at this | |
7524 | point. (If it lookup found multiple functions the one selected | |
7525 | later by overload resolution will be marked as used at that | |
7526 | point.) */ | |
7527 | if (BASELINK_P (baselink)) | |
7528 | fns = BASELINK_FUNCTIONS (baselink); | |
7529 | if (!template_id_p && !really_overloaded_fn (fns)) | |
7530 | mark_used (OVL_CURRENT (fns)); | |
7531 | ||
7532 | /* Add back the template arguments, if present. */ | |
ee76b931 MM |
7533 | if (BASELINK_P (baselink) && template_id_p) |
7534 | BASELINK_FUNCTIONS (baselink) | |
7535 | = build_nt (TEMPLATE_ID_EXPR, | |
7536 | BASELINK_FUNCTIONS (baselink), | |
7537 | template_args); | |
9b60dfd7 | 7538 | |
ee76b931 MM |
7539 | if (!object_type) |
7540 | object_type = current_class_type; | |
7541 | return adjust_result_of_qualified_name_lookup (baselink, | |
7542 | qualifying_scope, | |
7543 | object_type); | |
7544 | } | |
7545 | ||
7546 | /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is | |
7547 | true if the qualified-id will be a postfix-expression in-and-of | |
7548 | itself; false if more of the postfix-expression follows the | |
7549 | QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand | |
7550 | of "&". */ | |
7551 | ||
7552 | static tree | |
7553 | tsubst_qualified_id (tree qualified_id, tree args, | |
7554 | tsubst_flags_t complain, tree in_decl, | |
7555 | bool done, bool address_p) | |
7556 | { | |
7557 | tree expr; | |
7558 | tree scope; | |
7559 | tree name; | |
7560 | bool is_template; | |
7561 | tree template_args; | |
7562 | ||
50bc768d | 7563 | gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF); |
ee76b931 | 7564 | |
ee76b931 MM |
7565 | /* Figure out what name to look up. */ |
7566 | name = TREE_OPERAND (qualified_id, 1); | |
7567 | if (TREE_CODE (name) == TEMPLATE_ID_EXPR) | |
7568 | { | |
7569 | is_template = true; | |
bf12d54d NS |
7570 | template_args = TREE_OPERAND (name, 1); |
7571 | if (template_args) | |
7572 | template_args = tsubst_template_args (template_args, args, | |
7573 | complain, in_decl); | |
ee76b931 MM |
7574 | name = TREE_OPERAND (name, 0); |
7575 | } | |
7576 | else | |
7577 | { | |
7578 | is_template = false; | |
7579 | template_args = NULL_TREE; | |
7580 | } | |
7581 | ||
6eeba0cc MM |
7582 | /* Substitute into the qualifying scope. When there are no ARGS, we |
7583 | are just trying to simplify a non-dependent expression. In that | |
7584 | case the qualifying scope may be dependent, and, in any case, | |
7585 | substituting will not help. */ | |
7586 | scope = TREE_OPERAND (qualified_id, 0); | |
7587 | if (args) | |
7588 | { | |
7589 | scope = tsubst (scope, args, complain, in_decl); | |
7590 | expr = tsubst_copy (name, args, complain, in_decl); | |
7591 | } | |
7592 | else | |
7593 | expr = name; | |
10b1d5e7 | 7594 | |
ab73670a MM |
7595 | if (dependent_type_p (scope)) |
7596 | return build_nt (SCOPE_REF, scope, expr); | |
fb5ce3c9 | 7597 | |
5e08432e | 7598 | if (!BASELINK_P (name) && !DECL_P (expr)) |
12483c9f NS |
7599 | { |
7600 | expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false); | |
7601 | if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL | |
7602 | ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL) | |
7603 | { | |
7604 | if (complain & tf_error) | |
b2693faf | 7605 | { |
0f51ccfc | 7606 | error ("dependent-name %qE is parsed as a non-type, but " |
b2693faf | 7607 | "instantiation yields a type", qualified_id); |
0f51ccfc | 7608 | inform ("say %<typename %E%> if a type is meant", qualified_id); |
b2693faf | 7609 | } |
12483c9f NS |
7610 | return error_mark_node; |
7611 | } | |
7612 | } | |
fb5ce3c9 | 7613 | |
279b8466 | 7614 | if (DECL_P (expr)) |
8f78f01f MM |
7615 | { |
7616 | check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE, | |
7617 | scope); | |
7618 | /* Remember that there was a reference to this entity. */ | |
7619 | mark_used (expr); | |
7620 | } | |
7621 | ||
7622 | if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST) | |
7623 | { | |
7624 | if (complain & tf_error) | |
7625 | qualified_name_lookup_error (scope, | |
7626 | TREE_OPERAND (qualified_id, 1), | |
7627 | expr); | |
7628 | return error_mark_node; | |
7629 | } | |
ee76b931 MM |
7630 | |
7631 | if (is_template) | |
10b1d5e7 | 7632 | expr = lookup_template_function (expr, template_args); |
ee76b931 | 7633 | |
22038b2c | 7634 | if (expr == error_mark_node && complain & tf_error) |
8f78f01f MM |
7635 | qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1), |
7636 | expr); | |
22038b2c | 7637 | else if (TYPE_P (scope)) |
ee76b931 MM |
7638 | { |
7639 | expr = (adjust_result_of_qualified_name_lookup | |
7640 | (expr, scope, current_class_type)); | |
7641 | expr = finish_qualified_id_expr (scope, expr, done, address_p); | |
7642 | } | |
5aa56f9d NS |
7643 | |
7644 | expr = convert_from_reference (expr); | |
ee76b931 MM |
7645 | |
7646 | return expr; | |
7647 | } | |
7648 | ||
00d3396f JM |
7649 | /* Like tsubst, but deals with expressions. This function just replaces |
7650 | template parms; to finish processing the resultant expression, use | |
7651 | tsubst_expr. */ | |
7652 | ||
14d22dd6 | 7653 | static tree |
3a978d72 | 7654 | tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl) |
5566b478 MS |
7655 | { |
7656 | enum tree_code code; | |
8452b1d3 | 7657 | tree r; |
8d08fdba | 7658 | |
5566b478 MS |
7659 | if (t == NULL_TREE || t == error_mark_node) |
7660 | return t; | |
7661 | ||
7662 | code = TREE_CODE (t); | |
b7484fbe | 7663 | |
5566b478 MS |
7664 | switch (code) |
7665 | { | |
7666 | case PARM_DECL: | |
a723baf1 | 7667 | r = retrieve_local_specialization (t); |
50bc768d | 7668 | gcc_assert (r != NULL); |
c0694c4b | 7669 | mark_used (r); |
a723baf1 | 7670 | return r; |
5566b478 MS |
7671 | |
7672 | case CONST_DECL: | |
ed44da02 MM |
7673 | { |
7674 | tree enum_type; | |
7675 | tree v; | |
7676 | ||
a723baf1 MM |
7677 | if (DECL_TEMPLATE_PARM_P (t)) |
7678 | return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl); | |
7b6d72fc MM |
7679 | /* There is no need to substitute into namespace-scope |
7680 | enumerators. */ | |
7681 | if (DECL_NAMESPACE_SCOPE_P (t)) | |
ed44da02 | 7682 | return t; |
d5a10cf0 MM |
7683 | /* If ARGS is NULL, then T is known to be non-dependent. */ |
7684 | if (args == NULL_TREE) | |
8a784e4a | 7685 | return integral_constant_value (t); |
ed44da02 MM |
7686 | |
7687 | /* Unfortunately, we cannot just call lookup_name here. | |
9188c363 MM |
7688 | Consider: |
7689 | ||
7690 | template <int I> int f() { | |
7691 | enum E { a = I }; | |
7692 | struct S { void g() { E e = a; } }; | |
7693 | }; | |
7694 | ||
7695 | When we instantiate f<7>::S::g(), say, lookup_name is not | |
7696 | clever enough to find f<7>::a. */ | |
ed44da02 | 7697 | enum_type |
4393e105 | 7698 | = tsubst_aggr_type (TREE_TYPE (t), args, complain, in_decl, |
ed44da02 MM |
7699 | /*entering_scope=*/0); |
7700 | ||
7701 | for (v = TYPE_VALUES (enum_type); | |
7702 | v != NULL_TREE; | |
7703 | v = TREE_CHAIN (v)) | |
7704 | if (TREE_PURPOSE (v) == DECL_NAME (t)) | |
7705 | return TREE_VALUE (v); | |
7706 | ||
7707 | /* We didn't find the name. That should never happen; if | |
7708 | name-lookup found it during preliminary parsing, we | |
7709 | should find it again here during instantiation. */ | |
315fb5db | 7710 | gcc_unreachable (); |
ed44da02 | 7711 | } |
db04386f | 7712 | return t; |
ed44da02 | 7713 | |
5566b478 MS |
7714 | case FIELD_DECL: |
7715 | if (DECL_CONTEXT (t)) | |
7716 | { | |
0978790f | 7717 | tree ctx; |
0978790f | 7718 | |
4393e105 | 7719 | ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl, |
36a117a5 | 7720 | /*entering_scope=*/1); |
0978790f | 7721 | if (ctx != DECL_CONTEXT (t)) |
bad1f462 KL |
7722 | { |
7723 | tree r = lookup_field (ctx, DECL_NAME (t), 0, false); | |
7724 | if (!r) | |
7725 | { | |
7726 | if (complain & tf_error) | |
7727 | error ("using invalid field %qD", t); | |
7728 | return error_mark_node; | |
7729 | } | |
7730 | return r; | |
7731 | } | |
5566b478 | 7732 | } |
fa743e8c | 7733 | |
5566b478 MS |
7734 | return t; |
7735 | ||
7736 | case VAR_DECL: | |
7737 | case FUNCTION_DECL: | |
a723baf1 MM |
7738 | if ((DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)) |
7739 | || local_variable_p (t)) | |
4393e105 | 7740 | t = tsubst (t, args, complain, in_decl); |
5566b478 MS |
7741 | mark_used (t); |
7742 | return t; | |
7743 | ||
a723baf1 | 7744 | case BASELINK: |
ee76b931 | 7745 | return tsubst_baselink (t, current_class_type, args, complain, in_decl); |
a723baf1 | 7746 | |
98c1c668 | 7747 | case TEMPLATE_DECL: |
a723baf1 MM |
7748 | if (DECL_TEMPLATE_TEMPLATE_PARM_P (t)) |
7749 | return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)), | |
7750 | args, complain, in_decl); | |
c7222c02 | 7751 | else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t)) |
4393e105 | 7752 | return tsubst (t, args, complain, in_decl); |
fcea7401 KL |
7753 | else if (DECL_CLASS_SCOPE_P (t) |
7754 | && uses_template_parms (DECL_CONTEXT (t))) | |
7755 | { | |
7756 | /* Template template argument like the following example need | |
7757 | special treatment: | |
7758 | ||
7759 | template <template <class> class TT> struct C {}; | |
7760 | template <class T> struct D { | |
7761 | template <class U> struct E {}; | |
7762 | C<E> c; // #1 | |
7763 | }; | |
7764 | D<int> d; // #2 | |
7765 | ||
7766 | We are processing the template argument `E' in #1 for | |
7767 | the template instantiation #2. Originally, `E' is a | |
7768 | TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we | |
7769 | have to substitute this with one having context `D<int>'. */ | |
7770 | ||
7771 | tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl); | |
7772 | return lookup_field (context, DECL_NAME(t), 0, false); | |
7773 | } | |
98c1c668 | 7774 | else |
fcea7401 | 7775 | /* Ordinary template template argument. */ |
98c1c668 JM |
7776 | return t; |
7777 | ||
5566b478 MS |
7778 | case CAST_EXPR: |
7779 | case REINTERPRET_CAST_EXPR: | |
e92cc029 MS |
7780 | case CONST_CAST_EXPR: |
7781 | case STATIC_CAST_EXPR: | |
7782 | case DYNAMIC_CAST_EXPR: | |
51924768 | 7783 | case NOP_EXPR: |
5566b478 | 7784 | return build1 |
4393e105 MM |
7785 | (code, tsubst (TREE_TYPE (t), args, complain, in_decl), |
7786 | tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl)); | |
5566b478 MS |
7787 | |
7788 | case INDIRECT_REF: | |
5566b478 MS |
7789 | case NEGATE_EXPR: |
7790 | case TRUTH_NOT_EXPR: | |
b87692e5 | 7791 | case BIT_NOT_EXPR: |
5566b478 MS |
7792 | case ADDR_EXPR: |
7793 | case CONVERT_EXPR: /* Unary + */ | |
7794 | case SIZEOF_EXPR: | |
abff8e06 | 7795 | case ALIGNOF_EXPR: |
5566b478 | 7796 | case ARROW_EXPR: |
fc378698 | 7797 | case THROW_EXPR: |
5156628f | 7798 | case TYPEID_EXPR: |
f5733617 SS |
7799 | case REALPART_EXPR: |
7800 | case IMAGPART_EXPR: | |
5566b478 | 7801 | return build1 |
6a629cac | 7802 | (code, tsubst (TREE_TYPE (t), args, complain, in_decl), |
4393e105 | 7803 | tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl)); |
5566b478 | 7804 | |
a723baf1 MM |
7805 | case COMPONENT_REF: |
7806 | { | |
7807 | tree object; | |
7808 | tree name; | |
7809 | ||
7810 | object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl); | |
7811 | name = TREE_OPERAND (t, 1); | |
7812 | if (TREE_CODE (name) == BIT_NOT_EXPR) | |
7813 | { | |
7814 | name = tsubst_copy (TREE_OPERAND (name, 0), args, | |
7815 | complain, in_decl); | |
7816 | name = build1 (BIT_NOT_EXPR, NULL_TREE, name); | |
7817 | } | |
7818 | else if (TREE_CODE (name) == SCOPE_REF | |
7819 | && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR) | |
7820 | { | |
7821 | tree base = tsubst_copy (TREE_OPERAND (name, 0), args, | |
7822 | complain, in_decl); | |
7823 | name = TREE_OPERAND (name, 1); | |
7824 | name = tsubst_copy (TREE_OPERAND (name, 0), args, | |
7825 | complain, in_decl); | |
7826 | name = build1 (BIT_NOT_EXPR, NULL_TREE, name); | |
7827 | name = build_nt (SCOPE_REF, base, name); | |
7828 | } | |
ee76b931 MM |
7829 | else if (TREE_CODE (name) == BASELINK) |
7830 | name = tsubst_baselink (name, | |
7831 | non_reference (TREE_TYPE (object)), | |
7832 | args, complain, | |
7833 | in_decl); | |
a723baf1 | 7834 | else |
ee76b931 | 7835 | name = tsubst_copy (name, args, complain, in_decl); |
44de5aeb | 7836 | return build_nt (COMPONENT_REF, object, name, NULL_TREE); |
a723baf1 MM |
7837 | } |
7838 | ||
5566b478 MS |
7839 | case PLUS_EXPR: |
7840 | case MINUS_EXPR: | |
7841 | case MULT_EXPR: | |
7842 | case TRUNC_DIV_EXPR: | |
7843 | case CEIL_DIV_EXPR: | |
7844 | case FLOOR_DIV_EXPR: | |
7845 | case ROUND_DIV_EXPR: | |
7846 | case EXACT_DIV_EXPR: | |
7847 | case BIT_AND_EXPR: | |
5566b478 MS |
7848 | case BIT_IOR_EXPR: |
7849 | case BIT_XOR_EXPR: | |
7850 | case TRUNC_MOD_EXPR: | |
7851 | case FLOOR_MOD_EXPR: | |
7852 | case TRUTH_ANDIF_EXPR: | |
7853 | case TRUTH_ORIF_EXPR: | |
7854 | case TRUTH_AND_EXPR: | |
7855 | case TRUTH_OR_EXPR: | |
7856 | case RSHIFT_EXPR: | |
7857 | case LSHIFT_EXPR: | |
7858 | case RROTATE_EXPR: | |
7859 | case LROTATE_EXPR: | |
7860 | case EQ_EXPR: | |
7861 | case NE_EXPR: | |
7862 | case MAX_EXPR: | |
7863 | case MIN_EXPR: | |
7864 | case LE_EXPR: | |
7865 | case GE_EXPR: | |
7866 | case LT_EXPR: | |
7867 | case GT_EXPR: | |
5566b478 MS |
7868 | case COMPOUND_EXPR: |
7869 | case SCOPE_REF: | |
7870 | case DOTSTAR_EXPR: | |
7871 | case MEMBER_REF: | |
519c9806 MM |
7872 | case PREDECREMENT_EXPR: |
7873 | case PREINCREMENT_EXPR: | |
7874 | case POSTDECREMENT_EXPR: | |
7875 | case POSTINCREMENT_EXPR: | |
5566b478 | 7876 | return build_nt |
4393e105 MM |
7877 | (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl), |
7878 | tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl)); | |
5566b478 | 7879 | |
d8987adb NS |
7880 | case ARRAY_REF: |
7881 | return build_nt | |
7882 | (ARRAY_REF, | |
7883 | tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl), | |
7884 | tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl), | |
7885 | NULL_TREE, NULL_TREE); | |
7886 | ||
5566b478 | 7887 | case CALL_EXPR: |
a723baf1 MM |
7888 | return build_nt (code, |
7889 | tsubst_copy (TREE_OPERAND (t, 0), args, | |
7890 | complain, in_decl), | |
7891 | tsubst_copy (TREE_OPERAND (t, 1), args, complain, | |
7892 | in_decl), | |
7893 | NULL_TREE); | |
5566b478 | 7894 | |
5566b478 MS |
7895 | case COND_EXPR: |
7896 | case MODOP_EXPR: | |
40242ccf | 7897 | case PSEUDO_DTOR_EXPR: |
67da3287 | 7898 | { |
8452b1d3 | 7899 | r = build_nt |
4393e105 MM |
7900 | (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl), |
7901 | tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl), | |
7902 | tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl)); | |
e4c2c34b | 7903 | TREE_NO_WARNING (r) = TREE_NO_WARNING (t); |
67da3287 MM |
7904 | return r; |
7905 | } | |
5566b478 MS |
7906 | |
7907 | case NEW_EXPR: | |
7908 | { | |
8452b1d3 | 7909 | r = build_nt |
4393e105 MM |
7910 | (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl), |
7911 | tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl), | |
7912 | tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl)); | |
5566b478 MS |
7913 | NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t); |
7914 | return r; | |
7915 | } | |
7916 | ||
7917 | case DELETE_EXPR: | |
7918 | { | |
8452b1d3 | 7919 | r = build_nt |
4393e105 MM |
7920 | (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl), |
7921 | tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl)); | |
5566b478 MS |
7922 | DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t); |
7923 | DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t); | |
7924 | return r; | |
7925 | } | |
7926 | ||
386b8a85 JM |
7927 | case TEMPLATE_ID_EXPR: |
7928 | { | |
00d3396f | 7929 | /* Substituted template arguments */ |
a91db711 NS |
7930 | tree fn = TREE_OPERAND (t, 0); |
7931 | tree targs = TREE_OPERAND (t, 1); | |
856216bb | 7932 | |
a91db711 | 7933 | fn = tsubst_copy (fn, args, complain, in_decl); |
bf12d54d NS |
7934 | if (targs) |
7935 | targs = tsubst_template_args (targs, args, complain, in_decl); | |
a91db711 NS |
7936 | |
7937 | return lookup_template_function (fn, targs); | |
386b8a85 JM |
7938 | } |
7939 | ||
5566b478 MS |
7940 | case TREE_LIST: |
7941 | { | |
7942 | tree purpose, value, chain; | |
7943 | ||
7944 | if (t == void_list_node) | |
7945 | return t; | |
7946 | ||
7947 | purpose = TREE_PURPOSE (t); | |
7948 | if (purpose) | |
4393e105 | 7949 | purpose = tsubst_copy (purpose, args, complain, in_decl); |
5566b478 MS |
7950 | value = TREE_VALUE (t); |
7951 | if (value) | |
4393e105 | 7952 | value = tsubst_copy (value, args, complain, in_decl); |
5566b478 MS |
7953 | chain = TREE_CHAIN (t); |
7954 | if (chain && chain != void_type_node) | |
4393e105 | 7955 | chain = tsubst_copy (chain, args, complain, in_decl); |
5566b478 MS |
7956 | if (purpose == TREE_PURPOSE (t) |
7957 | && value == TREE_VALUE (t) | |
7958 | && chain == TREE_CHAIN (t)) | |
7959 | return t; | |
7960 | return tree_cons (purpose, value, chain); | |
7961 | } | |
7962 | ||
7963 | case RECORD_TYPE: | |
7964 | case UNION_TYPE: | |
7965 | case ENUMERAL_TYPE: | |
7966 | case INTEGER_TYPE: | |
7967 | case TEMPLATE_TYPE_PARM: | |
73b0fce8 | 7968 | case TEMPLATE_TEMPLATE_PARM: |
a1281f45 | 7969 | case BOUND_TEMPLATE_TEMPLATE_PARM: |
f84b4be9 | 7970 | case TEMPLATE_PARM_INDEX: |
5566b478 MS |
7971 | case POINTER_TYPE: |
7972 | case REFERENCE_TYPE: | |
7973 | case OFFSET_TYPE: | |
7974 | case FUNCTION_TYPE: | |
7975 | case METHOD_TYPE: | |
7976 | case ARRAY_TYPE: | |
7977 | case TYPENAME_TYPE: | |
b8c6534b | 7978 | case UNBOUND_CLASS_TEMPLATE: |
2a2b1d56 | 7979 | case TYPEOF_TYPE: |
f84b4be9 | 7980 | case TYPE_DECL: |
4393e105 | 7981 | return tsubst (t, args, complain, in_decl); |
5566b478 | 7982 | |
e92cc029 | 7983 | case IDENTIFIER_NODE: |
421844e7 | 7984 | if (IDENTIFIER_TYPENAME_P (t)) |
1f6e1acc AS |
7985 | { |
7986 | tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl); | |
1f84ec23 | 7987 | return mangle_conv_op_name_for_type (new_type); |
1f6e1acc | 7988 | } |
e92cc029 MS |
7989 | else |
7990 | return t; | |
7991 | ||
5156628f | 7992 | case CONSTRUCTOR: |
8452b1d3 | 7993 | { |
dcf92453 ZW |
7994 | r = build_constructor |
7995 | (tsubst (TREE_TYPE (t), args, complain, in_decl), | |
7996 | tsubst_copy (CONSTRUCTOR_ELTS (t), args, complain, in_decl)); | |
8452b1d3 JM |
7997 | TREE_HAS_CONSTRUCTOR (r) = TREE_HAS_CONSTRUCTOR (t); |
7998 | return r; | |
7999 | } | |
5156628f | 8000 | |
371534a9 | 8001 | case VA_ARG_EXPR: |
ea333e1c | 8002 | return build_x_va_arg (tsubst_copy (TREE_OPERAND (t, 0), args, complain, |
c2ea3a40 NS |
8003 | in_decl), |
8004 | tsubst (TREE_TYPE (t), args, complain, in_decl)); | |
f9817201 | 8005 | |
543a0daa RH |
8006 | case CLEANUP_POINT_EXPR: |
8007 | /* We shouldn't have built any of these during initial template | |
8008 | generation. Instead, they should be built during instantiation | |
8009 | in response to the saved STMT_IS_FULL_EXPR_P setting. */ | |
315fb5db | 8010 | gcc_unreachable (); |
543a0daa | 8011 | |
5566b478 MS |
8012 | default: |
8013 | return t; | |
8014 | } | |
8015 | } | |
8016 | ||
cc23546e JO |
8017 | /* Like tsubst_copy for expressions, etc. but also does semantic |
8018 | processing. */ | |
00d3396f | 8019 | |
14d22dd6 | 8020 | static tree |
3a978d72 | 8021 | tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl) |
5566b478 | 8022 | { |
fd10dd09 | 8023 | tree stmt, tmp; |
558475f0 | 8024 | |
5566b478 MS |
8025 | if (t == NULL_TREE || t == error_mark_node) |
8026 | return t; | |
8027 | ||
93409b8c PB |
8028 | if (EXPR_HAS_LOCATION (t)) |
8029 | input_location = EXPR_LOCATION (t); | |
7c34ced1 RH |
8030 | if (STATEMENT_CODE_P (TREE_CODE (t))) |
8031 | current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t); | |
8032 | ||
5566b478 | 8033 | switch (TREE_CODE (t)) |
8d08fdba | 8034 | { |
325c3691 RH |
8035 | case STATEMENT_LIST: |
8036 | { | |
8037 | tree_stmt_iterator i; | |
8038 | for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i)) | |
8039 | tsubst_expr (tsi_stmt (i), args, complain, in_decl); | |
8040 | break; | |
8041 | } | |
8042 | ||
558475f0 | 8043 | case CTOR_INITIALIZER: |
2282d28d MM |
8044 | finish_mem_initializers (tsubst_initializer_list |
8045 | (TREE_OPERAND (t, 0), args)); | |
8046 | break; | |
558475f0 | 8047 | |
5088b058 RH |
8048 | case RETURN_EXPR: |
8049 | finish_return_stmt (tsubst_expr (TREE_OPERAND (t, 0), | |
4393e105 | 8050 | args, complain, in_decl)); |
5566b478 MS |
8051 | break; |
8052 | ||
8053 | case EXPR_STMT: | |
7c34ced1 RH |
8054 | tmp = tsubst_expr (EXPR_STMT_EXPR (t), args, complain, in_decl); |
8055 | if (EXPR_STMT_STMT_EXPR_RESULT (t)) | |
8056 | finish_stmt_expr_expr (tmp, cur_stmt_expr); | |
8057 | else | |
8058 | finish_expr_stmt (tmp); | |
8059 | break; | |
5566b478 | 8060 | |
9da99f7d | 8061 | case USING_STMT: |
9da99f7d NS |
8062 | do_using_directive (tsubst_expr (USING_STMT_NAMESPACE (t), |
8063 | args, complain, in_decl)); | |
8064 | break; | |
8065 | ||
350fae66 | 8066 | case DECL_EXPR: |
5566b478 | 8067 | { |
e0942dcd MM |
8068 | tree decl; |
8069 | tree init; | |
5566b478 | 8070 | |
350fae66 | 8071 | decl = DECL_EXPR_DECL (t); |
acef433b MM |
8072 | if (TREE_CODE (decl) == LABEL_DECL) |
8073 | finish_label_decl (DECL_NAME (decl)); | |
fbfe8c9e NS |
8074 | else if (TREE_CODE (decl) == USING_DECL) |
8075 | { | |
8076 | tree scope = DECL_INITIAL (decl); | |
8077 | tree name = DECL_NAME (decl); | |
22038b2c | 8078 | tree decl; |
fbfe8c9e NS |
8079 | |
8080 | scope = tsubst_expr (scope, args, complain, in_decl); | |
22038b2c | 8081 | decl = lookup_qualified_name (scope, name, |
12483c9f NS |
8082 | /*is_type_p=*/false, |
8083 | /*complain=*/false); | |
8f78f01f MM |
8084 | if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST) |
8085 | qualified_name_lookup_error (scope, name, decl); | |
22038b2c | 8086 | else |
ed5f054f | 8087 | do_local_using_decl (decl, scope, name); |
fbfe8c9e | 8088 | } |
acef433b MM |
8089 | else |
8090 | { | |
8091 | init = DECL_INITIAL (decl); | |
8092 | decl = tsubst (decl, args, complain, in_decl); | |
ce2e5191 NS |
8093 | if (decl != error_mark_node) |
8094 | { | |
8095 | if (init) | |
8096 | DECL_INITIAL (decl) = error_mark_node; | |
8097 | /* By marking the declaration as instantiated, we avoid | |
8098 | trying to instantiate it. Since instantiate_decl can't | |
8099 | handle local variables, and since we've already done | |
8100 | all that needs to be done, that's the right thing to | |
8101 | do. */ | |
8102 | if (TREE_CODE (decl) == VAR_DECL) | |
8103 | DECL_TEMPLATE_INSTANTIATED (decl) = 1; | |
edebf865 MM |
8104 | if (TREE_CODE (decl) == VAR_DECL |
8105 | && ANON_AGGR_TYPE_P (TREE_TYPE (decl))) | |
8106 | /* Anonymous aggregates are a special case. */ | |
8107 | finish_anon_union (decl); | |
8108 | else | |
ed2fa432 | 8109 | { |
edebf865 | 8110 | maybe_push_decl (decl); |
39703eb9 MM |
8111 | if (TREE_CODE (decl) == VAR_DECL |
8112 | && DECL_PRETTY_FUNCTION_P (decl)) | |
edebf865 MM |
8113 | { |
8114 | /* For __PRETTY_FUNCTION__ we have to adjust the | |
8115 | initializer. */ | |
8116 | const char *const name | |
8117 | = cxx_printable_name (current_function_decl, 2); | |
bb885938 | 8118 | init = cp_fname_init (name, &TREE_TYPE (decl)); |
edebf865 MM |
8119 | } |
8120 | else | |
8121 | init = tsubst_expr (init, args, complain, in_decl); | |
8122 | cp_finish_decl (decl, init, NULL_TREE, 0); | |
ed2fa432 | 8123 | } |
ce2e5191 | 8124 | } |
acef433b | 8125 | } |
fd10dd09 | 8126 | |
350fae66 | 8127 | /* A DECL_EXPR can also be used as an expression, in the condition |
325c3691 RH |
8128 | clause of an if/for/while construct. */ |
8129 | return decl; | |
5566b478 | 8130 | } |
8d08fdba | 8131 | |
5566b478 | 8132 | case FOR_STMT: |
7c34ced1 RH |
8133 | stmt = begin_for_stmt (); |
8134 | tsubst_expr (FOR_INIT_STMT (t), args, complain, in_decl); | |
8135 | finish_for_init_stmt (stmt); | |
8136 | tmp = tsubst_expr (FOR_COND (t), args, complain, in_decl); | |
8137 | finish_for_cond (tmp, stmt); | |
8138 | tmp = tsubst_expr (FOR_EXPR (t), args, complain, in_decl); | |
8139 | finish_for_expr (tmp, stmt); | |
8140 | tsubst_expr (FOR_BODY (t), args, complain, in_decl); | |
8141 | finish_for_stmt (stmt); | |
5566b478 | 8142 | break; |
8d08fdba | 8143 | |
5566b478 | 8144 | case WHILE_STMT: |
7c34ced1 RH |
8145 | stmt = begin_while_stmt (); |
8146 | tmp = tsubst_expr (WHILE_COND (t), args, complain, in_decl); | |
8147 | finish_while_stmt_cond (tmp, stmt); | |
8148 | tsubst_expr (WHILE_BODY (t), args, complain, in_decl); | |
8149 | finish_while_stmt (stmt); | |
5566b478 | 8150 | break; |
8d08fdba | 8151 | |
5566b478 | 8152 | case DO_STMT: |
7c34ced1 RH |
8153 | stmt = begin_do_stmt (); |
8154 | tsubst_expr (DO_BODY (t), args, complain, in_decl); | |
8155 | finish_do_body (stmt); | |
8156 | tmp = tsubst_expr (DO_COND (t), args, complain, in_decl); | |
8157 | finish_do_stmt (tmp, stmt); | |
5566b478 | 8158 | break; |
a0a33927 | 8159 | |
5566b478 | 8160 | case IF_STMT: |
7c34ced1 RH |
8161 | stmt = begin_if_stmt (); |
8162 | tmp = tsubst_expr (IF_COND (t), args, complain, in_decl); | |
8163 | finish_if_stmt_cond (tmp, stmt); | |
8164 | tsubst_expr (THEN_CLAUSE (t), args, complain, in_decl); | |
8165 | finish_then_clause (stmt); | |
8d08fdba | 8166 | |
7c34ced1 RH |
8167 | if (ELSE_CLAUSE (t)) |
8168 | { | |
8169 | begin_else_clause (stmt); | |
8170 | tsubst_expr (ELSE_CLAUSE (t), args, complain, in_decl); | |
8171 | finish_else_clause (stmt); | |
8172 | } | |
8173 | ||
8174 | finish_if_stmt (stmt); | |
5566b478 | 8175 | break; |
8d08fdba | 8176 | |
5882f0f3 | 8177 | case BIND_EXPR: |
7c34ced1 RH |
8178 | if (BIND_EXPR_BODY_BLOCK (t)) |
8179 | stmt = begin_function_body (); | |
8180 | else | |
8181 | stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t) | |
8182 | ? BCS_TRY_BLOCK : 0); | |
ade3dc07 | 8183 | |
7c34ced1 | 8184 | tsubst_expr (BIND_EXPR_BODY (t), args, complain, in_decl); |
ade3dc07 | 8185 | |
7c34ced1 RH |
8186 | if (BIND_EXPR_BODY_BLOCK (t)) |
8187 | finish_function_body (stmt); | |
8188 | else | |
8189 | finish_compound_stmt (stmt); | |
5566b478 | 8190 | break; |
8d08fdba | 8191 | |
5566b478 | 8192 | case BREAK_STMT: |
ad321293 | 8193 | finish_break_stmt (); |
5566b478 | 8194 | break; |
8d08fdba | 8195 | |
6467930b | 8196 | case CONTINUE_STMT: |
ad321293 | 8197 | finish_continue_stmt (); |
6467930b MS |
8198 | break; |
8199 | ||
5566b478 | 8200 | case SWITCH_STMT: |
7c34ced1 | 8201 | stmt = begin_switch_stmt (); |
ebaae582 | 8202 | tmp = tsubst_expr (SWITCH_STMT_COND (t), args, complain, in_decl); |
7c34ced1 | 8203 | finish_switch_cond (tmp, stmt); |
ebaae582 | 8204 | tsubst_expr (SWITCH_STMT_BODY (t), args, complain, in_decl); |
7c34ced1 | 8205 | finish_switch_stmt (stmt); |
5566b478 MS |
8206 | break; |
8207 | ||
8c161995 | 8208 | case CASE_LABEL_EXPR: |
4393e105 | 8209 | finish_case_label (tsubst_expr (CASE_LOW (t), args, complain, in_decl), |
56cb9733 MM |
8210 | tsubst_expr (CASE_HIGH (t), args, complain, |
8211 | in_decl)); | |
5566b478 MS |
8212 | break; |
8213 | ||
9e14e18f | 8214 | case LABEL_EXPR: |
9e14e18f | 8215 | finish_label_stmt (DECL_NAME (LABEL_EXPR_LABEL (t))); |
5566b478 MS |
8216 | break; |
8217 | ||
9e14e18f | 8218 | case GOTO_EXPR: |
fd10dd09 JM |
8219 | tmp = GOTO_DESTINATION (t); |
8220 | if (TREE_CODE (tmp) != LABEL_DECL) | |
aa09da44 MM |
8221 | /* Computed goto's must be tsubst'd into. On the other hand, |
8222 | non-computed gotos must not be; the identifier in question | |
8223 | will have no binding. */ | |
fd10dd09 | 8224 | tmp = tsubst_expr (tmp, args, complain, in_decl); |
3fa56191 | 8225 | else |
fd10dd09 JM |
8226 | tmp = DECL_NAME (tmp); |
8227 | finish_goto_stmt (tmp); | |
ad321293 MM |
8228 | break; |
8229 | ||
e130a54b | 8230 | case ASM_EXPR: |
c87978aa | 8231 | tmp = finish_asm_stmt |
6de9cd9a | 8232 | (ASM_VOLATILE_P (t), |
c87978aa JM |
8233 | tsubst_expr (ASM_STRING (t), args, complain, in_decl), |
8234 | tsubst_expr (ASM_OUTPUTS (t), args, complain, in_decl), | |
8235 | tsubst_expr (ASM_INPUTS (t), args, complain, in_decl), | |
8236 | tsubst_expr (ASM_CLOBBERS (t), args, complain, in_decl)); | |
131263fa AP |
8237 | { |
8238 | tree asm_expr = tmp; | |
8239 | if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR) | |
8240 | asm_expr = TREE_OPERAND (asm_expr, 0); | |
8241 | ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t); | |
8242 | } | |
5566b478 | 8243 | break; |
faf5394a MS |
8244 | |
8245 | case TRY_BLOCK: | |
f1dedc31 | 8246 | if (CLEANUP_P (t)) |
62409b39 | 8247 | { |
57b52417 | 8248 | stmt = begin_try_block (); |
62409b39 MM |
8249 | tsubst_expr (TRY_STMTS (t), args, complain, in_decl); |
8250 | finish_cleanup_try_block (stmt); | |
8251 | finish_cleanup (tsubst_expr (TRY_HANDLERS (t), args, | |
8252 | complain, in_decl), | |
8253 | stmt); | |
8254 | } | |
f1dedc31 MM |
8255 | else |
8256 | { | |
62409b39 MM |
8257 | if (FN_TRY_BLOCK_P (t)) |
8258 | stmt = begin_function_try_block (); | |
8259 | else | |
8260 | stmt = begin_try_block (); | |
8261 | ||
8262 | tsubst_expr (TRY_STMTS (t), args, complain, in_decl); | |
8263 | ||
8264 | if (FN_TRY_BLOCK_P (t)) | |
8265 | finish_function_try_block (stmt); | |
8266 | else | |
8267 | finish_try_block (stmt); | |
8268 | ||
fd10dd09 | 8269 | tsubst_expr (TRY_HANDLERS (t), args, complain, in_decl); |
84df082b MM |
8270 | if (FN_TRY_BLOCK_P (t)) |
8271 | finish_function_handler_sequence (stmt); | |
8272 | else | |
8273 | finish_handler_sequence (stmt); | |
f1dedc31 | 8274 | } |
faf5394a | 8275 | break; |
b35d4555 | 8276 | |
faf5394a | 8277 | case HANDLER: |
b35d4555 MM |
8278 | { |
8279 | tree decl; | |
b35d4555 | 8280 | |
b35d4555 MM |
8281 | stmt = begin_handler (); |
8282 | if (HANDLER_PARMS (t)) | |
8283 | { | |
325c3691 | 8284 | decl = HANDLER_PARMS (t); |
b35d4555 | 8285 | decl = tsubst (decl, args, complain, in_decl); |
f8191e64 MM |
8286 | /* Prevent instantiate_decl from trying to instantiate |
8287 | this variable. We've already done all that needs to be | |
8288 | done. */ | |
8289 | DECL_TEMPLATE_INSTANTIATED (decl) = 1; | |
b35d4555 MM |
8290 | } |
8291 | else | |
8292 | decl = NULL_TREE; | |
1a6025b4 | 8293 | finish_handler_parms (decl, stmt); |
b35d4555 | 8294 | tsubst_expr (HANDLER_BODY (t), args, complain, in_decl); |
1a6025b4 | 8295 | finish_handler (stmt); |
b35d4555 | 8296 | } |
faf5394a MS |
8297 | break; |
8298 | ||
b87692e5 | 8299 | case TAG_DEFN: |
fd10dd09 | 8300 | tsubst (TREE_TYPE (t), args, complain, NULL_TREE); |
b87692e5 MS |
8301 | break; |
8302 | ||
5566b478 | 8303 | default: |
315fb5db NS |
8304 | gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t))); |
8305 | ||
8306 | return tsubst_copy_and_build (t, args, complain, in_decl, | |
8307 | /*function_p=*/false); | |
5566b478 | 8308 | } |
fd10dd09 | 8309 | |
325c3691 | 8310 | return NULL_TREE; |
8d08fdba MS |
8311 | } |
8312 | ||
b3445994 MM |
8313 | /* T is a postfix-expression that is not being used in a function |
8314 | call. Return the substituted version of T. */ | |
8315 | ||
8316 | static tree | |
8317 | tsubst_non_call_postfix_expression (tree t, tree args, | |
8318 | tsubst_flags_t complain, | |
8319 | tree in_decl) | |
8320 | { | |
8321 | if (TREE_CODE (t) == SCOPE_REF) | |
8322 | t = tsubst_qualified_id (t, args, complain, in_decl, | |
8323 | /*done=*/false, /*address_p=*/false); | |
8324 | else | |
8325 | t = tsubst_copy_and_build (t, args, complain, in_decl, | |
8326 | /*function_p=*/false); | |
8327 | ||
8328 | return t; | |
8329 | } | |
8330 | ||
cc23546e | 8331 | /* Like tsubst but deals with expressions and performs semantic |
b3445994 | 8332 | analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */ |
cc23546e | 8333 | |
ee76b931 | 8334 | tree |
3a978d72 NN |
8335 | tsubst_copy_and_build (tree t, |
8336 | tree args, | |
8337 | tsubst_flags_t complain, | |
b3445994 MM |
8338 | tree in_decl, |
8339 | bool function_p) | |
cc23546e | 8340 | { |
b3445994 MM |
8341 | #define RECUR(NODE) \ |
8342 | tsubst_copy_and_build (NODE, args, complain, in_decl, /*function_p=*/false) | |
8343 | ||
ee76b931 MM |
8344 | tree op1; |
8345 | ||
cc23546e JO |
8346 | if (t == NULL_TREE || t == error_mark_node) |
8347 | return t; | |
8348 | ||
8349 | switch (TREE_CODE (t)) | |
8350 | { | |
399dedb9 NS |
8351 | case USING_DECL: |
8352 | t = DECL_NAME (t); | |
852dcbdd | 8353 | /* Fall through. */ |
b3445994 | 8354 | case IDENTIFIER_NODE: |
cc23546e | 8355 | { |
b3445994 | 8356 | tree decl; |
b3445994 MM |
8357 | cp_id_kind idk; |
8358 | tree qualifying_class; | |
67c03833 | 8359 | bool non_integral_constant_expression_p; |
b3445994 MM |
8360 | const char *error_msg; |
8361 | ||
b3445994 | 8362 | if (IDENTIFIER_TYPENAME_P (t)) |
cc23546e | 8363 | { |
b3445994 MM |
8364 | tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl); |
8365 | t = mangle_conv_op_name_for_type (new_type); | |
cc23546e | 8366 | } |
b3445994 MM |
8367 | |
8368 | /* Look up the name. */ | |
10b1d5e7 | 8369 | decl = lookup_name (t, 0); |
b3445994 MM |
8370 | |
8371 | /* By convention, expressions use ERROR_MARK_NODE to indicate | |
8372 | failure, not NULL_TREE. */ | |
8373 | if (decl == NULL_TREE) | |
8374 | decl = error_mark_node; | |
8375 | ||
10b1d5e7 | 8376 | decl = finish_id_expression (t, decl, NULL_TREE, |
b3445994 MM |
8377 | &idk, |
8378 | &qualifying_class, | |
67c03833 JM |
8379 | /*integral_constant_expression_p=*/false, |
8380 | /*allow_non_integral_constant_expression_p=*/false, | |
8381 | &non_integral_constant_expression_p, | |
b3445994 MM |
8382 | &error_msg); |
8383 | if (error_msg) | |
8384 | error (error_msg); | |
8385 | if (!function_p && TREE_CODE (decl) == IDENTIFIER_NODE) | |
8386 | decl = unqualified_name_lookup_error (decl); | |
8387 | return decl; | |
cc23546e JO |
8388 | } |
8389 | ||
8390 | case TEMPLATE_ID_EXPR: | |
8391 | { | |
8392 | tree object; | |
b3445994 | 8393 | tree template = RECUR (TREE_OPERAND (t, 0)); |
bf12d54d NS |
8394 | tree targs = TREE_OPERAND (t, 1); |
8395 | ||
8396 | if (targs) | |
8397 | targs = tsubst_template_args (targs, args, complain, in_decl); | |
cc23546e JO |
8398 | |
8399 | if (TREE_CODE (template) == COMPONENT_REF) | |
8400 | { | |
8401 | object = TREE_OPERAND (template, 0); | |
8402 | template = TREE_OPERAND (template, 1); | |
8403 | } | |
8404 | else | |
8405 | object = NULL_TREE; | |
bd83b409 | 8406 | template = lookup_template_function (template, targs); |
cc23546e JO |
8407 | |
8408 | if (object) | |
f293ce4b RS |
8409 | return build3 (COMPONENT_REF, TREE_TYPE (template), |
8410 | object, template, NULL_TREE); | |
cc23546e JO |
8411 | else |
8412 | return template; | |
8413 | } | |
8414 | ||
8415 | case INDIRECT_REF: | |
db24eb1f NS |
8416 | { |
8417 | tree r = RECUR (TREE_OPERAND (t, 0)); | |
8418 | ||
8419 | if (REFERENCE_REF_P (t)) | |
8420 | { | |
e8c66fe0 NS |
8421 | /* A type conversion to reference type will be enclosed in |
8422 | such an indirect ref, but the substitution of the cast | |
8423 | will have also added such an indirect ref. */ | |
8424 | if (TREE_CODE (TREE_TYPE (r)) == REFERENCE_TYPE) | |
8425 | r = convert_from_reference (r); | |
db24eb1f NS |
8426 | } |
8427 | else | |
8428 | r = build_x_indirect_ref (r, "unary *"); | |
8429 | return r; | |
8430 | } | |
cc23546e | 8431 | |
0da99d4e GB |
8432 | case NOP_EXPR: |
8433 | return build_nop | |
8434 | (tsubst (TREE_TYPE (t), args, complain, in_decl), | |
8435 | RECUR (TREE_OPERAND (t, 0))); | |
8436 | ||
cc23546e JO |
8437 | case CAST_EXPR: |
8438 | return build_functional_cast | |
8439 | (tsubst (TREE_TYPE (t), args, complain, in_decl), | |
b3445994 | 8440 | RECUR (TREE_OPERAND (t, 0))); |
cc23546e JO |
8441 | |
8442 | case REINTERPRET_CAST_EXPR: | |
8443 | return build_reinterpret_cast | |
8444 | (tsubst (TREE_TYPE (t), args, complain, in_decl), | |
b3445994 | 8445 | RECUR (TREE_OPERAND (t, 0))); |
cc23546e JO |
8446 | |
8447 | case CONST_CAST_EXPR: | |
8448 | return build_const_cast | |
8449 | (tsubst (TREE_TYPE (t), args, complain, in_decl), | |
b3445994 | 8450 | RECUR (TREE_OPERAND (t, 0))); |
cc23546e JO |
8451 | |
8452 | case DYNAMIC_CAST_EXPR: | |
8453 | return build_dynamic_cast | |
8454 | (tsubst (TREE_TYPE (t), args, complain, in_decl), | |
b3445994 | 8455 | RECUR (TREE_OPERAND (t, 0))); |
cc23546e JO |
8456 | |
8457 | case STATIC_CAST_EXPR: | |
8458 | return build_static_cast | |
8459 | (tsubst (TREE_TYPE (t), args, complain, in_decl), | |
b3445994 | 8460 | RECUR (TREE_OPERAND (t, 0))); |
cc23546e | 8461 | |
cc23546e JO |
8462 | case POSTDECREMENT_EXPR: |
8463 | case POSTINCREMENT_EXPR: | |
b3445994 MM |
8464 | op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0), |
8465 | args, complain, in_decl); | |
ee76b931 MM |
8466 | return build_x_unary_op (TREE_CODE (t), op1); |
8467 | ||
8468 | case PREDECREMENT_EXPR: | |
8469 | case PREINCREMENT_EXPR: | |
cc23546e JO |
8470 | case NEGATE_EXPR: |
8471 | case BIT_NOT_EXPR: | |
cc23546e | 8472 | case ABS_EXPR: |
d17811fd MM |
8473 | case TRUTH_NOT_EXPR: |
8474 | case CONVERT_EXPR: /* Unary + */ | |
8475 | case REALPART_EXPR: | |
8476 | case IMAGPART_EXPR: | |
b3445994 | 8477 | return build_x_unary_op (TREE_CODE (t), RECUR (TREE_OPERAND (t, 0))); |
cc23546e | 8478 | |
cc23546e | 8479 | case ADDR_EXPR: |
ee76b931 MM |
8480 | op1 = TREE_OPERAND (t, 0); |
8481 | if (TREE_CODE (op1) == SCOPE_REF) | |
8482 | op1 = tsubst_qualified_id (op1, args, complain, in_decl, | |
8483 | /*done=*/true, /*address_p=*/true); | |
8484 | else | |
b3445994 MM |
8485 | op1 = tsubst_non_call_postfix_expression (op1, args, complain, |
8486 | in_decl); | |
fc2b8477 MM |
8487 | if (TREE_CODE (op1) == LABEL_DECL) |
8488 | return finish_label_address_expr (DECL_NAME (op1)); | |
ee76b931 MM |
8489 | return build_x_unary_op (ADDR_EXPR, op1); |
8490 | ||
cc23546e JO |
8491 | case PLUS_EXPR: |
8492 | case MINUS_EXPR: | |
8493 | case MULT_EXPR: | |
8494 | case TRUNC_DIV_EXPR: | |
8495 | case CEIL_DIV_EXPR: | |
8496 | case FLOOR_DIV_EXPR: | |
8497 | case ROUND_DIV_EXPR: | |
8498 | case EXACT_DIV_EXPR: | |
8499 | case BIT_AND_EXPR: | |
cc23546e JO |
8500 | case BIT_IOR_EXPR: |
8501 | case BIT_XOR_EXPR: | |
8502 | case TRUNC_MOD_EXPR: | |
8503 | case FLOOR_MOD_EXPR: | |
8504 | case TRUTH_ANDIF_EXPR: | |
8505 | case TRUTH_ORIF_EXPR: | |
8506 | case TRUTH_AND_EXPR: | |
8507 | case TRUTH_OR_EXPR: | |
8508 | case RSHIFT_EXPR: | |
8509 | case LSHIFT_EXPR: | |
8510 | case RROTATE_EXPR: | |
8511 | case LROTATE_EXPR: | |
8512 | case EQ_EXPR: | |
8513 | case NE_EXPR: | |
8514 | case MAX_EXPR: | |
8515 | case MIN_EXPR: | |
8516 | case LE_EXPR: | |
8517 | case GE_EXPR: | |
8518 | case LT_EXPR: | |
8519 | case GT_EXPR: | |
8520 | case MEMBER_REF: | |
b3445994 | 8521 | case DOTSTAR_EXPR: |
cc23546e JO |
8522 | return build_x_binary_op |
8523 | (TREE_CODE (t), | |
b3445994 | 8524 | RECUR (TREE_OPERAND (t, 0)), |
ec835fb2 MM |
8525 | RECUR (TREE_OPERAND (t, 1)), |
8526 | /*overloaded_p=*/NULL); | |
cc23546e JO |
8527 | |
8528 | case SCOPE_REF: | |
ee76b931 MM |
8529 | return tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true, |
8530 | /*address_p=*/false); | |
cc23546e | 8531 | case ARRAY_REF: |
b3445994 MM |
8532 | op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0), |
8533 | args, complain, in_decl); | |
d8987adb NS |
8534 | return build_x_binary_op (ARRAY_REF, op1, RECUR (TREE_OPERAND (t, 1)), |
8535 | /*overloaded_p=*/NULL); | |
8536 | ||
cc23546e JO |
8537 | case SIZEOF_EXPR: |
8538 | case ALIGNOF_EXPR: | |
d17811fd MM |
8539 | op1 = TREE_OPERAND (t, 0); |
8540 | if (!args) | |
8541 | { | |
8542 | /* When there are no ARGS, we are trying to evaluate a | |
8543 | non-dependent expression from the parser. Trying to do | |
8544 | the substitutions may not work. */ | |
8545 | if (!TYPE_P (op1)) | |
8546 | op1 = TREE_TYPE (op1); | |
8547 | } | |
8548 | else | |
8549 | { | |
8550 | ++skip_evaluation; | |
b3445994 | 8551 | op1 = RECUR (op1); |
d17811fd MM |
8552 | --skip_evaluation; |
8553 | } | |
7a18b933 NS |
8554 | if (TYPE_P (op1)) |
8555 | return cxx_sizeof_or_alignof_type (op1, TREE_CODE (t), true); | |
d17811fd | 8556 | else |
7a18b933 | 8557 | return cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t)); |
cc23546e JO |
8558 | |
8559 | case MODOP_EXPR: | |
e4c2c34b JM |
8560 | { |
8561 | tree r = build_x_modify_expr | |
8562 | (RECUR (TREE_OPERAND (t, 0)), | |
8563 | TREE_CODE (TREE_OPERAND (t, 1)), | |
8564 | RECUR (TREE_OPERAND (t, 2))); | |
bcf9a914 JM |
8565 | /* TREE_NO_WARNING must be set if either the expression was |
8566 | parenthesized or it uses an operator such as >>= rather | |
8567 | than plain assignment. In the former case, it was already | |
8568 | set and must be copied. In the latter case, | |
8569 | build_x_modify_expr sets it and it must not be reset | |
8570 | here. */ | |
8571 | if (TREE_NO_WARNING (t)) | |
8572 | TREE_NO_WARNING (r) = TREE_NO_WARNING (t); | |
e4c2c34b JM |
8573 | return r; |
8574 | } | |
cc23546e JO |
8575 | |
8576 | case ARROW_EXPR: | |
b3445994 MM |
8577 | op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0), |
8578 | args, complain, in_decl); | |
ee76b931 MM |
8579 | /* Remember that there was a reference to this entity. */ |
8580 | if (DECL_P (op1)) | |
8581 | mark_used (op1); | |
8582 | return build_x_arrow (op1); | |
cc23546e JO |
8583 | |
8584 | case NEW_EXPR: | |
8585 | return build_new | |
b3445994 MM |
8586 | (RECUR (TREE_OPERAND (t, 0)), |
8587 | RECUR (TREE_OPERAND (t, 1)), | |
8588 | RECUR (TREE_OPERAND (t, 2)), | |
058b15c1 | 8589 | RECUR (TREE_OPERAND (t, 3)), |
cc23546e JO |
8590 | NEW_EXPR_USE_GLOBAL (t)); |
8591 | ||
8592 | case DELETE_EXPR: | |
8593 | return delete_sanity | |
b3445994 MM |
8594 | (RECUR (TREE_OPERAND (t, 0)), |
8595 | RECUR (TREE_OPERAND (t, 1)), | |
cc23546e JO |
8596 | DELETE_EXPR_USE_VEC (t), |
8597 | DELETE_EXPR_USE_GLOBAL (t)); | |
8598 | ||
8599 | case COMPOUND_EXPR: | |
b3445994 MM |
8600 | return build_x_compound_expr (RECUR (TREE_OPERAND (t, 0)), |
8601 | RECUR (TREE_OPERAND (t, 1))); | |
cc23546e | 8602 | |
ee76b931 | 8603 | case CALL_EXPR: |
cc23546e | 8604 | { |
ee76b931 MM |
8605 | tree function; |
8606 | tree call_args; | |
ee76b931 | 8607 | bool qualified_p; |
ee935db4 | 8608 | bool koenig_p; |
ee76b931 MM |
8609 | |
8610 | function = TREE_OPERAND (t, 0); | |
6d80c4b9 MM |
8611 | /* When we parsed the expression, we determined whether or |
8612 | not Koenig lookup should be performed. */ | |
8613 | koenig_p = KOENIG_LOOKUP_P (t); | |
ee76b931 | 8614 | if (TREE_CODE (function) == SCOPE_REF) |
cc23546e | 8615 | { |
ee76b931 MM |
8616 | qualified_p = true; |
8617 | function = tsubst_qualified_id (function, args, complain, in_decl, | |
8618 | /*done=*/false, | |
8619 | /*address_p=*/false); | |
cc23546e | 8620 | } |
ee76b931 | 8621 | else |
cc23546e | 8622 | { |
ee76b931 MM |
8623 | qualified_p = (TREE_CODE (function) == COMPONENT_REF |
8624 | && (TREE_CODE (TREE_OPERAND (function, 1)) | |
8625 | == SCOPE_REF)); | |
8626 | function = tsubst_copy_and_build (function, args, complain, | |
b3445994 MM |
8627 | in_decl, |
8628 | !qualified_p); | |
6d80c4b9 MM |
8629 | if (BASELINK_P (function)) |
8630 | qualified_p = true; | |
cc23546e | 8631 | } |
cc23546e | 8632 | |
b3445994 | 8633 | call_args = RECUR (TREE_OPERAND (t, 1)); |
676e33ca MM |
8634 | |
8635 | /* We do not perform argument-dependent lookup if normal | |
8636 | lookup finds a non-function, in accordance with the | |
8637 | expected resolution of DR 218. */ | |
ee935db4 | 8638 | if (koenig_p |
44370687 MM |
8639 | && ((is_overloaded_fn (function) |
8640 | /* If lookup found a member function, the Koenig lookup is | |
8641 | not appropriate, even if an unqualified-name was used | |
8642 | to denote the function. */ | |
8643 | && !DECL_FUNCTION_MEMBER_P (get_first_fn (function))) | |
b3445994 | 8644 | || TREE_CODE (function) == IDENTIFIER_NODE)) |
6d80c4b9 MM |
8645 | function = perform_koenig_lookup (function, call_args); |
8646 | ||
8647 | if (TREE_CODE (function) == IDENTIFIER_NODE) | |
b3445994 | 8648 | { |
6d80c4b9 MM |
8649 | unqualified_name_lookup_error (function); |
8650 | return error_mark_node; | |
b3445994 MM |
8651 | } |
8652 | ||
8653 | /* Remember that there was a reference to this entity. */ | |
8654 | if (DECL_P (function)) | |
8655 | mark_used (function); | |
8656 | ||
d17811fd MM |
8657 | if (TREE_CODE (function) == OFFSET_REF) |
8658 | return build_offset_ref_call_from_tree (function, call_args); | |
8659 | if (TREE_CODE (function) == COMPONENT_REF) | |
9f880ef9 MM |
8660 | { |
8661 | if (!BASELINK_P (TREE_OPERAND (function, 1))) | |
8662 | return finish_call_expr (function, call_args, | |
8663 | /*disallow_virtual=*/false, | |
8664 | /*koenig_p=*/false); | |
8665 | else | |
8666 | return (build_new_method_call | |
8667 | (TREE_OPERAND (function, 0), | |
8668 | TREE_OPERAND (function, 1), | |
8669 | call_args, NULL_TREE, | |
8670 | qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)); | |
8671 | } | |
d17811fd | 8672 | return finish_call_expr (function, call_args, |
6d80c4b9 MM |
8673 | /*disallow_virtual=*/qualified_p, |
8674 | koenig_p); | |
cc23546e JO |
8675 | } |
8676 | ||
8677 | case COND_EXPR: | |
8678 | return build_x_conditional_expr | |
b3445994 MM |
8679 | (RECUR (TREE_OPERAND (t, 0)), |
8680 | RECUR (TREE_OPERAND (t, 1)), | |
8681 | RECUR (TREE_OPERAND (t, 2))); | |
cc23546e JO |
8682 | |
8683 | case PSEUDO_DTOR_EXPR: | |
8684 | return finish_pseudo_destructor_expr | |
b3445994 MM |
8685 | (RECUR (TREE_OPERAND (t, 0)), |
8686 | RECUR (TREE_OPERAND (t, 1)), | |
8687 | RECUR (TREE_OPERAND (t, 2))); | |
cc23546e JO |
8688 | |
8689 | case TREE_LIST: | |
8690 | { | |
8691 | tree purpose, value, chain; | |
8692 | ||
8693 | if (t == void_list_node) | |
8694 | return t; | |
8695 | ||
8696 | purpose = TREE_PURPOSE (t); | |
8697 | if (purpose) | |
b3445994 | 8698 | purpose = RECUR (purpose); |
cc23546e JO |
8699 | value = TREE_VALUE (t); |
8700 | if (value) | |
b3445994 | 8701 | value = RECUR (value); |
cc23546e JO |
8702 | chain = TREE_CHAIN (t); |
8703 | if (chain && chain != void_type_node) | |
b3445994 | 8704 | chain = RECUR (chain); |
cc23546e JO |
8705 | if (purpose == TREE_PURPOSE (t) |
8706 | && value == TREE_VALUE (t) | |
8707 | && chain == TREE_CHAIN (t)) | |
8708 | return t; | |
8709 | return tree_cons (purpose, value, chain); | |
8710 | } | |
8711 | ||
8712 | case COMPONENT_REF: | |
8713 | { | |
ee76b931 MM |
8714 | tree object; |
8715 | tree member; | |
8716 | ||
b3445994 MM |
8717 | object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0), |
8718 | args, complain, in_decl); | |
ee76b931 MM |
8719 | /* Remember that there was a reference to this entity. */ |
8720 | if (DECL_P (object)) | |
8721 | mark_used (object); | |
8722 | ||
8723 | member = TREE_OPERAND (t, 1); | |
8724 | if (BASELINK_P (member)) | |
8725 | member = tsubst_baselink (member, | |
8726 | non_reference (TREE_TYPE (object)), | |
8727 | args, complain, in_decl); | |
8728 | else | |
8729 | member = tsubst_copy (member, args, complain, in_decl); | |
cc23546e | 8730 | |
bad1f462 KL |
8731 | if (member == error_mark_node) |
8732 | return error_mark_node; | |
8733 | else if (!CLASS_TYPE_P (TREE_TYPE (object))) | |
cc23546e JO |
8734 | { |
8735 | if (TREE_CODE (member) == BIT_NOT_EXPR) | |
8736 | return finish_pseudo_destructor_expr (object, | |
8737 | NULL_TREE, | |
8738 | TREE_TYPE (object)); | |
8739 | else if (TREE_CODE (member) == SCOPE_REF | |
8740 | && (TREE_CODE (TREE_OPERAND (member, 1)) == BIT_NOT_EXPR)) | |
8741 | return finish_pseudo_destructor_expr (object, | |
8742 | object, | |
8743 | TREE_TYPE (object)); | |
8744 | } | |
8745 | else if (TREE_CODE (member) == SCOPE_REF | |
8746 | && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR) | |
8747 | { | |
8748 | tree tmpl; | |
8749 | tree args; | |
8750 | ||
8751 | /* Lookup the template functions now that we know what the | |
8752 | scope is. */ | |
8753 | tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0); | |
8754 | args = TREE_OPERAND (TREE_OPERAND (member, 1), 1); | |
22038b2c | 8755 | member = lookup_qualified_name (TREE_OPERAND (member, 0), tmpl, |
12483c9f NS |
8756 | /*is_type_p=*/false, |
8757 | /*complain=*/false); | |
cc23546e | 8758 | if (BASELINK_P (member)) |
44370687 MM |
8759 | { |
8760 | BASELINK_FUNCTIONS (member) | |
8761 | = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member), | |
8762 | args); | |
8763 | member = (adjust_result_of_qualified_name_lookup | |
8764 | (member, BINFO_TYPE (BASELINK_BINFO (member)), | |
8765 | TREE_TYPE (object))); | |
8766 | } | |
cc23546e JO |
8767 | else |
8768 | { | |
8f78f01f MM |
8769 | qualified_name_lookup_error (TREE_TYPE (object), tmpl, |
8770 | member); | |
cc23546e JO |
8771 | return error_mark_node; |
8772 | } | |
8773 | } | |
2436b51f MM |
8774 | else if (TREE_CODE (member) == SCOPE_REF |
8775 | && !CLASS_TYPE_P (TREE_OPERAND (member, 0)) | |
8776 | && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL) | |
8777 | { | |
8778 | if (complain & tf_error) | |
8779 | { | |
8780 | if (TYPE_P (TREE_OPERAND (member, 0))) | |
0f51ccfc | 8781 | error ("%qT is not a class or namespace", |
2436b51f MM |
8782 | TREE_OPERAND (member, 0)); |
8783 | else | |
0f51ccfc | 8784 | error ("%qD is not a class or namespace", |
2436b51f MM |
8785 | TREE_OPERAND (member, 0)); |
8786 | } | |
8787 | return error_mark_node; | |
8788 | } | |
a3f10e50 NS |
8789 | else if (TREE_CODE (member) == FIELD_DECL) |
8790 | return finish_non_static_data_member (member, object, NULL_TREE); | |
cc23546e JO |
8791 | |
8792 | return finish_class_member_access_expr (object, member); | |
8793 | } | |
8794 | ||
8795 | case THROW_EXPR: | |
8796 | return build_throw | |
b3445994 | 8797 | (RECUR (TREE_OPERAND (t, 0))); |
cc23546e JO |
8798 | |
8799 | case CONSTRUCTOR: | |
8800 | { | |
8801 | tree r; | |
8802 | tree elts; | |
8803 | tree type = tsubst (TREE_TYPE (t), args, complain, in_decl); | |
8804 | bool purpose_p; | |
8805 | ||
8806 | /* digest_init will do the wrong thing if we let it. */ | |
8807 | if (type && TYPE_PTRMEMFUNC_P (type)) | |
8808 | return t; | |
8809 | ||
8810 | r = NULL_TREE; | |
8811 | /* We do not want to process the purpose of aggregate | |
8812 | initializers as they are identifier nodes which will be | |
8813 | looked up by digest_init. */ | |
8814 | purpose_p = !(type && IS_AGGR_TYPE (type)); | |
db77ef4c | 8815 | for (elts = CONSTRUCTOR_ELTS (t); |
cc23546e JO |
8816 | elts; |
8817 | elts = TREE_CHAIN (elts)) | |
8818 | { | |
8819 | tree purpose = TREE_PURPOSE (elts); | |
8820 | tree value = TREE_VALUE (elts); | |
8821 | ||
8822 | if (purpose && purpose_p) | |
b3445994 MM |
8823 | purpose = RECUR (purpose); |
8824 | value = RECUR (value); | |
cc23546e JO |
8825 | r = tree_cons (purpose, value, r); |
8826 | } | |
8827 | ||
dcf92453 | 8828 | r = build_constructor (NULL_TREE, nreverse (r)); |
cc23546e JO |
8829 | TREE_HAS_CONSTRUCTOR (r) = TREE_HAS_CONSTRUCTOR (t); |
8830 | ||
8831 | if (type) | |
8832 | return digest_init (type, r, 0); | |
8833 | return r; | |
8834 | } | |
8835 | ||
8836 | case TYPEID_EXPR: | |
8837 | { | |
b3445994 | 8838 | tree operand_0 = RECUR (TREE_OPERAND (t, 0)); |
cc23546e JO |
8839 | if (TYPE_P (operand_0)) |
8840 | return get_typeid (operand_0); | |
8841 | return build_typeid (operand_0); | |
8842 | } | |
8843 | ||
cc23546e | 8844 | case VAR_DECL: |
db24eb1f NS |
8845 | if (!args) |
8846 | return t; | |
8847 | /* Fall through */ | |
8848 | ||
8849 | case PARM_DECL: | |
8850 | { | |
8851 | tree r = tsubst_copy (t, args, complain, in_decl); | |
8852 | ||
8853 | if (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE) | |
8854 | /* If the original type was a reference, we'll be wrapped in | |
8855 | the appropriate INDIRECT_REF. */ | |
8856 | r = convert_from_reference (r); | |
8857 | return r; | |
8858 | } | |
cc23546e JO |
8859 | |
8860 | case VA_ARG_EXPR: | |
b3445994 MM |
8861 | return build_x_va_arg (RECUR (TREE_OPERAND (t, 0)), |
8862 | tsubst_copy (TREE_TYPE (t), args, complain, | |
8863 | in_decl)); | |
cc23546e | 8864 | |
4bceb077 RH |
8865 | case OFFSETOF_EXPR: |
8866 | return fold_offsetof (RECUR (TREE_OPERAND (t, 0))); | |
8867 | ||
0fe0caa6 RH |
8868 | case STMT_EXPR: |
8869 | { | |
8870 | tree old_stmt_expr = cur_stmt_expr; | |
8871 | tree stmt_expr = begin_stmt_expr (); | |
8872 | ||
8873 | cur_stmt_expr = stmt_expr; | |
8874 | tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl); | |
8875 | stmt_expr = finish_stmt_expr (stmt_expr, false); | |
8876 | cur_stmt_expr = old_stmt_expr; | |
8877 | ||
8878 | return stmt_expr; | |
8879 | } | |
8880 | ||
a5201a91 MM |
8881 | case CONST_DECL: |
8882 | t = tsubst_copy (t, args, complain, in_decl); | |
8883 | /* As in finish_id_expression, we resolve enumeration constants | |
8884 | to their underlying values. */ | |
8885 | if (TREE_CODE (t) == CONST_DECL) | |
8886 | return DECL_INITIAL (t); | |
8887 | return t; | |
8888 | ||
cc23546e JO |
8889 | default: |
8890 | return tsubst_copy (t, args, complain, in_decl); | |
8891 | } | |
b3445994 MM |
8892 | |
8893 | #undef RECUR | |
cc23546e JO |
8894 | } |
8895 | ||
3e4a3562 NS |
8896 | /* Verify that the instantiated ARGS are valid. For type arguments, |
8897 | make sure that the type's linkage is ok. For non-type arguments, | |
34cd5ae7 | 8898 | make sure they are constants if they are integral or enumerations. |
9bcb9aae | 8899 | Emit an error under control of COMPLAIN, and return TRUE on error. */ |
3e4a3562 NS |
8900 | |
8901 | static bool | |
3a978d72 | 8902 | check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain) |
3e4a3562 NS |
8903 | { |
8904 | int ix, len = DECL_NTPARMS (tmpl); | |
8905 | bool result = false; | |
8906 | ||
8907 | for (ix = 0; ix != len; ix++) | |
8908 | { | |
8909 | tree t = TREE_VEC_ELT (args, ix); | |
8910 | ||
8911 | if (TYPE_P (t)) | |
8912 | { | |
8913 | /* [basic.link]: A name with no linkage (notably, the name | |
8914 | of a class or enumeration declared in a local scope) | |
8915 | shall not be used to declare an entity with linkage. | |
8916 | This implies that names with no linkage cannot be used as | |
8917 | template arguments. */ | |
4684cd27 | 8918 | tree nt = no_linkage_check (t, /*relaxed_p=*/false); |
3e4a3562 NS |
8919 | |
8920 | if (nt) | |
8921 | { | |
aae4a277 | 8922 | if (TYPE_ANONYMOUS_P (nt)) |
0f51ccfc | 8923 | error ("%qT uses anonymous type", t); |
3e4a3562 | 8924 | else |
0f51ccfc | 8925 | error ("%qT uses local type %qT", t, nt); |
3e4a3562 NS |
8926 | result = true; |
8927 | } | |
8928 | /* In order to avoid all sorts of complications, we do not | |
8929 | allow variably-modified types as template arguments. */ | |
5377d5ba | 8930 | else if (variably_modified_type_p (t, NULL_TREE)) |
3e4a3562 NS |
8931 | { |
8932 | if (complain & tf_error) | |
0f51ccfc | 8933 | error ("%qT is a variably modified type", t); |
3e4a3562 NS |
8934 | result = true; |
8935 | } | |
8936 | } | |
8937 | /* A non-type argument of integral or enumerated type must be a | |
8938 | constant. */ | |
8939 | else if (TREE_TYPE (t) | |
8940 | && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t)) | |
8941 | && !TREE_CONSTANT (t)) | |
8942 | { | |
8943 | if (complain & tf_error) | |
0f51ccfc | 8944 | error ("integral expression %qE is not constant", t); |
3e4a3562 NS |
8945 | result = true; |
8946 | } | |
8947 | } | |
8948 | if (result && complain & tf_error) | |
0f51ccfc | 8949 | error (" trying to instantiate %qD", tmpl); |
3e4a3562 NS |
8950 | return result; |
8951 | } | |
8952 | ||
6ba89f8e | 8953 | /* Instantiate the indicated variable or function template TMPL with |
36a117a5 MM |
8954 | the template arguments in TARG_PTR. */ |
8955 | ||
5566b478 | 8956 | tree |
3a978d72 | 8957 | instantiate_template (tree tmpl, tree targ_ptr, tsubst_flags_t complain) |
8d08fdba | 8958 | { |
5566b478 | 8959 | tree fndecl; |
36a117a5 MM |
8960 | tree gen_tmpl; |
8961 | tree spec; | |
5566b478 | 8962 | |
27fafc8d JM |
8963 | if (tmpl == error_mark_node) |
8964 | return error_mark_node; | |
8965 | ||
50bc768d | 8966 | gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL); |
386b8a85 | 8967 | |
db9b2174 MM |
8968 | /* If this function is a clone, handle it specially. */ |
8969 | if (DECL_CLONED_FUNCTION_P (tmpl)) | |
3ad97789 | 8970 | { |
a30f62e0 | 8971 | tree spec; |
3ad97789 NS |
8972 | tree clone; |
8973 | ||
a30f62e0 MM |
8974 | spec = instantiate_template (DECL_CLONED_FUNCTION (tmpl), targ_ptr, |
8975 | complain); | |
8976 | if (spec == error_mark_node) | |
8977 | return error_mark_node; | |
8978 | ||
c6002625 | 8979 | /* Look for the clone. */ |
4684cd27 | 8980 | FOR_EACH_CLONE (clone, spec) |
3ad97789 NS |
8981 | if (DECL_NAME (clone) == DECL_NAME (tmpl)) |
8982 | return clone; | |
8983 | /* We should always have found the clone by now. */ | |
315fb5db | 8984 | gcc_unreachable (); |
3ad97789 NS |
8985 | return NULL_TREE; |
8986 | } | |
8987 | ||
36a117a5 | 8988 | /* Check to see if we already have this specialization. */ |
c7222c02 MM |
8989 | spec = retrieve_specialization (tmpl, targ_ptr, |
8990 | /*class_specializations_p=*/false); | |
36a117a5 MM |
8991 | if (spec != NULL_TREE) |
8992 | return spec; | |
8993 | ||
f9a7ae04 MM |
8994 | gen_tmpl = most_general_template (tmpl); |
8995 | if (tmpl != gen_tmpl) | |
386b8a85 | 8996 | { |
36a117a5 MM |
8997 | /* The TMPL is a partial instantiation. To get a full set of |
8998 | arguments we must add the arguments used to perform the | |
8999 | partial instantiation. */ | |
9000 | targ_ptr = add_outermost_template_args (DECL_TI_ARGS (tmpl), | |
9001 | targ_ptr); | |
36a117a5 MM |
9002 | |
9003 | /* Check to see if we already have this specialization. */ | |
c7222c02 MM |
9004 | spec = retrieve_specialization (gen_tmpl, targ_ptr, |
9005 | /*class_specializations_p=*/false); | |
75650646 MM |
9006 | if (spec != NULL_TREE) |
9007 | return spec; | |
386b8a85 JM |
9008 | } |
9009 | ||
3e4a3562 NS |
9010 | if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr), |
9011 | complain)) | |
9012 | return error_mark_node; | |
9013 | ||
2b907f5c KL |
9014 | /* We are building a FUNCTION_DECL, during which the access of its |
9015 | parameters and return types have to be checked. However this | |
9016 | FUNCTION_DECL which is the desired context for access checking | |
9017 | is not built yet. We solve this chicken-and-egg problem by | |
9018 | deferring all checks until we have the FUNCTION_DECL. */ | |
9019 | push_deferring_access_checks (dk_deferred); | |
5c74d5b0 | 9020 | |
f4f206f4 | 9021 | /* Substitute template parameters. */ |
17aec3eb | 9022 | fndecl = tsubst (DECL_TEMPLATE_RESULT (gen_tmpl), |
3e4a3562 | 9023 | targ_ptr, complain, gen_tmpl); |
5c74d5b0 | 9024 | |
2b907f5c KL |
9025 | /* Now we know the specialization, compute access previously |
9026 | deferred. */ | |
9027 | push_access_scope (fndecl); | |
9028 | perform_deferred_access_checks (); | |
9029 | pop_access_scope (fndecl); | |
9030 | pop_deferring_access_checks (); | |
5c74d5b0 | 9031 | |
36a117a5 MM |
9032 | /* The DECL_TI_TEMPLATE should always be the immediate parent |
9033 | template, not the most general template. */ | |
9034 | DECL_TI_TEMPLATE (fndecl) = tmpl; | |
8d08fdba | 9035 | |
94350948 | 9036 | /* If we've just instantiated the main entry point for a function, |
3ad97789 NS |
9037 | instantiate all the alternate entry points as well. We do this |
9038 | by cloning the instantiation of the main entry point, not by | |
9039 | instantiating the template clones. */ | |
9040 | if (TREE_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (TREE_CHAIN (gen_tmpl))) | |
9041 | clone_function_decl (fndecl, /*update_method_vec_p=*/0); | |
94350948 | 9042 | |
5566b478 | 9043 | return fndecl; |
8d08fdba | 9044 | } |
5566b478 | 9045 | |
4393e105 MM |
9046 | /* The FN is a TEMPLATE_DECL for a function. The ARGS are the |
9047 | arguments that are being used when calling it. TARGS is a vector | |
9048 | into which the deduced template arguments are placed. | |
8d08fdba MS |
9049 | |
9050 | Return zero for success, 2 for an incomplete match that doesn't resolve | |
9051 | all the types, and 1 for complete failure. An error message will be | |
9052 | printed only for an incomplete match. | |
9053 | ||
e5214479 JM |
9054 | If FN is a conversion operator, or we are trying to produce a specific |
9055 | specialization, RETURN_TYPE is the return type desired. | |
9f54c803 MM |
9056 | |
9057 | The EXPLICIT_TARGS are explicit template arguments provided via a | |
9058 | template-id. | |
6467930b | 9059 | |
830bfa74 MM |
9060 | The parameter STRICT is one of: |
9061 | ||
9062 | DEDUCE_CALL: | |
9063 | We are deducing arguments for a function call, as in | |
9064 | [temp.deduct.call]. | |
9065 | ||
9066 | DEDUCE_CONV: | |
9067 | We are deducing arguments for a conversion function, as in | |
9068 | [temp.deduct.conv]. | |
9069 | ||
9070 | DEDUCE_EXACT: | |
62e4a758 NS |
9071 | We are deducing arguments when doing an explicit instantiation |
9072 | as in [temp.explicit], when determining an explicit specialization | |
9073 | as in [temp.expl.spec], or when taking the address of a function | |
9074 | template, as in [temp.deduct.funcaddr]. | |
9075 | ||
9076 | DEDUCE_ORDER: | |
830bfa74 MM |
9077 | We are deducing arguments when calculating the partial |
9078 | ordering between specializations of function or class | |
62e4a758 | 9079 | templates, as in [temp.func.order] and [temp.class.order]. |
4393e105 | 9080 | |
e5214479 JM |
9081 | LEN is the number of parms to consider before returning success, or -1 |
9082 | for all. This is used in partial ordering to avoid comparing parms for | |
9083 | which no actual argument was passed, since they are not considered in | |
9084 | overload resolution (and are explicitly excluded from consideration in | |
9085 | partial ordering in [temp.func.order]/6). */ | |
8d08fdba MS |
9086 | |
9087 | int | |
3a978d72 NN |
9088 | fn_type_unification (tree fn, |
9089 | tree explicit_targs, | |
9090 | tree targs, | |
9091 | tree args, | |
9092 | tree return_type, | |
9093 | unification_kind_t strict, | |
9094 | int len) | |
386b8a85 | 9095 | { |
4393e105 MM |
9096 | tree parms; |
9097 | tree fntype; | |
adecb3f4 | 9098 | int result; |
386b8a85 | 9099 | |
50bc768d | 9100 | gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL); |
2b59fc25 | 9101 | |
4393e105 MM |
9102 | fntype = TREE_TYPE (fn); |
9103 | if (explicit_targs) | |
75650646 | 9104 | { |
4393e105 MM |
9105 | /* [temp.deduct] |
9106 | ||
9107 | The specified template arguments must match the template | |
9108 | parameters in kind (i.e., type, nontype, template), and there | |
9109 | must not be more arguments than there are parameters; | |
9110 | otherwise type deduction fails. | |
9111 | ||
9112 | Nontype arguments must match the types of the corresponding | |
9113 | nontype template parameters, or must be convertible to the | |
9114 | types of the corresponding nontype parameters as specified in | |
9115 | _temp.arg.nontype_, otherwise type deduction fails. | |
9116 | ||
9117 | All references in the function type of the function template | |
9118 | to the corresponding template parameters are replaced by the | |
9119 | specified template argument values. If a substitution in a | |
9120 | template parameter or in the function type of the function | |
9121 | template results in an invalid type, type deduction fails. */ | |
9122 | int i; | |
9123 | tree converted_args; | |
ffd49b19 | 9124 | bool incomplete; |
75650646 | 9125 | |
c8a7ed43 AO |
9126 | if (explicit_targs == error_mark_node) |
9127 | return 1; | |
9128 | ||
4393e105 MM |
9129 | converted_args |
9130 | = (coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn), | |
c2ea3a40 | 9131 | explicit_targs, NULL_TREE, tf_none, |
4393e105 MM |
9132 | /*require_all_arguments=*/0)); |
9133 | if (converted_args == error_mark_node) | |
75650646 | 9134 | return 1; |
386b8a85 | 9135 | |
ffd49b19 NS |
9136 | /* Substitute the explicit args into the function type. This is |
9137 | necessary so that, for instance, explicitly declared function | |
9138 | arguments can match null pointed constants. If we were given | |
9139 | an incomplete set of explicit args, we must not do semantic | |
9140 | processing during substitution as we could create partial | |
9141 | instantiations. */ | |
9142 | incomplete = NUM_TMPL_ARGS (explicit_targs) != NUM_TMPL_ARGS (targs); | |
9143 | processing_template_decl += incomplete; | |
c2ea3a40 | 9144 | fntype = tsubst (fntype, converted_args, tf_none, NULL_TREE); |
ffd49b19 NS |
9145 | processing_template_decl -= incomplete; |
9146 | ||
4393e105 MM |
9147 | if (fntype == error_mark_node) |
9148 | return 1; | |
050367a3 | 9149 | |
4393e105 | 9150 | /* Place the explicitly specified arguments in TARGS. */ |
ffd49b19 | 9151 | for (i = NUM_TMPL_ARGS (converted_args); i--;) |
4393e105 | 9152 | TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (converted_args, i); |
75650646 | 9153 | } |
4393e105 MM |
9154 | |
9155 | parms = TYPE_ARG_TYPES (fntype); | |
e5214479 JM |
9156 | /* Never do unification on the 'this' parameter. */ |
9157 | if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)) | |
9158 | parms = TREE_CHAIN (parms); | |
8d3631f8 NS |
9159 | |
9160 | if (return_type) | |
9161 | { | |
9162 | /* We've been given a return type to match, prepend it. */ | |
9163 | parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms); | |
9164 | args = tree_cons (NULL_TREE, return_type, args); | |
16e42e7c JM |
9165 | if (len >= 0) |
9166 | ++len; | |
4393e105 MM |
9167 | } |
9168 | ||
4393e105 MM |
9169 | /* We allow incomplete unification without an error message here |
9170 | because the standard doesn't seem to explicitly prohibit it. Our | |
9171 | callers must be ready to deal with unification failures in any | |
9172 | event. */ | |
adecb3f4 MM |
9173 | result = type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn), |
9174 | targs, parms, args, /*subr=*/0, | |
e5214479 | 9175 | strict, /*allow_incomplete*/1, len); |
adecb3f4 MM |
9176 | |
9177 | if (result == 0) | |
9178 | /* All is well so far. Now, check: | |
9179 | ||
9180 | [temp.deduct] | |
9181 | ||
9182 | When all template arguments have been deduced, all uses of | |
9183 | template parameters in nondeduced contexts are replaced with | |
9184 | the corresponding deduced argument values. If the | |
9185 | substitution results in an invalid type, as described above, | |
9186 | type deduction fails. */ | |
c2ea3a40 | 9187 | if (tsubst (TREE_TYPE (fn), targs, tf_none, NULL_TREE) |
adecb3f4 MM |
9188 | == error_mark_node) |
9189 | return 1; | |
9190 | ||
9191 | return result; | |
830bfa74 MM |
9192 | } |
9193 | ||
9194 | /* Adjust types before performing type deduction, as described in | |
9195 | [temp.deduct.call] and [temp.deduct.conv]. The rules in these two | |
4c7d0dff MM |
9196 | sections are symmetric. PARM is the type of a function parameter |
9197 | or the return type of the conversion function. ARG is the type of | |
9198 | the argument passed to the call, or the type of the value | |
dc957d14 | 9199 | initialized with the result of the conversion function. */ |
386b8a85 | 9200 | |
62e4a758 | 9201 | static int |
3a978d72 NN |
9202 | maybe_adjust_types_for_deduction (unification_kind_t strict, |
9203 | tree* parm, | |
9204 | tree* arg) | |
830bfa74 | 9205 | { |
62e4a758 NS |
9206 | int result = 0; |
9207 | ||
830bfa74 MM |
9208 | switch (strict) |
9209 | { | |
9210 | case DEDUCE_CALL: | |
9211 | break; | |
9212 | ||
9213 | case DEDUCE_CONV: | |
9214 | { | |
4c7d0dff MM |
9215 | /* Swap PARM and ARG throughout the remainder of this |
9216 | function; the handling is precisely symmetric since PARM | |
9217 | will initialize ARG rather than vice versa. */ | |
830bfa74 MM |
9218 | tree* temp = parm; |
9219 | parm = arg; | |
9220 | arg = temp; | |
9221 | break; | |
9222 | } | |
9223 | ||
9224 | case DEDUCE_EXACT: | |
9225 | /* There is nothing to do in this case. */ | |
62e4a758 | 9226 | return 0; |
830bfa74 | 9227 | |
62e4a758 NS |
9228 | case DEDUCE_ORDER: |
9229 | /* DR 214. [temp.func.order] is underspecified, and leads to no | |
9230 | ordering between things like `T *' and `T const &' for `U *'. | |
9231 | The former has T=U and the latter T=U*. The former looks more | |
9232 | specialized and John Spicer considers it well-formed (the EDG | |
9233 | compiler accepts it). | |
9234 | ||
9235 | John also confirms that deduction should proceed as in a function | |
996065b4 | 9236 | call. Which implies the usual ARG and PARM conversions as DEDUCE_CALL. |
62e4a758 NS |
9237 | However, in ordering, ARG can have REFERENCE_TYPE, but no argument |
9238 | to an actual call can have such a type. | |
9239 | ||
996065b4 NS |
9240 | If both ARG and PARM are REFERENCE_TYPE, we change neither. |
9241 | If only ARG is a REFERENCE_TYPE, we look through that and then | |
9242 | proceed as with DEDUCE_CALL (which could further convert it). */ | |
62e4a758 NS |
9243 | if (TREE_CODE (*arg) == REFERENCE_TYPE) |
9244 | { | |
9245 | if (TREE_CODE (*parm) == REFERENCE_TYPE) | |
9246 | return 0; | |
9247 | *arg = TREE_TYPE (*arg); | |
62e4a758 NS |
9248 | } |
9249 | break; | |
830bfa74 | 9250 | default: |
315fb5db | 9251 | gcc_unreachable (); |
830bfa74 MM |
9252 | } |
9253 | ||
9254 | if (TREE_CODE (*parm) != REFERENCE_TYPE) | |
9255 | { | |
9256 | /* [temp.deduct.call] | |
9257 | ||
9258 | If P is not a reference type: | |
9259 | ||
9260 | --If A is an array type, the pointer type produced by the | |
9261 | array-to-pointer standard conversion (_conv.array_) is | |
9262 | used in place of A for type deduction; otherwise, | |
9263 | ||
9264 | --If A is a function type, the pointer type produced by | |
9265 | the function-to-pointer standard conversion | |
9266 | (_conv.func_) is used in place of A for type deduction; | |
9267 | otherwise, | |
9268 | ||
9269 | --If A is a cv-qualified type, the top level | |
9270 | cv-qualifiers of A's type are ignored for type | |
9271 | deduction. */ | |
9272 | if (TREE_CODE (*arg) == ARRAY_TYPE) | |
9273 | *arg = build_pointer_type (TREE_TYPE (*arg)); | |
d8f8dca1 | 9274 | else if (TREE_CODE (*arg) == FUNCTION_TYPE) |
830bfa74 MM |
9275 | *arg = build_pointer_type (*arg); |
9276 | else | |
9277 | *arg = TYPE_MAIN_VARIANT (*arg); | |
9278 | } | |
9279 | ||
9280 | /* [temp.deduct.call] | |
9281 | ||
9282 | If P is a cv-qualified type, the top level cv-qualifiers | |
9283 | of P's type are ignored for type deduction. If P is a | |
9284 | reference type, the type referred to by P is used for | |
9285 | type deduction. */ | |
9286 | *parm = TYPE_MAIN_VARIANT (*parm); | |
9287 | if (TREE_CODE (*parm) == REFERENCE_TYPE) | |
62e4a758 NS |
9288 | { |
9289 | *parm = TREE_TYPE (*parm); | |
9290 | result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL; | |
9291 | } | |
1c82cc90 NS |
9292 | |
9293 | /* DR 322. For conversion deduction, remove a reference type on parm | |
9294 | too (which has been swapped into ARG). */ | |
9295 | if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE) | |
9296 | *arg = TREE_TYPE (*arg); | |
9297 | ||
62e4a758 | 9298 | return result; |
386b8a85 JM |
9299 | } |
9300 | ||
e5214479 | 9301 | /* Most parms like fn_type_unification. |
9f54c803 MM |
9302 | |
9303 | If SUBR is 1, we're being called recursively (to unify the | |
9304 | arguments of a function or method parameter of a function | |
9305 | template). */ | |
386b8a85 | 9306 | |
4966381a | 9307 | static int |
3a978d72 NN |
9308 | type_unification_real (tree tparms, |
9309 | tree targs, | |
9310 | tree xparms, | |
9311 | tree xargs, | |
9312 | int subr, | |
9313 | unification_kind_t strict, | |
9314 | int allow_incomplete, | |
9315 | int xlen) | |
8d08fdba MS |
9316 | { |
9317 | tree parm, arg; | |
9318 | int i; | |
9319 | int ntparms = TREE_VEC_LENGTH (tparms); | |
830bfa74 | 9320 | int sub_strict; |
bd0d5d4a JM |
9321 | int saw_undeduced = 0; |
9322 | tree parms, args; | |
9323 | int len; | |
8d08fdba | 9324 | |
50bc768d NS |
9325 | gcc_assert (TREE_CODE (tparms) == TREE_VEC); |
9326 | gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST); | |
9327 | gcc_assert (!xargs || TREE_CODE (xargs) == TREE_LIST); | |
9328 | gcc_assert (ntparms > 0); | |
8d08fdba | 9329 | |
830bfa74 MM |
9330 | switch (strict) |
9331 | { | |
9332 | case DEDUCE_CALL: | |
028d1f20 NS |
9333 | sub_strict = (UNIFY_ALLOW_OUTER_LEVEL | UNIFY_ALLOW_MORE_CV_QUAL |
9334 | | UNIFY_ALLOW_DERIVED); | |
830bfa74 MM |
9335 | break; |
9336 | ||
9337 | case DEDUCE_CONV: | |
9338 | sub_strict = UNIFY_ALLOW_LESS_CV_QUAL; | |
9339 | break; | |
9340 | ||
9341 | case DEDUCE_EXACT: | |
9342 | sub_strict = UNIFY_ALLOW_NONE; | |
9343 | break; | |
62e4a758 NS |
9344 | |
9345 | case DEDUCE_ORDER: | |
9346 | sub_strict = UNIFY_ALLOW_NONE; | |
9347 | break; | |
830bfa74 MM |
9348 | |
9349 | default: | |
315fb5db | 9350 | gcc_unreachable (); |
830bfa74 MM |
9351 | } |
9352 | ||
bd0d5d4a | 9353 | if (xlen == 0) |
e5214479 JM |
9354 | return 0; |
9355 | ||
bd0d5d4a JM |
9356 | again: |
9357 | parms = xparms; | |
9358 | args = xargs; | |
9359 | len = xlen; | |
9360 | ||
8d08fdba MS |
9361 | while (parms |
9362 | && parms != void_list_node | |
9363 | && args | |
9364 | && args != void_list_node) | |
9365 | { | |
9366 | parm = TREE_VALUE (parms); | |
9367 | parms = TREE_CHAIN (parms); | |
9368 | arg = TREE_VALUE (args); | |
9369 | args = TREE_CHAIN (args); | |
9370 | ||
9371 | if (arg == error_mark_node) | |
9372 | return 1; | |
9373 | if (arg == unknown_type_node) | |
34016c81 JM |
9374 | /* We can't deduce anything from this, but we might get all the |
9375 | template args from other function args. */ | |
9376 | continue; | |
b7484fbe | 9377 | |
03e70705 JM |
9378 | /* Conversions will be performed on a function argument that |
9379 | corresponds with a function parameter that contains only | |
9380 | non-deducible template parameters and explicitly specified | |
9381 | template parameters. */ | |
c353b8e3 | 9382 | if (!uses_template_parms (parm)) |
b7484fbe | 9383 | { |
03e70705 JM |
9384 | tree type; |
9385 | ||
2f939d94 | 9386 | if (!TYPE_P (arg)) |
03e70705 JM |
9387 | type = TREE_TYPE (arg); |
9388 | else | |
c353b8e3 | 9389 | type = arg; |
03e70705 | 9390 | |
62e4a758 | 9391 | if (strict == DEDUCE_EXACT || strict == DEDUCE_ORDER) |
03e70705 | 9392 | { |
3bfdc719 | 9393 | if (same_type_p (parm, type)) |
03e70705 JM |
9394 | continue; |
9395 | } | |
03e70705 | 9396 | else |
343c89cd JM |
9397 | /* It might work; we shouldn't check now, because we might |
9398 | get into infinite recursion. Overload resolution will | |
9399 | handle it. */ | |
9400 | continue; | |
03e70705 | 9401 | |
b7484fbe MS |
9402 | return 1; |
9403 | } | |
9404 | ||
2f939d94 | 9405 | if (!TYPE_P (arg)) |
8d08fdba | 9406 | { |
50bc768d | 9407 | gcc_assert (TREE_TYPE (arg) != NULL_TREE); |
34016c81 | 9408 | if (type_unknown_p (arg)) |
28cbf42c | 9409 | { |
34016c81 JM |
9410 | /* [temp.deduct.type] A template-argument can be deduced from |
9411 | a pointer to function or pointer to member function | |
9412 | argument if the set of overloaded functions does not | |
9413 | contain function templates and at most one of a set of | |
9414 | overloaded functions provides a unique match. */ | |
9415 | ||
9416 | if (resolve_overloaded_unification | |
4393e105 | 9417 | (tparms, targs, parm, arg, strict, sub_strict) |
34016c81 JM |
9418 | != 0) |
9419 | return 1; | |
9420 | continue; | |
28cbf42c | 9421 | } |
8d08fdba | 9422 | arg = TREE_TYPE (arg); |
08476342 NS |
9423 | if (arg == error_mark_node) |
9424 | return 1; | |
8d08fdba | 9425 | } |
62e4a758 NS |
9426 | |
9427 | { | |
9428 | int arg_strict = sub_strict; | |
9429 | ||
9430 | if (!subr) | |
9431 | arg_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg); | |
4393e105 | 9432 | |
62e4a758 NS |
9433 | if (unify (tparms, targs, parm, arg, arg_strict)) |
9434 | return 1; | |
9435 | } | |
e5214479 JM |
9436 | |
9437 | /* Are we done with the interesting parms? */ | |
9438 | if (--len == 0) | |
bd0d5d4a | 9439 | goto done; |
8d08fdba MS |
9440 | } |
9441 | /* Fail if we've reached the end of the parm list, and more args | |
9442 | are present, and the parm list isn't variadic. */ | |
9443 | if (args && args != void_list_node && parms == void_list_node) | |
9444 | return 1; | |
f4f206f4 | 9445 | /* Fail if parms are left and they don't have default values. */ |
8d08fdba MS |
9446 | if (parms |
9447 | && parms != void_list_node | |
9448 | && TREE_PURPOSE (parms) == NULL_TREE) | |
9449 | return 1; | |
bd0d5d4a JM |
9450 | |
9451 | done: | |
8d08fdba MS |
9452 | if (!subr) |
9453 | for (i = 0; i < ntparms; i++) | |
050367a3 | 9454 | if (TREE_VEC_ELT (targs, i) == NULL_TREE) |
8d08fdba | 9455 | { |
bd0d5d4a JM |
9456 | tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i)); |
9457 | ||
9458 | /* If this is an undeduced nontype parameter that depends on | |
9459 | a type parameter, try another pass; its type may have been | |
9460 | deduced from a later argument than the one from which | |
9461 | this parameter can be deduced. */ | |
9462 | if (TREE_CODE (tparm) == PARM_DECL | |
9463 | && uses_template_parms (TREE_TYPE (tparm)) | |
9464 | && !saw_undeduced++) | |
9465 | goto again; | |
9466 | ||
386b8a85 | 9467 | if (!allow_incomplete) |
8251199e | 9468 | error ("incomplete type unification"); |
8d08fdba MS |
9469 | return 2; |
9470 | } | |
9471 | return 0; | |
9472 | } | |
9473 | ||
34016c81 JM |
9474 | /* Subroutine of type_unification_real. Args are like the variables at the |
9475 | call site. ARG is an overloaded function (or template-id); we try | |
9476 | deducing template args from each of the overloads, and if only one | |
9477 | succeeds, we go with that. Modifies TARGS and returns 0 on success. */ | |
9478 | ||
9479 | static int | |
3a978d72 NN |
9480 | resolve_overloaded_unification (tree tparms, |
9481 | tree targs, | |
9482 | tree parm, | |
9483 | tree arg, | |
9484 | unification_kind_t strict, | |
9485 | int sub_strict) | |
34016c81 JM |
9486 | { |
9487 | tree tempargs = copy_node (targs); | |
9488 | int good = 0; | |
f23fb7f5 | 9489 | bool addr_p; |
34016c81 JM |
9490 | |
9491 | if (TREE_CODE (arg) == ADDR_EXPR) | |
f23fb7f5 MM |
9492 | { |
9493 | arg = TREE_OPERAND (arg, 0); | |
9494 | addr_p = true; | |
9495 | } | |
9496 | else | |
9497 | addr_p = false; | |
9f3d9e46 | 9498 | |
d8f8dca1 MM |
9499 | if (TREE_CODE (arg) == COMPONENT_REF) |
9500 | /* Handle `&x' where `x' is some static or non-static member | |
9501 | function name. */ | |
9502 | arg = TREE_OPERAND (arg, 1); | |
9503 | ||
05e0b2f4 JM |
9504 | if (TREE_CODE (arg) == OFFSET_REF) |
9505 | arg = TREE_OPERAND (arg, 1); | |
9506 | ||
9f3d9e46 | 9507 | /* Strip baselink information. */ |
50ad9642 MM |
9508 | if (BASELINK_P (arg)) |
9509 | arg = BASELINK_FUNCTIONS (arg); | |
9f3d9e46 | 9510 | |
34016c81 JM |
9511 | if (TREE_CODE (arg) == TEMPLATE_ID_EXPR) |
9512 | { | |
9513 | /* If we got some explicit template args, we need to plug them into | |
9514 | the affected templates before we try to unify, in case the | |
9515 | explicit args will completely resolve the templates in question. */ | |
9516 | ||
9517 | tree expl_subargs = TREE_OPERAND (arg, 1); | |
9518 | arg = TREE_OPERAND (arg, 0); | |
9519 | ||
9520 | for (; arg; arg = OVL_NEXT (arg)) | |
9521 | { | |
9522 | tree fn = OVL_CURRENT (arg); | |
9523 | tree subargs, elem; | |
9524 | ||
9525 | if (TREE_CODE (fn) != TEMPLATE_DECL) | |
9526 | continue; | |
9527 | ||
17aec3eb RK |
9528 | subargs = get_bindings_overload (fn, DECL_TEMPLATE_RESULT (fn), |
9529 | expl_subargs); | |
34016c81 JM |
9530 | if (subargs) |
9531 | { | |
c2ea3a40 | 9532 | elem = tsubst (TREE_TYPE (fn), subargs, tf_none, NULL_TREE); |
f23fb7f5 MM |
9533 | good += try_one_overload (tparms, targs, tempargs, parm, |
9534 | elem, strict, sub_strict, addr_p); | |
34016c81 JM |
9535 | } |
9536 | } | |
9537 | } | |
315fb5db | 9538 | else |
34016c81 | 9539 | { |
315fb5db NS |
9540 | gcc_assert (TREE_CODE (arg) == OVERLOAD |
9541 | || TREE_CODE (arg) == FUNCTION_DECL); | |
9542 | ||
34016c81 | 9543 | for (; arg; arg = OVL_NEXT (arg)) |
f23fb7f5 MM |
9544 | good += try_one_overload (tparms, targs, tempargs, parm, |
9545 | TREE_TYPE (OVL_CURRENT (arg)), | |
9546 | strict, sub_strict, addr_p); | |
34016c81 | 9547 | } |
34016c81 JM |
9548 | |
9549 | /* [temp.deduct.type] A template-argument can be deduced from a pointer | |
9550 | to function or pointer to member function argument if the set of | |
9551 | overloaded functions does not contain function templates and at most | |
9552 | one of a set of overloaded functions provides a unique match. | |
9553 | ||
9554 | So if we found multiple possibilities, we return success but don't | |
9555 | deduce anything. */ | |
9556 | ||
9557 | if (good == 1) | |
9558 | { | |
9559 | int i = TREE_VEC_LENGTH (targs); | |
9560 | for (; i--; ) | |
9561 | if (TREE_VEC_ELT (tempargs, i)) | |
9562 | TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (tempargs, i); | |
9563 | } | |
9564 | if (good) | |
9565 | return 0; | |
9566 | ||
9567 | return 1; | |
9568 | } | |
9569 | ||
9570 | /* Subroutine of resolve_overloaded_unification; does deduction for a single | |
9571 | overload. Fills TARGS with any deduced arguments, or error_mark_node if | |
9572 | different overloads deduce different arguments for a given parm. | |
f23fb7f5 MM |
9573 | ADDR_P is true if the expression for which deduction is being |
9574 | performed was of the form "& fn" rather than simply "fn". | |
9575 | ||
34016c81 JM |
9576 | Returns 1 on success. */ |
9577 | ||
9578 | static int | |
3a978d72 NN |
9579 | try_one_overload (tree tparms, |
9580 | tree orig_targs, | |
9581 | tree targs, | |
9582 | tree parm, | |
9583 | tree arg, | |
9584 | unification_kind_t strict, | |
f23fb7f5 MM |
9585 | int sub_strict, |
9586 | bool addr_p) | |
34016c81 JM |
9587 | { |
9588 | int nargs; | |
9589 | tree tempargs; | |
9590 | int i; | |
9591 | ||
9592 | /* [temp.deduct.type] A template-argument can be deduced from a pointer | |
9593 | to function or pointer to member function argument if the set of | |
9594 | overloaded functions does not contain function templates and at most | |
9595 | one of a set of overloaded functions provides a unique match. | |
9596 | ||
9597 | So if this is a template, just return success. */ | |
9598 | ||
9599 | if (uses_template_parms (arg)) | |
9600 | return 1; | |
9601 | ||
f23fb7f5 MM |
9602 | if (TREE_CODE (arg) == METHOD_TYPE) |
9603 | arg = build_ptrmemfunc_type (build_pointer_type (arg)); | |
9604 | else if (addr_p) | |
9605 | arg = build_pointer_type (arg); | |
9606 | ||
62e4a758 | 9607 | sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg); |
34016c81 JM |
9608 | |
9609 | /* We don't copy orig_targs for this because if we have already deduced | |
9610 | some template args from previous args, unify would complain when we | |
9611 | try to deduce a template parameter for the same argument, even though | |
9612 | there isn't really a conflict. */ | |
9613 | nargs = TREE_VEC_LENGTH (targs); | |
f31c0a32 | 9614 | tempargs = make_tree_vec (nargs); |
34016c81 | 9615 | |
4393e105 | 9616 | if (unify (tparms, tempargs, parm, arg, sub_strict) != 0) |
34016c81 JM |
9617 | return 0; |
9618 | ||
9619 | /* First make sure we didn't deduce anything that conflicts with | |
e97e5263 | 9620 | explicitly specified args. */ |
34016c81 JM |
9621 | for (i = nargs; i--; ) |
9622 | { | |
9623 | tree elt = TREE_VEC_ELT (tempargs, i); | |
e97e5263 | 9624 | tree oldelt = TREE_VEC_ELT (orig_targs, i); |
34016c81 JM |
9625 | |
9626 | if (elt == NULL_TREE) | |
9627 | continue; | |
9628 | else if (uses_template_parms (elt)) | |
9629 | { | |
9630 | /* Since we're unifying against ourselves, we will fill in template | |
9631 | args used in the function parm list with our own template parms. | |
9632 | Discard them. */ | |
9633 | TREE_VEC_ELT (tempargs, i) = NULL_TREE; | |
9634 | continue; | |
9635 | } | |
9636 | else if (oldelt && ! template_args_equal (oldelt, elt)) | |
9637 | return 0; | |
9638 | } | |
9639 | ||
9640 | for (i = nargs; i--; ) | |
9641 | { | |
9642 | tree elt = TREE_VEC_ELT (tempargs, i); | |
9643 | ||
9644 | if (elt) | |
9645 | TREE_VEC_ELT (targs, i) = elt; | |
9646 | } | |
9647 | ||
9648 | return 1; | |
9649 | } | |
9650 | ||
74601d7c KL |
9651 | /* Verify that nondeduce template argument agrees with the type |
9652 | obtained from argument deduction. Return nonzero if the | |
9653 | verification fails. | |
9654 | ||
9655 | For example: | |
9656 | ||
9657 | struct A { typedef int X; }; | |
9658 | template <class T, class U> struct C {}; | |
9659 | template <class T> struct C<T, typename T::X> {}; | |
9660 | ||
9661 | Then with the instantiation `C<A, int>', we can deduce that | |
9662 | `T' is `A' but unify () does not check whether `typename T::X' | |
9663 | is `int'. This function ensure that they agree. | |
9664 | ||
9665 | TARGS, PARMS are the same as the arguments of unify. | |
9666 | ARGS contains template arguments from all levels. */ | |
9667 | ||
9668 | static int | |
3a978d72 | 9669 | verify_class_unification (tree targs, tree parms, tree args) |
74601d7c | 9670 | { |
e2005c8d KL |
9671 | parms = tsubst (parms, add_outermost_template_args (args, targs), |
9672 | tf_none, NULL_TREE); | |
9673 | if (parms == error_mark_node) | |
74601d7c KL |
9674 | return 1; |
9675 | ||
e2005c8d | 9676 | return !comp_template_args (parms, INNERMOST_TEMPLATE_ARGS (args)); |
74601d7c KL |
9677 | } |
9678 | ||
4393e105 MM |
9679 | /* PARM is a template class (perhaps with unbound template |
9680 | parameters). ARG is a fully instantiated type. If ARG can be | |
9681 | bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and | |
9682 | TARGS are as for unify. */ | |
fcfb9f96 MM |
9683 | |
9684 | static tree | |
3a978d72 | 9685 | try_class_unification (tree tparms, tree targs, tree parm, tree arg) |
4393e105 | 9686 | { |
4393e105 MM |
9687 | tree copy_of_targs; |
9688 | ||
9689 | if (!CLASSTYPE_TEMPLATE_INFO (arg) | |
68361a03 MM |
9690 | || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg)) |
9691 | != most_general_template (CLASSTYPE_TI_TEMPLATE (parm)))) | |
4393e105 MM |
9692 | return NULL_TREE; |
9693 | ||
9694 | /* We need to make a new template argument vector for the call to | |
9695 | unify. If we used TARGS, we'd clutter it up with the result of | |
9696 | the attempted unification, even if this class didn't work out. | |
9697 | We also don't want to commit ourselves to all the unifications | |
9698 | we've already done, since unification is supposed to be done on | |
9699 | an argument-by-argument basis. In other words, consider the | |
9700 | following pathological case: | |
9701 | ||
9702 | template <int I, int J, int K> | |
9703 | struct S {}; | |
9704 | ||
9705 | template <int I, int J> | |
9706 | struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {}; | |
9707 | ||
9708 | template <int I, int J, int K> | |
9709 | void f(S<I, J, K>, S<I, I, I>); | |
9710 | ||
9711 | void g() { | |
9712 | S<0, 0, 0> s0; | |
9713 | S<0, 1, 2> s2; | |
9714 | ||
9715 | f(s0, s2); | |
9716 | } | |
9717 | ||
9718 | Now, by the time we consider the unification involving `s2', we | |
9719 | already know that we must have `f<0, 0, 0>'. But, even though | |
0e339752 | 9720 | `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid |
4393e105 MM |
9721 | because there are two ways to unify base classes of S<0, 1, 2> |
9722 | with S<I, I, I>. If we kept the already deduced knowledge, we | |
9723 | would reject the possibility I=1. */ | |
f31c0a32 | 9724 | copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs)); |
4393e105 MM |
9725 | |
9726 | /* If unification failed, we're done. */ | |
74601d7c KL |
9727 | if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm), |
9728 | CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE)) | |
4393e105 | 9729 | return NULL_TREE; |
74601d7c KL |
9730 | |
9731 | return arg; | |
4393e105 MM |
9732 | } |
9733 | ||
a3a0fc7f NS |
9734 | /* Given a template type PARM and a class type ARG, find the unique |
9735 | base type in ARG that is an instance of PARM. We do not examine | |
9736 | ARG itself; only its base-classes. If there is not exactly one | |
9737 | appropriate base class, return NULL_TREE. PARM may be the type of | |
9738 | a partial specialization, as well as a plain template type. Used | |
9739 | by unify. */ | |
4393e105 MM |
9740 | |
9741 | static tree | |
a3a0fc7f | 9742 | get_template_base (tree tparms, tree targs, tree parm, tree arg) |
fcfb9f96 | 9743 | { |
a3a0fc7f NS |
9744 | tree rval = NULL_TREE; |
9745 | tree binfo; | |
9746 | ||
9747 | gcc_assert (IS_AGGR_TYPE_CODE (TREE_CODE (arg))); | |
9748 | ||
9749 | binfo = TYPE_BINFO (complete_type (arg)); | |
9750 | if (!binfo) | |
9751 | /* The type could not be completed. */ | |
9752 | return NULL_TREE; | |
fcfb9f96 | 9753 | |
a3a0fc7f NS |
9754 | /* Walk in inheritance graph order. The search order is not |
9755 | important, and this avoids multiple walks of virtual bases. */ | |
9756 | for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo)) | |
fcfb9f96 | 9757 | { |
a3a0fc7f | 9758 | tree r = try_class_unification (tparms, targs, parm, BINFO_TYPE (binfo)); |
fcfb9f96 | 9759 | |
8d83768f NS |
9760 | if (r) |
9761 | { | |
9762 | /* If there is more than one satisfactory baseclass, then: | |
fcfb9f96 | 9763 | |
8d83768f | 9764 | [temp.deduct.call] |
fcfb9f96 | 9765 | |
8d83768f NS |
9766 | If they yield more than one possible deduced A, the type |
9767 | deduction fails. | |
4393e105 | 9768 | |
8d83768f | 9769 | applies. */ |
a3a0fc7f NS |
9770 | if (rval && !same_type_p (r, rval)) |
9771 | return NULL_TREE; | |
8d83768f | 9772 | |
a3a0fc7f | 9773 | rval = r; |
8d83768f | 9774 | } |
fcfb9f96 MM |
9775 | } |
9776 | ||
a3a0fc7f | 9777 | return rval; |
fcfb9f96 MM |
9778 | } |
9779 | ||
db2767b6 MM |
9780 | /* Returns the level of DECL, which declares a template parameter. */ |
9781 | ||
e9659ab0 | 9782 | static int |
3a978d72 | 9783 | template_decl_level (tree decl) |
db2767b6 MM |
9784 | { |
9785 | switch (TREE_CODE (decl)) | |
9786 | { | |
9787 | case TYPE_DECL: | |
9788 | case TEMPLATE_DECL: | |
9789 | return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl)); | |
9790 | ||
9791 | case PARM_DECL: | |
9792 | return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl)); | |
9793 | ||
9794 | default: | |
315fb5db | 9795 | gcc_unreachable (); |
db2767b6 | 9796 | } |
315fb5db | 9797 | return 0; |
db2767b6 MM |
9798 | } |
9799 | ||
830bfa74 MM |
9800 | /* Decide whether ARG can be unified with PARM, considering only the |
9801 | cv-qualifiers of each type, given STRICT as documented for unify. | |
324f9dfb | 9802 | Returns nonzero iff the unification is OK on that basis. */ |
e92cc029 | 9803 | |
e9659ab0 | 9804 | static int |
3a978d72 | 9805 | check_cv_quals_for_unify (int strict, tree arg, tree parm) |
830bfa74 | 9806 | { |
4f2b0fb2 NS |
9807 | int arg_quals = cp_type_quals (arg); |
9808 | int parm_quals = cp_type_quals (parm); | |
9809 | ||
355f774d NS |
9810 | if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM |
9811 | && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL)) | |
4f2b0fb2 | 9812 | { |
2e9ceb77 NS |
9813 | /* Although a CVR qualifier is ignored when being applied to a |
9814 | substituted template parameter ([8.3.2]/1 for example), that | |
9815 | does not apply during deduction [14.8.2.4]/1, (even though | |
9816 | that is not explicitly mentioned, [14.8.2.4]/9 indicates | |
355f774d NS |
9817 | this). Except when we're allowing additional CV qualifiers |
9818 | at the outer level [14.8.2.1]/3,1st bullet. */ | |
2e9ceb77 NS |
9819 | if ((TREE_CODE (arg) == REFERENCE_TYPE |
9820 | || TREE_CODE (arg) == FUNCTION_TYPE | |
9821 | || TREE_CODE (arg) == METHOD_TYPE) | |
9822 | && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE))) | |
9823 | return 0; | |
9824 | ||
9825 | if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM) | |
9826 | && (parm_quals & TYPE_QUAL_RESTRICT)) | |
9827 | return 0; | |
4f2b0fb2 | 9828 | } |
2e9ceb77 | 9829 | |
62e4a758 | 9830 | if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL)) |
4f2b0fb2 | 9831 | && (arg_quals & parm_quals) != parm_quals) |
ef637255 MM |
9832 | return 0; |
9833 | ||
62e4a758 | 9834 | if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL)) |
4f2b0fb2 | 9835 | && (parm_quals & arg_quals) != arg_quals) |
ef637255 MM |
9836 | return 0; |
9837 | ||
ef637255 | 9838 | return 1; |
830bfa74 MM |
9839 | } |
9840 | ||
9841 | /* Takes parameters as for type_unification. Returns 0 if the | |
dc957d14 | 9842 | type deduction succeeds, 1 otherwise. The parameter STRICT is a |
830bfa74 MM |
9843 | bitwise or of the following flags: |
9844 | ||
9845 | UNIFY_ALLOW_NONE: | |
9846 | Require an exact match between PARM and ARG. | |
9847 | UNIFY_ALLOW_MORE_CV_QUAL: | |
028d1f20 NS |
9848 | Allow the deduced ARG to be more cv-qualified (by qualification |
9849 | conversion) than ARG. | |
830bfa74 MM |
9850 | UNIFY_ALLOW_LESS_CV_QUAL: |
9851 | Allow the deduced ARG to be less cv-qualified than ARG. | |
9852 | UNIFY_ALLOW_DERIVED: | |
9853 | Allow the deduced ARG to be a template base class of ARG, | |
9854 | or a pointer to a template base class of the type pointed to by | |
161c12b0 JM |
9855 | ARG. |
9856 | UNIFY_ALLOW_INTEGER: | |
9857 | Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX | |
028d1f20 NS |
9858 | case for more information. |
9859 | UNIFY_ALLOW_OUTER_LEVEL: | |
9860 | This is the outermost level of a deduction. Used to determine validity | |
9861 | of qualification conversions. A valid qualification conversion must | |
9862 | have const qualified pointers leading up to the inner type which | |
9863 | requires additional CV quals, except at the outer level, where const | |
9864 | is not required [conv.qual]. It would be normal to set this flag in | |
62e4a758 NS |
9865 | addition to setting UNIFY_ALLOW_MORE_CV_QUAL. |
9866 | UNIFY_ALLOW_OUTER_MORE_CV_QUAL: | |
9867 | This is the outermost level of a deduction, and PARM can be more CV | |
9868 | qualified at this point. | |
9869 | UNIFY_ALLOW_OUTER_LESS_CV_QUAL: | |
9870 | This is the outermost level of a deduction, and PARM can be less CV | |
8baddbf1 | 9871 | qualified at this point. */ |
830bfa74 | 9872 | |
e9659ab0 | 9873 | static int |
3a978d72 | 9874 | unify (tree tparms, tree targs, tree parm, tree arg, int strict) |
8d08fdba MS |
9875 | { |
9876 | int idx; | |
050367a3 | 9877 | tree targ; |
db2767b6 | 9878 | tree tparm; |
028d1f20 | 9879 | int strict_in = strict; |
8d08fdba MS |
9880 | |
9881 | /* I don't think this will do the right thing with respect to types. | |
9882 | But the only case I've seen it in so far has been array bounds, where | |
9883 | signedness is the only information lost, and I think that will be | |
9884 | okay. */ | |
9885 | while (TREE_CODE (parm) == NOP_EXPR) | |
9886 | parm = TREE_OPERAND (parm, 0); | |
9887 | ||
9888 | if (arg == error_mark_node) | |
9889 | return 1; | |
9890 | if (arg == unknown_type_node) | |
34016c81 JM |
9891 | /* We can't deduce anything from this, but we might get all the |
9892 | template args from other function args. */ | |
9893 | return 0; | |
9894 | ||
db2767b6 | 9895 | /* If PARM uses template parameters, then we can't bail out here, |
6bdb985f | 9896 | even if ARG == PARM, since we won't record unifications for the |
db2767b6 MM |
9897 | template parameters. We might need them if we're trying to |
9898 | figure out which of two things is more specialized. */ | |
9899 | if (arg == parm && !uses_template_parms (parm)) | |
8d08fdba MS |
9900 | return 0; |
9901 | ||
830bfa74 MM |
9902 | /* Immediately reject some pairs that won't unify because of |
9903 | cv-qualification mismatches. */ | |
9904 | if (TREE_CODE (arg) == TREE_CODE (parm) | |
2f939d94 | 9905 | && TYPE_P (arg) |
d0ab7624 NS |
9906 | /* It is the elements of the array which hold the cv quals of an array |
9907 | type, and the elements might be template type parms. We'll check | |
9908 | when we recurse. */ | |
9909 | && TREE_CODE (arg) != ARRAY_TYPE | |
830bfa74 MM |
9910 | /* We check the cv-qualifiers when unifying with template type |
9911 | parameters below. We want to allow ARG `const T' to unify with | |
9912 | PARM `T' for example, when computing which of two templates | |
9913 | is more specialized, for example. */ | |
9914 | && TREE_CODE (arg) != TEMPLATE_TYPE_PARM | |
028d1f20 | 9915 | && !check_cv_quals_for_unify (strict_in, arg, parm)) |
49432171 JM |
9916 | return 1; |
9917 | ||
028d1f20 | 9918 | if (!(strict & UNIFY_ALLOW_OUTER_LEVEL) |
3ea099f1 | 9919 | && TYPE_P (parm) && !CP_TYPE_CONST_P (parm)) |
028d1f20 NS |
9920 | strict &= ~UNIFY_ALLOW_MORE_CV_QUAL; |
9921 | strict &= ~UNIFY_ALLOW_OUTER_LEVEL; | |
9922 | strict &= ~UNIFY_ALLOW_DERIVED; | |
62e4a758 NS |
9923 | strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL; |
9924 | strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL; | |
028d1f20 | 9925 | |
8d08fdba MS |
9926 | switch (TREE_CODE (parm)) |
9927 | { | |
2ca340ae | 9928 | case TYPENAME_TYPE: |
fccef71e | 9929 | case SCOPE_REF: |
b8c6534b | 9930 | case UNBOUND_CLASS_TEMPLATE: |
2ca340ae JM |
9931 | /* In a type which contains a nested-name-specifier, template |
9932 | argument values cannot be deduced for template parameters used | |
9933 | within the nested-name-specifier. */ | |
9934 | return 0; | |
9935 | ||
8d08fdba | 9936 | case TEMPLATE_TYPE_PARM: |
73b0fce8 | 9937 | case TEMPLATE_TEMPLATE_PARM: |
a1281f45 | 9938 | case BOUND_TEMPLATE_TEMPLATE_PARM: |
db2767b6 MM |
9939 | tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0)); |
9940 | ||
9941 | if (TEMPLATE_TYPE_LEVEL (parm) | |
9942 | != template_decl_level (tparm)) | |
9943 | /* The PARM is not one we're trying to unify. Just check | |
9944 | to see if it matches ARG. */ | |
9945 | return (TREE_CODE (arg) == TREE_CODE (parm) | |
3bfdc719 | 9946 | && same_type_p (parm, arg)) ? 0 : 1; |
73b0fce8 | 9947 | idx = TEMPLATE_TYPE_IDX (parm); |
050367a3 | 9948 | targ = TREE_VEC_ELT (targs, idx); |
db2767b6 | 9949 | tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx)); |
050367a3 | 9950 | |
73b0fce8 | 9951 | /* Check for mixed types and values. */ |
db2767b6 MM |
9952 | if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM |
9953 | && TREE_CODE (tparm) != TYPE_DECL) | |
9954 | || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM | |
9955 | && TREE_CODE (tparm) != TEMPLATE_DECL)) | |
73b0fce8 KL |
9956 | return 1; |
9957 | ||
a1281f45 | 9958 | if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM) |
73b0fce8 | 9959 | { |
b429fdf0 KL |
9960 | /* ARG must be constructed from a template class or a template |
9961 | template parameter. */ | |
9962 | if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM | |
9963 | && (TREE_CODE (arg) != RECORD_TYPE || !CLASSTYPE_TEMPLATE_INFO (arg))) | |
a1281f45 | 9964 | return 1; |
73b0fce8 | 9965 | |
a1281f45 KL |
9966 | { |
9967 | tree parmtmpl = TYPE_TI_TEMPLATE (parm); | |
9968 | tree parmvec = TYPE_TI_ARGS (parm); | |
6df91b00 | 9969 | tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg)); |
a1281f45 | 9970 | tree argtmplvec |
b429fdf0 | 9971 | = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_TI_TEMPLATE (arg)); |
a1281f45 | 9972 | int i; |
73b0fce8 | 9973 | |
a1281f45 KL |
9974 | /* The parameter and argument roles have to be switched here |
9975 | in order to handle default arguments properly. For example, | |
9976 | template<template <class> class TT> void f(TT<int>) | |
9977 | should be able to accept vector<int> which comes from | |
9978 | template <class T, class Allocator = allocator> | |
9979 | class vector. */ | |
9980 | ||
ee3071ef NS |
9981 | if (coerce_template_parms (argtmplvec, parmvec, parmtmpl, 0, 1) |
9982 | == error_mark_node) | |
a1281f45 | 9983 | return 1; |
73b0fce8 | 9984 | |
a1281f45 KL |
9985 | /* Deduce arguments T, i from TT<T> or TT<i>. |
9986 | We check each element of PARMVEC and ARGVEC individually | |
9987 | rather than the whole TREE_VEC since they can have | |
9988 | different number of elements. */ | |
6b9b6b15 | 9989 | |
a1281f45 KL |
9990 | for (i = 0; i < TREE_VEC_LENGTH (parmvec); ++i) |
9991 | { | |
6df91b00 KL |
9992 | if (unify (tparms, targs, |
9993 | TREE_VEC_ELT (parmvec, i), | |
a1281f45 KL |
9994 | TREE_VEC_ELT (argvec, i), |
9995 | UNIFY_ALLOW_NONE)) | |
9996 | return 1; | |
73b0fce8 | 9997 | } |
a1281f45 | 9998 | } |
b429fdf0 | 9999 | arg = TYPE_TI_TEMPLATE (arg); |
a1281f45 KL |
10000 | |
10001 | /* Fall through to deduce template name. */ | |
10002 | } | |
10003 | ||
10004 | if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM | |
10005 | || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM) | |
10006 | { | |
10007 | /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */ | |
10008 | ||
10009 | /* Simple cases: Value already set, does match or doesn't. */ | |
10010 | if (targ != NULL_TREE && template_args_equal (targ, arg)) | |
10011 | return 0; | |
10012 | else if (targ) | |
10013 | return 1; | |
db2767b6 MM |
10014 | } |
10015 | else | |
10016 | { | |
830bfa74 MM |
10017 | /* If PARM is `const T' and ARG is only `int', we don't have |
10018 | a match unless we are allowing additional qualification. | |
10019 | If ARG is `const int' and PARM is just `T' that's OK; | |
10020 | that binds `const int' to `T'. */ | |
028d1f20 | 10021 | if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL, |
830bfa74 | 10022 | arg, parm)) |
db2767b6 MM |
10023 | return 1; |
10024 | ||
830bfa74 MM |
10025 | /* Consider the case where ARG is `const volatile int' and |
10026 | PARM is `const T'. Then, T should be `volatile int'. */ | |
c2ea3a40 NS |
10027 | arg = cp_build_qualified_type_real |
10028 | (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none); | |
adecb3f4 MM |
10029 | if (arg == error_mark_node) |
10030 | return 1; | |
73b0fce8 | 10031 | |
a1281f45 KL |
10032 | /* Simple cases: Value already set, does match or doesn't. */ |
10033 | if (targ != NULL_TREE && same_type_p (targ, arg)) | |
10034 | return 0; | |
10035 | else if (targ) | |
10036 | return 1; | |
61cd552e | 10037 | |
94fc547c MM |
10038 | /* Make sure that ARG is not a variable-sized array. (Note |
10039 | that were talking about variable-sized arrays (like | |
10040 | `int[n]'), rather than arrays of unknown size (like | |
10041 | `int[]').) We'll get very confused by such a type since | |
10042 | the bound of the array will not be computable in an | |
10043 | instantiation. Besides, such types are not allowed in | |
10044 | ISO C++, so we can do as we please here. */ | |
5377d5ba | 10045 | if (variably_modified_type_p (arg, NULL_TREE)) |
94fc547c MM |
10046 | return 1; |
10047 | } | |
61cd552e | 10048 | |
050367a3 | 10049 | TREE_VEC_ELT (targs, idx) = arg; |
73b0fce8 KL |
10050 | return 0; |
10051 | ||
f84b4be9 | 10052 | case TEMPLATE_PARM_INDEX: |
db2767b6 MM |
10053 | tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0)); |
10054 | ||
10055 | if (TEMPLATE_PARM_LEVEL (parm) | |
10056 | != template_decl_level (tparm)) | |
10057 | /* The PARM is not one we're trying to unify. Just check | |
10058 | to see if it matches ARG. */ | |
c8a209ca NS |
10059 | return !(TREE_CODE (arg) == TREE_CODE (parm) |
10060 | && cp_tree_equal (parm, arg)); | |
db2767b6 | 10061 | |
f84b4be9 | 10062 | idx = TEMPLATE_PARM_IDX (parm); |
050367a3 | 10063 | targ = TREE_VEC_ELT (targs, idx); |
db2767b6 | 10064 | |
050367a3 | 10065 | if (targ) |
c8a209ca | 10066 | return !cp_tree_equal (targ, arg); |
8d08fdba | 10067 | |
161c12b0 JM |
10068 | /* [temp.deduct.type] If, in the declaration of a function template |
10069 | with a non-type template-parameter, the non-type | |
10070 | template-parameter is used in an expression in the function | |
10071 | parameter-list and, if the corresponding template-argument is | |
10072 | deduced, the template-argument type shall match the type of the | |
10073 | template-parameter exactly, except that a template-argument | |
d7c4edd0 NS |
10074 | deduced from an array bound may be of any integral type. |
10075 | The non-type parameter might use already deduced type parameters. */ | |
bd0d5d4a | 10076 | tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE); |
e2005c8d KL |
10077 | if (!TREE_TYPE (arg)) |
10078 | /* Template-parameter dependent expression. Just accept it for now. | |
10079 | It will later be processed in convert_template_argument. */ | |
10080 | ; | |
10081 | else if (same_type_p (TREE_TYPE (arg), tparm)) | |
10082 | /* OK */; | |
161c12b0 | 10083 | else if ((strict & UNIFY_ALLOW_INTEGER) |
bd0d5d4a JM |
10084 | && (TREE_CODE (tparm) == INTEGER_TYPE |
10085 | || TREE_CODE (tparm) == BOOLEAN_TYPE)) | |
8baddbf1 MM |
10086 | /* Convert the ARG to the type of PARM; the deduced non-type |
10087 | template argument must exactly match the types of the | |
10088 | corresponding parameter. */ | |
10089 | arg = fold (build_nop (TREE_TYPE (parm), arg)); | |
bd0d5d4a JM |
10090 | else if (uses_template_parms (tparm)) |
10091 | /* We haven't deduced the type of this parameter yet. Try again | |
10092 | later. */ | |
10093 | return 0; | |
161c12b0 JM |
10094 | else |
10095 | return 1; | |
10096 | ||
2a1e9fdd | 10097 | TREE_VEC_ELT (targs, idx) = arg; |
8d08fdba MS |
10098 | return 0; |
10099 | ||
28e8f3a0 GB |
10100 | case PTRMEM_CST: |
10101 | { | |
10102 | /* A pointer-to-member constant can be unified only with | |
10103 | another constant. */ | |
10104 | if (TREE_CODE (arg) != PTRMEM_CST) | |
10105 | return 1; | |
10106 | ||
10107 | /* Just unify the class member. It would be useless (and possibly | |
10108 | wrong, depending on the strict flags) to unify also | |
10109 | PTRMEM_CST_CLASS, because we want to be sure that both parm and | |
10110 | arg refer to the same variable, even if through different | |
10111 | classes. For instance: | |
10112 | ||
10113 | struct A { int x; }; | |
10114 | struct B : A { }; | |
10115 | ||
10116 | Unification of &A::x and &B::x must succeed. */ | |
10117 | return unify (tparms, targs, PTRMEM_CST_MEMBER (parm), | |
10118 | PTRMEM_CST_MEMBER (arg), strict); | |
10119 | } | |
10120 | ||
8d08fdba | 10121 | case POINTER_TYPE: |
830bfa74 | 10122 | { |
830bfa74 MM |
10123 | if (TREE_CODE (arg) != POINTER_TYPE) |
10124 | return 1; | |
10125 | ||
10126 | /* [temp.deduct.call] | |
10127 | ||
10128 | A can be another pointer or pointer to member type that can | |
10129 | be converted to the deduced A via a qualification | |
10130 | conversion (_conv.qual_). | |
10131 | ||
10132 | We pass down STRICT here rather than UNIFY_ALLOW_NONE. | |
10133 | This will allow for additional cv-qualification of the | |
028d1f20 | 10134 | pointed-to types if appropriate. */ |
830bfa74 | 10135 | |
028d1f20 | 10136 | if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE) |
830bfa74 MM |
10137 | /* The derived-to-base conversion only persists through one |
10138 | level of pointers. */ | |
028d1f20 | 10139 | strict |= (strict_in & UNIFY_ALLOW_DERIVED); |
830bfa74 | 10140 | |
a7a64a77 | 10141 | return unify (tparms, targs, TREE_TYPE (parm), |
028d1f20 | 10142 | TREE_TYPE (arg), strict); |
830bfa74 | 10143 | } |
8d08fdba MS |
10144 | |
10145 | case REFERENCE_TYPE: | |
830bfa74 MM |
10146 | if (TREE_CODE (arg) != REFERENCE_TYPE) |
10147 | return 1; | |
10148 | return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg), | |
028d1f20 | 10149 | strict & UNIFY_ALLOW_MORE_CV_QUAL); |
8d08fdba MS |
10150 | |
10151 | case ARRAY_TYPE: | |
10152 | if (TREE_CODE (arg) != ARRAY_TYPE) | |
10153 | return 1; | |
3042d5be MM |
10154 | if ((TYPE_DOMAIN (parm) == NULL_TREE) |
10155 | != (TYPE_DOMAIN (arg) == NULL_TREE)) | |
10156 | return 1; | |
8baddbf1 MM |
10157 | if (TYPE_DOMAIN (parm) != NULL_TREE) |
10158 | { | |
10159 | tree parm_max; | |
10160 | tree arg_max; | |
10161 | ||
10162 | parm_max = TYPE_MAX_VALUE (TYPE_DOMAIN (parm)); | |
10163 | arg_max = TYPE_MAX_VALUE (TYPE_DOMAIN (arg)); | |
10164 | ||
10165 | /* Our representation of array types uses "N - 1" as the | |
10166 | TYPE_MAX_VALUE for an array with "N" elements, if "N" is | |
10167 | not an integer constant. */ | |
10168 | if (TREE_CODE (parm_max) == MINUS_EXPR) | |
10169 | { | |
10170 | arg_max = fold (build2 (PLUS_EXPR, | |
10171 | integer_type_node, | |
10172 | arg_max, | |
10173 | TREE_OPERAND (parm_max, 1))); | |
10174 | parm_max = TREE_OPERAND (parm_max, 0); | |
10175 | } | |
10176 | ||
10177 | if (unify (tparms, targs, parm_max, arg_max, UNIFY_ALLOW_INTEGER)) | |
10178 | return 1; | |
10179 | } | |
830bfa74 | 10180 | return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg), |
712467a4 | 10181 | strict & UNIFY_ALLOW_MORE_CV_QUAL); |
8d08fdba MS |
10182 | |
10183 | case REAL_TYPE: | |
37c46b43 | 10184 | case COMPLEX_TYPE: |
c00996a3 | 10185 | case VECTOR_TYPE: |
8d08fdba | 10186 | case INTEGER_TYPE: |
42976354 | 10187 | case BOOLEAN_TYPE: |
3590f0a6 | 10188 | case ENUMERAL_TYPE: |
5ad5a526 | 10189 | case VOID_TYPE: |
f376e137 MS |
10190 | if (TREE_CODE (arg) != TREE_CODE (parm)) |
10191 | return 1; | |
8baddbf1 | 10192 | |
9edc3913 | 10193 | /* We have already checked cv-qualification at the top of the |
514a1f18 | 10194 | function. */ |
8baddbf1 | 10195 | if (!same_type_ignoring_top_level_qualifiers_p (arg, parm)) |
ca79f85d JM |
10196 | return 1; |
10197 | ||
8d08fdba MS |
10198 | /* As far as unification is concerned, this wins. Later checks |
10199 | will invalidate it if necessary. */ | |
10200 | return 0; | |
10201 | ||
10202 | /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */ | |
bd6dd845 | 10203 | /* Type INTEGER_CST can come from ordinary constant template args. */ |
8d08fdba | 10204 | case INTEGER_CST: |
bd6dd845 MS |
10205 | while (TREE_CODE (arg) == NOP_EXPR) |
10206 | arg = TREE_OPERAND (arg, 0); | |
10207 | ||
8d08fdba MS |
10208 | if (TREE_CODE (arg) != INTEGER_CST) |
10209 | return 1; | |
10210 | return !tree_int_cst_equal (parm, arg); | |
10211 | ||
8d08fdba MS |
10212 | case TREE_VEC: |
10213 | { | |
10214 | int i; | |
10215 | if (TREE_CODE (arg) != TREE_VEC) | |
10216 | return 1; | |
10217 | if (TREE_VEC_LENGTH (parm) != TREE_VEC_LENGTH (arg)) | |
10218 | return 1; | |
0dc09a61 | 10219 | for (i = 0; i < TREE_VEC_LENGTH (parm); ++i) |
830bfa74 | 10220 | if (unify (tparms, targs, |
8d08fdba | 10221 | TREE_VEC_ELT (parm, i), TREE_VEC_ELT (arg, i), |
4393e105 | 10222 | UNIFY_ALLOW_NONE)) |
8d08fdba MS |
10223 | return 1; |
10224 | return 0; | |
10225 | } | |
10226 | ||
8d08fdba | 10227 | case RECORD_TYPE: |
f181d4ae | 10228 | case UNION_TYPE: |
f181d4ae | 10229 | if (TREE_CODE (arg) != TREE_CODE (parm)) |
a4443a08 | 10230 | return 1; |
830bfa74 | 10231 | |
a7a64a77 MM |
10232 | if (TYPE_PTRMEMFUNC_P (parm)) |
10233 | { | |
10234 | if (!TYPE_PTRMEMFUNC_P (arg)) | |
10235 | return 1; | |
10236 | ||
10237 | return unify (tparms, targs, | |
10238 | TYPE_PTRMEMFUNC_FN_TYPE (parm), | |
10239 | TYPE_PTRMEMFUNC_FN_TYPE (arg), | |
10240 | strict); | |
10241 | } | |
10242 | ||
5db698f6 | 10243 | if (CLASSTYPE_TEMPLATE_INFO (parm)) |
5566b478 | 10244 | { |
6467930b | 10245 | tree t = NULL_TREE; |
4393e105 | 10246 | |
028d1f20 | 10247 | if (strict_in & UNIFY_ALLOW_DERIVED) |
4393e105 MM |
10248 | { |
10249 | /* First, we try to unify the PARM and ARG directly. */ | |
10250 | t = try_class_unification (tparms, targs, | |
10251 | parm, arg); | |
10252 | ||
10253 | if (!t) | |
10254 | { | |
10255 | /* Fallback to the special case allowed in | |
10256 | [temp.deduct.call]: | |
10257 | ||
10258 | If P is a class, and P has the form | |
10259 | template-id, then A can be a derived class of | |
10260 | the deduced A. Likewise, if P is a pointer to | |
10261 | a class of the form template-id, A can be a | |
10262 | pointer to a derived class pointed to by the | |
10263 | deduced A. */ | |
8d83768f | 10264 | t = get_template_base (tparms, targs, parm, arg); |
4393e105 | 10265 | |
8d83768f | 10266 | if (!t) |
4393e105 MM |
10267 | return 1; |
10268 | } | |
10269 | } | |
6df47b06 | 10270 | else if (CLASSTYPE_TEMPLATE_INFO (arg) |
9fbf56f7 MM |
10271 | && (CLASSTYPE_TI_TEMPLATE (parm) |
10272 | == CLASSTYPE_TI_TEMPLATE (arg))) | |
6df47b06 MM |
10273 | /* Perhaps PARM is something like S<U> and ARG is S<int>. |
10274 | Then, we should unify `int' and `U'. */ | |
6467930b | 10275 | t = arg; |
4393e105 | 10276 | else |
dc957d14 | 10277 | /* There's no chance of unification succeeding. */ |
5566b478 | 10278 | return 1; |
6467930b | 10279 | |
830bfa74 | 10280 | return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm), |
4393e105 | 10281 | CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE); |
5566b478 | 10282 | } |
9edc3913 | 10283 | else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg)) |
5566b478 | 10284 | return 1; |
a4443a08 | 10285 | return 0; |
8d08fdba MS |
10286 | |
10287 | case METHOD_TYPE: | |
8d08fdba | 10288 | case FUNCTION_TYPE: |
830bfa74 | 10289 | if (TREE_CODE (arg) != TREE_CODE (parm)) |
8d08fdba | 10290 | return 1; |
830bfa74 MM |
10291 | |
10292 | if (unify (tparms, targs, TREE_TYPE (parm), | |
4393e105 | 10293 | TREE_TYPE (arg), UNIFY_ALLOW_NONE)) |
28cbf42c | 10294 | return 1; |
386b8a85 | 10295 | return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm), |
3b3ba9f0 | 10296 | TYPE_ARG_TYPES (arg), 1, |
e5214479 | 10297 | DEDUCE_EXACT, 0, -1); |
a4443a08 MS |
10298 | |
10299 | case OFFSET_TYPE: | |
10300 | if (TREE_CODE (arg) != OFFSET_TYPE) | |
10301 | return 1; | |
830bfa74 | 10302 | if (unify (tparms, targs, TYPE_OFFSET_BASETYPE (parm), |
4393e105 | 10303 | TYPE_OFFSET_BASETYPE (arg), UNIFY_ALLOW_NONE)) |
a4443a08 | 10304 | return 1; |
830bfa74 | 10305 | return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg), |
4393e105 | 10306 | strict); |
a4443a08 | 10307 | |
f62dbf03 | 10308 | case CONST_DECL: |
a723baf1 MM |
10309 | if (DECL_TEMPLATE_PARM_P (parm)) |
10310 | return unify (tparms, targs, DECL_INITIAL (parm), arg, strict); | |
8a784e4a | 10311 | if (arg != integral_constant_value (parm)) |
f62dbf03 JM |
10312 | return 1; |
10313 | return 0; | |
10314 | ||
28e8f3a0 | 10315 | case FIELD_DECL: |
027905b4 KL |
10316 | case TEMPLATE_DECL: |
10317 | /* Matched cases are handled by the ARG == PARM test above. */ | |
10318 | return 1; | |
10319 | ||
8d08fdba | 10320 | default: |
6615c446 | 10321 | gcc_assert (EXPR_P (parm)); |
98ddffc1 NS |
10322 | |
10323 | /* We must be looking at an expression. This can happen with | |
10324 | something like: | |
4393e105 | 10325 | |
98ddffc1 NS |
10326 | template <int I> |
10327 | void foo(S<I>, S<I + 2>); | |
050367a3 | 10328 | |
98ddffc1 | 10329 | This is a "nondeduced context": |
050367a3 | 10330 | |
98ddffc1 | 10331 | [deduct.type] |
4393e105 | 10332 | |
98ddffc1 | 10333 | The nondeduced contexts are: |
050367a3 | 10334 | |
98ddffc1 NS |
10335 | --A type that is a template-id in which one or more of |
10336 | the template-arguments is an expression that references | |
10337 | a template-parameter. | |
050367a3 | 10338 | |
98ddffc1 NS |
10339 | In these cases, we assume deduction succeeded, but don't |
10340 | actually infer any unifications. */ | |
74601d7c | 10341 | |
98ddffc1 NS |
10342 | if (!uses_template_parms (parm) |
10343 | && !template_args_equal (parm, arg)) | |
10344 | return 1; | |
10345 | else | |
10346 | return 0; | |
8d08fdba MS |
10347 | } |
10348 | } | |
8d08fdba | 10349 | \f |
4684cd27 MM |
10350 | /* Note that DECL can be defined in this translation unit, if |
10351 | required. */ | |
10352 | ||
10353 | static void | |
10354 | mark_definable (tree decl) | |
10355 | { | |
10356 | tree clone; | |
10357 | DECL_NOT_REALLY_EXTERN (decl) = 1; | |
10358 | FOR_EACH_CLONE (clone, decl) | |
10359 | DECL_NOT_REALLY_EXTERN (clone) = 1; | |
10360 | } | |
10361 | ||
03d0f4af | 10362 | /* Called if RESULT is explicitly instantiated, or is a member of an |
4684cd27 | 10363 | explicitly instantiated class. */ |
03d0f4af | 10364 | |
faae18ab | 10365 | void |
3a978d72 | 10366 | mark_decl_instantiated (tree result, int extern_p) |
faae18ab | 10367 | { |
415c974c | 10368 | SET_DECL_EXPLICIT_INSTANTIATION (result); |
3ae18eaf | 10369 | |
1f6f0cb6 MM |
10370 | /* If this entity has already been written out, it's too late to |
10371 | make any modifications. */ | |
10372 | if (TREE_ASM_WRITTEN (result)) | |
10373 | return; | |
10374 | ||
10375 | if (TREE_CODE (result) != FUNCTION_DECL) | |
10376 | /* The TREE_PUBLIC flag for function declarations will have been | |
10377 | set correctly by tsubst. */ | |
10378 | TREE_PUBLIC (result) = 1; | |
10379 | ||
346eeb15 JM |
10380 | /* This might have been set by an earlier implicit instantiation. */ |
10381 | DECL_COMDAT (result) = 0; | |
10382 | ||
4684cd27 MM |
10383 | if (extern_p) |
10384 | DECL_NOT_REALLY_EXTERN (result) = 0; | |
10385 | else | |
faae18ab | 10386 | { |
4684cd27 | 10387 | mark_definable (result); |
1a408d07 JM |
10388 | /* Always make artificials weak. */ |
10389 | if (DECL_ARTIFICIAL (result) && flag_weak) | |
10390 | comdat_linkage (result); | |
a7d87521 JM |
10391 | /* For WIN32 we also want to put explicit instantiations in |
10392 | linkonce sections. */ | |
1a408d07 | 10393 | else if (TREE_PUBLIC (result)) |
b385c841 | 10394 | maybe_make_one_only (result); |
faae18ab | 10395 | } |
4684cd27 MM |
10396 | |
10397 | /* If EXTERN_P, then this function will not be emitted -- unless | |
10398 | followed by an explicit instantiation, at which point its linkage | |
10399 | will be adjusted. If !EXTERN_P, then this function will be | |
10400 | emitted here. In neither circumstance do we want | |
10401 | import_export_decl to adjust the linkage. */ | |
10402 | DECL_INTERFACE_KNOWN (result) = 1; | |
faae18ab MS |
10403 | } |
10404 | ||
e5214479 | 10405 | /* Given two function templates PAT1 and PAT2, return: |
6467930b | 10406 | |
62e4a758 NS |
10407 | DEDUCE should be DEDUCE_EXACT or DEDUCE_ORDER. |
10408 | ||
6467930b MS |
10409 | 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order]. |
10410 | -1 if PAT2 is more specialized than PAT1. | |
e5214479 JM |
10411 | 0 if neither is more specialized. |
10412 | ||
10413 | LEN is passed through to fn_type_unification. */ | |
6467930b MS |
10414 | |
10415 | int | |
3a978d72 | 10416 | more_specialized (tree pat1, tree pat2, int deduce, int len) |
6467930b | 10417 | { |
98c1c668 | 10418 | tree targs; |
73aad9b9 | 10419 | int winner = 0; |
6467930b | 10420 | |
35c18a20 MM |
10421 | /* If template argument deduction succeeds, we substitute the |
10422 | resulting arguments into non-deduced contexts. While doing that, | |
10423 | we must be aware that we may encounter dependent types. */ | |
10424 | ++processing_template_decl; | |
62e4a758 NS |
10425 | targs = get_bindings_real (pat1, DECL_TEMPLATE_RESULT (pat2), |
10426 | NULL_TREE, 0, deduce, len); | |
73aad9b9 | 10427 | if (targs) |
36a117a5 | 10428 | --winner; |
6467930b | 10429 | |
62e4a758 NS |
10430 | targs = get_bindings_real (pat2, DECL_TEMPLATE_RESULT (pat1), |
10431 | NULL_TREE, 0, deduce, len); | |
73aad9b9 | 10432 | if (targs) |
36a117a5 | 10433 | ++winner; |
35c18a20 | 10434 | --processing_template_decl; |
6467930b | 10435 | |
73aad9b9 JM |
10436 | return winner; |
10437 | } | |
6467930b | 10438 | |
73aad9b9 | 10439 | /* Given two class template specialization list nodes PAT1 and PAT2, return: |
6467930b | 10440 | |
73aad9b9 JM |
10441 | 1 if PAT1 is more specialized than PAT2 as described in [temp.class.order]. |
10442 | -1 if PAT2 is more specialized than PAT1. | |
55ece1b3 KL |
10443 | 0 if neither is more specialized. |
10444 | ||
10445 | FULL_ARGS is the full set of template arguments that triggers this | |
10446 | partial ordering. */ | |
73aad9b9 JM |
10447 | |
10448 | int | |
55ece1b3 | 10449 | more_specialized_class (tree pat1, tree pat2, tree full_args) |
73aad9b9 JM |
10450 | { |
10451 | tree targs; | |
10452 | int winner = 0; | |
10453 | ||
baa49730 GB |
10454 | /* Just like what happens for functions, if we are ordering between |
10455 | different class template specializations, we may encounter dependent | |
10456 | types in the arguments, and we need our dependency check functions | |
10457 | to behave correctly. */ | |
10458 | ++processing_template_decl; | |
36a117a5 | 10459 | targs = get_class_bindings (TREE_VALUE (pat1), TREE_PURPOSE (pat1), |
55ece1b3 | 10460 | add_outermost_template_args (full_args, TREE_PURPOSE (pat2))); |
73aad9b9 JM |
10461 | if (targs) |
10462 | --winner; | |
10463 | ||
36a117a5 | 10464 | targs = get_class_bindings (TREE_VALUE (pat2), TREE_PURPOSE (pat2), |
55ece1b3 | 10465 | add_outermost_template_args (full_args, TREE_PURPOSE (pat1))); |
73aad9b9 | 10466 | if (targs) |
6467930b | 10467 | ++winner; |
baa49730 | 10468 | --processing_template_decl; |
6467930b MS |
10469 | |
10470 | return winner; | |
10471 | } | |
73aad9b9 JM |
10472 | |
10473 | /* Return the template arguments that will produce the function signature | |
e1467ff2 | 10474 | DECL from the function template FN, with the explicit template |
76b9a14d | 10475 | arguments EXPLICIT_ARGS. If CHECK_RETTYPE is 1, the return type must |
4393e105 | 10476 | also match. Return NULL_TREE if no satisfactory arguments could be |
62e4a758 | 10477 | found. DEDUCE and LEN are passed through to fn_type_unification. */ |
e5214479 | 10478 | |
76b9a14d | 10479 | static tree |
3a978d72 NN |
10480 | get_bindings_real (tree fn, |
10481 | tree decl, | |
10482 | tree explicit_args, | |
10483 | int check_rettype, | |
10484 | int deduce, | |
10485 | int len) | |
73aad9b9 | 10486 | { |
98c1c668 | 10487 | int ntparms = DECL_NTPARMS (fn); |
f31c0a32 | 10488 | tree targs = make_tree_vec (ntparms); |
4393e105 | 10489 | tree decl_type; |
03017874 | 10490 | tree decl_arg_types; |
98c1c668 JM |
10491 | int i; |
10492 | ||
4393e105 MM |
10493 | /* Substitute the explicit template arguments into the type of DECL. |
10494 | The call to fn_type_unification will handle substitution into the | |
10495 | FN. */ | |
10496 | decl_type = TREE_TYPE (decl); | |
10497 | if (explicit_args && uses_template_parms (decl_type)) | |
10498 | { | |
10499 | tree tmpl; | |
10500 | tree converted_args; | |
10501 | ||
10502 | if (DECL_TEMPLATE_INFO (decl)) | |
10503 | tmpl = DECL_TI_TEMPLATE (decl); | |
10504 | else | |
0e339752 | 10505 | /* We can get here for some invalid specializations. */ |
4393e105 MM |
10506 | return NULL_TREE; |
10507 | ||
10508 | converted_args | |
10509 | = (coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl), | |
10510 | explicit_args, NULL_TREE, | |
c2ea3a40 | 10511 | tf_none, /*require_all_arguments=*/0)); |
4393e105 MM |
10512 | if (converted_args == error_mark_node) |
10513 | return NULL_TREE; | |
10514 | ||
c2ea3a40 | 10515 | decl_type = tsubst (decl_type, converted_args, tf_none, NULL_TREE); |
4393e105 MM |
10516 | if (decl_type == error_mark_node) |
10517 | return NULL_TREE; | |
10518 | } | |
10519 | ||
10520 | decl_arg_types = TYPE_ARG_TYPES (decl_type); | |
e5214479 JM |
10521 | /* Never do unification on the 'this' parameter. */ |
10522 | if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)) | |
03017874 | 10523 | decl_arg_types = TREE_CHAIN (decl_arg_types); |
d7684f2d | 10524 | |
e1467ff2 | 10525 | i = fn_type_unification (fn, explicit_args, targs, |
03017874 | 10526 | decl_arg_types, |
8d3631f8 NS |
10527 | (check_rettype || DECL_CONV_FN_P (fn) |
10528 | ? TREE_TYPE (decl_type) : NULL_TREE), | |
62e4a758 | 10529 | deduce, len); |
98c1c668 | 10530 | |
76b9a14d JM |
10531 | if (i != 0) |
10532 | return NULL_TREE; | |
10533 | ||
76b9a14d JM |
10534 | return targs; |
10535 | } | |
10536 | ||
10537 | /* For most uses, we want to check the return type. */ | |
10538 | ||
eb1dfbb2 | 10539 | static tree |
3a978d72 | 10540 | get_bindings (tree fn, tree decl, tree explicit_args) |
76b9a14d | 10541 | { |
62e4a758 | 10542 | return get_bindings_real (fn, decl, explicit_args, 1, DEDUCE_EXACT, -1); |
76b9a14d JM |
10543 | } |
10544 | ||
e5214479 JM |
10545 | /* But for resolve_overloaded_unification, we only care about the parameter |
10546 | types. */ | |
76b9a14d JM |
10547 | |
10548 | static tree | |
3a978d72 | 10549 | get_bindings_overload (tree fn, tree decl, tree explicit_args) |
76b9a14d | 10550 | { |
62e4a758 | 10551 | return get_bindings_real (fn, decl, explicit_args, 0, DEDUCE_EXACT, -1); |
73aad9b9 JM |
10552 | } |
10553 | ||
36a117a5 MM |
10554 | /* Return the innermost template arguments that, when applied to a |
10555 | template specialization whose innermost template parameters are | |
9471d3e2 | 10556 | TPARMS, and whose specialization arguments are PARMS, yield the |
36a117a5 MM |
10557 | ARGS. |
10558 | ||
10559 | For example, suppose we have: | |
10560 | ||
10561 | template <class T, class U> struct S {}; | |
10562 | template <class T> struct S<T*, int> {}; | |
10563 | ||
10564 | Then, suppose we want to get `S<double*, int>'. The TPARMS will be | |
10565 | {T}, the PARMS will be {T*, int} and the ARGS will be {double*, | |
10566 | int}. The resulting vector will be {double}, indicating that `T' | |
10567 | is bound to `double'. */ | |
10568 | ||
bd6dd845 | 10569 | static tree |
3a978d72 | 10570 | get_class_bindings (tree tparms, tree parms, tree args) |
73aad9b9 | 10571 | { |
3b3ba9f0 | 10572 | int i, ntparms = TREE_VEC_LENGTH (tparms); |
f31c0a32 | 10573 | tree vec = make_tree_vec (ntparms); |
73aad9b9 | 10574 | |
74601d7c KL |
10575 | if (unify (tparms, vec, parms, INNERMOST_TEMPLATE_ARGS (args), |
10576 | UNIFY_ALLOW_NONE)) | |
fcfb9f96 | 10577 | return NULL_TREE; |
73aad9b9 JM |
10578 | |
10579 | for (i = 0; i < ntparms; ++i) | |
10580 | if (! TREE_VEC_ELT (vec, i)) | |
10581 | return NULL_TREE; | |
10582 | ||
74601d7c KL |
10583 | if (verify_class_unification (vec, parms, args)) |
10584 | return NULL_TREE; | |
10585 | ||
73aad9b9 JM |
10586 | return vec; |
10587 | } | |
10588 | ||
104bf76a MM |
10589 | /* In INSTANTIATIONS is a list of <INSTANTIATION, TEMPLATE> pairs. |
10590 | Pick the most specialized template, and return the corresponding | |
10591 | instantiation, or if there is no corresponding instantiation, the | |
e5214479 JM |
10592 | template itself. If there is no most specialized template, |
10593 | error_mark_node is returned. If there are no templates at all, | |
10594 | NULL_TREE is returned. */ | |
73aad9b9 JM |
10595 | |
10596 | tree | |
3a978d72 | 10597 | most_specialized_instantiation (tree instantiations) |
73aad9b9 | 10598 | { |
104bf76a | 10599 | tree fn, champ; |
73aad9b9 JM |
10600 | int fate; |
10601 | ||
104bf76a | 10602 | if (!instantiations) |
73aad9b9 JM |
10603 | return NULL_TREE; |
10604 | ||
104bf76a MM |
10605 | champ = instantiations; |
10606 | for (fn = TREE_CHAIN (instantiations); fn; fn = TREE_CHAIN (fn)) | |
73aad9b9 | 10607 | { |
62e4a758 NS |
10608 | fate = more_specialized (TREE_VALUE (champ), TREE_VALUE (fn), |
10609 | DEDUCE_EXACT, -1); | |
73aad9b9 JM |
10610 | if (fate == 1) |
10611 | ; | |
10612 | else | |
10613 | { | |
10614 | if (fate == 0) | |
10615 | { | |
10616 | fn = TREE_CHAIN (fn); | |
10617 | if (! fn) | |
10618 | return error_mark_node; | |
10619 | } | |
104bf76a | 10620 | champ = fn; |
73aad9b9 JM |
10621 | } |
10622 | } | |
10623 | ||
104bf76a | 10624 | for (fn = instantiations; fn && fn != champ; fn = TREE_CHAIN (fn)) |
73aad9b9 | 10625 | { |
62e4a758 NS |
10626 | fate = more_specialized (TREE_VALUE (champ), TREE_VALUE (fn), |
10627 | DEDUCE_EXACT, -1); | |
73aad9b9 JM |
10628 | if (fate != 1) |
10629 | return error_mark_node; | |
10630 | } | |
10631 | ||
104bf76a MM |
10632 | return TREE_PURPOSE (champ) ? TREE_PURPOSE (champ) : TREE_VALUE (champ); |
10633 | } | |
10634 | ||
10635 | /* Return the most specialized of the list of templates in FNS that can | |
10636 | produce an instantiation matching DECL, given the explicit template | |
10637 | arguments EXPLICIT_ARGS. */ | |
10638 | ||
e9659ab0 | 10639 | static tree |
3a978d72 | 10640 | most_specialized (tree fns, tree decl, tree explicit_args) |
104bf76a MM |
10641 | { |
10642 | tree candidates = NULL_TREE; | |
10643 | tree fn, args; | |
10644 | ||
10645 | for (fn = fns; fn; fn = TREE_CHAIN (fn)) | |
10646 | { | |
10647 | tree candidate = TREE_VALUE (fn); | |
10648 | ||
10649 | args = get_bindings (candidate, decl, explicit_args); | |
10650 | if (args) | |
e1b3e07d | 10651 | candidates = tree_cons (NULL_TREE, candidate, candidates); |
104bf76a MM |
10652 | } |
10653 | ||
e5214479 | 10654 | return most_specialized_instantiation (candidates); |
73aad9b9 JM |
10655 | } |
10656 | ||
36a117a5 | 10657 | /* If DECL is a specialization of some template, return the most |
f9a7ae04 MM |
10658 | general such template. Otherwise, returns NULL_TREE. |
10659 | ||
10660 | For example, given: | |
36a117a5 MM |
10661 | |
10662 | template <class T> struct S { template <class U> void f(U); }; | |
10663 | ||
10664 | if TMPL is `template <class U> void S<int>::f(U)' this will return | |
10665 | the full template. This function will not trace past partial | |
10666 | specializations, however. For example, given in addition: | |
10667 | ||
10668 | template <class T> struct S<T*> { template <class U> void f(U); }; | |
10669 | ||
10670 | if TMPL is `template <class U> void S<int*>::f(U)' this will return | |
10671 | `template <class T> template <class U> S<T*>::f(U)'. */ | |
73aad9b9 | 10672 | |
612c671a | 10673 | tree |
3a978d72 | 10674 | most_general_template (tree decl) |
73aad9b9 | 10675 | { |
f9a7ae04 MM |
10676 | /* If DECL is a FUNCTION_DECL, find the TEMPLATE_DECL of which it is |
10677 | an immediate specialization. */ | |
10678 | if (TREE_CODE (decl) == FUNCTION_DECL) | |
10679 | { | |
10680 | if (DECL_TEMPLATE_INFO (decl)) { | |
10681 | decl = DECL_TI_TEMPLATE (decl); | |
10682 | ||
10683 | /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE for a | |
10684 | template friend. */ | |
10685 | if (TREE_CODE (decl) != TEMPLATE_DECL) | |
10686 | return NULL_TREE; | |
10687 | } else | |
10688 | return NULL_TREE; | |
10689 | } | |
10690 | ||
10691 | /* Look for more and more general templates. */ | |
10692 | while (DECL_TEMPLATE_INFO (decl)) | |
10693 | { | |
10b1d5e7 MM |
10694 | /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases. |
10695 | (See cp-tree.h for details.) */ | |
f9a7ae04 MM |
10696 | if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL) |
10697 | break; | |
10698 | ||
6e049fcd KL |
10699 | if (CLASS_TYPE_P (TREE_TYPE (decl)) |
10700 | && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl))) | |
10701 | break; | |
10702 | ||
f9a7ae04 MM |
10703 | /* Stop if we run into an explicitly specialized class template. */ |
10704 | if (!DECL_NAMESPACE_SCOPE_P (decl) | |
10705 | && DECL_CONTEXT (decl) | |
10706 | && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl))) | |
10707 | break; | |
10708 | ||
10709 | decl = DECL_TI_TEMPLATE (decl); | |
10710 | } | |
36a117a5 MM |
10711 | |
10712 | return decl; | |
10713 | } | |
10714 | ||
10715 | /* Return the most specialized of the class template specializations | |
10716 | of TMPL which can produce an instantiation matching ARGS, or | |
10717 | error_mark_node if the choice is ambiguous. */ | |
10718 | ||
e9659ab0 | 10719 | static tree |
3a978d72 | 10720 | most_specialized_class (tree tmpl, tree args) |
36a117a5 MM |
10721 | { |
10722 | tree list = NULL_TREE; | |
10723 | tree t; | |
10724 | tree champ; | |
73aad9b9 JM |
10725 | int fate; |
10726 | ||
36a117a5 MM |
10727 | tmpl = most_general_template (tmpl); |
10728 | for (t = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); t; t = TREE_CHAIN (t)) | |
73aad9b9 | 10729 | { |
36a117a5 MM |
10730 | tree spec_args |
10731 | = get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t), args); | |
10732 | if (spec_args) | |
73aad9b9 | 10733 | { |
1f8f4a0b | 10734 | list = tree_cons (TREE_PURPOSE (t), TREE_VALUE (t), list); |
73aad9b9 JM |
10735 | TREE_TYPE (list) = TREE_TYPE (t); |
10736 | } | |
10737 | } | |
10738 | ||
10739 | if (! list) | |
10740 | return NULL_TREE; | |
10741 | ||
10742 | t = list; | |
10743 | champ = t; | |
10744 | t = TREE_CHAIN (t); | |
10745 | for (; t; t = TREE_CHAIN (t)) | |
10746 | { | |
55ece1b3 | 10747 | fate = more_specialized_class (champ, t, args); |
73aad9b9 JM |
10748 | if (fate == 1) |
10749 | ; | |
10750 | else | |
10751 | { | |
10752 | if (fate == 0) | |
10753 | { | |
10754 | t = TREE_CHAIN (t); | |
10755 | if (! t) | |
10756 | return error_mark_node; | |
10757 | } | |
10758 | champ = t; | |
10759 | } | |
10760 | } | |
10761 | ||
10762 | for (t = list; t && t != champ; t = TREE_CHAIN (t)) | |
10763 | { | |
55ece1b3 | 10764 | fate = more_specialized_class (champ, t, args); |
73aad9b9 JM |
10765 | if (fate != 1) |
10766 | return error_mark_node; | |
10767 | } | |
10768 | ||
10769 | return champ; | |
10770 | } | |
10771 | ||
eb8845be | 10772 | /* Explicitly instantiate DECL. */ |
e92cc029 | 10773 | |
8d08fdba | 10774 | void |
eb8845be | 10775 | do_decl_instantiation (tree decl, tree storage) |
8d08fdba | 10776 | { |
8d08fdba | 10777 | tree result = NULL_TREE; |
faae18ab | 10778 | int extern_p = 0; |
e8abc66f | 10779 | |
3fa56191 | 10780 | if (!decl) |
dc957d14 | 10781 | /* An error occurred, for which grokdeclarator has already issued |
3fa56191 MM |
10782 | an appropriate message. */ |
10783 | return; | |
10784 | else if (! DECL_LANG_SPECIFIC (decl)) | |
ec255269 | 10785 | { |
0f51ccfc | 10786 | error ("explicit instantiation of non-template %q#D", decl); |
ec255269 MS |
10787 | return; |
10788 | } | |
03d0f4af | 10789 | else if (TREE_CODE (decl) == VAR_DECL) |
6633d636 | 10790 | { |
03d0f4af MM |
10791 | /* There is an asymmetry here in the way VAR_DECLs and |
10792 | FUNCTION_DECLs are handled by grokdeclarator. In the case of | |
10793 | the latter, the DECL we get back will be marked as a | |
10794 | template instantiation, and the appropriate | |
10795 | DECL_TEMPLATE_INFO will be set up. This does not happen for | |
10796 | VAR_DECLs so we do the lookup here. Probably, grokdeclarator | |
10797 | should handle VAR_DECLs as it currently handles | |
10798 | FUNCTION_DECLs. */ | |
86ac0575 | 10799 | result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false); |
e0860732 | 10800 | if (!result || TREE_CODE (result) != VAR_DECL) |
03d0f4af | 10801 | { |
0f51ccfc | 10802 | error ("no matching template for %qD found", decl); |
03d0f4af MM |
10803 | return; |
10804 | } | |
6633d636 MS |
10805 | } |
10806 | else if (TREE_CODE (decl) != FUNCTION_DECL) | |
10807 | { | |
0f51ccfc | 10808 | error ("explicit instantiation of %q#D", decl); |
6633d636 MS |
10809 | return; |
10810 | } | |
03d0f4af MM |
10811 | else |
10812 | result = decl; | |
672476cb | 10813 | |
03d0f4af | 10814 | /* Check for various error cases. Note that if the explicit |
0e339752 | 10815 | instantiation is valid the RESULT will currently be marked as an |
03d0f4af MM |
10816 | *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set |
10817 | until we get here. */ | |
10818 | ||
10819 | if (DECL_TEMPLATE_SPECIALIZATION (result)) | |
672476cb | 10820 | { |
07782718 KL |
10821 | /* DR 259 [temp.spec]. |
10822 | ||
10823 | Both an explicit instantiation and a declaration of an explicit | |
10824 | specialization shall not appear in a program unless the explicit | |
10825 | instantiation follows a declaration of the explicit specialization. | |
03d0f4af | 10826 | |
07782718 KL |
10827 | For a given set of template parameters, if an explicit |
10828 | instantiation of a template appears after a declaration of an | |
10829 | explicit specialization for that template, the explicit | |
10830 | instantiation has no effect. */ | |
672476cb MM |
10831 | return; |
10832 | } | |
03d0f4af MM |
10833 | else if (DECL_EXPLICIT_INSTANTIATION (result)) |
10834 | { | |
10835 | /* [temp.spec] | |
98c1c668 | 10836 | |
03d0f4af MM |
10837 | No program shall explicitly instantiate any template more |
10838 | than once. | |
10839 | ||
4684cd27 MM |
10840 | We check DECL_NOT_REALLY_EXTERN so as not to complain when |
10841 | the first instantiation was `extern' and the second is not, | |
10842 | and EXTERN_P for the opposite case. */ | |
10843 | if (DECL_NOT_REALLY_EXTERN (result) && !extern_p) | |
0f51ccfc | 10844 | pedwarn ("duplicate explicit instantiation of %q#D", result); |
4684cd27 MM |
10845 | /* If an "extern" explicit instantiation follows an ordinary |
10846 | explicit instantiation, the template is instantiated. */ | |
10847 | if (extern_p) | |
03d0f4af MM |
10848 | return; |
10849 | } | |
10850 | else if (!DECL_IMPLICIT_INSTANTIATION (result)) | |
faae18ab | 10851 | { |
0f51ccfc | 10852 | error ("no matching template for %qD found", result); |
faae18ab MS |
10853 | return; |
10854 | } | |
03d0f4af | 10855 | else if (!DECL_TEMPLATE_INFO (result)) |
6633d636 | 10856 | { |
0f51ccfc | 10857 | pedwarn ("explicit instantiation of non-template %q#D", result); |
6633d636 MS |
10858 | return; |
10859 | } | |
10860 | ||
f0e01782 | 10861 | if (storage == NULL_TREE) |
00595019 | 10862 | ; |
faae18ab | 10863 | else if (storage == ridpointers[(int) RID_EXTERN]) |
03d0f4af | 10864 | { |
c02f5e29 | 10865 | if (pedantic && !in_system_header) |
0f51ccfc GDR |
10866 | pedwarn ("ISO C++ forbids the use of %<extern%> on explicit " |
10867 | "instantiations"); | |
03d0f4af MM |
10868 | extern_p = 1; |
10869 | } | |
f0e01782 | 10870 | else |
0f51ccfc | 10871 | error ("storage class %qD applied to template instantiation", storage); |
5566b478 | 10872 | |
5566b478 | 10873 | mark_decl_instantiated (result, extern_p); |
c91a56d2 | 10874 | if (! extern_p) |
415c974c | 10875 | instantiate_decl (result, /*defer_ok=*/1, /*undefined_ok=*/0); |
7177d104 MS |
10876 | } |
10877 | ||
faae18ab | 10878 | void |
3a978d72 | 10879 | mark_class_instantiated (tree t, int extern_p) |
faae18ab MS |
10880 | { |
10881 | SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t); | |
eb773359 JM |
10882 | SET_CLASSTYPE_INTERFACE_KNOWN (t); |
10883 | CLASSTYPE_INTERFACE_ONLY (t) = extern_p; | |
faae18ab MS |
10884 | TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p; |
10885 | if (! extern_p) | |
10886 | { | |
10887 | CLASSTYPE_DEBUG_REQUESTED (t) = 1; | |
10888 | rest_of_type_compilation (t, 1); | |
10889 | } | |
10890 | } | |
e8abc66f | 10891 | |
5e0c54e5 | 10892 | /* Called from do_type_instantiation through binding_table_foreach to |
9bcb9aae | 10893 | do recursive instantiation for the type bound in ENTRY. */ |
5e0c54e5 GDR |
10894 | static void |
10895 | bt_instantiate_type_proc (binding_entry entry, void *data) | |
10896 | { | |
10897 | tree storage = *(tree *) data; | |
10898 | ||
10899 | if (IS_AGGR_TYPE (entry->type) | |
10900 | && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type))) | |
10901 | do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0); | |
10902 | } | |
10903 | ||
415c974c MA |
10904 | /* Called from do_type_instantiation to instantiate a member |
10905 | (a member function or a static member variable) of an | |
03fd3f84 | 10906 | explicitly instantiated class template. */ |
415c974c MA |
10907 | static void |
10908 | instantiate_class_member (tree decl, int extern_p) | |
10909 | { | |
10910 | mark_decl_instantiated (decl, extern_p); | |
415c974c MA |
10911 | if (! extern_p) |
10912 | instantiate_decl (decl, /*defer_ok=*/1, /* undefined_ok=*/1); | |
10913 | } | |
10914 | ||
a1bcc528 JM |
10915 | /* Perform an explicit instantiation of template class T. STORAGE, if |
10916 | non-null, is the RID for extern, inline or static. COMPLAIN is | |
838dfd8a | 10917 | nonzero if this is called from the parser, zero if called recursively, |
a1bcc528 JM |
10918 | since the standard is unclear (as detailed below). */ |
10919 | ||
7177d104 | 10920 | void |
3a978d72 | 10921 | do_type_instantiation (tree t, tree storage, tsubst_flags_t complain) |
7177d104 | 10922 | { |
e8abc66f MS |
10923 | int extern_p = 0; |
10924 | int nomem_p = 0; | |
5566b478 | 10925 | int static_p = 0; |
4746cf84 | 10926 | int previous_instantiation_extern_p = 0; |
5566b478 | 10927 | |
ca79f85d JM |
10928 | if (TREE_CODE (t) == TYPE_DECL) |
10929 | t = TREE_TYPE (t); | |
10930 | ||
7ddedda4 | 10931 | if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t)) |
ca79f85d | 10932 | { |
0f51ccfc | 10933 | error ("explicit instantiation of non-template type %qT", t); |
ca79f85d JM |
10934 | return; |
10935 | } | |
10936 | ||
5566b478 | 10937 | complete_type (t); |
7177d104 | 10938 | |
d0f062fb | 10939 | if (!COMPLETE_TYPE_P (t)) |
f0e01782 | 10940 | { |
c2ea3a40 | 10941 | if (complain & tf_error) |
0f51ccfc GDR |
10942 | error ("explicit instantiation of %q#T before definition of template", |
10943 | t); | |
f0e01782 MS |
10944 | return; |
10945 | } | |
10946 | ||
03d0f4af | 10947 | if (storage != NULL_TREE) |
f0e01782 | 10948 | { |
c02f5e29 | 10949 | if (pedantic && !in_system_header) |
c4f73174 | 10950 | pedwarn("ISO C++ forbids the use of %qE on explicit instantiations", |
4460cef2 | 10951 | storage); |
03d0f4af MM |
10952 | |
10953 | if (storage == ridpointers[(int) RID_INLINE]) | |
10954 | nomem_p = 1; | |
10955 | else if (storage == ridpointers[(int) RID_EXTERN]) | |
10956 | extern_p = 1; | |
10957 | else if (storage == ridpointers[(int) RID_STATIC]) | |
10958 | static_p = 1; | |
10959 | else | |
10960 | { | |
0f51ccfc GDR |
10961 | error ("storage class %qD applied to template instantiation", |
10962 | storage); | |
03d0f4af MM |
10963 | extern_p = 0; |
10964 | } | |
f0e01782 MS |
10965 | } |
10966 | ||
370af2d5 | 10967 | if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t)) |
03d0f4af | 10968 | { |
07782718 | 10969 | /* DR 259 [temp.spec]. |
a292b002 | 10970 | |
07782718 KL |
10971 | Both an explicit instantiation and a declaration of an explicit |
10972 | specialization shall not appear in a program unless the explicit | |
10973 | instantiation follows a declaration of the explicit specialization. | |
10974 | ||
10975 | For a given set of template parameters, if an explicit | |
10976 | instantiation of a template appears after a declaration of an | |
10977 | explicit specialization for that template, the explicit | |
10978 | instantiation has no effect. */ | |
03d0f4af MM |
10979 | return; |
10980 | } | |
10981 | else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t)) | |
44a8d0b3 | 10982 | { |
03d0f4af MM |
10983 | /* [temp.spec] |
10984 | ||
10985 | No program shall explicitly instantiate any template more | |
10986 | than once. | |
10987 | ||
4746cf84 MA |
10988 | If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit |
10989 | instantiation was `extern'. If EXTERN_P then the second is. | |
4684cd27 | 10990 | These cases are OK. */ |
4746cf84 MA |
10991 | previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t); |
10992 | ||
10993 | if (!previous_instantiation_extern_p && !extern_p | |
c2ea3a40 | 10994 | && (complain & tf_error)) |
0f51ccfc | 10995 | pedwarn ("duplicate explicit instantiation of %q#T", t); |
03d0f4af MM |
10996 | |
10997 | /* If we've already instantiated the template, just return now. */ | |
10998 | if (!CLASSTYPE_INTERFACE_ONLY (t)) | |
10999 | return; | |
44a8d0b3 | 11000 | } |
e8abc66f | 11001 | |
03d0f4af | 11002 | mark_class_instantiated (t, extern_p); |
03d0f4af | 11003 | |
e8abc66f MS |
11004 | if (nomem_p) |
11005 | return; | |
11006 | ||
7177d104 | 11007 | { |
db5ae43f | 11008 | tree tmp; |
5566b478 | 11009 | |
03d0f4af MM |
11010 | /* In contrast to implicit instantiation, where only the |
11011 | declarations, and not the definitions, of members are | |
11012 | instantiated, we have here: | |
11013 | ||
11014 | [temp.explicit] | |
11015 | ||
11016 | The explicit instantiation of a class template specialization | |
11017 | implies the instantiation of all of its members not | |
11018 | previously explicitly specialized in the translation unit | |
11019 | containing the explicit instantiation. | |
11020 | ||
11021 | Of course, we can't instantiate member template classes, since | |
11022 | we don't have any arguments for them. Note that the standard | |
dc957d14 | 11023 | is unclear on whether the instantiation of the members are |
415c974c | 11024 | *explicit* instantiations or not. However, the most natural |
03fd3f84 | 11025 | interpretation is that it should be an explicit instantiation. */ |
03d0f4af | 11026 | |
5566b478 MS |
11027 | if (! static_p) |
11028 | for (tmp = TYPE_METHODS (t); tmp; tmp = TREE_CHAIN (tmp)) | |
a7d87521 | 11029 | if (TREE_CODE (tmp) == FUNCTION_DECL |
1f109f0f | 11030 | && DECL_TEMPLATE_INSTANTIATION (tmp)) |
415c974c | 11031 | instantiate_class_member (tmp, extern_p); |
5566b478 MS |
11032 | |
11033 | for (tmp = TYPE_FIELDS (t); tmp; tmp = TREE_CHAIN (tmp)) | |
11034 | if (TREE_CODE (tmp) == VAR_DECL && DECL_TEMPLATE_INSTANTIATION (tmp)) | |
415c974c | 11035 | instantiate_class_member (tmp, extern_p); |
7177d104 | 11036 | |
5e0c54e5 GDR |
11037 | if (CLASSTYPE_NESTED_UTDS (t)) |
11038 | binding_table_foreach (CLASSTYPE_NESTED_UTDS (t), | |
11039 | bt_instantiate_type_proc, &storage); | |
a292b002 | 11040 | } |
8d08fdba | 11041 | } |
a28e3c7f | 11042 | |
36a117a5 MM |
11043 | /* Given a function DECL, which is a specialization of TMPL, modify |
11044 | DECL to be a re-instantiation of TMPL with the same template | |
11045 | arguments. TMPL should be the template into which tsubst'ing | |
11046 | should occur for DECL, not the most general template. | |
830bfa74 MM |
11047 | |
11048 | One reason for doing this is a scenario like this: | |
11049 | ||
11050 | template <class T> | |
11051 | void f(const T&, int i); | |
11052 | ||
11053 | void g() { f(3, 7); } | |
11054 | ||
11055 | template <class T> | |
11056 | void f(const T& t, const int i) { } | |
11057 | ||
11058 | Note that when the template is first instantiated, with | |
11059 | instantiate_template, the resulting DECL will have no name for the | |
11060 | first parameter, and the wrong type for the second. So, when we go | |
11061 | to instantiate the DECL, we regenerate it. */ | |
11062 | ||
e9659ab0 | 11063 | static void |
3a978d72 | 11064 | regenerate_decl_from_template (tree decl, tree tmpl) |
830bfa74 | 11065 | { |
f9a7ae04 MM |
11066 | /* The arguments used to instantiate DECL, from the most general |
11067 | template. */ | |
830bfa74 | 11068 | tree args; |
830bfa74 | 11069 | tree code_pattern; |
830bfa74 MM |
11070 | |
11071 | args = DECL_TI_ARGS (decl); | |
11072 | code_pattern = DECL_TEMPLATE_RESULT (tmpl); | |
11073 | ||
2b907f5c KL |
11074 | /* Make sure that we can see identifiers, and compute access |
11075 | correctly. */ | |
11076 | push_access_scope (decl); | |
11077 | ||
c7222c02 MM |
11078 | if (TREE_CODE (decl) == FUNCTION_DECL) |
11079 | { | |
11080 | tree decl_parm; | |
11081 | tree pattern_parm; | |
11082 | tree specs; | |
11083 | int args_depth; | |
11084 | int parms_depth; | |
11085 | ||
11086 | args_depth = TMPL_ARGS_DEPTH (args); | |
11087 | parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)); | |
11088 | if (args_depth > parms_depth) | |
11089 | args = get_innermost_template_args (args, parms_depth); | |
11090 | ||
11091 | specs = tsubst_exception_specification (TREE_TYPE (code_pattern), | |
11092 | args, tf_error, NULL_TREE); | |
11093 | if (specs) | |
11094 | TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl), | |
11095 | specs); | |
11096 | ||
11097 | /* Merge parameter declarations. */ | |
11098 | decl_parm = skip_artificial_parms_for (decl, | |
11099 | DECL_ARGUMENTS (decl)); | |
11100 | pattern_parm | |
11101 | = skip_artificial_parms_for (code_pattern, | |
11102 | DECL_ARGUMENTS (code_pattern)); | |
11103 | while (decl_parm) | |
11104 | { | |
11105 | tree parm_type; | |
11106 | ||
11107 | if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm)) | |
11108 | DECL_NAME (decl_parm) = DECL_NAME (pattern_parm); | |
11109 | parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error, | |
11110 | NULL_TREE); | |
11111 | if (!same_type_p (TREE_TYPE (decl_parm), parm_type)) | |
11112 | TREE_TYPE (decl_parm) = parm_type; | |
11113 | decl_parm = TREE_CHAIN (decl_parm); | |
11114 | pattern_parm = TREE_CHAIN (pattern_parm); | |
11115 | } | |
830bfa74 | 11116 | |
c7222c02 MM |
11117 | /* Merge additional specifiers from the CODE_PATTERN. */ |
11118 | if (DECL_DECLARED_INLINE_P (code_pattern) | |
11119 | && !DECL_DECLARED_INLINE_P (decl)) | |
11120 | DECL_DECLARED_INLINE_P (decl) = 1; | |
11121 | if (DECL_INLINE (code_pattern) && !DECL_INLINE (decl)) | |
11122 | DECL_INLINE (decl) = 1; | |
11123 | } | |
11124 | else if (TREE_CODE (decl) == VAR_DECL) | |
830bfa74 | 11125 | { |
c7222c02 MM |
11126 | if (!DECL_INITIALIZED_IN_CLASS_P (decl) |
11127 | && DECL_INITIAL (code_pattern)) | |
11128 | DECL_INITIAL (decl) = | |
17bbb839 MM |
11129 | tsubst_expr (DECL_INITIAL (code_pattern), args, |
11130 | tf_error, DECL_TI_TEMPLATE (decl)); | |
830bfa74 | 11131 | } |
c7222c02 MM |
11132 | else |
11133 | gcc_unreachable (); | |
36a117a5 | 11134 | |
2b59fc25 | 11135 | pop_access_scope (decl); |
830bfa74 MM |
11136 | } |
11137 | ||
a723baf1 MM |
11138 | /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be |
11139 | substituted to get DECL. */ | |
11140 | ||
d58b7c2d | 11141 | tree |
a723baf1 MM |
11142 | template_for_substitution (tree decl) |
11143 | { | |
11144 | tree tmpl = DECL_TI_TEMPLATE (decl); | |
11145 | ||
11146 | /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern | |
11147 | for the instantiation. This is not always the most general | |
11148 | template. Consider, for example: | |
11149 | ||
11150 | template <class T> | |
11151 | struct S { template <class U> void f(); | |
11152 | template <> void f<int>(); }; | |
11153 | ||
11154 | and an instantiation of S<double>::f<int>. We want TD to be the | |
11155 | specialization S<T>::f<int>, not the more general S<T>::f<U>. */ | |
11156 | while (/* An instantiation cannot have a definition, so we need a | |
11157 | more general template. */ | |
11158 | DECL_TEMPLATE_INSTANTIATION (tmpl) | |
11159 | /* We must also deal with friend templates. Given: | |
11160 | ||
11161 | template <class T> struct S { | |
11162 | template <class U> friend void f() {}; | |
11163 | }; | |
11164 | ||
11165 | S<int>::f<U> say, is not an instantiation of S<T>::f<U>, | |
11166 | so far as the language is concerned, but that's still | |
11167 | where we get the pattern for the instantiation from. On | |
11168 | other hand, if the definition comes outside the class, say: | |
11169 | ||
11170 | template <class T> struct S { | |
11171 | template <class U> friend void f(); | |
11172 | }; | |
11173 | template <class U> friend void f() {} | |
11174 | ||
11175 | we don't need to look any further. That's what the check for | |
11176 | DECL_INITIAL is for. */ | |
11177 | || (TREE_CODE (decl) == FUNCTION_DECL | |
11178 | && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl) | |
11179 | && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl)))) | |
11180 | { | |
11181 | /* The present template, TD, should not be a definition. If it | |
11182 | were a definition, we should be using it! Note that we | |
11183 | cannot restructure the loop to just keep going until we find | |
11184 | a template with a definition, since that might go too far if | |
11185 | a specialization was declared, but not defined. */ | |
50bc768d NS |
11186 | gcc_assert (TREE_CODE (decl) != VAR_DECL |
11187 | || DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (tmpl))); | |
a723baf1 MM |
11188 | |
11189 | /* Fetch the more general template. */ | |
11190 | tmpl = DECL_TI_TEMPLATE (tmpl); | |
11191 | } | |
11192 | ||
11193 | return tmpl; | |
11194 | } | |
11195 | ||
16d53b64 | 11196 | /* Produce the definition of D, a _DECL generated from a template. If |
838dfd8a | 11197 | DEFER_OK is nonzero, then we don't have to actually do the |
415c974c MA |
11198 | instantiation now; we just have to do it sometime. Normally it is |
11199 | an error if this is an explicit instantiation but D is undefined. | |
11200 | If UNDEFINED_OK is nonzero, then instead we treat it as an implicit | |
11201 | instantiation. UNDEFINED_OK is nonzero only if we are being used | |
11202 | to instantiate the members of an explicitly instantiated class | |
03fd3f84 | 11203 | template. */ |
415c974c | 11204 | |
f84b4be9 | 11205 | |
a28e3c7f | 11206 | tree |
415c974c | 11207 | instantiate_decl (tree d, int defer_ok, int undefined_ok) |
a28e3c7f | 11208 | { |
36a117a5 | 11209 | tree tmpl = DECL_TI_TEMPLATE (d); |
65f8b0fb MM |
11210 | tree gen_args; |
11211 | tree args; | |
830bfa74 | 11212 | tree td; |
36a117a5 MM |
11213 | tree code_pattern; |
11214 | tree spec; | |
11215 | tree gen_tmpl; | |
5566b478 | 11216 | int pattern_defined; |
31a714f6 | 11217 | int need_push; |
82a98427 NS |
11218 | location_t saved_loc = input_location; |
11219 | ||
36a117a5 MM |
11220 | /* This function should only be used to instantiate templates for |
11221 | functions and static member variables. */ | |
50bc768d NS |
11222 | gcc_assert (TREE_CODE (d) == FUNCTION_DECL |
11223 | || TREE_CODE (d) == VAR_DECL); | |
36a117a5 | 11224 | |
cec24319 MM |
11225 | /* Variables are never deferred; if instantiation is required, they |
11226 | are instantiated right away. That allows for better code in the | |
11227 | case that an expression refers to the value of the variable -- | |
11228 | if the variable has a constant value the referring expression can | |
11229 | take advantage of that fact. */ | |
11230 | if (TREE_CODE (d) == VAR_DECL) | |
11231 | defer_ok = 0; | |
11232 | ||
db9b2174 MM |
11233 | /* Don't instantiate cloned functions. Instead, instantiate the |
11234 | functions they cloned. */ | |
11235 | if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d)) | |
11236 | d = DECL_CLONED_FUNCTION (d); | |
11237 | ||
fbf1c34b | 11238 | if (DECL_TEMPLATE_INSTANTIATED (d)) |
03d0f4af | 11239 | /* D has already been instantiated. It might seem reasonable to |
dc957d14 | 11240 | check whether or not D is an explicit instantiation, and, if so, |
03d0f4af MM |
11241 | stop here. But when an explicit instantiation is deferred |
11242 | until the end of the compilation, DECL_EXPLICIT_INSTANTIATION | |
11243 | is set, even though we still need to do the instantiation. */ | |
36a117a5 MM |
11244 | return d; |
11245 | ||
11246 | /* If we already have a specialization of this declaration, then | |
11247 | there's no reason to instantiate it. Note that | |
11248 | retrieve_specialization gives us both instantiations and | |
11249 | specializations, so we must explicitly check | |
11250 | DECL_TEMPLATE_SPECIALIZATION. */ | |
11251 | gen_tmpl = most_general_template (tmpl); | |
65f8b0fb | 11252 | gen_args = DECL_TI_ARGS (d); |
c7222c02 MM |
11253 | spec = retrieve_specialization (gen_tmpl, gen_args, |
11254 | /*class_specializations_p=*/false); | |
36a117a5 MM |
11255 | if (spec != NULL_TREE && DECL_TEMPLATE_SPECIALIZATION (spec)) |
11256 | return spec; | |
11257 | ||
11258 | /* This needs to happen before any tsubsting. */ | |
11259 | if (! push_tinst_level (d)) | |
11260 | return d; | |
11261 | ||
297a5329 JM |
11262 | timevar_push (TV_PARSE); |
11263 | ||
c1f927e8 | 11264 | /* We may be in the middle of deferred access check. Disable it now. */ |
7d021397 KL |
11265 | push_deferring_access_checks (dk_no_deferred); |
11266 | ||
4d85e00e | 11267 | /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern |
a723baf1 MM |
11268 | for the instantiation. */ |
11269 | td = template_for_substitution (d); | |
fee23f54 | 11270 | code_pattern = DECL_TEMPLATE_RESULT (td); |
27bb8339 | 11271 | |
76d3baad KL |
11272 | if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d)) |
11273 | || DECL_TEMPLATE_SPECIALIZATION (td)) | |
649fc72d NS |
11274 | /* In the case of a friend template whose definition is provided |
11275 | outside the class, we may have too many arguments. Drop the | |
76d3baad | 11276 | ones we don't need. The same is true for specializations. */ |
649fc72d NS |
11277 | args = get_innermost_template_args |
11278 | (gen_args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td))); | |
11279 | else | |
11280 | args = gen_args; | |
65f8b0fb | 11281 | |
5566b478 | 11282 | if (TREE_CODE (d) == FUNCTION_DECL) |
39c76b4f | 11283 | pattern_defined = (DECL_SAVED_TREE (code_pattern) != NULL_TREE); |
5566b478 | 11284 | else |
36a117a5 | 11285 | pattern_defined = ! DECL_IN_AGGR_P (code_pattern); |
4684cd27 MM |
11286 | /* Unless an explicit instantiation directive has already determined |
11287 | the linkage of D, remember that a definition is available for | |
11288 | this entity. */ | |
11289 | if (pattern_defined | |
11290 | && !DECL_INTERFACE_KNOWN (d) | |
11291 | && !DECL_NOT_REALLY_EXTERN (d)) | |
11292 | mark_definable (d); | |
de22184b | 11293 | |
f31686a3 | 11294 | input_location = DECL_SOURCE_LOCATION (d); |
de22184b | 11295 | |
4684cd27 | 11296 | if (! pattern_defined && DECL_EXPLICIT_INSTANTIATION (d) && undefined_ok) |
5566b478 | 11297 | { |
4684cd27 MM |
11298 | DECL_NOT_REALLY_EXTERN (d) = 0; |
11299 | SET_DECL_IMPLICIT_INSTANTIATION (d); | |
5566b478 MS |
11300 | } |
11301 | ||
4f2b0fb2 NS |
11302 | if (!defer_ok) |
11303 | { | |
11304 | /* Recheck the substitutions to obtain any warning messages | |
11305 | about ignoring cv qualifiers. */ | |
11306 | tree gen = DECL_TEMPLATE_RESULT (gen_tmpl); | |
11307 | tree type = TREE_TYPE (gen); | |
11308 | ||
0e902d98 | 11309 | /* Make sure that we can see identifiers, and compute access |
2b59fc25 KL |
11310 | correctly. D is already the target FUNCTION_DECL with the |
11311 | right context. */ | |
11312 | push_access_scope (d); | |
0e902d98 | 11313 | |
4f2b0fb2 NS |
11314 | if (TREE_CODE (gen) == FUNCTION_DECL) |
11315 | { | |
65f8b0fb MM |
11316 | tsubst (DECL_ARGUMENTS (gen), gen_args, tf_error | tf_warning, d); |
11317 | tsubst (TYPE_RAISES_EXCEPTIONS (type), gen_args, | |
4f2b0fb2 NS |
11318 | tf_error | tf_warning, d); |
11319 | /* Don't simply tsubst the function type, as that will give | |
11320 | duplicate warnings about poor parameter qualifications. | |
11321 | The function arguments are the same as the decl_arguments | |
c6002625 | 11322 | without the top level cv qualifiers. */ |
4f2b0fb2 NS |
11323 | type = TREE_TYPE (type); |
11324 | } | |
65f8b0fb | 11325 | tsubst (type, gen_args, tf_error | tf_warning, d); |
0e902d98 | 11326 | |
2b59fc25 | 11327 | pop_access_scope (d); |
4f2b0fb2 NS |
11328 | } |
11329 | ||
4684cd27 MM |
11330 | /* We should have set up DECL_INITIAL in instantiate_class_template |
11331 | for in-class definitions of static data members. */ | |
50bc768d NS |
11332 | gcc_assert (!(TREE_CODE (d) == VAR_DECL |
11333 | && DECL_INITIALIZED_IN_CLASS_P (d) | |
11334 | && DECL_INITIAL (d) == NULL_TREE)); | |
4684cd27 MM |
11335 | |
11336 | /* Do not instantiate templates that we know will be defined | |
11337 | elsewhere. */ | |
11338 | if (DECL_INTERFACE_KNOWN (d) | |
11339 | && DECL_REALLY_EXTERN (d) | |
11340 | && ! (TREE_CODE (d) == FUNCTION_DECL | |
11341 | && DECL_INLINE (d))) | |
930cd796 | 11342 | goto out; |
16d53b64 MM |
11343 | /* Defer all other templates, unless we have been explicitly |
11344 | forbidden from doing so. We restore the source position here | |
11345 | because it's used by add_pending_template. */ | |
11346 | else if (! pattern_defined || defer_ok) | |
11347 | { | |
82a98427 | 11348 | input_location = saved_loc; |
c27be9b9 | 11349 | |
03d0f4af MM |
11350 | if (at_eof && !pattern_defined |
11351 | && DECL_EXPLICIT_INSTANTIATION (d)) | |
11352 | /* [temp.explicit] | |
11353 | ||
11354 | The definition of a non-exported function template, a | |
11355 | non-exported member function template, or a non-exported | |
11356 | member function or static data member of a class template | |
11357 | shall be present in every translation unit in which it is | |
11358 | explicitly instantiated. */ | |
33bd39a2 | 11359 | pedwarn |
0f51ccfc | 11360 | ("explicit instantiation of %qD but no definition available", d); |
03d0f4af | 11361 | |
5566b478 | 11362 | add_pending_template (d); |
de22184b | 11363 | goto out; |
5566b478 | 11364 | } |
4684cd27 MM |
11365 | /* Tell the repository that D is available in this translation unit |
11366 | -- and see if it is supposed to be instantiated here. */ | |
11367 | if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d)) | |
11368 | { | |
11369 | /* In a PCH file, despite the fact that the repository hasn't | |
11370 | requested instantiation in the PCH it is still possible that | |
11371 | an instantiation will be required in a file that includes the | |
11372 | PCH. */ | |
11373 | if (pch_file) | |
11374 | add_pending_template (d); | |
11375 | /* Instantiate inline functions so that the inliner can do its | |
11376 | job, even though we'll not be emitting a copy of this | |
11377 | function. */ | |
c2a124b2 MM |
11378 | if (!(TREE_CODE (d) == FUNCTION_DECL |
11379 | && flag_inline_trees | |
11380 | && DECL_DECLARED_INLINE_P (d))) | |
4684cd27 MM |
11381 | goto out; |
11382 | } | |
5566b478 | 11383 | |
6de9cd9a | 11384 | need_push = !cfun || !global_bindings_p (); |
31a714f6 MM |
11385 | if (need_push) |
11386 | push_to_top_level (); | |
414ea4aa | 11387 | |
66e0c440 KL |
11388 | /* Mark D as instantiated so that recursive calls to |
11389 | instantiate_decl do not try to instantiate it again. */ | |
11390 | DECL_TEMPLATE_INSTANTIATED (d) = 1; | |
11391 | ||
2b0a63a3 MM |
11392 | /* Regenerate the declaration in case the template has been modified |
11393 | by a subsequent redeclaration. */ | |
11394 | regenerate_decl_from_template (d, td); | |
4684cd27 | 11395 | |
120722ac | 11396 | /* We already set the file and line above. Reset them now in case |
6de9cd9a | 11397 | they changed as a result of calling regenerate_decl_from_template. */ |
f31686a3 | 11398 | input_location = DECL_SOURCE_LOCATION (d); |
5156628f | 11399 | |
5566b478 MS |
11400 | if (TREE_CODE (d) == VAR_DECL) |
11401 | { | |
1d62c33e MM |
11402 | /* Clear out DECL_RTL; whatever was there before may not be right |
11403 | since we've reset the type of the declaration. */ | |
11404 | SET_DECL_RTL (d, NULL_RTX); | |
5566b478 | 11405 | DECL_IN_AGGR_P (d) = 0; |
ea56c40c | 11406 | |
4684cd27 MM |
11407 | /* Clear DECL_EXTERNAL so that cp_finish_decl will process the |
11408 | initializer. That function will defer actual emission until | |
11409 | we have a chance to determine linkage. */ | |
11410 | DECL_EXTERNAL (d) = 0; | |
11411 | ||
73a8adb6 | 11412 | /* Enter the scope of D so that access-checking works correctly. */ |
4684cd27 MM |
11413 | push_nested_class (DECL_CONTEXT (d)); |
11414 | cp_finish_decl (d, | |
11415 | (!DECL_INITIALIZED_IN_CLASS_P (d) | |
11416 | ? DECL_INITIAL (d) : NULL_TREE), | |
11417 | NULL_TREE, 0); | |
73a8adb6 | 11418 | pop_nested_class (); |
5566b478 MS |
11419 | } |
11420 | else if (TREE_CODE (d) == FUNCTION_DECL) | |
11421 | { | |
6bbf1598 | 11422 | htab_t saved_local_specializations; |
a723baf1 MM |
11423 | tree subst_decl; |
11424 | tree tmpl_parm; | |
11425 | tree spec_parm; | |
6bbf1598 MM |
11426 | |
11427 | /* Save away the current list, in case we are instantiating one | |
11428 | template from within the body of another. */ | |
11429 | saved_local_specializations = local_specializations; | |
11430 | ||
6dfbb909 | 11431 | /* Set up the list of local specializations. */ |
6dfbb909 | 11432 | local_specializations = htab_create (37, |
69f794a7 | 11433 | hash_local_specialization, |
a723baf1 | 11434 | eq_local_specializations, |
6dfbb909 MM |
11435 | NULL); |
11436 | ||
558475f0 | 11437 | /* Set up context. */ |
058b15c1 | 11438 | start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED); |
5566b478 | 11439 | |
a723baf1 MM |
11440 | /* Create substitution entries for the parameters. */ |
11441 | subst_decl = DECL_TEMPLATE_RESULT (template_for_substitution (d)); | |
11442 | tmpl_parm = DECL_ARGUMENTS (subst_decl); | |
11443 | spec_parm = DECL_ARGUMENTS (d); | |
11444 | if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d)) | |
11445 | { | |
11446 | register_local_specialization (spec_parm, tmpl_parm); | |
11447 | spec_parm = skip_artificial_parms_for (d, spec_parm); | |
11448 | tmpl_parm = skip_artificial_parms_for (subst_decl, tmpl_parm); | |
11449 | } | |
11450 | while (tmpl_parm) | |
11451 | { | |
11452 | register_local_specialization (spec_parm, tmpl_parm); | |
11453 | tmpl_parm = TREE_CHAIN (tmpl_parm); | |
11454 | spec_parm = TREE_CHAIN (spec_parm); | |
11455 | } | |
50bc768d | 11456 | gcc_assert (!spec_parm); |
a723baf1 | 11457 | |
558475f0 MM |
11458 | /* Substitute into the body of the function. */ |
11459 | tsubst_expr (DECL_SAVED_TREE (code_pattern), args, | |
c2ea3a40 | 11460 | tf_error | tf_warning, tmpl); |
558475f0 | 11461 | |
6dfbb909 MM |
11462 | /* We don't need the local specializations any more. */ |
11463 | htab_delete (local_specializations); | |
6bbf1598 | 11464 | local_specializations = saved_local_specializations; |
6dfbb909 | 11465 | |
4d6abc1c | 11466 | /* Finish the function. */ |
b2dd096b | 11467 | d = finish_function (0); |
8cd2462c | 11468 | expand_or_defer_fn (d); |
5566b478 MS |
11469 | } |
11470 | ||
971cbc14 MM |
11471 | /* We're not deferring instantiation any more. */ |
11472 | TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0; | |
11473 | ||
31a714f6 MM |
11474 | if (need_push) |
11475 | pop_from_top_level (); | |
414ea4aa | 11476 | |
de22184b | 11477 | out: |
82a98427 | 11478 | input_location = saved_loc; |
7d021397 | 11479 | pop_deferring_access_checks (); |
5566b478 | 11480 | pop_tinst_level (); |
a28e3c7f | 11481 | |
297a5329 JM |
11482 | timevar_pop (TV_PARSE); |
11483 | ||
a28e3c7f MS |
11484 | return d; |
11485 | } | |
5566b478 | 11486 | |
0aafb128 | 11487 | /* Run through the list of templates that we wish we could |
35046a54 KL |
11488 | instantiate, and instantiate any we can. RETRIES is the |
11489 | number of times we retry pending template instantiation. */ | |
0aafb128 | 11490 | |
35046a54 KL |
11491 | void |
11492 | instantiate_pending_templates (int retries) | |
0aafb128 MM |
11493 | { |
11494 | tree *t; | |
46ccf50a | 11495 | tree last = NULL_TREE; |
0aafb128 | 11496 | int reconsider; |
aad626f7 | 11497 | location_t saved_loc = input_location; |
35046a54 KL |
11498 | |
11499 | /* Instantiating templates may trigger vtable generation. This in turn | |
11500 | may require further template instantiations. We place a limit here | |
11501 | to avoid infinite loop. */ | |
11502 | if (pending_templates && retries >= max_tinst_depth) | |
11503 | { | |
11504 | cp_error_at ("template instantiation depth exceeds maximum of %d" | |
11505 | " (use -ftemplate-depth-NN to increase the maximum)" | |
0f51ccfc | 11506 | " instantiating %q+D, possibly from virtual table" |
35046a54 KL |
11507 | " generation", |
11508 | max_tinst_depth, TREE_VALUE (pending_templates)); | |
11509 | return; | |
11510 | } | |
11511 | ||
0aafb128 MM |
11512 | do |
11513 | { | |
11514 | reconsider = 0; | |
11515 | ||
11516 | t = &pending_templates; | |
11517 | while (*t) | |
11518 | { | |
0aafb128 MM |
11519 | tree instantiation = TREE_VALUE (*t); |
11520 | ||
3ae18eaf | 11521 | reopen_tinst_level (TREE_PURPOSE (*t)); |
0aafb128 | 11522 | |
2f939d94 | 11523 | if (TYPE_P (instantiation)) |
0aafb128 MM |
11524 | { |
11525 | tree fn; | |
11526 | ||
d0f062fb | 11527 | if (!COMPLETE_TYPE_P (instantiation)) |
0aafb128 MM |
11528 | { |
11529 | instantiate_class_template (instantiation); | |
11530 | if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation)) | |
11531 | for (fn = TYPE_METHODS (instantiation); | |
11532 | fn; | |
11533 | fn = TREE_CHAIN (fn)) | |
11534 | if (! DECL_ARTIFICIAL (fn)) | |
061cae1f NS |
11535 | instantiate_decl (fn, /*defer_ok=*/0, |
11536 | /*undefined_ok=*/0); | |
d0f062fb | 11537 | if (COMPLETE_TYPE_P (instantiation)) |
35046a54 | 11538 | reconsider = 1; |
0aafb128 MM |
11539 | } |
11540 | ||
d0f062fb | 11541 | if (COMPLETE_TYPE_P (instantiation)) |
0aafb128 MM |
11542 | /* If INSTANTIATION has been instantiated, then we don't |
11543 | need to consider it again in the future. */ | |
11544 | *t = TREE_CHAIN (*t); | |
46ccf50a JM |
11545 | else |
11546 | { | |
11547 | last = *t; | |
11548 | t = &TREE_CHAIN (*t); | |
11549 | } | |
0aafb128 MM |
11550 | } |
11551 | else | |
11552 | { | |
16d53b64 | 11553 | if (!DECL_TEMPLATE_SPECIALIZATION (instantiation) |
0aafb128 MM |
11554 | && !DECL_TEMPLATE_INSTANTIATED (instantiation)) |
11555 | { | |
16d53b64 | 11556 | instantiation = instantiate_decl (instantiation, |
415c974c MA |
11557 | /*defer_ok=*/0, |
11558 | /*undefined_ok=*/0); | |
0aafb128 | 11559 | if (DECL_TEMPLATE_INSTANTIATED (instantiation)) |
35046a54 | 11560 | reconsider = 1; |
0aafb128 MM |
11561 | } |
11562 | ||
16d53b64 | 11563 | if (DECL_TEMPLATE_SPECIALIZATION (instantiation) |
0aafb128 MM |
11564 | || DECL_TEMPLATE_INSTANTIATED (instantiation)) |
11565 | /* If INSTANTIATION has been instantiated, then we don't | |
11566 | need to consider it again in the future. */ | |
11567 | *t = TREE_CHAIN (*t); | |
46ccf50a JM |
11568 | else |
11569 | { | |
11570 | last = *t; | |
11571 | t = &TREE_CHAIN (*t); | |
11572 | } | |
0aafb128 | 11573 | } |
84e5ca0f NS |
11574 | tinst_depth = 0; |
11575 | current_tinst_level = NULL_TREE; | |
0aafb128 | 11576 | } |
46ccf50a | 11577 | last_pending_template = last; |
0aafb128 MM |
11578 | } |
11579 | while (reconsider); | |
11580 | ||
aad626f7 | 11581 | input_location = saved_loc; |
0aafb128 MM |
11582 | } |
11583 | ||
fd74ca0b MM |
11584 | /* Substitute ARGVEC into T, which is a list of initializers for |
11585 | either base class or a non-static data member. The TREE_PURPOSEs | |
11586 | are DECLs, and the TREE_VALUEs are the initializer values. Used by | |
11587 | instantiate_decl. */ | |
4393e105 | 11588 | |
824b9a4c | 11589 | static tree |
3a978d72 | 11590 | tsubst_initializer_list (tree t, tree argvec) |
5566b478 | 11591 | { |
2282d28d | 11592 | tree inits = NULL_TREE; |
5566b478 MS |
11593 | |
11594 | for (; t; t = TREE_CHAIN (t)) | |
11595 | { | |
fd74ca0b MM |
11596 | tree decl; |
11597 | tree init; | |
fd74ca0b | 11598 | |
c2ea3a40 | 11599 | decl = tsubst_copy (TREE_PURPOSE (t), argvec, tf_error | tf_warning, |
fd74ca0b | 11600 | NULL_TREE); |
1f5a253a NS |
11601 | decl = expand_member_init (decl); |
11602 | if (decl && !DECL_P (decl)) | |
11603 | in_base_initializer = 1; | |
11604 | ||
c2ea3a40 | 11605 | init = tsubst_expr (TREE_VALUE (t), argvec, tf_error | tf_warning, |
fd74ca0b | 11606 | NULL_TREE); |
1f5a253a NS |
11607 | in_base_initializer = 0; |
11608 | ||
11609 | if (decl) | |
2282d28d | 11610 | { |
1f5a253a | 11611 | init = build_tree_list (decl, init); |
2282d28d MM |
11612 | TREE_CHAIN (init) = inits; |
11613 | inits = init; | |
11614 | } | |
5566b478 | 11615 | } |
2282d28d | 11616 | return inits; |
5566b478 MS |
11617 | } |
11618 | ||
61a127b3 MM |
11619 | /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */ |
11620 | ||
11621 | static void | |
3a978d72 | 11622 | set_current_access_from_decl (tree decl) |
61a127b3 MM |
11623 | { |
11624 | if (TREE_PRIVATE (decl)) | |
11625 | current_access_specifier = access_private_node; | |
11626 | else if (TREE_PROTECTED (decl)) | |
11627 | current_access_specifier = access_protected_node; | |
11628 | else | |
11629 | current_access_specifier = access_public_node; | |
11630 | } | |
11631 | ||
dbfe2124 MM |
11632 | /* Instantiate an enumerated type. TAG is the template type, NEWTAG |
11633 | is the instantiation (which should have been created with | |
11634 | start_enum) and ARGS are the template arguments to use. */ | |
b87692e5 | 11635 | |
dbfe2124 | 11636 | static void |
3a978d72 | 11637 | tsubst_enum (tree tag, tree newtag, tree args) |
b87692e5 | 11638 | { |
dbfe2124 | 11639 | tree e; |
b87692e5 MS |
11640 | |
11641 | for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e)) | |
11642 | { | |
61a127b3 | 11643 | tree value; |
7b6d72fc MM |
11644 | tree decl; |
11645 | ||
11646 | decl = TREE_VALUE (e); | |
61a127b3 MM |
11647 | /* Note that in a template enum, the TREE_VALUE is the |
11648 | CONST_DECL, not the corresponding INTEGER_CST. */ | |
7b6d72fc | 11649 | value = tsubst_expr (DECL_INITIAL (decl), |
c2ea3a40 | 11650 | args, tf_error | tf_warning, |
4393e105 | 11651 | NULL_TREE); |
61a127b3 MM |
11652 | |
11653 | /* Give this enumeration constant the correct access. */ | |
7b6d72fc | 11654 | set_current_access_from_decl (decl); |
61a127b3 MM |
11655 | |
11656 | /* Actually build the enumerator itself. */ | |
7b6d72fc | 11657 | build_enumerator (DECL_NAME (decl), value, newtag); |
dbfe2124 | 11658 | } |
b3d5a58b | 11659 | |
219670f1 | 11660 | finish_enum (newtag); |
f31686a3 RH |
11661 | DECL_SOURCE_LOCATION (TYPE_NAME (newtag)) |
11662 | = DECL_SOURCE_LOCATION (TYPE_NAME (tag)); | |
b87692e5 | 11663 | } |
36a117a5 | 11664 | |
1f6e1acc AS |
11665 | /* DECL is a FUNCTION_DECL that is a template specialization. Return |
11666 | its type -- but without substituting the innermost set of template | |
11667 | arguments. So, innermost set of template parameters will appear in | |
5c74d5b0 | 11668 | the type. */ |
1f6e1acc AS |
11669 | |
11670 | tree | |
3a978d72 | 11671 | get_mostly_instantiated_function_type (tree decl) |
1f6e1acc | 11672 | { |
1f6e1acc AS |
11673 | tree fn_type; |
11674 | tree tmpl; | |
11675 | tree targs; | |
11676 | tree tparms; | |
11677 | int parm_depth; | |
11678 | ||
11679 | tmpl = most_general_template (DECL_TI_TEMPLATE (decl)); | |
11680 | targs = DECL_TI_ARGS (decl); | |
11681 | tparms = DECL_TEMPLATE_PARMS (tmpl); | |
11682 | parm_depth = TMPL_PARMS_DEPTH (tparms); | |
11683 | ||
11684 | /* There should be as many levels of arguments as there are levels | |
11685 | of parameters. */ | |
50bc768d | 11686 | gcc_assert (parm_depth == TMPL_ARGS_DEPTH (targs)); |
1f6e1acc AS |
11687 | |
11688 | fn_type = TREE_TYPE (tmpl); | |
1f6e1acc AS |
11689 | |
11690 | if (parm_depth == 1) | |
11691 | /* No substitution is necessary. */ | |
11692 | ; | |
11693 | else | |
11694 | { | |
11695 | int i; | |
11696 | tree partial_args; | |
11697 | ||
11698 | /* Replace the innermost level of the TARGS with NULL_TREEs to | |
dc957d14 | 11699 | let tsubst know not to substitute for those parameters. */ |
1f6e1acc AS |
11700 | partial_args = make_tree_vec (TREE_VEC_LENGTH (targs)); |
11701 | for (i = 1; i < TMPL_ARGS_DEPTH (targs); ++i) | |
11702 | SET_TMPL_ARGS_LEVEL (partial_args, i, | |
11703 | TMPL_ARGS_LEVEL (targs, i)); | |
11704 | SET_TMPL_ARGS_LEVEL (partial_args, | |
11705 | TMPL_ARGS_DEPTH (targs), | |
11706 | make_tree_vec (DECL_NTPARMS (tmpl))); | |
11707 | ||
5c74d5b0 KL |
11708 | /* Make sure that we can see identifiers, and compute access |
11709 | correctly. We can just use the context of DECL for the | |
11710 | partial substitution here. It depends only on outer template | |
11711 | parameters, regardless of whether the innermost level is | |
11712 | specialized or not. */ | |
2b59fc25 | 11713 | push_access_scope (decl); |
5c74d5b0 | 11714 | |
9579624e | 11715 | ++processing_template_decl; |
1f6e1acc AS |
11716 | /* Now, do the (partial) substitution to figure out the |
11717 | appropriate function type. */ | |
c2ea3a40 | 11718 | fn_type = tsubst (fn_type, partial_args, tf_error, NULL_TREE); |
9579624e | 11719 | --processing_template_decl; |
1f6e1acc AS |
11720 | |
11721 | /* Substitute into the template parameters to obtain the real | |
11722 | innermost set of parameters. This step is important if the | |
11723 | innermost set of template parameters contains value | |
11724 | parameters whose types depend on outer template parameters. */ | |
11725 | TREE_VEC_LENGTH (partial_args)--; | |
c2ea3a40 | 11726 | tparms = tsubst_template_parms (tparms, partial_args, tf_error); |
1f6e1acc | 11727 | |
2b59fc25 | 11728 | pop_access_scope (decl); |
5c74d5b0 | 11729 | } |
1f6e1acc AS |
11730 | |
11731 | return fn_type; | |
11732 | } | |
669ec2b4 | 11733 | |
cb753e49 | 11734 | /* Return truthvalue if we're processing a template different from |
dc957d14 | 11735 | the last one involved in diagnostics. */ |
cb753e49 | 11736 | int |
3a978d72 | 11737 | problematic_instantiation_changed (void) |
cb753e49 GDR |
11738 | { |
11739 | return last_template_error_tick != tinst_level_tick; | |
11740 | } | |
11741 | ||
11742 | /* Remember current template involved in diagnostics. */ | |
11743 | void | |
3a978d72 | 11744 | record_last_problematic_instantiation (void) |
cb753e49 GDR |
11745 | { |
11746 | last_template_error_tick = tinst_level_tick; | |
11747 | } | |
11748 | ||
11749 | tree | |
3a978d72 | 11750 | current_instantiation (void) |
cb753e49 GDR |
11751 | { |
11752 | return current_tinst_level; | |
11753 | } | |
db3f4e4e NS |
11754 | |
11755 | /* [temp.param] Check that template non-type parm TYPE is of an allowable | |
838dfd8a | 11756 | type. Return zero for ok, nonzero for disallowed. Issue error and |
c2ea3a40 | 11757 | warning messages under control of COMPLAIN. */ |
db3f4e4e NS |
11758 | |
11759 | static int | |
3a978d72 | 11760 | invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain) |
db3f4e4e NS |
11761 | { |
11762 | if (INTEGRAL_TYPE_P (type)) | |
11763 | return 0; | |
11764 | else if (POINTER_TYPE_P (type)) | |
11765 | return 0; | |
a5ac359a | 11766 | else if (TYPE_PTR_TO_MEMBER_P (type)) |
db3f4e4e | 11767 | return 0; |
db3f4e4e NS |
11768 | else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM) |
11769 | return 0; | |
11770 | else if (TREE_CODE (type) == TYPENAME_TYPE) | |
11771 | return 0; | |
11772 | ||
c2ea3a40 | 11773 | if (complain & tf_error) |
0f51ccfc | 11774 | error ("%q#T is not a valid type for a template constant parameter", type); |
db3f4e4e NS |
11775 | return 1; |
11776 | } | |
e2500fed | 11777 | |
5552b43c MM |
11778 | /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type]. |
11779 | Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/ | |
1fb3244a | 11780 | |
5552b43c MM |
11781 | static bool |
11782 | dependent_type_p_r (tree type) | |
1fb3244a MM |
11783 | { |
11784 | tree scope; | |
11785 | ||
1fb3244a MM |
11786 | /* [temp.dep.type] |
11787 | ||
11788 | A type is dependent if it is: | |
11789 | ||
6615c446 JO |
11790 | -- a template parameter. Template template parameters are types |
11791 | for us (since TYPE_P holds true for them) so we handle | |
11792 | them here. */ | |
6bf92cb6 GB |
11793 | if (TREE_CODE (type) == TEMPLATE_TYPE_PARM |
11794 | || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM) | |
1fb3244a MM |
11795 | return true; |
11796 | /* -- a qualified-id with a nested-name-specifier which contains a | |
11797 | class-name that names a dependent type or whose unqualified-id | |
11798 | names a dependent type. */ | |
11799 | if (TREE_CODE (type) == TYPENAME_TYPE) | |
11800 | return true; | |
11801 | /* -- a cv-qualified type where the cv-unqualified type is | |
11802 | dependent. */ | |
11803 | type = TYPE_MAIN_VARIANT (type); | |
11804 | /* -- a compound type constructed from any dependent type. */ | |
a5ac359a | 11805 | if (TYPE_PTR_TO_MEMBER_P (type)) |
1fb3244a MM |
11806 | return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type)) |
11807 | || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE | |
11808 | (type))); | |
11809 | else if (TREE_CODE (type) == POINTER_TYPE | |
11810 | || TREE_CODE (type) == REFERENCE_TYPE) | |
11811 | return dependent_type_p (TREE_TYPE (type)); | |
11812 | else if (TREE_CODE (type) == FUNCTION_TYPE | |
11813 | || TREE_CODE (type) == METHOD_TYPE) | |
11814 | { | |
11815 | tree arg_type; | |
11816 | ||
11817 | if (dependent_type_p (TREE_TYPE (type))) | |
11818 | return true; | |
11819 | for (arg_type = TYPE_ARG_TYPES (type); | |
11820 | arg_type; | |
11821 | arg_type = TREE_CHAIN (arg_type)) | |
11822 | if (dependent_type_p (TREE_VALUE (arg_type))) | |
11823 | return true; | |
11824 | return false; | |
11825 | } | |
11826 | /* -- an array type constructed from any dependent type or whose | |
11827 | size is specified by a constant expression that is | |
11828 | value-dependent. */ | |
11829 | if (TREE_CODE (type) == ARRAY_TYPE) | |
11830 | { | |
11831 | if (TYPE_DOMAIN (type) | |
11832 | && ((value_dependent_expression_p | |
11833 | (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))) | |
11834 | || (type_dependent_expression_p | |
11835 | (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))))) | |
11836 | return true; | |
11837 | return dependent_type_p (TREE_TYPE (type)); | |
11838 | } | |
86306a6b | 11839 | |
1fb3244a | 11840 | /* -- a template-id in which either the template name is a template |
86306a6b NS |
11841 | parameter ... */ |
11842 | if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM) | |
1fb3244a | 11843 | return true; |
86306a6b | 11844 | /* ... or any of the template arguments is a dependent type or |
04c06002 | 11845 | an expression that is type-dependent or value-dependent. */ |
86306a6b | 11846 | else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type) |
7e99327d MM |
11847 | && (any_dependent_template_arguments_p |
11848 | (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type))))) | |
1fb3244a | 11849 | return true; |
86306a6b | 11850 | |
1fb3244a MM |
11851 | /* All TYPEOF_TYPEs are dependent; if the argument of the `typeof' |
11852 | expression is not type-dependent, then it should already been | |
11853 | have resolved. */ | |
11854 | if (TREE_CODE (type) == TYPEOF_TYPE) | |
11855 | return true; | |
86306a6b | 11856 | |
1fb3244a MM |
11857 | /* The standard does not specifically mention types that are local |
11858 | to template functions or local classes, but they should be | |
11859 | considered dependent too. For example: | |
11860 | ||
11861 | template <int I> void f() { | |
11862 | enum E { a = I }; | |
11863 | S<sizeof (E)> s; | |
11864 | } | |
11865 | ||
11866 | The size of `E' cannot be known until the value of `I' has been | |
11867 | determined. Therefore, `E' must be considered dependent. */ | |
11868 | scope = TYPE_CONTEXT (type); | |
11869 | if (scope && TYPE_P (scope)) | |
11870 | return dependent_type_p (scope); | |
11871 | else if (scope && TREE_CODE (scope) == FUNCTION_DECL) | |
11872 | return type_dependent_expression_p (scope); | |
11873 | ||
11874 | /* Other types are non-dependent. */ | |
11875 | return false; | |
11876 | } | |
11877 | ||
5552b43c MM |
11878 | /* Returns TRUE if TYPE is dependent, in the sense of |
11879 | [temp.dep.type]. */ | |
11880 | ||
11881 | bool | |
11882 | dependent_type_p (tree type) | |
11883 | { | |
11884 | /* If there are no template parameters in scope, then there can't be | |
11885 | any dependent types. */ | |
11886 | if (!processing_template_decl) | |
11887 | return false; | |
11888 | ||
11889 | /* If the type is NULL, we have not computed a type for the entity | |
11890 | in question; in that case, the type is dependent. */ | |
11891 | if (!type) | |
11892 | return true; | |
11893 | ||
11894 | /* Erroneous types can be considered non-dependent. */ | |
11895 | if (type == error_mark_node) | |
11896 | return false; | |
11897 | ||
11898 | /* If we have not already computed the appropriate value for TYPE, | |
11899 | do so now. */ | |
11900 | if (!TYPE_DEPENDENT_P_VALID (type)) | |
11901 | { | |
11902 | TYPE_DEPENDENT_P (type) = dependent_type_p_r (type); | |
11903 | TYPE_DEPENDENT_P_VALID (type) = 1; | |
11904 | } | |
11905 | ||
11906 | return TYPE_DEPENDENT_P (type); | |
11907 | } | |
11908 | ||
8d83f792 MM |
11909 | /* Returns TRUE if EXPRESSION is dependent, according to CRITERION. */ |
11910 | ||
11911 | static bool | |
11912 | dependent_scope_ref_p (tree expression, bool criterion (tree)) | |
11913 | { | |
11914 | tree scope; | |
11915 | tree name; | |
11916 | ||
50bc768d | 11917 | gcc_assert (TREE_CODE (expression) == SCOPE_REF); |
8d83f792 MM |
11918 | |
11919 | if (!TYPE_P (TREE_OPERAND (expression, 0))) | |
11920 | return true; | |
11921 | ||
11922 | scope = TREE_OPERAND (expression, 0); | |
11923 | name = TREE_OPERAND (expression, 1); | |
11924 | ||
11925 | /* [temp.dep.expr] | |
11926 | ||
11927 | An id-expression is type-dependent if it contains a | |
11928 | nested-name-specifier that contains a class-name that names a | |
11929 | dependent type. */ | |
11930 | /* The suggested resolution to Core Issue 2 implies that if the | |
11931 | qualifying type is the current class, then we must peek | |
11932 | inside it. */ | |
11933 | if (DECL_P (name) | |
11934 | && currently_open_class (scope) | |
11935 | && !criterion (name)) | |
11936 | return false; | |
11937 | if (dependent_type_p (scope)) | |
11938 | return true; | |
11939 | ||
11940 | return false; | |
11941 | } | |
11942 | ||
20929c7f GB |
11943 | /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of |
11944 | [temp.dep.constexpr] */ | |
1fb3244a | 11945 | |
14d22dd6 | 11946 | bool |
1fb3244a MM |
11947 | value_dependent_expression_p (tree expression) |
11948 | { | |
11949 | if (!processing_template_decl) | |
11950 | return false; | |
11951 | ||
11952 | /* A name declared with a dependent type. */ | |
10b1d5e7 | 11953 | if (TREE_CODE (expression) == IDENTIFIER_NODE |
00cf3e31 MM |
11954 | || (DECL_P (expression) |
11955 | && type_dependent_expression_p (expression))) | |
1fb3244a MM |
11956 | return true; |
11957 | /* A non-type template parameter. */ | |
11958 | if ((TREE_CODE (expression) == CONST_DECL | |
11959 | && DECL_TEMPLATE_PARM_P (expression)) | |
11960 | || TREE_CODE (expression) == TEMPLATE_PARM_INDEX) | |
11961 | return true; | |
11962 | /* A constant with integral or enumeration type and is initialized | |
11963 | with an expression that is value-dependent. */ | |
11964 | if (TREE_CODE (expression) == VAR_DECL | |
11965 | && DECL_INITIAL (expression) | |
d17811fd | 11966 | && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (expression)) |
1fb3244a MM |
11967 | && value_dependent_expression_p (DECL_INITIAL (expression))) |
11968 | return true; | |
11969 | /* These expressions are value-dependent if the type to which the | |
20929c7f GB |
11970 | cast occurs is dependent or the expression being casted is |
11971 | value-dependent. */ | |
d17811fd MM |
11972 | if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR |
11973 | || TREE_CODE (expression) == STATIC_CAST_EXPR | |
11974 | || TREE_CODE (expression) == CONST_CAST_EXPR | |
11975 | || TREE_CODE (expression) == REINTERPRET_CAST_EXPR | |
11976 | || TREE_CODE (expression) == CAST_EXPR) | |
11977 | { | |
d36d5600 GB |
11978 | tree type = TREE_TYPE (expression); |
11979 | if (dependent_type_p (type)) | |
d17811fd MM |
11980 | return true; |
11981 | /* A functional cast has a list of operands. */ | |
11982 | expression = TREE_OPERAND (expression, 0); | |
d36d5600 GB |
11983 | if (!expression) |
11984 | { | |
11985 | /* If there are no operands, it must be an expression such | |
11986 | as "int()". This should not happen for aggregate types | |
11987 | because it would form non-constant expressions. */ | |
50bc768d | 11988 | gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type)); |
d36d5600 GB |
11989 | |
11990 | return false; | |
11991 | } | |
d17811fd MM |
11992 | if (TREE_CODE (expression) == TREE_LIST) |
11993 | { | |
11994 | do | |
11995 | { | |
11996 | if (value_dependent_expression_p (TREE_VALUE (expression))) | |
11997 | return true; | |
11998 | expression = TREE_CHAIN (expression); | |
11999 | } | |
12000 | while (expression); | |
12001 | return false; | |
12002 | } | |
12003 | else | |
12004 | return value_dependent_expression_p (expression); | |
12005 | } | |
12006 | /* A `sizeof' expression is value-dependent if the operand is | |
12007 | type-dependent. */ | |
12008 | if (TREE_CODE (expression) == SIZEOF_EXPR | |
12009 | || TREE_CODE (expression) == ALIGNOF_EXPR) | |
12010 | { | |
12011 | expression = TREE_OPERAND (expression, 0); | |
12012 | if (TYPE_P (expression)) | |
12013 | return dependent_type_p (expression); | |
12014 | return type_dependent_expression_p (expression); | |
12015 | } | |
8d83f792 MM |
12016 | if (TREE_CODE (expression) == SCOPE_REF) |
12017 | return dependent_scope_ref_p (expression, value_dependent_expression_p); | |
3d5ddced GB |
12018 | if (TREE_CODE (expression) == COMPONENT_REF) |
12019 | return (value_dependent_expression_p (TREE_OPERAND (expression, 0)) | |
12020 | || value_dependent_expression_p (TREE_OPERAND (expression, 1))); | |
1fb3244a MM |
12021 | /* A constant expression is value-dependent if any subexpression is |
12022 | value-dependent. */ | |
6615c446 | 12023 | if (EXPR_P (expression)) |
1fb3244a MM |
12024 | { |
12025 | switch (TREE_CODE_CLASS (TREE_CODE (expression))) | |
12026 | { | |
6615c446 | 12027 | case tcc_unary: |
1fb3244a MM |
12028 | return (value_dependent_expression_p |
12029 | (TREE_OPERAND (expression, 0))); | |
6615c446 JO |
12030 | case tcc_comparison: |
12031 | case tcc_binary: | |
1fb3244a MM |
12032 | return ((value_dependent_expression_p |
12033 | (TREE_OPERAND (expression, 0))) | |
12034 | || (value_dependent_expression_p | |
12035 | (TREE_OPERAND (expression, 1)))); | |
6615c446 | 12036 | case tcc_expression: |
1fb3244a MM |
12037 | { |
12038 | int i; | |
54e4aedb | 12039 | for (i = 0; i < TREE_CODE_LENGTH (TREE_CODE (expression)); ++i) |
14d22dd6 MM |
12040 | /* In some cases, some of the operands may be missing. |
12041 | (For example, in the case of PREDECREMENT_EXPR, the | |
12042 | amount to increment by may be missing.) That doesn't | |
12043 | make the expression dependent. */ | |
12044 | if (TREE_OPERAND (expression, i) | |
12045 | && (value_dependent_expression_p | |
12046 | (TREE_OPERAND (expression, i)))) | |
1fb3244a MM |
12047 | return true; |
12048 | return false; | |
12049 | } | |
6615c446 JO |
12050 | case tcc_reference: |
12051 | case tcc_statement: | |
12052 | /* These cannot be value dependent. */ | |
12053 | return false; | |
12054 | ||
12055 | default: | |
12056 | gcc_unreachable (); | |
1fb3244a MM |
12057 | } |
12058 | } | |
12059 | ||
12060 | /* The expression is not value-dependent. */ | |
12061 | return false; | |
12062 | } | |
12063 | ||
12064 | /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of | |
12065 | [temp.dep.expr]. */ | |
12066 | ||
12067 | bool | |
3a978d72 | 12068 | type_dependent_expression_p (tree expression) |
1fb3244a MM |
12069 | { |
12070 | if (!processing_template_decl) | |
12071 | return false; | |
12072 | ||
7efa3e22 NS |
12073 | if (expression == error_mark_node) |
12074 | return false; | |
10b1d5e7 MM |
12075 | |
12076 | /* An unresolved name is always dependent. */ | |
12077 | if (TREE_CODE (expression) == IDENTIFIER_NODE) | |
12078 | return true; | |
7efa3e22 | 12079 | |
1fb3244a MM |
12080 | /* Some expression forms are never type-dependent. */ |
12081 | if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR | |
12082 | || TREE_CODE (expression) == SIZEOF_EXPR | |
12083 | || TREE_CODE (expression) == ALIGNOF_EXPR | |
12084 | || TREE_CODE (expression) == TYPEID_EXPR | |
12085 | || TREE_CODE (expression) == DELETE_EXPR | |
12086 | || TREE_CODE (expression) == VEC_DELETE_EXPR | |
12087 | || TREE_CODE (expression) == THROW_EXPR) | |
12088 | return false; | |
12089 | ||
12090 | /* The types of these expressions depends only on the type to which | |
12091 | the cast occurs. */ | |
12092 | if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR | |
12093 | || TREE_CODE (expression) == STATIC_CAST_EXPR | |
12094 | || TREE_CODE (expression) == CONST_CAST_EXPR | |
12095 | || TREE_CODE (expression) == REINTERPRET_CAST_EXPR | |
12096 | || TREE_CODE (expression) == CAST_EXPR) | |
12097 | return dependent_type_p (TREE_TYPE (expression)); | |
d17811fd | 12098 | |
1fb3244a MM |
12099 | /* The types of these expressions depends only on the type created |
12100 | by the expression. */ | |
d17811fd MM |
12101 | if (TREE_CODE (expression) == NEW_EXPR |
12102 | || TREE_CODE (expression) == VEC_NEW_EXPR) | |
09d2f85f KL |
12103 | { |
12104 | /* For NEW_EXPR tree nodes created inside a template, either | |
12105 | the object type itself or a TREE_LIST may appear as the | |
12106 | operand 1. */ | |
12107 | tree type = TREE_OPERAND (expression, 1); | |
12108 | if (TREE_CODE (type) == TREE_LIST) | |
12109 | /* This is an array type. We need to check array dimensions | |
12110 | as well. */ | |
12111 | return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type))) | |
12112 | || value_dependent_expression_p | |
12113 | (TREE_OPERAND (TREE_VALUE (type), 1)); | |
12114 | else | |
12115 | return dependent_type_p (type); | |
12116 | } | |
1fb3244a | 12117 | |
5a57f1b2 JM |
12118 | if (TREE_CODE (expression) == SCOPE_REF |
12119 | && dependent_scope_ref_p (expression, | |
12120 | type_dependent_expression_p)) | |
12121 | return true; | |
12122 | ||
12123 | if (TREE_CODE (expression) == FUNCTION_DECL | |
12124 | && DECL_LANG_SPECIFIC (expression) | |
12125 | && DECL_TEMPLATE_INFO (expression) | |
12126 | && (any_dependent_template_arguments_p | |
12127 | (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression))))) | |
12128 | return true; | |
12129 | ||
12130 | if (TREE_CODE (expression) == TEMPLATE_DECL | |
12131 | && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression)) | |
12132 | return false; | |
12133 | ||
d17811fd MM |
12134 | if (TREE_TYPE (expression) == unknown_type_node) |
12135 | { | |
12136 | if (TREE_CODE (expression) == ADDR_EXPR) | |
12137 | return type_dependent_expression_p (TREE_OPERAND (expression, 0)); | |
c353b8e3 MM |
12138 | if (TREE_CODE (expression) == COMPONENT_REF |
12139 | || TREE_CODE (expression) == OFFSET_REF) | |
6cb89308 NS |
12140 | { |
12141 | if (type_dependent_expression_p (TREE_OPERAND (expression, 0))) | |
12142 | return true; | |
12143 | expression = TREE_OPERAND (expression, 1); | |
12144 | if (TREE_CODE (expression) == IDENTIFIER_NODE) | |
12145 | return false; | |
12146 | } | |
3601f003 KL |
12147 | /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */ |
12148 | if (TREE_CODE (expression) == SCOPE_REF) | |
12149 | return false; | |
5a57f1b2 | 12150 | |
d17811fd MM |
12151 | if (TREE_CODE (expression) == BASELINK) |
12152 | expression = BASELINK_FUNCTIONS (expression); | |
315fb5db | 12153 | |
d17811fd MM |
12154 | if (TREE_CODE (expression) == TEMPLATE_ID_EXPR) |
12155 | { | |
ee3071ef NS |
12156 | if (any_dependent_template_arguments_p |
12157 | (TREE_OPERAND (expression, 1))) | |
d17811fd MM |
12158 | return true; |
12159 | expression = TREE_OPERAND (expression, 0); | |
12160 | } | |
315fb5db NS |
12161 | gcc_assert (TREE_CODE (expression) == OVERLOAD); |
12162 | ||
12163 | while (expression) | |
d17811fd | 12164 | { |
315fb5db NS |
12165 | if (type_dependent_expression_p (OVL_CURRENT (expression))) |
12166 | return true; | |
12167 | expression = OVL_NEXT (expression); | |
d17811fd | 12168 | } |
315fb5db | 12169 | return false; |
d17811fd MM |
12170 | } |
12171 | ||
1fb3244a MM |
12172 | return (dependent_type_p (TREE_TYPE (expression))); |
12173 | } | |
12174 | ||
d17811fd MM |
12175 | /* Returns TRUE if ARGS (a TREE_LIST of arguments to a function call) |
12176 | contains a type-dependent expression. */ | |
1fb3244a MM |
12177 | |
12178 | bool | |
d17811fd MM |
12179 | any_type_dependent_arguments_p (tree args) |
12180 | { | |
12181 | while (args) | |
12182 | { | |
7efa3e22 NS |
12183 | tree arg = TREE_VALUE (args); |
12184 | ||
12185 | if (type_dependent_expression_p (arg)) | |
d17811fd MM |
12186 | return true; |
12187 | args = TREE_CHAIN (args); | |
12188 | } | |
12189 | return false; | |
12190 | } | |
12191 | ||
12192 | /* Returns TRUE if the ARG (a template argument) is dependent. */ | |
12193 | ||
12194 | static bool | |
1fb3244a MM |
12195 | dependent_template_arg_p (tree arg) |
12196 | { | |
12197 | if (!processing_template_decl) | |
12198 | return false; | |
12199 | ||
12200 | if (TREE_CODE (arg) == TEMPLATE_DECL | |
12201 | || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM) | |
12202 | return dependent_template_p (arg); | |
12203 | else if (TYPE_P (arg)) | |
12204 | return dependent_type_p (arg); | |
12205 | else | |
12206 | return (type_dependent_expression_p (arg) | |
12207 | || value_dependent_expression_p (arg)); | |
12208 | } | |
12209 | ||
d17811fd MM |
12210 | /* Returns true if ARGS (a collection of template arguments) contains |
12211 | any dependent arguments. */ | |
1fb3244a | 12212 | |
d17811fd MM |
12213 | bool |
12214 | any_dependent_template_arguments_p (tree args) | |
1fb3244a | 12215 | { |
bf12d54d | 12216 | int i; |
c353b8e3 MM |
12217 | int j; |
12218 | ||
d17811fd MM |
12219 | if (!args) |
12220 | return false; | |
12221 | ||
c353b8e3 MM |
12222 | for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i) |
12223 | { | |
12224 | tree level = TMPL_ARGS_LEVEL (args, i + 1); | |
12225 | for (j = 0; j < TREE_VEC_LENGTH (level); ++j) | |
12226 | if (dependent_template_arg_p (TREE_VEC_ELT (level, j))) | |
12227 | return true; | |
12228 | } | |
1fb3244a | 12229 | |
1fb3244a MM |
12230 | return false; |
12231 | } | |
12232 | ||
12233 | /* Returns TRUE if the template TMPL is dependent. */ | |
12234 | ||
12235 | bool | |
12236 | dependent_template_p (tree tmpl) | |
12237 | { | |
b95cc51a MM |
12238 | if (TREE_CODE (tmpl) == OVERLOAD) |
12239 | { | |
12240 | while (tmpl) | |
12241 | { | |
12242 | if (dependent_template_p (OVL_FUNCTION (tmpl))) | |
12243 | return true; | |
12244 | tmpl = OVL_CHAIN (tmpl); | |
12245 | } | |
12246 | return false; | |
12247 | } | |
12248 | ||
1fb3244a MM |
12249 | /* Template template parameters are dependent. */ |
12250 | if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl) | |
12251 | || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM) | |
12252 | return true; | |
27ab0504 | 12253 | /* So are names that have not been looked up. */ |
acccf788 MM |
12254 | if (TREE_CODE (tmpl) == SCOPE_REF |
12255 | || TREE_CODE (tmpl) == IDENTIFIER_NODE) | |
d17811fd | 12256 | return true; |
1fb3244a MM |
12257 | /* So are member templates of dependent classes. */ |
12258 | if (TYPE_P (CP_DECL_CONTEXT (tmpl))) | |
12259 | return dependent_type_p (DECL_CONTEXT (tmpl)); | |
12260 | return false; | |
12261 | } | |
12262 | ||
d17811fd MM |
12263 | /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */ |
12264 | ||
12265 | bool | |
12266 | dependent_template_id_p (tree tmpl, tree args) | |
12267 | { | |
12268 | return (dependent_template_p (tmpl) | |
12269 | || any_dependent_template_arguments_p (args)); | |
12270 | } | |
12271 | ||
14d22dd6 MM |
12272 | /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the |
12273 | TYPENAME_TYPE corresponds. Returns ERROR_MARK_NODE if no such TYPE | |
12274 | can be found. Note that this function peers inside uninstantiated | |
12275 | templates and therefore should be used only in extremely limited | |
12276 | situations. */ | |
12277 | ||
12278 | tree | |
12279 | resolve_typename_type (tree type, bool only_current_p) | |
12280 | { | |
12281 | tree scope; | |
12282 | tree name; | |
12283 | tree decl; | |
12284 | int quals; | |
4514aa8c | 12285 | tree pushed_scope; |
14d22dd6 | 12286 | |
50bc768d | 12287 | gcc_assert (TREE_CODE (type) == TYPENAME_TYPE); |
14d22dd6 MM |
12288 | |
12289 | scope = TYPE_CONTEXT (type); | |
12290 | name = TYPE_IDENTIFIER (type); | |
12291 | ||
12292 | /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve | |
12293 | it first before we can figure out what NAME refers to. */ | |
12294 | if (TREE_CODE (scope) == TYPENAME_TYPE) | |
12295 | scope = resolve_typename_type (scope, only_current_p); | |
12296 | /* If we don't know what SCOPE refers to, then we cannot resolve the | |
12297 | TYPENAME_TYPE. */ | |
12298 | if (scope == error_mark_node || TREE_CODE (scope) == TYPENAME_TYPE) | |
12299 | return error_mark_node; | |
12300 | /* If the SCOPE is a template type parameter, we have no way of | |
12301 | resolving the name. */ | |
12302 | if (TREE_CODE (scope) == TEMPLATE_TYPE_PARM) | |
12303 | return type; | |
12304 | /* If the SCOPE is not the current instantiation, there's no reason | |
12305 | to look inside it. */ | |
12306 | if (only_current_p && !currently_open_class (scope)) | |
12307 | return error_mark_node; | |
ca099ac8 MM |
12308 | /* If SCOPE is a partial instantiation, it will not have a valid |
12309 | TYPE_FIELDS list, so use the original template. */ | |
353b4fc0 | 12310 | scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope); |
14d22dd6 MM |
12311 | /* Enter the SCOPE so that name lookup will be resolved as if we |
12312 | were in the class definition. In particular, SCOPE will no | |
12313 | longer be considered a dependent type. */ | |
4514aa8c | 12314 | pushed_scope = push_scope (scope); |
14d22dd6 | 12315 | /* Look up the declaration. */ |
86ac0575 | 12316 | decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true); |
14d22dd6 MM |
12317 | /* Obtain the set of qualifiers applied to the TYPE. */ |
12318 | quals = cp_type_quals (type); | |
12319 | /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to | |
12320 | find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */ | |
12321 | if (!decl) | |
12322 | type = error_mark_node; | |
12323 | else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == IDENTIFIER_NODE | |
12324 | && TREE_CODE (decl) == TYPE_DECL) | |
12325 | type = TREE_TYPE (decl); | |
12326 | else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == TEMPLATE_ID_EXPR | |
12327 | && DECL_CLASS_TEMPLATE_P (decl)) | |
12328 | { | |
12329 | tree tmpl; | |
12330 | tree args; | |
12331 | /* Obtain the template and the arguments. */ | |
12332 | tmpl = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 0); | |
12333 | args = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 1); | |
12334 | /* Instantiate the template. */ | |
12335 | type = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE, | |
42eaed49 | 12336 | /*entering_scope=*/0, tf_error | tf_user); |
14d22dd6 MM |
12337 | } |
12338 | else | |
12339 | type = error_mark_node; | |
12340 | /* Qualify the resulting type. */ | |
12341 | if (type != error_mark_node && quals) | |
12342 | type = cp_build_qualified_type (type, quals); | |
12343 | /* Leave the SCOPE. */ | |
4514aa8c NS |
12344 | if (pushed_scope) |
12345 | pop_scope (pushed_scope); | |
14d22dd6 MM |
12346 | |
12347 | return type; | |
12348 | } | |
12349 | ||
d17811fd MM |
12350 | /* EXPR is an expression which is not type-dependent. Return a proxy |
12351 | for EXPR that can be used to compute the types of larger | |
12352 | expressions containing EXPR. */ | |
12353 | ||
12354 | tree | |
12355 | build_non_dependent_expr (tree expr) | |
12356 | { | |
0deb916c MM |
12357 | tree inner_expr; |
12358 | ||
d17811fd MM |
12359 | /* Preserve null pointer constants so that the type of things like |
12360 | "p == 0" where "p" is a pointer can be determined. */ | |
12361 | if (null_ptr_cst_p (expr)) | |
12362 | return expr; | |
12363 | /* Preserve OVERLOADs; the functions must be available to resolve | |
12364 | types. */ | |
0deb916c MM |
12365 | inner_expr = (TREE_CODE (expr) == ADDR_EXPR ? |
12366 | TREE_OPERAND (expr, 0) : expr); | |
12367 | if (TREE_CODE (inner_expr) == OVERLOAD | |
12368 | || TREE_CODE (inner_expr) == FUNCTION_DECL | |
12369 | || TREE_CODE (inner_expr) == TEMPLATE_DECL | |
12370 | || TREE_CODE (inner_expr) == TEMPLATE_ID_EXPR) | |
d17811fd | 12371 | return expr; |
5ae9ba3e MM |
12372 | /* There is no need to return a proxy for a variable. */ |
12373 | if (TREE_CODE (expr) == VAR_DECL) | |
12374 | return expr; | |
7433e6d4 MM |
12375 | /* Preserve string constants; conversions from string constants to |
12376 | "char *" are allowed, even though normally a "const char *" | |
12377 | cannot be used to initialize a "char *". */ | |
12378 | if (TREE_CODE (expr) == STRING_CST) | |
12379 | return expr; | |
b7c707d1 MM |
12380 | /* Preserve arithmetic constants, as an optimization -- there is no |
12381 | reason to create a new node. */ | |
12382 | if (TREE_CODE (expr) == INTEGER_CST || TREE_CODE (expr) == REAL_CST) | |
12383 | return expr; | |
9b7be7b5 MM |
12384 | /* Preserve THROW_EXPRs -- all throw-expressions have type "void". |
12385 | There is at least one place where we want to know that a | |
12386 | particular expression is a throw-expression: when checking a ?: | |
12387 | expression, there are special rules if the second or third | |
878cbb73 | 12388 | argument is a throw-expression. */ |
9b7be7b5 MM |
12389 | if (TREE_CODE (expr) == THROW_EXPR) |
12390 | return expr; | |
47d4c811 NS |
12391 | |
12392 | if (TREE_CODE (expr) == COND_EXPR) | |
f293ce4b RS |
12393 | return build3 (COND_EXPR, |
12394 | TREE_TYPE (expr), | |
12395 | TREE_OPERAND (expr, 0), | |
12396 | (TREE_OPERAND (expr, 1) | |
12397 | ? build_non_dependent_expr (TREE_OPERAND (expr, 1)) | |
12398 | : build_non_dependent_expr (TREE_OPERAND (expr, 0))), | |
12399 | build_non_dependent_expr (TREE_OPERAND (expr, 2))); | |
8e1daa34 NS |
12400 | if (TREE_CODE (expr) == COMPOUND_EXPR |
12401 | && !COMPOUND_EXPR_OVERLOADED (expr)) | |
f293ce4b RS |
12402 | return build2 (COMPOUND_EXPR, |
12403 | TREE_TYPE (expr), | |
12404 | TREE_OPERAND (expr, 0), | |
12405 | build_non_dependent_expr (TREE_OPERAND (expr, 1))); | |
47d4c811 | 12406 | |
d17811fd MM |
12407 | /* Otherwise, build a NON_DEPENDENT_EXPR. |
12408 | ||
12409 | REFERENCE_TYPEs are not stripped for expressions in templates | |
12410 | because doing so would play havoc with mangling. Consider, for | |
12411 | example: | |
12412 | ||
12413 | template <typename T> void f<T& g>() { g(); } | |
12414 | ||
12415 | In the body of "f", the expression for "g" will have | |
12416 | REFERENCE_TYPE, even though the standard says that it should | |
12417 | not. The reason is that we must preserve the syntactic form of | |
12418 | the expression so that mangling (say) "f<g>" inside the body of | |
12419 | "f" works out correctly. Therefore, the REFERENCE_TYPE is | |
12420 | stripped here. */ | |
018a5803 | 12421 | return build1 (NON_DEPENDENT_EXPR, non_reference (TREE_TYPE (expr)), expr); |
d17811fd MM |
12422 | } |
12423 | ||
12424 | /* ARGS is a TREE_LIST of expressions as arguments to a function call. | |
12425 | Return a new TREE_LIST with the various arguments replaced with | |
12426 | equivalent non-dependent expressions. */ | |
12427 | ||
12428 | tree | |
12429 | build_non_dependent_args (tree args) | |
12430 | { | |
12431 | tree a; | |
12432 | tree new_args; | |
12433 | ||
12434 | new_args = NULL_TREE; | |
12435 | for (a = args; a; a = TREE_CHAIN (a)) | |
12436 | new_args = tree_cons (NULL_TREE, | |
12437 | build_non_dependent_expr (TREE_VALUE (a)), | |
12438 | new_args); | |
12439 | return nreverse (new_args); | |
12440 | } | |
12441 | ||
e2500fed | 12442 | #include "gt-cp-pt.h" |