home *** CD-ROM | disk | FTP | other *** search
/ NeXTSTEP 3.2 (Developer) / NS_dev_3.2.iso / NextDeveloper / Source / GNU / cc / cc / cp-tree.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-06-09  |  45.7 KB  |  1,715 lines

  1. /* Language-dependent node constructors for parse phase of GNU compiler.
  2.    Copyright (C) 1987, 1988, 1992 Free Software Foundation, Inc.
  3.    Hacked by Michael Tiemann (tiemann@cygnus.com)
  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. #include "config.h"
  22. #include <stdio.h>
  23. #include "obstack.h"
  24. #include "tree.h"
  25. #include "cp-tree.h"
  26. #include "flags.h"
  27. #ifdef NeXT
  28. #include "next-aa.h"
  29. #else
  30. #include "assert.h"
  31. #endif
  32.  
  33. #define CEIL(x,y) (((x) + (y) - 1) / (y))
  34.  
  35. /* Return nonzero if REF is an lvalue valid for this language.
  36.    Lvalues can be assigned, unless they have TREE_READONLY.
  37.    Lvalues can have their address taken, unless they have TREE_REGDECL.  */
  38.  
  39. int
  40. lvalue_p (ref)
  41.      tree ref;
  42. {
  43.   register enum tree_code code = TREE_CODE (ref);
  44.  
  45.   if (language_lvalue_valid (ref))
  46.     switch (code)
  47.       {
  48.     /* preincrements and predecrements are valid lvals, provided
  49.        what they refer to are valid lvals. */
  50.       case PREINCREMENT_EXPR:
  51.       case PREDECREMENT_EXPR:
  52.       case COMPONENT_REF:
  53.     return lvalue_p (TREE_OPERAND (ref, 0));
  54.  
  55.       case STRING_CST:
  56.     return 1;
  57.  
  58.       case VAR_DECL:
  59.     if (TREE_READONLY (ref) && ! TREE_STATIC (ref)
  60.         && DECL_LANG_SPECIFIC (ref)
  61.         && DECL_IN_AGGR_P (ref))
  62.       return 0;
  63.       case INDIRECT_REF:
  64.       case ARRAY_REF:
  65.       case PARM_DECL:
  66.       case RESULT_DECL:
  67.       case ERROR_MARK:
  68.     if (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
  69.         && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE)
  70.       return 1;
  71.     break;
  72.  
  73.       case TARGET_EXPR:
  74.       case WITH_CLEANUP_EXPR:
  75.     return 1;
  76.  
  77.       case CALL_EXPR:
  78.     if (TREE_CODE (TREE_TYPE (ref)) == REFERENCE_TYPE
  79.         /* unary_complex_lvalue knows how to deal with this case.  */
  80.         || TREE_ADDRESSABLE (TREE_TYPE (ref)))
  81.       return 1;
  82.     break;
  83.  
  84.     /* A currently unresolved scope ref.  */
  85.       case SCOPE_REF:
  86.     my_friendly_abort (103);
  87.       case OFFSET_REF:
  88.     if (TREE_CODE (TREE_OPERAND (ref, 1)) == FUNCTION_DECL)
  89.       return 1;
  90.     if (TREE_CODE (TREE_OPERAND (ref, 1)) == VAR_DECL)
  91.       if (TREE_READONLY (ref) && ! TREE_STATIC (ref)
  92.           && DECL_LANG_SPECIFIC (ref)
  93.           && DECL_IN_AGGR_P (ref))
  94.         return 0;
  95.       else
  96.         return 1;
  97.     break;
  98.       }
  99.   return 0;
  100. }
  101.  
  102. /* Return nonzero if REF is an lvalue valid for this language;
  103.    otherwise, print an error message and return zero.  */
  104.  
  105. int
  106. lvalue_or_else (ref, string)
  107.      tree ref;
  108.      char *string;
  109. {
  110.   int win = lvalue_p (ref);
  111.   if (! win)
  112.     error ("invalid lvalue in %s", string);
  113.   return win;
  114. }
  115.  
  116. /* INIT is a CALL_EXPR which needs info about its target.
  117.    TYPE is the type that this initialization should appear to have.
  118.  
  119.    Build an encapsulation of the initialization to perform
  120.    and return it so that it can be processed by language-independent
  121.    and language-specific expression expanders.
  122.  
  123.    If WITH_CLEANUP_P is nonzero, we build a cleanup for this expression.
  124.    Otherwise, cleanups are not built here.  For example, when building
  125.    an initialization for a stack slot, since the called function handles
  126.    the cleanup, we would not want to do it here.  */
  127. tree
  128. build_cplus_new (type, init, with_cleanup_p)
  129.      tree type;
  130.      tree init;
  131.      int with_cleanup_p;
  132. {
  133.   tree slot = build (VAR_DECL, type);
  134.   tree rval = build (NEW_EXPR, type,
  135.              TREE_OPERAND (init, 0), TREE_OPERAND (init, 1), slot);
  136.   TREE_SIDE_EFFECTS (rval) = 1;
  137.   TREE_ADDRESSABLE (rval) = 1;
  138.   rval = build (TARGET_EXPR, type, slot, rval, 0);
  139.   TREE_SIDE_EFFECTS (rval) = 1;
  140.   TREE_ADDRESSABLE (rval) = 1;
  141.  
  142.   if (with_cleanup_p && TYPE_NEEDS_DESTRUCTOR (type))
  143.     {
  144.       rval = build (WITH_CLEANUP_EXPR, type, rval, 0,
  145.             build_delete (TYPE_POINTER_TO (type),
  146.                   build_unary_op (ADDR_EXPR, slot, 0),
  147.                   integer_two_node,
  148.                   LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 0, 0));
  149.       TREE_SIDE_EFFECTS (rval) = 1;
  150.     }
  151.   return rval;
  152. }
  153.  
  154. /* Recursively search EXP for CALL_EXPRs that need cleanups and replace
  155.    these CALL_EXPRs with tree nodes that will perform the cleanups.  */
  156.  
  157. tree
  158. break_out_cleanups (exp)
  159.      tree exp;
  160. {
  161.   tree tmp = exp;
  162.  
  163.   if (TREE_CODE (tmp) == CALL_EXPR
  164.       && TYPE_NEEDS_DESTRUCTOR (TREE_TYPE (tmp)))
  165.     return build_cplus_new (TREE_TYPE (tmp), tmp, 1);
  166.  
  167.   while (TREE_CODE (tmp) == NOP_EXPR
  168.      || TREE_CODE (tmp) == CONVERT_EXPR
  169.      || TREE_CODE (tmp) == NON_LVALUE_EXPR)
  170.     {
  171.       if (TREE_CODE (TREE_OPERAND (tmp, 0)) == CALL_EXPR
  172.       && TYPE_NEEDS_DESTRUCTOR (TREE_TYPE (TREE_OPERAND (tmp, 0))))
  173.     {
  174.       TREE_OPERAND (tmp, 0)
  175.         = build_cplus_new (TREE_TYPE (TREE_OPERAND (tmp, 0)),
  176.                    TREE_OPERAND (tmp, 0), 1);
  177.       break;
  178.     }
  179.       else
  180.     tmp = TREE_OPERAND (tmp, 0);
  181.     }
  182.   return exp;
  183. }
  184.  
  185. /* Recursively perform a preorder search EXP for CALL_EXPRs, making
  186.    copies where they are found.  Retuns a deep copy all nodes transitively
  187.    containing CALL_EXPRs.  */
  188.  
  189. tree
  190. break_out_calls (exp)
  191.      tree exp;
  192. {
  193.   register tree t1, t2;
  194.   register enum tree_code code;
  195.   register int changed = 0;
  196.   register int i;
  197.  
  198.   if (exp == NULL_TREE)
  199.     return exp;
  200.  
  201.   code = TREE_CODE (exp);
  202.  
  203.   if (code == CALL_EXPR)
  204.     return copy_node (exp);
  205.  
  206.   switch (TREE_CODE_CLASS (code))
  207.     {
  208.     case 'x':  /* something random, like an identifier.  */
  209.     case 't':  /* a type node */
  210.     default:
  211.       abort ();
  212.  
  213.     case 'c':  /* a constant */
  214.     case 'd':  /* A decl node */
  215.       return exp;
  216.  
  217.     case 'e':  /* a expression */
  218.     case 's':  /* an expression with side effects */
  219.       for (i = tree_code_length[(int) code]; i >= 0; i--)
  220.     {
  221.       t1 = break_out_calls (TREE_OPERAND (exp, i));
  222.       if (t1 != TREE_OPERAND (exp, i))
  223.         {
  224.           if (changed == 0)
  225.         exp = copy_node (exp);
  226.           TREE_OPERAND (exp, i) = t1;
  227.         }
  228.     }
  229.       return exp;
  230.  
  231.     case '<':  /* a comparison expression */
  232.     case '2':  /* a binary arithmetic expression */
  233.       t2 = break_out_calls (TREE_OPERAND (exp, 1));
  234.       if (t2 != TREE_OPERAND (exp, 1))
  235.     changed = 1;
  236.     case 'r':  /* a reference */
  237.     case '1':  /* a unary arithmetic expression */
  238.       t1 = break_out_calls (TREE_OPERAND (exp, 0));
  239.       if (t1 != TREE_OPERAND (exp, 0))
  240.     changed = 1;
  241.       if (changed)
  242.     {
  243.       if (tree_code_length[(int) code] == 1)
  244.         return build1 (code, TREE_TYPE (exp), t1);
  245.       else
  246.         return build (code, TREE_TYPE (exp), t1, t2);
  247.     }
  248.       return exp;
  249.     }
  250.  
  251. }
  252.  
  253. extern struct obstack *current_obstack;
  254. extern struct obstack permanent_obstack, class_obstack;
  255. extern struct obstack *saveable_obstack;
  256.  
  257. /* Here is how primitive or already-canonicalized types' hash
  258.    codes are made.  MUST BE CONSISTENT WITH tree.c !!! */
  259. #define TYPE_HASH(TYPE) ((int) (TYPE) & 0777777)
  260.  
  261. /* Construct, lay out and return the type of methods belonging to class
  262.    BASETYPE and whose arguments and values are described by TYPE.
  263.    If that type exists already, reuse it.
  264.    TYPE must be a FUNCTION_TYPE node.  */
  265.  
  266. tree
  267. build_cplus_method_type (basetype, rettype, argtypes)
  268.      tree basetype, rettype, argtypes;
  269. {
  270.   register tree t;
  271.   tree ptype = build_pointer_type (basetype);
  272.   int hashcode;
  273.  
  274.   /* Make a node of the sort we want.  */
  275.   t = make_node (METHOD_TYPE);
  276.  
  277.   TYPE_METHOD_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
  278.   TREE_TYPE (t) = rettype;
  279. #if 0
  280.   /* it is wrong to flag the object the pointer points to as readonly
  281.      when flag_this_is_variable is 0. */
  282.   ptype = build_type_variant (ptype, flag_this_is_variable <= 0, 0);
  283. #else
  284.   ptype = build_type_variant (ptype, 0, 0);
  285. #endif
  286.   /* The actual arglist for this function includes a "hidden" argument
  287.      which is "this".  Put it into the list of argument types.  */
  288.  
  289.   TYPE_ARG_TYPES (t) = tree_cons (NULL, ptype, argtypes);
  290.  
  291.   /* If we already have such a type, use the old one and free this one.
  292.      Note that it also frees up the above cons cell if found.  */
  293.   hashcode = TYPE_HASH (basetype) + TYPE_HASH (rettype) + type_hash_list (argtypes);
  294.   t = type_hash_canon (hashcode, t);
  295.  
  296.   if (TYPE_SIZE (t) == 0)
  297.     layout_type (t);
  298.  
  299.   return t;
  300. }
  301.  
  302. tree
  303. build_cplus_staticfn_type (basetype, rettype, argtypes)
  304.      tree basetype, rettype, argtypes;
  305. {
  306.   register tree t;
  307.   tree ptype = build_pointer_type (basetype);
  308.   int hashcode;
  309.  
  310.   /* Make a node of the sort we want.  */
  311.   t = make_node (FUNCTION_TYPE);
  312.  
  313.   TYPE_METHOD_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
  314.   TREE_TYPE (t) = rettype;
  315.  
  316.   /* The actual arglist for this function includes a "hidden" argument
  317.      which is "this".  Put it into the list of argument types.  */
  318.  
  319.   TYPE_ARG_TYPES (t) = argtypes;
  320.  
  321.   /* If we already have such a type, use the old one and free this one.
  322.      Note that it also frees up the above cons cell if found.  */
  323.   hashcode = TYPE_HASH (basetype) + TYPE_HASH (rettype) + type_hash_list (argtypes);
  324.   t = type_hash_canon (hashcode, t);
  325.  
  326.   if (TYPE_SIZE (t) == 0)
  327.     layout_type (t);
  328.  
  329.   return t;
  330. }
  331.  
  332. tree
  333. build_cplus_array_type (elt_type, index_type)
  334.      tree elt_type;
  335.      tree index_type;
  336. {
  337.   register struct obstack *ambient_obstack = current_obstack;
  338.   register struct obstack *ambient_saveable_obstack = saveable_obstack;
  339.   tree t;
  340.  
  341.   /* We need a new one.  If both ELT_TYPE and INDEX_TYPE are permanent,
  342.      make this permanent too.  */
  343.   if (TREE_PERMANENT (elt_type)
  344.       && (index_type == 0 || TREE_PERMANENT (index_type)))
  345.     {
  346.       current_obstack = &permanent_obstack;
  347.       saveable_obstack = &permanent_obstack;
  348.     }
  349.  
  350.   t = build_array_type (elt_type, index_type);
  351.  
  352.   /* Push these needs up so that initialization takes place
  353.      more easily.  */
  354.   TYPE_NEEDS_CONSTRUCTING (t) = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (elt_type));
  355.   TYPE_NEEDS_DESTRUCTOR (t) = TYPE_NEEDS_DESTRUCTOR (TYPE_MAIN_VARIANT (elt_type));
  356.   current_obstack = ambient_obstack;
  357.   saveable_obstack = ambient_saveable_obstack;
  358.   return t;
  359. }
  360.  
  361. /* Add OFFSET to all child types of T.
  362.  
  363.    OFFSET, which is a type offset, is number of bytes.
  364.  
  365.    Note that we don't have to worry about having two paths to the
  366.    same base type, since this type owns its association list.  */
  367. void
  368. propagate_binfo_offsets (binfo, offset)
  369.      tree binfo;
  370.      tree offset;
  371. {
  372.   tree t = BINFO_TYPE (binfo);
  373.   tree binfos = BINFO_BASETYPES (binfo);
  374.   int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
  375.  
  376.   for (i = 0; i < n_baselinks; /* note increment is done in the loop.  */)
  377.     {
  378.       tree child = TREE_VEC_ELT (binfos, i);
  379.  
  380.       if (TREE_VIA_VIRTUAL (child))
  381.     i += 1;
  382.       else
  383.     {
  384.       int j;
  385.       tree child_binfos = BINFO_BASETYPES (child);
  386.       tree basetype = BINFO_TYPE (child);
  387.       tree delta;
  388.  
  389.       for (j = i+1; j < n_baselinks; j++)
  390.         if (! TREE_VIA_VIRTUAL (TREE_VEC_ELT (binfos, j)))
  391.           {
  392.         /* The next basetype offset must take into account the space
  393.            between the classes, not just the size of each class.  */
  394.         delta = size_binop (MINUS_EXPR,
  395.                     BINFO_OFFSET (TREE_VEC_ELT (binfos, j)),
  396.                     BINFO_OFFSET (child));
  397.         break;
  398.           }
  399.  
  400. #if 0
  401.       if (BINFO_OFFSET_ZEROP (child))
  402.         BINFO_OFFSET (child) = offset;
  403.       else
  404.         BINFO_OFFSET (child)
  405.           = size_binop (PLUS_EXPR, BINFO_OFFSET (child), offset);
  406. #else
  407.       BINFO_OFFSET (child) = offset;
  408. #endif
  409.       if (child_binfos)
  410.         {
  411.           int k;
  412.           tree chain = NULL_TREE;
  413.  
  414.           /* Now unshare the structure beneath CHILD.  */
  415.           for (k = TREE_VEC_LENGTH (child_binfos)-1;
  416.            k >= 0; k--)
  417.         {
  418.           tree child_child = TREE_VEC_ELT (child_binfos, k);
  419.           if (! TREE_VIA_VIRTUAL (child_child))
  420.             TREE_VEC_ELT (child_binfos, k)
  421.               = make_binfo (BINFO_OFFSET (child_child),
  422.                     BINFO_TYPE (child_child),
  423.                     BINFO_VTABLE (child_child),
  424.                     BINFO_VIRTUALS (child_child),
  425.                     chain);
  426.           chain = TREE_VEC_ELT (child_binfos, k);
  427.           TREE_VIA_PUBLIC (chain) = TREE_VIA_PUBLIC (child_child);
  428.         }
  429.           /* Now propagate the offset to the children.  */
  430.           propagate_binfo_offsets (child, offset);
  431.         }
  432.  
  433.       /* Go to our next class that counts for offset propagation.  */
  434.       i = j;
  435.       if (i < n_baselinks)
  436.         offset = size_binop (PLUS_EXPR, offset, delta);
  437.     }
  438.     }
  439. }
  440.  
  441. /* Compute the actual offsets that our virtual base classes
  442.    will have *for this type*.  This must be performed after
  443.    the fields are laid out, since virtual baseclasses must
  444.    lay down at the end of the record.
  445.  
  446.    Returns the maximum number of virtual functions any of the virtual
  447.    baseclasses provide.  */
  448. int
  449. layout_vbasetypes (rec, max)
  450.      tree rec;
  451.      int max;
  452. {
  453.   /* Get all the virtual base types that this type uses.
  454.      The TREE_VALUE slot holds the virtual baseclass type.  */
  455.   tree vbase_types = get_vbase_types (rec);
  456.  
  457. #ifdef STRUCTURE_SIZE_BOUNDARY
  458.   unsigned record_align = MAX (STRUCTURE_SIZE_BOUNDARY, TYPE_ALIGN (rec));
  459. #else
  460.   unsigned record_align = MAX (BITS_PER_UNIT, TYPE_ALIGN (rec));
  461. #endif
  462.  
  463.   /* Record size so far is CONST_SIZE + VAR_SIZE bits,
  464.      where CONST_SIZE is an integer
  465.      and VAR_SIZE is a tree expression.
  466.      If VAR_SIZE is null, the size is just CONST_SIZE.
  467.      Naturally we try to avoid using VAR_SIZE.  */
  468.   register unsigned const_size = 0;
  469.   register tree var_size = 0;
  470.   int nonvirtual_const_size;
  471.   tree nonvirtual_var_size;
  472.  
  473.   CLASSTYPE_VBASECLASSES (rec) = vbase_types;
  474.  
  475.   if (TREE_CODE (TYPE_SIZE (rec)) == INTEGER_CST)
  476.     const_size = TREE_INT_CST_LOW (TYPE_SIZE (rec));
  477.   else
  478.     var_size = TYPE_SIZE (rec);
  479.  
  480.   nonvirtual_const_size = const_size;
  481.   nonvirtual_var_size = var_size;
  482.  
  483.   while (vbase_types)
  484.     {
  485.       tree basetype = BINFO_TYPE (vbase_types);
  486.       tree offset;
  487.  
  488.       if (const_size == 0)
  489.     offset = integer_zero_node;
  490.       else
  491.     offset = size_int ((const_size + BITS_PER_UNIT - 1) / BITS_PER_UNIT);
  492.  
  493.       if (CLASSTYPE_VSIZE (basetype) > max)
  494.     max = CLASSTYPE_VSIZE (basetype);
  495.       BINFO_OFFSET (vbase_types) = offset;
  496.  
  497.       if (TREE_CODE (TYPE_SIZE (basetype)) == INTEGER_CST)
  498.     const_size += MAX (record_align,
  499.                TREE_INT_CST_LOW (TYPE_SIZE (basetype))
  500.                - TREE_INT_CST_LOW (CLASSTYPE_VBASE_SIZE (basetype)));
  501.       else if (var_size == 0)
  502.     var_size = TYPE_SIZE (basetype);
  503.       else
  504.     var_size = size_binop (PLUS_EXPR, var_size, TYPE_SIZE (basetype));
  505.  
  506.       vbase_types = TREE_CHAIN (vbase_types);
  507.     }
  508.  
  509.   if (const_size != nonvirtual_const_size)
  510.     {
  511.       CLASSTYPE_VBASE_SIZE (rec)
  512.     = size_int (const_size - nonvirtual_const_size);
  513.       TYPE_SIZE (rec) = size_int (const_size);
  514.     }
  515.  
  516.   /* Now propagate offset information throughout the lattice
  517.      under the vbase type.  */
  518.   for (vbase_types = CLASSTYPE_VBASECLASSES (rec); vbase_types;
  519.        vbase_types = TREE_CHAIN (vbase_types))
  520.     {
  521.       tree child_binfos = BINFO_BASETYPES (vbase_types);
  522.  
  523.       if (child_binfos)
  524.     {
  525.       tree chain = NULL_TREE;
  526.       int j;
  527.       /* Now unshare the structure beneath CHILD.  */
  528.  
  529.       for (j = TREE_VEC_LENGTH (child_binfos)-1;
  530.            j >= 0; j--)
  531.         {
  532.           tree child_child = TREE_VEC_ELT (child_binfos, j);
  533.           if (! TREE_VIA_VIRTUAL (child_child))
  534.         TREE_VEC_ELT (child_binfos, j)
  535.           = make_binfo (BINFO_OFFSET (child_child),
  536.                 BINFO_TYPE (child_child),
  537.                 BINFO_VTABLE (child_child),
  538.                 BINFO_VIRTUALS (child_child),
  539.                 chain);
  540.           chain = TREE_VEC_ELT (child_binfos, j);
  541.           TREE_VIA_PUBLIC (chain) = TREE_VIA_PUBLIC (child_child);
  542.         }
  543.  
  544.       propagate_binfo_offsets (vbase_types, BINFO_OFFSET (vbase_types));
  545.     }
  546.     }
  547.  
  548.   return max;
  549. }
  550.  
  551. /* Lay out the base types of a record type, REC.
  552.    Tentatively set the size and alignment of REC
  553.    according to the base types alone.
  554.  
  555.    Offsets for immediate nonvirtual baseclasses are also computed here.
  556.  
  557.    Returns list of virtual base classes in a FIELD_DECL chain.  */
  558. tree
  559. layout_basetypes (rec, binfos)
  560.      tree rec, binfos;
  561. {
  562.   /* Chain to hold all the new FIELD_DECLs which point at virtual
  563.      base classes.  */
  564.   tree vbase_decls = NULL_TREE;
  565.  
  566. #ifdef STRUCTURE_SIZE_BOUNDARY
  567.   int record_align = MAX (STRUCTURE_SIZE_BOUNDARY, TYPE_ALIGN (rec));
  568. #else
  569.   int record_align = MAX (BITS_PER_UNIT, TYPE_ALIGN (rec));
  570. #endif
  571.  
  572.   /* Record size so far is CONST_SIZE + VAR_SIZE bits,
  573.      where CONST_SIZE is an integer
  574.      and VAR_SIZE is a tree expression.
  575.      If VAR_SIZE is null, the size is just CONST_SIZE.
  576.      Naturally we try to avoid using VAR_SIZE.  */
  577.   register int const_size = 0;
  578.   register tree var_size = 0;
  579.   int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
  580.  
  581.   /* Handle basetypes almost like fields, but record their
  582.      offsets differently.  */
  583.  
  584.   for (i = 0; i < n_baseclasses; i++)
  585.     {
  586.       int inc, desired_align, int_vbase_size;
  587.       register tree child = TREE_VEC_ELT (binfos, i);
  588.       register tree basetype = BINFO_TYPE (child);
  589.       tree decl, offset;
  590.  
  591.       if (TYPE_SIZE (basetype) == 0)
  592.     {
  593.       error_with_aggr_type (child, "base class `%s' has incomplete type");
  594.       TREE_VIA_PUBLIC (child) = 1;
  595.       TREE_VIA_VIRTUAL (child) = 0;
  596.       continue;
  597.     }
  598.  
  599.       /* All basetypes are recorded in the association list of the
  600.      derived type.  */
  601.  
  602.       if (TREE_VIA_VIRTUAL (child))
  603.     {
  604.       tree binfo;
  605.       int j;
  606.       char *name = (char *)alloca (TYPE_NAME_LENGTH (basetype)
  607.                        + sizeof (VBASE_NAME) + 1);
  608.  
  609.       /* The offset for a virtual base class is only used in computing
  610.          virtual function tables and for initializing virtual base
  611.          pointers.  It is built once `get_vbase_types' is called.  */
  612.  
  613.       /* If this basetype can come from another vbase pointer
  614.          without an additional indirection, we will share
  615.          that pointer.  If an indirection is involved, we
  616.          make our own pointer.  */
  617.       for (j = 0; j < n_baseclasses; j++)
  618.         {
  619.           tree other_child = TREE_VEC_ELT (binfos, j);
  620.           if (! TREE_VIA_VIRTUAL (other_child)
  621.           && binfo_member (basetype,
  622.                    CLASSTYPE_VBASECLASSES (BINFO_TYPE (other_child))))
  623.         goto got_it;
  624.         }
  625.       sprintf (name, VBASE_NAME_FORMAT, TYPE_NAME_STRING (basetype));
  626.       decl = build_lang_decl (FIELD_DECL, get_identifier (name),
  627.                   build_pointer_type (basetype));
  628.       DECL_ASSEMBLER_NAME (decl) = get_identifier (VTABLE_BASE);
  629.       DECL_VIRTUAL_P (decl) = 1;
  630.       DECL_FIELD_CONTEXT (decl) = rec;
  631.       DECL_CLASS_CONTEXT (decl) = rec;
  632.       DECL_FCONTEXT (decl) = basetype;
  633.       TREE_CHAIN (decl) = vbase_decls;
  634.       vbase_decls = decl;
  635.  
  636.       if (TYPE_HAS_DESTRUCTOR (basetype)
  637.           && DECL_VINDEX (TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (basetype), 0)) == NULL_TREE)
  638.         {
  639.           warning_with_decl (TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (basetype), 0),
  640.                  "destructor `%s' non-virtual");
  641.           warning ("in inheritance relationship `%s: virtual %s'",
  642.                TYPE_NAME_STRING (rec),
  643.                TYPE_NAME_STRING (basetype));
  644.         }
  645.     got_it:
  646.       /* The space this decl occupies has already been accounted for.  */
  647.       continue;
  648.     }
  649.  
  650.       if (const_size == 0)
  651.     offset = integer_zero_node;
  652.       else
  653.     {
  654.       /* Give each base type the alignment it wants.  */
  655.       const_size = CEIL (const_size, TYPE_ALIGN (basetype))
  656.         * TYPE_ALIGN (basetype);
  657.       offset = size_int ((const_size + BITS_PER_UNIT - 1) / BITS_PER_UNIT);
  658.  
  659.       if (TYPE_HAS_DESTRUCTOR (basetype)
  660.           && DECL_VINDEX (TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (basetype), 0)) == NULL_TREE)
  661.         {
  662.           warning_with_decl (TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (basetype), 0),
  663.                  "destructor `%s' non-virtual");
  664.           warning ("in inheritance relationship `%s:%s %s'",
  665.                TYPE_NAME_STRING (rec),
  666.                TREE_VIA_VIRTUAL (child) ? " virtual" : "",
  667.                TYPE_NAME_STRING (basetype));
  668.         }
  669.     }
  670.       BINFO_OFFSET (child) = offset;
  671.       if (CLASSTYPE_VSIZE (basetype))
  672.     {
  673.       BINFO_VTABLE (child) = TYPE_BINFO_VTABLE (basetype);
  674.       BINFO_VIRTUALS (child) = TYPE_BINFO_VIRTUALS (basetype);
  675.     }
  676.       TREE_CHAIN (child) = TYPE_BINFO (rec);
  677.       TYPE_BINFO (rec) = child;
  678.  
  679.       /* Add only the amount of storage not present in
  680.      the virtual baseclasses.  */
  681.  
  682.       int_vbase_size = TREE_INT_CST_LOW (CLASSTYPE_VBASE_SIZE (basetype));
  683.       if (TREE_INT_CST_LOW (TYPE_SIZE (basetype)) > int_vbase_size)
  684.     {
  685.       inc = MAX (record_align,
  686.              (TREE_INT_CST_LOW (TYPE_SIZE (basetype))
  687.               - int_vbase_size));
  688.  
  689.       /* Record must have at least as much alignment as any field.  */
  690.       desired_align = TYPE_ALIGN (basetype);
  691.       record_align = MAX (record_align, desired_align);
  692.  
  693.       const_size += inc;
  694.     }
  695.     }
  696.  
  697.   if (const_size)
  698.     CLASSTYPE_SIZE (rec) = size_int (const_size);
  699.   else
  700.     CLASSTYPE_SIZE (rec) = integer_zero_node;
  701.   CLASSTYPE_ALIGN (rec) = record_align;
  702.  
  703.   return vbase_decls;
  704. }
  705.  
  706. /* Hashing of lists so that we don't make duplicates.
  707.    The entry point is `list_hash_canon'.  */
  708.  
  709. /* Each hash table slot is a bucket containing a chain
  710.    of these structures.  */
  711.  
  712. struct list_hash
  713. {
  714.   struct list_hash *next;    /* Next structure in the bucket.  */
  715.   int hashcode;            /* Hash code of this list.  */
  716.   tree list;            /* The list recorded here.  */
  717. };
  718.  
  719. /* Now here is the hash table.  When recording a list, it is added
  720.    to the slot whose index is the hash code mod the table size.
  721.    Note that the hash table is used for several kinds of lists.
  722.    While all these live in the same table, they are completely independent,
  723.    and the hash code is computed differently for each of these.  */
  724.  
  725. #define TYPE_HASH_SIZE 59
  726. struct list_hash *list_hash_table[TYPE_HASH_SIZE];
  727.  
  728. /* Compute a hash code for a list (chain of TREE_LIST nodes
  729.    with goodies in the TREE_PURPOSE, TREE_VALUE, and bits of the
  730.    TREE_COMMON slots), by adding the hash codes of the individual entries.  */
  731.  
  732. int
  733. list_hash (list)
  734.      tree list;
  735. {
  736.   register int hashcode = 0;
  737.  
  738.   if (TREE_CHAIN (list))
  739.     hashcode += TYPE_HASH (TREE_CHAIN (list));
  740.  
  741.   if (TREE_VALUE (list))
  742.     hashcode += TYPE_HASH (TREE_VALUE (list));
  743.   else
  744.     hashcode += 1007;
  745.   if (TREE_PURPOSE (list))
  746.     hashcode += TYPE_HASH (TREE_PURPOSE (list));
  747.   else
  748.     hashcode += 1009;
  749.   return hashcode;
  750. }
  751.  
  752. /* Look in the type hash table for a type isomorphic to TYPE.
  753.    If one is found, return it.  Otherwise return 0.  */
  754.  
  755. tree
  756. list_hash_lookup (hashcode, list)
  757.      int hashcode;
  758.      tree list;
  759. {
  760.   register struct list_hash *h;
  761.   for (h = list_hash_table[hashcode % TYPE_HASH_SIZE]; h; h = h->next)
  762.     if (h->hashcode == hashcode
  763.     && TREE_VIA_VIRTUAL (h->list) == TREE_VIA_VIRTUAL (list)
  764.     && TREE_VIA_PUBLIC (h->list) == TREE_VIA_PUBLIC (list)
  765.     && TREE_PURPOSE (h->list) == TREE_PURPOSE (list)
  766.     && TREE_VALUE (h->list) == TREE_VALUE (list)
  767.     && TREE_CHAIN (h->list) == TREE_CHAIN (list))
  768.       {
  769.     assert (TREE_TYPE (h->list) == TREE_TYPE (list));
  770.     return h->list;
  771.       }
  772.   return 0;
  773. }
  774.  
  775. /* Add an entry to the list-hash-table
  776.    for a list TYPE whose hash code is HASHCODE.  */
  777.  
  778. void
  779. list_hash_add (hashcode, list)
  780.      int hashcode;
  781.      tree list;
  782. {
  783.   register struct list_hash *h;
  784.  
  785.   h = (struct list_hash *) obstack_alloc (&class_obstack, sizeof (struct list_hash));
  786.   h->hashcode = hashcode;
  787.   h->list = list;
  788.   h->next = list_hash_table[hashcode % TYPE_HASH_SIZE];
  789.   list_hash_table[hashcode % TYPE_HASH_SIZE] = h;
  790. }
  791.  
  792. /* Given TYPE, and HASHCODE its hash code, return the canonical
  793.    object for an identical list if one already exists.
  794.    Otherwise, return TYPE, and record it as the canonical object
  795.    if it is a permanent object.
  796.  
  797.    To use this function, first create a list of the sort you want.
  798.    Then compute its hash code from the fields of the list that
  799.    make it different from other similar lists.
  800.    Then call this function and use the value.
  801.    This function frees the list you pass in if it is a duplicate.  */
  802.  
  803. /* Set to 1 to debug without canonicalization.  Never set by program.  */
  804. int debug_no_list_hash = 0;
  805.  
  806. tree
  807. list_hash_canon (hashcode, list)
  808.      int hashcode;
  809.      tree list;
  810. {
  811.   tree t1;
  812.  
  813.   if (debug_no_list_hash)
  814.     return list;
  815.  
  816.   t1 = list_hash_lookup (hashcode, list);
  817.   if (t1 != 0)
  818.     {
  819.       obstack_free (&class_obstack, list);
  820.       return t1;
  821.     }
  822.  
  823.   /* If this is a new list, record it for later reuse.  */
  824.   list_hash_add (hashcode, list);
  825.  
  826.   return list;
  827. }
  828.  
  829. tree
  830. hash_tree_cons (via_public, via_virtual, purpose, value, chain)
  831.      int via_public, via_virtual;
  832.      tree purpose, value, chain;
  833. {
  834.   struct obstack *ambient_obstack = current_obstack;
  835.   tree t;
  836.   int hashcode;
  837.  
  838.   current_obstack = &class_obstack;
  839.   t = tree_cons (purpose, value, chain);
  840.   TREE_VIA_PUBLIC (t) = via_public;
  841.   TREE_VIA_VIRTUAL (t) = via_virtual;
  842.   hashcode = list_hash (t);
  843.   t = list_hash_canon (hashcode, t);
  844.   current_obstack = ambient_obstack;
  845.   return t;
  846. }
  847.  
  848. /* Constructor for hashed lists.  */
  849. tree
  850. hash_tree_chain (value, chain)
  851.      tree value, chain;
  852. {
  853.   struct obstack *ambient_obstack = current_obstack;
  854.   tree t;
  855.   int hashcode;
  856.  
  857.   current_obstack = &class_obstack;
  858.   t = tree_cons (NULL_TREE, value, chain);
  859.   hashcode = list_hash (t);
  860.   t = list_hash_canon (hashcode, t);
  861.   current_obstack = ambient_obstack;
  862.   return t;
  863. }
  864.  
  865. /* Similar, but used for concatenating two lists.  */
  866. tree
  867. hash_chainon (list1, list2)
  868.      tree list1, list2;
  869. {
  870.   if (list2 == 0)
  871.     return list1;
  872.   if (list1 == 0)
  873.     return list2;
  874.   if (TREE_CHAIN (list1) == NULL_TREE)
  875.     return hash_tree_chain (TREE_VALUE (list1), list2);
  876.   return hash_tree_chain (TREE_VALUE (list1),
  877.               hash_chainon (TREE_CHAIN (list1), list2));
  878. }
  879.  
  880. tree
  881. get_decl_list (value)
  882.      tree value;
  883. {
  884.   tree list = NULL_TREE;
  885.  
  886.   if (TREE_CODE (value) == IDENTIFIER_NODE)
  887.     {
  888.       list = IDENTIFIER_AS_LIST (value);
  889.       if (list != NULL_TREE
  890.       && (TREE_CODE (list) != TREE_LIST
  891.           || TREE_VALUE (list) != value))
  892.     list = NULL_TREE;
  893.       else if (IDENTIFIER_HAS_TYPE_VALUE (value)
  894.            && TREE_CODE (IDENTIFIER_TYPE_VALUE (value)) == RECORD_TYPE)
  895.     {
  896.       tree type = IDENTIFIER_TYPE_VALUE (value);
  897.       if (CLASSTYPE_ID_AS_LIST (type) == NULL_TREE)
  898.         CLASSTYPE_ID_AS_LIST (type) = perm_tree_cons (NULL_TREE, value, NULL_TREE);
  899.       list = CLASSTYPE_ID_AS_LIST (type);
  900.     }
  901.     }
  902.   else if (TREE_CODE (value) == RECORD_TYPE
  903.        && TYPE_LANG_SPECIFIC (value))
  904.     list = CLASSTYPE_AS_LIST (value);
  905.  
  906.   if (list != NULL_TREE)
  907.     {
  908.       assert (TREE_CHAIN (list) == NULL_TREE);
  909.       return list;
  910.     }
  911. #ifdef OBJCPLUS
  912.   return build_tree_list (NULL_TREE, value);
  913. #else
  914.   return build_decl_list (NULL_TREE, value);
  915. #endif
  916. }
  917.  
  918. /* Look in the type hash table for a type isomorphic to
  919.    `build_tree_list (NULL_TREE, VALUE)'.
  920.    If one is found, return it.  Otherwise return 0.  */
  921.  
  922. tree
  923. list_hash_lookup_or_cons (value)
  924.      tree value;
  925. {
  926.   register int hashcode = TYPE_HASH (value);
  927.   register struct list_hash *h;
  928.   struct obstack *ambient_obstack;
  929.   tree list = NULL_TREE;
  930.  
  931.   if (TREE_CODE (value) == IDENTIFIER_NODE)
  932.     {
  933.       list = IDENTIFIER_AS_LIST (value);
  934.       if (list != NULL_TREE
  935.       && (TREE_CODE (list) != TREE_LIST
  936.           || TREE_VALUE (list) != value))
  937.     list = NULL_TREE;
  938.       else if (IDENTIFIER_HAS_TYPE_VALUE (value)
  939.            && TREE_CODE (IDENTIFIER_TYPE_VALUE (value)) == RECORD_TYPE)
  940.     {
  941.       /* If the type name and constructor name are different, don't
  942.          write constructor name into type.  */
  943.       extern tree constructor_name ();
  944.       if (IDENTIFIER_TYPEDECL_VALUE (value)
  945.           && IDENTIFIER_TYPEDECL_VALUE (value) != constructor_name (value))
  946.         list = tree_cons (NULL_TREE, value, NULL_TREE);
  947.       else
  948.         {
  949.           tree type = IDENTIFIER_TYPE_VALUE (value);
  950.           if (CLASSTYPE_ID_AS_LIST (type) == NULL_TREE)
  951.         CLASSTYPE_ID_AS_LIST (type) = perm_tree_cons (NULL_TREE, value,
  952.                                   NULL_TREE);
  953.           list = CLASSTYPE_ID_AS_LIST (type);
  954.         }
  955.     }
  956.     }
  957.   else if (TREE_CODE (value) == TYPE_DECL
  958.        && TREE_CODE (TREE_TYPE (value)) == RECORD_TYPE
  959.        && TYPE_LANG_SPECIFIC (TREE_TYPE (value)))
  960.     list = CLASSTYPE_ID_AS_LIST (TREE_TYPE (value));
  961.   else if (TREE_CODE (value) == RECORD_TYPE
  962.        && TYPE_LANG_SPECIFIC (value))
  963.     list = CLASSTYPE_AS_LIST (value);
  964.  
  965.   if (list != NULL_TREE)
  966.     {
  967.       assert (TREE_CHAIN (list) == NULL_TREE);
  968.       return list;
  969.     }
  970.  
  971.   if (debug_no_list_hash)
  972.     return hash_tree_chain (value, NULL_TREE);
  973.  
  974.   for (h = list_hash_table[hashcode % TYPE_HASH_SIZE]; h; h = h->next)
  975.     if (h->hashcode == hashcode
  976.     && TREE_VIA_VIRTUAL (h->list) == 0
  977.     && TREE_VIA_PUBLIC (h->list) == 0
  978.     && TREE_PURPOSE (h->list) == 0
  979.     && TREE_VALUE (h->list) == value)
  980.       {
  981.     assert (TREE_TYPE (h->list) == 0);
  982.     assert (TREE_CHAIN (h->list) == 0);
  983.     return h->list;
  984.       }
  985.  
  986.   ambient_obstack = current_obstack;
  987.   current_obstack = &class_obstack;
  988.   list = build_tree_list (NULL_TREE, value);
  989.   list_hash_add (hashcode, list);
  990.   current_obstack = ambient_obstack;
  991.   return list;
  992. }
  993.  
  994. /* Build an association between TYPE and some parameters:
  995.  
  996.    OFFSET is the offset added to `this' to convert it to a pointer
  997.    of type `TYPE *'
  998.  
  999.    VTABLE is the virtual function table with which to initialize
  1000.    sub-objects of type TYPE.
  1001.  
  1002.    VIRTUALS are the virtual functions sitting in VTABLE.
  1003.  
  1004.    CHAIN are more associations we must retain.  */
  1005.  
  1006. tree
  1007. make_binfo (offset, type, vtable, virtuals, chain)
  1008.      tree offset, type;
  1009.      tree vtable, virtuals;
  1010.      tree chain;
  1011. {
  1012.   tree binfo = make_tree_vec (5);
  1013.   tree old_binfo = TYPE_BINFO (type);
  1014.   tree last;
  1015.  
  1016.   TREE_CHAIN (binfo) = chain;
  1017.   if (chain)
  1018.     TREE_USED (binfo) = TREE_USED (chain);
  1019.  
  1020.   TREE_TYPE (binfo) = TYPE_MAIN_VARIANT (type);
  1021.   TREE_VEC_ELT (binfo, 1) = offset;
  1022.   TREE_VEC_ELT (binfo, 2) = vtable;
  1023.   TREE_VEC_ELT (binfo, 3) = virtuals;
  1024.  
  1025.   last = binfo;
  1026.   if (old_binfo != NULL_TREE
  1027.       && BINFO_BASETYPES (old_binfo) != NULL_TREE)
  1028.     {
  1029.       int i, n_baseclasses = CLASSTYPE_N_BASECLASSES (type);
  1030.       tree binfos = TYPE_BINFO_BASETYPES (type);
  1031.  
  1032.       BINFO_BASETYPES (binfo) = make_tree_vec (n_baseclasses);
  1033.       for (i = 0; i < n_baseclasses; i++)
  1034.     {
  1035.       tree child = TREE_VEC_ELT (binfos, i);
  1036.       tree old_child = old_binfo ? BINFO_BASETYPE (old_binfo, i) : 0;
  1037.       BINFO_BASETYPE (binfo, i) = child;
  1038.       if (old_binfo)
  1039.         {
  1040.           TREE_VIA_PUBLIC (child) = TREE_VIA_PUBLIC (old_child);
  1041.           TREE_VIA_VIRTUAL (child) = TREE_VIA_VIRTUAL (old_child);
  1042.         }
  1043.     }
  1044.     }
  1045.   return binfo;
  1046. }
  1047.  
  1048. tree
  1049. copy_binfo (list)
  1050.      tree list;
  1051. {
  1052.   tree binfo = copy_list (list);
  1053.   tree rval = binfo;
  1054.   while (binfo)
  1055.     {
  1056.       TREE_USED (binfo) = 0;
  1057.       if (BINFO_BASETYPES (binfo))
  1058.     BINFO_BASETYPES (binfo) = copy_node (BINFO_BASETYPES (binfo));
  1059.       binfo = TREE_CHAIN (binfo);
  1060.     }
  1061.   return rval;
  1062. }
  1063.  
  1064. /* Return the binfo value for ELEM in TYPE.  Due to structure
  1065.    sharing, we may find ELEM only in the association list
  1066.    belonging to a basetype of TYPE.
  1067.  
  1068.    COPYING is 0 if we just want an binfo value without needing
  1069.    to modify it.
  1070.    COPYING is 1 if we want the binfo value in order to modify it.
  1071.    In this case, if we don't find ELEM immediately in the binfo
  1072.    values of TYPE, we return a copy.
  1073.    COPYING is -1 if we are called recursively and need a copy.
  1074.    In this case we return a copy of ELEM at the point we find it.  */
  1075. tree
  1076. binfo_value (elem, type, copying)
  1077.      tree elem;
  1078.      tree type;
  1079.      int copying;
  1080. {
  1081.   tree binfo = TYPE_BINFO (type);
  1082.   tree last;
  1083.   tree rval = NULL_TREE;
  1084.  
  1085.   /* Dispose quickly of degenerate case.  */
  1086.   if (elem == type)
  1087.     return copying < 0 ? copy_binfo (binfo) : binfo;
  1088.  
  1089.   /* Look for ELEM in two passes.  First pass checks the entire binfo list.
  1090.      Second pass recursively searches the binfo lists of binfos.  */
  1091.   while (binfo)
  1092.     {
  1093.       if (elem == BINFO_TYPE (binfo))
  1094.     /* If we find it on the main spine, then
  1095.        there can be no ambiguity.  */
  1096.     return copying < 0 ? copy_binfo (binfo) : binfo;
  1097.       last = binfo;
  1098.       binfo = TREE_CHAIN (binfo);
  1099.     }
  1100.  
  1101.   for (binfo = TYPE_BINFO (type);
  1102.        binfo != TREE_CHAIN (last);
  1103.        binfo = TREE_CHAIN (binfo))
  1104.     {
  1105.       /* ??? Should this condition instead test
  1106.      BINFO_TYPE (binfo) != TYPE_MAIN_VARIANT (type) ???  */
  1107.       if (BINFO_TYPE (binfo) != TYPE_MAIN_VARIANT (type))
  1108.     {
  1109.       tree nval = binfo_value (elem, BINFO_TYPE (binfo), copying ? -1 : 0);
  1110.  
  1111.       if (nval)
  1112.         {
  1113.           if (copying && rval == NULL_TREE)
  1114.         chainon (TYPE_BINFO (type), nval);
  1115.  
  1116.           if (rval && BINFO_TYPE (rval) != BINFO_TYPE (nval))
  1117.         /* If we find it underneath, we must make sure that
  1118.            there are no two ways to do it.  */
  1119.         compiler_error ("base class `%s' ambiguous in binfo_value",
  1120.                 TYPE_NAME_STRING (elem));
  1121.           else
  1122.         rval = nval;
  1123.         }
  1124.     }
  1125.     }
  1126.   return rval;
  1127. }
  1128.  
  1129. tree
  1130. reverse_path (path)
  1131.      tree path;
  1132. {
  1133.   register tree prev = 0, tmp, next;
  1134.   for (tmp = path; tmp; tmp = next)
  1135.     {
  1136.       next = BINFO_INHERITANCE_CHAIN (tmp);
  1137.       BINFO_INHERITANCE_CHAIN (tmp) = prev;
  1138.       prev = tmp;
  1139.     }
  1140.   return prev;
  1141. }
  1142.  
  1143. tree
  1144. virtual_member (elem, list)
  1145.      tree elem;
  1146.      tree list;
  1147. {
  1148.   tree t;
  1149.   tree rval, nval;
  1150.  
  1151.   for (t = list; t; t = TREE_CHAIN (t))
  1152.     if (elem == BINFO_TYPE (t))
  1153.       return t;
  1154.   rval = 0;
  1155.   for (t = list; t; t = TREE_CHAIN (t))
  1156.     {
  1157.       tree binfos = BINFO_BASETYPES (t);
  1158.       int i;
  1159.  
  1160.       if (binfos != NULL_TREE)
  1161.     for (i = TREE_VEC_LENGTH (binfos)-1; i >= 0; i--)
  1162.       {
  1163.         nval = binfo_value (elem, BINFO_TYPE (TREE_VEC_ELT (binfos, i)), 0);
  1164.         if (nval)
  1165.           {
  1166.         if (rval && BINFO_OFFSET (nval) != BINFO_OFFSET (rval))
  1167.           my_friendly_abort (104);
  1168.         rval = nval;
  1169.           }
  1170.       }
  1171.     }
  1172.   return rval;
  1173. }
  1174.  
  1175. /* Return the offset (as an INTEGER_CST) for ELEM in LIST.
  1176.    INITIAL_OFFSET is the value to add to the offset that ELEM's
  1177.    binfo entry in LIST provides.
  1178.  
  1179.    Returns NULL if ELEM does not have an binfo value in LIST.  */
  1180.  
  1181. tree
  1182. virtual_offset (elem, list, initial_offset)
  1183.      tree elem;
  1184.      tree list;
  1185.      tree initial_offset;
  1186. {
  1187.   tree vb, offset;
  1188.   tree rval, nval;
  1189.  
  1190.   for (vb = list; vb; vb = TREE_CHAIN (vb))
  1191.     if (elem == BINFO_TYPE (vb))
  1192.       return size_binop (PLUS_EXPR, initial_offset, BINFO_OFFSET (vb));
  1193.   rval = 0;
  1194.   for (vb = list; vb; vb = TREE_CHAIN (vb))
  1195.     {
  1196.       tree binfos = BINFO_BASETYPES (vb);
  1197.       int i;
  1198.  
  1199.       if (binfos == NULL_TREE)
  1200.     continue;
  1201.  
  1202.       for (i = TREE_VEC_LENGTH (binfos)-1; i >= 0; i--)
  1203.     {
  1204.       nval = binfo_value (elem, BINFO_TYPE (TREE_VEC_ELT (binfos, i)), 0);
  1205.       if (nval)
  1206.         {
  1207.           if (rval && BINFO_OFFSET (nval) != BINFO_OFFSET (rval))
  1208.         my_friendly_abort (105);
  1209.           offset = BINFO_OFFSET (vb);
  1210.           rval = nval;
  1211.         }
  1212.     }
  1213.     }
  1214.   if (rval == NULL_TREE)
  1215.     return rval;
  1216.   return size_binop (PLUS_EXPR, offset, BINFO_OFFSET (rval));
  1217. }
  1218.  
  1219. void
  1220. debug_binfo (elem)
  1221.      tree elem;
  1222. {
  1223.   int i;
  1224.   tree virtuals;
  1225.  
  1226.   fprintf (stderr, "type \"%s\"; offset = %d\n",
  1227.        TYPE_NAME_STRING (BINFO_TYPE (elem)),
  1228.        TREE_INT_CST_LOW (BINFO_OFFSET (elem)));
  1229.   fprintf (stderr, "vtable type:\n");
  1230.   debug_tree (BINFO_TYPE (elem));
  1231.   if (BINFO_VTABLE (elem))
  1232.     fprintf (stderr, "vtable decl \"%s\"\n", IDENTIFIER_POINTER (DECL_NAME (BINFO_VTABLE (elem))));
  1233.   else
  1234.     fprintf (stderr, "no vtable decl yet\n");
  1235.   fprintf (stderr, "virtuals:\n");
  1236.   virtuals = BINFO_VIRTUALS (elem);
  1237.   if (virtuals != 0)
  1238.     {
  1239.       virtuals = TREE_CHAIN (virtuals);
  1240.       if (flag_dossier)
  1241.     virtuals = TREE_CHAIN (virtuals);
  1242.     }
  1243.   i = 1;
  1244.   while (virtuals)
  1245.     {
  1246.       tree fndecl = TREE_OPERAND (FNADDR_FROM_VTABLE_ENTRY (TREE_VALUE (virtuals)), 0);
  1247.       fprintf (stderr, "%s [%d =? %d]\n",
  1248.            IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (fndecl)),
  1249.            i, TREE_INT_CST_LOW (DECL_VINDEX (fndecl)));
  1250.       virtuals = TREE_CHAIN (virtuals);
  1251.       i += 1;
  1252.     }
  1253. }
  1254.  
  1255. /* Return the length of a chain of nodes chained through DECL_CHAIN.
  1256.    We expect a null pointer to mark the end of the chain.
  1257.    This is the Lisp primitive `length'.  */
  1258.  
  1259. int
  1260. decl_list_length (t)
  1261.      tree t;
  1262. {
  1263.   register tree tail;
  1264.   register int len = 0;
  1265.  
  1266.   assert (TREE_CODE (t) == FUNCTION_DECL);
  1267.   for (tail = t; tail; tail = DECL_CHAIN (tail))
  1268.     len++;
  1269.  
  1270.   return len;
  1271. }
  1272.  
  1273. tree
  1274. fnaddr_from_vtable_entry (entry)
  1275.      tree entry;
  1276. {
  1277.   return TREE_VALUE (TREE_CHAIN (TREE_CHAIN (CONSTRUCTOR_ELTS (entry))));
  1278. }
  1279.  
  1280. void
  1281. set_fnaddr_from_vtable_entry (entry, value)
  1282.      tree entry, value;
  1283. {
  1284.   TREE_VALUE (TREE_CHAIN (TREE_CHAIN (CONSTRUCTOR_ELTS (entry)))) = value;
  1285. }
  1286.  
  1287. tree
  1288. function_arg_chain (t)
  1289.      tree t;
  1290. {
  1291.   return TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (t)));
  1292. }
  1293.  
  1294. int
  1295. promotes_to_aggr_type (t, code)
  1296.      tree t;
  1297.      enum tree_code code;
  1298. {
  1299.   if (TREE_CODE (t) == code)
  1300.     t = TREE_TYPE (t);
  1301.   return IS_AGGR_TYPE (t);
  1302. }
  1303.  
  1304. int
  1305. is_aggr_type_2 (t1, t2)
  1306.      tree t1, t2;
  1307. {
  1308.   if (TREE_CODE (t1) != TREE_CODE (t2))
  1309.     return 0;
  1310.   return IS_AGGR_TYPE (t1) && IS_AGGR_TYPE (t2);
  1311. }
  1312.  
  1313. /* Give message using types TYPE1 and TYPE2 as arguments.
  1314.    PFN is the function which will print the message;
  1315.    S is the format string for PFN to use.  */
  1316. void
  1317. message_2_types (pfn, s, type1, type2)
  1318.      void (*pfn) ();
  1319.      char *s;
  1320.      tree type1, type2;
  1321. {
  1322.   tree name1 = TYPE_NAME (type1);
  1323.   tree name2 = TYPE_NAME (type2);
  1324.   if (TREE_CODE (name1) == TYPE_DECL)
  1325.     name1 = DECL_NAME (name1);
  1326.   if (TREE_CODE (name2) == TYPE_DECL)
  1327.     name2 = DECL_NAME (name2);
  1328.   (*pfn) (s, IDENTIFIER_POINTER (name1), IDENTIFIER_POINTER (name2));
  1329. }
  1330.  
  1331. #define PRINT_RING_SIZE 4
  1332.  
  1333. char *
  1334. lang_printable_name (decl)
  1335.      tree decl;
  1336. {
  1337.   static tree decl_ring[PRINT_RING_SIZE];
  1338.   static char *print_ring[PRINT_RING_SIZE];
  1339.   static int ring_counter;
  1340.   int i;
  1341.  
  1342.   if (TREE_CODE (decl) != FUNCTION_DECL
  1343.       || DECL_LANG_SPECIFIC (decl) == 0)
  1344.     {
  1345.       if (DECL_NAME (decl))
  1346.     {
  1347.       if (THIS_NAME_P (DECL_NAME (decl)))
  1348.         return "this";
  1349.       return IDENTIFIER_POINTER (DECL_NAME (decl));
  1350.     }
  1351.       return "((anonymous))";
  1352.     }
  1353.  
  1354.   /* See if this print name is lying around.  */
  1355.   for (i = 0; i < PRINT_RING_SIZE; i++)
  1356.     if (decl_ring[i] == decl)
  1357.       /* yes, so return it.  */
  1358.       return print_ring[i];
  1359.  
  1360.   if (++ring_counter == PRINT_RING_SIZE)
  1361.     ring_counter = 0;
  1362.  
  1363.   if (current_function_decl != NULL_TREE)
  1364.     {
  1365.       if (decl_ring[ring_counter] == current_function_decl)
  1366.     ring_counter += 1;
  1367.       if (ring_counter == PRINT_RING_SIZE)
  1368.     ring_counter = 0;
  1369.       if (decl_ring[ring_counter] == current_function_decl)
  1370.     my_friendly_abort (106);
  1371.     }
  1372.  
  1373.   if (print_ring[ring_counter])
  1374.     free (print_ring[ring_counter]);
  1375.  
  1376.   {
  1377.     int print_ret_type_p
  1378.       = (!DECL_CONSTRUCTOR_P (decl)
  1379.      && !DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (decl)));
  1380.  
  1381.     char *name = (char *)fndecl_as_string (0, decl, print_ret_type_p);
  1382.     print_ring[ring_counter] = (char *)malloc (strlen (name) + 1);
  1383.     strcpy (print_ring[ring_counter], name);
  1384.     decl_ring[ring_counter] = decl;
  1385.   }
  1386.   return print_ring[ring_counter];
  1387. }
  1388.  
  1389. /* Comparison function for sorting identifiers in RAISES lists.
  1390.    Note that because IDENTIFIER_NODEs are unique, we can sort
  1391.    them by address, saving an indirection.  */
  1392. static int
  1393. id_cmp (p1, p2)
  1394.      tree *p1, *p2;
  1395. {
  1396.   return (int)TREE_VALUE (*p1) - (int)TREE_VALUE (*p2);
  1397. }
  1398.  
  1399. /* Build the FUNCTION_TYPE or METHOD_TYPE which may raise exceptions
  1400.    listed in RAISES.  */
  1401. tree
  1402. build_exception_variant (ctype, type, raises)
  1403.      tree ctype, type;
  1404.      tree raises;
  1405. {
  1406.   int i;
  1407.   tree v = TYPE_MAIN_VARIANT (type);
  1408.   tree t, t2, cname;
  1409.   tree *a = (tree *)alloca ((list_length (raises)+1) * sizeof (tree));
  1410.   int constp = TYPE_READONLY (type);
  1411.   int volatilep = TYPE_VOLATILE (type);
  1412.  
  1413.   if (raises && TREE_CHAIN (raises))
  1414.     {
  1415.       for (i = 0, t = raises; t; t = TREE_CHAIN (t), i++)
  1416.     a[i] = t;
  1417.       /* NULL terminator for list.  */
  1418.       a[i] = NULL_TREE;
  1419.       qsort (a, i, sizeof (tree), id_cmp);
  1420.       while (i--)
  1421.     TREE_CHAIN (a[i]) = a[i+1];
  1422.       raises = a[0];
  1423.     }
  1424.   else if (raises)
  1425.     /* do nothing.  */;
  1426.   else
  1427.     return build_type_variant (v, constp, volatilep);
  1428.  
  1429.   if (ctype)
  1430.     {
  1431.       cname = TYPE_NAME (ctype);
  1432.       if (TREE_CODE (cname) == TYPE_DECL)
  1433.     cname = DECL_NAME (cname);
  1434.     }
  1435.   else
  1436.     cname = NULL_TREE;
  1437.  
  1438.   for (t = raises; t; t = TREE_CHAIN (t))
  1439.     {
  1440.       /* See that all the exceptions we are thinking about
  1441.      raising have been declared.  */
  1442.       tree this_cname = lookup_exception_cname (ctype, cname, t);
  1443.       tree decl = lookup_exception_object (this_cname, TREE_VALUE (t), 1);
  1444.  
  1445.       if (decl == NULL_TREE)
  1446.     decl = lookup_exception_object (this_cname, TREE_VALUE (t), 0);
  1447.       /* Place canonical exception decl into TREE_TYPE of RAISES list.  */
  1448.       TREE_TYPE (t) = decl;
  1449.     }
  1450.  
  1451.   for (v = TYPE_NEXT_VARIANT (v); v; v = TYPE_NEXT_VARIANT (v))
  1452.     {
  1453.       if (TYPE_READONLY (v) != constp
  1454.       || TYPE_VOLATILE (v) != volatilep)
  1455.     continue;
  1456.  
  1457.       t = raises;
  1458.       t2 = TYPE_RAISES_EXCEPTIONS (v);
  1459.       while (t && t2)
  1460.     {
  1461.       if (TREE_TYPE (t) == TREE_TYPE (t2))
  1462.         {
  1463.           t = TREE_CHAIN (t);
  1464.           t2 = TREE_CHAIN (t2);
  1465.         }
  1466.       else break;
  1467.     }
  1468.       if (t || t2)
  1469.     continue;
  1470.       /* List of exceptions raised matches previously found list.
  1471.  
  1472.          @@ Nice to free up storage used in consing up the
  1473.      @@ list of exceptions raised.  */
  1474.       return v;
  1475.     }
  1476.  
  1477.   /* Need to build a new variant.  */
  1478.   v = copy_node (type);
  1479.   TYPE_NEXT_VARIANT (v) = TYPE_NEXT_VARIANT (type);
  1480.   TYPE_NEXT_VARIANT (type) = v;
  1481.   if (raises && ! TREE_PERMANENT (raises))
  1482.     {
  1483.       push_obstacks_nochange ();
  1484.       end_temporary_allocation ();
  1485.       raises = copy_list (raises);
  1486.       pop_obstacks ();
  1487.     }
  1488.   TYPE_RAISES_EXCEPTIONS (v) = raises;
  1489.   return v;
  1490. }
  1491.  
  1492. /* Subroutine of copy_to_permanent
  1493.  
  1494.    Assuming T is a node build bottom-up, make it all exist on
  1495.    permanent obstack, if it is not permanent already.  */
  1496. static tree
  1497. make_deep_copy (t)
  1498.      tree t;
  1499. {
  1500.   enum tree_code code;
  1501.  
  1502.   if (t == NULL_TREE || TREE_PERMANENT (t))
  1503.     return t;
  1504.  
  1505.   switch (code = TREE_CODE (t))
  1506.     {
  1507.     case ERROR_MARK:
  1508.       return error_mark_node;
  1509.  
  1510.     case VAR_DECL:
  1511.     case FUNCTION_DECL:
  1512.     case CONST_DECL:
  1513.       break;
  1514.  
  1515.     case PARM_DECL:
  1516.       {
  1517.     tree chain = TREE_CHAIN (t);
  1518.     t = copy_node (t);
  1519.     TREE_CHAIN (t) = make_deep_copy (chain);
  1520.     TREE_TYPE (t) = make_deep_copy (TREE_TYPE (t));
  1521.     DECL_INITIAL (t) = make_deep_copy (DECL_INITIAL (t));
  1522.     DECL_SIZE (t) = make_deep_copy (DECL_SIZE (t));
  1523.     return t;
  1524.       }
  1525.  
  1526.     case TREE_LIST:
  1527.       {
  1528.     tree chain = TREE_CHAIN (t);
  1529.     t = copy_node (t);
  1530.     TREE_PURPOSE (t) = make_deep_copy (TREE_PURPOSE (t));
  1531.     TREE_VALUE (t) = make_deep_copy (TREE_VALUE (t));
  1532.     TREE_CHAIN (t) = make_deep_copy (chain);
  1533.     return t;
  1534.       }
  1535.  
  1536.     case TREE_VEC:
  1537.       {
  1538.     int len = TREE_VEC_LENGTH (t);
  1539.  
  1540.     t = copy_node (t);
  1541.     while (len--)
  1542.       TREE_VEC_ELT (t, len) = make_deep_copy (TREE_VEC_ELT (t, len));
  1543.     return t;
  1544.       }
  1545.  
  1546.     case INTEGER_CST:
  1547.     case REAL_CST:
  1548.     case STRING_CST:
  1549.       return copy_node (t);
  1550.  
  1551.     case COND_EXPR:
  1552.     case TARGET_EXPR:
  1553.     case NEW_EXPR:
  1554.       t = copy_node (t);
  1555.       TREE_OPERAND (t, 0) = make_deep_copy (TREE_OPERAND (t, 0));
  1556.       TREE_OPERAND (t, 1) = make_deep_copy (TREE_OPERAND (t, 1));
  1557.       TREE_OPERAND (t, 2) = make_deep_copy (TREE_OPERAND (t, 2));
  1558.       return t;
  1559.  
  1560.     case SAVE_EXPR:
  1561.       t = copy_node (t);
  1562.       TREE_OPERAND (t, 0) = make_deep_copy (TREE_OPERAND (t, 0));
  1563.       return t;
  1564.  
  1565.     case MODIFY_EXPR:
  1566.     case PLUS_EXPR:
  1567.     case MINUS_EXPR:
  1568.     case MULT_EXPR:
  1569.     case TRUNC_DIV_EXPR:
  1570.     case TRUNC_MOD_EXPR:
  1571.     case MIN_EXPR:
  1572.     case MAX_EXPR:
  1573.     case LSHIFT_EXPR:
  1574.     case RSHIFT_EXPR:
  1575.     case BIT_IOR_EXPR:
  1576.     case BIT_XOR_EXPR:
  1577.     case BIT_AND_EXPR:
  1578.     case BIT_ANDTC_EXPR:
  1579.     case TRUTH_ANDIF_EXPR:
  1580.     case TRUTH_ORIF_EXPR:
  1581.     case LT_EXPR:
  1582.     case LE_EXPR:
  1583.     case GT_EXPR:
  1584.     case GE_EXPR:
  1585.     case EQ_EXPR:
  1586.     case NE_EXPR:
  1587.     case CEIL_DIV_EXPR:
  1588.     case FLOOR_DIV_EXPR:
  1589.     case ROUND_DIV_EXPR:
  1590.     case CEIL_MOD_EXPR:
  1591.     case FLOOR_MOD_EXPR:
  1592.     case ROUND_MOD_EXPR:
  1593.     case COMPOUND_EXPR:
  1594.     case PREDECREMENT_EXPR:
  1595.     case PREINCREMENT_EXPR:
  1596.     case POSTDECREMENT_EXPR:
  1597.     case POSTINCREMENT_EXPR:
  1598.     case CALL_EXPR:
  1599.       t = copy_node (t);
  1600.       TREE_OPERAND (t, 0) = make_deep_copy (TREE_OPERAND (t, 0));
  1601.       TREE_OPERAND (t, 1) = make_deep_copy (TREE_OPERAND (t, 1));
  1602.       return t;
  1603.  
  1604.     case CONVERT_EXPR:
  1605.     case ADDR_EXPR:
  1606.     case INDIRECT_REF:
  1607.     case NEGATE_EXPR:
  1608.     case BIT_NOT_EXPR:
  1609.     case TRUTH_NOT_EXPR:
  1610.     case NOP_EXPR:
  1611.     case COMPONENT_REF:
  1612.       t = copy_node (t);
  1613.       TREE_OPERAND (t, 0) = make_deep_copy (TREE_OPERAND (t, 0));
  1614.       return t;
  1615.  
  1616.       /*  This list is incomplete, but should suffice for now.
  1617.       It is very important that `sorry' does not call
  1618.       `report_error_function'.  That could cause an infinite loop.  */
  1619.     default:
  1620.       sorry ("initializer contains unrecognized tree code");
  1621.       return error_mark_node;
  1622.  
  1623.     }
  1624.   my_friendly_abort (107);
  1625.   /* NOTREACHED */
  1626.   return NULL_TREE;
  1627. }
  1628.  
  1629. /* Assuming T is a node built bottom-up, make it all exist on
  1630.    permanent obstack, if it is not permanent already.  */
  1631. tree
  1632. copy_to_permanent (t)
  1633.      tree t;
  1634. {
  1635.   register struct obstack *ambient_obstack = current_obstack;
  1636.   register struct obstack *ambient_saveable_obstack = saveable_obstack;
  1637.  
  1638.   if (t == NULL_TREE || TREE_PERMANENT (t))
  1639.     return t;
  1640.  
  1641.   saveable_obstack = &permanent_obstack;
  1642.   current_obstack = saveable_obstack;
  1643.  
  1644.   t = make_deep_copy (t);
  1645.  
  1646.   current_obstack = ambient_obstack;
  1647.   saveable_obstack = ambient_saveable_obstack;
  1648.  
  1649.   return t;
  1650. }
  1651.  
  1652. void
  1653. print_lang_statistics ()
  1654. {
  1655.   extern struct obstack maybepermanent_obstack;
  1656.   print_obstack_statistics ("class_obstack", &class_obstack);
  1657.   print_obstack_statistics ("permanent_obstack", &permanent_obstack);
  1658.   print_obstack_statistics ("maybepermanent_obstack", &maybepermanent_obstack);
  1659.   print_search_statistics ();
  1660.   print_class_statistics ();
  1661. }
  1662.  
  1663. /* This is used by the `assert' macro.  It is provided in libgcc.a,
  1664.    which `cc' doesn't know how to link.  */
  1665. void
  1666. __eprintf (string, expression, line, filename)
  1667. #ifdef __STDC__
  1668.      const char *string;
  1669.      const char *expression;
  1670.      int line;
  1671.      const char *filename;
  1672. #else
  1673.      char *string;
  1674.      char *expression;
  1675.      int line;
  1676.      char *filename;
  1677. #endif
  1678. {
  1679.   fprintf (stderr, string, expression, line, filename);
  1680.   fflush (stderr);
  1681.   abort ();
  1682. }
  1683.  
  1684. /* Return, as an INTEGER_CST node, the number of elements for
  1685.    TYPE (which is an ARRAY_TYPE).  This counts only elements of the top array. */
  1686.  
  1687. tree
  1688. array_type_nelts_top (type)
  1689.      tree type;
  1690. {
  1691.   return fold (build (PLUS_EXPR, integer_type_node,
  1692.               array_type_nelts (type),
  1693.               integer_one_node));
  1694. }
  1695.  
  1696. /* Return, as an INTEGER_CST node, the number of elements for
  1697.    TYPE (which is an ARRAY_TYPE).  This one is a recursive count of all
  1698.    ARRAY_TYPEs that are clumped together. */
  1699.  
  1700. tree
  1701. array_type_nelts_total (type)
  1702.      tree type;
  1703. {
  1704.   tree index_type = TYPE_DOMAIN (type);
  1705.   tree sz = array_type_nelts_top (type);
  1706.   type = TREE_TYPE (type);
  1707.   while (TREE_CODE (type) == ARRAY_TYPE)
  1708.     {
  1709.       tree n = array_type_nelts_top (type);
  1710.       sz = fold (build (MULT_EXPR, integer_type_node, sz, n));
  1711.       type = TREE_TYPE (type);
  1712.     }
  1713.   return sz;
  1714. }
  1715.