]>
Commit | Line | Data |
---|---|---|
8d08fdba | 1 | /* Handle parameterized types (templates) for GNU C++. |
06ceef4e RK |
2 | Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000 |
3 | 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 MS |
6 | |
7 | This file is part of GNU CC. | |
8 | ||
9 | GNU CC is free software; you can redistribute it and/or modify | |
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 | ||
14 | GNU CC is distributed in the hope that it will be useful, | |
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 | |
20 | along with GNU CC; 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" |
8d08fdba MS |
31 | #include "obstack.h" |
32 | ||
33 | #include "tree.h" | |
34 | #include "flags.h" | |
35 | #include "cp-tree.h" | |
36 | #include "decl.h" | |
37 | #include "parse.h" | |
f0e01782 | 38 | #include "lex.h" |
e8abc66f | 39 | #include "output.h" |
a9aedbc2 | 40 | #include "defaults.h" |
49c249e1 | 41 | #include "except.h" |
54f92bfb | 42 | #include "toplev.h" |
3dcaad8b | 43 | #include "rtl.h" |
091f9839 | 44 | #include "defaults.h" |
9cd64686 | 45 | #include "ggc.h" |
6dfbb909 | 46 | #include "hashtab.h" |
49c249e1 | 47 | |
050367a3 MM |
48 | /* The type of functions taking a tree, and some additional data, and |
49 | returning an int. */ | |
158991b7 | 50 | typedef int (*tree_fn_t) PARAMS ((tree, void*)); |
050367a3 | 51 | |
8d08fdba | 52 | extern struct obstack permanent_obstack; |
8d08fdba MS |
53 | |
54 | extern int lineno; | |
55 | extern char *input_filename; | |
8d08fdba | 56 | |
0aafb128 MM |
57 | /* The PENDING_TEMPLATES is a TREE_LIST of templates whose |
58 | instantiations have been deferred, either because their definitions | |
59 | were not yet available, or because we were putting off doing the | |
60 | work. The TREE_PURPOSE of each entry is a SRCLOC indicating where | |
61 | the instantiate request occurred; the TREE_VALUE is a either a DECL | |
62 | (for a function or static data member), or a TYPE (for a class) | |
63 | indicating what we are hoping to instantiate. */ | |
64 | static tree pending_templates; | |
5566b478 MS |
65 | static tree *template_tail = &pending_templates; |
66 | ||
0aafb128 | 67 | static tree maybe_templates; |
73aad9b9 JM |
68 | static tree *maybe_template_tail = &maybe_templates; |
69 | ||
67ffc812 | 70 | int processing_template_parmlist; |
386b8a85 JM |
71 | static int template_header_count; |
72 | ||
75650646 | 73 | static tree saved_trees; |
3dcaad8b MM |
74 | static varray_type inline_parm_levels; |
75 | static size_t inline_parm_levels_used; | |
75650646 | 76 | |
6dfbb909 MM |
77 | /* A map from local variable declarations in the body of the template |
78 | presently being instantiated to the corresponding instantiated | |
79 | local variables. */ | |
80 | static htab_t local_specializations; | |
81 | ||
8d08fdba MS |
82 | #define obstack_chunk_alloc xmalloc |
83 | #define obstack_chunk_free free | |
84 | ||
830bfa74 MM |
85 | #define UNIFY_ALLOW_NONE 0 |
86 | #define UNIFY_ALLOW_MORE_CV_QUAL 1 | |
87 | #define UNIFY_ALLOW_LESS_CV_QUAL 2 | |
88 | #define UNIFY_ALLOW_DERIVED 4 | |
161c12b0 | 89 | #define UNIFY_ALLOW_INTEGER 8 |
830bfa74 | 90 | |
4393e105 MM |
91 | #define GTB_VIA_VIRTUAL 1 /* The base class we are examining is |
92 | virtual, or a base class of a virtual | |
93 | base. */ | |
94 | #define GTB_IGNORE_TYPE 2 /* We don't need to try to unify the current | |
95 | type with the desired type. */ | |
96 | ||
158991b7 | 97 | static int resolve_overloaded_unification PARAMS ((tree, tree, tree, tree, |
4393e105 | 98 | unification_kind_t, int)); |
158991b7 | 99 | static int try_one_overload PARAMS ((tree, tree, tree, tree, tree, |
4393e105 | 100 | unification_kind_t, int)); |
158991b7 KG |
101 | static int unify PARAMS ((tree, tree, tree, tree, int)); |
102 | static void add_pending_template PARAMS ((tree)); | |
103 | static int push_tinst_level PARAMS ((tree)); | |
104 | static tree classtype_mangled_name PARAMS ((tree)); | |
105 | static char *mangle_class_name_for_template PARAMS ((char *, tree, tree)); | |
106 | static tree tsubst_expr_values PARAMS ((tree, tree)); | |
107 | static int list_eq PARAMS ((tree, tree)); | |
108 | static tree get_class_bindings PARAMS ((tree, tree, tree)); | |
109 | static tree coerce_template_parms PARAMS ((tree, tree, tree, int, int)); | |
110 | static void tsubst_enum PARAMS ((tree, tree, tree)); | |
111 | static tree add_to_template_args PARAMS ((tree, tree)); | |
112 | static tree add_outermost_template_args PARAMS ((tree, tree)); | |
113 | static void maybe_adjust_types_for_deduction PARAMS ((unification_kind_t, tree*, | |
830bfa74 | 114 | tree*)); |
158991b7 | 115 | static int type_unification_real PARAMS ((tree, tree, tree, tree, |
4393e105 | 116 | int, unification_kind_t, int)); |
158991b7 KG |
117 | static void note_template_header PARAMS ((int)); |
118 | static tree maybe_fold_nontype_arg PARAMS ((tree)); | |
119 | static tree convert_nontype_argument PARAMS ((tree, tree)); | |
120 | static tree convert_template_argument PARAMS ((tree, tree, tree, int, | |
8b5b8b7c | 121 | int , tree)); |
158991b7 KG |
122 | static tree get_bindings_overload PARAMS ((tree, tree, tree)); |
123 | static int for_each_template_parm PARAMS ((tree, tree_fn_t, void*)); | |
124 | static tree build_template_parm_index PARAMS ((int, int, int, tree, tree)); | |
125 | static int inline_needs_template_parms PARAMS ((tree)); | |
126 | static void push_inline_template_parms_recursive PARAMS ((tree, int)); | |
127 | static tree retrieve_specialization PARAMS ((tree, tree)); | |
6dfbb909 | 128 | static tree retrieve_local_specialization PARAMS ((tree)); |
158991b7 | 129 | static tree register_specialization PARAMS ((tree, tree, tree)); |
6dfbb909 | 130 | static tree register_local_specialization PARAMS ((tree, tree)); |
158991b7 KG |
131 | static int unregister_specialization PARAMS ((tree, tree)); |
132 | static tree reduce_template_parm_level PARAMS ((tree, tree, int)); | |
133 | static tree build_template_decl PARAMS ((tree, tree)); | |
134 | static int mark_template_parm PARAMS ((tree, void *)); | |
135 | static tree tsubst_friend_function PARAMS ((tree, tree)); | |
136 | static tree tsubst_friend_class PARAMS ((tree, tree)); | |
137 | static tree get_bindings_real PARAMS ((tree, tree, tree, int)); | |
138 | static int template_decl_level PARAMS ((tree)); | |
139 | static tree maybe_get_template_decl_from_type_decl PARAMS ((tree)); | |
140 | static int check_cv_quals_for_unify PARAMS ((int, tree, tree)); | |
141 | static tree tsubst_template_arg_vector PARAMS ((tree, tree, int)); | |
142 | static tree tsubst_template_parms PARAMS ((tree, tree, int)); | |
143 | static void regenerate_decl_from_template PARAMS ((tree, tree)); | |
144 | static tree most_specialized PARAMS ((tree, tree, tree)); | |
145 | static tree most_specialized_class PARAMS ((tree, tree)); | |
146 | static void set_mangled_name_for_template_decl PARAMS ((tree)); | |
147 | static int template_class_depth_real PARAMS ((tree, int)); | |
148 | static tree tsubst_aggr_type PARAMS ((tree, tree, int, tree, int)); | |
149 | static tree tsubst_decl PARAMS ((tree, tree, tree, tree)); | |
150 | static tree tsubst_arg_types PARAMS ((tree, tree, int, tree)); | |
151 | static tree tsubst_function_type PARAMS ((tree, tree, int, tree)); | |
152 | static void check_specialization_scope PARAMS ((void)); | |
153 | static tree process_partial_specialization PARAMS ((tree)); | |
154 | static void set_current_access_from_decl PARAMS ((tree)); | |
155 | static void check_default_tmpl_args PARAMS ((tree, tree, int, int)); | |
156 | static tree tsubst_call_declarator_parms PARAMS ((tree, tree, int, tree)); | |
157 | static tree get_template_base_recursive PARAMS ((tree, tree, | |
4393e105 | 158 | tree, tree, tree, int)); |
158991b7 KG |
159 | static tree get_template_base PARAMS ((tree, tree, tree, tree)); |
160 | static tree try_class_unification PARAMS ((tree, tree, tree, tree)); | |
161 | static int coerce_template_template_parms PARAMS ((tree, tree, int, | |
4393e105 | 162 | tree, tree)); |
158991b7 KG |
163 | static tree determine_specialization PARAMS ((tree, tree, tree *, int)); |
164 | static int template_args_equal PARAMS ((tree, tree)); | |
165 | static void print_template_context PARAMS ((int)); | |
166 | static void tsubst_default_arguments PARAMS ((tree)); | |
167 | static tree for_each_template_parm_r PARAMS ((tree *, int *, void *)); | |
db9b2174 | 168 | static tree instantiate_clone PARAMS ((tree, tree)); |
36a117a5 | 169 | |
9cd64686 MM |
170 | /* Called once to initialize pt.c. */ |
171 | ||
172 | void | |
173 | init_pt () | |
174 | { | |
175 | ggc_add_tree_root (&pending_templates, 1); | |
176 | ggc_add_tree_root (&maybe_templates, 1); | |
177 | ggc_add_tree_root (&saved_trees, 1); | |
178 | } | |
179 | ||
e1467ff2 MM |
180 | /* Do any processing required when DECL (a member template declaration |
181 | using TEMPLATE_PARAMETERS as its innermost parameter list) is | |
182 | finished. Returns the TEMPLATE_DECL corresponding to DECL, unless | |
183 | it is a specialization, in which case the DECL itself is returned. */ | |
184 | ||
185 | tree | |
61a127b3 | 186 | finish_member_template_decl (decl) |
e1467ff2 MM |
187 | tree decl; |
188 | { | |
93cdc044 JM |
189 | if (decl == NULL_TREE || decl == void_type_node) |
190 | return NULL_TREE; | |
3ddfb0e6 MM |
191 | else if (decl == error_mark_node) |
192 | /* By returning NULL_TREE, the parser will just ignore this | |
193 | declaration. We have already issued the error. */ | |
194 | return NULL_TREE; | |
93cdc044 JM |
195 | else if (TREE_CODE (decl) == TREE_LIST) |
196 | { | |
5f311aec | 197 | /* Assume that the class is the only declspec. */ |
93cdc044 | 198 | decl = TREE_VALUE (decl); |
8d019cef | 199 | if (IS_AGGR_TYPE (decl) && CLASSTYPE_TEMPLATE_INFO (decl) |
370af2d5 | 200 | && ! CLASSTYPE_TEMPLATE_SPECIALIZATION (decl)) |
93cdc044 JM |
201 | { |
202 | tree tmpl = CLASSTYPE_TI_TEMPLATE (decl); | |
203 | check_member_template (tmpl); | |
204 | return tmpl; | |
205 | } | |
8d019cef | 206 | return NULL_TREE; |
93cdc044 | 207 | } |
07c88314 MM |
208 | else if (TREE_CODE (decl) == FIELD_DECL) |
209 | cp_error ("data member `%D' cannot be a member template", decl); | |
a1da6cba | 210 | else if (DECL_TEMPLATE_INFO (decl)) |
e1467ff2 | 211 | { |
a1da6cba MM |
212 | if (!DECL_TEMPLATE_SPECIALIZATION (decl)) |
213 | { | |
214 | check_member_template (DECL_TI_TEMPLATE (decl)); | |
215 | return DECL_TI_TEMPLATE (decl); | |
216 | } | |
217 | else | |
218 | return decl; | |
219 | } | |
220 | else | |
8251199e | 221 | cp_error ("invalid member template declaration `%D'", decl); |
e1467ff2 | 222 | |
a1da6cba | 223 | return error_mark_node; |
f84b4be9 | 224 | } |
e1467ff2 | 225 | |
f84b4be9 JM |
226 | /* Returns the template nesting level of the indicated class TYPE. |
227 | ||
228 | For example, in: | |
229 | template <class T> | |
230 | struct A | |
231 | { | |
232 | template <class U> | |
233 | struct B {}; | |
234 | }; | |
235 | ||
39c01e4c MM |
236 | A<T>::B<U> has depth two, while A<T> has depth one. |
237 | Both A<T>::B<int> and A<int>::B<U> have depth one, if | |
238 | COUNT_SPECIALIZATIONS is 0 or if they are instantiations, not | |
239 | specializations. | |
240 | ||
241 | This function is guaranteed to return 0 if passed NULL_TREE so | |
242 | that, for example, `template_class_depth (current_class_type)' is | |
243 | always safe. */ | |
f84b4be9 | 244 | |
e9659ab0 | 245 | static int |
39c01e4c | 246 | template_class_depth_real (type, count_specializations) |
f84b4be9 | 247 | tree type; |
39c01e4c | 248 | int count_specializations; |
f84b4be9 | 249 | { |
93cdc044 | 250 | int depth; |
f84b4be9 | 251 | |
2c73f9f5 | 252 | for (depth = 0; |
ed44da02 MM |
253 | type && TREE_CODE (type) != NAMESPACE_DECL; |
254 | type = (TREE_CODE (type) == FUNCTION_DECL) | |
4f1c5b7d | 255 | ? CP_DECL_CONTEXT (type) : TYPE_CONTEXT (type)) |
ed44da02 MM |
256 | { |
257 | if (TREE_CODE (type) != FUNCTION_DECL) | |
258 | { | |
259 | if (CLASSTYPE_TEMPLATE_INFO (type) | |
260 | && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)) | |
261 | && ((count_specializations | |
370af2d5 | 262 | && CLASSTYPE_TEMPLATE_SPECIALIZATION (type)) |
ed44da02 MM |
263 | || uses_template_parms (CLASSTYPE_TI_ARGS (type)))) |
264 | ++depth; | |
265 | } | |
266 | else | |
267 | { | |
268 | if (DECL_TEMPLATE_INFO (type) | |
269 | && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (type)) | |
270 | && ((count_specializations | |
271 | && DECL_TEMPLATE_SPECIALIZATION (type)) | |
272 | || uses_template_parms (DECL_TI_ARGS (type)))) | |
273 | ++depth; | |
274 | } | |
275 | } | |
f84b4be9 JM |
276 | |
277 | return depth; | |
e1467ff2 | 278 | } |
98c1c668 | 279 | |
39c01e4c MM |
280 | /* Returns the template nesting level of the indicated class TYPE. |
281 | Like template_class_depth_real, but instantiations do not count in | |
282 | the depth. */ | |
283 | ||
284 | int | |
285 | template_class_depth (type) | |
286 | tree type; | |
287 | { | |
288 | return template_class_depth_real (type, /*count_specializations=*/0); | |
289 | } | |
290 | ||
cae40af6 JM |
291 | /* Returns 1 if processing DECL as part of do_pending_inlines |
292 | needs us to push template parms. */ | |
293 | ||
294 | static int | |
295 | inline_needs_template_parms (decl) | |
296 | tree decl; | |
297 | { | |
298 | if (! DECL_TEMPLATE_INFO (decl)) | |
299 | return 0; | |
f84b4be9 | 300 | |
36a117a5 | 301 | return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl))) |
cae40af6 JM |
302 | > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl))); |
303 | } | |
304 | ||
305 | /* Subroutine of maybe_begin_member_template_processing. | |
306 | Push the template parms in PARMS, starting from LEVELS steps into the | |
307 | chain, and ending at the beginning, since template parms are listed | |
308 | innermost first. */ | |
309 | ||
310 | static void | |
311 | push_inline_template_parms_recursive (parmlist, levels) | |
312 | tree parmlist; | |
313 | int levels; | |
314 | { | |
315 | tree parms = TREE_VALUE (parmlist); | |
316 | int i; | |
317 | ||
318 | if (levels > 1) | |
319 | push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1); | |
786b5245 | 320 | |
98c1c668 | 321 | ++processing_template_decl; |
cae40af6 | 322 | current_template_parms |
98c1c668 JM |
323 | = tree_cons (build_int_2 (0, processing_template_decl), |
324 | parms, current_template_parms); | |
cae40af6 JM |
325 | TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1; |
326 | ||
786b5245 | 327 | pushlevel (0); |
98c1c668 JM |
328 | for (i = 0; i < TREE_VEC_LENGTH (parms); ++i) |
329 | { | |
786b5245 | 330 | tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i)); |
2f939d94 | 331 | my_friendly_assert (DECL_P (parm), 0); |
cae40af6 | 332 | |
98c1c668 JM |
333 | switch (TREE_CODE (parm)) |
334 | { | |
786b5245 | 335 | case TYPE_DECL: |
73b0fce8 | 336 | case TEMPLATE_DECL: |
98c1c668 JM |
337 | pushdecl (parm); |
338 | break; | |
786b5245 MM |
339 | |
340 | case PARM_DECL: | |
341 | { | |
fc03edb3 MM |
342 | /* Make a CONST_DECL as is done in process_template_parm. |
343 | It is ugly that we recreate this here; the original | |
344 | version built in process_template_parm is no longer | |
345 | available. */ | |
786b5245 MM |
346 | tree decl = build_decl (CONST_DECL, DECL_NAME (parm), |
347 | TREE_TYPE (parm)); | |
fc03edb3 | 348 | SET_DECL_ARTIFICIAL (decl); |
786b5245 | 349 | DECL_INITIAL (decl) = DECL_INITIAL (parm); |
cd9f6678 | 350 | SET_DECL_TEMPLATE_PARM_P (decl); |
786b5245 MM |
351 | pushdecl (decl); |
352 | } | |
cae40af6 | 353 | break; |
786b5245 | 354 | |
98c1c668 JM |
355 | default: |
356 | my_friendly_abort (0); | |
357 | } | |
358 | } | |
359 | } | |
360 | ||
cae40af6 JM |
361 | /* Restore the template parameter context for a member template or |
362 | a friend template defined in a class definition. */ | |
363 | ||
364 | void | |
365 | maybe_begin_member_template_processing (decl) | |
366 | tree decl; | |
367 | { | |
368 | tree parms; | |
3dcaad8b | 369 | int levels = 0; |
cae40af6 | 370 | |
3dcaad8b MM |
371 | if (inline_needs_template_parms (decl)) |
372 | { | |
373 | parms = DECL_TEMPLATE_PARMS (most_general_template (decl)); | |
374 | levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl; | |
cae40af6 | 375 | |
3dcaad8b MM |
376 | if (DECL_TEMPLATE_SPECIALIZATION (decl)) |
377 | { | |
378 | --levels; | |
379 | parms = TREE_CHAIN (parms); | |
380 | } | |
cae40af6 | 381 | |
3dcaad8b | 382 | push_inline_template_parms_recursive (parms, levels); |
cae40af6 JM |
383 | } |
384 | ||
3dcaad8b MM |
385 | /* Remember how many levels of template parameters we pushed so that |
386 | we can pop them later. */ | |
387 | if (!inline_parm_levels) | |
388 | VARRAY_INT_INIT (inline_parm_levels, 4, "inline_parm_levels"); | |
389 | if (inline_parm_levels_used == inline_parm_levels->num_elements) | |
390 | VARRAY_GROW (inline_parm_levels, 2 * inline_parm_levels_used); | |
391 | VARRAY_INT (inline_parm_levels, inline_parm_levels_used) = levels; | |
392 | ++inline_parm_levels_used; | |
cae40af6 JM |
393 | } |
394 | ||
98c1c668 JM |
395 | /* Undo the effects of begin_member_template_processing. */ |
396 | ||
397 | void | |
b370501f | 398 | maybe_end_member_template_processing () |
98c1c668 | 399 | { |
3dcaad8b MM |
400 | int i; |
401 | ||
402 | if (!inline_parm_levels_used) | |
98c1c668 JM |
403 | return; |
404 | ||
3dcaad8b MM |
405 | --inline_parm_levels_used; |
406 | for (i = 0; | |
407 | i < VARRAY_INT (inline_parm_levels, inline_parm_levels_used); | |
408 | ++i) | |
cae40af6 JM |
409 | { |
410 | --processing_template_decl; | |
411 | current_template_parms = TREE_CHAIN (current_template_parms); | |
412 | poplevel (0, 0, 0); | |
413 | } | |
98c1c668 JM |
414 | } |
415 | ||
cae40af6 | 416 | /* Returns non-zero iff T is a member template function. We must be |
f84b4be9 | 417 | careful as in |
75650646 MM |
418 | |
419 | template <class T> class C { void f(); } | |
420 | ||
421 | Here, f is a template function, and a member, but not a member | |
422 | template. This function does not concern itself with the origin of | |
423 | T, only its present state. So if we have | |
424 | ||
425 | template <class T> class C { template <class U> void f(U); } | |
426 | ||
427 | then neither C<int>::f<char> nor C<T>::f<double> is considered | |
f181d4ae MM |
428 | to be a member template. But, `template <class U> void |
429 | C<int>::f(U)' is considered a member template. */ | |
98c1c668 | 430 | |
cae40af6 JM |
431 | int |
432 | is_member_template (t) | |
98c1c668 JM |
433 | tree t; |
434 | { | |
f181d4ae | 435 | if (!DECL_FUNCTION_TEMPLATE_P (t)) |
75650646 | 436 | /* Anything that isn't a function or a template function is |
aa5f3bad MM |
437 | certainly not a member template. */ |
438 | return 0; | |
439 | ||
cae40af6 | 440 | /* A local class can't have member templates. */ |
4f1c5b7d | 441 | if (decl_function_context (t)) |
cae40af6 JM |
442 | return 0; |
443 | ||
f181d4ae | 444 | return (DECL_FUNCTION_MEMBER_P (DECL_TEMPLATE_RESULT (t)) |
f84b4be9 JM |
445 | /* If there are more levels of template parameters than |
446 | there are template classes surrounding the declaration, | |
447 | then we have a member template. */ | |
f181d4ae | 448 | && (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (t)) > |
4f1c5b7d | 449 | template_class_depth (DECL_CONTEXT (t)))); |
f84b4be9 | 450 | } |
98c1c668 | 451 | |
ba4f4e5d | 452 | #if 0 /* UNUSED */ |
2ee4e084 MM |
453 | /* Returns non-zero iff T is a member template class. See |
454 | is_member_template for a description of what precisely constitutes | |
455 | a member template. */ | |
456 | ||
457 | int | |
458 | is_member_template_class (t) | |
459 | tree t; | |
460 | { | |
461 | if (!DECL_CLASS_TEMPLATE_P (t)) | |
462 | /* Anything that isn't a class template, is certainly not a member | |
463 | template. */ | |
464 | return 0; | |
465 | ||
466 | if (!DECL_CLASS_SCOPE_P (t)) | |
467 | /* Anything whose context isn't a class type is surely not a | |
468 | member template. */ | |
469 | return 0; | |
470 | ||
471 | /* If there are more levels of template parameters than there are | |
472 | template classes surrounding the declaration, then we have a | |
473 | member template. */ | |
36a117a5 | 474 | return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (t)) > |
2ee4e084 MM |
475 | template_class_depth (DECL_CONTEXT (t))); |
476 | } | |
ba4f4e5d | 477 | #endif |
2ee4e084 | 478 | |
36a117a5 MM |
479 | /* Return a new template argument vector which contains all of ARGS, |
480 | but has as its innermost set of arguments the EXTRA_ARGS. The | |
481 | resulting vector will be built on a temporary obstack, and so must | |
482 | be explicitly copied to the permanent obstack, if required. */ | |
e6f1275f JM |
483 | |
484 | static tree | |
36a117a5 MM |
485 | add_to_template_args (args, extra_args) |
486 | tree args; | |
487 | tree extra_args; | |
e6f1275f | 488 | { |
36a117a5 MM |
489 | tree new_args; |
490 | int extra_depth; | |
491 | int i; | |
492 | int j; | |
e6f1275f | 493 | |
36a117a5 | 494 | extra_depth = TMPL_ARGS_DEPTH (extra_args); |
f31c0a32 | 495 | new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth); |
e6f1275f | 496 | |
36a117a5 MM |
497 | for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i) |
498 | SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i)); | |
e6f1275f | 499 | |
36a117a5 MM |
500 | for (j = 1; j <= extra_depth; ++j, ++i) |
501 | SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j)); | |
502 | ||
e6f1275f JM |
503 | return new_args; |
504 | } | |
505 | ||
36a117a5 MM |
506 | /* Like add_to_template_args, but only the outermost ARGS are added to |
507 | the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH | |
508 | (EXTRA_ARGS) levels are added. This function is used to combine | |
509 | the template arguments from a partial instantiation with the | |
510 | template arguments used to attain the full instantiation from the | |
511 | partial instantiation. */ | |
98c1c668 | 512 | |
4966381a | 513 | static tree |
36a117a5 | 514 | add_outermost_template_args (args, extra_args) |
98c1c668 JM |
515 | tree args; |
516 | tree extra_args; | |
517 | { | |
518 | tree new_args; | |
519 | ||
e4a84209 MM |
520 | /* If there are more levels of EXTRA_ARGS than there are ARGS, |
521 | something very fishy is going on. */ | |
522 | my_friendly_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args), | |
523 | 0); | |
524 | ||
525 | /* If *all* the new arguments will be the EXTRA_ARGS, just return | |
526 | them. */ | |
527 | if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args)) | |
528 | return extra_args; | |
529 | ||
36a117a5 MM |
530 | /* For the moment, we make ARGS look like it contains fewer levels. */ |
531 | TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args); | |
532 | ||
533 | new_args = add_to_template_args (args, extra_args); | |
98c1c668 | 534 | |
36a117a5 MM |
535 | /* Now, we restore ARGS to its full dimensions. */ |
536 | TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args); | |
98c1c668 JM |
537 | |
538 | return new_args; | |
539 | } | |
5566b478 MS |
540 | |
541 | /* We've got a template header coming up; push to a new level for storing | |
542 | the parms. */ | |
8d08fdba | 543 | |
8d08fdba MS |
544 | void |
545 | begin_template_parm_list () | |
546 | { | |
6757edfe MM |
547 | /* We use a non-tag-transparent scope here, which causes pushtag to |
548 | put tags in this scope, rather than in the enclosing class or | |
549 | namespace scope. This is the right thing, since we want | |
550 | TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a | |
551 | global template class, push_template_decl handles putting the | |
552 | TEMPLATE_DECL into top-level scope. For a nested template class, | |
553 | e.g.: | |
554 | ||
555 | template <class T> struct S1 { | |
556 | template <class T> struct S2 {}; | |
557 | }; | |
558 | ||
559 | pushtag contains special code to call pushdecl_with_scope on the | |
560 | TEMPLATE_DECL for S2. */ | |
74b846e0 | 561 | begin_scope (sk_template_parms); |
5156628f | 562 | ++processing_template_decl; |
67ffc812 | 563 | ++processing_template_parmlist; |
386b8a85 JM |
564 | note_template_header (0); |
565 | } | |
566 | ||
6c30752f MM |
567 | /* This routine is called when a specialization is declared. If it is |
568 | illegal to declare a specialization here, an error is reported. */ | |
569 | ||
e9659ab0 | 570 | static void |
6c30752f MM |
571 | check_specialization_scope () |
572 | { | |
573 | tree scope = current_scope (); | |
3ddfb0e6 | 574 | |
6c30752f MM |
575 | /* [temp.expl.spec] |
576 | ||
577 | An explicit specialization shall be declared in the namespace of | |
578 | which the template is a member, or, for member templates, in the | |
579 | namespace of which the enclosing class or enclosing class | |
580 | template is a member. An explicit specialization of a member | |
581 | function, member class or static data member of a class template | |
582 | shall be declared in the namespace of which the class template | |
583 | is a member. */ | |
584 | if (scope && TREE_CODE (scope) != NAMESPACE_DECL) | |
8251199e | 585 | cp_error ("explicit specialization in non-namespace scope `%D'", |
6c30752f | 586 | scope); |
3ddfb0e6 | 587 | |
6c30752f MM |
588 | /* [temp.expl.spec] |
589 | ||
590 | In an explicit specialization declaration for a member of a class | |
591 | template or a member template that appears in namespace scope, | |
592 | the member template and some of its enclosing class templates may | |
593 | remain unspecialized, except that the declaration shall not | |
594 | explicitly specialize a class member template if its enclosing | |
595 | class templates are not explicitly specialized as well. */ | |
596 | if (current_template_parms) | |
fd4de5ff | 597 | cp_error ("enclosing class templates are not explicitly specialized"); |
6c30752f MM |
598 | } |
599 | ||
386b8a85 JM |
600 | /* We've just seen template <>. */ |
601 | ||
602 | void | |
603 | begin_specialization () | |
604 | { | |
74b846e0 | 605 | begin_scope (sk_template_spec); |
386b8a85 | 606 | note_template_header (1); |
6c30752f | 607 | check_specialization_scope (); |
386b8a85 JM |
608 | } |
609 | ||
386b8a85 JM |
610 | /* Called at then end of processing a declaration preceeded by |
611 | template<>. */ | |
612 | ||
613 | void | |
614 | end_specialization () | |
615 | { | |
74b846e0 | 616 | finish_scope (); |
386b8a85 JM |
617 | reset_specialization (); |
618 | } | |
619 | ||
386b8a85 JM |
620 | /* Any template <>'s that we have seen thus far are not referring to a |
621 | function specialization. */ | |
622 | ||
623 | void | |
624 | reset_specialization () | |
625 | { | |
626 | processing_specialization = 0; | |
627 | template_header_count = 0; | |
628 | } | |
629 | ||
386b8a85 JM |
630 | /* We've just seen a template header. If SPECIALIZATION is non-zero, |
631 | it was of the form template <>. */ | |
632 | ||
4966381a | 633 | static void |
386b8a85 JM |
634 | note_template_header (specialization) |
635 | int specialization; | |
636 | { | |
637 | processing_specialization = specialization; | |
638 | template_header_count++; | |
639 | } | |
640 | ||
75650646 | 641 | /* We're beginning an explicit instantiation. */ |
386b8a85 | 642 | |
75650646 MM |
643 | void |
644 | begin_explicit_instantiation () | |
386b8a85 | 645 | { |
75650646 MM |
646 | ++processing_explicit_instantiation; |
647 | } | |
386b8a85 | 648 | |
386b8a85 | 649 | |
75650646 MM |
650 | void |
651 | end_explicit_instantiation () | |
652 | { | |
653 | my_friendly_assert(processing_explicit_instantiation > 0, 0); | |
654 | --processing_explicit_instantiation; | |
655 | } | |
386b8a85 | 656 | |
36a117a5 MM |
657 | /* The TYPE is being declared. If it is a template type, that means it |
658 | is a partial specialization. Do appropriate error-checking. */ | |
659 | ||
660 | void | |
661 | maybe_process_partial_specialization (type) | |
662 | tree type; | |
663 | { | |
664 | if (IS_AGGR_TYPE (type) && CLASSTYPE_USE_TEMPLATE (type)) | |
665 | { | |
666 | if (CLASSTYPE_IMPLICIT_INSTANTIATION (type) | |
d0f062fb | 667 | && !COMPLETE_TYPE_P (type)) |
36a117a5 | 668 | { |
700466c2 JM |
669 | if (current_namespace |
670 | != decl_namespace_context (CLASSTYPE_TI_TEMPLATE (type))) | |
671 | { | |
672 | cp_pedwarn ("specializing `%#T' in different namespace", type); | |
673 | cp_pedwarn_at (" from definition of `%#D'", | |
674 | CLASSTYPE_TI_TEMPLATE (type)); | |
675 | } | |
370af2d5 | 676 | SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type); |
36a117a5 MM |
677 | if (processing_template_decl) |
678 | push_template_decl (TYPE_MAIN_DECL (type)); | |
679 | } | |
680 | else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type)) | |
8251199e | 681 | cp_error ("specialization of `%T' after instantiation", type); |
36a117a5 | 682 | } |
20496fa2 JM |
683 | else if (processing_specialization) |
684 | cp_error ("explicit specialization of non-template `%T'", type); | |
36a117a5 MM |
685 | } |
686 | ||
75650646 MM |
687 | /* Retrieve the specialization (in the sense of [temp.spec] - a |
688 | specialization is either an instantiation or an explicit | |
689 | specialization) of TMPL for the given template ARGS. If there is | |
690 | no such specialization, return NULL_TREE. The ARGS are a vector of | |
691 | arguments, or a vector of vectors of arguments, in the case of | |
692 | templates with more than one level of parameters. */ | |
693 | ||
694 | static tree | |
695 | retrieve_specialization (tmpl, args) | |
696 | tree tmpl; | |
697 | tree args; | |
698 | { | |
699 | tree s; | |
700 | ||
701 | my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 0); | |
702 | ||
36a117a5 MM |
703 | /* There should be as many levels of arguments as there are |
704 | levels of parameters. */ | |
705 | my_friendly_assert (TMPL_ARGS_DEPTH (args) | |
706 | == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)), | |
707 | 0); | |
708 | ||
75650646 MM |
709 | for (s = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); |
710 | s != NULL_TREE; | |
711 | s = TREE_CHAIN (s)) | |
712 | if (comp_template_args (TREE_PURPOSE (s), args)) | |
713 | return TREE_VALUE (s); | |
714 | ||
715 | return NULL_TREE; | |
386b8a85 JM |
716 | } |
717 | ||
6dfbb909 | 718 | /* Like retrieve_speciailization, but for local declarations. */ |
9188c363 MM |
719 | |
720 | static tree | |
6dfbb909 | 721 | retrieve_local_specialization (tmpl) |
9188c363 | 722 | tree tmpl; |
9188c363 | 723 | { |
6dfbb909 | 724 | return (tree) htab_find (local_specializations, tmpl); |
9188c363 MM |
725 | } |
726 | ||
6757edfe MM |
727 | /* Returns non-zero iff DECL is a specialization of TMPL. */ |
728 | ||
729 | int | |
730 | is_specialization_of (decl, tmpl) | |
731 | tree decl; | |
732 | tree tmpl; | |
733 | { | |
734 | tree t; | |
735 | ||
736 | if (TREE_CODE (decl) == FUNCTION_DECL) | |
737 | { | |
738 | for (t = decl; | |
739 | t != NULL_TREE; | |
740 | t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE) | |
741 | if (t == tmpl) | |
742 | return 1; | |
743 | } | |
744 | else | |
745 | { | |
746 | my_friendly_assert (TREE_CODE (decl) == TYPE_DECL, 0); | |
747 | ||
748 | for (t = TREE_TYPE (decl); | |
749 | t != NULL_TREE; | |
750 | t = CLASSTYPE_USE_TEMPLATE (t) | |
751 | ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE) | |
9edc3913 | 752 | if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl))) |
6757edfe MM |
753 | return 1; |
754 | } | |
386b8a85 | 755 | |
6757edfe MM |
756 | return 0; |
757 | } | |
75650646 MM |
758 | |
759 | /* Register the specialization SPEC as a specialization of TMPL with | |
36a117a5 MM |
760 | the indicated ARGS. Returns SPEC, or an equivalent prior |
761 | declaration, if available. */ | |
75650646 | 762 | |
36a117a5 | 763 | static tree |
75650646 MM |
764 | register_specialization (spec, tmpl, args) |
765 | tree spec; | |
766 | tree tmpl; | |
767 | tree args; | |
768 | { | |
769 | tree s; | |
770 | ||
771 | my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 0); | |
772 | ||
36a117a5 MM |
773 | if (TREE_CODE (spec) == FUNCTION_DECL |
774 | && uses_template_parms (DECL_TI_ARGS (spec))) | |
775 | /* This is the FUNCTION_DECL for a partial instantiation. Don't | |
776 | register it; we want the corresponding TEMPLATE_DECL instead. | |
777 | We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than | |
778 | the more obvious `uses_template_parms (spec)' to avoid problems | |
779 | with default function arguments. In particular, given | |
780 | something like this: | |
781 | ||
782 | template <class T> void f(T t1, T t = T()) | |
783 | ||
784 | the default argument expression is not substituted for in an | |
785 | instantiation unless and until it is actually needed. */ | |
786 | return spec; | |
75650646 | 787 | |
36a117a5 MM |
788 | /* There should be as many levels of arguments as there are |
789 | levels of parameters. */ | |
790 | my_friendly_assert (TMPL_ARGS_DEPTH (args) | |
791 | == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)), | |
792 | 0); | |
793 | ||
75650646 MM |
794 | for (s = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); |
795 | s != NULL_TREE; | |
796 | s = TREE_CHAIN (s)) | |
22e9174f MM |
797 | { |
798 | tree fn = TREE_VALUE (s); | |
799 | ||
800 | /* We can sometimes try to re-register a specialization that we've | |
801 | already got. In particular, regenerate_decl_from_template | |
802 | calls duplicate_decls which will update the specialization | |
803 | list. But, we'll still get called again here anyhow. It's | |
804 | more convenient to simply allow this than to try to prevent it. */ | |
805 | if (fn == spec) | |
806 | return spec; | |
807 | else if (comp_template_args (TREE_PURPOSE (s), args)) | |
808 | { | |
809 | if (DECL_TEMPLATE_SPECIALIZATION (spec)) | |
810 | { | |
811 | if (DECL_TEMPLATE_INSTANTIATION (fn)) | |
812 | { | |
813 | if (TREE_USED (fn) | |
814 | || DECL_EXPLICIT_INSTANTIATION (fn)) | |
815 | { | |
816 | cp_error ("specialization of %D after instantiation", | |
817 | fn); | |
818 | return spec; | |
819 | } | |
820 | else | |
821 | { | |
822 | /* This situation should occur only if the first | |
823 | specialization is an implicit instantiation, | |
824 | the second is an explicit specialization, and | |
825 | the implicit instantiation has not yet been | |
826 | used. That situation can occur if we have | |
827 | implicitly instantiated a member function and | |
828 | then specialized it later. | |
829 | ||
830 | We can also wind up here if a friend | |
831 | declaration that looked like an instantiation | |
832 | turns out to be a specialization: | |
833 | ||
834 | template <class T> void foo(T); | |
835 | class S { friend void foo<>(int) }; | |
836 | template <> void foo(int); | |
837 | ||
838 | We transform the existing DECL in place so that | |
839 | any pointers to it become pointers to the | |
840 | updated declaration. | |
841 | ||
842 | If there was a definition for the template, but | |
843 | not for the specialization, we want this to | |
844 | look as if there is no definition, and vice | |
845 | versa. */ | |
846 | DECL_INITIAL (fn) = NULL_TREE; | |
847 | duplicate_decls (spec, fn); | |
848 | ||
849 | return fn; | |
850 | } | |
851 | } | |
852 | else if (DECL_TEMPLATE_SPECIALIZATION (fn)) | |
853 | { | |
854 | duplicate_decls (spec, fn); | |
855 | return fn; | |
856 | } | |
857 | } | |
858 | } | |
75650646 MM |
859 | } |
860 | ||
861 | DECL_TEMPLATE_SPECIALIZATIONS (tmpl) | |
e1b3e07d | 862 | = tree_cons (args, spec, DECL_TEMPLATE_SPECIALIZATIONS (tmpl)); |
36a117a5 MM |
863 | |
864 | return spec; | |
865 | } | |
866 | ||
867 | /* Unregister the specialization SPEC as a specialization of TMPL. | |
868 | Returns nonzero if the SPEC was listed as a specialization of | |
869 | TMPL. */ | |
870 | ||
871 | static int | |
872 | unregister_specialization (spec, tmpl) | |
873 | tree spec; | |
874 | tree tmpl; | |
875 | { | |
876 | tree* s; | |
877 | ||
878 | for (s = &DECL_TEMPLATE_SPECIALIZATIONS (tmpl); | |
879 | *s != NULL_TREE; | |
880 | s = &TREE_CHAIN (*s)) | |
881 | if (TREE_VALUE (*s) == spec) | |
882 | { | |
883 | *s = TREE_CHAIN (*s); | |
884 | return 1; | |
885 | } | |
886 | ||
887 | return 0; | |
75650646 MM |
888 | } |
889 | ||
6dfbb909 MM |
890 | /* Like register_specialization, but for local declarations. We are |
891 | registering SPEC, an instantiation of TMPL. */ | |
9188c363 MM |
892 | |
893 | static tree | |
6dfbb909 | 894 | register_local_specialization (spec, tmpl) |
9188c363 MM |
895 | tree spec; |
896 | tree tmpl; | |
9188c363 | 897 | { |
6dfbb909 MM |
898 | void **slot; |
899 | ||
900 | slot = htab_find_slot (local_specializations, tmpl, INSERT); | |
901 | *slot = spec; | |
9188c363 MM |
902 | |
903 | return spec; | |
904 | } | |
905 | ||
e1467ff2 MM |
906 | /* Print the list of candidate FNS in an error message. */ |
907 | ||
104bf76a | 908 | void |
e1467ff2 MM |
909 | print_candidates (fns) |
910 | tree fns; | |
911 | { | |
912 | tree fn; | |
913 | ||
d8e178a0 | 914 | const char *str = "candidates are:"; |
e1467ff2 MM |
915 | |
916 | for (fn = fns; fn != NULL_TREE; fn = TREE_CHAIN (fn)) | |
917 | { | |
d6479fe7 MM |
918 | tree f; |
919 | ||
920 | for (f = TREE_VALUE (fn); f; f = OVL_NEXT (f)) | |
921 | cp_error_at ("%s %+#D", str, OVL_CURRENT (f)); | |
e1467ff2 MM |
922 | str = " "; |
923 | } | |
924 | } | |
925 | ||
75650646 | 926 | /* Returns the template (one of the functions given by TEMPLATE_ID) |
e1467ff2 | 927 | which can be specialized to match the indicated DECL with the |
03017874 MM |
928 | explicit template args given in TEMPLATE_ID. The DECL may be |
929 | NULL_TREE if none is available. In that case, the functions in | |
930 | TEMPLATE_ID are non-members. | |
931 | ||
932 | If NEED_MEMBER_TEMPLATE is non-zero the function is known to be a | |
933 | specialization of a member template. | |
934 | ||
935 | The template args (those explicitly specified and those deduced) | |
936 | are output in a newly created vector *TARGS_OUT. | |
937 | ||
938 | If it is impossible to determine the result, an error message is | |
bf8f3f93 | 939 | issued. The error_mark_node is returned to indicate failure. */ |
75650646 | 940 | |
e9659ab0 | 941 | static tree |
e1467ff2 | 942 | determine_specialization (template_id, decl, targs_out, |
bf8f3f93 | 943 | need_member_template) |
386b8a85 | 944 | tree template_id; |
e1467ff2 | 945 | tree decl; |
386b8a85 JM |
946 | tree* targs_out; |
947 | int need_member_template; | |
386b8a85 | 948 | { |
03017874 MM |
949 | tree fns; |
950 | tree targs; | |
951 | tree explicit_targs; | |
952 | tree candidates = NULL_TREE; | |
953 | tree templates = NULL_TREE; | |
386b8a85 | 954 | |
e1467ff2 MM |
955 | *targs_out = NULL_TREE; |
956 | ||
f2e48b67 BK |
957 | if (template_id == error_mark_node) |
958 | return error_mark_node; | |
959 | ||
960 | fns = TREE_OPERAND (template_id, 0); | |
03017874 | 961 | explicit_targs = TREE_OPERAND (template_id, 1); |
f2e48b67 | 962 | |
27fafc8d JM |
963 | if (fns == error_mark_node) |
964 | return error_mark_node; | |
965 | ||
2c73f9f5 | 966 | /* Check for baselinks. */ |
91e490ab | 967 | if (BASELINK_P (fns)) |
2c73f9f5 | 968 | fns = TREE_VALUE (fns); |
386b8a85 | 969 | |
91e490ab MM |
970 | if (!is_overloaded_fn (fns)) |
971 | { | |
9dfce8fd | 972 | cp_error ("`%D' is not a function template", fns); |
91e490ab MM |
973 | return error_mark_node; |
974 | } | |
975 | ||
2c73f9f5 | 976 | for (; fns; fns = OVL_NEXT (fns)) |
386b8a85 | 977 | { |
75650646 MM |
978 | tree tmpl; |
979 | ||
9dfce8fd | 980 | tree fn = OVL_CURRENT (fns); |
03017874 MM |
981 | |
982 | if (TREE_CODE (fn) == TEMPLATE_DECL) | |
983 | /* DECL might be a specialization of FN. */ | |
984 | tmpl = fn; | |
985 | else if (need_member_template) | |
986 | /* FN is an ordinary member function, and we need a | |
987 | specialization of a member template. */ | |
988 | continue; | |
989 | else if (TREE_CODE (fn) != FUNCTION_DECL) | |
990 | /* We can get IDENTIFIER_NODEs here in certain erroneous | |
991 | cases. */ | |
992 | continue; | |
993 | else if (!DECL_FUNCTION_MEMBER_P (fn)) | |
994 | /* This is just an ordinary non-member function. Nothing can | |
995 | be a specialization of that. */ | |
996 | continue; | |
75650646 | 997 | else |
03017874 MM |
998 | { |
999 | tree decl_arg_types; | |
386b8a85 | 1000 | |
03017874 MM |
1001 | /* This is an ordinary member function. However, since |
1002 | we're here, we can assume it's enclosing class is a | |
1003 | template class. For example, | |
1004 | ||
1005 | template <typename T> struct S { void f(); }; | |
1006 | template <> void S<int>::f() {} | |
1007 | ||
1008 | Here, S<int>::f is a non-template, but S<int> is a | |
1009 | template class. If FN has the same type as DECL, we | |
1010 | might be in business. */ | |
1011 | if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)), | |
1012 | TREE_TYPE (TREE_TYPE (fn)))) | |
1013 | /* The return types differ. */ | |
1014 | continue; | |
1015 | ||
1016 | /* Adjust the type of DECL in case FN is a static member. */ | |
1017 | decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl)); | |
1018 | if (DECL_STATIC_FUNCTION_P (fn) | |
1019 | && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)) | |
1020 | decl_arg_types = TREE_CHAIN (decl_arg_types); | |
1021 | ||
1022 | if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)), | |
1023 | decl_arg_types)) | |
1024 | /* They match! */ | |
1025 | candidates = tree_cons (NULL_TREE, fn, candidates); | |
1026 | ||
1027 | continue; | |
1028 | } | |
386b8a85 | 1029 | |
bf8f3f93 MM |
1030 | /* See whether this function might be a specialization of this |
1031 | template. */ | |
1032 | targs = get_bindings (tmpl, decl, explicit_targs); | |
03017874 MM |
1033 | |
1034 | if (!targs) | |
bf8f3f93 | 1035 | /* We cannot deduce template arguments that when used to |
03017874 MM |
1036 | specialize TMPL will produce DECL. */ |
1037 | continue; | |
1038 | ||
1039 | /* Save this template, and the arguments deduced. */ | |
e1b3e07d | 1040 | templates = tree_cons (targs, tmpl, templates); |
386b8a85 | 1041 | } |
03017874 | 1042 | |
bf8f3f93 | 1043 | if (templates && TREE_CHAIN (templates)) |
386b8a85 | 1044 | { |
03017874 MM |
1045 | /* We have: |
1046 | ||
1047 | [temp.expl.spec] | |
1048 | ||
1049 | It is possible for a specialization with a given function | |
1050 | signature to be instantiated from more than one function | |
1051 | template. In such cases, explicit specification of the | |
1052 | template arguments must be used to uniquely identify the | |
1053 | function template specialization being specialized. | |
1054 | ||
1055 | Note that here, there's no suggestion that we're supposed to | |
1056 | determine which of the candidate templates is most | |
1057 | specialized. However, we, also have: | |
1058 | ||
1059 | [temp.func.order] | |
1060 | ||
1061 | Partial ordering of overloaded function template | |
1062 | declarations is used in the following contexts to select | |
1063 | the function template to which a function template | |
1064 | specialization refers: | |
1065 | ||
1066 | -- when an explicit specialization refers to a function | |
1067 | template. | |
1068 | ||
1069 | So, we do use the partial ordering rules, at least for now. | |
1070 | This extension can only serve to make illegal programs legal, | |
1071 | so it's safe. And, there is strong anecdotal evidence that | |
1072 | the committee intended the partial ordering rules to apply; | |
1073 | the EDG front-end has that behavior, and John Spicer claims | |
1074 | that the committee simply forgot to delete the wording in | |
1075 | [temp.expl.spec]. */ | |
1076 | tree tmpl = most_specialized (templates, decl, explicit_targs); | |
1077 | if (tmpl && tmpl != error_mark_node) | |
1078 | { | |
1079 | targs = get_bindings (tmpl, decl, explicit_targs); | |
e1b3e07d | 1080 | templates = tree_cons (targs, tmpl, NULL_TREE); |
03017874 | 1081 | } |
e1467ff2 MM |
1082 | } |
1083 | ||
03017874 | 1084 | if (templates == NULL_TREE && candidates == NULL_TREE) |
e1467ff2 | 1085 | { |
bf8f3f93 MM |
1086 | cp_error_at ("template-id `%D' for `%+D' does not match any template declaration", |
1087 | template_id, decl); | |
03017874 | 1088 | return error_mark_node; |
386b8a85 | 1089 | } |
03017874 | 1090 | else if ((templates && TREE_CHAIN (templates)) |
bf8f3f93 MM |
1091 | || (candidates && TREE_CHAIN (candidates)) |
1092 | || (templates && candidates)) | |
386b8a85 | 1093 | { |
bf8f3f93 MM |
1094 | cp_error_at ("ambiguous template specialization `%D' for `%+D'", |
1095 | template_id, decl); | |
1096 | chainon (candidates, templates); | |
1097 | print_candidates (candidates); | |
03017874 | 1098 | return error_mark_node; |
386b8a85 JM |
1099 | } |
1100 | ||
1101 | /* We have one, and exactly one, match. */ | |
03017874 MM |
1102 | if (candidates) |
1103 | { | |
1104 | /* It was a specialization of an ordinary member function in a | |
1105 | template class. */ | |
1106 | *targs_out = copy_node (DECL_TI_ARGS (TREE_VALUE (candidates))); | |
1107 | return DECL_TI_TEMPLATE (TREE_VALUE (candidates)); | |
1108 | } | |
1109 | ||
1110 | /* It was a specialization of a template. */ | |
17aec3eb | 1111 | targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates))); |
03017874 MM |
1112 | if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs)) |
1113 | { | |
1114 | *targs_out = copy_node (targs); | |
1115 | SET_TMPL_ARGS_LEVEL (*targs_out, | |
1116 | TMPL_ARGS_DEPTH (*targs_out), | |
1117 | TREE_PURPOSE (templates)); | |
1118 | } | |
1119 | else | |
1120 | *targs_out = TREE_PURPOSE (templates); | |
e1467ff2 | 1121 | return TREE_VALUE (templates); |
8d08fdba | 1122 | } |
6757edfe | 1123 | |
386b8a85 | 1124 | /* Check to see if the function just declared, as indicated in |
75650646 MM |
1125 | DECLARATOR, and in DECL, is a specialization of a function |
1126 | template. We may also discover that the declaration is an explicit | |
1127 | instantiation at this point. | |
1128 | ||
e1467ff2 | 1129 | Returns DECL, or an equivalent declaration that should be used |
03017874 MM |
1130 | instead if all goes well. Issues an error message if something is |
1131 | amiss. Returns error_mark_node if the error is not easily | |
1132 | recoverable. | |
75650646 | 1133 | |
75650646 MM |
1134 | FLAGS is a bitmask consisting of the following flags: |
1135 | ||
75650646 MM |
1136 | 2: The function has a definition. |
1137 | 4: The function is a friend. | |
75650646 MM |
1138 | |
1139 | The TEMPLATE_COUNT is the number of references to qualifying | |
1140 | template classes that appeared in the name of the function. For | |
1141 | example, in | |
1142 | ||
1143 | template <class T> struct S { void f(); }; | |
1144 | void S<int>::f(); | |
1145 | ||
1146 | the TEMPLATE_COUNT would be 1. However, explicitly specialized | |
1147 | classes are not counted in the TEMPLATE_COUNT, so that in | |
1148 | ||
1149 | template <class T> struct S {}; | |
1150 | template <> struct S<int> { void f(); } | |
36a117a5 | 1151 | template <> void S<int>::f(); |
75650646 MM |
1152 | |
1153 | the TEMPLATE_COUNT would be 0. (Note that this declaration is | |
1154 | illegal; there should be no template <>.) | |
1155 | ||
1156 | If the function is a specialization, it is marked as such via | |
1157 | DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO | |
1158 | is set up correctly, and it is added to the list of specializations | |
1159 | for that template. */ | |
386b8a85 | 1160 | |
e1467ff2 | 1161 | tree |
27bb8339 | 1162 | check_explicit_specialization (declarator, decl, template_count, flags) |
386b8a85 JM |
1163 | tree declarator; |
1164 | tree decl; | |
1165 | int template_count; | |
1166 | int flags; | |
1167 | { | |
75650646 MM |
1168 | int have_def = flags & 2; |
1169 | int is_friend = flags & 4; | |
1170 | int specialization = 0; | |
e1467ff2 | 1171 | int explicit_instantiation = 0; |
fd4de5ff | 1172 | int member_specialization = 0; |
75650646 MM |
1173 | tree ctype = DECL_CLASS_CONTEXT (decl); |
1174 | tree dname = DECL_NAME (decl); | |
74b846e0 | 1175 | tmpl_spec_kind tsk; |
386b8a85 | 1176 | |
74b846e0 | 1177 | tsk = current_tmpl_spec_kind (template_count); |
75650646 | 1178 | |
74b846e0 MM |
1179 | switch (tsk) |
1180 | { | |
1181 | case tsk_none: | |
1182 | if (processing_specialization) | |
75650646 | 1183 | { |
fd4de5ff MM |
1184 | specialization = 1; |
1185 | SET_DECL_TEMPLATE_SPECIALIZATION (decl); | |
75650646 | 1186 | } |
74b846e0 | 1187 | else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR) |
386b8a85 | 1188 | { |
74b846e0 MM |
1189 | if (is_friend) |
1190 | /* This could be something like: | |
75650646 | 1191 | |
74b846e0 MM |
1192 | template <class T> void f(T); |
1193 | class S { friend void f<>(int); } */ | |
1194 | specialization = 1; | |
1195 | else | |
1196 | { | |
1197 | /* This case handles bogus declarations like template <> | |
1198 | template <class T> void f<int>(); */ | |
1199 | ||
1200 | cp_error ("template-id `%D' in declaration of primary template", | |
1201 | declarator); | |
1202 | return decl; | |
1203 | } | |
1204 | } | |
1205 | break; | |
1206 | ||
1207 | case tsk_invalid_member_spec: | |
1208 | /* The error has already been reported in | |
1209 | check_specialization_scope. */ | |
1210 | return error_mark_node; | |
1211 | ||
1212 | case tsk_invalid_expl_inst: | |
1213 | cp_error ("template parameter list used in explicit instantiation"); | |
1214 | ||
1215 | /* Fall through. */ | |
1216 | ||
1217 | case tsk_expl_inst: | |
fd4de5ff MM |
1218 | if (have_def) |
1219 | cp_error ("definition provided for explicit instantiation"); | |
74b846e0 | 1220 | |
fd4de5ff | 1221 | explicit_instantiation = 1; |
74b846e0 | 1222 | break; |
fd4de5ff | 1223 | |
74b846e0 MM |
1224 | case tsk_excessive_parms: |
1225 | cp_error ("too many template parameter lists in declaration of `%D'", | |
1226 | decl); | |
1227 | return error_mark_node; | |
75650646 | 1228 | |
74b846e0 MM |
1229 | /* Fall through. */ |
1230 | case tsk_expl_spec: | |
1231 | SET_DECL_TEMPLATE_SPECIALIZATION (decl); | |
1232 | if (ctype) | |
1233 | member_specialization = 1; | |
1234 | else | |
1235 | specialization = 1; | |
1236 | break; | |
1237 | ||
1238 | case tsk_insufficient_parms: | |
fd4de5ff MM |
1239 | if (template_header_count) |
1240 | { | |
74b846e0 MM |
1241 | cp_error("too few template parameter lists in declaration of `%D'", |
1242 | decl); | |
fd4de5ff | 1243 | return decl; |
c6f2ed0d | 1244 | } |
74b846e0 MM |
1245 | else if (ctype != NULL_TREE |
1246 | && !TYPE_BEING_DEFINED (ctype) | |
1247 | && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype) | |
1248 | && !is_friend) | |
1249 | { | |
1250 | /* For backwards compatibility, we accept: | |
fd4de5ff | 1251 | |
74b846e0 MM |
1252 | template <class T> struct S { void f(); }; |
1253 | void S<int>::f() {} // Missing template <> | |
fd4de5ff | 1254 | |
74b846e0 MM |
1255 | That used to be legal C++. */ |
1256 | if (pedantic) | |
1257 | cp_pedwarn | |
1258 | ("explicit specialization not preceded by `template <>'"); | |
1259 | specialization = 1; | |
1260 | SET_DECL_TEMPLATE_SPECIALIZATION (decl); | |
1261 | } | |
1262 | break; | |
1263 | ||
1264 | case tsk_template: | |
1265 | if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR) | |
c6f2ed0d | 1266 | { |
fd4de5ff MM |
1267 | /* This case handles bogus declarations like template <> |
1268 | template <class T> void f<int>(); */ | |
75650646 | 1269 | |
fd4de5ff MM |
1270 | cp_error ("template-id `%D' in declaration of primary template", |
1271 | declarator); | |
1272 | return decl; | |
386b8a85 | 1273 | } |
74b846e0 MM |
1274 | |
1275 | if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype)) | |
1276 | /* This is a specialization of a member template, without | |
1277 | specialization the containing class. Something like: | |
1278 | ||
1279 | template <class T> struct S { | |
1280 | template <class U> void f (U); | |
1281 | }; | |
1282 | template <> template <class U> void S<int>::f(U) {} | |
1283 | ||
1284 | That's a specialization -- but of the entire template. */ | |
1285 | specialization = 1; | |
1286 | break; | |
1287 | ||
1288 | default: | |
1289 | my_friendly_abort (20000309); | |
75650646 | 1290 | } |
386b8a85 | 1291 | |
670960ac JM |
1292 | if (specialization || member_specialization) |
1293 | { | |
1294 | tree t = TYPE_ARG_TYPES (TREE_TYPE (decl)); | |
1295 | for (; t; t = TREE_CHAIN (t)) | |
1296 | if (TREE_PURPOSE (t)) | |
1297 | { | |
8251199e JM |
1298 | cp_pedwarn |
1299 | ("default argument specified in explicit specialization"); | |
670960ac JM |
1300 | break; |
1301 | } | |
f3400fe2 JM |
1302 | if (current_lang_name == lang_name_c) |
1303 | cp_error ("template specialization with C linkage"); | |
670960ac JM |
1304 | } |
1305 | ||
e1467ff2 | 1306 | if (specialization || member_specialization || explicit_instantiation) |
75650646 MM |
1307 | { |
1308 | tree tmpl = NULL_TREE; | |
1309 | tree targs = NULL_TREE; | |
75650646 MM |
1310 | |
1311 | /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */ | |
386b8a85 JM |
1312 | if (TREE_CODE (declarator) != TEMPLATE_ID_EXPR) |
1313 | { | |
1314 | tree fns; | |
1315 | ||
1316 | my_friendly_assert (TREE_CODE (declarator) == IDENTIFIER_NODE, | |
1317 | 0); | |
1318 | if (!ctype) | |
30394414 | 1319 | fns = IDENTIFIER_NAMESPACE_VALUE (dname); |
386b8a85 JM |
1320 | else |
1321 | fns = dname; | |
1322 | ||
75650646 MM |
1323 | declarator = |
1324 | lookup_template_function (fns, NULL_TREE); | |
386b8a85 JM |
1325 | } |
1326 | ||
f2e48b67 BK |
1327 | if (declarator == error_mark_node) |
1328 | return error_mark_node; | |
1329 | ||
75650646 MM |
1330 | if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype)) |
1331 | { | |
8ec2ac34 | 1332 | if (!explicit_instantiation) |
6c30752f MM |
1333 | /* A specialization in class scope. This is illegal, |
1334 | but the error will already have been flagged by | |
1335 | check_specialization_scope. */ | |
1336 | return error_mark_node; | |
8ec2ac34 | 1337 | else |
b370501f KG |
1338 | { |
1339 | /* It's not legal to write an explicit instantiation in | |
1340 | class scope, e.g.: | |
8ec2ac34 | 1341 | |
b370501f | 1342 | class C { template void f(); } |
8ec2ac34 | 1343 | |
b370501f KG |
1344 | This case is caught by the parser. However, on |
1345 | something like: | |
8ec2ac34 | 1346 | |
b370501f | 1347 | template class C { void f(); }; |
8ec2ac34 | 1348 | |
b370501f KG |
1349 | (which is illegal) we can get here. The error will be |
1350 | issued later. */ | |
1351 | ; | |
1352 | } | |
8ec2ac34 | 1353 | |
e1467ff2 | 1354 | return decl; |
75650646 | 1355 | } |
8f032717 MM |
1356 | else if (TREE_CODE (TREE_OPERAND (declarator, 0)) == LOOKUP_EXPR) |
1357 | { | |
1358 | /* A friend declaration. We can't do much, because we don't | |
1359 | know what this resolves to, yet. */ | |
1360 | my_friendly_assert (is_friend != 0, 0); | |
1361 | my_friendly_assert (!explicit_instantiation, 0); | |
1362 | SET_DECL_IMPLICIT_INSTANTIATION (decl); | |
1363 | return decl; | |
1364 | } | |
75650646 MM |
1365 | else if (ctype != NULL_TREE |
1366 | && (TREE_CODE (TREE_OPERAND (declarator, 0)) == | |
1367 | IDENTIFIER_NODE)) | |
386b8a85 | 1368 | { |
75650646 MM |
1369 | /* Find the list of functions in ctype that have the same |
1370 | name as the declared function. */ | |
1371 | tree name = TREE_OPERAND (declarator, 0); | |
03017874 MM |
1372 | tree fns = NULL_TREE; |
1373 | int idx; | |
1374 | ||
75650646 MM |
1375 | if (name == constructor_name (ctype) |
1376 | || name == constructor_name_full (ctype)) | |
386b8a85 | 1377 | { |
75650646 MM |
1378 | int is_constructor = DECL_CONSTRUCTOR_P (decl); |
1379 | ||
1380 | if (is_constructor ? !TYPE_HAS_CONSTRUCTOR (ctype) | |
1381 | : !TYPE_HAS_DESTRUCTOR (ctype)) | |
1382 | { | |
1383 | /* From [temp.expl.spec]: | |
e1467ff2 | 1384 | |
75650646 MM |
1385 | If such an explicit specialization for the member |
1386 | of a class template names an implicitly-declared | |
1387 | special member function (clause _special_), the | |
e1467ff2 MM |
1388 | program is ill-formed. |
1389 | ||
1390 | Similar language is found in [temp.explicit]. */ | |
8251199e | 1391 | cp_error ("specialization of implicitly-declared special member function"); |
03017874 | 1392 | return error_mark_node; |
75650646 | 1393 | } |
386b8a85 | 1394 | |
42da2fd8 | 1395 | name = is_constructor ? ctor_identifier : dtor_identifier; |
75650646 | 1396 | } |
42da2fd8 | 1397 | |
03017874 MM |
1398 | if (!IDENTIFIER_TYPENAME_P (name)) |
1399 | { | |
1400 | idx = lookup_fnfields_1 (ctype, name); | |
1401 | if (idx >= 0) | |
1402 | fns = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (ctype), idx); | |
1403 | } | |
1404 | else | |
1405 | { | |
1406 | tree methods; | |
1407 | ||
1408 | /* For a type-conversion operator, we cannot do a | |
1409 | name-based lookup. We might be looking for `operator | |
1410 | int' which will be a specialization of `operator T'. | |
1411 | So, we find *all* the conversion operators, and then | |
1412 | select from them. */ | |
1413 | fns = NULL_TREE; | |
1414 | ||
1415 | methods = CLASSTYPE_METHOD_VEC (ctype); | |
1416 | if (methods) | |
1417 | for (idx = 2; idx < TREE_VEC_LENGTH (methods); ++idx) | |
1418 | { | |
1419 | tree ovl = TREE_VEC_ELT (methods, idx); | |
1420 | ||
1421 | if (!ovl || !DECL_CONV_FN_P (OVL_CURRENT (ovl))) | |
1422 | /* There are no more conversion functions. */ | |
1423 | break; | |
1424 | ||
1425 | /* Glue all these conversion functions together | |
1426 | with those we already have. */ | |
1427 | for (; ovl; ovl = OVL_NEXT (ovl)) | |
1428 | fns = ovl_cons (OVL_CURRENT (ovl), fns); | |
1429 | } | |
1430 | } | |
1431 | ||
386b8a85 JM |
1432 | if (fns == NULL_TREE) |
1433 | { | |
b3f738da MM |
1434 | cp_error ("no member function `%D' declared in `%T'", |
1435 | name, ctype); | |
03017874 | 1436 | return error_mark_node; |
386b8a85 JM |
1437 | } |
1438 | else | |
1439 | TREE_OPERAND (declarator, 0) = fns; | |
1440 | } | |
75650646 | 1441 | |
e1467ff2 MM |
1442 | /* Figure out what exactly is being specialized at this point. |
1443 | Note that for an explicit instantiation, even one for a | |
38e01259 | 1444 | member function, we cannot tell apriori whether the |
e1467ff2 | 1445 | instantiation is for a member template, or just a member |
36a117a5 MM |
1446 | function of a template class. Even if a member template is |
1447 | being instantiated, the member template arguments may be | |
1448 | elided if they can be deduced from the rest of the | |
1449 | declaration. */ | |
e1467ff2 MM |
1450 | tmpl = determine_specialization (declarator, decl, |
1451 | &targs, | |
bf8f3f93 | 1452 | member_specialization); |
386b8a85 | 1453 | |
03017874 MM |
1454 | if (!tmpl || tmpl == error_mark_node) |
1455 | /* We couldn't figure out what this declaration was | |
1456 | specializing. */ | |
1457 | return error_mark_node; | |
1458 | else | |
386b8a85 | 1459 | { |
25aab5d0 | 1460 | tree gen_tmpl = most_general_template (tmpl); |
36a117a5 | 1461 | |
e1467ff2 MM |
1462 | if (explicit_instantiation) |
1463 | { | |
03d0f4af | 1464 | /* We don't set DECL_EXPLICIT_INSTANTIATION here; that |
25aab5d0 MM |
1465 | is done by do_decl_instantiation later. */ |
1466 | ||
1467 | int arg_depth = TMPL_ARGS_DEPTH (targs); | |
1468 | int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)); | |
1469 | ||
1470 | if (arg_depth > parm_depth) | |
1471 | { | |
1472 | /* If TMPL is not the most general template (for | |
1473 | example, if TMPL is a friend template that is | |
1474 | injected into namespace scope), then there will | |
1475 | be too many levels fo TARGS. Remove some of them | |
1476 | here. */ | |
1477 | int i; | |
1478 | tree new_targs; | |
1479 | ||
f31c0a32 | 1480 | new_targs = make_tree_vec (parm_depth); |
25aab5d0 MM |
1481 | for (i = arg_depth - parm_depth; i < arg_depth; ++i) |
1482 | TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth)) | |
1483 | = TREE_VEC_ELT (targs, i); | |
1484 | targs = new_targs; | |
1485 | } | |
1486 | ||
74b846e0 | 1487 | return instantiate_template (tmpl, targs); |
e1467ff2 | 1488 | } |
74b846e0 MM |
1489 | |
1490 | /* If this is both a template specialization, then it's a | |
1491 | specialization of a member template of a template class. | |
1492 | In that case we want to return the TEMPLATE_DECL, not the | |
1493 | specialization of it. */ | |
1494 | if (tsk == tsk_template) | |
1495 | { | |
1496 | SET_DECL_TEMPLATE_SPECIALIZATION (tmpl); | |
1497 | return tmpl; | |
1498 | } | |
1499 | ||
36a117a5 MM |
1500 | /* If we though that the DECL was a member function, but it |
1501 | turns out to be specializing a static member function, | |
1502 | make DECL a static member function as well. */ | |
1503 | if (DECL_STATIC_FUNCTION_P (tmpl) | |
1504 | && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)) | |
366c0f1e | 1505 | { |
3afb32a4 | 1506 | revert_static_member_fn (decl); |
366c0f1e MM |
1507 | last_function_parms = TREE_CHAIN (last_function_parms); |
1508 | } | |
e1467ff2 | 1509 | |
36a117a5 | 1510 | /* Set up the DECL_TEMPLATE_INFO for DECL. */ |
e1b3e07d | 1511 | DECL_TEMPLATE_INFO (decl) = tree_cons (tmpl, targs, NULL_TREE); |
36a117a5 | 1512 | |
e1467ff2 MM |
1513 | /* Mangle the function name appropriately. Note that we do |
1514 | not mangle specializations of non-template member | |
1515 | functions of template classes, e.g. with | |
36a117a5 | 1516 | |
e1467ff2 | 1517 | template <class T> struct S { void f(); } |
36a117a5 | 1518 | |
e1467ff2 | 1519 | and given the specialization |
36a117a5 | 1520 | |
e1467ff2 | 1521 | template <> void S<int>::f() {} |
36a117a5 | 1522 | |
e1467ff2 MM |
1523 | we do not mangle S<int>::f() here. That's because it's |
1524 | just an ordinary member function and doesn't need special | |
36a117a5 | 1525 | treatment. We do this here so that the ordinary, |
74b846e0 | 1526 | non-template, name-mangling algorithm will not be used |
36a117a5 | 1527 | later. */ |
e1467ff2 | 1528 | if ((is_member_template (tmpl) || ctype == NULL_TREE) |
75650646 | 1529 | && name_mangling_version >= 1) |
36a117a5 | 1530 | set_mangled_name_for_template_decl (decl); |
386b8a85 JM |
1531 | |
1532 | if (is_friend && !have_def) | |
36a117a5 MM |
1533 | /* This is not really a declaration of a specialization. |
1534 | It's just the name of an instantiation. But, it's not | |
1535 | a request for an instantiation, either. */ | |
fbf1c34b | 1536 | SET_DECL_IMPLICIT_INSTANTIATION (decl); |
75650646 | 1537 | |
36a117a5 MM |
1538 | /* Register this specialization so that we can find it |
1539 | again. */ | |
1540 | decl = register_specialization (decl, gen_tmpl, targs); | |
386b8a85 JM |
1541 | } |
1542 | } | |
75650646 | 1543 | |
e1467ff2 | 1544 | return decl; |
386b8a85 | 1545 | } |
75650646 | 1546 | |
39c01e4c MM |
1547 | /* TYPE is being declared. Verify that the use of template headers |
1548 | and such is reasonable. Issue error messages if not. */ | |
1549 | ||
1550 | void | |
1551 | maybe_check_template_type (type) | |
1552 | tree type; | |
1553 | { | |
1554 | if (template_header_count) | |
1555 | { | |
1556 | /* We are in the scope of some `template <...>' header. */ | |
1557 | ||
1558 | int context_depth | |
1559 | = template_class_depth_real (TYPE_CONTEXT (type), | |
1560 | /*count_specializations=*/1); | |
1561 | ||
1562 | if (template_header_count <= context_depth) | |
1563 | /* This is OK; the template headers are for the context. We | |
1564 | are actually too lenient here; like | |
1565 | check_explicit_specialization we should consider the number | |
1566 | of template types included in the actual declaration. For | |
1567 | example, | |
1568 | ||
1569 | template <class T> struct S { | |
1570 | template <class U> template <class V> | |
1571 | struct I {}; | |
1572 | }; | |
1573 | ||
1574 | is illegal, but: | |
1575 | ||
1576 | template <class T> struct S { | |
1577 | template <class U> struct I; | |
1578 | }; | |
1579 | ||
1580 | template <class T> template <class U. | |
1581 | struct S<T>::I {}; | |
1582 | ||
1583 | is not. */ | |
1584 | ; | |
1585 | else if (template_header_count > context_depth + 1) | |
1586 | /* There are two many template parameter lists. */ | |
8251199e | 1587 | cp_error ("too many template parameter lists in declaration of `%T'", type); |
39c01e4c MM |
1588 | } |
1589 | } | |
1590 | ||
75650646 MM |
1591 | /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template |
1592 | parameters. These are represented in the same format used for | |
1593 | DECL_TEMPLATE_PARMS. */ | |
1594 | ||
1595 | int comp_template_parms (parms1, parms2) | |
1596 | tree parms1; | |
1597 | tree parms2; | |
1598 | { | |
1599 | tree p1; | |
1600 | tree p2; | |
1601 | ||
1602 | if (parms1 == parms2) | |
1603 | return 1; | |
1604 | ||
1605 | for (p1 = parms1, p2 = parms2; | |
1606 | p1 != NULL_TREE && p2 != NULL_TREE; | |
1607 | p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2)) | |
1608 | { | |
1609 | tree t1 = TREE_VALUE (p1); | |
1610 | tree t2 = TREE_VALUE (p2); | |
1611 | int i; | |
1612 | ||
1613 | my_friendly_assert (TREE_CODE (t1) == TREE_VEC, 0); | |
1614 | my_friendly_assert (TREE_CODE (t2) == TREE_VEC, 0); | |
1615 | ||
1616 | if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2)) | |
1617 | return 0; | |
1618 | ||
1619 | for (i = 0; i < TREE_VEC_LENGTH (t2); ++i) | |
1620 | { | |
1621 | tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i)); | |
1622 | tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i)); | |
1623 | ||
1624 | if (TREE_CODE (parm1) != TREE_CODE (parm2)) | |
1625 | return 0; | |
1626 | ||
1627 | if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM) | |
1628 | continue; | |
3bfdc719 | 1629 | else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2))) |
75650646 MM |
1630 | return 0; |
1631 | } | |
1632 | } | |
1633 | ||
1634 | if ((p1 != NULL_TREE) != (p2 != NULL_TREE)) | |
1635 | /* One set of parameters has more parameters lists than the | |
1636 | other. */ | |
1637 | return 0; | |
1638 | ||
1639 | return 1; | |
1640 | } | |
1641 | ||
f3400fe2 JM |
1642 | /* Complain if DECL shadows a template parameter. |
1643 | ||
1644 | [temp.local]: A template-parameter shall not be redeclared within its | |
1645 | scope (including nested scopes). */ | |
1646 | ||
1647 | void | |
1648 | check_template_shadow (decl) | |
1649 | tree decl; | |
1650 | { | |
8f032717 MM |
1651 | tree olddecl; |
1652 | ||
b5d9b9ab MM |
1653 | /* If we're not in a template, we can't possibly shadow a template |
1654 | parameter. */ | |
1655 | if (!current_template_parms) | |
1656 | return; | |
1657 | ||
1658 | /* Figure out what we're shadowing. */ | |
8f032717 MM |
1659 | if (TREE_CODE (decl) == OVERLOAD) |
1660 | decl = OVL_CURRENT (decl); | |
1661 | olddecl = IDENTIFIER_VALUE (DECL_NAME (decl)); | |
f3400fe2 | 1662 | |
b5d9b9ab MM |
1663 | /* If there's no previous binding for this name, we're not shadowing |
1664 | anything, let alone a template parameter. */ | |
1665 | if (!olddecl) | |
1666 | return; | |
1667 | ||
1668 | /* If we're not shadowing a template parameter, we're done. Note | |
1669 | that OLDDECL might be an OVERLOAD (or perhaps even an | |
1670 | ERROR_MARK), so we can't just blithely assume it to be a _DECL | |
1671 | node. */ | |
2f939d94 | 1672 | if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl)) |
b5d9b9ab MM |
1673 | return; |
1674 | ||
1675 | /* We check for decl != olddecl to avoid bogus errors for using a | |
1676 | name inside a class. We check TPFI to avoid duplicate errors for | |
1677 | inline member templates. */ | |
1678 | if (decl == olddecl | |
1679 | || TEMPLATE_PARMS_FOR_INLINE (current_template_parms)) | |
1680 | return; | |
1681 | ||
1682 | cp_error_at ("declaration of `%#D'", decl); | |
1683 | cp_error_at (" shadows template parm `%#D'", olddecl); | |
f3400fe2 | 1684 | } |
22a7be53 | 1685 | |
f3400fe2 | 1686 | /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL, |
f84b4be9 JM |
1687 | ORIG_LEVEL, DECL, and TYPE. */ |
1688 | ||
1689 | static tree | |
1690 | build_template_parm_index (index, level, orig_level, decl, type) | |
1691 | int index; | |
1692 | int level; | |
1693 | int orig_level; | |
1694 | tree decl; | |
1695 | tree type; | |
1696 | { | |
1697 | tree t = make_node (TEMPLATE_PARM_INDEX); | |
1698 | TEMPLATE_PARM_IDX (t) = index; | |
1699 | TEMPLATE_PARM_LEVEL (t) = level; | |
1700 | TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level; | |
1701 | TEMPLATE_PARM_DECL (t) = decl; | |
1702 | TREE_TYPE (t) = type; | |
1703 | ||
1704 | return t; | |
1705 | } | |
1706 | ||
f84b4be9 | 1707 | /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose |
93cdc044 | 1708 | TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a |
f84b4be9 JM |
1709 | TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a |
1710 | new one is created. */ | |
1711 | ||
1712 | static tree | |
93cdc044 | 1713 | reduce_template_parm_level (index, type, levels) |
f84b4be9 JM |
1714 | tree index; |
1715 | tree type; | |
93cdc044 | 1716 | int levels; |
f84b4be9 JM |
1717 | { |
1718 | if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE | |
1719 | || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index)) | |
93cdc044 | 1720 | != TEMPLATE_PARM_LEVEL (index) - levels)) |
f84b4be9 JM |
1721 | { |
1722 | tree decl | |
1723 | = build_decl (TREE_CODE (TEMPLATE_PARM_DECL (index)), | |
1724 | DECL_NAME (TEMPLATE_PARM_DECL (index)), | |
1725 | type); | |
1726 | tree t | |
1727 | = build_template_parm_index (TEMPLATE_PARM_IDX (index), | |
93cdc044 | 1728 | TEMPLATE_PARM_LEVEL (index) - levels, |
f84b4be9 JM |
1729 | TEMPLATE_PARM_ORIG_LEVEL (index), |
1730 | decl, type); | |
1731 | TEMPLATE_PARM_DESCENDANTS (index) = t; | |
cae40af6 JM |
1732 | |
1733 | /* Template template parameters need this. */ | |
1734 | DECL_TEMPLATE_PARMS (decl) | |
1735 | = DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index)); | |
f84b4be9 JM |
1736 | } |
1737 | ||
1738 | return TEMPLATE_PARM_DESCENDANTS (index); | |
1739 | } | |
1740 | ||
8d08fdba | 1741 | /* Process information from new template parameter NEXT and append it to the |
5566b478 | 1742 | LIST being built. */ |
e92cc029 | 1743 | |
8d08fdba MS |
1744 | tree |
1745 | process_template_parm (list, next) | |
1746 | tree list, next; | |
1747 | { | |
1748 | tree parm; | |
1749 | tree decl = 0; | |
a292b002 | 1750 | tree defval; |
5566b478 | 1751 | int is_type, idx; |
f84b4be9 | 1752 | |
8d08fdba MS |
1753 | parm = next; |
1754 | my_friendly_assert (TREE_CODE (parm) == TREE_LIST, 259); | |
a292b002 MS |
1755 | defval = TREE_PURPOSE (parm); |
1756 | parm = TREE_VALUE (parm); | |
1757 | is_type = TREE_PURPOSE (parm) == class_type_node; | |
5566b478 MS |
1758 | |
1759 | if (list) | |
1760 | { | |
1761 | tree p = TREE_VALUE (tree_last (list)); | |
1762 | ||
1763 | if (TREE_CODE (p) == TYPE_DECL) | |
1764 | idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p)); | |
73b0fce8 KL |
1765 | else if (TREE_CODE (p) == TEMPLATE_DECL) |
1766 | idx = TEMPLATE_TYPE_IDX (TREE_TYPE (DECL_TEMPLATE_RESULT (p))); | |
5566b478 | 1767 | else |
f84b4be9 | 1768 | idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p)); |
5566b478 MS |
1769 | ++idx; |
1770 | } | |
1771 | else | |
1772 | idx = 0; | |
1773 | ||
8d08fdba MS |
1774 | if (!is_type) |
1775 | { | |
a292b002 | 1776 | my_friendly_assert (TREE_CODE (TREE_PURPOSE (parm)) == TREE_LIST, 260); |
8d08fdba | 1777 | /* is a const-param */ |
a292b002 | 1778 | parm = grokdeclarator (TREE_VALUE (parm), TREE_PURPOSE (parm), |
c11b6f21 | 1779 | PARM, 0, NULL_TREE); |
d490621d MM |
1780 | |
1781 | /* [temp.param] | |
1782 | ||
1783 | The top-level cv-qualifiers on the template-parameter are | |
1784 | ignored when determining its type. */ | |
1785 | TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm)); | |
1786 | ||
8d08fdba MS |
1787 | /* A template parameter is not modifiable. */ |
1788 | TREE_READONLY (parm) = 1; | |
c91a56d2 | 1789 | if (IS_AGGR_TYPE (TREE_TYPE (parm)) |
410c2fba KL |
1790 | && TREE_CODE (TREE_TYPE (parm)) != TEMPLATE_TYPE_PARM |
1791 | && TREE_CODE (TREE_TYPE (parm)) != TYPENAME_TYPE) | |
8d08fdba | 1792 | { |
8251199e | 1793 | cp_error ("`%#T' is not a valid type for a template constant parameter", |
c91a56d2 MS |
1794 | TREE_TYPE (parm)); |
1795 | if (DECL_NAME (parm) == NULL_TREE) | |
8251199e | 1796 | error (" a template type parameter must begin with `class' or `typename'"); |
8d08fdba MS |
1797 | TREE_TYPE (parm) = void_type_node; |
1798 | } | |
37c46b43 MS |
1799 | else if (pedantic |
1800 | && (TREE_CODE (TREE_TYPE (parm)) == REAL_TYPE | |
1801 | || TREE_CODE (TREE_TYPE (parm)) == COMPLEX_TYPE)) | |
8251199e | 1802 | cp_pedwarn ("`%T' is not a valid type for a template constant parameter", |
eb66be0e | 1803 | TREE_TYPE (parm)); |
8d08fdba | 1804 | decl = build_decl (CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm)); |
f84b4be9 JM |
1805 | DECL_INITIAL (parm) = DECL_INITIAL (decl) |
1806 | = build_template_parm_index (idx, processing_template_decl, | |
1807 | processing_template_decl, | |
1808 | decl, TREE_TYPE (parm)); | |
8d08fdba MS |
1809 | } |
1810 | else | |
1811 | { | |
73b0fce8 KL |
1812 | tree t; |
1813 | parm = TREE_VALUE (parm); | |
1814 | ||
1815 | if (parm && TREE_CODE (parm) == TEMPLATE_DECL) | |
1816 | { | |
33848bb0 | 1817 | t = make_aggr_type (TEMPLATE_TEMPLATE_PARM); |
73b0fce8 KL |
1818 | /* This is for distinguishing between real templates and template |
1819 | template parameters */ | |
1820 | TREE_TYPE (parm) = t; | |
1821 | TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t; | |
1822 | decl = parm; | |
1823 | } | |
1824 | else | |
1825 | { | |
33848bb0 | 1826 | t = make_aggr_type (TEMPLATE_TYPE_PARM); |
73b0fce8 KL |
1827 | /* parm is either IDENTIFIER_NODE or NULL_TREE */ |
1828 | decl = build_decl (TYPE_DECL, parm, t); | |
1829 | } | |
1830 | ||
d2e5ee5c MS |
1831 | TYPE_NAME (t) = decl; |
1832 | TYPE_STUB_DECL (t) = decl; | |
a292b002 | 1833 | parm = decl; |
f84b4be9 JM |
1834 | TEMPLATE_TYPE_PARM_INDEX (t) |
1835 | = build_template_parm_index (idx, processing_template_decl, | |
1836 | processing_template_decl, | |
1837 | decl, TREE_TYPE (parm)); | |
8d08fdba | 1838 | } |
8ccc31eb | 1839 | SET_DECL_ARTIFICIAL (decl); |
cd9f6678 | 1840 | SET_DECL_TEMPLATE_PARM_P (decl); |
8d08fdba | 1841 | pushdecl (decl); |
a292b002 | 1842 | parm = build_tree_list (defval, parm); |
8d08fdba MS |
1843 | return chainon (list, parm); |
1844 | } | |
1845 | ||
1846 | /* The end of a template parameter list has been reached. Process the | |
1847 | tree list into a parameter vector, converting each parameter into a more | |
1848 | useful form. Type parameters are saved as IDENTIFIER_NODEs, and others | |
1849 | as PARM_DECLs. */ | |
1850 | ||
1851 | tree | |
1852 | end_template_parm_list (parms) | |
1853 | tree parms; | |
1854 | { | |
5566b478 | 1855 | int nparms; |
8d08fdba | 1856 | tree parm; |
5566b478 MS |
1857 | tree saved_parmlist = make_tree_vec (list_length (parms)); |
1858 | ||
5566b478 MS |
1859 | current_template_parms |
1860 | = tree_cons (build_int_2 (0, processing_template_decl), | |
1861 | saved_parmlist, current_template_parms); | |
8d08fdba | 1862 | |
91e490ab MM |
1863 | for (parm = parms, nparms = 0; |
1864 | parm; | |
1865 | parm = TREE_CHAIN (parm), nparms++) | |
66191c20 | 1866 | TREE_VEC_ELT (saved_parmlist, nparms) = parm; |
a292b002 | 1867 | |
67ffc812 MM |
1868 | --processing_template_parmlist; |
1869 | ||
8d08fdba MS |
1870 | return saved_parmlist; |
1871 | } | |
1872 | ||
5566b478 MS |
1873 | /* end_template_decl is called after a template declaration is seen. */ |
1874 | ||
8d08fdba | 1875 | void |
5566b478 | 1876 | end_template_decl () |
8d08fdba | 1877 | { |
386b8a85 JM |
1878 | reset_specialization (); |
1879 | ||
5156628f | 1880 | if (! processing_template_decl) |
73aad9b9 JM |
1881 | return; |
1882 | ||
5566b478 | 1883 | /* This matches the pushlevel in begin_template_parm_list. */ |
74b846e0 | 1884 | finish_scope (); |
8d08fdba | 1885 | |
5566b478 MS |
1886 | --processing_template_decl; |
1887 | current_template_parms = TREE_CHAIN (current_template_parms); | |
5566b478 | 1888 | } |
8d08fdba | 1889 | |
36a117a5 MM |
1890 | /* Given a template argument vector containing the template PARMS. |
1891 | The innermost PARMS are given first. */ | |
9a3b49ac MS |
1892 | |
1893 | tree | |
1894 | current_template_args () | |
5566b478 | 1895 | { |
36a117a5 | 1896 | tree header; |
b370501f | 1897 | tree args = NULL_TREE; |
36a117a5 | 1898 | int length = TMPL_PARMS_DEPTH (current_template_parms); |
98c1c668 JM |
1899 | int l = length; |
1900 | ||
36a117a5 MM |
1901 | /* If there is only one level of template parameters, we do not |
1902 | create a TREE_VEC of TREE_VECs. Instead, we return a single | |
1903 | TREE_VEC containing the arguments. */ | |
1904 | if (length > 1) | |
1905 | args = make_tree_vec (length); | |
1906 | ||
1907 | for (header = current_template_parms; header; header = TREE_CHAIN (header)) | |
8d08fdba | 1908 | { |
5566b478 | 1909 | tree a = copy_node (TREE_VALUE (header)); |
36a117a5 MM |
1910 | int i; |
1911 | ||
5566b478 | 1912 | TREE_TYPE (a) = NULL_TREE; |
36a117a5 | 1913 | for (i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i) |
5566b478 | 1914 | { |
98c1c668 JM |
1915 | tree t = TREE_VEC_ELT (a, i); |
1916 | ||
36a117a5 | 1917 | /* T will be a list if we are called from within a |
98c1c668 JM |
1918 | begin/end_template_parm_list pair, but a vector directly |
1919 | if within a begin/end_member_template_processing pair. */ | |
1920 | if (TREE_CODE (t) == TREE_LIST) | |
1921 | { | |
1922 | t = TREE_VALUE (t); | |
1923 | ||
73b0fce8 KL |
1924 | if (TREE_CODE (t) == TYPE_DECL |
1925 | || TREE_CODE (t) == TEMPLATE_DECL) | |
98c1c668 JM |
1926 | t = TREE_TYPE (t); |
1927 | else | |
1928 | t = DECL_INITIAL (t); | |
36a117a5 | 1929 | TREE_VEC_ELT (a, i) = t; |
98c1c668 | 1930 | } |
5566b478 | 1931 | } |
36a117a5 MM |
1932 | |
1933 | if (length > 1) | |
1934 | TREE_VEC_ELT (args, --l) = a; | |
1935 | else | |
1936 | args = a; | |
8d08fdba MS |
1937 | } |
1938 | ||
9a3b49ac MS |
1939 | return args; |
1940 | } | |
75650646 | 1941 | |
e1467ff2 MM |
1942 | /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated |
1943 | template PARMS. Used by push_template_decl below. */ | |
1944 | ||
75650646 MM |
1945 | static tree |
1946 | build_template_decl (decl, parms) | |
1947 | tree decl; | |
1948 | tree parms; | |
1949 | { | |
1950 | tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE); | |
1951 | DECL_TEMPLATE_PARMS (tmpl) = parms; | |
1952 | DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl); | |
1953 | if (DECL_LANG_SPECIFIC (decl)) | |
1954 | { | |
4f1c5b7d | 1955 | DECL_VIRTUAL_CONTEXT (tmpl) = DECL_VIRTUAL_CONTEXT (decl); |
8f032717 MM |
1956 | DECL_STATIC_FUNCTION_P (tmpl) = DECL_STATIC_FUNCTION_P (decl); |
1957 | DECL_CONSTRUCTOR_P (tmpl) = DECL_CONSTRUCTOR_P (decl); | |
11f98788 | 1958 | DECL_NONCONVERTING_P (tmpl) = DECL_NONCONVERTING_P (decl); |
75650646 MM |
1959 | } |
1960 | ||
1961 | return tmpl; | |
1962 | } | |
1963 | ||
050367a3 MM |
1964 | struct template_parm_data |
1965 | { | |
6c30752f MM |
1966 | /* The level of the template parameters we are currently |
1967 | processing. */ | |
050367a3 | 1968 | int level; |
6c30752f MM |
1969 | |
1970 | /* The index of the specialization argument we are currently | |
1971 | processing. */ | |
1972 | int current_arg; | |
1973 | ||
1974 | /* An array whose size is the number of template parameters. The | |
1975 | elements are non-zero if the parameter has been used in any one | |
1976 | of the arguments processed so far. */ | |
050367a3 | 1977 | int* parms; |
6c30752f MM |
1978 | |
1979 | /* An array whose size is the number of template arguments. The | |
1980 | elements are non-zero if the argument makes use of template | |
1981 | parameters of this level. */ | |
1982 | int* arg_uses_template_parms; | |
050367a3 MM |
1983 | }; |
1984 | ||
1985 | /* Subroutine of push_template_decl used to see if each template | |
1986 | parameter in a partial specialization is used in the explicit | |
1987 | argument list. If T is of the LEVEL given in DATA (which is | |
1988 | treated as a template_parm_data*), then DATA->PARMS is marked | |
1989 | appropriately. */ | |
1990 | ||
1991 | static int | |
1992 | mark_template_parm (t, data) | |
1993 | tree t; | |
1994 | void* data; | |
1995 | { | |
1996 | int level; | |
1997 | int idx; | |
1998 | struct template_parm_data* tpd = (struct template_parm_data*) data; | |
1999 | ||
2000 | if (TREE_CODE (t) == TEMPLATE_PARM_INDEX) | |
2001 | { | |
2002 | level = TEMPLATE_PARM_LEVEL (t); | |
2003 | idx = TEMPLATE_PARM_IDX (t); | |
2004 | } | |
2005 | else | |
2006 | { | |
2007 | level = TEMPLATE_TYPE_LEVEL (t); | |
2008 | idx = TEMPLATE_TYPE_IDX (t); | |
2009 | } | |
2010 | ||
2011 | if (level == tpd->level) | |
6c30752f MM |
2012 | { |
2013 | tpd->parms[idx] = 1; | |
2014 | tpd->arg_uses_template_parms[tpd->current_arg] = 1; | |
2015 | } | |
050367a3 MM |
2016 | |
2017 | /* Return zero so that for_each_template_parm will continue the | |
2018 | traversal of the tree; we want to mark *every* template parm. */ | |
2019 | return 0; | |
2020 | } | |
2021 | ||
6c30752f MM |
2022 | /* Process the partial specialization DECL. */ |
2023 | ||
e9659ab0 | 2024 | static tree |
6c30752f MM |
2025 | process_partial_specialization (decl) |
2026 | tree decl; | |
2027 | { | |
2028 | tree type = TREE_TYPE (decl); | |
2029 | tree maintmpl = CLASSTYPE_TI_TEMPLATE (type); | |
2030 | tree specargs = CLASSTYPE_TI_ARGS (type); | |
2031 | tree inner_args = innermost_args (specargs); | |
2032 | tree inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms); | |
2033 | tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl); | |
2034 | int nargs = TREE_VEC_LENGTH (inner_args); | |
2035 | int ntparms = TREE_VEC_LENGTH (inner_parms); | |
2036 | int i; | |
2037 | int did_error_intro = 0; | |
6c30752f MM |
2038 | struct template_parm_data tpd; |
2039 | struct template_parm_data tpd2; | |
2040 | ||
6c30752f MM |
2041 | /* We check that each of the template parameters given in the |
2042 | partial specialization is used in the argument list to the | |
2043 | specialization. For example: | |
2044 | ||
2045 | template <class T> struct S; | |
2046 | template <class T> struct S<T*>; | |
2047 | ||
2048 | The second declaration is OK because `T*' uses the template | |
2049 | parameter T, whereas | |
2050 | ||
2051 | template <class T> struct S<int>; | |
2052 | ||
2053 | is no good. Even trickier is: | |
2054 | ||
2055 | template <class T> | |
2056 | struct S1 | |
2057 | { | |
2058 | template <class U> | |
2059 | struct S2; | |
2060 | template <class U> | |
2061 | struct S2<T>; | |
2062 | }; | |
2063 | ||
2064 | The S2<T> declaration is actually illegal; it is a | |
2065 | full-specialization. Of course, | |
2066 | ||
2067 | template <class U> | |
2068 | struct S2<T (*)(U)>; | |
2069 | ||
2070 | or some such would have been OK. */ | |
2071 | tpd.level = TMPL_PARMS_DEPTH (current_template_parms); | |
2072 | tpd.parms = alloca (sizeof (int) * ntparms); | |
1ddb2906 | 2073 | bzero ((PTR) tpd.parms, sizeof (int) * ntparms); |
6c30752f MM |
2074 | |
2075 | tpd.arg_uses_template_parms = alloca (sizeof (int) * nargs); | |
1ddb2906 | 2076 | bzero ((PTR) tpd.arg_uses_template_parms, sizeof (int) * nargs); |
6c30752f MM |
2077 | for (i = 0; i < nargs; ++i) |
2078 | { | |
2079 | tpd.current_arg = i; | |
2080 | for_each_template_parm (TREE_VEC_ELT (inner_args, i), | |
2081 | &mark_template_parm, | |
2082 | &tpd); | |
2083 | } | |
2084 | for (i = 0; i < ntparms; ++i) | |
2085 | if (tpd.parms[i] == 0) | |
2086 | { | |
2087 | /* One of the template parms was not used in the | |
2088 | specialization. */ | |
2089 | if (!did_error_intro) | |
2090 | { | |
8251199e | 2091 | cp_error ("template parameters not used in partial specialization:"); |
6c30752f MM |
2092 | did_error_intro = 1; |
2093 | } | |
2094 | ||
8251199e | 2095 | cp_error (" `%D'", |
6c30752f MM |
2096 | TREE_VALUE (TREE_VEC_ELT (inner_parms, i))); |
2097 | } | |
2098 | ||
2099 | /* [temp.class.spec] | |
2100 | ||
2101 | The argument list of the specialization shall not be identical to | |
2102 | the implicit argument list of the primary template. */ | |
2103 | if (comp_template_args (inner_args, | |
2104 | innermost_args (CLASSTYPE_TI_ARGS (TREE_TYPE | |
2105 | (maintmpl))))) | |
8251199e | 2106 | cp_error ("partial specialization `%T' does not specialize any template arguments", type); |
6c30752f MM |
2107 | |
2108 | /* [temp.class.spec] | |
2109 | ||
2110 | A partially specialized non-type argument expression shall not | |
2111 | involve template parameters of the partial specialization except | |
2112 | when the argument expression is a simple identifier. | |
2113 | ||
2114 | The type of a template parameter corresponding to a specialized | |
2115 | non-type argument shall not be dependent on a parameter of the | |
2116 | specialization. */ | |
2117 | my_friendly_assert (nargs == DECL_NTPARMS (maintmpl), 0); | |
2118 | tpd2.parms = 0; | |
2119 | for (i = 0; i < nargs; ++i) | |
2120 | { | |
2121 | tree arg = TREE_VEC_ELT (inner_args, i); | |
2122 | if (/* These first two lines are the `non-type' bit. */ | |
2f939d94 | 2123 | !TYPE_P (arg) |
6c30752f MM |
2124 | && TREE_CODE (arg) != TEMPLATE_DECL |
2125 | /* This next line is the `argument expression is not just a | |
2126 | simple identifier' condition and also the `specialized | |
2127 | non-type argument' bit. */ | |
2128 | && TREE_CODE (arg) != TEMPLATE_PARM_INDEX) | |
2129 | { | |
2130 | if (tpd.arg_uses_template_parms[i]) | |
8251199e | 2131 | cp_error ("template argument `%E' involves template parameter(s)", arg); |
6c30752f MM |
2132 | else |
2133 | { | |
2134 | /* Look at the corresponding template parameter, | |
2135 | marking which template parameters its type depends | |
2136 | upon. */ | |
2137 | tree type = | |
2138 | TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (main_inner_parms, | |
2139 | i))); | |
2140 | ||
2141 | if (!tpd2.parms) | |
2142 | { | |
2143 | /* We haven't yet initialized TPD2. Do so now. */ | |
2144 | tpd2.arg_uses_template_parms | |
2145 | = (int*) alloca (sizeof (int) * nargs); | |
104bf76a | 2146 | /* The number of parameters here is the number in the |
76a83782 RH |
2147 | main template, which, as checked in the assertion |
2148 | above, is NARGS. */ | |
6c30752f MM |
2149 | tpd2.parms = (int*) alloca (sizeof (int) * nargs); |
2150 | tpd2.level = | |
2151 | TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl)); | |
2152 | } | |
2153 | ||
104bf76a | 2154 | /* Mark the template parameters. But this time, we're |
6c30752f MM |
2155 | looking for the template parameters of the main |
2156 | template, not in the specialization. */ | |
2157 | tpd2.current_arg = i; | |
2158 | tpd2.arg_uses_template_parms[i] = 0; | |
1ddb2906 | 2159 | bzero ((PTR) tpd2.parms, sizeof (int) * nargs); |
6c30752f MM |
2160 | for_each_template_parm (type, |
2161 | &mark_template_parm, | |
2162 | &tpd2); | |
2163 | ||
2164 | if (tpd2.arg_uses_template_parms [i]) | |
2165 | { | |
2166 | /* The type depended on some template parameters. | |
2167 | If they are fully specialized in the | |
2168 | specialization, that's OK. */ | |
2169 | int j; | |
2170 | for (j = 0; j < nargs; ++j) | |
2171 | if (tpd2.parms[j] != 0 | |
2172 | && tpd.arg_uses_template_parms [j]) | |
2173 | { | |
104bf76a | 2174 | cp_error ("type `%T' of template argument `%E' depends on template parameter(s)", |
6c30752f MM |
2175 | type, |
2176 | arg); | |
2177 | break; | |
2178 | } | |
2179 | } | |
2180 | } | |
2181 | } | |
2182 | } | |
2183 | ||
2184 | if (retrieve_specialization (maintmpl, specargs)) | |
2185 | /* We've already got this specialization. */ | |
2186 | return decl; | |
2187 | ||
d8b64f80 | 2188 | DECL_TEMPLATE_SPECIALIZATIONS (maintmpl) |
e1b3e07d MM |
2189 | = tree_cons (inner_args, inner_parms, |
2190 | DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)); | |
6c30752f MM |
2191 | TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type; |
2192 | return decl; | |
2193 | } | |
2194 | ||
6ba89f8e MM |
2195 | /* Check that a template declaration's use of default arguments is not |
2196 | invalid. Here, PARMS are the template parameters. IS_PRIMARY is | |
2197 | non-zero if DECL is the thing declared by a primary template. | |
2198 | IS_PARTIAL is non-zero if DECL is a partial specialization. */ | |
2199 | ||
2200 | static void | |
2201 | check_default_tmpl_args (decl, parms, is_primary, is_partial) | |
2202 | tree decl; | |
2203 | tree parms; | |
2204 | int is_primary; | |
2205 | int is_partial; | |
2206 | { | |
d8e178a0 | 2207 | const char *msg; |
66191c20 MM |
2208 | int last_level_to_check; |
2209 | tree parm_level; | |
6ba89f8e MM |
2210 | |
2211 | /* [temp.param] | |
2212 | ||
2213 | A default template-argument shall not be specified in a | |
2214 | function template declaration or a function template definition, nor | |
2215 | in the template-parameter-list of the definition of a member of a | |
2216 | class template. */ | |
2217 | ||
4f1c5b7d | 2218 | if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL) |
e0942dcd MM |
2219 | /* You can't have a function template declaration in a local |
2220 | scope, nor you can you define a member of a class template in a | |
2221 | local scope. */ | |
2222 | return; | |
2223 | ||
6ba89f8e MM |
2224 | if (current_class_type |
2225 | && !TYPE_BEING_DEFINED (current_class_type) | |
3d7de1fa | 2226 | && DECL_LANG_SPECIFIC (decl) |
5937a6f9 MM |
2227 | /* If this is either a friend defined in the scope of the class |
2228 | or a member function. */ | |
4f1c5b7d MM |
2229 | && ((DECL_CONTEXT (decl) |
2230 | && same_type_p (DECL_CONTEXT (decl), current_class_type)) | |
2231 | || (DECL_FRIEND_CONTEXT (decl) | |
2232 | && same_type_p (DECL_FRIEND_CONTEXT (decl), | |
2233 | current_class_type))) | |
5937a6f9 MM |
2234 | /* And, if it was a member function, it really was defined in |
2235 | the scope of the class. */ | |
2236 | && (!DECL_FUNCTION_MEMBER_P (decl) || DECL_DEFINED_IN_CLASS_P (decl))) | |
6ba89f8e | 2237 | /* We already checked these parameters when the template was |
5937a6f9 MM |
2238 | declared, so there's no need to do it again now. This function |
2239 | was defined in class scope, but we're processing it's body now | |
2240 | that the class is complete. */ | |
6ba89f8e MM |
2241 | return; |
2242 | ||
66191c20 MM |
2243 | /* [temp.param] |
2244 | ||
2245 | If a template-parameter has a default template-argument, all | |
2246 | subsequent template-parameters shall have a default | |
2247 | template-argument supplied. */ | |
2248 | for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level)) | |
2249 | { | |
2250 | tree inner_parms = TREE_VALUE (parm_level); | |
2251 | int ntparms = TREE_VEC_LENGTH (inner_parms); | |
2252 | int seen_def_arg_p = 0; | |
2253 | int i; | |
2254 | ||
2255 | for (i = 0; i < ntparms; ++i) | |
2256 | { | |
2257 | tree parm = TREE_VEC_ELT (inner_parms, i); | |
2258 | if (TREE_PURPOSE (parm)) | |
2259 | seen_def_arg_p = 1; | |
2260 | else if (seen_def_arg_p) | |
2261 | { | |
2262 | cp_error ("no default argument for `%D'", TREE_VALUE (parm)); | |
2263 | /* For better subsequent error-recovery, we indicate that | |
2264 | there should have been a default argument. */ | |
2265 | TREE_PURPOSE (parm) = error_mark_node; | |
2266 | } | |
2267 | } | |
2268 | } | |
2269 | ||
6ba89f8e MM |
2270 | if (TREE_CODE (decl) != TYPE_DECL || is_partial || !is_primary) |
2271 | /* For an ordinary class template, default template arguments are | |
2272 | allowed at the innermost level, e.g.: | |
2273 | template <class T = int> | |
2274 | struct S {}; | |
2275 | but, in a partial specialization, they're not allowed even | |
2276 | there, as we have in [temp.class.spec]: | |
2277 | ||
2278 | The template parameter list of a specialization shall not | |
2279 | contain default template argument values. | |
2280 | ||
2281 | So, for a partial specialization, or for a function template, | |
2282 | we look at all of them. */ | |
2283 | ; | |
2284 | else | |
2285 | /* But, for a primary class template that is not a partial | |
2286 | specialization we look at all template parameters except the | |
2287 | innermost ones. */ | |
2288 | parms = TREE_CHAIN (parms); | |
2289 | ||
2290 | /* Figure out what error message to issue. */ | |
2291 | if (TREE_CODE (decl) == FUNCTION_DECL) | |
2292 | msg = "default argument for template parameter in function template `%D'"; | |
2293 | else if (is_partial) | |
2294 | msg = "default argument in partial specialization `%D'"; | |
2295 | else | |
2296 | msg = "default argument for template parameter for class enclosing `%D'"; | |
2297 | ||
2298 | if (current_class_type && TYPE_BEING_DEFINED (current_class_type)) | |
2299 | /* If we're inside a class definition, there's no need to | |
104bf76a | 2300 | examine the parameters to the class itself. On the one |
6ba89f8e MM |
2301 | hand, they will be checked when the class is defined, and, |
2302 | on the other, default arguments are legal in things like: | |
2303 | template <class T = double> | |
2304 | struct S { template <class U> void f(U); }; | |
2305 | Here the default argument for `S' has no bearing on the | |
2306 | declaration of `f'. */ | |
2307 | last_level_to_check = template_class_depth (current_class_type) + 1; | |
2308 | else | |
2309 | /* Check everything. */ | |
2310 | last_level_to_check = 0; | |
2311 | ||
66191c20 MM |
2312 | for (parm_level = parms; |
2313 | parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check; | |
2314 | parm_level = TREE_CHAIN (parm_level)) | |
6ba89f8e | 2315 | { |
66191c20 MM |
2316 | tree inner_parms = TREE_VALUE (parm_level); |
2317 | int i; | |
2318 | int ntparms; | |
6ba89f8e MM |
2319 | |
2320 | ntparms = TREE_VEC_LENGTH (inner_parms); | |
2321 | for (i = 0; i < ntparms; ++i) | |
2322 | if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i))) | |
2323 | { | |
2324 | if (msg) | |
2325 | { | |
2326 | cp_error (msg, decl); | |
2327 | msg = 0; | |
2328 | } | |
2329 | ||
2330 | /* Clear out the default argument so that we are not | |
2331 | confused later. */ | |
2332 | TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE; | |
2333 | } | |
2334 | ||
2335 | /* At this point, if we're still interested in issuing messages, | |
2336 | they must apply to classes surrounding the object declared. */ | |
2337 | if (msg) | |
2338 | msg = "default argument for template parameter for class enclosing `%D'"; | |
2339 | } | |
2340 | } | |
2341 | ||
3ac3d9ea | 2342 | /* Creates a TEMPLATE_DECL for the indicated DECL using the template |
f84b4be9 JM |
2343 | parameters given by current_template_args, or reuses a |
2344 | previously existing one, if appropriate. Returns the DECL, or an | |
6757edfe MM |
2345 | equivalent one, if it is replaced via a call to duplicate_decls. |
2346 | ||
2347 | If IS_FRIEND is non-zero, DECL is a friend declaration. */ | |
3ac3d9ea MM |
2348 | |
2349 | tree | |
6757edfe | 2350 | push_template_decl_real (decl, is_friend) |
9a3b49ac | 2351 | tree decl; |
6757edfe | 2352 | int is_friend; |
9a3b49ac MS |
2353 | { |
2354 | tree tmpl; | |
f84b4be9 | 2355 | tree args; |
9a3b49ac | 2356 | tree info; |
f84b4be9 JM |
2357 | tree ctx; |
2358 | int primary; | |
6ba89f8e | 2359 | int is_partial; |
cfe507be | 2360 | int new_template_p = 0; |
6ba89f8e MM |
2361 | |
2362 | /* See if this is a partial specialization. */ | |
9188c363 | 2363 | is_partial = (DECL_IMPLICIT_TYPEDEF_P (decl) |
6ba89f8e | 2364 | && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE |
370af2d5 | 2365 | && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl))); |
6757edfe MM |
2366 | |
2367 | is_friend |= (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl)); | |
f84b4be9 JM |
2368 | |
2369 | if (is_friend) | |
2370 | /* For a friend, we want the context of the friend function, not | |
2371 | the type of which it is a friend. */ | |
2372 | ctx = DECL_CONTEXT (decl); | |
4f1c5b7d MM |
2373 | else if (CP_DECL_CONTEXT (decl) |
2374 | && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL) | |
f84b4be9 JM |
2375 | /* In the case of a virtual function, we want the class in which |
2376 | it is defined. */ | |
4f1c5b7d | 2377 | ctx = CP_DECL_CONTEXT (decl); |
f84b4be9 JM |
2378 | else |
2379 | /* Otherwise, if we're currently definining some class, the DECL | |
2380 | is assumed to be a member of the class. */ | |
9188c363 | 2381 | ctx = current_scope (); |
f84b4be9 | 2382 | |
2c73f9f5 ML |
2383 | if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL) |
2384 | ctx = NULL_TREE; | |
2385 | ||
2386 | if (!DECL_CONTEXT (decl)) | |
cb0dbb9a | 2387 | DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace); |
2c73f9f5 | 2388 | |
6ba89f8e | 2389 | /* See if this is a primary template. */ |
74b846e0 | 2390 | primary = template_parm_scope_p (); |
9a3b49ac | 2391 | |
83566abf JM |
2392 | if (primary) |
2393 | { | |
2394 | if (current_lang_name == lang_name_c) | |
8251199e | 2395 | cp_error ("template with C linkage"); |
4ce3d537 MM |
2396 | else if (TREE_CODE (decl) == TYPE_DECL |
2397 | && ANON_AGGRNAME_P (DECL_NAME (decl))) | |
8251199e | 2398 | cp_error ("template class without a name"); |
2aaf816d JM |
2399 | else if ((DECL_IMPLICIT_TYPEDEF_P (decl) |
2400 | && CLASS_TYPE_P (TREE_TYPE (decl))) | |
2401 | || (TREE_CODE (decl) == VAR_DECL && ctx && CLASS_TYPE_P (ctx)) | |
2402 | || TREE_CODE (decl) == FUNCTION_DECL) | |
2403 | /* OK */; | |
2404 | else | |
4ce3d537 | 2405 | cp_error ("template declaration of `%#D'", decl); |
83566abf JM |
2406 | } |
2407 | ||
6ba89f8e MM |
2408 | /* Check to see that the rules regarding the use of default |
2409 | arguments are not being violated. */ | |
2410 | check_default_tmpl_args (decl, current_template_parms, | |
2411 | primary, is_partial); | |
73aad9b9 | 2412 | |
6ba89f8e MM |
2413 | if (is_partial) |
2414 | return process_partial_specialization (decl); | |
d32789d8 | 2415 | |
9a3b49ac MS |
2416 | args = current_template_args (); |
2417 | ||
f84b4be9 JM |
2418 | if (!ctx |
2419 | || TREE_CODE (ctx) == FUNCTION_DECL | |
2420 | || TYPE_BEING_DEFINED (ctx) | |
2421 | || (is_friend && !DECL_TEMPLATE_INFO (decl))) | |
8d08fdba | 2422 | { |
75650646 | 2423 | if (DECL_LANG_SPECIFIC (decl) |
f84b4be9 JM |
2424 | && DECL_TEMPLATE_INFO (decl) |
2425 | && DECL_TI_TEMPLATE (decl)) | |
2426 | tmpl = DECL_TI_TEMPLATE (decl); | |
1c10870d AS |
2427 | /* If DECL is a TYPE_DECL for a class-template, then there won't |
2428 | be DECL_LANG_SPECIFIC. The information equivalent to | |
2429 | DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */ | |
2430 | else if (DECL_IMPLICIT_TYPEDEF_P (decl) | |
2431 | && TYPE_TEMPLATE_INFO (TREE_TYPE (decl)) | |
2432 | && TYPE_TI_TEMPLATE (TREE_TYPE (decl))) | |
2433 | { | |
2434 | /* Since a template declaration already existed for this | |
2435 | class-type, we must be redeclaring it here. Make sure | |
2436 | that the redeclaration is legal. */ | |
2437 | redeclare_class_template (TREE_TYPE (decl), | |
2438 | current_template_parms); | |
2439 | /* We don't need to create a new TEMPLATE_DECL; just use the | |
2440 | one we already had. */ | |
2441 | tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl)); | |
2442 | } | |
f84b4be9 | 2443 | else |
786b5245 | 2444 | { |
f84b4be9 | 2445 | tmpl = build_template_decl (decl, current_template_parms); |
cfe507be MM |
2446 | new_template_p = 1; |
2447 | ||
f84b4be9 JM |
2448 | if (DECL_LANG_SPECIFIC (decl) |
2449 | && DECL_TEMPLATE_SPECIALIZATION (decl)) | |
2450 | { | |
2451 | /* A specialization of a member template of a template | |
2452 | class. */ | |
2453 | SET_DECL_TEMPLATE_SPECIALIZATION (tmpl); | |
2454 | DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl); | |
2455 | DECL_TEMPLATE_INFO (decl) = NULL_TREE; | |
2456 | } | |
786b5245 | 2457 | } |
8d08fdba MS |
2458 | } |
2459 | else | |
2460 | { | |
e1a5ccf7 | 2461 | tree a, t, current, parms; |
ba4f4e5d | 2462 | int i; |
6633d636 | 2463 | |
6b9ab5cc MM |
2464 | if (TREE_CODE (decl) == TYPE_DECL) |
2465 | { | |
ed44da02 MM |
2466 | if ((IS_AGGR_TYPE_CODE (TREE_CODE (TREE_TYPE (decl))) |
2467 | || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE) | |
2468 | && TYPE_TEMPLATE_INFO (TREE_TYPE (decl)) | |
2469 | && TYPE_TI_TEMPLATE (TREE_TYPE (decl))) | |
2470 | tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl)); | |
6b9ab5cc MM |
2471 | else |
2472 | { | |
8251199e | 2473 | cp_error ("`%D' does not declare a template type", decl); |
6b9ab5cc MM |
2474 | return decl; |
2475 | } | |
2476 | } | |
fc378698 | 2477 | else if (! DECL_TEMPLATE_INFO (decl)) |
c91a56d2 | 2478 | { |
8251199e | 2479 | cp_error ("template definition of non-template `%#D'", decl); |
3ac3d9ea | 2480 | return decl; |
c91a56d2 | 2481 | } |
8d08fdba | 2482 | else |
5566b478 | 2483 | tmpl = DECL_TI_TEMPLATE (decl); |
98c1c668 | 2484 | |
e1a5ccf7 JM |
2485 | if (is_member_template (tmpl) |
2486 | && DECL_FUNCTION_TEMPLATE_P (tmpl) | |
2487 | && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl) | |
2488 | && DECL_TEMPLATE_SPECIALIZATION (decl)) | |
98c1c668 | 2489 | { |
e1a5ccf7 JM |
2490 | tree new_tmpl; |
2491 | ||
2492 | /* The declaration is a specialization of a member | |
2493 | template, declared outside the class. Therefore, the | |
2494 | innermost template arguments will be NULL, so we | |
2495 | replace them with the arguments determined by the | |
2496 | earlier call to check_explicit_specialization. */ | |
2497 | args = DECL_TI_ARGS (decl); | |
2498 | ||
2499 | new_tmpl | |
2500 | = build_template_decl (decl, current_template_parms); | |
2501 | DECL_TEMPLATE_RESULT (new_tmpl) = decl; | |
2502 | TREE_TYPE (new_tmpl) = TREE_TYPE (decl); | |
2503 | DECL_TI_TEMPLATE (decl) = new_tmpl; | |
2504 | SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl); | |
e1b3e07d MM |
2505 | DECL_TEMPLATE_INFO (new_tmpl) |
2506 | = tree_cons (tmpl, args, NULL_TREE); | |
e1a5ccf7 JM |
2507 | |
2508 | register_specialization (new_tmpl, tmpl, args); | |
2509 | return decl; | |
98c1c668 | 2510 | } |
98c1c668 | 2511 | |
e1a5ccf7 | 2512 | /* Make sure the template headers we got make sense. */ |
6633d636 | 2513 | |
e1a5ccf7 JM |
2514 | parms = DECL_TEMPLATE_PARMS (tmpl); |
2515 | i = TMPL_PARMS_DEPTH (parms); | |
2516 | if (TMPL_ARGS_DEPTH (args) != i) | |
98c1c668 | 2517 | { |
e1a5ccf7 JM |
2518 | cp_error ("expected %d levels of template parms for `%#D', got %d", |
2519 | i, decl, TMPL_ARGS_DEPTH (args)); | |
98c1c668 | 2520 | } |
e1a5ccf7 JM |
2521 | else |
2522 | for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms)) | |
2523 | { | |
2524 | a = TMPL_ARGS_LEVEL (args, i); | |
2525 | t = INNERMOST_TEMPLATE_PARMS (parms); | |
2526 | ||
2527 | if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a)) | |
2528 | { | |
2529 | if (current == decl) | |
2530 | cp_error ("got %d template parameters for `%#D'", | |
2531 | TREE_VEC_LENGTH (a), decl); | |
2532 | else | |
2533 | cp_error ("got %d template parameters for `%#T'", | |
2534 | TREE_VEC_LENGTH (a), current); | |
2535 | cp_error (" but %d required", TREE_VEC_LENGTH (t)); | |
2536 | } | |
98c1c668 | 2537 | |
e1a5ccf7 JM |
2538 | /* Perhaps we should also check that the parms are used in the |
2539 | appropriate qualifying scopes in the declarator? */ | |
6633d636 | 2540 | |
e1a5ccf7 JM |
2541 | if (current == decl) |
2542 | current = ctx; | |
2543 | else | |
2544 | current = TYPE_CONTEXT (current); | |
2545 | } | |
5566b478 | 2546 | } |
8d08fdba | 2547 | |
5566b478 MS |
2548 | DECL_TEMPLATE_RESULT (tmpl) = decl; |
2549 | TREE_TYPE (tmpl) = TREE_TYPE (decl); | |
8d08fdba | 2550 | |
36a117a5 MM |
2551 | /* Push template declarations for global functions and types. Note |
2552 | that we do not try to push a global template friend declared in a | |
2553 | template class; such a thing may well depend on the template | |
39c01e4c | 2554 | parameters of the class. */ |
cfe507be | 2555 | if (new_template_p && !ctx |
36a117a5 | 2556 | && !(is_friend && template_class_depth (current_class_type) > 0)) |
2c73f9f5 | 2557 | tmpl = pushdecl_namespace_level (tmpl); |
8d08fdba | 2558 | |
5566b478 | 2559 | if (primary) |
6757edfe | 2560 | DECL_PRIMARY_TEMPLATE (tmpl) = tmpl; |
5566b478 | 2561 | |
e1b3e07d | 2562 | info = tree_cons (tmpl, args, NULL_TREE); |
5566b478 | 2563 | |
9188c363 | 2564 | if (DECL_IMPLICIT_TYPEDEF_P (decl)) |
8d08fdba | 2565 | { |
ed44da02 MM |
2566 | SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info); |
2567 | if ((!ctx || TREE_CODE (ctx) != FUNCTION_DECL) | |
2568 | && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE) | |
75650646 | 2569 | DECL_NAME (decl) = classtype_mangled_name (TREE_TYPE (decl)); |
51c184be | 2570 | } |
2aaf816d | 2571 | else if (DECL_LANG_SPECIFIC (decl)) |
5566b478 | 2572 | DECL_TEMPLATE_INFO (decl) = info; |
3ac3d9ea MM |
2573 | |
2574 | return DECL_TEMPLATE_RESULT (tmpl); | |
8d08fdba MS |
2575 | } |
2576 | ||
6757edfe MM |
2577 | tree |
2578 | push_template_decl (decl) | |
2579 | tree decl; | |
2580 | { | |
2581 | return push_template_decl_real (decl, 0); | |
2582 | } | |
2583 | ||
2584 | /* Called when a class template TYPE is redeclared with the indicated | |
2585 | template PARMS, e.g.: | |
7fe6899f MM |
2586 | |
2587 | template <class T> struct S; | |
2588 | template <class T> struct S {}; */ | |
2589 | ||
2590 | void | |
6757edfe | 2591 | redeclare_class_template (type, parms) |
7fe6899f | 2592 | tree type; |
6757edfe | 2593 | tree parms; |
7fe6899f | 2594 | { |
3d7de1fa | 2595 | tree tmpl; |
6757edfe | 2596 | tree tmpl_parms; |
7fe6899f MM |
2597 | int i; |
2598 | ||
3d7de1fa MM |
2599 | if (!TYPE_TEMPLATE_INFO (type)) |
2600 | { | |
2601 | cp_error ("`%T' is not a template type", type); | |
2602 | return; | |
2603 | } | |
2604 | ||
2605 | tmpl = TYPE_TI_TEMPLATE (type); | |
7fe6899f MM |
2606 | if (!PRIMARY_TEMPLATE_P (tmpl)) |
2607 | /* The type is nested in some template class. Nothing to worry | |
2608 | about here; there are no new template parameters for the nested | |
2609 | type. */ | |
2610 | return; | |
2611 | ||
6757edfe MM |
2612 | parms = INNERMOST_TEMPLATE_PARMS (parms); |
2613 | tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl); | |
2614 | ||
7fe6899f MM |
2615 | if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms)) |
2616 | { | |
8251199e JM |
2617 | cp_error_at ("previous declaration `%D'", tmpl); |
2618 | cp_error ("used %d template parameter%s instead of %d", | |
7fe6899f MM |
2619 | TREE_VEC_LENGTH (tmpl_parms), |
2620 | TREE_VEC_LENGTH (tmpl_parms) == 1 ? "" : "s", | |
2621 | TREE_VEC_LENGTH (parms)); | |
2622 | return; | |
2623 | } | |
2624 | ||
2625 | for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i) | |
2626 | { | |
2627 | tree tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i)); | |
2628 | tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i)); | |
2629 | tree tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)); | |
2630 | tree parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i)); | |
2631 | ||
2632 | if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)) | |
2633 | { | |
8251199e JM |
2634 | cp_error_at ("template parameter `%#D'", tmpl_parm); |
2635 | cp_error ("redeclared here as `%#D'", parm); | |
7fe6899f MM |
2636 | return; |
2637 | } | |
2638 | ||
2639 | if (tmpl_default != NULL_TREE && parm_default != NULL_TREE) | |
2640 | { | |
2641 | /* We have in [temp.param]: | |
2642 | ||
2643 | A template-parameter may not be given default arguments | |
2644 | by two different declarations in the same scope. */ | |
8251199e JM |
2645 | cp_error ("redefinition of default argument for `%#D'", parm); |
2646 | cp_error_at (" original definition appeared here", tmpl_parm); | |
7fe6899f MM |
2647 | return; |
2648 | } | |
2649 | ||
2650 | if (parm_default != NULL_TREE) | |
2651 | /* Update the previous template parameters (which are the ones | |
2652 | that will really count) with the new default value. */ | |
2653 | TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default; | |
7ce74c5e MM |
2654 | else if (tmpl_default != NULL_TREE) |
2655 | /* Update the new parameters, too; they'll be used as the | |
2656 | parameters for any members. */ | |
2657 | TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default; | |
7fe6899f MM |
2658 | } |
2659 | } | |
75650646 | 2660 | |
75650646 MM |
2661 | /* Attempt to convert the non-type template parameter EXPR to the |
2662 | indicated TYPE. If the conversion is successful, return the | |
2663 | converted value. If the conversion is unsuccesful, return | |
2664 | NULL_TREE if we issued an error message, or error_mark_node if we | |
2665 | did not. We issue error messages for out-and-out bad template | |
2666 | parameters, but not simply because the conversion failed, since we | |
2667 | might be just trying to do argument deduction. By the time this | |
2668 | function is called, neither TYPE nor EXPR may make use of template | |
2669 | parameters. */ | |
2670 | ||
2671 | static tree | |
e1467ff2 | 2672 | convert_nontype_argument (type, expr) |
75650646 MM |
2673 | tree type; |
2674 | tree expr; | |
2675 | { | |
2676 | tree expr_type = TREE_TYPE (expr); | |
2677 | ||
2678 | /* A template-argument for a non-type, non-template | |
2679 | template-parameter shall be one of: | |
2680 | ||
2681 | --an integral constant-expression of integral or enumeration | |
2682 | type; or | |
2683 | ||
2684 | --the name of a non-type template-parameter; or | |
2685 | ||
2686 | --the name of an object or function with external linkage, | |
2687 | including function templates and function template-ids but | |
86052cc3 | 2688 | excluding non-static class members, expressed as id-expression; |
75650646 MM |
2689 | or |
2690 | ||
2691 | --the address of an object or function with external linkage, | |
2692 | including function templates and function template-ids but | |
2693 | excluding non-static class members, expressed as & id-expression | |
2694 | where the & is optional if the name refers to a function or | |
2695 | array; or | |
2696 | ||
2697 | --a pointer to member expressed as described in _expr.unary.op_. */ | |
2698 | ||
638dd8fc MM |
2699 | /* An integral constant-expression can include const variables or |
2700 | enumerators. Simplify things by folding them to their values, | |
2701 | unless we're about to bind the declaration to a reference | |
2702 | parameter. */ | |
fc611ce0 | 2703 | if (INTEGRAL_TYPE_P (expr_type) |
638dd8fc | 2704 | && TREE_CODE (type) != REFERENCE_TYPE) |
86052cc3 JM |
2705 | expr = decl_constant_value (expr); |
2706 | ||
7bf2682f MM |
2707 | if (is_overloaded_fn (expr)) |
2708 | /* OK for now. We'll check that it has external linkage later. | |
2709 | Check this first since if expr_type is the unknown_type_node | |
2710 | we would otherwise complain below. */ | |
2711 | ; | |
e08a8f45 MM |
2712 | else if (TYPE_PTRMEM_P (expr_type) |
2713 | || TYPE_PTRMEMFUNC_P (expr_type)) | |
2714 | { | |
2715 | if (TREE_CODE (expr) != PTRMEM_CST) | |
2716 | goto bad_argument; | |
2717 | } | |
f3400fe2 | 2718 | else if (TYPE_PTR_P (expr_type) |
e08a8f45 | 2719 | || TYPE_PTRMEM_P (expr_type) |
f3400fe2 JM |
2720 | || TREE_CODE (expr_type) == ARRAY_TYPE |
2721 | || TREE_CODE (type) == REFERENCE_TYPE | |
75650646 MM |
2722 | /* If expr is the address of an overloaded function, we |
2723 | will get the unknown_type_node at this point. */ | |
2724 | || expr_type == unknown_type_node) | |
2725 | { | |
2726 | tree referent; | |
49bf0d6f MM |
2727 | tree e = expr; |
2728 | STRIP_NOPS (e); | |
75650646 | 2729 | |
f3400fe2 JM |
2730 | if (TREE_CODE (type) == REFERENCE_TYPE |
2731 | || TREE_CODE (expr_type) == ARRAY_TYPE) | |
2732 | referent = e; | |
2733 | else | |
75650646 | 2734 | { |
f3400fe2 JM |
2735 | if (TREE_CODE (e) != ADDR_EXPR) |
2736 | { | |
2737 | bad_argument: | |
2738 | cp_error ("`%E' is not a valid template argument", expr); | |
e08a8f45 MM |
2739 | if (TYPE_PTR_P (expr_type)) |
2740 | { | |
2741 | if (TREE_CODE (TREE_TYPE (expr_type)) == FUNCTION_TYPE) | |
2742 | cp_error ("it must be the address of a function with external linkage"); | |
2743 | else | |
2744 | cp_error ("it must be the address of an object with external linkage"); | |
2745 | } | |
2746 | else if (TYPE_PTRMEM_P (expr_type) | |
2747 | || TYPE_PTRMEMFUNC_P (expr_type)) | |
2748 | cp_error ("it must be a pointer-to-member of the form `&X::Y'"); | |
2749 | ||
f3400fe2 JM |
2750 | return NULL_TREE; |
2751 | } | |
2752 | ||
2753 | referent = TREE_OPERAND (e, 0); | |
2754 | STRIP_NOPS (referent); | |
75650646 MM |
2755 | } |
2756 | ||
75650646 MM |
2757 | if (TREE_CODE (referent) == STRING_CST) |
2758 | { | |
b4f4233d | 2759 | cp_error ("string literal %E is not a valid template argument because it is the address of an object with static linkage", |
75650646 | 2760 | referent); |
75650646 MM |
2761 | return NULL_TREE; |
2762 | } | |
2763 | ||
2764 | if (is_overloaded_fn (referent)) | |
2765 | /* We'll check that it has external linkage later. */ | |
2766 | ; | |
2767 | else if (TREE_CODE (referent) != VAR_DECL) | |
2768 | goto bad_argument; | |
2769 | else if (!TREE_PUBLIC (referent)) | |
2770 | { | |
8251199e | 2771 | cp_error ("address of non-extern `%E' cannot be used as template argument", referent); |
75650646 MM |
2772 | return error_mark_node; |
2773 | } | |
2774 | } | |
f3400fe2 JM |
2775 | else if (INTEGRAL_TYPE_P (expr_type) |
2776 | || TYPE_PTRMEM_P (expr_type) | |
2777 | || TYPE_PTRMEMFUNC_P (expr_type) | |
2778 | /* The next two are g++ extensions. */ | |
2779 | || TREE_CODE (expr_type) == REAL_TYPE | |
2780 | || TREE_CODE (expr_type) == COMPLEX_TYPE) | |
75650646 | 2781 | { |
f3400fe2 JM |
2782 | if (! TREE_CONSTANT (expr)) |
2783 | { | |
2784 | non_constant: | |
2785 | cp_error ("non-constant `%E' cannot be used as template argument", | |
2786 | expr); | |
2787 | return NULL_TREE; | |
2788 | } | |
75650646 | 2789 | } |
75650646 MM |
2790 | else |
2791 | { | |
8251199e | 2792 | cp_error ("object `%E' cannot be used as template argument", expr); |
75650646 MM |
2793 | return NULL_TREE; |
2794 | } | |
2795 | ||
2796 | switch (TREE_CODE (type)) | |
2797 | { | |
2798 | case INTEGER_TYPE: | |
2799 | case BOOLEAN_TYPE: | |
2800 | case ENUMERAL_TYPE: | |
2801 | /* For a non-type template-parameter of integral or enumeration | |
2802 | type, integral promotions (_conv.prom_) and integral | |
2803 | conversions (_conv.integral_) are applied. */ | |
2804 | if (!INTEGRAL_TYPE_P (expr_type)) | |
2805 | return error_mark_node; | |
2806 | ||
2807 | /* It's safe to call digest_init in this case; we know we're | |
2808 | just converting one integral constant expression to another. */ | |
db02b6b9 | 2809 | expr = digest_init (type, expr, (tree*) 0); |
75650646 | 2810 | |
db02b6b9 | 2811 | if (TREE_CODE (expr) != INTEGER_CST) |
f3400fe2 | 2812 | /* Curiously, some TREE_CONSTANT integral expressions do not |
db02b6b9 MM |
2813 | simplify to integer constants. For example, `3 % 0', |
2814 | remains a TRUNC_MOD_EXPR. */ | |
2815 | goto non_constant; | |
2816 | ||
2817 | return expr; | |
2818 | ||
abff8e06 JM |
2819 | case REAL_TYPE: |
2820 | case COMPLEX_TYPE: | |
2821 | /* These are g++ extensions. */ | |
2822 | if (TREE_CODE (expr_type) != TREE_CODE (type)) | |
2823 | return error_mark_node; | |
2824 | ||
db02b6b9 MM |
2825 | expr = digest_init (type, expr, (tree*) 0); |
2826 | ||
2827 | if (TREE_CODE (expr) != REAL_CST) | |
2828 | goto non_constant; | |
2829 | ||
2830 | return expr; | |
abff8e06 | 2831 | |
75650646 MM |
2832 | case POINTER_TYPE: |
2833 | { | |
2834 | tree type_pointed_to = TREE_TYPE (type); | |
2835 | ||
2836 | if (TYPE_PTRMEM_P (type)) | |
93aa462d MM |
2837 | { |
2838 | tree e; | |
2839 | ||
2840 | /* For a non-type template-parameter of type pointer to data | |
2841 | member, qualification conversions (_conv.qual_) are | |
2842 | applied. */ | |
2843 | e = perform_qualification_conversions (type, expr); | |
2844 | if (TREE_CODE (e) == NOP_EXPR) | |
87533b37 MM |
2845 | /* The call to perform_qualification_conversions will |
2846 | insert a NOP_EXPR over EXPR to do express conversion, | |
2847 | if necessary. But, that will confuse us if we use | |
2848 | this (converted) template parameter to instantiate | |
2849 | another template; then the thing will not look like a | |
2850 | valid template argument. So, just make a new | |
2851 | constant, of the appropriate type. */ | |
2852 | e = make_ptrmem_cst (type, PTRMEM_CST_MEMBER (expr)); | |
93aa462d MM |
2853 | return e; |
2854 | } | |
75650646 MM |
2855 | else if (TREE_CODE (type_pointed_to) == FUNCTION_TYPE) |
2856 | { | |
2857 | /* For a non-type template-parameter of type pointer to | |
2858 | function, only the function-to-pointer conversion | |
2859 | (_conv.func_) is applied. If the template-argument | |
2860 | represents a set of overloaded functions (or a pointer to | |
2861 | such), the matching function is selected from the set | |
2862 | (_over.over_). */ | |
2863 | tree fns; | |
2864 | tree fn; | |
2865 | ||
7bf2682f | 2866 | if (TREE_CODE (expr) == ADDR_EXPR) |
75650646 MM |
2867 | fns = TREE_OPERAND (expr, 0); |
2868 | else | |
2869 | fns = expr; | |
2870 | ||
a05273d4 | 2871 | fn = instantiate_type (type_pointed_to, fns, 0); |
75650646 MM |
2872 | |
2873 | if (fn == error_mark_node) | |
2874 | return error_mark_node; | |
2875 | ||
2876 | if (!TREE_PUBLIC (fn)) | |
2877 | { | |
2878 | if (really_overloaded_fn (fns)) | |
2879 | return error_mark_node; | |
2880 | else | |
2881 | goto bad_argument; | |
2882 | } | |
2883 | ||
2884 | expr = build_unary_op (ADDR_EXPR, fn, 0); | |
2885 | ||
3bfdc719 | 2886 | my_friendly_assert (same_type_p (type, TREE_TYPE (expr)), |
75650646 MM |
2887 | 0); |
2888 | return expr; | |
2889 | } | |
2890 | else | |
2891 | { | |
2892 | /* For a non-type template-parameter of type pointer to | |
2893 | object, qualification conversions (_conv.qual_) and the | |
2894 | array-to-pointer conversion (_conv.array_) are applied. | |
2895 | [Note: In particular, neither the null pointer conversion | |
2896 | (_conv.ptr_) nor the derived-to-base conversion | |
2897 | (_conv.ptr_) are applied. Although 0 is a valid | |
2898 | template-argument for a non-type template-parameter of | |
2899 | integral type, it is not a valid template-argument for a | |
e1467ff2 MM |
2900 | non-type template-parameter of pointer type.] |
2901 | ||
2902 | The call to decay_conversion performs the | |
2903 | array-to-pointer conversion, if appropriate. */ | |
2904 | expr = decay_conversion (expr); | |
75650646 MM |
2905 | |
2906 | if (expr == error_mark_node) | |
2907 | return error_mark_node; | |
2908 | else | |
2909 | return perform_qualification_conversions (type, expr); | |
2910 | } | |
2911 | } | |
2912 | break; | |
2913 | ||
2914 | case REFERENCE_TYPE: | |
2915 | { | |
2916 | tree type_referred_to = TREE_TYPE (type); | |
2917 | ||
2918 | if (TREE_CODE (type_referred_to) == FUNCTION_TYPE) | |
2919 | { | |
2920 | /* For a non-type template-parameter of type reference to | |
39c01e4c MM |
2921 | function, no conversions apply. If the |
2922 | template-argument represents a set of overloaded | |
2923 | functions, the matching function is selected from the | |
2924 | set (_over.over_). */ | |
75650646 MM |
2925 | tree fns = expr; |
2926 | tree fn; | |
2927 | ||
a05273d4 | 2928 | fn = instantiate_type (type_referred_to, fns, 0); |
e6e174e5 JM |
2929 | |
2930 | if (fn == error_mark_node) | |
2931 | return error_mark_node; | |
75650646 MM |
2932 | |
2933 | if (!TREE_PUBLIC (fn)) | |
2934 | { | |
2935 | if (really_overloaded_fn (fns)) | |
2936 | /* Don't issue an error here; we might get a different | |
2937 | function if the overloading had worked out | |
2938 | differently. */ | |
2939 | return error_mark_node; | |
2940 | else | |
2941 | goto bad_argument; | |
2942 | } | |
2943 | ||
3bfdc719 MM |
2944 | my_friendly_assert (same_type_p (type_referred_to, |
2945 | TREE_TYPE (fn)), | |
75650646 MM |
2946 | 0); |
2947 | ||
2948 | return fn; | |
2949 | } | |
2950 | else | |
2951 | { | |
2952 | /* For a non-type template-parameter of type reference to | |
2953 | object, no conversions apply. The type referred to by the | |
2954 | reference may be more cv-qualified than the (otherwise | |
2955 | identical) type of the template-argument. The | |
2956 | template-parameter is bound directly to the | |
2957 | template-argument, which must be an lvalue. */ | |
ff350acd JM |
2958 | if ((TYPE_MAIN_VARIANT (expr_type) |
2959 | != TYPE_MAIN_VARIANT (type_referred_to)) | |
91063b51 MM |
2960 | || !at_least_as_qualified_p (type_referred_to, |
2961 | expr_type) | |
75650646 MM |
2962 | || !real_lvalue_p (expr)) |
2963 | return error_mark_node; | |
2964 | else | |
2965 | return expr; | |
2966 | } | |
2967 | } | |
2968 | break; | |
2969 | ||
2970 | case RECORD_TYPE: | |
2971 | { | |
410c2fba KL |
2972 | if (!TYPE_PTRMEMFUNC_P (type)) |
2973 | /* This handles templates like | |
2974 | template<class T, T t> void f(); | |
2975 | when T is substituted with any class. The second template | |
2976 | parameter becomes invalid and the template candidate is | |
2977 | rejected. */ | |
2978 | return error_mark_node; | |
75650646 MM |
2979 | |
2980 | /* For a non-type template-parameter of type pointer to member | |
2981 | function, no conversions apply. If the template-argument | |
2982 | represents a set of overloaded member functions, the | |
2983 | matching member function is selected from the set | |
2984 | (_over.over_). */ | |
2985 | ||
2986 | if (!TYPE_PTRMEMFUNC_P (expr_type) && | |
2987 | expr_type != unknown_type_node) | |
2988 | return error_mark_node; | |
2989 | ||
e08a8f45 | 2990 | if (TREE_CODE (expr) == PTRMEM_CST) |
75650646 MM |
2991 | { |
2992 | /* A ptr-to-member constant. */ | |
3bfdc719 | 2993 | if (!same_type_p (type, expr_type)) |
75650646 MM |
2994 | return error_mark_node; |
2995 | else | |
2996 | return expr; | |
2997 | } | |
2998 | ||
2999 | if (TREE_CODE (expr) != ADDR_EXPR) | |
3000 | return error_mark_node; | |
3001 | ||
d8f8dca1 | 3002 | expr = instantiate_type (type, expr, 0); |
75650646 | 3003 | |
d8f8dca1 | 3004 | if (expr == error_mark_node) |
75650646 MM |
3005 | return error_mark_node; |
3006 | ||
3bfdc719 | 3007 | my_friendly_assert (same_type_p (type, TREE_TYPE (expr)), |
75650646 MM |
3008 | 0); |
3009 | return expr; | |
3010 | } | |
3011 | break; | |
3012 | ||
3013 | default: | |
3014 | /* All non-type parameters must have one of these types. */ | |
3015 | my_friendly_abort (0); | |
3016 | break; | |
3017 | } | |
3018 | ||
3019 | return error_mark_node; | |
3020 | } | |
3021 | ||
744fac59 KL |
3022 | /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for |
3023 | template template parameters. Both PARM_PARMS and ARG_PARMS are | |
3024 | vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL | |
3025 | or PARM_DECL. | |
3026 | ||
3027 | ARG_PARMS may contain more parameters than PARM_PARMS. If this is | |
3028 | the case, then extra parameters must have default arguments. | |
3029 | ||
3030 | Consider the example: | |
3031 | template <class T, class Allocator = allocator> class vector; | |
3032 | template<template <class U> class TT> class C; | |
3033 | ||
3034 | C<vector> is a valid instantiation. PARM_PARMS for the above code | |
3035 | contains a TYPE_DECL (for U), ARG_PARMS contains two TYPE_DECLs (for | |
3036 | T and Allocator) and OUTER_ARGS contains the argument that is used to | |
3037 | substitute the TT parameter. */ | |
3038 | ||
3039 | static int | |
4393e105 MM |
3040 | coerce_template_template_parms (parm_parms, arg_parms, complain, |
3041 | in_decl, outer_args) | |
3042 | tree parm_parms, arg_parms; | |
3043 | int complain; | |
3044 | tree in_decl, outer_args; | |
744fac59 KL |
3045 | { |
3046 | int nparms, nargs, i; | |
3047 | tree parm, arg; | |
3048 | ||
3049 | my_friendly_assert (TREE_CODE (parm_parms) == TREE_VEC, 0); | |
3050 | my_friendly_assert (TREE_CODE (arg_parms) == TREE_VEC, 0); | |
3051 | ||
3052 | nparms = TREE_VEC_LENGTH (parm_parms); | |
3053 | nargs = TREE_VEC_LENGTH (arg_parms); | |
3054 | ||
3055 | /* The rule here is opposite of coerce_template_parms. */ | |
3056 | if (nargs < nparms | |
3057 | || (nargs > nparms | |
3058 | && TREE_PURPOSE (TREE_VEC_ELT (arg_parms, nparms)) == NULL_TREE)) | |
3059 | return 0; | |
3060 | ||
3061 | for (i = 0; i < nparms; ++i) | |
3062 | { | |
3063 | parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i)); | |
3064 | arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i)); | |
3065 | ||
3066 | if (arg == NULL_TREE || arg == error_mark_node | |
3067 | || parm == NULL_TREE || parm == error_mark_node) | |
3068 | return 0; | |
3069 | ||
3070 | if (TREE_CODE (arg) != TREE_CODE (parm)) | |
3071 | return 0; | |
3072 | ||
3073 | switch (TREE_CODE (parm)) | |
3074 | { | |
3075 | case TYPE_DECL: | |
3076 | break; | |
3077 | ||
3078 | case TEMPLATE_DECL: | |
3079 | /* We encounter instantiations of templates like | |
3080 | template <template <template <class> class> class TT> | |
3081 | class C; */ | |
700466c2 JM |
3082 | { |
3083 | tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm); | |
3084 | tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg); | |
3085 | ||
3086 | if (!coerce_template_template_parms (parmparm, argparm, | |
4393e105 MM |
3087 | complain, in_decl, |
3088 | outer_args)) | |
700466c2 JM |
3089 | return 0; |
3090 | } | |
3091 | break; | |
744fac59 KL |
3092 | |
3093 | case PARM_DECL: | |
3094 | /* The tsubst call is used to handle cases such as | |
3095 | template <class T, template <T> class TT> class D; | |
3096 | i.e. the parameter list of TT depends on earlier parameters. */ | |
4393e105 MM |
3097 | if (!same_type_p (tsubst (TREE_TYPE (parm), outer_args, |
3098 | complain, in_decl), | |
3bfdc719 | 3099 | TREE_TYPE (arg))) |
744fac59 KL |
3100 | return 0; |
3101 | break; | |
3102 | ||
3103 | default: | |
3104 | my_friendly_abort (0); | |
3105 | } | |
3106 | } | |
3107 | return 1; | |
3108 | } | |
3109 | ||
8b5b8b7c MM |
3110 | /* Convert the indicated template ARG as necessary to match the |
3111 | indicated template PARM. Returns the converted ARG, or | |
3112 | error_mark_node if the conversion was unsuccessful. Error messages | |
3113 | are issued if COMPLAIN is non-zero. This conversion is for the Ith | |
3114 | parameter in the parameter list. ARGS is the full set of template | |
3115 | arguments deduced so far. */ | |
3116 | ||
3117 | static tree | |
3118 | convert_template_argument (parm, arg, args, complain, i, in_decl) | |
3119 | tree parm; | |
3120 | tree arg; | |
3121 | tree args; | |
3122 | int complain; | |
3123 | int i; | |
3124 | tree in_decl; | |
3125 | { | |
3126 | tree val; | |
3127 | tree inner_args; | |
3128 | int is_type, requires_type, is_tmpl_type, requires_tmpl_type; | |
3129 | ||
3130 | inner_args = innermost_args (args); | |
3131 | ||
3132 | if (TREE_CODE (arg) == TREE_LIST | |
3133 | && TREE_TYPE (arg) != NULL_TREE | |
3134 | && TREE_CODE (TREE_TYPE (arg)) == OFFSET_TYPE) | |
3135 | { | |
3136 | /* The template argument was the name of some | |
3137 | member function. That's usually | |
3138 | illegal, but static members are OK. In any | |
3139 | case, grab the underlying fields/functions | |
3140 | and issue an error later if required. */ | |
3141 | arg = TREE_VALUE (arg); | |
3142 | TREE_TYPE (arg) = unknown_type_node; | |
3143 | } | |
3144 | ||
3145 | requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL; | |
3146 | requires_type = (TREE_CODE (parm) == TYPE_DECL | |
3147 | || requires_tmpl_type); | |
3148 | ||
3149 | /* Check if it is a class template. If REQUIRES_TMPL_TYPE is true, | |
3150 | we also accept implicitly created TYPE_DECL as a valid argument. | |
3151 | This is necessary to handle the case where we pass a template name | |
3152 | to a template template parameter in a scope where we've derived from | |
3153 | in instantiation of that template, so the template name refers to that | |
3154 | instantiation. We really ought to handle this better. */ | |
3155 | is_tmpl_type | |
3156 | = ((TREE_CODE (arg) == TEMPLATE_DECL | |
3157 | && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL) | |
3158 | || (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM | |
7ddedda4 | 3159 | && !TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (arg)) |
8b5b8b7c MM |
3160 | || (TREE_CODE (arg) == RECORD_TYPE |
3161 | && CLASSTYPE_TEMPLATE_INFO (arg) | |
3162 | && TREE_CODE (TYPE_NAME (arg)) == TYPE_DECL | |
3163 | && DECL_ARTIFICIAL (TYPE_NAME (arg)) | |
3164 | && requires_tmpl_type | |
b54ccf71 | 3165 | && is_base_of_enclosing_class (arg, current_class_type))); |
8b5b8b7c MM |
3166 | if (is_tmpl_type && TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM) |
3167 | arg = TYPE_STUB_DECL (arg); | |
3168 | else if (is_tmpl_type && TREE_CODE (arg) == RECORD_TYPE) | |
3169 | arg = CLASSTYPE_TI_TEMPLATE (arg); | |
3170 | ||
2f939d94 | 3171 | is_type = TYPE_P (arg) || is_tmpl_type; |
8b5b8b7c MM |
3172 | |
3173 | if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF | |
3174 | && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM) | |
3175 | { | |
b4f4233d | 3176 | cp_pedwarn ("to refer to a type member of a template parameter, use `typename %E'", arg); |
8b5b8b7c MM |
3177 | |
3178 | arg = make_typename_type (TREE_OPERAND (arg, 0), | |
3baa501d MM |
3179 | TREE_OPERAND (arg, 1), |
3180 | complain); | |
8b5b8b7c MM |
3181 | is_type = 1; |
3182 | } | |
3183 | if (is_type != requires_type) | |
3184 | { | |
3185 | if (in_decl) | |
3186 | { | |
3187 | if (complain) | |
3188 | { | |
8251199e | 3189 | cp_error ("type/value mismatch at argument %d in template parameter list for `%D'", |
8b5b8b7c MM |
3190 | i + 1, in_decl); |
3191 | if (is_type) | |
8251199e | 3192 | cp_error (" expected a constant of type `%T', got `%T'", |
8b5b8b7c MM |
3193 | TREE_TYPE (parm), |
3194 | (is_tmpl_type ? DECL_NAME (arg) : arg)); | |
3195 | else | |
8251199e | 3196 | cp_error (" expected a type, got `%E'", arg); |
8b5b8b7c MM |
3197 | } |
3198 | } | |
3199 | return error_mark_node; | |
3200 | } | |
3201 | if (is_tmpl_type ^ requires_tmpl_type) | |
3202 | { | |
3203 | if (in_decl && complain) | |
3204 | { | |
8251199e | 3205 | cp_error ("type/value mismatch at argument %d in template parameter list for `%D'", |
8b5b8b7c MM |
3206 | i + 1, in_decl); |
3207 | if (is_tmpl_type) | |
8251199e | 3208 | cp_error (" expected a type, got `%T'", DECL_NAME (arg)); |
8b5b8b7c | 3209 | else |
8251199e | 3210 | cp_error (" expected a class template, got `%T'", arg); |
8b5b8b7c MM |
3211 | } |
3212 | return error_mark_node; | |
3213 | } | |
3214 | ||
3215 | if (is_type) | |
3216 | { | |
3217 | if (requires_tmpl_type) | |
3218 | { | |
3219 | tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm); | |
3220 | tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg); | |
3221 | ||
4393e105 | 3222 | if (coerce_template_template_parms (parmparm, argparm, complain, |
8b5b8b7c MM |
3223 | in_decl, inner_args)) |
3224 | { | |
3225 | val = arg; | |
3226 | ||
3227 | /* TEMPLATE_TEMPLATE_PARM node is preferred over | |
3228 | TEMPLATE_DECL. */ | |
3229 | if (val != error_mark_node | |
3230 | && DECL_TEMPLATE_TEMPLATE_PARM_P (val)) | |
3231 | val = TREE_TYPE (val); | |
3232 | } | |
3233 | else | |
3234 | { | |
3235 | if (in_decl && complain) | |
3236 | { | |
8251199e | 3237 | cp_error ("type/value mismatch at argument %d in template parameter list for `%D'", |
8b5b8b7c | 3238 | i + 1, in_decl); |
8251199e | 3239 | cp_error (" expected a template of type `%D', got `%D'", parm, arg); |
8b5b8b7c MM |
3240 | } |
3241 | ||
3242 | val = error_mark_node; | |
3243 | } | |
3244 | } | |
3245 | else | |
3246 | { | |
3247 | val = groktypename (arg); | |
3248 | if (! processing_template_decl) | |
3249 | { | |
3250 | /* [basic.link]: A name with no linkage (notably, the | |
3251 | name of a class or enumeration declared in a local | |
3252 | scope) shall not be used to declare an entity with | |
3253 | linkage. This implies that names with no linkage | |
3254 | cannot be used as template arguments. */ | |
3255 | tree t = no_linkage_check (val); | |
3256 | if (t) | |
3257 | { | |
3258 | if (ANON_AGGRNAME_P (TYPE_IDENTIFIER (t))) | |
8251199e JM |
3259 | cp_pedwarn |
3260 | ("template-argument `%T' uses anonymous type", val); | |
8b5b8b7c | 3261 | else |
8251199e JM |
3262 | cp_error |
3263 | ("template-argument `%T' uses local type `%T'", | |
8b5b8b7c MM |
3264 | val, t); |
3265 | return error_mark_node; | |
3266 | } | |
3267 | } | |
3268 | } | |
3269 | } | |
3270 | else | |
3271 | { | |
4393e105 | 3272 | tree t = tsubst (TREE_TYPE (parm), args, complain, in_decl); |
8b5b8b7c MM |
3273 | |
3274 | if (processing_template_decl) | |
3275 | arg = maybe_fold_nontype_arg (arg); | |
3276 | ||
3277 | if (!uses_template_parms (arg) && !uses_template_parms (t)) | |
3278 | /* We used to call digest_init here. However, digest_init | |
3279 | will report errors, which we don't want when complain | |
3280 | is zero. More importantly, digest_init will try too | |
3281 | hard to convert things: for example, `0' should not be | |
3282 | converted to pointer type at this point according to | |
3283 | the standard. Accepting this is not merely an | |
3284 | extension, since deciding whether or not these | |
3285 | conversions can occur is part of determining which | |
3286 | function template to call, or whether a given epxlicit | |
3287 | argument specification is legal. */ | |
3288 | val = convert_nontype_argument (t, arg); | |
3289 | else | |
3290 | val = arg; | |
3291 | ||
3292 | if (val == NULL_TREE) | |
3293 | val = error_mark_node; | |
3294 | else if (val == error_mark_node && complain) | |
8251199e | 3295 | cp_error ("could not convert template argument `%E' to `%T'", |
8b5b8b7c MM |
3296 | arg, t); |
3297 | } | |
3298 | ||
3299 | return val; | |
3300 | } | |
3301 | ||
3302 | /* Convert all template arguments to their appropriate types, and | |
3303 | return a vector containing the innermost resulting template | |
3304 | arguments. If any error occurs, return error_mark_node, and, if | |
3305 | COMPLAIN is non-zero, issue an error message. Some error messages | |
3306 | are issued even if COMPLAIN is zero; for instance, if a template | |
3307 | argument is composed from a local class. | |
75650646 MM |
3308 | |
3309 | If REQUIRE_ALL_ARGUMENTS is non-zero, all arguments must be | |
3310 | provided in ARGLIST, or else trailing parameters must have default | |
3311 | values. If REQUIRE_ALL_ARGUMENTS is zero, we will attempt argument | |
6dfbb909 | 3312 | deduction for any unspecified trailing arguments. */ |
de575009 | 3313 | |
8d08fdba | 3314 | static tree |
8b5b8b7c | 3315 | coerce_template_parms (parms, args, in_decl, |
75650646 | 3316 | complain, |
744fac59 | 3317 | require_all_arguments) |
8b5b8b7c | 3318 | tree parms, args; |
8d08fdba | 3319 | tree in_decl; |
75650646 MM |
3320 | int complain; |
3321 | int require_all_arguments; | |
8d08fdba | 3322 | { |
a292b002 | 3323 | int nparms, nargs, i, lost = 0; |
e4a84209 | 3324 | tree inner_args; |
8b5b8b7c MM |
3325 | tree new_args; |
3326 | tree new_inner_args; | |
a292b002 | 3327 | |
8b5b8b7c | 3328 | inner_args = innermost_args (args); |
e4a84209 | 3329 | nargs = NUM_TMPL_ARGS (inner_args); |
a292b002 MS |
3330 | nparms = TREE_VEC_LENGTH (parms); |
3331 | ||
3332 | if (nargs > nparms | |
3333 | || (nargs < nparms | |
75650646 | 3334 | && require_all_arguments |
a292b002 | 3335 | && TREE_PURPOSE (TREE_VEC_ELT (parms, nargs)) == NULL_TREE)) |
8d08fdba | 3336 | { |
75650646 MM |
3337 | if (complain) |
3338 | { | |
8251199e | 3339 | cp_error ("wrong number of template arguments (%d, should be %d)", |
e4a84209 | 3340 | nargs, nparms); |
75650646 MM |
3341 | |
3342 | if (in_decl) | |
8251199e | 3343 | cp_error_at ("provided for `%D'", in_decl); |
75650646 MM |
3344 | } |
3345 | ||
8d08fdba MS |
3346 | return error_mark_node; |
3347 | } | |
3348 | ||
f31c0a32 | 3349 | new_inner_args = make_tree_vec (nparms); |
8b5b8b7c MM |
3350 | new_args = add_outermost_template_args (args, new_inner_args); |
3351 | for (i = 0; i < nparms; i++) | |
8d08fdba | 3352 | { |
8b5b8b7c MM |
3353 | tree arg; |
3354 | tree parm; | |
e4a84209 | 3355 | |
8b5b8b7c MM |
3356 | /* Get the Ith template parameter. */ |
3357 | parm = TREE_VEC_ELT (parms, i); | |
75650646 | 3358 | |
8b5b8b7c MM |
3359 | /* Calculate the Ith argument. */ |
3360 | if (inner_args && TREE_CODE (inner_args) == TREE_LIST) | |
8d08fdba | 3361 | { |
8b5b8b7c MM |
3362 | arg = TREE_VALUE (inner_args); |
3363 | inner_args = TREE_CHAIN (inner_args); | |
8d08fdba | 3364 | } |
8b5b8b7c MM |
3365 | else if (i < nargs) |
3366 | arg = TREE_VEC_ELT (inner_args, i); | |
3367 | /* If no template argument was supplied, look for a default | |
3368 | value. */ | |
3369 | else if (TREE_PURPOSE (parm) == NULL_TREE) | |
3370 | { | |
3371 | /* There was no default value. */ | |
3372 | my_friendly_assert (!require_all_arguments, 0); | |
3373 | break; | |
3374 | } | |
3375 | else if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL) | |
4393e105 | 3376 | arg = tsubst (TREE_PURPOSE (parm), new_args, complain, in_decl); |
8b5b8b7c | 3377 | else |
4393e105 MM |
3378 | arg = tsubst_expr (TREE_PURPOSE (parm), new_args, complain, |
3379 | in_decl); | |
e4a84209 | 3380 | |
8b5b8b7c | 3381 | /* Now, convert the Ith argument, as necessary. */ |
75650646 MM |
3382 | if (arg == NULL_TREE) |
3383 | /* We're out of arguments. */ | |
3384 | { | |
3385 | my_friendly_assert (!require_all_arguments, 0); | |
3386 | break; | |
3387 | } | |
8b5b8b7c | 3388 | else if (arg == error_mark_node) |
75650646 | 3389 | { |
8251199e | 3390 | cp_error ("template argument %d is invalid", i + 1); |
8b5b8b7c | 3391 | arg = error_mark_node; |
8d08fdba | 3392 | } |
8b5b8b7c MM |
3393 | else |
3394 | arg = convert_template_argument (TREE_VALUE (parm), | |
3395 | arg, new_args, complain, i, | |
3396 | in_decl); | |
3397 | ||
3398 | if (arg == error_mark_node) | |
8d08fdba | 3399 | lost++; |
8b5b8b7c | 3400 | TREE_VEC_ELT (new_inner_args, i) = arg; |
8d08fdba | 3401 | } |
8b5b8b7c | 3402 | |
8d08fdba MS |
3403 | if (lost) |
3404 | return error_mark_node; | |
8b5b8b7c MM |
3405 | |
3406 | return new_inner_args; | |
8d08fdba MS |
3407 | } |
3408 | ||
34016c81 JM |
3409 | /* Returns 1 if template args OT and NT are equivalent. */ |
3410 | ||
d8e178a0 | 3411 | static int |
34016c81 JM |
3412 | template_args_equal (ot, nt) |
3413 | tree ot, nt; | |
3414 | { | |
3415 | if (nt == ot) | |
3416 | return 1; | |
3417 | if (TREE_CODE (nt) != TREE_CODE (ot)) | |
3418 | return 0; | |
3419 | if (TREE_CODE (nt) == TREE_VEC) | |
3420 | /* For member templates */ | |
3421 | return comp_template_args (ot, nt); | |
2f939d94 | 3422 | else if (TYPE_P (ot)) |
3bfdc719 | 3423 | return same_type_p (ot, nt); |
34016c81 JM |
3424 | else |
3425 | return (cp_tree_equal (ot, nt) > 0); | |
3426 | } | |
3427 | ||
3428 | /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets | |
f84b4be9 JM |
3429 | of template arguments. Returns 0 otherwise. */ |
3430 | ||
6757edfe | 3431 | int |
5566b478 MS |
3432 | comp_template_args (oldargs, newargs) |
3433 | tree oldargs, newargs; | |
3434 | { | |
3435 | int i; | |
3436 | ||
386b8a85 JM |
3437 | if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs)) |
3438 | return 0; | |
3439 | ||
5566b478 MS |
3440 | for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i) |
3441 | { | |
3442 | tree nt = TREE_VEC_ELT (newargs, i); | |
3443 | tree ot = TREE_VEC_ELT (oldargs, i); | |
3444 | ||
34016c81 | 3445 | if (! template_args_equal (ot, nt)) |
61a127b3 | 3446 | return 0; |
5566b478 MS |
3447 | } |
3448 | return 1; | |
3449 | } | |
3450 | ||
8d08fdba MS |
3451 | /* Given class template name and parameter list, produce a user-friendly name |
3452 | for the instantiation. */ | |
e92cc029 | 3453 | |
8d08fdba | 3454 | static char * |
36a117a5 | 3455 | mangle_class_name_for_template (name, parms, arglist) |
8d08fdba MS |
3456 | char *name; |
3457 | tree parms, arglist; | |
3458 | { | |
3459 | static struct obstack scratch_obstack; | |
3460 | static char *scratch_firstobj; | |
3461 | int i, nparms; | |
8d08fdba MS |
3462 | |
3463 | if (!scratch_firstobj) | |
fc378698 | 3464 | gcc_obstack_init (&scratch_obstack); |
8d08fdba MS |
3465 | else |
3466 | obstack_free (&scratch_obstack, scratch_firstobj); | |
fc378698 | 3467 | scratch_firstobj = obstack_alloc (&scratch_obstack, 1); |
8d08fdba | 3468 | |
8d08fdba | 3469 | #define ccat(c) obstack_1grow (&scratch_obstack, (c)); |
8d08fdba | 3470 | #define cat(s) obstack_grow (&scratch_obstack, (s), strlen (s)) |
8d08fdba MS |
3471 | |
3472 | cat (name); | |
3473 | ccat ('<'); | |
3474 | nparms = TREE_VEC_LENGTH (parms); | |
36a117a5 | 3475 | arglist = innermost_args (arglist); |
8d08fdba MS |
3476 | my_friendly_assert (nparms == TREE_VEC_LENGTH (arglist), 268); |
3477 | for (i = 0; i < nparms; i++) | |
3478 | { | |
a292b002 MS |
3479 | tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i)); |
3480 | tree arg = TREE_VEC_ELT (arglist, i); | |
8d08fdba MS |
3481 | |
3482 | if (i) | |
3483 | ccat (','); | |
3484 | ||
a292b002 | 3485 | if (TREE_CODE (parm) == TYPE_DECL) |
8d08fdba | 3486 | { |
9e93bc9d | 3487 | cat (type_as_string (arg, TS_CHASE_TYPEDEFS)); |
8d08fdba MS |
3488 | continue; |
3489 | } | |
73b0fce8 KL |
3490 | else if (TREE_CODE (parm) == TEMPLATE_DECL) |
3491 | { | |
3492 | if (TREE_CODE (arg) == TEMPLATE_DECL) | |
2c73f9f5 ML |
3493 | { |
3494 | /* Already substituted with real template. Just output | |
3495 | the template name here */ | |
cb0dbb9a | 3496 | tree context = DECL_CONTEXT (arg); |
ec4f972f AS |
3497 | if (context) |
3498 | { | |
3499 | /* The template may be defined in a namespace, or | |
3500 | may be a member template. */ | |
3501 | my_friendly_assert (TREE_CODE (context) == NAMESPACE_DECL | |
3502 | || CLASS_TYPE_P (context), | |
3503 | 980422); | |
2c73f9f5 ML |
3504 | cat(decl_as_string (DECL_CONTEXT (arg), 0)); |
3505 | cat("::"); | |
3506 | } | |
3507 | cat (IDENTIFIER_POINTER (DECL_NAME (arg))); | |
3508 | } | |
73b0fce8 KL |
3509 | else |
3510 | /* Output the parameter declaration */ | |
9e93bc9d | 3511 | cat (type_as_string (arg, TS_CHASE_TYPEDEFS)); |
73b0fce8 KL |
3512 | continue; |
3513 | } | |
8d08fdba MS |
3514 | else |
3515 | my_friendly_assert (TREE_CODE (parm) == PARM_DECL, 269); | |
3516 | ||
3517 | if (TREE_CODE (arg) == TREE_LIST) | |
3518 | { | |
3519 | /* New list cell was built because old chain link was in | |
3520 | use. */ | |
3521 | my_friendly_assert (TREE_PURPOSE (arg) == NULL_TREE, 270); | |
3522 | arg = TREE_VALUE (arg); | |
3523 | } | |
3524 | /* No need to check arglist against parmlist here; we did that | |
3525 | in coerce_template_parms, called from lookup_template_class. */ | |
3526 | cat (expr_as_string (arg, 0)); | |
3527 | } | |
3528 | { | |
3529 | char *bufp = obstack_next_free (&scratch_obstack); | |
3530 | int offset = 0; | |
3531 | while (bufp[offset - 1] == ' ') | |
3532 | offset--; | |
3533 | obstack_blank_fast (&scratch_obstack, offset); | |
3534 | ||
3535 | /* B<C<char> >, not B<C<char>> */ | |
3536 | if (bufp[offset - 1] == '>') | |
3537 | ccat (' '); | |
3538 | } | |
3539 | ccat ('>'); | |
3540 | ccat ('\0'); | |
3541 | return (char *) obstack_base (&scratch_obstack); | |
8d08fdba MS |
3542 | } |
3543 | ||
bd6dd845 | 3544 | static tree |
5566b478 MS |
3545 | classtype_mangled_name (t) |
3546 | tree t; | |
3547 | { | |
3548 | if (CLASSTYPE_TEMPLATE_INFO (t) | |
36a117a5 MM |
3549 | /* Specializations have already had their names set up in |
3550 | lookup_template_class. */ | |
370af2d5 | 3551 | && !CLASSTYPE_TEMPLATE_SPECIALIZATION (t)) |
9fbf56f7 MM |
3552 | { |
3553 | tree tmpl = most_general_template (CLASSTYPE_TI_TEMPLATE (t)); | |
3554 | ||
36a117a5 MM |
3555 | /* For non-primary templates, the template parameters are |
3556 | implicit from their surrounding context. */ | |
9fbf56f7 MM |
3557 | if (PRIMARY_TEMPLATE_P (tmpl)) |
3558 | { | |
3559 | tree name = DECL_NAME (tmpl); | |
3560 | char *mangled_name = mangle_class_name_for_template | |
3561 | (IDENTIFIER_POINTER (name), | |
3562 | DECL_INNERMOST_TEMPLATE_PARMS (tmpl), | |
3563 | CLASSTYPE_TI_ARGS (t)); | |
3564 | tree id = get_identifier (mangled_name); | |
3565 | IDENTIFIER_TEMPLATE (id) = name; | |
3566 | return id; | |
3567 | } | |
5566b478 | 3568 | } |
9fbf56f7 MM |
3569 | |
3570 | return TYPE_IDENTIFIER (t); | |
5566b478 MS |
3571 | } |
3572 | ||
3573 | static void | |
3574 | add_pending_template (d) | |
3575 | tree d; | |
3576 | { | |
2f939d94 | 3577 | tree ti = (TYPE_P (d)) ? CLASSTYPE_TEMPLATE_INFO (d) : DECL_TEMPLATE_INFO (d); |
e92cc029 | 3578 | |
824b9a4c | 3579 | if (TI_PENDING_TEMPLATE_FLAG (ti)) |
5566b478 MS |
3580 | return; |
3581 | ||
e1b3e07d | 3582 | *template_tail = tree_cons (build_srcloc_here (), d, NULL_TREE); |
5566b478 | 3583 | template_tail = &TREE_CHAIN (*template_tail); |
824b9a4c | 3584 | TI_PENDING_TEMPLATE_FLAG (ti) = 1; |
5566b478 MS |
3585 | } |
3586 | ||
386b8a85 JM |
3587 | |
3588 | /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS (which | |
3589 | may be either a _DECL or an overloaded function or an | |
3590 | IDENTIFIER_NODE), and ARGLIST. */ | |
3591 | ||
3592 | tree | |
3593 | lookup_template_function (fns, arglist) | |
3594 | tree fns, arglist; | |
3595 | { | |
2c73f9f5 | 3596 | tree type; |
050367a3 | 3597 | |
386b8a85 JM |
3598 | if (fns == NULL_TREE) |
3599 | { | |
8251199e | 3600 | cp_error ("non-template used as template"); |
386b8a85 JM |
3601 | return error_mark_node; |
3602 | } | |
3603 | ||
2c73f9f5 ML |
3604 | type = TREE_TYPE (fns); |
3605 | if (TREE_CODE (fns) == OVERLOAD || !type) | |
3606 | type = unknown_type_node; | |
3607 | ||
672476cb MM |
3608 | if (processing_template_decl) |
3609 | return build_min (TEMPLATE_ID_EXPR, type, fns, arglist); | |
3610 | else | |
3611 | return build (TEMPLATE_ID_EXPR, type, fns, arglist); | |
386b8a85 JM |
3612 | } |
3613 | ||
a2b60a0e MM |
3614 | /* Within the scope of a template class S<T>, the name S gets bound |
3615 | (in build_self_reference) to a TYPE_DECL for the class, not a | |
3616 | TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type, | |
3617 | or one of its enclosing classes, and that type is a template, | |
3618 | return the associated TEMPLATE_DECL. Otherwise, the original | |
3619 | DECL is returned. */ | |
3620 | ||
e9659ab0 | 3621 | static tree |
a2b60a0e MM |
3622 | maybe_get_template_decl_from_type_decl (decl) |
3623 | tree decl; | |
3624 | { | |
3625 | return (decl != NULL_TREE | |
3626 | && TREE_CODE (decl) == TYPE_DECL | |
3627 | && DECL_ARTIFICIAL (decl) | |
511b60ff | 3628 | && CLASS_TYPE_P (TREE_TYPE (decl)) |
a2b60a0e MM |
3629 | && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl))) |
3630 | ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl; | |
3631 | } | |
386b8a85 | 3632 | |
8d08fdba MS |
3633 | /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of |
3634 | parameters, find the desired type. | |
3635 | ||
3636 | D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments. | |
36a117a5 MM |
3637 | (Actually ARGLIST may be either a TREE_LIST or a TREE_VEC. It will |
3638 | be a TREE_LIST if called directly from the parser, and a TREE_VEC | |
0a2c2fd1 | 3639 | otherwise.) Since ARGLIST is build on the temp_decl_obstack, we must |
36a117a5 MM |
3640 | copy it here to keep it from being reclaimed when the decl storage |
3641 | is reclaimed. | |
8d08fdba MS |
3642 | |
3643 | IN_DECL, if non-NULL, is the template declaration we are trying to | |
75650646 MM |
3644 | instantiate. |
3645 | ||
36a117a5 MM |
3646 | If ENTERING_SCOPE is non-zero, we are about to enter the scope of |
3647 | the class we are looking up. | |
3648 | ||
75650646 MM |
3649 | If the template class is really a local class in a template |
3650 | function, then the FUNCTION_CONTEXT is the function in which it is | |
3651 | being instantiated. */ | |
e92cc029 | 3652 | |
8d08fdba | 3653 | tree |
36a117a5 | 3654 | lookup_template_class (d1, arglist, in_decl, context, entering_scope) |
8d08fdba MS |
3655 | tree d1, arglist; |
3656 | tree in_decl; | |
e1467ff2 | 3657 | tree context; |
36a117a5 | 3658 | int entering_scope; |
8d08fdba | 3659 | { |
a703fb38 | 3660 | tree template = NULL_TREE, parmlist; |
dbfe2124 | 3661 | tree t; |
5566b478 MS |
3662 | |
3663 | if (TREE_CODE (d1) == IDENTIFIER_NODE) | |
3664 | { | |
f181d4ae MM |
3665 | if (IDENTIFIER_VALUE (d1) |
3666 | && DECL_TEMPLATE_TEMPLATE_PARM_P (IDENTIFIER_VALUE (d1))) | |
3667 | template = IDENTIFIER_VALUE (d1); | |
73b0fce8 KL |
3668 | else |
3669 | { | |
2c73f9f5 ML |
3670 | if (context) |
3671 | push_decl_namespace (context); | |
3ebc5c52 MM |
3672 | template = lookup_name (d1, /*prefer_type=*/0); |
3673 | template = maybe_get_template_decl_from_type_decl (template); | |
2c73f9f5 ML |
3674 | if (context) |
3675 | pop_decl_namespace (); | |
73b0fce8 | 3676 | } |
8d019cef JM |
3677 | if (template) |
3678 | context = DECL_CONTEXT (template); | |
5566b478 | 3679 | } |
c91a56d2 MS |
3680 | else if (TREE_CODE (d1) == TYPE_DECL && IS_AGGR_TYPE (TREE_TYPE (d1))) |
3681 | { | |
802dbc34 JM |
3682 | tree type = TREE_TYPE (d1); |
3683 | ||
3684 | /* If we are declaring a constructor, say A<T>::A<T>, we will get | |
3685 | an implicit typename for the second A. Deal with it. */ | |
3686 | if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type)) | |
3687 | type = TREE_TYPE (type); | |
3688 | ||
3689 | if (CLASSTYPE_TEMPLATE_INFO (type)) | |
f3400fe2 | 3690 | { |
802dbc34 | 3691 | template = CLASSTYPE_TI_TEMPLATE (type); |
f3400fe2 JM |
3692 | d1 = DECL_NAME (template); |
3693 | } | |
c91a56d2 | 3694 | } |
ed44da02 | 3695 | else if (TREE_CODE (d1) == ENUMERAL_TYPE |
2f939d94 | 3696 | || (TYPE_P (d1) && IS_AGGR_TYPE (d1))) |
5566b478 | 3697 | { |
ed44da02 | 3698 | template = TYPE_TI_TEMPLATE (d1); |
5566b478 MS |
3699 | d1 = DECL_NAME (template); |
3700 | } | |
93cdc044 | 3701 | else if (TREE_CODE (d1) == TEMPLATE_DECL |
17aec3eb | 3702 | && TREE_CODE (DECL_TEMPLATE_RESULT (d1)) == TYPE_DECL) |
93cdc044 JM |
3703 | { |
3704 | template = d1; | |
3705 | d1 = DECL_NAME (template); | |
3706 | context = DECL_CONTEXT (template); | |
3707 | } | |
5566b478 MS |
3708 | else |
3709 | my_friendly_abort (272); | |
8d08fdba | 3710 | |
8d08fdba | 3711 | /* With something like `template <class T> class X class X { ... };' |
f181d4ae MM |
3712 | we could end up with D1 having nothing but an IDENTIFIER_VALUE. |
3713 | We don't want to do that, but we have to deal with the situation, | |
3714 | so let's give them some syntax errors to chew on instead of a | |
3715 | crash. */ | |
8d08fdba | 3716 | if (! template) |
f3400fe2 JM |
3717 | { |
3718 | cp_error ("`%T' is not a template", d1); | |
3719 | return error_mark_node; | |
3720 | } | |
2c73f9f5 | 3721 | |
c3baf4b5 JM |
3722 | if (context == NULL_TREE) |
3723 | context = global_namespace; | |
2c73f9f5 | 3724 | |
8d08fdba MS |
3725 | if (TREE_CODE (template) != TEMPLATE_DECL) |
3726 | { | |
8251199e | 3727 | cp_error ("non-template type `%T' used as a template", d1); |
8d08fdba | 3728 | if (in_decl) |
8251199e | 3729 | cp_error_at ("for template declaration `%D'", in_decl); |
8d08fdba MS |
3730 | return error_mark_node; |
3731 | } | |
8d08fdba | 3732 | |
73b0fce8 KL |
3733 | if (DECL_TEMPLATE_TEMPLATE_PARM_P (template)) |
3734 | { | |
3735 | /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store | |
3736 | template arguments */ | |
3737 | ||
3738 | tree parm = copy_template_template_parm (TREE_TYPE (template)); | |
3739 | tree template2 = TYPE_STUB_DECL (parm); | |
3740 | tree arglist2; | |
3741 | ||
73b0fce8 KL |
3742 | parmlist = DECL_INNERMOST_TEMPLATE_PARMS (template); |
3743 | ||
744fac59 | 3744 | arglist2 = coerce_template_parms (parmlist, arglist, template, 1, 1); |
73b0fce8 KL |
3745 | if (arglist2 == error_mark_node) |
3746 | return error_mark_node; | |
3747 | ||
7ddedda4 | 3748 | TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (parm) |
e1b3e07d | 3749 | = tree_cons (template2, arglist2, NULL_TREE); |
73b0fce8 KL |
3750 | TYPE_SIZE (parm) = 0; |
3751 | return parm; | |
3752 | } | |
36a117a5 | 3753 | else |
8d08fdba | 3754 | { |
36a117a5 | 3755 | tree template_type = TREE_TYPE (template); |
7ac7b28f | 3756 | tree gen_tmpl; |
36a117a5 MM |
3757 | tree type_decl; |
3758 | tree found = NULL_TREE; | |
3759 | int arg_depth; | |
3760 | int parm_depth; | |
dbfe2124 | 3761 | int is_partial_instantiation; |
830bfa74 | 3762 | |
7ac7b28f MM |
3763 | gen_tmpl = most_general_template (template); |
3764 | parmlist = DECL_TEMPLATE_PARMS (gen_tmpl); | |
36a117a5 MM |
3765 | parm_depth = TMPL_PARMS_DEPTH (parmlist); |
3766 | arg_depth = TMPL_ARGS_DEPTH (arglist); | |
3767 | ||
3768 | if (arg_depth == 1 && parm_depth > 1) | |
3769 | { | |
39c01e4c | 3770 | /* We've been given an incomplete set of template arguments. |
36a117a5 MM |
3771 | For example, given: |
3772 | ||
3773 | template <class T> struct S1 { | |
3774 | template <class U> struct S2 {}; | |
3775 | template <class U> struct S2<U*> {}; | |
3776 | }; | |
3777 | ||
3778 | we will be called with an ARGLIST of `U*', but the | |
3779 | TEMPLATE will be `template <class T> template | |
3780 | <class U> struct S1<T>::S2'. We must fill in the missing | |
3781 | arguments. */ | |
7ac7b28f MM |
3782 | arglist |
3783 | = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (template)), | |
3784 | arglist); | |
36a117a5 MM |
3785 | arg_depth = TMPL_ARGS_DEPTH (arglist); |
3786 | } | |
5566b478 | 3787 | |
7ac7b28f | 3788 | /* Now we should enough arguments. */ |
36a117a5 MM |
3789 | my_friendly_assert (parm_depth == arg_depth, 0); |
3790 | ||
7ac7b28f MM |
3791 | /* From here on, we're only interested in the most general |
3792 | template. */ | |
3793 | template = gen_tmpl; | |
3794 | ||
36a117a5 MM |
3795 | /* Calculate the BOUND_ARGS. These will be the args that are |
3796 | actually tsubst'd into the definition to create the | |
3797 | instantiation. */ | |
3798 | if (parm_depth > 1) | |
830bfa74 MM |
3799 | { |
3800 | /* We have multiple levels of arguments to coerce, at once. */ | |
830bfa74 | 3801 | int i; |
e4a84209 | 3802 | int saved_depth = TMPL_ARGS_DEPTH (arglist); |
36a117a5 | 3803 | |
f31c0a32 | 3804 | tree bound_args = make_tree_vec (parm_depth); |
830bfa74 | 3805 | |
e4a84209 | 3806 | for (i = saved_depth, |
830bfa74 | 3807 | t = DECL_TEMPLATE_PARMS (template); |
e4a84209 | 3808 | i > 0 && t != NULL_TREE; |
830bfa74 | 3809 | --i, t = TREE_CHAIN (t)) |
e4a84209 MM |
3810 | { |
3811 | tree a = coerce_template_parms (TREE_VALUE (t), | |
3812 | arglist, template, 1, 1); | |
3813 | SET_TMPL_ARGS_LEVEL (bound_args, i, a); | |
3814 | ||
3815 | /* We temporarily reduce the length of the ARGLIST so | |
3816 | that coerce_template_parms will see only the arguments | |
3817 | corresponding to the template parameters it is | |
3818 | examining. */ | |
3819 | TREE_VEC_LENGTH (arglist)--; | |
3820 | } | |
3821 | ||
3822 | /* Restore the ARGLIST to its full size. */ | |
3823 | TREE_VEC_LENGTH (arglist) = saved_depth; | |
3824 | ||
36a117a5 | 3825 | arglist = bound_args; |
830bfa74 MM |
3826 | } |
3827 | else | |
36a117a5 MM |
3828 | arglist |
3829 | = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist), | |
3830 | innermost_args (arglist), | |
3831 | template, 1, 1); | |
3832 | ||
3833 | if (arglist == error_mark_node) | |
3834 | /* We were unable to bind the arguments. */ | |
5566b478 | 3835 | return error_mark_node; |
5566b478 | 3836 | |
36a117a5 MM |
3837 | /* In the scope of a template class, explicit references to the |
3838 | template class refer to the type of the template, not any | |
3839 | instantiation of it. For example, in: | |
3840 | ||
3841 | template <class T> class C { void f(C<T>); } | |
3842 | ||
3843 | the `C<T>' is just the same as `C'. Outside of the | |
3844 | class, however, such a reference is an instantiation. */ | |
ed44da02 | 3845 | if (comp_template_args (TYPE_TI_ARGS (template_type), |
36a117a5 MM |
3846 | arglist)) |
3847 | { | |
3848 | found = template_type; | |
3849 | ||
3850 | if (!entering_scope && PRIMARY_TEMPLATE_P (template)) | |
5566b478 | 3851 | { |
36a117a5 MM |
3852 | tree ctx; |
3853 | ||
3854 | /* Note that we use DECL_CONTEXT, rather than | |
3855 | CP_DECL_CONTEXT, so that the termination test is | |
3856 | always just `ctx'. We're not interested in namepace | |
3857 | scopes. */ | |
3858 | for (ctx = current_class_type; | |
3859 | ctx; | |
2f939d94 | 3860 | ctx = (TYPE_P (ctx)) ? TYPE_CONTEXT (ctx) : DECL_CONTEXT (ctx)) |
3bfdc719 | 3861 | if (same_type_p (ctx, template_type)) |
36a117a5 MM |
3862 | break; |
3863 | ||
3864 | if (!ctx) | |
3865 | /* We're not in the scope of the class, so the | |
3866 | TEMPLATE_TYPE is not the type we want after | |
3867 | all. */ | |
3868 | found = NULL_TREE; | |
5566b478 MS |
3869 | } |
3870 | } | |
36a117a5 MM |
3871 | |
3872 | if (!found) | |
75650646 | 3873 | { |
36a117a5 MM |
3874 | for (found = DECL_TEMPLATE_INSTANTIATIONS (template); |
3875 | found; found = TREE_CHAIN (found)) | |
3876 | if (comp_template_args (TREE_PURPOSE (found), arglist)) | |
3877 | break; | |
75650646 | 3878 | |
36a117a5 MM |
3879 | if (found) |
3880 | found = TREE_VALUE (found); | |
8d08fdba | 3881 | } |
3ebc5c52 | 3882 | |
36a117a5 | 3883 | if (found) |
f31c0a32 | 3884 | return found; |
5566b478 | 3885 | |
dbfe2124 | 3886 | /* This type is a "partial instantiation" if any of the template |
486e4077 MM |
3887 | arguments still inolve template parameters. Note that we set |
3888 | IS_PARTIAL_INSTANTIATION for partial specializations as | |
3889 | well. */ | |
dbfe2124 MM |
3890 | is_partial_instantiation = uses_template_parms (arglist); |
3891 | ||
3ebc5c52 MM |
3892 | if (!is_partial_instantiation |
3893 | && !PRIMARY_TEMPLATE_P (template) | |
3894 | && TREE_CODE (CP_DECL_CONTEXT (template)) == NAMESPACE_DECL) | |
3895 | { | |
3ebc5c52 MM |
3896 | found = xref_tag_from_type (TREE_TYPE (template), |
3897 | DECL_NAME (template), | |
3898 | /*globalize=*/1); | |
3899 | return found; | |
3900 | } | |
3ebc5c52 | 3901 | |
36a117a5 | 3902 | /* Create the type. */ |
ed44da02 MM |
3903 | if (TREE_CODE (template_type) == ENUMERAL_TYPE) |
3904 | { | |
dbfe2124 | 3905 | if (!is_partial_instantiation) |
61a127b3 | 3906 | t = start_enum (TYPE_IDENTIFIER (template_type)); |
ed44da02 | 3907 | else |
dbfe2124 | 3908 | /* We don't want to call start_enum for this type, since |
ed44da02 MM |
3909 | the values for the enumeration constants may involve |
3910 | template parameters. And, no one should be interested | |
3911 | in the enumeration constants for such a type. */ | |
3912 | t = make_node (ENUMERAL_TYPE); | |
3913 | } | |
3914 | else | |
3915 | { | |
33848bb0 | 3916 | t = make_aggr_type (TREE_CODE (template_type)); |
ed44da02 MM |
3917 | CLASSTYPE_DECLARED_CLASS (t) |
3918 | = CLASSTYPE_DECLARED_CLASS (template_type); | |
3919 | CLASSTYPE_GOT_SEMICOLON (t) = 1; | |
3920 | SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t); | |
f668f160 | 3921 | TYPE_FOR_JAVA (t) = TYPE_FOR_JAVA (template_type); |
ae673f14 JM |
3922 | |
3923 | /* A local class. Make sure the decl gets registered properly. */ | |
3924 | if (context == current_function_decl) | |
3925 | pushtag (DECL_NAME (template), t, 0); | |
ed44da02 MM |
3926 | } |
3927 | ||
ae673f14 JM |
3928 | /* If we called start_enum or pushtag above, this information |
3929 | will already be set up. */ | |
ed44da02 MM |
3930 | if (!TYPE_NAME (t)) |
3931 | { | |
3932 | TYPE_CONTEXT (t) = FROB_CONTEXT (context); | |
36a117a5 | 3933 | |
9188c363 | 3934 | type_decl = create_implicit_typedef (DECL_NAME (template), t); |
ed44da02 | 3935 | DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t); |
9188c363 | 3936 | TYPE_STUB_DECL (t) = type_decl; |
ed44da02 MM |
3937 | DECL_SOURCE_FILE (type_decl) |
3938 | = DECL_SOURCE_FILE (TYPE_STUB_DECL (template_type)); | |
3939 | DECL_SOURCE_LINE (type_decl) | |
3940 | = DECL_SOURCE_LINE (TYPE_STUB_DECL (template_type)); | |
ed44da02 MM |
3941 | } |
3942 | else | |
3943 | type_decl = TYPE_NAME (t); | |
36a117a5 | 3944 | |
9fbf56f7 MM |
3945 | /* Set up the template information. We have to figure out which |
3946 | template is the immediate parent if this is a full | |
3947 | instantiation. */ | |
3948 | if (parm_depth == 1 || is_partial_instantiation | |
3949 | || !PRIMARY_TEMPLATE_P (template)) | |
3950 | /* This case is easy; there are no member templates involved. */ | |
3951 | found = template; | |
3952 | else | |
3953 | { | |
3954 | /* This is a full instantiation of a member template. There | |
3955 | should be some partial instantiation of which this is an | |
3956 | instance. */ | |
3957 | ||
3958 | for (found = DECL_TEMPLATE_INSTANTIATIONS (template); | |
3959 | found; found = TREE_CHAIN (found)) | |
3960 | { | |
3961 | int success; | |
3962 | tree tmpl = CLASSTYPE_TI_TEMPLATE (TREE_VALUE (found)); | |
3963 | ||
3964 | /* We only want partial instantiations, here, not | |
3965 | specializations or full instantiations. */ | |
370af2d5 | 3966 | if (CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_VALUE (found)) |
9fbf56f7 MM |
3967 | || !uses_template_parms (TREE_VALUE (found))) |
3968 | continue; | |
3969 | ||
3970 | /* Temporarily reduce by one the number of levels in the | |
3971 | ARGLIST and in FOUND so as to avoid comparing the | |
3972 | last set of arguments. */ | |
3973 | TREE_VEC_LENGTH (arglist)--; | |
3974 | TREE_VEC_LENGTH (TREE_PURPOSE (found)) --; | |
3975 | ||
3976 | /* See if the arguments match. If they do, then TMPL is | |
3977 | the partial instantiation we want. */ | |
3978 | success = comp_template_args (TREE_PURPOSE (found), arglist); | |
3979 | ||
3980 | /* Restore the argument vectors to their full size. */ | |
3981 | TREE_VEC_LENGTH (arglist)++; | |
3982 | TREE_VEC_LENGTH (TREE_PURPOSE (found))++; | |
3983 | ||
3984 | if (success) | |
3985 | { | |
3986 | found = tmpl; | |
3987 | break; | |
3988 | } | |
3989 | } | |
3990 | ||
3991 | if (!found) | |
3992 | my_friendly_abort (0); | |
3993 | } | |
3994 | ||
ed44da02 | 3995 | SET_TYPE_TEMPLATE_INFO (t, |
9fbf56f7 | 3996 | tree_cons (found, arglist, NULL_TREE)); |
dbfe2124 MM |
3997 | DECL_TEMPLATE_INSTANTIATIONS (template) |
3998 | = tree_cons (arglist, t, | |
3999 | DECL_TEMPLATE_INSTANTIATIONS (template)); | |
4000 | ||
4001 | if (TREE_CODE (t) == ENUMERAL_TYPE | |
4002 | && !is_partial_instantiation) | |
61a127b3 MM |
4003 | /* Now that the type has been registered on the instantiations |
4004 | list, we set up the enumerators. Because the enumeration | |
4005 | constants may involve the enumeration type itself, we make | |
4006 | sure to register the type first, and then create the | |
4007 | constants. That way, doing tsubst_expr for the enumeration | |
4008 | constants won't result in recursive calls here; we'll find | |
4009 | the instantiation and exit above. */ | |
4010 | tsubst_enum (template_type, t, arglist); | |
dbfe2124 | 4011 | |
36a117a5 MM |
4012 | /* Reset the name of the type, now that CLASSTYPE_TEMPLATE_INFO |
4013 | is set up. */ | |
ed44da02 MM |
4014 | if (TREE_CODE (t) != ENUMERAL_TYPE) |
4015 | DECL_NAME (type_decl) = classtype_mangled_name (t); | |
36a117a5 | 4016 | DECL_ASSEMBLER_NAME (type_decl) = DECL_NAME (type_decl); |
dbfe2124 | 4017 | if (!is_partial_instantiation) |
36a117a5 MM |
4018 | { |
4019 | DECL_ASSEMBLER_NAME (type_decl) | |
4020 | = get_identifier (build_overload_name (t, 1, 1)); | |
50a6dbd7 JM |
4021 | |
4022 | /* For backwards compatibility; code that uses | |
4023 | -fexternal-templates expects looking up a template to | |
4024 | instantiate it. I think DDD still relies on this. | |
4025 | (jason 8/20/1998) */ | |
ed44da02 MM |
4026 | if (TREE_CODE (t) != ENUMERAL_TYPE |
4027 | && flag_external_templates | |
36a117a5 MM |
4028 | && CLASSTYPE_INTERFACE_KNOWN (TREE_TYPE (template)) |
4029 | && ! CLASSTYPE_INTERFACE_ONLY (TREE_TYPE (template))) | |
4030 | add_pending_template (t); | |
4031 | } | |
4032 | else | |
077e7015 MM |
4033 | /* If the type makes use of template parameters, the |
4034 | code that generates debugging information will crash. */ | |
4035 | DECL_IGNORED_P (TYPE_STUB_DECL (t)) = 1; | |
8d08fdba | 4036 | |
36a117a5 MM |
4037 | return t; |
4038 | } | |
8d08fdba MS |
4039 | } |
4040 | \f | |
8dfaeb63 | 4041 | struct pair_fn_data |
8d08fdba | 4042 | { |
8dfaeb63 MM |
4043 | tree_fn_t fn; |
4044 | void *data; | |
4045 | }; | |
4046 | ||
4047 | /* Called from for_each_template_parm via walk_tree. */ | |
581d38d0 | 4048 | |
8dfaeb63 MM |
4049 | static tree |
4050 | for_each_template_parm_r (tp, walk_subtrees, d) | |
4051 | tree *tp; | |
4052 | int *walk_subtrees; | |
4053 | void *d; | |
4054 | { | |
4055 | tree t = *tp; | |
4056 | struct pair_fn_data *pfd = (struct pair_fn_data *) d; | |
4057 | tree_fn_t fn = pfd->fn; | |
4058 | void *data = pfd->data; | |
4059 | ||
2f939d94 | 4060 | if (TYPE_P (t) |
581d38d0 | 4061 | && for_each_template_parm (TYPE_CONTEXT (t), fn, data)) |
8dfaeb63 | 4062 | return error_mark_node; |
581d38d0 | 4063 | |
8d08fdba MS |
4064 | switch (TREE_CODE (t)) |
4065 | { | |
8d08fdba | 4066 | case RECORD_TYPE: |
b7484fbe | 4067 | if (TYPE_PTRMEMFUNC_FLAG (t)) |
8dfaeb63 | 4068 | break; |
ed44da02 MM |
4069 | /* Fall through. */ |
4070 | ||
8d08fdba | 4071 | case UNION_TYPE: |
ed44da02 | 4072 | case ENUMERAL_TYPE: |
8dfaeb63 MM |
4073 | if (!TYPE_TEMPLATE_INFO (t)) |
4074 | *walk_subtrees = 0; | |
4075 | else if (for_each_template_parm (TREE_VALUE (TYPE_TEMPLATE_INFO (t)), | |
4076 | fn, data)) | |
4077 | return error_mark_node; | |
4078 | break; | |
4079 | ||
588c2d1c | 4080 | case METHOD_TYPE: |
8dfaeb63 MM |
4081 | /* Since we're not going to walk subtrees, we have to do this |
4082 | explicitly here. */ | |
588c2d1c | 4083 | if (for_each_template_parm (TYPE_METHOD_BASETYPE (t), fn, data)) |
8dfaeb63 | 4084 | return error_mark_node; |
588c2d1c MM |
4085 | |
4086 | case FUNCTION_TYPE: | |
8dfaeb63 MM |
4087 | /* Check the return type. */ |
4088 | if (for_each_template_parm (TREE_TYPE (t), fn, data)) | |
4089 | return error_mark_node; | |
4090 | ||
588c2d1c MM |
4091 | /* Check the parameter types. Since default arguments are not |
4092 | instantiated until they are needed, the TYPE_ARG_TYPES may | |
4093 | contain expressions that involve template parameters. But, | |
4094 | no-one should be looking at them yet. And, once they're | |
4095 | instantiated, they don't contain template parameters, so | |
4096 | there's no point in looking at them then, either. */ | |
4097 | { | |
4098 | tree parm; | |
4099 | ||
4100 | for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm)) | |
4101 | if (for_each_template_parm (TREE_VALUE (parm), fn, data)) | |
8dfaeb63 | 4102 | return error_mark_node; |
5566b478 | 4103 | |
8dfaeb63 MM |
4104 | /* Since we've already handled the TYPE_ARG_TYPES, we don't |
4105 | want walk_tree walking into them itself. */ | |
4106 | *walk_subtrees = 0; | |
4107 | } | |
4108 | break; | |
3ac3d9ea | 4109 | |
8d08fdba | 4110 | case FUNCTION_DECL: |
5566b478 | 4111 | case VAR_DECL: |
5566b478 | 4112 | if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t) |
050367a3 | 4113 | && for_each_template_parm (DECL_TI_ARGS (t), fn, data)) |
8dfaeb63 MM |
4114 | return error_mark_node; |
4115 | /* Fall through. */ | |
4116 | ||
4117 | case CONST_DECL: | |
8d08fdba | 4118 | case PARM_DECL: |
050367a3 MM |
4119 | if (DECL_CONTEXT (t) |
4120 | && for_each_template_parm (DECL_CONTEXT (t), fn, data)) | |
8dfaeb63 MM |
4121 | return error_mark_node; |
4122 | break; | |
8d08fdba | 4123 | |
73b0fce8 | 4124 | case TEMPLATE_TEMPLATE_PARM: |
744fac59 | 4125 | /* Record template parameters such as `T' inside `TT<T>'. */ |
7ddedda4 MM |
4126 | if (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t) |
4127 | && for_each_template_parm (TYPE_TI_ARGS (t), fn, data)) | |
8dfaeb63 MM |
4128 | return error_mark_node; |
4129 | /* Fall through. */ | |
4130 | ||
744fac59 | 4131 | case TEMPLATE_TYPE_PARM: |
f84b4be9 | 4132 | case TEMPLATE_PARM_INDEX: |
8dfaeb63 MM |
4133 | if (fn && (*fn)(t, data)) |
4134 | return error_mark_node; | |
4135 | else if (!fn) | |
4136 | return error_mark_node; | |
4137 | break; | |
5156628f | 4138 | |
8dfaeb63 MM |
4139 | case TEMPLATE_DECL: |
4140 | /* A template template parameter is encountered */ | |
4141 | if (DECL_TEMPLATE_TEMPLATE_PARM_P (t) | |
4142 | && for_each_template_parm (TREE_TYPE (t), fn, data)) | |
4143 | return error_mark_node; | |
db5ae43f | 4144 | |
8dfaeb63 MM |
4145 | /* Already substituted template template parameter */ |
4146 | *walk_subtrees = 0; | |
4147 | break; | |
b8865407 | 4148 | |
4699c561 | 4149 | case TYPENAME_TYPE: |
8dfaeb63 MM |
4150 | if (!fn || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t), fn, data)) |
4151 | return error_mark_node; | |
4152 | break; | |
4699c561 | 4153 | |
8dfaeb63 MM |
4154 | case CONSTRUCTOR: |
4155 | if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t)) | |
4156 | && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE | |
4157 | (TREE_TYPE (t)), fn, data)) | |
4158 | return error_mark_node; | |
4159 | break; | |
4160 | ||
b8865407 MM |
4161 | case INDIRECT_REF: |
4162 | case COMPONENT_REF: | |
4699c561 | 4163 | /* If there's no type, then this thing must be some expression |
b8865407 | 4164 | involving template parameters. */ |
4699c561 | 4165 | if (!fn && !TREE_TYPE (t)) |
8dfaeb63 MM |
4166 | return error_mark_node; |
4167 | break; | |
b8865407 | 4168 | |
42976354 BK |
4169 | case MODOP_EXPR: |
4170 | case CAST_EXPR: | |
4171 | case REINTERPRET_CAST_EXPR: | |
4172 | case CONST_CAST_EXPR: | |
4173 | case STATIC_CAST_EXPR: | |
4174 | case DYNAMIC_CAST_EXPR: | |
42976354 BK |
4175 | case ARROW_EXPR: |
4176 | case DOTSTAR_EXPR: | |
4177 | case TYPEID_EXPR: | |
b8865407 | 4178 | case LOOKUP_EXPR: |
40242ccf | 4179 | case PSEUDO_DTOR_EXPR: |
b8865407 | 4180 | if (!fn) |
8dfaeb63 MM |
4181 | return error_mark_node; |
4182 | break; | |
abff8e06 | 4183 | |
8d08fdba | 4184 | default: |
8dfaeb63 | 4185 | break; |
8d08fdba | 4186 | } |
8dfaeb63 MM |
4187 | |
4188 | /* We didn't find any template parameters we liked. */ | |
4189 | return NULL_TREE; | |
4190 | } | |
4191 | ||
4192 | /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM, or | |
4193 | TEMPLATE_PARM_INDEX in T, call FN with the parameter and the DATA. | |
4194 | If FN returns non-zero, the iteration is terminated, and | |
4195 | for_each_template_parm returns 1. Otherwise, the iteration | |
4196 | continues. If FN never returns a non-zero value, the value | |
4197 | returned by for_each_template_parm is 0. If FN is NULL, it is | |
4198 | considered to be the function which always returns 1. */ | |
4199 | ||
4200 | static int | |
4201 | for_each_template_parm (t, fn, data) | |
4202 | tree t; | |
4203 | tree_fn_t fn; | |
4204 | void* data; | |
4205 | { | |
4206 | struct pair_fn_data pfd; | |
4207 | ||
4208 | /* Set up. */ | |
4209 | pfd.fn = fn; | |
4210 | pfd.data = data; | |
4211 | ||
4212 | /* Walk the tree. */ | |
4213 | return walk_tree (&t, for_each_template_parm_r, &pfd) != NULL_TREE; | |
8d08fdba MS |
4214 | } |
4215 | ||
050367a3 MM |
4216 | int |
4217 | uses_template_parms (t) | |
4218 | tree t; | |
4219 | { | |
4220 | return for_each_template_parm (t, 0, 0); | |
4221 | } | |
4222 | ||
27fafc8d JM |
4223 | static struct tinst_level *current_tinst_level; |
4224 | static struct tinst_level *free_tinst_level; | |
4225 | static int tinst_depth; | |
e9f32eb5 | 4226 | extern int max_tinst_depth; |
5566b478 | 4227 | #ifdef GATHER_STATISTICS |
27fafc8d | 4228 | int depth_reached; |
5566b478 | 4229 | #endif |
8dfaeb63 MM |
4230 | static int tinst_level_tick; |
4231 | static int last_template_error_tick; | |
8d08fdba | 4232 | |
7bae46f4 | 4233 | /* Print out all the template instantiations that we are currently |
27fafc8d JM |
4234 | working on. If ERR, we are being called from cp_thing, so do |
4235 | the right thing for an error message. */ | |
7bae46f4 | 4236 | |
27fafc8d JM |
4237 | static void |
4238 | print_template_context (err) | |
4239 | int err; | |
7bae46f4 JM |
4240 | { |
4241 | struct tinst_level *p = current_tinst_level; | |
4242 | int line = lineno; | |
4243 | char *file = input_filename; | |
4244 | ||
49e04385 | 4245 | if (err && p) |
27fafc8d | 4246 | { |
49e04385 MM |
4247 | if (current_function_decl != p->decl |
4248 | && current_function_decl != NULL_TREE) | |
4249 | /* We can get here during the processing of some synthesized | |
4250 | method. Then, p->decl will be the function that's causing | |
4251 | the synthesis. */ | |
4252 | ; | |
27fafc8d | 4253 | else |
27fafc8d | 4254 | { |
49e04385 MM |
4255 | if (current_function_decl == p->decl) |
4256 | /* Avoid redundancy with the the "In function" line. */; | |
4257 | else | |
4258 | fprintf (stderr, "%s: In instantiation of `%s':\n", | |
9e93bc9d | 4259 | file, decl_as_string (p->decl, TS_DECL_TYPE | TS_FUNC_NORETURN)); |
49e04385 | 4260 | |
038fb86f JM |
4261 | line = p->line; |
4262 | file = p->file; | |
27fafc8d JM |
4263 | p = p->next; |
4264 | } | |
4265 | } | |
4266 | ||
7bae46f4 JM |
4267 | for (; p; p = p->next) |
4268 | { | |
038fb86f | 4269 | fprintf (stderr, "%s:%d: instantiated from `%s'\n", file, line, |
9e93bc9d | 4270 | decl_as_string (p->decl, TS_DECL_TYPE | TS_FUNC_NORETURN)); |
038fb86f JM |
4271 | line = p->line; |
4272 | file = p->file; | |
7bae46f4 | 4273 | } |
038fb86f | 4274 | fprintf (stderr, "%s:%d: instantiated from here\n", file, line); |
7bae46f4 JM |
4275 | } |
4276 | ||
27fafc8d JM |
4277 | /* Called from cp_thing to print the template context for an error. */ |
4278 | ||
4279 | void | |
4280 | maybe_print_template_context () | |
4281 | { | |
4282 | if (last_template_error_tick == tinst_level_tick | |
4283 | || current_tinst_level == 0) | |
4284 | return; | |
4285 | ||
4286 | last_template_error_tick = tinst_level_tick; | |
4287 | print_template_context (1); | |
4288 | } | |
4289 | ||
bd6dd845 | 4290 | static int |
5566b478 MS |
4291 | push_tinst_level (d) |
4292 | tree d; | |
8d08fdba MS |
4293 | { |
4294 | struct tinst_level *new; | |
8d08fdba | 4295 | |
7215f9a0 MS |
4296 | if (tinst_depth >= max_tinst_depth) |
4297 | { | |
8adf5b5e JM |
4298 | /* If the instantiation in question still has unbound template parms, |
4299 | we don't really care if we can't instantiate it, so just return. | |
4300 | This happens with base instantiation for implicit `typename'. */ | |
4301 | if (uses_template_parms (d)) | |
4302 | return 0; | |
4303 | ||
1139b3d8 | 4304 | last_template_error_tick = tinst_level_tick; |
b4f4233d GDR |
4305 | cp_error ("template instantiation depth exceeds maximum of %d (use -ftemplate-depth-NN to increase the maximum) instantiating `%D'", |
4306 | max_tinst_depth, d); | |
5566b478 | 4307 | |
27fafc8d | 4308 | print_template_context (0); |
5566b478 | 4309 | |
7215f9a0 MS |
4310 | return 0; |
4311 | } | |
4312 | ||
8d08fdba MS |
4313 | if (free_tinst_level) |
4314 | { | |
4315 | new = free_tinst_level; | |
4316 | free_tinst_level = new->next; | |
4317 | } | |
4318 | else | |
4319 | new = (struct tinst_level *) xmalloc (sizeof (struct tinst_level)); | |
4320 | ||
5566b478 MS |
4321 | new->decl = d; |
4322 | new->line = lineno; | |
4323 | new->file = input_filename; | |
8d08fdba MS |
4324 | new->next = current_tinst_level; |
4325 | current_tinst_level = new; | |
5566b478 | 4326 | |
7215f9a0 | 4327 | ++tinst_depth; |
5566b478 MS |
4328 | #ifdef GATHER_STATISTICS |
4329 | if (tinst_depth > depth_reached) | |
4330 | depth_reached = tinst_depth; | |
4331 | #endif | |
4332 | ||
27fafc8d | 4333 | ++tinst_level_tick; |
7215f9a0 | 4334 | return 1; |
8d08fdba MS |
4335 | } |
4336 | ||
4337 | void | |
4338 | pop_tinst_level () | |
4339 | { | |
4340 | struct tinst_level *old = current_tinst_level; | |
4341 | ||
ae58fa02 MM |
4342 | /* Restore the filename and line number stashed away when we started |
4343 | this instantiation. */ | |
4344 | lineno = old->line; | |
4345 | input_filename = old->file; | |
5f2c99c4 | 4346 | extract_interface_info (); |
ae58fa02 | 4347 | |
8d08fdba MS |
4348 | current_tinst_level = old->next; |
4349 | old->next = free_tinst_level; | |
4350 | free_tinst_level = old; | |
7215f9a0 | 4351 | --tinst_depth; |
27fafc8d | 4352 | ++tinst_level_tick; |
8d08fdba MS |
4353 | } |
4354 | ||
4355 | struct tinst_level * | |
4356 | tinst_for_decl () | |
4357 | { | |
4358 | struct tinst_level *p = current_tinst_level; | |
4359 | ||
4360 | if (p) | |
4361 | for (; p->next ; p = p->next ) | |
4362 | ; | |
4363 | return p; | |
4364 | } | |
4365 | ||
f84b4be9 JM |
4366 | /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the |
4367 | vector of template arguments, as for tsubst. | |
4368 | ||
4369 | Returns an appropriate tsbust'd friend declaration. */ | |
4370 | ||
4371 | static tree | |
4372 | tsubst_friend_function (decl, args) | |
4373 | tree decl; | |
4374 | tree args; | |
4375 | { | |
4376 | tree new_friend; | |
27fafc8d JM |
4377 | int line = lineno; |
4378 | char *file = input_filename; | |
4379 | ||
4380 | lineno = DECL_SOURCE_LINE (decl); | |
4381 | input_filename = DECL_SOURCE_FILE (decl); | |
4382 | ||
f84b4be9 JM |
4383 | if (TREE_CODE (decl) == FUNCTION_DECL |
4384 | && DECL_TEMPLATE_INSTANTIATION (decl) | |
4385 | && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL) | |
4386 | /* This was a friend declared with an explicit template | |
4387 | argument list, e.g.: | |
4388 | ||
4389 | friend void f<>(T); | |
4390 | ||
4391 | to indicate that f was a template instantiation, not a new | |
4392 | function declaration. Now, we have to figure out what | |
4393 | instantiation of what template. */ | |
4394 | { | |
4395 | tree template_id; | |
4396 | tree new_args; | |
4397 | tree tmpl; | |
f84b4be9 JM |
4398 | |
4399 | template_id | |
4400 | = lookup_template_function (tsubst_expr (DECL_TI_TEMPLATE (decl), | |
4393e105 MM |
4401 | args, /*complain=*/1, |
4402 | NULL_TREE), | |
f84b4be9 | 4403 | tsubst (DECL_TI_ARGS (decl), |
4393e105 MM |
4404 | args, /*complain=*/1, |
4405 | NULL_TREE)); | |
4393e105 | 4406 | new_friend = tsubst (decl, args, /*complain=*/1, NULL_TREE); |
36a117a5 MM |
4407 | tmpl = determine_specialization (template_id, new_friend, |
4408 | &new_args, | |
bf8f3f93 | 4409 | /*need_member_template=*/0); |
27fafc8d JM |
4410 | new_friend = instantiate_template (tmpl, new_args); |
4411 | goto done; | |
f84b4be9 | 4412 | } |
36a117a5 | 4413 | |
4393e105 | 4414 | new_friend = tsubst (decl, args, /*complain=*/1, NULL_TREE); |
f84b4be9 | 4415 | |
36a117a5 | 4416 | /* The NEW_FRIEND will look like an instantiation, to the |
f84b4be9 JM |
4417 | compiler, but is not an instantiation from the point of view of |
4418 | the language. For example, we might have had: | |
4419 | ||
4420 | template <class T> struct S { | |
4421 | template <class U> friend void f(T, U); | |
4422 | }; | |
4423 | ||
4424 | Then, in S<int>, template <class U> void f(int, U) is not an | |
4425 | instantiation of anything. */ | |
4426 | DECL_USE_TEMPLATE (new_friend) = 0; | |
4427 | if (TREE_CODE (decl) == TEMPLATE_DECL) | |
4428 | DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0; | |
36a117a5 MM |
4429 | |
4430 | /* The mangled name for the NEW_FRIEND is incorrect. The call to | |
4431 | tsubst will have resulted in a call to | |
4432 | set_mangled_name_for_template_decl. But, the function is not a | |
4433 | template instantiation and should not be mangled like one. | |
4434 | Therefore, we remangle the function name. We don't have to do | |
4435 | this if the NEW_FRIEND is a template since | |
4436 | set_mangled_name_for_template_decl doesn't do anything if the | |
4437 | function declaration still uses template arguments. */ | |
4438 | if (TREE_CODE (new_friend) != TEMPLATE_DECL) | |
4439 | { | |
4440 | set_mangled_name_for_decl (new_friend); | |
4441 | DECL_RTL (new_friend) = 0; | |
4442 | make_decl_rtl (new_friend, NULL_PTR, 1); | |
4443 | } | |
4444 | ||
6eb3bb27 | 4445 | if (DECL_NAMESPACE_SCOPE_P (new_friend)) |
f84b4be9 | 4446 | { |
36a117a5 | 4447 | tree old_decl; |
fbf1c34b MM |
4448 | tree new_friend_template_info; |
4449 | tree new_friend_result_template_info; | |
92da7074 | 4450 | tree ns; |
fbf1c34b MM |
4451 | int new_friend_is_defn; |
4452 | ||
4453 | /* We must save some information from NEW_FRIEND before calling | |
4454 | duplicate decls since that function will free NEW_FRIEND if | |
4455 | possible. */ | |
4456 | new_friend_template_info = DECL_TEMPLATE_INFO (new_friend); | |
f84b4be9 | 4457 | if (TREE_CODE (new_friend) == TEMPLATE_DECL) |
fbf1c34b MM |
4458 | { |
4459 | /* This declaration is a `primary' template. */ | |
4460 | DECL_PRIMARY_TEMPLATE (new_friend) = new_friend; | |
4461 | ||
4462 | new_friend_is_defn | |
17aec3eb | 4463 | = DECL_INITIAL (DECL_TEMPLATE_RESULT (new_friend)) != NULL_TREE; |
fbf1c34b | 4464 | new_friend_result_template_info |
17aec3eb | 4465 | = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend)); |
fbf1c34b MM |
4466 | } |
4467 | else | |
4468 | { | |
4469 | new_friend_is_defn = DECL_INITIAL (new_friend) != NULL_TREE; | |
4470 | new_friend_result_template_info = NULL_TREE; | |
4471 | } | |
36a117a5 | 4472 | |
1c227897 MM |
4473 | /* Inside pushdecl_namespace_level, we will push into the |
4474 | current namespace. However, the friend function should go | |
4475 | into the namespace of the template. */ | |
92da7074 ML |
4476 | ns = decl_namespace_context (new_friend); |
4477 | push_nested_namespace (ns); | |
36a117a5 | 4478 | old_decl = pushdecl_namespace_level (new_friend); |
92da7074 | 4479 | pop_nested_namespace (ns); |
36a117a5 MM |
4480 | |
4481 | if (old_decl != new_friend) | |
4482 | { | |
4483 | /* This new friend declaration matched an existing | |
4484 | declaration. For example, given: | |
4485 | ||
4486 | template <class T> void f(T); | |
4487 | template <class U> class C { | |
4488 | template <class T> friend void f(T) {} | |
4489 | }; | |
4490 | ||
4491 | the friend declaration actually provides the definition | |
4492 | of `f', once C has been instantiated for some type. So, | |
4493 | old_decl will be the out-of-class template declaration, | |
4494 | while new_friend is the in-class definition. | |
4495 | ||
4496 | But, if `f' was called before this point, the | |
4497 | instantiation of `f' will have DECL_TI_ARGS corresponding | |
4498 | to `T' but not to `U', references to which might appear | |
4499 | in the definition of `f'. Previously, the most general | |
4500 | template for an instantiation of `f' was the out-of-class | |
4501 | version; now it is the in-class version. Therefore, we | |
4502 | run through all specialization of `f', adding to their | |
4503 | DECL_TI_ARGS appropriately. In particular, they need a | |
4504 | new set of outer arguments, corresponding to the | |
4505 | arguments for this class instantiation. | |
4506 | ||
4507 | The same situation can arise with something like this: | |
4508 | ||
4509 | friend void f(int); | |
4510 | template <class T> class C { | |
4511 | friend void f(T) {} | |
4512 | }; | |
4513 | ||
4514 | when `C<int>' is instantiated. Now, `f(int)' is defined | |
4515 | in the class. */ | |
4516 | ||
fbf1c34b MM |
4517 | if (!new_friend_is_defn) |
4518 | /* On the other hand, if the in-class declaration does | |
4519 | *not* provide a definition, then we don't want to alter | |
4520 | existing definitions. We can just leave everything | |
4521 | alone. */ | |
36a117a5 | 4522 | ; |
fbf1c34b | 4523 | else |
36a117a5 | 4524 | { |
fbf1c34b MM |
4525 | /* Overwrite whatever template info was there before, if |
4526 | any, with the new template information pertaining to | |
4527 | the declaration. */ | |
4528 | DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info; | |
4529 | ||
4530 | if (TREE_CODE (old_decl) != TEMPLATE_DECL) | |
4531 | /* duplicate_decls will take care of this case. */ | |
4532 | ; | |
4533 | else | |
36a117a5 | 4534 | { |
fbf1c34b MM |
4535 | tree t; |
4536 | tree new_friend_args; | |
4537 | ||
17aec3eb | 4538 | DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl)) |
fbf1c34b MM |
4539 | = new_friend_result_template_info; |
4540 | ||
4541 | new_friend_args = TI_ARGS (new_friend_template_info); | |
4542 | for (t = DECL_TEMPLATE_SPECIALIZATIONS (old_decl); | |
4543 | t != NULL_TREE; | |
4544 | t = TREE_CHAIN (t)) | |
4545 | { | |
4546 | tree spec = TREE_VALUE (t); | |
36a117a5 | 4547 | |
fbf1c34b MM |
4548 | DECL_TI_ARGS (spec) |
4549 | = add_outermost_template_args (new_friend_args, | |
4550 | DECL_TI_ARGS (spec)); | |
fbf1c34b MM |
4551 | } |
4552 | ||
4553 | /* Now, since specializations are always supposed to | |
4554 | hang off of the most general template, we must move | |
4555 | them. */ | |
4556 | t = most_general_template (old_decl); | |
4557 | if (t != old_decl) | |
4558 | { | |
4559 | DECL_TEMPLATE_SPECIALIZATIONS (t) | |
4560 | = chainon (DECL_TEMPLATE_SPECIALIZATIONS (t), | |
4561 | DECL_TEMPLATE_SPECIALIZATIONS (old_decl)); | |
4562 | DECL_TEMPLATE_SPECIALIZATIONS (old_decl) = NULL_TREE; | |
4563 | } | |
36a117a5 MM |
4564 | } |
4565 | } | |
4566 | ||
4567 | /* The information from NEW_FRIEND has been merged into OLD_DECL | |
4568 | by duplicate_decls. */ | |
4569 | new_friend = old_decl; | |
4570 | } | |
f84b4be9 | 4571 | } |
d0f062fb | 4572 | else if (COMPLETE_TYPE_P (DECL_CONTEXT (new_friend))) |
f84b4be9 JM |
4573 | { |
4574 | /* Check to see that the declaration is really present, and, | |
4575 | possibly obtain an improved declaration. */ | |
4576 | tree fn = check_classfn (DECL_CONTEXT (new_friend), | |
4577 | new_friend); | |
4578 | ||
4579 | if (fn) | |
4580 | new_friend = fn; | |
4581 | } | |
4582 | ||
27fafc8d JM |
4583 | done: |
4584 | lineno = line; | |
4585 | input_filename = file; | |
f84b4be9 JM |
4586 | return new_friend; |
4587 | } | |
4588 | ||
1aed5355 MM |
4589 | /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of |
4590 | template arguments, as for tsubst. | |
6757edfe MM |
4591 | |
4592 | Returns an appropriate tsbust'd friend type. */ | |
4593 | ||
4594 | static tree | |
1aed5355 MM |
4595 | tsubst_friend_class (friend_tmpl, args) |
4596 | tree friend_tmpl; | |
6757edfe MM |
4597 | tree args; |
4598 | { | |
1aed5355 | 4599 | tree friend_type; |
25aab5d0 | 4600 | tree tmpl; |
6757edfe | 4601 | |
25aab5d0 MM |
4602 | /* First, we look for a class template. */ |
4603 | tmpl = lookup_name (DECL_NAME (friend_tmpl), /*prefer_type=*/0); | |
4604 | ||
4605 | /* But, if we don't find one, it might be because we're in a | |
4606 | situation like this: | |
4607 | ||
4608 | template <class T> | |
4609 | struct S { | |
4610 | template <class U> | |
4611 | friend struct S; | |
4612 | }; | |
4613 | ||
4614 | Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL | |
4615 | for `S<int>', not the TEMPLATE_DECL. */ | |
bc639f90 | 4616 | if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl)) |
25aab5d0 MM |
4617 | { |
4618 | tmpl = lookup_name (DECL_NAME (friend_tmpl), /*prefer_type=*/1); | |
4619 | tmpl = maybe_get_template_decl_from_type_decl (tmpl); | |
4620 | } | |
6757edfe | 4621 | |
25aab5d0 | 4622 | if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl)) |
6757edfe MM |
4623 | { |
4624 | /* The friend template has already been declared. Just | |
36a117a5 MM |
4625 | check to see that the declarations match, and install any new |
4626 | default parameters. We must tsubst the default parameters, | |
4627 | of course. We only need the innermost template parameters | |
4628 | because that is all that redeclare_class_template will look | |
4629 | at. */ | |
4630 | tree parms | |
4631 | = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl), | |
4393e105 | 4632 | args, /*complain=*/1); |
36a117a5 | 4633 | redeclare_class_template (TREE_TYPE (tmpl), parms); |
6757edfe MM |
4634 | friend_type = TREE_TYPE (tmpl); |
4635 | } | |
4636 | else | |
4637 | { | |
4638 | /* The friend template has not already been declared. In this | |
4639 | case, the instantiation of the template class will cause the | |
4640 | injection of this template into the global scope. */ | |
4393e105 | 4641 | tmpl = tsubst (friend_tmpl, args, /*complain=*/1, NULL_TREE); |
6757edfe MM |
4642 | |
4643 | /* The new TMPL is not an instantiation of anything, so we | |
4644 | forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for | |
4645 | the new type because that is supposed to be the corresponding | |
4646 | template decl, i.e., TMPL. */ | |
4647 | DECL_USE_TEMPLATE (tmpl) = 0; | |
4648 | DECL_TEMPLATE_INFO (tmpl) = NULL_TREE; | |
4649 | CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0; | |
4650 | ||
4651 | /* Inject this template into the global scope. */ | |
4652 | friend_type = TREE_TYPE (pushdecl_top_level (tmpl)); | |
4653 | } | |
4654 | ||
4655 | return friend_type; | |
4656 | } | |
f84b4be9 | 4657 | |
8d08fdba | 4658 | tree |
5566b478 MS |
4659 | instantiate_class_template (type) |
4660 | tree type; | |
8d08fdba | 4661 | { |
61a127b3 | 4662 | tree template, args, pattern, t; |
36a117a5 | 4663 | tree typedecl; |
8d08fdba | 4664 | |
5566b478 | 4665 | if (type == error_mark_node) |
8d08fdba MS |
4666 | return error_mark_node; |
4667 | ||
d0f062fb | 4668 | if (TYPE_BEING_DEFINED (type) || COMPLETE_TYPE_P (type)) |
5566b478 MS |
4669 | return type; |
4670 | ||
6bdb985f | 4671 | /* Figure out which template is being instantiated. */ |
36a117a5 | 4672 | template = most_general_template (CLASSTYPE_TI_TEMPLATE (type)); |
5566b478 | 4673 | my_friendly_assert (TREE_CODE (template) == TEMPLATE_DECL, 279); |
73aad9b9 | 4674 | |
6bdb985f MM |
4675 | /* Figure out which arguments are being used to do the |
4676 | instantiation. */ | |
4677 | args = CLASSTYPE_TI_ARGS (type); | |
4c571114 | 4678 | PARTIAL_INSTANTIATION_P (type) = uses_template_parms (args); |
6bdb985f | 4679 | |
4c571114 MM |
4680 | if (pedantic && PARTIAL_INSTANTIATION_P (type)) |
4681 | /* If this is a partial instantiation, then we can't instantiate | |
4682 | the type; there's no telling whether or not one of the | |
4683 | template parameters might eventually be instantiated to some | |
4684 | value that results in a specialization being used. For | |
4685 | example, consider: | |
4686 | ||
4687 | template <class T> | |
4688 | struct S {}; | |
4689 | ||
4690 | template <class U> | |
4691 | void f(S<U>); | |
4692 | ||
4693 | template <> | |
4694 | struct S<int> {}; | |
4695 | ||
4696 | Now, the `S<U>' in `f<int>' is the specialization, not an | |
5db698f6 | 4697 | instantiation of the original template. */ |
f31c0a32 | 4698 | return type; |
4c571114 MM |
4699 | |
4700 | /* Determine what specialization of the original template to | |
4701 | instantiate. */ | |
4702 | if (PARTIAL_INSTANTIATION_P (type)) | |
6bdb985f MM |
4703 | /* There's no telling which specialization is appropriate at this |
4704 | point. Since all peeking at the innards of this partial | |
4705 | instantiation are extensions (like the "implicit typename" | |
4706 | extension, which allows users to omit the keyword `typename' on | |
4707 | names that are declared as types in template base classes), we | |
4708 | are free to do what we please. | |
4709 | ||
4710 | Trying to figure out which partial instantiation to use can | |
4711 | cause a crash. (Some of the template arguments don't even have | |
4712 | types.) So, we just use the most general version. */ | |
4713 | t = NULL_TREE; | |
4714 | else | |
73aad9b9 | 4715 | { |
6bdb985f MM |
4716 | t = most_specialized_class (template, args); |
4717 | ||
4718 | if (t == error_mark_node) | |
73aad9b9 | 4719 | { |
d8e178a0 | 4720 | const char *str = "candidates are:"; |
6bdb985f MM |
4721 | cp_error ("ambiguous class template instantiation for `%#T'", type); |
4722 | for (t = DECL_TEMPLATE_SPECIALIZATIONS (template); t; | |
4723 | t = TREE_CHAIN (t)) | |
73aad9b9 | 4724 | { |
6bdb985f MM |
4725 | if (get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t), |
4726 | args)) | |
4727 | { | |
4728 | cp_error_at ("%s %+#T", str, TREE_TYPE (t)); | |
4729 | str = " "; | |
4730 | } | |
73aad9b9 | 4731 | } |
6bdb985f | 4732 | TYPE_BEING_DEFINED (type) = 1; |
f31c0a32 | 4733 | return error_mark_node; |
73aad9b9 | 4734 | } |
73aad9b9 | 4735 | } |
6bdb985f MM |
4736 | |
4737 | if (t) | |
73aad9b9 JM |
4738 | pattern = TREE_TYPE (t); |
4739 | else | |
4740 | pattern = TREE_TYPE (template); | |
5566b478 | 4741 | |
4c571114 MM |
4742 | /* If the template we're instantiating is incomplete, then clearly |
4743 | there's nothing we can do. */ | |
d0f062fb | 4744 | if (!COMPLETE_TYPE_P (pattern)) |
f31c0a32 | 4745 | return type; |
5566b478 | 4746 | |
4c571114 MM |
4747 | /* If this is a partial instantiation, don't tsubst anything. We will |
4748 | only use this type for implicit typename, so the actual contents don't | |
4749 | matter. All that matters is whether a particular name is a type. */ | |
4750 | if (PARTIAL_INSTANTIATION_P (type)) | |
4751 | { | |
4752 | /* The fields set here must be kept in sync with those cleared | |
4753 | in begin_class_definition. */ | |
4754 | TYPE_BINFO_BASETYPES (type) = TYPE_BINFO_BASETYPES (pattern); | |
4755 | TYPE_FIELDS (type) = TYPE_FIELDS (pattern); | |
4756 | TYPE_METHODS (type) = TYPE_METHODS (pattern); | |
4757 | CLASSTYPE_TAGS (type) = CLASSTYPE_TAGS (pattern); | |
4758 | /* Pretend that the type is complete, so that we will look | |
4759 | inside it during name lookup and such. */ | |
867580ce | 4760 | TYPE_SIZE (type) = bitsize_zero_node; |
f31c0a32 | 4761 | return type; |
4c571114 MM |
4762 | } |
4763 | ||
4764 | /* If we've recursively instantiated too many templates, stop. */ | |
4765 | if (! push_tinst_level (type)) | |
f31c0a32 | 4766 | return type; |
4c571114 MM |
4767 | |
4768 | /* Now we're really doing the instantiation. Mark the type as in | |
4769 | the process of being defined. */ | |
4770 | TYPE_BEING_DEFINED (type) = 1; | |
4771 | ||
4772 | maybe_push_to_top_level (uses_template_parms (type)); | |
4c571114 | 4773 | |
73aad9b9 | 4774 | if (t) |
36a117a5 MM |
4775 | { |
4776 | /* This TYPE is actually a instantiation of of a partial | |
4777 | specialization. We replace the innermost set of ARGS with | |
4778 | the arguments appropriate for substitution. For example, | |
4779 | given: | |
4780 | ||
4781 | template <class T> struct S {}; | |
4782 | template <class T> struct S<T*> {}; | |
4783 | ||
4784 | and supposing that we are instantiating S<int*>, ARGS will | |
4785 | present be {int*} but we need {int}. */ | |
4786 | tree inner_args | |
4787 | = get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t), | |
4788 | args); | |
4789 | ||
4790 | /* If there were multiple levels in ARGS, replacing the | |
4791 | innermost level would alter CLASSTYPE_TI_ARGS, which we don't | |
4792 | want, so we make a copy first. */ | |
4793 | if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args)) | |
4794 | { | |
4795 | args = copy_node (args); | |
4796 | SET_TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args), inner_args); | |
4797 | } | |
4798 | else | |
4799 | args = inner_args; | |
4800 | } | |
8d019cef | 4801 | |
5566b478 MS |
4802 | if (flag_external_templates) |
4803 | { | |
4804 | if (flag_alt_external_templates) | |
4805 | { | |
4806 | CLASSTYPE_INTERFACE_ONLY (type) = interface_only; | |
4807 | SET_CLASSTYPE_INTERFACE_UNKNOWN_X (type, interface_unknown); | |
4808 | CLASSTYPE_VTABLE_NEEDS_WRITING (type) | |
2604412d JM |
4809 | = (! CLASSTYPE_INTERFACE_ONLY (type) |
4810 | && CLASSTYPE_INTERFACE_KNOWN (type)); | |
5566b478 MS |
4811 | } |
4812 | else | |
4813 | { | |
4814 | CLASSTYPE_INTERFACE_ONLY (type) = CLASSTYPE_INTERFACE_ONLY (pattern); | |
4815 | SET_CLASSTYPE_INTERFACE_UNKNOWN_X | |
4816 | (type, CLASSTYPE_INTERFACE_UNKNOWN (pattern)); | |
4817 | CLASSTYPE_VTABLE_NEEDS_WRITING (type) | |
2604412d JM |
4818 | = (! CLASSTYPE_INTERFACE_ONLY (type) |
4819 | && CLASSTYPE_INTERFACE_KNOWN (type)); | |
5566b478 MS |
4820 | } |
4821 | } | |
4822 | else | |
8d08fdba | 4823 | { |
5566b478 MS |
4824 | SET_CLASSTYPE_INTERFACE_UNKNOWN (type); |
4825 | CLASSTYPE_VTABLE_NEEDS_WRITING (type) = 1; | |
8d08fdba MS |
4826 | } |
4827 | ||
f7da6097 MS |
4828 | TYPE_HAS_CONSTRUCTOR (type) = TYPE_HAS_CONSTRUCTOR (pattern); |
4829 | TYPE_HAS_DESTRUCTOR (type) = TYPE_HAS_DESTRUCTOR (pattern); | |
f7da6097 MS |
4830 | TYPE_OVERLOADS_CALL_EXPR (type) = TYPE_OVERLOADS_CALL_EXPR (pattern); |
4831 | TYPE_OVERLOADS_ARRAY_REF (type) = TYPE_OVERLOADS_ARRAY_REF (pattern); | |
4832 | TYPE_OVERLOADS_ARROW (type) = TYPE_OVERLOADS_ARROW (pattern); | |
834c6dff MM |
4833 | TYPE_HAS_NEW_OPERATOR (type) = TYPE_HAS_NEW_OPERATOR (pattern); |
4834 | TYPE_HAS_ARRAY_NEW_OPERATOR (type) = TYPE_HAS_ARRAY_NEW_OPERATOR (pattern); | |
f7da6097 MS |
4835 | TYPE_GETS_DELETE (type) = TYPE_GETS_DELETE (pattern); |
4836 | TYPE_VEC_DELETE_TAKES_SIZE (type) = TYPE_VEC_DELETE_TAKES_SIZE (pattern); | |
4837 | TYPE_HAS_ASSIGN_REF (type) = TYPE_HAS_ASSIGN_REF (pattern); | |
4838 | TYPE_HAS_CONST_ASSIGN_REF (type) = TYPE_HAS_CONST_ASSIGN_REF (pattern); | |
4839 | TYPE_HAS_ABSTRACT_ASSIGN_REF (type) = TYPE_HAS_ABSTRACT_ASSIGN_REF (pattern); | |
4840 | TYPE_HAS_INIT_REF (type) = TYPE_HAS_INIT_REF (pattern); | |
4841 | TYPE_HAS_CONST_INIT_REF (type) = TYPE_HAS_CONST_INIT_REF (pattern); | |
f7da6097 MS |
4842 | TYPE_HAS_DEFAULT_CONSTRUCTOR (type) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern); |
4843 | TYPE_HAS_CONVERSION (type) = TYPE_HAS_CONVERSION (pattern); | |
4c6b7393 MM |
4844 | TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (type) |
4845 | = TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (pattern); | |
f7da6097 MS |
4846 | TYPE_USES_MULTIPLE_INHERITANCE (type) |
4847 | = TYPE_USES_MULTIPLE_INHERITANCE (pattern); | |
4848 | TYPE_USES_VIRTUAL_BASECLASSES (type) | |
4849 | = TYPE_USES_VIRTUAL_BASECLASSES (pattern); | |
4850 | TYPE_PACKED (type) = TYPE_PACKED (pattern); | |
4851 | TYPE_ALIGN (type) = TYPE_ALIGN (pattern); | |
eff71ab0 | 4852 | TYPE_FOR_JAVA (type) = TYPE_FOR_JAVA (pattern); /* For libjava's JArray<T> */ |
6bdb8141 JM |
4853 | if (ANON_AGGR_TYPE_P (pattern)) |
4854 | SET_ANON_AGGR_TYPE_P (type); | |
f7da6097 | 4855 | |
3fd71a52 MM |
4856 | if (TYPE_BINFO_BASETYPES (pattern)) |
4857 | { | |
4858 | tree base_list = NULL_TREE; | |
4859 | tree pbases = TYPE_BINFO_BASETYPES (pattern); | |
4860 | int i; | |
5566b478 | 4861 | |
3fd71a52 MM |
4862 | /* Substitute into each of the bases to determine the actual |
4863 | basetypes. */ | |
4864 | for (i = 0; i < TREE_VEC_LENGTH (pbases); ++i) | |
4865 | { | |
4866 | tree base; | |
4867 | tree access; | |
4868 | tree pbase; | |
5566b478 | 4869 | |
3fd71a52 | 4870 | pbase = TREE_VEC_ELT (pbases, i); |
711734a9 | 4871 | |
3fd71a52 MM |
4872 | /* Substitue to figure out the base class. */ |
4873 | base = tsubst (BINFO_TYPE (pbase), args, | |
4874 | /*complain=*/1, NULL_TREE); | |
4875 | if (base == error_mark_node) | |
4876 | continue; | |
711734a9 | 4877 | |
3fd71a52 MM |
4878 | /* Calculate the correct access node. */ |
4879 | if (TREE_VIA_VIRTUAL (pbase)) | |
4880 | { | |
4881 | if (TREE_VIA_PUBLIC (pbase)) | |
4882 | access = access_public_virtual_node; | |
4883 | else if (TREE_VIA_PROTECTED (pbase)) | |
4884 | access = access_protected_virtual_node; | |
d6479fe7 | 4885 | else |
3fd71a52 MM |
4886 | access = access_private_virtual_node; |
4887 | } | |
4888 | else | |
4889 | { | |
4890 | if (TREE_VIA_PUBLIC (pbase)) | |
4891 | access = access_public_node; | |
4892 | else if (TREE_VIA_PROTECTED (pbase)) | |
4893 | access = access_protected_node; | |
d6479fe7 | 4894 | else |
3fd71a52 MM |
4895 | access = access_private_node; |
4896 | } | |
dfbcd65a | 4897 | |
3fd71a52 MM |
4898 | base_list = tree_cons (access, base, base_list); |
4899 | } | |
dfbcd65a | 4900 | |
3fd71a52 MM |
4901 | /* The list is now in reverse order; correct that. */ |
4902 | base_list = nreverse (base_list); | |
4903 | ||
4904 | /* Now call xref_basetypes to set up all the base-class | |
4905 | information. */ | |
4906 | xref_basetypes (TREE_CODE (pattern) == RECORD_TYPE | |
4907 | ? (CLASSTYPE_DECLARED_CLASS (pattern) | |
4908 | ? class_type_node : record_type_node) | |
4909 | : union_type_node, | |
4910 | DECL_NAME (TYPE_NAME (pattern)), | |
4911 | type, | |
4912 | base_list); | |
4913 | } | |
5566b478 | 4914 | |
b74a0560 MM |
4915 | /* Now that our base classes are set up, enter the scope of the |
4916 | class, so that name lookups into base classes, etc. will work | |
4917 | corectly. This is precisely analagous to what we do in | |
4918 | begin_class_definition when defining an ordinary non-template | |
4919 | class. */ | |
4920 | pushclass (type, 1); | |
4921 | ||
5566b478 | 4922 | for (t = CLASSTYPE_TAGS (pattern); t; t = TREE_CHAIN (t)) |
8d08fdba | 4923 | { |
5566b478 | 4924 | tree tag = TREE_VALUE (t); |
36a117a5 MM |
4925 | tree name = TYPE_IDENTIFIER (tag); |
4926 | tree newtag; | |
5566b478 | 4927 | |
4393e105 | 4928 | newtag = tsubst (tag, args, /*complain=*/1, NULL_TREE); |
61a127b3 | 4929 | if (TREE_CODE (newtag) != ENUMERAL_TYPE) |
ed44da02 | 4930 | { |
486e4077 MM |
4931 | if (TYPE_LANG_SPECIFIC (tag) && CLASSTYPE_IS_TEMPLATE (tag)) |
4932 | /* Unfortunately, lookup_template_class sets | |
4933 | CLASSTYPE_IMPLICIT_INSTANTIATION for a partial | |
4934 | instantiation (i.e., for the type of a member template | |
4935 | class nested within a template class.) This behavior is | |
4936 | required for maybe_process_partial_specialization to work | |
4937 | correctly, but is not accurate in this case; the TAG is not | |
4938 | an instantiation of anything. (The corresponding | |
4939 | TEMPLATE_DECL is an instantiation, but the TYPE is not.) */ | |
4940 | CLASSTYPE_USE_TEMPLATE (newtag) = 0; | |
4941 | ||
ed44da02 MM |
4942 | /* Now, we call pushtag to put this NEWTAG into the scope of |
4943 | TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid | |
4944 | pushtag calling push_template_decl. We don't have to do | |
4945 | this for enums because it will already have been done in | |
4946 | tsubst_enum. */ | |
4947 | if (name) | |
4948 | SET_IDENTIFIER_TYPE_VALUE (name, newtag); | |
4949 | pushtag (name, newtag, /*globalize=*/0); | |
4950 | } | |
8d08fdba MS |
4951 | } |
4952 | ||
5566b478 MS |
4953 | /* Don't replace enum constants here. */ |
4954 | for (t = TYPE_FIELDS (pattern); t; t = TREE_CHAIN (t)) | |
8dd3f57a | 4955 | if (TREE_CODE (t) != CONST_DECL) |
5566b478 | 4956 | { |
ae58fa02 MM |
4957 | tree r; |
4958 | ||
4959 | /* The the file and line for this declaration, to assist in | |
4960 | error message reporting. Since we called push_tinst_level | |
4961 | above, we don't need to restore these. */ | |
4962 | lineno = DECL_SOURCE_LINE (t); | |
4963 | input_filename = DECL_SOURCE_FILE (t); | |
4964 | ||
4393e105 | 4965 | r = tsubst (t, args, /*complain=*/1, NULL_TREE); |
5566b478 MS |
4966 | if (TREE_CODE (r) == VAR_DECL) |
4967 | { | |
fa8d6e85 MM |
4968 | tree init; |
4969 | ||
94c82a77 | 4970 | if (DECL_DEFINED_IN_CLASS_P (r)) |
fa8d6e85 MM |
4971 | init = tsubst_expr (DECL_INITIAL (t), args, |
4972 | /*complain=*/1, NULL_TREE); | |
4973 | else | |
4974 | init = NULL_TREE; | |
4975 | ||
4976 | finish_static_data_member_decl (r, init, | |
4977 | /*asmspec_tree=*/NULL_TREE, | |
fa8d6e85 MM |
4978 | /*flags=*/0); |
4979 | ||
6ba89f8e MM |
4980 | if (DECL_DEFINED_IN_CLASS_P (r)) |
4981 | check_static_variable_definition (r, TREE_TYPE (r)); | |
5566b478 | 4982 | } |
61a127b3 MM |
4983 | |
4984 | /* R will have a TREE_CHAIN if and only if it has already been | |
4985 | processed by finish_member_declaration. This can happen | |
4986 | if, for example, it is a TYPE_DECL for a class-scoped | |
4987 | ENUMERAL_TYPE; such a thing will already have been added to | |
4988 | the field list by tsubst_enum above. */ | |
4989 | if (!TREE_CHAIN (r)) | |
4990 | { | |
4991 | set_current_access_from_decl (r); | |
4992 | finish_member_declaration (r); | |
4993 | } | |
5566b478 | 4994 | } |
8d08fdba | 4995 | |
61a127b3 MM |
4996 | /* Set up the list (TYPE_METHODS) and vector (CLASSTYPE_METHOD_VEC) |
4997 | for this instantiation. */ | |
4998 | for (t = TYPE_METHODS (pattern); t; t = TREE_CHAIN (t)) | |
4999 | { | |
4393e105 | 5000 | tree r = tsubst (t, args, /*complain=*/1, NULL_TREE); |
61a127b3 MM |
5001 | set_current_access_from_decl (r); |
5002 | finish_member_declaration (r); | |
5003 | } | |
8d08fdba | 5004 | |
2604412d JM |
5005 | /* Construct the DECL_FRIENDLIST for the new class type. */ |
5006 | typedecl = TYPE_MAIN_DECL (type); | |
5007 | for (t = DECL_FRIENDLIST (TYPE_MAIN_DECL (pattern)); | |
5008 | t != NULL_TREE; | |
5009 | t = TREE_CHAIN (t)) | |
f84b4be9 | 5010 | { |
2604412d | 5011 | tree friends; |
f84b4be9 | 5012 | |
2604412d JM |
5013 | for (friends = TREE_VALUE (t); |
5014 | friends != NULL_TREE; | |
5015 | friends = TREE_CHAIN (friends)) | |
0c0aac2f MM |
5016 | if (TREE_PURPOSE (friends) == error_mark_node) |
5017 | add_friend (type, | |
5018 | tsubst_friend_function (TREE_VALUE (friends), | |
5019 | args)); | |
5020 | else | |
4f1c5b7d | 5021 | my_friendly_abort (20000216); |
2604412d | 5022 | } |
5566b478 | 5023 | |
6757edfe MM |
5024 | for (t = CLASSTYPE_FRIEND_CLASSES (pattern); |
5025 | t != NULL_TREE; | |
5026 | t = TREE_CHAIN (t)) | |
5027 | { | |
5028 | tree friend_type = TREE_VALUE (t); | |
1aed5355 | 5029 | tree new_friend_type; |
6757edfe | 5030 | |
36a117a5 MM |
5031 | if (TREE_CODE (friend_type) == TEMPLATE_DECL) |
5032 | new_friend_type = tsubst_friend_class (friend_type, args); | |
5033 | else if (uses_template_parms (friend_type)) | |
4393e105 MM |
5034 | new_friend_type = tsubst (friend_type, args, /*complain=*/1, |
5035 | NULL_TREE); | |
36a117a5 | 5036 | else |
61fc8c9e MM |
5037 | { |
5038 | tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type)); | |
36a117a5 | 5039 | |
61fc8c9e MM |
5040 | /* The call to xref_tag_from_type does injection for friend |
5041 | classes. */ | |
5042 | push_nested_namespace (ns); | |
5043 | new_friend_type = | |
5044 | xref_tag_from_type (friend_type, NULL_TREE, 1); | |
5045 | pop_nested_namespace (ns); | |
5046 | } | |
1aed5355 MM |
5047 | |
5048 | if (TREE_CODE (friend_type) == TEMPLATE_DECL) | |
5049 | /* Trick make_friend_class into realizing that the friend | |
5050 | we're adding is a template, not an ordinary class. It's | |
5051 | important that we use make_friend_class since it will | |
5052 | perform some error-checking and output cross-reference | |
5053 | information. */ | |
5054 | ++processing_template_decl; | |
5055 | ||
5056 | make_friend_class (type, new_friend_type); | |
fc378698 | 5057 | |
1aed5355 MM |
5058 | if (TREE_CODE (friend_type) == TEMPLATE_DECL) |
5059 | --processing_template_decl; | |
6757edfe | 5060 | } |
fc378698 | 5061 | |
2604412d JM |
5062 | for (t = TYPE_FIELDS (type); t; t = TREE_CHAIN (t)) |
5063 | if (TREE_CODE (t) == FIELD_DECL) | |
5064 | { | |
5065 | TREE_TYPE (t) = complete_type (TREE_TYPE (t)); | |
5066 | require_complete_type (t); | |
5067 | } | |
5566b478 | 5068 | |
61a127b3 MM |
5069 | /* Set the file and line number information to whatever is given for |
5070 | the class itself. This puts error messages involving generated | |
5071 | implicit functions at a predictable point, and the same point | |
5072 | that would be used for non-template classes. */ | |
5073 | lineno = DECL_SOURCE_LINE (typedecl); | |
5074 | input_filename = DECL_SOURCE_FILE (typedecl); | |
5075 | ||
5076 | unreverse_member_declarations (type); | |
9f33663b | 5077 | finish_struct_1 (type); |
2604412d | 5078 | CLASSTYPE_GOT_SEMICOLON (type) = 1; |
e92cc029 | 5079 | |
5524676d JM |
5080 | /* Clear this now so repo_template_used is happy. */ |
5081 | TYPE_BEING_DEFINED (type) = 0; | |
2604412d | 5082 | repo_template_used (type); |
8d08fdba | 5083 | |
9188c363 MM |
5084 | /* Now that the class is complete, instantiate default arguments for |
5085 | any member functions. We don't do this earlier because the | |
5086 | default arguments may reference members of the class. */ | |
5087 | if (!PRIMARY_TEMPLATE_P (template)) | |
5088 | for (t = TYPE_METHODS (type); t; t = TREE_CHAIN (t)) | |
5089 | if (TREE_CODE (t) == FUNCTION_DECL | |
5090 | /* Implicitly generated member functions will not have tmplate | |
5091 | information; they are not instantiations, but instead are | |
5092 | created "fresh" for each instantiation. */ | |
5093 | && DECL_TEMPLATE_INFO (t)) | |
5094 | tsubst_default_arguments (t); | |
5095 | ||
b74a0560 | 5096 | popclass (); |
5566b478 MS |
5097 | pop_from_top_level (); |
5098 | pop_tinst_level (); | |
5099 | ||
5100 | return type; | |
8d08fdba MS |
5101 | } |
5102 | ||
5103 | static int | |
5104 | list_eq (t1, t2) | |
5105 | tree t1, t2; | |
5106 | { | |
5107 | if (t1 == NULL_TREE) | |
5108 | return t2 == NULL_TREE; | |
5109 | if (t2 == NULL_TREE) | |
5110 | return 0; | |
5111 | /* Don't care if one declares its arg const and the other doesn't -- the | |
5112 | main variant of the arg type is all that matters. */ | |
5113 | if (TYPE_MAIN_VARIANT (TREE_VALUE (t1)) | |
5114 | != TYPE_MAIN_VARIANT (TREE_VALUE (t2))) | |
5115 | return 0; | |
5116 | return list_eq (TREE_CHAIN (t1), TREE_CHAIN (t2)); | |
5117 | } | |
5118 | ||
00d3396f JM |
5119 | /* If arg is a non-type template parameter that does not depend on template |
5120 | arguments, fold it like we weren't in the body of a template. */ | |
5121 | ||
5122 | static tree | |
5123 | maybe_fold_nontype_arg (arg) | |
5124 | tree arg; | |
5125 | { | |
7610f2ce JM |
5126 | /* If we're not in a template, ARG is already as simple as it's going to |
5127 | get, and trying to reprocess the trees will break. */ | |
5128 | if (! processing_template_decl) | |
5129 | return arg; | |
5130 | ||
2f939d94 | 5131 | if (!TYPE_P (arg) && !uses_template_parms (arg)) |
00d3396f JM |
5132 | { |
5133 | /* Sometimes, one of the args was an expression involving a | |
5134 | template constant parameter, like N - 1. Now that we've | |
5135 | tsubst'd, we might have something like 2 - 1. This will | |
5136 | confuse lookup_template_class, so we do constant folding | |
5137 | here. We have to unset processing_template_decl, to | |
5138 | fool build_expr_from_tree() into building an actual | |
5139 | tree. */ | |
5140 | ||
5141 | int saved_processing_template_decl = processing_template_decl; | |
5142 | processing_template_decl = 0; | |
5143 | arg = fold (build_expr_from_tree (arg)); | |
5144 | processing_template_decl = saved_processing_template_decl; | |
5145 | } | |
5146 | return arg; | |
5147 | } | |
5148 | ||
a221d52f | 5149 | /* Return the TREE_VEC with the arguments for the innermost template header, |
36a117a5 MM |
5150 | where ARGS is either that or the VEC of VECs for all the |
5151 | arguments. */ | |
a221d52f JM |
5152 | |
5153 | tree | |
36a117a5 | 5154 | innermost_args (args) |
a221d52f | 5155 | tree args; |
a221d52f | 5156 | { |
36a117a5 | 5157 | return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args)); |
a221d52f | 5158 | } |
f84b4be9 | 5159 | |
830bfa74 MM |
5160 | /* Substitute ARGS into the vector of template arguments T. */ |
5161 | ||
e9659ab0 | 5162 | static tree |
4393e105 | 5163 | tsubst_template_arg_vector (t, args, complain) |
830bfa74 MM |
5164 | tree t; |
5165 | tree args; | |
4393e105 | 5166 | int complain; |
830bfa74 MM |
5167 | { |
5168 | int len = TREE_VEC_LENGTH (t), need_new = 0, i; | |
5169 | tree *elts = (tree *) alloca (len * sizeof (tree)); | |
5170 | ||
5171 | bzero ((char *) elts, len * sizeof (tree)); | |
5172 | ||
5173 | for (i = 0; i < len; i++) | |
5174 | { | |
5175 | if (TREE_VEC_ELT (t, i) != NULL_TREE | |
5176 | && TREE_CODE (TREE_VEC_ELT (t, i)) == TREE_VEC) | |
4393e105 MM |
5177 | elts[i] = tsubst_template_arg_vector (TREE_VEC_ELT (t, i), |
5178 | args, complain); | |
830bfa74 MM |
5179 | else |
5180 | elts[i] = maybe_fold_nontype_arg | |
4393e105 MM |
5181 | (tsubst_expr (TREE_VEC_ELT (t, i), args, complain, |
5182 | NULL_TREE)); | |
830bfa74 MM |
5183 | |
5184 | if (elts[i] != TREE_VEC_ELT (t, i)) | |
5185 | need_new = 1; | |
5186 | } | |
5187 | ||
5188 | if (!need_new) | |
5189 | return t; | |
5190 | ||
f31c0a32 | 5191 | t = make_tree_vec (len); |
830bfa74 MM |
5192 | for (i = 0; i < len; i++) |
5193 | TREE_VEC_ELT (t, i) = elts[i]; | |
5194 | ||
5195 | return t; | |
5196 | } | |
5197 | ||
36a117a5 MM |
5198 | /* Return the result of substituting ARGS into the template parameters |
5199 | given by PARMS. If there are m levels of ARGS and m + n levels of | |
5200 | PARMS, then the result will contain n levels of PARMS. For | |
5201 | example, if PARMS is `template <class T> template <class U> | |
5202 | template <T*, U, class V>' and ARGS is {{int}, {double}} then the | |
5203 | result will be `template <int*, double, class V>'. */ | |
5204 | ||
e9659ab0 | 5205 | static tree |
4393e105 | 5206 | tsubst_template_parms (parms, args, complain) |
36a117a5 MM |
5207 | tree parms; |
5208 | tree args; | |
4393e105 | 5209 | int complain; |
36a117a5 | 5210 | { |
f71f87f9 MM |
5211 | tree r = NULL_TREE; |
5212 | tree* new_parms; | |
36a117a5 MM |
5213 | |
5214 | for (new_parms = &r; | |
5215 | TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args); | |
5216 | new_parms = &(TREE_CHAIN (*new_parms)), | |
5217 | parms = TREE_CHAIN (parms)) | |
5218 | { | |
5219 | tree new_vec = | |
5220 | make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms))); | |
5221 | int i; | |
5222 | ||
5223 | for (i = 0; i < TREE_VEC_LENGTH (new_vec); ++i) | |
5224 | { | |
5225 | tree default_value = | |
5226 | TREE_PURPOSE (TREE_VEC_ELT (TREE_VALUE (parms), i)); | |
5227 | tree parm_decl = | |
5228 | TREE_VALUE (TREE_VEC_ELT (TREE_VALUE (parms), i)); | |
5229 | ||
5230 | TREE_VEC_ELT (new_vec, i) | |
4393e105 MM |
5231 | = build_tree_list (tsubst (default_value, args, complain, |
5232 | NULL_TREE), | |
5233 | tsubst (parm_decl, args, complain, | |
5234 | NULL_TREE)); | |
36a117a5 MM |
5235 | } |
5236 | ||
5237 | *new_parms = | |
5238 | tree_cons (build_int_2 (0, (TMPL_PARMS_DEPTH (parms) | |
5239 | - TMPL_ARGS_DEPTH (args))), | |
5240 | new_vec, NULL_TREE); | |
5241 | } | |
5242 | ||
5243 | return r; | |
5244 | } | |
5245 | ||
ed44da02 MM |
5246 | /* Substitute the ARGS into the indicated aggregate (or enumeration) |
5247 | type T. If T is not an aggregate or enumeration type, it is | |
5248 | handled as if by tsubst. IN_DECL is as for tsubst. If | |
5249 | ENTERING_SCOPE is non-zero, T is the context for a template which | |
5250 | we are presently tsubst'ing. Return the subsituted value. */ | |
36a117a5 | 5251 | |
e9659ab0 | 5252 | static tree |
4393e105 | 5253 | tsubst_aggr_type (t, args, complain, in_decl, entering_scope) |
36a117a5 MM |
5254 | tree t; |
5255 | tree args; | |
4393e105 | 5256 | int complain; |
36a117a5 MM |
5257 | tree in_decl; |
5258 | int entering_scope; | |
5259 | { | |
5260 | if (t == NULL_TREE) | |
5261 | return NULL_TREE; | |
5262 | ||
5263 | switch (TREE_CODE (t)) | |
5264 | { | |
5265 | case RECORD_TYPE: | |
5266 | if (TYPE_PTRMEMFUNC_P (t)) | |
5267 | { | |
5268 | tree r = build_ptrmemfunc_type | |
4393e105 | 5269 | (tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl)); |
adecb3f4 MM |
5270 | return cp_build_qualified_type_real (r, TYPE_QUALS (t), |
5271 | complain); | |
36a117a5 MM |
5272 | } |
5273 | ||
5274 | /* else fall through */ | |
ed44da02 | 5275 | case ENUMERAL_TYPE: |
36a117a5 | 5276 | case UNION_TYPE: |
5db698f6 | 5277 | if (TYPE_TEMPLATE_INFO (t)) |
36a117a5 MM |
5278 | { |
5279 | tree argvec; | |
5280 | tree context; | |
5281 | tree r; | |
5282 | ||
5283 | /* First, determine the context for the type we are looking | |
5284 | up. */ | |
5285 | if (TYPE_CONTEXT (t) != NULL_TREE) | |
5286 | context = tsubst_aggr_type (TYPE_CONTEXT (t), args, | |
4393e105 | 5287 | complain, |
36a117a5 MM |
5288 | in_decl, /*entering_scope=*/1); |
5289 | else | |
5290 | context = NULL_TREE; | |
5291 | ||
5292 | /* Then, figure out what arguments are appropriate for the | |
5293 | type we are trying to find. For example, given: | |
5294 | ||
5295 | template <class T> struct S; | |
5296 | template <class T, class U> void f(T, U) { S<U> su; } | |
5297 | ||
5298 | and supposing that we are instantiating f<int, double>, | |
5299 | then our ARGS will be {int, double}, but, when looking up | |
5300 | S we only want {double}. */ | |
4393e105 MM |
5301 | argvec = tsubst_template_arg_vector (TYPE_TI_ARGS (t), args, |
5302 | complain); | |
36a117a5 MM |
5303 | |
5304 | r = lookup_template_class (t, argvec, in_decl, context, | |
5305 | entering_scope); | |
5306 | ||
adecb3f4 MM |
5307 | return cp_build_qualified_type_real (r, TYPE_QUALS (t), |
5308 | complain); | |
36a117a5 MM |
5309 | } |
5310 | else | |
5311 | /* This is not a template type, so there's nothing to do. */ | |
5312 | return t; | |
5313 | ||
5314 | default: | |
4393e105 | 5315 | return tsubst (t, args, complain, in_decl); |
36a117a5 MM |
5316 | } |
5317 | } | |
5318 | ||
9188c363 MM |
5319 | /* Substitute into the default argument ARG (a default argument for |
5320 | FN), which has the indicated TYPE. */ | |
5321 | ||
5322 | tree | |
5323 | tsubst_default_argument (fn, type, arg) | |
5324 | tree fn; | |
5325 | tree type; | |
5326 | tree arg; | |
5327 | { | |
5328 | /* This default argument came from a template. Instantiate the | |
5329 | default argument here, not in tsubst. In the case of | |
5330 | something like: | |
5331 | ||
5332 | template <class T> | |
5333 | struct S { | |
5334 | static T t(); | |
5335 | void f(T = t()); | |
5336 | }; | |
5337 | ||
5338 | we must be careful to do name lookup in the scope of S<T>, | |
5339 | rather than in the current class. */ | |
5340 | if (DECL_CLASS_SCOPE_P (fn)) | |
4f1c5b7d | 5341 | pushclass (DECL_CONTEXT (fn), 2); |
9188c363 MM |
5342 | |
5343 | arg = tsubst_expr (arg, DECL_TI_ARGS (fn), /*complain=*/1, NULL_TREE); | |
5344 | ||
5345 | if (DECL_CLASS_SCOPE_P (fn)) | |
5346 | popclass (); | |
5347 | ||
5348 | /* Make sure the default argument is reasonable. */ | |
5349 | arg = check_default_argument (type, arg); | |
5350 | ||
5351 | return arg; | |
5352 | } | |
5353 | ||
5354 | /* Substitute into all the default arguments for FN. */ | |
5355 | ||
5356 | static void | |
5357 | tsubst_default_arguments (fn) | |
5358 | tree fn; | |
5359 | { | |
5360 | tree arg; | |
5361 | tree tmpl_args; | |
5362 | ||
5363 | tmpl_args = DECL_TI_ARGS (fn); | |
5364 | ||
5365 | /* If this function is not yet instantiated, we certainly don't need | |
5366 | its default arguments. */ | |
5367 | if (uses_template_parms (tmpl_args)) | |
5368 | return; | |
5369 | ||
5370 | for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn)); | |
5371 | arg; | |
5372 | arg = TREE_CHAIN (arg)) | |
5373 | if (TREE_PURPOSE (arg)) | |
5374 | TREE_PURPOSE (arg) = tsubst_default_argument (fn, | |
5375 | TREE_VALUE (arg), | |
5376 | TREE_PURPOSE (arg)); | |
5377 | } | |
5378 | ||
ae58fa02 MM |
5379 | /* Substitute the ARGS into the T, which is a _DECL. TYPE is the |
5380 | (already computed) substitution of ARGS into TREE_TYPE (T), if | |
5381 | appropriate. Return the result of the substitution. IN_DECL is as | |
5382 | for tsubst. */ | |
00d3396f | 5383 | |
e9659ab0 | 5384 | static tree |
ae58fa02 MM |
5385 | tsubst_decl (t, args, type, in_decl) |
5386 | tree t; | |
5387 | tree args; | |
5388 | tree type; | |
8d08fdba MS |
5389 | tree in_decl; |
5390 | { | |
ae58fa02 MM |
5391 | int saved_lineno; |
5392 | char* saved_filename; | |
b370501f | 5393 | tree r = NULL_TREE; |
830bfa74 | 5394 | |
ae58fa02 MM |
5395 | /* Set the filename and linenumber to improve error-reporting. */ |
5396 | saved_lineno = lineno; | |
5397 | saved_filename = input_filename; | |
5398 | lineno = DECL_SOURCE_LINE (t); | |
5399 | input_filename = DECL_SOURCE_FILE (t); | |
b7484fbe | 5400 | |
8d08fdba MS |
5401 | switch (TREE_CODE (t)) |
5402 | { | |
98c1c668 JM |
5403 | case TEMPLATE_DECL: |
5404 | { | |
5405 | /* We can get here when processing a member template function | |
5406 | of a template class. */ | |
98c1c668 | 5407 | tree decl = DECL_TEMPLATE_RESULT (t); |
386b8a85 | 5408 | tree spec; |
db2767b6 | 5409 | int is_template_template_parm = DECL_TEMPLATE_TEMPLATE_PARM_P (t); |
98c1c668 | 5410 | |
db2767b6 MM |
5411 | if (!is_template_template_parm) |
5412 | { | |
36a117a5 MM |
5413 | /* We might already have an instance of this template. |
5414 | The ARGS are for the surrounding class type, so the | |
5415 | full args contain the tsubst'd args for the context, | |
5416 | plus the innermost args from the template decl. */ | |
5417 | tree tmpl_args = DECL_CLASS_TEMPLATE_P (t) | |
5418 | ? CLASSTYPE_TI_ARGS (TREE_TYPE (t)) | |
17aec3eb | 5419 | : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t)); |
7ddedda4 MM |
5420 | tree full_args; |
5421 | ||
4393e105 MM |
5422 | full_args = tsubst_template_arg_vector (tmpl_args, args, |
5423 | /*complain=*/1); | |
36a117a5 MM |
5424 | |
5425 | /* tsubst_template_arg_vector doesn't copy the vector if | |
5426 | nothing changed. But, *something* should have | |
5427 | changed. */ | |
5428 | my_friendly_assert (full_args != tmpl_args, 0); | |
5429 | ||
5430 | spec = retrieve_specialization (t, full_args); | |
db2767b6 | 5431 | if (spec != NULL_TREE) |
ae58fa02 MM |
5432 | { |
5433 | r = spec; | |
5434 | break; | |
5435 | } | |
db2767b6 | 5436 | } |
98c1c668 JM |
5437 | |
5438 | /* Make a new template decl. It will be similar to the | |
5439 | original, but will record the current template arguments. | |
5440 | We also create a new function declaration, which is just | |
5441 | like the old one, but points to this new template, rather | |
5442 | than the old one. */ | |
0acf7199 | 5443 | r = copy_decl (t); |
ae58fa02 MM |
5444 | my_friendly_assert (DECL_LANG_SPECIFIC (r) != 0, 0); |
5445 | TREE_CHAIN (r) = NULL_TREE; | |
db2767b6 MM |
5446 | |
5447 | if (is_template_template_parm) | |
5448 | { | |
4393e105 | 5449 | tree new_decl = tsubst (decl, args, /*complain=*/1, in_decl); |
17aec3eb | 5450 | DECL_TEMPLATE_RESULT (r) = new_decl; |
ae58fa02 MM |
5451 | TREE_TYPE (r) = TREE_TYPE (new_decl); |
5452 | break; | |
db2767b6 MM |
5453 | } |
5454 | ||
ae58fa02 | 5455 | DECL_CONTEXT (r) |
4f1c5b7d | 5456 | = tsubst_aggr_type (DECL_CONTEXT (t), args, |
4393e105 MM |
5457 | /*complain=*/1, in_decl, |
5458 | /*entering_scope=*/1); | |
4f1c5b7d MM |
5459 | DECL_VIRTUAL_CONTEXT (r) |
5460 | = tsubst_aggr_type (DECL_VIRTUAL_CONTEXT (t), args, | |
5461 | /*complain=*/1, in_decl, | |
5462 | /*entering_scope=*/1); | |
ae58fa02 | 5463 | DECL_TEMPLATE_INFO (r) = build_tree_list (t, args); |
93cdc044 JM |
5464 | |
5465 | if (TREE_CODE (decl) == TYPE_DECL) | |
5466 | { | |
4393e105 MM |
5467 | tree new_type = tsubst (TREE_TYPE (t), args, |
5468 | /*complain=*/1, in_decl); | |
ae58fa02 MM |
5469 | TREE_TYPE (r) = new_type; |
5470 | CLASSTYPE_TI_TEMPLATE (new_type) = r; | |
17aec3eb | 5471 | DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type); |
ae58fa02 | 5472 | DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type); |
93cdc044 JM |
5473 | } |
5474 | else | |
5475 | { | |
4393e105 | 5476 | tree new_decl = tsubst (decl, args, /*complain=*/1, in_decl); |
17aec3eb RK |
5477 | |
5478 | DECL_TEMPLATE_RESULT (r) = new_decl; | |
ae58fa02 MM |
5479 | DECL_TI_TEMPLATE (new_decl) = r; |
5480 | TREE_TYPE (r) = TREE_TYPE (new_decl); | |
5481 | DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl); | |
93cdc044 JM |
5482 | } |
5483 | ||
ae58fa02 MM |
5484 | SET_DECL_IMPLICIT_INSTANTIATION (r); |
5485 | DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE; | |
5486 | DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE; | |
98c1c668 JM |
5487 | |
5488 | /* The template parameters for this new template are all the | |
5489 | template parameters for the old template, except the | |
5490 | outermost level of parameters. */ | |
ae58fa02 | 5491 | DECL_TEMPLATE_PARMS (r) |
4393e105 MM |
5492 | = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args, |
5493 | /*complain=*/1); | |
98c1c668 | 5494 | |
93cdc044 | 5495 | if (PRIMARY_TEMPLATE_P (t)) |
ae58fa02 | 5496 | DECL_PRIMARY_TEMPLATE (r) = r; |
93cdc044 | 5497 | |
8d019cef | 5498 | /* We don't partially instantiate partial specializations. */ |
93cdc044 | 5499 | if (TREE_CODE (decl) == TYPE_DECL) |
ae58fa02 | 5500 | break; |
93cdc044 | 5501 | |
75650646 MM |
5502 | for (spec = DECL_TEMPLATE_SPECIALIZATIONS (t); |
5503 | spec != NULL_TREE; | |
5504 | spec = TREE_CHAIN (spec)) | |
5505 | { | |
5506 | /* It helps to consider example here. Consider: | |
5507 | ||
5508 | template <class T> | |
5509 | struct S { | |
5510 | template <class U> | |
5511 | void f(U u); | |
5512 | ||
5513 | template <> | |
5514 | void f(T* t) {} | |
5515 | }; | |
5516 | ||
5517 | Now, for example, we are instantiating S<int>::f(U u). | |
5518 | We want to make a template: | |
5519 | ||
5520 | template <class U> | |
5521 | void S<int>::f(U); | |
5522 | ||
5523 | It will have a specialization, for the case U = int*, of | |
5524 | the form: | |
5525 | ||
5526 | template <> | |
5527 | void S<int>::f<int*>(int*); | |
5528 | ||
5529 | This specialization will be an instantiation of | |
5530 | the specialization given in the declaration of S, with | |
5531 | argument list int*. */ | |
5532 | ||
5533 | tree fn = TREE_VALUE (spec); | |
5534 | tree spec_args; | |
5535 | tree new_fn; | |
5536 | ||
5537 | if (!DECL_TEMPLATE_SPECIALIZATION (fn)) | |
5538 | /* Instantiations are on the same list, but they're of | |
5539 | no concern to us. */ | |
5540 | continue; | |
5541 | ||
36a117a5 MM |
5542 | if (TREE_CODE (fn) != TEMPLATE_DECL) |
5543 | /* A full specialization. There's no need to record | |
5544 | that here. */ | |
5545 | continue; | |
5546 | ||
4393e105 MM |
5547 | spec_args = tsubst (DECL_TI_ARGS (fn), args, |
5548 | /*complain=*/1, in_decl); | |
17aec3eb RK |
5549 | new_fn |
5550 | = tsubst (DECL_TEMPLATE_RESULT (most_general_template (fn)), | |
5551 | spec_args, /*complain=*/1, in_decl); | |
36a117a5 | 5552 | DECL_TI_TEMPLATE (new_fn) = fn; |
ae58fa02 | 5553 | register_specialization (new_fn, r, |
36a117a5 | 5554 | innermost_args (spec_args)); |
75650646 MM |
5555 | } |
5556 | ||
5557 | /* Record this partial instantiation. */ | |
ae58fa02 | 5558 | register_specialization (r, t, |
17aec3eb | 5559 | DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r))); |
75650646 | 5560 | |
98c1c668 | 5561 | } |
ae58fa02 | 5562 | break; |
8d08fdba MS |
5563 | |
5564 | case FUNCTION_DECL: | |
5565 | { | |
386b8a85 | 5566 | tree ctx; |
87603ed0 | 5567 | tree argvec = NULL_TREE; |
cf38f48a | 5568 | tree *friends; |
36a117a5 | 5569 | tree gen_tmpl; |
5566b478 | 5570 | int member; |
d8c4447d MM |
5571 | int args_depth; |
5572 | int parms_depth; | |
5566b478 | 5573 | |
36a117a5 MM |
5574 | /* Nobody should be tsubst'ing into non-template functions. */ |
5575 | my_friendly_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE, 0); | |
5576 | ||
5577 | if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL) | |
5578 | { | |
5579 | tree spec; | |
5580 | ||
5581 | /* Calculate the most general template of which R is a | |
5582 | specialization, and the complete set of arguments used to | |
5583 | specialize R. */ | |
5584 | gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t)); | |
7ddedda4 MM |
5585 | argvec |
5586 | = tsubst_template_arg_vector (DECL_TI_ARGS | |
5587 | (DECL_TEMPLATE_RESULT (gen_tmpl)), | |
4393e105 | 5588 | args, /*complain=*/1); |
36a117a5 MM |
5589 | |
5590 | /* Check to see if we already have this specialization. */ | |
5591 | spec = retrieve_specialization (gen_tmpl, argvec); | |
7ddedda4 | 5592 | |
36a117a5 | 5593 | if (spec) |
ae58fa02 MM |
5594 | { |
5595 | r = spec; | |
5596 | break; | |
5597 | } | |
d8c4447d MM |
5598 | |
5599 | /* Here, we deal with the peculiar case: | |
5600 | ||
5601 | template <class T> struct S { | |
5602 | template <class U> friend void f(); | |
5603 | }; | |
74b846e0 | 5604 | template <class U> void f() {} |
d8c4447d MM |
5605 | template S<int>; |
5606 | template void f<double>(); | |
5607 | ||
5608 | Here, the ARGS for the instantiation of will be {int, | |
5609 | double}. But, we only need as many ARGS as there are | |
5610 | levels of template parameters in CODE_PATTERN. We are | |
5611 | careful not to get fooled into reducing the ARGS in | |
5612 | situations like: | |
5613 | ||
5614 | template <class T> struct S { template <class U> void f(U); } | |
5615 | template <class T> template <> void S<T>::f(int) {} | |
5616 | ||
5617 | which we can spot because the pattern will be a | |
5618 | specialization in this case. */ | |
5619 | args_depth = TMPL_ARGS_DEPTH (args); | |
5620 | parms_depth = | |
5621 | TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t))); | |
5622 | if (args_depth > parms_depth | |
5623 | && !DECL_TEMPLATE_SPECIALIZATION (t)) | |
5624 | { | |
5625 | my_friendly_assert (DECL_FRIEND_P (t), 0); | |
5626 | ||
5627 | if (parms_depth > 1) | |
5628 | { | |
5629 | int i; | |
5630 | ||
f31c0a32 | 5631 | args = make_tree_vec (parms_depth); |
d8c4447d MM |
5632 | for (i = 0; i < parms_depth; ++i) |
5633 | TREE_VEC_ELT (args, i) = | |
5634 | TREE_VEC_ELT (args, i + (args_depth - parms_depth)); | |
5635 | } | |
5636 | else | |
5637 | args = TREE_VEC_ELT (args, args_depth - parms_depth); | |
5638 | } | |
36a117a5 MM |
5639 | } |
5640 | else | |
5641 | { | |
5642 | /* This special case arises when we have something like this: | |
5643 | ||
5644 | template <class T> struct S { | |
5645 | friend void f<int>(int, double); | |
5646 | }; | |
5647 | ||
5648 | Here, the DECL_TI_TEMPLATE for the friend declaration | |
39c01e4c MM |
5649 | will be a LOOKUP_EXPR or an IDENTIFIER_NODE. We are |
5650 | being called from tsubst_friend_function, and we want | |
5651 | only to create a new decl (R) with appropriate types so | |
5652 | that we can call determine_specialization. */ | |
5653 | my_friendly_assert ((TREE_CODE (DECL_TI_TEMPLATE (t)) | |
5654 | == LOOKUP_EXPR) | |
5655 | || (TREE_CODE (DECL_TI_TEMPLATE (t)) | |
5656 | == IDENTIFIER_NODE), 0); | |
36a117a5 MM |
5657 | gen_tmpl = NULL_TREE; |
5658 | } | |
5659 | ||
6eb3bb27 | 5660 | if (DECL_CLASS_SCOPE_P (t)) |
8d08fdba | 5661 | { |
5566b478 MS |
5662 | if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t))) |
5663 | member = 2; | |
5664 | else | |
5665 | member = 1; | |
4f1c5b7d | 5666 | ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, |
4393e105 | 5667 | /*complain=*/1, t, |
36a117a5 | 5668 | /*entering_scope=*/1); |
5566b478 MS |
5669 | } |
5670 | else | |
5671 | { | |
5672 | member = 0; | |
4f1c5b7d | 5673 | ctx = DECL_CONTEXT (t); |
5566b478 | 5674 | } |
4393e105 | 5675 | type = tsubst (type, args, /*complain=*/1, in_decl); |
b3da7bb1 MM |
5676 | if (type == error_mark_node) |
5677 | return error_mark_node; | |
8d08fdba | 5678 | |
5566b478 MS |
5679 | /* We do NOT check for matching decls pushed separately at this |
5680 | point, as they may not represent instantiations of this | |
5681 | template, and in any case are considered separate under the | |
312e7d50 | 5682 | discrete model. Instead, see add_maybe_template. */ |
0acf7199 | 5683 | r = copy_decl (t); |
e1467ff2 | 5684 | DECL_USE_TEMPLATE (r) = 0; |
5566b478 MS |
5685 | TREE_TYPE (r) = type; |
5686 | ||
4f1c5b7d MM |
5687 | DECL_CONTEXT (r) = ctx; |
5688 | DECL_VIRTUAL_CONTEXT (r) | |
5689 | = tsubst_aggr_type (DECL_VIRTUAL_CONTEXT (t), args, | |
5690 | /*complain=*/1, t, | |
4393e105 | 5691 | /*entering_scope=*/1); |
5566b478 | 5692 | |
4c571114 | 5693 | if (member && IDENTIFIER_TYPENAME_P (DECL_NAME (r))) |
f181d4ae MM |
5694 | /* Type-conversion operator. Reconstruct the name, in |
5695 | case it's the name of one of the template's parameters. */ | |
5696 | DECL_NAME (r) = build_typename_overload (TREE_TYPE (type)); | |
5566b478 | 5697 | |
4393e105 MM |
5698 | DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args, |
5699 | /*complain=*/1, t); | |
17aec3eb | 5700 | DECL_TEMPLATE_RESULT (r) = NULL_TREE; |
711734a9 JM |
5701 | |
5702 | TREE_STATIC (r) = 0; | |
5703 | TREE_PUBLIC (r) = TREE_PUBLIC (t); | |
5704 | DECL_EXTERNAL (r) = 1; | |
5705 | DECL_INTERFACE_KNOWN (r) = 0; | |
5706 | DECL_DEFER_OUTPUT (r) = 0; | |
5707 | TREE_CHAIN (r) = NULL_TREE; | |
5708 | DECL_PENDING_INLINE_INFO (r) = 0; | |
59026e79 | 5709 | DECL_PENDING_INLINE_P (r) = 0; |
711734a9 | 5710 | TREE_USED (r) = 0; |
db9b2174 MM |
5711 | if (DECL_CLONED_FUNCTION (r)) |
5712 | { | |
5713 | DECL_CLONED_FUNCTION (r) = tsubst (DECL_CLONED_FUNCTION (t), | |
5714 | args, /*complain=*/1, t); | |
5715 | TREE_CHAIN (r) = TREE_CHAIN (DECL_CLONED_FUNCTION (r)); | |
5716 | TREE_CHAIN (DECL_CLONED_FUNCTION (r)) = r; | |
5717 | } | |
711734a9 | 5718 | |
36a117a5 MM |
5719 | /* Set up the DECL_TEMPLATE_INFO for R and compute its mangled |
5720 | name. There's no need to do this in the special friend | |
5721 | case mentioned above where GEN_TMPL is NULL. */ | |
5722 | if (gen_tmpl) | |
386b8a85 | 5723 | { |
36a117a5 | 5724 | DECL_TEMPLATE_INFO (r) |
e1b3e07d | 5725 | = tree_cons (gen_tmpl, argvec, NULL_TREE); |
36a117a5 MM |
5726 | SET_DECL_IMPLICIT_INSTANTIATION (r); |
5727 | register_specialization (r, gen_tmpl, argvec); | |
5728 | ||
5729 | /* Set the mangled name for R. */ | |
5730 | if (DECL_DESTRUCTOR_P (t)) | |
5731 | DECL_ASSEMBLER_NAME (r) = build_destructor_name (ctx); | |
5732 | else | |
386b8a85 | 5733 | { |
36a117a5 MM |
5734 | /* Instantiations of template functions must be mangled |
5735 | specially, in order to conform to 14.5.5.1 | |
5736 | [temp.over.link]. */ | |
5737 | tree tmpl = DECL_TI_TEMPLATE (t); | |
386b8a85 | 5738 | |
36a117a5 MM |
5739 | /* TMPL will be NULL if this is a specialization of a |
5740 | member function of a template class. */ | |
5741 | if (name_mangling_version < 1 | |
5742 | || tmpl == NULL_TREE | |
5743 | || (member && !is_member_template (tmpl) | |
5744 | && !DECL_TEMPLATE_INFO (tmpl))) | |
5745 | set_mangled_name_for_decl (r); | |
75650646 | 5746 | else |
36a117a5 | 5747 | set_mangled_name_for_template_decl (r); |
386b8a85 | 5748 | } |
36a117a5 MM |
5749 | |
5750 | DECL_RTL (r) = 0; | |
5751 | make_decl_rtl (r, NULL_PTR, 1); | |
5752 | ||
5753 | /* Like grokfndecl. If we don't do this, pushdecl will | |
5754 | mess up our TREE_CHAIN because it doesn't find a | |
5755 | previous decl. Sigh. */ | |
5756 | if (member | |
c36ae80f | 5757 | && ! uses_template_parms (r) |
36a117a5 MM |
5758 | && (IDENTIFIER_GLOBAL_VALUE (DECL_ASSEMBLER_NAME (r)) |
5759 | == NULL_TREE)) | |
5760 | SET_IDENTIFIER_GLOBAL_VALUE (DECL_ASSEMBLER_NAME (r), r); | |
9188c363 MM |
5761 | |
5762 | /* We're not supposed to instantiate default arguments | |
5763 | until they are called, for a template. But, for a | |
5764 | declaration like: | |
5765 | ||
5766 | template <class T> void f () | |
5767 | { extern void g(int i = T()); } | |
5768 | ||
5769 | we should do the substitution when the template is | |
5770 | instantiated. We handle the member function case in | |
5771 | instantiate_class_template since the default arguments | |
5772 | might refer to other members of the class. */ | |
5773 | if (!member | |
5774 | && !PRIMARY_TEMPLATE_P (gen_tmpl) | |
5775 | && !uses_template_parms (argvec)) | |
5776 | tsubst_default_arguments (r); | |
386b8a85 | 5777 | } |
f181d4ae | 5778 | |
cf38f48a MM |
5779 | /* Copy the list of befriending classes. */ |
5780 | for (friends = &DECL_BEFRIENDING_CLASSES (r); | |
5781 | *friends; | |
5782 | friends = &TREE_CHAIN (*friends)) | |
5783 | { | |
5784 | *friends = copy_node (*friends); | |
5785 | TREE_VALUE (*friends) = tsubst (TREE_VALUE (*friends), | |
5786 | args, /*complain=*/1, | |
5787 | in_decl); | |
5788 | } | |
5789 | ||
f181d4ae MM |
5790 | if (DECL_CONSTRUCTOR_P (r)) |
5791 | { | |
5792 | maybe_retrofit_in_chrg (r); | |
5793 | grok_ctor_properties (ctx, r); | |
5794 | } | |
f71f87f9 | 5795 | else if (DECL_OVERLOADED_OPERATOR_P (r)) |
f181d4ae | 5796 | grok_op_properties (r, DECL_VIRTUAL_P (r), DECL_FRIEND_P (r)); |
8d08fdba | 5797 | } |
ae58fa02 | 5798 | break; |
8d08fdba MS |
5799 | |
5800 | case PARM_DECL: | |
5801 | { | |
ae58fa02 | 5802 | r = copy_node (t); |
5566b478 | 5803 | TREE_TYPE (r) = type; |
1b8899d1 MM |
5804 | c_apply_type_quals_to_decl (CP_TYPE_QUALS (type), r); |
5805 | ||
db2767b6 MM |
5806 | if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX) |
5807 | DECL_INITIAL (r) = TREE_TYPE (r); | |
5808 | else | |
4393e105 MM |
5809 | DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args, |
5810 | /*complain=*/1, in_decl); | |
db2767b6 | 5811 | |
5566b478 | 5812 | DECL_CONTEXT (r) = NULL_TREE; |
fa56377d JJ |
5813 | if (PROMOTE_PROTOTYPES |
5814 | && (TREE_CODE (type) == INTEGER_TYPE | |
5815 | || TREE_CODE (type) == ENUMERAL_TYPE) | |
f83b0cb6 JM |
5816 | && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)) |
5817 | DECL_ARG_TYPE (r) = integer_type_node; | |
8d08fdba | 5818 | if (TREE_CHAIN (t)) |
4393e105 MM |
5819 | TREE_CHAIN (r) = tsubst (TREE_CHAIN (t), args, |
5820 | /*complain=*/1, TREE_CHAIN (t)); | |
8d08fdba | 5821 | } |
ae58fa02 | 5822 | break; |
8d08fdba | 5823 | |
5566b478 MS |
5824 | case FIELD_DECL: |
5825 | { | |
0acf7199 | 5826 | r = copy_decl (t); |
1b8899d1 MM |
5827 | TREE_TYPE (r) = type; |
5828 | c_apply_type_quals_to_decl (CP_TYPE_QUALS (type), r); | |
5829 | ||
5830 | /* We don't have to set DECL_CONTEXT here; it is set by | |
5831 | finish_member_declaration. */ | |
4393e105 MM |
5832 | DECL_INITIAL (r) = tsubst_expr (DECL_INITIAL (t), args, |
5833 | /*complain=*/1, in_decl); | |
5566b478 | 5834 | TREE_CHAIN (r) = NULL_TREE; |
4393e105 | 5835 | if (TREE_CODE (type) == VOID_TYPE) |
8251199e | 5836 | cp_error_at ("instantiation of `%D' as type void", r); |
5566b478 | 5837 | } |
ae58fa02 | 5838 | break; |
5566b478 MS |
5839 | |
5840 | case USING_DECL: | |
5841 | { | |
ae58fa02 | 5842 | r = copy_node (t); |
5566b478 | 5843 | DECL_INITIAL (r) |
4393e105 | 5844 | = tsubst_copy (DECL_INITIAL (t), args, /*complain=*/1, in_decl); |
5566b478 | 5845 | TREE_CHAIN (r) = NULL_TREE; |
5566b478 | 5846 | } |
ae58fa02 | 5847 | break; |
5566b478 | 5848 | |
9188c363 MM |
5849 | case TYPE_DECL: |
5850 | if (DECL_IMPLICIT_TYPEDEF_P (t)) | |
5851 | { | |
5852 | /* For an implicit typedef, we just want the implicit | |
5853 | typedef for the tsubst'd type. We've already got the | |
5854 | tsubst'd type, as TYPE, so we just need it's associated | |
5855 | declaration. */ | |
5856 | r = TYPE_NAME (type); | |
5857 | break; | |
5858 | } | |
6dfbb909 MM |
5859 | else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM |
5860 | || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM) | |
9188c363 MM |
5861 | { |
5862 | /* For a template type parameter, we don't have to do | |
5863 | anything special. */ | |
b9f39201 | 5864 | r = TYPE_NAME (type); |
9188c363 MM |
5865 | break; |
5866 | } | |
5867 | ||
5868 | /* Fall through. */ | |
5869 | ||
5566b478 MS |
5870 | case VAR_DECL: |
5871 | { | |
1cea0434 KG |
5872 | tree argvec = NULL_TREE; |
5873 | tree gen_tmpl = NULL_TREE; | |
36a117a5 | 5874 | tree spec; |
1cea0434 | 5875 | tree tmpl = NULL_TREE; |
9188c363 | 5876 | tree ctx; |
6dfbb909 | 5877 | int local_p; |
9188c363 | 5878 | |
6dfbb909 MM |
5879 | /* Assume this is a non-local variable. */ |
5880 | local_p = 0; | |
5566b478 | 5881 | |
de96bf57 | 5882 | if (TYPE_P (CP_DECL_CONTEXT (t))) |
9188c363 MM |
5883 | ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, |
5884 | /*complain=*/1, | |
5885 | in_decl, /*entering_scope=*/1); | |
5886 | else | |
6dfbb909 MM |
5887 | { |
5888 | /* Subsequent calls to pushdecl will fill this in. */ | |
5889 | ctx = NULL_TREE; | |
5890 | if (!DECL_NAMESPACE_SCOPE_P (t)) | |
5891 | local_p = 1; | |
5892 | } | |
9188c363 | 5893 | |
36a117a5 | 5894 | /* Check to see if we already have this specialization. */ |
6dfbb909 MM |
5895 | if (!local_p) |
5896 | { | |
5897 | tmpl = DECL_TI_TEMPLATE (t); | |
5898 | gen_tmpl = most_general_template (tmpl); | |
5899 | argvec = tsubst (DECL_TI_ARGS (t), args, /*complain=*/1, in_decl); | |
5900 | spec = retrieve_specialization (gen_tmpl, argvec); | |
5901 | } | |
9188c363 | 5902 | else |
6dfbb909 | 5903 | spec = retrieve_local_specialization (t); |
9188c363 | 5904 | |
36a117a5 | 5905 | if (spec) |
ae58fa02 MM |
5906 | { |
5907 | r = spec; | |
5908 | break; | |
5909 | } | |
5566b478 | 5910 | |
0acf7199 | 5911 | r = copy_decl (t); |
5566b478 | 5912 | TREE_TYPE (r) = type; |
1b8899d1 | 5913 | c_apply_type_quals_to_decl (CP_TYPE_QUALS (type), r); |
5566b478 | 5914 | DECL_CONTEXT (r) = ctx; |
d11ad92e MS |
5915 | |
5916 | /* Don't try to expand the initializer until someone tries to use | |
5917 | this variable; otherwise we run into circular dependencies. */ | |
5918 | DECL_INITIAL (r) = NULL_TREE; | |
5566b478 | 5919 | DECL_RTL (r) = 0; |
06ceef4e | 5920 | DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0; |
5566b478 | 5921 | |
f9817201 MM |
5922 | /* For __PRETTY_FUNCTION__ we have to adjust the initializer. */ |
5923 | if (DECL_PRETTY_FUNCTION_P (r)) | |
5924 | { | |
5925 | DECL_INITIAL (r) = tsubst (DECL_INITIAL (t), | |
5926 | args, | |
5927 | /*complain=*/1, | |
5928 | NULL_TREE); | |
5929 | TREE_TYPE (r) = TREE_TYPE (DECL_INITIAL (r)); | |
5930 | } | |
5931 | ||
e0942dcd MM |
5932 | /* Even if the original location is out of scope, the newly |
5933 | substituted one is not. */ | |
5934 | if (TREE_CODE (r) == VAR_DECL) | |
5935 | DECL_DEAD_FOR_LOCAL (r) = 0; | |
5936 | ||
6dfbb909 MM |
5937 | if (!local_p) |
5938 | { | |
5939 | /* A static data member declaration is always marked | |
5940 | external when it is declared in-class, even if an | |
5941 | initializer is present. We mimic the non-template | |
5942 | processing here. */ | |
5943 | DECL_EXTERNAL (r) = 1; | |
fa8d6e85 | 5944 | |
6dfbb909 MM |
5945 | register_specialization (r, gen_tmpl, argvec); |
5946 | DECL_TEMPLATE_INFO (r) = tree_cons (tmpl, argvec, NULL_TREE); | |
5947 | SET_DECL_IMPLICIT_INSTANTIATION (r); | |
5948 | } | |
9188c363 | 5949 | else |
6dfbb909 | 5950 | register_local_specialization (r, t); |
5566b478 | 5951 | |
5566b478 | 5952 | TREE_CHAIN (r) = NULL_TREE; |
9188c363 | 5953 | if (TREE_CODE (r) == VAR_DECL && TREE_CODE (type) == VOID_TYPE) |
8251199e | 5954 | cp_error_at ("instantiation of `%D' as type void", r); |
5566b478 | 5955 | } |
ae58fa02 | 5956 | break; |
5566b478 | 5957 | |
ae58fa02 MM |
5958 | default: |
5959 | my_friendly_abort (0); | |
5960 | } | |
5961 | ||
5962 | /* Restore the file and line information. */ | |
5963 | lineno = saved_lineno; | |
5964 | input_filename = saved_filename; | |
5965 | ||
5966 | return r; | |
5967 | } | |
5968 | ||
cabc336a MM |
5969 | /* Substitue into the ARG_TYPES of a function type. */ |
5970 | ||
e9659ab0 | 5971 | static tree |
4393e105 | 5972 | tsubst_arg_types (arg_types, args, complain, in_decl) |
cabc336a MM |
5973 | tree arg_types; |
5974 | tree args; | |
4393e105 | 5975 | int complain; |
cabc336a MM |
5976 | tree in_decl; |
5977 | { | |
5978 | tree remaining_arg_types; | |
cabc336a MM |
5979 | tree type; |
5980 | ||
5981 | if (!arg_types || arg_types == void_list_node) | |
5982 | return arg_types; | |
5983 | ||
5984 | remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types), | |
4393e105 MM |
5985 | args, complain, in_decl); |
5986 | if (remaining_arg_types == error_mark_node) | |
5987 | return error_mark_node; | |
5988 | ||
5989 | type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl); | |
5990 | if (type == error_mark_node) | |
5991 | return error_mark_node; | |
cabc336a | 5992 | |
4393e105 MM |
5993 | /* Do array-to-pointer, function-to-pointer conversion, and ignore |
5994 | top-level qualifiers as required. */ | |
5995 | type = TYPE_MAIN_VARIANT (type_decays_to (type)); | |
cabc336a MM |
5996 | |
5997 | /* Note that we do not substitute into default arguments here. The | |
5998 | standard mandates that they be instantiated only when needed, | |
5999 | which is done in build_over_call. */ | |
51632249 JM |
6000 | return hash_tree_cons (TREE_PURPOSE (arg_types), type, |
6001 | remaining_arg_types); | |
cabc336a MM |
6002 | |
6003 | } | |
6004 | ||
4393e105 MM |
6005 | /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does |
6006 | *not* handle the exception-specification for FNTYPE, because the | |
6007 | initial substitution of explicitly provided template parameters | |
6008 | during argument deduction forbids substitution into the | |
6009 | exception-specification: | |
6010 | ||
6011 | [temp.deduct] | |
6012 | ||
6013 | All references in the function type of the function template to the | |
6014 | corresponding template parameters are replaced by the specified tem- | |
6015 | plate argument values. If a substitution in a template parameter or | |
6016 | in the function type of the function template results in an invalid | |
6017 | type, type deduction fails. [Note: The equivalent substitution in | |
6018 | exception specifications is done only when the function is instanti- | |
6019 | ated, at which point a program is ill-formed if the substitution | |
6020 | results in an invalid type.] */ | |
6021 | ||
6022 | static tree | |
6023 | tsubst_function_type (t, args, complain, in_decl) | |
6024 | tree t; | |
6025 | tree args; | |
6026 | int complain; | |
6027 | tree in_decl; | |
6028 | { | |
6029 | tree return_type; | |
6030 | tree arg_types; | |
6031 | tree fntype; | |
6032 | ||
8dd3f57a | 6033 | /* The TYPE_CONTEXT is not used for function/method types. */ |
4393e105 MM |
6034 | my_friendly_assert (TYPE_CONTEXT (t) == NULL_TREE, 0); |
6035 | ||
6036 | /* Substitue the return type. */ | |
6037 | return_type = tsubst (TREE_TYPE (t), args, complain, in_decl); | |
6038 | if (return_type == error_mark_node) | |
6039 | return error_mark_node; | |
6040 | ||
6041 | /* Substitue the argument types. */ | |
6042 | arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, | |
6043 | complain, in_decl); | |
6044 | if (arg_types == error_mark_node) | |
6045 | return error_mark_node; | |
6046 | ||
6047 | /* Construct a new type node and return it. */ | |
6048 | if (TREE_CODE (t) == FUNCTION_TYPE) | |
6049 | fntype = build_function_type (return_type, arg_types); | |
6050 | else | |
6051 | { | |
6052 | tree r = TREE_TYPE (TREE_VALUE (arg_types)); | |
6053 | if (! IS_AGGR_TYPE (r)) | |
6054 | { | |
6055 | /* [temp.deduct] | |
6056 | ||
6057 | Type deduction may fail for any of the following | |
6058 | reasons: | |
6059 | ||
6060 | -- Attempting to create "pointer to member of T" when T | |
6061 | is not a class type. */ | |
6062 | if (complain) | |
6063 | cp_error ("creating pointer to member function of non-class type `%T'", | |
6064 | r); | |
6065 | return error_mark_node; | |
6066 | } | |
6067 | ||
6068 | fntype = build_cplus_method_type (r, return_type, TREE_CHAIN | |
6069 | (arg_types)); | |
6070 | } | |
6071 | fntype = build_qualified_type (fntype, TYPE_QUALS (t)); | |
ada846eb | 6072 | fntype = build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t)); |
4393e105 MM |
6073 | |
6074 | return fntype; | |
6075 | } | |
6076 | ||
297e73d8 MM |
6077 | /* Substitute into the PARMS of a call-declarator. */ |
6078 | ||
e9659ab0 | 6079 | static tree |
4393e105 | 6080 | tsubst_call_declarator_parms (parms, args, complain, in_decl) |
297e73d8 MM |
6081 | tree parms; |
6082 | tree args; | |
4393e105 | 6083 | int complain; |
297e73d8 MM |
6084 | tree in_decl; |
6085 | { | |
6086 | tree new_parms; | |
6087 | tree type; | |
6088 | tree defarg; | |
6089 | ||
6090 | if (!parms || parms == void_list_node) | |
6091 | return parms; | |
6092 | ||
6093 | new_parms = tsubst_call_declarator_parms (TREE_CHAIN (parms), | |
4393e105 | 6094 | args, complain, in_decl); |
297e73d8 MM |
6095 | |
6096 | /* Figure out the type of this parameter. */ | |
4393e105 | 6097 | type = tsubst (TREE_VALUE (parms), args, complain, in_decl); |
297e73d8 MM |
6098 | |
6099 | /* Figure out the default argument as well. Note that we use | |
4393e105 MM |
6100 | tsubst_expr since the default argument is really an expression. */ |
6101 | defarg = tsubst_expr (TREE_PURPOSE (parms), args, complain, in_decl); | |
297e73d8 MM |
6102 | |
6103 | /* Chain this parameter on to the front of those we have already | |
6104 | processed. We don't use hash_tree_cons because that function | |
6105 | doesn't check TREE_PARMLIST. */ | |
6106 | new_parms = tree_cons (defarg, type, new_parms); | |
6107 | ||
6108 | /* And note that these are parameters. */ | |
6109 | TREE_PARMLIST (new_parms) = 1; | |
6110 | ||
6111 | return new_parms; | |
6112 | } | |
6113 | ||
4393e105 MM |
6114 | /* Take the tree structure T and replace template parameters used |
6115 | therein with the argument vector ARGS. IN_DECL is an associated | |
6116 | decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE. | |
6117 | An appropriate error message is issued only if COMPLAIN is | |
6118 | non-zero. Note that we must be relatively non-tolerant of | |
6119 | extensions here, in order to preserve conformance; if we allow | |
6120 | substitutions that should not be allowed, we may allow argument | |
6121 | deductions that should not succeed, and therefore report ambiguous | |
6122 | overload situations where there are none. In theory, we could | |
6123 | allow the substitution, but indicate that it should have failed, | |
6124 | and allow our caller to make sure that the right thing happens, but | |
6125 | we don't try to do this yet. | |
6126 | ||
6127 | This function is used for dealing with types, decls and the like; | |
6128 | for expressions, use tsubst_expr or tsubst_copy. */ | |
ae58fa02 MM |
6129 | |
6130 | tree | |
4393e105 | 6131 | tsubst (t, args, complain, in_decl) |
ae58fa02 | 6132 | tree t, args; |
4393e105 | 6133 | int complain; |
ae58fa02 MM |
6134 | tree in_decl; |
6135 | { | |
0ecfe0b4 | 6136 | tree type, r; |
ae58fa02 MM |
6137 | |
6138 | if (t == NULL_TREE || t == error_mark_node | |
6139 | || t == integer_type_node | |
6140 | || t == void_type_node | |
6141 | || t == char_type_node | |
6142 | || TREE_CODE (t) == NAMESPACE_DECL) | |
6143 | return t; | |
6144 | ||
6145 | if (TREE_CODE (t) == IDENTIFIER_NODE) | |
6146 | type = IDENTIFIER_TYPE_VALUE (t); | |
6147 | else | |
6148 | type = TREE_TYPE (t); | |
6149 | if (type == unknown_type_node) | |
6150 | my_friendly_abort (42); | |
6151 | ||
6152 | if (type && TREE_CODE (t) != FUNCTION_DECL | |
6153 | && TREE_CODE (t) != TYPENAME_TYPE | |
6154 | && TREE_CODE (t) != TEMPLATE_DECL | |
4393e105 MM |
6155 | && TREE_CODE (t) != IDENTIFIER_NODE |
6156 | && TREE_CODE (t) != FUNCTION_TYPE | |
6157 | && TREE_CODE (t) != METHOD_TYPE) | |
6158 | type = tsubst (type, args, complain, in_decl); | |
6159 | if (type == error_mark_node) | |
6160 | return error_mark_node; | |
ae58fa02 | 6161 | |
2f939d94 | 6162 | if (DECL_P (t)) |
ae58fa02 MM |
6163 | return tsubst_decl (t, args, type, in_decl); |
6164 | ||
6165 | switch (TREE_CODE (t)) | |
6166 | { | |
6167 | case RECORD_TYPE: | |
6168 | case UNION_TYPE: | |
6169 | case ENUMERAL_TYPE: | |
4393e105 MM |
6170 | return tsubst_aggr_type (t, args, complain, in_decl, |
6171 | /*entering_scope=*/0); | |
ae58fa02 MM |
6172 | |
6173 | case ERROR_MARK: | |
6174 | case IDENTIFIER_NODE: | |
6175 | case OP_IDENTIFIER: | |
6176 | case VOID_TYPE: | |
6177 | case REAL_TYPE: | |
6178 | case COMPLEX_TYPE: | |
6179 | case BOOLEAN_TYPE: | |
6180 | case INTEGER_CST: | |
6181 | case REAL_CST: | |
6182 | case STRING_CST: | |
6183 | return t; | |
6184 | ||
6185 | case INTEGER_TYPE: | |
6186 | if (t == integer_type_node) | |
6187 | return t; | |
6188 | ||
6189 | if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST | |
6190 | && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST) | |
6191 | return t; | |
d2e5ee5c | 6192 | |
5566b478 | 6193 | { |
ddce3528 | 6194 | tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0); |
7ddedda4 | 6195 | |
ddce3528 | 6196 | max = tsubst_expr (omax, args, complain, in_decl); |
4393e105 MM |
6197 | if (max == error_mark_node) |
6198 | return error_mark_node; | |
6199 | ||
8dd3f57a MM |
6200 | /* See if we can reduce this expression to something simpler. */ |
6201 | max = maybe_fold_nontype_arg (max); | |
fc611ce0 | 6202 | if (!processing_template_decl) |
8dd3f57a MM |
6203 | max = decl_constant_value (max); |
6204 | ||
856216bb MM |
6205 | if (processing_template_decl |
6206 | /* When providing explicit arguments to a template | |
6207 | function, but leaving some arguments for subsequent | |
6208 | deduction, MAX may be template-dependent even if we're | |
c95cd22e JM |
6209 | not PROCESSING_TEMPLATE_DECL. We still need to check for |
6210 | template parms, though; MAX won't be an INTEGER_CST for | |
6211 | dynamic arrays, either. */ | |
6212 | || (TREE_CODE (max) != INTEGER_CST | |
6213 | && uses_template_parms (max))) | |
ae58fa02 | 6214 | { |
8dd3f57a MM |
6215 | tree itype = make_node (INTEGER_TYPE); |
6216 | TYPE_MIN_VALUE (itype) = size_zero_node; | |
6217 | TYPE_MAX_VALUE (itype) = build_min (MINUS_EXPR, sizetype, max, | |
6218 | integer_one_node); | |
6219 | return itype; | |
ae58fa02 MM |
6220 | } |
6221 | ||
ddce3528 JM |
6222 | if (integer_zerop (omax)) |
6223 | { | |
6224 | /* Still allow an explicit array of size zero. */ | |
6225 | if (pedantic) | |
6226 | pedwarn ("creating array with size zero"); | |
6227 | } | |
07c88314 MM |
6228 | else if (integer_zerop (max) |
6229 | || (TREE_CODE (max) == INTEGER_CST | |
6230 | && INT_CST_LT (max, integer_zero_node))) | |
0ecfe0b4 | 6231 | { |
4393e105 MM |
6232 | /* [temp.deduct] |
6233 | ||
6234 | Type deduction may fail for any of the following | |
6235 | reasons: | |
6236 | ||
bf8f3f93 MM |
6237 | Attempting to create an array with a size that is |
6238 | zero or negative. */ | |
4393e105 | 6239 | if (complain) |
16bb3387 | 6240 | cp_error ("creating array with size zero (`%E')", max); |
4393e105 MM |
6241 | |
6242 | return error_mark_node; | |
0ecfe0b4 JM |
6243 | } |
6244 | ||
c95cd22e | 6245 | return compute_array_index_type (NULL_TREE, max); |
ae58fa02 MM |
6246 | } |
6247 | ||
6248 | case TEMPLATE_TYPE_PARM: | |
6249 | case TEMPLATE_TEMPLATE_PARM: | |
6250 | case TEMPLATE_PARM_INDEX: | |
6251 | { | |
6252 | int idx; | |
6253 | int level; | |
6254 | int levels; | |
0ecfe0b4 JM |
6255 | |
6256 | r = NULL_TREE; | |
ae58fa02 MM |
6257 | |
6258 | if (TREE_CODE (t) == TEMPLATE_TYPE_PARM | |
6259 | || TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM) | |
6260 | { | |
6261 | idx = TEMPLATE_TYPE_IDX (t); | |
6262 | level = TEMPLATE_TYPE_LEVEL (t); | |
6263 | } | |
6264 | else | |
6265 | { | |
6266 | idx = TEMPLATE_PARM_IDX (t); | |
6267 | level = TEMPLATE_PARM_LEVEL (t); | |
6268 | } | |
6269 | ||
6270 | if (TREE_VEC_LENGTH (args) > 0) | |
6271 | { | |
6272 | tree arg = NULL_TREE; | |
6273 | ||
6274 | levels = TMPL_ARGS_DEPTH (args); | |
6275 | if (level <= levels) | |
6276 | arg = TMPL_ARG (args, level, idx); | |
6277 | ||
8b5b8b7c MM |
6278 | if (arg == error_mark_node) |
6279 | return error_mark_node; | |
6280 | else if (arg != NULL_TREE) | |
ae58fa02 MM |
6281 | { |
6282 | if (TREE_CODE (t) == TEMPLATE_TYPE_PARM) | |
6283 | { | |
2f939d94 | 6284 | my_friendly_assert (TYPE_P (arg), 0); |
adecb3f4 MM |
6285 | return cp_build_qualified_type_real |
6286 | (arg, CP_TYPE_QUALS (arg) | CP_TYPE_QUALS (t), | |
6287 | complain); | |
ae58fa02 MM |
6288 | } |
6289 | else if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM) | |
6290 | { | |
7ddedda4 | 6291 | if (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t)) |
ae58fa02 MM |
6292 | { |
6293 | /* We are processing a type constructed from | |
6294 | a template template parameter */ | |
7ddedda4 | 6295 | tree argvec = tsubst (TYPE_TI_ARGS (t), |
4393e105 MM |
6296 | args, complain, in_decl); |
6297 | if (argvec == error_mark_node) | |
6298 | return error_mark_node; | |
6299 | ||
ae58fa02 MM |
6300 | /* We can get a TEMPLATE_TEMPLATE_PARM here when |
6301 | we are resolving nested-types in the signature of | |
6302 | a member function templates. | |
6303 | Otherwise ARG is a TEMPLATE_DECL and is the real | |
6304 | template to be instantiated. */ | |
6305 | if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM) | |
6306 | arg = TYPE_NAME (arg); | |
6307 | ||
6308 | r = lookup_template_class (DECL_NAME (arg), | |
6309 | argvec, in_decl, | |
6310 | DECL_CONTEXT (arg), | |
6311 | /*entering_scope=*/0); | |
adecb3f4 MM |
6312 | return cp_build_qualified_type_real (r, |
6313 | TYPE_QUALS (t), | |
6314 | complain); | |
ae58fa02 MM |
6315 | } |
6316 | else | |
6317 | /* We are processing a template argument list. */ | |
6318 | return arg; | |
6319 | } | |
6320 | else | |
6321 | return arg; | |
6322 | } | |
6323 | } | |
51924768 JM |
6324 | else |
6325 | my_friendly_abort (981018); | |
ae58fa02 MM |
6326 | |
6327 | if (level == 1) | |
6328 | /* This can happen during the attempted tsubst'ing in | |
6329 | unify. This means that we don't yet have any information | |
6330 | about the template parameter in question. */ | |
6331 | return t; | |
6332 | ||
6333 | /* If we get here, we must have been looking at a parm for a | |
6334 | more deeply nested template. Make a new version of this | |
6335 | template parameter, but with a lower level. */ | |
6336 | switch (TREE_CODE (t)) | |
6337 | { | |
6338 | case TEMPLATE_TYPE_PARM: | |
6339 | case TEMPLATE_TEMPLATE_PARM: | |
6340 | r = copy_node (t); | |
6341 | TEMPLATE_TYPE_PARM_INDEX (r) | |
6342 | = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t), | |
6343 | r, levels); | |
6344 | TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r); | |
6345 | TYPE_MAIN_VARIANT (r) = r; | |
6346 | TYPE_POINTER_TO (r) = NULL_TREE; | |
6347 | TYPE_REFERENCE_TO (r) = NULL_TREE; | |
6348 | ||
6349 | if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM | |
7ddedda4 | 6350 | && TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t)) |
ae58fa02 | 6351 | { |
4393e105 MM |
6352 | tree argvec = tsubst (TYPE_TI_ARGS (t), args, |
6353 | complain, in_decl); | |
6354 | if (argvec == error_mark_node) | |
6355 | return error_mark_node; | |
6356 | ||
7ddedda4 | 6357 | TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r) |
e1b3e07d | 6358 | = tree_cons (TYPE_NAME (t), argvec, NULL_TREE); |
ae58fa02 MM |
6359 | } |
6360 | break; | |
6361 | ||
6362 | case TEMPLATE_PARM_INDEX: | |
6363 | r = reduce_template_parm_level (t, type, levels); | |
6364 | break; | |
6365 | ||
6366 | default: | |
6367 | my_friendly_abort (0); | |
6368 | } | |
6369 | ||
5566b478 | 6370 | return r; |
ae58fa02 | 6371 | } |
5566b478 | 6372 | |
8d08fdba MS |
6373 | case TREE_LIST: |
6374 | { | |
6375 | tree purpose, value, chain, result; | |
8d08fdba MS |
6376 | |
6377 | if (t == void_list_node) | |
6378 | return t; | |
6379 | ||
8d08fdba MS |
6380 | purpose = TREE_PURPOSE (t); |
6381 | if (purpose) | |
4393e105 MM |
6382 | { |
6383 | purpose = tsubst (purpose, args, complain, in_decl); | |
6384 | if (purpose == error_mark_node) | |
6385 | return error_mark_node; | |
6386 | } | |
8d08fdba MS |
6387 | value = TREE_VALUE (t); |
6388 | if (value) | |
4393e105 MM |
6389 | { |
6390 | value = tsubst (value, args, complain, in_decl); | |
6391 | if (value == error_mark_node) | |
6392 | return error_mark_node; | |
6393 | } | |
8d08fdba MS |
6394 | chain = TREE_CHAIN (t); |
6395 | if (chain && chain != void_type_node) | |
4393e105 MM |
6396 | { |
6397 | chain = tsubst (chain, args, complain, in_decl); | |
6398 | if (chain == error_mark_node) | |
6399 | return error_mark_node; | |
6400 | } | |
8d08fdba MS |
6401 | if (purpose == TREE_PURPOSE (t) |
6402 | && value == TREE_VALUE (t) | |
6403 | && chain == TREE_CHAIN (t)) | |
6404 | return t; | |
51632249 | 6405 | result = hash_tree_cons (purpose, value, chain); |
8d08fdba MS |
6406 | TREE_PARMLIST (result) = TREE_PARMLIST (t); |
6407 | return result; | |
6408 | } | |
6409 | case TREE_VEC: | |
5566b478 MS |
6410 | if (type != NULL_TREE) |
6411 | { | |
1ceaad38 JM |
6412 | /* A binfo node. We always need to make a copy, of the node |
6413 | itself and of its BINFO_BASETYPES. */ | |
85b71cf2 | 6414 | |
5566b478 MS |
6415 | t = copy_node (t); |
6416 | ||
c7449f2b JM |
6417 | /* Make sure type isn't a typedef copy. */ |
6418 | type = BINFO_TYPE (TYPE_BINFO (type)); | |
6419 | ||
5566b478 | 6420 | TREE_TYPE (t) = complete_type (type); |
6633d636 MS |
6421 | if (IS_AGGR_TYPE (type)) |
6422 | { | |
6423 | BINFO_VTABLE (t) = TYPE_BINFO_VTABLE (type); | |
6424 | BINFO_VIRTUALS (t) = TYPE_BINFO_VIRTUALS (type); | |
6425 | if (TYPE_BINFO_BASETYPES (type) != NULL_TREE) | |
6426 | BINFO_BASETYPES (t) = copy_node (TYPE_BINFO_BASETYPES (type)); | |
6427 | } | |
5566b478 MS |
6428 | return t; |
6429 | } | |
85b71cf2 JM |
6430 | |
6431 | /* Otherwise, a vector of template arguments. */ | |
4393e105 | 6432 | return tsubst_template_arg_vector (t, args, complain); |
8d08fdba | 6433 | |
8d08fdba MS |
6434 | case POINTER_TYPE: |
6435 | case REFERENCE_TYPE: | |
6436 | { | |
8d08fdba | 6437 | enum tree_code code; |
79a7c7fa | 6438 | |
8d08fdba MS |
6439 | if (type == TREE_TYPE (t)) |
6440 | return t; | |
6441 | ||
6442 | code = TREE_CODE (t); | |
4393e105 MM |
6443 | |
6444 | ||
6445 | /* [temp.deduct] | |
6446 | ||
6447 | Type deduction may fail for any of the following | |
6448 | reasons: | |
6449 | ||
6450 | -- Attempting to create a pointer to reference type. | |
6451 | -- Attempting to create a reference to a reference type or | |
6452 | a reference to void. */ | |
0ecfe0b4 JM |
6453 | if (TREE_CODE (type) == REFERENCE_TYPE |
6454 | || (code == REFERENCE_TYPE && TREE_CODE (type) == VOID_TYPE)) | |
79a7c7fa JM |
6455 | { |
6456 | static int last_line = 0; | |
6457 | static char* last_file = 0; | |
6458 | ||
6459 | /* We keep track of the last time we issued this error | |
6460 | message to avoid spewing a ton of messages during a | |
6461 | single bad template instantiation. */ | |
4393e105 MM |
6462 | if (complain && (last_line != lineno || |
6463 | last_file != input_filename)) | |
79a7c7fa | 6464 | { |
0ecfe0b4 JM |
6465 | if (TREE_CODE (type) == VOID_TYPE) |
6466 | cp_error ("forming reference to void"); | |
6467 | else | |
6468 | cp_error ("forming %s to reference type `%T'", | |
6469 | (code == POINTER_TYPE) ? "pointer" : "reference", | |
6470 | type); | |
79a7c7fa JM |
6471 | last_line = lineno; |
6472 | last_file = input_filename; | |
6473 | } | |
6474 | ||
4393e105 | 6475 | return error_mark_node; |
79a7c7fa JM |
6476 | } |
6477 | else if (code == POINTER_TYPE) | |
8d08fdba MS |
6478 | r = build_pointer_type (type); |
6479 | else | |
6480 | r = build_reference_type (type); | |
adecb3f4 | 6481 | r = cp_build_qualified_type_real (r, TYPE_QUALS (t), complain); |
79a7c7fa | 6482 | |
8d08fdba MS |
6483 | /* Will this ever be needed for TYPE_..._TO values? */ |
6484 | layout_type (r); | |
6485 | return r; | |
6486 | } | |
a4443a08 | 6487 | case OFFSET_TYPE: |
0ecfe0b4 | 6488 | { |
4393e105 MM |
6489 | r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl); |
6490 | if (r == error_mark_node || !IS_AGGR_TYPE (r)) | |
6491 | { | |
6492 | /* [temp.deduct] | |
6493 | ||
6494 | Type deduction may fail for any of the following | |
6495 | reasons: | |
6496 | ||
6497 | -- Attempting to create "pointer to member of T" when T | |
6498 | is not a class type. */ | |
6499 | if (complain) | |
6500 | cp_error ("creating pointer to member of non-class type `%T'", | |
6501 | r); | |
6502 | return error_mark_node; | |
6503 | } | |
0ecfe0b4 JM |
6504 | return build_offset_type (r, type); |
6505 | } | |
8d08fdba MS |
6506 | case FUNCTION_TYPE: |
6507 | case METHOD_TYPE: | |
6508 | { | |
c11b6f21 | 6509 | tree fntype; |
4393e105 | 6510 | tree raises; |
8d08fdba | 6511 | |
4393e105 MM |
6512 | fntype = tsubst_function_type (t, args, complain, in_decl); |
6513 | if (fntype == error_mark_node) | |
6514 | return error_mark_node; | |
cabc336a MM |
6515 | |
6516 | /* Substitue the exception specification. */ | |
6517 | raises = TYPE_RAISES_EXCEPTIONS (t); | |
c11b6f21 MS |
6518 | if (raises) |
6519 | { | |
4cc1d462 NS |
6520 | tree list = NULL_TREE; |
6521 | ||
6522 | if (! TREE_VALUE (raises)) | |
6523 | list = raises; | |
6524 | else | |
6525 | for (; raises != NULL_TREE; raises = TREE_CHAIN (raises)) | |
6526 | { | |
6527 | tree spec = TREE_VALUE (raises); | |
6528 | ||
6529 | spec = tsubst (spec, args, complain, in_decl); | |
6530 | if (spec == error_mark_node) | |
6531 | return spec; | |
6532 | list = add_exception_specifier (list, spec, complain); | |
6533 | } | |
6534 | fntype = build_exception_variant (fntype, list); | |
c11b6f21 MS |
6535 | } |
6536 | return fntype; | |
8d08fdba MS |
6537 | } |
6538 | case ARRAY_TYPE: | |
6539 | { | |
4393e105 MM |
6540 | tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl); |
6541 | if (domain == error_mark_node) | |
6542 | return error_mark_node; | |
6543 | ||
6544 | /* As an optimization, we avoid regenerating the array type if | |
6545 | it will obviously be the same as T. */ | |
8d08fdba MS |
6546 | if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t)) |
6547 | return t; | |
0ecfe0b4 | 6548 | |
4393e105 MM |
6549 | /* These checks should match the ones in grokdeclarator. |
6550 | ||
6551 | [temp.deduct] | |
6552 | ||
6553 | The deduction may fail for any of the following reasons: | |
6554 | ||
6555 | -- Attempting to create an array with an element type that | |
6556 | is void, a function type, or a reference type. */ | |
6557 | if (TREE_CODE (type) == VOID_TYPE | |
6558 | || TREE_CODE (type) == FUNCTION_TYPE | |
6559 | || TREE_CODE (type) == REFERENCE_TYPE) | |
0ecfe0b4 | 6560 | { |
4393e105 MM |
6561 | if (complain) |
6562 | cp_error ("creating array of `%T'", type); | |
6563 | return error_mark_node; | |
0ecfe0b4 JM |
6564 | } |
6565 | ||
8d08fdba MS |
6566 | r = build_cplus_array_type (type, domain); |
6567 | return r; | |
6568 | } | |
6569 | ||
8d08fdba | 6570 | case PLUS_EXPR: |
5566b478 | 6571 | case MINUS_EXPR: |
4393e105 MM |
6572 | { |
6573 | tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, | |
6574 | in_decl); | |
6575 | tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, | |
6576 | in_decl); | |
6577 | ||
6578 | if (e1 == error_mark_node || e2 == error_mark_node) | |
6579 | return error_mark_node; | |
6580 | ||
6581 | return fold (build (TREE_CODE (t), TREE_TYPE (t), e1, e2)); | |
6582 | } | |
8d08fdba MS |
6583 | |
6584 | case NEGATE_EXPR: | |
6585 | case NOP_EXPR: | |
4393e105 MM |
6586 | { |
6587 | tree e = tsubst (TREE_OPERAND (t, 0), args, complain, | |
6588 | in_decl); | |
6589 | if (e == error_mark_node) | |
6590 | return error_mark_node; | |
6591 | ||
6592 | return fold (build (TREE_CODE (t), TREE_TYPE (t), e)); | |
6593 | } | |
8d08fdba | 6594 | |
5566b478 MS |
6595 | case TYPENAME_TYPE: |
6596 | { | |
4393e105 MM |
6597 | tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain, |
6598 | in_decl, /*entering_scope=*/1); | |
6599 | tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args, | |
6600 | complain, in_decl); | |
6601 | ||
6602 | if (ctx == error_mark_node || f == error_mark_node) | |
6603 | return error_mark_node; | |
ae58fa02 | 6604 | |
bf8f3f93 MM |
6605 | if (!IS_AGGR_TYPE (ctx)) |
6606 | { | |
6607 | if (complain) | |
6608 | cp_error ("`%T' is not a class, struct, or union type", | |
6609 | ctx); | |
6610 | return error_mark_node; | |
6611 | } | |
6612 | else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx)) | |
6613 | { | |
6614 | /* Normally, make_typename_type does not require that the CTX | |
6615 | have complete type in order to allow things like: | |
ae58fa02 | 6616 | |
bf8f3f93 | 6617 | template <class T> struct S { typename S<T>::X Y; }; |
ae58fa02 | 6618 | |
bf8f3f93 MM |
6619 | But, such constructs have already been resolved by this |
6620 | point, so here CTX really should have complete type, unless | |
6621 | it's a partial instantiation. */ | |
4393e105 | 6622 | ctx = complete_type (ctx); |
d0f062fb | 6623 | if (!COMPLETE_TYPE_P (ctx)) |
4393e105 MM |
6624 | { |
6625 | if (complain) | |
6626 | incomplete_type_error (NULL_TREE, ctx); | |
6627 | return error_mark_node; | |
6628 | } | |
6629 | } | |
ae58fa02 | 6630 | |
3baa501d | 6631 | f = make_typename_type (ctx, f, complain); |
f0bcd168 MM |
6632 | if (f == error_mark_node) |
6633 | return f; | |
adecb3f4 MM |
6634 | return cp_build_qualified_type_real (f, |
6635 | CP_TYPE_QUALS (f) | |
6636 | | CP_TYPE_QUALS (t), | |
6637 | complain); | |
5566b478 MS |
6638 | } |
6639 | ||
6640 | case INDIRECT_REF: | |
4393e105 MM |
6641 | { |
6642 | tree e = tsubst (TREE_OPERAND (t, 0), args, complain, | |
6643 | in_decl); | |
6644 | if (e == error_mark_node) | |
6645 | return error_mark_node; | |
6646 | return make_pointer_declarator (type, e); | |
6647 | } | |
6648 | ||
5566b478 | 6649 | case ADDR_EXPR: |
4393e105 MM |
6650 | { |
6651 | tree e = tsubst (TREE_OPERAND (t, 0), args, complain, | |
6652 | in_decl); | |
6653 | if (e == error_mark_node) | |
6654 | return error_mark_node; | |
6655 | return make_reference_declarator (type, e); | |
6656 | } | |
5566b478 MS |
6657 | |
6658 | case ARRAY_REF: | |
4393e105 MM |
6659 | { |
6660 | tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, | |
6661 | in_decl); | |
6fb2c05a JM |
6662 | tree e2 = tsubst_expr (TREE_OPERAND (t, 1), args, complain, |
6663 | in_decl); | |
4393e105 MM |
6664 | if (e1 == error_mark_node || e2 == error_mark_node) |
6665 | return error_mark_node; | |
6666 | ||
6667 | return build_parse_node (ARRAY_REF, e1, e2, tsubst_expr); | |
6668 | } | |
5566b478 MS |
6669 | |
6670 | case CALL_EXPR: | |
4393e105 MM |
6671 | { |
6672 | tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, | |
6673 | in_decl); | |
43f887f9 MM |
6674 | tree e2 = (tsubst_call_declarator_parms |
6675 | (CALL_DECLARATOR_PARMS (t), args, complain, in_decl)); | |
6676 | tree e3 = tsubst (CALL_DECLARATOR_EXCEPTION_SPEC (t), args, | |
6677 | complain, in_decl); | |
4393e105 MM |
6678 | |
6679 | if (e1 == error_mark_node || e2 == error_mark_node | |
6680 | || e3 == error_mark_node) | |
6681 | return error_mark_node; | |
6682 | ||
43f887f9 | 6683 | return make_call_declarator (e1, e2, CALL_DECLARATOR_QUALS (t), e3); |
4393e105 | 6684 | } |
5566b478 | 6685 | |
fc378698 | 6686 | case SCOPE_REF: |
4393e105 MM |
6687 | { |
6688 | tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, | |
6689 | in_decl); | |
6690 | tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl); | |
6691 | if (e1 == error_mark_node || e2 == error_mark_node) | |
6692 | return error_mark_node; | |
6693 | ||
6694 | return build_parse_node (TREE_CODE (t), e1, e2); | |
6695 | } | |
fc378698 | 6696 | |
b894fc05 | 6697 | case TYPEOF_TYPE: |
4393e105 MM |
6698 | { |
6699 | tree e1 = tsubst_expr (TYPE_FIELDS (t), args, complain, | |
6700 | in_decl); | |
6701 | if (e1 == error_mark_node) | |
6702 | return error_mark_node; | |
6703 | ||
6704 | return TREE_TYPE (e1); | |
6705 | } | |
b894fc05 | 6706 | |
f9817201 MM |
6707 | case FUNCTION_NAME: |
6708 | { | |
6709 | const char *name; | |
6710 | int len; | |
6711 | tree type; | |
6712 | tree str; | |
6713 | ||
6714 | /* This code should match declare_hidden_char_array in | |
6715 | c-common.c. */ | |
6716 | name = (*decl_printable_name) (current_function_decl, 2); | |
6717 | len = strlen (name) + 1; | |
6718 | type = build_array_type (char_type_node, | |
6719 | build_index_type (build_int_2 (len, 0))); | |
6720 | str = build_string (len, name); | |
6721 | TREE_TYPE (str) = type; | |
6722 | return str; | |
6723 | } | |
6724 | ||
8d08fdba | 6725 | default: |
5566b478 | 6726 | sorry ("use of `%s' in template", |
8d08fdba MS |
6727 | tree_code_name [(int) TREE_CODE (t)]); |
6728 | return error_mark_node; | |
6729 | } | |
6730 | } | |
6731 | ||
00d3396f JM |
6732 | /* Like tsubst, but deals with expressions. This function just replaces |
6733 | template parms; to finish processing the resultant expression, use | |
6734 | tsubst_expr. */ | |
6735 | ||
5566b478 | 6736 | tree |
4393e105 | 6737 | tsubst_copy (t, args, complain, in_decl) |
98c1c668 | 6738 | tree t, args; |
4393e105 | 6739 | int complain; |
5566b478 MS |
6740 | tree in_decl; |
6741 | { | |
6742 | enum tree_code code; | |
8452b1d3 | 6743 | tree r; |
8d08fdba | 6744 | |
5566b478 MS |
6745 | if (t == NULL_TREE || t == error_mark_node) |
6746 | return t; | |
6747 | ||
6748 | code = TREE_CODE (t); | |
b7484fbe | 6749 | |
5566b478 MS |
6750 | switch (code) |
6751 | { | |
6752 | case PARM_DECL: | |
a759e627 | 6753 | return do_identifier (DECL_NAME (t), 0, NULL_TREE); |
5566b478 MS |
6754 | |
6755 | case CONST_DECL: | |
ed44da02 MM |
6756 | { |
6757 | tree enum_type; | |
6758 | tree v; | |
6759 | ||
6760 | if (!DECL_CONTEXT (t)) | |
6761 | /* This is a global enumeration constant. */ | |
6762 | return t; | |
6763 | ||
6764 | /* Unfortunately, we cannot just call lookup_name here. | |
9188c363 MM |
6765 | Consider: |
6766 | ||
6767 | template <int I> int f() { | |
6768 | enum E { a = I }; | |
6769 | struct S { void g() { E e = a; } }; | |
6770 | }; | |
6771 | ||
6772 | When we instantiate f<7>::S::g(), say, lookup_name is not | |
6773 | clever enough to find f<7>::a. */ | |
ed44da02 | 6774 | enum_type |
4393e105 | 6775 | = tsubst_aggr_type (TREE_TYPE (t), args, complain, in_decl, |
ed44da02 MM |
6776 | /*entering_scope=*/0); |
6777 | ||
6778 | for (v = TYPE_VALUES (enum_type); | |
6779 | v != NULL_TREE; | |
6780 | v = TREE_CHAIN (v)) | |
6781 | if (TREE_PURPOSE (v) == DECL_NAME (t)) | |
6782 | return TREE_VALUE (v); | |
6783 | ||
6784 | /* We didn't find the name. That should never happen; if | |
6785 | name-lookup found it during preliminary parsing, we | |
6786 | should find it again here during instantiation. */ | |
6787 | my_friendly_abort (0); | |
6788 | } | |
db04386f | 6789 | return t; |
ed44da02 | 6790 | |
5566b478 MS |
6791 | case FIELD_DECL: |
6792 | if (DECL_CONTEXT (t)) | |
6793 | { | |
0978790f | 6794 | tree ctx; |
0978790f | 6795 | |
4393e105 | 6796 | ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl, |
36a117a5 | 6797 | /*entering_scope=*/1); |
0978790f | 6798 | if (ctx != DECL_CONTEXT (t)) |
5566b478 MS |
6799 | return lookup_field (ctx, DECL_NAME (t), 0, 0); |
6800 | } | |
6801 | return t; | |
6802 | ||
6803 | case VAR_DECL: | |
6804 | case FUNCTION_DECL: | |
6805 | if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)) | |
4393e105 | 6806 | t = tsubst (t, args, complain, in_decl); |
5566b478 MS |
6807 | mark_used (t); |
6808 | return t; | |
6809 | ||
98c1c668 JM |
6810 | case TEMPLATE_DECL: |
6811 | if (is_member_template (t)) | |
4393e105 | 6812 | return tsubst (t, args, complain, in_decl); |
98c1c668 JM |
6813 | else |
6814 | return t; | |
6815 | ||
672476cb MM |
6816 | case LOOKUP_EXPR: |
6817 | { | |
6818 | /* We must tsbust into a LOOKUP_EXPR in case the names to | |
6819 | which it refers is a conversion operator; in that case the | |
6820 | name will change. We avoid making unnecessary copies, | |
6821 | however. */ | |
6822 | ||
4393e105 | 6823 | tree id = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl); |
672476cb MM |
6824 | |
6825 | if (id != TREE_OPERAND (t, 0)) | |
6826 | { | |
8452b1d3 | 6827 | r = build_nt (LOOKUP_EXPR, id); |
672476cb MM |
6828 | LOOKUP_EXPR_GLOBAL (r) = LOOKUP_EXPR_GLOBAL (t); |
6829 | t = r; | |
6830 | } | |
6831 | ||
6832 | return t; | |
6833 | } | |
6834 | ||
5566b478 MS |
6835 | case CAST_EXPR: |
6836 | case REINTERPRET_CAST_EXPR: | |
e92cc029 MS |
6837 | case CONST_CAST_EXPR: |
6838 | case STATIC_CAST_EXPR: | |
6839 | case DYNAMIC_CAST_EXPR: | |
51924768 | 6840 | case NOP_EXPR: |
5566b478 | 6841 | return build1 |
4393e105 MM |
6842 | (code, tsubst (TREE_TYPE (t), args, complain, in_decl), |
6843 | tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl)); | |
5566b478 MS |
6844 | |
6845 | case INDIRECT_REF: | |
6846 | case PREDECREMENT_EXPR: | |
6847 | case PREINCREMENT_EXPR: | |
6848 | case POSTDECREMENT_EXPR: | |
6849 | case POSTINCREMENT_EXPR: | |
6850 | case NEGATE_EXPR: | |
6851 | case TRUTH_NOT_EXPR: | |
b87692e5 | 6852 | case BIT_NOT_EXPR: |
5566b478 MS |
6853 | case ADDR_EXPR: |
6854 | case CONVERT_EXPR: /* Unary + */ | |
6855 | case SIZEOF_EXPR: | |
abff8e06 | 6856 | case ALIGNOF_EXPR: |
5566b478 | 6857 | case ARROW_EXPR: |
fc378698 | 6858 | case THROW_EXPR: |
5156628f | 6859 | case TYPEID_EXPR: |
f5733617 SS |
6860 | case REALPART_EXPR: |
6861 | case IMAGPART_EXPR: | |
5566b478 | 6862 | return build1 |
6a629cac | 6863 | (code, tsubst (TREE_TYPE (t), args, complain, in_decl), |
4393e105 | 6864 | tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl)); |
5566b478 MS |
6865 | |
6866 | case PLUS_EXPR: | |
6867 | case MINUS_EXPR: | |
6868 | case MULT_EXPR: | |
6869 | case TRUNC_DIV_EXPR: | |
6870 | case CEIL_DIV_EXPR: | |
6871 | case FLOOR_DIV_EXPR: | |
6872 | case ROUND_DIV_EXPR: | |
6873 | case EXACT_DIV_EXPR: | |
6874 | case BIT_AND_EXPR: | |
6875 | case BIT_ANDTC_EXPR: | |
6876 | case BIT_IOR_EXPR: | |
6877 | case BIT_XOR_EXPR: | |
6878 | case TRUNC_MOD_EXPR: | |
6879 | case FLOOR_MOD_EXPR: | |
6880 | case TRUTH_ANDIF_EXPR: | |
6881 | case TRUTH_ORIF_EXPR: | |
6882 | case TRUTH_AND_EXPR: | |
6883 | case TRUTH_OR_EXPR: | |
6884 | case RSHIFT_EXPR: | |
6885 | case LSHIFT_EXPR: | |
6886 | case RROTATE_EXPR: | |
6887 | case LROTATE_EXPR: | |
6888 | case EQ_EXPR: | |
6889 | case NE_EXPR: | |
6890 | case MAX_EXPR: | |
6891 | case MIN_EXPR: | |
6892 | case LE_EXPR: | |
6893 | case GE_EXPR: | |
6894 | case LT_EXPR: | |
6895 | case GT_EXPR: | |
6896 | case COMPONENT_REF: | |
6897 | case ARRAY_REF: | |
6898 | case COMPOUND_EXPR: | |
6899 | case SCOPE_REF: | |
6900 | case DOTSTAR_EXPR: | |
6901 | case MEMBER_REF: | |
6902 | return build_nt | |
4393e105 MM |
6903 | (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl), |
6904 | tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl)); | |
5566b478 MS |
6905 | |
6906 | case CALL_EXPR: | |
6907 | { | |
6908 | tree fn = TREE_OPERAND (t, 0); | |
f84b4be9 | 6909 | if (is_overloaded_fn (fn)) |
4393e105 | 6910 | fn = tsubst_copy (get_first_fn (fn), args, complain, in_decl); |
5566b478 | 6911 | else |
f84b4be9 | 6912 | /* Sometimes FN is a LOOKUP_EXPR. */ |
4393e105 | 6913 | fn = tsubst_copy (fn, args, complain, in_decl); |
5566b478 | 6914 | return build_nt |
4393e105 MM |
6915 | (code, fn, tsubst_copy (TREE_OPERAND (t, 1), args, complain, |
6916 | in_decl), | |
5566b478 MS |
6917 | NULL_TREE); |
6918 | } | |
6919 | ||
6920 | case METHOD_CALL_EXPR: | |
6921 | { | |
6922 | tree name = TREE_OPERAND (t, 0); | |
6923 | if (TREE_CODE (name) == BIT_NOT_EXPR) | |
6924 | { | |
4393e105 MM |
6925 | name = tsubst_copy (TREE_OPERAND (name, 0), args, |
6926 | complain, in_decl); | |
7bae46f4 | 6927 | name = build1 (BIT_NOT_EXPR, NULL_TREE, name); |
5566b478 MS |
6928 | } |
6929 | else if (TREE_CODE (name) == SCOPE_REF | |
6930 | && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR) | |
6931 | { | |
4393e105 MM |
6932 | tree base = tsubst_copy (TREE_OPERAND (name, 0), args, |
6933 | complain, in_decl); | |
5566b478 | 6934 | name = TREE_OPERAND (name, 1); |
4393e105 MM |
6935 | name = tsubst_copy (TREE_OPERAND (name, 0), args, |
6936 | complain, in_decl); | |
11686454 | 6937 | name = build1 (BIT_NOT_EXPR, NULL_TREE, name); |
5566b478 MS |
6938 | name = build_nt (SCOPE_REF, base, name); |
6939 | } | |
6940 | else | |
4393e105 | 6941 | name = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl); |
5566b478 | 6942 | return build_nt |
4393e105 MM |
6943 | (code, name, tsubst_copy (TREE_OPERAND (t, 1), args, |
6944 | complain, in_decl), | |
6945 | tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl), | |
5566b478 MS |
6946 | NULL_TREE); |
6947 | } | |
6948 | ||
558475f0 MM |
6949 | case STMT_EXPR: |
6950 | /* This processing should really occur in tsubst_expr, However, | |
6951 | tsubst_expr does not recurse into expressions, since it | |
6952 | assumes that there aren't any statements inside them. | |
6953 | Instead, it simply calls build_expr_from_tree. So, we need | |
6954 | to expand the STMT_EXPR here. */ | |
6955 | if (!processing_template_decl) | |
6956 | { | |
b5ac18ea | 6957 | tree stmt_expr = begin_stmt_expr (); |
447cfcb9 MM |
6958 | tsubst_expr (STMT_EXPR_STMT (t), args, |
6959 | complain, in_decl); | |
b5ac18ea | 6960 | return finish_stmt_expr (stmt_expr); |
558475f0 | 6961 | } |
9d9169de MM |
6962 | |
6963 | return t; | |
558475f0 | 6964 | |
5566b478 MS |
6965 | case COND_EXPR: |
6966 | case MODOP_EXPR: | |
40242ccf | 6967 | case PSEUDO_DTOR_EXPR: |
67da3287 | 6968 | { |
8452b1d3 | 6969 | r = build_nt |
4393e105 MM |
6970 | (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl), |
6971 | tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl), | |
6972 | tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl)); | |
67da3287 MM |
6973 | return r; |
6974 | } | |
5566b478 MS |
6975 | |
6976 | case NEW_EXPR: | |
6977 | { | |
8452b1d3 | 6978 | r = build_nt |
4393e105 MM |
6979 | (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl), |
6980 | tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl), | |
6981 | tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl)); | |
5566b478 MS |
6982 | NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t); |
6983 | return r; | |
6984 | } | |
6985 | ||
6986 | case DELETE_EXPR: | |
6987 | { | |
8452b1d3 | 6988 | r = build_nt |
4393e105 MM |
6989 | (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl), |
6990 | tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl)); | |
5566b478 MS |
6991 | DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t); |
6992 | DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t); | |
6993 | return r; | |
6994 | } | |
6995 | ||
386b8a85 JM |
6996 | case TEMPLATE_ID_EXPR: |
6997 | { | |
00d3396f | 6998 | /* Substituted template arguments */ |
4393e105 MM |
6999 | tree targs = tsubst_copy (TREE_OPERAND (t, 1), args, complain, |
7000 | in_decl); | |
856216bb MM |
7001 | |
7002 | if (targs && TREE_CODE (targs) == TREE_LIST) | |
7003 | { | |
7004 | tree chain; | |
7005 | for (chain = targs; chain; chain = TREE_CHAIN (chain)) | |
7006 | TREE_VALUE (chain) = maybe_fold_nontype_arg (TREE_VALUE (chain)); | |
7007 | } | |
7008 | else if (targs) | |
7009 | { | |
7010 | int i; | |
7011 | for (i = 0; i < TREE_VEC_LENGTH (targs); ++i) | |
7012 | TREE_VEC_ELT (targs, i) | |
7013 | = maybe_fold_nontype_arg (TREE_VEC_ELT (targs, i)); | |
7014 | } | |
00d3396f JM |
7015 | |
7016 | return lookup_template_function | |
4393e105 | 7017 | (tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl), targs); |
386b8a85 JM |
7018 | } |
7019 | ||
5566b478 MS |
7020 | case TREE_LIST: |
7021 | { | |
7022 | tree purpose, value, chain; | |
7023 | ||
7024 | if (t == void_list_node) | |
7025 | return t; | |
7026 | ||
7027 | purpose = TREE_PURPOSE (t); | |
7028 | if (purpose) | |
4393e105 | 7029 | purpose = tsubst_copy (purpose, args, complain, in_decl); |
5566b478 MS |
7030 | value = TREE_VALUE (t); |
7031 | if (value) | |
4393e105 | 7032 | value = tsubst_copy (value, args, complain, in_decl); |
5566b478 MS |
7033 | chain = TREE_CHAIN (t); |
7034 | if (chain && chain != void_type_node) | |
4393e105 | 7035 | chain = tsubst_copy (chain, args, complain, in_decl); |
5566b478 MS |
7036 | if (purpose == TREE_PURPOSE (t) |
7037 | && value == TREE_VALUE (t) | |
7038 | && chain == TREE_CHAIN (t)) | |
7039 | return t; | |
7040 | return tree_cons (purpose, value, chain); | |
7041 | } | |
7042 | ||
7043 | case RECORD_TYPE: | |
7044 | case UNION_TYPE: | |
7045 | case ENUMERAL_TYPE: | |
7046 | case INTEGER_TYPE: | |
7047 | case TEMPLATE_TYPE_PARM: | |
73b0fce8 | 7048 | case TEMPLATE_TEMPLATE_PARM: |
f84b4be9 | 7049 | case TEMPLATE_PARM_INDEX: |
5566b478 MS |
7050 | case POINTER_TYPE: |
7051 | case REFERENCE_TYPE: | |
7052 | case OFFSET_TYPE: | |
7053 | case FUNCTION_TYPE: | |
7054 | case METHOD_TYPE: | |
7055 | case ARRAY_TYPE: | |
7056 | case TYPENAME_TYPE: | |
f84b4be9 | 7057 | case TYPE_DECL: |
4393e105 | 7058 | return tsubst (t, args, complain, in_decl); |
5566b478 | 7059 | |
e92cc029 | 7060 | case IDENTIFIER_NODE: |
8e710788 BK |
7061 | if (IDENTIFIER_TYPENAME_P (t) |
7062 | /* Make sure it's not just a variable named `__opr', for instance, | |
7063 | which can occur in some existing code. */ | |
7064 | && TREE_TYPE (t)) | |
e92cc029 | 7065 | return build_typename_overload |
4393e105 | 7066 | (tsubst (TREE_TYPE (t), args, complain, in_decl)); |
e92cc029 MS |
7067 | else |
7068 | return t; | |
7069 | ||
5156628f | 7070 | case CONSTRUCTOR: |
8452b1d3 JM |
7071 | { |
7072 | r = build | |
4393e105 MM |
7073 | (CONSTRUCTOR, tsubst (TREE_TYPE (t), args, complain, in_decl), |
7074 | NULL_TREE, tsubst_copy (CONSTRUCTOR_ELTS (t), args, | |
7075 | complain, in_decl)); | |
8452b1d3 JM |
7076 | TREE_HAS_CONSTRUCTOR (r) = TREE_HAS_CONSTRUCTOR (t); |
7077 | return r; | |
7078 | } | |
5156628f | 7079 | |
371534a9 MM |
7080 | case VA_ARG_EXPR: |
7081 | return build_va_arg (tsubst_copy (TREE_OPERAND (t, 0), args, complain, | |
7082 | in_decl), | |
7083 | tsubst (TREE_TYPE (t), args, complain, in_decl)); | |
f9817201 MM |
7084 | |
7085 | case FUNCTION_NAME: | |
7086 | return tsubst (t, args, complain, in_decl); | |
7087 | ||
5566b478 MS |
7088 | default: |
7089 | return t; | |
7090 | } | |
7091 | } | |
7092 | ||
558475f0 | 7093 | /* Like tsubst_copy, but also does semantic processing. */ |
00d3396f | 7094 | |
5566b478 | 7095 | tree |
4393e105 | 7096 | tsubst_expr (t, args, complain, in_decl) |
98c1c668 | 7097 | tree t, args; |
4393e105 | 7098 | int complain; |
5566b478 MS |
7099 | tree in_decl; |
7100 | { | |
558475f0 MM |
7101 | tree stmt; |
7102 | ||
5566b478 MS |
7103 | if (t == NULL_TREE || t == error_mark_node) |
7104 | return t; | |
7105 | ||
5156628f | 7106 | if (processing_template_decl) |
4393e105 | 7107 | return tsubst_copy (t, args, complain, in_decl); |
5566b478 MS |
7108 | |
7109 | switch (TREE_CODE (t)) | |
8d08fdba | 7110 | { |
558475f0 | 7111 | case RETURN_INIT: |
a7e4cfa0 | 7112 | prep_stmt (t); |
558475f0 MM |
7113 | finish_named_return_value |
7114 | (TREE_OPERAND (t, 0), | |
7115 | tsubst_expr (TREE_OPERAND (t, 1), args, /*complain=*/1, in_decl)); | |
7116 | tsubst_expr (TREE_CHAIN (t), args, complain, in_decl); | |
7117 | break; | |
7118 | ||
7119 | case CTOR_INITIALIZER: | |
a7e4cfa0 | 7120 | prep_stmt (t); |
558475f0 MM |
7121 | current_member_init_list |
7122 | = tsubst_expr_values (TREE_OPERAND (t, 0), args); | |
7123 | current_base_init_list | |
7124 | = tsubst_expr_values (TREE_OPERAND (t, 1), args); | |
7125 | setup_vtbl_ptr (); | |
7126 | tsubst_expr (TREE_CHAIN (t), args, complain, in_decl); | |
7127 | break; | |
7128 | ||
5566b478 | 7129 | case RETURN_STMT: |
a7e4cfa0 | 7130 | prep_stmt (t); |
ad321293 | 7131 | finish_return_stmt (tsubst_expr (RETURN_EXPR (t), |
4393e105 | 7132 | args, complain, in_decl)); |
5566b478 MS |
7133 | break; |
7134 | ||
7135 | case EXPR_STMT: | |
a7e4cfa0 | 7136 | prep_stmt (t); |
ad321293 | 7137 | finish_expr_stmt (tsubst_expr (EXPR_STMT_EXPR (t), |
4393e105 | 7138 | args, complain, in_decl)); |
5566b478 MS |
7139 | break; |
7140 | ||
7141 | case DECL_STMT: | |
7142 | { | |
e0942dcd MM |
7143 | tree decl; |
7144 | tree init; | |
5566b478 | 7145 | |
a7e4cfa0 | 7146 | prep_stmt (t); |
e0942dcd | 7147 | decl = DECL_STMT_DECL (t); |
acef433b MM |
7148 | if (TREE_CODE (decl) == LABEL_DECL) |
7149 | finish_label_decl (DECL_NAME (decl)); | |
7150 | else | |
7151 | { | |
7152 | init = DECL_INITIAL (decl); | |
7153 | decl = tsubst (decl, args, complain, in_decl); | |
7154 | init = tsubst_expr (init, args, complain, in_decl); | |
24bef158 MM |
7155 | if (init) |
7156 | DECL_INITIAL (decl) = error_mark_node; | |
b7b8bcd2 MM |
7157 | /* By marking the declaration as instantiated, we avoid |
7158 | trying to instantiate it. Since instantiate_decl can't | |
7159 | handle local variables, and since we've already done | |
7160 | all that needs to be done, that's the right thing to | |
7161 | do. */ | |
acef433b MM |
7162 | if (TREE_CODE (decl) == VAR_DECL) |
7163 | DECL_TEMPLATE_INSTANTIATED (decl) = 1; | |
7164 | maybe_push_decl (decl); | |
cd9f6678 | 7165 | cp_finish_decl (decl, init, NULL_TREE, 0); |
acef433b | 7166 | } |
e0942dcd | 7167 | return decl; |
5566b478 | 7168 | } |
8d08fdba | 7169 | |
5566b478 MS |
7170 | case FOR_STMT: |
7171 | { | |
7172 | tree tmp; | |
a7e4cfa0 | 7173 | prep_stmt (t); |
5566b478 | 7174 | |
558475f0 | 7175 | stmt = begin_for_stmt (); |
ad321293 | 7176 | for (tmp = FOR_INIT_STMT (t); tmp; tmp = TREE_CHAIN (tmp)) |
4393e105 | 7177 | tsubst_expr (tmp, args, complain, in_decl); |
558475f0 | 7178 | finish_for_init_stmt (stmt); |
ad321293 | 7179 | finish_for_cond (tsubst_expr (FOR_COND (t), args, |
4393e105 | 7180 | complain, in_decl), |
558475f0 | 7181 | stmt); |
4393e105 | 7182 | tmp = tsubst_expr (FOR_EXPR (t), args, complain, in_decl); |
558475f0 | 7183 | finish_for_expr (tmp, stmt); |
4393e105 | 7184 | tsubst_expr (FOR_BODY (t), args, complain, in_decl); |
558475f0 | 7185 | finish_for_stmt (tmp, stmt); |
5566b478 MS |
7186 | } |
7187 | break; | |
8d08fdba | 7188 | |
5566b478 MS |
7189 | case WHILE_STMT: |
7190 | { | |
a7e4cfa0 | 7191 | prep_stmt (t); |
558475f0 | 7192 | stmt = begin_while_stmt (); |
ad321293 | 7193 | finish_while_stmt_cond (tsubst_expr (WHILE_COND (t), |
4393e105 | 7194 | args, complain, in_decl), |
558475f0 | 7195 | stmt); |
4393e105 | 7196 | tsubst_expr (WHILE_BODY (t), args, complain, in_decl); |
558475f0 | 7197 | finish_while_stmt (stmt); |
5566b478 MS |
7198 | } |
7199 | break; | |
8d08fdba | 7200 | |
5566b478 MS |
7201 | case DO_STMT: |
7202 | { | |
a7e4cfa0 | 7203 | prep_stmt (t); |
558475f0 | 7204 | stmt = begin_do_stmt (); |
4393e105 | 7205 | tsubst_expr (DO_BODY (t), args, complain, in_decl); |
558475f0 | 7206 | finish_do_body (stmt); |
ad321293 | 7207 | finish_do_stmt (tsubst_expr (DO_COND (t), args, |
4393e105 | 7208 | complain, in_decl), |
558475f0 | 7209 | stmt); |
5566b478 MS |
7210 | } |
7211 | break; | |
a0a33927 | 7212 | |
5566b478 | 7213 | case IF_STMT: |
8d08fdba | 7214 | { |
5566b478 | 7215 | tree tmp; |
5566b478 | 7216 | |
a7e4cfa0 | 7217 | prep_stmt (t); |
558475f0 | 7218 | stmt = begin_if_stmt (); |
ad321293 | 7219 | finish_if_stmt_cond (tsubst_expr (IF_COND (t), |
4393e105 | 7220 | args, complain, in_decl), |
558475f0 | 7221 | stmt); |
db5ae43f | 7222 | |
ad321293 | 7223 | if (tmp = THEN_CLAUSE (t), tmp) |
db5ae43f | 7224 | { |
4393e105 | 7225 | tsubst_expr (tmp, args, complain, in_decl); |
558475f0 | 7226 | finish_then_clause (stmt); |
db5ae43f MS |
7227 | } |
7228 | ||
ad321293 MM |
7229 | if (tmp = ELSE_CLAUSE (t), tmp) |
7230 | { | |
7231 | begin_else_clause (); | |
4393e105 | 7232 | tsubst_expr (tmp, args, complain, in_decl); |
558475f0 | 7233 | finish_else_clause (stmt); |
ad321293 | 7234 | } |
8d08fdba | 7235 | |
ad321293 | 7236 | finish_if_stmt (); |
8d08fdba | 7237 | } |
5566b478 | 7238 | break; |
8d08fdba | 7239 | |
5566b478 MS |
7240 | case COMPOUND_STMT: |
7241 | { | |
ad321293 | 7242 | tree substmt; |
8d08fdba | 7243 | |
a7e4cfa0 | 7244 | prep_stmt (t); |
558475f0 | 7245 | stmt = begin_compound_stmt (COMPOUND_STMT_NO_SCOPE (t)); |
ad321293 MM |
7246 | for (substmt = COMPOUND_BODY (t); |
7247 | substmt != NULL_TREE; | |
7248 | substmt = TREE_CHAIN (substmt)) | |
4393e105 | 7249 | tsubst_expr (substmt, args, complain, in_decl); |
558475f0 | 7250 | return finish_compound_stmt (COMPOUND_STMT_NO_SCOPE (t), stmt); |
5566b478 MS |
7251 | } |
7252 | break; | |
8d08fdba | 7253 | |
5566b478 | 7254 | case BREAK_STMT: |
a7e4cfa0 | 7255 | prep_stmt (t); |
ad321293 | 7256 | finish_break_stmt (); |
5566b478 | 7257 | break; |
8d08fdba | 7258 | |
6467930b | 7259 | case CONTINUE_STMT: |
a7e4cfa0 | 7260 | prep_stmt (t); |
ad321293 | 7261 | finish_continue_stmt (); |
6467930b MS |
7262 | break; |
7263 | ||
5566b478 MS |
7264 | case SWITCH_STMT: |
7265 | { | |
558475f0 | 7266 | tree val; |
5566b478 | 7267 | |
a7e4cfa0 | 7268 | prep_stmt (t); |
527f0080 | 7269 | stmt = begin_switch_stmt (); |
4393e105 | 7270 | val = tsubst_expr (SWITCH_COND (t), args, complain, in_decl); |
527f0080 | 7271 | finish_switch_cond (val, stmt); |
558475f0 MM |
7272 | tsubst_expr (SWITCH_BODY (t), args, complain, in_decl); |
7273 | finish_switch_stmt (val, stmt); | |
5566b478 MS |
7274 | } |
7275 | break; | |
7276 | ||
7277 | case CASE_LABEL: | |
a7e4cfa0 | 7278 | prep_stmt (t); |
4393e105 MM |
7279 | finish_case_label (tsubst_expr (CASE_LOW (t), args, complain, in_decl), |
7280 | tsubst_expr (CASE_HIGH (t), args, complain, in_decl)); | |
5566b478 MS |
7281 | break; |
7282 | ||
acef433b MM |
7283 | case LABEL_STMT: |
7284 | lineno = STMT_LINENO (t); | |
7285 | finish_label_stmt (DECL_NAME (LABEL_STMT_LABEL (t))); | |
5566b478 MS |
7286 | break; |
7287 | ||
7288 | case GOTO_STMT: | |
a7e4cfa0 | 7289 | prep_stmt (t); |
aa09da44 | 7290 | t = GOTO_DESTINATION (t); |
3fa56191 | 7291 | if (TREE_CODE (t) != LABEL_DECL) |
aa09da44 MM |
7292 | /* Computed goto's must be tsubst'd into. On the other hand, |
7293 | non-computed gotos must not be; the identifier in question | |
7294 | will have no binding. */ | |
4393e105 | 7295 | t = tsubst_expr (t, args, complain, in_decl); |
3fa56191 MM |
7296 | else |
7297 | t = DECL_NAME (t); | |
aa09da44 | 7298 | finish_goto_stmt (t); |
ad321293 MM |
7299 | break; |
7300 | ||
7301 | case ASM_STMT: | |
a7e4cfa0 | 7302 | prep_stmt (t); |
3ebc5c52 | 7303 | finish_asm_stmt (ASM_CV_QUAL (t), |
4393e105 MM |
7304 | tsubst_expr (ASM_STRING (t), args, complain, in_decl), |
7305 | tsubst_expr (ASM_OUTPUTS (t), args, complain, in_decl), | |
7306 | tsubst_expr (ASM_INPUTS (t), args, complain, in_decl), | |
7307 | tsubst_expr (ASM_CLOBBERS (t), args, complain, | |
7308 | in_decl)); | |
5566b478 | 7309 | break; |
faf5394a MS |
7310 | |
7311 | case TRY_BLOCK: | |
a7e4cfa0 | 7312 | prep_stmt (t); |
f1dedc31 | 7313 | if (CLEANUP_P (t)) |
62409b39 | 7314 | { |
57b52417 | 7315 | stmt = begin_try_block (); |
62409b39 MM |
7316 | tsubst_expr (TRY_STMTS (t), args, complain, in_decl); |
7317 | finish_cleanup_try_block (stmt); | |
7318 | finish_cleanup (tsubst_expr (TRY_HANDLERS (t), args, | |
7319 | complain, in_decl), | |
7320 | stmt); | |
7321 | } | |
f1dedc31 MM |
7322 | else |
7323 | { | |
62409b39 MM |
7324 | tree handler; |
7325 | ||
7326 | if (FN_TRY_BLOCK_P (t)) | |
7327 | stmt = begin_function_try_block (); | |
7328 | else | |
7329 | stmt = begin_try_block (); | |
7330 | ||
7331 | tsubst_expr (TRY_STMTS (t), args, complain, in_decl); | |
7332 | ||
7333 | if (FN_TRY_BLOCK_P (t)) | |
7334 | finish_function_try_block (stmt); | |
7335 | else | |
7336 | finish_try_block (stmt); | |
7337 | ||
7338 | handler = TRY_HANDLERS (t); | |
f1dedc31 MM |
7339 | for (; handler; handler = TREE_CHAIN (handler)) |
7340 | tsubst_expr (handler, args, complain, in_decl); | |
84df082b MM |
7341 | if (FN_TRY_BLOCK_P (t)) |
7342 | finish_function_handler_sequence (stmt); | |
7343 | else | |
7344 | finish_handler_sequence (stmt); | |
f1dedc31 | 7345 | } |
faf5394a | 7346 | break; |
b35d4555 | 7347 | |
faf5394a | 7348 | case HANDLER: |
b35d4555 MM |
7349 | { |
7350 | tree decl; | |
7351 | tree blocks; | |
7352 | ||
7353 | prep_stmt (t); | |
7354 | stmt = begin_handler (); | |
7355 | if (HANDLER_PARMS (t)) | |
7356 | { | |
7357 | decl = DECL_STMT_DECL (HANDLER_PARMS (t)); | |
7358 | decl = tsubst (decl, args, complain, in_decl); | |
f8191e64 MM |
7359 | /* Prevent instantiate_decl from trying to instantiate |
7360 | this variable. We've already done all that needs to be | |
7361 | done. */ | |
7362 | DECL_TEMPLATE_INSTANTIATED (decl) = 1; | |
b35d4555 MM |
7363 | } |
7364 | else | |
7365 | decl = NULL_TREE; | |
7366 | blocks = finish_handler_parms (decl, stmt); | |
7367 | tsubst_expr (HANDLER_BODY (t), args, complain, in_decl); | |
7368 | finish_handler (blocks, stmt); | |
7369 | } | |
faf5394a MS |
7370 | break; |
7371 | ||
b87692e5 | 7372 | case TAG_DEFN: |
a7e4cfa0 | 7373 | prep_stmt (t); |
b87692e5 | 7374 | t = TREE_TYPE (t); |
ae673f14 | 7375 | tsubst (t, args, complain, NULL_TREE); |
b87692e5 MS |
7376 | break; |
7377 | ||
5566b478 | 7378 | default: |
4393e105 | 7379 | return build_expr_from_tree (tsubst_copy (t, args, complain, in_decl)); |
5566b478 MS |
7380 | } |
7381 | return NULL_TREE; | |
8d08fdba MS |
7382 | } |
7383 | ||
db9b2174 MM |
7384 | /* TMPL is a TEMPLATE_DECL for a cloned constructor or destructor. |
7385 | Instantiate it with the ARGS. */ | |
7386 | ||
7387 | static tree | |
7388 | instantiate_clone (tmpl, args) | |
7389 | tree tmpl; | |
7390 | tree args; | |
7391 | { | |
7392 | tree spec; | |
7393 | tree clone; | |
7394 | ||
7395 | /* Instantiated the cloned function, rather than the clone. */ | |
7396 | spec = instantiate_template (DECL_CLONED_FUNCTION (tmpl), args); | |
7397 | ||
7398 | /* Then, see if we've already cloned the instantiation. */ | |
7399 | for (clone = TREE_CHAIN (spec); | |
7400 | clone && DECL_CLONED_FUNCTION_P (clone); | |
7401 | clone = TREE_CHAIN (clone)) | |
7402 | if (DECL_NAME (clone) == DECL_NAME (tmpl)) | |
7403 | return clone; | |
7404 | ||
7405 | /* If we haven't, do so know. */ | |
7406 | if (!clone) | |
7407 | clone_function_decl (spec, /*update_method_vec_p=*/0); | |
7408 | ||
7409 | /* Look again. */ | |
7410 | for (clone = TREE_CHAIN (spec); | |
7411 | clone && DECL_CLONED_FUNCTION_P (clone); | |
7412 | clone = TREE_CHAIN (clone)) | |
7413 | if (DECL_NAME (clone) == DECL_NAME (tmpl)) | |
7414 | return clone; | |
7415 | ||
7416 | /* We should always have found the clone by now. */ | |
7417 | my_friendly_abort (20000411); | |
7418 | return NULL_TREE; | |
7419 | } | |
7420 | ||
6ba89f8e | 7421 | /* Instantiate the indicated variable or function template TMPL with |
36a117a5 MM |
7422 | the template arguments in TARG_PTR. */ |
7423 | ||
5566b478 MS |
7424 | tree |
7425 | instantiate_template (tmpl, targ_ptr) | |
98c1c668 | 7426 | tree tmpl, targ_ptr; |
8d08fdba | 7427 | { |
5566b478 | 7428 | tree fndecl; |
36a117a5 MM |
7429 | tree gen_tmpl; |
7430 | tree spec; | |
5566b478 | 7431 | int i, len; |
36a117a5 | 7432 | tree inner_args; |
5566b478 | 7433 | |
27fafc8d JM |
7434 | if (tmpl == error_mark_node) |
7435 | return error_mark_node; | |
7436 | ||
386b8a85 JM |
7437 | my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 283); |
7438 | ||
db9b2174 MM |
7439 | /* If this function is a clone, handle it specially. */ |
7440 | if (DECL_CLONED_FUNCTION_P (tmpl)) | |
7441 | return instantiate_clone (tmpl, targ_ptr); | |
7442 | ||
36a117a5 MM |
7443 | /* Check to see if we already have this specialization. */ |
7444 | spec = retrieve_specialization (tmpl, targ_ptr); | |
7445 | if (spec != NULL_TREE) | |
7446 | return spec; | |
7447 | ||
74b846e0 | 7448 | if (DECL_TEMPLATE_INFO (tmpl) && !DECL_TEMPLATE_SPECIALIZATION (tmpl)) |
386b8a85 | 7449 | { |
36a117a5 MM |
7450 | /* The TMPL is a partial instantiation. To get a full set of |
7451 | arguments we must add the arguments used to perform the | |
7452 | partial instantiation. */ | |
7453 | targ_ptr = add_outermost_template_args (DECL_TI_ARGS (tmpl), | |
7454 | targ_ptr); | |
7455 | gen_tmpl = most_general_template (tmpl); | |
7456 | ||
7457 | /* Check to see if we already have this specialization. */ | |
7458 | spec = retrieve_specialization (gen_tmpl, targ_ptr); | |
75650646 MM |
7459 | if (spec != NULL_TREE) |
7460 | return spec; | |
386b8a85 | 7461 | } |
36a117a5 MM |
7462 | else |
7463 | gen_tmpl = tmpl; | |
386b8a85 | 7464 | |
36a117a5 MM |
7465 | len = DECL_NTPARMS (gen_tmpl); |
7466 | inner_args = innermost_args (targ_ptr); | |
5566b478 MS |
7467 | i = len; |
7468 | while (i--) | |
8d08fdba | 7469 | { |
36a117a5 | 7470 | tree t = TREE_VEC_ELT (inner_args, i); |
2f939d94 | 7471 | if (TYPE_P (t)) |
5566b478 MS |
7472 | { |
7473 | tree nt = target_type (t); | |
ec255269 | 7474 | if (IS_AGGR_TYPE (nt) && decl_function_context (TYPE_MAIN_DECL (nt))) |
5566b478 | 7475 | { |
8251199e JM |
7476 | cp_error ("type `%T' composed from a local class is not a valid template-argument", t); |
7477 | cp_error (" trying to instantiate `%D'", gen_tmpl); | |
454fa7a7 | 7478 | return error_mark_node; |
5566b478 MS |
7479 | } |
7480 | } | |
8d08fdba MS |
7481 | } |
7482 | ||
5566b478 | 7483 | /* substitute template parameters */ |
17aec3eb RK |
7484 | fndecl = tsubst (DECL_TEMPLATE_RESULT (gen_tmpl), |
7485 | targ_ptr, /*complain=*/1, gen_tmpl); | |
36a117a5 MM |
7486 | /* The DECL_TI_TEMPLATE should always be the immediate parent |
7487 | template, not the most general template. */ | |
7488 | DECL_TI_TEMPLATE (fndecl) = tmpl; | |
8d08fdba | 7489 | |
824b9a4c MS |
7490 | if (flag_external_templates) |
7491 | add_pending_template (fndecl); | |
7492 | ||
5566b478 | 7493 | return fndecl; |
8d08fdba | 7494 | } |
5566b478 MS |
7495 | |
7496 | /* Push the name of the class template into the scope of the instantiation. */ | |
8d08fdba MS |
7497 | |
7498 | void | |
5566b478 MS |
7499 | overload_template_name (type) |
7500 | tree type; | |
8d08fdba | 7501 | { |
5566b478 MS |
7502 | tree id = DECL_NAME (CLASSTYPE_TI_TEMPLATE (type)); |
7503 | tree decl; | |
8d08fdba | 7504 | |
5566b478 MS |
7505 | if (IDENTIFIER_CLASS_VALUE (id) |
7506 | && TREE_TYPE (IDENTIFIER_CLASS_VALUE (id)) == type) | |
7507 | return; | |
8d08fdba | 7508 | |
5566b478 MS |
7509 | decl = build_decl (TYPE_DECL, id, type); |
7510 | SET_DECL_ARTIFICIAL (decl); | |
7511 | pushdecl_class_level (decl); | |
8d08fdba MS |
7512 | } |
7513 | ||
4393e105 MM |
7514 | /* The FN is a TEMPLATE_DECL for a function. The ARGS are the |
7515 | arguments that are being used when calling it. TARGS is a vector | |
7516 | into which the deduced template arguments are placed. | |
8d08fdba MS |
7517 | |
7518 | Return zero for success, 2 for an incomplete match that doesn't resolve | |
7519 | all the types, and 1 for complete failure. An error message will be | |
7520 | printed only for an incomplete match. | |
7521 | ||
4393e105 MM |
7522 | If FN is a conversion operator, RETURN_TYPE is the type desired as |
7523 | the result of the conversion operator. | |
8d08fdba | 7524 | |
4393e105 | 7525 | TPARMS is a vector of template parameters. |
9f54c803 MM |
7526 | |
7527 | The EXPLICIT_TARGS are explicit template arguments provided via a | |
7528 | template-id. | |
6467930b | 7529 | |
830bfa74 MM |
7530 | The parameter STRICT is one of: |
7531 | ||
7532 | DEDUCE_CALL: | |
7533 | We are deducing arguments for a function call, as in | |
7534 | [temp.deduct.call]. | |
7535 | ||
7536 | DEDUCE_CONV: | |
7537 | We are deducing arguments for a conversion function, as in | |
7538 | [temp.deduct.conv]. | |
7539 | ||
7540 | DEDUCE_EXACT: | |
7541 | We are deducing arguments when calculating the partial | |
7542 | ordering between specializations of function or class | |
7543 | templates, as in [temp.func.order] and [temp.class.order], | |
7544 | when doing an explicit instantiation as in [temp.explicit], | |
9f54c803 | 7545 | when determining an explicit specialization as in |
830bfa74 | 7546 | [temp.expl.spec], or when taking the address of a function |
4393e105 MM |
7547 | template, as in [temp.deduct.funcaddr]. |
7548 | ||
4393e105 | 7549 | The other arguments are as for type_unification. */ |
8d08fdba MS |
7550 | |
7551 | int | |
4393e105 | 7552 | fn_type_unification (fn, explicit_targs, targs, args, return_type, |
03017874 | 7553 | strict) |
4393e105 | 7554 | tree fn, explicit_targs, targs, args, return_type; |
830bfa74 | 7555 | unification_kind_t strict; |
386b8a85 | 7556 | { |
4393e105 MM |
7557 | tree parms; |
7558 | tree fntype; | |
adecb3f4 | 7559 | int result; |
386b8a85 | 7560 | |
4393e105 MM |
7561 | my_friendly_assert (TREE_CODE (fn) == TEMPLATE_DECL, 0); |
7562 | ||
7563 | fntype = TREE_TYPE (fn); | |
7564 | if (explicit_targs) | |
75650646 | 7565 | { |
4393e105 MM |
7566 | /* [temp.deduct] |
7567 | ||
7568 | The specified template arguments must match the template | |
7569 | parameters in kind (i.e., type, nontype, template), and there | |
7570 | must not be more arguments than there are parameters; | |
7571 | otherwise type deduction fails. | |
7572 | ||
7573 | Nontype arguments must match the types of the corresponding | |
7574 | nontype template parameters, or must be convertible to the | |
7575 | types of the corresponding nontype parameters as specified in | |
7576 | _temp.arg.nontype_, otherwise type deduction fails. | |
7577 | ||
7578 | All references in the function type of the function template | |
7579 | to the corresponding template parameters are replaced by the | |
7580 | specified template argument values. If a substitution in a | |
7581 | template parameter or in the function type of the function | |
7582 | template results in an invalid type, type deduction fails. */ | |
7583 | int i; | |
7584 | tree converted_args; | |
75650646 | 7585 | |
4393e105 MM |
7586 | converted_args |
7587 | = (coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn), | |
7588 | explicit_targs, NULL_TREE, /*complain=*/0, | |
7589 | /*require_all_arguments=*/0)); | |
7590 | if (converted_args == error_mark_node) | |
75650646 | 7591 | return 1; |
386b8a85 | 7592 | |
4393e105 MM |
7593 | fntype = tsubst (fntype, converted_args, /*complain=*/0, NULL_TREE); |
7594 | if (fntype == error_mark_node) | |
7595 | return 1; | |
050367a3 | 7596 | |
4393e105 MM |
7597 | /* Place the explicitly specified arguments in TARGS. */ |
7598 | for (i = 0; i < TREE_VEC_LENGTH (targs); i++) | |
7599 | TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (converted_args, i); | |
75650646 | 7600 | } |
4393e105 MM |
7601 | |
7602 | parms = TYPE_ARG_TYPES (fntype); | |
386b8a85 | 7603 | |
4393e105 MM |
7604 | if (DECL_CONV_FN_P (fn)) |
7605 | { | |
8d3631f8 | 7606 | /* This is a template conversion operator. Remove `this', since |
edef8288 | 7607 | we could be comparing conversions from different classes. */ |
8d3631f8 NS |
7608 | parms = TREE_CHAIN (parms); |
7609 | args = TREE_CHAIN (args); | |
7610 | my_friendly_assert (return_type != NULL_TREE, 20000227); | |
7611 | } | |
7612 | ||
7613 | if (return_type) | |
7614 | { | |
7615 | /* We've been given a return type to match, prepend it. */ | |
7616 | parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms); | |
7617 | args = tree_cons (NULL_TREE, return_type, args); | |
4393e105 MM |
7618 | } |
7619 | ||
4393e105 MM |
7620 | /* We allow incomplete unification without an error message here |
7621 | because the standard doesn't seem to explicitly prohibit it. Our | |
7622 | callers must be ready to deal with unification failures in any | |
7623 | event. */ | |
adecb3f4 MM |
7624 | result = type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn), |
7625 | targs, parms, args, /*subr=*/0, | |
7626 | strict, /*allow_incomplete*/1); | |
7627 | ||
7628 | if (result == 0) | |
7629 | /* All is well so far. Now, check: | |
7630 | ||
7631 | [temp.deduct] | |
7632 | ||
7633 | When all template arguments have been deduced, all uses of | |
7634 | template parameters in nondeduced contexts are replaced with | |
7635 | the corresponding deduced argument values. If the | |
7636 | substitution results in an invalid type, as described above, | |
7637 | type deduction fails. */ | |
7638 | if (tsubst (TREE_TYPE (fn), targs, /*complain=*/0, NULL_TREE) | |
7639 | == error_mark_node) | |
7640 | return 1; | |
7641 | ||
7642 | return result; | |
830bfa74 MM |
7643 | } |
7644 | ||
7645 | /* Adjust types before performing type deduction, as described in | |
7646 | [temp.deduct.call] and [temp.deduct.conv]. The rules in these two | |
4c7d0dff MM |
7647 | sections are symmetric. PARM is the type of a function parameter |
7648 | or the return type of the conversion function. ARG is the type of | |
7649 | the argument passed to the call, or the type of the value | |
7650 | intialized with the result of the conversion function. */ | |
386b8a85 | 7651 | |
e9659ab0 | 7652 | static void |
830bfa74 MM |
7653 | maybe_adjust_types_for_deduction (strict, parm, arg) |
7654 | unification_kind_t strict; | |
7655 | tree* parm; | |
7656 | tree* arg; | |
7657 | { | |
7658 | switch (strict) | |
7659 | { | |
7660 | case DEDUCE_CALL: | |
7661 | break; | |
7662 | ||
7663 | case DEDUCE_CONV: | |
7664 | { | |
4c7d0dff MM |
7665 | /* Swap PARM and ARG throughout the remainder of this |
7666 | function; the handling is precisely symmetric since PARM | |
7667 | will initialize ARG rather than vice versa. */ | |
830bfa74 MM |
7668 | tree* temp = parm; |
7669 | parm = arg; | |
7670 | arg = temp; | |
7671 | break; | |
7672 | } | |
7673 | ||
7674 | case DEDUCE_EXACT: | |
7675 | /* There is nothing to do in this case. */ | |
7676 | return; | |
7677 | ||
7678 | default: | |
7679 | my_friendly_abort (0); | |
7680 | } | |
7681 | ||
7682 | if (TREE_CODE (*parm) != REFERENCE_TYPE) | |
7683 | { | |
7684 | /* [temp.deduct.call] | |
7685 | ||
7686 | If P is not a reference type: | |
7687 | ||
7688 | --If A is an array type, the pointer type produced by the | |
7689 | array-to-pointer standard conversion (_conv.array_) is | |
7690 | used in place of A for type deduction; otherwise, | |
7691 | ||
7692 | --If A is a function type, the pointer type produced by | |
7693 | the function-to-pointer standard conversion | |
7694 | (_conv.func_) is used in place of A for type deduction; | |
7695 | otherwise, | |
7696 | ||
7697 | --If A is a cv-qualified type, the top level | |
7698 | cv-qualifiers of A's type are ignored for type | |
7699 | deduction. */ | |
7700 | if (TREE_CODE (*arg) == ARRAY_TYPE) | |
7701 | *arg = build_pointer_type (TREE_TYPE (*arg)); | |
d8f8dca1 | 7702 | else if (TREE_CODE (*arg) == FUNCTION_TYPE) |
830bfa74 MM |
7703 | *arg = build_pointer_type (*arg); |
7704 | else | |
7705 | *arg = TYPE_MAIN_VARIANT (*arg); | |
7706 | } | |
7707 | ||
7708 | /* [temp.deduct.call] | |
7709 | ||
7710 | If P is a cv-qualified type, the top level cv-qualifiers | |
7711 | of P's type are ignored for type deduction. If P is a | |
7712 | reference type, the type referred to by P is used for | |
7713 | type deduction. */ | |
7714 | *parm = TYPE_MAIN_VARIANT (*parm); | |
7715 | if (TREE_CODE (*parm) == REFERENCE_TYPE) | |
7716 | *parm = TREE_TYPE (*parm); | |
386b8a85 JM |
7717 | } |
7718 | ||
4393e105 | 7719 | /* Like type_unfication. |
9f54c803 MM |
7720 | |
7721 | If SUBR is 1, we're being called recursively (to unify the | |
7722 | arguments of a function or method parameter of a function | |
7723 | template). */ | |
386b8a85 | 7724 | |
4966381a | 7725 | static int |
3b3ba9f0 | 7726 | type_unification_real (tparms, targs, parms, args, subr, |
4393e105 | 7727 | strict, allow_incomplete) |
050367a3 | 7728 | tree tparms, targs, parms, args; |
830bfa74 MM |
7729 | int subr; |
7730 | unification_kind_t strict; | |
7731 | int allow_incomplete; | |
8d08fdba MS |
7732 | { |
7733 | tree parm, arg; | |
7734 | int i; | |
7735 | int ntparms = TREE_VEC_LENGTH (tparms); | |
830bfa74 | 7736 | int sub_strict; |
8d08fdba MS |
7737 | |
7738 | my_friendly_assert (TREE_CODE (tparms) == TREE_VEC, 289); | |
386b8a85 JM |
7739 | my_friendly_assert (parms == NULL_TREE |
7740 | || TREE_CODE (parms) == TREE_LIST, 290); | |
51c184be | 7741 | /* ARGS could be NULL (via a call from parse.y to |
8d08fdba MS |
7742 | build_x_function_call). */ |
7743 | if (args) | |
7744 | my_friendly_assert (TREE_CODE (args) == TREE_LIST, 291); | |
7745 | my_friendly_assert (ntparms > 0, 292); | |
7746 | ||
830bfa74 MM |
7747 | switch (strict) |
7748 | { | |
7749 | case DEDUCE_CALL: | |
7750 | sub_strict = UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_DERIVED; | |
7751 | break; | |
7752 | ||
7753 | case DEDUCE_CONV: | |
7754 | sub_strict = UNIFY_ALLOW_LESS_CV_QUAL; | |
7755 | break; | |
7756 | ||
7757 | case DEDUCE_EXACT: | |
7758 | sub_strict = UNIFY_ALLOW_NONE; | |
7759 | break; | |
7760 | ||
7761 | default: | |
7762 | my_friendly_abort (0); | |
7763 | } | |
7764 | ||
8d08fdba MS |
7765 | while (parms |
7766 | && parms != void_list_node | |
7767 | && args | |
7768 | && args != void_list_node) | |
7769 | { | |
7770 | parm = TREE_VALUE (parms); | |
7771 | parms = TREE_CHAIN (parms); | |
7772 | arg = TREE_VALUE (args); | |
7773 | args = TREE_CHAIN (args); | |
7774 | ||
7775 | if (arg == error_mark_node) | |
7776 | return 1; | |
7777 | if (arg == unknown_type_node) | |
34016c81 JM |
7778 | /* We can't deduce anything from this, but we might get all the |
7779 | template args from other function args. */ | |
7780 | continue; | |
b7484fbe | 7781 | |
03e70705 JM |
7782 | /* Conversions will be performed on a function argument that |
7783 | corresponds with a function parameter that contains only | |
7784 | non-deducible template parameters and explicitly specified | |
7785 | template parameters. */ | |
7786 | if (! uses_template_parms (parm)) | |
b7484fbe | 7787 | { |
03e70705 JM |
7788 | tree type; |
7789 | ||
2f939d94 | 7790 | if (!TYPE_P (arg)) |
03e70705 JM |
7791 | type = TREE_TYPE (arg); |
7792 | else | |
7793 | { | |
7794 | type = arg; | |
7795 | arg = NULL_TREE; | |
7796 | } | |
7797 | ||
830bfa74 | 7798 | if (strict == DEDUCE_EXACT) |
03e70705 | 7799 | { |
3bfdc719 | 7800 | if (same_type_p (parm, type)) |
03e70705 JM |
7801 | continue; |
7802 | } | |
03e70705 | 7803 | else |
343c89cd JM |
7804 | /* It might work; we shouldn't check now, because we might |
7805 | get into infinite recursion. Overload resolution will | |
7806 | handle it. */ | |
7807 | continue; | |
03e70705 | 7808 | |
b7484fbe MS |
7809 | return 1; |
7810 | } | |
7811 | ||
2f939d94 | 7812 | if (!TYPE_P (arg)) |
8d08fdba MS |
7813 | { |
7814 | my_friendly_assert (TREE_TYPE (arg) != NULL_TREE, 293); | |
34016c81 | 7815 | if (type_unknown_p (arg)) |
28cbf42c | 7816 | { |
34016c81 JM |
7817 | /* [temp.deduct.type] A template-argument can be deduced from |
7818 | a pointer to function or pointer to member function | |
7819 | argument if the set of overloaded functions does not | |
7820 | contain function templates and at most one of a set of | |
7821 | overloaded functions provides a unique match. */ | |
7822 | ||
7823 | if (resolve_overloaded_unification | |
4393e105 | 7824 | (tparms, targs, parm, arg, strict, sub_strict) |
34016c81 JM |
7825 | != 0) |
7826 | return 1; | |
7827 | continue; | |
28cbf42c | 7828 | } |
8d08fdba MS |
7829 | arg = TREE_TYPE (arg); |
7830 | } | |
4393e105 | 7831 | |
830bfa74 MM |
7832 | if (!subr) |
7833 | maybe_adjust_types_for_deduction (strict, &parm, &arg); | |
db5ae43f | 7834 | |
4393e105 | 7835 | switch (unify (tparms, targs, parm, arg, sub_strict)) |
8d08fdba MS |
7836 | { |
7837 | case 0: | |
7838 | break; | |
7839 | case 1: | |
7840 | return 1; | |
7841 | } | |
7842 | } | |
7843 | /* Fail if we've reached the end of the parm list, and more args | |
7844 | are present, and the parm list isn't variadic. */ | |
7845 | if (args && args != void_list_node && parms == void_list_node) | |
7846 | return 1; | |
7847 | /* Fail if parms are left and they don't have default values. */ | |
7848 | if (parms | |
7849 | && parms != void_list_node | |
7850 | && TREE_PURPOSE (parms) == NULL_TREE) | |
7851 | return 1; | |
7852 | if (!subr) | |
7853 | for (i = 0; i < ntparms; i++) | |
050367a3 | 7854 | if (TREE_VEC_ELT (targs, i) == NULL_TREE) |
8d08fdba | 7855 | { |
386b8a85 | 7856 | if (!allow_incomplete) |
8251199e | 7857 | error ("incomplete type unification"); |
8d08fdba MS |
7858 | return 2; |
7859 | } | |
7860 | return 0; | |
7861 | } | |
7862 | ||
34016c81 JM |
7863 | /* Subroutine of type_unification_real. Args are like the variables at the |
7864 | call site. ARG is an overloaded function (or template-id); we try | |
7865 | deducing template args from each of the overloads, and if only one | |
7866 | succeeds, we go with that. Modifies TARGS and returns 0 on success. */ | |
7867 | ||
7868 | static int | |
7869 | resolve_overloaded_unification (tparms, targs, parm, arg, strict, | |
4393e105 | 7870 | sub_strict) |
34016c81 JM |
7871 | tree tparms, targs, parm, arg; |
7872 | unification_kind_t strict; | |
7873 | int sub_strict; | |
34016c81 JM |
7874 | { |
7875 | tree tempargs = copy_node (targs); | |
7876 | int good = 0; | |
7877 | ||
7878 | if (TREE_CODE (arg) == ADDR_EXPR) | |
7879 | arg = TREE_OPERAND (arg, 0); | |
9f3d9e46 | 7880 | |
d8f8dca1 MM |
7881 | if (TREE_CODE (arg) == COMPONENT_REF) |
7882 | /* Handle `&x' where `x' is some static or non-static member | |
7883 | function name. */ | |
7884 | arg = TREE_OPERAND (arg, 1); | |
7885 | ||
05e0b2f4 JM |
7886 | if (TREE_CODE (arg) == OFFSET_REF) |
7887 | arg = TREE_OPERAND (arg, 1); | |
7888 | ||
9f3d9e46 JM |
7889 | /* Strip baselink information. */ |
7890 | while (TREE_CODE (arg) == TREE_LIST) | |
7891 | arg = TREE_VALUE (arg); | |
7892 | ||
34016c81 JM |
7893 | if (TREE_CODE (arg) == TEMPLATE_ID_EXPR) |
7894 | { | |
7895 | /* If we got some explicit template args, we need to plug them into | |
7896 | the affected templates before we try to unify, in case the | |
7897 | explicit args will completely resolve the templates in question. */ | |
7898 | ||
7899 | tree expl_subargs = TREE_OPERAND (arg, 1); | |
7900 | arg = TREE_OPERAND (arg, 0); | |
7901 | ||
7902 | for (; arg; arg = OVL_NEXT (arg)) | |
7903 | { | |
7904 | tree fn = OVL_CURRENT (arg); | |
7905 | tree subargs, elem; | |
7906 | ||
7907 | if (TREE_CODE (fn) != TEMPLATE_DECL) | |
7908 | continue; | |
7909 | ||
17aec3eb RK |
7910 | subargs = get_bindings_overload (fn, DECL_TEMPLATE_RESULT (fn), |
7911 | expl_subargs); | |
34016c81 JM |
7912 | if (subargs) |
7913 | { | |
4393e105 MM |
7914 | elem = tsubst (TREE_TYPE (fn), subargs, /*complain=*/0, |
7915 | NULL_TREE); | |
d8f8dca1 MM |
7916 | if (TREE_CODE (elem) == METHOD_TYPE) |
7917 | elem = build_ptrmemfunc_type (build_pointer_type (elem)); | |
e97e5263 | 7918 | good += try_one_overload (tparms, targs, tempargs, parm, elem, |
4393e105 | 7919 | strict, sub_strict); |
34016c81 JM |
7920 | } |
7921 | } | |
7922 | } | |
7923 | else if (TREE_CODE (arg) == OVERLOAD) | |
7924 | { | |
7925 | for (; arg; arg = OVL_NEXT (arg)) | |
d8f8dca1 MM |
7926 | { |
7927 | tree type = TREE_TYPE (OVL_CURRENT (arg)); | |
7928 | if (TREE_CODE (type) == METHOD_TYPE) | |
7929 | type = build_ptrmemfunc_type (build_pointer_type (type)); | |
7930 | good += try_one_overload (tparms, targs, tempargs, parm, | |
7931 | type, | |
4393e105 | 7932 | strict, sub_strict); |
d8f8dca1 | 7933 | } |
34016c81 JM |
7934 | } |
7935 | else | |
7936 | my_friendly_abort (981006); | |
7937 | ||
7938 | /* [temp.deduct.type] A template-argument can be deduced from a pointer | |
7939 | to function or pointer to member function argument if the set of | |
7940 | overloaded functions does not contain function templates and at most | |
7941 | one of a set of overloaded functions provides a unique match. | |
7942 | ||
7943 | So if we found multiple possibilities, we return success but don't | |
7944 | deduce anything. */ | |
7945 | ||
7946 | if (good == 1) | |
7947 | { | |
7948 | int i = TREE_VEC_LENGTH (targs); | |
7949 | for (; i--; ) | |
7950 | if (TREE_VEC_ELT (tempargs, i)) | |
7951 | TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (tempargs, i); | |
7952 | } | |
7953 | if (good) | |
7954 | return 0; | |
7955 | ||
7956 | return 1; | |
7957 | } | |
7958 | ||
7959 | /* Subroutine of resolve_overloaded_unification; does deduction for a single | |
7960 | overload. Fills TARGS with any deduced arguments, or error_mark_node if | |
7961 | different overloads deduce different arguments for a given parm. | |
7962 | Returns 1 on success. */ | |
7963 | ||
7964 | static int | |
e97e5263 | 7965 | try_one_overload (tparms, orig_targs, targs, parm, arg, strict, |
4393e105 | 7966 | sub_strict) |
e97e5263 | 7967 | tree tparms, orig_targs, targs, parm, arg; |
34016c81 JM |
7968 | unification_kind_t strict; |
7969 | int sub_strict; | |
34016c81 JM |
7970 | { |
7971 | int nargs; | |
7972 | tree tempargs; | |
7973 | int i; | |
7974 | ||
7975 | /* [temp.deduct.type] A template-argument can be deduced from a pointer | |
7976 | to function or pointer to member function argument if the set of | |
7977 | overloaded functions does not contain function templates and at most | |
7978 | one of a set of overloaded functions provides a unique match. | |
7979 | ||
7980 | So if this is a template, just return success. */ | |
7981 | ||
7982 | if (uses_template_parms (arg)) | |
7983 | return 1; | |
7984 | ||
7985 | maybe_adjust_types_for_deduction (strict, &parm, &arg); | |
7986 | ||
7987 | /* We don't copy orig_targs for this because if we have already deduced | |
7988 | some template args from previous args, unify would complain when we | |
7989 | try to deduce a template parameter for the same argument, even though | |
7990 | there isn't really a conflict. */ | |
7991 | nargs = TREE_VEC_LENGTH (targs); | |
f31c0a32 | 7992 | tempargs = make_tree_vec (nargs); |
34016c81 | 7993 | |
4393e105 | 7994 | if (unify (tparms, tempargs, parm, arg, sub_strict) != 0) |
34016c81 JM |
7995 | return 0; |
7996 | ||
7997 | /* First make sure we didn't deduce anything that conflicts with | |
e97e5263 | 7998 | explicitly specified args. */ |
34016c81 JM |
7999 | for (i = nargs; i--; ) |
8000 | { | |
8001 | tree elt = TREE_VEC_ELT (tempargs, i); | |
e97e5263 | 8002 | tree oldelt = TREE_VEC_ELT (orig_targs, i); |
34016c81 JM |
8003 | |
8004 | if (elt == NULL_TREE) | |
8005 | continue; | |
8006 | else if (uses_template_parms (elt)) | |
8007 | { | |
8008 | /* Since we're unifying against ourselves, we will fill in template | |
8009 | args used in the function parm list with our own template parms. | |
8010 | Discard them. */ | |
8011 | TREE_VEC_ELT (tempargs, i) = NULL_TREE; | |
8012 | continue; | |
8013 | } | |
8014 | else if (oldelt && ! template_args_equal (oldelt, elt)) | |
8015 | return 0; | |
8016 | } | |
8017 | ||
8018 | for (i = nargs; i--; ) | |
8019 | { | |
8020 | tree elt = TREE_VEC_ELT (tempargs, i); | |
8021 | ||
8022 | if (elt) | |
8023 | TREE_VEC_ELT (targs, i) = elt; | |
8024 | } | |
8025 | ||
8026 | return 1; | |
8027 | } | |
8028 | ||
4393e105 MM |
8029 | /* PARM is a template class (perhaps with unbound template |
8030 | parameters). ARG is a fully instantiated type. If ARG can be | |
8031 | bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and | |
8032 | TARGS are as for unify. */ | |
fcfb9f96 MM |
8033 | |
8034 | static tree | |
4393e105 | 8035 | try_class_unification (tparms, targs, parm, arg) |
fcfb9f96 MM |
8036 | tree tparms; |
8037 | tree targs; | |
4393e105 MM |
8038 | tree parm; |
8039 | tree arg; | |
8040 | { | |
8041 | int i; | |
8042 | tree copy_of_targs; | |
8043 | ||
8044 | if (!CLASSTYPE_TEMPLATE_INFO (arg) | |
8045 | || CLASSTYPE_TI_TEMPLATE (arg) != CLASSTYPE_TI_TEMPLATE (parm)) | |
8046 | return NULL_TREE; | |
8047 | ||
8048 | /* We need to make a new template argument vector for the call to | |
8049 | unify. If we used TARGS, we'd clutter it up with the result of | |
8050 | the attempted unification, even if this class didn't work out. | |
8051 | We also don't want to commit ourselves to all the unifications | |
8052 | we've already done, since unification is supposed to be done on | |
8053 | an argument-by-argument basis. In other words, consider the | |
8054 | following pathological case: | |
8055 | ||
8056 | template <int I, int J, int K> | |
8057 | struct S {}; | |
8058 | ||
8059 | template <int I, int J> | |
8060 | struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {}; | |
8061 | ||
8062 | template <int I, int J, int K> | |
8063 | void f(S<I, J, K>, S<I, I, I>); | |
8064 | ||
8065 | void g() { | |
8066 | S<0, 0, 0> s0; | |
8067 | S<0, 1, 2> s2; | |
8068 | ||
8069 | f(s0, s2); | |
8070 | } | |
8071 | ||
8072 | Now, by the time we consider the unification involving `s2', we | |
8073 | already know that we must have `f<0, 0, 0>'. But, even though | |
8074 | `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is not legal | |
8075 | because there are two ways to unify base classes of S<0, 1, 2> | |
8076 | with S<I, I, I>. If we kept the already deduced knowledge, we | |
8077 | would reject the possibility I=1. */ | |
f31c0a32 | 8078 | copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs)); |
4393e105 MM |
8079 | i = unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm), |
8080 | CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE); | |
4393e105 MM |
8081 | |
8082 | /* If unification failed, we're done. */ | |
8083 | if (i != 0) | |
8084 | return NULL_TREE; | |
8085 | else | |
8086 | return arg; | |
8087 | } | |
8088 | ||
8089 | /* Subroutine of get_template_base. RVAL, if non-NULL, is a base we | |
8090 | have alreay discovered to be satisfactory. ARG_BINFO is the binfo | |
8091 | for the base class of ARG that we are currently examining. */ | |
8092 | ||
8093 | static tree | |
8094 | get_template_base_recursive (tparms, targs, parm, | |
8095 | arg_binfo, rval, flags) | |
8096 | tree tparms; | |
8097 | tree targs; | |
8098 | tree arg_binfo; | |
fcfb9f96 | 8099 | tree rval; |
4393e105 MM |
8100 | tree parm; |
8101 | int flags; | |
fcfb9f96 MM |
8102 | { |
8103 | tree binfos; | |
8104 | int i, n_baselinks; | |
4393e105 | 8105 | tree arg = BINFO_TYPE (arg_binfo); |
fcfb9f96 | 8106 | |
4393e105 | 8107 | if (!(flags & GTB_IGNORE_TYPE)) |
fcfb9f96 | 8108 | { |
4393e105 MM |
8109 | tree r = try_class_unification (tparms, targs, |
8110 | parm, arg); | |
fcfb9f96 | 8111 | |
4393e105 | 8112 | /* If there is more than one satisfactory baseclass, then: |
fcfb9f96 | 8113 | |
4393e105 MM |
8114 | [temp.deduct.call] |
8115 | ||
8116 | If they yield more than one possible deduced A, the type | |
8117 | deduction fails. | |
8118 | ||
8119 | applies. */ | |
8120 | if (r && rval && !same_type_p (r, rval)) | |
8121 | return error_mark_node; | |
8122 | else if (r) | |
8123 | rval = r; | |
fcfb9f96 MM |
8124 | } |
8125 | ||
4393e105 | 8126 | binfos = BINFO_BASETYPES (arg_binfo); |
fcfb9f96 MM |
8127 | n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0; |
8128 | ||
8129 | /* Process base types. */ | |
8130 | for (i = 0; i < n_baselinks; i++) | |
8131 | { | |
8132 | tree base_binfo = TREE_VEC_ELT (binfos, i); | |
4393e105 | 8133 | int this_virtual; |
fcfb9f96 | 8134 | |
4393e105 MM |
8135 | /* Skip this base, if we've already seen it. */ |
8136 | if (BINFO_MARKED (base_binfo)) | |
8137 | continue; | |
8138 | ||
8139 | this_virtual = | |
8140 | (flags & GTB_VIA_VIRTUAL) || TREE_VIA_VIRTUAL (base_binfo); | |
8141 | ||
8142 | /* When searching for a non-virtual, we cannot mark virtually | |
8143 | found binfos. */ | |
8144 | if (! this_virtual) | |
8145 | SET_BINFO_MARKED (base_binfo); | |
8146 | ||
8147 | rval = get_template_base_recursive (tparms, targs, | |
8148 | parm, | |
8149 | base_binfo, | |
8150 | rval, | |
8151 | GTB_VIA_VIRTUAL * this_virtual); | |
8152 | ||
8153 | /* If we discovered more than one matching base class, we can | |
8154 | stop now. */ | |
8155 | if (rval == error_mark_node) | |
8156 | return error_mark_node; | |
fcfb9f96 MM |
8157 | } |
8158 | ||
8159 | return rval; | |
8160 | } | |
8161 | ||
4393e105 MM |
8162 | /* Given a template type PARM and a class type ARG, find the unique |
8163 | base type in ARG that is an instance of PARM. We do not examine | |
8164 | ARG itself; only its base-classes. If there is no appropriate base | |
8165 | class, return NULL_TREE. If there is more than one, return | |
8166 | error_mark_node. PARM may be the type of a partial specialization, | |
8167 | as well as a plain template type. Used by unify. */ | |
fcfb9f96 MM |
8168 | |
8169 | static tree | |
4393e105 | 8170 | get_template_base (tparms, targs, parm, arg) |
fcfb9f96 MM |
8171 | tree tparms; |
8172 | tree targs; | |
4393e105 MM |
8173 | tree parm; |
8174 | tree arg; | |
fcfb9f96 | 8175 | { |
4393e105 MM |
8176 | tree rval; |
8177 | tree arg_binfo; | |
fcfb9f96 | 8178 | |
4393e105 MM |
8179 | my_friendly_assert (IS_AGGR_TYPE_CODE (TREE_CODE (arg)), 92); |
8180 | ||
8181 | arg_binfo = TYPE_BINFO (complete_type (arg)); | |
8182 | rval = get_template_base_recursive (tparms, targs, | |
8183 | parm, arg_binfo, | |
8184 | NULL_TREE, | |
8185 | GTB_IGNORE_TYPE); | |
fcfb9f96 | 8186 | |
4393e105 MM |
8187 | /* Since get_template_base_recursive marks the bases classes, we |
8188 | must unmark them here. */ | |
d6479fe7 | 8189 | dfs_walk (arg_binfo, dfs_unmark, markedp, 0); |
fcfb9f96 MM |
8190 | |
8191 | return rval; | |
8192 | } | |
8193 | ||
db2767b6 MM |
8194 | /* Returns the level of DECL, which declares a template parameter. */ |
8195 | ||
e9659ab0 | 8196 | static int |
db2767b6 MM |
8197 | template_decl_level (decl) |
8198 | tree decl; | |
8199 | { | |
8200 | switch (TREE_CODE (decl)) | |
8201 | { | |
8202 | case TYPE_DECL: | |
8203 | case TEMPLATE_DECL: | |
8204 | return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl)); | |
8205 | ||
8206 | case PARM_DECL: | |
8207 | return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl)); | |
8208 | ||
8209 | default: | |
8210 | my_friendly_abort (0); | |
5ffe581d | 8211 | return 0; |
db2767b6 MM |
8212 | } |
8213 | } | |
8214 | ||
830bfa74 MM |
8215 | /* Decide whether ARG can be unified with PARM, considering only the |
8216 | cv-qualifiers of each type, given STRICT as documented for unify. | |
8217 | Returns non-zero iff the unification is OK on that basis.*/ | |
e92cc029 | 8218 | |
e9659ab0 | 8219 | static int |
830bfa74 MM |
8220 | check_cv_quals_for_unify (strict, arg, parm) |
8221 | int strict; | |
8222 | tree arg; | |
8223 | tree parm; | |
8224 | { | |
ef637255 MM |
8225 | if (!(strict & UNIFY_ALLOW_MORE_CV_QUAL) |
8226 | && !at_least_as_qualified_p (arg, parm)) | |
8227 | return 0; | |
8228 | ||
8229 | if (!(strict & UNIFY_ALLOW_LESS_CV_QUAL) | |
8230 | && !at_least_as_qualified_p (parm, arg)) | |
8231 | return 0; | |
8232 | ||
ef637255 | 8233 | return 1; |
830bfa74 MM |
8234 | } |
8235 | ||
8236 | /* Takes parameters as for type_unification. Returns 0 if the | |
8237 | type deduction suceeds, 1 otherwise. The parameter STRICT is a | |
8238 | bitwise or of the following flags: | |
8239 | ||
8240 | UNIFY_ALLOW_NONE: | |
8241 | Require an exact match between PARM and ARG. | |
8242 | UNIFY_ALLOW_MORE_CV_QUAL: | |
8243 | Allow the deduced ARG to be more cv-qualified than ARG. | |
8244 | UNIFY_ALLOW_LESS_CV_QUAL: | |
8245 | Allow the deduced ARG to be less cv-qualified than ARG. | |
8246 | UNIFY_ALLOW_DERIVED: | |
8247 | Allow the deduced ARG to be a template base class of ARG, | |
8248 | or a pointer to a template base class of the type pointed to by | |
161c12b0 JM |
8249 | ARG. |
8250 | UNIFY_ALLOW_INTEGER: | |
8251 | Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX | |
8252 | case for more information. */ | |
830bfa74 | 8253 | |
e9659ab0 | 8254 | static int |
4393e105 | 8255 | unify (tparms, targs, parm, arg, strict) |
050367a3 | 8256 | tree tparms, targs, parm, arg; |
830bfa74 | 8257 | int strict; |
8d08fdba MS |
8258 | { |
8259 | int idx; | |
050367a3 | 8260 | tree targ; |
db2767b6 | 8261 | tree tparm; |
8d08fdba MS |
8262 | |
8263 | /* I don't think this will do the right thing with respect to types. | |
8264 | But the only case I've seen it in so far has been array bounds, where | |
8265 | signedness is the only information lost, and I think that will be | |
8266 | okay. */ | |
8267 | while (TREE_CODE (parm) == NOP_EXPR) | |
8268 | parm = TREE_OPERAND (parm, 0); | |
8269 | ||
8270 | if (arg == error_mark_node) | |
8271 | return 1; | |
8272 | if (arg == unknown_type_node) | |
34016c81 JM |
8273 | /* We can't deduce anything from this, but we might get all the |
8274 | template args from other function args. */ | |
8275 | return 0; | |
8276 | ||
db2767b6 | 8277 | /* If PARM uses template parameters, then we can't bail out here, |
6bdb985f | 8278 | even if ARG == PARM, since we won't record unifications for the |
db2767b6 MM |
8279 | template parameters. We might need them if we're trying to |
8280 | figure out which of two things is more specialized. */ | |
8281 | if (arg == parm && !uses_template_parms (parm)) | |
8d08fdba MS |
8282 | return 0; |
8283 | ||
830bfa74 MM |
8284 | /* Immediately reject some pairs that won't unify because of |
8285 | cv-qualification mismatches. */ | |
8286 | if (TREE_CODE (arg) == TREE_CODE (parm) | |
2f939d94 | 8287 | && TYPE_P (arg) |
830bfa74 MM |
8288 | /* We check the cv-qualifiers when unifying with template type |
8289 | parameters below. We want to allow ARG `const T' to unify with | |
8290 | PARM `T' for example, when computing which of two templates | |
8291 | is more specialized, for example. */ | |
8292 | && TREE_CODE (arg) != TEMPLATE_TYPE_PARM | |
8293 | && !check_cv_quals_for_unify (strict, arg, parm)) | |
49432171 JM |
8294 | return 1; |
8295 | ||
8d08fdba MS |
8296 | switch (TREE_CODE (parm)) |
8297 | { | |
2ca340ae JM |
8298 | case TYPENAME_TYPE: |
8299 | /* In a type which contains a nested-name-specifier, template | |
8300 | argument values cannot be deduced for template parameters used | |
8301 | within the nested-name-specifier. */ | |
8302 | return 0; | |
8303 | ||
8d08fdba | 8304 | case TEMPLATE_TYPE_PARM: |
73b0fce8 | 8305 | case TEMPLATE_TEMPLATE_PARM: |
db2767b6 MM |
8306 | tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0)); |
8307 | ||
8308 | if (TEMPLATE_TYPE_LEVEL (parm) | |
8309 | != template_decl_level (tparm)) | |
8310 | /* The PARM is not one we're trying to unify. Just check | |
8311 | to see if it matches ARG. */ | |
8312 | return (TREE_CODE (arg) == TREE_CODE (parm) | |
3bfdc719 | 8313 | && same_type_p (parm, arg)) ? 0 : 1; |
73b0fce8 | 8314 | idx = TEMPLATE_TYPE_IDX (parm); |
050367a3 | 8315 | targ = TREE_VEC_ELT (targs, idx); |
db2767b6 | 8316 | tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx)); |
050367a3 | 8317 | |
73b0fce8 | 8318 | /* Check for mixed types and values. */ |
db2767b6 MM |
8319 | if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM |
8320 | && TREE_CODE (tparm) != TYPE_DECL) | |
8321 | || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM | |
8322 | && TREE_CODE (tparm) != TEMPLATE_DECL)) | |
73b0fce8 KL |
8323 | return 1; |
8324 | ||
db2767b6 | 8325 | if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM) |
73b0fce8 | 8326 | { |
7ddedda4 | 8327 | if (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (parm)) |
db2767b6 MM |
8328 | { |
8329 | /* We arrive here when PARM does not involve template | |
8330 | specialization. */ | |
73b0fce8 | 8331 | |
db2767b6 MM |
8332 | /* ARG must be constructed from a template class. */ |
8333 | if (TREE_CODE (arg) != RECORD_TYPE || !CLASSTYPE_TEMPLATE_INFO (arg)) | |
8334 | return 1; | |
73b0fce8 | 8335 | |
db2767b6 | 8336 | { |
7ddedda4 MM |
8337 | tree parmtmpl = TYPE_TI_TEMPLATE (parm); |
8338 | tree parmvec = TYPE_TI_ARGS (parm); | |
db2767b6 MM |
8339 | tree argvec = CLASSTYPE_TI_ARGS (arg); |
8340 | tree argtmplvec | |
8341 | = DECL_INNERMOST_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (arg)); | |
8342 | int i; | |
8343 | ||
8344 | /* The parameter and argument roles have to be switched here | |
8345 | in order to handle default arguments properly. For example, | |
8346 | template<template <class> class TT> void f(TT<int>) | |
8347 | should be able to accept vector<int> which comes from | |
744fac59 | 8348 | template <class T, class Allocator = allocator> |
73b0fce8 KL |
8349 | class vector. */ |
8350 | ||
fcfcdfc8 | 8351 | if (coerce_template_parms (argtmplvec, parmvec, parmtmpl, 0, 1) |
db2767b6 MM |
8352 | == error_mark_node) |
8353 | return 1; | |
73b0fce8 | 8354 | |
6b9b6b15 JM |
8355 | /* Deduce arguments T, i from TT<T> or TT<i>. |
8356 | We check each element of PARMVEC and ARGVEC individually | |
8357 | rather than the whole TREE_VEC since they can have | |
8358 | different number of elements. */ | |
8359 | ||
db2767b6 MM |
8360 | for (i = 0; i < TREE_VEC_LENGTH (parmvec); ++i) |
8361 | { | |
8362 | tree t = TREE_VEC_ELT (parmvec, i); | |
73b0fce8 | 8363 | |
830bfa74 MM |
8364 | if (unify (tparms, targs, t, |
8365 | TREE_VEC_ELT (argvec, i), | |
4393e105 | 8366 | UNIFY_ALLOW_NONE)) |
db2767b6 MM |
8367 | return 1; |
8368 | } | |
73b0fce8 | 8369 | } |
db2767b6 MM |
8370 | arg = CLASSTYPE_TI_TEMPLATE (arg); |
8371 | } | |
8372 | } | |
8373 | else | |
8374 | { | |
830bfa74 MM |
8375 | /* If PARM is `const T' and ARG is only `int', we don't have |
8376 | a match unless we are allowing additional qualification. | |
8377 | If ARG is `const int' and PARM is just `T' that's OK; | |
8378 | that binds `const int' to `T'. */ | |
8379 | if (!check_cv_quals_for_unify (strict | UNIFY_ALLOW_LESS_CV_QUAL, | |
8380 | arg, parm)) | |
db2767b6 MM |
8381 | return 1; |
8382 | ||
830bfa74 MM |
8383 | /* Consider the case where ARG is `const volatile int' and |
8384 | PARM is `const T'. Then, T should be `volatile int'. */ | |
8385 | arg = | |
adecb3f4 MM |
8386 | cp_build_qualified_type_real (arg, |
8387 | CP_TYPE_QUALS (arg) | |
8388 | & ~CP_TYPE_QUALS (parm), | |
8389 | /*complain=*/0); | |
8390 | if (arg == error_mark_node) | |
8391 | return 1; | |
73b0fce8 KL |
8392 | } |
8393 | ||
8394 | /* Simple cases: Value already set, does match or doesn't. */ | |
4393e105 | 8395 | if (targ != NULL_TREE && same_type_p (targ, arg)) |
73b0fce8 | 8396 | return 0; |
050367a3 | 8397 | else if (targ) |
73b0fce8 | 8398 | return 1; |
61cd552e MM |
8399 | |
8400 | /* Make sure that ARG is not a variable-sized array. (Note that | |
8401 | were talking about variable-sized arrays (like `int[n]'), | |
8402 | rather than arrays of unknown size (like `int[]').) We'll | |
8403 | get very confused by such a type since the bound of the array | |
8404 | will not be computable in an instantiation. Besides, such | |
8405 | types are not allowed in ISO C++, so we can do as we please | |
8406 | here. */ | |
8407 | if (TREE_CODE (arg) == ARRAY_TYPE | |
8408 | && !uses_template_parms (arg) | |
8409 | && (TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (arg))) | |
8410 | != INTEGER_CST)) | |
8411 | return 1; | |
8412 | ||
050367a3 | 8413 | TREE_VEC_ELT (targs, idx) = arg; |
73b0fce8 KL |
8414 | return 0; |
8415 | ||
f84b4be9 | 8416 | case TEMPLATE_PARM_INDEX: |
db2767b6 MM |
8417 | tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0)); |
8418 | ||
8419 | if (TEMPLATE_PARM_LEVEL (parm) | |
8420 | != template_decl_level (tparm)) | |
8421 | /* The PARM is not one we're trying to unify. Just check | |
8422 | to see if it matches ARG. */ | |
8423 | return (TREE_CODE (arg) == TREE_CODE (parm) | |
67ffc812 | 8424 | && cp_tree_equal (parm, arg) > 0) ? 0 : 1; |
db2767b6 | 8425 | |
f84b4be9 | 8426 | idx = TEMPLATE_PARM_IDX (parm); |
050367a3 | 8427 | targ = TREE_VEC_ELT (targs, idx); |
db2767b6 | 8428 | |
050367a3 | 8429 | if (targ) |
8d08fdba | 8430 | { |
67ffc812 | 8431 | int i = (cp_tree_equal (targ, arg) > 0); |
312e7d50 JM |
8432 | if (i == 1) |
8433 | return 0; | |
8434 | else if (i == 0) | |
8435 | return 1; | |
8436 | else | |
8437 | my_friendly_abort (42); | |
8d08fdba | 8438 | } |
8d08fdba | 8439 | |
161c12b0 JM |
8440 | /* [temp.deduct.type] If, in the declaration of a function template |
8441 | with a non-type template-parameter, the non-type | |
8442 | template-parameter is used in an expression in the function | |
8443 | parameter-list and, if the corresponding template-argument is | |
8444 | deduced, the template-argument type shall match the type of the | |
8445 | template-parameter exactly, except that a template-argument | |
8446 | deduced from an array bound may be of any integral type. */ | |
8447 | if (same_type_p (TREE_TYPE (arg), TREE_TYPE (parm))) | |
8448 | /* OK */; | |
8449 | else if ((strict & UNIFY_ALLOW_INTEGER) | |
8450 | && (TREE_CODE (TREE_TYPE (parm)) == INTEGER_TYPE | |
8451 | || TREE_CODE (TREE_TYPE (parm)) == BOOLEAN_TYPE)) | |
8452 | /* OK */; | |
8453 | else | |
8454 | return 1; | |
8455 | ||
2a1e9fdd | 8456 | TREE_VEC_ELT (targs, idx) = arg; |
8d08fdba MS |
8457 | return 0; |
8458 | ||
8459 | case POINTER_TYPE: | |
830bfa74 MM |
8460 | { |
8461 | int sub_strict; | |
4ac14744 | 8462 | |
830bfa74 MM |
8463 | if (TREE_CODE (arg) != POINTER_TYPE) |
8464 | return 1; | |
8465 | ||
8466 | /* [temp.deduct.call] | |
8467 | ||
8468 | A can be another pointer or pointer to member type that can | |
8469 | be converted to the deduced A via a qualification | |
8470 | conversion (_conv.qual_). | |
8471 | ||
8472 | We pass down STRICT here rather than UNIFY_ALLOW_NONE. | |
8473 | This will allow for additional cv-qualification of the | |
8474 | pointed-to types if appropriate. In general, this is a bit | |
8475 | too generous; we are only supposed to allow qualification | |
8476 | conversions and this method will allow an ARG of char** and | |
8477 | a deduced ARG of const char**. However, overload | |
8478 | resolution will subsequently invalidate the candidate, so | |
8479 | this is probably OK. */ | |
8480 | sub_strict = strict; | |
8481 | ||
a7a64a77 | 8482 | if (TREE_CODE (TREE_TYPE (arg)) != RECORD_TYPE) |
830bfa74 MM |
8483 | /* The derived-to-base conversion only persists through one |
8484 | level of pointers. */ | |
8485 | sub_strict &= ~UNIFY_ALLOW_DERIVED; | |
8486 | ||
a7a64a77 MM |
8487 | return unify (tparms, targs, TREE_TYPE (parm), |
8488 | TREE_TYPE (arg), sub_strict); | |
830bfa74 | 8489 | } |
8d08fdba MS |
8490 | |
8491 | case REFERENCE_TYPE: | |
830bfa74 MM |
8492 | if (TREE_CODE (arg) != REFERENCE_TYPE) |
8493 | return 1; | |
8494 | return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg), | |
4393e105 | 8495 | UNIFY_ALLOW_NONE); |
8d08fdba MS |
8496 | |
8497 | case ARRAY_TYPE: | |
8498 | if (TREE_CODE (arg) != ARRAY_TYPE) | |
8499 | return 1; | |
3042d5be MM |
8500 | if ((TYPE_DOMAIN (parm) == NULL_TREE) |
8501 | != (TYPE_DOMAIN (arg) == NULL_TREE)) | |
8502 | return 1; | |
8503 | if (TYPE_DOMAIN (parm) != NULL_TREE | |
830bfa74 | 8504 | && unify (tparms, targs, TYPE_DOMAIN (parm), |
4393e105 | 8505 | TYPE_DOMAIN (arg), UNIFY_ALLOW_NONE) != 0) |
8d08fdba | 8506 | return 1; |
830bfa74 | 8507 | return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg), |
4393e105 | 8508 | UNIFY_ALLOW_NONE); |
8d08fdba MS |
8509 | |
8510 | case REAL_TYPE: | |
37c46b43 | 8511 | case COMPLEX_TYPE: |
8d08fdba | 8512 | case INTEGER_TYPE: |
42976354 | 8513 | case BOOLEAN_TYPE: |
5ad5a526 | 8514 | case VOID_TYPE: |
f376e137 MS |
8515 | if (TREE_CODE (arg) != TREE_CODE (parm)) |
8516 | return 1; | |
8517 | ||
514a1f18 JM |
8518 | if (TREE_CODE (parm) == INTEGER_TYPE |
8519 | && TREE_CODE (TYPE_MAX_VALUE (parm)) != INTEGER_CST) | |
8d08fdba MS |
8520 | { |
8521 | if (TYPE_MIN_VALUE (parm) && TYPE_MIN_VALUE (arg) | |
830bfa74 | 8522 | && unify (tparms, targs, TYPE_MIN_VALUE (parm), |
4393e105 | 8523 | TYPE_MIN_VALUE (arg), UNIFY_ALLOW_INTEGER)) |
8d08fdba MS |
8524 | return 1; |
8525 | if (TYPE_MAX_VALUE (parm) && TYPE_MAX_VALUE (arg) | |
830bfa74 | 8526 | && unify (tparms, targs, TYPE_MAX_VALUE (parm), |
4393e105 | 8527 | TYPE_MAX_VALUE (arg), UNIFY_ALLOW_INTEGER)) |
8d08fdba MS |
8528 | return 1; |
8529 | } | |
9edc3913 | 8530 | /* We have already checked cv-qualification at the top of the |
514a1f18 | 8531 | function. */ |
9edc3913 | 8532 | else if (!same_type_ignoring_top_level_qualifiers_p (arg, parm)) |
ca79f85d JM |
8533 | return 1; |
8534 | ||
8d08fdba MS |
8535 | /* As far as unification is concerned, this wins. Later checks |
8536 | will invalidate it if necessary. */ | |
8537 | return 0; | |
8538 | ||
8539 | /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */ | |
bd6dd845 | 8540 | /* Type INTEGER_CST can come from ordinary constant template args. */ |
8d08fdba | 8541 | case INTEGER_CST: |
bd6dd845 MS |
8542 | while (TREE_CODE (arg) == NOP_EXPR) |
8543 | arg = TREE_OPERAND (arg, 0); | |
8544 | ||
8d08fdba MS |
8545 | if (TREE_CODE (arg) != INTEGER_CST) |
8546 | return 1; | |
8547 | return !tree_int_cst_equal (parm, arg); | |
8548 | ||
8d08fdba MS |
8549 | case TREE_VEC: |
8550 | { | |
8551 | int i; | |
8552 | if (TREE_CODE (arg) != TREE_VEC) | |
8553 | return 1; | |
8554 | if (TREE_VEC_LENGTH (parm) != TREE_VEC_LENGTH (arg)) | |
8555 | return 1; | |
8556 | for (i = TREE_VEC_LENGTH (parm) - 1; i >= 0; i--) | |
830bfa74 | 8557 | if (unify (tparms, targs, |
8d08fdba | 8558 | TREE_VEC_ELT (parm, i), TREE_VEC_ELT (arg, i), |
4393e105 | 8559 | UNIFY_ALLOW_NONE)) |
8d08fdba MS |
8560 | return 1; |
8561 | return 0; | |
8562 | } | |
8563 | ||
8d08fdba | 8564 | case RECORD_TYPE: |
f181d4ae | 8565 | case UNION_TYPE: |
f181d4ae | 8566 | if (TREE_CODE (arg) != TREE_CODE (parm)) |
a4443a08 | 8567 | return 1; |
830bfa74 | 8568 | |
a7a64a77 MM |
8569 | if (TYPE_PTRMEMFUNC_P (parm)) |
8570 | { | |
8571 | if (!TYPE_PTRMEMFUNC_P (arg)) | |
8572 | return 1; | |
8573 | ||
8574 | return unify (tparms, targs, | |
8575 | TYPE_PTRMEMFUNC_FN_TYPE (parm), | |
8576 | TYPE_PTRMEMFUNC_FN_TYPE (arg), | |
8577 | strict); | |
8578 | } | |
8579 | ||
5db698f6 | 8580 | if (CLASSTYPE_TEMPLATE_INFO (parm)) |
5566b478 | 8581 | { |
6467930b | 8582 | tree t = NULL_TREE; |
4393e105 | 8583 | |
830bfa74 | 8584 | if (strict & UNIFY_ALLOW_DERIVED) |
4393e105 MM |
8585 | { |
8586 | /* First, we try to unify the PARM and ARG directly. */ | |
8587 | t = try_class_unification (tparms, targs, | |
8588 | parm, arg); | |
8589 | ||
8590 | if (!t) | |
8591 | { | |
8592 | /* Fallback to the special case allowed in | |
8593 | [temp.deduct.call]: | |
8594 | ||
8595 | If P is a class, and P has the form | |
8596 | template-id, then A can be a derived class of | |
8597 | the deduced A. Likewise, if P is a pointer to | |
8598 | a class of the form template-id, A can be a | |
8599 | pointer to a derived class pointed to by the | |
8600 | deduced A. */ | |
8601 | t = get_template_base (tparms, targs, | |
8602 | parm, arg); | |
8603 | ||
8604 | if (! t || t == error_mark_node) | |
8605 | return 1; | |
8606 | } | |
8607 | } | |
6df47b06 | 8608 | else if (CLASSTYPE_TEMPLATE_INFO (arg) |
9fbf56f7 MM |
8609 | && (CLASSTYPE_TI_TEMPLATE (parm) |
8610 | == CLASSTYPE_TI_TEMPLATE (arg))) | |
6df47b06 MM |
8611 | /* Perhaps PARM is something like S<U> and ARG is S<int>. |
8612 | Then, we should unify `int' and `U'. */ | |
6467930b | 8613 | t = arg; |
4393e105 MM |
8614 | else |
8615 | /* There's no chance of unication succeeding. */ | |
5566b478 | 8616 | return 1; |
6467930b | 8617 | |
830bfa74 | 8618 | return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm), |
4393e105 | 8619 | CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE); |
5566b478 | 8620 | } |
9edc3913 | 8621 | else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg)) |
5566b478 | 8622 | return 1; |
a4443a08 | 8623 | return 0; |
8d08fdba MS |
8624 | |
8625 | case METHOD_TYPE: | |
8d08fdba | 8626 | case FUNCTION_TYPE: |
830bfa74 | 8627 | if (TREE_CODE (arg) != TREE_CODE (parm)) |
8d08fdba | 8628 | return 1; |
830bfa74 MM |
8629 | |
8630 | if (unify (tparms, targs, TREE_TYPE (parm), | |
4393e105 | 8631 | TREE_TYPE (arg), UNIFY_ALLOW_NONE)) |
28cbf42c | 8632 | return 1; |
386b8a85 | 8633 | return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm), |
3b3ba9f0 | 8634 | TYPE_ARG_TYPES (arg), 1, |
4393e105 | 8635 | DEDUCE_EXACT, 0); |
a4443a08 MS |
8636 | |
8637 | case OFFSET_TYPE: | |
8638 | if (TREE_CODE (arg) != OFFSET_TYPE) | |
8639 | return 1; | |
830bfa74 | 8640 | if (unify (tparms, targs, TYPE_OFFSET_BASETYPE (parm), |
4393e105 | 8641 | TYPE_OFFSET_BASETYPE (arg), UNIFY_ALLOW_NONE)) |
a4443a08 | 8642 | return 1; |
830bfa74 | 8643 | return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg), |
4393e105 | 8644 | strict); |
a4443a08 | 8645 | |
f62dbf03 | 8646 | case CONST_DECL: |
830bfa74 | 8647 | if (arg != decl_constant_value (parm)) |
f62dbf03 JM |
8648 | return 1; |
8649 | return 0; | |
8650 | ||
027905b4 KL |
8651 | case TEMPLATE_DECL: |
8652 | /* Matched cases are handled by the ARG == PARM test above. */ | |
8653 | return 1; | |
8654 | ||
53929c47 JM |
8655 | case MINUS_EXPR: |
8656 | if (TREE_CODE (TREE_OPERAND (parm, 1)) == INTEGER_CST) | |
8657 | { | |
8658 | /* We handle this case specially, since it comes up with | |
8659 | arrays. In particular, something like: | |
8660 | ||
8661 | template <int N> void f(int (&x)[N]); | |
8662 | ||
8663 | Here, we are trying to unify the range type, which | |
8664 | looks like [0 ... (N - 1)]. */ | |
8665 | tree t, t1, t2; | |
8666 | t1 = TREE_OPERAND (parm, 0); | |
8667 | t2 = TREE_OPERAND (parm, 1); | |
8668 | ||
24e2e58e | 8669 | t = fold (build (PLUS_EXPR, integer_type_node, arg, t2)); |
53929c47 | 8670 | |
4393e105 | 8671 | return unify (tparms, targs, t1, t, strict); |
53929c47 JM |
8672 | } |
8673 | /* else fall through */ | |
8674 | ||
8d08fdba | 8675 | default: |
050367a3 | 8676 | if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (parm)))) |
4393e105 MM |
8677 | /* We're looking at an expression. This can happen with |
8678 | something like: | |
8679 | ||
8680 | template <int I> | |
8681 | void foo(S<I>, S<I + 2>); | |
050367a3 | 8682 | |
4393e105 | 8683 | This is a "nondeduced context": |
050367a3 | 8684 | |
4393e105 MM |
8685 | [deduct.type] |
8686 | ||
8687 | The nondeduced contexts are: | |
050367a3 | 8688 | |
4393e105 MM |
8689 | --A type that is a template-id in which one or more of |
8690 | the template-arguments is an expression that references | |
8691 | a template-parameter. | |
050367a3 | 8692 | |
4393e105 MM |
8693 | In these cases, we assume deduction succeeded, but don't |
8694 | actually infer any unifications. */ | |
8695 | return 0; | |
050367a3 | 8696 | else |
b4c4a9ec MM |
8697 | sorry ("use of `%s' in template type unification", |
8698 | tree_code_name [(int) TREE_CODE (parm)]); | |
050367a3 | 8699 | |
8d08fdba MS |
8700 | return 1; |
8701 | } | |
8702 | } | |
8d08fdba | 8703 | \f |
03d0f4af MM |
8704 | /* Called if RESULT is explicitly instantiated, or is a member of an |
8705 | explicitly instantiated class, or if using -frepo and the | |
8706 | instantiation of RESULT has been assigned to this file. */ | |
8707 | ||
faae18ab | 8708 | void |
5566b478 | 8709 | mark_decl_instantiated (result, extern_p) |
faae18ab MS |
8710 | tree result; |
8711 | int extern_p; | |
8712 | { | |
75650646 MM |
8713 | if (TREE_CODE (result) != FUNCTION_DECL) |
8714 | /* The TREE_PUBLIC flag for function declarations will have been | |
8715 | set correctly by tsubst. */ | |
8716 | TREE_PUBLIC (result) = 1; | |
faae18ab MS |
8717 | |
8718 | if (! extern_p) | |
8719 | { | |
8720 | DECL_INTERFACE_KNOWN (result) = 1; | |
8721 | DECL_NOT_REALLY_EXTERN (result) = 1; | |
a7d87521 | 8722 | |
1a408d07 JM |
8723 | /* Always make artificials weak. */ |
8724 | if (DECL_ARTIFICIAL (result) && flag_weak) | |
8725 | comdat_linkage (result); | |
a7d87521 JM |
8726 | /* For WIN32 we also want to put explicit instantiations in |
8727 | linkonce sections. */ | |
1a408d07 | 8728 | else if (TREE_PUBLIC (result)) |
b385c841 | 8729 | maybe_make_one_only (result); |
faae18ab | 8730 | } |
f49422da | 8731 | else if (TREE_CODE (result) == FUNCTION_DECL) |
56e770bf | 8732 | defer_fn (result); |
faae18ab MS |
8733 | } |
8734 | ||
e1467ff2 MM |
8735 | /* Given two function templates PAT1 and PAT2, and explicit template |
8736 | arguments EXPLICIT_ARGS return: | |
6467930b MS |
8737 | |
8738 | 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order]. | |
8739 | -1 if PAT2 is more specialized than PAT1. | |
8740 | 0 if neither is more specialized. */ | |
8741 | ||
8742 | int | |
e1467ff2 MM |
8743 | more_specialized (pat1, pat2, explicit_args) |
8744 | tree pat1, pat2, explicit_args; | |
6467930b | 8745 | { |
98c1c668 | 8746 | tree targs; |
73aad9b9 | 8747 | int winner = 0; |
6467930b | 8748 | |
17aec3eb RK |
8749 | targs |
8750 | = get_bindings_overload (pat1, DECL_TEMPLATE_RESULT (pat2), explicit_args); | |
73aad9b9 | 8751 | if (targs) |
36a117a5 | 8752 | --winner; |
6467930b | 8753 | |
17aec3eb RK |
8754 | targs |
8755 | = get_bindings_overload (pat2, DECL_TEMPLATE_RESULT (pat1), explicit_args); | |
73aad9b9 | 8756 | if (targs) |
36a117a5 | 8757 | ++winner; |
6467930b | 8758 | |
73aad9b9 JM |
8759 | return winner; |
8760 | } | |
6467930b | 8761 | |
73aad9b9 | 8762 | /* Given two class template specialization list nodes PAT1 and PAT2, return: |
6467930b | 8763 | |
73aad9b9 JM |
8764 | 1 if PAT1 is more specialized than PAT2 as described in [temp.class.order]. |
8765 | -1 if PAT2 is more specialized than PAT1. | |
8766 | 0 if neither is more specialized. */ | |
8767 | ||
8768 | int | |
8769 | more_specialized_class (pat1, pat2) | |
8770 | tree pat1, pat2; | |
8771 | { | |
8772 | tree targs; | |
8773 | int winner = 0; | |
8774 | ||
36a117a5 MM |
8775 | targs = get_class_bindings (TREE_VALUE (pat1), TREE_PURPOSE (pat1), |
8776 | TREE_PURPOSE (pat2)); | |
73aad9b9 JM |
8777 | if (targs) |
8778 | --winner; | |
8779 | ||
36a117a5 MM |
8780 | targs = get_class_bindings (TREE_VALUE (pat2), TREE_PURPOSE (pat2), |
8781 | TREE_PURPOSE (pat1)); | |
73aad9b9 | 8782 | if (targs) |
6467930b MS |
8783 | ++winner; |
8784 | ||
8785 | return winner; | |
8786 | } | |
73aad9b9 JM |
8787 | |
8788 | /* Return the template arguments that will produce the function signature | |
e1467ff2 | 8789 | DECL from the function template FN, with the explicit template |
76b9a14d | 8790 | arguments EXPLICIT_ARGS. If CHECK_RETTYPE is 1, the return type must |
4393e105 MM |
8791 | also match. Return NULL_TREE if no satisfactory arguments could be |
8792 | found. */ | |
73aad9b9 | 8793 | |
76b9a14d JM |
8794 | static tree |
8795 | get_bindings_real (fn, decl, explicit_args, check_rettype) | |
e1467ff2 | 8796 | tree fn, decl, explicit_args; |
76b9a14d | 8797 | int check_rettype; |
73aad9b9 | 8798 | { |
98c1c668 | 8799 | int ntparms = DECL_NTPARMS (fn); |
f31c0a32 | 8800 | tree targs = make_tree_vec (ntparms); |
4393e105 | 8801 | tree decl_type; |
03017874 | 8802 | tree decl_arg_types; |
98c1c668 JM |
8803 | int i; |
8804 | ||
4393e105 MM |
8805 | /* Substitute the explicit template arguments into the type of DECL. |
8806 | The call to fn_type_unification will handle substitution into the | |
8807 | FN. */ | |
8808 | decl_type = TREE_TYPE (decl); | |
8809 | if (explicit_args && uses_template_parms (decl_type)) | |
8810 | { | |
8811 | tree tmpl; | |
8812 | tree converted_args; | |
8813 | ||
8814 | if (DECL_TEMPLATE_INFO (decl)) | |
8815 | tmpl = DECL_TI_TEMPLATE (decl); | |
8816 | else | |
8817 | /* We can get here for some illegal specializations. */ | |
8818 | return NULL_TREE; | |
8819 | ||
8820 | converted_args | |
8821 | = (coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl), | |
8822 | explicit_args, NULL_TREE, | |
8823 | /*complain=*/0, | |
8824 | /*require_all_arguments=*/0)); | |
8825 | if (converted_args == error_mark_node) | |
8826 | return NULL_TREE; | |
8827 | ||
8828 | decl_type = tsubst (decl_type, converted_args, /*complain=*/0, | |
8829 | NULL_TREE); | |
8830 | if (decl_type == error_mark_node) | |
8831 | return NULL_TREE; | |
8832 | } | |
8833 | ||
03017874 MM |
8834 | /* If FN is a static member function, adjust the type of DECL |
8835 | appropriately. */ | |
4393e105 | 8836 | decl_arg_types = TYPE_ARG_TYPES (decl_type); |
d7684f2d MM |
8837 | if (DECL_STATIC_FUNCTION_P (fn) |
8838 | && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)) | |
03017874 | 8839 | decl_arg_types = TREE_CHAIN (decl_arg_types); |
d7684f2d | 8840 | |
e1467ff2 | 8841 | i = fn_type_unification (fn, explicit_args, targs, |
03017874 | 8842 | decl_arg_types, |
8d3631f8 NS |
8843 | (check_rettype || DECL_CONV_FN_P (fn) |
8844 | ? TREE_TYPE (decl_type) : NULL_TREE), | |
03017874 | 8845 | DEDUCE_EXACT); |
98c1c668 | 8846 | |
76b9a14d JM |
8847 | if (i != 0) |
8848 | return NULL_TREE; | |
8849 | ||
76b9a14d JM |
8850 | return targs; |
8851 | } | |
8852 | ||
8853 | /* For most uses, we want to check the return type. */ | |
8854 | ||
8855 | tree | |
8856 | get_bindings (fn, decl, explicit_args) | |
8857 | tree fn, decl, explicit_args; | |
8858 | { | |
8859 | return get_bindings_real (fn, decl, explicit_args, 1); | |
8860 | } | |
8861 | ||
8862 | /* But for more_specialized, we only care about the parameter types. */ | |
8863 | ||
8864 | static tree | |
8865 | get_bindings_overload (fn, decl, explicit_args) | |
8866 | tree fn, decl, explicit_args; | |
8867 | { | |
8868 | return get_bindings_real (fn, decl, explicit_args, 0); | |
73aad9b9 JM |
8869 | } |
8870 | ||
36a117a5 MM |
8871 | /* Return the innermost template arguments that, when applied to a |
8872 | template specialization whose innermost template parameters are | |
8873 | TPARMS, and whose specialization arguments are ARGS, yield the | |
8874 | ARGS. | |
8875 | ||
8876 | For example, suppose we have: | |
8877 | ||
8878 | template <class T, class U> struct S {}; | |
8879 | template <class T> struct S<T*, int> {}; | |
8880 | ||
8881 | Then, suppose we want to get `S<double*, int>'. The TPARMS will be | |
8882 | {T}, the PARMS will be {T*, int} and the ARGS will be {double*, | |
8883 | int}. The resulting vector will be {double}, indicating that `T' | |
8884 | is bound to `double'. */ | |
8885 | ||
bd6dd845 | 8886 | static tree |
36a117a5 MM |
8887 | get_class_bindings (tparms, parms, args) |
8888 | tree tparms, parms, args; | |
73aad9b9 | 8889 | { |
3b3ba9f0 | 8890 | int i, ntparms = TREE_VEC_LENGTH (tparms); |
f31c0a32 | 8891 | tree vec = make_tree_vec (ntparms); |
73aad9b9 | 8892 | |
36a117a5 | 8893 | args = innermost_args (args); |
8d019cef | 8894 | |
4393e105 | 8895 | if (unify (tparms, vec, parms, args, UNIFY_ALLOW_NONE)) |
fcfb9f96 | 8896 | return NULL_TREE; |
73aad9b9 JM |
8897 | |
8898 | for (i = 0; i < ntparms; ++i) | |
8899 | if (! TREE_VEC_ELT (vec, i)) | |
8900 | return NULL_TREE; | |
8901 | ||
8902 | return vec; | |
8903 | } | |
8904 | ||
104bf76a MM |
8905 | /* In INSTANTIATIONS is a list of <INSTANTIATION, TEMPLATE> pairs. |
8906 | Pick the most specialized template, and return the corresponding | |
8907 | instantiation, or if there is no corresponding instantiation, the | |
8908 | template itself. EXPLICIT_ARGS is any template arguments explicity | |
8909 | mentioned in a template-id. If there is no most specialized | |
5186142b | 8910 | template, error_mark_node is returned. If there are no templates |
104bf76a | 8911 | at all, NULL_TREE is returned. */ |
73aad9b9 JM |
8912 | |
8913 | tree | |
104bf76a MM |
8914 | most_specialized_instantiation (instantiations, explicit_args) |
8915 | tree instantiations; | |
8916 | tree explicit_args; | |
73aad9b9 | 8917 | { |
104bf76a | 8918 | tree fn, champ; |
73aad9b9 JM |
8919 | int fate; |
8920 | ||
104bf76a | 8921 | if (!instantiations) |
73aad9b9 JM |
8922 | return NULL_TREE; |
8923 | ||
104bf76a MM |
8924 | champ = instantiations; |
8925 | for (fn = TREE_CHAIN (instantiations); fn; fn = TREE_CHAIN (fn)) | |
73aad9b9 | 8926 | { |
104bf76a MM |
8927 | fate = more_specialized (TREE_VALUE (champ), |
8928 | TREE_VALUE (fn), explicit_args); | |
73aad9b9 JM |
8929 | if (fate == 1) |
8930 | ; | |
8931 | else | |
8932 | { | |
8933 | if (fate == 0) | |
8934 | { | |
8935 | fn = TREE_CHAIN (fn); | |
8936 | if (! fn) | |
8937 | return error_mark_node; | |
8938 | } | |
104bf76a | 8939 | champ = fn; |
73aad9b9 JM |
8940 | } |
8941 | } | |
8942 | ||
104bf76a | 8943 | for (fn = instantiations; fn && fn != champ; fn = TREE_CHAIN (fn)) |
73aad9b9 | 8944 | { |
104bf76a MM |
8945 | fate = more_specialized (TREE_VALUE (champ), |
8946 | TREE_VALUE (fn), explicit_args); | |
73aad9b9 JM |
8947 | if (fate != 1) |
8948 | return error_mark_node; | |
8949 | } | |
8950 | ||
104bf76a MM |
8951 | return TREE_PURPOSE (champ) ? TREE_PURPOSE (champ) : TREE_VALUE (champ); |
8952 | } | |
8953 | ||
8954 | /* Return the most specialized of the list of templates in FNS that can | |
8955 | produce an instantiation matching DECL, given the explicit template | |
8956 | arguments EXPLICIT_ARGS. */ | |
8957 | ||
e9659ab0 | 8958 | static tree |
104bf76a MM |
8959 | most_specialized (fns, decl, explicit_args) |
8960 | tree fns, decl, explicit_args; | |
8961 | { | |
8962 | tree candidates = NULL_TREE; | |
8963 | tree fn, args; | |
8964 | ||
8965 | for (fn = fns; fn; fn = TREE_CHAIN (fn)) | |
8966 | { | |
8967 | tree candidate = TREE_VALUE (fn); | |
8968 | ||
8969 | args = get_bindings (candidate, decl, explicit_args); | |
8970 | if (args) | |
e1b3e07d | 8971 | candidates = tree_cons (NULL_TREE, candidate, candidates); |
104bf76a MM |
8972 | } |
8973 | ||
8974 | return most_specialized_instantiation (candidates, explicit_args); | |
73aad9b9 JM |
8975 | } |
8976 | ||
36a117a5 MM |
8977 | /* If DECL is a specialization of some template, return the most |
8978 | general such template. For example, given: | |
8979 | ||
8980 | template <class T> struct S { template <class U> void f(U); }; | |
8981 | ||
8982 | if TMPL is `template <class U> void S<int>::f(U)' this will return | |
8983 | the full template. This function will not trace past partial | |
8984 | specializations, however. For example, given in addition: | |
8985 | ||
8986 | template <class T> struct S<T*> { template <class U> void f(U); }; | |
8987 | ||
8988 | if TMPL is `template <class U> void S<int*>::f(U)' this will return | |
8989 | `template <class T> template <class U> S<T*>::f(U)'. */ | |
73aad9b9 | 8990 | |
612c671a | 8991 | tree |
36a117a5 MM |
8992 | most_general_template (decl) |
8993 | tree decl; | |
73aad9b9 | 8994 | { |
b5ac18ea | 8995 | while (DECL_TEMPLATE_INFO (decl) |
74b846e0 MM |
8996 | && !(TREE_CODE (decl) == TEMPLATE_DECL |
8997 | && DECL_TEMPLATE_SPECIALIZATION (decl)) | |
b5ac18ea MM |
8998 | /* The DECL_TI_TEMPLATE can be a LOOKUP_EXPR or |
8999 | IDENTIFIER_NODE in some cases. (See cp-tree.h for | |
9000 | details.) */ | |
9001 | && TREE_CODE (DECL_TI_TEMPLATE (decl)) == TEMPLATE_DECL) | |
36a117a5 MM |
9002 | decl = DECL_TI_TEMPLATE (decl); |
9003 | ||
9004 | return decl; | |
9005 | } | |
9006 | ||
9007 | /* Return the most specialized of the class template specializations | |
9008 | of TMPL which can produce an instantiation matching ARGS, or | |
9009 | error_mark_node if the choice is ambiguous. */ | |
9010 | ||
e9659ab0 | 9011 | static tree |
36a117a5 MM |
9012 | most_specialized_class (tmpl, args) |
9013 | tree tmpl; | |
9014 | tree args; | |
9015 | { | |
9016 | tree list = NULL_TREE; | |
9017 | tree t; | |
9018 | tree champ; | |
73aad9b9 JM |
9019 | int fate; |
9020 | ||
36a117a5 MM |
9021 | tmpl = most_general_template (tmpl); |
9022 | for (t = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); t; t = TREE_CHAIN (t)) | |
73aad9b9 | 9023 | { |
36a117a5 MM |
9024 | tree spec_args |
9025 | = get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t), args); | |
9026 | if (spec_args) | |
73aad9b9 JM |
9027 | { |
9028 | list = decl_tree_cons (TREE_PURPOSE (t), TREE_VALUE (t), list); | |
9029 | TREE_TYPE (list) = TREE_TYPE (t); | |
9030 | } | |
9031 | } | |
9032 | ||
9033 | if (! list) | |
9034 | return NULL_TREE; | |
9035 | ||
9036 | t = list; | |
9037 | champ = t; | |
9038 | t = TREE_CHAIN (t); | |
9039 | for (; t; t = TREE_CHAIN (t)) | |
9040 | { | |
9041 | fate = more_specialized_class (champ, t); | |
9042 | if (fate == 1) | |
9043 | ; | |
9044 | else | |
9045 | { | |
9046 | if (fate == 0) | |
9047 | { | |
9048 | t = TREE_CHAIN (t); | |
9049 | if (! t) | |
9050 | return error_mark_node; | |
9051 | } | |
9052 | champ = t; | |
9053 | } | |
9054 | } | |
9055 | ||
9056 | for (t = list; t && t != champ; t = TREE_CHAIN (t)) | |
9057 | { | |
85b71cf2 | 9058 | fate = more_specialized_class (champ, t); |
73aad9b9 JM |
9059 | if (fate != 1) |
9060 | return error_mark_node; | |
9061 | } | |
9062 | ||
9063 | return champ; | |
9064 | } | |
9065 | ||
8d08fdba | 9066 | /* called from the parser. */ |
e92cc029 | 9067 | |
8d08fdba | 9068 | void |
6633d636 | 9069 | do_decl_instantiation (declspecs, declarator, storage) |
f0e01782 | 9070 | tree declspecs, declarator, storage; |
8d08fdba | 9071 | { |
c11b6f21 | 9072 | tree decl = grokdeclarator (declarator, declspecs, NORMAL, 0, NULL_TREE); |
8d08fdba | 9073 | tree result = NULL_TREE; |
faae18ab | 9074 | int extern_p = 0; |
e8abc66f | 9075 | |
3fa56191 MM |
9076 | if (!decl) |
9077 | /* An error ocurred, for which grokdeclarator has already issued | |
9078 | an appropriate message. */ | |
9079 | return; | |
9080 | else if (! DECL_LANG_SPECIFIC (decl)) | |
ec255269 | 9081 | { |
8251199e | 9082 | cp_error ("explicit instantiation of non-template `%#D'", decl); |
ec255269 MS |
9083 | return; |
9084 | } | |
03d0f4af | 9085 | else if (TREE_CODE (decl) == VAR_DECL) |
6633d636 | 9086 | { |
03d0f4af MM |
9087 | /* There is an asymmetry here in the way VAR_DECLs and |
9088 | FUNCTION_DECLs are handled by grokdeclarator. In the case of | |
9089 | the latter, the DECL we get back will be marked as a | |
9090 | template instantiation, and the appropriate | |
9091 | DECL_TEMPLATE_INFO will be set up. This does not happen for | |
9092 | VAR_DECLs so we do the lookup here. Probably, grokdeclarator | |
9093 | should handle VAR_DECLs as it currently handles | |
9094 | FUNCTION_DECLs. */ | |
6633d636 MS |
9095 | result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, 0); |
9096 | if (result && TREE_CODE (result) != VAR_DECL) | |
03d0f4af | 9097 | { |
8251199e | 9098 | cp_error ("no matching template for `%D' found", result); |
03d0f4af MM |
9099 | return; |
9100 | } | |
6633d636 MS |
9101 | } |
9102 | else if (TREE_CODE (decl) != FUNCTION_DECL) | |
9103 | { | |
8251199e | 9104 | cp_error ("explicit instantiation of `%#D'", decl); |
6633d636 MS |
9105 | return; |
9106 | } | |
03d0f4af MM |
9107 | else |
9108 | result = decl; | |
672476cb | 9109 | |
03d0f4af MM |
9110 | /* Check for various error cases. Note that if the explicit |
9111 | instantiation is legal the RESULT will currently be marked as an | |
9112 | *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set | |
9113 | until we get here. */ | |
9114 | ||
9115 | if (DECL_TEMPLATE_SPECIALIZATION (result)) | |
672476cb | 9116 | { |
03d0f4af MM |
9117 | /* [temp.spec] |
9118 | ||
9119 | No program shall both explicitly instantiate and explicitly | |
9120 | specialize a template. */ | |
6f1b4c42 JM |
9121 | cp_pedwarn ("explicit instantiation of `%#D' after", result); |
9122 | cp_pedwarn_at ("explicit specialization here", result); | |
672476cb MM |
9123 | return; |
9124 | } | |
03d0f4af MM |
9125 | else if (DECL_EXPLICIT_INSTANTIATION (result)) |
9126 | { | |
9127 | /* [temp.spec] | |
98c1c668 | 9128 | |
03d0f4af MM |
9129 | No program shall explicitly instantiate any template more |
9130 | than once. | |
9131 | ||
2aaf816d JM |
9132 | We check DECL_INTERFACE_KNOWN so as not to complain when the first |
9133 | instantiation was `extern' and the second is not, and EXTERN_P for | |
9134 | the opposite case. If -frepo, chances are we already got marked | |
9135 | as an explicit instantion because of the repo file. */ | |
9136 | if (DECL_INTERFACE_KNOWN (result) && !extern_p && !flag_use_repository) | |
6f1b4c42 | 9137 | cp_pedwarn ("duplicate explicit instantiation of `%#D'", result); |
03d0f4af MM |
9138 | |
9139 | /* If we've already instantiated the template, just return now. */ | |
9140 | if (DECL_INTERFACE_KNOWN (result)) | |
9141 | return; | |
9142 | } | |
9143 | else if (!DECL_IMPLICIT_INSTANTIATION (result)) | |
faae18ab | 9144 | { |
8251199e | 9145 | cp_error ("no matching template for `%D' found", result); |
faae18ab MS |
9146 | return; |
9147 | } | |
03d0f4af | 9148 | else if (!DECL_TEMPLATE_INFO (result)) |
6633d636 | 9149 | { |
8251199e | 9150 | cp_pedwarn ("explicit instantiation of non-template `%#D'", result); |
6633d636 MS |
9151 | return; |
9152 | } | |
9153 | ||
a0a33927 MS |
9154 | if (flag_external_templates) |
9155 | return; | |
9156 | ||
f0e01782 | 9157 | if (storage == NULL_TREE) |
00595019 | 9158 | ; |
faae18ab | 9159 | else if (storage == ridpointers[(int) RID_EXTERN]) |
03d0f4af MM |
9160 | { |
9161 | if (pedantic) | |
cab1f180 | 9162 | cp_pedwarn ("ISO C++ forbids the use of `extern' on explicit instantiations"); |
03d0f4af MM |
9163 | extern_p = 1; |
9164 | } | |
f0e01782 | 9165 | else |
8251199e | 9166 | cp_error ("storage class `%D' applied to template instantiation", |
f0e01782 | 9167 | storage); |
5566b478 | 9168 | |
03d0f4af | 9169 | SET_DECL_EXPLICIT_INSTANTIATION (result); |
5566b478 | 9170 | mark_decl_instantiated (result, extern_p); |
44a8d0b3 | 9171 | repo_template_instantiated (result, extern_p); |
c91a56d2 | 9172 | if (! extern_p) |
16d53b64 | 9173 | instantiate_decl (result, /*defer_ok=*/1); |
7177d104 MS |
9174 | } |
9175 | ||
faae18ab MS |
9176 | void |
9177 | mark_class_instantiated (t, extern_p) | |
9178 | tree t; | |
9179 | int extern_p; | |
9180 | { | |
9181 | SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t); | |
eb773359 JM |
9182 | SET_CLASSTYPE_INTERFACE_KNOWN (t); |
9183 | CLASSTYPE_INTERFACE_ONLY (t) = extern_p; | |
faae18ab MS |
9184 | CLASSTYPE_VTABLE_NEEDS_WRITING (t) = ! extern_p; |
9185 | TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p; | |
9186 | if (! extern_p) | |
9187 | { | |
9188 | CLASSTYPE_DEBUG_REQUESTED (t) = 1; | |
9189 | rest_of_type_compilation (t, 1); | |
9190 | } | |
9191 | } | |
e8abc66f | 9192 | |
7177d104 | 9193 | void |
ca79f85d JM |
9194 | do_type_instantiation (t, storage) |
9195 | tree t, storage; | |
7177d104 | 9196 | { |
e8abc66f MS |
9197 | int extern_p = 0; |
9198 | int nomem_p = 0; | |
5566b478 MS |
9199 | int static_p = 0; |
9200 | ||
ca79f85d JM |
9201 | if (TREE_CODE (t) == TYPE_DECL) |
9202 | t = TREE_TYPE (t); | |
9203 | ||
7ddedda4 | 9204 | if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t)) |
ca79f85d | 9205 | { |
8251199e | 9206 | cp_error ("explicit instantiation of non-template type `%T'", t); |
ca79f85d JM |
9207 | return; |
9208 | } | |
9209 | ||
5566b478 | 9210 | complete_type (t); |
7177d104 | 9211 | |
a292b002 MS |
9212 | /* With -fexternal-templates, explicit instantiations are treated the same |
9213 | as implicit ones. */ | |
a0a33927 MS |
9214 | if (flag_external_templates) |
9215 | return; | |
9216 | ||
d0f062fb | 9217 | if (!COMPLETE_TYPE_P (t)) |
f0e01782 | 9218 | { |
8251199e | 9219 | cp_error ("explicit instantiation of `%#T' before definition of template", |
f0e01782 MS |
9220 | t); |
9221 | return; | |
9222 | } | |
9223 | ||
03d0f4af | 9224 | if (storage != NULL_TREE) |
f0e01782 | 9225 | { |
03d0f4af | 9226 | if (pedantic) |
cab1f180 | 9227 | cp_pedwarn("ISO C++ forbids the use of `%s' on explicit instantiations", |
8251199e | 9228 | IDENTIFIER_POINTER (storage)); |
03d0f4af MM |
9229 | |
9230 | if (storage == ridpointers[(int) RID_INLINE]) | |
9231 | nomem_p = 1; | |
9232 | else if (storage == ridpointers[(int) RID_EXTERN]) | |
9233 | extern_p = 1; | |
9234 | else if (storage == ridpointers[(int) RID_STATIC]) | |
9235 | static_p = 1; | |
9236 | else | |
9237 | { | |
8251199e | 9238 | cp_error ("storage class `%D' applied to template instantiation", |
03d0f4af MM |
9239 | storage); |
9240 | extern_p = 0; | |
9241 | } | |
f0e01782 MS |
9242 | } |
9243 | ||
370af2d5 | 9244 | if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t)) |
03d0f4af MM |
9245 | { |
9246 | /* [temp.spec] | |
a292b002 | 9247 | |
03d0f4af MM |
9248 | No program shall both explicitly instantiate and explicitly |
9249 | specialize a template. */ | |
8251199e JM |
9250 | cp_error ("explicit instantiation of `%#T' after", t); |
9251 | cp_error_at ("explicit specialization here", t); | |
03d0f4af MM |
9252 | return; |
9253 | } | |
9254 | else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t)) | |
44a8d0b3 | 9255 | { |
03d0f4af MM |
9256 | /* [temp.spec] |
9257 | ||
9258 | No program shall explicitly instantiate any template more | |
9259 | than once. | |
9260 | ||
2aaf816d JM |
9261 | If CLASSTYPE_INTERFACE_ONLY, then the first explicit instantiation |
9262 | was `extern'. If EXTERN_P then the second is. If -frepo, chances | |
9263 | are we already got marked as an explicit instantion because of the | |
9264 | repo file. All these cases are OK. */ | |
9265 | if (!CLASSTYPE_INTERFACE_ONLY (t) && !extern_p && !flag_use_repository) | |
9266 | cp_pedwarn ("duplicate explicit instantiation of `%#T'", t); | |
03d0f4af MM |
9267 | |
9268 | /* If we've already instantiated the template, just return now. */ | |
9269 | if (!CLASSTYPE_INTERFACE_ONLY (t)) | |
9270 | return; | |
44a8d0b3 | 9271 | } |
e8abc66f | 9272 | |
03d0f4af MM |
9273 | mark_class_instantiated (t, extern_p); |
9274 | repo_template_instantiated (t, extern_p); | |
9275 | ||
e8abc66f MS |
9276 | if (nomem_p) |
9277 | return; | |
9278 | ||
7177d104 | 9279 | { |
db5ae43f | 9280 | tree tmp; |
5566b478 | 9281 | |
03d0f4af MM |
9282 | /* In contrast to implicit instantiation, where only the |
9283 | declarations, and not the definitions, of members are | |
9284 | instantiated, we have here: | |
9285 | ||
9286 | [temp.explicit] | |
9287 | ||
9288 | The explicit instantiation of a class template specialization | |
9289 | implies the instantiation of all of its members not | |
9290 | previously explicitly specialized in the translation unit | |
9291 | containing the explicit instantiation. | |
9292 | ||
9293 | Of course, we can't instantiate member template classes, since | |
9294 | we don't have any arguments for them. Note that the standard | |
9295 | is unclear on whether the instatiation of the members are | |
9296 | *explicit* instantiations or not. We choose to be generous, | |
9297 | and not set DECL_EXPLICIT_INSTANTIATION. Therefore, we allow | |
9298 | the explicit instantiation of a class where some of the members | |
9299 | have no definition in the current translation unit. */ | |
9300 | ||
5566b478 MS |
9301 | if (! static_p) |
9302 | for (tmp = TYPE_METHODS (t); tmp; tmp = TREE_CHAIN (tmp)) | |
a7d87521 | 9303 | if (TREE_CODE (tmp) == FUNCTION_DECL |
1f109f0f | 9304 | && DECL_TEMPLATE_INSTANTIATION (tmp)) |
5566b478 MS |
9305 | { |
9306 | mark_decl_instantiated (tmp, extern_p); | |
9307 | repo_template_instantiated (tmp, extern_p); | |
9308 | if (! extern_p) | |
16d53b64 | 9309 | instantiate_decl (tmp, /*defer_ok=*/1); |
5566b478 MS |
9310 | } |
9311 | ||
9312 | for (tmp = TYPE_FIELDS (t); tmp; tmp = TREE_CHAIN (tmp)) | |
9313 | if (TREE_CODE (tmp) == VAR_DECL && DECL_TEMPLATE_INSTANTIATION (tmp)) | |
863adfc0 | 9314 | { |
5566b478 | 9315 | mark_decl_instantiated (tmp, extern_p); |
863adfc0 | 9316 | repo_template_instantiated (tmp, extern_p); |
5566b478 | 9317 | if (! extern_p) |
16d53b64 | 9318 | instantiate_decl (tmp, /*defer_ok=*/1); |
863adfc0 | 9319 | } |
7177d104 | 9320 | |
a292b002 | 9321 | for (tmp = CLASSTYPE_TAGS (t); tmp; tmp = TREE_CHAIN (tmp)) |
e4a84209 MM |
9322 | if (IS_AGGR_TYPE (TREE_VALUE (tmp)) |
9323 | && !uses_template_parms (CLASSTYPE_TI_ARGS (TREE_VALUE (tmp)))) | |
f376e137 | 9324 | do_type_instantiation (TYPE_MAIN_DECL (TREE_VALUE (tmp)), storage); |
a292b002 | 9325 | } |
8d08fdba | 9326 | } |
a28e3c7f | 9327 | |
36a117a5 MM |
9328 | /* Given a function DECL, which is a specialization of TMPL, modify |
9329 | DECL to be a re-instantiation of TMPL with the same template | |
9330 | arguments. TMPL should be the template into which tsubst'ing | |
9331 | should occur for DECL, not the most general template. | |
830bfa74 MM |
9332 | |
9333 | One reason for doing this is a scenario like this: | |
9334 | ||
9335 | template <class T> | |
9336 | void f(const T&, int i); | |
9337 | ||
9338 | void g() { f(3, 7); } | |
9339 | ||
9340 | template <class T> | |
9341 | void f(const T& t, const int i) { } | |
9342 | ||
9343 | Note that when the template is first instantiated, with | |
9344 | instantiate_template, the resulting DECL will have no name for the | |
9345 | first parameter, and the wrong type for the second. So, when we go | |
9346 | to instantiate the DECL, we regenerate it. */ | |
9347 | ||
e9659ab0 | 9348 | static void |
830bfa74 MM |
9349 | regenerate_decl_from_template (decl, tmpl) |
9350 | tree decl; | |
9351 | tree tmpl; | |
9352 | { | |
9353 | tree args; | |
830bfa74 MM |
9354 | tree code_pattern; |
9355 | tree new_decl; | |
36a117a5 MM |
9356 | tree gen_tmpl; |
9357 | int unregistered; | |
830bfa74 MM |
9358 | |
9359 | args = DECL_TI_ARGS (decl); | |
9360 | code_pattern = DECL_TEMPLATE_RESULT (tmpl); | |
9361 | ||
8251199e JM |
9362 | /* Unregister the specialization so that when we tsubst we will not |
9363 | just return DECL. We don't have to unregister DECL from TMPL | |
9364 | because if would only be registered there if it were a partial | |
9365 | instantiation of a specialization, which it isn't: it's a full | |
9366 | instantiation. */ | |
36a117a5 MM |
9367 | gen_tmpl = most_general_template (tmpl); |
9368 | unregistered = unregister_specialization (decl, gen_tmpl); | |
9369 | ||
9370 | /* If the DECL was not unregistered then something peculiar is | |
9371 | happening: we created a specialization but did not call | |
9372 | register_specialization for it. */ | |
9373 | my_friendly_assert (unregistered, 0); | |
9374 | ||
61a127b3 MM |
9375 | if (TREE_CODE (decl) == VAR_DECL) |
9376 | /* Make sure that we can see identifiers, and compute access | |
9377 | correctly, for the class members used in the declaration of | |
9378 | this static variable. */ | |
9379 | pushclass (DECL_CONTEXT (decl), 2); | |
9380 | ||
d8c4447d | 9381 | /* Do the substitution to get the new declaration. */ |
4393e105 | 9382 | new_decl = tsubst (code_pattern, args, /*complain=*/1, NULL_TREE); |
830bfa74 MM |
9383 | |
9384 | if (TREE_CODE (decl) == VAR_DECL) | |
9385 | { | |
9386 | /* Set up DECL_INITIAL, since tsubst doesn't. */ | |
830bfa74 | 9387 | DECL_INITIAL (new_decl) = |
d8c4447d | 9388 | tsubst_expr (DECL_INITIAL (code_pattern), args, |
4393e105 | 9389 | /*complain=*/1, DECL_TI_TEMPLATE (decl)); |
61a127b3 | 9390 | /* Pop the class context we pushed above. */ |
b74a0560 | 9391 | popclass (); |
830bfa74 | 9392 | } |
2b0a63a3 | 9393 | else if (TREE_CODE (decl) == FUNCTION_DECL) |
fbf1c34b MM |
9394 | { |
9395 | /* Convince duplicate_decls to use the DECL_ARGUMENTS from the | |
9396 | new decl. */ | |
9397 | DECL_INITIAL (new_decl) = error_mark_node; | |
9398 | /* And don't complain about a duplicate definition. */ | |
9399 | DECL_INITIAL (decl) = NULL_TREE; | |
9400 | } | |
36a117a5 MM |
9401 | |
9402 | /* The immediate parent of the new template is still whatever it was | |
9403 | before, even though tsubst sets DECL_TI_TEMPLATE up as the most | |
9404 | general template. We also reset the DECL_ASSEMBLER_NAME since | |
9405 | tsubst always calculates the name as if the function in question | |
9406 | were really a template instance, and sometimes, with friend | |
9407 | functions, this is not so. See tsubst_friend_function for | |
9408 | details. */ | |
9409 | DECL_TI_TEMPLATE (new_decl) = DECL_TI_TEMPLATE (decl); | |
9410 | DECL_ASSEMBLER_NAME (new_decl) = DECL_ASSEMBLER_NAME (decl); | |
9411 | DECL_RTL (new_decl) = DECL_RTL (decl); | |
9412 | ||
9413 | /* Call duplicate decls to merge the old and new declarations. */ | |
830bfa74 MM |
9414 | duplicate_decls (new_decl, decl); |
9415 | ||
36a117a5 MM |
9416 | /* Now, re-register the specialization. */ |
9417 | register_specialization (decl, gen_tmpl, args); | |
830bfa74 MM |
9418 | } |
9419 | ||
16d53b64 MM |
9420 | /* Produce the definition of D, a _DECL generated from a template. If |
9421 | DEFER_OK is non-zero, then we don't have to actually do the | |
9422 | instantiation now; we just have to do it sometime. */ | |
f84b4be9 | 9423 | |
a28e3c7f | 9424 | tree |
16d53b64 | 9425 | instantiate_decl (d, defer_ok) |
5566b478 | 9426 | tree d; |
16d53b64 | 9427 | int defer_ok; |
a28e3c7f | 9428 | { |
36a117a5 MM |
9429 | tree tmpl = DECL_TI_TEMPLATE (d); |
9430 | tree args = DECL_TI_ARGS (d); | |
830bfa74 | 9431 | tree td; |
36a117a5 MM |
9432 | tree code_pattern; |
9433 | tree spec; | |
9434 | tree gen_tmpl; | |
5566b478 | 9435 | int pattern_defined; |
c27be9b9 JM |
9436 | int line = lineno; |
9437 | char *file = input_filename; | |
5566b478 | 9438 | |
36a117a5 MM |
9439 | /* This function should only be used to instantiate templates for |
9440 | functions and static member variables. */ | |
9441 | my_friendly_assert (TREE_CODE (d) == FUNCTION_DECL | |
9442 | || TREE_CODE (d) == VAR_DECL, 0); | |
9443 | ||
db9b2174 MM |
9444 | /* Don't instantiate cloned functions. Instead, instantiate the |
9445 | functions they cloned. */ | |
9446 | if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d)) | |
9447 | d = DECL_CLONED_FUNCTION (d); | |
9448 | ||
fbf1c34b | 9449 | if (DECL_TEMPLATE_INSTANTIATED (d)) |
03d0f4af MM |
9450 | /* D has already been instantiated. It might seem reasonable to |
9451 | check whether or not D is an explict instantiation, and, if so, | |
9452 | stop here. But when an explicit instantiation is deferred | |
9453 | until the end of the compilation, DECL_EXPLICIT_INSTANTIATION | |
9454 | is set, even though we still need to do the instantiation. */ | |
36a117a5 MM |
9455 | return d; |
9456 | ||
9457 | /* If we already have a specialization of this declaration, then | |
9458 | there's no reason to instantiate it. Note that | |
9459 | retrieve_specialization gives us both instantiations and | |
9460 | specializations, so we must explicitly check | |
9461 | DECL_TEMPLATE_SPECIALIZATION. */ | |
9462 | gen_tmpl = most_general_template (tmpl); | |
9463 | spec = retrieve_specialization (gen_tmpl, args); | |
9464 | if (spec != NULL_TREE && DECL_TEMPLATE_SPECIALIZATION (spec)) | |
9465 | return spec; | |
9466 | ||
9467 | /* This needs to happen before any tsubsting. */ | |
9468 | if (! push_tinst_level (d)) | |
9469 | return d; | |
9470 | ||
4d85e00e MM |
9471 | /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern |
9472 | for the instantiation. This is not always the most general | |
9473 | template. Consider, for example: | |
9474 | ||
9475 | template <class T> | |
9476 | struct S { template <class U> void f(); | |
9477 | template <> void f<int>(); }; | |
9478 | ||
9479 | and an instantiation of S<double>::f<int>. We want TD to be the | |
9480 | specialization S<T>::f<int>, not the more general S<T>::f<U>. */ | |
9481 | td = tmpl; | |
9482 | for (td = tmpl; | |
9483 | /* An instantiation cannot have a definition, so we need a | |
9484 | more general template. */ | |
9485 | DECL_TEMPLATE_INSTANTIATION (td) | |
9486 | /* We must also deal with friend templates. Given: | |
9487 | ||
9488 | template <class T> struct S { | |
9489 | template <class U> friend void f() {}; | |
9490 | }; | |
9491 | ||
9492 | S<int>::f<U> say, is not an instantiation of S<T>::f<U>, | |
9493 | so far as the language is concerned, but that's still | |
9494 | where we get the pattern for the instantiation from. On | |
9495 | ther hand, if the definition comes outside the class, say: | |
9496 | ||
9497 | template <class T> struct S { | |
9498 | template <class U> friend void f(); | |
9499 | }; | |
9500 | template <class U> friend void f() {} | |
9501 | ||
9502 | we don't need to look any further. That's what the check for | |
9503 | DECL_INITIAL is for. */ | |
9504 | || (TREE_CODE (d) == FUNCTION_DECL | |
61289ca3 | 9505 | && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (td) |
4d85e00e | 9506 | && !DECL_INITIAL (DECL_TEMPLATE_RESULT (td))); |
f84b4be9 | 9507 | ) |
4d85e00e MM |
9508 | { |
9509 | /* The present template, TD, should not be a definition. If it | |
9510 | were a definition, we should be using it! Note that we | |
9511 | cannot restructure the loop to just keep going until we find | |
9512 | a template with a definition, since that might go too far if | |
9513 | a specialization was declared, but not defined. */ | |
4d85e00e MM |
9514 | my_friendly_assert (!(TREE_CODE (d) == VAR_DECL |
9515 | && !DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (td))), | |
9516 | 0); | |
9517 | ||
9518 | /* Fetch the more general template. */ | |
9519 | td = DECL_TI_TEMPLATE (td); | |
9520 | } | |
27bb8339 | 9521 | |
fee23f54 | 9522 | code_pattern = DECL_TEMPLATE_RESULT (td); |
27bb8339 | 9523 | |
5566b478 | 9524 | if (TREE_CODE (d) == FUNCTION_DECL) |
39c76b4f | 9525 | pattern_defined = (DECL_SAVED_TREE (code_pattern) != NULL_TREE); |
5566b478 | 9526 | else |
36a117a5 | 9527 | pattern_defined = ! DECL_IN_AGGR_P (code_pattern); |
de22184b MS |
9528 | |
9529 | push_to_top_level (); | |
9530 | lineno = DECL_SOURCE_LINE (d); | |
9531 | input_filename = DECL_SOURCE_FILE (d); | |
9532 | ||
de22184b | 9533 | if (pattern_defined) |
5566b478 MS |
9534 | { |
9535 | repo_template_used (d); | |
9536 | ||
9537 | if (flag_external_templates && ! DECL_INTERFACE_KNOWN (d)) | |
9538 | { | |
9539 | if (flag_alt_external_templates) | |
9540 | { | |
9541 | if (interface_unknown) | |
9542 | warn_if_unknown_interface (d); | |
9543 | } | |
fee23f54 | 9544 | else if (DECL_INTERFACE_KNOWN (code_pattern)) |
5566b478 MS |
9545 | { |
9546 | DECL_INTERFACE_KNOWN (d) = 1; | |
fee23f54 | 9547 | DECL_NOT_REALLY_EXTERN (d) = ! DECL_EXTERNAL (code_pattern); |
5566b478 MS |
9548 | } |
9549 | else | |
fee23f54 | 9550 | warn_if_unknown_interface (code_pattern); |
5566b478 MS |
9551 | } |
9552 | ||
e92cc029 | 9553 | if (at_eof) |
5566b478 MS |
9554 | import_export_decl (d); |
9555 | } | |
9556 | ||
c4ae3f91 JM |
9557 | /* Reject all external templates except inline functions. */ |
9558 | if (DECL_INTERFACE_KNOWN (d) | |
9559 | && ! DECL_NOT_REALLY_EXTERN (d) | |
9560 | && ! (TREE_CODE (d) == FUNCTION_DECL && DECL_INLINE (d))) | |
9561 | goto out; | |
9562 | ||
16d53b64 MM |
9563 | /* We need to set up DECL_INITIAL regardless of pattern_defined if |
9564 | the variable is a static const initialized in the class body. */ | |
67ffc812 | 9565 | if (TREE_CODE (d) == VAR_DECL |
711734a9 | 9566 | && TREE_READONLY (d) |
67ffc812 MM |
9567 | && DECL_INITIAL (d) == NULL_TREE |
9568 | && DECL_INITIAL (code_pattern) != NULL_TREE) | |
16d53b64 MM |
9569 | ; |
9570 | /* Defer all other templates, unless we have been explicitly | |
9571 | forbidden from doing so. We restore the source position here | |
9572 | because it's used by add_pending_template. */ | |
9573 | else if (! pattern_defined || defer_ok) | |
9574 | { | |
c27be9b9 JM |
9575 | lineno = line; |
9576 | input_filename = file; | |
9577 | ||
03d0f4af MM |
9578 | if (at_eof && !pattern_defined |
9579 | && DECL_EXPLICIT_INSTANTIATION (d)) | |
9580 | /* [temp.explicit] | |
9581 | ||
9582 | The definition of a non-exported function template, a | |
9583 | non-exported member function template, or a non-exported | |
9584 | member function or static data member of a class template | |
9585 | shall be present in every translation unit in which it is | |
9586 | explicitly instantiated. */ | |
8251199e | 9587 | cp_error ("explicit instantiation of `%D' but no definition available", |
03d0f4af MM |
9588 | d); |
9589 | ||
5566b478 | 9590 | add_pending_template (d); |
de22184b | 9591 | goto out; |
5566b478 MS |
9592 | } |
9593 | ||
2b0a63a3 MM |
9594 | /* We're now committed to instantiating this template. Mark it as |
9595 | instantiated so that recursive calls to instantiate_decl do not | |
9596 | try to instantiate it again. */ | |
fbf1c34b | 9597 | DECL_TEMPLATE_INSTANTIATED (d) = 1; |
120722ac | 9598 | |
2b0a63a3 MM |
9599 | /* Regenerate the declaration in case the template has been modified |
9600 | by a subsequent redeclaration. */ | |
9601 | regenerate_decl_from_template (d, td); | |
9602 | ||
120722ac MM |
9603 | /* We already set the file and line above. Reset them now in case |
9604 | they changed as a result of calling regenerate_decl_from_template. */ | |
5156628f MS |
9605 | lineno = DECL_SOURCE_LINE (d); |
9606 | input_filename = DECL_SOURCE_FILE (d); | |
9607 | ||
5566b478 MS |
9608 | if (TREE_CODE (d) == VAR_DECL) |
9609 | { | |
9610 | DECL_IN_AGGR_P (d) = 0; | |
9611 | if (DECL_INTERFACE_KNOWN (d)) | |
9612 | DECL_EXTERNAL (d) = ! DECL_NOT_REALLY_EXTERN (d); | |
9613 | else | |
9614 | { | |
9615 | DECL_EXTERNAL (d) = 1; | |
9616 | DECL_NOT_REALLY_EXTERN (d) = 1; | |
9617 | } | |
cd9f6678 | 9618 | cp_finish_decl (d, DECL_INITIAL (d), NULL_TREE, 0); |
5566b478 MS |
9619 | } |
9620 | else if (TREE_CODE (d) == FUNCTION_DECL) | |
9621 | { | |
6dfbb909 MM |
9622 | /* Set up the list of local specializations. */ |
9623 | my_friendly_assert (local_specializations == NULL, 20000422); | |
9624 | local_specializations = htab_create (37, | |
9625 | htab_hash_pointer, | |
9626 | htab_eq_pointer, | |
9627 | NULL); | |
9628 | ||
558475f0 | 9629 | /* Set up context. */ |
a8f73d4b | 9630 | start_function (NULL_TREE, d, NULL_TREE, SF_PRE_PARSED); |
5566b478 MS |
9631 | store_parm_decls (); |
9632 | ||
f9817201 MM |
9633 | /* We already set up __FUNCTION__, etc., so we don't want to do |
9634 | it again now. */ | |
9635 | current_function_name_declared = 1; | |
9636 | ||
558475f0 MM |
9637 | /* Substitute into the body of the function. */ |
9638 | tsubst_expr (DECL_SAVED_TREE (code_pattern), args, | |
9639 | /*complain=*/1, tmpl); | |
9640 | ||
6dfbb909 MM |
9641 | /* We don't need the local specializations any more. */ |
9642 | htab_delete (local_specializations); | |
9643 | local_specializations = NULL; | |
9644 | ||
4d6abc1c | 9645 | /* Finish the function. */ |
0acf7199 | 9646 | expand_body (finish_function (0)); |
5566b478 MS |
9647 | } |
9648 | ||
971cbc14 MM |
9649 | /* We're not deferring instantiation any more. */ |
9650 | TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0; | |
9651 | ||
de22184b | 9652 | out: |
c27be9b9 JM |
9653 | lineno = line; |
9654 | input_filename = file; | |
9655 | ||
5566b478 | 9656 | pop_from_top_level (); |
5566b478 | 9657 | pop_tinst_level (); |
a28e3c7f | 9658 | |
a28e3c7f MS |
9659 | return d; |
9660 | } | |
5566b478 | 9661 | |
0aafb128 MM |
9662 | /* Run through the list of templates that we wish we could |
9663 | instantiate, and instantiate any we can. */ | |
9664 | ||
9665 | int | |
9666 | instantiate_pending_templates () | |
9667 | { | |
9668 | tree *t; | |
9669 | int instantiated_something = 0; | |
9670 | int reconsider; | |
9671 | ||
9672 | do | |
9673 | { | |
9674 | reconsider = 0; | |
9675 | ||
9676 | t = &pending_templates; | |
9677 | while (*t) | |
9678 | { | |
9679 | tree srcloc = TREE_PURPOSE (*t); | |
9680 | tree instantiation = TREE_VALUE (*t); | |
9681 | ||
9682 | input_filename = SRCLOC_FILE (srcloc); | |
9683 | lineno = SRCLOC_LINE (srcloc); | |
9684 | ||
2f939d94 | 9685 | if (TYPE_P (instantiation)) |
0aafb128 MM |
9686 | { |
9687 | tree fn; | |
9688 | ||
d0f062fb | 9689 | if (!COMPLETE_TYPE_P (instantiation)) |
0aafb128 MM |
9690 | { |
9691 | instantiate_class_template (instantiation); | |
9692 | if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation)) | |
9693 | for (fn = TYPE_METHODS (instantiation); | |
9694 | fn; | |
9695 | fn = TREE_CHAIN (fn)) | |
9696 | if (! DECL_ARTIFICIAL (fn)) | |
16d53b64 | 9697 | instantiate_decl (fn, /*defer_ok=*/0); |
d0f062fb | 9698 | if (COMPLETE_TYPE_P (instantiation)) |
0aafb128 MM |
9699 | { |
9700 | instantiated_something = 1; | |
9701 | reconsider = 1; | |
9702 | } | |
9703 | } | |
9704 | ||
d0f062fb | 9705 | if (COMPLETE_TYPE_P (instantiation)) |
0aafb128 MM |
9706 | /* If INSTANTIATION has been instantiated, then we don't |
9707 | need to consider it again in the future. */ | |
9708 | *t = TREE_CHAIN (*t); | |
9709 | else | |
9710 | t = &TREE_CHAIN (*t); | |
9711 | } | |
9712 | else | |
9713 | { | |
16d53b64 | 9714 | if (!DECL_TEMPLATE_SPECIALIZATION (instantiation) |
0aafb128 MM |
9715 | && !DECL_TEMPLATE_INSTANTIATED (instantiation)) |
9716 | { | |
16d53b64 MM |
9717 | instantiation = instantiate_decl (instantiation, |
9718 | /*defer_ok=*/0); | |
0aafb128 MM |
9719 | if (DECL_TEMPLATE_INSTANTIATED (instantiation)) |
9720 | { | |
9721 | instantiated_something = 1; | |
9722 | reconsider = 1; | |
9723 | } | |
9724 | } | |
9725 | ||
16d53b64 | 9726 | if (DECL_TEMPLATE_SPECIALIZATION (instantiation) |
0aafb128 MM |
9727 | || DECL_TEMPLATE_INSTANTIATED (instantiation)) |
9728 | /* If INSTANTIATION has been instantiated, then we don't | |
9729 | need to consider it again in the future. */ | |
9730 | *t = TREE_CHAIN (*t); | |
9731 | else | |
9732 | t = &TREE_CHAIN (*t); | |
9733 | } | |
9734 | } | |
9735 | template_tail = t; | |
9736 | ||
9737 | /* Go through the things that are template instantiations if we are | |
9738 | using guiding declarations. */ | |
9739 | t = &maybe_templates; | |
9740 | while (*t) | |
9741 | { | |
9742 | tree template; | |
9743 | tree fn; | |
9744 | tree args; | |
9745 | ||
9746 | fn = TREE_VALUE (*t); | |
9747 | ||
9748 | if (DECL_INITIAL (fn)) | |
9749 | /* If the FN is already defined, then it was either already | |
9750 | instantiated or, even though guiding declarations were | |
9751 | allowed, a non-template definition was provided. */ | |
9752 | ; | |
9753 | else | |
9754 | { | |
9755 | template = TREE_PURPOSE (*t); | |
9756 | args = get_bindings (template, fn, NULL_TREE); | |
9757 | fn = instantiate_template (template, args); | |
16d53b64 | 9758 | instantiate_decl (fn, /*defer_ok=*/0); |
0aafb128 MM |
9759 | reconsider = 1; |
9760 | } | |
9761 | ||
9762 | /* Remove this entry from the chain. */ | |
9763 | *t = TREE_CHAIN (*t); | |
9764 | } | |
9765 | maybe_template_tail = t; | |
9766 | } | |
9767 | while (reconsider); | |
9768 | ||
9769 | return instantiated_something; | |
9770 | } | |
9771 | ||
4393e105 MM |
9772 | /* Substitute ARGVEC into T, which is a TREE_LIST. In particular, it |
9773 | is an initializer list: the TREE_PURPOSEs are DECLs, and the | |
9774 | TREE_VALUEs are initializer values. Used by instantiate_decl. */ | |
9775 | ||
824b9a4c | 9776 | static tree |
5566b478 MS |
9777 | tsubst_expr_values (t, argvec) |
9778 | tree t, argvec; | |
9779 | { | |
9780 | tree first = NULL_TREE; | |
9781 | tree *p = &first; | |
9782 | ||
9783 | for (; t; t = TREE_CHAIN (t)) | |
9784 | { | |
4393e105 MM |
9785 | tree pur = tsubst_copy (TREE_PURPOSE (t), argvec, |
9786 | /*complain=*/1, NULL_TREE); | |
9787 | tree val = tsubst_expr (TREE_VALUE (t), argvec, /*complain=*/1, | |
9788 | NULL_TREE); | |
5566b478 MS |
9789 | *p = build_tree_list (pur, val); |
9790 | p = &TREE_CHAIN (*p); | |
9791 | } | |
9792 | return first; | |
9793 | } | |
9794 | ||
73aad9b9 JM |
9795 | /* D is an undefined function declaration in the presence of templates with |
9796 | the same name, listed in FNS. If one of them can produce D as an | |
9797 | instantiation, remember this so we can instantiate it at EOF if D has | |
9798 | not been defined by that time. */ | |
9799 | ||
9800 | void | |
9801 | add_maybe_template (d, fns) | |
9802 | tree d, fns; | |
9803 | { | |
9804 | tree t; | |
9805 | ||
9806 | if (DECL_MAYBE_TEMPLATE (d)) | |
9807 | return; | |
9808 | ||
e1467ff2 | 9809 | t = most_specialized (fns, d, NULL_TREE); |
73aad9b9 JM |
9810 | if (! t) |
9811 | return; | |
9812 | if (t == error_mark_node) | |
9813 | { | |
8251199e | 9814 | cp_error ("ambiguous template instantiation for `%D'", d); |
73aad9b9 JM |
9815 | return; |
9816 | } | |
9817 | ||
e1b3e07d | 9818 | *maybe_template_tail = tree_cons (t, d, NULL_TREE); |
73aad9b9 JM |
9819 | maybe_template_tail = &TREE_CHAIN (*maybe_template_tail); |
9820 | DECL_MAYBE_TEMPLATE (d) = 1; | |
9821 | } | |
b87692e5 | 9822 | |
61a127b3 MM |
9823 | /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */ |
9824 | ||
9825 | static void | |
9826 | set_current_access_from_decl (decl) | |
9827 | tree decl; | |
9828 | { | |
9829 | if (TREE_PRIVATE (decl)) | |
9830 | current_access_specifier = access_private_node; | |
9831 | else if (TREE_PROTECTED (decl)) | |
9832 | current_access_specifier = access_protected_node; | |
9833 | else | |
9834 | current_access_specifier = access_public_node; | |
9835 | } | |
9836 | ||
dbfe2124 MM |
9837 | /* Instantiate an enumerated type. TAG is the template type, NEWTAG |
9838 | is the instantiation (which should have been created with | |
9839 | start_enum) and ARGS are the template arguments to use. */ | |
b87692e5 | 9840 | |
dbfe2124 MM |
9841 | static void |
9842 | tsubst_enum (tag, newtag, args) | |
9843 | tree tag; | |
9844 | tree newtag; | |
9845 | tree args; | |
b87692e5 | 9846 | { |
dbfe2124 | 9847 | tree e; |
b87692e5 MS |
9848 | |
9849 | for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e)) | |
9850 | { | |
61a127b3 | 9851 | tree value; |
58595203 | 9852 | |
61a127b3 MM |
9853 | /* Note that in a template enum, the TREE_VALUE is the |
9854 | CONST_DECL, not the corresponding INTEGER_CST. */ | |
9855 | value = tsubst_expr (DECL_INITIAL (TREE_VALUE (e)), | |
4393e105 MM |
9856 | args, /*complain=*/1, |
9857 | NULL_TREE); | |
61a127b3 MM |
9858 | |
9859 | /* Give this enumeration constant the correct access. */ | |
9860 | set_current_access_from_decl (TREE_VALUE (e)); | |
9861 | ||
9862 | /* Actually build the enumerator itself. */ | |
58595203 | 9863 | build_enumerator (TREE_PURPOSE (e), value, newtag); |
dbfe2124 | 9864 | } |
b3d5a58b | 9865 | |
dbfe2124 | 9866 | finish_enum (newtag); |
b87692e5 | 9867 | } |
36a117a5 MM |
9868 | |
9869 | /* Set the DECL_ASSEMBLER_NAME for DECL, which is a FUNCTION_DECL that | |
9870 | is either an instantiation or specialization of a template | |
9871 | function. */ | |
9872 | ||
9873 | static void | |
9874 | set_mangled_name_for_template_decl (decl) | |
9875 | tree decl; | |
9876 | { | |
9877 | tree saved_namespace; | |
b370501f | 9878 | tree context = NULL_TREE; |
36a117a5 MM |
9879 | tree fn_type; |
9880 | tree ret_type; | |
9881 | tree parm_types; | |
9882 | tree tparms; | |
9883 | tree targs; | |
9884 | tree tmpl; | |
9885 | int parm_depth; | |
9886 | ||
9887 | my_friendly_assert (TREE_CODE (decl) == FUNCTION_DECL, 0); | |
9888 | my_friendly_assert (DECL_TEMPLATE_INFO (decl) != NULL_TREE, 0); | |
9889 | ||
9890 | /* The names of template functions must be mangled so as to indicate | |
9891 | what template is being specialized with what template arguments. | |
9892 | For example, each of the following three functions must get | |
9893 | different mangled names: | |
9894 | ||
9895 | void f(int); | |
9896 | template <> void f<7>(int); | |
9897 | template <> void f<8>(int); */ | |
9898 | ||
9899 | targs = DECL_TI_ARGS (decl); | |
9900 | if (uses_template_parms (targs)) | |
9901 | /* This DECL is for a partial instantiation. There's no need to | |
9902 | mangle the name of such an entity. */ | |
9903 | return; | |
9904 | ||
9905 | tmpl = most_general_template (DECL_TI_TEMPLATE (decl)); | |
9906 | tparms = DECL_TEMPLATE_PARMS (tmpl); | |
9907 | parm_depth = TMPL_PARMS_DEPTH (tparms); | |
9908 | ||
9909 | /* There should be as many levels of arguments as there are levels | |
9910 | of parameters. */ | |
9911 | my_friendly_assert (parm_depth == TMPL_ARGS_DEPTH (targs), 0); | |
9912 | ||
9913 | /* We now compute the PARMS and RET_TYPE to give to | |
9914 | build_decl_overload_real. The PARMS and RET_TYPE are the | |
9915 | parameter and return types of the template, after all but the | |
9916 | innermost template arguments have been substituted, not the | |
9917 | parameter and return types of the function DECL. For example, | |
9918 | given: | |
9919 | ||
9920 | template <class T> T f(T); | |
9921 | ||
9922 | both PARMS and RET_TYPE should be `T' even if DECL is `int f(int)'. | |
9923 | A more subtle example is: | |
9924 | ||
9925 | template <class T> struct S { template <class U> void f(T, U); } | |
9926 | ||
9927 | Here, if DECL is `void S<int>::f(int, double)', PARMS should be | |
9928 | {int, U}. Thus, the args that we want to subsitute into the | |
9929 | return and parameter type for the function are those in TARGS, | |
9930 | with the innermost level omitted. */ | |
9931 | fn_type = TREE_TYPE (tmpl); | |
9932 | if (DECL_STATIC_FUNCTION_P (decl)) | |
4f1c5b7d | 9933 | context = DECL_CONTEXT (decl); |
36a117a5 MM |
9934 | |
9935 | if (parm_depth == 1) | |
9936 | /* No substitution is necessary. */ | |
9937 | ; | |
9938 | else | |
9939 | { | |
9940 | int i; | |
9941 | tree partial_args; | |
9942 | ||
9943 | /* Replace the innermost level of the TARGS with NULL_TREEs to | |
9944 | let tsubst know not to subsitute for those parameters. */ | |
f31c0a32 | 9945 | partial_args = make_tree_vec (TREE_VEC_LENGTH (targs)); |
36a117a5 MM |
9946 | for (i = 1; i < TMPL_ARGS_DEPTH (targs); ++i) |
9947 | SET_TMPL_ARGS_LEVEL (partial_args, i, | |
9948 | TMPL_ARGS_LEVEL (targs, i)); | |
9949 | SET_TMPL_ARGS_LEVEL (partial_args, | |
9950 | TMPL_ARGS_DEPTH (targs), | |
f31c0a32 | 9951 | make_tree_vec (DECL_NTPARMS (tmpl))); |
36a117a5 MM |
9952 | |
9953 | /* Now, do the (partial) substitution to figure out the | |
9954 | appropriate function type. */ | |
4393e105 | 9955 | fn_type = tsubst (fn_type, partial_args, /*complain=*/1, NULL_TREE); |
36a117a5 | 9956 | if (DECL_STATIC_FUNCTION_P (decl)) |
4393e105 | 9957 | context = tsubst (context, partial_args, /*complain=*/1, NULL_TREE); |
36a117a5 MM |
9958 | |
9959 | /* Substitute into the template parameters to obtain the real | |
9960 | innermost set of parameters. This step is important if the | |
9961 | innermost set of template parameters contains value | |
9962 | parameters whose types depend on outer template parameters. */ | |
9963 | TREE_VEC_LENGTH (partial_args)--; | |
4393e105 | 9964 | tparms = tsubst_template_parms (tparms, partial_args, /*complain=*/1); |
36a117a5 MM |
9965 | } |
9966 | ||
9967 | /* Now, get the innermost parameters and arguments, and figure out | |
9968 | the parameter and return types. */ | |
9969 | tparms = INNERMOST_TEMPLATE_PARMS (tparms); | |
9970 | targs = innermost_args (targs); | |
9971 | ret_type = TREE_TYPE (fn_type); | |
9972 | parm_types = TYPE_ARG_TYPES (fn_type); | |
9973 | ||
9974 | /* For a static member function, we generate a fake `this' pointer, | |
9975 | for the purposes of mangling. This indicates of which class the | |
9976 | function is a member. Because of: | |
9977 | ||
9978 | [class.static] | |
9979 | ||
9980 | There shall not be a static and a nonstatic member function | |
9981 | with the same name and the same parameter types | |
9982 | ||
9983 | we don't have to worry that this will result in a clash with a | |
9984 | non-static member function. */ | |
9985 | if (DECL_STATIC_FUNCTION_P (decl)) | |
9986 | parm_types = hash_tree_chain (build_pointer_type (context), parm_types); | |
9987 | ||
9988 | /* There should be the same number of template parameters as | |
9989 | template arguments. */ | |
9990 | my_friendly_assert (TREE_VEC_LENGTH (tparms) == TREE_VEC_LENGTH (targs), | |
9991 | 0); | |
9992 | ||
9993 | /* If the template is in a namespace, we need to put that into the | |
9994 | mangled name. Unfortunately, build_decl_overload_real does not | |
9995 | get the decl to mangle, so it relies on the current | |
9996 | namespace. Therefore, we set that here temporarily. */ | |
2f939d94 | 9997 | my_friendly_assert (DECL_P (decl), 980702); |
36a117a5 MM |
9998 | saved_namespace = current_namespace; |
9999 | current_namespace = CP_DECL_CONTEXT (decl); | |
10000 | ||
10001 | /* Actually set the DCL_ASSEMBLER_NAME. */ | |
10002 | DECL_ASSEMBLER_NAME (decl) | |
10003 | = build_decl_overload_real (DECL_NAME (decl), parm_types, ret_type, | |
10004 | tparms, targs, | |
10005 | DECL_FUNCTION_MEMBER_P (decl) | |
db9b2174 | 10006 | + DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl)); |
36a117a5 MM |
10007 | |
10008 | /* Restore the previously active namespace. */ | |
10009 | current_namespace = saved_namespace; | |
10010 | } |