OSDN Git Service

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