67th Cygnus<->FSF merge
[gcc.git] / gcc / cp / pt.c
1 /* Handle parameterized types (templates) for GNU C++.
2 Copyright (C) 1992, 1993 Free Software Foundation, Inc.
3 Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
4
5 This file is part of GNU CC.
6
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to
19 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
20
21 /* Known bugs or deficiencies include:
22 * templates for class static data don't work (methods only)
23 * duplicated method templates can crash the compiler
24 * interface/impl data is taken from file defining the template
25 * all methods must be provided in header files; can't use a source
26 file that contains only the method templates and "just win"
27 * method templates must be seen before the expansion of the
28 class template is done
29 */
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"
40 #include "lex.h"
41 #include "output.h"
42
43 extern struct obstack permanent_obstack;
44 extern tree grokdeclarator ();
45
46 extern int lineno;
47 extern char *input_filename;
48 struct pending_inline *pending_template_expansions;
49
50 int processing_template_decl;
51 int processing_template_defn;
52
53 #define obstack_chunk_alloc xmalloc
54 #define obstack_chunk_free free
55
56 static int unify ();
57 static void add_pending_template ();
58
59 void overload_template_name (), pop_template_decls ();
60
61 /* We've got a template header coming up; set obstacks up to save the
62 nodes created permanently. (There might be cases with nested templates
63 where we don't have to do this, but they aren't implemented, and it
64 probably wouldn't be worth the effort.) */
65 void
66 begin_template_parm_list ()
67 {
68 pushlevel (0);
69 push_obstacks (&permanent_obstack, &permanent_obstack);
70 pushlevel (0);
71 }
72
73 /* Process information from new template parameter NEXT and append it to the
74 LIST being built. The rules for use of a template parameter type name
75 by later parameters are not well-defined for us just yet. However, the
76 only way to avoid having to parse expressions of unknown complexity (and
77 with tokens of unknown types) is to disallow it completely. So for now,
78 that is what is assumed. */
79 tree
80 process_template_parm (list, next)
81 tree list, next;
82 {
83 tree parm;
84 tree decl = 0;
85 tree defval;
86 int is_type;
87 parm = next;
88 my_friendly_assert (TREE_CODE (parm) == TREE_LIST, 259);
89 defval = TREE_PURPOSE (parm);
90 parm = TREE_VALUE (parm);
91 is_type = TREE_PURPOSE (parm) == class_type_node;
92 if (!is_type)
93 {
94 tree tinfo = 0;
95 my_friendly_assert (TREE_CODE (TREE_PURPOSE (parm)) == TREE_LIST, 260);
96 /* is a const-param */
97 parm = grokdeclarator (TREE_VALUE (parm), TREE_PURPOSE (parm),
98 PARM, 0, NULL_TREE);
99 /* A template parameter is not modifiable. */
100 TREE_READONLY (parm) = 1;
101 if (IS_AGGR_TYPE (TREE_TYPE (parm)))
102 {
103 sorry ("aggregate template parameter types");
104 TREE_TYPE (parm) = void_type_node;
105 }
106 tinfo = make_node (TEMPLATE_CONST_PARM);
107 my_friendly_assert (TREE_PERMANENT (tinfo), 260.5);
108 if (TREE_PERMANENT (parm) == 0)
109 {
110 parm = copy_node (parm);
111 TREE_PERMANENT (parm) = 1;
112 }
113 TREE_TYPE (tinfo) = TREE_TYPE (parm);
114 decl = build_decl (CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
115 DECL_INITIAL (decl) = tinfo;
116 DECL_INITIAL (parm) = tinfo;
117 }
118 else
119 {
120 tree t = make_node (TEMPLATE_TYPE_PARM);
121 decl = build_decl (TYPE_DECL, TREE_VALUE (parm), t);
122 TYPE_MAIN_DECL (t) = decl;
123 parm = decl;
124 if (defval)
125 {
126 if (IDENTIFIER_HAS_TYPE_VALUE (defval))
127 defval = IDENTIFIER_TYPE_VALUE (defval);
128 else
129 defval = TREE_TYPE (IDENTIFIER_GLOBAL_VALUE (defval));
130 }
131 }
132 SET_DECL_ARTIFICIAL (decl);
133 pushdecl (decl);
134 parm = build_tree_list (defval, parm);
135 return chainon (list, parm);
136 }
137
138 /* The end of a template parameter list has been reached. Process the
139 tree list into a parameter vector, converting each parameter into a more
140 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
141 as PARM_DECLs. */
142
143 tree
144 end_template_parm_list (parms)
145 tree parms;
146 {
147 int nparms = 0;
148 int saw_default = 0;
149 tree saved_parmlist;
150 tree parm;
151 for (parm = parms; parm; parm = TREE_CHAIN (parm))
152 nparms++;
153 saved_parmlist = make_tree_vec (nparms);
154
155 for (parm = parms, nparms = 0; parm; parm = TREE_CHAIN (parm), nparms++)
156 {
157 tree p = TREE_VALUE (parm);
158 if (TREE_PURPOSE (parm))
159 saw_default = 1;
160 else if (saw_default)
161 {
162 error ("if a default argument is given for one template parameter");
163 error ("default arguments must be given for all subsequent");
164 error ("parameters as well");
165 }
166
167 if (TREE_CODE (p) == TYPE_DECL)
168 {
169 tree t = TREE_TYPE (p);
170 TEMPLATE_TYPE_SET_INFO (t, saved_parmlist, nparms);
171 }
172 else
173 {
174 tree tinfo = DECL_INITIAL (p);
175 DECL_INITIAL (p) = NULL_TREE;
176 TEMPLATE_CONST_SET_INFO (tinfo, saved_parmlist, nparms);
177 }
178 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
179 }
180 set_current_level_tags_transparency (1);
181 processing_template_decl++;
182 return saved_parmlist;
183 }
184
185 /* end_template_decl is called after a template declaration is seen.
186 D1 is template header; D2 is class_head_sans_basetype or a
187 TEMPLATE_DECL with its DECL_RESULT field set. */
188 void
189 end_template_decl (d1, d2, is_class, defn)
190 tree d1, d2, is_class;
191 int defn;
192 {
193 tree decl;
194 struct template_info *tmpl;
195
196 tmpl = (struct template_info *) obstack_alloc (&permanent_obstack,
197 sizeof (struct template_info));
198 tmpl->text = 0;
199 tmpl->length = 0;
200 tmpl->aggr = is_class;
201
202 /* cloned from reinit_parse_for_template */
203 tmpl->filename = input_filename;
204 tmpl->lineno = lineno;
205 tmpl->parm_vec = d1; /* [eichin:19911015.2306EST] */
206
207 if (d2 == NULL_TREE || d2 == error_mark_node)
208 {
209 decl = 0;
210 goto lose;
211 }
212
213 if (is_class)
214 {
215 decl = build_lang_decl (TEMPLATE_DECL, d2, NULL_TREE);
216 GNU_xref_decl (current_function_decl, decl);
217 }
218 else
219 {
220 if (TREE_CODE (d2) == TEMPLATE_DECL)
221 decl = d2;
222 else
223 {
224 /* Class destructor templates and operator templates are
225 slipping past as non-template nodes. Process them here, since
226 I haven't figured out where to catch them earlier. I could
227 go do that, but it's a choice between getting that done and
228 staying only N months behind schedule. Sorry.... */
229 enum tree_code code;
230 my_friendly_assert (TREE_CODE (d2) == CALL_EXPR, 263);
231 code = TREE_CODE (TREE_OPERAND (d2, 0));
232 my_friendly_assert (code == BIT_NOT_EXPR
233 || code == OP_IDENTIFIER
234 || code == SCOPE_REF, 264);
235 d2 = grokdeclarator (d2, NULL_TREE, MEMFUNCDEF, 0, NULL_TREE);
236 decl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (d2),
237 TREE_TYPE (d2));
238 DECL_TEMPLATE_RESULT (decl) = d2;
239 DECL_CONTEXT (decl) = DECL_CONTEXT (d2);
240 DECL_CLASS_CONTEXT (decl) = DECL_CLASS_CONTEXT (d2);
241 DECL_NAME (decl) = DECL_NAME (d2);
242 TREE_TYPE (decl) = TREE_TYPE (d2);
243 if (interface_unknown && flag_external_templates
244 && ! flag_alt_external_templates
245 && ! DECL_IN_SYSTEM_HEADER (decl))
246 warn_if_unknown_interface (decl);
247 TREE_PUBLIC (decl) = TREE_PUBLIC (d2) = flag_external_templates && !interface_unknown;
248 DECL_EXTERNAL (decl) = (DECL_EXTERNAL (d2)
249 && !(DECL_CLASS_CONTEXT (d2)
250 && !DECL_THIS_EXTERN (d2)));
251 }
252
253 /* All routines creating TEMPLATE_DECL nodes should now be using
254 build_lang_decl, which will have set this up already. */
255 my_friendly_assert (DECL_LANG_SPECIFIC (decl) != 0, 265);
256
257 /* @@ Somewhere, permanent allocation isn't being used. */
258 if (! DECL_TEMPLATE_IS_CLASS (decl)
259 && TREE_CODE (DECL_TEMPLATE_RESULT (decl)) == FUNCTION_DECL)
260 {
261 tree result = DECL_TEMPLATE_RESULT (decl);
262 /* Will do nothing if allocation was already permanent. */
263 DECL_ARGUMENTS (result) = copy_to_permanent (DECL_ARGUMENTS (result));
264 }
265
266 /* If this is for a method, there's an extra binding level here. */
267 if (DECL_CONTEXT (DECL_TEMPLATE_RESULT (decl)) != NULL_TREE)
268 {
269 /* @@ Find out where this should be getting set! */
270 tree r = DECL_TEMPLATE_RESULT (decl);
271 if (DECL_LANG_SPECIFIC (r) && DECL_CLASS_CONTEXT (r) == NULL_TREE)
272 DECL_CLASS_CONTEXT (r) = DECL_CONTEXT (r);
273 }
274 }
275 DECL_TEMPLATE_INFO (decl) = tmpl;
276 DECL_TEMPLATE_PARMS (decl) = d1;
277
278 /* So that duplicate_decls can do the right thing. */
279 if (defn)
280 DECL_INITIAL (decl) = error_mark_node;
281
282 /* If context of decl is non-null (i.e., method template), add it
283 to the appropriate class template, and pop the binding levels. */
284 if (! is_class && DECL_CONTEXT (DECL_TEMPLATE_RESULT (decl)) != NULL_TREE)
285 {
286 tree ctx = DECL_CONTEXT (DECL_TEMPLATE_RESULT (decl));
287 tree tmpl, t;
288 my_friendly_assert (TREE_CODE (ctx) == UNINSTANTIATED_P_TYPE, 266);
289 tmpl = UPT_TEMPLATE (ctx);
290 for (t = DECL_TEMPLATE_MEMBERS (tmpl); t; t = TREE_CHAIN (t))
291 if (TREE_PURPOSE (t) == DECL_NAME (decl)
292 && duplicate_decls (decl, TREE_VALUE (t)))
293 goto already_there;
294 DECL_TEMPLATE_MEMBERS (tmpl) =
295 perm_tree_cons (DECL_NAME (decl), decl, DECL_TEMPLATE_MEMBERS (tmpl));
296 already_there:
297 poplevel (0, 0, 0);
298 poplevel (0, 0, 0);
299 }
300 /* Otherwise, go back to top level first, and push the template decl
301 again there. */
302 else
303 {
304 poplevel (0, 0, 0);
305 poplevel (0, 0, 0);
306 pushdecl (decl);
307 }
308 lose:
309 #if 0 /* It happens sometimes, with syntactic or semantic errors.
310
311 One specific case:
312 template <class A, int X, int Y> class Foo { ... };
313 template <class A, int X, int y> Foo<X,Y>::method (Foo& x) { ... }
314 Note the missing "A" in the class containing "method". */
315 my_friendly_assert (global_bindings_p (), 267);
316 #else
317 while (! global_bindings_p ())
318 poplevel (0, 0, 0);
319 #endif
320 pop_obstacks ();
321 processing_template_decl--;
322 (void) get_pending_sizes ();
323 }
324
325 /* If TYPE contains a template parm type, then substitute that type
326 with its actual type that is found in TVEC. */
327 static void
328 grok_template_type (tvec, type)
329 tree tvec;
330 tree* type;
331 {
332 switch (TREE_CODE (*type))
333 {
334 case TEMPLATE_TYPE_PARM:
335 if (*type != TYPE_MAIN_VARIANT (*type))
336 {
337 /* we are here for cases like const T* etc. */
338 grok_template_type (tvec, &TYPE_MAIN_VARIANT (*type));
339 *type = cp_build_type_variant (TYPE_MAIN_VARIANT (*type),
340 TYPE_READONLY (*type),
341 TYPE_VOLATILE (*type));
342 }
343 else
344 *type = TREE_VEC_ELT (tvec, TEMPLATE_TYPE_IDX (*type));
345 return;
346 case POINTER_TYPE:
347 case REFERENCE_TYPE:
348 grok_template_type (tvec, &TREE_TYPE (*type));
349 return;
350 case FUNCTION_TYPE:
351 {
352 tree p;
353
354 /* take care of function's return type first */
355 grok_template_type (tvec, &TREE_TYPE (*type));
356
357 /* take care of function's arguments */
358 for (p = TYPE_ARG_TYPES (*type); p; p = TREE_CHAIN (p))
359 grok_template_type (tvec, &TREE_VALUE (p));
360 return;
361 }
362 default:
363 break;
364 }
365 return;
366 }
367
368 /* Convert all template arguments to their appropriate types, and return
369 a vector containing the resulting values. If any error occurs, return
370 error_mark_node. */
371 static tree
372 coerce_template_parms (parms, arglist, in_decl)
373 tree parms, arglist;
374 tree in_decl;
375 {
376 int nparms, nargs, i, lost = 0;
377 tree vec;
378
379 if (arglist == NULL_TREE)
380 nargs = 0;
381 else if (TREE_CODE (arglist) == TREE_VEC)
382 nargs = TREE_VEC_LENGTH (arglist);
383 else
384 nargs = list_length (arglist);
385
386 nparms = TREE_VEC_LENGTH (parms);
387
388 if (nargs > nparms
389 || (nargs < nparms
390 && TREE_PURPOSE (TREE_VEC_ELT (parms, nargs)) == NULL_TREE))
391 {
392 error ("incorrect number of parameters (%d, should be %d)",
393 nargs, nparms);
394 if (in_decl)
395 cp_error_at ("in template expansion for decl `%D'", in_decl);
396 return error_mark_node;
397 }
398
399 if (arglist && TREE_CODE (arglist) == TREE_VEC)
400 vec = copy_node (arglist);
401 else
402 {
403 vec = make_tree_vec (nparms);
404 for (i = 0; i < nparms; i++)
405 {
406 tree arg;
407
408 if (arglist)
409 {
410 arg = arglist;
411 arglist = TREE_CHAIN (arglist);
412
413 if (arg == error_mark_node)
414 lost++;
415 else
416 arg = TREE_VALUE (arg);
417 }
418 else
419 arg = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
420
421 TREE_VEC_ELT (vec, i) = arg;
422 }
423 }
424 for (i = 0; i < nparms; i++)
425 {
426 tree arg = TREE_VEC_ELT (vec, i);
427 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
428 tree val = 0;
429 int is_type, requires_type;
430
431 is_type = TREE_CODE_CLASS (TREE_CODE (arg)) == 't';
432 requires_type = TREE_CODE (parm) == TYPE_DECL;
433 if (is_type != requires_type)
434 {
435 if (in_decl)
436 cp_error ("type/value mismatch in template parameter list for `%D'",
437 in_decl);
438 lost++;
439 TREE_VEC_ELT (vec, i) = error_mark_node;
440 continue;
441 }
442 if (is_type)
443 val = groktypename (arg);
444 else if (TREE_CODE (arg) == STRING_CST)
445 {
446 cp_error ("string literal %E is not a valid template argument", arg);
447 error ("because it is the address of an object with static linkage");
448 val = error_mark_node;
449 }
450 else
451 {
452 grok_template_type (vec, &TREE_TYPE (parm));
453 val = digest_init (TREE_TYPE (parm), arg, (tree *) 0);
454
455 if (val == error_mark_node)
456 ;
457
458 /* 14.2: Other template-arguments must be constant-expressions,
459 addresses of objects or functions with external linkage, or of
460 static class members. */
461 else if (!TREE_CONSTANT (val))
462 {
463 cp_error ("non-const `%E' cannot be used as template argument",
464 arg);
465 val = error_mark_node;
466 }
467 else if (TREE_CODE (val) == ADDR_EXPR)
468 {
469 tree a = TREE_OPERAND (val, 0);
470 if ((TREE_CODE (a) == VAR_DECL
471 || TREE_CODE (a) == FUNCTION_DECL)
472 && ! DECL_PUBLIC (a))
473 {
474 cp_error ("address of non-extern `%E' cannot be used as template argument", a);
475 val = error_mark_node;
476 }
477 }
478 }
479
480 if (val == error_mark_node)
481 lost++;
482
483 TREE_VEC_ELT (vec, i) = val;
484 }
485 if (lost)
486 return error_mark_node;
487 return vec;
488 }
489
490 /* Given class template name and parameter list, produce a user-friendly name
491 for the instantiation. */
492 static char *
493 mangle_class_name_for_template (name, parms, arglist)
494 char *name;
495 tree parms, arglist;
496 {
497 static struct obstack scratch_obstack;
498 static char *scratch_firstobj;
499 int i, nparms;
500
501 if (!scratch_firstobj)
502 {
503 gcc_obstack_init (&scratch_obstack);
504 scratch_firstobj = obstack_alloc (&scratch_obstack, 1);
505 }
506 else
507 obstack_free (&scratch_obstack, scratch_firstobj);
508
509 #if 0
510 #define buflen sizeof(buf)
511 #define check if (bufp >= buf+buflen-1) goto too_long
512 #define ccat(c) *bufp++=(c); check
513 #define advance bufp+=strlen(bufp); check
514 #define cat(s) strncpy(bufp, s, buf+buflen-bufp-1); advance
515 #else
516 #define check
517 #define ccat(c) obstack_1grow (&scratch_obstack, (c));
518 #define advance
519 #define cat(s) obstack_grow (&scratch_obstack, (s), strlen (s))
520 #endif
521
522 cat (name);
523 ccat ('<');
524 nparms = TREE_VEC_LENGTH (parms);
525 my_friendly_assert (nparms == TREE_VEC_LENGTH (arglist), 268);
526 for (i = 0; i < nparms; i++)
527 {
528 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
529 tree arg = TREE_VEC_ELT (arglist, i);
530
531 if (i)
532 ccat (',');
533
534 if (TREE_CODE (parm) == TYPE_DECL)
535 {
536 cat (type_as_string (arg, 0));
537 continue;
538 }
539 else
540 my_friendly_assert (TREE_CODE (parm) == PARM_DECL, 269);
541
542 if (TREE_CODE (arg) == TREE_LIST)
543 {
544 /* New list cell was built because old chain link was in
545 use. */
546 my_friendly_assert (TREE_PURPOSE (arg) == NULL_TREE, 270);
547 arg = TREE_VALUE (arg);
548 }
549 /* No need to check arglist against parmlist here; we did that
550 in coerce_template_parms, called from lookup_template_class. */
551 cat (expr_as_string (arg, 0));
552 }
553 {
554 char *bufp = obstack_next_free (&scratch_obstack);
555 int offset = 0;
556 while (bufp[offset - 1] == ' ')
557 offset--;
558 obstack_blank_fast (&scratch_obstack, offset);
559
560 /* B<C<char> >, not B<C<char>> */
561 if (bufp[offset - 1] == '>')
562 ccat (' ');
563 }
564 ccat ('>');
565 ccat ('\0');
566 return (char *) obstack_base (&scratch_obstack);
567
568 #if 0
569 too_long:
570 #endif
571 fatal ("out of (preallocated) string space creating template instantiation name");
572 /* NOTREACHED */
573 return NULL;
574 }
575
576 /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
577 parameters, find the desired type.
578
579 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
580 Since ARGLIST is build on the decl_obstack, we must copy it here
581 to keep it from being reclaimed when the decl storage is reclaimed.
582
583 IN_DECL, if non-NULL, is the template declaration we are trying to
584 instantiate. */
585 tree
586 lookup_template_class (d1, arglist, in_decl)
587 tree d1, arglist;
588 tree in_decl;
589 {
590 tree template, parmlist;
591 char *mangled_name;
592 tree id;
593
594 my_friendly_assert (TREE_CODE (d1) == IDENTIFIER_NODE, 272);
595 template = IDENTIFIER_GLOBAL_VALUE (d1); /* XXX */
596 if (! template)
597 template = IDENTIFIER_CLASS_VALUE (d1);
598 /* With something like `template <class T> class X class X { ... };'
599 we could end up with D1 having nothing but an IDENTIFIER_LOCAL_VALUE.
600 We don't want to do that, but we have to deal with the situation, so
601 let's give them some syntax errors to chew on instead of a crash. */
602 if (! template)
603 return error_mark_node;
604 if (TREE_CODE (template) != TEMPLATE_DECL)
605 {
606 cp_error ("non-template type `%T' used as a template", d1);
607 if (in_decl)
608 cp_error_at ("for template declaration `%D'", in_decl);
609 return error_mark_node;
610 }
611 parmlist = DECL_TEMPLATE_PARMS (template);
612
613 arglist = coerce_template_parms (parmlist, arglist, template);
614 if (arglist == error_mark_node)
615 return error_mark_node;
616 if (uses_template_parms (arglist))
617 {
618 tree t = make_lang_type (UNINSTANTIATED_P_TYPE);
619 tree d;
620 id = make_anon_name ();
621 d = build_decl (TYPE_DECL, id, t);
622 TYPE_NAME (t) = d;
623 TYPE_VALUES (t) = build_tree_list (template, arglist);
624 pushdecl_top_level (d);
625 }
626 else
627 {
628 mangled_name = mangle_class_name_for_template (IDENTIFIER_POINTER (d1),
629 parmlist, arglist);
630 id = get_identifier (mangled_name);
631 }
632 if (!IDENTIFIER_TEMPLATE (id))
633 {
634 arglist = copy_to_permanent (arglist);
635 IDENTIFIER_TEMPLATE (id) = perm_tree_cons (template, arglist, NULL_TREE);
636 }
637 return id;
638 }
639 \f
640 void
641 push_template_decls (parmlist, arglist, class_level)
642 tree parmlist, arglist;
643 int class_level;
644 {
645 int i, nparms;
646
647 /* Don't want to push values into global context. */
648 if (!class_level)
649 {
650 pushlevel (1);
651 declare_pseudo_global_level ();
652 }
653
654 nparms = TREE_VEC_LENGTH (parmlist);
655
656 for (i = 0; i < nparms; i++)
657 {
658 int requires_type, is_type;
659 tree parm = TREE_VALUE (TREE_VEC_ELT (parmlist, i));
660 tree arg = TREE_VEC_ELT (arglist, i);
661 tree decl = 0;
662
663 requires_type = TREE_CODE (parm) == TYPE_DECL;
664 is_type = TREE_CODE_CLASS (TREE_CODE (arg)) == 't';
665 if (is_type)
666 {
667 /* add typename to namespace */
668 if (!requires_type)
669 {
670 error ("template use error: type provided where value needed");
671 continue;
672 }
673 decl = arg;
674 my_friendly_assert (TREE_CODE_CLASS (TREE_CODE (decl)) == 't', 273);
675 decl = build_decl (TYPE_DECL, DECL_NAME (parm), decl);
676 }
677 else
678 {
679 /* add const decl to namespace */
680 tree val;
681 if (requires_type)
682 {
683 error ("template use error: value provided where type needed");
684 continue;
685 }
686 val = digest_init (TREE_TYPE (parm), arg, (tree *) 0);
687 if (val != error_mark_node)
688 {
689 decl = build_decl (CONST_DECL, DECL_NAME (parm),
690 TREE_TYPE (parm));
691 DECL_INITIAL (decl) = val;
692 TREE_READONLY (decl) = 1;
693 }
694 }
695 if (decl != 0)
696 {
697 SET_DECL_ARTIFICIAL (decl);
698 layout_decl (decl, 0);
699 if (class_level)
700 pushdecl_class_level (decl);
701 else
702 pushdecl (decl);
703 }
704 }
705 }
706
707 void
708 pop_template_decls (parmlist, arglist, class_level)
709 tree parmlist, arglist;
710 int class_level;
711 {
712 if (!class_level)
713 poplevel (0, 0, 0);
714 }
715 \f
716 /* Should be defined in parse.h. */
717 extern int yychar;
718
719 int
720 uses_template_parms (t)
721 tree t;
722 {
723 if (!t)
724 return 0;
725 switch (TREE_CODE (t))
726 {
727 case INDIRECT_REF:
728 case COMPONENT_REF:
729 /* We assume that the object must be instantiated in order to build
730 the COMPONENT_REF, so we test only whether the type of the
731 COMPONENT_REF uses template parms. */
732 return uses_template_parms (TREE_TYPE (t));
733
734 case IDENTIFIER_NODE:
735 if (!IDENTIFIER_TEMPLATE (t))
736 return 0;
737 return uses_template_parms (TREE_VALUE (IDENTIFIER_TEMPLATE (t)));
738
739 /* aggregates of tree nodes */
740 case TREE_VEC:
741 {
742 int i = TREE_VEC_LENGTH (t);
743 while (i--)
744 if (uses_template_parms (TREE_VEC_ELT (t, i)))
745 return 1;
746 return 0;
747 }
748 case TREE_LIST:
749 if (uses_template_parms (TREE_PURPOSE (t))
750 || uses_template_parms (TREE_VALUE (t)))
751 return 1;
752 return uses_template_parms (TREE_CHAIN (t));
753
754 /* constructed type nodes */
755 case POINTER_TYPE:
756 case REFERENCE_TYPE:
757 return uses_template_parms (TREE_TYPE (t));
758 case RECORD_TYPE:
759 if (TYPE_PTRMEMFUNC_FLAG (t))
760 return uses_template_parms (TYPE_PTRMEMFUNC_FN_TYPE (t));
761 case UNION_TYPE:
762 if (!TYPE_NAME (t))
763 return 0;
764 if (!TYPE_IDENTIFIER (t))
765 return 0;
766 return uses_template_parms (TYPE_IDENTIFIER (t));
767 case FUNCTION_TYPE:
768 if (uses_template_parms (TYPE_ARG_TYPES (t)))
769 return 1;
770 return uses_template_parms (TREE_TYPE (t));
771 case ARRAY_TYPE:
772 if (uses_template_parms (TYPE_DOMAIN (t)))
773 return 1;
774 return uses_template_parms (TREE_TYPE (t));
775 case OFFSET_TYPE:
776 if (uses_template_parms (TYPE_OFFSET_BASETYPE (t)))
777 return 1;
778 return uses_template_parms (TREE_TYPE (t));
779 case METHOD_TYPE:
780 if (uses_template_parms (TYPE_OFFSET_BASETYPE (t)))
781 return 1;
782 if (uses_template_parms (TYPE_ARG_TYPES (t)))
783 return 1;
784 return uses_template_parms (TREE_TYPE (t));
785
786 /* decl nodes */
787 case TYPE_DECL:
788 return uses_template_parms (DECL_NAME (t));
789 case FUNCTION_DECL:
790 if (uses_template_parms (TREE_TYPE (t)))
791 return 1;
792 /* fall through */
793 case VAR_DECL:
794 case PARM_DECL:
795 /* ??? What about FIELD_DECLs? */
796 /* The type of a decl can't use template parms if the name of the
797 variable doesn't, because it's impossible to resolve them. So
798 ignore the type field for now. */
799 if (DECL_CONTEXT (t) && uses_template_parms (DECL_CONTEXT (t)))
800 return 1;
801 if (uses_template_parms (TREE_TYPE (t)))
802 {
803 error ("template parms used where they can't be resolved");
804 }
805 return 0;
806
807 case CALL_EXPR:
808 return uses_template_parms (TREE_TYPE (t));
809 case ADDR_EXPR:
810 return uses_template_parms (TREE_OPERAND (t, 0));
811
812 /* template parm nodes */
813 case TEMPLATE_TYPE_PARM:
814 case TEMPLATE_CONST_PARM:
815 return 1;
816
817 /* simple type nodes */
818 case INTEGER_TYPE:
819 if (uses_template_parms (TYPE_MIN_VALUE (t)))
820 return 1;
821 return uses_template_parms (TYPE_MAX_VALUE (t));
822
823 case REAL_TYPE:
824 case VOID_TYPE:
825 case ENUMERAL_TYPE:
826 case BOOLEAN_TYPE:
827 return 0;
828
829 /* constants */
830 case INTEGER_CST:
831 case REAL_CST:
832 case STRING_CST:
833 return 0;
834
835 case ERROR_MARK:
836 /* Non-error_mark_node ERROR_MARKs are bad things. */
837 my_friendly_assert (t == error_mark_node, 274);
838 /* NOTREACHED */
839 return 0;
840
841 case UNINSTANTIATED_P_TYPE:
842 return 1;
843
844 case CONSTRUCTOR:
845 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t)))
846 return uses_template_parms (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t)));
847 /* else fall through */
848
849 default:
850 switch (TREE_CODE_CLASS (TREE_CODE (t)))
851 {
852 case '1':
853 case '2':
854 case '3':
855 case '<':
856 {
857 int i;
858 for (i = tree_code_length[(int) TREE_CODE (t)]; --i >= 0;)
859 if (uses_template_parms (TREE_OPERAND (t, i)))
860 return 1;
861 return 0;
862 }
863 default:
864 break;
865 }
866 sorry ("testing %s for template parms",
867 tree_code_name [(int) TREE_CODE (t)]);
868 my_friendly_abort (82);
869 /* NOTREACHED */
870 return 0;
871 }
872 }
873
874 void
875 instantiate_member_templates (classname)
876 tree classname;
877 {
878 tree t;
879 tree id = classname;
880 tree members = DECL_TEMPLATE_MEMBERS (TREE_PURPOSE (IDENTIFIER_TEMPLATE (id)));
881
882 for (t = members; t; t = TREE_CHAIN (t))
883 {
884 tree parmvec, type, classparms, tdecl, t2;
885 int nparms, xxx = 0, i;
886
887 my_friendly_assert (TREE_VALUE (t) != NULL_TREE, 275);
888 my_friendly_assert (TREE_CODE (TREE_VALUE (t)) == TEMPLATE_DECL, 276);
889 /* @@ Should verify that class parm list is a list of
890 distinct template parameters, and covers all the template
891 parameters. */
892 tdecl = TREE_VALUE (t);
893 type = DECL_CONTEXT (DECL_TEMPLATE_RESULT (tdecl));
894 classparms = UPT_PARMS (type);
895 nparms = TREE_VEC_LENGTH (classparms);
896 parmvec = make_tree_vec (nparms);
897 for (i = 0; i < nparms; i++)
898 TREE_VEC_ELT (parmvec, i) = NULL_TREE;
899 switch (unify (DECL_TEMPLATE_PARMS (tdecl),
900 &TREE_VEC_ELT (parmvec, 0), nparms,
901 type, IDENTIFIER_TYPE_VALUE (classname),
902 &xxx))
903 {
904 case 0:
905 /* Success -- well, no inconsistency, at least. */
906 for (i = 0; i < nparms; i++)
907 if (TREE_VEC_ELT (parmvec, i) == NULL_TREE)
908 goto failure;
909 t2 = instantiate_template (tdecl,
910 &TREE_VEC_ELT (parmvec, 0));
911 type = IDENTIFIER_TYPE_VALUE (id);
912 my_friendly_assert (type != 0, 277);
913 break;
914 case 1:
915 /* Failure. */
916 failure:
917 cp_error_at ("type unification error instantiating `%D'", tdecl);
918 cp_error ("while instantiating members of `%T'", classname);
919
920 continue /* loop of members */;
921 default:
922 /* Eek, a bug. */
923 my_friendly_abort (83);
924 }
925 }
926 }
927
928 static struct tinst_level *current_tinst_level = 0;
929 static struct tinst_level *free_tinst_level = 0;
930 static int tinst_depth = 0;
931 int max_tinst_depth = 17;
932
933 int
934 push_tinst_level (name)
935 tree name;
936 {
937 struct tinst_level *new;
938 tree global = IDENTIFIER_GLOBAL_VALUE (name);
939
940 if (tinst_depth >= max_tinst_depth)
941 {
942 error ("template instantiation depth exceeds maximum of %d",
943 max_tinst_depth);
944 cp_error (" instantiating `%D'", name);
945 return 0;
946 }
947
948 if (free_tinst_level)
949 {
950 new = free_tinst_level;
951 free_tinst_level = new->next;
952 }
953 else
954 new = (struct tinst_level *) xmalloc (sizeof (struct tinst_level));
955
956 new->classname = name;
957 if (global)
958 {
959 new->line = DECL_SOURCE_LINE (global);
960 new->file = DECL_SOURCE_FILE (global);
961 }
962 else
963 {
964 new->line = lineno;
965 new->file = input_filename;
966 }
967 new->next = current_tinst_level;
968 current_tinst_level = new;
969 ++tinst_depth;
970 return 1;
971 }
972
973 void
974 pop_tinst_level ()
975 {
976 struct tinst_level *old = current_tinst_level;
977
978 current_tinst_level = old->next;
979 old->next = free_tinst_level;
980 free_tinst_level = old;
981 --tinst_depth;
982 }
983
984 struct tinst_level *
985 tinst_for_decl ()
986 {
987 struct tinst_level *p = current_tinst_level;
988
989 if (p)
990 for (; p->next ; p = p->next )
991 ;
992 return p;
993 }
994
995 tree
996 instantiate_class_template (classname, setup_parse)
997 tree classname;
998 int setup_parse;
999 {
1000 struct template_info *template_info;
1001 tree template, t1;
1002
1003 if (classname == error_mark_node)
1004 return error_mark_node;
1005
1006 my_friendly_assert (TREE_CODE (classname) == IDENTIFIER_NODE, 278);
1007 template = IDENTIFIER_TEMPLATE (classname);
1008
1009 if (IDENTIFIER_HAS_TYPE_VALUE (classname))
1010 {
1011 tree type = IDENTIFIER_TYPE_VALUE (classname);
1012 if (TREE_CODE (type) == UNINSTANTIATED_P_TYPE)
1013 return type;
1014 if (TYPE_BEING_DEFINED (type)
1015 || TYPE_SIZE (type)
1016 || CLASSTYPE_USE_TEMPLATE (type) != 0)
1017 return type;
1018 }
1019
1020 /* If IDENTIFIER_LOCAL_VALUE is already set on this template classname
1021 (it's something like `foo<int>'), that means we're already working on
1022 the instantiation for it. Normally, a classname comes in with nothing
1023 but its IDENTIFIER_TEMPLATE slot set. If we were to try to instantiate
1024 this again, we'd get a redeclaration error. Since we're already working
1025 on it, we'll pass back this classname's TYPE_DECL (it's the value of
1026 the classname's IDENTIFIER_LOCAL_VALUE). Only do this if we're setting
1027 things up for the parser, though---if we're just trying to instantiate
1028 it (e.g., via tsubst) we can trip up cuz it may not have an
1029 IDENTIFIER_TYPE_VALUE when it will need one. */
1030 if (setup_parse && IDENTIFIER_LOCAL_VALUE (classname))
1031 return IDENTIFIER_LOCAL_VALUE (classname);
1032
1033 if (uses_template_parms (classname))
1034 {
1035 if (!TREE_TYPE (classname))
1036 {
1037 tree t = make_lang_type (RECORD_TYPE);
1038 tree d = build_decl (TYPE_DECL, classname, t);
1039 DECL_NAME (d) = classname;
1040 TYPE_NAME (t) = d;
1041 pushdecl (d);
1042 }
1043 return NULL_TREE;
1044 }
1045
1046 t1 = TREE_PURPOSE (template);
1047 my_friendly_assert (TREE_CODE (t1) == TEMPLATE_DECL, 279);
1048
1049 /* If a template is declared but not defined, accept it; don't crash.
1050 Later uses requiring the definition will be flagged as errors by
1051 other code. Thanks to niklas@appli.se for this bug fix. */
1052 if (DECL_TEMPLATE_INFO (t1)->text == 0)
1053 setup_parse = 0;
1054
1055 push_to_top_level ();
1056 template_info = DECL_TEMPLATE_INFO (t1);
1057 if (setup_parse && push_tinst_level (classname))
1058 {
1059 push_template_decls (DECL_TEMPLATE_PARMS (TREE_PURPOSE (template)),
1060 TREE_VALUE (template), 0);
1061 set_current_level_tags_transparency (1);
1062 feed_input (template_info->text, template_info->length, (struct obstack *)0);
1063 lineno = template_info->lineno;
1064 input_filename = template_info->filename;
1065 /* Get interface/implementation back in sync. */
1066 extract_interface_info ();
1067 overload_template_name (classname, 0);
1068 /* Kludge so that we don't get screwed by our own base classes. */
1069 TYPE_BEING_DEFINED (TREE_TYPE (classname)) = 1;
1070 yychar = PRE_PARSED_CLASS_DECL;
1071 yylval.ttype = classname;
1072 processing_template_defn++;
1073 if (!flag_external_templates)
1074 interface_unknown++;
1075 }
1076 else
1077 {
1078 tree t, decl, id, tmpl;
1079
1080 id = classname;
1081 tmpl = TREE_PURPOSE (IDENTIFIER_TEMPLATE (id));
1082 t = xref_tag (DECL_TEMPLATE_INFO (tmpl)->aggr, id, NULL_TREE, 0);
1083 my_friendly_assert (TREE_CODE (t) == RECORD_TYPE
1084 || TREE_CODE (t) == UNION_TYPE, 280);
1085
1086 /* Now, put a copy of the decl in global scope, to avoid
1087 * recursive expansion. */
1088 decl = IDENTIFIER_LOCAL_VALUE (id);
1089 if (!decl)
1090 decl = IDENTIFIER_CLASS_VALUE (id);
1091 if (decl)
1092 {
1093 my_friendly_assert (TREE_CODE (decl) == TYPE_DECL, 281);
1094 /* We'd better make sure we're on the permanent obstack or else
1095 * we'll get a "friendly" abort 124 in pushdecl. Perhaps a
1096 * copy_to_permanent would be sufficient here, but then a
1097 * sharing problem might occur. I don't know -- niklas@appli.se */
1098 push_obstacks (&permanent_obstack, &permanent_obstack);
1099 pushdecl_top_level (copy_node (decl));
1100 pop_obstacks ();
1101 }
1102 pop_from_top_level ();
1103 }
1104
1105 return NULL_TREE;
1106 }
1107
1108 static int
1109 list_eq (t1, t2)
1110 tree t1, t2;
1111 {
1112 if (t1 == NULL_TREE)
1113 return t2 == NULL_TREE;
1114 if (t2 == NULL_TREE)
1115 return 0;
1116 /* Don't care if one declares its arg const and the other doesn't -- the
1117 main variant of the arg type is all that matters. */
1118 if (TYPE_MAIN_VARIANT (TREE_VALUE (t1))
1119 != TYPE_MAIN_VARIANT (TREE_VALUE (t2)))
1120 return 0;
1121 return list_eq (TREE_CHAIN (t1), TREE_CHAIN (t2));
1122 }
1123
1124 static tree
1125 lookup_nested_type_by_name (ctype, name)
1126 tree ctype, name;
1127 {
1128 tree t;
1129
1130 for (t = CLASSTYPE_TAGS (ctype); t; t = TREE_CHAIN (t))
1131 {
1132 if (name == TREE_PURPOSE (t))
1133 return TREE_VALUE (t);
1134 }
1135 return NULL_TREE;
1136 }
1137
1138 static tree
1139 search_nested_type_in_tmpl (tmpl, type)
1140 tree tmpl, type;
1141 {
1142 tree t;
1143
1144 if (tmpl == NULL || TYPE_CONTEXT(type) == NULL)
1145 return tmpl;
1146 t = search_nested_type_in_tmpl (tmpl, TYPE_CONTEXT(type));
1147 if (t == NULL) return t;
1148 t = lookup_nested_type_by_name(t, DECL_NAME(TYPE_NAME(type)));
1149 return t;
1150 }
1151
1152 static tree
1153 tsubst (t, args, nargs, in_decl)
1154 tree t, *args;
1155 int nargs;
1156 tree in_decl;
1157 {
1158 tree type;
1159
1160 if (t == NULL_TREE || t == error_mark_node)
1161 return t;
1162
1163 type = TREE_TYPE (t);
1164 if (type
1165 /* Minor optimization.
1166 ?? Are these really the most frequent cases? Is the savings
1167 significant? */
1168 && type != integer_type_node
1169 && type != void_type_node
1170 && type != char_type_node)
1171 type = tsubst (type, args, nargs, in_decl);
1172
1173 switch (TREE_CODE (t))
1174 {
1175 case RECORD_TYPE:
1176 if (TYPE_PTRMEMFUNC_P (t))
1177 return build_ptrmemfunc_type
1178 (tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, nargs, in_decl));
1179
1180 /* else fall through */
1181
1182 case ERROR_MARK:
1183 case IDENTIFIER_NODE:
1184 case OP_IDENTIFIER:
1185 case VOID_TYPE:
1186 case REAL_TYPE:
1187 case ENUMERAL_TYPE:
1188 case BOOLEAN_TYPE:
1189 case INTEGER_CST:
1190 case REAL_CST:
1191 case STRING_CST:
1192 case UNION_TYPE:
1193 return t;
1194
1195 case INTEGER_TYPE:
1196 if (t == integer_type_node)
1197 return t;
1198
1199 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
1200 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
1201 return t;
1202 return build_index_2_type
1203 (tsubst (TYPE_MIN_VALUE (t), args, nargs, in_decl),
1204 tsubst (TYPE_MAX_VALUE (t), args, nargs, in_decl));
1205
1206 case TEMPLATE_TYPE_PARM:
1207 {
1208 tree arg = args[TEMPLATE_TYPE_IDX (t)];
1209 return cp_build_type_variant
1210 (arg, TYPE_READONLY (arg) || TYPE_READONLY (t),
1211 TYPE_VOLATILE (arg) || TYPE_VOLATILE (t));
1212 }
1213
1214 case TEMPLATE_CONST_PARM:
1215 return args[TEMPLATE_CONST_IDX (t)];
1216
1217 case FUNCTION_DECL:
1218 {
1219 tree r;
1220 tree fnargs, result;
1221
1222 if (type == TREE_TYPE (t)
1223 && (DECL_CONTEXT (t) == NULL_TREE
1224 || TREE_CODE_CLASS (TREE_CODE (DECL_CONTEXT (t))) != 't'))
1225 return t;
1226 fnargs = tsubst (DECL_ARGUMENTS (t), args, nargs, t);
1227 result = tsubst (DECL_RESULT (t), args, nargs, t);
1228 if (DECL_CONTEXT (t) != NULL_TREE
1229 && TREE_CODE_CLASS (TREE_CODE (DECL_CONTEXT (t))) == 't')
1230 {
1231 /* Look it up in that class, and return the decl node there,
1232 instead of creating a new one. */
1233 tree ctx, methods, name, method;
1234 int n_methods;
1235 int i, found = 0;
1236
1237 name = DECL_NAME (t);
1238 ctx = tsubst (DECL_CONTEXT (t), args, nargs, t);
1239 methods = CLASSTYPE_METHOD_VEC (ctx);
1240 if (methods == NULL_TREE)
1241 /* No methods at all -- no way this one can match. */
1242 goto no_match;
1243 n_methods = TREE_VEC_LENGTH (methods);
1244
1245 r = NULL_TREE;
1246
1247 if (!strncmp (OPERATOR_TYPENAME_FORMAT,
1248 IDENTIFIER_POINTER (name),
1249 sizeof (OPERATOR_TYPENAME_FORMAT) - 1))
1250 {
1251 /* Type-conversion operator. Reconstruct the name, in
1252 case it's the name of one of the template's parameters. */
1253 name = build_typename_overload (TREE_TYPE (type));
1254 }
1255
1256 if (DECL_CONTEXT (t) != NULL_TREE
1257 && TREE_CODE_CLASS (TREE_CODE (DECL_CONTEXT (t))) == 't'
1258 && constructor_name (DECL_CONTEXT (t)) == DECL_NAME (t))
1259 name = constructor_name (ctx);
1260
1261 if (DECL_CONSTRUCTOR_P (t) && TYPE_USES_VIRTUAL_BASECLASSES (ctx))
1262 {
1263 /* Since we didn't know that this class had virtual bases until after
1264 we instantiated it, we have to recreate the arguments to this
1265 constructor, as otherwise it would miss the __in_chrg parameter. */
1266 tree newtype, parm;
1267 tree parms = TREE_CHAIN (TYPE_ARG_TYPES (type));
1268 parms = hash_tree_chain (integer_type_node, parms);
1269 newtype = build_cplus_method_type (ctx,
1270 TREE_TYPE (type),
1271 parms);
1272 newtype = build_type_variant (newtype,
1273 TYPE_READONLY (type),
1274 TYPE_VOLATILE (type));
1275 type = newtype;
1276
1277 fnargs = copy_node (DECL_ARGUMENTS (t));
1278 TREE_CHAIN (fnargs) = TREE_CHAIN (DECL_ARGUMENTS (t));
1279
1280 /* In this case we need "in-charge" flag saying whether
1281 this constructor is responsible for initialization
1282 of virtual baseclasses or not. */
1283 parm = build_decl (PARM_DECL, in_charge_identifier, integer_type_node);
1284 /* Mark the artificial `__in_chrg' parameter as "artificial". */
1285 SET_DECL_ARTIFICIAL (parm);
1286 DECL_ARG_TYPE (parm) = integer_type_node;
1287 DECL_REGISTER (parm) = 1;
1288 TREE_CHAIN (parm) = TREE_CHAIN (fnargs);
1289 TREE_CHAIN (fnargs) = parm;
1290
1291 fnargs = tsubst (fnargs, args, nargs, t);
1292 }
1293 #if 0
1294 fprintf (stderr, "\nfor function %s in class %s:\n",
1295 IDENTIFIER_POINTER (name),
1296 IDENTIFIER_POINTER (TYPE_IDENTIFIER (ctx)));
1297 #endif
1298 for (i = 0; i < n_methods; i++)
1299 {
1300 int pass;
1301
1302 method = TREE_VEC_ELT (methods, i);
1303 if (method == NULL_TREE || DECL_NAME (method) != name)
1304 continue;
1305
1306 pass = 0;
1307 maybe_error:
1308 for (; method; method = DECL_CHAIN (method))
1309 {
1310 my_friendly_assert (TREE_CODE (method) == FUNCTION_DECL,
1311 282);
1312 if (! comptypes (type, TREE_TYPE (method), 1))
1313 {
1314 tree mtype = TREE_TYPE (method);
1315 tree t1, t2;
1316
1317 /* Keep looking for a method that matches
1318 perfectly. This takes care of the problem
1319 where destructors (which have implicit int args)
1320 look like constructors which have an int arg. */
1321 if (pass == 0)
1322 continue;
1323
1324 t1 = TYPE_ARG_TYPES (mtype);
1325 t2 = TYPE_ARG_TYPES (type);
1326 if (TREE_CODE (mtype) == FUNCTION_TYPE)
1327 t2 = TREE_CHAIN (t2);
1328
1329 if (list_eq (t1, t2))
1330 {
1331 if (TREE_CODE (mtype) == FUNCTION_TYPE)
1332 {
1333 tree newtype;
1334 newtype = build_function_type (TREE_TYPE (type),
1335 TYPE_ARG_TYPES (type));
1336 newtype = build_type_variant (newtype,
1337 TYPE_READONLY (type),
1338 TYPE_VOLATILE (type));
1339 type = newtype;
1340 if (TREE_TYPE (type) != TREE_TYPE (mtype))
1341 goto maybe_bad_return_type;
1342 }
1343 else if (TYPE_METHOD_BASETYPE (mtype)
1344 == TYPE_METHOD_BASETYPE (type))
1345 {
1346 /* Types didn't match, but arg types and
1347 `this' do match, so the return type is
1348 all that should be messing it up. */
1349 maybe_bad_return_type:
1350 if (TREE_TYPE (type) != TREE_TYPE (mtype))
1351 error ("inconsistent return types for method `%s' in class `%s'",
1352 IDENTIFIER_POINTER (name),
1353 IDENTIFIER_POINTER (TYPE_IDENTIFIER (ctx)));
1354 }
1355 r = method;
1356 break;
1357 }
1358 found = 1;
1359 continue;
1360 }
1361 #if 0
1362 fprintf (stderr, "\tfound %s\n\n",
1363 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (method)));
1364 #endif
1365 if (DECL_ARTIFICIAL (method))
1366 {
1367 cp_error ("template for method `%D' which has default implementation in class `%T'", name, ctx);
1368 if (in_decl)
1369 cp_error_at ("in attempt to instantiate `%D' declared at this point in file", in_decl);
1370 return error_mark_node;
1371 }
1372
1373 if (DECL_ARGUMENTS (method)
1374 && ! TREE_PERMANENT (DECL_ARGUMENTS (method)))
1375 /* @@ Is this early enough? Might we want to do
1376 this instead while processing the expansion? */
1377 DECL_ARGUMENTS (method)
1378 = tsubst (DECL_ARGUMENTS (t), args, nargs, t);
1379 r = method;
1380 break;
1381 }
1382 if (r == NULL_TREE && pass == 0)
1383 {
1384 pass = 1;
1385 method = TREE_VEC_ELT (methods, i);
1386 goto maybe_error;
1387 }
1388 }
1389 if (r == NULL_TREE)
1390 {
1391 no_match:
1392 cp_error
1393 (found
1394 ? "template for method `%D' doesn't match any in class `%T'"
1395 : "method `%D' not found in class `%T'", name, ctx);
1396 if (in_decl)
1397 cp_error_at ("in attempt to instantiate `%D' declared at this point in file", in_decl);
1398 return error_mark_node;
1399 }
1400 }
1401 else
1402 {
1403 r = DECL_NAME (t);
1404 {
1405 tree decls;
1406 int got_it = 0;
1407
1408 decls = lookup_name_nonclass (r);
1409 if (decls == NULL_TREE)
1410 /* no match */;
1411 else if (TREE_CODE (decls) == TREE_LIST)
1412 for (decls = TREE_VALUE (decls); decls ;
1413 decls = DECL_CHAIN (decls))
1414 {
1415 if (TREE_CODE (decls) == FUNCTION_DECL
1416 && TREE_TYPE (decls) == type)
1417 {
1418 got_it = 1;
1419 r = decls;
1420 break;
1421 }
1422 }
1423 else
1424 {
1425 tree val = decls;
1426 decls = NULL_TREE;
1427 if (TREE_CODE (val) == FUNCTION_DECL
1428 && TREE_TYPE (val) == type)
1429 {
1430 got_it = 1;
1431 r = val;
1432 }
1433 }
1434
1435 if (!got_it)
1436 {
1437 tree a = build_decl_overload (r, TYPE_VALUES (type),
1438 DECL_CONTEXT (t) != NULL_TREE);
1439 r = build_lang_decl (FUNCTION_DECL, r, type);
1440 DECL_ASSEMBLER_NAME (r) = a;
1441 }
1442 else if (TREE_STATIC (r))
1443 {
1444 /* This overrides the template version, use it. */
1445 return r;
1446 }
1447 }
1448 }
1449 TREE_PUBLIC (r) = 1;
1450 DECL_EXTERNAL (r) = 1;
1451 TREE_STATIC (r) = 0;
1452 DECL_INTERFACE_KNOWN (r) = 0;
1453 DECL_INLINE (r) = DECL_INLINE (t);
1454 DECL_THIS_INLINE (r) = DECL_THIS_INLINE (t);
1455 {
1456 #if 0 /* Maybe later. -jason */
1457 struct tinst_level *til = tinst_for_decl();
1458
1459 /* should always be true under new approach */
1460 if (til)
1461 {
1462 DECL_SOURCE_FILE (r) = til->file;
1463 DECL_SOURCE_LINE (r) = til->line;
1464 }
1465 else
1466 #endif
1467 {
1468 DECL_SOURCE_FILE (r) = DECL_SOURCE_FILE (t);
1469 DECL_SOURCE_LINE (r) = DECL_SOURCE_LINE (t);
1470 }
1471 }
1472 DECL_CLASS_CONTEXT (r) = tsubst (DECL_CLASS_CONTEXT (t), args, nargs, t);
1473 make_decl_rtl (r, NULL_PTR, 1);
1474 DECL_ARGUMENTS (r) = fnargs;
1475 DECL_RESULT (r) = result;
1476 #if 0
1477 if (DECL_CONTEXT (t) == NULL_TREE
1478 || TREE_CODE_CLASS (TREE_CODE (DECL_CONTEXT (t))) != 't')
1479 push_overloaded_decl_top_level (r, 0);
1480 #endif
1481 return r;
1482 }
1483
1484 case PARM_DECL:
1485 {
1486 tree r;
1487 r = build_decl (PARM_DECL, DECL_NAME (t), type);
1488 DECL_INITIAL (r) = TREE_TYPE (r);
1489 if (TREE_CHAIN (t))
1490 TREE_CHAIN (r) = tsubst (TREE_CHAIN (t), args, nargs, TREE_CHAIN (t));
1491 return r;
1492 }
1493
1494 case TREE_LIST:
1495 {
1496 tree purpose, value, chain, result;
1497 int via_public, via_virtual, via_protected;
1498
1499 if (t == void_list_node)
1500 return t;
1501
1502 via_public = TREE_VIA_PUBLIC (t);
1503 via_protected = TREE_VIA_PROTECTED (t);
1504 via_virtual = TREE_VIA_VIRTUAL (t);
1505
1506 purpose = TREE_PURPOSE (t);
1507 if (purpose)
1508 purpose = tsubst (purpose, args, nargs, in_decl);
1509 value = TREE_VALUE (t);
1510 if (value)
1511 value = tsubst (value, args, nargs, in_decl);
1512 chain = TREE_CHAIN (t);
1513 if (chain && chain != void_type_node)
1514 chain = tsubst (chain, args, nargs, in_decl);
1515 if (purpose == TREE_PURPOSE (t)
1516 && value == TREE_VALUE (t)
1517 && chain == TREE_CHAIN (t))
1518 return t;
1519 result = hash_tree_cons (via_public, via_virtual, via_protected,
1520 purpose, value, chain);
1521 TREE_PARMLIST (result) = TREE_PARMLIST (t);
1522 return result;
1523 }
1524 case TREE_VEC:
1525 {
1526 int len = TREE_VEC_LENGTH (t), need_new = 0, i;
1527 tree *elts = (tree *) alloca (len * sizeof (tree));
1528 bzero ((char *) elts, len * sizeof (tree));
1529
1530 for (i = 0; i < len; i++)
1531 {
1532 elts[i] = tsubst (TREE_VEC_ELT (t, i), args, nargs, in_decl);
1533 if (elts[i] != TREE_VEC_ELT (t, i))
1534 need_new = 1;
1535 }
1536
1537 if (!need_new)
1538 return t;
1539
1540 t = make_tree_vec (len);
1541 for (i = 0; i < len; i++)
1542 TREE_VEC_ELT (t, i) = elts[i];
1543 return t;
1544 }
1545 case POINTER_TYPE:
1546 case REFERENCE_TYPE:
1547 {
1548 tree r;
1549 enum tree_code code;
1550 if (type == TREE_TYPE (t))
1551 return t;
1552
1553 code = TREE_CODE (t);
1554 if (code == POINTER_TYPE)
1555 r = build_pointer_type (type);
1556 else
1557 r = build_reference_type (type);
1558 r = cp_build_type_variant (r, TYPE_READONLY (t), TYPE_VOLATILE (t));
1559 /* Will this ever be needed for TYPE_..._TO values? */
1560 layout_type (r);
1561 return r;
1562 }
1563 case OFFSET_TYPE:
1564 return build_offset_type
1565 (tsubst (TYPE_OFFSET_BASETYPE (t), args, nargs, in_decl), type);
1566 case FUNCTION_TYPE:
1567 case METHOD_TYPE:
1568 {
1569 tree values = TYPE_VALUES (t); /* same as TYPE_ARG_TYPES */
1570 tree context = TYPE_CONTEXT (t);
1571 tree new_value;
1572
1573 /* Don't bother recursing if we know it won't change anything. */
1574 if (values != void_list_node)
1575 values = tsubst (values, args, nargs, in_decl);
1576 if (context)
1577 context = tsubst (context, args, nargs, in_decl);
1578 /* Could also optimize cases where return value and
1579 values have common elements (e.g., T min(const &T, const T&). */
1580
1581 /* If the above parameters haven't changed, just return the type. */
1582 if (type == TREE_TYPE (t)
1583 && values == TYPE_VALUES (t)
1584 && context == TYPE_CONTEXT (t))
1585 return t;
1586
1587 /* Construct a new type node and return it. */
1588 if (TREE_CODE (t) == FUNCTION_TYPE
1589 && context == NULL_TREE)
1590 {
1591 new_value = build_function_type (type, values);
1592 }
1593 else if (context == NULL_TREE)
1594 {
1595 tree base = tsubst (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t))),
1596 args, nargs, in_decl);
1597 new_value = build_cplus_method_type (base, type,
1598 TREE_CHAIN (values));
1599 }
1600 else
1601 {
1602 new_value = make_node (TREE_CODE (t));
1603 TREE_TYPE (new_value) = type;
1604 TYPE_CONTEXT (new_value) = context;
1605 TYPE_VALUES (new_value) = values;
1606 TYPE_SIZE (new_value) = TYPE_SIZE (t);
1607 TYPE_ALIGN (new_value) = TYPE_ALIGN (t);
1608 TYPE_MODE (new_value) = TYPE_MODE (t);
1609 if (TYPE_METHOD_BASETYPE (t))
1610 TYPE_METHOD_BASETYPE (new_value) = tsubst (TYPE_METHOD_BASETYPE (t),
1611 args, nargs, in_decl);
1612 /* Need to generate hash value. */
1613 my_friendly_abort (84);
1614 }
1615 new_value = build_type_variant (new_value,
1616 TYPE_READONLY (t),
1617 TYPE_VOLATILE (t));
1618 return new_value;
1619 }
1620 case ARRAY_TYPE:
1621 {
1622 tree domain = tsubst (TYPE_DOMAIN (t), args, nargs, in_decl);
1623 tree r;
1624 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
1625 return t;
1626 r = build_cplus_array_type (type, domain);
1627 return r;
1628 }
1629
1630 case UNINSTANTIATED_P_TYPE:
1631 {
1632 int nparms = TREE_VEC_LENGTH (DECL_TEMPLATE_PARMS (UPT_TEMPLATE (t)));
1633 tree argvec = make_tree_vec (nparms);
1634 tree parmvec = UPT_PARMS (t);
1635 int i;
1636 tree id, rt;
1637 for (i = 0; i < nparms; i++)
1638 TREE_VEC_ELT (argvec, i) = tsubst (TREE_VEC_ELT (parmvec, i),
1639 args, nargs, in_decl);
1640 id = lookup_template_class (DECL_NAME (UPT_TEMPLATE (t)), argvec, NULL_TREE);
1641 if (! IDENTIFIER_HAS_TYPE_VALUE (id)) {
1642 instantiate_class_template(id, 0);
1643 /* set up pending_classes */
1644 add_pending_template (id);
1645
1646 TYPE_MAIN_VARIANT (IDENTIFIER_TYPE_VALUE (id)) =
1647 IDENTIFIER_TYPE_VALUE (id);
1648 }
1649 rt = IDENTIFIER_TYPE_VALUE (id);
1650
1651 /* kung: this part handles nested type in template definition */
1652
1653 if ( !ANON_AGGRNAME_P (DECL_NAME(TYPE_NAME(t))))
1654 {
1655 rt = search_nested_type_in_tmpl (rt, t);
1656 }
1657
1658 return build_type_variant (rt, TYPE_READONLY (t), TYPE_VOLATILE (t));
1659 }
1660
1661 case MINUS_EXPR:
1662 case PLUS_EXPR:
1663 return fold (build (TREE_CODE (t), TREE_TYPE (t),
1664 tsubst (TREE_OPERAND (t, 0), args, nargs, in_decl),
1665 tsubst (TREE_OPERAND (t, 1), args, nargs, in_decl)));
1666
1667 case NEGATE_EXPR:
1668 case NOP_EXPR:
1669 return fold (build1 (TREE_CODE (t), TREE_TYPE (t),
1670 tsubst (TREE_OPERAND (t, 0), args, nargs, in_decl)));
1671
1672 default:
1673 sorry ("use of `%s' in function template",
1674 tree_code_name [(int) TREE_CODE (t)]);
1675 return error_mark_node;
1676 }
1677 }
1678
1679 tree
1680 instantiate_template (tmpl, targ_ptr)
1681 tree tmpl, *targ_ptr;
1682 {
1683 tree targs, fndecl;
1684 int i, len;
1685 struct pending_inline *p;
1686 struct template_info *t;
1687 struct obstack *old_fmp_obstack;
1688 extern struct obstack *function_maybepermanent_obstack;
1689
1690 push_obstacks (&permanent_obstack, &permanent_obstack);
1691 old_fmp_obstack = function_maybepermanent_obstack;
1692 function_maybepermanent_obstack = &permanent_obstack;
1693
1694 my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 283);
1695 len = TREE_VEC_LENGTH (DECL_TEMPLATE_PARMS (tmpl));
1696
1697 i = len;
1698 while (i--)
1699 targ_ptr[i] = copy_to_permanent (targ_ptr[i]);
1700
1701 for (fndecl = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
1702 fndecl; fndecl = TREE_CHAIN (fndecl))
1703 {
1704 tree *t1 = &TREE_VEC_ELT (TREE_PURPOSE (fndecl), 0);
1705 for (i = len - 1; i >= 0; i--)
1706 if (simple_cst_equal (t1[i], targ_ptr[i]) <= 0)
1707 goto no_match;
1708
1709 /* Here, we have a match. */
1710 fndecl = TREE_VALUE (fndecl);
1711 goto exit;
1712
1713 no_match:
1714 ;
1715 }
1716
1717 targs = make_tree_vec (len);
1718 i = len;
1719 while (i--)
1720 TREE_VEC_ELT (targs, i) = targ_ptr[i];
1721
1722 /* substitute template parameters */
1723 fndecl = tsubst (DECL_RESULT (tmpl), targ_ptr,
1724 TREE_VEC_LENGTH (targs), tmpl);
1725
1726 if (fndecl == error_mark_node)
1727 goto exit;
1728
1729 assemble_external (fndecl);
1730
1731 /* If it's a static member fn in the template, we need to change it
1732 into a FUNCTION_TYPE and chop off its this pointer. */
1733 if (TREE_CODE (TREE_TYPE (DECL_RESULT (tmpl))) == METHOD_TYPE
1734 && DECL_STATIC_FUNCTION_P (fndecl))
1735 {
1736 tree olddecl = DECL_RESULT (tmpl);
1737 revert_static_member_fn (&DECL_RESULT (tmpl), NULL, NULL);
1738 /* Chop off the this pointer that grokclassfn so kindly added
1739 for us (it didn't know yet if the fn was static or not). */
1740 DECL_ARGUMENTS (olddecl) = TREE_CHAIN (DECL_ARGUMENTS (olddecl));
1741 DECL_ARGUMENTS (fndecl) = TREE_CHAIN (DECL_ARGUMENTS (fndecl));
1742 }
1743
1744 t = DECL_TEMPLATE_INFO (tmpl);
1745
1746 /* If we have a preexisting version of this function, don't expand
1747 the template version, use the other instead. */
1748 if (TREE_STATIC (fndecl))
1749 {
1750 SET_DECL_TEMPLATE_SPECIALIZATION (fndecl);
1751 p = (struct pending_inline *)0;
1752 }
1753 else if (t->text)
1754 {
1755 SET_DECL_IMPLICIT_INSTANTIATION (fndecl);
1756 repo_template_used (fndecl);
1757 p = (struct pending_inline *) permalloc (sizeof (struct pending_inline));
1758 p->parm_vec = t->parm_vec;
1759 p->bindings = targs;
1760 p->can_free = 0;
1761 p->deja_vu = 0;
1762 p->buf = t->text;
1763 p->len = t->length;
1764 p->fndecl = fndecl;
1765 {
1766 int l = lineno;
1767 char * f = input_filename;
1768
1769 lineno = p->lineno = t->lineno;
1770 input_filename = p->filename = t->filename;
1771
1772 extract_interface_info ();
1773
1774 if (interface_unknown && flag_external_templates)
1775 {
1776 if (DECL_CLASS_CONTEXT (fndecl)
1777 && CLASSTYPE_INTERFACE_KNOWN (DECL_CLASS_CONTEXT (fndecl)))
1778 {
1779 interface_unknown = 0;
1780 interface_only
1781 = CLASSTYPE_INTERFACE_ONLY (DECL_CLASS_CONTEXT (fndecl));
1782 }
1783 else if (! DECL_IN_SYSTEM_HEADER (tmpl))
1784 warn_if_unknown_interface (tmpl);
1785 }
1786
1787 if (interface_unknown || ! flag_external_templates)
1788 p->interface = 1; /* unknown */
1789 else
1790 p->interface = interface_only ? 0 : 2;
1791
1792 lineno = l;
1793 input_filename = f;
1794
1795 extract_interface_info ();
1796 }
1797 }
1798 else
1799 p = (struct pending_inline *)0;
1800
1801 DECL_TEMPLATE_INSTANTIATIONS (tmpl) =
1802 tree_cons (targs, fndecl, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
1803
1804 if (p == (struct pending_inline *)0)
1805 {
1806 /* do nothing */
1807 }
1808 else if (DECL_INLINE (fndecl))
1809 {
1810 DECL_PENDING_INLINE_INFO (fndecl) = p;
1811 p->next = pending_inlines;
1812 pending_inlines = p;
1813 }
1814 else
1815 {
1816 p->next = pending_template_expansions;
1817 pending_template_expansions = p;
1818 }
1819 exit:
1820 function_maybepermanent_obstack = old_fmp_obstack;
1821 pop_obstacks ();
1822
1823 return fndecl;
1824 }
1825
1826 /* classlevel should now never be true. jason 4/12/94 */
1827 void
1828 undo_template_name_overload (id, classlevel)
1829 tree id;
1830 int classlevel;
1831 {
1832 tree template;
1833
1834 template = IDENTIFIER_TEMPLATE (id);
1835 if (!template)
1836 return;
1837
1838 #if 0 /* not yet, should get fixed properly later */
1839 poplevel (0, 0, 0);
1840 #endif
1841 #if 1 /* XXX */
1842 /* This was a botch... See `overload_template_name' just below. */
1843 if (!classlevel)
1844 poplevel (0, 0, 0);
1845 #endif
1846 }
1847
1848 /* classlevel should now never be true. jason 4/12/94 */
1849 void
1850 overload_template_name (id, classlevel)
1851 tree id;
1852 int classlevel;
1853 {
1854 tree template, t, decl;
1855 struct template_info *tinfo;
1856
1857 my_friendly_assert (TREE_CODE (id) == IDENTIFIER_NODE, 284);
1858 template = IDENTIFIER_TEMPLATE (id);
1859 if (!template)
1860 return;
1861
1862 template = TREE_PURPOSE (template);
1863 tinfo = DECL_TEMPLATE_INFO (template);
1864 template = DECL_NAME (template);
1865 my_friendly_assert (template != NULL_TREE, 285);
1866
1867 #if 1 /* XXX */
1868 /* This was a botch... names of templates do not get their own private
1869 scopes. Rather, they should go into the binding level already created
1870 by push_template_decls. Except that there isn't one of those for
1871 specializations. */
1872 if (!classlevel)
1873 {
1874 pushlevel (1);
1875 declare_pseudo_global_level ();
1876 }
1877 #endif
1878
1879 t = xref_tag (tinfo->aggr, id, NULL_TREE, 1);
1880 my_friendly_assert (TREE_CODE (t) == RECORD_TYPE
1881 || TREE_CODE (t) == UNION_TYPE
1882 || TREE_CODE (t) == UNINSTANTIATED_P_TYPE, 286);
1883
1884 decl = build_decl (TYPE_DECL, template, t);
1885 SET_DECL_ARTIFICIAL (decl);
1886
1887 #if 0 /* fix this later */
1888 /* We don't want to call here if the work has already been done. */
1889 t = (classlevel
1890 ? IDENTIFIER_CLASS_VALUE (template)
1891 : IDENTIFIER_LOCAL_VALUE (template));
1892 if (t
1893 && TREE_CODE (t) == TYPE_DECL
1894 && TREE_TYPE (t) == t)
1895 my_friendly_abort (85);
1896 #endif
1897
1898 if (classlevel)
1899 pushdecl_class_level (decl);
1900 else
1901 pushdecl (decl);
1902
1903 #if 0 /* This seems bogus to me; if it isn't, explain why. (jason) */
1904 /* Fake this for now, just to make dwarfout.c happy. It will have to
1905 be done in a proper way later on. */
1906 DECL_CONTEXT (decl) = t;
1907 #endif
1908 }
1909
1910 /* NAME is the IDENTIFIER value of a PRE_PARSED_CLASS_DECL. */
1911 void
1912 end_template_instantiation (name)
1913 tree name;
1914 {
1915 extern struct pending_input *to_be_restored;
1916 tree t, decl;
1917
1918 processing_template_defn--;
1919 if (!flag_external_templates)
1920 interface_unknown--;
1921
1922 /* Restore the old parser input state. */
1923 if (yychar == YYEMPTY)
1924 yychar = yylex ();
1925 if (yychar != END_OF_SAVED_INPUT)
1926 error ("parse error at end of class template");
1927 else
1928 {
1929 restore_pending_input (to_be_restored);
1930 to_be_restored = 0;
1931 }
1932
1933 /* Our declarations didn't get stored in the global slot, since
1934 there was a (supposedly tags-transparent) scope in between. */
1935 t = IDENTIFIER_TYPE_VALUE (name);
1936 my_friendly_assert (t != NULL_TREE
1937 && TREE_CODE_CLASS (TREE_CODE (t)) == 't',
1938 287);
1939 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
1940 /* Make methods of template classes static, unless
1941 -fexternal-templates is given. */
1942 if (!flag_external_templates)
1943 SET_CLASSTYPE_INTERFACE_UNKNOWN (t);
1944 decl = IDENTIFIER_GLOBAL_VALUE (name);
1945 my_friendly_assert (TREE_CODE (decl) == TYPE_DECL, 288);
1946
1947 undo_template_name_overload (name, 0);
1948 t = IDENTIFIER_TEMPLATE (name);
1949 pop_template_decls (DECL_TEMPLATE_PARMS (TREE_PURPOSE (t)), TREE_VALUE (t),
1950 0);
1951 /* This will fix up the type-value field. */
1952 pushdecl (decl);
1953 pop_from_top_level ();
1954
1955 #ifdef DWARF_DEBUGGING_INFO
1956 if (write_symbols == DWARF_DEBUG && TREE_CODE (decl) == TYPE_DECL)
1957 {
1958 /* We just completed the definition of a new file-scope type,
1959 so we can go ahead and output debug-info for it now. */
1960 TYPE_STUB_DECL (TREE_TYPE (decl)) = decl;
1961 rest_of_type_compilation (TREE_TYPE (decl), 1);
1962 }
1963 #endif /* DWARF_DEBUGGING_INFO */
1964
1965 /* Restore interface/implementation settings. */
1966 extract_interface_info ();
1967 }
1968 \f
1969 /* Store away the text of an template. */
1970
1971 void
1972 reinit_parse_for_template (yychar, d1, d2)
1973 int yychar;
1974 tree d1, d2;
1975 {
1976 struct template_info *template_info;
1977 extern struct obstack inline_text_obstack; /* see comment in lex.c */
1978
1979 if (d2 == NULL_TREE || d2 == error_mark_node)
1980 {
1981 lose:
1982 /* @@ Should use temp obstack, and discard results. */
1983 reinit_parse_for_block (yychar, &inline_text_obstack, 1);
1984 return;
1985 }
1986
1987 if (TREE_CODE (d2) == IDENTIFIER_NODE)
1988 d2 = IDENTIFIER_GLOBAL_VALUE (d2);
1989 if (!d2)
1990 goto lose;
1991 template_info = DECL_TEMPLATE_INFO (d2);
1992 if (!template_info)
1993 {
1994 template_info = (struct template_info *) permalloc (sizeof (struct template_info));
1995 bzero ((char *) template_info, sizeof (struct template_info));
1996 DECL_TEMPLATE_INFO (d2) = template_info;
1997 }
1998 template_info->filename = input_filename;
1999 template_info->lineno = lineno;
2000 reinit_parse_for_block (yychar, &inline_text_obstack, 1);
2001 template_info->text = obstack_base (&inline_text_obstack);
2002 template_info->length = obstack_object_size (&inline_text_obstack);
2003 obstack_finish (&inline_text_obstack);
2004 template_info->parm_vec = d1;
2005 }
2006
2007 /* Type unification.
2008
2009 We have a function template signature with one or more references to
2010 template parameters, and a parameter list we wish to fit to this
2011 template. If possible, produce a list of parameters for the template
2012 which will cause it to fit the supplied parameter list.
2013
2014 Return zero for success, 2 for an incomplete match that doesn't resolve
2015 all the types, and 1 for complete failure. An error message will be
2016 printed only for an incomplete match.
2017
2018 TPARMS[NTPARMS] is an array of template parameter types;
2019 TARGS[NTPARMS] is the array of template parameter values. PARMS is
2020 the function template's signature (using TEMPLATE_PARM_IDX nodes),
2021 and ARGS is the argument list we're trying to match against it.
2022
2023 If SUBR is 1, we're being called recursively (to unify the arguments of
2024 a function or method parameter of a function template), so don't zero
2025 out targs and don't fail on an incomplete match. */
2026
2027 int
2028 type_unification (tparms, targs, parms, args, nsubsts, subr)
2029 tree tparms, *targs, parms, args;
2030 int *nsubsts, subr;
2031 {
2032 tree parm, arg;
2033 int i;
2034 int ntparms = TREE_VEC_LENGTH (tparms);
2035
2036 my_friendly_assert (TREE_CODE (tparms) == TREE_VEC, 289);
2037 my_friendly_assert (TREE_CODE (parms) == TREE_LIST, 290);
2038 /* ARGS could be NULL (via a call from parse.y to
2039 build_x_function_call). */
2040 if (args)
2041 my_friendly_assert (TREE_CODE (args) == TREE_LIST, 291);
2042 my_friendly_assert (ntparms > 0, 292);
2043
2044 if (!subr)
2045 bzero ((char *) targs, sizeof (tree) * ntparms);
2046
2047 while (parms
2048 && parms != void_list_node
2049 && args
2050 && args != void_list_node)
2051 {
2052 parm = TREE_VALUE (parms);
2053 parms = TREE_CHAIN (parms);
2054 arg = TREE_VALUE (args);
2055 args = TREE_CHAIN (args);
2056
2057 if (arg == error_mark_node)
2058 return 1;
2059 if (arg == unknown_type_node)
2060 return 1;
2061
2062 if (! uses_template_parms (parm)
2063 && TREE_CODE_CLASS (TREE_CODE (arg)) != 't')
2064 {
2065 if (can_convert_arg (parm, TREE_TYPE (arg), arg))
2066 continue;
2067 return 1;
2068 }
2069
2070 #if 0
2071 if (TREE_CODE (arg) == VAR_DECL)
2072 arg = TREE_TYPE (arg);
2073 else if (TREE_CODE_CLASS (TREE_CODE (arg)) == 'e')
2074 arg = TREE_TYPE (arg);
2075 #else
2076 if (TREE_CODE_CLASS (TREE_CODE (arg)) != 't')
2077 {
2078 my_friendly_assert (TREE_TYPE (arg) != NULL_TREE, 293);
2079 if (TREE_CODE (arg) == TREE_LIST
2080 && TREE_TYPE (arg) == unknown_type_node
2081 && TREE_CODE (TREE_VALUE (arg)) == TEMPLATE_DECL)
2082 {
2083 int nsubsts, ntparms;
2084 tree *targs;
2085
2086 /* Have to back unify here */
2087 arg = TREE_VALUE (arg);
2088 nsubsts = 0;
2089 ntparms = TREE_VEC_LENGTH (DECL_TEMPLATE_PARMS (arg));
2090 targs = (tree *) alloca (sizeof (tree) * ntparms);
2091 parm = tree_cons (NULL_TREE, parm, NULL_TREE);
2092 return type_unification (DECL_TEMPLATE_PARMS (arg), targs,
2093 TYPE_ARG_TYPES (TREE_TYPE (arg)),
2094 parm, &nsubsts, 0);
2095 }
2096 arg = TREE_TYPE (arg);
2097 }
2098 #endif
2099 if (TREE_CODE (arg) == REFERENCE_TYPE)
2100 arg = TREE_TYPE (arg);
2101
2102 if (TREE_CODE (parm) != REFERENCE_TYPE)
2103 {
2104 if (TREE_CODE (arg) == FUNCTION_TYPE
2105 || TREE_CODE (arg) == METHOD_TYPE)
2106 arg = build_pointer_type (arg);
2107 else if (TREE_CODE (arg) == ARRAY_TYPE)
2108 arg = build_pointer_type (TREE_TYPE (arg));
2109 else
2110 arg = TYPE_MAIN_VARIANT (arg);
2111 }
2112
2113 switch (unify (tparms, targs, ntparms, parm, arg, nsubsts))
2114 {
2115 case 0:
2116 break;
2117 case 1:
2118 return 1;
2119 }
2120 }
2121 /* Fail if we've reached the end of the parm list, and more args
2122 are present, and the parm list isn't variadic. */
2123 if (args && args != void_list_node && parms == void_list_node)
2124 return 1;
2125 /* Fail if parms are left and they don't have default values. */
2126 if (parms
2127 && parms != void_list_node
2128 && TREE_PURPOSE (parms) == NULL_TREE)
2129 return 1;
2130 if (!subr)
2131 for (i = 0; i < ntparms; i++)
2132 if (!targs[i])
2133 {
2134 error ("incomplete type unification");
2135 return 2;
2136 }
2137 return 0;
2138 }
2139
2140 /* Tail recursion is your friend. */
2141 static int
2142 unify (tparms, targs, ntparms, parm, arg, nsubsts)
2143 tree tparms, *targs, parm, arg;
2144 int *nsubsts, ntparms;
2145 {
2146 int idx;
2147
2148 /* I don't think this will do the right thing with respect to types.
2149 But the only case I've seen it in so far has been array bounds, where
2150 signedness is the only information lost, and I think that will be
2151 okay. */
2152 while (TREE_CODE (parm) == NOP_EXPR)
2153 parm = TREE_OPERAND (parm, 0);
2154
2155 if (arg == error_mark_node)
2156 return 1;
2157 if (arg == unknown_type_node)
2158 return 1;
2159 if (arg == parm)
2160 return 0;
2161
2162 switch (TREE_CODE (parm))
2163 {
2164 case TEMPLATE_TYPE_PARM:
2165 (*nsubsts)++;
2166 if (TEMPLATE_TYPE_TPARMLIST (parm) != tparms)
2167 {
2168 error ("mixed template headers?!");
2169 my_friendly_abort (86);
2170 return 1;
2171 }
2172 idx = TEMPLATE_TYPE_IDX (parm);
2173 #if 0
2174 /* Template type parameters cannot contain cv-quals; i.e.
2175 template <class T> void f (T& a, T& b) will not generate
2176 void f (const int& a, const int& b). */
2177 if (TYPE_READONLY (arg) > TYPE_READONLY (parm)
2178 || TYPE_VOLATILE (arg) > TYPE_VOLATILE (parm))
2179 return 1;
2180 arg = TYPE_MAIN_VARIANT (arg);
2181 #else
2182 {
2183 int constp = TYPE_READONLY (arg) > TYPE_READONLY (parm);
2184 int volatilep = TYPE_VOLATILE (arg) > TYPE_VOLATILE (parm);
2185 arg = cp_build_type_variant (arg, constp, volatilep);
2186 }
2187 #endif
2188 /* Simple cases: Value already set, does match or doesn't. */
2189 if (targs[idx] == arg)
2190 return 0;
2191 else if (targs[idx])
2192 return 1;
2193 /* Check for mixed types and values. */
2194 if (TREE_CODE (TREE_VALUE (TREE_VEC_ELT (tparms, idx))) != TYPE_DECL)
2195 return 1;
2196 targs[idx] = arg;
2197 return 0;
2198 case TEMPLATE_CONST_PARM:
2199 (*nsubsts)++;
2200 idx = TEMPLATE_CONST_IDX (parm);
2201 if (targs[idx] == arg)
2202 return 0;
2203 else if (targs[idx])
2204 {
2205 tree t = targs[idx];
2206 if (TREE_CODE (t) == TREE_CODE (arg))
2207 switch (TREE_CODE (arg))
2208 {
2209 case INTEGER_CST:
2210 if (tree_int_cst_equal (t, arg))
2211 return 0;
2212 break;
2213 case REAL_CST:
2214 if (REAL_VALUES_EQUAL (TREE_REAL_CST (t), TREE_REAL_CST (arg)))
2215 return 0;
2216 break;
2217 /* STRING_CST values are not valid template const parms. */
2218 default:
2219 ;
2220 }
2221 my_friendly_abort (87);
2222 return 1;
2223 }
2224 /* else if (typeof arg != tparms[idx])
2225 return 1;*/
2226
2227 targs[idx] = copy_to_permanent (arg);
2228 return 0;
2229
2230 case POINTER_TYPE:
2231 if (TREE_CODE (arg) != POINTER_TYPE)
2232 return 1;
2233 return unify (tparms, targs, ntparms, TREE_TYPE (parm), TREE_TYPE (arg),
2234 nsubsts);
2235
2236 case REFERENCE_TYPE:
2237 if (TREE_CODE (arg) == REFERENCE_TYPE)
2238 arg = TREE_TYPE (arg);
2239 return unify (tparms, targs, ntparms, TREE_TYPE (parm), arg, nsubsts);
2240
2241 case ARRAY_TYPE:
2242 if (TREE_CODE (arg) != ARRAY_TYPE)
2243 return 1;
2244 if (unify (tparms, targs, ntparms, TYPE_DOMAIN (parm), TYPE_DOMAIN (arg),
2245 nsubsts) != 0)
2246 return 1;
2247 return unify (tparms, targs, ntparms, TREE_TYPE (parm), TREE_TYPE (arg),
2248 nsubsts);
2249
2250 case REAL_TYPE:
2251 case INTEGER_TYPE:
2252 if (TREE_CODE (arg) != TREE_CODE (parm))
2253 return 1;
2254
2255 if (TREE_CODE (parm) == INTEGER_TYPE)
2256 {
2257 if (TYPE_MIN_VALUE (parm) && TYPE_MIN_VALUE (arg)
2258 && unify (tparms, targs, ntparms,
2259 TYPE_MIN_VALUE (parm), TYPE_MIN_VALUE (arg), nsubsts))
2260 return 1;
2261 if (TYPE_MAX_VALUE (parm) && TYPE_MAX_VALUE (arg)
2262 && unify (tparms, targs, ntparms,
2263 TYPE_MAX_VALUE (parm), TYPE_MAX_VALUE (arg), nsubsts))
2264 return 1;
2265 }
2266 /* As far as unification is concerned, this wins. Later checks
2267 will invalidate it if necessary. */
2268 return 0;
2269
2270 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
2271 case INTEGER_CST:
2272 if (TREE_CODE (arg) != INTEGER_CST)
2273 return 1;
2274 return !tree_int_cst_equal (parm, arg);
2275
2276 case MINUS_EXPR:
2277 {
2278 tree t1, t2;
2279 t1 = TREE_OPERAND (parm, 0);
2280 t2 = TREE_OPERAND (parm, 1);
2281 return unify (tparms, targs, ntparms, t1,
2282 fold (build (PLUS_EXPR, integer_type_node, arg, t2)),
2283 nsubsts);
2284 }
2285
2286 case TREE_VEC:
2287 {
2288 int i;
2289 if (TREE_CODE (arg) != TREE_VEC)
2290 return 1;
2291 if (TREE_VEC_LENGTH (parm) != TREE_VEC_LENGTH (arg))
2292 return 1;
2293 for (i = TREE_VEC_LENGTH (parm) - 1; i >= 0; i--)
2294 if (unify (tparms, targs, ntparms,
2295 TREE_VEC_ELT (parm, i), TREE_VEC_ELT (arg, i),
2296 nsubsts))
2297 return 1;
2298 return 0;
2299 }
2300
2301 case UNINSTANTIATED_P_TYPE:
2302 {
2303 tree a;
2304 /* Unification of something that is not a class fails. */
2305 if (! IS_AGGR_TYPE (arg))
2306 return 1;
2307 a = IDENTIFIER_TEMPLATE (TYPE_IDENTIFIER (arg));
2308 if (a && UPT_TEMPLATE (parm) == TREE_PURPOSE (a))
2309 return unify (tparms, targs, ntparms, UPT_PARMS (parm),
2310 TREE_VALUE (a), nsubsts);
2311 /* FIXME: Should check base conversions here. */
2312 return 1;
2313 }
2314
2315 case RECORD_TYPE:
2316 if (TYPE_PTRMEMFUNC_FLAG (parm))
2317 return unify (tparms, targs, ntparms, TYPE_PTRMEMFUNC_FN_TYPE (parm),
2318 arg, nsubsts);
2319
2320 /* Allow trivial conversions. */
2321 if (TYPE_MAIN_VARIANT (parm) != TYPE_MAIN_VARIANT (arg)
2322 || TYPE_READONLY (parm) < TYPE_READONLY (arg)
2323 || TYPE_VOLATILE (parm) < TYPE_VOLATILE (arg))
2324 return 1;
2325 return 0;
2326
2327 case METHOD_TYPE:
2328 if (TREE_CODE (arg) != METHOD_TYPE)
2329 return 1;
2330 goto check_args;
2331
2332 case FUNCTION_TYPE:
2333 if (TREE_CODE (arg) != FUNCTION_TYPE)
2334 return 1;
2335 check_args:
2336 if (unify (tparms, targs, ntparms, TREE_TYPE (parm),
2337 TREE_TYPE (arg), nsubsts))
2338 return 1;
2339 return type_unification (tparms, targs, TYPE_ARG_TYPES (parm),
2340 TYPE_ARG_TYPES (arg), nsubsts, 1);
2341
2342 case OFFSET_TYPE:
2343 if (TREE_CODE (arg) != OFFSET_TYPE)
2344 return 1;
2345 if (unify (tparms, targs, ntparms, TYPE_OFFSET_BASETYPE (parm),
2346 TYPE_OFFSET_BASETYPE (arg), nsubsts))
2347 return 1;
2348 return unify (tparms, targs, ntparms, TREE_TYPE (parm),
2349 TREE_TYPE (arg), nsubsts);
2350
2351 default:
2352 sorry ("use of `%s' in template type unification",
2353 tree_code_name [(int) TREE_CODE (parm)]);
2354 return 1;
2355 }
2356 }
2357
2358 \f
2359 #undef DEBUG
2360
2361 int
2362 do_pending_expansions ()
2363 {
2364 struct pending_inline *i, *new_list = 0;
2365
2366 if (!pending_template_expansions)
2367 return 0;
2368
2369 #ifdef DEBUG
2370 fprintf (stderr, "\n\n\t\t IN DO_PENDING_EXPANSIONS\n\n");
2371 #endif
2372
2373 i = pending_template_expansions;
2374 while (i)
2375 {
2376 tree context;
2377
2378 struct pending_inline *next = i->next;
2379 tree t = i->fndecl;
2380
2381 int decision = 0;
2382 #define DECIDE(N) do {decision=(N); goto decided;} while(0)
2383
2384 my_friendly_assert (TREE_CODE (t) == FUNCTION_DECL
2385 || TREE_CODE (t) == VAR_DECL, 294);
2386 if (TREE_ASM_WRITTEN (t))
2387 DECIDE (0);
2388
2389 if (DECL_EXPLICIT_INSTANTIATION (t))
2390 DECIDE (DECL_NOT_REALLY_EXTERN (t));
2391 else if (! flag_implicit_templates)
2392 DECIDE (0);
2393
2394 if (i->interface == 1)
2395 /* OK, it was an implicit instantiation. */
2396 TREE_PUBLIC (t) = 0;
2397
2398 /* If it's a method, let the class type decide it.
2399 @@ What if the method template is in a separate file?
2400 Maybe both file contexts should be taken into account?
2401 Maybe only do this if i->interface == 1 (unknown)? */
2402 context = DECL_CONTEXT (t);
2403 if (context != NULL_TREE
2404 && TREE_CODE_CLASS (TREE_CODE (context)) == 't')
2405 {
2406 /* I'm interested in the context of this version of the function,
2407 not the original virtual declaration. */
2408 context = DECL_CLASS_CONTEXT (t);
2409
2410 /* If `unknown', we might want a static copy.
2411 If `implementation', we want a global one.
2412 If `interface', ext ref. */
2413 if (CLASSTYPE_INTERFACE_KNOWN (context))
2414 DECIDE (!CLASSTYPE_INTERFACE_ONLY (context));
2415 #if 0 /* This doesn't get us stuff needed only by the file initializer. */
2416 DECIDE (TREE_USED (t));
2417 #else /* This compiles too much stuff, but that's probably better in
2418 most cases than never compiling the stuff we need. */
2419 DECIDE (1);
2420 #endif
2421 }
2422
2423 if (i->interface == 1)
2424 DECIDE (TREE_USED (t));
2425 else
2426 DECIDE (i->interface);
2427
2428 decided:
2429 #ifdef DEBUG
2430 print_node_brief (stderr, decision ? "yes: " : "no: ", t, 0);
2431 fprintf (stderr, "\t%s\n",
2432 (DECL_ASSEMBLER_NAME (t)
2433 ? IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (t))
2434 : ""));
2435 #endif
2436 if (decision)
2437 {
2438 i->next = pending_inlines;
2439 pending_inlines = i;
2440 }
2441 else
2442 {
2443 i->next = new_list;
2444 new_list = i;
2445 }
2446 i = next;
2447 }
2448 pending_template_expansions = new_list;
2449 if (!pending_inlines)
2450 return 0;
2451 do_pending_inlines ();
2452 return 1;
2453 }
2454
2455 \f
2456 struct pending_template {
2457 struct pending_template *next;
2458 tree id;
2459 };
2460
2461 static struct pending_template* pending_templates;
2462
2463 void
2464 do_pending_templates ()
2465 {
2466 struct pending_template* t;
2467
2468 for ( t = pending_templates; t; t = t->next)
2469 {
2470 instantiate_class_template (t->id, 1);
2471 }
2472
2473 for ( t = pending_templates; t; t = pending_templates)
2474 {
2475 pending_templates = t->next;
2476 free(t);
2477 }
2478 }
2479
2480 static void
2481 add_pending_template (pt)
2482 tree pt;
2483 {
2484 struct pending_template *p;
2485
2486 p = (struct pending_template *) malloc (sizeof (struct pending_template));
2487 p->next = pending_templates;
2488 pending_templates = p;
2489 p->id = pt;
2490 }
2491
2492 void
2493 mark_function_instantiated (result, extern_p)
2494 tree result;
2495 int extern_p;
2496 {
2497 if (DECL_TEMPLATE_INSTANTIATION (result))
2498 SET_DECL_EXPLICIT_INSTANTIATION (result);
2499 TREE_PUBLIC (result) = 1;
2500
2501 if (! extern_p)
2502 {
2503 DECL_INTERFACE_KNOWN (result) = 1;
2504 DECL_NOT_REALLY_EXTERN (result) = 1;
2505 }
2506 }
2507
2508 /* called from the parser. */
2509 void
2510 do_function_instantiation (declspecs, declarator, storage)
2511 tree declspecs, declarator, storage;
2512 {
2513 tree decl = grokdeclarator (declarator, declspecs, NORMAL, 0, 0);
2514 tree name;
2515 tree fn;
2516 tree result = NULL_TREE;
2517 int extern_p = 0;
2518
2519 /* If we've already seen this template instance, use it. */
2520 if (name = DECL_ASSEMBLER_NAME (decl),
2521 fn = IDENTIFIER_GLOBAL_VALUE (name),
2522 fn && DECL_TEMPLATE_INSTANTIATION (fn))
2523 result = fn;
2524 else if (name = DECL_NAME (decl), fn = IDENTIFIER_GLOBAL_VALUE (name), fn)
2525 {
2526 for (fn = get_first_fn (fn); fn; fn = DECL_CHAIN (fn))
2527 if (decls_match (fn, decl)
2528 && DECL_DEFER_OUTPUT (fn))
2529 {
2530 result = fn;
2531 break;
2532 }
2533 else if (TREE_CODE (fn) == TEMPLATE_DECL)
2534 {
2535 int ntparms = TREE_VEC_LENGTH (DECL_TEMPLATE_PARMS (fn));
2536 tree *targs = (tree *) malloc (sizeof (tree) * ntparms);
2537 int i, dummy = 0;
2538 i = type_unification (DECL_TEMPLATE_PARMS (fn), targs,
2539 TYPE_ARG_TYPES (TREE_TYPE (fn)),
2540 TYPE_ARG_TYPES (TREE_TYPE (decl)),
2541 &dummy, 0);
2542 if (i == 0)
2543 {
2544 if (result)
2545 cp_error ("ambiguous template instantiation for `%D' requested", decl);
2546 else
2547 result = instantiate_template (fn, targs);
2548 }
2549 free (targs);
2550 }
2551 }
2552 if (! result)
2553 {
2554 cp_error ("no matching template for `%D' found", decl);
2555 return;
2556 }
2557
2558 if (flag_external_templates)
2559 return;
2560
2561 if (storage == NULL_TREE)
2562 ;
2563 else if (storage == ridpointers[(int) RID_EXTERN])
2564 extern_p = 1;
2565 else
2566 cp_error ("storage class `%D' applied to template instantiation",
2567 storage);
2568 mark_function_instantiated (result, extern_p);
2569 repo_template_instantiated (result, extern_p);
2570 }
2571
2572 void
2573 mark_class_instantiated (t, extern_p)
2574 tree t;
2575 int extern_p;
2576 {
2577 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
2578 SET_CLASSTYPE_INTERFACE_KNOWN (t);
2579 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
2580 CLASSTYPE_VTABLE_NEEDS_WRITING (t) = ! extern_p;
2581 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
2582 if (! extern_p)
2583 {
2584 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
2585 rest_of_type_compilation (t, 1);
2586 }
2587 }
2588
2589 void
2590 do_type_instantiation (name, storage)
2591 tree name, storage;
2592 {
2593 tree t = TREE_TYPE (name);
2594 int extern_p = 0;
2595 int nomem_p = 0;
2596
2597 /* With -fexternal-templates, explicit instantiations are treated the same
2598 as implicit ones. */
2599 if (flag_external_templates)
2600 return;
2601
2602 if (TYPE_SIZE (t) == NULL_TREE)
2603 {
2604 cp_error ("explicit instantiation of `%#T' before definition of template",
2605 t);
2606 return;
2607 }
2608
2609 if (storage == NULL_TREE)
2610 /* OK */;
2611 else if (storage == ridpointers[(int) RID_INLINE])
2612 nomem_p = 1;
2613 else if (storage == ridpointers[(int) RID_EXTERN])
2614 extern_p = 1;
2615 else
2616 {
2617 cp_error ("storage class `%D' applied to template instantiation",
2618 storage);
2619 extern_p = 0;
2620 }
2621
2622 /* We've already instantiated this. */
2623 if (CLASSTYPE_EXPLICIT_INSTANTIATION (t) && ! CLASSTYPE_INTERFACE_ONLY (t)
2624 && extern_p)
2625 return;
2626
2627 if (! CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
2628 {
2629 mark_class_instantiated (t, extern_p);
2630 repo_template_instantiated (t, extern_p);
2631 }
2632
2633 if (nomem_p)
2634 return;
2635
2636 {
2637 tree tmp;
2638 /* Classes nested in template classes currently don't have an
2639 IDENTIFIER_TEMPLATE--their out-of-line members are handled
2640 by the enclosing template class. Note that there are name
2641 conflict bugs with this approach. */
2642 tmp = TYPE_IDENTIFIER (t);
2643 if (IDENTIFIER_TEMPLATE (tmp))
2644 instantiate_member_templates (tmp);
2645
2646 /* this should really be done by instantiate_member_templates */
2647 tmp = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 0);
2648 for (; tmp; tmp = TREE_CHAIN (tmp))
2649 {
2650 mark_function_instantiated (tmp, extern_p);
2651 repo_template_instantiated (tmp, extern_p);
2652 }
2653
2654 #if 0
2655 for (tmp = TYPE_FIELDS (t); tmp; tmp = TREE_CHAIN (tmp))
2656 {
2657 if (TREE_CODE (tmp) == VAR_DECL)
2658 /* eventually do something */;
2659 }
2660 #endif
2661
2662 for (tmp = CLASSTYPE_TAGS (t); tmp; tmp = TREE_CHAIN (tmp))
2663 if (IS_AGGR_TYPE (TREE_VALUE (tmp)))
2664 do_type_instantiation (TYPE_MAIN_DECL (TREE_VALUE (tmp)), storage);
2665 }
2666 }
2667
2668 tree
2669 create_nested_upt (scope, name)
2670 tree scope, name;
2671 {
2672 tree t = make_lang_type (UNINSTANTIATED_P_TYPE);
2673 tree d = build_decl (TYPE_DECL, name, t);
2674
2675 TYPE_NAME (t) = d;
2676 TYPE_VALUES (t) = TYPE_VALUES (scope);
2677 TYPE_CONTEXT (t) = scope;
2678
2679 pushdecl (d);
2680 return d;
2681 }