]>
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; |
386b8a85 JM |
66 | static int template_header_count; |
67 | ||
75650646 MM |
68 | static tree saved_trees; |
69 | ||
8d08fdba MS |
70 | #define obstack_chunk_alloc xmalloc |
71 | #define obstack_chunk_free free | |
72 | ||
050367a3 | 73 | static int unify PROTO((tree, tree, int, tree, tree, int, int*)); |
49c249e1 JM |
74 | static void add_pending_template PROTO((tree)); |
75 | static int push_tinst_level PROTO((tree)); | |
76 | static tree classtype_mangled_name PROTO((tree)); | |
75650646 | 77 | static char *mangle_class_name_for_template PROTO((char *, tree, tree, tree)); |
49c249e1 | 78 | static tree tsubst_expr_values PROTO((tree, tree)); |
49c249e1 | 79 | static int list_eq PROTO((tree, tree)); |
8d019cef | 80 | static tree get_class_bindings PROTO((tree, tree, tree, tree)); |
de575009 | 81 | static tree coerce_template_parms PROTO((tree, tree, tree, int, int, int)); |
f7d98d58 | 82 | static tree tsubst_enum PROTO((tree, tree, tree *)); |
98c1c668 | 83 | static tree add_to_template_args PROTO((tree, tree)); |
050367a3 MM |
84 | static int type_unification_real PROTO((tree, tree, tree, tree, |
85 | int, int, int, int*)); | |
e6f1275f | 86 | static tree complete_template_args PROTO((tree, tree, int)); |
386b8a85 | 87 | static void note_template_header PROTO((int)); |
840b09b4 | 88 | static tree maybe_fold_nontype_arg PROTO((tree)); |
e1467ff2 | 89 | static tree convert_nontype_argument PROTO((tree, tree)); |
76b9a14d | 90 | static tree get_bindings_overload PROTO((tree, tree, tree)); |
050367a3 | 91 | static int for_each_template_parm PROTO((tree, tree_fn_t, void*)); |
4b580446 | 92 | static tree build_template_parm_index PROTO((int, int, int, tree, tree)); |
69ac77ce JL |
93 | static tree original_template PROTO((tree)); |
94 | static int inline_needs_template_parms PROTO((tree)); | |
95 | static void push_inline_template_parms_recursive PROTO((tree, int)); | |
96 | static tree retrieve_specialization PROTO((tree, tree)); | |
97 | static void register_specialization PROTO((tree, tree, tree)); | |
98 | static void print_candidates PROTO((tree)); | |
99 | static tree reduce_template_parm_level PROTO((tree, tree, int)); | |
100 | static tree build_template_decl PROTO((tree, tree)); | |
101 | static int mark_template_parm PROTO((tree, void *)); | |
102 | static tree tsubst_friend_function PROTO((tree, tree)); | |
6757edfe | 103 | static tree tsubst_friend_class PROTO((tree, tree)); |
69ac77ce | 104 | static tree get_bindings_real PROTO((tree, tree, tree, int)); |
04a81d94 | 105 | static int template_decl_level PROTO((tree)); |
a2b60a0e | 106 | static tree maybe_get_template_decl_from_type_decl PROTO((tree)); |
e1467ff2 MM |
107 | |
108 | /* Do any processing required when DECL (a member template declaration | |
109 | using TEMPLATE_PARAMETERS as its innermost parameter list) is | |
110 | finished. Returns the TEMPLATE_DECL corresponding to DECL, unless | |
111 | it is a specialization, in which case the DECL itself is returned. */ | |
112 | ||
113 | tree | |
114 | finish_member_template_decl (template_parameters, decl) | |
115 | tree template_parameters; | |
116 | tree decl; | |
117 | { | |
118 | if (template_parameters) | |
93cdc044 | 119 | end_template_decl (); |
e1467ff2 | 120 | else |
93cdc044 | 121 | end_specialization (); |
e1467ff2 | 122 | |
93cdc044 JM |
123 | if (decl == NULL_TREE || decl == void_type_node) |
124 | return NULL_TREE; | |
125 | else if (TREE_CODE (decl) == TREE_LIST) | |
126 | { | |
5f311aec | 127 | /* Assume that the class is the only declspec. */ |
93cdc044 | 128 | decl = TREE_VALUE (decl); |
8d019cef JM |
129 | if (IS_AGGR_TYPE (decl) && CLASSTYPE_TEMPLATE_INFO (decl) |
130 | && ! CLASSTYPE_TEMPLATE_SPECIALIZATION (decl)) | |
93cdc044 JM |
131 | { |
132 | tree tmpl = CLASSTYPE_TI_TEMPLATE (decl); | |
133 | check_member_template (tmpl); | |
134 | return tmpl; | |
135 | } | |
8d019cef | 136 | return NULL_TREE; |
93cdc044 | 137 | } |
a1da6cba | 138 | else if (DECL_TEMPLATE_INFO (decl)) |
e1467ff2 | 139 | { |
a1da6cba MM |
140 | if (!DECL_TEMPLATE_SPECIALIZATION (decl)) |
141 | { | |
142 | check_member_template (DECL_TI_TEMPLATE (decl)); | |
143 | return DECL_TI_TEMPLATE (decl); | |
144 | } | |
145 | else | |
146 | return decl; | |
147 | } | |
148 | else | |
149 | cp_error ("invalid member template declaration `%D'", decl); | |
150 | ||
e1467ff2 | 151 | |
a1da6cba | 152 | return error_mark_node; |
f84b4be9 | 153 | } |
e1467ff2 | 154 | |
f84b4be9 JM |
155 | /* Returns the template nesting level of the indicated class TYPE. |
156 | ||
157 | For example, in: | |
158 | template <class T> | |
159 | struct A | |
160 | { | |
161 | template <class U> | |
162 | struct B {}; | |
163 | }; | |
164 | ||
165 | A<T>::B<U> has depth two, while A<T> has depth one. Also, | |
166 | both A<T>::B<int> and A<int>::B<U> have depth one. */ | |
167 | ||
168 | int | |
169 | template_class_depth (type) | |
170 | tree type; | |
171 | { | |
93cdc044 | 172 | int depth; |
f84b4be9 | 173 | |
93cdc044 JM |
174 | for (depth = 0; type && TREE_CODE (type) != FUNCTION_DECL; |
175 | type = TYPE_CONTEXT (type)) | |
176 | if (CLASSTYPE_TEMPLATE_INFO (type) | |
177 | && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)) | |
178 | && uses_template_parms (CLASSTYPE_TI_ARGS (type))) | |
179 | ++depth; | |
f84b4be9 JM |
180 | |
181 | return depth; | |
e1467ff2 | 182 | } |
98c1c668 | 183 | |
cae40af6 JM |
184 | /* Return the original template for this decl, disregarding any |
185 | specializations. */ | |
98c1c668 | 186 | |
cae40af6 JM |
187 | static tree |
188 | original_template (decl) | |
786b5245 | 189 | tree decl; |
98c1c668 | 190 | { |
cae40af6 JM |
191 | while (DECL_TEMPLATE_INFO (decl)) |
192 | decl = DECL_TI_TEMPLATE (decl); | |
193 | return decl; | |
194 | } | |
98c1c668 | 195 | |
cae40af6 JM |
196 | /* Returns 1 if processing DECL as part of do_pending_inlines |
197 | needs us to push template parms. */ | |
198 | ||
199 | static int | |
200 | inline_needs_template_parms (decl) | |
201 | tree decl; | |
202 | { | |
203 | if (! DECL_TEMPLATE_INFO (decl)) | |
204 | return 0; | |
f84b4be9 | 205 | |
cae40af6 JM |
206 | return (list_length (DECL_TEMPLATE_PARMS (original_template (decl))) |
207 | > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl))); | |
208 | } | |
209 | ||
210 | /* Subroutine of maybe_begin_member_template_processing. | |
211 | Push the template parms in PARMS, starting from LEVELS steps into the | |
212 | chain, and ending at the beginning, since template parms are listed | |
213 | innermost first. */ | |
214 | ||
215 | static void | |
216 | push_inline_template_parms_recursive (parmlist, levels) | |
217 | tree parmlist; | |
218 | int levels; | |
219 | { | |
220 | tree parms = TREE_VALUE (parmlist); | |
221 | int i; | |
222 | ||
223 | if (levels > 1) | |
224 | push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1); | |
786b5245 | 225 | |
98c1c668 | 226 | ++processing_template_decl; |
cae40af6 | 227 | current_template_parms |
98c1c668 JM |
228 | = tree_cons (build_int_2 (0, processing_template_decl), |
229 | parms, current_template_parms); | |
cae40af6 JM |
230 | TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1; |
231 | ||
786b5245 | 232 | pushlevel (0); |
98c1c668 JM |
233 | for (i = 0; i < TREE_VEC_LENGTH (parms); ++i) |
234 | { | |
786b5245 MM |
235 | tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i)); |
236 | my_friendly_assert (TREE_CODE_CLASS (TREE_CODE (parm)) == 'd', 0); | |
cae40af6 | 237 | |
98c1c668 JM |
238 | switch (TREE_CODE (parm)) |
239 | { | |
786b5245 | 240 | case TYPE_DECL: |
73b0fce8 | 241 | case TEMPLATE_DECL: |
98c1c668 JM |
242 | pushdecl (parm); |
243 | break; | |
786b5245 MM |
244 | |
245 | case PARM_DECL: | |
246 | { | |
247 | /* Make a CONST_DECL as is done in process_template_parm. */ | |
248 | tree decl = build_decl (CONST_DECL, DECL_NAME (parm), | |
249 | TREE_TYPE (parm)); | |
250 | DECL_INITIAL (decl) = DECL_INITIAL (parm); | |
251 | pushdecl (decl); | |
252 | } | |
cae40af6 | 253 | break; |
786b5245 | 254 | |
98c1c668 JM |
255 | default: |
256 | my_friendly_abort (0); | |
257 | } | |
258 | } | |
259 | } | |
260 | ||
cae40af6 JM |
261 | /* Restore the template parameter context for a member template or |
262 | a friend template defined in a class definition. */ | |
263 | ||
264 | void | |
265 | maybe_begin_member_template_processing (decl) | |
266 | tree decl; | |
267 | { | |
268 | tree parms; | |
269 | int levels; | |
270 | ||
271 | if (! inline_needs_template_parms (decl)) | |
272 | return; | |
273 | ||
274 | parms = DECL_TEMPLATE_PARMS (original_template (decl)); | |
275 | ||
276 | levels = list_length (parms) - processing_template_decl; | |
277 | ||
278 | if (DECL_TEMPLATE_SPECIALIZATION (decl)) | |
279 | { | |
280 | --levels; | |
281 | parms = TREE_CHAIN (parms); | |
282 | } | |
283 | ||
284 | push_inline_template_parms_recursive (parms, levels); | |
285 | } | |
286 | ||
98c1c668 JM |
287 | /* Undo the effects of begin_member_template_processing. */ |
288 | ||
289 | void | |
f84b4be9 JM |
290 | maybe_end_member_template_processing (decl) |
291 | tree decl; | |
98c1c668 JM |
292 | { |
293 | if (! processing_template_decl) | |
294 | return; | |
295 | ||
cae40af6 JM |
296 | while (current_template_parms |
297 | && TEMPLATE_PARMS_FOR_INLINE (current_template_parms)) | |
298 | { | |
299 | --processing_template_decl; | |
300 | current_template_parms = TREE_CHAIN (current_template_parms); | |
301 | poplevel (0, 0, 0); | |
302 | } | |
98c1c668 JM |
303 | } |
304 | ||
cae40af6 | 305 | /* Returns non-zero iff T is a member template function. We must be |
f84b4be9 | 306 | careful as in |
75650646 MM |
307 | |
308 | template <class T> class C { void f(); } | |
309 | ||
310 | Here, f is a template function, and a member, but not a member | |
311 | template. This function does not concern itself with the origin of | |
312 | T, only its present state. So if we have | |
313 | ||
314 | template <class T> class C { template <class U> void f(U); } | |
315 | ||
316 | then neither C<int>::f<char> nor C<T>::f<double> is considered | |
317 | to be a member template. */ | |
98c1c668 | 318 | |
cae40af6 JM |
319 | int |
320 | is_member_template (t) | |
98c1c668 JM |
321 | tree t; |
322 | { | |
aa5f3bad MM |
323 | if (TREE_CODE (t) != FUNCTION_DECL |
324 | && !DECL_FUNCTION_TEMPLATE_P (t)) | |
75650646 | 325 | /* Anything that isn't a function or a template function is |
aa5f3bad MM |
326 | certainly not a member template. */ |
327 | return 0; | |
328 | ||
cae40af6 JM |
329 | /* A local class can't have member templates. */ |
330 | if (hack_decl_function_context (t)) | |
331 | return 0; | |
332 | ||
333 | if ((DECL_FUNCTION_MEMBER_P (t) | |
386b8a85 | 334 | && !DECL_TEMPLATE_SPECIALIZATION (t)) |
f84b4be9 | 335 | || (TREE_CODE (t) == TEMPLATE_DECL |
cae40af6 | 336 | && DECL_FUNCTION_MEMBER_P (DECL_TEMPLATE_RESULT (t)))) |
98c1c668 | 337 | { |
f84b4be9 | 338 | tree tmpl; |
98c1c668 JM |
339 | |
340 | if (DECL_FUNCTION_TEMPLATE_P (t)) | |
341 | tmpl = t; | |
342 | else if (DECL_TEMPLATE_INFO (t) | |
343 | && DECL_FUNCTION_TEMPLATE_P (DECL_TI_TEMPLATE (t))) | |
344 | tmpl = DECL_TI_TEMPLATE (t); | |
f84b4be9 JM |
345 | else |
346 | tmpl = NULL_TREE; | |
347 | ||
cae40af6 | 348 | if (tmpl |
f84b4be9 JM |
349 | /* If there are more levels of template parameters than |
350 | there are template classes surrounding the declaration, | |
351 | then we have a member template. */ | |
cae40af6 JM |
352 | && (list_length (DECL_TEMPLATE_PARMS (tmpl)) > |
353 | template_class_depth (DECL_CLASS_CONTEXT (t)))) | |
f84b4be9 JM |
354 | return 1; |
355 | } | |
98c1c668 | 356 | |
f84b4be9 JM |
357 | return 0; |
358 | } | |
98c1c668 | 359 | |
e6f1275f JM |
360 | /* Return a new template argument vector which contains all of ARGS |
361 | for all outer templates TYPE is contained in, but has as its | |
362 | innermost set of arguments the EXTRA_ARGS. If UNBOUND_ONLY, we | |
363 | are only interested in unbound template arguments, not arguments from | |
364 | enclosing templates that have been instantiated already. */ | |
365 | ||
366 | static tree | |
367 | complete_template_args (tmpl, extra_args, unbound_only) | |
368 | tree tmpl, extra_args; | |
369 | int unbound_only; | |
370 | { | |
371 | /* depth is the number of levels of enclosing args we're adding. */ | |
372 | int depth, i; | |
373 | tree args, new_args, spec_args = NULL_TREE; | |
374 | ||
375 | my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 0); | |
376 | my_friendly_assert (TREE_CODE (extra_args) == TREE_VEC, 0); | |
377 | ||
378 | if (DECL_TEMPLATE_INFO (tmpl) && !unbound_only) | |
379 | { | |
380 | /* A specialization of a member template of a template class shows up | |
381 | as a TEMPLATE_DECL with DECL_TEMPLATE_SPECIALIZATION set. | |
382 | DECL_TI_ARGS is the specialization args, and DECL_TI_TEMPLATE | |
383 | is the template being specialized. */ | |
384 | if (DECL_TEMPLATE_SPECIALIZATION (tmpl)) | |
385 | { | |
386 | spec_args = DECL_TI_ARGS (tmpl); | |
387 | tmpl = DECL_TI_TEMPLATE (tmpl); | |
388 | } | |
389 | ||
390 | if (DECL_TEMPLATE_INFO (tmpl)) | |
391 | { | |
392 | /* A partial instantiation of a member template shows up as a | |
393 | TEMPLATE_DECL with DECL_TEMPLATE_INFO. DECL_TI_ARGS is | |
394 | all the bound template arguments. */ | |
395 | args = DECL_TI_ARGS (tmpl); | |
396 | if (TREE_CODE (TREE_VEC_ELT (args, 0)) != TREE_VEC) | |
397 | depth = 1; | |
398 | else | |
399 | depth = TREE_VEC_LENGTH (args); | |
400 | } | |
401 | else | |
402 | /* If we are a specialization, we might have no previously bound | |
403 | template args. */ | |
404 | depth = 0; | |
405 | ||
406 | new_args = make_tree_vec (depth + 1 + (!!spec_args)); | |
407 | ||
408 | if (depth == 1) | |
409 | TREE_VEC_ELT (new_args, 0) = args; | |
410 | else | |
411 | for (i = 0; i < depth; ++i) | |
412 | TREE_VEC_ELT (new_args, i) = TREE_VEC_ELT (args, i); | |
413 | } | |
414 | else | |
415 | { | |
416 | tree type; | |
417 | int skip; | |
418 | ||
419 | /* For unbound args, we have to do more work. We are getting bindings | |
420 | for the innermost args from extra_args, so we start from our | |
421 | context and work out until we've seen all the args. We need to | |
422 | do it this way to handle partial specialization. */ | |
423 | ||
424 | depth = list_length (DECL_TEMPLATE_PARMS (tmpl)) - 1; | |
425 | if (depth == 0) | |
426 | return extra_args; | |
427 | ||
428 | new_args = make_tree_vec (depth + 1); | |
429 | ||
9c65eeca JM |
430 | /* If this isn't a member template, extra_args is for the innermost |
431 | template class, so skip over it. */ | |
432 | skip = (! is_member_template (tmpl)); | |
e6f1275f JM |
433 | |
434 | type = DECL_REAL_CONTEXT (tmpl); | |
435 | for (i = depth; i; type = TYPE_CONTEXT (type)) | |
436 | if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type))) | |
437 | { | |
438 | if (skip) | |
439 | skip = 0; | |
440 | else | |
441 | { | |
442 | --i; | |
443 | TREE_VEC_ELT (new_args, i) = CLASSTYPE_TI_ARGS (type); | |
444 | } | |
445 | } | |
446 | } | |
447 | ||
448 | TREE_VEC_ELT (new_args, depth) = extra_args; | |
449 | ||
450 | if (spec_args) | |
451 | TREE_VEC_ELT (new_args, depth + 1) = spec_args; | |
452 | ||
453 | return new_args; | |
454 | } | |
455 | ||
98c1c668 JM |
456 | /* Return a new template argument vector which contains all of ARGS, |
457 | but has as its innermost set of arguments the EXTRA_ARGS. */ | |
458 | ||
4966381a | 459 | static tree |
98c1c668 JM |
460 | add_to_template_args (args, extra_args) |
461 | tree args; | |
462 | tree extra_args; | |
463 | { | |
464 | tree new_args; | |
465 | ||
466 | if (TREE_CODE (TREE_VEC_ELT (args, 0)) != TREE_VEC) | |
467 | { | |
468 | new_args = make_tree_vec (2); | |
469 | TREE_VEC_ELT (new_args, 0) = args; | |
470 | } | |
471 | else | |
472 | { | |
473 | int i; | |
474 | ||
db897306 | 475 | new_args = make_tree_vec (TREE_VEC_LENGTH (args) + 1); |
98c1c668 JM |
476 | |
477 | for (i = 0; i < TREE_VEC_LENGTH (args); ++i) | |
478 | TREE_VEC_ELT (new_args, i) = TREE_VEC_ELT (args, i); | |
479 | } | |
480 | ||
481 | TREE_VEC_ELT (new_args, | |
482 | TREE_VEC_LENGTH (new_args) - 1) = extra_args; | |
483 | ||
484 | return new_args; | |
485 | } | |
5566b478 MS |
486 | |
487 | /* We've got a template header coming up; push to a new level for storing | |
488 | the parms. */ | |
8d08fdba | 489 | |
8d08fdba MS |
490 | void |
491 | begin_template_parm_list () | |
492 | { | |
6757edfe MM |
493 | /* We use a non-tag-transparent scope here, which causes pushtag to |
494 | put tags in this scope, rather than in the enclosing class or | |
495 | namespace scope. This is the right thing, since we want | |
496 | TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a | |
497 | global template class, push_template_decl handles putting the | |
498 | TEMPLATE_DECL into top-level scope. For a nested template class, | |
499 | e.g.: | |
500 | ||
501 | template <class T> struct S1 { | |
502 | template <class T> struct S2 {}; | |
503 | }; | |
504 | ||
505 | pushtag contains special code to call pushdecl_with_scope on the | |
506 | TEMPLATE_DECL for S2. */ | |
8d08fdba | 507 | pushlevel (0); |
5566b478 | 508 | declare_pseudo_global_level (); |
5156628f | 509 | ++processing_template_decl; |
386b8a85 JM |
510 | note_template_header (0); |
511 | } | |
512 | ||
386b8a85 JM |
513 | /* We've just seen template <>. */ |
514 | ||
515 | void | |
516 | begin_specialization () | |
517 | { | |
518 | note_template_header (1); | |
519 | } | |
520 | ||
386b8a85 JM |
521 | /* Called at then end of processing a declaration preceeded by |
522 | template<>. */ | |
523 | ||
524 | void | |
525 | end_specialization () | |
526 | { | |
527 | reset_specialization (); | |
528 | } | |
529 | ||
386b8a85 JM |
530 | /* Any template <>'s that we have seen thus far are not referring to a |
531 | function specialization. */ | |
532 | ||
533 | void | |
534 | reset_specialization () | |
535 | { | |
536 | processing_specialization = 0; | |
537 | template_header_count = 0; | |
538 | } | |
539 | ||
386b8a85 JM |
540 | /* We've just seen a template header. If SPECIALIZATION is non-zero, |
541 | it was of the form template <>. */ | |
542 | ||
4966381a | 543 | static void |
386b8a85 JM |
544 | note_template_header (specialization) |
545 | int specialization; | |
546 | { | |
547 | processing_specialization = specialization; | |
548 | template_header_count++; | |
549 | } | |
550 | ||
75650646 | 551 | /* We're beginning an explicit instantiation. */ |
386b8a85 | 552 | |
75650646 MM |
553 | void |
554 | begin_explicit_instantiation () | |
386b8a85 | 555 | { |
75650646 MM |
556 | ++processing_explicit_instantiation; |
557 | } | |
386b8a85 | 558 | |
386b8a85 | 559 | |
75650646 MM |
560 | void |
561 | end_explicit_instantiation () | |
562 | { | |
563 | my_friendly_assert(processing_explicit_instantiation > 0, 0); | |
564 | --processing_explicit_instantiation; | |
565 | } | |
386b8a85 | 566 | |
75650646 MM |
567 | /* Retrieve the specialization (in the sense of [temp.spec] - a |
568 | specialization is either an instantiation or an explicit | |
569 | specialization) of TMPL for the given template ARGS. If there is | |
570 | no such specialization, return NULL_TREE. The ARGS are a vector of | |
571 | arguments, or a vector of vectors of arguments, in the case of | |
572 | templates with more than one level of parameters. */ | |
573 | ||
574 | static tree | |
575 | retrieve_specialization (tmpl, args) | |
576 | tree tmpl; | |
577 | tree args; | |
578 | { | |
579 | tree s; | |
580 | ||
581 | my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 0); | |
582 | ||
583 | for (s = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); | |
584 | s != NULL_TREE; | |
585 | s = TREE_CHAIN (s)) | |
586 | if (comp_template_args (TREE_PURPOSE (s), args)) | |
587 | return TREE_VALUE (s); | |
588 | ||
589 | return NULL_TREE; | |
386b8a85 JM |
590 | } |
591 | ||
6757edfe MM |
592 | /* Returns non-zero iff DECL is a specialization of TMPL. */ |
593 | ||
594 | int | |
595 | is_specialization_of (decl, tmpl) | |
596 | tree decl; | |
597 | tree tmpl; | |
598 | { | |
599 | tree t; | |
600 | ||
601 | if (TREE_CODE (decl) == FUNCTION_DECL) | |
602 | { | |
603 | for (t = decl; | |
604 | t != NULL_TREE; | |
605 | t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE) | |
606 | if (t == tmpl) | |
607 | return 1; | |
608 | } | |
609 | else | |
610 | { | |
611 | my_friendly_assert (TREE_CODE (decl) == TYPE_DECL, 0); | |
612 | ||
613 | for (t = TREE_TYPE (decl); | |
614 | t != NULL_TREE; | |
615 | t = CLASSTYPE_USE_TEMPLATE (t) | |
616 | ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE) | |
617 | if (comptypes (TYPE_MAIN_VARIANT (t), | |
618 | TYPE_MAIN_VARIANT (TREE_TYPE (tmpl)), 1)) | |
619 | return 1; | |
620 | } | |
386b8a85 | 621 | |
6757edfe MM |
622 | return 0; |
623 | } | |
75650646 MM |
624 | |
625 | /* Register the specialization SPEC as a specialization of TMPL with | |
626 | the indicated ARGS. */ | |
627 | ||
628 | static void | |
629 | register_specialization (spec, tmpl, args) | |
630 | tree spec; | |
631 | tree tmpl; | |
632 | tree args; | |
633 | { | |
634 | tree s; | |
635 | ||
636 | my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 0); | |
637 | ||
638 | if (TREE_CODE (spec) != TEMPLATE_DECL | |
639 | && list_length (DECL_TEMPLATE_PARMS (tmpl)) > 1) | |
640 | /* Avoid registering function declarations as | |
641 | specializations of member templates, as would otherwise | |
642 | happen with out-of-class specializations of member | |
643 | templates. */ | |
644 | return; | |
645 | ||
646 | for (s = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); | |
647 | s != NULL_TREE; | |
648 | s = TREE_CHAIN (s)) | |
649 | if (comp_template_args (TREE_PURPOSE (s), args)) | |
650 | { | |
651 | tree fn = TREE_VALUE (s); | |
652 | ||
653 | if (DECL_TEMPLATE_SPECIALIZATION (spec)) | |
654 | { | |
655 | if (DECL_TEMPLATE_INSTANTIATION (fn)) | |
656 | { | |
657 | if (TREE_USED (fn) | |
658 | || DECL_EXPLICIT_INSTANTIATION (fn)) | |
659 | { | |
660 | cp_error ("specialization of %D after instantiation", | |
661 | fn); | |
662 | return; | |
663 | } | |
664 | else | |
665 | { | |
666 | /* This situation should occur only if the first | |
667 | specialization is an implicit instantiation, | |
668 | the second is an explicit specialization, and | |
669 | the implicit instantiation has not yet been | |
670 | used. That situation can occur if we have | |
671 | implicitly instantiated a member function of | |
672 | class type, and then specialized it later. */ | |
75650646 MM |
673 | TREE_VALUE (s) = spec; |
674 | return; | |
675 | } | |
676 | } | |
677 | else if (DECL_TEMPLATE_SPECIALIZATION (fn)) | |
678 | { | |
679 | if (DECL_INITIAL (fn)) | |
680 | cp_error ("duplicate specialization of %D", fn); | |
681 | ||
75650646 MM |
682 | TREE_VALUE (s) = spec; |
683 | return; | |
684 | } | |
685 | } | |
686 | } | |
687 | ||
688 | DECL_TEMPLATE_SPECIALIZATIONS (tmpl) | |
689 | = perm_tree_cons (args, spec, DECL_TEMPLATE_SPECIALIZATIONS (tmpl)); | |
690 | } | |
691 | ||
e1467ff2 MM |
692 | /* Print the list of candidate FNS in an error message. */ |
693 | ||
694 | static void | |
695 | print_candidates (fns) | |
696 | tree fns; | |
697 | { | |
698 | tree fn; | |
699 | ||
700 | char* str = "candidates are:"; | |
701 | ||
702 | for (fn = fns; fn != NULL_TREE; fn = TREE_CHAIN (fn)) | |
703 | { | |
704 | cp_error_at ("%s %+#D", str, TREE_VALUE (fn)); | |
705 | str = " "; | |
706 | } | |
707 | } | |
708 | ||
75650646 | 709 | /* Returns the template (one of the functions given by TEMPLATE_ID) |
e1467ff2 MM |
710 | which can be specialized to match the indicated DECL with the |
711 | explicit template args given in TEMPLATE_ID. If | |
712 | NEED_MEMBER_TEMPLATE is true the function is a specialization of a | |
713 | member template. The template args (those explicitly specified and | |
714 | those deduced) are output in a newly created vector *TARGS_OUT. If | |
715 | it is impossible to determine the result, an error message is | |
716 | issued, unless COMPLAIN is 0. The DECL may be NULL_TREE if none is | |
717 | available. */ | |
75650646 | 718 | |
386b8a85 | 719 | tree |
e1467ff2 | 720 | determine_specialization (template_id, decl, targs_out, |
75650646 MM |
721 | need_member_template, |
722 | complain) | |
386b8a85 | 723 | tree template_id; |
e1467ff2 | 724 | tree decl; |
386b8a85 JM |
725 | tree* targs_out; |
726 | int need_member_template; | |
727 | int complain; | |
728 | { | |
75650646 MM |
729 | tree fns = TREE_OPERAND (template_id, 0); |
730 | tree targs_in = TREE_OPERAND (template_id, 1); | |
e1467ff2 | 731 | tree templates = NULL_TREE; |
386b8a85 | 732 | tree fn; |
75650646 MM |
733 | int overloaded; |
734 | int i; | |
386b8a85 | 735 | |
e1467ff2 MM |
736 | *targs_out = NULL_TREE; |
737 | ||
aa36c081 JM |
738 | if (is_overloaded_fn (fns)) |
739 | fn = get_first_fn (fns); | |
740 | else | |
741 | fn = NULL_TREE; | |
386b8a85 | 742 | |
aa36c081 | 743 | overloaded = really_overloaded_fn (fns); |
aa36c081 | 744 | for (; fn != NULL_TREE; |
386b8a85 JM |
745 | fn = overloaded ? DECL_CHAIN (fn) : NULL_TREE) |
746 | { | |
75650646 MM |
747 | tree tmpl; |
748 | ||
749 | if (!need_member_template | |
750 | && TREE_CODE (fn) == FUNCTION_DECL | |
e1467ff2 | 751 | && DECL_FUNCTION_MEMBER_P (fn) |
75650646 MM |
752 | && DECL_USE_TEMPLATE (fn) |
753 | && DECL_TI_TEMPLATE (fn)) | |
e1467ff2 MM |
754 | /* We can get here when processing something like: |
755 | template <class T> class X { void f(); } | |
756 | template <> void X<int>::f() {} | |
757 | We're specializing a member function, but not a member | |
758 | template. */ | |
75650646 MM |
759 | tmpl = DECL_TI_TEMPLATE (fn); |
760 | else if (TREE_CODE (fn) != TEMPLATE_DECL | |
e1467ff2 | 761 | || (need_member_template && !is_member_template (fn))) |
798eed5e | 762 | continue; |
75650646 MM |
763 | else |
764 | tmpl = fn; | |
386b8a85 | 765 | |
75650646 | 766 | if (list_length (targs_in) > DECL_NTPARMS (tmpl)) |
386b8a85 JM |
767 | continue; |
768 | ||
e1467ff2 | 769 | if (decl == NULL_TREE) |
386b8a85 | 770 | { |
e1467ff2 MM |
771 | tree targs = make_scratch_vec (DECL_NTPARMS (tmpl)); |
772 | ||
773 | /* We allow incomplete unification here, because we are going to | |
774 | check all the functions. */ | |
775 | i = type_unification (DECL_INNERMOST_TEMPLATE_PARMS (tmpl), | |
050367a3 | 776 | targs, |
e1467ff2 MM |
777 | NULL_TREE, |
778 | NULL_TREE, | |
779 | targs_in, | |
3b3ba9f0 | 780 | 1, 1); |
e1467ff2 MM |
781 | |
782 | if (i == 0) | |
783 | /* Unification was successful. */ | |
784 | templates = scratch_tree_cons (targs, tmpl, templates); | |
386b8a85 | 785 | } |
e1467ff2 MM |
786 | else |
787 | templates = scratch_tree_cons (NULL_TREE, tmpl, templates); | |
386b8a85 | 788 | } |
75650646 | 789 | |
e1467ff2 | 790 | if (decl != NULL_TREE) |
386b8a85 | 791 | { |
e1467ff2 MM |
792 | tree tmpl = most_specialized (templates, decl, targs_in); |
793 | ||
794 | if (tmpl == error_mark_node) | |
795 | goto ambiguous; | |
796 | else if (tmpl == NULL_TREE) | |
797 | goto no_match; | |
798 | ||
799 | *targs_out = get_bindings (tmpl, decl, targs_in); | |
800 | return tmpl; | |
801 | } | |
802 | ||
803 | if (templates == NULL_TREE) | |
804 | { | |
805 | no_match: | |
386b8a85 | 806 | if (complain) |
798eed5e JM |
807 | cp_error ("`%D' does not match any template declaration", |
808 | template_id); | |
75650646 | 809 | |
386b8a85 JM |
810 | return NULL_TREE; |
811 | } | |
e1467ff2 | 812 | else if (TREE_CHAIN (templates) != NULL_TREE) |
386b8a85 | 813 | { |
e1467ff2 | 814 | ambiguous: |
386b8a85 JM |
815 | if (complain) |
816 | { | |
798eed5e JM |
817 | cp_error ("ambiguous template specialization `%D'", |
818 | template_id); | |
e1467ff2 | 819 | print_candidates (templates); |
386b8a85 | 820 | } |
386b8a85 JM |
821 | return NULL_TREE; |
822 | } | |
823 | ||
824 | /* We have one, and exactly one, match. */ | |
e1467ff2 MM |
825 | *targs_out = TREE_PURPOSE (templates); |
826 | return TREE_VALUE (templates); | |
8d08fdba | 827 | } |
6757edfe | 828 | |
386b8a85 | 829 | /* Check to see if the function just declared, as indicated in |
75650646 MM |
830 | DECLARATOR, and in DECL, is a specialization of a function |
831 | template. We may also discover that the declaration is an explicit | |
832 | instantiation at this point. | |
833 | ||
e1467ff2 MM |
834 | Returns DECL, or an equivalent declaration that should be used |
835 | instead. | |
75650646 | 836 | |
75650646 MM |
837 | FLAGS is a bitmask consisting of the following flags: |
838 | ||
839 | 1: We are being called by finish_struct. (We are unable to | |
840 | determine what template is specialized by an in-class | |
841 | declaration until the class definition is complete, so | |
842 | finish_struct_methods calls this function again later to finish | |
843 | the job.) | |
844 | 2: The function has a definition. | |
845 | 4: The function is a friend. | |
846 | 8: The function is known to be a specialization of a member | |
847 | template. | |
848 | ||
849 | The TEMPLATE_COUNT is the number of references to qualifying | |
850 | template classes that appeared in the name of the function. For | |
851 | example, in | |
852 | ||
853 | template <class T> struct S { void f(); }; | |
854 | void S<int>::f(); | |
855 | ||
856 | the TEMPLATE_COUNT would be 1. However, explicitly specialized | |
857 | classes are not counted in the TEMPLATE_COUNT, so that in | |
858 | ||
859 | template <class T> struct S {}; | |
860 | template <> struct S<int> { void f(); } | |
861 | template <> | |
862 | void S<int>::f(); | |
863 | ||
864 | the TEMPLATE_COUNT would be 0. (Note that this declaration is | |
865 | illegal; there should be no template <>.) | |
866 | ||
867 | If the function is a specialization, it is marked as such via | |
868 | DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO | |
869 | is set up correctly, and it is added to the list of specializations | |
870 | for that template. */ | |
386b8a85 | 871 | |
e1467ff2 | 872 | tree |
27bb8339 | 873 | check_explicit_specialization (declarator, decl, template_count, flags) |
386b8a85 JM |
874 | tree declarator; |
875 | tree decl; | |
876 | int template_count; | |
877 | int flags; | |
878 | { | |
75650646 MM |
879 | int finish_member = flags & 1; |
880 | int have_def = flags & 2; | |
881 | int is_friend = flags & 4; | |
882 | int specialization = 0; | |
e1467ff2 | 883 | int explicit_instantiation = 0; |
75650646 MM |
884 | int member_specialization = flags & 8; |
885 | ||
886 | tree ctype = DECL_CLASS_CONTEXT (decl); | |
887 | tree dname = DECL_NAME (decl); | |
386b8a85 | 888 | |
75650646 | 889 | if (!finish_member) |
386b8a85 | 890 | { |
75650646 MM |
891 | if (processing_specialization) |
892 | { | |
893 | /* The last template header was of the form template <>. */ | |
894 | ||
895 | if (template_header_count > template_count) | |
896 | { | |
897 | /* There were more template headers than qualifying template | |
898 | classes. */ | |
899 | if (template_header_count - template_count > 1) | |
900 | /* There shouldn't be that many template parameter | |
901 | lists. There can be at most one parameter list for | |
902 | every qualifying class, plus one for the function | |
903 | itself. */ | |
904 | cp_error ("too many template parameter lists in declaration of `%D'", decl); | |
386b8a85 | 905 | |
75650646 MM |
906 | SET_DECL_TEMPLATE_SPECIALIZATION (decl); |
907 | if (ctype) | |
908 | member_specialization = 1; | |
909 | else | |
910 | specialization = 1; | |
911 | } | |
912 | else if (template_header_count == template_count) | |
913 | { | |
914 | /* The counts are equal. So, this might be a | |
915 | specialization, but it is not a specialization of a | |
916 | member template. It might be something like | |
917 | ||
918 | template <class T> struct S { | |
919 | void f(int i); | |
920 | }; | |
921 | template <> | |
922 | void S<int>::f(int i) {} */ | |
923 | specialization = 1; | |
924 | SET_DECL_TEMPLATE_SPECIALIZATION (decl); | |
925 | } | |
926 | else | |
927 | { | |
928 | /* This cannot be an explicit specialization. There are not | |
929 | enough headers for all of the qualifying classes. For | |
930 | example, we might have: | |
931 | ||
932 | template <> | |
933 | void S<int>::T<char>::f(); | |
934 | ||
935 | But, we're missing another template <>. */ | |
936 | cp_error("too few template parameter lists in declaration of `%D'", decl); | |
e1467ff2 | 937 | return decl; |
75650646 MM |
938 | } |
939 | } | |
940 | else if (processing_explicit_instantiation) | |
941 | { | |
942 | if (template_header_count) | |
943 | cp_error ("template parameter list used in explicit instantiation"); | |
944 | ||
945 | if (have_def) | |
946 | cp_error ("definition provided for explicit instantiation"); | |
386b8a85 | 947 | |
e1467ff2 | 948 | explicit_instantiation = 1; |
75650646 | 949 | } |
c6f2ed0d JM |
950 | else if (ctype != NULL_TREE |
951 | && !TYPE_BEING_DEFINED (ctype) | |
952 | && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype)) | |
386b8a85 | 953 | { |
c6f2ed0d | 954 | /* This case catches outdated code that looks like this: |
75650646 | 955 | |
c6f2ed0d | 956 | template <class T> struct S { void f(); }; |
75650646 MM |
957 | void S<int>::f() {} // Missing template <> |
958 | ||
959 | We disable this check when the type is being defined to | |
960 | avoid complaining about default compiler-generated | |
961 | constructors, destructors, and assignment operators. | |
962 | Since the type is an instantiation, not a specialization, | |
963 | these are the only functions that can be defined before | |
c6f2ed0d | 964 | the class is complete. */ |
75650646 | 965 | |
c6f2ed0d JM |
966 | /* If they said |
967 | template <class T> void S<int>::f() {} | |
968 | that's bogus. */ | |
969 | if (template_header_count) | |
970 | { | |
971 | cp_error ("template parameters specified in specialization"); | |
972 | return decl; | |
973 | } | |
974 | ||
975 | if (pedantic) | |
976 | cp_pedwarn | |
977 | ("explicit specialization not preceded by `template <>'"); | |
978 | specialization = 1; | |
979 | SET_DECL_TEMPLATE_SPECIALIZATION (decl); | |
980 | } | |
981 | else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR) | |
982 | { | |
983 | /* This case handles bogus declarations like | |
75650646 MM |
984 | template <> template <class T> |
985 | void f<int>(); */ | |
986 | ||
c6f2ed0d JM |
987 | cp_error ("template-id `%D' in declaration of primary template", |
988 | declarator); | |
e1467ff2 | 989 | return decl; |
386b8a85 | 990 | } |
75650646 | 991 | } |
386b8a85 | 992 | |
670960ac JM |
993 | if (specialization || member_specialization) |
994 | { | |
995 | tree t = TYPE_ARG_TYPES (TREE_TYPE (decl)); | |
996 | for (; t; t = TREE_CHAIN (t)) | |
997 | if (TREE_PURPOSE (t)) | |
998 | { | |
999 | cp_pedwarn | |
1000 | ("default argument specified in explicit specialization"); | |
1001 | break; | |
1002 | } | |
1003 | } | |
1004 | ||
e1467ff2 | 1005 | if (specialization || member_specialization || explicit_instantiation) |
75650646 MM |
1006 | { |
1007 | tree tmpl = NULL_TREE; | |
1008 | tree targs = NULL_TREE; | |
75650646 MM |
1009 | |
1010 | /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */ | |
386b8a85 JM |
1011 | if (TREE_CODE (declarator) != TEMPLATE_ID_EXPR) |
1012 | { | |
1013 | tree fns; | |
1014 | ||
1015 | my_friendly_assert (TREE_CODE (declarator) == IDENTIFIER_NODE, | |
1016 | 0); | |
1017 | if (!ctype) | |
30394414 | 1018 | fns = IDENTIFIER_NAMESPACE_VALUE (dname); |
386b8a85 JM |
1019 | else |
1020 | fns = dname; | |
1021 | ||
75650646 MM |
1022 | declarator = |
1023 | lookup_template_function (fns, NULL_TREE); | |
386b8a85 JM |
1024 | } |
1025 | ||
1026 | if (TREE_CODE (TREE_OPERAND (declarator, 0)) == LOOKUP_EXPR) | |
1027 | { | |
1028 | /* A friend declaration. We can't do much, because we don't | |
1029 | know what this resolves to, yet. */ | |
1030 | my_friendly_assert (is_friend != 0, 0); | |
e1467ff2 | 1031 | my_friendly_assert (!explicit_instantiation, 0); |
386b8a85 | 1032 | SET_DECL_IMPLICIT_INSTANTIATION (decl); |
e1467ff2 | 1033 | return decl; |
386b8a85 JM |
1034 | } |
1035 | ||
75650646 MM |
1036 | if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype)) |
1037 | { | |
1038 | /* Since finish_struct_1 has not been called yet, we | |
1039 | can't call lookup_fnfields. We note that this | |
1040 | template is a specialization, and proceed, letting | |
1041 | finish_struct fix this up later. */ | |
e1467ff2 MM |
1042 | tree ti = perm_tree_cons (NULL_TREE, |
1043 | TREE_OPERAND (declarator, 1), | |
1044 | NULL_TREE); | |
1045 | TI_PENDING_SPECIALIZATION_FLAG (ti) = 1; | |
1046 | DECL_TEMPLATE_INFO (decl) = ti; | |
1047 | /* This should not be an instantiation; explicit | |
1048 | instantiation directives can only occur at the top | |
1049 | level. */ | |
1050 | my_friendly_assert (!explicit_instantiation, 0); | |
1051 | return decl; | |
75650646 MM |
1052 | } |
1053 | else if (ctype != NULL_TREE | |
1054 | && (TREE_CODE (TREE_OPERAND (declarator, 0)) == | |
1055 | IDENTIFIER_NODE)) | |
386b8a85 | 1056 | { |
75650646 MM |
1057 | /* Find the list of functions in ctype that have the same |
1058 | name as the declared function. */ | |
1059 | tree name = TREE_OPERAND (declarator, 0); | |
386b8a85 | 1060 | tree fns; |
75650646 MM |
1061 | |
1062 | if (name == constructor_name (ctype) | |
1063 | || name == constructor_name_full (ctype)) | |
386b8a85 | 1064 | { |
75650646 MM |
1065 | int is_constructor = DECL_CONSTRUCTOR_P (decl); |
1066 | ||
1067 | if (is_constructor ? !TYPE_HAS_CONSTRUCTOR (ctype) | |
1068 | : !TYPE_HAS_DESTRUCTOR (ctype)) | |
1069 | { | |
1070 | /* From [temp.expl.spec]: | |
e1467ff2 | 1071 | |
75650646 MM |
1072 | If such an explicit specialization for the member |
1073 | of a class template names an implicitly-declared | |
1074 | special member function (clause _special_), the | |
e1467ff2 MM |
1075 | program is ill-formed. |
1076 | ||
1077 | Similar language is found in [temp.explicit]. */ | |
75650646 MM |
1078 | cp_error ("specialization of implicitly-declared special member function"); |
1079 | ||
e1467ff2 | 1080 | return decl; |
75650646 | 1081 | } |
386b8a85 | 1082 | |
42da2fd8 | 1083 | name = is_constructor ? ctor_identifier : dtor_identifier; |
75650646 | 1084 | } |
42da2fd8 MM |
1085 | |
1086 | fns = lookup_fnfields (TYPE_BINFO (ctype), name, 1); | |
386b8a85 JM |
1087 | |
1088 | if (fns == NULL_TREE) | |
1089 | { | |
75650646 MM |
1090 | cp_error ("no member function `%s' declared in `%T'", |
1091 | IDENTIFIER_POINTER (name), | |
386b8a85 | 1092 | ctype); |
e1467ff2 | 1093 | return decl; |
386b8a85 JM |
1094 | } |
1095 | else | |
1096 | TREE_OPERAND (declarator, 0) = fns; | |
1097 | } | |
75650646 | 1098 | |
e1467ff2 MM |
1099 | /* Figure out what exactly is being specialized at this point. |
1100 | Note that for an explicit instantiation, even one for a | |
38e01259 | 1101 | member function, we cannot tell apriori whether the |
e1467ff2 MM |
1102 | instantiation is for a member template, or just a member |
1103 | function of a template class. In particular, even in if the | |
1104 | instantiation is for a member template, the template | |
1105 | arguments could be deduced from the declaration. */ | |
1106 | tmpl = determine_specialization (declarator, decl, | |
1107 | &targs, | |
75650646 MM |
1108 | member_specialization, |
1109 | 1); | |
386b8a85 JM |
1110 | |
1111 | if (tmpl) | |
1112 | { | |
e1467ff2 MM |
1113 | if (explicit_instantiation) |
1114 | { | |
1115 | decl = instantiate_template (tmpl, targs); | |
1116 | if (!DECL_TEMPLATE_SPECIALIZATION (decl)) | |
1117 | /* There doesn't seem to be anything in the draft to | |
1118 | prevent a specialization from being explicitly | |
1119 | instantiated. We're careful not to destroy the | |
1120 | information indicating that this is a | |
1121 | specialization here. */ | |
1122 | SET_DECL_EXPLICIT_INSTANTIATION (decl); | |
1123 | return decl; | |
1124 | } | |
8857f91e MM |
1125 | else if (DECL_STATIC_FUNCTION_P (tmpl) |
1126 | && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)) | |
366c0f1e MM |
1127 | { |
1128 | revert_static_member_fn (&decl, 0, 0); | |
1129 | last_function_parms = TREE_CHAIN (last_function_parms); | |
1130 | } | |
e1467ff2 MM |
1131 | |
1132 | /* Mangle the function name appropriately. Note that we do | |
1133 | not mangle specializations of non-template member | |
1134 | functions of template classes, e.g. with | |
1135 | template <class T> struct S { void f(); } | |
1136 | and given the specialization | |
1137 | template <> void S<int>::f() {} | |
1138 | we do not mangle S<int>::f() here. That's because it's | |
1139 | just an ordinary member function and doesn't need special | |
1140 | treatment. */ | |
1141 | if ((is_member_template (tmpl) || ctype == NULL_TREE) | |
75650646 | 1142 | && name_mangling_version >= 1) |
386b8a85 JM |
1143 | { |
1144 | tree arg_types = TYPE_ARG_TYPES (TREE_TYPE (tmpl)); | |
1145 | ||
1146 | if (ctype | |
1147 | && TREE_CODE (TREE_TYPE (tmpl)) == FUNCTION_TYPE) | |
1148 | arg_types = | |
1149 | hash_tree_chain (build_pointer_type (ctype), | |
1150 | arg_types); | |
1151 | ||
1152 | DECL_ASSEMBLER_NAME (decl) | |
1153 | = build_template_decl_overload | |
1154 | (DECL_NAME (decl), | |
1155 | arg_types, | |
1156 | TREE_TYPE (TREE_TYPE (tmpl)), | |
1157 | DECL_INNERMOST_TEMPLATE_PARMS (tmpl), | |
1158 | targs, ctype != NULL_TREE); | |
1159 | } | |
1160 | ||
1161 | if (is_friend && !have_def) | |
1162 | { | |
1163 | /* This is not really a declaration of a specialization. | |
1164 | It's just the name of an instantiation. But, it's not | |
1165 | a request for an instantiation, either. */ | |
1166 | SET_DECL_IMPLICIT_INSTANTIATION (decl); | |
1167 | DECL_TEMPLATE_INFO (decl) | |
1168 | = perm_tree_cons (tmpl, targs, NULL_TREE); | |
e1467ff2 | 1169 | return decl; |
386b8a85 JM |
1170 | } |
1171 | ||
386b8a85 JM |
1172 | /* If DECL_TI_TEMPLATE (decl), the decl is an |
1173 | instantiation of a specialization of a member template. | |
1174 | (In other words, there was a member template, in a | |
1175 | class template. That member template was specialized. | |
1176 | We then instantiated the class, so there is now an | |
1177 | instance of that specialization.) | |
1178 | ||
1179 | According to the CD2, | |
1180 | ||
1181 | 14.7.3.13 [tmpl.expl.spec] | |
1182 | ||
1183 | A specialization of a member function template or | |
1184 | member class template of a non-specialized class | |
1185 | template is itself a template. | |
1186 | ||
7ac63bca | 1187 | So, we just leave the template info alone in this case. */ |
386b8a85 JM |
1188 | if (!(DECL_TEMPLATE_INFO (decl) && DECL_TI_TEMPLATE (decl))) |
1189 | DECL_TEMPLATE_INFO (decl) | |
1190 | = perm_tree_cons (tmpl, targs, NULL_TREE); | |
75650646 MM |
1191 | |
1192 | register_specialization (decl, tmpl, targs); | |
1193 | ||
e1467ff2 | 1194 | return decl; |
386b8a85 JM |
1195 | } |
1196 | } | |
75650646 | 1197 | |
e1467ff2 | 1198 | return decl; |
386b8a85 | 1199 | } |
75650646 | 1200 | |
75650646 MM |
1201 | /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template |
1202 | parameters. These are represented in the same format used for | |
1203 | DECL_TEMPLATE_PARMS. */ | |
1204 | ||
1205 | int comp_template_parms (parms1, parms2) | |
1206 | tree parms1; | |
1207 | tree parms2; | |
1208 | { | |
1209 | tree p1; | |
1210 | tree p2; | |
1211 | ||
1212 | if (parms1 == parms2) | |
1213 | return 1; | |
1214 | ||
1215 | for (p1 = parms1, p2 = parms2; | |
1216 | p1 != NULL_TREE && p2 != NULL_TREE; | |
1217 | p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2)) | |
1218 | { | |
1219 | tree t1 = TREE_VALUE (p1); | |
1220 | tree t2 = TREE_VALUE (p2); | |
1221 | int i; | |
1222 | ||
1223 | my_friendly_assert (TREE_CODE (t1) == TREE_VEC, 0); | |
1224 | my_friendly_assert (TREE_CODE (t2) == TREE_VEC, 0); | |
1225 | ||
1226 | if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2)) | |
1227 | return 0; | |
1228 | ||
1229 | for (i = 0; i < TREE_VEC_LENGTH (t2); ++i) | |
1230 | { | |
1231 | tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i)); | |
1232 | tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i)); | |
1233 | ||
1234 | if (TREE_CODE (parm1) != TREE_CODE (parm2)) | |
1235 | return 0; | |
1236 | ||
1237 | if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM) | |
1238 | continue; | |
1239 | else if (!comptypes (TREE_TYPE (parm1), | |
1240 | TREE_TYPE (parm2), 1)) | |
1241 | return 0; | |
1242 | } | |
1243 | } | |
1244 | ||
1245 | if ((p1 != NULL_TREE) != (p2 != NULL_TREE)) | |
1246 | /* One set of parameters has more parameters lists than the | |
1247 | other. */ | |
1248 | return 0; | |
1249 | ||
1250 | return 1; | |
1251 | } | |
1252 | ||
f84b4be9 JM |
1253 | /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL, |
1254 | ORIG_LEVEL, DECL, and TYPE. */ | |
1255 | ||
1256 | static tree | |
1257 | build_template_parm_index (index, level, orig_level, decl, type) | |
1258 | int index; | |
1259 | int level; | |
1260 | int orig_level; | |
1261 | tree decl; | |
1262 | tree type; | |
1263 | { | |
1264 | tree t = make_node (TEMPLATE_PARM_INDEX); | |
1265 | TEMPLATE_PARM_IDX (t) = index; | |
1266 | TEMPLATE_PARM_LEVEL (t) = level; | |
1267 | TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level; | |
1268 | TEMPLATE_PARM_DECL (t) = decl; | |
1269 | TREE_TYPE (t) = type; | |
1270 | ||
1271 | return t; | |
1272 | } | |
1273 | ||
f84b4be9 | 1274 | /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose |
93cdc044 | 1275 | TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a |
f84b4be9 JM |
1276 | TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a |
1277 | new one is created. */ | |
1278 | ||
1279 | static tree | |
93cdc044 | 1280 | reduce_template_parm_level (index, type, levels) |
f84b4be9 JM |
1281 | tree index; |
1282 | tree type; | |
93cdc044 | 1283 | int levels; |
f84b4be9 JM |
1284 | { |
1285 | if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE | |
1286 | || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index)) | |
93cdc044 | 1287 | != TEMPLATE_PARM_LEVEL (index) - levels)) |
f84b4be9 JM |
1288 | { |
1289 | tree decl | |
1290 | = build_decl (TREE_CODE (TEMPLATE_PARM_DECL (index)), | |
1291 | DECL_NAME (TEMPLATE_PARM_DECL (index)), | |
1292 | type); | |
1293 | tree t | |
1294 | = build_template_parm_index (TEMPLATE_PARM_IDX (index), | |
93cdc044 | 1295 | TEMPLATE_PARM_LEVEL (index) - levels, |
f84b4be9 JM |
1296 | TEMPLATE_PARM_ORIG_LEVEL (index), |
1297 | decl, type); | |
1298 | TEMPLATE_PARM_DESCENDANTS (index) = t; | |
cae40af6 JM |
1299 | |
1300 | /* Template template parameters need this. */ | |
1301 | DECL_TEMPLATE_PARMS (decl) | |
1302 | = DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index)); | |
f84b4be9 JM |
1303 | } |
1304 | ||
1305 | return TEMPLATE_PARM_DESCENDANTS (index); | |
1306 | } | |
1307 | ||
8d08fdba | 1308 | /* Process information from new template parameter NEXT and append it to the |
5566b478 | 1309 | LIST being built. */ |
e92cc029 | 1310 | |
8d08fdba MS |
1311 | tree |
1312 | process_template_parm (list, next) | |
1313 | tree list, next; | |
1314 | { | |
1315 | tree parm; | |
1316 | tree decl = 0; | |
a292b002 | 1317 | tree defval; |
5566b478 | 1318 | int is_type, idx; |
f84b4be9 | 1319 | |
8d08fdba MS |
1320 | parm = next; |
1321 | my_friendly_assert (TREE_CODE (parm) == TREE_LIST, 259); | |
a292b002 MS |
1322 | defval = TREE_PURPOSE (parm); |
1323 | parm = TREE_VALUE (parm); | |
1324 | is_type = TREE_PURPOSE (parm) == class_type_node; | |
5566b478 MS |
1325 | |
1326 | if (list) | |
1327 | { | |
1328 | tree p = TREE_VALUE (tree_last (list)); | |
1329 | ||
1330 | if (TREE_CODE (p) == TYPE_DECL) | |
1331 | idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p)); | |
73b0fce8 KL |
1332 | else if (TREE_CODE (p) == TEMPLATE_DECL) |
1333 | idx = TEMPLATE_TYPE_IDX (TREE_TYPE (DECL_TEMPLATE_RESULT (p))); | |
5566b478 | 1334 | else |
f84b4be9 | 1335 | idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p)); |
5566b478 MS |
1336 | ++idx; |
1337 | } | |
1338 | else | |
1339 | idx = 0; | |
1340 | ||
8d08fdba MS |
1341 | if (!is_type) |
1342 | { | |
a292b002 | 1343 | my_friendly_assert (TREE_CODE (TREE_PURPOSE (parm)) == TREE_LIST, 260); |
8d08fdba | 1344 | /* is a const-param */ |
a292b002 | 1345 | parm = grokdeclarator (TREE_VALUE (parm), TREE_PURPOSE (parm), |
c11b6f21 | 1346 | PARM, 0, NULL_TREE); |
8d08fdba MS |
1347 | /* A template parameter is not modifiable. */ |
1348 | TREE_READONLY (parm) = 1; | |
c91a56d2 MS |
1349 | if (IS_AGGR_TYPE (TREE_TYPE (parm)) |
1350 | && TREE_CODE (TREE_TYPE (parm)) != TEMPLATE_TYPE_PARM) | |
8d08fdba | 1351 | { |
c91a56d2 MS |
1352 | cp_error ("`%#T' is not a valid type for a template constant parameter", |
1353 | TREE_TYPE (parm)); | |
1354 | if (DECL_NAME (parm) == NULL_TREE) | |
1355 | error (" a template type parameter must begin with `class' or `typename'"); | |
8d08fdba MS |
1356 | TREE_TYPE (parm) = void_type_node; |
1357 | } | |
37c46b43 MS |
1358 | else if (pedantic |
1359 | && (TREE_CODE (TREE_TYPE (parm)) == REAL_TYPE | |
1360 | || TREE_CODE (TREE_TYPE (parm)) == COMPLEX_TYPE)) | |
eb66be0e MS |
1361 | cp_pedwarn ("`%T' is not a valid type for a template constant parameter", |
1362 | TREE_TYPE (parm)); | |
8d08fdba MS |
1363 | if (TREE_PERMANENT (parm) == 0) |
1364 | { | |
1365 | parm = copy_node (parm); | |
1366 | TREE_PERMANENT (parm) = 1; | |
1367 | } | |
8d08fdba | 1368 | decl = build_decl (CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm)); |
f84b4be9 JM |
1369 | DECL_INITIAL (parm) = DECL_INITIAL (decl) |
1370 | = build_template_parm_index (idx, processing_template_decl, | |
1371 | processing_template_decl, | |
1372 | decl, TREE_TYPE (parm)); | |
8d08fdba MS |
1373 | } |
1374 | else | |
1375 | { | |
73b0fce8 KL |
1376 | tree t; |
1377 | parm = TREE_VALUE (parm); | |
1378 | ||
1379 | if (parm && TREE_CODE (parm) == TEMPLATE_DECL) | |
1380 | { | |
1381 | t = make_lang_type (TEMPLATE_TEMPLATE_PARM); | |
1382 | /* This is for distinguishing between real templates and template | |
1383 | template parameters */ | |
1384 | TREE_TYPE (parm) = t; | |
1385 | TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t; | |
1386 | decl = parm; | |
1387 | } | |
1388 | else | |
1389 | { | |
1390 | t = make_lang_type (TEMPLATE_TYPE_PARM); | |
1391 | /* parm is either IDENTIFIER_NODE or NULL_TREE */ | |
1392 | decl = build_decl (TYPE_DECL, parm, t); | |
1393 | } | |
1394 | ||
5566b478 | 1395 | CLASSTYPE_GOT_SEMICOLON (t) = 1; |
d2e5ee5c MS |
1396 | TYPE_NAME (t) = decl; |
1397 | TYPE_STUB_DECL (t) = decl; | |
a292b002 | 1398 | parm = decl; |
f84b4be9 JM |
1399 | TEMPLATE_TYPE_PARM_INDEX (t) |
1400 | = build_template_parm_index (idx, processing_template_decl, | |
1401 | processing_template_decl, | |
1402 | decl, TREE_TYPE (parm)); | |
8d08fdba | 1403 | } |
8ccc31eb | 1404 | SET_DECL_ARTIFICIAL (decl); |
8d08fdba | 1405 | pushdecl (decl); |
a292b002 | 1406 | parm = build_tree_list (defval, parm); |
8d08fdba MS |
1407 | return chainon (list, parm); |
1408 | } | |
1409 | ||
1410 | /* The end of a template parameter list has been reached. Process the | |
1411 | tree list into a parameter vector, converting each parameter into a more | |
1412 | useful form. Type parameters are saved as IDENTIFIER_NODEs, and others | |
1413 | as PARM_DECLs. */ | |
1414 | ||
1415 | tree | |
1416 | end_template_parm_list (parms) | |
1417 | tree parms; | |
1418 | { | |
5566b478 | 1419 | int nparms; |
8d08fdba | 1420 | tree parm; |
5566b478 MS |
1421 | tree saved_parmlist = make_tree_vec (list_length (parms)); |
1422 | ||
5566b478 MS |
1423 | current_template_parms |
1424 | = tree_cons (build_int_2 (0, processing_template_decl), | |
1425 | saved_parmlist, current_template_parms); | |
8d08fdba MS |
1426 | |
1427 | for (parm = parms, nparms = 0; parm; parm = TREE_CHAIN (parm), nparms++) | |
5566b478 | 1428 | TREE_VEC_ELT (saved_parmlist, nparms) = parm; |
a292b002 | 1429 | |
8d08fdba MS |
1430 | return saved_parmlist; |
1431 | } | |
1432 | ||
5566b478 MS |
1433 | /* end_template_decl is called after a template declaration is seen. */ |
1434 | ||
8d08fdba | 1435 | void |
5566b478 | 1436 | end_template_decl () |
8d08fdba | 1437 | { |
386b8a85 JM |
1438 | reset_specialization (); |
1439 | ||
5156628f | 1440 | if (! processing_template_decl) |
73aad9b9 JM |
1441 | return; |
1442 | ||
5566b478 MS |
1443 | /* This matches the pushlevel in begin_template_parm_list. */ |
1444 | poplevel (0, 0, 0); | |
8d08fdba | 1445 | |
5566b478 MS |
1446 | --processing_template_decl; |
1447 | current_template_parms = TREE_CHAIN (current_template_parms); | |
1448 | (void) get_pending_sizes (); /* Why? */ | |
1449 | } | |
8d08fdba | 1450 | |
9a3b49ac MS |
1451 | /* Generate a valid set of template args from current_template_parms. */ |
1452 | ||
1453 | tree | |
1454 | current_template_args () | |
5566b478 MS |
1455 | { |
1456 | tree header = current_template_parms; | |
98c1c668 JM |
1457 | int length = list_length (header); |
1458 | tree args = make_tree_vec (length); | |
1459 | int l = length; | |
1460 | ||
5566b478 | 1461 | while (header) |
8d08fdba | 1462 | { |
5566b478 MS |
1463 | tree a = copy_node (TREE_VALUE (header)); |
1464 | int i = TREE_VEC_LENGTH (a); | |
1465 | TREE_TYPE (a) = NULL_TREE; | |
1466 | while (i--) | |
1467 | { | |
98c1c668 JM |
1468 | tree t = TREE_VEC_ELT (a, i); |
1469 | ||
1470 | /* t will be a list if we are called from within a | |
1471 | begin/end_template_parm_list pair, but a vector directly | |
1472 | if within a begin/end_member_template_processing pair. */ | |
1473 | if (TREE_CODE (t) == TREE_LIST) | |
1474 | { | |
1475 | t = TREE_VALUE (t); | |
1476 | ||
73b0fce8 KL |
1477 | if (TREE_CODE (t) == TYPE_DECL |
1478 | || TREE_CODE (t) == TEMPLATE_DECL) | |
98c1c668 JM |
1479 | t = TREE_TYPE (t); |
1480 | else | |
1481 | t = DECL_INITIAL (t); | |
1482 | } | |
1483 | ||
5566b478 MS |
1484 | TREE_VEC_ELT (a, i) = t; |
1485 | } | |
98c1c668 | 1486 | TREE_VEC_ELT (args, --l) = a; |
5566b478 | 1487 | header = TREE_CHAIN (header); |
8d08fdba MS |
1488 | } |
1489 | ||
9a3b49ac MS |
1490 | return args; |
1491 | } | |
75650646 | 1492 | |
e1467ff2 MM |
1493 | |
1494 | /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated | |
1495 | template PARMS. Used by push_template_decl below. */ | |
1496 | ||
75650646 MM |
1497 | static tree |
1498 | build_template_decl (decl, parms) | |
1499 | tree decl; | |
1500 | tree parms; | |
1501 | { | |
1502 | tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE); | |
1503 | DECL_TEMPLATE_PARMS (tmpl) = parms; | |
1504 | DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl); | |
1505 | if (DECL_LANG_SPECIFIC (decl)) | |
1506 | { | |
1507 | DECL_CLASS_CONTEXT (tmpl) = DECL_CLASS_CONTEXT (decl); | |
1508 | DECL_STATIC_FUNCTION_P (tmpl) = | |
1509 | DECL_STATIC_FUNCTION_P (decl); | |
1510 | } | |
1511 | ||
1512 | return tmpl; | |
1513 | } | |
1514 | ||
050367a3 MM |
1515 | struct template_parm_data |
1516 | { | |
1517 | int level; | |
1518 | int* parms; | |
1519 | }; | |
1520 | ||
1521 | /* Subroutine of push_template_decl used to see if each template | |
1522 | parameter in a partial specialization is used in the explicit | |
1523 | argument list. If T is of the LEVEL given in DATA (which is | |
1524 | treated as a template_parm_data*), then DATA->PARMS is marked | |
1525 | appropriately. */ | |
1526 | ||
1527 | static int | |
1528 | mark_template_parm (t, data) | |
1529 | tree t; | |
1530 | void* data; | |
1531 | { | |
1532 | int level; | |
1533 | int idx; | |
1534 | struct template_parm_data* tpd = (struct template_parm_data*) data; | |
1535 | ||
1536 | if (TREE_CODE (t) == TEMPLATE_PARM_INDEX) | |
1537 | { | |
1538 | level = TEMPLATE_PARM_LEVEL (t); | |
1539 | idx = TEMPLATE_PARM_IDX (t); | |
1540 | } | |
1541 | else | |
1542 | { | |
1543 | level = TEMPLATE_TYPE_LEVEL (t); | |
1544 | idx = TEMPLATE_TYPE_IDX (t); | |
1545 | } | |
1546 | ||
1547 | if (level == tpd->level) | |
1548 | tpd->parms[idx] = 1; | |
1549 | ||
1550 | /* Return zero so that for_each_template_parm will continue the | |
1551 | traversal of the tree; we want to mark *every* template parm. */ | |
1552 | return 0; | |
1553 | } | |
1554 | ||
3ac3d9ea | 1555 | /* Creates a TEMPLATE_DECL for the indicated DECL using the template |
f84b4be9 JM |
1556 | parameters given by current_template_args, or reuses a |
1557 | previously existing one, if appropriate. Returns the DECL, or an | |
6757edfe MM |
1558 | equivalent one, if it is replaced via a call to duplicate_decls. |
1559 | ||
1560 | If IS_FRIEND is non-zero, DECL is a friend declaration. */ | |
3ac3d9ea MM |
1561 | |
1562 | tree | |
6757edfe | 1563 | push_template_decl_real (decl, is_friend) |
9a3b49ac | 1564 | tree decl; |
6757edfe | 1565 | int is_friend; |
9a3b49ac MS |
1566 | { |
1567 | tree tmpl; | |
f84b4be9 | 1568 | tree args; |
9a3b49ac | 1569 | tree info; |
f84b4be9 JM |
1570 | tree ctx; |
1571 | int primary; | |
6757edfe MM |
1572 | |
1573 | is_friend |= (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl)); | |
f84b4be9 JM |
1574 | |
1575 | if (is_friend) | |
1576 | /* For a friend, we want the context of the friend function, not | |
1577 | the type of which it is a friend. */ | |
1578 | ctx = DECL_CONTEXT (decl); | |
1579 | else if (DECL_REAL_CONTEXT (decl)) | |
1580 | /* In the case of a virtual function, we want the class in which | |
1581 | it is defined. */ | |
1582 | ctx = DECL_REAL_CONTEXT (decl); | |
1583 | else | |
1584 | /* Otherwise, if we're currently definining some class, the DECL | |
1585 | is assumed to be a member of the class. */ | |
1586 | ctx = current_class_type; | |
1587 | ||
93cdc044 JM |
1588 | /* For determining whether this is a primary template or not, we're really |
1589 | interested in the lexical context, not the true context. */ | |
1590 | if (is_friend) | |
6757edfe MM |
1591 | /* For a TYPE_DECL, there is no DECL_CLASS_CONTEXT. */ |
1592 | info = TREE_CODE (decl) == FUNCTION_DECL | |
1593 | ? DECL_CLASS_CONTEXT (decl) : current_class_type; | |
93cdc044 JM |
1594 | else |
1595 | info = ctx; | |
1596 | ||
1597 | if (info && TREE_CODE (info) == FUNCTION_DECL) | |
1598 | primary = 0; | |
6757edfe MM |
1599 | /* Note that template_class_depth returns 0 if given NULL_TREE, so |
1600 | this next line works even when we are at global scope. */ | |
1601 | else if (processing_template_decl > template_class_depth (info)) | |
9a3b49ac | 1602 | primary = 1; |
f84b4be9 JM |
1603 | else |
1604 | primary = 0; | |
9a3b49ac | 1605 | |
83566abf JM |
1606 | if (primary) |
1607 | { | |
1608 | if (current_lang_name == lang_name_c) | |
1609 | cp_error ("template with C linkage"); | |
1610 | if (TREE_CODE (decl) == TYPE_DECL && ANON_AGGRNAME_P (DECL_NAME (decl))) | |
1611 | cp_error ("template class without a name"); | |
1612 | } | |
1613 | ||
73aad9b9 | 1614 | /* Partial specialization. */ |
824b9a4c | 1615 | if (TREE_CODE (decl) == TYPE_DECL && DECL_ARTIFICIAL (decl) |
73aad9b9 JM |
1616 | && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl))) |
1617 | { | |
1618 | tree type = TREE_TYPE (decl); | |
1619 | tree maintmpl = CLASSTYPE_TI_TEMPLATE (type); | |
1620 | tree mainargs = CLASSTYPE_TI_ARGS (type); | |
1621 | tree spec = DECL_TEMPLATE_SPECIALIZATIONS (maintmpl); | |
1622 | ||
050367a3 MM |
1623 | /* We check that each of the template parameters given in the |
1624 | partial specialization is used in the argument list to the | |
1625 | specialization. For example: | |
1626 | ||
1627 | template <class T> struct S; | |
1628 | template <class T> struct S<T*>; | |
1629 | ||
1630 | The second declaration is OK because `T*' uses the template | |
1631 | parameter T, whereas | |
1632 | ||
1633 | template <class T> struct S<int>; | |
1634 | ||
1635 | is no good. Even trickier is: | |
1636 | ||
1637 | template <class T> | |
1638 | struct S1 | |
1639 | { | |
1640 | template <class U> | |
1641 | struct S2; | |
1642 | template <class U> | |
1643 | struct S2<T>; | |
1644 | }; | |
1645 | ||
1646 | The S2<T> declaration is actually illegal; it is a | |
1647 | full-specialization. Of course, | |
1648 | ||
1649 | template <class U> | |
1650 | struct S2<T (*)(U)>; | |
1651 | ||
1652 | or some such would have been OK. */ | |
1653 | int i; | |
1654 | struct template_parm_data tpd; | |
1655 | int ntparms = TREE_VEC_LENGTH (TREE_VALUE (current_template_parms)); | |
1656 | int did_error_intro = 0; | |
1657 | ||
1658 | tpd.level = TREE_INT_CST_HIGH (TREE_PURPOSE (current_template_parms)); | |
1659 | tpd.parms = alloca (sizeof (int) * ntparms); | |
1660 | for (i = 0; i < ntparms; ++i) | |
1661 | tpd.parms[i] = 0; | |
1662 | for (i = 0; i < TREE_VEC_LENGTH (mainargs); ++i) | |
1663 | for_each_template_parm (TREE_VEC_ELT (mainargs, i), | |
1664 | &mark_template_parm, | |
1665 | &tpd); | |
1666 | for (i = 0; i < ntparms; ++i) | |
1667 | if (tpd.parms[i] == 0) | |
1668 | { | |
1669 | /* One of the template parms was not used in the | |
1670 | specialization. */ | |
1671 | if (!did_error_intro) | |
1672 | { | |
1673 | cp_error ("template parameters not used in partial specialization:"); | |
1674 | did_error_intro = 1; | |
1675 | } | |
1676 | ||
1677 | cp_error (" `%D'", | |
1678 | TREE_VALUE (TREE_VEC_ELT | |
1679 | (TREE_VALUE (current_template_parms), | |
1680 | i))); | |
1681 | } | |
1682 | ||
73aad9b9 JM |
1683 | for (; spec; spec = TREE_CHAIN (spec)) |
1684 | { | |
1685 | /* purpose: args to main template | |
1686 | value: spec template */ | |
1687 | if (comp_template_args (TREE_PURPOSE (spec), mainargs)) | |
3ac3d9ea | 1688 | return decl; |
73aad9b9 JM |
1689 | } |
1690 | ||
6633d636 MS |
1691 | DECL_TEMPLATE_SPECIALIZATIONS (maintmpl) = CLASSTYPE_TI_SPEC_INFO (type) |
1692 | = perm_tree_cons (mainargs, TREE_VALUE (current_template_parms), | |
1693 | DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)); | |
73aad9b9 | 1694 | TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type; |
3ac3d9ea | 1695 | return decl; |
73aad9b9 JM |
1696 | } |
1697 | ||
9a3b49ac MS |
1698 | args = current_template_args (); |
1699 | ||
f84b4be9 JM |
1700 | if (!ctx |
1701 | || TREE_CODE (ctx) == FUNCTION_DECL | |
1702 | || TYPE_BEING_DEFINED (ctx) | |
1703 | || (is_friend && !DECL_TEMPLATE_INFO (decl))) | |
8d08fdba | 1704 | { |
75650646 | 1705 | if (DECL_LANG_SPECIFIC (decl) |
f84b4be9 JM |
1706 | && DECL_TEMPLATE_INFO (decl) |
1707 | && DECL_TI_TEMPLATE (decl)) | |
1708 | tmpl = DECL_TI_TEMPLATE (decl); | |
1709 | else | |
786b5245 | 1710 | { |
f84b4be9 JM |
1711 | tmpl = build_template_decl (decl, current_template_parms); |
1712 | ||
1713 | if (DECL_LANG_SPECIFIC (decl) | |
1714 | && DECL_TEMPLATE_SPECIALIZATION (decl)) | |
1715 | { | |
1716 | /* A specialization of a member template of a template | |
1717 | class. */ | |
1718 | SET_DECL_TEMPLATE_SPECIALIZATION (tmpl); | |
1719 | DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl); | |
1720 | DECL_TEMPLATE_INFO (decl) = NULL_TREE; | |
1721 | } | |
786b5245 | 1722 | } |
8d08fdba MS |
1723 | } |
1724 | else | |
1725 | { | |
6633d636 | 1726 | tree t; |
98c1c668 | 1727 | tree a; |
6633d636 | 1728 | |
73aad9b9 JM |
1729 | if (CLASSTYPE_TEMPLATE_INSTANTIATION (ctx)) |
1730 | cp_error ("must specialize `%#T' before defining member `%#D'", | |
1731 | ctx, decl); | |
824b9a4c | 1732 | if (TREE_CODE (decl) == TYPE_DECL && DECL_ARTIFICIAL (decl)) |
5566b478 | 1733 | tmpl = CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)); |
fc378698 | 1734 | else if (! DECL_TEMPLATE_INFO (decl)) |
c91a56d2 MS |
1735 | { |
1736 | cp_error ("template definition of non-template `%#D'", decl); | |
3ac3d9ea | 1737 | return decl; |
c91a56d2 | 1738 | } |
8d08fdba | 1739 | else |
5566b478 | 1740 | tmpl = DECL_TI_TEMPLATE (decl); |
98c1c668 JM |
1741 | |
1742 | if (is_member_template (tmpl)) | |
1743 | { | |
75650646 MM |
1744 | if (DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl) |
1745 | && DECL_TEMPLATE_SPECIALIZATION (decl)) | |
1746 | { | |
1747 | tree new_tmpl; | |
1748 | ||
1749 | /* The declaration is a specialization of a member | |
1750 | template, declared outside the class. Therefore, the | |
1751 | innermost template arguments will be NULL, so we | |
1752 | replace them with the arguments determined by the | |
1753 | earlier call to check_explicit_specialization. */ | |
1754 | args = DECL_TI_ARGS (decl); | |
1755 | ||
1756 | new_tmpl | |
1757 | = build_template_decl (decl, current_template_parms); | |
1758 | DECL_TEMPLATE_RESULT (new_tmpl) = decl; | |
1759 | TREE_TYPE (new_tmpl) = TREE_TYPE (decl); | |
1760 | DECL_TI_TEMPLATE (decl) = new_tmpl; | |
1761 | SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl); | |
1762 | DECL_TEMPLATE_INFO (new_tmpl) = | |
1763 | perm_tree_cons (tmpl, args, NULL_TREE); | |
1764 | ||
1765 | register_specialization (new_tmpl, tmpl, args); | |
3ac3d9ea | 1766 | return decl; |
75650646 MM |
1767 | } |
1768 | ||
98c1c668 JM |
1769 | a = TREE_VEC_ELT (args, TREE_VEC_LENGTH (args) - 1); |
1770 | t = DECL_INNERMOST_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)); | |
f84b4be9 | 1771 | if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a)) |
98c1c668 JM |
1772 | { |
1773 | cp_error ("got %d template parameters for `%#D'", | |
1774 | TREE_VEC_LENGTH (a), decl); | |
1775 | cp_error (" but %d required", TREE_VEC_LENGTH (t)); | |
1776 | } | |
1777 | if (TREE_VEC_LENGTH (args) > 1) | |
1778 | /* Get the template parameters for the enclosing template | |
1779 | class. */ | |
1780 | a = TREE_VEC_ELT (args, TREE_VEC_LENGTH (args) - 2); | |
1781 | else | |
1782 | a = NULL_TREE; | |
1783 | } | |
1784 | else | |
1785 | a = TREE_VEC_ELT (args, TREE_VEC_LENGTH (args) - 1); | |
1786 | ||
1787 | t = NULL_TREE; | |
6633d636 MS |
1788 | |
1789 | if (CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx)) | |
98c1c668 JM |
1790 | { |
1791 | /* When processing an inline member template of a | |
1792 | specialized class, there is no CLASSTYPE_TI_SPEC_INFO. */ | |
1793 | if (CLASSTYPE_TI_SPEC_INFO (ctx)) | |
1794 | t = TREE_VALUE (CLASSTYPE_TI_SPEC_INFO (ctx)); | |
1795 | } | |
1796 | else if (CLASSTYPE_TEMPLATE_INFO (ctx)) | |
1797 | t = DECL_INNERMOST_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (ctx)); | |
1798 | ||
1799 | /* There should be template arguments if and only if there is a | |
1800 | template class. */ | |
1801 | my_friendly_assert((a != NULL_TREE) == (t != NULL_TREE), 0); | |
6633d636 | 1802 | |
98c1c668 JM |
1803 | if (t != NULL_TREE |
1804 | && TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a)) | |
6633d636 MS |
1805 | { |
1806 | cp_error ("got %d template parameters for `%#D'", | |
98c1c668 | 1807 | TREE_VEC_LENGTH (a), decl); |
6633d636 MS |
1808 | cp_error (" but `%#T' has %d", ctx, TREE_VEC_LENGTH (t)); |
1809 | } | |
5566b478 | 1810 | } |
98c1c668 | 1811 | /* Get the innermost set of template arguments. */ |
30394414 | 1812 | args = innermost_args (args, 0); |
8d08fdba | 1813 | |
5566b478 MS |
1814 | DECL_TEMPLATE_RESULT (tmpl) = decl; |
1815 | TREE_TYPE (tmpl) = TREE_TYPE (decl); | |
8d08fdba | 1816 | |
6757edfe MM |
1817 | if (! ctx && !(is_friend && template_class_depth (info) > 0)) |
1818 | /* Note that we do not try to push a global template friend | |
1819 | declared in a template class; such a thing may well depend on | |
1820 | the template parameters of the class. */ | |
5566b478 | 1821 | tmpl = pushdecl_top_level (tmpl); |
8d08fdba | 1822 | |
5566b478 | 1823 | if (primary) |
6757edfe | 1824 | DECL_PRIMARY_TEMPLATE (tmpl) = tmpl; |
5566b478 MS |
1825 | |
1826 | info = perm_tree_cons (tmpl, args, NULL_TREE); | |
1827 | ||
824b9a4c | 1828 | if (TREE_CODE (decl) == TYPE_DECL && DECL_ARTIFICIAL (decl)) |
8d08fdba | 1829 | { |
5566b478 | 1830 | CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (tmpl)) = info; |
75650646 MM |
1831 | if (!ctx || TREE_CODE (ctx) != FUNCTION_DECL) |
1832 | DECL_NAME (decl) = classtype_mangled_name (TREE_TYPE (decl)); | |
51c184be | 1833 | } |
ec255269 MS |
1834 | else if (! DECL_LANG_SPECIFIC (decl)) |
1835 | cp_error ("template declaration of `%#D'", decl); | |
51c184be | 1836 | else |
5566b478 | 1837 | DECL_TEMPLATE_INFO (decl) = info; |
3ac3d9ea MM |
1838 | |
1839 | return DECL_TEMPLATE_RESULT (tmpl); | |
8d08fdba MS |
1840 | } |
1841 | ||
6757edfe MM |
1842 | tree |
1843 | push_template_decl (decl) | |
1844 | tree decl; | |
1845 | { | |
1846 | return push_template_decl_real (decl, 0); | |
1847 | } | |
1848 | ||
1849 | /* Called when a class template TYPE is redeclared with the indicated | |
1850 | template PARMS, e.g.: | |
7fe6899f MM |
1851 | |
1852 | template <class T> struct S; | |
1853 | template <class T> struct S {}; */ | |
1854 | ||
1855 | void | |
6757edfe | 1856 | redeclare_class_template (type, parms) |
7fe6899f | 1857 | tree type; |
6757edfe | 1858 | tree parms; |
7fe6899f MM |
1859 | { |
1860 | tree tmpl = CLASSTYPE_TI_TEMPLATE (type); | |
6757edfe | 1861 | tree tmpl_parms; |
7fe6899f MM |
1862 | int i; |
1863 | ||
1864 | if (!PRIMARY_TEMPLATE_P (tmpl)) | |
1865 | /* The type is nested in some template class. Nothing to worry | |
1866 | about here; there are no new template parameters for the nested | |
1867 | type. */ | |
1868 | return; | |
1869 | ||
6757edfe MM |
1870 | parms = INNERMOST_TEMPLATE_PARMS (parms); |
1871 | tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl); | |
1872 | ||
7fe6899f MM |
1873 | if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms)) |
1874 | { | |
1875 | cp_error_at ("previous declaration `%D'", tmpl); | |
1876 | cp_error ("used %d template parameter%s instead of %d", | |
1877 | TREE_VEC_LENGTH (tmpl_parms), | |
1878 | TREE_VEC_LENGTH (tmpl_parms) == 1 ? "" : "s", | |
1879 | TREE_VEC_LENGTH (parms)); | |
1880 | return; | |
1881 | } | |
1882 | ||
1883 | for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i) | |
1884 | { | |
1885 | tree tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i)); | |
1886 | tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i)); | |
1887 | tree tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)); | |
1888 | tree parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i)); | |
1889 | ||
1890 | if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)) | |
1891 | { | |
1892 | cp_error_at ("template parameter `%#D'", tmpl_parm); | |
1893 | cp_error ("redeclared here as `%#D'", parm); | |
1894 | return; | |
1895 | } | |
1896 | ||
1897 | if (tmpl_default != NULL_TREE && parm_default != NULL_TREE) | |
1898 | { | |
1899 | /* We have in [temp.param]: | |
1900 | ||
1901 | A template-parameter may not be given default arguments | |
1902 | by two different declarations in the same scope. */ | |
1903 | cp_error ("redefinition of default argument for `%#D'", parm); | |
1904 | return; | |
1905 | } | |
1906 | ||
1907 | if (parm_default != NULL_TREE) | |
1908 | /* Update the previous template parameters (which are the ones | |
1909 | that will really count) with the new default value. */ | |
1910 | TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default; | |
1911 | } | |
1912 | } | |
75650646 | 1913 | |
75650646 MM |
1914 | /* Attempt to convert the non-type template parameter EXPR to the |
1915 | indicated TYPE. If the conversion is successful, return the | |
1916 | converted value. If the conversion is unsuccesful, return | |
1917 | NULL_TREE if we issued an error message, or error_mark_node if we | |
1918 | did not. We issue error messages for out-and-out bad template | |
1919 | parameters, but not simply because the conversion failed, since we | |
1920 | might be just trying to do argument deduction. By the time this | |
1921 | function is called, neither TYPE nor EXPR may make use of template | |
1922 | parameters. */ | |
1923 | ||
1924 | static tree | |
e1467ff2 | 1925 | convert_nontype_argument (type, expr) |
75650646 MM |
1926 | tree type; |
1927 | tree expr; | |
1928 | { | |
1929 | tree expr_type = TREE_TYPE (expr); | |
1930 | ||
1931 | /* A template-argument for a non-type, non-template | |
1932 | template-parameter shall be one of: | |
1933 | ||
1934 | --an integral constant-expression of integral or enumeration | |
1935 | type; or | |
1936 | ||
1937 | --the name of a non-type template-parameter; or | |
1938 | ||
1939 | --the name of an object or function with external linkage, | |
1940 | including function templates and function template-ids but | |
86052cc3 | 1941 | excluding non-static class members, expressed as id-expression; |
75650646 MM |
1942 | or |
1943 | ||
1944 | --the address of an object or function with external linkage, | |
1945 | including function templates and function template-ids but | |
1946 | excluding non-static class members, expressed as & id-expression | |
1947 | where the & is optional if the name refers to a function or | |
1948 | array; or | |
1949 | ||
1950 | --a pointer to member expressed as described in _expr.unary.op_. */ | |
1951 | ||
86052cc3 JM |
1952 | /* An integral constant-expression can include const variables |
1953 | or enumerators. */ | |
1954 | if (INTEGRAL_TYPE_P (expr_type) && TREE_READONLY_DECL_P (expr)) | |
1955 | expr = decl_constant_value (expr); | |
1956 | ||
7bf2682f MM |
1957 | if (is_overloaded_fn (expr)) |
1958 | /* OK for now. We'll check that it has external linkage later. | |
1959 | Check this first since if expr_type is the unknown_type_node | |
1960 | we would otherwise complain below. */ | |
1961 | ; | |
1962 | else if (INTEGRAL_TYPE_P (expr_type) | |
1963 | || TYPE_PTRMEM_P (expr_type) | |
1964 | || TYPE_PTRMEMFUNC_P (expr_type) | |
1965 | /* The next two are g++ extensions. */ | |
1966 | || TREE_CODE (expr_type) == REAL_TYPE | |
1967 | || TREE_CODE (expr_type) == COMPLEX_TYPE) | |
75650646 | 1968 | { |
86052cc3 | 1969 | if (! TREE_CONSTANT (expr)) |
75650646 MM |
1970 | { |
1971 | cp_error ("non-constant `%E' cannot be used as template argument", | |
1972 | expr); | |
1973 | return NULL_TREE; | |
1974 | } | |
1975 | } | |
1976 | else if (TYPE_PTR_P (expr_type) | |
1977 | /* If expr is the address of an overloaded function, we | |
1978 | will get the unknown_type_node at this point. */ | |
1979 | || expr_type == unknown_type_node) | |
1980 | { | |
1981 | tree referent; | |
49bf0d6f MM |
1982 | tree e = expr; |
1983 | STRIP_NOPS (e); | |
75650646 | 1984 | |
49bf0d6f | 1985 | if (TREE_CODE (e) != ADDR_EXPR) |
75650646 MM |
1986 | { |
1987 | bad_argument: | |
1988 | cp_error ("`%E' is not a valid template argument", expr); | |
1989 | error ("it must be %s%s with external linkage", | |
1990 | TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE | |
1991 | ? "a pointer to " : "", | |
1992 | TREE_CODE (TREE_TYPE (TREE_TYPE (expr))) == FUNCTION_TYPE | |
1993 | ? "a function" : "an object"); | |
1994 | return NULL_TREE; | |
1995 | } | |
1996 | ||
49bf0d6f | 1997 | referent = TREE_OPERAND (e, 0); |
75650646 MM |
1998 | STRIP_NOPS (referent); |
1999 | ||
2000 | if (TREE_CODE (referent) == STRING_CST) | |
2001 | { | |
2002 | cp_error ("string literal %E is not a valid template argument", | |
2003 | referent); | |
2004 | error ("because it is the address of an object with static linkage"); | |
2005 | return NULL_TREE; | |
2006 | } | |
2007 | ||
2008 | if (is_overloaded_fn (referent)) | |
2009 | /* We'll check that it has external linkage later. */ | |
2010 | ; | |
2011 | else if (TREE_CODE (referent) != VAR_DECL) | |
2012 | goto bad_argument; | |
2013 | else if (!TREE_PUBLIC (referent)) | |
2014 | { | |
2015 | cp_error ("address of non-extern `%E' cannot be used as template argument", referent); | |
2016 | return error_mark_node; | |
2017 | } | |
2018 | } | |
c29c4e23 | 2019 | else if (TREE_CODE (expr) == VAR_DECL) |
75650646 MM |
2020 | { |
2021 | if (!TREE_PUBLIC (expr)) | |
2022 | goto bad_argument; | |
2023 | } | |
75650646 MM |
2024 | else |
2025 | { | |
2026 | cp_error ("object `%E' cannot be used as template argument", expr); | |
2027 | return NULL_TREE; | |
2028 | } | |
2029 | ||
2030 | switch (TREE_CODE (type)) | |
2031 | { | |
2032 | case INTEGER_TYPE: | |
2033 | case BOOLEAN_TYPE: | |
2034 | case ENUMERAL_TYPE: | |
2035 | /* For a non-type template-parameter of integral or enumeration | |
2036 | type, integral promotions (_conv.prom_) and integral | |
2037 | conversions (_conv.integral_) are applied. */ | |
2038 | if (!INTEGRAL_TYPE_P (expr_type)) | |
2039 | return error_mark_node; | |
2040 | ||
2041 | /* It's safe to call digest_init in this case; we know we're | |
2042 | just converting one integral constant expression to another. */ | |
2043 | return digest_init (type, expr, (tree*) 0); | |
2044 | ||
abff8e06 JM |
2045 | case REAL_TYPE: |
2046 | case COMPLEX_TYPE: | |
2047 | /* These are g++ extensions. */ | |
2048 | if (TREE_CODE (expr_type) != TREE_CODE (type)) | |
2049 | return error_mark_node; | |
2050 | ||
2051 | return digest_init (type, expr, (tree*) 0); | |
2052 | ||
75650646 MM |
2053 | case POINTER_TYPE: |
2054 | { | |
2055 | tree type_pointed_to = TREE_TYPE (type); | |
2056 | ||
2057 | if (TYPE_PTRMEM_P (type)) | |
2058 | /* For a non-type template-parameter of type pointer to data | |
2059 | member, qualification conversions (_conv.qual_) are | |
2060 | applied. */ | |
2061 | return perform_qualification_conversions (type, expr); | |
2062 | else if (TREE_CODE (type_pointed_to) == FUNCTION_TYPE) | |
2063 | { | |
2064 | /* For a non-type template-parameter of type pointer to | |
2065 | function, only the function-to-pointer conversion | |
2066 | (_conv.func_) is applied. If the template-argument | |
2067 | represents a set of overloaded functions (or a pointer to | |
2068 | such), the matching function is selected from the set | |
2069 | (_over.over_). */ | |
2070 | tree fns; | |
2071 | tree fn; | |
2072 | ||
7bf2682f | 2073 | if (TREE_CODE (expr) == ADDR_EXPR) |
75650646 MM |
2074 | fns = TREE_OPERAND (expr, 0); |
2075 | else | |
2076 | fns = expr; | |
2077 | ||
e1467ff2 | 2078 | fn = instantiate_type (type_pointed_to, fns, 0); |
75650646 MM |
2079 | |
2080 | if (fn == error_mark_node) | |
2081 | return error_mark_node; | |
2082 | ||
2083 | if (!TREE_PUBLIC (fn)) | |
2084 | { | |
2085 | if (really_overloaded_fn (fns)) | |
2086 | return error_mark_node; | |
2087 | else | |
2088 | goto bad_argument; | |
2089 | } | |
2090 | ||
2091 | expr = build_unary_op (ADDR_EXPR, fn, 0); | |
2092 | ||
2093 | my_friendly_assert (comptypes (type, TREE_TYPE (expr), 1), | |
2094 | 0); | |
2095 | return expr; | |
2096 | } | |
2097 | else | |
2098 | { | |
2099 | /* For a non-type template-parameter of type pointer to | |
2100 | object, qualification conversions (_conv.qual_) and the | |
2101 | array-to-pointer conversion (_conv.array_) are applied. | |
2102 | [Note: In particular, neither the null pointer conversion | |
2103 | (_conv.ptr_) nor the derived-to-base conversion | |
2104 | (_conv.ptr_) are applied. Although 0 is a valid | |
2105 | template-argument for a non-type template-parameter of | |
2106 | integral type, it is not a valid template-argument for a | |
e1467ff2 MM |
2107 | non-type template-parameter of pointer type.] |
2108 | ||
2109 | The call to decay_conversion performs the | |
2110 | array-to-pointer conversion, if appropriate. */ | |
2111 | expr = decay_conversion (expr); | |
75650646 MM |
2112 | |
2113 | if (expr == error_mark_node) | |
2114 | return error_mark_node; | |
2115 | else | |
2116 | return perform_qualification_conversions (type, expr); | |
2117 | } | |
2118 | } | |
2119 | break; | |
2120 | ||
2121 | case REFERENCE_TYPE: | |
2122 | { | |
2123 | tree type_referred_to = TREE_TYPE (type); | |
2124 | ||
2125 | if (TREE_CODE (type_referred_to) == FUNCTION_TYPE) | |
2126 | { | |
2127 | /* For a non-type template-parameter of type reference to | |
2128 | function, no conversions apply. If the | |
2129 | template-argument represents a set of overloaded | |
2130 | functions, the matching function is selected from the | |
2131 | set (_over.over_). */ | |
2132 | tree fns = expr; | |
2133 | tree fn; | |
2134 | ||
e1467ff2 | 2135 | fn = instantiate_type (type_referred_to, fns, 0); |
75650646 MM |
2136 | |
2137 | if (!TREE_PUBLIC (fn)) | |
2138 | { | |
2139 | if (really_overloaded_fn (fns)) | |
2140 | /* Don't issue an error here; we might get a different | |
2141 | function if the overloading had worked out | |
2142 | differently. */ | |
2143 | return error_mark_node; | |
2144 | else | |
2145 | goto bad_argument; | |
2146 | } | |
2147 | ||
2148 | if (fn == error_mark_node) | |
2149 | return error_mark_node; | |
2150 | ||
2151 | my_friendly_assert (comptypes (type, TREE_TYPE (fn), 1), | |
2152 | 0); | |
2153 | ||
2154 | return fn; | |
2155 | } | |
2156 | else | |
2157 | { | |
2158 | /* For a non-type template-parameter of type reference to | |
2159 | object, no conversions apply. The type referred to by the | |
2160 | reference may be more cv-qualified than the (otherwise | |
2161 | identical) type of the template-argument. The | |
2162 | template-parameter is bound directly to the | |
2163 | template-argument, which must be an lvalue. */ | |
2164 | if (!comptypes (TYPE_MAIN_VARIANT (expr_type), | |
2165 | TYPE_MAIN_VARIANT (type), 1) | |
2166 | || (TYPE_READONLY (expr_type) > | |
2167 | TYPE_READONLY (type_referred_to)) | |
2168 | || (TYPE_VOLATILE (expr_type) > | |
2169 | TYPE_VOLATILE (type_referred_to)) | |
2170 | || !real_lvalue_p (expr)) | |
2171 | return error_mark_node; | |
2172 | else | |
2173 | return expr; | |
2174 | } | |
2175 | } | |
2176 | break; | |
2177 | ||
2178 | case RECORD_TYPE: | |
2179 | { | |
2180 | tree fns; | |
2181 | tree fn; | |
2182 | ||
2183 | my_friendly_assert (TYPE_PTRMEMFUNC_P (type), 0); | |
2184 | ||
2185 | /* For a non-type template-parameter of type pointer to member | |
2186 | function, no conversions apply. If the template-argument | |
2187 | represents a set of overloaded member functions, the | |
2188 | matching member function is selected from the set | |
2189 | (_over.over_). */ | |
2190 | ||
2191 | if (!TYPE_PTRMEMFUNC_P (expr_type) && | |
2192 | expr_type != unknown_type_node) | |
2193 | return error_mark_node; | |
2194 | ||
2195 | if (TREE_CODE (expr) == CONSTRUCTOR) | |
2196 | { | |
2197 | /* A ptr-to-member constant. */ | |
2198 | if (!comptypes (type, expr_type, 1)) | |
2199 | return error_mark_node; | |
2200 | else | |
2201 | return expr; | |
2202 | } | |
2203 | ||
2204 | if (TREE_CODE (expr) != ADDR_EXPR) | |
2205 | return error_mark_node; | |
2206 | ||
2207 | fns = TREE_OPERAND (expr, 0); | |
2208 | ||
e1467ff2 MM |
2209 | fn = instantiate_type (TREE_TYPE (TREE_TYPE (type)), |
2210 | fns, 0); | |
75650646 MM |
2211 | |
2212 | if (fn == error_mark_node) | |
2213 | return error_mark_node; | |
2214 | ||
2215 | expr = build_unary_op (ADDR_EXPR, fn, 0); | |
2216 | ||
2217 | my_friendly_assert (comptypes (type, TREE_TYPE (expr), 1), | |
2218 | 0); | |
2219 | return expr; | |
2220 | } | |
2221 | break; | |
2222 | ||
2223 | default: | |
2224 | /* All non-type parameters must have one of these types. */ | |
2225 | my_friendly_abort (0); | |
2226 | break; | |
2227 | } | |
2228 | ||
2229 | return error_mark_node; | |
2230 | } | |
2231 | ||
8d08fdba MS |
2232 | /* Convert all template arguments to their appropriate types, and return |
2233 | a vector containing the resulting values. If any error occurs, return | |
75650646 MM |
2234 | error_mark_node, and, if COMPLAIN is non-zero, issue an error message. |
2235 | Some error messages are issued even if COMPLAIN is zero; for | |
2236 | instance, if a template argument is composed from a local class. | |
2237 | ||
2238 | If REQUIRE_ALL_ARGUMENTS is non-zero, all arguments must be | |
2239 | provided in ARGLIST, or else trailing parameters must have default | |
2240 | values. If REQUIRE_ALL_ARGUMENTS is zero, we will attempt argument | |
de575009 KL |
2241 | deduction for any unspecified trailing arguments. |
2242 | ||
2243 | If IS_TMPL_PARM is non-zero, we will coercing parameters of template | |
2244 | template arguments. In this case, ARGLIST is a chain of TREE_LIST | |
2245 | nodes containing TYPE_DECL, TEMPLATE_DECL or PARM_DECL. */ | |
e92cc029 | 2246 | |
8d08fdba | 2247 | static tree |
75650646 MM |
2248 | coerce_template_parms (parms, arglist, in_decl, |
2249 | complain, | |
de575009 KL |
2250 | require_all_arguments, |
2251 | is_tmpl_parm) | |
8d08fdba MS |
2252 | tree parms, arglist; |
2253 | tree in_decl; | |
75650646 MM |
2254 | int complain; |
2255 | int require_all_arguments; | |
de575009 | 2256 | int is_tmpl_parm; |
8d08fdba | 2257 | { |
a292b002 | 2258 | int nparms, nargs, i, lost = 0; |
75650646 | 2259 | tree vec = NULL_TREE; |
8d08fdba | 2260 | |
a292b002 MS |
2261 | if (arglist == NULL_TREE) |
2262 | nargs = 0; | |
2263 | else if (TREE_CODE (arglist) == TREE_VEC) | |
2264 | nargs = TREE_VEC_LENGTH (arglist); | |
8d08fdba | 2265 | else |
a292b002 MS |
2266 | nargs = list_length (arglist); |
2267 | ||
2268 | nparms = TREE_VEC_LENGTH (parms); | |
2269 | ||
2270 | if (nargs > nparms | |
2271 | || (nargs < nparms | |
75650646 | 2272 | && require_all_arguments |
a292b002 | 2273 | && TREE_PURPOSE (TREE_VEC_ELT (parms, nargs)) == NULL_TREE)) |
8d08fdba | 2274 | { |
75650646 MM |
2275 | if (complain) |
2276 | { | |
2277 | error ("incorrect number of parameters (%d, should be %d)", | |
2278 | nargs, nparms); | |
2279 | ||
2280 | if (in_decl) | |
2281 | cp_error_at ("in template expansion for decl `%D'", | |
2282 | in_decl); | |
2283 | } | |
2284 | ||
8d08fdba MS |
2285 | return error_mark_node; |
2286 | } | |
2287 | ||
e7e66632 KL |
2288 | if (arglist && TREE_CODE (arglist) == TREE_VEC && nargs == nparms) |
2289 | vec = copy_node (arglist); | |
8d08fdba MS |
2290 | else |
2291 | { | |
2292 | vec = make_tree_vec (nparms); | |
75650646 | 2293 | |
8d08fdba MS |
2294 | for (i = 0; i < nparms; i++) |
2295 | { | |
a292b002 | 2296 | tree arg; |
75650646 | 2297 | tree parm = TREE_VEC_ELT (parms, i); |
a292b002 MS |
2298 | |
2299 | if (arglist) | |
2300 | { | |
2301 | arg = arglist; | |
2302 | arglist = TREE_CHAIN (arglist); | |
2303 | ||
2304 | if (arg == error_mark_node) | |
2305 | lost++; | |
2306 | else | |
2307 | arg = TREE_VALUE (arg); | |
2308 | } | |
e7e66632 KL |
2309 | else if (is_tmpl_parm && i < nargs) |
2310 | { | |
2311 | arg = TREE_VEC_ELT (arglist, i); | |
2312 | if (arg == error_mark_node) | |
2313 | lost++; | |
2314 | } | |
75650646 MM |
2315 | else if (TREE_PURPOSE (parm) == NULL_TREE) |
2316 | { | |
2317 | my_friendly_assert (!require_all_arguments, 0); | |
2318 | break; | |
2319 | } | |
2320 | else if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL) | |
f7d98d58 | 2321 | arg = tsubst (TREE_PURPOSE (parm), vec, in_decl); |
d2e5ee5c | 2322 | else |
f7d98d58 | 2323 | arg = tsubst_expr (TREE_PURPOSE (parm), vec, in_decl); |
a292b002 | 2324 | |
8d08fdba MS |
2325 | TREE_VEC_ELT (vec, i) = arg; |
2326 | } | |
2327 | } | |
2328 | for (i = 0; i < nparms; i++) | |
2329 | { | |
2330 | tree arg = TREE_VEC_ELT (vec, i); | |
a292b002 | 2331 | tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i)); |
8d08fdba | 2332 | tree val = 0; |
73b0fce8 | 2333 | int is_type, requires_type, is_tmpl_type, requires_tmpl_type; |
8d08fdba | 2334 | |
de575009 KL |
2335 | if (is_tmpl_parm && i < nargs) |
2336 | { | |
2337 | /* In case we are checking arguments inside a template template | |
2338 | parameter, ARG that does not come from default argument is | |
2339 | also a TREE_LIST node. Note that ARG can also be a TREE_LIST | |
2340 | in other cases such as overloaded functions. */ | |
2341 | if (arg != NULL_TREE && arg != error_mark_node) | |
2342 | arg = TREE_VALUE (arg); | |
2343 | } | |
2344 | ||
75650646 MM |
2345 | if (arg == NULL_TREE) |
2346 | /* We're out of arguments. */ | |
2347 | { | |
2348 | my_friendly_assert (!require_all_arguments, 0); | |
2349 | break; | |
2350 | } | |
2351 | ||
2352 | if (arg == error_mark_node) | |
2353 | { | |
2354 | cp_error ("template argument %d is invalid", i + 1); | |
2355 | lost++; | |
2356 | continue; | |
2357 | } | |
2358 | ||
8857f91e MM |
2359 | if (TREE_CODE (arg) == TREE_LIST |
2360 | && TREE_TYPE (arg) != NULL_TREE | |
2361 | && TREE_CODE (TREE_TYPE (arg)) == OFFSET_TYPE) | |
2362 | { | |
2363 | /* The template argument was the name of some | |
2364 | member function. That's usually | |
2365 | illegal, but static members are OK. In any | |
2366 | case, grab the underlying fields/functions | |
2367 | and issue an error later if required. */ | |
2368 | arg = TREE_VALUE (arg); | |
2369 | TREE_TYPE (arg) = unknown_type_node; | |
2370 | } | |
73b0fce8 | 2371 | |
e7e66632 KL |
2372 | requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL; |
2373 | requires_type = TREE_CODE (parm) == TYPE_DECL | |
2374 | || requires_tmpl_type; | |
2375 | ||
2376 | /* Check if it is a class template. If REQUIRES_TMPL_TYPE is true, | |
2377 | we also accept implicitly created TYPE_DECL as a valid argument. */ | |
73b0fce8 KL |
2378 | is_tmpl_type = (TREE_CODE (arg) == TEMPLATE_DECL |
2379 | && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL) | |
2380 | || (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM | |
e7e66632 KL |
2381 | && !CLASSTYPE_TEMPLATE_INFO (arg)) |
2382 | || (TREE_CODE (arg) == RECORD_TYPE | |
2383 | && CLASSTYPE_TEMPLATE_INFO (arg) | |
2384 | && TREE_CODE (TYPE_NAME (arg)) == TYPE_DECL | |
2385 | && DECL_ARTIFICIAL (TYPE_NAME (arg)) | |
2386 | && requires_tmpl_type); | |
73b0fce8 KL |
2387 | if (is_tmpl_type && TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM) |
2388 | arg = TYPE_STUB_DECL (arg); | |
e7e66632 KL |
2389 | else if (is_tmpl_type && TREE_CODE (arg) == RECORD_TYPE) |
2390 | arg = CLASSTYPE_TI_TEMPLATE (arg); | |
de575009 KL |
2391 | |
2392 | if (is_tmpl_parm && i < nargs) | |
2393 | is_type = TREE_CODE (arg) == TYPE_DECL || is_tmpl_type; | |
2394 | else | |
2395 | is_type = TREE_CODE_CLASS (TREE_CODE (arg)) == 't' || is_tmpl_type; | |
5566b478 MS |
2396 | |
2397 | if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF | |
2398 | && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM) | |
2399 | { | |
2400 | cp_pedwarn ("to refer to a type member of a template parameter,"); | |
2401 | cp_pedwarn (" use `typename %E'", arg); | |
75650646 | 2402 | |
5566b478 MS |
2403 | arg = make_typename_type (TREE_OPERAND (arg, 0), |
2404 | TREE_OPERAND (arg, 1)); | |
2405 | is_type = 1; | |
2406 | } | |
8d08fdba MS |
2407 | if (is_type != requires_type) |
2408 | { | |
2409 | if (in_decl) | |
5566b478 | 2410 | { |
75650646 MM |
2411 | if (complain) |
2412 | { | |
2413 | cp_error ("type/value mismatch at argument %d in template parameter list for `%D'", | |
2414 | i + 1, in_decl); | |
2415 | if (is_type) | |
2416 | cp_error (" expected a constant of type `%T', got `%T'", | |
73b0fce8 KL |
2417 | TREE_TYPE (parm), |
2418 | (is_tmpl_type ? DECL_NAME (arg) : arg)); | |
75650646 MM |
2419 | else |
2420 | cp_error (" expected a type, got `%E'", arg); | |
2421 | } | |
5566b478 | 2422 | } |
8d08fdba MS |
2423 | lost++; |
2424 | TREE_VEC_ELT (vec, i) = error_mark_node; | |
2425 | continue; | |
2426 | } | |
73b0fce8 KL |
2427 | if (is_tmpl_type ^ requires_tmpl_type) |
2428 | { | |
2429 | if (in_decl) | |
2430 | { | |
2431 | cp_error ("type/value mismatch at argument %d in template parameter list for `%D'", | |
e7e66632 | 2432 | i + 1, in_decl); |
73b0fce8 KL |
2433 | if (is_tmpl_type) |
2434 | cp_error (" expected a type, got `%T'", DECL_NAME (arg)); | |
2435 | else | |
2436 | cp_error (" expected a class template, got `%T'", arg); | |
2437 | } | |
2438 | lost++; | |
2439 | TREE_VEC_ELT (vec, i) = error_mark_node; | |
2440 | continue; | |
2441 | } | |
2442 | if (is_tmpl_parm) | |
2443 | { | |
2444 | if (requires_tmpl_type) | |
2445 | { | |
2446 | cp_error ("nested template template parameter not implemented"); | |
2447 | lost++; | |
2448 | TREE_VEC_ELT (vec, i) = error_mark_node; | |
2449 | } | |
2450 | continue; | |
2451 | } | |
2452 | ||
8d08fdba | 2453 | if (is_type) |
ec255269 | 2454 | { |
73b0fce8 KL |
2455 | if (requires_tmpl_type) |
2456 | { | |
2457 | tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm); | |
2458 | tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg); | |
2459 | ||
2460 | /* The parameter and argument roles have to be switched | |
2461 | here in order to handle default arguments properly. | |
2462 | For example, | |
2463 | template<template <class> class TT> void f(TT<int>) | |
2464 | should be able to accept vector<int> which comes from | |
2465 | template <class T, class Allcator = allocator> | |
2466 | class vector. */ | |
2467 | ||
de575009 | 2468 | val = coerce_template_parms (argparm, parmparm, in_decl, 1, 1, 1); |
73b0fce8 KL |
2469 | if (val != error_mark_node) |
2470 | val = arg; | |
2471 | ||
2472 | /* TEMPLATE_TEMPLATE_PARM node is preferred over | |
2473 | TEMPLATE_DECL. */ | |
2474 | if (val != error_mark_node | |
2475 | && DECL_TEMPLATE_TEMPLATE_PARM_P (val)) | |
2476 | val = TREE_TYPE (val); | |
2477 | } | |
2478 | else | |
ec255269 | 2479 | { |
73b0fce8 KL |
2480 | val = groktypename (arg); |
2481 | if (! processing_template_decl) | |
ec255269 | 2482 | { |
73b0fce8 KL |
2483 | tree t = target_type (val); |
2484 | if (TREE_CODE (t) != TYPENAME_TYPE | |
2485 | && IS_AGGR_TYPE (t) | |
2486 | && decl_function_context (TYPE_MAIN_DECL (t))) | |
2487 | { | |
2488 | cp_error ("type `%T' composed from a local class is not a valid template-argument", | |
2489 | val); | |
2490 | return error_mark_node; | |
2491 | } | |
ec255269 MS |
2492 | } |
2493 | } | |
2494 | } | |
8d08fdba MS |
2495 | else |
2496 | { | |
f7d98d58 | 2497 | tree t = tsubst (TREE_TYPE (parm), vec, in_decl); |
75650646 | 2498 | |
4bfc4dda | 2499 | if (processing_template_decl) |
75650646 MM |
2500 | arg = maybe_fold_nontype_arg (arg); |
2501 | ||
2502 | if (!uses_template_parms (arg) && !uses_template_parms (t)) | |
2503 | /* We used to call digest_init here. However, digest_init | |
2504 | will report errors, which we don't want when complain | |
2505 | is zero. More importantly, digest_init will try too | |
2506 | hard to convert things: for example, `0' should not be | |
2507 | converted to pointer type at this point according to | |
2508 | the standard. Accepting this is not merely an | |
2509 | extension, since deciding whether or not these | |
2510 | conversions can occur is part of determining which | |
2511 | function template to call, or whether a given epxlicit | |
2512 | argument specification is legal. */ | |
e1467ff2 | 2513 | val = convert_nontype_argument (t, arg); |
5566b478 | 2514 | else |
6ba4439c MM |
2515 | val = arg; |
2516 | ||
75650646 MM |
2517 | if (val == NULL_TREE) |
2518 | val = error_mark_node; | |
2519 | else if (val == error_mark_node && complain) | |
2520 | cp_error ("could not convert template argument `%E' to `%T'", | |
2521 | arg, t); | |
8d08fdba MS |
2522 | } |
2523 | ||
2524 | if (val == error_mark_node) | |
2525 | lost++; | |
2526 | ||
2527 | TREE_VEC_ELT (vec, i) = val; | |
2528 | } | |
2529 | if (lost) | |
2530 | return error_mark_node; | |
2531 | return vec; | |
2532 | } | |
2533 | ||
f84b4be9 JM |
2534 | /* Renturns 1 iff the OLDARGS and NEWARGS are in fact identical sets |
2535 | of template arguments. Returns 0 otherwise. */ | |
2536 | ||
6757edfe | 2537 | int |
5566b478 MS |
2538 | comp_template_args (oldargs, newargs) |
2539 | tree oldargs, newargs; | |
2540 | { | |
2541 | int i; | |
2542 | ||
386b8a85 JM |
2543 | if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs)) |
2544 | return 0; | |
2545 | ||
5566b478 MS |
2546 | for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i) |
2547 | { | |
2548 | tree nt = TREE_VEC_ELT (newargs, i); | |
2549 | tree ot = TREE_VEC_ELT (oldargs, i); | |
2550 | ||
2551 | if (nt == ot) | |
2552 | continue; | |
2553 | if (TREE_CODE (nt) != TREE_CODE (ot)) | |
2554 | return 0; | |
00d3396f JM |
2555 | if (TREE_CODE (nt) == TREE_VEC) |
2556 | { | |
2557 | /* For member templates */ | |
2558 | if (comp_template_args (nt, ot)) | |
2559 | continue; | |
2560 | } | |
2561 | else if (TREE_CODE_CLASS (TREE_CODE (ot)) == 't') | |
67d743fe MS |
2562 | { |
2563 | if (comptypes (ot, nt, 1)) | |
2564 | continue; | |
2565 | } | |
2566 | else if (cp_tree_equal (ot, nt) > 0) | |
5566b478 MS |
2567 | continue; |
2568 | return 0; | |
2569 | } | |
2570 | return 1; | |
2571 | } | |
2572 | ||
8d08fdba MS |
2573 | /* Given class template name and parameter list, produce a user-friendly name |
2574 | for the instantiation. */ | |
e92cc029 | 2575 | |
8d08fdba | 2576 | static char * |
75650646 | 2577 | mangle_class_name_for_template (name, parms, arglist, ctx) |
8d08fdba MS |
2578 | char *name; |
2579 | tree parms, arglist; | |
75650646 | 2580 | tree ctx; |
8d08fdba MS |
2581 | { |
2582 | static struct obstack scratch_obstack; | |
2583 | static char *scratch_firstobj; | |
2584 | int i, nparms; | |
8d08fdba MS |
2585 | |
2586 | if (!scratch_firstobj) | |
fc378698 | 2587 | gcc_obstack_init (&scratch_obstack); |
8d08fdba MS |
2588 | else |
2589 | obstack_free (&scratch_obstack, scratch_firstobj); | |
fc378698 | 2590 | scratch_firstobj = obstack_alloc (&scratch_obstack, 1); |
8d08fdba MS |
2591 | |
2592 | #if 0 | |
2593 | #define buflen sizeof(buf) | |
2594 | #define check if (bufp >= buf+buflen-1) goto too_long | |
2595 | #define ccat(c) *bufp++=(c); check | |
2596 | #define advance bufp+=strlen(bufp); check | |
2597 | #define cat(s) strncpy(bufp, s, buf+buflen-bufp-1); advance | |
2598 | #else | |
2599 | #define check | |
2600 | #define ccat(c) obstack_1grow (&scratch_obstack, (c)); | |
2601 | #define advance | |
2602 | #define cat(s) obstack_grow (&scratch_obstack, (s), strlen (s)) | |
2603 | #endif | |
8d08fdba | 2604 | |
75650646 MM |
2605 | if (ctx) |
2606 | { | |
e1467ff2 MM |
2607 | char* s; |
2608 | ||
2609 | if (TREE_CODE (ctx) == FUNCTION_DECL) | |
3a56f0ab | 2610 | s = fndecl_as_string (ctx, 0); |
e1467ff2 | 2611 | else if (TREE_CODE_CLASS (TREE_CODE (ctx)) == 't') |
294471c3 | 2612 | s = type_as_string_real (ctx, 0, 1); |
e1467ff2 MM |
2613 | else |
2614 | my_friendly_abort (0); | |
75650646 MM |
2615 | cat (s); |
2616 | cat ("::"); | |
2617 | } | |
8d08fdba MS |
2618 | cat (name); |
2619 | ccat ('<'); | |
2620 | nparms = TREE_VEC_LENGTH (parms); | |
2621 | my_friendly_assert (nparms == TREE_VEC_LENGTH (arglist), 268); | |
2622 | for (i = 0; i < nparms; i++) | |
2623 | { | |
a292b002 MS |
2624 | tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i)); |
2625 | tree arg = TREE_VEC_ELT (arglist, i); | |
8d08fdba MS |
2626 | |
2627 | if (i) | |
2628 | ccat (','); | |
2629 | ||
a292b002 | 2630 | if (TREE_CODE (parm) == TYPE_DECL) |
8d08fdba | 2631 | { |
294471c3 | 2632 | cat (type_as_string_real (arg, 0, 1)); |
8d08fdba MS |
2633 | continue; |
2634 | } | |
73b0fce8 KL |
2635 | else if (TREE_CODE (parm) == TEMPLATE_DECL) |
2636 | { | |
2637 | if (TREE_CODE (arg) == TEMPLATE_DECL) | |
2638 | /* Already substituted with real template. Just output | |
2639 | the template name here */ | |
2640 | cat (IDENTIFIER_POINTER (DECL_NAME (arg))); | |
2641 | else | |
2642 | /* Output the parameter declaration */ | |
294471c3 | 2643 | cat (type_as_string_real (arg, 0, 1)); |
73b0fce8 KL |
2644 | continue; |
2645 | } | |
8d08fdba MS |
2646 | else |
2647 | my_friendly_assert (TREE_CODE (parm) == PARM_DECL, 269); | |
2648 | ||
2649 | if (TREE_CODE (arg) == TREE_LIST) | |
2650 | { | |
2651 | /* New list cell was built because old chain link was in | |
2652 | use. */ | |
2653 | my_friendly_assert (TREE_PURPOSE (arg) == NULL_TREE, 270); | |
2654 | arg = TREE_VALUE (arg); | |
2655 | } | |
2656 | /* No need to check arglist against parmlist here; we did that | |
2657 | in coerce_template_parms, called from lookup_template_class. */ | |
2658 | cat (expr_as_string (arg, 0)); | |
2659 | } | |
2660 | { | |
2661 | char *bufp = obstack_next_free (&scratch_obstack); | |
2662 | int offset = 0; | |
2663 | while (bufp[offset - 1] == ' ') | |
2664 | offset--; | |
2665 | obstack_blank_fast (&scratch_obstack, offset); | |
2666 | ||
2667 | /* B<C<char> >, not B<C<char>> */ | |
2668 | if (bufp[offset - 1] == '>') | |
2669 | ccat (' '); | |
2670 | } | |
2671 | ccat ('>'); | |
2672 | ccat ('\0'); | |
2673 | return (char *) obstack_base (&scratch_obstack); | |
2674 | ||
8926095f | 2675 | #if 0 |
8d08fdba | 2676 | too_long: |
8926095f | 2677 | #endif |
8d08fdba MS |
2678 | fatal ("out of (preallocated) string space creating template instantiation name"); |
2679 | /* NOTREACHED */ | |
2680 | return NULL; | |
2681 | } | |
2682 | ||
bd6dd845 | 2683 | static tree |
5566b478 MS |
2684 | classtype_mangled_name (t) |
2685 | tree t; | |
2686 | { | |
2687 | if (CLASSTYPE_TEMPLATE_INFO (t) | |
2688 | && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t))) | |
2689 | { | |
2690 | tree name = DECL_NAME (CLASSTYPE_TI_TEMPLATE (t)); | |
2691 | char *mangled_name = mangle_class_name_for_template | |
2692 | (IDENTIFIER_POINTER (name), | |
98c1c668 | 2693 | DECL_INNERMOST_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (t)), |
e1467ff2 | 2694 | CLASSTYPE_TI_ARGS (t), DECL_CONTEXT (t)); |
5566b478 MS |
2695 | tree id = get_identifier (mangled_name); |
2696 | IDENTIFIER_TEMPLATE (id) = name; | |
2697 | return id; | |
2698 | } | |
2699 | else | |
2700 | return TYPE_IDENTIFIER (t); | |
2701 | } | |
2702 | ||
2703 | static void | |
2704 | add_pending_template (d) | |
2705 | tree d; | |
2706 | { | |
e92cc029 MS |
2707 | tree ti; |
2708 | ||
2709 | if (TREE_CODE_CLASS (TREE_CODE (d)) == 't') | |
2710 | ti = CLASSTYPE_TEMPLATE_INFO (d); | |
2711 | else | |
2712 | ti = DECL_TEMPLATE_INFO (d); | |
2713 | ||
824b9a4c | 2714 | if (TI_PENDING_TEMPLATE_FLAG (ti)) |
5566b478 MS |
2715 | return; |
2716 | ||
2717 | *template_tail = perm_tree_cons | |
2718 | (current_function_decl, d, NULL_TREE); | |
2719 | template_tail = &TREE_CHAIN (*template_tail); | |
824b9a4c | 2720 | TI_PENDING_TEMPLATE_FLAG (ti) = 1; |
5566b478 MS |
2721 | } |
2722 | ||
386b8a85 JM |
2723 | |
2724 | /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS (which | |
2725 | may be either a _DECL or an overloaded function or an | |
2726 | IDENTIFIER_NODE), and ARGLIST. */ | |
2727 | ||
2728 | tree | |
2729 | lookup_template_function (fns, arglist) | |
2730 | tree fns, arglist; | |
2731 | { | |
050367a3 MM |
2732 | tree t; |
2733 | ||
386b8a85 JM |
2734 | if (fns == NULL_TREE) |
2735 | { | |
2736 | cp_error ("non-template used as template"); | |
2737 | return error_mark_node; | |
2738 | } | |
2739 | ||
2740 | if (arglist != NULL_TREE && !TREE_PERMANENT (arglist)) | |
4de02abd | 2741 | copy_to_permanent (arglist); |
386b8a85 JM |
2742 | |
2743 | return build_min (TEMPLATE_ID_EXPR, | |
2744 | TREE_TYPE (fns) | |
2745 | ? TREE_TYPE (fns) : unknown_type_node, | |
2746 | fns, arglist); | |
2747 | } | |
2748 | ||
a2b60a0e MM |
2749 | /* Within the scope of a template class S<T>, the name S gets bound |
2750 | (in build_self_reference) to a TYPE_DECL for the class, not a | |
2751 | TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type, | |
2752 | or one of its enclosing classes, and that type is a template, | |
2753 | return the associated TEMPLATE_DECL. Otherwise, the original | |
2754 | DECL is returned. */ | |
2755 | ||
2756 | tree | |
2757 | maybe_get_template_decl_from_type_decl (decl) | |
2758 | tree decl; | |
2759 | { | |
2760 | return (decl != NULL_TREE | |
2761 | && TREE_CODE (decl) == TYPE_DECL | |
2762 | && DECL_ARTIFICIAL (decl) | |
2763 | && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl))) | |
2764 | ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl; | |
2765 | } | |
386b8a85 | 2766 | |
8d08fdba MS |
2767 | /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of |
2768 | parameters, find the desired type. | |
2769 | ||
2770 | D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments. | |
2771 | Since ARGLIST is build on the decl_obstack, we must copy it here | |
2772 | to keep it from being reclaimed when the decl storage is reclaimed. | |
2773 | ||
2774 | IN_DECL, if non-NULL, is the template declaration we are trying to | |
75650646 MM |
2775 | instantiate. |
2776 | ||
2777 | If the template class is really a local class in a template | |
2778 | function, then the FUNCTION_CONTEXT is the function in which it is | |
2779 | being instantiated. */ | |
e92cc029 | 2780 | |
8d08fdba | 2781 | tree |
e1467ff2 | 2782 | lookup_template_class (d1, arglist, in_decl, context) |
8d08fdba MS |
2783 | tree d1, arglist; |
2784 | tree in_decl; | |
e1467ff2 | 2785 | tree context; |
8d08fdba | 2786 | { |
a703fb38 | 2787 | tree template = NULL_TREE, parmlist; |
8d08fdba | 2788 | char *mangled_name; |
5566b478 | 2789 | tree id, t; |
5566b478 MS |
2790 | |
2791 | if (TREE_CODE (d1) == IDENTIFIER_NODE) | |
2792 | { | |
73b0fce8 KL |
2793 | if (IDENTIFIER_LOCAL_VALUE (d1) |
2794 | && DECL_TEMPLATE_TEMPLATE_PARM_P (IDENTIFIER_LOCAL_VALUE (d1))) | |
2795 | template = IDENTIFIER_LOCAL_VALUE (d1); | |
2796 | else | |
2797 | { | |
a2b60a0e MM |
2798 | template = |
2799 | maybe_get_template_decl_from_type_decl | |
2800 | (IDENTIFIER_CLASS_VALUE (d1)); | |
2801 | if (template == NULL_TREE) | |
2802 | template = IDENTIFIER_NAMESPACE_VALUE (d1); | |
73b0fce8 | 2803 | } |
8d019cef JM |
2804 | if (template) |
2805 | context = DECL_CONTEXT (template); | |
5566b478 | 2806 | } |
c91a56d2 MS |
2807 | else if (TREE_CODE (d1) == TYPE_DECL && IS_AGGR_TYPE (TREE_TYPE (d1))) |
2808 | { | |
7bf2682f MM |
2809 | if (CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (d1)) == NULL_TREE) |
2810 | return error_mark_node; | |
c91a56d2 MS |
2811 | template = CLASSTYPE_TI_TEMPLATE (TREE_TYPE (d1)); |
2812 | d1 = DECL_NAME (template); | |
2813 | } | |
5566b478 MS |
2814 | else if (TREE_CODE_CLASS (TREE_CODE (d1)) == 't' && IS_AGGR_TYPE (d1)) |
2815 | { | |
2816 | template = CLASSTYPE_TI_TEMPLATE (d1); | |
2817 | d1 = DECL_NAME (template); | |
2818 | } | |
93cdc044 JM |
2819 | else if (TREE_CODE (d1) == TEMPLATE_DECL |
2820 | && TREE_CODE (DECL_RESULT (d1)) == TYPE_DECL) | |
2821 | { | |
2822 | template = d1; | |
2823 | d1 = DECL_NAME (template); | |
2824 | context = DECL_CONTEXT (template); | |
2825 | } | |
5566b478 MS |
2826 | else |
2827 | my_friendly_abort (272); | |
8d08fdba | 2828 | |
8d08fdba MS |
2829 | /* With something like `template <class T> class X class X { ... };' |
2830 | we could end up with D1 having nothing but an IDENTIFIER_LOCAL_VALUE. | |
2831 | We don't want to do that, but we have to deal with the situation, so | |
2832 | let's give them some syntax errors to chew on instead of a crash. */ | |
2833 | if (! template) | |
2834 | return error_mark_node; | |
2835 | if (TREE_CODE (template) != TEMPLATE_DECL) | |
2836 | { | |
2837 | cp_error ("non-template type `%T' used as a template", d1); | |
2838 | if (in_decl) | |
2839 | cp_error_at ("for template declaration `%D'", in_decl); | |
2840 | return error_mark_node; | |
2841 | } | |
8d08fdba | 2842 | |
73b0fce8 KL |
2843 | if (DECL_TEMPLATE_TEMPLATE_PARM_P (template)) |
2844 | { | |
2845 | /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store | |
2846 | template arguments */ | |
2847 | ||
2848 | tree parm = copy_template_template_parm (TREE_TYPE (template)); | |
2849 | tree template2 = TYPE_STUB_DECL (parm); | |
2850 | tree arglist2; | |
2851 | ||
2852 | CLASSTYPE_GOT_SEMICOLON (parm) = 1; | |
2853 | parmlist = DECL_INNERMOST_TEMPLATE_PARMS (template); | |
2854 | ||
de575009 | 2855 | arglist2 = coerce_template_parms (parmlist, arglist, template, 1, 1, 0); |
73b0fce8 KL |
2856 | if (arglist2 == error_mark_node) |
2857 | return error_mark_node; | |
2858 | ||
2859 | arglist2 = copy_to_permanent (arglist2); | |
2860 | CLASSTYPE_TEMPLATE_INFO (parm) | |
2861 | = perm_tree_cons (template2, arglist2, NULL_TREE); | |
2862 | TYPE_SIZE (parm) = 0; | |
2863 | return parm; | |
2864 | } | |
e1467ff2 MM |
2865 | else if (PRIMARY_TEMPLATE_P (template) |
2866 | || (TREE_CODE (TYPE_CONTEXT (TREE_TYPE (template))) | |
2867 | == FUNCTION_DECL)) | |
8d08fdba | 2868 | { |
98c1c668 | 2869 | parmlist = DECL_INNERMOST_TEMPLATE_PARMS (template); |
5566b478 | 2870 | |
75650646 | 2871 | arglist = coerce_template_parms (parmlist, arglist, template, |
de575009 | 2872 | 1, 1, 0); |
5566b478 MS |
2873 | if (arglist == error_mark_node) |
2874 | return error_mark_node; | |
2875 | if (uses_template_parms (arglist)) | |
2876 | { | |
2877 | tree found; | |
2878 | if (comp_template_args | |
2879 | (CLASSTYPE_TI_ARGS (TREE_TYPE (template)), arglist)) | |
2880 | found = TREE_TYPE (template); | |
2881 | else | |
2882 | { | |
2883 | for (found = DECL_TEMPLATE_INSTANTIATIONS (template); | |
2884 | found; found = TREE_CHAIN (found)) | |
2885 | { | |
2886 | if (TI_USES_TEMPLATE_PARMS (found) | |
2887 | && comp_template_args (TREE_PURPOSE (found), arglist)) | |
2888 | break; | |
2889 | } | |
2890 | if (found) | |
2891 | found = TREE_VALUE (found); | |
2892 | } | |
2893 | ||
2894 | if (found) | |
2895 | { | |
2896 | if (can_free (&permanent_obstack, arglist)) | |
2897 | obstack_free (&permanent_obstack, arglist); | |
2898 | return found; | |
2899 | } | |
2900 | } | |
2901 | ||
db897306 | 2902 | /* FIXME avoid duplication. */ |
8d08fdba | 2903 | mangled_name = mangle_class_name_for_template (IDENTIFIER_POINTER (d1), |
75650646 MM |
2904 | parmlist, |
2905 | arglist, | |
e1467ff2 | 2906 | context); |
8d08fdba | 2907 | id = get_identifier (mangled_name); |
5566b478 | 2908 | IDENTIFIER_TEMPLATE (id) = d1; |
8d08fdba | 2909 | |
5566b478 | 2910 | maybe_push_to_top_level (uses_template_parms (arglist)); |
fc378698 | 2911 | t = xref_tag_from_type (TREE_TYPE (template), id, 1); |
75650646 | 2912 | |
e1467ff2 | 2913 | if (context != NULL_TREE) |
75650646 MM |
2914 | { |
2915 | /* Set up the context for the type_decl correctly. Note | |
2916 | that we must clear DECL_ASSEMBLER_NAME to fool | |
2917 | build_overload_name into creating a new name. */ | |
2918 | tree type_decl = TYPE_STUB_DECL (t); | |
2919 | ||
e1467ff2 MM |
2920 | TYPE_CONTEXT (t) = context; |
2921 | DECL_CONTEXT (type_decl) = context; | |
75650646 MM |
2922 | DECL_ASSEMBLER_NAME (type_decl) = DECL_NAME (type_decl); |
2923 | DECL_ASSEMBLER_NAME (type_decl) = | |
2924 | get_identifier (build_overload_name (t, 1, 1)); | |
2925 | } | |
2926 | ||
5566b478 | 2927 | pop_from_top_level (); |
8926095f | 2928 | } |
5566b478 | 2929 | else |
8d08fdba | 2930 | { |
3ac3d9ea | 2931 | tree type_ctx = TYPE_CONTEXT (TREE_TYPE (template)); |
f7d98d58 | 2932 | tree args = tsubst (CLASSTYPE_TI_ARGS (type_ctx), arglist, in_decl); |
3ac3d9ea MM |
2933 | tree ctx = lookup_template_class (type_ctx, args, |
2934 | in_decl, NULL_TREE); | |
5566b478 MS |
2935 | id = d1; |
2936 | arglist = CLASSTYPE_TI_ARGS (ctx); | |
8d08fdba | 2937 | |
5566b478 | 2938 | if (TYPE_BEING_DEFINED (ctx) && ctx == current_class_type) |
8d08fdba | 2939 | { |
5156628f MS |
2940 | int save_temp = processing_template_decl; |
2941 | processing_template_decl = 0; | |
fc378698 | 2942 | t = xref_tag_from_type (TREE_TYPE (template), id, 0); |
5156628f | 2943 | processing_template_decl = save_temp; |
8d08fdba MS |
2944 | } |
2945 | else | |
2946 | { | |
5566b478 MS |
2947 | t = lookup_nested_type_by_name (ctx, id); |
2948 | my_friendly_assert (t != NULL_TREE, 42); | |
8d08fdba | 2949 | } |
5566b478 MS |
2950 | } |
2951 | ||
e92cc029 | 2952 | /* Seems to be wanted. */ |
5566b478 MS |
2953 | CLASSTYPE_GOT_SEMICOLON (t) = 1; |
2954 | ||
2955 | if (! CLASSTYPE_TEMPLATE_INFO (t)) | |
2956 | { | |
2957 | arglist = copy_to_permanent (arglist); | |
2958 | CLASSTYPE_TEMPLATE_INFO (t) | |
2959 | = perm_tree_cons (template, arglist, NULL_TREE); | |
2960 | DECL_TEMPLATE_INSTANTIATIONS (template) = perm_tree_cons | |
2961 | (arglist, t, DECL_TEMPLATE_INSTANTIATIONS (template)); | |
2962 | TI_USES_TEMPLATE_PARMS (DECL_TEMPLATE_INSTANTIATIONS (template)) | |
2963 | = uses_template_parms (arglist); | |
2964 | ||
2965 | SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t); | |
2966 | ||
e92cc029 | 2967 | /* We need to set this again after CLASSTYPE_TEMPLATE_INFO is set up. */ |
5566b478 | 2968 | DECL_ASSEMBLER_NAME (TYPE_MAIN_DECL (t)) = id; |
aa09da44 | 2969 | if (! uses_template_parms (arglist)) |
5566b478 MS |
2970 | DECL_ASSEMBLER_NAME (TYPE_MAIN_DECL (t)) |
2971 | = get_identifier (build_overload_name (t, 1, 1)); | |
2972 | ||
2973 | if (flag_external_templates && ! uses_template_parms (arglist) | |
2974 | && CLASSTYPE_INTERFACE_KNOWN (TREE_TYPE (template)) | |
2975 | && ! CLASSTYPE_INTERFACE_ONLY (TREE_TYPE (template))) | |
e92cc029 | 2976 | add_pending_template (t); |
8d08fdba | 2977 | } |
8d08fdba | 2978 | |
5566b478 | 2979 | return t; |
8d08fdba MS |
2980 | } |
2981 | \f | |
51c184be | 2982 | /* Should be defined in parse.h. */ |
8d08fdba MS |
2983 | extern int yychar; |
2984 | ||
050367a3 MM |
2985 | /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM, or |
2986 | TEMPLATE_PARM_INDEX in T, call FN with the parameter and the DATA. | |
2987 | If FN returns non-zero, the iteration is terminated, and | |
2988 | for_each_template_parm returns 1. Otherwise, the iteration | |
2989 | continues. If FN never returns a non-zero value, the value | |
2990 | returned by for_each_template_parm is 0. If FN is NULL, it is | |
2991 | considered to be the function which always returns 1. */ | |
2992 | ||
8d08fdba | 2993 | int |
050367a3 | 2994 | for_each_template_parm (t, fn, data) |
8d08fdba | 2995 | tree t; |
050367a3 MM |
2996 | tree_fn_t fn; |
2997 | void* data; | |
8d08fdba MS |
2998 | { |
2999 | if (!t) | |
3000 | return 0; | |
3001 | switch (TREE_CODE (t)) | |
3002 | { | |
3003 | case INDIRECT_REF: | |
3004 | case COMPONENT_REF: | |
3005 | /* We assume that the object must be instantiated in order to build | |
3006 | the COMPONENT_REF, so we test only whether the type of the | |
3007 | COMPONENT_REF uses template parms. */ | |
050367a3 | 3008 | return for_each_template_parm (TREE_TYPE (t), fn, data); |
8d08fdba MS |
3009 | |
3010 | case IDENTIFIER_NODE: | |
3011 | if (!IDENTIFIER_TEMPLATE (t)) | |
3012 | return 0; | |
5566b478 | 3013 | my_friendly_abort (42); |
8d08fdba MS |
3014 | |
3015 | /* aggregates of tree nodes */ | |
3016 | case TREE_VEC: | |
3017 | { | |
3018 | int i = TREE_VEC_LENGTH (t); | |
3019 | while (i--) | |
050367a3 | 3020 | if (for_each_template_parm (TREE_VEC_ELT (t, i), fn, data)) |
8d08fdba MS |
3021 | return 1; |
3022 | return 0; | |
3023 | } | |
3024 | case TREE_LIST: | |
050367a3 MM |
3025 | if (for_each_template_parm (TREE_PURPOSE (t), fn, data) |
3026 | || for_each_template_parm (TREE_VALUE (t), fn, data)) | |
8d08fdba | 3027 | return 1; |
050367a3 | 3028 | return for_each_template_parm (TREE_CHAIN (t), fn, data); |
8d08fdba MS |
3029 | |
3030 | /* constructed type nodes */ | |
3031 | case POINTER_TYPE: | |
3032 | case REFERENCE_TYPE: | |
050367a3 | 3033 | return for_each_template_parm (TREE_TYPE (t), fn, data); |
8d08fdba | 3034 | case RECORD_TYPE: |
b7484fbe | 3035 | if (TYPE_PTRMEMFUNC_FLAG (t)) |
050367a3 MM |
3036 | return for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE (t), |
3037 | fn, data); | |
8d08fdba | 3038 | case UNION_TYPE: |
5566b478 | 3039 | if (! CLASSTYPE_TEMPLATE_INFO (t)) |
8d08fdba | 3040 | return 0; |
050367a3 MM |
3041 | return for_each_template_parm (TREE_VALUE |
3042 | (CLASSTYPE_TEMPLATE_INFO (t)), | |
3043 | fn, data); | |
8d08fdba | 3044 | case FUNCTION_TYPE: |
050367a3 | 3045 | if (for_each_template_parm (TYPE_ARG_TYPES (t), fn, data)) |
8d08fdba | 3046 | return 1; |
050367a3 | 3047 | return for_each_template_parm (TREE_TYPE (t), fn, data); |
8d08fdba | 3048 | case ARRAY_TYPE: |
050367a3 | 3049 | if (for_each_template_parm (TYPE_DOMAIN (t), fn, data)) |
8d08fdba | 3050 | return 1; |
050367a3 | 3051 | return for_each_template_parm (TREE_TYPE (t), fn, data); |
8d08fdba | 3052 | case OFFSET_TYPE: |
050367a3 | 3053 | if (for_each_template_parm (TYPE_OFFSET_BASETYPE (t), fn, data)) |
8d08fdba | 3054 | return 1; |
050367a3 | 3055 | return for_each_template_parm (TREE_TYPE (t), fn, data); |
8d08fdba | 3056 | case METHOD_TYPE: |
050367a3 | 3057 | if (for_each_template_parm (TYPE_METHOD_BASETYPE (t), fn, data)) |
8d08fdba | 3058 | return 1; |
050367a3 | 3059 | if (for_each_template_parm (TYPE_ARG_TYPES (t), fn, data)) |
8d08fdba | 3060 | return 1; |
050367a3 | 3061 | return for_each_template_parm (TREE_TYPE (t), fn, data); |
8d08fdba MS |
3062 | |
3063 | /* decl nodes */ | |
3064 | case TYPE_DECL: | |
050367a3 | 3065 | return for_each_template_parm (TREE_TYPE (t), fn, data); |
5566b478 | 3066 | |
73b0fce8 KL |
3067 | case TEMPLATE_DECL: |
3068 | /* A template template parameter is encountered */ | |
3069 | if (DECL_TEMPLATE_TEMPLATE_PARM_P (t)) | |
3070 | /* We are parsing a template declaration */ | |
3071 | return 1; | |
3072 | /* We are instantiating templates with template template | |
3073 | parameter */ | |
3074 | return 0; | |
3075 | ||
3ac3d9ea | 3076 | case CONST_DECL: |
050367a3 | 3077 | if (for_each_template_parm (DECL_INITIAL (t), fn, data)) |
3ac3d9ea MM |
3078 | return 1; |
3079 | goto check_type_and_context; | |
3080 | ||
8d08fdba | 3081 | case FUNCTION_DECL: |
5566b478 MS |
3082 | case VAR_DECL: |
3083 | /* ??? What about FIELD_DECLs? */ | |
3084 | if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t) | |
050367a3 | 3085 | && for_each_template_parm (DECL_TI_ARGS (t), fn, data)) |
8d08fdba MS |
3086 | return 1; |
3087 | /* fall through */ | |
8d08fdba | 3088 | case PARM_DECL: |
3ac3d9ea | 3089 | check_type_and_context: |
050367a3 | 3090 | if (for_each_template_parm (TREE_TYPE (t), fn, data)) |
5566b478 | 3091 | return 1; |
050367a3 MM |
3092 | if (DECL_CONTEXT (t) |
3093 | && for_each_template_parm (DECL_CONTEXT (t), fn, data)) | |
8d08fdba | 3094 | return 1; |
8d08fdba MS |
3095 | return 0; |
3096 | ||
3097 | case CALL_EXPR: | |
050367a3 | 3098 | return for_each_template_parm (TREE_TYPE (t), fn, data); |
8d08fdba | 3099 | case ADDR_EXPR: |
050367a3 | 3100 | return for_each_template_parm (TREE_OPERAND (t, 0), fn, data); |
8d08fdba MS |
3101 | |
3102 | /* template parm nodes */ | |
3103 | case TEMPLATE_TYPE_PARM: | |
73b0fce8 | 3104 | case TEMPLATE_TEMPLATE_PARM: |
f84b4be9 | 3105 | case TEMPLATE_PARM_INDEX: |
050367a3 MM |
3106 | if (fn) |
3107 | return (*fn)(t, data); | |
3108 | else | |
3109 | return 1; | |
8d08fdba MS |
3110 | |
3111 | /* simple type nodes */ | |
3112 | case INTEGER_TYPE: | |
050367a3 | 3113 | if (for_each_template_parm (TYPE_MIN_VALUE (t), fn, data)) |
8d08fdba | 3114 | return 1; |
050367a3 | 3115 | return for_each_template_parm (TYPE_MAX_VALUE (t), fn, data); |
8d08fdba MS |
3116 | |
3117 | case REAL_TYPE: | |
37c46b43 | 3118 | case COMPLEX_TYPE: |
8d08fdba | 3119 | case VOID_TYPE: |
2986ae00 | 3120 | case BOOLEAN_TYPE: |
8d08fdba MS |
3121 | return 0; |
3122 | ||
85b71cf2 JM |
3123 | case ENUMERAL_TYPE: |
3124 | { | |
3125 | tree v; | |
3126 | ||
3127 | for (v = TYPE_VALUES (t); v != NULL_TREE; v = TREE_CHAIN (v)) | |
050367a3 | 3128 | if (for_each_template_parm (TREE_VALUE (v), fn, data)) |
85b71cf2 JM |
3129 | return 1; |
3130 | } | |
3131 | return 0; | |
3132 | ||
8d08fdba MS |
3133 | /* constants */ |
3134 | case INTEGER_CST: | |
3135 | case REAL_CST: | |
3136 | case STRING_CST: | |
3137 | return 0; | |
3138 | ||
3139 | case ERROR_MARK: | |
3140 | /* Non-error_mark_node ERROR_MARKs are bad things. */ | |
3141 | my_friendly_assert (t == error_mark_node, 274); | |
3142 | /* NOTREACHED */ | |
3143 | return 0; | |
3144 | ||
ec255269 | 3145 | case LOOKUP_EXPR: |
5566b478 | 3146 | case TYPENAME_TYPE: |
8d08fdba MS |
3147 | return 1; |
3148 | ||
5156628f | 3149 | case SCOPE_REF: |
050367a3 | 3150 | return for_each_template_parm (TREE_OPERAND (t, 0), fn, data); |
5156628f | 3151 | |
db5ae43f MS |
3152 | case CONSTRUCTOR: |
3153 | if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))) | |
050367a3 MM |
3154 | return for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE |
3155 | (TREE_TYPE (t)), fn, data); | |
3156 | return for_each_template_parm (TREE_OPERAND (t, 1), fn, data); | |
db5ae43f | 3157 | |
42976354 BK |
3158 | case MODOP_EXPR: |
3159 | case CAST_EXPR: | |
3160 | case REINTERPRET_CAST_EXPR: | |
3161 | case CONST_CAST_EXPR: | |
3162 | case STATIC_CAST_EXPR: | |
3163 | case DYNAMIC_CAST_EXPR: | |
42976354 BK |
3164 | case ARROW_EXPR: |
3165 | case DOTSTAR_EXPR: | |
3166 | case TYPEID_EXPR: | |
3167 | return 1; | |
3168 | ||
abff8e06 JM |
3169 | case SIZEOF_EXPR: |
3170 | case ALIGNOF_EXPR: | |
050367a3 | 3171 | return for_each_template_parm (TREE_OPERAND (t, 0), fn, data); |
abff8e06 | 3172 | |
8d08fdba MS |
3173 | default: |
3174 | switch (TREE_CODE_CLASS (TREE_CODE (t))) | |
3175 | { | |
3176 | case '1': | |
3177 | case '2': | |
ec255269 | 3178 | case 'e': |
8d08fdba MS |
3179 | case '<': |
3180 | { | |
3181 | int i; | |
3182 | for (i = tree_code_length[(int) TREE_CODE (t)]; --i >= 0;) | |
050367a3 | 3183 | if (for_each_template_parm (TREE_OPERAND (t, i), fn, data)) |
8d08fdba MS |
3184 | return 1; |
3185 | return 0; | |
3186 | } | |
3187 | default: | |
3188 | break; | |
3189 | } | |
3190 | sorry ("testing %s for template parms", | |
3191 | tree_code_name [(int) TREE_CODE (t)]); | |
3192 | my_friendly_abort (82); | |
3193 | /* NOTREACHED */ | |
3194 | return 0; | |
3195 | } | |
3196 | } | |
3197 | ||
050367a3 MM |
3198 | int |
3199 | uses_template_parms (t) | |
3200 | tree t; | |
3201 | { | |
3202 | return for_each_template_parm (t, 0, 0); | |
3203 | } | |
3204 | ||
7215f9a0 MS |
3205 | static struct tinst_level *current_tinst_level = 0; |
3206 | static struct tinst_level *free_tinst_level = 0; | |
3207 | static int tinst_depth = 0; | |
e9f32eb5 | 3208 | extern int max_tinst_depth; |
5566b478 MS |
3209 | #ifdef GATHER_STATISTICS |
3210 | int depth_reached = 0; | |
3211 | #endif | |
8d08fdba | 3212 | |
7bae46f4 JM |
3213 | /* Print out all the template instantiations that we are currently |
3214 | working on. */ | |
3215 | ||
3216 | void | |
3217 | print_template_context () | |
3218 | { | |
3219 | struct tinst_level *p = current_tinst_level; | |
3220 | int line = lineno; | |
3221 | char *file = input_filename; | |
3222 | ||
3223 | for (; p; p = p->next) | |
3224 | { | |
3225 | cp_error (" instantiated from `%D'", p->decl); | |
3226 | lineno = p->line; | |
3227 | input_filename = p->file; | |
3228 | } | |
3229 | error (" instantiated from here"); | |
3230 | ||
3231 | lineno = line; | |
3232 | input_filename = file; | |
3233 | } | |
3234 | ||
bd6dd845 | 3235 | static int |
5566b478 MS |
3236 | push_tinst_level (d) |
3237 | tree d; | |
8d08fdba MS |
3238 | { |
3239 | struct tinst_level *new; | |
8d08fdba | 3240 | |
7215f9a0 MS |
3241 | if (tinst_depth >= max_tinst_depth) |
3242 | { | |
8adf5b5e JM |
3243 | /* If the instantiation in question still has unbound template parms, |
3244 | we don't really care if we can't instantiate it, so just return. | |
3245 | This happens with base instantiation for implicit `typename'. */ | |
3246 | if (uses_template_parms (d)) | |
3247 | return 0; | |
3248 | ||
7215f9a0 MS |
3249 | error ("template instantiation depth exceeds maximum of %d", |
3250 | max_tinst_depth); | |
e9f32eb5 | 3251 | error (" (use -ftemplate-depth-NN to increase the maximum)"); |
5566b478 MS |
3252 | cp_error (" instantiating `%D'", d); |
3253 | ||
7bae46f4 | 3254 | print_template_context (); |
5566b478 | 3255 | |
7215f9a0 MS |
3256 | return 0; |
3257 | } | |
3258 | ||
8d08fdba MS |
3259 | if (free_tinst_level) |
3260 | { | |
3261 | new = free_tinst_level; | |
3262 | free_tinst_level = new->next; | |
3263 | } | |
3264 | else | |
3265 | new = (struct tinst_level *) xmalloc (sizeof (struct tinst_level)); | |
3266 | ||
5566b478 MS |
3267 | new->decl = d; |
3268 | new->line = lineno; | |
3269 | new->file = input_filename; | |
8d08fdba MS |
3270 | new->next = current_tinst_level; |
3271 | current_tinst_level = new; | |
5566b478 | 3272 | |
7215f9a0 | 3273 | ++tinst_depth; |
5566b478 MS |
3274 | #ifdef GATHER_STATISTICS |
3275 | if (tinst_depth > depth_reached) | |
3276 | depth_reached = tinst_depth; | |
3277 | #endif | |
3278 | ||
7215f9a0 | 3279 | return 1; |
8d08fdba MS |
3280 | } |
3281 | ||
3282 | void | |
3283 | pop_tinst_level () | |
3284 | { | |
3285 | struct tinst_level *old = current_tinst_level; | |
3286 | ||
3287 | current_tinst_level = old->next; | |
3288 | old->next = free_tinst_level; | |
3289 | free_tinst_level = old; | |
7215f9a0 | 3290 | --tinst_depth; |
8d08fdba MS |
3291 | } |
3292 | ||
3293 | struct tinst_level * | |
3294 | tinst_for_decl () | |
3295 | { | |
3296 | struct tinst_level *p = current_tinst_level; | |
3297 | ||
3298 | if (p) | |
3299 | for (; p->next ; p = p->next ) | |
3300 | ; | |
3301 | return p; | |
3302 | } | |
3303 | ||
f84b4be9 JM |
3304 | /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the |
3305 | vector of template arguments, as for tsubst. | |
3306 | ||
3307 | Returns an appropriate tsbust'd friend declaration. */ | |
3308 | ||
3309 | static tree | |
3310 | tsubst_friend_function (decl, args) | |
3311 | tree decl; | |
3312 | tree args; | |
3313 | { | |
3314 | tree new_friend; | |
3315 | ||
3316 | if (TREE_CODE (decl) == FUNCTION_DECL | |
3317 | && DECL_TEMPLATE_INSTANTIATION (decl) | |
3318 | && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL) | |
3319 | /* This was a friend declared with an explicit template | |
3320 | argument list, e.g.: | |
3321 | ||
3322 | friend void f<>(T); | |
3323 | ||
3324 | to indicate that f was a template instantiation, not a new | |
3325 | function declaration. Now, we have to figure out what | |
3326 | instantiation of what template. */ | |
3327 | { | |
3328 | tree template_id; | |
3329 | tree new_args; | |
3330 | tree tmpl; | |
3331 | tree tinfo; | |
3332 | ||
3333 | template_id | |
3334 | = lookup_template_function (tsubst_expr (DECL_TI_TEMPLATE (decl), | |
f7d98d58 | 3335 | args, NULL_TREE), |
f84b4be9 | 3336 | tsubst (DECL_TI_ARGS (decl), |
f7d98d58 | 3337 | args, NULL_TREE)); |
f84b4be9 JM |
3338 | |
3339 | /* Temporarily remove the DECL_TEMPLATE_INFO so as not to | |
3340 | confuse tsubst. */ | |
3341 | tinfo = DECL_TEMPLATE_INFO (decl); | |
3342 | DECL_TEMPLATE_INFO (decl) = NULL_TREE; | |
f7d98d58 | 3343 | new_friend = tsubst (decl, args, NULL_TREE); |
f84b4be9 JM |
3344 | DECL_TEMPLATE_INFO (decl) = tinfo; |
3345 | ||
3346 | tmpl = determine_specialization (template_id, | |
3347 | new_friend, | |
3348 | &new_args, | |
3349 | 0, 1); | |
3350 | return instantiate_template (tmpl, new_args); | |
3351 | } | |
3352 | else | |
f7d98d58 | 3353 | new_friend = tsubst (decl, args, NULL_TREE); |
f84b4be9 JM |
3354 | |
3355 | /* The new_friend will look like an instantiation, to the | |
3356 | compiler, but is not an instantiation from the point of view of | |
3357 | the language. For example, we might have had: | |
3358 | ||
3359 | template <class T> struct S { | |
3360 | template <class U> friend void f(T, U); | |
3361 | }; | |
3362 | ||
3363 | Then, in S<int>, template <class U> void f(int, U) is not an | |
3364 | instantiation of anything. */ | |
3365 | DECL_USE_TEMPLATE (new_friend) = 0; | |
3366 | if (TREE_CODE (decl) == TEMPLATE_DECL) | |
3367 | DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0; | |
3368 | ||
3369 | if (DECL_CONTEXT (new_friend) == NULL_TREE) | |
3370 | { | |
3371 | if (TREE_CODE (new_friend) == TEMPLATE_DECL) | |
3372 | /* This declaration is a `primary' template. */ | |
3373 | TREE_TYPE (DECL_INNERMOST_TEMPLATE_PARMS (new_friend)) | |
3374 | = new_friend; | |
3375 | ||
3376 | new_friend = pushdecl_top_level (new_friend); | |
3377 | } | |
3378 | else if (TYPE_SIZE (DECL_CONTEXT (new_friend))) | |
3379 | { | |
3380 | /* Check to see that the declaration is really present, and, | |
3381 | possibly obtain an improved declaration. */ | |
3382 | tree fn = check_classfn (DECL_CONTEXT (new_friend), | |
3383 | new_friend); | |
3384 | ||
3385 | if (fn) | |
3386 | new_friend = fn; | |
3387 | } | |
3388 | ||
3389 | return new_friend; | |
3390 | } | |
3391 | ||
6757edfe MM |
3392 | /* FRIEND_TYPE is a friend RECORD_TYPE or UNION_TYPE. ARGS is the |
3393 | vector of template arguments, as for tsubst. | |
3394 | ||
3395 | Returns an appropriate tsbust'd friend type. */ | |
3396 | ||
3397 | static tree | |
3398 | tsubst_friend_class (friend_type, args) | |
3399 | tree friend_type; | |
3400 | tree args; | |
3401 | { | |
3402 | tree tmpl = | |
3403 | lookup_name (DECL_NAME (CLASSTYPE_TI_TEMPLATE (friend_type)), 1); | |
3404 | ||
3405 | tmpl = maybe_get_template_decl_from_type_decl (tmpl); | |
3406 | ||
3407 | if (tmpl != NULL_TREE && DECL_CLASS_TEMPLATE_P (tmpl)) | |
3408 | { | |
3409 | /* The friend template has already been declared. Just | |
3410 | check to see that the declarations match. */ | |
3411 | redeclare_class_template (TREE_TYPE (tmpl), | |
3412 | DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE | |
3413 | (friend_type))); | |
3414 | friend_type = TREE_TYPE (tmpl); | |
3415 | } | |
3416 | else | |
3417 | { | |
3418 | /* The friend template has not already been declared. In this | |
3419 | case, the instantiation of the template class will cause the | |
3420 | injection of this template into the global scope. */ | |
3421 | tmpl = tsubst (CLASSTYPE_TI_TEMPLATE (friend_type), args, NULL_TREE); | |
3422 | ||
3423 | /* The new TMPL is not an instantiation of anything, so we | |
3424 | forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for | |
3425 | the new type because that is supposed to be the corresponding | |
3426 | template decl, i.e., TMPL. */ | |
3427 | DECL_USE_TEMPLATE (tmpl) = 0; | |
3428 | DECL_TEMPLATE_INFO (tmpl) = NULL_TREE; | |
3429 | CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0; | |
3430 | ||
3431 | /* Inject this template into the global scope. */ | |
3432 | friend_type = TREE_TYPE (pushdecl_top_level (tmpl)); | |
3433 | } | |
3434 | ||
3435 | return friend_type; | |
3436 | } | |
f84b4be9 | 3437 | |
8d08fdba | 3438 | tree |
5566b478 MS |
3439 | instantiate_class_template (type) |
3440 | tree type; | |
8d08fdba | 3441 | { |
fc378698 | 3442 | tree template, template_info, args, pattern, t, *field_chain; |
8d019cef | 3443 | tree typedecl, outer_args; |
8d08fdba | 3444 | |
5566b478 | 3445 | if (type == error_mark_node) |
8d08fdba MS |
3446 | return error_mark_node; |
3447 | ||
5566b478 MS |
3448 | template_info = CLASSTYPE_TEMPLATE_INFO (type); |
3449 | ||
3450 | if (TYPE_BEING_DEFINED (type) || TYPE_SIZE (type)) | |
3451 | return type; | |
3452 | ||
3453 | template = TI_TEMPLATE (template_info); | |
3454 | my_friendly_assert (TREE_CODE (template) == TEMPLATE_DECL, 279); | |
3455 | args = TI_ARGS (template_info); | |
73aad9b9 | 3456 | |
93cdc044 JM |
3457 | if (DECL_TEMPLATE_INFO (template)) |
3458 | { | |
8d019cef | 3459 | outer_args = DECL_TI_ARGS (template); |
93cdc044 JM |
3460 | while (DECL_TEMPLATE_INFO (template)) |
3461 | template = DECL_TI_TEMPLATE (template); | |
3462 | } | |
8d019cef JM |
3463 | else |
3464 | outer_args = NULL_TREE; | |
93cdc044 | 3465 | |
73aad9b9 | 3466 | t = most_specialized_class |
8d019cef | 3467 | (DECL_TEMPLATE_SPECIALIZATIONS (template), args, outer_args); |
73aad9b9 JM |
3468 | |
3469 | if (t == error_mark_node) | |
3470 | { | |
3471 | char *str = "candidates are:"; | |
3472 | cp_error ("ambiguous class template instantiation for `%#T'", type); | |
3473 | for (t = DECL_TEMPLATE_SPECIALIZATIONS (template); t; t = TREE_CHAIN (t)) | |
3474 | { | |
8d019cef JM |
3475 | if (get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t), |
3476 | args, outer_args)) | |
73aad9b9 JM |
3477 | { |
3478 | cp_error_at ("%s %+#T", str, TREE_TYPE (t)); | |
3479 | str = " "; | |
3480 | } | |
3481 | } | |
3482 | TYPE_BEING_DEFINED (type) = 1; | |
de22184b | 3483 | return error_mark_node; |
73aad9b9 JM |
3484 | } |
3485 | else if (t) | |
3486 | pattern = TREE_TYPE (t); | |
3487 | else | |
3488 | pattern = TREE_TYPE (template); | |
5566b478 MS |
3489 | |
3490 | if (TYPE_SIZE (pattern) == NULL_TREE) | |
ec255269 | 3491 | return type; |
5566b478 | 3492 | |
73aad9b9 | 3493 | if (t) |
8d019cef JM |
3494 | args = get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t), |
3495 | args, outer_args); | |
3496 | ||
8adf5b5e JM |
3497 | if (pedantic && uses_template_parms (args)) |
3498 | /* If there are still template parameters amongst the args, then | |
3499 | we can't instantiate the type; there's no telling whether or not one | |
3500 | of the template parameters might eventually be instantiated to some | |
3501 | value that results in a specialization being used. */ | |
3502 | return type; | |
3503 | ||
b4c4a9ec MM |
3504 | /* We must copy the arguments to the permanent obstack since |
3505 | during the tsubst'ing below they may wind up in the | |
3506 | DECL_TI_ARGS of some instantiated member template. */ | |
3507 | args = copy_to_permanent (args); | |
3508 | ||
5566b478 MS |
3509 | TYPE_BEING_DEFINED (type) = 1; |
3510 | ||
3511 | if (! push_tinst_level (type)) | |
3512 | return type; | |
8d08fdba | 3513 | |
5566b478 MS |
3514 | maybe_push_to_top_level (uses_template_parms (type)); |
3515 | pushclass (type, 0); | |
3516 | ||
db897306 JM |
3517 | if (outer_args) |
3518 | args = add_to_template_args (outer_args, args); | |
3519 | ||
5566b478 MS |
3520 | if (flag_external_templates) |
3521 | { | |
3522 | if (flag_alt_external_templates) | |
3523 | { | |
3524 | CLASSTYPE_INTERFACE_ONLY (type) = interface_only; | |
3525 | SET_CLASSTYPE_INTERFACE_UNKNOWN_X (type, interface_unknown); | |
3526 | CLASSTYPE_VTABLE_NEEDS_WRITING (type) | |
2604412d JM |
3527 | = (! CLASSTYPE_INTERFACE_ONLY (type) |
3528 | && CLASSTYPE_INTERFACE_KNOWN (type)); | |
5566b478 MS |
3529 | } |
3530 | else | |
3531 | { | |
3532 | CLASSTYPE_INTERFACE_ONLY (type) = CLASSTYPE_INTERFACE_ONLY (pattern); | |
3533 | SET_CLASSTYPE_INTERFACE_UNKNOWN_X | |
3534 | (type, CLASSTYPE_INTERFACE_UNKNOWN (pattern)); | |
3535 | CLASSTYPE_VTABLE_NEEDS_WRITING (type) | |
2604412d JM |
3536 | = (! CLASSTYPE_INTERFACE_ONLY (type) |
3537 | && CLASSTYPE_INTERFACE_KNOWN (type)); | |
5566b478 MS |
3538 | } |
3539 | } | |
3540 | else | |
8d08fdba | 3541 | { |
5566b478 MS |
3542 | SET_CLASSTYPE_INTERFACE_UNKNOWN (type); |
3543 | CLASSTYPE_VTABLE_NEEDS_WRITING (type) = 1; | |
8d08fdba MS |
3544 | } |
3545 | ||
f7da6097 MS |
3546 | TYPE_HAS_CONSTRUCTOR (type) = TYPE_HAS_CONSTRUCTOR (pattern); |
3547 | TYPE_HAS_DESTRUCTOR (type) = TYPE_HAS_DESTRUCTOR (pattern); | |
3548 | TYPE_HAS_ASSIGNMENT (type) = TYPE_HAS_ASSIGNMENT (pattern); | |
3549 | TYPE_OVERLOADS_CALL_EXPR (type) = TYPE_OVERLOADS_CALL_EXPR (pattern); | |
3550 | TYPE_OVERLOADS_ARRAY_REF (type) = TYPE_OVERLOADS_ARRAY_REF (pattern); | |
3551 | TYPE_OVERLOADS_ARROW (type) = TYPE_OVERLOADS_ARROW (pattern); | |
3552 | TYPE_GETS_NEW (type) = TYPE_GETS_NEW (pattern); | |
3553 | TYPE_GETS_DELETE (type) = TYPE_GETS_DELETE (pattern); | |
3554 | TYPE_VEC_DELETE_TAKES_SIZE (type) = TYPE_VEC_DELETE_TAKES_SIZE (pattern); | |
3555 | TYPE_HAS_ASSIGN_REF (type) = TYPE_HAS_ASSIGN_REF (pattern); | |
3556 | TYPE_HAS_CONST_ASSIGN_REF (type) = TYPE_HAS_CONST_ASSIGN_REF (pattern); | |
3557 | TYPE_HAS_ABSTRACT_ASSIGN_REF (type) = TYPE_HAS_ABSTRACT_ASSIGN_REF (pattern); | |
3558 | TYPE_HAS_INIT_REF (type) = TYPE_HAS_INIT_REF (pattern); | |
3559 | TYPE_HAS_CONST_INIT_REF (type) = TYPE_HAS_CONST_INIT_REF (pattern); | |
f7da6097 MS |
3560 | TYPE_HAS_DEFAULT_CONSTRUCTOR (type) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern); |
3561 | TYPE_HAS_CONVERSION (type) = TYPE_HAS_CONVERSION (pattern); | |
3562 | TYPE_USES_COMPLEX_INHERITANCE (type) | |
3563 | = TYPE_USES_COMPLEX_INHERITANCE (pattern); | |
3564 | TYPE_USES_MULTIPLE_INHERITANCE (type) | |
3565 | = TYPE_USES_MULTIPLE_INHERITANCE (pattern); | |
3566 | TYPE_USES_VIRTUAL_BASECLASSES (type) | |
3567 | = TYPE_USES_VIRTUAL_BASECLASSES (pattern); | |
3568 | TYPE_PACKED (type) = TYPE_PACKED (pattern); | |
3569 | TYPE_ALIGN (type) = TYPE_ALIGN (pattern); | |
3570 | ||
2604412d JM |
3571 | CLASSTYPE_LOCAL_TYPEDECLS (type) = CLASSTYPE_LOCAL_TYPEDECLS (pattern); |
3572 | ||
3573 | /* If this is a partial instantiation, don't tsubst anything. We will | |
3574 | only use this type for implicit typename, so the actual contents don't | |
3575 | matter. All that matters is whether a particular name is a type. */ | |
3576 | if (uses_template_parms (type)) | |
3577 | { | |
3578 | TYPE_BINFO_BASETYPES (type) = TYPE_BINFO_BASETYPES (pattern); | |
3579 | TYPE_FIELDS (type) = TYPE_FIELDS (pattern); | |
3580 | TYPE_METHODS (type) = TYPE_METHODS (pattern); | |
3581 | CLASSTYPE_TAGS (type) = CLASSTYPE_TAGS (pattern); | |
3582 | TYPE_SIZE (type) = integer_zero_node; | |
3583 | goto end; | |
3584 | } | |
3585 | ||
5566b478 MS |
3586 | { |
3587 | tree binfo = TYPE_BINFO (type); | |
3588 | tree pbases = TYPE_BINFO_BASETYPES (pattern); | |
3589 | ||
3590 | if (pbases) | |
3591 | { | |
3592 | tree bases; | |
3593 | int i; | |
3594 | int len = TREE_VEC_LENGTH (pbases); | |
de22184b | 3595 | bases = make_tree_vec (len); |
5566b478 MS |
3596 | for (i = 0; i < len; ++i) |
3597 | { | |
3598 | tree elt; | |
3599 | ||
3600 | TREE_VEC_ELT (bases, i) = elt | |
f7d98d58 | 3601 | = tsubst (TREE_VEC_ELT (pbases, i), args, NULL_TREE); |
5566b478 MS |
3602 | BINFO_INHERITANCE_CHAIN (elt) = binfo; |
3603 | ||
6633d636 MS |
3604 | if (! IS_AGGR_TYPE (TREE_TYPE (elt))) |
3605 | cp_error | |
3606 | ("base type `%T' of `%T' fails to be a struct or class type", | |
3607 | TREE_TYPE (elt), type); | |
2604412d | 3608 | else if (TYPE_SIZE (complete_type (TREE_TYPE (elt))) == NULL_TREE) |
5566b478 MS |
3609 | cp_error ("base class `%T' of `%T' has incomplete type", |
3610 | TREE_TYPE (elt), type); | |
3611 | } | |
de22184b MS |
3612 | /* Don't initialize this until the vector is filled out, or |
3613 | lookups will crash. */ | |
3614 | BINFO_BASETYPES (binfo) = bases; | |
5566b478 MS |
3615 | } |
3616 | } | |
3617 | ||
5566b478 | 3618 | field_chain = &TYPE_FIELDS (type); |
5566b478 MS |
3619 | |
3620 | for (t = CLASSTYPE_TAGS (pattern); t; t = TREE_CHAIN (t)) | |
8d08fdba | 3621 | { |
5566b478 | 3622 | tree tag = TREE_VALUE (t); |
5566b478 | 3623 | |
fc378698 | 3624 | /* These will add themselves to CLASSTYPE_TAGS for the new type. */ |
5566b478 | 3625 | if (TREE_CODE (tag) == ENUMERAL_TYPE) |
8d08fdba | 3626 | { |
56c5d8bf | 3627 | (void) tsubst_enum (tag, args, field_chain); |
5566b478 | 3628 | while (*field_chain) |
37c46b43 MS |
3629 | { |
3630 | DECL_FIELD_CONTEXT (*field_chain) = type; | |
3631 | field_chain = &TREE_CHAIN (*field_chain); | |
3632 | } | |
8d08fdba | 3633 | } |
b87692e5 | 3634 | else |
f7d98d58 | 3635 | tsubst (tag, args, NULL_TREE); |
8d08fdba MS |
3636 | } |
3637 | ||
5566b478 MS |
3638 | /* Don't replace enum constants here. */ |
3639 | for (t = TYPE_FIELDS (pattern); t; t = TREE_CHAIN (t)) | |
3640 | if (TREE_CODE (t) != CONST_DECL) | |
3641 | { | |
f7d98d58 | 3642 | tree r = tsubst (t, args, NULL_TREE); |
5566b478 MS |
3643 | if (TREE_CODE (r) == VAR_DECL) |
3644 | { | |
2604412d JM |
3645 | pending_statics = perm_tree_cons (NULL_TREE, r, pending_statics); |
3646 | /* Perhaps we should do more of grokfield here. */ | |
5566b478 MS |
3647 | start_decl_1 (r); |
3648 | DECL_IN_AGGR_P (r) = 1; | |
3649 | DECL_EXTERNAL (r) = 1; | |
3650 | cp_finish_decl (r, DECL_INITIAL (r), NULL_TREE, 0, 0); | |
3651 | } | |
3652 | ||
3653 | *field_chain = r; | |
3654 | field_chain = &TREE_CHAIN (r); | |
3655 | } | |
8d08fdba | 3656 | |
5566b478 | 3657 | TYPE_METHODS (type) = tsubst_chain (TYPE_METHODS (pattern), args); |
f7da6097 MS |
3658 | for (t = TYPE_METHODS (type); t; t = TREE_CHAIN (t)) |
3659 | { | |
3660 | if (DECL_CONSTRUCTOR_P (t)) | |
3661 | grok_ctor_properties (type, t); | |
3662 | else if (IDENTIFIER_OPNAME_P (DECL_NAME (t))) | |
3663 | grok_op_properties (t, DECL_VIRTUAL_P (t), 0); | |
3664 | } | |
8d08fdba | 3665 | |
2604412d JM |
3666 | /* Construct the DECL_FRIENDLIST for the new class type. */ |
3667 | typedecl = TYPE_MAIN_DECL (type); | |
3668 | for (t = DECL_FRIENDLIST (TYPE_MAIN_DECL (pattern)); | |
3669 | t != NULL_TREE; | |
3670 | t = TREE_CHAIN (t)) | |
f84b4be9 | 3671 | { |
2604412d | 3672 | tree friends; |
f84b4be9 | 3673 | |
2604412d JM |
3674 | DECL_FRIENDLIST (typedecl) |
3675 | = tree_cons (TREE_PURPOSE (t), NULL_TREE, | |
3676 | DECL_FRIENDLIST (typedecl)); | |
f84b4be9 | 3677 | |
2604412d JM |
3678 | for (friends = TREE_VALUE (t); |
3679 | friends != NULL_TREE; | |
3680 | friends = TREE_CHAIN (friends)) | |
3681 | { | |
3682 | if (TREE_PURPOSE (friends) == error_mark_node) | |
f84b4be9 | 3683 | { |
2604412d JM |
3684 | TREE_VALUE (DECL_FRIENDLIST (typedecl)) |
3685 | = tree_cons (error_mark_node, | |
3686 | tsubst_friend_function (TREE_VALUE (friends), | |
3687 | args), | |
3688 | TREE_VALUE (DECL_FRIENDLIST (typedecl))); | |
3689 | } | |
3690 | else | |
3691 | { | |
3692 | TREE_VALUE (DECL_FRIENDLIST (typedecl)) | |
3693 | = tree_cons (tsubst (TREE_PURPOSE (friends), args, NULL_TREE), | |
3694 | NULL_TREE, | |
3695 | TREE_VALUE (DECL_FRIENDLIST (typedecl))); | |
f84b4be9 JM |
3696 | |
3697 | } | |
3698 | } | |
2604412d | 3699 | } |
5566b478 | 3700 | |
6757edfe MM |
3701 | for (t = CLASSTYPE_FRIEND_CLASSES (pattern); |
3702 | t != NULL_TREE; | |
3703 | t = TREE_CHAIN (t)) | |
3704 | { | |
3705 | tree friend_type = TREE_VALUE (t); | |
3706 | ||
3707 | if (!CLASSTYPE_IS_TEMPLATE (friend_type)) | |
3708 | /* The call to xref_tag_from_type does injection for friend | |
3709 | classes. */ | |
3710 | friend_type = | |
3711 | xref_tag_from_type (tsubst (friend_type, args, NULL_TREE), | |
3712 | NULL_TREE, 1); | |
3713 | else | |
3714 | friend_type = tsubst_friend_class (friend_type, args); | |
fc378698 | 3715 | |
6757edfe MM |
3716 | CLASSTYPE_FRIEND_CLASSES (type) = |
3717 | tree_cons (NULL_TREE, friend_type, | |
3718 | CLASSTYPE_FRIEND_CLASSES (type)); | |
3719 | } | |
fc378698 | 3720 | |
2604412d JM |
3721 | /* This does injection for friend functions. */ |
3722 | if (!processing_template_decl) | |
3723 | { | |
3724 | t = tsubst (DECL_TEMPLATE_INJECT (template), args, NULL_TREE); | |
5566b478 | 3725 | |
2604412d JM |
3726 | for (; t; t = TREE_CHAIN (t)) |
3727 | { | |
3728 | tree d = TREE_VALUE (t); | |
fc378698 | 3729 | |
2604412d JM |
3730 | if (TREE_CODE (d) == TYPE_DECL) |
3731 | /* Already injected. */; | |
3732 | else | |
3733 | pushdecl (d); | |
3734 | } | |
3735 | } | |
5566b478 | 3736 | |
2604412d JM |
3737 | for (t = TYPE_FIELDS (type); t; t = TREE_CHAIN (t)) |
3738 | if (TREE_CODE (t) == FIELD_DECL) | |
3739 | { | |
3740 | TREE_TYPE (t) = complete_type (TREE_TYPE (t)); | |
3741 | require_complete_type (t); | |
3742 | } | |
5566b478 | 3743 | |
2604412d JM |
3744 | type = finish_struct_1 (type, 0); |
3745 | CLASSTYPE_GOT_SEMICOLON (type) = 1; | |
e92cc029 | 3746 | |
2604412d JM |
3747 | repo_template_used (type); |
3748 | if (at_eof && TYPE_BINFO_VTABLE (type) != NULL_TREE) | |
3749 | finish_prevtable_vardecl (NULL, TYPE_BINFO_VTABLE (type)); | |
8d08fdba | 3750 | |
2604412d | 3751 | end: |
5566b478 MS |
3752 | TYPE_BEING_DEFINED (type) = 0; |
3753 | popclass (0); | |
3754 | ||
3755 | pop_from_top_level (); | |
3756 | pop_tinst_level (); | |
3757 | ||
3758 | return type; | |
8d08fdba MS |
3759 | } |
3760 | ||
3761 | static int | |
3762 | list_eq (t1, t2) | |
3763 | tree t1, t2; | |
3764 | { | |
3765 | if (t1 == NULL_TREE) | |
3766 | return t2 == NULL_TREE; | |
3767 | if (t2 == NULL_TREE) | |
3768 | return 0; | |
3769 | /* Don't care if one declares its arg const and the other doesn't -- the | |
3770 | main variant of the arg type is all that matters. */ | |
3771 | if (TYPE_MAIN_VARIANT (TREE_VALUE (t1)) | |
3772 | != TYPE_MAIN_VARIANT (TREE_VALUE (t2))) | |
3773 | return 0; | |
3774 | return list_eq (TREE_CHAIN (t1), TREE_CHAIN (t2)); | |
3775 | } | |
3776 | ||
5566b478 | 3777 | tree |
8d08fdba MS |
3778 | lookup_nested_type_by_name (ctype, name) |
3779 | tree ctype, name; | |
3780 | { | |
3781 | tree t; | |
3782 | ||
5566b478 MS |
3783 | complete_type (ctype); |
3784 | ||
db5ae43f MS |
3785 | for (t = CLASSTYPE_TAGS (ctype); t; t = TREE_CHAIN (t)) |
3786 | { | |
f017f649 JM |
3787 | if (name == TREE_PURPOSE (t) |
3788 | /* this catches typedef enum { foo } bar; */ | |
3789 | || name == TYPE_IDENTIFIER (TREE_VALUE (t))) | |
db5ae43f MS |
3790 | return TREE_VALUE (t); |
3791 | } | |
8d08fdba MS |
3792 | return NULL_TREE; |
3793 | } | |
3794 | ||
00d3396f JM |
3795 | /* If arg is a non-type template parameter that does not depend on template |
3796 | arguments, fold it like we weren't in the body of a template. */ | |
3797 | ||
3798 | static tree | |
3799 | maybe_fold_nontype_arg (arg) | |
3800 | tree arg; | |
3801 | { | |
3802 | if (TREE_CODE_CLASS (TREE_CODE (arg)) != 't' | |
3803 | && !uses_template_parms (arg)) | |
3804 | { | |
3805 | /* Sometimes, one of the args was an expression involving a | |
3806 | template constant parameter, like N - 1. Now that we've | |
3807 | tsubst'd, we might have something like 2 - 1. This will | |
3808 | confuse lookup_template_class, so we do constant folding | |
3809 | here. We have to unset processing_template_decl, to | |
3810 | fool build_expr_from_tree() into building an actual | |
3811 | tree. */ | |
3812 | ||
3813 | int saved_processing_template_decl = processing_template_decl; | |
3814 | processing_template_decl = 0; | |
3815 | arg = fold (build_expr_from_tree (arg)); | |
3816 | processing_template_decl = saved_processing_template_decl; | |
3817 | } | |
3818 | return arg; | |
3819 | } | |
3820 | ||
a221d52f JM |
3821 | /* Return the TREE_VEC with the arguments for the innermost template header, |
3822 | where ARGS is either that or the VEC of VECs for all the arguments. | |
3823 | ||
3824 | If is_spec, then we are dealing with a specialization of a member | |
3825 | template, and want the second-innermost args, the innermost ones that | |
3826 | are instantiated. */ | |
3827 | ||
3828 | tree | |
3829 | innermost_args (args, is_spec) | |
3830 | tree args; | |
3831 | int is_spec; | |
3832 | { | |
3833 | if (TREE_CODE (TREE_VEC_ELT (args, 0)) == TREE_VEC) | |
3834 | return TREE_VEC_ELT (args, TREE_VEC_LENGTH (args) - 1 - is_spec); | |
3835 | return args; | |
3836 | } | |
f84b4be9 | 3837 | |
00d3396f | 3838 | /* Take the tree structure T and replace template parameters used therein |
f7d98d58 JM |
3839 | with the argument vector ARGS. IN_DECL is an associated decl for |
3840 | diagnostics. | |
00d3396f JM |
3841 | |
3842 | tsubst is used for dealing with types, decls and the like; for | |
3843 | expressions, use tsubst_expr or tsubst_copy. */ | |
3844 | ||
75b0bbce | 3845 | tree |
f7d98d58 | 3846 | tsubst (t, args, in_decl) |
98c1c668 | 3847 | tree t, args; |
8d08fdba MS |
3848 | tree in_decl; |
3849 | { | |
3850 | tree type; | |
3851 | ||
5566b478 MS |
3852 | if (t == NULL_TREE || t == error_mark_node |
3853 | || t == integer_type_node | |
3854 | || t == void_type_node | |
3855 | || t == char_type_node) | |
8d08fdba MS |
3856 | return t; |
3857 | ||
3858 | type = TREE_TYPE (t); | |
5566b478 MS |
3859 | if (type == unknown_type_node) |
3860 | my_friendly_abort (42); | |
824b9a4c | 3861 | if (type && TREE_CODE (t) != FUNCTION_DECL |
f84b4be9 JM |
3862 | && TREE_CODE (t) != TYPENAME_TYPE |
3863 | && TREE_CODE (t) != TEMPLATE_DECL) | |
f7d98d58 | 3864 | type = tsubst (type, args, in_decl); |
b7484fbe | 3865 | |
8d08fdba MS |
3866 | switch (TREE_CODE (t)) |
3867 | { | |
3868 | case RECORD_TYPE: | |
3869 | if (TYPE_PTRMEMFUNC_P (t)) | |
5566b478 MS |
3870 | { |
3871 | tree r = build_ptrmemfunc_type | |
f7d98d58 | 3872 | (tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, in_decl)); |
5566b478 MS |
3873 | return cp_build_type_variant (r, TYPE_READONLY (t), |
3874 | TYPE_VOLATILE (t)); | |
3875 | } | |
3876 | ||
8d08fdba | 3877 | /* else fall through */ |
5566b478 MS |
3878 | case UNION_TYPE: |
3879 | if (uses_template_parms (t)) | |
3880 | { | |
f7d98d58 | 3881 | tree argvec = tsubst (CLASSTYPE_TI_ARGS (t), args, in_decl); |
75650646 MM |
3882 | tree context; |
3883 | tree r; | |
3884 | ||
e1467ff2 MM |
3885 | context = |
3886 | TYPE_CONTEXT (t) | |
f7d98d58 | 3887 | ? tsubst (TYPE_CONTEXT (t), args, in_decl) : NULL_TREE; |
75650646 MM |
3888 | |
3889 | r = lookup_template_class (t, argvec, in_decl, context); | |
3890 | ||
5566b478 | 3891 | return cp_build_type_variant (r, TYPE_READONLY (t), |
050367a3 | 3892 | TYPE_VOLATILE (t)); |
5566b478 | 3893 | } |
8d08fdba | 3894 | |
5566b478 | 3895 | /* else fall through */ |
8d08fdba MS |
3896 | case ERROR_MARK: |
3897 | case IDENTIFIER_NODE: | |
3898 | case OP_IDENTIFIER: | |
3899 | case VOID_TYPE: | |
3900 | case REAL_TYPE: | |
37c46b43 | 3901 | case COMPLEX_TYPE: |
2986ae00 | 3902 | case BOOLEAN_TYPE: |
8d08fdba MS |
3903 | case INTEGER_CST: |
3904 | case REAL_CST: | |
3905 | case STRING_CST: | |
8d08fdba MS |
3906 | return t; |
3907 | ||
5566b478 MS |
3908 | case ENUMERAL_TYPE: |
3909 | { | |
f7d98d58 | 3910 | tree ctx = tsubst (TYPE_CONTEXT (t), args, in_decl); |
5566b478 MS |
3911 | if (ctx == NULL_TREE) |
3912 | return t; | |
b87692e5 MS |
3913 | else if (ctx == current_function_decl) |
3914 | return lookup_name (TYPE_IDENTIFIER (t), 1); | |
5566b478 MS |
3915 | else |
3916 | return lookup_nested_type_by_name (ctx, TYPE_IDENTIFIER (t)); | |
3917 | } | |
3918 | ||
8d08fdba MS |
3919 | case INTEGER_TYPE: |
3920 | if (t == integer_type_node) | |
3921 | return t; | |
3922 | ||
3923 | if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST | |
3924 | && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST) | |
3925 | return t; | |
5566b478 MS |
3926 | |
3927 | { | |
37c46b43 | 3928 | tree max = TREE_OPERAND (TYPE_MAX_VALUE (t), 0); |
f7d98d58 | 3929 | max = tsubst_expr (max, args, in_decl); |
5156628f | 3930 | if (processing_template_decl) |
5566b478 MS |
3931 | { |
3932 | tree itype = make_node (INTEGER_TYPE); | |
3933 | TYPE_MIN_VALUE (itype) = size_zero_node; | |
37c46b43 MS |
3934 | TYPE_MAX_VALUE (itype) = build_min (MINUS_EXPR, sizetype, max, |
3935 | integer_one_node); | |
5566b478 MS |
3936 | return itype; |
3937 | } | |
37c46b43 MS |
3938 | |
3939 | max = fold (build_binary_op (MINUS_EXPR, max, integer_one_node, 1)); | |
5566b478 MS |
3940 | return build_index_2_type (size_zero_node, max); |
3941 | } | |
8d08fdba MS |
3942 | |
3943 | case TEMPLATE_TYPE_PARM: | |
73b0fce8 | 3944 | case TEMPLATE_TEMPLATE_PARM: |
f84b4be9 | 3945 | case TEMPLATE_PARM_INDEX: |
db5ae43f | 3946 | { |
98c1c668 JM |
3947 | int idx; |
3948 | int level; | |
93cdc044 | 3949 | int levels; |
f84b4be9 | 3950 | tree r = NULL_TREE; |
98c1c668 | 3951 | |
73b0fce8 KL |
3952 | if (TREE_CODE (t) == TEMPLATE_TYPE_PARM |
3953 | || TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM) | |
98c1c668 JM |
3954 | { |
3955 | idx = TEMPLATE_TYPE_IDX (t); | |
3956 | level = TEMPLATE_TYPE_LEVEL (t); | |
3957 | } | |
3958 | else | |
3959 | { | |
f84b4be9 JM |
3960 | idx = TEMPLATE_PARM_IDX (t); |
3961 | level = TEMPLATE_PARM_LEVEL (t); | |
98c1c668 JM |
3962 | } |
3963 | ||
f84b4be9 | 3964 | if (TREE_VEC_LENGTH (args) > 0) |
98c1c668 JM |
3965 | { |
3966 | tree arg = NULL_TREE; | |
3967 | ||
050367a3 MM |
3968 | if (TREE_VEC_ELT (args, 0) != NULL_TREE |
3969 | && TREE_CODE (TREE_VEC_ELT (args, 0)) == TREE_VEC) | |
98c1c668 | 3970 | { |
93cdc044 JM |
3971 | levels = TREE_VEC_LENGTH (args); |
3972 | if (level <= levels) | |
98c1c668 JM |
3973 | arg = TREE_VEC_ELT |
3974 | (TREE_VEC_ELT (args, level - 1), idx); | |
3975 | } | |
93cdc044 JM |
3976 | else |
3977 | { | |
3978 | levels = 1; | |
3979 | if (level == 1) | |
3980 | arg = TREE_VEC_ELT (args, idx); | |
3981 | } | |
98c1c668 JM |
3982 | |
3983 | if (arg != NULL_TREE) | |
3984 | { | |
3985 | if (TREE_CODE (t) == TEMPLATE_TYPE_PARM) | |
3986 | return cp_build_type_variant | |
3987 | (arg, TYPE_READONLY (arg) || TYPE_READONLY (t), | |
3988 | TYPE_VOLATILE (arg) || TYPE_VOLATILE (t)); | |
73b0fce8 KL |
3989 | else if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM) |
3990 | { | |
3991 | if (CLASSTYPE_TEMPLATE_INFO (t)) | |
3992 | { | |
3993 | /* We are processing a type constructed from | |
3994 | a template template parameter */ | |
b7a29012 | 3995 | tree argvec = tsubst (CLASSTYPE_TI_ARGS (t), |
f7d98d58 | 3996 | args, in_decl); |
73b0fce8 KL |
3997 | tree r; |
3998 | ||
3999 | /* We can get a TEMPLATE_TEMPLATE_PARM here when | |
4000 | we are resolving nested-types in the signature of | |
4001 | a member function templates. | |
4002 | Otherwise ARG is a TEMPLATE_DECL and is the real | |
4003 | template to be instantiated. */ | |
4004 | if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM) | |
4005 | arg = TYPE_NAME (arg); | |
4006 | ||
4007 | r = lookup_template_class (DECL_NAME (arg), | |
e1467ff2 MM |
4008 | argvec, in_decl, |
4009 | DECL_CONTEXT (arg)); | |
73b0fce8 KL |
4010 | return cp_build_type_variant (r, TYPE_READONLY (t), |
4011 | TYPE_VOLATILE (t)); | |
4012 | } | |
4013 | else | |
4014 | /* We are processing a template argument list. */ | |
4015 | return arg; | |
4016 | } | |
98c1c668 JM |
4017 | else |
4018 | return arg; | |
4019 | } | |
4020 | } | |
4021 | ||
050367a3 MM |
4022 | if (level == 1) |
4023 | /* This can happen during the attempted tsubst'ing in | |
4024 | unify. This means that we don't yet have any information | |
4025 | about the template parameter in question. */ | |
4026 | return t; | |
4027 | ||
98c1c668 | 4028 | /* If we get here, we must have been looking at a parm for a |
050367a3 MM |
4029 | more deeply nested template. Make a new version of this |
4030 | template parameter, but with a lower level. */ | |
f84b4be9 JM |
4031 | switch (TREE_CODE (t)) |
4032 | { | |
4033 | case TEMPLATE_TYPE_PARM: | |
4034 | case TEMPLATE_TEMPLATE_PARM: | |
362badb0 | 4035 | r = copy_node (t); |
f84b4be9 JM |
4036 | TEMPLATE_TYPE_PARM_INDEX (r) |
4037 | = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t), | |
93cdc044 | 4038 | r, levels); |
f84b4be9 JM |
4039 | TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r); |
4040 | TYPE_MAIN_VARIANT (r) = r; | |
4041 | TYPE_POINTER_TO (r) = NULL_TREE; | |
debf0b88 | 4042 | TYPE_REFERENCE_TO (r) = NULL_TREE; |
362badb0 KL |
4043 | |
4044 | if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM | |
4045 | && CLASSTYPE_TEMPLATE_INFO (t)) | |
4046 | { | |
4047 | tree argvec = tsubst (CLASSTYPE_TI_ARGS (t), args, in_decl); | |
4048 | CLASSTYPE_TEMPLATE_INFO (r) | |
4049 | = perm_tree_cons (TYPE_NAME (t), argvec, NULL_TREE); | |
4050 | } | |
f84b4be9 JM |
4051 | break; |
4052 | ||
4053 | case TEMPLATE_PARM_INDEX: | |
93cdc044 | 4054 | r = reduce_template_parm_level (t, TREE_TYPE (t), levels); |
f84b4be9 JM |
4055 | break; |
4056 | ||
4057 | default: | |
4058 | my_friendly_abort (0); | |
4059 | } | |
4060 | ||
4061 | return r; | |
db5ae43f | 4062 | } |
8d08fdba | 4063 | |
98c1c668 JM |
4064 | case TEMPLATE_DECL: |
4065 | { | |
4066 | /* We can get here when processing a member template function | |
4067 | of a template class. */ | |
4068 | tree tmpl; | |
4069 | tree decl = DECL_TEMPLATE_RESULT (t); | |
98c1c668 | 4070 | tree parms; |
f84b4be9 | 4071 | tree* new_parms; |
386b8a85 | 4072 | tree spec; |
db2767b6 | 4073 | int is_template_template_parm = DECL_TEMPLATE_TEMPLATE_PARM_P (t); |
98c1c668 | 4074 | |
db2767b6 MM |
4075 | if (!is_template_template_parm) |
4076 | { | |
4077 | /* We might already have an instance of this template. */ | |
4078 | spec = retrieve_specialization (t, args); | |
4079 | if (spec != NULL_TREE) | |
4080 | return spec; | |
4081 | } | |
98c1c668 JM |
4082 | |
4083 | /* Make a new template decl. It will be similar to the | |
4084 | original, but will record the current template arguments. | |
4085 | We also create a new function declaration, which is just | |
4086 | like the old one, but points to this new template, rather | |
4087 | than the old one. */ | |
4088 | tmpl = copy_node (t); | |
4089 | copy_lang_decl (tmpl); | |
4090 | my_friendly_assert (DECL_LANG_SPECIFIC (tmpl) != 0, 0); | |
4091 | DECL_CHAIN (tmpl) = NULL_TREE; | |
4092 | TREE_CHAIN (tmpl) = NULL_TREE; | |
db2767b6 MM |
4093 | |
4094 | if (is_template_template_parm) | |
4095 | { | |
4096 | tree new_decl = tsubst (decl, args, in_decl); | |
4097 | DECL_RESULT (tmpl) = new_decl; | |
4098 | TREE_TYPE (tmpl) = TREE_TYPE (new_decl); | |
4099 | return tmpl; | |
4100 | } | |
4101 | ||
f84b4be9 | 4102 | DECL_CONTEXT (tmpl) = tsubst (DECL_CONTEXT (t), |
f7d98d58 | 4103 | args, in_decl); |
f84b4be9 | 4104 | DECL_CLASS_CONTEXT (tmpl) = tsubst (DECL_CLASS_CONTEXT (t), |
f7d98d58 | 4105 | args, in_decl); |
98c1c668 | 4106 | DECL_TEMPLATE_INFO (tmpl) = build_tree_list (t, args); |
93cdc044 JM |
4107 | |
4108 | if (TREE_CODE (decl) == TYPE_DECL) | |
4109 | { | |
4110 | tree new_type = tsubst (TREE_TYPE (t), args, in_decl); | |
4111 | TREE_TYPE (tmpl) = new_type; | |
4112 | CLASSTYPE_TI_TEMPLATE (new_type) = tmpl; | |
4113 | DECL_RESULT (tmpl) = TYPE_MAIN_DECL (new_type); | |
4114 | } | |
4115 | else | |
4116 | { | |
4117 | tree new_decl = tsubst (decl, args, in_decl); | |
4118 | DECL_RESULT (tmpl) = new_decl; | |
4119 | DECL_TI_TEMPLATE (new_decl) = tmpl; | |
4120 | TREE_TYPE (tmpl) = TREE_TYPE (new_decl); | |
4121 | } | |
4122 | ||
27bb8339 | 4123 | DECL_TEMPLATE_INSTANTIATIONS (tmpl) = NULL_TREE; |
fee23f54 | 4124 | SET_DECL_IMPLICIT_INSTANTIATION (tmpl); |
98c1c668 JM |
4125 | |
4126 | /* The template parameters for this new template are all the | |
4127 | template parameters for the old template, except the | |
4128 | outermost level of parameters. */ | |
f84b4be9 JM |
4129 | for (new_parms = &DECL_TEMPLATE_PARMS (tmpl), |
4130 | parms = DECL_TEMPLATE_PARMS (t); | |
98c1c668 | 4131 | TREE_CHAIN (parms) != NULL_TREE; |
f84b4be9 JM |
4132 | new_parms = &(TREE_CHAIN (*new_parms)), |
4133 | parms = TREE_CHAIN (parms)) | |
4134 | { | |
4135 | tree new_vec = | |
4136 | make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms))); | |
4137 | int i; | |
4138 | ||
4139 | for (i = 0; i < TREE_VEC_LENGTH (new_vec); ++i) | |
4140 | { | |
4141 | tree default_value = | |
4142 | TREE_PURPOSE (TREE_VEC_ELT (TREE_VALUE (parms), i)); | |
4143 | tree parm_decl = | |
4144 | TREE_VALUE (TREE_VEC_ELT (TREE_VALUE (parms), i)); | |
4145 | ||
4146 | TREE_VEC_ELT (new_vec, i) | |
f7d98d58 JM |
4147 | = build_tree_list (tsubst (default_value, args, in_decl), |
4148 | tsubst (parm_decl, args, in_decl)); | |
f84b4be9 JM |
4149 | |
4150 | } | |
4151 | ||
4152 | *new_parms = | |
4153 | tree_cons (build_int_2 (0, | |
4154 | TREE_INT_CST_HIGH | |
4155 | (TREE_PURPOSE (parms)) - 1), | |
4156 | new_vec, | |
4157 | NULL_TREE); | |
4158 | } | |
98c1c668 | 4159 | |
93cdc044 JM |
4160 | if (PRIMARY_TEMPLATE_P (t)) |
4161 | TREE_TYPE (DECL_INNERMOST_TEMPLATE_PARMS (tmpl)) = tmpl; | |
4162 | ||
8d019cef | 4163 | /* We don't partially instantiate partial specializations. */ |
93cdc044 JM |
4164 | if (TREE_CODE (decl) == TYPE_DECL) |
4165 | return tmpl; | |
4166 | ||
75650646 MM |
4167 | /* What should we do with the specializations of this member |
4168 | template? Are they specializations of this new template, | |
4169 | or instantiations of the templates they previously were? | |
4170 | this new template? And where should their | |
4171 | DECL_TI_TEMPLATES point? */ | |
386b8a85 | 4172 | DECL_TEMPLATE_SPECIALIZATIONS (tmpl) = NULL_TREE; |
75650646 MM |
4173 | for (spec = DECL_TEMPLATE_SPECIALIZATIONS (t); |
4174 | spec != NULL_TREE; | |
4175 | spec = TREE_CHAIN (spec)) | |
4176 | { | |
4177 | /* It helps to consider example here. Consider: | |
4178 | ||
4179 | template <class T> | |
4180 | struct S { | |
4181 | template <class U> | |
4182 | void f(U u); | |
4183 | ||
4184 | template <> | |
4185 | void f(T* t) {} | |
4186 | }; | |
4187 | ||
4188 | Now, for example, we are instantiating S<int>::f(U u). | |
4189 | We want to make a template: | |
4190 | ||
4191 | template <class U> | |
4192 | void S<int>::f(U); | |
4193 | ||
4194 | It will have a specialization, for the case U = int*, of | |
4195 | the form: | |
4196 | ||
4197 | template <> | |
4198 | void S<int>::f<int*>(int*); | |
4199 | ||
4200 | This specialization will be an instantiation of | |
4201 | the specialization given in the declaration of S, with | |
4202 | argument list int*. */ | |
4203 | ||
4204 | tree fn = TREE_VALUE (spec); | |
4205 | tree spec_args; | |
4206 | tree new_fn; | |
4207 | ||
4208 | if (!DECL_TEMPLATE_SPECIALIZATION (fn)) | |
4209 | /* Instantiations are on the same list, but they're of | |
4210 | no concern to us. */ | |
4211 | continue; | |
4212 | ||
f7d98d58 | 4213 | spec_args = tsubst (DECL_TI_ARGS (fn), args, |
75650646 | 4214 | in_decl); |
f7d98d58 | 4215 | new_fn = tsubst (DECL_RESULT (fn), args, |
75650646 MM |
4216 | in_decl); |
4217 | DECL_TEMPLATE_SPECIALIZATIONS (tmpl) = | |
4218 | perm_tree_cons (spec_args, new_fn, | |
4219 | DECL_TEMPLATE_SPECIALIZATIONS (tmpl)); | |
4220 | } | |
4221 | ||
4222 | /* Record this partial instantiation. */ | |
4223 | register_specialization (tmpl, t, args); | |
4224 | ||
98c1c668 JM |
4225 | return tmpl; |
4226 | } | |
8d08fdba MS |
4227 | |
4228 | case FUNCTION_DECL: | |
4229 | { | |
5566b478 | 4230 | tree r = NULL_TREE; |
386b8a85 | 4231 | tree ctx; |
a221d52f | 4232 | tree argvec; |
f84b4be9 | 4233 | tree tmpl = NULL_TREE; |
5566b478 MS |
4234 | int member; |
4235 | ||
8d08fdba MS |
4236 | if (DECL_CONTEXT (t) != NULL_TREE |
4237 | && TREE_CODE_CLASS (TREE_CODE (DECL_CONTEXT (t))) == 't') | |
4238 | { | |
5566b478 MS |
4239 | if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t))) |
4240 | member = 2; | |
4241 | else | |
4242 | member = 1; | |
f7d98d58 JM |
4243 | ctx = tsubst (DECL_CLASS_CONTEXT (t), args, t); |
4244 | type = tsubst (type, args, in_decl); | |
5566b478 MS |
4245 | } |
4246 | else | |
4247 | { | |
4248 | member = 0; | |
4249 | ctx = NULL_TREE; | |
f7d98d58 | 4250 | type = tsubst (type, args, in_decl); |
5566b478 | 4251 | } |
8d08fdba | 4252 | |
f84b4be9 | 4253 | /* If we are instantiating a specialization, get the other args. */ |
5566b478 MS |
4254 | if (DECL_TEMPLATE_INFO (t) != NULL_TREE) |
4255 | { | |
f84b4be9 JM |
4256 | tree spec; |
4257 | ||
4258 | tmpl = DECL_TI_TEMPLATE (t); | |
a221d52f JM |
4259 | |
4260 | /* Start by getting the innermost args. */ | |
f7d98d58 | 4261 | argvec = tsubst (DECL_TI_ARGS (t), args, in_decl); |
a221d52f | 4262 | |
e6f1275f JM |
4263 | if (DECL_TEMPLATE_INFO (tmpl)) |
4264 | argvec = complete_template_args (tmpl, argvec, 0); | |
5566b478 | 4265 | |
f84b4be9 | 4266 | /* Do we already have this instantiation? */ |
a221d52f | 4267 | spec = retrieve_specialization (tmpl, argvec); |
75650646 MM |
4268 | if (spec) |
4269 | return spec; | |
5566b478 MS |
4270 | } |
4271 | ||
4272 | /* We do NOT check for matching decls pushed separately at this | |
4273 | point, as they may not represent instantiations of this | |
4274 | template, and in any case are considered separate under the | |
312e7d50 | 4275 | discrete model. Instead, see add_maybe_template. */ |
5566b478 MS |
4276 | |
4277 | r = copy_node (t); | |
4278 | copy_lang_decl (r); | |
e1467ff2 | 4279 | DECL_USE_TEMPLATE (r) = 0; |
5566b478 MS |
4280 | TREE_TYPE (r) = type; |
4281 | ||
4282 | DECL_CONTEXT (r) | |
f7d98d58 | 4283 | = tsubst (DECL_CONTEXT (t), args, t); |
5566b478 MS |
4284 | DECL_CLASS_CONTEXT (r) = ctx; |
4285 | ||
4286 | if (member && !strncmp (OPERATOR_TYPENAME_FORMAT, | |
4287 | IDENTIFIER_POINTER (DECL_NAME (r)), | |
4288 | sizeof (OPERATOR_TYPENAME_FORMAT) - 1)) | |
4289 | { | |
4290 | /* Type-conversion operator. Reconstruct the name, in | |
4291 | case it's the name of one of the template's parameters. */ | |
4292 | DECL_NAME (r) = build_typename_overload (TREE_TYPE (type)); | |
4293 | } | |
4294 | ||
5566b478 MS |
4295 | if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (t))) |
4296 | { | |
4297 | char *buf, *dbuf = build_overload_name (ctx, 1, 1); | |
4298 | int len = sizeof (DESTRUCTOR_DECL_PREFIX) - 1; | |
4299 | buf = (char *) alloca (strlen (dbuf) | |
4300 | + sizeof (DESTRUCTOR_DECL_PREFIX)); | |
4301 | bcopy (DESTRUCTOR_DECL_PREFIX, buf, len); | |
4302 | buf[len] = '\0'; | |
4303 | strcat (buf, dbuf); | |
4304 | DECL_ASSEMBLER_NAME (r) = get_identifier (buf); | |
8d08fdba | 4305 | } |
386b8a85 JM |
4306 | else |
4307 | { | |
4308 | /* Instantiations of template functions must be mangled | |
4309 | specially, in order to conform to 14.5.5.1 | |
4310 | [temp.over.link]. We use in_decl below rather than | |
4311 | DECL_TI_TEMPLATE (r) because the latter is set to | |
4312 | NULL_TREE in instantiate_decl. */ | |
4313 | tree tmpl; | |
4314 | tree arg_types; | |
4315 | ||
4316 | if (DECL_TEMPLATE_INFO (r)) | |
4317 | tmpl = DECL_TI_TEMPLATE (r); | |
4318 | else | |
4319 | tmpl = in_decl; | |
4320 | ||
4321 | /* tmpl will be NULL if this is a specialization of a | |
a221d52f | 4322 | member function of a template class. */ |
386b8a85 JM |
4323 | if (name_mangling_version < 1 |
4324 | || tmpl == NULL_TREE | |
4325 | || (member && !is_member_template (tmpl) | |
4326 | && !DECL_TEMPLATE_INFO (tmpl))) | |
4327 | { | |
4328 | arg_types = TYPE_ARG_TYPES (type); | |
4329 | if (member && TREE_CODE (type) == FUNCTION_TYPE) | |
4330 | arg_types = hash_tree_chain | |
4331 | (build_pointer_type (DECL_CONTEXT (r)), | |
4332 | arg_types); | |
4333 | ||
4334 | DECL_ASSEMBLER_NAME (r) | |
4335 | = build_decl_overload (DECL_NAME (r), arg_types, | |
4336 | member); | |
4337 | } | |
4338 | else | |
4339 | { | |
f84b4be9 | 4340 | tree tparms; |
75650646 MM |
4341 | tree targs; |
4342 | ||
4343 | if (!DECL_TEMPLATE_SPECIALIZATION (tmpl)) | |
4344 | { | |
a221d52f JM |
4345 | /* We pass the outermost template parameters to |
4346 | build_template_decl_overload, since the innermost | |
4347 | template parameters are still just template | |
4348 | parameters; there are no corresponding subsitution | |
4349 | arguments. Levels of parms that have been bound | |
4350 | before are not represented in DECL_TEMPLATE_PARMS. */ | |
75650646 | 4351 | tparms = DECL_TEMPLATE_PARMS (tmpl); |
75650646 MM |
4352 | while (tparms && TREE_CHAIN (tparms) != NULL_TREE) |
4353 | tparms = TREE_CHAIN (tparms); | |
4354 | ||
a221d52f | 4355 | targs = innermost_args (args, 0); |
75650646 MM |
4356 | } |
4357 | else | |
4358 | { | |
4359 | /* If the template is a specialization, then it is | |
4360 | a member template specialization. We have | |
4361 | something like: | |
4362 | ||
4363 | template <class T> struct S { | |
4364 | template <int i> void f(); | |
4365 | template <> void f<7>(); | |
4366 | }; | |
4367 | ||
4368 | and now we are forming S<double>::f<7>. | |
4369 | Therefore, the template parameters of interest | |
4370 | are those that are specialized by the template | |
4371 | (i.e., the int), not those we are using to | |
4372 | instantiate the template, i.e. the double. */ | |
4373 | tparms = DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (tmpl)); | |
4374 | targs = DECL_TI_ARGS (tmpl); | |
4375 | } | |
4376 | ||
386b8a85 JM |
4377 | my_friendly_assert (tparms != NULL_TREE |
4378 | && TREE_CODE (tparms) == TREE_LIST, | |
4379 | 0); | |
4380 | tparms = TREE_VALUE (tparms); | |
75650646 | 4381 | |
386b8a85 JM |
4382 | arg_types = TYPE_ARG_TYPES (TREE_TYPE (tmpl)); |
4383 | if (member && TREE_CODE (type) == FUNCTION_TYPE) | |
4384 | arg_types = hash_tree_chain | |
4385 | (build_pointer_type (DECL_CONTEXT (r)), | |
4386 | arg_types); | |
4387 | ||
4388 | DECL_ASSEMBLER_NAME (r) | |
4389 | = build_template_decl_overload | |
4390 | (DECL_NAME (r), arg_types, | |
4391 | TREE_TYPE (TREE_TYPE (tmpl)), | |
75650646 | 4392 | tparms, targs, member); |
386b8a85 JM |
4393 | } |
4394 | } | |
5566b478 MS |
4395 | DECL_RTL (r) = 0; |
4396 | make_decl_rtl (r, NULL_PTR, 1); | |
4397 | ||
f7d98d58 | 4398 | DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args, t); |
5566b478 MS |
4399 | DECL_MAIN_VARIANT (r) = r; |
4400 | DECL_RESULT (r) = NULL_TREE; | |
4401 | DECL_INITIAL (r) = NULL_TREE; | |
4402 | ||
4403 | TREE_STATIC (r) = 0; | |
75650646 | 4404 | TREE_PUBLIC (r) = TREE_PUBLIC (t); |
5566b478 MS |
4405 | DECL_EXTERNAL (r) = 1; |
4406 | DECL_INTERFACE_KNOWN (r) = 0; | |
4407 | DECL_DEFER_OUTPUT (r) = 0; | |
4408 | TREE_CHAIN (r) = NULL_TREE; | |
4409 | DECL_CHAIN (r) = NULL_TREE; | |
ff369548 | 4410 | DECL_PENDING_INLINE_INFO (r) = 0; |
b4c4a9ec | 4411 | TREE_USED (r) = 0; |
5566b478 MS |
4412 | |
4413 | if (IDENTIFIER_OPNAME_P (DECL_NAME (r))) | |
4414 | grok_op_properties (r, DECL_VIRTUAL_P (r), DECL_FRIEND_P (r)); | |
4415 | ||
5566b478 MS |
4416 | if (DECL_TEMPLATE_INFO (t) != NULL_TREE) |
4417 | { | |
5566b478 | 4418 | DECL_TEMPLATE_INFO (r) = perm_tree_cons (tmpl, argvec, NULL_TREE); |
5566b478 | 4419 | |
e1467ff2 MM |
4420 | /* If we're not using ANSI overloading, then we might have |
4421 | called duplicate_decls above, and gotten back an | |
4422 | preexisting version of this function. We treat such a | |
4423 | function as a specialization. Otherwise, we cleared | |
4424 | both TREE_STATIC and DECL_TEMPLATE_SPECIALIZATION, so | |
4425 | this condition will be false. */ | |
4426 | if (TREE_STATIC (r) || DECL_TEMPLATE_SPECIALIZATION (r)) | |
5566b478 MS |
4427 | SET_DECL_TEMPLATE_SPECIALIZATION (r); |
4428 | else | |
4429 | SET_DECL_IMPLICIT_INSTANTIATION (r); | |
4430 | ||
75650646 | 4431 | register_specialization (r, tmpl, argvec); |
8d08fdba | 4432 | } |
8d08fdba | 4433 | |
e92cc029 MS |
4434 | /* Like grokfndecl. If we don't do this, pushdecl will mess up our |
4435 | TREE_CHAIN because it doesn't find a previous decl. Sigh. */ | |
4436 | if (member | |
4437 | && IDENTIFIER_GLOBAL_VALUE (DECL_ASSEMBLER_NAME (r)) == NULL_TREE) | |
4438 | IDENTIFIER_GLOBAL_VALUE (DECL_ASSEMBLER_NAME (r)) = r; | |
4439 | ||
8d08fdba MS |
4440 | return r; |
4441 | } | |
4442 | ||
4443 | case PARM_DECL: | |
4444 | { | |
5566b478 MS |
4445 | tree r = copy_node (t); |
4446 | TREE_TYPE (r) = type; | |
db2767b6 MM |
4447 | if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX) |
4448 | DECL_INITIAL (r) = TREE_TYPE (r); | |
4449 | else | |
4450 | DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args, in_decl); | |
4451 | ||
5566b478 | 4452 | DECL_CONTEXT (r) = NULL_TREE; |
f83b0cb6 JM |
4453 | #ifdef PROMOTE_PROTOTYPES |
4454 | if ((TREE_CODE (type) == INTEGER_TYPE | |
4455 | || TREE_CODE (type) == ENUMERAL_TYPE) | |
4456 | && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)) | |
4457 | DECL_ARG_TYPE (r) = integer_type_node; | |
4458 | #endif | |
8d08fdba | 4459 | if (TREE_CHAIN (t)) |
f7d98d58 | 4460 | TREE_CHAIN (r) = tsubst (TREE_CHAIN (t), args, TREE_CHAIN (t)); |
8d08fdba MS |
4461 | return r; |
4462 | } | |
4463 | ||
5566b478 MS |
4464 | case FIELD_DECL: |
4465 | { | |
4466 | tree r = copy_node (t); | |
4467 | TREE_TYPE (r) = type; | |
4468 | copy_lang_decl (r); | |
4469 | #if 0 | |
f7d98d58 | 4470 | DECL_FIELD_CONTEXT (r) = tsubst (DECL_FIELD_CONTEXT (t), args, in_decl); |
5566b478 | 4471 | #endif |
f7d98d58 | 4472 | DECL_INITIAL (r) = tsubst_expr (DECL_INITIAL (t), args, in_decl); |
5566b478 MS |
4473 | TREE_CHAIN (r) = NULL_TREE; |
4474 | return r; | |
4475 | } | |
4476 | ||
4477 | case USING_DECL: | |
4478 | { | |
4479 | tree r = copy_node (t); | |
4480 | DECL_INITIAL (r) | |
f7d98d58 | 4481 | = tsubst_copy (DECL_INITIAL (t), args, in_decl); |
5566b478 MS |
4482 | TREE_CHAIN (r) = NULL_TREE; |
4483 | return r; | |
4484 | } | |
4485 | ||
4486 | case VAR_DECL: | |
4487 | { | |
4488 | tree r; | |
f7d98d58 | 4489 | tree ctx = tsubst_copy (DECL_CONTEXT (t), args, in_decl); |
5566b478 MS |
4490 | |
4491 | /* Do we already have this instantiation? */ | |
4492 | if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)) | |
4493 | { | |
4494 | tree tmpl = DECL_TI_TEMPLATE (t); | |
4495 | tree decls = DECL_TEMPLATE_INSTANTIATIONS (tmpl); | |
4496 | ||
4497 | for (; decls; decls = TREE_CHAIN (decls)) | |
4498 | if (DECL_CONTEXT (TREE_VALUE (decls)) == ctx) | |
4499 | return TREE_VALUE (decls); | |
4500 | } | |
4501 | ||
4502 | r = copy_node (t); | |
4503 | TREE_TYPE (r) = type; | |
4504 | DECL_CONTEXT (r) = ctx; | |
4505 | if (TREE_STATIC (r)) | |
4506 | DECL_ASSEMBLER_NAME (r) | |
4507 | = build_static_name (DECL_CONTEXT (r), DECL_NAME (r)); | |
d11ad92e MS |
4508 | |
4509 | /* Don't try to expand the initializer until someone tries to use | |
4510 | this variable; otherwise we run into circular dependencies. */ | |
4511 | DECL_INITIAL (r) = NULL_TREE; | |
5566b478 MS |
4512 | |
4513 | DECL_RTL (r) = 0; | |
4514 | DECL_SIZE (r) = 0; | |
4515 | ||
4516 | if (DECL_LANG_SPECIFIC (r)) | |
4517 | { | |
4518 | copy_lang_decl (r); | |
4519 | DECL_CLASS_CONTEXT (r) = DECL_CONTEXT (r); | |
4520 | } | |
4521 | ||
4522 | if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)) | |
4523 | { | |
4524 | tree tmpl = DECL_TI_TEMPLATE (t); | |
4525 | tree *declsp = &DECL_TEMPLATE_INSTANTIATIONS (tmpl); | |
f7d98d58 | 4526 | tree argvec = tsubst (DECL_TI_ARGS (t), args, in_decl); |
5566b478 MS |
4527 | |
4528 | DECL_TEMPLATE_INFO (r) = perm_tree_cons (tmpl, argvec, NULL_TREE); | |
4529 | *declsp = perm_tree_cons (argvec, r, *declsp); | |
4530 | SET_DECL_IMPLICIT_INSTANTIATION (r); | |
4531 | } | |
4532 | TREE_CHAIN (r) = NULL_TREE; | |
4533 | return r; | |
4534 | } | |
4535 | ||
4536 | case TYPE_DECL: | |
d2e5ee5c MS |
4537 | if (t == TYPE_NAME (TREE_TYPE (t))) |
4538 | return TYPE_NAME (type); | |
4539 | ||
5566b478 MS |
4540 | { |
4541 | tree r = copy_node (t); | |
4542 | TREE_TYPE (r) = type; | |
909e536a | 4543 | DECL_CONTEXT (r) = current_class_type; |
5566b478 MS |
4544 | TREE_CHAIN (r) = NULL_TREE; |
4545 | return r; | |
4546 | } | |
4547 | ||
8d08fdba MS |
4548 | case TREE_LIST: |
4549 | { | |
4550 | tree purpose, value, chain, result; | |
4551 | int via_public, via_virtual, via_protected; | |
4552 | ||
4553 | if (t == void_list_node) | |
4554 | return t; | |
4555 | ||
4556 | via_public = TREE_VIA_PUBLIC (t); | |
4557 | via_protected = TREE_VIA_PROTECTED (t); | |
4558 | via_virtual = TREE_VIA_VIRTUAL (t); | |
4559 | ||
4560 | purpose = TREE_PURPOSE (t); | |
4561 | if (purpose) | |
f7d98d58 | 4562 | purpose = tsubst (purpose, args, in_decl); |
8d08fdba MS |
4563 | value = TREE_VALUE (t); |
4564 | if (value) | |
f7d98d58 | 4565 | value = tsubst (value, args, in_decl); |
8d08fdba MS |
4566 | chain = TREE_CHAIN (t); |
4567 | if (chain && chain != void_type_node) | |
f7d98d58 | 4568 | chain = tsubst (chain, args, in_decl); |
8d08fdba MS |
4569 | if (purpose == TREE_PURPOSE (t) |
4570 | && value == TREE_VALUE (t) | |
4571 | && chain == TREE_CHAIN (t)) | |
4572 | return t; | |
4573 | result = hash_tree_cons (via_public, via_virtual, via_protected, | |
4574 | purpose, value, chain); | |
4575 | TREE_PARMLIST (result) = TREE_PARMLIST (t); | |
4576 | return result; | |
4577 | } | |
4578 | case TREE_VEC: | |
5566b478 MS |
4579 | if (type != NULL_TREE) |
4580 | { | |
85b71cf2 JM |
4581 | /* A binfo node. */ |
4582 | ||
5566b478 MS |
4583 | t = copy_node (t); |
4584 | ||
4585 | if (type == TREE_TYPE (t)) | |
4586 | return t; | |
4587 | ||
4588 | TREE_TYPE (t) = complete_type (type); | |
6633d636 MS |
4589 | if (IS_AGGR_TYPE (type)) |
4590 | { | |
4591 | BINFO_VTABLE (t) = TYPE_BINFO_VTABLE (type); | |
4592 | BINFO_VIRTUALS (t) = TYPE_BINFO_VIRTUALS (type); | |
4593 | if (TYPE_BINFO_BASETYPES (type) != NULL_TREE) | |
4594 | BINFO_BASETYPES (t) = copy_node (TYPE_BINFO_BASETYPES (type)); | |
4595 | } | |
5566b478 MS |
4596 | return t; |
4597 | } | |
85b71cf2 JM |
4598 | |
4599 | /* Otherwise, a vector of template arguments. */ | |
8d08fdba MS |
4600 | { |
4601 | int len = TREE_VEC_LENGTH (t), need_new = 0, i; | |
4602 | tree *elts = (tree *) alloca (len * sizeof (tree)); | |
5566b478 | 4603 | |
1daa5dd8 | 4604 | bzero ((char *) elts, len * sizeof (tree)); |
8d08fdba MS |
4605 | |
4606 | for (i = 0; i < len; i++) | |
4607 | { | |
00d3396f | 4608 | elts[i] = maybe_fold_nontype_arg |
f7d98d58 | 4609 | (tsubst_expr (TREE_VEC_ELT (t, i), args, in_decl)); |
85b71cf2 | 4610 | |
8d08fdba MS |
4611 | if (elts[i] != TREE_VEC_ELT (t, i)) |
4612 | need_new = 1; | |
4613 | } | |
4614 | ||
4615 | if (!need_new) | |
4616 | return t; | |
4617 | ||
4618 | t = make_tree_vec (len); | |
4619 | for (i = 0; i < len; i++) | |
4620 | TREE_VEC_ELT (t, i) = elts[i]; | |
5566b478 | 4621 | |
8d08fdba MS |
4622 | return t; |
4623 | } | |
4624 | case POINTER_TYPE: | |
4625 | case REFERENCE_TYPE: | |
4626 | { | |
4627 | tree r; | |
4628 | enum tree_code code; | |
79a7c7fa | 4629 | |
8d08fdba MS |
4630 | if (type == TREE_TYPE (t)) |
4631 | return t; | |
4632 | ||
4633 | code = TREE_CODE (t); | |
79a7c7fa JM |
4634 | if (TREE_CODE (type) == REFERENCE_TYPE) |
4635 | { | |
4636 | static int last_line = 0; | |
4637 | static char* last_file = 0; | |
4638 | ||
4639 | /* We keep track of the last time we issued this error | |
4640 | message to avoid spewing a ton of messages during a | |
4641 | single bad template instantiation. */ | |
4642 | if (last_line != lineno || | |
4643 | last_file != input_filename) | |
4644 | { | |
4645 | cp_error ("cannot form type %s to reference type %T during template instantiation", | |
4646 | (code == POINTER_TYPE) ? "pointer" : "reference", | |
4647 | type); | |
4648 | last_line = lineno; | |
4649 | last_file = input_filename; | |
4650 | } | |
4651 | ||
4652 | /* Use the underlying type in an attempt at error | |
4653 | recovery; maybe the user meant vector<int> and wrote | |
4654 | vector<int&>, or some such. */ | |
4655 | if (code == REFERENCE_TYPE) | |
4656 | r = type; | |
4657 | else | |
4658 | r = build_pointer_type (TREE_TYPE (type)); | |
4659 | } | |
4660 | else if (code == POINTER_TYPE) | |
8d08fdba MS |
4661 | r = build_pointer_type (type); |
4662 | else | |
4663 | r = build_reference_type (type); | |
f376e137 | 4664 | r = cp_build_type_variant (r, TYPE_READONLY (t), TYPE_VOLATILE (t)); |
79a7c7fa | 4665 | |
8d08fdba MS |
4666 | /* Will this ever be needed for TYPE_..._TO values? */ |
4667 | layout_type (r); | |
4668 | return r; | |
4669 | } | |
a4443a08 MS |
4670 | case OFFSET_TYPE: |
4671 | return build_offset_type | |
f7d98d58 | 4672 | (tsubst (TYPE_OFFSET_BASETYPE (t), args, in_decl), type); |
8d08fdba MS |
4673 | case FUNCTION_TYPE: |
4674 | case METHOD_TYPE: | |
4675 | { | |
75b0bbce | 4676 | tree values = TYPE_ARG_TYPES (t); |
8d08fdba | 4677 | tree context = TYPE_CONTEXT (t); |
c11b6f21 MS |
4678 | tree raises = TYPE_RAISES_EXCEPTIONS (t); |
4679 | tree fntype; | |
8d08fdba MS |
4680 | |
4681 | /* Don't bother recursing if we know it won't change anything. */ | |
4682 | if (values != void_list_node) | |
5566b478 MS |
4683 | { |
4684 | /* This should probably be rewritten to use hash_tree_cons for | |
4685 | the memory savings. */ | |
4686 | tree first = NULL_TREE; | |
a703fb38 | 4687 | tree last = NULL_TREE; |
5566b478 MS |
4688 | |
4689 | for (; values && values != void_list_node; | |
4690 | values = TREE_CHAIN (values)) | |
4691 | { | |
f7da6097 | 4692 | tree value = TYPE_MAIN_VARIANT (type_decays_to |
f7d98d58 | 4693 | (tsubst (TREE_VALUE (values), args, in_decl))); |
de22184b MS |
4694 | /* Don't instantiate default args unless they are used. |
4695 | Handle it in build_over_call instead. */ | |
4696 | tree purpose = TREE_PURPOSE (values); | |
5566b478 MS |
4697 | tree x = build_tree_list (purpose, value); |
4698 | ||
4699 | if (first) | |
4700 | TREE_CHAIN (last) = x; | |
4701 | else | |
4702 | first = x; | |
4703 | last = x; | |
4704 | } | |
4705 | ||
4706 | if (values == void_list_node) | |
4707 | TREE_CHAIN (last) = void_list_node; | |
4708 | ||
4709 | values = first; | |
4710 | } | |
8d08fdba | 4711 | if (context) |
f7d98d58 | 4712 | context = tsubst (context, args, in_decl); |
8d08fdba MS |
4713 | /* Could also optimize cases where return value and |
4714 | values have common elements (e.g., T min(const &T, const T&). */ | |
4715 | ||
4716 | /* If the above parameters haven't changed, just return the type. */ | |
4717 | if (type == TREE_TYPE (t) | |
4718 | && values == TYPE_VALUES (t) | |
4719 | && context == TYPE_CONTEXT (t)) | |
4720 | return t; | |
4721 | ||
4722 | /* Construct a new type node and return it. */ | |
4723 | if (TREE_CODE (t) == FUNCTION_TYPE | |
4724 | && context == NULL_TREE) | |
4725 | { | |
c11b6f21 | 4726 | fntype = build_function_type (type, values); |
8d08fdba MS |
4727 | } |
4728 | else if (context == NULL_TREE) | |
4729 | { | |
4730 | tree base = tsubst (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t))), | |
f7d98d58 | 4731 | args, in_decl); |
c11b6f21 MS |
4732 | fntype = build_cplus_method_type (base, type, |
4733 | TREE_CHAIN (values)); | |
8d08fdba MS |
4734 | } |
4735 | else | |
4736 | { | |
c11b6f21 MS |
4737 | fntype = make_node (TREE_CODE (t)); |
4738 | TREE_TYPE (fntype) = type; | |
4739 | TYPE_CONTEXT (fntype) = context; | |
4740 | TYPE_VALUES (fntype) = values; | |
4741 | TYPE_SIZE (fntype) = TYPE_SIZE (t); | |
4742 | TYPE_ALIGN (fntype) = TYPE_ALIGN (t); | |
4743 | TYPE_MODE (fntype) = TYPE_MODE (t); | |
8d08fdba | 4744 | if (TYPE_METHOD_BASETYPE (t)) |
c11b6f21 | 4745 | TYPE_METHOD_BASETYPE (fntype) = tsubst (TYPE_METHOD_BASETYPE (t), |
f7d98d58 | 4746 | args, in_decl); |
8d08fdba MS |
4747 | /* Need to generate hash value. */ |
4748 | my_friendly_abort (84); | |
4749 | } | |
c11b6f21 MS |
4750 | fntype = build_type_variant (fntype, |
4751 | TYPE_READONLY (t), | |
4752 | TYPE_VOLATILE (t)); | |
4753 | if (raises) | |
4754 | { | |
f7d98d58 | 4755 | raises = tsubst (raises, args, in_decl); |
c11b6f21 MS |
4756 | fntype = build_exception_variant (fntype, raises); |
4757 | } | |
4758 | return fntype; | |
8d08fdba MS |
4759 | } |
4760 | case ARRAY_TYPE: | |
4761 | { | |
f7d98d58 | 4762 | tree domain = tsubst (TYPE_DOMAIN (t), args, in_decl); |
8d08fdba MS |
4763 | tree r; |
4764 | if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t)) | |
4765 | return t; | |
4766 | r = build_cplus_array_type (type, domain); | |
4767 | return r; | |
4768 | } | |
4769 | ||
8d08fdba | 4770 | case PLUS_EXPR: |
5566b478 | 4771 | case MINUS_EXPR: |
8d08fdba | 4772 | return fold (build (TREE_CODE (t), TREE_TYPE (t), |
f7d98d58 JM |
4773 | tsubst (TREE_OPERAND (t, 0), args, in_decl), |
4774 | tsubst (TREE_OPERAND (t, 1), args, in_decl))); | |
8d08fdba MS |
4775 | |
4776 | case NEGATE_EXPR: | |
4777 | case NOP_EXPR: | |
4778 | return fold (build1 (TREE_CODE (t), TREE_TYPE (t), | |
f7d98d58 | 4779 | tsubst (TREE_OPERAND (t, 0), args, in_decl))); |
8d08fdba | 4780 | |
5566b478 MS |
4781 | case TYPENAME_TYPE: |
4782 | { | |
f7d98d58 | 4783 | tree ctx = tsubst (TYPE_CONTEXT (t), args, in_decl); |
b2b7d40a JM |
4784 | tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args, in_decl); |
4785 | f = make_typename_type (ctx, f); | |
5566b478 MS |
4786 | return cp_build_type_variant |
4787 | (f, TYPE_READONLY (f) || TYPE_READONLY (t), | |
4788 | TYPE_VOLATILE (f) || TYPE_VOLATILE (t)); | |
4789 | } | |
4790 | ||
4791 | case INDIRECT_REF: | |
4792 | return make_pointer_declarator | |
f7d98d58 | 4793 | (type, tsubst (TREE_OPERAND (t, 0), args, in_decl)); |
5566b478 MS |
4794 | |
4795 | case ADDR_EXPR: | |
4796 | return make_reference_declarator | |
f7d98d58 | 4797 | (type, tsubst (TREE_OPERAND (t, 0), args, in_decl)); |
5566b478 MS |
4798 | |
4799 | case ARRAY_REF: | |
4800 | return build_parse_node | |
f7d98d58 JM |
4801 | (ARRAY_REF, tsubst (TREE_OPERAND (t, 0), args, in_decl), |
4802 | tsubst_expr (TREE_OPERAND (t, 1), args, in_decl)); | |
5566b478 MS |
4803 | |
4804 | case CALL_EXPR: | |
c11b6f21 | 4805 | return make_call_declarator |
f7d98d58 JM |
4806 | (tsubst (TREE_OPERAND (t, 0), args, in_decl), |
4807 | tsubst (TREE_OPERAND (t, 1), args, in_decl), | |
c11b6f21 | 4808 | TREE_OPERAND (t, 2), |
f7d98d58 | 4809 | tsubst (TREE_TYPE (t), args, in_decl)); |
5566b478 | 4810 | |
fc378698 MS |
4811 | case SCOPE_REF: |
4812 | return build_parse_node | |
f7d98d58 JM |
4813 | (TREE_CODE (t), tsubst (TREE_OPERAND (t, 0), args, in_decl), |
4814 | tsubst (TREE_OPERAND (t, 1), args, in_decl)); | |
fc378698 | 4815 | |
8d08fdba | 4816 | default: |
5566b478 | 4817 | sorry ("use of `%s' in template", |
8d08fdba MS |
4818 | tree_code_name [(int) TREE_CODE (t)]); |
4819 | return error_mark_node; | |
4820 | } | |
4821 | } | |
4822 | ||
5566b478 MS |
4823 | void |
4824 | do_pushlevel () | |
4825 | { | |
4826 | emit_line_note (input_filename, lineno); | |
4827 | pushlevel (0); | |
4828 | clear_last_expr (); | |
4829 | push_momentary (); | |
4830 | expand_start_bindings (0); | |
4831 | } | |
4832 | ||
8d08fdba | 4833 | tree |
5566b478 | 4834 | do_poplevel () |
8d08fdba | 4835 | { |
5566b478 | 4836 | tree t; |
a703fb38 | 4837 | int saved_warn_unused = 0; |
8d08fdba | 4838 | |
85b71cf2 JM |
4839 | if (processing_template_decl) |
4840 | { | |
4841 | saved_warn_unused = warn_unused; | |
4842 | warn_unused = 0; | |
4843 | } | |
8baa713c | 4844 | expand_end_bindings (getdecls (), kept_level_p (), 0); |
85b71cf2 JM |
4845 | if (processing_template_decl) |
4846 | warn_unused = saved_warn_unused; | |
5566b478 MS |
4847 | t = poplevel (kept_level_p (), 1, 0); |
4848 | pop_momentary (); | |
4849 | return t; | |
4850 | } | |
8d08fdba | 4851 | |
00d3396f JM |
4852 | /* Like tsubst, but deals with expressions. This function just replaces |
4853 | template parms; to finish processing the resultant expression, use | |
4854 | tsubst_expr. */ | |
4855 | ||
5566b478 | 4856 | tree |
f7d98d58 | 4857 | tsubst_copy (t, args, in_decl) |
98c1c668 | 4858 | tree t, args; |
5566b478 MS |
4859 | tree in_decl; |
4860 | { | |
4861 | enum tree_code code; | |
8d08fdba | 4862 | |
5566b478 MS |
4863 | if (t == NULL_TREE || t == error_mark_node) |
4864 | return t; | |
4865 | ||
4866 | code = TREE_CODE (t); | |
b7484fbe | 4867 | |
5566b478 MS |
4868 | switch (code) |
4869 | { | |
4870 | case PARM_DECL: | |
4871 | return do_identifier (DECL_NAME (t), 0); | |
4872 | ||
4873 | case CONST_DECL: | |
4874 | case FIELD_DECL: | |
4875 | if (DECL_CONTEXT (t)) | |
4876 | { | |
0978790f JM |
4877 | tree ctx; |
4878 | if (TREE_CODE (DECL_CONTEXT (t)) == FUNCTION_DECL) | |
b87692e5 | 4879 | return lookup_name (DECL_NAME (t), 0); |
0978790f JM |
4880 | |
4881 | ctx = tsubst (DECL_CONTEXT (t), args, in_decl); | |
4882 | if (ctx != DECL_CONTEXT (t)) | |
5566b478 MS |
4883 | return lookup_field (ctx, DECL_NAME (t), 0, 0); |
4884 | } | |
4885 | return t; | |
4886 | ||
4887 | case VAR_DECL: | |
4888 | case FUNCTION_DECL: | |
4889 | if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)) | |
f7d98d58 | 4890 | t = tsubst (t, args, in_decl); |
5566b478 MS |
4891 | mark_used (t); |
4892 | return t; | |
4893 | ||
98c1c668 JM |
4894 | case TEMPLATE_DECL: |
4895 | if (is_member_template (t)) | |
f7d98d58 | 4896 | return tsubst (t, args, in_decl); |
98c1c668 JM |
4897 | else |
4898 | return t; | |
4899 | ||
5566b478 MS |
4900 | #if 0 |
4901 | case IDENTIFIER_NODE: | |
4902 | return do_identifier (t, 0); | |
4903 | #endif | |
4904 | ||
4905 | case CAST_EXPR: | |
4906 | case REINTERPRET_CAST_EXPR: | |
e92cc029 MS |
4907 | case CONST_CAST_EXPR: |
4908 | case STATIC_CAST_EXPR: | |
4909 | case DYNAMIC_CAST_EXPR: | |
5566b478 | 4910 | return build1 |
f7d98d58 JM |
4911 | (code, tsubst (TREE_TYPE (t), args, in_decl), |
4912 | tsubst_copy (TREE_OPERAND (t, 0), args, in_decl)); | |
5566b478 MS |
4913 | |
4914 | case INDIRECT_REF: | |
4915 | case PREDECREMENT_EXPR: | |
4916 | case PREINCREMENT_EXPR: | |
4917 | case POSTDECREMENT_EXPR: | |
4918 | case POSTINCREMENT_EXPR: | |
4919 | case NEGATE_EXPR: | |
4920 | case TRUTH_NOT_EXPR: | |
b87692e5 | 4921 | case BIT_NOT_EXPR: |
5566b478 MS |
4922 | case ADDR_EXPR: |
4923 | case CONVERT_EXPR: /* Unary + */ | |
4924 | case SIZEOF_EXPR: | |
abff8e06 | 4925 | case ALIGNOF_EXPR: |
5566b478 | 4926 | case ARROW_EXPR: |
fc378698 | 4927 | case THROW_EXPR: |
5156628f | 4928 | case TYPEID_EXPR: |
5566b478 MS |
4929 | return build1 |
4930 | (code, NULL_TREE, | |
f7d98d58 | 4931 | tsubst_copy (TREE_OPERAND (t, 0), args, in_decl)); |
5566b478 MS |
4932 | |
4933 | case PLUS_EXPR: | |
4934 | case MINUS_EXPR: | |
4935 | case MULT_EXPR: | |
4936 | case TRUNC_DIV_EXPR: | |
4937 | case CEIL_DIV_EXPR: | |
4938 | case FLOOR_DIV_EXPR: | |
4939 | case ROUND_DIV_EXPR: | |
4940 | case EXACT_DIV_EXPR: | |
4941 | case BIT_AND_EXPR: | |
4942 | case BIT_ANDTC_EXPR: | |
4943 | case BIT_IOR_EXPR: | |
4944 | case BIT_XOR_EXPR: | |
4945 | case TRUNC_MOD_EXPR: | |
4946 | case FLOOR_MOD_EXPR: | |
4947 | case TRUTH_ANDIF_EXPR: | |
4948 | case TRUTH_ORIF_EXPR: | |
4949 | case TRUTH_AND_EXPR: | |
4950 | case TRUTH_OR_EXPR: | |
4951 | case RSHIFT_EXPR: | |
4952 | case LSHIFT_EXPR: | |
4953 | case RROTATE_EXPR: | |
4954 | case LROTATE_EXPR: | |
4955 | case EQ_EXPR: | |
4956 | case NE_EXPR: | |
4957 | case MAX_EXPR: | |
4958 | case MIN_EXPR: | |
4959 | case LE_EXPR: | |
4960 | case GE_EXPR: | |
4961 | case LT_EXPR: | |
4962 | case GT_EXPR: | |
4963 | case COMPONENT_REF: | |
4964 | case ARRAY_REF: | |
4965 | case COMPOUND_EXPR: | |
4966 | case SCOPE_REF: | |
4967 | case DOTSTAR_EXPR: | |
4968 | case MEMBER_REF: | |
4969 | return build_nt | |
f7d98d58 JM |
4970 | (code, tsubst_copy (TREE_OPERAND (t, 0), args, in_decl), |
4971 | tsubst_copy (TREE_OPERAND (t, 1), args, in_decl)); | |
5566b478 MS |
4972 | |
4973 | case CALL_EXPR: | |
4974 | { | |
4975 | tree fn = TREE_OPERAND (t, 0); | |
f84b4be9 | 4976 | if (is_overloaded_fn (fn)) |
f7d98d58 | 4977 | fn = tsubst_copy (get_first_fn (fn), args, in_decl); |
5566b478 | 4978 | else |
f84b4be9 | 4979 | /* Sometimes FN is a LOOKUP_EXPR. */ |
f7d98d58 | 4980 | fn = tsubst_copy (fn, args, in_decl); |
5566b478 | 4981 | return build_nt |
f7d98d58 | 4982 | (code, fn, tsubst_copy (TREE_OPERAND (t, 1), args, in_decl), |
5566b478 MS |
4983 | NULL_TREE); |
4984 | } | |
4985 | ||
4986 | case METHOD_CALL_EXPR: | |
4987 | { | |
4988 | tree name = TREE_OPERAND (t, 0); | |
4989 | if (TREE_CODE (name) == BIT_NOT_EXPR) | |
4990 | { | |
f7d98d58 | 4991 | name = tsubst_copy (TREE_OPERAND (name, 0), args, in_decl); |
7bae46f4 JM |
4992 | if (TREE_CODE (name) != IDENTIFIER_NODE) |
4993 | name = TYPE_MAIN_VARIANT (name); | |
4994 | name = build1 (BIT_NOT_EXPR, NULL_TREE, name); | |
5566b478 MS |
4995 | } |
4996 | else if (TREE_CODE (name) == SCOPE_REF | |
4997 | && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR) | |
4998 | { | |
f7d98d58 | 4999 | tree base = tsubst_copy (TREE_OPERAND (name, 0), args, in_decl); |
5566b478 | 5000 | name = TREE_OPERAND (name, 1); |
f7d98d58 | 5001 | name = tsubst_copy (TREE_OPERAND (name, 0), args, in_decl); |
11686454 JM |
5002 | if (TREE_CODE (name) != IDENTIFIER_NODE) |
5003 | name = TYPE_MAIN_VARIANT (name); | |
5004 | name = build1 (BIT_NOT_EXPR, NULL_TREE, name); | |
5566b478 MS |
5005 | name = build_nt (SCOPE_REF, base, name); |
5006 | } | |
5007 | else | |
f7d98d58 | 5008 | name = tsubst_copy (TREE_OPERAND (t, 0), args, in_decl); |
5566b478 | 5009 | return build_nt |
f7d98d58 JM |
5010 | (code, name, tsubst_copy (TREE_OPERAND (t, 1), args, in_decl), |
5011 | tsubst_copy (TREE_OPERAND (t, 2), args, in_decl), | |
5566b478 MS |
5012 | NULL_TREE); |
5013 | } | |
5014 | ||
67da3287 | 5015 | case BIND_EXPR: |
5566b478 MS |
5016 | case COND_EXPR: |
5017 | case MODOP_EXPR: | |
67da3287 MM |
5018 | { |
5019 | tree r = build_nt | |
f7d98d58 JM |
5020 | (code, tsubst_copy (TREE_OPERAND (t, 0), args, in_decl), |
5021 | tsubst_copy (TREE_OPERAND (t, 1), args, in_decl), | |
5022 | tsubst_copy (TREE_OPERAND (t, 2), args, in_decl)); | |
67da3287 MM |
5023 | |
5024 | if (code == BIND_EXPR && !processing_template_decl) | |
5025 | { | |
5026 | /* This processing should really occur in tsubst_expr, | |
5027 | However, tsubst_expr does not recurse into expressions, | |
5028 | since it assumes that there aren't any statements | |
5029 | inside them. Instead, it simply calls | |
5030 | build_expr_from_tree. So, we need to expand the | |
5031 | BIND_EXPR here. */ | |
b4c4a9ec | 5032 | tree rtl_expr = begin_stmt_expr (); |
544c188d | 5033 | tree block = tsubst_expr (TREE_OPERAND (r, 1), args, in_decl); |
b4c4a9ec | 5034 | r = finish_stmt_expr (rtl_expr, block); |
67da3287 MM |
5035 | } |
5036 | ||
5037 | return r; | |
5038 | } | |
5566b478 MS |
5039 | |
5040 | case NEW_EXPR: | |
5041 | { | |
5042 | tree r = build_nt | |
f7d98d58 JM |
5043 | (code, tsubst_copy (TREE_OPERAND (t, 0), args, in_decl), |
5044 | tsubst_copy (TREE_OPERAND (t, 1), args, in_decl), | |
5045 | tsubst_copy (TREE_OPERAND (t, 2), args, in_decl)); | |
5566b478 MS |
5046 | NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t); |
5047 | return r; | |
5048 | } | |
5049 | ||
5050 | case DELETE_EXPR: | |
5051 | { | |
5052 | tree r = build_nt | |
f7d98d58 JM |
5053 | (code, tsubst_copy (TREE_OPERAND (t, 0), args, in_decl), |
5054 | tsubst_copy (TREE_OPERAND (t, 1), args, in_decl)); | |
5566b478 MS |
5055 | DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t); |
5056 | DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t); | |
5057 | return r; | |
5058 | } | |
5059 | ||
386b8a85 JM |
5060 | case TEMPLATE_ID_EXPR: |
5061 | { | |
00d3396f | 5062 | /* Substituted template arguments */ |
f7d98d58 | 5063 | tree targs = tsubst_copy (TREE_OPERAND (t, 1), args, in_decl); |
00d3396f JM |
5064 | tree chain; |
5065 | for (chain = targs; chain; chain = TREE_CHAIN (chain)) | |
5066 | TREE_VALUE (chain) = maybe_fold_nontype_arg (TREE_VALUE (chain)); | |
5067 | ||
5068 | return lookup_template_function | |
f7d98d58 | 5069 | (tsubst_copy (TREE_OPERAND (t, 0), args, in_decl), targs); |
386b8a85 JM |
5070 | } |
5071 | ||
5566b478 MS |
5072 | case TREE_LIST: |
5073 | { | |
5074 | tree purpose, value, chain; | |
5075 | ||
5076 | if (t == void_list_node) | |
5077 | return t; | |
5078 | ||
5079 | purpose = TREE_PURPOSE (t); | |
5080 | if (purpose) | |
f7d98d58 | 5081 | purpose = tsubst_copy (purpose, args, in_decl); |
5566b478 MS |
5082 | value = TREE_VALUE (t); |
5083 | if (value) | |
f7d98d58 | 5084 | value = tsubst_copy (value, args, in_decl); |
5566b478 MS |
5085 | chain = TREE_CHAIN (t); |
5086 | if (chain && chain != void_type_node) | |
f7d98d58 | 5087 | chain = tsubst_copy (chain, args, in_decl); |
5566b478 MS |
5088 | if (purpose == TREE_PURPOSE (t) |
5089 | && value == TREE_VALUE (t) | |
5090 | && chain == TREE_CHAIN (t)) | |
5091 | return t; | |
5092 | return tree_cons (purpose, value, chain); | |
5093 | } | |
5094 | ||
5095 | case RECORD_TYPE: | |
5096 | case UNION_TYPE: | |
5097 | case ENUMERAL_TYPE: | |
5098 | case INTEGER_TYPE: | |
5099 | case TEMPLATE_TYPE_PARM: | |
73b0fce8 | 5100 | case TEMPLATE_TEMPLATE_PARM: |
f84b4be9 | 5101 | case TEMPLATE_PARM_INDEX: |
5566b478 MS |
5102 | case POINTER_TYPE: |
5103 | case REFERENCE_TYPE: | |
5104 | case OFFSET_TYPE: | |
5105 | case FUNCTION_TYPE: | |
5106 | case METHOD_TYPE: | |
5107 | case ARRAY_TYPE: | |
5108 | case TYPENAME_TYPE: | |
f84b4be9 | 5109 | case TYPE_DECL: |
f7d98d58 | 5110 | return tsubst (t, args, in_decl); |
5566b478 | 5111 | |
e92cc029 MS |
5112 | case IDENTIFIER_NODE: |
5113 | if (IDENTIFIER_TYPENAME_P (t)) | |
5114 | return build_typename_overload | |
f7d98d58 | 5115 | (tsubst (TREE_TYPE (t), args, in_decl)); |
e92cc029 MS |
5116 | else |
5117 | return t; | |
5118 | ||
5156628f MS |
5119 | case CONSTRUCTOR: |
5120 | return build | |
f7d98d58 JM |
5121 | (CONSTRUCTOR, tsubst (TREE_TYPE (t), args, in_decl), NULL_TREE, |
5122 | tsubst_copy (CONSTRUCTOR_ELTS (t), args, in_decl)); | |
5156628f | 5123 | |
5566b478 MS |
5124 | default: |
5125 | return t; | |
5126 | } | |
5127 | } | |
5128 | ||
00d3396f JM |
5129 | /* Like tsubst_copy, but also does semantic processing and RTL expansion. */ |
5130 | ||
5566b478 | 5131 | tree |
f7d98d58 | 5132 | tsubst_expr (t, args, in_decl) |
98c1c668 | 5133 | tree t, args; |
5566b478 MS |
5134 | tree in_decl; |
5135 | { | |
5136 | if (t == NULL_TREE || t == error_mark_node) | |
5137 | return t; | |
5138 | ||
5156628f | 5139 | if (processing_template_decl) |
f7d98d58 | 5140 | return tsubst_copy (t, args, in_decl); |
5566b478 MS |
5141 | |
5142 | switch (TREE_CODE (t)) | |
8d08fdba | 5143 | { |
5566b478 MS |
5144 | case RETURN_STMT: |
5145 | lineno = TREE_COMPLEXITY (t); | |
ad321293 MM |
5146 | finish_return_stmt (tsubst_expr (RETURN_EXPR (t), |
5147 | args, in_decl)); | |
5566b478 MS |
5148 | break; |
5149 | ||
5150 | case EXPR_STMT: | |
5151 | lineno = TREE_COMPLEXITY (t); | |
ad321293 MM |
5152 | finish_expr_stmt (tsubst_expr (EXPR_STMT_EXPR (t), |
5153 | args, in_decl)); | |
5566b478 MS |
5154 | break; |
5155 | ||
5156 | case DECL_STMT: | |
5157 | { | |
5158 | int i = suspend_momentary (); | |
67d743fe | 5159 | tree dcl, init; |
5566b478 MS |
5160 | |
5161 | lineno = TREE_COMPLEXITY (t); | |
5162 | emit_line_note (input_filename, lineno); | |
5163 | dcl = start_decl | |
f7d98d58 JM |
5164 | (tsubst (TREE_OPERAND (t, 0), args, in_decl), |
5165 | tsubst (TREE_OPERAND (t, 1), args, in_decl), | |
a1774733 | 5166 | TREE_OPERAND (t, 2) != 0, NULL_TREE, NULL_TREE); |
f7d98d58 | 5167 | init = tsubst_expr (TREE_OPERAND (t, 2), args, in_decl); |
5566b478 | 5168 | cp_finish_decl |
a0128b67 | 5169 | (dcl, init, NULL_TREE, 1, /*init ? LOOKUP_ONLYCONVERTING :*/ 0); |
5566b478 MS |
5170 | resume_momentary (i); |
5171 | return dcl; | |
5172 | } | |
8d08fdba | 5173 | |
5566b478 MS |
5174 | case FOR_STMT: |
5175 | { | |
5176 | tree tmp; | |
5566b478 | 5177 | lineno = TREE_COMPLEXITY (t); |
5566b478 | 5178 | |
ad321293 MM |
5179 | begin_for_stmt (); |
5180 | for (tmp = FOR_INIT_STMT (t); tmp; tmp = TREE_CHAIN (tmp)) | |
5181 | tsubst_expr (tmp, args, in_decl); | |
5182 | finish_for_init_stmt (NULL_TREE); | |
5183 | finish_for_cond (tsubst_expr (FOR_COND (t), args, | |
5184 | in_decl), | |
5185 | NULL_TREE); | |
5186 | tmp = tsubst_expr (FOR_EXPR (t), args, in_decl); | |
5187 | finish_for_expr (tmp, NULL_TREE); | |
5188 | tsubst_expr (FOR_BODY (t), args, in_decl); | |
5189 | finish_for_stmt (tmp, NULL_TREE); | |
5566b478 MS |
5190 | } |
5191 | break; | |
8d08fdba | 5192 | |
5566b478 MS |
5193 | case WHILE_STMT: |
5194 | { | |
5566b478 | 5195 | lineno = TREE_COMPLEXITY (t); |
ad321293 MM |
5196 | begin_while_stmt (); |
5197 | finish_while_stmt_cond (tsubst_expr (WHILE_COND (t), | |
5198 | args, in_decl), | |
5199 | NULL_TREE); | |
5200 | tsubst_expr (WHILE_BODY (t), args, in_decl); | |
5201 | finish_while_stmt (NULL_TREE); | |
5566b478 MS |
5202 | } |
5203 | break; | |
8d08fdba | 5204 | |
5566b478 MS |
5205 | case DO_STMT: |
5206 | { | |
5566b478 | 5207 | lineno = TREE_COMPLEXITY (t); |
ad321293 MM |
5208 | begin_do_stmt (); |
5209 | tsubst_expr (DO_BODY (t), args, in_decl); | |
5210 | finish_do_body (NULL_TREE); | |
5211 | finish_do_stmt (tsubst_expr (DO_COND (t), args, | |
5212 | in_decl), | |
5213 | NULL_TREE); | |
5566b478 MS |
5214 | } |
5215 | break; | |
a0a33927 | 5216 | |
5566b478 | 5217 | case IF_STMT: |
8d08fdba | 5218 | { |
5566b478 | 5219 | tree tmp; |
5566b478 MS |
5220 | |
5221 | lineno = TREE_COMPLEXITY (t); | |
ad321293 MM |
5222 | begin_if_stmt (); |
5223 | finish_if_stmt_cond (tsubst_expr (IF_COND (t), | |
5224 | args, in_decl), | |
5225 | NULL_TREE); | |
db5ae43f | 5226 | |
ad321293 | 5227 | if (tmp = THEN_CLAUSE (t), tmp) |
db5ae43f | 5228 | { |
f7d98d58 | 5229 | tsubst_expr (tmp, args, in_decl); |
ad321293 | 5230 | finish_then_clause (NULL_TREE); |
db5ae43f MS |
5231 | } |
5232 | ||
ad321293 MM |
5233 | if (tmp = ELSE_CLAUSE (t), tmp) |
5234 | { | |
5235 | begin_else_clause (); | |
5236 | tsubst_expr (tmp, args, in_decl); | |
5237 | finish_else_clause (NULL_TREE); | |
5238 | } | |
8d08fdba | 5239 | |
ad321293 | 5240 | finish_if_stmt (); |
8d08fdba | 5241 | } |
5566b478 | 5242 | break; |
8d08fdba | 5243 | |
5566b478 MS |
5244 | case COMPOUND_STMT: |
5245 | { | |
ad321293 | 5246 | tree substmt; |
8d08fdba | 5247 | |
5566b478 | 5248 | lineno = TREE_COMPLEXITY (t); |
ad321293 MM |
5249 | begin_compound_stmt (COMPOUND_STMT_NO_SCOPE (t)); |
5250 | for (substmt = COMPOUND_BODY (t); | |
5251 | substmt != NULL_TREE; | |
5252 | substmt = TREE_CHAIN (substmt)) | |
f7d98d58 | 5253 | tsubst_expr (substmt, args, in_decl); |
ad321293 MM |
5254 | return finish_compound_stmt (COMPOUND_STMT_NO_SCOPE (t), |
5255 | NULL_TREE); | |
5566b478 MS |
5256 | } |
5257 | break; | |
8d08fdba | 5258 | |
5566b478 MS |
5259 | case BREAK_STMT: |
5260 | lineno = TREE_COMPLEXITY (t); | |
ad321293 | 5261 | finish_break_stmt (); |
5566b478 | 5262 | break; |
8d08fdba | 5263 | |
6467930b MS |
5264 | case CONTINUE_STMT: |
5265 | lineno = TREE_COMPLEXITY (t); | |
ad321293 | 5266 | finish_continue_stmt (); |
6467930b MS |
5267 | break; |
5268 | ||
5566b478 MS |
5269 | case SWITCH_STMT: |
5270 | { | |
5271 | tree val, tmp; | |
5566b478 MS |
5272 | |
5273 | lineno = TREE_COMPLEXITY (t); | |
ad321293 MM |
5274 | begin_switch_stmt (); |
5275 | val = tsubst_expr (SWITCH_COND (t), args, in_decl); | |
5276 | finish_switch_cond (val); | |
5566b478 MS |
5277 | |
5278 | if (tmp = TREE_OPERAND (t, 1), tmp) | |
f7d98d58 | 5279 | tsubst_expr (tmp, args, in_decl); |
8d08fdba | 5280 | |
ad321293 | 5281 | finish_switch_stmt (val, NULL_TREE); |
5566b478 MS |
5282 | } |
5283 | break; | |
5284 | ||
5285 | case CASE_LABEL: | |
ad321293 MM |
5286 | finish_case_label (tsubst_expr (CASE_LOW (t), args, in_decl), |
5287 | tsubst_expr (CASE_HIGH (t), args, in_decl)); | |
5566b478 MS |
5288 | break; |
5289 | ||
5290 | case LABEL_DECL: | |
5291 | t = define_label (DECL_SOURCE_FILE (t), DECL_SOURCE_LINE (t), | |
5292 | DECL_NAME (t)); | |
5293 | if (t) | |
5294 | expand_label (t); | |
5295 | break; | |
5296 | ||
5297 | case GOTO_STMT: | |
5298 | lineno = TREE_COMPLEXITY (t); | |
aa09da44 MM |
5299 | t = GOTO_DESTINATION (t); |
5300 | if (TREE_CODE (t) != IDENTIFIER_NODE) | |
5301 | /* Computed goto's must be tsubst'd into. On the other hand, | |
5302 | non-computed gotos must not be; the identifier in question | |
5303 | will have no binding. */ | |
5304 | t = tsubst_expr (t, args, in_decl); | |
5305 | finish_goto_stmt (t); | |
ad321293 MM |
5306 | break; |
5307 | ||
5308 | case ASM_STMT: | |
5309 | lineno = TREE_COMPLEXITY (t); | |
5310 | finish_asm_stmt (tsubst_expr (ASM_CV_QUAL (t), args, in_decl), | |
5311 | tsubst_expr (ASM_STRING (t), args, in_decl), | |
5312 | tsubst_expr (ASM_OUTPUTS (t), args, in_decl), | |
5313 | tsubst_expr (ASM_INPUTS (t), args, in_decl), | |
5314 | tsubst_expr (ASM_CLOBBERS (t), args, in_decl)); | |
5566b478 | 5315 | break; |
faf5394a MS |
5316 | |
5317 | case TRY_BLOCK: | |
5318 | lineno = TREE_COMPLEXITY (t); | |
ad321293 MM |
5319 | begin_try_block (); |
5320 | tsubst_expr (TRY_STMTS (t), args, in_decl); | |
5321 | finish_try_block (NULL_TREE); | |
faf5394a | 5322 | { |
ad321293 | 5323 | tree handler = TRY_HANDLERS (t); |
faf5394a | 5324 | for (; handler; handler = TREE_CHAIN (handler)) |
f7d98d58 | 5325 | tsubst_expr (handler, args, in_decl); |
faf5394a | 5326 | } |
ad321293 | 5327 | finish_handler_sequence (NULL_TREE); |
faf5394a MS |
5328 | break; |
5329 | ||
5330 | case HANDLER: | |
5331 | lineno = TREE_COMPLEXITY (t); | |
ad321293 MM |
5332 | begin_handler (); |
5333 | if (HANDLER_PARMS (t)) | |
faf5394a | 5334 | { |
ad321293 | 5335 | tree d = HANDLER_PARMS (t); |
faf5394a | 5336 | expand_start_catch_block |
f7d98d58 JM |
5337 | (tsubst (TREE_OPERAND (d, 1), args, in_decl), |
5338 | tsubst (TREE_OPERAND (d, 0), args, in_decl)); | |
faf5394a MS |
5339 | } |
5340 | else | |
5341 | expand_start_catch_block (NULL_TREE, NULL_TREE); | |
ad321293 MM |
5342 | finish_handler_parms (NULL_TREE); |
5343 | tsubst_expr (HANDLER_BODY (t), args, in_decl); | |
5344 | finish_handler (NULL_TREE); | |
faf5394a MS |
5345 | break; |
5346 | ||
b87692e5 MS |
5347 | case TAG_DEFN: |
5348 | lineno = TREE_COMPLEXITY (t); | |
5349 | t = TREE_TYPE (t); | |
5350 | if (TREE_CODE (t) == ENUMERAL_TYPE) | |
f7d98d58 | 5351 | tsubst_enum (t, args, NULL); |
b87692e5 MS |
5352 | break; |
5353 | ||
5566b478 | 5354 | default: |
f7d98d58 | 5355 | return build_expr_from_tree (tsubst_copy (t, args, in_decl)); |
5566b478 MS |
5356 | } |
5357 | return NULL_TREE; | |
8d08fdba MS |
5358 | } |
5359 | ||
5566b478 MS |
5360 | tree |
5361 | instantiate_template (tmpl, targ_ptr) | |
98c1c668 | 5362 | tree tmpl, targ_ptr; |
8d08fdba | 5363 | { |
5566b478 MS |
5364 | tree fndecl; |
5365 | int i, len; | |
5366 | struct obstack *old_fmp_obstack; | |
5367 | extern struct obstack *function_maybepermanent_obstack; | |
5368 | ||
386b8a85 JM |
5369 | my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 283); |
5370 | ||
f84b4be9 JM |
5371 | /* FIXME this won't work with member templates; we only have one level |
5372 | of args here. */ | |
386b8a85 JM |
5373 | if (DECL_FUNCTION_TEMPLATE_P (tmpl)) |
5374 | { | |
75650646 MM |
5375 | /* Check to see if we already have this specialization. */ |
5376 | tree spec = retrieve_specialization (tmpl, targ_ptr); | |
386b8a85 | 5377 | |
75650646 MM |
5378 | if (spec != NULL_TREE) |
5379 | return spec; | |
386b8a85 JM |
5380 | } |
5381 | ||
5566b478 MS |
5382 | push_obstacks (&permanent_obstack, &permanent_obstack); |
5383 | old_fmp_obstack = function_maybepermanent_obstack; | |
5384 | function_maybepermanent_obstack = &permanent_obstack; | |
8d08fdba | 5385 | |
98c1c668 | 5386 | len = DECL_NTPARMS (tmpl); |
8d08fdba | 5387 | |
5566b478 MS |
5388 | i = len; |
5389 | while (i--) | |
8d08fdba | 5390 | { |
98c1c668 | 5391 | tree t = TREE_VEC_ELT (targ_ptr, i); |
5566b478 MS |
5392 | if (TREE_CODE_CLASS (TREE_CODE (t)) == 't') |
5393 | { | |
5394 | tree nt = target_type (t); | |
ec255269 | 5395 | if (IS_AGGR_TYPE (nt) && decl_function_context (TYPE_MAIN_DECL (nt))) |
5566b478 MS |
5396 | { |
5397 | cp_error ("type `%T' composed from a local class is not a valid template-argument", t); | |
5398 | cp_error (" trying to instantiate `%D'", tmpl); | |
5399 | fndecl = error_mark_node; | |
5400 | goto out; | |
5401 | } | |
5402 | } | |
98c1c668 | 5403 | TREE_VEC_ELT (targ_ptr, i) = copy_to_permanent (t); |
8d08fdba | 5404 | } |
e66d884e | 5405 | targ_ptr = copy_to_permanent (targ_ptr); |
8d08fdba | 5406 | |
5566b478 | 5407 | /* substitute template parameters */ |
f7d98d58 | 5408 | fndecl = tsubst (DECL_RESULT (tmpl), targ_ptr, tmpl); |
8d08fdba | 5409 | |
824b9a4c MS |
5410 | if (flag_external_templates) |
5411 | add_pending_template (fndecl); | |
5412 | ||
5566b478 MS |
5413 | out: |
5414 | function_maybepermanent_obstack = old_fmp_obstack; | |
5415 | pop_obstacks (); | |
8d08fdba | 5416 | |
5566b478 | 5417 | return fndecl; |
8d08fdba | 5418 | } |
5566b478 MS |
5419 | |
5420 | /* Push the name of the class template into the scope of the instantiation. */ | |
8d08fdba MS |
5421 | |
5422 | void | |
5566b478 MS |
5423 | overload_template_name (type) |
5424 | tree type; | |
8d08fdba | 5425 | { |
5566b478 MS |
5426 | tree id = DECL_NAME (CLASSTYPE_TI_TEMPLATE (type)); |
5427 | tree decl; | |
8d08fdba | 5428 | |
5566b478 MS |
5429 | if (IDENTIFIER_CLASS_VALUE (id) |
5430 | && TREE_TYPE (IDENTIFIER_CLASS_VALUE (id)) == type) | |
5431 | return; | |
8d08fdba | 5432 | |
5566b478 MS |
5433 | decl = build_decl (TYPE_DECL, id, type); |
5434 | SET_DECL_ARTIFICIAL (decl); | |
5435 | pushdecl_class_level (decl); | |
8d08fdba MS |
5436 | } |
5437 | ||
d7684f2d | 5438 | |
956d6950 | 5439 | /* Like type_unification but designed specially to handle conversion |
d7684f2d MM |
5440 | operators. The EXTRA_FN_ARG, if any, is the type of an additional |
5441 | parameter to be added to the beginning of FN's parameter list. */ | |
98c1c668 JM |
5442 | |
5443 | int | |
d7684f2d MM |
5444 | fn_type_unification (fn, explicit_targs, targs, args, return_type, |
5445 | strict, extra_fn_arg) | |
386b8a85 | 5446 | tree fn, explicit_targs, targs, args, return_type; |
98c1c668 | 5447 | int strict; |
d7684f2d | 5448 | tree extra_fn_arg; |
98c1c668 | 5449 | { |
3b3ba9f0 | 5450 | int i; |
98c1c668 JM |
5451 | tree fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn)); |
5452 | tree decl_arg_types = args; | |
5453 | ||
5454 | my_friendly_assert (TREE_CODE (fn) == TEMPLATE_DECL, 0); | |
5455 | ||
5456 | if (IDENTIFIER_TYPENAME_P (DECL_NAME (fn))) | |
5457 | { | |
5458 | /* This is a template conversion operator. Use the return types | |
5459 | as well as the argument types. */ | |
e66d884e | 5460 | fn_arg_types = scratch_tree_cons (NULL_TREE, |
d7684f2d MM |
5461 | TREE_TYPE (TREE_TYPE (fn)), |
5462 | fn_arg_types); | |
e66d884e | 5463 | decl_arg_types = scratch_tree_cons (NULL_TREE, |
d7684f2d MM |
5464 | return_type, |
5465 | decl_arg_types); | |
98c1c668 JM |
5466 | } |
5467 | ||
d7684f2d MM |
5468 | if (extra_fn_arg != NULL_TREE) |
5469 | fn_arg_types = scratch_tree_cons (NULL_TREE, extra_fn_arg, | |
5470 | fn_arg_types); | |
5471 | ||
7cd55873 MM |
5472 | /* We allow incomplete unification without an error message here |
5473 | because the standard doesn't seem to explicitly prohibit it. Our | |
5474 | callers must be ready to deal with unification failures in any | |
5475 | event. */ | |
98c1c668 | 5476 | i = type_unification (DECL_INNERMOST_TEMPLATE_PARMS (fn), |
050367a3 | 5477 | targs, |
98c1c668 JM |
5478 | fn_arg_types, |
5479 | decl_arg_types, | |
386b8a85 | 5480 | explicit_targs, |
7cd55873 | 5481 | strict, 1); |
98c1c668 JM |
5482 | |
5483 | return i; | |
5484 | } | |
5485 | ||
5486 | ||
8d08fdba MS |
5487 | /* Type unification. |
5488 | ||
5489 | We have a function template signature with one or more references to | |
5490 | template parameters, and a parameter list we wish to fit to this | |
5491 | template. If possible, produce a list of parameters for the template | |
5492 | which will cause it to fit the supplied parameter list. | |
5493 | ||
5494 | Return zero for success, 2 for an incomplete match that doesn't resolve | |
5495 | all the types, and 1 for complete failure. An error message will be | |
5496 | printed only for an incomplete match. | |
5497 | ||
5498 | TPARMS[NTPARMS] is an array of template parameter types; | |
5499 | TARGS[NTPARMS] is the array of template parameter values. PARMS is | |
5500 | the function template's signature (using TEMPLATE_PARM_IDX nodes), | |
5501 | and ARGS is the argument list we're trying to match against it. | |
5502 | ||
5503 | If SUBR is 1, we're being called recursively (to unify the arguments of | |
5504 | a function or method parameter of a function template), so don't zero | |
6467930b MS |
5505 | out targs and don't fail on an incomplete match. |
5506 | ||
5507 | If STRICT is 1, the match must be exact (for casts of overloaded | |
5508 | addresses, explicit instantiation, and more_specialized). */ | |
8d08fdba MS |
5509 | |
5510 | int | |
3b3ba9f0 | 5511 | type_unification (tparms, targs, parms, args, targs_in, |
386b8a85 | 5512 | strict, allow_incomplete) |
050367a3 | 5513 | tree tparms, targs, parms, args, targs_in; |
3b3ba9f0 | 5514 | int strict, allow_incomplete; |
386b8a85 JM |
5515 | { |
5516 | int ntparms = TREE_VEC_LENGTH (tparms); | |
75650646 | 5517 | tree arg; |
050367a3 | 5518 | int* explicit_mask; |
386b8a85 JM |
5519 | int i; |
5520 | int r; | |
5521 | ||
050367a3 MM |
5522 | for (i = 0; i < ntparms; i++) |
5523 | TREE_VEC_ELT (targs, i) = NULL_TREE; | |
386b8a85 | 5524 | |
75650646 MM |
5525 | if (targs_in != NULL_TREE) |
5526 | { | |
5527 | tree arg_vec; | |
5528 | arg_vec = coerce_template_parms (tparms, targs_in, NULL_TREE, 0, | |
de575009 | 5529 | 0, 0); |
75650646 MM |
5530 | |
5531 | if (arg_vec == error_mark_node) | |
5532 | return 1; | |
386b8a85 | 5533 | |
050367a3 MM |
5534 | explicit_mask = alloca (sizeof (int) * TREE_VEC_LENGTH (targs)); |
5535 | bzero (explicit_mask, sizeof(int) * TREE_VEC_LENGTH (targs)); | |
5536 | ||
75650646 MM |
5537 | for (i = 0; |
5538 | i < TREE_VEC_LENGTH (arg_vec) | |
050367a3 | 5539 | && TREE_VEC_ELT (arg_vec, i) != NULL_TREE; |
75650646 | 5540 | ++i) |
050367a3 MM |
5541 | { |
5542 | TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (arg_vec, i); | |
5543 | /* Let unify know that this argument was explicit. */ | |
5544 | explicit_mask [i] = 1; | |
5545 | } | |
75650646 | 5546 | } |
050367a3 MM |
5547 | else |
5548 | explicit_mask = 0; | |
386b8a85 | 5549 | |
050367a3 MM |
5550 | r = type_unification_real (tparms, targs, parms, args, 0, |
5551 | strict, allow_incomplete, explicit_mask); | |
386b8a85 JM |
5552 | |
5553 | return r; | |
5554 | } | |
5555 | ||
050367a3 MM |
5556 | /* Like type_unfication. EXPLICIT_MASK, if non-NULL, is an array of |
5557 | integers, with ones in positions corresponding to arguments in | |
5558 | targs that were provided explicitly, and zeros elsewhere. */ | |
386b8a85 | 5559 | |
4966381a | 5560 | static int |
3b3ba9f0 | 5561 | type_unification_real (tparms, targs, parms, args, subr, |
050367a3 MM |
5562 | strict, allow_incomplete, explicit_mask) |
5563 | tree tparms, targs, parms, args; | |
3b3ba9f0 | 5564 | int subr, strict, allow_incomplete; |
050367a3 | 5565 | int* explicit_mask; |
8d08fdba MS |
5566 | { |
5567 | tree parm, arg; | |
5568 | int i; | |
5569 | int ntparms = TREE_VEC_LENGTH (tparms); | |
5570 | ||
5571 | my_friendly_assert (TREE_CODE (tparms) == TREE_VEC, 289); | |
386b8a85 JM |
5572 | my_friendly_assert (parms == NULL_TREE |
5573 | || TREE_CODE (parms) == TREE_LIST, 290); | |
51c184be | 5574 | /* ARGS could be NULL (via a call from parse.y to |
8d08fdba MS |
5575 | build_x_function_call). */ |
5576 | if (args) | |
5577 | my_friendly_assert (TREE_CODE (args) == TREE_LIST, 291); | |
5578 | my_friendly_assert (ntparms > 0, 292); | |
5579 | ||
8d08fdba MS |
5580 | while (parms |
5581 | && parms != void_list_node | |
5582 | && args | |
5583 | && args != void_list_node) | |
5584 | { | |
5585 | parm = TREE_VALUE (parms); | |
5586 | parms = TREE_CHAIN (parms); | |
5587 | arg = TREE_VALUE (args); | |
5588 | args = TREE_CHAIN (args); | |
5589 | ||
5590 | if (arg == error_mark_node) | |
5591 | return 1; | |
5592 | if (arg == unknown_type_node) | |
5593 | return 1; | |
b7484fbe | 5594 | |
03e70705 JM |
5595 | /* Conversions will be performed on a function argument that |
5596 | corresponds with a function parameter that contains only | |
5597 | non-deducible template parameters and explicitly specified | |
5598 | template parameters. */ | |
5599 | if (! uses_template_parms (parm)) | |
b7484fbe | 5600 | { |
03e70705 JM |
5601 | tree type; |
5602 | ||
5603 | if (TREE_CODE_CLASS (TREE_CODE (arg)) != 't') | |
5604 | type = TREE_TYPE (arg); | |
5605 | else | |
5606 | { | |
5607 | type = arg; | |
5608 | arg = NULL_TREE; | |
5609 | } | |
5610 | ||
5611 | if (strict) | |
5612 | { | |
5613 | if (comptypes (parm, type, 1)) | |
5614 | continue; | |
5615 | } | |
03e70705 | 5616 | else |
343c89cd JM |
5617 | /* It might work; we shouldn't check now, because we might |
5618 | get into infinite recursion. Overload resolution will | |
5619 | handle it. */ | |
5620 | continue; | |
03e70705 | 5621 | |
b7484fbe MS |
5622 | return 1; |
5623 | } | |
5624 | ||
8d08fdba MS |
5625 | #if 0 |
5626 | if (TREE_CODE (arg) == VAR_DECL) | |
5627 | arg = TREE_TYPE (arg); | |
5628 | else if (TREE_CODE_CLASS (TREE_CODE (arg)) == 'e') | |
5629 | arg = TREE_TYPE (arg); | |
5630 | #else | |
5631 | if (TREE_CODE_CLASS (TREE_CODE (arg)) != 't') | |
5632 | { | |
5633 | my_friendly_assert (TREE_TYPE (arg) != NULL_TREE, 293); | |
28cbf42c MS |
5634 | if (TREE_CODE (arg) == TREE_LIST |
5635 | && TREE_TYPE (arg) == unknown_type_node | |
5636 | && TREE_CODE (TREE_VALUE (arg)) == TEMPLATE_DECL) | |
5637 | { | |
3b3ba9f0 | 5638 | int ntparms; |
050367a3 | 5639 | tree targs; |
28cbf42c MS |
5640 | |
5641 | /* Have to back unify here */ | |
5642 | arg = TREE_VALUE (arg); | |
98c1c668 | 5643 | ntparms = DECL_NTPARMS (arg); |
050367a3 | 5644 | targs = make_scratch_vec (ntparms); |
e66d884e | 5645 | parm = expr_tree_cons (NULL_TREE, parm, NULL_TREE); |
386b8a85 JM |
5646 | return |
5647 | type_unification (DECL_INNERMOST_TEMPLATE_PARMS (arg), | |
5648 | targs, | |
5649 | TYPE_ARG_TYPES (TREE_TYPE (arg)), | |
3b3ba9f0 | 5650 | parm, NULL_TREE, strict, |
386b8a85 | 5651 | allow_incomplete); |
28cbf42c | 5652 | } |
8d08fdba MS |
5653 | arg = TREE_TYPE (arg); |
5654 | } | |
5655 | #endif | |
5c0ad672 JM |
5656 | if (! flag_ansi && arg == TREE_TYPE (null_node)) |
5657 | { | |
5658 | warning ("using type void* for NULL"); | |
5659 | arg = ptr_type_node; | |
5660 | } | |
5661 | ||
7834ab39 | 5662 | if (! subr && TREE_CODE (arg) == REFERENCE_TYPE) |
db5ae43f MS |
5663 | arg = TREE_TYPE (arg); |
5664 | ||
7834ab39 | 5665 | if (! subr && TREE_CODE (parm) != REFERENCE_TYPE) |
4cabb798 JM |
5666 | { |
5667 | if (TREE_CODE (arg) == FUNCTION_TYPE | |
5668 | || TREE_CODE (arg) == METHOD_TYPE) | |
5669 | arg = build_pointer_type (arg); | |
5670 | else if (TREE_CODE (arg) == ARRAY_TYPE) | |
5671 | arg = build_pointer_type (TREE_TYPE (arg)); | |
5672 | else | |
5673 | arg = TYPE_MAIN_VARIANT (arg); | |
5674 | } | |
8d08fdba | 5675 | |
050367a3 MM |
5676 | switch (unify (tparms, targs, ntparms, parm, arg, strict, |
5677 | explicit_mask)) | |
8d08fdba MS |
5678 | { |
5679 | case 0: | |
5680 | break; | |
5681 | case 1: | |
5682 | return 1; | |
5683 | } | |
5684 | } | |
5685 | /* Fail if we've reached the end of the parm list, and more args | |
5686 | are present, and the parm list isn't variadic. */ | |
5687 | if (args && args != void_list_node && parms == void_list_node) | |
5688 | return 1; | |
5689 | /* Fail if parms are left and they don't have default values. */ | |
5690 | if (parms | |
5691 | && parms != void_list_node | |
5692 | && TREE_PURPOSE (parms) == NULL_TREE) | |
5693 | return 1; | |
5694 | if (!subr) | |
5695 | for (i = 0; i < ntparms; i++) | |
050367a3 | 5696 | if (TREE_VEC_ELT (targs, i) == NULL_TREE) |
8d08fdba | 5697 | { |
386b8a85 JM |
5698 | if (!allow_incomplete) |
5699 | error ("incomplete type unification"); | |
8d08fdba MS |
5700 | return 2; |
5701 | } | |
5702 | return 0; | |
5703 | } | |
5704 | ||
db2767b6 MM |
5705 | /* Returns the level of DECL, which declares a template parameter. */ |
5706 | ||
04a81d94 | 5707 | int |
db2767b6 MM |
5708 | template_decl_level (decl) |
5709 | tree decl; | |
5710 | { | |
5711 | switch (TREE_CODE (decl)) | |
5712 | { | |
5713 | case TYPE_DECL: | |
5714 | case TEMPLATE_DECL: | |
5715 | return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl)); | |
5716 | ||
5717 | case PARM_DECL: | |
5718 | return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl)); | |
5719 | ||
5720 | default: | |
5721 | my_friendly_abort (0); | |
5ffe581d | 5722 | return 0; |
db2767b6 MM |
5723 | } |
5724 | } | |
5725 | ||
8d08fdba | 5726 | /* Tail recursion is your friend. */ |
e92cc029 | 5727 | |
8d08fdba | 5728 | static int |
050367a3 MM |
5729 | unify (tparms, targs, ntparms, parm, arg, strict, explicit_mask) |
5730 | tree tparms, targs, parm, arg; | |
3b3ba9f0 | 5731 | int ntparms, strict; |
050367a3 | 5732 | int* explicit_mask; |
8d08fdba MS |
5733 | { |
5734 | int idx; | |
050367a3 | 5735 | tree targ; |
db2767b6 | 5736 | tree tparm; |
8d08fdba MS |
5737 | |
5738 | /* I don't think this will do the right thing with respect to types. | |
5739 | But the only case I've seen it in so far has been array bounds, where | |
5740 | signedness is the only information lost, and I think that will be | |
5741 | okay. */ | |
5742 | while (TREE_CODE (parm) == NOP_EXPR) | |
5743 | parm = TREE_OPERAND (parm, 0); | |
5744 | ||
5745 | if (arg == error_mark_node) | |
5746 | return 1; | |
5747 | if (arg == unknown_type_node) | |
5748 | return 1; | |
db2767b6 MM |
5749 | /* If PARM uses template parameters, then we can't bail out here, |
5750 | even in ARG == PARM, since we won't record unifications for the | |
5751 | template parameters. We might need them if we're trying to | |
5752 | figure out which of two things is more specialized. */ | |
5753 | if (arg == parm && !uses_template_parms (parm)) | |
8d08fdba MS |
5754 | return 0; |
5755 | ||
49432171 JM |
5756 | /* We can't remove cv-quals when strict. */ |
5757 | if (strict && TREE_CODE (arg) == TREE_CODE (parm) | |
5758 | && TREE_CODE_CLASS (TREE_CODE (arg)) == 't' | |
5759 | && (TYPE_READONLY (arg) < TYPE_READONLY (parm) | |
5760 | || TYPE_VOLATILE (arg) < TYPE_VOLATILE (parm))) | |
5761 | return 1; | |
5762 | ||
8d08fdba MS |
5763 | switch (TREE_CODE (parm)) |
5764 | { | |
2ca340ae JM |
5765 | case TYPENAME_TYPE: |
5766 | /* In a type which contains a nested-name-specifier, template | |
5767 | argument values cannot be deduced for template parameters used | |
5768 | within the nested-name-specifier. */ | |
5769 | return 0; | |
5770 | ||
8d08fdba | 5771 | case TEMPLATE_TYPE_PARM: |
73b0fce8 | 5772 | case TEMPLATE_TEMPLATE_PARM: |
db2767b6 MM |
5773 | tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0)); |
5774 | ||
5775 | if (TEMPLATE_TYPE_LEVEL (parm) | |
5776 | != template_decl_level (tparm)) | |
5777 | /* The PARM is not one we're trying to unify. Just check | |
5778 | to see if it matches ARG. */ | |
5779 | return (TREE_CODE (arg) == TREE_CODE (parm) | |
2e925bfc | 5780 | && comptypes (parm, arg, 1)) ? 0 : 1; |
73b0fce8 | 5781 | idx = TEMPLATE_TYPE_IDX (parm); |
050367a3 | 5782 | targ = TREE_VEC_ELT (targs, idx); |
db2767b6 | 5783 | tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx)); |
050367a3 | 5784 | |
73b0fce8 | 5785 | /* Check for mixed types and values. */ |
db2767b6 MM |
5786 | if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM |
5787 | && TREE_CODE (tparm) != TYPE_DECL) | |
5788 | || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM | |
5789 | && TREE_CODE (tparm) != TEMPLATE_DECL)) | |
73b0fce8 KL |
5790 | return 1; |
5791 | ||
050367a3 MM |
5792 | if (!strict && targ != NULL_TREE |
5793 | && explicit_mask && explicit_mask[idx]) | |
73b0fce8 KL |
5794 | /* An explicit template argument. Don't even try to match |
5795 | here; the overload resolution code will manage check to | |
5796 | see whether the call is legal. */ | |
5797 | return 0; | |
5798 | ||
db2767b6 | 5799 | if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM) |
73b0fce8 | 5800 | { |
db2767b6 MM |
5801 | if (CLASSTYPE_TEMPLATE_INFO (parm)) |
5802 | { | |
5803 | /* We arrive here when PARM does not involve template | |
5804 | specialization. */ | |
73b0fce8 | 5805 | |
db2767b6 MM |
5806 | /* ARG must be constructed from a template class. */ |
5807 | if (TREE_CODE (arg) != RECORD_TYPE || !CLASSTYPE_TEMPLATE_INFO (arg)) | |
5808 | return 1; | |
73b0fce8 | 5809 | |
db2767b6 MM |
5810 | { |
5811 | tree parmtmpl = CLASSTYPE_TI_TEMPLATE (parm); | |
5812 | tree parmvec = CLASSTYPE_TI_ARGS (parm); | |
5813 | tree argvec = CLASSTYPE_TI_ARGS (arg); | |
5814 | tree argtmplvec | |
5815 | = DECL_INNERMOST_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (arg)); | |
5816 | int i; | |
5817 | ||
5818 | /* The parameter and argument roles have to be switched here | |
5819 | in order to handle default arguments properly. For example, | |
5820 | template<template <class> class TT> void f(TT<int>) | |
5821 | should be able to accept vector<int> which comes from | |
5822 | template <class T, class Allcator = allocator> | |
73b0fce8 KL |
5823 | class vector. */ |
5824 | ||
db2767b6 MM |
5825 | if (coerce_template_parms (argtmplvec, parmvec, parmtmpl, 1, 1, 0) |
5826 | == error_mark_node) | |
5827 | return 1; | |
73b0fce8 | 5828 | |
db2767b6 MM |
5829 | /* Deduce arguments T, i from TT<T> or TT<i>. */ |
5830 | for (i = 0; i < TREE_VEC_LENGTH (parmvec); ++i) | |
5831 | { | |
5832 | tree t = TREE_VEC_ELT (parmvec, i); | |
5833 | if (TREE_CODE (t) != TEMPLATE_TYPE_PARM | |
5834 | && TREE_CODE (t) != TEMPLATE_TEMPLATE_PARM | |
5835 | && TREE_CODE (t) != TEMPLATE_PARM_INDEX) | |
5836 | continue; | |
73b0fce8 | 5837 | |
db2767b6 | 5838 | /* This argument can be deduced. */ |
73b0fce8 | 5839 | |
db2767b6 MM |
5840 | if (unify (tparms, targs, ntparms, t, |
5841 | TREE_VEC_ELT (argvec, i), strict, explicit_mask)) | |
5842 | return 1; | |
5843 | } | |
73b0fce8 | 5844 | } |
db2767b6 MM |
5845 | arg = CLASSTYPE_TI_TEMPLATE (arg); |
5846 | } | |
5847 | } | |
5848 | else | |
5849 | { | |
5850 | if (strict && (TYPE_READONLY (arg) < TYPE_READONLY (parm) | |
5851 | || TYPE_VOLATILE (arg) < TYPE_VOLATILE (parm))) | |
5852 | return 1; | |
5853 | ||
5854 | #if 0 | |
5855 | /* Template type parameters cannot contain cv-quals; i.e. | |
5856 | template <class T> void f (T& a, T& b) will not generate | |
5857 | void f (const int& a, const int& b). */ | |
5858 | if (TYPE_READONLY (arg) > TYPE_READONLY (parm) | |
5859 | || TYPE_VOLATILE (arg) > TYPE_VOLATILE (parm)) | |
5860 | return 1; | |
5861 | arg = TYPE_MAIN_VARIANT (arg); | |
5862 | #else | |
5863 | { | |
5864 | int constp = TYPE_READONLY (arg) > TYPE_READONLY (parm); | |
5865 | int volatilep = TYPE_VOLATILE (arg) > TYPE_VOLATILE (parm); | |
5866 | arg = cp_build_type_variant (arg, constp, volatilep); | |
73b0fce8 | 5867 | } |
db2767b6 | 5868 | #endif |
73b0fce8 KL |
5869 | } |
5870 | ||
5871 | /* Simple cases: Value already set, does match or doesn't. */ | |
3a56f0ab JM |
5872 | if (targ != NULL_TREE |
5873 | && (comptypes (targ, arg, 1) | |
5874 | || (explicit_mask && explicit_mask[idx]))) | |
73b0fce8 | 5875 | return 0; |
050367a3 | 5876 | else if (targ) |
73b0fce8 | 5877 | return 1; |
050367a3 | 5878 | TREE_VEC_ELT (targs, idx) = arg; |
73b0fce8 KL |
5879 | return 0; |
5880 | ||
f84b4be9 | 5881 | case TEMPLATE_PARM_INDEX: |
db2767b6 MM |
5882 | tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0)); |
5883 | ||
5884 | if (TEMPLATE_PARM_LEVEL (parm) | |
5885 | != template_decl_level (tparm)) | |
5886 | /* The PARM is not one we're trying to unify. Just check | |
5887 | to see if it matches ARG. */ | |
5888 | return (TREE_CODE (arg) == TREE_CODE (parm) | |
2e925bfc | 5889 | && cp_tree_equal (parm, arg)) ? 0 : 1; |
db2767b6 | 5890 | |
f84b4be9 | 5891 | idx = TEMPLATE_PARM_IDX (parm); |
050367a3 | 5892 | targ = TREE_VEC_ELT (targs, idx); |
db2767b6 | 5893 | |
050367a3 | 5894 | if (targ) |
8d08fdba | 5895 | { |
050367a3 | 5896 | int i = cp_tree_equal (targ, arg); |
312e7d50 JM |
5897 | if (i == 1) |
5898 | return 0; | |
5899 | else if (i == 0) | |
5900 | return 1; | |
5901 | else | |
5902 | my_friendly_abort (42); | |
8d08fdba | 5903 | } |
8d08fdba | 5904 | |
050367a3 | 5905 | TREE_VEC_ELT (targs, idx) = copy_to_permanent (arg); |
8d08fdba MS |
5906 | return 0; |
5907 | ||
5908 | case POINTER_TYPE: | |
4ac14744 MS |
5909 | if (TREE_CODE (arg) == RECORD_TYPE && TYPE_PTRMEMFUNC_FLAG (arg)) |
5910 | return unify (tparms, targs, ntparms, parm, | |
050367a3 | 5911 | TYPE_PTRMEMFUNC_FN_TYPE (arg), strict, explicit_mask); |
4ac14744 | 5912 | |
8d08fdba MS |
5913 | if (TREE_CODE (arg) != POINTER_TYPE) |
5914 | return 1; | |
5915 | return unify (tparms, targs, ntparms, TREE_TYPE (parm), TREE_TYPE (arg), | |
050367a3 | 5916 | strict, explicit_mask); |
8d08fdba MS |
5917 | |
5918 | case REFERENCE_TYPE: | |
28cbf42c MS |
5919 | if (TREE_CODE (arg) == REFERENCE_TYPE) |
5920 | arg = TREE_TYPE (arg); | |
6467930b | 5921 | return unify (tparms, targs, ntparms, TREE_TYPE (parm), arg, |
050367a3 | 5922 | strict, explicit_mask); |
8d08fdba MS |
5923 | |
5924 | case ARRAY_TYPE: | |
5925 | if (TREE_CODE (arg) != ARRAY_TYPE) | |
5926 | return 1; | |
3042d5be MM |
5927 | if ((TYPE_DOMAIN (parm) == NULL_TREE) |
5928 | != (TYPE_DOMAIN (arg) == NULL_TREE)) | |
5929 | return 1; | |
5930 | if (TYPE_DOMAIN (parm) != NULL_TREE | |
5931 | && unify (tparms, targs, ntparms, TYPE_DOMAIN (parm), | |
050367a3 | 5932 | TYPE_DOMAIN (arg), strict, explicit_mask) != 0) |
8d08fdba MS |
5933 | return 1; |
5934 | return unify (tparms, targs, ntparms, TREE_TYPE (parm), TREE_TYPE (arg), | |
050367a3 | 5935 | strict, explicit_mask); |
8d08fdba MS |
5936 | |
5937 | case REAL_TYPE: | |
37c46b43 | 5938 | case COMPLEX_TYPE: |
8d08fdba | 5939 | case INTEGER_TYPE: |
42976354 | 5940 | case BOOLEAN_TYPE: |
5ad5a526 | 5941 | case VOID_TYPE: |
f376e137 MS |
5942 | if (TREE_CODE (arg) != TREE_CODE (parm)) |
5943 | return 1; | |
5944 | ||
5945 | if (TREE_CODE (parm) == INTEGER_TYPE) | |
8d08fdba MS |
5946 | { |
5947 | if (TYPE_MIN_VALUE (parm) && TYPE_MIN_VALUE (arg) | |
6467930b | 5948 | && unify (tparms, targs, ntparms, TYPE_MIN_VALUE (parm), |
050367a3 | 5949 | TYPE_MIN_VALUE (arg), strict, explicit_mask)) |
8d08fdba MS |
5950 | return 1; |
5951 | if (TYPE_MAX_VALUE (parm) && TYPE_MAX_VALUE (arg) | |
6467930b | 5952 | && unify (tparms, targs, ntparms, TYPE_MAX_VALUE (parm), |
050367a3 | 5953 | TYPE_MAX_VALUE (arg), strict, explicit_mask)) |
8d08fdba MS |
5954 | return 1; |
5955 | } | |
ca79f85d JM |
5956 | else if (TREE_CODE (parm) == REAL_TYPE |
5957 | && TYPE_MAIN_VARIANT (arg) != TYPE_MAIN_VARIANT (parm)) | |
5958 | return 1; | |
5959 | ||
8d08fdba MS |
5960 | /* As far as unification is concerned, this wins. Later checks |
5961 | will invalidate it if necessary. */ | |
5962 | return 0; | |
5963 | ||
5964 | /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */ | |
bd6dd845 | 5965 | /* Type INTEGER_CST can come from ordinary constant template args. */ |
8d08fdba | 5966 | case INTEGER_CST: |
bd6dd845 MS |
5967 | while (TREE_CODE (arg) == NOP_EXPR) |
5968 | arg = TREE_OPERAND (arg, 0); | |
5969 | ||
8d08fdba MS |
5970 | if (TREE_CODE (arg) != INTEGER_CST) |
5971 | return 1; | |
5972 | return !tree_int_cst_equal (parm, arg); | |
5973 | ||
8d08fdba MS |
5974 | case TREE_VEC: |
5975 | { | |
5976 | int i; | |
5977 | if (TREE_CODE (arg) != TREE_VEC) | |
5978 | return 1; | |
5979 | if (TREE_VEC_LENGTH (parm) != TREE_VEC_LENGTH (arg)) | |
5980 | return 1; | |
5981 | for (i = TREE_VEC_LENGTH (parm) - 1; i >= 0; i--) | |
5982 | if (unify (tparms, targs, ntparms, | |
5983 | TREE_VEC_ELT (parm, i), TREE_VEC_ELT (arg, i), | |
050367a3 | 5984 | strict, explicit_mask)) |
8d08fdba MS |
5985 | return 1; |
5986 | return 0; | |
5987 | } | |
5988 | ||
8d08fdba | 5989 | case RECORD_TYPE: |
db5ae43f | 5990 | if (TYPE_PTRMEMFUNC_FLAG (parm)) |
8d08fdba | 5991 | return unify (tparms, targs, ntparms, TYPE_PTRMEMFUNC_FN_TYPE (parm), |
050367a3 | 5992 | arg, strict, explicit_mask); |
8d08fdba | 5993 | |
a4443a08 | 5994 | /* Allow trivial conversions. */ |
5566b478 | 5995 | if (TREE_CODE (arg) != RECORD_TYPE |
a4443a08 MS |
5996 | || TYPE_READONLY (parm) < TYPE_READONLY (arg) |
5997 | || TYPE_VOLATILE (parm) < TYPE_VOLATILE (arg)) | |
5998 | return 1; | |
5566b478 | 5999 | |
6467930b | 6000 | if (CLASSTYPE_TEMPLATE_INFO (parm) && uses_template_parms (parm)) |
5566b478 | 6001 | { |
6467930b | 6002 | tree t = NULL_TREE; |
277294d7 | 6003 | if (! strict) |
6467930b | 6004 | t = get_template_base (CLASSTYPE_TI_TEMPLATE (parm), arg); |
c73964b2 MS |
6005 | else if |
6006 | (CLASSTYPE_TEMPLATE_INFO (arg) | |
6007 | && CLASSTYPE_TI_TEMPLATE (parm) == CLASSTYPE_TI_TEMPLATE (arg)) | |
6467930b MS |
6008 | t = arg; |
6009 | if (! t || t == error_mark_node) | |
5566b478 | 6010 | return 1; |
6467930b | 6011 | |
5566b478 | 6012 | return unify (tparms, targs, ntparms, CLASSTYPE_TI_ARGS (parm), |
050367a3 | 6013 | CLASSTYPE_TI_ARGS (t), strict, explicit_mask); |
5566b478 MS |
6014 | } |
6015 | else if (TYPE_MAIN_VARIANT (parm) != TYPE_MAIN_VARIANT (arg)) | |
6016 | return 1; | |
a4443a08 | 6017 | return 0; |
8d08fdba MS |
6018 | |
6019 | case METHOD_TYPE: | |
6020 | if (TREE_CODE (arg) != METHOD_TYPE) | |
6021 | return 1; | |
6022 | goto check_args; | |
6023 | ||
6024 | case FUNCTION_TYPE: | |
6025 | if (TREE_CODE (arg) != FUNCTION_TYPE) | |
6026 | return 1; | |
6027 | check_args: | |
28cbf42c | 6028 | if (unify (tparms, targs, ntparms, TREE_TYPE (parm), |
050367a3 | 6029 | TREE_TYPE (arg), strict, explicit_mask)) |
28cbf42c | 6030 | return 1; |
386b8a85 | 6031 | return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm), |
3b3ba9f0 | 6032 | TYPE_ARG_TYPES (arg), 1, |
050367a3 | 6033 | strict, 0, explicit_mask); |
a4443a08 MS |
6034 | |
6035 | case OFFSET_TYPE: | |
6036 | if (TREE_CODE (arg) != OFFSET_TYPE) | |
6037 | return 1; | |
6038 | if (unify (tparms, targs, ntparms, TYPE_OFFSET_BASETYPE (parm), | |
050367a3 | 6039 | TYPE_OFFSET_BASETYPE (arg), strict, explicit_mask)) |
a4443a08 MS |
6040 | return 1; |
6041 | return unify (tparms, targs, ntparms, TREE_TYPE (parm), | |
050367a3 | 6042 | TREE_TYPE (arg), strict, explicit_mask); |
a4443a08 | 6043 | |
f62dbf03 JM |
6044 | case CONST_DECL: |
6045 | if (arg != decl_constant_value (parm)) | |
6046 | return 1; | |
6047 | return 0; | |
6048 | ||
027905b4 KL |
6049 | case TEMPLATE_DECL: |
6050 | /* Matched cases are handled by the ARG == PARM test above. */ | |
6051 | return 1; | |
6052 | ||
8d08fdba | 6053 | default: |
050367a3 MM |
6054 | if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (parm)))) |
6055 | { | |
6056 | /* We're looking at an expression. This can happen with | |
6057 | something like: | |
6058 | ||
6059 | template <int I> | |
6060 | void foo(S<I>, S<I + 2>); | |
6061 | ||
6062 | If the call looked like: | |
6063 | ||
6064 | foo(S<2>(), S<4>()); | |
6065 | ||
6066 | we would have already matched `I' with `2'. Now, we'd | |
6067 | like to know if `4' matches `I + 2'. So, we substitute | |
6068 | into that expression, and fold constants, in the hope of | |
6069 | figuring it out. */ | |
6070 | tree t = | |
6071 | maybe_fold_nontype_arg (tsubst_expr (parm, targs, NULL_TREE)); | |
6072 | enum tree_code tc = TREE_CODE (t); | |
6073 | ||
6074 | if (tc == MINUS_EXPR | |
6075 | && TREE_CODE (TREE_OPERAND (t, 0)) == TEMPLATE_PARM_INDEX | |
6076 | && TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST) | |
6077 | { | |
6078 | /* We handle this case specially, since it comes up with | |
6079 | arrays. In particular, something like: | |
6080 | ||
6081 | template <int N> void f(int (&x)[N]); | |
6082 | ||
6083 | Here, we are trying to unify the range type, which | |
6084 | looks like [0 ... (N - 1)]. */ | |
6085 | tree t1, t2; | |
6086 | t1 = TREE_OPERAND (parm, 0); | |
6087 | t2 = TREE_OPERAND (parm, 1); | |
6088 | ||
6089 | t = maybe_fold_nontype_arg (build (PLUS_EXPR, | |
6090 | integer_type_node, | |
6091 | arg, t2)); | |
6092 | ||
6093 | return unify (tparms, targs, ntparms, t1, t, | |
6094 | strict, explicit_mask); | |
6095 | } | |
6096 | ||
6097 | if (!IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (tc))) | |
6098 | /* Good, we mangaged to simplify the exression. */ | |
6099 | return unify (tparms, targs, ntparms, t, arg, strict, | |
6100 | explicit_mask); | |
6101 | else | |
6102 | /* Bad, we couldn't simplify this. Assume it doesn't | |
6103 | unify. */ | |
6104 | return 1; | |
6105 | } | |
6106 | else | |
b4c4a9ec MM |
6107 | sorry ("use of `%s' in template type unification", |
6108 | tree_code_name [(int) TREE_CODE (parm)]); | |
050367a3 | 6109 | |
8d08fdba MS |
6110 | return 1; |
6111 | } | |
6112 | } | |
8d08fdba | 6113 | \f |
faae18ab | 6114 | void |
5566b478 | 6115 | mark_decl_instantiated (result, extern_p) |
faae18ab MS |
6116 | tree result; |
6117 | int extern_p; | |
6118 | { | |
6119 | if (DECL_TEMPLATE_INSTANTIATION (result)) | |
6120 | SET_DECL_EXPLICIT_INSTANTIATION (result); | |
75650646 MM |
6121 | |
6122 | if (TREE_CODE (result) != FUNCTION_DECL) | |
6123 | /* The TREE_PUBLIC flag for function declarations will have been | |
6124 | set correctly by tsubst. */ | |
6125 | TREE_PUBLIC (result) = 1; | |
faae18ab MS |
6126 | |
6127 | if (! extern_p) | |
6128 | { | |
6129 | DECL_INTERFACE_KNOWN (result) = 1; | |
6130 | DECL_NOT_REALLY_EXTERN (result) = 1; | |
a7d87521 JM |
6131 | |
6132 | /* For WIN32 we also want to put explicit instantiations in | |
6133 | linkonce sections. */ | |
75650646 | 6134 | if (supports_one_only () && ! SUPPORTS_WEAK && TREE_PUBLIC (result)) |
ab23f787 | 6135 | make_decl_one_only (result); |
faae18ab | 6136 | } |
f49422da MS |
6137 | else if (TREE_CODE (result) == FUNCTION_DECL) |
6138 | mark_inline_for_output (result); | |
faae18ab MS |
6139 | } |
6140 | ||
e1467ff2 MM |
6141 | /* Given two function templates PAT1 and PAT2, and explicit template |
6142 | arguments EXPLICIT_ARGS return: | |
6467930b MS |
6143 | |
6144 | 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order]. | |
6145 | -1 if PAT2 is more specialized than PAT1. | |
6146 | 0 if neither is more specialized. */ | |
6147 | ||
6148 | int | |
e1467ff2 MM |
6149 | more_specialized (pat1, pat2, explicit_args) |
6150 | tree pat1, pat2, explicit_args; | |
6467930b | 6151 | { |
98c1c668 | 6152 | tree targs; |
73aad9b9 | 6153 | int winner = 0; |
6467930b | 6154 | |
76b9a14d | 6155 | targs = get_bindings_overload (pat1, pat2, explicit_args); |
73aad9b9 JM |
6156 | if (targs) |
6157 | { | |
73aad9b9 JM |
6158 | --winner; |
6159 | } | |
6467930b | 6160 | |
76b9a14d | 6161 | targs = get_bindings_overload (pat2, pat1, explicit_args); |
73aad9b9 JM |
6162 | if (targs) |
6163 | { | |
73aad9b9 JM |
6164 | ++winner; |
6165 | } | |
6467930b | 6166 | |
73aad9b9 JM |
6167 | return winner; |
6168 | } | |
6467930b | 6169 | |
73aad9b9 | 6170 | /* Given two class template specialization list nodes PAT1 and PAT2, return: |
6467930b | 6171 | |
73aad9b9 JM |
6172 | 1 if PAT1 is more specialized than PAT2 as described in [temp.class.order]. |
6173 | -1 if PAT2 is more specialized than PAT1. | |
6174 | 0 if neither is more specialized. */ | |
6175 | ||
6176 | int | |
6177 | more_specialized_class (pat1, pat2) | |
6178 | tree pat1, pat2; | |
6179 | { | |
6180 | tree targs; | |
6181 | int winner = 0; | |
6182 | ||
6183 | targs = get_class_bindings | |
8d019cef JM |
6184 | (TREE_VALUE (pat1), TREE_PURPOSE (pat1), |
6185 | TREE_PURPOSE (pat2), NULL_TREE); | |
73aad9b9 JM |
6186 | if (targs) |
6187 | --winner; | |
6188 | ||
6189 | targs = get_class_bindings | |
8d019cef JM |
6190 | (TREE_VALUE (pat2), TREE_PURPOSE (pat2), |
6191 | TREE_PURPOSE (pat1), NULL_TREE); | |
73aad9b9 | 6192 | if (targs) |
6467930b MS |
6193 | ++winner; |
6194 | ||
6195 | return winner; | |
6196 | } | |
73aad9b9 JM |
6197 | |
6198 | /* Return the template arguments that will produce the function signature | |
e1467ff2 | 6199 | DECL from the function template FN, with the explicit template |
76b9a14d JM |
6200 | arguments EXPLICIT_ARGS. If CHECK_RETTYPE is 1, the return type must |
6201 | also match. */ | |
73aad9b9 | 6202 | |
76b9a14d JM |
6203 | static tree |
6204 | get_bindings_real (fn, decl, explicit_args, check_rettype) | |
e1467ff2 | 6205 | tree fn, decl, explicit_args; |
76b9a14d | 6206 | int check_rettype; |
73aad9b9 | 6207 | { |
98c1c668 | 6208 | int ntparms = DECL_NTPARMS (fn); |
e66d884e | 6209 | tree targs = make_scratch_vec (ntparms); |
d7684f2d MM |
6210 | tree decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl)); |
6211 | tree extra_fn_arg = NULL_TREE; | |
98c1c668 JM |
6212 | int i; |
6213 | ||
d7684f2d MM |
6214 | if (DECL_STATIC_FUNCTION_P (fn) |
6215 | && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)) | |
6216 | { | |
6217 | /* Sometimes we are trying to figure out what's being | |
6218 | specialized by a declaration that looks like a method, and it | |
6219 | turns out to be a static member function. */ | |
6220 | if (CLASSTYPE_TEMPLATE_INFO (DECL_REAL_CONTEXT (fn)) | |
6221 | && !is_member_template (fn)) | |
6222 | /* The natural thing to do here seems to be to remove the | |
6223 | spurious `this' parameter from the DECL, but that prevents | |
6224 | unification from making use of the class type. So, | |
6225 | instead, we have fn_type_unification add to the parameters | |
6226 | for FN. */ | |
6227 | extra_fn_arg = build_pointer_type (DECL_REAL_CONTEXT (fn)); | |
6228 | else | |
6229 | /* In this case, though, adding the extra_fn_arg can confuse | |
6230 | things, so we remove from decl_arg_types instead. */ | |
6231 | decl_arg_types = TREE_CHAIN (decl_arg_types); | |
6232 | } | |
6233 | ||
e1467ff2 | 6234 | i = fn_type_unification (fn, explicit_args, targs, |
d7684f2d | 6235 | decl_arg_types, |
98c1c668 | 6236 | TREE_TYPE (TREE_TYPE (decl)), |
d7684f2d MM |
6237 | 1, |
6238 | extra_fn_arg); | |
98c1c668 | 6239 | |
76b9a14d JM |
6240 | if (i != 0) |
6241 | return NULL_TREE; | |
6242 | ||
6243 | if (check_rettype) | |
e1467ff2 MM |
6244 | { |
6245 | /* Check to see that the resulting return type is also OK. */ | |
e6f1275f JM |
6246 | tree t = tsubst (TREE_TYPE (TREE_TYPE (fn)), |
6247 | complete_template_args (fn, targs, 1), | |
6248 | NULL_TREE); | |
e1467ff2 | 6249 | |
f84b4be9 | 6250 | if (!comptypes (t, TREE_TYPE (TREE_TYPE (decl)), 1)) |
e1467ff2 | 6251 | return NULL_TREE; |
e1467ff2 MM |
6252 | } |
6253 | ||
76b9a14d JM |
6254 | return targs; |
6255 | } | |
6256 | ||
6257 | /* For most uses, we want to check the return type. */ | |
6258 | ||
6259 | tree | |
6260 | get_bindings (fn, decl, explicit_args) | |
6261 | tree fn, decl, explicit_args; | |
6262 | { | |
6263 | return get_bindings_real (fn, decl, explicit_args, 1); | |
6264 | } | |
6265 | ||
6266 | /* But for more_specialized, we only care about the parameter types. */ | |
6267 | ||
6268 | static tree | |
6269 | get_bindings_overload (fn, decl, explicit_args) | |
6270 | tree fn, decl, explicit_args; | |
6271 | { | |
6272 | return get_bindings_real (fn, decl, explicit_args, 0); | |
73aad9b9 JM |
6273 | } |
6274 | ||
bd6dd845 | 6275 | static tree |
8d019cef JM |
6276 | get_class_bindings (tparms, parms, args, outer_args) |
6277 | tree tparms, parms, args, outer_args; | |
73aad9b9 | 6278 | { |
3b3ba9f0 | 6279 | int i, ntparms = TREE_VEC_LENGTH (tparms); |
73aad9b9 JM |
6280 | tree vec = make_temp_vec (ntparms); |
6281 | ||
8d019cef JM |
6282 | if (outer_args) |
6283 | { | |
6284 | tparms = tsubst (tparms, outer_args, NULL_TREE); | |
6285 | parms = tsubst (parms, outer_args, NULL_TREE); | |
6286 | } | |
6287 | ||
73aad9b9 JM |
6288 | for (i = 0; i < TREE_VEC_LENGTH (parms); ++i) |
6289 | { | |
050367a3 MM |
6290 | switch (unify (tparms, vec, ntparms, |
6291 | TREE_VEC_ELT (parms, i), TREE_VEC_ELT (args, i), | |
6292 | 1, 0)) | |
73aad9b9 JM |
6293 | { |
6294 | case 0: | |
6295 | break; | |
6296 | case 1: | |
6297 | return NULL_TREE; | |
6298 | } | |
6299 | } | |
6300 | ||
6301 | for (i = 0; i < ntparms; ++i) | |
6302 | if (! TREE_VEC_ELT (vec, i)) | |
6303 | return NULL_TREE; | |
6304 | ||
6305 | return vec; | |
6306 | } | |
6307 | ||
6308 | /* Return the most specialized of the list of templates in FNS that can | |
e1467ff2 MM |
6309 | produce an instantiation matching DECL, given the explicit template |
6310 | arguments EXPLICIT_ARGS. */ | |
73aad9b9 JM |
6311 | |
6312 | tree | |
e1467ff2 MM |
6313 | most_specialized (fns, decl, explicit_args) |
6314 | tree fns, decl, explicit_args; | |
73aad9b9 | 6315 | { |
98c1c668 | 6316 | tree fn, champ, args, *p; |
73aad9b9 JM |
6317 | int fate; |
6318 | ||
6319 | for (p = &fns; *p; ) | |
6320 | { | |
e1467ff2 | 6321 | args = get_bindings (TREE_VALUE (*p), decl, explicit_args); |
73aad9b9 JM |
6322 | if (args) |
6323 | { | |
73aad9b9 JM |
6324 | p = &TREE_CHAIN (*p); |
6325 | } | |
6326 | else | |
6327 | *p = TREE_CHAIN (*p); | |
6328 | } | |
6329 | ||
6330 | if (! fns) | |
6331 | return NULL_TREE; | |
6332 | ||
6333 | fn = fns; | |
6334 | champ = TREE_VALUE (fn); | |
6335 | fn = TREE_CHAIN (fn); | |
6336 | for (; fn; fn = TREE_CHAIN (fn)) | |
6337 | { | |
e1467ff2 | 6338 | fate = more_specialized (champ, TREE_VALUE (fn), explicit_args); |
73aad9b9 JM |
6339 | if (fate == 1) |
6340 | ; | |
6341 | else | |
6342 | { | |
6343 | if (fate == 0) | |
6344 | { | |
6345 | fn = TREE_CHAIN (fn); | |
6346 | if (! fn) | |
6347 | return error_mark_node; | |
6348 | } | |
6349 | champ = TREE_VALUE (fn); | |
6350 | } | |
6351 | } | |
6352 | ||
6353 | for (fn = fns; fn && TREE_VALUE (fn) != champ; fn = TREE_CHAIN (fn)) | |
6354 | { | |
e1467ff2 | 6355 | fate = more_specialized (champ, TREE_VALUE (fn), explicit_args); |
73aad9b9 JM |
6356 | if (fate != 1) |
6357 | return error_mark_node; | |
6358 | } | |
6359 | ||
6360 | return champ; | |
6361 | } | |
6362 | ||
6363 | /* Return the most specialized of the class template specializations in | |
6364 | SPECS that can produce an instantiation matching ARGS. */ | |
6365 | ||
6366 | tree | |
8d019cef JM |
6367 | most_specialized_class (specs, mainargs, outer_args) |
6368 | tree specs, mainargs, outer_args; | |
73aad9b9 JM |
6369 | { |
6370 | tree list = NULL_TREE, t, args, champ; | |
6371 | int fate; | |
6372 | ||
6373 | for (t = specs; t; t = TREE_CHAIN (t)) | |
6374 | { | |
8d019cef JM |
6375 | args = get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t), |
6376 | mainargs, outer_args); | |
73aad9b9 JM |
6377 | if (args) |
6378 | { | |
6379 | list = decl_tree_cons (TREE_PURPOSE (t), TREE_VALUE (t), list); | |
6380 | TREE_TYPE (list) = TREE_TYPE (t); | |
6381 | } | |
6382 | } | |
6383 | ||
6384 | if (! list) | |
6385 | return NULL_TREE; | |
6386 | ||
6387 | t = list; | |
6388 | champ = t; | |
6389 | t = TREE_CHAIN (t); | |
6390 | for (; t; t = TREE_CHAIN (t)) | |
6391 | { | |
6392 | fate = more_specialized_class (champ, t); | |
6393 | if (fate == 1) | |
6394 | ; | |
6395 | else | |
6396 | { | |
6397 | if (fate == 0) | |
6398 | { | |
6399 | t = TREE_CHAIN (t); | |
6400 | if (! t) | |
6401 | return error_mark_node; | |
6402 | } | |
6403 | champ = t; | |
6404 | } | |
6405 | } | |
6406 | ||
6407 | for (t = list; t && t != champ; t = TREE_CHAIN (t)) | |
6408 | { | |
85b71cf2 | 6409 | fate = more_specialized_class (champ, t); |
73aad9b9 JM |
6410 | if (fate != 1) |
6411 | return error_mark_node; | |
6412 | } | |
6413 | ||
6414 | return champ; | |
6415 | } | |
6416 | ||
8d08fdba | 6417 | /* called from the parser. */ |
e92cc029 | 6418 | |
8d08fdba | 6419 | void |
6633d636 | 6420 | do_decl_instantiation (declspecs, declarator, storage) |
f0e01782 | 6421 | tree declspecs, declarator, storage; |
8d08fdba | 6422 | { |
c11b6f21 | 6423 | tree decl = grokdeclarator (declarator, declspecs, NORMAL, 0, NULL_TREE); |
8d08fdba | 6424 | tree result = NULL_TREE; |
faae18ab | 6425 | int extern_p = 0; |
e8abc66f | 6426 | |
ec255269 MS |
6427 | if (! DECL_LANG_SPECIFIC (decl)) |
6428 | { | |
6429 | cp_error ("explicit instantiation of non-template `%#D'", decl); | |
6430 | return; | |
6431 | } | |
6432 | ||
e8abc66f | 6433 | /* If we've already seen this template instance, use it. */ |
6633d636 MS |
6434 | if (TREE_CODE (decl) == VAR_DECL) |
6435 | { | |
6436 | result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, 0); | |
6437 | if (result && TREE_CODE (result) != VAR_DECL) | |
6438 | result = NULL_TREE; | |
6439 | } | |
6440 | else if (TREE_CODE (decl) != FUNCTION_DECL) | |
6441 | { | |
6442 | cp_error ("explicit instantiation of `%#D'", decl); | |
6443 | return; | |
6444 | } | |
e1467ff2 MM |
6445 | else if (DECL_TEMPLATE_INSTANTIATION (decl)) |
6446 | result = decl; | |
98c1c668 | 6447 | |
7177d104 | 6448 | if (! result) |
faae18ab MS |
6449 | { |
6450 | cp_error ("no matching template for `%D' found", decl); | |
6451 | return; | |
6452 | } | |
7177d104 | 6453 | |
6633d636 MS |
6454 | if (! DECL_TEMPLATE_INFO (result)) |
6455 | { | |
6456 | cp_pedwarn ("explicit instantiation of non-template `%#D'", result); | |
6457 | return; | |
6458 | } | |
6459 | ||
a0a33927 MS |
6460 | if (flag_external_templates) |
6461 | return; | |
6462 | ||
f0e01782 | 6463 | if (storage == NULL_TREE) |
00595019 | 6464 | ; |
faae18ab MS |
6465 | else if (storage == ridpointers[(int) RID_EXTERN]) |
6466 | extern_p = 1; | |
f0e01782 MS |
6467 | else |
6468 | cp_error ("storage class `%D' applied to template instantiation", | |
6469 | storage); | |
5566b478 | 6470 | |
5566b478 | 6471 | mark_decl_instantiated (result, extern_p); |
44a8d0b3 | 6472 | repo_template_instantiated (result, extern_p); |
c91a56d2 MS |
6473 | if (! extern_p) |
6474 | instantiate_decl (result); | |
7177d104 MS |
6475 | } |
6476 | ||
faae18ab MS |
6477 | void |
6478 | mark_class_instantiated (t, extern_p) | |
6479 | tree t; | |
6480 | int extern_p; | |
6481 | { | |
6482 | SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t); | |
eb773359 JM |
6483 | SET_CLASSTYPE_INTERFACE_KNOWN (t); |
6484 | CLASSTYPE_INTERFACE_ONLY (t) = extern_p; | |
faae18ab MS |
6485 | CLASSTYPE_VTABLE_NEEDS_WRITING (t) = ! extern_p; |
6486 | TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p; | |
6487 | if (! extern_p) | |
6488 | { | |
6489 | CLASSTYPE_DEBUG_REQUESTED (t) = 1; | |
6490 | rest_of_type_compilation (t, 1); | |
6491 | } | |
6492 | } | |
e8abc66f | 6493 | |
7177d104 | 6494 | void |
ca79f85d JM |
6495 | do_type_instantiation (t, storage) |
6496 | tree t, storage; | |
7177d104 | 6497 | { |
e8abc66f MS |
6498 | int extern_p = 0; |
6499 | int nomem_p = 0; | |
5566b478 MS |
6500 | int static_p = 0; |
6501 | ||
ca79f85d JM |
6502 | if (TREE_CODE (t) == TYPE_DECL) |
6503 | t = TREE_TYPE (t); | |
6504 | ||
6505 | if (! IS_AGGR_TYPE (t) || ! CLASSTYPE_TEMPLATE_INFO (t)) | |
6506 | { | |
6507 | cp_error ("explicit instantiation of non-template type `%T'", t); | |
6508 | return; | |
6509 | } | |
6510 | ||
5566b478 | 6511 | complete_type (t); |
7177d104 | 6512 | |
a292b002 MS |
6513 | /* With -fexternal-templates, explicit instantiations are treated the same |
6514 | as implicit ones. */ | |
a0a33927 MS |
6515 | if (flag_external_templates) |
6516 | return; | |
6517 | ||
f0e01782 MS |
6518 | if (TYPE_SIZE (t) == NULL_TREE) |
6519 | { | |
6520 | cp_error ("explicit instantiation of `%#T' before definition of template", | |
6521 | t); | |
6522 | return; | |
6523 | } | |
6524 | ||
6525 | if (storage == NULL_TREE) | |
e8abc66f MS |
6526 | /* OK */; |
6527 | else if (storage == ridpointers[(int) RID_INLINE]) | |
6528 | nomem_p = 1; | |
f0e01782 MS |
6529 | else if (storage == ridpointers[(int) RID_EXTERN]) |
6530 | extern_p = 1; | |
5566b478 MS |
6531 | else if (storage == ridpointers[(int) RID_STATIC]) |
6532 | static_p = 1; | |
f0e01782 MS |
6533 | else |
6534 | { | |
6535 | cp_error ("storage class `%D' applied to template instantiation", | |
6536 | storage); | |
6537 | extern_p = 0; | |
6538 | } | |
6539 | ||
a292b002 | 6540 | /* We've already instantiated this. */ |
44a8d0b3 MS |
6541 | if (CLASSTYPE_EXPLICIT_INSTANTIATION (t) && ! CLASSTYPE_INTERFACE_ONLY (t) |
6542 | && extern_p) | |
6543 | return; | |
a292b002 | 6544 | |
f376e137 | 6545 | if (! CLASSTYPE_TEMPLATE_SPECIALIZATION (t)) |
44a8d0b3 MS |
6546 | { |
6547 | mark_class_instantiated (t, extern_p); | |
6548 | repo_template_instantiated (t, extern_p); | |
6549 | } | |
e8abc66f MS |
6550 | |
6551 | if (nomem_p) | |
6552 | return; | |
6553 | ||
7177d104 | 6554 | { |
db5ae43f | 6555 | tree tmp; |
5566b478 MS |
6556 | |
6557 | if (! static_p) | |
6558 | for (tmp = TYPE_METHODS (t); tmp; tmp = TREE_CHAIN (tmp)) | |
a7d87521 | 6559 | if (TREE_CODE (tmp) == FUNCTION_DECL |
1f109f0f | 6560 | && DECL_TEMPLATE_INSTANTIATION (tmp)) |
5566b478 MS |
6561 | { |
6562 | mark_decl_instantiated (tmp, extern_p); | |
6563 | repo_template_instantiated (tmp, extern_p); | |
6564 | if (! extern_p) | |
6565 | instantiate_decl (tmp); | |
6566 | } | |
6567 | ||
6568 | for (tmp = TYPE_FIELDS (t); tmp; tmp = TREE_CHAIN (tmp)) | |
6569 | if (TREE_CODE (tmp) == VAR_DECL && DECL_TEMPLATE_INSTANTIATION (tmp)) | |
863adfc0 | 6570 | { |
5566b478 | 6571 | mark_decl_instantiated (tmp, extern_p); |
863adfc0 | 6572 | repo_template_instantiated (tmp, extern_p); |
5566b478 MS |
6573 | if (! extern_p) |
6574 | instantiate_decl (tmp); | |
863adfc0 | 6575 | } |
7177d104 | 6576 | |
a292b002 | 6577 | for (tmp = CLASSTYPE_TAGS (t); tmp; tmp = TREE_CHAIN (tmp)) |
f376e137 MS |
6578 | if (IS_AGGR_TYPE (TREE_VALUE (tmp))) |
6579 | do_type_instantiation (TYPE_MAIN_DECL (TREE_VALUE (tmp)), storage); | |
a292b002 | 6580 | } |
8d08fdba | 6581 | } |
a28e3c7f | 6582 | |
f84b4be9 JM |
6583 | /* Produce the definition of D, a _DECL generated from a template. */ |
6584 | ||
a28e3c7f | 6585 | tree |
5566b478 MS |
6586 | instantiate_decl (d) |
6587 | tree d; | |
a28e3c7f | 6588 | { |
5566b478 MS |
6589 | tree ti = DECL_TEMPLATE_INFO (d); |
6590 | tree tmpl = TI_TEMPLATE (ti); | |
6591 | tree args = TI_ARGS (ti); | |
a221d52f | 6592 | tree td, temp; |
fee23f54 | 6593 | tree decl_pattern, code_pattern; |
5566b478 MS |
6594 | tree save_ti; |
6595 | int nested = in_function_p (); | |
6596 | int d_defined; | |
6597 | int pattern_defined; | |
5156628f MS |
6598 | int line = lineno; |
6599 | char *file = input_filename; | |
5566b478 | 6600 | |
f84b4be9 JM |
6601 | for (td = tmpl; |
6602 | DECL_TEMPLATE_INSTANTIATION (td) | |
6603 | /* This next clause handles friend templates defined inside | |
6604 | class templates. The friend templates are not really | |
6605 | instantiations from the point of view of the language, but | |
6606 | they are instantiations from the point of view of the | |
6607 | compiler. */ | |
6608 | || (DECL_TEMPLATE_INFO (td) && !DECL_TEMPLATE_SPECIALIZATION (td)); | |
6609 | ) | |
fee23f54 | 6610 | td = DECL_TI_TEMPLATE (td); |
27bb8339 | 6611 | |
fee23f54 JM |
6612 | /* In the case of a member template, decl_pattern is the partially |
6613 | instantiated declaration (in the instantiated class), and code_pattern | |
6614 | is the original template definition. */ | |
6615 | decl_pattern = DECL_TEMPLATE_RESULT (tmpl); | |
6616 | code_pattern = DECL_TEMPLATE_RESULT (td); | |
27bb8339 | 6617 | |
5566b478 MS |
6618 | if (TREE_CODE (d) == FUNCTION_DECL) |
6619 | { | |
6620 | d_defined = (DECL_INITIAL (d) != NULL_TREE); | |
fee23f54 | 6621 | pattern_defined = (DECL_INITIAL (code_pattern) != NULL_TREE); |
5566b478 MS |
6622 | } |
6623 | else | |
6624 | { | |
6625 | d_defined = ! DECL_IN_AGGR_P (d); | |
fee23f54 | 6626 | pattern_defined = ! DECL_IN_AGGR_P (code_pattern); |
5566b478 MS |
6627 | } |
6628 | ||
6629 | if (d_defined) | |
6630 | return d; | |
de22184b | 6631 | |
7ac63bca JM |
6632 | if (TREE_CODE (d) == FUNCTION_DECL) |
6633 | { | |
75650646 MM |
6634 | tree spec = retrieve_specialization (tmpl, args); |
6635 | ||
6636 | if (spec != NULL_TREE | |
6637 | && DECL_TEMPLATE_SPECIALIZATION (spec)) | |
6638 | return spec; | |
7ac63bca JM |
6639 | } |
6640 | ||
de22184b MS |
6641 | /* This needs to happen before any tsubsting. */ |
6642 | if (! push_tinst_level (d)) | |
6643 | return d; | |
6644 | ||
6645 | push_to_top_level (); | |
6646 | lineno = DECL_SOURCE_LINE (d); | |
6647 | input_filename = DECL_SOURCE_FILE (d); | |
6648 | ||
6649 | /* We need to set up DECL_INITIAL regardless of pattern_defined if the | |
6650 | variable is a static const initialized in the class body. */ | |
6651 | if (TREE_CODE (d) == VAR_DECL | |
fee23f54 | 6652 | && ! DECL_INITIAL (d) && DECL_INITIAL (code_pattern)) |
de22184b MS |
6653 | { |
6654 | pushclass (DECL_CONTEXT (d), 2); | |
fee23f54 | 6655 | DECL_INITIAL (d) = tsubst_expr (DECL_INITIAL (code_pattern), args, |
f7d98d58 | 6656 | tmpl); |
bd0f14dc | 6657 | cp_finish_decl (d, DECL_INITIAL (d), NULL_TREE, 0, LOOKUP_NORMAL); |
de22184b MS |
6658 | } |
6659 | ||
de22184b | 6660 | if (pattern_defined) |
5566b478 MS |
6661 | { |
6662 | repo_template_used (d); | |
6663 | ||
6664 | if (flag_external_templates && ! DECL_INTERFACE_KNOWN (d)) | |
6665 | { | |
6666 | if (flag_alt_external_templates) | |
6667 | { | |
6668 | if (interface_unknown) | |
6669 | warn_if_unknown_interface (d); | |
6670 | } | |
fee23f54 | 6671 | else if (DECL_INTERFACE_KNOWN (code_pattern)) |
5566b478 MS |
6672 | { |
6673 | DECL_INTERFACE_KNOWN (d) = 1; | |
fee23f54 | 6674 | DECL_NOT_REALLY_EXTERN (d) = ! DECL_EXTERNAL (code_pattern); |
5566b478 MS |
6675 | } |
6676 | else | |
fee23f54 | 6677 | warn_if_unknown_interface (code_pattern); |
5566b478 MS |
6678 | } |
6679 | ||
e92cc029 | 6680 | if (at_eof) |
5566b478 MS |
6681 | import_export_decl (d); |
6682 | } | |
6683 | ||
c4ae3f91 JM |
6684 | /* Reject all external templates except inline functions. */ |
6685 | if (DECL_INTERFACE_KNOWN (d) | |
6686 | && ! DECL_NOT_REALLY_EXTERN (d) | |
6687 | && ! (TREE_CODE (d) == FUNCTION_DECL && DECL_INLINE (d))) | |
6688 | goto out; | |
6689 | ||
6690 | /* Defer all templates except inline functions used in another function. */ | |
5566b478 | 6691 | if (! pattern_defined |
c4ae3f91 JM |
6692 | || (! (TREE_CODE (d) == FUNCTION_DECL && DECL_INLINE (d) && nested) |
6693 | && ! at_eof)) | |
5566b478 MS |
6694 | { |
6695 | add_pending_template (d); | |
de22184b | 6696 | goto out; |
5566b478 MS |
6697 | } |
6698 | ||
5156628f MS |
6699 | lineno = DECL_SOURCE_LINE (d); |
6700 | input_filename = DECL_SOURCE_FILE (d); | |
6701 | ||
5566b478 | 6702 | /* Trick tsubst into giving us a new decl in case the template changed. */ |
fee23f54 JM |
6703 | save_ti = DECL_TEMPLATE_INFO (decl_pattern); |
6704 | DECL_TEMPLATE_INFO (decl_pattern) = NULL_TREE; | |
2d6df019 JM |
6705 | /* decl_pattern has all but one level of template parms bound. Only pass |
6706 | in that one level of args. */ | |
a221d52f | 6707 | temp = innermost_args (args, DECL_TEMPLATE_SPECIALIZATION (decl_pattern)); |
f7d98d58 | 6708 | td = tsubst (decl_pattern, temp, tmpl); |
7b4f18a3 | 6709 | SET_DECL_IMPLICIT_INSTANTIATION (td); |
fee23f54 | 6710 | DECL_TEMPLATE_INFO (decl_pattern) = save_ti; |
5566b478 | 6711 | |
d11ad92e MS |
6712 | /* And set up DECL_INITIAL, since tsubst doesn't. */ |
6713 | if (TREE_CODE (td) == VAR_DECL) | |
5156628f MS |
6714 | { |
6715 | pushclass (DECL_CONTEXT (d), 2); | |
fee23f54 | 6716 | DECL_INITIAL (td) = tsubst_expr (DECL_INITIAL (code_pattern), args, |
f7d98d58 | 6717 | tmpl); |
5156628f MS |
6718 | popclass (1); |
6719 | } | |
d11ad92e | 6720 | |
5566b478 | 6721 | if (TREE_CODE (d) == FUNCTION_DECL) |
386b8a85 JM |
6722 | { |
6723 | /* Convince duplicate_decls to use the DECL_ARGUMENTS from the | |
6724 | new decl. */ | |
6725 | DECL_INITIAL (td) = error_mark_node; | |
6726 | ||
6727 | if (DECL_TEMPLATE_SPECIALIZATION (td) && !DECL_TEMPLATE_INFO (td)) | |
6728 | /* Set up the information about what is being specialized. */ | |
6729 | DECL_TEMPLATE_INFO (td) = DECL_TEMPLATE_INFO (d); | |
6730 | } | |
5566b478 MS |
6731 | duplicate_decls (td, d); |
6732 | if (TREE_CODE (d) == FUNCTION_DECL) | |
6733 | DECL_INITIAL (td) = 0; | |
6734 | ||
6735 | if (TREE_CODE (d) == VAR_DECL) | |
6736 | { | |
6737 | DECL_IN_AGGR_P (d) = 0; | |
6738 | if (DECL_INTERFACE_KNOWN (d)) | |
6739 | DECL_EXTERNAL (d) = ! DECL_NOT_REALLY_EXTERN (d); | |
6740 | else | |
6741 | { | |
6742 | DECL_EXTERNAL (d) = 1; | |
6743 | DECL_NOT_REALLY_EXTERN (d) = 1; | |
6744 | } | |
6745 | cp_finish_decl (d, DECL_INITIAL (d), NULL_TREE, 0, 0); | |
6746 | } | |
6747 | else if (TREE_CODE (d) == FUNCTION_DECL) | |
6748 | { | |
fee23f54 | 6749 | tree t = DECL_SAVED_TREE (code_pattern); |
5566b478 | 6750 | |
c11b6f21 | 6751 | start_function (NULL_TREE, d, NULL_TREE, 1); |
5566b478 MS |
6752 | store_parm_decls (); |
6753 | ||
e76a2646 MS |
6754 | if (t && TREE_CODE (t) == RETURN_INIT) |
6755 | { | |
6756 | store_return_init | |
6757 | (TREE_OPERAND (t, 0), | |
f7d98d58 | 6758 | tsubst_expr (TREE_OPERAND (t, 1), args, tmpl)); |
e76a2646 MS |
6759 | t = TREE_CHAIN (t); |
6760 | } | |
6761 | ||
5566b478 MS |
6762 | if (t && TREE_CODE (t) == CTOR_INITIALIZER) |
6763 | { | |
6764 | current_member_init_list | |
6765 | = tsubst_expr_values (TREE_OPERAND (t, 0), args); | |
6766 | current_base_init_list | |
6767 | = tsubst_expr_values (TREE_OPERAND (t, 1), args); | |
6768 | t = TREE_CHAIN (t); | |
6769 | } | |
6770 | ||
6771 | setup_vtbl_ptr (); | |
6772 | /* Always keep the BLOCK node associated with the outermost | |
956d6950 | 6773 | pair of curly braces of a function. These are needed |
5566b478 MS |
6774 | for correct operation of dwarfout.c. */ |
6775 | keep_next_level (); | |
6776 | ||
6777 | my_friendly_assert (TREE_CODE (t) == COMPOUND_STMT, 42); | |
f7d98d58 | 6778 | tsubst_expr (t, args, tmpl); |
a28e3c7f | 6779 | |
5566b478 | 6780 | finish_function (lineno, 0, nested); |
5566b478 MS |
6781 | } |
6782 | ||
de22184b | 6783 | out: |
5156628f MS |
6784 | lineno = line; |
6785 | input_filename = file; | |
6786 | ||
5566b478 | 6787 | pop_from_top_level (); |
5566b478 | 6788 | pop_tinst_level (); |
a28e3c7f | 6789 | |
a28e3c7f MS |
6790 | return d; |
6791 | } | |
5566b478 MS |
6792 | |
6793 | tree | |
6794 | tsubst_chain (t, argvec) | |
6795 | tree t, argvec; | |
6796 | { | |
6797 | if (t) | |
6798 | { | |
f7d98d58 | 6799 | tree first = tsubst (t, argvec, NULL_TREE); |
5566b478 MS |
6800 | tree last = first; |
6801 | ||
6802 | for (t = TREE_CHAIN (t); t; t = TREE_CHAIN (t)) | |
6803 | { | |
f7d98d58 | 6804 | tree x = tsubst (t, argvec, NULL_TREE); |
5566b478 MS |
6805 | TREE_CHAIN (last) = x; |
6806 | last = x; | |
6807 | } | |
6808 | ||
6809 | return first; | |
6810 | } | |
6811 | return NULL_TREE; | |
6812 | } | |
6813 | ||
824b9a4c | 6814 | static tree |
5566b478 MS |
6815 | tsubst_expr_values (t, argvec) |
6816 | tree t, argvec; | |
6817 | { | |
6818 | tree first = NULL_TREE; | |
6819 | tree *p = &first; | |
6820 | ||
6821 | for (; t; t = TREE_CHAIN (t)) | |
6822 | { | |
f7d98d58 JM |
6823 | tree pur = tsubst_copy (TREE_PURPOSE (t), argvec, NULL_TREE); |
6824 | tree val = tsubst_expr (TREE_VALUE (t), argvec, NULL_TREE); | |
5566b478 MS |
6825 | *p = build_tree_list (pur, val); |
6826 | p = &TREE_CHAIN (*p); | |
6827 | } | |
6828 | return first; | |
6829 | } | |
6830 | ||
6831 | tree last_tree; | |
6832 | ||
6833 | void | |
6834 | add_tree (t) | |
6835 | tree t; | |
6836 | { | |
6837 | last_tree = TREE_CHAIN (last_tree) = t; | |
6838 | } | |
73aad9b9 | 6839 | |
75650646 MM |
6840 | |
6841 | void | |
6842 | begin_tree () | |
6843 | { | |
6844 | saved_trees = tree_cons (NULL_TREE, last_tree, saved_trees); | |
6845 | last_tree = NULL_TREE; | |
6846 | } | |
6847 | ||
6848 | ||
6849 | void | |
6850 | end_tree () | |
6851 | { | |
6852 | my_friendly_assert (saved_trees != NULL_TREE, 0); | |
6853 | ||
6854 | last_tree = TREE_VALUE (saved_trees); | |
6855 | saved_trees = TREE_CHAIN (saved_trees); | |
6856 | } | |
6857 | ||
73aad9b9 JM |
6858 | /* D is an undefined function declaration in the presence of templates with |
6859 | the same name, listed in FNS. If one of them can produce D as an | |
6860 | instantiation, remember this so we can instantiate it at EOF if D has | |
6861 | not been defined by that time. */ | |
6862 | ||
6863 | void | |
6864 | add_maybe_template (d, fns) | |
6865 | tree d, fns; | |
6866 | { | |
6867 | tree t; | |
6868 | ||
6869 | if (DECL_MAYBE_TEMPLATE (d)) | |
6870 | return; | |
6871 | ||
e1467ff2 | 6872 | t = most_specialized (fns, d, NULL_TREE); |
73aad9b9 JM |
6873 | if (! t) |
6874 | return; | |
6875 | if (t == error_mark_node) | |
6876 | { | |
6877 | cp_error ("ambiguous template instantiation for `%D'", d); | |
6878 | return; | |
6879 | } | |
6880 | ||
6881 | *maybe_template_tail = perm_tree_cons (t, d, NULL_TREE); | |
6882 | maybe_template_tail = &TREE_CHAIN (*maybe_template_tail); | |
6883 | DECL_MAYBE_TEMPLATE (d) = 1; | |
6884 | } | |
b87692e5 MS |
6885 | |
6886 | /* Instantiate an enumerated type. Used by instantiate_class_template and | |
6887 | tsubst_expr. */ | |
6888 | ||
6889 | static tree | |
f7d98d58 | 6890 | tsubst_enum (tag, args, field_chain) |
98c1c668 | 6891 | tree tag, args; |
b3d5a58b | 6892 | tree * field_chain; |
b87692e5 | 6893 | { |
b3d5a58b JG |
6894 | extern tree current_local_enum; |
6895 | tree prev_local_enum = current_local_enum; | |
6896 | ||
b87692e5 MS |
6897 | tree newtag = start_enum (TYPE_IDENTIFIER (tag)); |
6898 | tree e, values = NULL_TREE; | |
6899 | ||
6900 | for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e)) | |
6901 | { | |
6902 | tree elt = build_enumerator (TREE_PURPOSE (e), | |
6903 | tsubst_expr (TREE_VALUE (e), args, | |
f7d98d58 | 6904 | NULL_TREE)); |
b87692e5 MS |
6905 | TREE_CHAIN (elt) = values; |
6906 | values = elt; | |
6907 | } | |
6908 | ||
6909 | finish_enum (newtag, values); | |
6910 | ||
b3d5a58b | 6911 | if (NULL != field_chain) |
86052cc3 | 6912 | *field_chain = grok_enum_decls (NULL_TREE); |
b3d5a58b JG |
6913 | |
6914 | current_local_enum = prev_local_enum; | |
6915 | ||
b87692e5 MS |
6916 | return newtag; |
6917 | } |