30th Cygnus<->FSF merge.
[gcc.git] / gcc / cp / tree.c
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)
4
5 This file is part of GNU CC.
6
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.
11
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.
16
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. */
20
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"
27
28 #define CEIL(x,y) (((x) + (y) - 1) / (y))
29
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. */
33
34 int
35 lvalue_p (ref)
36 tree ref;
37 {
38 register enum tree_code code = TREE_CODE (ref);
39
40 if (language_lvalue_valid (ref))
41 switch (code)
42 {
43 /* preincrements and predecrements are valid lvals, provided
44 what they refer to are valid lvals. */
45 case PREINCREMENT_EXPR:
46 case PREDECREMENT_EXPR:
47 case COMPONENT_REF:
48 case SAVE_EXPR:
49 return lvalue_p (TREE_OPERAND (ref, 0));
50
51 case STRING_CST:
52 return 1;
53
54 case VAR_DECL:
55 if (TREE_READONLY (ref) && ! TREE_STATIC (ref)
56 && DECL_LANG_SPECIFIC (ref)
57 && DECL_IN_AGGR_P (ref))
58 return 0;
59 case INDIRECT_REF:
60 case ARRAY_REF:
61 case PARM_DECL:
62 case RESULT_DECL:
63 case ERROR_MARK:
64 if (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
65 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE)
66 return 1;
67 break;
68
69 case TARGET_EXPR:
70 case WITH_CLEANUP_EXPR:
71 return 1;
72
73 case CALL_EXPR:
74 if (TREE_CODE (TREE_TYPE (ref)) == REFERENCE_TYPE
75 /* unary_complex_lvalue knows how to deal with this case. */
76 || TREE_ADDRESSABLE (TREE_TYPE (ref)))
77 return 1;
78 break;
79
80 /* A currently unresolved scope ref. */
81 case SCOPE_REF:
82 my_friendly_abort (103);
83 case OFFSET_REF:
84 if (TREE_CODE (TREE_OPERAND (ref, 1)) == FUNCTION_DECL)
85 return 1;
86 return lvalue_p (TREE_OPERAND (ref, 0))
87 && lvalue_p (TREE_OPERAND (ref, 1));
88 break;
89
90 case ADDR_EXPR:
91 /* ANSI C++ June 5 1992 WP 5.4.14. The result of a cast to a
92 reference is an lvalue. */
93 if (TREE_CODE (TREE_TYPE (ref)) == REFERENCE_TYPE)
94 return 1;
95 break;
96
97 case COND_EXPR:
98 return (lvalue_p (TREE_OPERAND (ref, 1))
99 && lvalue_p (TREE_OPERAND (ref, 2)));
100 }
101 return 0;
102 }
103
104 /* Return nonzero if REF is an lvalue valid for this language;
105 otherwise, print an error message and return zero. */
106
107 int
108 lvalue_or_else (ref, string)
109 tree ref;
110 char *string;
111 {
112 int win = lvalue_p (ref);
113 if (! win)
114 error ("invalid lvalue in %s", string);
115 return win;
116 }
117
118 /* INIT is a CALL_EXPR which needs info about its target.
119 TYPE is the type that this initialization should appear to have.
120
121 Build an encapsulation of the initialization to perform
122 and return it so that it can be processed by language-independent
123 and language-specific expression expanders.
124
125 If WITH_CLEANUP_P is nonzero, we build a cleanup for this expression.
126 Otherwise, cleanups are not built here. For example, when building
127 an initialization for a stack slot, since the called function handles
128 the cleanup, we would not want to do it here. */
129 tree
130 build_cplus_new (type, init, with_cleanup_p)
131 tree type;
132 tree init;
133 int with_cleanup_p;
134 {
135 tree slot = build (VAR_DECL, type);
136 tree rval = build (NEW_EXPR, type,
137 TREE_OPERAND (init, 0), TREE_OPERAND (init, 1), slot);
138 TREE_SIDE_EFFECTS (rval) = 1;
139 TREE_ADDRESSABLE (rval) = 1;
140 rval = build (TARGET_EXPR, type, slot, rval, 0);
141 TREE_SIDE_EFFECTS (rval) = 1;
142 TREE_ADDRESSABLE (rval) = 1;
143
144 if (with_cleanup_p && TYPE_NEEDS_DESTRUCTOR (type))
145 {
146 TREE_OPERAND (rval, 2) = error_mark_node;
147 rval = build (WITH_CLEANUP_EXPR, type, rval, 0,
148 build_delete (TYPE_POINTER_TO (type),
149 build_unary_op (ADDR_EXPR, slot, 0),
150 integer_two_node,
151 LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 0));
152 TREE_SIDE_EFFECTS (rval) = 1;
153 TREE_ADDRESSABLE (rval) = 1;
154 }
155 return rval;
156 }
157
158 /* Recursively search EXP for CALL_EXPRs that need cleanups and replace
159 these CALL_EXPRs with tree nodes that will perform the cleanups. */
160
161 tree
162 break_out_cleanups (exp)
163 tree exp;
164 {
165 tree tmp = exp;
166
167 if (TREE_CODE (tmp) == CALL_EXPR
168 && TYPE_NEEDS_DESTRUCTOR (TREE_TYPE (tmp)))
169 return build_cplus_new (TREE_TYPE (tmp), tmp, 1);
170
171 while (TREE_CODE (tmp) == NOP_EXPR
172 || TREE_CODE (tmp) == CONVERT_EXPR
173 || TREE_CODE (tmp) == NON_LVALUE_EXPR)
174 {
175 if (TREE_CODE (TREE_OPERAND (tmp, 0)) == CALL_EXPR
176 && TYPE_NEEDS_DESTRUCTOR (TREE_TYPE (TREE_OPERAND (tmp, 0))))
177 {
178 TREE_OPERAND (tmp, 0)
179 = build_cplus_new (TREE_TYPE (TREE_OPERAND (tmp, 0)),
180 TREE_OPERAND (tmp, 0), 1);
181 break;
182 }
183 else
184 tmp = TREE_OPERAND (tmp, 0);
185 }
186 return exp;
187 }
188
189 /* Recursively perform a preorder search EXP for CALL_EXPRs, making
190 copies where they are found. Returns a deep copy all nodes transitively
191 containing CALL_EXPRs. */
192
193 tree
194 break_out_calls (exp)
195 tree exp;
196 {
197 register tree t1, t2;
198 register enum tree_code code;
199 register int changed = 0;
200 register int i;
201
202 if (exp == NULL_TREE)
203 return exp;
204
205 code = TREE_CODE (exp);
206
207 if (code == CALL_EXPR)
208 return copy_node (exp);
209
210 /* Don't try and defeat a save_expr, as it should only be done once. */
211 if (code == SAVE_EXPR)
212 return exp;
213
214 switch (TREE_CODE_CLASS (code))
215 {
216 default:
217 abort ();
218
219 case 'c': /* a constant */
220 case 't': /* a type node */
221 case 'x': /* something random, like an identifier or an ERROR_MARK. */
222 return exp;
223
224 case 'd': /* A decl node */
225 t1 = break_out_calls (DECL_INITIAL (exp));
226 if (t1 != DECL_INITIAL (exp))
227 {
228 exp = copy_node (exp);
229 DECL_INITIAL (exp) = t1;
230 }
231 return exp;
232
233 case 'b': /* A block node */
234 {
235 /* Don't know how to handle these correctly yet. Must do a
236 break_out_calls on all DECL_INITIAL values for local variables,
237 and also break_out_calls on all sub-blocks and sub-statements. */
238 abort ();
239 }
240 return exp;
241
242 case 'e': /* an expression */
243 case 'r': /* a reference */
244 case 's': /* an expression with side effects */
245 for (i = tree_code_length[(int) code] - 1; i >= 0; i--)
246 {
247 t1 = break_out_calls (TREE_OPERAND (exp, i));
248 if (t1 != TREE_OPERAND (exp, i))
249 {
250 exp = copy_node (exp);
251 TREE_OPERAND (exp, i) = t1;
252 }
253 }
254 return exp;
255
256 case '<': /* a comparison expression */
257 case '2': /* a binary arithmetic expression */
258 t2 = break_out_calls (TREE_OPERAND (exp, 1));
259 if (t2 != TREE_OPERAND (exp, 1))
260 changed = 1;
261 case '1': /* a unary arithmetic expression */
262 t1 = break_out_calls (TREE_OPERAND (exp, 0));
263 if (t1 != TREE_OPERAND (exp, 0))
264 changed = 1;
265 if (changed)
266 {
267 if (tree_code_length[(int) code] == 1)
268 return build1 (code, TREE_TYPE (exp), t1);
269 else
270 return build (code, TREE_TYPE (exp), t1, t2);
271 }
272 return exp;
273 }
274
275 }
276 \f
277 extern struct obstack *current_obstack;
278 extern struct obstack permanent_obstack, class_obstack;
279 extern struct obstack *saveable_obstack;
280
281 /* Here is how primitive or already-canonicalized types' hash
282 codes are made. MUST BE CONSISTENT WITH tree.c !!! */
283 #define TYPE_HASH(TYPE) ((HOST_WIDE_INT) (TYPE) & 0777777)
284
285 /* Construct, lay out and return the type of methods belonging to class
286 BASETYPE and whose arguments are described by ARGTYPES and whose values
287 are described by RETTYPE. If each type exists already, reuse it. */
288 tree
289 build_cplus_method_type (basetype, rettype, argtypes)
290 tree basetype, rettype, argtypes;
291 {
292 register tree t;
293 tree ptype;
294 int hashcode;
295
296 /* Make a node of the sort we want. */
297 t = make_node (METHOD_TYPE);
298
299 TYPE_METHOD_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
300 TREE_TYPE (t) = rettype;
301 if (IS_SIGNATURE (basetype))
302 ptype = build_signature_pointer_type (TYPE_MAIN_VARIANT (basetype),
303 TYPE_READONLY (basetype),
304 TYPE_VOLATILE (basetype));
305 else
306 {
307 ptype = build_pointer_type (basetype);
308 #if 0
309 /* it is wrong to flag the object the pointer points to as readonly
310 when flag_this_is_variable is 0. */
311 ptype = build_type_variant (ptype, flag_this_is_variable <= 0, 0);
312 #else
313 ptype = build_type_variant (ptype, 0, 0);
314 #endif
315 }
316 /* The actual arglist for this function includes a "hidden" argument
317 which is "this". Put it into the list of argument types. */
318
319 argtypes = tree_cons (NULL_TREE, ptype, argtypes);
320 TYPE_ARG_TYPES (t) = argtypes;
321 TREE_SIDE_EFFECTS (argtypes) = 1; /* Mark first argtype as "artificial". */
322
323 /* If we already have such a type, use the old one and free this one.
324 Note that it also frees up the above cons cell if found. */
325 hashcode = TYPE_HASH (basetype) + TYPE_HASH (rettype) + type_hash_list (argtypes);
326 t = type_hash_canon (hashcode, t);
327
328 if (TYPE_SIZE (t) == 0)
329 layout_type (t);
330
331 return t;
332 }
333
334 tree
335 build_cplus_staticfn_type (basetype, rettype, argtypes)
336 tree basetype, rettype, argtypes;
337 {
338 register tree t;
339 int hashcode;
340
341 /* Make a node of the sort we want. */
342 t = make_node (FUNCTION_TYPE);
343
344 TYPE_METHOD_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
345 TREE_TYPE (t) = rettype;
346
347 /* The actual arglist for this function includes a "hidden" argument
348 which is "this". Put it into the list of argument types. */
349
350 TYPE_ARG_TYPES (t) = argtypes;
351
352 /* If we already have such a type, use the old one and free this one.
353 Note that it also frees up the above cons cell if found. */
354 hashcode = TYPE_HASH (basetype) + TYPE_HASH (rettype) + type_hash_list (argtypes);
355 t = type_hash_canon (hashcode, t);
356
357 if (TYPE_SIZE (t) == 0)
358 layout_type (t);
359
360 return t;
361 }
362
363 tree
364 build_cplus_array_type (elt_type, index_type)
365 tree elt_type;
366 tree index_type;
367 {
368 register struct obstack *ambient_obstack = current_obstack;
369 register struct obstack *ambient_saveable_obstack = saveable_obstack;
370 tree t;
371
372 /* We need a new one. If both ELT_TYPE and INDEX_TYPE are permanent,
373 make this permanent too. */
374 if (TREE_PERMANENT (elt_type)
375 && (index_type == 0 || TREE_PERMANENT (index_type)))
376 {
377 current_obstack = &permanent_obstack;
378 saveable_obstack = &permanent_obstack;
379 }
380
381 t = build_array_type (elt_type, index_type);
382
383 /* Push these needs up so that initialization takes place
384 more easily. */
385 TYPE_NEEDS_CONSTRUCTING (t) = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (elt_type));
386 TYPE_NEEDS_DESTRUCTOR (t) = TYPE_NEEDS_DESTRUCTOR (TYPE_MAIN_VARIANT (elt_type));
387 current_obstack = ambient_obstack;
388 saveable_obstack = ambient_saveable_obstack;
389 return t;
390 }
391 \f
392 /* Add OFFSET to all base types of T.
393
394 OFFSET, which is a type offset, is number of bytes.
395
396 Note that we don't have to worry about having two paths to the
397 same base type, since this type owns its association list. */
398 void
399 propagate_binfo_offsets (binfo, offset)
400 tree binfo;
401 tree offset;
402 {
403 tree binfos = BINFO_BASETYPES (binfo);
404 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
405
406 for (i = 0; i < n_baselinks; /* note increment is done in the loop. */)
407 {
408 tree base_binfo = TREE_VEC_ELT (binfos, i);
409
410 if (TREE_VIA_VIRTUAL (base_binfo))
411 i += 1;
412 else
413 {
414 int j;
415 tree base_binfos = BINFO_BASETYPES (base_binfo);
416 tree delta;
417
418 for (j = i+1; j < n_baselinks; j++)
419 if (! TREE_VIA_VIRTUAL (TREE_VEC_ELT (binfos, j)))
420 {
421 /* The next basetype offset must take into account the space
422 between the classes, not just the size of each class. */
423 delta = size_binop (MINUS_EXPR,
424 BINFO_OFFSET (TREE_VEC_ELT (binfos, j)),
425 BINFO_OFFSET (base_binfo));
426 break;
427 }
428
429 #if 0
430 if (BINFO_OFFSET_ZEROP (base_binfo))
431 BINFO_OFFSET (base_binfo) = offset;
432 else
433 BINFO_OFFSET (base_binfo)
434 = size_binop (PLUS_EXPR, BINFO_OFFSET (base_binfo), offset);
435 #else
436 BINFO_OFFSET (base_binfo) = offset;
437 #endif
438 if (base_binfos)
439 {
440 int k;
441 tree chain = NULL_TREE;
442
443 /* Now unshare the structure beneath BASE_BINFO. */
444 for (k = TREE_VEC_LENGTH (base_binfos)-1;
445 k >= 0; k--)
446 {
447 tree base_base_binfo = TREE_VEC_ELT (base_binfos, k);
448 if (! TREE_VIA_VIRTUAL (base_base_binfo))
449 TREE_VEC_ELT (base_binfos, k)
450 = make_binfo (BINFO_OFFSET (base_base_binfo),
451 BINFO_TYPE (base_base_binfo),
452 BINFO_VTABLE (base_base_binfo),
453 BINFO_VIRTUALS (base_base_binfo),
454 chain);
455 chain = TREE_VEC_ELT (base_binfos, k);
456 TREE_VIA_PUBLIC (chain) = TREE_VIA_PUBLIC (base_base_binfo);
457 TREE_VIA_PROTECTED (chain) = TREE_VIA_PROTECTED (base_base_binfo);
458 }
459 /* Now propagate the offset to the base types. */
460 propagate_binfo_offsets (base_binfo, offset);
461 }
462
463 /* Go to our next class that counts for offset propagation. */
464 i = j;
465 if (i < n_baselinks)
466 offset = size_binop (PLUS_EXPR, offset, delta);
467 }
468 }
469 }
470
471 /* Compute the actual offsets that our virtual base classes
472 will have *for this type*. This must be performed after
473 the fields are laid out, since virtual baseclasses must
474 lay down at the end of the record.
475
476 Returns the maximum number of virtual functions any of the virtual
477 baseclasses provide. */
478 int
479 layout_vbasetypes (rec, max)
480 tree rec;
481 int max;
482 {
483 /* Get all the virtual base types that this type uses.
484 The TREE_VALUE slot holds the virtual baseclass type. */
485 tree vbase_types = get_vbase_types (rec);
486
487 #ifdef STRUCTURE_SIZE_BOUNDARY
488 unsigned record_align = MAX (STRUCTURE_SIZE_BOUNDARY, TYPE_ALIGN (rec));
489 #else
490 unsigned record_align = MAX (BITS_PER_UNIT, TYPE_ALIGN (rec));
491 #endif
492
493 /* Record size so far is CONST_SIZE + VAR_SIZE bits,
494 where CONST_SIZE is an integer
495 and VAR_SIZE is a tree expression.
496 If VAR_SIZE is null, the size is just CONST_SIZE.
497 Naturally we try to avoid using VAR_SIZE. */
498 register unsigned const_size = 0;
499 register tree var_size = 0;
500 int nonvirtual_const_size;
501 tree nonvirtual_var_size;
502
503 CLASSTYPE_VBASECLASSES (rec) = vbase_types;
504
505 if (TREE_CODE (TYPE_SIZE (rec)) == INTEGER_CST)
506 const_size = TREE_INT_CST_LOW (TYPE_SIZE (rec));
507 else
508 var_size = TYPE_SIZE (rec);
509
510 nonvirtual_const_size = const_size;
511 nonvirtual_var_size = var_size;
512
513 while (vbase_types)
514 {
515 tree basetype = BINFO_TYPE (vbase_types);
516 tree offset;
517
518 if (const_size == 0)
519 offset = integer_zero_node;
520 else
521 offset = size_int ((const_size + BITS_PER_UNIT - 1) / BITS_PER_UNIT);
522
523 if (CLASSTYPE_VSIZE (basetype) > max)
524 max = CLASSTYPE_VSIZE (basetype);
525 BINFO_OFFSET (vbase_types) = offset;
526
527 if (TREE_CODE (TYPE_SIZE (basetype)) == INTEGER_CST)
528 const_size += MAX (record_align,
529 TREE_INT_CST_LOW (TYPE_SIZE (basetype))
530 - TREE_INT_CST_LOW (CLASSTYPE_VBASE_SIZE (basetype)));
531 else if (var_size == 0)
532 var_size = TYPE_SIZE (basetype);
533 else
534 var_size = size_binop (PLUS_EXPR, var_size, TYPE_SIZE (basetype));
535
536 vbase_types = TREE_CHAIN (vbase_types);
537 }
538
539 if (const_size != nonvirtual_const_size)
540 {
541 CLASSTYPE_VBASE_SIZE (rec)
542 = size_int (const_size - nonvirtual_const_size);
543 TYPE_SIZE (rec) = size_int (const_size);
544 }
545
546 /* Now propagate offset information throughout the lattice
547 under the vbase type. */
548 for (vbase_types = CLASSTYPE_VBASECLASSES (rec); vbase_types;
549 vbase_types = TREE_CHAIN (vbase_types))
550 {
551 tree base_binfos = BINFO_BASETYPES (vbase_types);
552
553 if (base_binfos)
554 {
555 tree chain = NULL_TREE;
556 int j;
557 /* Now unshare the structure beneath BASE_BINFO. */
558
559 for (j = TREE_VEC_LENGTH (base_binfos)-1;
560 j >= 0; j--)
561 {
562 tree base_base_binfo = TREE_VEC_ELT (base_binfos, j);
563 if (! TREE_VIA_VIRTUAL (base_base_binfo))
564 TREE_VEC_ELT (base_binfos, j)
565 = make_binfo (BINFO_OFFSET (base_base_binfo),
566 BINFO_TYPE (base_base_binfo),
567 BINFO_VTABLE (base_base_binfo),
568 BINFO_VIRTUALS (base_base_binfo),
569 chain);
570 chain = TREE_VEC_ELT (base_binfos, j);
571 TREE_VIA_PUBLIC (chain) = TREE_VIA_PUBLIC (base_base_binfo);
572 TREE_VIA_PROTECTED (chain) = TREE_VIA_PROTECTED (base_base_binfo);
573 }
574
575 propagate_binfo_offsets (vbase_types, BINFO_OFFSET (vbase_types));
576 }
577 }
578
579 return max;
580 }
581
582 /* Lay out the base types of a record type, REC.
583 Tentatively set the size and alignment of REC
584 according to the base types alone.
585
586 Offsets for immediate nonvirtual baseclasses are also computed here.
587
588 Returns list of virtual base classes in a FIELD_DECL chain. */
589 tree
590 layout_basetypes (rec, binfos)
591 tree rec, binfos;
592 {
593 /* Chain to hold all the new FIELD_DECLs which point at virtual
594 base classes. */
595 tree vbase_decls = NULL_TREE;
596
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
602
603 /* Record size so far is CONST_SIZE + VAR_SIZE bits,
604 where CONST_SIZE is an integer
605 and VAR_SIZE is a tree expression.
606 If VAR_SIZE is null, the size is just CONST_SIZE.
607 Naturally we try to avoid using VAR_SIZE. */
608 register unsigned const_size = 0;
609 register tree var_size = 0;
610 int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
611
612 /* Handle basetypes almost like fields, but record their
613 offsets differently. */
614
615 for (i = 0; i < n_baseclasses; i++)
616 {
617 int inc, desired_align, int_vbase_size;
618 register tree base_binfo = TREE_VEC_ELT (binfos, i);
619 register tree basetype = BINFO_TYPE (base_binfo);
620 tree decl, offset;
621
622 if (TYPE_SIZE (basetype) == 0)
623 {
624 #if 0
625 /* This error is now reported in xref_tag, thus giving better
626 location information. */
627 error_with_aggr_type (base_binfo,
628 "base class `%s' has incomplete type");
629
630 TREE_VIA_PUBLIC (base_binfo) = 1;
631 TREE_VIA_PROTECTED (base_binfo) = 0;
632 TREE_VIA_VIRTUAL (base_binfo) = 0;
633
634 /* Should handle this better so that
635
636 class A;
637 class B: private A { virtual void F(); };
638
639 does not dump core when compiled. */
640 my_friendly_abort (121);
641 #endif
642 continue;
643 }
644
645 /* All basetypes are recorded in the association list of the
646 derived type. */
647
648 if (TREE_VIA_VIRTUAL (base_binfo))
649 {
650 int j;
651 char *name = (char *)alloca (TYPE_NAME_LENGTH (basetype)
652 + sizeof (VBASE_NAME) + 1);
653
654 /* The offset for a virtual base class is only used in computing
655 virtual function tables and for initializing virtual base
656 pointers. It is built once `get_vbase_types' is called. */
657
658 /* If this basetype can come from another vbase pointer
659 without an additional indirection, we will share
660 that pointer. If an indirection is involved, we
661 make our own pointer. */
662 for (j = 0; j < n_baseclasses; j++)
663 {
664 tree other_base_binfo = TREE_VEC_ELT (binfos, j);
665 if (! TREE_VIA_VIRTUAL (other_base_binfo)
666 && binfo_member (basetype,
667 CLASSTYPE_VBASECLASSES (BINFO_TYPE (other_base_binfo))))
668 goto got_it;
669 }
670 sprintf (name, VBASE_NAME_FORMAT, TYPE_NAME_STRING (basetype));
671 decl = build_lang_decl (FIELD_DECL, get_identifier (name),
672 build_pointer_type (basetype));
673 /* If you change any of the below, take a look at all the
674 other VFIELD_BASEs and VTABLE_BASEs in the code, and change
675 them too. */
676 DECL_ASSEMBLER_NAME (decl) = get_identifier (VTABLE_BASE);
677 DECL_VIRTUAL_P (decl) = 1;
678 DECL_FIELD_CONTEXT (decl) = rec;
679 DECL_CLASS_CONTEXT (decl) = rec;
680 DECL_FCONTEXT (decl) = basetype;
681 DECL_FIELD_SIZE (decl) = 0;
682 DECL_ALIGN (decl) = TYPE_ALIGN (ptr_type_node);
683 TREE_CHAIN (decl) = vbase_decls;
684 BINFO_VPTR_FIELD (base_binfo) = decl;
685 vbase_decls = decl;
686
687 if (warn_nonvdtor && TYPE_HAS_DESTRUCTOR (basetype)
688 && DECL_VINDEX (TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (basetype), 0)) == NULL_TREE)
689 {
690 warning_with_decl (TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (basetype), 0),
691 "destructor `%s' non-virtual");
692 warning ("in inheritance relationship `%s: virtual %s'",
693 TYPE_NAME_STRING (rec),
694 TYPE_NAME_STRING (basetype));
695 }
696 got_it:
697 /* The space this decl occupies has already been accounted for. */
698 continue;
699 }
700
701 if (const_size == 0)
702 offset = integer_zero_node;
703 else
704 {
705 /* Give each base type the alignment it wants. */
706 const_size = CEIL (const_size, TYPE_ALIGN (basetype))
707 * TYPE_ALIGN (basetype);
708 offset = size_int ((const_size + BITS_PER_UNIT - 1) / BITS_PER_UNIT);
709
710 #if 0
711 /* bpk: Disabled this check until someone is willing to
712 claim it as theirs and explain exactly what circumstances
713 warrant the warning. */
714 if (warn_nonvdtor && TYPE_HAS_DESTRUCTOR (basetype)
715 && DECL_VINDEX (TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (basetype), 0)) == NULL_TREE)
716 {
717 warning_with_decl (TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (basetype), 0),
718 "destructor `%s' non-virtual");
719 warning ("in inheritance relationship `%s:%s %s'",
720 TYPE_NAME_STRING (rec),
721 TREE_VIA_VIRTUAL (base_binfo) ? " virtual" : "",
722 TYPE_NAME_STRING (basetype));
723 }
724 #endif
725 }
726 BINFO_OFFSET (base_binfo) = offset;
727 if (CLASSTYPE_VSIZE (basetype))
728 {
729 BINFO_VTABLE (base_binfo) = TYPE_BINFO_VTABLE (basetype);
730 BINFO_VIRTUALS (base_binfo) = TYPE_BINFO_VIRTUALS (basetype);
731 }
732 TREE_CHAIN (base_binfo) = TYPE_BINFO (rec);
733 TYPE_BINFO (rec) = base_binfo;
734
735 /* Add only the amount of storage not present in
736 the virtual baseclasses. */
737
738 int_vbase_size = TREE_INT_CST_LOW (CLASSTYPE_VBASE_SIZE (basetype));
739 if (TREE_INT_CST_LOW (TYPE_SIZE (basetype)) > int_vbase_size)
740 {
741 inc = MAX (record_align,
742 (TREE_INT_CST_LOW (TYPE_SIZE (basetype))
743 - int_vbase_size));
744
745 /* Record must have at least as much alignment as any field. */
746 desired_align = TYPE_ALIGN (basetype);
747 record_align = MAX (record_align, desired_align);
748
749 const_size += inc;
750 }
751 }
752
753 if (const_size)
754 CLASSTYPE_SIZE (rec) = size_int (const_size);
755 else
756 CLASSTYPE_SIZE (rec) = integer_zero_node;
757 CLASSTYPE_ALIGN (rec) = record_align;
758
759 return vbase_decls;
760 }
761 \f
762 /* Hashing of lists so that we don't make duplicates.
763 The entry point is `list_hash_canon'. */
764
765 /* Each hash table slot is a bucket containing a chain
766 of these structures. */
767
768 struct list_hash
769 {
770 struct list_hash *next; /* Next structure in the bucket. */
771 int hashcode; /* Hash code of this list. */
772 tree list; /* The list recorded here. */
773 };
774
775 /* Now here is the hash table. When recording a list, it is added
776 to the slot whose index is the hash code mod the table size.
777 Note that the hash table is used for several kinds of lists.
778 While all these live in the same table, they are completely independent,
779 and the hash code is computed differently for each of these. */
780
781 #define TYPE_HASH_SIZE 59
782 struct list_hash *list_hash_table[TYPE_HASH_SIZE];
783
784 /* Compute a hash code for a list (chain of TREE_LIST nodes
785 with goodies in the TREE_PURPOSE, TREE_VALUE, and bits of the
786 TREE_COMMON slots), by adding the hash codes of the individual entries. */
787
788 int
789 list_hash (list)
790 tree list;
791 {
792 register int hashcode = 0;
793
794 if (TREE_CHAIN (list))
795 hashcode += TYPE_HASH (TREE_CHAIN (list));
796
797 if (TREE_VALUE (list))
798 hashcode += TYPE_HASH (TREE_VALUE (list));
799 else
800 hashcode += 1007;
801 if (TREE_PURPOSE (list))
802 hashcode += TYPE_HASH (TREE_PURPOSE (list));
803 else
804 hashcode += 1009;
805 return hashcode;
806 }
807
808 /* Look in the type hash table for a type isomorphic to TYPE.
809 If one is found, return it. Otherwise return 0. */
810
811 tree
812 list_hash_lookup (hashcode, list)
813 int hashcode;
814 tree list;
815 {
816 register struct list_hash *h;
817 for (h = list_hash_table[hashcode % TYPE_HASH_SIZE]; h; h = h->next)
818 if (h->hashcode == hashcode
819 && TREE_VIA_VIRTUAL (h->list) == TREE_VIA_VIRTUAL (list)
820 && TREE_VIA_PUBLIC (h->list) == TREE_VIA_PUBLIC (list)
821 && TREE_VIA_PROTECTED (h->list) == TREE_VIA_PROTECTED (list)
822 && TREE_PURPOSE (h->list) == TREE_PURPOSE (list)
823 && TREE_VALUE (h->list) == TREE_VALUE (list)
824 && TREE_CHAIN (h->list) == TREE_CHAIN (list))
825 {
826 my_friendly_assert (TREE_TYPE (h->list) == TREE_TYPE (list), 299);
827 return h->list;
828 }
829 return 0;
830 }
831
832 /* Add an entry to the list-hash-table
833 for a list TYPE whose hash code is HASHCODE. */
834
835 void
836 list_hash_add (hashcode, list)
837 int hashcode;
838 tree list;
839 {
840 register struct list_hash *h;
841
842 h = (struct list_hash *) obstack_alloc (&class_obstack, sizeof (struct list_hash));
843 h->hashcode = hashcode;
844 h->list = list;
845 h->next = list_hash_table[hashcode % TYPE_HASH_SIZE];
846 list_hash_table[hashcode % TYPE_HASH_SIZE] = h;
847 }
848
849 /* Given TYPE, and HASHCODE its hash code, return the canonical
850 object for an identical list if one already exists.
851 Otherwise, return TYPE, and record it as the canonical object
852 if it is a permanent object.
853
854 To use this function, first create a list of the sort you want.
855 Then compute its hash code from the fields of the list that
856 make it different from other similar lists.
857 Then call this function and use the value.
858 This function frees the list you pass in if it is a duplicate. */
859
860 /* Set to 1 to debug without canonicalization. Never set by program. */
861 int debug_no_list_hash = 0;
862
863 tree
864 list_hash_canon (hashcode, list)
865 int hashcode;
866 tree list;
867 {
868 tree t1;
869
870 if (debug_no_list_hash)
871 return list;
872
873 t1 = list_hash_lookup (hashcode, list);
874 if (t1 != 0)
875 {
876 obstack_free (&class_obstack, list);
877 return t1;
878 }
879
880 /* If this is a new list, record it for later reuse. */
881 list_hash_add (hashcode, list);
882
883 return list;
884 }
885
886 tree
887 hash_tree_cons (via_public, via_virtual, via_protected, purpose, value, chain)
888 int via_public, via_virtual, via_protected;
889 tree purpose, value, chain;
890 {
891 struct obstack *ambient_obstack = current_obstack;
892 tree t;
893 int hashcode;
894
895 current_obstack = &class_obstack;
896 t = tree_cons (purpose, value, chain);
897 TREE_VIA_PUBLIC (t) = via_public;
898 TREE_VIA_PROTECTED (t) = via_protected;
899 TREE_VIA_VIRTUAL (t) = via_virtual;
900 hashcode = list_hash (t);
901 t = list_hash_canon (hashcode, t);
902 current_obstack = ambient_obstack;
903 return t;
904 }
905
906 /* Constructor for hashed lists. */
907 tree
908 hash_tree_chain (value, chain)
909 tree value, chain;
910 {
911 struct obstack *ambient_obstack = current_obstack;
912 tree t;
913 int hashcode;
914
915 current_obstack = &class_obstack;
916 t = tree_cons (NULL_TREE, value, chain);
917 hashcode = list_hash (t);
918 t = list_hash_canon (hashcode, t);
919 current_obstack = ambient_obstack;
920 return t;
921 }
922
923 /* Similar, but used for concatenating two lists. */
924 tree
925 hash_chainon (list1, list2)
926 tree list1, list2;
927 {
928 if (list2 == 0)
929 return list1;
930 if (list1 == 0)
931 return list2;
932 if (TREE_CHAIN (list1) == NULL_TREE)
933 return hash_tree_chain (TREE_VALUE (list1), list2);
934 return hash_tree_chain (TREE_VALUE (list1),
935 hash_chainon (TREE_CHAIN (list1), list2));
936 }
937
938 static tree
939 get_identifier_list (value)
940 tree value;
941 {
942 tree list = IDENTIFIER_AS_LIST (value);
943 if (list != NULL_TREE
944 && (TREE_CODE (list) != TREE_LIST
945 || TREE_VALUE (list) != value))
946 list = NULL_TREE;
947 else if (IDENTIFIER_HAS_TYPE_VALUE (value)
948 && TREE_CODE (IDENTIFIER_TYPE_VALUE (value)) == RECORD_TYPE)
949 {
950 tree type = IDENTIFIER_TYPE_VALUE (value);
951
952 if (TYPE_PTRMEMFUNC_P (type))
953 list = NULL_TREE;
954 else if (type == current_class_type)
955 /* Don't mess up the constructor name. */
956 list = tree_cons (NULL_TREE, value, NULL_TREE);
957 else
958 {
959 register tree id;
960 /* This will return the correct thing for regular types,
961 nested types, and templates. Yay! */
962 if (TYPE_NESTED_NAME (type))
963 id = TYPE_NESTED_NAME (type);
964 else
965 id = TYPE_IDENTIFIER (type);
966
967 if (CLASSTYPE_ID_AS_LIST (type) == NULL_TREE)
968 CLASSTYPE_ID_AS_LIST (type)
969 = perm_tree_cons (NULL_TREE, id, NULL_TREE);
970 list = CLASSTYPE_ID_AS_LIST (type);
971 }
972 }
973 return list;
974 }
975
976 tree
977 get_decl_list (value)
978 tree value;
979 {
980 tree list = NULL_TREE;
981
982 if (TREE_CODE (value) == IDENTIFIER_NODE)
983 list = get_identifier_list (value);
984 else if (TREE_CODE (value) == RECORD_TYPE
985 && TYPE_LANG_SPECIFIC (value))
986 list = CLASSTYPE_AS_LIST (value);
987
988 if (list != NULL_TREE)
989 {
990 my_friendly_assert (TREE_CHAIN (list) == NULL_TREE, 301);
991 return list;
992 }
993
994 return build_decl_list (NULL_TREE, value);
995 }
996
997 /* Look in the type hash table for a type isomorphic to
998 `build_tree_list (NULL_TREE, VALUE)'.
999 If one is found, return it. Otherwise return 0. */
1000
1001 tree
1002 list_hash_lookup_or_cons (value)
1003 tree value;
1004 {
1005 register int hashcode = TYPE_HASH (value);
1006 register struct list_hash *h;
1007 struct obstack *ambient_obstack;
1008 tree list = NULL_TREE;
1009
1010 if (TREE_CODE (value) == IDENTIFIER_NODE)
1011 list = get_identifier_list (value);
1012 else if (TREE_CODE (value) == TYPE_DECL
1013 && TREE_CODE (TREE_TYPE (value)) == RECORD_TYPE
1014 && TYPE_LANG_SPECIFIC (TREE_TYPE (value)))
1015 list = CLASSTYPE_ID_AS_LIST (TREE_TYPE (value));
1016 else if (TREE_CODE (value) == RECORD_TYPE
1017 && TYPE_LANG_SPECIFIC (value))
1018 list = CLASSTYPE_AS_LIST (value);
1019
1020 if (list != NULL_TREE)
1021 {
1022 my_friendly_assert (TREE_CHAIN (list) == NULL_TREE, 302);
1023 return list;
1024 }
1025
1026 if (debug_no_list_hash)
1027 return hash_tree_chain (value, NULL_TREE);
1028
1029 for (h = list_hash_table[hashcode % TYPE_HASH_SIZE]; h; h = h->next)
1030 if (h->hashcode == hashcode
1031 && TREE_VIA_VIRTUAL (h->list) == 0
1032 && TREE_VIA_PUBLIC (h->list) == 0
1033 && TREE_VIA_PROTECTED (h->list) == 0
1034 && TREE_PURPOSE (h->list) == 0
1035 && TREE_VALUE (h->list) == value)
1036 {
1037 my_friendly_assert (TREE_TYPE (h->list) == 0, 303);
1038 my_friendly_assert (TREE_CHAIN (h->list) == 0, 304);
1039 return h->list;
1040 }
1041
1042 ambient_obstack = current_obstack;
1043 current_obstack = &class_obstack;
1044 list = build_tree_list (NULL_TREE, value);
1045 list_hash_add (hashcode, list);
1046 current_obstack = ambient_obstack;
1047 return list;
1048 }
1049 \f
1050 /* Build an association between TYPE and some parameters:
1051
1052 OFFSET is the offset added to `this' to convert it to a pointer
1053 of type `TYPE *'
1054
1055 VTABLE is the virtual function table with which to initialize
1056 sub-objects of type TYPE.
1057
1058 VIRTUALS are the virtual functions sitting in VTABLE.
1059
1060 CHAIN are more associations we must retain. */
1061
1062 tree
1063 make_binfo (offset, type, vtable, virtuals, chain)
1064 tree offset, type;
1065 tree vtable, virtuals;
1066 tree chain;
1067 {
1068 tree binfo = make_tree_vec (6);
1069 tree old_binfo = TYPE_BINFO (type);
1070 tree last;
1071
1072 TREE_CHAIN (binfo) = chain;
1073 if (chain)
1074 TREE_USED (binfo) = TREE_USED (chain);
1075
1076 TREE_TYPE (binfo) = TYPE_MAIN_VARIANT (type);
1077 BINFO_OFFSET (binfo) = offset;
1078 BINFO_VTABLE (binfo) = vtable;
1079 BINFO_VIRTUALS (binfo) = virtuals;
1080 BINFO_VPTR_FIELD (binfo) = NULL_TREE;
1081
1082 last = binfo;
1083 if (old_binfo != NULL_TREE
1084 && BINFO_BASETYPES (old_binfo) != NULL_TREE)
1085 {
1086 int i, n_baseclasses = CLASSTYPE_N_BASECLASSES (type);
1087 tree binfos = TYPE_BINFO_BASETYPES (type);
1088
1089 BINFO_BASETYPES (binfo) = make_tree_vec (n_baseclasses);
1090 for (i = 0; i < n_baseclasses; i++)
1091 {
1092 tree base_binfo = TREE_VEC_ELT (binfos, i);
1093 tree old_base_binfo = old_binfo ? BINFO_BASETYPE (old_binfo, i) : 0;
1094 BINFO_BASETYPE (binfo, i) = base_binfo;
1095 if (old_binfo)
1096 {
1097 TREE_VIA_PUBLIC (base_binfo) = TREE_VIA_PUBLIC (old_base_binfo);
1098 TREE_VIA_PROTECTED (base_binfo) = TREE_VIA_PROTECTED (old_base_binfo);
1099 TREE_VIA_VIRTUAL (base_binfo) = TREE_VIA_VIRTUAL (old_base_binfo);
1100 }
1101 }
1102 }
1103 return binfo;
1104 }
1105
1106 tree
1107 copy_binfo (list)
1108 tree list;
1109 {
1110 tree binfo = copy_list (list);
1111 tree rval = binfo;
1112 while (binfo)
1113 {
1114 TREE_USED (binfo) = 0;
1115 if (BINFO_BASETYPES (binfo))
1116 BINFO_BASETYPES (binfo) = copy_node (BINFO_BASETYPES (binfo));
1117 binfo = TREE_CHAIN (binfo);
1118 }
1119 return rval;
1120 }
1121
1122 /* Return the binfo value for ELEM in TYPE. */
1123
1124 tree
1125 binfo_value (elem, type)
1126 tree elem;
1127 tree type;
1128 {
1129 if (get_base_distance (elem, type, 0, (tree *)0) == -2)
1130 compiler_error ("base class `%s' ambiguous in binfo_value",
1131 TYPE_NAME_STRING (elem));
1132 if (elem == type)
1133 return TYPE_BINFO (type);
1134 if (TREE_CODE (elem) == RECORD_TYPE && TYPE_BINFO (elem) == type)
1135 return type;
1136 return get_binfo (elem, type, 0);
1137 }
1138
1139 tree
1140 reverse_path (path)
1141 tree path;
1142 {
1143 register tree prev = 0, tmp, next;
1144 for (tmp = path; tmp; tmp = next)
1145 {
1146 next = BINFO_INHERITANCE_CHAIN (tmp);
1147 BINFO_INHERITANCE_CHAIN (tmp) = prev;
1148 prev = tmp;
1149 }
1150 return prev;
1151 }
1152
1153 tree
1154 virtual_member (elem, list)
1155 tree elem;
1156 tree list;
1157 {
1158 tree t;
1159 tree rval, nval;
1160
1161 for (t = list; t; t = TREE_CHAIN (t))
1162 if (elem == BINFO_TYPE (t))
1163 return t;
1164 rval = 0;
1165 for (t = list; t; t = TREE_CHAIN (t))
1166 {
1167 tree binfos = BINFO_BASETYPES (t);
1168 int i;
1169
1170 if (binfos != NULL_TREE)
1171 for (i = TREE_VEC_LENGTH (binfos)-1; i >= 0; i--)
1172 {
1173 nval = binfo_value (elem, BINFO_TYPE (TREE_VEC_ELT (binfos, i)));
1174 if (nval)
1175 {
1176 if (rval && BINFO_OFFSET (nval) != BINFO_OFFSET (rval))
1177 my_friendly_abort (104);
1178 rval = nval;
1179 }
1180 }
1181 }
1182 return rval;
1183 }
1184
1185 /* Return the offset (as an INTEGER_CST) for ELEM in LIST.
1186 INITIAL_OFFSET is the value to add to the offset that ELEM's
1187 binfo entry in LIST provides.
1188
1189 Returns NULL if ELEM does not have an binfo value in LIST. */
1190
1191 tree
1192 virtual_offset (elem, list, initial_offset)
1193 tree elem;
1194 tree list;
1195 tree initial_offset;
1196 {
1197 tree vb, offset;
1198 tree rval, nval;
1199
1200 for (vb = list; vb; vb = TREE_CHAIN (vb))
1201 if (elem == BINFO_TYPE (vb))
1202 return size_binop (PLUS_EXPR, initial_offset, BINFO_OFFSET (vb));
1203 rval = 0;
1204 for (vb = list; vb; vb = TREE_CHAIN (vb))
1205 {
1206 tree binfos = BINFO_BASETYPES (vb);
1207 int i;
1208
1209 if (binfos == NULL_TREE)
1210 continue;
1211
1212 for (i = TREE_VEC_LENGTH (binfos)-1; i >= 0; i--)
1213 {
1214 nval = binfo_value (elem, BINFO_TYPE (TREE_VEC_ELT (binfos, i)));
1215 if (nval)
1216 {
1217 if (rval && BINFO_OFFSET (nval) != BINFO_OFFSET (rval))
1218 my_friendly_abort (105);
1219 offset = BINFO_OFFSET (vb);
1220 rval = nval;
1221 }
1222 }
1223 }
1224 if (rval == NULL_TREE)
1225 return rval;
1226 return size_binop (PLUS_EXPR, offset, BINFO_OFFSET (rval));
1227 }
1228
1229 void
1230 debug_binfo (elem)
1231 tree elem;
1232 {
1233 int i;
1234 tree virtuals;
1235
1236 fprintf (stderr, "type \"%s\"; offset = %d\n",
1237 TYPE_NAME_STRING (BINFO_TYPE (elem)),
1238 TREE_INT_CST_LOW (BINFO_OFFSET (elem)));
1239 fprintf (stderr, "vtable type:\n");
1240 debug_tree (BINFO_TYPE (elem));
1241 if (BINFO_VTABLE (elem))
1242 fprintf (stderr, "vtable decl \"%s\"\n", IDENTIFIER_POINTER (DECL_NAME (BINFO_VTABLE (elem))));
1243 else
1244 fprintf (stderr, "no vtable decl yet\n");
1245 fprintf (stderr, "virtuals:\n");
1246 virtuals = BINFO_VIRTUALS (elem);
1247 if (virtuals != 0)
1248 {
1249 virtuals = TREE_CHAIN (virtuals);
1250 if (flag_dossier)
1251 virtuals = TREE_CHAIN (virtuals);
1252 }
1253 i = 1;
1254 while (virtuals)
1255 {
1256 tree fndecl = TREE_OPERAND (FNADDR_FROM_VTABLE_ENTRY (TREE_VALUE (virtuals)), 0);
1257 fprintf (stderr, "%s [%d =? %d]\n",
1258 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (fndecl)),
1259 i, TREE_INT_CST_LOW (DECL_VINDEX (fndecl)));
1260 virtuals = TREE_CHAIN (virtuals);
1261 i += 1;
1262 }
1263 }
1264
1265 /* Return the length of a chain of nodes chained through DECL_CHAIN.
1266 We expect a null pointer to mark the end of the chain.
1267 This is the Lisp primitive `length'. */
1268
1269 int
1270 decl_list_length (t)
1271 tree t;
1272 {
1273 register tree tail;
1274 register int len = 0;
1275
1276 my_friendly_assert (TREE_CODE (t) == FUNCTION_DECL
1277 || TREE_CODE (t) == TEMPLATE_DECL, 300);
1278 for (tail = t; tail; tail = DECL_CHAIN (tail))
1279 len++;
1280
1281 return len;
1282 }
1283
1284 int
1285 count_functions (t)
1286 tree t;
1287 {
1288 if (TREE_CODE (t) == FUNCTION_DECL)
1289 return 1;
1290
1291 return decl_list_length (TREE_VALUE (t));
1292 }
1293
1294 /* Like value_member, but for DECL_CHAINs. */
1295 tree
1296 decl_value_member (elem, list)
1297 tree elem, list;
1298 {
1299 while (list)
1300 {
1301 if (elem == list)
1302 return list;
1303 list = DECL_CHAIN (list);
1304 }
1305 return NULL_TREE;
1306 }
1307
1308 int
1309 is_overloaded_fn (x)
1310 tree x;
1311 {
1312 if (TREE_CODE (x) == FUNCTION_DECL)
1313 return 1;
1314
1315 if (TREE_CODE (x) == TREE_LIST
1316 && (TREE_CODE (TREE_VALUE (x)) == FUNCTION_DECL
1317 || TREE_CODE (TREE_VALUE (x)) == TEMPLATE_DECL))
1318 return 1;
1319
1320 return 0;
1321 }
1322
1323 tree
1324 get_first_fn (from)
1325 tree from;
1326 {
1327 if (TREE_CODE (from) == FUNCTION_DECL)
1328 return from;
1329
1330 my_friendly_assert (TREE_CODE (from) == TREE_LIST, 9);
1331
1332 return TREE_VALUE (from);
1333 }
1334
1335 tree
1336 fnaddr_from_vtable_entry (entry)
1337 tree entry;
1338 {
1339 return TREE_VALUE (TREE_CHAIN (TREE_CHAIN (CONSTRUCTOR_ELTS (entry))));
1340 }
1341
1342 void
1343 set_fnaddr_from_vtable_entry (entry, value)
1344 tree entry, value;
1345 {
1346 TREE_VALUE (TREE_CHAIN (TREE_CHAIN (CONSTRUCTOR_ELTS (entry)))) = value;
1347 }
1348
1349 tree
1350 function_arg_chain (t)
1351 tree t;
1352 {
1353 return TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (t)));
1354 }
1355
1356 int
1357 promotes_to_aggr_type (t, code)
1358 tree t;
1359 enum tree_code code;
1360 {
1361 if (TREE_CODE (t) == code)
1362 t = TREE_TYPE (t);
1363 return IS_AGGR_TYPE (t);
1364 }
1365
1366 int
1367 is_aggr_type_2 (t1, t2)
1368 tree t1, t2;
1369 {
1370 if (TREE_CODE (t1) != TREE_CODE (t2))
1371 return 0;
1372 return IS_AGGR_TYPE (t1) && IS_AGGR_TYPE (t2);
1373 }
1374
1375 /* Give message using types TYPE1 and TYPE2 as arguments.
1376 PFN is the function which will print the message;
1377 S is the format string for PFN to use. */
1378 void
1379 message_2_types (pfn, s, type1, type2)
1380 void (*pfn) ();
1381 char *s;
1382 tree type1, type2;
1383 {
1384 tree name1 = TYPE_NAME (type1);
1385 tree name2 = TYPE_NAME (type2);
1386 if (TREE_CODE (name1) == TYPE_DECL)
1387 name1 = DECL_NAME (name1);
1388 if (TREE_CODE (name2) == TYPE_DECL)
1389 name2 = DECL_NAME (name2);
1390 (*pfn) (s, IDENTIFIER_POINTER (name1), IDENTIFIER_POINTER (name2));
1391 }
1392 \f
1393 #define PRINT_RING_SIZE 4
1394
1395 char *
1396 lang_printable_name (decl)
1397 tree decl;
1398 {
1399 static tree decl_ring[PRINT_RING_SIZE];
1400 static char *print_ring[PRINT_RING_SIZE];
1401 static int ring_counter;
1402 int i;
1403
1404 /* Only cache functions. */
1405 if (TREE_CODE (decl) != FUNCTION_DECL
1406 || DECL_LANG_SPECIFIC (decl) == 0)
1407 return decl_as_string (decl, 1);
1408
1409 /* See if this print name is lying around. */
1410 for (i = 0; i < PRINT_RING_SIZE; i++)
1411 if (decl_ring[i] == decl)
1412 /* yes, so return it. */
1413 return print_ring[i];
1414
1415 if (++ring_counter == PRINT_RING_SIZE)
1416 ring_counter = 0;
1417
1418 if (current_function_decl != NULL_TREE)
1419 {
1420 if (decl_ring[ring_counter] == current_function_decl)
1421 ring_counter += 1;
1422 if (ring_counter == PRINT_RING_SIZE)
1423 ring_counter = 0;
1424 if (decl_ring[ring_counter] == current_function_decl)
1425 my_friendly_abort (106);
1426 }
1427
1428 if (print_ring[ring_counter])
1429 free (print_ring[ring_counter]);
1430
1431 {
1432 int print_ret_type_p
1433 = (!DECL_CONSTRUCTOR_P (decl)
1434 && !DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (decl)));
1435
1436 char *name = (char *)decl_as_string (decl, print_ret_type_p);
1437 print_ring[ring_counter] = (char *)malloc (strlen (name) + 1);
1438 strcpy (print_ring[ring_counter], name);
1439 decl_ring[ring_counter] = decl;
1440 }
1441 return print_ring[ring_counter];
1442 }
1443 \f
1444 /* Comparison function for sorting identifiers in RAISES lists.
1445 Note that because IDENTIFIER_NODEs are unique, we can sort
1446 them by address, saving an indirection. */
1447 static int
1448 id_cmp (p1, p2)
1449 tree *p1, *p2;
1450 {
1451 return (HOST_WIDE_INT)TREE_VALUE (*p1) - (HOST_WIDE_INT)TREE_VALUE (*p2);
1452 }
1453
1454 /* Build the FUNCTION_TYPE or METHOD_TYPE which may raise exceptions
1455 listed in RAISES. */
1456 tree
1457 build_exception_variant (ctype, type, raises)
1458 tree ctype, type;
1459 tree raises;
1460 {
1461 int i;
1462 tree v = TYPE_MAIN_VARIANT (type);
1463 tree t, t2, cname;
1464 tree *a = (tree *)alloca ((list_length (raises)+1) * sizeof (tree));
1465 int constp = TYPE_READONLY (type);
1466 int volatilep = TYPE_VOLATILE (type);
1467
1468 if (raises && TREE_CHAIN (raises))
1469 {
1470 for (i = 0, t = raises; t; t = TREE_CHAIN (t), i++)
1471 a[i] = t;
1472 /* NULL terminator for list. */
1473 a[i] = NULL_TREE;
1474 qsort (a, i, sizeof (tree), id_cmp);
1475 while (i--)
1476 TREE_CHAIN (a[i]) = a[i+1];
1477 raises = a[0];
1478 }
1479 else if (raises)
1480 /* do nothing. */;
1481 else
1482 return build_type_variant (v, constp, volatilep);
1483
1484 if (ctype)
1485 {
1486 cname = TYPE_NAME (ctype);
1487 if (TREE_CODE (cname) == TYPE_DECL)
1488 cname = DECL_NAME (cname);
1489 }
1490 else
1491 cname = NULL_TREE;
1492
1493 for (t = raises; t; t = TREE_CHAIN (t))
1494 {
1495 /* See that all the exceptions we are thinking about
1496 raising have been declared. */
1497 tree this_cname = lookup_exception_cname (ctype, cname, t);
1498 tree decl = lookup_exception_object (this_cname, TREE_VALUE (t), 1);
1499
1500 if (decl == NULL_TREE)
1501 decl = lookup_exception_object (this_cname, TREE_VALUE (t), 0);
1502 /* Place canonical exception decl into TREE_TYPE of RAISES list. */
1503 TREE_TYPE (t) = decl;
1504 }
1505
1506 for (v = TYPE_NEXT_VARIANT (v); v; v = TYPE_NEXT_VARIANT (v))
1507 {
1508 if (TYPE_READONLY (v) != constp
1509 || TYPE_VOLATILE (v) != volatilep)
1510 continue;
1511
1512 t = raises;
1513 t2 = TYPE_RAISES_EXCEPTIONS (v);
1514 while (t && t2)
1515 {
1516 if (TREE_TYPE (t) == TREE_TYPE (t2))
1517 {
1518 t = TREE_CHAIN (t);
1519 t2 = TREE_CHAIN (t2);
1520 }
1521 else break;
1522 }
1523 if (t || t2)
1524 continue;
1525 /* List of exceptions raised matches previously found list.
1526
1527 @@ Nice to free up storage used in consing up the
1528 @@ list of exceptions raised. */
1529 return v;
1530 }
1531
1532 /* Need to build a new variant. */
1533 v = copy_node (type);
1534 TYPE_NEXT_VARIANT (v) = TYPE_NEXT_VARIANT (type);
1535 TYPE_NEXT_VARIANT (type) = v;
1536 if (raises && ! TREE_PERMANENT (raises))
1537 {
1538 push_obstacks_nochange ();
1539 end_temporary_allocation ();
1540 raises = copy_list (raises);
1541 pop_obstacks ();
1542 }
1543 TYPE_RAISES_EXCEPTIONS (v) = raises;
1544 return v;
1545 }
1546
1547 /* Subroutine of copy_to_permanent
1548
1549 Assuming T is a node build bottom-up, make it all exist on
1550 permanent obstack, if it is not permanent already. */
1551 static tree
1552 make_deep_copy (t)
1553 tree t;
1554 {
1555 enum tree_code code;
1556
1557 if (t == NULL_TREE || TREE_PERMANENT (t))
1558 return t;
1559
1560 switch (code = TREE_CODE (t))
1561 {
1562 case ERROR_MARK:
1563 return error_mark_node;
1564
1565 case VAR_DECL:
1566 case FUNCTION_DECL:
1567 case CONST_DECL:
1568 break;
1569
1570 case PARM_DECL:
1571 {
1572 tree chain = TREE_CHAIN (t);
1573 t = copy_node (t);
1574 TREE_CHAIN (t) = make_deep_copy (chain);
1575 TREE_TYPE (t) = make_deep_copy (TREE_TYPE (t));
1576 DECL_INITIAL (t) = make_deep_copy (DECL_INITIAL (t));
1577 DECL_SIZE (t) = make_deep_copy (DECL_SIZE (t));
1578 return t;
1579 }
1580
1581 case TREE_LIST:
1582 {
1583 tree chain = TREE_CHAIN (t);
1584 t = copy_node (t);
1585 TREE_PURPOSE (t) = make_deep_copy (TREE_PURPOSE (t));
1586 TREE_VALUE (t) = make_deep_copy (TREE_VALUE (t));
1587 TREE_CHAIN (t) = make_deep_copy (chain);
1588 return t;
1589 }
1590
1591 case TREE_VEC:
1592 {
1593 int len = TREE_VEC_LENGTH (t);
1594
1595 t = copy_node (t);
1596 while (len--)
1597 TREE_VEC_ELT (t, len) = make_deep_copy (TREE_VEC_ELT (t, len));
1598 return t;
1599 }
1600
1601 case INTEGER_CST:
1602 case REAL_CST:
1603 case STRING_CST:
1604 return copy_node (t);
1605
1606 case COND_EXPR:
1607 case TARGET_EXPR:
1608 case NEW_EXPR:
1609 t = copy_node (t);
1610 TREE_OPERAND (t, 0) = make_deep_copy (TREE_OPERAND (t, 0));
1611 TREE_OPERAND (t, 1) = make_deep_copy (TREE_OPERAND (t, 1));
1612 TREE_OPERAND (t, 2) = make_deep_copy (TREE_OPERAND (t, 2));
1613 return t;
1614
1615 case SAVE_EXPR:
1616 t = copy_node (t);
1617 TREE_OPERAND (t, 0) = make_deep_copy (TREE_OPERAND (t, 0));
1618 return t;
1619
1620 case MODIFY_EXPR:
1621 case PLUS_EXPR:
1622 case MINUS_EXPR:
1623 case MULT_EXPR:
1624 case TRUNC_DIV_EXPR:
1625 case TRUNC_MOD_EXPR:
1626 case MIN_EXPR:
1627 case MAX_EXPR:
1628 case LSHIFT_EXPR:
1629 case RSHIFT_EXPR:
1630 case BIT_IOR_EXPR:
1631 case BIT_XOR_EXPR:
1632 case BIT_AND_EXPR:
1633 case BIT_ANDTC_EXPR:
1634 case TRUTH_ANDIF_EXPR:
1635 case TRUTH_ORIF_EXPR:
1636 case LT_EXPR:
1637 case LE_EXPR:
1638 case GT_EXPR:
1639 case GE_EXPR:
1640 case EQ_EXPR:
1641 case NE_EXPR:
1642 case CEIL_DIV_EXPR:
1643 case FLOOR_DIV_EXPR:
1644 case ROUND_DIV_EXPR:
1645 case CEIL_MOD_EXPR:
1646 case FLOOR_MOD_EXPR:
1647 case ROUND_MOD_EXPR:
1648 case COMPOUND_EXPR:
1649 case PREDECREMENT_EXPR:
1650 case PREINCREMENT_EXPR:
1651 case POSTDECREMENT_EXPR:
1652 case POSTINCREMENT_EXPR:
1653 case CALL_EXPR:
1654 t = copy_node (t);
1655 TREE_OPERAND (t, 0) = make_deep_copy (TREE_OPERAND (t, 0));
1656 TREE_OPERAND (t, 1) = make_deep_copy (TREE_OPERAND (t, 1));
1657 return t;
1658
1659 case CONVERT_EXPR:
1660 case ADDR_EXPR:
1661 case INDIRECT_REF:
1662 case NEGATE_EXPR:
1663 case BIT_NOT_EXPR:
1664 case TRUTH_NOT_EXPR:
1665 case NOP_EXPR:
1666 case COMPONENT_REF:
1667 t = copy_node (t);
1668 TREE_OPERAND (t, 0) = make_deep_copy (TREE_OPERAND (t, 0));
1669 return t;
1670
1671 /* This list is incomplete, but should suffice for now.
1672 It is very important that `sorry' does not call
1673 `report_error_function'. That could cause an infinite loop. */
1674 default:
1675 sorry ("initializer contains unrecognized tree code");
1676 return error_mark_node;
1677
1678 }
1679 my_friendly_abort (107);
1680 /* NOTREACHED */
1681 return NULL_TREE;
1682 }
1683
1684 /* Assuming T is a node built bottom-up, make it all exist on
1685 permanent obstack, if it is not permanent already. */
1686 tree
1687 copy_to_permanent (t)
1688 tree t;
1689 {
1690 register struct obstack *ambient_obstack = current_obstack;
1691 register struct obstack *ambient_saveable_obstack = saveable_obstack;
1692
1693 if (t == NULL_TREE || TREE_PERMANENT (t))
1694 return t;
1695
1696 saveable_obstack = &permanent_obstack;
1697 current_obstack = saveable_obstack;
1698
1699 t = make_deep_copy (t);
1700
1701 current_obstack = ambient_obstack;
1702 saveable_obstack = ambient_saveable_obstack;
1703
1704 return t;
1705 }
1706
1707 void
1708 print_lang_statistics ()
1709 {
1710 extern struct obstack maybepermanent_obstack;
1711 print_obstack_statistics ("class_obstack", &class_obstack);
1712 print_obstack_statistics ("permanent_obstack", &permanent_obstack);
1713 print_obstack_statistics ("maybepermanent_obstack", &maybepermanent_obstack);
1714 print_search_statistics ();
1715 print_class_statistics ();
1716 }
1717
1718 /* This is used by the `assert' macro. It is provided in libgcc.a,
1719 which `cc' doesn't know how to link. Note that the C++ front-end
1720 no longer actually uses the `assert' macro (instead, it calls
1721 my_friendly_assert). But all of the back-end files still need this. */
1722 void
1723 __eprintf (string, expression, line, filename)
1724 #ifdef __STDC__
1725 const char *string;
1726 const char *expression;
1727 unsigned line;
1728 const char *filename;
1729 #else
1730 char *string;
1731 char *expression;
1732 unsigned line;
1733 char *filename;
1734 #endif
1735 {
1736 fprintf (stderr, string, expression, line, filename);
1737 fflush (stderr);
1738 abort ();
1739 }
1740
1741 /* Return, as an INTEGER_CST node, the number of elements for
1742 TYPE (which is an ARRAY_TYPE). This counts only elements of the top array. */
1743
1744 tree
1745 array_type_nelts_top (type)
1746 tree type;
1747 {
1748 return fold (build (PLUS_EXPR, integer_type_node,
1749 array_type_nelts (type),
1750 integer_one_node));
1751 }
1752
1753 /* Return, as an INTEGER_CST node, the number of elements for
1754 TYPE (which is an ARRAY_TYPE). This one is a recursive count of all
1755 ARRAY_TYPEs that are clumped together. */
1756
1757 tree
1758 array_type_nelts_total (type)
1759 tree type;
1760 {
1761 tree sz = array_type_nelts_top (type);
1762 type = TREE_TYPE (type);
1763 while (TREE_CODE (type) == ARRAY_TYPE)
1764 {
1765 tree n = array_type_nelts_top (type);
1766 sz = fold (build (MULT_EXPR, integer_type_node, sz, n));
1767 type = TREE_TYPE (type);
1768 }
1769 return sz;
1770 }