]>
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) |
73b0fce8 KL |
1745 | if (nargs == nparms) |
1746 | vec = copy_node (arglist); | |
1747 | else | |
1748 | { | |
1749 | /* We arrive here when a template with some default arguments | |
1750 | is used as template template argument. */ | |
1751 | is_tmpl_parm = 1; | |
1752 | vec = make_tree_vec (nparms); | |
1753 | for (i = 0; i < nparms; i++) | |
1754 | { | |
1755 | tree arg; | |
1756 | ||
1757 | if (i < nargs) | |
1758 | arg = TREE_VEC_ELT (arglist, i); | |
1759 | else if (TREE_CODE (TREE_VALUE (TREE_VEC_ELT (parms, i))) | |
1760 | == TYPE_DECL) | |
1761 | arg = tsubst (TREE_PURPOSE (TREE_VEC_ELT (parms, i)), | |
1762 | vec, i, in_decl); | |
1763 | else | |
1764 | arg = tsubst_expr (TREE_PURPOSE (TREE_VEC_ELT (parms, i)), | |
1765 | vec, i, in_decl); | |
1766 | ||
1767 | TREE_VEC_ELT (vec, i) = arg; | |
1768 | } | |
1769 | } | |
8d08fdba MS |
1770 | else |
1771 | { | |
1772 | vec = make_tree_vec (nparms); | |
75650646 | 1773 | |
8d08fdba MS |
1774 | for (i = 0; i < nparms; i++) |
1775 | { | |
a292b002 | 1776 | tree arg; |
75650646 | 1777 | tree parm = TREE_VEC_ELT (parms, i); |
a292b002 MS |
1778 | |
1779 | if (arglist) | |
1780 | { | |
1781 | arg = arglist; | |
1782 | arglist = TREE_CHAIN (arglist); | |
1783 | ||
1784 | if (arg == error_mark_node) | |
1785 | lost++; | |
1786 | else | |
1787 | arg = TREE_VALUE (arg); | |
1788 | } | |
75650646 MM |
1789 | else if (TREE_PURPOSE (parm) == NULL_TREE) |
1790 | { | |
1791 | my_friendly_assert (!require_all_arguments, 0); | |
1792 | break; | |
1793 | } | |
1794 | else if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL) | |
1795 | arg = tsubst (TREE_PURPOSE (parm), vec, i, in_decl); | |
d2e5ee5c | 1796 | else |
75650646 | 1797 | arg = tsubst_expr (TREE_PURPOSE (parm), vec, i, in_decl); |
a292b002 | 1798 | |
8d08fdba MS |
1799 | TREE_VEC_ELT (vec, i) = arg; |
1800 | } | |
1801 | } | |
1802 | for (i = 0; i < nparms; i++) | |
1803 | { | |
1804 | tree arg = TREE_VEC_ELT (vec, i); | |
a292b002 | 1805 | tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i)); |
8d08fdba | 1806 | tree val = 0; |
73b0fce8 | 1807 | int is_type, requires_type, is_tmpl_type, requires_tmpl_type; |
8d08fdba | 1808 | |
75650646 MM |
1809 | if (arg == NULL_TREE) |
1810 | /* We're out of arguments. */ | |
1811 | { | |
1812 | my_friendly_assert (!require_all_arguments, 0); | |
1813 | break; | |
1814 | } | |
1815 | ||
1816 | if (arg == error_mark_node) | |
1817 | { | |
1818 | cp_error ("template argument %d is invalid", i + 1); | |
1819 | lost++; | |
1820 | continue; | |
1821 | } | |
1822 | ||
73b0fce8 KL |
1823 | /* In case we are checking arguments inside a template template |
1824 | parameter, ARG that does not come from default argument is | |
1825 | also a TREE_LIST node */ | |
7bf2682f | 1826 | if (TREE_CODE (arg) == TREE_LIST && ! is_overloaded_fn (arg)) |
73b0fce8 KL |
1827 | { |
1828 | is_tmpl_parm = 1; | |
1829 | arg = TREE_VALUE (arg); | |
1830 | } | |
1831 | ||
1832 | /* Check if it is a class template. */ | |
1833 | is_tmpl_type = (TREE_CODE (arg) == TEMPLATE_DECL | |
1834 | && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL) | |
1835 | || (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM | |
1836 | && !CLASSTYPE_TEMPLATE_INFO (arg)); | |
1837 | if (is_tmpl_type && TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM) | |
1838 | arg = TYPE_STUB_DECL (arg); | |
1839 | ||
1840 | requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL; | |
1841 | is_type = TREE_CODE_CLASS (TREE_CODE (arg)) == 't' | |
1842 | || is_tmpl_type | |
1843 | || (is_tmpl_parm && TREE_CODE (arg) == TYPE_DECL); | |
1844 | requires_type = TREE_CODE (parm) == TYPE_DECL | |
1845 | || requires_tmpl_type; | |
5566b478 MS |
1846 | |
1847 | if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF | |
1848 | && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM) | |
1849 | { | |
1850 | cp_pedwarn ("to refer to a type member of a template parameter,"); | |
1851 | cp_pedwarn (" use `typename %E'", arg); | |
75650646 | 1852 | |
5566b478 MS |
1853 | arg = make_typename_type (TREE_OPERAND (arg, 0), |
1854 | TREE_OPERAND (arg, 1)); | |
1855 | is_type = 1; | |
1856 | } | |
8d08fdba MS |
1857 | if (is_type != requires_type) |
1858 | { | |
1859 | if (in_decl) | |
5566b478 | 1860 | { |
75650646 MM |
1861 | if (complain) |
1862 | { | |
1863 | cp_error ("type/value mismatch at argument %d in template parameter list for `%D'", | |
1864 | i + 1, in_decl); | |
1865 | if (is_type) | |
1866 | cp_error (" expected a constant of type `%T', got `%T'", | |
73b0fce8 KL |
1867 | TREE_TYPE (parm), |
1868 | (is_tmpl_type ? DECL_NAME (arg) : arg)); | |
75650646 MM |
1869 | else |
1870 | cp_error (" expected a type, got `%E'", arg); | |
1871 | } | |
5566b478 | 1872 | } |
8d08fdba MS |
1873 | lost++; |
1874 | TREE_VEC_ELT (vec, i) = error_mark_node; | |
1875 | continue; | |
1876 | } | |
73b0fce8 KL |
1877 | if (is_tmpl_type ^ requires_tmpl_type) |
1878 | { | |
1879 | if (in_decl) | |
1880 | { | |
1881 | cp_error ("type/value mismatch at argument %d in template parameter list for `%D'", | |
1882 | i, in_decl); | |
1883 | if (is_tmpl_type) | |
1884 | cp_error (" expected a type, got `%T'", DECL_NAME (arg)); | |
1885 | else | |
1886 | cp_error (" expected a class template, got `%T'", arg); | |
1887 | } | |
1888 | lost++; | |
1889 | TREE_VEC_ELT (vec, i) = error_mark_node; | |
1890 | continue; | |
1891 | } | |
1892 | if (is_tmpl_parm) | |
1893 | { | |
1894 | if (requires_tmpl_type) | |
1895 | { | |
1896 | cp_error ("nested template template parameter not implemented"); | |
1897 | lost++; | |
1898 | TREE_VEC_ELT (vec, i) = error_mark_node; | |
1899 | } | |
1900 | continue; | |
1901 | } | |
1902 | ||
8d08fdba | 1903 | if (is_type) |
ec255269 | 1904 | { |
73b0fce8 KL |
1905 | if (requires_tmpl_type) |
1906 | { | |
1907 | tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm); | |
1908 | tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg); | |
1909 | ||
1910 | /* The parameter and argument roles have to be switched | |
1911 | here in order to handle default arguments properly. | |
1912 | For example, | |
1913 | template<template <class> class TT> void f(TT<int>) | |
1914 | should be able to accept vector<int> which comes from | |
1915 | template <class T, class Allcator = allocator> | |
1916 | class vector. */ | |
1917 | ||
1918 | val = coerce_template_parms (argparm, parmparm, in_decl, 1, 1); | |
1919 | if (val != error_mark_node) | |
1920 | val = arg; | |
1921 | ||
1922 | /* TEMPLATE_TEMPLATE_PARM node is preferred over | |
1923 | TEMPLATE_DECL. */ | |
1924 | if (val != error_mark_node | |
1925 | && DECL_TEMPLATE_TEMPLATE_PARM_P (val)) | |
1926 | val = TREE_TYPE (val); | |
1927 | } | |
1928 | else | |
ec255269 | 1929 | { |
73b0fce8 KL |
1930 | val = groktypename (arg); |
1931 | if (! processing_template_decl) | |
ec255269 | 1932 | { |
73b0fce8 KL |
1933 | tree t = target_type (val); |
1934 | if (TREE_CODE (t) != TYPENAME_TYPE | |
1935 | && IS_AGGR_TYPE (t) | |
1936 | && decl_function_context (TYPE_MAIN_DECL (t))) | |
1937 | { | |
1938 | cp_error ("type `%T' composed from a local class is not a valid template-argument", | |
1939 | val); | |
1940 | return error_mark_node; | |
1941 | } | |
ec255269 MS |
1942 | } |
1943 | } | |
1944 | } | |
8d08fdba MS |
1945 | else |
1946 | { | |
98c1c668 | 1947 | tree t = tsubst (TREE_TYPE (parm), vec, |
75b0bbce | 1948 | TREE_VEC_LENGTH (vec), in_decl); |
75650646 | 1949 | |
4bfc4dda | 1950 | if (processing_template_decl) |
75650646 MM |
1951 | arg = maybe_fold_nontype_arg (arg); |
1952 | ||
1953 | if (!uses_template_parms (arg) && !uses_template_parms (t)) | |
1954 | /* We used to call digest_init here. However, digest_init | |
1955 | will report errors, which we don't want when complain | |
1956 | is zero. More importantly, digest_init will try too | |
1957 | hard to convert things: for example, `0' should not be | |
1958 | converted to pointer type at this point according to | |
1959 | the standard. Accepting this is not merely an | |
1960 | extension, since deciding whether or not these | |
1961 | conversions can occur is part of determining which | |
1962 | function template to call, or whether a given epxlicit | |
1963 | argument specification is legal. */ | |
e1467ff2 | 1964 | val = convert_nontype_argument (t, arg); |
5566b478 | 1965 | else |
6ba4439c MM |
1966 | val = arg; |
1967 | ||
75650646 MM |
1968 | if (val == NULL_TREE) |
1969 | val = error_mark_node; | |
1970 | else if (val == error_mark_node && complain) | |
1971 | cp_error ("could not convert template argument `%E' to `%T'", | |
1972 | arg, t); | |
8d08fdba MS |
1973 | } |
1974 | ||
1975 | if (val == error_mark_node) | |
1976 | lost++; | |
1977 | ||
1978 | TREE_VEC_ELT (vec, i) = val; | |
1979 | } | |
1980 | if (lost) | |
1981 | return error_mark_node; | |
1982 | return vec; | |
1983 | } | |
1984 | ||
bd6dd845 | 1985 | static int |
5566b478 MS |
1986 | comp_template_args (oldargs, newargs) |
1987 | tree oldargs, newargs; | |
1988 | { | |
1989 | int i; | |
1990 | ||
386b8a85 JM |
1991 | if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs)) |
1992 | return 0; | |
1993 | ||
5566b478 MS |
1994 | for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i) |
1995 | { | |
1996 | tree nt = TREE_VEC_ELT (newargs, i); | |
1997 | tree ot = TREE_VEC_ELT (oldargs, i); | |
1998 | ||
1999 | if (nt == ot) | |
2000 | continue; | |
2001 | if (TREE_CODE (nt) != TREE_CODE (ot)) | |
2002 | return 0; | |
00d3396f JM |
2003 | if (TREE_CODE (nt) == TREE_VEC) |
2004 | { | |
2005 | /* For member templates */ | |
2006 | if (comp_template_args (nt, ot)) | |
2007 | continue; | |
2008 | } | |
2009 | else if (TREE_CODE_CLASS (TREE_CODE (ot)) == 't') | |
67d743fe MS |
2010 | { |
2011 | if (comptypes (ot, nt, 1)) | |
2012 | continue; | |
2013 | } | |
2014 | else if (cp_tree_equal (ot, nt) > 0) | |
5566b478 MS |
2015 | continue; |
2016 | return 0; | |
2017 | } | |
2018 | return 1; | |
2019 | } | |
2020 | ||
8d08fdba MS |
2021 | /* Given class template name and parameter list, produce a user-friendly name |
2022 | for the instantiation. */ | |
e92cc029 | 2023 | |
8d08fdba | 2024 | static char * |
75650646 | 2025 | mangle_class_name_for_template (name, parms, arglist, ctx) |
8d08fdba MS |
2026 | char *name; |
2027 | tree parms, arglist; | |
75650646 | 2028 | tree ctx; |
8d08fdba MS |
2029 | { |
2030 | static struct obstack scratch_obstack; | |
2031 | static char *scratch_firstobj; | |
2032 | int i, nparms; | |
8d08fdba MS |
2033 | |
2034 | if (!scratch_firstobj) | |
fc378698 | 2035 | gcc_obstack_init (&scratch_obstack); |
8d08fdba MS |
2036 | else |
2037 | obstack_free (&scratch_obstack, scratch_firstobj); | |
fc378698 | 2038 | scratch_firstobj = obstack_alloc (&scratch_obstack, 1); |
8d08fdba MS |
2039 | |
2040 | #if 0 | |
2041 | #define buflen sizeof(buf) | |
2042 | #define check if (bufp >= buf+buflen-1) goto too_long | |
2043 | #define ccat(c) *bufp++=(c); check | |
2044 | #define advance bufp+=strlen(bufp); check | |
2045 | #define cat(s) strncpy(bufp, s, buf+buflen-bufp-1); advance | |
2046 | #else | |
2047 | #define check | |
2048 | #define ccat(c) obstack_1grow (&scratch_obstack, (c)); | |
2049 | #define advance | |
2050 | #define cat(s) obstack_grow (&scratch_obstack, (s), strlen (s)) | |
2051 | #endif | |
8d08fdba | 2052 | |
75650646 MM |
2053 | if (ctx) |
2054 | { | |
e1467ff2 MM |
2055 | char* s; |
2056 | ||
2057 | if (TREE_CODE (ctx) == FUNCTION_DECL) | |
2058 | s = fndecl_as_string(ctx, 0); | |
2059 | else if (TREE_CODE_CLASS (TREE_CODE (ctx)) == 't') | |
2060 | s = type_as_string(ctx, 0); | |
2061 | else | |
2062 | my_friendly_abort (0); | |
75650646 MM |
2063 | cat (s); |
2064 | cat ("::"); | |
2065 | } | |
8d08fdba MS |
2066 | cat (name); |
2067 | ccat ('<'); | |
2068 | nparms = TREE_VEC_LENGTH (parms); | |
2069 | my_friendly_assert (nparms == TREE_VEC_LENGTH (arglist), 268); | |
2070 | for (i = 0; i < nparms; i++) | |
2071 | { | |
a292b002 MS |
2072 | tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i)); |
2073 | tree arg = TREE_VEC_ELT (arglist, i); | |
8d08fdba MS |
2074 | |
2075 | if (i) | |
2076 | ccat (','); | |
2077 | ||
a292b002 | 2078 | if (TREE_CODE (parm) == TYPE_DECL) |
8d08fdba MS |
2079 | { |
2080 | cat (type_as_string (arg, 0)); | |
2081 | continue; | |
2082 | } | |
73b0fce8 KL |
2083 | else if (TREE_CODE (parm) == TEMPLATE_DECL) |
2084 | { | |
2085 | if (TREE_CODE (arg) == TEMPLATE_DECL) | |
2086 | /* Already substituted with real template. Just output | |
2087 | the template name here */ | |
2088 | cat (IDENTIFIER_POINTER (DECL_NAME (arg))); | |
2089 | else | |
2090 | /* Output the parameter declaration */ | |
2091 | cat (type_as_string (arg, 0)); | |
2092 | continue; | |
2093 | } | |
8d08fdba MS |
2094 | else |
2095 | my_friendly_assert (TREE_CODE (parm) == PARM_DECL, 269); | |
2096 | ||
2097 | if (TREE_CODE (arg) == TREE_LIST) | |
2098 | { | |
2099 | /* New list cell was built because old chain link was in | |
2100 | use. */ | |
2101 | my_friendly_assert (TREE_PURPOSE (arg) == NULL_TREE, 270); | |
2102 | arg = TREE_VALUE (arg); | |
2103 | } | |
2104 | /* No need to check arglist against parmlist here; we did that | |
2105 | in coerce_template_parms, called from lookup_template_class. */ | |
2106 | cat (expr_as_string (arg, 0)); | |
2107 | } | |
2108 | { | |
2109 | char *bufp = obstack_next_free (&scratch_obstack); | |
2110 | int offset = 0; | |
2111 | while (bufp[offset - 1] == ' ') | |
2112 | offset--; | |
2113 | obstack_blank_fast (&scratch_obstack, offset); | |
2114 | ||
2115 | /* B<C<char> >, not B<C<char>> */ | |
2116 | if (bufp[offset - 1] == '>') | |
2117 | ccat (' '); | |
2118 | } | |
2119 | ccat ('>'); | |
2120 | ccat ('\0'); | |
2121 | return (char *) obstack_base (&scratch_obstack); | |
2122 | ||
8926095f | 2123 | #if 0 |
8d08fdba | 2124 | too_long: |
8926095f | 2125 | #endif |
8d08fdba MS |
2126 | fatal ("out of (preallocated) string space creating template instantiation name"); |
2127 | /* NOTREACHED */ | |
2128 | return NULL; | |
2129 | } | |
2130 | ||
bd6dd845 | 2131 | static tree |
5566b478 MS |
2132 | classtype_mangled_name (t) |
2133 | tree t; | |
2134 | { | |
2135 | if (CLASSTYPE_TEMPLATE_INFO (t) | |
2136 | && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t))) | |
2137 | { | |
2138 | tree name = DECL_NAME (CLASSTYPE_TI_TEMPLATE (t)); | |
2139 | char *mangled_name = mangle_class_name_for_template | |
2140 | (IDENTIFIER_POINTER (name), | |
98c1c668 | 2141 | DECL_INNERMOST_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (t)), |
e1467ff2 | 2142 | CLASSTYPE_TI_ARGS (t), DECL_CONTEXT (t)); |
5566b478 MS |
2143 | tree id = get_identifier (mangled_name); |
2144 | IDENTIFIER_TEMPLATE (id) = name; | |
2145 | return id; | |
2146 | } | |
2147 | else | |
2148 | return TYPE_IDENTIFIER (t); | |
2149 | } | |
2150 | ||
2151 | static void | |
2152 | add_pending_template (d) | |
2153 | tree d; | |
2154 | { | |
e92cc029 MS |
2155 | tree ti; |
2156 | ||
2157 | if (TREE_CODE_CLASS (TREE_CODE (d)) == 't') | |
2158 | ti = CLASSTYPE_TEMPLATE_INFO (d); | |
2159 | else | |
2160 | ti = DECL_TEMPLATE_INFO (d); | |
2161 | ||
824b9a4c | 2162 | if (TI_PENDING_TEMPLATE_FLAG (ti)) |
5566b478 MS |
2163 | return; |
2164 | ||
2165 | *template_tail = perm_tree_cons | |
2166 | (current_function_decl, d, NULL_TREE); | |
2167 | template_tail = &TREE_CHAIN (*template_tail); | |
824b9a4c | 2168 | TI_PENDING_TEMPLATE_FLAG (ti) = 1; |
5566b478 MS |
2169 | } |
2170 | ||
386b8a85 JM |
2171 | |
2172 | /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS (which | |
2173 | may be either a _DECL or an overloaded function or an | |
2174 | IDENTIFIER_NODE), and ARGLIST. */ | |
2175 | ||
2176 | tree | |
2177 | lookup_template_function (fns, arglist) | |
2178 | tree fns, arglist; | |
2179 | { | |
2180 | if (fns == NULL_TREE) | |
2181 | { | |
2182 | cp_error ("non-template used as template"); | |
2183 | return error_mark_node; | |
2184 | } | |
2185 | ||
2186 | if (arglist != NULL_TREE && !TREE_PERMANENT (arglist)) | |
4de02abd | 2187 | copy_to_permanent (arglist); |
386b8a85 JM |
2188 | |
2189 | return build_min (TEMPLATE_ID_EXPR, | |
2190 | TREE_TYPE (fns) | |
2191 | ? TREE_TYPE (fns) : unknown_type_node, | |
2192 | fns, arglist); | |
2193 | } | |
2194 | ||
2195 | ||
8d08fdba MS |
2196 | /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of |
2197 | parameters, find the desired type. | |
2198 | ||
2199 | D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments. | |
2200 | Since ARGLIST is build on the decl_obstack, we must copy it here | |
2201 | to keep it from being reclaimed when the decl storage is reclaimed. | |
2202 | ||
2203 | IN_DECL, if non-NULL, is the template declaration we are trying to | |
75650646 MM |
2204 | instantiate. |
2205 | ||
2206 | If the template class is really a local class in a template | |
2207 | function, then the FUNCTION_CONTEXT is the function in which it is | |
2208 | being instantiated. */ | |
e92cc029 | 2209 | |
8d08fdba | 2210 | tree |
e1467ff2 | 2211 | lookup_template_class (d1, arglist, in_decl, context) |
8d08fdba MS |
2212 | tree d1, arglist; |
2213 | tree in_decl; | |
e1467ff2 | 2214 | tree context; |
8d08fdba MS |
2215 | { |
2216 | tree template, parmlist; | |
2217 | char *mangled_name; | |
5566b478 | 2218 | tree id, t; |
5566b478 MS |
2219 | |
2220 | if (TREE_CODE (d1) == IDENTIFIER_NODE) | |
2221 | { | |
73b0fce8 KL |
2222 | if (IDENTIFIER_LOCAL_VALUE (d1) |
2223 | && DECL_TEMPLATE_TEMPLATE_PARM_P (IDENTIFIER_LOCAL_VALUE (d1))) | |
2224 | template = IDENTIFIER_LOCAL_VALUE (d1); | |
2225 | else | |
2226 | { | |
2227 | template = IDENTIFIER_GLOBAL_VALUE (d1); /* XXX */ | |
2228 | if (! template) | |
2229 | template = IDENTIFIER_CLASS_VALUE (d1); | |
2230 | } | |
5566b478 | 2231 | } |
c91a56d2 MS |
2232 | else if (TREE_CODE (d1) == TYPE_DECL && IS_AGGR_TYPE (TREE_TYPE (d1))) |
2233 | { | |
7bf2682f MM |
2234 | if (CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (d1)) == NULL_TREE) |
2235 | return error_mark_node; | |
c91a56d2 MS |
2236 | template = CLASSTYPE_TI_TEMPLATE (TREE_TYPE (d1)); |
2237 | d1 = DECL_NAME (template); | |
2238 | } | |
5566b478 MS |
2239 | else if (TREE_CODE_CLASS (TREE_CODE (d1)) == 't' && IS_AGGR_TYPE (d1)) |
2240 | { | |
2241 | template = CLASSTYPE_TI_TEMPLATE (d1); | |
2242 | d1 = DECL_NAME (template); | |
2243 | } | |
2244 | else | |
2245 | my_friendly_abort (272); | |
8d08fdba | 2246 | |
8d08fdba MS |
2247 | /* With something like `template <class T> class X class X { ... };' |
2248 | we could end up with D1 having nothing but an IDENTIFIER_LOCAL_VALUE. | |
2249 | We don't want to do that, but we have to deal with the situation, so | |
2250 | let's give them some syntax errors to chew on instead of a crash. */ | |
2251 | if (! template) | |
2252 | return error_mark_node; | |
2253 | if (TREE_CODE (template) != TEMPLATE_DECL) | |
2254 | { | |
2255 | cp_error ("non-template type `%T' used as a template", d1); | |
2256 | if (in_decl) | |
2257 | cp_error_at ("for template declaration `%D'", in_decl); | |
2258 | return error_mark_node; | |
2259 | } | |
8d08fdba | 2260 | |
73b0fce8 KL |
2261 | if (DECL_TEMPLATE_TEMPLATE_PARM_P (template)) |
2262 | { | |
2263 | /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store | |
2264 | template arguments */ | |
2265 | ||
2266 | tree parm = copy_template_template_parm (TREE_TYPE (template)); | |
2267 | tree template2 = TYPE_STUB_DECL (parm); | |
2268 | tree arglist2; | |
2269 | ||
2270 | CLASSTYPE_GOT_SEMICOLON (parm) = 1; | |
2271 | parmlist = DECL_INNERMOST_TEMPLATE_PARMS (template); | |
2272 | ||
2273 | arglist2 = coerce_template_parms (parmlist, arglist, template, 1, 1); | |
2274 | if (arglist2 == error_mark_node) | |
2275 | return error_mark_node; | |
2276 | ||
2277 | arglist2 = copy_to_permanent (arglist2); | |
2278 | CLASSTYPE_TEMPLATE_INFO (parm) | |
2279 | = perm_tree_cons (template2, arglist2, NULL_TREE); | |
2280 | TYPE_SIZE (parm) = 0; | |
2281 | return parm; | |
2282 | } | |
e1467ff2 MM |
2283 | else if (PRIMARY_TEMPLATE_P (template) |
2284 | || (TREE_CODE (TYPE_CONTEXT (TREE_TYPE (template))) | |
2285 | == FUNCTION_DECL)) | |
8d08fdba | 2286 | { |
98c1c668 | 2287 | parmlist = DECL_INNERMOST_TEMPLATE_PARMS (template); |
5566b478 | 2288 | |
75650646 MM |
2289 | arglist = coerce_template_parms (parmlist, arglist, template, |
2290 | 1, 1); | |
5566b478 MS |
2291 | if (arglist == error_mark_node) |
2292 | return error_mark_node; | |
2293 | if (uses_template_parms (arglist)) | |
2294 | { | |
2295 | tree found; | |
2296 | if (comp_template_args | |
2297 | (CLASSTYPE_TI_ARGS (TREE_TYPE (template)), arglist)) | |
2298 | found = TREE_TYPE (template); | |
2299 | else | |
2300 | { | |
2301 | for (found = DECL_TEMPLATE_INSTANTIATIONS (template); | |
2302 | found; found = TREE_CHAIN (found)) | |
2303 | { | |
2304 | if (TI_USES_TEMPLATE_PARMS (found) | |
2305 | && comp_template_args (TREE_PURPOSE (found), arglist)) | |
2306 | break; | |
2307 | } | |
2308 | if (found) | |
2309 | found = TREE_VALUE (found); | |
2310 | } | |
2311 | ||
2312 | if (found) | |
2313 | { | |
2314 | if (can_free (&permanent_obstack, arglist)) | |
2315 | obstack_free (&permanent_obstack, arglist); | |
2316 | return found; | |
2317 | } | |
2318 | } | |
2319 | ||
8d08fdba | 2320 | mangled_name = mangle_class_name_for_template (IDENTIFIER_POINTER (d1), |
75650646 MM |
2321 | parmlist, |
2322 | arglist, | |
e1467ff2 | 2323 | context); |
8d08fdba | 2324 | id = get_identifier (mangled_name); |
5566b478 | 2325 | IDENTIFIER_TEMPLATE (id) = d1; |
8d08fdba | 2326 | |
5566b478 | 2327 | maybe_push_to_top_level (uses_template_parms (arglist)); |
fc378698 | 2328 | t = xref_tag_from_type (TREE_TYPE (template), id, 1); |
75650646 | 2329 | |
e1467ff2 | 2330 | if (context != NULL_TREE) |
75650646 MM |
2331 | { |
2332 | /* Set up the context for the type_decl correctly. Note | |
2333 | that we must clear DECL_ASSEMBLER_NAME to fool | |
2334 | build_overload_name into creating a new name. */ | |
2335 | tree type_decl = TYPE_STUB_DECL (t); | |
2336 | ||
e1467ff2 MM |
2337 | TYPE_CONTEXT (t) = context; |
2338 | DECL_CONTEXT (type_decl) = context; | |
75650646 MM |
2339 | DECL_ASSEMBLER_NAME (type_decl) = DECL_NAME (type_decl); |
2340 | DECL_ASSEMBLER_NAME (type_decl) = | |
2341 | get_identifier (build_overload_name (t, 1, 1)); | |
2342 | } | |
2343 | ||
5566b478 | 2344 | pop_from_top_level (); |
8926095f | 2345 | } |
5566b478 | 2346 | else |
8d08fdba | 2347 | { |
5566b478 | 2348 | tree ctx = lookup_template_class (TYPE_CONTEXT (TREE_TYPE (template)), |
75650646 | 2349 | arglist, in_decl, NULL_TREE); |
5566b478 MS |
2350 | id = d1; |
2351 | arglist = CLASSTYPE_TI_ARGS (ctx); | |
8d08fdba | 2352 | |
5566b478 | 2353 | if (TYPE_BEING_DEFINED (ctx) && ctx == current_class_type) |
8d08fdba | 2354 | { |
5156628f MS |
2355 | int save_temp = processing_template_decl; |
2356 | processing_template_decl = 0; | |
fc378698 | 2357 | t = xref_tag_from_type (TREE_TYPE (template), id, 0); |
5156628f | 2358 | processing_template_decl = save_temp; |
8d08fdba MS |
2359 | } |
2360 | else | |
2361 | { | |
5566b478 MS |
2362 | t = lookup_nested_type_by_name (ctx, id); |
2363 | my_friendly_assert (t != NULL_TREE, 42); | |
8d08fdba | 2364 | } |
5566b478 MS |
2365 | } |
2366 | ||
e92cc029 | 2367 | /* Seems to be wanted. */ |
5566b478 MS |
2368 | CLASSTYPE_GOT_SEMICOLON (t) = 1; |
2369 | ||
2370 | if (! CLASSTYPE_TEMPLATE_INFO (t)) | |
2371 | { | |
2372 | arglist = copy_to_permanent (arglist); | |
2373 | CLASSTYPE_TEMPLATE_INFO (t) | |
2374 | = perm_tree_cons (template, arglist, NULL_TREE); | |
2375 | DECL_TEMPLATE_INSTANTIATIONS (template) = perm_tree_cons | |
2376 | (arglist, t, DECL_TEMPLATE_INSTANTIATIONS (template)); | |
2377 | TI_USES_TEMPLATE_PARMS (DECL_TEMPLATE_INSTANTIATIONS (template)) | |
2378 | = uses_template_parms (arglist); | |
2379 | ||
2380 | SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t); | |
2381 | ||
e92cc029 | 2382 | /* We need to set this again after CLASSTYPE_TEMPLATE_INFO is set up. */ |
5566b478 | 2383 | DECL_ASSEMBLER_NAME (TYPE_MAIN_DECL (t)) = id; |
386b8a85 | 2384 | /* if (! uses_template_parms (arglist)) */ |
5566b478 MS |
2385 | DECL_ASSEMBLER_NAME (TYPE_MAIN_DECL (t)) |
2386 | = get_identifier (build_overload_name (t, 1, 1)); | |
2387 | ||
2388 | if (flag_external_templates && ! uses_template_parms (arglist) | |
2389 | && CLASSTYPE_INTERFACE_KNOWN (TREE_TYPE (template)) | |
2390 | && ! CLASSTYPE_INTERFACE_ONLY (TREE_TYPE (template))) | |
e92cc029 | 2391 | add_pending_template (t); |
8d08fdba | 2392 | } |
8d08fdba | 2393 | |
5566b478 | 2394 | return t; |
8d08fdba MS |
2395 | } |
2396 | \f | |
51c184be | 2397 | /* Should be defined in parse.h. */ |
8d08fdba MS |
2398 | extern int yychar; |
2399 | ||
2400 | int | |
2401 | uses_template_parms (t) | |
2402 | tree t; | |
2403 | { | |
2404 | if (!t) | |
2405 | return 0; | |
2406 | switch (TREE_CODE (t)) | |
2407 | { | |
2408 | case INDIRECT_REF: | |
2409 | case COMPONENT_REF: | |
2410 | /* We assume that the object must be instantiated in order to build | |
2411 | the COMPONENT_REF, so we test only whether the type of the | |
2412 | COMPONENT_REF uses template parms. */ | |
2413 | return uses_template_parms (TREE_TYPE (t)); | |
2414 | ||
2415 | case IDENTIFIER_NODE: | |
2416 | if (!IDENTIFIER_TEMPLATE (t)) | |
2417 | return 0; | |
5566b478 | 2418 | my_friendly_abort (42); |
8d08fdba MS |
2419 | |
2420 | /* aggregates of tree nodes */ | |
2421 | case TREE_VEC: | |
2422 | { | |
2423 | int i = TREE_VEC_LENGTH (t); | |
2424 | while (i--) | |
2425 | if (uses_template_parms (TREE_VEC_ELT (t, i))) | |
2426 | return 1; | |
2427 | return 0; | |
2428 | } | |
2429 | case TREE_LIST: | |
2430 | if (uses_template_parms (TREE_PURPOSE (t)) | |
2431 | || uses_template_parms (TREE_VALUE (t))) | |
2432 | return 1; | |
2433 | return uses_template_parms (TREE_CHAIN (t)); | |
2434 | ||
2435 | /* constructed type nodes */ | |
2436 | case POINTER_TYPE: | |
2437 | case REFERENCE_TYPE: | |
2438 | return uses_template_parms (TREE_TYPE (t)); | |
2439 | case RECORD_TYPE: | |
b7484fbe MS |
2440 | if (TYPE_PTRMEMFUNC_FLAG (t)) |
2441 | return uses_template_parms (TYPE_PTRMEMFUNC_FN_TYPE (t)); | |
8d08fdba | 2442 | case UNION_TYPE: |
5566b478 | 2443 | if (! CLASSTYPE_TEMPLATE_INFO (t)) |
8d08fdba | 2444 | return 0; |
5566b478 | 2445 | return uses_template_parms (TREE_VALUE (CLASSTYPE_TEMPLATE_INFO (t))); |
8d08fdba MS |
2446 | case FUNCTION_TYPE: |
2447 | if (uses_template_parms (TYPE_ARG_TYPES (t))) | |
2448 | return 1; | |
2449 | return uses_template_parms (TREE_TYPE (t)); | |
2450 | case ARRAY_TYPE: | |
2451 | if (uses_template_parms (TYPE_DOMAIN (t))) | |
2452 | return 1; | |
2453 | return uses_template_parms (TREE_TYPE (t)); | |
2454 | case OFFSET_TYPE: | |
2455 | if (uses_template_parms (TYPE_OFFSET_BASETYPE (t))) | |
2456 | return 1; | |
2457 | return uses_template_parms (TREE_TYPE (t)); | |
2458 | case METHOD_TYPE: | |
75b0bbce | 2459 | if (uses_template_parms (TYPE_METHOD_BASETYPE (t))) |
8d08fdba MS |
2460 | return 1; |
2461 | if (uses_template_parms (TYPE_ARG_TYPES (t))) | |
2462 | return 1; | |
2463 | return uses_template_parms (TREE_TYPE (t)); | |
2464 | ||
2465 | /* decl nodes */ | |
2466 | case TYPE_DECL: | |
5566b478 MS |
2467 | return uses_template_parms (TREE_TYPE (t)); |
2468 | ||
73b0fce8 KL |
2469 | case TEMPLATE_DECL: |
2470 | /* A template template parameter is encountered */ | |
2471 | if (DECL_TEMPLATE_TEMPLATE_PARM_P (t)) | |
2472 | /* We are parsing a template declaration */ | |
2473 | return 1; | |
2474 | /* We are instantiating templates with template template | |
2475 | parameter */ | |
2476 | return 0; | |
2477 | ||
8d08fdba | 2478 | case FUNCTION_DECL: |
5566b478 MS |
2479 | case VAR_DECL: |
2480 | /* ??? What about FIELD_DECLs? */ | |
2481 | if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t) | |
2482 | && uses_template_parms (DECL_TI_ARGS (t))) | |
8d08fdba MS |
2483 | return 1; |
2484 | /* fall through */ | |
5566b478 | 2485 | case CONST_DECL: |
8d08fdba | 2486 | case PARM_DECL: |
5566b478 MS |
2487 | if (uses_template_parms (TREE_TYPE (t))) |
2488 | return 1; | |
8d08fdba MS |
2489 | if (DECL_CONTEXT (t) && uses_template_parms (DECL_CONTEXT (t))) |
2490 | return 1; | |
8d08fdba MS |
2491 | return 0; |
2492 | ||
2493 | case CALL_EXPR: | |
2494 | return uses_template_parms (TREE_TYPE (t)); | |
2495 | case ADDR_EXPR: | |
2496 | return uses_template_parms (TREE_OPERAND (t, 0)); | |
2497 | ||
2498 | /* template parm nodes */ | |
2499 | case TEMPLATE_TYPE_PARM: | |
73b0fce8 | 2500 | case TEMPLATE_TEMPLATE_PARM: |
8d08fdba MS |
2501 | case TEMPLATE_CONST_PARM: |
2502 | return 1; | |
2503 | ||
2504 | /* simple type nodes */ | |
2505 | case INTEGER_TYPE: | |
2506 | if (uses_template_parms (TYPE_MIN_VALUE (t))) | |
2507 | return 1; | |
2508 | return uses_template_parms (TYPE_MAX_VALUE (t)); | |
2509 | ||
2510 | case REAL_TYPE: | |
37c46b43 | 2511 | case COMPLEX_TYPE: |
8d08fdba | 2512 | case VOID_TYPE: |
2986ae00 | 2513 | case BOOLEAN_TYPE: |
8d08fdba MS |
2514 | return 0; |
2515 | ||
85b71cf2 JM |
2516 | case ENUMERAL_TYPE: |
2517 | { | |
2518 | tree v; | |
2519 | ||
2520 | for (v = TYPE_VALUES (t); v != NULL_TREE; v = TREE_CHAIN (v)) | |
2521 | if (uses_template_parms (TREE_VALUE (v))) | |
2522 | return 1; | |
2523 | } | |
2524 | return 0; | |
2525 | ||
8d08fdba MS |
2526 | /* constants */ |
2527 | case INTEGER_CST: | |
2528 | case REAL_CST: | |
2529 | case STRING_CST: | |
2530 | return 0; | |
2531 | ||
2532 | case ERROR_MARK: | |
2533 | /* Non-error_mark_node ERROR_MARKs are bad things. */ | |
2534 | my_friendly_assert (t == error_mark_node, 274); | |
2535 | /* NOTREACHED */ | |
2536 | return 0; | |
2537 | ||
ec255269 | 2538 | case LOOKUP_EXPR: |
5566b478 | 2539 | case TYPENAME_TYPE: |
8d08fdba MS |
2540 | return 1; |
2541 | ||
5156628f MS |
2542 | case SCOPE_REF: |
2543 | return uses_template_parms (TREE_OPERAND (t, 0)); | |
2544 | ||
db5ae43f MS |
2545 | case CONSTRUCTOR: |
2546 | if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))) | |
2547 | return uses_template_parms (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t))); | |
5156628f | 2548 | return uses_template_parms (TREE_OPERAND (t, 1)); |
db5ae43f | 2549 | |
42976354 BK |
2550 | case MODOP_EXPR: |
2551 | case CAST_EXPR: | |
2552 | case REINTERPRET_CAST_EXPR: | |
2553 | case CONST_CAST_EXPR: | |
2554 | case STATIC_CAST_EXPR: | |
2555 | case DYNAMIC_CAST_EXPR: | |
42976354 BK |
2556 | case ARROW_EXPR: |
2557 | case DOTSTAR_EXPR: | |
2558 | case TYPEID_EXPR: | |
2559 | return 1; | |
2560 | ||
abff8e06 JM |
2561 | case SIZEOF_EXPR: |
2562 | case ALIGNOF_EXPR: | |
2563 | return uses_template_parms (TREE_OPERAND (t, 0)); | |
2564 | ||
8d08fdba MS |
2565 | default: |
2566 | switch (TREE_CODE_CLASS (TREE_CODE (t))) | |
2567 | { | |
2568 | case '1': | |
2569 | case '2': | |
ec255269 | 2570 | case 'e': |
8d08fdba MS |
2571 | case '<': |
2572 | { | |
2573 | int i; | |
2574 | for (i = tree_code_length[(int) TREE_CODE (t)]; --i >= 0;) | |
2575 | if (uses_template_parms (TREE_OPERAND (t, i))) | |
2576 | return 1; | |
2577 | return 0; | |
2578 | } | |
2579 | default: | |
2580 | break; | |
2581 | } | |
2582 | sorry ("testing %s for template parms", | |
2583 | tree_code_name [(int) TREE_CODE (t)]); | |
2584 | my_friendly_abort (82); | |
2585 | /* NOTREACHED */ | |
2586 | return 0; | |
2587 | } | |
2588 | } | |
2589 | ||
7215f9a0 MS |
2590 | static struct tinst_level *current_tinst_level = 0; |
2591 | static struct tinst_level *free_tinst_level = 0; | |
2592 | static int tinst_depth = 0; | |
e9f32eb5 | 2593 | extern int max_tinst_depth; |
5566b478 MS |
2594 | #ifdef GATHER_STATISTICS |
2595 | int depth_reached = 0; | |
2596 | #endif | |
8d08fdba | 2597 | |
bd6dd845 | 2598 | static int |
5566b478 MS |
2599 | push_tinst_level (d) |
2600 | tree d; | |
8d08fdba MS |
2601 | { |
2602 | struct tinst_level *new; | |
8d08fdba | 2603 | |
7215f9a0 MS |
2604 | if (tinst_depth >= max_tinst_depth) |
2605 | { | |
5566b478 MS |
2606 | struct tinst_level *p = current_tinst_level; |
2607 | int line = lineno; | |
2608 | char *file = input_filename; | |
2609 | ||
7215f9a0 MS |
2610 | error ("template instantiation depth exceeds maximum of %d", |
2611 | max_tinst_depth); | |
e9f32eb5 | 2612 | error (" (use -ftemplate-depth-NN to increase the maximum)"); |
5566b478 MS |
2613 | cp_error (" instantiating `%D'", d); |
2614 | ||
2615 | for (; p; p = p->next) | |
2616 | { | |
2617 | cp_error (" instantiated from `%D'", p->decl); | |
2618 | lineno = p->line; | |
2619 | input_filename = p->file; | |
2620 | } | |
2621 | error (" instantiated from here"); | |
2622 | ||
2623 | lineno = line; | |
2624 | input_filename = file; | |
2625 | ||
7215f9a0 MS |
2626 | return 0; |
2627 | } | |
2628 | ||
8d08fdba MS |
2629 | if (free_tinst_level) |
2630 | { | |
2631 | new = free_tinst_level; | |
2632 | free_tinst_level = new->next; | |
2633 | } | |
2634 | else | |
2635 | new = (struct tinst_level *) xmalloc (sizeof (struct tinst_level)); | |
2636 | ||
5566b478 MS |
2637 | new->decl = d; |
2638 | new->line = lineno; | |
2639 | new->file = input_filename; | |
8d08fdba MS |
2640 | new->next = current_tinst_level; |
2641 | current_tinst_level = new; | |
5566b478 | 2642 | |
7215f9a0 | 2643 | ++tinst_depth; |
5566b478 MS |
2644 | #ifdef GATHER_STATISTICS |
2645 | if (tinst_depth > depth_reached) | |
2646 | depth_reached = tinst_depth; | |
2647 | #endif | |
2648 | ||
7215f9a0 | 2649 | return 1; |
8d08fdba MS |
2650 | } |
2651 | ||
2652 | void | |
2653 | pop_tinst_level () | |
2654 | { | |
2655 | struct tinst_level *old = current_tinst_level; | |
2656 | ||
2657 | current_tinst_level = old->next; | |
2658 | old->next = free_tinst_level; | |
2659 | free_tinst_level = old; | |
7215f9a0 | 2660 | --tinst_depth; |
8d08fdba MS |
2661 | } |
2662 | ||
2663 | struct tinst_level * | |
2664 | tinst_for_decl () | |
2665 | { | |
2666 | struct tinst_level *p = current_tinst_level; | |
2667 | ||
2668 | if (p) | |
2669 | for (; p->next ; p = p->next ) | |
2670 | ; | |
2671 | return p; | |
2672 | } | |
2673 | ||
2674 | tree | |
5566b478 MS |
2675 | instantiate_class_template (type) |
2676 | tree type; | |
8d08fdba | 2677 | { |
fc378698 | 2678 | tree template, template_info, args, pattern, t, *field_chain; |
8d08fdba | 2679 | |
5566b478 | 2680 | if (type == error_mark_node) |
8d08fdba MS |
2681 | return error_mark_node; |
2682 | ||
5566b478 MS |
2683 | template_info = CLASSTYPE_TEMPLATE_INFO (type); |
2684 | ||
2685 | if (TYPE_BEING_DEFINED (type) || TYPE_SIZE (type)) | |
2686 | return type; | |
2687 | ||
2688 | template = TI_TEMPLATE (template_info); | |
2689 | my_friendly_assert (TREE_CODE (template) == TEMPLATE_DECL, 279); | |
2690 | args = TI_ARGS (template_info); | |
73aad9b9 JM |
2691 | |
2692 | t = most_specialized_class | |
2693 | (DECL_TEMPLATE_SPECIALIZATIONS (template), args); | |
2694 | ||
2695 | if (t == error_mark_node) | |
2696 | { | |
2697 | char *str = "candidates are:"; | |
2698 | cp_error ("ambiguous class template instantiation for `%#T'", type); | |
2699 | for (t = DECL_TEMPLATE_SPECIALIZATIONS (template); t; t = TREE_CHAIN (t)) | |
2700 | { | |
2701 | if (get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t), args)) | |
2702 | { | |
2703 | cp_error_at ("%s %+#T", str, TREE_TYPE (t)); | |
2704 | str = " "; | |
2705 | } | |
2706 | } | |
2707 | TYPE_BEING_DEFINED (type) = 1; | |
de22184b | 2708 | return error_mark_node; |
73aad9b9 JM |
2709 | } |
2710 | else if (t) | |
2711 | pattern = TREE_TYPE (t); | |
2712 | else | |
2713 | pattern = TREE_TYPE (template); | |
5566b478 MS |
2714 | |
2715 | if (TYPE_SIZE (pattern) == NULL_TREE) | |
ec255269 | 2716 | return type; |
5566b478 | 2717 | |
73aad9b9 JM |
2718 | if (t) |
2719 | args = get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t), args); | |
2720 | ||
5566b478 MS |
2721 | TYPE_BEING_DEFINED (type) = 1; |
2722 | ||
2723 | if (! push_tinst_level (type)) | |
2724 | return type; | |
8d08fdba | 2725 | |
5566b478 MS |
2726 | maybe_push_to_top_level (uses_template_parms (type)); |
2727 | pushclass (type, 0); | |
2728 | ||
2729 | if (flag_external_templates) | |
2730 | { | |
2731 | if (flag_alt_external_templates) | |
2732 | { | |
2733 | CLASSTYPE_INTERFACE_ONLY (type) = interface_only; | |
2734 | SET_CLASSTYPE_INTERFACE_UNKNOWN_X (type, interface_unknown); | |
2735 | CLASSTYPE_VTABLE_NEEDS_WRITING (type) | |
2736 | = ! CLASSTYPE_INTERFACE_ONLY (type) | |
2737 | && CLASSTYPE_INTERFACE_KNOWN (type); | |
2738 | } | |
2739 | else | |
2740 | { | |
2741 | CLASSTYPE_INTERFACE_ONLY (type) = CLASSTYPE_INTERFACE_ONLY (pattern); | |
2742 | SET_CLASSTYPE_INTERFACE_UNKNOWN_X | |
2743 | (type, CLASSTYPE_INTERFACE_UNKNOWN (pattern)); | |
2744 | CLASSTYPE_VTABLE_NEEDS_WRITING (type) | |
2745 | = ! CLASSTYPE_INTERFACE_ONLY (type) | |
2746 | && CLASSTYPE_INTERFACE_KNOWN (type); | |
2747 | } | |
2748 | } | |
2749 | else | |
8d08fdba | 2750 | { |
5566b478 MS |
2751 | SET_CLASSTYPE_INTERFACE_UNKNOWN (type); |
2752 | CLASSTYPE_VTABLE_NEEDS_WRITING (type) = 1; | |
8d08fdba MS |
2753 | } |
2754 | ||
f7da6097 MS |
2755 | TYPE_HAS_CONSTRUCTOR (type) = TYPE_HAS_CONSTRUCTOR (pattern); |
2756 | TYPE_HAS_DESTRUCTOR (type) = TYPE_HAS_DESTRUCTOR (pattern); | |
2757 | TYPE_HAS_ASSIGNMENT (type) = TYPE_HAS_ASSIGNMENT (pattern); | |
2758 | TYPE_OVERLOADS_CALL_EXPR (type) = TYPE_OVERLOADS_CALL_EXPR (pattern); | |
2759 | TYPE_OVERLOADS_ARRAY_REF (type) = TYPE_OVERLOADS_ARRAY_REF (pattern); | |
2760 | TYPE_OVERLOADS_ARROW (type) = TYPE_OVERLOADS_ARROW (pattern); | |
2761 | TYPE_GETS_NEW (type) = TYPE_GETS_NEW (pattern); | |
2762 | TYPE_GETS_DELETE (type) = TYPE_GETS_DELETE (pattern); | |
2763 | TYPE_VEC_DELETE_TAKES_SIZE (type) = TYPE_VEC_DELETE_TAKES_SIZE (pattern); | |
2764 | TYPE_HAS_ASSIGN_REF (type) = TYPE_HAS_ASSIGN_REF (pattern); | |
2765 | TYPE_HAS_CONST_ASSIGN_REF (type) = TYPE_HAS_CONST_ASSIGN_REF (pattern); | |
2766 | TYPE_HAS_ABSTRACT_ASSIGN_REF (type) = TYPE_HAS_ABSTRACT_ASSIGN_REF (pattern); | |
2767 | TYPE_HAS_INIT_REF (type) = TYPE_HAS_INIT_REF (pattern); | |
2768 | TYPE_HAS_CONST_INIT_REF (type) = TYPE_HAS_CONST_INIT_REF (pattern); | |
2769 | TYPE_GETS_INIT_AGGR (type) = TYPE_GETS_INIT_AGGR (pattern); | |
2770 | TYPE_HAS_DEFAULT_CONSTRUCTOR (type) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern); | |
2771 | TYPE_HAS_CONVERSION (type) = TYPE_HAS_CONVERSION (pattern); | |
2772 | TYPE_USES_COMPLEX_INHERITANCE (type) | |
2773 | = TYPE_USES_COMPLEX_INHERITANCE (pattern); | |
2774 | TYPE_USES_MULTIPLE_INHERITANCE (type) | |
2775 | = TYPE_USES_MULTIPLE_INHERITANCE (pattern); | |
2776 | TYPE_USES_VIRTUAL_BASECLASSES (type) | |
2777 | = TYPE_USES_VIRTUAL_BASECLASSES (pattern); | |
2778 | TYPE_PACKED (type) = TYPE_PACKED (pattern); | |
2779 | TYPE_ALIGN (type) = TYPE_ALIGN (pattern); | |
2780 | ||
5566b478 MS |
2781 | { |
2782 | tree binfo = TYPE_BINFO (type); | |
2783 | tree pbases = TYPE_BINFO_BASETYPES (pattern); | |
2784 | ||
2785 | if (pbases) | |
2786 | { | |
2787 | tree bases; | |
2788 | int i; | |
2789 | int len = TREE_VEC_LENGTH (pbases); | |
de22184b | 2790 | bases = make_tree_vec (len); |
5566b478 MS |
2791 | for (i = 0; i < len; ++i) |
2792 | { | |
2793 | tree elt; | |
2794 | ||
2795 | TREE_VEC_ELT (bases, i) = elt | |
98c1c668 | 2796 | = tsubst (TREE_VEC_ELT (pbases, i), args, |
5566b478 MS |
2797 | TREE_VEC_LENGTH (args), NULL_TREE); |
2798 | BINFO_INHERITANCE_CHAIN (elt) = binfo; | |
2799 | ||
6633d636 MS |
2800 | if (! IS_AGGR_TYPE (TREE_TYPE (elt))) |
2801 | cp_error | |
2802 | ("base type `%T' of `%T' fails to be a struct or class type", | |
2803 | TREE_TYPE (elt), type); | |
2804 | else if (! uses_template_parms (type) | |
2805 | && (TYPE_SIZE (complete_type (TREE_TYPE (elt))) | |
2806 | == NULL_TREE)) | |
5566b478 MS |
2807 | cp_error ("base class `%T' of `%T' has incomplete type", |
2808 | TREE_TYPE (elt), type); | |
2809 | } | |
de22184b MS |
2810 | /* Don't initialize this until the vector is filled out, or |
2811 | lookups will crash. */ | |
2812 | BINFO_BASETYPES (binfo) = bases; | |
5566b478 MS |
2813 | } |
2814 | } | |
2815 | ||
2816 | CLASSTYPE_LOCAL_TYPEDECLS (type) = CLASSTYPE_LOCAL_TYPEDECLS (pattern); | |
2817 | ||
2818 | field_chain = &TYPE_FIELDS (type); | |
5566b478 MS |
2819 | |
2820 | for (t = CLASSTYPE_TAGS (pattern); t; t = TREE_CHAIN (t)) | |
8d08fdba | 2821 | { |
5566b478 MS |
2822 | tree name = TREE_PURPOSE (t); |
2823 | tree tag = TREE_VALUE (t); | |
5566b478 | 2824 | |
fc378698 | 2825 | /* These will add themselves to CLASSTYPE_TAGS for the new type. */ |
5566b478 | 2826 | if (TREE_CODE (tag) == ENUMERAL_TYPE) |
8d08fdba | 2827 | { |
98c1c668 | 2828 | tree e, newtag = tsubst_enum (tag, args, |
b3d5a58b | 2829 | TREE_VEC_LENGTH (args), field_chain); |
5566b478 | 2830 | |
5566b478 | 2831 | while (*field_chain) |
37c46b43 MS |
2832 | { |
2833 | DECL_FIELD_CONTEXT (*field_chain) = type; | |
2834 | field_chain = &TREE_CHAIN (*field_chain); | |
2835 | } | |
8d08fdba | 2836 | } |
b87692e5 | 2837 | else |
98c1c668 | 2838 | tsubst (tag, args, |
b87692e5 | 2839 | TREE_VEC_LENGTH (args), NULL_TREE); |
8d08fdba MS |
2840 | } |
2841 | ||
5566b478 MS |
2842 | /* Don't replace enum constants here. */ |
2843 | for (t = TYPE_FIELDS (pattern); t; t = TREE_CHAIN (t)) | |
2844 | if (TREE_CODE (t) != CONST_DECL) | |
2845 | { | |
98c1c668 | 2846 | tree r = tsubst (t, args, |
5566b478 MS |
2847 | TREE_VEC_LENGTH (args), NULL_TREE); |
2848 | if (TREE_CODE (r) == VAR_DECL) | |
2849 | { | |
2850 | if (! uses_template_parms (r)) | |
2851 | pending_statics = perm_tree_cons (NULL_TREE, r, pending_statics); | |
e92cc029 | 2852 | /* Perhaps I should do more of grokfield here. */ |
5566b478 MS |
2853 | start_decl_1 (r); |
2854 | DECL_IN_AGGR_P (r) = 1; | |
2855 | DECL_EXTERNAL (r) = 1; | |
2856 | cp_finish_decl (r, DECL_INITIAL (r), NULL_TREE, 0, 0); | |
2857 | } | |
2858 | ||
2859 | *field_chain = r; | |
2860 | field_chain = &TREE_CHAIN (r); | |
2861 | } | |
8d08fdba | 2862 | |
5566b478 | 2863 | TYPE_METHODS (type) = tsubst_chain (TYPE_METHODS (pattern), args); |
f7da6097 MS |
2864 | for (t = TYPE_METHODS (type); t; t = TREE_CHAIN (t)) |
2865 | { | |
2866 | if (DECL_CONSTRUCTOR_P (t)) | |
2867 | grok_ctor_properties (type, t); | |
2868 | else if (IDENTIFIER_OPNAME_P (DECL_NAME (t))) | |
2869 | grok_op_properties (t, DECL_VIRTUAL_P (t), 0); | |
2870 | } | |
8d08fdba | 2871 | |
5566b478 | 2872 | DECL_FRIENDLIST (TYPE_MAIN_DECL (type)) |
fc378698 | 2873 | = tsubst (DECL_FRIENDLIST (TYPE_MAIN_DECL (pattern)), |
98c1c668 | 2874 | args, TREE_VEC_LENGTH (args), NULL_TREE); |
5566b478 MS |
2875 | |
2876 | { | |
beb53fb8 | 2877 | tree d = CLASSTYPE_FRIEND_CLASSES (type) |
98c1c668 | 2878 | = tsubst (CLASSTYPE_FRIEND_CLASSES (pattern), args, |
beb53fb8 | 2879 | TREE_VEC_LENGTH (args), NULL_TREE); |
fc378698 MS |
2880 | |
2881 | /* This does injection for friend classes. */ | |
2882 | for (; d; d = TREE_CHAIN (d)) | |
2883 | TREE_VALUE (d) = xref_tag_from_type (TREE_VALUE (d), NULL_TREE, 1); | |
2884 | ||
4f4da4e9 BK |
2885 | /* This does injection for friend functions. */ |
2886 | if (!processing_template_decl) | |
2887 | { | |
2888 | d = tsubst (DECL_TEMPLATE_INJECT (template), args, | |
056c014d | 2889 | TREE_VEC_LENGTH (args), NULL_TREE); |
5566b478 | 2890 | |
4f4da4e9 BK |
2891 | for (; d; d = TREE_CHAIN (d)) |
2892 | { | |
2893 | tree t = TREE_VALUE (d); | |
fc378698 | 2894 | |
4f4da4e9 BK |
2895 | if (TREE_CODE (t) == TYPE_DECL) |
2896 | /* Already injected. */; | |
2897 | else | |
2898 | pushdecl (t); | |
2899 | } | |
2900 | } | |
5566b478 MS |
2901 | } |
2902 | ||
5566b478 | 2903 | if (! uses_template_parms (type)) |
8d08fdba | 2904 | { |
5566b478 MS |
2905 | tree tmp; |
2906 | for (tmp = TYPE_FIELDS (type); tmp; tmp = TREE_CHAIN (tmp)) | |
ce122a86 | 2907 | if (TREE_CODE (tmp) == FIELD_DECL) |
c73964b2 MS |
2908 | { |
2909 | TREE_TYPE (tmp) = complete_type (TREE_TYPE (tmp)); | |
2910 | require_complete_type (tmp); | |
2911 | } | |
5566b478 | 2912 | |
6467930b | 2913 | type = finish_struct_1 (type, 0); |
e349ee73 | 2914 | CLASSTYPE_GOT_SEMICOLON (type) = 1; |
e92cc029 MS |
2915 | |
2916 | repo_template_used (type); | |
fe4e8851 JM |
2917 | if (at_eof && TYPE_BINFO_VTABLE (type) != NULL_TREE) |
2918 | finish_prevtable_vardecl (NULL, TYPE_BINFO_VTABLE (type)); | |
8d08fdba MS |
2919 | } |
2920 | else | |
2921 | { | |
5566b478 MS |
2922 | TYPE_SIZE (type) = integer_zero_node; |
2923 | CLASSTYPE_METHOD_VEC (type) | |
2924 | = finish_struct_methods (type, TYPE_METHODS (type), 1); | |
8d08fdba MS |
2925 | } |
2926 | ||
5566b478 MS |
2927 | TYPE_BEING_DEFINED (type) = 0; |
2928 | popclass (0); | |
2929 | ||
2930 | pop_from_top_level (); | |
2931 | pop_tinst_level (); | |
2932 | ||
2933 | return type; | |
8d08fdba MS |
2934 | } |
2935 | ||
2936 | static int | |
2937 | list_eq (t1, t2) | |
2938 | tree t1, t2; | |
2939 | { | |
2940 | if (t1 == NULL_TREE) | |
2941 | return t2 == NULL_TREE; | |
2942 | if (t2 == NULL_TREE) | |
2943 | return 0; | |
2944 | /* Don't care if one declares its arg const and the other doesn't -- the | |
2945 | main variant of the arg type is all that matters. */ | |
2946 | if (TYPE_MAIN_VARIANT (TREE_VALUE (t1)) | |
2947 | != TYPE_MAIN_VARIANT (TREE_VALUE (t2))) | |
2948 | return 0; | |
2949 | return list_eq (TREE_CHAIN (t1), TREE_CHAIN (t2)); | |
2950 | } | |
2951 | ||
5566b478 | 2952 | tree |
8d08fdba MS |
2953 | lookup_nested_type_by_name (ctype, name) |
2954 | tree ctype, name; | |
2955 | { | |
2956 | tree t; | |
2957 | ||
5566b478 MS |
2958 | complete_type (ctype); |
2959 | ||
db5ae43f MS |
2960 | for (t = CLASSTYPE_TAGS (ctype); t; t = TREE_CHAIN (t)) |
2961 | { | |
f017f649 JM |
2962 | if (name == TREE_PURPOSE (t) |
2963 | /* this catches typedef enum { foo } bar; */ | |
2964 | || name == TYPE_IDENTIFIER (TREE_VALUE (t))) | |
db5ae43f MS |
2965 | return TREE_VALUE (t); |
2966 | } | |
8d08fdba MS |
2967 | return NULL_TREE; |
2968 | } | |
2969 | ||
00d3396f JM |
2970 | /* If arg is a non-type template parameter that does not depend on template |
2971 | arguments, fold it like we weren't in the body of a template. */ | |
2972 | ||
2973 | static tree | |
2974 | maybe_fold_nontype_arg (arg) | |
2975 | tree arg; | |
2976 | { | |
2977 | if (TREE_CODE_CLASS (TREE_CODE (arg)) != 't' | |
2978 | && !uses_template_parms (arg)) | |
2979 | { | |
2980 | /* Sometimes, one of the args was an expression involving a | |
2981 | template constant parameter, like N - 1. Now that we've | |
2982 | tsubst'd, we might have something like 2 - 1. This will | |
2983 | confuse lookup_template_class, so we do constant folding | |
2984 | here. We have to unset processing_template_decl, to | |
2985 | fool build_expr_from_tree() into building an actual | |
2986 | tree. */ | |
2987 | ||
2988 | int saved_processing_template_decl = processing_template_decl; | |
2989 | processing_template_decl = 0; | |
2990 | arg = fold (build_expr_from_tree (arg)); | |
2991 | processing_template_decl = saved_processing_template_decl; | |
2992 | } | |
2993 | return arg; | |
2994 | } | |
2995 | ||
2996 | /* Take the tree structure T and replace template parameters used therein | |
2997 | with the argument vector ARGS. NARGS is the number of args; should | |
2998 | be removed. IN_DECL is an associated decl for diagnostics. | |
2999 | ||
3000 | tsubst is used for dealing with types, decls and the like; for | |
3001 | expressions, use tsubst_expr or tsubst_copy. */ | |
3002 | ||
75b0bbce | 3003 | tree |
8d08fdba | 3004 | tsubst (t, args, nargs, in_decl) |
98c1c668 | 3005 | tree t, args; |
8d08fdba MS |
3006 | int nargs; |
3007 | tree in_decl; | |
3008 | { | |
3009 | tree type; | |
3010 | ||
5566b478 MS |
3011 | if (t == NULL_TREE || t == error_mark_node |
3012 | || t == integer_type_node | |
3013 | || t == void_type_node | |
3014 | || t == char_type_node) | |
8d08fdba MS |
3015 | return t; |
3016 | ||
3017 | type = TREE_TYPE (t); | |
5566b478 MS |
3018 | if (type == unknown_type_node) |
3019 | my_friendly_abort (42); | |
824b9a4c MS |
3020 | if (type && TREE_CODE (t) != FUNCTION_DECL |
3021 | && TREE_CODE (t) != TYPENAME_TYPE) | |
b7484fbe MS |
3022 | type = tsubst (type, args, nargs, in_decl); |
3023 | ||
8d08fdba MS |
3024 | switch (TREE_CODE (t)) |
3025 | { | |
3026 | case RECORD_TYPE: | |
3027 | if (TYPE_PTRMEMFUNC_P (t)) | |
5566b478 MS |
3028 | { |
3029 | tree r = build_ptrmemfunc_type | |
3030 | (tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, nargs, in_decl)); | |
3031 | return cp_build_type_variant (r, TYPE_READONLY (t), | |
3032 | TYPE_VOLATILE (t)); | |
3033 | } | |
3034 | ||
8d08fdba | 3035 | /* else fall through */ |
5566b478 MS |
3036 | case UNION_TYPE: |
3037 | if (uses_template_parms (t)) | |
3038 | { | |
3039 | tree argvec = tsubst (CLASSTYPE_TI_ARGS (t), args, nargs, in_decl); | |
75650646 MM |
3040 | tree context; |
3041 | tree r; | |
3042 | ||
e1467ff2 MM |
3043 | context = |
3044 | TYPE_CONTEXT (t) | |
75650646 MM |
3045 | ? tsubst (TYPE_CONTEXT (t), args, nargs, in_decl) : NULL_TREE; |
3046 | ||
3047 | r = lookup_template_class (t, argvec, in_decl, context); | |
3048 | ||
5566b478 MS |
3049 | return cp_build_type_variant (r, TYPE_READONLY (t), |
3050 | TYPE_VOLATILE (t)); | |
3051 | } | |
8d08fdba | 3052 | |
5566b478 | 3053 | /* else fall through */ |
8d08fdba MS |
3054 | case ERROR_MARK: |
3055 | case IDENTIFIER_NODE: | |
3056 | case OP_IDENTIFIER: | |
3057 | case VOID_TYPE: | |
3058 | case REAL_TYPE: | |
37c46b43 | 3059 | case COMPLEX_TYPE: |
2986ae00 | 3060 | case BOOLEAN_TYPE: |
8d08fdba MS |
3061 | case INTEGER_CST: |
3062 | case REAL_CST: | |
3063 | case STRING_CST: | |
8d08fdba MS |
3064 | return t; |
3065 | ||
5566b478 MS |
3066 | case ENUMERAL_TYPE: |
3067 | { | |
3068 | tree ctx = tsubst (TYPE_CONTEXT (t), args, nargs, in_decl); | |
3069 | if (ctx == NULL_TREE) | |
3070 | return t; | |
b87692e5 MS |
3071 | else if (ctx == current_function_decl) |
3072 | return lookup_name (TYPE_IDENTIFIER (t), 1); | |
5566b478 MS |
3073 | else |
3074 | return lookup_nested_type_by_name (ctx, TYPE_IDENTIFIER (t)); | |
3075 | } | |
3076 | ||
8d08fdba MS |
3077 | case INTEGER_TYPE: |
3078 | if (t == integer_type_node) | |
3079 | return t; | |
3080 | ||
3081 | if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST | |
3082 | && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST) | |
3083 | return t; | |
5566b478 MS |
3084 | |
3085 | { | |
37c46b43 MS |
3086 | tree max = TREE_OPERAND (TYPE_MAX_VALUE (t), 0); |
3087 | max = tsubst_expr (max, args, nargs, in_decl); | |
5156628f | 3088 | if (processing_template_decl) |
5566b478 MS |
3089 | { |
3090 | tree itype = make_node (INTEGER_TYPE); | |
3091 | TYPE_MIN_VALUE (itype) = size_zero_node; | |
37c46b43 MS |
3092 | TYPE_MAX_VALUE (itype) = build_min (MINUS_EXPR, sizetype, max, |
3093 | integer_one_node); | |
5566b478 MS |
3094 | return itype; |
3095 | } | |
37c46b43 MS |
3096 | |
3097 | max = fold (build_binary_op (MINUS_EXPR, max, integer_one_node, 1)); | |
5566b478 MS |
3098 | return build_index_2_type (size_zero_node, max); |
3099 | } | |
8d08fdba MS |
3100 | |
3101 | case TEMPLATE_TYPE_PARM: | |
73b0fce8 | 3102 | case TEMPLATE_TEMPLATE_PARM: |
98c1c668 | 3103 | case TEMPLATE_CONST_PARM: |
db5ae43f | 3104 | { |
98c1c668 JM |
3105 | int idx; |
3106 | int level; | |
3107 | ||
73b0fce8 KL |
3108 | if (TREE_CODE (t) == TEMPLATE_TYPE_PARM |
3109 | || TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM) | |
98c1c668 JM |
3110 | { |
3111 | idx = TEMPLATE_TYPE_IDX (t); | |
3112 | level = TEMPLATE_TYPE_LEVEL (t); | |
3113 | } | |
3114 | else | |
3115 | { | |
3116 | idx = TEMPLATE_CONST_IDX (t); | |
3117 | level = TEMPLATE_CONST_LEVEL (t); | |
3118 | } | |
3119 | ||
3120 | if (TREE_VEC_LENGTH (args) > 0) | |
3121 | { | |
3122 | tree arg = NULL_TREE; | |
3123 | ||
3124 | if (TREE_CODE (TREE_VEC_ELT (args, 0)) == TREE_VEC) | |
3125 | { | |
3126 | if (TREE_VEC_LENGTH (args) >= level - 1) | |
3127 | arg = TREE_VEC_ELT | |
3128 | (TREE_VEC_ELT (args, level - 1), idx); | |
3129 | } | |
3130 | else if (level == 1) | |
3131 | arg = TREE_VEC_ELT (args, idx); | |
3132 | ||
3133 | if (arg != NULL_TREE) | |
3134 | { | |
3135 | if (TREE_CODE (t) == TEMPLATE_TYPE_PARM) | |
3136 | return cp_build_type_variant | |
3137 | (arg, TYPE_READONLY (arg) || TYPE_READONLY (t), | |
3138 | TYPE_VOLATILE (arg) || TYPE_VOLATILE (t)); | |
73b0fce8 KL |
3139 | else if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM) |
3140 | { | |
3141 | if (CLASSTYPE_TEMPLATE_INFO (t)) | |
3142 | { | |
3143 | /* We are processing a type constructed from | |
3144 | a template template parameter */ | |
b7a29012 JM |
3145 | tree argvec = tsubst (CLASSTYPE_TI_ARGS (t), |
3146 | args, nargs, in_decl); | |
73b0fce8 KL |
3147 | tree r; |
3148 | ||
3149 | /* We can get a TEMPLATE_TEMPLATE_PARM here when | |
3150 | we are resolving nested-types in the signature of | |
3151 | a member function templates. | |
3152 | Otherwise ARG is a TEMPLATE_DECL and is the real | |
3153 | template to be instantiated. */ | |
3154 | if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM) | |
3155 | arg = TYPE_NAME (arg); | |
3156 | ||
3157 | r = lookup_template_class (DECL_NAME (arg), | |
e1467ff2 MM |
3158 | argvec, in_decl, |
3159 | DECL_CONTEXT (arg)); | |
73b0fce8 KL |
3160 | return cp_build_type_variant (r, TYPE_READONLY (t), |
3161 | TYPE_VOLATILE (t)); | |
3162 | } | |
3163 | else | |
3164 | /* We are processing a template argument list. */ | |
3165 | return arg; | |
3166 | } | |
98c1c668 JM |
3167 | else |
3168 | return arg; | |
3169 | } | |
3170 | } | |
3171 | ||
3172 | /* If we get here, we must have been looking at a parm for a | |
3173 | more deeply nested template. */ | |
3174 | my_friendly_assert((TREE_CODE (t) == TEMPLATE_CONST_PARM | |
3175 | && TEMPLATE_CONST_LEVEL (t) > 1) | |
3176 | || (TREE_CODE (t) == TEMPLATE_TYPE_PARM | |
73b0fce8 KL |
3177 | && TEMPLATE_TYPE_LEVEL (t) > 1) |
3178 | || (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM | |
98c1c668 JM |
3179 | && TEMPLATE_TYPE_LEVEL (t) > 1), |
3180 | 0); | |
3181 | return t; | |
db5ae43f | 3182 | } |
8d08fdba | 3183 | |
98c1c668 JM |
3184 | case TEMPLATE_DECL: |
3185 | { | |
3186 | /* We can get here when processing a member template function | |
3187 | of a template class. */ | |
3188 | tree tmpl; | |
3189 | tree decl = DECL_TEMPLATE_RESULT (t); | |
3190 | tree new_decl; | |
3191 | tree parms; | |
386b8a85 | 3192 | tree spec; |
98c1c668 JM |
3193 | int i; |
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; | |
3788 | tree last; | |
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; |
85b71cf2 | 3937 | int saved_warn_unused; |
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 MM |
4654 | tree arg; |
4655 | tree parm; | |
386b8a85 JM |
4656 | int i; |
4657 | int r; | |
4658 | ||
4659 | bzero ((char *) targs, sizeof (tree) * ntparms); | |
4660 | ||
75650646 MM |
4661 | if (targs_in != NULL_TREE) |
4662 | { | |
4663 | tree arg_vec; | |
4664 | arg_vec = coerce_template_parms (tparms, targs_in, NULL_TREE, 0, | |
4665 | 0); | |
4666 | ||
4667 | if (arg_vec == error_mark_node) | |
4668 | return 1; | |
386b8a85 | 4669 | |
75650646 MM |
4670 | for (i = 0; |
4671 | i < TREE_VEC_LENGTH (arg_vec) | |
4672 | && TREE_VEC_ELT (arg_vec, i) != NULL_TREE; | |
4673 | ++i) | |
4674 | /* Insert the template argument. It is encoded as the operands | |
4675 | of NOP_EXPRs so that unify can tell that it is an explicit | |
4676 | arguments. */ | |
4677 | targs[i] = build1 (NOP_EXPR, NULL_TREE, TREE_VEC_ELT (arg_vec, i)); | |
4678 | } | |
4679 | ||
386b8a85 JM |
4680 | r = type_unification_real (tparms, targs, parms, args, nsubsts, 0, |
4681 | strict, allow_incomplete); | |
4682 | ||
75650646 MM |
4683 | for (i = 0, arg = targs_in; |
4684 | arg != NULL_TREE; | |
4685 | arg = TREE_CHAIN (arg), ++i) | |
386b8a85 JM |
4686 | if (TREE_CODE (targs[i]) == NOP_EXPR) |
4687 | targs[i] = TREE_OPERAND (targs[i], 0); | |
4688 | ||
4689 | return r; | |
4690 | } | |
4691 | ||
4692 | ||
4966381a | 4693 | static int |
386b8a85 JM |
4694 | type_unification_real (tparms, targs, parms, args, nsubsts, subr, |
4695 | strict, allow_incomplete) | |
8d08fdba | 4696 | tree tparms, *targs, parms, args; |
386b8a85 | 4697 | int *nsubsts, subr, strict, allow_incomplete; |
8d08fdba MS |
4698 | { |
4699 | tree parm, arg; | |
4700 | int i; | |
4701 | int ntparms = TREE_VEC_LENGTH (tparms); | |
4702 | ||
4703 | my_friendly_assert (TREE_CODE (tparms) == TREE_VEC, 289); | |
386b8a85 JM |
4704 | my_friendly_assert (parms == NULL_TREE |
4705 | || TREE_CODE (parms) == TREE_LIST, 290); | |
51c184be | 4706 | /* ARGS could be NULL (via a call from parse.y to |
8d08fdba MS |
4707 | build_x_function_call). */ |
4708 | if (args) | |
4709 | my_friendly_assert (TREE_CODE (args) == TREE_LIST, 291); | |
4710 | my_friendly_assert (ntparms > 0, 292); | |
4711 | ||
8d08fdba MS |
4712 | while (parms |
4713 | && parms != void_list_node | |
4714 | && args | |
4715 | && args != void_list_node) | |
4716 | { | |
4717 | parm = TREE_VALUE (parms); | |
4718 | parms = TREE_CHAIN (parms); | |
4719 | arg = TREE_VALUE (args); | |
4720 | args = TREE_CHAIN (args); | |
4721 | ||
4722 | if (arg == error_mark_node) | |
4723 | return 1; | |
4724 | if (arg == unknown_type_node) | |
4725 | return 1; | |
b7484fbe | 4726 | |
03e70705 JM |
4727 | /* Conversions will be performed on a function argument that |
4728 | corresponds with a function parameter that contains only | |
4729 | non-deducible template parameters and explicitly specified | |
4730 | template parameters. */ | |
4731 | if (! uses_template_parms (parm)) | |
b7484fbe | 4732 | { |
03e70705 JM |
4733 | tree type; |
4734 | ||
4735 | if (TREE_CODE_CLASS (TREE_CODE (arg)) != 't') | |
4736 | type = TREE_TYPE (arg); | |
4737 | else | |
4738 | { | |
4739 | type = arg; | |
4740 | arg = NULL_TREE; | |
4741 | } | |
4742 | ||
4743 | if (strict) | |
4744 | { | |
4745 | if (comptypes (parm, type, 1)) | |
4746 | continue; | |
4747 | } | |
03e70705 | 4748 | else |
343c89cd JM |
4749 | /* It might work; we shouldn't check now, because we might |
4750 | get into infinite recursion. Overload resolution will | |
4751 | handle it. */ | |
4752 | continue; | |
03e70705 | 4753 | |
b7484fbe MS |
4754 | return 1; |
4755 | } | |
4756 | ||
8d08fdba MS |
4757 | #if 0 |
4758 | if (TREE_CODE (arg) == VAR_DECL) | |
4759 | arg = TREE_TYPE (arg); | |
4760 | else if (TREE_CODE_CLASS (TREE_CODE (arg)) == 'e') | |
4761 | arg = TREE_TYPE (arg); | |
4762 | #else | |
4763 | if (TREE_CODE_CLASS (TREE_CODE (arg)) != 't') | |
4764 | { | |
4765 | my_friendly_assert (TREE_TYPE (arg) != NULL_TREE, 293); | |
28cbf42c MS |
4766 | if (TREE_CODE (arg) == TREE_LIST |
4767 | && TREE_TYPE (arg) == unknown_type_node | |
4768 | && TREE_CODE (TREE_VALUE (arg)) == TEMPLATE_DECL) | |
4769 | { | |
4770 | int nsubsts, ntparms; | |
4771 | tree *targs; | |
4772 | ||
4773 | /* Have to back unify here */ | |
4774 | arg = TREE_VALUE (arg); | |
4775 | nsubsts = 0; | |
98c1c668 | 4776 | ntparms = DECL_NTPARMS (arg); |
28cbf42c | 4777 | targs = (tree *) alloca (sizeof (tree) * ntparms); |
e66d884e | 4778 | parm = expr_tree_cons (NULL_TREE, parm, NULL_TREE); |
386b8a85 JM |
4779 | return |
4780 | type_unification (DECL_INNERMOST_TEMPLATE_PARMS (arg), | |
4781 | targs, | |
4782 | TYPE_ARG_TYPES (TREE_TYPE (arg)), | |
4783 | parm, NULL_TREE, &nsubsts, strict, | |
4784 | allow_incomplete); | |
28cbf42c | 4785 | } |
8d08fdba MS |
4786 | arg = TREE_TYPE (arg); |
4787 | } | |
4788 | #endif | |
5c0ad672 JM |
4789 | if (! flag_ansi && arg == TREE_TYPE (null_node)) |
4790 | { | |
4791 | warning ("using type void* for NULL"); | |
4792 | arg = ptr_type_node; | |
4793 | } | |
4794 | ||
7834ab39 | 4795 | if (! subr && TREE_CODE (arg) == REFERENCE_TYPE) |
db5ae43f MS |
4796 | arg = TREE_TYPE (arg); |
4797 | ||
7834ab39 | 4798 | if (! subr && TREE_CODE (parm) != REFERENCE_TYPE) |
4cabb798 JM |
4799 | { |
4800 | if (TREE_CODE (arg) == FUNCTION_TYPE | |
4801 | || TREE_CODE (arg) == METHOD_TYPE) | |
4802 | arg = build_pointer_type (arg); | |
4803 | else if (TREE_CODE (arg) == ARRAY_TYPE) | |
4804 | arg = build_pointer_type (TREE_TYPE (arg)); | |
4805 | else | |
4806 | arg = TYPE_MAIN_VARIANT (arg); | |
4807 | } | |
8d08fdba | 4808 | |
6467930b | 4809 | switch (unify (tparms, targs, ntparms, parm, arg, nsubsts, strict)) |
8d08fdba MS |
4810 | { |
4811 | case 0: | |
4812 | break; | |
4813 | case 1: | |
4814 | return 1; | |
4815 | } | |
4816 | } | |
4817 | /* Fail if we've reached the end of the parm list, and more args | |
4818 | are present, and the parm list isn't variadic. */ | |
4819 | if (args && args != void_list_node && parms == void_list_node) | |
4820 | return 1; | |
4821 | /* Fail if parms are left and they don't have default values. */ | |
4822 | if (parms | |
4823 | && parms != void_list_node | |
4824 | && TREE_PURPOSE (parms) == NULL_TREE) | |
4825 | return 1; | |
4826 | if (!subr) | |
4827 | for (i = 0; i < ntparms; i++) | |
4828 | if (!targs[i]) | |
4829 | { | |
386b8a85 JM |
4830 | if (!allow_incomplete) |
4831 | error ("incomplete type unification"); | |
8d08fdba MS |
4832 | return 2; |
4833 | } | |
4834 | return 0; | |
4835 | } | |
4836 | ||
4837 | /* Tail recursion is your friend. */ | |
e92cc029 | 4838 | |
8d08fdba | 4839 | static int |
6467930b | 4840 | unify (tparms, targs, ntparms, parm, arg, nsubsts, strict) |
8d08fdba | 4841 | tree tparms, *targs, parm, arg; |
6467930b | 4842 | int *nsubsts, ntparms, strict; |
8d08fdba MS |
4843 | { |
4844 | int idx; | |
4845 | ||
4846 | /* I don't think this will do the right thing with respect to types. | |
4847 | But the only case I've seen it in so far has been array bounds, where | |
4848 | signedness is the only information lost, and I think that will be | |
4849 | okay. */ | |
4850 | while (TREE_CODE (parm) == NOP_EXPR) | |
4851 | parm = TREE_OPERAND (parm, 0); | |
4852 | ||
4853 | if (arg == error_mark_node) | |
4854 | return 1; | |
4855 | if (arg == unknown_type_node) | |
4856 | return 1; | |
4857 | if (arg == parm) | |
4858 | return 0; | |
4859 | ||
8d08fdba MS |
4860 | switch (TREE_CODE (parm)) |
4861 | { | |
2ca340ae JM |
4862 | case TYPENAME_TYPE: |
4863 | /* In a type which contains a nested-name-specifier, template | |
4864 | argument values cannot be deduced for template parameters used | |
4865 | within the nested-name-specifier. */ | |
4866 | return 0; | |
4867 | ||
8d08fdba MS |
4868 | case TEMPLATE_TYPE_PARM: |
4869 | (*nsubsts)++; | |
8d08fdba | 4870 | idx = TEMPLATE_TYPE_IDX (parm); |
386b8a85 JM |
4871 | /* Check for mixed types and values. */ |
4872 | if (TREE_CODE (TREE_VALUE (TREE_VEC_ELT (tparms, idx))) != TYPE_DECL) | |
4873 | return 1; | |
4874 | ||
4875 | if (!strict && targs[idx] != NULL_TREE && | |
4876 | TREE_CODE (targs[idx]) == NOP_EXPR) | |
4877 | /* An explicit template argument. Don't even try to match | |
4878 | here; the overload resolution code will manage check to | |
4879 | see whether the call is legal. */ | |
4880 | return 0; | |
4881 | ||
6467930b MS |
4882 | if (strict && (TYPE_READONLY (arg) < TYPE_READONLY (parm) |
4883 | || TYPE_VOLATILE (arg) < TYPE_VOLATILE (parm))) | |
4884 | return 1; | |
db5ae43f | 4885 | #if 0 |
a292b002 MS |
4886 | /* Template type parameters cannot contain cv-quals; i.e. |
4887 | template <class T> void f (T& a, T& b) will not generate | |
4888 | void f (const int& a, const int& b). */ | |
4889 | if (TYPE_READONLY (arg) > TYPE_READONLY (parm) | |
4890 | || TYPE_VOLATILE (arg) > TYPE_VOLATILE (parm)) | |
4891 | return 1; | |
4892 | arg = TYPE_MAIN_VARIANT (arg); | |
db5ae43f MS |
4893 | #else |
4894 | { | |
4895 | int constp = TYPE_READONLY (arg) > TYPE_READONLY (parm); | |
4896 | int volatilep = TYPE_VOLATILE (arg) > TYPE_VOLATILE (parm); | |
4897 | arg = cp_build_type_variant (arg, constp, volatilep); | |
4898 | } | |
4899 | #endif | |
8d08fdba | 4900 | /* Simple cases: Value already set, does match or doesn't. */ |
386b8a85 JM |
4901 | if (targs[idx] == arg |
4902 | || (targs[idx] | |
4903 | && TREE_CODE (targs[idx]) == NOP_EXPR | |
4904 | && TREE_OPERAND (targs[idx], 0) == arg)) | |
8d08fdba MS |
4905 | return 0; |
4906 | else if (targs[idx]) | |
8d08fdba MS |
4907 | return 1; |
4908 | targs[idx] = arg; | |
4909 | return 0; | |
73b0fce8 KL |
4910 | |
4911 | case TEMPLATE_TEMPLATE_PARM: | |
4912 | (*nsubsts)++; | |
4913 | idx = TEMPLATE_TYPE_IDX (parm); | |
4914 | /* Check for mixed types and values. */ | |
4915 | if (TREE_CODE (TREE_VALUE (TREE_VEC_ELT (tparms, idx))) != TEMPLATE_DECL) | |
4916 | return 1; | |
4917 | ||
4918 | if (!strict && targs[idx] != NULL_TREE && | |
4919 | TREE_CODE (targs[idx]) == NOP_EXPR) | |
4920 | /* An explicit template argument. Don't even try to match | |
4921 | here; the overload resolution code will manage check to | |
4922 | see whether the call is legal. */ | |
4923 | return 0; | |
4924 | ||
4925 | if (CLASSTYPE_TEMPLATE_INFO (parm)) | |
4926 | { | |
4927 | /* We arrive here when PARM does not involve template | |
4928 | specialization. */ | |
4929 | ||
4930 | /* ARG must be constructed from a template class. */ | |
4931 | if (TREE_CODE (arg) != RECORD_TYPE || !CLASSTYPE_TEMPLATE_INFO (arg)) | |
4932 | return 1; | |
4933 | ||
4934 | { | |
4935 | tree parmtmpl = CLASSTYPE_TI_TEMPLATE (parm); | |
4936 | tree parmvec = CLASSTYPE_TI_ARGS (parm); | |
4937 | tree argvec = CLASSTYPE_TI_ARGS (arg); | |
4938 | tree argtmplvec | |
4939 | = DECL_INNERMOST_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (arg)); | |
4940 | int i, j; | |
4941 | ||
4942 | /* The parameter and argument roles have to be switched here | |
4943 | in order to handle default arguments properly. For example, | |
4944 | template<template <class> class TT> void f(TT<int>) | |
4945 | should be able to accept vector<int> which comes from | |
4946 | template <class T, class Allcator = allocator> | |
4947 | class vector. */ | |
4948 | ||
4949 | if (coerce_template_parms (argtmplvec, parmvec, parmtmpl, 1, 1) | |
4950 | == error_mark_node) | |
4951 | return 1; | |
4952 | ||
4953 | /* Deduce arguments T, i from TT<T> or TT<i>. */ | |
4954 | for (i = 0; i < TREE_VEC_LENGTH (parmvec); ++i) | |
4955 | { | |
4956 | tree t = TREE_VEC_ELT (parmvec, i); | |
4957 | if (TREE_CODE (t) != TEMPLATE_TYPE_PARM | |
4958 | && TREE_CODE (t) != TEMPLATE_TEMPLATE_PARM | |
4959 | && TREE_CODE (t) != TEMPLATE_CONST_PARM) | |
4960 | continue; | |
4961 | ||
4962 | /* This argument can be deduced. */ | |
4963 | ||
4964 | if (unify (tparms, targs, ntparms, t, | |
4965 | TREE_VEC_ELT (argvec, i), nsubsts, strict)) | |
4966 | return 1; | |
4967 | } | |
4968 | } | |
4969 | arg = CLASSTYPE_TI_TEMPLATE (arg); | |
4970 | } | |
4971 | ||
4972 | /* Simple cases: Value already set, does match or doesn't. */ | |
4973 | if (targs[idx] == arg | |
4974 | || (targs[idx] | |
4975 | && TREE_CODE (targs[idx]) == NOP_EXPR | |
4976 | && TREE_OPERAND (targs[idx], 0) == arg)) | |
4977 | return 0; | |
4978 | else if (targs[idx]) | |
4979 | return 1; | |
4980 | targs[idx] = arg; | |
4981 | return 0; | |
4982 | ||
8d08fdba MS |
4983 | case TEMPLATE_CONST_PARM: |
4984 | (*nsubsts)++; | |
4985 | idx = TEMPLATE_CONST_IDX (parm); | |
312e7d50 | 4986 | if (targs[idx]) |
8d08fdba | 4987 | { |
312e7d50 JM |
4988 | int i = cp_tree_equal (targs[idx], arg); |
4989 | if (i == 1) | |
4990 | return 0; | |
4991 | else if (i == 0) | |
4992 | return 1; | |
4993 | else | |
4994 | my_friendly_abort (42); | |
8d08fdba | 4995 | } |
8d08fdba MS |
4996 | |
4997 | targs[idx] = copy_to_permanent (arg); | |
4998 | return 0; | |
4999 | ||
5000 | case POINTER_TYPE: | |
4ac14744 MS |
5001 | if (TREE_CODE (arg) == RECORD_TYPE && TYPE_PTRMEMFUNC_FLAG (arg)) |
5002 | return unify (tparms, targs, ntparms, parm, | |
6467930b | 5003 | TYPE_PTRMEMFUNC_FN_TYPE (arg), nsubsts, strict); |
4ac14744 | 5004 | |
8d08fdba MS |
5005 | if (TREE_CODE (arg) != POINTER_TYPE) |
5006 | return 1; | |
5007 | return unify (tparms, targs, ntparms, TREE_TYPE (parm), TREE_TYPE (arg), | |
6467930b | 5008 | nsubsts, strict); |
8d08fdba MS |
5009 | |
5010 | case REFERENCE_TYPE: | |
28cbf42c MS |
5011 | if (TREE_CODE (arg) == REFERENCE_TYPE) |
5012 | arg = TREE_TYPE (arg); | |
6467930b MS |
5013 | return unify (tparms, targs, ntparms, TREE_TYPE (parm), arg, |
5014 | nsubsts, strict); | |
8d08fdba MS |
5015 | |
5016 | case ARRAY_TYPE: | |
5017 | if (TREE_CODE (arg) != ARRAY_TYPE) | |
5018 | return 1; | |
3042d5be MM |
5019 | if ((TYPE_DOMAIN (parm) == NULL_TREE) |
5020 | != (TYPE_DOMAIN (arg) == NULL_TREE)) | |
5021 | return 1; | |
5022 | if (TYPE_DOMAIN (parm) != NULL_TREE | |
5023 | && unify (tparms, targs, ntparms, TYPE_DOMAIN (parm), | |
5024 | TYPE_DOMAIN (arg), nsubsts, strict) != 0) | |
8d08fdba MS |
5025 | return 1; |
5026 | return unify (tparms, targs, ntparms, TREE_TYPE (parm), TREE_TYPE (arg), | |
6467930b | 5027 | nsubsts, strict); |
8d08fdba MS |
5028 | |
5029 | case REAL_TYPE: | |
37c46b43 | 5030 | case COMPLEX_TYPE: |
8d08fdba | 5031 | case INTEGER_TYPE: |
42976354 | 5032 | case BOOLEAN_TYPE: |
5ad5a526 | 5033 | case VOID_TYPE: |
f376e137 MS |
5034 | if (TREE_CODE (arg) != TREE_CODE (parm)) |
5035 | return 1; | |
5036 | ||
5037 | if (TREE_CODE (parm) == INTEGER_TYPE) | |
8d08fdba MS |
5038 | { |
5039 | if (TYPE_MIN_VALUE (parm) && TYPE_MIN_VALUE (arg) | |
6467930b MS |
5040 | && unify (tparms, targs, ntparms, TYPE_MIN_VALUE (parm), |
5041 | TYPE_MIN_VALUE (arg), nsubsts, strict)) | |
8d08fdba MS |
5042 | return 1; |
5043 | if (TYPE_MAX_VALUE (parm) && TYPE_MAX_VALUE (arg) | |
6467930b MS |
5044 | && unify (tparms, targs, ntparms, TYPE_MAX_VALUE (parm), |
5045 | TYPE_MAX_VALUE (arg), nsubsts, strict)) | |
8d08fdba MS |
5046 | return 1; |
5047 | } | |
ca79f85d JM |
5048 | else if (TREE_CODE (parm) == REAL_TYPE |
5049 | && TYPE_MAIN_VARIANT (arg) != TYPE_MAIN_VARIANT (parm)) | |
5050 | return 1; | |
5051 | ||
8d08fdba MS |
5052 | /* As far as unification is concerned, this wins. Later checks |
5053 | will invalidate it if necessary. */ | |
5054 | return 0; | |
5055 | ||
5056 | /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */ | |
bd6dd845 | 5057 | /* Type INTEGER_CST can come from ordinary constant template args. */ |
8d08fdba | 5058 | case INTEGER_CST: |
bd6dd845 MS |
5059 | while (TREE_CODE (arg) == NOP_EXPR) |
5060 | arg = TREE_OPERAND (arg, 0); | |
5061 | ||
8d08fdba MS |
5062 | if (TREE_CODE (arg) != INTEGER_CST) |
5063 | return 1; | |
5064 | return !tree_int_cst_equal (parm, arg); | |
5065 | ||
5066 | case MINUS_EXPR: | |
5067 | { | |
5068 | tree t1, t2; | |
5069 | t1 = TREE_OPERAND (parm, 0); | |
5070 | t2 = TREE_OPERAND (parm, 1); | |
8d08fdba MS |
5071 | return unify (tparms, targs, ntparms, t1, |
5072 | fold (build (PLUS_EXPR, integer_type_node, arg, t2)), | |
6467930b | 5073 | nsubsts, strict); |
8d08fdba MS |
5074 | } |
5075 | ||
5076 | case TREE_VEC: | |
5077 | { | |
5078 | int i; | |
5079 | if (TREE_CODE (arg) != TREE_VEC) | |
5080 | return 1; | |
5081 | if (TREE_VEC_LENGTH (parm) != TREE_VEC_LENGTH (arg)) | |
5082 | return 1; | |
5083 | for (i = TREE_VEC_LENGTH (parm) - 1; i >= 0; i--) | |
5084 | if (unify (tparms, targs, ntparms, | |
5085 | TREE_VEC_ELT (parm, i), TREE_VEC_ELT (arg, i), | |
6467930b | 5086 | nsubsts, strict)) |
8d08fdba MS |
5087 | return 1; |
5088 | return 0; | |
5089 | } | |
5090 | ||
8d08fdba | 5091 | case RECORD_TYPE: |
db5ae43f | 5092 | if (TYPE_PTRMEMFUNC_FLAG (parm)) |
8d08fdba | 5093 | return unify (tparms, targs, ntparms, TYPE_PTRMEMFUNC_FN_TYPE (parm), |
6467930b | 5094 | arg, nsubsts, strict); |
8d08fdba | 5095 | |
a4443a08 | 5096 | /* Allow trivial conversions. */ |
5566b478 | 5097 | if (TREE_CODE (arg) != RECORD_TYPE |
a4443a08 MS |
5098 | || TYPE_READONLY (parm) < TYPE_READONLY (arg) |
5099 | || TYPE_VOLATILE (parm) < TYPE_VOLATILE (arg)) | |
5100 | return 1; | |
5566b478 | 5101 | |
6467930b | 5102 | if (CLASSTYPE_TEMPLATE_INFO (parm) && uses_template_parms (parm)) |
5566b478 | 5103 | { |
6467930b | 5104 | tree t = NULL_TREE; |
c73964b2 | 5105 | if (flag_ansi_overloading && ! strict) |
6467930b | 5106 | t = get_template_base (CLASSTYPE_TI_TEMPLATE (parm), arg); |
c73964b2 MS |
5107 | else if |
5108 | (CLASSTYPE_TEMPLATE_INFO (arg) | |
5109 | && CLASSTYPE_TI_TEMPLATE (parm) == CLASSTYPE_TI_TEMPLATE (arg)) | |
6467930b MS |
5110 | t = arg; |
5111 | if (! t || t == error_mark_node) | |
5566b478 | 5112 | return 1; |
6467930b | 5113 | |
5566b478 | 5114 | return unify (tparms, targs, ntparms, CLASSTYPE_TI_ARGS (parm), |
6467930b | 5115 | CLASSTYPE_TI_ARGS (t), nsubsts, strict); |
5566b478 MS |
5116 | } |
5117 | else if (TYPE_MAIN_VARIANT (parm) != TYPE_MAIN_VARIANT (arg)) | |
5118 | return 1; | |
a4443a08 | 5119 | return 0; |
8d08fdba MS |
5120 | |
5121 | case METHOD_TYPE: | |
5122 | if (TREE_CODE (arg) != METHOD_TYPE) | |
5123 | return 1; | |
5124 | goto check_args; | |
5125 | ||
5126 | case FUNCTION_TYPE: | |
5127 | if (TREE_CODE (arg) != FUNCTION_TYPE) | |
5128 | return 1; | |
5129 | check_args: | |
28cbf42c | 5130 | if (unify (tparms, targs, ntparms, TREE_TYPE (parm), |
6467930b | 5131 | TREE_TYPE (arg), nsubsts, strict)) |
28cbf42c | 5132 | return 1; |
386b8a85 JM |
5133 | return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm), |
5134 | TYPE_ARG_TYPES (arg), nsubsts, 1, | |
5135 | strict, 0); | |
a4443a08 MS |
5136 | |
5137 | case OFFSET_TYPE: | |
5138 | if (TREE_CODE (arg) != OFFSET_TYPE) | |
5139 | return 1; | |
5140 | if (unify (tparms, targs, ntparms, TYPE_OFFSET_BASETYPE (parm), | |
6467930b | 5141 | TYPE_OFFSET_BASETYPE (arg), nsubsts, strict)) |
a4443a08 MS |
5142 | return 1; |
5143 | return unify (tparms, targs, ntparms, TREE_TYPE (parm), | |
6467930b | 5144 | TREE_TYPE (arg), nsubsts, strict); |
a4443a08 | 5145 | |
f62dbf03 JM |
5146 | case CONST_DECL: |
5147 | if (arg != decl_constant_value (parm)) | |
5148 | return 1; | |
5149 | return 0; | |
5150 | ||
8d08fdba MS |
5151 | default: |
5152 | sorry ("use of `%s' in template type unification", | |
5153 | tree_code_name [(int) TREE_CODE (parm)]); | |
5154 | return 1; | |
5155 | } | |
5156 | } | |
8d08fdba | 5157 | \f |
faae18ab | 5158 | void |
5566b478 | 5159 | mark_decl_instantiated (result, extern_p) |
faae18ab MS |
5160 | tree result; |
5161 | int extern_p; | |
5162 | { | |
5163 | if (DECL_TEMPLATE_INSTANTIATION (result)) | |
5164 | SET_DECL_EXPLICIT_INSTANTIATION (result); | |
75650646 MM |
5165 | |
5166 | if (TREE_CODE (result) != FUNCTION_DECL) | |
5167 | /* The TREE_PUBLIC flag for function declarations will have been | |
5168 | set correctly by tsubst. */ | |
5169 | TREE_PUBLIC (result) = 1; | |
faae18ab MS |
5170 | |
5171 | if (! extern_p) | |
5172 | { | |
5173 | DECL_INTERFACE_KNOWN (result) = 1; | |
5174 | DECL_NOT_REALLY_EXTERN (result) = 1; | |
a7d87521 JM |
5175 | |
5176 | /* For WIN32 we also want to put explicit instantiations in | |
5177 | linkonce sections. */ | |
75650646 | 5178 | if (supports_one_only () && ! SUPPORTS_WEAK && TREE_PUBLIC (result)) |
ab23f787 | 5179 | make_decl_one_only (result); |
faae18ab | 5180 | } |
f49422da MS |
5181 | else if (TREE_CODE (result) == FUNCTION_DECL) |
5182 | mark_inline_for_output (result); | |
faae18ab MS |
5183 | } |
5184 | ||
e1467ff2 MM |
5185 | /* Given two function templates PAT1 and PAT2, and explicit template |
5186 | arguments EXPLICIT_ARGS return: | |
6467930b MS |
5187 | |
5188 | 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order]. | |
5189 | -1 if PAT2 is more specialized than PAT1. | |
5190 | 0 if neither is more specialized. */ | |
5191 | ||
5192 | int | |
e1467ff2 MM |
5193 | more_specialized (pat1, pat2, explicit_args) |
5194 | tree pat1, pat2, explicit_args; | |
6467930b | 5195 | { |
98c1c668 | 5196 | tree targs; |
73aad9b9 | 5197 | int winner = 0; |
6467930b | 5198 | |
e1467ff2 | 5199 | targs = get_bindings (pat1, pat2, explicit_args); |
73aad9b9 JM |
5200 | if (targs) |
5201 | { | |
73aad9b9 JM |
5202 | --winner; |
5203 | } | |
6467930b | 5204 | |
e1467ff2 | 5205 | targs = get_bindings (pat2, pat1, explicit_args); |
73aad9b9 JM |
5206 | if (targs) |
5207 | { | |
73aad9b9 JM |
5208 | ++winner; |
5209 | } | |
6467930b | 5210 | |
73aad9b9 JM |
5211 | return winner; |
5212 | } | |
6467930b | 5213 | |
73aad9b9 | 5214 | /* Given two class template specialization list nodes PAT1 and PAT2, return: |
6467930b | 5215 | |
73aad9b9 JM |
5216 | 1 if PAT1 is more specialized than PAT2 as described in [temp.class.order]. |
5217 | -1 if PAT2 is more specialized than PAT1. | |
5218 | 0 if neither is more specialized. */ | |
5219 | ||
5220 | int | |
5221 | more_specialized_class (pat1, pat2) | |
5222 | tree pat1, pat2; | |
5223 | { | |
5224 | tree targs; | |
5225 | int winner = 0; | |
5226 | ||
5227 | targs = get_class_bindings | |
5228 | (TREE_VALUE (pat1), TREE_PURPOSE (pat1), TREE_PURPOSE (pat2)); | |
5229 | if (targs) | |
5230 | --winner; | |
5231 | ||
5232 | targs = get_class_bindings | |
5233 | (TREE_VALUE (pat2), TREE_PURPOSE (pat2), TREE_PURPOSE (pat1)); | |
5234 | if (targs) | |
6467930b MS |
5235 | ++winner; |
5236 | ||
5237 | return winner; | |
5238 | } | |
73aad9b9 JM |
5239 | |
5240 | /* Return the template arguments that will produce the function signature | |
e1467ff2 MM |
5241 | DECL from the function template FN, with the explicit template |
5242 | arguments EXPLICIT_ARGS. */ | |
73aad9b9 | 5243 | |
98c1c668 | 5244 | tree |
e1467ff2 MM |
5245 | get_bindings (fn, decl, explicit_args) |
5246 | tree fn, decl, explicit_args; | |
73aad9b9 | 5247 | { |
98c1c668 | 5248 | int ntparms = DECL_NTPARMS (fn); |
e66d884e | 5249 | tree targs = make_scratch_vec (ntparms); |
98c1c668 JM |
5250 | int i; |
5251 | ||
e1467ff2 | 5252 | i = fn_type_unification (fn, explicit_args, targs, |
98c1c668 JM |
5253 | TYPE_ARG_TYPES (TREE_TYPE (decl)), |
5254 | TREE_TYPE (TREE_TYPE (decl)), | |
5255 | 1); | |
5256 | ||
73aad9b9 | 5257 | if (i == 0) |
e1467ff2 MM |
5258 | { |
5259 | /* Check to see that the resulting return type is also OK. */ | |
5260 | tree t = tsubst (TREE_TYPE (TREE_TYPE (fn)), | |
5261 | targs, | |
5262 | DECL_NTPARMS (fn), | |
5263 | NULL_TREE); | |
5264 | ||
5265 | if (!comptypes(t, TREE_TYPE (TREE_TYPE (decl)), 1)) | |
5266 | return NULL_TREE; | |
5267 | ||
5268 | return targs; | |
5269 | } | |
5270 | ||
5271 | return NULL_TREE; | |
73aad9b9 JM |
5272 | } |
5273 | ||
bd6dd845 | 5274 | static tree |
73aad9b9 JM |
5275 | get_class_bindings (tparms, parms, args) |
5276 | tree tparms, parms, args; | |
5277 | { | |
5278 | int i, dummy, ntparms = TREE_VEC_LENGTH (tparms); | |
5279 | tree vec = make_temp_vec (ntparms); | |
5280 | ||
5281 | for (i = 0; i < TREE_VEC_LENGTH (parms); ++i) | |
5282 | { | |
5283 | switch (unify (tparms, &TREE_VEC_ELT (vec, 0), ntparms, | |
5284 | TREE_VEC_ELT (parms, i), TREE_VEC_ELT (args, i), | |
5285 | &dummy, 1)) | |
5286 | { | |
5287 | case 0: | |
5288 | break; | |
5289 | case 1: | |
5290 | return NULL_TREE; | |
5291 | } | |
5292 | } | |
5293 | ||
5294 | for (i = 0; i < ntparms; ++i) | |
5295 | if (! TREE_VEC_ELT (vec, i)) | |
5296 | return NULL_TREE; | |
5297 | ||
5298 | return vec; | |
5299 | } | |
5300 | ||
5301 | /* Return the most specialized of the list of templates in FNS that can | |
e1467ff2 MM |
5302 | produce an instantiation matching DECL, given the explicit template |
5303 | arguments EXPLICIT_ARGS. */ | |
73aad9b9 JM |
5304 | |
5305 | tree | |
e1467ff2 MM |
5306 | most_specialized (fns, decl, explicit_args) |
5307 | tree fns, decl, explicit_args; | |
73aad9b9 | 5308 | { |
98c1c668 | 5309 | tree fn, champ, args, *p; |
73aad9b9 JM |
5310 | int fate; |
5311 | ||
5312 | for (p = &fns; *p; ) | |
5313 | { | |
e1467ff2 | 5314 | args = get_bindings (TREE_VALUE (*p), decl, explicit_args); |
73aad9b9 JM |
5315 | if (args) |
5316 | { | |
73aad9b9 JM |
5317 | p = &TREE_CHAIN (*p); |
5318 | } | |
5319 | else | |
5320 | *p = TREE_CHAIN (*p); | |
5321 | } | |
5322 | ||
5323 | if (! fns) | |
5324 | return NULL_TREE; | |
5325 | ||
5326 | fn = fns; | |
5327 | champ = TREE_VALUE (fn); | |
5328 | fn = TREE_CHAIN (fn); | |
5329 | for (; fn; fn = TREE_CHAIN (fn)) | |
5330 | { | |
e1467ff2 | 5331 | fate = more_specialized (champ, TREE_VALUE (fn), explicit_args); |
73aad9b9 JM |
5332 | if (fate == 1) |
5333 | ; | |
5334 | else | |
5335 | { | |
5336 | if (fate == 0) | |
5337 | { | |
5338 | fn = TREE_CHAIN (fn); | |
5339 | if (! fn) | |
5340 | return error_mark_node; | |
5341 | } | |
5342 | champ = TREE_VALUE (fn); | |
5343 | } | |
5344 | } | |
5345 | ||
5346 | for (fn = fns; fn && TREE_VALUE (fn) != champ; fn = TREE_CHAIN (fn)) | |
5347 | { | |
e1467ff2 | 5348 | fate = more_specialized (champ, TREE_VALUE (fn), explicit_args); |
73aad9b9 JM |
5349 | if (fate != 1) |
5350 | return error_mark_node; | |
5351 | } | |
5352 | ||
5353 | return champ; | |
5354 | } | |
5355 | ||
5356 | /* Return the most specialized of the class template specializations in | |
5357 | SPECS that can produce an instantiation matching ARGS. */ | |
5358 | ||
5359 | tree | |
5360 | most_specialized_class (specs, mainargs) | |
5361 | tree specs, mainargs; | |
5362 | { | |
5363 | tree list = NULL_TREE, t, args, champ; | |
5364 | int fate; | |
5365 | ||
5366 | for (t = specs; t; t = TREE_CHAIN (t)) | |
5367 | { | |
5368 | args = get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t), mainargs); | |
5369 | if (args) | |
5370 | { | |
5371 | list = decl_tree_cons (TREE_PURPOSE (t), TREE_VALUE (t), list); | |
5372 | TREE_TYPE (list) = TREE_TYPE (t); | |
5373 | } | |
5374 | } | |
5375 | ||
5376 | if (! list) | |
5377 | return NULL_TREE; | |
5378 | ||
5379 | t = list; | |
5380 | champ = t; | |
5381 | t = TREE_CHAIN (t); | |
5382 | for (; t; t = TREE_CHAIN (t)) | |
5383 | { | |
5384 | fate = more_specialized_class (champ, t); | |
5385 | if (fate == 1) | |
5386 | ; | |
5387 | else | |
5388 | { | |
5389 | if (fate == 0) | |
5390 | { | |
5391 | t = TREE_CHAIN (t); | |
5392 | if (! t) | |
5393 | return error_mark_node; | |
5394 | } | |
5395 | champ = t; | |
5396 | } | |
5397 | } | |
5398 | ||
5399 | for (t = list; t && t != champ; t = TREE_CHAIN (t)) | |
5400 | { | |
85b71cf2 | 5401 | fate = more_specialized_class (champ, t); |
73aad9b9 JM |
5402 | if (fate != 1) |
5403 | return error_mark_node; | |
5404 | } | |
5405 | ||
5406 | return champ; | |
5407 | } | |
5408 | ||
8d08fdba | 5409 | /* called from the parser. */ |
e92cc029 | 5410 | |
8d08fdba | 5411 | void |
6633d636 | 5412 | do_decl_instantiation (declspecs, declarator, storage) |
f0e01782 | 5413 | tree declspecs, declarator, storage; |
8d08fdba | 5414 | { |
c11b6f21 | 5415 | tree decl = grokdeclarator (declarator, declspecs, NORMAL, 0, NULL_TREE); |
e8abc66f MS |
5416 | tree name; |
5417 | tree fn; | |
8d08fdba | 5418 | tree result = NULL_TREE; |
faae18ab | 5419 | int extern_p = 0; |
e8abc66f | 5420 | |
ec255269 MS |
5421 | if (! DECL_LANG_SPECIFIC (decl)) |
5422 | { | |
5423 | cp_error ("explicit instantiation of non-template `%#D'", decl); | |
5424 | return; | |
5425 | } | |
5426 | ||
e8abc66f | 5427 | /* If we've already seen this template instance, use it. */ |
6633d636 MS |
5428 | if (TREE_CODE (decl) == VAR_DECL) |
5429 | { | |
5430 | result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, 0); | |
5431 | if (result && TREE_CODE (result) != VAR_DECL) | |
5432 | result = NULL_TREE; | |
5433 | } | |
5434 | else if (TREE_CODE (decl) != FUNCTION_DECL) | |
5435 | { | |
5436 | cp_error ("explicit instantiation of `%#D'", decl); | |
5437 | return; | |
5438 | } | |
e1467ff2 MM |
5439 | else if (DECL_TEMPLATE_INSTANTIATION (decl)) |
5440 | result = decl; | |
98c1c668 | 5441 | |
7177d104 | 5442 | if (! result) |
faae18ab MS |
5443 | { |
5444 | cp_error ("no matching template for `%D' found", decl); | |
5445 | return; | |
5446 | } | |
7177d104 | 5447 | |
6633d636 MS |
5448 | if (! DECL_TEMPLATE_INFO (result)) |
5449 | { | |
5450 | cp_pedwarn ("explicit instantiation of non-template `%#D'", result); | |
5451 | return; | |
5452 | } | |
5453 | ||
a0a33927 MS |
5454 | if (flag_external_templates) |
5455 | return; | |
5456 | ||
f0e01782 | 5457 | if (storage == NULL_TREE) |
00595019 | 5458 | ; |
faae18ab MS |
5459 | else if (storage == ridpointers[(int) RID_EXTERN]) |
5460 | extern_p = 1; | |
f0e01782 MS |
5461 | else |
5462 | cp_error ("storage class `%D' applied to template instantiation", | |
5463 | storage); | |
5566b478 | 5464 | |
5566b478 | 5465 | mark_decl_instantiated (result, extern_p); |
44a8d0b3 | 5466 | repo_template_instantiated (result, extern_p); |
c91a56d2 MS |
5467 | if (! extern_p) |
5468 | instantiate_decl (result); | |
7177d104 MS |
5469 | } |
5470 | ||
faae18ab MS |
5471 | void |
5472 | mark_class_instantiated (t, extern_p) | |
5473 | tree t; | |
5474 | int extern_p; | |
5475 | { | |
5476 | SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t); | |
eb773359 JM |
5477 | SET_CLASSTYPE_INTERFACE_KNOWN (t); |
5478 | CLASSTYPE_INTERFACE_ONLY (t) = extern_p; | |
faae18ab MS |
5479 | CLASSTYPE_VTABLE_NEEDS_WRITING (t) = ! extern_p; |
5480 | TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p; | |
5481 | if (! extern_p) | |
5482 | { | |
5483 | CLASSTYPE_DEBUG_REQUESTED (t) = 1; | |
5484 | rest_of_type_compilation (t, 1); | |
5485 | } | |
5486 | } | |
e8abc66f | 5487 | |
7177d104 | 5488 | void |
ca79f85d JM |
5489 | do_type_instantiation (t, storage) |
5490 | tree t, storage; | |
7177d104 | 5491 | { |
e8abc66f MS |
5492 | int extern_p = 0; |
5493 | int nomem_p = 0; | |
5566b478 MS |
5494 | int static_p = 0; |
5495 | ||
ca79f85d JM |
5496 | if (TREE_CODE (t) == TYPE_DECL) |
5497 | t = TREE_TYPE (t); | |
5498 | ||
5499 | if (! IS_AGGR_TYPE (t) || ! CLASSTYPE_TEMPLATE_INFO (t)) | |
5500 | { | |
5501 | cp_error ("explicit instantiation of non-template type `%T'", t); | |
5502 | return; | |
5503 | } | |
5504 | ||
5566b478 | 5505 | complete_type (t); |
7177d104 | 5506 | |
a292b002 MS |
5507 | /* With -fexternal-templates, explicit instantiations are treated the same |
5508 | as implicit ones. */ | |
a0a33927 MS |
5509 | if (flag_external_templates) |
5510 | return; | |
5511 | ||
f0e01782 MS |
5512 | if (TYPE_SIZE (t) == NULL_TREE) |
5513 | { | |
5514 | cp_error ("explicit instantiation of `%#T' before definition of template", | |
5515 | t); | |
5516 | return; | |
5517 | } | |
5518 | ||
5519 | if (storage == NULL_TREE) | |
e8abc66f MS |
5520 | /* OK */; |
5521 | else if (storage == ridpointers[(int) RID_INLINE]) | |
5522 | nomem_p = 1; | |
f0e01782 MS |
5523 | else if (storage == ridpointers[(int) RID_EXTERN]) |
5524 | extern_p = 1; | |
5566b478 MS |
5525 | else if (storage == ridpointers[(int) RID_STATIC]) |
5526 | static_p = 1; | |
f0e01782 MS |
5527 | else |
5528 | { | |
5529 | cp_error ("storage class `%D' applied to template instantiation", | |
5530 | storage); | |
5531 | extern_p = 0; | |
5532 | } | |
5533 | ||
a292b002 | 5534 | /* We've already instantiated this. */ |
44a8d0b3 MS |
5535 | if (CLASSTYPE_EXPLICIT_INSTANTIATION (t) && ! CLASSTYPE_INTERFACE_ONLY (t) |
5536 | && extern_p) | |
5537 | return; | |
a292b002 | 5538 | |
f376e137 | 5539 | if (! CLASSTYPE_TEMPLATE_SPECIALIZATION (t)) |
44a8d0b3 MS |
5540 | { |
5541 | mark_class_instantiated (t, extern_p); | |
5542 | repo_template_instantiated (t, extern_p); | |
5543 | } | |
e8abc66f MS |
5544 | |
5545 | if (nomem_p) | |
5546 | return; | |
5547 | ||
7177d104 | 5548 | { |
db5ae43f | 5549 | tree tmp; |
5566b478 MS |
5550 | |
5551 | if (! static_p) | |
5552 | for (tmp = TYPE_METHODS (t); tmp; tmp = TREE_CHAIN (tmp)) | |
a7d87521 | 5553 | if (TREE_CODE (tmp) == FUNCTION_DECL |
1f109f0f | 5554 | && DECL_TEMPLATE_INSTANTIATION (tmp)) |
5566b478 MS |
5555 | { |
5556 | mark_decl_instantiated (tmp, extern_p); | |
5557 | repo_template_instantiated (tmp, extern_p); | |
5558 | if (! extern_p) | |
5559 | instantiate_decl (tmp); | |
5560 | } | |
5561 | ||
5562 | for (tmp = TYPE_FIELDS (t); tmp; tmp = TREE_CHAIN (tmp)) | |
5563 | if (TREE_CODE (tmp) == VAR_DECL && DECL_TEMPLATE_INSTANTIATION (tmp)) | |
863adfc0 | 5564 | { |
5566b478 | 5565 | mark_decl_instantiated (tmp, extern_p); |
863adfc0 | 5566 | repo_template_instantiated (tmp, extern_p); |
5566b478 MS |
5567 | if (! extern_p) |
5568 | instantiate_decl (tmp); | |
863adfc0 | 5569 | } |
7177d104 | 5570 | |
a292b002 | 5571 | for (tmp = CLASSTYPE_TAGS (t); tmp; tmp = TREE_CHAIN (tmp)) |
f376e137 MS |
5572 | if (IS_AGGR_TYPE (TREE_VALUE (tmp))) |
5573 | do_type_instantiation (TYPE_MAIN_DECL (TREE_VALUE (tmp)), storage); | |
a292b002 | 5574 | } |
8d08fdba | 5575 | } |
a28e3c7f MS |
5576 | |
5577 | tree | |
5566b478 MS |
5578 | instantiate_decl (d) |
5579 | tree d; | |
a28e3c7f | 5580 | { |
5566b478 MS |
5581 | tree ti = DECL_TEMPLATE_INFO (d); |
5582 | tree tmpl = TI_TEMPLATE (ti); | |
5583 | tree args = TI_ARGS (ti); | |
5584 | tree td; | |
fee23f54 | 5585 | tree decl_pattern, code_pattern; |
5566b478 MS |
5586 | tree save_ti; |
5587 | int nested = in_function_p (); | |
5588 | int d_defined; | |
5589 | int pattern_defined; | |
5156628f MS |
5590 | int line = lineno; |
5591 | char *file = input_filename; | |
5566b478 | 5592 | |
fee23f54 JM |
5593 | for (td = tmpl; DECL_TEMPLATE_INSTANTIATION (td); ) |
5594 | td = DECL_TI_TEMPLATE (td); | |
27bb8339 | 5595 | |
fee23f54 JM |
5596 | /* In the case of a member template, decl_pattern is the partially |
5597 | instantiated declaration (in the instantiated class), and code_pattern | |
5598 | is the original template definition. */ | |
5599 | decl_pattern = DECL_TEMPLATE_RESULT (tmpl); | |
5600 | code_pattern = DECL_TEMPLATE_RESULT (td); | |
27bb8339 | 5601 | |
5566b478 MS |
5602 | if (TREE_CODE (d) == FUNCTION_DECL) |
5603 | { | |
5604 | d_defined = (DECL_INITIAL (d) != NULL_TREE); | |
fee23f54 | 5605 | pattern_defined = (DECL_INITIAL (code_pattern) != NULL_TREE); |
5566b478 MS |
5606 | } |
5607 | else | |
5608 | { | |
5609 | d_defined = ! DECL_IN_AGGR_P (d); | |
fee23f54 | 5610 | pattern_defined = ! DECL_IN_AGGR_P (code_pattern); |
5566b478 MS |
5611 | } |
5612 | ||
5613 | if (d_defined) | |
5614 | return d; | |
de22184b | 5615 | |
7ac63bca JM |
5616 | if (TREE_CODE (d) == FUNCTION_DECL) |
5617 | { | |
75650646 MM |
5618 | tree spec = retrieve_specialization (tmpl, args); |
5619 | ||
5620 | if (spec != NULL_TREE | |
5621 | && DECL_TEMPLATE_SPECIALIZATION (spec)) | |
5622 | return spec; | |
7ac63bca JM |
5623 | } |
5624 | ||
de22184b MS |
5625 | /* This needs to happen before any tsubsting. */ |
5626 | if (! push_tinst_level (d)) | |
5627 | return d; | |
5628 | ||
5629 | push_to_top_level (); | |
5630 | lineno = DECL_SOURCE_LINE (d); | |
5631 | input_filename = DECL_SOURCE_FILE (d); | |
5632 | ||
5633 | /* We need to set up DECL_INITIAL regardless of pattern_defined if the | |
5634 | variable is a static const initialized in the class body. */ | |
5635 | if (TREE_CODE (d) == VAR_DECL | |
fee23f54 | 5636 | && ! DECL_INITIAL (d) && DECL_INITIAL (code_pattern)) |
de22184b MS |
5637 | { |
5638 | pushclass (DECL_CONTEXT (d), 2); | |
fee23f54 JM |
5639 | DECL_INITIAL (d) = tsubst_expr (DECL_INITIAL (code_pattern), args, |
5640 | TREE_VEC_LENGTH (args), tmpl); | |
de22184b MS |
5641 | popclass (1); |
5642 | } | |
5643 | ||
de22184b | 5644 | if (pattern_defined) |
5566b478 MS |
5645 | { |
5646 | repo_template_used (d); | |
5647 | ||
5648 | if (flag_external_templates && ! DECL_INTERFACE_KNOWN (d)) | |
5649 | { | |
5650 | if (flag_alt_external_templates) | |
5651 | { | |
5652 | if (interface_unknown) | |
5653 | warn_if_unknown_interface (d); | |
5654 | } | |
fee23f54 | 5655 | else if (DECL_INTERFACE_KNOWN (code_pattern)) |
5566b478 MS |
5656 | { |
5657 | DECL_INTERFACE_KNOWN (d) = 1; | |
fee23f54 | 5658 | DECL_NOT_REALLY_EXTERN (d) = ! DECL_EXTERNAL (code_pattern); |
5566b478 MS |
5659 | } |
5660 | else | |
fee23f54 | 5661 | warn_if_unknown_interface (code_pattern); |
5566b478 MS |
5662 | } |
5663 | ||
e92cc029 | 5664 | if (at_eof) |
5566b478 MS |
5665 | import_export_decl (d); |
5666 | } | |
5667 | ||
c4ae3f91 JM |
5668 | /* Reject all external templates except inline functions. */ |
5669 | if (DECL_INTERFACE_KNOWN (d) | |
5670 | && ! DECL_NOT_REALLY_EXTERN (d) | |
5671 | && ! (TREE_CODE (d) == FUNCTION_DECL && DECL_INLINE (d))) | |
5672 | goto out; | |
5673 | ||
5674 | /* Defer all templates except inline functions used in another function. */ | |
5566b478 | 5675 | if (! pattern_defined |
c4ae3f91 JM |
5676 | || (! (TREE_CODE (d) == FUNCTION_DECL && DECL_INLINE (d) && nested) |
5677 | && ! at_eof)) | |
5566b478 MS |
5678 | { |
5679 | add_pending_template (d); | |
de22184b | 5680 | goto out; |
5566b478 MS |
5681 | } |
5682 | ||
5156628f MS |
5683 | lineno = DECL_SOURCE_LINE (d); |
5684 | input_filename = DECL_SOURCE_FILE (d); | |
5685 | ||
5566b478 | 5686 | /* Trick tsubst into giving us a new decl in case the template changed. */ |
fee23f54 JM |
5687 | save_ti = DECL_TEMPLATE_INFO (decl_pattern); |
5688 | DECL_TEMPLATE_INFO (decl_pattern) = NULL_TREE; | |
5689 | td = tsubst (decl_pattern, args, TREE_VEC_LENGTH (args), tmpl); | |
7b4f18a3 | 5690 | SET_DECL_IMPLICIT_INSTANTIATION (td); |
fee23f54 | 5691 | DECL_TEMPLATE_INFO (decl_pattern) = save_ti; |
5566b478 | 5692 | |
d11ad92e MS |
5693 | /* And set up DECL_INITIAL, since tsubst doesn't. */ |
5694 | if (TREE_CODE (td) == VAR_DECL) | |
5156628f MS |
5695 | { |
5696 | pushclass (DECL_CONTEXT (d), 2); | |
fee23f54 JM |
5697 | DECL_INITIAL (td) = tsubst_expr (DECL_INITIAL (code_pattern), args, |
5698 | TREE_VEC_LENGTH (args), tmpl); | |
5156628f MS |
5699 | popclass (1); |
5700 | } | |
d11ad92e | 5701 | |
5566b478 | 5702 | if (TREE_CODE (d) == FUNCTION_DECL) |
386b8a85 JM |
5703 | { |
5704 | /* Convince duplicate_decls to use the DECL_ARGUMENTS from the | |
5705 | new decl. */ | |
5706 | DECL_INITIAL (td) = error_mark_node; | |
5707 | ||
5708 | if (DECL_TEMPLATE_SPECIALIZATION (td) && !DECL_TEMPLATE_INFO (td)) | |
5709 | /* Set up the information about what is being specialized. */ | |
5710 | DECL_TEMPLATE_INFO (td) = DECL_TEMPLATE_INFO (d); | |
5711 | } | |
5566b478 MS |
5712 | duplicate_decls (td, d); |
5713 | if (TREE_CODE (d) == FUNCTION_DECL) | |
5714 | DECL_INITIAL (td) = 0; | |
5715 | ||
5716 | if (TREE_CODE (d) == VAR_DECL) | |
5717 | { | |
5718 | DECL_IN_AGGR_P (d) = 0; | |
5719 | if (DECL_INTERFACE_KNOWN (d)) | |
5720 | DECL_EXTERNAL (d) = ! DECL_NOT_REALLY_EXTERN (d); | |
5721 | else | |
5722 | { | |
5723 | DECL_EXTERNAL (d) = 1; | |
5724 | DECL_NOT_REALLY_EXTERN (d) = 1; | |
5725 | } | |
5726 | cp_finish_decl (d, DECL_INITIAL (d), NULL_TREE, 0, 0); | |
5727 | } | |
5728 | else if (TREE_CODE (d) == FUNCTION_DECL) | |
5729 | { | |
fee23f54 | 5730 | tree t = DECL_SAVED_TREE (code_pattern); |
5566b478 | 5731 | |
c11b6f21 | 5732 | start_function (NULL_TREE, d, NULL_TREE, 1); |
5566b478 MS |
5733 | store_parm_decls (); |
5734 | ||
e76a2646 MS |
5735 | if (t && TREE_CODE (t) == RETURN_INIT) |
5736 | { | |
5737 | store_return_init | |
5738 | (TREE_OPERAND (t, 0), | |
98c1c668 | 5739 | tsubst_expr (TREE_OPERAND (t, 1), args, |
e76a2646 MS |
5740 | TREE_VEC_LENGTH (args), tmpl)); |
5741 | t = TREE_CHAIN (t); | |
5742 | } | |
5743 | ||
5566b478 MS |
5744 | if (t && TREE_CODE (t) == CTOR_INITIALIZER) |
5745 | { | |
5746 | current_member_init_list | |
5747 | = tsubst_expr_values (TREE_OPERAND (t, 0), args); | |
5748 | current_base_init_list | |
5749 | = tsubst_expr_values (TREE_OPERAND (t, 1), args); | |
5750 | t = TREE_CHAIN (t); | |
5751 | } | |
5752 | ||
5753 | setup_vtbl_ptr (); | |
5754 | /* Always keep the BLOCK node associated with the outermost | |
956d6950 | 5755 | pair of curly braces of a function. These are needed |
5566b478 MS |
5756 | for correct operation of dwarfout.c. */ |
5757 | keep_next_level (); | |
5758 | ||
5759 | my_friendly_assert (TREE_CODE (t) == COMPOUND_STMT, 42); | |
98c1c668 | 5760 | tsubst_expr (t, args, TREE_VEC_LENGTH (args), tmpl); |
a28e3c7f | 5761 | |
5566b478 | 5762 | finish_function (lineno, 0, nested); |
5566b478 MS |
5763 | } |
5764 | ||
de22184b | 5765 | out: |
5156628f MS |
5766 | lineno = line; |
5767 | input_filename = file; | |
5768 | ||
5566b478 | 5769 | pop_from_top_level (); |
5566b478 | 5770 | pop_tinst_level (); |
a28e3c7f | 5771 | |
a28e3c7f MS |
5772 | return d; |
5773 | } | |
5566b478 MS |
5774 | |
5775 | tree | |
5776 | tsubst_chain (t, argvec) | |
5777 | tree t, argvec; | |
5778 | { | |
5779 | if (t) | |
5780 | { | |
98c1c668 | 5781 | tree first = tsubst (t, argvec, |
5566b478 MS |
5782 | TREE_VEC_LENGTH (argvec), NULL_TREE); |
5783 | tree last = first; | |
5784 | ||
5785 | for (t = TREE_CHAIN (t); t; t = TREE_CHAIN (t)) | |
5786 | { | |
98c1c668 | 5787 | tree x = tsubst (t, argvec, TREE_VEC_LENGTH (argvec), NULL_TREE); |
5566b478 MS |
5788 | TREE_CHAIN (last) = x; |
5789 | last = x; | |
5790 | } | |
5791 | ||
5792 | return first; | |
5793 | } | |
5794 | return NULL_TREE; | |
5795 | } | |
5796 | ||
824b9a4c | 5797 | static tree |
5566b478 MS |
5798 | tsubst_expr_values (t, argvec) |
5799 | tree t, argvec; | |
5800 | { | |
5801 | tree first = NULL_TREE; | |
5802 | tree *p = &first; | |
5803 | ||
5804 | for (; t; t = TREE_CHAIN (t)) | |
5805 | { | |
98c1c668 | 5806 | tree pur = tsubst_copy (TREE_PURPOSE (t), argvec, |
5566b478 | 5807 | TREE_VEC_LENGTH (argvec), NULL_TREE); |
98c1c668 | 5808 | tree val = tsubst_expr (TREE_VALUE (t), argvec, |
5566b478 MS |
5809 | TREE_VEC_LENGTH (argvec), NULL_TREE); |
5810 | *p = build_tree_list (pur, val); | |
5811 | p = &TREE_CHAIN (*p); | |
5812 | } | |
5813 | return first; | |
5814 | } | |
5815 | ||
5816 | tree last_tree; | |
5817 | ||
5818 | void | |
5819 | add_tree (t) | |
5820 | tree t; | |
5821 | { | |
5822 | last_tree = TREE_CHAIN (last_tree) = t; | |
5823 | } | |
73aad9b9 | 5824 | |
75650646 MM |
5825 | |
5826 | void | |
5827 | begin_tree () | |
5828 | { | |
5829 | saved_trees = tree_cons (NULL_TREE, last_tree, saved_trees); | |
5830 | last_tree = NULL_TREE; | |
5831 | } | |
5832 | ||
5833 | ||
5834 | void | |
5835 | end_tree () | |
5836 | { | |
5837 | my_friendly_assert (saved_trees != NULL_TREE, 0); | |
5838 | ||
5839 | last_tree = TREE_VALUE (saved_trees); | |
5840 | saved_trees = TREE_CHAIN (saved_trees); | |
5841 | } | |
5842 | ||
73aad9b9 JM |
5843 | /* D is an undefined function declaration in the presence of templates with |
5844 | the same name, listed in FNS. If one of them can produce D as an | |
5845 | instantiation, remember this so we can instantiate it at EOF if D has | |
5846 | not been defined by that time. */ | |
5847 | ||
5848 | void | |
5849 | add_maybe_template (d, fns) | |
5850 | tree d, fns; | |
5851 | { | |
5852 | tree t; | |
5853 | ||
5854 | if (DECL_MAYBE_TEMPLATE (d)) | |
5855 | return; | |
5856 | ||
e1467ff2 | 5857 | t = most_specialized (fns, d, NULL_TREE); |
73aad9b9 JM |
5858 | if (! t) |
5859 | return; | |
5860 | if (t == error_mark_node) | |
5861 | { | |
5862 | cp_error ("ambiguous template instantiation for `%D'", d); | |
5863 | return; | |
5864 | } | |
5865 | ||
5866 | *maybe_template_tail = perm_tree_cons (t, d, NULL_TREE); | |
5867 | maybe_template_tail = &TREE_CHAIN (*maybe_template_tail); | |
5868 | DECL_MAYBE_TEMPLATE (d) = 1; | |
5869 | } | |
b87692e5 MS |
5870 | |
5871 | /* Instantiate an enumerated type. Used by instantiate_class_template and | |
5872 | tsubst_expr. */ | |
5873 | ||
5874 | static tree | |
b3d5a58b | 5875 | tsubst_enum (tag, args, nargs, field_chain) |
98c1c668 | 5876 | tree tag, args; |
b87692e5 | 5877 | int nargs; |
b3d5a58b | 5878 | tree * field_chain; |
b87692e5 | 5879 | { |
b3d5a58b JG |
5880 | extern tree current_local_enum; |
5881 | tree prev_local_enum = current_local_enum; | |
5882 | ||
b87692e5 MS |
5883 | tree newtag = start_enum (TYPE_IDENTIFIER (tag)); |
5884 | tree e, values = NULL_TREE; | |
5885 | ||
5886 | for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e)) | |
5887 | { | |
5888 | tree elt = build_enumerator (TREE_PURPOSE (e), | |
5889 | tsubst_expr (TREE_VALUE (e), args, | |
5890 | nargs, NULL_TREE)); | |
5891 | TREE_CHAIN (elt) = values; | |
5892 | values = elt; | |
5893 | } | |
5894 | ||
5895 | finish_enum (newtag, values); | |
5896 | ||
b3d5a58b | 5897 | if (NULL != field_chain) |
86052cc3 | 5898 | *field_chain = grok_enum_decls (NULL_TREE); |
b3d5a58b JG |
5899 | |
5900 | current_local_enum = prev_local_enum; | |
5901 | ||
b87692e5 MS |
5902 | return newtag; |
5903 | } |