OSDN Git Service

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