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