OSDN Git Service

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