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