OSDN Git Service

b5fc508664ef3626a548a26bc992db7513b5b2b1
[pf3gnuchains/gcc-fork.git] / gcc / cp / tree.c
1 /* Language-dependent node constructors for parse phase of GNU compiler.
2    Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3    1999, 2000, 2001 Free Software Foundation, Inc.
4    Hacked by Michael Tiemann (tiemann@cygnus.com)
5
6 This file is part of GNU CC.
7
8 GNU CC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
12
13 GNU CC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GNU CC; see the file COPYING.  If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA.  */
22
23 #include "config.h"
24 #include "system.h"
25 #include "obstack.h"
26 #include "tree.h"
27 #include "cp-tree.h"
28 #include "flags.h"
29 #include "rtl.h"
30 #include "toplev.h"
31 #include "ggc.h"
32 #include "insn-config.h"
33 #include "integrate.h"
34 #include "tree-inline.h"
35
36 static tree bot_manip PARAMS ((tree *, int *, void *));
37 static tree bot_replace PARAMS ((tree *, int *, void *));
38 static tree build_cplus_array_type_1 PARAMS ((tree, tree));
39 static int list_hash_eq PARAMS ((const void *, const void *));
40 static hashval_t list_hash_pieces PARAMS ((tree, tree, tree));
41 static hashval_t list_hash PARAMS ((const void *));
42 static cp_lvalue_kind lvalue_p_1 PARAMS ((tree, int));
43 static tree no_linkage_helper PARAMS ((tree *, int *, void *));
44 static tree build_srcloc PARAMS ((const char *, int));
45 static tree mark_local_for_remap_r PARAMS ((tree *, int *, void *));
46 static tree cp_unsave_r PARAMS ((tree *, int *, void *));
47 static void cp_unsave PARAMS ((tree *));
48 static tree build_target_expr PARAMS ((tree, tree));
49 static tree count_trees_r PARAMS ((tree *, int *, void *));
50 static tree verify_stmt_tree_r PARAMS ((tree *, int *, void *));
51 static tree find_tree_r PARAMS ((tree *, int *, void *));
52 extern int cp_statement_code_p PARAMS ((enum tree_code));
53 static treeopt_walk_subtrees_type cp_walk_subtrees;
54 static treeopt_cannot_inline_tree_fn_type cp_cannot_inline_tree_fn;
55 static treeopt_add_pending_fn_decls_type cp_add_pending_fn_decls;
56 static treeopt_tree_chain_matters_p_type cp_is_overload_p;
57 static treeopt_auto_var_in_fn_p_type cp_auto_var_in_fn_p;
58 static treeopt_copy_res_decl_for_inlining_type cp_copy_res_decl_for_inlining;
59
60 static tree handle_java_interface_attribute PARAMS ((tree *, tree, tree, int, bool *));
61 static tree handle_com_interface_attribute PARAMS ((tree *, tree, tree, int, bool *));
62 static tree handle_init_priority_attribute PARAMS ((tree *, tree, tree, int, bool *));
63
64 /* If REF is an lvalue, returns the kind of lvalue that REF is.
65    Otherwise, returns clk_none.  If TREAT_CLASS_RVALUES_AS_LVALUES is
66    non-zero, rvalues of class type are considered lvalues.  */
67
68 static cp_lvalue_kind
69 lvalue_p_1 (ref, treat_class_rvalues_as_lvalues)
70      tree ref;
71      int treat_class_rvalues_as_lvalues;
72 {
73   cp_lvalue_kind op1_lvalue_kind = clk_none;
74   cp_lvalue_kind op2_lvalue_kind = clk_none;
75
76   if (TREE_CODE (TREE_TYPE (ref)) == REFERENCE_TYPE)
77     return clk_ordinary;
78
79   if (ref == current_class_ptr)
80     return clk_none;
81
82   switch (TREE_CODE (ref))
83     {
84       /* preincrements and predecrements are valid lvals, provided
85          what they refer to are valid lvals.  */
86     case PREINCREMENT_EXPR:
87     case PREDECREMENT_EXPR:
88     case SAVE_EXPR:
89     case UNSAVE_EXPR:
90     case TRY_CATCH_EXPR:
91     case WITH_CLEANUP_EXPR:
92     case REALPART_EXPR:
93     case IMAGPART_EXPR:
94       /* This shouldn't be here, but there are lots of places in the compiler
95          that are sloppy about tacking on NOP_EXPRs to the same type when
96          no actual conversion is happening.  */
97     case NOP_EXPR:
98       return lvalue_p_1 (TREE_OPERAND (ref, 0),
99                          treat_class_rvalues_as_lvalues);
100
101     case COMPONENT_REF:
102       op1_lvalue_kind = lvalue_p_1 (TREE_OPERAND (ref, 0),
103                                     treat_class_rvalues_as_lvalues);
104       if (op1_lvalue_kind 
105           /* The "field" can be a FUNCTION_DECL or an OVERLOAD in some
106              situations.  */
107           && TREE_CODE (TREE_OPERAND (ref, 1)) == FIELD_DECL
108           && DECL_C_BIT_FIELD (TREE_OPERAND (ref, 1)))
109         {
110           /* Clear the ordinary bit.  If this object was a class
111              rvalue we want to preserve that information.  */
112           op1_lvalue_kind &= ~clk_ordinary;
113           /* The lvalue is for a btifield.  */
114           op1_lvalue_kind |= clk_bitfield;
115         }
116       return op1_lvalue_kind;
117
118     case STRING_CST:
119       return clk_ordinary;
120
121     case VAR_DECL:
122       if (TREE_READONLY (ref) && ! TREE_STATIC (ref)
123           && DECL_LANG_SPECIFIC (ref)
124           && DECL_IN_AGGR_P (ref))
125         return clk_none;
126     case INDIRECT_REF:
127     case ARRAY_REF:
128     case PARM_DECL:
129     case RESULT_DECL:
130       if (TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE)
131         return clk_ordinary;
132       break;
133
134       /* A currently unresolved scope ref.  */
135     case SCOPE_REF:
136       my_friendly_abort (103);
137     case OFFSET_REF:
138       if (TREE_CODE (TREE_OPERAND (ref, 1)) == FUNCTION_DECL)
139         return clk_ordinary;
140       /* Fall through.  */
141     case MAX_EXPR:
142     case MIN_EXPR:
143       op1_lvalue_kind = lvalue_p_1 (TREE_OPERAND (ref, 0),
144                                     treat_class_rvalues_as_lvalues);
145       op2_lvalue_kind = lvalue_p_1 (TREE_OPERAND (ref, 1),
146                                     treat_class_rvalues_as_lvalues);
147       break;
148
149     case COND_EXPR:
150       op1_lvalue_kind = lvalue_p_1 (TREE_OPERAND (ref, 1),
151                                     treat_class_rvalues_as_lvalues);
152       op2_lvalue_kind = lvalue_p_1 (TREE_OPERAND (ref, 2),
153                                     treat_class_rvalues_as_lvalues);
154       break;
155
156     case MODIFY_EXPR:
157       return clk_ordinary;
158
159     case COMPOUND_EXPR:
160       return lvalue_p_1 (TREE_OPERAND (ref, 1),
161                          treat_class_rvalues_as_lvalues);
162
163     case TARGET_EXPR:
164       return treat_class_rvalues_as_lvalues ? clk_class : clk_none;
165
166     case CALL_EXPR:
167     case VA_ARG_EXPR:
168       return ((treat_class_rvalues_as_lvalues
169                && IS_AGGR_TYPE (TREE_TYPE (ref)))
170               ? clk_class : clk_none);
171
172     case FUNCTION_DECL:
173       /* All functions (except non-static-member functions) are
174          lvalues.  */
175       return (DECL_NONSTATIC_MEMBER_FUNCTION_P (ref) 
176               ? clk_none : clk_ordinary);
177
178     default:
179       break;
180     }
181
182   /* If one operand is not an lvalue at all, then this expression is
183      not an lvalue.  */
184   if (!op1_lvalue_kind || !op2_lvalue_kind)
185     return clk_none;
186
187   /* Otherwise, it's an lvalue, and it has all the odd properties
188      contributed by either operand.  */
189   op1_lvalue_kind = op1_lvalue_kind | op2_lvalue_kind;
190   /* It's not an ordinary lvalue if it involves either a bit-field or
191      a class rvalue.  */
192   if ((op1_lvalue_kind & ~clk_ordinary) != clk_none)
193     op1_lvalue_kind &= ~clk_ordinary;
194   return op1_lvalue_kind;
195 }
196
197 /* If REF is an lvalue, returns the kind of lvalue that REF is.
198    Otherwise, returns clk_none.  Lvalues can be assigned, unless they
199    have TREE_READONLY, or unless they are FUNCTION_DECLs.  Lvalues can
200    have their address taken, unless they have DECL_REGISTER.  */
201
202 cp_lvalue_kind
203 real_lvalue_p (ref)
204      tree ref;
205 {
206   return lvalue_p_1 (ref, /*treat_class_rvalues_as_lvalues=*/0);
207 }
208
209 /* This differs from real_lvalue_p in that class rvalues are
210    considered lvalues.  */
211
212 int
213 lvalue_p (ref)
214      tree ref;
215 {
216   return 
217     (lvalue_p_1 (ref, /*treat_class_rvalues_as_lvalues=*/1) != clk_none);
218 }
219
220 /* Return nonzero if REF is an lvalue valid for this language;
221    otherwise, print an error message and return zero.  */
222
223 int
224 lvalue_or_else (ref, string)
225      tree ref;
226      const char *string;
227 {
228   int win = lvalue_p (ref);
229   if (! win)
230     error ("non-lvalue in %s", string);
231   return win;
232 }
233
234 /* Build a TARGET_EXPR, initializing the DECL with the VALUE.  */
235
236 static tree
237 build_target_expr (decl, value)
238      tree decl;
239      tree value;
240 {
241   tree t;
242
243   t = build (TARGET_EXPR, TREE_TYPE (decl), decl, value, 
244              maybe_build_cleanup (decl), NULL_TREE);
245   /* We always set TREE_SIDE_EFFECTS so that expand_expr does not
246      ignore the TARGET_EXPR.  If there really turn out to be no
247      side-effects, then the optimizer should be able to get rid of
248      whatever code is generated anyhow.  */
249   TREE_SIDE_EFFECTS (t) = 1;
250
251   return t;
252 }
253
254 /* INIT is a CALL_EXPR which needs info about its target.
255    TYPE is the type that this initialization should appear to have.
256
257    Build an encapsulation of the initialization to perform
258    and return it so that it can be processed by language-independent
259    and language-specific expression expanders.  */
260
261 tree
262 build_cplus_new (type, init)
263      tree type;
264      tree init;
265 {
266   tree fn;
267   tree slot;
268   tree rval;
269
270   /* Make sure that we're not trying to create an instance of an
271      abstract class.  */
272   abstract_virtuals_error (NULL_TREE, type);
273
274   if (TREE_CODE (init) != CALL_EXPR && TREE_CODE (init) != AGGR_INIT_EXPR)
275     return convert (type, init);
276
277   slot = build (VAR_DECL, type);
278   DECL_ARTIFICIAL (slot) = 1;
279   DECL_CONTEXT (slot) = current_function_decl;
280   layout_decl (slot, 0);
281
282   /* We split the CALL_EXPR into its function and its arguments here.
283      Then, in expand_expr, we put them back together.  The reason for
284      this is that this expression might be a default argument
285      expression.  In that case, we need a new temporary every time the
286      expression is used.  That's what break_out_target_exprs does; it
287      replaces every AGGR_INIT_EXPR with a copy that uses a fresh
288      temporary slot.  Then, expand_expr builds up a call-expression
289      using the new slot.  */
290   fn = TREE_OPERAND (init, 0);
291   rval = build (AGGR_INIT_EXPR, type, fn, TREE_OPERAND (init, 1), slot);
292   TREE_SIDE_EFFECTS (rval) = 1;
293   AGGR_INIT_VIA_CTOR_P (rval) 
294     = (TREE_CODE (fn) == ADDR_EXPR
295        && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
296        && DECL_CONSTRUCTOR_P (TREE_OPERAND (fn, 0)));
297   rval = build_target_expr (slot, rval);
298
299   return rval;
300 }
301
302 /* Buidl a TARGET_EXPR using INIT to initialize a new temporary of the
303    indicated TYPE.  */
304
305 tree
306 build_target_expr_with_type (init, type)
307      tree init;
308      tree type;
309 {
310   tree slot;
311   tree rval;
312
313   if (TREE_CODE (init) == TARGET_EXPR)
314     return init;
315
316   slot = build (VAR_DECL, type);
317   DECL_ARTIFICIAL (slot) = 1;
318   DECL_CONTEXT (slot) = current_function_decl;
319   layout_decl (slot, 0);
320   rval = build_target_expr (slot, init);
321
322   return rval;
323 }
324
325 /* Like build_target_expr_with_type, but use the type of INIT.  */
326
327 tree
328 get_target_expr (init)
329      tree init;
330 {
331   return build_target_expr_with_type (init, TREE_TYPE (init));
332 }
333
334 /* Recursively perform a preorder search EXP for CALL_EXPRs, making
335    copies where they are found.  Returns a deep copy all nodes transitively
336    containing CALL_EXPRs.  */
337
338 tree
339 break_out_calls (exp)
340      tree exp;
341 {
342   register tree t1, t2 = NULL_TREE;
343   register enum tree_code code;
344   register int changed = 0;
345   register int i;
346
347   if (exp == NULL_TREE)
348     return exp;
349
350   code = TREE_CODE (exp);
351
352   if (code == CALL_EXPR)
353     return copy_node (exp);
354
355   /* Don't try and defeat a save_expr, as it should only be done once.  */
356     if (code == SAVE_EXPR)
357        return exp;
358
359   switch (TREE_CODE_CLASS (code))
360     {
361     default:
362       abort ();
363
364     case 'c':  /* a constant */
365     case 't':  /* a type node */
366     case 'x':  /* something random, like an identifier or an ERROR_MARK.  */
367       return exp;
368
369     case 'd':  /* A decl node */
370 #if 0                               /* This is bogus.  jason 9/21/94 */
371
372       t1 = break_out_calls (DECL_INITIAL (exp));
373       if (t1 != DECL_INITIAL (exp))
374         {
375           exp = copy_node (exp);
376           DECL_INITIAL (exp) = t1;
377         }
378 #endif
379       return exp;
380
381     case 'b':  /* A block node */
382       {
383         /* Don't know how to handle these correctly yet.   Must do a
384            break_out_calls on all DECL_INITIAL values for local variables,
385            and also break_out_calls on all sub-blocks and sub-statements.  */
386         abort ();
387       }
388       return exp;
389
390     case 'e':  /* an expression */
391     case 'r':  /* a reference */
392     case 's':  /* an expression with side effects */
393       for (i = TREE_CODE_LENGTH (code) - 1; i >= 0; i--)
394         {
395           t1 = break_out_calls (TREE_OPERAND (exp, i));
396           if (t1 != TREE_OPERAND (exp, i))
397             {
398               exp = copy_node (exp);
399               TREE_OPERAND (exp, i) = t1;
400             }
401         }
402       return exp;
403
404     case '<':  /* a comparison expression */
405     case '2':  /* a binary arithmetic expression */
406       t2 = break_out_calls (TREE_OPERAND (exp, 1));
407       if (t2 != TREE_OPERAND (exp, 1))
408         changed = 1;
409     case '1':  /* a unary arithmetic expression */
410       t1 = break_out_calls (TREE_OPERAND (exp, 0));
411       if (t1 != TREE_OPERAND (exp, 0))
412         changed = 1;
413       if (changed)
414         {
415           if (TREE_CODE_LENGTH (code) == 1)
416             return build1 (code, TREE_TYPE (exp), t1);
417           else
418             return build (code, TREE_TYPE (exp), t1, t2);
419         }
420       return exp;
421     }
422
423 }
424 \f
425 /* Construct, lay out and return the type of methods belonging to class
426    BASETYPE and whose arguments are described by ARGTYPES and whose values
427    are described by RETTYPE.  If each type exists already, reuse it.  */
428
429 tree
430 build_cplus_method_type (basetype, rettype, argtypes)
431      tree basetype, rettype, argtypes;
432 {
433   register tree t;
434   tree ptype;
435   int hashcode;
436
437   /* Make a node of the sort we want.  */
438   t = make_node (METHOD_TYPE);
439
440   TYPE_METHOD_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
441   TREE_TYPE (t) = rettype;
442   ptype = build_pointer_type (basetype);
443
444   /* The actual arglist for this function includes a "hidden" argument
445      which is "this".  Put it into the list of argument types.  */
446   argtypes = tree_cons (NULL_TREE, ptype, argtypes);
447   TYPE_ARG_TYPES (t) = argtypes;
448   TREE_SIDE_EFFECTS (argtypes) = 1;  /* Mark first argtype as "artificial".  */
449
450   /* If we already have such a type, use the old one and free this one.
451      Note that it also frees up the above cons cell if found.  */
452   hashcode = TYPE_HASH (basetype) + TYPE_HASH (rettype) +
453     type_hash_list (argtypes);
454
455   t = type_hash_canon (hashcode, t);
456
457   if (!COMPLETE_TYPE_P (t))
458     layout_type (t);
459
460   return t;
461 }
462
463 static tree
464 build_cplus_array_type_1 (elt_type, index_type)
465      tree elt_type;
466      tree index_type;
467 {
468   tree t;
469
470   if (elt_type == error_mark_node || index_type == error_mark_node)
471     return error_mark_node;
472
473   if (processing_template_decl 
474       || uses_template_parms (elt_type) 
475       || uses_template_parms (index_type))
476     {
477       t = make_node (ARRAY_TYPE);
478       TREE_TYPE (t) = elt_type;
479       TYPE_DOMAIN (t) = index_type;
480     }
481   else
482     t = build_array_type (elt_type, index_type);
483
484   /* Push these needs up so that initialization takes place
485      more easily.  */
486   TYPE_NEEDS_CONSTRUCTING (t) 
487     = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (elt_type));
488   TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) 
489     = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (elt_type));
490   return t;
491 }
492
493 tree
494 build_cplus_array_type (elt_type, index_type)
495      tree elt_type;
496      tree index_type;
497 {
498   tree t;
499   int type_quals = CP_TYPE_QUALS (elt_type);
500
501   if (type_quals != TYPE_UNQUALIFIED)
502     elt_type = cp_build_qualified_type (elt_type, TYPE_UNQUALIFIED);
503
504   t = build_cplus_array_type_1 (elt_type, index_type);
505
506   if (type_quals != TYPE_UNQUALIFIED)
507     t = cp_build_qualified_type (t, type_quals);
508
509   return t;
510 }
511 \f
512 /* Make a variant of TYPE, qualified with the TYPE_QUALS.  Handles
513    arrays correctly.  In particular, if TYPE is an array of T's, and
514    TYPE_QUALS is non-empty, returns an array of qualified T's.  If
515    at attempt is made to qualify a type illegally, and COMPLAIN is
516    non-zero, an error is issued.  If COMPLAIN is zero, error_mark_node
517    is returned.  */
518
519 tree
520 cp_build_qualified_type_real (type, type_quals, complain)
521      tree type;
522      int type_quals;
523      int complain;
524 {
525   tree result;
526
527   if (type == error_mark_node)
528     return type;
529
530   if (type_quals == CP_TYPE_QUALS (type))
531     return type;
532
533   /* A restrict-qualified pointer type must be a pointer (or reference)
534      to object or incomplete type.  */
535   if ((type_quals & TYPE_QUAL_RESTRICT)
536       && TREE_CODE (type) != TEMPLATE_TYPE_PARM
537       && (!POINTER_TYPE_P (type)
538           || TYPE_PTRMEM_P (type)
539           || TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE))
540     {
541       if (complain)
542         cp_error ("`%T' cannot be `restrict'-qualified", type);
543       else
544         return error_mark_node;
545
546       type_quals &= ~TYPE_QUAL_RESTRICT;
547     }
548
549   if (type_quals != TYPE_UNQUALIFIED
550       && TREE_CODE (type) == FUNCTION_TYPE)
551     {
552       if (complain)
553         cp_error ("`%T' cannot be `const'-, `volatile'-, or `restrict'-qualified", type);
554       else
555         return error_mark_node;
556       type_quals = TYPE_UNQUALIFIED;
557     }
558   else if (TREE_CODE (type) == ARRAY_TYPE)
559     {
560       /* In C++, the qualification really applies to the array element
561          type.  Obtain the appropriately qualified element type.  */
562       tree t;
563       tree element_type 
564         = cp_build_qualified_type_real (TREE_TYPE (type), 
565                                         type_quals,
566                                         complain);
567
568       if (element_type == error_mark_node)
569         return error_mark_node;
570
571       /* See if we already have an identically qualified type.  */
572       t = get_qualified_type (type, type_quals);
573
574       /* If we didn't already have it, create it now.  */
575       if (!t)
576         {
577           /* Make a new array type, just like the old one, but with the
578              appropriately qualified element type.  */
579           t = build_type_copy (type);
580           TREE_TYPE (t) = element_type;
581         }
582
583       /* Even if we already had this variant, we update
584          TYPE_NEEDS_CONSTRUCTING and TYPE_HAS_NONTRIVIAL_DESTRUCTOR in case
585          they changed since the variant was originally created.  
586          
587          This seems hokey; if there is some way to use a previous
588          variant *without* coming through here,
589          TYPE_NEEDS_CONSTRUCTING will never be updated.  */
590       TYPE_NEEDS_CONSTRUCTING (t) 
591         = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (element_type));
592       TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) 
593         = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (element_type));
594       return t;
595     }
596   else if (TYPE_PTRMEMFUNC_P (type))
597     {
598       /* For a pointer-to-member type, we can't just return a
599          cv-qualified version of the RECORD_TYPE.  If we do, we
600          haven't change the field that contains the actual pointer to
601          a method, and so TYPE_PTRMEMFUNC_FN_TYPE will be wrong.  */
602       tree t;
603
604       t = TYPE_PTRMEMFUNC_FN_TYPE (type);
605       t = cp_build_qualified_type_real (t, type_quals, complain);
606       return build_ptrmemfunc_type (t);
607     }
608
609   /* Retrieve (or create) the appropriately qualified variant.  */
610   result = build_qualified_type (type, type_quals);
611
612   /* If this was a pointer-to-method type, and we just made a copy,
613      then we need to clear the cached associated
614      pointer-to-member-function type; it is not valid for the new
615      type.  */
616   if (result != type 
617       && TREE_CODE (type) == POINTER_TYPE
618       && TREE_CODE (TREE_TYPE (type)) == METHOD_TYPE)
619     TYPE_SET_PTRMEMFUNC_TYPE (result, NULL_TREE);
620
621   return result;
622 }
623
624 /* Returns the canonical version of TYPE.  In other words, if TYPE is
625    a typedef, returns the underlying type.  The cv-qualification of
626    the type returned matches the type input; they will always be
627    compatible types.  */
628
629 tree
630 canonical_type_variant (t)
631      tree t;
632 {
633   return cp_build_qualified_type (TYPE_MAIN_VARIANT (t), CP_TYPE_QUALS (t));
634 }
635 \f
636 /* Makes new binfos for the indirect bases under BINFO, and updates
637    BINFO_OFFSET for them and their bases.  */
638
639 void
640 unshare_base_binfos (binfo)
641      tree binfo;
642 {
643   tree binfos = BINFO_BASETYPES (binfo);
644   tree new_binfo;
645   int j;
646
647   if (binfos == NULL_TREE)
648     return;
649
650   /* Now unshare the structure beneath BINFO.  */
651   for (j = TREE_VEC_LENGTH (binfos)-1;
652        j >= 0; j--)
653     {
654       tree base_binfo = TREE_VEC_ELT (binfos, j);
655       new_binfo = TREE_VEC_ELT (binfos, j)
656         = make_binfo (BINFO_OFFSET (base_binfo),
657                       base_binfo,
658                       BINFO_VTABLE (base_binfo),
659                       BINFO_VIRTUALS (base_binfo));
660       TREE_VIA_PUBLIC (new_binfo) = TREE_VIA_PUBLIC (base_binfo);
661       TREE_VIA_PROTECTED (new_binfo) = TREE_VIA_PROTECTED (base_binfo);
662       TREE_VIA_VIRTUAL (new_binfo) = TREE_VIA_VIRTUAL (base_binfo);
663       BINFO_INHERITANCE_CHAIN (new_binfo) = binfo;
664       BINFO_PRIMARY_BASE_OF (new_binfo) = NULL_TREE;
665       unshare_base_binfos (new_binfo);
666     }
667 }
668
669 \f
670 /* Hashing of lists so that we don't make duplicates.
671    The entry point is `list_hash_canon'.  */
672
673 /* Now here is the hash table.  When recording a list, it is added
674    to the slot whose index is the hash code mod the table size.
675    Note that the hash table is used for several kinds of lists.
676    While all these live in the same table, they are completely independent,
677    and the hash code is computed differently for each of these.  */
678
679 static htab_t list_hash_table;
680
681 struct list_proxy 
682 {
683   tree purpose;
684   tree value;
685   tree chain;
686 };
687
688 /* Compare ENTRY (an entry in the hash table) with DATA (a list_proxy
689    for a node we are thinking about adding).  */
690
691 static int
692 list_hash_eq (entry, data)
693      const void *entry;
694      const void *data;
695 {
696   tree t = (tree) entry;
697   struct list_proxy *proxy = (struct list_proxy *) data;
698
699   return (TREE_VALUE (t) == proxy->value
700           && TREE_PURPOSE (t) == proxy->purpose
701           && TREE_CHAIN (t) == proxy->chain);
702 }
703
704 /* Compute a hash code for a list (chain of TREE_LIST nodes
705    with goodies in the TREE_PURPOSE, TREE_VALUE, and bits of the
706    TREE_COMMON slots), by adding the hash codes of the individual entries.  */
707
708 static hashval_t
709 list_hash_pieces (purpose, value, chain)
710      tree purpose;
711      tree value;
712      tree chain;
713 {
714   hashval_t hashcode = 0;
715   
716   if (chain)
717     hashcode += TYPE_HASH (chain);
718   
719   if (value)
720     hashcode += TYPE_HASH (value);
721   else
722     hashcode += 1007;
723   if (purpose)
724     hashcode += TYPE_HASH (purpose);
725   else
726     hashcode += 1009;
727   return hashcode;
728 }
729
730 /* Hash an already existing TREE_LIST.  */
731
732 static hashval_t
733 list_hash (p)
734      const void *p;
735 {
736   tree t = (tree) p;
737   return list_hash_pieces (TREE_PURPOSE (t), 
738                            TREE_VALUE (t), 
739                            TREE_CHAIN (t));
740 }
741
742 /* Given list components PURPOSE, VALUE, AND CHAIN, return the canonical
743    object for an identical list if one already exists.  Otherwise, build a
744    new one, and record it as the canonical object.  */
745
746 tree
747 hash_tree_cons (purpose, value, chain)
748      tree purpose, value, chain;
749 {
750   int hashcode = 0;
751   PTR* slot;
752   struct list_proxy proxy;
753
754   /* Hash the list node.  */
755   hashcode = list_hash_pieces (purpose, value, chain);
756   /* Create a proxy for the TREE_LIST we would like to create.  We
757      don't actually create it so as to avoid creating garbage.  */
758   proxy.purpose = purpose;
759   proxy.value = value;
760   proxy.chain = chain;
761   /* See if it is already in the table.  */
762   slot = htab_find_slot_with_hash (list_hash_table, &proxy, hashcode,
763                                    INSERT);
764   /* If not, create a new node.  */
765   if (!*slot)
766     *slot = (PTR) tree_cons (purpose, value, chain);
767   return *slot;
768 }
769
770 /* Constructor for hashed lists.  */
771
772 tree
773 hash_tree_chain (value, chain)
774      tree value, chain;
775 {
776   return hash_tree_cons (NULL_TREE, value, chain);
777 }
778
779 /* Similar, but used for concatenating two lists.  */
780
781 tree
782 hash_chainon (list1, list2)
783      tree list1, list2;
784 {
785   if (list2 == 0)
786     return list1;
787   if (list1 == 0)
788     return list2;
789   if (TREE_CHAIN (list1) == NULL_TREE)
790     return hash_tree_chain (TREE_VALUE (list1), list2);
791   return hash_tree_chain (TREE_VALUE (list1),
792                           hash_chainon (TREE_CHAIN (list1), list2));
793 }
794 \f
795 /* Build an association between TYPE and some parameters:
796
797    OFFSET is the offset added to `this' to convert it to a pointer
798    of type `TYPE *'
799
800    BINFO is the base binfo to use, if we are deriving from one.  This
801    is necessary, as we want specialized parent binfos from base
802    classes, so that the VTABLE_NAMEs of bases are for the most derived
803    type, instead of the simple type.
804
805    VTABLE is the virtual function table with which to initialize
806    sub-objects of type TYPE.
807
808    VIRTUALS are the virtual functions sitting in VTABLE.  */
809
810 tree
811 make_binfo (offset, binfo, vtable, virtuals)
812      tree offset, binfo;
813      tree vtable, virtuals;
814 {
815   tree new_binfo = make_tree_vec (11);
816   tree type;
817
818   if (TREE_CODE (binfo) == TREE_VEC)
819     type = BINFO_TYPE (binfo);
820   else
821     {
822       type = binfo;
823       binfo = CLASS_TYPE_P (type) ? TYPE_BINFO (binfo) : NULL_TREE;
824     }
825
826   TREE_TYPE (new_binfo) = TYPE_MAIN_VARIANT (type);
827   BINFO_OFFSET (new_binfo) = offset;
828   BINFO_VTABLE (new_binfo) = vtable;
829   BINFO_VIRTUALS (new_binfo) = virtuals;
830
831   if (binfo && BINFO_BASETYPES (binfo) != NULL_TREE)
832     BINFO_BASETYPES (new_binfo) = copy_node (BINFO_BASETYPES (binfo));      
833   return new_binfo;
834 }
835
836 /* Return the binfo value for ELEM in TYPE.  */
837
838 tree
839 binfo_value (elem, type)
840      tree elem;
841      tree type;
842 {
843   if (get_base_distance (elem, type, 0, (tree *)0) == -2)
844     compiler_error ("base class `%s' ambiguous in binfo_value",
845                     TYPE_NAME_STRING (elem));
846   if (elem == type)
847     return TYPE_BINFO (type);
848   if (TREE_CODE (elem) == RECORD_TYPE && TYPE_BINFO (elem) == type)
849     return type;
850   return get_binfo (elem, type, 0);
851 }
852
853 /* Return a TREE_LIST whose TREE_VALUE nodes along the
854    BINFO_INHERITANCE_CHAIN for BINFO, but in the opposite order.  In
855    other words, while the BINFO_INHERITANCE_CHAIN goes from base
856    classes to derived classes, the reversed path goes from derived
857    classes to base classes.  */
858
859 tree
860 reverse_path (binfo)
861      tree binfo;
862 {
863   tree reversed_path;
864
865   reversed_path = NULL_TREE;
866   while (binfo) 
867     {
868       reversed_path = tree_cons (NULL_TREE, binfo, reversed_path);
869       binfo = BINFO_INHERITANCE_CHAIN (binfo);
870     }
871
872   return reversed_path;
873 }
874
875 void
876 debug_binfo (elem)
877      tree elem;
878 {
879   HOST_WIDE_INT n;
880   tree virtuals;
881
882   fprintf (stderr, "type \"%s\", offset = ",
883            TYPE_NAME_STRING (BINFO_TYPE (elem)));
884   fprintf (stderr, HOST_WIDE_INT_PRINT_DEC,
885            TREE_INT_CST_LOW (BINFO_OFFSET (elem)));
886   fprintf (stderr, "\nvtable type:\n");
887   debug_tree (BINFO_TYPE (elem));
888   if (BINFO_VTABLE (elem))
889     fprintf (stderr, "vtable decl \"%s\"\n",
890              IDENTIFIER_POINTER (DECL_NAME (get_vtbl_decl_for_binfo (elem))));
891   else
892     fprintf (stderr, "no vtable decl yet\n");
893   fprintf (stderr, "virtuals:\n");
894   virtuals = BINFO_VIRTUALS (elem);
895   n = 0;
896
897   while (virtuals)
898     {
899       tree fndecl = TREE_VALUE (virtuals);
900       fprintf (stderr, "%s [%ld =? %ld]\n",
901                IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (fndecl)),
902                (long) n, (long) TREE_INT_CST_LOW (DECL_VINDEX (fndecl)));
903       ++n;
904       virtuals = TREE_CHAIN (virtuals);
905     }
906 }
907
908 int
909 count_functions (t)
910      tree t;
911 {
912   int i;
913   if (TREE_CODE (t) == FUNCTION_DECL)
914     return 1;
915   else if (TREE_CODE (t) == OVERLOAD)
916     {
917       for (i=0; t; t = OVL_CHAIN (t))
918         i++;
919       return i;
920     }
921
922   my_friendly_abort (359);
923   return 0;
924 }
925
926 int
927 is_overloaded_fn (x)
928      tree x;
929 {
930   /* A baselink is also considered an overloaded function.  */
931   if (TREE_CODE (x) == OFFSET_REF)
932     x = TREE_OPERAND (x, 1);
933   if (BASELINK_P (x))
934     x = TREE_VALUE (x);
935   return (TREE_CODE (x) == FUNCTION_DECL
936           || TREE_CODE (x) == TEMPLATE_ID_EXPR
937           || DECL_FUNCTION_TEMPLATE_P (x)
938           || TREE_CODE (x) == OVERLOAD);
939 }
940
941 int
942 really_overloaded_fn (x)
943      tree x;
944 {     
945   /* A baselink is also considered an overloaded function.  */
946   if (TREE_CODE (x) == OFFSET_REF)
947     x = TREE_OPERAND (x, 1);
948   if (BASELINK_P (x))
949     x = TREE_VALUE (x);
950   return (TREE_CODE (x) == OVERLOAD 
951           && (TREE_CHAIN (x) != NULL_TREE
952               || DECL_FUNCTION_TEMPLATE_P (OVL_FUNCTION (x))));
953 }
954
955 tree
956 get_first_fn (from)
957      tree from;
958 {
959   my_friendly_assert (is_overloaded_fn (from), 9);
960   /* A baselink is also considered an overloaded function. */
961   if (BASELINK_P (from))
962     from = TREE_VALUE (from);
963   return OVL_CURRENT (from);
964 }
965
966 /* Returns nonzero if T is a ->* or .* expression that refers to a
967    member function.  */
968
969 int
970 bound_pmf_p (t)
971      tree t;
972 {
973   return (TREE_CODE (t) == OFFSET_REF
974           && TYPE_PTRMEMFUNC_P (TREE_TYPE (TREE_OPERAND (t, 1))));
975 }
976
977 /* Return a new OVL node, concatenating it with the old one. */
978
979 tree
980 ovl_cons (decl, chain)
981      tree decl;
982      tree chain;
983 {
984   tree result = make_node (OVERLOAD);
985   TREE_TYPE (result) = unknown_type_node;
986   OVL_FUNCTION (result) = decl;
987   TREE_CHAIN (result) = chain;
988   
989   return result;
990 }
991
992 /* Build a new overloaded function. If this is the first one,
993    just return it; otherwise, ovl_cons the _DECLs */
994
995 tree
996 build_overload (decl, chain)
997      tree decl;
998      tree chain;
999 {
1000   if (! chain && TREE_CODE (decl) != TEMPLATE_DECL)
1001     return decl;
1002   if (chain && TREE_CODE (chain) != OVERLOAD)
1003     chain = ovl_cons (chain, NULL_TREE);
1004   return ovl_cons (decl, chain);
1005 }
1006
1007 /* True if fn is in ovl. */
1008
1009 int
1010 ovl_member (fn, ovl)
1011      tree fn;
1012      tree ovl;
1013 {
1014   if (ovl == NULL_TREE)
1015     return 0;
1016   if (TREE_CODE (ovl) != OVERLOAD)
1017     return ovl == fn;
1018   for (; ovl; ovl = OVL_CHAIN (ovl))
1019     if (OVL_FUNCTION (ovl) == fn)
1020       return 1;
1021   return 0;
1022 }
1023
1024 int
1025 is_aggr_type_2 (t1, t2)
1026      tree t1, t2;
1027 {
1028   if (TREE_CODE (t1) != TREE_CODE (t2))
1029     return 0;
1030   return IS_AGGR_TYPE (t1) && IS_AGGR_TYPE (t2);
1031 }
1032
1033 /* Returns non-zero if CODE is the code for a statement.  */
1034
1035 int
1036 cp_statement_code_p (code)
1037      enum tree_code code;
1038 {
1039   switch (code)
1040     {
1041     case SUBOBJECT:
1042     case CLEANUP_STMT:
1043     case CTOR_STMT:
1044     case CTOR_INITIALIZER:
1045     case RETURN_INIT:
1046     case TRY_BLOCK:
1047     case HANDLER:
1048     case EH_SPEC_BLOCK:
1049     case USING_STMT:
1050     case TAG_DEFN:
1051       return 1;
1052
1053     default:
1054       return 0;
1055     }
1056 }
1057 \f
1058 #define PRINT_RING_SIZE 4
1059
1060 const char *
1061 lang_printable_name (decl, v)
1062      tree decl;
1063      int v;
1064 {
1065   static tree decl_ring[PRINT_RING_SIZE];
1066   static char *print_ring[PRINT_RING_SIZE];
1067   static int ring_counter;
1068   int i;
1069
1070   /* Only cache functions.  */
1071   if (v < 2
1072       || TREE_CODE (decl) != FUNCTION_DECL
1073       || DECL_LANG_SPECIFIC (decl) == 0)
1074     return lang_decl_name (decl, v);
1075
1076   /* See if this print name is lying around.  */
1077   for (i = 0; i < PRINT_RING_SIZE; i++)
1078     if (decl_ring[i] == decl)
1079       /* yes, so return it.  */
1080       return print_ring[i];
1081
1082   if (++ring_counter == PRINT_RING_SIZE)
1083     ring_counter = 0;
1084
1085   if (current_function_decl != NULL_TREE)
1086     {
1087       if (decl_ring[ring_counter] == current_function_decl)
1088         ring_counter += 1;
1089       if (ring_counter == PRINT_RING_SIZE)
1090         ring_counter = 0;
1091       if (decl_ring[ring_counter] == current_function_decl)
1092         my_friendly_abort (106);
1093     }
1094
1095   if (print_ring[ring_counter])
1096     free (print_ring[ring_counter]);
1097
1098   print_ring[ring_counter] = xstrdup (lang_decl_name (decl, v));
1099   decl_ring[ring_counter] = decl;
1100   return print_ring[ring_counter];
1101 }
1102 \f
1103 /* Build the FUNCTION_TYPE or METHOD_TYPE which may throw exceptions
1104    listed in RAISES.  */
1105
1106 tree
1107 build_exception_variant (type, raises)
1108      tree type;
1109      tree raises;
1110 {
1111   tree v = TYPE_MAIN_VARIANT (type);
1112   int type_quals = TYPE_QUALS (type);
1113
1114   for (; v; v = TYPE_NEXT_VARIANT (v))
1115     if (TYPE_QUALS (v) == type_quals
1116         && comp_except_specs (raises, TYPE_RAISES_EXCEPTIONS (v), 1))
1117       return v;
1118
1119   /* Need to build a new variant.  */
1120   v = build_type_copy (type);
1121   TYPE_RAISES_EXCEPTIONS (v) = raises;
1122   return v;
1123 }
1124
1125 /* Given a TEMPLATE_TEMPLATE_PARM node T, create a new
1126    BOUND_TEMPLATE_TEMPLATE_PARM bound with NEWARGS as its template
1127    arguments.  */
1128
1129 tree
1130 bind_template_template_parm (t, newargs)
1131      tree t;
1132      tree newargs;
1133 {
1134   tree decl = TYPE_NAME (t);
1135   tree t2;
1136
1137   t2 = make_aggr_type (BOUND_TEMPLATE_TEMPLATE_PARM);
1138   decl = build_decl (TYPE_DECL, DECL_NAME (decl), NULL_TREE);
1139
1140   /* These nodes have to be created to reflect new TYPE_DECL and template
1141      arguments.  */
1142   TEMPLATE_TYPE_PARM_INDEX (t2) = copy_node (TEMPLATE_TYPE_PARM_INDEX (t));
1143   TEMPLATE_PARM_DECL (TEMPLATE_TYPE_PARM_INDEX (t2)) = decl;
1144   TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t2)
1145     = tree_cons (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t), 
1146                  newargs, NULL_TREE);
1147
1148   TREE_TYPE (decl) = t2;
1149   TYPE_NAME (t2) = decl;
1150   TYPE_STUB_DECL (t2) = decl;
1151   TYPE_SIZE (t2) = 0;
1152
1153   return t2;
1154 }
1155
1156 /* Called from count_trees via walk_tree.  */
1157
1158 static tree
1159 count_trees_r (tp, walk_subtrees, data)
1160      tree *tp ATTRIBUTE_UNUSED;
1161      int *walk_subtrees ATTRIBUTE_UNUSED;
1162      void *data;
1163 {
1164   ++ *((int*) data);
1165   return NULL_TREE;
1166 }
1167
1168 /* Debugging function for measuring the rough complexity of a tree
1169    representation.  */
1170
1171 int
1172 count_trees (t)
1173      tree t;
1174 {
1175   int n_trees = 0;
1176   walk_tree_without_duplicates (&t, count_trees_r, &n_trees);
1177   return n_trees;
1178 }  
1179
1180 /* Called from verify_stmt_tree via walk_tree.  */
1181
1182 static tree
1183 verify_stmt_tree_r (tp, walk_subtrees, data)
1184      tree *tp;
1185      int *walk_subtrees ATTRIBUTE_UNUSED;
1186      void *data;
1187 {
1188   tree t = *tp;
1189   htab_t *statements = (htab_t *) data;
1190   void **slot;
1191
1192   if (!statement_code_p (TREE_CODE (t)))
1193     return NULL_TREE;
1194
1195   /* If this statement is already present in the hash table, then
1196      there is a circularity in the statement tree.  */
1197   if (htab_find (*statements, t))
1198     my_friendly_abort (20000727);
1199   
1200   slot = htab_find_slot (*statements, t, INSERT);
1201   *slot = t;
1202
1203   return NULL_TREE;
1204 }
1205
1206 /* Debugging function to check that the statement T has not been
1207    corrupted.  For now, this function simply checks that T contains no
1208    circularities.  */
1209
1210 void
1211 verify_stmt_tree (t)
1212      tree t;
1213 {
1214   htab_t statements;
1215   statements = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL);
1216   walk_tree (&t, verify_stmt_tree_r, &statements, NULL);
1217   htab_delete (statements);
1218 }
1219
1220 /* Called from find_tree via walk_tree.  */
1221
1222 static tree
1223 find_tree_r (tp, walk_subtrees, data)
1224      tree *tp;
1225      int *walk_subtrees ATTRIBUTE_UNUSED;
1226      void *data;
1227 {
1228   if (*tp == (tree) data)
1229     return (tree) data;
1230
1231   return NULL_TREE;
1232 }
1233
1234 /* Returns X if X appears in the tree structure rooted at T.  */
1235
1236 tree
1237 find_tree (t, x)
1238      tree t;
1239      tree x;
1240 {
1241   return walk_tree_without_duplicates (&t, find_tree_r, x);
1242 }
1243
1244 /* Passed to walk_tree.  Checks for the use of types with no linkage.  */
1245
1246 static tree
1247 no_linkage_helper (tp, walk_subtrees, data)
1248      tree *tp;
1249      int *walk_subtrees ATTRIBUTE_UNUSED;
1250      void *data ATTRIBUTE_UNUSED;
1251 {
1252   tree t = *tp;
1253
1254   if (TYPE_P (t)
1255       && (CLASS_TYPE_P (t) || TREE_CODE (t) == ENUMERAL_TYPE)
1256       && (decl_function_context (TYPE_MAIN_DECL (t))
1257           || TYPE_ANONYMOUS_P (t)))
1258     return t;
1259   return NULL_TREE;
1260 }
1261
1262 /* Check if the type T depends on a type with no linkage and if so, return
1263    it.  */
1264
1265 tree
1266 no_linkage_check (t)
1267      tree t;
1268 {
1269   /* There's no point in checking linkage on template functions; we
1270      can't know their complete types.  */
1271   if (processing_template_decl)
1272     return NULL_TREE;
1273
1274   t = walk_tree_without_duplicates (&t, no_linkage_helper, NULL);
1275   if (t != error_mark_node)
1276     return t;
1277   return NULL_TREE;
1278 }
1279
1280 #ifdef GATHER_STATISTICS
1281 extern int depth_reached;
1282 #endif
1283
1284 void
1285 print_lang_statistics ()
1286 {
1287   print_search_statistics ();
1288   print_class_statistics ();
1289 #ifdef GATHER_STATISTICS
1290   fprintf (stderr, "maximum template instantiation depth reached: %d\n",
1291            depth_reached);
1292 #endif
1293 }
1294
1295 /* Return, as an INTEGER_CST node, the number of elements for TYPE
1296    (which is an ARRAY_TYPE).  This counts only elements of the top
1297    array.  */
1298
1299 tree
1300 array_type_nelts_top (type)
1301      tree type;
1302 {
1303   return fold (build (PLUS_EXPR, sizetype,
1304                       array_type_nelts (type),
1305                       integer_one_node));
1306 }
1307
1308 /* Return, as an INTEGER_CST node, the number of elements for TYPE
1309    (which is an ARRAY_TYPE).  This one is a recursive count of all
1310    ARRAY_TYPEs that are clumped together.  */
1311
1312 tree
1313 array_type_nelts_total (type)
1314      tree type;
1315 {
1316   tree sz = array_type_nelts_top (type);
1317   type = TREE_TYPE (type);
1318   while (TREE_CODE (type) == ARRAY_TYPE)
1319     {
1320       tree n = array_type_nelts_top (type);
1321       sz = fold (build (MULT_EXPR, sizetype, sz, n));
1322       type = TREE_TYPE (type);
1323     }
1324   return sz;
1325 }
1326
1327 /* Called from break_out_target_exprs via mapcar.  */
1328
1329 static tree
1330 bot_manip (tp, walk_subtrees, data)
1331      tree *tp;
1332      int *walk_subtrees;
1333      void *data;
1334 {
1335   splay_tree target_remap = ((splay_tree) data);
1336   tree t = *tp;
1337
1338   if (TREE_CONSTANT (t))
1339     {
1340       /* There can't be any TARGET_EXPRs or their slot variables below
1341          this point.  We used to check !TREE_SIDE_EFFECTS, but then we
1342          failed to copy an ADDR_EXPR of the slot VAR_DECL.  */
1343       *walk_subtrees = 0;
1344       return NULL_TREE;
1345     }
1346   if (TREE_CODE (t) == TARGET_EXPR)
1347     {
1348       tree u;
1349
1350       if (TREE_CODE (TREE_OPERAND (t, 1)) == AGGR_INIT_EXPR)
1351         {
1352           mark_used (TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (t, 1), 0), 0));
1353           u = build_cplus_new
1354             (TREE_TYPE (t), break_out_target_exprs (TREE_OPERAND (t, 1)));
1355         }
1356       else 
1357         {
1358           u = build_target_expr_with_type
1359             (break_out_target_exprs (TREE_OPERAND (t, 1)), TREE_TYPE (t));
1360         }
1361
1362       /* Map the old variable to the new one.  */
1363       splay_tree_insert (target_remap, 
1364                          (splay_tree_key) TREE_OPERAND (t, 0), 
1365                          (splay_tree_value) TREE_OPERAND (u, 0));
1366
1367       /* Replace the old expression with the new version.  */
1368       *tp = u;
1369       /* We don't have to go below this point; the recursive call to
1370          break_out_target_exprs will have handled anything below this
1371          point.  */
1372       *walk_subtrees = 0;
1373       return NULL_TREE;
1374     }
1375   else if (TREE_CODE (t) == CALL_EXPR)
1376     mark_used (TREE_OPERAND (TREE_OPERAND (t, 0), 0));
1377
1378   /* Make a copy of this node.  */
1379   return copy_tree_r (tp, walk_subtrees, NULL);
1380 }
1381   
1382 /* Replace all remapped VAR_DECLs in T with their new equivalents.
1383    DATA is really a splay-tree mapping old variables to new
1384    variables.  */
1385
1386 static tree
1387 bot_replace (t, walk_subtrees, data)
1388      tree *t;
1389      int *walk_subtrees ATTRIBUTE_UNUSED;
1390      void *data;
1391 {
1392   splay_tree target_remap = ((splay_tree) data);
1393
1394   if (TREE_CODE (*t) == VAR_DECL)
1395     {
1396       splay_tree_node n = splay_tree_lookup (target_remap,
1397                                              (splay_tree_key) *t);
1398       if (n)
1399         *t = (tree) n->value;
1400     }
1401
1402   return NULL_TREE;
1403 }
1404         
1405 /* When we parse a default argument expression, we may create
1406    temporary variables via TARGET_EXPRs.  When we actually use the
1407    default-argument expression, we make a copy of the expression, but
1408    we must replace the temporaries with appropriate local versions.  */
1409
1410 tree
1411 break_out_target_exprs (t)
1412      tree t;
1413 {
1414   static int target_remap_count;
1415   static splay_tree target_remap;
1416
1417   if (!target_remap_count++)
1418     target_remap = splay_tree_new (splay_tree_compare_pointers, 
1419                                    /*splay_tree_delete_key_fn=*/NULL, 
1420                                    /*splay_tree_delete_value_fn=*/NULL);
1421   walk_tree (&t, bot_manip, target_remap, NULL);
1422   walk_tree (&t, bot_replace, target_remap, NULL);
1423
1424   if (!--target_remap_count)
1425     {
1426       splay_tree_delete (target_remap);
1427       target_remap = NULL;
1428     }
1429
1430   return t;
1431 }
1432
1433 /* Obstack used for allocating nodes in template function and variable
1434    definitions.  */
1435
1436 /* Similar to `build_nt', except that we set TREE_COMPLEXITY to be the
1437    current line number.  */
1438
1439 tree
1440 build_min_nt VPARAMS ((enum tree_code code, ...))
1441 {
1442   register tree t;
1443   register int length;
1444   register int i;
1445
1446   VA_OPEN (p, code);
1447   VA_FIXEDARG (p, enum tree_code, code);
1448
1449   t = make_node (code);
1450   length = TREE_CODE_LENGTH (code);
1451   TREE_COMPLEXITY (t) = lineno;
1452
1453   for (i = 0; i < length; i++)
1454     {
1455       tree x = va_arg (p, tree);
1456       TREE_OPERAND (t, i) = x;
1457     }
1458
1459   VA_CLOSE (p);
1460   return t;
1461 }
1462
1463 /* Similar to `build', except we set TREE_COMPLEXITY to the current
1464    line-number.  */
1465
1466 tree
1467 build_min VPARAMS ((enum tree_code code, tree tt, ...))
1468 {
1469   register tree t;
1470   register int length;
1471   register int i;
1472
1473   VA_OPEN (p, tt);
1474   VA_FIXEDARG (p, enum tree_code, code);
1475   VA_FIXEDARG (p, tree, tt);
1476
1477   t = make_node (code);
1478   length = TREE_CODE_LENGTH (code);
1479   TREE_TYPE (t) = tt;
1480   TREE_COMPLEXITY (t) = lineno;
1481
1482   for (i = 0; i < length; i++)
1483     {
1484       tree x = va_arg (p, tree);
1485       TREE_OPERAND (t, i) = x;
1486     }
1487
1488   VA_CLOSE (p);
1489   return t;
1490 }
1491
1492 /* Returns an INTEGER_CST (of type `int') corresponding to I.
1493    Multiple calls with the same value of I may or may not yield the
1494    same node; therefore, callers should never modify the node
1495    returned.  */
1496
1497 tree
1498 build_shared_int_cst (i)
1499      int i;
1500 {
1501   static tree cache[256];
1502
1503   if (i >= 256)
1504     return build_int_2 (i, 0);
1505   
1506   if (!cache[i])
1507     cache[i] = build_int_2 (i, 0);
1508   
1509   return cache[i];
1510 }
1511
1512 tree
1513 get_type_decl (t)
1514      tree t;
1515 {
1516   if (TREE_CODE (t) == TYPE_DECL)
1517     return t;
1518   if (TYPE_P (t))
1519     return TYPE_STUB_DECL (t);
1520   if (t == error_mark_node)
1521     return t;
1522   
1523   my_friendly_abort (42);
1524
1525   /* Stop compiler from complaining control reaches end of non-void function.  */
1526   return 0;
1527 }
1528
1529 /* Return first vector element whose BINFO_TYPE is ELEM.
1530    Return 0 if ELEM is not in VEC.  VEC may be NULL_TREE.  */
1531
1532 tree
1533 vec_binfo_member (elem, vec)
1534      tree elem, vec;
1535 {
1536   int i;
1537
1538   if (vec)
1539     for (i = 0; i < TREE_VEC_LENGTH (vec); ++i)
1540       if (same_type_p (elem, BINFO_TYPE (TREE_VEC_ELT (vec, i))))
1541         return TREE_VEC_ELT (vec, i);
1542
1543   return NULL_TREE;
1544 }
1545
1546 /* Returns the namespace that contains DECL, whether directly or
1547    indirectly.  */
1548
1549 tree
1550 decl_namespace_context (decl)
1551      tree decl;
1552 {
1553   while (1)
1554     {
1555       if (TREE_CODE (decl) == NAMESPACE_DECL)
1556         return decl;
1557       else if (TYPE_P (decl))
1558         decl = CP_DECL_CONTEXT (TYPE_MAIN_DECL (decl));
1559       else
1560         decl = CP_DECL_CONTEXT (decl);
1561     }
1562 }
1563
1564 /* Return truthvalue of whether T1 is the same tree structure as T2.
1565    Return 1 if they are the same.
1566    Return 0 if they are understandably different.
1567    Return -1 if either contains tree structure not understood by
1568    this function.  */
1569
1570 int
1571 cp_tree_equal (t1, t2)
1572      tree t1, t2;
1573 {
1574   register enum tree_code code1, code2;
1575   int cmp;
1576
1577   if (t1 == t2)
1578     return 1;
1579   if (t1 == 0 || t2 == 0)
1580     return 0;
1581
1582   code1 = TREE_CODE (t1);
1583   code2 = TREE_CODE (t2);
1584
1585   if (code1 == NOP_EXPR || code1 == CONVERT_EXPR || code1 == NON_LVALUE_EXPR)
1586     {
1587       if (code2 == NOP_EXPR || code2 == CONVERT_EXPR || code2 == NON_LVALUE_EXPR)
1588         return cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
1589       else
1590         return cp_tree_equal (TREE_OPERAND (t1, 0), t2);
1591     }
1592   else if (code2 == NOP_EXPR || code2 == CONVERT_EXPR
1593            || code2 == NON_LVALUE_EXPR)
1594     return cp_tree_equal (t1, TREE_OPERAND (t2, 0));
1595
1596   if (code1 != code2)
1597     return 0;
1598
1599   switch (code1)
1600     {
1601     case INTEGER_CST:
1602       return TREE_INT_CST_LOW (t1) == TREE_INT_CST_LOW (t2)
1603         && TREE_INT_CST_HIGH (t1) == TREE_INT_CST_HIGH (t2);
1604
1605     case REAL_CST:
1606       return REAL_VALUES_EQUAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
1607
1608     case STRING_CST:
1609       return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
1610         && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
1611                   TREE_STRING_LENGTH (t1));
1612
1613     case CONSTRUCTOR:
1614       /* We need to do this when determining whether or not two
1615          non-type pointer to member function template arguments
1616          are the same.  */
1617       if (!(same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))
1618             /* The first operand is RTL.  */
1619             && TREE_OPERAND (t1, 0) == TREE_OPERAND (t2, 0)))
1620         return 0;
1621       return cp_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
1622
1623     case TREE_LIST:
1624       cmp = cp_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2));
1625       if (cmp <= 0)
1626         return cmp;
1627       cmp = cp_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2));
1628       if (cmp <= 0)
1629         return cmp;
1630       return cp_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
1631
1632     case SAVE_EXPR:
1633       return cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
1634
1635     case CALL_EXPR:
1636       cmp = cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
1637       if (cmp <= 0)
1638         return cmp;
1639       return simple_cst_list_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
1640
1641     case TARGET_EXPR:
1642       /* Special case: if either target is an unallocated VAR_DECL,
1643          it means that it's going to be unified with whatever the
1644          TARGET_EXPR is really supposed to initialize, so treat it
1645          as being equivalent to anything.  */
1646       if ((TREE_CODE (TREE_OPERAND (t1, 0)) == VAR_DECL
1647            && DECL_NAME (TREE_OPERAND (t1, 0)) == NULL_TREE
1648            && !DECL_RTL_SET_P (TREE_OPERAND (t1, 0)))
1649           || (TREE_CODE (TREE_OPERAND (t2, 0)) == VAR_DECL
1650               && DECL_NAME (TREE_OPERAND (t2, 0)) == NULL_TREE
1651               && !DECL_RTL_SET_P (TREE_OPERAND (t2, 0))))
1652         cmp = 1;
1653       else
1654         cmp = cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
1655       if (cmp <= 0)
1656         return cmp;
1657       return cp_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
1658
1659     case WITH_CLEANUP_EXPR:
1660       cmp = cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
1661       if (cmp <= 0)
1662         return cmp;
1663       return cp_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t1, 1));
1664
1665     case COMPONENT_REF:
1666       if (TREE_OPERAND (t1, 1) == TREE_OPERAND (t2, 1))
1667         return cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
1668       return 0;
1669
1670     case VAR_DECL:
1671     case PARM_DECL:
1672     case CONST_DECL:
1673     case FUNCTION_DECL:
1674       return 0;
1675
1676     case TEMPLATE_PARM_INDEX:
1677       return TEMPLATE_PARM_IDX (t1) == TEMPLATE_PARM_IDX (t2)
1678         && TEMPLATE_PARM_LEVEL (t1) == TEMPLATE_PARM_LEVEL (t2);
1679
1680     case SIZEOF_EXPR:
1681     case ALIGNOF_EXPR:
1682       if (TREE_CODE (TREE_OPERAND (t1, 0)) != TREE_CODE (TREE_OPERAND (t2, 0)))
1683         return 0;
1684       if (TYPE_P (TREE_OPERAND (t1, 0)))
1685         return same_type_p (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
1686       break;
1687
1688     case PTRMEM_CST:
1689       /* Two pointer-to-members are the same if they point to the same
1690          field or function in the same class.  */
1691       return (PTRMEM_CST_MEMBER (t1) == PTRMEM_CST_MEMBER (t2)
1692               && same_type_p (PTRMEM_CST_CLASS (t1), PTRMEM_CST_CLASS (t2)));
1693
1694     default:
1695       break;
1696     }
1697
1698   switch (TREE_CODE_CLASS (code1))
1699     {
1700     case '1':
1701     case '2':
1702     case '<':
1703     case 'e':
1704     case 'r':
1705     case 's':
1706       {
1707         int i;
1708         
1709         cmp = 1;
1710         for (i = 0; i < TREE_CODE_LENGTH (code1); ++i)
1711           {
1712             cmp = cp_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i));
1713             if (cmp <= 0)
1714               return cmp;
1715           }
1716         return cmp;
1717       }
1718     
1719       case 't':
1720         return same_type_p (t1, t2) ? 1 : 0;
1721     }
1722
1723   return -1;
1724 }
1725
1726 /* Build a wrapper around some pointer PTR so we can use it as a tree.  */
1727
1728 tree
1729 build_ptr_wrapper (ptr)
1730      void *ptr;
1731 {
1732   tree t = make_node (WRAPPER);
1733   WRAPPER_PTR (t) = ptr;
1734   return t;
1735 }
1736
1737 /* Build a wrapper around some integer I so we can use it as a tree.  */
1738
1739 tree
1740 build_int_wrapper (i)
1741      int i;
1742 {
1743   tree t = make_node (WRAPPER);
1744   WRAPPER_INT (t) = i;
1745   return t;
1746 }
1747
1748 static tree
1749 build_srcloc (file, line)
1750      const char *file;
1751      int line;
1752 {
1753   tree t;
1754
1755   t = make_node (SRCLOC);
1756   SRCLOC_FILE (t) = file;
1757   SRCLOC_LINE (t) = line;
1758
1759   return t;
1760 }
1761
1762 tree
1763 build_srcloc_here ()
1764 {
1765   return build_srcloc (input_filename, lineno);
1766 }
1767
1768 /* The type of ARG when used as an lvalue.  */
1769
1770 tree
1771 lvalue_type (arg)
1772      tree arg;
1773 {
1774   tree type = TREE_TYPE (arg);
1775   if (TREE_CODE (arg) == OVERLOAD)
1776     type = unknown_type_node;
1777   return type;
1778 }
1779
1780 /* The type of ARG for printing error messages; denote lvalues with
1781    reference types.  */
1782
1783 tree
1784 error_type (arg)
1785      tree arg;
1786 {
1787   tree type = TREE_TYPE (arg);
1788   if (TREE_CODE (type) == ARRAY_TYPE)
1789     ;
1790   else if (real_lvalue_p (arg))
1791     type = build_reference_type (lvalue_type (arg));
1792   else if (IS_AGGR_TYPE (type))
1793     type = lvalue_type (arg);
1794
1795   return type;
1796 }
1797
1798 /* Does FUNCTION use a variable-length argument list?  */
1799
1800 int
1801 varargs_function_p (function)
1802      tree function;
1803 {
1804   tree parm = TYPE_ARG_TYPES (TREE_TYPE (function));
1805   for (; parm; parm = TREE_CHAIN (parm))
1806     if (TREE_VALUE (parm) == void_type_node)
1807       return 0;
1808   return 1;
1809 }
1810
1811 /* Returns 1 if decl is a member of a class.  */
1812
1813 int
1814 member_p (decl)
1815      tree decl;
1816 {
1817   const tree ctx = DECL_CONTEXT (decl);
1818   return (ctx && TYPE_P (ctx));
1819 }
1820
1821 /* Create a placeholder for member access where we don't actually have an
1822    object that the access is against.  */
1823
1824 tree
1825 build_dummy_object (type)
1826      tree type;
1827 {
1828   tree decl = build1 (NOP_EXPR, build_pointer_type (type), void_zero_node);
1829   return build_indirect_ref (decl, NULL);
1830 }
1831
1832 /* We've gotten a reference to a member of TYPE.  Return *this if appropriate,
1833    or a dummy object otherwise.  If BINFOP is non-0, it is filled with the
1834    binfo path from current_class_type to TYPE, or 0.  */
1835
1836 tree
1837 maybe_dummy_object (type, binfop)
1838      tree type;
1839      tree *binfop;
1840 {
1841   tree decl, context;
1842
1843   if (current_class_type
1844       && get_base_distance (type, current_class_type, 0, binfop) != -1)
1845     context = current_class_type;
1846   else
1847     {
1848       /* Reference from a nested class member function.  */
1849       context = type;
1850       if (binfop)
1851         *binfop = TYPE_BINFO (type);
1852     }
1853
1854   if (current_class_ref && context == current_class_type)
1855     decl = current_class_ref;
1856   else
1857     decl = build_dummy_object (context);
1858
1859   return decl;
1860 }
1861
1862 /* Returns 1 if OB is a placeholder object, or a pointer to one.  */
1863
1864 int
1865 is_dummy_object (ob)
1866      tree ob;
1867 {
1868   if (TREE_CODE (ob) == INDIRECT_REF)
1869     ob = TREE_OPERAND (ob, 0);
1870   return (TREE_CODE (ob) == NOP_EXPR
1871           && TREE_OPERAND (ob, 0) == void_zero_node);
1872 }
1873
1874 /* Returns 1 iff type T is a POD type, as defined in [basic.types].  */
1875
1876 int
1877 pod_type_p (t)
1878      tree t;
1879 {
1880   t = strip_array_types (t);
1881
1882   if (INTEGRAL_TYPE_P (t))
1883     return 1;  /* integral, character or enumeral type */
1884   if (FLOAT_TYPE_P (t))
1885     return 1;
1886   if (TYPE_PTR_P (t))
1887     return 1; /* pointer to non-member */
1888   if (TYPE_PTRMEM_P (t))
1889     return 1; /* pointer to member object */
1890   if (TYPE_PTRMEMFUNC_P (t))
1891     return 1; /* pointer to member function */
1892   
1893   if (! CLASS_TYPE_P (t))
1894     return 0; /* other non-class type (reference or function) */
1895   if (CLASSTYPE_NON_POD_P (t))
1896     return 0;
1897   return 1;
1898 }
1899
1900 /* Table of valid C++ attributes.  */
1901 const struct attribute_spec cp_attribute_table[] =
1902 {
1903   /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
1904   { "java_interface", 0, 0, false, false, false, handle_java_interface_attribute },
1905   { "com_interface",  0, 0, false, false, false, handle_com_interface_attribute },
1906   { "init_priority",  1, 1, true,  false, false, handle_init_priority_attribute },
1907   { NULL,             0, 0, false, false, false, NULL }
1908 };
1909
1910 /* Handle a "java_interface" attribute; arguments as in
1911    struct attribute_spec.handler.  */
1912 static tree
1913 handle_java_interface_attribute (node, name, args, flags, no_add_attrs)
1914      tree *node;
1915      tree name;
1916      tree args ATTRIBUTE_UNUSED;
1917      int flags;
1918      bool *no_add_attrs;
1919 {
1920   if (DECL_P (*node)
1921       || !CLASS_TYPE_P (*node)
1922       || !TYPE_FOR_JAVA (*node))
1923     {
1924       error ("`%s' attribute can only be applied to Java class definitions",
1925              IDENTIFIER_POINTER (name));
1926       *no_add_attrs = true;
1927       return NULL_TREE;
1928     }
1929   if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
1930     *node = build_type_copy (*node);
1931   TYPE_JAVA_INTERFACE (*node) = 1;
1932
1933   return NULL_TREE;
1934 }
1935
1936 /* Handle a "com_interface" attribute; arguments as in
1937    struct attribute_spec.handler.  */
1938 static tree
1939 handle_com_interface_attribute (node, name, args, flags, no_add_attrs)
1940      tree *node;
1941      tree name;
1942      tree args ATTRIBUTE_UNUSED;
1943      int flags ATTRIBUTE_UNUSED;
1944      bool *no_add_attrs;
1945 {
1946   static int warned;
1947
1948   *no_add_attrs = true;
1949
1950   if (DECL_P (*node)
1951       || !CLASS_TYPE_P (*node)
1952       || *node != TYPE_MAIN_VARIANT (*node))
1953     {
1954       warning ("`%s' attribute can only be applied to class definitions",
1955                IDENTIFIER_POINTER (name));
1956       return NULL_TREE;
1957     }
1958
1959   if (!warned++)
1960     warning ("`%s' is obsolete; g++ vtables are now COM-compatible by default",
1961              IDENTIFIER_POINTER (name));
1962
1963   return NULL_TREE;
1964 }
1965
1966 /* Handle an "init_priority" attribute; arguments as in
1967    struct attribute_spec.handler.  */
1968 static tree
1969 handle_init_priority_attribute (node, name, args, flags, no_add_attrs)
1970      tree *node;
1971      tree name;
1972      tree args;
1973      int flags ATTRIBUTE_UNUSED;
1974      bool *no_add_attrs;
1975 {
1976   tree initp_expr = TREE_VALUE (args);
1977   tree decl = *node;
1978   tree type = TREE_TYPE (decl);
1979   int pri;
1980
1981   STRIP_NOPS (initp_expr);
1982           
1983   if (!initp_expr || TREE_CODE (initp_expr) != INTEGER_CST)
1984     {
1985       error ("requested init_priority is not an integer constant");
1986       *no_add_attrs = true;
1987       return NULL_TREE;
1988     }
1989
1990   pri = TREE_INT_CST_LOW (initp_expr);
1991         
1992   type = strip_array_types (type);
1993
1994   if (decl == NULL_TREE
1995       || TREE_CODE (decl) != VAR_DECL
1996       || !TREE_STATIC (decl)
1997       || DECL_EXTERNAL (decl)
1998       || (TREE_CODE (type) != RECORD_TYPE
1999           && TREE_CODE (type) != UNION_TYPE)
2000       /* Static objects in functions are initialized the
2001          first time control passes through that
2002          function. This is not precise enough to pin down an
2003          init_priority value, so don't allow it. */
2004       || current_function_decl) 
2005     {
2006       error ("can only use `%s' attribute on file-scope definitions of objects of class type",
2007              IDENTIFIER_POINTER (name));
2008       *no_add_attrs = true;
2009       return NULL_TREE;
2010     }
2011
2012   if (pri > MAX_INIT_PRIORITY || pri <= 0)
2013     {
2014       error ("requested init_priority is out of range");
2015       *no_add_attrs = true;
2016       return NULL_TREE;
2017     }
2018
2019   /* Check for init_priorities that are reserved for
2020      language and runtime support implementations.*/
2021   if (pri <= MAX_RESERVED_INIT_PRIORITY)
2022     {
2023       warning 
2024         ("requested init_priority is reserved for internal use");
2025     }
2026
2027   if (SUPPORTS_INIT_PRIORITY)
2028     {
2029       DECL_INIT_PRIORITY (decl) = pri;
2030       return NULL_TREE;
2031     }
2032   else
2033     {
2034       error ("`%s' attribute is not supported on this platform",
2035              IDENTIFIER_POINTER (name));
2036       *no_add_attrs = true;
2037       return NULL_TREE;
2038     }
2039 }
2040
2041 /* Return a new PTRMEM_CST of the indicated TYPE.  The MEMBER is the
2042    thing pointed to by the constant.  */
2043
2044 tree
2045 make_ptrmem_cst (type, member)
2046      tree type;
2047      tree member;
2048 {
2049   tree ptrmem_cst = make_node (PTRMEM_CST);
2050   /* If would seem a great convenience if make_node would set
2051      TREE_CONSTANT for things of class `c', but it does not.  */
2052   TREE_CONSTANT (ptrmem_cst) = 1;
2053   TREE_TYPE (ptrmem_cst) = type;
2054   PTRMEM_CST_MEMBER (ptrmem_cst) = member;
2055   return ptrmem_cst;
2056 }
2057
2058 /* Apply FUNC to all language-specific sub-trees of TP in a pre-order
2059    traversal.  Called from walk_tree().  */
2060
2061 static tree 
2062 cp_walk_subtrees (tp, walk_subtrees_p, func, data, htab)
2063      tree *tp;
2064      int *walk_subtrees_p;
2065      walk_tree_fn func;
2066      void *data;
2067      void *htab;
2068 {
2069   enum tree_code code = TREE_CODE (*tp);
2070   tree result;
2071   
2072 #define WALK_SUBTREE(NODE)                              \
2073   do                                                    \
2074     {                                                   \
2075       result = walk_tree (&(NODE), func, data, htab);   \
2076       if (result)                                       \
2077         return result;                                  \
2078     }                                                   \
2079   while (0)
2080
2081   /* Not one of the easy cases.  We must explicitly go through the
2082      children.  */
2083   switch (code)
2084     {
2085     case DEFAULT_ARG:
2086     case TEMPLATE_TEMPLATE_PARM:
2087     case BOUND_TEMPLATE_TEMPLATE_PARM:
2088     case TEMPLATE_PARM_INDEX:
2089     case TEMPLATE_TYPE_PARM:
2090     case TYPENAME_TYPE:
2091     case TYPEOF_TYPE:
2092       /* None of thse have subtrees other than those already walked
2093          above.  */
2094       *walk_subtrees_p = 0;
2095       break;
2096
2097     case PTRMEM_CST:
2098       WALK_SUBTREE (TREE_TYPE (*tp));
2099       *walk_subtrees_p = 0;
2100       break;
2101
2102     case TREE_LIST:
2103       /* A BASELINK_P's TREE_PURPOSE is a BINFO, and hence circular.  */
2104       if (!BASELINK_P (*tp))
2105         WALK_SUBTREE (TREE_PURPOSE (*tp));
2106       break;
2107
2108     case OVERLOAD:
2109       WALK_SUBTREE (OVL_FUNCTION (*tp));
2110       WALK_SUBTREE (OVL_CHAIN (*tp));
2111       *walk_subtrees_p = 0;
2112       break;
2113
2114     case RECORD_TYPE:
2115       if (TYPE_PTRMEMFUNC_P (*tp))
2116         WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (*tp));
2117       break;
2118
2119     default:
2120       break;
2121     }
2122
2123   /* We didn't find what we were looking for.  */
2124   return NULL_TREE;
2125
2126 #undef WALK_SUBTREE
2127 }
2128
2129 /* Decide whether there are language-specific reasons to not inline a
2130    function as a tree.  */
2131
2132 static int
2133 cp_cannot_inline_tree_fn (fnp)
2134      tree *fnp;
2135 {
2136   tree fn = *fnp;
2137
2138   /* We can inline a template instantiation only if it's fully
2139      instantiated.  */
2140   if (DECL_TEMPLATE_INFO (fn)
2141       && TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (fn)))
2142     {
2143       fn = *fnp = instantiate_decl (fn, /*defer_ok=*/0);
2144       return TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (fn));
2145     }
2146
2147   if (varargs_function_p (fn))
2148     {
2149       DECL_UNINLINABLE (fn) = 1;
2150       return 1;
2151     }
2152
2153   if (! function_attribute_inlinable_p (fn))
2154     {
2155       DECL_UNINLINABLE (fn) = 1;
2156       return 1;
2157     }
2158
2159   return 0;
2160 }
2161
2162 /* Add any pending functions other than the current function (already
2163    handled by the caller), that thus cannot be inlined, to FNS_P, then
2164    return the latest function added to the array, PREV_FN.  */
2165
2166 static tree
2167 cp_add_pending_fn_decls (fns_p, prev_fn)
2168      void *fns_p;
2169      tree prev_fn;
2170 {
2171   varray_type *fnsp = (varray_type *)fns_p;
2172   struct saved_scope *s;
2173
2174   for (s = scope_chain; s; s = s->prev)
2175     if (s->function_decl && s->function_decl != prev_fn)
2176       {
2177         VARRAY_PUSH_TREE (*fnsp, s->function_decl);
2178         prev_fn = s->function_decl;
2179       }
2180
2181   return prev_fn;
2182 }
2183
2184 /* Determine whether a tree node is an OVERLOAD node.  Used to decide
2185    whether to copy a node or to preserve its chain when inlining a
2186    function.  */
2187
2188 static int
2189 cp_is_overload_p (t)
2190      tree t;
2191 {
2192   return TREE_CODE (t) == OVERLOAD;
2193 }
2194
2195 /* Determine whether VAR is a declaration of an automatic variable in
2196    function FN.  */
2197
2198 static int
2199 cp_auto_var_in_fn_p (var, fn)
2200      tree var, fn;
2201 {
2202   return (DECL_P (var) && DECL_CONTEXT (var) == fn
2203           && nonstatic_local_decl_p (var));
2204 }
2205
2206 /* Tell whether a declaration is needed for the RESULT of a function
2207    FN being inlined into CALLER or if the top node of target_exprs is
2208    to be used.  */
2209
2210 static tree
2211 cp_copy_res_decl_for_inlining (result, fn, caller, decl_map_,
2212                                need_decl, target_exprs)
2213      tree result, fn, caller;
2214      void *decl_map_;
2215      int *need_decl;
2216      void *target_exprs;
2217 {
2218   splay_tree decl_map = (splay_tree)decl_map_;
2219   varray_type *texps = (varray_type *)target_exprs;
2220   tree var;
2221   int aggregate_return_p;
2222
2223   /* Figure out whether or not FN returns an aggregate.  */
2224   aggregate_return_p = IS_AGGR_TYPE (TREE_TYPE (result));
2225   *need_decl = ! aggregate_return_p;
2226
2227   /* If FN returns an aggregate then the caller will always create the
2228      temporary (using a TARGET_EXPR) and the call will be the
2229      initializing expression for the TARGET_EXPR.  If we were just to
2230      create a new VAR_DECL here, then the result of this function
2231      would be copied (bitwise) into the variable initialized by the
2232      TARGET_EXPR.  That's incorrect, so we must transform any
2233      references to the RESULT into references to the target.  */
2234   if (aggregate_return_p)
2235     {
2236       if (VARRAY_ACTIVE_SIZE (*texps) == 0)
2237         abort ();
2238       var = TREE_OPERAND (VARRAY_TOP_TREE (*texps), 0);
2239       if (! same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (var),
2240                                                        TREE_TYPE (result)))
2241         abort ();
2242     }
2243   /* Otherwise, make an appropriate copy.  */
2244   else
2245     var = copy_decl_for_inlining (result, fn, caller);
2246
2247   if (DECL_SAVED_FUNCTION_DATA (fn))
2248     {
2249       tree nrv = DECL_SAVED_FUNCTION_DATA (fn)->x_return_value;
2250       if (nrv)
2251         {
2252           /* We have a named return value; copy the name and source
2253              position so we can get reasonable debugging information, and
2254              register the return variable as its equivalent.  */
2255           DECL_NAME (var) = DECL_NAME (nrv);
2256           DECL_SOURCE_FILE (var) = DECL_SOURCE_FILE (nrv);
2257           DECL_SOURCE_LINE (var) = DECL_SOURCE_LINE (nrv);
2258           splay_tree_insert (decl_map,
2259                              (splay_tree_key) nrv,
2260                              (splay_tree_value) var);
2261         }
2262     }
2263
2264   return var;
2265 }
2266
2267 /* Initialize tree.c.  */
2268
2269 void
2270 init_tree ()
2271 {
2272   make_lang_type_fn = cp_make_lang_type;
2273   lang_walk_subtrees = cp_walk_subtrees;
2274   lang_cannot_inline_tree_fn = cp_cannot_inline_tree_fn;
2275   lang_add_pending_fn_decls = cp_add_pending_fn_decls;
2276   lang_tree_chain_matters_p = cp_is_overload_p;
2277   lang_auto_var_in_fn_p = cp_auto_var_in_fn_p;
2278   lang_copy_res_decl_for_inlining = cp_copy_res_decl_for_inlining;
2279   lang_unsave = cp_unsave;
2280   lang_statement_code_p = cp_statement_code_p;
2281   lang_set_decl_assembler_name = mangle_decl;
2282   list_hash_table = htab_create (31, list_hash, list_hash_eq, NULL);
2283   ggc_add_root (&list_hash_table, 1, 
2284                 sizeof (list_hash_table),
2285                 mark_tree_hashtable);
2286 }
2287
2288 /* Called via walk_tree.  If *TP points to a DECL_STMT for a local
2289    declaration, copies the declaration and enters it in the splay_tree
2290    pointed to by DATA (which is really a `splay_tree *').  */
2291
2292 static tree
2293 mark_local_for_remap_r (tp, walk_subtrees, data)
2294      tree *tp;
2295      int *walk_subtrees ATTRIBUTE_UNUSED;
2296      void *data;
2297 {
2298   tree t = *tp;
2299   splay_tree st = (splay_tree) data;
2300   tree decl;
2301
2302   
2303   if (TREE_CODE (t) == DECL_STMT
2304       && nonstatic_local_decl_p (DECL_STMT_DECL (t)))
2305     decl = DECL_STMT_DECL (t);
2306   else if (TREE_CODE (t) == LABEL_STMT)
2307     decl = LABEL_STMT_LABEL (t);
2308   else if (TREE_CODE (t) == TARGET_EXPR
2309            && nonstatic_local_decl_p (TREE_OPERAND (t, 0)))
2310     decl = TREE_OPERAND (t, 0);
2311   else if (TREE_CODE (t) == CASE_LABEL)
2312     decl = CASE_LABEL_DECL (t);
2313   else
2314     decl = NULL_TREE;
2315
2316   if (decl)
2317     {
2318       tree copy;
2319
2320       /* Make a copy.  */
2321       copy = copy_decl_for_inlining (decl, 
2322                                      DECL_CONTEXT (decl), 
2323                                      DECL_CONTEXT (decl));
2324
2325       /* Remember the copy.  */
2326       splay_tree_insert (st,
2327                          (splay_tree_key) decl, 
2328                          (splay_tree_value) copy);
2329     }
2330
2331   return NULL_TREE;
2332 }
2333
2334 /* Called via walk_tree when an expression is unsaved.  Using the
2335    splay_tree pointed to by ST (which is really a `splay_tree'),
2336    remaps all local declarations to appropriate replacements.  */
2337
2338 static tree
2339 cp_unsave_r (tp, walk_subtrees, data)
2340      tree *tp;
2341      int *walk_subtrees;
2342      void *data;
2343 {
2344   splay_tree st = (splay_tree) data;
2345   splay_tree_node n;
2346
2347   /* Only a local declaration (variable or label).  */
2348   if (nonstatic_local_decl_p (*tp))
2349     {
2350       /* Lookup the declaration.  */
2351       n = splay_tree_lookup (st, (splay_tree_key) *tp);
2352       
2353       /* If it's there, remap it.  */
2354       if (n)
2355         *tp = (tree) n->value;
2356     }
2357   else if (TREE_CODE (*tp) == SAVE_EXPR)
2358     remap_save_expr (tp, st, current_function_decl, walk_subtrees);
2359   else
2360     {
2361       copy_tree_r (tp, walk_subtrees, NULL);
2362
2363       /* Do whatever unsaving is required.  */
2364       unsave_expr_1 (*tp);
2365     }
2366
2367   /* Keep iterating.  */
2368   return NULL_TREE;
2369 }
2370
2371 /* Called by unsave_expr_now whenever an expression (*TP) needs to be
2372    unsaved.  */
2373
2374 static void
2375 cp_unsave (tp)
2376      tree *tp;
2377 {
2378   splay_tree st;
2379
2380   /* Create a splay-tree to map old local variable declarations to new
2381      ones.  */
2382   st = splay_tree_new (splay_tree_compare_pointers, NULL, NULL);
2383
2384   /* Walk the tree once figuring out what needs to be remapped.  */
2385   walk_tree (tp, mark_local_for_remap_r, st, NULL);
2386
2387   /* Walk the tree again, copying, remapping, and unsaving.  */
2388   walk_tree (tp, cp_unsave_r, st, NULL);
2389
2390   /* Clean up.  */
2391   splay_tree_delete (st);
2392 }
2393
2394 /* Returns the kind of special function that DECL (a FUNCTION_DECL)
2395    is.  Note that this sfk_none is zero, so this function can be used
2396    as a predicate to test whether or not DECL is a special function.  */
2397
2398 special_function_kind
2399 special_function_p (decl)
2400      tree decl;
2401 {
2402   /* Rather than doing all this stuff with magic names, we should
2403      probably have a field of type `special_function_kind' in
2404      DECL_LANG_SPECIFIC.  */
2405   if (DECL_COPY_CONSTRUCTOR_P (decl))
2406     return sfk_copy_constructor;
2407   if (DECL_CONSTRUCTOR_P (decl))
2408     return sfk_constructor;
2409   if (DECL_OVERLOADED_OPERATOR_P (decl) == NOP_EXPR)
2410     return sfk_assignment_operator;
2411   if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (decl))
2412     return sfk_destructor;
2413   if (DECL_COMPLETE_DESTRUCTOR_P (decl))
2414     return sfk_complete_destructor;
2415   if (DECL_BASE_DESTRUCTOR_P (decl))
2416     return sfk_base_destructor;
2417   if (DECL_DELETING_DESTRUCTOR_P (decl))
2418     return sfk_deleting_destructor;
2419   if (DECL_CONV_FN_P (decl))
2420     return sfk_conversion;
2421
2422   return sfk_none;
2423 }
2424
2425 /* Returns non-zero if TYPE is a character type, including wchar_t.  */
2426
2427 int
2428 char_type_p (type)
2429      tree type;
2430 {
2431   return (same_type_p (type, char_type_node)
2432           || same_type_p (type, unsigned_char_type_node)
2433           || same_type_p (type, signed_char_type_node)
2434           || same_type_p (type, wchar_type_node));
2435 }
2436
2437 /* Returns the kind of linkage associated with the indicated DECL.  Th
2438    value returned is as specified by the language standard; it is
2439    independent of implementation details regarding template
2440    instantiation, etc.  For example, it is possible that a declaration
2441    to which this function assigns external linkage would not show up
2442    as a global symbol when you run `nm' on the resulting object file.  */
2443
2444 linkage_kind
2445 decl_linkage (decl)
2446      tree decl;
2447 {
2448   /* This function doesn't attempt to calculate the linkage from first
2449      principles as given in [basic.link].  Instead, it makes use of
2450      the fact that we have already set TREE_PUBLIC appropriately, and
2451      then handles a few special cases.  Ideally, we would calculate
2452      linkage first, and then transform that into a concrete
2453      implementation.  */
2454
2455   /* Things that don't have names have no linkage.  */
2456   if (!DECL_NAME (decl))
2457     return lk_none;
2458
2459   /* Things that are TREE_PUBLIC have external linkage.  */
2460   if (TREE_PUBLIC (decl))
2461     return lk_external;
2462
2463   /* Some things that are not TREE_PUBLIC have external linkage, too.
2464      For example, on targets that don't have weak symbols, we make all
2465      template instantiations have internal linkage (in the object
2466      file), but the symbols should still be treated as having external
2467      linkage from the point of view of the language.  */
2468   if (DECL_LANG_SPECIFIC (decl) && DECL_COMDAT (decl))
2469     return lk_external;
2470
2471   /* Things in local scope do not have linkage, if they don't have
2472      TREE_PUBLIC set.  */
2473   if (decl_function_context (decl))
2474     return lk_none;
2475
2476   /* Everything else has internal linkage.  */
2477   return lk_internal;
2478 }