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