Fix cut and paste error in last change
[official-gcc.git] / gcc / cp / tree.c
blobf0558999815a8ca44875d7e16baad16bf4ec948b
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 Free Software Foundation, Inc.
4 Hacked by Michael Tiemann (tiemann@cygnus.com)
6 This file is part of GNU CC.
8 GNU CC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
13 GNU CC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU CC; see the file COPYING. If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
23 #include "config.h"
24 #include "system.h"
25 #include "obstack.h"
26 #include "tree.h"
27 #include "cp-tree.h"
28 #include "flags.h"
29 #include "rtl.h"
30 #include "toplev.h"
31 #include "ggc.h"
32 #include "insn-config.h"
33 #include "integrate.h"
35 static tree bot_manip PARAMS ((tree *, int *, void *));
36 static tree bot_replace PARAMS ((tree *, int *, void *));
37 static tree build_cplus_array_type_1 PARAMS ((tree, tree));
38 static void list_hash_add PARAMS ((int, tree));
39 static int list_hash PARAMS ((tree, tree, tree));
40 static tree list_hash_lookup PARAMS ((int, tree, tree, tree));
41 static cp_lvalue_kind lvalue_p_1 PARAMS ((tree, int));
42 static tree no_linkage_helper PARAMS ((tree *, int *, void *));
43 static tree build_srcloc PARAMS ((const char *, int));
44 static void mark_list_hash PARAMS ((void *));
45 static int statement_code_p PARAMS ((enum tree_code));
46 static tree mark_local_for_remap_r PARAMS ((tree *, int *, void *));
47 static tree cp_unsave_r PARAMS ((tree *, int *, void *));
48 static void cp_unsave PARAMS ((tree *));
49 static tree build_target_expr PARAMS ((tree, tree));
50 static tree count_trees_r PARAMS ((tree *, int *, void *));
52 /* If REF is an lvalue, returns the kind of lvalue that REF is.
53 Otherwise, returns clk_none. If TREAT_CLASS_RVALUES_AS_LVALUES is
54 non-zero, rvalues of class type are considered lvalues. */
56 static cp_lvalue_kind
57 lvalue_p_1 (ref, treat_class_rvalues_as_lvalues)
58 tree ref;
59 int treat_class_rvalues_as_lvalues;
61 cp_lvalue_kind op1_lvalue_kind = clk_none;
62 cp_lvalue_kind op2_lvalue_kind = clk_none;
64 if (TREE_CODE (TREE_TYPE (ref)) == REFERENCE_TYPE)
65 return clk_ordinary;
67 if (ref == current_class_ptr && flag_this_is_variable <= 0)
68 return clk_none;
70 switch (TREE_CODE (ref))
72 /* preincrements and predecrements are valid lvals, provided
73 what they refer to are valid lvals. */
74 case PREINCREMENT_EXPR:
75 case PREDECREMENT_EXPR:
76 case SAVE_EXPR:
77 case UNSAVE_EXPR:
78 case TRY_CATCH_EXPR:
79 case WITH_CLEANUP_EXPR:
80 case REALPART_EXPR:
81 case IMAGPART_EXPR:
82 case NOP_EXPR:
83 return lvalue_p_1 (TREE_OPERAND (ref, 0),
84 treat_class_rvalues_as_lvalues);
86 case COMPONENT_REF:
87 op1_lvalue_kind = lvalue_p_1 (TREE_OPERAND (ref, 0),
88 treat_class_rvalues_as_lvalues);
89 if (op1_lvalue_kind
90 /* The "field" can be a FUNCTION_DECL or an OVERLOAD in some
91 situations. */
92 && TREE_CODE (TREE_OPERAND (ref, 1)) == FIELD_DECL
93 && DECL_C_BIT_FIELD (TREE_OPERAND (ref, 1)))
95 /* Clear the ordinary bit. If this object was a class
96 rvalue we want to preserve that information. */
97 op1_lvalue_kind &= ~clk_ordinary;
98 /* The lvalue is for a btifield. */
99 op1_lvalue_kind |= clk_bitfield;
101 return op1_lvalue_kind;
103 case STRING_CST:
104 return clk_ordinary;
106 case VAR_DECL:
107 if (TREE_READONLY (ref) && ! TREE_STATIC (ref)
108 && DECL_LANG_SPECIFIC (ref)
109 && DECL_IN_AGGR_P (ref))
110 return clk_none;
111 case INDIRECT_REF:
112 case ARRAY_REF:
113 case PARM_DECL:
114 case RESULT_DECL:
115 if (TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE)
116 return clk_ordinary;
117 break;
119 /* A currently unresolved scope ref. */
120 case SCOPE_REF:
121 my_friendly_abort (103);
122 case OFFSET_REF:
123 if (TREE_CODE (TREE_OPERAND (ref, 1)) == FUNCTION_DECL)
124 return clk_ordinary;
125 /* Fall through. */
126 case MAX_EXPR:
127 case MIN_EXPR:
128 op1_lvalue_kind = lvalue_p_1 (TREE_OPERAND (ref, 0),
129 treat_class_rvalues_as_lvalues);
130 op2_lvalue_kind = lvalue_p_1 (TREE_OPERAND (ref, 1),
131 treat_class_rvalues_as_lvalues);
132 break;
134 case COND_EXPR:
135 op1_lvalue_kind = lvalue_p_1 (TREE_OPERAND (ref, 1),
136 treat_class_rvalues_as_lvalues);
137 op2_lvalue_kind = lvalue_p_1 (TREE_OPERAND (ref, 2),
138 treat_class_rvalues_as_lvalues);
139 break;
141 case MODIFY_EXPR:
142 return clk_ordinary;
144 case COMPOUND_EXPR:
145 return lvalue_p_1 (TREE_OPERAND (ref, 1),
146 treat_class_rvalues_as_lvalues);
148 case TARGET_EXPR:
149 return treat_class_rvalues_as_lvalues ? clk_class : clk_none;
151 case CALL_EXPR:
152 case VA_ARG_EXPR:
153 return ((treat_class_rvalues_as_lvalues
154 && IS_AGGR_TYPE (TREE_TYPE (ref)))
155 ? clk_class : clk_none);
157 case FUNCTION_DECL:
158 /* All functions (except non-static-member functions) are
159 lvalues. */
160 return (DECL_NONSTATIC_MEMBER_FUNCTION_P (ref)
161 ? clk_none : clk_ordinary);
163 default:
164 break;
167 /* If one operand is not an lvalue at all, then this expression is
168 not an lvalue. */
169 if (!op1_lvalue_kind || !op2_lvalue_kind)
170 return clk_none;
172 /* Otherwise, it's an lvalue, and it has all the odd properties
173 contributed by either operand. */
174 op1_lvalue_kind = op1_lvalue_kind | op2_lvalue_kind;
175 /* It's not an ordinary lvalue if it involves either a bit-field or
176 a class rvalue. */
177 if ((op1_lvalue_kind & ~clk_ordinary) != clk_none)
178 op1_lvalue_kind &= ~clk_ordinary;
179 return op1_lvalue_kind;
182 /* If REF is an lvalue, returns the kind of lvalue that REF is.
183 Otherwise, returns clk_none. Lvalues can be assigned, unless they
184 have TREE_READONLY, or unless they are FUNCTION_DECLs. Lvalues can
185 have their address taken, unless they have DECL_REGISTER. */
187 cp_lvalue_kind
188 real_lvalue_p (ref)
189 tree ref;
191 return lvalue_p_1 (ref, /*treat_class_rvalues_as_lvalues=*/0);
194 /* This differs from real_lvalue_p in that class rvalues are
195 considered lvalues. */
198 lvalue_p (ref)
199 tree ref;
201 return
202 (lvalue_p_1 (ref, /*treat_class_rvalues_as_lvalues=*/1) != clk_none);
205 /* Return nonzero if REF is an lvalue valid for this language;
206 otherwise, print an error message and return zero. */
209 lvalue_or_else (ref, string)
210 tree ref;
211 const char *string;
213 int win = lvalue_p (ref);
214 if (! win)
215 error ("non-lvalue in %s", string);
216 return win;
219 /* Build a TARGET_EXPR, initializing the DECL with the VALUE. */
221 static tree
222 build_target_expr (decl, value)
223 tree decl;
224 tree value;
226 tree t;
228 t = build (TARGET_EXPR, TREE_TYPE (decl), decl, value,
229 maybe_build_cleanup (decl), NULL_TREE);
230 /* We always set TREE_SIDE_EFFECTS so that expand_expr does not
231 ignore the TARGET_EXPR. If there really turn out to be no
232 side-effects, then the optimizer should be able to get rid of
233 whatever code is generated anyhow. */
234 TREE_SIDE_EFFECTS (t) = 1;
236 return t;
239 /* INIT is a CALL_EXPR which needs info about its target.
240 TYPE is the type that this initialization should appear to have.
242 Build an encapsulation of the initialization to perform
243 and return it so that it can be processed by language-independent
244 and language-specific expression expanders. */
246 tree
247 build_cplus_new (type, init)
248 tree type;
249 tree init;
251 tree fn;
252 tree slot;
253 tree rval;
255 /* Make sure that we're not trying to create an instance of an
256 abstract class. */
257 abstract_virtuals_error (NULL_TREE, type);
259 if (TREE_CODE (init) != CALL_EXPR && TREE_CODE (init) != AGGR_INIT_EXPR)
260 return convert (type, init);
262 slot = build (VAR_DECL, type);
263 DECL_ARTIFICIAL (slot) = 1;
264 DECL_CONTEXT (slot) = current_function_decl;
265 layout_decl (slot, 0);
267 /* We split the CALL_EXPR into its function and its arguments here.
268 Then, in expand_expr, we put them back together. The reason for
269 this is that this expression might be a default argument
270 expression. In that case, we need a new temporary every time the
271 expression is used. That's what break_out_target_exprs does; it
272 replaces every AGGR_INIT_EXPR with a copy that uses a fresh
273 temporary slot. Then, expand_expr builds up a call-expression
274 using the new slot. */
275 fn = TREE_OPERAND (init, 0);
276 rval = build (AGGR_INIT_EXPR, type, fn, TREE_OPERAND (init, 1), slot);
277 TREE_SIDE_EFFECTS (rval) = 1;
278 AGGR_INIT_VIA_CTOR_P (rval)
279 = (TREE_CODE (fn) == ADDR_EXPR
280 && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
281 && DECL_CONSTRUCTOR_P (TREE_OPERAND (fn, 0)));
282 rval = build_target_expr (slot, rval);
284 return rval;
287 /* Buidl a TARGET_EXPR using INIT to initialize a new temporary of the
288 indicated TYPE. */
290 tree
291 build_target_expr_with_type (init, type)
292 tree init;
293 tree type;
295 tree slot;
296 tree rval;
298 if (TREE_CODE (init) == TARGET_EXPR)
299 return init;
301 slot = build (VAR_DECL, type);
302 DECL_ARTIFICIAL (slot) = 1;
303 DECL_CONTEXT (slot) = current_function_decl;
304 layout_decl (slot, 0);
305 rval = build_target_expr (slot, init);
307 return rval;
310 /* Like build_target_expr_with_type, but use the type of INIT. */
312 tree
313 get_target_expr (init)
314 tree init;
316 return build_target_expr_with_type (init, TREE_TYPE (init));
319 /* Recursively search EXP for CALL_EXPRs that need cleanups and replace
320 these CALL_EXPRs with tree nodes that will perform the cleanups. */
322 tree
323 break_out_cleanups (exp)
324 tree exp;
326 tree tmp = exp;
328 if (TREE_CODE (tmp) == CALL_EXPR
329 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (tmp)))
330 return build_cplus_new (TREE_TYPE (tmp), tmp);
332 while (TREE_CODE (tmp) == NOP_EXPR
333 || TREE_CODE (tmp) == CONVERT_EXPR
334 || TREE_CODE (tmp) == NON_LVALUE_EXPR)
336 if (TREE_CODE (TREE_OPERAND (tmp, 0)) == CALL_EXPR
337 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (TREE_OPERAND (tmp, 0))))
339 TREE_OPERAND (tmp, 0)
340 = build_cplus_new (TREE_TYPE (TREE_OPERAND (tmp, 0)),
341 TREE_OPERAND (tmp, 0));
342 break;
344 else
345 tmp = TREE_OPERAND (tmp, 0);
347 return exp;
350 /* Recursively perform a preorder search EXP for CALL_EXPRs, making
351 copies where they are found. Returns a deep copy all nodes transitively
352 containing CALL_EXPRs. */
354 tree
355 break_out_calls (exp)
356 tree exp;
358 register tree t1, t2 = NULL_TREE;
359 register enum tree_code code;
360 register int changed = 0;
361 register int i;
363 if (exp == NULL_TREE)
364 return exp;
366 code = TREE_CODE (exp);
368 if (code == CALL_EXPR)
369 return copy_node (exp);
371 /* Don't try and defeat a save_expr, as it should only be done once. */
372 if (code == SAVE_EXPR)
373 return exp;
375 switch (TREE_CODE_CLASS (code))
377 default:
378 abort ();
380 case 'c': /* a constant */
381 case 't': /* a type node */
382 case 'x': /* something random, like an identifier or an ERROR_MARK. */
383 return exp;
385 case 'd': /* A decl node */
386 #if 0 /* This is bogus. jason 9/21/94 */
388 t1 = break_out_calls (DECL_INITIAL (exp));
389 if (t1 != DECL_INITIAL (exp))
391 exp = copy_node (exp);
392 DECL_INITIAL (exp) = t1;
394 #endif
395 return exp;
397 case 'b': /* A block node */
399 /* Don't know how to handle these correctly yet. Must do a
400 break_out_calls on all DECL_INITIAL values for local variables,
401 and also break_out_calls on all sub-blocks and sub-statements. */
402 abort ();
404 return exp;
406 case 'e': /* an expression */
407 case 'r': /* a reference */
408 case 's': /* an expression with side effects */
409 for (i = TREE_CODE_LENGTH (code) - 1; i >= 0; i--)
411 t1 = break_out_calls (TREE_OPERAND (exp, i));
412 if (t1 != TREE_OPERAND (exp, i))
414 exp = copy_node (exp);
415 TREE_OPERAND (exp, i) = t1;
418 return exp;
420 case '<': /* a comparison expression */
421 case '2': /* a binary arithmetic expression */
422 t2 = break_out_calls (TREE_OPERAND (exp, 1));
423 if (t2 != TREE_OPERAND (exp, 1))
424 changed = 1;
425 case '1': /* a unary arithmetic expression */
426 t1 = break_out_calls (TREE_OPERAND (exp, 0));
427 if (t1 != TREE_OPERAND (exp, 0))
428 changed = 1;
429 if (changed)
431 if (TREE_CODE_LENGTH (code) == 1)
432 return build1 (code, TREE_TYPE (exp), t1);
433 else
434 return build (code, TREE_TYPE (exp), t1, t2);
436 return exp;
441 extern struct obstack permanent_obstack;
443 /* Here is how primitive or already-canonicalized types' hash
444 codes are made. MUST BE CONSISTENT WITH tree.c !!! */
445 #define TYPE_HASH(TYPE) ((HOST_WIDE_INT) (TYPE) & 0777777)
447 /* Construct, lay out and return the type of methods belonging to class
448 BASETYPE and whose arguments are described by ARGTYPES and whose values
449 are described by RETTYPE. If each type exists already, reuse it. */
451 tree
452 build_cplus_method_type (basetype, rettype, argtypes)
453 tree basetype, rettype, argtypes;
455 register tree t;
456 tree ptype;
457 int hashcode;
459 /* Make a node of the sort we want. */
460 t = make_node (METHOD_TYPE);
462 TYPE_METHOD_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
463 TREE_TYPE (t) = rettype;
464 ptype = build_pointer_type (basetype);
466 /* The actual arglist for this function includes a "hidden" argument
467 which is "this". Put it into the list of argument types. */
468 argtypes = tree_cons (NULL_TREE, ptype, argtypes);
469 TYPE_ARG_TYPES (t) = argtypes;
470 TREE_SIDE_EFFECTS (argtypes) = 1; /* Mark first argtype as "artificial". */
472 /* If we already have such a type, use the old one and free this one.
473 Note that it also frees up the above cons cell if found. */
474 hashcode = TYPE_HASH (basetype) + TYPE_HASH (rettype) +
475 type_hash_list (argtypes);
477 t = type_hash_canon (hashcode, t);
479 if (!COMPLETE_TYPE_P (t))
480 layout_type (t);
482 return t;
485 static tree
486 build_cplus_array_type_1 (elt_type, index_type)
487 tree elt_type;
488 tree index_type;
490 tree t;
492 if (elt_type == error_mark_node || index_type == error_mark_node)
493 return error_mark_node;
495 if (processing_template_decl
496 || uses_template_parms (elt_type)
497 || uses_template_parms (index_type))
499 t = make_node (ARRAY_TYPE);
500 TREE_TYPE (t) = elt_type;
501 TYPE_DOMAIN (t) = index_type;
503 else
504 t = build_array_type (elt_type, index_type);
506 /* Push these needs up so that initialization takes place
507 more easily. */
508 TYPE_NEEDS_CONSTRUCTING (t)
509 = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (elt_type));
510 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
511 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (elt_type));
512 return t;
515 tree
516 build_cplus_array_type (elt_type, index_type)
517 tree elt_type;
518 tree index_type;
520 tree t;
521 int type_quals = CP_TYPE_QUALS (elt_type);
523 elt_type = TYPE_MAIN_VARIANT (elt_type);
525 t = build_cplus_array_type_1 (elt_type, index_type);
527 if (type_quals != TYPE_UNQUALIFIED)
528 t = cp_build_qualified_type (t, type_quals);
530 return t;
533 /* Make a variant of TYPE, qualified with the TYPE_QUALS. Handles
534 arrays correctly. In particular, if TYPE is an array of T's, and
535 TYPE_QUALS is non-empty, returns an array of qualified T's. If
536 at attempt is made to qualify a type illegally, and COMPLAIN is
537 non-zero, an error is issued. If COMPLAIN is zero, error_mark_node
538 is returned. */
540 tree
541 cp_build_qualified_type_real (type, type_quals, complain)
542 tree type;
543 int type_quals;
544 int complain;
546 tree result;
548 if (type == error_mark_node)
549 return type;
551 if (type_quals == TYPE_QUALS (type))
552 return type;
554 /* A restrict-qualified pointer type must be a pointer (or reference)
555 to object or incomplete type. */
556 if ((type_quals & TYPE_QUAL_RESTRICT)
557 && TREE_CODE (type) != TEMPLATE_TYPE_PARM
558 && (!POINTER_TYPE_P (type)
559 || TYPE_PTRMEM_P (type)
560 || TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE))
562 if (complain)
563 cp_error ("`%T' cannot be `restrict'-qualified", type);
564 else
565 return error_mark_node;
567 type_quals &= ~TYPE_QUAL_RESTRICT;
570 if (type_quals != TYPE_UNQUALIFIED
571 && TREE_CODE (type) == FUNCTION_TYPE)
573 if (complain)
574 cp_error ("`%T' cannot be `const'-, `volatile'-, or `restrict'-qualified", type);
575 else
576 return error_mark_node;
577 type_quals = TYPE_UNQUALIFIED;
579 else if (TREE_CODE (type) == ARRAY_TYPE)
581 /* In C++, the qualification really applies to the array element
582 type. Obtain the appropriately qualified element type. */
583 tree t;
584 tree element_type
585 = cp_build_qualified_type_real (TREE_TYPE (type),
586 type_quals,
587 complain);
589 if (element_type == error_mark_node)
590 return error_mark_node;
592 /* See if we already have an identically qualified type. */
593 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
594 if (CP_TYPE_QUALS (t) == type_quals)
595 break;
597 /* If we didn't already have it, create it now. */
598 if (!t)
600 /* Make a new array type, just like the old one, but with the
601 appropriately qualified element type. */
602 t = build_type_copy (type);
603 TREE_TYPE (t) = element_type;
606 /* Even if we already had this variant, we update
607 TYPE_NEEDS_CONSTRUCTING and TYPE_HAS_NONTRIVIAL_DESTRUCTOR in case
608 they changed since the variant was originally created.
610 This seems hokey; if there is some way to use a previous
611 variant *without* coming through here,
612 TYPE_NEEDS_CONSTRUCTING will never be updated. */
613 TYPE_NEEDS_CONSTRUCTING (t)
614 = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (element_type));
615 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
616 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (element_type));
617 return t;
619 else if (TYPE_PTRMEMFUNC_P (type))
621 /* For a pointer-to-member type, we can't just return a
622 cv-qualified version of the RECORD_TYPE. If we do, we
623 haven't change the field that contains the actual pointer to
624 a method, and so TYPE_PTRMEMFUNC_FN_TYPE will be wrong. */
625 tree t;
627 t = TYPE_PTRMEMFUNC_FN_TYPE (type);
628 t = cp_build_qualified_type_real (t, type_quals, complain);
629 return build_ptrmemfunc_type (t);
632 /* Retrieve (or create) the appropriately qualified variant. */
633 result = build_qualified_type (type, type_quals);
635 /* If this was a pointer-to-method type, and we just made a copy,
636 then we need to clear the cached associated
637 pointer-to-member-function type; it is not valid for the new
638 type. */
639 if (result != type
640 && TREE_CODE (type) == POINTER_TYPE
641 && TREE_CODE (TREE_TYPE (type)) == METHOD_TYPE)
642 TYPE_SET_PTRMEMFUNC_TYPE (result, NULL_TREE);
644 return result;
647 /* Returns the canonical version of TYPE. In other words, if TYPE is
648 a typedef, returns the underlying type. The cv-qualification of
649 the type returned matches the type input; they will always be
650 compatible types. */
652 tree
653 canonical_type_variant (t)
654 tree t;
656 return cp_build_qualified_type (TYPE_MAIN_VARIANT (t), CP_TYPE_QUALS (t));
659 /* Makes new binfos for the indirect bases under BINFO, and updates
660 BINFO_OFFSET for them and their bases. */
662 void
663 unshare_base_binfos (binfo)
664 tree binfo;
666 tree binfos = BINFO_BASETYPES (binfo);
667 tree new_binfo;
668 int j;
670 if (binfos == NULL_TREE)
671 return;
673 /* Now unshare the structure beneath BINFO. */
674 for (j = TREE_VEC_LENGTH (binfos)-1;
675 j >= 0; j--)
677 tree base_binfo = TREE_VEC_ELT (binfos, j);
678 new_binfo = TREE_VEC_ELT (binfos, j)
679 = make_binfo (BINFO_OFFSET (base_binfo),
680 base_binfo,
681 BINFO_VTABLE (base_binfo),
682 BINFO_VIRTUALS (base_binfo));
683 TREE_VIA_PUBLIC (new_binfo) = TREE_VIA_PUBLIC (base_binfo);
684 TREE_VIA_PROTECTED (new_binfo) = TREE_VIA_PROTECTED (base_binfo);
685 TREE_VIA_VIRTUAL (new_binfo) = TREE_VIA_VIRTUAL (base_binfo);
686 BINFO_INHERITANCE_CHAIN (new_binfo) = binfo;
687 unshare_base_binfos (new_binfo);
692 /* Hashing of lists so that we don't make duplicates.
693 The entry point is `list_hash_canon'. */
695 /* Each hash table slot is a bucket containing a chain
696 of these structures. */
698 struct list_hash
700 struct list_hash *next; /* Next structure in the bucket. */
701 int hashcode; /* Hash code of this list. */
702 tree list; /* The list recorded here. */
705 /* Now here is the hash table. When recording a list, it is added
706 to the slot whose index is the hash code mod the table size.
707 Note that the hash table is used for several kinds of lists.
708 While all these live in the same table, they are completely independent,
709 and the hash code is computed differently for each of these. */
711 #define TYPE_HASH_SIZE 59
712 static struct list_hash *list_hash_table[TYPE_HASH_SIZE];
714 /* Compute a hash code for a list (chain of TREE_LIST nodes
715 with goodies in the TREE_PURPOSE, TREE_VALUE, and bits of the
716 TREE_COMMON slots), by adding the hash codes of the individual entries. */
718 static int
719 list_hash (purpose, value, chain)
720 tree purpose, value, chain;
722 register int hashcode = 0;
724 if (chain)
725 hashcode += TYPE_HASH (chain);
727 if (value)
728 hashcode += TYPE_HASH (value);
729 else
730 hashcode += 1007;
731 if (purpose)
732 hashcode += TYPE_HASH (purpose);
733 else
734 hashcode += 1009;
735 return hashcode;
738 /* Look in the type hash table for a type isomorphic to TYPE.
739 If one is found, return it. Otherwise return 0. */
741 static tree
742 list_hash_lookup (hashcode, purpose, value, chain)
743 int hashcode;
744 tree purpose, value, chain;
746 register struct list_hash *h;
748 for (h = list_hash_table[hashcode % TYPE_HASH_SIZE]; h; h = h->next)
749 if (h->hashcode == hashcode
750 && TREE_PURPOSE (h->list) == purpose
751 && TREE_VALUE (h->list) == value
752 && TREE_CHAIN (h->list) == chain)
753 return h->list;
754 return 0;
757 /* Add an entry to the list-hash-table
758 for a list TYPE whose hash code is HASHCODE. */
760 static void
761 list_hash_add (hashcode, list)
762 int hashcode;
763 tree list;
765 register struct list_hash *h;
767 h = (struct list_hash *) obstack_alloc (&permanent_obstack, sizeof (struct list_hash));
768 h->hashcode = hashcode;
769 h->list = list;
770 h->next = list_hash_table[hashcode % TYPE_HASH_SIZE];
771 list_hash_table[hashcode % TYPE_HASH_SIZE] = h;
774 /* Given list components PURPOSE, VALUE, AND CHAIN, return the canonical
775 object for an identical list if one already exists. Otherwise, build a
776 new one, and record it as the canonical object. */
778 /* Set to 1 to debug without canonicalization. Never set by program. */
780 static int debug_no_list_hash = 0;
782 tree
783 hash_tree_cons (purpose, value, chain)
784 tree purpose, value, chain;
786 tree t;
787 int hashcode = 0;
789 if (! debug_no_list_hash)
791 hashcode = list_hash (purpose, value, chain);
792 t = list_hash_lookup (hashcode, purpose, value, chain);
793 if (t)
794 return t;
797 t = tree_cons (purpose, value, chain);
799 /* If this is a new list, record it for later reuse. */
800 if (! debug_no_list_hash)
801 list_hash_add (hashcode, t);
803 return t;
806 /* Constructor for hashed lists. */
808 tree
809 hash_tree_chain (value, chain)
810 tree value, chain;
812 return hash_tree_cons (NULL_TREE, value, chain);
815 /* Similar, but used for concatenating two lists. */
817 tree
818 hash_chainon (list1, list2)
819 tree list1, list2;
821 if (list2 == 0)
822 return list1;
823 if (list1 == 0)
824 return list2;
825 if (TREE_CHAIN (list1) == NULL_TREE)
826 return hash_tree_chain (TREE_VALUE (list1), list2);
827 return hash_tree_chain (TREE_VALUE (list1),
828 hash_chainon (TREE_CHAIN (list1), list2));
831 /* Build an association between TYPE and some parameters:
833 OFFSET is the offset added to `this' to convert it to a pointer
834 of type `TYPE *'
836 BINFO is the base binfo to use, if we are deriving from one. This
837 is necessary, as we want specialized parent binfos from base
838 classes, so that the VTABLE_NAMEs of bases are for the most derived
839 type, instead of the simple type.
841 VTABLE is the virtual function table with which to initialize
842 sub-objects of type TYPE.
844 VIRTUALS are the virtual functions sitting in VTABLE. */
846 tree
847 make_binfo (offset, binfo, vtable, virtuals)
848 tree offset, binfo;
849 tree vtable, virtuals;
851 tree new_binfo = make_tree_vec (10);
852 tree type;
854 if (TREE_CODE (binfo) == TREE_VEC)
855 type = BINFO_TYPE (binfo);
856 else
858 type = binfo;
859 binfo = CLASS_TYPE_P (type) ? TYPE_BINFO (binfo) : NULL_TREE;
862 TREE_TYPE (new_binfo) = TYPE_MAIN_VARIANT (type);
863 BINFO_OFFSET (new_binfo) = offset;
864 BINFO_VTABLE (new_binfo) = vtable;
865 BINFO_VIRTUALS (new_binfo) = virtuals;
867 if (binfo && BINFO_BASETYPES (binfo) != NULL_TREE)
868 BINFO_BASETYPES (new_binfo) = copy_node (BINFO_BASETYPES (binfo));
869 return new_binfo;
872 /* Return the binfo value for ELEM in TYPE. */
874 tree
875 binfo_value (elem, type)
876 tree elem;
877 tree type;
879 if (get_base_distance (elem, type, 0, (tree *)0) == -2)
880 compiler_error ("base class `%s' ambiguous in binfo_value",
881 TYPE_NAME_STRING (elem));
882 if (elem == type)
883 return TYPE_BINFO (type);
884 if (TREE_CODE (elem) == RECORD_TYPE && TYPE_BINFO (elem) == type)
885 return type;
886 return get_binfo (elem, type, 0);
889 /* Return a TREE_LIST whose TREE_VALUE nodes along the
890 BINFO_INHERITANCE_CHAIN for BINFO, but in the opposite order. In
891 other words, while the BINFO_INHERITANCE_CHAIN goes from base
892 classes to derived classes, the reversed path goes from derived
893 classes to base classes. */
895 tree
896 reverse_path (binfo)
897 tree binfo;
899 tree reversed_path;
901 reversed_path = NULL_TREE;
902 while (binfo)
904 reversed_path = tree_cons (NULL_TREE, binfo, reversed_path);
905 binfo = BINFO_INHERITANCE_CHAIN (binfo);
908 return reversed_path;
911 void
912 debug_binfo (elem)
913 tree elem;
915 HOST_WIDE_INT n;
916 tree virtuals;
918 fprintf (stderr, "type \"%s\", offset = ",
919 TYPE_NAME_STRING (BINFO_TYPE (elem)));
920 fprintf (stderr, HOST_WIDE_INT_PRINT_DEC,
921 TREE_INT_CST_LOW (BINFO_OFFSET (elem)));
922 fprintf (stderr, "\nvtable type:\n");
923 debug_tree (BINFO_TYPE (elem));
924 if (BINFO_VTABLE (elem))
925 fprintf (stderr, "vtable decl \"%s\"\n",
926 IDENTIFIER_POINTER (DECL_NAME (get_vtbl_decl_for_binfo (elem))));
927 else
928 fprintf (stderr, "no vtable decl yet\n");
929 fprintf (stderr, "virtuals:\n");
930 virtuals = BINFO_VIRTUALS (elem);
931 n = first_vfun_index (BINFO_TYPE (elem));
933 while (virtuals)
935 tree fndecl = TREE_VALUE (virtuals);
936 fprintf (stderr, "%s [%ld =? %ld]\n",
937 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (fndecl)),
938 (long) n, (long) TREE_INT_CST_LOW (DECL_VINDEX (fndecl)));
939 ++n;
940 virtuals = TREE_CHAIN (virtuals);
945 count_functions (t)
946 tree t;
948 int i;
949 if (TREE_CODE (t) == FUNCTION_DECL)
950 return 1;
951 else if (TREE_CODE (t) == OVERLOAD)
953 for (i=0; t; t = OVL_CHAIN (t))
954 i++;
955 return i;
958 my_friendly_abort (359);
959 return 0;
963 is_overloaded_fn (x)
964 tree x;
966 /* A baselink is also considered an overloaded function. */
967 if (TREE_CODE (x) == OFFSET_REF)
968 x = TREE_OPERAND (x, 1);
969 if (BASELINK_P (x))
970 x = TREE_VALUE (x);
971 return (TREE_CODE (x) == FUNCTION_DECL
972 || TREE_CODE (x) == TEMPLATE_ID_EXPR
973 || DECL_FUNCTION_TEMPLATE_P (x)
974 || TREE_CODE (x) == OVERLOAD);
978 really_overloaded_fn (x)
979 tree x;
981 /* A baselink is also considered an overloaded function. */
982 if (TREE_CODE (x) == OFFSET_REF)
983 x = TREE_OPERAND (x, 1);
984 if (BASELINK_P (x))
985 x = TREE_VALUE (x);
986 return (TREE_CODE (x) == OVERLOAD
987 && (TREE_CHAIN (x) != NULL_TREE
988 || DECL_FUNCTION_TEMPLATE_P (OVL_FUNCTION (x))));
991 tree
992 get_first_fn (from)
993 tree from;
995 my_friendly_assert (is_overloaded_fn (from), 9);
996 /* A baselink is also considered an overloaded function. */
997 if (BASELINK_P (from))
998 from = TREE_VALUE (from);
999 return OVL_CURRENT (from);
1002 /* Returns nonzero if T is a ->* or .* expression that refers to a
1003 member function. */
1006 bound_pmf_p (t)
1007 tree t;
1009 return (TREE_CODE (t) == OFFSET_REF
1010 && TYPE_PTRMEMFUNC_P (TREE_TYPE (TREE_OPERAND (t, 1))));
1013 /* Return a new OVL node, concatenating it with the old one. */
1015 tree
1016 ovl_cons (decl, chain)
1017 tree decl;
1018 tree chain;
1020 tree result = make_node (OVERLOAD);
1021 TREE_TYPE (result) = unknown_type_node;
1022 OVL_FUNCTION (result) = decl;
1023 TREE_CHAIN (result) = chain;
1025 return result;
1028 /* Build a new overloaded function. If this is the first one,
1029 just return it; otherwise, ovl_cons the _DECLs */
1031 tree
1032 build_overload (decl, chain)
1033 tree decl;
1034 tree chain;
1036 if (! chain && TREE_CODE (decl) != TEMPLATE_DECL)
1037 return decl;
1038 if (chain && TREE_CODE (chain) != OVERLOAD)
1039 chain = ovl_cons (chain, NULL_TREE);
1040 return ovl_cons (decl, chain);
1043 /* True if fn is in ovl. */
1046 ovl_member (fn, ovl)
1047 tree fn;
1048 tree ovl;
1050 if (ovl == NULL_TREE)
1051 return 0;
1052 if (TREE_CODE (ovl) != OVERLOAD)
1053 return ovl == fn;
1054 for (; ovl; ovl = OVL_CHAIN (ovl))
1055 if (OVL_FUNCTION (ovl) == fn)
1056 return 1;
1057 return 0;
1061 is_aggr_type_2 (t1, t2)
1062 tree t1, t2;
1064 if (TREE_CODE (t1) != TREE_CODE (t2))
1065 return 0;
1066 return IS_AGGR_TYPE (t1) && IS_AGGR_TYPE (t2);
1069 /* Returns non-zero if CODE is the code for a statement. */
1071 static int
1072 statement_code_p (code)
1073 enum tree_code code;
1075 switch (code)
1077 case EXPR_STMT:
1078 case COMPOUND_STMT:
1079 case DECL_STMT:
1080 case IF_STMT:
1081 case FOR_STMT:
1082 case WHILE_STMT:
1083 case DO_STMT:
1084 case RETURN_STMT:
1085 case BREAK_STMT:
1086 case CONTINUE_STMT:
1087 case SWITCH_STMT:
1088 case GOTO_STMT:
1089 case LABEL_STMT:
1090 case ASM_STMT:
1091 case SUBOBJECT:
1092 case CLEANUP_STMT:
1093 case START_CATCH_STMT:
1094 case CTOR_STMT:
1095 case SCOPE_STMT:
1096 case CTOR_INITIALIZER:
1097 case CASE_LABEL:
1098 case RETURN_INIT:
1099 case TRY_BLOCK:
1100 case HANDLER:
1101 return 1;
1103 default:
1104 return 0;
1108 #define PRINT_RING_SIZE 4
1110 const char *
1111 lang_printable_name (decl, v)
1112 tree decl;
1113 int v;
1115 static tree decl_ring[PRINT_RING_SIZE];
1116 static char *print_ring[PRINT_RING_SIZE];
1117 static int ring_counter;
1118 int i;
1120 /* Only cache functions. */
1121 if (v < 2
1122 || TREE_CODE (decl) != FUNCTION_DECL
1123 || DECL_LANG_SPECIFIC (decl) == 0)
1124 return lang_decl_name (decl, v);
1126 /* See if this print name is lying around. */
1127 for (i = 0; i < PRINT_RING_SIZE; i++)
1128 if (decl_ring[i] == decl)
1129 /* yes, so return it. */
1130 return print_ring[i];
1132 if (++ring_counter == PRINT_RING_SIZE)
1133 ring_counter = 0;
1135 if (current_function_decl != NULL_TREE)
1137 if (decl_ring[ring_counter] == current_function_decl)
1138 ring_counter += 1;
1139 if (ring_counter == PRINT_RING_SIZE)
1140 ring_counter = 0;
1141 if (decl_ring[ring_counter] == current_function_decl)
1142 my_friendly_abort (106);
1145 if (print_ring[ring_counter])
1146 free (print_ring[ring_counter]);
1148 print_ring[ring_counter] = xstrdup (lang_decl_name (decl, v));
1149 decl_ring[ring_counter] = decl;
1150 return print_ring[ring_counter];
1153 /* Build the FUNCTION_TYPE or METHOD_TYPE which may throw exceptions
1154 listed in RAISES. */
1156 tree
1157 build_exception_variant (type, raises)
1158 tree type;
1159 tree raises;
1161 tree v = TYPE_MAIN_VARIANT (type);
1162 int type_quals = TYPE_QUALS (type);
1164 for (; v; v = TYPE_NEXT_VARIANT (v))
1165 if (TYPE_QUALS (v) == type_quals
1166 && comp_except_specs (raises, TYPE_RAISES_EXCEPTIONS (v), 1))
1167 return v;
1169 /* Need to build a new variant. */
1170 v = build_type_copy (type);
1171 TYPE_RAISES_EXCEPTIONS (v) = raises;
1172 return v;
1175 /* Given a TEMPLATE_TEMPLATE_PARM node T, create a new one together with its
1176 lang_specific field and its corresponding *_DECL node.
1177 If NEWARGS is not NULL_TREE, this parameter is bound with new set of
1178 arguments. */
1180 tree
1181 copy_template_template_parm (t, newargs)
1182 tree t;
1183 tree newargs;
1185 tree decl = TYPE_NAME (t);
1186 tree t2;
1188 t2 = make_aggr_type (TEMPLATE_TEMPLATE_PARM);
1189 if (newargs == NULL_TREE)
1191 decl = copy_decl (decl);
1193 /* No need to copy these. */
1194 TEMPLATE_TYPE_PARM_INDEX (t2) = TEMPLATE_TYPE_PARM_INDEX (t);
1195 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t2)
1196 = TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t);
1198 else
1200 decl = build_decl (TYPE_DECL, DECL_NAME (decl), NULL_TREE);
1202 /* These nodes have to be created to reflect new TYPE_DECL and template
1203 arguments. */
1204 TEMPLATE_TYPE_PARM_INDEX (t2) = copy_node (TEMPLATE_TYPE_PARM_INDEX (t));
1205 TEMPLATE_PARM_DECL (TEMPLATE_TYPE_PARM_INDEX (t2)) = decl;
1206 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t2)
1207 = tree_cons (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t),
1208 newargs, NULL_TREE);
1211 TREE_TYPE (decl) = t2;
1212 TYPE_NAME (t2) = decl;
1213 TYPE_STUB_DECL (t2) = decl;
1215 return t2;
1218 /* Apply FUNC to all the sub-trees of TP in a pre-order traversal.
1219 FUNC is called with the DATA and the address of each sub-tree. If
1220 FUNC returns a non-NULL value, the traversal is aborted, and the
1221 value returned by FUNC is returned. */
1223 tree
1224 walk_tree (tp, func, data)
1225 tree *tp;
1226 walk_tree_fn func;
1227 void *data;
1229 enum tree_code code;
1230 int walk_subtrees;
1231 tree result;
1233 #define WALK_SUBTREE(NODE) \
1234 do \
1236 result = walk_tree (&(NODE), func, data); \
1237 if (result) \
1238 return result; \
1240 while (0)
1242 /* Skip empty subtrees. */
1243 if (!*tp)
1244 return NULL_TREE;
1246 /* Call the function. */
1247 walk_subtrees = 1;
1248 result = (*func) (tp, &walk_subtrees, data);
1250 /* If we found something, return it. */
1251 if (result)
1252 return result;
1254 /* Even if we didn't, FUNC may have decided that there was nothing
1255 interesting below this point in the tree. */
1256 if (!walk_subtrees)
1257 return NULL_TREE;
1259 code = TREE_CODE (*tp);
1261 /* Handle common cases up front. */
1262 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code))
1263 || TREE_CODE_CLASS (code) == 'r'
1264 || TREE_CODE_CLASS (code) == 's')
1266 int i, len;
1268 /* Set lineno here so we get the right instantiation context
1269 if we call instantiate_decl from inlinable_function_p. */
1270 if (statement_code_p (code) && !STMT_LINENO_FOR_FN_P (*tp))
1271 lineno = STMT_LINENO (*tp);
1273 /* Walk over all the sub-trees of this operand. */
1274 len = first_rtl_op (code);
1275 /* TARGET_EXPRs are peculiar: operands 1 and 3 can be the same.
1276 But, we only want to walk once. */
1277 if (code == TARGET_EXPR
1278 && TREE_OPERAND (*tp, 3) == TREE_OPERAND (*tp, 1))
1279 --len;
1280 /* Go through the subtrees. We need to do this in forward order so
1281 that the scope of a FOR_EXPR is handled properly. */
1282 for (i = 0; i < len; ++i)
1283 WALK_SUBTREE (TREE_OPERAND (*tp, i));
1285 /* For statements, we also walk the chain so that we cover the
1286 entire statement tree. */
1287 if (statement_code_p (code))
1289 if (code == DECL_STMT
1290 && DECL_STMT_DECL (*tp)
1291 && DECL_P (DECL_STMT_DECL (*tp)))
1293 /* Walk the DECL_INITIAL and DECL_SIZE. We don't want to walk
1294 into declarations that are just mentioned, rather than
1295 declared; they don't really belong to this part of the tree.
1296 And, we can see cycles: the initializer for a declaration can
1297 refer to the declaration itself. */
1298 WALK_SUBTREE (DECL_INITIAL (DECL_STMT_DECL (*tp)));
1299 WALK_SUBTREE (DECL_SIZE (DECL_STMT_DECL (*tp)));
1300 WALK_SUBTREE (DECL_SIZE_UNIT (DECL_STMT_DECL (*tp)));
1303 WALK_SUBTREE (TREE_CHAIN (*tp));
1306 /* We didn't find what we were looking for. */
1307 return NULL_TREE;
1309 else if (TREE_CODE_CLASS (code) == 'd')
1311 WALK_SUBTREE (TREE_TYPE (*tp));
1313 /* We didn't find what we were looking for. */
1314 return NULL_TREE;
1317 /* Not one of the easy cases. We must explicitly go through the
1318 children. */
1319 switch (code)
1321 case ERROR_MARK:
1322 case IDENTIFIER_NODE:
1323 case INTEGER_CST:
1324 case REAL_CST:
1325 case STRING_CST:
1326 case DEFAULT_ARG:
1327 case TEMPLATE_TEMPLATE_PARM:
1328 case TEMPLATE_PARM_INDEX:
1329 case TEMPLATE_TYPE_PARM:
1330 case REAL_TYPE:
1331 case COMPLEX_TYPE:
1332 case VOID_TYPE:
1333 case BOOLEAN_TYPE:
1334 case TYPENAME_TYPE:
1335 case UNION_TYPE:
1336 case ENUMERAL_TYPE:
1337 case TYPEOF_TYPE:
1338 case BLOCK:
1339 /* None of thse have subtrees other than those already walked
1340 above. */
1341 break;
1343 case PTRMEM_CST:
1344 WALK_SUBTREE (TREE_TYPE (*tp));
1345 break;
1347 case POINTER_TYPE:
1348 case REFERENCE_TYPE:
1349 WALK_SUBTREE (TREE_TYPE (*tp));
1350 break;
1352 case TREE_LIST:
1353 WALK_SUBTREE (TREE_PURPOSE (*tp));
1354 WALK_SUBTREE (TREE_VALUE (*tp));
1355 WALK_SUBTREE (TREE_CHAIN (*tp));
1356 break;
1358 case OVERLOAD:
1359 WALK_SUBTREE (OVL_FUNCTION (*tp));
1360 WALK_SUBTREE (OVL_CHAIN (*tp));
1361 break;
1363 case TREE_VEC:
1365 int len = TREE_VEC_LENGTH (*tp);
1366 while (len--)
1367 WALK_SUBTREE (TREE_VEC_ELT (*tp, len));
1369 break;
1371 case COMPLEX_CST:
1372 WALK_SUBTREE (TREE_REALPART (*tp));
1373 WALK_SUBTREE (TREE_IMAGPART (*tp));
1374 break;
1376 case CONSTRUCTOR:
1377 WALK_SUBTREE (CONSTRUCTOR_ELTS (*tp));
1378 break;
1380 case METHOD_TYPE:
1381 WALK_SUBTREE (TYPE_METHOD_BASETYPE (*tp));
1382 /* Fall through. */
1384 case FUNCTION_TYPE:
1385 WALK_SUBTREE (TREE_TYPE (*tp));
1386 WALK_SUBTREE (TYPE_ARG_TYPES (*tp));
1387 break;
1389 case ARRAY_TYPE:
1390 WALK_SUBTREE (TREE_TYPE (*tp));
1391 WALK_SUBTREE (TYPE_DOMAIN (*tp));
1392 break;
1394 case INTEGER_TYPE:
1395 WALK_SUBTREE (TYPE_MIN_VALUE (*tp));
1396 WALK_SUBTREE (TYPE_MAX_VALUE (*tp));
1397 break;
1399 case OFFSET_TYPE:
1400 WALK_SUBTREE (TREE_TYPE (*tp));
1401 WALK_SUBTREE (TYPE_OFFSET_BASETYPE (*tp));
1402 break;
1404 case RECORD_TYPE:
1405 if (TYPE_PTRMEMFUNC_P (*tp))
1406 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (*tp));
1407 break;
1409 default:
1410 my_friendly_abort (19990803);
1413 /* We didn't find what we were looking for. */
1414 return NULL_TREE;
1416 #undef WALK_SUBTREE
1419 /* Called from count_trees via walk_tree. */
1421 static tree
1422 count_trees_r (tp, walk_subtrees, data)
1423 tree *tp ATTRIBUTE_UNUSED;
1424 int *walk_subtrees ATTRIBUTE_UNUSED;
1425 void *data;
1427 ++ *((int*) data);
1428 return NULL_TREE;
1431 /* Debugging function for measuring the rough complexity of a tree
1432 representation. */
1435 count_trees (t)
1436 tree t;
1438 int n_trees = 0;
1439 walk_tree (&t, count_trees_r, &n_trees);
1440 return n_trees;
1443 /* Passed to walk_tree. Checks for the use of types with no linkage. */
1445 static tree
1446 no_linkage_helper (tp, walk_subtrees, data)
1447 tree *tp;
1448 int *walk_subtrees ATTRIBUTE_UNUSED;
1449 void *data ATTRIBUTE_UNUSED;
1451 tree t = *tp;
1453 if (TYPE_P (t)
1454 && (IS_AGGR_TYPE (t) || TREE_CODE (t) == ENUMERAL_TYPE)
1455 && (decl_function_context (TYPE_MAIN_DECL (t))
1456 || ANON_AGGRNAME_P (TYPE_IDENTIFIER (t))))
1457 return t;
1458 return NULL_TREE;
1461 /* Check if the type T depends on a type with no linkage and if so, return
1462 it. */
1464 tree
1465 no_linkage_check (t)
1466 tree t;
1468 /* There's no point in checking linkage on template functions; we
1469 can't know their complete types. */
1470 if (processing_template_decl)
1471 return NULL_TREE;
1473 t = walk_tree (&t, no_linkage_helper, NULL);
1474 if (t != error_mark_node)
1475 return t;
1476 return NULL_TREE;
1479 /* Passed to walk_tree. Copies the node pointed to, if appropriate. */
1481 tree
1482 copy_tree_r (tp, walk_subtrees, data)
1483 tree *tp;
1484 int *walk_subtrees;
1485 void *data ATTRIBUTE_UNUSED;
1487 enum tree_code code = TREE_CODE (*tp);
1489 /* We make copies of most nodes. */
1490 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code))
1491 || TREE_CODE_CLASS (code) == 'r'
1492 || TREE_CODE_CLASS (code) == 'c'
1493 || TREE_CODE_CLASS (code) == 's'
1494 || code == TREE_LIST
1495 || code == TREE_VEC
1496 || code == OVERLOAD)
1498 /* Because the chain gets clobbered when we make a copy, we save it
1499 here. */
1500 tree chain = TREE_CHAIN (*tp);
1502 /* Copy the node. */
1503 *tp = copy_node (*tp);
1505 /* Now, restore the chain, if appropriate. That will cause
1506 walk_tree to walk into the chain as well. */
1507 if (code == PARM_DECL || code == TREE_LIST || code == OVERLOAD
1508 || statement_code_p (code))
1509 TREE_CHAIN (*tp) = chain;
1511 /* For now, we don't update BLOCKs when we make copies. So, we
1512 have to nullify all scope-statements. */
1513 if (TREE_CODE (*tp) == SCOPE_STMT)
1514 SCOPE_STMT_BLOCK (*tp) = NULL_TREE;
1516 else if (code == TEMPLATE_TEMPLATE_PARM)
1517 /* These must be copied specially. */
1518 *tp = copy_template_template_parm (*tp, NULL_TREE);
1519 else if (TREE_CODE_CLASS (code) == 't')
1520 /* There's no need to copy types, or anything beneath them. */
1521 *walk_subtrees = 0;
1523 return NULL_TREE;
1526 #ifdef GATHER_STATISTICS
1527 extern int depth_reached;
1528 #endif
1530 void
1531 print_lang_statistics ()
1533 print_search_statistics ();
1534 print_class_statistics ();
1535 #ifdef GATHER_STATISTICS
1536 fprintf (stderr, "maximum template instantiation depth reached: %d\n",
1537 depth_reached);
1538 #endif
1541 /* Return, as an INTEGER_CST node, the number of elements for TYPE
1542 (which is an ARRAY_TYPE). This counts only elements of the top
1543 array. */
1545 tree
1546 array_type_nelts_top (type)
1547 tree type;
1549 return fold (build (PLUS_EXPR, sizetype,
1550 array_type_nelts (type),
1551 integer_one_node));
1554 /* Return, as an INTEGER_CST node, the number of elements for TYPE
1555 (which is an ARRAY_TYPE). This one is a recursive count of all
1556 ARRAY_TYPEs that are clumped together. */
1558 tree
1559 array_type_nelts_total (type)
1560 tree type;
1562 tree sz = array_type_nelts_top (type);
1563 type = TREE_TYPE (type);
1564 while (TREE_CODE (type) == ARRAY_TYPE)
1566 tree n = array_type_nelts_top (type);
1567 sz = fold (build (MULT_EXPR, sizetype, sz, n));
1568 type = TREE_TYPE (type);
1570 return sz;
1573 /* Called from break_out_target_exprs via mapcar. */
1575 static tree
1576 bot_manip (tp, walk_subtrees, data)
1577 tree *tp;
1578 int *walk_subtrees;
1579 void *data;
1581 splay_tree target_remap = ((splay_tree) data);
1582 tree t = *tp;
1584 if (TREE_CODE (t) != TREE_LIST && ! TREE_SIDE_EFFECTS (t))
1586 /* There can't be any TARGET_EXPRs below this point. */
1587 *walk_subtrees = 0;
1588 return NULL_TREE;
1590 else if (TREE_CODE (t) == TARGET_EXPR)
1592 tree u;
1594 if (TREE_CODE (TREE_OPERAND (t, 1)) == AGGR_INIT_EXPR)
1596 mark_used (TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (t, 1), 0), 0));
1597 u = build_cplus_new
1598 (TREE_TYPE (t), break_out_target_exprs (TREE_OPERAND (t, 1)));
1600 else
1602 tree var;
1604 u = copy_node (t);
1605 var = build (VAR_DECL, TREE_TYPE (t));
1606 DECL_CONTEXT (var) = current_function_decl;
1607 layout_decl (var, 0);
1608 TREE_OPERAND (u, 0) = var;
1611 /* Map the old variable to the new one. */
1612 splay_tree_insert (target_remap,
1613 (splay_tree_key) TREE_OPERAND (t, 0),
1614 (splay_tree_value) TREE_OPERAND (u, 0));
1616 /* Replace the old expression with the new version. */
1617 *tp = u;
1618 /* We don't have to go below this point; the recursive call to
1619 break_out_target_exprs will have handled anything below this
1620 point. */
1621 *walk_subtrees = 0;
1622 return NULL_TREE;
1624 else if (TREE_CODE (t) == CALL_EXPR)
1625 mark_used (TREE_OPERAND (TREE_OPERAND (t, 0), 0));
1627 /* Make a copy of this node. */
1628 return copy_tree_r (tp, walk_subtrees, NULL);
1631 /* Replace all remapped VAR_DECLs in T with their new equivalents.
1632 DATA is really a splay-tree mapping old variables to new
1633 variables. */
1635 static tree
1636 bot_replace (t, walk_subtrees, data)
1637 tree *t;
1638 int *walk_subtrees ATTRIBUTE_UNUSED;
1639 void *data;
1641 splay_tree target_remap = ((splay_tree) data);
1643 if (TREE_CODE (*t) == VAR_DECL)
1645 splay_tree_node n = splay_tree_lookup (target_remap,
1646 (splay_tree_key) *t);
1647 if (n)
1648 *t = (tree) n->value;
1651 return NULL_TREE;
1654 /* When we parse a default argument expression, we may create
1655 temporary variables via TARGET_EXPRs. When we actually use the
1656 default-argument expression, we make a copy of the expression, but
1657 we must replace the temporaries with appropriate local versions. */
1659 tree
1660 break_out_target_exprs (t)
1661 tree t;
1663 static int target_remap_count;
1664 static splay_tree target_remap;
1666 if (!target_remap_count++)
1667 target_remap = splay_tree_new (splay_tree_compare_pointers,
1668 /*splay_tree_delete_key_fn=*/NULL,
1669 /*splay_tree_delete_value_fn=*/NULL);
1670 walk_tree (&t, bot_manip, target_remap);
1671 walk_tree (&t, bot_replace, target_remap);
1673 if (!--target_remap_count)
1675 splay_tree_delete (target_remap);
1676 target_remap = NULL;
1679 return t;
1682 /* Obstack used for allocating nodes in template function and variable
1683 definitions. */
1685 /* Similar to `build_nt', except that we set TREE_COMPLEXITY to be the
1686 current line number. */
1688 tree
1689 build_min_nt VPARAMS ((enum tree_code code, ...))
1691 #ifndef ANSI_PROTOTYPES
1692 enum tree_code code;
1693 #endif
1694 va_list p;
1695 register tree t;
1696 register int length;
1697 register int i;
1699 VA_START (p, code);
1701 #ifndef ANSI_PROTOTYPES
1702 code = va_arg (p, enum tree_code);
1703 #endif
1705 t = make_node (code);
1706 length = TREE_CODE_LENGTH (code);
1707 TREE_COMPLEXITY (t) = lineno;
1709 for (i = 0; i < length; i++)
1711 tree x = va_arg (p, tree);
1712 TREE_OPERAND (t, i) = x;
1715 va_end (p);
1716 return t;
1719 /* Similar to `build', except we set TREE_COMPLEXITY to the current
1720 line-number. */
1722 tree
1723 build_min VPARAMS ((enum tree_code code, tree tt, ...))
1725 #ifndef ANSI_PROTOTYPES
1726 enum tree_code code;
1727 tree tt;
1728 #endif
1729 va_list p;
1730 register tree t;
1731 register int length;
1732 register int i;
1734 VA_START (p, tt);
1736 #ifndef ANSI_PROTOTYPES
1737 code = va_arg (p, enum tree_code);
1738 tt = va_arg (p, tree);
1739 #endif
1741 t = make_node (code);
1742 length = TREE_CODE_LENGTH (code);
1743 TREE_TYPE (t) = tt;
1744 TREE_COMPLEXITY (t) = lineno;
1746 for (i = 0; i < length; i++)
1748 tree x = va_arg (p, tree);
1749 TREE_OPERAND (t, i) = x;
1752 va_end (p);
1753 return t;
1756 /* Returns an INTEGER_CST (of type `int') corresponding to I.
1757 Multiple calls with the same value of I may or may not yield the
1758 same node; therefore, callers should never modify the node
1759 returned. */
1761 tree
1762 build_shared_int_cst (i)
1763 int i;
1765 static tree cache[256];
1767 if (i >= 256)
1768 return build_int_2 (i, 0);
1770 if (!cache[i])
1771 cache[i] = build_int_2 (i, 0);
1773 return cache[i];
1776 tree
1777 get_type_decl (t)
1778 tree t;
1780 if (TREE_CODE (t) == TYPE_DECL)
1781 return t;
1782 if (TYPE_P (t))
1783 return TYPE_STUB_DECL (t);
1785 my_friendly_abort (42);
1787 /* Stop compiler from complaining control reaches end of non-void function. */
1788 return 0;
1792 can_free (obstack, t)
1793 struct obstack *obstack;
1794 tree t;
1796 int size = 0;
1798 if (TREE_CODE (t) == TREE_VEC)
1799 size = (TREE_VEC_LENGTH (t)-1) * sizeof (tree) + sizeof (struct tree_vec);
1800 else
1801 my_friendly_abort (42);
1803 #define ROUND(x) ((x + obstack_alignment_mask (obstack)) \
1804 & ~ obstack_alignment_mask (obstack))
1805 if ((char *)t + ROUND (size) == obstack_next_free (obstack))
1806 return 1;
1807 #undef ROUND
1809 return 0;
1812 /* Return first vector element whose BINFO_TYPE is ELEM.
1813 Return 0 if ELEM is not in VEC. VEC may be NULL_TREE. */
1815 tree
1816 vec_binfo_member (elem, vec)
1817 tree elem, vec;
1819 int i;
1821 if (vec)
1822 for (i = 0; i < TREE_VEC_LENGTH (vec); ++i)
1823 if (same_type_p (elem, BINFO_TYPE (TREE_VEC_ELT (vec, i))))
1824 return TREE_VEC_ELT (vec, i);
1826 return NULL_TREE;
1829 /* Returns the namespace that contains DECL, whether directly or
1830 indirectly. */
1832 tree
1833 decl_namespace_context (decl)
1834 tree decl;
1836 while (1)
1838 if (TREE_CODE (decl) == NAMESPACE_DECL)
1839 return decl;
1840 else if (TYPE_P (decl))
1841 decl = CP_DECL_CONTEXT (TYPE_MAIN_DECL (decl));
1842 else
1843 decl = CP_DECL_CONTEXT (decl);
1847 /* Return truthvalue of whether T1 is the same tree structure as T2.
1848 Return 1 if they are the same.
1849 Return 0 if they are understandably different.
1850 Return -1 if either contains tree structure not understood by
1851 this function. */
1854 cp_tree_equal (t1, t2)
1855 tree t1, t2;
1857 register enum tree_code code1, code2;
1858 int cmp;
1860 if (t1 == t2)
1861 return 1;
1862 if (t1 == 0 || t2 == 0)
1863 return 0;
1865 code1 = TREE_CODE (t1);
1866 code2 = TREE_CODE (t2);
1868 if (code1 == NOP_EXPR || code1 == CONVERT_EXPR || code1 == NON_LVALUE_EXPR)
1870 if (code2 == NOP_EXPR || code2 == CONVERT_EXPR || code2 == NON_LVALUE_EXPR)
1871 return cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
1872 else
1873 return cp_tree_equal (TREE_OPERAND (t1, 0), t2);
1875 else if (code2 == NOP_EXPR || code2 == CONVERT_EXPR
1876 || code2 == NON_LVALUE_EXPR)
1877 return cp_tree_equal (t1, TREE_OPERAND (t2, 0));
1879 if (code1 != code2)
1880 return 0;
1882 switch (code1)
1884 case INTEGER_CST:
1885 return TREE_INT_CST_LOW (t1) == TREE_INT_CST_LOW (t2)
1886 && TREE_INT_CST_HIGH (t1) == TREE_INT_CST_HIGH (t2);
1888 case REAL_CST:
1889 return REAL_VALUES_EQUAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
1891 case STRING_CST:
1892 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
1893 && !bcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
1894 TREE_STRING_LENGTH (t1));
1896 case CONSTRUCTOR:
1897 /* We need to do this when determining whether or not two
1898 non-type pointer to member function template arguments
1899 are the same. */
1900 if (!(same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))
1901 /* The first operand is RTL. */
1902 && TREE_OPERAND (t1, 0) == TREE_OPERAND (t2, 0)))
1903 return 0;
1904 return cp_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
1906 case TREE_LIST:
1907 cmp = cp_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2));
1908 if (cmp <= 0)
1909 return cmp;
1910 cmp = cp_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2));
1911 if (cmp <= 0)
1912 return cmp;
1913 return cp_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
1915 case SAVE_EXPR:
1916 return cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
1918 case CALL_EXPR:
1919 cmp = cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
1920 if (cmp <= 0)
1921 return cmp;
1922 return simple_cst_list_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
1924 case TARGET_EXPR:
1925 /* Special case: if either target is an unallocated VAR_DECL,
1926 it means that it's going to be unified with whatever the
1927 TARGET_EXPR is really supposed to initialize, so treat it
1928 as being equivalent to anything. */
1929 if ((TREE_CODE (TREE_OPERAND (t1, 0)) == VAR_DECL
1930 && DECL_NAME (TREE_OPERAND (t1, 0)) == NULL_TREE
1931 && DECL_RTL (TREE_OPERAND (t1, 0)) == 0)
1932 || (TREE_CODE (TREE_OPERAND (t2, 0)) == VAR_DECL
1933 && DECL_NAME (TREE_OPERAND (t2, 0)) == NULL_TREE
1934 && DECL_RTL (TREE_OPERAND (t2, 0)) == 0))
1935 cmp = 1;
1936 else
1937 cmp = cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
1938 if (cmp <= 0)
1939 return cmp;
1940 return cp_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
1942 case WITH_CLEANUP_EXPR:
1943 cmp = cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
1944 if (cmp <= 0)
1945 return cmp;
1946 return cp_tree_equal (TREE_OPERAND (t1, 2), TREE_OPERAND (t1, 2));
1948 case COMPONENT_REF:
1949 if (TREE_OPERAND (t1, 1) == TREE_OPERAND (t2, 1))
1950 return cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
1951 return 0;
1953 case VAR_DECL:
1954 case PARM_DECL:
1955 case CONST_DECL:
1956 case FUNCTION_DECL:
1957 return 0;
1959 case TEMPLATE_PARM_INDEX:
1960 return TEMPLATE_PARM_IDX (t1) == TEMPLATE_PARM_IDX (t2)
1961 && TEMPLATE_PARM_LEVEL (t1) == TEMPLATE_PARM_LEVEL (t2);
1963 case SIZEOF_EXPR:
1964 case ALIGNOF_EXPR:
1965 if (TREE_CODE (TREE_OPERAND (t1, 0)) != TREE_CODE (TREE_OPERAND (t2, 0)))
1966 return 0;
1967 if (TYPE_P (TREE_OPERAND (t1, 0)))
1968 return same_type_p (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
1969 break;
1971 case PTRMEM_CST:
1972 /* Two pointer-to-members are the same if they point to the same
1973 field or function in the same class. */
1974 return (PTRMEM_CST_MEMBER (t1) == PTRMEM_CST_MEMBER (t2)
1975 && same_type_p (PTRMEM_CST_CLASS (t1), PTRMEM_CST_CLASS (t2)));
1977 default:
1978 break;
1981 switch (TREE_CODE_CLASS (code1))
1983 int i;
1984 case '1':
1985 case '2':
1986 case '<':
1987 case 'e':
1988 case 'r':
1989 case 's':
1990 cmp = 1;
1991 for (i = 0; i < TREE_CODE_LENGTH (code1); ++i)
1993 cmp = cp_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i));
1994 if (cmp <= 0)
1995 return cmp;
1997 return cmp;
2000 return -1;
2003 /* Build a wrapper around some pointer PTR so we can use it as a tree. */
2005 tree
2006 build_ptr_wrapper (ptr)
2007 void *ptr;
2009 tree t = make_node (WRAPPER);
2010 WRAPPER_PTR (t) = ptr;
2011 return t;
2014 /* Same, but on the expression_obstack. */
2016 tree
2017 build_expr_ptr_wrapper (ptr)
2018 void *ptr;
2020 return build_ptr_wrapper (ptr);
2023 /* Build a wrapper around some integer I so we can use it as a tree. */
2025 tree
2026 build_int_wrapper (i)
2027 int i;
2029 tree t = make_node (WRAPPER);
2030 WRAPPER_INT (t) = i;
2031 return t;
2034 static tree
2035 build_srcloc (file, line)
2036 const char *file;
2037 int line;
2039 tree t;
2041 t = make_node (SRCLOC);
2042 SRCLOC_FILE (t) = file;
2043 SRCLOC_LINE (t) = line;
2045 return t;
2048 tree
2049 build_srcloc_here ()
2051 return build_srcloc (input_filename, lineno);
2054 /* The type of ARG when used as an lvalue. */
2056 tree
2057 lvalue_type (arg)
2058 tree arg;
2060 tree type = TREE_TYPE (arg);
2061 if (TREE_CODE (arg) == OVERLOAD)
2062 type = unknown_type_node;
2063 return type;
2066 /* The type of ARG for printing error messages; denote lvalues with
2067 reference types. */
2069 tree
2070 error_type (arg)
2071 tree arg;
2073 tree type = TREE_TYPE (arg);
2074 if (TREE_CODE (type) == ARRAY_TYPE)
2076 else if (real_lvalue_p (arg))
2077 type = build_reference_type (lvalue_type (arg));
2078 else if (IS_AGGR_TYPE (type))
2079 type = lvalue_type (arg);
2081 return type;
2084 /* Does FUNCTION use a variable-length argument list? */
2087 varargs_function_p (function)
2088 tree function;
2090 tree parm = TYPE_ARG_TYPES (TREE_TYPE (function));
2091 for (; parm; parm = TREE_CHAIN (parm))
2092 if (TREE_VALUE (parm) == void_type_node)
2093 return 0;
2094 return 1;
2097 /* Returns 1 if decl is a member of a class. */
2100 member_p (decl)
2101 tree decl;
2103 const tree ctx = DECL_CONTEXT (decl);
2104 return (ctx && TYPE_P (ctx));
2107 /* Create a placeholder for member access where we don't actually have an
2108 object that the access is against. */
2110 tree
2111 build_dummy_object (type)
2112 tree type;
2114 tree decl = build1 (NOP_EXPR, build_pointer_type (type), void_zero_node);
2115 return build_indirect_ref (decl, NULL_PTR);
2118 /* We've gotten a reference to a member of TYPE. Return *this if appropriate,
2119 or a dummy object otherwise. If BINFOP is non-0, it is filled with the
2120 binfo path from current_class_type to TYPE, or 0. */
2122 tree
2123 maybe_dummy_object (type, binfop)
2124 tree type;
2125 tree *binfop;
2127 tree decl, context;
2129 if (current_class_type
2130 && get_base_distance (type, current_class_type, 0, binfop) != -1)
2131 context = current_class_type;
2132 else
2134 /* Reference from a nested class member function. */
2135 context = type;
2136 if (binfop)
2137 *binfop = TYPE_BINFO (type);
2140 if (current_class_ref && context == current_class_type)
2141 decl = current_class_ref;
2142 else
2143 decl = build_dummy_object (context);
2145 return decl;
2148 /* Returns 1 if OB is a placeholder object, or a pointer to one. */
2151 is_dummy_object (ob)
2152 tree ob;
2154 if (TREE_CODE (ob) == INDIRECT_REF)
2155 ob = TREE_OPERAND (ob, 0);
2156 return (TREE_CODE (ob) == NOP_EXPR
2157 && TREE_OPERAND (ob, 0) == void_zero_node);
2160 /* Returns 1 iff type T is a POD type, as defined in [basic.types]. */
2163 pod_type_p (t)
2164 tree t;
2166 while (TREE_CODE (t) == ARRAY_TYPE)
2167 t = TREE_TYPE (t);
2169 if (INTEGRAL_TYPE_P (t))
2170 return 1; /* integral, character or enumeral type */
2171 if (FLOAT_TYPE_P (t))
2172 return 1;
2173 if (TYPE_PTR_P (t))
2174 return 1; /* pointer to non-member */
2175 if (TYPE_PTRMEM_P (t))
2176 return 1; /* pointer to member object */
2177 if (TYPE_PTRMEMFUNC_P (t))
2178 return 1; /* pointer to member function */
2180 if (! CLASS_TYPE_P (t))
2181 return 0; /* other non-class type (reference or function) */
2182 if (CLASSTYPE_NON_POD_P (t))
2183 return 0;
2184 return 1;
2187 /* Return a 1 if ATTR_NAME and ATTR_ARGS denote a valid C++-specific
2188 attribute for either declaration DECL or type TYPE and 0 otherwise.
2189 Plugged into valid_lang_attribute. */
2192 cp_valid_lang_attribute (attr_name, attr_args, decl, type)
2193 tree attr_name;
2194 tree attr_args ATTRIBUTE_UNUSED;
2195 tree decl ATTRIBUTE_UNUSED;
2196 tree type ATTRIBUTE_UNUSED;
2198 if (is_attribute_p ("com_interface", attr_name))
2200 if (! flag_vtable_thunks)
2202 error ("`com_interface' only supported with -fvtable-thunks");
2203 return 0;
2206 if (attr_args != NULL_TREE
2207 || decl != NULL_TREE
2208 || ! CLASS_TYPE_P (type)
2209 || type != TYPE_MAIN_VARIANT (type))
2211 warning ("`com_interface' attribute can only be applied to class definitions");
2212 return 0;
2215 CLASSTYPE_COM_INTERFACE (type) = 1;
2216 return 1;
2218 else if (is_attribute_p ("init_priority", attr_name))
2220 tree initp_expr = (attr_args ? TREE_VALUE (attr_args): NULL_TREE);
2221 int pri;
2223 if (initp_expr)
2224 STRIP_NOPS (initp_expr);
2226 if (!initp_expr || TREE_CODE (initp_expr) != INTEGER_CST)
2228 error ("requested init_priority is not an integer constant");
2229 return 0;
2232 pri = TREE_INT_CST_LOW (initp_expr);
2234 while (TREE_CODE (type) == ARRAY_TYPE)
2235 type = TREE_TYPE (type);
2237 if (decl == NULL_TREE
2238 || TREE_CODE (decl) != VAR_DECL
2239 || ! TREE_STATIC (decl)
2240 || DECL_EXTERNAL (decl)
2241 || (TREE_CODE (type) != RECORD_TYPE
2242 && TREE_CODE (type) != UNION_TYPE)
2243 /* Static objects in functions are initialized the
2244 first time control passes through that
2245 function. This is not precise enough to pin down an
2246 init_priority value, so don't allow it. */
2247 || current_function_decl)
2249 error ("can only use init_priority attribute on file-scope definitions of objects of class type");
2250 return 0;
2253 if (pri > MAX_INIT_PRIORITY || pri <= 0)
2255 error ("requested init_priority is out of range");
2256 return 0;
2259 /* Check for init_priorities that are reserved for
2260 language and runtime support implementations.*/
2261 if (pri <= MAX_RESERVED_INIT_PRIORITY)
2263 warning
2264 ("requested init_priority is reserved for internal use");
2267 DECL_INIT_PRIORITY (decl) = pri;
2268 return 1;
2271 return 0;
2274 /* Return a new PTRMEM_CST of the indicated TYPE. The MEMBER is the
2275 thing pointed to by the constant. */
2277 tree
2278 make_ptrmem_cst (type, member)
2279 tree type;
2280 tree member;
2282 tree ptrmem_cst = make_node (PTRMEM_CST);
2283 /* If would seem a great convenience if make_node would set
2284 TREE_CONSTANT for things of class `c', but it does not. */
2285 TREE_CONSTANT (ptrmem_cst) = 1;
2286 TREE_TYPE (ptrmem_cst) = type;
2287 PTRMEM_CST_MEMBER (ptrmem_cst) = member;
2288 return ptrmem_cst;
2291 /* Mark ARG (which is really a list_hash_table **) for GC. */
2293 static void
2294 mark_list_hash (arg)
2295 void *arg;
2297 struct list_hash *lh;
2299 for (lh = * ((struct list_hash **) arg); lh; lh = lh->next)
2300 ggc_mark_tree (lh->list);
2303 /* Initialize tree.c. */
2305 void
2306 init_tree ()
2308 make_lang_type_fn = cp_make_lang_type;
2309 lang_unsave = cp_unsave;
2310 ggc_add_root (list_hash_table,
2311 sizeof (list_hash_table) / sizeof (struct list_hash *),
2312 sizeof (struct list_hash *),
2313 mark_list_hash);
2316 /* The SAVE_EXPR pointed to by TP is being copied. If ST contains
2317 information indicating to what new SAVE_EXPR this one should be
2318 mapped, use that one. Otherwise, create a new node and enter it in
2319 ST. FN is the function into which the copy will be placed. */
2321 void
2322 remap_save_expr (tp, st, fn, walk_subtrees)
2323 tree *tp;
2324 splay_tree st;
2325 tree fn;
2326 int *walk_subtrees;
2328 splay_tree_node n;
2330 /* See if we already encountered this SAVE_EXPR. */
2331 n = splay_tree_lookup (st, (splay_tree_key) *tp);
2333 /* If we didn't already remap this SAVE_EXPR, do so now. */
2334 if (!n)
2336 tree t = copy_node (*tp);
2338 /* The SAVE_EXPR is now part of the function into which we
2339 are inlining this body. */
2340 SAVE_EXPR_CONTEXT (t) = fn;
2341 /* And we haven't evaluated it yet. */
2342 SAVE_EXPR_RTL (t) = NULL_RTX;
2343 /* Remember this SAVE_EXPR. */
2344 n = splay_tree_insert (st,
2345 (splay_tree_key) *tp,
2346 (splay_tree_value) t);
2348 else
2349 /* We've already walked into this SAVE_EXPR, so we needn't do it
2350 again. */
2351 *walk_subtrees = 0;
2353 /* Replace this SAVE_EXPR with the copy. */
2354 *tp = (tree) n->value;
2357 /* Called via walk_tree. If *TP points to a DECL_STMT for a local
2358 declaration, copies the declaration and enters it in the splay_tree
2359 pointed to by DATA (which is really a `splay_tree *'). */
2361 static tree
2362 mark_local_for_remap_r (tp, walk_subtrees, data)
2363 tree *tp;
2364 int *walk_subtrees ATTRIBUTE_UNUSED;
2365 void *data;
2367 tree t = *tp;
2368 splay_tree st = (splay_tree) data;
2369 tree decl;
2372 if (TREE_CODE (t) == DECL_STMT
2373 && nonstatic_local_decl_p (DECL_STMT_DECL (t)))
2374 decl = DECL_STMT_DECL (t);
2375 else if (TREE_CODE (t) == LABEL_STMT)
2376 decl = LABEL_STMT_LABEL (t);
2377 else if (TREE_CODE (t) == TARGET_EXPR
2378 && nonstatic_local_decl_p (TREE_OPERAND (t, 0)))
2379 decl = TREE_OPERAND (t, 0);
2380 else
2381 decl = NULL_TREE;
2383 if (decl)
2385 tree copy;
2387 /* Make a copy. */
2388 copy = copy_decl_for_inlining (decl,
2389 DECL_CONTEXT (decl),
2390 DECL_CONTEXT (decl));
2392 /* Remember the copy. */
2393 splay_tree_insert (st,
2394 (splay_tree_key) decl,
2395 (splay_tree_value) copy);
2398 return NULL_TREE;
2401 /* Called via walk_tree when an expression is unsaved. Using the
2402 splay_tree pointed to by ST (which is really a `splay_tree'),
2403 remaps all local declarations to appropriate replacements. */
2405 static tree
2406 cp_unsave_r (tp, walk_subtrees, data)
2407 tree *tp;
2408 int *walk_subtrees;
2409 void *data;
2411 splay_tree st = (splay_tree) data;
2412 splay_tree_node n;
2414 /* Only a local declaration (variable or label). */
2415 if (nonstatic_local_decl_p (*tp))
2417 /* Lookup the declaration. */
2418 n = splay_tree_lookup (st, (splay_tree_key) *tp);
2420 /* If it's there, remap it. */
2421 if (n)
2422 *tp = (tree) n->value;
2424 else if (TREE_CODE (*tp) == SAVE_EXPR)
2425 remap_save_expr (tp, st, current_function_decl, walk_subtrees);
2426 else
2428 copy_tree_r (tp, walk_subtrees, NULL);
2430 /* Do whatever unsaving is required. */
2431 unsave_expr_1 (*tp);
2434 /* Keep iterating. */
2435 return NULL_TREE;
2438 /* Called by unsave_expr_now whenever an expression (*TP) needs to be
2439 unsaved. */
2441 static void
2442 cp_unsave (tp)
2443 tree *tp;
2445 splay_tree st;
2447 /* Create a splay-tree to map old local variable declarations to new
2448 ones. */
2449 st = splay_tree_new (splay_tree_compare_pointers, NULL, NULL);
2451 /* Walk the tree once figuring out what needs to be remapped. */
2452 walk_tree (tp, mark_local_for_remap_r, st);
2454 /* Walk the tree again, copying, remapping, and unsaving. */
2455 walk_tree (tp, cp_unsave_r, st);
2457 /* Clean up. */
2458 splay_tree_delete (st);
2461 /* Returns the kind of special function that DECL (a FUNCTION_DECL)
2462 is. Note that this sfk_none is zero, so this function can be used
2463 as a predicate to test whether or not DECL is a special function. */
2465 special_function_kind
2466 special_function_p (decl)
2467 tree decl;
2469 /* Rather than doing all this stuff with magic names, we should
2470 probably have a field of type `special_function_kind' in
2471 DECL_LANG_SPECIFIC. */
2472 if (DECL_COPY_CONSTRUCTOR_P (decl))
2473 return sfk_copy_constructor;
2474 if (DECL_CONSTRUCTOR_P (decl))
2475 return sfk_constructor;
2476 if (DECL_OVERLOADED_OPERATOR_P (decl) == NOP_EXPR)
2477 return sfk_assignment_operator;
2478 if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (decl))
2479 return sfk_destructor;
2480 if (DECL_COMPLETE_DESTRUCTOR_P (decl))
2481 return sfk_complete_destructor;
2482 if (DECL_BASE_DESTRUCTOR_P (decl))
2483 return sfk_base_destructor;
2484 if (DECL_DELETING_DESTRUCTOR_P (decl))
2485 return sfk_deleting_destructor;
2486 if (DECL_CONV_FN_P (decl))
2487 return sfk_conversion;
2489 return sfk_none;