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