]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/cp/tree.c
Fix typo
[thirdparty/gcc.git] / gcc / cp / tree.c
CommitLineData
8d08fdba 1/* Language-dependent node constructors for parse phase of GNU compiler.
357a4089 2 Copyright (C) 1987, 88, 92, 93, 94, 95, 1996 Free Software Foundation, Inc.
8d08fdba
MS
3 Hacked by Michael Tiemann (tiemann@cygnus.com)
4
5This file is part of GNU CC.
6
7GNU CC is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2, or (at your option)
10any later version.
11
12GNU CC is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with GNU CC; see the file COPYING. If not, write to
e9fa0c7c
RK
19the Free Software Foundation, 59 Temple Place - Suite 330,
20Boston, MA 02111-1307, USA. */
8d08fdba
MS
21
22#include "config.h"
23#include <stdio.h>
24#include "obstack.h"
25#include "tree.h"
26#include "cp-tree.h"
27#include "flags.h"
28cbf42c 28#include "rtl.h"
5566b478
MS
29#ifdef __STDC__
30#include <stdarg.h>
31#else
32#include <varargs.h>
33#endif
8d08fdba
MS
34
35#define CEIL(x,y) (((x) + (y) - 1) / (y))
36
37/* Return nonzero if REF is an lvalue valid for this language.
38 Lvalues can be assigned, unless they have TREE_READONLY.
39 Lvalues can have their address taken, unless they have DECL_REGISTER. */
40
8ccc31eb
MS
41int
42real_lvalue_p (ref)
43 tree ref;
44{
45 if (! language_lvalue_valid (ref))
46 return 0;
47
48 if (TREE_CODE (TREE_TYPE (ref)) == REFERENCE_TYPE)
49 return 1;
50
4ac14744 51 if (ref == current_class_ptr && flag_this_is_variable <= 0)
8ccc31eb
MS
52 return 0;
53
54 switch (TREE_CODE (ref))
55 {
56 /* preincrements and predecrements are valid lvals, provided
e92cc029 57 what they refer to are valid lvals. */
8ccc31eb
MS
58 case PREINCREMENT_EXPR:
59 case PREDECREMENT_EXPR:
60 case COMPONENT_REF:
61 case SAVE_EXPR:
62 return real_lvalue_p (TREE_OPERAND (ref, 0));
63
64 case STRING_CST:
65 return 1;
66
67 case VAR_DECL:
68 if (TREE_READONLY (ref) && ! TREE_STATIC (ref)
69 && DECL_LANG_SPECIFIC (ref)
70 && DECL_IN_AGGR_P (ref))
71 return 0;
72 case INDIRECT_REF:
73 case ARRAY_REF:
74 case PARM_DECL:
75 case RESULT_DECL:
76 case ERROR_MARK:
77 if (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
78 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE)
79 return 1;
80 break;
81
8ccc31eb
MS
82 /* A currently unresolved scope ref. */
83 case SCOPE_REF:
84 my_friendly_abort (103);
85 case OFFSET_REF:
86 if (TREE_CODE (TREE_OPERAND (ref, 1)) == FUNCTION_DECL)
87 return 1;
88 return real_lvalue_p (TREE_OPERAND (ref, 0))
89 && real_lvalue_p (TREE_OPERAND (ref, 1));
90 break;
91
92 case COND_EXPR:
93 return (real_lvalue_p (TREE_OPERAND (ref, 1))
94 && real_lvalue_p (TREE_OPERAND (ref, 2)));
95
96 case MODIFY_EXPR:
97 return 1;
98
99 case COMPOUND_EXPR:
100 return real_lvalue_p (TREE_OPERAND (ref, 1));
101
102 case MAX_EXPR:
103 case MIN_EXPR:
104 return (real_lvalue_p (TREE_OPERAND (ref, 0))
105 && real_lvalue_p (TREE_OPERAND (ref, 1)));
106 }
107
108 return 0;
109}
110
8d08fdba
MS
111int
112lvalue_p (ref)
113 tree ref;
114{
a292b002
MS
115 if (! language_lvalue_valid (ref))
116 return 0;
117
118 if (TREE_CODE (TREE_TYPE (ref)) == REFERENCE_TYPE)
119 return 1;
8d08fdba 120
4ac14744 121 if (ref == current_class_ptr && flag_this_is_variable <= 0)
a292b002
MS
122 return 0;
123
124 switch (TREE_CODE (ref))
39211cd5 125 {
a292b002 126 /* preincrements and predecrements are valid lvals, provided
e92cc029 127 what they refer to are valid lvals. */
a292b002
MS
128 case PREINCREMENT_EXPR:
129 case PREDECREMENT_EXPR:
130 case COMPONENT_REF:
131 case SAVE_EXPR:
132 return lvalue_p (TREE_OPERAND (ref, 0));
133
134 case STRING_CST:
135 return 1;
136
137 case VAR_DECL:
138 if (TREE_READONLY (ref) && ! TREE_STATIC (ref)
139 && DECL_LANG_SPECIFIC (ref)
140 && DECL_IN_AGGR_P (ref))
141 return 0;
142 case INDIRECT_REF:
143 case ARRAY_REF:
144 case PARM_DECL:
145 case RESULT_DECL:
146 case ERROR_MARK:
147 if (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
148 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE)
8d08fdba 149 return 1;
a292b002 150 break;
39211cd5 151
a292b002
MS
152 case TARGET_EXPR:
153 return 1;
39211cd5 154
a292b002 155 case CALL_EXPR:
e8abc66f 156 if (IS_AGGR_TYPE (TREE_TYPE (ref)))
a292b002
MS
157 return 1;
158 break;
2986ae00 159
a292b002
MS
160 /* A currently unresolved scope ref. */
161 case SCOPE_REF:
162 my_friendly_abort (103);
163 case OFFSET_REF:
164 if (TREE_CODE (TREE_OPERAND (ref, 1)) == FUNCTION_DECL)
165 return 1;
166 return lvalue_p (TREE_OPERAND (ref, 0))
167 && lvalue_p (TREE_OPERAND (ref, 1));
168 break;
169
170 case COND_EXPR:
171 return (lvalue_p (TREE_OPERAND (ref, 1))
172 && lvalue_p (TREE_OPERAND (ref, 2)));
173
174 case MODIFY_EXPR:
175 return 1;
176
177 case COMPOUND_EXPR:
178 return lvalue_p (TREE_OPERAND (ref, 1));
e1cd6e56
MS
179
180 case MAX_EXPR:
181 case MIN_EXPR:
182 return (lvalue_p (TREE_OPERAND (ref, 0))
183 && lvalue_p (TREE_OPERAND (ref, 1)));
39211cd5 184 }
a292b002 185
8d08fdba
MS
186 return 0;
187}
188
189/* Return nonzero if REF is an lvalue valid for this language;
190 otherwise, print an error message and return zero. */
191
192int
193lvalue_or_else (ref, string)
194 tree ref;
195 char *string;
196{
197 int win = lvalue_p (ref);
198 if (! win)
2986ae00 199 error ("non-lvalue in %s", string);
8d08fdba
MS
200 return win;
201}
202
203/* INIT is a CALL_EXPR which needs info about its target.
204 TYPE is the type that this initialization should appear to have.
205
206 Build an encapsulation of the initialization to perform
207 and return it so that it can be processed by language-independent
2ee887f2 208 and language-specific expression expanders. */
e92cc029 209
8d08fdba 210tree
5566b478 211build_cplus_new (type, init)
8d08fdba
MS
212 tree type;
213 tree init;
8d08fdba 214{
e8abc66f
MS
215 tree slot;
216 tree rval;
217
218 slot = build (VAR_DECL, type);
219 layout_decl (slot, 0);
220 rval = build (NEW_EXPR, type,
221 TREE_OPERAND (init, 0), TREE_OPERAND (init, 1), slot);
8d08fdba
MS
222 TREE_SIDE_EFFECTS (rval) = 1;
223 TREE_ADDRESSABLE (rval) = 1;
e349ee73 224 rval = build (TARGET_EXPR, type, slot, rval, NULL_TREE, NULL_TREE);
8d08fdba
MS
225 TREE_SIDE_EFFECTS (rval) = 1;
226 TREE_ADDRESSABLE (rval) = 1;
227
8d08fdba
MS
228 return rval;
229}
230
231/* Recursively search EXP for CALL_EXPRs that need cleanups and replace
232 these CALL_EXPRs with tree nodes that will perform the cleanups. */
233
234tree
235break_out_cleanups (exp)
236 tree exp;
237{
238 tree tmp = exp;
239
240 if (TREE_CODE (tmp) == CALL_EXPR
241 && TYPE_NEEDS_DESTRUCTOR (TREE_TYPE (tmp)))
5566b478 242 return build_cplus_new (TREE_TYPE (tmp), tmp);
8d08fdba
MS
243
244 while (TREE_CODE (tmp) == NOP_EXPR
245 || TREE_CODE (tmp) == CONVERT_EXPR
246 || TREE_CODE (tmp) == NON_LVALUE_EXPR)
247 {
248 if (TREE_CODE (TREE_OPERAND (tmp, 0)) == CALL_EXPR
249 && TYPE_NEEDS_DESTRUCTOR (TREE_TYPE (TREE_OPERAND (tmp, 0))))
250 {
251 TREE_OPERAND (tmp, 0)
252 = build_cplus_new (TREE_TYPE (TREE_OPERAND (tmp, 0)),
5566b478 253 TREE_OPERAND (tmp, 0));
8d08fdba
MS
254 break;
255 }
256 else
257 tmp = TREE_OPERAND (tmp, 0);
258 }
259 return exp;
260}
261
262/* Recursively perform a preorder search EXP for CALL_EXPRs, making
263 copies where they are found. Returns a deep copy all nodes transitively
264 containing CALL_EXPRs. */
265
266tree
267break_out_calls (exp)
268 tree exp;
269{
270 register tree t1, t2;
271 register enum tree_code code;
272 register int changed = 0;
273 register int i;
274
275 if (exp == NULL_TREE)
276 return exp;
277
278 code = TREE_CODE (exp);
279
280 if (code == CALL_EXPR)
281 return copy_node (exp);
282
e92cc029 283 /* Don't try and defeat a save_expr, as it should only be done once. */
8d08fdba
MS
284 if (code == SAVE_EXPR)
285 return exp;
286
287 switch (TREE_CODE_CLASS (code))
288 {
289 default:
290 abort ();
291
292 case 'c': /* a constant */
293 case 't': /* a type node */
294 case 'x': /* something random, like an identifier or an ERROR_MARK. */
295 return exp;
296
297 case 'd': /* A decl node */
f376e137
MS
298#if 0 /* This is bogus. jason 9/21/94 */
299
8d08fdba
MS
300 t1 = break_out_calls (DECL_INITIAL (exp));
301 if (t1 != DECL_INITIAL (exp))
302 {
303 exp = copy_node (exp);
304 DECL_INITIAL (exp) = t1;
305 }
f376e137 306#endif
8d08fdba
MS
307 return exp;
308
309 case 'b': /* A block node */
310 {
311 /* Don't know how to handle these correctly yet. Must do a
312 break_out_calls on all DECL_INITIAL values for local variables,
313 and also break_out_calls on all sub-blocks and sub-statements. */
314 abort ();
315 }
316 return exp;
317
318 case 'e': /* an expression */
319 case 'r': /* a reference */
320 case 's': /* an expression with side effects */
321 for (i = tree_code_length[(int) code] - 1; i >= 0; i--)
322 {
323 t1 = break_out_calls (TREE_OPERAND (exp, i));
324 if (t1 != TREE_OPERAND (exp, i))
325 {
326 exp = copy_node (exp);
327 TREE_OPERAND (exp, i) = t1;
328 }
329 }
330 return exp;
331
332 case '<': /* a comparison expression */
333 case '2': /* a binary arithmetic expression */
334 t2 = break_out_calls (TREE_OPERAND (exp, 1));
335 if (t2 != TREE_OPERAND (exp, 1))
336 changed = 1;
337 case '1': /* a unary arithmetic expression */
338 t1 = break_out_calls (TREE_OPERAND (exp, 0));
339 if (t1 != TREE_OPERAND (exp, 0))
340 changed = 1;
341 if (changed)
342 {
343 if (tree_code_length[(int) code] == 1)
344 return build1 (code, TREE_TYPE (exp), t1);
345 else
346 return build (code, TREE_TYPE (exp), t1, t2);
347 }
348 return exp;
349 }
350
351}
352\f
353extern struct obstack *current_obstack;
354extern struct obstack permanent_obstack, class_obstack;
355extern struct obstack *saveable_obstack;
356
357/* Here is how primitive or already-canonicalized types' hash
358 codes are made. MUST BE CONSISTENT WITH tree.c !!! */
359#define TYPE_HASH(TYPE) ((HOST_WIDE_INT) (TYPE) & 0777777)
360
361/* Construct, lay out and return the type of methods belonging to class
362 BASETYPE and whose arguments are described by ARGTYPES and whose values
363 are described by RETTYPE. If each type exists already, reuse it. */
e92cc029 364
8d08fdba
MS
365tree
366build_cplus_method_type (basetype, rettype, argtypes)
367 tree basetype, rettype, argtypes;
368{
369 register tree t;
370 tree ptype;
371 int hashcode;
372
373 /* Make a node of the sort we want. */
374 t = make_node (METHOD_TYPE);
375
376 TYPE_METHOD_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
377 TREE_TYPE (t) = rettype;
378 if (IS_SIGNATURE (basetype))
379 ptype = build_signature_pointer_type (TYPE_MAIN_VARIANT (basetype),
380 TYPE_READONLY (basetype),
381 TYPE_VOLATILE (basetype));
382 else
863adfc0
MS
383 ptype = build_pointer_type (basetype);
384
8d08fdba
MS
385 /* The actual arglist for this function includes a "hidden" argument
386 which is "this". Put it into the list of argument types. */
387
388 argtypes = tree_cons (NULL_TREE, ptype, argtypes);
389 TYPE_ARG_TYPES (t) = argtypes;
390 TREE_SIDE_EFFECTS (argtypes) = 1; /* Mark first argtype as "artificial". */
391
392 /* If we already have such a type, use the old one and free this one.
393 Note that it also frees up the above cons cell if found. */
394 hashcode = TYPE_HASH (basetype) + TYPE_HASH (rettype) + type_hash_list (argtypes);
395 t = type_hash_canon (hashcode, t);
396
397 if (TYPE_SIZE (t) == 0)
398 layout_type (t);
399
400 return t;
401}
402
8d08fdba 403tree
e349ee73 404build_cplus_array_type_1 (elt_type, index_type)
8d08fdba
MS
405 tree elt_type;
406 tree index_type;
407{
408 register struct obstack *ambient_obstack = current_obstack;
409 register struct obstack *ambient_saveable_obstack = saveable_obstack;
410 tree t;
411
412 /* We need a new one. If both ELT_TYPE and INDEX_TYPE are permanent,
413 make this permanent too. */
414 if (TREE_PERMANENT (elt_type)
415 && (index_type == 0 || TREE_PERMANENT (index_type)))
416 {
417 current_obstack = &permanent_obstack;
418 saveable_obstack = &permanent_obstack;
419 }
420
5566b478
MS
421 if (current_template_parms)
422 {
423 t = make_node (ARRAY_TYPE);
424 TREE_TYPE (t) = elt_type;
425 TYPE_DOMAIN (t) = index_type;
426 }
427 else
428 t = build_array_type (elt_type, index_type);
8d08fdba
MS
429
430 /* Push these needs up so that initialization takes place
431 more easily. */
432 TYPE_NEEDS_CONSTRUCTING (t) = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (elt_type));
433 TYPE_NEEDS_DESTRUCTOR (t) = TYPE_NEEDS_DESTRUCTOR (TYPE_MAIN_VARIANT (elt_type));
434 current_obstack = ambient_obstack;
435 saveable_obstack = ambient_saveable_obstack;
436 return t;
437}
e349ee73
MS
438
439tree
440build_cplus_array_type (elt_type, index_type)
441 tree elt_type;
442 tree index_type;
443{
444 tree t;
445 int constp = TYPE_READONLY (elt_type);
446 int volatilep = TYPE_VOLATILE (elt_type);
447 elt_type = TYPE_MAIN_VARIANT (elt_type);
448
449 t = build_cplus_array_type_1 (elt_type, index_type);
450
451 if (constp || volatilep)
452 t = cp_build_type_variant (t, constp, volatilep);
453
454 return t;
455}
8d08fdba 456\f
f376e137
MS
457/* Make a variant type in the proper way for C/C++, propagating qualifiers
458 down to the element type of an array. */
459
460tree
461cp_build_type_variant (type, constp, volatilep)
462 tree type;
463 int constp, volatilep;
464{
e76a2646
MS
465 if (type == error_mark_node)
466 return type;
467
f376e137
MS
468 if (TREE_CODE (type) == ARRAY_TYPE)
469 {
470 tree real_main_variant = TYPE_MAIN_VARIANT (type);
471
472 push_obstacks (TYPE_OBSTACK (real_main_variant),
473 TYPE_OBSTACK (real_main_variant));
e349ee73
MS
474 type = build_cplus_array_type_1 (cp_build_type_variant
475 (TREE_TYPE (type), constp, volatilep),
476 TYPE_DOMAIN (type));
f376e137
MS
477
478 /* TYPE must be on same obstack as REAL_MAIN_VARIANT. If not,
479 make a copy. (TYPE might have come from the hash table and
480 REAL_MAIN_VARIANT might be in some function's obstack.) */
481
482 if (TYPE_OBSTACK (type) != TYPE_OBSTACK (real_main_variant))
483 {
484 type = copy_node (type);
485 TYPE_POINTER_TO (type) = TYPE_REFERENCE_TO (type) = 0;
486 }
487
488 TYPE_MAIN_VARIANT (type) = real_main_variant;
489 pop_obstacks ();
e349ee73 490 return type;
f376e137
MS
491 }
492 return build_type_variant (type, constp, volatilep);
493}
494\f
8d08fdba
MS
495/* Add OFFSET to all base types of T.
496
497 OFFSET, which is a type offset, is number of bytes.
498
499 Note that we don't have to worry about having two paths to the
500 same base type, since this type owns its association list. */
e92cc029 501
8d08fdba
MS
502void
503propagate_binfo_offsets (binfo, offset)
504 tree binfo;
505 tree offset;
506{
507 tree binfos = BINFO_BASETYPES (binfo);
508 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
509
510 for (i = 0; i < n_baselinks; /* note increment is done in the loop. */)
511 {
512 tree base_binfo = TREE_VEC_ELT (binfos, i);
513
514 if (TREE_VIA_VIRTUAL (base_binfo))
515 i += 1;
516 else
517 {
518 int j;
519 tree base_binfos = BINFO_BASETYPES (base_binfo);
520 tree delta;
521
522 for (j = i+1; j < n_baselinks; j++)
523 if (! TREE_VIA_VIRTUAL (TREE_VEC_ELT (binfos, j)))
524 {
525 /* The next basetype offset must take into account the space
526 between the classes, not just the size of each class. */
527 delta = size_binop (MINUS_EXPR,
528 BINFO_OFFSET (TREE_VEC_ELT (binfos, j)),
529 BINFO_OFFSET (base_binfo));
530 break;
531 }
532
533#if 0
534 if (BINFO_OFFSET_ZEROP (base_binfo))
535 BINFO_OFFSET (base_binfo) = offset;
536 else
537 BINFO_OFFSET (base_binfo)
538 = size_binop (PLUS_EXPR, BINFO_OFFSET (base_binfo), offset);
539#else
540 BINFO_OFFSET (base_binfo) = offset;
541#endif
542 if (base_binfos)
543 {
544 int k;
545 tree chain = NULL_TREE;
546
547 /* Now unshare the structure beneath BASE_BINFO. */
548 for (k = TREE_VEC_LENGTH (base_binfos)-1;
549 k >= 0; k--)
550 {
551 tree base_base_binfo = TREE_VEC_ELT (base_binfos, k);
552 if (! TREE_VIA_VIRTUAL (base_base_binfo))
553 TREE_VEC_ELT (base_binfos, k)
554 = make_binfo (BINFO_OFFSET (base_base_binfo),
8926095f 555 base_base_binfo,
8d08fdba
MS
556 BINFO_VTABLE (base_base_binfo),
557 BINFO_VIRTUALS (base_base_binfo),
558 chain);
559 chain = TREE_VEC_ELT (base_binfos, k);
560 TREE_VIA_PUBLIC (chain) = TREE_VIA_PUBLIC (base_base_binfo);
561 TREE_VIA_PROTECTED (chain) = TREE_VIA_PROTECTED (base_base_binfo);
8ccc31eb 562 BINFO_INHERITANCE_CHAIN (chain) = base_binfo;
8d08fdba
MS
563 }
564 /* Now propagate the offset to the base types. */
565 propagate_binfo_offsets (base_binfo, offset);
566 }
567
568 /* Go to our next class that counts for offset propagation. */
569 i = j;
570 if (i < n_baselinks)
571 offset = size_binop (PLUS_EXPR, offset, delta);
572 }
573 }
574}
575
576/* Compute the actual offsets that our virtual base classes
577 will have *for this type*. This must be performed after
578 the fields are laid out, since virtual baseclasses must
579 lay down at the end of the record.
580
581 Returns the maximum number of virtual functions any of the virtual
582 baseclasses provide. */
e92cc029 583
8d08fdba
MS
584int
585layout_vbasetypes (rec, max)
586 tree rec;
587 int max;
588{
589 /* Get all the virtual base types that this type uses.
590 The TREE_VALUE slot holds the virtual baseclass type. */
591 tree vbase_types = get_vbase_types (rec);
592
593#ifdef STRUCTURE_SIZE_BOUNDARY
594 unsigned record_align = MAX (STRUCTURE_SIZE_BOUNDARY, TYPE_ALIGN (rec));
595#else
596 unsigned record_align = MAX (BITS_PER_UNIT, TYPE_ALIGN (rec));
597#endif
f0e01782 598 int desired_align;
8d08fdba
MS
599
600 /* Record size so far is CONST_SIZE + VAR_SIZE bits,
601 where CONST_SIZE is an integer
602 and VAR_SIZE is a tree expression.
603 If VAR_SIZE is null, the size is just CONST_SIZE.
604 Naturally we try to avoid using VAR_SIZE. */
605 register unsigned const_size = 0;
606 register tree var_size = 0;
607 int nonvirtual_const_size;
8d08fdba
MS
608
609 CLASSTYPE_VBASECLASSES (rec) = vbase_types;
610
611 if (TREE_CODE (TYPE_SIZE (rec)) == INTEGER_CST)
612 const_size = TREE_INT_CST_LOW (TYPE_SIZE (rec));
613 else
614 var_size = TYPE_SIZE (rec);
615
616 nonvirtual_const_size = const_size;
8d08fdba
MS
617
618 while (vbase_types)
619 {
620 tree basetype = BINFO_TYPE (vbase_types);
621 tree offset;
622
f0e01782
MS
623 desired_align = TYPE_ALIGN (basetype);
624 record_align = MAX (record_align, desired_align);
625
8d08fdba
MS
626 if (const_size == 0)
627 offset = integer_zero_node;
628 else
f0e01782
MS
629 {
630 /* Give each virtual base type the alignment it wants. */
631 const_size = CEIL (const_size, TYPE_ALIGN (basetype))
632 * TYPE_ALIGN (basetype);
633 offset = size_int (CEIL (const_size, BITS_PER_UNIT));
634 }
8d08fdba
MS
635
636 if (CLASSTYPE_VSIZE (basetype) > max)
637 max = CLASSTYPE_VSIZE (basetype);
638 BINFO_OFFSET (vbase_types) = offset;
639
640 if (TREE_CODE (TYPE_SIZE (basetype)) == INTEGER_CST)
e1cd6e56
MS
641 {
642 /* Every virtual baseclass takes a least a UNIT, so that we can
643 take it's address and get something different for each base. */
644 const_size += MAX (BITS_PER_UNIT,
645 TREE_INT_CST_LOW (TYPE_SIZE (basetype))
646 - TREE_INT_CST_LOW (CLASSTYPE_VBASE_SIZE (basetype)));
647 }
8d08fdba
MS
648 else if (var_size == 0)
649 var_size = TYPE_SIZE (basetype);
650 else
651 var_size = size_binop (PLUS_EXPR, var_size, TYPE_SIZE (basetype));
652
653 vbase_types = TREE_CHAIN (vbase_types);
654 }
655
e1cd6e56
MS
656 if (const_size)
657 {
658 /* Because a virtual base might take a single byte above,
659 we have to re-adjust the total size to make sure it it
660 a multiple of the alignment. */
661 /* Give the whole object the alignment it wants. */
662 const_size = CEIL (const_size, record_align) * record_align;
663 }
664
f0e01782
MS
665 /* Set the alignment in the complete type. We don't set CLASSTYPE_ALIGN
666 here, as that is for this class, without any virtual base classes. */
667 TYPE_ALIGN (rec) = record_align;
8d08fdba
MS
668 if (const_size != nonvirtual_const_size)
669 {
670 CLASSTYPE_VBASE_SIZE (rec)
671 = size_int (const_size - nonvirtual_const_size);
672 TYPE_SIZE (rec) = size_int (const_size);
673 }
674
675 /* Now propagate offset information throughout the lattice
676 under the vbase type. */
677 for (vbase_types = CLASSTYPE_VBASECLASSES (rec); vbase_types;
678 vbase_types = TREE_CHAIN (vbase_types))
679 {
680 tree base_binfos = BINFO_BASETYPES (vbase_types);
681
8ccc31eb
MS
682 BINFO_INHERITANCE_CHAIN (vbase_types) = TYPE_BINFO (rec);
683
8d08fdba
MS
684 if (base_binfos)
685 {
686 tree chain = NULL_TREE;
687 int j;
688 /* Now unshare the structure beneath BASE_BINFO. */
689
690 for (j = TREE_VEC_LENGTH (base_binfos)-1;
691 j >= 0; j--)
692 {
693 tree base_base_binfo = TREE_VEC_ELT (base_binfos, j);
694 if (! TREE_VIA_VIRTUAL (base_base_binfo))
695 TREE_VEC_ELT (base_binfos, j)
696 = make_binfo (BINFO_OFFSET (base_base_binfo),
8926095f 697 base_base_binfo,
8d08fdba
MS
698 BINFO_VTABLE (base_base_binfo),
699 BINFO_VIRTUALS (base_base_binfo),
700 chain);
701 chain = TREE_VEC_ELT (base_binfos, j);
702 TREE_VIA_PUBLIC (chain) = TREE_VIA_PUBLIC (base_base_binfo);
703 TREE_VIA_PROTECTED (chain) = TREE_VIA_PROTECTED (base_base_binfo);
8ccc31eb 704 BINFO_INHERITANCE_CHAIN (chain) = vbase_types;
8d08fdba
MS
705 }
706
707 propagate_binfo_offsets (vbase_types, BINFO_OFFSET (vbase_types));
708 }
709 }
710
711 return max;
712}
713
714/* Lay out the base types of a record type, REC.
715 Tentatively set the size and alignment of REC
716 according to the base types alone.
717
718 Offsets for immediate nonvirtual baseclasses are also computed here.
719
7177d104
MS
720 TYPE_BINFO (REC) should be NULL_TREE on entry, and this routine
721 creates a list of base_binfos in TYPE_BINFO (REC) from BINFOS.
722
8d08fdba 723 Returns list of virtual base classes in a FIELD_DECL chain. */
e92cc029 724
8d08fdba
MS
725tree
726layout_basetypes (rec, binfos)
727 tree rec, binfos;
728{
729 /* Chain to hold all the new FIELD_DECLs which point at virtual
730 base classes. */
731 tree vbase_decls = NULL_TREE;
732
733#ifdef STRUCTURE_SIZE_BOUNDARY
734 unsigned record_align = MAX (STRUCTURE_SIZE_BOUNDARY, TYPE_ALIGN (rec));
735#else
736 unsigned record_align = MAX (BITS_PER_UNIT, TYPE_ALIGN (rec));
737#endif
738
8926095f
MS
739 /* Record size so far is CONST_SIZE + VAR_SIZE bits, where CONST_SIZE is
740 an integer and VAR_SIZE is a tree expression. If VAR_SIZE is null,
741 the size is just CONST_SIZE. Naturally we try to avoid using
e92cc029 742 VAR_SIZE. And so far, we've been successful. */
8926095f 743#if 0
8d08fdba 744 register tree var_size = 0;
8926095f
MS
745#endif
746
747 register unsigned const_size = 0;
8d08fdba
MS
748 int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
749
750 /* Handle basetypes almost like fields, but record their
751 offsets differently. */
752
753 for (i = 0; i < n_baseclasses; i++)
754 {
755 int inc, desired_align, int_vbase_size;
756 register tree base_binfo = TREE_VEC_ELT (binfos, i);
757 register tree basetype = BINFO_TYPE (base_binfo);
758 tree decl, offset;
759
760 if (TYPE_SIZE (basetype) == 0)
761 {
762#if 0
763 /* This error is now reported in xref_tag, thus giving better
764 location information. */
765 error_with_aggr_type (base_binfo,
766 "base class `%s' has incomplete type");
767
768 TREE_VIA_PUBLIC (base_binfo) = 1;
769 TREE_VIA_PROTECTED (base_binfo) = 0;
770 TREE_VIA_VIRTUAL (base_binfo) = 0;
771
772 /* Should handle this better so that
773
774 class A;
775 class B: private A { virtual void F(); };
776
e92cc029 777 does not dump core when compiled. */
8d08fdba
MS
778 my_friendly_abort (121);
779#endif
780 continue;
781 }
782
783 /* All basetypes are recorded in the association list of the
784 derived type. */
785
786 if (TREE_VIA_VIRTUAL (base_binfo))
787 {
788 int j;
789 char *name = (char *)alloca (TYPE_NAME_LENGTH (basetype)
790 + sizeof (VBASE_NAME) + 1);
791
792 /* The offset for a virtual base class is only used in computing
793 virtual function tables and for initializing virtual base
794 pointers. It is built once `get_vbase_types' is called. */
795
796 /* If this basetype can come from another vbase pointer
797 without an additional indirection, we will share
798 that pointer. If an indirection is involved, we
799 make our own pointer. */
800 for (j = 0; j < n_baseclasses; j++)
801 {
802 tree other_base_binfo = TREE_VEC_ELT (binfos, j);
803 if (! TREE_VIA_VIRTUAL (other_base_binfo)
804 && binfo_member (basetype,
805 CLASSTYPE_VBASECLASSES (BINFO_TYPE (other_base_binfo))))
806 goto got_it;
807 }
808 sprintf (name, VBASE_NAME_FORMAT, TYPE_NAME_STRING (basetype));
be99da77
MS
809 decl = build_lang_field_decl (FIELD_DECL, get_identifier (name),
810 build_pointer_type (basetype));
8d08fdba
MS
811 /* If you change any of the below, take a look at all the
812 other VFIELD_BASEs and VTABLE_BASEs in the code, and change
e92cc029 813 them too. */
8d08fdba
MS
814 DECL_ASSEMBLER_NAME (decl) = get_identifier (VTABLE_BASE);
815 DECL_VIRTUAL_P (decl) = 1;
816 DECL_FIELD_CONTEXT (decl) = rec;
817 DECL_CLASS_CONTEXT (decl) = rec;
818 DECL_FCONTEXT (decl) = basetype;
28cbf42c 819 DECL_SAVED_INSNS (decl) = NULL_RTX;
8d08fdba
MS
820 DECL_FIELD_SIZE (decl) = 0;
821 DECL_ALIGN (decl) = TYPE_ALIGN (ptr_type_node);
822 TREE_CHAIN (decl) = vbase_decls;
823 BINFO_VPTR_FIELD (base_binfo) = decl;
824 vbase_decls = decl;
825
826 if (warn_nonvdtor && TYPE_HAS_DESTRUCTOR (basetype)
fc378698 827 && DECL_VINDEX (TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (basetype), 1)) == NULL_TREE)
8d08fdba 828 {
fc378698 829 warning_with_decl (TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (basetype), 1),
8d08fdba
MS
830 "destructor `%s' non-virtual");
831 warning ("in inheritance relationship `%s: virtual %s'",
832 TYPE_NAME_STRING (rec),
833 TYPE_NAME_STRING (basetype));
834 }
835 got_it:
836 /* The space this decl occupies has already been accounted for. */
837 continue;
838 }
839
840 if (const_size == 0)
841 offset = integer_zero_node;
842 else
843 {
844 /* Give each base type the alignment it wants. */
845 const_size = CEIL (const_size, TYPE_ALIGN (basetype))
846 * TYPE_ALIGN (basetype);
847 offset = size_int ((const_size + BITS_PER_UNIT - 1) / BITS_PER_UNIT);
848
849#if 0
850 /* bpk: Disabled this check until someone is willing to
851 claim it as theirs and explain exactly what circumstances
852 warrant the warning. */
853 if (warn_nonvdtor && TYPE_HAS_DESTRUCTOR (basetype)
fc378698 854 && DECL_VINDEX (TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (basetype), 1)) == NULL_TREE)
8d08fdba 855 {
fc378698 856 warning_with_decl (TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (basetype), 1),
8d08fdba
MS
857 "destructor `%s' non-virtual");
858 warning ("in inheritance relationship `%s:%s %s'",
859 TYPE_NAME_STRING (rec),
860 TREE_VIA_VIRTUAL (base_binfo) ? " virtual" : "",
861 TYPE_NAME_STRING (basetype));
862 }
863#endif
864 }
865 BINFO_OFFSET (base_binfo) = offset;
866 if (CLASSTYPE_VSIZE (basetype))
867 {
868 BINFO_VTABLE (base_binfo) = TYPE_BINFO_VTABLE (basetype);
869 BINFO_VIRTUALS (base_binfo) = TYPE_BINFO_VIRTUALS (basetype);
870 }
871 TREE_CHAIN (base_binfo) = TYPE_BINFO (rec);
872 TYPE_BINFO (rec) = base_binfo;
873
874 /* Add only the amount of storage not present in
875 the virtual baseclasses. */
876
877 int_vbase_size = TREE_INT_CST_LOW (CLASSTYPE_VBASE_SIZE (basetype));
878 if (TREE_INT_CST_LOW (TYPE_SIZE (basetype)) > int_vbase_size)
879 {
880 inc = MAX (record_align,
881 (TREE_INT_CST_LOW (TYPE_SIZE (basetype))
882 - int_vbase_size));
883
884 /* Record must have at least as much alignment as any field. */
885 desired_align = TYPE_ALIGN (basetype);
886 record_align = MAX (record_align, desired_align);
887
888 const_size += inc;
889 }
890 }
891
892 if (const_size)
893 CLASSTYPE_SIZE (rec) = size_int (const_size);
894 else
895 CLASSTYPE_SIZE (rec) = integer_zero_node;
896 CLASSTYPE_ALIGN (rec) = record_align;
897
898 return vbase_decls;
899}
900\f
901/* Hashing of lists so that we don't make duplicates.
902 The entry point is `list_hash_canon'. */
903
904/* Each hash table slot is a bucket containing a chain
905 of these structures. */
906
907struct list_hash
908{
909 struct list_hash *next; /* Next structure in the bucket. */
910 int hashcode; /* Hash code of this list. */
911 tree list; /* The list recorded here. */
912};
913
914/* Now here is the hash table. When recording a list, it is added
915 to the slot whose index is the hash code mod the table size.
916 Note that the hash table is used for several kinds of lists.
917 While all these live in the same table, they are completely independent,
918 and the hash code is computed differently for each of these. */
919
920#define TYPE_HASH_SIZE 59
921struct list_hash *list_hash_table[TYPE_HASH_SIZE];
922
923/* Compute a hash code for a list (chain of TREE_LIST nodes
924 with goodies in the TREE_PURPOSE, TREE_VALUE, and bits of the
925 TREE_COMMON slots), by adding the hash codes of the individual entries. */
926
927int
928list_hash (list)
929 tree list;
930{
931 register int hashcode = 0;
932
933 if (TREE_CHAIN (list))
934 hashcode += TYPE_HASH (TREE_CHAIN (list));
935
936 if (TREE_VALUE (list))
937 hashcode += TYPE_HASH (TREE_VALUE (list));
938 else
939 hashcode += 1007;
940 if (TREE_PURPOSE (list))
941 hashcode += TYPE_HASH (TREE_PURPOSE (list));
942 else
943 hashcode += 1009;
944 return hashcode;
945}
946
947/* Look in the type hash table for a type isomorphic to TYPE.
948 If one is found, return it. Otherwise return 0. */
949
950tree
951list_hash_lookup (hashcode, list)
952 int hashcode;
953 tree list;
954{
955 register struct list_hash *h;
956 for (h = list_hash_table[hashcode % TYPE_HASH_SIZE]; h; h = h->next)
957 if (h->hashcode == hashcode
958 && TREE_VIA_VIRTUAL (h->list) == TREE_VIA_VIRTUAL (list)
959 && TREE_VIA_PUBLIC (h->list) == TREE_VIA_PUBLIC (list)
960 && TREE_VIA_PROTECTED (h->list) == TREE_VIA_PROTECTED (list)
961 && TREE_PURPOSE (h->list) == TREE_PURPOSE (list)
962 && TREE_VALUE (h->list) == TREE_VALUE (list)
963 && TREE_CHAIN (h->list) == TREE_CHAIN (list))
964 {
965 my_friendly_assert (TREE_TYPE (h->list) == TREE_TYPE (list), 299);
966 return h->list;
967 }
968 return 0;
969}
970
971/* Add an entry to the list-hash-table
972 for a list TYPE whose hash code is HASHCODE. */
973
974void
975list_hash_add (hashcode, list)
976 int hashcode;
977 tree list;
978{
979 register struct list_hash *h;
980
981 h = (struct list_hash *) obstack_alloc (&class_obstack, sizeof (struct list_hash));
982 h->hashcode = hashcode;
983 h->list = list;
984 h->next = list_hash_table[hashcode % TYPE_HASH_SIZE];
985 list_hash_table[hashcode % TYPE_HASH_SIZE] = h;
986}
987
988/* Given TYPE, and HASHCODE its hash code, return the canonical
989 object for an identical list if one already exists.
990 Otherwise, return TYPE, and record it as the canonical object
991 if it is a permanent object.
992
993 To use this function, first create a list of the sort you want.
994 Then compute its hash code from the fields of the list that
995 make it different from other similar lists.
996 Then call this function and use the value.
997 This function frees the list you pass in if it is a duplicate. */
998
999/* Set to 1 to debug without canonicalization. Never set by program. */
e92cc029 1000
a0a33927 1001static int debug_no_list_hash = 0;
8d08fdba
MS
1002
1003tree
1004list_hash_canon (hashcode, list)
1005 int hashcode;
1006 tree list;
1007{
1008 tree t1;
1009
1010 if (debug_no_list_hash)
1011 return list;
1012
1013 t1 = list_hash_lookup (hashcode, list);
1014 if (t1 != 0)
1015 {
1016 obstack_free (&class_obstack, list);
1017 return t1;
1018 }
1019
1020 /* If this is a new list, record it for later reuse. */
1021 list_hash_add (hashcode, list);
1022
1023 return list;
1024}
1025
1026tree
1027hash_tree_cons (via_public, via_virtual, via_protected, purpose, value, chain)
1028 int via_public, via_virtual, via_protected;
1029 tree purpose, value, chain;
1030{
1031 struct obstack *ambient_obstack = current_obstack;
1032 tree t;
1033 int hashcode;
1034
1035 current_obstack = &class_obstack;
1036 t = tree_cons (purpose, value, chain);
1037 TREE_VIA_PUBLIC (t) = via_public;
1038 TREE_VIA_PROTECTED (t) = via_protected;
1039 TREE_VIA_VIRTUAL (t) = via_virtual;
1040 hashcode = list_hash (t);
1041 t = list_hash_canon (hashcode, t);
1042 current_obstack = ambient_obstack;
1043 return t;
1044}
1045
1046/* Constructor for hashed lists. */
e92cc029 1047
8d08fdba
MS
1048tree
1049hash_tree_chain (value, chain)
1050 tree value, chain;
1051{
1052 struct obstack *ambient_obstack = current_obstack;
1053 tree t;
1054 int hashcode;
1055
1056 current_obstack = &class_obstack;
1057 t = tree_cons (NULL_TREE, value, chain);
1058 hashcode = list_hash (t);
1059 t = list_hash_canon (hashcode, t);
1060 current_obstack = ambient_obstack;
1061 return t;
1062}
1063
1064/* Similar, but used for concatenating two lists. */
e92cc029 1065
8d08fdba
MS
1066tree
1067hash_chainon (list1, list2)
1068 tree list1, list2;
1069{
1070 if (list2 == 0)
1071 return list1;
1072 if (list1 == 0)
1073 return list2;
1074 if (TREE_CHAIN (list1) == NULL_TREE)
1075 return hash_tree_chain (TREE_VALUE (list1), list2);
1076 return hash_tree_chain (TREE_VALUE (list1),
1077 hash_chainon (TREE_CHAIN (list1), list2));
1078}
1079
51c184be
MS
1080static tree
1081get_identifier_list (value)
8d08fdba
MS
1082 tree value;
1083{
51c184be
MS
1084 tree list = IDENTIFIER_AS_LIST (value);
1085 if (list != NULL_TREE
1086 && (TREE_CODE (list) != TREE_LIST
1087 || TREE_VALUE (list) != value))
1088 list = NULL_TREE;
1089 else if (IDENTIFIER_HAS_TYPE_VALUE (value)
8926095f
MS
1090 && TREE_CODE (IDENTIFIER_TYPE_VALUE (value)) == RECORD_TYPE
1091 && IDENTIFIER_TYPE_VALUE (value)
1092 == TYPE_MAIN_VARIANT (IDENTIFIER_TYPE_VALUE (value)))
8d08fdba 1093 {
51c184be
MS
1094 tree type = IDENTIFIER_TYPE_VALUE (value);
1095
1096 if (TYPE_PTRMEMFUNC_P (type))
8d08fdba 1097 list = NULL_TREE;
51c184be
MS
1098 else if (type == current_class_type)
1099 /* Don't mess up the constructor name. */
1100 list = tree_cons (NULL_TREE, value, NULL_TREE);
1101 else
8d08fdba 1102 {
a80e4195 1103 if (! CLASSTYPE_ID_AS_LIST (type))
51c184be 1104 CLASSTYPE_ID_AS_LIST (type)
a80e4195 1105 = perm_tree_cons (NULL_TREE, TYPE_IDENTIFIER (type), NULL_TREE);
51c184be 1106 list = CLASSTYPE_ID_AS_LIST (type);
8d08fdba
MS
1107 }
1108 }
51c184be
MS
1109 return list;
1110}
1111
1112tree
1113get_decl_list (value)
1114 tree value;
1115{
1116 tree list = NULL_TREE;
1117
1118 if (TREE_CODE (value) == IDENTIFIER_NODE)
1119 list = get_identifier_list (value);
8d08fdba 1120 else if (TREE_CODE (value) == RECORD_TYPE
be99da77
MS
1121 && TYPE_LANG_SPECIFIC (value)
1122 && value == TYPE_MAIN_VARIANT (value))
8d08fdba
MS
1123 list = CLASSTYPE_AS_LIST (value);
1124
1125 if (list != NULL_TREE)
1126 {
1127 my_friendly_assert (TREE_CHAIN (list) == NULL_TREE, 301);
1128 return list;
1129 }
1130
1131 return build_decl_list (NULL_TREE, value);
1132}
8d08fdba
MS
1133\f
1134/* Build an association between TYPE and some parameters:
1135
1136 OFFSET is the offset added to `this' to convert it to a pointer
1137 of type `TYPE *'
1138
8926095f
MS
1139 BINFO is the base binfo to use, if we are deriving from one. This
1140 is necessary, as we want specialized parent binfos from base
1141 classes, so that the VTABLE_NAMEs of bases are for the most derived
1142 type, instead of of the simple type.
1143
8d08fdba
MS
1144 VTABLE is the virtual function table with which to initialize
1145 sub-objects of type TYPE.
1146
1147 VIRTUALS are the virtual functions sitting in VTABLE.
1148
1149 CHAIN are more associations we must retain. */
1150
1151tree
8926095f
MS
1152make_binfo (offset, binfo, vtable, virtuals, chain)
1153 tree offset, binfo;
8d08fdba
MS
1154 tree vtable, virtuals;
1155 tree chain;
1156{
8926095f
MS
1157 tree new_binfo = make_tree_vec (6);
1158 tree type;
8d08fdba 1159
8926095f
MS
1160 if (TREE_CODE (binfo) == TREE_VEC)
1161 type = BINFO_TYPE (binfo);
1162 else
1163 {
1164 type = binfo;
1165 binfo = TYPE_BINFO (binfo);
1166 }
8d08fdba 1167
8926095f
MS
1168 TREE_CHAIN (new_binfo) = chain;
1169 if (chain)
1170 TREE_USED (new_binfo) = TREE_USED (chain);
8d08fdba 1171
8926095f
MS
1172 TREE_TYPE (new_binfo) = TYPE_MAIN_VARIANT (type);
1173 BINFO_OFFSET (new_binfo) = offset;
1174 BINFO_VTABLE (new_binfo) = vtable;
1175 BINFO_VIRTUALS (new_binfo) = virtuals;
1176 BINFO_VPTR_FIELD (new_binfo) = NULL_TREE;
8d08fdba 1177
8926095f
MS
1178 if (binfo && BINFO_BASETYPES (binfo) != NULL_TREE)
1179 BINFO_BASETYPES (new_binfo) = copy_node (BINFO_BASETYPES (binfo));
1180 return new_binfo;
8d08fdba
MS
1181}
1182
8d08fdba
MS
1183/* Return the binfo value for ELEM in TYPE. */
1184
1185tree
1186binfo_value (elem, type)
1187 tree elem;
1188 tree type;
1189{
1190 if (get_base_distance (elem, type, 0, (tree *)0) == -2)
1191 compiler_error ("base class `%s' ambiguous in binfo_value",
1192 TYPE_NAME_STRING (elem));
1193 if (elem == type)
1194 return TYPE_BINFO (type);
1195 if (TREE_CODE (elem) == RECORD_TYPE && TYPE_BINFO (elem) == type)
1196 return type;
1197 return get_binfo (elem, type, 0);
1198}
1199
1200tree
1201reverse_path (path)
1202 tree path;
1203{
1204 register tree prev = 0, tmp, next;
1205 for (tmp = path; tmp; tmp = next)
1206 {
1207 next = BINFO_INHERITANCE_CHAIN (tmp);
1208 BINFO_INHERITANCE_CHAIN (tmp) = prev;
1209 prev = tmp;
1210 }
1211 return prev;
1212}
1213
8d08fdba
MS
1214void
1215debug_binfo (elem)
1216 tree elem;
1217{
f30432d7 1218 unsigned HOST_WIDE_INT n;
8d08fdba
MS
1219 tree virtuals;
1220
1221 fprintf (stderr, "type \"%s\"; offset = %d\n",
1222 TYPE_NAME_STRING (BINFO_TYPE (elem)),
1223 TREE_INT_CST_LOW (BINFO_OFFSET (elem)));
1224 fprintf (stderr, "vtable type:\n");
1225 debug_tree (BINFO_TYPE (elem));
1226 if (BINFO_VTABLE (elem))
1227 fprintf (stderr, "vtable decl \"%s\"\n", IDENTIFIER_POINTER (DECL_NAME (BINFO_VTABLE (elem))));
1228 else
1229 fprintf (stderr, "no vtable decl yet\n");
1230 fprintf (stderr, "virtuals:\n");
1231 virtuals = BINFO_VIRTUALS (elem);
f30432d7
MS
1232
1233 n = skip_rtti_stuff (&virtuals);
1234
8d08fdba
MS
1235 while (virtuals)
1236 {
1237 tree fndecl = TREE_OPERAND (FNADDR_FROM_VTABLE_ENTRY (TREE_VALUE (virtuals)), 0);
1238 fprintf (stderr, "%s [%d =? %d]\n",
1239 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (fndecl)),
f30432d7
MS
1240 n, TREE_INT_CST_LOW (DECL_VINDEX (fndecl)));
1241 ++n;
8d08fdba 1242 virtuals = TREE_CHAIN (virtuals);
8d08fdba
MS
1243 }
1244}
1245
1246/* Return the length of a chain of nodes chained through DECL_CHAIN.
1247 We expect a null pointer to mark the end of the chain.
1248 This is the Lisp primitive `length'. */
1249
1250int
1251decl_list_length (t)
1252 tree t;
1253{
1254 register tree tail;
1255 register int len = 0;
1256
1257 my_friendly_assert (TREE_CODE (t) == FUNCTION_DECL
1258 || TREE_CODE (t) == TEMPLATE_DECL, 300);
1259 for (tail = t; tail; tail = DECL_CHAIN (tail))
1260 len++;
1261
1262 return len;
1263}
1264
1265int
1266count_functions (t)
1267 tree t;
1268{
1269 if (TREE_CODE (t) == FUNCTION_DECL)
1270 return 1;
5b605f68
MS
1271 else if (TREE_CODE (t) == TREE_LIST)
1272 return decl_list_length (TREE_VALUE (t));
8d08fdba 1273
5b605f68 1274 my_friendly_abort (359);
0d16d68e 1275 return 0;
8d08fdba
MS
1276}
1277
8d08fdba
MS
1278int
1279is_overloaded_fn (x)
1280 tree x;
1281{
1282 if (TREE_CODE (x) == FUNCTION_DECL)
1283 return 1;
1284
1285 if (TREE_CODE (x) == TREE_LIST
1286 && (TREE_CODE (TREE_VALUE (x)) == FUNCTION_DECL
1287 || TREE_CODE (TREE_VALUE (x)) == TEMPLATE_DECL))
1288 return 1;
1289
1290 return 0;
1291}
1292
8926095f
MS
1293int
1294really_overloaded_fn (x)
1295 tree x;
1296{
1297 if (TREE_CODE (x) == TREE_LIST
1298 && (TREE_CODE (TREE_VALUE (x)) == FUNCTION_DECL
1299 || TREE_CODE (TREE_VALUE (x)) == TEMPLATE_DECL))
1300 return 1;
1301
1302 return 0;
1303}
1304
8d08fdba
MS
1305tree
1306get_first_fn (from)
1307 tree from;
1308{
1309 if (TREE_CODE (from) == FUNCTION_DECL)
1310 return from;
1311
1312 my_friendly_assert (TREE_CODE (from) == TREE_LIST, 9);
1313
1314 return TREE_VALUE (from);
1315}
1316
1317tree
1318fnaddr_from_vtable_entry (entry)
1319 tree entry;
1320{
8926095f
MS
1321 if (flag_vtable_thunks)
1322 {
1323 tree func = entry;
1324 if (TREE_CODE (func) == ADDR_EXPR)
1325 func = TREE_OPERAND (func, 0);
1326 if (TREE_CODE (func) == THUNK_DECL)
1327 return DECL_INITIAL (func);
1328 else
1329 return entry;
1330 }
1331 else
1332 return TREE_VALUE (TREE_CHAIN (TREE_CHAIN (CONSTRUCTOR_ELTS (entry))));
8d08fdba
MS
1333}
1334
8d08fdba
MS
1335tree
1336function_arg_chain (t)
1337 tree t;
1338{
1339 return TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (t)));
1340}
1341
1342int
1343promotes_to_aggr_type (t, code)
1344 tree t;
1345 enum tree_code code;
1346{
1347 if (TREE_CODE (t) == code)
1348 t = TREE_TYPE (t);
1349 return IS_AGGR_TYPE (t);
1350}
1351
1352int
1353is_aggr_type_2 (t1, t2)
1354 tree t1, t2;
1355{
1356 if (TREE_CODE (t1) != TREE_CODE (t2))
1357 return 0;
1358 return IS_AGGR_TYPE (t1) && IS_AGGR_TYPE (t2);
1359}
1360
1361/* Give message using types TYPE1 and TYPE2 as arguments.
1362 PFN is the function which will print the message;
1363 S is the format string for PFN to use. */
e92cc029 1364
8d08fdba
MS
1365void
1366message_2_types (pfn, s, type1, type2)
1367 void (*pfn) ();
1368 char *s;
1369 tree type1, type2;
1370{
1371 tree name1 = TYPE_NAME (type1);
1372 tree name2 = TYPE_NAME (type2);
1373 if (TREE_CODE (name1) == TYPE_DECL)
1374 name1 = DECL_NAME (name1);
1375 if (TREE_CODE (name2) == TYPE_DECL)
1376 name2 = DECL_NAME (name2);
1377 (*pfn) (s, IDENTIFIER_POINTER (name1), IDENTIFIER_POINTER (name2));
1378}
1379\f
1380#define PRINT_RING_SIZE 4
1381
1382char *
1383lang_printable_name (decl)
1384 tree decl;
1385{
1386 static tree decl_ring[PRINT_RING_SIZE];
1387 static char *print_ring[PRINT_RING_SIZE];
1388 static int ring_counter;
1389 int i;
1390
1391 /* Only cache functions. */
1392 if (TREE_CODE (decl) != FUNCTION_DECL
1393 || DECL_LANG_SPECIFIC (decl) == 0)
1394 return decl_as_string (decl, 1);
1395
1396 /* See if this print name is lying around. */
1397 for (i = 0; i < PRINT_RING_SIZE; i++)
1398 if (decl_ring[i] == decl)
1399 /* yes, so return it. */
1400 return print_ring[i];
1401
1402 if (++ring_counter == PRINT_RING_SIZE)
1403 ring_counter = 0;
1404
1405 if (current_function_decl != NULL_TREE)
1406 {
1407 if (decl_ring[ring_counter] == current_function_decl)
1408 ring_counter += 1;
1409 if (ring_counter == PRINT_RING_SIZE)
1410 ring_counter = 0;
1411 if (decl_ring[ring_counter] == current_function_decl)
1412 my_friendly_abort (106);
1413 }
1414
1415 if (print_ring[ring_counter])
1416 free (print_ring[ring_counter]);
1417
1418 {
1419 int print_ret_type_p
1420 = (!DECL_CONSTRUCTOR_P (decl)
1421 && !DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (decl)));
1422
1423 char *name = (char *)decl_as_string (decl, print_ret_type_p);
1424 print_ring[ring_counter] = (char *)malloc (strlen (name) + 1);
1425 strcpy (print_ring[ring_counter], name);
1426 decl_ring[ring_counter] = decl;
1427 }
1428 return print_ring[ring_counter];
1429}
1430\f
f30432d7 1431/* Build the FUNCTION_TYPE or METHOD_TYPE which may throw exceptions
8d08fdba 1432 listed in RAISES. */
e92cc029 1433
8d08fdba 1434tree
f30432d7
MS
1435build_exception_variant (type, raises)
1436 tree type;
8d08fdba
MS
1437 tree raises;
1438{
8d08fdba 1439 tree v = TYPE_MAIN_VARIANT (type);
8d08fdba
MS
1440 int constp = TYPE_READONLY (type);
1441 int volatilep = TYPE_VOLATILE (type);
1442
45537677 1443 for (; v; v = TYPE_NEXT_VARIANT (v))
8d08fdba
MS
1444 {
1445 if (TYPE_READONLY (v) != constp
1446 || TYPE_VOLATILE (v) != volatilep)
1447 continue;
1448
e92cc029 1449 /* @@ This should do set equality, not exact match. */
6060a796
MS
1450 if (simple_cst_list_equal (TYPE_RAISES_EXCEPTIONS (v), raises))
1451 /* List of exceptions raised matches previously found list.
8d08fdba 1452
6060a796
MS
1453 @@ Nice to free up storage used in consing up the
1454 @@ list of exceptions raised. */
1455 return v;
8d08fdba
MS
1456 }
1457
1458 /* Need to build a new variant. */
45537677
MS
1459 v = build_type_copy (type);
1460
8d08fdba
MS
1461 if (raises && ! TREE_PERMANENT (raises))
1462 {
1463 push_obstacks_nochange ();
1464 end_temporary_allocation ();
1465 raises = copy_list (raises);
1466 pop_obstacks ();
1467 }
5566b478 1468
8d08fdba
MS
1469 TYPE_RAISES_EXCEPTIONS (v) = raises;
1470 return v;
1471}
1472
1473/* Subroutine of copy_to_permanent
1474
1475 Assuming T is a node build bottom-up, make it all exist on
1476 permanent obstack, if it is not permanent already. */
878cd289
MS
1477
1478tree
1479mapcar (t, func)
8d08fdba 1480 tree t;
878cd289 1481 tree (*func)();
8d08fdba 1482{
878cd289 1483 tree tmp;
8d08fdba 1484
878cd289 1485 if (t == NULL_TREE)
8d08fdba
MS
1486 return t;
1487
878cd289
MS
1488 if (tmp = func (t), tmp != NULL_TREE)
1489 return tmp;
1490
5566b478 1491 switch (TREE_CODE (t))
8d08fdba
MS
1492 {
1493 case ERROR_MARK:
1494 return error_mark_node;
1495
1496 case VAR_DECL:
1497 case FUNCTION_DECL:
1498 case CONST_DECL:
1499 break;
1500
1501 case PARM_DECL:
1502 {
1503 tree chain = TREE_CHAIN (t);
1504 t = copy_node (t);
878cd289
MS
1505 TREE_CHAIN (t) = mapcar (chain, func);
1506 TREE_TYPE (t) = mapcar (TREE_TYPE (t), func);
1507 DECL_INITIAL (t) = mapcar (DECL_INITIAL (t), func);
1508 DECL_SIZE (t) = mapcar (DECL_SIZE (t), func);
8d08fdba
MS
1509 return t;
1510 }
1511
1512 case TREE_LIST:
1513 {
1514 tree chain = TREE_CHAIN (t);
1515 t = copy_node (t);
878cd289
MS
1516 TREE_PURPOSE (t) = mapcar (TREE_PURPOSE (t), func);
1517 TREE_VALUE (t) = mapcar (TREE_VALUE (t), func);
1518 TREE_CHAIN (t) = mapcar (chain, func);
8d08fdba
MS
1519 return t;
1520 }
1521
1522 case TREE_VEC:
1523 {
1524 int len = TREE_VEC_LENGTH (t);
1525
1526 t = copy_node (t);
1527 while (len--)
878cd289 1528 TREE_VEC_ELT (t, len) = mapcar (TREE_VEC_ELT (t, len), func);
8d08fdba
MS
1529 return t;
1530 }
1531
1532 case INTEGER_CST:
1533 case REAL_CST:
1534 case STRING_CST:
1535 return copy_node (t);
1536
1537 case COND_EXPR:
1538 case TARGET_EXPR:
1539 case NEW_EXPR:
1540 t = copy_node (t);
878cd289
MS
1541 TREE_OPERAND (t, 0) = mapcar (TREE_OPERAND (t, 0), func);
1542 TREE_OPERAND (t, 1) = mapcar (TREE_OPERAND (t, 1), func);
1543 TREE_OPERAND (t, 2) = mapcar (TREE_OPERAND (t, 2), func);
8d08fdba
MS
1544 return t;
1545
1546 case SAVE_EXPR:
1547 t = copy_node (t);
878cd289 1548 TREE_OPERAND (t, 0) = mapcar (TREE_OPERAND (t, 0), func);
8d08fdba
MS
1549 return t;
1550
1551 case MODIFY_EXPR:
1552 case PLUS_EXPR:
1553 case MINUS_EXPR:
1554 case MULT_EXPR:
1555 case TRUNC_DIV_EXPR:
1556 case TRUNC_MOD_EXPR:
1557 case MIN_EXPR:
1558 case MAX_EXPR:
1559 case LSHIFT_EXPR:
1560 case RSHIFT_EXPR:
1561 case BIT_IOR_EXPR:
1562 case BIT_XOR_EXPR:
1563 case BIT_AND_EXPR:
1564 case BIT_ANDTC_EXPR:
1565 case TRUTH_ANDIF_EXPR:
1566 case TRUTH_ORIF_EXPR:
1567 case LT_EXPR:
1568 case LE_EXPR:
1569 case GT_EXPR:
1570 case GE_EXPR:
1571 case EQ_EXPR:
1572 case NE_EXPR:
1573 case CEIL_DIV_EXPR:
1574 case FLOOR_DIV_EXPR:
1575 case ROUND_DIV_EXPR:
1576 case CEIL_MOD_EXPR:
1577 case FLOOR_MOD_EXPR:
1578 case ROUND_MOD_EXPR:
1579 case COMPOUND_EXPR:
1580 case PREDECREMENT_EXPR:
1581 case PREINCREMENT_EXPR:
1582 case POSTDECREMENT_EXPR:
1583 case POSTINCREMENT_EXPR:
1584 case CALL_EXPR:
5566b478
MS
1585 case ARRAY_REF:
1586 case SCOPE_REF:
8d08fdba 1587 t = copy_node (t);
878cd289
MS
1588 TREE_OPERAND (t, 0) = mapcar (TREE_OPERAND (t, 0), func);
1589 TREE_OPERAND (t, 1) = mapcar (TREE_OPERAND (t, 1), func);
8d08fdba
MS
1590 return t;
1591
1592 case CONVERT_EXPR:
1593 case ADDR_EXPR:
1594 case INDIRECT_REF:
1595 case NEGATE_EXPR:
1596 case BIT_NOT_EXPR:
1597 case TRUTH_NOT_EXPR:
1598 case NOP_EXPR:
1599 case COMPONENT_REF:
1600 t = copy_node (t);
878cd289 1601 TREE_OPERAND (t, 0) = mapcar (TREE_OPERAND (t, 0), func);
8d08fdba
MS
1602 return t;
1603
00595019 1604 case POINTER_TYPE:
e76a2646
MS
1605 tmp = build_pointer_type (mapcar (TREE_TYPE (t), func));
1606 return cp_build_type_variant (tmp, TYPE_READONLY (t), TYPE_VOLATILE (t));
00595019 1607 case REFERENCE_TYPE:
e76a2646
MS
1608 tmp = build_reference_type (mapcar (TREE_TYPE (t), func));
1609 return cp_build_type_variant (tmp, TYPE_READONLY (t), TYPE_VOLATILE (t));
00595019 1610 case FUNCTION_TYPE:
e76a2646
MS
1611 tmp = build_function_type (mapcar (TREE_TYPE (t), func),
1612 mapcar (TYPE_ARG_TYPES (t), func));
1613 return cp_build_type_variant (tmp, TYPE_READONLY (t), TYPE_VOLATILE (t));
00595019 1614 case ARRAY_TYPE:
e76a2646
MS
1615 tmp = build_array_type (mapcar (TREE_TYPE (t), func),
1616 mapcar (TYPE_DOMAIN (t), func));
1617 return cp_build_type_variant (tmp, TYPE_READONLY (t), TYPE_VOLATILE (t));
b7484fbe 1618 case INTEGER_TYPE:
e76a2646
MS
1619 tmp = build_index_type (mapcar (TYPE_MAX_VALUE (t), func));
1620 return cp_build_type_variant (tmp, TYPE_READONLY (t), TYPE_VOLATILE (t));
00595019 1621 case OFFSET_TYPE:
e76a2646
MS
1622 tmp = build_offset_type (mapcar (TYPE_OFFSET_BASETYPE (t), func),
1623 mapcar (TREE_TYPE (t), func));
1624 return cp_build_type_variant (tmp, TYPE_READONLY (t), TYPE_VOLATILE (t));
00595019 1625 case METHOD_TYPE:
e76a2646
MS
1626 tmp = build_cplus_method_type
1627 (mapcar (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t))), func),
1628 mapcar (TREE_TYPE (t), func),
1629 mapcar (TREE_CHAIN (TYPE_ARG_TYPES (t)), func));
1630 return cp_build_type_variant (tmp, TYPE_READONLY (t), TYPE_VOLATILE (t));
b7484fbe 1631
00595019
MS
1632 case RECORD_TYPE:
1633 if (TYPE_PTRMEMFUNC_P (t))
1634 return build_ptrmemfunc_type
878cd289 1635 (mapcar (TYPE_PTRMEMFUNC_FN_TYPE (t), func));
00595019
MS
1636 /* else fall through */
1637
8d08fdba 1638 /* This list is incomplete, but should suffice for now.
e76a2646 1639 It is very important that `sorry' not call
8d08fdba
MS
1640 `report_error_function'. That could cause an infinite loop. */
1641 default:
1642 sorry ("initializer contains unrecognized tree code");
1643 return error_mark_node;
1644
1645 }
1646 my_friendly_abort (107);
1647 /* NOTREACHED */
1648 return NULL_TREE;
1649}
1650
878cd289
MS
1651static tree
1652perm_manip (t)
1653 tree t;
1654{
1655 if (TREE_PERMANENT (t))
1656 return t;
ec255269
MS
1657 /* Support `void f () { extern int i; A<&i> a; }' */
1658 if ((TREE_CODE (t) == VAR_DECL || TREE_CODE (t) == FUNCTION_DECL)
1659 && TREE_PUBLIC (t))
1660 return copy_node (t);
878cd289
MS
1661 return NULL_TREE;
1662}
1663
8d08fdba
MS
1664/* Assuming T is a node built bottom-up, make it all exist on
1665 permanent obstack, if it is not permanent already. */
e92cc029 1666
8d08fdba
MS
1667tree
1668copy_to_permanent (t)
1669 tree t;
1670{
1671 register struct obstack *ambient_obstack = current_obstack;
1672 register struct obstack *ambient_saveable_obstack = saveable_obstack;
71851aaa 1673 int resume;
8d08fdba
MS
1674
1675 if (t == NULL_TREE || TREE_PERMANENT (t))
1676 return t;
1677
1678 saveable_obstack = &permanent_obstack;
1679 current_obstack = saveable_obstack;
71851aaa 1680 resume = suspend_momentary ();
8d08fdba 1681
878cd289 1682 t = mapcar (t, perm_manip);
8d08fdba 1683
71851aaa 1684 resume_momentary (resume);
8d08fdba
MS
1685 current_obstack = ambient_obstack;
1686 saveable_obstack = ambient_saveable_obstack;
1687
1688 return t;
1689}
1690
5566b478
MS
1691#ifdef GATHER_STATISTICS
1692extern int depth_reached;
1693#endif
1694
8d08fdba
MS
1695void
1696print_lang_statistics ()
1697{
5566b478 1698 extern struct obstack maybepermanent_obstack, decl_obstack;
8d08fdba 1699 print_obstack_statistics ("class_obstack", &class_obstack);
5566b478 1700 print_obstack_statistics ("decl_obstack", &decl_obstack);
8d08fdba
MS
1701 print_obstack_statistics ("permanent_obstack", &permanent_obstack);
1702 print_obstack_statistics ("maybepermanent_obstack", &maybepermanent_obstack);
1703 print_search_statistics ();
1704 print_class_statistics ();
5566b478
MS
1705#ifdef GATHER_STATISTICS
1706 fprintf (stderr, "maximum template instantiation depth reached: %d\n",
1707 depth_reached);
1708#endif
8d08fdba
MS
1709}
1710
1711/* This is used by the `assert' macro. It is provided in libgcc.a,
1712 which `cc' doesn't know how to link. Note that the C++ front-end
1713 no longer actually uses the `assert' macro (instead, it calls
1714 my_friendly_assert). But all of the back-end files still need this. */
e92cc029 1715
8d08fdba
MS
1716void
1717__eprintf (string, expression, line, filename)
1718#ifdef __STDC__
1719 const char *string;
1720 const char *expression;
1721 unsigned line;
1722 const char *filename;
1723#else
1724 char *string;
1725 char *expression;
1726 unsigned line;
1727 char *filename;
1728#endif
1729{
1730 fprintf (stderr, string, expression, line, filename);
1731 fflush (stderr);
1732 abort ();
1733}
1734
e92cc029
MS
1735/* Return, as an INTEGER_CST node, the number of elements for TYPE
1736 (which is an ARRAY_TYPE). This counts only elements of the top
1737 array. */
8d08fdba
MS
1738
1739tree
1740array_type_nelts_top (type)
1741 tree type;
1742{
eae89e04 1743 return fold (build (PLUS_EXPR, sizetype,
8d08fdba
MS
1744 array_type_nelts (type),
1745 integer_one_node));
1746}
1747
e92cc029
MS
1748/* Return, as an INTEGER_CST node, the number of elements for TYPE
1749 (which is an ARRAY_TYPE). This one is a recursive count of all
1750 ARRAY_TYPEs that are clumped together. */
8d08fdba
MS
1751
1752tree
1753array_type_nelts_total (type)
1754 tree type;
1755{
1756 tree sz = array_type_nelts_top (type);
1757 type = TREE_TYPE (type);
1758 while (TREE_CODE (type) == ARRAY_TYPE)
1759 {
1760 tree n = array_type_nelts_top (type);
eae89e04 1761 sz = fold (build (MULT_EXPR, sizetype, sz, n));
8d08fdba
MS
1762 type = TREE_TYPE (type);
1763 }
1764 return sz;
1765}
878cd289
MS
1766
1767static
1768tree
1769bot_manip (t)
1770 tree t;
1771{
1772 if (TREE_CODE (t) != TREE_LIST && ! TREE_SIDE_EFFECTS (t))
1773 return t;
1774 else if (TREE_CODE (t) == TARGET_EXPR)
1775 return build_cplus_new (TREE_TYPE (t),
5566b478 1776 break_out_target_exprs (TREE_OPERAND (t, 1)));
878cd289
MS
1777 return NULL_TREE;
1778}
1779
1780/* Actually, we'll just clean out the target exprs for the moment. */
e92cc029 1781
878cd289
MS
1782tree
1783break_out_target_exprs (t)
1784 tree t;
1785{
1786 return mapcar (t, bot_manip);
1787}
f30432d7 1788
5566b478
MS
1789/* Obstack used for allocating nodes in template function and variable
1790 definitions. */
1791
1792extern struct obstack *expression_obstack;
1793
1794/* Similar to `build_nt', except we build
1795 on the permanent_obstack, regardless. */
1796
1797tree
1798build_min_nt VPROTO((enum tree_code code, ...))
1799{
1800#ifndef __STDC__
1801 enum tree_code code;
1802#endif
1803 register struct obstack *ambient_obstack = expression_obstack;
1804 va_list p;
1805 register tree t;
1806 register int length;
1807 register int i;
1808
1809 VA_START (p, code);
1810
1811#ifndef __STDC__
1812 code = va_arg (p, enum tree_code);
1813#endif
1814
1815 expression_obstack = &permanent_obstack;
1816
1817 t = make_node (code);
1818 length = tree_code_length[(int) code];
1819 TREE_COMPLEXITY (t) = lineno;
1820
1821 for (i = 0; i < length; i++)
1822 {
1823 tree x = va_arg (p, tree);
1824 TREE_OPERAND (t, i) = copy_to_permanent (x);
1825 }
1826
1827 va_end (p);
1828 expression_obstack = ambient_obstack;
1829 return t;
1830}
1831
1832/* Similar to `build', except we build
1833 on the permanent_obstack, regardless. */
1834
1835tree
1836build_min VPROTO((enum tree_code code, tree tt, ...))
1837{
1838#ifndef __STDC__
1839 enum tree_code code;
1840 tree tt;
1841#endif
1842 register struct obstack *ambient_obstack = expression_obstack;
1843 va_list p;
1844 register tree t;
1845 register int length;
1846 register int i;
1847
1848 VA_START (p, tt);
1849
1850#ifndef __STDC__
1851 code = va_arg (p, enum tree_code);
1852 tt = va_arg (p, tree);
1853#endif
1854
1855 expression_obstack = &permanent_obstack;
1856
1857 t = make_node (code);
1858 length = tree_code_length[(int) code];
1859 TREE_TYPE (t) = tt;
1860 TREE_COMPLEXITY (t) = lineno;
1861
1862 for (i = 0; i < length; i++)
1863 {
1864 tree x = va_arg (p, tree);
1865 TREE_OPERAND (t, i) = copy_to_permanent (x);
1866 }
1867
1868 va_end (p);
1869 expression_obstack = ambient_obstack;
1870 return t;
1871}
1872
1873/* Same as `tree_cons' but make a permanent object. */
1874
1875tree
1876min_tree_cons (purpose, value, chain)
1877 tree purpose, value, chain;
1878{
1879 register tree node;
1880 register struct obstack *ambient_obstack = current_obstack;
1881 current_obstack = &permanent_obstack;
1882
fc378698
MS
1883 node = tree_cons (copy_to_permanent (purpose),
1884 copy_to_permanent (value), chain);
5566b478
MS
1885 current_obstack = ambient_obstack;
1886 return node;
1887}
1888
1889tree
1890get_type_decl (t)
1891 tree t;
1892{
1893 if (TREE_CODE (t) == IDENTIFIER_NODE)
1894 return identifier_typedecl_value (t);
1895 if (TREE_CODE (t) == TYPE_DECL)
1896 return t;
1897 if (TREE_CODE_CLASS (TREE_CODE (t)) == 't')
1898 return TYPE_STUB_DECL (t);
1899
1900 my_friendly_abort (42);
1901}
1902
1903int
1904can_free (obstack, t)
1905 struct obstack *obstack;
1906 tree t;
1907{
1908 int size;
1909
1910 if (TREE_CODE (t) == TREE_VEC)
1911 size = (TREE_VEC_LENGTH (t)-1) * sizeof (tree) + sizeof (struct tree_vec);
1912 else
1913 my_friendly_abort (42);
1914
1915#define ROUND(x) ((x + obstack_alignment_mask (obstack)) \
1916 & ~ obstack_alignment_mask (obstack))
1917 if ((char *)t + ROUND (size) == obstack_next_free (obstack))
1918 return 1;
1919#undef ROUND
1920
1921 return 0;
1922}
1923
1924/* Return first vector element whose BINFO_TYPE is ELEM.
934c6b13 1925 Return 0 if ELEM is not in VEC. VEC may be NULL_TREE. */
5566b478
MS
1926
1927tree
1928vec_binfo_member (elem, vec)
1929 tree elem, vec;
1930{
1931 int i;
934c6b13
MS
1932
1933 if (vec)
1934 for (i = 0; i < TREE_VEC_LENGTH (vec); ++i)
e92cc029 1935 if (comptypes (elem, BINFO_TYPE (TREE_VEC_ELT (vec, i)), 1))
934c6b13
MS
1936 return TREE_VEC_ELT (vec, i);
1937
5566b478
MS
1938 return NULL_TREE;
1939}
e76a2646
MS
1940
1941/* Kludge around the fact that DECL_CONTEXT for virtual functions returns
1942 the wrong thing for decl_function_context. Hopefully the uses in the
1943 backend won't matter, since we don't need a static chain for local class
1944 methods. FIXME! */
1945
1946tree
1947hack_decl_function_context (decl)
1948 tree decl;
1949{
1950 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FUNCTION_MEMBER_P (decl))
1951 return decl_function_context (TYPE_MAIN_DECL (DECL_CLASS_CONTEXT (decl)));
1952 return decl_function_context (decl);
1953}
67d743fe
MS
1954
1955/* Return truthvalue of whether T1 is the same tree structure as T2.
1956 Return 1 if they are the same.
1957 Return 0 if they are understandably different.
1958 Return -1 if either contains tree structure not understood by
1959 this function. */
1960
1961int
1962cp_tree_equal (t1, t2)
1963 tree t1, t2;
1964{
1965 register enum tree_code code1, code2;
1966 int cmp;
1967
1968 if (t1 == t2)
1969 return 1;
1970 if (t1 == 0 || t2 == 0)
1971 return 0;
1972
1973 code1 = TREE_CODE (t1);
1974 code2 = TREE_CODE (t2);
1975
1976 if (code1 == NOP_EXPR || code1 == CONVERT_EXPR || code1 == NON_LVALUE_EXPR)
1977 if (code2 == NOP_EXPR || code2 == CONVERT_EXPR || code2 == NON_LVALUE_EXPR)
1978 return cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
1979 else
1980 return cp_tree_equal (TREE_OPERAND (t1, 0), t2);
1981 else if (code2 == NOP_EXPR || code2 == CONVERT_EXPR
1982 || code2 == NON_LVALUE_EXPR)
1983 return cp_tree_equal (t1, TREE_OPERAND (t2, 0));
1984
1985 if (code1 != code2)
1986 return 0;
1987
1988 switch (code1)
1989 {
1990 case INTEGER_CST:
1991 return TREE_INT_CST_LOW (t1) == TREE_INT_CST_LOW (t2)
1992 && TREE_INT_CST_HIGH (t1) == TREE_INT_CST_HIGH (t2);
1993
1994 case REAL_CST:
1995 return REAL_VALUES_EQUAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
1996
1997 case STRING_CST:
1998 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
1999 && !bcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
2000 TREE_STRING_LENGTH (t1));
2001
2002 case CONSTRUCTOR:
2003 abort ();
2004
2005 case SAVE_EXPR:
2006 return cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
2007
2008 case CALL_EXPR:
2009 cmp = cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
2010 if (cmp <= 0)
2011 return cmp;
2012 return simple_cst_list_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
2013
2014 case TARGET_EXPR:
2015 /* Special case: if either target is an unallocated VAR_DECL,
2016 it means that it's going to be unified with whatever the
2017 TARGET_EXPR is really supposed to initialize, so treat it
2018 as being equivalent to anything. */
2019 if ((TREE_CODE (TREE_OPERAND (t1, 0)) == VAR_DECL
2020 && DECL_NAME (TREE_OPERAND (t1, 0)) == NULL_TREE
2021 && DECL_RTL (TREE_OPERAND (t1, 0)) == 0)
2022 || (TREE_CODE (TREE_OPERAND (t2, 0)) == VAR_DECL
2023 && DECL_NAME (TREE_OPERAND (t2, 0)) == NULL_TREE
2024 && DECL_RTL (TREE_OPERAND (t2, 0)) == 0))
2025 cmp = 1;
2026 else
2027 cmp = cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
2028 if (cmp <= 0)
2029 return cmp;
2030 return cp_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
2031
2032 case WITH_CLEANUP_EXPR:
2033 cmp = cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
2034 if (cmp <= 0)
2035 return cmp;
2036 return cp_tree_equal (TREE_OPERAND (t1, 2), TREE_OPERAND (t1, 2));
2037
2038 case COMPONENT_REF:
2039 if (TREE_OPERAND (t1, 1) == TREE_OPERAND (t2, 1))
2040 return cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
2041 return 0;
2042
2043 case VAR_DECL:
2044 case PARM_DECL:
2045 case CONST_DECL:
2046 case FUNCTION_DECL:
2047 return 0;
2048
2049 case TEMPLATE_CONST_PARM:
2050 return TEMPLATE_CONST_IDX (t1) == TEMPLATE_CONST_IDX (t2);
2051
2052 case SIZEOF_EXPR:
2053 if (TREE_CODE (TREE_OPERAND (t1, 0)) != TREE_CODE (TREE_OPERAND (t2, 0)))
2054 return 0;
2055 if (TREE_CODE_CLASS (TREE_CODE (TREE_OPERAND (t1, 0))) == 't')
2056 return comptypes (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0), 1);
2057 break;
2058 }
2059
2060 switch (TREE_CODE_CLASS (code1))
2061 {
2062 int i;
2063 case '1':
2064 case '2':
2065 case '<':
2066 case 'e':
2067 case 'r':
2068 case 's':
2069 cmp = 1;
2070 for (i=0; i<tree_code_length[(int) code1]; ++i)
2071 {
2072 cmp = cp_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i));
2073 if (cmp <= 0)
2074 return cmp;
2075 }
2076 return cmp;
2077 }
2078
2079 return -1;
2080}