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