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