]>
Commit | Line | Data |
---|---|---|
8d08fdba | 1 | /* Handle parameterized types (templates) for GNU C++. |
357a4089 | 2 | Copyright (C) 1992, 93, 94, 95, 1996 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 | |
386b8a85 JM |
63 | static int processing_specialization; |
64 | static int template_header_count; | |
65 | ||
8d08fdba MS |
66 | #define obstack_chunk_alloc xmalloc |
67 | #define obstack_chunk_free free | |
68 | ||
49c249e1 JM |
69 | static int unify PROTO((tree, tree *, int, tree, tree, int *, int)); |
70 | static void add_pending_template PROTO((tree)); | |
71 | static int push_tinst_level PROTO((tree)); | |
72 | static tree classtype_mangled_name PROTO((tree)); | |
73 | static char *mangle_class_name_for_template PROTO((char *, tree, tree)); | |
74 | static tree tsubst_expr_values PROTO((tree, tree)); | |
bd6dd845 | 75 | static int comp_template_args PROTO((tree, tree)); |
49c249e1 | 76 | static int list_eq PROTO((tree, tree)); |
bd6dd845 | 77 | static tree get_class_bindings PROTO((tree, tree, tree)); |
49c249e1 | 78 | static tree coerce_template_parms PROTO((tree, tree, tree)); |
b3d5a58b | 79 | static tree tsubst_enum PROTO((tree, tree, int, tree *)); |
98c1c668 | 80 | static tree add_to_template_args PROTO((tree, tree)); |
386b8a85 JM |
81 | static int type_unification_real PROTO((tree, tree *, tree, tree, int*, |
82 | int, int, int)); | |
83 | static int processing_explicit_specialization PROTO((int)); | |
84 | static void note_template_header PROTO((int)); | |
98c1c668 JM |
85 | |
86 | /* Restore the template parameter context. */ | |
87 | ||
88 | void | |
786b5245 MM |
89 | begin_member_template_processing (decl) |
90 | tree decl; | |
98c1c668 | 91 | { |
786b5245 | 92 | tree parms; |
98c1c668 JM |
93 | int i; |
94 | ||
786b5245 MM |
95 | parms = DECL_INNERMOST_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)); |
96 | ||
98c1c668 JM |
97 | ++processing_template_decl; |
98 | current_template_parms | |
99 | = tree_cons (build_int_2 (0, processing_template_decl), | |
100 | parms, current_template_parms); | |
786b5245 | 101 | pushlevel (0); |
98c1c668 JM |
102 | for (i = 0; i < TREE_VEC_LENGTH (parms); ++i) |
103 | { | |
786b5245 MM |
104 | tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i)); |
105 | my_friendly_assert (TREE_CODE_CLASS (TREE_CODE (parm)) == 'd', 0); | |
106 | ||
98c1c668 JM |
107 | switch (TREE_CODE (parm)) |
108 | { | |
786b5245 | 109 | case TYPE_DECL: |
98c1c668 JM |
110 | pushdecl (parm); |
111 | break; | |
786b5245 MM |
112 | |
113 | case PARM_DECL: | |
114 | { | |
115 | /* Make a CONST_DECL as is done in process_template_parm. */ | |
116 | tree decl = build_decl (CONST_DECL, DECL_NAME (parm), | |
117 | TREE_TYPE (parm)); | |
118 | DECL_INITIAL (decl) = DECL_INITIAL (parm); | |
119 | pushdecl (decl); | |
120 | } | |
121 | break; | |
122 | ||
98c1c668 JM |
123 | default: |
124 | my_friendly_abort (0); | |
125 | } | |
126 | } | |
127 | } | |
128 | ||
129 | /* Undo the effects of begin_member_template_processing. */ | |
130 | ||
131 | void | |
132 | end_member_template_processing () | |
133 | { | |
134 | if (! processing_template_decl) | |
135 | return; | |
136 | ||
137 | --processing_template_decl; | |
138 | current_template_parms = TREE_CHAIN (current_template_parms); | |
786b5245 | 139 | poplevel (0, 0, 0); |
98c1c668 JM |
140 | } |
141 | ||
142 | /* Returns non-zero iff T is a member template function. Works if T | |
143 | is either a FUNCTION_DECL or a TEMPLATE_DECL. */ | |
144 | ||
145 | int | |
146 | is_member_template (t) | |
147 | tree t; | |
148 | { | |
149 | int r = 0; | |
150 | ||
aa5f3bad MM |
151 | if (TREE_CODE (t) != FUNCTION_DECL |
152 | && !DECL_FUNCTION_TEMPLATE_P (t)) | |
153 | /* Anything that isn't a template or a template functon is | |
154 | certainly not a member template. */ | |
155 | return 0; | |
156 | ||
386b8a85 JM |
157 | if ((DECL_FUNCTION_MEMBER_P (t) |
158 | && !DECL_TEMPLATE_SPECIALIZATION (t)) | |
159 | || (TREE_CODE (t) == TEMPLATE_DECL && | |
160 | DECL_FUNCTION_MEMBER_P (DECL_TEMPLATE_RESULT (t)))) | |
98c1c668 JM |
161 | { |
162 | tree tmpl = NULL_TREE; | |
163 | ||
164 | if (DECL_FUNCTION_TEMPLATE_P (t)) | |
165 | tmpl = t; | |
166 | else if (DECL_TEMPLATE_INFO (t) | |
167 | && DECL_FUNCTION_TEMPLATE_P (DECL_TI_TEMPLATE (t))) | |
168 | tmpl = DECL_TI_TEMPLATE (t); | |
169 | ||
170 | if (tmpl) | |
171 | { | |
172 | tree parms = DECL_TEMPLATE_PARMS (tmpl); | |
173 | int parm_levels = list_length (parms); | |
174 | int template_class_levels = 0; | |
175 | tree ctx = DECL_CLASS_CONTEXT (t); | |
176 | ||
177 | if (CLASSTYPE_TEMPLATE_INFO (ctx)) | |
178 | { | |
179 | tree args; | |
180 | ||
181 | /* Here, we should really count the number of levels | |
182 | deep ctx is, making sure not to count any levels that | |
183 | are just specializations. Since there are no member | |
184 | template classes yet, we don't have to do all that. */ | |
185 | ||
186 | if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx)) | |
187 | template_class_levels = 1; | |
188 | else | |
189 | { | |
190 | int i; | |
191 | ||
192 | args = CLASSTYPE_TI_ARGS (ctx); | |
193 | ||
194 | if (args == NULL_TREE) | |
195 | template_class_levels = 1; | |
196 | else | |
197 | for (i = 0; i < TREE_VEC_LENGTH (args); ++i) | |
198 | if (uses_template_parms (TREE_VEC_ELT (args, i))) | |
199 | { | |
200 | template_class_levels++; | |
201 | break; | |
202 | } | |
203 | } | |
204 | } | |
205 | ||
206 | if (parm_levels > template_class_levels) | |
207 | r = 1; | |
208 | } | |
209 | } | |
210 | ||
211 | return r; | |
212 | } | |
213 | ||
214 | /* Return a new template argument vector which contains all of ARGS, | |
215 | but has as its innermost set of arguments the EXTRA_ARGS. */ | |
216 | ||
217 | tree | |
218 | add_to_template_args (args, extra_args) | |
219 | tree args; | |
220 | tree extra_args; | |
221 | { | |
222 | tree new_args; | |
223 | ||
224 | if (TREE_CODE (TREE_VEC_ELT (args, 0)) != TREE_VEC) | |
225 | { | |
226 | new_args = make_tree_vec (2); | |
227 | TREE_VEC_ELT (new_args, 0) = args; | |
228 | } | |
229 | else | |
230 | { | |
231 | int i; | |
232 | ||
233 | new_args = make_tree_vec (TREE_VEC_LENGTH (args) - 1); | |
234 | ||
235 | for (i = 0; i < TREE_VEC_LENGTH (args); ++i) | |
236 | TREE_VEC_ELT (new_args, i) = TREE_VEC_ELT (args, i); | |
237 | } | |
238 | ||
239 | TREE_VEC_ELT (new_args, | |
240 | TREE_VEC_LENGTH (new_args) - 1) = extra_args; | |
241 | ||
242 | return new_args; | |
243 | } | |
5566b478 MS |
244 | |
245 | /* We've got a template header coming up; push to a new level for storing | |
246 | the parms. */ | |
8d08fdba | 247 | |
8d08fdba MS |
248 | void |
249 | begin_template_parm_list () | |
250 | { | |
251 | pushlevel (0); | |
5566b478 | 252 | declare_pseudo_global_level (); |
5156628f | 253 | ++processing_template_decl; |
386b8a85 JM |
254 | note_template_header (0); |
255 | } | |
256 | ||
257 | ||
258 | /* We've just seen template <>. */ | |
259 | ||
260 | void | |
261 | begin_specialization () | |
262 | { | |
263 | note_template_header (1); | |
264 | } | |
265 | ||
266 | ||
267 | /* Called at then end of processing a declaration preceeded by | |
268 | template<>. */ | |
269 | ||
270 | void | |
271 | end_specialization () | |
272 | { | |
273 | reset_specialization (); | |
274 | } | |
275 | ||
276 | ||
277 | /* Any template <>'s that we have seen thus far are not referring to a | |
278 | function specialization. */ | |
279 | ||
280 | void | |
281 | reset_specialization () | |
282 | { | |
283 | processing_specialization = 0; | |
284 | template_header_count = 0; | |
285 | } | |
286 | ||
287 | ||
288 | /* We've just seen a template header. If SPECIALIZATION is non-zero, | |
289 | it was of the form template <>. */ | |
290 | ||
291 | void | |
292 | note_template_header (specialization) | |
293 | int specialization; | |
294 | { | |
295 | processing_specialization = specialization; | |
296 | template_header_count++; | |
297 | } | |
298 | ||
299 | ||
300 | /* Returns non-zero iff a declarator, in which the number of template | |
301 | types that appeared was TEMPLATE_COUNT, is an explicit | |
302 | specialization. */ | |
303 | ||
304 | int | |
305 | processing_explicit_specialization(template_count) | |
306 | int template_count; | |
307 | { | |
308 | /* A function declaration is an explicit specialization of a member | |
309 | template if all of the following conditions hold: | |
310 | ||
311 | o There was a template <...> preceeding the declaration. | |
312 | o The last template <...> was in fact template <>. | |
313 | o The number of template <...>'s preceeding the declaration, less | |
314 | the number of template classes with arguments specified used to | |
315 | qualify the function name, is 1. | |
316 | ||
317 | For example: | |
318 | ||
319 | template <> void S<int>::foo(); | |
320 | template <class T> template <> void S<T>::foo(); | |
321 | template <> struct S<int> { ... template <> void foo(); } | |
322 | ||
323 | The first of these is not a specialization of S<int>::foo() (it | |
324 | is instead a specialization of S<T>::foo), while the next two are | |
325 | specializations of member template functions. */ | |
326 | ||
327 | return processing_specialization | |
328 | && template_header_count > template_count; | |
329 | } | |
330 | ||
331 | /* Returns the template function specialized by TEMPLATE_ID, or | |
332 | NULL_TREE if there is none. | |
333 | ||
334 | The TEMPLATE_ID is a TEMPLATE_ID_EXPR. The TYPE is | |
335 | the type it has been declared to have. Return the TEMPLATE_DECL | |
336 | that is being specialized, and put the specialization arguments in | |
337 | *TARGS. If no appropriate specialization can be found, NULL_TREE is | |
338 | returned, and *TARGS is assigned NULL_TREE. If complain is | |
339 | non-zero, error messages are printed where appropriate. */ | |
340 | ||
341 | tree | |
342 | determine_explicit_specialization (template_id, type, targs_out, | |
343 | need_member_template, | |
344 | complain) | |
345 | tree template_id; | |
346 | tree type; | |
347 | tree* targs_out; | |
348 | int need_member_template; | |
349 | int complain; | |
350 | { | |
351 | int i; | |
352 | int overloaded; | |
353 | tree fns; | |
354 | tree matching_fns = NULL_TREE; | |
355 | tree name = NULL_TREE; | |
356 | tree result; | |
357 | tree fn; | |
358 | ||
359 | my_friendly_assert (TREE_CODE (template_id) == TEMPLATE_ID_EXPR, | |
360 | 0); | |
361 | ||
362 | fns = TREE_OPERAND (template_id, 0); | |
363 | ||
364 | overloaded = fns != NULL_TREE && really_overloaded_fn (fns); | |
365 | ||
366 | for (fn = (fns != NULL_TREE) ? get_first_fn (fns) : NULL_TREE; | |
367 | fn != NULL_TREE; | |
368 | fn = overloaded ? DECL_CHAIN (fn) : NULL_TREE) | |
369 | { | |
370 | int dummy; | |
371 | tree targs; | |
372 | ||
373 | if (name == NULL_TREE) | |
374 | name = DECL_NAME (fn); | |
375 | ||
376 | if (TREE_CODE (fn) != TEMPLATE_DECL | |
377 | || (need_member_template && !is_member_template (fn))) | |
378 | continue; | |
379 | ||
380 | if (list_length (TREE_OPERAND (template_id, 1)) > DECL_NTPARMS (fn)) | |
381 | continue; | |
382 | ||
383 | targs = make_tree_vec (DECL_NTPARMS (fn)); | |
384 | ||
385 | /* We allow incomplete unification here, because we are going to | |
386 | check all the functions. */ | |
387 | i = type_unification (DECL_INNERMOST_TEMPLATE_PARMS (fn), | |
388 | &TREE_VEC_ELT (targs, 0), | |
389 | type | |
390 | ? TYPE_ARG_TYPES (TREE_TYPE (fn)) : NULL_TREE, | |
391 | type ? TYPE_ARG_TYPES (type) : NULL_TREE, | |
392 | TREE_OPERAND (template_id, 1), | |
393 | &dummy, 1, 1); | |
394 | ||
395 | if (i == 0) | |
396 | { | |
397 | /* Unification was successful. See if the return types | |
398 | match. */ | |
399 | if (type != NULL_TREE) | |
400 | { | |
401 | tree tmpl_return_type = tsubst (TREE_TYPE (TREE_TYPE (fn)), | |
402 | targs, | |
403 | DECL_NTPARMS (fn), | |
404 | NULL_TREE); | |
405 | ||
406 | if (tmpl_return_type != TREE_TYPE (type)) | |
407 | { | |
408 | /* Always complain about this. With ambiguity, some | |
409 | other context, might resolve things. But, a | |
410 | non-matching return type will always be a | |
411 | problem. */ | |
412 | cp_error ("Return type of explicit specialization of"); | |
413 | cp_error ("`%D' is `%T', but should be `%T'.", | |
414 | fn, TREE_TYPE (type), tmpl_return_type); | |
415 | *targs_out = NULL_TREE; | |
416 | return NULL_TREE; | |
417 | } | |
418 | } | |
419 | ||
420 | matching_fns = tree_cons (fn, targs, matching_fns); | |
421 | } | |
422 | } | |
423 | ||
424 | if (matching_fns == NULL_TREE) | |
425 | { | |
426 | if (complain) | |
427 | cp_error ("Specialization of `%s' does not match any template " | |
428 | "declaration.", IDENTIFIER_POINTER (name)); | |
429 | *targs_out = NULL_TREE; | |
430 | return NULL_TREE; | |
431 | } | |
432 | ||
433 | if (TREE_CHAIN (matching_fns) != NULL_TREE) | |
434 | { | |
435 | if (complain) | |
436 | { | |
437 | tree fn; | |
438 | ||
439 | cp_error ("Ambiguous explicit specialization. Candidates are:"); | |
440 | for (fn = matching_fns; fn != NULL_TREE; fn = TREE_CHAIN (fn)) | |
441 | cp_error (" %D", TREE_PURPOSE (fn)); | |
442 | } | |
443 | ||
444 | *targs_out = NULL_TREE; | |
445 | return NULL_TREE; | |
446 | } | |
447 | ||
448 | /* We have one, and exactly one, match. */ | |
449 | *targs_out = TREE_VALUE (matching_fns); | |
450 | return TREE_PURPOSE (matching_fns); | |
8d08fdba MS |
451 | } |
452 | ||
386b8a85 JM |
453 | |
454 | /* Check to see if the function just declared, as indicated in | |
455 | DECLARATOR, and in DECL, is a specialization. Check that the | |
456 | specialization is OK. If FLAGS == 1, we are being called by | |
457 | finish_struct_methods. If FLAGS == 2, we are being called by | |
458 | grokfndecl, and the function has a definition, or is a friend. If | |
459 | FLAGS == 3, this is a friend declaration. | |
460 | Returns 0 if the decl is not an explicit specialization or | |
461 | instantiation, 1 if it is an explicit specialization, and 2 if it | |
462 | is an explicit instantiation. */ | |
463 | ||
464 | int | |
27bb8339 | 465 | check_explicit_specialization (declarator, decl, template_count, flags) |
386b8a85 JM |
466 | tree declarator; |
467 | tree decl; | |
468 | int template_count; | |
469 | int flags; | |
470 | { | |
471 | int finish_member = flags == 1; | |
472 | int have_def = flags == 2; | |
473 | int is_friend = flags == 3; | |
474 | ||
475 | if (processing_explicit_specialization (template_count) | |
476 | || finish_member | |
477 | || TREE_CODE (declarator) == TEMPLATE_ID_EXPR) | |
478 | { | |
479 | tree tmpl = NULL_TREE; | |
480 | tree dname = DECL_NAME (decl); | |
481 | tree ctype = DECL_CLASS_CONTEXT (decl); | |
482 | tree targs; | |
483 | ||
484 | /* We've come across a declarator that looks like: U f<T1, | |
485 | T2, ...>(A1, A2, ..). This is an explicit template | |
486 | specialization. Check that: | |
487 | ||
488 | o The explicitly specified parameters together with those | |
489 | that can be deduced by template argument deduction | |
490 | uniquely determine a particular specialization. | |
491 | ||
492 | See [temp.expl.spec]. */ | |
493 | ||
494 | if (!finish_member | |
495 | && TREE_CODE (declarator) == TEMPLATE_ID_EXPR | |
496 | && !processing_explicit_specialization (template_count) | |
497 | && !is_friend) | |
498 | { | |
499 | if (!have_def) | |
500 | /* This is not an explicit specialization. It must be | |
501 | an explicit instantiation. */ | |
502 | return 2; | |
503 | else if (pedantic) | |
504 | pedwarn ("Explicit specialization not preceeded by " | |
505 | "`template <>'"); | |
506 | } | |
507 | ||
508 | if (TREE_CODE (declarator) != TEMPLATE_ID_EXPR) | |
509 | { | |
510 | tree fns; | |
511 | ||
512 | my_friendly_assert (TREE_CODE (declarator) == IDENTIFIER_NODE, | |
513 | 0); | |
514 | if (!ctype) | |
515 | fns = IDENTIFIER_GLOBAL_VALUE (dname); | |
516 | else | |
517 | fns = dname; | |
518 | ||
519 | declarator = lookup_template_function (fns, NULL_TREE); | |
520 | } | |
521 | ||
522 | if (TREE_CODE (TREE_OPERAND (declarator, 0)) == LOOKUP_EXPR) | |
523 | { | |
524 | /* A friend declaration. We can't do much, because we don't | |
525 | know what this resolves to, yet. */ | |
526 | my_friendly_assert (is_friend != 0, 0); | |
527 | SET_DECL_IMPLICIT_INSTANTIATION (decl); | |
528 | return 1; | |
529 | } | |
530 | ||
531 | if (ctype | |
532 | && TREE_CODE (TREE_OPERAND (declarator, 0)) == IDENTIFIER_NODE) | |
533 | { | |
534 | tree fns; | |
535 | ||
536 | if (TYPE_BEING_DEFINED (ctype) && !finish_member) | |
537 | { | |
538 | /* Since finish_struct_1 has not been called yet, we | |
539 | can't call lookup_fnfields. We note that this | |
540 | template is a specialization, and proceed, letting | |
541 | finish_struct_methods fix this up later. */ | |
542 | SET_DECL_TEMPLATE_SPECIALIZATION (decl); | |
543 | DECL_TEMPLATE_INFO (decl) | |
544 | = perm_tree_cons (NULL_TREE, | |
545 | TREE_OPERAND (declarator, 1), | |
546 | NULL_TREE); | |
547 | return 1; | |
548 | } | |
549 | ||
550 | fns = lookup_fnfields (TYPE_BINFO (ctype), | |
551 | TREE_OPERAND (declarator, 0), | |
552 | 1); | |
553 | ||
554 | if (fns == NULL_TREE) | |
555 | { | |
556 | cp_error ("No member template `%s' declared in `%T'", | |
557 | IDENTIFIER_POINTER (TREE_OPERAND (declarator, | |
558 | 0)), | |
559 | ctype); | |
560 | return 1; | |
561 | } | |
562 | else | |
563 | TREE_OPERAND (declarator, 0) = fns; | |
564 | } | |
565 | ||
566 | tmpl = | |
567 | determine_explicit_specialization | |
568 | (declarator, TREE_TYPE (decl), &targs, | |
569 | TREE_CODE (decl) == TEMPLATE_DECL, 1); | |
570 | ||
571 | if (tmpl) | |
572 | { | |
573 | /* Mangle the function name appropriately. */ | |
574 | if (name_mangling_version >= 1) | |
575 | { | |
576 | tree arg_types = TYPE_ARG_TYPES (TREE_TYPE (tmpl)); | |
577 | ||
578 | if (ctype | |
579 | && TREE_CODE (TREE_TYPE (tmpl)) == FUNCTION_TYPE) | |
580 | arg_types = | |
581 | hash_tree_chain (build_pointer_type (ctype), | |
582 | arg_types); | |
583 | ||
584 | DECL_ASSEMBLER_NAME (decl) | |
585 | = build_template_decl_overload | |
586 | (DECL_NAME (decl), | |
587 | arg_types, | |
588 | TREE_TYPE (TREE_TYPE (tmpl)), | |
589 | DECL_INNERMOST_TEMPLATE_PARMS (tmpl), | |
590 | targs, ctype != NULL_TREE); | |
591 | } | |
592 | ||
593 | if (is_friend && !have_def) | |
594 | { | |
595 | /* This is not really a declaration of a specialization. | |
596 | It's just the name of an instantiation. But, it's not | |
597 | a request for an instantiation, either. */ | |
598 | SET_DECL_IMPLICIT_INSTANTIATION (decl); | |
599 | DECL_TEMPLATE_INFO (decl) | |
600 | = perm_tree_cons (tmpl, targs, NULL_TREE); | |
601 | return 1; | |
602 | } | |
603 | ||
604 | /* This function declaration is a template specialization. | |
605 | Record that fact. */ | |
606 | SET_DECL_TEMPLATE_SPECIALIZATION (decl); | |
607 | DECL_TEMPLATE_SPECIALIZATIONS (tmpl) | |
608 | = perm_tree_cons (targs, decl, | |
609 | DECL_TEMPLATE_SPECIALIZATIONS | |
610 | (tmpl)); | |
611 | /* If DECL_TI_TEMPLATE (decl), the decl is an | |
612 | instantiation of a specialization of a member template. | |
613 | (In other words, there was a member template, in a | |
614 | class template. That member template was specialized. | |
615 | We then instantiated the class, so there is now an | |
616 | instance of that specialization.) | |
617 | ||
618 | According to the CD2, | |
619 | ||
620 | 14.7.3.13 [tmpl.expl.spec] | |
621 | ||
622 | A specialization of a member function template or | |
623 | member class template of a non-specialized class | |
624 | template is itself a template. | |
625 | ||
626 | So, we just leave the template info alone in this case. | |
627 | */ | |
628 | if (!(DECL_TEMPLATE_INFO (decl) && DECL_TI_TEMPLATE (decl))) | |
629 | DECL_TEMPLATE_INFO (decl) | |
630 | = perm_tree_cons (tmpl, targs, NULL_TREE); | |
631 | return 1; | |
632 | } | |
633 | } | |
634 | ||
635 | return 0; | |
636 | } | |
637 | ||
8d08fdba | 638 | /* Process information from new template parameter NEXT and append it to the |
5566b478 | 639 | LIST being built. */ |
e92cc029 | 640 | |
8d08fdba MS |
641 | tree |
642 | process_template_parm (list, next) | |
643 | tree list, next; | |
644 | { | |
645 | tree parm; | |
646 | tree decl = 0; | |
a292b002 | 647 | tree defval; |
5566b478 | 648 | int is_type, idx; |
8d08fdba MS |
649 | parm = next; |
650 | my_friendly_assert (TREE_CODE (parm) == TREE_LIST, 259); | |
a292b002 MS |
651 | defval = TREE_PURPOSE (parm); |
652 | parm = TREE_VALUE (parm); | |
653 | is_type = TREE_PURPOSE (parm) == class_type_node; | |
5566b478 MS |
654 | |
655 | if (list) | |
656 | { | |
657 | tree p = TREE_VALUE (tree_last (list)); | |
658 | ||
659 | if (TREE_CODE (p) == TYPE_DECL) | |
660 | idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p)); | |
661 | else | |
662 | idx = TEMPLATE_CONST_IDX (DECL_INITIAL (p)); | |
663 | ++idx; | |
664 | } | |
665 | else | |
666 | idx = 0; | |
667 | ||
8d08fdba MS |
668 | if (!is_type) |
669 | { | |
670 | tree tinfo = 0; | |
a292b002 | 671 | my_friendly_assert (TREE_CODE (TREE_PURPOSE (parm)) == TREE_LIST, 260); |
8d08fdba | 672 | /* is a const-param */ |
a292b002 | 673 | parm = grokdeclarator (TREE_VALUE (parm), TREE_PURPOSE (parm), |
c11b6f21 | 674 | PARM, 0, NULL_TREE); |
8d08fdba MS |
675 | /* A template parameter is not modifiable. */ |
676 | TREE_READONLY (parm) = 1; | |
c91a56d2 MS |
677 | if (IS_AGGR_TYPE (TREE_TYPE (parm)) |
678 | && TREE_CODE (TREE_TYPE (parm)) != TEMPLATE_TYPE_PARM) | |
8d08fdba | 679 | { |
c91a56d2 MS |
680 | cp_error ("`%#T' is not a valid type for a template constant parameter", |
681 | TREE_TYPE (parm)); | |
682 | if (DECL_NAME (parm) == NULL_TREE) | |
683 | error (" a template type parameter must begin with `class' or `typename'"); | |
8d08fdba MS |
684 | TREE_TYPE (parm) = void_type_node; |
685 | } | |
37c46b43 MS |
686 | else if (pedantic |
687 | && (TREE_CODE (TREE_TYPE (parm)) == REAL_TYPE | |
688 | || TREE_CODE (TREE_TYPE (parm)) == COMPLEX_TYPE)) | |
eb66be0e MS |
689 | cp_pedwarn ("`%T' is not a valid type for a template constant parameter", |
690 | TREE_TYPE (parm)); | |
8d08fdba MS |
691 | tinfo = make_node (TEMPLATE_CONST_PARM); |
692 | my_friendly_assert (TREE_PERMANENT (tinfo), 260.5); | |
693 | if (TREE_PERMANENT (parm) == 0) | |
694 | { | |
695 | parm = copy_node (parm); | |
696 | TREE_PERMANENT (parm) = 1; | |
697 | } | |
698 | TREE_TYPE (tinfo) = TREE_TYPE (parm); | |
699 | decl = build_decl (CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm)); | |
700 | DECL_INITIAL (decl) = tinfo; | |
701 | DECL_INITIAL (parm) = tinfo; | |
5156628f | 702 | TEMPLATE_CONST_SET_INFO (tinfo, idx, processing_template_decl); |
8d08fdba MS |
703 | } |
704 | else | |
705 | { | |
5566b478 MS |
706 | tree t = make_lang_type (TEMPLATE_TYPE_PARM); |
707 | CLASSTYPE_GOT_SEMICOLON (t) = 1; | |
a292b002 | 708 | decl = build_decl (TYPE_DECL, TREE_VALUE (parm), t); |
d2e5ee5c MS |
709 | TYPE_NAME (t) = decl; |
710 | TYPE_STUB_DECL (t) = decl; | |
a292b002 | 711 | parm = decl; |
5156628f | 712 | TEMPLATE_TYPE_SET_INFO (t, idx, processing_template_decl); |
8d08fdba | 713 | } |
8ccc31eb | 714 | SET_DECL_ARTIFICIAL (decl); |
8d08fdba | 715 | pushdecl (decl); |
a292b002 | 716 | parm = build_tree_list (defval, parm); |
8d08fdba MS |
717 | return chainon (list, parm); |
718 | } | |
719 | ||
720 | /* The end of a template parameter list has been reached. Process the | |
721 | tree list into a parameter vector, converting each parameter into a more | |
722 | useful form. Type parameters are saved as IDENTIFIER_NODEs, and others | |
723 | as PARM_DECLs. */ | |
724 | ||
725 | tree | |
726 | end_template_parm_list (parms) | |
727 | tree parms; | |
728 | { | |
5566b478 | 729 | int nparms; |
8d08fdba | 730 | tree parm; |
5566b478 MS |
731 | tree saved_parmlist = make_tree_vec (list_length (parms)); |
732 | ||
5566b478 MS |
733 | current_template_parms |
734 | = tree_cons (build_int_2 (0, processing_template_decl), | |
735 | saved_parmlist, current_template_parms); | |
8d08fdba MS |
736 | |
737 | for (parm = parms, nparms = 0; parm; parm = TREE_CHAIN (parm), nparms++) | |
5566b478 | 738 | TREE_VEC_ELT (saved_parmlist, nparms) = parm; |
a292b002 | 739 | |
8d08fdba MS |
740 | return saved_parmlist; |
741 | } | |
742 | ||
5566b478 MS |
743 | /* end_template_decl is called after a template declaration is seen. */ |
744 | ||
8d08fdba | 745 | void |
5566b478 | 746 | end_template_decl () |
8d08fdba | 747 | { |
386b8a85 JM |
748 | reset_specialization (); |
749 | ||
5156628f | 750 | if (! processing_template_decl) |
73aad9b9 JM |
751 | return; |
752 | ||
5566b478 MS |
753 | /* This matches the pushlevel in begin_template_parm_list. */ |
754 | poplevel (0, 0, 0); | |
8d08fdba | 755 | |
5566b478 MS |
756 | --processing_template_decl; |
757 | current_template_parms = TREE_CHAIN (current_template_parms); | |
758 | (void) get_pending_sizes (); /* Why? */ | |
759 | } | |
8d08fdba | 760 | |
9a3b49ac MS |
761 | /* Generate a valid set of template args from current_template_parms. */ |
762 | ||
763 | tree | |
764 | current_template_args () | |
5566b478 MS |
765 | { |
766 | tree header = current_template_parms; | |
98c1c668 JM |
767 | int length = list_length (header); |
768 | tree args = make_tree_vec (length); | |
769 | int l = length; | |
770 | ||
5566b478 | 771 | while (header) |
8d08fdba | 772 | { |
5566b478 MS |
773 | tree a = copy_node (TREE_VALUE (header)); |
774 | int i = TREE_VEC_LENGTH (a); | |
775 | TREE_TYPE (a) = NULL_TREE; | |
776 | while (i--) | |
777 | { | |
98c1c668 JM |
778 | tree t = TREE_VEC_ELT (a, i); |
779 | ||
780 | /* t will be a list if we are called from within a | |
781 | begin/end_template_parm_list pair, but a vector directly | |
782 | if within a begin/end_member_template_processing pair. */ | |
783 | if (TREE_CODE (t) == TREE_LIST) | |
784 | { | |
785 | t = TREE_VALUE (t); | |
786 | ||
787 | if (TREE_CODE (t) == TYPE_DECL) | |
788 | t = TREE_TYPE (t); | |
789 | else | |
790 | t = DECL_INITIAL (t); | |
791 | } | |
792 | ||
5566b478 MS |
793 | TREE_VEC_ELT (a, i) = t; |
794 | } | |
98c1c668 | 795 | TREE_VEC_ELT (args, --l) = a; |
5566b478 | 796 | header = TREE_CHAIN (header); |
8d08fdba MS |
797 | } |
798 | ||
9a3b49ac MS |
799 | return args; |
800 | } | |
801 | ||
802 | void | |
803 | push_template_decl (decl) | |
804 | tree decl; | |
805 | { | |
806 | tree tmpl; | |
807 | tree args = NULL_TREE; | |
808 | tree info; | |
809 | tree ctx = DECL_CONTEXT (decl) ? DECL_CONTEXT (decl) : current_class_type; | |
810 | int primary = 0; | |
811 | ||
812 | /* Kludge! */ | |
813 | if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl) | |
814 | && DECL_CLASS_CONTEXT (decl)) | |
815 | ; | |
816 | /* Note that this template is a "primary template" */ | |
817 | else if (! ctx || ! CLASSTYPE_TEMPLATE_INFO (ctx) | |
818 | /* || (processing_template_decl > CLASSTYPE_TEMPLATE_LEVEL (ctx)) */) | |
819 | primary = 1; | |
820 | ||
73aad9b9 | 821 | /* Partial specialization. */ |
824b9a4c | 822 | if (TREE_CODE (decl) == TYPE_DECL && DECL_ARTIFICIAL (decl) |
73aad9b9 JM |
823 | && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl))) |
824 | { | |
825 | tree type = TREE_TYPE (decl); | |
826 | tree maintmpl = CLASSTYPE_TI_TEMPLATE (type); | |
827 | tree mainargs = CLASSTYPE_TI_ARGS (type); | |
828 | tree spec = DECL_TEMPLATE_SPECIALIZATIONS (maintmpl); | |
829 | ||
830 | for (; spec; spec = TREE_CHAIN (spec)) | |
831 | { | |
832 | /* purpose: args to main template | |
833 | value: spec template */ | |
834 | if (comp_template_args (TREE_PURPOSE (spec), mainargs)) | |
835 | return; | |
836 | } | |
837 | ||
6633d636 MS |
838 | DECL_TEMPLATE_SPECIALIZATIONS (maintmpl) = CLASSTYPE_TI_SPEC_INFO (type) |
839 | = perm_tree_cons (mainargs, TREE_VALUE (current_template_parms), | |
840 | DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)); | |
73aad9b9 JM |
841 | TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type; |
842 | return; | |
843 | } | |
844 | ||
9a3b49ac MS |
845 | args = current_template_args (); |
846 | ||
5566b478 | 847 | if (! ctx || TYPE_BEING_DEFINED (ctx)) |
8d08fdba | 848 | { |
5566b478 | 849 | tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE); |
98c1c668 | 850 | DECL_TEMPLATE_PARMS (tmpl) = current_template_parms; |
5566b478 | 851 | DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl); |
98c1c668 | 852 | if (DECL_LANG_SPECIFIC (decl)) |
786b5245 MM |
853 | { |
854 | DECL_CLASS_CONTEXT (tmpl) = DECL_CLASS_CONTEXT (decl); | |
855 | DECL_STATIC_FUNCTION_P (tmpl) = | |
856 | DECL_STATIC_FUNCTION_P (decl); | |
386b8a85 JM |
857 | |
858 | if (DECL_TEMPLATE_SPECIALIZATION (decl)) | |
859 | { | |
860 | /* A specialization of a member template of a template | |
861 | class. */ | |
862 | SET_DECL_TEMPLATE_SPECIALIZATION (tmpl); | |
863 | DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl); | |
864 | DECL_TEMPLATE_INFO (decl) = NULL_TREE; | |
865 | } | |
786b5245 | 866 | } |
8d08fdba MS |
867 | } |
868 | else | |
869 | { | |
6633d636 | 870 | tree t; |
98c1c668 | 871 | tree a; |
6633d636 | 872 | |
73aad9b9 JM |
873 | if (CLASSTYPE_TEMPLATE_INSTANTIATION (ctx)) |
874 | cp_error ("must specialize `%#T' before defining member `%#D'", | |
875 | ctx, decl); | |
824b9a4c | 876 | if (TREE_CODE (decl) == TYPE_DECL && DECL_ARTIFICIAL (decl)) |
5566b478 | 877 | tmpl = CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)); |
fc378698 | 878 | else if (! DECL_TEMPLATE_INFO (decl)) |
c91a56d2 MS |
879 | { |
880 | cp_error ("template definition of non-template `%#D'", decl); | |
881 | return; | |
882 | } | |
8d08fdba | 883 | else |
5566b478 | 884 | tmpl = DECL_TI_TEMPLATE (decl); |
98c1c668 JM |
885 | |
886 | if (is_member_template (tmpl)) | |
887 | { | |
888 | a = TREE_VEC_ELT (args, TREE_VEC_LENGTH (args) - 1); | |
889 | t = DECL_INNERMOST_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)); | |
890 | if (TREE_VEC_LENGTH (t) | |
891 | != TREE_VEC_LENGTH (a)) | |
892 | { | |
893 | cp_error ("got %d template parameters for `%#D'", | |
894 | TREE_VEC_LENGTH (a), decl); | |
895 | cp_error (" but %d required", TREE_VEC_LENGTH (t)); | |
896 | } | |
897 | if (TREE_VEC_LENGTH (args) > 1) | |
898 | /* Get the template parameters for the enclosing template | |
899 | class. */ | |
900 | a = TREE_VEC_ELT (args, TREE_VEC_LENGTH (args) - 2); | |
901 | else | |
902 | a = NULL_TREE; | |
903 | } | |
904 | else | |
905 | a = TREE_VEC_ELT (args, TREE_VEC_LENGTH (args) - 1); | |
906 | ||
907 | t = NULL_TREE; | |
6633d636 MS |
908 | |
909 | if (CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx)) | |
98c1c668 JM |
910 | { |
911 | /* When processing an inline member template of a | |
912 | specialized class, there is no CLASSTYPE_TI_SPEC_INFO. */ | |
913 | if (CLASSTYPE_TI_SPEC_INFO (ctx)) | |
914 | t = TREE_VALUE (CLASSTYPE_TI_SPEC_INFO (ctx)); | |
915 | } | |
916 | else if (CLASSTYPE_TEMPLATE_INFO (ctx)) | |
917 | t = DECL_INNERMOST_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (ctx)); | |
918 | ||
919 | /* There should be template arguments if and only if there is a | |
920 | template class. */ | |
921 | my_friendly_assert((a != NULL_TREE) == (t != NULL_TREE), 0); | |
6633d636 | 922 | |
98c1c668 JM |
923 | if (t != NULL_TREE |
924 | && TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a)) | |
6633d636 MS |
925 | { |
926 | cp_error ("got %d template parameters for `%#D'", | |
98c1c668 | 927 | TREE_VEC_LENGTH (a), decl); |
6633d636 MS |
928 | cp_error (" but `%#T' has %d", ctx, TREE_VEC_LENGTH (t)); |
929 | } | |
5566b478 | 930 | } |
98c1c668 JM |
931 | /* Get the innermost set of template arguments. */ |
932 | args = TREE_VEC_ELT (args, TREE_VEC_LENGTH (args) - 1); | |
8d08fdba | 933 | |
5566b478 MS |
934 | DECL_TEMPLATE_RESULT (tmpl) = decl; |
935 | TREE_TYPE (tmpl) = TREE_TYPE (decl); | |
8d08fdba | 936 | |
5566b478 MS |
937 | if (! ctx) |
938 | tmpl = pushdecl_top_level (tmpl); | |
8d08fdba | 939 | |
5566b478 | 940 | if (primary) |
98c1c668 | 941 | TREE_TYPE (DECL_INNERMOST_TEMPLATE_PARMS (tmpl)) = tmpl; |
5566b478 MS |
942 | |
943 | info = perm_tree_cons (tmpl, args, NULL_TREE); | |
944 | ||
824b9a4c | 945 | if (TREE_CODE (decl) == TYPE_DECL && DECL_ARTIFICIAL (decl)) |
8d08fdba | 946 | { |
5566b478 MS |
947 | CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (tmpl)) = info; |
948 | DECL_NAME (decl) = classtype_mangled_name (TREE_TYPE (decl)); | |
51c184be | 949 | } |
ec255269 MS |
950 | else if (! DECL_LANG_SPECIFIC (decl)) |
951 | cp_error ("template declaration of `%#D'", decl); | |
51c184be | 952 | else |
5566b478 | 953 | DECL_TEMPLATE_INFO (decl) = info; |
8d08fdba MS |
954 | } |
955 | ||
8d08fdba MS |
956 | /* Convert all template arguments to their appropriate types, and return |
957 | a vector containing the resulting values. If any error occurs, return | |
958 | error_mark_node. */ | |
e92cc029 | 959 | |
8d08fdba MS |
960 | static tree |
961 | coerce_template_parms (parms, arglist, in_decl) | |
962 | tree parms, arglist; | |
963 | tree in_decl; | |
964 | { | |
a292b002 | 965 | int nparms, nargs, i, lost = 0; |
8d08fdba MS |
966 | tree vec; |
967 | ||
a292b002 MS |
968 | if (arglist == NULL_TREE) |
969 | nargs = 0; | |
970 | else if (TREE_CODE (arglist) == TREE_VEC) | |
971 | nargs = TREE_VEC_LENGTH (arglist); | |
8d08fdba | 972 | else |
a292b002 MS |
973 | nargs = list_length (arglist); |
974 | ||
975 | nparms = TREE_VEC_LENGTH (parms); | |
976 | ||
977 | if (nargs > nparms | |
978 | || (nargs < nparms | |
979 | && TREE_PURPOSE (TREE_VEC_ELT (parms, nargs)) == NULL_TREE)) | |
8d08fdba MS |
980 | { |
981 | error ("incorrect number of parameters (%d, should be %d)", | |
a292b002 | 982 | nargs, nparms); |
8d08fdba MS |
983 | if (in_decl) |
984 | cp_error_at ("in template expansion for decl `%D'", in_decl); | |
985 | return error_mark_node; | |
986 | } | |
987 | ||
a292b002 | 988 | if (arglist && TREE_CODE (arglist) == TREE_VEC) |
8d08fdba MS |
989 | vec = copy_node (arglist); |
990 | else | |
991 | { | |
992 | vec = make_tree_vec (nparms); | |
993 | for (i = 0; i < nparms; i++) | |
994 | { | |
a292b002 MS |
995 | tree arg; |
996 | ||
997 | if (arglist) | |
998 | { | |
999 | arg = arglist; | |
1000 | arglist = TREE_CHAIN (arglist); | |
1001 | ||
1002 | if (arg == error_mark_node) | |
1003 | lost++; | |
1004 | else | |
1005 | arg = TREE_VALUE (arg); | |
1006 | } | |
d2e5ee5c MS |
1007 | else if (TREE_CODE (TREE_VALUE (TREE_VEC_ELT (parms, i))) |
1008 | == TYPE_DECL) | |
5566b478 | 1009 | arg = tsubst (TREE_PURPOSE (TREE_VEC_ELT (parms, i)), |
98c1c668 | 1010 | vec, i, in_decl); |
d2e5ee5c MS |
1011 | else |
1012 | arg = tsubst_expr (TREE_PURPOSE (TREE_VEC_ELT (parms, i)), | |
98c1c668 | 1013 | vec, i, in_decl); |
a292b002 | 1014 | |
8d08fdba MS |
1015 | TREE_VEC_ELT (vec, i) = arg; |
1016 | } | |
1017 | } | |
1018 | for (i = 0; i < nparms; i++) | |
1019 | { | |
1020 | tree arg = TREE_VEC_ELT (vec, i); | |
a292b002 | 1021 | tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i)); |
8d08fdba MS |
1022 | tree val = 0; |
1023 | int is_type, requires_type; | |
1024 | ||
1025 | is_type = TREE_CODE_CLASS (TREE_CODE (arg)) == 't'; | |
a292b002 | 1026 | requires_type = TREE_CODE (parm) == TYPE_DECL; |
5566b478 MS |
1027 | |
1028 | if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF | |
1029 | && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM) | |
1030 | { | |
1031 | cp_pedwarn ("to refer to a type member of a template parameter,"); | |
1032 | cp_pedwarn (" use `typename %E'", arg); | |
1033 | arg = make_typename_type (TREE_OPERAND (arg, 0), | |
1034 | TREE_OPERAND (arg, 1)); | |
1035 | is_type = 1; | |
1036 | } | |
8d08fdba MS |
1037 | if (is_type != requires_type) |
1038 | { | |
1039 | if (in_decl) | |
5566b478 MS |
1040 | { |
1041 | cp_error ("type/value mismatch at argument %d in template parameter list for `%D'", | |
1042 | i, in_decl); | |
1043 | if (is_type) | |
1044 | cp_error (" expected a constant of type `%T', got `%T'", | |
1045 | TREE_TYPE (parm), arg); | |
1046 | else | |
1047 | cp_error (" expected a type, got `%E'", arg); | |
1048 | } | |
8d08fdba MS |
1049 | lost++; |
1050 | TREE_VEC_ELT (vec, i) = error_mark_node; | |
1051 | continue; | |
1052 | } | |
1053 | if (is_type) | |
ec255269 MS |
1054 | { |
1055 | val = groktypename (arg); | |
5156628f | 1056 | if (! processing_template_decl) |
ec255269 MS |
1057 | { |
1058 | tree t = target_type (val); | |
85b71cf2 JM |
1059 | if (TREE_CODE (t) != TYPENAME_TYPE |
1060 | && IS_AGGR_TYPE (t) | |
ec255269 MS |
1061 | && decl_function_context (TYPE_MAIN_DECL (t))) |
1062 | { | |
1063 | cp_error ("type `%T' composed from a local class is not a valid template-argument", val); | |
1064 | return error_mark_node; | |
1065 | } | |
1066 | } | |
1067 | } | |
8d08fdba MS |
1068 | else |
1069 | { | |
98c1c668 | 1070 | tree t = tsubst (TREE_TYPE (parm), vec, |
75b0bbce | 1071 | TREE_VEC_LENGTH (vec), in_decl); |
5156628f | 1072 | if (processing_template_decl) |
5566b478 MS |
1073 | val = arg; |
1074 | else | |
1075 | val = digest_init (t, arg, (tree *) 0); | |
a292b002 | 1076 | |
5156628f | 1077 | if (val == error_mark_node || processing_template_decl) |
8d08fdba MS |
1078 | ; |
1079 | ||
1080 | /* 14.2: Other template-arguments must be constant-expressions, | |
1081 | addresses of objects or functions with external linkage, or of | |
1082 | static class members. */ | |
bd6dd845 MS |
1083 | else if (IS_AGGR_TYPE (TREE_TYPE (val))) |
1084 | { | |
1085 | cp_error ("object `%E' cannot be used as template argument", arg); | |
1086 | val = error_mark_node; | |
1087 | } | |
8d08fdba MS |
1088 | else if (!TREE_CONSTANT (val)) |
1089 | { | |
1090 | cp_error ("non-const `%E' cannot be used as template argument", | |
1091 | arg); | |
1092 | val = error_mark_node; | |
1093 | } | |
f30432d7 MS |
1094 | else if (POINTER_TYPE_P (TREE_TYPE (val)) |
1095 | && ! integer_zerop (val) | |
1096 | && TREE_CODE (TREE_TYPE (TREE_TYPE (val))) != OFFSET_TYPE | |
1097 | && TREE_CODE (TREE_TYPE (TREE_TYPE (val))) != METHOD_TYPE) | |
8d08fdba | 1098 | { |
f30432d7 MS |
1099 | t = val; |
1100 | STRIP_NOPS (t); | |
1101 | if (TREE_CODE (t) == ADDR_EXPR) | |
1102 | { | |
1103 | tree a = TREE_OPERAND (t, 0); | |
1104 | STRIP_NOPS (a); | |
1105 | if (TREE_CODE (a) == STRING_CST) | |
1106 | { | |
1107 | cp_error ("string literal %E is not a valid template argument", a); | |
1108 | error ("because it is the address of an object with static linkage"); | |
1109 | val = error_mark_node; | |
1110 | } | |
1111 | else if (TREE_CODE (a) != VAR_DECL | |
1112 | && TREE_CODE (a) != FUNCTION_DECL) | |
1113 | goto bad; | |
893de33c | 1114 | else if (! TREE_PUBLIC (a)) |
f30432d7 MS |
1115 | { |
1116 | cp_error ("address of non-extern `%E' cannot be used as template argument", a); | |
1117 | val = error_mark_node; | |
1118 | } | |
1119 | } | |
1120 | else | |
8d08fdba | 1121 | { |
f30432d7 MS |
1122 | bad: |
1123 | cp_error ("`%E' is not a valid template argument", t); | |
1124 | error ("it must be %s%s with external linkage", | |
1125 | TREE_CODE (TREE_TYPE (val)) == POINTER_TYPE | |
1126 | ? "a pointer to " : "", | |
1127 | TREE_CODE (TREE_TYPE (TREE_TYPE (val))) == FUNCTION_TYPE | |
1128 | ? "a function" : "an object"); | |
8d08fdba MS |
1129 | val = error_mark_node; |
1130 | } | |
1131 | } | |
1132 | } | |
1133 | ||
1134 | if (val == error_mark_node) | |
1135 | lost++; | |
1136 | ||
1137 | TREE_VEC_ELT (vec, i) = val; | |
1138 | } | |
1139 | if (lost) | |
1140 | return error_mark_node; | |
1141 | return vec; | |
1142 | } | |
1143 | ||
bd6dd845 | 1144 | static int |
5566b478 MS |
1145 | comp_template_args (oldargs, newargs) |
1146 | tree oldargs, newargs; | |
1147 | { | |
1148 | int i; | |
1149 | ||
386b8a85 JM |
1150 | if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs)) |
1151 | return 0; | |
1152 | ||
5566b478 MS |
1153 | for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i) |
1154 | { | |
1155 | tree nt = TREE_VEC_ELT (newargs, i); | |
1156 | tree ot = TREE_VEC_ELT (oldargs, i); | |
1157 | ||
1158 | if (nt == ot) | |
1159 | continue; | |
1160 | if (TREE_CODE (nt) != TREE_CODE (ot)) | |
1161 | return 0; | |
67d743fe MS |
1162 | if (TREE_CODE_CLASS (TREE_CODE (ot)) == 't') |
1163 | { | |
1164 | if (comptypes (ot, nt, 1)) | |
1165 | continue; | |
1166 | } | |
1167 | else if (cp_tree_equal (ot, nt) > 0) | |
5566b478 MS |
1168 | continue; |
1169 | return 0; | |
1170 | } | |
1171 | return 1; | |
1172 | } | |
1173 | ||
8d08fdba MS |
1174 | /* Given class template name and parameter list, produce a user-friendly name |
1175 | for the instantiation. */ | |
e92cc029 | 1176 | |
8d08fdba MS |
1177 | static char * |
1178 | mangle_class_name_for_template (name, parms, arglist) | |
1179 | char *name; | |
1180 | tree parms, arglist; | |
1181 | { | |
1182 | static struct obstack scratch_obstack; | |
1183 | static char *scratch_firstobj; | |
1184 | int i, nparms; | |
8d08fdba MS |
1185 | |
1186 | if (!scratch_firstobj) | |
fc378698 | 1187 | gcc_obstack_init (&scratch_obstack); |
8d08fdba MS |
1188 | else |
1189 | obstack_free (&scratch_obstack, scratch_firstobj); | |
fc378698 | 1190 | scratch_firstobj = obstack_alloc (&scratch_obstack, 1); |
8d08fdba MS |
1191 | |
1192 | #if 0 | |
1193 | #define buflen sizeof(buf) | |
1194 | #define check if (bufp >= buf+buflen-1) goto too_long | |
1195 | #define ccat(c) *bufp++=(c); check | |
1196 | #define advance bufp+=strlen(bufp); check | |
1197 | #define cat(s) strncpy(bufp, s, buf+buflen-bufp-1); advance | |
1198 | #else | |
1199 | #define check | |
1200 | #define ccat(c) obstack_1grow (&scratch_obstack, (c)); | |
1201 | #define advance | |
1202 | #define cat(s) obstack_grow (&scratch_obstack, (s), strlen (s)) | |
1203 | #endif | |
8d08fdba MS |
1204 | |
1205 | cat (name); | |
1206 | ccat ('<'); | |
1207 | nparms = TREE_VEC_LENGTH (parms); | |
1208 | my_friendly_assert (nparms == TREE_VEC_LENGTH (arglist), 268); | |
1209 | for (i = 0; i < nparms; i++) | |
1210 | { | |
a292b002 MS |
1211 | tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i)); |
1212 | tree arg = TREE_VEC_ELT (arglist, i); | |
8d08fdba MS |
1213 | |
1214 | if (i) | |
1215 | ccat (','); | |
1216 | ||
a292b002 | 1217 | if (TREE_CODE (parm) == TYPE_DECL) |
8d08fdba MS |
1218 | { |
1219 | cat (type_as_string (arg, 0)); | |
1220 | continue; | |
1221 | } | |
1222 | else | |
1223 | my_friendly_assert (TREE_CODE (parm) == PARM_DECL, 269); | |
1224 | ||
1225 | if (TREE_CODE (arg) == TREE_LIST) | |
1226 | { | |
1227 | /* New list cell was built because old chain link was in | |
1228 | use. */ | |
1229 | my_friendly_assert (TREE_PURPOSE (arg) == NULL_TREE, 270); | |
1230 | arg = TREE_VALUE (arg); | |
1231 | } | |
1232 | /* No need to check arglist against parmlist here; we did that | |
1233 | in coerce_template_parms, called from lookup_template_class. */ | |
1234 | cat (expr_as_string (arg, 0)); | |
1235 | } | |
1236 | { | |
1237 | char *bufp = obstack_next_free (&scratch_obstack); | |
1238 | int offset = 0; | |
1239 | while (bufp[offset - 1] == ' ') | |
1240 | offset--; | |
1241 | obstack_blank_fast (&scratch_obstack, offset); | |
1242 | ||
1243 | /* B<C<char> >, not B<C<char>> */ | |
1244 | if (bufp[offset - 1] == '>') | |
1245 | ccat (' '); | |
1246 | } | |
1247 | ccat ('>'); | |
1248 | ccat ('\0'); | |
1249 | return (char *) obstack_base (&scratch_obstack); | |
1250 | ||
8926095f | 1251 | #if 0 |
8d08fdba | 1252 | too_long: |
8926095f | 1253 | #endif |
8d08fdba MS |
1254 | fatal ("out of (preallocated) string space creating template instantiation name"); |
1255 | /* NOTREACHED */ | |
1256 | return NULL; | |
1257 | } | |
1258 | ||
bd6dd845 | 1259 | static tree |
5566b478 MS |
1260 | classtype_mangled_name (t) |
1261 | tree t; | |
1262 | { | |
1263 | if (CLASSTYPE_TEMPLATE_INFO (t) | |
1264 | && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t))) | |
1265 | { | |
1266 | tree name = DECL_NAME (CLASSTYPE_TI_TEMPLATE (t)); | |
1267 | char *mangled_name = mangle_class_name_for_template | |
1268 | (IDENTIFIER_POINTER (name), | |
98c1c668 | 1269 | DECL_INNERMOST_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (t)), |
5566b478 MS |
1270 | CLASSTYPE_TI_ARGS (t)); |
1271 | tree id = get_identifier (mangled_name); | |
1272 | IDENTIFIER_TEMPLATE (id) = name; | |
1273 | return id; | |
1274 | } | |
1275 | else | |
1276 | return TYPE_IDENTIFIER (t); | |
1277 | } | |
1278 | ||
1279 | static void | |
1280 | add_pending_template (d) | |
1281 | tree d; | |
1282 | { | |
e92cc029 MS |
1283 | tree ti; |
1284 | ||
1285 | if (TREE_CODE_CLASS (TREE_CODE (d)) == 't') | |
1286 | ti = CLASSTYPE_TEMPLATE_INFO (d); | |
1287 | else | |
1288 | ti = DECL_TEMPLATE_INFO (d); | |
1289 | ||
824b9a4c | 1290 | if (TI_PENDING_TEMPLATE_FLAG (ti)) |
5566b478 MS |
1291 | return; |
1292 | ||
1293 | *template_tail = perm_tree_cons | |
1294 | (current_function_decl, d, NULL_TREE); | |
1295 | template_tail = &TREE_CHAIN (*template_tail); | |
824b9a4c | 1296 | TI_PENDING_TEMPLATE_FLAG (ti) = 1; |
5566b478 MS |
1297 | } |
1298 | ||
386b8a85 JM |
1299 | |
1300 | /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS (which | |
1301 | may be either a _DECL or an overloaded function or an | |
1302 | IDENTIFIER_NODE), and ARGLIST. */ | |
1303 | ||
1304 | tree | |
1305 | lookup_template_function (fns, arglist) | |
1306 | tree fns, arglist; | |
1307 | { | |
1308 | if (fns == NULL_TREE) | |
1309 | { | |
1310 | cp_error ("non-template used as template"); | |
1311 | return error_mark_node; | |
1312 | } | |
1313 | ||
1314 | if (arglist != NULL_TREE && !TREE_PERMANENT (arglist)) | |
1315 | { | |
1316 | push_obstacks (&permanent_obstack, &permanent_obstack); | |
1317 | arglist = copy_list (arglist); | |
1318 | pop_obstacks (); | |
1319 | } | |
1320 | ||
1321 | return build_min (TEMPLATE_ID_EXPR, | |
1322 | TREE_TYPE (fns) | |
1323 | ? TREE_TYPE (fns) : unknown_type_node, | |
1324 | fns, arglist); | |
1325 | } | |
1326 | ||
1327 | ||
8d08fdba MS |
1328 | /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of |
1329 | parameters, find the desired type. | |
1330 | ||
1331 | D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments. | |
1332 | Since ARGLIST is build on the decl_obstack, we must copy it here | |
1333 | to keep it from being reclaimed when the decl storage is reclaimed. | |
1334 | ||
1335 | IN_DECL, if non-NULL, is the template declaration we are trying to | |
1336 | instantiate. */ | |
e92cc029 | 1337 | |
8d08fdba MS |
1338 | tree |
1339 | lookup_template_class (d1, arglist, in_decl) | |
1340 | tree d1, arglist; | |
1341 | tree in_decl; | |
1342 | { | |
1343 | tree template, parmlist; | |
1344 | char *mangled_name; | |
5566b478 | 1345 | tree id, t; |
5566b478 MS |
1346 | |
1347 | if (TREE_CODE (d1) == IDENTIFIER_NODE) | |
1348 | { | |
1349 | template = IDENTIFIER_GLOBAL_VALUE (d1); /* XXX */ | |
1350 | if (! template) | |
1351 | template = IDENTIFIER_CLASS_VALUE (d1); | |
1352 | } | |
c91a56d2 MS |
1353 | else if (TREE_CODE (d1) == TYPE_DECL && IS_AGGR_TYPE (TREE_TYPE (d1))) |
1354 | { | |
1355 | template = CLASSTYPE_TI_TEMPLATE (TREE_TYPE (d1)); | |
1356 | d1 = DECL_NAME (template); | |
1357 | } | |
5566b478 MS |
1358 | else if (TREE_CODE_CLASS (TREE_CODE (d1)) == 't' && IS_AGGR_TYPE (d1)) |
1359 | { | |
1360 | template = CLASSTYPE_TI_TEMPLATE (d1); | |
1361 | d1 = DECL_NAME (template); | |
1362 | } | |
1363 | else | |
1364 | my_friendly_abort (272); | |
8d08fdba | 1365 | |
8d08fdba MS |
1366 | /* With something like `template <class T> class X class X { ... };' |
1367 | we could end up with D1 having nothing but an IDENTIFIER_LOCAL_VALUE. | |
1368 | We don't want to do that, but we have to deal with the situation, so | |
1369 | let's give them some syntax errors to chew on instead of a crash. */ | |
1370 | if (! template) | |
1371 | return error_mark_node; | |
1372 | if (TREE_CODE (template) != TEMPLATE_DECL) | |
1373 | { | |
1374 | cp_error ("non-template type `%T' used as a template", d1); | |
1375 | if (in_decl) | |
1376 | cp_error_at ("for template declaration `%D'", in_decl); | |
1377 | return error_mark_node; | |
1378 | } | |
8d08fdba | 1379 | |
5566b478 | 1380 | if (PRIMARY_TEMPLATE_P (template)) |
8d08fdba | 1381 | { |
98c1c668 | 1382 | parmlist = DECL_INNERMOST_TEMPLATE_PARMS (template); |
5566b478 MS |
1383 | |
1384 | arglist = coerce_template_parms (parmlist, arglist, template); | |
1385 | if (arglist == error_mark_node) | |
1386 | return error_mark_node; | |
1387 | if (uses_template_parms (arglist)) | |
1388 | { | |
1389 | tree found; | |
1390 | if (comp_template_args | |
1391 | (CLASSTYPE_TI_ARGS (TREE_TYPE (template)), arglist)) | |
1392 | found = TREE_TYPE (template); | |
1393 | else | |
1394 | { | |
1395 | for (found = DECL_TEMPLATE_INSTANTIATIONS (template); | |
1396 | found; found = TREE_CHAIN (found)) | |
1397 | { | |
1398 | if (TI_USES_TEMPLATE_PARMS (found) | |
1399 | && comp_template_args (TREE_PURPOSE (found), arglist)) | |
1400 | break; | |
1401 | } | |
1402 | if (found) | |
1403 | found = TREE_VALUE (found); | |
1404 | } | |
1405 | ||
1406 | if (found) | |
1407 | { | |
1408 | if (can_free (&permanent_obstack, arglist)) | |
1409 | obstack_free (&permanent_obstack, arglist); | |
1410 | return found; | |
1411 | } | |
1412 | } | |
1413 | ||
8d08fdba MS |
1414 | mangled_name = mangle_class_name_for_template (IDENTIFIER_POINTER (d1), |
1415 | parmlist, arglist); | |
1416 | id = get_identifier (mangled_name); | |
5566b478 | 1417 | IDENTIFIER_TEMPLATE (id) = d1; |
8d08fdba | 1418 | |
5566b478 | 1419 | maybe_push_to_top_level (uses_template_parms (arglist)); |
fc378698 | 1420 | t = xref_tag_from_type (TREE_TYPE (template), id, 1); |
5566b478 | 1421 | pop_from_top_level (); |
8926095f | 1422 | } |
5566b478 | 1423 | else |
8d08fdba | 1424 | { |
5566b478 MS |
1425 | tree ctx = lookup_template_class (TYPE_CONTEXT (TREE_TYPE (template)), |
1426 | arglist, in_decl); | |
1427 | id = d1; | |
1428 | arglist = CLASSTYPE_TI_ARGS (ctx); | |
8d08fdba | 1429 | |
5566b478 | 1430 | if (TYPE_BEING_DEFINED (ctx) && ctx == current_class_type) |
8d08fdba | 1431 | { |
5156628f MS |
1432 | int save_temp = processing_template_decl; |
1433 | processing_template_decl = 0; | |
fc378698 | 1434 | t = xref_tag_from_type (TREE_TYPE (template), id, 0); |
5156628f | 1435 | processing_template_decl = save_temp; |
8d08fdba MS |
1436 | } |
1437 | else | |
1438 | { | |
5566b478 MS |
1439 | t = lookup_nested_type_by_name (ctx, id); |
1440 | my_friendly_assert (t != NULL_TREE, 42); | |
8d08fdba | 1441 | } |
5566b478 MS |
1442 | } |
1443 | ||
e92cc029 | 1444 | /* Seems to be wanted. */ |
5566b478 MS |
1445 | CLASSTYPE_GOT_SEMICOLON (t) = 1; |
1446 | ||
1447 | if (! CLASSTYPE_TEMPLATE_INFO (t)) | |
1448 | { | |
1449 | arglist = copy_to_permanent (arglist); | |
1450 | CLASSTYPE_TEMPLATE_INFO (t) | |
1451 | = perm_tree_cons (template, arglist, NULL_TREE); | |
1452 | DECL_TEMPLATE_INSTANTIATIONS (template) = perm_tree_cons | |
1453 | (arglist, t, DECL_TEMPLATE_INSTANTIATIONS (template)); | |
1454 | TI_USES_TEMPLATE_PARMS (DECL_TEMPLATE_INSTANTIATIONS (template)) | |
1455 | = uses_template_parms (arglist); | |
1456 | ||
1457 | SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t); | |
1458 | ||
e92cc029 | 1459 | /* We need to set this again after CLASSTYPE_TEMPLATE_INFO is set up. */ |
5566b478 | 1460 | DECL_ASSEMBLER_NAME (TYPE_MAIN_DECL (t)) = id; |
386b8a85 | 1461 | /* if (! uses_template_parms (arglist)) */ |
5566b478 MS |
1462 | DECL_ASSEMBLER_NAME (TYPE_MAIN_DECL (t)) |
1463 | = get_identifier (build_overload_name (t, 1, 1)); | |
1464 | ||
1465 | if (flag_external_templates && ! uses_template_parms (arglist) | |
1466 | && CLASSTYPE_INTERFACE_KNOWN (TREE_TYPE (template)) | |
1467 | && ! CLASSTYPE_INTERFACE_ONLY (TREE_TYPE (template))) | |
e92cc029 | 1468 | add_pending_template (t); |
8d08fdba | 1469 | } |
8d08fdba | 1470 | |
5566b478 | 1471 | return t; |
8d08fdba MS |
1472 | } |
1473 | \f | |
51c184be | 1474 | /* Should be defined in parse.h. */ |
8d08fdba MS |
1475 | extern int yychar; |
1476 | ||
1477 | int | |
1478 | uses_template_parms (t) | |
1479 | tree t; | |
1480 | { | |
1481 | if (!t) | |
1482 | return 0; | |
1483 | switch (TREE_CODE (t)) | |
1484 | { | |
1485 | case INDIRECT_REF: | |
1486 | case COMPONENT_REF: | |
1487 | /* We assume that the object must be instantiated in order to build | |
1488 | the COMPONENT_REF, so we test only whether the type of the | |
1489 | COMPONENT_REF uses template parms. */ | |
1490 | return uses_template_parms (TREE_TYPE (t)); | |
1491 | ||
1492 | case IDENTIFIER_NODE: | |
1493 | if (!IDENTIFIER_TEMPLATE (t)) | |
1494 | return 0; | |
5566b478 | 1495 | my_friendly_abort (42); |
8d08fdba MS |
1496 | |
1497 | /* aggregates of tree nodes */ | |
1498 | case TREE_VEC: | |
1499 | { | |
1500 | int i = TREE_VEC_LENGTH (t); | |
1501 | while (i--) | |
1502 | if (uses_template_parms (TREE_VEC_ELT (t, i))) | |
1503 | return 1; | |
1504 | return 0; | |
1505 | } | |
1506 | case TREE_LIST: | |
1507 | if (uses_template_parms (TREE_PURPOSE (t)) | |
1508 | || uses_template_parms (TREE_VALUE (t))) | |
1509 | return 1; | |
1510 | return uses_template_parms (TREE_CHAIN (t)); | |
1511 | ||
1512 | /* constructed type nodes */ | |
1513 | case POINTER_TYPE: | |
1514 | case REFERENCE_TYPE: | |
1515 | return uses_template_parms (TREE_TYPE (t)); | |
1516 | case RECORD_TYPE: | |
b7484fbe MS |
1517 | if (TYPE_PTRMEMFUNC_FLAG (t)) |
1518 | return uses_template_parms (TYPE_PTRMEMFUNC_FN_TYPE (t)); | |
8d08fdba | 1519 | case UNION_TYPE: |
5566b478 | 1520 | if (! CLASSTYPE_TEMPLATE_INFO (t)) |
8d08fdba | 1521 | return 0; |
5566b478 | 1522 | return uses_template_parms (TREE_VALUE (CLASSTYPE_TEMPLATE_INFO (t))); |
8d08fdba MS |
1523 | case FUNCTION_TYPE: |
1524 | if (uses_template_parms (TYPE_ARG_TYPES (t))) | |
1525 | return 1; | |
1526 | return uses_template_parms (TREE_TYPE (t)); | |
1527 | case ARRAY_TYPE: | |
1528 | if (uses_template_parms (TYPE_DOMAIN (t))) | |
1529 | return 1; | |
1530 | return uses_template_parms (TREE_TYPE (t)); | |
1531 | case OFFSET_TYPE: | |
1532 | if (uses_template_parms (TYPE_OFFSET_BASETYPE (t))) | |
1533 | return 1; | |
1534 | return uses_template_parms (TREE_TYPE (t)); | |
1535 | case METHOD_TYPE: | |
75b0bbce | 1536 | if (uses_template_parms (TYPE_METHOD_BASETYPE (t))) |
8d08fdba MS |
1537 | return 1; |
1538 | if (uses_template_parms (TYPE_ARG_TYPES (t))) | |
1539 | return 1; | |
1540 | return uses_template_parms (TREE_TYPE (t)); | |
1541 | ||
1542 | /* decl nodes */ | |
1543 | case TYPE_DECL: | |
5566b478 MS |
1544 | return uses_template_parms (TREE_TYPE (t)); |
1545 | ||
8d08fdba | 1546 | case FUNCTION_DECL: |
5566b478 MS |
1547 | case VAR_DECL: |
1548 | /* ??? What about FIELD_DECLs? */ | |
1549 | if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t) | |
1550 | && uses_template_parms (DECL_TI_ARGS (t))) | |
8d08fdba MS |
1551 | return 1; |
1552 | /* fall through */ | |
5566b478 | 1553 | case CONST_DECL: |
8d08fdba | 1554 | case PARM_DECL: |
5566b478 MS |
1555 | if (uses_template_parms (TREE_TYPE (t))) |
1556 | return 1; | |
8d08fdba MS |
1557 | if (DECL_CONTEXT (t) && uses_template_parms (DECL_CONTEXT (t))) |
1558 | return 1; | |
8d08fdba MS |
1559 | return 0; |
1560 | ||
1561 | case CALL_EXPR: | |
1562 | return uses_template_parms (TREE_TYPE (t)); | |
1563 | case ADDR_EXPR: | |
1564 | return uses_template_parms (TREE_OPERAND (t, 0)); | |
1565 | ||
1566 | /* template parm nodes */ | |
1567 | case TEMPLATE_TYPE_PARM: | |
1568 | case TEMPLATE_CONST_PARM: | |
1569 | return 1; | |
1570 | ||
1571 | /* simple type nodes */ | |
1572 | case INTEGER_TYPE: | |
1573 | if (uses_template_parms (TYPE_MIN_VALUE (t))) | |
1574 | return 1; | |
1575 | return uses_template_parms (TYPE_MAX_VALUE (t)); | |
1576 | ||
1577 | case REAL_TYPE: | |
37c46b43 | 1578 | case COMPLEX_TYPE: |
8d08fdba | 1579 | case VOID_TYPE: |
2986ae00 | 1580 | case BOOLEAN_TYPE: |
8d08fdba MS |
1581 | return 0; |
1582 | ||
85b71cf2 JM |
1583 | case ENUMERAL_TYPE: |
1584 | { | |
1585 | tree v; | |
1586 | ||
1587 | for (v = TYPE_VALUES (t); v != NULL_TREE; v = TREE_CHAIN (v)) | |
1588 | if (uses_template_parms (TREE_VALUE (v))) | |
1589 | return 1; | |
1590 | } | |
1591 | return 0; | |
1592 | ||
8d08fdba MS |
1593 | /* constants */ |
1594 | case INTEGER_CST: | |
1595 | case REAL_CST: | |
1596 | case STRING_CST: | |
1597 | return 0; | |
1598 | ||
1599 | case ERROR_MARK: | |
1600 | /* Non-error_mark_node ERROR_MARKs are bad things. */ | |
1601 | my_friendly_assert (t == error_mark_node, 274); | |
1602 | /* NOTREACHED */ | |
1603 | return 0; | |
1604 | ||
ec255269 | 1605 | case LOOKUP_EXPR: |
5566b478 | 1606 | case TYPENAME_TYPE: |
8d08fdba MS |
1607 | return 1; |
1608 | ||
5156628f MS |
1609 | case SCOPE_REF: |
1610 | return uses_template_parms (TREE_OPERAND (t, 0)); | |
1611 | ||
db5ae43f MS |
1612 | case CONSTRUCTOR: |
1613 | if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))) | |
1614 | return uses_template_parms (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t))); | |
5156628f | 1615 | return uses_template_parms (TREE_OPERAND (t, 1)); |
db5ae43f | 1616 | |
42976354 BK |
1617 | case MODOP_EXPR: |
1618 | case CAST_EXPR: | |
1619 | case REINTERPRET_CAST_EXPR: | |
1620 | case CONST_CAST_EXPR: | |
1621 | case STATIC_CAST_EXPR: | |
1622 | case DYNAMIC_CAST_EXPR: | |
1623 | case SIZEOF_EXPR: | |
1624 | case ARROW_EXPR: | |
1625 | case DOTSTAR_EXPR: | |
1626 | case TYPEID_EXPR: | |
1627 | return 1; | |
1628 | ||
8d08fdba MS |
1629 | default: |
1630 | switch (TREE_CODE_CLASS (TREE_CODE (t))) | |
1631 | { | |
1632 | case '1': | |
1633 | case '2': | |
ec255269 | 1634 | case 'e': |
8d08fdba MS |
1635 | case '<': |
1636 | { | |
1637 | int i; | |
1638 | for (i = tree_code_length[(int) TREE_CODE (t)]; --i >= 0;) | |
1639 | if (uses_template_parms (TREE_OPERAND (t, i))) | |
1640 | return 1; | |
1641 | return 0; | |
1642 | } | |
1643 | default: | |
1644 | break; | |
1645 | } | |
1646 | sorry ("testing %s for template parms", | |
1647 | tree_code_name [(int) TREE_CODE (t)]); | |
1648 | my_friendly_abort (82); | |
1649 | /* NOTREACHED */ | |
1650 | return 0; | |
1651 | } | |
1652 | } | |
1653 | ||
7215f9a0 MS |
1654 | static struct tinst_level *current_tinst_level = 0; |
1655 | static struct tinst_level *free_tinst_level = 0; | |
1656 | static int tinst_depth = 0; | |
e9f32eb5 | 1657 | extern int max_tinst_depth; |
5566b478 MS |
1658 | #ifdef GATHER_STATISTICS |
1659 | int depth_reached = 0; | |
1660 | #endif | |
8d08fdba | 1661 | |
bd6dd845 | 1662 | static int |
5566b478 MS |
1663 | push_tinst_level (d) |
1664 | tree d; | |
8d08fdba MS |
1665 | { |
1666 | struct tinst_level *new; | |
8d08fdba | 1667 | |
7215f9a0 MS |
1668 | if (tinst_depth >= max_tinst_depth) |
1669 | { | |
5566b478 MS |
1670 | struct tinst_level *p = current_tinst_level; |
1671 | int line = lineno; | |
1672 | char *file = input_filename; | |
1673 | ||
7215f9a0 MS |
1674 | error ("template instantiation depth exceeds maximum of %d", |
1675 | max_tinst_depth); | |
e9f32eb5 | 1676 | error (" (use -ftemplate-depth-NN to increase the maximum)"); |
5566b478 MS |
1677 | cp_error (" instantiating `%D'", d); |
1678 | ||
1679 | for (; p; p = p->next) | |
1680 | { | |
1681 | cp_error (" instantiated from `%D'", p->decl); | |
1682 | lineno = p->line; | |
1683 | input_filename = p->file; | |
1684 | } | |
1685 | error (" instantiated from here"); | |
1686 | ||
1687 | lineno = line; | |
1688 | input_filename = file; | |
1689 | ||
7215f9a0 MS |
1690 | return 0; |
1691 | } | |
1692 | ||
8d08fdba MS |
1693 | if (free_tinst_level) |
1694 | { | |
1695 | new = free_tinst_level; | |
1696 | free_tinst_level = new->next; | |
1697 | } | |
1698 | else | |
1699 | new = (struct tinst_level *) xmalloc (sizeof (struct tinst_level)); | |
1700 | ||
5566b478 MS |
1701 | new->decl = d; |
1702 | new->line = lineno; | |
1703 | new->file = input_filename; | |
8d08fdba MS |
1704 | new->next = current_tinst_level; |
1705 | current_tinst_level = new; | |
5566b478 | 1706 | |
7215f9a0 | 1707 | ++tinst_depth; |
5566b478 MS |
1708 | #ifdef GATHER_STATISTICS |
1709 | if (tinst_depth > depth_reached) | |
1710 | depth_reached = tinst_depth; | |
1711 | #endif | |
1712 | ||
7215f9a0 | 1713 | return 1; |
8d08fdba MS |
1714 | } |
1715 | ||
1716 | void | |
1717 | pop_tinst_level () | |
1718 | { | |
1719 | struct tinst_level *old = current_tinst_level; | |
1720 | ||
1721 | current_tinst_level = old->next; | |
1722 | old->next = free_tinst_level; | |
1723 | free_tinst_level = old; | |
7215f9a0 | 1724 | --tinst_depth; |
8d08fdba MS |
1725 | } |
1726 | ||
1727 | struct tinst_level * | |
1728 | tinst_for_decl () | |
1729 | { | |
1730 | struct tinst_level *p = current_tinst_level; | |
1731 | ||
1732 | if (p) | |
1733 | for (; p->next ; p = p->next ) | |
1734 | ; | |
1735 | return p; | |
1736 | } | |
1737 | ||
1738 | tree | |
5566b478 MS |
1739 | instantiate_class_template (type) |
1740 | tree type; | |
8d08fdba | 1741 | { |
fc378698 | 1742 | tree template, template_info, args, pattern, t, *field_chain; |
8d08fdba | 1743 | |
5566b478 | 1744 | if (type == error_mark_node) |
8d08fdba MS |
1745 | return error_mark_node; |
1746 | ||
5566b478 MS |
1747 | template_info = CLASSTYPE_TEMPLATE_INFO (type); |
1748 | ||
1749 | if (TYPE_BEING_DEFINED (type) || TYPE_SIZE (type)) | |
1750 | return type; | |
1751 | ||
1752 | template = TI_TEMPLATE (template_info); | |
1753 | my_friendly_assert (TREE_CODE (template) == TEMPLATE_DECL, 279); | |
1754 | args = TI_ARGS (template_info); | |
73aad9b9 JM |
1755 | |
1756 | t = most_specialized_class | |
1757 | (DECL_TEMPLATE_SPECIALIZATIONS (template), args); | |
1758 | ||
1759 | if (t == error_mark_node) | |
1760 | { | |
1761 | char *str = "candidates are:"; | |
1762 | cp_error ("ambiguous class template instantiation for `%#T'", type); | |
1763 | for (t = DECL_TEMPLATE_SPECIALIZATIONS (template); t; t = TREE_CHAIN (t)) | |
1764 | { | |
1765 | if (get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t), args)) | |
1766 | { | |
1767 | cp_error_at ("%s %+#T", str, TREE_TYPE (t)); | |
1768 | str = " "; | |
1769 | } | |
1770 | } | |
1771 | TYPE_BEING_DEFINED (type) = 1; | |
de22184b | 1772 | return error_mark_node; |
73aad9b9 JM |
1773 | } |
1774 | else if (t) | |
1775 | pattern = TREE_TYPE (t); | |
1776 | else | |
1777 | pattern = TREE_TYPE (template); | |
5566b478 MS |
1778 | |
1779 | if (TYPE_SIZE (pattern) == NULL_TREE) | |
ec255269 | 1780 | return type; |
5566b478 | 1781 | |
73aad9b9 JM |
1782 | if (t) |
1783 | args = get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t), args); | |
1784 | ||
5566b478 MS |
1785 | TYPE_BEING_DEFINED (type) = 1; |
1786 | ||
1787 | if (! push_tinst_level (type)) | |
1788 | return type; | |
8d08fdba | 1789 | |
5566b478 MS |
1790 | maybe_push_to_top_level (uses_template_parms (type)); |
1791 | pushclass (type, 0); | |
1792 | ||
1793 | if (flag_external_templates) | |
1794 | { | |
1795 | if (flag_alt_external_templates) | |
1796 | { | |
1797 | CLASSTYPE_INTERFACE_ONLY (type) = interface_only; | |
1798 | SET_CLASSTYPE_INTERFACE_UNKNOWN_X (type, interface_unknown); | |
1799 | CLASSTYPE_VTABLE_NEEDS_WRITING (type) | |
1800 | = ! CLASSTYPE_INTERFACE_ONLY (type) | |
1801 | && CLASSTYPE_INTERFACE_KNOWN (type); | |
1802 | } | |
1803 | else | |
1804 | { | |
1805 | CLASSTYPE_INTERFACE_ONLY (type) = CLASSTYPE_INTERFACE_ONLY (pattern); | |
1806 | SET_CLASSTYPE_INTERFACE_UNKNOWN_X | |
1807 | (type, CLASSTYPE_INTERFACE_UNKNOWN (pattern)); | |
1808 | CLASSTYPE_VTABLE_NEEDS_WRITING (type) | |
1809 | = ! CLASSTYPE_INTERFACE_ONLY (type) | |
1810 | && CLASSTYPE_INTERFACE_KNOWN (type); | |
1811 | } | |
1812 | } | |
1813 | else | |
8d08fdba | 1814 | { |
5566b478 MS |
1815 | SET_CLASSTYPE_INTERFACE_UNKNOWN (type); |
1816 | CLASSTYPE_VTABLE_NEEDS_WRITING (type) = 1; | |
8d08fdba MS |
1817 | } |
1818 | ||
f7da6097 MS |
1819 | TYPE_HAS_CONSTRUCTOR (type) = TYPE_HAS_CONSTRUCTOR (pattern); |
1820 | TYPE_HAS_DESTRUCTOR (type) = TYPE_HAS_DESTRUCTOR (pattern); | |
1821 | TYPE_HAS_ASSIGNMENT (type) = TYPE_HAS_ASSIGNMENT (pattern); | |
1822 | TYPE_OVERLOADS_CALL_EXPR (type) = TYPE_OVERLOADS_CALL_EXPR (pattern); | |
1823 | TYPE_OVERLOADS_ARRAY_REF (type) = TYPE_OVERLOADS_ARRAY_REF (pattern); | |
1824 | TYPE_OVERLOADS_ARROW (type) = TYPE_OVERLOADS_ARROW (pattern); | |
1825 | TYPE_GETS_NEW (type) = TYPE_GETS_NEW (pattern); | |
1826 | TYPE_GETS_DELETE (type) = TYPE_GETS_DELETE (pattern); | |
1827 | TYPE_VEC_DELETE_TAKES_SIZE (type) = TYPE_VEC_DELETE_TAKES_SIZE (pattern); | |
1828 | TYPE_HAS_ASSIGN_REF (type) = TYPE_HAS_ASSIGN_REF (pattern); | |
1829 | TYPE_HAS_CONST_ASSIGN_REF (type) = TYPE_HAS_CONST_ASSIGN_REF (pattern); | |
1830 | TYPE_HAS_ABSTRACT_ASSIGN_REF (type) = TYPE_HAS_ABSTRACT_ASSIGN_REF (pattern); | |
1831 | TYPE_HAS_INIT_REF (type) = TYPE_HAS_INIT_REF (pattern); | |
1832 | TYPE_HAS_CONST_INIT_REF (type) = TYPE_HAS_CONST_INIT_REF (pattern); | |
1833 | TYPE_GETS_INIT_AGGR (type) = TYPE_GETS_INIT_AGGR (pattern); | |
1834 | TYPE_HAS_DEFAULT_CONSTRUCTOR (type) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern); | |
1835 | TYPE_HAS_CONVERSION (type) = TYPE_HAS_CONVERSION (pattern); | |
1836 | TYPE_USES_COMPLEX_INHERITANCE (type) | |
1837 | = TYPE_USES_COMPLEX_INHERITANCE (pattern); | |
1838 | TYPE_USES_MULTIPLE_INHERITANCE (type) | |
1839 | = TYPE_USES_MULTIPLE_INHERITANCE (pattern); | |
1840 | TYPE_USES_VIRTUAL_BASECLASSES (type) | |
1841 | = TYPE_USES_VIRTUAL_BASECLASSES (pattern); | |
1842 | TYPE_PACKED (type) = TYPE_PACKED (pattern); | |
1843 | TYPE_ALIGN (type) = TYPE_ALIGN (pattern); | |
1844 | ||
5566b478 MS |
1845 | { |
1846 | tree binfo = TYPE_BINFO (type); | |
1847 | tree pbases = TYPE_BINFO_BASETYPES (pattern); | |
1848 | ||
1849 | if (pbases) | |
1850 | { | |
1851 | tree bases; | |
1852 | int i; | |
1853 | int len = TREE_VEC_LENGTH (pbases); | |
de22184b | 1854 | bases = make_tree_vec (len); |
5566b478 MS |
1855 | for (i = 0; i < len; ++i) |
1856 | { | |
1857 | tree elt; | |
1858 | ||
1859 | TREE_VEC_ELT (bases, i) = elt | |
98c1c668 | 1860 | = tsubst (TREE_VEC_ELT (pbases, i), args, |
5566b478 MS |
1861 | TREE_VEC_LENGTH (args), NULL_TREE); |
1862 | BINFO_INHERITANCE_CHAIN (elt) = binfo; | |
1863 | ||
6633d636 MS |
1864 | if (! IS_AGGR_TYPE (TREE_TYPE (elt))) |
1865 | cp_error | |
1866 | ("base type `%T' of `%T' fails to be a struct or class type", | |
1867 | TREE_TYPE (elt), type); | |
1868 | else if (! uses_template_parms (type) | |
1869 | && (TYPE_SIZE (complete_type (TREE_TYPE (elt))) | |
1870 | == NULL_TREE)) | |
5566b478 MS |
1871 | cp_error ("base class `%T' of `%T' has incomplete type", |
1872 | TREE_TYPE (elt), type); | |
1873 | } | |
de22184b MS |
1874 | /* Don't initialize this until the vector is filled out, or |
1875 | lookups will crash. */ | |
1876 | BINFO_BASETYPES (binfo) = bases; | |
5566b478 MS |
1877 | } |
1878 | } | |
1879 | ||
1880 | CLASSTYPE_LOCAL_TYPEDECLS (type) = CLASSTYPE_LOCAL_TYPEDECLS (pattern); | |
1881 | ||
1882 | field_chain = &TYPE_FIELDS (type); | |
5566b478 MS |
1883 | |
1884 | for (t = CLASSTYPE_TAGS (pattern); t; t = TREE_CHAIN (t)) | |
8d08fdba | 1885 | { |
5566b478 MS |
1886 | tree name = TREE_PURPOSE (t); |
1887 | tree tag = TREE_VALUE (t); | |
5566b478 | 1888 | |
fc378698 | 1889 | /* These will add themselves to CLASSTYPE_TAGS for the new type. */ |
5566b478 | 1890 | if (TREE_CODE (tag) == ENUMERAL_TYPE) |
8d08fdba | 1891 | { |
98c1c668 | 1892 | tree e, newtag = tsubst_enum (tag, args, |
b3d5a58b | 1893 | TREE_VEC_LENGTH (args), field_chain); |
5566b478 | 1894 | |
5566b478 | 1895 | while (*field_chain) |
37c46b43 MS |
1896 | { |
1897 | DECL_FIELD_CONTEXT (*field_chain) = type; | |
1898 | field_chain = &TREE_CHAIN (*field_chain); | |
1899 | } | |
8d08fdba | 1900 | } |
b87692e5 | 1901 | else |
98c1c668 | 1902 | tsubst (tag, args, |
b87692e5 | 1903 | TREE_VEC_LENGTH (args), NULL_TREE); |
8d08fdba MS |
1904 | } |
1905 | ||
5566b478 MS |
1906 | /* Don't replace enum constants here. */ |
1907 | for (t = TYPE_FIELDS (pattern); t; t = TREE_CHAIN (t)) | |
1908 | if (TREE_CODE (t) != CONST_DECL) | |
1909 | { | |
98c1c668 | 1910 | tree r = tsubst (t, args, |
5566b478 MS |
1911 | TREE_VEC_LENGTH (args), NULL_TREE); |
1912 | if (TREE_CODE (r) == VAR_DECL) | |
1913 | { | |
1914 | if (! uses_template_parms (r)) | |
1915 | pending_statics = perm_tree_cons (NULL_TREE, r, pending_statics); | |
e92cc029 | 1916 | /* Perhaps I should do more of grokfield here. */ |
5566b478 MS |
1917 | start_decl_1 (r); |
1918 | DECL_IN_AGGR_P (r) = 1; | |
1919 | DECL_EXTERNAL (r) = 1; | |
1920 | cp_finish_decl (r, DECL_INITIAL (r), NULL_TREE, 0, 0); | |
1921 | } | |
1922 | ||
1923 | *field_chain = r; | |
1924 | field_chain = &TREE_CHAIN (r); | |
1925 | } | |
8d08fdba | 1926 | |
5566b478 | 1927 | TYPE_METHODS (type) = tsubst_chain (TYPE_METHODS (pattern), args); |
f7da6097 MS |
1928 | for (t = TYPE_METHODS (type); t; t = TREE_CHAIN (t)) |
1929 | { | |
1930 | if (DECL_CONSTRUCTOR_P (t)) | |
1931 | grok_ctor_properties (type, t); | |
1932 | else if (IDENTIFIER_OPNAME_P (DECL_NAME (t))) | |
1933 | grok_op_properties (t, DECL_VIRTUAL_P (t), 0); | |
1934 | } | |
8d08fdba | 1935 | |
5566b478 | 1936 | DECL_FRIENDLIST (TYPE_MAIN_DECL (type)) |
fc378698 | 1937 | = tsubst (DECL_FRIENDLIST (TYPE_MAIN_DECL (pattern)), |
98c1c668 | 1938 | args, TREE_VEC_LENGTH (args), NULL_TREE); |
5566b478 MS |
1939 | |
1940 | { | |
beb53fb8 | 1941 | tree d = CLASSTYPE_FRIEND_CLASSES (type) |
98c1c668 | 1942 | = tsubst (CLASSTYPE_FRIEND_CLASSES (pattern), args, |
beb53fb8 | 1943 | TREE_VEC_LENGTH (args), NULL_TREE); |
fc378698 MS |
1944 | |
1945 | /* This does injection for friend classes. */ | |
1946 | for (; d; d = TREE_CHAIN (d)) | |
1947 | TREE_VALUE (d) = xref_tag_from_type (TREE_VALUE (d), NULL_TREE, 1); | |
1948 | ||
98c1c668 | 1949 | d = tsubst (DECL_TEMPLATE_INJECT (template), args, |
056c014d | 1950 | TREE_VEC_LENGTH (args), NULL_TREE); |
5566b478 MS |
1951 | |
1952 | for (; d; d = TREE_CHAIN (d)) | |
fc378698 MS |
1953 | { |
1954 | tree t = TREE_VALUE (d); | |
1955 | ||
1956 | if (TREE_CODE (t) == TYPE_DECL) | |
1957 | /* Already injected. */; | |
1958 | else | |
1959 | pushdecl (t); | |
1960 | } | |
5566b478 MS |
1961 | } |
1962 | ||
5566b478 | 1963 | if (! uses_template_parms (type)) |
8d08fdba | 1964 | { |
5566b478 MS |
1965 | tree tmp; |
1966 | for (tmp = TYPE_FIELDS (type); tmp; tmp = TREE_CHAIN (tmp)) | |
ce122a86 | 1967 | if (TREE_CODE (tmp) == FIELD_DECL) |
c73964b2 MS |
1968 | { |
1969 | TREE_TYPE (tmp) = complete_type (TREE_TYPE (tmp)); | |
1970 | require_complete_type (tmp); | |
1971 | } | |
5566b478 | 1972 | |
6467930b | 1973 | type = finish_struct_1 (type, 0); |
e349ee73 | 1974 | CLASSTYPE_GOT_SEMICOLON (type) = 1; |
e92cc029 MS |
1975 | |
1976 | repo_template_used (type); | |
fe4e8851 JM |
1977 | if (at_eof && TYPE_BINFO_VTABLE (type) != NULL_TREE) |
1978 | finish_prevtable_vardecl (NULL, TYPE_BINFO_VTABLE (type)); | |
8d08fdba MS |
1979 | } |
1980 | else | |
1981 | { | |
5566b478 MS |
1982 | TYPE_SIZE (type) = integer_zero_node; |
1983 | CLASSTYPE_METHOD_VEC (type) | |
1984 | = finish_struct_methods (type, TYPE_METHODS (type), 1); | |
8d08fdba MS |
1985 | } |
1986 | ||
5566b478 MS |
1987 | TYPE_BEING_DEFINED (type) = 0; |
1988 | popclass (0); | |
1989 | ||
1990 | pop_from_top_level (); | |
1991 | pop_tinst_level (); | |
1992 | ||
1993 | return type; | |
8d08fdba MS |
1994 | } |
1995 | ||
1996 | static int | |
1997 | list_eq (t1, t2) | |
1998 | tree t1, t2; | |
1999 | { | |
2000 | if (t1 == NULL_TREE) | |
2001 | return t2 == NULL_TREE; | |
2002 | if (t2 == NULL_TREE) | |
2003 | return 0; | |
2004 | /* Don't care if one declares its arg const and the other doesn't -- the | |
2005 | main variant of the arg type is all that matters. */ | |
2006 | if (TYPE_MAIN_VARIANT (TREE_VALUE (t1)) | |
2007 | != TYPE_MAIN_VARIANT (TREE_VALUE (t2))) | |
2008 | return 0; | |
2009 | return list_eq (TREE_CHAIN (t1), TREE_CHAIN (t2)); | |
2010 | } | |
2011 | ||
5566b478 | 2012 | tree |
8d08fdba MS |
2013 | lookup_nested_type_by_name (ctype, name) |
2014 | tree ctype, name; | |
2015 | { | |
2016 | tree t; | |
2017 | ||
5566b478 MS |
2018 | complete_type (ctype); |
2019 | ||
db5ae43f MS |
2020 | for (t = CLASSTYPE_TAGS (ctype); t; t = TREE_CHAIN (t)) |
2021 | { | |
f017f649 JM |
2022 | if (name == TREE_PURPOSE (t) |
2023 | /* this catches typedef enum { foo } bar; */ | |
2024 | || name == TYPE_IDENTIFIER (TREE_VALUE (t))) | |
db5ae43f MS |
2025 | return TREE_VALUE (t); |
2026 | } | |
8d08fdba MS |
2027 | return NULL_TREE; |
2028 | } | |
2029 | ||
75b0bbce | 2030 | tree |
8d08fdba | 2031 | tsubst (t, args, nargs, in_decl) |
98c1c668 | 2032 | tree t, args; |
8d08fdba MS |
2033 | int nargs; |
2034 | tree in_decl; | |
2035 | { | |
2036 | tree type; | |
2037 | ||
5566b478 MS |
2038 | if (t == NULL_TREE || t == error_mark_node |
2039 | || t == integer_type_node | |
2040 | || t == void_type_node | |
2041 | || t == char_type_node) | |
8d08fdba MS |
2042 | return t; |
2043 | ||
2044 | type = TREE_TYPE (t); | |
5566b478 MS |
2045 | if (type == unknown_type_node) |
2046 | my_friendly_abort (42); | |
824b9a4c MS |
2047 | if (type && TREE_CODE (t) != FUNCTION_DECL |
2048 | && TREE_CODE (t) != TYPENAME_TYPE) | |
b7484fbe MS |
2049 | type = tsubst (type, args, nargs, in_decl); |
2050 | ||
8d08fdba MS |
2051 | switch (TREE_CODE (t)) |
2052 | { | |
2053 | case RECORD_TYPE: | |
2054 | if (TYPE_PTRMEMFUNC_P (t)) | |
5566b478 MS |
2055 | { |
2056 | tree r = build_ptrmemfunc_type | |
2057 | (tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, nargs, in_decl)); | |
2058 | return cp_build_type_variant (r, TYPE_READONLY (t), | |
2059 | TYPE_VOLATILE (t)); | |
2060 | } | |
2061 | ||
8d08fdba | 2062 | /* else fall through */ |
5566b478 MS |
2063 | case UNION_TYPE: |
2064 | if (uses_template_parms (t)) | |
2065 | { | |
2066 | tree argvec = tsubst (CLASSTYPE_TI_ARGS (t), args, nargs, in_decl); | |
2067 | tree r = lookup_template_class (t, argvec, in_decl); | |
2068 | return cp_build_type_variant (r, TYPE_READONLY (t), | |
2069 | TYPE_VOLATILE (t)); | |
2070 | } | |
8d08fdba | 2071 | |
5566b478 | 2072 | /* else fall through */ |
8d08fdba MS |
2073 | case ERROR_MARK: |
2074 | case IDENTIFIER_NODE: | |
2075 | case OP_IDENTIFIER: | |
2076 | case VOID_TYPE: | |
2077 | case REAL_TYPE: | |
37c46b43 | 2078 | case COMPLEX_TYPE: |
2986ae00 | 2079 | case BOOLEAN_TYPE: |
8d08fdba MS |
2080 | case INTEGER_CST: |
2081 | case REAL_CST: | |
2082 | case STRING_CST: | |
8d08fdba MS |
2083 | return t; |
2084 | ||
5566b478 MS |
2085 | case ENUMERAL_TYPE: |
2086 | { | |
2087 | tree ctx = tsubst (TYPE_CONTEXT (t), args, nargs, in_decl); | |
2088 | if (ctx == NULL_TREE) | |
2089 | return t; | |
b87692e5 MS |
2090 | else if (ctx == current_function_decl) |
2091 | return lookup_name (TYPE_IDENTIFIER (t), 1); | |
5566b478 MS |
2092 | else |
2093 | return lookup_nested_type_by_name (ctx, TYPE_IDENTIFIER (t)); | |
2094 | } | |
2095 | ||
8d08fdba MS |
2096 | case INTEGER_TYPE: |
2097 | if (t == integer_type_node) | |
2098 | return t; | |
2099 | ||
2100 | if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST | |
2101 | && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST) | |
2102 | return t; | |
5566b478 MS |
2103 | |
2104 | { | |
37c46b43 MS |
2105 | tree max = TREE_OPERAND (TYPE_MAX_VALUE (t), 0); |
2106 | max = tsubst_expr (max, args, nargs, in_decl); | |
5156628f | 2107 | if (processing_template_decl) |
5566b478 MS |
2108 | { |
2109 | tree itype = make_node (INTEGER_TYPE); | |
2110 | TYPE_MIN_VALUE (itype) = size_zero_node; | |
37c46b43 MS |
2111 | TYPE_MAX_VALUE (itype) = build_min (MINUS_EXPR, sizetype, max, |
2112 | integer_one_node); | |
5566b478 MS |
2113 | return itype; |
2114 | } | |
37c46b43 MS |
2115 | |
2116 | max = fold (build_binary_op (MINUS_EXPR, max, integer_one_node, 1)); | |
5566b478 MS |
2117 | return build_index_2_type (size_zero_node, max); |
2118 | } | |
8d08fdba MS |
2119 | |
2120 | case TEMPLATE_TYPE_PARM: | |
98c1c668 | 2121 | case TEMPLATE_CONST_PARM: |
db5ae43f | 2122 | { |
98c1c668 JM |
2123 | int idx; |
2124 | int level; | |
2125 | ||
2126 | if (TREE_CODE (t) == TEMPLATE_TYPE_PARM) | |
2127 | { | |
2128 | idx = TEMPLATE_TYPE_IDX (t); | |
2129 | level = TEMPLATE_TYPE_LEVEL (t); | |
2130 | } | |
2131 | else | |
2132 | { | |
2133 | idx = TEMPLATE_CONST_IDX (t); | |
2134 | level = TEMPLATE_CONST_LEVEL (t); | |
2135 | } | |
2136 | ||
2137 | if (TREE_VEC_LENGTH (args) > 0) | |
2138 | { | |
2139 | tree arg = NULL_TREE; | |
2140 | ||
2141 | if (TREE_CODE (TREE_VEC_ELT (args, 0)) == TREE_VEC) | |
2142 | { | |
2143 | if (TREE_VEC_LENGTH (args) >= level - 1) | |
2144 | arg = TREE_VEC_ELT | |
2145 | (TREE_VEC_ELT (args, level - 1), idx); | |
2146 | } | |
2147 | else if (level == 1) | |
2148 | arg = TREE_VEC_ELT (args, idx); | |
2149 | ||
2150 | if (arg != NULL_TREE) | |
2151 | { | |
2152 | if (TREE_CODE (t) == TEMPLATE_TYPE_PARM) | |
2153 | return cp_build_type_variant | |
2154 | (arg, TYPE_READONLY (arg) || TYPE_READONLY (t), | |
2155 | TYPE_VOLATILE (arg) || TYPE_VOLATILE (t)); | |
2156 | else | |
2157 | return arg; | |
2158 | } | |
2159 | } | |
2160 | ||
2161 | /* If we get here, we must have been looking at a parm for a | |
2162 | more deeply nested template. */ | |
2163 | my_friendly_assert((TREE_CODE (t) == TEMPLATE_CONST_PARM | |
2164 | && TEMPLATE_CONST_LEVEL (t) > 1) | |
2165 | || (TREE_CODE (t) == TEMPLATE_TYPE_PARM | |
2166 | && TEMPLATE_TYPE_LEVEL (t) > 1), | |
2167 | 0); | |
2168 | return t; | |
db5ae43f | 2169 | } |
8d08fdba | 2170 | |
98c1c668 JM |
2171 | case TEMPLATE_DECL: |
2172 | { | |
2173 | /* We can get here when processing a member template function | |
2174 | of a template class. */ | |
2175 | tree tmpl; | |
2176 | tree decl = DECL_TEMPLATE_RESULT (t); | |
2177 | tree new_decl; | |
2178 | tree parms; | |
386b8a85 | 2179 | tree spec; |
98c1c668 JM |
2180 | int i; |
2181 | ||
2182 | /* We might already have an instance of this template. */ | |
2183 | tree instances = DECL_TEMPLATE_INSTANTIATIONS (t); | |
2184 | tree ctx = tsubst (DECL_CLASS_CONTEXT (t), args, nargs, in_decl); | |
2185 | ||
2186 | for (; instances; instances = TREE_CHAIN (instances)) | |
2187 | if (DECL_CLASS_CONTEXT (TREE_VALUE (instances)) == ctx) | |
2188 | return TREE_VALUE (instances); | |
2189 | ||
2190 | /* Make a new template decl. It will be similar to the | |
2191 | original, but will record the current template arguments. | |
2192 | We also create a new function declaration, which is just | |
2193 | like the old one, but points to this new template, rather | |
2194 | than the old one. */ | |
2195 | tmpl = copy_node (t); | |
2196 | copy_lang_decl (tmpl); | |
2197 | my_friendly_assert (DECL_LANG_SPECIFIC (tmpl) != 0, 0); | |
2198 | DECL_CHAIN (tmpl) = NULL_TREE; | |
2199 | TREE_CHAIN (tmpl) = NULL_TREE; | |
2200 | DECL_TEMPLATE_INFO (tmpl) = build_tree_list (t, args); | |
2201 | new_decl = tsubst (decl, args, nargs, in_decl); | |
2202 | DECL_RESULT (tmpl) = new_decl; | |
98c1c668 JM |
2203 | DECL_TI_TEMPLATE (new_decl) = tmpl; |
2204 | TREE_TYPE (tmpl) = TREE_TYPE (new_decl); | |
27bb8339 | 2205 | DECL_TEMPLATE_INSTANTIATIONS (tmpl) = NULL_TREE; |
fee23f54 | 2206 | SET_DECL_IMPLICIT_INSTANTIATION (tmpl); |
98c1c668 JM |
2207 | |
2208 | /* The template parameters for this new template are all the | |
2209 | template parameters for the old template, except the | |
2210 | outermost level of parameters. */ | |
2211 | DECL_TEMPLATE_PARMS (tmpl) | |
2212 | = copy_node (DECL_TEMPLATE_PARMS (tmpl)); | |
2213 | for (parms = DECL_TEMPLATE_PARMS (tmpl); | |
2214 | TREE_CHAIN (parms) != NULL_TREE; | |
2215 | parms = TREE_CHAIN (parms)) | |
2216 | TREE_CHAIN (parms) = copy_node (TREE_CHAIN (parms)); | |
2217 | ||
2218 | /* Record this partial instantiation. */ | |
2219 | DECL_TEMPLATE_INSTANTIATIONS (t) | |
2220 | = perm_tree_cons (NULL_TREE, tmpl, | |
2221 | DECL_TEMPLATE_INSTANTIATIONS (t)); | |
386b8a85 JM |
2222 | |
2223 | DECL_TEMPLATE_SPECIALIZATIONS (tmpl) = NULL_TREE; | |
98c1c668 JM |
2224 | return tmpl; |
2225 | } | |
8d08fdba MS |
2226 | |
2227 | case FUNCTION_DECL: | |
2228 | { | |
5566b478 | 2229 | tree r = NULL_TREE; |
386b8a85 | 2230 | tree ctx; |
5566b478 MS |
2231 | |
2232 | int member; | |
2233 | ||
8d08fdba MS |
2234 | if (DECL_CONTEXT (t) != NULL_TREE |
2235 | && TREE_CODE_CLASS (TREE_CODE (DECL_CONTEXT (t))) == 't') | |
2236 | { | |
5566b478 MS |
2237 | if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t))) |
2238 | member = 2; | |
2239 | else | |
2240 | member = 1; | |
2241 | ctx = tsubst (DECL_CLASS_CONTEXT (t), args, nargs, t); | |
2242 | type = tsubst (type, args, nargs, in_decl); | |
2243 | } | |
2244 | else | |
2245 | { | |
2246 | member = 0; | |
2247 | ctx = NULL_TREE; | |
2248 | type = tsubst (type, args, nargs, in_decl); | |
2249 | } | |
8d08fdba | 2250 | |
5566b478 MS |
2251 | if (type == TREE_TYPE (t) |
2252 | && (! member || ctx == DECL_CLASS_CONTEXT (t))) | |
d22c8596 MS |
2253 | { |
2254 | t = copy_node (t); | |
2255 | copy_lang_decl (t); | |
2256 | return t; | |
2257 | } | |
8145f082 | 2258 | |
5566b478 MS |
2259 | /* Do we already have this instantiation? */ |
2260 | if (DECL_TEMPLATE_INFO (t) != NULL_TREE) | |
2261 | { | |
98c1c668 | 2262 | tree tmpl = DECL_TI_TEMPLATE (t); |
5566b478 MS |
2263 | tree decls = DECL_TEMPLATE_INSTANTIATIONS (tmpl); |
2264 | ||
2265 | for (; decls; decls = TREE_CHAIN (decls)) | |
2266 | if (TREE_TYPE (TREE_VALUE (decls)) == type | |
2267 | && DECL_CLASS_CONTEXT (TREE_VALUE (decls)) == ctx) | |
2268 | return TREE_VALUE (decls); | |
2269 | } | |
2270 | ||
2271 | /* We do NOT check for matching decls pushed separately at this | |
2272 | point, as they may not represent instantiations of this | |
2273 | template, and in any case are considered separate under the | |
312e7d50 | 2274 | discrete model. Instead, see add_maybe_template. */ |
5566b478 MS |
2275 | |
2276 | r = copy_node (t); | |
2277 | copy_lang_decl (r); | |
2278 | TREE_TYPE (r) = type; | |
2279 | ||
2280 | DECL_CONTEXT (r) | |
2281 | = tsubst (DECL_CONTEXT (t), args, nargs, t); | |
2282 | DECL_CLASS_CONTEXT (r) = ctx; | |
2283 | ||
2284 | if (member && !strncmp (OPERATOR_TYPENAME_FORMAT, | |
2285 | IDENTIFIER_POINTER (DECL_NAME (r)), | |
2286 | sizeof (OPERATOR_TYPENAME_FORMAT) - 1)) | |
2287 | { | |
2288 | /* Type-conversion operator. Reconstruct the name, in | |
2289 | case it's the name of one of the template's parameters. */ | |
2290 | DECL_NAME (r) = build_typename_overload (TREE_TYPE (type)); | |
2291 | } | |
2292 | ||
5566b478 MS |
2293 | if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (t))) |
2294 | { | |
2295 | char *buf, *dbuf = build_overload_name (ctx, 1, 1); | |
2296 | int len = sizeof (DESTRUCTOR_DECL_PREFIX) - 1; | |
2297 | buf = (char *) alloca (strlen (dbuf) | |
2298 | + sizeof (DESTRUCTOR_DECL_PREFIX)); | |
2299 | bcopy (DESTRUCTOR_DECL_PREFIX, buf, len); | |
2300 | buf[len] = '\0'; | |
2301 | strcat (buf, dbuf); | |
2302 | DECL_ASSEMBLER_NAME (r) = get_identifier (buf); | |
8d08fdba | 2303 | } |
386b8a85 JM |
2304 | else |
2305 | { | |
2306 | /* Instantiations of template functions must be mangled | |
2307 | specially, in order to conform to 14.5.5.1 | |
2308 | [temp.over.link]. We use in_decl below rather than | |
2309 | DECL_TI_TEMPLATE (r) because the latter is set to | |
2310 | NULL_TREE in instantiate_decl. */ | |
2311 | tree tmpl; | |
2312 | tree arg_types; | |
2313 | ||
2314 | if (DECL_TEMPLATE_INFO (r)) | |
2315 | tmpl = DECL_TI_TEMPLATE (r); | |
2316 | else | |
2317 | tmpl = in_decl; | |
2318 | ||
2319 | /* tmpl will be NULL if this is a specialization of a | |
2320 | member template of a template class. */ | |
2321 | if (name_mangling_version < 1 | |
2322 | || tmpl == NULL_TREE | |
2323 | || (member && !is_member_template (tmpl) | |
2324 | && !DECL_TEMPLATE_INFO (tmpl))) | |
2325 | { | |
2326 | arg_types = TYPE_ARG_TYPES (type); | |
2327 | if (member && TREE_CODE (type) == FUNCTION_TYPE) | |
2328 | arg_types = hash_tree_chain | |
2329 | (build_pointer_type (DECL_CONTEXT (r)), | |
2330 | arg_types); | |
2331 | ||
2332 | DECL_ASSEMBLER_NAME (r) | |
2333 | = build_decl_overload (DECL_NAME (r), arg_types, | |
2334 | member); | |
2335 | } | |
2336 | else | |
2337 | { | |
2338 | /* We pass the outermost template parameters to | |
2339 | build_template_decl_overload since the innermost | |
2340 | template parameters are still just template | |
2341 | parameters; there are no corresponding subsitution | |
2342 | arguments. */ | |
fee23f54 JM |
2343 | /* FIXME The messed up thing here is that we get here with |
2344 | full args and only one level of parms. This is necessary | |
2345 | because when we partially instantiate a member template, | |
2346 | even though there's really only one level of parms left | |
2347 | we re-use the parms from the original template, which | |
2348 | have level 2. When this is fixed we can remove the | |
2349 | add_to_template_args from instantiate_template. */ | |
386b8a85 JM |
2350 | tree tparms = DECL_TEMPLATE_PARMS (tmpl); |
2351 | ||
2352 | while (tparms && TREE_CHAIN (tparms) != NULL_TREE) | |
2353 | tparms = TREE_CHAIN (tparms); | |
2354 | ||
2355 | my_friendly_assert (tparms != NULL_TREE | |
2356 | && TREE_CODE (tparms) == TREE_LIST, | |
2357 | 0); | |
2358 | tparms = TREE_VALUE (tparms); | |
2359 | ||
2360 | arg_types = TYPE_ARG_TYPES (TREE_TYPE (tmpl)); | |
2361 | if (member && TREE_CODE (type) == FUNCTION_TYPE) | |
2362 | arg_types = hash_tree_chain | |
2363 | (build_pointer_type (DECL_CONTEXT (r)), | |
2364 | arg_types); | |
2365 | ||
2366 | DECL_ASSEMBLER_NAME (r) | |
2367 | = build_template_decl_overload | |
2368 | (DECL_NAME (r), arg_types, | |
2369 | TREE_TYPE (TREE_TYPE (tmpl)), | |
2370 | tparms, | |
2371 | TREE_CODE (TREE_VEC_ELT (args, 0)) == TREE_VEC | |
2372 | ? TREE_VEC_ELT (args, TREE_VEC_LENGTH (args) - 1) : | |
2373 | args, | |
2374 | member); | |
2375 | } | |
2376 | } | |
5566b478 MS |
2377 | DECL_RTL (r) = 0; |
2378 | make_decl_rtl (r, NULL_PTR, 1); | |
2379 | ||
2380 | DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args, nargs, t); | |
2381 | DECL_MAIN_VARIANT (r) = r; | |
2382 | DECL_RESULT (r) = NULL_TREE; | |
2383 | DECL_INITIAL (r) = NULL_TREE; | |
2384 | ||
2385 | TREE_STATIC (r) = 0; | |
2386 | TREE_PUBLIC (r) = 1; | |
2387 | DECL_EXTERNAL (r) = 1; | |
2388 | DECL_INTERFACE_KNOWN (r) = 0; | |
2389 | DECL_DEFER_OUTPUT (r) = 0; | |
2390 | TREE_CHAIN (r) = NULL_TREE; | |
2391 | DECL_CHAIN (r) = NULL_TREE; | |
2392 | ||
2393 | if (IDENTIFIER_OPNAME_P (DECL_NAME (r))) | |
2394 | grok_op_properties (r, DECL_VIRTUAL_P (r), DECL_FRIEND_P (r)); | |
2395 | ||
2396 | /* Look for matching decls for the moment. */ | |
312e7d50 | 2397 | if (! member && ! flag_ansi_overloading) |
8d08fdba | 2398 | { |
5566b478 MS |
2399 | tree decls = lookup_name_nonclass (DECL_NAME (t)); |
2400 | tree d = NULL_TREE; | |
2401 | ||
2402 | if (decls == NULL_TREE) | |
2403 | /* no match */; | |
2404 | else if (is_overloaded_fn (decls)) | |
2405 | for (decls = get_first_fn (decls); decls; | |
2406 | decls = DECL_CHAIN (decls)) | |
8d08fdba | 2407 | { |
5566b478 MS |
2408 | if (TREE_CODE (decls) == FUNCTION_DECL |
2409 | && TREE_TYPE (decls) == type) | |
8d08fdba | 2410 | { |
5566b478 MS |
2411 | d = decls; |
2412 | break; | |
8d08fdba MS |
2413 | } |
2414 | } | |
2415 | ||
5566b478 MS |
2416 | if (d) |
2417 | { | |
2418 | int dcl_only = ! DECL_INITIAL (d); | |
2419 | if (dcl_only) | |
2420 | DECL_INITIAL (r) = error_mark_node; | |
2421 | duplicate_decls (r, d); | |
2422 | r = d; | |
2423 | if (dcl_only) | |
2424 | DECL_INITIAL (r) = 0; | |
2425 | } | |
2426 | } | |
2427 | ||
2428 | if (DECL_TEMPLATE_INFO (t) != NULL_TREE) | |
2429 | { | |
2430 | tree tmpl = DECL_TI_TEMPLATE (t); | |
2431 | tree *declsp = &DECL_TEMPLATE_INSTANTIATIONS (tmpl); | |
98c1c668 JM |
2432 | tree argvec = tsubst (DECL_TI_ARGS (t), args, nargs, in_decl); |
2433 | ||
2434 | if (DECL_TEMPLATE_INFO (tmpl) && DECL_TI_ARGS (tmpl)) | |
2435 | argvec = add_to_template_args (DECL_TI_ARGS (tmpl), argvec); | |
5566b478 MS |
2436 | |
2437 | DECL_TEMPLATE_INFO (r) = perm_tree_cons (tmpl, argvec, NULL_TREE); | |
2438 | *declsp = perm_tree_cons (argvec, r, *declsp); | |
2439 | ||
2440 | /* If we have a preexisting version of this function, don't expand | |
2441 | the template version, use the other instead. */ | |
2442 | if (TREE_STATIC (r) || DECL_TEMPLATE_SPECIALIZATION (r)) | |
2443 | SET_DECL_TEMPLATE_SPECIALIZATION (r); | |
2444 | else | |
2445 | SET_DECL_IMPLICIT_INSTANTIATION (r); | |
2446 | ||
beb53fb8 JM |
2447 | DECL_TEMPLATE_INSTANTIATIONS (tmpl) |
2448 | = tree_cons (argvec, r, DECL_TEMPLATE_INSTANTIATIONS (tmpl)); | |
8d08fdba | 2449 | } |
8d08fdba | 2450 | |
e92cc029 MS |
2451 | /* Like grokfndecl. If we don't do this, pushdecl will mess up our |
2452 | TREE_CHAIN because it doesn't find a previous decl. Sigh. */ | |
2453 | if (member | |
2454 | && IDENTIFIER_GLOBAL_VALUE (DECL_ASSEMBLER_NAME (r)) == NULL_TREE) | |
2455 | IDENTIFIER_GLOBAL_VALUE (DECL_ASSEMBLER_NAME (r)) = r; | |
2456 | ||
8d08fdba MS |
2457 | return r; |
2458 | } | |
2459 | ||
2460 | case PARM_DECL: | |
2461 | { | |
5566b478 MS |
2462 | tree r = copy_node (t); |
2463 | TREE_TYPE (r) = type; | |
8d08fdba | 2464 | DECL_INITIAL (r) = TREE_TYPE (r); |
5566b478 | 2465 | DECL_CONTEXT (r) = NULL_TREE; |
f83b0cb6 JM |
2466 | #ifdef PROMOTE_PROTOTYPES |
2467 | if ((TREE_CODE (type) == INTEGER_TYPE | |
2468 | || TREE_CODE (type) == ENUMERAL_TYPE) | |
2469 | && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)) | |
2470 | DECL_ARG_TYPE (r) = integer_type_node; | |
2471 | #endif | |
8d08fdba MS |
2472 | if (TREE_CHAIN (t)) |
2473 | TREE_CHAIN (r) = tsubst (TREE_CHAIN (t), args, nargs, TREE_CHAIN (t)); | |
2474 | return r; | |
2475 | } | |
2476 | ||
5566b478 MS |
2477 | case FIELD_DECL: |
2478 | { | |
2479 | tree r = copy_node (t); | |
2480 | TREE_TYPE (r) = type; | |
2481 | copy_lang_decl (r); | |
2482 | #if 0 | |
2483 | DECL_FIELD_CONTEXT (r) = tsubst (DECL_FIELD_CONTEXT (t), args, nargs, in_decl); | |
2484 | #endif | |
2485 | DECL_INITIAL (r) = tsubst_expr (DECL_INITIAL (t), args, nargs, in_decl); | |
2486 | TREE_CHAIN (r) = NULL_TREE; | |
2487 | return r; | |
2488 | } | |
2489 | ||
2490 | case USING_DECL: | |
2491 | { | |
2492 | tree r = copy_node (t); | |
2493 | DECL_INITIAL (r) | |
2494 | = tsubst_copy (DECL_INITIAL (t), args, nargs, in_decl); | |
2495 | TREE_CHAIN (r) = NULL_TREE; | |
2496 | return r; | |
2497 | } | |
2498 | ||
2499 | case VAR_DECL: | |
2500 | { | |
2501 | tree r; | |
2502 | tree ctx = tsubst_copy (DECL_CONTEXT (t), args, nargs, in_decl); | |
2503 | ||
2504 | /* Do we already have this instantiation? */ | |
2505 | if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)) | |
2506 | { | |
2507 | tree tmpl = DECL_TI_TEMPLATE (t); | |
2508 | tree decls = DECL_TEMPLATE_INSTANTIATIONS (tmpl); | |
2509 | ||
2510 | for (; decls; decls = TREE_CHAIN (decls)) | |
2511 | if (DECL_CONTEXT (TREE_VALUE (decls)) == ctx) | |
2512 | return TREE_VALUE (decls); | |
2513 | } | |
2514 | ||
2515 | r = copy_node (t); | |
2516 | TREE_TYPE (r) = type; | |
2517 | DECL_CONTEXT (r) = ctx; | |
2518 | if (TREE_STATIC (r)) | |
2519 | DECL_ASSEMBLER_NAME (r) | |
2520 | = build_static_name (DECL_CONTEXT (r), DECL_NAME (r)); | |
d11ad92e MS |
2521 | |
2522 | /* Don't try to expand the initializer until someone tries to use | |
2523 | this variable; otherwise we run into circular dependencies. */ | |
2524 | DECL_INITIAL (r) = NULL_TREE; | |
5566b478 MS |
2525 | |
2526 | DECL_RTL (r) = 0; | |
2527 | DECL_SIZE (r) = 0; | |
2528 | ||
2529 | if (DECL_LANG_SPECIFIC (r)) | |
2530 | { | |
2531 | copy_lang_decl (r); | |
2532 | DECL_CLASS_CONTEXT (r) = DECL_CONTEXT (r); | |
2533 | } | |
2534 | ||
2535 | if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)) | |
2536 | { | |
2537 | tree tmpl = DECL_TI_TEMPLATE (t); | |
2538 | tree *declsp = &DECL_TEMPLATE_INSTANTIATIONS (tmpl); | |
98c1c668 | 2539 | tree argvec = tsubst (DECL_TI_ARGS (t), args, nargs, in_decl); |
5566b478 MS |
2540 | |
2541 | DECL_TEMPLATE_INFO (r) = perm_tree_cons (tmpl, argvec, NULL_TREE); | |
2542 | *declsp = perm_tree_cons (argvec, r, *declsp); | |
2543 | SET_DECL_IMPLICIT_INSTANTIATION (r); | |
2544 | } | |
2545 | TREE_CHAIN (r) = NULL_TREE; | |
2546 | return r; | |
2547 | } | |
2548 | ||
2549 | case TYPE_DECL: | |
d2e5ee5c MS |
2550 | if (t == TYPE_NAME (TREE_TYPE (t))) |
2551 | return TYPE_NAME (type); | |
2552 | ||
5566b478 MS |
2553 | { |
2554 | tree r = copy_node (t); | |
2555 | TREE_TYPE (r) = type; | |
909e536a | 2556 | DECL_CONTEXT (r) = current_class_type; |
5566b478 MS |
2557 | TREE_CHAIN (r) = NULL_TREE; |
2558 | return r; | |
2559 | } | |
2560 | ||
8d08fdba MS |
2561 | case TREE_LIST: |
2562 | { | |
2563 | tree purpose, value, chain, result; | |
2564 | int via_public, via_virtual, via_protected; | |
2565 | ||
2566 | if (t == void_list_node) | |
2567 | return t; | |
2568 | ||
2569 | via_public = TREE_VIA_PUBLIC (t); | |
2570 | via_protected = TREE_VIA_PROTECTED (t); | |
2571 | via_virtual = TREE_VIA_VIRTUAL (t); | |
2572 | ||
2573 | purpose = TREE_PURPOSE (t); | |
2574 | if (purpose) | |
2575 | purpose = tsubst (purpose, args, nargs, in_decl); | |
2576 | value = TREE_VALUE (t); | |
2577 | if (value) | |
2578 | value = tsubst (value, args, nargs, in_decl); | |
2579 | chain = TREE_CHAIN (t); | |
2580 | if (chain && chain != void_type_node) | |
2581 | chain = tsubst (chain, args, nargs, in_decl); | |
2582 | if (purpose == TREE_PURPOSE (t) | |
2583 | && value == TREE_VALUE (t) | |
2584 | && chain == TREE_CHAIN (t)) | |
2585 | return t; | |
2586 | result = hash_tree_cons (via_public, via_virtual, via_protected, | |
2587 | purpose, value, chain); | |
2588 | TREE_PARMLIST (result) = TREE_PARMLIST (t); | |
2589 | return result; | |
2590 | } | |
2591 | case TREE_VEC: | |
5566b478 MS |
2592 | if (type != NULL_TREE) |
2593 | { | |
85b71cf2 JM |
2594 | /* A binfo node. */ |
2595 | ||
5566b478 MS |
2596 | t = copy_node (t); |
2597 | ||
2598 | if (type == TREE_TYPE (t)) | |
2599 | return t; | |
2600 | ||
2601 | TREE_TYPE (t) = complete_type (type); | |
6633d636 MS |
2602 | if (IS_AGGR_TYPE (type)) |
2603 | { | |
2604 | BINFO_VTABLE (t) = TYPE_BINFO_VTABLE (type); | |
2605 | BINFO_VIRTUALS (t) = TYPE_BINFO_VIRTUALS (type); | |
2606 | if (TYPE_BINFO_BASETYPES (type) != NULL_TREE) | |
2607 | BINFO_BASETYPES (t) = copy_node (TYPE_BINFO_BASETYPES (type)); | |
2608 | } | |
5566b478 MS |
2609 | return t; |
2610 | } | |
85b71cf2 JM |
2611 | |
2612 | /* Otherwise, a vector of template arguments. */ | |
8d08fdba MS |
2613 | { |
2614 | int len = TREE_VEC_LENGTH (t), need_new = 0, i; | |
2615 | tree *elts = (tree *) alloca (len * sizeof (tree)); | |
5566b478 | 2616 | |
1daa5dd8 | 2617 | bzero ((char *) elts, len * sizeof (tree)); |
8d08fdba MS |
2618 | |
2619 | for (i = 0; i < len; i++) | |
2620 | { | |
ec255269 | 2621 | elts[i] = tsubst_expr (TREE_VEC_ELT (t, i), args, nargs, in_decl); |
85b71cf2 JM |
2622 | |
2623 | if (TREE_CODE_CLASS (TREE_CODE (elts[i])) != 't' | |
2624 | && !uses_template_parms (elts[i])) | |
2625 | { | |
2626 | /* Sometimes, one of the args was an expression involving a | |
2627 | template constant parameter, like N - 1. Now that we've | |
2628 | tsubst'd, we might have something like 2 - 1. This will | |
2629 | confuse lookup_template_class, so we do constant folding | |
2630 | here. We have to unset processing_template_decl, to | |
2631 | fool build_expr_from_tree() into building an actual | |
2632 | tree. */ | |
2633 | ||
2634 | int saved_processing_template_decl = processing_template_decl; | |
2635 | processing_template_decl = 0; | |
2636 | elts[i] = fold (build_expr_from_tree (elts[i])); | |
2637 | processing_template_decl = saved_processing_template_decl; | |
2638 | } | |
2639 | ||
8d08fdba MS |
2640 | if (elts[i] != TREE_VEC_ELT (t, i)) |
2641 | need_new = 1; | |
2642 | } | |
2643 | ||
2644 | if (!need_new) | |
2645 | return t; | |
2646 | ||
2647 | t = make_tree_vec (len); | |
2648 | for (i = 0; i < len; i++) | |
2649 | TREE_VEC_ELT (t, i) = elts[i]; | |
5566b478 | 2650 | |
8d08fdba MS |
2651 | return t; |
2652 | } | |
2653 | case POINTER_TYPE: | |
2654 | case REFERENCE_TYPE: | |
2655 | { | |
2656 | tree r; | |
2657 | enum tree_code code; | |
2658 | if (type == TREE_TYPE (t)) | |
2659 | return t; | |
2660 | ||
2661 | code = TREE_CODE (t); | |
2662 | if (code == POINTER_TYPE) | |
2663 | r = build_pointer_type (type); | |
2664 | else | |
2665 | r = build_reference_type (type); | |
f376e137 | 2666 | r = cp_build_type_variant (r, TYPE_READONLY (t), TYPE_VOLATILE (t)); |
8d08fdba MS |
2667 | /* Will this ever be needed for TYPE_..._TO values? */ |
2668 | layout_type (r); | |
2669 | return r; | |
2670 | } | |
a4443a08 MS |
2671 | case OFFSET_TYPE: |
2672 | return build_offset_type | |
2673 | (tsubst (TYPE_OFFSET_BASETYPE (t), args, nargs, in_decl), type); | |
8d08fdba MS |
2674 | case FUNCTION_TYPE: |
2675 | case METHOD_TYPE: | |
2676 | { | |
75b0bbce | 2677 | tree values = TYPE_ARG_TYPES (t); |
8d08fdba | 2678 | tree context = TYPE_CONTEXT (t); |
c11b6f21 MS |
2679 | tree raises = TYPE_RAISES_EXCEPTIONS (t); |
2680 | tree fntype; | |
8d08fdba MS |
2681 | |
2682 | /* Don't bother recursing if we know it won't change anything. */ | |
2683 | if (values != void_list_node) | |
5566b478 MS |
2684 | { |
2685 | /* This should probably be rewritten to use hash_tree_cons for | |
2686 | the memory savings. */ | |
2687 | tree first = NULL_TREE; | |
2688 | tree last; | |
2689 | ||
2690 | for (; values && values != void_list_node; | |
2691 | values = TREE_CHAIN (values)) | |
2692 | { | |
f7da6097 MS |
2693 | tree value = TYPE_MAIN_VARIANT (type_decays_to |
2694 | (tsubst (TREE_VALUE (values), args, nargs, in_decl))); | |
de22184b MS |
2695 | /* Don't instantiate default args unless they are used. |
2696 | Handle it in build_over_call instead. */ | |
2697 | tree purpose = TREE_PURPOSE (values); | |
5566b478 MS |
2698 | tree x = build_tree_list (purpose, value); |
2699 | ||
2700 | if (first) | |
2701 | TREE_CHAIN (last) = x; | |
2702 | else | |
2703 | first = x; | |
2704 | last = x; | |
2705 | } | |
2706 | ||
2707 | if (values == void_list_node) | |
2708 | TREE_CHAIN (last) = void_list_node; | |
2709 | ||
2710 | values = first; | |
2711 | } | |
8d08fdba MS |
2712 | if (context) |
2713 | context = tsubst (context, args, nargs, in_decl); | |
2714 | /* Could also optimize cases where return value and | |
2715 | values have common elements (e.g., T min(const &T, const T&). */ | |
2716 | ||
2717 | /* If the above parameters haven't changed, just return the type. */ | |
2718 | if (type == TREE_TYPE (t) | |
2719 | && values == TYPE_VALUES (t) | |
2720 | && context == TYPE_CONTEXT (t)) | |
2721 | return t; | |
2722 | ||
2723 | /* Construct a new type node and return it. */ | |
2724 | if (TREE_CODE (t) == FUNCTION_TYPE | |
2725 | && context == NULL_TREE) | |
2726 | { | |
c11b6f21 | 2727 | fntype = build_function_type (type, values); |
8d08fdba MS |
2728 | } |
2729 | else if (context == NULL_TREE) | |
2730 | { | |
2731 | tree base = tsubst (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t))), | |
2732 | args, nargs, in_decl); | |
c11b6f21 MS |
2733 | fntype = build_cplus_method_type (base, type, |
2734 | TREE_CHAIN (values)); | |
8d08fdba MS |
2735 | } |
2736 | else | |
2737 | { | |
c11b6f21 MS |
2738 | fntype = make_node (TREE_CODE (t)); |
2739 | TREE_TYPE (fntype) = type; | |
2740 | TYPE_CONTEXT (fntype) = context; | |
2741 | TYPE_VALUES (fntype) = values; | |
2742 | TYPE_SIZE (fntype) = TYPE_SIZE (t); | |
2743 | TYPE_ALIGN (fntype) = TYPE_ALIGN (t); | |
2744 | TYPE_MODE (fntype) = TYPE_MODE (t); | |
8d08fdba | 2745 | if (TYPE_METHOD_BASETYPE (t)) |
c11b6f21 MS |
2746 | TYPE_METHOD_BASETYPE (fntype) = tsubst (TYPE_METHOD_BASETYPE (t), |
2747 | args, nargs, in_decl); | |
8d08fdba MS |
2748 | /* Need to generate hash value. */ |
2749 | my_friendly_abort (84); | |
2750 | } | |
c11b6f21 MS |
2751 | fntype = build_type_variant (fntype, |
2752 | TYPE_READONLY (t), | |
2753 | TYPE_VOLATILE (t)); | |
2754 | if (raises) | |
2755 | { | |
2756 | raises = tsubst (raises, args, nargs, in_decl); | |
2757 | fntype = build_exception_variant (fntype, raises); | |
2758 | } | |
2759 | return fntype; | |
8d08fdba MS |
2760 | } |
2761 | case ARRAY_TYPE: | |
2762 | { | |
2763 | tree domain = tsubst (TYPE_DOMAIN (t), args, nargs, in_decl); | |
2764 | tree r; | |
2765 | if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t)) | |
2766 | return t; | |
2767 | r = build_cplus_array_type (type, domain); | |
2768 | return r; | |
2769 | } | |
2770 | ||
8d08fdba | 2771 | case PLUS_EXPR: |
5566b478 | 2772 | case MINUS_EXPR: |
8d08fdba MS |
2773 | return fold (build (TREE_CODE (t), TREE_TYPE (t), |
2774 | tsubst (TREE_OPERAND (t, 0), args, nargs, in_decl), | |
2775 | tsubst (TREE_OPERAND (t, 1), args, nargs, in_decl))); | |
2776 | ||
2777 | case NEGATE_EXPR: | |
2778 | case NOP_EXPR: | |
2779 | return fold (build1 (TREE_CODE (t), TREE_TYPE (t), | |
2780 | tsubst (TREE_OPERAND (t, 0), args, nargs, in_decl))); | |
2781 | ||
5566b478 MS |
2782 | case TYPENAME_TYPE: |
2783 | { | |
2784 | tree ctx = tsubst (TYPE_CONTEXT (t), args, nargs, in_decl); | |
2785 | tree f = make_typename_type (ctx, TYPE_IDENTIFIER (t)); | |
2786 | return cp_build_type_variant | |
2787 | (f, TYPE_READONLY (f) || TYPE_READONLY (t), | |
2788 | TYPE_VOLATILE (f) || TYPE_VOLATILE (t)); | |
2789 | } | |
2790 | ||
2791 | case INDIRECT_REF: | |
2792 | return make_pointer_declarator | |
2793 | (type, tsubst (TREE_OPERAND (t, 0), args, nargs, in_decl)); | |
2794 | ||
2795 | case ADDR_EXPR: | |
2796 | return make_reference_declarator | |
2797 | (type, tsubst (TREE_OPERAND (t, 0), args, nargs, in_decl)); | |
2798 | ||
2799 | case ARRAY_REF: | |
2800 | return build_parse_node | |
2801 | (ARRAY_REF, tsubst (TREE_OPERAND (t, 0), args, nargs, in_decl), | |
e76a2646 | 2802 | tsubst_expr (TREE_OPERAND (t, 1), args, nargs, in_decl)); |
5566b478 MS |
2803 | |
2804 | case CALL_EXPR: | |
c11b6f21 MS |
2805 | return make_call_declarator |
2806 | (tsubst (TREE_OPERAND (t, 0), args, nargs, in_decl), | |
2807 | tsubst (TREE_OPERAND (t, 1), args, nargs, in_decl), | |
2808 | TREE_OPERAND (t, 2), | |
2809 | tsubst (TREE_TYPE (t), args, nargs, in_decl)); | |
5566b478 | 2810 | |
fc378698 MS |
2811 | case SCOPE_REF: |
2812 | return build_parse_node | |
2813 | (TREE_CODE (t), tsubst (TREE_OPERAND (t, 0), args, nargs, in_decl), | |
2814 | tsubst (TREE_OPERAND (t, 1), args, nargs, in_decl)); | |
2815 | ||
8d08fdba | 2816 | default: |
5566b478 | 2817 | sorry ("use of `%s' in template", |
8d08fdba MS |
2818 | tree_code_name [(int) TREE_CODE (t)]); |
2819 | return error_mark_node; | |
2820 | } | |
2821 | } | |
2822 | ||
5566b478 MS |
2823 | void |
2824 | do_pushlevel () | |
2825 | { | |
2826 | emit_line_note (input_filename, lineno); | |
2827 | pushlevel (0); | |
2828 | clear_last_expr (); | |
2829 | push_momentary (); | |
2830 | expand_start_bindings (0); | |
2831 | } | |
2832 | ||
8d08fdba | 2833 | tree |
5566b478 | 2834 | do_poplevel () |
8d08fdba | 2835 | { |
5566b478 | 2836 | tree t; |
85b71cf2 | 2837 | int saved_warn_unused; |
8d08fdba | 2838 | |
85b71cf2 JM |
2839 | if (processing_template_decl) |
2840 | { | |
2841 | saved_warn_unused = warn_unused; | |
2842 | warn_unused = 0; | |
2843 | } | |
5566b478 | 2844 | expand_end_bindings (getdecls (), kept_level_p (), 1); |
85b71cf2 JM |
2845 | if (processing_template_decl) |
2846 | warn_unused = saved_warn_unused; | |
5566b478 MS |
2847 | t = poplevel (kept_level_p (), 1, 0); |
2848 | pop_momentary (); | |
2849 | return t; | |
2850 | } | |
8d08fdba | 2851 | |
5566b478 MS |
2852 | tree |
2853 | tsubst_copy (t, args, nargs, in_decl) | |
98c1c668 | 2854 | tree t, args; |
5566b478 MS |
2855 | int nargs; |
2856 | tree in_decl; | |
2857 | { | |
2858 | enum tree_code code; | |
8d08fdba | 2859 | |
5566b478 MS |
2860 | if (t == NULL_TREE || t == error_mark_node) |
2861 | return t; | |
2862 | ||
2863 | code = TREE_CODE (t); | |
b7484fbe | 2864 | |
5566b478 MS |
2865 | switch (code) |
2866 | { | |
2867 | case PARM_DECL: | |
2868 | return do_identifier (DECL_NAME (t), 0); | |
2869 | ||
2870 | case CONST_DECL: | |
2871 | case FIELD_DECL: | |
2872 | if (DECL_CONTEXT (t)) | |
2873 | { | |
2874 | tree ctx = tsubst (DECL_CONTEXT (t), args, nargs, in_decl); | |
b87692e5 MS |
2875 | if (ctx == current_function_decl) |
2876 | return lookup_name (DECL_NAME (t), 0); | |
2877 | else if (ctx != DECL_CONTEXT (t)) | |
5566b478 MS |
2878 | return lookup_field (ctx, DECL_NAME (t), 0, 0); |
2879 | } | |
2880 | return t; | |
2881 | ||
2882 | case VAR_DECL: | |
2883 | case FUNCTION_DECL: | |
2884 | if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)) | |
2885 | t = tsubst (t, args, nargs, in_decl); | |
2886 | mark_used (t); | |
2887 | return t; | |
2888 | ||
98c1c668 JM |
2889 | case TEMPLATE_DECL: |
2890 | if (is_member_template (t)) | |
2891 | return tsubst (t, args, nargs, in_decl); | |
2892 | else | |
2893 | return t; | |
2894 | ||
5566b478 MS |
2895 | #if 0 |
2896 | case IDENTIFIER_NODE: | |
2897 | return do_identifier (t, 0); | |
2898 | #endif | |
2899 | ||
2900 | case CAST_EXPR: | |
2901 | case REINTERPRET_CAST_EXPR: | |
e92cc029 MS |
2902 | case CONST_CAST_EXPR: |
2903 | case STATIC_CAST_EXPR: | |
2904 | case DYNAMIC_CAST_EXPR: | |
5566b478 MS |
2905 | return build1 |
2906 | (code, tsubst (TREE_TYPE (t), args, nargs, in_decl), | |
2907 | tsubst_copy (TREE_OPERAND (t, 0), args, nargs, in_decl)); | |
2908 | ||
2909 | case INDIRECT_REF: | |
2910 | case PREDECREMENT_EXPR: | |
2911 | case PREINCREMENT_EXPR: | |
2912 | case POSTDECREMENT_EXPR: | |
2913 | case POSTINCREMENT_EXPR: | |
2914 | case NEGATE_EXPR: | |
2915 | case TRUTH_NOT_EXPR: | |
b87692e5 | 2916 | case BIT_NOT_EXPR: |
5566b478 MS |
2917 | case ADDR_EXPR: |
2918 | case CONVERT_EXPR: /* Unary + */ | |
2919 | case SIZEOF_EXPR: | |
2920 | case ARROW_EXPR: | |
fc378698 | 2921 | case THROW_EXPR: |
5156628f | 2922 | case TYPEID_EXPR: |
5566b478 MS |
2923 | return build1 |
2924 | (code, NULL_TREE, | |
2925 | tsubst_copy (TREE_OPERAND (t, 0), args, nargs, in_decl)); | |
2926 | ||
2927 | case PLUS_EXPR: | |
2928 | case MINUS_EXPR: | |
2929 | case MULT_EXPR: | |
2930 | case TRUNC_DIV_EXPR: | |
2931 | case CEIL_DIV_EXPR: | |
2932 | case FLOOR_DIV_EXPR: | |
2933 | case ROUND_DIV_EXPR: | |
2934 | case EXACT_DIV_EXPR: | |
2935 | case BIT_AND_EXPR: | |
2936 | case BIT_ANDTC_EXPR: | |
2937 | case BIT_IOR_EXPR: | |
2938 | case BIT_XOR_EXPR: | |
2939 | case TRUNC_MOD_EXPR: | |
2940 | case FLOOR_MOD_EXPR: | |
2941 | case TRUTH_ANDIF_EXPR: | |
2942 | case TRUTH_ORIF_EXPR: | |
2943 | case TRUTH_AND_EXPR: | |
2944 | case TRUTH_OR_EXPR: | |
2945 | case RSHIFT_EXPR: | |
2946 | case LSHIFT_EXPR: | |
2947 | case RROTATE_EXPR: | |
2948 | case LROTATE_EXPR: | |
2949 | case EQ_EXPR: | |
2950 | case NE_EXPR: | |
2951 | case MAX_EXPR: | |
2952 | case MIN_EXPR: | |
2953 | case LE_EXPR: | |
2954 | case GE_EXPR: | |
2955 | case LT_EXPR: | |
2956 | case GT_EXPR: | |
2957 | case COMPONENT_REF: | |
2958 | case ARRAY_REF: | |
2959 | case COMPOUND_EXPR: | |
2960 | case SCOPE_REF: | |
2961 | case DOTSTAR_EXPR: | |
2962 | case MEMBER_REF: | |
2963 | return build_nt | |
2964 | (code, tsubst_copy (TREE_OPERAND (t, 0), args, nargs, in_decl), | |
2965 | tsubst_copy (TREE_OPERAND (t, 1), args, nargs, in_decl)); | |
2966 | ||
2967 | case CALL_EXPR: | |
2968 | { | |
2969 | tree fn = TREE_OPERAND (t, 0); | |
2970 | if (really_overloaded_fn (fn)) | |
2971 | fn = tsubst_copy (TREE_VALUE (fn), args, nargs, in_decl); | |
2972 | else | |
2973 | fn = tsubst_copy (fn, args, nargs, in_decl); | |
2974 | return build_nt | |
2975 | (code, fn, tsubst_copy (TREE_OPERAND (t, 1), args, nargs, in_decl), | |
2976 | NULL_TREE); | |
2977 | } | |
2978 | ||
2979 | case METHOD_CALL_EXPR: | |
2980 | { | |
2981 | tree name = TREE_OPERAND (t, 0); | |
2982 | if (TREE_CODE (name) == BIT_NOT_EXPR) | |
2983 | { | |
2984 | name = tsubst_copy (TREE_OPERAND (name, 0), args, nargs, in_decl); | |
fc378698 | 2985 | name = build1 (BIT_NOT_EXPR, NULL_TREE, TYPE_MAIN_VARIANT (name)); |
5566b478 MS |
2986 | } |
2987 | else if (TREE_CODE (name) == SCOPE_REF | |
2988 | && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR) | |
2989 | { | |
2990 | tree base = tsubst_copy (TREE_OPERAND (name, 0), args, nargs, in_decl); | |
2991 | name = TREE_OPERAND (name, 1); | |
2992 | name = tsubst_copy (TREE_OPERAND (name, 0), args, nargs, in_decl); | |
fc378698 | 2993 | name = build1 (BIT_NOT_EXPR, NULL_TREE, TYPE_MAIN_VARIANT (name)); |
5566b478 MS |
2994 | name = build_nt (SCOPE_REF, base, name); |
2995 | } | |
2996 | else | |
2997 | name = tsubst_copy (TREE_OPERAND (t, 0), args, nargs, in_decl); | |
2998 | return build_nt | |
2999 | (code, name, tsubst_copy (TREE_OPERAND (t, 1), args, nargs, in_decl), | |
3000 | tsubst_copy (TREE_OPERAND (t, 2), args, nargs, in_decl), | |
3001 | NULL_TREE); | |
3002 | } | |
3003 | ||
3004 | case COND_EXPR: | |
3005 | case MODOP_EXPR: | |
3006 | return build_nt | |
3007 | (code, tsubst_copy (TREE_OPERAND (t, 0), args, nargs, in_decl), | |
3008 | tsubst_copy (TREE_OPERAND (t, 1), args, nargs, in_decl), | |
3009 | tsubst_copy (TREE_OPERAND (t, 2), args, nargs, in_decl)); | |
3010 | ||
3011 | case NEW_EXPR: | |
3012 | { | |
3013 | tree r = build_nt | |
3014 | (code, tsubst_copy (TREE_OPERAND (t, 0), args, nargs, in_decl), | |
3015 | tsubst_copy (TREE_OPERAND (t, 1), args, nargs, in_decl), | |
3016 | tsubst_copy (TREE_OPERAND (t, 2), args, nargs, in_decl)); | |
3017 | NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t); | |
3018 | return r; | |
3019 | } | |
3020 | ||
3021 | case DELETE_EXPR: | |
3022 | { | |
3023 | tree r = build_nt | |
3024 | (code, tsubst_copy (TREE_OPERAND (t, 0), args, nargs, in_decl), | |
3025 | tsubst_copy (TREE_OPERAND (t, 1), args, nargs, in_decl)); | |
3026 | DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t); | |
3027 | DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t); | |
3028 | return r; | |
3029 | } | |
3030 | ||
386b8a85 JM |
3031 | case TEMPLATE_ID_EXPR: |
3032 | { | |
3033 | tree r = lookup_template_function | |
3034 | (tsubst_copy (TREE_OPERAND (t, 0), args, nargs, in_decl), | |
3035 | tsubst_copy (TREE_OPERAND (t, 1), args, nargs, in_decl)); | |
3036 | return r; | |
3037 | } | |
3038 | ||
5566b478 MS |
3039 | case TREE_LIST: |
3040 | { | |
3041 | tree purpose, value, chain; | |
3042 | ||
3043 | if (t == void_list_node) | |
3044 | return t; | |
3045 | ||
3046 | purpose = TREE_PURPOSE (t); | |
3047 | if (purpose) | |
3048 | purpose = tsubst_copy (purpose, args, nargs, in_decl); | |
3049 | value = TREE_VALUE (t); | |
3050 | if (value) | |
3051 | value = tsubst_copy (value, args, nargs, in_decl); | |
3052 | chain = TREE_CHAIN (t); | |
3053 | if (chain && chain != void_type_node) | |
3054 | chain = tsubst_copy (chain, args, nargs, in_decl); | |
3055 | if (purpose == TREE_PURPOSE (t) | |
3056 | && value == TREE_VALUE (t) | |
3057 | && chain == TREE_CHAIN (t)) | |
3058 | return t; | |
3059 | return tree_cons (purpose, value, chain); | |
3060 | } | |
3061 | ||
3062 | case RECORD_TYPE: | |
3063 | case UNION_TYPE: | |
3064 | case ENUMERAL_TYPE: | |
3065 | case INTEGER_TYPE: | |
3066 | case TEMPLATE_TYPE_PARM: | |
3067 | case TEMPLATE_CONST_PARM: | |
3068 | case POINTER_TYPE: | |
3069 | case REFERENCE_TYPE: | |
3070 | case OFFSET_TYPE: | |
3071 | case FUNCTION_TYPE: | |
3072 | case METHOD_TYPE: | |
3073 | case ARRAY_TYPE: | |
3074 | case TYPENAME_TYPE: | |
3075 | return tsubst (t, args, nargs, in_decl); | |
3076 | ||
e92cc029 MS |
3077 | case IDENTIFIER_NODE: |
3078 | if (IDENTIFIER_TYPENAME_P (t)) | |
3079 | return build_typename_overload | |
3080 | (tsubst (TREE_TYPE (t), args, nargs, in_decl)); | |
3081 | else | |
3082 | return t; | |
3083 | ||
5156628f MS |
3084 | case CONSTRUCTOR: |
3085 | return build | |
3086 | (CONSTRUCTOR, tsubst (TREE_TYPE (t), args, nargs, in_decl), NULL_TREE, | |
3087 | tsubst_copy (CONSTRUCTOR_ELTS (t), args, nargs, in_decl)); | |
3088 | ||
5566b478 MS |
3089 | default: |
3090 | return t; | |
3091 | } | |
3092 | } | |
3093 | ||
3094 | tree | |
3095 | tsubst_expr (t, args, nargs, in_decl) | |
98c1c668 | 3096 | tree t, args; |
5566b478 MS |
3097 | int nargs; |
3098 | tree in_decl; | |
3099 | { | |
3100 | if (t == NULL_TREE || t == error_mark_node) | |
3101 | return t; | |
3102 | ||
5156628f | 3103 | if (processing_template_decl) |
5566b478 MS |
3104 | return tsubst_copy (t, args, nargs, in_decl); |
3105 | ||
3106 | switch (TREE_CODE (t)) | |
8d08fdba | 3107 | { |
5566b478 MS |
3108 | case RETURN_STMT: |
3109 | lineno = TREE_COMPLEXITY (t); | |
3110 | emit_line_note (input_filename, lineno); | |
3111 | c_expand_return | |
3112 | (tsubst_expr (TREE_OPERAND (t, 0), args, nargs, in_decl)); | |
3113 | finish_stmt (); | |
3114 | break; | |
3115 | ||
3116 | case EXPR_STMT: | |
3117 | lineno = TREE_COMPLEXITY (t); | |
3118 | emit_line_note (input_filename, lineno); | |
3119 | t = tsubst_expr (TREE_OPERAND (t, 0), args, nargs, in_decl); | |
3120 | /* Do default conversion if safe and possibly important, | |
3121 | in case within ({...}). */ | |
3122 | if ((TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE && lvalue_p (t)) | |
3123 | || TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE) | |
3124 | t = default_conversion (t); | |
3125 | cplus_expand_expr_stmt (t); | |
3126 | clear_momentary (); | |
3127 | finish_stmt (); | |
3128 | break; | |
3129 | ||
3130 | case DECL_STMT: | |
3131 | { | |
3132 | int i = suspend_momentary (); | |
67d743fe | 3133 | tree dcl, init; |
5566b478 MS |
3134 | |
3135 | lineno = TREE_COMPLEXITY (t); | |
3136 | emit_line_note (input_filename, lineno); | |
3137 | dcl = start_decl | |
3138 | (tsubst (TREE_OPERAND (t, 0), args, nargs, in_decl), | |
3139 | tsubst (TREE_OPERAND (t, 1), args, nargs, in_decl), | |
c11b6f21 MS |
3140 | TREE_OPERAND (t, 2) != 0); |
3141 | init = tsubst_expr (TREE_OPERAND (t, 2), args, nargs, in_decl); | |
5566b478 | 3142 | cp_finish_decl |
a0128b67 | 3143 | (dcl, init, NULL_TREE, 1, /*init ? LOOKUP_ONLYCONVERTING :*/ 0); |
5566b478 MS |
3144 | resume_momentary (i); |
3145 | return dcl; | |
3146 | } | |
8d08fdba | 3147 | |
5566b478 MS |
3148 | case FOR_STMT: |
3149 | { | |
3150 | tree tmp; | |
3151 | int init_scope = (flag_new_for_scope > 0 && TREE_OPERAND (t, 0) | |
3152 | && TREE_CODE (TREE_OPERAND (t, 0)) == DECL_STMT); | |
3153 | int cond_scope = (TREE_OPERAND (t, 1) | |
3154 | && TREE_CODE (TREE_OPERAND (t, 1)) == DECL_STMT); | |
3155 | ||
3156 | lineno = TREE_COMPLEXITY (t); | |
3157 | emit_line_note (input_filename, lineno); | |
3158 | if (init_scope) | |
3159 | do_pushlevel (); | |
e76a2646 MS |
3160 | for (tmp = TREE_OPERAND (t, 0); tmp; tmp = TREE_CHAIN (tmp)) |
3161 | tsubst_expr (tmp, args, nargs, in_decl); | |
5566b478 MS |
3162 | emit_nop (); |
3163 | emit_line_note (input_filename, lineno); | |
3164 | expand_start_loop_continue_elsewhere (1); | |
3165 | ||
3166 | if (cond_scope) | |
3167 | do_pushlevel (); | |
3168 | tmp = tsubst_expr (TREE_OPERAND (t, 1), args, nargs, in_decl); | |
3169 | emit_line_note (input_filename, lineno); | |
3170 | if (tmp) | |
3171 | expand_exit_loop_if_false (0, condition_conversion (tmp)); | |
3172 | ||
3173 | if (! cond_scope) | |
3174 | do_pushlevel (); | |
3175 | tsubst_expr (TREE_OPERAND (t, 3), args, nargs, in_decl); | |
3176 | do_poplevel (); | |
3177 | ||
3178 | emit_line_note (input_filename, lineno); | |
3179 | expand_loop_continue_here (); | |
3180 | tmp = tsubst_expr (TREE_OPERAND (t, 2), args, nargs, in_decl); | |
3181 | if (tmp) | |
3182 | cplus_expand_expr_stmt (tmp); | |
3183 | ||
3184 | expand_end_loop (); | |
3185 | if (init_scope) | |
3186 | do_poplevel (); | |
3187 | finish_stmt (); | |
3188 | } | |
3189 | break; | |
8d08fdba | 3190 | |
5566b478 MS |
3191 | case WHILE_STMT: |
3192 | { | |
3193 | tree cond; | |
3194 | ||
3195 | lineno = TREE_COMPLEXITY (t); | |
3196 | emit_nop (); | |
3197 | emit_line_note (input_filename, lineno); | |
3198 | expand_start_loop (1); | |
3199 | ||
3200 | cond = TREE_OPERAND (t, 0); | |
3201 | if (TREE_CODE (cond) == DECL_STMT) | |
3202 | do_pushlevel (); | |
3203 | cond = tsubst_expr (cond, args, nargs, in_decl); | |
3204 | emit_line_note (input_filename, lineno); | |
3205 | expand_exit_loop_if_false (0, condition_conversion (cond)); | |
3206 | ||
3207 | if (TREE_CODE (TREE_OPERAND (t, 0)) != DECL_STMT) | |
3208 | do_pushlevel (); | |
3209 | tsubst_expr (TREE_OPERAND (t, 1), args, nargs, in_decl); | |
3210 | do_poplevel (); | |
3211 | ||
3212 | expand_end_loop (); | |
3213 | finish_stmt (); | |
3214 | } | |
3215 | break; | |
8d08fdba | 3216 | |
5566b478 MS |
3217 | case DO_STMT: |
3218 | { | |
3219 | tree cond; | |
8d08fdba | 3220 | |
5566b478 MS |
3221 | lineno = TREE_COMPLEXITY (t); |
3222 | emit_nop (); | |
3223 | emit_line_note (input_filename, lineno); | |
3224 | expand_start_loop_continue_elsewhere (1); | |
8d08fdba | 3225 | |
5566b478 MS |
3226 | tsubst_expr (TREE_OPERAND (t, 0), args, nargs, in_decl); |
3227 | expand_loop_continue_here (); | |
f0e01782 | 3228 | |
5566b478 MS |
3229 | cond = tsubst_expr (TREE_OPERAND (t, 1), args, nargs, in_decl); |
3230 | emit_line_note (input_filename, lineno); | |
3231 | expand_exit_loop_if_false (0, condition_conversion (cond)); | |
3232 | expand_end_loop (); | |
28cbf42c | 3233 | |
5566b478 MS |
3234 | clear_momentary (); |
3235 | finish_stmt (); | |
3236 | } | |
3237 | break; | |
a0a33927 | 3238 | |
5566b478 | 3239 | case IF_STMT: |
8d08fdba | 3240 | { |
5566b478 MS |
3241 | tree tmp; |
3242 | int cond_scope = (TREE_CODE (TREE_OPERAND (t, 0)) == DECL_STMT); | |
3243 | ||
3244 | lineno = TREE_COMPLEXITY (t); | |
3245 | if (cond_scope) | |
3246 | do_pushlevel (); | |
3247 | tmp = tsubst_expr (TREE_OPERAND (t, 0), args, nargs, in_decl); | |
3248 | emit_line_note (input_filename, lineno); | |
3249 | expand_start_cond (condition_conversion (tmp), 0); | |
3250 | ||
3251 | if (tmp = TREE_OPERAND (t, 1), tmp) | |
3252 | tsubst_expr (tmp, args, nargs, in_decl); | |
db5ae43f | 3253 | |
5566b478 | 3254 | if (tmp = TREE_OPERAND (t, 2), tmp) |
db5ae43f | 3255 | { |
5566b478 MS |
3256 | expand_start_else (); |
3257 | tsubst_expr (tmp, args, nargs, in_decl); | |
db5ae43f MS |
3258 | } |
3259 | ||
5566b478 | 3260 | expand_end_cond (); |
8d08fdba | 3261 | |
5566b478 MS |
3262 | if (cond_scope) |
3263 | do_poplevel (); | |
8d08fdba | 3264 | |
5566b478 | 3265 | finish_stmt (); |
8d08fdba | 3266 | } |
5566b478 | 3267 | break; |
8d08fdba | 3268 | |
5566b478 MS |
3269 | case COMPOUND_STMT: |
3270 | { | |
3271 | tree substmt = TREE_OPERAND (t, 0); | |
8d08fdba | 3272 | |
5566b478 | 3273 | lineno = TREE_COMPLEXITY (t); |
8d08fdba | 3274 | |
5566b478 MS |
3275 | if (COMPOUND_STMT_NO_SCOPE (t) == 0) |
3276 | do_pushlevel (); | |
8d08fdba | 3277 | |
5566b478 MS |
3278 | for (; substmt; substmt = TREE_CHAIN (substmt)) |
3279 | tsubst_expr (substmt, args, nargs, in_decl); | |
8d08fdba | 3280 | |
5566b478 MS |
3281 | if (COMPOUND_STMT_NO_SCOPE (t) == 0) |
3282 | do_poplevel (); | |
3283 | } | |
3284 | break; | |
8d08fdba | 3285 | |
5566b478 MS |
3286 | case BREAK_STMT: |
3287 | lineno = TREE_COMPLEXITY (t); | |
3288 | emit_line_note (input_filename, lineno); | |
3289 | if (! expand_exit_something ()) | |
3290 | error ("break statement not within loop or switch"); | |
3291 | break; | |
8d08fdba | 3292 | |
6467930b MS |
3293 | case CONTINUE_STMT: |
3294 | lineno = TREE_COMPLEXITY (t); | |
3295 | emit_line_note (input_filename, lineno); | |
3296 | if (! expand_continue_loop (0)) | |
3297 | error ("continue statement not within a loop"); | |
3298 | break; | |
3299 | ||
5566b478 MS |
3300 | case SWITCH_STMT: |
3301 | { | |
3302 | tree val, tmp; | |
3303 | int cond_scope = (TREE_CODE (TREE_OPERAND (t, 0)) == DECL_STMT); | |
3304 | ||
3305 | lineno = TREE_COMPLEXITY (t); | |
3306 | if (cond_scope) | |
3307 | do_pushlevel (); | |
3308 | val = tsubst_expr (TREE_OPERAND (t, 0), args, nargs, in_decl); | |
3309 | emit_line_note (input_filename, lineno); | |
3310 | c_expand_start_case (val); | |
3311 | push_switch (); | |
3312 | ||
3313 | if (tmp = TREE_OPERAND (t, 1), tmp) | |
3314 | tsubst_expr (tmp, args, nargs, in_decl); | |
8d08fdba | 3315 | |
5566b478 MS |
3316 | expand_end_case (val); |
3317 | pop_switch (); | |
8d08fdba | 3318 | |
5566b478 MS |
3319 | if (cond_scope) |
3320 | do_poplevel (); | |
8d08fdba | 3321 | |
5566b478 MS |
3322 | finish_stmt (); |
3323 | } | |
3324 | break; | |
3325 | ||
3326 | case CASE_LABEL: | |
3327 | do_case (tsubst_expr (TREE_OPERAND (t, 0), args, nargs, in_decl), | |
3328 | tsubst_expr (TREE_OPERAND (t, 1), args, nargs, in_decl)); | |
3329 | break; | |
3330 | ||
3331 | case LABEL_DECL: | |
3332 | t = define_label (DECL_SOURCE_FILE (t), DECL_SOURCE_LINE (t), | |
3333 | DECL_NAME (t)); | |
3334 | if (t) | |
3335 | expand_label (t); | |
3336 | break; | |
3337 | ||
3338 | case GOTO_STMT: | |
3339 | lineno = TREE_COMPLEXITY (t); | |
3340 | emit_line_note (input_filename, lineno); | |
3341 | if (TREE_CODE (TREE_OPERAND (t, 0)) == IDENTIFIER_NODE) | |
3342 | { | |
3343 | tree decl = lookup_label (TREE_OPERAND (t, 0)); | |
3344 | TREE_USED (decl) = 1; | |
3345 | expand_goto (decl); | |
3346 | } | |
3347 | else | |
3348 | expand_computed_goto | |
3349 | (tsubst_expr (TREE_OPERAND (t, 0), args, nargs, in_decl)); | |
3350 | break; | |
faf5394a MS |
3351 | |
3352 | case TRY_BLOCK: | |
3353 | lineno = TREE_COMPLEXITY (t); | |
3354 | emit_line_note (input_filename, lineno); | |
3355 | expand_start_try_stmts (); | |
3356 | tsubst_expr (TREE_OPERAND (t, 0), args, nargs, in_decl); | |
3357 | expand_start_all_catch (); | |
3358 | { | |
3359 | tree handler = TREE_OPERAND (t, 1); | |
3360 | for (; handler; handler = TREE_CHAIN (handler)) | |
3361 | tsubst_expr (handler, args, nargs, in_decl); | |
3362 | } | |
3363 | expand_end_all_catch (); | |
3364 | break; | |
3365 | ||
3366 | case HANDLER: | |
3367 | lineno = TREE_COMPLEXITY (t); | |
3368 | do_pushlevel (); | |
3369 | if (TREE_OPERAND (t, 0)) | |
3370 | { | |
3371 | tree d = TREE_OPERAND (t, 0); | |
3372 | expand_start_catch_block | |
3373 | (tsubst (TREE_OPERAND (d, 1), args, nargs, in_decl), | |
3374 | tsubst (TREE_OPERAND (d, 0), args, nargs, in_decl)); | |
3375 | } | |
3376 | else | |
3377 | expand_start_catch_block (NULL_TREE, NULL_TREE); | |
3378 | tsubst_expr (TREE_OPERAND (t, 1), args, nargs, in_decl); | |
3379 | expand_end_catch_block (); | |
3380 | do_poplevel (); | |
3381 | break; | |
3382 | ||
b87692e5 MS |
3383 | case TAG_DEFN: |
3384 | lineno = TREE_COMPLEXITY (t); | |
3385 | t = TREE_TYPE (t); | |
3386 | if (TREE_CODE (t) == ENUMERAL_TYPE) | |
b3d5a58b | 3387 | tsubst_enum (t, args, nargs, NULL); |
b87692e5 MS |
3388 | break; |
3389 | ||
5566b478 MS |
3390 | default: |
3391 | return build_expr_from_tree (tsubst_copy (t, args, nargs, in_decl)); | |
3392 | } | |
3393 | return NULL_TREE; | |
8d08fdba MS |
3394 | } |
3395 | ||
5566b478 MS |
3396 | tree |
3397 | instantiate_template (tmpl, targ_ptr) | |
98c1c668 | 3398 | tree tmpl, targ_ptr; |
8d08fdba | 3399 | { |
5566b478 MS |
3400 | tree fndecl; |
3401 | int i, len; | |
3402 | struct obstack *old_fmp_obstack; | |
3403 | extern struct obstack *function_maybepermanent_obstack; | |
3404 | ||
386b8a85 JM |
3405 | my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 283); |
3406 | ||
3407 | if (DECL_FUNCTION_TEMPLATE_P (tmpl)) | |
3408 | { | |
3409 | tree specs; | |
3410 | ||
3411 | /* Check to see if there is a matching specialization. */ | |
3412 | for (specs = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); | |
3413 | specs != NULL_TREE; | |
3414 | specs = TREE_CHAIN (specs)) | |
fee23f54 | 3415 | if (comp_template_args (TREE_PURPOSE (specs), targ_ptr)) |
386b8a85 JM |
3416 | return TREE_VALUE (specs); |
3417 | } | |
3418 | ||
5566b478 MS |
3419 | push_obstacks (&permanent_obstack, &permanent_obstack); |
3420 | old_fmp_obstack = function_maybepermanent_obstack; | |
3421 | function_maybepermanent_obstack = &permanent_obstack; | |
8d08fdba | 3422 | |
98c1c668 | 3423 | len = DECL_NTPARMS (tmpl); |
8d08fdba | 3424 | |
5566b478 MS |
3425 | i = len; |
3426 | while (i--) | |
8d08fdba | 3427 | { |
98c1c668 | 3428 | tree t = TREE_VEC_ELT (targ_ptr, i); |
5566b478 MS |
3429 | if (TREE_CODE_CLASS (TREE_CODE (t)) == 't') |
3430 | { | |
3431 | tree nt = target_type (t); | |
ec255269 | 3432 | if (IS_AGGR_TYPE (nt) && decl_function_context (TYPE_MAIN_DECL (nt))) |
5566b478 MS |
3433 | { |
3434 | cp_error ("type `%T' composed from a local class is not a valid template-argument", t); | |
3435 | cp_error (" trying to instantiate `%D'", tmpl); | |
3436 | fndecl = error_mark_node; | |
3437 | goto out; | |
3438 | } | |
3439 | } | |
98c1c668 | 3440 | TREE_VEC_ELT (targ_ptr, i) = copy_to_permanent (t); |
8d08fdba MS |
3441 | } |
3442 | ||
98c1c668 JM |
3443 | if (DECL_TEMPLATE_INFO (tmpl) && DECL_TI_ARGS (tmpl)) |
3444 | targ_ptr = add_to_template_args (DECL_TI_ARGS (tmpl), targ_ptr); | |
3445 | ||
5566b478 MS |
3446 | /* substitute template parameters */ |
3447 | fndecl = tsubst (DECL_RESULT (tmpl), targ_ptr, len, tmpl); | |
8d08fdba | 3448 | |
824b9a4c MS |
3449 | if (flag_external_templates) |
3450 | add_pending_template (fndecl); | |
3451 | ||
5566b478 MS |
3452 | out: |
3453 | function_maybepermanent_obstack = old_fmp_obstack; | |
3454 | pop_obstacks (); | |
8d08fdba | 3455 | |
5566b478 | 3456 | return fndecl; |
8d08fdba | 3457 | } |
5566b478 MS |
3458 | |
3459 | /* Push the name of the class template into the scope of the instantiation. */ | |
8d08fdba MS |
3460 | |
3461 | void | |
5566b478 MS |
3462 | overload_template_name (type) |
3463 | tree type; | |
8d08fdba | 3464 | { |
5566b478 MS |
3465 | tree id = DECL_NAME (CLASSTYPE_TI_TEMPLATE (type)); |
3466 | tree decl; | |
8d08fdba | 3467 | |
5566b478 MS |
3468 | if (IDENTIFIER_CLASS_VALUE (id) |
3469 | && TREE_TYPE (IDENTIFIER_CLASS_VALUE (id)) == type) | |
3470 | return; | |
8d08fdba | 3471 | |
5566b478 MS |
3472 | decl = build_decl (TYPE_DECL, id, type); |
3473 | SET_DECL_ARTIFICIAL (decl); | |
3474 | pushdecl_class_level (decl); | |
8d08fdba MS |
3475 | } |
3476 | ||
98c1c668 JM |
3477 | /* Like type_unfication but designed specially to handle conversion |
3478 | operators. */ | |
3479 | ||
3480 | int | |
386b8a85 JM |
3481 | fn_type_unification (fn, explicit_targs, targs, args, return_type, strict) |
3482 | tree fn, explicit_targs, targs, args, return_type; | |
98c1c668 JM |
3483 | int strict; |
3484 | { | |
3485 | int i, dummy = 0; | |
3486 | tree fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn)); | |
3487 | tree decl_arg_types = args; | |
3488 | ||
3489 | my_friendly_assert (TREE_CODE (fn) == TEMPLATE_DECL, 0); | |
3490 | ||
3491 | if (IDENTIFIER_TYPENAME_P (DECL_NAME (fn))) | |
3492 | { | |
3493 | /* This is a template conversion operator. Use the return types | |
3494 | as well as the argument types. */ | |
3495 | fn_arg_types = tree_cons (NULL_TREE, | |
3496 | TREE_TYPE (TREE_TYPE (fn)), | |
3497 | fn_arg_types); | |
3498 | decl_arg_types = tree_cons (NULL_TREE, | |
3499 | return_type, | |
3500 | decl_arg_types); | |
3501 | } | |
3502 | ||
3503 | i = type_unification (DECL_INNERMOST_TEMPLATE_PARMS (fn), | |
3504 | &TREE_VEC_ELT (targs, 0), | |
3505 | fn_arg_types, | |
3506 | decl_arg_types, | |
386b8a85 JM |
3507 | explicit_targs, |
3508 | &dummy, strict, 0); | |
98c1c668 JM |
3509 | |
3510 | return i; | |
3511 | } | |
3512 | ||
3513 | ||
8d08fdba MS |
3514 | /* Type unification. |
3515 | ||
3516 | We have a function template signature with one or more references to | |
3517 | template parameters, and a parameter list we wish to fit to this | |
3518 | template. If possible, produce a list of parameters for the template | |
3519 | which will cause it to fit the supplied parameter list. | |
3520 | ||
3521 | Return zero for success, 2 for an incomplete match that doesn't resolve | |
3522 | all the types, and 1 for complete failure. An error message will be | |
3523 | printed only for an incomplete match. | |
3524 | ||
3525 | TPARMS[NTPARMS] is an array of template parameter types; | |
3526 | TARGS[NTPARMS] is the array of template parameter values. PARMS is | |
3527 | the function template's signature (using TEMPLATE_PARM_IDX nodes), | |
3528 | and ARGS is the argument list we're trying to match against it. | |
3529 | ||
3530 | If SUBR is 1, we're being called recursively (to unify the arguments of | |
3531 | a function or method parameter of a function template), so don't zero | |
6467930b MS |
3532 | out targs and don't fail on an incomplete match. |
3533 | ||
3534 | If STRICT is 1, the match must be exact (for casts of overloaded | |
3535 | addresses, explicit instantiation, and more_specialized). */ | |
8d08fdba MS |
3536 | |
3537 | int | |
386b8a85 JM |
3538 | type_unification (tparms, targs, parms, args, targs_in, nsubsts, |
3539 | strict, allow_incomplete) | |
3540 | tree tparms, *targs, parms, args, targs_in; | |
3541 | int *nsubsts, strict, allow_incomplete; | |
3542 | { | |
3543 | int ntparms = TREE_VEC_LENGTH (tparms); | |
3544 | tree t; | |
3545 | int i; | |
3546 | int r; | |
3547 | ||
3548 | bzero ((char *) targs, sizeof (tree) * ntparms); | |
3549 | ||
3550 | /* Insert any explicit template arguments. They are encoded as the | |
3551 | operands of NOP_EXPRs so that unify can tell that they are | |
3552 | explicit arguments. */ | |
3553 | for (i = 0, t = targs_in; t != NULL_TREE; t = TREE_CHAIN (t), ++i) | |
3554 | targs[i] = build1 (NOP_EXPR, NULL_TREE, TREE_VALUE (t)); | |
3555 | ||
3556 | r = type_unification_real (tparms, targs, parms, args, nsubsts, 0, | |
3557 | strict, allow_incomplete); | |
3558 | ||
3559 | for (i = 0, t = targs_in; t != NULL_TREE; t = TREE_CHAIN (t), ++i) | |
3560 | if (TREE_CODE (targs[i]) == NOP_EXPR) | |
3561 | targs[i] = TREE_OPERAND (targs[i], 0); | |
3562 | ||
3563 | return r; | |
3564 | } | |
3565 | ||
3566 | ||
3567 | int | |
3568 | type_unification_real (tparms, targs, parms, args, nsubsts, subr, | |
3569 | strict, allow_incomplete) | |
8d08fdba | 3570 | tree tparms, *targs, parms, args; |
386b8a85 | 3571 | int *nsubsts, subr, strict, allow_incomplete; |
8d08fdba MS |
3572 | { |
3573 | tree parm, arg; | |
3574 | int i; | |
3575 | int ntparms = TREE_VEC_LENGTH (tparms); | |
3576 | ||
3577 | my_friendly_assert (TREE_CODE (tparms) == TREE_VEC, 289); | |
386b8a85 JM |
3578 | my_friendly_assert (parms == NULL_TREE |
3579 | || TREE_CODE (parms) == TREE_LIST, 290); | |
51c184be | 3580 | /* ARGS could be NULL (via a call from parse.y to |
8d08fdba MS |
3581 | build_x_function_call). */ |
3582 | if (args) | |
3583 | my_friendly_assert (TREE_CODE (args) == TREE_LIST, 291); | |
3584 | my_friendly_assert (ntparms > 0, 292); | |
3585 | ||
8d08fdba MS |
3586 | while (parms |
3587 | && parms != void_list_node | |
3588 | && args | |
3589 | && args != void_list_node) | |
3590 | { | |
3591 | parm = TREE_VALUE (parms); | |
3592 | parms = TREE_CHAIN (parms); | |
3593 | arg = TREE_VALUE (args); | |
3594 | args = TREE_CHAIN (args); | |
3595 | ||
3596 | if (arg == error_mark_node) | |
3597 | return 1; | |
3598 | if (arg == unknown_type_node) | |
3599 | return 1; | |
b7484fbe | 3600 | |
03e70705 JM |
3601 | /* Conversions will be performed on a function argument that |
3602 | corresponds with a function parameter that contains only | |
3603 | non-deducible template parameters and explicitly specified | |
3604 | template parameters. */ | |
3605 | if (! uses_template_parms (parm)) | |
b7484fbe | 3606 | { |
03e70705 JM |
3607 | tree type; |
3608 | ||
3609 | if (TREE_CODE_CLASS (TREE_CODE (arg)) != 't') | |
3610 | type = TREE_TYPE (arg); | |
3611 | else | |
3612 | { | |
3613 | type = arg; | |
3614 | arg = NULL_TREE; | |
3615 | } | |
3616 | ||
3617 | if (strict) | |
3618 | { | |
3619 | if (comptypes (parm, type, 1)) | |
3620 | continue; | |
3621 | } | |
3622 | else if (arg) | |
3623 | { | |
3624 | if (can_convert_arg (parm, type, arg)) | |
3625 | continue; | |
3626 | } | |
3627 | else | |
3628 | { | |
3629 | if (can_convert (parm, type)) | |
3630 | continue; | |
3631 | } | |
3632 | ||
b7484fbe MS |
3633 | return 1; |
3634 | } | |
3635 | ||
8d08fdba MS |
3636 | #if 0 |
3637 | if (TREE_CODE (arg) == VAR_DECL) | |
3638 | arg = TREE_TYPE (arg); | |
3639 | else if (TREE_CODE_CLASS (TREE_CODE (arg)) == 'e') | |
3640 | arg = TREE_TYPE (arg); | |
3641 | #else | |
3642 | if (TREE_CODE_CLASS (TREE_CODE (arg)) != 't') | |
3643 | { | |
3644 | my_friendly_assert (TREE_TYPE (arg) != NULL_TREE, 293); | |
28cbf42c MS |
3645 | if (TREE_CODE (arg) == TREE_LIST |
3646 | && TREE_TYPE (arg) == unknown_type_node | |
3647 | && TREE_CODE (TREE_VALUE (arg)) == TEMPLATE_DECL) | |
3648 | { | |
3649 | int nsubsts, ntparms; | |
3650 | tree *targs; | |
3651 | ||
3652 | /* Have to back unify here */ | |
3653 | arg = TREE_VALUE (arg); | |
3654 | nsubsts = 0; | |
98c1c668 | 3655 | ntparms = DECL_NTPARMS (arg); |
28cbf42c MS |
3656 | targs = (tree *) alloca (sizeof (tree) * ntparms); |
3657 | parm = tree_cons (NULL_TREE, parm, NULL_TREE); | |
386b8a85 JM |
3658 | return |
3659 | type_unification (DECL_INNERMOST_TEMPLATE_PARMS (arg), | |
3660 | targs, | |
3661 | TYPE_ARG_TYPES (TREE_TYPE (arg)), | |
3662 | parm, NULL_TREE, &nsubsts, strict, | |
3663 | allow_incomplete); | |
28cbf42c | 3664 | } |
8d08fdba MS |
3665 | arg = TREE_TYPE (arg); |
3666 | } | |
3667 | #endif | |
7834ab39 | 3668 | if (! subr && TREE_CODE (arg) == REFERENCE_TYPE) |
db5ae43f MS |
3669 | arg = TREE_TYPE (arg); |
3670 | ||
7834ab39 | 3671 | if (! subr && TREE_CODE (parm) != REFERENCE_TYPE) |
4cabb798 JM |
3672 | { |
3673 | if (TREE_CODE (arg) == FUNCTION_TYPE | |
3674 | || TREE_CODE (arg) == METHOD_TYPE) | |
3675 | arg = build_pointer_type (arg); | |
3676 | else if (TREE_CODE (arg) == ARRAY_TYPE) | |
3677 | arg = build_pointer_type (TREE_TYPE (arg)); | |
3678 | else | |
3679 | arg = TYPE_MAIN_VARIANT (arg); | |
3680 | } | |
8d08fdba | 3681 | |
6467930b | 3682 | switch (unify (tparms, targs, ntparms, parm, arg, nsubsts, strict)) |
8d08fdba MS |
3683 | { |
3684 | case 0: | |
3685 | break; | |
3686 | case 1: | |
3687 | return 1; | |
3688 | } | |
3689 | } | |
3690 | /* Fail if we've reached the end of the parm list, and more args | |
3691 | are present, and the parm list isn't variadic. */ | |
3692 | if (args && args != void_list_node && parms == void_list_node) | |
3693 | return 1; | |
3694 | /* Fail if parms are left and they don't have default values. */ | |
3695 | if (parms | |
3696 | && parms != void_list_node | |
3697 | && TREE_PURPOSE (parms) == NULL_TREE) | |
3698 | return 1; | |
3699 | if (!subr) | |
3700 | for (i = 0; i < ntparms; i++) | |
3701 | if (!targs[i]) | |
3702 | { | |
386b8a85 JM |
3703 | if (!allow_incomplete) |
3704 | error ("incomplete type unification"); | |
8d08fdba MS |
3705 | return 2; |
3706 | } | |
3707 | return 0; | |
3708 | } | |
3709 | ||
3710 | /* Tail recursion is your friend. */ | |
e92cc029 | 3711 | |
8d08fdba | 3712 | static int |
6467930b | 3713 | unify (tparms, targs, ntparms, parm, arg, nsubsts, strict) |
8d08fdba | 3714 | tree tparms, *targs, parm, arg; |
6467930b | 3715 | int *nsubsts, ntparms, strict; |
8d08fdba MS |
3716 | { |
3717 | int idx; | |
3718 | ||
3719 | /* I don't think this will do the right thing with respect to types. | |
3720 | But the only case I've seen it in so far has been array bounds, where | |
3721 | signedness is the only information lost, and I think that will be | |
3722 | okay. */ | |
3723 | while (TREE_CODE (parm) == NOP_EXPR) | |
3724 | parm = TREE_OPERAND (parm, 0); | |
3725 | ||
3726 | if (arg == error_mark_node) | |
3727 | return 1; | |
3728 | if (arg == unknown_type_node) | |
3729 | return 1; | |
3730 | if (arg == parm) | |
3731 | return 0; | |
3732 | ||
8d08fdba MS |
3733 | switch (TREE_CODE (parm)) |
3734 | { | |
2ca340ae JM |
3735 | case TYPENAME_TYPE: |
3736 | /* In a type which contains a nested-name-specifier, template | |
3737 | argument values cannot be deduced for template parameters used | |
3738 | within the nested-name-specifier. */ | |
3739 | return 0; | |
3740 | ||
8d08fdba MS |
3741 | case TEMPLATE_TYPE_PARM: |
3742 | (*nsubsts)++; | |
8d08fdba | 3743 | idx = TEMPLATE_TYPE_IDX (parm); |
386b8a85 JM |
3744 | /* Check for mixed types and values. */ |
3745 | if (TREE_CODE (TREE_VALUE (TREE_VEC_ELT (tparms, idx))) != TYPE_DECL) | |
3746 | return 1; | |
3747 | ||
3748 | if (!strict && targs[idx] != NULL_TREE && | |
3749 | TREE_CODE (targs[idx]) == NOP_EXPR) | |
3750 | /* An explicit template argument. Don't even try to match | |
3751 | here; the overload resolution code will manage check to | |
3752 | see whether the call is legal. */ | |
3753 | return 0; | |
3754 | ||
6467930b MS |
3755 | if (strict && (TYPE_READONLY (arg) < TYPE_READONLY (parm) |
3756 | || TYPE_VOLATILE (arg) < TYPE_VOLATILE (parm))) | |
3757 | return 1; | |
db5ae43f | 3758 | #if 0 |
a292b002 MS |
3759 | /* Template type parameters cannot contain cv-quals; i.e. |
3760 | template <class T> void f (T& a, T& b) will not generate | |
3761 | void f (const int& a, const int& b). */ | |
3762 | if (TYPE_READONLY (arg) > TYPE_READONLY (parm) | |
3763 | || TYPE_VOLATILE (arg) > TYPE_VOLATILE (parm)) | |
3764 | return 1; | |
3765 | arg = TYPE_MAIN_VARIANT (arg); | |
db5ae43f MS |
3766 | #else |
3767 | { | |
3768 | int constp = TYPE_READONLY (arg) > TYPE_READONLY (parm); | |
3769 | int volatilep = TYPE_VOLATILE (arg) > TYPE_VOLATILE (parm); | |
3770 | arg = cp_build_type_variant (arg, constp, volatilep); | |
3771 | } | |
3772 | #endif | |
8d08fdba | 3773 | /* Simple cases: Value already set, does match or doesn't. */ |
386b8a85 JM |
3774 | if (targs[idx] == arg |
3775 | || (targs[idx] | |
3776 | && TREE_CODE (targs[idx]) == NOP_EXPR | |
3777 | && TREE_OPERAND (targs[idx], 0) == arg)) | |
8d08fdba MS |
3778 | return 0; |
3779 | else if (targs[idx]) | |
8d08fdba MS |
3780 | return 1; |
3781 | targs[idx] = arg; | |
3782 | return 0; | |
3783 | case TEMPLATE_CONST_PARM: | |
3784 | (*nsubsts)++; | |
3785 | idx = TEMPLATE_CONST_IDX (parm); | |
312e7d50 | 3786 | if (targs[idx]) |
8d08fdba | 3787 | { |
312e7d50 JM |
3788 | int i = cp_tree_equal (targs[idx], arg); |
3789 | if (i == 1) | |
3790 | return 0; | |
3791 | else if (i == 0) | |
3792 | return 1; | |
3793 | else | |
3794 | my_friendly_abort (42); | |
8d08fdba | 3795 | } |
8d08fdba MS |
3796 | |
3797 | targs[idx] = copy_to_permanent (arg); | |
3798 | return 0; | |
3799 | ||
3800 | case POINTER_TYPE: | |
4ac14744 MS |
3801 | if (TREE_CODE (arg) == RECORD_TYPE && TYPE_PTRMEMFUNC_FLAG (arg)) |
3802 | return unify (tparms, targs, ntparms, parm, | |
6467930b | 3803 | TYPE_PTRMEMFUNC_FN_TYPE (arg), nsubsts, strict); |
4ac14744 | 3804 | |
8d08fdba MS |
3805 | if (TREE_CODE (arg) != POINTER_TYPE) |
3806 | return 1; | |
3807 | return unify (tparms, targs, ntparms, TREE_TYPE (parm), TREE_TYPE (arg), | |
6467930b | 3808 | nsubsts, strict); |
8d08fdba MS |
3809 | |
3810 | case REFERENCE_TYPE: | |
28cbf42c MS |
3811 | if (TREE_CODE (arg) == REFERENCE_TYPE) |
3812 | arg = TREE_TYPE (arg); | |
6467930b MS |
3813 | return unify (tparms, targs, ntparms, TREE_TYPE (parm), arg, |
3814 | nsubsts, strict); | |
8d08fdba MS |
3815 | |
3816 | case ARRAY_TYPE: | |
3817 | if (TREE_CODE (arg) != ARRAY_TYPE) | |
3818 | return 1; | |
3819 | if (unify (tparms, targs, ntparms, TYPE_DOMAIN (parm), TYPE_DOMAIN (arg), | |
6467930b | 3820 | nsubsts, strict) != 0) |
8d08fdba MS |
3821 | return 1; |
3822 | return unify (tparms, targs, ntparms, TREE_TYPE (parm), TREE_TYPE (arg), | |
6467930b | 3823 | nsubsts, strict); |
8d08fdba MS |
3824 | |
3825 | case REAL_TYPE: | |
37c46b43 | 3826 | case COMPLEX_TYPE: |
8d08fdba | 3827 | case INTEGER_TYPE: |
42976354 | 3828 | case BOOLEAN_TYPE: |
f376e137 MS |
3829 | if (TREE_CODE (arg) != TREE_CODE (parm)) |
3830 | return 1; | |
3831 | ||
3832 | if (TREE_CODE (parm) == INTEGER_TYPE) | |
8d08fdba MS |
3833 | { |
3834 | if (TYPE_MIN_VALUE (parm) && TYPE_MIN_VALUE (arg) | |
6467930b MS |
3835 | && unify (tparms, targs, ntparms, TYPE_MIN_VALUE (parm), |
3836 | TYPE_MIN_VALUE (arg), nsubsts, strict)) | |
8d08fdba MS |
3837 | return 1; |
3838 | if (TYPE_MAX_VALUE (parm) && TYPE_MAX_VALUE (arg) | |
6467930b MS |
3839 | && unify (tparms, targs, ntparms, TYPE_MAX_VALUE (parm), |
3840 | TYPE_MAX_VALUE (arg), nsubsts, strict)) | |
8d08fdba MS |
3841 | return 1; |
3842 | } | |
ca79f85d JM |
3843 | else if (TREE_CODE (parm) == REAL_TYPE |
3844 | && TYPE_MAIN_VARIANT (arg) != TYPE_MAIN_VARIANT (parm)) | |
3845 | return 1; | |
3846 | ||
8d08fdba MS |
3847 | /* As far as unification is concerned, this wins. Later checks |
3848 | will invalidate it if necessary. */ | |
3849 | return 0; | |
3850 | ||
3851 | /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */ | |
bd6dd845 | 3852 | /* Type INTEGER_CST can come from ordinary constant template args. */ |
8d08fdba | 3853 | case INTEGER_CST: |
bd6dd845 MS |
3854 | while (TREE_CODE (arg) == NOP_EXPR) |
3855 | arg = TREE_OPERAND (arg, 0); | |
3856 | ||
8d08fdba MS |
3857 | if (TREE_CODE (arg) != INTEGER_CST) |
3858 | return 1; | |
3859 | return !tree_int_cst_equal (parm, arg); | |
3860 | ||
3861 | case MINUS_EXPR: | |
3862 | { | |
3863 | tree t1, t2; | |
3864 | t1 = TREE_OPERAND (parm, 0); | |
3865 | t2 = TREE_OPERAND (parm, 1); | |
8d08fdba MS |
3866 | return unify (tparms, targs, ntparms, t1, |
3867 | fold (build (PLUS_EXPR, integer_type_node, arg, t2)), | |
6467930b | 3868 | nsubsts, strict); |
8d08fdba MS |
3869 | } |
3870 | ||
3871 | case TREE_VEC: | |
3872 | { | |
3873 | int i; | |
3874 | if (TREE_CODE (arg) != TREE_VEC) | |
3875 | return 1; | |
3876 | if (TREE_VEC_LENGTH (parm) != TREE_VEC_LENGTH (arg)) | |
3877 | return 1; | |
3878 | for (i = TREE_VEC_LENGTH (parm) - 1; i >= 0; i--) | |
3879 | if (unify (tparms, targs, ntparms, | |
3880 | TREE_VEC_ELT (parm, i), TREE_VEC_ELT (arg, i), | |
6467930b | 3881 | nsubsts, strict)) |
8d08fdba MS |
3882 | return 1; |
3883 | return 0; | |
3884 | } | |
3885 | ||
8d08fdba | 3886 | case RECORD_TYPE: |
db5ae43f | 3887 | if (TYPE_PTRMEMFUNC_FLAG (parm)) |
8d08fdba | 3888 | return unify (tparms, targs, ntparms, TYPE_PTRMEMFUNC_FN_TYPE (parm), |
6467930b | 3889 | arg, nsubsts, strict); |
8d08fdba | 3890 | |
a4443a08 | 3891 | /* Allow trivial conversions. */ |
5566b478 | 3892 | if (TREE_CODE (arg) != RECORD_TYPE |
a4443a08 MS |
3893 | || TYPE_READONLY (parm) < TYPE_READONLY (arg) |
3894 | || TYPE_VOLATILE (parm) < TYPE_VOLATILE (arg)) | |
3895 | return 1; | |
5566b478 | 3896 | |
6467930b | 3897 | if (CLASSTYPE_TEMPLATE_INFO (parm) && uses_template_parms (parm)) |
5566b478 | 3898 | { |
6467930b | 3899 | tree t = NULL_TREE; |
c73964b2 | 3900 | if (flag_ansi_overloading && ! strict) |
6467930b | 3901 | t = get_template_base (CLASSTYPE_TI_TEMPLATE (parm), arg); |
c73964b2 MS |
3902 | else if |
3903 | (CLASSTYPE_TEMPLATE_INFO (arg) | |
3904 | && CLASSTYPE_TI_TEMPLATE (parm) == CLASSTYPE_TI_TEMPLATE (arg)) | |
6467930b MS |
3905 | t = arg; |
3906 | if (! t || t == error_mark_node) | |
5566b478 | 3907 | return 1; |
6467930b | 3908 | |
5566b478 | 3909 | return unify (tparms, targs, ntparms, CLASSTYPE_TI_ARGS (parm), |
6467930b | 3910 | CLASSTYPE_TI_ARGS (t), nsubsts, strict); |
5566b478 MS |
3911 | } |
3912 | else if (TYPE_MAIN_VARIANT (parm) != TYPE_MAIN_VARIANT (arg)) | |
3913 | return 1; | |
a4443a08 | 3914 | return 0; |
8d08fdba MS |
3915 | |
3916 | case METHOD_TYPE: | |
3917 | if (TREE_CODE (arg) != METHOD_TYPE) | |
3918 | return 1; | |
3919 | goto check_args; | |
3920 | ||
3921 | case FUNCTION_TYPE: | |
3922 | if (TREE_CODE (arg) != FUNCTION_TYPE) | |
3923 | return 1; | |
3924 | check_args: | |
28cbf42c | 3925 | if (unify (tparms, targs, ntparms, TREE_TYPE (parm), |
6467930b | 3926 | TREE_TYPE (arg), nsubsts, strict)) |
28cbf42c | 3927 | return 1; |
386b8a85 JM |
3928 | return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm), |
3929 | TYPE_ARG_TYPES (arg), nsubsts, 1, | |
3930 | strict, 0); | |
a4443a08 MS |
3931 | |
3932 | case OFFSET_TYPE: | |
3933 | if (TREE_CODE (arg) != OFFSET_TYPE) | |
3934 | return 1; | |
3935 | if (unify (tparms, targs, ntparms, TYPE_OFFSET_BASETYPE (parm), | |
6467930b | 3936 | TYPE_OFFSET_BASETYPE (arg), nsubsts, strict)) |
a4443a08 MS |
3937 | return 1; |
3938 | return unify (tparms, targs, ntparms, TREE_TYPE (parm), | |
6467930b | 3939 | TREE_TYPE (arg), nsubsts, strict); |
a4443a08 | 3940 | |
f62dbf03 JM |
3941 | case CONST_DECL: |
3942 | if (arg != decl_constant_value (parm)) | |
3943 | return 1; | |
3944 | return 0; | |
3945 | ||
8d08fdba MS |
3946 | default: |
3947 | sorry ("use of `%s' in template type unification", | |
3948 | tree_code_name [(int) TREE_CODE (parm)]); | |
3949 | return 1; | |
3950 | } | |
3951 | } | |
8d08fdba | 3952 | \f |
faae18ab | 3953 | void |
5566b478 | 3954 | mark_decl_instantiated (result, extern_p) |
faae18ab MS |
3955 | tree result; |
3956 | int extern_p; | |
3957 | { | |
3958 | if (DECL_TEMPLATE_INSTANTIATION (result)) | |
3959 | SET_DECL_EXPLICIT_INSTANTIATION (result); | |
3960 | TREE_PUBLIC (result) = 1; | |
3961 | ||
3962 | if (! extern_p) | |
3963 | { | |
3964 | DECL_INTERFACE_KNOWN (result) = 1; | |
3965 | DECL_NOT_REALLY_EXTERN (result) = 1; | |
3966 | } | |
f49422da MS |
3967 | else if (TREE_CODE (result) == FUNCTION_DECL) |
3968 | mark_inline_for_output (result); | |
faae18ab MS |
3969 | } |
3970 | ||
6467930b MS |
3971 | /* Given two function templates PAT1 and PAT2, return: |
3972 | ||
3973 | 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order]. | |
3974 | -1 if PAT2 is more specialized than PAT1. | |
3975 | 0 if neither is more specialized. */ | |
3976 | ||
3977 | int | |
3978 | more_specialized (pat1, pat2) | |
3979 | tree pat1, pat2; | |
3980 | { | |
98c1c668 | 3981 | tree targs; |
73aad9b9 | 3982 | int winner = 0; |
6467930b | 3983 | |
73aad9b9 JM |
3984 | targs = get_bindings (pat1, pat2); |
3985 | if (targs) | |
3986 | { | |
73aad9b9 JM |
3987 | --winner; |
3988 | } | |
6467930b | 3989 | |
73aad9b9 JM |
3990 | targs = get_bindings (pat2, pat1); |
3991 | if (targs) | |
3992 | { | |
73aad9b9 JM |
3993 | ++winner; |
3994 | } | |
6467930b | 3995 | |
73aad9b9 JM |
3996 | return winner; |
3997 | } | |
6467930b | 3998 | |
73aad9b9 | 3999 | /* Given two class template specialization list nodes PAT1 and PAT2, return: |
6467930b | 4000 | |
73aad9b9 JM |
4001 | 1 if PAT1 is more specialized than PAT2 as described in [temp.class.order]. |
4002 | -1 if PAT2 is more specialized than PAT1. | |
4003 | 0 if neither is more specialized. */ | |
4004 | ||
4005 | int | |
4006 | more_specialized_class (pat1, pat2) | |
4007 | tree pat1, pat2; | |
4008 | { | |
4009 | tree targs; | |
4010 | int winner = 0; | |
4011 | ||
4012 | targs = get_class_bindings | |
4013 | (TREE_VALUE (pat1), TREE_PURPOSE (pat1), TREE_PURPOSE (pat2)); | |
4014 | if (targs) | |
4015 | --winner; | |
4016 | ||
4017 | targs = get_class_bindings | |
4018 | (TREE_VALUE (pat2), TREE_PURPOSE (pat2), TREE_PURPOSE (pat1)); | |
4019 | if (targs) | |
6467930b MS |
4020 | ++winner; |
4021 | ||
4022 | return winner; | |
4023 | } | |
73aad9b9 JM |
4024 | |
4025 | /* Return the template arguments that will produce the function signature | |
4026 | DECL from the function template FN. */ | |
4027 | ||
98c1c668 | 4028 | tree |
73aad9b9 JM |
4029 | get_bindings (fn, decl) |
4030 | tree fn, decl; | |
4031 | { | |
98c1c668 JM |
4032 | int ntparms = DECL_NTPARMS (fn); |
4033 | tree targs = make_tree_vec (ntparms); | |
4034 | int i; | |
4035 | ||
386b8a85 | 4036 | i = fn_type_unification (fn, NULL_TREE, targs, |
98c1c668 JM |
4037 | TYPE_ARG_TYPES (TREE_TYPE (decl)), |
4038 | TREE_TYPE (TREE_TYPE (decl)), | |
4039 | 1); | |
4040 | ||
73aad9b9 JM |
4041 | if (i == 0) |
4042 | return targs; | |
73aad9b9 JM |
4043 | return 0; |
4044 | } | |
4045 | ||
bd6dd845 | 4046 | static tree |
73aad9b9 JM |
4047 | get_class_bindings (tparms, parms, args) |
4048 | tree tparms, parms, args; | |
4049 | { | |
4050 | int i, dummy, ntparms = TREE_VEC_LENGTH (tparms); | |
4051 | tree vec = make_temp_vec (ntparms); | |
4052 | ||
4053 | for (i = 0; i < TREE_VEC_LENGTH (parms); ++i) | |
4054 | { | |
4055 | switch (unify (tparms, &TREE_VEC_ELT (vec, 0), ntparms, | |
4056 | TREE_VEC_ELT (parms, i), TREE_VEC_ELT (args, i), | |
4057 | &dummy, 1)) | |
4058 | { | |
4059 | case 0: | |
4060 | break; | |
4061 | case 1: | |
4062 | return NULL_TREE; | |
4063 | } | |
4064 | } | |
4065 | ||
4066 | for (i = 0; i < ntparms; ++i) | |
4067 | if (! TREE_VEC_ELT (vec, i)) | |
4068 | return NULL_TREE; | |
4069 | ||
4070 | return vec; | |
4071 | } | |
4072 | ||
4073 | /* Return the most specialized of the list of templates in FNS that can | |
4074 | produce an instantiation matching DECL. */ | |
4075 | ||
4076 | tree | |
4077 | most_specialized (fns, decl) | |
4078 | tree fns, decl; | |
4079 | { | |
98c1c668 | 4080 | tree fn, champ, args, *p; |
73aad9b9 JM |
4081 | int fate; |
4082 | ||
4083 | for (p = &fns; *p; ) | |
4084 | { | |
4085 | args = get_bindings (TREE_VALUE (*p), decl); | |
4086 | if (args) | |
4087 | { | |
73aad9b9 JM |
4088 | p = &TREE_CHAIN (*p); |
4089 | } | |
4090 | else | |
4091 | *p = TREE_CHAIN (*p); | |
4092 | } | |
4093 | ||
4094 | if (! fns) | |
4095 | return NULL_TREE; | |
4096 | ||
4097 | fn = fns; | |
4098 | champ = TREE_VALUE (fn); | |
4099 | fn = TREE_CHAIN (fn); | |
4100 | for (; fn; fn = TREE_CHAIN (fn)) | |
4101 | { | |
4102 | fate = more_specialized (champ, TREE_VALUE (fn)); | |
4103 | if (fate == 1) | |
4104 | ; | |
4105 | else | |
4106 | { | |
4107 | if (fate == 0) | |
4108 | { | |
4109 | fn = TREE_CHAIN (fn); | |
4110 | if (! fn) | |
4111 | return error_mark_node; | |
4112 | } | |
4113 | champ = TREE_VALUE (fn); | |
4114 | } | |
4115 | } | |
4116 | ||
4117 | for (fn = fns; fn && TREE_VALUE (fn) != champ; fn = TREE_CHAIN (fn)) | |
4118 | { | |
4119 | fate = more_specialized (champ, TREE_VALUE (fn)); | |
4120 | if (fate != 1) | |
4121 | return error_mark_node; | |
4122 | } | |
4123 | ||
4124 | return champ; | |
4125 | } | |
4126 | ||
4127 | /* Return the most specialized of the class template specializations in | |
4128 | SPECS that can produce an instantiation matching ARGS. */ | |
4129 | ||
4130 | tree | |
4131 | most_specialized_class (specs, mainargs) | |
4132 | tree specs, mainargs; | |
4133 | { | |
4134 | tree list = NULL_TREE, t, args, champ; | |
4135 | int fate; | |
4136 | ||
4137 | for (t = specs; t; t = TREE_CHAIN (t)) | |
4138 | { | |
4139 | args = get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t), mainargs); | |
4140 | if (args) | |
4141 | { | |
4142 | list = decl_tree_cons (TREE_PURPOSE (t), TREE_VALUE (t), list); | |
4143 | TREE_TYPE (list) = TREE_TYPE (t); | |
4144 | } | |
4145 | } | |
4146 | ||
4147 | if (! list) | |
4148 | return NULL_TREE; | |
4149 | ||
4150 | t = list; | |
4151 | champ = t; | |
4152 | t = TREE_CHAIN (t); | |
4153 | for (; t; t = TREE_CHAIN (t)) | |
4154 | { | |
4155 | fate = more_specialized_class (champ, t); | |
4156 | if (fate == 1) | |
4157 | ; | |
4158 | else | |
4159 | { | |
4160 | if (fate == 0) | |
4161 | { | |
4162 | t = TREE_CHAIN (t); | |
4163 | if (! t) | |
4164 | return error_mark_node; | |
4165 | } | |
4166 | champ = t; | |
4167 | } | |
4168 | } | |
4169 | ||
4170 | for (t = list; t && t != champ; t = TREE_CHAIN (t)) | |
4171 | { | |
85b71cf2 | 4172 | fate = more_specialized_class (champ, t); |
73aad9b9 JM |
4173 | if (fate != 1) |
4174 | return error_mark_node; | |
4175 | } | |
4176 | ||
4177 | return champ; | |
4178 | } | |
4179 | ||
8d08fdba | 4180 | /* called from the parser. */ |
e92cc029 | 4181 | |
8d08fdba | 4182 | void |
6633d636 | 4183 | do_decl_instantiation (declspecs, declarator, storage) |
f0e01782 | 4184 | tree declspecs, declarator, storage; |
8d08fdba | 4185 | { |
c11b6f21 | 4186 | tree decl = grokdeclarator (declarator, declspecs, NORMAL, 0, NULL_TREE); |
e8abc66f MS |
4187 | tree name; |
4188 | tree fn; | |
8d08fdba | 4189 | tree result = NULL_TREE; |
faae18ab | 4190 | int extern_p = 0; |
98c1c668 | 4191 | tree templates = NULL_TREE; |
e8abc66f | 4192 | |
ec255269 MS |
4193 | if (! DECL_LANG_SPECIFIC (decl)) |
4194 | { | |
4195 | cp_error ("explicit instantiation of non-template `%#D'", decl); | |
4196 | return; | |
4197 | } | |
4198 | ||
e8abc66f | 4199 | /* If we've already seen this template instance, use it. */ |
6633d636 MS |
4200 | if (TREE_CODE (decl) == VAR_DECL) |
4201 | { | |
4202 | result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, 0); | |
4203 | if (result && TREE_CODE (result) != VAR_DECL) | |
4204 | result = NULL_TREE; | |
4205 | } | |
4206 | else if (TREE_CODE (decl) != FUNCTION_DECL) | |
4207 | { | |
4208 | cp_error ("explicit instantiation of `%#D'", decl); | |
4209 | return; | |
4210 | } | |
4211 | else if (DECL_FUNCTION_MEMBER_P (decl)) | |
c91a56d2 MS |
4212 | { |
4213 | if (DECL_TEMPLATE_INSTANTIATION (decl)) | |
4214 | result = decl; | |
4215 | else if (name = DECL_ASSEMBLER_NAME (decl), | |
4216 | fn = IDENTIFIER_GLOBAL_VALUE (name), | |
4217 | fn && DECL_TEMPLATE_INSTANTIATION (fn)) | |
4218 | result = fn; | |
98c1c668 JM |
4219 | else |
4220 | { | |
4221 | /* Maybe this is an instantiation of a member template | |
4222 | function. */ | |
4223 | tree ctype = DECL_CONTEXT (decl); | |
4224 | ||
4225 | name = DECL_NAME (decl); | |
4226 | fn = lookup_fnfields (TYPE_BINFO (ctype), name, 1); | |
4227 | if (fn) | |
4228 | fn = TREE_VALUE (fn); | |
4229 | ||
4230 | for (; fn; fn = DECL_CHAIN (fn)) | |
c32381b1 | 4231 | if (TREE_CODE (fn) == TEMPLATE_DECL) |
98c1c668 JM |
4232 | templates = decl_tree_cons (NULL_TREE, fn, templates); |
4233 | } | |
c91a56d2 | 4234 | } |
e8abc66f | 4235 | else if (name = DECL_NAME (decl), fn = IDENTIFIER_GLOBAL_VALUE (name), fn) |
8d08fdba MS |
4236 | { |
4237 | for (fn = get_first_fn (fn); fn; fn = DECL_CHAIN (fn)) | |
c32381b1 | 4238 | if (TREE_CODE (fn) == TEMPLATE_DECL) |
73aad9b9 | 4239 | templates = decl_tree_cons (NULL_TREE, fn, templates); |
98c1c668 | 4240 | } |
73aad9b9 | 4241 | |
98c1c668 JM |
4242 | if (templates && !result) |
4243 | { | |
4244 | tree args; | |
4245 | result = most_specialized (templates, decl); | |
4246 | if (result == error_mark_node) | |
73aad9b9 | 4247 | { |
98c1c668 JM |
4248 | char *str = "candidates are:"; |
4249 | cp_error ("ambiguous template instantiation for `%D' requested", decl); | |
4250 | for (fn = templates; fn; fn = TREE_CHAIN (fn)) | |
73aad9b9 | 4251 | { |
98c1c668 JM |
4252 | cp_error_at ("%s %+#D", str, TREE_VALUE (fn)); |
4253 | str = " "; | |
73aad9b9 | 4254 | } |
98c1c668 JM |
4255 | return; |
4256 | } | |
4257 | else if (result) | |
4258 | { | |
4259 | args = get_bindings (result, decl); | |
4260 | result = instantiate_template (result, args); | |
73aad9b9 | 4261 | } |
8d08fdba | 4262 | } |
98c1c668 | 4263 | |
7177d104 | 4264 | if (! result) |
faae18ab MS |
4265 | { |
4266 | cp_error ("no matching template for `%D' found", decl); | |
4267 | return; | |
4268 | } | |
7177d104 | 4269 | |
6633d636 MS |
4270 | if (! DECL_TEMPLATE_INFO (result)) |
4271 | { | |
4272 | cp_pedwarn ("explicit instantiation of non-template `%#D'", result); | |
4273 | return; | |
4274 | } | |
4275 | ||
a0a33927 MS |
4276 | if (flag_external_templates) |
4277 | return; | |
4278 | ||
f0e01782 | 4279 | if (storage == NULL_TREE) |
00595019 | 4280 | ; |
faae18ab MS |
4281 | else if (storage == ridpointers[(int) RID_EXTERN]) |
4282 | extern_p = 1; | |
f0e01782 MS |
4283 | else |
4284 | cp_error ("storage class `%D' applied to template instantiation", | |
4285 | storage); | |
5566b478 | 4286 | |
5566b478 | 4287 | mark_decl_instantiated (result, extern_p); |
44a8d0b3 | 4288 | repo_template_instantiated (result, extern_p); |
c91a56d2 MS |
4289 | if (! extern_p) |
4290 | instantiate_decl (result); | |
7177d104 MS |
4291 | } |
4292 | ||
faae18ab MS |
4293 | void |
4294 | mark_class_instantiated (t, extern_p) | |
4295 | tree t; | |
4296 | int extern_p; | |
4297 | { | |
4298 | SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t); | |
4299 | SET_CLASSTYPE_INTERFACE_KNOWN (t); | |
4300 | CLASSTYPE_INTERFACE_ONLY (t) = extern_p; | |
4301 | CLASSTYPE_VTABLE_NEEDS_WRITING (t) = ! extern_p; | |
4302 | TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p; | |
4303 | if (! extern_p) | |
4304 | { | |
4305 | CLASSTYPE_DEBUG_REQUESTED (t) = 1; | |
4306 | rest_of_type_compilation (t, 1); | |
4307 | } | |
4308 | } | |
e8abc66f | 4309 | |
7177d104 | 4310 | void |
ca79f85d JM |
4311 | do_type_instantiation (t, storage) |
4312 | tree t, storage; | |
7177d104 | 4313 | { |
e8abc66f MS |
4314 | int extern_p = 0; |
4315 | int nomem_p = 0; | |
5566b478 MS |
4316 | int static_p = 0; |
4317 | ||
ca79f85d JM |
4318 | if (TREE_CODE (t) == TYPE_DECL) |
4319 | t = TREE_TYPE (t); | |
4320 | ||
4321 | if (! IS_AGGR_TYPE (t) || ! CLASSTYPE_TEMPLATE_INFO (t)) | |
4322 | { | |
4323 | cp_error ("explicit instantiation of non-template type `%T'", t); | |
4324 | return; | |
4325 | } | |
4326 | ||
5566b478 | 4327 | complete_type (t); |
7177d104 | 4328 | |
a292b002 MS |
4329 | /* With -fexternal-templates, explicit instantiations are treated the same |
4330 | as implicit ones. */ | |
a0a33927 MS |
4331 | if (flag_external_templates) |
4332 | return; | |
4333 | ||
f0e01782 MS |
4334 | if (TYPE_SIZE (t) == NULL_TREE) |
4335 | { | |
4336 | cp_error ("explicit instantiation of `%#T' before definition of template", | |
4337 | t); | |
4338 | return; | |
4339 | } | |
4340 | ||
4341 | if (storage == NULL_TREE) | |
e8abc66f MS |
4342 | /* OK */; |
4343 | else if (storage == ridpointers[(int) RID_INLINE]) | |
4344 | nomem_p = 1; | |
f0e01782 MS |
4345 | else if (storage == ridpointers[(int) RID_EXTERN]) |
4346 | extern_p = 1; | |
5566b478 MS |
4347 | else if (storage == ridpointers[(int) RID_STATIC]) |
4348 | static_p = 1; | |
f0e01782 MS |
4349 | else |
4350 | { | |
4351 | cp_error ("storage class `%D' applied to template instantiation", | |
4352 | storage); | |
4353 | extern_p = 0; | |
4354 | } | |
4355 | ||
a292b002 | 4356 | /* We've already instantiated this. */ |
44a8d0b3 MS |
4357 | if (CLASSTYPE_EXPLICIT_INSTANTIATION (t) && ! CLASSTYPE_INTERFACE_ONLY (t) |
4358 | && extern_p) | |
4359 | return; | |
a292b002 | 4360 | |
f376e137 | 4361 | if (! CLASSTYPE_TEMPLATE_SPECIALIZATION (t)) |
44a8d0b3 MS |
4362 | { |
4363 | mark_class_instantiated (t, extern_p); | |
4364 | repo_template_instantiated (t, extern_p); | |
4365 | } | |
e8abc66f MS |
4366 | |
4367 | if (nomem_p) | |
4368 | return; | |
4369 | ||
7177d104 | 4370 | { |
db5ae43f | 4371 | tree tmp; |
5566b478 MS |
4372 | |
4373 | if (! static_p) | |
4374 | for (tmp = TYPE_METHODS (t); tmp; tmp = TREE_CHAIN (tmp)) | |
4375 | if (DECL_TEMPLATE_INSTANTIATION (tmp)) | |
4376 | { | |
4377 | mark_decl_instantiated (tmp, extern_p); | |
4378 | repo_template_instantiated (tmp, extern_p); | |
4379 | if (! extern_p) | |
4380 | instantiate_decl (tmp); | |
4381 | } | |
4382 | ||
4383 | for (tmp = TYPE_FIELDS (t); tmp; tmp = TREE_CHAIN (tmp)) | |
4384 | if (TREE_CODE (tmp) == VAR_DECL && DECL_TEMPLATE_INSTANTIATION (tmp)) | |
863adfc0 | 4385 | { |
5566b478 | 4386 | mark_decl_instantiated (tmp, extern_p); |
863adfc0 | 4387 | repo_template_instantiated (tmp, extern_p); |
5566b478 MS |
4388 | if (! extern_p) |
4389 | instantiate_decl (tmp); | |
863adfc0 | 4390 | } |
7177d104 | 4391 | |
a292b002 | 4392 | for (tmp = CLASSTYPE_TAGS (t); tmp; tmp = TREE_CHAIN (tmp)) |
f376e137 MS |
4393 | if (IS_AGGR_TYPE (TREE_VALUE (tmp))) |
4394 | do_type_instantiation (TYPE_MAIN_DECL (TREE_VALUE (tmp)), storage); | |
a292b002 | 4395 | } |
8d08fdba | 4396 | } |
a28e3c7f MS |
4397 | |
4398 | tree | |
5566b478 MS |
4399 | instantiate_decl (d) |
4400 | tree d; | |
a28e3c7f | 4401 | { |
5566b478 MS |
4402 | tree ti = DECL_TEMPLATE_INFO (d); |
4403 | tree tmpl = TI_TEMPLATE (ti); | |
4404 | tree args = TI_ARGS (ti); | |
4405 | tree td; | |
fee23f54 | 4406 | tree decl_pattern, code_pattern; |
5566b478 MS |
4407 | tree save_ti; |
4408 | int nested = in_function_p (); | |
4409 | int d_defined; | |
4410 | int pattern_defined; | |
5156628f MS |
4411 | int line = lineno; |
4412 | char *file = input_filename; | |
5566b478 | 4413 | |
fee23f54 JM |
4414 | if (DECL_TEMPLATE_SPECIALIZATION (d)) |
4415 | return d; | |
4416 | ||
4417 | for (td = tmpl; DECL_TEMPLATE_INSTANTIATION (td); ) | |
4418 | td = DECL_TI_TEMPLATE (td); | |
27bb8339 | 4419 | |
fee23f54 JM |
4420 | /* In the case of a member template, decl_pattern is the partially |
4421 | instantiated declaration (in the instantiated class), and code_pattern | |
4422 | is the original template definition. */ | |
4423 | decl_pattern = DECL_TEMPLATE_RESULT (tmpl); | |
4424 | code_pattern = DECL_TEMPLATE_RESULT (td); | |
27bb8339 | 4425 | |
5566b478 MS |
4426 | if (TREE_CODE (d) == FUNCTION_DECL) |
4427 | { | |
4428 | d_defined = (DECL_INITIAL (d) != NULL_TREE); | |
fee23f54 | 4429 | pattern_defined = (DECL_INITIAL (code_pattern) != NULL_TREE); |
5566b478 MS |
4430 | } |
4431 | else | |
4432 | { | |
4433 | d_defined = ! DECL_IN_AGGR_P (d); | |
fee23f54 | 4434 | pattern_defined = ! DECL_IN_AGGR_P (code_pattern); |
5566b478 MS |
4435 | } |
4436 | ||
4437 | if (d_defined) | |
4438 | return d; | |
de22184b MS |
4439 | |
4440 | /* This needs to happen before any tsubsting. */ | |
4441 | if (! push_tinst_level (d)) | |
4442 | return d; | |
4443 | ||
4444 | push_to_top_level (); | |
4445 | lineno = DECL_SOURCE_LINE (d); | |
4446 | input_filename = DECL_SOURCE_FILE (d); | |
4447 | ||
4448 | /* We need to set up DECL_INITIAL regardless of pattern_defined if the | |
4449 | variable is a static const initialized in the class body. */ | |
4450 | if (TREE_CODE (d) == VAR_DECL | |
fee23f54 | 4451 | && ! DECL_INITIAL (d) && DECL_INITIAL (code_pattern)) |
de22184b MS |
4452 | { |
4453 | pushclass (DECL_CONTEXT (d), 2); | |
fee23f54 JM |
4454 | DECL_INITIAL (d) = tsubst_expr (DECL_INITIAL (code_pattern), args, |
4455 | TREE_VEC_LENGTH (args), tmpl); | |
de22184b MS |
4456 | popclass (1); |
4457 | } | |
4458 | ||
4459 | /* import_export_decl has to happen after DECL_INITIAL is set up. */ | |
4460 | if (pattern_defined) | |
5566b478 MS |
4461 | { |
4462 | repo_template_used (d); | |
4463 | ||
4464 | if (flag_external_templates && ! DECL_INTERFACE_KNOWN (d)) | |
4465 | { | |
4466 | if (flag_alt_external_templates) | |
4467 | { | |
4468 | if (interface_unknown) | |
4469 | warn_if_unknown_interface (d); | |
4470 | } | |
fee23f54 | 4471 | else if (DECL_INTERFACE_KNOWN (code_pattern)) |
5566b478 MS |
4472 | { |
4473 | DECL_INTERFACE_KNOWN (d) = 1; | |
fee23f54 | 4474 | DECL_NOT_REALLY_EXTERN (d) = ! DECL_EXTERNAL (code_pattern); |
5566b478 MS |
4475 | } |
4476 | else | |
fee23f54 | 4477 | warn_if_unknown_interface (code_pattern); |
5566b478 MS |
4478 | } |
4479 | ||
e92cc029 | 4480 | if (at_eof) |
5566b478 MS |
4481 | import_export_decl (d); |
4482 | } | |
4483 | ||
4484 | if (! pattern_defined | |
4485 | || (TREE_CODE (d) == FUNCTION_DECL && ! DECL_INLINE (d) | |
4486 | && (! DECL_INTERFACE_KNOWN (d) | |
909e536a MS |
4487 | || ! DECL_NOT_REALLY_EXTERN (d))) |
4488 | /* Kludge: if we compile a constructor in the middle of processing a | |
4489 | toplevel declaration, we blow away the declspecs in | |
4490 | temp_decl_obstack when we call permanent_allocation in | |
4491 | finish_function. So don't compile it yet. */ | |
4492 | || (TREE_CODE (d) == FUNCTION_DECL && ! nested && ! at_eof)) | |
5566b478 MS |
4493 | { |
4494 | add_pending_template (d); | |
de22184b | 4495 | goto out; |
5566b478 MS |
4496 | } |
4497 | ||
5156628f MS |
4498 | lineno = DECL_SOURCE_LINE (d); |
4499 | input_filename = DECL_SOURCE_FILE (d); | |
4500 | ||
5566b478 | 4501 | /* Trick tsubst into giving us a new decl in case the template changed. */ |
fee23f54 JM |
4502 | save_ti = DECL_TEMPLATE_INFO (decl_pattern); |
4503 | DECL_TEMPLATE_INFO (decl_pattern) = NULL_TREE; | |
4504 | td = tsubst (decl_pattern, args, TREE_VEC_LENGTH (args), tmpl); | |
4505 | DECL_TEMPLATE_INFO (decl_pattern) = save_ti; | |
5566b478 | 4506 | |
d11ad92e MS |
4507 | /* And set up DECL_INITIAL, since tsubst doesn't. */ |
4508 | if (TREE_CODE (td) == VAR_DECL) | |
5156628f MS |
4509 | { |
4510 | pushclass (DECL_CONTEXT (d), 2); | |
fee23f54 JM |
4511 | DECL_INITIAL (td) = tsubst_expr (DECL_INITIAL (code_pattern), args, |
4512 | TREE_VEC_LENGTH (args), tmpl); | |
5156628f MS |
4513 | popclass (1); |
4514 | } | |
d11ad92e | 4515 | |
5566b478 | 4516 | if (TREE_CODE (d) == FUNCTION_DECL) |
386b8a85 JM |
4517 | { |
4518 | /* Convince duplicate_decls to use the DECL_ARGUMENTS from the | |
4519 | new decl. */ | |
4520 | DECL_INITIAL (td) = error_mark_node; | |
4521 | ||
4522 | if (DECL_TEMPLATE_SPECIALIZATION (td) && !DECL_TEMPLATE_INFO (td)) | |
4523 | /* Set up the information about what is being specialized. */ | |
4524 | DECL_TEMPLATE_INFO (td) = DECL_TEMPLATE_INFO (d); | |
4525 | } | |
5566b478 MS |
4526 | duplicate_decls (td, d); |
4527 | if (TREE_CODE (d) == FUNCTION_DECL) | |
4528 | DECL_INITIAL (td) = 0; | |
4529 | ||
4530 | if (TREE_CODE (d) == VAR_DECL) | |
4531 | { | |
4532 | DECL_IN_AGGR_P (d) = 0; | |
4533 | if (DECL_INTERFACE_KNOWN (d)) | |
4534 | DECL_EXTERNAL (d) = ! DECL_NOT_REALLY_EXTERN (d); | |
4535 | else | |
4536 | { | |
4537 | DECL_EXTERNAL (d) = 1; | |
4538 | DECL_NOT_REALLY_EXTERN (d) = 1; | |
4539 | } | |
4540 | cp_finish_decl (d, DECL_INITIAL (d), NULL_TREE, 0, 0); | |
4541 | } | |
4542 | else if (TREE_CODE (d) == FUNCTION_DECL) | |
4543 | { | |
fee23f54 | 4544 | tree t = DECL_SAVED_TREE (code_pattern); |
5566b478 | 4545 | |
c11b6f21 | 4546 | start_function (NULL_TREE, d, NULL_TREE, 1); |
5566b478 MS |
4547 | store_parm_decls (); |
4548 | ||
e76a2646 MS |
4549 | if (t && TREE_CODE (t) == RETURN_INIT) |
4550 | { | |
4551 | store_return_init | |
4552 | (TREE_OPERAND (t, 0), | |
98c1c668 | 4553 | tsubst_expr (TREE_OPERAND (t, 1), args, |
e76a2646 MS |
4554 | TREE_VEC_LENGTH (args), tmpl)); |
4555 | t = TREE_CHAIN (t); | |
4556 | } | |
4557 | ||
5566b478 MS |
4558 | if (t && TREE_CODE (t) == CTOR_INITIALIZER) |
4559 | { | |
4560 | current_member_init_list | |
4561 | = tsubst_expr_values (TREE_OPERAND (t, 0), args); | |
4562 | current_base_init_list | |
4563 | = tsubst_expr_values (TREE_OPERAND (t, 1), args); | |
4564 | t = TREE_CHAIN (t); | |
4565 | } | |
4566 | ||
4567 | setup_vtbl_ptr (); | |
4568 | /* Always keep the BLOCK node associated with the outermost | |
4569 | pair of curley braces of a function. These are needed | |
4570 | for correct operation of dwarfout.c. */ | |
4571 | keep_next_level (); | |
4572 | ||
4573 | my_friendly_assert (TREE_CODE (t) == COMPOUND_STMT, 42); | |
98c1c668 | 4574 | tsubst_expr (t, args, TREE_VEC_LENGTH (args), tmpl); |
a28e3c7f | 4575 | |
5566b478 | 4576 | finish_function (lineno, 0, nested); |
5566b478 MS |
4577 | } |
4578 | ||
de22184b | 4579 | out: |
5156628f MS |
4580 | lineno = line; |
4581 | input_filename = file; | |
4582 | ||
5566b478 | 4583 | pop_from_top_level (); |
5566b478 | 4584 | pop_tinst_level (); |
a28e3c7f | 4585 | |
a28e3c7f MS |
4586 | return d; |
4587 | } | |
5566b478 MS |
4588 | |
4589 | tree | |
4590 | tsubst_chain (t, argvec) | |
4591 | tree t, argvec; | |
4592 | { | |
4593 | if (t) | |
4594 | { | |
98c1c668 | 4595 | tree first = tsubst (t, argvec, |
5566b478 MS |
4596 | TREE_VEC_LENGTH (argvec), NULL_TREE); |
4597 | tree last = first; | |
4598 | ||
4599 | for (t = TREE_CHAIN (t); t; t = TREE_CHAIN (t)) | |
4600 | { | |
98c1c668 | 4601 | tree x = tsubst (t, argvec, TREE_VEC_LENGTH (argvec), NULL_TREE); |
5566b478 MS |
4602 | TREE_CHAIN (last) = x; |
4603 | last = x; | |
4604 | } | |
4605 | ||
4606 | return first; | |
4607 | } | |
4608 | return NULL_TREE; | |
4609 | } | |
4610 | ||
824b9a4c | 4611 | static tree |
5566b478 MS |
4612 | tsubst_expr_values (t, argvec) |
4613 | tree t, argvec; | |
4614 | { | |
4615 | tree first = NULL_TREE; | |
4616 | tree *p = &first; | |
4617 | ||
4618 | for (; t; t = TREE_CHAIN (t)) | |
4619 | { | |
98c1c668 | 4620 | tree pur = tsubst_copy (TREE_PURPOSE (t), argvec, |
5566b478 | 4621 | TREE_VEC_LENGTH (argvec), NULL_TREE); |
98c1c668 | 4622 | tree val = tsubst_expr (TREE_VALUE (t), argvec, |
5566b478 MS |
4623 | TREE_VEC_LENGTH (argvec), NULL_TREE); |
4624 | *p = build_tree_list (pur, val); | |
4625 | p = &TREE_CHAIN (*p); | |
4626 | } | |
4627 | return first; | |
4628 | } | |
4629 | ||
4630 | tree last_tree; | |
4631 | ||
4632 | void | |
4633 | add_tree (t) | |
4634 | tree t; | |
4635 | { | |
4636 | last_tree = TREE_CHAIN (last_tree) = t; | |
4637 | } | |
73aad9b9 JM |
4638 | |
4639 | /* D is an undefined function declaration in the presence of templates with | |
4640 | the same name, listed in FNS. If one of them can produce D as an | |
4641 | instantiation, remember this so we can instantiate it at EOF if D has | |
4642 | not been defined by that time. */ | |
4643 | ||
4644 | void | |
4645 | add_maybe_template (d, fns) | |
4646 | tree d, fns; | |
4647 | { | |
4648 | tree t; | |
4649 | ||
4650 | if (DECL_MAYBE_TEMPLATE (d)) | |
4651 | return; | |
4652 | ||
4653 | t = most_specialized (fns, d); | |
4654 | if (! t) | |
4655 | return; | |
4656 | if (t == error_mark_node) | |
4657 | { | |
4658 | cp_error ("ambiguous template instantiation for `%D'", d); | |
4659 | return; | |
4660 | } | |
4661 | ||
4662 | *maybe_template_tail = perm_tree_cons (t, d, NULL_TREE); | |
4663 | maybe_template_tail = &TREE_CHAIN (*maybe_template_tail); | |
4664 | DECL_MAYBE_TEMPLATE (d) = 1; | |
4665 | } | |
b87692e5 MS |
4666 | |
4667 | /* Instantiate an enumerated type. Used by instantiate_class_template and | |
4668 | tsubst_expr. */ | |
4669 | ||
4670 | static tree | |
b3d5a58b | 4671 | tsubst_enum (tag, args, nargs, field_chain) |
98c1c668 | 4672 | tree tag, args; |
b87692e5 | 4673 | int nargs; |
b3d5a58b | 4674 | tree * field_chain; |
b87692e5 | 4675 | { |
b3d5a58b JG |
4676 | extern tree current_local_enum; |
4677 | tree prev_local_enum = current_local_enum; | |
4678 | ||
b87692e5 MS |
4679 | tree newtag = start_enum (TYPE_IDENTIFIER (tag)); |
4680 | tree e, values = NULL_TREE; | |
4681 | ||
4682 | for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e)) | |
4683 | { | |
4684 | tree elt = build_enumerator (TREE_PURPOSE (e), | |
4685 | tsubst_expr (TREE_VALUE (e), args, | |
4686 | nargs, NULL_TREE)); | |
4687 | TREE_CHAIN (elt) = values; | |
4688 | values = elt; | |
4689 | } | |
4690 | ||
4691 | finish_enum (newtag, values); | |
4692 | ||
b3d5a58b JG |
4693 | if (NULL != field_chain) |
4694 | *field_chain = grok_enum_decls (newtag, NULL_TREE); | |
4695 | ||
4696 | current_local_enum = prev_local_enum; | |
4697 | ||
b87692e5 MS |
4698 | return newtag; |
4699 | } |