OSDN Git Service

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