OSDN Git Service

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