(all insn and peephole patterns): Rewrite without using arm_output_asm_insn.
[official-gcc.git] / gcc / cp / tree.c
blobb4712a6ac4d6e1816563bc22ddd91496be79ecce
1 /* Language-dependent node constructors for parse phase of GNU compiler.
2 Copyright (C) 1987, 1988, 1992, 1993 Free Software Foundation, Inc.
3 Hacked by Michael Tiemann (tiemann@cygnus.com)
5 This file is part of GNU CC.
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to
19 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
21 #include "config.h"
22 #include <stdio.h>
23 #include "obstack.h"
24 #include "tree.h"
25 #include "cp-tree.h"
26 #include "flags.h"
28 #define CEIL(x,y) (((x) + (y) - 1) / (y))
30 /* Return nonzero if REF is an lvalue valid for this language.
31 Lvalues can be assigned, unless they have TREE_READONLY.
32 Lvalues can have their address taken, unless they have DECL_REGISTER. */
34 int
35 lvalue_p (ref)
36 tree ref;
38 register enum tree_code code = TREE_CODE (ref);
40 if (language_lvalue_valid (ref))
42 if (TREE_CODE (TREE_TYPE (ref)) == REFERENCE_TYPE)
43 return 1;
45 switch (code)
47 /* preincrements and predecrements are valid lvals, provided
48 what they refer to are valid lvals. */
49 case PREINCREMENT_EXPR:
50 case PREDECREMENT_EXPR:
51 case COMPONENT_REF:
52 case SAVE_EXPR:
53 return lvalue_p (TREE_OPERAND (ref, 0));
55 case STRING_CST:
56 return 1;
58 case VAR_DECL:
59 if (TREE_READONLY (ref) && ! TREE_STATIC (ref)
60 && DECL_LANG_SPECIFIC (ref)
61 && DECL_IN_AGGR_P (ref))
62 return 0;
63 case INDIRECT_REF:
64 case ARRAY_REF:
65 case PARM_DECL:
66 case RESULT_DECL:
67 case ERROR_MARK:
68 if (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
69 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE)
70 return 1;
71 break;
73 case TARGET_EXPR:
74 case WITH_CLEANUP_EXPR:
75 return 1;
77 case CALL_EXPR:
78 /* unary_complex_lvalue knows how to deal with this case. */
79 if (TREE_ADDRESSABLE (TREE_TYPE (ref)))
80 return 1;
81 break;
83 /* A currently unresolved scope ref. */
84 case SCOPE_REF:
85 my_friendly_abort (103);
86 case OFFSET_REF:
87 if (TREE_CODE (TREE_OPERAND (ref, 1)) == FUNCTION_DECL)
88 return 1;
89 return lvalue_p (TREE_OPERAND (ref, 0))
90 && lvalue_p (TREE_OPERAND (ref, 1));
91 break;
93 case COND_EXPR:
94 return (lvalue_p (TREE_OPERAND (ref, 1))
95 && lvalue_p (TREE_OPERAND (ref, 2)));
97 case MODIFY_EXPR:
98 return 1;
100 case COMPOUND_EXPR:
101 return lvalue_p (TREE_OPERAND (ref, 1));
104 return 0;
107 /* Return nonzero if REF is an lvalue valid for this language;
108 otherwise, print an error message and return zero. */
111 lvalue_or_else (ref, string)
112 tree ref;
113 char *string;
115 int win = lvalue_p (ref);
116 if (! win)
117 error ("non-lvalue in %s", string);
118 return win;
121 /* INIT is a CALL_EXPR which needs info about its target.
122 TYPE is the type that this initialization should appear to have.
124 Build an encapsulation of the initialization to perform
125 and return it so that it can be processed by language-independent
126 and language-specific expression expanders.
128 If WITH_CLEANUP_P is nonzero, we build a cleanup for this expression.
129 Otherwise, cleanups are not built here. For example, when building
130 an initialization for a stack slot, since the called function handles
131 the cleanup, we would not want to do it here. */
132 tree
133 build_cplus_new (type, init, with_cleanup_p)
134 tree type;
135 tree init;
136 int with_cleanup_p;
138 tree slot = build (VAR_DECL, type);
139 tree rval = build (NEW_EXPR, type,
140 TREE_OPERAND (init, 0), TREE_OPERAND (init, 1), slot);
141 TREE_SIDE_EFFECTS (rval) = 1;
142 TREE_ADDRESSABLE (rval) = 1;
143 rval = build (TARGET_EXPR, type, slot, rval, 0);
144 TREE_SIDE_EFFECTS (rval) = 1;
145 TREE_ADDRESSABLE (rval) = 1;
147 if (with_cleanup_p && TYPE_NEEDS_DESTRUCTOR (type))
149 TREE_OPERAND (rval, 2) = error_mark_node;
150 rval = build (WITH_CLEANUP_EXPR, type, rval, 0,
151 build_delete (TYPE_POINTER_TO (type),
152 build_unary_op (ADDR_EXPR, slot, 0),
153 integer_two_node,
154 LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 0));
155 TREE_SIDE_EFFECTS (rval) = 1;
156 TREE_ADDRESSABLE (rval) = 1;
158 return rval;
161 /* Recursively search EXP for CALL_EXPRs that need cleanups and replace
162 these CALL_EXPRs with tree nodes that will perform the cleanups. */
164 tree
165 break_out_cleanups (exp)
166 tree exp;
168 tree tmp = exp;
170 if (TREE_CODE (tmp) == CALL_EXPR
171 && TYPE_NEEDS_DESTRUCTOR (TREE_TYPE (tmp)))
172 return build_cplus_new (TREE_TYPE (tmp), tmp, 1);
174 while (TREE_CODE (tmp) == NOP_EXPR
175 || TREE_CODE (tmp) == CONVERT_EXPR
176 || TREE_CODE (tmp) == NON_LVALUE_EXPR)
178 if (TREE_CODE (TREE_OPERAND (tmp, 0)) == CALL_EXPR
179 && TYPE_NEEDS_DESTRUCTOR (TREE_TYPE (TREE_OPERAND (tmp, 0))))
181 TREE_OPERAND (tmp, 0)
182 = build_cplus_new (TREE_TYPE (TREE_OPERAND (tmp, 0)),
183 TREE_OPERAND (tmp, 0), 1);
184 break;
186 else
187 tmp = TREE_OPERAND (tmp, 0);
189 return exp;
192 /* Recursively perform a preorder search EXP for CALL_EXPRs, making
193 copies where they are found. Returns a deep copy all nodes transitively
194 containing CALL_EXPRs. */
196 tree
197 break_out_calls (exp)
198 tree exp;
200 register tree t1, t2;
201 register enum tree_code code;
202 register int changed = 0;
203 register int i;
205 if (exp == NULL_TREE)
206 return exp;
208 code = TREE_CODE (exp);
210 if (code == CALL_EXPR)
211 return copy_node (exp);
213 /* Don't try and defeat a save_expr, as it should only be done once. */
214 if (code == SAVE_EXPR)
215 return exp;
217 switch (TREE_CODE_CLASS (code))
219 default:
220 abort ();
222 case 'c': /* a constant */
223 case 't': /* a type node */
224 case 'x': /* something random, like an identifier or an ERROR_MARK. */
225 return exp;
227 case 'd': /* A decl node */
228 t1 = break_out_calls (DECL_INITIAL (exp));
229 if (t1 != DECL_INITIAL (exp))
231 exp = copy_node (exp);
232 DECL_INITIAL (exp) = t1;
234 return exp;
236 case 'b': /* A block node */
238 /* Don't know how to handle these correctly yet. Must do a
239 break_out_calls on all DECL_INITIAL values for local variables,
240 and also break_out_calls on all sub-blocks and sub-statements. */
241 abort ();
243 return exp;
245 case 'e': /* an expression */
246 case 'r': /* a reference */
247 case 's': /* an expression with side effects */
248 for (i = tree_code_length[(int) code] - 1; i >= 0; i--)
250 t1 = break_out_calls (TREE_OPERAND (exp, i));
251 if (t1 != TREE_OPERAND (exp, i))
253 exp = copy_node (exp);
254 TREE_OPERAND (exp, i) = t1;
257 return exp;
259 case '<': /* a comparison expression */
260 case '2': /* a binary arithmetic expression */
261 t2 = break_out_calls (TREE_OPERAND (exp, 1));
262 if (t2 != TREE_OPERAND (exp, 1))
263 changed = 1;
264 case '1': /* a unary arithmetic expression */
265 t1 = break_out_calls (TREE_OPERAND (exp, 0));
266 if (t1 != TREE_OPERAND (exp, 0))
267 changed = 1;
268 if (changed)
270 if (tree_code_length[(int) code] == 1)
271 return build1 (code, TREE_TYPE (exp), t1);
272 else
273 return build (code, TREE_TYPE (exp), t1, t2);
275 return exp;
280 extern struct obstack *current_obstack;
281 extern struct obstack permanent_obstack, class_obstack;
282 extern struct obstack *saveable_obstack;
284 /* Here is how primitive or already-canonicalized types' hash
285 codes are made. MUST BE CONSISTENT WITH tree.c !!! */
286 #define TYPE_HASH(TYPE) ((HOST_WIDE_INT) (TYPE) & 0777777)
288 /* Construct, lay out and return the type of methods belonging to class
289 BASETYPE and whose arguments are described by ARGTYPES and whose values
290 are described by RETTYPE. If each type exists already, reuse it. */
291 tree
292 build_cplus_method_type (basetype, rettype, argtypes)
293 tree basetype, rettype, argtypes;
295 register tree t;
296 tree ptype;
297 int hashcode;
299 /* Make a node of the sort we want. */
300 t = make_node (METHOD_TYPE);
302 TYPE_METHOD_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
303 TREE_TYPE (t) = rettype;
304 if (IS_SIGNATURE (basetype))
305 ptype = build_signature_pointer_type (TYPE_MAIN_VARIANT (basetype),
306 TYPE_READONLY (basetype),
307 TYPE_VOLATILE (basetype));
308 else
310 ptype = build_pointer_type (basetype);
311 ptype = build_type_variant (ptype, 1, 0);
313 /* The actual arglist for this function includes a "hidden" argument
314 which is "this". Put it into the list of argument types. */
316 argtypes = tree_cons (NULL_TREE, ptype, argtypes);
317 TYPE_ARG_TYPES (t) = argtypes;
318 TREE_SIDE_EFFECTS (argtypes) = 1; /* Mark first argtype as "artificial". */
320 /* If we already have such a type, use the old one and free this one.
321 Note that it also frees up the above cons cell if found. */
322 hashcode = TYPE_HASH (basetype) + TYPE_HASH (rettype) + type_hash_list (argtypes);
323 t = type_hash_canon (hashcode, t);
325 if (TYPE_SIZE (t) == 0)
326 layout_type (t);
328 return t;
331 tree
332 build_cplus_staticfn_type (basetype, rettype, argtypes)
333 tree basetype, rettype, argtypes;
335 register tree t;
336 int hashcode;
338 /* Make a node of the sort we want. */
339 t = make_node (FUNCTION_TYPE);
341 TYPE_METHOD_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
342 TREE_TYPE (t) = rettype;
344 /* The actual arglist for this function includes a "hidden" argument
345 which is "this". Put it into the list of argument types. */
347 TYPE_ARG_TYPES (t) = argtypes;
349 /* If we already have such a type, use the old one and free this one.
350 Note that it also frees up the above cons cell if found. */
351 hashcode = TYPE_HASH (basetype) + TYPE_HASH (rettype) + type_hash_list (argtypes);
352 t = type_hash_canon (hashcode, t);
354 if (TYPE_SIZE (t) == 0)
355 layout_type (t);
357 return t;
360 tree
361 build_cplus_array_type (elt_type, index_type)
362 tree elt_type;
363 tree index_type;
365 register struct obstack *ambient_obstack = current_obstack;
366 register struct obstack *ambient_saveable_obstack = saveable_obstack;
367 tree t;
369 /* We need a new one. If both ELT_TYPE and INDEX_TYPE are permanent,
370 make this permanent too. */
371 if (TREE_PERMANENT (elt_type)
372 && (index_type == 0 || TREE_PERMANENT (index_type)))
374 current_obstack = &permanent_obstack;
375 saveable_obstack = &permanent_obstack;
378 t = build_array_type (elt_type, index_type);
380 /* Push these needs up so that initialization takes place
381 more easily. */
382 TYPE_NEEDS_CONSTRUCTING (t) = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (elt_type));
383 TYPE_NEEDS_DESTRUCTOR (t) = TYPE_NEEDS_DESTRUCTOR (TYPE_MAIN_VARIANT (elt_type));
384 current_obstack = ambient_obstack;
385 saveable_obstack = ambient_saveable_obstack;
386 return t;
389 /* Add OFFSET to all base types of T.
391 OFFSET, which is a type offset, is number of bytes.
393 Note that we don't have to worry about having two paths to the
394 same base type, since this type owns its association list. */
395 void
396 propagate_binfo_offsets (binfo, offset)
397 tree binfo;
398 tree offset;
400 tree binfos = BINFO_BASETYPES (binfo);
401 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
403 for (i = 0; i < n_baselinks; /* note increment is done in the loop. */)
405 tree base_binfo = TREE_VEC_ELT (binfos, i);
407 if (TREE_VIA_VIRTUAL (base_binfo))
408 i += 1;
409 else
411 int j;
412 tree base_binfos = BINFO_BASETYPES (base_binfo);
413 tree delta;
415 for (j = i+1; j < n_baselinks; j++)
416 if (! TREE_VIA_VIRTUAL (TREE_VEC_ELT (binfos, j)))
418 /* The next basetype offset must take into account the space
419 between the classes, not just the size of each class. */
420 delta = size_binop (MINUS_EXPR,
421 BINFO_OFFSET (TREE_VEC_ELT (binfos, j)),
422 BINFO_OFFSET (base_binfo));
423 break;
426 #if 0
427 if (BINFO_OFFSET_ZEROP (base_binfo))
428 BINFO_OFFSET (base_binfo) = offset;
429 else
430 BINFO_OFFSET (base_binfo)
431 = size_binop (PLUS_EXPR, BINFO_OFFSET (base_binfo), offset);
432 #else
433 BINFO_OFFSET (base_binfo) = offset;
434 #endif
435 if (base_binfos)
437 int k;
438 tree chain = NULL_TREE;
440 /* Now unshare the structure beneath BASE_BINFO. */
441 for (k = TREE_VEC_LENGTH (base_binfos)-1;
442 k >= 0; k--)
444 tree base_base_binfo = TREE_VEC_ELT (base_binfos, k);
445 if (! TREE_VIA_VIRTUAL (base_base_binfo))
446 TREE_VEC_ELT (base_binfos, k)
447 = make_binfo (BINFO_OFFSET (base_base_binfo),
448 base_base_binfo,
449 BINFO_VTABLE (base_base_binfo),
450 BINFO_VIRTUALS (base_base_binfo),
451 chain);
452 chain = TREE_VEC_ELT (base_binfos, k);
453 TREE_VIA_PUBLIC (chain) = TREE_VIA_PUBLIC (base_base_binfo);
454 TREE_VIA_PROTECTED (chain) = TREE_VIA_PROTECTED (base_base_binfo);
456 /* Now propagate the offset to the base types. */
457 propagate_binfo_offsets (base_binfo, offset);
460 /* Go to our next class that counts for offset propagation. */
461 i = j;
462 if (i < n_baselinks)
463 offset = size_binop (PLUS_EXPR, offset, delta);
468 /* Compute the actual offsets that our virtual base classes
469 will have *for this type*. This must be performed after
470 the fields are laid out, since virtual baseclasses must
471 lay down at the end of the record.
473 Returns the maximum number of virtual functions any of the virtual
474 baseclasses provide. */
476 layout_vbasetypes (rec, max)
477 tree rec;
478 int max;
480 /* Get all the virtual base types that this type uses.
481 The TREE_VALUE slot holds the virtual baseclass type. */
482 tree vbase_types = get_vbase_types (rec);
484 #ifdef STRUCTURE_SIZE_BOUNDARY
485 unsigned record_align = MAX (STRUCTURE_SIZE_BOUNDARY, TYPE_ALIGN (rec));
486 #else
487 unsigned record_align = MAX (BITS_PER_UNIT, TYPE_ALIGN (rec));
488 #endif
490 /* Record size so far is CONST_SIZE + VAR_SIZE bits,
491 where CONST_SIZE is an integer
492 and VAR_SIZE is a tree expression.
493 If VAR_SIZE is null, the size is just CONST_SIZE.
494 Naturally we try to avoid using VAR_SIZE. */
495 register unsigned const_size = 0;
496 register tree var_size = 0;
497 int nonvirtual_const_size;
498 tree nonvirtual_var_size;
500 CLASSTYPE_VBASECLASSES (rec) = vbase_types;
502 if (TREE_CODE (TYPE_SIZE (rec)) == INTEGER_CST)
503 const_size = TREE_INT_CST_LOW (TYPE_SIZE (rec));
504 else
505 var_size = TYPE_SIZE (rec);
507 nonvirtual_const_size = const_size;
508 nonvirtual_var_size = var_size;
510 while (vbase_types)
512 tree basetype = BINFO_TYPE (vbase_types);
513 tree offset;
515 if (const_size == 0)
516 offset = integer_zero_node;
517 else
518 offset = size_int ((const_size + BITS_PER_UNIT - 1) / BITS_PER_UNIT);
520 if (CLASSTYPE_VSIZE (basetype) > max)
521 max = CLASSTYPE_VSIZE (basetype);
522 BINFO_OFFSET (vbase_types) = offset;
524 if (TREE_CODE (TYPE_SIZE (basetype)) == INTEGER_CST)
525 const_size += MAX (record_align,
526 TREE_INT_CST_LOW (TYPE_SIZE (basetype))
527 - TREE_INT_CST_LOW (CLASSTYPE_VBASE_SIZE (basetype)));
528 else if (var_size == 0)
529 var_size = TYPE_SIZE (basetype);
530 else
531 var_size = size_binop (PLUS_EXPR, var_size, TYPE_SIZE (basetype));
533 vbase_types = TREE_CHAIN (vbase_types);
536 if (const_size != nonvirtual_const_size)
538 CLASSTYPE_VBASE_SIZE (rec)
539 = size_int (const_size - nonvirtual_const_size);
540 TYPE_SIZE (rec) = size_int (const_size);
543 /* Now propagate offset information throughout the lattice
544 under the vbase type. */
545 for (vbase_types = CLASSTYPE_VBASECLASSES (rec); vbase_types;
546 vbase_types = TREE_CHAIN (vbase_types))
548 tree base_binfos = BINFO_BASETYPES (vbase_types);
550 if (base_binfos)
552 tree chain = NULL_TREE;
553 int j;
554 /* Now unshare the structure beneath BASE_BINFO. */
556 for (j = TREE_VEC_LENGTH (base_binfos)-1;
557 j >= 0; j--)
559 tree base_base_binfo = TREE_VEC_ELT (base_binfos, j);
560 if (! TREE_VIA_VIRTUAL (base_base_binfo))
561 TREE_VEC_ELT (base_binfos, j)
562 = make_binfo (BINFO_OFFSET (base_base_binfo),
563 base_base_binfo,
564 BINFO_VTABLE (base_base_binfo),
565 BINFO_VIRTUALS (base_base_binfo),
566 chain);
567 chain = TREE_VEC_ELT (base_binfos, j);
568 TREE_VIA_PUBLIC (chain) = TREE_VIA_PUBLIC (base_base_binfo);
569 TREE_VIA_PROTECTED (chain) = TREE_VIA_PROTECTED (base_base_binfo);
572 propagate_binfo_offsets (vbase_types, BINFO_OFFSET (vbase_types));
576 return max;
579 /* Lay out the base types of a record type, REC.
580 Tentatively set the size and alignment of REC
581 according to the base types alone.
583 Offsets for immediate nonvirtual baseclasses are also computed here.
585 TYPE_BINFO (REC) should be NULL_TREE on entry, and this routine
586 creates a list of base_binfos in TYPE_BINFO (REC) from BINFOS.
588 Returns list of virtual base classes in a FIELD_DECL chain. */
589 tree
590 layout_basetypes (rec, binfos)
591 tree rec, binfos;
593 /* Chain to hold all the new FIELD_DECLs which point at virtual
594 base classes. */
595 tree vbase_decls = NULL_TREE;
597 #ifdef STRUCTURE_SIZE_BOUNDARY
598 unsigned record_align = MAX (STRUCTURE_SIZE_BOUNDARY, TYPE_ALIGN (rec));
599 #else
600 unsigned record_align = MAX (BITS_PER_UNIT, TYPE_ALIGN (rec));
601 #endif
603 /* Record size so far is CONST_SIZE + VAR_SIZE bits, where CONST_SIZE is
604 an integer and VAR_SIZE is a tree expression. If VAR_SIZE is null,
605 the size is just CONST_SIZE. Naturally we try to avoid using
606 VAR_SIZE. And so far, we've been sucessful. */
607 #if 0
608 register tree var_size = 0;
609 #endif
611 register unsigned const_size = 0;
612 int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
614 /* Handle basetypes almost like fields, but record their
615 offsets differently. */
617 for (i = 0; i < n_baseclasses; i++)
619 int inc, desired_align, int_vbase_size;
620 register tree base_binfo = TREE_VEC_ELT (binfos, i);
621 register tree basetype = BINFO_TYPE (base_binfo);
622 tree decl, offset;
624 if (TYPE_SIZE (basetype) == 0)
626 #if 0
627 /* This error is now reported in xref_tag, thus giving better
628 location information. */
629 error_with_aggr_type (base_binfo,
630 "base class `%s' has incomplete type");
632 TREE_VIA_PUBLIC (base_binfo) = 1;
633 TREE_VIA_PROTECTED (base_binfo) = 0;
634 TREE_VIA_VIRTUAL (base_binfo) = 0;
636 /* Should handle this better so that
638 class A;
639 class B: private A { virtual void F(); };
641 does not dump core when compiled. */
642 my_friendly_abort (121);
643 #endif
644 continue;
647 /* All basetypes are recorded in the association list of the
648 derived type. */
650 if (TREE_VIA_VIRTUAL (base_binfo))
652 int j;
653 char *name = (char *)alloca (TYPE_NAME_LENGTH (basetype)
654 + sizeof (VBASE_NAME) + 1);
656 /* The offset for a virtual base class is only used in computing
657 virtual function tables and for initializing virtual base
658 pointers. It is built once `get_vbase_types' is called. */
660 /* If this basetype can come from another vbase pointer
661 without an additional indirection, we will share
662 that pointer. If an indirection is involved, we
663 make our own pointer. */
664 for (j = 0; j < n_baseclasses; j++)
666 tree other_base_binfo = TREE_VEC_ELT (binfos, j);
667 if (! TREE_VIA_VIRTUAL (other_base_binfo)
668 && binfo_member (basetype,
669 CLASSTYPE_VBASECLASSES (BINFO_TYPE (other_base_binfo))))
670 goto got_it;
672 sprintf (name, VBASE_NAME_FORMAT, TYPE_NAME_STRING (basetype));
673 decl = build_lang_decl (FIELD_DECL, get_identifier (name),
674 build_pointer_type (basetype));
675 /* If you change any of the below, take a look at all the
676 other VFIELD_BASEs and VTABLE_BASEs in the code, and change
677 them too. */
678 DECL_ASSEMBLER_NAME (decl) = get_identifier (VTABLE_BASE);
679 DECL_VIRTUAL_P (decl) = 1;
680 DECL_FIELD_CONTEXT (decl) = rec;
681 DECL_CLASS_CONTEXT (decl) = rec;
682 DECL_FCONTEXT (decl) = basetype;
683 DECL_FIELD_SIZE (decl) = 0;
684 DECL_ALIGN (decl) = TYPE_ALIGN (ptr_type_node);
685 TREE_CHAIN (decl) = vbase_decls;
686 BINFO_VPTR_FIELD (base_binfo) = decl;
687 vbase_decls = decl;
689 if (warn_nonvdtor && TYPE_HAS_DESTRUCTOR (basetype)
690 && DECL_VINDEX (TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (basetype), 0)) == NULL_TREE)
692 warning_with_decl (TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (basetype), 0),
693 "destructor `%s' non-virtual");
694 warning ("in inheritance relationship `%s: virtual %s'",
695 TYPE_NAME_STRING (rec),
696 TYPE_NAME_STRING (basetype));
698 got_it:
699 /* The space this decl occupies has already been accounted for. */
700 continue;
703 if (const_size == 0)
704 offset = integer_zero_node;
705 else
707 /* Give each base type the alignment it wants. */
708 const_size = CEIL (const_size, TYPE_ALIGN (basetype))
709 * TYPE_ALIGN (basetype);
710 offset = size_int ((const_size + BITS_PER_UNIT - 1) / BITS_PER_UNIT);
712 #if 0
713 /* bpk: Disabled this check until someone is willing to
714 claim it as theirs and explain exactly what circumstances
715 warrant the warning. */
716 if (warn_nonvdtor && TYPE_HAS_DESTRUCTOR (basetype)
717 && DECL_VINDEX (TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (basetype), 0)) == NULL_TREE)
719 warning_with_decl (TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (basetype), 0),
720 "destructor `%s' non-virtual");
721 warning ("in inheritance relationship `%s:%s %s'",
722 TYPE_NAME_STRING (rec),
723 TREE_VIA_VIRTUAL (base_binfo) ? " virtual" : "",
724 TYPE_NAME_STRING (basetype));
726 #endif
728 BINFO_OFFSET (base_binfo) = offset;
729 if (CLASSTYPE_VSIZE (basetype))
731 BINFO_VTABLE (base_binfo) = TYPE_BINFO_VTABLE (basetype);
732 BINFO_VIRTUALS (base_binfo) = TYPE_BINFO_VIRTUALS (basetype);
734 TREE_CHAIN (base_binfo) = TYPE_BINFO (rec);
735 TYPE_BINFO (rec) = base_binfo;
737 /* Add only the amount of storage not present in
738 the virtual baseclasses. */
740 int_vbase_size = TREE_INT_CST_LOW (CLASSTYPE_VBASE_SIZE (basetype));
741 if (TREE_INT_CST_LOW (TYPE_SIZE (basetype)) > int_vbase_size)
743 inc = MAX (record_align,
744 (TREE_INT_CST_LOW (TYPE_SIZE (basetype))
745 - int_vbase_size));
747 /* Record must have at least as much alignment as any field. */
748 desired_align = TYPE_ALIGN (basetype);
749 record_align = MAX (record_align, desired_align);
751 const_size += inc;
755 if (const_size)
756 CLASSTYPE_SIZE (rec) = size_int (const_size);
757 else
758 CLASSTYPE_SIZE (rec) = integer_zero_node;
759 CLASSTYPE_ALIGN (rec) = record_align;
761 return vbase_decls;
764 /* Hashing of lists so that we don't make duplicates.
765 The entry point is `list_hash_canon'. */
767 /* Each hash table slot is a bucket containing a chain
768 of these structures. */
770 struct list_hash
772 struct list_hash *next; /* Next structure in the bucket. */
773 int hashcode; /* Hash code of this list. */
774 tree list; /* The list recorded here. */
777 /* Now here is the hash table. When recording a list, it is added
778 to the slot whose index is the hash code mod the table size.
779 Note that the hash table is used for several kinds of lists.
780 While all these live in the same table, they are completely independent,
781 and the hash code is computed differently for each of these. */
783 #define TYPE_HASH_SIZE 59
784 struct list_hash *list_hash_table[TYPE_HASH_SIZE];
786 /* Compute a hash code for a list (chain of TREE_LIST nodes
787 with goodies in the TREE_PURPOSE, TREE_VALUE, and bits of the
788 TREE_COMMON slots), by adding the hash codes of the individual entries. */
791 list_hash (list)
792 tree list;
794 register int hashcode = 0;
796 if (TREE_CHAIN (list))
797 hashcode += TYPE_HASH (TREE_CHAIN (list));
799 if (TREE_VALUE (list))
800 hashcode += TYPE_HASH (TREE_VALUE (list));
801 else
802 hashcode += 1007;
803 if (TREE_PURPOSE (list))
804 hashcode += TYPE_HASH (TREE_PURPOSE (list));
805 else
806 hashcode += 1009;
807 return hashcode;
810 /* Look in the type hash table for a type isomorphic to TYPE.
811 If one is found, return it. Otherwise return 0. */
813 tree
814 list_hash_lookup (hashcode, list)
815 int hashcode;
816 tree list;
818 register struct list_hash *h;
819 for (h = list_hash_table[hashcode % TYPE_HASH_SIZE]; h; h = h->next)
820 if (h->hashcode == hashcode
821 && TREE_VIA_VIRTUAL (h->list) == TREE_VIA_VIRTUAL (list)
822 && TREE_VIA_PUBLIC (h->list) == TREE_VIA_PUBLIC (list)
823 && TREE_VIA_PROTECTED (h->list) == TREE_VIA_PROTECTED (list)
824 && TREE_PURPOSE (h->list) == TREE_PURPOSE (list)
825 && TREE_VALUE (h->list) == TREE_VALUE (list)
826 && TREE_CHAIN (h->list) == TREE_CHAIN (list))
828 my_friendly_assert (TREE_TYPE (h->list) == TREE_TYPE (list), 299);
829 return h->list;
831 return 0;
834 /* Add an entry to the list-hash-table
835 for a list TYPE whose hash code is HASHCODE. */
837 void
838 list_hash_add (hashcode, list)
839 int hashcode;
840 tree list;
842 register struct list_hash *h;
844 h = (struct list_hash *) obstack_alloc (&class_obstack, sizeof (struct list_hash));
845 h->hashcode = hashcode;
846 h->list = list;
847 h->next = list_hash_table[hashcode % TYPE_HASH_SIZE];
848 list_hash_table[hashcode % TYPE_HASH_SIZE] = h;
851 /* Given TYPE, and HASHCODE its hash code, return the canonical
852 object for an identical list if one already exists.
853 Otherwise, return TYPE, and record it as the canonical object
854 if it is a permanent object.
856 To use this function, first create a list of the sort you want.
857 Then compute its hash code from the fields of the list that
858 make it different from other similar lists.
859 Then call this function and use the value.
860 This function frees the list you pass in if it is a duplicate. */
862 /* Set to 1 to debug without canonicalization. Never set by program. */
863 int debug_no_list_hash = 0;
865 tree
866 list_hash_canon (hashcode, list)
867 int hashcode;
868 tree list;
870 tree t1;
872 if (debug_no_list_hash)
873 return list;
875 t1 = list_hash_lookup (hashcode, list);
876 if (t1 != 0)
878 obstack_free (&class_obstack, list);
879 return t1;
882 /* If this is a new list, record it for later reuse. */
883 list_hash_add (hashcode, list);
885 return list;
888 tree
889 hash_tree_cons (via_public, via_virtual, via_protected, purpose, value, chain)
890 int via_public, via_virtual, via_protected;
891 tree purpose, value, chain;
893 struct obstack *ambient_obstack = current_obstack;
894 tree t;
895 int hashcode;
897 current_obstack = &class_obstack;
898 t = tree_cons (purpose, value, chain);
899 TREE_VIA_PUBLIC (t) = via_public;
900 TREE_VIA_PROTECTED (t) = via_protected;
901 TREE_VIA_VIRTUAL (t) = via_virtual;
902 hashcode = list_hash (t);
903 t = list_hash_canon (hashcode, t);
904 current_obstack = ambient_obstack;
905 return t;
908 /* Constructor for hashed lists. */
909 tree
910 hash_tree_chain (value, chain)
911 tree value, chain;
913 struct obstack *ambient_obstack = current_obstack;
914 tree t;
915 int hashcode;
917 current_obstack = &class_obstack;
918 t = tree_cons (NULL_TREE, value, chain);
919 hashcode = list_hash (t);
920 t = list_hash_canon (hashcode, t);
921 current_obstack = ambient_obstack;
922 return t;
925 /* Similar, but used for concatenating two lists. */
926 tree
927 hash_chainon (list1, list2)
928 tree list1, list2;
930 if (list2 == 0)
931 return list1;
932 if (list1 == 0)
933 return list2;
934 if (TREE_CHAIN (list1) == NULL_TREE)
935 return hash_tree_chain (TREE_VALUE (list1), list2);
936 return hash_tree_chain (TREE_VALUE (list1),
937 hash_chainon (TREE_CHAIN (list1), list2));
940 static tree
941 get_identifier_list (value)
942 tree value;
944 tree list = IDENTIFIER_AS_LIST (value);
945 if (list != NULL_TREE
946 && (TREE_CODE (list) != TREE_LIST
947 || TREE_VALUE (list) != value))
948 list = NULL_TREE;
949 else if (IDENTIFIER_HAS_TYPE_VALUE (value)
950 && TREE_CODE (IDENTIFIER_TYPE_VALUE (value)) == RECORD_TYPE
951 && IDENTIFIER_TYPE_VALUE (value)
952 == TYPE_MAIN_VARIANT (IDENTIFIER_TYPE_VALUE (value)))
954 tree type = IDENTIFIER_TYPE_VALUE (value);
956 if (TYPE_PTRMEMFUNC_P (type))
957 list = NULL_TREE;
958 else if (type == current_class_type)
959 /* Don't mess up the constructor name. */
960 list = tree_cons (NULL_TREE, value, NULL_TREE);
961 else
963 register tree id;
964 /* This will return the correct thing for regular types,
965 nested types, and templates. Yay! */
966 if (TYPE_NESTED_NAME (type))
967 id = TYPE_NESTED_NAME (type);
968 else
969 id = TYPE_IDENTIFIER (type);
971 if (CLASSTYPE_ID_AS_LIST (type) == NULL_TREE)
972 CLASSTYPE_ID_AS_LIST (type)
973 = perm_tree_cons (NULL_TREE, id, NULL_TREE);
974 list = CLASSTYPE_ID_AS_LIST (type);
977 return list;
980 tree
981 get_decl_list (value)
982 tree value;
984 tree list = NULL_TREE;
986 if (TREE_CODE (value) == IDENTIFIER_NODE)
987 list = get_identifier_list (value);
988 else if (TREE_CODE (value) == RECORD_TYPE
989 && TYPE_LANG_SPECIFIC (value))
990 list = CLASSTYPE_AS_LIST (value);
992 if (list != NULL_TREE)
994 my_friendly_assert (TREE_CHAIN (list) == NULL_TREE, 301);
995 return list;
998 return build_decl_list (NULL_TREE, value);
1001 /* Look in the type hash table for a type isomorphic to
1002 `build_tree_list (NULL_TREE, VALUE)'.
1003 If one is found, return it. Otherwise return 0. */
1005 tree
1006 list_hash_lookup_or_cons (value)
1007 tree value;
1009 register int hashcode = TYPE_HASH (value);
1010 register struct list_hash *h;
1011 struct obstack *ambient_obstack;
1012 tree list = NULL_TREE;
1014 if (TREE_CODE (value) == IDENTIFIER_NODE)
1015 list = get_identifier_list (value);
1016 else if (TREE_CODE (value) == TYPE_DECL
1017 && TREE_CODE (TREE_TYPE (value)) == RECORD_TYPE
1018 && TYPE_LANG_SPECIFIC (TREE_TYPE (value)))
1019 list = CLASSTYPE_ID_AS_LIST (TREE_TYPE (value));
1020 else if (TREE_CODE (value) == RECORD_TYPE
1021 && TYPE_LANG_SPECIFIC (value))
1022 list = CLASSTYPE_AS_LIST (value);
1024 if (list != NULL_TREE)
1026 my_friendly_assert (TREE_CHAIN (list) == NULL_TREE, 302);
1027 return list;
1030 if (debug_no_list_hash)
1031 return hash_tree_chain (value, NULL_TREE);
1033 for (h = list_hash_table[hashcode % TYPE_HASH_SIZE]; h; h = h->next)
1034 if (h->hashcode == hashcode
1035 && TREE_VIA_VIRTUAL (h->list) == 0
1036 && TREE_VIA_PUBLIC (h->list) == 0
1037 && TREE_VIA_PROTECTED (h->list) == 0
1038 && TREE_PURPOSE (h->list) == 0
1039 && TREE_VALUE (h->list) == value)
1041 my_friendly_assert (TREE_TYPE (h->list) == 0, 303);
1042 my_friendly_assert (TREE_CHAIN (h->list) == 0, 304);
1043 return h->list;
1046 ambient_obstack = current_obstack;
1047 current_obstack = &class_obstack;
1048 list = build_tree_list (NULL_TREE, value);
1049 list_hash_add (hashcode, list);
1050 current_obstack = ambient_obstack;
1051 return list;
1054 /* Build an association between TYPE and some parameters:
1056 OFFSET is the offset added to `this' to convert it to a pointer
1057 of type `TYPE *'
1059 BINFO is the base binfo to use, if we are deriving from one. This
1060 is necessary, as we want specialized parent binfos from base
1061 classes, so that the VTABLE_NAMEs of bases are for the most derived
1062 type, instead of of the simple type.
1064 VTABLE is the virtual function table with which to initialize
1065 sub-objects of type TYPE.
1067 VIRTUALS are the virtual functions sitting in VTABLE.
1069 CHAIN are more associations we must retain. */
1071 tree
1072 make_binfo (offset, binfo, vtable, virtuals, chain)
1073 tree offset, binfo;
1074 tree vtable, virtuals;
1075 tree chain;
1077 tree new_binfo = make_tree_vec (6);
1078 tree type;
1080 if (TREE_CODE (binfo) == TREE_VEC)
1081 type = BINFO_TYPE (binfo);
1082 else
1084 type = binfo;
1085 binfo = TYPE_BINFO (binfo);
1088 TREE_CHAIN (new_binfo) = chain;
1089 if (chain)
1090 TREE_USED (new_binfo) = TREE_USED (chain);
1092 TREE_TYPE (new_binfo) = TYPE_MAIN_VARIANT (type);
1093 BINFO_OFFSET (new_binfo) = offset;
1094 BINFO_VTABLE (new_binfo) = vtable;
1095 BINFO_VIRTUALS (new_binfo) = virtuals;
1096 BINFO_VPTR_FIELD (new_binfo) = NULL_TREE;
1098 if (binfo && BINFO_BASETYPES (binfo) != NULL_TREE)
1099 BINFO_BASETYPES (new_binfo) = copy_node (BINFO_BASETYPES (binfo));
1100 return new_binfo;
1103 tree
1104 copy_binfo (list)
1105 tree list;
1107 tree binfo = copy_list (list);
1108 tree rval = binfo;
1109 while (binfo)
1111 TREE_USED (binfo) = 0;
1112 if (BINFO_BASETYPES (binfo))
1113 BINFO_BASETYPES (binfo) = copy_node (BINFO_BASETYPES (binfo));
1114 binfo = TREE_CHAIN (binfo);
1116 return rval;
1119 /* Return the binfo value for ELEM in TYPE. */
1121 tree
1122 binfo_value (elem, type)
1123 tree elem;
1124 tree type;
1126 if (get_base_distance (elem, type, 0, (tree *)0) == -2)
1127 compiler_error ("base class `%s' ambiguous in binfo_value",
1128 TYPE_NAME_STRING (elem));
1129 if (elem == type)
1130 return TYPE_BINFO (type);
1131 if (TREE_CODE (elem) == RECORD_TYPE && TYPE_BINFO (elem) == type)
1132 return type;
1133 return get_binfo (elem, type, 0);
1136 tree
1137 reverse_path (path)
1138 tree path;
1140 register tree prev = 0, tmp, next;
1141 for (tmp = path; tmp; tmp = next)
1143 next = BINFO_INHERITANCE_CHAIN (tmp);
1144 BINFO_INHERITANCE_CHAIN (tmp) = prev;
1145 prev = tmp;
1147 return prev;
1150 tree
1151 virtual_member (elem, list)
1152 tree elem;
1153 tree list;
1155 tree t;
1156 tree rval, nval;
1158 for (t = list; t; t = TREE_CHAIN (t))
1159 if (elem == BINFO_TYPE (t))
1160 return t;
1161 rval = 0;
1162 for (t = list; t; t = TREE_CHAIN (t))
1164 tree binfos = BINFO_BASETYPES (t);
1165 int i;
1167 if (binfos != NULL_TREE)
1168 for (i = TREE_VEC_LENGTH (binfos)-1; i >= 0; i--)
1170 nval = binfo_value (elem, BINFO_TYPE (TREE_VEC_ELT (binfos, i)));
1171 if (nval)
1173 if (rval && BINFO_OFFSET (nval) != BINFO_OFFSET (rval))
1174 my_friendly_abort (104);
1175 rval = nval;
1179 return rval;
1182 /* Return the offset (as an INTEGER_CST) for ELEM in LIST.
1183 INITIAL_OFFSET is the value to add to the offset that ELEM's
1184 binfo entry in LIST provides.
1186 Returns NULL if ELEM does not have an binfo value in LIST. */
1188 tree
1189 virtual_offset (elem, list, initial_offset)
1190 tree elem;
1191 tree list;
1192 tree initial_offset;
1194 tree vb, offset;
1195 tree rval, nval;
1197 for (vb = list; vb; vb = TREE_CHAIN (vb))
1198 if (elem == BINFO_TYPE (vb))
1199 return size_binop (PLUS_EXPR, initial_offset, BINFO_OFFSET (vb));
1200 rval = 0;
1201 for (vb = list; vb; vb = TREE_CHAIN (vb))
1203 tree binfos = BINFO_BASETYPES (vb);
1204 int i;
1206 if (binfos == NULL_TREE)
1207 continue;
1209 for (i = TREE_VEC_LENGTH (binfos)-1; i >= 0; i--)
1211 nval = binfo_value (elem, BINFO_TYPE (TREE_VEC_ELT (binfos, i)));
1212 if (nval)
1214 if (rval && BINFO_OFFSET (nval) != BINFO_OFFSET (rval))
1215 my_friendly_abort (105);
1216 offset = BINFO_OFFSET (vb);
1217 rval = nval;
1221 if (rval == NULL_TREE)
1222 return rval;
1223 return size_binop (PLUS_EXPR, offset, BINFO_OFFSET (rval));
1226 void
1227 debug_binfo (elem)
1228 tree elem;
1230 int i;
1231 tree virtuals;
1233 fprintf (stderr, "type \"%s\"; offset = %d\n",
1234 TYPE_NAME_STRING (BINFO_TYPE (elem)),
1235 TREE_INT_CST_LOW (BINFO_OFFSET (elem)));
1236 fprintf (stderr, "vtable type:\n");
1237 debug_tree (BINFO_TYPE (elem));
1238 if (BINFO_VTABLE (elem))
1239 fprintf (stderr, "vtable decl \"%s\"\n", IDENTIFIER_POINTER (DECL_NAME (BINFO_VTABLE (elem))));
1240 else
1241 fprintf (stderr, "no vtable decl yet\n");
1242 fprintf (stderr, "virtuals:\n");
1243 virtuals = BINFO_VIRTUALS (elem);
1244 if (virtuals != 0)
1246 virtuals = TREE_CHAIN (virtuals);
1247 if (flag_dossier)
1248 virtuals = TREE_CHAIN (virtuals);
1250 i = 1;
1251 while (virtuals)
1253 tree fndecl = TREE_OPERAND (FNADDR_FROM_VTABLE_ENTRY (TREE_VALUE (virtuals)), 0);
1254 fprintf (stderr, "%s [%d =? %d]\n",
1255 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (fndecl)),
1256 i, TREE_INT_CST_LOW (DECL_VINDEX (fndecl)));
1257 virtuals = TREE_CHAIN (virtuals);
1258 i += 1;
1262 /* Return the length of a chain of nodes chained through DECL_CHAIN.
1263 We expect a null pointer to mark the end of the chain.
1264 This is the Lisp primitive `length'. */
1267 decl_list_length (t)
1268 tree t;
1270 register tree tail;
1271 register int len = 0;
1273 my_friendly_assert (TREE_CODE (t) == FUNCTION_DECL
1274 || TREE_CODE (t) == TEMPLATE_DECL, 300);
1275 for (tail = t; tail; tail = DECL_CHAIN (tail))
1276 len++;
1278 return len;
1282 count_functions (t)
1283 tree t;
1285 if (TREE_CODE (t) == FUNCTION_DECL)
1286 return 1;
1288 return decl_list_length (TREE_VALUE (t));
1291 /* Like value_member, but for DECL_CHAINs. */
1292 tree
1293 decl_value_member (elem, list)
1294 tree elem, list;
1296 while (list)
1298 if (elem == list)
1299 return list;
1300 list = DECL_CHAIN (list);
1302 return NULL_TREE;
1306 is_overloaded_fn (x)
1307 tree x;
1309 if (TREE_CODE (x) == FUNCTION_DECL)
1310 return 1;
1312 if (TREE_CODE (x) == TREE_LIST
1313 && (TREE_CODE (TREE_VALUE (x)) == FUNCTION_DECL
1314 || TREE_CODE (TREE_VALUE (x)) == TEMPLATE_DECL))
1315 return 1;
1317 return 0;
1321 really_overloaded_fn (x)
1322 tree x;
1324 if (TREE_CODE (x) == TREE_LIST
1325 && (TREE_CODE (TREE_VALUE (x)) == FUNCTION_DECL
1326 || TREE_CODE (TREE_VALUE (x)) == TEMPLATE_DECL))
1327 return 1;
1329 return 0;
1332 tree
1333 get_first_fn (from)
1334 tree from;
1336 if (TREE_CODE (from) == FUNCTION_DECL)
1337 return from;
1339 my_friendly_assert (TREE_CODE (from) == TREE_LIST, 9);
1341 return TREE_VALUE (from);
1344 tree
1345 fnaddr_from_vtable_entry (entry)
1346 tree entry;
1348 if (flag_vtable_thunks)
1350 tree func = entry;
1351 if (TREE_CODE (func) == ADDR_EXPR)
1352 func = TREE_OPERAND (func, 0);
1353 if (TREE_CODE (func) == THUNK_DECL)
1354 return DECL_INITIAL (func);
1355 else
1356 return entry;
1358 else
1359 return TREE_VALUE (TREE_CHAIN (TREE_CHAIN (CONSTRUCTOR_ELTS (entry))));
1362 void
1363 set_fnaddr_from_vtable_entry (entry, value)
1364 tree entry, value;
1366 if (flag_vtable_thunks)
1367 abort ();
1368 else
1369 TREE_VALUE (TREE_CHAIN (TREE_CHAIN (CONSTRUCTOR_ELTS (entry)))) = value;
1372 tree
1373 function_arg_chain (t)
1374 tree t;
1376 return TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (t)));
1380 promotes_to_aggr_type (t, code)
1381 tree t;
1382 enum tree_code code;
1384 if (TREE_CODE (t) == code)
1385 t = TREE_TYPE (t);
1386 return IS_AGGR_TYPE (t);
1390 is_aggr_type_2 (t1, t2)
1391 tree t1, t2;
1393 if (TREE_CODE (t1) != TREE_CODE (t2))
1394 return 0;
1395 return IS_AGGR_TYPE (t1) && IS_AGGR_TYPE (t2);
1398 /* Give message using types TYPE1 and TYPE2 as arguments.
1399 PFN is the function which will print the message;
1400 S is the format string for PFN to use. */
1401 void
1402 message_2_types (pfn, s, type1, type2)
1403 void (*pfn) ();
1404 char *s;
1405 tree type1, type2;
1407 tree name1 = TYPE_NAME (type1);
1408 tree name2 = TYPE_NAME (type2);
1409 if (TREE_CODE (name1) == TYPE_DECL)
1410 name1 = DECL_NAME (name1);
1411 if (TREE_CODE (name2) == TYPE_DECL)
1412 name2 = DECL_NAME (name2);
1413 (*pfn) (s, IDENTIFIER_POINTER (name1), IDENTIFIER_POINTER (name2));
1416 #define PRINT_RING_SIZE 4
1418 char *
1419 lang_printable_name (decl)
1420 tree decl;
1422 static tree decl_ring[PRINT_RING_SIZE];
1423 static char *print_ring[PRINT_RING_SIZE];
1424 static int ring_counter;
1425 int i;
1427 /* Only cache functions. */
1428 if (TREE_CODE (decl) != FUNCTION_DECL
1429 || DECL_LANG_SPECIFIC (decl) == 0)
1430 return decl_as_string (decl, 1);
1432 /* See if this print name is lying around. */
1433 for (i = 0; i < PRINT_RING_SIZE; i++)
1434 if (decl_ring[i] == decl)
1435 /* yes, so return it. */
1436 return print_ring[i];
1438 if (++ring_counter == PRINT_RING_SIZE)
1439 ring_counter = 0;
1441 if (current_function_decl != NULL_TREE)
1443 if (decl_ring[ring_counter] == current_function_decl)
1444 ring_counter += 1;
1445 if (ring_counter == PRINT_RING_SIZE)
1446 ring_counter = 0;
1447 if (decl_ring[ring_counter] == current_function_decl)
1448 my_friendly_abort (106);
1451 if (print_ring[ring_counter])
1452 free (print_ring[ring_counter]);
1455 int print_ret_type_p
1456 = (!DECL_CONSTRUCTOR_P (decl)
1457 && !DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (decl)));
1459 char *name = (char *)decl_as_string (decl, print_ret_type_p);
1460 print_ring[ring_counter] = (char *)malloc (strlen (name) + 1);
1461 strcpy (print_ring[ring_counter], name);
1462 decl_ring[ring_counter] = decl;
1464 return print_ring[ring_counter];
1467 /* Comparison function for sorting identifiers in RAISES lists.
1468 Note that because IDENTIFIER_NODEs are unique, we can sort
1469 them by address, saving an indirection. */
1470 static int
1471 id_cmp (p1, p2)
1472 tree *p1, *p2;
1474 return (HOST_WIDE_INT)TREE_VALUE (*p1) - (HOST_WIDE_INT)TREE_VALUE (*p2);
1477 /* Build the FUNCTION_TYPE or METHOD_TYPE which may raise exceptions
1478 listed in RAISES. */
1479 tree
1480 build_exception_variant (ctype, type, raises)
1481 tree ctype, type;
1482 tree raises;
1484 int i;
1485 tree v = TYPE_MAIN_VARIANT (type);
1486 tree t, t2, cname;
1487 tree *a = (tree *)alloca ((list_length (raises)+1) * sizeof (tree));
1488 int constp = TYPE_READONLY (type);
1489 int volatilep = TYPE_VOLATILE (type);
1491 for (v = TYPE_NEXT_VARIANT (v); v; v = TYPE_NEXT_VARIANT (v))
1493 if (TYPE_READONLY (v) != constp
1494 || TYPE_VOLATILE (v) != volatilep)
1495 continue;
1497 t = raises;
1498 t2 = TYPE_RAISES_EXCEPTIONS (v);
1499 while (t && t2)
1501 if (TREE_TYPE (t) == TREE_TYPE (t2))
1503 t = TREE_CHAIN (t);
1504 t2 = TREE_CHAIN (t2);
1506 else break;
1508 if (t || t2)
1509 continue;
1510 /* List of exceptions raised matches previously found list.
1512 @@ Nice to free up storage used in consing up the
1513 @@ list of exceptions raised. */
1514 return v;
1517 /* Need to build a new variant. */
1518 v = copy_node (type);
1519 TYPE_NEXT_VARIANT (v) = TYPE_NEXT_VARIANT (type);
1520 TYPE_NEXT_VARIANT (type) = v;
1521 if (raises && ! TREE_PERMANENT (raises))
1523 push_obstacks_nochange ();
1524 end_temporary_allocation ();
1525 raises = copy_list (raises);
1526 pop_obstacks ();
1528 TYPE_RAISES_EXCEPTIONS (v) = raises;
1529 return v;
1532 /* Subroutine of copy_to_permanent
1534 Assuming T is a node build bottom-up, make it all exist on
1535 permanent obstack, if it is not permanent already. */
1536 static tree
1537 make_deep_copy (t)
1538 tree t;
1540 enum tree_code code;
1542 if (t == NULL_TREE || TREE_PERMANENT (t))
1543 return t;
1545 switch (code = TREE_CODE (t))
1547 case ERROR_MARK:
1548 return error_mark_node;
1550 case VAR_DECL:
1551 case FUNCTION_DECL:
1552 case CONST_DECL:
1553 break;
1555 case PARM_DECL:
1557 tree chain = TREE_CHAIN (t);
1558 t = copy_node (t);
1559 TREE_CHAIN (t) = make_deep_copy (chain);
1560 TREE_TYPE (t) = make_deep_copy (TREE_TYPE (t));
1561 DECL_INITIAL (t) = make_deep_copy (DECL_INITIAL (t));
1562 DECL_SIZE (t) = make_deep_copy (DECL_SIZE (t));
1563 return t;
1566 case TREE_LIST:
1568 tree chain = TREE_CHAIN (t);
1569 t = copy_node (t);
1570 TREE_PURPOSE (t) = make_deep_copy (TREE_PURPOSE (t));
1571 TREE_VALUE (t) = make_deep_copy (TREE_VALUE (t));
1572 TREE_CHAIN (t) = make_deep_copy (chain);
1573 return t;
1576 case TREE_VEC:
1578 int len = TREE_VEC_LENGTH (t);
1580 t = copy_node (t);
1581 while (len--)
1582 TREE_VEC_ELT (t, len) = make_deep_copy (TREE_VEC_ELT (t, len));
1583 return t;
1586 case INTEGER_CST:
1587 case REAL_CST:
1588 case STRING_CST:
1589 return copy_node (t);
1591 case COND_EXPR:
1592 case TARGET_EXPR:
1593 case NEW_EXPR:
1594 t = copy_node (t);
1595 TREE_OPERAND (t, 0) = make_deep_copy (TREE_OPERAND (t, 0));
1596 TREE_OPERAND (t, 1) = make_deep_copy (TREE_OPERAND (t, 1));
1597 TREE_OPERAND (t, 2) = make_deep_copy (TREE_OPERAND (t, 2));
1598 return t;
1600 case SAVE_EXPR:
1601 t = copy_node (t);
1602 TREE_OPERAND (t, 0) = make_deep_copy (TREE_OPERAND (t, 0));
1603 return t;
1605 case MODIFY_EXPR:
1606 case PLUS_EXPR:
1607 case MINUS_EXPR:
1608 case MULT_EXPR:
1609 case TRUNC_DIV_EXPR:
1610 case TRUNC_MOD_EXPR:
1611 case MIN_EXPR:
1612 case MAX_EXPR:
1613 case LSHIFT_EXPR:
1614 case RSHIFT_EXPR:
1615 case BIT_IOR_EXPR:
1616 case BIT_XOR_EXPR:
1617 case BIT_AND_EXPR:
1618 case BIT_ANDTC_EXPR:
1619 case TRUTH_ANDIF_EXPR:
1620 case TRUTH_ORIF_EXPR:
1621 case LT_EXPR:
1622 case LE_EXPR:
1623 case GT_EXPR:
1624 case GE_EXPR:
1625 case EQ_EXPR:
1626 case NE_EXPR:
1627 case CEIL_DIV_EXPR:
1628 case FLOOR_DIV_EXPR:
1629 case ROUND_DIV_EXPR:
1630 case CEIL_MOD_EXPR:
1631 case FLOOR_MOD_EXPR:
1632 case ROUND_MOD_EXPR:
1633 case COMPOUND_EXPR:
1634 case PREDECREMENT_EXPR:
1635 case PREINCREMENT_EXPR:
1636 case POSTDECREMENT_EXPR:
1637 case POSTINCREMENT_EXPR:
1638 case CALL_EXPR:
1639 t = copy_node (t);
1640 TREE_OPERAND (t, 0) = make_deep_copy (TREE_OPERAND (t, 0));
1641 TREE_OPERAND (t, 1) = make_deep_copy (TREE_OPERAND (t, 1));
1642 return t;
1644 case CONVERT_EXPR:
1645 case ADDR_EXPR:
1646 case INDIRECT_REF:
1647 case NEGATE_EXPR:
1648 case BIT_NOT_EXPR:
1649 case TRUTH_NOT_EXPR:
1650 case NOP_EXPR:
1651 case COMPONENT_REF:
1652 t = copy_node (t);
1653 TREE_OPERAND (t, 0) = make_deep_copy (TREE_OPERAND (t, 0));
1654 return t;
1656 /* This list is incomplete, but should suffice for now.
1657 It is very important that `sorry' does not call
1658 `report_error_function'. That could cause an infinite loop. */
1659 default:
1660 sorry ("initializer contains unrecognized tree code");
1661 return error_mark_node;
1664 my_friendly_abort (107);
1665 /* NOTREACHED */
1666 return NULL_TREE;
1669 /* Assuming T is a node built bottom-up, make it all exist on
1670 permanent obstack, if it is not permanent already. */
1671 tree
1672 copy_to_permanent (t)
1673 tree t;
1675 register struct obstack *ambient_obstack = current_obstack;
1676 register struct obstack *ambient_saveable_obstack = saveable_obstack;
1678 if (t == NULL_TREE || TREE_PERMANENT (t))
1679 return t;
1681 saveable_obstack = &permanent_obstack;
1682 current_obstack = saveable_obstack;
1684 t = make_deep_copy (t);
1686 current_obstack = ambient_obstack;
1687 saveable_obstack = ambient_saveable_obstack;
1689 return t;
1692 void
1693 print_lang_statistics ()
1695 extern struct obstack maybepermanent_obstack;
1696 print_obstack_statistics ("class_obstack", &class_obstack);
1697 print_obstack_statistics ("permanent_obstack", &permanent_obstack);
1698 print_obstack_statistics ("maybepermanent_obstack", &maybepermanent_obstack);
1699 print_search_statistics ();
1700 print_class_statistics ();
1703 /* This is used by the `assert' macro. It is provided in libgcc.a,
1704 which `cc' doesn't know how to link. Note that the C++ front-end
1705 no longer actually uses the `assert' macro (instead, it calls
1706 my_friendly_assert). But all of the back-end files still need this. */
1707 void
1708 __eprintf (string, expression, line, filename)
1709 #ifdef __STDC__
1710 const char *string;
1711 const char *expression;
1712 unsigned line;
1713 const char *filename;
1714 #else
1715 char *string;
1716 char *expression;
1717 unsigned line;
1718 char *filename;
1719 #endif
1721 fprintf (stderr, string, expression, line, filename);
1722 fflush (stderr);
1723 abort ();
1726 /* Return, as an INTEGER_CST node, the number of elements for
1727 TYPE (which is an ARRAY_TYPE). This counts only elements of the top array. */
1729 tree
1730 array_type_nelts_top (type)
1731 tree type;
1733 return fold (build (PLUS_EXPR, integer_type_node,
1734 array_type_nelts (type),
1735 integer_one_node));
1738 /* Return, as an INTEGER_CST node, the number of elements for
1739 TYPE (which is an ARRAY_TYPE). This one is a recursive count of all
1740 ARRAY_TYPEs that are clumped together. */
1742 tree
1743 array_type_nelts_total (type)
1744 tree type;
1746 tree sz = array_type_nelts_top (type);
1747 type = TREE_TYPE (type);
1748 while (TREE_CODE (type) == ARRAY_TYPE)
1750 tree n = array_type_nelts_top (type);
1751 sz = fold (build (MULT_EXPR, integer_type_node, sz, n));
1752 type = TREE_TYPE (type);
1754 return sz;