OSDN Git Service

* call.c (build_conditional_expr): Use build_target_expr_with_type.
[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-98, 1999 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 "system.h"
24 #include "obstack.h"
25 #include "tree.h"
26 #include "cp-tree.h"
27 #include "flags.h"
28 #include "rtl.h"
29 #include "toplev.h"
30 #include "ggc.h"
31 #include "splay-tree.h"
32
33 static tree bot_manip PROTO((tree *, int *, void *));
34 static tree bot_replace PROTO((tree *, int *, void *));
35 static tree build_cplus_array_type_1 PROTO((tree, tree));
36 static void list_hash_add PROTO((int, tree));
37 static int list_hash PROTO((tree, tree, tree));
38 static tree list_hash_lookup PROTO((int, tree, tree, tree));
39 static void propagate_binfo_offsets PROTO((tree, tree));
40 static int avoid_overlap PROTO((tree, tree));
41 static cp_lvalue_kind lvalue_p_1 PROTO((tree, int));
42 static tree no_linkage_helper PROTO((tree *, int *, void *));
43 static tree build_srcloc PROTO((char *, int));
44 static void mark_list_hash PROTO ((void *));
45 static tree copy_tree_r PROTO ((tree *, int *, void *));
46 static tree build_target_expr PROTO((tree, tree));
47
48 #define CEIL(x,y) (((x) + (y) - 1) / (y))
49
50 /* If REF is an lvalue, returns the kind of lvalue that REF is.
51    Otherwise, returns clk_none.  If TREAT_CLASS_RVALUES_AS_LVALUES is
52    non-zero, rvalues of class type are considered lvalues.  */
53
54 static cp_lvalue_kind
55 lvalue_p_1 (ref, treat_class_rvalues_as_lvalues)
56      tree ref;
57      int treat_class_rvalues_as_lvalues;
58 {
59   cp_lvalue_kind op1_lvalue_kind = clk_none;
60   cp_lvalue_kind op2_lvalue_kind = clk_none;
61
62   if (TREE_CODE (TREE_TYPE (ref)) == REFERENCE_TYPE)
63     return clk_ordinary;
64
65   if (ref == current_class_ptr && flag_this_is_variable <= 0)
66     return clk_none;
67
68   switch (TREE_CODE (ref))
69     {
70       /* preincrements and predecrements are valid lvals, provided
71          what they refer to are valid lvals.  */
72     case PREINCREMENT_EXPR:
73     case PREDECREMENT_EXPR:
74     case SAVE_EXPR:
75     case UNSAVE_EXPR:
76     case TRY_CATCH_EXPR:
77     case WITH_CLEANUP_EXPR:
78     case REALPART_EXPR:
79     case IMAGPART_EXPR:
80     case NOP_EXPR:
81       return lvalue_p_1 (TREE_OPERAND (ref, 0),
82                          treat_class_rvalues_as_lvalues);
83
84     case COMPONENT_REF:
85       op1_lvalue_kind = lvalue_p_1 (TREE_OPERAND (ref, 0),
86                                     treat_class_rvalues_as_lvalues);
87       if (op1_lvalue_kind 
88           /* The "field" can be a FUNCTION_DECL or an OVERLOAD in some
89              situations.  */
90           && TREE_CODE (TREE_OPERAND (ref, 1)) == FIELD_DECL
91           && DECL_C_BIT_FIELD (TREE_OPERAND (ref, 1)))
92         {
93           /* Clear the ordinary bit.  If this object was a class
94              rvalue we want to preserve that information.  */
95           op1_lvalue_kind &= ~clk_ordinary;
96           /* The lvalue is for a btifield.  */
97           op1_lvalue_kind |= clk_bitfield;
98         }
99       return op1_lvalue_kind;
100
101     case STRING_CST:
102       return clk_ordinary;
103
104     case VAR_DECL:
105       if (TREE_READONLY (ref) && ! TREE_STATIC (ref)
106           && DECL_LANG_SPECIFIC (ref)
107           && DECL_IN_AGGR_P (ref))
108         return clk_none;
109     case INDIRECT_REF:
110     case ARRAY_REF:
111     case PARM_DECL:
112     case RESULT_DECL:
113       if (TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE)
114         return clk_ordinary;
115       break;
116
117       /* A currently unresolved scope ref.  */
118     case SCOPE_REF:
119       my_friendly_abort (103);
120     case OFFSET_REF:
121       if (TREE_CODE (TREE_OPERAND (ref, 1)) == FUNCTION_DECL)
122         return clk_ordinary;
123       /* Fall through.  */
124     case MAX_EXPR:
125     case MIN_EXPR:
126       op1_lvalue_kind = lvalue_p_1 (TREE_OPERAND (ref, 0),
127                                     treat_class_rvalues_as_lvalues);
128       op2_lvalue_kind = lvalue_p_1 (TREE_OPERAND (ref, 1),
129                                     treat_class_rvalues_as_lvalues);
130       break;
131
132     case COND_EXPR:
133       op1_lvalue_kind = lvalue_p_1 (TREE_OPERAND (ref, 1),
134                                     treat_class_rvalues_as_lvalues);
135       op2_lvalue_kind = lvalue_p_1 (TREE_OPERAND (ref, 2),
136                                     treat_class_rvalues_as_lvalues);
137       break;
138
139     case MODIFY_EXPR:
140       return clk_ordinary;
141
142     case COMPOUND_EXPR:
143       return lvalue_p_1 (TREE_OPERAND (ref, 1),
144                          treat_class_rvalues_as_lvalues);
145
146     case TARGET_EXPR:
147       return treat_class_rvalues_as_lvalues ? clk_class : clk_none;
148
149     case CALL_EXPR:
150     case VA_ARG_EXPR:
151       return ((treat_class_rvalues_as_lvalues
152                && IS_AGGR_TYPE (TREE_TYPE (ref)))
153               ? clk_class : clk_none);
154
155     case FUNCTION_DECL:
156       /* All functions (except non-static-member functions) are
157          lvalues.  */
158       return (DECL_NONSTATIC_MEMBER_FUNCTION_P (ref) 
159               ? clk_none : clk_ordinary);
160
161     default:
162       break;
163     }
164
165   /* If one operand is not an lvalue at all, then this expression is
166      not an lvalue.  */
167   if (!op1_lvalue_kind || !op2_lvalue_kind)
168     return clk_none;
169
170   /* Otherwise, it's an lvalue, and it has all the odd properties
171      contributed by either operand.  */
172   op1_lvalue_kind = op1_lvalue_kind | op2_lvalue_kind;
173   /* It's not an ordinary lvalue if it involves either a bit-field or
174      a class rvalue.  */
175   if ((op1_lvalue_kind & ~clk_ordinary) != clk_none)
176     op1_lvalue_kind &= ~clk_ordinary;
177   return op1_lvalue_kind;
178 }
179
180 /* If REF is an lvalue, returns the kind of lvalue that REF is.
181    Otherwise, returns clk_none.  Lvalues can be assigned, unless they
182    have TREE_READONLY, or unless they are FUNCTION_DECLs.  Lvalues can
183    have their address taken, unless they have DECL_REGISTER.  */
184
185 cp_lvalue_kind
186 real_lvalue_p (ref)
187      tree ref;
188 {
189   return lvalue_p_1 (ref, /*treat_class_rvalues_as_lvalues=*/0);
190 }
191
192 /* This differs from real_lvalue_p in that class rvalues are
193    considered lvalues.  */
194
195 int
196 lvalue_p (ref)
197      tree ref;
198 {
199   return 
200     (lvalue_p_1 (ref, /*treat_class_rvalues_as_lvalues=*/1) != clk_none);
201 }
202
203 /* Return nonzero if REF is an lvalue valid for this language;
204    otherwise, print an error message and return zero.  */
205
206 int
207 lvalue_or_else (ref, string)
208      tree ref;
209      const char *string;
210 {
211   int win = lvalue_p (ref);
212   if (! win)
213     error ("non-lvalue in %s", string);
214   return win;
215 }
216
217 /* Build a TARGET_EXPR, initializing the DECL with the VALUE.  */
218
219 static tree
220 build_target_expr (decl, value)
221      tree decl;
222      tree value;
223 {
224   tree t;
225
226   t = build (TARGET_EXPR, TREE_TYPE (decl), decl, value, 
227              maybe_build_cleanup (decl), NULL_TREE);
228   /* We always set TREE_SIDE_EFFECTS so that expand_expr does not
229      ignore the TARGET_EXPR.  If there really turn out to be no
230      side-effects, then the optimizer should be able to get rid of
231      whatever code is generated anyhow.  */
232   TREE_SIDE_EFFECTS (t) = 1;
233
234   return t;
235 }
236
237 /* INIT is a CALL_EXPR which needs info about its target.
238    TYPE is the type that this initialization should appear to have.
239
240    Build an encapsulation of the initialization to perform
241    and return it so that it can be processed by language-independent
242    and language-specific expression expanders.  */
243
244 tree
245 build_cplus_new (type, init)
246      tree type;
247      tree init;
248 {
249   tree fn;
250   tree slot;
251   tree rval;
252
253   /* Make sure that we're not trying to create an instance of an
254      abstract class.  */
255   abstract_virtuals_error (NULL_TREE, type);
256
257   if (TREE_CODE (init) != CALL_EXPR && TREE_CODE (init) != AGGR_INIT_EXPR)
258     return convert (type, init);
259
260   slot = build (VAR_DECL, type);
261   DECL_ARTIFICIAL (slot) = 1;
262   layout_decl (slot, 0);
263
264   /* We split the CALL_EXPR into its function and its arguments here.
265      Then, in expand_expr, we put them back together.  The reason for
266      this is that this expression might be a default argument
267      expression.  In that case, we need a new temporary every time the
268      expression is used.  That's what break_out_target_exprs does; it
269      replaces every AGGR_INIT_EXPR with a copy that uses a fresh
270      temporary slot.  Then, expand_expr builds up a call-expression
271      using the new slot.  */
272   fn = TREE_OPERAND (init, 0);
273   rval = build (AGGR_INIT_EXPR, type, fn, TREE_OPERAND (init, 1), slot);
274   TREE_SIDE_EFFECTS (rval) = 1;
275   AGGR_INIT_VIA_CTOR_P (rval) 
276     = (TREE_CODE (fn) == ADDR_EXPR
277        && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
278        && DECL_CONSTRUCTOR_P (TREE_OPERAND (fn, 0)));
279   rval = build_target_expr (slot, rval);
280
281   return rval;
282 }
283
284 /* Buidl a TARGET_EXPR using INIT to initialize a new temporary of the
285    indicated TYPE.  */
286
287 tree
288 build_target_expr_with_type (init, type)
289      tree init;
290      tree type;
291 {
292   tree slot;
293   tree rval;
294
295   slot = build (VAR_DECL, type);
296   DECL_ARTIFICIAL (slot) = 1;
297   DECL_CONTEXT (slot) = current_function_decl;
298   layout_decl (slot, 0);
299   rval = build_target_expr (slot, init);
300
301   return rval;
302 }
303
304 /* Like build_target_expr_with_type, but use the type of INIT.  */
305
306 tree
307 get_target_expr (init)
308      tree init;
309 {
310   return build_target_expr_with_type (init, TREE_TYPE (init));
311 }
312
313 /* Recursively search EXP for CALL_EXPRs that need cleanups and replace
314    these CALL_EXPRs with tree nodes that will perform the cleanups.  */
315
316 tree
317 break_out_cleanups (exp)
318      tree exp;
319 {
320   tree tmp = exp;
321
322   if (TREE_CODE (tmp) == CALL_EXPR
323       && TYPE_NEEDS_DESTRUCTOR (TREE_TYPE (tmp)))
324     return build_cplus_new (TREE_TYPE (tmp), tmp);
325
326   while (TREE_CODE (tmp) == NOP_EXPR
327          || TREE_CODE (tmp) == CONVERT_EXPR
328          || TREE_CODE (tmp) == NON_LVALUE_EXPR)
329     {
330       if (TREE_CODE (TREE_OPERAND (tmp, 0)) == CALL_EXPR
331           && TYPE_NEEDS_DESTRUCTOR (TREE_TYPE (TREE_OPERAND (tmp, 0))))
332         {
333           TREE_OPERAND (tmp, 0)
334             = build_cplus_new (TREE_TYPE (TREE_OPERAND (tmp, 0)),
335                                TREE_OPERAND (tmp, 0));
336           break;
337         }
338       else
339         tmp = TREE_OPERAND (tmp, 0);
340     }
341   return exp;
342 }
343
344 /* Recursively perform a preorder search EXP for CALL_EXPRs, making
345    copies where they are found.  Returns a deep copy all nodes transitively
346    containing CALL_EXPRs.  */
347
348 tree
349 break_out_calls (exp)
350      tree exp;
351 {
352   register tree t1, t2 = NULL_TREE;
353   register enum tree_code code;
354   register int changed = 0;
355   register int i;
356
357   if (exp == NULL_TREE)
358     return exp;
359
360   code = TREE_CODE (exp);
361
362   if (code == CALL_EXPR)
363     return copy_node (exp);
364
365   /* Don't try and defeat a save_expr, as it should only be done once.  */
366     if (code == SAVE_EXPR)
367        return exp;
368
369   switch (TREE_CODE_CLASS (code))
370     {
371     default:
372       abort ();
373
374     case 'c':  /* a constant */
375     case 't':  /* a type node */
376     case 'x':  /* something random, like an identifier or an ERROR_MARK.  */
377       return exp;
378
379     case 'd':  /* A decl node */
380 #if 0                               /* This is bogus.  jason 9/21/94 */
381
382       t1 = break_out_calls (DECL_INITIAL (exp));
383       if (t1 != DECL_INITIAL (exp))
384         {
385           exp = copy_node (exp);
386           DECL_INITIAL (exp) = t1;
387         }
388 #endif
389       return exp;
390
391     case 'b':  /* A block node */
392       {
393         /* Don't know how to handle these correctly yet.   Must do a
394            break_out_calls on all DECL_INITIAL values for local variables,
395            and also break_out_calls on all sub-blocks and sub-statements.  */
396         abort ();
397       }
398       return exp;
399
400     case 'e':  /* an expression */
401     case 'r':  /* a reference */
402     case 's':  /* an expression with side effects */
403       for (i = tree_code_length[(int) code] - 1; i >= 0; i--)
404         {
405           t1 = break_out_calls (TREE_OPERAND (exp, i));
406           if (t1 != TREE_OPERAND (exp, i))
407             {
408               exp = copy_node (exp);
409               TREE_OPERAND (exp, i) = t1;
410             }
411         }
412       return exp;
413
414     case '<':  /* a comparison expression */
415     case '2':  /* a binary arithmetic expression */
416       t2 = break_out_calls (TREE_OPERAND (exp, 1));
417       if (t2 != TREE_OPERAND (exp, 1))
418         changed = 1;
419     case '1':  /* a unary arithmetic expression */
420       t1 = break_out_calls (TREE_OPERAND (exp, 0));
421       if (t1 != TREE_OPERAND (exp, 0))
422         changed = 1;
423       if (changed)
424         {
425           if (tree_code_length[(int) code] == 1)
426             return build1 (code, TREE_TYPE (exp), t1);
427           else
428             return build (code, TREE_TYPE (exp), t1, t2);
429         }
430       return exp;
431     }
432
433 }
434 \f
435 extern struct obstack *current_obstack;
436 extern struct obstack permanent_obstack;
437 extern struct obstack *saveable_obstack;
438 extern struct obstack *expression_obstack;
439
440 /* Here is how primitive or already-canonicalized types' hash
441    codes are made.  MUST BE CONSISTENT WITH tree.c !!! */
442 #define TYPE_HASH(TYPE) ((HOST_WIDE_INT) (TYPE) & 0777777)
443
444 /* Construct, lay out and return the type of methods belonging to class
445    BASETYPE and whose arguments are described by ARGTYPES and whose values
446    are described by RETTYPE.  If each type exists already, reuse it.  */
447
448 tree
449 build_cplus_method_type (basetype, rettype, argtypes)
450      tree basetype, rettype, argtypes;
451 {
452   register tree t;
453   tree ptype;
454   int hashcode;
455
456   /* Make a node of the sort we want.  */
457   t = make_node (METHOD_TYPE);
458
459   TYPE_METHOD_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
460   TREE_TYPE (t) = rettype;
461   ptype = build_pointer_type (basetype);
462
463   /* The actual arglist for this function includes a "hidden" argument
464      which is "this".  Put it into the list of argument types.  Make
465      sure that the new argument list is allocated on the same obstack
466      as the type.  */
467   push_obstacks (TYPE_OBSTACK (t), TYPE_OBSTACK (t));
468   argtypes = tree_cons (NULL_TREE, ptype, argtypes);
469   TYPE_ARG_TYPES (t) = argtypes;
470   TREE_SIDE_EFFECTS (argtypes) = 1;  /* Mark first argtype as "artificial".  */
471   pop_obstacks ();
472
473   /* If we already have such a type, use the old one and free this one.
474      Note that it also frees up the above cons cell if found.  */
475   hashcode = TYPE_HASH (basetype) + TYPE_HASH (rettype) +
476     type_hash_list (argtypes);
477
478   t = type_hash_canon (hashcode, t);
479
480   if (TYPE_SIZE (t) == 0)
481     layout_type (t);
482
483   return t;
484 }
485
486 static tree
487 build_cplus_array_type_1 (elt_type, index_type)
488      tree elt_type;
489      tree index_type;
490 {
491   tree t;
492
493   if (elt_type == error_mark_node || index_type == error_mark_node)
494     return error_mark_node;
495
496   if (processing_template_decl 
497       || uses_template_parms (elt_type) 
498       || uses_template_parms (index_type))
499     {
500       t = make_node (ARRAY_TYPE);
501       TREE_TYPE (t) = elt_type;
502       TYPE_DOMAIN (t) = index_type;
503     }
504   else
505     t = build_array_type (elt_type, index_type);
506
507   /* Push these needs up so that initialization takes place
508      more easily.  */
509   TYPE_NEEDS_CONSTRUCTING (t) 
510     = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (elt_type));
511   TYPE_NEEDS_DESTRUCTOR (t) 
512     = TYPE_NEEDS_DESTRUCTOR (TYPE_MAIN_VARIANT (elt_type));
513   return t;
514 }
515
516 tree
517 build_cplus_array_type (elt_type, index_type)
518      tree elt_type;
519      tree index_type;
520 {
521   tree t;
522   int type_quals = CP_TYPE_QUALS (elt_type);
523
524   elt_type = TYPE_MAIN_VARIANT (elt_type);
525
526   t = build_cplus_array_type_1 (elt_type, index_type);
527
528   if (type_quals != TYPE_UNQUALIFIED)
529     t = cp_build_qualified_type (t, type_quals);
530
531   return t;
532 }
533 \f
534 /* Make a variant of TYPE, qualified with the TYPE_QUALS.  Handles
535    arrays correctly.  In particular, if TYPE is an array of T's, and
536    TYPE_QUALS is non-empty, returns an array of qualified T's.  If
537    at attempt is made to qualify a type illegally, and COMPLAIN is
538    non-zero, an error is issued.  If COMPLAIN is zero, error_mark_node
539    is returned.  */
540
541 tree
542 cp_build_qualified_type_real (type, type_quals, complain)
543      tree type;
544      int type_quals;
545      int complain;
546 {
547   tree result;
548
549   if (type == error_mark_node)
550     return type;
551
552   if (type_quals == TYPE_QUALS (type))
553     return type;
554
555   /* A restrict-qualified pointer type must be a pointer (or reference)
556      to object or incomplete type.  */
557   if ((type_quals & TYPE_QUAL_RESTRICT)
558       && TREE_CODE (type) != TEMPLATE_TYPE_PARM
559       && (!POINTER_TYPE_P (type)
560           || TYPE_PTRMEM_P (type)
561           || TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE))
562     {
563       if (complain)
564         cp_error ("`%T' cannot be `restrict'-qualified", type);
565       else
566         return error_mark_node;
567
568       type_quals &= ~TYPE_QUAL_RESTRICT;
569     }
570
571   if (type_quals != TYPE_UNQUALIFIED
572       && TREE_CODE (type) == FUNCTION_TYPE)
573     {
574       if (complain)
575         cp_error ("`%T' cannot be `const'-, `volatile'-, or `restrict'-qualified", type);
576       else
577         return error_mark_node;
578       type_quals = TYPE_UNQUALIFIED;
579     }
580   else if (TREE_CODE (type) == ARRAY_TYPE)
581     {
582       /* In C++, the qualification really applies to the array element
583          type.  Obtain the appropriately qualified element type.  */
584       tree t;
585       tree element_type 
586         = cp_build_qualified_type_real (TREE_TYPE (type), 
587                                         type_quals,
588                                         complain);
589
590       if (element_type == error_mark_node)
591         return error_mark_node;
592
593       /* See if we already have an identically qualified type.  */
594       for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
595         if (CP_TYPE_QUALS (t) == type_quals)
596           break;
597
598       /* If we didn't already have it, create it now.  */
599       if (!t)
600         {
601           /* Make a new array type, just like the old one, but with the
602              appropriately qualified element type.  */
603           t = build_type_copy (type);
604           TREE_TYPE (t) = element_type;
605         }
606
607       /* Even if we already had this variant, we update
608          TYPE_NEEDS_CONSTRUCTING and TYPE_NEEDS_DESTRUCTOR in case
609          they changed since the variant was originally created.  
610          
611          This seems hokey; if there is some way to use a previous
612          variant *without* coming through here,
613          TYPE_NEEDS_CONSTRUCTING will never be updated.  */
614       TYPE_NEEDS_CONSTRUCTING (t) 
615         = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (element_type));
616       TYPE_NEEDS_DESTRUCTOR (t) 
617         = TYPE_NEEDS_DESTRUCTOR (TYPE_MAIN_VARIANT (element_type));
618       return t;
619     }
620   else if (TYPE_PTRMEMFUNC_P (type))
621     {
622       /* For a pointer-to-member type, we can't just return a
623          cv-qualified version of the RECORD_TYPE.  If we do, we
624          haven't change the field that contains the actual pointer to
625          a method, and so TYPE_PTRMEMFUNC_FN_TYPE will be wrong.  */
626       tree t;
627
628       t = TYPE_PTRMEMFUNC_FN_TYPE (type);
629       t = cp_build_qualified_type_real (t, type_quals, complain);
630       return build_ptrmemfunc_type (t);
631     }
632
633   /* Retrieve (or create) the appropriately qualified variant.  */
634   result = build_qualified_type (type, type_quals);
635
636   /* If this was a pointer-to-method type, and we just made a copy,
637      then we need to clear the cached associated
638      pointer-to-member-function type; it is not valid for the new
639      type.  */
640   if (result != type 
641       && TREE_CODE (type) == POINTER_TYPE
642       && TREE_CODE (TREE_TYPE (type)) == METHOD_TYPE)
643     TYPE_SET_PTRMEMFUNC_TYPE (result, NULL_TREE);
644
645   return result;
646 }
647
648 /* Returns the canonical version of TYPE.  In other words, if TYPE is
649    a typedef, returns the underlying type.  The cv-qualification of
650    the type returned matches the type input; they will always be
651    compatible types.  */
652
653 tree
654 canonical_type_variant (t)
655      tree t;
656 {
657   return cp_build_qualified_type (TYPE_MAIN_VARIANT (t), CP_TYPE_QUALS (t));
658 }
659 \f
660 /* Add OFFSET to all base types of T.
661
662    OFFSET, which is a type offset, is number of bytes.
663
664    Note that we don't have to worry about having two paths to the
665    same base type, since this type owns its association list.  */
666
667 static void
668 propagate_binfo_offsets (binfo, offset)
669      tree binfo;
670      tree offset;
671 {
672   tree binfos = BINFO_BASETYPES (binfo);
673   int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
674
675   for (i = 0; i < n_baselinks; /* note increment is done in the loop.  */)
676     {
677       tree base_binfo = TREE_VEC_ELT (binfos, i);
678
679       if (TREE_VIA_VIRTUAL (base_binfo))
680         i += 1;
681       else
682         {
683           int j;
684           tree delta = NULL_TREE;
685
686           for (j = i+1; j < n_baselinks; j++)
687             if (! TREE_VIA_VIRTUAL (TREE_VEC_ELT (binfos, j)))
688               {
689                 /* The next basetype offset must take into account the space
690                    between the classes, not just the size of each class.  */
691                 delta = size_binop (MINUS_EXPR,
692                                     BINFO_OFFSET (TREE_VEC_ELT (binfos, j)),
693                                     BINFO_OFFSET (base_binfo));
694                 break;
695               }
696
697 #if 0
698           if (BINFO_OFFSET_ZEROP (base_binfo))
699             BINFO_OFFSET (base_binfo) = offset;
700           else
701             BINFO_OFFSET (base_binfo)
702               = size_binop (PLUS_EXPR, BINFO_OFFSET (base_binfo), offset);
703 #else
704           BINFO_OFFSET (base_binfo) = offset;
705 #endif
706
707           propagate_binfo_offsets (base_binfo, offset);
708
709           /* Go to our next class that counts for offset propagation.  */
710           i = j;
711           if (i < n_baselinks)
712             offset = size_binop (PLUS_EXPR, offset, delta);
713         }
714     }
715 }
716
717 /* Makes new binfos for the indirect bases under BINFO, and updates
718    BINFO_OFFSET for them and their bases.  */
719
720 void
721 unshare_base_binfos (binfo)
722      tree binfo;
723 {
724   tree binfos = BINFO_BASETYPES (binfo);
725   tree new_binfo;
726   int j;
727
728   if (binfos == NULL_TREE)
729     return;
730
731   /* Now unshare the structure beneath BINFO.  */
732   for (j = TREE_VEC_LENGTH (binfos)-1;
733        j >= 0; j--)
734     {
735       tree base_binfo = TREE_VEC_ELT (binfos, j);
736       new_binfo = TREE_VEC_ELT (binfos, j)
737         = make_binfo (BINFO_OFFSET (base_binfo),
738                       base_binfo,
739                       BINFO_VTABLE (base_binfo),
740                       BINFO_VIRTUALS (base_binfo));
741       TREE_VIA_PUBLIC (new_binfo) = TREE_VIA_PUBLIC (base_binfo);
742       TREE_VIA_PROTECTED (new_binfo) = TREE_VIA_PROTECTED (base_binfo);
743       TREE_VIA_VIRTUAL (new_binfo) = TREE_VIA_VIRTUAL (base_binfo);
744       BINFO_INHERITANCE_CHAIN (new_binfo) = binfo;
745       unshare_base_binfos (new_binfo);
746     }
747 }
748
749 /* Finish the work of layout_record, now taking virtual bases into account.
750    Also compute the actual offsets that our base classes will have.
751    This must be performed after the fields are laid out, since virtual
752    baseclasses must lay down at the end of the record.
753
754    Returns the maximum number of virtual functions any of the
755    baseclasses provide.  */
756
757 int
758 layout_basetypes (rec, max)
759      tree rec;
760      int max;
761 {
762   tree binfos = TYPE_BINFO_BASETYPES (rec);
763   int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
764
765   tree vbase_types;
766
767   unsigned int record_align = MAX (BITS_PER_UNIT, TYPE_ALIGN (rec));
768   unsigned int desired_align;
769
770   /* Record size so far is CONST_SIZE bits, where CONST_SIZE is an integer.  */
771   register unsigned int const_size = 0;
772   unsigned int nonvirtual_const_size;
773
774 #ifdef STRUCTURE_SIZE_BOUNDARY
775   /* Packed structures don't need to have minimum size.  */
776   if (! TYPE_PACKED (rec))
777     record_align = MAX (record_align, STRUCTURE_SIZE_BOUNDARY);
778 #endif
779
780   /* Get all the virtual base types that this type uses.  The
781      TREE_VALUE slot holds the virtual baseclass type.  Note that
782      get_vbase_types makes copies of the virtual base BINFOs, so that
783      the vbase_types are unshared.  */
784   vbase_types = CLASSTYPE_VBASECLASSES (rec);
785
786   my_friendly_assert (TREE_CODE (TYPE_SIZE (rec)) == INTEGER_CST, 19970302);
787   const_size = TREE_INT_CST_LOW (TYPE_SIZE (rec));
788
789   nonvirtual_const_size = const_size;
790
791   while (vbase_types)
792     {
793       tree basetype = BINFO_TYPE (vbase_types);
794       tree offset;
795
796       desired_align = TYPE_ALIGN (basetype);
797       record_align = MAX (record_align, desired_align);
798
799       if (const_size == 0)
800         offset = integer_zero_node;
801       else
802         {
803           /* Give each virtual base type the alignment it wants.  */
804           const_size = CEIL (const_size, desired_align) * desired_align;
805           offset = size_int (CEIL (const_size, BITS_PER_UNIT));
806         }
807
808       if (CLASSTYPE_VSIZE (basetype) > max)
809         max = CLASSTYPE_VSIZE (basetype);
810       BINFO_OFFSET (vbase_types) = offset;
811
812       /* Every virtual baseclass takes a least a UNIT, so that we can
813          take it's address and get something different for each base.  */
814       const_size += MAX (BITS_PER_UNIT,
815                          TREE_INT_CST_LOW (CLASSTYPE_SIZE (basetype)));
816
817       vbase_types = TREE_CHAIN (vbase_types);
818     }
819
820   if (const_size)
821     {
822       /* Because a virtual base might take a single byte above,
823          we have to re-adjust the total size to make sure it is
824          a multiple of the alignment.  */
825       /* Give the whole object the alignment it wants.  */
826       const_size = CEIL (const_size, record_align) * record_align;
827     }
828
829   /* Set the alignment in the complete type.  We don't set CLASSTYPE_ALIGN
830    here, as that is for this class, without any virtual base classes.  */
831   TYPE_ALIGN (rec) = record_align;
832   if (const_size != nonvirtual_const_size)
833     {
834       TYPE_SIZE (rec) = size_int (const_size);
835       TYPE_SIZE_UNIT (rec) = size_binop (FLOOR_DIV_EXPR, TYPE_SIZE (rec),
836                                          size_int (BITS_PER_UNIT));
837     }
838
839   /* Now propagate offset information throughout the lattice.  */
840   for (i = 0; i < n_baseclasses; i++)
841     {
842       register tree base_binfo = TREE_VEC_ELT (binfos, i);
843       register tree basetype = BINFO_TYPE (base_binfo);
844       tree field = TYPE_FIELDS (rec);
845
846       if (TREE_VIA_VIRTUAL (base_binfo))
847         continue;
848
849       my_friendly_assert (TREE_TYPE (field) == basetype, 23897);
850
851       if (get_base_distance (basetype, rec, 0, (tree*)0) == -2)
852         cp_warning ("direct base `%T' inaccessible in `%T' due to ambiguity",
853                     basetype, rec);
854
855       BINFO_OFFSET (base_binfo)
856         = size_int (CEIL (TREE_INT_CST_LOW (DECL_FIELD_BITPOS (field)),
857                           BITS_PER_UNIT));
858       propagate_binfo_offsets (base_binfo, BINFO_OFFSET (base_binfo));
859       TYPE_FIELDS (rec) = TREE_CHAIN (field);
860     }
861
862   for (vbase_types = CLASSTYPE_VBASECLASSES (rec); vbase_types;
863        vbase_types = TREE_CHAIN (vbase_types))
864     {
865       BINFO_INHERITANCE_CHAIN (vbase_types) = TYPE_BINFO (rec);
866       unshare_base_binfos (vbase_types);
867       propagate_binfo_offsets (vbase_types, BINFO_OFFSET (vbase_types));
868
869       if (extra_warnings)
870         {
871           tree basetype = BINFO_TYPE (vbase_types);
872           if (get_base_distance (basetype, rec, 0, (tree*)0) == -2)
873             cp_warning ("virtual base `%T' inaccessible in `%T' due to ambiguity",
874                         basetype, rec);
875         }
876     }
877
878   return max;
879 }
880
881 /* If the empty base field in DECL overlaps with a base of the same type in
882    NEWDECL, which is either another base field or the first data field of
883    the class, pad the base just before NEWDECL and return 1.  Otherwise,
884    return 0.  */
885
886 static int
887 avoid_overlap (decl, newdecl)
888      tree decl, newdecl;
889 {
890   tree field;
891
892   if (newdecl == NULL_TREE
893       || ! types_overlap_p (TREE_TYPE (decl), TREE_TYPE (newdecl)))
894     return 0;
895
896   for (field = decl; TREE_CHAIN (field) && TREE_CHAIN (field) != newdecl;
897        field = TREE_CHAIN (field))
898     ;
899
900   DECL_SIZE (field) = integer_one_node;
901
902   return 1;
903 }
904
905 /* Returns a list of fields to stand in for the base class subobjects
906    of REC.  These fields are later removed by layout_basetypes.  */
907
908 tree
909 build_base_fields (rec)
910      tree rec;
911 {
912   /* Chain to hold all the new FIELD_DECLs which stand in for base class
913      subobjects.  */
914   tree base_decls = NULL_TREE;
915   tree binfos = TYPE_BINFO_BASETYPES (rec);
916   int n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
917   tree decl, nextdecl;
918   int i, saw_empty = 0;
919   unsigned int base_align = 0;
920
921   for (i = 0; i < n_baseclasses; ++i)
922     {
923       register tree base_binfo = TREE_VEC_ELT (binfos, i);
924       register tree basetype = BINFO_TYPE (base_binfo);
925
926       if (TYPE_SIZE (basetype) == 0)
927         /* This error is now reported in xref_tag, thus giving better
928            location information.  */
929         continue;
930
931       if (TREE_VIA_VIRTUAL (base_binfo))
932         continue;
933
934       decl = build_lang_decl (FIELD_DECL, NULL_TREE, basetype);
935       DECL_ARTIFICIAL (decl) = 1;
936       DECL_FIELD_CONTEXT (decl) = DECL_CLASS_CONTEXT (decl) = rec;
937       DECL_SIZE (decl) = CLASSTYPE_SIZE (basetype);
938       DECL_ALIGN (decl) = CLASSTYPE_ALIGN (basetype);
939       TREE_CHAIN (decl) = base_decls;
940       base_decls = decl;
941
942       if (! flag_new_abi)
943         {
944           /* Brain damage for backwards compatibility.  For no good reason,
945              the old layout_basetypes made every base at least as large as
946              the alignment for the bases up to that point, gratuitously
947              wasting space.  So we do the same thing here.  */
948           base_align = MAX (base_align, DECL_ALIGN (decl));
949           DECL_SIZE (decl)
950             = size_int (MAX (TREE_INT_CST_LOW (DECL_SIZE (decl)),
951                              (int) base_align));
952         }
953       else if (DECL_SIZE (decl) == integer_zero_node)
954         saw_empty = 1;
955     }
956
957   /* Reverse the list of fields so we allocate the bases in the proper
958      order.  */
959   base_decls = nreverse (base_decls);
960
961   /* In the presence of empty base classes, we run the risk of allocating
962      two objects of the same class on top of one another.  Avoid that.  */
963   if (flag_new_abi && saw_empty)
964     for (decl = base_decls; decl; decl = TREE_CHAIN (decl))
965       {
966         if (DECL_SIZE (decl) == integer_zero_node)
967           {
968             /* First step through the following bases until we find
969                an overlap or a non-empty base.  */
970             for (nextdecl = TREE_CHAIN (decl); nextdecl;
971                  nextdecl = TREE_CHAIN (nextdecl))
972               {
973                 if (avoid_overlap (decl, nextdecl)
974                     || DECL_SIZE (nextdecl) != integer_zero_node)
975                   goto nextbase;
976               }
977
978             /* If we're still looking, also check against the first
979                field.  */
980             for (nextdecl = TYPE_FIELDS (rec);
981                  nextdecl && TREE_CODE (nextdecl) != FIELD_DECL;
982                  nextdecl = TREE_CHAIN (nextdecl))
983               /* keep looking */;
984             avoid_overlap (decl, nextdecl);
985           }
986       nextbase:;
987       }
988
989   return base_decls;
990 }
991
992 /* Returns list of virtual base class pointers in a FIELD_DECL chain.  */
993
994 tree
995 build_vbase_pointer_fields (rec)
996      tree rec;
997 {
998   /* Chain to hold all the new FIELD_DECLs which point at virtual
999      base classes.  */
1000   tree vbase_decls = NULL_TREE;
1001   tree binfos = TYPE_BINFO_BASETYPES (rec);
1002   int n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
1003   tree decl;
1004   int i;
1005
1006   /* Handle basetypes almost like fields, but record their
1007      offsets differently.  */
1008
1009   for (i = 0; i < n_baseclasses; i++)
1010     {
1011       register tree base_binfo = TREE_VEC_ELT (binfos, i);
1012       register tree basetype = BINFO_TYPE (base_binfo);
1013
1014       if (TYPE_SIZE (basetype) == 0)
1015         /* This error is now reported in xref_tag, thus giving better
1016            location information.  */
1017         continue;
1018
1019       /* All basetypes are recorded in the association list of the
1020          derived type.  */
1021
1022       if (TREE_VIA_VIRTUAL (base_binfo))
1023         {
1024           int j;
1025           const char *name;
1026
1027           /* The offset for a virtual base class is only used in computing
1028              virtual function tables and for initializing virtual base
1029              pointers.  It is built once `get_vbase_types' is called.  */
1030
1031           /* If this basetype can come from another vbase pointer
1032              without an additional indirection, we will share
1033              that pointer.  If an indirection is involved, we
1034              make our own pointer.  */
1035           for (j = 0; j < n_baseclasses; j++)
1036             {
1037               tree other_base_binfo = TREE_VEC_ELT (binfos, j);
1038               if (! TREE_VIA_VIRTUAL (other_base_binfo)
1039                   && binfo_member (basetype,
1040                                    CLASSTYPE_VBASECLASSES (BINFO_TYPE
1041                                                            (other_base_binfo))
1042                                    ))
1043                 goto got_it;
1044             }
1045           FORMAT_VBASE_NAME (name, basetype);
1046           decl = build_lang_decl (FIELD_DECL, get_identifier (name),
1047                                   build_pointer_type (basetype));
1048           /* If you change any of the below, take a look at all the
1049              other VFIELD_BASEs and VTABLE_BASEs in the code, and change
1050              them too.  */
1051           DECL_ASSEMBLER_NAME (decl) = get_identifier (VTABLE_BASE);
1052           DECL_VIRTUAL_P (decl) = 1;
1053           DECL_ARTIFICIAL (decl) = 1;
1054           DECL_FIELD_CONTEXT (decl) = rec;
1055           DECL_CLASS_CONTEXT (decl) = rec;
1056           DECL_FCONTEXT (decl) = basetype;
1057           DECL_SAVED_INSNS (decl) = 0;
1058           DECL_FIELD_SIZE (decl) = 0;
1059           DECL_ALIGN (decl) = TYPE_ALIGN (ptr_type_node);
1060           TREE_CHAIN (decl) = vbase_decls;
1061           BINFO_VPTR_FIELD (base_binfo) = decl;
1062           vbase_decls = decl;
1063
1064         got_it:
1065           /* The space this decl occupies has already been accounted for.  */
1066           ;
1067         }
1068     }
1069
1070   return vbase_decls;
1071 }
1072 \f
1073 /* Hashing of lists so that we don't make duplicates.
1074    The entry point is `list_hash_canon'.  */
1075
1076 /* Each hash table slot is a bucket containing a chain
1077    of these structures.  */
1078
1079 struct list_hash
1080 {
1081   struct list_hash *next;       /* Next structure in the bucket.  */
1082   int hashcode;                 /* Hash code of this list.  */
1083   tree list;                    /* The list recorded here.  */
1084 };
1085
1086 /* Now here is the hash table.  When recording a list, it is added
1087    to the slot whose index is the hash code mod the table size.
1088    Note that the hash table is used for several kinds of lists.
1089    While all these live in the same table, they are completely independent,
1090    and the hash code is computed differently for each of these.  */
1091
1092 #define TYPE_HASH_SIZE 59
1093 static struct list_hash *list_hash_table[TYPE_HASH_SIZE];
1094
1095 /* Compute a hash code for a list (chain of TREE_LIST nodes
1096    with goodies in the TREE_PURPOSE, TREE_VALUE, and bits of the
1097    TREE_COMMON slots), by adding the hash codes of the individual entries.  */
1098
1099 static int
1100 list_hash (purpose, value, chain)
1101      tree purpose, value, chain;
1102 {
1103   register int hashcode = 0;
1104
1105   if (chain)
1106     hashcode += TYPE_HASH (chain);
1107
1108   if (value)
1109     hashcode += TYPE_HASH (value);
1110   else
1111     hashcode += 1007;
1112   if (purpose)
1113     hashcode += TYPE_HASH (purpose);
1114   else
1115     hashcode += 1009;
1116   return hashcode;
1117 }
1118
1119 /* Look in the type hash table for a type isomorphic to TYPE.
1120    If one is found, return it.  Otherwise return 0.  */
1121
1122 static tree
1123 list_hash_lookup (hashcode, purpose, value, chain)
1124      int hashcode;
1125      tree purpose, value, chain;
1126 {
1127   register struct list_hash *h;
1128
1129   for (h = list_hash_table[hashcode % TYPE_HASH_SIZE]; h; h = h->next)
1130     if (h->hashcode == hashcode
1131         && TREE_PURPOSE (h->list) == purpose
1132         && TREE_VALUE (h->list) == value
1133         && TREE_CHAIN (h->list) == chain)
1134       return h->list;
1135   return 0;
1136 }
1137
1138 /* Add an entry to the list-hash-table
1139    for a list TYPE whose hash code is HASHCODE.  */
1140
1141 static void
1142 list_hash_add (hashcode, list)
1143      int hashcode;
1144      tree list;
1145 {
1146   register struct list_hash *h;
1147
1148   h = (struct list_hash *) obstack_alloc (&permanent_obstack, sizeof (struct list_hash));
1149   h->hashcode = hashcode;
1150   h->list = list;
1151   h->next = list_hash_table[hashcode % TYPE_HASH_SIZE];
1152   list_hash_table[hashcode % TYPE_HASH_SIZE] = h;
1153 }
1154
1155 /* Given list components PURPOSE, VALUE, AND CHAIN, return the canonical
1156    object for an identical list if one already exists.  Otherwise, build a
1157    new one, and record it as the canonical object.  */
1158
1159 /* Set to 1 to debug without canonicalization.  Never set by program.  */
1160
1161 static int debug_no_list_hash = 0;
1162
1163 tree
1164 hash_tree_cons (purpose, value, chain)
1165      tree purpose, value, chain;
1166 {
1167   tree t;
1168   int hashcode = 0;
1169
1170   if (! debug_no_list_hash)
1171     {
1172       hashcode = list_hash (purpose, value, chain);
1173       t = list_hash_lookup (hashcode, purpose, value, chain);
1174       if (t)
1175         return t;
1176     }
1177
1178   t = tree_cons (purpose, value, chain);
1179
1180   /* If this is a new list, record it for later reuse.  */
1181   if (! debug_no_list_hash)
1182     list_hash_add (hashcode, t);
1183
1184   return t;
1185 }
1186
1187 /* Constructor for hashed lists.  */
1188
1189 tree
1190 hash_tree_chain (value, chain)
1191      tree value, chain;
1192 {
1193   return hash_tree_cons (NULL_TREE, value, chain);
1194 }
1195
1196 /* Similar, but used for concatenating two lists.  */
1197
1198 tree
1199 hash_chainon (list1, list2)
1200      tree list1, list2;
1201 {
1202   if (list2 == 0)
1203     return list1;
1204   if (list1 == 0)
1205     return list2;
1206   if (TREE_CHAIN (list1) == NULL_TREE)
1207     return hash_tree_chain (TREE_VALUE (list1), list2);
1208   return hash_tree_chain (TREE_VALUE (list1),
1209                           hash_chainon (TREE_CHAIN (list1), list2));
1210 }
1211 \f
1212 /* Build an association between TYPE and some parameters:
1213
1214    OFFSET is the offset added to `this' to convert it to a pointer
1215    of type `TYPE *'
1216
1217    BINFO is the base binfo to use, if we are deriving from one.  This
1218    is necessary, as we want specialized parent binfos from base
1219    classes, so that the VTABLE_NAMEs of bases are for the most derived
1220    type, instead of the simple type.
1221
1222    VTABLE is the virtual function table with which to initialize
1223    sub-objects of type TYPE.
1224
1225    VIRTUALS are the virtual functions sitting in VTABLE.  */
1226
1227 tree
1228 make_binfo (offset, binfo, vtable, virtuals)
1229      tree offset, binfo;
1230      tree vtable, virtuals;
1231 {
1232   tree new_binfo = make_tree_vec (7);
1233   tree type;
1234
1235   if (TREE_CODE (binfo) == TREE_VEC)
1236     type = BINFO_TYPE (binfo);
1237   else
1238     {
1239       type = binfo;
1240       binfo = CLASS_TYPE_P (type) ? TYPE_BINFO (binfo) : NULL_TREE;
1241     }
1242
1243   TREE_TYPE (new_binfo) = TYPE_MAIN_VARIANT (type);
1244   BINFO_OFFSET (new_binfo) = offset;
1245   BINFO_VTABLE (new_binfo) = vtable;
1246   BINFO_VIRTUALS (new_binfo) = virtuals;
1247   BINFO_VPTR_FIELD (new_binfo) = NULL_TREE;
1248
1249   if (binfo && BINFO_BASETYPES (binfo) != NULL_TREE)
1250     BINFO_BASETYPES (new_binfo) = copy_node (BINFO_BASETYPES (binfo));      
1251   return new_binfo;
1252 }
1253
1254 /* Return the binfo value for ELEM in TYPE.  */
1255
1256 tree
1257 binfo_value (elem, type)
1258      tree elem;
1259      tree type;
1260 {
1261   if (get_base_distance (elem, type, 0, (tree *)0) == -2)
1262     compiler_error ("base class `%s' ambiguous in binfo_value",
1263                     TYPE_NAME_STRING (elem));
1264   if (elem == type)
1265     return TYPE_BINFO (type);
1266   if (TREE_CODE (elem) == RECORD_TYPE && TYPE_BINFO (elem) == type)
1267     return type;
1268   return get_binfo (elem, type, 0);
1269 }
1270
1271 /* Return a reversed copy of the BINFO-chain given by PATH.  (If the 
1272    BINFO_INHERITANCE_CHAIN points from base classes to derived
1273    classes, it will instead point from derived classes to base
1274    classes.)  Returns the first node in the reversed chain.  */
1275
1276 tree
1277 reverse_path (path)
1278      tree path;
1279 {
1280   register tree prev = NULL_TREE, cur;
1281   push_expression_obstack ();
1282   for (cur = path; cur; cur = BINFO_INHERITANCE_CHAIN (cur))
1283     {
1284       tree r = copy_node (cur);
1285       BINFO_INHERITANCE_CHAIN (r) = prev;
1286       prev = r;
1287     }
1288   pop_obstacks ();
1289   return prev;
1290 }
1291
1292 void
1293 debug_binfo (elem)
1294      tree elem;
1295 {
1296   unsigned HOST_WIDE_INT n;
1297   tree virtuals;
1298
1299   fprintf (stderr, "type \"%s\"; offset = %ld\n",
1300            TYPE_NAME_STRING (BINFO_TYPE (elem)),
1301            (long) TREE_INT_CST_LOW (BINFO_OFFSET (elem)));
1302   fprintf (stderr, "vtable type:\n");
1303   debug_tree (BINFO_TYPE (elem));
1304   if (BINFO_VTABLE (elem))
1305     fprintf (stderr, "vtable decl \"%s\"\n", IDENTIFIER_POINTER (DECL_NAME (BINFO_VTABLE (elem))));
1306   else
1307     fprintf (stderr, "no vtable decl yet\n");
1308   fprintf (stderr, "virtuals:\n");
1309   virtuals = BINFO_VIRTUALS (elem);
1310
1311   n = skip_rtti_stuff (&virtuals, BINFO_TYPE (elem));
1312
1313   while (virtuals)
1314     {
1315       tree fndecl = TREE_VALUE (virtuals);
1316       fprintf (stderr, "%s [%ld =? %ld]\n",
1317                IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (fndecl)),
1318                (long) n, (long) TREE_INT_CST_LOW (DECL_VINDEX (fndecl)));
1319       ++n;
1320       virtuals = TREE_CHAIN (virtuals);
1321     }
1322 }
1323
1324 int
1325 count_functions (t)
1326      tree t;
1327 {
1328   int i;
1329   if (TREE_CODE (t) == FUNCTION_DECL)
1330     return 1;
1331   else if (TREE_CODE (t) == OVERLOAD)
1332     {
1333       for (i=0; t; t = OVL_CHAIN (t))
1334         i++;
1335       return i;
1336     }
1337
1338   my_friendly_abort (359);
1339   return 0;
1340 }
1341
1342 int
1343 is_overloaded_fn (x)
1344      tree x;
1345 {
1346   /* A baselink is also considered an overloaded function.  */
1347   if (TREE_CODE (x) == OFFSET_REF)
1348     x = TREE_OPERAND (x, 1);
1349   if (BASELINK_P (x))
1350     x = TREE_VALUE (x);
1351   return (TREE_CODE (x) == FUNCTION_DECL
1352           || TREE_CODE (x) == TEMPLATE_ID_EXPR
1353           || DECL_FUNCTION_TEMPLATE_P (x)
1354           || TREE_CODE (x) == OVERLOAD);
1355 }
1356
1357 int
1358 really_overloaded_fn (x)
1359      tree x;
1360 {     
1361   /* A baselink is also considered an overloaded function.  */
1362   if (TREE_CODE (x) == OFFSET_REF)
1363     x = TREE_OPERAND (x, 1);
1364   if (BASELINK_P (x))
1365     x = TREE_VALUE (x);
1366   return (TREE_CODE (x) == OVERLOAD 
1367           && (TREE_CHAIN (x) != NULL_TREE
1368               || DECL_FUNCTION_TEMPLATE_P (OVL_FUNCTION (x))));
1369 }
1370
1371 tree
1372 get_first_fn (from)
1373      tree from;
1374 {
1375   my_friendly_assert (is_overloaded_fn (from), 9);
1376   /* A baselink is also considered an overloaded function. */
1377   if (BASELINK_P (from))
1378     from = TREE_VALUE (from);
1379   return OVL_CURRENT (from);
1380 }
1381
1382 /* Returns nonzero if T is a ->* or .* expression that refers to a
1383    member function.  */
1384
1385 int
1386 bound_pmf_p (t)
1387      tree t;
1388 {
1389   return (TREE_CODE (t) == OFFSET_REF
1390           && TYPE_PTRMEMFUNC_P (TREE_TYPE (TREE_OPERAND (t, 1))));
1391 }
1392
1393 /* Return a new OVL node, concatenating it with the old one. */
1394
1395 tree
1396 ovl_cons (decl, chain)
1397      tree decl;
1398      tree chain;
1399 {
1400   tree result = make_node (OVERLOAD);
1401   TREE_TYPE (result) = unknown_type_node;
1402   OVL_FUNCTION (result) = decl;
1403   TREE_CHAIN (result) = chain;
1404   
1405   return result;
1406 }
1407
1408 /* Same as ovl_cons, but on the scratch_obstack. */
1409
1410 tree
1411 scratch_ovl_cons (value, chain)
1412      tree value, chain;
1413 {
1414   register tree node;
1415   register struct obstack *ambient_obstack = current_obstack;
1416   extern struct obstack *expression_obstack;
1417   current_obstack = expression_obstack;
1418   node = ovl_cons (value, chain);
1419   current_obstack = ambient_obstack;
1420   return node;
1421 }
1422
1423 /* Build a new overloaded function. If this is the first one,
1424    just return it; otherwise, ovl_cons the _DECLs */
1425
1426 tree
1427 build_overload (decl, chain)
1428      tree decl;
1429      tree chain;
1430 {
1431   if (! chain && TREE_CODE (decl) != TEMPLATE_DECL)
1432     return decl;
1433   if (chain && TREE_CODE (chain) != OVERLOAD)
1434     chain = ovl_cons (chain, NULL_TREE);
1435   return ovl_cons (decl, chain);
1436 }
1437
1438 /* True if fn is in ovl. */
1439
1440 int
1441 ovl_member (fn, ovl)
1442      tree fn;
1443      tree ovl;
1444 {
1445   if (ovl == NULL_TREE)
1446     return 0;
1447   if (TREE_CODE (ovl) != OVERLOAD)
1448     return ovl == fn;
1449   for (; ovl; ovl = OVL_CHAIN (ovl))
1450     if (OVL_FUNCTION (ovl) == fn)
1451       return 1;
1452   return 0;
1453 }
1454
1455 int
1456 is_aggr_type_2 (t1, t2)
1457      tree t1, t2;
1458 {
1459   if (TREE_CODE (t1) != TREE_CODE (t2))
1460     return 0;
1461   return IS_AGGR_TYPE (t1) && IS_AGGR_TYPE (t2);
1462 }
1463 \f
1464 #define PRINT_RING_SIZE 4
1465
1466 const char *
1467 lang_printable_name (decl, v)
1468      tree decl;
1469      int v;
1470 {
1471   static tree decl_ring[PRINT_RING_SIZE];
1472   static char *print_ring[PRINT_RING_SIZE];
1473   static int ring_counter;
1474   int i;
1475
1476   /* Only cache functions.  */
1477   if (v < 2
1478       || TREE_CODE (decl) != FUNCTION_DECL
1479       || DECL_LANG_SPECIFIC (decl) == 0)
1480     return lang_decl_name (decl, v);
1481
1482   /* See if this print name is lying around.  */
1483   for (i = 0; i < PRINT_RING_SIZE; i++)
1484     if (decl_ring[i] == decl)
1485       /* yes, so return it.  */
1486       return print_ring[i];
1487
1488   if (++ring_counter == PRINT_RING_SIZE)
1489     ring_counter = 0;
1490
1491   if (current_function_decl != NULL_TREE)
1492     {
1493       if (decl_ring[ring_counter] == current_function_decl)
1494         ring_counter += 1;
1495       if (ring_counter == PRINT_RING_SIZE)
1496         ring_counter = 0;
1497       if (decl_ring[ring_counter] == current_function_decl)
1498         my_friendly_abort (106);
1499     }
1500
1501   if (print_ring[ring_counter])
1502     free (print_ring[ring_counter]);
1503
1504   print_ring[ring_counter] = xstrdup (lang_decl_name (decl, v));
1505   decl_ring[ring_counter] = decl;
1506   return print_ring[ring_counter];
1507 }
1508 \f
1509 /* Build the FUNCTION_TYPE or METHOD_TYPE which may throw exceptions
1510    listed in RAISES.  */
1511
1512 tree
1513 build_exception_variant (type, raises)
1514      tree type;
1515      tree raises;
1516 {
1517   tree v = TYPE_MAIN_VARIANT (type);
1518   int type_quals = TYPE_QUALS (type);
1519
1520   for (; v; v = TYPE_NEXT_VARIANT (v))
1521     if (TYPE_QUALS (v) == type_quals
1522         && comp_except_specs (raises, TYPE_RAISES_EXCEPTIONS (v), 1))
1523       return v;
1524
1525   /* Need to build a new variant.  */
1526   v = build_type_copy (type);
1527   TYPE_RAISES_EXCEPTIONS (v) = raises;
1528   return v;
1529 }
1530
1531 /* Given a TEMPLATE_TEMPLATE_PARM node T, create a new one together with its 
1532    lang_specific field and its corresponding TEMPLATE_DECL node */
1533
1534 tree
1535 copy_template_template_parm (t)
1536      tree t;
1537 {
1538   tree template = TYPE_NAME (t);
1539   tree t2;
1540
1541   /* Make sure these end up on the permanent_obstack.  */
1542   push_permanent_obstack ();
1543   
1544   t2 = make_lang_type (TEMPLATE_TEMPLATE_PARM);
1545   template = copy_node (template);
1546   copy_lang_decl (template);
1547
1548   pop_obstacks ();
1549
1550   TREE_TYPE (template) = t2;
1551   TYPE_NAME (t2) = template;
1552   TYPE_STUB_DECL (t2) = template;
1553
1554   /* No need to copy these */
1555   TYPE_FIELDS (t2) = TYPE_FIELDS (t);
1556   TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t2) 
1557     = TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t);
1558   return t2;
1559 }
1560
1561 /* Apply FUNC to all the sub-trees of TP in a pre-order traversal.
1562    FUNC is called with the DATA and the address of each sub-tree.  If
1563    FUNC returns a non-NULL value, the traversal is aborted, and the
1564    value returned by FUNC is returned.  */
1565
1566 tree 
1567 walk_tree (tp, func, data)
1568      tree *tp;
1569      walk_tree_fn func;
1570      void *data;
1571 {
1572   enum tree_code code;
1573   int walk_subtrees;
1574   tree result;
1575   
1576 #define WALK_SUBTREE(NODE)                      \
1577   do                                            \
1578     {                                           \
1579       result = walk_tree (&(NODE), func, data); \
1580       if (result)                               \
1581         return result;                          \
1582     }                                           \
1583   while (0)
1584
1585   /* Skip empty subtrees.  */
1586   if (!*tp)
1587     return NULL_TREE;
1588
1589   /* Call the function.  */
1590   walk_subtrees = 1;
1591   result = (*func) (tp, &walk_subtrees, data);
1592
1593   /* If we found something, return it.  */
1594   if (result)
1595     return result;
1596
1597   /* Even if we didn't, FUNC may have decided that there was nothing
1598      interesting below this point in the tree.  */
1599   if (!walk_subtrees)
1600     return NULL_TREE;
1601
1602   code = TREE_CODE (*tp);
1603
1604   /* Handle commmon cases up front.  */
1605   if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code))
1606       || TREE_CODE_CLASS (code) == 'r')
1607     {
1608       int i;
1609
1610       /* Walk over all the sub-trees of this operand.  */
1611       for (i = first_rtl_op (code) - 1; i >= 0; --i)
1612         WALK_SUBTREE (TREE_OPERAND (*tp, i));
1613
1614       /* We didn't find what we were looking for.  */
1615       return NULL_TREE;
1616     }
1617   else if (TREE_CODE_CLASS (code) == 'd')
1618     {
1619       WALK_SUBTREE (TREE_TYPE (*tp));
1620       WALK_SUBTREE (DECL_INITIAL (*tp));
1621       WALK_SUBTREE (DECL_SIZE (*tp));
1622
1623       /* We didn't find what we were looking for.  */
1624       return NULL_TREE;
1625     }
1626
1627   /* Not one of the easy cases.  We must explicitly go through the
1628      children.  */
1629   switch (code)
1630     {
1631     case ERROR_MARK:
1632     case IDENTIFIER_NODE:
1633     case INTEGER_CST:
1634     case REAL_CST:
1635     case STRING_CST:
1636     case DEFAULT_ARG:
1637     case TEMPLATE_TEMPLATE_PARM:
1638     case TEMPLATE_PARM_INDEX:
1639     case TEMPLATE_TYPE_PARM:
1640     case REAL_TYPE:
1641     case COMPLEX_TYPE:
1642     case VOID_TYPE:
1643     case BOOLEAN_TYPE:
1644     case TYPENAME_TYPE:
1645     case UNION_TYPE:
1646     case ENUMERAL_TYPE:
1647     case TYPEOF_TYPE:
1648     case BLOCK:
1649       /* None of thse have subtrees other than those already walked
1650          above.  */
1651       break;
1652
1653     case PTRMEM_CST:
1654       WALK_SUBTREE (TREE_TYPE (*tp));
1655       break;
1656
1657     case POINTER_TYPE:
1658     case REFERENCE_TYPE:
1659       WALK_SUBTREE (TREE_TYPE (*tp));
1660       break;
1661
1662     case TREE_LIST:
1663       WALK_SUBTREE (TREE_PURPOSE (*tp));
1664       WALK_SUBTREE (TREE_VALUE (*tp));
1665       WALK_SUBTREE (TREE_CHAIN (*tp));
1666       break;
1667
1668     case OVERLOAD:
1669       WALK_SUBTREE (OVL_FUNCTION (*tp));
1670       WALK_SUBTREE (OVL_CHAIN (*tp));
1671       break;
1672
1673     case TREE_VEC:
1674       {
1675         int len = TREE_VEC_LENGTH (*tp);
1676         while (len--)
1677           WALK_SUBTREE (TREE_VEC_ELT (*tp, len));
1678       }
1679       break;
1680
1681     case COMPLEX_CST:
1682       WALK_SUBTREE (TREE_REALPART (*tp));
1683       WALK_SUBTREE (TREE_IMAGPART (*tp));
1684       break;
1685
1686     case CONSTRUCTOR:
1687       WALK_SUBTREE (CONSTRUCTOR_ELTS (*tp));
1688       break;
1689
1690     case METHOD_TYPE:
1691       WALK_SUBTREE (TYPE_METHOD_BASETYPE (*tp));
1692       /* Fall through.  */
1693
1694     case FUNCTION_TYPE:
1695       WALK_SUBTREE (TREE_TYPE (*tp));
1696       WALK_SUBTREE (TYPE_ARG_TYPES (*tp));
1697       break;
1698
1699     case ARRAY_TYPE:
1700       WALK_SUBTREE (TREE_TYPE (*tp));
1701       WALK_SUBTREE (TYPE_DOMAIN (*tp));
1702       break;
1703
1704     case INTEGER_TYPE:
1705       WALK_SUBTREE (TYPE_MIN_VALUE (*tp));
1706       WALK_SUBTREE (TYPE_MAX_VALUE (*tp));
1707       break;
1708
1709     case OFFSET_TYPE:
1710       WALK_SUBTREE (TREE_TYPE (*tp));
1711       WALK_SUBTREE (TYPE_OFFSET_BASETYPE (*tp));
1712       break;
1713
1714     case RECORD_TYPE:
1715       if (TYPE_PTRMEMFUNC_P (*tp))
1716         WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (*tp));
1717       break;
1718       
1719     default:
1720       my_friendly_abort (19990803);
1721     }
1722
1723   /* We didn't find what we were looking for.  */
1724   return NULL_TREE;
1725
1726 #undef WALK_SUBTREE
1727 }
1728
1729 /* Passed to walk_tree.  Checks for the use of types with no linkage.  */
1730
1731 static tree
1732 no_linkage_helper (tp, walk_subtrees, data)
1733      tree *tp;
1734      int *walk_subtrees ATTRIBUTE_UNUSED;
1735      void *data ATTRIBUTE_UNUSED;
1736 {
1737   tree t = *tp;
1738
1739   if (TYPE_P (t)
1740       && (IS_AGGR_TYPE (t) || TREE_CODE (t) == ENUMERAL_TYPE)
1741       && (decl_function_context (TYPE_MAIN_DECL (t))
1742           || ANON_AGGRNAME_P (TYPE_IDENTIFIER (t))))
1743     return t;
1744   return NULL_TREE;
1745 }
1746
1747 /* Check if the type T depends on a type with no linkage and if so, return
1748    it.  */
1749
1750 tree
1751 no_linkage_check (t)
1752      tree t;
1753 {
1754   /* There's no point in checking linkage on template functions; we
1755      can't know their complete types.  */
1756   if (processing_template_decl)
1757     return NULL_TREE;
1758
1759   t = walk_tree (&t, no_linkage_helper, NULL);
1760   if (t != error_mark_node)
1761     return t;
1762   return NULL_TREE;
1763 }
1764
1765 /* Passed to walk_tree.  Copies the node pointed to, if appropriate.  */
1766
1767 static tree
1768 copy_tree_r (tp, walk_subtrees, data)
1769      tree *tp;
1770      int *walk_subtrees ATTRIBUTE_UNUSED;
1771      void *data ATTRIBUTE_UNUSED;
1772 {
1773   enum tree_code code = TREE_CODE (*tp);
1774
1775   /* We make copies of most nodes.  */
1776   if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code))
1777       || TREE_CODE_CLASS (code) == 'r'
1778       || TREE_CODE_CLASS (code) == 'c'
1779       || code == PARM_DECL
1780       || code == TREE_LIST
1781       || code == TREE_VEC
1782       || code == OVERLOAD)
1783     {
1784       /* Because the chain gets clobbered when we make a copy, we save it
1785          here.  */
1786       tree chain = TREE_CHAIN (*tp);
1787
1788       /* Copy the node.  */
1789       *tp = copy_node (*tp);
1790
1791       /* Now, restore the chain, if appropriate.  That will cause
1792          walk_tree to walk into the chain as well.  */
1793       if (code == PARM_DECL || code == TREE_LIST || code == OVERLOAD)
1794         TREE_CHAIN (*tp) = chain;
1795     }
1796   else if (code == TEMPLATE_TEMPLATE_PARM)
1797     /* These must be copied specially.  */
1798     *tp = copy_template_template_parm (*tp);
1799
1800   return NULL_TREE;
1801 }
1802
1803 #ifdef GATHER_STATISTICS
1804 extern int depth_reached;
1805 #endif
1806
1807 void
1808 print_lang_statistics ()
1809 {
1810   print_search_statistics ();
1811   print_class_statistics ();
1812 #ifdef GATHER_STATISTICS
1813   fprintf (stderr, "maximum template instantiation depth reached: %d\n",
1814            depth_reached);
1815 #endif
1816 }
1817
1818 /* This is used by the `assert' macro.  It is provided in libgcc.a,
1819    which `cc' doesn't know how to link.  Note that the C++ front-end
1820    no longer actually uses the `assert' macro (instead, it calls
1821    my_friendly_assert).  But all of the back-end files still need this.  */
1822
1823 void
1824 __eprintf (string, expression, line, filename)
1825      const char *string;
1826      const char *expression;
1827      unsigned line;
1828      const char *filename;
1829 {
1830   fprintf (stderr, string, expression, line, filename);
1831   fflush (stderr);
1832   abort ();
1833 }
1834
1835 /* Return, as an INTEGER_CST node, the number of elements for TYPE
1836    (which is an ARRAY_TYPE).  This counts only elements of the top
1837    array.  */
1838
1839 tree
1840 array_type_nelts_top (type)
1841      tree type;
1842 {
1843   return fold (build (PLUS_EXPR, sizetype,
1844                       array_type_nelts (type),
1845                       integer_one_node));
1846 }
1847
1848 /* Return, as an INTEGER_CST node, the number of elements for TYPE
1849    (which is an ARRAY_TYPE).  This one is a recursive count of all
1850    ARRAY_TYPEs that are clumped together.  */
1851
1852 tree
1853 array_type_nelts_total (type)
1854      tree type;
1855 {
1856   tree sz = array_type_nelts_top (type);
1857   type = TREE_TYPE (type);
1858   while (TREE_CODE (type) == ARRAY_TYPE)
1859     {
1860       tree n = array_type_nelts_top (type);
1861       sz = fold (build (MULT_EXPR, sizetype, sz, n));
1862       type = TREE_TYPE (type);
1863     }
1864   return sz;
1865 }
1866
1867 /* Called from break_out_target_exprs via mapcar.  */
1868
1869 static tree
1870 bot_manip (tp, walk_subtrees, data)
1871      tree *tp;
1872      int *walk_subtrees;
1873      void *data;
1874 {
1875   splay_tree target_remap = ((splay_tree) data);
1876   tree t = *tp;
1877
1878   if (TREE_CODE (t) != TREE_LIST && ! TREE_SIDE_EFFECTS (t))
1879     {
1880       /* There can't be any TARGET_EXPRs below this point.  */
1881       *walk_subtrees = 0;
1882       return NULL_TREE;
1883     }
1884   else if (TREE_CODE (t) == TARGET_EXPR)
1885     {
1886       tree u;
1887
1888       if (TREE_CODE (TREE_OPERAND (t, 1)) == AGGR_INIT_EXPR)
1889         {
1890           mark_used (TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (t, 1), 0), 0));
1891           u = build_cplus_new
1892             (TREE_TYPE (t), break_out_target_exprs (TREE_OPERAND (t, 1)));
1893         }
1894       else 
1895         {
1896           u = copy_node (t);
1897           TREE_OPERAND (u, 0) = build (VAR_DECL, TREE_TYPE (t));
1898           layout_decl (TREE_OPERAND (u, 0), 0);
1899         }
1900
1901       /* Map the old variable to the new one.  */
1902       splay_tree_insert (target_remap, 
1903                          (splay_tree_key) TREE_OPERAND (t, 0), 
1904                          (splay_tree_value) TREE_OPERAND (u, 0));
1905
1906       /* Replace the old expression with the new version.  */
1907       *tp = u;
1908       /* We don't have to go below this point; the recursive call to
1909          break_out_target_exprs will have handled anything below this
1910          point.  */
1911       *walk_subtrees = 0;
1912       return NULL_TREE;
1913     }
1914   else if (TREE_CODE (t) == CALL_EXPR)
1915     mark_used (TREE_OPERAND (TREE_OPERAND (t, 0), 0));
1916
1917   /* Make a copy of this node.  */
1918   return copy_tree_r (tp, walk_subtrees, NULL);
1919 }
1920   
1921 /* Replace all remapped VAR_DECLs in T with their new equivalents.
1922    DATA is really a splay-tree mapping old variables to new
1923    variables.  */
1924
1925 static tree
1926 bot_replace (t, walk_subtrees, data)
1927      tree *t;
1928      int *walk_subtrees ATTRIBUTE_UNUSED;
1929      void *data;
1930 {
1931   splay_tree target_remap = ((splay_tree) data);
1932
1933   if (TREE_CODE (*t) == VAR_DECL)
1934     {
1935       splay_tree_node n = splay_tree_lookup (target_remap,
1936                                              (splay_tree_key) *t);
1937       if (n)
1938         *t = (tree) n->value;
1939     }
1940
1941   return NULL_TREE;
1942 }
1943         
1944 /* When we parse a default argument expression, we may create
1945    temporary variables via TARGET_EXPRs.  When we actually use the
1946    default-argument expression, we make a copy of the expression, but
1947    we must replace the temporaries with appropriate local versions.  */
1948
1949 tree
1950 break_out_target_exprs (t)
1951      tree t;
1952 {
1953   static int target_remap_count;
1954   static splay_tree target_remap;
1955
1956   if (!target_remap_count++)
1957     target_remap = splay_tree_new (splay_tree_compare_pointers, 
1958                                    /*splay_tree_delete_key_fn=*/NULL, 
1959                                    /*splay_tree_delete_value_fn=*/NULL);
1960   walk_tree (&t, bot_manip, target_remap);
1961   walk_tree (&t, bot_replace, target_remap);
1962
1963   if (!--target_remap_count)
1964     {
1965       splay_tree_delete (target_remap);
1966       target_remap = NULL;
1967     }
1968
1969   return t;
1970 }
1971
1972 /* Obstack used for allocating nodes in template function and variable
1973    definitions.  */
1974
1975 /* Similar to `build_nt', except we build
1976    on the permanent_obstack, regardless.  */
1977
1978 tree
1979 build_min_nt VPROTO((enum tree_code code, ...))
1980 {
1981 #ifndef ANSI_PROTOTYPES
1982   enum tree_code code;
1983 #endif
1984   register struct obstack *ambient_obstack = expression_obstack;
1985   va_list p;
1986   register tree t;
1987   register int length;
1988   register int i;
1989
1990   VA_START (p, code);
1991
1992 #ifndef ANSI_PROTOTYPES
1993   code = va_arg (p, enum tree_code);
1994 #endif
1995
1996   expression_obstack = &permanent_obstack;
1997
1998   t = make_node (code);
1999   length = tree_code_length[(int) code];
2000   TREE_COMPLEXITY (t) = lineno;
2001
2002   for (i = 0; i < length; i++)
2003     {
2004       tree x = va_arg (p, tree);
2005       TREE_OPERAND (t, i) = x;
2006     }
2007
2008   va_end (p);
2009   expression_obstack = ambient_obstack;
2010   return t;
2011 }
2012
2013 /* Similar to `build', except we build
2014    on the permanent_obstack, regardless.  */
2015
2016 tree
2017 build_min VPROTO((enum tree_code code, tree tt, ...))
2018 {
2019 #ifndef ANSI_PROTOTYPES
2020   enum tree_code code;
2021   tree tt;
2022 #endif
2023   register struct obstack *ambient_obstack = expression_obstack;
2024   va_list p;
2025   register tree t;
2026   register int length;
2027   register int i;
2028
2029   VA_START (p, tt);
2030
2031 #ifndef ANSI_PROTOTYPES
2032   code = va_arg (p, enum tree_code);
2033   tt = va_arg (p, tree);
2034 #endif
2035
2036   expression_obstack = &permanent_obstack;
2037
2038   t = make_node (code);
2039   length = tree_code_length[(int) code];
2040   TREE_TYPE (t) = tt;
2041   TREE_COMPLEXITY (t) = lineno;
2042
2043   for (i = 0; i < length; i++)
2044     {
2045       tree x = va_arg (p, tree);
2046       TREE_OPERAND (t, i) = x;
2047     }
2048
2049   va_end (p);
2050   expression_obstack = ambient_obstack;
2051   return t;
2052 }
2053
2054 /* Same as `tree_cons' but make a permanent object.  */
2055
2056 tree
2057 min_tree_cons (purpose, value, chain)
2058      tree purpose, value, chain;
2059 {
2060   register tree node;
2061   register struct obstack *ambient_obstack = current_obstack;
2062   current_obstack = &permanent_obstack;
2063
2064   node = tree_cons (purpose, value, chain);
2065
2066   current_obstack = ambient_obstack;
2067   return node;
2068 }
2069
2070 tree
2071 get_type_decl (t)
2072      tree t;
2073 {
2074   if (TREE_CODE (t) == TYPE_DECL)
2075     return t;
2076   if (TREE_CODE_CLASS (TREE_CODE (t)) == 't')
2077     return TYPE_STUB_DECL (t);
2078   
2079   my_friendly_abort (42);
2080
2081   /* Stop compiler from complaining control reaches end of non-void function.  */
2082   return 0;
2083 }
2084
2085 int
2086 can_free (obstack, t)
2087      struct obstack *obstack;
2088      tree t;
2089 {
2090   int size = 0;
2091
2092   if (TREE_CODE (t) == TREE_VEC)
2093     size = (TREE_VEC_LENGTH (t)-1) * sizeof (tree) + sizeof (struct tree_vec);
2094   else
2095     my_friendly_abort (42);
2096
2097 #define ROUND(x) ((x + obstack_alignment_mask (obstack)) \
2098                   & ~ obstack_alignment_mask (obstack))
2099   if ((char *)t + ROUND (size) == obstack_next_free (obstack))
2100     return 1;
2101 #undef ROUND
2102
2103   return 0;
2104 }
2105
2106 /* Return first vector element whose BINFO_TYPE is ELEM.
2107    Return 0 if ELEM is not in VEC.  VEC may be NULL_TREE.  */
2108
2109 tree
2110 vec_binfo_member (elem, vec)
2111      tree elem, vec;
2112 {
2113   int i;
2114
2115   if (vec)
2116     for (i = 0; i < TREE_VEC_LENGTH (vec); ++i)
2117       if (same_type_p (elem, BINFO_TYPE (TREE_VEC_ELT (vec, i))))
2118         return TREE_VEC_ELT (vec, i);
2119
2120   return NULL_TREE;
2121 }
2122
2123 /* Kludge around the fact that DECL_CONTEXT for virtual functions returns
2124    the wrong thing for decl_function_context.  Hopefully the uses in the
2125    backend won't matter, since we don't need a static chain for local class
2126    methods.  FIXME!  */
2127
2128 tree
2129 hack_decl_function_context (decl)
2130      tree decl;
2131 {
2132   if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FUNCTION_MEMBER_P (decl))
2133     return decl_function_context (TYPE_MAIN_DECL (DECL_CLASS_CONTEXT (decl)));
2134   return decl_function_context (decl);
2135 }
2136
2137 /* Returns the namespace that contains DECL, whether directly or
2138    indirectly.  */
2139
2140 tree
2141 decl_namespace_context (decl)
2142      tree decl;
2143 {
2144   while (1)
2145     {
2146       if (TREE_CODE (decl) == NAMESPACE_DECL)
2147         return decl;
2148       else if (TYPE_P (decl))
2149         decl = CP_DECL_CONTEXT (TYPE_MAIN_DECL (decl));
2150       else
2151         decl = CP_DECL_CONTEXT (decl);
2152     }
2153 }
2154
2155 /* Return truthvalue of whether T1 is the same tree structure as T2.
2156    Return 1 if they are the same.
2157    Return 0 if they are understandably different.
2158    Return -1 if either contains tree structure not understood by
2159    this function.  */
2160
2161 int
2162 cp_tree_equal (t1, t2)
2163      tree t1, t2;
2164 {
2165   register enum tree_code code1, code2;
2166   int cmp;
2167
2168   if (t1 == t2)
2169     return 1;
2170   if (t1 == 0 || t2 == 0)
2171     return 0;
2172
2173   code1 = TREE_CODE (t1);
2174   code2 = TREE_CODE (t2);
2175
2176   if (code1 == NOP_EXPR || code1 == CONVERT_EXPR || code1 == NON_LVALUE_EXPR)
2177     {
2178       if (code2 == NOP_EXPR || code2 == CONVERT_EXPR || code2 == NON_LVALUE_EXPR)
2179         return cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
2180       else
2181         return cp_tree_equal (TREE_OPERAND (t1, 0), t2);
2182     }
2183   else if (code2 == NOP_EXPR || code2 == CONVERT_EXPR
2184            || code2 == NON_LVALUE_EXPR)
2185     return cp_tree_equal (t1, TREE_OPERAND (t2, 0));
2186
2187   if (code1 != code2)
2188     return 0;
2189
2190   switch (code1)
2191     {
2192     case INTEGER_CST:
2193       return TREE_INT_CST_LOW (t1) == TREE_INT_CST_LOW (t2)
2194         && TREE_INT_CST_HIGH (t1) == TREE_INT_CST_HIGH (t2);
2195
2196     case REAL_CST:
2197       return REAL_VALUES_EQUAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
2198
2199     case STRING_CST:
2200       return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
2201         && !bcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
2202                   TREE_STRING_LENGTH (t1));
2203
2204     case CONSTRUCTOR:
2205       /* We need to do this when determining whether or not two
2206          non-type pointer to member function template arguments
2207          are the same.  */
2208       if (!(same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))
2209             /* The first operand is RTL.  */
2210             && TREE_OPERAND (t1, 0) == TREE_OPERAND (t2, 0)))
2211         return 0;
2212       return cp_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
2213
2214     case TREE_LIST:
2215       cmp = cp_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2));
2216       if (cmp <= 0)
2217         return cmp;
2218       cmp = cp_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2));
2219       if (cmp <= 0)
2220         return cmp;
2221       return cp_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
2222
2223     case SAVE_EXPR:
2224       return cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
2225
2226     case CALL_EXPR:
2227       cmp = cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
2228       if (cmp <= 0)
2229         return cmp;
2230       return simple_cst_list_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
2231
2232     case TARGET_EXPR:
2233       /* Special case: if either target is an unallocated VAR_DECL,
2234          it means that it's going to be unified with whatever the
2235          TARGET_EXPR is really supposed to initialize, so treat it
2236          as being equivalent to anything.  */
2237       if ((TREE_CODE (TREE_OPERAND (t1, 0)) == VAR_DECL
2238            && DECL_NAME (TREE_OPERAND (t1, 0)) == NULL_TREE
2239            && DECL_RTL (TREE_OPERAND (t1, 0)) == 0)
2240           || (TREE_CODE (TREE_OPERAND (t2, 0)) == VAR_DECL
2241               && DECL_NAME (TREE_OPERAND (t2, 0)) == NULL_TREE
2242               && DECL_RTL (TREE_OPERAND (t2, 0)) == 0))
2243         cmp = 1;
2244       else
2245         cmp = cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
2246       if (cmp <= 0)
2247         return cmp;
2248       return cp_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
2249
2250     case WITH_CLEANUP_EXPR:
2251       cmp = cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
2252       if (cmp <= 0)
2253         return cmp;
2254       return cp_tree_equal (TREE_OPERAND (t1, 2), TREE_OPERAND (t1, 2));
2255
2256     case COMPONENT_REF:
2257       if (TREE_OPERAND (t1, 1) == TREE_OPERAND (t2, 1))
2258         return cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
2259       return 0;
2260
2261     case VAR_DECL:
2262     case PARM_DECL:
2263     case CONST_DECL:
2264     case FUNCTION_DECL:
2265       return 0;
2266
2267     case TEMPLATE_PARM_INDEX:
2268       return TEMPLATE_PARM_IDX (t1) == TEMPLATE_PARM_IDX (t2)
2269         && TEMPLATE_PARM_LEVEL (t1) == TEMPLATE_PARM_LEVEL (t2);
2270
2271     case SIZEOF_EXPR:
2272     case ALIGNOF_EXPR:
2273       if (TREE_CODE (TREE_OPERAND (t1, 0)) != TREE_CODE (TREE_OPERAND (t2, 0)))
2274         return 0;
2275       if (TREE_CODE_CLASS (TREE_CODE (TREE_OPERAND (t1, 0))) == 't')
2276         return same_type_p (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
2277       break;
2278
2279     case PTRMEM_CST:
2280       /* Two pointer-to-members are the same if they point to the same
2281          field or function in the same class.  */
2282       return (PTRMEM_CST_MEMBER (t1) == PTRMEM_CST_MEMBER (t2)
2283               && same_type_p (PTRMEM_CST_CLASS (t1), PTRMEM_CST_CLASS (t2)));
2284
2285     default:
2286       break;
2287     }
2288
2289   switch (TREE_CODE_CLASS (code1))
2290     {
2291       int i;
2292     case '1':
2293     case '2':
2294     case '<':
2295     case 'e':
2296     case 'r':
2297     case 's':
2298       cmp = 1;
2299       for (i=0; i<tree_code_length[(int) code1]; ++i)
2300         {
2301           cmp = cp_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i));
2302           if (cmp <= 0)
2303             return cmp;
2304         }
2305       return cmp;
2306     }
2307
2308   return -1;
2309 }
2310
2311 /* Build a wrapper around some pointer PTR so we can use it as a tree.  */
2312
2313 tree
2314 build_ptr_wrapper (ptr)
2315      void *ptr;
2316 {
2317   tree t = make_node (WRAPPER);
2318   WRAPPER_PTR (t) = ptr;
2319   return t;
2320 }
2321
2322 /* Same, but on the expression_obstack.  */
2323
2324 tree
2325 build_expr_ptr_wrapper (ptr)
2326      void *ptr;
2327 {
2328   tree t;
2329   push_expression_obstack ();
2330   t = build_ptr_wrapper (ptr);
2331   pop_obstacks ();
2332   return t;
2333 }
2334
2335 /* Build a wrapper around some integer I so we can use it as a tree.  */
2336
2337 tree
2338 build_int_wrapper (i)
2339      int i;
2340 {
2341   tree t = make_node (WRAPPER);
2342   WRAPPER_INT (t) = i;
2343   return t;
2344 }
2345
2346 static tree
2347 build_srcloc (file, line)
2348      char *file;
2349      int line;
2350 {
2351   tree t;
2352
2353   t = make_node (SRCLOC);
2354   SRCLOC_FILE (t) = file;
2355   SRCLOC_LINE (t) = line;
2356
2357   return t;
2358 }
2359
2360 tree
2361 build_srcloc_here ()
2362 {
2363   return build_srcloc (input_filename, lineno);
2364 }
2365
2366 void
2367 push_expression_obstack ()
2368 {
2369   push_obstacks_nochange ();
2370   current_obstack = expression_obstack;
2371 }
2372
2373 /* Begin allocating on the permanent obstack.  When you're done
2374    allocating there, call pop_obstacks to return to the previous set
2375    of obstacks.  */
2376
2377 void
2378 push_permanent_obstack ()
2379 {
2380   push_obstacks_nochange ();
2381   end_temporary_allocation ();
2382 }
2383
2384 /* The type of ARG when used as an lvalue.  */
2385
2386 tree
2387 lvalue_type (arg)
2388      tree arg;
2389 {
2390   tree type = TREE_TYPE (arg);
2391   if (TREE_CODE (arg) == OVERLOAD)
2392     type = unknown_type_node;
2393   return type;
2394 }
2395
2396 /* The type of ARG for printing error messages; denote lvalues with
2397    reference types.  */
2398
2399 tree
2400 error_type (arg)
2401      tree arg;
2402 {
2403   tree type = TREE_TYPE (arg);
2404   if (TREE_CODE (type) == ARRAY_TYPE)
2405     ;
2406   else if (real_lvalue_p (arg))
2407     type = build_reference_type (lvalue_type (arg));
2408   else if (IS_AGGR_TYPE (type))
2409     type = lvalue_type (arg);
2410
2411   return type;
2412 }
2413
2414 /* Does FUNCTION use a variable-length argument list?  */
2415
2416 int
2417 varargs_function_p (function)
2418      tree function;
2419 {
2420   tree parm = TYPE_ARG_TYPES (TREE_TYPE (function));
2421   for (; parm; parm = TREE_CHAIN (parm))
2422     if (TREE_VALUE (parm) == void_type_node)
2423       return 0;
2424   return 1;
2425 }
2426
2427 /* Returns 1 if decl is a member of a class.  */
2428
2429 int
2430 member_p (decl)
2431      tree decl;
2432 {
2433   tree ctx = DECL_CONTEXT (decl);
2434   return (ctx && TREE_CODE_CLASS (TREE_CODE (ctx)) == 't');
2435 }
2436
2437 /* Create a placeholder for member access where we don't actually have an
2438    object that the access is against.  */
2439
2440 tree
2441 build_dummy_object (type)
2442      tree type;
2443 {
2444   tree decl = build1 (NOP_EXPR, build_pointer_type (type), void_zero_node);
2445   return build_indirect_ref (decl, NULL_PTR);
2446 }
2447
2448 /* We've gotten a reference to a member of TYPE.  Return *this if appropriate,
2449    or a dummy object otherwise.  If BINFOP is non-0, it is filled with the
2450    binfo path from current_class_type to TYPE, or 0.  */
2451
2452 tree
2453 maybe_dummy_object (type, binfop)
2454      tree type;
2455      tree *binfop;
2456 {
2457   tree decl, context;
2458
2459   if (current_class_type
2460       && get_base_distance (type, current_class_type, 0, binfop) != -1)
2461     context = current_class_type;
2462   else
2463     {
2464       /* Reference from a nested class member function.  */
2465       context = type;
2466       if (binfop)
2467         *binfop = TYPE_BINFO (type);
2468     }
2469
2470   if (current_class_ref && context == current_class_type)
2471     decl = current_class_ref;
2472   else
2473     decl = build_dummy_object (context);
2474
2475   return decl;
2476 }
2477
2478 /* Returns 1 if OB is a placeholder object, or a pointer to one.  */
2479
2480 int
2481 is_dummy_object (ob)
2482      tree ob;
2483 {
2484   if (TREE_CODE (ob) == INDIRECT_REF)
2485     ob = TREE_OPERAND (ob, 0);
2486   return (TREE_CODE (ob) == NOP_EXPR
2487           && TREE_OPERAND (ob, 0) == void_zero_node);
2488 }
2489
2490 /* Returns 1 iff type T is a POD type, as defined in [basic.types].  */
2491
2492 int
2493 pod_type_p (t)
2494      tree t;
2495 {
2496   while (TREE_CODE (t) == ARRAY_TYPE)
2497     t = TREE_TYPE (t);
2498
2499   if (INTEGRAL_TYPE_P (t))
2500     return 1;  /* integral, character or enumeral type */
2501   if (FLOAT_TYPE_P (t))
2502     return 1;
2503   if (TYPE_PTR_P (t))
2504     return 1; /* pointer to non-member */
2505   if (TYPE_PTRMEM_P (t))
2506     return 1; /* pointer to member object */
2507   if (TYPE_PTRMEMFUNC_P (t))
2508     return 1; /* pointer to member function */
2509   
2510   if (! CLASS_TYPE_P (t))
2511     return 0; /* other non-class type (reference or function) */
2512   if (CLASSTYPE_NON_POD_P (t))
2513     return 0;
2514   return 1;
2515 }
2516
2517 /* Return a 1 if ATTR_NAME and ATTR_ARGS denote a valid C++-specific
2518    attribute for either declaration DECL or type TYPE and 0 otherwise.
2519    Plugged into valid_lang_attribute.  */
2520
2521 int
2522 cp_valid_lang_attribute (attr_name, attr_args, decl, type)
2523   tree attr_name;
2524   tree attr_args ATTRIBUTE_UNUSED;
2525   tree decl ATTRIBUTE_UNUSED;
2526   tree type ATTRIBUTE_UNUSED;
2527 {
2528   if (is_attribute_p ("com_interface", attr_name))
2529     {
2530       if (! flag_vtable_thunks)
2531         {
2532           error ("`com_interface' only supported with -fvtable-thunks");
2533           return 0;
2534         }
2535
2536       if (attr_args != NULL_TREE
2537           || decl != NULL_TREE
2538           || ! CLASS_TYPE_P (type)
2539           || type != TYPE_MAIN_VARIANT (type))
2540         {
2541           warning ("`com_interface' attribute can only be applied to class definitions");
2542           return 0;
2543         }
2544
2545       CLASSTYPE_COM_INTERFACE (type) = 1;
2546       return 1;
2547     }
2548   else if (is_attribute_p ("init_priority", attr_name))
2549     {
2550       tree initp_expr = (attr_args ? TREE_VALUE (attr_args): NULL_TREE);
2551       int pri;
2552
2553       if (initp_expr)
2554         STRIP_NOPS (initp_expr);
2555           
2556       if (!initp_expr || TREE_CODE (initp_expr) != INTEGER_CST)
2557         {
2558           error ("requested init_priority is not an integer constant");
2559           return 0;
2560         }
2561
2562       pri = TREE_INT_CST_LOW (initp_expr);
2563         
2564       while (TREE_CODE (type) == ARRAY_TYPE)
2565         type = TREE_TYPE (type);
2566
2567       if (decl == NULL_TREE
2568           || TREE_CODE (decl) != VAR_DECL
2569           || ! TREE_STATIC (decl)
2570           || DECL_EXTERNAL (decl)
2571           || (TREE_CODE (type) != RECORD_TYPE
2572               && TREE_CODE (type) != UNION_TYPE)
2573           /* Static objects in functions are initialized the
2574              first time control passes through that
2575              function. This is not precise enough to pin down an
2576              init_priority value, so don't allow it. */
2577           || current_function_decl) 
2578         {
2579           error ("can only use init_priority attribute on file-scope definitions of objects of class type");
2580           return 0;
2581         }
2582
2583       if (pri > MAX_INIT_PRIORITY || pri <= 0)
2584         {
2585           error ("requested init_priority is out of range");
2586           return 0;
2587         }
2588
2589       /* Check for init_priorities that are reserved for
2590          language and runtime support implementations.*/
2591       if (pri <= MAX_RESERVED_INIT_PRIORITY)
2592         {
2593           warning 
2594             ("requested init_priority is reserved for internal use");
2595         }
2596
2597       DECL_INIT_PRIORITY (decl) = pri;
2598       return 1;
2599     }
2600
2601   return 0;
2602 }
2603
2604 /* Return a new PTRMEM_CST of the indicated TYPE.  The MEMBER is the
2605    thing pointed to by the constant.  */
2606
2607 tree
2608 make_ptrmem_cst (type, member)
2609      tree type;
2610      tree member;
2611 {
2612   tree ptrmem_cst = make_node (PTRMEM_CST);
2613   /* If would seem a great convenience if make_node would set
2614      TREE_CONSTANT for things of class `c', but it does not.  */
2615   TREE_CONSTANT (ptrmem_cst) = 1;
2616   TREE_TYPE (ptrmem_cst) = type;
2617   PTRMEM_CST_MEMBER (ptrmem_cst) = member;
2618   return ptrmem_cst;
2619 }
2620
2621 /* Mark ARG (which is really a list_hash_table **) for GC.  */
2622
2623 static void
2624 mark_list_hash (arg)
2625      void *arg;
2626 {
2627   struct list_hash *lh;
2628
2629   for (lh = * ((struct list_hash **) arg); lh; lh = lh->next)
2630     ggc_mark_tree (lh->list);
2631 }
2632
2633 /* Initialize tree.c.  */
2634
2635 void
2636 init_tree ()
2637 {
2638   make_lang_type_fn = cp_make_lang_type;
2639   lang_unsave_expr_now = cplus_unsave_expr_now;
2640   ggc_add_root (list_hash_table, 
2641                 sizeof (list_hash_table) / sizeof (struct list_hash *),
2642                 sizeof (struct list_hash *),
2643                 mark_list_hash);
2644 }
2645
2646 /* The C++ version of unsave_expr_now.
2647    See gcc/tree.c:unsave_expr_now for comments. */
2648
2649 void
2650 cplus_unsave_expr_now (expr)
2651      tree expr;
2652 {
2653   if (expr == NULL)
2654     return;
2655
2656   else if (TREE_CODE (expr) == AGGR_INIT_EXPR)
2657     {
2658       unsave_expr_now (TREE_OPERAND (expr,0));
2659       if (TREE_OPERAND (expr, 1)
2660           && TREE_CODE (TREE_OPERAND (expr, 1)) == TREE_LIST)
2661         {
2662           tree exp = TREE_OPERAND (expr, 1);
2663           while (exp)
2664             {
2665               unsave_expr_now (TREE_VALUE (exp));
2666               exp = TREE_CHAIN (exp);
2667             }
2668         }
2669       unsave_expr_now (TREE_OPERAND (expr,2));
2670       return;
2671     }
2672
2673   else
2674     return;
2675 }
2676