OSDN Git Service

8fbd59bcc9c3c403b22f0b47703d3da23025b495
[pf3gnuchains/gcc-fork.git] / gcc / cp / tree.c
1 /* Language-dependent node constructors for parse phase of GNU compiler.
2    Copyright (C) 1987, 88, 92, 93, 94, 1995 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, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.  */
21
22 #include "config.h"
23 #include <stdio.h>
24 #include "obstack.h"
25 #include "tree.h"
26 #include "cp-tree.h"
27 #include "flags.h"
28 #include "rtl.h"
29 #ifdef __STDC__
30 #include <stdarg.h>
31 #else
32 #include <varargs.h>
33 #endif
34
35 #define CEIL(x,y) (((x) + (y) - 1) / (y))
36
37 /* Return nonzero if REF is an lvalue valid for this language.
38    Lvalues can be assigned, unless they have TREE_READONLY.
39    Lvalues can have their address taken, unless they have DECL_REGISTER.  */
40
41 int
42 real_lvalue_p (ref)
43      tree ref;
44 {
45   if (! language_lvalue_valid (ref))
46     return 0;
47   
48   if (TREE_CODE (TREE_TYPE (ref)) == REFERENCE_TYPE)
49     return 1;
50
51   if (ref == current_class_decl && flag_this_is_variable <= 0)
52     return 0;
53
54   switch (TREE_CODE (ref))
55     {
56       /* preincrements and predecrements are valid lvals, provided
57          what they refer to are valid lvals. */
58     case PREINCREMENT_EXPR:
59     case PREDECREMENT_EXPR:
60     case COMPONENT_REF:
61     case SAVE_EXPR:
62       return real_lvalue_p (TREE_OPERAND (ref, 0));
63
64     case STRING_CST:
65       return 1;
66
67     case VAR_DECL:
68       if (TREE_READONLY (ref) && ! TREE_STATIC (ref)
69           && DECL_LANG_SPECIFIC (ref)
70           && DECL_IN_AGGR_P (ref))
71         return 0;
72     case INDIRECT_REF:
73     case ARRAY_REF:
74     case PARM_DECL:
75     case RESULT_DECL:
76     case ERROR_MARK:
77       if (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
78           && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE)
79         return 1;
80       break;
81
82       /* A currently unresolved scope ref.  */
83     case SCOPE_REF:
84       my_friendly_abort (103);
85     case OFFSET_REF:
86       if (TREE_CODE (TREE_OPERAND (ref, 1)) == FUNCTION_DECL)
87         return 1;
88       return real_lvalue_p (TREE_OPERAND (ref, 0))
89         && real_lvalue_p (TREE_OPERAND (ref, 1));
90       break;
91
92     case COND_EXPR:
93       return (real_lvalue_p (TREE_OPERAND (ref, 1))
94               && real_lvalue_p (TREE_OPERAND (ref, 2)));
95
96     case MODIFY_EXPR:
97       return 1;
98
99     case COMPOUND_EXPR:
100       return real_lvalue_p (TREE_OPERAND (ref, 1));
101
102     case MAX_EXPR:
103     case MIN_EXPR:
104       return (real_lvalue_p (TREE_OPERAND (ref, 0))
105               && real_lvalue_p (TREE_OPERAND (ref, 1)));
106     }
107
108   return 0;
109 }
110
111 int
112 lvalue_p (ref)
113      tree ref;
114 {
115   if (! language_lvalue_valid (ref))
116     return 0;
117   
118   if (TREE_CODE (TREE_TYPE (ref)) == REFERENCE_TYPE)
119     return 1;
120
121   if (ref == current_class_decl && flag_this_is_variable <= 0)
122     return 0;
123
124   switch (TREE_CODE (ref))
125     {
126       /* preincrements and predecrements are valid lvals, provided
127          what they refer to are valid lvals. */
128     case PREINCREMENT_EXPR:
129     case PREDECREMENT_EXPR:
130     case COMPONENT_REF:
131     case SAVE_EXPR:
132       return lvalue_p (TREE_OPERAND (ref, 0));
133
134     case STRING_CST:
135       return 1;
136
137     case VAR_DECL:
138       if (TREE_READONLY (ref) && ! TREE_STATIC (ref)
139           && DECL_LANG_SPECIFIC (ref)
140           && DECL_IN_AGGR_P (ref))
141         return 0;
142     case INDIRECT_REF:
143     case ARRAY_REF:
144     case PARM_DECL:
145     case RESULT_DECL:
146     case ERROR_MARK:
147       if (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
148           && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE)
149         return 1;
150       break;
151
152     case TARGET_EXPR:
153       return 1;
154
155     case CALL_EXPR:
156       if (IS_AGGR_TYPE (TREE_TYPE (ref)))
157         return 1;
158       break;
159
160       /* A currently unresolved scope ref.  */
161     case SCOPE_REF:
162       my_friendly_abort (103);
163     case OFFSET_REF:
164       if (TREE_CODE (TREE_OPERAND (ref, 1)) == FUNCTION_DECL)
165         return 1;
166       return lvalue_p (TREE_OPERAND (ref, 0))
167         && lvalue_p (TREE_OPERAND (ref, 1));
168       break;
169
170     case COND_EXPR:
171       return (lvalue_p (TREE_OPERAND (ref, 1))
172               && lvalue_p (TREE_OPERAND (ref, 2)));
173
174     case MODIFY_EXPR:
175       return 1;
176
177     case COMPOUND_EXPR:
178       return lvalue_p (TREE_OPERAND (ref, 1));
179
180     case MAX_EXPR:
181     case MIN_EXPR:
182       return (lvalue_p (TREE_OPERAND (ref, 0))
183               && lvalue_p (TREE_OPERAND (ref, 1)));
184     }
185
186   return 0;
187 }
188
189 /* Return nonzero if REF is an lvalue valid for this language;
190    otherwise, print an error message and return zero.  */
191
192 int
193 lvalue_or_else (ref, string)
194      tree ref;
195      char *string;
196 {
197   int win = lvalue_p (ref);
198   if (! win)
199     error ("non-lvalue in %s", string);
200   return win;
201 }
202
203 /* INIT is a CALL_EXPR which needs info about its target.
204    TYPE is the type that this initialization should appear to have.
205
206    Build an encapsulation of the initialization to perform
207    and return it so that it can be processed by language-independent
208    and language-specific expression expanders.  */
209 tree
210 build_cplus_new (type, init)
211      tree type;
212      tree init;
213 {
214   tree slot;
215   tree rval;
216
217   slot = build (VAR_DECL, type);
218   layout_decl (slot, 0);
219   rval = build (NEW_EXPR, type,
220                 TREE_OPERAND (init, 0), TREE_OPERAND (init, 1), slot);
221   TREE_SIDE_EFFECTS (rval) = 1;
222   TREE_ADDRESSABLE (rval) = 1;
223   rval = build (TARGET_EXPR, type, slot, rval, 0);
224   TREE_SIDE_EFFECTS (rval) = 1;
225   TREE_ADDRESSABLE (rval) = 1;
226
227   return rval;
228 }
229
230 /* Recursively search EXP for CALL_EXPRs that need cleanups and replace
231    these CALL_EXPRs with tree nodes that will perform the cleanups.  */
232
233 tree
234 break_out_cleanups (exp)
235      tree exp;
236 {
237   tree tmp = exp;
238
239   if (TREE_CODE (tmp) == CALL_EXPR
240       && TYPE_NEEDS_DESTRUCTOR (TREE_TYPE (tmp)))
241     return build_cplus_new (TREE_TYPE (tmp), tmp);
242
243   while (TREE_CODE (tmp) == NOP_EXPR
244          || TREE_CODE (tmp) == CONVERT_EXPR
245          || TREE_CODE (tmp) == NON_LVALUE_EXPR)
246     {
247       if (TREE_CODE (TREE_OPERAND (tmp, 0)) == CALL_EXPR
248           && TYPE_NEEDS_DESTRUCTOR (TREE_TYPE (TREE_OPERAND (tmp, 0))))
249         {
250           TREE_OPERAND (tmp, 0)
251             = build_cplus_new (TREE_TYPE (TREE_OPERAND (tmp, 0)),
252                                TREE_OPERAND (tmp, 0));
253           break;
254         }
255       else
256         tmp = TREE_OPERAND (tmp, 0);
257     }
258   return exp;
259 }
260
261 /* Recursively perform a preorder search EXP for CALL_EXPRs, making
262    copies where they are found.  Returns a deep copy all nodes transitively
263    containing CALL_EXPRs.  */
264
265 tree
266 break_out_calls (exp)
267      tree exp;
268 {
269   register tree t1, t2;
270   register enum tree_code code;
271   register int changed = 0;
272   register int i;
273
274   if (exp == NULL_TREE)
275     return exp;
276
277   code = TREE_CODE (exp);
278
279   if (code == CALL_EXPR)
280     return copy_node (exp);
281
282   /* Don't try and defeat a save_expr, as it should only be done once. */
283     if (code == SAVE_EXPR)
284        return exp;
285
286   switch (TREE_CODE_CLASS (code))
287     {
288     default:
289       abort ();
290
291     case 'c':  /* a constant */
292     case 't':  /* a type node */
293     case 'x':  /* something random, like an identifier or an ERROR_MARK.  */
294       return exp;
295
296     case 'd':  /* A decl node */
297 #if 0                               /* This is bogus.  jason 9/21/94 */
298
299       t1 = break_out_calls (DECL_INITIAL (exp));
300       if (t1 != DECL_INITIAL (exp))
301         {
302           exp = copy_node (exp);
303           DECL_INITIAL (exp) = t1;
304         }
305 #endif
306       return exp;
307
308     case 'b':  /* A block node */
309       {
310         /* Don't know how to handle these correctly yet.   Must do a
311            break_out_calls on all DECL_INITIAL values for local variables,
312            and also break_out_calls on all sub-blocks and sub-statements.  */
313         abort ();
314       }
315       return exp;
316
317     case 'e':  /* an expression */
318     case 'r':  /* a reference */
319     case 's':  /* an expression with side effects */
320       for (i = tree_code_length[(int) code] - 1; i >= 0; i--)
321         {
322           t1 = break_out_calls (TREE_OPERAND (exp, i));
323           if (t1 != TREE_OPERAND (exp, i))
324             {
325               exp = copy_node (exp);
326               TREE_OPERAND (exp, i) = t1;
327             }
328         }
329       return exp;
330
331     case '<':  /* a comparison expression */
332     case '2':  /* a binary arithmetic expression */
333       t2 = break_out_calls (TREE_OPERAND (exp, 1));
334       if (t2 != TREE_OPERAND (exp, 1))
335         changed = 1;
336     case '1':  /* a unary arithmetic expression */
337       t1 = break_out_calls (TREE_OPERAND (exp, 0));
338       if (t1 != TREE_OPERAND (exp, 0))
339         changed = 1;
340       if (changed)
341         {
342           if (tree_code_length[(int) code] == 1)
343             return build1 (code, TREE_TYPE (exp), t1);
344           else
345             return build (code, TREE_TYPE (exp), t1, t2);
346         }
347       return exp;
348     }
349
350 }
351 \f
352 extern struct obstack *current_obstack;
353 extern struct obstack permanent_obstack, class_obstack;
354 extern struct obstack *saveable_obstack;
355
356 /* Here is how primitive or already-canonicalized types' hash
357    codes are made.  MUST BE CONSISTENT WITH tree.c !!! */
358 #define TYPE_HASH(TYPE) ((HOST_WIDE_INT) (TYPE) & 0777777)
359
360 /* Construct, lay out and return the type of methods belonging to class
361    BASETYPE and whose arguments are described by ARGTYPES and whose values
362    are described by RETTYPE.  If each type exists already, reuse it.  */
363 tree
364 build_cplus_method_type (basetype, rettype, argtypes)
365      tree basetype, rettype, argtypes;
366 {
367   register tree t;
368   tree ptype;
369   int hashcode;
370
371   /* Make a node of the sort we want.  */
372   t = make_node (METHOD_TYPE);
373
374   TYPE_METHOD_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
375   TREE_TYPE (t) = rettype;
376   if (IS_SIGNATURE (basetype))
377     ptype = build_signature_pointer_type (TYPE_MAIN_VARIANT (basetype),
378                                           TYPE_READONLY (basetype),
379                                           TYPE_VOLATILE (basetype));
380   else
381     ptype = build_pointer_type (basetype);
382
383   /* The actual arglist for this function includes a "hidden" argument
384      which is "this".  Put it into the list of argument types.  */
385
386   argtypes = tree_cons (NULL_TREE, ptype, argtypes);
387   TYPE_ARG_TYPES (t) = argtypes;
388   TREE_SIDE_EFFECTS (argtypes) = 1;  /* Mark first argtype as "artificial".  */
389
390   /* If we already have such a type, use the old one and free this one.
391      Note that it also frees up the above cons cell if found.  */
392   hashcode = TYPE_HASH (basetype) + TYPE_HASH (rettype) + type_hash_list (argtypes);
393   t = type_hash_canon (hashcode, t);
394
395   if (TYPE_SIZE (t) == 0)
396     layout_type (t);
397
398   return t;
399 }
400
401 tree
402 build_cplus_array_type (elt_type, index_type)
403      tree elt_type;
404      tree index_type;
405 {
406   register struct obstack *ambient_obstack = current_obstack;
407   register struct obstack *ambient_saveable_obstack = saveable_obstack;
408   tree t;
409
410   /* We need a new one.  If both ELT_TYPE and INDEX_TYPE are permanent,
411      make this permanent too.  */
412   if (TREE_PERMANENT (elt_type)
413       && (index_type == 0 || TREE_PERMANENT (index_type)))
414     {
415       current_obstack = &permanent_obstack;
416       saveable_obstack = &permanent_obstack;
417     }
418
419   if (current_template_parms)
420     {
421       t = make_node (ARRAY_TYPE);
422       TREE_TYPE (t) = elt_type;
423       TYPE_DOMAIN (t) = index_type;
424     }
425   else
426     t = build_array_type (elt_type, index_type);
427
428   /* Push these needs up so that initialization takes place
429      more easily.  */
430   TYPE_NEEDS_CONSTRUCTING (t) = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (elt_type));
431   TYPE_NEEDS_DESTRUCTOR (t) = TYPE_NEEDS_DESTRUCTOR (TYPE_MAIN_VARIANT (elt_type));
432   current_obstack = ambient_obstack;
433   saveable_obstack = ambient_saveable_obstack;
434   return t;
435 }
436 \f
437 /* Make a variant type in the proper way for C/C++, propagating qualifiers
438    down to the element type of an array.  */
439
440 tree
441 cp_build_type_variant (type, constp, volatilep)
442      tree type;
443      int constp, volatilep;
444 {
445   if (TREE_CODE (type) == ARRAY_TYPE)
446     {
447       tree real_main_variant = TYPE_MAIN_VARIANT (type);
448
449       push_obstacks (TYPE_OBSTACK (real_main_variant),
450                      TYPE_OBSTACK (real_main_variant));
451       type = build_cplus_array_type (cp_build_type_variant (TREE_TYPE (type),
452                                                             constp, volatilep),
453                                      TYPE_DOMAIN (type));
454
455       /* TYPE must be on same obstack as REAL_MAIN_VARIANT.  If not,
456          make a copy.  (TYPE might have come from the hash table and
457          REAL_MAIN_VARIANT might be in some function's obstack.)  */
458
459       if (TYPE_OBSTACK (type) != TYPE_OBSTACK (real_main_variant))
460         {
461           type = copy_node (type);
462           TYPE_POINTER_TO (type) = TYPE_REFERENCE_TO (type) = 0;
463         }
464
465       TYPE_MAIN_VARIANT (type) = real_main_variant;
466       pop_obstacks ();
467     }
468   return build_type_variant (type, constp, volatilep);
469 }
470 \f
471 /* Add OFFSET to all base types of T.
472
473    OFFSET, which is a type offset, is number of bytes.
474
475    Note that we don't have to worry about having two paths to the
476    same base type, since this type owns its association list.  */
477 void
478 propagate_binfo_offsets (binfo, offset)
479      tree binfo;
480      tree offset;
481 {
482   tree binfos = BINFO_BASETYPES (binfo);
483   int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
484
485   for (i = 0; i < n_baselinks; /* note increment is done in the loop.  */)
486     {
487       tree base_binfo = TREE_VEC_ELT (binfos, i);
488
489       if (TREE_VIA_VIRTUAL (base_binfo))
490         i += 1;
491       else
492         {
493           int j;
494           tree base_binfos = BINFO_BASETYPES (base_binfo);
495           tree delta;
496
497           for (j = i+1; j < n_baselinks; j++)
498             if (! TREE_VIA_VIRTUAL (TREE_VEC_ELT (binfos, j)))
499               {
500                 /* The next basetype offset must take into account the space
501                    between the classes, not just the size of each class.  */
502                 delta = size_binop (MINUS_EXPR,
503                                     BINFO_OFFSET (TREE_VEC_ELT (binfos, j)),
504                                     BINFO_OFFSET (base_binfo));
505                 break;
506               }
507
508 #if 0
509           if (BINFO_OFFSET_ZEROP (base_binfo))
510             BINFO_OFFSET (base_binfo) = offset;
511           else
512             BINFO_OFFSET (base_binfo)
513               = size_binop (PLUS_EXPR, BINFO_OFFSET (base_binfo), offset);
514 #else
515           BINFO_OFFSET (base_binfo) = offset;
516 #endif
517           if (base_binfos)
518             {
519               int k;
520               tree chain = NULL_TREE;
521
522               /* Now unshare the structure beneath BASE_BINFO.  */
523               for (k = TREE_VEC_LENGTH (base_binfos)-1;
524                    k >= 0; k--)
525                 {
526                   tree base_base_binfo = TREE_VEC_ELT (base_binfos, k);
527                   if (! TREE_VIA_VIRTUAL (base_base_binfo))
528                     TREE_VEC_ELT (base_binfos, k)
529                       = make_binfo (BINFO_OFFSET (base_base_binfo),
530                                     base_base_binfo,
531                                     BINFO_VTABLE (base_base_binfo),
532                                     BINFO_VIRTUALS (base_base_binfo),
533                                     chain);
534                   chain = TREE_VEC_ELT (base_binfos, k);
535                   TREE_VIA_PUBLIC (chain) = TREE_VIA_PUBLIC (base_base_binfo);
536                   TREE_VIA_PROTECTED (chain) = TREE_VIA_PROTECTED (base_base_binfo);
537                   BINFO_INHERITANCE_CHAIN (chain) = base_binfo;
538                 }
539               /* Now propagate the offset to the base types.  */
540               propagate_binfo_offsets (base_binfo, offset);
541             }
542
543           /* Go to our next class that counts for offset propagation.  */
544           i = j;
545           if (i < n_baselinks)
546             offset = size_binop (PLUS_EXPR, offset, delta);
547         }
548     }
549 }
550
551 /* Compute the actual offsets that our virtual base classes
552    will have *for this type*.  This must be performed after
553    the fields are laid out, since virtual baseclasses must
554    lay down at the end of the record.
555
556    Returns the maximum number of virtual functions any of the virtual
557    baseclasses provide.  */
558 int
559 layout_vbasetypes (rec, max)
560      tree rec;
561      int max;
562 {
563   /* Get all the virtual base types that this type uses.
564      The TREE_VALUE slot holds the virtual baseclass type.  */
565   tree vbase_types = get_vbase_types (rec);
566
567 #ifdef STRUCTURE_SIZE_BOUNDARY
568   unsigned record_align = MAX (STRUCTURE_SIZE_BOUNDARY, TYPE_ALIGN (rec));
569 #else
570   unsigned record_align = MAX (BITS_PER_UNIT, TYPE_ALIGN (rec));
571 #endif
572   int desired_align;
573
574   /* Record size so far is CONST_SIZE + VAR_SIZE bits,
575      where CONST_SIZE is an integer
576      and VAR_SIZE is a tree expression.
577      If VAR_SIZE is null, the size is just CONST_SIZE.
578      Naturally we try to avoid using VAR_SIZE.  */
579   register unsigned const_size = 0;
580   register tree var_size = 0;
581   int nonvirtual_const_size;
582
583   CLASSTYPE_VBASECLASSES (rec) = vbase_types;
584
585   if (TREE_CODE (TYPE_SIZE (rec)) == INTEGER_CST)
586     const_size = TREE_INT_CST_LOW (TYPE_SIZE (rec));
587   else
588     var_size = TYPE_SIZE (rec);
589
590   nonvirtual_const_size = const_size;
591
592   while (vbase_types)
593     {
594       tree basetype = BINFO_TYPE (vbase_types);
595       tree offset;
596
597       desired_align = TYPE_ALIGN (basetype);
598       record_align = MAX (record_align, desired_align);
599
600       if (const_size == 0)
601         offset = integer_zero_node;
602       else
603         {
604           /* Give each virtual base type the alignment it wants.  */
605           const_size = CEIL (const_size, TYPE_ALIGN (basetype))
606             * TYPE_ALIGN (basetype);
607           offset = size_int (CEIL (const_size, BITS_PER_UNIT));
608         }
609
610       if (CLASSTYPE_VSIZE (basetype) > max)
611         max = CLASSTYPE_VSIZE (basetype);
612       BINFO_OFFSET (vbase_types) = offset;
613
614       if (TREE_CODE (TYPE_SIZE (basetype)) == INTEGER_CST)
615         {
616           /* Every virtual baseclass takes a least a UNIT, so that we can
617              take it's address and get something different for each base.  */
618           const_size += MAX (BITS_PER_UNIT,
619                              TREE_INT_CST_LOW (TYPE_SIZE (basetype))
620                              - TREE_INT_CST_LOW (CLASSTYPE_VBASE_SIZE (basetype)));
621         }
622       else if (var_size == 0)
623         var_size = TYPE_SIZE (basetype);
624       else
625         var_size = size_binop (PLUS_EXPR, var_size, TYPE_SIZE (basetype));
626
627       vbase_types = TREE_CHAIN (vbase_types);
628     }
629
630   if (const_size)
631     {
632       /* Because a virtual base might take a single byte above,
633          we have to re-adjust the total size to make sure it it
634          a multiple of the alignment.  */
635       /* Give the whole object the alignment it wants.  */
636       const_size = CEIL (const_size, record_align) * record_align;
637     }
638
639   /* Set the alignment in the complete type.  We don't set CLASSTYPE_ALIGN
640    here, as that is for this class, without any virtual base classes.  */
641   TYPE_ALIGN (rec) = record_align;
642   if (const_size != nonvirtual_const_size)
643     {
644       CLASSTYPE_VBASE_SIZE (rec)
645         = size_int (const_size - nonvirtual_const_size);
646       TYPE_SIZE (rec) = size_int (const_size);
647     }
648
649   /* Now propagate offset information throughout the lattice
650      under the vbase type.  */
651   for (vbase_types = CLASSTYPE_VBASECLASSES (rec); vbase_types;
652        vbase_types = TREE_CHAIN (vbase_types))
653     {
654       tree base_binfos = BINFO_BASETYPES (vbase_types);
655
656       BINFO_INHERITANCE_CHAIN (vbase_types) = TYPE_BINFO (rec);
657
658       if (base_binfos)
659         {
660           tree chain = NULL_TREE;
661           int j;
662           /* Now unshare the structure beneath BASE_BINFO.  */
663
664           for (j = TREE_VEC_LENGTH (base_binfos)-1;
665                j >= 0; j--)
666             {
667               tree base_base_binfo = TREE_VEC_ELT (base_binfos, j);
668               if (! TREE_VIA_VIRTUAL (base_base_binfo))
669                 TREE_VEC_ELT (base_binfos, j)
670                   = make_binfo (BINFO_OFFSET (base_base_binfo),
671                                 base_base_binfo,
672                                 BINFO_VTABLE (base_base_binfo),
673                                 BINFO_VIRTUALS (base_base_binfo),
674                                 chain);
675               chain = TREE_VEC_ELT (base_binfos, j);
676               TREE_VIA_PUBLIC (chain) = TREE_VIA_PUBLIC (base_base_binfo);
677               TREE_VIA_PROTECTED (chain) = TREE_VIA_PROTECTED (base_base_binfo);
678               BINFO_INHERITANCE_CHAIN (chain) = vbase_types;
679             }
680
681           propagate_binfo_offsets (vbase_types, BINFO_OFFSET (vbase_types));
682         }
683     }
684
685   return max;
686 }
687
688 /* Lay out the base types of a record type, REC.
689    Tentatively set the size and alignment of REC
690    according to the base types alone.
691
692    Offsets for immediate nonvirtual baseclasses are also computed here.
693
694    TYPE_BINFO (REC) should be NULL_TREE on entry, and this routine
695    creates a list of base_binfos in TYPE_BINFO (REC) from BINFOS.
696
697    Returns list of virtual base classes in a FIELD_DECL chain.  */
698 tree
699 layout_basetypes (rec, binfos)
700      tree rec, binfos;
701 {
702   /* Chain to hold all the new FIELD_DECLs which point at virtual
703      base classes.  */
704   tree vbase_decls = NULL_TREE;
705
706 #ifdef STRUCTURE_SIZE_BOUNDARY
707   unsigned record_align = MAX (STRUCTURE_SIZE_BOUNDARY, TYPE_ALIGN (rec));
708 #else
709   unsigned record_align = MAX (BITS_PER_UNIT, TYPE_ALIGN (rec));
710 #endif
711
712   /* Record size so far is CONST_SIZE + VAR_SIZE bits, where CONST_SIZE is
713      an integer and VAR_SIZE is a tree expression.  If VAR_SIZE is null,
714      the size is just CONST_SIZE.  Naturally we try to avoid using
715      VAR_SIZE.  And so far, we've been successful. */
716 #if 0
717   register tree var_size = 0;
718 #endif
719
720   register unsigned const_size = 0;
721   int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
722
723   /* Handle basetypes almost like fields, but record their
724      offsets differently.  */
725
726   for (i = 0; i < n_baseclasses; i++)
727     {
728       int inc, desired_align, int_vbase_size;
729       register tree base_binfo = TREE_VEC_ELT (binfos, i);
730       register tree basetype = BINFO_TYPE (base_binfo);
731       tree decl, offset;
732
733       if (TYPE_SIZE (basetype) == 0)
734         {
735 #if 0
736           /* This error is now reported in xref_tag, thus giving better
737              location information.  */
738           error_with_aggr_type (base_binfo,
739                                 "base class `%s' has incomplete type");
740
741           TREE_VIA_PUBLIC (base_binfo) = 1;
742           TREE_VIA_PROTECTED (base_binfo) = 0;
743           TREE_VIA_VIRTUAL (base_binfo) = 0;
744
745           /* Should handle this better so that
746
747              class A;
748              class B: private A { virtual void F(); };
749
750              does not dump core when compiled. */
751           my_friendly_abort (121);
752 #endif
753           continue;
754         }
755
756       /* All basetypes are recorded in the association list of the
757          derived type.  */
758
759       if (TREE_VIA_VIRTUAL (base_binfo))
760         {
761           int j;
762           char *name = (char *)alloca (TYPE_NAME_LENGTH (basetype)
763                                        + sizeof (VBASE_NAME) + 1);
764
765           /* The offset for a virtual base class is only used in computing
766              virtual function tables and for initializing virtual base
767              pointers.  It is built once `get_vbase_types' is called.  */
768
769           /* If this basetype can come from another vbase pointer
770              without an additional indirection, we will share
771              that pointer.  If an indirection is involved, we
772              make our own pointer.  */
773           for (j = 0; j < n_baseclasses; j++)
774             {
775               tree other_base_binfo = TREE_VEC_ELT (binfos, j);
776               if (! TREE_VIA_VIRTUAL (other_base_binfo)
777                   && binfo_member (basetype,
778                                    CLASSTYPE_VBASECLASSES (BINFO_TYPE (other_base_binfo))))
779                 goto got_it;
780             }
781           sprintf (name, VBASE_NAME_FORMAT, TYPE_NAME_STRING (basetype));
782           decl = build_lang_field_decl (FIELD_DECL, get_identifier (name),
783                                         build_pointer_type (basetype));
784           /* If you change any of the below, take a look at all the
785              other VFIELD_BASEs and VTABLE_BASEs in the code, and change
786              them too. */
787           DECL_ASSEMBLER_NAME (decl) = get_identifier (VTABLE_BASE);
788           DECL_VIRTUAL_P (decl) = 1;
789           DECL_FIELD_CONTEXT (decl) = rec;
790           DECL_CLASS_CONTEXT (decl) = rec;
791           DECL_FCONTEXT (decl) = basetype;
792           DECL_SAVED_INSNS (decl) = NULL_RTX;
793           DECL_FIELD_SIZE (decl) = 0;
794           DECL_ALIGN (decl) = TYPE_ALIGN (ptr_type_node);
795           TREE_CHAIN (decl) = vbase_decls;
796           BINFO_VPTR_FIELD (base_binfo) = decl;
797           vbase_decls = decl;
798
799           if (warn_nonvdtor && TYPE_HAS_DESTRUCTOR (basetype)
800               && DECL_VINDEX (TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (basetype), 0)) == NULL_TREE)
801             {
802               warning_with_decl (TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (basetype), 0),
803                                  "destructor `%s' non-virtual");
804               warning ("in inheritance relationship `%s: virtual %s'",
805                        TYPE_NAME_STRING (rec),
806                        TYPE_NAME_STRING (basetype));
807             }
808         got_it:
809           /* The space this decl occupies has already been accounted for.  */
810           continue;
811         }
812
813       if (const_size == 0)
814         offset = integer_zero_node;
815       else
816         {
817           /* Give each base type the alignment it wants.  */
818           const_size = CEIL (const_size, TYPE_ALIGN (basetype))
819             * TYPE_ALIGN (basetype);
820           offset = size_int ((const_size + BITS_PER_UNIT - 1) / BITS_PER_UNIT);
821
822 #if 0
823           /* bpk: Disabled this check until someone is willing to
824              claim it as theirs and explain exactly what circumstances
825              warrant the warning.  */ 
826           if (warn_nonvdtor && TYPE_HAS_DESTRUCTOR (basetype)
827               && DECL_VINDEX (TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (basetype), 0)) == NULL_TREE)
828             {
829               warning_with_decl (TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (basetype), 0),
830                                  "destructor `%s' non-virtual");
831               warning ("in inheritance relationship `%s:%s %s'",
832                        TYPE_NAME_STRING (rec),
833                        TREE_VIA_VIRTUAL (base_binfo) ? " virtual" : "",
834                        TYPE_NAME_STRING (basetype));
835             }
836 #endif
837         }
838       BINFO_OFFSET (base_binfo) = offset;
839       if (CLASSTYPE_VSIZE (basetype))
840         {
841           BINFO_VTABLE (base_binfo) = TYPE_BINFO_VTABLE (basetype);
842           BINFO_VIRTUALS (base_binfo) = TYPE_BINFO_VIRTUALS (basetype);
843         }
844       TREE_CHAIN (base_binfo) = TYPE_BINFO (rec);
845       TYPE_BINFO (rec) = base_binfo;
846
847       /* Add only the amount of storage not present in
848          the virtual baseclasses.  */
849
850       int_vbase_size = TREE_INT_CST_LOW (CLASSTYPE_VBASE_SIZE (basetype));
851       if (TREE_INT_CST_LOW (TYPE_SIZE (basetype)) > int_vbase_size)
852         {
853           inc = MAX (record_align,
854                      (TREE_INT_CST_LOW (TYPE_SIZE (basetype))
855                       - int_vbase_size));
856
857           /* Record must have at least as much alignment as any field.  */
858           desired_align = TYPE_ALIGN (basetype);
859           record_align = MAX (record_align, desired_align);
860
861           const_size += inc;
862         }
863     }
864
865   if (const_size)
866     CLASSTYPE_SIZE (rec) = size_int (const_size);
867   else
868     CLASSTYPE_SIZE (rec) = integer_zero_node;
869   CLASSTYPE_ALIGN (rec) = record_align;
870
871   return vbase_decls;
872 }
873 \f
874 /* Hashing of lists so that we don't make duplicates.
875    The entry point is `list_hash_canon'.  */
876
877 /* Each hash table slot is a bucket containing a chain
878    of these structures.  */
879
880 struct list_hash
881 {
882   struct list_hash *next;       /* Next structure in the bucket.  */
883   int hashcode;                 /* Hash code of this list.  */
884   tree list;                    /* The list recorded here.  */
885 };
886
887 /* Now here is the hash table.  When recording a list, it is added
888    to the slot whose index is the hash code mod the table size.
889    Note that the hash table is used for several kinds of lists.
890    While all these live in the same table, they are completely independent,
891    and the hash code is computed differently for each of these.  */
892
893 #define TYPE_HASH_SIZE 59
894 struct list_hash *list_hash_table[TYPE_HASH_SIZE];
895
896 /* Compute a hash code for a list (chain of TREE_LIST nodes
897    with goodies in the TREE_PURPOSE, TREE_VALUE, and bits of the
898    TREE_COMMON slots), by adding the hash codes of the individual entries.  */
899
900 int
901 list_hash (list)
902      tree list;
903 {
904   register int hashcode = 0;
905
906   if (TREE_CHAIN (list))
907     hashcode += TYPE_HASH (TREE_CHAIN (list));
908
909   if (TREE_VALUE (list))
910     hashcode += TYPE_HASH (TREE_VALUE (list));
911   else
912     hashcode += 1007;
913   if (TREE_PURPOSE (list))
914     hashcode += TYPE_HASH (TREE_PURPOSE (list));
915   else
916     hashcode += 1009;
917   return hashcode;
918 }
919
920 /* Look in the type hash table for a type isomorphic to TYPE.
921    If one is found, return it.  Otherwise return 0.  */
922
923 tree
924 list_hash_lookup (hashcode, list)
925      int hashcode;
926      tree list;
927 {
928   register struct list_hash *h;
929   for (h = list_hash_table[hashcode % TYPE_HASH_SIZE]; h; h = h->next)
930     if (h->hashcode == hashcode
931         && TREE_VIA_VIRTUAL (h->list) == TREE_VIA_VIRTUAL (list)
932         && TREE_VIA_PUBLIC (h->list) == TREE_VIA_PUBLIC (list)
933         && TREE_VIA_PROTECTED (h->list) == TREE_VIA_PROTECTED (list)
934         && TREE_PURPOSE (h->list) == TREE_PURPOSE (list)
935         && TREE_VALUE (h->list) == TREE_VALUE (list)
936         && TREE_CHAIN (h->list) == TREE_CHAIN (list))
937       {
938         my_friendly_assert (TREE_TYPE (h->list) == TREE_TYPE (list), 299);
939         return h->list;
940       }
941   return 0;
942 }
943
944 /* Add an entry to the list-hash-table
945    for a list TYPE whose hash code is HASHCODE.  */
946
947 void
948 list_hash_add (hashcode, list)
949      int hashcode;
950      tree list;
951 {
952   register struct list_hash *h;
953
954   h = (struct list_hash *) obstack_alloc (&class_obstack, sizeof (struct list_hash));
955   h->hashcode = hashcode;
956   h->list = list;
957   h->next = list_hash_table[hashcode % TYPE_HASH_SIZE];
958   list_hash_table[hashcode % TYPE_HASH_SIZE] = h;
959 }
960
961 /* Given TYPE, and HASHCODE its hash code, return the canonical
962    object for an identical list if one already exists.
963    Otherwise, return TYPE, and record it as the canonical object
964    if it is a permanent object.
965
966    To use this function, first create a list of the sort you want.
967    Then compute its hash code from the fields of the list that
968    make it different from other similar lists.
969    Then call this function and use the value.
970    This function frees the list you pass in if it is a duplicate.  */
971
972 /* Set to 1 to debug without canonicalization.  Never set by program.  */
973 static int debug_no_list_hash = 0;
974
975 tree
976 list_hash_canon (hashcode, list)
977      int hashcode;
978      tree list;
979 {
980   tree t1;
981
982   if (debug_no_list_hash)
983     return list;
984
985   t1 = list_hash_lookup (hashcode, list);
986   if (t1 != 0)
987     {
988       obstack_free (&class_obstack, list);
989       return t1;
990     }
991
992   /* If this is a new list, record it for later reuse.  */
993   list_hash_add (hashcode, list);
994
995   return list;
996 }
997
998 tree
999 hash_tree_cons (via_public, via_virtual, via_protected, purpose, value, chain)
1000      int via_public, via_virtual, via_protected;
1001      tree purpose, value, chain;
1002 {
1003   struct obstack *ambient_obstack = current_obstack;
1004   tree t;
1005   int hashcode;
1006
1007   current_obstack = &class_obstack;
1008   t = tree_cons (purpose, value, chain);
1009   TREE_VIA_PUBLIC (t) = via_public;
1010   TREE_VIA_PROTECTED (t) = via_protected;
1011   TREE_VIA_VIRTUAL (t) = via_virtual;
1012   hashcode = list_hash (t);
1013   t = list_hash_canon (hashcode, t);
1014   current_obstack = ambient_obstack;
1015   return t;
1016 }
1017
1018 /* Constructor for hashed lists.  */
1019 tree
1020 hash_tree_chain (value, chain)
1021      tree value, chain;
1022 {
1023   struct obstack *ambient_obstack = current_obstack;
1024   tree t;
1025   int hashcode;
1026
1027   current_obstack = &class_obstack;
1028   t = tree_cons (NULL_TREE, value, chain);
1029   hashcode = list_hash (t);
1030   t = list_hash_canon (hashcode, t);
1031   current_obstack = ambient_obstack;
1032   return t;
1033 }
1034
1035 /* Similar, but used for concatenating two lists.  */
1036 tree
1037 hash_chainon (list1, list2)
1038      tree list1, list2;
1039 {
1040   if (list2 == 0)
1041     return list1;
1042   if (list1 == 0)
1043     return list2;
1044   if (TREE_CHAIN (list1) == NULL_TREE)
1045     return hash_tree_chain (TREE_VALUE (list1), list2);
1046   return hash_tree_chain (TREE_VALUE (list1),
1047                           hash_chainon (TREE_CHAIN (list1), list2));
1048 }
1049
1050 static tree
1051 get_identifier_list (value)
1052      tree value;
1053 {
1054   tree list = IDENTIFIER_AS_LIST (value);
1055   if (list != NULL_TREE
1056       && (TREE_CODE (list) != TREE_LIST
1057           || TREE_VALUE (list) != value))
1058     list = NULL_TREE;
1059   else if (IDENTIFIER_HAS_TYPE_VALUE (value)
1060            && TREE_CODE (IDENTIFIER_TYPE_VALUE (value)) == RECORD_TYPE
1061            && IDENTIFIER_TYPE_VALUE (value)
1062               == TYPE_MAIN_VARIANT (IDENTIFIER_TYPE_VALUE (value)))
1063     {
1064       tree type = IDENTIFIER_TYPE_VALUE (value);
1065
1066       if (TYPE_PTRMEMFUNC_P (type))
1067         list = NULL_TREE;
1068       else if (type == current_class_type)
1069         /* Don't mess up the constructor name.  */
1070         list = tree_cons (NULL_TREE, value, NULL_TREE);
1071       else
1072         {
1073           register tree id;
1074           /* This will return the correct thing for regular types,
1075              nested types, and templates.  Yay! */
1076           if (TYPE_NESTED_NAME (type))
1077             id = TYPE_NESTED_NAME (type);
1078           else
1079             id = TYPE_IDENTIFIER (type);
1080
1081           if (CLASSTYPE_ID_AS_LIST (type) == NULL_TREE)
1082             CLASSTYPE_ID_AS_LIST (type)
1083               = perm_tree_cons (NULL_TREE, id, NULL_TREE);
1084           list = CLASSTYPE_ID_AS_LIST (type);
1085         }
1086     }
1087   return list;
1088 }
1089
1090 tree
1091 get_decl_list (value)
1092      tree value;
1093 {
1094   tree list = NULL_TREE;
1095
1096   if (TREE_CODE (value) == IDENTIFIER_NODE)
1097     list = get_identifier_list (value);
1098   else if (TREE_CODE (value) == RECORD_TYPE
1099            && TYPE_LANG_SPECIFIC (value)
1100            && value == TYPE_MAIN_VARIANT (value))
1101     list = CLASSTYPE_AS_LIST (value);
1102
1103   if (list != NULL_TREE)
1104     {
1105       my_friendly_assert (TREE_CHAIN (list) == NULL_TREE, 301);
1106       return list;
1107     }
1108
1109   return build_decl_list (NULL_TREE, value);
1110 }
1111 \f
1112 /* Build an association between TYPE and some parameters:
1113
1114    OFFSET is the offset added to `this' to convert it to a pointer
1115    of type `TYPE *'
1116
1117    BINFO is the base binfo to use, if we are deriving from one.  This
1118    is necessary, as we want specialized parent binfos from base
1119    classes, so that the VTABLE_NAMEs of bases are for the most derived
1120    type, instead of of the simple type.
1121
1122    VTABLE is the virtual function table with which to initialize
1123    sub-objects of type TYPE.
1124
1125    VIRTUALS are the virtual functions sitting in VTABLE.
1126
1127    CHAIN are more associations we must retain.  */
1128
1129 tree
1130 make_binfo (offset, binfo, vtable, virtuals, chain)
1131      tree offset, binfo;
1132      tree vtable, virtuals;
1133      tree chain;
1134 {
1135   tree new_binfo = make_tree_vec (6);
1136   tree type;
1137
1138   if (TREE_CODE (binfo) == TREE_VEC)
1139     type = BINFO_TYPE (binfo);
1140   else
1141     {
1142       type = binfo;
1143       binfo = TYPE_BINFO (binfo);
1144     }
1145
1146   TREE_CHAIN (new_binfo) = chain;
1147   if (chain)
1148     TREE_USED (new_binfo) = TREE_USED (chain);
1149
1150   TREE_TYPE (new_binfo) = TYPE_MAIN_VARIANT (type);
1151   BINFO_OFFSET (new_binfo) = offset;
1152   BINFO_VTABLE (new_binfo) = vtable;
1153   BINFO_VIRTUALS (new_binfo) = virtuals;
1154   BINFO_VPTR_FIELD (new_binfo) = NULL_TREE;
1155
1156   if (binfo && BINFO_BASETYPES (binfo) != NULL_TREE)
1157     BINFO_BASETYPES (new_binfo) = copy_node (BINFO_BASETYPES (binfo));      
1158   return new_binfo;
1159 }
1160
1161 /* Return the binfo value for ELEM in TYPE.  */
1162
1163 tree
1164 binfo_value (elem, type)
1165      tree elem;
1166      tree type;
1167 {
1168   if (get_base_distance (elem, type, 0, (tree *)0) == -2)
1169     compiler_error ("base class `%s' ambiguous in binfo_value",
1170                     TYPE_NAME_STRING (elem));
1171   if (elem == type)
1172     return TYPE_BINFO (type);
1173   if (TREE_CODE (elem) == RECORD_TYPE && TYPE_BINFO (elem) == type)
1174     return type;
1175   return get_binfo (elem, type, 0);
1176 }
1177
1178 tree
1179 reverse_path (path)
1180      tree path;
1181 {
1182   register tree prev = 0, tmp, next;
1183   for (tmp = path; tmp; tmp = next)
1184     {
1185       next = BINFO_INHERITANCE_CHAIN (tmp);
1186       BINFO_INHERITANCE_CHAIN (tmp) = prev;
1187       prev = tmp;
1188     }
1189   return prev;
1190 }
1191
1192 void
1193 debug_binfo (elem)
1194      tree elem;
1195 {
1196   unsigned HOST_WIDE_INT n;
1197   tree virtuals;
1198
1199   fprintf (stderr, "type \"%s\"; offset = %d\n",
1200            TYPE_NAME_STRING (BINFO_TYPE (elem)),
1201            TREE_INT_CST_LOW (BINFO_OFFSET (elem)));
1202   fprintf (stderr, "vtable type:\n");
1203   debug_tree (BINFO_TYPE (elem));
1204   if (BINFO_VTABLE (elem))
1205     fprintf (stderr, "vtable decl \"%s\"\n", IDENTIFIER_POINTER (DECL_NAME (BINFO_VTABLE (elem))));
1206   else
1207     fprintf (stderr, "no vtable decl yet\n");
1208   fprintf (stderr, "virtuals:\n");
1209   virtuals = BINFO_VIRTUALS (elem);
1210
1211   n = skip_rtti_stuff (&virtuals);
1212
1213   while (virtuals)
1214     {
1215       tree fndecl = TREE_OPERAND (FNADDR_FROM_VTABLE_ENTRY (TREE_VALUE (virtuals)), 0);
1216       fprintf (stderr, "%s [%d =? %d]\n",
1217                IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (fndecl)),
1218                n, TREE_INT_CST_LOW (DECL_VINDEX (fndecl)));
1219       ++n;
1220       virtuals = TREE_CHAIN (virtuals);
1221     }
1222 }
1223
1224 /* Return the length of a chain of nodes chained through DECL_CHAIN.
1225    We expect a null pointer to mark the end of the chain.
1226    This is the Lisp primitive `length'.  */
1227
1228 int
1229 decl_list_length (t)
1230      tree t;
1231 {
1232   register tree tail;
1233   register int len = 0;
1234
1235   my_friendly_assert (TREE_CODE (t) == FUNCTION_DECL
1236                       || TREE_CODE (t) == TEMPLATE_DECL, 300);
1237   for (tail = t; tail; tail = DECL_CHAIN (tail))
1238     len++;
1239
1240   return len;
1241 }
1242
1243 int
1244 count_functions (t)
1245      tree t;
1246 {
1247   if (TREE_CODE (t) == FUNCTION_DECL)
1248     return 1;
1249   else if (TREE_CODE (t) == TREE_LIST)
1250     return decl_list_length (TREE_VALUE (t));
1251
1252   my_friendly_abort (359);
1253   return 0;
1254 }
1255
1256 int
1257 is_overloaded_fn (x)
1258      tree x;
1259 {
1260   if (TREE_CODE (x) == FUNCTION_DECL)
1261     return 1;
1262
1263   if (TREE_CODE (x) == TREE_LIST
1264       && (TREE_CODE (TREE_VALUE (x)) == FUNCTION_DECL
1265           || TREE_CODE (TREE_VALUE (x)) == TEMPLATE_DECL))
1266     return 1;
1267
1268   return 0;
1269 }
1270
1271 int
1272 really_overloaded_fn (x)
1273      tree x;
1274 {     
1275   if (TREE_CODE (x) == TREE_LIST
1276       && (TREE_CODE (TREE_VALUE (x)) == FUNCTION_DECL
1277           || TREE_CODE (TREE_VALUE (x)) == TEMPLATE_DECL))
1278     return 1;
1279
1280   return 0;
1281 }
1282
1283 tree
1284 get_first_fn (from)
1285      tree from;
1286 {
1287   if (TREE_CODE (from) == FUNCTION_DECL)
1288     return from;
1289
1290   my_friendly_assert (TREE_CODE (from) == TREE_LIST, 9);
1291   
1292   return TREE_VALUE (from);
1293 }
1294
1295 tree
1296 fnaddr_from_vtable_entry (entry)
1297      tree entry;
1298 {
1299   if (flag_vtable_thunks)
1300     {
1301       tree func = entry;
1302       if (TREE_CODE (func) == ADDR_EXPR)
1303         func = TREE_OPERAND (func, 0);
1304       if (TREE_CODE (func) == THUNK_DECL)
1305         return DECL_INITIAL (func);
1306       else
1307         return entry;
1308     }
1309   else
1310     return TREE_VALUE (TREE_CHAIN (TREE_CHAIN (CONSTRUCTOR_ELTS (entry))));
1311 }
1312
1313 tree
1314 function_arg_chain (t)
1315      tree t;
1316 {
1317   return TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (t)));
1318 }
1319
1320 int
1321 promotes_to_aggr_type (t, code)
1322      tree t;
1323      enum tree_code code;
1324 {
1325   if (TREE_CODE (t) == code)
1326     t = TREE_TYPE (t);
1327   return IS_AGGR_TYPE (t);
1328 }
1329
1330 int
1331 is_aggr_type_2 (t1, t2)
1332      tree t1, t2;
1333 {
1334   if (TREE_CODE (t1) != TREE_CODE (t2))
1335     return 0;
1336   return IS_AGGR_TYPE (t1) && IS_AGGR_TYPE (t2);
1337 }
1338
1339 /* Give message using types TYPE1 and TYPE2 as arguments.
1340    PFN is the function which will print the message;
1341    S is the format string for PFN to use.  */
1342 void
1343 message_2_types (pfn, s, type1, type2)
1344      void (*pfn) ();
1345      char *s;
1346      tree type1, type2;
1347 {
1348   tree name1 = TYPE_NAME (type1);
1349   tree name2 = TYPE_NAME (type2);
1350   if (TREE_CODE (name1) == TYPE_DECL)
1351     name1 = DECL_NAME (name1);
1352   if (TREE_CODE (name2) == TYPE_DECL)
1353     name2 = DECL_NAME (name2);
1354   (*pfn) (s, IDENTIFIER_POINTER (name1), IDENTIFIER_POINTER (name2));
1355 }
1356 \f
1357 #define PRINT_RING_SIZE 4
1358
1359 char *
1360 lang_printable_name (decl)
1361      tree decl;
1362 {
1363   static tree decl_ring[PRINT_RING_SIZE];
1364   static char *print_ring[PRINT_RING_SIZE];
1365   static int ring_counter;
1366   int i;
1367
1368   /* Only cache functions.  */
1369   if (TREE_CODE (decl) != FUNCTION_DECL
1370       || DECL_LANG_SPECIFIC (decl) == 0)
1371     return decl_as_string (decl, 1);
1372
1373   /* See if this print name is lying around.  */
1374   for (i = 0; i < PRINT_RING_SIZE; i++)
1375     if (decl_ring[i] == decl)
1376       /* yes, so return it.  */
1377       return print_ring[i];
1378
1379   if (++ring_counter == PRINT_RING_SIZE)
1380     ring_counter = 0;
1381
1382   if (current_function_decl != NULL_TREE)
1383     {
1384       if (decl_ring[ring_counter] == current_function_decl)
1385         ring_counter += 1;
1386       if (ring_counter == PRINT_RING_SIZE)
1387         ring_counter = 0;
1388       if (decl_ring[ring_counter] == current_function_decl)
1389         my_friendly_abort (106);
1390     }
1391
1392   if (print_ring[ring_counter])
1393     free (print_ring[ring_counter]);
1394
1395   {
1396     int print_ret_type_p
1397       = (!DECL_CONSTRUCTOR_P (decl)
1398          && !DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (decl)));
1399
1400     char *name = (char *)decl_as_string (decl, print_ret_type_p);
1401     print_ring[ring_counter] = (char *)malloc (strlen (name) + 1);
1402     strcpy (print_ring[ring_counter], name);
1403     decl_ring[ring_counter] = decl;
1404   }
1405   return print_ring[ring_counter];
1406 }
1407 \f
1408 /* Build the FUNCTION_TYPE or METHOD_TYPE which may throw exceptions
1409    listed in RAISES.  */
1410 tree
1411 build_exception_variant (type, raises)
1412      tree type;
1413      tree raises;
1414 {
1415   tree v = TYPE_MAIN_VARIANT (type);
1416   int constp = TYPE_READONLY (type);
1417   int volatilep = TYPE_VOLATILE (type);
1418
1419   for (; v; v = TYPE_NEXT_VARIANT (v))
1420     {
1421       if (TYPE_READONLY (v) != constp
1422           || TYPE_VOLATILE (v) != volatilep)
1423         continue;
1424
1425       /* @@ This should do set equality, not exact match. */
1426       if (simple_cst_list_equal (TYPE_RAISES_EXCEPTIONS (v), raises))
1427         /* List of exceptions raised matches previously found list.
1428
1429            @@ Nice to free up storage used in consing up the
1430            @@ list of exceptions raised.  */
1431         return v;
1432     }
1433
1434   /* Need to build a new variant.  */
1435   v = build_type_copy (type);
1436
1437   if (raises && ! TREE_PERMANENT (raises))
1438     {
1439       push_obstacks_nochange ();
1440       end_temporary_allocation ();
1441       raises = copy_list (raises);
1442       pop_obstacks ();
1443     }
1444
1445   TYPE_RAISES_EXCEPTIONS (v) = raises;
1446   return v;
1447 }
1448
1449 /* Subroutine of copy_to_permanent
1450
1451    Assuming T is a node build bottom-up, make it all exist on
1452    permanent obstack, if it is not permanent already.  */
1453
1454 tree
1455 mapcar (t, func)
1456      tree t;
1457      tree (*func)();
1458 {
1459   tree tmp;
1460
1461   if (t == NULL_TREE)
1462     return t;
1463
1464   if (tmp = func (t), tmp != NULL_TREE)
1465     return tmp;
1466
1467   switch (TREE_CODE (t))
1468     {
1469     case ERROR_MARK:
1470       return error_mark_node;
1471
1472     case VAR_DECL:
1473     case FUNCTION_DECL:
1474     case CONST_DECL:
1475       break;
1476
1477     case PARM_DECL:
1478       {
1479         tree chain = TREE_CHAIN (t);
1480         t = copy_node (t);
1481         TREE_CHAIN (t) = mapcar (chain, func);
1482         TREE_TYPE (t) = mapcar (TREE_TYPE (t), func);
1483         DECL_INITIAL (t) = mapcar (DECL_INITIAL (t), func);
1484         DECL_SIZE (t) = mapcar (DECL_SIZE (t), func);
1485         return t;
1486       }
1487
1488     case TREE_LIST:
1489       {
1490         tree chain = TREE_CHAIN (t);
1491         t = copy_node (t);
1492         TREE_PURPOSE (t) = mapcar (TREE_PURPOSE (t), func);
1493         TREE_VALUE (t) = mapcar (TREE_VALUE (t), func);
1494         TREE_CHAIN (t) = mapcar (chain, func);
1495         return t;
1496       }
1497
1498     case TREE_VEC:
1499       {
1500         int len = TREE_VEC_LENGTH (t);
1501
1502         t = copy_node (t);
1503         while (len--)
1504           TREE_VEC_ELT (t, len) = mapcar (TREE_VEC_ELT (t, len), func);
1505         return t;
1506       }
1507
1508     case INTEGER_CST:
1509     case REAL_CST:
1510     case STRING_CST:
1511       return copy_node (t);
1512
1513     case COND_EXPR:
1514     case TARGET_EXPR:
1515     case NEW_EXPR:
1516       t = copy_node (t);
1517       TREE_OPERAND (t, 0) = mapcar (TREE_OPERAND (t, 0), func);
1518       TREE_OPERAND (t, 1) = mapcar (TREE_OPERAND (t, 1), func);
1519       TREE_OPERAND (t, 2) = mapcar (TREE_OPERAND (t, 2), func);
1520       return t;
1521
1522     case SAVE_EXPR:
1523       t = copy_node (t);
1524       TREE_OPERAND (t, 0) = mapcar (TREE_OPERAND (t, 0), func);
1525       return t;
1526
1527     case MODIFY_EXPR:
1528     case PLUS_EXPR:
1529     case MINUS_EXPR:
1530     case MULT_EXPR:
1531     case TRUNC_DIV_EXPR:
1532     case TRUNC_MOD_EXPR:
1533     case MIN_EXPR:
1534     case MAX_EXPR:
1535     case LSHIFT_EXPR:
1536     case RSHIFT_EXPR:
1537     case BIT_IOR_EXPR:
1538     case BIT_XOR_EXPR:
1539     case BIT_AND_EXPR:
1540     case BIT_ANDTC_EXPR:
1541     case TRUTH_ANDIF_EXPR:
1542     case TRUTH_ORIF_EXPR:
1543     case LT_EXPR:
1544     case LE_EXPR:
1545     case GT_EXPR:
1546     case GE_EXPR:
1547     case EQ_EXPR:
1548     case NE_EXPR:
1549     case CEIL_DIV_EXPR:
1550     case FLOOR_DIV_EXPR:
1551     case ROUND_DIV_EXPR:
1552     case CEIL_MOD_EXPR:
1553     case FLOOR_MOD_EXPR:
1554     case ROUND_MOD_EXPR:
1555     case COMPOUND_EXPR:
1556     case PREDECREMENT_EXPR:
1557     case PREINCREMENT_EXPR:
1558     case POSTDECREMENT_EXPR:
1559     case POSTINCREMENT_EXPR:
1560     case CALL_EXPR:
1561     case ARRAY_REF:
1562     case SCOPE_REF:
1563       t = copy_node (t);
1564       TREE_OPERAND (t, 0) = mapcar (TREE_OPERAND (t, 0), func);
1565       TREE_OPERAND (t, 1) = mapcar (TREE_OPERAND (t, 1), func);
1566       return t;
1567
1568     case CONVERT_EXPR:
1569     case ADDR_EXPR:
1570     case INDIRECT_REF:
1571     case NEGATE_EXPR:
1572     case BIT_NOT_EXPR:
1573     case TRUTH_NOT_EXPR:
1574     case NOP_EXPR:
1575     case COMPONENT_REF:
1576       t = copy_node (t);
1577       TREE_OPERAND (t, 0) = mapcar (TREE_OPERAND (t, 0), func);
1578       return t;
1579
1580     case POINTER_TYPE:
1581       return build_pointer_type (mapcar (TREE_TYPE (t), func));
1582     case REFERENCE_TYPE:
1583       return build_reference_type (mapcar (TREE_TYPE (t), func));
1584     case FUNCTION_TYPE:
1585       return build_function_type (mapcar (TREE_TYPE (t), func),
1586                                   mapcar (TYPE_ARG_TYPES (t), func));
1587     case ARRAY_TYPE:
1588       return build_array_type (mapcar (TREE_TYPE (t), func),
1589                                mapcar (TYPE_DOMAIN (t), func));
1590     case INTEGER_TYPE:
1591       return build_index_type (mapcar (TYPE_MAX_VALUE (t), func));
1592
1593     case OFFSET_TYPE:
1594       return build_offset_type (mapcar (TYPE_OFFSET_BASETYPE (t), func),
1595                                 mapcar (TREE_TYPE (t), func));
1596     case METHOD_TYPE:
1597       return build_method_type
1598         (mapcar (TYPE_METHOD_BASETYPE (t), func),
1599          build_function_type
1600          (mapcar (TREE_TYPE (t), func),
1601           mapcar (TREE_CHAIN (TYPE_ARG_TYPES (t)), func)));
1602
1603     case RECORD_TYPE:
1604       if (TYPE_PTRMEMFUNC_P (t))
1605         return build_ptrmemfunc_type
1606           (mapcar (TYPE_PTRMEMFUNC_FN_TYPE (t), func));
1607       /* else fall through */
1608       
1609       /*  This list is incomplete, but should suffice for now.
1610           It is very important that `sorry' does not call
1611           `report_error_function'.  That could cause an infinite loop.  */
1612     default:
1613       sorry ("initializer contains unrecognized tree code");
1614       return error_mark_node;
1615
1616     }
1617   my_friendly_abort (107);
1618   /* NOTREACHED */
1619   return NULL_TREE;
1620 }
1621
1622 static tree
1623 perm_manip (t)
1624      tree t;
1625 {
1626   if (TREE_PERMANENT (t))
1627     return t;
1628   /* Support `void f () { extern int i; A<&i> a; }' */
1629   if ((TREE_CODE (t) == VAR_DECL || TREE_CODE (t) == FUNCTION_DECL)
1630       && TREE_PUBLIC (t))
1631     return copy_node (t);
1632   return NULL_TREE;
1633 }
1634
1635 /* Assuming T is a node built bottom-up, make it all exist on
1636    permanent obstack, if it is not permanent already.  */
1637 tree
1638 copy_to_permanent (t)
1639      tree t;
1640 {
1641   register struct obstack *ambient_obstack = current_obstack;
1642   register struct obstack *ambient_saveable_obstack = saveable_obstack;
1643   int resume;
1644
1645   if (t == NULL_TREE || TREE_PERMANENT (t))
1646     return t;
1647
1648   saveable_obstack = &permanent_obstack;
1649   current_obstack = saveable_obstack;
1650   resume = suspend_momentary ();
1651
1652   t = mapcar (t, perm_manip);
1653
1654   resume_momentary (resume);
1655   current_obstack = ambient_obstack;
1656   saveable_obstack = ambient_saveable_obstack;
1657
1658   return t;
1659 }
1660
1661 #ifdef GATHER_STATISTICS
1662 extern int depth_reached;
1663 #endif
1664
1665 void
1666 print_lang_statistics ()
1667 {
1668   extern struct obstack maybepermanent_obstack, decl_obstack;
1669   print_obstack_statistics ("class_obstack", &class_obstack);
1670   print_obstack_statistics ("decl_obstack", &decl_obstack);
1671   print_obstack_statistics ("permanent_obstack", &permanent_obstack);
1672   print_obstack_statistics ("maybepermanent_obstack", &maybepermanent_obstack);
1673   print_search_statistics ();
1674   print_class_statistics ();
1675 #ifdef GATHER_STATISTICS
1676   fprintf (stderr, "maximum template instantiation depth reached: %d\n",
1677            depth_reached);
1678 #endif
1679 }
1680
1681 /* This is used by the `assert' macro.  It is provided in libgcc.a,
1682    which `cc' doesn't know how to link.  Note that the C++ front-end
1683    no longer actually uses the `assert' macro (instead, it calls
1684    my_friendly_assert).  But all of the back-end files still need this.  */
1685 void
1686 __eprintf (string, expression, line, filename)
1687 #ifdef __STDC__
1688      const char *string;
1689      const char *expression;
1690      unsigned line;
1691      const char *filename;
1692 #else
1693      char *string;
1694      char *expression;
1695      unsigned line;
1696      char *filename;
1697 #endif
1698 {
1699   fprintf (stderr, string, expression, line, filename);
1700   fflush (stderr);
1701   abort ();
1702 }
1703
1704 /* Return, as an INTEGER_CST node, the number of elements for
1705    TYPE (which is an ARRAY_TYPE).  This counts only elements of the top array. */
1706
1707 tree
1708 array_type_nelts_top (type)
1709      tree type;
1710 {
1711   return fold (build (PLUS_EXPR, sizetype,
1712                       array_type_nelts (type),
1713                       integer_one_node));
1714 }
1715
1716 /* Return, as an INTEGER_CST node, the number of elements for
1717    TYPE (which is an ARRAY_TYPE).  This one is a recursive count of all
1718    ARRAY_TYPEs that are clumped together. */
1719
1720 tree
1721 array_type_nelts_total (type)
1722      tree type;
1723 {
1724   tree sz = array_type_nelts_top (type);
1725   type = TREE_TYPE (type);
1726   while (TREE_CODE (type) == ARRAY_TYPE)
1727     {
1728       tree n = array_type_nelts_top (type);
1729       sz = fold (build (MULT_EXPR, sizetype, sz, n));
1730       type = TREE_TYPE (type);
1731     }
1732   return sz;
1733 }
1734
1735 static
1736 tree
1737 bot_manip (t)
1738      tree t;
1739 {
1740   if (TREE_CODE (t) != TREE_LIST && ! TREE_SIDE_EFFECTS (t))
1741     return t;
1742   else if (TREE_CODE (t) == TARGET_EXPR)
1743     return build_cplus_new (TREE_TYPE (t),
1744                             break_out_target_exprs (TREE_OPERAND (t, 1)));
1745   return NULL_TREE;
1746 }
1747   
1748 /* Actually, we'll just clean out the target exprs for the moment.  */
1749 tree
1750 break_out_target_exprs (t)
1751      tree t;
1752 {
1753   return mapcar (t, bot_manip);
1754 }
1755
1756 tree
1757 unsave_expr (expr)
1758      tree expr;
1759 {
1760   tree t;
1761
1762   t = build1 (UNSAVE_EXPR, TREE_TYPE (expr), expr);
1763   TREE_SIDE_EFFECTS (t) = TREE_SIDE_EFFECTS (expr);
1764   return t;
1765 }
1766
1767 /* Modify a tree in place so that all the evaluate only once things
1768    are cleared out.  Return the EXPR given.  */
1769 tree
1770 unsave_expr_now (expr)
1771      tree expr;
1772 {
1773   enum tree_code code;
1774   register int i;
1775
1776   if (expr == NULL_TREE)
1777     return expr;
1778
1779   code = TREE_CODE (expr);
1780   switch (code)
1781     {
1782     case SAVE_EXPR:
1783       SAVE_EXPR_RTL (expr) = NULL_RTX;
1784       break;
1785
1786     case TARGET_EXPR:
1787       sorry ("TARGET_EXPR reused inside UNSAVE_EXPR");
1788       break;
1789       
1790     case RTL_EXPR:
1791       warning ("RTL_EXPR reused inside UNSAVE_EXPR");
1792       RTL_EXPR_SEQUENCE (expr) = NULL_RTX;
1793       break;
1794
1795     case CALL_EXPR:
1796       CALL_EXPR_RTL (expr) = NULL_RTX;
1797       if (TREE_OPERAND (expr, 1)
1798           && TREE_CODE (TREE_OPERAND (expr, 1)) == TREE_LIST)
1799         {
1800           tree exp = TREE_OPERAND (expr, 1);
1801           while (exp)
1802             {
1803               unsave_expr_now (TREE_VALUE (exp));
1804               exp = TREE_CHAIN (exp);
1805             }
1806         }
1807       break;
1808     }
1809
1810   switch (TREE_CODE_CLASS (code))
1811     {
1812     case 'c':  /* a constant */
1813     case 't':  /* a type node */
1814     case 'x':  /* something random, like an identifier or an ERROR_MARK.  */
1815     case 'd':  /* A decl node */
1816     case 'b':  /* A block node */
1817       return expr;
1818
1819     case 'e':  /* an expression */
1820     case 'r':  /* a reference */
1821     case 's':  /* an expression with side effects */
1822     case '<':  /* a comparison expression */
1823     case '2':  /* a binary arithmetic expression */
1824     case '1':  /* a unary arithmetic expression */
1825       for (i = tree_code_length[(int) code] - 1; i >= 0; i--)
1826         unsave_expr_now (TREE_OPERAND (expr, i));
1827       return expr;
1828
1829     default:
1830       my_friendly_abort (999);
1831     }
1832 }
1833
1834 /* Since cleanup may have SAVE_EXPRs in it, we protect it with an
1835    UNSAVE_EXPR as the backend cannot yet handle SAVE_EXPRs in cleanups
1836    by itself.  */
1837 int
1838 cp_expand_decl_cleanup (decl, cleanup)
1839      tree decl, cleanup;
1840 {
1841   return expand_decl_cleanup (decl, unsave_expr (cleanup));
1842 }
1843
1844 /* Obstack used for allocating nodes in template function and variable
1845    definitions.  */
1846
1847 extern struct obstack *expression_obstack;
1848
1849 /* Similar to `build_nt', except we build
1850    on the permanent_obstack, regardless.  */
1851
1852 tree
1853 build_min_nt VPROTO((enum tree_code code, ...))
1854 {
1855 #ifndef __STDC__
1856   enum tree_code code;
1857 #endif
1858   register struct obstack *ambient_obstack = expression_obstack;
1859   va_list p;
1860   register tree t;
1861   register int length;
1862   register int i;
1863
1864   VA_START (p, code);
1865
1866 #ifndef __STDC__
1867   code = va_arg (p, enum tree_code);
1868 #endif
1869
1870   expression_obstack = &permanent_obstack;
1871
1872   t = make_node (code);
1873   length = tree_code_length[(int) code];
1874   TREE_COMPLEXITY (t) = lineno;
1875
1876   for (i = 0; i < length; i++)
1877     {
1878       tree x = va_arg (p, tree);
1879       TREE_OPERAND (t, i) = copy_to_permanent (x);
1880     }
1881
1882   va_end (p);
1883   expression_obstack = ambient_obstack;
1884   return t;
1885 }
1886
1887 /* Similar to `build', except we build
1888    on the permanent_obstack, regardless.  */
1889
1890 tree
1891 build_min VPROTO((enum tree_code code, tree tt, ...))
1892 {
1893 #ifndef __STDC__
1894   enum tree_code code;
1895   tree tt;
1896 #endif
1897   register struct obstack *ambient_obstack = expression_obstack;
1898   va_list p;
1899   register tree t;
1900   register int length;
1901   register int i;
1902
1903   VA_START (p, tt);
1904
1905 #ifndef __STDC__
1906   code = va_arg (p, enum tree_code);
1907   tt = va_arg (p, tree);
1908 #endif
1909
1910   expression_obstack = &permanent_obstack;
1911
1912   t = make_node (code);
1913   length = tree_code_length[(int) code];
1914   TREE_TYPE (t) = tt;
1915   TREE_COMPLEXITY (t) = lineno;
1916
1917   for (i = 0; i < length; i++)
1918     {
1919       tree x = va_arg (p, tree);
1920       TREE_OPERAND (t, i) = copy_to_permanent (x);
1921     }
1922
1923   va_end (p);
1924   expression_obstack = ambient_obstack;
1925   return t;
1926 }
1927
1928 /* Same as `tree_cons' but make a permanent object.  */
1929
1930 tree
1931 min_tree_cons (purpose, value, chain)
1932      tree purpose, value, chain;
1933 {
1934   register tree node;
1935   register struct obstack *ambient_obstack = current_obstack;
1936   current_obstack = &permanent_obstack;
1937
1938   node = tree_cons (purpose, value, chain);
1939   current_obstack = ambient_obstack;
1940   return node;
1941 }
1942
1943 tree
1944 get_type_decl (t)
1945      tree t;
1946 {
1947   if (TREE_CODE (t) == IDENTIFIER_NODE)
1948     return identifier_typedecl_value (t);
1949   if (TREE_CODE (t) == TYPE_DECL)
1950     return t;
1951   if (TREE_CODE_CLASS (TREE_CODE (t)) == 't')
1952     return TYPE_STUB_DECL (t);
1953   
1954   my_friendly_abort (42);
1955 }
1956
1957 int
1958 can_free (obstack, t)
1959      struct obstack *obstack;
1960      tree t;
1961 {
1962   int size;
1963
1964   if (TREE_CODE (t) == TREE_VEC)
1965     size = (TREE_VEC_LENGTH (t)-1) * sizeof (tree) + sizeof (struct tree_vec);
1966   else
1967     my_friendly_abort (42);
1968
1969 #define ROUND(x) ((x + obstack_alignment_mask (obstack)) \
1970                   & ~ obstack_alignment_mask (obstack))
1971   if ((char *)t + ROUND (size) == obstack_next_free (obstack))
1972     return 1;
1973 #undef ROUND
1974
1975   return 0;
1976 }
1977
1978 /* Return first vector element whose BINFO_TYPE is ELEM.
1979    Return 0 if ELEM is not in VEC.  */
1980
1981 tree
1982 vec_binfo_member (elem, vec)
1983      tree elem, vec;
1984 {
1985   int i;
1986   for (i = 0; i < TREE_VEC_LENGTH (vec); ++i)
1987     if (elem == BINFO_TYPE (TREE_VEC_ELT (vec, i)))
1988       return TREE_VEC_ELT (vec, i);
1989   return NULL_TREE;
1990 }