OSDN Git Service

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