home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / gcc-2.4.5 / cp-pt.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-05-20  |  58.1 KB  |  2,124 lines

  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 "cp-tree.h"
  37. #include "cp-decl.h"
  38. #include "cp-parse.h"
  39.  
  40. extern struct obstack permanent_obstack;
  41. extern tree grokdeclarator ();
  42.  
  43. extern int lineno;
  44. extern char *input_filename;
  45. struct pending_inline *pending_template_expansions;
  46.  
  47. int processing_template_decl;
  48. int processing_template_defn;
  49.  
  50. #define obstack_chunk_alloc xmalloc
  51. #define obstack_chunk_free free
  52.  
  53. static int unify ();
  54. static void add_pending_template ();
  55.  
  56. void overload_template_name (), pop_template_decls ();
  57.  
  58. /* We've got a template header coming up; set obstacks up to save the
  59.    nodes created permanently.  (There might be cases with nested templates
  60.    where we don't have to do this, but they aren't implemented, and it
  61.    probably wouldn't be worth the effort.)  */
  62. void
  63. begin_template_parm_list ()
  64. {
  65.   pushlevel (0);
  66.   push_obstacks (&permanent_obstack, &permanent_obstack);
  67. }
  68.  
  69. /* Process information from new template parameter NEXT and append it to the
  70.    LIST being built.  The rules for use of a template parameter type name
  71.    by later parameters are not well-defined for us just yet.  However, the
  72.    only way to avoid having to parse expressions of unknown complexity (and
  73.    with tokens of unknown types) is to disallow it completely.    So for now,
  74.    that is what is assumed.  */
  75. tree
  76. process_template_parm (list, next)
  77.      tree list, next;
  78. {
  79.   tree parm;
  80.   int is_type;
  81.   parm = next;
  82.   my_friendly_assert (TREE_CODE (parm) == TREE_LIST, 259);
  83.   is_type = TREE_CODE (TREE_PURPOSE (parm)) == IDENTIFIER_NODE;
  84.   if (!is_type)
  85.     {
  86.       parm = TREE_PURPOSE (parm);
  87.       my_friendly_assert (TREE_CODE (parm) == TREE_LIST, 260);
  88.       parm = TREE_VALUE (parm);
  89.       /* is a const-param */
  90.       parm = grokdeclarator (TREE_VALUE (next), TREE_PURPOSE (next),
  91.                  NORMAL, 0, NULL_TREE);
  92.       /* A template parameter is not modifiable.  */
  93.       TREE_READONLY (parm) = 1;
  94.       if (TREE_CODE (TREE_TYPE (parm)) == RECORD_TYPE
  95.       || TREE_CODE (TREE_TYPE (parm)) == UNION_TYPE)
  96.     {
  97.       sorry ("aggregate template parameter types");
  98.       TREE_TYPE (parm) = void_type_node;
  99.     }
  100.     }
  101.   return chainon (list, parm);
  102. }
  103.  
  104. /* The end of a template parameter list has been reached.  Process the
  105.    tree list into a parameter vector, converting each parameter into a more
  106.    useful form.     Type parameters are saved as IDENTIFIER_NODEs, and others
  107.    as PARM_DECLs.  */
  108.  
  109. tree
  110. end_template_parm_list (parms)
  111.      tree parms;
  112. {
  113.   int nparms = 0;
  114.   tree saved_parmlist;
  115.   tree parm;
  116.   for (parm = parms; parm; parm = TREE_CHAIN (parm))
  117.     nparms++;
  118.   saved_parmlist = make_tree_vec (nparms);
  119.  
  120.   pushlevel (0);
  121.  
  122.   for (parm = parms, nparms = 0; parm; parm = TREE_CHAIN (parm), nparms++)
  123.     {
  124.       tree p = parm, decl;
  125.       if (TREE_CODE (p) == TREE_LIST)
  126.     {
  127.       tree t;
  128.       p = TREE_PURPOSE (p);
  129.       my_friendly_assert (TREE_CODE (p) == IDENTIFIER_NODE, 261);
  130.       t = make_node (TEMPLATE_TYPE_PARM);
  131.       TEMPLATE_TYPE_SET_INFO (t, saved_parmlist, nparms);
  132.       decl = build_lang_decl (TYPE_DECL, p, t);
  133.       TYPE_NAME (t) = decl;
  134.     }
  135.       else
  136.     {
  137.       tree tinfo = make_node (TEMPLATE_CONST_PARM);
  138.       my_friendly_assert (TREE_PERMANENT (tinfo), 262);
  139.       if (!TREE_PERMANENT (p))
  140.         {
  141.           tree old_p = p;
  142.           TREE_PERMANENT (old_p) = 1;
  143.           p = copy_node (p);
  144.           TREE_PERMANENT (old_p) = 0;
  145.         }
  146.       TEMPLATE_CONST_SET_INFO (tinfo, saved_parmlist, nparms);
  147.       TREE_TYPE (tinfo) = TREE_TYPE (p);
  148.       decl = build_decl (CONST_DECL, DECL_NAME (p), TREE_TYPE (p));
  149.       DECL_INITIAL (decl) = tinfo;
  150.     }
  151.       TREE_VEC_ELT (saved_parmlist, nparms) = p;
  152.       pushdecl (decl);
  153.     }
  154.   set_current_level_tags_transparency (1);
  155.   processing_template_decl++;
  156.   return saved_parmlist;
  157. }
  158.  
  159. /* end_template_decl is called after a template declaration is seen.
  160.    D1 is template header; D2 is class_head_sans_basetype or a
  161.    TEMPLATE_DECL with its DECL_RESULT field set.  */
  162. void
  163. end_template_decl (d1, d2, is_class)
  164.      tree d1, d2, is_class;
  165. {
  166.   tree decl;
  167.   struct template_info *tmpl;
  168.  
  169.   tmpl = (struct template_info *) obstack_alloc (&permanent_obstack,
  170.                         sizeof (struct template_info));
  171.   tmpl->text = 0;
  172.   tmpl->length = 0;
  173.   tmpl->aggr = is_class;
  174.  
  175.   /* cloned from reinit_parse_for_template */
  176.   tmpl->filename = input_filename;
  177.   tmpl->lineno = lineno;
  178.   tmpl->parm_vec = d1;          /* [eichin:19911015.2306EST] */
  179.  
  180. #ifdef DEBUG_CP_BINDING_LEVELS
  181.   indent_to (stderr, debug_bindings_indentation);
  182.   fprintf (stderr, "end_template_decl");
  183.   debug_bindings_indentation += 4;
  184. #endif
  185.  
  186.   if (d2 == NULL_TREE || d2 == error_mark_node)
  187.     {
  188.       decl = 0;
  189.       goto lose;
  190.     }
  191.  
  192.   if (is_class)
  193.     {
  194.       decl = build_lang_decl (TEMPLATE_DECL, d2, NULL_TREE);
  195.     }
  196.   else
  197.     {
  198.       if (TREE_CODE (d2) == TEMPLATE_DECL)
  199.     decl = d2;
  200.       else
  201.     {
  202.       /* Class destructor templates and operator templates are
  203.          slipping past as non-template nodes.  Process them here, since
  204.          I haven't figured out where to catch them earlier.  I could
  205.          go do that, but it's a choice between getting that done and
  206.          staying only N months behind schedule.  Sorry....  */
  207.       enum tree_code code;
  208.       my_friendly_assert (TREE_CODE (d2) == CALL_EXPR, 263);
  209.       code = TREE_CODE (TREE_OPERAND (d2, 0));
  210.       my_friendly_assert (code == BIT_NOT_EXPR
  211.           || code == OP_IDENTIFIER
  212.           || code == SCOPE_REF, 264);
  213.       d2 = grokdeclarator (d2, NULL_TREE, MEMFUNCDEF, 0, NULL_TREE);
  214.       decl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (d2),
  215.                   TREE_TYPE (d2));
  216.       DECL_TEMPLATE_RESULT (decl) = d2;
  217.       DECL_CONTEXT (decl) = DECL_CONTEXT (d2);
  218.       DECL_CLASS_CONTEXT (decl) = DECL_CLASS_CONTEXT (d2);
  219.       DECL_NAME (decl) = DECL_NAME (d2);
  220.       TREE_TYPE (decl) = TREE_TYPE (d2);
  221.       TREE_PUBLIC (decl) = TREE_PUBLIC (d2) = 0;
  222.       DECL_EXTERNAL (decl) = (DECL_EXTERNAL (d2)
  223.                   && !(DECL_CLASS_CONTEXT (d2)
  224.                        && !DECL_THIS_EXTERN (d2)));
  225.     }
  226.  
  227.       /* All routines creating TEMPLATE_DECL nodes should now be using
  228.      build_lang_decl, which will have set this up already.    */
  229.       my_friendly_assert (DECL_LANG_SPECIFIC (decl) != 0, 265);
  230.  
  231.       /* @@ Somewhere, permanent allocation isn't being used.  */
  232.       if (! DECL_TEMPLATE_IS_CLASS (decl)
  233.       && TREE_CODE (DECL_TEMPLATE_RESULT (decl)) == FUNCTION_DECL)
  234.     {
  235.       tree result = DECL_TEMPLATE_RESULT (decl);
  236.       /* Will do nothing if allocation was already permanent.  */
  237.       DECL_ARGUMENTS (result) = copy_to_permanent (DECL_ARGUMENTS (result));
  238.     }
  239.  
  240.       /* If this is for a method, there's an extra binding level here.    */
  241.       if (! DECL_TEMPLATE_IS_CLASS (decl)
  242.       && DECL_CONTEXT (DECL_TEMPLATE_RESULT (decl)) != NULL_TREE)
  243.     {
  244.       /* @@ Find out where this should be getting set!  */
  245.       tree r = DECL_TEMPLATE_RESULT (decl);
  246.       if (DECL_CLASS_CONTEXT (r) == NULL_TREE)
  247.         DECL_CLASS_CONTEXT (r) = DECL_CONTEXT (r);
  248.     }
  249.     }
  250.   DECL_TEMPLATE_INFO (decl) = tmpl;
  251.   DECL_TEMPLATE_PARMS (decl) = d1;
  252. lose:
  253.   if (decl)
  254.     {
  255.       /* If context of decl is non-null (i.e., method template), add it
  256.      to the appropriate class template, and pop the binding levels.  */
  257.       if (! DECL_TEMPLATE_IS_CLASS (decl)
  258.       && DECL_CONTEXT (DECL_TEMPLATE_RESULT (decl)) != NULL_TREE)
  259.     {
  260.       tree ctx = DECL_CONTEXT (DECL_TEMPLATE_RESULT (decl));
  261.       tree tmpl;
  262.       my_friendly_assert (TREE_CODE (ctx) == UNINSTANTIATED_P_TYPE, 266);
  263.       tmpl = UPT_TEMPLATE (ctx);
  264.       DECL_TEMPLATE_MEMBERS (tmpl) =
  265.         perm_tree_cons (DECL_NAME (decl), decl,
  266.                 DECL_TEMPLATE_MEMBERS (tmpl));
  267.       poplevel (0, 0, 0);
  268.       poplevel (0, 0, 0);
  269.     }
  270.       /* Otherwise, go back to top level first, and push the template decl
  271.      again there.  */
  272.       else
  273.     {
  274.       poplevel (0, 0, 0);
  275.       poplevel (0, 0, 0);
  276.       if (TREE_TYPE (decl)
  277.           && IDENTIFIER_GLOBAL_VALUE (DECL_NAME (decl)) != NULL_TREE)
  278.         push_overloaded_decl (decl, 0);
  279.       else
  280.         pushdecl (decl);
  281.     }
  282.     }
  283. #if 0 /* It happens sometimes, with syntactic or semantic errors.
  284.  
  285.      One specific case:
  286.      template <class A, int X, int Y> class Foo { ... };
  287.      template <class A, int X, int y> Foo<X,Y>::method (Foo& x) { ... }
  288.      Note the missing "A" in the class containing "method".  */
  289.   my_friendly_assert (global_bindings_p (), 267);
  290. #else
  291.   while (! global_bindings_p ())
  292.     poplevel (0, 0, 0);
  293. #endif
  294.   pop_obstacks ();
  295.   processing_template_decl--;
  296.   (void) get_pending_sizes ();
  297. #ifdef DEBUG_CP_BINDING_LEVELS
  298.   debug_bindings_indentation -= 4;
  299. #endif
  300. }
  301.  
  302.  
  303. /* Convert all template arguments to their appropriate types, and return
  304.    a vector containing the resulting values.  If any error occurs, return
  305.    error_mark_node.  */
  306. static tree
  307. coerce_template_parms (parms, arglist, in_decl)
  308.      tree parms, arglist;
  309.      tree in_decl;
  310. {
  311.   int nparms, i, lost = 0;
  312.   tree vec;
  313.  
  314.   if (TREE_CODE (arglist) == TREE_VEC)
  315.     nparms = TREE_VEC_LENGTH (arglist);
  316.   else
  317.     nparms = list_length (arglist);
  318.   if (nparms != TREE_VEC_LENGTH (parms))
  319.     {
  320.       error ("incorrect number of parameters (%d, should be %d)",
  321.          nparms, TREE_VEC_LENGTH (parms));
  322.       if (in_decl)
  323.     error_with_decl (in_decl, "in template expansion for decl `%s'");
  324.       return error_mark_node;
  325.     }
  326.  
  327.   if (TREE_CODE (arglist) == TREE_VEC)
  328.     vec = copy_node (arglist);
  329.   else
  330.     {
  331.       vec = make_tree_vec (nparms);
  332.       for (i = 0; i < nparms; i++)
  333.     {
  334.       tree arg = arglist;
  335.       arglist = TREE_CHAIN (arglist);
  336.       if (arg == error_mark_node)
  337.         lost++;
  338.       else
  339.         arg = TREE_VALUE (arg);
  340.       TREE_VEC_ELT (vec, i) = arg;
  341.     }
  342.     }
  343.   for (i = 0; i < nparms; i++)
  344.     {
  345.       tree arg = TREE_VEC_ELT (vec, i);
  346.       tree parm = TREE_VEC_ELT (parms, i);
  347.       tree val;
  348.       int is_type, requires_type;
  349.  
  350.       is_type = TREE_CODE_CLASS (TREE_CODE (arg)) == 't';
  351.       requires_type = TREE_CODE (parm) == IDENTIFIER_NODE;
  352.       if (is_type != requires_type)
  353.     {
  354.       if (in_decl)
  355.         error_with_decl (in_decl,
  356.                  "type/value mismatch in template parameter list for `%s'");
  357.       lost++;
  358.       TREE_VEC_ELT (vec, i) = error_mark_node;
  359.       continue;
  360.     }
  361.       if (is_type)
  362.     val = groktypename (arg);
  363.       else
  364.     val = digest_init (TREE_TYPE (parm), arg, (tree *) 0);
  365.  
  366.       if (val == error_mark_node)
  367.     lost++;
  368.  
  369.       TREE_VEC_ELT (vec, i) = val;
  370.     }
  371.   if (lost)
  372.     return error_mark_node;
  373.   return vec;
  374. }
  375.  
  376. /* Given class template name and parameter list, produce a user-friendly name
  377.    for the instantiation.  Note that this name isn't necessarily valid as
  378.    input to the compiler, because ">" characters may be adjacent.  */
  379. static char *
  380. mangle_class_name_for_template (name, parms, arglist)
  381.      char *name;
  382.      tree parms, arglist;
  383. {
  384.   static struct obstack scratch_obstack;
  385.   static char *scratch_firstobj;
  386.   int i, nparms;
  387.   char ibuf[100];
  388.  
  389.   if (!scratch_firstobj)
  390.     {
  391.       gcc_obstack_init (&scratch_obstack);
  392.       scratch_firstobj = obstack_alloc (&scratch_obstack, 1);
  393.     }
  394.   else
  395.     obstack_free (&scratch_obstack, scratch_firstobj);
  396.  
  397. #if 0
  398. #define buflen    sizeof(buf)
  399. #define check    if (bufp >= buf+buflen-1) goto too_long
  400. #define ccat(c) *bufp++=(c); check
  401. #define advance    bufp+=strlen(bufp); check
  402. #define cat(s)    strncpy(bufp, s, buf+buflen-bufp-1); advance
  403. #else
  404. #define check
  405. #define ccat(c)    obstack_1grow (&scratch_obstack, (c));
  406. #define advance
  407. #define cat(s)    obstack_grow (&scratch_obstack, (s), strlen (s))
  408. #endif
  409. #define icat(n)    sprintf(ibuf,"%d",(n)); cat(ibuf)
  410. #define xcat(n)    sprintf(ibuf,"%ux",n); cat(ibuf)
  411.  
  412.   cat (name);
  413.   ccat ('<');
  414.   nparms = TREE_VEC_LENGTH (parms);
  415.   my_friendly_assert (nparms == TREE_VEC_LENGTH (arglist), 268);
  416.   for (i = 0; i < nparms; i++)
  417.     {
  418.       tree parm = TREE_VEC_ELT (parms, i), arg = TREE_VEC_ELT (arglist, i);
  419.       tree type, id;
  420.  
  421.       if (i)
  422.     ccat (',');
  423.  
  424.       if (TREE_CODE (parm) == IDENTIFIER_NODE)
  425.     {
  426.       /* parm is a type */
  427.       char *typename;
  428.  
  429.       if (TYPE_NAME (arg)
  430.           && (TREE_CODE (arg) == RECORD_TYPE
  431.           || TREE_CODE (arg) == UNION_TYPE
  432.           || TREE_CODE (arg) == ENUMERAL_TYPE)
  433.           && TYPE_IDENTIFIER (arg)
  434.           && IDENTIFIER_POINTER (TYPE_IDENTIFIER (arg)))
  435.         typename = IDENTIFIER_POINTER (TYPE_IDENTIFIER (arg));
  436.       else
  437.         typename = type_as_string (arg);
  438.       cat (typename);
  439.       continue;
  440.     }
  441.       else
  442.     my_friendly_assert (TREE_CODE (parm) == PARM_DECL, 269);
  443.  
  444.       /* Should do conversions as for "const" initializers.  */
  445.       type = TREE_TYPE (parm);
  446.       id = DECL_NAME (parm);
  447.     
  448.       if (TREE_CODE (arg) == TREE_LIST)
  449.     {
  450.       /* New list cell was built because old chain link was in
  451.          use.  */
  452.       my_friendly_assert (TREE_PURPOSE (arg) == NULL_TREE, 270);
  453.       arg = TREE_VALUE (arg);
  454.     }
  455.       
  456.       switch (TREE_CODE (type))
  457.     {
  458.     case INTEGER_TYPE:
  459.     case ENUMERAL_TYPE:
  460.       if (TREE_CODE (arg) == INTEGER_CST)
  461.         {
  462.           if (TREE_INT_CST_HIGH (arg)
  463.           != (TREE_INT_CST_LOW (arg) >> (HOST_BITS_PER_WIDE_INT - 1)))
  464.         {
  465.           tree val = arg;
  466.           if (TREE_INT_CST_HIGH (val) < 0)
  467.             {
  468.               ccat ('-');
  469.               val = build_int_2 (~TREE_INT_CST_LOW (val),
  470.                      -TREE_INT_CST_HIGH (val));
  471.             }
  472.           /* Would "%x%0*x" or "%x%*0x" get zero-padding on all
  473.              systems?  */
  474.           {
  475.             static char format[10]; /* "%x%09999x\0" */
  476.             if (!format[0])
  477.               sprintf (format, "%%x%%0%dx", HOST_BITS_PER_INT / 4);
  478.             sprintf (ibuf, format, TREE_INT_CST_HIGH (val),
  479.                  TREE_INT_CST_LOW (val));
  480.             cat (ibuf);
  481.           }
  482.         }
  483.           else
  484.         icat (TREE_INT_CST_LOW (arg));
  485.         }
  486.       else
  487.         {
  488.           error ("invalid integer constant for template parameter");
  489.           cat ("*error*");
  490.         }
  491.       break;
  492. #ifndef REAL_IS_NOT_DOUBLE
  493.     case REAL_TYPE:
  494.       sprintf (ibuf, "%e", TREE_REAL_CST (arg));
  495.       cat (ibuf);
  496.       break;
  497. #endif
  498.     case POINTER_TYPE:
  499.       if (TREE_CODE (arg) != ADDR_EXPR)
  500.         {
  501.           error ("invalid pointer constant for template parameter");
  502.           cat ("*error*");
  503.           break;
  504.         }
  505.       ccat ('&');
  506.       arg = TREE_OPERAND (arg, 0);
  507.       if (TREE_CODE (arg) == FUNCTION_DECL)
  508.         cat (fndecl_as_string (0, arg, 0));
  509.       else
  510.         {
  511.           my_friendly_assert (TREE_CODE_CLASS (TREE_CODE (arg)) == 'd',
  512.                   271);
  513.           cat (IDENTIFIER_POINTER (DECL_NAME (arg)));
  514.         }
  515.       break;
  516.     default:
  517.       sorry ("encoding %s as template parm",
  518.          tree_code_name [(int) TREE_CODE (type)]);
  519.       my_friendly_abort (81);
  520.     }
  521.     }
  522.   {
  523.     char *bufp = obstack_next_free (&scratch_obstack);
  524.     int offset = 0;
  525.     while (bufp[offset - 1] == ' ')
  526.       offset--;
  527.     obstack_blank_fast (&scratch_obstack, offset);
  528.   }
  529.   ccat ('>');
  530.   ccat ('\0');
  531.   return (char *) obstack_base (&scratch_obstack);
  532.  
  533.  too_long:
  534.   fatal ("out of (preallocated) string space creating template instantiation name");
  535.   /* NOTREACHED */
  536.   return NULL;
  537. }
  538.  
  539. /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
  540.    parameters, find the desired type.
  541.  
  542.    D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
  543.    Since ARGLIST is build on the decl_obstack, we must copy it here
  544.    to keep it from being reclaimed when the decl storage is reclaimed.
  545.  
  546.    IN_DECL, if non-NULL, is the template declaration we are trying to
  547.    instantiate.  */
  548. tree
  549. lookup_template_class (d1, arglist, in_decl)
  550.      tree d1, arglist;
  551.      tree in_decl;
  552. {
  553.   tree template, parmlist;
  554.   char *mangled_name;
  555.   tree id;
  556.  
  557.   my_friendly_assert (TREE_CODE (d1) == IDENTIFIER_NODE, 272);
  558.   template = IDENTIFIER_GLOBAL_VALUE (d1); /* XXX */
  559.   if (! template)
  560.     template = IDENTIFIER_CLASS_VALUE (d1);
  561.   /* With something like `template <class T> class X class X { ... };'
  562.      we could end up with D1 having nothing but an IDENTIFIER_LOCAL_VALUE.
  563.      We don't want to do that, but we have to deal with the situation, so
  564.      let's give them some syntax errors to chew on instead of a crash.  */
  565.   if (! template)
  566.     return error_mark_node;
  567.   if (TREE_CODE (template) != TEMPLATE_DECL)
  568.     {
  569.       error ("non-template type '%s' used as a template",
  570.          IDENTIFIER_POINTER (d1));
  571.       if (in_decl)
  572.     error_with_decl (in_decl, "for template declaration `%s'");
  573.       return error_mark_node;
  574.     }
  575.   parmlist = DECL_TEMPLATE_PARMS (template);
  576.  
  577.   arglist = coerce_template_parms (parmlist, arglist, in_decl);
  578.   if (arglist == error_mark_node)
  579.     return error_mark_node;
  580.   if (uses_template_parms (arglist))
  581.     {
  582.       tree t = make_lang_type (UNINSTANTIATED_P_TYPE);
  583.       tree d;
  584.       id = make_anon_name ();
  585.       d = build_lang_decl (TYPE_DECL, id, t);
  586.       TYPE_NAME (t) = d;
  587.       TYPE_VALUES (t) = build_tree_list (template, arglist);
  588.       pushdecl_top_level (d);
  589.     }
  590.   else
  591.     {
  592.       mangled_name = mangle_class_name_for_template (IDENTIFIER_POINTER (d1),
  593.                              parmlist, arglist);
  594.       id = get_identifier (mangled_name);
  595.     }
  596.   if (!IDENTIFIER_TEMPLATE (id))
  597.     {
  598.       arglist = copy_to_permanent (arglist);
  599.       IDENTIFIER_TEMPLATE (id) = perm_tree_cons (template, arglist, NULL_TREE);
  600.     }
  601.   return id;
  602. }
  603.  
  604. void
  605. push_template_decls (parmlist, arglist, class_level)
  606.      tree parmlist, arglist;
  607.      int class_level;
  608. {
  609.   int i, nparms;
  610.  
  611. #ifdef DEBUG_CP_BINDING_LEVELS
  612.   indent_to (stderr, debug_bindings_indentation);
  613.   fprintf (stderr, "push_template_decls");
  614.   debug_bindings_indentation += 4;
  615. #endif
  616.  
  617.   /* Don't want to push values into global context.  */
  618.   if (!class_level)
  619.     pushlevel (0);
  620.   nparms = TREE_VEC_LENGTH (parmlist);
  621.  
  622.   for (i = 0; i < nparms; i++)
  623.     {
  624.       int requires_type, is_type;
  625.       tree parm = TREE_VEC_ELT (parmlist, i);
  626.       tree arg = TREE_VEC_ELT (arglist, i);
  627.       tree decl = 0;
  628.  
  629.       requires_type = TREE_CODE (parm) == IDENTIFIER_NODE;
  630.       is_type = TREE_CODE_CLASS (TREE_CODE (arg)) == 't';
  631.       if (is_type)
  632.     {
  633.       /* add typename to namespace */
  634.       if (!requires_type)
  635.         {
  636.           error ("template use error: type provided where value needed");
  637.           continue;
  638.         }
  639.       decl = arg;
  640.       my_friendly_assert (TREE_CODE_CLASS (TREE_CODE (decl)) == 't', 273);
  641.       decl = build_lang_decl (TYPE_DECL, parm, decl);
  642.     }
  643.       else
  644.     {
  645.       /* add const decl to namespace */
  646.       tree val;
  647.       if (requires_type)
  648.         {
  649.           error ("template use error: value provided where type needed");
  650.           continue;
  651.         }
  652.       val = digest_init (TREE_TYPE (parm), arg, (tree *) 0);
  653.       if (val != error_mark_node)
  654.         {
  655.           decl = build_decl (VAR_DECL, DECL_NAME (parm), TREE_TYPE (parm));
  656.           DECL_INITIAL (decl) = val;
  657.           TREE_READONLY (decl) = 1;
  658.         }
  659.     }
  660.       if (decl != 0)
  661.     {
  662.       layout_decl (decl, 0);
  663.       if (class_level)
  664.         pushdecl_class_level (decl);
  665.       else
  666.         pushdecl (decl);
  667.     }
  668.     }
  669.   if (!class_level)
  670.     set_current_level_tags_transparency (1);
  671. #ifdef DEBUG_CP_BINDING_LEVELS
  672.   debug_bindings_indentation -= 4;
  673. #endif
  674. }
  675.  
  676. void
  677. pop_template_decls (parmlist, arglist, class_level)
  678.      tree parmlist, arglist;
  679.      int class_level;
  680. {
  681. #ifdef DEBUG_CP_BINDING_LEVELS
  682.   indent_to (stderr, debug_bindings_indentation);
  683.   fprintf (stderr, "pop_template_decls");
  684.   debug_bindings_indentation += 4;
  685. #endif
  686.  
  687.   if (!class_level)
  688.     poplevel (0, 0, 0);
  689.  
  690. #ifdef DEBUG_CP_BINDING_LEVELS
  691.   debug_bindings_indentation -= 4;
  692. #endif
  693. }
  694.  
  695. /* Should be defined in cp-parse.h.  */
  696. extern int yychar;
  697.  
  698. int
  699. uses_template_parms (t)
  700.      tree t;
  701. {
  702.   if (!t)
  703.     return 0;
  704.   switch (TREE_CODE (t))
  705.     {
  706.     case INDIRECT_REF:
  707.     case COMPONENT_REF:
  708.       /* We assume that the object must be instantiated in order to build
  709.      the COMPONENT_REF, so we test only whether the type of the
  710.      COMPONENT_REF uses template parms.  */
  711.       return uses_template_parms (TREE_TYPE (t));
  712.  
  713.     case IDENTIFIER_NODE:
  714.       if (!IDENTIFIER_TEMPLATE (t))
  715.     return 0;
  716.       return uses_template_parms (TREE_VALUE (IDENTIFIER_TEMPLATE (t)));
  717.  
  718.       /* aggregates of tree nodes */
  719.     case TREE_VEC:
  720.       {
  721.     int i = TREE_VEC_LENGTH (t);
  722.     while (i--)
  723.       if (uses_template_parms (TREE_VEC_ELT (t, i)))
  724.         return 1;
  725.     return 0;
  726.       }
  727.     case TREE_LIST:
  728.       if (uses_template_parms (TREE_PURPOSE (t))
  729.       || uses_template_parms (TREE_VALUE (t)))
  730.     return 1;
  731.       return uses_template_parms (TREE_CHAIN (t));
  732.  
  733.       /* constructed type nodes */
  734.     case POINTER_TYPE:
  735.     case REFERENCE_TYPE:
  736.       return uses_template_parms (TREE_TYPE (t));
  737.     case RECORD_TYPE:
  738.     case UNION_TYPE:
  739.       if (!TYPE_NAME (t))
  740.     return 0;
  741.       if (!TYPE_IDENTIFIER (t))
  742.     return 0;
  743.       return uses_template_parms (TYPE_IDENTIFIER (t));
  744.     case FUNCTION_TYPE:
  745.       if (uses_template_parms (TYPE_ARG_TYPES (t)))
  746.     return 1;
  747.       return uses_template_parms (TREE_TYPE (t));
  748.     case ARRAY_TYPE:
  749.       if (uses_template_parms (TYPE_DOMAIN (t)))
  750.     return 1;
  751.       return uses_template_parms (TREE_TYPE (t));
  752.     case OFFSET_TYPE:
  753.       if (uses_template_parms (TYPE_OFFSET_BASETYPE (t)))
  754.     return 1;
  755.       return uses_template_parms (TREE_TYPE (t));
  756.     case METHOD_TYPE:
  757.       if (uses_template_parms (TYPE_OFFSET_BASETYPE (t)))
  758.     return 1;
  759.       if (uses_template_parms (TYPE_ARG_TYPES (t)))
  760.     return 1;
  761.       return uses_template_parms (TREE_TYPE (t));
  762.  
  763.       /* decl nodes */
  764.     case TYPE_DECL:
  765.       return uses_template_parms (DECL_NAME (t));
  766.     case FUNCTION_DECL:
  767.       if (uses_template_parms (TREE_TYPE (t)))
  768.     return 1;
  769.       /* fall through */
  770.     case VAR_DECL:
  771.     case PARM_DECL:
  772.       /* ??? What about FIELD_DECLs?  */
  773.       /* The type of a decl can't use template parms if the name of the
  774.      variable doesn't, because it's impossible to resolve them.  So
  775.      ignore the type field for now.     */
  776.       if (DECL_CONTEXT (t) && uses_template_parms (DECL_CONTEXT (t)))
  777.     return 1;
  778.       if (uses_template_parms (TREE_TYPE (t)))
  779.     {
  780.       error ("template parms used where they can't be resolved");
  781.     }
  782.       return 0;
  783.  
  784.     case CALL_EXPR:
  785.       return uses_template_parms (TREE_TYPE (t));
  786.     case ADDR_EXPR:
  787.       return uses_template_parms (TREE_OPERAND (t, 0));
  788.  
  789.       /* template parm nodes */
  790.     case TEMPLATE_TYPE_PARM:
  791.     case TEMPLATE_CONST_PARM:
  792.       return 1;
  793.  
  794.       /* simple type nodes */
  795.     case INTEGER_TYPE:
  796.       if (uses_template_parms (TYPE_MIN_VALUE (t)))
  797.     return 1;
  798.       return uses_template_parms (TYPE_MAX_VALUE (t));
  799.  
  800.     case REAL_TYPE:
  801.     case VOID_TYPE:
  802.     case ENUMERAL_TYPE:
  803.       return 0;
  804.  
  805.       /* constants */
  806.     case INTEGER_CST:
  807.     case REAL_CST:
  808.     case STRING_CST:
  809.       return 0;
  810.  
  811.     case ERROR_MARK:
  812.       /* Non-error_mark_node ERROR_MARKs are bad things.  */
  813.       my_friendly_assert (t == error_mark_node, 274);
  814.       /* NOTREACHED */
  815.       return 0;
  816.  
  817.     case UNINSTANTIATED_P_TYPE:
  818.       return 1;
  819.  
  820.     default:
  821.       switch (TREE_CODE_CLASS (TREE_CODE (t)))
  822.     {
  823.     case '1':
  824.     case '2':
  825.     case '3':
  826.     case '<':
  827.       {
  828.         int i;
  829.         for (i = tree_code_length[(int) TREE_CODE (t)]; --i >= 0;)
  830.           if (uses_template_parms (TREE_OPERAND (t, i)))
  831.         return 1;
  832.         return 0;
  833.       }
  834.     default:
  835.       break;
  836.     }
  837.       sorry ("testing %s for template parms",
  838.          tree_code_name [(int) TREE_CODE (t)]);
  839.       my_friendly_abort (82);
  840.       /* NOTREACHED */
  841.       return 0;
  842.     }
  843. }
  844.  
  845. void
  846. instantiate_member_templates (arg)
  847.      tree arg;
  848. {
  849.   tree t;
  850.   tree classname = TREE_VALUE (arg);
  851.   tree id = classname;
  852.   tree members = DECL_TEMPLATE_MEMBERS (TREE_PURPOSE (IDENTIFIER_TEMPLATE (id)));
  853.  
  854.   for (t = members; t; t = TREE_CHAIN (t))
  855.     {
  856.       tree parmvec, type, classparms, tdecl, t2;
  857.       int nparms, xxx, i;
  858.  
  859.       my_friendly_assert (TREE_VALUE (t) != NULL_TREE, 275);
  860.       my_friendly_assert (TREE_CODE (TREE_VALUE (t)) == TEMPLATE_DECL, 276);
  861.       /* @@ Should verify that class parm list is a list of
  862.      distinct template parameters, and covers all the template
  863.      parameters.  */
  864.       tdecl = TREE_VALUE (t);
  865.       type = DECL_CONTEXT (DECL_TEMPLATE_RESULT (tdecl));
  866.       classparms = UPT_PARMS (type);
  867.       nparms = TREE_VEC_LENGTH (classparms);
  868.       parmvec = make_tree_vec (nparms);
  869.       for (i = 0; i < nparms; i++)
  870.     TREE_VEC_ELT (parmvec, i) = NULL_TREE;
  871.       switch (unify (DECL_TEMPLATE_PARMS (tdecl),
  872.              &TREE_VEC_ELT (parmvec, 0), nparms,
  873.              type, IDENTIFIER_TYPE_VALUE (classname),
  874.              &xxx))
  875.     {
  876.     case 0:
  877.       /* Success -- well, no inconsistency, at least.  */
  878.       for (i = 0; i < nparms; i++)
  879.         if (TREE_VEC_ELT (parmvec, i) == NULL_TREE)
  880.           goto failure;
  881.       t2 = instantiate_template (tdecl,
  882.                      &TREE_VEC_ELT (parmvec, 0));
  883.       type = IDENTIFIER_TYPE_VALUE (id);
  884.       my_friendly_assert (type != 0, 277);
  885.       if (CLASSTYPE_INTERFACE_UNKNOWN (type))
  886.         {
  887.           DECL_EXTERNAL (t2) = 0;
  888.           TREE_PUBLIC (t2) = 0;
  889.         }
  890.       else
  891.         {
  892.           DECL_EXTERNAL (t2) = CLASSTYPE_INTERFACE_ONLY (type);
  893.           TREE_PUBLIC (t2) = 1;
  894.         }
  895.       break;
  896.     case 1:
  897.       /* Failure.  */
  898.     failure:
  899.       error ("type unification error instantiating %s::%s",
  900.          IDENTIFIER_POINTER (classname),
  901.          IDENTIFIER_POINTER (DECL_NAME (tdecl)));
  902.       error_with_decl (tdecl, "for template declaration `%s'");
  903.  
  904.       continue /* loop of members */;
  905.     default:
  906.       /* Eek, a bug.  */
  907.       my_friendly_abort (83);
  908.     }
  909.     }
  910. }
  911.  
  912. tree
  913. instantiate_class_template (classname, setup_parse)
  914.      tree classname;
  915.      int setup_parse;
  916. {
  917.   struct template_info *template_info;
  918.   tree template, t1;
  919.  
  920.   if (classname == error_mark_node)
  921.     return error_mark_node;
  922.  
  923.   my_friendly_assert (TREE_CODE (classname) == IDENTIFIER_NODE, 278);
  924.   template = IDENTIFIER_TEMPLATE (classname);
  925.  
  926.   if (IDENTIFIER_HAS_TYPE_VALUE (classname))
  927.     {
  928.       tree type = IDENTIFIER_TYPE_VALUE (classname);
  929.       if (TREE_CODE (type) == UNINSTANTIATED_P_TYPE)
  930.     return type;
  931.       if (TYPE_BEING_DEFINED (type)
  932.       || TYPE_SIZE (type)
  933.       || CLASSTYPE_USE_TEMPLATE (type) != 0)
  934.     return type;
  935.     }
  936.   if (uses_template_parms (classname))
  937.     {
  938.       if (!TREE_TYPE (classname))
  939.     {
  940.       tree t = make_lang_type (RECORD_TYPE);
  941.       tree d = build_lang_decl (TYPE_DECL, classname, t);
  942.       DECL_NAME (d) = classname;
  943.       TYPE_NAME (t) = d;
  944.       pushdecl (d);
  945.     }
  946.       return NULL_TREE;
  947.     }
  948.  
  949.   t1 = TREE_PURPOSE (template);
  950.   my_friendly_assert (TREE_CODE (t1) == TEMPLATE_DECL, 279);
  951.  
  952.   /* If a template is declared but not defined, accept it; don't crash.
  953.      Later uses requiring the definition will be flagged as errors by
  954.      other code.  Thanks to niklas@appli.se for this bug fix.  */
  955.   if (DECL_TEMPLATE_INFO (t1)->text == 0)
  956.     setup_parse = 0;
  957.  
  958. #ifdef DEBUG_CP_BINDING_LEVELS
  959.   indent_to (stderr, debug_bindings_indentation);
  960.   fprintf (stderr, "instantiate_class_template");
  961.   debug_bindings_indentation += 4;
  962. #endif
  963.  
  964.   push_to_top_level ();
  965.   push_template_decls (DECL_TEMPLATE_PARMS (TREE_PURPOSE (template)),
  966.                TREE_VALUE (template), 0);
  967.   set_current_level_tags_transparency (1);
  968.   template_info = DECL_TEMPLATE_INFO (t1);
  969.   if (setup_parse)
  970.     {
  971.       feed_input (template_info->text, template_info->length, (struct obstack *)0);
  972.       lineno = template_info->lineno;
  973.       input_filename = template_info->filename;
  974.       /* Get interface/implementation back in sync.  */
  975.       extract_interface_info ();
  976.       overload_template_name (classname, 0);
  977.       yychar = PRE_PARSED_CLASS_DECL;
  978.       yylval.ttype = build_tree_list (class_type_node, classname);
  979.       processing_template_defn++;
  980.     }
  981.   else
  982.     {
  983.       tree t, decl, id, tmpl;
  984.  
  985.       id = classname;
  986.       tmpl = TREE_PURPOSE (IDENTIFIER_TEMPLATE (id));
  987.       t = xref_tag (DECL_TEMPLATE_INFO (tmpl)->aggr, id, NULL_TREE);
  988.       my_friendly_assert (TREE_CODE (t) == RECORD_TYPE, 280);
  989. #if 1
  990.       lineno = template_info->lineno;
  991.       input_filename = template_info->filename;
  992.       /* Get interface/implementation back in sync.  */
  993.       extract_interface_info ();
  994. #endif
  995.  
  996.       /* Now, put a copy of the decl in global scope, to avoid
  997.        * recursive expansion.  */
  998.       decl = IDENTIFIER_LOCAL_VALUE (id);
  999.       if (!decl)
  1000.     decl = IDENTIFIER_CLASS_VALUE (id);
  1001.       if (decl)
  1002.     {
  1003.       my_friendly_assert (TREE_CODE (decl) == TYPE_DECL, 281);
  1004.       /* We'd better make sure we're on the permanent obstack or else
  1005.        * we'll get a "friendly" abort 124 in pushdecl.  Perhaps a
  1006.        * copy_to_permanent would be sufficient here, but then a
  1007.        * sharing problem might occur.  I don't know -- niklas@appli.se */
  1008.       push_obstacks (&permanent_obstack, &permanent_obstack);
  1009.       pushdecl_top_level (copy_node (decl));
  1010.       pop_obstacks ();
  1011.     }
  1012.       pop_from_top_level ();
  1013.     }
  1014.  
  1015. #ifdef DEBUG_CP_BINDING_LEVELS
  1016.   debug_bindings_indentation -= 4;
  1017. #endif
  1018.  
  1019.   return NULL_TREE;
  1020. }
  1021.  
  1022. static int
  1023. list_eq (t1, t2)
  1024.      tree t1, t2;
  1025. {
  1026.   if (t1 == NULL_TREE)
  1027.     return t2 == NULL_TREE;
  1028.   if (t2 == NULL_TREE)
  1029.     return 0;
  1030.   /* Don't care if one declares its arg const and the other doesn't -- the
  1031.      main variant of the arg type is all that matters.  */
  1032.   if (TYPE_MAIN_VARIANT (TREE_VALUE (t1))
  1033.       != TYPE_MAIN_VARIANT (TREE_VALUE (t2)))
  1034.     return 0;
  1035.   return list_eq (TREE_CHAIN (t1), TREE_CHAIN (t2));
  1036. }
  1037.  
  1038. static tree
  1039. tsubst (t, args, nargs, in_decl)
  1040.      tree t, *args;
  1041.      int nargs;
  1042.      tree in_decl;
  1043. {
  1044.   tree type;
  1045.  
  1046.   if (t == NULL_TREE || t == error_mark_node)
  1047.     return t;
  1048.  
  1049.   type = TREE_TYPE (t);
  1050.   if (type
  1051.       /* Minor optimization.
  1052.      ?? Are these really the most frequent cases?  Is the savings
  1053.      significant?  */
  1054.       && type != integer_type_node
  1055.       && type != void_type_node
  1056.       && type != char_type_node)
  1057.     type = build_type_variant (tsubst (type, args, nargs, in_decl),
  1058.                    TYPE_READONLY (type),
  1059.                    TYPE_VOLATILE (type));
  1060.   switch (TREE_CODE (t))
  1061.     {
  1062.     case ERROR_MARK:
  1063.     case IDENTIFIER_NODE:
  1064.     case OP_IDENTIFIER:
  1065.     case VOID_TYPE:
  1066.     case REAL_TYPE:
  1067.     case ENUMERAL_TYPE:
  1068.     case INTEGER_CST:
  1069.     case REAL_CST:
  1070.     case STRING_CST:
  1071.     case RECORD_TYPE:
  1072.     case UNION_TYPE:
  1073.       return t;
  1074.  
  1075.     case INTEGER_TYPE:
  1076.       if (t == integer_type_node)
  1077.     return t;
  1078.  
  1079.       if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
  1080.       && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
  1081.     return t;
  1082.       return build_index_2_type (tsubst (TYPE_MIN_VALUE (t), args, nargs, in_decl),
  1083.                  tsubst (TYPE_MAX_VALUE (t), args, nargs, in_decl));
  1084.  
  1085.     case TEMPLATE_TYPE_PARM:
  1086.       return build_type_variant (args[TEMPLATE_TYPE_IDX (t)],
  1087.                  TYPE_READONLY (t),
  1088.                  TYPE_VOLATILE (t));
  1089.  
  1090.     case TEMPLATE_CONST_PARM:
  1091.       return args[TEMPLATE_CONST_IDX (t)];
  1092.  
  1093.     case FUNCTION_DECL:
  1094.       {
  1095.     tree r;
  1096.     tree fnargs, result;
  1097.     
  1098.     if (type == TREE_TYPE (t)
  1099.         && (DECL_CONTEXT (t) == NULL_TREE
  1100.         || TREE_CODE_CLASS (TREE_CODE (DECL_CONTEXT (t))) != 't'))
  1101.       return t;
  1102.     fnargs = tsubst (DECL_ARGUMENTS (t), args, nargs, t);
  1103.     result = tsubst (DECL_RESULT (t), args, nargs, t);
  1104.     if (DECL_CONTEXT (t) != NULL_TREE
  1105.         && TREE_CODE_CLASS (TREE_CODE (DECL_CONTEXT (t))) == 't')
  1106.       {
  1107.         /* Look it up in that class, and return the decl node there,
  1108.            instead of creating a new one.  */
  1109.         tree ctx, methods, name, method;
  1110.         int n_methods;
  1111.         int i, found = 0;
  1112.  
  1113.         name = DECL_NAME (t);
  1114.         ctx = tsubst (DECL_CONTEXT (t), args, nargs, t);
  1115.         methods = CLASSTYPE_METHOD_VEC (ctx);
  1116.         if (methods == NULL_TREE)
  1117.           /* No methods at all -- no way this one can match.  */
  1118.           goto no_match;
  1119.         n_methods = TREE_VEC_LENGTH (methods);
  1120.  
  1121.         r = NULL_TREE;
  1122.  
  1123.         if (!strncmp (OPERATOR_TYPENAME_FORMAT,
  1124.               IDENTIFIER_POINTER (name),
  1125.               sizeof (OPERATOR_TYPENAME_FORMAT) - 1))
  1126.           {
  1127.         /* Type-conversion operator.  Reconstruct the name, in
  1128.            case it's the name of one of the template's parameters.  */
  1129.         name = build_typename_overload (TREE_TYPE (type));
  1130.           }
  1131.  
  1132.         if (DECL_CONTEXT (t) != NULL_TREE
  1133.         && TREE_CODE_CLASS (TREE_CODE (DECL_CONTEXT (t))) == 't'
  1134.         && constructor_name (DECL_CONTEXT (t)) == DECL_NAME (t))
  1135.           name = constructor_name (ctx);
  1136. #if 0
  1137.         fprintf (stderr, "\nfor function %s in class %s:\n",
  1138.              IDENTIFIER_POINTER (name),
  1139.              IDENTIFIER_POINTER (TYPE_IDENTIFIER (ctx)));
  1140. #endif
  1141.         for (i = 0; i < n_methods; i++)
  1142.           {
  1143.         int pass;
  1144.  
  1145.         method = TREE_VEC_ELT (methods, i);
  1146.         if (method == NULL_TREE || DECL_NAME (method) != name)
  1147.           continue;
  1148.  
  1149.         pass = 0;
  1150.           maybe_error:
  1151.         for (; method; method = TREE_CHAIN (method))
  1152.           {
  1153.             my_friendly_assert (TREE_CODE (method) == FUNCTION_DECL,
  1154.                     282);
  1155.             if (TREE_TYPE (method) != type)
  1156.               {
  1157.             tree mtype = TREE_TYPE (method);
  1158.             tree t1, t2;
  1159.  
  1160.             /* Keep looking for a method that matches
  1161.                perfectly.  This takes care of the problem
  1162.                where destructors (which have implicit int args)
  1163.                look like constructors which have an int arg.  */
  1164.             if (pass == 0)
  1165.               continue;
  1166.  
  1167.             t1 = TYPE_ARG_TYPES (mtype);
  1168.             t2 = TYPE_ARG_TYPES (type);
  1169.             if (TREE_CODE (mtype) == FUNCTION_TYPE)
  1170.               t2 = TREE_CHAIN (t2);
  1171.  
  1172.             if (list_eq (t1, t2))
  1173.               {
  1174.                 if (TREE_CODE (mtype) == FUNCTION_TYPE)
  1175.                   {
  1176.                 tree newtype;
  1177.                 newtype = build_function_type (TREE_TYPE (type),
  1178.                                    TYPE_ARG_TYPES (type));
  1179.                 newtype = build_type_variant (newtype,
  1180.                                   TYPE_READONLY (type),
  1181.                                   TYPE_VOLATILE (type));
  1182.                 type = newtype;
  1183.                 if (TREE_TYPE (type) != TREE_TYPE (mtype))
  1184.                   goto maybe_bad_return_type;
  1185.                   }
  1186.                 else if (TYPE_METHOD_BASETYPE (mtype)
  1187.                      == TYPE_METHOD_BASETYPE (type))
  1188.                   {
  1189.                 /* Types didn't match, but arg types and
  1190.                    `this' do match, so the return type is
  1191.                    all that should be messing it up.  */
  1192.                   maybe_bad_return_type:
  1193.                 if (TREE_TYPE (type) != TREE_TYPE (mtype))
  1194.                   error ("inconsistent return types for method `%s' in class `%s'",
  1195.                      IDENTIFIER_POINTER (name),
  1196.                      IDENTIFIER_POINTER (TYPE_IDENTIFIER (ctx)));
  1197.                   }
  1198.                 r = method;
  1199.                 break;
  1200.               }
  1201.             found = 1;
  1202.             continue;
  1203.               }
  1204. #if 0
  1205.             fprintf (stderr, "\tfound %s\n\n",
  1206.                  IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (method)));
  1207. #endif
  1208.             if (DECL_ARGUMENTS (method)
  1209.             && ! TREE_PERMANENT (DECL_ARGUMENTS (method)))
  1210.               /* @@ Is this early enough?  Might we want to do
  1211.              this instead while processing the expansion?     */
  1212.               DECL_ARGUMENTS (method)
  1213.             = tsubst (DECL_ARGUMENTS (t), args, nargs, t);
  1214.             r = method;
  1215.             break;
  1216.           }
  1217.         if (r == NULL_TREE && pass == 0)
  1218.           {
  1219.             pass = 1;
  1220.             method = TREE_VEC_ELT (methods, i);
  1221.             goto maybe_error;
  1222.           }
  1223.           }
  1224.         if (r == NULL_TREE)
  1225.           {
  1226.           no_match:
  1227.         error (found
  1228.                ? "template for method `%s' doesn't match any in class `%s'"
  1229.                : "method `%s' not found in class `%s'",
  1230.                IDENTIFIER_OPNAME_P (name)
  1231.                ? operator_name_string (name) : IDENTIFIER_POINTER (name),
  1232.                IDENTIFIER_POINTER (TYPE_IDENTIFIER (ctx)));
  1233.         if (in_decl)
  1234.           error_with_decl (in_decl, "in attempt to instantiate `%s' declared at this point in file");
  1235.         return error_mark_node;
  1236.           }
  1237.       }
  1238.     else
  1239.       {
  1240.         r = DECL_NAME (t);
  1241.         {
  1242.           tree decls, val;
  1243.           int got_it = 0;
  1244.  
  1245.           decls = IDENTIFIER_GLOBAL_VALUE (r);
  1246.           if (decls == NULL_TREE)
  1247.         /* no match */;
  1248.           else if (TREE_CODE (decls) == TREE_LIST)
  1249.         while (decls)
  1250.           {
  1251.             val = TREE_VALUE (decls);
  1252.             decls = TREE_CHAIN (decls);
  1253.           try_one:
  1254.             if (TREE_CODE (val) == FUNCTION_DECL
  1255.             && TREE_TYPE (val) == type)
  1256.               {
  1257.             got_it = 1;
  1258.             r = val;
  1259.             break;
  1260.               }
  1261.           }
  1262.           else
  1263.         {
  1264.           val = decls;
  1265.           decls = NULL_TREE;
  1266.           goto try_one;
  1267.         }
  1268.  
  1269.           if (!got_it)
  1270.         {
  1271.           r = build_decl_overload (r, TYPE_VALUES (type),
  1272.                        DECL_CONTEXT (t) != NULL_TREE);
  1273.           r = build_lang_decl (FUNCTION_DECL, r, type);
  1274.         }
  1275.         }
  1276.       }
  1277.     TREE_PUBLIC (r) = TREE_PUBLIC (t);
  1278.     DECL_EXTERNAL (r) = DECL_EXTERNAL (t);
  1279.     TREE_STATIC (r) = TREE_STATIC (t);
  1280.     DECL_INLINE (r) = DECL_INLINE (t);
  1281.     DECL_SOURCE_FILE (r) = DECL_SOURCE_FILE (t);
  1282.     DECL_SOURCE_LINE (r) = DECL_SOURCE_LINE (t);
  1283.     DECL_CLASS_CONTEXT (r) = tsubst (DECL_CLASS_CONTEXT (t), args, nargs, t);
  1284.     make_decl_rtl (r, NULL_PTR, 1);
  1285.     DECL_ARGUMENTS (r) = fnargs;
  1286.     DECL_RESULT (r) = result;
  1287.     if (DECL_CONTEXT (t) == NULL_TREE
  1288.         || TREE_CODE_CLASS (TREE_CODE (DECL_CONTEXT (t))) != 't')
  1289.       push_overloaded_decl_top_level (r, 0);
  1290.     return r;
  1291.       }
  1292.  
  1293.     case PARM_DECL:
  1294.       {
  1295.     tree r;
  1296.     r = build_decl (PARM_DECL, DECL_NAME (t), type);
  1297.     DECL_INITIAL (r) = TREE_TYPE (r);
  1298.     if (TREE_CHAIN (t))
  1299.       TREE_CHAIN (r) = tsubst (TREE_CHAIN (t), args, nargs, TREE_CHAIN (t));
  1300.     return r;
  1301.       }
  1302.  
  1303.     case TREE_LIST:
  1304.       {
  1305.     tree purpose, value, chain, result;
  1306.     int via_public, via_virtual, via_protected;
  1307.  
  1308.     if (t == void_list_node)
  1309.       return t;
  1310.  
  1311.     via_public = TREE_VIA_PUBLIC (t);
  1312.     via_protected = TREE_VIA_PROTECTED (t);
  1313.     via_virtual = TREE_VIA_VIRTUAL (t);
  1314.  
  1315.     purpose = TREE_PURPOSE (t);
  1316.     if (purpose)
  1317.       purpose = tsubst (purpose, args, nargs, in_decl);
  1318.     value = TREE_VALUE (t);
  1319.     if (value)
  1320.       value = tsubst (value, args, nargs, in_decl);
  1321.     chain = TREE_CHAIN (t);
  1322.     if (chain && chain != void_type_node)
  1323.       chain = tsubst (chain, args, nargs, in_decl);
  1324.     if (purpose == TREE_PURPOSE (t)
  1325.         && value == TREE_VALUE (t)
  1326.         && chain == TREE_CHAIN (t))
  1327.       return t;
  1328.     result = hash_tree_cons (via_public, via_virtual, via_protected,
  1329.                  purpose, value, chain);
  1330.     TREE_PARMLIST (result) = TREE_PARMLIST (t);
  1331.     return result;
  1332.       }
  1333.     case TREE_VEC:
  1334.       {
  1335.     int len = TREE_VEC_LENGTH (t), need_new = 0, i;
  1336.     tree *elts = (tree *) alloca (len * sizeof (tree));
  1337.     bzero (elts, len * sizeof (tree));
  1338.  
  1339.     for (i = 0; i < len; i++)
  1340.       {
  1341.         elts[i] = tsubst (TREE_VEC_ELT (t, i), args, nargs, in_decl);
  1342.         if (elts[i] != TREE_VEC_ELT (t, i))
  1343.           need_new = 1;
  1344.       }
  1345.  
  1346.     if (!need_new)
  1347.       return t;
  1348.  
  1349.     t = make_tree_vec (len);
  1350.     for (i = 0; i < len; i++)
  1351.       TREE_VEC_ELT (t, i) = elts[i];
  1352.     return t;
  1353.       }
  1354.     case POINTER_TYPE:
  1355.     case REFERENCE_TYPE:
  1356.       {
  1357.     tree r;
  1358.     enum tree_code code;
  1359.     if (type == TREE_TYPE (t))
  1360.       return t;
  1361.  
  1362.     code = TREE_CODE (t);
  1363.     if (code == POINTER_TYPE)
  1364.       r = build_pointer_type (type);
  1365.     else
  1366.       r = build_reference_type (type);
  1367.     r = build_type_variant (r, TYPE_READONLY (t), TYPE_VOLATILE (t));
  1368.     /* Will this ever be needed for TYPE_..._TO values?  */
  1369.     layout_type (r);
  1370.     return r;
  1371.       }
  1372.     case FUNCTION_TYPE:
  1373.     case METHOD_TYPE:
  1374.       {
  1375.     tree values = TYPE_VALUES (t); /* same as TYPE_ARG_TYPES */
  1376.     tree context = TYPE_CONTEXT (t);
  1377.     tree new_value;
  1378.  
  1379.     /* Don't bother recursing if we know it won't change anything.    */
  1380.     if (! (values == void_type_node
  1381.            || values == integer_type_node))
  1382.       values = tsubst (values, args, nargs, in_decl);
  1383.     if (context)
  1384.       context = tsubst (context, args, nargs, in_decl);
  1385.     /* Could also optimize cases where return value and
  1386.        values have common elements (e.g., T min(const &T, const T&).  */
  1387.  
  1388.     /* If the above parameters haven't changed, just return the type.  */
  1389.     if (type == TREE_TYPE (t)
  1390.         && values == TYPE_VALUES (t)
  1391.         && context == TYPE_CONTEXT (t))
  1392.       return t;
  1393.  
  1394.     /* Construct a new type node and return it.  */
  1395.     if (TREE_CODE (t) == FUNCTION_TYPE
  1396.         && context == NULL_TREE)
  1397.       {
  1398.         new_value = build_function_type (type, values);
  1399.       }
  1400.     else if (context == NULL_TREE)
  1401.       {
  1402.         tree base = tsubst (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t))),
  1403.                 args, nargs, in_decl);
  1404.         new_value = build_cplus_method_type (base, type,
  1405.                          TREE_CHAIN (values));
  1406.       }
  1407.     else
  1408.       {
  1409.         new_value = make_node (TREE_CODE (t));
  1410.         TREE_TYPE (new_value) = type;
  1411.         TYPE_CONTEXT (new_value) = context;
  1412.         TYPE_VALUES (new_value) = values;
  1413.         TYPE_SIZE (new_value) = TYPE_SIZE (t);
  1414.         TYPE_ALIGN (new_value) = TYPE_ALIGN (t);
  1415.         TYPE_MODE (new_value) = TYPE_MODE (t);
  1416.         if (TYPE_METHOD_BASETYPE (t))
  1417.           TYPE_METHOD_BASETYPE (new_value) = tsubst (TYPE_METHOD_BASETYPE (t),
  1418.                              args, nargs, in_decl);
  1419.         /* Need to generate hash value.  */
  1420.         my_friendly_abort (84);
  1421.       }
  1422.     new_value = build_type_variant (new_value,
  1423.                     TYPE_READONLY (t),
  1424.                     TYPE_VOLATILE (t));
  1425.     return new_value;
  1426.       }
  1427.     case ARRAY_TYPE:
  1428.       {
  1429.     tree domain = tsubst (TYPE_DOMAIN (t), args, nargs, in_decl);
  1430.     tree r;
  1431.     if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
  1432.       return t;
  1433.     r = build_cplus_array_type (type, domain);
  1434.     return r;
  1435.       }
  1436.  
  1437.     case UNINSTANTIATED_P_TYPE:
  1438.       {
  1439.     int nparms = TREE_VEC_LENGTH (DECL_TEMPLATE_PARMS (UPT_TEMPLATE (t)));
  1440.     tree argvec = make_tree_vec (nparms);
  1441.     tree parmvec = UPT_PARMS (t);
  1442.     int i;
  1443.     tree id;
  1444.     for (i = 0; i < nparms; i++)
  1445.       TREE_VEC_ELT (argvec, i) = tsubst (TREE_VEC_ELT (parmvec, i),
  1446.                          args, nargs, in_decl);
  1447.     id = lookup_template_class (DECL_NAME (UPT_TEMPLATE (t)), argvec, NULL_TREE);
  1448.     if (! IDENTIFIER_HAS_TYPE_VALUE (id)) {
  1449.       instantiate_class_template(id, 0);
  1450.       /* set up pending_classes */
  1451.       add_pending_template (id);
  1452.  
  1453.       TYPE_MAIN_VARIANT (IDENTIFIER_TYPE_VALUE (id)) =
  1454.         IDENTIFIER_TYPE_VALUE (id);
  1455.     }
  1456.     return build_type_variant (IDENTIFIER_TYPE_VALUE (id),
  1457.                    TYPE_READONLY (t),
  1458.                    TYPE_VOLATILE (t));
  1459.       }
  1460.  
  1461.     case MINUS_EXPR:
  1462.     case PLUS_EXPR:
  1463.       return fold (build (TREE_CODE (t), TREE_TYPE (t),
  1464.               tsubst (TREE_OPERAND (t, 0), args, nargs, in_decl),
  1465.               tsubst (TREE_OPERAND (t, 1), args, nargs, in_decl)));
  1466.  
  1467.     case NEGATE_EXPR:
  1468.     case NOP_EXPR:
  1469.       return fold (build1 (TREE_CODE (t), TREE_TYPE (t),
  1470.                tsubst (TREE_OPERAND (t, 0), args, nargs, in_decl)));
  1471.  
  1472.     default:
  1473.       sorry ("use of `%s' in function template",
  1474.          tree_code_name [(int) TREE_CODE (t)]);
  1475.       return error_mark_node;
  1476.     }
  1477. }
  1478.  
  1479. tree
  1480. instantiate_template (tmpl, targ_ptr)
  1481.      tree tmpl, *targ_ptr;
  1482. {
  1483.   tree targs, fndecl;
  1484.   int i, len;
  1485.   struct pending_inline *p;
  1486.   struct template_info *t;
  1487.   struct obstack *old_fmp_obstack;
  1488.   extern struct obstack *function_maybepermanent_obstack;
  1489.  
  1490.   push_obstacks (&permanent_obstack, &permanent_obstack);
  1491.   old_fmp_obstack = function_maybepermanent_obstack;
  1492.   function_maybepermanent_obstack = &permanent_obstack;
  1493.  
  1494.   my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 283);
  1495.   len = TREE_VEC_LENGTH (DECL_TEMPLATE_PARMS (tmpl));
  1496.  
  1497.   for (fndecl = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
  1498.        fndecl; fndecl = TREE_CHAIN (fndecl))
  1499.     {
  1500.       tree *t1 = &TREE_VEC_ELT (TREE_PURPOSE (fndecl), 0);
  1501.       for (i = len - 1; i >= 0; i--)
  1502.     if (t1[i] != targ_ptr[i])
  1503.       goto no_match;
  1504.  
  1505.       /* Here, we have a match.  */
  1506.       fndecl = TREE_VALUE (fndecl);
  1507.       function_maybepermanent_obstack = old_fmp_obstack;
  1508.       pop_obstacks ();
  1509.       return fndecl;
  1510.  
  1511.     no_match:
  1512.       ;
  1513.     }
  1514.  
  1515.   targs = make_tree_vec (len);
  1516.   i = len;
  1517.   while (i--)
  1518.     TREE_VEC_ELT (targs, i) = targ_ptr[i];
  1519.  
  1520.   /* substitute template parameters */
  1521.   fndecl = tsubst (DECL_RESULT (tmpl), targ_ptr,
  1522.            TREE_VEC_LENGTH (targs), tmpl);
  1523.  
  1524.   /* If it's a static member fn in the template, we need to change it
  1525.      into a FUNCTION_TYPE and chop off its this pointer.  */
  1526.   if (TREE_CODE (TREE_TYPE (DECL_RESULT (tmpl))) == METHOD_TYPE
  1527.       && fndecl != error_mark_node
  1528.       && DECL_STATIC_FUNCTION_P (fndecl))
  1529.     {
  1530.       tree olddecl = DECL_RESULT (tmpl);
  1531.       revert_static_member_fn (&TREE_TYPE (olddecl), &DECL_RESULT (tmpl),
  1532.                    &TYPE_ARG_TYPES (TREE_TYPE (olddecl)));
  1533.       /* Chop off the this pointer that grokclassfn so kindly added
  1534.      for us (it didn't know yet if the fn was static or not).  */
  1535.       DECL_ARGUMENTS (olddecl) = TREE_CHAIN (DECL_ARGUMENTS (olddecl));
  1536.       DECL_ARGUMENTS (fndecl) = TREE_CHAIN (DECL_ARGUMENTS (fndecl));
  1537.     }
  1538.      
  1539.   t = DECL_TEMPLATE_INFO (tmpl);
  1540.   if (t->text)
  1541.     {
  1542.       p = (struct pending_inline *) permalloc (sizeof (struct pending_inline));
  1543.       p->parm_vec = t->parm_vec;
  1544.       p->bindings = targs;
  1545.       p->can_free = 0;
  1546.       p->deja_vu = 0;
  1547.       p->lineno = t->lineno;
  1548.       p->filename = t->filename;
  1549.       p->buf = t->text;
  1550.       p->len = t->length;
  1551.       p->fndecl = fndecl;
  1552.       p->interface = 1;        /* unknown */
  1553.     }
  1554.   else
  1555.     p = (struct pending_inline *)0;
  1556.  
  1557.   DECL_TEMPLATE_INSTANTIATIONS (tmpl) =
  1558.     tree_cons (targs, fndecl, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
  1559.  
  1560.   function_maybepermanent_obstack = old_fmp_obstack;
  1561.   pop_obstacks ();
  1562.  
  1563.   if (fndecl == error_mark_node || p == (struct pending_inline *)0)
  1564.     {
  1565.       /* do nothing */
  1566.     }
  1567.   else if (DECL_INLINE (fndecl))
  1568.     {
  1569.       DECL_PENDING_INLINE_INFO (fndecl) = p;
  1570.       p->next = pending_inlines;
  1571.       pending_inlines = p;
  1572.     }
  1573.   else
  1574.     {
  1575.       p->next = pending_template_expansions;
  1576.       pending_template_expansions = p;
  1577.     }
  1578.   return fndecl;
  1579. }
  1580.  
  1581. void
  1582. undo_template_name_overload (id, classlevel)
  1583.      tree id;
  1584.      int classlevel;
  1585. {
  1586.   tree template;
  1587.  
  1588.   template = IDENTIFIER_TEMPLATE (id);
  1589.   if (!template)
  1590.     return;
  1591.  
  1592. #ifdef DEBUG_CP_BINDING_LEVELS
  1593.   indent_to (stderr, debug_bindings_indentation);
  1594.   fprintf (stderr, "undo_template_name_overload");
  1595.   debug_bindings_indentation += 4;
  1596. #endif
  1597.  
  1598. #if 0 /* not yet, should get fixed properly later */
  1599.   poplevel (0, 0, 0);
  1600. #endif
  1601.   if (!classlevel)
  1602.     poplevel (0, 0, 0);
  1603. #ifdef DEBUG_CP_BINDING_LEVELS
  1604.   debug_bindings_indentation -= 4;
  1605. #endif
  1606. }
  1607.  
  1608. void
  1609. overload_template_name (id, classlevel)
  1610.      tree id;
  1611.      int classlevel;
  1612. {
  1613.   tree template, t, decl;
  1614.   struct template_info *tinfo;
  1615.  
  1616.   my_friendly_assert (TREE_CODE (id) == IDENTIFIER_NODE, 284);
  1617.   template = IDENTIFIER_TEMPLATE (id);
  1618.   if (!template)
  1619.     return;
  1620.  
  1621. #ifdef DEBUG_CP_BINDING_LEVELS
  1622.   indent_to (stderr, debug_bindings_indentation);
  1623.   fprintf (stderr, "overload_template_name(%d)", classlevel);
  1624.   debug_bindings_indentation += 4;
  1625. #endif
  1626.   template = TREE_PURPOSE (template);
  1627.   tinfo = DECL_TEMPLATE_INFO (template);
  1628.   template = DECL_NAME (template);
  1629.   my_friendly_assert (template != NULL_TREE, 285);
  1630.  
  1631.   if (!classlevel)
  1632.     {
  1633.       pushlevel (1);
  1634.       declare_pseudo_global_level ();
  1635.     }
  1636.  
  1637.   t = xref_tag (tinfo->aggr, id, NULL_TREE);
  1638.   my_friendly_assert (TREE_CODE (t) == RECORD_TYPE
  1639.       || TREE_CODE (t) == UNINSTANTIATED_P_TYPE, 286);
  1640.  
  1641.   decl = build_decl (TYPE_DECL, template, t);
  1642.  
  1643. #if 0 /* fix this later */
  1644.   /* We don't want to call here if the work has already been done.  */
  1645.   t = (classlevel
  1646.        ? IDENTIFIER_CLASS_VALUE (template)
  1647.        : IDENTIFIER_LOCAL_VALUE (template));
  1648.   if (t
  1649.       && TREE_CODE (t) == TYPE_DECL
  1650.       && TREE_TYPE (t) == t)
  1651.     my_friendly_abort (85);
  1652. #endif
  1653.  
  1654.   if (classlevel)
  1655.     pushdecl_class_level (decl);
  1656.   else
  1657. #if 0 /* not yet, should get fixed properly later */
  1658.     pushdecl (decl);
  1659.   pushlevel (1);
  1660. #else
  1661.     {
  1662.       pushdecl (decl);
  1663.       /* @@ Is this necessary now?  */
  1664.       IDENTIFIER_LOCAL_VALUE (template) = decl;
  1665.     }
  1666. #endif
  1667.  
  1668. #ifdef DEBUG_CP_BINDING_LEVELS
  1669.   debug_bindings_indentation -= 4;
  1670. #endif
  1671. }
  1672.  
  1673. /* T1 is PRE_PARSED_CLASS_DECL; T3 is result of XREF_TAG lookup.  */
  1674. void
  1675. end_template_instantiation (t1, t3)
  1676.      tree t1, t3;
  1677. {
  1678.   extern struct pending_input *to_be_restored;
  1679.   tree t, decl;
  1680.  
  1681. #ifdef DEBUG_CP_BINDING_LEVELS
  1682.   indent_to (stderr, debug_bindings_indentation);
  1683.   fprintf (stderr, "end_template_instantiation");
  1684.   debug_bindings_indentation += 4;
  1685. #endif
  1686.  
  1687.   processing_template_defn--;
  1688.  
  1689.   /* Restore the old parser input state.  */
  1690.   if (yychar == YYEMPTY)
  1691.     yychar = yylex ();
  1692.   if (yychar != END_OF_SAVED_INPUT)
  1693.     error ("parse error at end of class template");
  1694.   else
  1695.     {
  1696.       restore_pending_input (to_be_restored);
  1697.       to_be_restored = 0;
  1698.     }
  1699.  
  1700.   /* Our declarations didn't get stored in the global slot, since
  1701.      there was a (supposedly tags-transparent) scope in between.  */
  1702.   t = IDENTIFIER_TYPE_VALUE (TREE_VALUE (t1));
  1703.   my_friendly_assert (t != NULL_TREE
  1704.               && TREE_CODE_CLASS (TREE_CODE (t)) == 't',
  1705.               287);
  1706.   CLASSTYPE_USE_TEMPLATE (t) = 2;
  1707.   /* Always make methods of template classes static, until we've
  1708.      got a decent scheme for handling them.  The pragmas as they
  1709.      are now are inadequate.  */
  1710.   CLASSTYPE_INTERFACE_UNKNOWN (t) = 1;
  1711.   decl = IDENTIFIER_GLOBAL_VALUE (TREE_VALUE (t1));
  1712.   my_friendly_assert (TREE_CODE (decl) == TYPE_DECL, 288);
  1713.  
  1714.   undo_template_name_overload (TREE_VALUE (t1), 0);
  1715.   t = IDENTIFIER_TEMPLATE (TREE_VALUE (t1));
  1716.   pop_template_decls (DECL_TEMPLATE_PARMS (TREE_PURPOSE (t)), TREE_VALUE (t),
  1717.               0);
  1718.   pop_from_top_level ();
  1719.  
  1720.   /* This will fix up the type-value field.  */
  1721.   pushdecl_top_level (decl);
  1722.  
  1723.   /* Restore interface/implementation settings.     */
  1724.   extract_interface_info ();
  1725.  
  1726. #ifdef DEBUG_CP_BINDING_LEVELS
  1727.   debug_bindings_indentation -= 4;
  1728. #endif
  1729. }
  1730.  
  1731. /* Store away the text of an inline template function.    No rtl is
  1732.    generated for this function until it is actually needed.  */
  1733.  
  1734. void
  1735. reinit_parse_for_template (yychar, d1, d2)
  1736.      int yychar;
  1737.      tree d1, d2;
  1738. {
  1739.   struct template_info *template_info;
  1740.  
  1741.   if (d2 == NULL_TREE || d2 == error_mark_node)
  1742.     {
  1743.     lose:
  1744.       /* @@ Should use temp obstack, and discard results.  */
  1745.       reinit_parse_for_block (yychar, &permanent_obstack, 1);
  1746.       return;
  1747.     }
  1748.  
  1749.   if (TREE_CODE (d2) == IDENTIFIER_NODE)
  1750.     d2 = IDENTIFIER_GLOBAL_VALUE (d2);
  1751.   if (!d2)
  1752.     goto lose;
  1753.   template_info = DECL_TEMPLATE_INFO (d2);
  1754.   if (!template_info)
  1755.     {
  1756.       template_info = (struct template_info *) permalloc (sizeof (struct template_info));
  1757.       bzero (template_info, sizeof (struct template_info));
  1758.       DECL_TEMPLATE_INFO (d2) = template_info;
  1759.     }
  1760.   template_info->filename = input_filename;
  1761.   template_info->lineno = lineno;
  1762.   reinit_parse_for_block (yychar, &permanent_obstack, 1);
  1763.   template_info->text = obstack_base (&permanent_obstack);
  1764.   template_info->length = obstack_object_size (&permanent_obstack);
  1765.   obstack_finish (&permanent_obstack);
  1766.   template_info->parm_vec = d1;
  1767. }
  1768.  
  1769. /* Type unification.
  1770.  
  1771.    We have a function template signature with one or more references to
  1772.    template parameters, and a parameter list we wish to fit to this
  1773.    template.  If possible, produce a list of parameters for the template
  1774.    which will cause it to fit the supplied parameter list.
  1775.  
  1776.    Return zero for success, 2 for an incomplete match that doesn't resolve
  1777.    all the types, and 1 for complete failure.  An error message will be
  1778.    printed only for an incomplete match.
  1779.  
  1780.    TPARMS[NTPARMS] is an array of template parameter types;
  1781.    TARGS[NTPARMS] is the array of template parameter values.  PARMS is
  1782.    the function template's signature (using TEMPLATE_PARM_IDX nodes),
  1783.    and ARGS is the argument list we're trying to match against it.  */
  1784.  
  1785. int
  1786. type_unification (tparms, targs, parms, args, nsubsts)
  1787.      tree tparms, *targs, parms, args;
  1788.      int *nsubsts;
  1789. {
  1790.   tree parm, arg;
  1791.   int i;
  1792.   int ntparms = TREE_VEC_LENGTH (tparms);
  1793.  
  1794.   my_friendly_assert (TREE_CODE (tparms) == TREE_VEC, 289);
  1795.   my_friendly_assert (TREE_CODE (parms) == TREE_LIST, 290);
  1796.   /* ARGS could be NULL (via a call from cp-parse.y to
  1797.      build_x_function_call).  */
  1798.   if (args)
  1799.     my_friendly_assert (TREE_CODE (args) == TREE_LIST, 291);
  1800.   my_friendly_assert (ntparms > 0, 292);
  1801.  
  1802.   bzero (targs, sizeof (tree) * ntparms);
  1803.  
  1804.   while (parms
  1805.      && parms != void_list_node
  1806.      && args)
  1807.     {
  1808.       parm = TREE_VALUE (parms);
  1809.       parms = TREE_CHAIN (parms);
  1810.       arg = TREE_VALUE (args);
  1811.       args = TREE_CHAIN (args);
  1812.  
  1813.       if (arg == error_mark_node)
  1814.     return 1;
  1815.       if (arg == unknown_type_node)
  1816.     return 1;
  1817. #if 0
  1818.       if (TREE_CODE (arg) == VAR_DECL)
  1819.     arg = TREE_TYPE (arg);
  1820.       else if (TREE_CODE_CLASS (TREE_CODE (arg)) == 'e')
  1821.     arg = TREE_TYPE (arg);
  1822. #else
  1823.       my_friendly_assert (TREE_TYPE (arg) != NULL_TREE, 293);
  1824.       arg = TREE_TYPE (arg);
  1825. #endif
  1826.  
  1827.       switch (unify (tparms, targs, ntparms, parm, arg, nsubsts))
  1828.     {
  1829.     case 0:
  1830.       break;
  1831.     case 1:
  1832.       return 1;
  1833.     }
  1834.     }
  1835.   /* Fail if we've reached the end of the parm list, and more args
  1836.      are present, and the parm list isn't variadic.  */
  1837.   if (args && parms == void_list_node)
  1838.     return 1;
  1839.   /* Fail if parms are left and they don't have default values.     */
  1840.   if (parms
  1841.       && parms != void_list_node
  1842.       && TREE_PURPOSE (parms) == NULL_TREE)
  1843.     return 1;
  1844.   for (i = 0; i < ntparms; i++)
  1845.     if (!targs[i])
  1846.       {
  1847.     error ("incomplete type unification");
  1848.     return 2;
  1849.       }
  1850.   return 0;
  1851. }
  1852.  
  1853. /* Tail recursion is your friend.  */
  1854. static int
  1855. unify (tparms, targs, ntparms, parm, arg, nsubsts)
  1856.      tree tparms, *targs, parm, arg;
  1857.      int *nsubsts;
  1858. {
  1859.   int idx;
  1860.  
  1861.   /* I don't think this will do the right thing with respect to types.
  1862.      But the only case I've seen it in so far has been array bounds, where
  1863.      signedness is the only information lost, and I think that will be
  1864.      okay.  */
  1865.   while (TREE_CODE (parm) == NOP_EXPR)
  1866.     parm = TREE_OPERAND (parm, 0);
  1867.  
  1868.   if (arg == error_mark_node)
  1869.     return 1;
  1870.   if (arg == unknown_type_node)
  1871.     return 1;
  1872.   if (arg == parm)
  1873.     return 0;
  1874.  
  1875.   switch (TREE_CODE (parm))
  1876.     {
  1877.     case TEMPLATE_TYPE_PARM:
  1878.       (*nsubsts)++;
  1879.       if (TEMPLATE_TYPE_TPARMLIST (parm) != tparms)
  1880.     {
  1881.       error ("mixed template headers?!");
  1882.       my_friendly_abort (86);
  1883.       return 1;
  1884.     }
  1885.       idx = TEMPLATE_TYPE_IDX (parm);
  1886.       /* Simple cases: Value already set, does match or doesn't.  */
  1887.       if (targs[idx] == arg)
  1888.     return 0;
  1889.       else if (targs[idx])
  1890.     return 1;
  1891.       /* Check for mixed types and values.  */
  1892.       if (TREE_CODE (TREE_VEC_ELT (tparms, idx)) != IDENTIFIER_NODE)
  1893.     return 1;
  1894.       targs[idx] = arg;
  1895.       return 0;
  1896.     case TEMPLATE_CONST_PARM:
  1897.       (*nsubsts)++;
  1898.       idx = TEMPLATE_CONST_IDX (parm);
  1899.       if (targs[idx] == arg)
  1900.     return 0;
  1901.       else if (targs[idx])
  1902.     {
  1903.       my_friendly_abort (87);
  1904.       return 1;
  1905.     }
  1906. /*    else if (typeof arg != tparms[idx])
  1907.     return 1;*/
  1908.  
  1909.       targs[idx] = copy_to_permanent (arg);
  1910.       return 0;
  1911.  
  1912.     case POINTER_TYPE:
  1913.       if (TREE_CODE (arg) != POINTER_TYPE)
  1914.     return 1;
  1915.       return unify (tparms, targs, ntparms, TREE_TYPE (parm), TREE_TYPE (arg),
  1916.             nsubsts);
  1917.  
  1918.     case REFERENCE_TYPE:
  1919.       return unify (tparms, targs, ntparms, TREE_TYPE (parm), arg, nsubsts);
  1920.  
  1921.     case ARRAY_TYPE:
  1922.       if (TREE_CODE (arg) != ARRAY_TYPE)
  1923.     return 1;
  1924.       if (unify (tparms, targs, ntparms, TYPE_DOMAIN (parm), TYPE_DOMAIN (arg),
  1925.          nsubsts) != 0)
  1926.     return 1;
  1927.       return unify (tparms, targs, ntparms, TREE_TYPE (parm), TREE_TYPE (arg),
  1928.             nsubsts);
  1929.  
  1930.     case REAL_TYPE:
  1931.     case INTEGER_TYPE:
  1932.       if (TREE_CODE (parm) == INTEGER_TYPE && TREE_CODE (arg) == INTEGER_TYPE)
  1933.     {
  1934.       if (TYPE_MIN_VALUE (parm) && TYPE_MIN_VALUE (arg)
  1935.           && unify (tparms, targs, ntparms,
  1936.             TYPE_MIN_VALUE (parm), TYPE_MIN_VALUE (arg), nsubsts))
  1937.         return 1;
  1938.       if (TYPE_MAX_VALUE (parm) && TYPE_MAX_VALUE (arg)
  1939.           && unify (tparms, targs, ntparms,
  1940.             TYPE_MAX_VALUE (parm), TYPE_MAX_VALUE (arg), nsubsts))
  1941.         return 1;
  1942.     }
  1943.       /* As far as unification is concerned, this wins.     Later checks
  1944.      will invalidate it if necessary.  */
  1945.       return 0;
  1946.  
  1947.       /* Types INTEGER_CST and MINUS_EXPR can come from array bounds.  */
  1948.     case INTEGER_CST:
  1949.       if (TREE_CODE (arg) != INTEGER_CST)
  1950.     return 1;
  1951.       return !tree_int_cst_equal (parm, arg);
  1952.  
  1953.     case MINUS_EXPR:
  1954.       {
  1955.     tree t1, t2;
  1956.     t1 = TREE_OPERAND (parm, 0);
  1957.     t2 = TREE_OPERAND (parm, 1);
  1958.     if (TREE_CODE (t1) != TEMPLATE_CONST_PARM)
  1959.       return 1;
  1960.     return unify (tparms, targs, ntparms, t1,
  1961.               fold (build (PLUS_EXPR, integer_type_node, arg, t2)),
  1962.               nsubsts);
  1963.       }
  1964.  
  1965.     case TREE_VEC:
  1966.       {
  1967.     int i;
  1968.     if (TREE_CODE (arg) != TREE_VEC)
  1969.       return 1;
  1970.     if (TREE_VEC_LENGTH (parm) != TREE_VEC_LENGTH (arg))
  1971.       return 1;
  1972.     for (i = TREE_VEC_LENGTH (parm) - 1; i >= 0; i--)
  1973.       if (unify (tparms, targs, ntparms,
  1974.              TREE_VEC_ELT (parm, i), TREE_VEC_ELT (arg, i),
  1975.              nsubsts))
  1976.         return 1;
  1977.     return 0;
  1978.       }
  1979.  
  1980.     case UNINSTANTIATED_P_TYPE:
  1981.       {
  1982.     tree a;
  1983.     /* Unification of something that is not a template fails. (mrs) */
  1984.     if (TYPE_NAME (arg) == 0)
  1985.       return 1;
  1986.     a = IDENTIFIER_TEMPLATE (TYPE_IDENTIFIER (arg));
  1987.     /* Unification of something that is not a template fails. (mrs) */
  1988.     if (a == 0)
  1989.       return 1;
  1990.     if (UPT_TEMPLATE (parm) != TREE_PURPOSE (a))
  1991.       /* different templates */
  1992.       return 1;
  1993.     return unify (tparms, targs, ntparms, UPT_PARMS (parm), TREE_VALUE (a),
  1994.               nsubsts);
  1995.       }
  1996.  
  1997.     case RECORD_TYPE:
  1998.       /* Unification of something that is not a template fails. (mrs) */
  1999.       return 1;
  2000.  
  2001.     default:
  2002.       sorry ("use of `%s' in template type unification",
  2003.          tree_code_name [(int) TREE_CODE (parm)]);
  2004.       return 1;
  2005.     }
  2006. }
  2007.  
  2008.  
  2009. #undef DEBUG
  2010.  
  2011. int
  2012. do_pending_expansions ()
  2013. {
  2014.   struct pending_inline *i, *new_list = 0;
  2015.  
  2016.   if (!pending_template_expansions)
  2017.     return 0;
  2018.  
  2019. #ifdef DEBUG
  2020.   fprintf (stderr, "\n\n\t\t IN DO_PENDING_EXPANSIONS\n\n");
  2021. #endif
  2022.  
  2023.   i = pending_template_expansions;
  2024.   while (i)
  2025.     {
  2026.       tree context;
  2027.  
  2028.       struct pending_inline *next = i->next;
  2029.       tree t = i->fndecl;
  2030.  
  2031.       int decision = 0;
  2032. #define DECIDE(N) if(1){decision=(N); goto decided;}else
  2033.  
  2034.       my_friendly_assert (TREE_CODE (t) == FUNCTION_DECL
  2035.               || TREE_CODE (t) == VAR_DECL, 294);
  2036.       if (TREE_ASM_WRITTEN (t))
  2037.     DECIDE (0);
  2038.       /* If it's a method, let the class type decide it.
  2039.      @@ What if the method template is in a separate file?
  2040.      Maybe both file contexts should be taken into account?  */
  2041.       context = DECL_CONTEXT (t);
  2042.       if (context != NULL_TREE
  2043.       && TREE_CODE_CLASS (TREE_CODE (context)) == 't')
  2044.     {
  2045.       /* If `unknown', we might want a static copy.
  2046.          If `implementation', we want a global one.
  2047.          If `interface', ext ref.  */
  2048.       if (!CLASSTYPE_INTERFACE_UNKNOWN (context))
  2049.         DECIDE (!CLASSTYPE_INTERFACE_ONLY (context));
  2050. #if 0 /* This doesn't get us stuff needed only by the file initializer.  */
  2051.       DECIDE (TREE_USED (t));
  2052. #else /* This compiles too much stuff, but that's probably better in
  2053.      most cases than never compiling the stuff we need.  */
  2054.       DECIDE (1);
  2055. #endif
  2056.     }
  2057.       /* else maybe call extract_interface_info? */
  2058.       if (TREE_USED (t)) /* is this right? */
  2059.     DECIDE (1);
  2060.  
  2061.     decided:
  2062. #ifdef DEBUG
  2063.       print_node_brief (stderr, decision ? "yes: " : "no: ", t, 0);
  2064.       fprintf (stderr, "\t%s\n",
  2065.            (DECL_ASSEMBLER_NAME (t)
  2066.         ? IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (t))
  2067.         : ""));
  2068. #endif
  2069.       if (decision == 1)
  2070.     {
  2071.       i->next = pending_inlines;
  2072.       pending_inlines = i;
  2073.     }
  2074.       else
  2075.     {
  2076.       i->next = new_list;
  2077.       new_list = i;
  2078.     }
  2079.       i = next;
  2080.     }
  2081.   pending_template_expansions = new_list;
  2082.   if (!pending_inlines)
  2083.     return 0;
  2084.   do_pending_inlines ();
  2085.   return 1;
  2086. }
  2087.  
  2088.  
  2089. struct pending_template {
  2090.   struct pending_template *next;
  2091.   tree id;
  2092. };
  2093.  
  2094. static struct pending_template* pending_templates;
  2095.  
  2096. void
  2097. do_pending_templates ()
  2098. {
  2099.   struct pending_template* t;
  2100.   
  2101.   for ( t = pending_templates; t; t = t->next)
  2102.     {
  2103.       instantiate_class_template (t->id, 1);
  2104.     }
  2105.  
  2106.   for ( t = pending_templates; t; t = pending_templates)
  2107.     {
  2108.       pending_templates = t->next;
  2109.       free(t);
  2110.     }
  2111. }
  2112.  
  2113. static void
  2114. add_pending_template (pt)
  2115.      tree pt;
  2116. {
  2117.   struct pending_template *p;
  2118.   
  2119.   p = (struct pending_template *) malloc (sizeof (struct pending_template));
  2120.   p->next = pending_templates;
  2121.   pending_templates = p;
  2122.   p->id = pt;
  2123. }
  2124.