OSDN Git Service

* cp-tree.h (dfs_mark_primary_bases_queue_p): New function.
[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 "insn-config.h"
32 #include "integrate.h"
33
34 static tree bot_manip PROTO((tree *, int *, void *));
35 static tree bot_replace PROTO((tree *, int *, void *));
36 static tree build_cplus_array_type_1 PROTO((tree, tree));
37 static void list_hash_add PROTO((int, tree));
38 static int list_hash PROTO((tree, tree, tree));
39 static tree list_hash_lookup PROTO((int, tree, tree, tree));
40 static cp_lvalue_kind lvalue_p_1 PROTO((tree, int));
41 static tree no_linkage_helper PROTO((tree *, int *, void *));
42 static tree build_srcloc PROTO((char *, int));
43 static void mark_list_hash PROTO ((void *));
44 static int statement_code_p PROTO((enum tree_code));
45 static tree mark_local_for_remap_r PROTO((tree *, int *, void *));
46 static tree cp_unsave_r PROTO ((tree *, int *, void *));
47 static void cp_unsave PROTO((tree *));
48 static tree build_target_expr PROTO((tree, tree));
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   DECL_CONTEXT (slot) = current_function_decl;
263   layout_decl (slot, 0);
264
265   /* We split the CALL_EXPR into its function and its arguments here.
266      Then, in expand_expr, we put them back together.  The reason for
267      this is that this expression might be a default argument
268      expression.  In that case, we need a new temporary every time the
269      expression is used.  That's what break_out_target_exprs does; it
270      replaces every AGGR_INIT_EXPR with a copy that uses a fresh
271      temporary slot.  Then, expand_expr builds up a call-expression
272      using the new slot.  */
273   fn = TREE_OPERAND (init, 0);
274   rval = build (AGGR_INIT_EXPR, type, fn, TREE_OPERAND (init, 1), slot);
275   TREE_SIDE_EFFECTS (rval) = 1;
276   AGGR_INIT_VIA_CTOR_P (rval) 
277     = (TREE_CODE (fn) == ADDR_EXPR
278        && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
279        && DECL_CONSTRUCTOR_P (TREE_OPERAND (fn, 0)));
280   rval = build_target_expr (slot, rval);
281
282   return rval;
283 }
284
285 /* Buidl a TARGET_EXPR using INIT to initialize a new temporary of the
286    indicated TYPE.  */
287
288 tree
289 build_target_expr_with_type (init, type)
290      tree init;
291      tree type;
292 {
293   tree slot;
294   tree rval;
295
296   slot = build (VAR_DECL, type);
297   DECL_ARTIFICIAL (slot) = 1;
298   DECL_CONTEXT (slot) = current_function_decl;
299   layout_decl (slot, 0);
300   rval = build_target_expr (slot, init);
301
302   return rval;
303 }
304
305 /* Like build_target_expr_with_type, but use the type of INIT.  */
306
307 tree
308 get_target_expr (init)
309      tree init;
310 {
311   return build_target_expr_with_type (init, TREE_TYPE (init));
312 }
313
314 /* Recursively search EXP for CALL_EXPRs that need cleanups and replace
315    these CALL_EXPRs with tree nodes that will perform the cleanups.  */
316
317 tree
318 break_out_cleanups (exp)
319      tree exp;
320 {
321   tree tmp = exp;
322
323   if (TREE_CODE (tmp) == CALL_EXPR
324       && TYPE_NEEDS_DESTRUCTOR (TREE_TYPE (tmp)))
325     return build_cplus_new (TREE_TYPE (tmp), tmp);
326
327   while (TREE_CODE (tmp) == NOP_EXPR
328          || TREE_CODE (tmp) == CONVERT_EXPR
329          || TREE_CODE (tmp) == NON_LVALUE_EXPR)
330     {
331       if (TREE_CODE (TREE_OPERAND (tmp, 0)) == CALL_EXPR
332           && TYPE_NEEDS_DESTRUCTOR (TREE_TYPE (TREE_OPERAND (tmp, 0))))
333         {
334           TREE_OPERAND (tmp, 0)
335             = build_cplus_new (TREE_TYPE (TREE_OPERAND (tmp, 0)),
336                                TREE_OPERAND (tmp, 0));
337           break;
338         }
339       else
340         tmp = TREE_OPERAND (tmp, 0);
341     }
342   return exp;
343 }
344
345 /* Recursively perform a preorder search EXP for CALL_EXPRs, making
346    copies where they are found.  Returns a deep copy all nodes transitively
347    containing CALL_EXPRs.  */
348
349 tree
350 break_out_calls (exp)
351      tree exp;
352 {
353   register tree t1, t2 = NULL_TREE;
354   register enum tree_code code;
355   register int changed = 0;
356   register int i;
357
358   if (exp == NULL_TREE)
359     return exp;
360
361   code = TREE_CODE (exp);
362
363   if (code == CALL_EXPR)
364     return copy_node (exp);
365
366   /* Don't try and defeat a save_expr, as it should only be done once.  */
367     if (code == SAVE_EXPR)
368        return exp;
369
370   switch (TREE_CODE_CLASS (code))
371     {
372     default:
373       abort ();
374
375     case 'c':  /* a constant */
376     case 't':  /* a type node */
377     case 'x':  /* something random, like an identifier or an ERROR_MARK.  */
378       return exp;
379
380     case 'd':  /* A decl node */
381 #if 0                               /* This is bogus.  jason 9/21/94 */
382
383       t1 = break_out_calls (DECL_INITIAL (exp));
384       if (t1 != DECL_INITIAL (exp))
385         {
386           exp = copy_node (exp);
387           DECL_INITIAL (exp) = t1;
388         }
389 #endif
390       return exp;
391
392     case 'b':  /* A block node */
393       {
394         /* Don't know how to handle these correctly yet.   Must do a
395            break_out_calls on all DECL_INITIAL values for local variables,
396            and also break_out_calls on all sub-blocks and sub-statements.  */
397         abort ();
398       }
399       return exp;
400
401     case 'e':  /* an expression */
402     case 'r':  /* a reference */
403     case 's':  /* an expression with side effects */
404       for (i = tree_code_length[(int) code] - 1; i >= 0; i--)
405         {
406           t1 = break_out_calls (TREE_OPERAND (exp, i));
407           if (t1 != TREE_OPERAND (exp, i))
408             {
409               exp = copy_node (exp);
410               TREE_OPERAND (exp, i) = t1;
411             }
412         }
413       return exp;
414
415     case '<':  /* a comparison expression */
416     case '2':  /* a binary arithmetic expression */
417       t2 = break_out_calls (TREE_OPERAND (exp, 1));
418       if (t2 != TREE_OPERAND (exp, 1))
419         changed = 1;
420     case '1':  /* a unary arithmetic expression */
421       t1 = break_out_calls (TREE_OPERAND (exp, 0));
422       if (t1 != TREE_OPERAND (exp, 0))
423         changed = 1;
424       if (changed)
425         {
426           if (tree_code_length[(int) code] == 1)
427             return build1 (code, TREE_TYPE (exp), t1);
428           else
429             return build (code, TREE_TYPE (exp), t1, t2);
430         }
431       return exp;
432     }
433
434 }
435 \f
436 extern struct obstack permanent_obstack;
437
438 /* Here is how primitive or already-canonicalized types' hash
439    codes are made.  MUST BE CONSISTENT WITH tree.c !!! */
440 #define TYPE_HASH(TYPE) ((HOST_WIDE_INT) (TYPE) & 0777777)
441
442 /* Construct, lay out and return the type of methods belonging to class
443    BASETYPE and whose arguments are described by ARGTYPES and whose values
444    are described by RETTYPE.  If each type exists already, reuse it.  */
445
446 tree
447 build_cplus_method_type (basetype, rettype, argtypes)
448      tree basetype, rettype, argtypes;
449 {
450   register tree t;
451   tree ptype;
452   int hashcode;
453
454   /* Make a node of the sort we want.  */
455   t = make_node (METHOD_TYPE);
456
457   TYPE_METHOD_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
458   TREE_TYPE (t) = rettype;
459   ptype = build_pointer_type (basetype);
460
461   /* The actual arglist for this function includes a "hidden" argument
462      which is "this".  Put it into the list of argument types.  Make
463      sure that the new argument list is allocated on the same obstack
464      as the type.  */
465   argtypes = tree_cons (NULL_TREE, ptype, argtypes);
466   TYPE_ARG_TYPES (t) = argtypes;
467   TREE_SIDE_EFFECTS (argtypes) = 1;  /* Mark first argtype as "artificial".  */
468
469   /* If we already have such a type, use the old one and free this one.
470      Note that it also frees up the above cons cell if found.  */
471   hashcode = TYPE_HASH (basetype) + TYPE_HASH (rettype) +
472     type_hash_list (argtypes);
473
474   t = type_hash_canon (hashcode, t);
475
476   if (TYPE_SIZE (t) == 0)
477     layout_type (t);
478
479   return t;
480 }
481
482 static tree
483 build_cplus_array_type_1 (elt_type, index_type)
484      tree elt_type;
485      tree index_type;
486 {
487   tree t;
488
489   if (elt_type == error_mark_node || index_type == error_mark_node)
490     return error_mark_node;
491
492   if (processing_template_decl 
493       || uses_template_parms (elt_type) 
494       || uses_template_parms (index_type))
495     {
496       t = make_node (ARRAY_TYPE);
497       TREE_TYPE (t) = elt_type;
498       TYPE_DOMAIN (t) = index_type;
499     }
500   else
501     t = build_array_type (elt_type, index_type);
502
503   /* Push these needs up so that initialization takes place
504      more easily.  */
505   TYPE_NEEDS_CONSTRUCTING (t) 
506     = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (elt_type));
507   TYPE_NEEDS_DESTRUCTOR (t) 
508     = TYPE_NEEDS_DESTRUCTOR (TYPE_MAIN_VARIANT (elt_type));
509   return t;
510 }
511
512 tree
513 build_cplus_array_type (elt_type, index_type)
514      tree elt_type;
515      tree index_type;
516 {
517   tree t;
518   int type_quals = CP_TYPE_QUALS (elt_type);
519
520   elt_type = TYPE_MAIN_VARIANT (elt_type);
521
522   t = build_cplus_array_type_1 (elt_type, index_type);
523
524   if (type_quals != TYPE_UNQUALIFIED)
525     t = cp_build_qualified_type (t, type_quals);
526
527   return t;
528 }
529 \f
530 /* Make a variant of TYPE, qualified with the TYPE_QUALS.  Handles
531    arrays correctly.  In particular, if TYPE is an array of T's, and
532    TYPE_QUALS is non-empty, returns an array of qualified T's.  If
533    at attempt is made to qualify a type illegally, and COMPLAIN is
534    non-zero, an error is issued.  If COMPLAIN is zero, error_mark_node
535    is returned.  */
536
537 tree
538 cp_build_qualified_type_real (type, type_quals, complain)
539      tree type;
540      int type_quals;
541      int complain;
542 {
543   tree result;
544
545   if (type == error_mark_node)
546     return type;
547
548   if (type_quals == TYPE_QUALS (type))
549     return type;
550
551   /* A restrict-qualified pointer type must be a pointer (or reference)
552      to object or incomplete type.  */
553   if ((type_quals & TYPE_QUAL_RESTRICT)
554       && TREE_CODE (type) != TEMPLATE_TYPE_PARM
555       && (!POINTER_TYPE_P (type)
556           || TYPE_PTRMEM_P (type)
557           || TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE))
558     {
559       if (complain)
560         cp_error ("`%T' cannot be `restrict'-qualified", type);
561       else
562         return error_mark_node;
563
564       type_quals &= ~TYPE_QUAL_RESTRICT;
565     }
566
567   if (type_quals != TYPE_UNQUALIFIED
568       && TREE_CODE (type) == FUNCTION_TYPE)
569     {
570       if (complain)
571         cp_error ("`%T' cannot be `const'-, `volatile'-, or `restrict'-qualified", type);
572       else
573         return error_mark_node;
574       type_quals = TYPE_UNQUALIFIED;
575     }
576   else if (TREE_CODE (type) == ARRAY_TYPE)
577     {
578       /* In C++, the qualification really applies to the array element
579          type.  Obtain the appropriately qualified element type.  */
580       tree t;
581       tree element_type 
582         = cp_build_qualified_type_real (TREE_TYPE (type), 
583                                         type_quals,
584                                         complain);
585
586       if (element_type == error_mark_node)
587         return error_mark_node;
588
589       /* See if we already have an identically qualified type.  */
590       for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
591         if (CP_TYPE_QUALS (t) == type_quals)
592           break;
593
594       /* If we didn't already have it, create it now.  */
595       if (!t)
596         {
597           /* Make a new array type, just like the old one, but with the
598              appropriately qualified element type.  */
599           t = build_type_copy (type);
600           TREE_TYPE (t) = element_type;
601         }
602
603       /* Even if we already had this variant, we update
604          TYPE_NEEDS_CONSTRUCTING and TYPE_NEEDS_DESTRUCTOR in case
605          they changed since the variant was originally created.  
606          
607          This seems hokey; if there is some way to use a previous
608          variant *without* coming through here,
609          TYPE_NEEDS_CONSTRUCTING will never be updated.  */
610       TYPE_NEEDS_CONSTRUCTING (t) 
611         = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (element_type));
612       TYPE_NEEDS_DESTRUCTOR (t) 
613         = TYPE_NEEDS_DESTRUCTOR (TYPE_MAIN_VARIANT (element_type));
614       return t;
615     }
616   else if (TYPE_PTRMEMFUNC_P (type))
617     {
618       /* For a pointer-to-member type, we can't just return a
619          cv-qualified version of the RECORD_TYPE.  If we do, we
620          haven't change the field that contains the actual pointer to
621          a method, and so TYPE_PTRMEMFUNC_FN_TYPE will be wrong.  */
622       tree t;
623
624       t = TYPE_PTRMEMFUNC_FN_TYPE (type);
625       t = cp_build_qualified_type_real (t, type_quals, complain);
626       return build_ptrmemfunc_type (t);
627     }
628
629   /* Retrieve (or create) the appropriately qualified variant.  */
630   result = build_qualified_type (type, type_quals);
631
632   /* If this was a pointer-to-method type, and we just made a copy,
633      then we need to clear the cached associated
634      pointer-to-member-function type; it is not valid for the new
635      type.  */
636   if (result != type 
637       && TREE_CODE (type) == POINTER_TYPE
638       && TREE_CODE (TREE_TYPE (type)) == METHOD_TYPE)
639     TYPE_SET_PTRMEMFUNC_TYPE (result, NULL_TREE);
640
641   return result;
642 }
643
644 /* Returns the canonical version of TYPE.  In other words, if TYPE is
645    a typedef, returns the underlying type.  The cv-qualification of
646    the type returned matches the type input; they will always be
647    compatible types.  */
648
649 tree
650 canonical_type_variant (t)
651      tree t;
652 {
653   return cp_build_qualified_type (TYPE_MAIN_VARIANT (t), CP_TYPE_QUALS (t));
654 }
655 \f
656 /* Makes new binfos for the indirect bases under BINFO, and updates
657    BINFO_OFFSET for them and their bases.  */
658
659 void
660 unshare_base_binfos (binfo)
661      tree binfo;
662 {
663   tree binfos = BINFO_BASETYPES (binfo);
664   tree new_binfo;
665   int j;
666
667   if (binfos == NULL_TREE)
668     return;
669
670   /* Now unshare the structure beneath BINFO.  */
671   for (j = TREE_VEC_LENGTH (binfos)-1;
672        j >= 0; j--)
673     {
674       tree base_binfo = TREE_VEC_ELT (binfos, j);
675       new_binfo = TREE_VEC_ELT (binfos, j)
676         = make_binfo (BINFO_OFFSET (base_binfo),
677                       base_binfo,
678                       BINFO_VTABLE (base_binfo),
679                       BINFO_VIRTUALS (base_binfo));
680       TREE_VIA_PUBLIC (new_binfo) = TREE_VIA_PUBLIC (base_binfo);
681       TREE_VIA_PROTECTED (new_binfo) = TREE_VIA_PROTECTED (base_binfo);
682       TREE_VIA_VIRTUAL (new_binfo) = TREE_VIA_VIRTUAL (base_binfo);
683       BINFO_INHERITANCE_CHAIN (new_binfo) = binfo;
684       unshare_base_binfos (new_binfo);
685     }
686 }
687
688 \f
689 /* Hashing of lists so that we don't make duplicates.
690    The entry point is `list_hash_canon'.  */
691
692 /* Each hash table slot is a bucket containing a chain
693    of these structures.  */
694
695 struct list_hash
696 {
697   struct list_hash *next;       /* Next structure in the bucket.  */
698   int hashcode;                 /* Hash code of this list.  */
699   tree list;                    /* The list recorded here.  */
700 };
701
702 /* Now here is the hash table.  When recording a list, it is added
703    to the slot whose index is the hash code mod the table size.
704    Note that the hash table is used for several kinds of lists.
705    While all these live in the same table, they are completely independent,
706    and the hash code is computed differently for each of these.  */
707
708 #define TYPE_HASH_SIZE 59
709 static struct list_hash *list_hash_table[TYPE_HASH_SIZE];
710
711 /* Compute a hash code for a list (chain of TREE_LIST nodes
712    with goodies in the TREE_PURPOSE, TREE_VALUE, and bits of the
713    TREE_COMMON slots), by adding the hash codes of the individual entries.  */
714
715 static int
716 list_hash (purpose, value, chain)
717      tree purpose, value, chain;
718 {
719   register int hashcode = 0;
720
721   if (chain)
722     hashcode += TYPE_HASH (chain);
723
724   if (value)
725     hashcode += TYPE_HASH (value);
726   else
727     hashcode += 1007;
728   if (purpose)
729     hashcode += TYPE_HASH (purpose);
730   else
731     hashcode += 1009;
732   return hashcode;
733 }
734
735 /* Look in the type hash table for a type isomorphic to TYPE.
736    If one is found, return it.  Otherwise return 0.  */
737
738 static tree
739 list_hash_lookup (hashcode, purpose, value, chain)
740      int hashcode;
741      tree purpose, value, chain;
742 {
743   register struct list_hash *h;
744
745   for (h = list_hash_table[hashcode % TYPE_HASH_SIZE]; h; h = h->next)
746     if (h->hashcode == hashcode
747         && TREE_PURPOSE (h->list) == purpose
748         && TREE_VALUE (h->list) == value
749         && TREE_CHAIN (h->list) == chain)
750       return h->list;
751   return 0;
752 }
753
754 /* Add an entry to the list-hash-table
755    for a list TYPE whose hash code is HASHCODE.  */
756
757 static void
758 list_hash_add (hashcode, list)
759      int hashcode;
760      tree list;
761 {
762   register struct list_hash *h;
763
764   h = (struct list_hash *) obstack_alloc (&permanent_obstack, sizeof (struct list_hash));
765   h->hashcode = hashcode;
766   h->list = list;
767   h->next = list_hash_table[hashcode % TYPE_HASH_SIZE];
768   list_hash_table[hashcode % TYPE_HASH_SIZE] = h;
769 }
770
771 /* Given list components PURPOSE, VALUE, AND CHAIN, return the canonical
772    object for an identical list if one already exists.  Otherwise, build a
773    new one, and record it as the canonical object.  */
774
775 /* Set to 1 to debug without canonicalization.  Never set by program.  */
776
777 static int debug_no_list_hash = 0;
778
779 tree
780 hash_tree_cons (purpose, value, chain)
781      tree purpose, value, chain;
782 {
783   tree t;
784   int hashcode = 0;
785
786   if (! debug_no_list_hash)
787     {
788       hashcode = list_hash (purpose, value, chain);
789       t = list_hash_lookup (hashcode, purpose, value, chain);
790       if (t)
791         return t;
792     }
793
794   t = tree_cons (purpose, value, chain);
795
796   /* If this is a new list, record it for later reuse.  */
797   if (! debug_no_list_hash)
798     list_hash_add (hashcode, t);
799
800   return t;
801 }
802
803 /* Constructor for hashed lists.  */
804
805 tree
806 hash_tree_chain (value, chain)
807      tree value, chain;
808 {
809   return hash_tree_cons (NULL_TREE, value, chain);
810 }
811
812 /* Similar, but used for concatenating two lists.  */
813
814 tree
815 hash_chainon (list1, list2)
816      tree list1, list2;
817 {
818   if (list2 == 0)
819     return list1;
820   if (list1 == 0)
821     return list2;
822   if (TREE_CHAIN (list1) == NULL_TREE)
823     return hash_tree_chain (TREE_VALUE (list1), list2);
824   return hash_tree_chain (TREE_VALUE (list1),
825                           hash_chainon (TREE_CHAIN (list1), list2));
826 }
827 \f
828 /* Build an association between TYPE and some parameters:
829
830    OFFSET is the offset added to `this' to convert it to a pointer
831    of type `TYPE *'
832
833    BINFO is the base binfo to use, if we are deriving from one.  This
834    is necessary, as we want specialized parent binfos from base
835    classes, so that the VTABLE_NAMEs of bases are for the most derived
836    type, instead of the simple type.
837
838    VTABLE is the virtual function table with which to initialize
839    sub-objects of type TYPE.
840
841    VIRTUALS are the virtual functions sitting in VTABLE.  */
842
843 tree
844 make_binfo (offset, binfo, vtable, virtuals)
845      tree offset, binfo;
846      tree vtable, virtuals;
847 {
848   tree new_binfo = make_tree_vec (7);
849   tree type;
850
851   if (TREE_CODE (binfo) == TREE_VEC)
852     type = BINFO_TYPE (binfo);
853   else
854     {
855       type = binfo;
856       binfo = CLASS_TYPE_P (type) ? TYPE_BINFO (binfo) : NULL_TREE;
857     }
858
859   TREE_TYPE (new_binfo) = TYPE_MAIN_VARIANT (type);
860   BINFO_OFFSET (new_binfo) = offset;
861   BINFO_VTABLE (new_binfo) = vtable;
862   BINFO_VIRTUALS (new_binfo) = virtuals;
863   BINFO_VPTR_FIELD (new_binfo) = NULL_TREE;
864
865   if (binfo && BINFO_BASETYPES (binfo) != NULL_TREE)
866     BINFO_BASETYPES (new_binfo) = copy_node (BINFO_BASETYPES (binfo));      
867   return new_binfo;
868 }
869
870 /* Return the binfo value for ELEM in TYPE.  */
871
872 tree
873 binfo_value (elem, type)
874      tree elem;
875      tree type;
876 {
877   if (get_base_distance (elem, type, 0, (tree *)0) == -2)
878     compiler_error ("base class `%s' ambiguous in binfo_value",
879                     TYPE_NAME_STRING (elem));
880   if (elem == type)
881     return TYPE_BINFO (type);
882   if (TREE_CODE (elem) == RECORD_TYPE && TYPE_BINFO (elem) == type)
883     return type;
884   return get_binfo (elem, type, 0);
885 }
886
887 /* Return a reversed copy of the BINFO-chain given by PATH.  (If the 
888    BINFO_INHERITANCE_CHAIN points from base classes to derived
889    classes, it will instead point from derived classes to base
890    classes.)  Returns the first node in the reversed chain.  */
891
892 tree
893 reverse_path (path)
894      tree path;
895 {
896   register tree prev = NULL_TREE, cur;
897   for (cur = path; cur; cur = BINFO_INHERITANCE_CHAIN (cur))
898     {
899       tree r = copy_node (cur);
900       BINFO_INHERITANCE_CHAIN (r) = prev;
901       prev = r;
902     }
903   return prev;
904 }
905
906 void
907 debug_binfo (elem)
908      tree elem;
909 {
910   unsigned HOST_WIDE_INT n;
911   tree virtuals;
912
913   fprintf (stderr, "type \"%s\"; offset = %ld\n",
914            TYPE_NAME_STRING (BINFO_TYPE (elem)),
915            (long) TREE_INT_CST_LOW (BINFO_OFFSET (elem)));
916   fprintf (stderr, "vtable type:\n");
917   debug_tree (BINFO_TYPE (elem));
918   if (BINFO_VTABLE (elem))
919     fprintf (stderr, "vtable decl \"%s\"\n", IDENTIFIER_POINTER (DECL_NAME (BINFO_VTABLE (elem))));
920   else
921     fprintf (stderr, "no vtable decl yet\n");
922   fprintf (stderr, "virtuals:\n");
923   virtuals = skip_rtti_stuff (elem, BINFO_TYPE (elem), &n);
924
925   while (virtuals)
926     {
927       tree fndecl = TREE_VALUE (virtuals);
928       fprintf (stderr, "%s [%ld =? %ld]\n",
929                IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (fndecl)),
930                (long) n, (long) TREE_INT_CST_LOW (DECL_VINDEX (fndecl)));
931       ++n;
932       virtuals = TREE_CHAIN (virtuals);
933     }
934 }
935
936 int
937 count_functions (t)
938      tree t;
939 {
940   int i;
941   if (TREE_CODE (t) == FUNCTION_DECL)
942     return 1;
943   else if (TREE_CODE (t) == OVERLOAD)
944     {
945       for (i=0; t; t = OVL_CHAIN (t))
946         i++;
947       return i;
948     }
949
950   my_friendly_abort (359);
951   return 0;
952 }
953
954 int
955 is_overloaded_fn (x)
956      tree x;
957 {
958   /* A baselink is also considered an overloaded function.  */
959   if (TREE_CODE (x) == OFFSET_REF)
960     x = TREE_OPERAND (x, 1);
961   if (BASELINK_P (x))
962     x = TREE_VALUE (x);
963   return (TREE_CODE (x) == FUNCTION_DECL
964           || TREE_CODE (x) == TEMPLATE_ID_EXPR
965           || DECL_FUNCTION_TEMPLATE_P (x)
966           || TREE_CODE (x) == OVERLOAD);
967 }
968
969 int
970 really_overloaded_fn (x)
971      tree x;
972 {     
973   /* A baselink is also considered an overloaded function.  */
974   if (TREE_CODE (x) == OFFSET_REF)
975     x = TREE_OPERAND (x, 1);
976   if (BASELINK_P (x))
977     x = TREE_VALUE (x);
978   return (TREE_CODE (x) == OVERLOAD 
979           && (TREE_CHAIN (x) != NULL_TREE
980               || DECL_FUNCTION_TEMPLATE_P (OVL_FUNCTION (x))));
981 }
982
983 tree
984 get_first_fn (from)
985      tree from;
986 {
987   my_friendly_assert (is_overloaded_fn (from), 9);
988   /* A baselink is also considered an overloaded function. */
989   if (BASELINK_P (from))
990     from = TREE_VALUE (from);
991   return OVL_CURRENT (from);
992 }
993
994 /* Returns nonzero if T is a ->* or .* expression that refers to a
995    member function.  */
996
997 int
998 bound_pmf_p (t)
999      tree t;
1000 {
1001   return (TREE_CODE (t) == OFFSET_REF
1002           && TYPE_PTRMEMFUNC_P (TREE_TYPE (TREE_OPERAND (t, 1))));
1003 }
1004
1005 /* Return a new OVL node, concatenating it with the old one. */
1006
1007 tree
1008 ovl_cons (decl, chain)
1009      tree decl;
1010      tree chain;
1011 {
1012   tree result = make_node (OVERLOAD);
1013   TREE_TYPE (result) = unknown_type_node;
1014   OVL_FUNCTION (result) = decl;
1015   TREE_CHAIN (result) = chain;
1016   
1017   return result;
1018 }
1019
1020 /* Build a new overloaded function. If this is the first one,
1021    just return it; otherwise, ovl_cons the _DECLs */
1022
1023 tree
1024 build_overload (decl, chain)
1025      tree decl;
1026      tree chain;
1027 {
1028   if (! chain && TREE_CODE (decl) != TEMPLATE_DECL)
1029     return decl;
1030   if (chain && TREE_CODE (chain) != OVERLOAD)
1031     chain = ovl_cons (chain, NULL_TREE);
1032   return ovl_cons (decl, chain);
1033 }
1034
1035 /* True if fn is in ovl. */
1036
1037 int
1038 ovl_member (fn, ovl)
1039      tree fn;
1040      tree ovl;
1041 {
1042   if (ovl == NULL_TREE)
1043     return 0;
1044   if (TREE_CODE (ovl) != OVERLOAD)
1045     return ovl == fn;
1046   for (; ovl; ovl = OVL_CHAIN (ovl))
1047     if (OVL_FUNCTION (ovl) == fn)
1048       return 1;
1049   return 0;
1050 }
1051
1052 int
1053 is_aggr_type_2 (t1, t2)
1054      tree t1, t2;
1055 {
1056   if (TREE_CODE (t1) != TREE_CODE (t2))
1057     return 0;
1058   return IS_AGGR_TYPE (t1) && IS_AGGR_TYPE (t2);
1059 }
1060
1061 /* Returns non-zero if CODE is the code for a statement.  */
1062
1063 static int
1064 statement_code_p (code)
1065      enum tree_code code;
1066 {
1067   switch (code)
1068     {
1069     case EXPR_STMT:
1070     case COMPOUND_STMT:
1071     case DECL_STMT:
1072     case IF_STMT:
1073     case FOR_STMT:
1074     case WHILE_STMT:
1075     case DO_STMT:
1076     case RETURN_STMT:
1077     case BREAK_STMT:
1078     case CONTINUE_STMT:
1079     case SWITCH_STMT:
1080     case GOTO_STMT:
1081     case LABEL_STMT:
1082     case ASM_STMT:
1083     case SUBOBJECT:
1084     case CLEANUP_STMT:
1085     case START_CATCH_STMT:
1086     case CTOR_STMT:
1087     case SCOPE_STMT:
1088     case CTOR_INITIALIZER:
1089     case CASE_LABEL:
1090     case RETURN_INIT:
1091     case TRY_BLOCK:
1092     case HANDLER:
1093       return 1;
1094
1095     default:
1096       return 0;
1097     }
1098 }
1099 \f
1100 #define PRINT_RING_SIZE 4
1101
1102 const char *
1103 lang_printable_name (decl, v)
1104      tree decl;
1105      int v;
1106 {
1107   static tree decl_ring[PRINT_RING_SIZE];
1108   static char *print_ring[PRINT_RING_SIZE];
1109   static int ring_counter;
1110   int i;
1111
1112   /* Only cache functions.  */
1113   if (v < 2
1114       || TREE_CODE (decl) != FUNCTION_DECL
1115       || DECL_LANG_SPECIFIC (decl) == 0)
1116     return lang_decl_name (decl, v);
1117
1118   /* See if this print name is lying around.  */
1119   for (i = 0; i < PRINT_RING_SIZE; i++)
1120     if (decl_ring[i] == decl)
1121       /* yes, so return it.  */
1122       return print_ring[i];
1123
1124   if (++ring_counter == PRINT_RING_SIZE)
1125     ring_counter = 0;
1126
1127   if (current_function_decl != NULL_TREE)
1128     {
1129       if (decl_ring[ring_counter] == current_function_decl)
1130         ring_counter += 1;
1131       if (ring_counter == PRINT_RING_SIZE)
1132         ring_counter = 0;
1133       if (decl_ring[ring_counter] == current_function_decl)
1134         my_friendly_abort (106);
1135     }
1136
1137   if (print_ring[ring_counter])
1138     free (print_ring[ring_counter]);
1139
1140   print_ring[ring_counter] = xstrdup (lang_decl_name (decl, v));
1141   decl_ring[ring_counter] = decl;
1142   return print_ring[ring_counter];
1143 }
1144 \f
1145 /* Build the FUNCTION_TYPE or METHOD_TYPE which may throw exceptions
1146    listed in RAISES.  */
1147
1148 tree
1149 build_exception_variant (type, raises)
1150      tree type;
1151      tree raises;
1152 {
1153   tree v = TYPE_MAIN_VARIANT (type);
1154   int type_quals = TYPE_QUALS (type);
1155
1156   for (; v; v = TYPE_NEXT_VARIANT (v))
1157     if (TYPE_QUALS (v) == type_quals
1158         && comp_except_specs (raises, TYPE_RAISES_EXCEPTIONS (v), 1))
1159       return v;
1160
1161   /* Need to build a new variant.  */
1162   v = build_type_copy (type);
1163   TYPE_RAISES_EXCEPTIONS (v) = raises;
1164   return v;
1165 }
1166
1167 /* Given a TEMPLATE_TEMPLATE_PARM node T, create a new one together with its 
1168    lang_specific field and its corresponding TEMPLATE_DECL node */
1169
1170 tree
1171 copy_template_template_parm (t)
1172      tree t;
1173 {
1174   tree template = TYPE_NAME (t);
1175   tree t2;
1176
1177   t2 = make_aggr_type (TEMPLATE_TEMPLATE_PARM);
1178   template = copy_node (template);
1179   copy_lang_decl (template);
1180
1181   TREE_TYPE (template) = t2;
1182   TYPE_NAME (t2) = template;
1183   TYPE_STUB_DECL (t2) = template;
1184
1185   /* No need to copy these */
1186   TYPE_FIELDS (t2) = TYPE_FIELDS (t);
1187   TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t2) 
1188     = TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t);
1189   return t2;
1190 }
1191
1192 /* Apply FUNC to all the sub-trees of TP in a pre-order traversal.
1193    FUNC is called with the DATA and the address of each sub-tree.  If
1194    FUNC returns a non-NULL value, the traversal is aborted, and the
1195    value returned by FUNC is returned.  */
1196
1197 tree 
1198 walk_tree (tp, func, data)
1199      tree *tp;
1200      walk_tree_fn func;
1201      void *data;
1202 {
1203   enum tree_code code;
1204   int walk_subtrees;
1205   tree result;
1206   
1207 #define WALK_SUBTREE(NODE)                      \
1208   do                                            \
1209     {                                           \
1210       result = walk_tree (&(NODE), func, data); \
1211       if (result)                               \
1212         return result;                          \
1213     }                                           \
1214   while (0)
1215
1216   /* Skip empty subtrees.  */
1217   if (!*tp)
1218     return NULL_TREE;
1219
1220   /* Call the function.  */
1221   walk_subtrees = 1;
1222   result = (*func) (tp, &walk_subtrees, data);
1223
1224   /* If we found something, return it.  */
1225   if (result)
1226     return result;
1227
1228   /* Even if we didn't, FUNC may have decided that there was nothing
1229      interesting below this point in the tree.  */
1230   if (!walk_subtrees)
1231     return NULL_TREE;
1232
1233   code = TREE_CODE (*tp);
1234
1235   /* Handle common cases up front.  */
1236   if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code))
1237       || TREE_CODE_CLASS (code) == 'r'
1238       || TREE_CODE_CLASS (code) == 's')
1239     {
1240       int i, len;
1241
1242       /* Walk over all the sub-trees of this operand.  */
1243       len = first_rtl_op (code);
1244       /* TARGET_EXPRs are peculiar: operands 1 and 3 can be the same.
1245          But, we only want to walk once.  */
1246       if (code == TARGET_EXPR
1247           && TREE_OPERAND (*tp, 3) == TREE_OPERAND (*tp, 1))
1248         --len;
1249       /* Go through the subtrees.  We need to do this in forward order so
1250          that the scope of a FOR_EXPR is handled properly.  */
1251       for (i = 0; i < len; ++i)
1252         WALK_SUBTREE (TREE_OPERAND (*tp, i));
1253
1254       /* For statements, we also walk the chain so that we cover the
1255          entire statement tree.  */
1256       if (statement_code_p (code))
1257         {
1258           if (code == DECL_STMT 
1259               && DECL_STMT_DECL (*tp) 
1260               && TREE_CODE_CLASS (TREE_CODE (DECL_STMT_DECL (*tp))) == 'd')
1261             {
1262               /* Walk the DECL_INITIAL and DECL_SIZE.  We don't want to walk
1263                  into declarations that are just mentioned, rather than
1264                  declared; they don't really belong to this part of the tree.
1265                  And, we can see cycles: the initializer for a declaration can
1266                  refer to the declaration itself.  */
1267               WALK_SUBTREE (DECL_INITIAL (DECL_STMT_DECL (*tp)));
1268               WALK_SUBTREE (DECL_SIZE (DECL_STMT_DECL (*tp)));
1269             }
1270
1271           WALK_SUBTREE (TREE_CHAIN (*tp));
1272         }
1273
1274       /* We didn't find what we were looking for.  */
1275       return NULL_TREE;
1276     }
1277   else if (TREE_CODE_CLASS (code) == 'd')
1278     {
1279       WALK_SUBTREE (TREE_TYPE (*tp));
1280
1281       /* We didn't find what we were looking for.  */
1282       return NULL_TREE;
1283     }
1284
1285   /* Not one of the easy cases.  We must explicitly go through the
1286      children.  */
1287   switch (code)
1288     {
1289     case ERROR_MARK:
1290     case IDENTIFIER_NODE:
1291     case INTEGER_CST:
1292     case REAL_CST:
1293     case STRING_CST:
1294     case DEFAULT_ARG:
1295     case TEMPLATE_TEMPLATE_PARM:
1296     case TEMPLATE_PARM_INDEX:
1297     case TEMPLATE_TYPE_PARM:
1298     case REAL_TYPE:
1299     case COMPLEX_TYPE:
1300     case VOID_TYPE:
1301     case BOOLEAN_TYPE:
1302     case TYPENAME_TYPE:
1303     case UNION_TYPE:
1304     case ENUMERAL_TYPE:
1305     case TYPEOF_TYPE:
1306     case BLOCK:
1307       /* None of thse have subtrees other than those already walked
1308          above.  */
1309       break;
1310
1311     case PTRMEM_CST:
1312       WALK_SUBTREE (TREE_TYPE (*tp));
1313       break;
1314
1315     case POINTER_TYPE:
1316     case REFERENCE_TYPE:
1317       WALK_SUBTREE (TREE_TYPE (*tp));
1318       break;
1319
1320     case TREE_LIST:
1321       WALK_SUBTREE (TREE_PURPOSE (*tp));
1322       WALK_SUBTREE (TREE_VALUE (*tp));
1323       WALK_SUBTREE (TREE_CHAIN (*tp));
1324       break;
1325
1326     case OVERLOAD:
1327       WALK_SUBTREE (OVL_FUNCTION (*tp));
1328       WALK_SUBTREE (OVL_CHAIN (*tp));
1329       break;
1330
1331     case TREE_VEC:
1332       {
1333         int len = TREE_VEC_LENGTH (*tp);
1334         while (len--)
1335           WALK_SUBTREE (TREE_VEC_ELT (*tp, len));
1336       }
1337       break;
1338
1339     case COMPLEX_CST:
1340       WALK_SUBTREE (TREE_REALPART (*tp));
1341       WALK_SUBTREE (TREE_IMAGPART (*tp));
1342       break;
1343
1344     case CONSTRUCTOR:
1345       WALK_SUBTREE (CONSTRUCTOR_ELTS (*tp));
1346       break;
1347
1348     case METHOD_TYPE:
1349       WALK_SUBTREE (TYPE_METHOD_BASETYPE (*tp));
1350       /* Fall through.  */
1351
1352     case FUNCTION_TYPE:
1353       WALK_SUBTREE (TREE_TYPE (*tp));
1354       WALK_SUBTREE (TYPE_ARG_TYPES (*tp));
1355       break;
1356
1357     case ARRAY_TYPE:
1358       WALK_SUBTREE (TREE_TYPE (*tp));
1359       WALK_SUBTREE (TYPE_DOMAIN (*tp));
1360       break;
1361
1362     case INTEGER_TYPE:
1363       WALK_SUBTREE (TYPE_MIN_VALUE (*tp));
1364       WALK_SUBTREE (TYPE_MAX_VALUE (*tp));
1365       break;
1366
1367     case OFFSET_TYPE:
1368       WALK_SUBTREE (TREE_TYPE (*tp));
1369       WALK_SUBTREE (TYPE_OFFSET_BASETYPE (*tp));
1370       break;
1371
1372     case RECORD_TYPE:
1373       if (TYPE_PTRMEMFUNC_P (*tp))
1374         WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (*tp));
1375       break;
1376
1377     default:
1378       my_friendly_abort (19990803);
1379     }
1380
1381   /* We didn't find what we were looking for.  */
1382   return NULL_TREE;
1383
1384 #undef WALK_SUBTREE
1385 }
1386
1387 /* Passed to walk_tree.  Checks for the use of types with no linkage.  */
1388
1389 static tree
1390 no_linkage_helper (tp, walk_subtrees, data)
1391      tree *tp;
1392      int *walk_subtrees ATTRIBUTE_UNUSED;
1393      void *data ATTRIBUTE_UNUSED;
1394 {
1395   tree t = *tp;
1396
1397   if (TYPE_P (t)
1398       && (IS_AGGR_TYPE (t) || TREE_CODE (t) == ENUMERAL_TYPE)
1399       && (decl_function_context (TYPE_MAIN_DECL (t))
1400           || ANON_AGGRNAME_P (TYPE_IDENTIFIER (t))))
1401     return t;
1402   return NULL_TREE;
1403 }
1404
1405 /* Check if the type T depends on a type with no linkage and if so, return
1406    it.  */
1407
1408 tree
1409 no_linkage_check (t)
1410      tree t;
1411 {
1412   /* There's no point in checking linkage on template functions; we
1413      can't know their complete types.  */
1414   if (processing_template_decl)
1415     return NULL_TREE;
1416
1417   t = walk_tree (&t, no_linkage_helper, NULL);
1418   if (t != error_mark_node)
1419     return t;
1420   return NULL_TREE;
1421 }
1422
1423 /* Passed to walk_tree.  Copies the node pointed to, if appropriate.  */
1424
1425 tree
1426 copy_tree_r (tp, walk_subtrees, data)
1427      tree *tp;
1428      int *walk_subtrees;
1429      void *data ATTRIBUTE_UNUSED;
1430 {
1431   enum tree_code code = TREE_CODE (*tp);
1432
1433   /* We make copies of most nodes.  */
1434   if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code))
1435       || TREE_CODE_CLASS (code) == 'r'
1436       || TREE_CODE_CLASS (code) == 'c'
1437       || TREE_CODE_CLASS (code) == 's'
1438       || code == PARM_DECL
1439       || code == TREE_LIST
1440       || code == TREE_VEC
1441       || code == OVERLOAD)
1442     {
1443       /* Because the chain gets clobbered when we make a copy, we save it
1444          here.  */
1445       tree chain = TREE_CHAIN (*tp);
1446
1447       /* Copy the node.  */
1448       *tp = copy_node (*tp);
1449
1450       /* Now, restore the chain, if appropriate.  That will cause
1451          walk_tree to walk into the chain as well.  */
1452       if (code == PARM_DECL || code == TREE_LIST || code == OVERLOAD
1453           || statement_code_p (code))
1454         TREE_CHAIN (*tp) = chain;
1455
1456       /* For now, we don't update BLOCKs when we make copies.  So, we
1457          have to nullify all scope-statements.  */
1458       if (TREE_CODE (*tp) == SCOPE_STMT)
1459         SCOPE_STMT_BLOCK (*tp) = NULL_TREE;
1460     }
1461   else if (code == TEMPLATE_TEMPLATE_PARM)
1462     /* These must be copied specially.  */
1463     *tp = copy_template_template_parm (*tp);
1464   else if (TREE_CODE_CLASS (code) == 't')
1465     /* There's no need to copy types, or anything beneath them.  */
1466     *walk_subtrees = 0;
1467
1468   return NULL_TREE;
1469 }
1470
1471 #ifdef GATHER_STATISTICS
1472 extern int depth_reached;
1473 #endif
1474
1475 void
1476 print_lang_statistics ()
1477 {
1478   print_search_statistics ();
1479   print_class_statistics ();
1480 #ifdef GATHER_STATISTICS
1481   fprintf (stderr, "maximum template instantiation depth reached: %d\n",
1482            depth_reached);
1483 #endif
1484 }
1485
1486 /* This is used by the `assert' macro.  It is provided in libgcc.a,
1487    which `cc' doesn't know how to link.  Note that the C++ front-end
1488    no longer actually uses the `assert' macro (instead, it calls
1489    my_friendly_assert).  But all of the back-end files still need this.  */
1490
1491 void
1492 __eprintf (string, expression, line, filename)
1493      const char *string;
1494      const char *expression;
1495      unsigned line;
1496      const char *filename;
1497 {
1498   fprintf (stderr, string, expression, line, filename);
1499   fflush (stderr);
1500   abort ();
1501 }
1502
1503 /* Return, as an INTEGER_CST node, the number of elements for TYPE
1504    (which is an ARRAY_TYPE).  This counts only elements of the top
1505    array.  */
1506
1507 tree
1508 array_type_nelts_top (type)
1509      tree type;
1510 {
1511   return fold (build (PLUS_EXPR, sizetype,
1512                       array_type_nelts (type),
1513                       integer_one_node));
1514 }
1515
1516 /* Return, as an INTEGER_CST node, the number of elements for TYPE
1517    (which is an ARRAY_TYPE).  This one is a recursive count of all
1518    ARRAY_TYPEs that are clumped together.  */
1519
1520 tree
1521 array_type_nelts_total (type)
1522      tree type;
1523 {
1524   tree sz = array_type_nelts_top (type);
1525   type = TREE_TYPE (type);
1526   while (TREE_CODE (type) == ARRAY_TYPE)
1527     {
1528       tree n = array_type_nelts_top (type);
1529       sz = fold (build (MULT_EXPR, sizetype, sz, n));
1530       type = TREE_TYPE (type);
1531     }
1532   return sz;
1533 }
1534
1535 /* Called from break_out_target_exprs via mapcar.  */
1536
1537 static tree
1538 bot_manip (tp, walk_subtrees, data)
1539      tree *tp;
1540      int *walk_subtrees;
1541      void *data;
1542 {
1543   splay_tree target_remap = ((splay_tree) data);
1544   tree t = *tp;
1545
1546   if (TREE_CODE (t) != TREE_LIST && ! TREE_SIDE_EFFECTS (t))
1547     {
1548       /* There can't be any TARGET_EXPRs below this point.  */
1549       *walk_subtrees = 0;
1550       return NULL_TREE;
1551     }
1552   else if (TREE_CODE (t) == TARGET_EXPR)
1553     {
1554       tree u;
1555
1556       if (TREE_CODE (TREE_OPERAND (t, 1)) == AGGR_INIT_EXPR)
1557         {
1558           mark_used (TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (t, 1), 0), 0));
1559           u = build_cplus_new
1560             (TREE_TYPE (t), break_out_target_exprs (TREE_OPERAND (t, 1)));
1561         }
1562       else 
1563         {
1564           u = copy_node (t);
1565           TREE_OPERAND (u, 0) = build (VAR_DECL, TREE_TYPE (t));
1566           layout_decl (TREE_OPERAND (u, 0), 0);
1567         }
1568
1569       /* Map the old variable to the new one.  */
1570       splay_tree_insert (target_remap, 
1571                          (splay_tree_key) TREE_OPERAND (t, 0), 
1572                          (splay_tree_value) TREE_OPERAND (u, 0));
1573
1574       /* Replace the old expression with the new version.  */
1575       *tp = u;
1576       /* We don't have to go below this point; the recursive call to
1577          break_out_target_exprs will have handled anything below this
1578          point.  */
1579       *walk_subtrees = 0;
1580       return NULL_TREE;
1581     }
1582   else if (TREE_CODE (t) == CALL_EXPR)
1583     mark_used (TREE_OPERAND (TREE_OPERAND (t, 0), 0));
1584
1585   /* Make a copy of this node.  */
1586   return copy_tree_r (tp, walk_subtrees, NULL);
1587 }
1588   
1589 /* Replace all remapped VAR_DECLs in T with their new equivalents.
1590    DATA is really a splay-tree mapping old variables to new
1591    variables.  */
1592
1593 static tree
1594 bot_replace (t, walk_subtrees, data)
1595      tree *t;
1596      int *walk_subtrees ATTRIBUTE_UNUSED;
1597      void *data;
1598 {
1599   splay_tree target_remap = ((splay_tree) data);
1600
1601   if (TREE_CODE (*t) == VAR_DECL)
1602     {
1603       splay_tree_node n = splay_tree_lookup (target_remap,
1604                                              (splay_tree_key) *t);
1605       if (n)
1606         *t = (tree) n->value;
1607     }
1608
1609   return NULL_TREE;
1610 }
1611         
1612 /* When we parse a default argument expression, we may create
1613    temporary variables via TARGET_EXPRs.  When we actually use the
1614    default-argument expression, we make a copy of the expression, but
1615    we must replace the temporaries with appropriate local versions.  */
1616
1617 tree
1618 break_out_target_exprs (t)
1619      tree t;
1620 {
1621   static int target_remap_count;
1622   static splay_tree target_remap;
1623
1624   if (!target_remap_count++)
1625     target_remap = splay_tree_new (splay_tree_compare_pointers, 
1626                                    /*splay_tree_delete_key_fn=*/NULL, 
1627                                    /*splay_tree_delete_value_fn=*/NULL);
1628   walk_tree (&t, bot_manip, target_remap);
1629   walk_tree (&t, bot_replace, target_remap);
1630
1631   if (!--target_remap_count)
1632     {
1633       splay_tree_delete (target_remap);
1634       target_remap = NULL;
1635     }
1636
1637   return t;
1638 }
1639
1640 /* Obstack used for allocating nodes in template function and variable
1641    definitions.  */
1642
1643 /* Similar to `build_nt', except that we set TREE_COMPLEXITY to be the
1644    current line number.  */
1645
1646 tree
1647 build_min_nt VPROTO((enum tree_code code, ...))
1648 {
1649 #ifndef ANSI_PROTOTYPES
1650   enum tree_code code;
1651 #endif
1652   va_list p;
1653   register tree t;
1654   register int length;
1655   register int i;
1656
1657   VA_START (p, code);
1658
1659 #ifndef ANSI_PROTOTYPES
1660   code = va_arg (p, enum tree_code);
1661 #endif
1662
1663   t = make_node (code);
1664   length = tree_code_length[(int) code];
1665   TREE_COMPLEXITY (t) = lineno;
1666
1667   for (i = 0; i < length; i++)
1668     {
1669       tree x = va_arg (p, tree);
1670       TREE_OPERAND (t, i) = x;
1671     }
1672
1673   va_end (p);
1674   return t;
1675 }
1676
1677 /* Similar to `build', except we set TREE_COMPLEXITY to the current
1678    line-number.  */
1679
1680 tree
1681 build_min VPROTO((enum tree_code code, tree tt, ...))
1682 {
1683 #ifndef ANSI_PROTOTYPES
1684   enum tree_code code;
1685   tree tt;
1686 #endif
1687   va_list p;
1688   register tree t;
1689   register int length;
1690   register int i;
1691
1692   VA_START (p, tt);
1693
1694 #ifndef ANSI_PROTOTYPES
1695   code = va_arg (p, enum tree_code);
1696   tt = va_arg (p, tree);
1697 #endif
1698
1699   t = make_node (code);
1700   length = tree_code_length[(int) code];
1701   TREE_TYPE (t) = tt;
1702   TREE_COMPLEXITY (t) = lineno;
1703
1704   for (i = 0; i < length; i++)
1705     {
1706       tree x = va_arg (p, tree);
1707       TREE_OPERAND (t, i) = x;
1708     }
1709
1710   va_end (p);
1711   return t;
1712 }
1713
1714 tree
1715 get_type_decl (t)
1716      tree t;
1717 {
1718   if (TREE_CODE (t) == TYPE_DECL)
1719     return t;
1720   if (TREE_CODE_CLASS (TREE_CODE (t)) == 't')
1721     return TYPE_STUB_DECL (t);
1722   
1723   my_friendly_abort (42);
1724
1725   /* Stop compiler from complaining control reaches end of non-void function.  */
1726   return 0;
1727 }
1728
1729 int
1730 can_free (obstack, t)
1731      struct obstack *obstack;
1732      tree t;
1733 {
1734   int size = 0;
1735
1736   if (TREE_CODE (t) == TREE_VEC)
1737     size = (TREE_VEC_LENGTH (t)-1) * sizeof (tree) + sizeof (struct tree_vec);
1738   else
1739     my_friendly_abort (42);
1740
1741 #define ROUND(x) ((x + obstack_alignment_mask (obstack)) \
1742                   & ~ obstack_alignment_mask (obstack))
1743   if ((char *)t + ROUND (size) == obstack_next_free (obstack))
1744     return 1;
1745 #undef ROUND
1746
1747   return 0;
1748 }
1749
1750 /* Return first vector element whose BINFO_TYPE is ELEM.
1751    Return 0 if ELEM is not in VEC.  VEC may be NULL_TREE.  */
1752
1753 tree
1754 vec_binfo_member (elem, vec)
1755      tree elem, vec;
1756 {
1757   int i;
1758
1759   if (vec)
1760     for (i = 0; i < TREE_VEC_LENGTH (vec); ++i)
1761       if (same_type_p (elem, BINFO_TYPE (TREE_VEC_ELT (vec, i))))
1762         return TREE_VEC_ELT (vec, i);
1763
1764   return NULL_TREE;
1765 }
1766
1767 /* Kludge around the fact that DECL_CONTEXT for virtual functions returns
1768    the wrong thing for decl_function_context.  Hopefully the uses in the
1769    backend won't matter, since we don't need a static chain for local class
1770    methods.  FIXME!  */
1771
1772 tree
1773 hack_decl_function_context (decl)
1774      tree decl;
1775 {
1776   if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FUNCTION_MEMBER_P (decl))
1777     return decl_function_context (TYPE_MAIN_DECL (DECL_CLASS_CONTEXT (decl)));
1778   return decl_function_context (decl);
1779 }
1780
1781 /* Returns the namespace that contains DECL, whether directly or
1782    indirectly.  */
1783
1784 tree
1785 decl_namespace_context (decl)
1786      tree decl;
1787 {
1788   while (1)
1789     {
1790       if (TREE_CODE (decl) == NAMESPACE_DECL)
1791         return decl;
1792       else if (TYPE_P (decl))
1793         decl = CP_DECL_CONTEXT (TYPE_MAIN_DECL (decl));
1794       else
1795         decl = CP_DECL_CONTEXT (decl);
1796     }
1797 }
1798
1799 /* Return truthvalue of whether T1 is the same tree structure as T2.
1800    Return 1 if they are the same.
1801    Return 0 if they are understandably different.
1802    Return -1 if either contains tree structure not understood by
1803    this function.  */
1804
1805 int
1806 cp_tree_equal (t1, t2)
1807      tree t1, t2;
1808 {
1809   register enum tree_code code1, code2;
1810   int cmp;
1811
1812   if (t1 == t2)
1813     return 1;
1814   if (t1 == 0 || t2 == 0)
1815     return 0;
1816
1817   code1 = TREE_CODE (t1);
1818   code2 = TREE_CODE (t2);
1819
1820   if (code1 == NOP_EXPR || code1 == CONVERT_EXPR || code1 == NON_LVALUE_EXPR)
1821     {
1822       if (code2 == NOP_EXPR || code2 == CONVERT_EXPR || code2 == NON_LVALUE_EXPR)
1823         return cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
1824       else
1825         return cp_tree_equal (TREE_OPERAND (t1, 0), t2);
1826     }
1827   else if (code2 == NOP_EXPR || code2 == CONVERT_EXPR
1828            || code2 == NON_LVALUE_EXPR)
1829     return cp_tree_equal (t1, TREE_OPERAND (t2, 0));
1830
1831   if (code1 != code2)
1832     return 0;
1833
1834   switch (code1)
1835     {
1836     case INTEGER_CST:
1837       return TREE_INT_CST_LOW (t1) == TREE_INT_CST_LOW (t2)
1838         && TREE_INT_CST_HIGH (t1) == TREE_INT_CST_HIGH (t2);
1839
1840     case REAL_CST:
1841       return REAL_VALUES_EQUAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
1842
1843     case STRING_CST:
1844       return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
1845         && !bcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
1846                   TREE_STRING_LENGTH (t1));
1847
1848     case CONSTRUCTOR:
1849       /* We need to do this when determining whether or not two
1850          non-type pointer to member function template arguments
1851          are the same.  */
1852       if (!(same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))
1853             /* The first operand is RTL.  */
1854             && TREE_OPERAND (t1, 0) == TREE_OPERAND (t2, 0)))
1855         return 0;
1856       return cp_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
1857
1858     case TREE_LIST:
1859       cmp = cp_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2));
1860       if (cmp <= 0)
1861         return cmp;
1862       cmp = cp_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2));
1863       if (cmp <= 0)
1864         return cmp;
1865       return cp_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
1866
1867     case SAVE_EXPR:
1868       return cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
1869
1870     case CALL_EXPR:
1871       cmp = cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
1872       if (cmp <= 0)
1873         return cmp;
1874       return simple_cst_list_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
1875
1876     case TARGET_EXPR:
1877       /* Special case: if either target is an unallocated VAR_DECL,
1878          it means that it's going to be unified with whatever the
1879          TARGET_EXPR is really supposed to initialize, so treat it
1880          as being equivalent to anything.  */
1881       if ((TREE_CODE (TREE_OPERAND (t1, 0)) == VAR_DECL
1882            && DECL_NAME (TREE_OPERAND (t1, 0)) == NULL_TREE
1883            && DECL_RTL (TREE_OPERAND (t1, 0)) == 0)
1884           || (TREE_CODE (TREE_OPERAND (t2, 0)) == VAR_DECL
1885               && DECL_NAME (TREE_OPERAND (t2, 0)) == NULL_TREE
1886               && DECL_RTL (TREE_OPERAND (t2, 0)) == 0))
1887         cmp = 1;
1888       else
1889         cmp = cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
1890       if (cmp <= 0)
1891         return cmp;
1892       return cp_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
1893
1894     case WITH_CLEANUP_EXPR:
1895       cmp = cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
1896       if (cmp <= 0)
1897         return cmp;
1898       return cp_tree_equal (TREE_OPERAND (t1, 2), TREE_OPERAND (t1, 2));
1899
1900     case COMPONENT_REF:
1901       if (TREE_OPERAND (t1, 1) == TREE_OPERAND (t2, 1))
1902         return cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
1903       return 0;
1904
1905     case VAR_DECL:
1906     case PARM_DECL:
1907     case CONST_DECL:
1908     case FUNCTION_DECL:
1909       return 0;
1910
1911     case TEMPLATE_PARM_INDEX:
1912       return TEMPLATE_PARM_IDX (t1) == TEMPLATE_PARM_IDX (t2)
1913         && TEMPLATE_PARM_LEVEL (t1) == TEMPLATE_PARM_LEVEL (t2);
1914
1915     case SIZEOF_EXPR:
1916     case ALIGNOF_EXPR:
1917       if (TREE_CODE (TREE_OPERAND (t1, 0)) != TREE_CODE (TREE_OPERAND (t2, 0)))
1918         return 0;
1919       if (TREE_CODE_CLASS (TREE_CODE (TREE_OPERAND (t1, 0))) == 't')
1920         return same_type_p (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
1921       break;
1922
1923     case PTRMEM_CST:
1924       /* Two pointer-to-members are the same if they point to the same
1925          field or function in the same class.  */
1926       return (PTRMEM_CST_MEMBER (t1) == PTRMEM_CST_MEMBER (t2)
1927               && same_type_p (PTRMEM_CST_CLASS (t1), PTRMEM_CST_CLASS (t2)));
1928
1929     default:
1930       break;
1931     }
1932
1933   switch (TREE_CODE_CLASS (code1))
1934     {
1935       int i;
1936     case '1':
1937     case '2':
1938     case '<':
1939     case 'e':
1940     case 'r':
1941     case 's':
1942       cmp = 1;
1943       for (i=0; i<tree_code_length[(int) code1]; ++i)
1944         {
1945           cmp = cp_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i));
1946           if (cmp <= 0)
1947             return cmp;
1948         }
1949       return cmp;
1950     }
1951
1952   return -1;
1953 }
1954
1955 /* Build a wrapper around some pointer PTR so we can use it as a tree.  */
1956
1957 tree
1958 build_ptr_wrapper (ptr)
1959      void *ptr;
1960 {
1961   tree t = make_node (WRAPPER);
1962   WRAPPER_PTR (t) = ptr;
1963   return t;
1964 }
1965
1966 /* Same, but on the expression_obstack.  */
1967
1968 tree
1969 build_expr_ptr_wrapper (ptr)
1970      void *ptr;
1971 {
1972   return build_ptr_wrapper (ptr);
1973 }
1974
1975 /* Build a wrapper around some integer I so we can use it as a tree.  */
1976
1977 tree
1978 build_int_wrapper (i)
1979      int i;
1980 {
1981   tree t = make_node (WRAPPER);
1982   WRAPPER_INT (t) = i;
1983   return t;
1984 }
1985
1986 static tree
1987 build_srcloc (file, line)
1988      char *file;
1989      int line;
1990 {
1991   tree t;
1992
1993   t = make_node (SRCLOC);
1994   SRCLOC_FILE (t) = file;
1995   SRCLOC_LINE (t) = line;
1996
1997   return t;
1998 }
1999
2000 tree
2001 build_srcloc_here ()
2002 {
2003   return build_srcloc (input_filename, lineno);
2004 }
2005
2006 /* The type of ARG when used as an lvalue.  */
2007
2008 tree
2009 lvalue_type (arg)
2010      tree arg;
2011 {
2012   tree type = TREE_TYPE (arg);
2013   if (TREE_CODE (arg) == OVERLOAD)
2014     type = unknown_type_node;
2015   return type;
2016 }
2017
2018 /* The type of ARG for printing error messages; denote lvalues with
2019    reference types.  */
2020
2021 tree
2022 error_type (arg)
2023      tree arg;
2024 {
2025   tree type = TREE_TYPE (arg);
2026   if (TREE_CODE (type) == ARRAY_TYPE)
2027     ;
2028   else if (real_lvalue_p (arg))
2029     type = build_reference_type (lvalue_type (arg));
2030   else if (IS_AGGR_TYPE (type))
2031     type = lvalue_type (arg);
2032
2033   return type;
2034 }
2035
2036 /* Does FUNCTION use a variable-length argument list?  */
2037
2038 int
2039 varargs_function_p (function)
2040      tree function;
2041 {
2042   tree parm = TYPE_ARG_TYPES (TREE_TYPE (function));
2043   for (; parm; parm = TREE_CHAIN (parm))
2044     if (TREE_VALUE (parm) == void_type_node)
2045       return 0;
2046   return 1;
2047 }
2048
2049 /* Returns 1 if decl is a member of a class.  */
2050
2051 int
2052 member_p (decl)
2053      tree decl;
2054 {
2055   tree ctx = DECL_CONTEXT (decl);
2056   return (ctx && TREE_CODE_CLASS (TREE_CODE (ctx)) == 't');
2057 }
2058
2059 /* Create a placeholder for member access where we don't actually have an
2060    object that the access is against.  */
2061
2062 tree
2063 build_dummy_object (type)
2064      tree type;
2065 {
2066   tree decl = build1 (NOP_EXPR, build_pointer_type (type), void_zero_node);
2067   return build_indirect_ref (decl, NULL_PTR);
2068 }
2069
2070 /* We've gotten a reference to a member of TYPE.  Return *this if appropriate,
2071    or a dummy object otherwise.  If BINFOP is non-0, it is filled with the
2072    binfo path from current_class_type to TYPE, or 0.  */
2073
2074 tree
2075 maybe_dummy_object (type, binfop)
2076      tree type;
2077      tree *binfop;
2078 {
2079   tree decl, context;
2080
2081   if (current_class_type
2082       && get_base_distance (type, current_class_type, 0, binfop) != -1)
2083     context = current_class_type;
2084   else
2085     {
2086       /* Reference from a nested class member function.  */
2087       context = type;
2088       if (binfop)
2089         *binfop = TYPE_BINFO (type);
2090     }
2091
2092   if (current_class_ref && context == current_class_type)
2093     decl = current_class_ref;
2094   else
2095     decl = build_dummy_object (context);
2096
2097   return decl;
2098 }
2099
2100 /* Returns 1 if OB is a placeholder object, or a pointer to one.  */
2101
2102 int
2103 is_dummy_object (ob)
2104      tree ob;
2105 {
2106   if (TREE_CODE (ob) == INDIRECT_REF)
2107     ob = TREE_OPERAND (ob, 0);
2108   return (TREE_CODE (ob) == NOP_EXPR
2109           && TREE_OPERAND (ob, 0) == void_zero_node);
2110 }
2111
2112 /* Returns 1 iff type T is a POD type, as defined in [basic.types].  */
2113
2114 int
2115 pod_type_p (t)
2116      tree t;
2117 {
2118   while (TREE_CODE (t) == ARRAY_TYPE)
2119     t = TREE_TYPE (t);
2120
2121   if (INTEGRAL_TYPE_P (t))
2122     return 1;  /* integral, character or enumeral type */
2123   if (FLOAT_TYPE_P (t))
2124     return 1;
2125   if (TYPE_PTR_P (t))
2126     return 1; /* pointer to non-member */
2127   if (TYPE_PTRMEM_P (t))
2128     return 1; /* pointer to member object */
2129   if (TYPE_PTRMEMFUNC_P (t))
2130     return 1; /* pointer to member function */
2131   
2132   if (! CLASS_TYPE_P (t))
2133     return 0; /* other non-class type (reference or function) */
2134   if (CLASSTYPE_NON_POD_P (t))
2135     return 0;
2136   return 1;
2137 }
2138
2139 /* Return a 1 if ATTR_NAME and ATTR_ARGS denote a valid C++-specific
2140    attribute for either declaration DECL or type TYPE and 0 otherwise.
2141    Plugged into valid_lang_attribute.  */
2142
2143 int
2144 cp_valid_lang_attribute (attr_name, attr_args, decl, type)
2145   tree attr_name;
2146   tree attr_args ATTRIBUTE_UNUSED;
2147   tree decl ATTRIBUTE_UNUSED;
2148   tree type ATTRIBUTE_UNUSED;
2149 {
2150   if (is_attribute_p ("com_interface", attr_name))
2151     {
2152       if (! flag_vtable_thunks)
2153         {
2154           error ("`com_interface' only supported with -fvtable-thunks");
2155           return 0;
2156         }
2157
2158       if (attr_args != NULL_TREE
2159           || decl != NULL_TREE
2160           || ! CLASS_TYPE_P (type)
2161           || type != TYPE_MAIN_VARIANT (type))
2162         {
2163           warning ("`com_interface' attribute can only be applied to class definitions");
2164           return 0;
2165         }
2166
2167       CLASSTYPE_COM_INTERFACE (type) = 1;
2168       return 1;
2169     }
2170   else if (is_attribute_p ("init_priority", attr_name))
2171     {
2172       tree initp_expr = (attr_args ? TREE_VALUE (attr_args): NULL_TREE);
2173       int pri;
2174
2175       if (initp_expr)
2176         STRIP_NOPS (initp_expr);
2177           
2178       if (!initp_expr || TREE_CODE (initp_expr) != INTEGER_CST)
2179         {
2180           error ("requested init_priority is not an integer constant");
2181           return 0;
2182         }
2183
2184       pri = TREE_INT_CST_LOW (initp_expr);
2185         
2186       while (TREE_CODE (type) == ARRAY_TYPE)
2187         type = TREE_TYPE (type);
2188
2189       if (decl == NULL_TREE
2190           || TREE_CODE (decl) != VAR_DECL
2191           || ! TREE_STATIC (decl)
2192           || DECL_EXTERNAL (decl)
2193           || (TREE_CODE (type) != RECORD_TYPE
2194               && TREE_CODE (type) != UNION_TYPE)
2195           /* Static objects in functions are initialized the
2196              first time control passes through that
2197              function. This is not precise enough to pin down an
2198              init_priority value, so don't allow it. */
2199           || current_function_decl) 
2200         {
2201           error ("can only use init_priority attribute on file-scope definitions of objects of class type");
2202           return 0;
2203         }
2204
2205       if (pri > MAX_INIT_PRIORITY || pri <= 0)
2206         {
2207           error ("requested init_priority is out of range");
2208           return 0;
2209         }
2210
2211       /* Check for init_priorities that are reserved for
2212          language and runtime support implementations.*/
2213       if (pri <= MAX_RESERVED_INIT_PRIORITY)
2214         {
2215           warning 
2216             ("requested init_priority is reserved for internal use");
2217         }
2218
2219       DECL_INIT_PRIORITY (decl) = pri;
2220       return 1;
2221     }
2222
2223   return 0;
2224 }
2225
2226 /* Return a new PTRMEM_CST of the indicated TYPE.  The MEMBER is the
2227    thing pointed to by the constant.  */
2228
2229 tree
2230 make_ptrmem_cst (type, member)
2231      tree type;
2232      tree member;
2233 {
2234   tree ptrmem_cst = make_node (PTRMEM_CST);
2235   /* If would seem a great convenience if make_node would set
2236      TREE_CONSTANT for things of class `c', but it does not.  */
2237   TREE_CONSTANT (ptrmem_cst) = 1;
2238   TREE_TYPE (ptrmem_cst) = type;
2239   PTRMEM_CST_MEMBER (ptrmem_cst) = member;
2240   return ptrmem_cst;
2241 }
2242
2243 /* Mark ARG (which is really a list_hash_table **) for GC.  */
2244
2245 static void
2246 mark_list_hash (arg)
2247      void *arg;
2248 {
2249   struct list_hash *lh;
2250
2251   for (lh = * ((struct list_hash **) arg); lh; lh = lh->next)
2252     ggc_mark_tree (lh->list);
2253 }
2254
2255 /* Initialize tree.c.  */
2256
2257 void
2258 init_tree ()
2259 {
2260   make_lang_type_fn = cp_make_lang_type;
2261   lang_unsave = cp_unsave;
2262   ggc_add_root (list_hash_table, 
2263                 sizeof (list_hash_table) / sizeof (struct list_hash *),
2264                 sizeof (struct list_hash *),
2265                 mark_list_hash);
2266 }
2267
2268 /* The SAVE_EXPR pointed to by TP is being copied.  If ST contains
2269    information indicating to what new SAVE_EXPR this one should be
2270    mapped, use that one.  Otherwise, create a new node and enter it in
2271    ST.  FN is the function into which the copy will be placed.  */
2272
2273 void
2274 remap_save_expr (tp, st, fn, walk_subtrees)
2275      tree *tp;
2276      splay_tree st;
2277      tree fn;
2278      int *walk_subtrees;
2279 {
2280   splay_tree_node n;
2281
2282   /* See if we already encountered this SAVE_EXPR.  */
2283   n = splay_tree_lookup (st, (splay_tree_key) *tp);
2284       
2285   /* If we didn't already remap this SAVE_EXPR, do so now.  */
2286   if (!n)
2287     {
2288       tree t = copy_node (*tp);
2289
2290       /* The SAVE_EXPR is now part of the function into which we
2291          are inlining this body.  */
2292       SAVE_EXPR_CONTEXT (t) = fn;
2293       /* And we haven't evaluated it yet.  */
2294       SAVE_EXPR_RTL (t) = NULL_RTX;
2295       /* Remember this SAVE_EXPR.  */
2296       n = splay_tree_insert (st,
2297                              (splay_tree_key) *tp,
2298                              (splay_tree_value) t);
2299     }
2300   else
2301     /* We've already walked into this SAVE_EXPR, so we needn't do it
2302        again.  */
2303     *walk_subtrees = 0;
2304
2305   /* Replace this SAVE_EXPR with the copy.  */
2306   *tp = (tree) n->value;
2307 }
2308
2309 /* Called via walk_tree.  If *TP points to a DECL_STMT for a local
2310    declaration, copies the declaration and enters it in the splay_tree
2311    pointed to by DATA (which is really a `splay_tree *').  */
2312
2313 static tree
2314 mark_local_for_remap_r (tp, walk_subtrees, data)
2315      tree *tp;
2316      int *walk_subtrees ATTRIBUTE_UNUSED;
2317      void *data;
2318 {
2319   tree t = *tp;
2320   splay_tree st = (splay_tree) data;
2321
2322   if ((TREE_CODE (t) == DECL_STMT
2323        && nonstatic_local_decl_p (DECL_STMT_DECL (t)))
2324       || TREE_CODE (t) == LABEL_STMT)
2325     {
2326       tree decl;
2327       tree copy;
2328
2329       /* Figure out what's being declared.  */
2330       decl = (TREE_CODE (t) == DECL_STMT
2331               ? DECL_STMT_DECL (t) : LABEL_STMT_LABEL (t));
2332       
2333       /* Make a copy.  */
2334       copy = copy_decl_for_inlining (decl, 
2335                                      DECL_CONTEXT (decl), 
2336                                      DECL_CONTEXT (decl));
2337
2338       /* Remember the copy.  */
2339       splay_tree_insert (st,
2340                          (splay_tree_key) decl, 
2341                          (splay_tree_value) copy);
2342     }
2343
2344   return NULL_TREE;
2345 }
2346
2347 /* Called via walk_tree when an expression is unsaved.  Using the
2348    splay_tree pointed to by ST (which is really a `splay_tree *'),
2349    remaps all local declarations to appropriate replacements.  */
2350
2351 static tree
2352 cp_unsave_r (tp, walk_subtrees, data)
2353      tree *tp;
2354      int *walk_subtrees;
2355      void *data;
2356 {
2357   splay_tree st = (splay_tree) data;
2358   splay_tree_node n;
2359
2360   /* Only a local declaration (variable or label).  */
2361   if (nonstatic_local_decl_p (*tp))
2362     {
2363       /* Lookup the declaration.  */
2364       n = splay_tree_lookup (st, (splay_tree_key) *tp);
2365       
2366       /* If it's there, remap it.  */
2367       if (n)
2368         *tp = (tree) n->value;
2369     }
2370   else if (TREE_CODE (*tp) == SAVE_EXPR)
2371     remap_save_expr (tp, st, current_function_decl, walk_subtrees);
2372   else
2373     {
2374       copy_tree_r (tp, walk_subtrees, NULL);
2375
2376       /* Do whatever unsaving is required.  */
2377       unsave_expr_1 (*tp);
2378     }
2379
2380   /* Keep iterating.  */
2381   return NULL_TREE;
2382 }
2383
2384 /* Called by unsave_expr_now whenever an expression (*TP) needs to be
2385    unsaved.  */
2386
2387 static void
2388 cp_unsave (tp)
2389      tree *tp;
2390 {
2391   splay_tree st;
2392
2393   /* Create a splay-tree to map old local variable declarations to new
2394      ones.  */
2395   st = splay_tree_new (splay_tree_compare_pointers, NULL, NULL);
2396
2397   /* Walk the tree once figuring out what needs to be remapped.  */
2398   walk_tree (tp, mark_local_for_remap_r, st);
2399
2400   /* Walk the tree again, copying, remapping, and unsaving.  */
2401   walk_tree (tp, cp_unsave_r, st);
2402
2403   /* Clean up.  */
2404   splay_tree_delete (st);
2405 }