]>
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; | |
8452b1d3 | 5920 | tree r; |
8d08fdba | 5921 | |
5566b478 MS |
5922 | if (t == NULL_TREE || t == error_mark_node) |
5923 | return t; | |
5924 | ||
5925 | code = TREE_CODE (t); | |
b7484fbe | 5926 | |
5566b478 MS |
5927 | switch (code) |
5928 | { | |
5929 | case PARM_DECL: | |
a759e627 | 5930 | return do_identifier (DECL_NAME (t), 0, NULL_TREE); |
5566b478 MS |
5931 | |
5932 | case CONST_DECL: | |
ed44da02 MM |
5933 | { |
5934 | tree enum_type; | |
5935 | tree v; | |
5936 | ||
5937 | if (!DECL_CONTEXT (t)) | |
5938 | /* This is a global enumeration constant. */ | |
5939 | return t; | |
5940 | ||
5941 | /* Unfortunately, we cannot just call lookup_name here. | |
5942 | Consider: | |
5943 | ||
5944 | template <int I> int f() { | |
5945 | enum E { a = I }; | |
5946 | struct S { void g() { E e = a; } }; | |
5947 | }; | |
5948 | ||
5949 | When we instantiate f<7>::S::g(), say, lookup_name is not | |
5950 | clever enough to find f<7>::a. */ | |
5951 | enum_type | |
5952 | = tsubst_aggr_type (TREE_TYPE (t), args, in_decl, | |
5953 | /*entering_scope=*/0); | |
5954 | ||
5955 | for (v = TYPE_VALUES (enum_type); | |
5956 | v != NULL_TREE; | |
5957 | v = TREE_CHAIN (v)) | |
5958 | if (TREE_PURPOSE (v) == DECL_NAME (t)) | |
5959 | return TREE_VALUE (v); | |
5960 | ||
5961 | /* We didn't find the name. That should never happen; if | |
5962 | name-lookup found it during preliminary parsing, we | |
5963 | should find it again here during instantiation. */ | |
5964 | my_friendly_abort (0); | |
5965 | } | |
db04386f | 5966 | return t; |
ed44da02 | 5967 | |
5566b478 MS |
5968 | case FIELD_DECL: |
5969 | if (DECL_CONTEXT (t)) | |
5970 | { | |
0978790f | 5971 | tree ctx; |
0978790f | 5972 | |
36a117a5 MM |
5973 | ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, in_decl, |
5974 | /*entering_scope=*/1); | |
0978790f | 5975 | if (ctx != DECL_CONTEXT (t)) |
5566b478 MS |
5976 | return lookup_field (ctx, DECL_NAME (t), 0, 0); |
5977 | } | |
5978 | return t; | |
5979 | ||
5980 | case VAR_DECL: | |
5981 | case FUNCTION_DECL: | |
5982 | if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)) | |
f7d98d58 | 5983 | t = tsubst (t, args, in_decl); |
5566b478 MS |
5984 | mark_used (t); |
5985 | return t; | |
5986 | ||
98c1c668 JM |
5987 | case TEMPLATE_DECL: |
5988 | if (is_member_template (t)) | |
f7d98d58 | 5989 | return tsubst (t, args, in_decl); |
98c1c668 JM |
5990 | else |
5991 | return t; | |
5992 | ||
672476cb MM |
5993 | case LOOKUP_EXPR: |
5994 | { | |
5995 | /* We must tsbust into a LOOKUP_EXPR in case the names to | |
5996 | which it refers is a conversion operator; in that case the | |
5997 | name will change. We avoid making unnecessary copies, | |
5998 | however. */ | |
5999 | ||
6000 | tree id = tsubst_copy (TREE_OPERAND (t, 0), args, in_decl); | |
6001 | ||
6002 | if (id != TREE_OPERAND (t, 0)) | |
6003 | { | |
8452b1d3 | 6004 | r = build_nt (LOOKUP_EXPR, id); |
672476cb MM |
6005 | LOOKUP_EXPR_GLOBAL (r) = LOOKUP_EXPR_GLOBAL (t); |
6006 | t = r; | |
6007 | } | |
6008 | ||
6009 | return t; | |
6010 | } | |
6011 | ||
5566b478 MS |
6012 | case CAST_EXPR: |
6013 | case REINTERPRET_CAST_EXPR: | |
e92cc029 MS |
6014 | case CONST_CAST_EXPR: |
6015 | case STATIC_CAST_EXPR: | |
6016 | case DYNAMIC_CAST_EXPR: | |
51924768 | 6017 | case NOP_EXPR: |
5566b478 | 6018 | return build1 |
f7d98d58 JM |
6019 | (code, tsubst (TREE_TYPE (t), args, in_decl), |
6020 | tsubst_copy (TREE_OPERAND (t, 0), args, in_decl)); | |
5566b478 MS |
6021 | |
6022 | case INDIRECT_REF: | |
6023 | case PREDECREMENT_EXPR: | |
6024 | case PREINCREMENT_EXPR: | |
6025 | case POSTDECREMENT_EXPR: | |
6026 | case POSTINCREMENT_EXPR: | |
6027 | case NEGATE_EXPR: | |
6028 | case TRUTH_NOT_EXPR: | |
b87692e5 | 6029 | case BIT_NOT_EXPR: |
5566b478 MS |
6030 | case ADDR_EXPR: |
6031 | case CONVERT_EXPR: /* Unary + */ | |
6032 | case SIZEOF_EXPR: | |
abff8e06 | 6033 | case ALIGNOF_EXPR: |
5566b478 | 6034 | case ARROW_EXPR: |
fc378698 | 6035 | case THROW_EXPR: |
5156628f | 6036 | case TYPEID_EXPR: |
5566b478 MS |
6037 | return build1 |
6038 | (code, NULL_TREE, | |
f7d98d58 | 6039 | tsubst_copy (TREE_OPERAND (t, 0), args, in_decl)); |
5566b478 MS |
6040 | |
6041 | case PLUS_EXPR: | |
6042 | case MINUS_EXPR: | |
6043 | case MULT_EXPR: | |
6044 | case TRUNC_DIV_EXPR: | |
6045 | case CEIL_DIV_EXPR: | |
6046 | case FLOOR_DIV_EXPR: | |
6047 | case ROUND_DIV_EXPR: | |
6048 | case EXACT_DIV_EXPR: | |
6049 | case BIT_AND_EXPR: | |
6050 | case BIT_ANDTC_EXPR: | |
6051 | case BIT_IOR_EXPR: | |
6052 | case BIT_XOR_EXPR: | |
6053 | case TRUNC_MOD_EXPR: | |
6054 | case FLOOR_MOD_EXPR: | |
6055 | case TRUTH_ANDIF_EXPR: | |
6056 | case TRUTH_ORIF_EXPR: | |
6057 | case TRUTH_AND_EXPR: | |
6058 | case TRUTH_OR_EXPR: | |
6059 | case RSHIFT_EXPR: | |
6060 | case LSHIFT_EXPR: | |
6061 | case RROTATE_EXPR: | |
6062 | case LROTATE_EXPR: | |
6063 | case EQ_EXPR: | |
6064 | case NE_EXPR: | |
6065 | case MAX_EXPR: | |
6066 | case MIN_EXPR: | |
6067 | case LE_EXPR: | |
6068 | case GE_EXPR: | |
6069 | case LT_EXPR: | |
6070 | case GT_EXPR: | |
6071 | case COMPONENT_REF: | |
6072 | case ARRAY_REF: | |
6073 | case COMPOUND_EXPR: | |
6074 | case SCOPE_REF: | |
6075 | case DOTSTAR_EXPR: | |
6076 | case MEMBER_REF: | |
6077 | return build_nt | |
f7d98d58 JM |
6078 | (code, tsubst_copy (TREE_OPERAND (t, 0), args, in_decl), |
6079 | tsubst_copy (TREE_OPERAND (t, 1), args, in_decl)); | |
5566b478 MS |
6080 | |
6081 | case CALL_EXPR: | |
6082 | { | |
6083 | tree fn = TREE_OPERAND (t, 0); | |
f84b4be9 | 6084 | if (is_overloaded_fn (fn)) |
f7d98d58 | 6085 | fn = tsubst_copy (get_first_fn (fn), args, in_decl); |
5566b478 | 6086 | else |
f84b4be9 | 6087 | /* Sometimes FN is a LOOKUP_EXPR. */ |
f7d98d58 | 6088 | fn = tsubst_copy (fn, args, in_decl); |
5566b478 | 6089 | return build_nt |
f7d98d58 | 6090 | (code, fn, tsubst_copy (TREE_OPERAND (t, 1), args, in_decl), |
5566b478 MS |
6091 | NULL_TREE); |
6092 | } | |
6093 | ||
6094 | case METHOD_CALL_EXPR: | |
6095 | { | |
6096 | tree name = TREE_OPERAND (t, 0); | |
6097 | if (TREE_CODE (name) == BIT_NOT_EXPR) | |
6098 | { | |
f7d98d58 | 6099 | name = tsubst_copy (TREE_OPERAND (name, 0), args, in_decl); |
7bae46f4 | 6100 | name = build1 (BIT_NOT_EXPR, NULL_TREE, name); |
5566b478 MS |
6101 | } |
6102 | else if (TREE_CODE (name) == SCOPE_REF | |
6103 | && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR) | |
6104 | { | |
f7d98d58 | 6105 | tree base = tsubst_copy (TREE_OPERAND (name, 0), args, in_decl); |
5566b478 | 6106 | name = TREE_OPERAND (name, 1); |
f7d98d58 | 6107 | name = tsubst_copy (TREE_OPERAND (name, 0), args, in_decl); |
11686454 | 6108 | name = build1 (BIT_NOT_EXPR, NULL_TREE, name); |
5566b478 MS |
6109 | name = build_nt (SCOPE_REF, base, name); |
6110 | } | |
6111 | else | |
f7d98d58 | 6112 | name = tsubst_copy (TREE_OPERAND (t, 0), args, in_decl); |
5566b478 | 6113 | return build_nt |
f7d98d58 JM |
6114 | (code, name, tsubst_copy (TREE_OPERAND (t, 1), args, in_decl), |
6115 | tsubst_copy (TREE_OPERAND (t, 2), args, in_decl), | |
5566b478 MS |
6116 | NULL_TREE); |
6117 | } | |
6118 | ||
67da3287 | 6119 | case BIND_EXPR: |
5566b478 MS |
6120 | case COND_EXPR: |
6121 | case MODOP_EXPR: | |
67da3287 | 6122 | { |
8452b1d3 | 6123 | r = build_nt |
f7d98d58 JM |
6124 | (code, tsubst_copy (TREE_OPERAND (t, 0), args, in_decl), |
6125 | tsubst_copy (TREE_OPERAND (t, 1), args, in_decl), | |
6126 | tsubst_copy (TREE_OPERAND (t, 2), args, in_decl)); | |
67da3287 MM |
6127 | |
6128 | if (code == BIND_EXPR && !processing_template_decl) | |
6129 | { | |
6130 | /* This processing should really occur in tsubst_expr, | |
6131 | However, tsubst_expr does not recurse into expressions, | |
6132 | since it assumes that there aren't any statements | |
6133 | inside them. Instead, it simply calls | |
6134 | build_expr_from_tree. So, we need to expand the | |
6135 | BIND_EXPR here. */ | |
b4c4a9ec | 6136 | tree rtl_expr = begin_stmt_expr (); |
544c188d | 6137 | tree block = tsubst_expr (TREE_OPERAND (r, 1), args, in_decl); |
b4c4a9ec | 6138 | r = finish_stmt_expr (rtl_expr, block); |
67da3287 MM |
6139 | } |
6140 | ||
6141 | return r; | |
6142 | } | |
5566b478 MS |
6143 | |
6144 | case NEW_EXPR: | |
6145 | { | |
8452b1d3 | 6146 | r = build_nt |
f7d98d58 JM |
6147 | (code, tsubst_copy (TREE_OPERAND (t, 0), args, in_decl), |
6148 | tsubst_copy (TREE_OPERAND (t, 1), args, in_decl), | |
6149 | tsubst_copy (TREE_OPERAND (t, 2), args, in_decl)); | |
5566b478 MS |
6150 | NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t); |
6151 | return r; | |
6152 | } | |
6153 | ||
6154 | case DELETE_EXPR: | |
6155 | { | |
8452b1d3 | 6156 | r = build_nt |
f7d98d58 JM |
6157 | (code, tsubst_copy (TREE_OPERAND (t, 0), args, in_decl), |
6158 | tsubst_copy (TREE_OPERAND (t, 1), args, in_decl)); | |
5566b478 MS |
6159 | DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t); |
6160 | DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t); | |
6161 | return r; | |
6162 | } | |
6163 | ||
386b8a85 JM |
6164 | case TEMPLATE_ID_EXPR: |
6165 | { | |
00d3396f | 6166 | /* Substituted template arguments */ |
f7d98d58 | 6167 | tree targs = tsubst_copy (TREE_OPERAND (t, 1), args, in_decl); |
00d3396f JM |
6168 | tree chain; |
6169 | for (chain = targs; chain; chain = TREE_CHAIN (chain)) | |
6170 | TREE_VALUE (chain) = maybe_fold_nontype_arg (TREE_VALUE (chain)); | |
6171 | ||
6172 | return lookup_template_function | |
f7d98d58 | 6173 | (tsubst_copy (TREE_OPERAND (t, 0), args, in_decl), targs); |
386b8a85 JM |
6174 | } |
6175 | ||
5566b478 MS |
6176 | case TREE_LIST: |
6177 | { | |
6178 | tree purpose, value, chain; | |
6179 | ||
6180 | if (t == void_list_node) | |
6181 | return t; | |
6182 | ||
6183 | purpose = TREE_PURPOSE (t); | |
6184 | if (purpose) | |
f7d98d58 | 6185 | purpose = tsubst_copy (purpose, args, in_decl); |
5566b478 MS |
6186 | value = TREE_VALUE (t); |
6187 | if (value) | |
f7d98d58 | 6188 | value = tsubst_copy (value, args, in_decl); |
5566b478 MS |
6189 | chain = TREE_CHAIN (t); |
6190 | if (chain && chain != void_type_node) | |
f7d98d58 | 6191 | chain = tsubst_copy (chain, args, in_decl); |
5566b478 MS |
6192 | if (purpose == TREE_PURPOSE (t) |
6193 | && value == TREE_VALUE (t) | |
6194 | && chain == TREE_CHAIN (t)) | |
6195 | return t; | |
6196 | return tree_cons (purpose, value, chain); | |
6197 | } | |
6198 | ||
6199 | case RECORD_TYPE: | |
6200 | case UNION_TYPE: | |
6201 | case ENUMERAL_TYPE: | |
6202 | case INTEGER_TYPE: | |
6203 | case TEMPLATE_TYPE_PARM: | |
73b0fce8 | 6204 | case TEMPLATE_TEMPLATE_PARM: |
f84b4be9 | 6205 | case TEMPLATE_PARM_INDEX: |
5566b478 MS |
6206 | case POINTER_TYPE: |
6207 | case REFERENCE_TYPE: | |
6208 | case OFFSET_TYPE: | |
6209 | case FUNCTION_TYPE: | |
6210 | case METHOD_TYPE: | |
6211 | case ARRAY_TYPE: | |
6212 | case TYPENAME_TYPE: | |
f84b4be9 | 6213 | case TYPE_DECL: |
f7d98d58 | 6214 | return tsubst (t, args, in_decl); |
5566b478 | 6215 | |
e92cc029 | 6216 | case IDENTIFIER_NODE: |
8e710788 BK |
6217 | if (IDENTIFIER_TYPENAME_P (t) |
6218 | /* Make sure it's not just a variable named `__opr', for instance, | |
6219 | which can occur in some existing code. */ | |
6220 | && TREE_TYPE (t)) | |
e92cc029 | 6221 | return build_typename_overload |
f7d98d58 | 6222 | (tsubst (TREE_TYPE (t), args, in_decl)); |
e92cc029 MS |
6223 | else |
6224 | return t; | |
6225 | ||
5156628f | 6226 | case CONSTRUCTOR: |
8452b1d3 JM |
6227 | { |
6228 | r = build | |
6229 | (CONSTRUCTOR, tsubst (TREE_TYPE (t), args, in_decl), NULL_TREE, | |
6230 | tsubst_copy (CONSTRUCTOR_ELTS (t), args, in_decl)); | |
6231 | TREE_HAS_CONSTRUCTOR (r) = TREE_HAS_CONSTRUCTOR (t); | |
6232 | return r; | |
6233 | } | |
5156628f | 6234 | |
5566b478 MS |
6235 | default: |
6236 | return t; | |
6237 | } | |
6238 | } | |
6239 | ||
00d3396f JM |
6240 | /* Like tsubst_copy, but also does semantic processing and RTL expansion. */ |
6241 | ||
5566b478 | 6242 | tree |
f7d98d58 | 6243 | tsubst_expr (t, args, in_decl) |
98c1c668 | 6244 | tree t, args; |
5566b478 MS |
6245 | tree in_decl; |
6246 | { | |
6247 | if (t == NULL_TREE || t == error_mark_node) | |
6248 | return t; | |
6249 | ||
5156628f | 6250 | if (processing_template_decl) |
f7d98d58 | 6251 | return tsubst_copy (t, args, in_decl); |
5566b478 MS |
6252 | |
6253 | switch (TREE_CODE (t)) | |
8d08fdba | 6254 | { |
5566b478 MS |
6255 | case RETURN_STMT: |
6256 | lineno = TREE_COMPLEXITY (t); | |
ad321293 MM |
6257 | finish_return_stmt (tsubst_expr (RETURN_EXPR (t), |
6258 | args, in_decl)); | |
5566b478 MS |
6259 | break; |
6260 | ||
6261 | case EXPR_STMT: | |
6262 | lineno = TREE_COMPLEXITY (t); | |
ad321293 MM |
6263 | finish_expr_stmt (tsubst_expr (EXPR_STMT_EXPR (t), |
6264 | args, in_decl)); | |
5566b478 MS |
6265 | break; |
6266 | ||
6267 | case DECL_STMT: | |
6268 | { | |
6269 | int i = suspend_momentary (); | |
67d743fe | 6270 | tree dcl, init; |
5566b478 MS |
6271 | |
6272 | lineno = TREE_COMPLEXITY (t); | |
6273 | emit_line_note (input_filename, lineno); | |
6274 | dcl = start_decl | |
f7d98d58 JM |
6275 | (tsubst (TREE_OPERAND (t, 0), args, in_decl), |
6276 | tsubst (TREE_OPERAND (t, 1), args, in_decl), | |
a1774733 | 6277 | TREE_OPERAND (t, 2) != 0, NULL_TREE, NULL_TREE); |
f7d98d58 | 6278 | init = tsubst_expr (TREE_OPERAND (t, 2), args, in_decl); |
5566b478 | 6279 | cp_finish_decl |
a0128b67 | 6280 | (dcl, init, NULL_TREE, 1, /*init ? LOOKUP_ONLYCONVERTING :*/ 0); |
5566b478 MS |
6281 | resume_momentary (i); |
6282 | return dcl; | |
6283 | } | |
8d08fdba | 6284 | |
5566b478 MS |
6285 | case FOR_STMT: |
6286 | { | |
6287 | tree tmp; | |
5566b478 | 6288 | lineno = TREE_COMPLEXITY (t); |
5566b478 | 6289 | |
ad321293 MM |
6290 | begin_for_stmt (); |
6291 | for (tmp = FOR_INIT_STMT (t); tmp; tmp = TREE_CHAIN (tmp)) | |
6292 | tsubst_expr (tmp, args, in_decl); | |
6293 | finish_for_init_stmt (NULL_TREE); | |
6294 | finish_for_cond (tsubst_expr (FOR_COND (t), args, | |
6295 | in_decl), | |
6296 | NULL_TREE); | |
6297 | tmp = tsubst_expr (FOR_EXPR (t), args, in_decl); | |
6298 | finish_for_expr (tmp, NULL_TREE); | |
6299 | tsubst_expr (FOR_BODY (t), args, in_decl); | |
6300 | finish_for_stmt (tmp, NULL_TREE); | |
5566b478 MS |
6301 | } |
6302 | break; | |
8d08fdba | 6303 | |
5566b478 MS |
6304 | case WHILE_STMT: |
6305 | { | |
5566b478 | 6306 | lineno = TREE_COMPLEXITY (t); |
ad321293 MM |
6307 | begin_while_stmt (); |
6308 | finish_while_stmt_cond (tsubst_expr (WHILE_COND (t), | |
6309 | args, in_decl), | |
6310 | NULL_TREE); | |
6311 | tsubst_expr (WHILE_BODY (t), args, in_decl); | |
6312 | finish_while_stmt (NULL_TREE); | |
5566b478 MS |
6313 | } |
6314 | break; | |
8d08fdba | 6315 | |
5566b478 MS |
6316 | case DO_STMT: |
6317 | { | |
5566b478 | 6318 | lineno = TREE_COMPLEXITY (t); |
ad321293 MM |
6319 | begin_do_stmt (); |
6320 | tsubst_expr (DO_BODY (t), args, in_decl); | |
6321 | finish_do_body (NULL_TREE); | |
6322 | finish_do_stmt (tsubst_expr (DO_COND (t), args, | |
6323 | in_decl), | |
6324 | NULL_TREE); | |
5566b478 MS |
6325 | } |
6326 | break; | |
a0a33927 | 6327 | |
5566b478 | 6328 | case IF_STMT: |
8d08fdba | 6329 | { |
5566b478 | 6330 | tree tmp; |
5566b478 MS |
6331 | |
6332 | lineno = TREE_COMPLEXITY (t); | |
ad321293 MM |
6333 | begin_if_stmt (); |
6334 | finish_if_stmt_cond (tsubst_expr (IF_COND (t), | |
6335 | args, in_decl), | |
6336 | NULL_TREE); | |
db5ae43f | 6337 | |
ad321293 | 6338 | if (tmp = THEN_CLAUSE (t), tmp) |
db5ae43f | 6339 | { |
f7d98d58 | 6340 | tsubst_expr (tmp, args, in_decl); |
ad321293 | 6341 | finish_then_clause (NULL_TREE); |
db5ae43f MS |
6342 | } |
6343 | ||
ad321293 MM |
6344 | if (tmp = ELSE_CLAUSE (t), tmp) |
6345 | { | |
6346 | begin_else_clause (); | |
6347 | tsubst_expr (tmp, args, in_decl); | |
6348 | finish_else_clause (NULL_TREE); | |
6349 | } | |
8d08fdba | 6350 | |
ad321293 | 6351 | finish_if_stmt (); |
8d08fdba | 6352 | } |
5566b478 | 6353 | break; |
8d08fdba | 6354 | |
5566b478 MS |
6355 | case COMPOUND_STMT: |
6356 | { | |
ad321293 | 6357 | tree substmt; |
8d08fdba | 6358 | |
5566b478 | 6359 | lineno = TREE_COMPLEXITY (t); |
ad321293 MM |
6360 | begin_compound_stmt (COMPOUND_STMT_NO_SCOPE (t)); |
6361 | for (substmt = COMPOUND_BODY (t); | |
6362 | substmt != NULL_TREE; | |
6363 | substmt = TREE_CHAIN (substmt)) | |
f7d98d58 | 6364 | tsubst_expr (substmt, args, in_decl); |
ad321293 MM |
6365 | return finish_compound_stmt (COMPOUND_STMT_NO_SCOPE (t), |
6366 | NULL_TREE); | |
5566b478 MS |
6367 | } |
6368 | break; | |
8d08fdba | 6369 | |
5566b478 MS |
6370 | case BREAK_STMT: |
6371 | lineno = TREE_COMPLEXITY (t); | |
ad321293 | 6372 | finish_break_stmt (); |
5566b478 | 6373 | break; |
8d08fdba | 6374 | |
6467930b MS |
6375 | case CONTINUE_STMT: |
6376 | lineno = TREE_COMPLEXITY (t); | |
ad321293 | 6377 | finish_continue_stmt (); |
6467930b MS |
6378 | break; |
6379 | ||
5566b478 MS |
6380 | case SWITCH_STMT: |
6381 | { | |
6382 | tree val, tmp; | |
5566b478 MS |
6383 | |
6384 | lineno = TREE_COMPLEXITY (t); | |
ad321293 MM |
6385 | begin_switch_stmt (); |
6386 | val = tsubst_expr (SWITCH_COND (t), args, in_decl); | |
6387 | finish_switch_cond (val); | |
5566b478 MS |
6388 | |
6389 | if (tmp = TREE_OPERAND (t, 1), tmp) | |
f7d98d58 | 6390 | tsubst_expr (tmp, args, in_decl); |
8d08fdba | 6391 | |
ad321293 | 6392 | finish_switch_stmt (val, NULL_TREE); |
5566b478 MS |
6393 | } |
6394 | break; | |
6395 | ||
6396 | case CASE_LABEL: | |
ad321293 MM |
6397 | finish_case_label (tsubst_expr (CASE_LOW (t), args, in_decl), |
6398 | tsubst_expr (CASE_HIGH (t), args, in_decl)); | |
5566b478 MS |
6399 | break; |
6400 | ||
6401 | case LABEL_DECL: | |
6402 | t = define_label (DECL_SOURCE_FILE (t), DECL_SOURCE_LINE (t), | |
6403 | DECL_NAME (t)); | |
6404 | if (t) | |
6405 | expand_label (t); | |
6406 | break; | |
6407 | ||
6408 | case GOTO_STMT: | |
6409 | lineno = TREE_COMPLEXITY (t); | |
aa09da44 MM |
6410 | t = GOTO_DESTINATION (t); |
6411 | if (TREE_CODE (t) != IDENTIFIER_NODE) | |
6412 | /* Computed goto's must be tsubst'd into. On the other hand, | |
6413 | non-computed gotos must not be; the identifier in question | |
6414 | will have no binding. */ | |
6415 | t = tsubst_expr (t, args, in_decl); | |
6416 | finish_goto_stmt (t); | |
ad321293 MM |
6417 | break; |
6418 | ||
6419 | case ASM_STMT: | |
6420 | lineno = TREE_COMPLEXITY (t); | |
6421 | finish_asm_stmt (tsubst_expr (ASM_CV_QUAL (t), args, in_decl), | |
6422 | tsubst_expr (ASM_STRING (t), args, in_decl), | |
6423 | tsubst_expr (ASM_OUTPUTS (t), args, in_decl), | |
6424 | tsubst_expr (ASM_INPUTS (t), args, in_decl), | |
6425 | tsubst_expr (ASM_CLOBBERS (t), args, in_decl)); | |
5566b478 | 6426 | break; |
faf5394a MS |
6427 | |
6428 | case TRY_BLOCK: | |
6429 | lineno = TREE_COMPLEXITY (t); | |
ad321293 MM |
6430 | begin_try_block (); |
6431 | tsubst_expr (TRY_STMTS (t), args, in_decl); | |
6432 | finish_try_block (NULL_TREE); | |
faf5394a | 6433 | { |
ad321293 | 6434 | tree handler = TRY_HANDLERS (t); |
faf5394a | 6435 | for (; handler; handler = TREE_CHAIN (handler)) |
f7d98d58 | 6436 | tsubst_expr (handler, args, in_decl); |
faf5394a | 6437 | } |
ad321293 | 6438 | finish_handler_sequence (NULL_TREE); |
faf5394a MS |
6439 | break; |
6440 | ||
6441 | case HANDLER: | |
6442 | lineno = TREE_COMPLEXITY (t); | |
ad321293 MM |
6443 | begin_handler (); |
6444 | if (HANDLER_PARMS (t)) | |
faf5394a | 6445 | { |
ad321293 | 6446 | tree d = HANDLER_PARMS (t); |
faf5394a | 6447 | expand_start_catch_block |
f7d98d58 JM |
6448 | (tsubst (TREE_OPERAND (d, 1), args, in_decl), |
6449 | tsubst (TREE_OPERAND (d, 0), args, in_decl)); | |
faf5394a MS |
6450 | } |
6451 | else | |
6452 | expand_start_catch_block (NULL_TREE, NULL_TREE); | |
ad321293 MM |
6453 | finish_handler_parms (NULL_TREE); |
6454 | tsubst_expr (HANDLER_BODY (t), args, in_decl); | |
6455 | finish_handler (NULL_TREE); | |
faf5394a MS |
6456 | break; |
6457 | ||
b87692e5 MS |
6458 | case TAG_DEFN: |
6459 | lineno = TREE_COMPLEXITY (t); | |
6460 | t = TREE_TYPE (t); | |
6461 | if (TREE_CODE (t) == ENUMERAL_TYPE) | |
ed44da02 | 6462 | tsubst (t, args, NULL_TREE); |
b87692e5 MS |
6463 | break; |
6464 | ||
5566b478 | 6465 | default: |
f7d98d58 | 6466 | return build_expr_from_tree (tsubst_copy (t, args, in_decl)); |
5566b478 MS |
6467 | } |
6468 | return NULL_TREE; | |
8d08fdba MS |
6469 | } |
6470 | ||
36a117a5 MM |
6471 | /* Instantiate the indicated variable of function template TMPL with |
6472 | the template arguments in TARG_PTR. */ | |
6473 | ||
5566b478 MS |
6474 | tree |
6475 | instantiate_template (tmpl, targ_ptr) | |
98c1c668 | 6476 | tree tmpl, targ_ptr; |
8d08fdba | 6477 | { |
5566b478 | 6478 | tree fndecl; |
36a117a5 MM |
6479 | tree gen_tmpl; |
6480 | tree spec; | |
5566b478 MS |
6481 | int i, len; |
6482 | struct obstack *old_fmp_obstack; | |
6483 | extern struct obstack *function_maybepermanent_obstack; | |
36a117a5 | 6484 | tree inner_args; |
5566b478 | 6485 | |
27fafc8d JM |
6486 | if (tmpl == error_mark_node) |
6487 | return error_mark_node; | |
6488 | ||
386b8a85 JM |
6489 | my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 283); |
6490 | ||
36a117a5 MM |
6491 | /* Check to see if we already have this specialization. */ |
6492 | spec = retrieve_specialization (tmpl, targ_ptr); | |
6493 | if (spec != NULL_TREE) | |
6494 | return spec; | |
6495 | ||
6496 | if (DECL_TEMPLATE_INFO (tmpl)) | |
386b8a85 | 6497 | { |
36a117a5 MM |
6498 | /* The TMPL is a partial instantiation. To get a full set of |
6499 | arguments we must add the arguments used to perform the | |
6500 | partial instantiation. */ | |
6501 | targ_ptr = add_outermost_template_args (DECL_TI_ARGS (tmpl), | |
6502 | targ_ptr); | |
6503 | gen_tmpl = most_general_template (tmpl); | |
6504 | ||
6505 | /* Check to see if we already have this specialization. */ | |
6506 | spec = retrieve_specialization (gen_tmpl, targ_ptr); | |
75650646 MM |
6507 | if (spec != NULL_TREE) |
6508 | return spec; | |
386b8a85 | 6509 | } |
36a117a5 MM |
6510 | else |
6511 | gen_tmpl = tmpl; | |
386b8a85 | 6512 | |
5566b478 MS |
6513 | push_obstacks (&permanent_obstack, &permanent_obstack); |
6514 | old_fmp_obstack = function_maybepermanent_obstack; | |
6515 | function_maybepermanent_obstack = &permanent_obstack; | |
8d08fdba | 6516 | |
36a117a5 MM |
6517 | len = DECL_NTPARMS (gen_tmpl); |
6518 | inner_args = innermost_args (targ_ptr); | |
5566b478 MS |
6519 | i = len; |
6520 | while (i--) | |
8d08fdba | 6521 | { |
36a117a5 | 6522 | tree t = TREE_VEC_ELT (inner_args, i); |
5566b478 MS |
6523 | if (TREE_CODE_CLASS (TREE_CODE (t)) == 't') |
6524 | { | |
6525 | tree nt = target_type (t); | |
ec255269 | 6526 | if (IS_AGGR_TYPE (nt) && decl_function_context (TYPE_MAIN_DECL (nt))) |
5566b478 | 6527 | { |
8251199e JM |
6528 | cp_error ("type `%T' composed from a local class is not a valid template-argument", t); |
6529 | cp_error (" trying to instantiate `%D'", gen_tmpl); | |
5566b478 MS |
6530 | fndecl = error_mark_node; |
6531 | goto out; | |
6532 | } | |
6533 | } | |
8d08fdba | 6534 | } |
e66d884e | 6535 | targ_ptr = copy_to_permanent (targ_ptr); |
8d08fdba | 6536 | |
5566b478 | 6537 | /* substitute template parameters */ |
36a117a5 MM |
6538 | fndecl = tsubst (DECL_RESULT (gen_tmpl), targ_ptr, gen_tmpl); |
6539 | /* The DECL_TI_TEMPLATE should always be the immediate parent | |
6540 | template, not the most general template. */ | |
6541 | DECL_TI_TEMPLATE (fndecl) = tmpl; | |
8d08fdba | 6542 | |
824b9a4c MS |
6543 | if (flag_external_templates) |
6544 | add_pending_template (fndecl); | |
6545 | ||
5566b478 MS |
6546 | out: |
6547 | function_maybepermanent_obstack = old_fmp_obstack; | |
6548 | pop_obstacks (); | |
8d08fdba | 6549 | |
5566b478 | 6550 | return fndecl; |
8d08fdba | 6551 | } |
5566b478 MS |
6552 | |
6553 | /* Push the name of the class template into the scope of the instantiation. */ | |
8d08fdba MS |
6554 | |
6555 | void | |
5566b478 MS |
6556 | overload_template_name (type) |
6557 | tree type; | |
8d08fdba | 6558 | { |
5566b478 MS |
6559 | tree id = DECL_NAME (CLASSTYPE_TI_TEMPLATE (type)); |
6560 | tree decl; | |
8d08fdba | 6561 | |
5566b478 MS |
6562 | if (IDENTIFIER_CLASS_VALUE (id) |
6563 | && TREE_TYPE (IDENTIFIER_CLASS_VALUE (id)) == type) | |
6564 | return; | |
8d08fdba | 6565 | |
5566b478 MS |
6566 | decl = build_decl (TYPE_DECL, id, type); |
6567 | SET_DECL_ARTIFICIAL (decl); | |
6568 | pushdecl_class_level (decl); | |
8d08fdba MS |
6569 | } |
6570 | ||
956d6950 | 6571 | /* Like type_unification but designed specially to handle conversion |
9f54c803 MM |
6572 | operators. |
6573 | ||
6574 | The FN is a TEMPLATE_DECL for a function. The ARGS are the | |
6575 | arguments that are being used when calling it. | |
6576 | ||
6577 | If FN is a conversion operator, RETURN_TYPE is the type desired as | |
6578 | the result of the conversion operator. | |
6579 | ||
6580 | The EXTRA_FN_ARG, if any, is the type of an additional | |
6581 | parameter to be added to the beginning of FN's parameter list. | |
6582 | ||
6583 | The other arguments are as for type_unification. */ | |
98c1c668 JM |
6584 | |
6585 | int | |
d7684f2d MM |
6586 | fn_type_unification (fn, explicit_targs, targs, args, return_type, |
6587 | strict, extra_fn_arg) | |
386b8a85 | 6588 | tree fn, explicit_targs, targs, args, return_type; |
830bfa74 | 6589 | unification_kind_t strict; |
d7684f2d | 6590 | tree extra_fn_arg; |
98c1c668 | 6591 | { |
9f54c803 | 6592 | tree parms; |
98c1c668 JM |
6593 | |
6594 | my_friendly_assert (TREE_CODE (fn) == TEMPLATE_DECL, 0); | |
6595 | ||
9f54c803 MM |
6596 | parms = TYPE_ARG_TYPES (TREE_TYPE (fn)); |
6597 | ||
aa45967f | 6598 | if (DECL_CONV_FN_P (fn)) |
98c1c668 JM |
6599 | { |
6600 | /* This is a template conversion operator. Use the return types | |
6601 | as well as the argument types. */ | |
9f54c803 MM |
6602 | parms = scratch_tree_cons (NULL_TREE, |
6603 | TREE_TYPE (TREE_TYPE (fn)), | |
6604 | parms); | |
6605 | args = scratch_tree_cons (NULL_TREE, return_type, args); | |
98c1c668 JM |
6606 | } |
6607 | ||
d7684f2d | 6608 | if (extra_fn_arg != NULL_TREE) |
9f54c803 | 6609 | parms = scratch_tree_cons (NULL_TREE, extra_fn_arg, parms); |
d7684f2d | 6610 | |
7cd55873 MM |
6611 | /* We allow incomplete unification without an error message here |
6612 | because the standard doesn't seem to explicitly prohibit it. Our | |
6613 | callers must be ready to deal with unification failures in any | |
6614 | event. */ | |
9f54c803 MM |
6615 | return type_unification (DECL_INNERMOST_TEMPLATE_PARMS (fn), |
6616 | targs, | |
6617 | parms, | |
6618 | args, | |
6619 | explicit_targs, | |
6620 | strict, 1); | |
98c1c668 JM |
6621 | } |
6622 | ||
6623 | ||
8d08fdba MS |
6624 | /* Type unification. |
6625 | ||
6626 | We have a function template signature with one or more references to | |
6627 | template parameters, and a parameter list we wish to fit to this | |
6628 | template. If possible, produce a list of parameters for the template | |
6629 | which will cause it to fit the supplied parameter list. | |
6630 | ||
6631 | Return zero for success, 2 for an incomplete match that doesn't resolve | |
6632 | all the types, and 1 for complete failure. An error message will be | |
6633 | printed only for an incomplete match. | |
6634 | ||
9f54c803 | 6635 | TPARMS[NTPARMS] is an array of template parameter types. |
8d08fdba | 6636 | |
9f54c803 MM |
6637 | TARGS[NTPARMS] is the array into which the deduced template |
6638 | parameter values are placed. PARMS is the function template's | |
6639 | signature (using TEMPLATE_PARM_IDX nodes), and ARGS is the argument | |
6640 | list we're trying to match against it. | |
6641 | ||
6642 | The EXPLICIT_TARGS are explicit template arguments provided via a | |
6643 | template-id. | |
6467930b | 6644 | |
830bfa74 MM |
6645 | The parameter STRICT is one of: |
6646 | ||
6647 | DEDUCE_CALL: | |
6648 | We are deducing arguments for a function call, as in | |
6649 | [temp.deduct.call]. | |
6650 | ||
6651 | DEDUCE_CONV: | |
6652 | We are deducing arguments for a conversion function, as in | |
6653 | [temp.deduct.conv]. | |
6654 | ||
6655 | DEDUCE_EXACT: | |
6656 | We are deducing arguments when calculating the partial | |
6657 | ordering between specializations of function or class | |
6658 | templates, as in [temp.func.order] and [temp.class.order], | |
6659 | when doing an explicit instantiation as in [temp.explicit], | |
9f54c803 | 6660 | when determining an explicit specialization as in |
830bfa74 MM |
6661 | [temp.expl.spec], or when taking the address of a function |
6662 | template, as in [temp.deduct.funcaddr]. */ | |
8d08fdba MS |
6663 | |
6664 | int | |
9f54c803 | 6665 | type_unification (tparms, targs, parms, args, explicit_targs, |
386b8a85 | 6666 | strict, allow_incomplete) |
9f54c803 | 6667 | tree tparms, targs, parms, args, explicit_targs; |
830bfa74 MM |
6668 | unification_kind_t strict; |
6669 | int allow_incomplete; | |
386b8a85 | 6670 | { |
050367a3 | 6671 | int* explicit_mask; |
386b8a85 | 6672 | int i; |
386b8a85 | 6673 | |
830bfa74 | 6674 | for (i = 0; i < TREE_VEC_LENGTH (tparms); i++) |
050367a3 | 6675 | TREE_VEC_ELT (targs, i) = NULL_TREE; |
386b8a85 | 6676 | |
9f54c803 | 6677 | if (explicit_targs != NULL_TREE) |
75650646 MM |
6678 | { |
6679 | tree arg_vec; | |
9f54c803 | 6680 | arg_vec = coerce_template_parms (tparms, explicit_targs, NULL_TREE, 0, |
744fac59 | 6681 | 0); |
75650646 MM |
6682 | |
6683 | if (arg_vec == error_mark_node) | |
6684 | return 1; | |
386b8a85 | 6685 | |
050367a3 | 6686 | explicit_mask = alloca (sizeof (int) * TREE_VEC_LENGTH (targs)); |
7dee3f36 | 6687 | bzero ((char *) explicit_mask, sizeof(int) * TREE_VEC_LENGTH (targs)); |
050367a3 | 6688 | |
75650646 MM |
6689 | for (i = 0; |
6690 | i < TREE_VEC_LENGTH (arg_vec) | |
050367a3 | 6691 | && TREE_VEC_ELT (arg_vec, i) != NULL_TREE; |
75650646 | 6692 | ++i) |
050367a3 MM |
6693 | { |
6694 | TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (arg_vec, i); | |
6695 | /* Let unify know that this argument was explicit. */ | |
6696 | explicit_mask [i] = 1; | |
6697 | } | |
75650646 | 6698 | } |
050367a3 MM |
6699 | else |
6700 | explicit_mask = 0; | |
386b8a85 | 6701 | |
830bfa74 MM |
6702 | return |
6703 | type_unification_real (tparms, targs, parms, args, 0, | |
6704 | strict, allow_incomplete, explicit_mask); | |
6705 | } | |
6706 | ||
6707 | /* Adjust types before performing type deduction, as described in | |
6708 | [temp.deduct.call] and [temp.deduct.conv]. The rules in these two | |
4c7d0dff MM |
6709 | sections are symmetric. PARM is the type of a function parameter |
6710 | or the return type of the conversion function. ARG is the type of | |
6711 | the argument passed to the call, or the type of the value | |
6712 | intialized with the result of the conversion function. */ | |
386b8a85 | 6713 | |
830bfa74 MM |
6714 | void |
6715 | maybe_adjust_types_for_deduction (strict, parm, arg) | |
6716 | unification_kind_t strict; | |
6717 | tree* parm; | |
6718 | tree* arg; | |
6719 | { | |
6720 | switch (strict) | |
6721 | { | |
6722 | case DEDUCE_CALL: | |
6723 | break; | |
6724 | ||
6725 | case DEDUCE_CONV: | |
6726 | { | |
4c7d0dff MM |
6727 | /* Swap PARM and ARG throughout the remainder of this |
6728 | function; the handling is precisely symmetric since PARM | |
6729 | will initialize ARG rather than vice versa. */ | |
830bfa74 MM |
6730 | tree* temp = parm; |
6731 | parm = arg; | |
6732 | arg = temp; | |
6733 | break; | |
6734 | } | |
6735 | ||
6736 | case DEDUCE_EXACT: | |
6737 | /* There is nothing to do in this case. */ | |
6738 | return; | |
6739 | ||
6740 | default: | |
6741 | my_friendly_abort (0); | |
6742 | } | |
6743 | ||
6744 | if (TREE_CODE (*parm) != REFERENCE_TYPE) | |
6745 | { | |
6746 | /* [temp.deduct.call] | |
6747 | ||
6748 | If P is not a reference type: | |
6749 | ||
6750 | --If A is an array type, the pointer type produced by the | |
6751 | array-to-pointer standard conversion (_conv.array_) is | |
6752 | used in place of A for type deduction; otherwise, | |
6753 | ||
6754 | --If A is a function type, the pointer type produced by | |
6755 | the function-to-pointer standard conversion | |
6756 | (_conv.func_) is used in place of A for type deduction; | |
6757 | otherwise, | |
6758 | ||
6759 | --If A is a cv-qualified type, the top level | |
6760 | cv-qualifiers of A's type are ignored for type | |
6761 | deduction. */ | |
6762 | if (TREE_CODE (*arg) == ARRAY_TYPE) | |
6763 | *arg = build_pointer_type (TREE_TYPE (*arg)); | |
6764 | else if (TREE_CODE (*arg) == FUNCTION_TYPE | |
6765 | || TREE_CODE (*arg) == METHOD_TYPE) | |
6766 | *arg = build_pointer_type (*arg); | |
6767 | else | |
6768 | *arg = TYPE_MAIN_VARIANT (*arg); | |
6769 | } | |
6770 | ||
6771 | /* [temp.deduct.call] | |
6772 | ||
6773 | If P is a cv-qualified type, the top level cv-qualifiers | |
6774 | of P's type are ignored for type deduction. If P is a | |
6775 | reference type, the type referred to by P is used for | |
6776 | type deduction. */ | |
6777 | *parm = TYPE_MAIN_VARIANT (*parm); | |
6778 | if (TREE_CODE (*parm) == REFERENCE_TYPE) | |
6779 | *parm = TREE_TYPE (*parm); | |
386b8a85 JM |
6780 | } |
6781 | ||
050367a3 MM |
6782 | /* Like type_unfication. EXPLICIT_MASK, if non-NULL, is an array of |
6783 | integers, with ones in positions corresponding to arguments in | |
9f54c803 MM |
6784 | targs that were provided explicitly, and zeros elsewhere. |
6785 | ||
6786 | If SUBR is 1, we're being called recursively (to unify the | |
6787 | arguments of a function or method parameter of a function | |
6788 | template). */ | |
386b8a85 | 6789 | |
4966381a | 6790 | static int |
3b3ba9f0 | 6791 | type_unification_real (tparms, targs, parms, args, subr, |
050367a3 MM |
6792 | strict, allow_incomplete, explicit_mask) |
6793 | tree tparms, targs, parms, args; | |
830bfa74 MM |
6794 | int subr; |
6795 | unification_kind_t strict; | |
6796 | int allow_incomplete; | |
050367a3 | 6797 | int* explicit_mask; |
8d08fdba MS |
6798 | { |
6799 | tree parm, arg; | |
6800 | int i; | |
6801 | int ntparms = TREE_VEC_LENGTH (tparms); | |
830bfa74 | 6802 | int sub_strict; |
8d08fdba MS |
6803 | |
6804 | my_friendly_assert (TREE_CODE (tparms) == TREE_VEC, 289); | |
386b8a85 JM |
6805 | my_friendly_assert (parms == NULL_TREE |
6806 | || TREE_CODE (parms) == TREE_LIST, 290); | |
51c184be | 6807 | /* ARGS could be NULL (via a call from parse.y to |
8d08fdba MS |
6808 | build_x_function_call). */ |
6809 | if (args) | |
6810 | my_friendly_assert (TREE_CODE (args) == TREE_LIST, 291); | |
6811 | my_friendly_assert (ntparms > 0, 292); | |
6812 | ||
830bfa74 MM |
6813 | switch (strict) |
6814 | { | |
6815 | case DEDUCE_CALL: | |
6816 | sub_strict = UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_DERIVED; | |
6817 | break; | |
6818 | ||
6819 | case DEDUCE_CONV: | |
6820 | sub_strict = UNIFY_ALLOW_LESS_CV_QUAL; | |
6821 | break; | |
6822 | ||
6823 | case DEDUCE_EXACT: | |
6824 | sub_strict = UNIFY_ALLOW_NONE; | |
6825 | break; | |
6826 | ||
6827 | default: | |
6828 | my_friendly_abort (0); | |
6829 | } | |
6830 | ||
8d08fdba MS |
6831 | while (parms |
6832 | && parms != void_list_node | |
6833 | && args | |
6834 | && args != void_list_node) | |
6835 | { | |
6836 | parm = TREE_VALUE (parms); | |
6837 | parms = TREE_CHAIN (parms); | |
6838 | arg = TREE_VALUE (args); | |
6839 | args = TREE_CHAIN (args); | |
6840 | ||
6841 | if (arg == error_mark_node) | |
6842 | return 1; | |
6843 | if (arg == unknown_type_node) | |
34016c81 JM |
6844 | /* We can't deduce anything from this, but we might get all the |
6845 | template args from other function args. */ | |
6846 | continue; | |
b7484fbe | 6847 | |
03e70705 JM |
6848 | /* Conversions will be performed on a function argument that |
6849 | corresponds with a function parameter that contains only | |
6850 | non-deducible template parameters and explicitly specified | |
6851 | template parameters. */ | |
6852 | if (! uses_template_parms (parm)) | |
b7484fbe | 6853 | { |
03e70705 JM |
6854 | tree type; |
6855 | ||
6856 | if (TREE_CODE_CLASS (TREE_CODE (arg)) != 't') | |
6857 | type = TREE_TYPE (arg); | |
6858 | else | |
6859 | { | |
6860 | type = arg; | |
6861 | arg = NULL_TREE; | |
6862 | } | |
6863 | ||
830bfa74 | 6864 | if (strict == DEDUCE_EXACT) |
03e70705 | 6865 | { |
3bfdc719 | 6866 | if (same_type_p (parm, type)) |
03e70705 JM |
6867 | continue; |
6868 | } | |
03e70705 | 6869 | else |
343c89cd JM |
6870 | /* It might work; we shouldn't check now, because we might |
6871 | get into infinite recursion. Overload resolution will | |
6872 | handle it. */ | |
6873 | continue; | |
03e70705 | 6874 | |
b7484fbe MS |
6875 | return 1; |
6876 | } | |
6877 | ||
8d08fdba MS |
6878 | #if 0 |
6879 | if (TREE_CODE (arg) == VAR_DECL) | |
6880 | arg = TREE_TYPE (arg); | |
6881 | else if (TREE_CODE_CLASS (TREE_CODE (arg)) == 'e') | |
6882 | arg = TREE_TYPE (arg); | |
6883 | #else | |
6884 | if (TREE_CODE_CLASS (TREE_CODE (arg)) != 't') | |
6885 | { | |
6886 | my_friendly_assert (TREE_TYPE (arg) != NULL_TREE, 293); | |
34016c81 | 6887 | if (type_unknown_p (arg)) |
28cbf42c | 6888 | { |
34016c81 JM |
6889 | /* [temp.deduct.type] A template-argument can be deduced from |
6890 | a pointer to function or pointer to member function | |
6891 | argument if the set of overloaded functions does not | |
6892 | contain function templates and at most one of a set of | |
6893 | overloaded functions provides a unique match. */ | |
6894 | ||
6895 | if (resolve_overloaded_unification | |
6896 | (tparms, targs, parm, arg, strict, sub_strict, explicit_mask) | |
6897 | != 0) | |
6898 | return 1; | |
6899 | continue; | |
28cbf42c | 6900 | } |
8d08fdba MS |
6901 | arg = TREE_TYPE (arg); |
6902 | } | |
6903 | #endif | |
830bfa74 MM |
6904 | if (!subr) |
6905 | maybe_adjust_types_for_deduction (strict, &parm, &arg); | |
db5ae43f | 6906 | |
830bfa74 | 6907 | switch (unify (tparms, targs, parm, arg, sub_strict, |
050367a3 | 6908 | explicit_mask)) |
8d08fdba MS |
6909 | { |
6910 | case 0: | |
6911 | break; | |
6912 | case 1: | |
6913 | return 1; | |
6914 | } | |
6915 | } | |
6916 | /* Fail if we've reached the end of the parm list, and more args | |
6917 | are present, and the parm list isn't variadic. */ | |
6918 | if (args && args != void_list_node && parms == void_list_node) | |
6919 | return 1; | |
6920 | /* Fail if parms are left and they don't have default values. */ | |
6921 | if (parms | |
6922 | && parms != void_list_node | |
6923 | && TREE_PURPOSE (parms) == NULL_TREE) | |
6924 | return 1; | |
6925 | if (!subr) | |
6926 | for (i = 0; i < ntparms; i++) | |
050367a3 | 6927 | if (TREE_VEC_ELT (targs, i) == NULL_TREE) |
8d08fdba | 6928 | { |
386b8a85 | 6929 | if (!allow_incomplete) |
8251199e | 6930 | error ("incomplete type unification"); |
8d08fdba MS |
6931 | return 2; |
6932 | } | |
6933 | return 0; | |
6934 | } | |
6935 | ||
34016c81 JM |
6936 | /* Subroutine of type_unification_real. Args are like the variables at the |
6937 | call site. ARG is an overloaded function (or template-id); we try | |
6938 | deducing template args from each of the overloads, and if only one | |
6939 | succeeds, we go with that. Modifies TARGS and returns 0 on success. */ | |
6940 | ||
6941 | static int | |
6942 | resolve_overloaded_unification (tparms, targs, parm, arg, strict, | |
6943 | sub_strict, explicit_mask) | |
6944 | tree tparms, targs, parm, arg; | |
6945 | unification_kind_t strict; | |
6946 | int sub_strict; | |
6947 | int* explicit_mask; | |
6948 | { | |
6949 | tree tempargs = copy_node (targs); | |
6950 | int good = 0; | |
6951 | ||
6952 | if (TREE_CODE (arg) == ADDR_EXPR) | |
6953 | arg = TREE_OPERAND (arg, 0); | |
6954 | if (TREE_CODE (arg) == TEMPLATE_ID_EXPR) | |
6955 | { | |
6956 | /* If we got some explicit template args, we need to plug them into | |
6957 | the affected templates before we try to unify, in case the | |
6958 | explicit args will completely resolve the templates in question. */ | |
6959 | ||
6960 | tree expl_subargs = TREE_OPERAND (arg, 1); | |
6961 | arg = TREE_OPERAND (arg, 0); | |
6962 | ||
6963 | for (; arg; arg = OVL_NEXT (arg)) | |
6964 | { | |
6965 | tree fn = OVL_CURRENT (arg); | |
6966 | tree subargs, elem; | |
6967 | ||
6968 | if (TREE_CODE (fn) != TEMPLATE_DECL) | |
6969 | continue; | |
6970 | ||
6971 | subargs = get_bindings_overload (fn, DECL_RESULT (fn), expl_subargs); | |
6972 | if (subargs) | |
6973 | { | |
6974 | elem = tsubst (TREE_TYPE (fn), subargs, NULL_TREE); | |
6975 | good += try_one_overload (tparms, tempargs, parm, elem, | |
6976 | strict, sub_strict, explicit_mask); | |
6977 | } | |
6978 | } | |
6979 | } | |
6980 | else if (TREE_CODE (arg) == OVERLOAD) | |
6981 | { | |
6982 | for (; arg; arg = OVL_NEXT (arg)) | |
6983 | good += try_one_overload (tparms, tempargs, parm, | |
6984 | TREE_TYPE (OVL_CURRENT (arg)), | |
6985 | strict, sub_strict, explicit_mask); | |
6986 | } | |
6987 | else | |
6988 | my_friendly_abort (981006); | |
6989 | ||
6990 | /* [temp.deduct.type] A template-argument can be deduced from a pointer | |
6991 | to function or pointer to member function argument if the set of | |
6992 | overloaded functions does not contain function templates and at most | |
6993 | one of a set of overloaded functions provides a unique match. | |
6994 | ||
6995 | So if we found multiple possibilities, we return success but don't | |
6996 | deduce anything. */ | |
6997 | ||
6998 | if (good == 1) | |
6999 | { | |
7000 | int i = TREE_VEC_LENGTH (targs); | |
7001 | for (; i--; ) | |
7002 | if (TREE_VEC_ELT (tempargs, i)) | |
7003 | TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (tempargs, i); | |
7004 | } | |
7005 | if (good) | |
7006 | return 0; | |
7007 | ||
7008 | return 1; | |
7009 | } | |
7010 | ||
7011 | /* Subroutine of resolve_overloaded_unification; does deduction for a single | |
7012 | overload. Fills TARGS with any deduced arguments, or error_mark_node if | |
7013 | different overloads deduce different arguments for a given parm. | |
7014 | Returns 1 on success. */ | |
7015 | ||
7016 | static int | |
7017 | try_one_overload (tparms, targs, parm, arg, strict, | |
7018 | sub_strict, explicit_mask) | |
7019 | tree tparms, targs, parm, arg; | |
7020 | unification_kind_t strict; | |
7021 | int sub_strict; | |
7022 | int* explicit_mask; | |
7023 | { | |
7024 | int nargs; | |
7025 | tree tempargs; | |
7026 | int i; | |
7027 | ||
7028 | /* [temp.deduct.type] A template-argument can be deduced from a pointer | |
7029 | to function or pointer to member function argument if the set of | |
7030 | overloaded functions does not contain function templates and at most | |
7031 | one of a set of overloaded functions provides a unique match. | |
7032 | ||
7033 | So if this is a template, just return success. */ | |
7034 | ||
7035 | if (uses_template_parms (arg)) | |
7036 | return 1; | |
7037 | ||
7038 | maybe_adjust_types_for_deduction (strict, &parm, &arg); | |
7039 | ||
7040 | /* We don't copy orig_targs for this because if we have already deduced | |
7041 | some template args from previous args, unify would complain when we | |
7042 | try to deduce a template parameter for the same argument, even though | |
7043 | there isn't really a conflict. */ | |
7044 | nargs = TREE_VEC_LENGTH (targs); | |
7045 | tempargs = make_scratch_vec (nargs); | |
7046 | ||
7047 | if (unify (tparms, tempargs, parm, arg, sub_strict, explicit_mask) != 0) | |
7048 | return 0; | |
7049 | ||
7050 | /* First make sure we didn't deduce anything that conflicts with | |
7051 | previously deduced/specified args. */ | |
7052 | for (i = nargs; i--; ) | |
7053 | { | |
7054 | tree elt = TREE_VEC_ELT (tempargs, i); | |
7055 | tree oldelt = TREE_VEC_ELT (targs, i); | |
7056 | ||
7057 | if (elt == NULL_TREE) | |
7058 | continue; | |
7059 | else if (uses_template_parms (elt)) | |
7060 | { | |
7061 | /* Since we're unifying against ourselves, we will fill in template | |
7062 | args used in the function parm list with our own template parms. | |
7063 | Discard them. */ | |
7064 | TREE_VEC_ELT (tempargs, i) = NULL_TREE; | |
7065 | continue; | |
7066 | } | |
7067 | else if (oldelt && ! template_args_equal (oldelt, elt)) | |
7068 | return 0; | |
7069 | } | |
7070 | ||
7071 | for (i = nargs; i--; ) | |
7072 | { | |
7073 | tree elt = TREE_VEC_ELT (tempargs, i); | |
7074 | ||
7075 | if (elt) | |
7076 | TREE_VEC_ELT (targs, i) = elt; | |
7077 | } | |
7078 | ||
7079 | return 1; | |
7080 | } | |
7081 | ||
db2767b6 MM |
7082 | /* Returns the level of DECL, which declares a template parameter. */ |
7083 | ||
04a81d94 | 7084 | int |
db2767b6 MM |
7085 | template_decl_level (decl) |
7086 | tree decl; | |
7087 | { | |
7088 | switch (TREE_CODE (decl)) | |
7089 | { | |
7090 | case TYPE_DECL: | |
7091 | case TEMPLATE_DECL: | |
7092 | return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl)); | |
7093 | ||
7094 | case PARM_DECL: | |
7095 | return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl)); | |
7096 | ||
7097 | default: | |
7098 | my_friendly_abort (0); | |
5ffe581d | 7099 | return 0; |
db2767b6 MM |
7100 | } |
7101 | } | |
7102 | ||
830bfa74 MM |
7103 | /* Decide whether ARG can be unified with PARM, considering only the |
7104 | cv-qualifiers of each type, given STRICT as documented for unify. | |
7105 | Returns non-zero iff the unification is OK on that basis.*/ | |
e92cc029 | 7106 | |
830bfa74 MM |
7107 | int |
7108 | check_cv_quals_for_unify (strict, arg, parm) | |
7109 | int strict; | |
7110 | tree arg; | |
7111 | tree parm; | |
7112 | { | |
7113 | return !((!(strict & UNIFY_ALLOW_MORE_CV_QUAL) | |
91063b51 | 7114 | && !at_least_as_qualified_p (arg, parm)) |
830bfa74 | 7115 | || (!(strict & UNIFY_ALLOW_LESS_CV_QUAL) |
91063b51 | 7116 | && (!at_least_as_qualified_p (parm, arg)))); |
830bfa74 MM |
7117 | } |
7118 | ||
7119 | /* Takes parameters as for type_unification. Returns 0 if the | |
7120 | type deduction suceeds, 1 otherwise. The parameter STRICT is a | |
7121 | bitwise or of the following flags: | |
7122 | ||
7123 | UNIFY_ALLOW_NONE: | |
7124 | Require an exact match between PARM and ARG. | |
7125 | UNIFY_ALLOW_MORE_CV_QUAL: | |
7126 | Allow the deduced ARG to be more cv-qualified than ARG. | |
7127 | UNIFY_ALLOW_LESS_CV_QUAL: | |
7128 | Allow the deduced ARG to be less cv-qualified than ARG. | |
7129 | UNIFY_ALLOW_DERIVED: | |
7130 | Allow the deduced ARG to be a template base class of ARG, | |
7131 | or a pointer to a template base class of the type pointed to by | |
7132 | ARG. */ | |
7133 | ||
7134 | int | |
7135 | unify (tparms, targs, parm, arg, strict, explicit_mask) | |
050367a3 | 7136 | tree tparms, targs, parm, arg; |
830bfa74 | 7137 | int strict; |
050367a3 | 7138 | int* explicit_mask; |
8d08fdba MS |
7139 | { |
7140 | int idx; | |
050367a3 | 7141 | tree targ; |
db2767b6 | 7142 | tree tparm; |
8d08fdba MS |
7143 | |
7144 | /* I don't think this will do the right thing with respect to types. | |
7145 | But the only case I've seen it in so far has been array bounds, where | |
7146 | signedness is the only information lost, and I think that will be | |
7147 | okay. */ | |
7148 | while (TREE_CODE (parm) == NOP_EXPR) | |
7149 | parm = TREE_OPERAND (parm, 0); | |
7150 | ||
7151 | if (arg == error_mark_node) | |
7152 | return 1; | |
7153 | if (arg == unknown_type_node) | |
34016c81 JM |
7154 | /* We can't deduce anything from this, but we might get all the |
7155 | template args from other function args. */ | |
7156 | return 0; | |
7157 | ||
db2767b6 MM |
7158 | /* If PARM uses template parameters, then we can't bail out here, |
7159 | even in ARG == PARM, since we won't record unifications for the | |
7160 | template parameters. We might need them if we're trying to | |
7161 | figure out which of two things is more specialized. */ | |
7162 | if (arg == parm && !uses_template_parms (parm)) | |
8d08fdba MS |
7163 | return 0; |
7164 | ||
830bfa74 MM |
7165 | /* Immediately reject some pairs that won't unify because of |
7166 | cv-qualification mismatches. */ | |
7167 | if (TREE_CODE (arg) == TREE_CODE (parm) | |
49432171 | 7168 | && TREE_CODE_CLASS (TREE_CODE (arg)) == 't' |
830bfa74 MM |
7169 | /* We check the cv-qualifiers when unifying with template type |
7170 | parameters below. We want to allow ARG `const T' to unify with | |
7171 | PARM `T' for example, when computing which of two templates | |
7172 | is more specialized, for example. */ | |
7173 | && TREE_CODE (arg) != TEMPLATE_TYPE_PARM | |
7174 | && !check_cv_quals_for_unify (strict, arg, parm)) | |
49432171 JM |
7175 | return 1; |
7176 | ||
8d08fdba MS |
7177 | switch (TREE_CODE (parm)) |
7178 | { | |
2ca340ae JM |
7179 | case TYPENAME_TYPE: |
7180 | /* In a type which contains a nested-name-specifier, template | |
7181 | argument values cannot be deduced for template parameters used | |
7182 | within the nested-name-specifier. */ | |
7183 | return 0; | |
7184 | ||
8d08fdba | 7185 | case TEMPLATE_TYPE_PARM: |
73b0fce8 | 7186 | case TEMPLATE_TEMPLATE_PARM: |
db2767b6 MM |
7187 | tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0)); |
7188 | ||
7189 | if (TEMPLATE_TYPE_LEVEL (parm) | |
7190 | != template_decl_level (tparm)) | |
7191 | /* The PARM is not one we're trying to unify. Just check | |
7192 | to see if it matches ARG. */ | |
7193 | return (TREE_CODE (arg) == TREE_CODE (parm) | |
3bfdc719 | 7194 | && same_type_p (parm, arg)) ? 0 : 1; |
73b0fce8 | 7195 | idx = TEMPLATE_TYPE_IDX (parm); |
050367a3 | 7196 | targ = TREE_VEC_ELT (targs, idx); |
db2767b6 | 7197 | tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx)); |
050367a3 | 7198 | |
73b0fce8 | 7199 | /* Check for mixed types and values. */ |
db2767b6 MM |
7200 | if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM |
7201 | && TREE_CODE (tparm) != TYPE_DECL) | |
7202 | || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM | |
7203 | && TREE_CODE (tparm) != TEMPLATE_DECL)) | |
73b0fce8 KL |
7204 | return 1; |
7205 | ||
050367a3 MM |
7206 | if (!strict && targ != NULL_TREE |
7207 | && explicit_mask && explicit_mask[idx]) | |
73b0fce8 KL |
7208 | /* An explicit template argument. Don't even try to match |
7209 | here; the overload resolution code will manage check to | |
7210 | see whether the call is legal. */ | |
7211 | return 0; | |
7212 | ||
db2767b6 | 7213 | if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM) |
73b0fce8 | 7214 | { |
db2767b6 MM |
7215 | if (CLASSTYPE_TEMPLATE_INFO (parm)) |
7216 | { | |
7217 | /* We arrive here when PARM does not involve template | |
7218 | specialization. */ | |
73b0fce8 | 7219 | |
db2767b6 MM |
7220 | /* ARG must be constructed from a template class. */ |
7221 | if (TREE_CODE (arg) != RECORD_TYPE || !CLASSTYPE_TEMPLATE_INFO (arg)) | |
7222 | return 1; | |
73b0fce8 | 7223 | |
db2767b6 MM |
7224 | { |
7225 | tree parmtmpl = CLASSTYPE_TI_TEMPLATE (parm); | |
7226 | tree parmvec = CLASSTYPE_TI_ARGS (parm); | |
7227 | tree argvec = CLASSTYPE_TI_ARGS (arg); | |
7228 | tree argtmplvec | |
7229 | = DECL_INNERMOST_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (arg)); | |
7230 | int i; | |
7231 | ||
7232 | /* The parameter and argument roles have to be switched here | |
7233 | in order to handle default arguments properly. For example, | |
7234 | template<template <class> class TT> void f(TT<int>) | |
7235 | should be able to accept vector<int> which comes from | |
744fac59 | 7236 | template <class T, class Allocator = allocator> |
73b0fce8 KL |
7237 | class vector. */ |
7238 | ||
744fac59 | 7239 | if (coerce_template_parms (argtmplvec, parmvec, parmtmpl, 1, 1) |
db2767b6 MM |
7240 | == error_mark_node) |
7241 | return 1; | |
73b0fce8 | 7242 | |
db2767b6 MM |
7243 | /* Deduce arguments T, i from TT<T> or TT<i>. */ |
7244 | for (i = 0; i < TREE_VEC_LENGTH (parmvec); ++i) | |
7245 | { | |
7246 | tree t = TREE_VEC_ELT (parmvec, i); | |
7247 | if (TREE_CODE (t) != TEMPLATE_TYPE_PARM | |
7248 | && TREE_CODE (t) != TEMPLATE_TEMPLATE_PARM | |
7249 | && TREE_CODE (t) != TEMPLATE_PARM_INDEX) | |
7250 | continue; | |
73b0fce8 | 7251 | |
db2767b6 | 7252 | /* This argument can be deduced. */ |
73b0fce8 | 7253 | |
830bfa74 MM |
7254 | if (unify (tparms, targs, t, |
7255 | TREE_VEC_ELT (argvec, i), | |
7256 | UNIFY_ALLOW_NONE, explicit_mask)) | |
db2767b6 MM |
7257 | return 1; |
7258 | } | |
73b0fce8 | 7259 | } |
db2767b6 MM |
7260 | arg = CLASSTYPE_TI_TEMPLATE (arg); |
7261 | } | |
7262 | } | |
7263 | else | |
7264 | { | |
830bfa74 MM |
7265 | /* If PARM is `const T' and ARG is only `int', we don't have |
7266 | a match unless we are allowing additional qualification. | |
7267 | If ARG is `const int' and PARM is just `T' that's OK; | |
7268 | that binds `const int' to `T'. */ | |
7269 | if (!check_cv_quals_for_unify (strict | UNIFY_ALLOW_LESS_CV_QUAL, | |
7270 | arg, parm)) | |
db2767b6 MM |
7271 | return 1; |
7272 | ||
830bfa74 MM |
7273 | /* Consider the case where ARG is `const volatile int' and |
7274 | PARM is `const T'. Then, T should be `volatile int'. */ | |
7275 | arg = | |
91063b51 MM |
7276 | cp_build_qualified_type (arg, |
7277 | CP_TYPE_QUALS (arg) | |
7278 | & ~CP_TYPE_QUALS (parm)); | |
73b0fce8 KL |
7279 | } |
7280 | ||
7281 | /* Simple cases: Value already set, does match or doesn't. */ | |
3a56f0ab | 7282 | if (targ != NULL_TREE |
3bfdc719 | 7283 | && (same_type_p (targ, arg) |
3a56f0ab | 7284 | || (explicit_mask && explicit_mask[idx]))) |
73b0fce8 | 7285 | return 0; |
050367a3 | 7286 | else if (targ) |
73b0fce8 | 7287 | return 1; |
050367a3 | 7288 | TREE_VEC_ELT (targs, idx) = arg; |
73b0fce8 KL |
7289 | return 0; |
7290 | ||
f84b4be9 | 7291 | case TEMPLATE_PARM_INDEX: |
db2767b6 MM |
7292 | tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0)); |
7293 | ||
7294 | if (TEMPLATE_PARM_LEVEL (parm) | |
7295 | != template_decl_level (tparm)) | |
7296 | /* The PARM is not one we're trying to unify. Just check | |
7297 | to see if it matches ARG. */ | |
7298 | return (TREE_CODE (arg) == TREE_CODE (parm) | |
67ffc812 | 7299 | && cp_tree_equal (parm, arg) > 0) ? 0 : 1; |
db2767b6 | 7300 | |
f84b4be9 | 7301 | idx = TEMPLATE_PARM_IDX (parm); |
050367a3 | 7302 | targ = TREE_VEC_ELT (targs, idx); |
db2767b6 | 7303 | |
050367a3 | 7304 | if (targ) |
8d08fdba | 7305 | { |
67ffc812 | 7306 | int i = (cp_tree_equal (targ, arg) > 0); |
312e7d50 JM |
7307 | if (i == 1) |
7308 | return 0; | |
7309 | else if (i == 0) | |
7310 | return 1; | |
7311 | else | |
7312 | my_friendly_abort (42); | |
8d08fdba | 7313 | } |
8d08fdba | 7314 | |
050367a3 | 7315 | TREE_VEC_ELT (targs, idx) = copy_to_permanent (arg); |
8d08fdba MS |
7316 | return 0; |
7317 | ||
7318 | case POINTER_TYPE: | |
830bfa74 MM |
7319 | { |
7320 | int sub_strict; | |
4ac14744 | 7321 | |
830bfa74 MM |
7322 | if (TREE_CODE (arg) == RECORD_TYPE && TYPE_PTRMEMFUNC_FLAG (arg)) |
7323 | return (unify (tparms, targs, parm, | |
7324 | TYPE_PTRMEMFUNC_FN_TYPE (arg), strict, | |
7325 | explicit_mask)); | |
7326 | ||
7327 | if (TREE_CODE (arg) != POINTER_TYPE) | |
7328 | return 1; | |
7329 | ||
7330 | /* [temp.deduct.call] | |
7331 | ||
7332 | A can be another pointer or pointer to member type that can | |
7333 | be converted to the deduced A via a qualification | |
7334 | conversion (_conv.qual_). | |
7335 | ||
7336 | We pass down STRICT here rather than UNIFY_ALLOW_NONE. | |
7337 | This will allow for additional cv-qualification of the | |
7338 | pointed-to types if appropriate. In general, this is a bit | |
7339 | too generous; we are only supposed to allow qualification | |
7340 | conversions and this method will allow an ARG of char** and | |
7341 | a deduced ARG of const char**. However, overload | |
7342 | resolution will subsequently invalidate the candidate, so | |
7343 | this is probably OK. */ | |
7344 | sub_strict = strict; | |
7345 | ||
7346 | if (TREE_CODE (TREE_TYPE (arg)) != RECORD_TYPE | |
7347 | || TYPE_PTRMEMFUNC_FLAG (TREE_TYPE (arg))) | |
7348 | /* The derived-to-base conversion only persists through one | |
7349 | level of pointers. */ | |
7350 | sub_strict &= ~UNIFY_ALLOW_DERIVED; | |
7351 | ||
7352 | return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE | |
7353 | (arg), sub_strict, explicit_mask); | |
7354 | } | |
8d08fdba MS |
7355 | |
7356 | case REFERENCE_TYPE: | |
830bfa74 MM |
7357 | if (TREE_CODE (arg) != REFERENCE_TYPE) |
7358 | return 1; | |
7359 | return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg), | |
7360 | UNIFY_ALLOW_NONE, explicit_mask); | |
8d08fdba MS |
7361 | |
7362 | case ARRAY_TYPE: | |
7363 | if (TREE_CODE (arg) != ARRAY_TYPE) | |
7364 | return 1; | |
3042d5be MM |
7365 | if ((TYPE_DOMAIN (parm) == NULL_TREE) |
7366 | != (TYPE_DOMAIN (arg) == NULL_TREE)) | |
7367 | return 1; | |
7368 | if (TYPE_DOMAIN (parm) != NULL_TREE | |
830bfa74 MM |
7369 | && unify (tparms, targs, TYPE_DOMAIN (parm), |
7370 | TYPE_DOMAIN (arg), UNIFY_ALLOW_NONE, explicit_mask) != 0) | |
8d08fdba | 7371 | return 1; |
830bfa74 MM |
7372 | return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg), |
7373 | UNIFY_ALLOW_NONE, explicit_mask); | |
8d08fdba MS |
7374 | |
7375 | case REAL_TYPE: | |
37c46b43 | 7376 | case COMPLEX_TYPE: |
8d08fdba | 7377 | case INTEGER_TYPE: |
42976354 | 7378 | case BOOLEAN_TYPE: |
5ad5a526 | 7379 | case VOID_TYPE: |
f376e137 MS |
7380 | if (TREE_CODE (arg) != TREE_CODE (parm)) |
7381 | return 1; | |
7382 | ||
514a1f18 JM |
7383 | if (TREE_CODE (parm) == INTEGER_TYPE |
7384 | && TREE_CODE (TYPE_MAX_VALUE (parm)) != INTEGER_CST) | |
8d08fdba MS |
7385 | { |
7386 | if (TYPE_MIN_VALUE (parm) && TYPE_MIN_VALUE (arg) | |
830bfa74 MM |
7387 | && unify (tparms, targs, TYPE_MIN_VALUE (parm), |
7388 | TYPE_MIN_VALUE (arg), UNIFY_ALLOW_NONE, explicit_mask)) | |
8d08fdba MS |
7389 | return 1; |
7390 | if (TYPE_MAX_VALUE (parm) && TYPE_MAX_VALUE (arg) | |
830bfa74 MM |
7391 | && unify (tparms, targs, TYPE_MAX_VALUE (parm), |
7392 | TYPE_MAX_VALUE (arg), UNIFY_ALLOW_NONE, explicit_mask)) | |
8d08fdba MS |
7393 | return 1; |
7394 | } | |
514a1f18 JM |
7395 | /* We use the TYPE_MAIN_VARIANT since we have already |
7396 | checked cv-qualification at the top of the | |
7397 | function. */ | |
3bfdc719 MM |
7398 | else if (!same_type_p (TYPE_MAIN_VARIANT (arg), |
7399 | TYPE_MAIN_VARIANT (parm))) | |
ca79f85d JM |
7400 | return 1; |
7401 | ||
8d08fdba MS |
7402 | /* As far as unification is concerned, this wins. Later checks |
7403 | will invalidate it if necessary. */ | |
7404 | return 0; | |
7405 | ||
7406 | /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */ | |
bd6dd845 | 7407 | /* Type INTEGER_CST can come from ordinary constant template args. */ |
8d08fdba | 7408 | case INTEGER_CST: |
bd6dd845 MS |
7409 | while (TREE_CODE (arg) == NOP_EXPR) |
7410 | arg = TREE_OPERAND (arg, 0); | |
7411 | ||
8d08fdba MS |
7412 | if (TREE_CODE (arg) != INTEGER_CST) |
7413 | return 1; | |
7414 | return !tree_int_cst_equal (parm, arg); | |
7415 | ||
8d08fdba MS |
7416 | case TREE_VEC: |
7417 | { | |
7418 | int i; | |
7419 | if (TREE_CODE (arg) != TREE_VEC) | |
7420 | return 1; | |
7421 | if (TREE_VEC_LENGTH (parm) != TREE_VEC_LENGTH (arg)) | |
7422 | return 1; | |
7423 | for (i = TREE_VEC_LENGTH (parm) - 1; i >= 0; i--) | |
830bfa74 | 7424 | if (unify (tparms, targs, |
8d08fdba | 7425 | TREE_VEC_ELT (parm, i), TREE_VEC_ELT (arg, i), |
830bfa74 | 7426 | UNIFY_ALLOW_NONE, explicit_mask)) |
8d08fdba MS |
7427 | return 1; |
7428 | return 0; | |
7429 | } | |
7430 | ||
8d08fdba | 7431 | case RECORD_TYPE: |
db5ae43f | 7432 | if (TYPE_PTRMEMFUNC_FLAG (parm)) |
830bfa74 | 7433 | return unify (tparms, targs, TYPE_PTRMEMFUNC_FN_TYPE (parm), |
050367a3 | 7434 | arg, strict, explicit_mask); |
8d08fdba | 7435 | |
830bfa74 | 7436 | if (TREE_CODE (arg) != RECORD_TYPE) |
a4443a08 | 7437 | return 1; |
830bfa74 | 7438 | |
6467930b | 7439 | if (CLASSTYPE_TEMPLATE_INFO (parm) && uses_template_parms (parm)) |
5566b478 | 7440 | { |
6467930b | 7441 | tree t = NULL_TREE; |
830bfa74 MM |
7442 | if (strict & UNIFY_ALLOW_DERIVED) |
7443 | /* [temp.deduct.call] | |
7444 | ||
7445 | If P is a class, and P has the form template-id, then A | |
7446 | can be a derived class of the deduced A. Likewise, if | |
7447 | P is a pointer to a class of the form template-id, A | |
7448 | can be a pointer to a derived class pointed to by the | |
6df47b06 MM |
7449 | deduced A. |
7450 | ||
7451 | The call to get_template_base also handles the case | |
7452 | where PARM and ARG are the same type, i.e., where no | |
7453 | derivation is involved. */ | |
6467930b | 7454 | t = get_template_base (CLASSTYPE_TI_TEMPLATE (parm), arg); |
6df47b06 | 7455 | else if (CLASSTYPE_TEMPLATE_INFO (arg) |
9fbf56f7 MM |
7456 | && (CLASSTYPE_TI_TEMPLATE (parm) |
7457 | == CLASSTYPE_TI_TEMPLATE (arg))) | |
6df47b06 MM |
7458 | /* Perhaps PARM is something like S<U> and ARG is S<int>. |
7459 | Then, we should unify `int' and `U'. */ | |
6467930b | 7460 | t = arg; |
6df47b06 | 7461 | |
6467930b | 7462 | if (! t || t == error_mark_node) |
5566b478 | 7463 | return 1; |
6467930b | 7464 | |
830bfa74 MM |
7465 | return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm), |
7466 | CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE, | |
7467 | explicit_mask); | |
5566b478 | 7468 | } |
3bfdc719 MM |
7469 | else if (!same_type_p (TYPE_MAIN_VARIANT (parm), |
7470 | TYPE_MAIN_VARIANT (arg))) | |
5566b478 | 7471 | return 1; |
a4443a08 | 7472 | return 0; |
8d08fdba MS |
7473 | |
7474 | case METHOD_TYPE: | |
8d08fdba | 7475 | case FUNCTION_TYPE: |
830bfa74 | 7476 | if (TREE_CODE (arg) != TREE_CODE (parm)) |
8d08fdba | 7477 | return 1; |
830bfa74 MM |
7478 | |
7479 | if (unify (tparms, targs, TREE_TYPE (parm), | |
7480 | TREE_TYPE (arg), UNIFY_ALLOW_NONE, explicit_mask)) | |
28cbf42c | 7481 | return 1; |
386b8a85 | 7482 | return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm), |
3b3ba9f0 | 7483 | TYPE_ARG_TYPES (arg), 1, |
830bfa74 | 7484 | DEDUCE_EXACT, 0, explicit_mask); |
a4443a08 MS |
7485 | |
7486 | case OFFSET_TYPE: | |
7487 | if (TREE_CODE (arg) != OFFSET_TYPE) | |
7488 | return 1; | |
830bfa74 MM |
7489 | if (unify (tparms, targs, TYPE_OFFSET_BASETYPE (parm), |
7490 | TYPE_OFFSET_BASETYPE (arg), UNIFY_ALLOW_NONE, explicit_mask)) | |
a4443a08 | 7491 | return 1; |
830bfa74 MM |
7492 | return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg), |
7493 | UNIFY_ALLOW_NONE, explicit_mask); | |
a4443a08 | 7494 | |
f62dbf03 | 7495 | case CONST_DECL: |
830bfa74 | 7496 | if (arg != decl_constant_value (parm)) |
f62dbf03 JM |
7497 | return 1; |
7498 | return 0; | |
7499 | ||
027905b4 KL |
7500 | case TEMPLATE_DECL: |
7501 | /* Matched cases are handled by the ARG == PARM test above. */ | |
7502 | return 1; | |
7503 | ||
53929c47 JM |
7504 | case MINUS_EXPR: |
7505 | if (TREE_CODE (TREE_OPERAND (parm, 1)) == INTEGER_CST) | |
7506 | { | |
7507 | /* We handle this case specially, since it comes up with | |
7508 | arrays. In particular, something like: | |
7509 | ||
7510 | template <int N> void f(int (&x)[N]); | |
7511 | ||
7512 | Here, we are trying to unify the range type, which | |
7513 | looks like [0 ... (N - 1)]. */ | |
7514 | tree t, t1, t2; | |
7515 | t1 = TREE_OPERAND (parm, 0); | |
7516 | t2 = TREE_OPERAND (parm, 1); | |
7517 | ||
7518 | /* Should this be a regular fold? */ | |
7519 | t = maybe_fold_nontype_arg (build (PLUS_EXPR, | |
7520 | integer_type_node, | |
7521 | arg, t2)); | |
7522 | ||
7523 | return unify (tparms, targs, t1, t, UNIFY_ALLOW_NONE, | |
7524 | explicit_mask); | |
7525 | } | |
7526 | /* else fall through */ | |
7527 | ||
8d08fdba | 7528 | default: |
050367a3 MM |
7529 | if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (parm)))) |
7530 | { | |
7531 | /* We're looking at an expression. This can happen with | |
7532 | something like: | |
7533 | ||
7534 | template <int I> | |
7535 | void foo(S<I>, S<I + 2>); | |
7536 | ||
7537 | If the call looked like: | |
7538 | ||
7539 | foo(S<2>(), S<4>()); | |
7540 | ||
7541 | we would have already matched `I' with `2'. Now, we'd | |
7542 | like to know if `4' matches `I + 2'. So, we substitute | |
7543 | into that expression, and fold constants, in the hope of | |
7544 | figuring it out. */ | |
7545 | tree t = | |
7546 | maybe_fold_nontype_arg (tsubst_expr (parm, targs, NULL_TREE)); | |
53929c47 | 7547 | tree a = maybe_fold_nontype_arg (arg); |
050367a3 | 7548 | |
53929c47 | 7549 | if (!IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (t)))) |
050367a3 | 7550 | /* Good, we mangaged to simplify the exression. */ |
53929c47 | 7551 | return unify (tparms, targs, t, a, UNIFY_ALLOW_NONE, |
050367a3 MM |
7552 | explicit_mask); |
7553 | else | |
7554 | /* Bad, we couldn't simplify this. Assume it doesn't | |
7555 | unify. */ | |
7556 | return 1; | |
7557 | } | |
7558 | else | |
b4c4a9ec MM |
7559 | sorry ("use of `%s' in template type unification", |
7560 | tree_code_name [(int) TREE_CODE (parm)]); | |
050367a3 | 7561 | |
8d08fdba MS |
7562 | return 1; |
7563 | } | |
7564 | } | |
8d08fdba | 7565 | \f |
03d0f4af MM |
7566 | /* Called if RESULT is explicitly instantiated, or is a member of an |
7567 | explicitly instantiated class, or if using -frepo and the | |
7568 | instantiation of RESULT has been assigned to this file. */ | |
7569 | ||
faae18ab | 7570 | void |
5566b478 | 7571 | mark_decl_instantiated (result, extern_p) |
faae18ab MS |
7572 | tree result; |
7573 | int extern_p; | |
7574 | { | |
75650646 MM |
7575 | if (TREE_CODE (result) != FUNCTION_DECL) |
7576 | /* The TREE_PUBLIC flag for function declarations will have been | |
7577 | set correctly by tsubst. */ | |
7578 | TREE_PUBLIC (result) = 1; | |
faae18ab MS |
7579 | |
7580 | if (! extern_p) | |
7581 | { | |
7582 | DECL_INTERFACE_KNOWN (result) = 1; | |
7583 | DECL_NOT_REALLY_EXTERN (result) = 1; | |
a7d87521 | 7584 | |
1a408d07 JM |
7585 | /* Always make artificials weak. */ |
7586 | if (DECL_ARTIFICIAL (result) && flag_weak) | |
7587 | comdat_linkage (result); | |
a7d87521 JM |
7588 | /* For WIN32 we also want to put explicit instantiations in |
7589 | linkonce sections. */ | |
1a408d07 | 7590 | else if (TREE_PUBLIC (result)) |
b385c841 | 7591 | maybe_make_one_only (result); |
faae18ab | 7592 | } |
f49422da MS |
7593 | else if (TREE_CODE (result) == FUNCTION_DECL) |
7594 | mark_inline_for_output (result); | |
faae18ab MS |
7595 | } |
7596 | ||
e1467ff2 MM |
7597 | /* Given two function templates PAT1 and PAT2, and explicit template |
7598 | arguments EXPLICIT_ARGS return: | |
6467930b MS |
7599 | |
7600 | 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order]. | |
7601 | -1 if PAT2 is more specialized than PAT1. | |
7602 | 0 if neither is more specialized. */ | |
7603 | ||
7604 | int | |
e1467ff2 MM |
7605 | more_specialized (pat1, pat2, explicit_args) |
7606 | tree pat1, pat2, explicit_args; | |
6467930b | 7607 | { |
98c1c668 | 7608 | tree targs; |
73aad9b9 | 7609 | int winner = 0; |
6467930b | 7610 | |
76b9a14d | 7611 | targs = get_bindings_overload (pat1, pat2, explicit_args); |
73aad9b9 | 7612 | if (targs) |
36a117a5 | 7613 | --winner; |
6467930b | 7614 | |
76b9a14d | 7615 | targs = get_bindings_overload (pat2, pat1, explicit_args); |
73aad9b9 | 7616 | if (targs) |
36a117a5 | 7617 | ++winner; |
6467930b | 7618 | |
73aad9b9 JM |
7619 | return winner; |
7620 | } | |
6467930b | 7621 | |
73aad9b9 | 7622 | /* Given two class template specialization list nodes PAT1 and PAT2, return: |
6467930b | 7623 | |
73aad9b9 JM |
7624 | 1 if PAT1 is more specialized than PAT2 as described in [temp.class.order]. |
7625 | -1 if PAT2 is more specialized than PAT1. | |
7626 | 0 if neither is more specialized. */ | |
7627 | ||
7628 | int | |
7629 | more_specialized_class (pat1, pat2) | |
7630 | tree pat1, pat2; | |
7631 | { | |
7632 | tree targs; | |
7633 | int winner = 0; | |
7634 | ||
36a117a5 MM |
7635 | targs = get_class_bindings (TREE_VALUE (pat1), TREE_PURPOSE (pat1), |
7636 | TREE_PURPOSE (pat2)); | |
73aad9b9 JM |
7637 | if (targs) |
7638 | --winner; | |
7639 | ||
36a117a5 MM |
7640 | targs = get_class_bindings (TREE_VALUE (pat2), TREE_PURPOSE (pat2), |
7641 | TREE_PURPOSE (pat1)); | |
73aad9b9 | 7642 | if (targs) |
6467930b MS |
7643 | ++winner; |
7644 | ||
7645 | return winner; | |
7646 | } | |
73aad9b9 JM |
7647 | |
7648 | /* Return the template arguments that will produce the function signature | |
e1467ff2 | 7649 | DECL from the function template FN, with the explicit template |
76b9a14d JM |
7650 | arguments EXPLICIT_ARGS. If CHECK_RETTYPE is 1, the return type must |
7651 | also match. */ | |
73aad9b9 | 7652 | |
76b9a14d JM |
7653 | static tree |
7654 | get_bindings_real (fn, decl, explicit_args, check_rettype) | |
e1467ff2 | 7655 | tree fn, decl, explicit_args; |
76b9a14d | 7656 | int check_rettype; |
73aad9b9 | 7657 | { |
98c1c668 | 7658 | int ntparms = DECL_NTPARMS (fn); |
e66d884e | 7659 | tree targs = make_scratch_vec (ntparms); |
d7684f2d MM |
7660 | tree decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl)); |
7661 | tree extra_fn_arg = NULL_TREE; | |
98c1c668 JM |
7662 | int i; |
7663 | ||
d7684f2d MM |
7664 | if (DECL_STATIC_FUNCTION_P (fn) |
7665 | && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)) | |
7666 | { | |
7667 | /* Sometimes we are trying to figure out what's being | |
7668 | specialized by a declaration that looks like a method, and it | |
7669 | turns out to be a static member function. */ | |
7670 | if (CLASSTYPE_TEMPLATE_INFO (DECL_REAL_CONTEXT (fn)) | |
7671 | && !is_member_template (fn)) | |
7672 | /* The natural thing to do here seems to be to remove the | |
7673 | spurious `this' parameter from the DECL, but that prevents | |
7674 | unification from making use of the class type. So, | |
7675 | instead, we have fn_type_unification add to the parameters | |
7676 | for FN. */ | |
7677 | extra_fn_arg = build_pointer_type (DECL_REAL_CONTEXT (fn)); | |
7678 | else | |
7679 | /* In this case, though, adding the extra_fn_arg can confuse | |
7680 | things, so we remove from decl_arg_types instead. */ | |
7681 | decl_arg_types = TREE_CHAIN (decl_arg_types); | |
7682 | } | |
7683 | ||
e1467ff2 | 7684 | i = fn_type_unification (fn, explicit_args, targs, |
d7684f2d | 7685 | decl_arg_types, |
98c1c668 | 7686 | TREE_TYPE (TREE_TYPE (decl)), |
830bfa74 | 7687 | DEDUCE_EXACT, |
d7684f2d | 7688 | extra_fn_arg); |
98c1c668 | 7689 | |
76b9a14d JM |
7690 | if (i != 0) |
7691 | return NULL_TREE; | |
7692 | ||
7693 | if (check_rettype) | |
e1467ff2 MM |
7694 | { |
7695 | /* Check to see that the resulting return type is also OK. */ | |
36a117a5 | 7696 | tree t = tsubst (TREE_TYPE (TREE_TYPE (fn)), targs, |
e6f1275f | 7697 | NULL_TREE); |
e1467ff2 | 7698 | |
3bfdc719 | 7699 | if (!same_type_p (t, TREE_TYPE (TREE_TYPE (decl)))) |
e1467ff2 | 7700 | return NULL_TREE; |
e1467ff2 MM |
7701 | } |
7702 | ||
76b9a14d JM |
7703 | return targs; |
7704 | } | |
7705 | ||
7706 | /* For most uses, we want to check the return type. */ | |
7707 | ||
7708 | tree | |
7709 | get_bindings (fn, decl, explicit_args) | |
7710 | tree fn, decl, explicit_args; | |
7711 | { | |
7712 | return get_bindings_real (fn, decl, explicit_args, 1); | |
7713 | } | |
7714 | ||
7715 | /* But for more_specialized, we only care about the parameter types. */ | |
7716 | ||
7717 | static tree | |
7718 | get_bindings_overload (fn, decl, explicit_args) | |
7719 | tree fn, decl, explicit_args; | |
7720 | { | |
7721 | return get_bindings_real (fn, decl, explicit_args, 0); | |
73aad9b9 JM |
7722 | } |
7723 | ||
36a117a5 MM |
7724 | /* Return the innermost template arguments that, when applied to a |
7725 | template specialization whose innermost template parameters are | |
7726 | TPARMS, and whose specialization arguments are ARGS, yield the | |
7727 | ARGS. | |
7728 | ||
7729 | For example, suppose we have: | |
7730 | ||
7731 | template <class T, class U> struct S {}; | |
7732 | template <class T> struct S<T*, int> {}; | |
7733 | ||
7734 | Then, suppose we want to get `S<double*, int>'. The TPARMS will be | |
7735 | {T}, the PARMS will be {T*, int} and the ARGS will be {double*, | |
7736 | int}. The resulting vector will be {double}, indicating that `T' | |
7737 | is bound to `double'. */ | |
7738 | ||
bd6dd845 | 7739 | static tree |
36a117a5 MM |
7740 | get_class_bindings (tparms, parms, args) |
7741 | tree tparms, parms, args; | |
73aad9b9 | 7742 | { |
3b3ba9f0 | 7743 | int i, ntparms = TREE_VEC_LENGTH (tparms); |
73aad9b9 JM |
7744 | tree vec = make_temp_vec (ntparms); |
7745 | ||
36a117a5 | 7746 | args = innermost_args (args); |
8d019cef | 7747 | |
73aad9b9 JM |
7748 | for (i = 0; i < TREE_VEC_LENGTH (parms); ++i) |
7749 | { | |
830bfa74 | 7750 | switch (unify (tparms, vec, |
050367a3 | 7751 | TREE_VEC_ELT (parms, i), TREE_VEC_ELT (args, i), |
830bfa74 | 7752 | UNIFY_ALLOW_NONE, 0)) |
73aad9b9 JM |
7753 | { |
7754 | case 0: | |
7755 | break; | |
7756 | case 1: | |
7757 | return NULL_TREE; | |
7758 | } | |
7759 | } | |
7760 | ||
7761 | for (i = 0; i < ntparms; ++i) | |
7762 | if (! TREE_VEC_ELT (vec, i)) | |
7763 | return NULL_TREE; | |
7764 | ||
7765 | return vec; | |
7766 | } | |
7767 | ||
7768 | /* Return the most specialized of the list of templates in FNS that can | |
e1467ff2 MM |
7769 | produce an instantiation matching DECL, given the explicit template |
7770 | arguments EXPLICIT_ARGS. */ | |
73aad9b9 JM |
7771 | |
7772 | tree | |
e1467ff2 MM |
7773 | most_specialized (fns, decl, explicit_args) |
7774 | tree fns, decl, explicit_args; | |
73aad9b9 | 7775 | { |
36a117a5 MM |
7776 | tree candidates = NULL_TREE; |
7777 | tree fn, champ, args; | |
73aad9b9 JM |
7778 | int fate; |
7779 | ||
36a117a5 | 7780 | for (fn = fns; fn; fn = TREE_CHAIN (fn)) |
73aad9b9 | 7781 | { |
36a117a5 MM |
7782 | tree candidate = TREE_VALUE (fn); |
7783 | ||
7784 | args = get_bindings (candidate, decl, explicit_args); | |
73aad9b9 | 7785 | if (args) |
36a117a5 MM |
7786 | candidates = scratch_tree_cons (NULL_TREE, candidate, |
7787 | candidates); | |
73aad9b9 JM |
7788 | } |
7789 | ||
36a117a5 | 7790 | if (!candidates) |
73aad9b9 JM |
7791 | return NULL_TREE; |
7792 | ||
36a117a5 MM |
7793 | champ = TREE_VALUE (candidates); |
7794 | for (fn = TREE_CHAIN (candidates); fn; fn = TREE_CHAIN (fn)) | |
73aad9b9 | 7795 | { |
e1467ff2 | 7796 | fate = more_specialized (champ, TREE_VALUE (fn), explicit_args); |
73aad9b9 JM |
7797 | if (fate == 1) |
7798 | ; | |
7799 | else | |
7800 | { | |
7801 | if (fate == 0) | |
7802 | { | |
7803 | fn = TREE_CHAIN (fn); | |
7804 | if (! fn) | |
7805 | return error_mark_node; | |
7806 | } | |
7807 | champ = TREE_VALUE (fn); | |
7808 | } | |
7809 | } | |
7810 | ||
36a117a5 | 7811 | for (fn = candidates; fn && TREE_VALUE (fn) != champ; fn = TREE_CHAIN (fn)) |
73aad9b9 | 7812 | { |
e1467ff2 | 7813 | fate = more_specialized (champ, TREE_VALUE (fn), explicit_args); |
73aad9b9 JM |
7814 | if (fate != 1) |
7815 | return error_mark_node; | |
7816 | } | |
7817 | ||
7818 | return champ; | |
7819 | } | |
7820 | ||
36a117a5 MM |
7821 | /* If DECL is a specialization of some template, return the most |
7822 | general such template. For example, given: | |
7823 | ||
7824 | template <class T> struct S { template <class U> void f(U); }; | |
7825 | ||
7826 | if TMPL is `template <class U> void S<int>::f(U)' this will return | |
7827 | the full template. This function will not trace past partial | |
7828 | specializations, however. For example, given in addition: | |
7829 | ||
7830 | template <class T> struct S<T*> { template <class U> void f(U); }; | |
7831 | ||
7832 | if TMPL is `template <class U> void S<int*>::f(U)' this will return | |
7833 | `template <class T> template <class U> S<T*>::f(U)'. */ | |
73aad9b9 JM |
7834 | |
7835 | tree | |
36a117a5 MM |
7836 | most_general_template (decl) |
7837 | tree decl; | |
73aad9b9 | 7838 | { |
36a117a5 MM |
7839 | while (DECL_TEMPLATE_INFO (decl)) |
7840 | decl = DECL_TI_TEMPLATE (decl); | |
7841 | ||
7842 | return decl; | |
7843 | } | |
7844 | ||
7845 | /* Return the most specialized of the class template specializations | |
7846 | of TMPL which can produce an instantiation matching ARGS, or | |
7847 | error_mark_node if the choice is ambiguous. */ | |
7848 | ||
7849 | tree | |
7850 | most_specialized_class (tmpl, args) | |
7851 | tree tmpl; | |
7852 | tree args; | |
7853 | { | |
7854 | tree list = NULL_TREE; | |
7855 | tree t; | |
7856 | tree champ; | |
73aad9b9 JM |
7857 | int fate; |
7858 | ||
36a117a5 MM |
7859 | tmpl = most_general_template (tmpl); |
7860 | for (t = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); t; t = TREE_CHAIN (t)) | |
73aad9b9 | 7861 | { |
36a117a5 MM |
7862 | tree spec_args |
7863 | = get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t), args); | |
7864 | if (spec_args) | |
73aad9b9 JM |
7865 | { |
7866 | list = decl_tree_cons (TREE_PURPOSE (t), TREE_VALUE (t), list); | |
7867 | TREE_TYPE (list) = TREE_TYPE (t); | |
7868 | } | |
7869 | } | |
7870 | ||
7871 | if (! list) | |
7872 | return NULL_TREE; | |
7873 | ||
7874 | t = list; | |
7875 | champ = t; | |
7876 | t = TREE_CHAIN (t); | |
7877 | for (; t; t = TREE_CHAIN (t)) | |
7878 | { | |
7879 | fate = more_specialized_class (champ, t); | |
7880 | if (fate == 1) | |
7881 | ; | |
7882 | else | |
7883 | { | |
7884 | if (fate == 0) | |
7885 | { | |
7886 | t = TREE_CHAIN (t); | |
7887 | if (! t) | |
7888 | return error_mark_node; | |
7889 | } | |
7890 | champ = t; | |
7891 | } | |
7892 | } | |
7893 | ||
7894 | for (t = list; t && t != champ; t = TREE_CHAIN (t)) | |
7895 | { | |
85b71cf2 | 7896 | fate = more_specialized_class (champ, t); |
73aad9b9 JM |
7897 | if (fate != 1) |
7898 | return error_mark_node; | |
7899 | } | |
7900 | ||
7901 | return champ; | |
7902 | } | |
7903 | ||
8d08fdba | 7904 | /* called from the parser. */ |
e92cc029 | 7905 | |
8d08fdba | 7906 | void |
6633d636 | 7907 | do_decl_instantiation (declspecs, declarator, storage) |
f0e01782 | 7908 | tree declspecs, declarator, storage; |
8d08fdba | 7909 | { |
c11b6f21 | 7910 | tree decl = grokdeclarator (declarator, declspecs, NORMAL, 0, NULL_TREE); |
8d08fdba | 7911 | tree result = NULL_TREE; |
faae18ab | 7912 | int extern_p = 0; |
e8abc66f | 7913 | |
ec255269 MS |
7914 | if (! DECL_LANG_SPECIFIC (decl)) |
7915 | { | |
8251199e | 7916 | cp_error ("explicit instantiation of non-template `%#D'", decl); |
ec255269 MS |
7917 | return; |
7918 | } | |
03d0f4af | 7919 | else if (TREE_CODE (decl) == VAR_DECL) |
6633d636 | 7920 | { |
03d0f4af MM |
7921 | /* There is an asymmetry here in the way VAR_DECLs and |
7922 | FUNCTION_DECLs are handled by grokdeclarator. In the case of | |
7923 | the latter, the DECL we get back will be marked as a | |
7924 | template instantiation, and the appropriate | |
7925 | DECL_TEMPLATE_INFO will be set up. This does not happen for | |
7926 | VAR_DECLs so we do the lookup here. Probably, grokdeclarator | |
7927 | should handle VAR_DECLs as it currently handles | |
7928 | FUNCTION_DECLs. */ | |
6633d636 MS |
7929 | result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, 0); |
7930 | if (result && TREE_CODE (result) != VAR_DECL) | |
03d0f4af | 7931 | { |
8251199e | 7932 | cp_error ("no matching template for `%D' found", result); |
03d0f4af MM |
7933 | return; |
7934 | } | |
6633d636 MS |
7935 | } |
7936 | else if (TREE_CODE (decl) != FUNCTION_DECL) | |
7937 | { | |
8251199e | 7938 | cp_error ("explicit instantiation of `%#D'", decl); |
6633d636 MS |
7939 | return; |
7940 | } | |
03d0f4af MM |
7941 | else |
7942 | result = decl; | |
672476cb | 7943 | |
03d0f4af MM |
7944 | /* Check for various error cases. Note that if the explicit |
7945 | instantiation is legal the RESULT will currently be marked as an | |
7946 | *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set | |
7947 | until we get here. */ | |
7948 | ||
7949 | if (DECL_TEMPLATE_SPECIALIZATION (result)) | |
672476cb | 7950 | { |
03d0f4af MM |
7951 | /* [temp.spec] |
7952 | ||
7953 | No program shall both explicitly instantiate and explicitly | |
7954 | specialize a template. */ | |
8251199e JM |
7955 | cp_error ("explicit instantiation of `%#D' after", result); |
7956 | cp_error_at ("explicit specialization here", result); | |
672476cb MM |
7957 | return; |
7958 | } | |
03d0f4af MM |
7959 | else if (DECL_EXPLICIT_INSTANTIATION (result)) |
7960 | { | |
7961 | /* [temp.spec] | |
98c1c668 | 7962 | |
03d0f4af MM |
7963 | No program shall explicitly instantiate any template more |
7964 | than once. | |
7965 | ||
7966 | We check DECL_INTERFACE_KNOWN so as not to complain when the | |
7967 | first instantiation was `extern' and the second is not, and | |
7968 | EXTERN_P for the opposite case. */ | |
7969 | if (DECL_INTERFACE_KNOWN (result) && !extern_p) | |
8251199e | 7970 | cp_error ("duplicate explicit instantiation of `%#D'", result); |
03d0f4af MM |
7971 | |
7972 | /* If we've already instantiated the template, just return now. */ | |
7973 | if (DECL_INTERFACE_KNOWN (result)) | |
7974 | return; | |
7975 | } | |
7976 | else if (!DECL_IMPLICIT_INSTANTIATION (result)) | |
faae18ab | 7977 | { |
8251199e | 7978 | cp_error ("no matching template for `%D' found", result); |
faae18ab MS |
7979 | return; |
7980 | } | |
03d0f4af | 7981 | else if (!DECL_TEMPLATE_INFO (result)) |
6633d636 | 7982 | { |
8251199e | 7983 | cp_pedwarn ("explicit instantiation of non-template `%#D'", result); |
6633d636 MS |
7984 | return; |
7985 | } | |
7986 | ||
a0a33927 MS |
7987 | if (flag_external_templates) |
7988 | return; | |
7989 | ||
f0e01782 | 7990 | if (storage == NULL_TREE) |
00595019 | 7991 | ; |
faae18ab | 7992 | else if (storage == ridpointers[(int) RID_EXTERN]) |
03d0f4af MM |
7993 | { |
7994 | if (pedantic) | |
8251199e | 7995 | cp_pedwarn ("ANSI C++ forbids the use of `extern' on explicit instantiations"); |
03d0f4af MM |
7996 | extern_p = 1; |
7997 | } | |
f0e01782 | 7998 | else |
8251199e | 7999 | cp_error ("storage class `%D' applied to template instantiation", |
f0e01782 | 8000 | storage); |
5566b478 | 8001 | |
03d0f4af | 8002 | SET_DECL_EXPLICIT_INSTANTIATION (result); |
5566b478 | 8003 | mark_decl_instantiated (result, extern_p); |
44a8d0b3 | 8004 | repo_template_instantiated (result, extern_p); |
c91a56d2 MS |
8005 | if (! extern_p) |
8006 | instantiate_decl (result); | |
7177d104 MS |
8007 | } |
8008 | ||
faae18ab MS |
8009 | void |
8010 | mark_class_instantiated (t, extern_p) | |
8011 | tree t; | |
8012 | int extern_p; | |
8013 | { | |
8014 | SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t); | |
eb773359 JM |
8015 | SET_CLASSTYPE_INTERFACE_KNOWN (t); |
8016 | CLASSTYPE_INTERFACE_ONLY (t) = extern_p; | |
faae18ab MS |
8017 | CLASSTYPE_VTABLE_NEEDS_WRITING (t) = ! extern_p; |
8018 | TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p; | |
8019 | if (! extern_p) | |
8020 | { | |
8021 | CLASSTYPE_DEBUG_REQUESTED (t) = 1; | |
8022 | rest_of_type_compilation (t, 1); | |
8023 | } | |
8024 | } | |
e8abc66f | 8025 | |
7177d104 | 8026 | void |
ca79f85d JM |
8027 | do_type_instantiation (t, storage) |
8028 | tree t, storage; | |
7177d104 | 8029 | { |
e8abc66f MS |
8030 | int extern_p = 0; |
8031 | int nomem_p = 0; | |
5566b478 MS |
8032 | int static_p = 0; |
8033 | ||
ca79f85d JM |
8034 | if (TREE_CODE (t) == TYPE_DECL) |
8035 | t = TREE_TYPE (t); | |
8036 | ||
8037 | if (! IS_AGGR_TYPE (t) || ! CLASSTYPE_TEMPLATE_INFO (t)) | |
8038 | { | |
8251199e | 8039 | cp_error ("explicit instantiation of non-template type `%T'", t); |
ca79f85d JM |
8040 | return; |
8041 | } | |
8042 | ||
5566b478 | 8043 | complete_type (t); |
7177d104 | 8044 | |
a292b002 MS |
8045 | /* With -fexternal-templates, explicit instantiations are treated the same |
8046 | as implicit ones. */ | |
a0a33927 MS |
8047 | if (flag_external_templates) |
8048 | return; | |
8049 | ||
f0e01782 MS |
8050 | if (TYPE_SIZE (t) == NULL_TREE) |
8051 | { | |
8251199e | 8052 | cp_error ("explicit instantiation of `%#T' before definition of template", |
f0e01782 MS |
8053 | t); |
8054 | return; | |
8055 | } | |
8056 | ||
03d0f4af | 8057 | if (storage != NULL_TREE) |
f0e01782 | 8058 | { |
03d0f4af | 8059 | if (pedantic) |
8251199e JM |
8060 | cp_pedwarn("ANSI C++ forbids the use of `%s' on explicit instantiations", |
8061 | IDENTIFIER_POINTER (storage)); | |
03d0f4af MM |
8062 | |
8063 | if (storage == ridpointers[(int) RID_INLINE]) | |
8064 | nomem_p = 1; | |
8065 | else if (storage == ridpointers[(int) RID_EXTERN]) | |
8066 | extern_p = 1; | |
8067 | else if (storage == ridpointers[(int) RID_STATIC]) | |
8068 | static_p = 1; | |
8069 | else | |
8070 | { | |
8251199e | 8071 | cp_error ("storage class `%D' applied to template instantiation", |
03d0f4af MM |
8072 | storage); |
8073 | extern_p = 0; | |
8074 | } | |
f0e01782 MS |
8075 | } |
8076 | ||
03d0f4af MM |
8077 | if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t)) |
8078 | { | |
8079 | /* [temp.spec] | |
a292b002 | 8080 | |
03d0f4af MM |
8081 | No program shall both explicitly instantiate and explicitly |
8082 | specialize a template. */ | |
8251199e JM |
8083 | cp_error ("explicit instantiation of `%#T' after", t); |
8084 | cp_error_at ("explicit specialization here", t); | |
03d0f4af MM |
8085 | return; |
8086 | } | |
8087 | else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t)) | |
44a8d0b3 | 8088 | { |
03d0f4af MM |
8089 | /* [temp.spec] |
8090 | ||
8091 | No program shall explicitly instantiate any template more | |
8092 | than once. | |
8093 | ||
8094 | If CLASSTYPE_INTERFACE_ONLY, then the first explicit | |
8095 | instantiation was `extern', and if EXTERN_P then the second | |
8096 | is. Both cases are OK. */ | |
8097 | if (!CLASSTYPE_INTERFACE_ONLY (t) && !extern_p) | |
8251199e | 8098 | cp_error ("duplicate explicit instantiation of `%#T'", t); |
03d0f4af MM |
8099 | |
8100 | /* If we've already instantiated the template, just return now. */ | |
8101 | if (!CLASSTYPE_INTERFACE_ONLY (t)) | |
8102 | return; | |
44a8d0b3 | 8103 | } |
e8abc66f | 8104 | |
03d0f4af MM |
8105 | mark_class_instantiated (t, extern_p); |
8106 | repo_template_instantiated (t, extern_p); | |
8107 | ||
e8abc66f MS |
8108 | if (nomem_p) |
8109 | return; | |
8110 | ||
7177d104 | 8111 | { |
db5ae43f | 8112 | tree tmp; |
5566b478 | 8113 | |
03d0f4af MM |
8114 | /* In contrast to implicit instantiation, where only the |
8115 | declarations, and not the definitions, of members are | |
8116 | instantiated, we have here: | |
8117 | ||
8118 | [temp.explicit] | |
8119 | ||
8120 | The explicit instantiation of a class template specialization | |
8121 | implies the instantiation of all of its members not | |
8122 | previously explicitly specialized in the translation unit | |
8123 | containing the explicit instantiation. | |
8124 | ||
8125 | Of course, we can't instantiate member template classes, since | |
8126 | we don't have any arguments for them. Note that the standard | |
8127 | is unclear on whether the instatiation of the members are | |
8128 | *explicit* instantiations or not. We choose to be generous, | |
8129 | and not set DECL_EXPLICIT_INSTANTIATION. Therefore, we allow | |
8130 | the explicit instantiation of a class where some of the members | |
8131 | have no definition in the current translation unit. */ | |
8132 | ||
5566b478 MS |
8133 | if (! static_p) |
8134 | for (tmp = TYPE_METHODS (t); tmp; tmp = TREE_CHAIN (tmp)) | |
a7d87521 | 8135 | if (TREE_CODE (tmp) == FUNCTION_DECL |
1f109f0f | 8136 | && DECL_TEMPLATE_INSTANTIATION (tmp)) |
5566b478 MS |
8137 | { |
8138 | mark_decl_instantiated (tmp, extern_p); | |
8139 | repo_template_instantiated (tmp, extern_p); | |
8140 | if (! extern_p) | |
8141 | instantiate_decl (tmp); | |
8142 | } | |
8143 | ||
8144 | for (tmp = TYPE_FIELDS (t); tmp; tmp = TREE_CHAIN (tmp)) | |
8145 | if (TREE_CODE (tmp) == VAR_DECL && DECL_TEMPLATE_INSTANTIATION (tmp)) | |
863adfc0 | 8146 | { |
5566b478 | 8147 | mark_decl_instantiated (tmp, extern_p); |
863adfc0 | 8148 | repo_template_instantiated (tmp, extern_p); |
5566b478 MS |
8149 | if (! extern_p) |
8150 | instantiate_decl (tmp); | |
863adfc0 | 8151 | } |
7177d104 | 8152 | |
a292b002 | 8153 | for (tmp = CLASSTYPE_TAGS (t); tmp; tmp = TREE_CHAIN (tmp)) |
e4a84209 MM |
8154 | if (IS_AGGR_TYPE (TREE_VALUE (tmp)) |
8155 | && !uses_template_parms (CLASSTYPE_TI_ARGS (TREE_VALUE (tmp)))) | |
f376e137 | 8156 | do_type_instantiation (TYPE_MAIN_DECL (TREE_VALUE (tmp)), storage); |
a292b002 | 8157 | } |
8d08fdba | 8158 | } |
a28e3c7f | 8159 | |
36a117a5 MM |
8160 | /* Given a function DECL, which is a specialization of TMPL, modify |
8161 | DECL to be a re-instantiation of TMPL with the same template | |
8162 | arguments. TMPL should be the template into which tsubst'ing | |
8163 | should occur for DECL, not the most general template. | |
830bfa74 MM |
8164 | |
8165 | One reason for doing this is a scenario like this: | |
8166 | ||
8167 | template <class T> | |
8168 | void f(const T&, int i); | |
8169 | ||
8170 | void g() { f(3, 7); } | |
8171 | ||
8172 | template <class T> | |
8173 | void f(const T& t, const int i) { } | |
8174 | ||
8175 | Note that when the template is first instantiated, with | |
8176 | instantiate_template, the resulting DECL will have no name for the | |
8177 | first parameter, and the wrong type for the second. So, when we go | |
8178 | to instantiate the DECL, we regenerate it. */ | |
8179 | ||
8180 | void | |
8181 | regenerate_decl_from_template (decl, tmpl) | |
8182 | tree decl; | |
8183 | tree tmpl; | |
8184 | { | |
8185 | tree args; | |
830bfa74 MM |
8186 | tree code_pattern; |
8187 | tree new_decl; | |
36a117a5 MM |
8188 | tree gen_tmpl; |
8189 | int unregistered; | |
830bfa74 MM |
8190 | |
8191 | args = DECL_TI_ARGS (decl); | |
8192 | code_pattern = DECL_TEMPLATE_RESULT (tmpl); | |
8193 | ||
8251199e JM |
8194 | /* Unregister the specialization so that when we tsubst we will not |
8195 | just return DECL. We don't have to unregister DECL from TMPL | |
8196 | because if would only be registered there if it were a partial | |
8197 | instantiation of a specialization, which it isn't: it's a full | |
8198 | instantiation. */ | |
36a117a5 MM |
8199 | gen_tmpl = most_general_template (tmpl); |
8200 | unregistered = unregister_specialization (decl, gen_tmpl); | |
8201 | ||
8202 | /* If the DECL was not unregistered then something peculiar is | |
8203 | happening: we created a specialization but did not call | |
8204 | register_specialization for it. */ | |
8205 | my_friendly_assert (unregistered, 0); | |
8206 | ||
61a127b3 MM |
8207 | if (TREE_CODE (decl) == VAR_DECL) |
8208 | /* Make sure that we can see identifiers, and compute access | |
8209 | correctly, for the class members used in the declaration of | |
8210 | this static variable. */ | |
8211 | pushclass (DECL_CONTEXT (decl), 2); | |
8212 | ||
d8c4447d MM |
8213 | /* Do the substitution to get the new declaration. */ |
8214 | new_decl = tsubst (code_pattern, args, NULL_TREE); | |
830bfa74 MM |
8215 | |
8216 | if (TREE_CODE (decl) == VAR_DECL) | |
8217 | { | |
8218 | /* Set up DECL_INITIAL, since tsubst doesn't. */ | |
830bfa74 | 8219 | DECL_INITIAL (new_decl) = |
d8c4447d | 8220 | tsubst_expr (DECL_INITIAL (code_pattern), args, |
830bfa74 | 8221 | DECL_TI_TEMPLATE (decl)); |
61a127b3 | 8222 | /* Pop the class context we pushed above. */ |
830bfa74 MM |
8223 | popclass (1); |
8224 | } | |
8225 | ||
8226 | if (TREE_CODE (decl) == FUNCTION_DECL) | |
36a117a5 MM |
8227 | /* Convince duplicate_decls to use the DECL_ARGUMENTS from the |
8228 | new decl. */ | |
8229 | DECL_INITIAL (new_decl) = error_mark_node; | |
8230 | ||
8231 | /* The immediate parent of the new template is still whatever it was | |
8232 | before, even though tsubst sets DECL_TI_TEMPLATE up as the most | |
8233 | general template. We also reset the DECL_ASSEMBLER_NAME since | |
8234 | tsubst always calculates the name as if the function in question | |
8235 | were really a template instance, and sometimes, with friend | |
8236 | functions, this is not so. See tsubst_friend_function for | |
8237 | details. */ | |
8238 | DECL_TI_TEMPLATE (new_decl) = DECL_TI_TEMPLATE (decl); | |
8239 | DECL_ASSEMBLER_NAME (new_decl) = DECL_ASSEMBLER_NAME (decl); | |
8240 | DECL_RTL (new_decl) = DECL_RTL (decl); | |
8241 | ||
8242 | /* Call duplicate decls to merge the old and new declarations. */ | |
830bfa74 MM |
8243 | duplicate_decls (new_decl, decl); |
8244 | ||
8245 | if (TREE_CODE (decl) == FUNCTION_DECL) | |
8246 | DECL_INITIAL (new_decl) = NULL_TREE; | |
36a117a5 MM |
8247 | |
8248 | /* Now, re-register the specialization. */ | |
8249 | register_specialization (decl, gen_tmpl, args); | |
830bfa74 MM |
8250 | } |
8251 | ||
f84b4be9 JM |
8252 | /* Produce the definition of D, a _DECL generated from a template. */ |
8253 | ||
a28e3c7f | 8254 | tree |
5566b478 MS |
8255 | instantiate_decl (d) |
8256 | tree d; | |
a28e3c7f | 8257 | { |
36a117a5 MM |
8258 | tree tmpl = DECL_TI_TEMPLATE (d); |
8259 | tree args = DECL_TI_ARGS (d); | |
830bfa74 | 8260 | tree td; |
36a117a5 MM |
8261 | tree code_pattern; |
8262 | tree spec; | |
8263 | tree gen_tmpl; | |
5566b478 | 8264 | int nested = in_function_p (); |
5566b478 | 8265 | int pattern_defined; |
c27be9b9 JM |
8266 | int line = lineno; |
8267 | char *file = input_filename; | |
5566b478 | 8268 | |
36a117a5 MM |
8269 | /* This function should only be used to instantiate templates for |
8270 | functions and static member variables. */ | |
8271 | my_friendly_assert (TREE_CODE (d) == FUNCTION_DECL | |
8272 | || TREE_CODE (d) == VAR_DECL, 0); | |
8273 | ||
8274 | if ((TREE_CODE (d) == FUNCTION_DECL && DECL_INITIAL (d)) | |
8275 | || (TREE_CODE (d) == VAR_DECL && !DECL_IN_AGGR_P (d))) | |
03d0f4af MM |
8276 | /* D has already been instantiated. It might seem reasonable to |
8277 | check whether or not D is an explict instantiation, and, if so, | |
8278 | stop here. But when an explicit instantiation is deferred | |
8279 | until the end of the compilation, DECL_EXPLICIT_INSTANTIATION | |
8280 | is set, even though we still need to do the instantiation. */ | |
36a117a5 MM |
8281 | return d; |
8282 | ||
8283 | /* If we already have a specialization of this declaration, then | |
8284 | there's no reason to instantiate it. Note that | |
8285 | retrieve_specialization gives us both instantiations and | |
8286 | specializations, so we must explicitly check | |
8287 | DECL_TEMPLATE_SPECIALIZATION. */ | |
8288 | gen_tmpl = most_general_template (tmpl); | |
8289 | spec = retrieve_specialization (gen_tmpl, args); | |
8290 | if (spec != NULL_TREE && DECL_TEMPLATE_SPECIALIZATION (spec)) | |
8291 | return spec; | |
8292 | ||
8293 | /* This needs to happen before any tsubsting. */ | |
8294 | if (! push_tinst_level (d)) | |
8295 | return d; | |
8296 | ||
4d85e00e MM |
8297 | /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern |
8298 | for the instantiation. This is not always the most general | |
8299 | template. Consider, for example: | |
8300 | ||
8301 | template <class T> | |
8302 | struct S { template <class U> void f(); | |
8303 | template <> void f<int>(); }; | |
8304 | ||
8305 | and an instantiation of S<double>::f<int>. We want TD to be the | |
8306 | specialization S<T>::f<int>, not the more general S<T>::f<U>. */ | |
8307 | td = tmpl; | |
8308 | for (td = tmpl; | |
8309 | /* An instantiation cannot have a definition, so we need a | |
8310 | more general template. */ | |
8311 | DECL_TEMPLATE_INSTANTIATION (td) | |
8312 | /* We must also deal with friend templates. Given: | |
8313 | ||
8314 | template <class T> struct S { | |
8315 | template <class U> friend void f() {}; | |
8316 | }; | |
8317 | ||
8318 | S<int>::f<U> say, is not an instantiation of S<T>::f<U>, | |
8319 | so far as the language is concerned, but that's still | |
8320 | where we get the pattern for the instantiation from. On | |
8321 | ther hand, if the definition comes outside the class, say: | |
8322 | ||
8323 | template <class T> struct S { | |
8324 | template <class U> friend void f(); | |
8325 | }; | |
8326 | template <class U> friend void f() {} | |
8327 | ||
8328 | we don't need to look any further. That's what the check for | |
8329 | DECL_INITIAL is for. */ | |
8330 | || (TREE_CODE (d) == FUNCTION_DECL | |
61289ca3 | 8331 | && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (td) |
4d85e00e | 8332 | && !DECL_INITIAL (DECL_TEMPLATE_RESULT (td))); |
f84b4be9 | 8333 | ) |
4d85e00e MM |
8334 | { |
8335 | /* The present template, TD, should not be a definition. If it | |
8336 | were a definition, we should be using it! Note that we | |
8337 | cannot restructure the loop to just keep going until we find | |
8338 | a template with a definition, since that might go too far if | |
8339 | a specialization was declared, but not defined. */ | |
8340 | my_friendly_assert (!(TREE_CODE (d) == FUNCTION_DECL | |
8341 | && DECL_INITIAL (DECL_TEMPLATE_RESULT (td))), | |
8342 | 0); | |
8343 | my_friendly_assert (!(TREE_CODE (d) == VAR_DECL | |
8344 | && !DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (td))), | |
8345 | 0); | |
8346 | ||
8347 | /* Fetch the more general template. */ | |
8348 | td = DECL_TI_TEMPLATE (td); | |
8349 | } | |
27bb8339 | 8350 | |
fee23f54 | 8351 | code_pattern = DECL_TEMPLATE_RESULT (td); |
27bb8339 | 8352 | |
5566b478 | 8353 | if (TREE_CODE (d) == FUNCTION_DECL) |
36a117a5 | 8354 | pattern_defined = (DECL_INITIAL (code_pattern) != NULL_TREE); |
5566b478 | 8355 | else |
36a117a5 | 8356 | pattern_defined = ! DECL_IN_AGGR_P (code_pattern); |
de22184b MS |
8357 | |
8358 | push_to_top_level (); | |
8359 | lineno = DECL_SOURCE_LINE (d); | |
8360 | input_filename = DECL_SOURCE_FILE (d); | |
8361 | ||
de22184b | 8362 | if (pattern_defined) |
5566b478 MS |
8363 | { |
8364 | repo_template_used (d); | |
8365 | ||
8366 | if (flag_external_templates && ! DECL_INTERFACE_KNOWN (d)) | |
8367 | { | |
8368 | if (flag_alt_external_templates) | |
8369 | { | |
8370 | if (interface_unknown) | |
8371 | warn_if_unknown_interface (d); | |
8372 | } | |
fee23f54 | 8373 | else if (DECL_INTERFACE_KNOWN (code_pattern)) |
5566b478 MS |
8374 | { |
8375 | DECL_INTERFACE_KNOWN (d) = 1; | |
fee23f54 | 8376 | DECL_NOT_REALLY_EXTERN (d) = ! DECL_EXTERNAL (code_pattern); |
5566b478 MS |
8377 | } |
8378 | else | |
fee23f54 | 8379 | warn_if_unknown_interface (code_pattern); |
5566b478 MS |
8380 | } |
8381 | ||
e92cc029 | 8382 | if (at_eof) |
5566b478 MS |
8383 | import_export_decl (d); |
8384 | } | |
8385 | ||
c4ae3f91 JM |
8386 | /* Reject all external templates except inline functions. */ |
8387 | if (DECL_INTERFACE_KNOWN (d) | |
8388 | && ! DECL_NOT_REALLY_EXTERN (d) | |
8389 | && ! (TREE_CODE (d) == FUNCTION_DECL && DECL_INLINE (d))) | |
8390 | goto out; | |
8391 | ||
67ffc812 | 8392 | if (TREE_CODE (d) == VAR_DECL |
711734a9 | 8393 | && TREE_READONLY (d) |
67ffc812 MM |
8394 | && DECL_INITIAL (d) == NULL_TREE |
8395 | && DECL_INITIAL (code_pattern) != NULL_TREE) | |
8396 | /* We need to set up DECL_INITIAL regardless of pattern_defined if | |
8397 | the variable is a static const initialized in the class body. */; | |
8398 | else if (! pattern_defined | |
8399 | || (! (TREE_CODE (d) == FUNCTION_DECL && DECL_INLINE (d) && nested) | |
8400 | && ! at_eof)) | |
5566b478 | 8401 | { |
67ffc812 | 8402 | /* Defer all templates except inline functions used in another |
c27be9b9 JM |
8403 | function. We restore the source position here because it's used |
8404 | by add_pending_template. */ | |
8405 | lineno = line; | |
8406 | input_filename = file; | |
8407 | ||
03d0f4af MM |
8408 | if (at_eof && !pattern_defined |
8409 | && DECL_EXPLICIT_INSTANTIATION (d)) | |
8410 | /* [temp.explicit] | |
8411 | ||
8412 | The definition of a non-exported function template, a | |
8413 | non-exported member function template, or a non-exported | |
8414 | member function or static data member of a class template | |
8415 | shall be present in every translation unit in which it is | |
8416 | explicitly instantiated. */ | |
8251199e | 8417 | cp_error ("explicit instantiation of `%D' but no definition available", |
03d0f4af MM |
8418 | d); |
8419 | ||
5566b478 | 8420 | add_pending_template (d); |
de22184b | 8421 | goto out; |
5566b478 MS |
8422 | } |
8423 | ||
120722ac MM |
8424 | regenerate_decl_from_template (d, td); |
8425 | ||
8426 | /* We already set the file and line above. Reset them now in case | |
8427 | they changed as a result of calling regenerate_decl_from_template. */ | |
5156628f MS |
8428 | lineno = DECL_SOURCE_LINE (d); |
8429 | input_filename = DECL_SOURCE_FILE (d); | |
8430 | ||
5566b478 MS |
8431 | if (TREE_CODE (d) == VAR_DECL) |
8432 | { | |
8433 | DECL_IN_AGGR_P (d) = 0; | |
8434 | if (DECL_INTERFACE_KNOWN (d)) | |
8435 | DECL_EXTERNAL (d) = ! DECL_NOT_REALLY_EXTERN (d); | |
8436 | else | |
8437 | { | |
8438 | DECL_EXTERNAL (d) = 1; | |
8439 | DECL_NOT_REALLY_EXTERN (d) = 1; | |
8440 | } | |
8441 | cp_finish_decl (d, DECL_INITIAL (d), NULL_TREE, 0, 0); | |
8442 | } | |
8443 | else if (TREE_CODE (d) == FUNCTION_DECL) | |
8444 | { | |
fee23f54 | 8445 | tree t = DECL_SAVED_TREE (code_pattern); |
5566b478 | 8446 | |
c11b6f21 | 8447 | start_function (NULL_TREE, d, NULL_TREE, 1); |
5566b478 MS |
8448 | store_parm_decls (); |
8449 | ||
e76a2646 MS |
8450 | if (t && TREE_CODE (t) == RETURN_INIT) |
8451 | { | |
8452 | store_return_init | |
8453 | (TREE_OPERAND (t, 0), | |
f7d98d58 | 8454 | tsubst_expr (TREE_OPERAND (t, 1), args, tmpl)); |
e76a2646 MS |
8455 | t = TREE_CHAIN (t); |
8456 | } | |
8457 | ||
5566b478 MS |
8458 | if (t && TREE_CODE (t) == CTOR_INITIALIZER) |
8459 | { | |
8460 | current_member_init_list | |
8461 | = tsubst_expr_values (TREE_OPERAND (t, 0), args); | |
8462 | current_base_init_list | |
8463 | = tsubst_expr_values (TREE_OPERAND (t, 1), args); | |
8464 | t = TREE_CHAIN (t); | |
8465 | } | |
8466 | ||
8467 | setup_vtbl_ptr (); | |
8468 | /* Always keep the BLOCK node associated with the outermost | |
956d6950 | 8469 | pair of curly braces of a function. These are needed |
5566b478 MS |
8470 | for correct operation of dwarfout.c. */ |
8471 | keep_next_level (); | |
8472 | ||
8473 | my_friendly_assert (TREE_CODE (t) == COMPOUND_STMT, 42); | |
f7d98d58 | 8474 | tsubst_expr (t, args, tmpl); |
a28e3c7f | 8475 | |
5566b478 | 8476 | finish_function (lineno, 0, nested); |
5566b478 MS |
8477 | } |
8478 | ||
de22184b | 8479 | out: |
c27be9b9 JM |
8480 | lineno = line; |
8481 | input_filename = file; | |
8482 | ||
5566b478 | 8483 | pop_from_top_level (); |
5566b478 | 8484 | pop_tinst_level (); |
a28e3c7f | 8485 | |
a28e3c7f MS |
8486 | return d; |
8487 | } | |
5566b478 | 8488 | |
824b9a4c | 8489 | static tree |
5566b478 MS |
8490 | tsubst_expr_values (t, argvec) |
8491 | tree t, argvec; | |
8492 | { | |
8493 | tree first = NULL_TREE; | |
8494 | tree *p = &first; | |
8495 | ||
8496 | for (; t; t = TREE_CHAIN (t)) | |
8497 | { | |
f7d98d58 JM |
8498 | tree pur = tsubst_copy (TREE_PURPOSE (t), argvec, NULL_TREE); |
8499 | tree val = tsubst_expr (TREE_VALUE (t), argvec, NULL_TREE); | |
5566b478 MS |
8500 | *p = build_tree_list (pur, val); |
8501 | p = &TREE_CHAIN (*p); | |
8502 | } | |
8503 | return first; | |
8504 | } | |
8505 | ||
8506 | tree last_tree; | |
8507 | ||
8508 | void | |
8509 | add_tree (t) | |
8510 | tree t; | |
8511 | { | |
8512 | last_tree = TREE_CHAIN (last_tree) = t; | |
8513 | } | |
73aad9b9 | 8514 | |
75650646 MM |
8515 | |
8516 | void | |
8517 | begin_tree () | |
8518 | { | |
8519 | saved_trees = tree_cons (NULL_TREE, last_tree, saved_trees); | |
8520 | last_tree = NULL_TREE; | |
8521 | } | |
8522 | ||
8523 | ||
8524 | void | |
8525 | end_tree () | |
8526 | { | |
8527 | my_friendly_assert (saved_trees != NULL_TREE, 0); | |
8528 | ||
8529 | last_tree = TREE_VALUE (saved_trees); | |
8530 | saved_trees = TREE_CHAIN (saved_trees); | |
8531 | } | |
8532 | ||
73aad9b9 JM |
8533 | /* D is an undefined function declaration in the presence of templates with |
8534 | the same name, listed in FNS. If one of them can produce D as an | |
8535 | instantiation, remember this so we can instantiate it at EOF if D has | |
8536 | not been defined by that time. */ | |
8537 | ||
8538 | void | |
8539 | add_maybe_template (d, fns) | |
8540 | tree d, fns; | |
8541 | { | |
8542 | tree t; | |
8543 | ||
8544 | if (DECL_MAYBE_TEMPLATE (d)) | |
8545 | return; | |
8546 | ||
e1467ff2 | 8547 | t = most_specialized (fns, d, NULL_TREE); |
73aad9b9 JM |
8548 | if (! t) |
8549 | return; | |
8550 | if (t == error_mark_node) | |
8551 | { | |
8251199e | 8552 | cp_error ("ambiguous template instantiation for `%D'", d); |
73aad9b9 JM |
8553 | return; |
8554 | } | |
8555 | ||
8556 | *maybe_template_tail = perm_tree_cons (t, d, NULL_TREE); | |
8557 | maybe_template_tail = &TREE_CHAIN (*maybe_template_tail); | |
8558 | DECL_MAYBE_TEMPLATE (d) = 1; | |
8559 | } | |
b87692e5 | 8560 | |
61a127b3 MM |
8561 | /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */ |
8562 | ||
8563 | static void | |
8564 | set_current_access_from_decl (decl) | |
8565 | tree decl; | |
8566 | { | |
8567 | if (TREE_PRIVATE (decl)) | |
8568 | current_access_specifier = access_private_node; | |
8569 | else if (TREE_PROTECTED (decl)) | |
8570 | current_access_specifier = access_protected_node; | |
8571 | else | |
8572 | current_access_specifier = access_public_node; | |
8573 | } | |
8574 | ||
dbfe2124 MM |
8575 | /* Instantiate an enumerated type. TAG is the template type, NEWTAG |
8576 | is the instantiation (which should have been created with | |
8577 | start_enum) and ARGS are the template arguments to use. */ | |
b87692e5 | 8578 | |
dbfe2124 MM |
8579 | static void |
8580 | tsubst_enum (tag, newtag, args) | |
8581 | tree tag; | |
8582 | tree newtag; | |
8583 | tree args; | |
b87692e5 | 8584 | { |
dbfe2124 | 8585 | tree e; |
b87692e5 MS |
8586 | |
8587 | for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e)) | |
8588 | { | |
61a127b3 MM |
8589 | tree value; |
8590 | tree elt; | |
8591 | ||
8592 | /* Note that in a template enum, the TREE_VALUE is the | |
8593 | CONST_DECL, not the corresponding INTEGER_CST. */ | |
8594 | value = tsubst_expr (DECL_INITIAL (TREE_VALUE (e)), | |
72f2bd78 | 8595 | args, |
61a127b3 MM |
8596 | NULL_TREE); |
8597 | ||
8598 | /* Give this enumeration constant the correct access. */ | |
8599 | set_current_access_from_decl (TREE_VALUE (e)); | |
8600 | ||
8601 | /* Actually build the enumerator itself. */ | |
8602 | elt = build_enumerator (TREE_PURPOSE (e), value, newtag); | |
b87692e5 | 8603 | |
dbfe2124 MM |
8604 | /* We save the enumerators we have built so far in the |
8605 | TYPE_VALUES so that if the enumeration constants for | |
8606 | subsequent enumerators involve those for previous ones, | |
8607 | tsubst_copy will be able to find them. */ | |
8608 | TREE_CHAIN (elt) = TYPE_VALUES (newtag); | |
8609 | TYPE_VALUES (newtag) = elt; | |
8610 | } | |
b3d5a58b | 8611 | |
dbfe2124 | 8612 | finish_enum (newtag); |
b87692e5 | 8613 | } |
36a117a5 MM |
8614 | |
8615 | /* Set the DECL_ASSEMBLER_NAME for DECL, which is a FUNCTION_DECL that | |
8616 | is either an instantiation or specialization of a template | |
8617 | function. */ | |
8618 | ||
8619 | static void | |
8620 | set_mangled_name_for_template_decl (decl) | |
8621 | tree decl; | |
8622 | { | |
8623 | tree saved_namespace; | |
b370501f | 8624 | tree context = NULL_TREE; |
36a117a5 MM |
8625 | tree fn_type; |
8626 | tree ret_type; | |
8627 | tree parm_types; | |
8628 | tree tparms; | |
8629 | tree targs; | |
8630 | tree tmpl; | |
8631 | int parm_depth; | |
8632 | ||
8633 | my_friendly_assert (TREE_CODE (decl) == FUNCTION_DECL, 0); | |
8634 | my_friendly_assert (DECL_TEMPLATE_INFO (decl) != NULL_TREE, 0); | |
8635 | ||
8636 | /* The names of template functions must be mangled so as to indicate | |
8637 | what template is being specialized with what template arguments. | |
8638 | For example, each of the following three functions must get | |
8639 | different mangled names: | |
8640 | ||
8641 | void f(int); | |
8642 | template <> void f<7>(int); | |
8643 | template <> void f<8>(int); */ | |
8644 | ||
8645 | targs = DECL_TI_ARGS (decl); | |
8646 | if (uses_template_parms (targs)) | |
8647 | /* This DECL is for a partial instantiation. There's no need to | |
8648 | mangle the name of such an entity. */ | |
8649 | return; | |
8650 | ||
8651 | tmpl = most_general_template (DECL_TI_TEMPLATE (decl)); | |
8652 | tparms = DECL_TEMPLATE_PARMS (tmpl); | |
8653 | parm_depth = TMPL_PARMS_DEPTH (tparms); | |
8654 | ||
8655 | /* There should be as many levels of arguments as there are levels | |
8656 | of parameters. */ | |
8657 | my_friendly_assert (parm_depth == TMPL_ARGS_DEPTH (targs), 0); | |
8658 | ||
8659 | /* We now compute the PARMS and RET_TYPE to give to | |
8660 | build_decl_overload_real. The PARMS and RET_TYPE are the | |
8661 | parameter and return types of the template, after all but the | |
8662 | innermost template arguments have been substituted, not the | |
8663 | parameter and return types of the function DECL. For example, | |
8664 | given: | |
8665 | ||
8666 | template <class T> T f(T); | |
8667 | ||
8668 | both PARMS and RET_TYPE should be `T' even if DECL is `int f(int)'. | |
8669 | A more subtle example is: | |
8670 | ||
8671 | template <class T> struct S { template <class U> void f(T, U); } | |
8672 | ||
8673 | Here, if DECL is `void S<int>::f(int, double)', PARMS should be | |
8674 | {int, U}. Thus, the args that we want to subsitute into the | |
8675 | return and parameter type for the function are those in TARGS, | |
8676 | with the innermost level omitted. */ | |
8677 | fn_type = TREE_TYPE (tmpl); | |
8678 | if (DECL_STATIC_FUNCTION_P (decl)) | |
61a127b3 | 8679 | context = DECL_CLASS_CONTEXT (decl); |
36a117a5 MM |
8680 | |
8681 | if (parm_depth == 1) | |
8682 | /* No substitution is necessary. */ | |
8683 | ; | |
8684 | else | |
8685 | { | |
8686 | int i; | |
8687 | tree partial_args; | |
8688 | ||
8689 | /* Replace the innermost level of the TARGS with NULL_TREEs to | |
8690 | let tsubst know not to subsitute for those parameters. */ | |
8691 | partial_args = make_temp_vec (TREE_VEC_LENGTH (targs)); | |
8692 | for (i = 1; i < TMPL_ARGS_DEPTH (targs); ++i) | |
8693 | SET_TMPL_ARGS_LEVEL (partial_args, i, | |
8694 | TMPL_ARGS_LEVEL (targs, i)); | |
8695 | SET_TMPL_ARGS_LEVEL (partial_args, | |
8696 | TMPL_ARGS_DEPTH (targs), | |
8697 | make_temp_vec (DECL_NTPARMS (tmpl))); | |
8698 | ||
8699 | /* Now, do the (partial) substitution to figure out the | |
8700 | appropriate function type. */ | |
8701 | fn_type = tsubst (fn_type, partial_args, NULL_TREE); | |
8702 | if (DECL_STATIC_FUNCTION_P (decl)) | |
8703 | context = tsubst (context, partial_args, NULL_TREE); | |
8704 | ||
8705 | /* Substitute into the template parameters to obtain the real | |
8706 | innermost set of parameters. This step is important if the | |
8707 | innermost set of template parameters contains value | |
8708 | parameters whose types depend on outer template parameters. */ | |
8709 | TREE_VEC_LENGTH (partial_args)--; | |
8710 | tparms = tsubst_template_parms (tparms, partial_args); | |
8711 | } | |
8712 | ||
8713 | /* Now, get the innermost parameters and arguments, and figure out | |
8714 | the parameter and return types. */ | |
8715 | tparms = INNERMOST_TEMPLATE_PARMS (tparms); | |
8716 | targs = innermost_args (targs); | |
8717 | ret_type = TREE_TYPE (fn_type); | |
8718 | parm_types = TYPE_ARG_TYPES (fn_type); | |
8719 | ||
8720 | /* For a static member function, we generate a fake `this' pointer, | |
8721 | for the purposes of mangling. This indicates of which class the | |
8722 | function is a member. Because of: | |
8723 | ||
8724 | [class.static] | |
8725 | ||
8726 | There shall not be a static and a nonstatic member function | |
8727 | with the same name and the same parameter types | |
8728 | ||
8729 | we don't have to worry that this will result in a clash with a | |
8730 | non-static member function. */ | |
8731 | if (DECL_STATIC_FUNCTION_P (decl)) | |
8732 | parm_types = hash_tree_chain (build_pointer_type (context), parm_types); | |
8733 | ||
8734 | /* There should be the same number of template parameters as | |
8735 | template arguments. */ | |
8736 | my_friendly_assert (TREE_VEC_LENGTH (tparms) == TREE_VEC_LENGTH (targs), | |
8737 | 0); | |
8738 | ||
8739 | /* If the template is in a namespace, we need to put that into the | |
8740 | mangled name. Unfortunately, build_decl_overload_real does not | |
8741 | get the decl to mangle, so it relies on the current | |
8742 | namespace. Therefore, we set that here temporarily. */ | |
8743 | my_friendly_assert (TREE_CODE_CLASS (TREE_CODE (decl)) == 'd', 980702); | |
8744 | saved_namespace = current_namespace; | |
8745 | current_namespace = CP_DECL_CONTEXT (decl); | |
8746 | ||
8747 | /* Actually set the DCL_ASSEMBLER_NAME. */ | |
8748 | DECL_ASSEMBLER_NAME (decl) | |
8749 | = build_decl_overload_real (DECL_NAME (decl), parm_types, ret_type, | |
8750 | tparms, targs, | |
8751 | DECL_FUNCTION_MEMBER_P (decl) | |
8752 | + DECL_CONSTRUCTOR_P (decl)); | |
8753 | ||
8754 | /* Restore the previously active namespace. */ | |
8755 | current_namespace = saved_namespace; | |
8756 | } |