]>
Commit | Line | Data |
---|---|---|
8d08fdba | 1 | /* Handle parameterized types (templates) for GNU C++. |
956d6950 | 2 | Copyright (C) 1992, 93, 94, 95, 96, 1997 Free Software Foundation, Inc. |
8d08fdba | 3 | Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing. |
fc378698 | 4 | Rewritten by Jason Merrill (jason@cygnus.com). |
8d08fdba MS |
5 | |
6 | This file is part of GNU CC. | |
7 | ||
8 | GNU CC is free software; you can redistribute it and/or modify | |
9 | it under the terms of the GNU General Public License as published by | |
10 | the Free Software Foundation; either version 2, or (at your option) | |
11 | any later version. | |
12 | ||
13 | GNU CC is distributed in the hope that it will be useful, | |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
19 | along with GNU CC; see the file COPYING. If not, write to | |
e9fa0c7c RK |
20 | the Free Software Foundation, 59 Temple Place - Suite 330, |
21 | Boston, MA 02111-1307, USA. */ | |
8d08fdba MS |
22 | |
23 | /* Known bugs or deficiencies include: | |
e92cc029 | 24 | |
e92cc029 MS |
25 | all methods must be provided in header files; can't use a source |
26 | file that contains only the method templates and "just win". */ | |
8d08fdba MS |
27 | |
28 | #include "config.h" | |
8d052bc7 | 29 | #include "system.h" |
8d08fdba MS |
30 | #include "obstack.h" |
31 | ||
32 | #include "tree.h" | |
33 | #include "flags.h" | |
34 | #include "cp-tree.h" | |
35 | #include "decl.h" | |
36 | #include "parse.h" | |
f0e01782 | 37 | #include "lex.h" |
e8abc66f | 38 | #include "output.h" |
a9aedbc2 | 39 | #include "defaults.h" |
49c249e1 | 40 | #include "except.h" |
54f92bfb | 41 | #include "toplev.h" |
49c249e1 | 42 | |
050367a3 MM |
43 | /* The type of functions taking a tree, and some additional data, and |
44 | returning an int. */ | |
45 | typedef int (*tree_fn_t) PROTO((tree, void*)); | |
46 | ||
8d08fdba | 47 | extern struct obstack permanent_obstack; |
8d08fdba MS |
48 | |
49 | extern int lineno; | |
50 | extern char *input_filename; | |
51 | struct pending_inline *pending_template_expansions; | |
52 | ||
5566b478 MS |
53 | tree current_template_parms; |
54 | HOST_WIDE_INT processing_template_decl; | |
8d08fdba | 55 | |
5566b478 MS |
56 | tree pending_templates; |
57 | static tree *template_tail = &pending_templates; | |
58 | ||
73aad9b9 JM |
59 | tree maybe_templates; |
60 | static tree *maybe_template_tail = &maybe_templates; | |
61 | ||
5566b478 | 62 | int minimal_parse_mode; |
75b0bbce | 63 | |
92eca640 | 64 | int processing_specialization; |
75650646 | 65 | int processing_explicit_instantiation; |
67ffc812 | 66 | int processing_template_parmlist; |
386b8a85 JM |
67 | static int template_header_count; |
68 | ||
75650646 MM |
69 | static tree saved_trees; |
70 | ||
8d08fdba MS |
71 | #define obstack_chunk_alloc xmalloc |
72 | #define obstack_chunk_free free | |
73 | ||
830bfa74 MM |
74 | #define UNIFY_ALLOW_NONE 0 |
75 | #define UNIFY_ALLOW_MORE_CV_QUAL 1 | |
76 | #define UNIFY_ALLOW_LESS_CV_QUAL 2 | |
77 | #define UNIFY_ALLOW_DERIVED 4 | |
78 | ||
34016c81 JM |
79 | static int unify PROTO((tree, tree, tree, tree, int, int*)); |
80 | static int resolve_overloaded_unification PROTO((tree, tree, tree, tree, | |
81 | unification_kind_t, int, | |
82 | int*)); | |
83 | static int try_one_overload PROTO((tree, tree, tree, tree, | |
84 | unification_kind_t, int, int*)); | |
830bfa74 | 85 | static int unify PROTO((tree, tree, tree, tree, int, int*)); |
49c249e1 JM |
86 | static void add_pending_template PROTO((tree)); |
87 | static int push_tinst_level PROTO((tree)); | |
88 | static tree classtype_mangled_name PROTO((tree)); | |
36a117a5 | 89 | static char *mangle_class_name_for_template PROTO((char *, tree, tree)); |
49c249e1 | 90 | static tree tsubst_expr_values PROTO((tree, tree)); |
49c249e1 | 91 | static int list_eq PROTO((tree, tree)); |
36a117a5 | 92 | static tree get_class_bindings PROTO((tree, tree, tree)); |
744fac59 | 93 | static tree coerce_template_parms PROTO((tree, tree, tree, int, int)); |
dbfe2124 | 94 | static void tsubst_enum PROTO((tree, tree, tree)); |
98c1c668 | 95 | static tree add_to_template_args PROTO((tree, tree)); |
36a117a5 | 96 | static tree add_outermost_template_args PROTO((tree, tree)); |
830bfa74 MM |
97 | static void maybe_adjust_types_for_deduction PROTO((unification_kind_t, tree*, |
98 | tree*)); | |
050367a3 | 99 | static int type_unification_real PROTO((tree, tree, tree, tree, |
830bfa74 | 100 | int, unification_kind_t, int, int*)); |
386b8a85 | 101 | static void note_template_header PROTO((int)); |
840b09b4 | 102 | static tree maybe_fold_nontype_arg PROTO((tree)); |
e1467ff2 | 103 | static tree convert_nontype_argument PROTO((tree, tree)); |
8b5b8b7c MM |
104 | static tree convert_template_argument PROTO ((tree, tree, tree, int, |
105 | int , tree)); | |
76b9a14d | 106 | static tree get_bindings_overload PROTO((tree, tree, tree)); |
050367a3 | 107 | static int for_each_template_parm PROTO((tree, tree_fn_t, void*)); |
4b580446 | 108 | static tree build_template_parm_index PROTO((int, int, int, tree, tree)); |
69ac77ce JL |
109 | static int inline_needs_template_parms PROTO((tree)); |
110 | static void push_inline_template_parms_recursive PROTO((tree, int)); | |
111 | static tree retrieve_specialization PROTO((tree, tree)); | |
36a117a5 MM |
112 | static tree register_specialization PROTO((tree, tree, tree)); |
113 | static int unregister_specialization PROTO((tree, tree)); | |
69ac77ce JL |
114 | static void print_candidates PROTO((tree)); |
115 | static tree reduce_template_parm_level PROTO((tree, tree, int)); | |
116 | static tree build_template_decl PROTO((tree, tree)); | |
117 | static int mark_template_parm PROTO((tree, void *)); | |
118 | static tree tsubst_friend_function PROTO((tree, tree)); | |
6757edfe | 119 | static tree tsubst_friend_class PROTO((tree, tree)); |
69ac77ce | 120 | static tree get_bindings_real PROTO((tree, tree, tree, int)); |
04a81d94 | 121 | static int template_decl_level PROTO((tree)); |
a2b60a0e | 122 | static tree maybe_get_template_decl_from_type_decl PROTO((tree)); |
830bfa74 MM |
123 | static int check_cv_quals_for_unify PROTO((int, tree, tree)); |
124 | static tree tsubst_template_arg_vector PROTO((tree, tree)); | |
36a117a5 | 125 | static tree tsubst_template_parms PROTO((tree, tree)); |
830bfa74 | 126 | static void regenerate_decl_from_template PROTO((tree, tree)); |
36a117a5 MM |
127 | static tree most_specialized PROTO((tree, tree, tree)); |
128 | static tree most_specialized_class PROTO((tree, tree)); | |
129 | static tree most_general_template PROTO((tree)); | |
130 | static void set_mangled_name_for_template_decl PROTO((tree)); | |
39c01e4c | 131 | static int template_class_depth_real PROTO((tree, int)); |
ae58fa02 MM |
132 | static tree tsubst_aggr_type PROTO((tree, tree, tree, int)); |
133 | static tree tsubst_decl PROTO((tree, tree, tree, tree)); | |
cabc336a | 134 | static tree tsubst_arg_types PROTO((tree, tree, tree)); |
6c30752f MM |
135 | static void check_specialization_scope PROTO((void)); |
136 | static tree process_partial_specialization PROTO((tree)); | |
61a127b3 | 137 | static void set_current_access_from_decl PROTO((tree)); |
36a117a5 MM |
138 | |
139 | /* We use TREE_VECs to hold template arguments. If there is only one | |
140 | level of template arguments, then the TREE_VEC contains the | |
141 | arguments directly. If there is more than one level of template | |
142 | arguments, then each entry in the TREE_VEC is itself a TREE_VEC, | |
143 | containing the template arguments for a single level. The first | |
144 | entry in the outer TREE_VEC is the outermost level of template | |
145 | parameters; the last is the innermost. | |
146 | ||
147 | It is incorrect to ever form a template argument vector containing | |
148 | only one level of arguments, but which is a TREE_VEC containing as | |
149 | its only entry the TREE_VEC for that level. */ | |
150 | ||
36a117a5 MM |
151 | /* Non-zero if the template arguments is actually a vector of vectors, |
152 | rather than just a vector. */ | |
153 | #define TMPL_ARGS_HAVE_MULTIPLE_LEVELS(NODE) \ | |
154 | (NODE != NULL_TREE \ | |
155 | && TREE_CODE (NODE) == TREE_VEC \ | |
156 | && TREE_VEC_LENGTH (NODE) > 0 \ | |
157 | && TREE_VEC_ELT (NODE, 0) != NULL_TREE \ | |
67ffc812 MM |
158 | && TREE_CODE (TREE_VEC_ELT (NODE, 0)) == TREE_VEC) |
159 | ||
e4a84209 MM |
160 | /* The depth of a template argument vector. When called directly by |
161 | the parser, we use a TREE_LIST rather than a TREE_VEC to represent | |
162 | template arguments. In fact, we may even see NULL_TREE if there | |
163 | are no template arguments. In both of those cases, there is only | |
164 | one level of template arguments. */ | |
165 | #define TMPL_ARGS_DEPTH(NODE) \ | |
166 | (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (NODE) ? TREE_VEC_LENGTH (NODE) : 1) | |
167 | ||
36a117a5 MM |
168 | /* The LEVELth level of the template ARGS. Note that template |
169 | parameter levels are indexed from 1, not from 0. */ | |
170 | #define TMPL_ARGS_LEVEL(ARGS, LEVEL) \ | |
171 | (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (ARGS) \ | |
172 | ? TREE_VEC_ELT ((ARGS), (LEVEL) - 1) : ARGS) | |
173 | ||
174 | /* Set the LEVELth level of the template ARGS to VAL. This macro does | |
175 | not work with single-level argument vectors. */ | |
176 | #define SET_TMPL_ARGS_LEVEL(ARGS, LEVEL, VAL) \ | |
177 | (TREE_VEC_ELT ((ARGS), (LEVEL) - 1) = (VAL)) | |
178 | ||
179 | /* Accesses the IDXth parameter in the LEVELth level of the ARGS. */ | |
180 | #define TMPL_ARG(ARGS, LEVEL, IDX) \ | |
181 | (TREE_VEC_ELT (TMPL_ARGS_LEVEL (ARGS, LEVEL), IDX)) | |
182 | ||
183 | /* Set the IDXth element in the LEVELth level of ARGS to VAL. This | |
184 | macro does not work with single-level argument vectors. */ | |
185 | #define SET_TMPL_ARG(ARGS, LEVEL, IDX, VAL) \ | |
186 | (TREE_VEC_ELT (TREE_VEC_ELT ((ARGS), (LEVEL) - 1), (IDX)) = (VAL)) | |
187 | ||
e4a84209 MM |
188 | /* Given a single level of template arguments in NODE, return the |
189 | number of arguments. */ | |
190 | #define NUM_TMPL_ARGS(NODE) \ | |
191 | ((NODE) == NULL_TREE ? 0 \ | |
192 | : (TREE_CODE (NODE) == TREE_VEC \ | |
193 | ? TREE_VEC_LENGTH (NODE) : list_length (NODE))) | |
194 | ||
36a117a5 MM |
195 | /* The number of levels of template parameters given by NODE. */ |
196 | #define TMPL_PARMS_DEPTH(NODE) \ | |
197 | (TREE_INT_CST_HIGH (TREE_PURPOSE (NODE))) | |
198 | ||
e1467ff2 MM |
199 | /* Do any processing required when DECL (a member template declaration |
200 | using TEMPLATE_PARAMETERS as its innermost parameter list) is | |
201 | finished. Returns the TEMPLATE_DECL corresponding to DECL, unless | |
202 | it is a specialization, in which case the DECL itself is returned. */ | |
203 | ||
204 | tree | |
61a127b3 | 205 | finish_member_template_decl (decl) |
e1467ff2 MM |
206 | tree decl; |
207 | { | |
93cdc044 JM |
208 | if (decl == NULL_TREE || decl == void_type_node) |
209 | return NULL_TREE; | |
3ddfb0e6 MM |
210 | else if (decl == error_mark_node) |
211 | /* By returning NULL_TREE, the parser will just ignore this | |
212 | declaration. We have already issued the error. */ | |
213 | return NULL_TREE; | |
93cdc044 JM |
214 | else if (TREE_CODE (decl) == TREE_LIST) |
215 | { | |
5f311aec | 216 | /* Assume that the class is the only declspec. */ |
93cdc044 | 217 | decl = TREE_VALUE (decl); |
8d019cef JM |
218 | if (IS_AGGR_TYPE (decl) && CLASSTYPE_TEMPLATE_INFO (decl) |
219 | && ! CLASSTYPE_TEMPLATE_SPECIALIZATION (decl)) | |
93cdc044 JM |
220 | { |
221 | tree tmpl = CLASSTYPE_TI_TEMPLATE (decl); | |
222 | check_member_template (tmpl); | |
223 | return tmpl; | |
224 | } | |
8d019cef | 225 | return NULL_TREE; |
93cdc044 | 226 | } |
a1da6cba | 227 | else if (DECL_TEMPLATE_INFO (decl)) |
e1467ff2 | 228 | { |
a1da6cba MM |
229 | if (!DECL_TEMPLATE_SPECIALIZATION (decl)) |
230 | { | |
231 | check_member_template (DECL_TI_TEMPLATE (decl)); | |
232 | return DECL_TI_TEMPLATE (decl); | |
233 | } | |
234 | else | |
235 | return decl; | |
236 | } | |
237 | else | |
8251199e | 238 | cp_error ("invalid member template declaration `%D'", decl); |
e1467ff2 | 239 | |
a1da6cba | 240 | return error_mark_node; |
f84b4be9 | 241 | } |
e1467ff2 | 242 | |
f84b4be9 JM |
243 | /* Returns the template nesting level of the indicated class TYPE. |
244 | ||
245 | For example, in: | |
246 | template <class T> | |
247 | struct A | |
248 | { | |
249 | template <class U> | |
250 | struct B {}; | |
251 | }; | |
252 | ||
39c01e4c MM |
253 | A<T>::B<U> has depth two, while A<T> has depth one. |
254 | Both A<T>::B<int> and A<int>::B<U> have depth one, if | |
255 | COUNT_SPECIALIZATIONS is 0 or if they are instantiations, not | |
256 | specializations. | |
257 | ||
258 | This function is guaranteed to return 0 if passed NULL_TREE so | |
259 | that, for example, `template_class_depth (current_class_type)' is | |
260 | always safe. */ | |
f84b4be9 JM |
261 | |
262 | int | |
39c01e4c | 263 | template_class_depth_real (type, count_specializations) |
f84b4be9 | 264 | tree type; |
39c01e4c | 265 | int count_specializations; |
f84b4be9 | 266 | { |
93cdc044 | 267 | int depth; |
f84b4be9 | 268 | |
2c73f9f5 | 269 | for (depth = 0; |
ed44da02 MM |
270 | type && TREE_CODE (type) != NAMESPACE_DECL; |
271 | type = (TREE_CODE (type) == FUNCTION_DECL) | |
272 | ? DECL_REAL_CONTEXT (type) : TYPE_CONTEXT (type)) | |
273 | { | |
274 | if (TREE_CODE (type) != FUNCTION_DECL) | |
275 | { | |
276 | if (CLASSTYPE_TEMPLATE_INFO (type) | |
277 | && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)) | |
278 | && ((count_specializations | |
279 | && CLASSTYPE_TEMPLATE_SPECIALIZATION (type)) | |
280 | || uses_template_parms (CLASSTYPE_TI_ARGS (type)))) | |
281 | ++depth; | |
282 | } | |
283 | else | |
284 | { | |
285 | if (DECL_TEMPLATE_INFO (type) | |
286 | && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (type)) | |
287 | && ((count_specializations | |
288 | && DECL_TEMPLATE_SPECIALIZATION (type)) | |
289 | || uses_template_parms (DECL_TI_ARGS (type)))) | |
290 | ++depth; | |
291 | } | |
292 | } | |
f84b4be9 JM |
293 | |
294 | return depth; | |
e1467ff2 | 295 | } |
98c1c668 | 296 | |
39c01e4c MM |
297 | /* Returns the template nesting level of the indicated class TYPE. |
298 | Like template_class_depth_real, but instantiations do not count in | |
299 | the depth. */ | |
300 | ||
301 | int | |
302 | template_class_depth (type) | |
303 | tree type; | |
304 | { | |
305 | return template_class_depth_real (type, /*count_specializations=*/0); | |
306 | } | |
307 | ||
cae40af6 JM |
308 | /* Returns 1 if processing DECL as part of do_pending_inlines |
309 | needs us to push template parms. */ | |
310 | ||
311 | static int | |
312 | inline_needs_template_parms (decl) | |
313 | tree decl; | |
314 | { | |
315 | if (! DECL_TEMPLATE_INFO (decl)) | |
316 | return 0; | |
f84b4be9 | 317 | |
36a117a5 | 318 | return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl))) |
cae40af6 JM |
319 | > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl))); |
320 | } | |
321 | ||
322 | /* Subroutine of maybe_begin_member_template_processing. | |
323 | Push the template parms in PARMS, starting from LEVELS steps into the | |
324 | chain, and ending at the beginning, since template parms are listed | |
325 | innermost first. */ | |
326 | ||
327 | static void | |
328 | push_inline_template_parms_recursive (parmlist, levels) | |
329 | tree parmlist; | |
330 | int levels; | |
331 | { | |
332 | tree parms = TREE_VALUE (parmlist); | |
333 | int i; | |
334 | ||
335 | if (levels > 1) | |
336 | push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1); | |
786b5245 | 337 | |
98c1c668 | 338 | ++processing_template_decl; |
cae40af6 | 339 | current_template_parms |
98c1c668 JM |
340 | = tree_cons (build_int_2 (0, processing_template_decl), |
341 | parms, current_template_parms); | |
cae40af6 JM |
342 | TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1; |
343 | ||
786b5245 | 344 | pushlevel (0); |
98c1c668 JM |
345 | for (i = 0; i < TREE_VEC_LENGTH (parms); ++i) |
346 | { | |
786b5245 MM |
347 | tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i)); |
348 | my_friendly_assert (TREE_CODE_CLASS (TREE_CODE (parm)) == 'd', 0); | |
cae40af6 | 349 | |
98c1c668 JM |
350 | switch (TREE_CODE (parm)) |
351 | { | |
786b5245 | 352 | case TYPE_DECL: |
73b0fce8 | 353 | case TEMPLATE_DECL: |
98c1c668 JM |
354 | pushdecl (parm); |
355 | break; | |
786b5245 MM |
356 | |
357 | case PARM_DECL: | |
358 | { | |
359 | /* Make a CONST_DECL as is done in process_template_parm. */ | |
360 | tree decl = build_decl (CONST_DECL, DECL_NAME (parm), | |
361 | TREE_TYPE (parm)); | |
362 | DECL_INITIAL (decl) = DECL_INITIAL (parm); | |
363 | pushdecl (decl); | |
364 | } | |
cae40af6 | 365 | break; |
786b5245 | 366 | |
98c1c668 JM |
367 | default: |
368 | my_friendly_abort (0); | |
369 | } | |
370 | } | |
371 | } | |
372 | ||
cae40af6 JM |
373 | /* Restore the template parameter context for a member template or |
374 | a friend template defined in a class definition. */ | |
375 | ||
376 | void | |
377 | maybe_begin_member_template_processing (decl) | |
378 | tree decl; | |
379 | { | |
380 | tree parms; | |
381 | int levels; | |
382 | ||
383 | if (! inline_needs_template_parms (decl)) | |
384 | return; | |
385 | ||
36a117a5 | 386 | parms = DECL_TEMPLATE_PARMS (most_general_template (decl)); |
cae40af6 | 387 | |
36a117a5 | 388 | levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl; |
cae40af6 JM |
389 | |
390 | if (DECL_TEMPLATE_SPECIALIZATION (decl)) | |
391 | { | |
392 | --levels; | |
393 | parms = TREE_CHAIN (parms); | |
394 | } | |
395 | ||
396 | push_inline_template_parms_recursive (parms, levels); | |
397 | } | |
398 | ||
98c1c668 JM |
399 | /* Undo the effects of begin_member_template_processing. */ |
400 | ||
401 | void | |
b370501f | 402 | maybe_end_member_template_processing () |
98c1c668 JM |
403 | { |
404 | if (! processing_template_decl) | |
405 | return; | |
406 | ||
cae40af6 JM |
407 | while (current_template_parms |
408 | && TEMPLATE_PARMS_FOR_INLINE (current_template_parms)) | |
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 | |
428 | to be a member template. */ | |
98c1c668 | 429 | |
cae40af6 JM |
430 | int |
431 | is_member_template (t) | |
98c1c668 JM |
432 | tree t; |
433 | { | |
aa5f3bad MM |
434 | if (TREE_CODE (t) != FUNCTION_DECL |
435 | && !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 JM |
440 | /* A local class can't have member templates. */ |
441 | if (hack_decl_function_context (t)) | |
442 | return 0; | |
443 | ||
444 | if ((DECL_FUNCTION_MEMBER_P (t) | |
386b8a85 | 445 | && !DECL_TEMPLATE_SPECIALIZATION (t)) |
f84b4be9 | 446 | || (TREE_CODE (t) == TEMPLATE_DECL |
cae40af6 | 447 | && DECL_FUNCTION_MEMBER_P (DECL_TEMPLATE_RESULT (t)))) |
98c1c668 | 448 | { |
f84b4be9 | 449 | tree tmpl; |
98c1c668 JM |
450 | |
451 | if (DECL_FUNCTION_TEMPLATE_P (t)) | |
452 | tmpl = t; | |
453 | else if (DECL_TEMPLATE_INFO (t) | |
454 | && DECL_FUNCTION_TEMPLATE_P (DECL_TI_TEMPLATE (t))) | |
455 | tmpl = DECL_TI_TEMPLATE (t); | |
f84b4be9 JM |
456 | else |
457 | tmpl = NULL_TREE; | |
458 | ||
cae40af6 | 459 | if (tmpl |
f84b4be9 JM |
460 | /* If there are more levels of template parameters than |
461 | there are template classes surrounding the declaration, | |
462 | then we have a member template. */ | |
36a117a5 | 463 | && (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)) > |
cae40af6 | 464 | template_class_depth (DECL_CLASS_CONTEXT (t)))) |
f84b4be9 JM |
465 | return 1; |
466 | } | |
98c1c668 | 467 | |
f84b4be9 JM |
468 | return 0; |
469 | } | |
98c1c668 | 470 | |
ba4f4e5d | 471 | #if 0 /* UNUSED */ |
2ee4e084 MM |
472 | /* Returns non-zero iff T is a member template class. See |
473 | is_member_template for a description of what precisely constitutes | |
474 | a member template. */ | |
475 | ||
476 | int | |
477 | is_member_template_class (t) | |
478 | tree t; | |
479 | { | |
480 | if (!DECL_CLASS_TEMPLATE_P (t)) | |
481 | /* Anything that isn't a class template, is certainly not a member | |
482 | template. */ | |
483 | return 0; | |
484 | ||
485 | if (!DECL_CLASS_SCOPE_P (t)) | |
486 | /* Anything whose context isn't a class type is surely not a | |
487 | member template. */ | |
488 | return 0; | |
489 | ||
490 | /* If there are more levels of template parameters than there are | |
491 | template classes surrounding the declaration, then we have a | |
492 | member template. */ | |
36a117a5 | 493 | return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (t)) > |
2ee4e084 MM |
494 | template_class_depth (DECL_CONTEXT (t))); |
495 | } | |
ba4f4e5d | 496 | #endif |
2ee4e084 | 497 | |
36a117a5 MM |
498 | /* Return a new template argument vector which contains all of ARGS, |
499 | but has as its innermost set of arguments the EXTRA_ARGS. The | |
500 | resulting vector will be built on a temporary obstack, and so must | |
501 | be explicitly copied to the permanent obstack, if required. */ | |
e6f1275f JM |
502 | |
503 | static tree | |
36a117a5 MM |
504 | add_to_template_args (args, extra_args) |
505 | tree args; | |
506 | tree extra_args; | |
e6f1275f | 507 | { |
36a117a5 MM |
508 | tree new_args; |
509 | int extra_depth; | |
510 | int i; | |
511 | int j; | |
e6f1275f | 512 | |
36a117a5 MM |
513 | extra_depth = TMPL_ARGS_DEPTH (extra_args); |
514 | new_args = make_temp_vec (TMPL_ARGS_DEPTH (args) + extra_depth); | |
e6f1275f | 515 | |
36a117a5 MM |
516 | for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i) |
517 | SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i)); | |
e6f1275f | 518 | |
36a117a5 MM |
519 | for (j = 1; j <= extra_depth; ++j, ++i) |
520 | SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j)); | |
521 | ||
e6f1275f JM |
522 | return new_args; |
523 | } | |
524 | ||
36a117a5 MM |
525 | /* Like add_to_template_args, but only the outermost ARGS are added to |
526 | the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH | |
527 | (EXTRA_ARGS) levels are added. This function is used to combine | |
528 | the template arguments from a partial instantiation with the | |
529 | template arguments used to attain the full instantiation from the | |
530 | partial instantiation. */ | |
98c1c668 | 531 | |
4966381a | 532 | static tree |
36a117a5 | 533 | add_outermost_template_args (args, extra_args) |
98c1c668 JM |
534 | tree args; |
535 | tree extra_args; | |
536 | { | |
537 | tree new_args; | |
538 | ||
e4a84209 MM |
539 | /* If there are more levels of EXTRA_ARGS than there are ARGS, |
540 | something very fishy is going on. */ | |
541 | my_friendly_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args), | |
542 | 0); | |
543 | ||
544 | /* If *all* the new arguments will be the EXTRA_ARGS, just return | |
545 | them. */ | |
546 | if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args)) | |
547 | return extra_args; | |
548 | ||
36a117a5 MM |
549 | /* For the moment, we make ARGS look like it contains fewer levels. */ |
550 | TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args); | |
551 | ||
552 | new_args = add_to_template_args (args, extra_args); | |
98c1c668 | 553 | |
36a117a5 MM |
554 | /* Now, we restore ARGS to its full dimensions. */ |
555 | TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args); | |
98c1c668 JM |
556 | |
557 | return new_args; | |
558 | } | |
5566b478 MS |
559 | |
560 | /* We've got a template header coming up; push to a new level for storing | |
561 | the parms. */ | |
8d08fdba | 562 | |
8d08fdba MS |
563 | void |
564 | begin_template_parm_list () | |
565 | { | |
6757edfe MM |
566 | /* We use a non-tag-transparent scope here, which causes pushtag to |
567 | put tags in this scope, rather than in the enclosing class or | |
568 | namespace scope. This is the right thing, since we want | |
569 | TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a | |
570 | global template class, push_template_decl handles putting the | |
571 | TEMPLATE_DECL into top-level scope. For a nested template class, | |
572 | e.g.: | |
573 | ||
574 | template <class T> struct S1 { | |
575 | template <class T> struct S2 {}; | |
576 | }; | |
577 | ||
578 | pushtag contains special code to call pushdecl_with_scope on the | |
579 | TEMPLATE_DECL for S2. */ | |
8d08fdba | 580 | pushlevel (0); |
5566b478 | 581 | declare_pseudo_global_level (); |
5156628f | 582 | ++processing_template_decl; |
67ffc812 | 583 | ++processing_template_parmlist; |
386b8a85 JM |
584 | note_template_header (0); |
585 | } | |
586 | ||
6c30752f MM |
587 | /* This routine is called when a specialization is declared. If it is |
588 | illegal to declare a specialization here, an error is reported. */ | |
589 | ||
590 | void | |
591 | check_specialization_scope () | |
592 | { | |
593 | tree scope = current_scope (); | |
3ddfb0e6 | 594 | |
6c30752f MM |
595 | /* [temp.expl.spec] |
596 | ||
597 | An explicit specialization shall be declared in the namespace of | |
598 | which the template is a member, or, for member templates, in the | |
599 | namespace of which the enclosing class or enclosing class | |
600 | template is a member. An explicit specialization of a member | |
601 | function, member class or static data member of a class template | |
602 | shall be declared in the namespace of which the class template | |
603 | is a member. */ | |
604 | if (scope && TREE_CODE (scope) != NAMESPACE_DECL) | |
8251199e | 605 | cp_error ("explicit specialization in non-namespace scope `%D'", |
6c30752f | 606 | scope); |
3ddfb0e6 | 607 | |
6c30752f MM |
608 | /* [temp.expl.spec] |
609 | ||
610 | In an explicit specialization declaration for a member of a class | |
611 | template or a member template that appears in namespace scope, | |
612 | the member template and some of its enclosing class templates may | |
613 | remain unspecialized, except that the declaration shall not | |
614 | explicitly specialize a class member template if its enclosing | |
615 | class templates are not explicitly specialized as well. */ | |
616 | if (current_template_parms) | |
fd4de5ff | 617 | cp_error ("enclosing class templates are not explicitly specialized"); |
6c30752f MM |
618 | } |
619 | ||
386b8a85 JM |
620 | /* We've just seen template <>. */ |
621 | ||
622 | void | |
623 | begin_specialization () | |
624 | { | |
625 | note_template_header (1); | |
6c30752f | 626 | check_specialization_scope (); |
386b8a85 JM |
627 | } |
628 | ||
386b8a85 JM |
629 | /* Called at then end of processing a declaration preceeded by |
630 | template<>. */ | |
631 | ||
632 | void | |
633 | end_specialization () | |
634 | { | |
635 | reset_specialization (); | |
636 | } | |
637 | ||
386b8a85 JM |
638 | /* Any template <>'s that we have seen thus far are not referring to a |
639 | function specialization. */ | |
640 | ||
641 | void | |
642 | reset_specialization () | |
643 | { | |
644 | processing_specialization = 0; | |
645 | template_header_count = 0; | |
646 | } | |
647 | ||
386b8a85 JM |
648 | /* We've just seen a template header. If SPECIALIZATION is non-zero, |
649 | it was of the form template <>. */ | |
650 | ||
4966381a | 651 | static void |
386b8a85 JM |
652 | note_template_header (specialization) |
653 | int specialization; | |
654 | { | |
655 | processing_specialization = specialization; | |
656 | template_header_count++; | |
657 | } | |
658 | ||
75650646 | 659 | /* We're beginning an explicit instantiation. */ |
386b8a85 | 660 | |
75650646 MM |
661 | void |
662 | begin_explicit_instantiation () | |
386b8a85 | 663 | { |
75650646 MM |
664 | ++processing_explicit_instantiation; |
665 | } | |
386b8a85 | 666 | |
386b8a85 | 667 | |
75650646 MM |
668 | void |
669 | end_explicit_instantiation () | |
670 | { | |
671 | my_friendly_assert(processing_explicit_instantiation > 0, 0); | |
672 | --processing_explicit_instantiation; | |
673 | } | |
386b8a85 | 674 | |
36a117a5 MM |
675 | /* The TYPE is being declared. If it is a template type, that means it |
676 | is a partial specialization. Do appropriate error-checking. */ | |
677 | ||
678 | void | |
679 | maybe_process_partial_specialization (type) | |
680 | tree type; | |
681 | { | |
682 | if (IS_AGGR_TYPE (type) && CLASSTYPE_USE_TEMPLATE (type)) | |
683 | { | |
684 | if (CLASSTYPE_IMPLICIT_INSTANTIATION (type) | |
685 | && TYPE_SIZE (type) == NULL_TREE) | |
686 | { | |
687 | SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type); | |
688 | if (processing_template_decl) | |
689 | push_template_decl (TYPE_MAIN_DECL (type)); | |
690 | } | |
691 | else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type)) | |
8251199e | 692 | cp_error ("specialization of `%T' after instantiation", type); |
36a117a5 MM |
693 | } |
694 | } | |
695 | ||
75650646 MM |
696 | /* Retrieve the specialization (in the sense of [temp.spec] - a |
697 | specialization is either an instantiation or an explicit | |
698 | specialization) of TMPL for the given template ARGS. If there is | |
699 | no such specialization, return NULL_TREE. The ARGS are a vector of | |
700 | arguments, or a vector of vectors of arguments, in the case of | |
701 | templates with more than one level of parameters. */ | |
702 | ||
703 | static tree | |
704 | retrieve_specialization (tmpl, args) | |
705 | tree tmpl; | |
706 | tree args; | |
707 | { | |
708 | tree s; | |
709 | ||
710 | my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 0); | |
711 | ||
36a117a5 MM |
712 | /* There should be as many levels of arguments as there are |
713 | levels of parameters. */ | |
714 | my_friendly_assert (TMPL_ARGS_DEPTH (args) | |
715 | == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)), | |
716 | 0); | |
717 | ||
75650646 MM |
718 | for (s = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); |
719 | s != NULL_TREE; | |
720 | s = TREE_CHAIN (s)) | |
721 | if (comp_template_args (TREE_PURPOSE (s), args)) | |
722 | return TREE_VALUE (s); | |
723 | ||
724 | return NULL_TREE; | |
386b8a85 JM |
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) | |
3bfdc719 MM |
752 | if (same_type_p (TYPE_MAIN_VARIANT (t), |
753 | TYPE_MAIN_VARIANT (TREE_TYPE (tmpl)))) | |
6757edfe MM |
754 | return 1; |
755 | } | |
386b8a85 | 756 | |
6757edfe MM |
757 | return 0; |
758 | } | |
75650646 MM |
759 | |
760 | /* Register the specialization SPEC as a specialization of TMPL with | |
36a117a5 MM |
761 | the indicated ARGS. Returns SPEC, or an equivalent prior |
762 | declaration, if available. */ | |
75650646 | 763 | |
36a117a5 | 764 | static tree |
75650646 MM |
765 | register_specialization (spec, tmpl, args) |
766 | tree spec; | |
767 | tree tmpl; | |
768 | tree args; | |
769 | { | |
770 | tree s; | |
771 | ||
772 | my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 0); | |
773 | ||
36a117a5 MM |
774 | if (TREE_CODE (spec) == FUNCTION_DECL |
775 | && uses_template_parms (DECL_TI_ARGS (spec))) | |
776 | /* This is the FUNCTION_DECL for a partial instantiation. Don't | |
777 | register it; we want the corresponding TEMPLATE_DECL instead. | |
778 | We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than | |
779 | the more obvious `uses_template_parms (spec)' to avoid problems | |
780 | with default function arguments. In particular, given | |
781 | something like this: | |
782 | ||
783 | template <class T> void f(T t1, T t = T()) | |
784 | ||
785 | the default argument expression is not substituted for in an | |
786 | instantiation unless and until it is actually needed. */ | |
787 | return spec; | |
75650646 | 788 | |
36a117a5 MM |
789 | /* There should be as many levels of arguments as there are |
790 | levels of parameters. */ | |
791 | my_friendly_assert (TMPL_ARGS_DEPTH (args) | |
792 | == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)), | |
793 | 0); | |
794 | ||
75650646 MM |
795 | for (s = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); |
796 | s != NULL_TREE; | |
797 | s = TREE_CHAIN (s)) | |
798 | if (comp_template_args (TREE_PURPOSE (s), args)) | |
799 | { | |
800 | tree fn = TREE_VALUE (s); | |
801 | ||
802 | if (DECL_TEMPLATE_SPECIALIZATION (spec)) | |
803 | { | |
804 | if (DECL_TEMPLATE_INSTANTIATION (fn)) | |
805 | { | |
806 | if (TREE_USED (fn) | |
807 | || DECL_EXPLICIT_INSTANTIATION (fn)) | |
808 | { | |
8251199e | 809 | cp_error ("specialization of %D after instantiation", |
75650646 | 810 | fn); |
36a117a5 | 811 | return spec; |
75650646 MM |
812 | } |
813 | else | |
814 | { | |
815 | /* This situation should occur only if the first | |
816 | specialization is an implicit instantiation, | |
817 | the second is an explicit specialization, and | |
818 | the implicit instantiation has not yet been | |
819 | used. That situation can occur if we have | |
36a117a5 MM |
820 | implicitly instantiated a member function and |
821 | then specialized it later. | |
822 | ||
823 | We can also wind up here if a friend | |
824 | declaration that looked like an instantiation | |
825 | turns out to be a specialization: | |
826 | ||
827 | template <class T> void foo(T); | |
828 | class S { friend void foo<>(int) }; | |
829 | template <> void foo(int); | |
830 | ||
831 | We transform the existing DECL in place so that | |
832 | any pointers to it become pointers to the | |
833 | updated declaration. */ | |
834 | duplicate_decls (spec, TREE_VALUE (s)); | |
835 | return TREE_VALUE (s); | |
75650646 MM |
836 | } |
837 | } | |
838 | else if (DECL_TEMPLATE_SPECIALIZATION (fn)) | |
839 | { | |
39c01e4c MM |
840 | duplicate_decls (spec, TREE_VALUE (s)); |
841 | return TREE_VALUE (s); | |
75650646 MM |
842 | } |
843 | } | |
844 | } | |
845 | ||
846 | DECL_TEMPLATE_SPECIALIZATIONS (tmpl) | |
847 | = perm_tree_cons (args, spec, DECL_TEMPLATE_SPECIALIZATIONS (tmpl)); | |
36a117a5 MM |
848 | |
849 | return spec; | |
850 | } | |
851 | ||
852 | /* Unregister the specialization SPEC as a specialization of TMPL. | |
853 | Returns nonzero if the SPEC was listed as a specialization of | |
854 | TMPL. */ | |
855 | ||
856 | static int | |
857 | unregister_specialization (spec, tmpl) | |
858 | tree spec; | |
859 | tree tmpl; | |
860 | { | |
861 | tree* s; | |
862 | ||
863 | for (s = &DECL_TEMPLATE_SPECIALIZATIONS (tmpl); | |
864 | *s != NULL_TREE; | |
865 | s = &TREE_CHAIN (*s)) | |
866 | if (TREE_VALUE (*s) == spec) | |
867 | { | |
868 | *s = TREE_CHAIN (*s); | |
869 | return 1; | |
870 | } | |
871 | ||
872 | return 0; | |
75650646 MM |
873 | } |
874 | ||
e1467ff2 MM |
875 | /* Print the list of candidate FNS in an error message. */ |
876 | ||
877 | static void | |
878 | print_candidates (fns) | |
879 | tree fns; | |
880 | { | |
881 | tree fn; | |
882 | ||
883 | char* str = "candidates are:"; | |
884 | ||
885 | for (fn = fns; fn != NULL_TREE; fn = TREE_CHAIN (fn)) | |
886 | { | |
8251199e | 887 | cp_error_at ("%s %+#D", str, TREE_VALUE (fn)); |
e1467ff2 MM |
888 | str = " "; |
889 | } | |
890 | } | |
891 | ||
75650646 | 892 | /* Returns the template (one of the functions given by TEMPLATE_ID) |
e1467ff2 MM |
893 | which can be specialized to match the indicated DECL with the |
894 | explicit template args given in TEMPLATE_ID. If | |
895 | NEED_MEMBER_TEMPLATE is true the function is a specialization of a | |
896 | member template. The template args (those explicitly specified and | |
897 | those deduced) are output in a newly created vector *TARGS_OUT. If | |
898 | it is impossible to determine the result, an error message is | |
899 | issued, unless COMPLAIN is 0. The DECL may be NULL_TREE if none is | |
900 | available. */ | |
75650646 | 901 | |
386b8a85 | 902 | tree |
e1467ff2 | 903 | determine_specialization (template_id, decl, targs_out, |
75650646 MM |
904 | need_member_template, |
905 | complain) | |
386b8a85 | 906 | tree template_id; |
e1467ff2 | 907 | tree decl; |
386b8a85 JM |
908 | tree* targs_out; |
909 | int need_member_template; | |
910 | int complain; | |
911 | { | |
f2e48b67 | 912 | tree fns, targs_in; |
e1467ff2 | 913 | tree templates = NULL_TREE; |
386b8a85 | 914 | tree fn; |
386b8a85 | 915 | |
e1467ff2 MM |
916 | *targs_out = NULL_TREE; |
917 | ||
f2e48b67 BK |
918 | if (template_id == error_mark_node) |
919 | return error_mark_node; | |
920 | ||
921 | fns = TREE_OPERAND (template_id, 0); | |
922 | targs_in = TREE_OPERAND (template_id, 1); | |
923 | ||
27fafc8d JM |
924 | if (fns == error_mark_node) |
925 | return error_mark_node; | |
926 | ||
2c73f9f5 ML |
927 | /* Check for baselinks. */ |
928 | if (TREE_CODE (fns) == TREE_LIST) | |
929 | fns = TREE_VALUE (fns); | |
386b8a85 | 930 | |
2c73f9f5 | 931 | for (; fns; fns = OVL_NEXT (fns)) |
386b8a85 | 932 | { |
75650646 MM |
933 | tree tmpl; |
934 | ||
2c73f9f5 | 935 | fn = OVL_CURRENT (fns); |
75650646 MM |
936 | if (!need_member_template |
937 | && TREE_CODE (fn) == FUNCTION_DECL | |
e1467ff2 | 938 | && DECL_FUNCTION_MEMBER_P (fn) |
75650646 MM |
939 | && DECL_USE_TEMPLATE (fn) |
940 | && DECL_TI_TEMPLATE (fn)) | |
e1467ff2 MM |
941 | /* We can get here when processing something like: |
942 | template <class T> class X { void f(); } | |
943 | template <> void X<int>::f() {} | |
944 | We're specializing a member function, but not a member | |
945 | template. */ | |
75650646 MM |
946 | tmpl = DECL_TI_TEMPLATE (fn); |
947 | else if (TREE_CODE (fn) != TEMPLATE_DECL | |
e1467ff2 | 948 | || (need_member_template && !is_member_template (fn))) |
798eed5e | 949 | continue; |
75650646 MM |
950 | else |
951 | tmpl = fn; | |
386b8a85 | 952 | |
75650646 | 953 | if (list_length (targs_in) > DECL_NTPARMS (tmpl)) |
386b8a85 JM |
954 | continue; |
955 | ||
e1467ff2 | 956 | if (decl == NULL_TREE) |
386b8a85 | 957 | { |
34016c81 JM |
958 | /* Unify against ourselves to make sure that the args we have |
959 | make sense and there aren't any undeducible parms. It's OK if | |
960 | not all the parms are specified; they might be deduced | |
961 | later. */ | |
962 | tree targs = get_bindings_overload (tmpl, DECL_RESULT (tmpl), | |
963 | targs_in); | |
964 | ||
965 | if (targs) | |
e1467ff2 MM |
966 | /* Unification was successful. */ |
967 | templates = scratch_tree_cons (targs, tmpl, templates); | |
386b8a85 | 968 | } |
e1467ff2 MM |
969 | else |
970 | templates = scratch_tree_cons (NULL_TREE, tmpl, templates); | |
386b8a85 | 971 | } |
75650646 | 972 | |
e1467ff2 | 973 | if (decl != NULL_TREE) |
386b8a85 | 974 | { |
e1467ff2 | 975 | tree tmpl = most_specialized (templates, decl, targs_in); |
36a117a5 MM |
976 | tree inner_args; |
977 | tree tmpl_args; | |
e1467ff2 MM |
978 | |
979 | if (tmpl == error_mark_node) | |
980 | goto ambiguous; | |
981 | else if (tmpl == NULL_TREE) | |
982 | goto no_match; | |
983 | ||
36a117a5 MM |
984 | inner_args = get_bindings (tmpl, decl, targs_in); |
985 | tmpl_args = DECL_TI_ARGS (DECL_RESULT (tmpl)); | |
986 | if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (tmpl_args)) | |
987 | { | |
988 | *targs_out = copy_node (tmpl_args); | |
989 | SET_TMPL_ARGS_LEVEL (*targs_out, | |
990 | TMPL_ARGS_DEPTH (*targs_out), | |
991 | inner_args); | |
992 | } | |
993 | else | |
994 | *targs_out = inner_args; | |
995 | ||
e1467ff2 MM |
996 | return tmpl; |
997 | } | |
998 | ||
999 | if (templates == NULL_TREE) | |
1000 | { | |
1001 | no_match: | |
386b8a85 | 1002 | if (complain) |
27fafc8d | 1003 | { |
8251199e | 1004 | cp_error_at ("template-id `%D' for `%+D' does not match any template declaration", |
27fafc8d JM |
1005 | template_id, decl); |
1006 | return error_mark_node; | |
1007 | } | |
386b8a85 JM |
1008 | return NULL_TREE; |
1009 | } | |
34016c81 JM |
1010 | else if (TREE_CHAIN (templates) != NULL_TREE |
1011 | || uses_template_parms (TREE_PURPOSE (templates))) | |
386b8a85 | 1012 | { |
e1467ff2 | 1013 | ambiguous: |
386b8a85 JM |
1014 | if (complain) |
1015 | { | |
8251199e | 1016 | cp_error_at ("ambiguous template specialization `%D' for `%+D'", |
27fafc8d | 1017 | template_id, decl); |
e1467ff2 | 1018 | print_candidates (templates); |
27fafc8d | 1019 | return error_mark_node; |
386b8a85 | 1020 | } |
386b8a85 JM |
1021 | return NULL_TREE; |
1022 | } | |
1023 | ||
1024 | /* We have one, and exactly one, match. */ | |
e1467ff2 MM |
1025 | *targs_out = TREE_PURPOSE (templates); |
1026 | return TREE_VALUE (templates); | |
8d08fdba | 1027 | } |
6757edfe | 1028 | |
386b8a85 | 1029 | /* Check to see if the function just declared, as indicated in |
75650646 MM |
1030 | DECLARATOR, and in DECL, is a specialization of a function |
1031 | template. We may also discover that the declaration is an explicit | |
1032 | instantiation at this point. | |
1033 | ||
e1467ff2 MM |
1034 | Returns DECL, or an equivalent declaration that should be used |
1035 | instead. | |
75650646 | 1036 | |
75650646 MM |
1037 | FLAGS is a bitmask consisting of the following flags: |
1038 | ||
75650646 MM |
1039 | 2: The function has a definition. |
1040 | 4: The function is a friend. | |
75650646 MM |
1041 | |
1042 | The TEMPLATE_COUNT is the number of references to qualifying | |
1043 | template classes that appeared in the name of the function. For | |
1044 | example, in | |
1045 | ||
1046 | template <class T> struct S { void f(); }; | |
1047 | void S<int>::f(); | |
1048 | ||
1049 | the TEMPLATE_COUNT would be 1. However, explicitly specialized | |
1050 | classes are not counted in the TEMPLATE_COUNT, so that in | |
1051 | ||
1052 | template <class T> struct S {}; | |
1053 | template <> struct S<int> { void f(); } | |
36a117a5 | 1054 | template <> void S<int>::f(); |
75650646 MM |
1055 | |
1056 | the TEMPLATE_COUNT would be 0. (Note that this declaration is | |
1057 | illegal; there should be no template <>.) | |
1058 | ||
1059 | If the function is a specialization, it is marked as such via | |
1060 | DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO | |
1061 | is set up correctly, and it is added to the list of specializations | |
1062 | for that template. */ | |
386b8a85 | 1063 | |
e1467ff2 | 1064 | tree |
27bb8339 | 1065 | check_explicit_specialization (declarator, decl, template_count, flags) |
386b8a85 JM |
1066 | tree declarator; |
1067 | tree decl; | |
1068 | int template_count; | |
1069 | int flags; | |
1070 | { | |
75650646 MM |
1071 | int have_def = flags & 2; |
1072 | int is_friend = flags & 4; | |
1073 | int specialization = 0; | |
e1467ff2 | 1074 | int explicit_instantiation = 0; |
fd4de5ff | 1075 | int member_specialization = 0; |
75650646 MM |
1076 | |
1077 | tree ctype = DECL_CLASS_CONTEXT (decl); | |
1078 | tree dname = DECL_NAME (decl); | |
386b8a85 | 1079 | |
fd4de5ff | 1080 | if (processing_specialization) |
386b8a85 | 1081 | { |
fd4de5ff | 1082 | /* The last template header was of the form template <>. */ |
75650646 | 1083 | |
fd4de5ff MM |
1084 | if (template_header_count > template_count) |
1085 | { | |
1086 | /* There were more template headers than qualifying template | |
1087 | classes. */ | |
1088 | if (template_header_count - template_count > 1) | |
1089 | /* There shouldn't be that many template parameter | |
75650646 MM |
1090 | lists. There can be at most one parameter list for |
1091 | every qualifying class, plus one for the function | |
1092 | itself. */ | |
fd4de5ff | 1093 | cp_error ("too many template parameter lists in declaration of `%D'", decl); |
75650646 | 1094 | |
fd4de5ff MM |
1095 | SET_DECL_TEMPLATE_SPECIALIZATION (decl); |
1096 | if (ctype) | |
1097 | member_specialization = 1; | |
1098 | else | |
1099 | specialization = 1; | |
75650646 | 1100 | } |
fd4de5ff | 1101 | else if (template_header_count == template_count) |
75650646 | 1102 | { |
fd4de5ff MM |
1103 | /* The counts are equal. So, this might be a |
1104 | specialization, but it is not a specialization of a | |
1105 | member template. It might be something like | |
1106 | ||
1107 | template <class T> struct S { | |
1108 | void f(int i); | |
1109 | }; | |
1110 | template <> | |
1111 | void S<int>::f(int i) {} */ | |
1112 | specialization = 1; | |
1113 | SET_DECL_TEMPLATE_SPECIALIZATION (decl); | |
75650646 | 1114 | } |
fd4de5ff | 1115 | else |
386b8a85 | 1116 | { |
fd4de5ff MM |
1117 | /* This cannot be an explicit specialization. There are not |
1118 | enough headers for all of the qualifying classes. For | |
1119 | example, we might have: | |
1120 | ||
1121 | template <> | |
1122 | void S<int>::T<char>::f(); | |
75650646 | 1123 | |
fd4de5ff MM |
1124 | But, we're missing another template <>. */ |
1125 | cp_error("too few template parameter lists in declaration of `%D'", decl); | |
1126 | return decl; | |
1127 | } | |
1128 | } | |
1129 | else if (processing_explicit_instantiation) | |
1130 | { | |
1131 | if (template_header_count) | |
1132 | cp_error ("template parameter list used in explicit instantiation"); | |
1133 | ||
1134 | if (have_def) | |
1135 | cp_error ("definition provided for explicit instantiation"); | |
75650646 | 1136 | |
fd4de5ff MM |
1137 | explicit_instantiation = 1; |
1138 | } | |
1139 | else if (ctype != NULL_TREE | |
1140 | && !TYPE_BEING_DEFINED (ctype) | |
1141 | && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype) | |
1142 | && !is_friend) | |
1143 | { | |
1144 | /* This case catches outdated code that looks like this: | |
1145 | ||
1146 | template <class T> struct S { void f(); }; | |
1147 | void S<int>::f() {} // Missing template <> | |
1148 | ||
1149 | We disable this check when the type is being defined to | |
1150 | avoid complaining about default compiler-generated | |
1151 | constructors, destructors, and assignment operators. | |
1152 | Since the type is an instantiation, not a specialization, | |
1153 | these are the only functions that can be defined before | |
1154 | the class is complete. */ | |
75650646 | 1155 | |
c6f2ed0d JM |
1156 | /* If they said |
1157 | template <class T> void S<int>::f() {} | |
1158 | that's bogus. */ | |
fd4de5ff MM |
1159 | if (template_header_count) |
1160 | { | |
1161 | cp_error ("template parameters specified in specialization"); | |
1162 | return decl; | |
c6f2ed0d | 1163 | } |
fd4de5ff MM |
1164 | |
1165 | if (pedantic) | |
1166 | cp_pedwarn | |
1167 | ("explicit specialization not preceded by `template <>'"); | |
1168 | specialization = 1; | |
1169 | SET_DECL_TEMPLATE_SPECIALIZATION (decl); | |
1170 | } | |
1171 | else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR) | |
1172 | { | |
1173 | if (is_friend) | |
1174 | /* This could be something like: | |
1175 | ||
1176 | template <class T> void f(T); | |
1177 | class S { friend void f<>(int); } */ | |
1178 | specialization = 1; | |
1179 | else | |
c6f2ed0d | 1180 | { |
fd4de5ff MM |
1181 | /* This case handles bogus declarations like template <> |
1182 | template <class T> void f<int>(); */ | |
75650646 | 1183 | |
fd4de5ff MM |
1184 | cp_error ("template-id `%D' in declaration of primary template", |
1185 | declarator); | |
1186 | return decl; | |
386b8a85 | 1187 | } |
75650646 | 1188 | } |
386b8a85 | 1189 | |
670960ac JM |
1190 | if (specialization || member_specialization) |
1191 | { | |
1192 | tree t = TYPE_ARG_TYPES (TREE_TYPE (decl)); | |
1193 | for (; t; t = TREE_CHAIN (t)) | |
1194 | if (TREE_PURPOSE (t)) | |
1195 | { | |
8251199e JM |
1196 | cp_pedwarn |
1197 | ("default argument specified in explicit specialization"); | |
670960ac JM |
1198 | break; |
1199 | } | |
1200 | } | |
1201 | ||
e1467ff2 | 1202 | if (specialization || member_specialization || explicit_instantiation) |
75650646 | 1203 | { |
36a117a5 | 1204 | tree gen_tmpl; |
75650646 MM |
1205 | tree tmpl = NULL_TREE; |
1206 | tree targs = NULL_TREE; | |
75650646 MM |
1207 | |
1208 | /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */ | |
386b8a85 JM |
1209 | if (TREE_CODE (declarator) != TEMPLATE_ID_EXPR) |
1210 | { | |
1211 | tree fns; | |
1212 | ||
1213 | my_friendly_assert (TREE_CODE (declarator) == IDENTIFIER_NODE, | |
1214 | 0); | |
1215 | if (!ctype) | |
30394414 | 1216 | fns = IDENTIFIER_NAMESPACE_VALUE (dname); |
386b8a85 JM |
1217 | else |
1218 | fns = dname; | |
1219 | ||
75650646 MM |
1220 | declarator = |
1221 | lookup_template_function (fns, NULL_TREE); | |
386b8a85 JM |
1222 | } |
1223 | ||
f2e48b67 BK |
1224 | if (declarator == error_mark_node) |
1225 | return error_mark_node; | |
1226 | ||
1227 | if (TREE_CODE (TREE_OPERAND (declarator, 0)) == LOOKUP_EXPR) | |
386b8a85 JM |
1228 | { |
1229 | /* A friend declaration. We can't do much, because we don't | |
1230 | know what this resolves to, yet. */ | |
1231 | my_friendly_assert (is_friend != 0, 0); | |
e1467ff2 | 1232 | my_friendly_assert (!explicit_instantiation, 0); |
386b8a85 | 1233 | SET_DECL_IMPLICIT_INSTANTIATION (decl); |
e1467ff2 | 1234 | return decl; |
386b8a85 JM |
1235 | } |
1236 | ||
75650646 MM |
1237 | if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype)) |
1238 | { | |
8ec2ac34 | 1239 | if (!explicit_instantiation) |
6c30752f MM |
1240 | /* A specialization in class scope. This is illegal, |
1241 | but the error will already have been flagged by | |
1242 | check_specialization_scope. */ | |
1243 | return error_mark_node; | |
8ec2ac34 | 1244 | else |
b370501f KG |
1245 | { |
1246 | /* It's not legal to write an explicit instantiation in | |
1247 | class scope, e.g.: | |
8ec2ac34 | 1248 | |
b370501f | 1249 | class C { template void f(); } |
8ec2ac34 | 1250 | |
b370501f KG |
1251 | This case is caught by the parser. However, on |
1252 | something like: | |
8ec2ac34 | 1253 | |
b370501f | 1254 | template class C { void f(); }; |
8ec2ac34 | 1255 | |
b370501f KG |
1256 | (which is illegal) we can get here. The error will be |
1257 | issued later. */ | |
1258 | ; | |
1259 | } | |
8ec2ac34 | 1260 | |
e1467ff2 | 1261 | return decl; |
75650646 MM |
1262 | } |
1263 | else if (ctype != NULL_TREE | |
1264 | && (TREE_CODE (TREE_OPERAND (declarator, 0)) == | |
1265 | IDENTIFIER_NODE)) | |
386b8a85 | 1266 | { |
75650646 MM |
1267 | /* Find the list of functions in ctype that have the same |
1268 | name as the declared function. */ | |
1269 | tree name = TREE_OPERAND (declarator, 0); | |
386b8a85 | 1270 | tree fns; |
75650646 MM |
1271 | |
1272 | if (name == constructor_name (ctype) | |
1273 | || name == constructor_name_full (ctype)) | |
386b8a85 | 1274 | { |
75650646 MM |
1275 | int is_constructor = DECL_CONSTRUCTOR_P (decl); |
1276 | ||
1277 | if (is_constructor ? !TYPE_HAS_CONSTRUCTOR (ctype) | |
1278 | : !TYPE_HAS_DESTRUCTOR (ctype)) | |
1279 | { | |
1280 | /* From [temp.expl.spec]: | |
e1467ff2 | 1281 | |
75650646 MM |
1282 | If such an explicit specialization for the member |
1283 | of a class template names an implicitly-declared | |
1284 | special member function (clause _special_), the | |
e1467ff2 MM |
1285 | program is ill-formed. |
1286 | ||
1287 | Similar language is found in [temp.explicit]. */ | |
8251199e | 1288 | cp_error ("specialization of implicitly-declared special member function"); |
75650646 | 1289 | |
e1467ff2 | 1290 | return decl; |
75650646 | 1291 | } |
386b8a85 | 1292 | |
42da2fd8 | 1293 | name = is_constructor ? ctor_identifier : dtor_identifier; |
75650646 | 1294 | } |
42da2fd8 MM |
1295 | |
1296 | fns = lookup_fnfields (TYPE_BINFO (ctype), name, 1); | |
386b8a85 JM |
1297 | |
1298 | if (fns == NULL_TREE) | |
1299 | { | |
8251199e | 1300 | cp_error ("no member function `%s' declared in `%T'", |
75650646 | 1301 | IDENTIFIER_POINTER (name), |
386b8a85 | 1302 | ctype); |
e1467ff2 | 1303 | return decl; |
386b8a85 JM |
1304 | } |
1305 | else | |
1306 | TREE_OPERAND (declarator, 0) = fns; | |
1307 | } | |
75650646 | 1308 | |
e1467ff2 MM |
1309 | /* Figure out what exactly is being specialized at this point. |
1310 | Note that for an explicit instantiation, even one for a | |
38e01259 | 1311 | member function, we cannot tell apriori whether the |
e1467ff2 | 1312 | instantiation is for a member template, or just a member |
36a117a5 MM |
1313 | function of a template class. Even if a member template is |
1314 | being instantiated, the member template arguments may be | |
1315 | elided if they can be deduced from the rest of the | |
1316 | declaration. */ | |
e1467ff2 MM |
1317 | tmpl = determine_specialization (declarator, decl, |
1318 | &targs, | |
75650646 MM |
1319 | member_specialization, |
1320 | 1); | |
386b8a85 | 1321 | |
82a7448c | 1322 | if (tmpl && tmpl != error_mark_node) |
386b8a85 | 1323 | { |
36a117a5 MM |
1324 | gen_tmpl = most_general_template (tmpl); |
1325 | ||
e1467ff2 MM |
1326 | if (explicit_instantiation) |
1327 | { | |
03d0f4af MM |
1328 | /* We don't set DECL_EXPLICIT_INSTANTIATION here; that |
1329 | is done by do_decl_instantiation later. */ | |
36a117a5 | 1330 | decl = instantiate_template (tmpl, innermost_args (targs)); |
e1467ff2 MM |
1331 | return decl; |
1332 | } | |
36a117a5 MM |
1333 | |
1334 | /* If we though that the DECL was a member function, but it | |
1335 | turns out to be specializing a static member function, | |
1336 | make DECL a static member function as well. */ | |
1337 | if (DECL_STATIC_FUNCTION_P (tmpl) | |
1338 | && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)) | |
366c0f1e MM |
1339 | { |
1340 | revert_static_member_fn (&decl, 0, 0); | |
1341 | last_function_parms = TREE_CHAIN (last_function_parms); | |
1342 | } | |
e1467ff2 | 1343 | |
36a117a5 MM |
1344 | /* Set up the DECL_TEMPLATE_INFO for DECL. */ |
1345 | DECL_TEMPLATE_INFO (decl) | |
1346 | = perm_tree_cons (tmpl, targs, NULL_TREE); | |
1347 | ||
e1467ff2 MM |
1348 | /* Mangle the function name appropriately. Note that we do |
1349 | not mangle specializations of non-template member | |
1350 | functions of template classes, e.g. with | |
36a117a5 | 1351 | |
e1467ff2 | 1352 | template <class T> struct S { void f(); } |
36a117a5 | 1353 | |
e1467ff2 | 1354 | and given the specialization |
36a117a5 | 1355 | |
e1467ff2 | 1356 | template <> void S<int>::f() {} |
36a117a5 | 1357 | |
e1467ff2 MM |
1358 | we do not mangle S<int>::f() here. That's because it's |
1359 | just an ordinary member function and doesn't need special | |
36a117a5 MM |
1360 | treatment. We do this here so that the ordinary, |
1361 | non-template, name-mangling algorith will not be used | |
1362 | later. */ | |
e1467ff2 | 1363 | if ((is_member_template (tmpl) || ctype == NULL_TREE) |
75650646 | 1364 | && name_mangling_version >= 1) |
36a117a5 | 1365 | set_mangled_name_for_template_decl (decl); |
386b8a85 JM |
1366 | |
1367 | if (is_friend && !have_def) | |
36a117a5 MM |
1368 | /* This is not really a declaration of a specialization. |
1369 | It's just the name of an instantiation. But, it's not | |
1370 | a request for an instantiation, either. */ | |
386b8a85 | 1371 | SET_DECL_IMPLICIT_INSTANTIATION (decl); |
75650646 | 1372 | |
36a117a5 MM |
1373 | /* Register this specialization so that we can find it |
1374 | again. */ | |
1375 | decl = register_specialization (decl, gen_tmpl, targs); | |
75650646 | 1376 | |
e1467ff2 | 1377 | return decl; |
386b8a85 JM |
1378 | } |
1379 | } | |
75650646 | 1380 | |
e1467ff2 | 1381 | return decl; |
386b8a85 | 1382 | } |
75650646 | 1383 | |
39c01e4c MM |
1384 | /* TYPE is being declared. Verify that the use of template headers |
1385 | and such is reasonable. Issue error messages if not. */ | |
1386 | ||
1387 | void | |
1388 | maybe_check_template_type (type) | |
1389 | tree type; | |
1390 | { | |
1391 | if (template_header_count) | |
1392 | { | |
1393 | /* We are in the scope of some `template <...>' header. */ | |
1394 | ||
1395 | int context_depth | |
1396 | = template_class_depth_real (TYPE_CONTEXT (type), | |
1397 | /*count_specializations=*/1); | |
1398 | ||
1399 | if (template_header_count <= context_depth) | |
1400 | /* This is OK; the template headers are for the context. We | |
1401 | are actually too lenient here; like | |
1402 | check_explicit_specialization we should consider the number | |
1403 | of template types included in the actual declaration. For | |
1404 | example, | |
1405 | ||
1406 | template <class T> struct S { | |
1407 | template <class U> template <class V> | |
1408 | struct I {}; | |
1409 | }; | |
1410 | ||
1411 | is illegal, but: | |
1412 | ||
1413 | template <class T> struct S { | |
1414 | template <class U> struct I; | |
1415 | }; | |
1416 | ||
1417 | template <class T> template <class U. | |
1418 | struct S<T>::I {}; | |
1419 | ||
1420 | is not. */ | |
1421 | ; | |
1422 | else if (template_header_count > context_depth + 1) | |
1423 | /* There are two many template parameter lists. */ | |
8251199e | 1424 | cp_error ("too many template parameter lists in declaration of `%T'", type); |
39c01e4c MM |
1425 | } |
1426 | } | |
1427 | ||
75650646 MM |
1428 | /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template |
1429 | parameters. These are represented in the same format used for | |
1430 | DECL_TEMPLATE_PARMS. */ | |
1431 | ||
1432 | int comp_template_parms (parms1, parms2) | |
1433 | tree parms1; | |
1434 | tree parms2; | |
1435 | { | |
1436 | tree p1; | |
1437 | tree p2; | |
1438 | ||
1439 | if (parms1 == parms2) | |
1440 | return 1; | |
1441 | ||
1442 | for (p1 = parms1, p2 = parms2; | |
1443 | p1 != NULL_TREE && p2 != NULL_TREE; | |
1444 | p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2)) | |
1445 | { | |
1446 | tree t1 = TREE_VALUE (p1); | |
1447 | tree t2 = TREE_VALUE (p2); | |
1448 | int i; | |
1449 | ||
1450 | my_friendly_assert (TREE_CODE (t1) == TREE_VEC, 0); | |
1451 | my_friendly_assert (TREE_CODE (t2) == TREE_VEC, 0); | |
1452 | ||
1453 | if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2)) | |
1454 | return 0; | |
1455 | ||
1456 | for (i = 0; i < TREE_VEC_LENGTH (t2); ++i) | |
1457 | { | |
1458 | tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i)); | |
1459 | tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i)); | |
1460 | ||
1461 | if (TREE_CODE (parm1) != TREE_CODE (parm2)) | |
1462 | return 0; | |
1463 | ||
1464 | if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM) | |
1465 | continue; | |
3bfdc719 | 1466 | else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2))) |
75650646 MM |
1467 | return 0; |
1468 | } | |
1469 | } | |
1470 | ||
1471 | if ((p1 != NULL_TREE) != (p2 != NULL_TREE)) | |
1472 | /* One set of parameters has more parameters lists than the | |
1473 | other. */ | |
1474 | return 0; | |
1475 | ||
1476 | return 1; | |
1477 | } | |
1478 | ||
22a7be53 BK |
1479 | |
1480 | /* Returns 1 iff old_id is a template parameter. OLD_DECL is the decl | |
1481 | from IDENTIFIER_LOCAL_VALUE (new identifier). */ | |
1482 | ||
1483 | int decl_template_parm_p (old_decl) | |
1484 | tree old_decl; | |
1485 | { | |
60917625 BK |
1486 | /* For template template parms. */ |
1487 | if (TREE_CODE (old_decl) == TEMPLATE_DECL | |
1488 | && TREE_TYPE (old_decl) | |
1489 | && TREE_CODE (TREE_TYPE (old_decl)) == TEMPLATE_TEMPLATE_PARM) | |
22a7be53 | 1490 | return 1; |
60917625 BK |
1491 | |
1492 | /* For template type parms. */ | |
1493 | if (TREE_CODE (old_decl) == TYPE_DECL | |
1494 | && TREE_TYPE (old_decl) | |
1495 | && TREE_CODE (TREE_TYPE (old_decl)) == TEMPLATE_TYPE_PARM) | |
1496 | return 1; | |
1497 | ||
1498 | /* For template non-type parms. */ | |
1499 | if (TREE_CODE (old_decl) == CONST_DECL | |
1500 | && DECL_INITIAL (old_decl) | |
1501 | && TREE_CODE (DECL_INITIAL (old_decl)) == TEMPLATE_PARM_INDEX) | |
1502 | return 1; | |
1503 | ||
1504 | return 0; | |
22a7be53 BK |
1505 | } |
1506 | ||
1507 | ||
1508 | /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL, | |
f84b4be9 JM |
1509 | ORIG_LEVEL, DECL, and TYPE. */ |
1510 | ||
1511 | static tree | |
1512 | build_template_parm_index (index, level, orig_level, decl, type) | |
1513 | int index; | |
1514 | int level; | |
1515 | int orig_level; | |
1516 | tree decl; | |
1517 | tree type; | |
1518 | { | |
1519 | tree t = make_node (TEMPLATE_PARM_INDEX); | |
1520 | TEMPLATE_PARM_IDX (t) = index; | |
1521 | TEMPLATE_PARM_LEVEL (t) = level; | |
1522 | TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level; | |
1523 | TEMPLATE_PARM_DECL (t) = decl; | |
1524 | TREE_TYPE (t) = type; | |
1525 | ||
1526 | return t; | |
1527 | } | |
1528 | ||
f84b4be9 | 1529 | /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose |
93cdc044 | 1530 | TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a |
f84b4be9 JM |
1531 | TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a |
1532 | new one is created. */ | |
1533 | ||
1534 | static tree | |
93cdc044 | 1535 | reduce_template_parm_level (index, type, levels) |
f84b4be9 JM |
1536 | tree index; |
1537 | tree type; | |
93cdc044 | 1538 | int levels; |
f84b4be9 JM |
1539 | { |
1540 | if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE | |
1541 | || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index)) | |
93cdc044 | 1542 | != TEMPLATE_PARM_LEVEL (index) - levels)) |
f84b4be9 JM |
1543 | { |
1544 | tree decl | |
1545 | = build_decl (TREE_CODE (TEMPLATE_PARM_DECL (index)), | |
1546 | DECL_NAME (TEMPLATE_PARM_DECL (index)), | |
1547 | type); | |
1548 | tree t | |
1549 | = build_template_parm_index (TEMPLATE_PARM_IDX (index), | |
93cdc044 | 1550 | TEMPLATE_PARM_LEVEL (index) - levels, |
f84b4be9 JM |
1551 | TEMPLATE_PARM_ORIG_LEVEL (index), |
1552 | decl, type); | |
1553 | TEMPLATE_PARM_DESCENDANTS (index) = t; | |
cae40af6 JM |
1554 | |
1555 | /* Template template parameters need this. */ | |
1556 | DECL_TEMPLATE_PARMS (decl) | |
1557 | = DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index)); | |
f84b4be9 JM |
1558 | } |
1559 | ||
1560 | return TEMPLATE_PARM_DESCENDANTS (index); | |
1561 | } | |
1562 | ||
8d08fdba | 1563 | /* Process information from new template parameter NEXT and append it to the |
5566b478 | 1564 | LIST being built. */ |
e92cc029 | 1565 | |
8d08fdba MS |
1566 | tree |
1567 | process_template_parm (list, next) | |
1568 | tree list, next; | |
1569 | { | |
1570 | tree parm; | |
1571 | tree decl = 0; | |
a292b002 | 1572 | tree defval; |
5566b478 | 1573 | int is_type, idx; |
f84b4be9 | 1574 | |
8d08fdba MS |
1575 | parm = next; |
1576 | my_friendly_assert (TREE_CODE (parm) == TREE_LIST, 259); | |
a292b002 MS |
1577 | defval = TREE_PURPOSE (parm); |
1578 | parm = TREE_VALUE (parm); | |
1579 | is_type = TREE_PURPOSE (parm) == class_type_node; | |
5566b478 MS |
1580 | |
1581 | if (list) | |
1582 | { | |
1583 | tree p = TREE_VALUE (tree_last (list)); | |
1584 | ||
1585 | if (TREE_CODE (p) == TYPE_DECL) | |
1586 | idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p)); | |
73b0fce8 KL |
1587 | else if (TREE_CODE (p) == TEMPLATE_DECL) |
1588 | idx = TEMPLATE_TYPE_IDX (TREE_TYPE (DECL_TEMPLATE_RESULT (p))); | |
5566b478 | 1589 | else |
f84b4be9 | 1590 | idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p)); |
5566b478 MS |
1591 | ++idx; |
1592 | } | |
1593 | else | |
1594 | idx = 0; | |
1595 | ||
8d08fdba MS |
1596 | if (!is_type) |
1597 | { | |
a292b002 | 1598 | my_friendly_assert (TREE_CODE (TREE_PURPOSE (parm)) == TREE_LIST, 260); |
8d08fdba | 1599 | /* is a const-param */ |
a292b002 | 1600 | parm = grokdeclarator (TREE_VALUE (parm), TREE_PURPOSE (parm), |
c11b6f21 | 1601 | PARM, 0, NULL_TREE); |
8d08fdba MS |
1602 | /* A template parameter is not modifiable. */ |
1603 | TREE_READONLY (parm) = 1; | |
c91a56d2 | 1604 | if (IS_AGGR_TYPE (TREE_TYPE (parm)) |
410c2fba KL |
1605 | && TREE_CODE (TREE_TYPE (parm)) != TEMPLATE_TYPE_PARM |
1606 | && TREE_CODE (TREE_TYPE (parm)) != TYPENAME_TYPE) | |
8d08fdba | 1607 | { |
8251199e | 1608 | cp_error ("`%#T' is not a valid type for a template constant parameter", |
c91a56d2 MS |
1609 | TREE_TYPE (parm)); |
1610 | if (DECL_NAME (parm) == NULL_TREE) | |
8251199e | 1611 | error (" a template type parameter must begin with `class' or `typename'"); |
8d08fdba MS |
1612 | TREE_TYPE (parm) = void_type_node; |
1613 | } | |
37c46b43 MS |
1614 | else if (pedantic |
1615 | && (TREE_CODE (TREE_TYPE (parm)) == REAL_TYPE | |
1616 | || TREE_CODE (TREE_TYPE (parm)) == COMPLEX_TYPE)) | |
8251199e | 1617 | cp_pedwarn ("`%T' is not a valid type for a template constant parameter", |
eb66be0e | 1618 | TREE_TYPE (parm)); |
8d08fdba MS |
1619 | if (TREE_PERMANENT (parm) == 0) |
1620 | { | |
1621 | parm = copy_node (parm); | |
1622 | TREE_PERMANENT (parm) = 1; | |
1623 | } | |
8d08fdba | 1624 | decl = build_decl (CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm)); |
f84b4be9 JM |
1625 | DECL_INITIAL (parm) = DECL_INITIAL (decl) |
1626 | = build_template_parm_index (idx, processing_template_decl, | |
1627 | processing_template_decl, | |
1628 | decl, TREE_TYPE (parm)); | |
8d08fdba MS |
1629 | } |
1630 | else | |
1631 | { | |
73b0fce8 KL |
1632 | tree t; |
1633 | parm = TREE_VALUE (parm); | |
1634 | ||
1635 | if (parm && TREE_CODE (parm) == TEMPLATE_DECL) | |
1636 | { | |
1637 | t = make_lang_type (TEMPLATE_TEMPLATE_PARM); | |
1638 | /* This is for distinguishing between real templates and template | |
1639 | template parameters */ | |
1640 | TREE_TYPE (parm) = t; | |
1641 | TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t; | |
1642 | decl = parm; | |
1643 | } | |
1644 | else | |
1645 | { | |
1646 | t = make_lang_type (TEMPLATE_TYPE_PARM); | |
1647 | /* parm is either IDENTIFIER_NODE or NULL_TREE */ | |
1648 | decl = build_decl (TYPE_DECL, parm, t); | |
1649 | } | |
1650 | ||
5566b478 | 1651 | CLASSTYPE_GOT_SEMICOLON (t) = 1; |
d2e5ee5c MS |
1652 | TYPE_NAME (t) = decl; |
1653 | TYPE_STUB_DECL (t) = decl; | |
a292b002 | 1654 | parm = decl; |
f84b4be9 JM |
1655 | TEMPLATE_TYPE_PARM_INDEX (t) |
1656 | = build_template_parm_index (idx, processing_template_decl, | |
1657 | processing_template_decl, | |
1658 | decl, TREE_TYPE (parm)); | |
8d08fdba | 1659 | } |
8ccc31eb | 1660 | SET_DECL_ARTIFICIAL (decl); |
8d08fdba | 1661 | pushdecl (decl); |
a292b002 | 1662 | parm = build_tree_list (defval, parm); |
8d08fdba MS |
1663 | return chainon (list, parm); |
1664 | } | |
1665 | ||
1666 | /* The end of a template parameter list has been reached. Process the | |
1667 | tree list into a parameter vector, converting each parameter into a more | |
1668 | useful form. Type parameters are saved as IDENTIFIER_NODEs, and others | |
1669 | as PARM_DECLs. */ | |
1670 | ||
1671 | tree | |
1672 | end_template_parm_list (parms) | |
1673 | tree parms; | |
1674 | { | |
5566b478 | 1675 | int nparms; |
8d08fdba | 1676 | tree parm; |
5566b478 MS |
1677 | tree saved_parmlist = make_tree_vec (list_length (parms)); |
1678 | ||
5566b478 MS |
1679 | current_template_parms |
1680 | = tree_cons (build_int_2 (0, processing_template_decl), | |
1681 | saved_parmlist, current_template_parms); | |
8d08fdba MS |
1682 | |
1683 | for (parm = parms, nparms = 0; parm; parm = TREE_CHAIN (parm), nparms++) | |
5566b478 | 1684 | TREE_VEC_ELT (saved_parmlist, nparms) = parm; |
a292b002 | 1685 | |
67ffc812 MM |
1686 | --processing_template_parmlist; |
1687 | ||
8d08fdba MS |
1688 | return saved_parmlist; |
1689 | } | |
1690 | ||
5566b478 MS |
1691 | /* end_template_decl is called after a template declaration is seen. */ |
1692 | ||
8d08fdba | 1693 | void |
5566b478 | 1694 | end_template_decl () |
8d08fdba | 1695 | { |
386b8a85 JM |
1696 | reset_specialization (); |
1697 | ||
5156628f | 1698 | if (! processing_template_decl) |
73aad9b9 JM |
1699 | return; |
1700 | ||
5566b478 MS |
1701 | /* This matches the pushlevel in begin_template_parm_list. */ |
1702 | poplevel (0, 0, 0); | |
8d08fdba | 1703 | |
5566b478 MS |
1704 | --processing_template_decl; |
1705 | current_template_parms = TREE_CHAIN (current_template_parms); | |
1706 | (void) get_pending_sizes (); /* Why? */ | |
1707 | } | |
8d08fdba | 1708 | |
36a117a5 MM |
1709 | /* Given a template argument vector containing the template PARMS. |
1710 | The innermost PARMS are given first. */ | |
9a3b49ac MS |
1711 | |
1712 | tree | |
1713 | current_template_args () | |
5566b478 | 1714 | { |
36a117a5 | 1715 | tree header; |
b370501f | 1716 | tree args = NULL_TREE; |
36a117a5 | 1717 | int length = TMPL_PARMS_DEPTH (current_template_parms); |
98c1c668 JM |
1718 | int l = length; |
1719 | ||
36a117a5 MM |
1720 | /* If there is only one level of template parameters, we do not |
1721 | create a TREE_VEC of TREE_VECs. Instead, we return a single | |
1722 | TREE_VEC containing the arguments. */ | |
1723 | if (length > 1) | |
1724 | args = make_tree_vec (length); | |
1725 | ||
1726 | for (header = current_template_parms; header; header = TREE_CHAIN (header)) | |
8d08fdba | 1727 | { |
5566b478 | 1728 | tree a = copy_node (TREE_VALUE (header)); |
36a117a5 MM |
1729 | int i; |
1730 | ||
5566b478 | 1731 | TREE_TYPE (a) = NULL_TREE; |
36a117a5 | 1732 | for (i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i) |
5566b478 | 1733 | { |
98c1c668 JM |
1734 | tree t = TREE_VEC_ELT (a, i); |
1735 | ||
36a117a5 | 1736 | /* T will be a list if we are called from within a |
98c1c668 JM |
1737 | begin/end_template_parm_list pair, but a vector directly |
1738 | if within a begin/end_member_template_processing pair. */ | |
1739 | if (TREE_CODE (t) == TREE_LIST) | |
1740 | { | |
1741 | t = TREE_VALUE (t); | |
1742 | ||
73b0fce8 KL |
1743 | if (TREE_CODE (t) == TYPE_DECL |
1744 | || TREE_CODE (t) == TEMPLATE_DECL) | |
98c1c668 JM |
1745 | t = TREE_TYPE (t); |
1746 | else | |
1747 | t = DECL_INITIAL (t); | |
36a117a5 | 1748 | TREE_VEC_ELT (a, i) = t; |
98c1c668 | 1749 | } |
5566b478 | 1750 | } |
36a117a5 MM |
1751 | |
1752 | if (length > 1) | |
1753 | TREE_VEC_ELT (args, --l) = a; | |
1754 | else | |
1755 | args = a; | |
8d08fdba MS |
1756 | } |
1757 | ||
9a3b49ac MS |
1758 | return args; |
1759 | } | |
75650646 | 1760 | |
e1467ff2 MM |
1761 | /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated |
1762 | template PARMS. Used by push_template_decl below. */ | |
1763 | ||
75650646 MM |
1764 | static tree |
1765 | build_template_decl (decl, parms) | |
1766 | tree decl; | |
1767 | tree parms; | |
1768 | { | |
1769 | tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE); | |
1770 | DECL_TEMPLATE_PARMS (tmpl) = parms; | |
1771 | DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl); | |
1772 | if (DECL_LANG_SPECIFIC (decl)) | |
1773 | { | |
1774 | DECL_CLASS_CONTEXT (tmpl) = DECL_CLASS_CONTEXT (decl); | |
1775 | DECL_STATIC_FUNCTION_P (tmpl) = | |
1776 | DECL_STATIC_FUNCTION_P (decl); | |
1777 | } | |
1778 | ||
1779 | return tmpl; | |
1780 | } | |
1781 | ||
050367a3 MM |
1782 | struct template_parm_data |
1783 | { | |
6c30752f MM |
1784 | /* The level of the template parameters we are currently |
1785 | processing. */ | |
050367a3 | 1786 | int level; |
6c30752f MM |
1787 | |
1788 | /* The index of the specialization argument we are currently | |
1789 | processing. */ | |
1790 | int current_arg; | |
1791 | ||
1792 | /* An array whose size is the number of template parameters. The | |
1793 | elements are non-zero if the parameter has been used in any one | |
1794 | of the arguments processed so far. */ | |
050367a3 | 1795 | int* parms; |
6c30752f MM |
1796 | |
1797 | /* An array whose size is the number of template arguments. The | |
1798 | elements are non-zero if the argument makes use of template | |
1799 | parameters of this level. */ | |
1800 | int* arg_uses_template_parms; | |
050367a3 MM |
1801 | }; |
1802 | ||
1803 | /* Subroutine of push_template_decl used to see if each template | |
1804 | parameter in a partial specialization is used in the explicit | |
1805 | argument list. If T is of the LEVEL given in DATA (which is | |
1806 | treated as a template_parm_data*), then DATA->PARMS is marked | |
1807 | appropriately. */ | |
1808 | ||
1809 | static int | |
1810 | mark_template_parm (t, data) | |
1811 | tree t; | |
1812 | void* data; | |
1813 | { | |
1814 | int level; | |
1815 | int idx; | |
1816 | struct template_parm_data* tpd = (struct template_parm_data*) data; | |
1817 | ||
1818 | if (TREE_CODE (t) == TEMPLATE_PARM_INDEX) | |
1819 | { | |
1820 | level = TEMPLATE_PARM_LEVEL (t); | |
1821 | idx = TEMPLATE_PARM_IDX (t); | |
1822 | } | |
1823 | else | |
1824 | { | |
1825 | level = TEMPLATE_TYPE_LEVEL (t); | |
1826 | idx = TEMPLATE_TYPE_IDX (t); | |
1827 | } | |
1828 | ||
1829 | if (level == tpd->level) | |
6c30752f MM |
1830 | { |
1831 | tpd->parms[idx] = 1; | |
1832 | tpd->arg_uses_template_parms[tpd->current_arg] = 1; | |
1833 | } | |
050367a3 MM |
1834 | |
1835 | /* Return zero so that for_each_template_parm will continue the | |
1836 | traversal of the tree; we want to mark *every* template parm. */ | |
1837 | return 0; | |
1838 | } | |
1839 | ||
6c30752f MM |
1840 | /* Process the partial specialization DECL. */ |
1841 | ||
1842 | tree | |
1843 | process_partial_specialization (decl) | |
1844 | tree decl; | |
1845 | { | |
1846 | tree type = TREE_TYPE (decl); | |
1847 | tree maintmpl = CLASSTYPE_TI_TEMPLATE (type); | |
1848 | tree specargs = CLASSTYPE_TI_ARGS (type); | |
1849 | tree inner_args = innermost_args (specargs); | |
1850 | tree inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms); | |
1851 | tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl); | |
1852 | int nargs = TREE_VEC_LENGTH (inner_args); | |
1853 | int ntparms = TREE_VEC_LENGTH (inner_parms); | |
1854 | int i; | |
1855 | int did_error_intro = 0; | |
1856 | int issued_default_arg_message = 0; | |
1857 | struct template_parm_data tpd; | |
1858 | struct template_parm_data tpd2; | |
1859 | ||
1860 | /* [temp.class.spec] | |
1861 | ||
1862 | The template parameter list of a specialization shall not | |
1863 | contain default template argument values. */ | |
1864 | for (i = 0; i < ntparms; ++i) | |
1865 | { | |
1866 | if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i))) | |
1867 | { | |
1868 | if (!issued_default_arg_message) | |
1869 | { | |
8251199e | 1870 | cp_error ("default argument in partial specialization `%T'", |
6c30752f MM |
1871 | type); |
1872 | issued_default_arg_message = 1; | |
1873 | } | |
1874 | TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE; | |
1875 | } | |
1876 | } | |
1877 | ||
1878 | /* We check that each of the template parameters given in the | |
1879 | partial specialization is used in the argument list to the | |
1880 | specialization. For example: | |
1881 | ||
1882 | template <class T> struct S; | |
1883 | template <class T> struct S<T*>; | |
1884 | ||
1885 | The second declaration is OK because `T*' uses the template | |
1886 | parameter T, whereas | |
1887 | ||
1888 | template <class T> struct S<int>; | |
1889 | ||
1890 | is no good. Even trickier is: | |
1891 | ||
1892 | template <class T> | |
1893 | struct S1 | |
1894 | { | |
1895 | template <class U> | |
1896 | struct S2; | |
1897 | template <class U> | |
1898 | struct S2<T>; | |
1899 | }; | |
1900 | ||
1901 | The S2<T> declaration is actually illegal; it is a | |
1902 | full-specialization. Of course, | |
1903 | ||
1904 | template <class U> | |
1905 | struct S2<T (*)(U)>; | |
1906 | ||
1907 | or some such would have been OK. */ | |
1908 | tpd.level = TMPL_PARMS_DEPTH (current_template_parms); | |
1909 | tpd.parms = alloca (sizeof (int) * ntparms); | |
1ddb2906 | 1910 | bzero ((PTR) tpd.parms, sizeof (int) * ntparms); |
6c30752f MM |
1911 | |
1912 | tpd.arg_uses_template_parms = alloca (sizeof (int) * nargs); | |
1ddb2906 | 1913 | bzero ((PTR) tpd.arg_uses_template_parms, sizeof (int) * nargs); |
6c30752f MM |
1914 | for (i = 0; i < nargs; ++i) |
1915 | { | |
1916 | tpd.current_arg = i; | |
1917 | for_each_template_parm (TREE_VEC_ELT (inner_args, i), | |
1918 | &mark_template_parm, | |
1919 | &tpd); | |
1920 | } | |
1921 | for (i = 0; i < ntparms; ++i) | |
1922 | if (tpd.parms[i] == 0) | |
1923 | { | |
1924 | /* One of the template parms was not used in the | |
1925 | specialization. */ | |
1926 | if (!did_error_intro) | |
1927 | { | |
8251199e | 1928 | cp_error ("template parameters not used in partial specialization:"); |
6c30752f MM |
1929 | did_error_intro = 1; |
1930 | } | |
1931 | ||
8251199e | 1932 | cp_error (" `%D'", |
6c30752f MM |
1933 | TREE_VALUE (TREE_VEC_ELT (inner_parms, i))); |
1934 | } | |
1935 | ||
1936 | /* [temp.class.spec] | |
1937 | ||
1938 | The argument list of the specialization shall not be identical to | |
1939 | the implicit argument list of the primary template. */ | |
1940 | if (comp_template_args (inner_args, | |
1941 | innermost_args (CLASSTYPE_TI_ARGS (TREE_TYPE | |
1942 | (maintmpl))))) | |
8251199e | 1943 | cp_error ("partial specialization `%T' does not specialize any template arguments", type); |
6c30752f MM |
1944 | |
1945 | /* [temp.class.spec] | |
1946 | ||
1947 | A partially specialized non-type argument expression shall not | |
1948 | involve template parameters of the partial specialization except | |
1949 | when the argument expression is a simple identifier. | |
1950 | ||
1951 | The type of a template parameter corresponding to a specialized | |
1952 | non-type argument shall not be dependent on a parameter of the | |
1953 | specialization. */ | |
1954 | my_friendly_assert (nargs == DECL_NTPARMS (maintmpl), 0); | |
1955 | tpd2.parms = 0; | |
1956 | for (i = 0; i < nargs; ++i) | |
1957 | { | |
1958 | tree arg = TREE_VEC_ELT (inner_args, i); | |
1959 | if (/* These first two lines are the `non-type' bit. */ | |
1960 | TREE_CODE_CLASS (TREE_CODE (arg)) != 't' | |
1961 | && TREE_CODE (arg) != TEMPLATE_DECL | |
1962 | /* This next line is the `argument expression is not just a | |
1963 | simple identifier' condition and also the `specialized | |
1964 | non-type argument' bit. */ | |
1965 | && TREE_CODE (arg) != TEMPLATE_PARM_INDEX) | |
1966 | { | |
1967 | if (tpd.arg_uses_template_parms[i]) | |
8251199e | 1968 | cp_error ("template argument `%E' involves template parameter(s)", arg); |
6c30752f MM |
1969 | else |
1970 | { | |
1971 | /* Look at the corresponding template parameter, | |
1972 | marking which template parameters its type depends | |
1973 | upon. */ | |
1974 | tree type = | |
1975 | TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (main_inner_parms, | |
1976 | i))); | |
1977 | ||
1978 | if (!tpd2.parms) | |
1979 | { | |
1980 | /* We haven't yet initialized TPD2. Do so now. */ | |
1981 | tpd2.arg_uses_template_parms | |
1982 | = (int*) alloca (sizeof (int) * nargs); | |
76a83782 RH |
1983 | /* The number of paramters here is the number in the |
1984 | main template, which, as checked in the assertion | |
1985 | above, is NARGS. */ | |
6c30752f MM |
1986 | tpd2.parms = (int*) alloca (sizeof (int) * nargs); |
1987 | tpd2.level = | |
1988 | TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl)); | |
1989 | } | |
1990 | ||
1991 | /* Mark the template paramters. But this time, we're | |
1992 | looking for the template parameters of the main | |
1993 | template, not in the specialization. */ | |
1994 | tpd2.current_arg = i; | |
1995 | tpd2.arg_uses_template_parms[i] = 0; | |
1ddb2906 | 1996 | bzero ((PTR) tpd2.parms, sizeof (int) * nargs); |
6c30752f MM |
1997 | for_each_template_parm (type, |
1998 | &mark_template_parm, | |
1999 | &tpd2); | |
2000 | ||
2001 | if (tpd2.arg_uses_template_parms [i]) | |
2002 | { | |
2003 | /* The type depended on some template parameters. | |
2004 | If they are fully specialized in the | |
2005 | specialization, that's OK. */ | |
2006 | int j; | |
2007 | for (j = 0; j < nargs; ++j) | |
2008 | if (tpd2.parms[j] != 0 | |
2009 | && tpd.arg_uses_template_parms [j]) | |
2010 | { | |
8251199e | 2011 | cp_error ("type `%T' of template argument `%E' depends on template paramter(s)", |
6c30752f MM |
2012 | type, |
2013 | arg); | |
2014 | break; | |
2015 | } | |
2016 | } | |
2017 | } | |
2018 | } | |
2019 | } | |
2020 | ||
2021 | if (retrieve_specialization (maintmpl, specargs)) | |
2022 | /* We've already got this specialization. */ | |
2023 | return decl; | |
2024 | ||
2025 | DECL_TEMPLATE_SPECIALIZATIONS (maintmpl) = CLASSTYPE_TI_SPEC_INFO (type) | |
2026 | = perm_tree_cons (inner_args, inner_parms, | |
2027 | DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)); | |
2028 | TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type; | |
2029 | return decl; | |
2030 | } | |
2031 | ||
3ac3d9ea | 2032 | /* Creates a TEMPLATE_DECL for the indicated DECL using the template |
f84b4be9 JM |
2033 | parameters given by current_template_args, or reuses a |
2034 | previously existing one, if appropriate. Returns the DECL, or an | |
6757edfe MM |
2035 | equivalent one, if it is replaced via a call to duplicate_decls. |
2036 | ||
2037 | If IS_FRIEND is non-zero, DECL is a friend declaration. */ | |
3ac3d9ea MM |
2038 | |
2039 | tree | |
6757edfe | 2040 | push_template_decl_real (decl, is_friend) |
9a3b49ac | 2041 | tree decl; |
6757edfe | 2042 | int is_friend; |
9a3b49ac MS |
2043 | { |
2044 | tree tmpl; | |
f84b4be9 | 2045 | tree args; |
9a3b49ac | 2046 | tree info; |
f84b4be9 JM |
2047 | tree ctx; |
2048 | int primary; | |
6757edfe MM |
2049 | |
2050 | is_friend |= (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl)); | |
f84b4be9 JM |
2051 | |
2052 | if (is_friend) | |
2053 | /* For a friend, we want the context of the friend function, not | |
2054 | the type of which it is a friend. */ | |
2055 | ctx = DECL_CONTEXT (decl); | |
2c73f9f5 ML |
2056 | else if (DECL_REAL_CONTEXT (decl) |
2057 | && TREE_CODE (DECL_REAL_CONTEXT (decl)) != NAMESPACE_DECL) | |
f84b4be9 JM |
2058 | /* In the case of a virtual function, we want the class in which |
2059 | it is defined. */ | |
2060 | ctx = DECL_REAL_CONTEXT (decl); | |
2061 | else | |
2062 | /* Otherwise, if we're currently definining some class, the DECL | |
2063 | is assumed to be a member of the class. */ | |
2064 | ctx = current_class_type; | |
2065 | ||
2c73f9f5 ML |
2066 | if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL) |
2067 | ctx = NULL_TREE; | |
2068 | ||
2069 | if (!DECL_CONTEXT (decl)) | |
cb0dbb9a | 2070 | DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace); |
2c73f9f5 | 2071 | |
93cdc044 JM |
2072 | /* For determining whether this is a primary template or not, we're really |
2073 | interested in the lexical context, not the true context. */ | |
2074 | if (is_friend) | |
36a117a5 | 2075 | info = current_class_type; |
93cdc044 JM |
2076 | else |
2077 | info = ctx; | |
2078 | ||
2079 | if (info && TREE_CODE (info) == FUNCTION_DECL) | |
2080 | primary = 0; | |
6757edfe MM |
2081 | /* Note that template_class_depth returns 0 if given NULL_TREE, so |
2082 | this next line works even when we are at global scope. */ | |
2083 | else if (processing_template_decl > template_class_depth (info)) | |
9a3b49ac | 2084 | primary = 1; |
f84b4be9 JM |
2085 | else |
2086 | primary = 0; | |
9a3b49ac | 2087 | |
83566abf JM |
2088 | if (primary) |
2089 | { | |
2090 | if (current_lang_name == lang_name_c) | |
8251199e | 2091 | cp_error ("template with C linkage"); |
83566abf | 2092 | if (TREE_CODE (decl) == TYPE_DECL && ANON_AGGRNAME_P (DECL_NAME (decl))) |
8251199e | 2093 | cp_error ("template class without a name"); |
ed44da02 MM |
2094 | if (TREE_CODE (decl) == TYPE_DECL |
2095 | && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE) | |
8251199e | 2096 | cp_error ("template declaration of `%#T'", TREE_TYPE (decl)); |
83566abf JM |
2097 | } |
2098 | ||
73aad9b9 | 2099 | /* Partial specialization. */ |
824b9a4c | 2100 | if (TREE_CODE (decl) == TYPE_DECL && DECL_ARTIFICIAL (decl) |
ed44da02 | 2101 | && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE |
73aad9b9 | 2102 | && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl))) |
6c30752f | 2103 | return process_partial_specialization (decl); |
73aad9b9 | 2104 | |
d32789d8 JM |
2105 | /* [temp.param] A default template-argument shall not be specified in a |
2106 | function template declaration or a function template definition, nor | |
2107 | in the template-parameter-list of the definition of a member of a | |
2108 | class template. */ | |
2109 | { | |
2110 | tree parms; | |
2111 | int issued_default_arg_message = 0; | |
2112 | ||
2113 | parms = current_template_parms; | |
2114 | if (primary) | |
2115 | parms = TREE_CHAIN (parms); | |
2116 | for (; parms; parms = TREE_CHAIN (parms)) | |
2117 | { | |
2118 | tree inner_parms = TREE_VALUE (parms); | |
2119 | int i, ntparms; | |
2120 | ||
2121 | if (TREE_TYPE (inner_parms)) | |
2122 | continue; | |
2123 | ||
2124 | ntparms = TREE_VEC_LENGTH (inner_parms); | |
2125 | for (i = 0; i < ntparms; ++i) | |
2126 | { | |
2127 | if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i))) | |
2128 | { | |
2129 | if (!issued_default_arg_message) | |
2130 | { | |
2131 | cp_error ("default argument for template parameter of class enclosing `%D'", | |
2132 | decl); | |
2133 | issued_default_arg_message = 1; | |
2134 | } | |
2135 | TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE; | |
2136 | } | |
2137 | } | |
2138 | } | |
2139 | } | |
2140 | ||
9a3b49ac MS |
2141 | args = current_template_args (); |
2142 | ||
f84b4be9 JM |
2143 | if (!ctx |
2144 | || TREE_CODE (ctx) == FUNCTION_DECL | |
2145 | || TYPE_BEING_DEFINED (ctx) | |
2146 | || (is_friend && !DECL_TEMPLATE_INFO (decl))) | |
8d08fdba | 2147 | { |
75650646 | 2148 | if (DECL_LANG_SPECIFIC (decl) |
f84b4be9 JM |
2149 | && DECL_TEMPLATE_INFO (decl) |
2150 | && DECL_TI_TEMPLATE (decl)) | |
2151 | tmpl = DECL_TI_TEMPLATE (decl); | |
2152 | else | |
786b5245 | 2153 | { |
f84b4be9 JM |
2154 | tmpl = build_template_decl (decl, current_template_parms); |
2155 | ||
2156 | if (DECL_LANG_SPECIFIC (decl) | |
2157 | && DECL_TEMPLATE_SPECIALIZATION (decl)) | |
2158 | { | |
2159 | /* A specialization of a member template of a template | |
2160 | class. */ | |
2161 | SET_DECL_TEMPLATE_SPECIALIZATION (tmpl); | |
2162 | DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl); | |
2163 | DECL_TEMPLATE_INFO (decl) = NULL_TREE; | |
2164 | } | |
786b5245 | 2165 | } |
8d08fdba MS |
2166 | } |
2167 | else | |
2168 | { | |
e1a5ccf7 | 2169 | tree a, t, current, parms; |
ba4f4e5d | 2170 | int i; |
6633d636 | 2171 | |
73aad9b9 | 2172 | if (CLASSTYPE_TEMPLATE_INSTANTIATION (ctx)) |
8251199e | 2173 | cp_error ("must specialize `%#T' before defining member `%#D'", |
73aad9b9 | 2174 | ctx, decl); |
6b9ab5cc MM |
2175 | if (TREE_CODE (decl) == TYPE_DECL) |
2176 | { | |
ed44da02 MM |
2177 | if ((IS_AGGR_TYPE_CODE (TREE_CODE (TREE_TYPE (decl))) |
2178 | || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE) | |
2179 | && TYPE_TEMPLATE_INFO (TREE_TYPE (decl)) | |
2180 | && TYPE_TI_TEMPLATE (TREE_TYPE (decl))) | |
2181 | tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl)); | |
6b9ab5cc MM |
2182 | else |
2183 | { | |
8251199e | 2184 | cp_error ("`%D' does not declare a template type", decl); |
6b9ab5cc MM |
2185 | return decl; |
2186 | } | |
2187 | } | |
fc378698 | 2188 | else if (! DECL_TEMPLATE_INFO (decl)) |
c91a56d2 | 2189 | { |
8251199e | 2190 | cp_error ("template definition of non-template `%#D'", decl); |
3ac3d9ea | 2191 | return decl; |
c91a56d2 | 2192 | } |
8d08fdba | 2193 | else |
5566b478 | 2194 | tmpl = DECL_TI_TEMPLATE (decl); |
98c1c668 | 2195 | |
e1a5ccf7 JM |
2196 | if (is_member_template (tmpl) |
2197 | && DECL_FUNCTION_TEMPLATE_P (tmpl) | |
2198 | && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl) | |
2199 | && DECL_TEMPLATE_SPECIALIZATION (decl)) | |
98c1c668 | 2200 | { |
e1a5ccf7 JM |
2201 | tree new_tmpl; |
2202 | ||
2203 | /* The declaration is a specialization of a member | |
2204 | template, declared outside the class. Therefore, the | |
2205 | innermost template arguments will be NULL, so we | |
2206 | replace them with the arguments determined by the | |
2207 | earlier call to check_explicit_specialization. */ | |
2208 | args = DECL_TI_ARGS (decl); | |
2209 | ||
2210 | new_tmpl | |
2211 | = build_template_decl (decl, current_template_parms); | |
2212 | DECL_TEMPLATE_RESULT (new_tmpl) = decl; | |
2213 | TREE_TYPE (new_tmpl) = TREE_TYPE (decl); | |
2214 | DECL_TI_TEMPLATE (decl) = new_tmpl; | |
2215 | SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl); | |
2216 | DECL_TEMPLATE_INFO (new_tmpl) = | |
2217 | perm_tree_cons (tmpl, args, NULL_TREE); | |
2218 | ||
2219 | register_specialization (new_tmpl, tmpl, args); | |
2220 | return decl; | |
98c1c668 | 2221 | } |
98c1c668 | 2222 | |
e1a5ccf7 | 2223 | /* Make sure the template headers we got make sense. */ |
6633d636 | 2224 | |
e1a5ccf7 JM |
2225 | parms = DECL_TEMPLATE_PARMS (tmpl); |
2226 | i = TMPL_PARMS_DEPTH (parms); | |
2227 | if (TMPL_ARGS_DEPTH (args) != i) | |
98c1c668 | 2228 | { |
e1a5ccf7 JM |
2229 | cp_error ("expected %d levels of template parms for `%#D', got %d", |
2230 | i, decl, TMPL_ARGS_DEPTH (args)); | |
98c1c668 | 2231 | } |
e1a5ccf7 JM |
2232 | else |
2233 | for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms)) | |
2234 | { | |
2235 | a = TMPL_ARGS_LEVEL (args, i); | |
2236 | t = INNERMOST_TEMPLATE_PARMS (parms); | |
2237 | ||
2238 | if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a)) | |
2239 | { | |
2240 | if (current == decl) | |
2241 | cp_error ("got %d template parameters for `%#D'", | |
2242 | TREE_VEC_LENGTH (a), decl); | |
2243 | else | |
2244 | cp_error ("got %d template parameters for `%#T'", | |
2245 | TREE_VEC_LENGTH (a), current); | |
2246 | cp_error (" but %d required", TREE_VEC_LENGTH (t)); | |
2247 | } | |
98c1c668 | 2248 | |
e1a5ccf7 JM |
2249 | /* Perhaps we should also check that the parms are used in the |
2250 | appropriate qualifying scopes in the declarator? */ | |
6633d636 | 2251 | |
e1a5ccf7 JM |
2252 | if (current == decl) |
2253 | current = ctx; | |
2254 | else | |
2255 | current = TYPE_CONTEXT (current); | |
2256 | } | |
5566b478 | 2257 | } |
8d08fdba | 2258 | |
5566b478 MS |
2259 | DECL_TEMPLATE_RESULT (tmpl) = decl; |
2260 | TREE_TYPE (tmpl) = TREE_TYPE (decl); | |
8d08fdba | 2261 | |
36a117a5 MM |
2262 | /* Push template declarations for global functions and types. Note |
2263 | that we do not try to push a global template friend declared in a | |
2264 | template class; such a thing may well depend on the template | |
39c01e4c | 2265 | parameters of the class. */ |
36a117a5 MM |
2266 | if (! ctx |
2267 | && !(is_friend && template_class_depth (current_class_type) > 0)) | |
2c73f9f5 | 2268 | tmpl = pushdecl_namespace_level (tmpl); |
8d08fdba | 2269 | |
5566b478 | 2270 | if (primary) |
6757edfe | 2271 | DECL_PRIMARY_TEMPLATE (tmpl) = tmpl; |
5566b478 MS |
2272 | |
2273 | info = perm_tree_cons (tmpl, args, NULL_TREE); | |
2274 | ||
824b9a4c | 2275 | if (TREE_CODE (decl) == TYPE_DECL && DECL_ARTIFICIAL (decl)) |
8d08fdba | 2276 | { |
ed44da02 MM |
2277 | SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info); |
2278 | if ((!ctx || TREE_CODE (ctx) != FUNCTION_DECL) | |
2279 | && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE) | |
75650646 | 2280 | DECL_NAME (decl) = classtype_mangled_name (TREE_TYPE (decl)); |
51c184be | 2281 | } |
ec255269 | 2282 | else if (! DECL_LANG_SPECIFIC (decl)) |
8251199e | 2283 | cp_error ("template declaration of `%#D'", decl); |
51c184be | 2284 | else |
5566b478 | 2285 | DECL_TEMPLATE_INFO (decl) = info; |
3ac3d9ea MM |
2286 | |
2287 | return DECL_TEMPLATE_RESULT (tmpl); | |
8d08fdba MS |
2288 | } |
2289 | ||
6757edfe MM |
2290 | tree |
2291 | push_template_decl (decl) | |
2292 | tree decl; | |
2293 | { | |
2294 | return push_template_decl_real (decl, 0); | |
2295 | } | |
2296 | ||
2297 | /* Called when a class template TYPE is redeclared with the indicated | |
2298 | template PARMS, e.g.: | |
7fe6899f MM |
2299 | |
2300 | template <class T> struct S; | |
2301 | template <class T> struct S {}; */ | |
2302 | ||
2303 | void | |
6757edfe | 2304 | redeclare_class_template (type, parms) |
7fe6899f | 2305 | tree type; |
6757edfe | 2306 | tree parms; |
7fe6899f MM |
2307 | { |
2308 | tree tmpl = CLASSTYPE_TI_TEMPLATE (type); | |
6757edfe | 2309 | tree tmpl_parms; |
7fe6899f MM |
2310 | int i; |
2311 | ||
2312 | if (!PRIMARY_TEMPLATE_P (tmpl)) | |
2313 | /* The type is nested in some template class. Nothing to worry | |
2314 | about here; there are no new template parameters for the nested | |
2315 | type. */ | |
2316 | return; | |
2317 | ||
6757edfe MM |
2318 | parms = INNERMOST_TEMPLATE_PARMS (parms); |
2319 | tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl); | |
2320 | ||
7fe6899f MM |
2321 | if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms)) |
2322 | { | |
8251199e JM |
2323 | cp_error_at ("previous declaration `%D'", tmpl); |
2324 | cp_error ("used %d template parameter%s instead of %d", | |
7fe6899f MM |
2325 | TREE_VEC_LENGTH (tmpl_parms), |
2326 | TREE_VEC_LENGTH (tmpl_parms) == 1 ? "" : "s", | |
2327 | TREE_VEC_LENGTH (parms)); | |
2328 | return; | |
2329 | } | |
2330 | ||
2331 | for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i) | |
2332 | { | |
2333 | tree tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i)); | |
2334 | tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i)); | |
2335 | tree tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)); | |
2336 | tree parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i)); | |
2337 | ||
2338 | if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)) | |
2339 | { | |
8251199e JM |
2340 | cp_error_at ("template parameter `%#D'", tmpl_parm); |
2341 | cp_error ("redeclared here as `%#D'", parm); | |
7fe6899f MM |
2342 | return; |
2343 | } | |
2344 | ||
2345 | if (tmpl_default != NULL_TREE && parm_default != NULL_TREE) | |
2346 | { | |
2347 | /* We have in [temp.param]: | |
2348 | ||
2349 | A template-parameter may not be given default arguments | |
2350 | by two different declarations in the same scope. */ | |
8251199e JM |
2351 | cp_error ("redefinition of default argument for `%#D'", parm); |
2352 | cp_error_at (" original definition appeared here", tmpl_parm); | |
7fe6899f MM |
2353 | return; |
2354 | } | |
2355 | ||
2356 | if (parm_default != NULL_TREE) | |
2357 | /* Update the previous template parameters (which are the ones | |
2358 | that will really count) with the new default value. */ | |
2359 | TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default; | |
2360 | } | |
2361 | } | |
75650646 | 2362 | |
75650646 MM |
2363 | /* Attempt to convert the non-type template parameter EXPR to the |
2364 | indicated TYPE. If the conversion is successful, return the | |
2365 | converted value. If the conversion is unsuccesful, return | |
2366 | NULL_TREE if we issued an error message, or error_mark_node if we | |
2367 | did not. We issue error messages for out-and-out bad template | |
2368 | parameters, but not simply because the conversion failed, since we | |
2369 | might be just trying to do argument deduction. By the time this | |
2370 | function is called, neither TYPE nor EXPR may make use of template | |
2371 | parameters. */ | |
2372 | ||
2373 | static tree | |
e1467ff2 | 2374 | convert_nontype_argument (type, expr) |
75650646 MM |
2375 | tree type; |
2376 | tree expr; | |
2377 | { | |
2378 | tree expr_type = TREE_TYPE (expr); | |
2379 | ||
2380 | /* A template-argument for a non-type, non-template | |
2381 | template-parameter shall be one of: | |
2382 | ||
2383 | --an integral constant-expression of integral or enumeration | |
2384 | type; or | |
2385 | ||
2386 | --the name of a non-type template-parameter; or | |
2387 | ||
2388 | --the name of an object or function with external linkage, | |
2389 | including function templates and function template-ids but | |
86052cc3 | 2390 | excluding non-static class members, expressed as id-expression; |
75650646 MM |
2391 | or |
2392 | ||
2393 | --the address of an object or function with external linkage, | |
2394 | including function templates and function template-ids but | |
2395 | excluding non-static class members, expressed as & id-expression | |
2396 | where the & is optional if the name refers to a function or | |
2397 | array; or | |
2398 | ||
2399 | --a pointer to member expressed as described in _expr.unary.op_. */ | |
2400 | ||
86052cc3 JM |
2401 | /* An integral constant-expression can include const variables |
2402 | or enumerators. */ | |
2403 | if (INTEGRAL_TYPE_P (expr_type) && TREE_READONLY_DECL_P (expr)) | |
2404 | expr = decl_constant_value (expr); | |
2405 | ||
7bf2682f MM |
2406 | if (is_overloaded_fn (expr)) |
2407 | /* OK for now. We'll check that it has external linkage later. | |
2408 | Check this first since if expr_type is the unknown_type_node | |
2409 | we would otherwise complain below. */ | |
2410 | ; | |
2411 | else if (INTEGRAL_TYPE_P (expr_type) | |
2412 | || TYPE_PTRMEM_P (expr_type) | |
2413 | || TYPE_PTRMEMFUNC_P (expr_type) | |
2414 | /* The next two are g++ extensions. */ | |
2415 | || TREE_CODE (expr_type) == REAL_TYPE | |
2416 | || TREE_CODE (expr_type) == COMPLEX_TYPE) | |
75650646 | 2417 | { |
86052cc3 | 2418 | if (! TREE_CONSTANT (expr)) |
75650646 | 2419 | { |
db02b6b9 | 2420 | non_constant: |
8251199e | 2421 | cp_error ("non-constant `%E' cannot be used as template argument", |
75650646 MM |
2422 | expr); |
2423 | return NULL_TREE; | |
2424 | } | |
2425 | } | |
2426 | else if (TYPE_PTR_P (expr_type) | |
2427 | /* If expr is the address of an overloaded function, we | |
2428 | will get the unknown_type_node at this point. */ | |
2429 | || expr_type == unknown_type_node) | |
2430 | { | |
2431 | tree referent; | |
49bf0d6f MM |
2432 | tree e = expr; |
2433 | STRIP_NOPS (e); | |
75650646 | 2434 | |
49bf0d6f | 2435 | if (TREE_CODE (e) != ADDR_EXPR) |
75650646 MM |
2436 | { |
2437 | bad_argument: | |
8251199e JM |
2438 | cp_error ("`%E' is not a valid template argument", expr); |
2439 | error ("it must be %s%s with external linkage", | |
75650646 MM |
2440 | TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE |
2441 | ? "a pointer to " : "", | |
2442 | TREE_CODE (TREE_TYPE (TREE_TYPE (expr))) == FUNCTION_TYPE | |
2443 | ? "a function" : "an object"); | |
2444 | return NULL_TREE; | |
2445 | } | |
2446 | ||
49bf0d6f | 2447 | referent = TREE_OPERAND (e, 0); |
75650646 MM |
2448 | STRIP_NOPS (referent); |
2449 | ||
2450 | if (TREE_CODE (referent) == STRING_CST) | |
2451 | { | |
8251199e | 2452 | cp_error ("string literal %E is not a valid template argument", |
75650646 | 2453 | referent); |
8251199e | 2454 | error ("because it is the address of an object with static linkage"); |
75650646 MM |
2455 | return NULL_TREE; |
2456 | } | |
2457 | ||
2458 | if (is_overloaded_fn (referent)) | |
2459 | /* We'll check that it has external linkage later. */ | |
2460 | ; | |
2461 | else if (TREE_CODE (referent) != VAR_DECL) | |
2462 | goto bad_argument; | |
2463 | else if (!TREE_PUBLIC (referent)) | |
2464 | { | |
8251199e | 2465 | cp_error ("address of non-extern `%E' cannot be used as template argument", referent); |
75650646 MM |
2466 | return error_mark_node; |
2467 | } | |
2468 | } | |
c29c4e23 | 2469 | else if (TREE_CODE (expr) == VAR_DECL) |
75650646 MM |
2470 | { |
2471 | if (!TREE_PUBLIC (expr)) | |
2472 | goto bad_argument; | |
2473 | } | |
75650646 MM |
2474 | else |
2475 | { | |
8251199e | 2476 | cp_error ("object `%E' cannot be used as template argument", expr); |
75650646 MM |
2477 | return NULL_TREE; |
2478 | } | |
2479 | ||
2480 | switch (TREE_CODE (type)) | |
2481 | { | |
2482 | case INTEGER_TYPE: | |
2483 | case BOOLEAN_TYPE: | |
2484 | case ENUMERAL_TYPE: | |
2485 | /* For a non-type template-parameter of integral or enumeration | |
2486 | type, integral promotions (_conv.prom_) and integral | |
2487 | conversions (_conv.integral_) are applied. */ | |
2488 | if (!INTEGRAL_TYPE_P (expr_type)) | |
2489 | return error_mark_node; | |
2490 | ||
2491 | /* It's safe to call digest_init in this case; we know we're | |
2492 | just converting one integral constant expression to another. */ | |
db02b6b9 | 2493 | expr = digest_init (type, expr, (tree*) 0); |
75650646 | 2494 | |
db02b6b9 MM |
2495 | if (TREE_CODE (expr) != INTEGER_CST) |
2496 | /* Curiously, some TREE_CONSTNAT integral expressions do not | |
2497 | simplify to integer constants. For example, `3 % 0', | |
2498 | remains a TRUNC_MOD_EXPR. */ | |
2499 | goto non_constant; | |
2500 | ||
2501 | return expr; | |
2502 | ||
abff8e06 JM |
2503 | case REAL_TYPE: |
2504 | case COMPLEX_TYPE: | |
2505 | /* These are g++ extensions. */ | |
2506 | if (TREE_CODE (expr_type) != TREE_CODE (type)) | |
2507 | return error_mark_node; | |
2508 | ||
db02b6b9 MM |
2509 | expr = digest_init (type, expr, (tree*) 0); |
2510 | ||
2511 | if (TREE_CODE (expr) != REAL_CST) | |
2512 | goto non_constant; | |
2513 | ||
2514 | return expr; | |
abff8e06 | 2515 | |
75650646 MM |
2516 | case POINTER_TYPE: |
2517 | { | |
2518 | tree type_pointed_to = TREE_TYPE (type); | |
2519 | ||
2520 | if (TYPE_PTRMEM_P (type)) | |
2521 | /* For a non-type template-parameter of type pointer to data | |
2522 | member, qualification conversions (_conv.qual_) are | |
2523 | applied. */ | |
2524 | return perform_qualification_conversions (type, expr); | |
2525 | else if (TREE_CODE (type_pointed_to) == FUNCTION_TYPE) | |
2526 | { | |
2527 | /* For a non-type template-parameter of type pointer to | |
2528 | function, only the function-to-pointer conversion | |
2529 | (_conv.func_) is applied. If the template-argument | |
2530 | represents a set of overloaded functions (or a pointer to | |
2531 | such), the matching function is selected from the set | |
2532 | (_over.over_). */ | |
2533 | tree fns; | |
2534 | tree fn; | |
2535 | ||
7bf2682f | 2536 | if (TREE_CODE (expr) == ADDR_EXPR) |
75650646 MM |
2537 | fns = TREE_OPERAND (expr, 0); |
2538 | else | |
2539 | fns = expr; | |
2540 | ||
a05273d4 | 2541 | fn = instantiate_type (type_pointed_to, fns, 0); |
75650646 MM |
2542 | |
2543 | if (fn == error_mark_node) | |
2544 | return error_mark_node; | |
2545 | ||
2546 | if (!TREE_PUBLIC (fn)) | |
2547 | { | |
2548 | if (really_overloaded_fn (fns)) | |
2549 | return error_mark_node; | |
2550 | else | |
2551 | goto bad_argument; | |
2552 | } | |
2553 | ||
2554 | expr = build_unary_op (ADDR_EXPR, fn, 0); | |
2555 | ||
3bfdc719 | 2556 | my_friendly_assert (same_type_p (type, TREE_TYPE (expr)), |
75650646 MM |
2557 | 0); |
2558 | return expr; | |
2559 | } | |
2560 | else | |
2561 | { | |
2562 | /* For a non-type template-parameter of type pointer to | |
2563 | object, qualification conversions (_conv.qual_) and the | |
2564 | array-to-pointer conversion (_conv.array_) are applied. | |
2565 | [Note: In particular, neither the null pointer conversion | |
2566 | (_conv.ptr_) nor the derived-to-base conversion | |
2567 | (_conv.ptr_) are applied. Although 0 is a valid | |
2568 | template-argument for a non-type template-parameter of | |
2569 | integral type, it is not a valid template-argument for a | |
e1467ff2 MM |
2570 | non-type template-parameter of pointer type.] |
2571 | ||
2572 | The call to decay_conversion performs the | |
2573 | array-to-pointer conversion, if appropriate. */ | |
2574 | expr = decay_conversion (expr); | |
75650646 MM |
2575 | |
2576 | if (expr == error_mark_node) | |
2577 | return error_mark_node; | |
2578 | else | |
2579 | return perform_qualification_conversions (type, expr); | |
2580 | } | |
2581 | } | |
2582 | break; | |
2583 | ||
2584 | case REFERENCE_TYPE: | |
2585 | { | |
2586 | tree type_referred_to = TREE_TYPE (type); | |
2587 | ||
2588 | if (TREE_CODE (type_referred_to) == FUNCTION_TYPE) | |
2589 | { | |
2590 | /* For a non-type template-parameter of type reference to | |
39c01e4c MM |
2591 | function, no conversions apply. If the |
2592 | template-argument represents a set of overloaded | |
2593 | functions, the matching function is selected from the | |
2594 | set (_over.over_). */ | |
75650646 MM |
2595 | tree fns = expr; |
2596 | tree fn; | |
2597 | ||
a05273d4 | 2598 | fn = instantiate_type (type_referred_to, fns, 0); |
e6e174e5 JM |
2599 | |
2600 | if (fn == error_mark_node) | |
2601 | return error_mark_node; | |
75650646 MM |
2602 | |
2603 | if (!TREE_PUBLIC (fn)) | |
2604 | { | |
2605 | if (really_overloaded_fn (fns)) | |
2606 | /* Don't issue an error here; we might get a different | |
2607 | function if the overloading had worked out | |
2608 | differently. */ | |
2609 | return error_mark_node; | |
2610 | else | |
2611 | goto bad_argument; | |
2612 | } | |
2613 | ||
3bfdc719 MM |
2614 | my_friendly_assert (same_type_p (type_referred_to, |
2615 | TREE_TYPE (fn)), | |
75650646 MM |
2616 | 0); |
2617 | ||
2618 | return fn; | |
2619 | } | |
2620 | else | |
2621 | { | |
2622 | /* For a non-type template-parameter of type reference to | |
2623 | object, no conversions apply. The type referred to by the | |
2624 | reference may be more cv-qualified than the (otherwise | |
2625 | identical) type of the template-argument. The | |
2626 | template-parameter is bound directly to the | |
2627 | template-argument, which must be an lvalue. */ | |
3bfdc719 MM |
2628 | if (!same_type_p (TYPE_MAIN_VARIANT (expr_type), |
2629 | TYPE_MAIN_VARIANT (type)) | |
91063b51 MM |
2630 | || !at_least_as_qualified_p (type_referred_to, |
2631 | expr_type) | |
75650646 MM |
2632 | || !real_lvalue_p (expr)) |
2633 | return error_mark_node; | |
2634 | else | |
2635 | return expr; | |
2636 | } | |
2637 | } | |
2638 | break; | |
2639 | ||
2640 | case RECORD_TYPE: | |
2641 | { | |
2642 | tree fns; | |
2643 | tree fn; | |
2644 | ||
410c2fba KL |
2645 | if (!TYPE_PTRMEMFUNC_P (type)) |
2646 | /* This handles templates like | |
2647 | template<class T, T t> void f(); | |
2648 | when T is substituted with any class. The second template | |
2649 | parameter becomes invalid and the template candidate is | |
2650 | rejected. */ | |
2651 | return error_mark_node; | |
75650646 MM |
2652 | |
2653 | /* For a non-type template-parameter of type pointer to member | |
2654 | function, no conversions apply. If the template-argument | |
2655 | represents a set of overloaded member functions, the | |
2656 | matching member function is selected from the set | |
2657 | (_over.over_). */ | |
2658 | ||
2659 | if (!TYPE_PTRMEMFUNC_P (expr_type) && | |
2660 | expr_type != unknown_type_node) | |
2661 | return error_mark_node; | |
2662 | ||
2663 | if (TREE_CODE (expr) == CONSTRUCTOR) | |
2664 | { | |
2665 | /* A ptr-to-member constant. */ | |
3bfdc719 | 2666 | if (!same_type_p (type, expr_type)) |
75650646 MM |
2667 | return error_mark_node; |
2668 | else | |
2669 | return expr; | |
2670 | } | |
2671 | ||
2672 | if (TREE_CODE (expr) != ADDR_EXPR) | |
2673 | return error_mark_node; | |
2674 | ||
2675 | fns = TREE_OPERAND (expr, 0); | |
2676 | ||
e1467ff2 | 2677 | fn = instantiate_type (TREE_TYPE (TREE_TYPE (type)), |
a05273d4 | 2678 | fns, 0); |
75650646 MM |
2679 | |
2680 | if (fn == error_mark_node) | |
2681 | return error_mark_node; | |
2682 | ||
2683 | expr = build_unary_op (ADDR_EXPR, fn, 0); | |
2684 | ||
3bfdc719 | 2685 | my_friendly_assert (same_type_p (type, TREE_TYPE (expr)), |
75650646 MM |
2686 | 0); |
2687 | return expr; | |
2688 | } | |
2689 | break; | |
2690 | ||
2691 | default: | |
2692 | /* All non-type parameters must have one of these types. */ | |
2693 | my_friendly_abort (0); | |
2694 | break; | |
2695 | } | |
2696 | ||
2697 | return error_mark_node; | |
2698 | } | |
2699 | ||
744fac59 KL |
2700 | /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for |
2701 | template template parameters. Both PARM_PARMS and ARG_PARMS are | |
2702 | vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL | |
2703 | or PARM_DECL. | |
2704 | ||
2705 | ARG_PARMS may contain more parameters than PARM_PARMS. If this is | |
2706 | the case, then extra parameters must have default arguments. | |
2707 | ||
2708 | Consider the example: | |
2709 | template <class T, class Allocator = allocator> class vector; | |
2710 | template<template <class U> class TT> class C; | |
2711 | ||
2712 | C<vector> is a valid instantiation. PARM_PARMS for the above code | |
2713 | contains a TYPE_DECL (for U), ARG_PARMS contains two TYPE_DECLs (for | |
2714 | T and Allocator) and OUTER_ARGS contains the argument that is used to | |
2715 | substitute the TT parameter. */ | |
2716 | ||
2717 | static int | |
2718 | coerce_template_template_parms (parm_parms, arg_parms, in_decl, outer_args) | |
2719 | tree parm_parms, arg_parms, in_decl, outer_args; | |
2720 | { | |
2721 | int nparms, nargs, i; | |
2722 | tree parm, arg; | |
2723 | ||
2724 | my_friendly_assert (TREE_CODE (parm_parms) == TREE_VEC, 0); | |
2725 | my_friendly_assert (TREE_CODE (arg_parms) == TREE_VEC, 0); | |
2726 | ||
2727 | nparms = TREE_VEC_LENGTH (parm_parms); | |
2728 | nargs = TREE_VEC_LENGTH (arg_parms); | |
2729 | ||
2730 | /* The rule here is opposite of coerce_template_parms. */ | |
2731 | if (nargs < nparms | |
2732 | || (nargs > nparms | |
2733 | && TREE_PURPOSE (TREE_VEC_ELT (arg_parms, nparms)) == NULL_TREE)) | |
2734 | return 0; | |
2735 | ||
2736 | for (i = 0; i < nparms; ++i) | |
2737 | { | |
2738 | parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i)); | |
2739 | arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i)); | |
2740 | ||
2741 | if (arg == NULL_TREE || arg == error_mark_node | |
2742 | || parm == NULL_TREE || parm == error_mark_node) | |
2743 | return 0; | |
2744 | ||
2745 | if (TREE_CODE (arg) != TREE_CODE (parm)) | |
2746 | return 0; | |
2747 | ||
2748 | switch (TREE_CODE (parm)) | |
2749 | { | |
2750 | case TYPE_DECL: | |
2751 | break; | |
2752 | ||
2753 | case TEMPLATE_DECL: | |
2754 | /* We encounter instantiations of templates like | |
2755 | template <template <template <class> class> class TT> | |
2756 | class C; */ | |
2757 | sorry ("nested template template parameter"); | |
2758 | return 0; | |
2759 | ||
2760 | case PARM_DECL: | |
2761 | /* The tsubst call is used to handle cases such as | |
2762 | template <class T, template <T> class TT> class D; | |
2763 | i.e. the parameter list of TT depends on earlier parameters. */ | |
3bfdc719 MM |
2764 | if (!same_type_p (tsubst (TREE_TYPE (parm), outer_args, in_decl), |
2765 | TREE_TYPE (arg))) | |
744fac59 KL |
2766 | return 0; |
2767 | break; | |
2768 | ||
2769 | default: | |
2770 | my_friendly_abort (0); | |
2771 | } | |
2772 | } | |
2773 | return 1; | |
2774 | } | |
2775 | ||
8b5b8b7c MM |
2776 | /* Convert the indicated template ARG as necessary to match the |
2777 | indicated template PARM. Returns the converted ARG, or | |
2778 | error_mark_node if the conversion was unsuccessful. Error messages | |
2779 | are issued if COMPLAIN is non-zero. This conversion is for the Ith | |
2780 | parameter in the parameter list. ARGS is the full set of template | |
2781 | arguments deduced so far. */ | |
2782 | ||
2783 | static tree | |
2784 | convert_template_argument (parm, arg, args, complain, i, in_decl) | |
2785 | tree parm; | |
2786 | tree arg; | |
2787 | tree args; | |
2788 | int complain; | |
2789 | int i; | |
2790 | tree in_decl; | |
2791 | { | |
2792 | tree val; | |
2793 | tree inner_args; | |
2794 | int is_type, requires_type, is_tmpl_type, requires_tmpl_type; | |
2795 | ||
2796 | inner_args = innermost_args (args); | |
2797 | ||
2798 | if (TREE_CODE (arg) == TREE_LIST | |
2799 | && TREE_TYPE (arg) != NULL_TREE | |
2800 | && TREE_CODE (TREE_TYPE (arg)) == OFFSET_TYPE) | |
2801 | { | |
2802 | /* The template argument was the name of some | |
2803 | member function. That's usually | |
2804 | illegal, but static members are OK. In any | |
2805 | case, grab the underlying fields/functions | |
2806 | and issue an error later if required. */ | |
2807 | arg = TREE_VALUE (arg); | |
2808 | TREE_TYPE (arg) = unknown_type_node; | |
2809 | } | |
2810 | ||
2811 | requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL; | |
2812 | requires_type = (TREE_CODE (parm) == TYPE_DECL | |
2813 | || requires_tmpl_type); | |
2814 | ||
2815 | /* Check if it is a class template. If REQUIRES_TMPL_TYPE is true, | |
2816 | we also accept implicitly created TYPE_DECL as a valid argument. | |
2817 | This is necessary to handle the case where we pass a template name | |
2818 | to a template template parameter in a scope where we've derived from | |
2819 | in instantiation of that template, so the template name refers to that | |
2820 | instantiation. We really ought to handle this better. */ | |
2821 | is_tmpl_type | |
2822 | = ((TREE_CODE (arg) == TEMPLATE_DECL | |
2823 | && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL) | |
2824 | || (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM | |
2825 | && !CLASSTYPE_TEMPLATE_INFO (arg)) | |
2826 | || (TREE_CODE (arg) == RECORD_TYPE | |
2827 | && CLASSTYPE_TEMPLATE_INFO (arg) | |
2828 | && TREE_CODE (TYPE_NAME (arg)) == TYPE_DECL | |
2829 | && DECL_ARTIFICIAL (TYPE_NAME (arg)) | |
2830 | && requires_tmpl_type | |
b54ccf71 | 2831 | && is_base_of_enclosing_class (arg, current_class_type))); |
8b5b8b7c MM |
2832 | if (is_tmpl_type && TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM) |
2833 | arg = TYPE_STUB_DECL (arg); | |
2834 | else if (is_tmpl_type && TREE_CODE (arg) == RECORD_TYPE) | |
2835 | arg = CLASSTYPE_TI_TEMPLATE (arg); | |
2836 | ||
2837 | is_type = TREE_CODE_CLASS (TREE_CODE (arg)) == 't' || is_tmpl_type; | |
2838 | ||
2839 | if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF | |
2840 | && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM) | |
2841 | { | |
8251199e JM |
2842 | cp_pedwarn ("to refer to a type member of a template parameter,"); |
2843 | cp_pedwarn (" use `typename %E'", arg); | |
8b5b8b7c MM |
2844 | |
2845 | arg = make_typename_type (TREE_OPERAND (arg, 0), | |
2846 | TREE_OPERAND (arg, 1)); | |
2847 | is_type = 1; | |
2848 | } | |
2849 | if (is_type != requires_type) | |
2850 | { | |
2851 | if (in_decl) | |
2852 | { | |
2853 | if (complain) | |
2854 | { | |
8251199e | 2855 | cp_error ("type/value mismatch at argument %d in template parameter list for `%D'", |
8b5b8b7c MM |
2856 | i + 1, in_decl); |
2857 | if (is_type) | |
8251199e | 2858 | cp_error (" expected a constant of type `%T', got `%T'", |
8b5b8b7c MM |
2859 | TREE_TYPE (parm), |
2860 | (is_tmpl_type ? DECL_NAME (arg) : arg)); | |
2861 | else | |
8251199e | 2862 | cp_error (" expected a type, got `%E'", arg); |
8b5b8b7c MM |
2863 | } |
2864 | } | |
2865 | return error_mark_node; | |
2866 | } | |
2867 | if (is_tmpl_type ^ requires_tmpl_type) | |
2868 | { | |
2869 | if (in_decl && complain) | |
2870 | { | |
8251199e | 2871 | cp_error ("type/value mismatch at argument %d in template parameter list for `%D'", |
8b5b8b7c MM |
2872 | i + 1, in_decl); |
2873 | if (is_tmpl_type) | |
8251199e | 2874 | cp_error (" expected a type, got `%T'", DECL_NAME (arg)); |
8b5b8b7c | 2875 | else |
8251199e | 2876 | cp_error (" expected a class template, got `%T'", arg); |
8b5b8b7c MM |
2877 | } |
2878 | return error_mark_node; | |
2879 | } | |
2880 | ||
2881 | if (is_type) | |
2882 | { | |
2883 | if (requires_tmpl_type) | |
2884 | { | |
2885 | tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm); | |
2886 | tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg); | |
2887 | ||
2888 | if (coerce_template_template_parms (parmparm, argparm, | |
2889 | in_decl, inner_args)) | |
2890 | { | |
2891 | val = arg; | |
2892 | ||
2893 | /* TEMPLATE_TEMPLATE_PARM node is preferred over | |
2894 | TEMPLATE_DECL. */ | |
2895 | if (val != error_mark_node | |
2896 | && DECL_TEMPLATE_TEMPLATE_PARM_P (val)) | |
2897 | val = TREE_TYPE (val); | |
2898 | } | |
2899 | else | |
2900 | { | |
2901 | if (in_decl && complain) | |
2902 | { | |
8251199e | 2903 | cp_error ("type/value mismatch at argument %d in template parameter list for `%D'", |
8b5b8b7c | 2904 | i + 1, in_decl); |
8251199e | 2905 | cp_error (" expected a template of type `%D', got `%D'", parm, arg); |
8b5b8b7c MM |
2906 | } |
2907 | ||
2908 | val = error_mark_node; | |
2909 | } | |
2910 | } | |
2911 | else | |
2912 | { | |
2913 | val = groktypename (arg); | |
2914 | if (! processing_template_decl) | |
2915 | { | |
2916 | /* [basic.link]: A name with no linkage (notably, the | |
2917 | name of a class or enumeration declared in a local | |
2918 | scope) shall not be used to declare an entity with | |
2919 | linkage. This implies that names with no linkage | |
2920 | cannot be used as template arguments. */ | |
2921 | tree t = no_linkage_check (val); | |
2922 | if (t) | |
2923 | { | |
2924 | if (ANON_AGGRNAME_P (TYPE_IDENTIFIER (t))) | |
8251199e JM |
2925 | cp_pedwarn |
2926 | ("template-argument `%T' uses anonymous type", val); | |
8b5b8b7c | 2927 | else |
8251199e JM |
2928 | cp_error |
2929 | ("template-argument `%T' uses local type `%T'", | |
8b5b8b7c MM |
2930 | val, t); |
2931 | return error_mark_node; | |
2932 | } | |
2933 | } | |
2934 | } | |
2935 | } | |
2936 | else | |
2937 | { | |
2938 | tree t = tsubst (TREE_TYPE (parm), args, in_decl); | |
2939 | ||
2940 | if (processing_template_decl) | |
2941 | arg = maybe_fold_nontype_arg (arg); | |
2942 | ||
2943 | if (!uses_template_parms (arg) && !uses_template_parms (t)) | |
2944 | /* We used to call digest_init here. However, digest_init | |
2945 | will report errors, which we don't want when complain | |
2946 | is zero. More importantly, digest_init will try too | |
2947 | hard to convert things: for example, `0' should not be | |
2948 | converted to pointer type at this point according to | |
2949 | the standard. Accepting this is not merely an | |
2950 | extension, since deciding whether or not these | |
2951 | conversions can occur is part of determining which | |
2952 | function template to call, or whether a given epxlicit | |
2953 | argument specification is legal. */ | |
2954 | val = convert_nontype_argument (t, arg); | |
2955 | else | |
2956 | val = arg; | |
2957 | ||
2958 | if (val == NULL_TREE) | |
2959 | val = error_mark_node; | |
2960 | else if (val == error_mark_node && complain) | |
8251199e | 2961 | cp_error ("could not convert template argument `%E' to `%T'", |
8b5b8b7c MM |
2962 | arg, t); |
2963 | } | |
2964 | ||
2965 | return val; | |
2966 | } | |
2967 | ||
2968 | /* Convert all template arguments to their appropriate types, and | |
2969 | return a vector containing the innermost resulting template | |
2970 | arguments. If any error occurs, return error_mark_node, and, if | |
2971 | COMPLAIN is non-zero, issue an error message. Some error messages | |
2972 | are issued even if COMPLAIN is zero; for instance, if a template | |
2973 | argument is composed from a local class. | |
75650646 MM |
2974 | |
2975 | If REQUIRE_ALL_ARGUMENTS is non-zero, all arguments must be | |
2976 | provided in ARGLIST, or else trailing parameters must have default | |
2977 | values. If REQUIRE_ALL_ARGUMENTS is zero, we will attempt argument | |
cabc336a MM |
2978 | deduction for any unspecified trailing arguments. |
2979 | ||
2980 | The resulting TREE_VEC is allocated on a temporary obstack, and | |
2981 | must be explicitly copied if it will be permanent. */ | |
de575009 | 2982 | |
8d08fdba | 2983 | static tree |
8b5b8b7c | 2984 | coerce_template_parms (parms, args, in_decl, |
75650646 | 2985 | complain, |
744fac59 | 2986 | require_all_arguments) |
8b5b8b7c | 2987 | tree parms, args; |
8d08fdba | 2988 | tree in_decl; |
75650646 MM |
2989 | int complain; |
2990 | int require_all_arguments; | |
8d08fdba | 2991 | { |
a292b002 | 2992 | int nparms, nargs, i, lost = 0; |
e4a84209 | 2993 | tree inner_args; |
8b5b8b7c MM |
2994 | tree new_args; |
2995 | tree new_inner_args; | |
a292b002 | 2996 | |
8b5b8b7c | 2997 | inner_args = innermost_args (args); |
e4a84209 | 2998 | nargs = NUM_TMPL_ARGS (inner_args); |
a292b002 MS |
2999 | nparms = TREE_VEC_LENGTH (parms); |
3000 | ||
3001 | if (nargs > nparms | |
3002 | || (nargs < nparms | |
75650646 | 3003 | && require_all_arguments |
a292b002 | 3004 | && TREE_PURPOSE (TREE_VEC_ELT (parms, nargs)) == NULL_TREE)) |
8d08fdba | 3005 | { |
75650646 MM |
3006 | if (complain) |
3007 | { | |
8251199e | 3008 | cp_error ("wrong number of template arguments (%d, should be %d)", |
e4a84209 | 3009 | nargs, nparms); |
75650646 MM |
3010 | |
3011 | if (in_decl) | |
8251199e | 3012 | cp_error_at ("provided for `%D'", in_decl); |
75650646 MM |
3013 | } |
3014 | ||
8d08fdba MS |
3015 | return error_mark_node; |
3016 | } | |
3017 | ||
cabc336a | 3018 | new_inner_args = make_temp_vec (nparms); |
8b5b8b7c MM |
3019 | new_args = add_outermost_template_args (args, new_inner_args); |
3020 | for (i = 0; i < nparms; i++) | |
8d08fdba | 3021 | { |
8b5b8b7c MM |
3022 | tree arg; |
3023 | tree parm; | |
e4a84209 | 3024 | |
8b5b8b7c MM |
3025 | /* Get the Ith template parameter. */ |
3026 | parm = TREE_VEC_ELT (parms, i); | |
75650646 | 3027 | |
8b5b8b7c MM |
3028 | /* Calculate the Ith argument. */ |
3029 | if (inner_args && TREE_CODE (inner_args) == TREE_LIST) | |
8d08fdba | 3030 | { |
8b5b8b7c MM |
3031 | arg = TREE_VALUE (inner_args); |
3032 | inner_args = TREE_CHAIN (inner_args); | |
8d08fdba | 3033 | } |
8b5b8b7c MM |
3034 | else if (i < nargs) |
3035 | arg = TREE_VEC_ELT (inner_args, i); | |
3036 | /* If no template argument was supplied, look for a default | |
3037 | value. */ | |
3038 | else if (TREE_PURPOSE (parm) == NULL_TREE) | |
3039 | { | |
3040 | /* There was no default value. */ | |
3041 | my_friendly_assert (!require_all_arguments, 0); | |
3042 | break; | |
3043 | } | |
3044 | else if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL) | |
3045 | arg = tsubst (TREE_PURPOSE (parm), new_args, in_decl); | |
3046 | else | |
3047 | arg = tsubst_expr (TREE_PURPOSE (parm), new_args, in_decl); | |
e4a84209 | 3048 | |
8b5b8b7c | 3049 | /* Now, convert the Ith argument, as necessary. */ |
75650646 MM |
3050 | if (arg == NULL_TREE) |
3051 | /* We're out of arguments. */ | |
3052 | { | |
3053 | my_friendly_assert (!require_all_arguments, 0); | |
3054 | break; | |
3055 | } | |
8b5b8b7c | 3056 | else if (arg == error_mark_node) |
75650646 | 3057 | { |
8251199e | 3058 | cp_error ("template argument %d is invalid", i + 1); |
8b5b8b7c | 3059 | arg = error_mark_node; |
8d08fdba | 3060 | } |
8b5b8b7c MM |
3061 | else |
3062 | arg = convert_template_argument (TREE_VALUE (parm), | |
3063 | arg, new_args, complain, i, | |
3064 | in_decl); | |
3065 | ||
3066 | if (arg == error_mark_node) | |
8d08fdba | 3067 | lost++; |
8b5b8b7c | 3068 | TREE_VEC_ELT (new_inner_args, i) = arg; |
8d08fdba | 3069 | } |
8b5b8b7c | 3070 | |
8d08fdba MS |
3071 | if (lost) |
3072 | return error_mark_node; | |
8b5b8b7c MM |
3073 | |
3074 | return new_inner_args; | |
8d08fdba MS |
3075 | } |
3076 | ||
34016c81 JM |
3077 | /* Returns 1 if template args OT and NT are equivalent. */ |
3078 | ||
3079 | int | |
3080 | template_args_equal (ot, nt) | |
3081 | tree ot, nt; | |
3082 | { | |
3083 | if (nt == ot) | |
3084 | return 1; | |
3085 | if (TREE_CODE (nt) != TREE_CODE (ot)) | |
3086 | return 0; | |
3087 | if (TREE_CODE (nt) == TREE_VEC) | |
3088 | /* For member templates */ | |
3089 | return comp_template_args (ot, nt); | |
3090 | else if (TREE_CODE_CLASS (TREE_CODE (ot)) == 't') | |
3bfdc719 | 3091 | return same_type_p (ot, nt); |
34016c81 JM |
3092 | else |
3093 | return (cp_tree_equal (ot, nt) > 0); | |
3094 | } | |
3095 | ||
3096 | /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets | |
f84b4be9 JM |
3097 | of template arguments. Returns 0 otherwise. */ |
3098 | ||
6757edfe | 3099 | int |
5566b478 MS |
3100 | comp_template_args (oldargs, newargs) |
3101 | tree oldargs, newargs; | |
3102 | { | |
3103 | int i; | |
3104 | ||
386b8a85 JM |
3105 | if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs)) |
3106 | return 0; | |
3107 | ||
5566b478 MS |
3108 | for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i) |
3109 | { | |
3110 | tree nt = TREE_VEC_ELT (newargs, i); | |
3111 | tree ot = TREE_VEC_ELT (oldargs, i); | |
3112 | ||
34016c81 | 3113 | if (! template_args_equal (ot, nt)) |
61a127b3 | 3114 | return 0; |
5566b478 MS |
3115 | } |
3116 | return 1; | |
3117 | } | |
3118 | ||
8d08fdba MS |
3119 | /* Given class template name and parameter list, produce a user-friendly name |
3120 | for the instantiation. */ | |
e92cc029 | 3121 | |
8d08fdba | 3122 | static char * |
36a117a5 | 3123 | mangle_class_name_for_template (name, parms, arglist) |
8d08fdba MS |
3124 | char *name; |
3125 | tree parms, arglist; | |
3126 | { | |
3127 | static struct obstack scratch_obstack; | |
3128 | static char *scratch_firstobj; | |
3129 | int i, nparms; | |
8d08fdba MS |
3130 | |
3131 | if (!scratch_firstobj) | |
fc378698 | 3132 | gcc_obstack_init (&scratch_obstack); |
8d08fdba MS |
3133 | else |
3134 | obstack_free (&scratch_obstack, scratch_firstobj); | |
fc378698 | 3135 | scratch_firstobj = obstack_alloc (&scratch_obstack, 1); |
8d08fdba | 3136 | |
8d08fdba | 3137 | #define ccat(c) obstack_1grow (&scratch_obstack, (c)); |
8d08fdba | 3138 | #define cat(s) obstack_grow (&scratch_obstack, (s), strlen (s)) |
8d08fdba MS |
3139 | |
3140 | cat (name); | |
3141 | ccat ('<'); | |
3142 | nparms = TREE_VEC_LENGTH (parms); | |
36a117a5 | 3143 | arglist = innermost_args (arglist); |
8d08fdba MS |
3144 | my_friendly_assert (nparms == TREE_VEC_LENGTH (arglist), 268); |
3145 | for (i = 0; i < nparms; i++) | |
3146 | { | |
a292b002 MS |
3147 | tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i)); |
3148 | tree arg = TREE_VEC_ELT (arglist, i); | |
8d08fdba MS |
3149 | |
3150 | if (i) | |
3151 | ccat (','); | |
3152 | ||
a292b002 | 3153 | if (TREE_CODE (parm) == TYPE_DECL) |
8d08fdba | 3154 | { |
294471c3 | 3155 | cat (type_as_string_real (arg, 0, 1)); |
8d08fdba MS |
3156 | continue; |
3157 | } | |
73b0fce8 KL |
3158 | else if (TREE_CODE (parm) == TEMPLATE_DECL) |
3159 | { | |
3160 | if (TREE_CODE (arg) == TEMPLATE_DECL) | |
2c73f9f5 ML |
3161 | { |
3162 | /* Already substituted with real template. Just output | |
3163 | the template name here */ | |
cb0dbb9a JM |
3164 | tree context = DECL_CONTEXT (arg); |
3165 | if (context) | |
2c73f9f5 | 3166 | { |
cb0dbb9a | 3167 | my_friendly_assert (TREE_CODE (context) == NAMESPACE_DECL, 980422); |
2c73f9f5 ML |
3168 | cat(decl_as_string (DECL_CONTEXT (arg), 0)); |
3169 | cat("::"); | |
3170 | } | |
3171 | cat (IDENTIFIER_POINTER (DECL_NAME (arg))); | |
3172 | } | |
73b0fce8 KL |
3173 | else |
3174 | /* Output the parameter declaration */ | |
294471c3 | 3175 | cat (type_as_string_real (arg, 0, 1)); |
73b0fce8 KL |
3176 | continue; |
3177 | } | |
8d08fdba MS |
3178 | else |
3179 | my_friendly_assert (TREE_CODE (parm) == PARM_DECL, 269); | |
3180 | ||
3181 | if (TREE_CODE (arg) == TREE_LIST) | |
3182 | { | |
3183 | /* New list cell was built because old chain link was in | |
3184 | use. */ | |
3185 | my_friendly_assert (TREE_PURPOSE (arg) == NULL_TREE, 270); | |
3186 | arg = TREE_VALUE (arg); | |
3187 | } | |
3188 | /* No need to check arglist against parmlist here; we did that | |
3189 | in coerce_template_parms, called from lookup_template_class. */ | |
3190 | cat (expr_as_string (arg, 0)); | |
3191 | } | |
3192 | { | |
3193 | char *bufp = obstack_next_free (&scratch_obstack); | |
3194 | int offset = 0; | |
3195 | while (bufp[offset - 1] == ' ') | |
3196 | offset--; | |
3197 | obstack_blank_fast (&scratch_obstack, offset); | |
3198 | ||
3199 | /* B<C<char> >, not B<C<char>> */ | |
3200 | if (bufp[offset - 1] == '>') | |
3201 | ccat (' '); | |
3202 | } | |
3203 | ccat ('>'); | |
3204 | ccat ('\0'); | |
3205 | return (char *) obstack_base (&scratch_obstack); | |
8d08fdba MS |
3206 | } |
3207 | ||
bd6dd845 | 3208 | static tree |
5566b478 MS |
3209 | classtype_mangled_name (t) |
3210 | tree t; | |
3211 | { | |
3212 | if (CLASSTYPE_TEMPLATE_INFO (t) | |
36a117a5 MM |
3213 | /* Specializations have already had their names set up in |
3214 | lookup_template_class. */ | |
9fbf56f7 MM |
3215 | && !CLASSTYPE_TEMPLATE_SPECIALIZATION (t)) |
3216 | { | |
3217 | tree tmpl = most_general_template (CLASSTYPE_TI_TEMPLATE (t)); | |
3218 | ||
36a117a5 MM |
3219 | /* For non-primary templates, the template parameters are |
3220 | implicit from their surrounding context. */ | |
9fbf56f7 MM |
3221 | if (PRIMARY_TEMPLATE_P (tmpl)) |
3222 | { | |
3223 | tree name = DECL_NAME (tmpl); | |
3224 | char *mangled_name = mangle_class_name_for_template | |
3225 | (IDENTIFIER_POINTER (name), | |
3226 | DECL_INNERMOST_TEMPLATE_PARMS (tmpl), | |
3227 | CLASSTYPE_TI_ARGS (t)); | |
3228 | tree id = get_identifier (mangled_name); | |
3229 | IDENTIFIER_TEMPLATE (id) = name; | |
3230 | return id; | |
3231 | } | |
5566b478 | 3232 | } |
9fbf56f7 MM |
3233 | |
3234 | return TYPE_IDENTIFIER (t); | |
5566b478 MS |
3235 | } |
3236 | ||
3237 | static void | |
3238 | add_pending_template (d) | |
3239 | tree d; | |
3240 | { | |
e92cc029 MS |
3241 | tree ti; |
3242 | ||
3243 | if (TREE_CODE_CLASS (TREE_CODE (d)) == 't') | |
3244 | ti = CLASSTYPE_TEMPLATE_INFO (d); | |
3245 | else | |
3246 | ti = DECL_TEMPLATE_INFO (d); | |
3247 | ||
824b9a4c | 3248 | if (TI_PENDING_TEMPLATE_FLAG (ti)) |
5566b478 MS |
3249 | return; |
3250 | ||
3251 | *template_tail = perm_tree_cons | |
1139b3d8 | 3252 | (build_srcloc_here (), d, NULL_TREE); |
5566b478 | 3253 | template_tail = &TREE_CHAIN (*template_tail); |
824b9a4c | 3254 | TI_PENDING_TEMPLATE_FLAG (ti) = 1; |
5566b478 MS |
3255 | } |
3256 | ||
386b8a85 JM |
3257 | |
3258 | /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS (which | |
3259 | may be either a _DECL or an overloaded function or an | |
3260 | IDENTIFIER_NODE), and ARGLIST. */ | |
3261 | ||
3262 | tree | |
3263 | lookup_template_function (fns, arglist) | |
3264 | tree fns, arglist; | |
3265 | { | |
2c73f9f5 | 3266 | tree type; |
050367a3 | 3267 | |
386b8a85 JM |
3268 | if (fns == NULL_TREE) |
3269 | { | |
8251199e | 3270 | cp_error ("non-template used as template"); |
386b8a85 JM |
3271 | return error_mark_node; |
3272 | } | |
3273 | ||
2c73f9f5 ML |
3274 | type = TREE_TYPE (fns); |
3275 | if (TREE_CODE (fns) == OVERLOAD || !type) | |
3276 | type = unknown_type_node; | |
3277 | ||
672476cb MM |
3278 | if (processing_template_decl) |
3279 | return build_min (TEMPLATE_ID_EXPR, type, fns, arglist); | |
3280 | else | |
3281 | return build (TEMPLATE_ID_EXPR, type, fns, arglist); | |
386b8a85 JM |
3282 | } |
3283 | ||
a2b60a0e MM |
3284 | /* Within the scope of a template class S<T>, the name S gets bound |
3285 | (in build_self_reference) to a TYPE_DECL for the class, not a | |
3286 | TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type, | |
3287 | or one of its enclosing classes, and that type is a template, | |
3288 | return the associated TEMPLATE_DECL. Otherwise, the original | |
3289 | DECL is returned. */ | |
3290 | ||
3291 | tree | |
3292 | maybe_get_template_decl_from_type_decl (decl) | |
3293 | tree decl; | |
3294 | { | |
3295 | return (decl != NULL_TREE | |
3296 | && TREE_CODE (decl) == TYPE_DECL | |
3297 | && DECL_ARTIFICIAL (decl) | |
3298 | && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl))) | |
3299 | ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl; | |
3300 | } | |
386b8a85 | 3301 | |
8d08fdba MS |
3302 | /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of |
3303 | parameters, find the desired type. | |
3304 | ||
3305 | D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments. | |
36a117a5 MM |
3306 | (Actually ARGLIST may be either a TREE_LIST or a TREE_VEC. It will |
3307 | be a TREE_LIST if called directly from the parser, and a TREE_VEC | |
3308 | otherwise.) Since ARGLIST is build on the decl_obstack, we must | |
3309 | copy it here to keep it from being reclaimed when the decl storage | |
3310 | is reclaimed. | |
8d08fdba MS |
3311 | |
3312 | IN_DECL, if non-NULL, is the template declaration we are trying to | |
75650646 MM |
3313 | instantiate. |
3314 | ||
36a117a5 MM |
3315 | If ENTERING_SCOPE is non-zero, we are about to enter the scope of |
3316 | the class we are looking up. | |
3317 | ||
75650646 MM |
3318 | If the template class is really a local class in a template |
3319 | function, then the FUNCTION_CONTEXT is the function in which it is | |
3320 | being instantiated. */ | |
e92cc029 | 3321 | |
8d08fdba | 3322 | tree |
36a117a5 | 3323 | lookup_template_class (d1, arglist, in_decl, context, entering_scope) |
8d08fdba MS |
3324 | tree d1, arglist; |
3325 | tree in_decl; | |
e1467ff2 | 3326 | tree context; |
36a117a5 | 3327 | int entering_scope; |
8d08fdba | 3328 | { |
a703fb38 | 3329 | tree template = NULL_TREE, parmlist; |
dbfe2124 | 3330 | tree t; |
5566b478 MS |
3331 | |
3332 | if (TREE_CODE (d1) == IDENTIFIER_NODE) | |
3333 | { | |
73b0fce8 KL |
3334 | if (IDENTIFIER_LOCAL_VALUE (d1) |
3335 | && DECL_TEMPLATE_TEMPLATE_PARM_P (IDENTIFIER_LOCAL_VALUE (d1))) | |
3336 | template = IDENTIFIER_LOCAL_VALUE (d1); | |
3337 | else | |
3338 | { | |
2c73f9f5 ML |
3339 | if (context) |
3340 | push_decl_namespace (context); | |
67ffc812 MM |
3341 | if (current_class_type != NULL_TREE) |
3342 | template = | |
3343 | maybe_get_template_decl_from_type_decl | |
3344 | (IDENTIFIER_CLASS_VALUE (d1)); | |
a2b60a0e | 3345 | if (template == NULL_TREE) |
2c73f9f5 ML |
3346 | template = lookup_name_nonclass (d1); |
3347 | if (context) | |
3348 | pop_decl_namespace (); | |
73b0fce8 | 3349 | } |
8d019cef JM |
3350 | if (template) |
3351 | context = DECL_CONTEXT (template); | |
5566b478 | 3352 | } |
c91a56d2 MS |
3353 | else if (TREE_CODE (d1) == TYPE_DECL && IS_AGGR_TYPE (TREE_TYPE (d1))) |
3354 | { | |
7bf2682f MM |
3355 | if (CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (d1)) == NULL_TREE) |
3356 | return error_mark_node; | |
c91a56d2 MS |
3357 | template = CLASSTYPE_TI_TEMPLATE (TREE_TYPE (d1)); |
3358 | d1 = DECL_NAME (template); | |
3359 | } | |
ed44da02 MM |
3360 | else if (TREE_CODE (d1) == ENUMERAL_TYPE |
3361 | || (TREE_CODE_CLASS (TREE_CODE (d1)) == 't' | |
3362 | && IS_AGGR_TYPE (d1))) | |
5566b478 | 3363 | { |
ed44da02 | 3364 | template = TYPE_TI_TEMPLATE (d1); |
5566b478 MS |
3365 | d1 = DECL_NAME (template); |
3366 | } | |
93cdc044 JM |
3367 | else if (TREE_CODE (d1) == TEMPLATE_DECL |
3368 | && TREE_CODE (DECL_RESULT (d1)) == TYPE_DECL) | |
3369 | { | |
3370 | template = d1; | |
3371 | d1 = DECL_NAME (template); | |
3372 | context = DECL_CONTEXT (template); | |
3373 | } | |
5566b478 MS |
3374 | else |
3375 | my_friendly_abort (272); | |
8d08fdba | 3376 | |
8d08fdba MS |
3377 | /* With something like `template <class T> class X class X { ... };' |
3378 | we could end up with D1 having nothing but an IDENTIFIER_LOCAL_VALUE. | |
3379 | We don't want to do that, but we have to deal with the situation, so | |
3380 | let's give them some syntax errors to chew on instead of a crash. */ | |
3381 | if (! template) | |
3382 | return error_mark_node; | |
2c73f9f5 | 3383 | |
c3baf4b5 JM |
3384 | if (context == NULL_TREE) |
3385 | context = global_namespace; | |
2c73f9f5 | 3386 | |
8d08fdba MS |
3387 | if (TREE_CODE (template) != TEMPLATE_DECL) |
3388 | { | |
8251199e | 3389 | cp_error ("non-template type `%T' used as a template", d1); |
8d08fdba | 3390 | if (in_decl) |
8251199e | 3391 | cp_error_at ("for template declaration `%D'", in_decl); |
8d08fdba MS |
3392 | return error_mark_node; |
3393 | } | |
8d08fdba | 3394 | |
73b0fce8 KL |
3395 | if (DECL_TEMPLATE_TEMPLATE_PARM_P (template)) |
3396 | { | |
3397 | /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store | |
3398 | template arguments */ | |
3399 | ||
3400 | tree parm = copy_template_template_parm (TREE_TYPE (template)); | |
3401 | tree template2 = TYPE_STUB_DECL (parm); | |
3402 | tree arglist2; | |
3403 | ||
3404 | CLASSTYPE_GOT_SEMICOLON (parm) = 1; | |
3405 | parmlist = DECL_INNERMOST_TEMPLATE_PARMS (template); | |
3406 | ||
744fac59 | 3407 | arglist2 = coerce_template_parms (parmlist, arglist, template, 1, 1); |
73b0fce8 KL |
3408 | if (arglist2 == error_mark_node) |
3409 | return error_mark_node; | |
3410 | ||
3411 | arglist2 = copy_to_permanent (arglist2); | |
3412 | CLASSTYPE_TEMPLATE_INFO (parm) | |
3413 | = perm_tree_cons (template2, arglist2, NULL_TREE); | |
3414 | TYPE_SIZE (parm) = 0; | |
3415 | return parm; | |
3416 | } | |
36a117a5 | 3417 | else |
8d08fdba | 3418 | { |
36a117a5 MM |
3419 | tree template_type = TREE_TYPE (template); |
3420 | tree type_decl; | |
3421 | tree found = NULL_TREE; | |
3422 | int arg_depth; | |
3423 | int parm_depth; | |
dbfe2124 | 3424 | int is_partial_instantiation; |
830bfa74 | 3425 | |
36a117a5 MM |
3426 | template = most_general_template (template); |
3427 | parmlist = DECL_TEMPLATE_PARMS (template); | |
3428 | parm_depth = TMPL_PARMS_DEPTH (parmlist); | |
3429 | arg_depth = TMPL_ARGS_DEPTH (arglist); | |
3430 | ||
3431 | if (arg_depth == 1 && parm_depth > 1) | |
3432 | { | |
39c01e4c | 3433 | /* We've been given an incomplete set of template arguments. |
36a117a5 MM |
3434 | For example, given: |
3435 | ||
3436 | template <class T> struct S1 { | |
3437 | template <class U> struct S2 {}; | |
3438 | template <class U> struct S2<U*> {}; | |
3439 | }; | |
3440 | ||
3441 | we will be called with an ARGLIST of `U*', but the | |
3442 | TEMPLATE will be `template <class T> template | |
3443 | <class U> struct S1<T>::S2'. We must fill in the missing | |
3444 | arguments. */ | |
3445 | my_friendly_assert (context != NULL_TREE, 0); | |
39c01e4c MM |
3446 | while (!IS_AGGR_TYPE_CODE (TREE_CODE (context)) |
3447 | && context != global_namespace) | |
36a117a5 | 3448 | context = DECL_REAL_CONTEXT (context); |
39c01e4c MM |
3449 | |
3450 | if (context == global_namespace) | |
3451 | /* This is bad. We cannot get enough arguments, even from | |
3452 | the surrounding context, to resolve this class. One | |
3453 | case where this might happen is (illegal) code like: | |
3454 | ||
3455 | template <class U> | |
3456 | template <class T> | |
3457 | struct S { | |
3458 | A(const A<T>& a) {} | |
3459 | }; | |
3460 | ||
3461 | We should catch this error sooner (at the opening curly | |
3462 | for `S', but it is better to be safe than sorry here. */ | |
3463 | { | |
8251199e | 3464 | cp_error ("invalid use of `%D'", template); |
39c01e4c MM |
3465 | return error_mark_node; |
3466 | } | |
3467 | ||
ed44da02 | 3468 | arglist = add_to_template_args (TYPE_TI_ARGS (context), |
36a117a5 MM |
3469 | arglist); |
3470 | arg_depth = TMPL_ARGS_DEPTH (arglist); | |
3471 | } | |
5566b478 | 3472 | |
36a117a5 MM |
3473 | my_friendly_assert (parm_depth == arg_depth, 0); |
3474 | ||
3475 | /* Calculate the BOUND_ARGS. These will be the args that are | |
3476 | actually tsubst'd into the definition to create the | |
3477 | instantiation. */ | |
3478 | if (parm_depth > 1) | |
830bfa74 MM |
3479 | { |
3480 | /* We have multiple levels of arguments to coerce, at once. */ | |
830bfa74 | 3481 | int i; |
e4a84209 | 3482 | int saved_depth = TMPL_ARGS_DEPTH (arglist); |
36a117a5 | 3483 | |
cabc336a | 3484 | tree bound_args = make_temp_vec (parm_depth); |
830bfa74 | 3485 | |
e4a84209 | 3486 | for (i = saved_depth, |
830bfa74 | 3487 | t = DECL_TEMPLATE_PARMS (template); |
e4a84209 | 3488 | i > 0 && t != NULL_TREE; |
830bfa74 | 3489 | --i, t = TREE_CHAIN (t)) |
e4a84209 MM |
3490 | { |
3491 | tree a = coerce_template_parms (TREE_VALUE (t), | |
3492 | arglist, template, 1, 1); | |
3493 | SET_TMPL_ARGS_LEVEL (bound_args, i, a); | |
3494 | ||
3495 | /* We temporarily reduce the length of the ARGLIST so | |
3496 | that coerce_template_parms will see only the arguments | |
3497 | corresponding to the template parameters it is | |
3498 | examining. */ | |
3499 | TREE_VEC_LENGTH (arglist)--; | |
3500 | } | |
3501 | ||
3502 | /* Restore the ARGLIST to its full size. */ | |
3503 | TREE_VEC_LENGTH (arglist) = saved_depth; | |
3504 | ||
36a117a5 | 3505 | arglist = bound_args; |
830bfa74 MM |
3506 | } |
3507 | else | |
36a117a5 MM |
3508 | arglist |
3509 | = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist), | |
3510 | innermost_args (arglist), | |
3511 | template, 1, 1); | |
3512 | ||
3513 | if (arglist == error_mark_node) | |
3514 | /* We were unable to bind the arguments. */ | |
5566b478 | 3515 | return error_mark_node; |
5566b478 | 3516 | |
36a117a5 MM |
3517 | /* In the scope of a template class, explicit references to the |
3518 | template class refer to the type of the template, not any | |
3519 | instantiation of it. For example, in: | |
3520 | ||
3521 | template <class T> class C { void f(C<T>); } | |
3522 | ||
3523 | the `C<T>' is just the same as `C'. Outside of the | |
3524 | class, however, such a reference is an instantiation. */ | |
ed44da02 | 3525 | if (comp_template_args (TYPE_TI_ARGS (template_type), |
36a117a5 MM |
3526 | arglist)) |
3527 | { | |
3528 | found = template_type; | |
3529 | ||
3530 | if (!entering_scope && PRIMARY_TEMPLATE_P (template)) | |
5566b478 | 3531 | { |
36a117a5 MM |
3532 | tree ctx; |
3533 | ||
3534 | /* Note that we use DECL_CONTEXT, rather than | |
3535 | CP_DECL_CONTEXT, so that the termination test is | |
3536 | always just `ctx'. We're not interested in namepace | |
3537 | scopes. */ | |
3538 | for (ctx = current_class_type; | |
3539 | ctx; | |
3540 | ctx = (TREE_CODE_CLASS (TREE_CODE (ctx)) == 't') | |
3541 | ? TYPE_CONTEXT (ctx) : DECL_CONTEXT (ctx)) | |
3bfdc719 | 3542 | if (same_type_p (ctx, template_type)) |
36a117a5 MM |
3543 | break; |
3544 | ||
3545 | if (!ctx) | |
3546 | /* We're not in the scope of the class, so the | |
3547 | TEMPLATE_TYPE is not the type we want after | |
3548 | all. */ | |
3549 | found = NULL_TREE; | |
5566b478 MS |
3550 | } |
3551 | } | |
36a117a5 MM |
3552 | |
3553 | if (!found) | |
75650646 | 3554 | { |
36a117a5 MM |
3555 | for (found = DECL_TEMPLATE_INSTANTIATIONS (template); |
3556 | found; found = TREE_CHAIN (found)) | |
3557 | if (comp_template_args (TREE_PURPOSE (found), arglist)) | |
3558 | break; | |
75650646 | 3559 | |
36a117a5 MM |
3560 | if (found) |
3561 | found = TREE_VALUE (found); | |
8d08fdba | 3562 | } |
36a117a5 MM |
3563 | |
3564 | if (found) | |
8d08fdba | 3565 | { |
36a117a5 MM |
3566 | if (can_free (&permanent_obstack, arglist)) |
3567 | obstack_free (&permanent_obstack, arglist); | |
3568 | return found; | |
8d08fdba | 3569 | } |
5566b478 | 3570 | |
36a117a5 MM |
3571 | /* Since we didn't find the type, we'll have to create it. |
3572 | Since we'll be saving this type on the | |
3573 | DECL_TEMPLATE_INSTANTIATIONS list, it must be permanent. */ | |
3574 | push_obstacks (&permanent_obstack, &permanent_obstack); | |
3575 | ||
dbfe2124 | 3576 | /* This type is a "partial instantiation" if any of the template |
486e4077 MM |
3577 | arguments still inolve template parameters. Note that we set |
3578 | IS_PARTIAL_INSTANTIATION for partial specializations as | |
3579 | well. */ | |
dbfe2124 MM |
3580 | is_partial_instantiation = uses_template_parms (arglist); |
3581 | ||
36a117a5 | 3582 | /* Create the type. */ |
ed44da02 MM |
3583 | if (TREE_CODE (template_type) == ENUMERAL_TYPE) |
3584 | { | |
dbfe2124 | 3585 | if (!is_partial_instantiation) |
61a127b3 | 3586 | t = start_enum (TYPE_IDENTIFIER (template_type)); |
ed44da02 | 3587 | else |
dbfe2124 | 3588 | /* We don't want to call start_enum for this type, since |
ed44da02 MM |
3589 | the values for the enumeration constants may involve |
3590 | template parameters. And, no one should be interested | |
3591 | in the enumeration constants for such a type. */ | |
3592 | t = make_node (ENUMERAL_TYPE); | |
3593 | } | |
3594 | else | |
3595 | { | |
3596 | t = make_lang_type (TREE_CODE (template_type)); | |
3597 | CLASSTYPE_DECLARED_CLASS (t) | |
3598 | = CLASSTYPE_DECLARED_CLASS (template_type); | |
3599 | CLASSTYPE_GOT_SEMICOLON (t) = 1; | |
3600 | SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t); | |
f668f160 | 3601 | TYPE_FOR_JAVA (t) = TYPE_FOR_JAVA (template_type); |
ed44da02 MM |
3602 | } |
3603 | ||
61a127b3 | 3604 | /* If we called start_enum above, this information will already |
ed44da02 MM |
3605 | be set up. */ |
3606 | if (!TYPE_NAME (t)) | |
3607 | { | |
3608 | TYPE_CONTEXT (t) = FROB_CONTEXT (context); | |
36a117a5 | 3609 | |
ed44da02 MM |
3610 | /* Create a stub TYPE_DECL for it. */ |
3611 | type_decl = build_decl (TYPE_DECL, DECL_NAME (template), t); | |
3612 | SET_DECL_ARTIFICIAL (type_decl); | |
3613 | DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t); | |
3614 | DECL_SOURCE_FILE (type_decl) | |
3615 | = DECL_SOURCE_FILE (TYPE_STUB_DECL (template_type)); | |
3616 | DECL_SOURCE_LINE (type_decl) | |
3617 | = DECL_SOURCE_LINE (TYPE_STUB_DECL (template_type)); | |
3618 | TYPE_STUB_DECL (t) = TYPE_NAME (t) = type_decl; | |
3619 | } | |
3620 | else | |
3621 | type_decl = TYPE_NAME (t); | |
36a117a5 | 3622 | |
9fbf56f7 MM |
3623 | /* Set up the template information. We have to figure out which |
3624 | template is the immediate parent if this is a full | |
3625 | instantiation. */ | |
3626 | if (parm_depth == 1 || is_partial_instantiation | |
3627 | || !PRIMARY_TEMPLATE_P (template)) | |
3628 | /* This case is easy; there are no member templates involved. */ | |
3629 | found = template; | |
3630 | else | |
3631 | { | |
3632 | /* This is a full instantiation of a member template. There | |
3633 | should be some partial instantiation of which this is an | |
3634 | instance. */ | |
3635 | ||
3636 | for (found = DECL_TEMPLATE_INSTANTIATIONS (template); | |
3637 | found; found = TREE_CHAIN (found)) | |
3638 | { | |
3639 | int success; | |
3640 | tree tmpl = CLASSTYPE_TI_TEMPLATE (TREE_VALUE (found)); | |
3641 | ||
3642 | /* We only want partial instantiations, here, not | |
3643 | specializations or full instantiations. */ | |
3644 | if (CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_VALUE (found)) | |
3645 | || !uses_template_parms (TREE_VALUE (found))) | |
3646 | continue; | |
3647 | ||
3648 | /* Temporarily reduce by one the number of levels in the | |
3649 | ARGLIST and in FOUND so as to avoid comparing the | |
3650 | last set of arguments. */ | |
3651 | TREE_VEC_LENGTH (arglist)--; | |
3652 | TREE_VEC_LENGTH (TREE_PURPOSE (found)) --; | |
3653 | ||
3654 | /* See if the arguments match. If they do, then TMPL is | |
3655 | the partial instantiation we want. */ | |
3656 | success = comp_template_args (TREE_PURPOSE (found), arglist); | |
3657 | ||
3658 | /* Restore the argument vectors to their full size. */ | |
3659 | TREE_VEC_LENGTH (arglist)++; | |
3660 | TREE_VEC_LENGTH (TREE_PURPOSE (found))++; | |
3661 | ||
3662 | if (success) | |
3663 | { | |
3664 | found = tmpl; | |
3665 | break; | |
3666 | } | |
3667 | } | |
3668 | ||
3669 | if (!found) | |
3670 | my_friendly_abort (0); | |
3671 | } | |
3672 | ||
5566b478 | 3673 | arglist = copy_to_permanent (arglist); |
ed44da02 | 3674 | SET_TYPE_TEMPLATE_INFO (t, |
9fbf56f7 | 3675 | tree_cons (found, arglist, NULL_TREE)); |
dbfe2124 MM |
3676 | DECL_TEMPLATE_INSTANTIATIONS (template) |
3677 | = tree_cons (arglist, t, | |
3678 | DECL_TEMPLATE_INSTANTIATIONS (template)); | |
3679 | ||
3680 | if (TREE_CODE (t) == ENUMERAL_TYPE | |
3681 | && !is_partial_instantiation) | |
61a127b3 MM |
3682 | /* Now that the type has been registered on the instantiations |
3683 | list, we set up the enumerators. Because the enumeration | |
3684 | constants may involve the enumeration type itself, we make | |
3685 | sure to register the type first, and then create the | |
3686 | constants. That way, doing tsubst_expr for the enumeration | |
3687 | constants won't result in recursive calls here; we'll find | |
3688 | the instantiation and exit above. */ | |
3689 | tsubst_enum (template_type, t, arglist); | |
dbfe2124 MM |
3690 | |
3691 | /* We're done with the permanent obstack, now. */ | |
3692 | pop_obstacks (); | |
5566b478 | 3693 | |
36a117a5 MM |
3694 | /* Reset the name of the type, now that CLASSTYPE_TEMPLATE_INFO |
3695 | is set up. */ | |
ed44da02 MM |
3696 | if (TREE_CODE (t) != ENUMERAL_TYPE) |
3697 | DECL_NAME (type_decl) = classtype_mangled_name (t); | |
36a117a5 | 3698 | DECL_ASSEMBLER_NAME (type_decl) = DECL_NAME (type_decl); |
dbfe2124 | 3699 | if (!is_partial_instantiation) |
36a117a5 MM |
3700 | { |
3701 | DECL_ASSEMBLER_NAME (type_decl) | |
3702 | = get_identifier (build_overload_name (t, 1, 1)); | |
50a6dbd7 JM |
3703 | |
3704 | /* For backwards compatibility; code that uses | |
3705 | -fexternal-templates expects looking up a template to | |
3706 | instantiate it. I think DDD still relies on this. | |
3707 | (jason 8/20/1998) */ | |
ed44da02 MM |
3708 | if (TREE_CODE (t) != ENUMERAL_TYPE |
3709 | && flag_external_templates | |
36a117a5 MM |
3710 | && CLASSTYPE_INTERFACE_KNOWN (TREE_TYPE (template)) |
3711 | && ! CLASSTYPE_INTERFACE_ONLY (TREE_TYPE (template))) | |
3712 | add_pending_template (t); | |
3713 | } | |
3714 | else | |
077e7015 MM |
3715 | /* If the type makes use of template parameters, the |
3716 | code that generates debugging information will crash. */ | |
3717 | DECL_IGNORED_P (TYPE_STUB_DECL (t)) = 1; | |
8d08fdba | 3718 | |
36a117a5 MM |
3719 | return t; |
3720 | } | |
8d08fdba MS |
3721 | } |
3722 | \f | |
050367a3 MM |
3723 | /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM, or |
3724 | TEMPLATE_PARM_INDEX in T, call FN with the parameter and the DATA. | |
3725 | If FN returns non-zero, the iteration is terminated, and | |
3726 | for_each_template_parm returns 1. Otherwise, the iteration | |
3727 | continues. If FN never returns a non-zero value, the value | |
3728 | returned by for_each_template_parm is 0. If FN is NULL, it is | |
3729 | considered to be the function which always returns 1. */ | |
3730 | ||
8d08fdba | 3731 | int |
050367a3 | 3732 | for_each_template_parm (t, fn, data) |
8d08fdba | 3733 | tree t; |
050367a3 MM |
3734 | tree_fn_t fn; |
3735 | void* data; | |
8d08fdba MS |
3736 | { |
3737 | if (!t) | |
3738 | return 0; | |
581d38d0 MM |
3739 | |
3740 | if (TREE_CODE_CLASS (TREE_CODE (t)) == 't' | |
3741 | && for_each_template_parm (TYPE_CONTEXT (t), fn, data)) | |
3742 | return 1; | |
3743 | ||
8d08fdba MS |
3744 | switch (TREE_CODE (t)) |
3745 | { | |
3746 | case INDIRECT_REF: | |
3747 | case COMPONENT_REF: | |
3748 | /* We assume that the object must be instantiated in order to build | |
3749 | the COMPONENT_REF, so we test only whether the type of the | |
3750 | COMPONENT_REF uses template parms. */ | |
050367a3 | 3751 | return for_each_template_parm (TREE_TYPE (t), fn, data); |
8d08fdba MS |
3752 | |
3753 | case IDENTIFIER_NODE: | |
3754 | if (!IDENTIFIER_TEMPLATE (t)) | |
3755 | return 0; | |
5566b478 | 3756 | my_friendly_abort (42); |
8d08fdba MS |
3757 | |
3758 | /* aggregates of tree nodes */ | |
3759 | case TREE_VEC: | |
3760 | { | |
3761 | int i = TREE_VEC_LENGTH (t); | |
3762 | while (i--) | |
050367a3 | 3763 | if (for_each_template_parm (TREE_VEC_ELT (t, i), fn, data)) |
8d08fdba MS |
3764 | return 1; |
3765 | return 0; | |
3766 | } | |
3767 | case TREE_LIST: | |
050367a3 MM |
3768 | if (for_each_template_parm (TREE_PURPOSE (t), fn, data) |
3769 | || for_each_template_parm (TREE_VALUE (t), fn, data)) | |
8d08fdba | 3770 | return 1; |
050367a3 | 3771 | return for_each_template_parm (TREE_CHAIN (t), fn, data); |
8d08fdba | 3772 | |
2c73f9f5 ML |
3773 | case OVERLOAD: |
3774 | if (for_each_template_parm (OVL_FUNCTION (t), fn, data)) | |
3775 | return 1; | |
3776 | return for_each_template_parm (OVL_CHAIN (t), fn, data); | |
3777 | ||
8d08fdba MS |
3778 | /* constructed type nodes */ |
3779 | case POINTER_TYPE: | |
3780 | case REFERENCE_TYPE: | |
050367a3 | 3781 | return for_each_template_parm (TREE_TYPE (t), fn, data); |
ed44da02 | 3782 | |
8d08fdba | 3783 | case RECORD_TYPE: |
b7484fbe | 3784 | if (TYPE_PTRMEMFUNC_FLAG (t)) |
050367a3 MM |
3785 | return for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE (t), |
3786 | fn, data); | |
ed44da02 MM |
3787 | /* Fall through. */ |
3788 | ||
8d08fdba | 3789 | case UNION_TYPE: |
ed44da02 MM |
3790 | case ENUMERAL_TYPE: |
3791 | if (! TYPE_TEMPLATE_INFO (t)) | |
8d08fdba | 3792 | return 0; |
050367a3 | 3793 | return for_each_template_parm (TREE_VALUE |
ed44da02 | 3794 | (TYPE_TEMPLATE_INFO (t)), |
050367a3 | 3795 | fn, data); |
588c2d1c MM |
3796 | case METHOD_TYPE: |
3797 | if (for_each_template_parm (TYPE_METHOD_BASETYPE (t), fn, data)) | |
8d08fdba | 3798 | return 1; |
588c2d1c MM |
3799 | /* Fall through. */ |
3800 | ||
3801 | case FUNCTION_TYPE: | |
3802 | /* Check the parameter types. Since default arguments are not | |
3803 | instantiated until they are needed, the TYPE_ARG_TYPES may | |
3804 | contain expressions that involve template parameters. But, | |
3805 | no-one should be looking at them yet. And, once they're | |
3806 | instantiated, they don't contain template parameters, so | |
3807 | there's no point in looking at them then, either. */ | |
3808 | { | |
3809 | tree parm; | |
3810 | ||
3811 | for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm)) | |
3812 | if (for_each_template_parm (TREE_VALUE (parm), fn, data)) | |
3813 | return 1; | |
3814 | } | |
3815 | ||
3816 | /* Check the return type, too. */ | |
050367a3 | 3817 | return for_each_template_parm (TREE_TYPE (t), fn, data); |
588c2d1c | 3818 | |
8d08fdba | 3819 | case ARRAY_TYPE: |
050367a3 | 3820 | if (for_each_template_parm (TYPE_DOMAIN (t), fn, data)) |
8d08fdba | 3821 | return 1; |
050367a3 | 3822 | return for_each_template_parm (TREE_TYPE (t), fn, data); |
8d08fdba | 3823 | case OFFSET_TYPE: |
050367a3 | 3824 | if (for_each_template_parm (TYPE_OFFSET_BASETYPE (t), fn, data)) |
8d08fdba | 3825 | return 1; |
050367a3 | 3826 | return for_each_template_parm (TREE_TYPE (t), fn, data); |
8d08fdba MS |
3827 | |
3828 | /* decl nodes */ | |
3829 | case TYPE_DECL: | |
050367a3 | 3830 | return for_each_template_parm (TREE_TYPE (t), fn, data); |
5566b478 | 3831 | |
73b0fce8 KL |
3832 | case TEMPLATE_DECL: |
3833 | /* A template template parameter is encountered */ | |
3834 | if (DECL_TEMPLATE_TEMPLATE_PARM_P (t)) | |
744fac59 KL |
3835 | return for_each_template_parm (TREE_TYPE (t), fn, data); |
3836 | /* Already substituted template template parameter */ | |
73b0fce8 KL |
3837 | return 0; |
3838 | ||
3ac3d9ea | 3839 | case CONST_DECL: |
050367a3 | 3840 | if (for_each_template_parm (DECL_INITIAL (t), fn, data)) |
3ac3d9ea MM |
3841 | return 1; |
3842 | goto check_type_and_context; | |
3843 | ||
8d08fdba | 3844 | case FUNCTION_DECL: |
5566b478 MS |
3845 | case VAR_DECL: |
3846 | /* ??? What about FIELD_DECLs? */ | |
3847 | if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t) | |
050367a3 | 3848 | && for_each_template_parm (DECL_TI_ARGS (t), fn, data)) |
8d08fdba MS |
3849 | return 1; |
3850 | /* fall through */ | |
8d08fdba | 3851 | case PARM_DECL: |
3ac3d9ea | 3852 | check_type_and_context: |
050367a3 | 3853 | if (for_each_template_parm (TREE_TYPE (t), fn, data)) |
5566b478 | 3854 | return 1; |
050367a3 MM |
3855 | if (DECL_CONTEXT (t) |
3856 | && for_each_template_parm (DECL_CONTEXT (t), fn, data)) | |
8d08fdba | 3857 | return 1; |
8d08fdba MS |
3858 | return 0; |
3859 | ||
3860 | case CALL_EXPR: | |
050367a3 | 3861 | return for_each_template_parm (TREE_TYPE (t), fn, data); |
8d08fdba | 3862 | case ADDR_EXPR: |
050367a3 | 3863 | return for_each_template_parm (TREE_OPERAND (t, 0), fn, data); |
8d08fdba MS |
3864 | |
3865 | /* template parm nodes */ | |
73b0fce8 | 3866 | case TEMPLATE_TEMPLATE_PARM: |
744fac59 KL |
3867 | /* Record template parameters such as `T' inside `TT<T>'. */ |
3868 | if (CLASSTYPE_TEMPLATE_INFO (t) | |
3869 | && for_each_template_parm (CLASSTYPE_TI_ARGS (t), fn, data)) | |
3870 | return 1; | |
3871 | case TEMPLATE_TYPE_PARM: | |
f84b4be9 | 3872 | case TEMPLATE_PARM_INDEX: |
050367a3 MM |
3873 | if (fn) |
3874 | return (*fn)(t, data); | |
3875 | else | |
3876 | return 1; | |
8d08fdba MS |
3877 | |
3878 | /* simple type nodes */ | |
3879 | case INTEGER_TYPE: | |
050367a3 | 3880 | if (for_each_template_parm (TYPE_MIN_VALUE (t), fn, data)) |
8d08fdba | 3881 | return 1; |
050367a3 | 3882 | return for_each_template_parm (TYPE_MAX_VALUE (t), fn, data); |
8d08fdba MS |
3883 | |
3884 | case REAL_TYPE: | |
37c46b43 | 3885 | case COMPLEX_TYPE: |
8d08fdba | 3886 | case VOID_TYPE: |
2986ae00 | 3887 | case BOOLEAN_TYPE: |
2c73f9f5 | 3888 | case NAMESPACE_DECL: |
8d08fdba MS |
3889 | return 0; |
3890 | ||
3891 | /* constants */ | |
3892 | case INTEGER_CST: | |
3893 | case REAL_CST: | |
3894 | case STRING_CST: | |
3895 | return 0; | |
3896 | ||
3897 | case ERROR_MARK: | |
3898 | /* Non-error_mark_node ERROR_MARKs are bad things. */ | |
3899 | my_friendly_assert (t == error_mark_node, 274); | |
3900 | /* NOTREACHED */ | |
3901 | return 0; | |
3902 | ||
ec255269 | 3903 | case LOOKUP_EXPR: |
5566b478 | 3904 | case TYPENAME_TYPE: |
8d08fdba MS |
3905 | return 1; |
3906 | ||
61a127b3 MM |
3907 | case PTRMEM_CST: |
3908 | return for_each_template_parm (TREE_TYPE (t), fn, data); | |
3909 | ||
5156628f | 3910 | case SCOPE_REF: |
050367a3 | 3911 | return for_each_template_parm (TREE_OPERAND (t, 0), fn, data); |
5156628f | 3912 | |
db5ae43f MS |
3913 | case CONSTRUCTOR: |
3914 | if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))) | |
050367a3 MM |
3915 | return for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE |
3916 | (TREE_TYPE (t)), fn, data); | |
3917 | return for_each_template_parm (TREE_OPERAND (t, 1), fn, data); | |
db5ae43f | 3918 | |
42976354 BK |
3919 | case MODOP_EXPR: |
3920 | case CAST_EXPR: | |
3921 | case REINTERPRET_CAST_EXPR: | |
3922 | case CONST_CAST_EXPR: | |
3923 | case STATIC_CAST_EXPR: | |
3924 | case DYNAMIC_CAST_EXPR: | |
42976354 BK |
3925 | case ARROW_EXPR: |
3926 | case DOTSTAR_EXPR: | |
3927 | case TYPEID_EXPR: | |
3928 | return 1; | |
3929 | ||
abff8e06 JM |
3930 | case SIZEOF_EXPR: |
3931 | case ALIGNOF_EXPR: | |
050367a3 | 3932 | return for_each_template_parm (TREE_OPERAND (t, 0), fn, data); |
abff8e06 | 3933 | |
8d08fdba MS |
3934 | default: |
3935 | switch (TREE_CODE_CLASS (TREE_CODE (t))) | |
3936 | { | |
3937 | case '1': | |
3938 | case '2': | |
ec255269 | 3939 | case 'e': |
8d08fdba MS |
3940 | case '<': |
3941 | { | |
3942 | int i; | |
bbc06b4b | 3943 | for (i = first_rtl_op (TREE_CODE (t)); --i >= 0;) |
050367a3 | 3944 | if (for_each_template_parm (TREE_OPERAND (t, i), fn, data)) |
8d08fdba MS |
3945 | return 1; |
3946 | return 0; | |
3947 | } | |
3948 | default: | |
3949 | break; | |
3950 | } | |
3951 | sorry ("testing %s for template parms", | |
3952 | tree_code_name [(int) TREE_CODE (t)]); | |
3953 | my_friendly_abort (82); | |
3954 | /* NOTREACHED */ | |
3955 | return 0; | |
3956 | } | |
3957 | } | |
3958 | ||
050367a3 MM |
3959 | int |
3960 | uses_template_parms (t) | |
3961 | tree t; | |
3962 | { | |
3963 | return for_each_template_parm (t, 0, 0); | |
3964 | } | |
3965 | ||
27fafc8d JM |
3966 | static struct tinst_level *current_tinst_level; |
3967 | static struct tinst_level *free_tinst_level; | |
3968 | static int tinst_depth; | |
e9f32eb5 | 3969 | extern int max_tinst_depth; |
5566b478 | 3970 | #ifdef GATHER_STATISTICS |
27fafc8d | 3971 | int depth_reached; |
5566b478 | 3972 | #endif |
27fafc8d JM |
3973 | int tinst_level_tick; |
3974 | int last_template_error_tick; | |
8d08fdba | 3975 | |
7bae46f4 | 3976 | /* Print out all the template instantiations that we are currently |
27fafc8d JM |
3977 | working on. If ERR, we are being called from cp_thing, so do |
3978 | the right thing for an error message. */ | |
7bae46f4 | 3979 | |
27fafc8d JM |
3980 | static void |
3981 | print_template_context (err) | |
3982 | int err; | |
7bae46f4 JM |
3983 | { |
3984 | struct tinst_level *p = current_tinst_level; | |
3985 | int line = lineno; | |
3986 | char *file = input_filename; | |
3987 | ||
49e04385 | 3988 | if (err && p) |
27fafc8d | 3989 | { |
49e04385 MM |
3990 | if (current_function_decl != p->decl |
3991 | && current_function_decl != NULL_TREE) | |
3992 | /* We can get here during the processing of some synthesized | |
3993 | method. Then, p->decl will be the function that's causing | |
3994 | the synthesis. */ | |
3995 | ; | |
27fafc8d | 3996 | else |
27fafc8d | 3997 | { |
49e04385 MM |
3998 | if (current_function_decl == p->decl) |
3999 | /* Avoid redundancy with the the "In function" line. */; | |
4000 | else | |
4001 | fprintf (stderr, "%s: In instantiation of `%s':\n", | |
4002 | file, decl_as_string (p->decl, 0)); | |
4003 | ||
038fb86f JM |
4004 | line = p->line; |
4005 | file = p->file; | |
27fafc8d JM |
4006 | p = p->next; |
4007 | } | |
4008 | } | |
4009 | ||
7bae46f4 JM |
4010 | for (; p; p = p->next) |
4011 | { | |
038fb86f JM |
4012 | fprintf (stderr, "%s:%d: instantiated from `%s'\n", file, line, |
4013 | decl_as_string (p->decl, 0)); | |
4014 | line = p->line; | |
4015 | file = p->file; | |
7bae46f4 | 4016 | } |
038fb86f | 4017 | fprintf (stderr, "%s:%d: instantiated from here\n", file, line); |
7bae46f4 JM |
4018 | } |
4019 | ||
27fafc8d JM |
4020 | /* Called from cp_thing to print the template context for an error. */ |
4021 | ||
4022 | void | |
4023 | maybe_print_template_context () | |
4024 | { | |
4025 | if (last_template_error_tick == tinst_level_tick | |
4026 | || current_tinst_level == 0) | |
4027 | return; | |
4028 | ||
4029 | last_template_error_tick = tinst_level_tick; | |
4030 | print_template_context (1); | |
4031 | } | |
4032 | ||
bd6dd845 | 4033 | static int |
5566b478 MS |
4034 | push_tinst_level (d) |
4035 | tree d; | |
8d08fdba MS |
4036 | { |
4037 | struct tinst_level *new; | |
8d08fdba | 4038 | |
7215f9a0 MS |
4039 | if (tinst_depth >= max_tinst_depth) |
4040 | { | |
8adf5b5e JM |
4041 | /* If the instantiation in question still has unbound template parms, |
4042 | we don't really care if we can't instantiate it, so just return. | |
4043 | This happens with base instantiation for implicit `typename'. */ | |
4044 | if (uses_template_parms (d)) | |
4045 | return 0; | |
4046 | ||
1139b3d8 | 4047 | last_template_error_tick = tinst_level_tick; |
8251199e | 4048 | error ("template instantiation depth exceeds maximum of %d", |
7215f9a0 | 4049 | max_tinst_depth); |
8251199e JM |
4050 | error (" (use -ftemplate-depth-NN to increase the maximum)"); |
4051 | cp_error (" instantiating `%D'", d); | |
5566b478 | 4052 | |
27fafc8d | 4053 | print_template_context (0); |
5566b478 | 4054 | |
7215f9a0 MS |
4055 | return 0; |
4056 | } | |
4057 | ||
8d08fdba MS |
4058 | if (free_tinst_level) |
4059 | { | |
4060 | new = free_tinst_level; | |
4061 | free_tinst_level = new->next; | |
4062 | } | |
4063 | else | |
4064 | new = (struct tinst_level *) xmalloc (sizeof (struct tinst_level)); | |
4065 | ||
5566b478 MS |
4066 | new->decl = d; |
4067 | new->line = lineno; | |
4068 | new->file = input_filename; | |
8d08fdba MS |
4069 | new->next = current_tinst_level; |
4070 | current_tinst_level = new; | |
5566b478 | 4071 | |
7215f9a0 | 4072 | ++tinst_depth; |
5566b478 MS |
4073 | #ifdef GATHER_STATISTICS |
4074 | if (tinst_depth > depth_reached) | |
4075 | depth_reached = tinst_depth; | |
4076 | #endif | |
4077 | ||
27fafc8d | 4078 | ++tinst_level_tick; |
7215f9a0 | 4079 | return 1; |
8d08fdba MS |
4080 | } |
4081 | ||
4082 | void | |
4083 | pop_tinst_level () | |
4084 | { | |
4085 | struct tinst_level *old = current_tinst_level; | |
4086 | ||
ae58fa02 MM |
4087 | /* Restore the filename and line number stashed away when we started |
4088 | this instantiation. */ | |
4089 | lineno = old->line; | |
4090 | input_filename = old->file; | |
5f2c99c4 | 4091 | extract_interface_info (); |
ae58fa02 | 4092 | |
8d08fdba MS |
4093 | current_tinst_level = old->next; |
4094 | old->next = free_tinst_level; | |
4095 | free_tinst_level = old; | |
7215f9a0 | 4096 | --tinst_depth; |
27fafc8d | 4097 | ++tinst_level_tick; |
8d08fdba MS |
4098 | } |
4099 | ||
4100 | struct tinst_level * | |
4101 | tinst_for_decl () | |
4102 | { | |
4103 | struct tinst_level *p = current_tinst_level; | |
4104 | ||
4105 | if (p) | |
4106 | for (; p->next ; p = p->next ) | |
4107 | ; | |
4108 | return p; | |
4109 | } | |
4110 | ||
f84b4be9 JM |
4111 | /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the |
4112 | vector of template arguments, as for tsubst. | |
4113 | ||
4114 | Returns an appropriate tsbust'd friend declaration. */ | |
4115 | ||
4116 | static tree | |
4117 | tsubst_friend_function (decl, args) | |
4118 | tree decl; | |
4119 | tree args; | |
4120 | { | |
4121 | tree new_friend; | |
27fafc8d JM |
4122 | int line = lineno; |
4123 | char *file = input_filename; | |
4124 | ||
4125 | lineno = DECL_SOURCE_LINE (decl); | |
4126 | input_filename = DECL_SOURCE_FILE (decl); | |
4127 | ||
f84b4be9 JM |
4128 | if (TREE_CODE (decl) == FUNCTION_DECL |
4129 | && DECL_TEMPLATE_INSTANTIATION (decl) | |
4130 | && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL) | |
4131 | /* This was a friend declared with an explicit template | |
4132 | argument list, e.g.: | |
4133 | ||
4134 | friend void f<>(T); | |
4135 | ||
4136 | to indicate that f was a template instantiation, not a new | |
4137 | function declaration. Now, we have to figure out what | |
4138 | instantiation of what template. */ | |
4139 | { | |
4140 | tree template_id; | |
4141 | tree new_args; | |
4142 | tree tmpl; | |
f84b4be9 JM |
4143 | |
4144 | template_id | |
4145 | = lookup_template_function (tsubst_expr (DECL_TI_TEMPLATE (decl), | |
f7d98d58 | 4146 | args, NULL_TREE), |
f84b4be9 | 4147 | tsubst (DECL_TI_ARGS (decl), |
f7d98d58 | 4148 | args, NULL_TREE)); |
39c01e4c MM |
4149 | /* FIXME: The decl we create via the next tsubst could be |
4150 | created on a temporary obstack. */ | |
f7d98d58 | 4151 | new_friend = tsubst (decl, args, NULL_TREE); |
36a117a5 MM |
4152 | tmpl = determine_specialization (template_id, new_friend, |
4153 | &new_args, | |
4154 | /*need_member_template=*/0, | |
4155 | /*complain=*/1); | |
27fafc8d JM |
4156 | new_friend = instantiate_template (tmpl, new_args); |
4157 | goto done; | |
f84b4be9 | 4158 | } |
36a117a5 MM |
4159 | |
4160 | new_friend = tsubst (decl, args, NULL_TREE); | |
f84b4be9 | 4161 | |
36a117a5 | 4162 | /* The NEW_FRIEND will look like an instantiation, to the |
f84b4be9 JM |
4163 | compiler, but is not an instantiation from the point of view of |
4164 | the language. For example, we might have had: | |
4165 | ||
4166 | template <class T> struct S { | |
4167 | template <class U> friend void f(T, U); | |
4168 | }; | |
4169 | ||
4170 | Then, in S<int>, template <class U> void f(int, U) is not an | |
4171 | instantiation of anything. */ | |
4172 | DECL_USE_TEMPLATE (new_friend) = 0; | |
4173 | if (TREE_CODE (decl) == TEMPLATE_DECL) | |
4174 | DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0; | |
36a117a5 MM |
4175 | |
4176 | /* The mangled name for the NEW_FRIEND is incorrect. The call to | |
4177 | tsubst will have resulted in a call to | |
4178 | set_mangled_name_for_template_decl. But, the function is not a | |
4179 | template instantiation and should not be mangled like one. | |
4180 | Therefore, we remangle the function name. We don't have to do | |
4181 | this if the NEW_FRIEND is a template since | |
4182 | set_mangled_name_for_template_decl doesn't do anything if the | |
4183 | function declaration still uses template arguments. */ | |
4184 | if (TREE_CODE (new_friend) != TEMPLATE_DECL) | |
4185 | { | |
4186 | set_mangled_name_for_decl (new_friend); | |
4187 | DECL_RTL (new_friend) = 0; | |
4188 | make_decl_rtl (new_friend, NULL_PTR, 1); | |
4189 | } | |
4190 | ||
6eb3bb27 | 4191 | if (DECL_NAMESPACE_SCOPE_P (new_friend)) |
f84b4be9 | 4192 | { |
36a117a5 MM |
4193 | tree old_decl; |
4194 | tree new_friend_args; | |
4195 | ||
f84b4be9 JM |
4196 | if (TREE_CODE (new_friend) == TEMPLATE_DECL) |
4197 | /* This declaration is a `primary' template. */ | |
36a117a5 MM |
4198 | DECL_PRIMARY_TEMPLATE (new_friend) = new_friend; |
4199 | ||
4200 | /* We must save the DECL_TI_ARGS for NEW_FRIEND here because | |
4201 | pushdecl may call duplicate_decls which will free NEW_FRIEND | |
4202 | if possible. */ | |
4203 | new_friend_args = DECL_TI_ARGS (new_friend); | |
4204 | old_decl = pushdecl_namespace_level (new_friend); | |
4205 | ||
4206 | if (old_decl != new_friend) | |
4207 | { | |
4208 | /* This new friend declaration matched an existing | |
4209 | declaration. For example, given: | |
4210 | ||
4211 | template <class T> void f(T); | |
4212 | template <class U> class C { | |
4213 | template <class T> friend void f(T) {} | |
4214 | }; | |
4215 | ||
4216 | the friend declaration actually provides the definition | |
4217 | of `f', once C has been instantiated for some type. So, | |
4218 | old_decl will be the out-of-class template declaration, | |
4219 | while new_friend is the in-class definition. | |
4220 | ||
4221 | But, if `f' was called before this point, the | |
4222 | instantiation of `f' will have DECL_TI_ARGS corresponding | |
4223 | to `T' but not to `U', references to which might appear | |
4224 | in the definition of `f'. Previously, the most general | |
4225 | template for an instantiation of `f' was the out-of-class | |
4226 | version; now it is the in-class version. Therefore, we | |
4227 | run through all specialization of `f', adding to their | |
4228 | DECL_TI_ARGS appropriately. In particular, they need a | |
4229 | new set of outer arguments, corresponding to the | |
4230 | arguments for this class instantiation. | |
4231 | ||
4232 | The same situation can arise with something like this: | |
4233 | ||
4234 | friend void f(int); | |
4235 | template <class T> class C { | |
4236 | friend void f(T) {} | |
4237 | }; | |
4238 | ||
4239 | when `C<int>' is instantiated. Now, `f(int)' is defined | |
4240 | in the class. */ | |
4241 | ||
4242 | if (TREE_CODE (old_decl) != TEMPLATE_DECL) | |
4243 | /* duplicate_decls will take care of this case. */ | |
4244 | ; | |
4245 | else | |
4246 | { | |
4247 | tree t; | |
4248 | ||
4249 | for (t = DECL_TEMPLATE_SPECIALIZATIONS (old_decl); | |
4250 | t != NULL_TREE; | |
4251 | t = TREE_CHAIN (t)) | |
4252 | { | |
4253 | tree spec = TREE_VALUE (t); | |
4254 | ||
4255 | DECL_TI_ARGS (spec) | |
4256 | = add_outermost_template_args (new_friend_args, | |
4257 | DECL_TI_ARGS (spec)); | |
4258 | DECL_TI_ARGS (spec) | |
4259 | = copy_to_permanent (DECL_TI_ARGS (spec)); | |
4260 | } | |
f84b4be9 | 4261 | |
36a117a5 MM |
4262 | /* Now, since specializations are always supposed to |
4263 | hang off of the most general template, we must move | |
4264 | them. */ | |
4265 | t = most_general_template (old_decl); | |
4266 | if (t != old_decl) | |
4267 | { | |
4268 | DECL_TEMPLATE_SPECIALIZATIONS (t) | |
4269 | = chainon (DECL_TEMPLATE_SPECIALIZATIONS (t), | |
4270 | DECL_TEMPLATE_SPECIALIZATIONS (old_decl)); | |
4271 | DECL_TEMPLATE_SPECIALIZATIONS (old_decl) = NULL_TREE; | |
4272 | } | |
4273 | } | |
4274 | ||
4275 | /* The information from NEW_FRIEND has been merged into OLD_DECL | |
4276 | by duplicate_decls. */ | |
4277 | new_friend = old_decl; | |
4278 | } | |
f84b4be9 JM |
4279 | } |
4280 | else if (TYPE_SIZE (DECL_CONTEXT (new_friend))) | |
4281 | { | |
4282 | /* Check to see that the declaration is really present, and, | |
4283 | possibly obtain an improved declaration. */ | |
4284 | tree fn = check_classfn (DECL_CONTEXT (new_friend), | |
4285 | new_friend); | |
4286 | ||
4287 | if (fn) | |
4288 | new_friend = fn; | |
4289 | } | |
4290 | ||
27fafc8d JM |
4291 | done: |
4292 | lineno = line; | |
4293 | input_filename = file; | |
f84b4be9 JM |
4294 | return new_friend; |
4295 | } | |
4296 | ||
1aed5355 MM |
4297 | /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of |
4298 | template arguments, as for tsubst. | |
6757edfe MM |
4299 | |
4300 | Returns an appropriate tsbust'd friend type. */ | |
4301 | ||
4302 | static tree | |
1aed5355 MM |
4303 | tsubst_friend_class (friend_tmpl, args) |
4304 | tree friend_tmpl; | |
6757edfe MM |
4305 | tree args; |
4306 | { | |
1aed5355 | 4307 | tree friend_type; |
f9c3243c | 4308 | tree tmpl = lookup_name (DECL_NAME (friend_tmpl), 1); |
6757edfe MM |
4309 | |
4310 | tmpl = maybe_get_template_decl_from_type_decl (tmpl); | |
4311 | ||
4312 | if (tmpl != NULL_TREE && DECL_CLASS_TEMPLATE_P (tmpl)) | |
4313 | { | |
4314 | /* The friend template has already been declared. Just | |
36a117a5 MM |
4315 | check to see that the declarations match, and install any new |
4316 | default parameters. We must tsubst the default parameters, | |
4317 | of course. We only need the innermost template parameters | |
4318 | because that is all that redeclare_class_template will look | |
4319 | at. */ | |
4320 | tree parms | |
4321 | = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl), | |
4322 | args); | |
4323 | redeclare_class_template (TREE_TYPE (tmpl), parms); | |
6757edfe MM |
4324 | friend_type = TREE_TYPE (tmpl); |
4325 | } | |
4326 | else | |
4327 | { | |
4328 | /* The friend template has not already been declared. In this | |
4329 | case, the instantiation of the template class will cause the | |
4330 | injection of this template into the global scope. */ | |
f9c3243c | 4331 | tmpl = tsubst (friend_tmpl, args, NULL_TREE); |
6757edfe MM |
4332 | |
4333 | /* The new TMPL is not an instantiation of anything, so we | |
4334 | forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for | |
4335 | the new type because that is supposed to be the corresponding | |
4336 | template decl, i.e., TMPL. */ | |
4337 | DECL_USE_TEMPLATE (tmpl) = 0; | |
4338 | DECL_TEMPLATE_INFO (tmpl) = NULL_TREE; | |
4339 | CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0; | |
4340 | ||
4341 | /* Inject this template into the global scope. */ | |
4342 | friend_type = TREE_TYPE (pushdecl_top_level (tmpl)); | |
4343 | } | |
4344 | ||
4345 | return friend_type; | |
4346 | } | |
f84b4be9 | 4347 | |
8d08fdba | 4348 | tree |
5566b478 MS |
4349 | instantiate_class_template (type) |
4350 | tree type; | |
8d08fdba | 4351 | { |
61a127b3 | 4352 | tree template, args, pattern, t; |
36a117a5 | 4353 | tree typedecl; |
8d08fdba | 4354 | |
5566b478 | 4355 | if (type == error_mark_node) |
8d08fdba MS |
4356 | return error_mark_node; |
4357 | ||
5566b478 MS |
4358 | if (TYPE_BEING_DEFINED (type) || TYPE_SIZE (type)) |
4359 | return type; | |
4360 | ||
36a117a5 MM |
4361 | template = most_general_template (CLASSTYPE_TI_TEMPLATE (type)); |
4362 | args = CLASSTYPE_TI_ARGS (type); | |
5566b478 | 4363 | my_friendly_assert (TREE_CODE (template) == TEMPLATE_DECL, 279); |
36a117a5 | 4364 | t = most_specialized_class (template, args); |
73aad9b9 JM |
4365 | |
4366 | if (t == error_mark_node) | |
4367 | { | |
4368 | char *str = "candidates are:"; | |
8251199e | 4369 | cp_error ("ambiguous class template instantiation for `%#T'", type); |
73aad9b9 JM |
4370 | for (t = DECL_TEMPLATE_SPECIALIZATIONS (template); t; t = TREE_CHAIN (t)) |
4371 | { | |
8d019cef | 4372 | if (get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t), |
36a117a5 | 4373 | args)) |
73aad9b9 | 4374 | { |
8251199e | 4375 | cp_error_at ("%s %+#T", str, TREE_TYPE (t)); |
73aad9b9 JM |
4376 | str = " "; |
4377 | } | |
4378 | } | |
4379 | TYPE_BEING_DEFINED (type) = 1; | |
de22184b | 4380 | return error_mark_node; |
73aad9b9 JM |
4381 | } |
4382 | else if (t) | |
4383 | pattern = TREE_TYPE (t); | |
4384 | else | |
4385 | pattern = TREE_TYPE (template); | |
5566b478 MS |
4386 | |
4387 | if (TYPE_SIZE (pattern) == NULL_TREE) | |
ec255269 | 4388 | return type; |
5566b478 | 4389 | |
73aad9b9 | 4390 | if (t) |
36a117a5 MM |
4391 | { |
4392 | /* This TYPE is actually a instantiation of of a partial | |
4393 | specialization. We replace the innermost set of ARGS with | |
4394 | the arguments appropriate for substitution. For example, | |
4395 | given: | |
4396 | ||
4397 | template <class T> struct S {}; | |
4398 | template <class T> struct S<T*> {}; | |
4399 | ||
4400 | and supposing that we are instantiating S<int*>, ARGS will | |
4401 | present be {int*} but we need {int}. */ | |
4402 | tree inner_args | |
4403 | = get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t), | |
4404 | args); | |
4405 | ||
4406 | /* If there were multiple levels in ARGS, replacing the | |
4407 | innermost level would alter CLASSTYPE_TI_ARGS, which we don't | |
4408 | want, so we make a copy first. */ | |
4409 | if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args)) | |
4410 | { | |
4411 | args = copy_node (args); | |
4412 | SET_TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args), inner_args); | |
4413 | } | |
4414 | else | |
4415 | args = inner_args; | |
4416 | } | |
8d019cef | 4417 | |
8adf5b5e | 4418 | if (pedantic && uses_template_parms (args)) |
61a127b3 MM |
4419 | { |
4420 | /* If there are still template parameters amongst the args, then | |
4421 | we can't instantiate the type; there's no telling whether or not one | |
4422 | of the template parameters might eventually be instantiated to some | |
4423 | value that results in a specialization being used. We do the | |
4424 | type as complete so that, for example, declaring one of its | |
4425 | members to be a friend will not be rejected. */ | |
4426 | TYPE_SIZE (type) = integer_zero_node; | |
4427 | return type; | |
4428 | } | |
8adf5b5e | 4429 | |
5566b478 MS |
4430 | TYPE_BEING_DEFINED (type) = 1; |
4431 | ||
4432 | if (! push_tinst_level (type)) | |
4433 | return type; | |
8d08fdba | 4434 | |
5566b478 MS |
4435 | maybe_push_to_top_level (uses_template_parms (type)); |
4436 | pushclass (type, 0); | |
4437 | ||
36a117a5 MM |
4438 | /* We must copy the arguments to the permanent obstack since |
4439 | during the tsubst'ing below they may wind up in the | |
4440 | DECL_TI_ARGS of some instantiated member template. */ | |
4441 | args = copy_to_permanent (args); | |
db897306 | 4442 | |
5566b478 MS |
4443 | if (flag_external_templates) |
4444 | { | |
4445 | if (flag_alt_external_templates) | |
4446 | { | |
4447 | CLASSTYPE_INTERFACE_ONLY (type) = interface_only; | |
4448 | SET_CLASSTYPE_INTERFACE_UNKNOWN_X (type, interface_unknown); | |
4449 | CLASSTYPE_VTABLE_NEEDS_WRITING (type) | |
2604412d JM |
4450 | = (! CLASSTYPE_INTERFACE_ONLY (type) |
4451 | && CLASSTYPE_INTERFACE_KNOWN (type)); | |
5566b478 MS |
4452 | } |
4453 | else | |
4454 | { | |
4455 | CLASSTYPE_INTERFACE_ONLY (type) = CLASSTYPE_INTERFACE_ONLY (pattern); | |
4456 | SET_CLASSTYPE_INTERFACE_UNKNOWN_X | |
4457 | (type, CLASSTYPE_INTERFACE_UNKNOWN (pattern)); | |
4458 | CLASSTYPE_VTABLE_NEEDS_WRITING (type) | |
2604412d JM |
4459 | = (! CLASSTYPE_INTERFACE_ONLY (type) |
4460 | && CLASSTYPE_INTERFACE_KNOWN (type)); | |
5566b478 MS |
4461 | } |
4462 | } | |
4463 | else | |
8d08fdba | 4464 | { |
5566b478 MS |
4465 | SET_CLASSTYPE_INTERFACE_UNKNOWN (type); |
4466 | CLASSTYPE_VTABLE_NEEDS_WRITING (type) = 1; | |
8d08fdba MS |
4467 | } |
4468 | ||
f7da6097 MS |
4469 | TYPE_HAS_CONSTRUCTOR (type) = TYPE_HAS_CONSTRUCTOR (pattern); |
4470 | TYPE_HAS_DESTRUCTOR (type) = TYPE_HAS_DESTRUCTOR (pattern); | |
4471 | TYPE_HAS_ASSIGNMENT (type) = TYPE_HAS_ASSIGNMENT (pattern); | |
4472 | TYPE_OVERLOADS_CALL_EXPR (type) = TYPE_OVERLOADS_CALL_EXPR (pattern); | |
4473 | TYPE_OVERLOADS_ARRAY_REF (type) = TYPE_OVERLOADS_ARRAY_REF (pattern); | |
4474 | TYPE_OVERLOADS_ARROW (type) = TYPE_OVERLOADS_ARROW (pattern); | |
4475 | TYPE_GETS_NEW (type) = TYPE_GETS_NEW (pattern); | |
4476 | TYPE_GETS_DELETE (type) = TYPE_GETS_DELETE (pattern); | |
4477 | TYPE_VEC_DELETE_TAKES_SIZE (type) = TYPE_VEC_DELETE_TAKES_SIZE (pattern); | |
4478 | TYPE_HAS_ASSIGN_REF (type) = TYPE_HAS_ASSIGN_REF (pattern); | |
4479 | TYPE_HAS_CONST_ASSIGN_REF (type) = TYPE_HAS_CONST_ASSIGN_REF (pattern); | |
4480 | TYPE_HAS_ABSTRACT_ASSIGN_REF (type) = TYPE_HAS_ABSTRACT_ASSIGN_REF (pattern); | |
4481 | TYPE_HAS_INIT_REF (type) = TYPE_HAS_INIT_REF (pattern); | |
4482 | TYPE_HAS_CONST_INIT_REF (type) = TYPE_HAS_CONST_INIT_REF (pattern); | |
f7da6097 MS |
4483 | TYPE_HAS_DEFAULT_CONSTRUCTOR (type) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern); |
4484 | TYPE_HAS_CONVERSION (type) = TYPE_HAS_CONVERSION (pattern); | |
4485 | TYPE_USES_COMPLEX_INHERITANCE (type) | |
4486 | = TYPE_USES_COMPLEX_INHERITANCE (pattern); | |
4487 | TYPE_USES_MULTIPLE_INHERITANCE (type) | |
4488 | = TYPE_USES_MULTIPLE_INHERITANCE (pattern); | |
4489 | TYPE_USES_VIRTUAL_BASECLASSES (type) | |
4490 | = TYPE_USES_VIRTUAL_BASECLASSES (pattern); | |
4491 | TYPE_PACKED (type) = TYPE_PACKED (pattern); | |
4492 | TYPE_ALIGN (type) = TYPE_ALIGN (pattern); | |
eff71ab0 | 4493 | TYPE_FOR_JAVA (type) = TYPE_FOR_JAVA (pattern); /* For libjava's JArray<T> */ |
f7da6097 | 4494 | |
2604412d JM |
4495 | /* If this is a partial instantiation, don't tsubst anything. We will |
4496 | only use this type for implicit typename, so the actual contents don't | |
4497 | matter. All that matters is whether a particular name is a type. */ | |
4498 | if (uses_template_parms (type)) | |
4499 | { | |
4500 | TYPE_BINFO_BASETYPES (type) = TYPE_BINFO_BASETYPES (pattern); | |
4501 | TYPE_FIELDS (type) = TYPE_FIELDS (pattern); | |
4502 | TYPE_METHODS (type) = TYPE_METHODS (pattern); | |
4503 | CLASSTYPE_TAGS (type) = CLASSTYPE_TAGS (pattern); | |
4504 | TYPE_SIZE (type) = integer_zero_node; | |
4505 | goto end; | |
4506 | } | |
4507 | ||
5566b478 MS |
4508 | { |
4509 | tree binfo = TYPE_BINFO (type); | |
4510 | tree pbases = TYPE_BINFO_BASETYPES (pattern); | |
4511 | ||
4512 | if (pbases) | |
4513 | { | |
4514 | tree bases; | |
4515 | int i; | |
4516 | int len = TREE_VEC_LENGTH (pbases); | |
de22184b | 4517 | bases = make_tree_vec (len); |
5566b478 MS |
4518 | for (i = 0; i < len; ++i) |
4519 | { | |
711734a9 | 4520 | tree elt, basetype; |
5566b478 MS |
4521 | |
4522 | TREE_VEC_ELT (bases, i) = elt | |
f7d98d58 | 4523 | = tsubst (TREE_VEC_ELT (pbases, i), args, NULL_TREE); |
5566b478 MS |
4524 | BINFO_INHERITANCE_CHAIN (elt) = binfo; |
4525 | ||
711734a9 JM |
4526 | basetype = TREE_TYPE (elt); |
4527 | ||
4528 | if (! IS_AGGR_TYPE (basetype)) | |
8251199e JM |
4529 | cp_error |
4530 | ("base type `%T' of `%T' fails to be a struct or class type", | |
711734a9 JM |
4531 | basetype, type); |
4532 | else if (TYPE_SIZE (complete_type (basetype)) == NULL_TREE) | |
8251199e | 4533 | cp_error ("base class `%T' of `%T' has incomplete type", |
711734a9 JM |
4534 | basetype, type); |
4535 | ||
4536 | /* These are set up in xref_basetypes for normal classes, so | |
4537 | we have to handle them here for template bases. */ | |
dfbcd65a JM |
4538 | |
4539 | unshare_base_binfos (elt); | |
4540 | ||
711734a9 JM |
4541 | if (TYPE_USES_VIRTUAL_BASECLASSES (basetype)) |
4542 | { | |
4543 | TYPE_USES_VIRTUAL_BASECLASSES (type) = 1; | |
4544 | TYPE_USES_COMPLEX_INHERITANCE (type) = 1; | |
4545 | } | |
4546 | TYPE_GETS_NEW (type) |= TYPE_GETS_NEW (basetype); | |
4547 | TYPE_GETS_DELETE (type) |= TYPE_GETS_DELETE (basetype); | |
5566b478 | 4548 | } |
de22184b MS |
4549 | /* Don't initialize this until the vector is filled out, or |
4550 | lookups will crash. */ | |
4551 | BINFO_BASETYPES (binfo) = bases; | |
5566b478 MS |
4552 | } |
4553 | } | |
4554 | ||
5566b478 | 4555 | for (t = CLASSTYPE_TAGS (pattern); t; t = TREE_CHAIN (t)) |
8d08fdba | 4556 | { |
5566b478 | 4557 | tree tag = TREE_VALUE (t); |
36a117a5 MM |
4558 | tree name = TYPE_IDENTIFIER (tag); |
4559 | tree newtag; | |
5566b478 | 4560 | |
ed44da02 | 4561 | newtag = tsubst (tag, args, NULL_TREE); |
61a127b3 | 4562 | if (TREE_CODE (newtag) != ENUMERAL_TYPE) |
ed44da02 | 4563 | { |
486e4077 MM |
4564 | if (TYPE_LANG_SPECIFIC (tag) && CLASSTYPE_IS_TEMPLATE (tag)) |
4565 | /* Unfortunately, lookup_template_class sets | |
4566 | CLASSTYPE_IMPLICIT_INSTANTIATION for a partial | |
4567 | instantiation (i.e., for the type of a member template | |
4568 | class nested within a template class.) This behavior is | |
4569 | required for maybe_process_partial_specialization to work | |
4570 | correctly, but is not accurate in this case; the TAG is not | |
4571 | an instantiation of anything. (The corresponding | |
4572 | TEMPLATE_DECL is an instantiation, but the TYPE is not.) */ | |
4573 | CLASSTYPE_USE_TEMPLATE (newtag) = 0; | |
4574 | ||
ed44da02 MM |
4575 | /* Now, we call pushtag to put this NEWTAG into the scope of |
4576 | TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid | |
4577 | pushtag calling push_template_decl. We don't have to do | |
4578 | this for enums because it will already have been done in | |
4579 | tsubst_enum. */ | |
4580 | if (name) | |
4581 | SET_IDENTIFIER_TYPE_VALUE (name, newtag); | |
4582 | pushtag (name, newtag, /*globalize=*/0); | |
4583 | } | |
8d08fdba MS |
4584 | } |
4585 | ||
5566b478 MS |
4586 | /* Don't replace enum constants here. */ |
4587 | for (t = TYPE_FIELDS (pattern); t; t = TREE_CHAIN (t)) | |
4588 | if (TREE_CODE (t) != CONST_DECL) | |
4589 | { | |
ae58fa02 MM |
4590 | tree r; |
4591 | ||
4592 | /* The the file and line for this declaration, to assist in | |
4593 | error message reporting. Since we called push_tinst_level | |
4594 | above, we don't need to restore these. */ | |
4595 | lineno = DECL_SOURCE_LINE (t); | |
4596 | input_filename = DECL_SOURCE_FILE (t); | |
4597 | ||
4598 | r = tsubst (t, args, NULL_TREE); | |
5566b478 MS |
4599 | if (TREE_CODE (r) == VAR_DECL) |
4600 | { | |
2604412d JM |
4601 | pending_statics = perm_tree_cons (NULL_TREE, r, pending_statics); |
4602 | /* Perhaps we should do more of grokfield here. */ | |
5566b478 MS |
4603 | start_decl_1 (r); |
4604 | DECL_IN_AGGR_P (r) = 1; | |
4605 | DECL_EXTERNAL (r) = 1; | |
4606 | cp_finish_decl (r, DECL_INITIAL (r), NULL_TREE, 0, 0); | |
4607 | } | |
61a127b3 MM |
4608 | |
4609 | /* R will have a TREE_CHAIN if and only if it has already been | |
4610 | processed by finish_member_declaration. This can happen | |
4611 | if, for example, it is a TYPE_DECL for a class-scoped | |
4612 | ENUMERAL_TYPE; such a thing will already have been added to | |
4613 | the field list by tsubst_enum above. */ | |
4614 | if (!TREE_CHAIN (r)) | |
4615 | { | |
4616 | set_current_access_from_decl (r); | |
4617 | finish_member_declaration (r); | |
4618 | } | |
5566b478 | 4619 | } |
8d08fdba | 4620 | |
61a127b3 MM |
4621 | /* Set up the list (TYPE_METHODS) and vector (CLASSTYPE_METHOD_VEC) |
4622 | for this instantiation. */ | |
4623 | for (t = TYPE_METHODS (pattern); t; t = TREE_CHAIN (t)) | |
4624 | { | |
4625 | tree r = tsubst (t, args, NULL_TREE); | |
4626 | set_current_access_from_decl (r); | |
4627 | finish_member_declaration (r); | |
4628 | } | |
8d08fdba | 4629 | |
2604412d JM |
4630 | /* Construct the DECL_FRIENDLIST for the new class type. */ |
4631 | typedecl = TYPE_MAIN_DECL (type); | |
4632 | for (t = DECL_FRIENDLIST (TYPE_MAIN_DECL (pattern)); | |
4633 | t != NULL_TREE; | |
4634 | t = TREE_CHAIN (t)) | |
f84b4be9 | 4635 | { |
2604412d | 4636 | tree friends; |
f84b4be9 | 4637 | |
2604412d JM |
4638 | DECL_FRIENDLIST (typedecl) |
4639 | = tree_cons (TREE_PURPOSE (t), NULL_TREE, | |
4640 | DECL_FRIENDLIST (typedecl)); | |
f84b4be9 | 4641 | |
2604412d JM |
4642 | for (friends = TREE_VALUE (t); |
4643 | friends != NULL_TREE; | |
4644 | friends = TREE_CHAIN (friends)) | |
4645 | { | |
4646 | if (TREE_PURPOSE (friends) == error_mark_node) | |
f84b4be9 | 4647 | { |
2604412d JM |
4648 | TREE_VALUE (DECL_FRIENDLIST (typedecl)) |
4649 | = tree_cons (error_mark_node, | |
4650 | tsubst_friend_function (TREE_VALUE (friends), | |
4651 | args), | |
4652 | TREE_VALUE (DECL_FRIENDLIST (typedecl))); | |
4653 | } | |
4654 | else | |
4655 | { | |
4656 | TREE_VALUE (DECL_FRIENDLIST (typedecl)) | |
4657 | = tree_cons (tsubst (TREE_PURPOSE (friends), args, NULL_TREE), | |
4658 | NULL_TREE, | |
4659 | TREE_VALUE (DECL_FRIENDLIST (typedecl))); | |
f84b4be9 JM |
4660 | |
4661 | } | |
4662 | } | |
2604412d | 4663 | } |
5566b478 | 4664 | |
6757edfe MM |
4665 | for (t = CLASSTYPE_FRIEND_CLASSES (pattern); |
4666 | t != NULL_TREE; | |
4667 | t = TREE_CHAIN (t)) | |
4668 | { | |
4669 | tree friend_type = TREE_VALUE (t); | |
1aed5355 | 4670 | tree new_friend_type; |
6757edfe | 4671 | |
36a117a5 MM |
4672 | if (TREE_CODE (friend_type) == TEMPLATE_DECL) |
4673 | new_friend_type = tsubst_friend_class (friend_type, args); | |
4674 | else if (uses_template_parms (friend_type)) | |
4675 | new_friend_type = tsubst (friend_type, args, NULL_TREE); | |
4676 | else | |
6757edfe MM |
4677 | /* The call to xref_tag_from_type does injection for friend |
4678 | classes. */ | |
1aed5355 | 4679 | new_friend_type = |
36a117a5 MM |
4680 | xref_tag_from_type (friend_type, NULL_TREE, 1); |
4681 | ||
1aed5355 MM |
4682 | |
4683 | if (TREE_CODE (friend_type) == TEMPLATE_DECL) | |
4684 | /* Trick make_friend_class into realizing that the friend | |
4685 | we're adding is a template, not an ordinary class. It's | |
4686 | important that we use make_friend_class since it will | |
4687 | perform some error-checking and output cross-reference | |
4688 | information. */ | |
4689 | ++processing_template_decl; | |
4690 | ||
4691 | make_friend_class (type, new_friend_type); | |
fc378698 | 4692 | |
1aed5355 MM |
4693 | if (TREE_CODE (friend_type) == TEMPLATE_DECL) |
4694 | --processing_template_decl; | |
6757edfe | 4695 | } |
fc378698 | 4696 | |
2604412d JM |
4697 | /* This does injection for friend functions. */ |
4698 | if (!processing_template_decl) | |
4699 | { | |
4700 | t = tsubst (DECL_TEMPLATE_INJECT (template), args, NULL_TREE); | |
5566b478 | 4701 | |
2604412d JM |
4702 | for (; t; t = TREE_CHAIN (t)) |
4703 | { | |
4704 | tree d = TREE_VALUE (t); | |
fc378698 | 4705 | |
2604412d JM |
4706 | if (TREE_CODE (d) == TYPE_DECL) |
4707 | /* Already injected. */; | |
4708 | else | |
4709 | pushdecl (d); | |
4710 | } | |
4711 | } | |
5566b478 | 4712 | |
2604412d JM |
4713 | for (t = TYPE_FIELDS (type); t; t = TREE_CHAIN (t)) |
4714 | if (TREE_CODE (t) == FIELD_DECL) | |
4715 | { | |
4716 | TREE_TYPE (t) = complete_type (TREE_TYPE (t)); | |
4717 | require_complete_type (t); | |
4718 | } | |
5566b478 | 4719 | |
61a127b3 MM |
4720 | /* Set the file and line number information to whatever is given for |
4721 | the class itself. This puts error messages involving generated | |
4722 | implicit functions at a predictable point, and the same point | |
4723 | that would be used for non-template classes. */ | |
4724 | lineno = DECL_SOURCE_LINE (typedecl); | |
4725 | input_filename = DECL_SOURCE_FILE (typedecl); | |
4726 | ||
4727 | unreverse_member_declarations (type); | |
2604412d JM |
4728 | type = finish_struct_1 (type, 0); |
4729 | CLASSTYPE_GOT_SEMICOLON (type) = 1; | |
e92cc029 | 4730 | |
2604412d | 4731 | repo_template_used (type); |
8d08fdba | 4732 | |
2604412d | 4733 | end: |
5566b478 MS |
4734 | TYPE_BEING_DEFINED (type) = 0; |
4735 | popclass (0); | |
4736 | ||
4737 | pop_from_top_level (); | |
4738 | pop_tinst_level (); | |
4739 | ||
4740 | return type; | |
8d08fdba MS |
4741 | } |
4742 | ||
4743 | static int | |
4744 | list_eq (t1, t2) | |
4745 | tree t1, t2; | |
4746 | { | |
4747 | if (t1 == NULL_TREE) | |
4748 | return t2 == NULL_TREE; | |
4749 | if (t2 == NULL_TREE) | |
4750 | return 0; | |
4751 | /* Don't care if one declares its arg const and the other doesn't -- the | |
4752 | main variant of the arg type is all that matters. */ | |
4753 | if (TYPE_MAIN_VARIANT (TREE_VALUE (t1)) | |
4754 | != TYPE_MAIN_VARIANT (TREE_VALUE (t2))) | |
4755 | return 0; | |
4756 | return list_eq (TREE_CHAIN (t1), TREE_CHAIN (t2)); | |
4757 | } | |
4758 | ||
00d3396f JM |
4759 | /* If arg is a non-type template parameter that does not depend on template |
4760 | arguments, fold it like we weren't in the body of a template. */ | |
4761 | ||
4762 | static tree | |
4763 | maybe_fold_nontype_arg (arg) | |
4764 | tree arg; | |
4765 | { | |
4766 | if (TREE_CODE_CLASS (TREE_CODE (arg)) != 't' | |
4767 | && !uses_template_parms (arg)) | |
4768 | { | |
4769 | /* Sometimes, one of the args was an expression involving a | |
4770 | template constant parameter, like N - 1. Now that we've | |
4771 | tsubst'd, we might have something like 2 - 1. This will | |
4772 | confuse lookup_template_class, so we do constant folding | |
4773 | here. We have to unset processing_template_decl, to | |
4774 | fool build_expr_from_tree() into building an actual | |
4775 | tree. */ | |
4776 | ||
4777 | int saved_processing_template_decl = processing_template_decl; | |
4778 | processing_template_decl = 0; | |
4779 | arg = fold (build_expr_from_tree (arg)); | |
4780 | processing_template_decl = saved_processing_template_decl; | |
4781 | } | |
4782 | return arg; | |
4783 | } | |
4784 | ||
a221d52f | 4785 | /* Return the TREE_VEC with the arguments for the innermost template header, |
36a117a5 MM |
4786 | where ARGS is either that or the VEC of VECs for all the |
4787 | arguments. */ | |
a221d52f JM |
4788 | |
4789 | tree | |
36a117a5 | 4790 | innermost_args (args) |
a221d52f | 4791 | tree args; |
a221d52f | 4792 | { |
36a117a5 | 4793 | return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args)); |
a221d52f | 4794 | } |
f84b4be9 | 4795 | |
830bfa74 MM |
4796 | /* Substitute ARGS into the vector of template arguments T. */ |
4797 | ||
4798 | tree | |
4799 | tsubst_template_arg_vector (t, args) | |
4800 | tree t; | |
4801 | tree args; | |
4802 | { | |
4803 | int len = TREE_VEC_LENGTH (t), need_new = 0, i; | |
4804 | tree *elts = (tree *) alloca (len * sizeof (tree)); | |
4805 | ||
4806 | bzero ((char *) elts, len * sizeof (tree)); | |
4807 | ||
4808 | for (i = 0; i < len; i++) | |
4809 | { | |
4810 | if (TREE_VEC_ELT (t, i) != NULL_TREE | |
4811 | && TREE_CODE (TREE_VEC_ELT (t, i)) == TREE_VEC) | |
4812 | elts[i] = tsubst_template_arg_vector (TREE_VEC_ELT (t, i), args); | |
4813 | else | |
4814 | elts[i] = maybe_fold_nontype_arg | |
4815 | (tsubst_expr (TREE_VEC_ELT (t, i), args, NULL_TREE)); | |
4816 | ||
4817 | if (elts[i] != TREE_VEC_ELT (t, i)) | |
4818 | need_new = 1; | |
4819 | } | |
4820 | ||
4821 | if (!need_new) | |
4822 | return t; | |
4823 | ||
4824 | t = make_tree_vec (len); | |
4825 | for (i = 0; i < len; i++) | |
4826 | TREE_VEC_ELT (t, i) = elts[i]; | |
4827 | ||
4828 | return t; | |
4829 | } | |
4830 | ||
36a117a5 MM |
4831 | /* Return the result of substituting ARGS into the template parameters |
4832 | given by PARMS. If there are m levels of ARGS and m + n levels of | |
4833 | PARMS, then the result will contain n levels of PARMS. For | |
4834 | example, if PARMS is `template <class T> template <class U> | |
4835 | template <T*, U, class V>' and ARGS is {{int}, {double}} then the | |
4836 | result will be `template <int*, double, class V>'. */ | |
4837 | ||
4838 | tree | |
4839 | tsubst_template_parms (parms, args) | |
4840 | tree parms; | |
4841 | tree args; | |
4842 | { | |
4843 | tree r; | |
4844 | tree* new_parms = &r; | |
4845 | ||
4846 | for (new_parms = &r; | |
4847 | TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args); | |
4848 | new_parms = &(TREE_CHAIN (*new_parms)), | |
4849 | parms = TREE_CHAIN (parms)) | |
4850 | { | |
4851 | tree new_vec = | |
4852 | make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms))); | |
4853 | int i; | |
4854 | ||
4855 | for (i = 0; i < TREE_VEC_LENGTH (new_vec); ++i) | |
4856 | { | |
4857 | tree default_value = | |
4858 | TREE_PURPOSE (TREE_VEC_ELT (TREE_VALUE (parms), i)); | |
4859 | tree parm_decl = | |
4860 | TREE_VALUE (TREE_VEC_ELT (TREE_VALUE (parms), i)); | |
4861 | ||
4862 | TREE_VEC_ELT (new_vec, i) | |
4863 | = build_tree_list (tsubst (default_value, args, NULL_TREE), | |
4864 | tsubst (parm_decl, args, NULL_TREE)); | |
4865 | ||
4866 | } | |
4867 | ||
4868 | *new_parms = | |
4869 | tree_cons (build_int_2 (0, (TMPL_PARMS_DEPTH (parms) | |
4870 | - TMPL_ARGS_DEPTH (args))), | |
4871 | new_vec, NULL_TREE); | |
4872 | } | |
4873 | ||
4874 | return r; | |
4875 | } | |
4876 | ||
ed44da02 MM |
4877 | /* Substitute the ARGS into the indicated aggregate (or enumeration) |
4878 | type T. If T is not an aggregate or enumeration type, it is | |
4879 | handled as if by tsubst. IN_DECL is as for tsubst. If | |
4880 | ENTERING_SCOPE is non-zero, T is the context for a template which | |
4881 | we are presently tsubst'ing. Return the subsituted value. */ | |
36a117a5 MM |
4882 | |
4883 | tree | |
4884 | tsubst_aggr_type (t, args, in_decl, entering_scope) | |
4885 | tree t; | |
4886 | tree args; | |
4887 | tree in_decl; | |
4888 | int entering_scope; | |
4889 | { | |
4890 | if (t == NULL_TREE) | |
4891 | return NULL_TREE; | |
4892 | ||
4893 | switch (TREE_CODE (t)) | |
4894 | { | |
4895 | case RECORD_TYPE: | |
4896 | if (TYPE_PTRMEMFUNC_P (t)) | |
4897 | { | |
4898 | tree r = build_ptrmemfunc_type | |
4899 | (tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, in_decl)); | |
91063b51 | 4900 | return cp_build_qualified_type (r, TYPE_QUALS (t)); |
36a117a5 MM |
4901 | } |
4902 | ||
4903 | /* else fall through */ | |
ed44da02 | 4904 | case ENUMERAL_TYPE: |
36a117a5 MM |
4905 | case UNION_TYPE: |
4906 | if (uses_template_parms (t)) | |
4907 | { | |
4908 | tree argvec; | |
4909 | tree context; | |
4910 | tree r; | |
4911 | ||
4912 | /* First, determine the context for the type we are looking | |
4913 | up. */ | |
4914 | if (TYPE_CONTEXT (t) != NULL_TREE) | |
4915 | context = tsubst_aggr_type (TYPE_CONTEXT (t), args, | |
4916 | in_decl, /*entering_scope=*/1); | |
4917 | else | |
4918 | context = NULL_TREE; | |
4919 | ||
4920 | /* Then, figure out what arguments are appropriate for the | |
4921 | type we are trying to find. For example, given: | |
4922 | ||
4923 | template <class T> struct S; | |
4924 | template <class T, class U> void f(T, U) { S<U> su; } | |
4925 | ||
4926 | and supposing that we are instantiating f<int, double>, | |
4927 | then our ARGS will be {int, double}, but, when looking up | |
4928 | S we only want {double}. */ | |
ed44da02 | 4929 | argvec = tsubst (TYPE_TI_ARGS (t), args, in_decl); |
36a117a5 MM |
4930 | |
4931 | r = lookup_template_class (t, argvec, in_decl, context, | |
4932 | entering_scope); | |
4933 | ||
91063b51 | 4934 | return cp_build_qualified_type (r, TYPE_QUALS (t)); |
36a117a5 MM |
4935 | } |
4936 | else | |
4937 | /* This is not a template type, so there's nothing to do. */ | |
4938 | return t; | |
4939 | ||
4940 | default: | |
4941 | return tsubst (t, args, in_decl); | |
4942 | } | |
4943 | } | |
4944 | ||
ae58fa02 MM |
4945 | /* Substitute the ARGS into the T, which is a _DECL. TYPE is the |
4946 | (already computed) substitution of ARGS into TREE_TYPE (T), if | |
4947 | appropriate. Return the result of the substitution. IN_DECL is as | |
4948 | for tsubst. */ | |
00d3396f | 4949 | |
75b0bbce | 4950 | tree |
ae58fa02 MM |
4951 | tsubst_decl (t, args, type, in_decl) |
4952 | tree t; | |
4953 | tree args; | |
4954 | tree type; | |
8d08fdba MS |
4955 | tree in_decl; |
4956 | { | |
ae58fa02 MM |
4957 | int saved_lineno; |
4958 | char* saved_filename; | |
b370501f | 4959 | tree r = NULL_TREE; |
830bfa74 | 4960 | |
ae58fa02 MM |
4961 | /* Set the filename and linenumber to improve error-reporting. */ |
4962 | saved_lineno = lineno; | |
4963 | saved_filename = input_filename; | |
4964 | lineno = DECL_SOURCE_LINE (t); | |
4965 | input_filename = DECL_SOURCE_FILE (t); | |
b7484fbe | 4966 | |
8d08fdba MS |
4967 | switch (TREE_CODE (t)) |
4968 | { | |
98c1c668 JM |
4969 | case TEMPLATE_DECL: |
4970 | { | |
4971 | /* We can get here when processing a member template function | |
4972 | of a template class. */ | |
98c1c668 | 4973 | tree decl = DECL_TEMPLATE_RESULT (t); |
386b8a85 | 4974 | tree spec; |
db2767b6 | 4975 | int is_template_template_parm = DECL_TEMPLATE_TEMPLATE_PARM_P (t); |
98c1c668 | 4976 | |
db2767b6 MM |
4977 | if (!is_template_template_parm) |
4978 | { | |
36a117a5 MM |
4979 | /* We might already have an instance of this template. |
4980 | The ARGS are for the surrounding class type, so the | |
4981 | full args contain the tsubst'd args for the context, | |
4982 | plus the innermost args from the template decl. */ | |
4983 | tree tmpl_args = DECL_CLASS_TEMPLATE_P (t) | |
4984 | ? CLASSTYPE_TI_ARGS (TREE_TYPE (t)) | |
4985 | : DECL_TI_ARGS (DECL_RESULT (t)); | |
4986 | tree full_args = tsubst (tmpl_args, args, in_decl); | |
4987 | ||
4988 | /* tsubst_template_arg_vector doesn't copy the vector if | |
4989 | nothing changed. But, *something* should have | |
4990 | changed. */ | |
4991 | my_friendly_assert (full_args != tmpl_args, 0); | |
4992 | ||
4993 | spec = retrieve_specialization (t, full_args); | |
db2767b6 | 4994 | if (spec != NULL_TREE) |
ae58fa02 MM |
4995 | { |
4996 | r = spec; | |
4997 | break; | |
4998 | } | |
db2767b6 | 4999 | } |
98c1c668 JM |
5000 | |
5001 | /* Make a new template decl. It will be similar to the | |
5002 | original, but will record the current template arguments. | |
5003 | We also create a new function declaration, which is just | |
5004 | like the old one, but points to this new template, rather | |
5005 | than the old one. */ | |
ae58fa02 MM |
5006 | r = copy_node (t); |
5007 | copy_lang_decl (r); | |
5008 | my_friendly_assert (DECL_LANG_SPECIFIC (r) != 0, 0); | |
5009 | TREE_CHAIN (r) = NULL_TREE; | |
db2767b6 MM |
5010 | |
5011 | if (is_template_template_parm) | |
5012 | { | |
5013 | tree new_decl = tsubst (decl, args, in_decl); | |
ae58fa02 MM |
5014 | DECL_RESULT (r) = new_decl; |
5015 | TREE_TYPE (r) = TREE_TYPE (new_decl); | |
5016 | break; | |
db2767b6 MM |
5017 | } |
5018 | ||
ae58fa02 | 5019 | DECL_CONTEXT (r) |
36a117a5 MM |
5020 | = tsubst_aggr_type (DECL_CONTEXT (t), args, in_decl, |
5021 | /*entering_scope=*/1); | |
ae58fa02 | 5022 | DECL_CLASS_CONTEXT (r) |
36a117a5 MM |
5023 | = tsubst_aggr_type (DECL_CLASS_CONTEXT (t), args, in_decl, |
5024 | /*entering_scope=*/1); | |
ae58fa02 | 5025 | DECL_TEMPLATE_INFO (r) = build_tree_list (t, args); |
93cdc044 JM |
5026 | |
5027 | if (TREE_CODE (decl) == TYPE_DECL) | |
5028 | { | |
5029 | tree new_type = tsubst (TREE_TYPE (t), args, in_decl); | |
ae58fa02 MM |
5030 | TREE_TYPE (r) = new_type; |
5031 | CLASSTYPE_TI_TEMPLATE (new_type) = r; | |
5032 | DECL_RESULT (r) = TYPE_MAIN_DECL (new_type); | |
5033 | DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type); | |
93cdc044 JM |
5034 | } |
5035 | else | |
5036 | { | |
5037 | tree new_decl = tsubst (decl, args, in_decl); | |
ae58fa02 MM |
5038 | DECL_RESULT (r) = new_decl; |
5039 | DECL_TI_TEMPLATE (new_decl) = r; | |
5040 | TREE_TYPE (r) = TREE_TYPE (new_decl); | |
5041 | DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl); | |
93cdc044 JM |
5042 | } |
5043 | ||
ae58fa02 MM |
5044 | SET_DECL_IMPLICIT_INSTANTIATION (r); |
5045 | DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE; | |
5046 | DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE; | |
98c1c668 JM |
5047 | |
5048 | /* The template parameters for this new template are all the | |
5049 | template parameters for the old template, except the | |
5050 | outermost level of parameters. */ | |
ae58fa02 | 5051 | DECL_TEMPLATE_PARMS (r) |
36a117a5 | 5052 | = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args); |
98c1c668 | 5053 | |
93cdc044 | 5054 | if (PRIMARY_TEMPLATE_P (t)) |
ae58fa02 | 5055 | DECL_PRIMARY_TEMPLATE (r) = r; |
93cdc044 | 5056 | |
8d019cef | 5057 | /* We don't partially instantiate partial specializations. */ |
93cdc044 | 5058 | if (TREE_CODE (decl) == TYPE_DECL) |
ae58fa02 | 5059 | break; |
93cdc044 | 5060 | |
75650646 MM |
5061 | for (spec = DECL_TEMPLATE_SPECIALIZATIONS (t); |
5062 | spec != NULL_TREE; | |
5063 | spec = TREE_CHAIN (spec)) | |
5064 | { | |
5065 | /* It helps to consider example here. Consider: | |
5066 | ||
5067 | template <class T> | |
5068 | struct S { | |
5069 | template <class U> | |
5070 | void f(U u); | |
5071 | ||
5072 | template <> | |
5073 | void f(T* t) {} | |
5074 | }; | |
5075 | ||
5076 | Now, for example, we are instantiating S<int>::f(U u). | |
5077 | We want to make a template: | |
5078 | ||
5079 | template <class U> | |
5080 | void S<int>::f(U); | |
5081 | ||
5082 | It will have a specialization, for the case U = int*, of | |
5083 | the form: | |
5084 | ||
5085 | template <> | |
5086 | void S<int>::f<int*>(int*); | |
5087 | ||
5088 | This specialization will be an instantiation of | |
5089 | the specialization given in the declaration of S, with | |
5090 | argument list int*. */ | |
5091 | ||
5092 | tree fn = TREE_VALUE (spec); | |
5093 | tree spec_args; | |
5094 | tree new_fn; | |
5095 | ||
5096 | if (!DECL_TEMPLATE_SPECIALIZATION (fn)) | |
5097 | /* Instantiations are on the same list, but they're of | |
5098 | no concern to us. */ | |
5099 | continue; | |
5100 | ||
36a117a5 MM |
5101 | if (TREE_CODE (fn) != TEMPLATE_DECL) |
5102 | /* A full specialization. There's no need to record | |
5103 | that here. */ | |
5104 | continue; | |
5105 | ||
5106 | spec_args = tsubst (DECL_TI_ARGS (fn), args, in_decl); | |
5107 | new_fn = tsubst (DECL_RESULT (most_general_template (fn)), | |
5108 | spec_args, in_decl); | |
5109 | DECL_TI_TEMPLATE (new_fn) = fn; | |
ae58fa02 | 5110 | register_specialization (new_fn, r, |
36a117a5 | 5111 | innermost_args (spec_args)); |
75650646 MM |
5112 | } |
5113 | ||
5114 | /* Record this partial instantiation. */ | |
ae58fa02 MM |
5115 | register_specialization (r, t, |
5116 | DECL_TI_ARGS (DECL_RESULT (r))); | |
75650646 | 5117 | |
98c1c668 | 5118 | } |
ae58fa02 | 5119 | break; |
8d08fdba MS |
5120 | |
5121 | case FUNCTION_DECL: | |
5122 | { | |
386b8a85 | 5123 | tree ctx; |
87603ed0 | 5124 | tree argvec = NULL_TREE; |
36a117a5 | 5125 | tree gen_tmpl; |
5566b478 | 5126 | int member; |
d8c4447d MM |
5127 | int args_depth; |
5128 | int parms_depth; | |
5566b478 | 5129 | |
36a117a5 MM |
5130 | /* Nobody should be tsubst'ing into non-template functions. */ |
5131 | my_friendly_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE, 0); | |
5132 | ||
5133 | if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL) | |
5134 | { | |
5135 | tree spec; | |
5136 | ||
5137 | /* Calculate the most general template of which R is a | |
5138 | specialization, and the complete set of arguments used to | |
5139 | specialize R. */ | |
5140 | gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t)); | |
4d85e00e MM |
5141 | argvec = tsubst (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (gen_tmpl)), |
5142 | args, in_decl); | |
36a117a5 MM |
5143 | |
5144 | /* Check to see if we already have this specialization. */ | |
5145 | spec = retrieve_specialization (gen_tmpl, argvec); | |
5146 | if (spec) | |
ae58fa02 MM |
5147 | { |
5148 | r = spec; | |
5149 | break; | |
5150 | } | |
d8c4447d MM |
5151 | |
5152 | /* Here, we deal with the peculiar case: | |
5153 | ||
5154 | template <class T> struct S { | |
5155 | template <class U> friend void f(); | |
5156 | }; | |
5157 | template <class U> friend void f() {} | |
5158 | template S<int>; | |
5159 | template void f<double>(); | |
5160 | ||
5161 | Here, the ARGS for the instantiation of will be {int, | |
5162 | double}. But, we only need as many ARGS as there are | |
5163 | levels of template parameters in CODE_PATTERN. We are | |
5164 | careful not to get fooled into reducing the ARGS in | |
5165 | situations like: | |
5166 | ||
5167 | template <class T> struct S { template <class U> void f(U); } | |
5168 | template <class T> template <> void S<T>::f(int) {} | |
5169 | ||
5170 | which we can spot because the pattern will be a | |
5171 | specialization in this case. */ | |
5172 | args_depth = TMPL_ARGS_DEPTH (args); | |
5173 | parms_depth = | |
5174 | TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t))); | |
5175 | if (args_depth > parms_depth | |
5176 | && !DECL_TEMPLATE_SPECIALIZATION (t)) | |
5177 | { | |
5178 | my_friendly_assert (DECL_FRIEND_P (t), 0); | |
5179 | ||
5180 | if (parms_depth > 1) | |
5181 | { | |
5182 | int i; | |
5183 | ||
5184 | args = make_temp_vec (parms_depth); | |
5185 | for (i = 0; i < parms_depth; ++i) | |
5186 | TREE_VEC_ELT (args, i) = | |
5187 | TREE_VEC_ELT (args, i + (args_depth - parms_depth)); | |
5188 | } | |
5189 | else | |
5190 | args = TREE_VEC_ELT (args, args_depth - parms_depth); | |
5191 | } | |
36a117a5 MM |
5192 | } |
5193 | else | |
5194 | { | |
5195 | /* This special case arises when we have something like this: | |
5196 | ||
5197 | template <class T> struct S { | |
5198 | friend void f<int>(int, double); | |
5199 | }; | |
5200 | ||
5201 | Here, the DECL_TI_TEMPLATE for the friend declaration | |
39c01e4c MM |
5202 | will be a LOOKUP_EXPR or an IDENTIFIER_NODE. We are |
5203 | being called from tsubst_friend_function, and we want | |
5204 | only to create a new decl (R) with appropriate types so | |
5205 | that we can call determine_specialization. */ | |
5206 | my_friendly_assert ((TREE_CODE (DECL_TI_TEMPLATE (t)) | |
5207 | == LOOKUP_EXPR) | |
5208 | || (TREE_CODE (DECL_TI_TEMPLATE (t)) | |
5209 | == IDENTIFIER_NODE), 0); | |
36a117a5 MM |
5210 | gen_tmpl = NULL_TREE; |
5211 | } | |
5212 | ||
6eb3bb27 | 5213 | if (DECL_CLASS_SCOPE_P (t)) |
8d08fdba | 5214 | { |
5566b478 MS |
5215 | if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t))) |
5216 | member = 2; | |
5217 | else | |
5218 | member = 1; | |
36a117a5 MM |
5219 | ctx = tsubst_aggr_type (DECL_CLASS_CONTEXT (t), args, t, |
5220 | /*entering_scope=*/1); | |
5566b478 MS |
5221 | } |
5222 | else | |
5223 | { | |
5224 | member = 0; | |
5225 | ctx = NULL_TREE; | |
5566b478 | 5226 | } |
eff71ab0 | 5227 | type = tsubst (type, args, in_decl); |
8d08fdba | 5228 | |
5566b478 MS |
5229 | /* We do NOT check for matching decls pushed separately at this |
5230 | point, as they may not represent instantiations of this | |
5231 | template, and in any case are considered separate under the | |
312e7d50 | 5232 | discrete model. Instead, see add_maybe_template. */ |
5566b478 MS |
5233 | |
5234 | r = copy_node (t); | |
5235 | copy_lang_decl (r); | |
e1467ff2 | 5236 | DECL_USE_TEMPLATE (r) = 0; |
5566b478 MS |
5237 | TREE_TYPE (r) = type; |
5238 | ||
5239 | DECL_CONTEXT (r) | |
36a117a5 | 5240 | = tsubst_aggr_type (DECL_CONTEXT (t), args, t, /*entering_scope=*/1); |
5566b478 MS |
5241 | DECL_CLASS_CONTEXT (r) = ctx; |
5242 | ||
5243 | if (member && !strncmp (OPERATOR_TYPENAME_FORMAT, | |
5244 | IDENTIFIER_POINTER (DECL_NAME (r)), | |
5245 | sizeof (OPERATOR_TYPENAME_FORMAT) - 1)) | |
5246 | { | |
5247 | /* Type-conversion operator. Reconstruct the name, in | |
5248 | case it's the name of one of the template's parameters. */ | |
5249 | DECL_NAME (r) = build_typename_overload (TREE_TYPE (type)); | |
5250 | } | |
5251 | ||
711734a9 JM |
5252 | DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args, t); |
5253 | DECL_MAIN_VARIANT (r) = r; | |
5254 | DECL_RESULT (r) = NULL_TREE; | |
5255 | DECL_INITIAL (r) = NULL_TREE; | |
5256 | ||
5257 | TREE_STATIC (r) = 0; | |
5258 | TREE_PUBLIC (r) = TREE_PUBLIC (t); | |
5259 | DECL_EXTERNAL (r) = 1; | |
5260 | DECL_INTERFACE_KNOWN (r) = 0; | |
5261 | DECL_DEFER_OUTPUT (r) = 0; | |
5262 | TREE_CHAIN (r) = NULL_TREE; | |
5263 | DECL_PENDING_INLINE_INFO (r) = 0; | |
5264 | TREE_USED (r) = 0; | |
5265 | ||
5266 | if (DECL_CONSTRUCTOR_P (r)) | |
5566b478 | 5267 | { |
711734a9 JM |
5268 | maybe_retrofit_in_chrg (r); |
5269 | grok_ctor_properties (ctx, r); | |
8d08fdba | 5270 | } |
711734a9 JM |
5271 | if (IDENTIFIER_OPNAME_P (DECL_NAME (r))) |
5272 | grok_op_properties (r, DECL_VIRTUAL_P (r), DECL_FRIEND_P (r)); | |
5273 | ||
36a117a5 MM |
5274 | /* Set up the DECL_TEMPLATE_INFO for R and compute its mangled |
5275 | name. There's no need to do this in the special friend | |
5276 | case mentioned above where GEN_TMPL is NULL. */ | |
5277 | if (gen_tmpl) | |
386b8a85 | 5278 | { |
36a117a5 MM |
5279 | DECL_TEMPLATE_INFO (r) |
5280 | = perm_tree_cons (gen_tmpl, argvec, NULL_TREE); | |
5281 | SET_DECL_IMPLICIT_INSTANTIATION (r); | |
5282 | register_specialization (r, gen_tmpl, argvec); | |
5283 | ||
5284 | /* Set the mangled name for R. */ | |
5285 | if (DECL_DESTRUCTOR_P (t)) | |
5286 | DECL_ASSEMBLER_NAME (r) = build_destructor_name (ctx); | |
5287 | else | |
386b8a85 | 5288 | { |
36a117a5 MM |
5289 | /* Instantiations of template functions must be mangled |
5290 | specially, in order to conform to 14.5.5.1 | |
5291 | [temp.over.link]. */ | |
5292 | tree tmpl = DECL_TI_TEMPLATE (t); | |
386b8a85 | 5293 | |
36a117a5 MM |
5294 | /* TMPL will be NULL if this is a specialization of a |
5295 | member function of a template class. */ | |
5296 | if (name_mangling_version < 1 | |
5297 | || tmpl == NULL_TREE | |
5298 | || (member && !is_member_template (tmpl) | |
5299 | && !DECL_TEMPLATE_INFO (tmpl))) | |
5300 | set_mangled_name_for_decl (r); | |
75650646 | 5301 | else |
36a117a5 | 5302 | set_mangled_name_for_template_decl (r); |
386b8a85 | 5303 | } |
36a117a5 MM |
5304 | |
5305 | DECL_RTL (r) = 0; | |
5306 | make_decl_rtl (r, NULL_PTR, 1); | |
5307 | ||
5308 | /* Like grokfndecl. If we don't do this, pushdecl will | |
5309 | mess up our TREE_CHAIN because it doesn't find a | |
5310 | previous decl. Sigh. */ | |
5311 | if (member | |
5312 | && (IDENTIFIER_GLOBAL_VALUE (DECL_ASSEMBLER_NAME (r)) | |
5313 | == NULL_TREE)) | |
5314 | SET_IDENTIFIER_GLOBAL_VALUE (DECL_ASSEMBLER_NAME (r), r); | |
386b8a85 | 5315 | } |
8d08fdba | 5316 | } |
ae58fa02 | 5317 | break; |
8d08fdba MS |
5318 | |
5319 | case PARM_DECL: | |
5320 | { | |
ae58fa02 | 5321 | r = copy_node (t); |
5566b478 | 5322 | TREE_TYPE (r) = type; |
db2767b6 MM |
5323 | if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX) |
5324 | DECL_INITIAL (r) = TREE_TYPE (r); | |
5325 | else | |
5326 | DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args, in_decl); | |
5327 | ||
5566b478 | 5328 | DECL_CONTEXT (r) = NULL_TREE; |
f83b0cb6 JM |
5329 | #ifdef PROMOTE_PROTOTYPES |
5330 | if ((TREE_CODE (type) == INTEGER_TYPE | |
5331 | || TREE_CODE (type) == ENUMERAL_TYPE) | |
5332 | && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)) | |
5333 | DECL_ARG_TYPE (r) = integer_type_node; | |
5334 | #endif | |
8d08fdba | 5335 | if (TREE_CHAIN (t)) |
f7d98d58 | 5336 | TREE_CHAIN (r) = tsubst (TREE_CHAIN (t), args, TREE_CHAIN (t)); |
8d08fdba | 5337 | } |
ae58fa02 | 5338 | break; |
8d08fdba | 5339 | |
5566b478 MS |
5340 | case FIELD_DECL: |
5341 | { | |
ae58fa02 | 5342 | r = copy_node (t); |
5566b478 MS |
5343 | TREE_TYPE (r) = type; |
5344 | copy_lang_decl (r); | |
5345 | #if 0 | |
f7d98d58 | 5346 | DECL_FIELD_CONTEXT (r) = tsubst (DECL_FIELD_CONTEXT (t), args, in_decl); |
5566b478 | 5347 | #endif |
f7d98d58 | 5348 | DECL_INITIAL (r) = tsubst_expr (DECL_INITIAL (t), args, in_decl); |
5566b478 | 5349 | TREE_CHAIN (r) = NULL_TREE; |
519ebd1e | 5350 | if (TREE_CODE (type) == VOID_TYPE) |
8251199e | 5351 | cp_error_at ("instantiation of `%D' as type void", r); |
5566b478 | 5352 | } |
ae58fa02 | 5353 | break; |
5566b478 MS |
5354 | |
5355 | case USING_DECL: | |
5356 | { | |
ae58fa02 | 5357 | r = copy_node (t); |
5566b478 | 5358 | DECL_INITIAL (r) |
f7d98d58 | 5359 | = tsubst_copy (DECL_INITIAL (t), args, in_decl); |
5566b478 | 5360 | TREE_CHAIN (r) = NULL_TREE; |
5566b478 | 5361 | } |
ae58fa02 | 5362 | break; |
5566b478 MS |
5363 | |
5364 | case VAR_DECL: | |
5365 | { | |
36a117a5 MM |
5366 | tree argvec; |
5367 | tree gen_tmpl; | |
5368 | tree spec; | |
5369 | tree tmpl; | |
5370 | tree ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, in_decl, | |
5371 | /*entering_scope=*/1); | |
5372 | ||
5373 | /* Nobody should be tsubst'ing into non-template variables. */ | |
5374 | my_friendly_assert (DECL_LANG_SPECIFIC (t) | |
5375 | && DECL_TEMPLATE_INFO (t) != NULL_TREE, 0); | |
5566b478 | 5376 | |
36a117a5 MM |
5377 | /* Check to see if we already have this specialization. */ |
5378 | tmpl = DECL_TI_TEMPLATE (t); | |
5379 | gen_tmpl = most_general_template (tmpl); | |
5380 | argvec = tsubst (DECL_TI_ARGS (t), args, in_decl); | |
5381 | spec = retrieve_specialization (gen_tmpl, argvec); | |
5382 | ||
5383 | if (spec) | |
ae58fa02 MM |
5384 | { |
5385 | r = spec; | |
5386 | break; | |
5387 | } | |
5566b478 MS |
5388 | |
5389 | r = copy_node (t); | |
5390 | TREE_TYPE (r) = type; | |
5391 | DECL_CONTEXT (r) = ctx; | |
5392 | if (TREE_STATIC (r)) | |
5393 | DECL_ASSEMBLER_NAME (r) | |
5394 | = build_static_name (DECL_CONTEXT (r), DECL_NAME (r)); | |
d11ad92e MS |
5395 | |
5396 | /* Don't try to expand the initializer until someone tries to use | |
5397 | this variable; otherwise we run into circular dependencies. */ | |
5398 | DECL_INITIAL (r) = NULL_TREE; | |
5566b478 MS |
5399 | DECL_RTL (r) = 0; |
5400 | DECL_SIZE (r) = 0; | |
36a117a5 MM |
5401 | copy_lang_decl (r); |
5402 | DECL_CLASS_CONTEXT (r) = DECL_CONTEXT (r); | |
5566b478 | 5403 | |
36a117a5 MM |
5404 | DECL_TEMPLATE_INFO (r) = perm_tree_cons (tmpl, argvec, NULL_TREE); |
5405 | SET_DECL_IMPLICIT_INSTANTIATION (r); | |
5406 | register_specialization (r, gen_tmpl, argvec); | |
5566b478 | 5407 | |
5566b478 | 5408 | TREE_CHAIN (r) = NULL_TREE; |
519ebd1e | 5409 | if (TREE_CODE (type) == VOID_TYPE) |
8251199e | 5410 | cp_error_at ("instantiation of `%D' as type void", r); |
5566b478 | 5411 | } |
ae58fa02 | 5412 | break; |
5566b478 MS |
5413 | |
5414 | case TYPE_DECL: | |
d2e5ee5c | 5415 | if (t == TYPE_NAME (TREE_TYPE (t))) |
ae58fa02 MM |
5416 | r = TYPE_NAME (type); |
5417 | else | |
5418 | { | |
5419 | r = copy_node (t); | |
5420 | TREE_TYPE (r) = type; | |
5421 | DECL_CONTEXT (r) = current_class_type; | |
5422 | TREE_CHAIN (r) = NULL_TREE; | |
5423 | } | |
5424 | break; | |
5425 | ||
5426 | default: | |
5427 | my_friendly_abort (0); | |
5428 | } | |
5429 | ||
5430 | /* Restore the file and line information. */ | |
5431 | lineno = saved_lineno; | |
5432 | input_filename = saved_filename; | |
5433 | ||
5434 | return r; | |
5435 | } | |
5436 | ||
cabc336a MM |
5437 | /* Substitue into the ARG_TYPES of a function type. */ |
5438 | ||
5439 | tree | |
5440 | tsubst_arg_types (arg_types, args, in_decl) | |
5441 | tree arg_types; | |
5442 | tree args; | |
5443 | tree in_decl; | |
5444 | { | |
5445 | tree remaining_arg_types; | |
cabc336a MM |
5446 | tree type; |
5447 | ||
5448 | if (!arg_types || arg_types == void_list_node) | |
5449 | return arg_types; | |
5450 | ||
5451 | remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types), | |
5452 | args, in_decl); | |
5453 | ||
5454 | /* We use TYPE_MAIN_VARIANT is because top-level qualifiers don't | |
5455 | matter on function types. */ | |
5456 | type = TYPE_MAIN_VARIANT (type_decays_to | |
5457 | (tsubst (TREE_VALUE (arg_types), | |
5458 | args, in_decl))); | |
5459 | ||
5460 | /* Note that we do not substitute into default arguments here. The | |
5461 | standard mandates that they be instantiated only when needed, | |
5462 | which is done in build_over_call. */ | |
5463 | return hash_tree_cons_simple (TREE_PURPOSE (arg_types), type, | |
5464 | remaining_arg_types); | |
5465 | ||
5466 | } | |
5467 | ||
ae58fa02 MM |
5468 | /* Take the tree structure T and replace template parameters used therein |
5469 | with the argument vector ARGS. IN_DECL is an associated decl for | |
5470 | diagnostics. | |
5471 | ||
5472 | tsubst is used for dealing with types, decls and the like; for | |
5473 | expressions, use tsubst_expr or tsubst_copy. */ | |
5474 | ||
5475 | tree | |
5476 | tsubst (t, args, in_decl) | |
5477 | tree t, args; | |
5478 | tree in_decl; | |
5479 | { | |
5480 | tree type; | |
5481 | ||
5482 | if (t == NULL_TREE || t == error_mark_node | |
5483 | || t == integer_type_node | |
5484 | || t == void_type_node | |
5485 | || t == char_type_node | |
5486 | || TREE_CODE (t) == NAMESPACE_DECL) | |
5487 | return t; | |
5488 | ||
5489 | if (TREE_CODE (t) == IDENTIFIER_NODE) | |
5490 | type = IDENTIFIER_TYPE_VALUE (t); | |
5491 | else | |
5492 | type = TREE_TYPE (t); | |
5493 | if (type == unknown_type_node) | |
5494 | my_friendly_abort (42); | |
5495 | ||
5496 | if (type && TREE_CODE (t) != FUNCTION_DECL | |
5497 | && TREE_CODE (t) != TYPENAME_TYPE | |
5498 | && TREE_CODE (t) != TEMPLATE_DECL | |
5499 | && TREE_CODE (t) != IDENTIFIER_NODE) | |
5500 | type = tsubst (type, args, in_decl); | |
5501 | ||
5502 | if (TREE_CODE_CLASS (TREE_CODE (t)) == 'd') | |
5503 | return tsubst_decl (t, args, type, in_decl); | |
5504 | ||
5505 | switch (TREE_CODE (t)) | |
5506 | { | |
5507 | case RECORD_TYPE: | |
5508 | case UNION_TYPE: | |
5509 | case ENUMERAL_TYPE: | |
5510 | return tsubst_aggr_type (t, args, in_decl, /*entering_scope=*/0); | |
5511 | ||
5512 | case ERROR_MARK: | |
5513 | case IDENTIFIER_NODE: | |
5514 | case OP_IDENTIFIER: | |
5515 | case VOID_TYPE: | |
5516 | case REAL_TYPE: | |
5517 | case COMPLEX_TYPE: | |
5518 | case BOOLEAN_TYPE: | |
5519 | case INTEGER_CST: | |
5520 | case REAL_CST: | |
5521 | case STRING_CST: | |
5522 | return t; | |
5523 | ||
5524 | case INTEGER_TYPE: | |
5525 | if (t == integer_type_node) | |
5526 | return t; | |
5527 | ||
5528 | if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST | |
5529 | && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST) | |
5530 | return t; | |
d2e5ee5c | 5531 | |
5566b478 | 5532 | { |
ae58fa02 MM |
5533 | tree max = TREE_OPERAND (TYPE_MAX_VALUE (t), 0); |
5534 | max = tsubst_expr (max, args, in_decl); | |
5535 | if (processing_template_decl) | |
5536 | { | |
5537 | tree itype = make_node (INTEGER_TYPE); | |
5538 | TYPE_MIN_VALUE (itype) = size_zero_node; | |
5539 | TYPE_MAX_VALUE (itype) = build_min (MINUS_EXPR, sizetype, max, | |
5540 | integer_one_node); | |
5541 | return itype; | |
5542 | } | |
5543 | ||
5544 | max = fold (build_binary_op (MINUS_EXPR, max, integer_one_node, 1)); | |
5545 | return build_index_2_type (size_zero_node, max); | |
5546 | } | |
5547 | ||
5548 | case TEMPLATE_TYPE_PARM: | |
5549 | case TEMPLATE_TEMPLATE_PARM: | |
5550 | case TEMPLATE_PARM_INDEX: | |
5551 | { | |
5552 | int idx; | |
5553 | int level; | |
5554 | int levels; | |
5555 | tree r = NULL_TREE; | |
5556 | ||
5557 | if (TREE_CODE (t) == TEMPLATE_TYPE_PARM | |
5558 | || TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM) | |
5559 | { | |
5560 | idx = TEMPLATE_TYPE_IDX (t); | |
5561 | level = TEMPLATE_TYPE_LEVEL (t); | |
5562 | } | |
5563 | else | |
5564 | { | |
5565 | idx = TEMPLATE_PARM_IDX (t); | |
5566 | level = TEMPLATE_PARM_LEVEL (t); | |
5567 | } | |
5568 | ||
5569 | if (TREE_VEC_LENGTH (args) > 0) | |
5570 | { | |
5571 | tree arg = NULL_TREE; | |
5572 | ||
5573 | levels = TMPL_ARGS_DEPTH (args); | |
5574 | if (level <= levels) | |
5575 | arg = TMPL_ARG (args, level, idx); | |
5576 | ||
8b5b8b7c MM |
5577 | if (arg == error_mark_node) |
5578 | return error_mark_node; | |
5579 | else if (arg != NULL_TREE) | |
ae58fa02 MM |
5580 | { |
5581 | if (TREE_CODE (t) == TEMPLATE_TYPE_PARM) | |
5582 | { | |
5583 | my_friendly_assert (TREE_CODE_CLASS (TREE_CODE (arg)) | |
5584 | == 't', 0); | |
91063b51 MM |
5585 | return cp_build_qualified_type |
5586 | (arg, CP_TYPE_QUALS (arg) | CP_TYPE_QUALS (t)); | |
ae58fa02 MM |
5587 | } |
5588 | else if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM) | |
5589 | { | |
5590 | if (CLASSTYPE_TEMPLATE_INFO (t)) | |
5591 | { | |
5592 | /* We are processing a type constructed from | |
5593 | a template template parameter */ | |
5594 | tree argvec = tsubst (CLASSTYPE_TI_ARGS (t), | |
5595 | args, in_decl); | |
5596 | tree r; | |
5597 | ||
5598 | /* We can get a TEMPLATE_TEMPLATE_PARM here when | |
5599 | we are resolving nested-types in the signature of | |
5600 | a member function templates. | |
5601 | Otherwise ARG is a TEMPLATE_DECL and is the real | |
5602 | template to be instantiated. */ | |
5603 | if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM) | |
5604 | arg = TYPE_NAME (arg); | |
5605 | ||
5606 | r = lookup_template_class (DECL_NAME (arg), | |
5607 | argvec, in_decl, | |
5608 | DECL_CONTEXT (arg), | |
5609 | /*entering_scope=*/0); | |
91063b51 | 5610 | return cp_build_qualified_type (r, TYPE_QUALS (t)); |
ae58fa02 MM |
5611 | } |
5612 | else | |
5613 | /* We are processing a template argument list. */ | |
5614 | return arg; | |
5615 | } | |
5616 | else | |
5617 | return arg; | |
5618 | } | |
5619 | } | |
51924768 JM |
5620 | else |
5621 | my_friendly_abort (981018); | |
ae58fa02 MM |
5622 | |
5623 | if (level == 1) | |
5624 | /* This can happen during the attempted tsubst'ing in | |
5625 | unify. This means that we don't yet have any information | |
5626 | about the template parameter in question. */ | |
5627 | return t; | |
5628 | ||
5629 | /* If we get here, we must have been looking at a parm for a | |
5630 | more deeply nested template. Make a new version of this | |
5631 | template parameter, but with a lower level. */ | |
5632 | switch (TREE_CODE (t)) | |
5633 | { | |
5634 | case TEMPLATE_TYPE_PARM: | |
5635 | case TEMPLATE_TEMPLATE_PARM: | |
5636 | r = copy_node (t); | |
5637 | TEMPLATE_TYPE_PARM_INDEX (r) | |
5638 | = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t), | |
5639 | r, levels); | |
5640 | TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r); | |
5641 | TYPE_MAIN_VARIANT (r) = r; | |
5642 | TYPE_POINTER_TO (r) = NULL_TREE; | |
5643 | TYPE_REFERENCE_TO (r) = NULL_TREE; | |
5644 | ||
5645 | if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM | |
5646 | && CLASSTYPE_TEMPLATE_INFO (t)) | |
5647 | { | |
5648 | tree argvec = tsubst (CLASSTYPE_TI_ARGS (t), args, in_decl); | |
5649 | CLASSTYPE_TEMPLATE_INFO (r) | |
5650 | = perm_tree_cons (TYPE_NAME (t), argvec, NULL_TREE); | |
5651 | } | |
5652 | break; | |
5653 | ||
5654 | case TEMPLATE_PARM_INDEX: | |
5655 | r = reduce_template_parm_level (t, type, levels); | |
5656 | break; | |
5657 | ||
5658 | default: | |
5659 | my_friendly_abort (0); | |
5660 | } | |
5661 | ||
5566b478 | 5662 | return r; |
ae58fa02 | 5663 | } |
5566b478 | 5664 | |
8d08fdba MS |
5665 | case TREE_LIST: |
5666 | { | |
5667 | tree purpose, value, chain, result; | |
5668 | int via_public, via_virtual, via_protected; | |
5669 | ||
5670 | if (t == void_list_node) | |
5671 | return t; | |
5672 | ||
5673 | via_public = TREE_VIA_PUBLIC (t); | |
5674 | via_protected = TREE_VIA_PROTECTED (t); | |
5675 | via_virtual = TREE_VIA_VIRTUAL (t); | |
5676 | ||
5677 | purpose = TREE_PURPOSE (t); | |
5678 | if (purpose) | |
f7d98d58 | 5679 | purpose = tsubst (purpose, args, in_decl); |
8d08fdba MS |
5680 | value = TREE_VALUE (t); |
5681 | if (value) | |
f7d98d58 | 5682 | value = tsubst (value, args, in_decl); |
8d08fdba MS |
5683 | chain = TREE_CHAIN (t); |
5684 | if (chain && chain != void_type_node) | |
f7d98d58 | 5685 | chain = tsubst (chain, args, in_decl); |
8d08fdba MS |
5686 | if (purpose == TREE_PURPOSE (t) |
5687 | && value == TREE_VALUE (t) | |
5688 | && chain == TREE_CHAIN (t)) | |
5689 | return t; | |
5690 | result = hash_tree_cons (via_public, via_virtual, via_protected, | |
5691 | purpose, value, chain); | |
5692 | TREE_PARMLIST (result) = TREE_PARMLIST (t); | |
5693 | return result; | |
5694 | } | |
5695 | case TREE_VEC: | |
5566b478 MS |
5696 | if (type != NULL_TREE) |
5697 | { | |
1ceaad38 JM |
5698 | /* A binfo node. We always need to make a copy, of the node |
5699 | itself and of its BINFO_BASETYPES. */ | |
85b71cf2 | 5700 | |
5566b478 MS |
5701 | t = copy_node (t); |
5702 | ||
c7449f2b JM |
5703 | /* Make sure type isn't a typedef copy. */ |
5704 | type = BINFO_TYPE (TYPE_BINFO (type)); | |
5705 | ||
5566b478 | 5706 | TREE_TYPE (t) = complete_type (type); |
6633d636 MS |
5707 | if (IS_AGGR_TYPE (type)) |
5708 | { | |
5709 | BINFO_VTABLE (t) = TYPE_BINFO_VTABLE (type); | |
5710 | BINFO_VIRTUALS (t) = TYPE_BINFO_VIRTUALS (type); | |
5711 | if (TYPE_BINFO_BASETYPES (type) != NULL_TREE) | |
5712 | BINFO_BASETYPES (t) = copy_node (TYPE_BINFO_BASETYPES (type)); | |
5713 | } | |
5566b478 MS |
5714 | return t; |
5715 | } | |
85b71cf2 JM |
5716 | |
5717 | /* Otherwise, a vector of template arguments. */ | |
830bfa74 | 5718 | return tsubst_template_arg_vector (t, args); |
8d08fdba | 5719 | |
8d08fdba MS |
5720 | case POINTER_TYPE: |
5721 | case REFERENCE_TYPE: | |
5722 | { | |
5723 | tree r; | |
5724 | enum tree_code code; | |
79a7c7fa | 5725 | |
8d08fdba MS |
5726 | if (type == TREE_TYPE (t)) |
5727 | return t; | |
5728 | ||
5729 | code = TREE_CODE (t); | |
79a7c7fa JM |
5730 | if (TREE_CODE (type) == REFERENCE_TYPE) |
5731 | { | |
5732 | static int last_line = 0; | |
5733 | static char* last_file = 0; | |
5734 | ||
5735 | /* We keep track of the last time we issued this error | |
5736 | message to avoid spewing a ton of messages during a | |
5737 | single bad template instantiation. */ | |
5738 | if (last_line != lineno || | |
5739 | last_file != input_filename) | |
5740 | { | |
8251199e | 5741 | cp_error ("cannot form type %s to reference type %T during template instantiation", |
79a7c7fa JM |
5742 | (code == POINTER_TYPE) ? "pointer" : "reference", |
5743 | type); | |
5744 | last_line = lineno; | |
5745 | last_file = input_filename; | |
5746 | } | |
5747 | ||
5748 | /* Use the underlying type in an attempt at error | |
5749 | recovery; maybe the user meant vector<int> and wrote | |
5750 | vector<int&>, or some such. */ | |
5751 | if (code == REFERENCE_TYPE) | |
5752 | r = type; | |
5753 | else | |
5754 | r = build_pointer_type (TREE_TYPE (type)); | |
5755 | } | |
5756 | else if (code == POINTER_TYPE) | |
8d08fdba MS |
5757 | r = build_pointer_type (type); |
5758 | else | |
5759 | r = build_reference_type (type); | |
91063b51 | 5760 | r = cp_build_qualified_type (r, TYPE_QUALS (t)); |
79a7c7fa | 5761 | |
8d08fdba MS |
5762 | /* Will this ever be needed for TYPE_..._TO values? */ |
5763 | layout_type (r); | |
5764 | return r; | |
5765 | } | |
a4443a08 MS |
5766 | case OFFSET_TYPE: |
5767 | return build_offset_type | |
f7d98d58 | 5768 | (tsubst (TYPE_OFFSET_BASETYPE (t), args, in_decl), type); |
8d08fdba MS |
5769 | case FUNCTION_TYPE: |
5770 | case METHOD_TYPE: | |
5771 | { | |
cabc336a MM |
5772 | tree arg_types; |
5773 | tree raises; | |
c11b6f21 | 5774 | tree fntype; |
8d08fdba | 5775 | |
cabc336a MM |
5776 | /* The TYPE_CONTEXT is not used for function/method types. */ |
5777 | my_friendly_assert (TYPE_CONTEXT (t) == NULL_TREE, 0); | |
5778 | ||
5779 | /* Substitue the argument types. */ | |
5780 | arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, in_decl); | |
8d08fdba MS |
5781 | |
5782 | /* Construct a new type node and return it. */ | |
cabc336a MM |
5783 | if (TREE_CODE (t) == FUNCTION_TYPE) |
5784 | fntype = build_function_type (type, arg_types); | |
5785 | else | |
5786 | fntype | |
5787 | = build_cplus_method_type (TREE_TYPE (TREE_VALUE (arg_types)), | |
5788 | type, | |
5789 | TREE_CHAIN (arg_types)); | |
91063b51 | 5790 | fntype = build_qualified_type (fntype, TYPE_QUALS (t)); |
cabc336a MM |
5791 | |
5792 | /* Substitue the exception specification. */ | |
5793 | raises = TYPE_RAISES_EXCEPTIONS (t); | |
c11b6f21 MS |
5794 | if (raises) |
5795 | { | |
f7d98d58 | 5796 | raises = tsubst (raises, args, in_decl); |
c11b6f21 MS |
5797 | fntype = build_exception_variant (fntype, raises); |
5798 | } | |
5799 | return fntype; | |
8d08fdba MS |
5800 | } |
5801 | case ARRAY_TYPE: | |
5802 | { | |
f7d98d58 | 5803 | tree domain = tsubst (TYPE_DOMAIN (t), args, in_decl); |
8d08fdba MS |
5804 | tree r; |
5805 | if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t)) | |
5806 | return t; | |
5807 | r = build_cplus_array_type (type, domain); | |
5808 | return r; | |
5809 | } | |
5810 | ||
8d08fdba | 5811 | case PLUS_EXPR: |
5566b478 | 5812 | case MINUS_EXPR: |
8d08fdba | 5813 | return fold (build (TREE_CODE (t), TREE_TYPE (t), |
f7d98d58 JM |
5814 | tsubst (TREE_OPERAND (t, 0), args, in_decl), |
5815 | tsubst (TREE_OPERAND (t, 1), args, in_decl))); | |
8d08fdba MS |
5816 | |
5817 | case NEGATE_EXPR: | |
5818 | case NOP_EXPR: | |
5819 | return fold (build1 (TREE_CODE (t), TREE_TYPE (t), | |
f7d98d58 | 5820 | tsubst (TREE_OPERAND (t, 0), args, in_decl))); |
8d08fdba | 5821 | |
5566b478 MS |
5822 | case TYPENAME_TYPE: |
5823 | { | |
36a117a5 MM |
5824 | tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, in_decl, |
5825 | /*entering_scope=*/1); | |
b2b7d40a | 5826 | tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args, in_decl); |
ae58fa02 MM |
5827 | |
5828 | /* Normally, make_typename_type does not require that the CTX | |
5829 | have complete type in order to allow things like: | |
5830 | ||
5831 | template <class T> struct S { typename S<T>::X Y; }; | |
5832 | ||
5833 | But, such constructs have already been resolved by this | |
5834 | point, so here CTX really should have complete type, unless | |
5835 | it's a partial instantiation. */ | |
5836 | if (!uses_template_parms (ctx) | |
5837 | && !complete_type_or_else (ctx)) | |
5838 | return error_mark_node; | |
5839 | ||
b2b7d40a | 5840 | f = make_typename_type (ctx, f); |
91063b51 MM |
5841 | return cp_build_qualified_type (f, |
5842 | CP_TYPE_QUALS (f) | |
5843 | | CP_TYPE_QUALS (t)); | |
5566b478 MS |
5844 | } |
5845 | ||
5846 | case INDIRECT_REF: | |
5847 | return make_pointer_declarator | |
f7d98d58 | 5848 | (type, tsubst (TREE_OPERAND (t, 0), args, in_decl)); |
5566b478 MS |
5849 | |
5850 | case ADDR_EXPR: | |
5851 | return make_reference_declarator | |
f7d98d58 | 5852 | (type, tsubst (TREE_OPERAND (t, 0), args, in_decl)); |
5566b478 MS |
5853 | |
5854 | case ARRAY_REF: | |
5855 | return build_parse_node | |
f7d98d58 JM |
5856 | (ARRAY_REF, tsubst (TREE_OPERAND (t, 0), args, in_decl), |
5857 | tsubst_expr (TREE_OPERAND (t, 1), args, in_decl)); | |
5566b478 MS |
5858 | |
5859 | case CALL_EXPR: | |
c11b6f21 | 5860 | return make_call_declarator |
f7d98d58 JM |
5861 | (tsubst (TREE_OPERAND (t, 0), args, in_decl), |
5862 | tsubst (TREE_OPERAND (t, 1), args, in_decl), | |
c11b6f21 | 5863 | TREE_OPERAND (t, 2), |
f7d98d58 | 5864 | tsubst (TREE_TYPE (t), args, in_decl)); |
5566b478 | 5865 | |
fc378698 MS |
5866 | case SCOPE_REF: |
5867 | return build_parse_node | |
f7d98d58 JM |
5868 | (TREE_CODE (t), tsubst (TREE_OPERAND (t, 0), args, in_decl), |
5869 | tsubst (TREE_OPERAND (t, 1), args, in_decl)); | |
fc378698 | 5870 | |
b894fc05 JM |
5871 | case TYPEOF_TYPE: |
5872 | return TREE_TYPE (tsubst_expr (TYPE_FIELDS (t), args, in_decl)); | |
5873 | ||
8d08fdba | 5874 | default: |
5566b478 | 5875 | sorry ("use of `%s' in template", |
8d08fdba MS |
5876 | tree_code_name [(int) TREE_CODE (t)]); |
5877 | return error_mark_node; | |
5878 | } | |
5879 | } | |
5880 | ||
5566b478 MS |
5881 | void |
5882 | do_pushlevel () | |
5883 | { | |
5884 | emit_line_note (input_filename, lineno); | |
5885 | pushlevel (0); | |
5886 | clear_last_expr (); | |
5887 | push_momentary (); | |
5888 | expand_start_bindings (0); | |
5889 | } | |
5890 | ||
8d08fdba | 5891 | tree |
5566b478 | 5892 | do_poplevel () |
8d08fdba | 5893 | { |
5566b478 | 5894 | tree t; |
a703fb38 | 5895 | int saved_warn_unused = 0; |
8d08fdba | 5896 | |
85b71cf2 JM |
5897 | if (processing_template_decl) |
5898 | { | |
5899 | saved_warn_unused = warn_unused; | |
5900 | warn_unused = 0; | |
5901 | } | |
8baa713c | 5902 | expand_end_bindings (getdecls (), kept_level_p (), 0); |
85b71cf2 JM |
5903 | if (processing_template_decl) |
5904 | warn_unused = saved_warn_unused; | |
5566b478 MS |
5905 | t = poplevel (kept_level_p (), 1, 0); |
5906 | pop_momentary (); | |
5907 | return t; | |
5908 | } | |
8d08fdba | 5909 | |
00d3396f JM |
5910 | /* Like tsubst, but deals with expressions. This function just replaces |
5911 | template parms; to finish processing the resultant expression, use | |
5912 | tsubst_expr. */ | |
5913 | ||
5566b478 | 5914 | tree |
f7d98d58 | 5915 | tsubst_copy (t, args, in_decl) |
98c1c668 | 5916 | tree t, args; |
5566b478 MS |
5917 | tree in_decl; |
5918 | { | |
5919 | enum tree_code code; | |
8d08fdba | 5920 | |
5566b478 MS |
5921 | if (t == NULL_TREE || t == error_mark_node) |
5922 | return t; | |
5923 | ||
5924 | code = TREE_CODE (t); | |
b7484fbe | 5925 | |
5566b478 MS |
5926 | switch (code) |
5927 | { | |
5928 | case PARM_DECL: | |
a759e627 | 5929 | return do_identifier (DECL_NAME (t), 0, NULL_TREE); |
5566b478 MS |
5930 | |
5931 | case CONST_DECL: | |
ed44da02 MM |
5932 | { |
5933 | tree enum_type; | |
5934 | tree v; | |
5935 | ||
5936 | if (!DECL_CONTEXT (t)) | |
5937 | /* This is a global enumeration constant. */ | |
5938 | return t; | |
5939 | ||
5940 | /* Unfortunately, we cannot just call lookup_name here. | |
5941 | Consider: | |
5942 | ||
5943 | template <int I> int f() { | |
5944 | enum E { a = I }; | |
5945 | struct S { void g() { E e = a; } }; | |
5946 | }; | |
5947 | ||
5948 | When we instantiate f<7>::S::g(), say, lookup_name is not | |
5949 | clever enough to find f<7>::a. */ | |
5950 | enum_type | |
5951 | = tsubst_aggr_type (TREE_TYPE (t), args, in_decl, | |
5952 | /*entering_scope=*/0); | |
5953 | ||
5954 | for (v = TYPE_VALUES (enum_type); | |
5955 | v != NULL_TREE; | |
5956 | v = TREE_CHAIN (v)) | |
5957 | if (TREE_PURPOSE (v) == DECL_NAME (t)) | |
5958 | return TREE_VALUE (v); | |
5959 | ||
5960 | /* We didn't find the name. That should never happen; if | |
5961 | name-lookup found it during preliminary parsing, we | |
5962 | should find it again here during instantiation. */ | |
5963 | my_friendly_abort (0); | |
5964 | } | |
db04386f | 5965 | return t; |
ed44da02 | 5966 | |
5566b478 MS |
5967 | case FIELD_DECL: |
5968 | if (DECL_CONTEXT (t)) | |
5969 | { | |
0978790f | 5970 | tree ctx; |
0978790f | 5971 | |
36a117a5 MM |
5972 | ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, in_decl, |
5973 | /*entering_scope=*/1); | |
0978790f | 5974 | if (ctx != DECL_CONTEXT (t)) |
5566b478 MS |
5975 | return lookup_field (ctx, DECL_NAME (t), 0, 0); |
5976 | } | |
5977 | return t; | |
5978 | ||
5979 | case VAR_DECL: | |
5980 | case FUNCTION_DECL: | |
5981 | if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)) | |
f7d98d58 | 5982 | t = tsubst (t, args, in_decl); |
5566b478 MS |
5983 | mark_used (t); |
5984 | return t; | |
5985 | ||
98c1c668 JM |
5986 | case TEMPLATE_DECL: |
5987 | if (is_member_template (t)) | |
f7d98d58 | 5988 | return tsubst (t, args, in_decl); |
98c1c668 JM |
5989 | else |
5990 | return t; | |
5991 | ||
672476cb MM |
5992 | case LOOKUP_EXPR: |
5993 | { | |
5994 | /* We must tsbust into a LOOKUP_EXPR in case the names to | |
5995 | which it refers is a conversion operator; in that case the | |
5996 | name will change. We avoid making unnecessary copies, | |
5997 | however. */ | |
5998 | ||
5999 | tree id = tsubst_copy (TREE_OPERAND (t, 0), args, in_decl); | |
6000 | ||
6001 | if (id != TREE_OPERAND (t, 0)) | |
6002 | { | |
6003 | tree r = build_nt (LOOKUP_EXPR, id); | |
6004 | LOOKUP_EXPR_GLOBAL (r) = LOOKUP_EXPR_GLOBAL (t); | |
6005 | t = r; | |
6006 | } | |
6007 | ||
6008 | return t; | |
6009 | } | |
6010 | ||
5566b478 MS |
6011 | case CAST_EXPR: |
6012 | case REINTERPRET_CAST_EXPR: | |
e92cc029 MS |
6013 | case CONST_CAST_EXPR: |
6014 | case STATIC_CAST_EXPR: | |
6015 | case DYNAMIC_CAST_EXPR: | |
51924768 | 6016 | case NOP_EXPR: |
5566b478 | 6017 | return build1 |
f7d98d58 JM |
6018 | (code, tsubst (TREE_TYPE (t), args, in_decl), |
6019 | tsubst_copy (TREE_OPERAND (t, 0), args, in_decl)); | |
5566b478 MS |
6020 | |
6021 | case INDIRECT_REF: | |
6022 | case PREDECREMENT_EXPR: | |
6023 | case PREINCREMENT_EXPR: | |
6024 | case POSTDECREMENT_EXPR: | |
6025 | case POSTINCREMENT_EXPR: | |
6026 | case NEGATE_EXPR: | |
6027 | case TRUTH_NOT_EXPR: | |
b87692e5 | 6028 | case BIT_NOT_EXPR: |
5566b478 MS |
6029 | case ADDR_EXPR: |
6030 | case CONVERT_EXPR: /* Unary + */ | |
6031 | case SIZEOF_EXPR: | |
abff8e06 | 6032 | case ALIGNOF_EXPR: |
5566b478 | 6033 | case ARROW_EXPR: |
fc378698 | 6034 | case THROW_EXPR: |
5156628f | 6035 | case TYPEID_EXPR: |
5566b478 MS |
6036 | return build1 |
6037 | (code, NULL_TREE, | |
f7d98d58 | 6038 | tsubst_copy (TREE_OPERAND (t, 0), args, in_decl)); |
5566b478 MS |
6039 | |
6040 | case PLUS_EXPR: | |
6041 | case MINUS_EXPR: | |
6042 | case MULT_EXPR: | |
6043 | case TRUNC_DIV_EXPR: | |
6044 | case CEIL_DIV_EXPR: | |
6045 | case FLOOR_DIV_EXPR: | |
6046 | case ROUND_DIV_EXPR: | |
6047 | case EXACT_DIV_EXPR: | |
6048 | case BIT_AND_EXPR: | |
6049 | case BIT_ANDTC_EXPR: | |
6050 | case BIT_IOR_EXPR: | |
6051 | case BIT_XOR_EXPR: | |
6052 | case TRUNC_MOD_EXPR: | |
6053 | case FLOOR_MOD_EXPR: | |
6054 | case TRUTH_ANDIF_EXPR: | |
6055 | case TRUTH_ORIF_EXPR: | |
6056 | case TRUTH_AND_EXPR: | |
6057 | case TRUTH_OR_EXPR: | |
6058 | case RSHIFT_EXPR: | |
6059 | case LSHIFT_EXPR: | |
6060 | case RROTATE_EXPR: | |
6061 | case LROTATE_EXPR: | |
6062 | case EQ_EXPR: | |
6063 | case NE_EXPR: | |
6064 | case MAX_EXPR: | |
6065 | case MIN_EXPR: | |
6066 | case LE_EXPR: | |
6067 | case GE_EXPR: | |
6068 | case LT_EXPR: | |
6069 | case GT_EXPR: | |
6070 | case COMPONENT_REF: | |
6071 | case ARRAY_REF: | |
6072 | case COMPOUND_EXPR: | |
6073 | case SCOPE_REF: | |
6074 | case DOTSTAR_EXPR: | |
6075 | case MEMBER_REF: | |
6076 | return build_nt | |
f7d98d58 JM |
6077 | (code, tsubst_copy (TREE_OPERAND (t, 0), args, in_decl), |
6078 | tsubst_copy (TREE_OPERAND (t, 1), args, in_decl)); | |
5566b478 MS |
6079 | |
6080 | case CALL_EXPR: | |
6081 | { | |
6082 | tree fn = TREE_OPERAND (t, 0); | |
f84b4be9 | 6083 | if (is_overloaded_fn (fn)) |
f7d98d58 | 6084 | fn = tsubst_copy (get_first_fn (fn), args, in_decl); |
5566b478 | 6085 | else |
f84b4be9 | 6086 | /* Sometimes FN is a LOOKUP_EXPR. */ |
f7d98d58 | 6087 | fn = tsubst_copy (fn, args, in_decl); |
5566b478 | 6088 | return build_nt |
f7d98d58 | 6089 | (code, fn, tsubst_copy (TREE_OPERAND (t, 1), args, in_decl), |
5566b478 MS |
6090 | NULL_TREE); |
6091 | } | |
6092 | ||
6093 | case METHOD_CALL_EXPR: | |
6094 | { | |
6095 | tree name = TREE_OPERAND (t, 0); | |
6096 | if (TREE_CODE (name) == BIT_NOT_EXPR) | |
6097 | { | |
f7d98d58 | 6098 | name = tsubst_copy (TREE_OPERAND (name, 0), args, in_decl); |
7bae46f4 | 6099 | name = build1 (BIT_NOT_EXPR, NULL_TREE, name); |
5566b478 MS |
6100 | } |
6101 | else if (TREE_CODE (name) == SCOPE_REF | |
6102 | && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR) | |
6103 | { | |
f7d98d58 | 6104 | tree base = tsubst_copy (TREE_OPERAND (name, 0), args, in_decl); |
5566b478 | 6105 | name = TREE_OPERAND (name, 1); |
f7d98d58 | 6106 | name = tsubst_copy (TREE_OPERAND (name, 0), args, in_decl); |
11686454 | 6107 | name = build1 (BIT_NOT_EXPR, NULL_TREE, name); |
5566b478 MS |
6108 | name = build_nt (SCOPE_REF, base, name); |
6109 | } | |
6110 | else | |
f7d98d58 | 6111 | name = tsubst_copy (TREE_OPERAND (t, 0), args, in_decl); |
5566b478 | 6112 | return build_nt |
f7d98d58 JM |
6113 | (code, name, tsubst_copy (TREE_OPERAND (t, 1), args, in_decl), |
6114 | tsubst_copy (TREE_OPERAND (t, 2), args, in_decl), | |
5566b478 MS |
6115 | NULL_TREE); |
6116 | } | |
6117 | ||
67da3287 | 6118 | case BIND_EXPR: |
5566b478 MS |
6119 | case COND_EXPR: |
6120 | case MODOP_EXPR: | |
67da3287 MM |
6121 | { |
6122 | tree r = build_nt | |
f7d98d58 JM |
6123 | (code, tsubst_copy (TREE_OPERAND (t, 0), args, in_decl), |
6124 | tsubst_copy (TREE_OPERAND (t, 1), args, in_decl), | |
6125 | tsubst_copy (TREE_OPERAND (t, 2), args, in_decl)); | |
67da3287 MM |
6126 | |
6127 | if (code == BIND_EXPR && !processing_template_decl) | |
6128 | { | |
6129 | /* This processing should really occur in tsubst_expr, | |
6130 | However, tsubst_expr does not recurse into expressions, | |
6131 | since it assumes that there aren't any statements | |
6132 | inside them. Instead, it simply calls | |
6133 | build_expr_from_tree. So, we need to expand the | |
6134 | BIND_EXPR here. */ | |
b4c4a9ec | 6135 | tree rtl_expr = begin_stmt_expr (); |
544c188d | 6136 | tree block = tsubst_expr (TREE_OPERAND (r, 1), args, in_decl); |
b4c4a9ec | 6137 | r = finish_stmt_expr (rtl_expr, block); |
67da3287 MM |
6138 | } |
6139 | ||
6140 | return r; | |
6141 | } | |
5566b478 MS |
6142 | |
6143 | case NEW_EXPR: | |
6144 | { | |
6145 | tree r = build_nt | |
f7d98d58 JM |
6146 | (code, tsubst_copy (TREE_OPERAND (t, 0), args, in_decl), |
6147 | tsubst_copy (TREE_OPERAND (t, 1), args, in_decl), | |
6148 | tsubst_copy (TREE_OPERAND (t, 2), args, in_decl)); | |
5566b478 MS |
6149 | NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t); |
6150 | return r; | |
6151 | } | |
6152 | ||
6153 | case DELETE_EXPR: | |
6154 | { | |
6155 | tree r = build_nt | |
f7d98d58 JM |
6156 | (code, tsubst_copy (TREE_OPERAND (t, 0), args, in_decl), |
6157 | tsubst_copy (TREE_OPERAND (t, 1), args, in_decl)); | |
5566b478 MS |
6158 | DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t); |
6159 | DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t); | |
6160 | return r; | |
6161 | } | |
6162 | ||
386b8a85 JM |
6163 | case TEMPLATE_ID_EXPR: |
6164 | { | |
00d3396f | 6165 | /* Substituted template arguments */ |
f7d98d58 | 6166 | tree targs = tsubst_copy (TREE_OPERAND (t, 1), args, in_decl); |
00d3396f JM |
6167 | tree chain; |
6168 | for (chain = targs; chain; chain = TREE_CHAIN (chain)) | |
6169 | TREE_VALUE (chain) = maybe_fold_nontype_arg (TREE_VALUE (chain)); | |
6170 | ||
6171 | return lookup_template_function | |
f7d98d58 | 6172 | (tsubst_copy (TREE_OPERAND (t, 0), args, in_decl), targs); |
386b8a85 JM |
6173 | } |
6174 | ||
5566b478 MS |
6175 | case TREE_LIST: |
6176 | { | |
6177 | tree purpose, value, chain; | |
6178 | ||
6179 | if (t == void_list_node) | |
6180 | return t; | |
6181 | ||
6182 | purpose = TREE_PURPOSE (t); | |
6183 | if (purpose) | |
f7d98d58 | 6184 | purpose = tsubst_copy (purpose, args, in_decl); |
5566b478 MS |
6185 | value = TREE_VALUE (t); |
6186 | if (value) | |
f7d98d58 | 6187 | value = tsubst_copy (value, args, in_decl); |
5566b478 MS |
6188 | chain = TREE_CHAIN (t); |
6189 | if (chain && chain != void_type_node) | |
f7d98d58 | 6190 | chain = tsubst_copy (chain, args, in_decl); |
5566b478 MS |
6191 | if (purpose == TREE_PURPOSE (t) |
6192 | && value == TREE_VALUE (t) | |
6193 | && chain == TREE_CHAIN (t)) | |
6194 | return t; | |
6195 | return tree_cons (purpose, value, chain); | |
6196 | } | |
6197 | ||
6198 | case RECORD_TYPE: | |
6199 | case UNION_TYPE: | |
6200 | case ENUMERAL_TYPE: | |
6201 | case INTEGER_TYPE: | |
6202 | case TEMPLATE_TYPE_PARM: | |
73b0fce8 | 6203 | case TEMPLATE_TEMPLATE_PARM: |
f84b4be9 | 6204 | case TEMPLATE_PARM_INDEX: |
5566b478 MS |
6205 | case POINTER_TYPE: |
6206 | case REFERENCE_TYPE: | |
6207 | case OFFSET_TYPE: | |
6208 | case FUNCTION_TYPE: | |
6209 | case METHOD_TYPE: | |
6210 | case ARRAY_TYPE: | |
6211 | case TYPENAME_TYPE: | |
f84b4be9 | 6212 | case TYPE_DECL: |
f7d98d58 | 6213 | return tsubst (t, args, in_decl); |
5566b478 | 6214 | |
e92cc029 | 6215 | case IDENTIFIER_NODE: |
8e710788 BK |
6216 | if (IDENTIFIER_TYPENAME_P (t) |
6217 | /* Make sure it's not just a variable named `__opr', for instance, | |
6218 | which can occur in some existing code. */ | |
6219 | && TREE_TYPE (t)) | |
e92cc029 | 6220 | return build_typename_overload |
f7d98d58 | 6221 | (tsubst (TREE_TYPE (t), args, in_decl)); |
e92cc029 MS |
6222 | else |
6223 | return t; | |
6224 | ||
5156628f MS |
6225 | case CONSTRUCTOR: |
6226 | return build | |
f7d98d58 JM |
6227 | (CONSTRUCTOR, tsubst (TREE_TYPE (t), args, in_decl), NULL_TREE, |
6228 | tsubst_copy (CONSTRUCTOR_ELTS (t), args, in_decl)); | |
5156628f | 6229 | |
5566b478 MS |
6230 | default: |
6231 | return t; | |
6232 | } | |
6233 | } | |
6234 | ||
00d3396f JM |
6235 | /* Like tsubst_copy, but also does semantic processing and RTL expansion. */ |
6236 | ||
5566b478 | 6237 | tree |
f7d98d58 | 6238 | tsubst_expr (t, args, in_decl) |
98c1c668 | 6239 | tree t, args; |
5566b478 MS |
6240 | tree in_decl; |
6241 | { | |
6242 | if (t == NULL_TREE || t == error_mark_node) | |
6243 | return t; | |
6244 | ||
5156628f | 6245 | if (processing_template_decl) |
f7d98d58 | 6246 | return tsubst_copy (t, args, in_decl); |
5566b478 MS |
6247 | |
6248 | switch (TREE_CODE (t)) | |
8d08fdba | 6249 | { |
5566b478 MS |
6250 | case RETURN_STMT: |
6251 | lineno = TREE_COMPLEXITY (t); | |
ad321293 MM |
6252 | finish_return_stmt (tsubst_expr (RETURN_EXPR (t), |
6253 | args, in_decl)); | |
5566b478 MS |
6254 | break; |
6255 | ||
6256 | case EXPR_STMT: | |
6257 | lineno = TREE_COMPLEXITY (t); | |
ad321293 MM |
6258 | finish_expr_stmt (tsubst_expr (EXPR_STMT_EXPR (t), |
6259 | args, in_decl)); | |
5566b478 MS |
6260 | break; |
6261 | ||
6262 | case DECL_STMT: | |
6263 | { | |
6264 | int i = suspend_momentary (); | |
67d743fe | 6265 | tree dcl, init; |
5566b478 MS |
6266 | |
6267 | lineno = TREE_COMPLEXITY (t); | |
6268 | emit_line_note (input_filename, lineno); | |
6269 | dcl = start_decl | |
f7d98d58 JM |
6270 | (tsubst (TREE_OPERAND (t, 0), args, in_decl), |
6271 | tsubst (TREE_OPERAND (t, 1), args, in_decl), | |
a1774733 | 6272 | TREE_OPERAND (t, 2) != 0, NULL_TREE, NULL_TREE); |
f7d98d58 | 6273 | init = tsubst_expr (TREE_OPERAND (t, 2), args, in_decl); |
5566b478 | 6274 | cp_finish_decl |
a0128b67 | 6275 | (dcl, init, NULL_TREE, 1, /*init ? LOOKUP_ONLYCONVERTING :*/ 0); |
5566b478 MS |
6276 | resume_momentary (i); |
6277 | return dcl; | |
6278 | } | |
8d08fdba | 6279 | |
5566b478 MS |
6280 | case FOR_STMT: |
6281 | { | |
6282 | tree tmp; | |
5566b478 | 6283 | lineno = TREE_COMPLEXITY (t); |
5566b478 | 6284 | |
ad321293 MM |
6285 | begin_for_stmt (); |
6286 | for (tmp = FOR_INIT_STMT (t); tmp; tmp = TREE_CHAIN (tmp)) | |
6287 | tsubst_expr (tmp, args, in_decl); | |
6288 | finish_for_init_stmt (NULL_TREE); | |
6289 | finish_for_cond (tsubst_expr (FOR_COND (t), args, | |
6290 | in_decl), | |
6291 | NULL_TREE); | |
6292 | tmp = tsubst_expr (FOR_EXPR (t), args, in_decl); | |
6293 | finish_for_expr (tmp, NULL_TREE); | |
6294 | tsubst_expr (FOR_BODY (t), args, in_decl); | |
6295 | finish_for_stmt (tmp, NULL_TREE); | |
5566b478 MS |
6296 | } |
6297 | break; | |
8d08fdba | 6298 | |
5566b478 MS |
6299 | case WHILE_STMT: |
6300 | { | |
5566b478 | 6301 | lineno = TREE_COMPLEXITY (t); |
ad321293 MM |
6302 | begin_while_stmt (); |
6303 | finish_while_stmt_cond (tsubst_expr (WHILE_COND (t), | |
6304 | args, in_decl), | |
6305 | NULL_TREE); | |
6306 | tsubst_expr (WHILE_BODY (t), args, in_decl); | |
6307 | finish_while_stmt (NULL_TREE); | |
5566b478 MS |
6308 | } |
6309 | break; | |
8d08fdba | 6310 | |
5566b478 MS |
6311 | case DO_STMT: |
6312 | { | |
5566b478 | 6313 | lineno = TREE_COMPLEXITY (t); |
ad321293 MM |
6314 | begin_do_stmt (); |
6315 | tsubst_expr (DO_BODY (t), args, in_decl); | |
6316 | finish_do_body (NULL_TREE); | |
6317 | finish_do_stmt (tsubst_expr (DO_COND (t), args, | |
6318 | in_decl), | |
6319 | NULL_TREE); | |
5566b478 MS |
6320 | } |
6321 | break; | |
a0a33927 | 6322 | |
5566b478 | 6323 | case IF_STMT: |
8d08fdba | 6324 | { |
5566b478 | 6325 | tree tmp; |
5566b478 MS |
6326 | |
6327 | lineno = TREE_COMPLEXITY (t); | |
ad321293 MM |
6328 | begin_if_stmt (); |
6329 | finish_if_stmt_cond (tsubst_expr (IF_COND (t), | |
6330 | args, in_decl), | |
6331 | NULL_TREE); | |
db5ae43f | 6332 | |
ad321293 | 6333 | if (tmp = THEN_CLAUSE (t), tmp) |
db5ae43f | 6334 | { |
f7d98d58 | 6335 | tsubst_expr (tmp, args, in_decl); |
ad321293 | 6336 | finish_then_clause (NULL_TREE); |
db5ae43f MS |
6337 | } |
6338 | ||
ad321293 MM |
6339 | if (tmp = ELSE_CLAUSE (t), tmp) |
6340 | { | |
6341 | begin_else_clause (); | |
6342 | tsubst_expr (tmp, args, in_decl); | |
6343 | finish_else_clause (NULL_TREE); | |
6344 | } | |
8d08fdba | 6345 | |
ad321293 | 6346 | finish_if_stmt (); |
8d08fdba | 6347 | } |
5566b478 | 6348 | break; |
8d08fdba | 6349 | |
5566b478 MS |
6350 | case COMPOUND_STMT: |
6351 | { | |
ad321293 | 6352 | tree substmt; |
8d08fdba | 6353 | |
5566b478 | 6354 | lineno = TREE_COMPLEXITY (t); |
ad321293 MM |
6355 | begin_compound_stmt (COMPOUND_STMT_NO_SCOPE (t)); |
6356 | for (substmt = COMPOUND_BODY (t); | |
6357 | substmt != NULL_TREE; | |
6358 | substmt = TREE_CHAIN (substmt)) | |
f7d98d58 | 6359 | tsubst_expr (substmt, args, in_decl); |
ad321293 MM |
6360 | return finish_compound_stmt (COMPOUND_STMT_NO_SCOPE (t), |
6361 | NULL_TREE); | |
5566b478 MS |
6362 | } |
6363 | break; | |
8d08fdba | 6364 | |
5566b478 MS |
6365 | case BREAK_STMT: |
6366 | lineno = TREE_COMPLEXITY (t); | |
ad321293 | 6367 | finish_break_stmt (); |
5566b478 | 6368 | break; |
8d08fdba | 6369 | |
6467930b MS |
6370 | case CONTINUE_STMT: |
6371 | lineno = TREE_COMPLEXITY (t); | |
ad321293 | 6372 | finish_continue_stmt (); |
6467930b MS |
6373 | break; |
6374 | ||
5566b478 MS |
6375 | case SWITCH_STMT: |
6376 | { | |
6377 | tree val, tmp; | |
5566b478 MS |
6378 | |
6379 | lineno = TREE_COMPLEXITY (t); | |
ad321293 MM |
6380 | begin_switch_stmt (); |
6381 | val = tsubst_expr (SWITCH_COND (t), args, in_decl); | |
6382 | finish_switch_cond (val); | |
5566b478 MS |
6383 | |
6384 | if (tmp = TREE_OPERAND (t, 1), tmp) | |
f7d98d58 | 6385 | tsubst_expr (tmp, args, in_decl); |
8d08fdba | 6386 | |
ad321293 | 6387 | finish_switch_stmt (val, NULL_TREE); |
5566b478 MS |
6388 | } |
6389 | break; | |
6390 | ||
6391 | case CASE_LABEL: | |
ad321293 MM |
6392 | finish_case_label (tsubst_expr (CASE_LOW (t), args, in_decl), |
6393 | tsubst_expr (CASE_HIGH (t), args, in_decl)); | |
5566b478 MS |
6394 | break; |
6395 | ||
6396 | case LABEL_DECL: | |
6397 | t = define_label (DECL_SOURCE_FILE (t), DECL_SOURCE_LINE (t), | |
6398 | DECL_NAME (t)); | |
6399 | if (t) | |
6400 | expand_label (t); | |
6401 | break; | |
6402 | ||
6403 | case GOTO_STMT: | |
6404 | lineno = TREE_COMPLEXITY (t); | |
aa09da44 MM |
6405 | t = GOTO_DESTINATION (t); |
6406 | if (TREE_CODE (t) != IDENTIFIER_NODE) | |
6407 | /* Computed goto's must be tsubst'd into. On the other hand, | |
6408 | non-computed gotos must not be; the identifier in question | |
6409 | will have no binding. */ | |
6410 | t = tsubst_expr (t, args, in_decl); | |
6411 | finish_goto_stmt (t); | |
ad321293 MM |
6412 | break; |
6413 | ||
6414 | case ASM_STMT: | |
6415 | lineno = TREE_COMPLEXITY (t); | |
6416 | finish_asm_stmt (tsubst_expr (ASM_CV_QUAL (t), args, in_decl), | |
6417 | tsubst_expr (ASM_STRING (t), args, in_decl), | |
6418 | tsubst_expr (ASM_OUTPUTS (t), args, in_decl), | |
6419 | tsubst_expr (ASM_INPUTS (t), args, in_decl), | |
6420 | tsubst_expr (ASM_CLOBBERS (t), args, in_decl)); | |
5566b478 | 6421 | break; |
faf5394a MS |
6422 | |
6423 | case TRY_BLOCK: | |
6424 | lineno = TREE_COMPLEXITY (t); | |
ad321293 MM |
6425 | begin_try_block (); |
6426 | tsubst_expr (TRY_STMTS (t), args, in_decl); | |
6427 | finish_try_block (NULL_TREE); | |
faf5394a | 6428 | { |
ad321293 | 6429 | tree handler = TRY_HANDLERS (t); |
faf5394a | 6430 | for (; handler; handler = TREE_CHAIN (handler)) |
f7d98d58 | 6431 | tsubst_expr (handler, args, in_decl); |
faf5394a | 6432 | } |
ad321293 | 6433 | finish_handler_sequence (NULL_TREE); |
faf5394a MS |
6434 | break; |
6435 | ||
6436 | case HANDLER: | |
6437 | lineno = TREE_COMPLEXITY (t); | |
ad321293 MM |
6438 | begin_handler (); |
6439 | if (HANDLER_PARMS (t)) | |
faf5394a | 6440 | { |
ad321293 | 6441 | tree d = HANDLER_PARMS (t); |
faf5394a | 6442 | expand_start_catch_block |
f7d98d58 JM |
6443 | (tsubst (TREE_OPERAND (d, 1), args, in_decl), |
6444 | tsubst (TREE_OPERAND (d, 0), args, in_decl)); | |
faf5394a MS |
6445 | } |
6446 | else | |
6447 | expand_start_catch_block (NULL_TREE, NULL_TREE); | |
ad321293 MM |
6448 | finish_handler_parms (NULL_TREE); |
6449 | tsubst_expr (HANDLER_BODY (t), args, in_decl); | |
6450 | finish_handler (NULL_TREE); | |
faf5394a MS |
6451 | break; |
6452 | ||
b87692e5 MS |
6453 | case TAG_DEFN: |
6454 | lineno = TREE_COMPLEXITY (t); | |
6455 | t = TREE_TYPE (t); | |
6456 | if (TREE_CODE (t) == ENUMERAL_TYPE) | |
ed44da02 | 6457 | tsubst (t, args, NULL_TREE); |
b87692e5 MS |
6458 | break; |
6459 | ||
5566b478 | 6460 | default: |
f7d98d58 | 6461 | return build_expr_from_tree (tsubst_copy (t, args, in_decl)); |
5566b478 MS |
6462 | } |
6463 | return NULL_TREE; | |
8d08fdba MS |
6464 | } |
6465 | ||
36a117a5 MM |
6466 | /* Instantiate the indicated variable of function template TMPL with |
6467 | the template arguments in TARG_PTR. */ | |
6468 | ||
5566b478 MS |
6469 | tree |
6470 | instantiate_template (tmpl, targ_ptr) | |
98c1c668 | 6471 | tree tmpl, targ_ptr; |
8d08fdba | 6472 | { |
5566b478 | 6473 | tree fndecl; |
36a117a5 MM |
6474 | tree gen_tmpl; |
6475 | tree spec; | |
5566b478 MS |
6476 | int i, len; |
6477 | struct obstack *old_fmp_obstack; | |
6478 | extern struct obstack *function_maybepermanent_obstack; | |
36a117a5 | 6479 | tree inner_args; |
5566b478 | 6480 | |
27fafc8d JM |
6481 | if (tmpl == error_mark_node) |
6482 | return error_mark_node; | |
6483 | ||
386b8a85 JM |
6484 | my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 283); |
6485 | ||
36a117a5 MM |
6486 | /* Check to see if we already have this specialization. */ |
6487 | spec = retrieve_specialization (tmpl, targ_ptr); | |
6488 | if (spec != NULL_TREE) | |
6489 | return spec; | |
6490 | ||
6491 | if (DECL_TEMPLATE_INFO (tmpl)) | |
386b8a85 | 6492 | { |
36a117a5 MM |
6493 | /* The TMPL is a partial instantiation. To get a full set of |
6494 | arguments we must add the arguments used to perform the | |
6495 | partial instantiation. */ | |
6496 | targ_ptr = add_outermost_template_args (DECL_TI_ARGS (tmpl), | |
6497 | targ_ptr); | |
6498 | gen_tmpl = most_general_template (tmpl); | |
6499 | ||
6500 | /* Check to see if we already have this specialization. */ | |
6501 | spec = retrieve_specialization (gen_tmpl, targ_ptr); | |
75650646 MM |
6502 | if (spec != NULL_TREE) |
6503 | return spec; | |
386b8a85 | 6504 | } |
36a117a5 MM |
6505 | else |
6506 | gen_tmpl = tmpl; | |
386b8a85 | 6507 | |
5566b478 MS |
6508 | push_obstacks (&permanent_obstack, &permanent_obstack); |
6509 | old_fmp_obstack = function_maybepermanent_obstack; | |
6510 | function_maybepermanent_obstack = &permanent_obstack; | |
8d08fdba | 6511 | |
36a117a5 MM |
6512 | len = DECL_NTPARMS (gen_tmpl); |
6513 | inner_args = innermost_args (targ_ptr); | |
5566b478 MS |
6514 | i = len; |
6515 | while (i--) | |
8d08fdba | 6516 | { |
36a117a5 | 6517 | tree t = TREE_VEC_ELT (inner_args, i); |
5566b478 MS |
6518 | if (TREE_CODE_CLASS (TREE_CODE (t)) == 't') |
6519 | { | |
6520 | tree nt = target_type (t); | |
ec255269 | 6521 | if (IS_AGGR_TYPE (nt) && decl_function_context (TYPE_MAIN_DECL (nt))) |
5566b478 | 6522 | { |
8251199e JM |
6523 | cp_error ("type `%T' composed from a local class is not a valid template-argument", t); |
6524 | cp_error (" trying to instantiate `%D'", gen_tmpl); | |
5566b478 MS |
6525 | fndecl = error_mark_node; |
6526 | goto out; | |
6527 | } | |
6528 | } | |
8d08fdba | 6529 | } |
e66d884e | 6530 | targ_ptr = copy_to_permanent (targ_ptr); |
8d08fdba | 6531 | |
5566b478 | 6532 | /* substitute template parameters */ |
36a117a5 MM |
6533 | fndecl = tsubst (DECL_RESULT (gen_tmpl), targ_ptr, gen_tmpl); |
6534 | /* The DECL_TI_TEMPLATE should always be the immediate parent | |
6535 | template, not the most general template. */ | |
6536 | DECL_TI_TEMPLATE (fndecl) = tmpl; | |
8d08fdba | 6537 | |
824b9a4c MS |
6538 | if (flag_external_templates) |
6539 | add_pending_template (fndecl); | |
6540 | ||
5566b478 MS |
6541 | out: |
6542 | function_maybepermanent_obstack = old_fmp_obstack; | |
6543 | pop_obstacks (); | |
8d08fdba | 6544 | |
5566b478 | 6545 | return fndecl; |
8d08fdba | 6546 | } |
5566b478 MS |
6547 | |
6548 | /* Push the name of the class template into the scope of the instantiation. */ | |
8d08fdba MS |
6549 | |
6550 | void | |
5566b478 MS |
6551 | overload_template_name (type) |
6552 | tree type; | |
8d08fdba | 6553 | { |
5566b478 MS |
6554 | tree id = DECL_NAME (CLASSTYPE_TI_TEMPLATE (type)); |
6555 | tree decl; | |
8d08fdba | 6556 | |
5566b478 MS |
6557 | if (IDENTIFIER_CLASS_VALUE (id) |
6558 | && TREE_TYPE (IDENTIFIER_CLASS_VALUE (id)) == type) | |
6559 | return; | |
8d08fdba | 6560 | |
5566b478 MS |
6561 | decl = build_decl (TYPE_DECL, id, type); |
6562 | SET_DECL_ARTIFICIAL (decl); | |
6563 | pushdecl_class_level (decl); | |
8d08fdba MS |
6564 | } |
6565 | ||
956d6950 | 6566 | /* Like type_unification but designed specially to handle conversion |
9f54c803 MM |
6567 | operators. |
6568 | ||
6569 | The FN is a TEMPLATE_DECL for a function. The ARGS are the | |
6570 | arguments that are being used when calling it. | |
6571 | ||
6572 | If FN is a conversion operator, RETURN_TYPE is the type desired as | |
6573 | the result of the conversion operator. | |
6574 | ||
6575 | The EXTRA_FN_ARG, if any, is the type of an additional | |
6576 | parameter to be added to the beginning of FN's parameter list. | |
6577 | ||
6578 | The other arguments are as for type_unification. */ | |
98c1c668 JM |
6579 | |
6580 | int | |
d7684f2d MM |
6581 | fn_type_unification (fn, explicit_targs, targs, args, return_type, |
6582 | strict, extra_fn_arg) | |
386b8a85 | 6583 | tree fn, explicit_targs, targs, args, return_type; |
830bfa74 | 6584 | unification_kind_t strict; |
d7684f2d | 6585 | tree extra_fn_arg; |
98c1c668 | 6586 | { |
9f54c803 | 6587 | tree parms; |
98c1c668 JM |
6588 | |
6589 | my_friendly_assert (TREE_CODE (fn) == TEMPLATE_DECL, 0); | |
6590 | ||
9f54c803 MM |
6591 | parms = TYPE_ARG_TYPES (TREE_TYPE (fn)); |
6592 | ||
aa45967f | 6593 | if (DECL_CONV_FN_P (fn)) |
98c1c668 JM |
6594 | { |
6595 | /* This is a template conversion operator. Use the return types | |
6596 | as well as the argument types. */ | |
9f54c803 MM |
6597 | parms = scratch_tree_cons (NULL_TREE, |
6598 | TREE_TYPE (TREE_TYPE (fn)), | |
6599 | parms); | |
6600 | args = scratch_tree_cons (NULL_TREE, return_type, args); | |
98c1c668 JM |
6601 | } |
6602 | ||
d7684f2d | 6603 | if (extra_fn_arg != NULL_TREE) |
9f54c803 | 6604 | parms = scratch_tree_cons (NULL_TREE, extra_fn_arg, parms); |
d7684f2d | 6605 | |
7cd55873 MM |
6606 | /* We allow incomplete unification without an error message here |
6607 | because the standard doesn't seem to explicitly prohibit it. Our | |
6608 | callers must be ready to deal with unification failures in any | |
6609 | event. */ | |
9f54c803 MM |
6610 | return type_unification (DECL_INNERMOST_TEMPLATE_PARMS (fn), |
6611 | targs, | |
6612 | parms, | |
6613 | args, | |
6614 | explicit_targs, | |
6615 | strict, 1); | |
98c1c668 JM |
6616 | } |
6617 | ||
6618 | ||
8d08fdba MS |
6619 | /* Type unification. |
6620 | ||
6621 | We have a function template signature with one or more references to | |
6622 | template parameters, and a parameter list we wish to fit to this | |
6623 | template. If possible, produce a list of parameters for the template | |
6624 | which will cause it to fit the supplied parameter list. | |
6625 | ||
6626 | Return zero for success, 2 for an incomplete match that doesn't resolve | |
6627 | all the types, and 1 for complete failure. An error message will be | |
6628 | printed only for an incomplete match. | |
6629 | ||
9f54c803 | 6630 | TPARMS[NTPARMS] is an array of template parameter types. |
8d08fdba | 6631 | |
9f54c803 MM |
6632 | TARGS[NTPARMS] is the array into which the deduced template |
6633 | parameter values are placed. PARMS is the function template's | |
6634 | signature (using TEMPLATE_PARM_IDX nodes), and ARGS is the argument | |
6635 | list we're trying to match against it. | |
6636 | ||
6637 | The EXPLICIT_TARGS are explicit template arguments provided via a | |
6638 | template-id. | |
6467930b | 6639 | |
830bfa74 MM |
6640 | The parameter STRICT is one of: |
6641 | ||
6642 | DEDUCE_CALL: | |
6643 | We are deducing arguments for a function call, as in | |
6644 | [temp.deduct.call]. | |
6645 | ||
6646 | DEDUCE_CONV: | |
6647 | We are deducing arguments for a conversion function, as in | |
6648 | [temp.deduct.conv]. | |
6649 | ||
6650 | DEDUCE_EXACT: | |
6651 | We are deducing arguments when calculating the partial | |
6652 | ordering between specializations of function or class | |
6653 | templates, as in [temp.func.order] and [temp.class.order], | |
6654 | when doing an explicit instantiation as in [temp.explicit], | |
9f54c803 | 6655 | when determining an explicit specialization as in |
830bfa74 MM |
6656 | [temp.expl.spec], or when taking the address of a function |
6657 | template, as in [temp.deduct.funcaddr]. */ | |
8d08fdba MS |
6658 | |
6659 | int | |
9f54c803 | 6660 | type_unification (tparms, targs, parms, args, explicit_targs, |
386b8a85 | 6661 | strict, allow_incomplete) |
9f54c803 | 6662 | tree tparms, targs, parms, args, explicit_targs; |
830bfa74 MM |
6663 | unification_kind_t strict; |
6664 | int allow_incomplete; | |
386b8a85 | 6665 | { |
050367a3 | 6666 | int* explicit_mask; |
386b8a85 | 6667 | int i; |
386b8a85 | 6668 | |
830bfa74 | 6669 | for (i = 0; i < TREE_VEC_LENGTH (tparms); i++) |
050367a3 | 6670 | TREE_VEC_ELT (targs, i) = NULL_TREE; |
386b8a85 | 6671 | |
9f54c803 | 6672 | if (explicit_targs != NULL_TREE) |
75650646 MM |
6673 | { |
6674 | tree arg_vec; | |
9f54c803 | 6675 | arg_vec = coerce_template_parms (tparms, explicit_targs, NULL_TREE, 0, |
744fac59 | 6676 | 0); |
75650646 MM |
6677 | |
6678 | if (arg_vec == error_mark_node) | |
6679 | return 1; | |
386b8a85 | 6680 | |
050367a3 | 6681 | explicit_mask = alloca (sizeof (int) * TREE_VEC_LENGTH (targs)); |
7dee3f36 | 6682 | bzero ((char *) explicit_mask, sizeof(int) * TREE_VEC_LENGTH (targs)); |
050367a3 | 6683 | |
75650646 MM |
6684 | for (i = 0; |
6685 | i < TREE_VEC_LENGTH (arg_vec) | |
050367a3 | 6686 | && TREE_VEC_ELT (arg_vec, i) != NULL_TREE; |
75650646 | 6687 | ++i) |
050367a3 MM |
6688 | { |
6689 | TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (arg_vec, i); | |
6690 | /* Let unify know that this argument was explicit. */ | |
6691 | explicit_mask [i] = 1; | |
6692 | } | |
75650646 | 6693 | } |
050367a3 MM |
6694 | else |
6695 | explicit_mask = 0; | |
386b8a85 | 6696 | |
830bfa74 MM |
6697 | return |
6698 | type_unification_real (tparms, targs, parms, args, 0, | |
6699 | strict, allow_incomplete, explicit_mask); | |
6700 | } | |
6701 | ||
6702 | /* Adjust types before performing type deduction, as described in | |
6703 | [temp.deduct.call] and [temp.deduct.conv]. The rules in these two | |
4c7d0dff MM |
6704 | sections are symmetric. PARM is the type of a function parameter |
6705 | or the return type of the conversion function. ARG is the type of | |
6706 | the argument passed to the call, or the type of the value | |
6707 | intialized with the result of the conversion function. */ | |
386b8a85 | 6708 | |
830bfa74 MM |
6709 | void |
6710 | maybe_adjust_types_for_deduction (strict, parm, arg) | |
6711 | unification_kind_t strict; | |
6712 | tree* parm; | |
6713 | tree* arg; | |
6714 | { | |
6715 | switch (strict) | |
6716 | { | |
6717 | case DEDUCE_CALL: | |
6718 | break; | |
6719 | ||
6720 | case DEDUCE_CONV: | |
6721 | { | |
4c7d0dff MM |
6722 | /* Swap PARM and ARG throughout the remainder of this |
6723 | function; the handling is precisely symmetric since PARM | |
6724 | will initialize ARG rather than vice versa. */ | |
830bfa74 MM |
6725 | tree* temp = parm; |
6726 | parm = arg; | |
6727 | arg = temp; | |
6728 | break; | |
6729 | } | |
6730 | ||
6731 | case DEDUCE_EXACT: | |
6732 | /* There is nothing to do in this case. */ | |
6733 | return; | |
6734 | ||
6735 | default: | |
6736 | my_friendly_abort (0); | |
6737 | } | |
6738 | ||
6739 | if (TREE_CODE (*parm) != REFERENCE_TYPE) | |
6740 | { | |
6741 | /* [temp.deduct.call] | |
6742 | ||
6743 | If P is not a reference type: | |
6744 | ||
6745 | --If A is an array type, the pointer type produced by the | |
6746 | array-to-pointer standard conversion (_conv.array_) is | |
6747 | used in place of A for type deduction; otherwise, | |
6748 | ||
6749 | --If A is a function type, the pointer type produced by | |
6750 | the function-to-pointer standard conversion | |
6751 | (_conv.func_) is used in place of A for type deduction; | |
6752 | otherwise, | |
6753 | ||
6754 | --If A is a cv-qualified type, the top level | |
6755 | cv-qualifiers of A's type are ignored for type | |
6756 | deduction. */ | |
6757 | if (TREE_CODE (*arg) == ARRAY_TYPE) | |
6758 | *arg = build_pointer_type (TREE_TYPE (*arg)); | |
6759 | else if (TREE_CODE (*arg) == FUNCTION_TYPE | |
6760 | || TREE_CODE (*arg) == METHOD_TYPE) | |
6761 | *arg = build_pointer_type (*arg); | |
6762 | else | |
6763 | *arg = TYPE_MAIN_VARIANT (*arg); | |
6764 | } | |
6765 | ||
6766 | /* [temp.deduct.call] | |
6767 | ||
6768 | If P is a cv-qualified type, the top level cv-qualifiers | |
6769 | of P's type are ignored for type deduction. If P is a | |
6770 | reference type, the type referred to by P is used for | |
6771 | type deduction. */ | |
6772 | *parm = TYPE_MAIN_VARIANT (*parm); | |
6773 | if (TREE_CODE (*parm) == REFERENCE_TYPE) | |
6774 | *parm = TREE_TYPE (*parm); | |
386b8a85 JM |
6775 | } |
6776 | ||
050367a3 MM |
6777 | /* Like type_unfication. EXPLICIT_MASK, if non-NULL, is an array of |
6778 | integers, with ones in positions corresponding to arguments in | |
9f54c803 MM |
6779 | targs that were provided explicitly, and zeros elsewhere. |
6780 | ||
6781 | If SUBR is 1, we're being called recursively (to unify the | |
6782 | arguments of a function or method parameter of a function | |
6783 | template). */ | |
386b8a85 | 6784 | |
4966381a | 6785 | static int |
3b3ba9f0 | 6786 | type_unification_real (tparms, targs, parms, args, subr, |
050367a3 MM |
6787 | strict, allow_incomplete, explicit_mask) |
6788 | tree tparms, targs, parms, args; | |
830bfa74 MM |
6789 | int subr; |
6790 | unification_kind_t strict; | |
6791 | int allow_incomplete; | |
050367a3 | 6792 | int* explicit_mask; |
8d08fdba MS |
6793 | { |
6794 | tree parm, arg; | |
6795 | int i; | |
6796 | int ntparms = TREE_VEC_LENGTH (tparms); | |
830bfa74 | 6797 | int sub_strict; |
8d08fdba MS |
6798 | |
6799 | my_friendly_assert (TREE_CODE (tparms) == TREE_VEC, 289); | |
386b8a85 JM |
6800 | my_friendly_assert (parms == NULL_TREE |
6801 | || TREE_CODE (parms) == TREE_LIST, 290); | |
51c184be | 6802 | /* ARGS could be NULL (via a call from parse.y to |
8d08fdba MS |
6803 | build_x_function_call). */ |
6804 | if (args) | |
6805 | my_friendly_assert (TREE_CODE (args) == TREE_LIST, 291); | |
6806 | my_friendly_assert (ntparms > 0, 292); | |
6807 | ||
830bfa74 MM |
6808 | switch (strict) |
6809 | { | |
6810 | case DEDUCE_CALL: | |
6811 | sub_strict = UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_DERIVED; | |
6812 | break; | |
6813 | ||
6814 | case DEDUCE_CONV: | |
6815 | sub_strict = UNIFY_ALLOW_LESS_CV_QUAL; | |
6816 | break; | |
6817 | ||
6818 | case DEDUCE_EXACT: | |
6819 | sub_strict = UNIFY_ALLOW_NONE; | |
6820 | break; | |
6821 | ||
6822 | default: | |
6823 | my_friendly_abort (0); | |
6824 | } | |
6825 | ||
8d08fdba MS |
6826 | while (parms |
6827 | && parms != void_list_node | |
6828 | && args | |
6829 | && args != void_list_node) | |
6830 | { | |
6831 | parm = TREE_VALUE (parms); | |
6832 | parms = TREE_CHAIN (parms); | |
6833 | arg = TREE_VALUE (args); | |
6834 | args = TREE_CHAIN (args); | |
6835 | ||
6836 | if (arg == error_mark_node) | |
6837 | return 1; | |
6838 | if (arg == unknown_type_node) | |
34016c81 JM |
6839 | /* We can't deduce anything from this, but we might get all the |
6840 | template args from other function args. */ | |
6841 | continue; | |
b7484fbe | 6842 | |
03e70705 JM |
6843 | /* Conversions will be performed on a function argument that |
6844 | corresponds with a function parameter that contains only | |
6845 | non-deducible template parameters and explicitly specified | |
6846 | template parameters. */ | |
6847 | if (! uses_template_parms (parm)) | |
b7484fbe | 6848 | { |
03e70705 JM |
6849 | tree type; |
6850 | ||
6851 | if (TREE_CODE_CLASS (TREE_CODE (arg)) != 't') | |
6852 | type = TREE_TYPE (arg); | |
6853 | else | |
6854 | { | |
6855 | type = arg; | |
6856 | arg = NULL_TREE; | |
6857 | } | |
6858 | ||
830bfa74 | 6859 | if (strict == DEDUCE_EXACT) |
03e70705 | 6860 | { |
3bfdc719 | 6861 | if (same_type_p (parm, type)) |
03e70705 JM |
6862 | continue; |
6863 | } | |
03e70705 | 6864 | else |
343c89cd JM |
6865 | /* It might work; we shouldn't check now, because we might |
6866 | get into infinite recursion. Overload resolution will | |
6867 | handle it. */ | |
6868 | continue; | |
03e70705 | 6869 | |
b7484fbe MS |
6870 | return 1; |
6871 | } | |
6872 | ||
8d08fdba MS |
6873 | #if 0 |
6874 | if (TREE_CODE (arg) == VAR_DECL) | |
6875 | arg = TREE_TYPE (arg); | |
6876 | else if (TREE_CODE_CLASS (TREE_CODE (arg)) == 'e') | |
6877 | arg = TREE_TYPE (arg); | |
6878 | #else | |
6879 | if (TREE_CODE_CLASS (TREE_CODE (arg)) != 't') | |
6880 | { | |
6881 | my_friendly_assert (TREE_TYPE (arg) != NULL_TREE, 293); | |
34016c81 | 6882 | if (type_unknown_p (arg)) |
28cbf42c | 6883 | { |
34016c81 JM |
6884 | /* [temp.deduct.type] A template-argument can be deduced from |
6885 | a pointer to function or pointer to member function | |
6886 | argument if the set of overloaded functions does not | |
6887 | contain function templates and at most one of a set of | |
6888 | overloaded functions provides a unique match. */ | |
6889 | ||
6890 | if (resolve_overloaded_unification | |
6891 | (tparms, targs, parm, arg, strict, sub_strict, explicit_mask) | |
6892 | != 0) | |
6893 | return 1; | |
6894 | continue; | |
28cbf42c | 6895 | } |
8d08fdba MS |
6896 | arg = TREE_TYPE (arg); |
6897 | } | |
6898 | #endif | |
830bfa74 MM |
6899 | if (!subr) |
6900 | maybe_adjust_types_for_deduction (strict, &parm, &arg); | |
db5ae43f | 6901 | |
830bfa74 | 6902 | switch (unify (tparms, targs, parm, arg, sub_strict, |
050367a3 | 6903 | explicit_mask)) |
8d08fdba MS |
6904 | { |
6905 | case 0: | |
6906 | break; | |
6907 | case 1: | |
6908 | return 1; | |
6909 | } | |
6910 | } | |
6911 | /* Fail if we've reached the end of the parm list, and more args | |
6912 | are present, and the parm list isn't variadic. */ | |
6913 | if (args && args != void_list_node && parms == void_list_node) | |
6914 | return 1; | |
6915 | /* Fail if parms are left and they don't have default values. */ | |
6916 | if (parms | |
6917 | && parms != void_list_node | |
6918 | && TREE_PURPOSE (parms) == NULL_TREE) | |
6919 | return 1; | |
6920 | if (!subr) | |
6921 | for (i = 0; i < ntparms; i++) | |
050367a3 | 6922 | if (TREE_VEC_ELT (targs, i) == NULL_TREE) |
8d08fdba | 6923 | { |
386b8a85 | 6924 | if (!allow_incomplete) |
8251199e | 6925 | error ("incomplete type unification"); |
8d08fdba MS |
6926 | return 2; |
6927 | } | |
6928 | return 0; | |
6929 | } | |
6930 | ||
34016c81 JM |
6931 | /* Subroutine of type_unification_real. Args are like the variables at the |
6932 | call site. ARG is an overloaded function (or template-id); we try | |
6933 | deducing template args from each of the overloads, and if only one | |
6934 | succeeds, we go with that. Modifies TARGS and returns 0 on success. */ | |
6935 | ||
6936 | static int | |
6937 | resolve_overloaded_unification (tparms, targs, parm, arg, strict, | |
6938 | sub_strict, explicit_mask) | |
6939 | tree tparms, targs, parm, arg; | |
6940 | unification_kind_t strict; | |
6941 | int sub_strict; | |
6942 | int* explicit_mask; | |
6943 | { | |
6944 | tree tempargs = copy_node (targs); | |
6945 | int good = 0; | |
6946 | ||
6947 | if (TREE_CODE (arg) == ADDR_EXPR) | |
6948 | arg = TREE_OPERAND (arg, 0); | |
6949 | if (TREE_CODE (arg) == TEMPLATE_ID_EXPR) | |
6950 | { | |
6951 | /* If we got some explicit template args, we need to plug them into | |
6952 | the affected templates before we try to unify, in case the | |
6953 | explicit args will completely resolve the templates in question. */ | |
6954 | ||
6955 | tree expl_subargs = TREE_OPERAND (arg, 1); | |
6956 | arg = TREE_OPERAND (arg, 0); | |
6957 | ||
6958 | for (; arg; arg = OVL_NEXT (arg)) | |
6959 | { | |
6960 | tree fn = OVL_CURRENT (arg); | |
6961 | tree subargs, elem; | |
6962 | ||
6963 | if (TREE_CODE (fn) != TEMPLATE_DECL) | |
6964 | continue; | |
6965 | ||
6966 | subargs = get_bindings_overload (fn, DECL_RESULT (fn), expl_subargs); | |
6967 | if (subargs) | |
6968 | { | |
6969 | elem = tsubst (TREE_TYPE (fn), subargs, NULL_TREE); | |
6970 | good += try_one_overload (tparms, tempargs, parm, elem, | |
6971 | strict, sub_strict, explicit_mask); | |
6972 | } | |
6973 | } | |
6974 | } | |
6975 | else if (TREE_CODE (arg) == OVERLOAD) | |
6976 | { | |
6977 | for (; arg; arg = OVL_NEXT (arg)) | |
6978 | good += try_one_overload (tparms, tempargs, parm, | |
6979 | TREE_TYPE (OVL_CURRENT (arg)), | |
6980 | strict, sub_strict, explicit_mask); | |
6981 | } | |
6982 | else | |
6983 | my_friendly_abort (981006); | |
6984 | ||
6985 | /* [temp.deduct.type] A template-argument can be deduced from a pointer | |
6986 | to function or pointer to member function argument if the set of | |
6987 | overloaded functions does not contain function templates and at most | |
6988 | one of a set of overloaded functions provides a unique match. | |
6989 | ||
6990 | So if we found multiple possibilities, we return success but don't | |
6991 | deduce anything. */ | |
6992 | ||
6993 | if (good == 1) | |
6994 | { | |
6995 | int i = TREE_VEC_LENGTH (targs); | |
6996 | for (; i--; ) | |
6997 | if (TREE_VEC_ELT (tempargs, i)) | |
6998 | TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (tempargs, i); | |
6999 | } | |
7000 | if (good) | |
7001 | return 0; | |
7002 | ||
7003 | return 1; | |
7004 | } | |
7005 | ||
7006 | /* Subroutine of resolve_overloaded_unification; does deduction for a single | |
7007 | overload. Fills TARGS with any deduced arguments, or error_mark_node if | |
7008 | different overloads deduce different arguments for a given parm. | |
7009 | Returns 1 on success. */ | |
7010 | ||
7011 | static int | |
7012 | try_one_overload (tparms, targs, parm, arg, strict, | |
7013 | sub_strict, explicit_mask) | |
7014 | tree tparms, targs, parm, arg; | |
7015 | unification_kind_t strict; | |
7016 | int sub_strict; | |
7017 | int* explicit_mask; | |
7018 | { | |
7019 | int nargs; | |
7020 | tree tempargs; | |
7021 | int i; | |
7022 | ||
7023 | /* [temp.deduct.type] A template-argument can be deduced from a pointer | |
7024 | to function or pointer to member function argument if the set of | |
7025 | overloaded functions does not contain function templates and at most | |
7026 | one of a set of overloaded functions provides a unique match. | |
7027 | ||
7028 | So if this is a template, just return success. */ | |
7029 | ||
7030 | if (uses_template_parms (arg)) | |
7031 | return 1; | |
7032 | ||
7033 | maybe_adjust_types_for_deduction (strict, &parm, &arg); | |
7034 | ||
7035 | /* We don't copy orig_targs for this because if we have already deduced | |
7036 | some template args from previous args, unify would complain when we | |
7037 | try to deduce a template parameter for the same argument, even though | |
7038 | there isn't really a conflict. */ | |
7039 | nargs = TREE_VEC_LENGTH (targs); | |
7040 | tempargs = make_scratch_vec (nargs); | |
7041 | ||
7042 | if (unify (tparms, tempargs, parm, arg, sub_strict, explicit_mask) != 0) | |
7043 | return 0; | |
7044 | ||
7045 | /* First make sure we didn't deduce anything that conflicts with | |
7046 | previously deduced/specified args. */ | |
7047 | for (i = nargs; i--; ) | |
7048 | { | |
7049 | tree elt = TREE_VEC_ELT (tempargs, i); | |
7050 | tree oldelt = TREE_VEC_ELT (targs, i); | |
7051 | ||
7052 | if (elt == NULL_TREE) | |
7053 | continue; | |
7054 | else if (uses_template_parms (elt)) | |
7055 | { | |
7056 | /* Since we're unifying against ourselves, we will fill in template | |
7057 | args used in the function parm list with our own template parms. | |
7058 | Discard them. */ | |
7059 | TREE_VEC_ELT (tempargs, i) = NULL_TREE; | |
7060 | continue; | |
7061 | } | |
7062 | else if (oldelt && ! template_args_equal (oldelt, elt)) | |
7063 | return 0; | |
7064 | } | |
7065 | ||
7066 | for (i = nargs; i--; ) | |
7067 | { | |
7068 | tree elt = TREE_VEC_ELT (tempargs, i); | |
7069 | ||
7070 | if (elt) | |
7071 | TREE_VEC_ELT (targs, i) = elt; | |
7072 | } | |
7073 | ||
7074 | return 1; | |
7075 | } | |
7076 | ||
db2767b6 MM |
7077 | /* Returns the level of DECL, which declares a template parameter. */ |
7078 | ||
04a81d94 | 7079 | int |
db2767b6 MM |
7080 | template_decl_level (decl) |
7081 | tree decl; | |
7082 | { | |
7083 | switch (TREE_CODE (decl)) | |
7084 | { | |
7085 | case TYPE_DECL: | |
7086 | case TEMPLATE_DECL: | |
7087 | return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl)); | |
7088 | ||
7089 | case PARM_DECL: | |
7090 | return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl)); | |
7091 | ||
7092 | default: | |
7093 | my_friendly_abort (0); | |
5ffe581d | 7094 | return 0; |
db2767b6 MM |
7095 | } |
7096 | } | |
7097 | ||
830bfa74 MM |
7098 | /* Decide whether ARG can be unified with PARM, considering only the |
7099 | cv-qualifiers of each type, given STRICT as documented for unify. | |
7100 | Returns non-zero iff the unification is OK on that basis.*/ | |
e92cc029 | 7101 | |
830bfa74 MM |
7102 | int |
7103 | check_cv_quals_for_unify (strict, arg, parm) | |
7104 | int strict; | |
7105 | tree arg; | |
7106 | tree parm; | |
7107 | { | |
7108 | return !((!(strict & UNIFY_ALLOW_MORE_CV_QUAL) | |
91063b51 | 7109 | && !at_least_as_qualified_p (arg, parm)) |
830bfa74 | 7110 | || (!(strict & UNIFY_ALLOW_LESS_CV_QUAL) |
91063b51 | 7111 | && (!at_least_as_qualified_p (parm, arg)))); |
830bfa74 MM |
7112 | } |
7113 | ||
7114 | /* Takes parameters as for type_unification. Returns 0 if the | |
7115 | type deduction suceeds, 1 otherwise. The parameter STRICT is a | |
7116 | bitwise or of the following flags: | |
7117 | ||
7118 | UNIFY_ALLOW_NONE: | |
7119 | Require an exact match between PARM and ARG. | |
7120 | UNIFY_ALLOW_MORE_CV_QUAL: | |
7121 | Allow the deduced ARG to be more cv-qualified than ARG. | |
7122 | UNIFY_ALLOW_LESS_CV_QUAL: | |
7123 | Allow the deduced ARG to be less cv-qualified than ARG. | |
7124 | UNIFY_ALLOW_DERIVED: | |
7125 | Allow the deduced ARG to be a template base class of ARG, | |
7126 | or a pointer to a template base class of the type pointed to by | |
7127 | ARG. */ | |
7128 | ||
7129 | int | |
7130 | unify (tparms, targs, parm, arg, strict, explicit_mask) | |
050367a3 | 7131 | tree tparms, targs, parm, arg; |
830bfa74 | 7132 | int strict; |
050367a3 | 7133 | int* explicit_mask; |
8d08fdba MS |
7134 | { |
7135 | int idx; | |
050367a3 | 7136 | tree targ; |
db2767b6 | 7137 | tree tparm; |
8d08fdba MS |
7138 | |
7139 | /* I don't think this will do the right thing with respect to types. | |
7140 | But the only case I've seen it in so far has been array bounds, where | |
7141 | signedness is the only information lost, and I think that will be | |
7142 | okay. */ | |
7143 | while (TREE_CODE (parm) == NOP_EXPR) | |
7144 | parm = TREE_OPERAND (parm, 0); | |
7145 | ||
7146 | if (arg == error_mark_node) | |
7147 | return 1; | |
7148 | if (arg == unknown_type_node) | |
34016c81 JM |
7149 | /* We can't deduce anything from this, but we might get all the |
7150 | template args from other function args. */ | |
7151 | return 0; | |
7152 | ||
db2767b6 MM |
7153 | /* If PARM uses template parameters, then we can't bail out here, |
7154 | even in ARG == PARM, since we won't record unifications for the | |
7155 | template parameters. We might need them if we're trying to | |
7156 | figure out which of two things is more specialized. */ | |
7157 | if (arg == parm && !uses_template_parms (parm)) | |
8d08fdba MS |
7158 | return 0; |
7159 | ||
830bfa74 MM |
7160 | /* Immediately reject some pairs that won't unify because of |
7161 | cv-qualification mismatches. */ | |
7162 | if (TREE_CODE (arg) == TREE_CODE (parm) | |
49432171 | 7163 | && TREE_CODE_CLASS (TREE_CODE (arg)) == 't' |
830bfa74 MM |
7164 | /* We check the cv-qualifiers when unifying with template type |
7165 | parameters below. We want to allow ARG `const T' to unify with | |
7166 | PARM `T' for example, when computing which of two templates | |
7167 | is more specialized, for example. */ | |
7168 | && TREE_CODE (arg) != TEMPLATE_TYPE_PARM | |
7169 | && !check_cv_quals_for_unify (strict, arg, parm)) | |
49432171 JM |
7170 | return 1; |
7171 | ||
8d08fdba MS |
7172 | switch (TREE_CODE (parm)) |
7173 | { | |
2ca340ae JM |
7174 | case TYPENAME_TYPE: |
7175 | /* In a type which contains a nested-name-specifier, template | |
7176 | argument values cannot be deduced for template parameters used | |
7177 | within the nested-name-specifier. */ | |
7178 | return 0; | |
7179 | ||
8d08fdba | 7180 | case TEMPLATE_TYPE_PARM: |
73b0fce8 | 7181 | case TEMPLATE_TEMPLATE_PARM: |
db2767b6 MM |
7182 | tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0)); |
7183 | ||
7184 | if (TEMPLATE_TYPE_LEVEL (parm) | |
7185 | != template_decl_level (tparm)) | |
7186 | /* The PARM is not one we're trying to unify. Just check | |
7187 | to see if it matches ARG. */ | |
7188 | return (TREE_CODE (arg) == TREE_CODE (parm) | |
3bfdc719 | 7189 | && same_type_p (parm, arg)) ? 0 : 1; |
73b0fce8 | 7190 | idx = TEMPLATE_TYPE_IDX (parm); |
050367a3 | 7191 | targ = TREE_VEC_ELT (targs, idx); |
db2767b6 | 7192 | tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx)); |
050367a3 | 7193 | |
73b0fce8 | 7194 | /* Check for mixed types and values. */ |
db2767b6 MM |
7195 | if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM |
7196 | && TREE_CODE (tparm) != TYPE_DECL) | |
7197 | || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM | |
7198 | && TREE_CODE (tparm) != TEMPLATE_DECL)) | |
73b0fce8 KL |
7199 | return 1; |
7200 | ||
050367a3 MM |
7201 | if (!strict && targ != NULL_TREE |
7202 | && explicit_mask && explicit_mask[idx]) | |
73b0fce8 KL |
7203 | /* An explicit template argument. Don't even try to match |
7204 | here; the overload resolution code will manage check to | |
7205 | see whether the call is legal. */ | |
7206 | return 0; | |
7207 | ||
db2767b6 | 7208 | if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM) |
73b0fce8 | 7209 | { |
db2767b6 MM |
7210 | if (CLASSTYPE_TEMPLATE_INFO (parm)) |
7211 | { | |
7212 | /* We arrive here when PARM does not involve template | |
7213 | specialization. */ | |
73b0fce8 | 7214 | |
db2767b6 MM |
7215 | /* ARG must be constructed from a template class. */ |
7216 | if (TREE_CODE (arg) != RECORD_TYPE || !CLASSTYPE_TEMPLATE_INFO (arg)) | |
7217 | return 1; | |
73b0fce8 | 7218 | |
db2767b6 MM |
7219 | { |
7220 | tree parmtmpl = CLASSTYPE_TI_TEMPLATE (parm); | |
7221 | tree parmvec = CLASSTYPE_TI_ARGS (parm); | |
7222 | tree argvec = CLASSTYPE_TI_ARGS (arg); | |
7223 | tree argtmplvec | |
7224 | = DECL_INNERMOST_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (arg)); | |
7225 | int i; | |
7226 | ||
7227 | /* The parameter and argument roles have to be switched here | |
7228 | in order to handle default arguments properly. For example, | |
7229 | template<template <class> class TT> void f(TT<int>) | |
7230 | should be able to accept vector<int> which comes from | |
744fac59 | 7231 | template <class T, class Allocator = allocator> |
73b0fce8 KL |
7232 | class vector. */ |
7233 | ||
744fac59 | 7234 | if (coerce_template_parms (argtmplvec, parmvec, parmtmpl, 1, 1) |
db2767b6 MM |
7235 | == error_mark_node) |
7236 | return 1; | |
73b0fce8 | 7237 | |
db2767b6 MM |
7238 | /* Deduce arguments T, i from TT<T> or TT<i>. */ |
7239 | for (i = 0; i < TREE_VEC_LENGTH (parmvec); ++i) | |
7240 | { | |
7241 | tree t = TREE_VEC_ELT (parmvec, i); | |
7242 | if (TREE_CODE (t) != TEMPLATE_TYPE_PARM | |
7243 | && TREE_CODE (t) != TEMPLATE_TEMPLATE_PARM | |
7244 | && TREE_CODE (t) != TEMPLATE_PARM_INDEX) | |
7245 | continue; | |
73b0fce8 | 7246 | |
db2767b6 | 7247 | /* This argument can be deduced. */ |
73b0fce8 | 7248 | |
830bfa74 MM |
7249 | if (unify (tparms, targs, t, |
7250 | TREE_VEC_ELT (argvec, i), | |
7251 | UNIFY_ALLOW_NONE, explicit_mask)) | |
db2767b6 MM |
7252 | return 1; |
7253 | } | |
73b0fce8 | 7254 | } |
db2767b6 MM |
7255 | arg = CLASSTYPE_TI_TEMPLATE (arg); |
7256 | } | |
7257 | } | |
7258 | else | |
7259 | { | |
830bfa74 MM |
7260 | /* If PARM is `const T' and ARG is only `int', we don't have |
7261 | a match unless we are allowing additional qualification. | |
7262 | If ARG is `const int' and PARM is just `T' that's OK; | |
7263 | that binds `const int' to `T'. */ | |
7264 | if (!check_cv_quals_for_unify (strict | UNIFY_ALLOW_LESS_CV_QUAL, | |
7265 | arg, parm)) | |
db2767b6 MM |
7266 | return 1; |
7267 | ||
830bfa74 MM |
7268 | /* Consider the case where ARG is `const volatile int' and |
7269 | PARM is `const T'. Then, T should be `volatile int'. */ | |
7270 | arg = | |
91063b51 MM |
7271 | cp_build_qualified_type (arg, |
7272 | CP_TYPE_QUALS (arg) | |
7273 | & ~CP_TYPE_QUALS (parm)); | |
73b0fce8 KL |
7274 | } |
7275 | ||
7276 | /* Simple cases: Value already set, does match or doesn't. */ | |
3a56f0ab | 7277 | if (targ != NULL_TREE |
3bfdc719 | 7278 | && (same_type_p (targ, arg) |
3a56f0ab | 7279 | || (explicit_mask && explicit_mask[idx]))) |
73b0fce8 | 7280 | return 0; |
050367a3 | 7281 | else if (targ) |
73b0fce8 | 7282 | return 1; |
050367a3 | 7283 | TREE_VEC_ELT (targs, idx) = arg; |
73b0fce8 KL |
7284 | return 0; |
7285 | ||
f84b4be9 | 7286 | case TEMPLATE_PARM_INDEX: |
db2767b6 MM |
7287 | tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0)); |
7288 | ||
7289 | if (TEMPLATE_PARM_LEVEL (parm) | |
7290 | != template_decl_level (tparm)) | |
7291 | /* The PARM is not one we're trying to unify. Just check | |
7292 | to see if it matches ARG. */ | |
7293 | return (TREE_CODE (arg) == TREE_CODE (parm) | |
67ffc812 | 7294 | && cp_tree_equal (parm, arg) > 0) ? 0 : 1; |
db2767b6 | 7295 | |
f84b4be9 | 7296 | idx = TEMPLATE_PARM_IDX (parm); |
050367a3 | 7297 | targ = TREE_VEC_ELT (targs, idx); |
db2767b6 | 7298 | |
050367a3 | 7299 | if (targ) |
8d08fdba | 7300 | { |
67ffc812 | 7301 | int i = (cp_tree_equal (targ, arg) > 0); |
312e7d50 JM |
7302 | if (i == 1) |
7303 | return 0; | |
7304 | else if (i == 0) | |
7305 | return 1; | |
7306 | else | |
7307 | my_friendly_abort (42); | |
8d08fdba | 7308 | } |
8d08fdba | 7309 | |
050367a3 | 7310 | TREE_VEC_ELT (targs, idx) = copy_to_permanent (arg); |
8d08fdba MS |
7311 | return 0; |
7312 | ||
7313 | case POINTER_TYPE: | |
830bfa74 MM |
7314 | { |
7315 | int sub_strict; | |
4ac14744 | 7316 | |
830bfa74 MM |
7317 | if (TREE_CODE (arg) == RECORD_TYPE && TYPE_PTRMEMFUNC_FLAG (arg)) |
7318 | return (unify (tparms, targs, parm, | |
7319 | TYPE_PTRMEMFUNC_FN_TYPE (arg), strict, | |
7320 | explicit_mask)); | |
7321 | ||
7322 | if (TREE_CODE (arg) != POINTER_TYPE) | |
7323 | return 1; | |
7324 | ||
7325 | /* [temp.deduct.call] | |
7326 | ||
7327 | A can be another pointer or pointer to member type that can | |
7328 | be converted to the deduced A via a qualification | |
7329 | conversion (_conv.qual_). | |
7330 | ||
7331 | We pass down STRICT here rather than UNIFY_ALLOW_NONE. | |
7332 | This will allow for additional cv-qualification of the | |
7333 | pointed-to types if appropriate. In general, this is a bit | |
7334 | too generous; we are only supposed to allow qualification | |
7335 | conversions and this method will allow an ARG of char** and | |
7336 | a deduced ARG of const char**. However, overload | |
7337 | resolution will subsequently invalidate the candidate, so | |
7338 | this is probably OK. */ | |
7339 | sub_strict = strict; | |
7340 | ||
7341 | if (TREE_CODE (TREE_TYPE (arg)) != RECORD_TYPE | |
7342 | || TYPE_PTRMEMFUNC_FLAG (TREE_TYPE (arg))) | |
7343 | /* The derived-to-base conversion only persists through one | |
7344 | level of pointers. */ | |
7345 | sub_strict &= ~UNIFY_ALLOW_DERIVED; | |
7346 | ||
7347 | return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE | |
7348 | (arg), sub_strict, explicit_mask); | |
7349 | } | |
8d08fdba MS |
7350 | |
7351 | case REFERENCE_TYPE: | |
830bfa74 MM |
7352 | if (TREE_CODE (arg) != REFERENCE_TYPE) |
7353 | return 1; | |
7354 | return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg), | |
7355 | UNIFY_ALLOW_NONE, explicit_mask); | |
8d08fdba MS |
7356 | |
7357 | case ARRAY_TYPE: | |
7358 | if (TREE_CODE (arg) != ARRAY_TYPE) | |
7359 | return 1; | |
3042d5be MM |
7360 | if ((TYPE_DOMAIN (parm) == NULL_TREE) |
7361 | != (TYPE_DOMAIN (arg) == NULL_TREE)) | |
7362 | return 1; | |
7363 | if (TYPE_DOMAIN (parm) != NULL_TREE | |
830bfa74 MM |
7364 | && unify (tparms, targs, TYPE_DOMAIN (parm), |
7365 | TYPE_DOMAIN (arg), UNIFY_ALLOW_NONE, explicit_mask) != 0) | |
8d08fdba | 7366 | return 1; |
830bfa74 MM |
7367 | return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg), |
7368 | UNIFY_ALLOW_NONE, explicit_mask); | |
8d08fdba MS |
7369 | |
7370 | case REAL_TYPE: | |
37c46b43 | 7371 | case COMPLEX_TYPE: |
8d08fdba | 7372 | case INTEGER_TYPE: |
42976354 | 7373 | case BOOLEAN_TYPE: |
5ad5a526 | 7374 | case VOID_TYPE: |
f376e137 MS |
7375 | if (TREE_CODE (arg) != TREE_CODE (parm)) |
7376 | return 1; | |
7377 | ||
514a1f18 JM |
7378 | if (TREE_CODE (parm) == INTEGER_TYPE |
7379 | && TREE_CODE (TYPE_MAX_VALUE (parm)) != INTEGER_CST) | |
8d08fdba MS |
7380 | { |
7381 | if (TYPE_MIN_VALUE (parm) && TYPE_MIN_VALUE (arg) | |
830bfa74 MM |
7382 | && unify (tparms, targs, TYPE_MIN_VALUE (parm), |
7383 | TYPE_MIN_VALUE (arg), UNIFY_ALLOW_NONE, explicit_mask)) | |
8d08fdba MS |
7384 | return 1; |
7385 | if (TYPE_MAX_VALUE (parm) && TYPE_MAX_VALUE (arg) | |
830bfa74 MM |
7386 | && unify (tparms, targs, TYPE_MAX_VALUE (parm), |
7387 | TYPE_MAX_VALUE (arg), UNIFY_ALLOW_NONE, explicit_mask)) | |
8d08fdba MS |
7388 | return 1; |
7389 | } | |
514a1f18 JM |
7390 | /* We use the TYPE_MAIN_VARIANT since we have already |
7391 | checked cv-qualification at the top of the | |
7392 | function. */ | |
3bfdc719 MM |
7393 | else if (!same_type_p (TYPE_MAIN_VARIANT (arg), |
7394 | TYPE_MAIN_VARIANT (parm))) | |
ca79f85d JM |
7395 | return 1; |
7396 | ||
8d08fdba MS |
7397 | /* As far as unification is concerned, this wins. Later checks |
7398 | will invalidate it if necessary. */ | |
7399 | return 0; | |
7400 | ||
7401 | /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */ | |
bd6dd845 | 7402 | /* Type INTEGER_CST can come from ordinary constant template args. */ |
8d08fdba | 7403 | case INTEGER_CST: |
bd6dd845 MS |
7404 | while (TREE_CODE (arg) == NOP_EXPR) |
7405 | arg = TREE_OPERAND (arg, 0); | |
7406 | ||
8d08fdba MS |
7407 | if (TREE_CODE (arg) != INTEGER_CST) |
7408 | return 1; | |
7409 | return !tree_int_cst_equal (parm, arg); | |
7410 | ||
8d08fdba MS |
7411 | case TREE_VEC: |
7412 | { | |
7413 | int i; | |
7414 | if (TREE_CODE (arg) != TREE_VEC) | |
7415 | return 1; | |
7416 | if (TREE_VEC_LENGTH (parm) != TREE_VEC_LENGTH (arg)) | |
7417 | return 1; | |
7418 | for (i = TREE_VEC_LENGTH (parm) - 1; i >= 0; i--) | |
830bfa74 | 7419 | if (unify (tparms, targs, |
8d08fdba | 7420 | TREE_VEC_ELT (parm, i), TREE_VEC_ELT (arg, i), |
830bfa74 | 7421 | UNIFY_ALLOW_NONE, explicit_mask)) |
8d08fdba MS |
7422 | return 1; |
7423 | return 0; | |
7424 | } | |
7425 | ||
8d08fdba | 7426 | case RECORD_TYPE: |
db5ae43f | 7427 | if (TYPE_PTRMEMFUNC_FLAG (parm)) |
830bfa74 | 7428 | return unify (tparms, targs, TYPE_PTRMEMFUNC_FN_TYPE (parm), |
050367a3 | 7429 | arg, strict, explicit_mask); |
8d08fdba | 7430 | |
830bfa74 | 7431 | if (TREE_CODE (arg) != RECORD_TYPE) |
a4443a08 | 7432 | return 1; |
830bfa74 | 7433 | |
6467930b | 7434 | if (CLASSTYPE_TEMPLATE_INFO (parm) && uses_template_parms (parm)) |
5566b478 | 7435 | { |
6467930b | 7436 | tree t = NULL_TREE; |
830bfa74 MM |
7437 | if (strict & UNIFY_ALLOW_DERIVED) |
7438 | /* [temp.deduct.call] | |
7439 | ||
7440 | If P is a class, and P has the form template-id, then A | |
7441 | can be a derived class of the deduced A. Likewise, if | |
7442 | P is a pointer to a class of the form template-id, A | |
7443 | can be a pointer to a derived class pointed to by the | |
6df47b06 MM |
7444 | deduced A. |
7445 | ||
7446 | The call to get_template_base also handles the case | |
7447 | where PARM and ARG are the same type, i.e., where no | |
7448 | derivation is involved. */ | |
6467930b | 7449 | t = get_template_base (CLASSTYPE_TI_TEMPLATE (parm), arg); |
6df47b06 | 7450 | else if (CLASSTYPE_TEMPLATE_INFO (arg) |
9fbf56f7 MM |
7451 | && (CLASSTYPE_TI_TEMPLATE (parm) |
7452 | == CLASSTYPE_TI_TEMPLATE (arg))) | |
6df47b06 MM |
7453 | /* Perhaps PARM is something like S<U> and ARG is S<int>. |
7454 | Then, we should unify `int' and `U'. */ | |
6467930b | 7455 | t = arg; |
6df47b06 | 7456 | |
6467930b | 7457 | if (! t || t == error_mark_node) |
5566b478 | 7458 | return 1; |
6467930b | 7459 | |
830bfa74 MM |
7460 | return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm), |
7461 | CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE, | |
7462 | explicit_mask); | |
5566b478 | 7463 | } |
3bfdc719 MM |
7464 | else if (!same_type_p (TYPE_MAIN_VARIANT (parm), |
7465 | TYPE_MAIN_VARIANT (arg))) | |
5566b478 | 7466 | return 1; |
a4443a08 | 7467 | return 0; |
8d08fdba MS |
7468 | |
7469 | case METHOD_TYPE: | |
8d08fdba | 7470 | case FUNCTION_TYPE: |
830bfa74 | 7471 | if (TREE_CODE (arg) != TREE_CODE (parm)) |
8d08fdba | 7472 | return 1; |
830bfa74 MM |
7473 | |
7474 | if (unify (tparms, targs, TREE_TYPE (parm), | |
7475 | TREE_TYPE (arg), UNIFY_ALLOW_NONE, explicit_mask)) | |
28cbf42c | 7476 | return 1; |
386b8a85 | 7477 | return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm), |
3b3ba9f0 | 7478 | TYPE_ARG_TYPES (arg), 1, |
830bfa74 | 7479 | DEDUCE_EXACT, 0, explicit_mask); |
a4443a08 MS |
7480 | |
7481 | case OFFSET_TYPE: | |
7482 | if (TREE_CODE (arg) != OFFSET_TYPE) | |
7483 | return 1; | |
830bfa74 MM |
7484 | if (unify (tparms, targs, TYPE_OFFSET_BASETYPE (parm), |
7485 | TYPE_OFFSET_BASETYPE (arg), UNIFY_ALLOW_NONE, explicit_mask)) | |
a4443a08 | 7486 | return 1; |
830bfa74 MM |
7487 | return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg), |
7488 | UNIFY_ALLOW_NONE, explicit_mask); | |
a4443a08 | 7489 | |
f62dbf03 | 7490 | case CONST_DECL: |
830bfa74 | 7491 | if (arg != decl_constant_value (parm)) |
f62dbf03 JM |
7492 | return 1; |
7493 | return 0; | |
7494 | ||
027905b4 KL |
7495 | case TEMPLATE_DECL: |
7496 | /* Matched cases are handled by the ARG == PARM test above. */ | |
7497 | return 1; | |
7498 | ||
53929c47 JM |
7499 | case MINUS_EXPR: |
7500 | if (TREE_CODE (TREE_OPERAND (parm, 1)) == INTEGER_CST) | |
7501 | { | |
7502 | /* We handle this case specially, since it comes up with | |
7503 | arrays. In particular, something like: | |
7504 | ||
7505 | template <int N> void f(int (&x)[N]); | |
7506 | ||
7507 | Here, we are trying to unify the range type, which | |
7508 | looks like [0 ... (N - 1)]. */ | |
7509 | tree t, t1, t2; | |
7510 | t1 = TREE_OPERAND (parm, 0); | |
7511 | t2 = TREE_OPERAND (parm, 1); | |
7512 | ||
7513 | /* Should this be a regular fold? */ | |
7514 | t = maybe_fold_nontype_arg (build (PLUS_EXPR, | |
7515 | integer_type_node, | |
7516 | arg, t2)); | |
7517 | ||
7518 | return unify (tparms, targs, t1, t, UNIFY_ALLOW_NONE, | |
7519 | explicit_mask); | |
7520 | } | |
7521 | /* else fall through */ | |
7522 | ||
8d08fdba | 7523 | default: |
050367a3 MM |
7524 | if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (parm)))) |
7525 | { | |
7526 | /* We're looking at an expression. This can happen with | |
7527 | something like: | |
7528 | ||
7529 | template <int I> | |
7530 | void foo(S<I>, S<I + 2>); | |
7531 | ||
7532 | If the call looked like: | |
7533 | ||
7534 | foo(S<2>(), S<4>()); | |
7535 | ||
7536 | we would have already matched `I' with `2'. Now, we'd | |
7537 | like to know if `4' matches `I + 2'. So, we substitute | |
7538 | into that expression, and fold constants, in the hope of | |
7539 | figuring it out. */ | |
7540 | tree t = | |
7541 | maybe_fold_nontype_arg (tsubst_expr (parm, targs, NULL_TREE)); | |
53929c47 | 7542 | tree a = maybe_fold_nontype_arg (arg); |
050367a3 | 7543 | |
53929c47 | 7544 | if (!IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (t)))) |
050367a3 | 7545 | /* Good, we mangaged to simplify the exression. */ |
53929c47 | 7546 | return unify (tparms, targs, t, a, UNIFY_ALLOW_NONE, |
050367a3 MM |
7547 | explicit_mask); |
7548 | else | |
7549 | /* Bad, we couldn't simplify this. Assume it doesn't | |
7550 | unify. */ | |
7551 | return 1; | |
7552 | } | |
7553 | else | |
b4c4a9ec MM |
7554 | sorry ("use of `%s' in template type unification", |
7555 | tree_code_name [(int) TREE_CODE (parm)]); | |
050367a3 | 7556 | |
8d08fdba MS |
7557 | return 1; |
7558 | } | |
7559 | } | |
8d08fdba | 7560 | \f |
03d0f4af MM |
7561 | /* Called if RESULT is explicitly instantiated, or is a member of an |
7562 | explicitly instantiated class, or if using -frepo and the | |
7563 | instantiation of RESULT has been assigned to this file. */ | |
7564 | ||
faae18ab | 7565 | void |
5566b478 | 7566 | mark_decl_instantiated (result, extern_p) |
faae18ab MS |
7567 | tree result; |
7568 | int extern_p; | |
7569 | { | |
75650646 MM |
7570 | if (TREE_CODE (result) != FUNCTION_DECL) |
7571 | /* The TREE_PUBLIC flag for function declarations will have been | |
7572 | set correctly by tsubst. */ | |
7573 | TREE_PUBLIC (result) = 1; | |
faae18ab MS |
7574 | |
7575 | if (! extern_p) | |
7576 | { | |
7577 | DECL_INTERFACE_KNOWN (result) = 1; | |
7578 | DECL_NOT_REALLY_EXTERN (result) = 1; | |
a7d87521 | 7579 | |
1a408d07 JM |
7580 | /* Always make artificials weak. */ |
7581 | if (DECL_ARTIFICIAL (result) && flag_weak) | |
7582 | comdat_linkage (result); | |
a7d87521 JM |
7583 | /* For WIN32 we also want to put explicit instantiations in |
7584 | linkonce sections. */ | |
1a408d07 | 7585 | else if (TREE_PUBLIC (result)) |
b385c841 | 7586 | maybe_make_one_only (result); |
faae18ab | 7587 | } |
f49422da MS |
7588 | else if (TREE_CODE (result) == FUNCTION_DECL) |
7589 | mark_inline_for_output (result); | |
faae18ab MS |
7590 | } |
7591 | ||
e1467ff2 MM |
7592 | /* Given two function templates PAT1 and PAT2, and explicit template |
7593 | arguments EXPLICIT_ARGS return: | |
6467930b MS |
7594 | |
7595 | 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order]. | |
7596 | -1 if PAT2 is more specialized than PAT1. | |
7597 | 0 if neither is more specialized. */ | |
7598 | ||
7599 | int | |
e1467ff2 MM |
7600 | more_specialized (pat1, pat2, explicit_args) |
7601 | tree pat1, pat2, explicit_args; | |
6467930b | 7602 | { |
98c1c668 | 7603 | tree targs; |
73aad9b9 | 7604 | int winner = 0; |
6467930b | 7605 | |
76b9a14d | 7606 | targs = get_bindings_overload (pat1, pat2, explicit_args); |
73aad9b9 | 7607 | if (targs) |
36a117a5 | 7608 | --winner; |
6467930b | 7609 | |
76b9a14d | 7610 | targs = get_bindings_overload (pat2, pat1, explicit_args); |
73aad9b9 | 7611 | if (targs) |
36a117a5 | 7612 | ++winner; |
6467930b | 7613 | |
73aad9b9 JM |
7614 | return winner; |
7615 | } | |
6467930b | 7616 | |
73aad9b9 | 7617 | /* Given two class template specialization list nodes PAT1 and PAT2, return: |
6467930b | 7618 | |
73aad9b9 JM |
7619 | 1 if PAT1 is more specialized than PAT2 as described in [temp.class.order]. |
7620 | -1 if PAT2 is more specialized than PAT1. | |
7621 | 0 if neither is more specialized. */ | |
7622 | ||
7623 | int | |
7624 | more_specialized_class (pat1, pat2) | |
7625 | tree pat1, pat2; | |
7626 | { | |
7627 | tree targs; | |
7628 | int winner = 0; | |
7629 | ||
36a117a5 MM |
7630 | targs = get_class_bindings (TREE_VALUE (pat1), TREE_PURPOSE (pat1), |
7631 | TREE_PURPOSE (pat2)); | |
73aad9b9 JM |
7632 | if (targs) |
7633 | --winner; | |
7634 | ||
36a117a5 MM |
7635 | targs = get_class_bindings (TREE_VALUE (pat2), TREE_PURPOSE (pat2), |
7636 | TREE_PURPOSE (pat1)); | |
73aad9b9 | 7637 | if (targs) |
6467930b MS |
7638 | ++winner; |
7639 | ||
7640 | return winner; | |
7641 | } | |
73aad9b9 JM |
7642 | |
7643 | /* Return the template arguments that will produce the function signature | |
e1467ff2 | 7644 | DECL from the function template FN, with the explicit template |
76b9a14d JM |
7645 | arguments EXPLICIT_ARGS. If CHECK_RETTYPE is 1, the return type must |
7646 | also match. */ | |
73aad9b9 | 7647 | |
76b9a14d JM |
7648 | static tree |
7649 | get_bindings_real (fn, decl, explicit_args, check_rettype) | |
e1467ff2 | 7650 | tree fn, decl, explicit_args; |
76b9a14d | 7651 | int check_rettype; |
73aad9b9 | 7652 | { |
98c1c668 | 7653 | int ntparms = DECL_NTPARMS (fn); |
e66d884e | 7654 | tree targs = make_scratch_vec (ntparms); |
d7684f2d MM |
7655 | tree decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl)); |
7656 | tree extra_fn_arg = NULL_TREE; | |
98c1c668 JM |
7657 | int i; |
7658 | ||
d7684f2d MM |
7659 | if (DECL_STATIC_FUNCTION_P (fn) |
7660 | && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)) | |
7661 | { | |
7662 | /* Sometimes we are trying to figure out what's being | |
7663 | specialized by a declaration that looks like a method, and it | |
7664 | turns out to be a static member function. */ | |
7665 | if (CLASSTYPE_TEMPLATE_INFO (DECL_REAL_CONTEXT (fn)) | |
7666 | && !is_member_template (fn)) | |
7667 | /* The natural thing to do here seems to be to remove the | |
7668 | spurious `this' parameter from the DECL, but that prevents | |
7669 | unification from making use of the class type. So, | |
7670 | instead, we have fn_type_unification add to the parameters | |
7671 | for FN. */ | |
7672 | extra_fn_arg = build_pointer_type (DECL_REAL_CONTEXT (fn)); | |
7673 | else | |
7674 | /* In this case, though, adding the extra_fn_arg can confuse | |
7675 | things, so we remove from decl_arg_types instead. */ | |
7676 | decl_arg_types = TREE_CHAIN (decl_arg_types); | |
7677 | } | |
7678 | ||
e1467ff2 | 7679 | i = fn_type_unification (fn, explicit_args, targs, |
d7684f2d | 7680 | decl_arg_types, |
98c1c668 | 7681 | TREE_TYPE (TREE_TYPE (decl)), |
830bfa74 | 7682 | DEDUCE_EXACT, |
d7684f2d | 7683 | extra_fn_arg); |
98c1c668 | 7684 | |
76b9a14d JM |
7685 | if (i != 0) |
7686 | return NULL_TREE; | |
7687 | ||
7688 | if (check_rettype) | |
e1467ff2 MM |
7689 | { |
7690 | /* Check to see that the resulting return type is also OK. */ | |
36a117a5 | 7691 | tree t = tsubst (TREE_TYPE (TREE_TYPE (fn)), targs, |
e6f1275f | 7692 | NULL_TREE); |
e1467ff2 | 7693 | |
3bfdc719 | 7694 | if (!same_type_p (t, TREE_TYPE (TREE_TYPE (decl)))) |
e1467ff2 | 7695 | return NULL_TREE; |
e1467ff2 MM |
7696 | } |
7697 | ||
76b9a14d JM |
7698 | return targs; |
7699 | } | |
7700 | ||
7701 | /* For most uses, we want to check the return type. */ | |
7702 | ||
7703 | tree | |
7704 | get_bindings (fn, decl, explicit_args) | |
7705 | tree fn, decl, explicit_args; | |
7706 | { | |
7707 | return get_bindings_real (fn, decl, explicit_args, 1); | |
7708 | } | |
7709 | ||
7710 | /* But for more_specialized, we only care about the parameter types. */ | |
7711 | ||
7712 | static tree | |
7713 | get_bindings_overload (fn, decl, explicit_args) | |
7714 | tree fn, decl, explicit_args; | |
7715 | { | |
7716 | return get_bindings_real (fn, decl, explicit_args, 0); | |
73aad9b9 JM |
7717 | } |
7718 | ||
36a117a5 MM |
7719 | /* Return the innermost template arguments that, when applied to a |
7720 | template specialization whose innermost template parameters are | |
7721 | TPARMS, and whose specialization arguments are ARGS, yield the | |
7722 | ARGS. | |
7723 | ||
7724 | For example, suppose we have: | |
7725 | ||
7726 | template <class T, class U> struct S {}; | |
7727 | template <class T> struct S<T*, int> {}; | |
7728 | ||
7729 | Then, suppose we want to get `S<double*, int>'. The TPARMS will be | |
7730 | {T}, the PARMS will be {T*, int} and the ARGS will be {double*, | |
7731 | int}. The resulting vector will be {double}, indicating that `T' | |
7732 | is bound to `double'. */ | |
7733 | ||
bd6dd845 | 7734 | static tree |
36a117a5 MM |
7735 | get_class_bindings (tparms, parms, args) |
7736 | tree tparms, parms, args; | |
73aad9b9 | 7737 | { |
3b3ba9f0 | 7738 | int i, ntparms = TREE_VEC_LENGTH (tparms); |
73aad9b9 JM |
7739 | tree vec = make_temp_vec (ntparms); |
7740 | ||
36a117a5 | 7741 | args = innermost_args (args); |
8d019cef | 7742 | |
73aad9b9 JM |
7743 | for (i = 0; i < TREE_VEC_LENGTH (parms); ++i) |
7744 | { | |
830bfa74 | 7745 | switch (unify (tparms, vec, |
050367a3 | 7746 | TREE_VEC_ELT (parms, i), TREE_VEC_ELT (args, i), |
830bfa74 | 7747 | UNIFY_ALLOW_NONE, 0)) |
73aad9b9 JM |
7748 | { |
7749 | case 0: | |
7750 | break; | |
7751 | case 1: | |
7752 | return NULL_TREE; | |
7753 | } | |
7754 | } | |
7755 | ||
7756 | for (i = 0; i < ntparms; ++i) | |
7757 | if (! TREE_VEC_ELT (vec, i)) | |
7758 | return NULL_TREE; | |
7759 | ||
7760 | return vec; | |
7761 | } | |
7762 | ||
7763 | /* Return the most specialized of the list of templates in FNS that can | |
e1467ff2 MM |
7764 | produce an instantiation matching DECL, given the explicit template |
7765 | arguments EXPLICIT_ARGS. */ | |
73aad9b9 JM |
7766 | |
7767 | tree | |
e1467ff2 MM |
7768 | most_specialized (fns, decl, explicit_args) |
7769 | tree fns, decl, explicit_args; | |
73aad9b9 | 7770 | { |
36a117a5 MM |
7771 | tree candidates = NULL_TREE; |
7772 | tree fn, champ, args; | |
73aad9b9 JM |
7773 | int fate; |
7774 | ||
36a117a5 | 7775 | for (fn = fns; fn; fn = TREE_CHAIN (fn)) |
73aad9b9 | 7776 | { |
36a117a5 MM |
7777 | tree candidate = TREE_VALUE (fn); |
7778 | ||
7779 | args = get_bindings (candidate, decl, explicit_args); | |
73aad9b9 | 7780 | if (args) |
36a117a5 MM |
7781 | candidates = scratch_tree_cons (NULL_TREE, candidate, |
7782 | candidates); | |
73aad9b9 JM |
7783 | } |
7784 | ||
36a117a5 | 7785 | if (!candidates) |
73aad9b9 JM |
7786 | return NULL_TREE; |
7787 | ||
36a117a5 MM |
7788 | champ = TREE_VALUE (candidates); |
7789 | for (fn = TREE_CHAIN (candidates); fn; fn = TREE_CHAIN (fn)) | |
73aad9b9 | 7790 | { |
e1467ff2 | 7791 | fate = more_specialized (champ, TREE_VALUE (fn), explicit_args); |
73aad9b9 JM |
7792 | if (fate == 1) |
7793 | ; | |
7794 | else | |
7795 | { | |
7796 | if (fate == 0) | |
7797 | { | |
7798 | fn = TREE_CHAIN (fn); | |
7799 | if (! fn) | |
7800 | return error_mark_node; | |
7801 | } | |
7802 | champ = TREE_VALUE (fn); | |
7803 | } | |
7804 | } | |
7805 | ||
36a117a5 | 7806 | for (fn = candidates; fn && TREE_VALUE (fn) != champ; fn = TREE_CHAIN (fn)) |
73aad9b9 | 7807 | { |
e1467ff2 | 7808 | fate = more_specialized (champ, TREE_VALUE (fn), explicit_args); |
73aad9b9 JM |
7809 | if (fate != 1) |
7810 | return error_mark_node; | |
7811 | } | |
7812 | ||
7813 | return champ; | |
7814 | } | |
7815 | ||
36a117a5 MM |
7816 | /* If DECL is a specialization of some template, return the most |
7817 | general such template. For example, given: | |
7818 | ||
7819 | template <class T> struct S { template <class U> void f(U); }; | |
7820 | ||
7821 | if TMPL is `template <class U> void S<int>::f(U)' this will return | |
7822 | the full template. This function will not trace past partial | |
7823 | specializations, however. For example, given in addition: | |
7824 | ||
7825 | template <class T> struct S<T*> { template <class U> void f(U); }; | |
7826 | ||
7827 | if TMPL is `template <class U> void S<int*>::f(U)' this will return | |
7828 | `template <class T> template <class U> S<T*>::f(U)'. */ | |
73aad9b9 JM |
7829 | |
7830 | tree | |
36a117a5 MM |
7831 | most_general_template (decl) |
7832 | tree decl; | |
73aad9b9 | 7833 | { |
36a117a5 MM |
7834 | while (DECL_TEMPLATE_INFO (decl)) |
7835 | decl = DECL_TI_TEMPLATE (decl); | |
7836 | ||
7837 | return decl; | |
7838 | } | |
7839 | ||
7840 | /* Return the most specialized of the class template specializations | |
7841 | of TMPL which can produce an instantiation matching ARGS, or | |
7842 | error_mark_node if the choice is ambiguous. */ | |
7843 | ||
7844 | tree | |
7845 | most_specialized_class (tmpl, args) | |
7846 | tree tmpl; | |
7847 | tree args; | |
7848 | { | |
7849 | tree list = NULL_TREE; | |
7850 | tree t; | |
7851 | tree champ; | |
73aad9b9 JM |
7852 | int fate; |
7853 | ||
36a117a5 MM |
7854 | tmpl = most_general_template (tmpl); |
7855 | for (t = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); t; t = TREE_CHAIN (t)) | |
73aad9b9 | 7856 | { |
36a117a5 MM |
7857 | tree spec_args |
7858 | = get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t), args); | |
7859 | if (spec_args) | |
73aad9b9 JM |
7860 | { |
7861 | list = decl_tree_cons (TREE_PURPOSE (t), TREE_VALUE (t), list); | |
7862 | TREE_TYPE (list) = TREE_TYPE (t); | |
7863 | } | |
7864 | } | |
7865 | ||
7866 | if (! list) | |
7867 | return NULL_TREE; | |
7868 | ||
7869 | t = list; | |
7870 | champ = t; | |
7871 | t = TREE_CHAIN (t); | |
7872 | for (; t; t = TREE_CHAIN (t)) | |
7873 | { | |
7874 | fate = more_specialized_class (champ, t); | |
7875 | if (fate == 1) | |
7876 | ; | |
7877 | else | |
7878 | { | |
7879 | if (fate == 0) | |
7880 | { | |
7881 | t = TREE_CHAIN (t); | |
7882 | if (! t) | |
7883 | return error_mark_node; | |
7884 | } | |
7885 | champ = t; | |
7886 | } | |
7887 | } | |
7888 | ||
7889 | for (t = list; t && t != champ; t = TREE_CHAIN (t)) | |
7890 | { | |
85b71cf2 | 7891 | fate = more_specialized_class (champ, t); |
73aad9b9 JM |
7892 | if (fate != 1) |
7893 | return error_mark_node; | |
7894 | } | |
7895 | ||
7896 | return champ; | |
7897 | } | |
7898 | ||
8d08fdba | 7899 | /* called from the parser. */ |
e92cc029 | 7900 | |
8d08fdba | 7901 | void |
6633d636 | 7902 | do_decl_instantiation (declspecs, declarator, storage) |
f0e01782 | 7903 | tree declspecs, declarator, storage; |
8d08fdba | 7904 | { |
c11b6f21 | 7905 | tree decl = grokdeclarator (declarator, declspecs, NORMAL, 0, NULL_TREE); |
8d08fdba | 7906 | tree result = NULL_TREE; |
faae18ab | 7907 | int extern_p = 0; |
e8abc66f | 7908 | |
ec255269 MS |
7909 | if (! DECL_LANG_SPECIFIC (decl)) |
7910 | { | |
8251199e | 7911 | cp_error ("explicit instantiation of non-template `%#D'", decl); |
ec255269 MS |
7912 | return; |
7913 | } | |
03d0f4af | 7914 | else if (TREE_CODE (decl) == VAR_DECL) |
6633d636 | 7915 | { |
03d0f4af MM |
7916 | /* There is an asymmetry here in the way VAR_DECLs and |
7917 | FUNCTION_DECLs are handled by grokdeclarator. In the case of | |
7918 | the latter, the DECL we get back will be marked as a | |
7919 | template instantiation, and the appropriate | |
7920 | DECL_TEMPLATE_INFO will be set up. This does not happen for | |
7921 | VAR_DECLs so we do the lookup here. Probably, grokdeclarator | |
7922 | should handle VAR_DECLs as it currently handles | |
7923 | FUNCTION_DECLs. */ | |
6633d636 MS |
7924 | result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, 0); |
7925 | if (result && TREE_CODE (result) != VAR_DECL) | |
03d0f4af | 7926 | { |
8251199e | 7927 | cp_error ("no matching template for `%D' found", result); |
03d0f4af MM |
7928 | return; |
7929 | } | |
6633d636 MS |
7930 | } |
7931 | else if (TREE_CODE (decl) != FUNCTION_DECL) | |
7932 | { | |
8251199e | 7933 | cp_error ("explicit instantiation of `%#D'", decl); |
6633d636 MS |
7934 | return; |
7935 | } | |
03d0f4af MM |
7936 | else |
7937 | result = decl; | |
672476cb | 7938 | |
03d0f4af MM |
7939 | /* Check for various error cases. Note that if the explicit |
7940 | instantiation is legal the RESULT will currently be marked as an | |
7941 | *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set | |
7942 | until we get here. */ | |
7943 | ||
7944 | if (DECL_TEMPLATE_SPECIALIZATION (result)) | |
672476cb | 7945 | { |
03d0f4af MM |
7946 | /* [temp.spec] |
7947 | ||
7948 | No program shall both explicitly instantiate and explicitly | |
7949 | specialize a template. */ | |
8251199e JM |
7950 | cp_error ("explicit instantiation of `%#D' after", result); |
7951 | cp_error_at ("explicit specialization here", result); | |
672476cb MM |
7952 | return; |
7953 | } | |
03d0f4af MM |
7954 | else if (DECL_EXPLICIT_INSTANTIATION (result)) |
7955 | { | |
7956 | /* [temp.spec] | |
98c1c668 | 7957 | |
03d0f4af MM |
7958 | No program shall explicitly instantiate any template more |
7959 | than once. | |
7960 | ||
7961 | We check DECL_INTERFACE_KNOWN so as not to complain when the | |
7962 | first instantiation was `extern' and the second is not, and | |
7963 | EXTERN_P for the opposite case. */ | |
7964 | if (DECL_INTERFACE_KNOWN (result) && !extern_p) | |
8251199e | 7965 | cp_error ("duplicate explicit instantiation of `%#D'", result); |
03d0f4af MM |
7966 | |
7967 | /* If we've already instantiated the template, just return now. */ | |
7968 | if (DECL_INTERFACE_KNOWN (result)) | |
7969 | return; | |
7970 | } | |
7971 | else if (!DECL_IMPLICIT_INSTANTIATION (result)) | |
faae18ab | 7972 | { |
8251199e | 7973 | cp_error ("no matching template for `%D' found", result); |
faae18ab MS |
7974 | return; |
7975 | } | |
03d0f4af | 7976 | else if (!DECL_TEMPLATE_INFO (result)) |
6633d636 | 7977 | { |
8251199e | 7978 | cp_pedwarn ("explicit instantiation of non-template `%#D'", result); |
6633d636 MS |
7979 | return; |
7980 | } | |
7981 | ||
a0a33927 MS |
7982 | if (flag_external_templates) |
7983 | return; | |
7984 | ||
f0e01782 | 7985 | if (storage == NULL_TREE) |
00595019 | 7986 | ; |
faae18ab | 7987 | else if (storage == ridpointers[(int) RID_EXTERN]) |
03d0f4af MM |
7988 | { |
7989 | if (pedantic) | |
8251199e | 7990 | cp_pedwarn ("ANSI C++ forbids the use of `extern' on explicit instantiations"); |
03d0f4af MM |
7991 | extern_p = 1; |
7992 | } | |
f0e01782 | 7993 | else |
8251199e | 7994 | cp_error ("storage class `%D' applied to template instantiation", |
f0e01782 | 7995 | storage); |
5566b478 | 7996 | |
03d0f4af | 7997 | SET_DECL_EXPLICIT_INSTANTIATION (result); |
5566b478 | 7998 | mark_decl_instantiated (result, extern_p); |
44a8d0b3 | 7999 | repo_template_instantiated (result, extern_p); |
c91a56d2 MS |
8000 | if (! extern_p) |
8001 | instantiate_decl (result); | |
7177d104 MS |
8002 | } |
8003 | ||
faae18ab MS |
8004 | void |
8005 | mark_class_instantiated (t, extern_p) | |
8006 | tree t; | |
8007 | int extern_p; | |
8008 | { | |
8009 | SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t); | |
eb773359 JM |
8010 | SET_CLASSTYPE_INTERFACE_KNOWN (t); |
8011 | CLASSTYPE_INTERFACE_ONLY (t) = extern_p; | |
faae18ab MS |
8012 | CLASSTYPE_VTABLE_NEEDS_WRITING (t) = ! extern_p; |
8013 | TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p; | |
8014 | if (! extern_p) | |
8015 | { | |
8016 | CLASSTYPE_DEBUG_REQUESTED (t) = 1; | |
8017 | rest_of_type_compilation (t, 1); | |
8018 | } | |
8019 | } | |
e8abc66f | 8020 | |
7177d104 | 8021 | void |
ca79f85d JM |
8022 | do_type_instantiation (t, storage) |
8023 | tree t, storage; | |
7177d104 | 8024 | { |
e8abc66f MS |
8025 | int extern_p = 0; |
8026 | int nomem_p = 0; | |
5566b478 MS |
8027 | int static_p = 0; |
8028 | ||
ca79f85d JM |
8029 | if (TREE_CODE (t) == TYPE_DECL) |
8030 | t = TREE_TYPE (t); | |
8031 | ||
8032 | if (! IS_AGGR_TYPE (t) || ! CLASSTYPE_TEMPLATE_INFO (t)) | |
8033 | { | |
8251199e | 8034 | cp_error ("explicit instantiation of non-template type `%T'", t); |
ca79f85d JM |
8035 | return; |
8036 | } | |
8037 | ||
5566b478 | 8038 | complete_type (t); |
7177d104 | 8039 | |
a292b002 MS |
8040 | /* With -fexternal-templates, explicit instantiations are treated the same |
8041 | as implicit ones. */ | |
a0a33927 MS |
8042 | if (flag_external_templates) |
8043 | return; | |
8044 | ||
f0e01782 MS |
8045 | if (TYPE_SIZE (t) == NULL_TREE) |
8046 | { | |
8251199e | 8047 | cp_error ("explicit instantiation of `%#T' before definition of template", |
f0e01782 MS |
8048 | t); |
8049 | return; | |
8050 | } | |
8051 | ||
03d0f4af | 8052 | if (storage != NULL_TREE) |
f0e01782 | 8053 | { |
03d0f4af | 8054 | if (pedantic) |
8251199e JM |
8055 | cp_pedwarn("ANSI C++ forbids the use of `%s' on explicit instantiations", |
8056 | IDENTIFIER_POINTER (storage)); | |
03d0f4af MM |
8057 | |
8058 | if (storage == ridpointers[(int) RID_INLINE]) | |
8059 | nomem_p = 1; | |
8060 | else if (storage == ridpointers[(int) RID_EXTERN]) | |
8061 | extern_p = 1; | |
8062 | else if (storage == ridpointers[(int) RID_STATIC]) | |
8063 | static_p = 1; | |
8064 | else | |
8065 | { | |
8251199e | 8066 | cp_error ("storage class `%D' applied to template instantiation", |
03d0f4af MM |
8067 | storage); |
8068 | extern_p = 0; | |
8069 | } | |
f0e01782 MS |
8070 | } |
8071 | ||
03d0f4af MM |
8072 | if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t)) |
8073 | { | |
8074 | /* [temp.spec] | |
a292b002 | 8075 | |
03d0f4af MM |
8076 | No program shall both explicitly instantiate and explicitly |
8077 | specialize a template. */ | |
8251199e JM |
8078 | cp_error ("explicit instantiation of `%#T' after", t); |
8079 | cp_error_at ("explicit specialization here", t); | |
03d0f4af MM |
8080 | return; |
8081 | } | |
8082 | else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t)) | |
44a8d0b3 | 8083 | { |
03d0f4af MM |
8084 | /* [temp.spec] |
8085 | ||
8086 | No program shall explicitly instantiate any template more | |
8087 | than once. | |
8088 | ||
8089 | If CLASSTYPE_INTERFACE_ONLY, then the first explicit | |
8090 | instantiation was `extern', and if EXTERN_P then the second | |
8091 | is. Both cases are OK. */ | |
8092 | if (!CLASSTYPE_INTERFACE_ONLY (t) && !extern_p) | |
8251199e | 8093 | cp_error ("duplicate explicit instantiation of `%#T'", t); |
03d0f4af MM |
8094 | |
8095 | /* If we've already instantiated the template, just return now. */ | |
8096 | if (!CLASSTYPE_INTERFACE_ONLY (t)) | |
8097 | return; | |
44a8d0b3 | 8098 | } |
e8abc66f | 8099 | |
03d0f4af MM |
8100 | mark_class_instantiated (t, extern_p); |
8101 | repo_template_instantiated (t, extern_p); | |
8102 | ||
e8abc66f MS |
8103 | if (nomem_p) |
8104 | return; | |
8105 | ||
7177d104 | 8106 | { |
db5ae43f | 8107 | tree tmp; |
5566b478 | 8108 | |
03d0f4af MM |
8109 | /* In contrast to implicit instantiation, where only the |
8110 | declarations, and not the definitions, of members are | |
8111 | instantiated, we have here: | |
8112 | ||
8113 | [temp.explicit] | |
8114 | ||
8115 | The explicit instantiation of a class template specialization | |
8116 | implies the instantiation of all of its members not | |
8117 | previously explicitly specialized in the translation unit | |
8118 | containing the explicit instantiation. | |
8119 | ||
8120 | Of course, we can't instantiate member template classes, since | |
8121 | we don't have any arguments for them. Note that the standard | |
8122 | is unclear on whether the instatiation of the members are | |
8123 | *explicit* instantiations or not. We choose to be generous, | |
8124 | and not set DECL_EXPLICIT_INSTANTIATION. Therefore, we allow | |
8125 | the explicit instantiation of a class where some of the members | |
8126 | have no definition in the current translation unit. */ | |
8127 | ||
5566b478 MS |
8128 | if (! static_p) |
8129 | for (tmp = TYPE_METHODS (t); tmp; tmp = TREE_CHAIN (tmp)) | |
a7d87521 | 8130 | if (TREE_CODE (tmp) == FUNCTION_DECL |
1f109f0f | 8131 | && DECL_TEMPLATE_INSTANTIATION (tmp)) |
5566b478 MS |
8132 | { |
8133 | mark_decl_instantiated (tmp, extern_p); | |
8134 | repo_template_instantiated (tmp, extern_p); | |
8135 | if (! extern_p) | |
8136 | instantiate_decl (tmp); | |
8137 | } | |
8138 | ||
8139 | for (tmp = TYPE_FIELDS (t); tmp; tmp = TREE_CHAIN (tmp)) | |
8140 | if (TREE_CODE (tmp) == VAR_DECL && DECL_TEMPLATE_INSTANTIATION (tmp)) | |
863adfc0 | 8141 | { |
5566b478 | 8142 | mark_decl_instantiated (tmp, extern_p); |
863adfc0 | 8143 | repo_template_instantiated (tmp, extern_p); |
5566b478 MS |
8144 | if (! extern_p) |
8145 | instantiate_decl (tmp); | |
863adfc0 | 8146 | } |
7177d104 | 8147 | |
a292b002 | 8148 | for (tmp = CLASSTYPE_TAGS (t); tmp; tmp = TREE_CHAIN (tmp)) |
e4a84209 MM |
8149 | if (IS_AGGR_TYPE (TREE_VALUE (tmp)) |
8150 | && !uses_template_parms (CLASSTYPE_TI_ARGS (TREE_VALUE (tmp)))) | |
f376e137 | 8151 | do_type_instantiation (TYPE_MAIN_DECL (TREE_VALUE (tmp)), storage); |
a292b002 | 8152 | } |
8d08fdba | 8153 | } |
a28e3c7f | 8154 | |
36a117a5 MM |
8155 | /* Given a function DECL, which is a specialization of TMPL, modify |
8156 | DECL to be a re-instantiation of TMPL with the same template | |
8157 | arguments. TMPL should be the template into which tsubst'ing | |
8158 | should occur for DECL, not the most general template. | |
830bfa74 MM |
8159 | |
8160 | One reason for doing this is a scenario like this: | |
8161 | ||
8162 | template <class T> | |
8163 | void f(const T&, int i); | |
8164 | ||
8165 | void g() { f(3, 7); } | |
8166 | ||
8167 | template <class T> | |
8168 | void f(const T& t, const int i) { } | |
8169 | ||
8170 | Note that when the template is first instantiated, with | |
8171 | instantiate_template, the resulting DECL will have no name for the | |
8172 | first parameter, and the wrong type for the second. So, when we go | |
8173 | to instantiate the DECL, we regenerate it. */ | |
8174 | ||
8175 | void | |
8176 | regenerate_decl_from_template (decl, tmpl) | |
8177 | tree decl; | |
8178 | tree tmpl; | |
8179 | { | |
8180 | tree args; | |
830bfa74 MM |
8181 | tree code_pattern; |
8182 | tree new_decl; | |
36a117a5 MM |
8183 | tree gen_tmpl; |
8184 | int unregistered; | |
830bfa74 MM |
8185 | |
8186 | args = DECL_TI_ARGS (decl); | |
8187 | code_pattern = DECL_TEMPLATE_RESULT (tmpl); | |
8188 | ||
8251199e JM |
8189 | /* Unregister the specialization so that when we tsubst we will not |
8190 | just return DECL. We don't have to unregister DECL from TMPL | |
8191 | because if would only be registered there if it were a partial | |
8192 | instantiation of a specialization, which it isn't: it's a full | |
8193 | instantiation. */ | |
36a117a5 MM |
8194 | gen_tmpl = most_general_template (tmpl); |
8195 | unregistered = unregister_specialization (decl, gen_tmpl); | |
8196 | ||
8197 | /* If the DECL was not unregistered then something peculiar is | |
8198 | happening: we created a specialization but did not call | |
8199 | register_specialization for it. */ | |
8200 | my_friendly_assert (unregistered, 0); | |
8201 | ||
61a127b3 MM |
8202 | if (TREE_CODE (decl) == VAR_DECL) |
8203 | /* Make sure that we can see identifiers, and compute access | |
8204 | correctly, for the class members used in the declaration of | |
8205 | this static variable. */ | |
8206 | pushclass (DECL_CONTEXT (decl), 2); | |
8207 | ||
d8c4447d MM |
8208 | /* Do the substitution to get the new declaration. */ |
8209 | new_decl = tsubst (code_pattern, args, NULL_TREE); | |
830bfa74 MM |
8210 | |
8211 | if (TREE_CODE (decl) == VAR_DECL) | |
8212 | { | |
8213 | /* Set up DECL_INITIAL, since tsubst doesn't. */ | |
830bfa74 | 8214 | DECL_INITIAL (new_decl) = |
d8c4447d | 8215 | tsubst_expr (DECL_INITIAL (code_pattern), args, |
830bfa74 | 8216 | DECL_TI_TEMPLATE (decl)); |
61a127b3 | 8217 | /* Pop the class context we pushed above. */ |
830bfa74 MM |
8218 | popclass (1); |
8219 | } | |
8220 | ||
8221 | if (TREE_CODE (decl) == FUNCTION_DECL) | |
36a117a5 MM |
8222 | /* Convince duplicate_decls to use the DECL_ARGUMENTS from the |
8223 | new decl. */ | |
8224 | DECL_INITIAL (new_decl) = error_mark_node; | |
8225 | ||
8226 | /* The immediate parent of the new template is still whatever it was | |
8227 | before, even though tsubst sets DECL_TI_TEMPLATE up as the most | |
8228 | general template. We also reset the DECL_ASSEMBLER_NAME since | |
8229 | tsubst always calculates the name as if the function in question | |
8230 | were really a template instance, and sometimes, with friend | |
8231 | functions, this is not so. See tsubst_friend_function for | |
8232 | details. */ | |
8233 | DECL_TI_TEMPLATE (new_decl) = DECL_TI_TEMPLATE (decl); | |
8234 | DECL_ASSEMBLER_NAME (new_decl) = DECL_ASSEMBLER_NAME (decl); | |
8235 | DECL_RTL (new_decl) = DECL_RTL (decl); | |
8236 | ||
8237 | /* Call duplicate decls to merge the old and new declarations. */ | |
830bfa74 MM |
8238 | duplicate_decls (new_decl, decl); |
8239 | ||
8240 | if (TREE_CODE (decl) == FUNCTION_DECL) | |
8241 | DECL_INITIAL (new_decl) = NULL_TREE; | |
36a117a5 MM |
8242 | |
8243 | /* Now, re-register the specialization. */ | |
8244 | register_specialization (decl, gen_tmpl, args); | |
830bfa74 MM |
8245 | } |
8246 | ||
f84b4be9 JM |
8247 | /* Produce the definition of D, a _DECL generated from a template. */ |
8248 | ||
a28e3c7f | 8249 | tree |
5566b478 MS |
8250 | instantiate_decl (d) |
8251 | tree d; | |
a28e3c7f | 8252 | { |
36a117a5 MM |
8253 | tree tmpl = DECL_TI_TEMPLATE (d); |
8254 | tree args = DECL_TI_ARGS (d); | |
830bfa74 | 8255 | tree td; |
36a117a5 MM |
8256 | tree code_pattern; |
8257 | tree spec; | |
8258 | tree gen_tmpl; | |
5566b478 | 8259 | int nested = in_function_p (); |
5566b478 | 8260 | int pattern_defined; |
c27be9b9 JM |
8261 | int line = lineno; |
8262 | char *file = input_filename; | |
5566b478 | 8263 | |
36a117a5 MM |
8264 | /* This function should only be used to instantiate templates for |
8265 | functions and static member variables. */ | |
8266 | my_friendly_assert (TREE_CODE (d) == FUNCTION_DECL | |
8267 | || TREE_CODE (d) == VAR_DECL, 0); | |
8268 | ||
8269 | if ((TREE_CODE (d) == FUNCTION_DECL && DECL_INITIAL (d)) | |
8270 | || (TREE_CODE (d) == VAR_DECL && !DECL_IN_AGGR_P (d))) | |
03d0f4af MM |
8271 | /* D has already been instantiated. It might seem reasonable to |
8272 | check whether or not D is an explict instantiation, and, if so, | |
8273 | stop here. But when an explicit instantiation is deferred | |
8274 | until the end of the compilation, DECL_EXPLICIT_INSTANTIATION | |
8275 | is set, even though we still need to do the instantiation. */ | |
36a117a5 MM |
8276 | return d; |
8277 | ||
8278 | /* If we already have a specialization of this declaration, then | |
8279 | there's no reason to instantiate it. Note that | |
8280 | retrieve_specialization gives us both instantiations and | |
8281 | specializations, so we must explicitly check | |
8282 | DECL_TEMPLATE_SPECIALIZATION. */ | |
8283 | gen_tmpl = most_general_template (tmpl); | |
8284 | spec = retrieve_specialization (gen_tmpl, args); | |
8285 | if (spec != NULL_TREE && DECL_TEMPLATE_SPECIALIZATION (spec)) | |
8286 | return spec; | |
8287 | ||
8288 | /* This needs to happen before any tsubsting. */ | |
8289 | if (! push_tinst_level (d)) | |
8290 | return d; | |
8291 | ||
4d85e00e MM |
8292 | /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern |
8293 | for the instantiation. This is not always the most general | |
8294 | template. Consider, for example: | |
8295 | ||
8296 | template <class T> | |
8297 | struct S { template <class U> void f(); | |
8298 | template <> void f<int>(); }; | |
8299 | ||
8300 | and an instantiation of S<double>::f<int>. We want TD to be the | |
8301 | specialization S<T>::f<int>, not the more general S<T>::f<U>. */ | |
8302 | td = tmpl; | |
8303 | for (td = tmpl; | |
8304 | /* An instantiation cannot have a definition, so we need a | |
8305 | more general template. */ | |
8306 | DECL_TEMPLATE_INSTANTIATION (td) | |
8307 | /* We must also deal with friend templates. Given: | |
8308 | ||
8309 | template <class T> struct S { | |
8310 | template <class U> friend void f() {}; | |
8311 | }; | |
8312 | ||
8313 | S<int>::f<U> say, is not an instantiation of S<T>::f<U>, | |
8314 | so far as the language is concerned, but that's still | |
8315 | where we get the pattern for the instantiation from. On | |
8316 | ther hand, if the definition comes outside the class, say: | |
8317 | ||
8318 | template <class T> struct S { | |
8319 | template <class U> friend void f(); | |
8320 | }; | |
8321 | template <class U> friend void f() {} | |
8322 | ||
8323 | we don't need to look any further. That's what the check for | |
8324 | DECL_INITIAL is for. */ | |
8325 | || (TREE_CODE (d) == FUNCTION_DECL | |
61289ca3 | 8326 | && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (td) |
4d85e00e | 8327 | && !DECL_INITIAL (DECL_TEMPLATE_RESULT (td))); |
f84b4be9 | 8328 | ) |
4d85e00e MM |
8329 | { |
8330 | /* The present template, TD, should not be a definition. If it | |
8331 | were a definition, we should be using it! Note that we | |
8332 | cannot restructure the loop to just keep going until we find | |
8333 | a template with a definition, since that might go too far if | |
8334 | a specialization was declared, but not defined. */ | |
8335 | my_friendly_assert (!(TREE_CODE (d) == FUNCTION_DECL | |
8336 | && DECL_INITIAL (DECL_TEMPLATE_RESULT (td))), | |
8337 | 0); | |
8338 | my_friendly_assert (!(TREE_CODE (d) == VAR_DECL | |
8339 | && !DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (td))), | |
8340 | 0); | |
8341 | ||
8342 | /* Fetch the more general template. */ | |
8343 | td = DECL_TI_TEMPLATE (td); | |
8344 | } | |
27bb8339 | 8345 | |
fee23f54 | 8346 | code_pattern = DECL_TEMPLATE_RESULT (td); |
27bb8339 | 8347 | |
5566b478 | 8348 | if (TREE_CODE (d) == FUNCTION_DECL) |
36a117a5 | 8349 | pattern_defined = (DECL_INITIAL (code_pattern) != NULL_TREE); |
5566b478 | 8350 | else |
36a117a5 | 8351 | pattern_defined = ! DECL_IN_AGGR_P (code_pattern); |
de22184b MS |
8352 | |
8353 | push_to_top_level (); | |
8354 | lineno = DECL_SOURCE_LINE (d); | |
8355 | input_filename = DECL_SOURCE_FILE (d); | |
8356 | ||
de22184b | 8357 | if (pattern_defined) |
5566b478 MS |
8358 | { |
8359 | repo_template_used (d); | |
8360 | ||
8361 | if (flag_external_templates && ! DECL_INTERFACE_KNOWN (d)) | |
8362 | { | |
8363 | if (flag_alt_external_templates) | |
8364 | { | |
8365 | if (interface_unknown) | |
8366 | warn_if_unknown_interface (d); | |
8367 | } | |
fee23f54 | 8368 | else if (DECL_INTERFACE_KNOWN (code_pattern)) |
5566b478 MS |
8369 | { |
8370 | DECL_INTERFACE_KNOWN (d) = 1; | |
fee23f54 | 8371 | DECL_NOT_REALLY_EXTERN (d) = ! DECL_EXTERNAL (code_pattern); |
5566b478 MS |
8372 | } |
8373 | else | |
fee23f54 | 8374 | warn_if_unknown_interface (code_pattern); |
5566b478 MS |
8375 | } |
8376 | ||
e92cc029 | 8377 | if (at_eof) |
5566b478 MS |
8378 | import_export_decl (d); |
8379 | } | |
8380 | ||
c4ae3f91 JM |
8381 | /* Reject all external templates except inline functions. */ |
8382 | if (DECL_INTERFACE_KNOWN (d) | |
8383 | && ! DECL_NOT_REALLY_EXTERN (d) | |
8384 | && ! (TREE_CODE (d) == FUNCTION_DECL && DECL_INLINE (d))) | |
8385 | goto out; | |
8386 | ||
67ffc812 | 8387 | if (TREE_CODE (d) == VAR_DECL |
711734a9 | 8388 | && TREE_READONLY (d) |
67ffc812 MM |
8389 | && DECL_INITIAL (d) == NULL_TREE |
8390 | && DECL_INITIAL (code_pattern) != NULL_TREE) | |
8391 | /* We need to set up DECL_INITIAL regardless of pattern_defined if | |
8392 | the variable is a static const initialized in the class body. */; | |
8393 | else if (! pattern_defined | |
8394 | || (! (TREE_CODE (d) == FUNCTION_DECL && DECL_INLINE (d) && nested) | |
8395 | && ! at_eof)) | |
5566b478 | 8396 | { |
67ffc812 | 8397 | /* Defer all templates except inline functions used in another |
c27be9b9 JM |
8398 | function. We restore the source position here because it's used |
8399 | by add_pending_template. */ | |
8400 | lineno = line; | |
8401 | input_filename = file; | |
8402 | ||
03d0f4af MM |
8403 | if (at_eof && !pattern_defined |
8404 | && DECL_EXPLICIT_INSTANTIATION (d)) | |
8405 | /* [temp.explicit] | |
8406 | ||
8407 | The definition of a non-exported function template, a | |
8408 | non-exported member function template, or a non-exported | |
8409 | member function or static data member of a class template | |
8410 | shall be present in every translation unit in which it is | |
8411 | explicitly instantiated. */ | |
8251199e | 8412 | cp_error ("explicit instantiation of `%D' but no definition available", |
03d0f4af MM |
8413 | d); |
8414 | ||
5566b478 | 8415 | add_pending_template (d); |
de22184b | 8416 | goto out; |
5566b478 MS |
8417 | } |
8418 | ||
120722ac MM |
8419 | regenerate_decl_from_template (d, td); |
8420 | ||
8421 | /* We already set the file and line above. Reset them now in case | |
8422 | they changed as a result of calling regenerate_decl_from_template. */ | |
5156628f MS |
8423 | lineno = DECL_SOURCE_LINE (d); |
8424 | input_filename = DECL_SOURCE_FILE (d); | |
8425 | ||
5566b478 MS |
8426 | if (TREE_CODE (d) == VAR_DECL) |
8427 | { | |
8428 | DECL_IN_AGGR_P (d) = 0; | |
8429 | if (DECL_INTERFACE_KNOWN (d)) | |
8430 | DECL_EXTERNAL (d) = ! DECL_NOT_REALLY_EXTERN (d); | |
8431 | else | |
8432 | { | |
8433 | DECL_EXTERNAL (d) = 1; | |
8434 | DECL_NOT_REALLY_EXTERN (d) = 1; | |
8435 | } | |
8436 | cp_finish_decl (d, DECL_INITIAL (d), NULL_TREE, 0, 0); | |
8437 | } | |
8438 | else if (TREE_CODE (d) == FUNCTION_DECL) | |
8439 | { | |
fee23f54 | 8440 | tree t = DECL_SAVED_TREE (code_pattern); |
5566b478 | 8441 | |
c11b6f21 | 8442 | start_function (NULL_TREE, d, NULL_TREE, 1); |
5566b478 MS |
8443 | store_parm_decls (); |
8444 | ||
e76a2646 MS |
8445 | if (t && TREE_CODE (t) == RETURN_INIT) |
8446 | { | |
8447 | store_return_init | |
8448 | (TREE_OPERAND (t, 0), | |
f7d98d58 | 8449 | tsubst_expr (TREE_OPERAND (t, 1), args, tmpl)); |
e76a2646 MS |
8450 | t = TREE_CHAIN (t); |
8451 | } | |
8452 | ||
5566b478 MS |
8453 | if (t && TREE_CODE (t) == CTOR_INITIALIZER) |
8454 | { | |
8455 | current_member_init_list | |
8456 | = tsubst_expr_values (TREE_OPERAND (t, 0), args); | |
8457 | current_base_init_list | |
8458 | = tsubst_expr_values (TREE_OPERAND (t, 1), args); | |
8459 | t = TREE_CHAIN (t); | |
8460 | } | |
8461 | ||
8462 | setup_vtbl_ptr (); | |
8463 | /* Always keep the BLOCK node associated with the outermost | |
956d6950 | 8464 | pair of curly braces of a function. These are needed |
5566b478 MS |
8465 | for correct operation of dwarfout.c. */ |
8466 | keep_next_level (); | |
8467 | ||
8468 | my_friendly_assert (TREE_CODE (t) == COMPOUND_STMT, 42); | |
f7d98d58 | 8469 | tsubst_expr (t, args, tmpl); |
a28e3c7f | 8470 | |
5566b478 | 8471 | finish_function (lineno, 0, nested); |
5566b478 MS |
8472 | } |
8473 | ||
de22184b | 8474 | out: |
c27be9b9 JM |
8475 | lineno = line; |
8476 | input_filename = file; | |
8477 | ||
5566b478 | 8478 | pop_from_top_level (); |
5566b478 | 8479 | pop_tinst_level (); |
a28e3c7f | 8480 | |
a28e3c7f MS |
8481 | return d; |
8482 | } | |
5566b478 | 8483 | |
824b9a4c | 8484 | static tree |
5566b478 MS |
8485 | tsubst_expr_values (t, argvec) |
8486 | tree t, argvec; | |
8487 | { | |
8488 | tree first = NULL_TREE; | |
8489 | tree *p = &first; | |
8490 | ||
8491 | for (; t; t = TREE_CHAIN (t)) | |
8492 | { | |
f7d98d58 JM |
8493 | tree pur = tsubst_copy (TREE_PURPOSE (t), argvec, NULL_TREE); |
8494 | tree val = tsubst_expr (TREE_VALUE (t), argvec, NULL_TREE); | |
5566b478 MS |
8495 | *p = build_tree_list (pur, val); |
8496 | p = &TREE_CHAIN (*p); | |
8497 | } | |
8498 | return first; | |
8499 | } | |
8500 | ||
8501 | tree last_tree; | |
8502 | ||
8503 | void | |
8504 | add_tree (t) | |
8505 | tree t; | |
8506 | { | |
8507 | last_tree = TREE_CHAIN (last_tree) = t; | |
8508 | } | |
73aad9b9 | 8509 | |
75650646 MM |
8510 | |
8511 | void | |
8512 | begin_tree () | |
8513 | { | |
8514 | saved_trees = tree_cons (NULL_TREE, last_tree, saved_trees); | |
8515 | last_tree = NULL_TREE; | |
8516 | } | |
8517 | ||
8518 | ||
8519 | void | |
8520 | end_tree () | |
8521 | { | |
8522 | my_friendly_assert (saved_trees != NULL_TREE, 0); | |
8523 | ||
8524 | last_tree = TREE_VALUE (saved_trees); | |
8525 | saved_trees = TREE_CHAIN (saved_trees); | |
8526 | } | |
8527 | ||
73aad9b9 JM |
8528 | /* D is an undefined function declaration in the presence of templates with |
8529 | the same name, listed in FNS. If one of them can produce D as an | |
8530 | instantiation, remember this so we can instantiate it at EOF if D has | |
8531 | not been defined by that time. */ | |
8532 | ||
8533 | void | |
8534 | add_maybe_template (d, fns) | |
8535 | tree d, fns; | |
8536 | { | |
8537 | tree t; | |
8538 | ||
8539 | if (DECL_MAYBE_TEMPLATE (d)) | |
8540 | return; | |
8541 | ||
e1467ff2 | 8542 | t = most_specialized (fns, d, NULL_TREE); |
73aad9b9 JM |
8543 | if (! t) |
8544 | return; | |
8545 | if (t == error_mark_node) | |
8546 | { | |
8251199e | 8547 | cp_error ("ambiguous template instantiation for `%D'", d); |
73aad9b9 JM |
8548 | return; |
8549 | } | |
8550 | ||
8551 | *maybe_template_tail = perm_tree_cons (t, d, NULL_TREE); | |
8552 | maybe_template_tail = &TREE_CHAIN (*maybe_template_tail); | |
8553 | DECL_MAYBE_TEMPLATE (d) = 1; | |
8554 | } | |
b87692e5 | 8555 | |
61a127b3 MM |
8556 | /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */ |
8557 | ||
8558 | static void | |
8559 | set_current_access_from_decl (decl) | |
8560 | tree decl; | |
8561 | { | |
8562 | if (TREE_PRIVATE (decl)) | |
8563 | current_access_specifier = access_private_node; | |
8564 | else if (TREE_PROTECTED (decl)) | |
8565 | current_access_specifier = access_protected_node; | |
8566 | else | |
8567 | current_access_specifier = access_public_node; | |
8568 | } | |
8569 | ||
dbfe2124 MM |
8570 | /* Instantiate an enumerated type. TAG is the template type, NEWTAG |
8571 | is the instantiation (which should have been created with | |
8572 | start_enum) and ARGS are the template arguments to use. */ | |
b87692e5 | 8573 | |
dbfe2124 MM |
8574 | static void |
8575 | tsubst_enum (tag, newtag, args) | |
8576 | tree tag; | |
8577 | tree newtag; | |
8578 | tree args; | |
b87692e5 | 8579 | { |
dbfe2124 | 8580 | tree e; |
b87692e5 MS |
8581 | |
8582 | for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e)) | |
8583 | { | |
61a127b3 MM |
8584 | tree value; |
8585 | tree elt; | |
8586 | ||
8587 | /* Note that in a template enum, the TREE_VALUE is the | |
8588 | CONST_DECL, not the corresponding INTEGER_CST. */ | |
8589 | value = tsubst_expr (DECL_INITIAL (TREE_VALUE (e)), | |
72f2bd78 | 8590 | args, |
61a127b3 MM |
8591 | NULL_TREE); |
8592 | ||
8593 | /* Give this enumeration constant the correct access. */ | |
8594 | set_current_access_from_decl (TREE_VALUE (e)); | |
8595 | ||
8596 | /* Actually build the enumerator itself. */ | |
8597 | elt = build_enumerator (TREE_PURPOSE (e), value, newtag); | |
b87692e5 | 8598 | |
dbfe2124 MM |
8599 | /* We save the enumerators we have built so far in the |
8600 | TYPE_VALUES so that if the enumeration constants for | |
8601 | subsequent enumerators involve those for previous ones, | |
8602 | tsubst_copy will be able to find them. */ | |
8603 | TREE_CHAIN (elt) = TYPE_VALUES (newtag); | |
8604 | TYPE_VALUES (newtag) = elt; | |
8605 | } | |
b3d5a58b | 8606 | |
dbfe2124 | 8607 | finish_enum (newtag); |
b87692e5 | 8608 | } |
36a117a5 MM |
8609 | |
8610 | /* Set the DECL_ASSEMBLER_NAME for DECL, which is a FUNCTION_DECL that | |
8611 | is either an instantiation or specialization of a template | |
8612 | function. */ | |
8613 | ||
8614 | static void | |
8615 | set_mangled_name_for_template_decl (decl) | |
8616 | tree decl; | |
8617 | { | |
8618 | tree saved_namespace; | |
b370501f | 8619 | tree context = NULL_TREE; |
36a117a5 MM |
8620 | tree fn_type; |
8621 | tree ret_type; | |
8622 | tree parm_types; | |
8623 | tree tparms; | |
8624 | tree targs; | |
8625 | tree tmpl; | |
8626 | int parm_depth; | |
8627 | ||
8628 | my_friendly_assert (TREE_CODE (decl) == FUNCTION_DECL, 0); | |
8629 | my_friendly_assert (DECL_TEMPLATE_INFO (decl) != NULL_TREE, 0); | |
8630 | ||
8631 | /* The names of template functions must be mangled so as to indicate | |
8632 | what template is being specialized with what template arguments. | |
8633 | For example, each of the following three functions must get | |
8634 | different mangled names: | |
8635 | ||
8636 | void f(int); | |
8637 | template <> void f<7>(int); | |
8638 | template <> void f<8>(int); */ | |
8639 | ||
8640 | targs = DECL_TI_ARGS (decl); | |
8641 | if (uses_template_parms (targs)) | |
8642 | /* This DECL is for a partial instantiation. There's no need to | |
8643 | mangle the name of such an entity. */ | |
8644 | return; | |
8645 | ||
8646 | tmpl = most_general_template (DECL_TI_TEMPLATE (decl)); | |
8647 | tparms = DECL_TEMPLATE_PARMS (tmpl); | |
8648 | parm_depth = TMPL_PARMS_DEPTH (tparms); | |
8649 | ||
8650 | /* There should be as many levels of arguments as there are levels | |
8651 | of parameters. */ | |
8652 | my_friendly_assert (parm_depth == TMPL_ARGS_DEPTH (targs), 0); | |
8653 | ||
8654 | /* We now compute the PARMS and RET_TYPE to give to | |
8655 | build_decl_overload_real. The PARMS and RET_TYPE are the | |
8656 | parameter and return types of the template, after all but the | |
8657 | innermost template arguments have been substituted, not the | |
8658 | parameter and return types of the function DECL. For example, | |
8659 | given: | |
8660 | ||
8661 | template <class T> T f(T); | |
8662 | ||
8663 | both PARMS and RET_TYPE should be `T' even if DECL is `int f(int)'. | |
8664 | A more subtle example is: | |
8665 | ||
8666 | template <class T> struct S { template <class U> void f(T, U); } | |
8667 | ||
8668 | Here, if DECL is `void S<int>::f(int, double)', PARMS should be | |
8669 | {int, U}. Thus, the args that we want to subsitute into the | |
8670 | return and parameter type for the function are those in TARGS, | |
8671 | with the innermost level omitted. */ | |
8672 | fn_type = TREE_TYPE (tmpl); | |
8673 | if (DECL_STATIC_FUNCTION_P (decl)) | |
61a127b3 | 8674 | context = DECL_CLASS_CONTEXT (decl); |
36a117a5 MM |
8675 | |
8676 | if (parm_depth == 1) | |
8677 | /* No substitution is necessary. */ | |
8678 | ; | |
8679 | else | |
8680 | { | |
8681 | int i; | |
8682 | tree partial_args; | |
8683 | ||
8684 | /* Replace the innermost level of the TARGS with NULL_TREEs to | |
8685 | let tsubst know not to subsitute for those parameters. */ | |
8686 | partial_args = make_temp_vec (TREE_VEC_LENGTH (targs)); | |
8687 | for (i = 1; i < TMPL_ARGS_DEPTH (targs); ++i) | |
8688 | SET_TMPL_ARGS_LEVEL (partial_args, i, | |
8689 | TMPL_ARGS_LEVEL (targs, i)); | |
8690 | SET_TMPL_ARGS_LEVEL (partial_args, | |
8691 | TMPL_ARGS_DEPTH (targs), | |
8692 | make_temp_vec (DECL_NTPARMS (tmpl))); | |
8693 | ||
8694 | /* Now, do the (partial) substitution to figure out the | |
8695 | appropriate function type. */ | |
8696 | fn_type = tsubst (fn_type, partial_args, NULL_TREE); | |
8697 | if (DECL_STATIC_FUNCTION_P (decl)) | |
8698 | context = tsubst (context, partial_args, NULL_TREE); | |
8699 | ||
8700 | /* Substitute into the template parameters to obtain the real | |
8701 | innermost set of parameters. This step is important if the | |
8702 | innermost set of template parameters contains value | |
8703 | parameters whose types depend on outer template parameters. */ | |
8704 | TREE_VEC_LENGTH (partial_args)--; | |
8705 | tparms = tsubst_template_parms (tparms, partial_args); | |
8706 | } | |
8707 | ||
8708 | /* Now, get the innermost parameters and arguments, and figure out | |
8709 | the parameter and return types. */ | |
8710 | tparms = INNERMOST_TEMPLATE_PARMS (tparms); | |
8711 | targs = innermost_args (targs); | |
8712 | ret_type = TREE_TYPE (fn_type); | |
8713 | parm_types = TYPE_ARG_TYPES (fn_type); | |
8714 | ||
8715 | /* For a static member function, we generate a fake `this' pointer, | |
8716 | for the purposes of mangling. This indicates of which class the | |
8717 | function is a member. Because of: | |
8718 | ||
8719 | [class.static] | |
8720 | ||
8721 | There shall not be a static and a nonstatic member function | |
8722 | with the same name and the same parameter types | |
8723 | ||
8724 | we don't have to worry that this will result in a clash with a | |
8725 | non-static member function. */ | |
8726 | if (DECL_STATIC_FUNCTION_P (decl)) | |
8727 | parm_types = hash_tree_chain (build_pointer_type (context), parm_types); | |
8728 | ||
8729 | /* There should be the same number of template parameters as | |
8730 | template arguments. */ | |
8731 | my_friendly_assert (TREE_VEC_LENGTH (tparms) == TREE_VEC_LENGTH (targs), | |
8732 | 0); | |
8733 | ||
8734 | /* If the template is in a namespace, we need to put that into the | |
8735 | mangled name. Unfortunately, build_decl_overload_real does not | |
8736 | get the decl to mangle, so it relies on the current | |
8737 | namespace. Therefore, we set that here temporarily. */ | |
8738 | my_friendly_assert (TREE_CODE_CLASS (TREE_CODE (decl)) == 'd', 980702); | |
8739 | saved_namespace = current_namespace; | |
8740 | current_namespace = CP_DECL_CONTEXT (decl); | |
8741 | ||
8742 | /* Actually set the DCL_ASSEMBLER_NAME. */ | |
8743 | DECL_ASSEMBLER_NAME (decl) | |
8744 | = build_decl_overload_real (DECL_NAME (decl), parm_types, ret_type, | |
8745 | tparms, targs, | |
8746 | DECL_FUNCTION_MEMBER_P (decl) | |
8747 | + DECL_CONSTRUCTOR_P (decl)); | |
8748 | ||
8749 | /* Restore the previously active namespace. */ | |
8750 | current_namespace = saved_namespace; | |
8751 | } |