OSDN Git Service

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