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