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