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