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