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