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