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