class.c (instantiate_type): Don't just return a known type if it's wrong.
[gcc.git] / gcc / cp / tree.c
1 /* Language-dependent node constructors for parse phase of GNU compiler.
2 Copyright (C) 1987, 88, 92, 93, 94, 95, 1996 Free Software Foundation, Inc.
3 Hacked by Michael Tiemann (tiemann@cygnus.com)
4
5 This file is part of GNU CC.
6
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
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"
28 #include "rtl.h"
29 #ifdef __STDC__
30 #include <stdarg.h>
31 #else
32 #include <varargs.h>
33 #endif
34
35 #ifdef HAVE_STDLIB_H
36 #include <stdlib.h>
37 #endif
38
39 #ifdef NEED_DECLARATION_FREE
40 extern void free PROTO((void *));
41 #endif
42
43 extern void compiler_error ();
44
45 static tree get_identifier_list PROTO((tree));
46 static tree bot_manip PROTO((tree));
47 static tree perm_manip PROTO((tree));
48 static tree build_cplus_array_type_1 PROTO((tree, tree));
49 static void list_hash_add PROTO((int, tree));
50 static int list_hash PROTO((tree, tree, tree));
51 static tree list_hash_lookup PROTO((int, int, int, int, tree, tree,
52 tree));
53
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
60 int
61 real_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
70 if (ref == current_class_ptr && flag_this_is_variable <= 0)
71 return 0;
72
73 switch (TREE_CODE (ref))
74 {
75 /* preincrements and predecrements are valid lvals, provided
76 what they refer to are valid lvals. */
77 case PREINCREMENT_EXPR:
78 case PREDECREMENT_EXPR:
79 case COMPONENT_REF:
80 case SAVE_EXPR:
81 case UNSAVE_EXPR:
82 case TRY_CATCH_EXPR:
83 case WITH_CLEANUP_EXPR:
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
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)));
128
129 default:
130 break;
131 }
132
133 return 0;
134 }
135
136 /* This differs from real_lvalue_p in that class rvalues are considered
137 lvalues. */
138 int
139 lvalue_p (ref)
140 tree ref;
141 {
142 if (! language_lvalue_valid (ref))
143 return 0;
144
145 if (TREE_CODE (TREE_TYPE (ref)) == REFERENCE_TYPE)
146 return 1;
147
148 if (ref == current_class_ptr && flag_this_is_variable <= 0)
149 return 0;
150
151 switch (TREE_CODE (ref))
152 {
153 /* preincrements and predecrements are valid lvals, provided
154 what they refer to are valid lvals. */
155 case PREINCREMENT_EXPR:
156 case PREDECREMENT_EXPR:
157 case REALPART_EXPR:
158 case IMAGPART_EXPR:
159 case COMPONENT_REF:
160 case SAVE_EXPR:
161 case UNSAVE_EXPR:
162 case TRY_CATCH_EXPR:
163 case WITH_CLEANUP_EXPR:
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)
181 return 1;
182 break;
183
184 case TARGET_EXPR:
185 return 1;
186
187 case CALL_EXPR:
188 if (IS_AGGR_TYPE (TREE_TYPE (ref)))
189 return 1;
190 break;
191
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));
211
212 case MAX_EXPR:
213 case MIN_EXPR:
214 return (lvalue_p (TREE_OPERAND (ref, 0))
215 && lvalue_p (TREE_OPERAND (ref, 1)));
216
217 default:
218 break;
219 }
220
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
227 int
228 lvalue_or_else (ref, string)
229 tree ref;
230 char *string;
231 {
232 int win = lvalue_p (ref);
233 if (! win)
234 error ("non-lvalue in %s", string);
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
243 and language-specific expression expanders. */
244
245 tree
246 build_cplus_new (type, init)
247 tree type;
248 tree init;
249 {
250 tree slot;
251 tree rval;
252
253 if (TREE_CODE (init) != CALL_EXPR && TREE_CODE (init) != AGGR_INIT_EXPR)
254 return init;
255
256 slot = build (VAR_DECL, type);
257 DECL_ARTIFICIAL (slot) = 1;
258 layout_decl (slot, 0);
259 rval = build (AGGR_INIT_EXPR, type,
260 TREE_OPERAND (init, 0), TREE_OPERAND (init, 1), slot);
261 TREE_SIDE_EFFECTS (rval) = 1;
262 rval = build (TARGET_EXPR, type, slot, rval, NULL_TREE, NULL_TREE);
263 TREE_SIDE_EFFECTS (rval) = 1;
264
265 return rval;
266 }
267
268 /* Encapsulate the expression INIT in a TARGET_EXPR. */
269
270 tree
271 get_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
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
290 tree
291 break_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)))
298 return build_cplus_new (TREE_TYPE (tmp), tmp);
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)),
309 TREE_OPERAND (tmp, 0));
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
322 tree
323 break_out_calls (exp)
324 tree exp;
325 {
326 register tree t1, t2;
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
339 /* Don't try and defeat a save_expr, as it should only be done once. */
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 */
354 #if 0 /* This is bogus. jason 9/21/94 */
355
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 }
362 #endif
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
409 extern struct obstack *current_obstack;
410 extern struct obstack permanent_obstack, class_obstack;
411 extern struct obstack *saveable_obstack;
412 extern struct obstack *expression_obstack;
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. */
421
422 tree
423 build_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
440 ptype = build_pointer_type (basetype);
441
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
460 static tree
461 build_cplus_array_type_1 (elt_type, index_type)
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
478 if (processing_template_decl)
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);
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 }
495
496 tree
497 build_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 }
513 \f
514 /* Make a variant type in the proper way for C/C++, propagating qualifiers
515 down to the element type of an array. */
516
517 tree
518 cp_build_type_variant (type, constp, volatilep)
519 tree type;
520 int constp, volatilep;
521 {
522 if (type == error_mark_node)
523 return type;
524
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));
531 type = build_cplus_array_type_1 (cp_build_type_variant
532 (TREE_TYPE (type), constp, volatilep),
533 TYPE_DOMAIN (type));
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 ();
547 return type;
548 }
549 return build_type_variant (type, constp, volatilep);
550 }
551 \f
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. */
558
559 void
560 propagate_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);
577 tree delta;
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),
612 base_base_binfo,
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);
619 BINFO_INHERITANCE_CHAIN (chain) = base_binfo;
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. */
640
641 int
642 layout_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
651 unsigned record_align = MAX (STRUCTURE_SIZE_BOUNDARY, TYPE_ALIGN (rec));
652 #else
653 unsigned record_align = MAX (BITS_PER_UNIT, TYPE_ALIGN (rec));
654 #endif
655 int desired_align;
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. */
662 register unsigned const_size = 0;
663 register tree var_size = 0;
664 int nonvirtual_const_size;
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;
674
675 while (vbase_types)
676 {
677 tree basetype = BINFO_TYPE (vbase_types);
678 tree offset;
679
680 desired_align = TYPE_ALIGN (basetype);
681 record_align = MAX (record_align, desired_align);
682
683 if (const_size == 0)
684 offset = integer_zero_node;
685 else
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 }
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)
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 }
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
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
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;
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
739 BINFO_INHERITANCE_CHAIN (vbase_types) = TYPE_BINFO (rec);
740
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),
754 base_base_binfo,
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);
761 BINFO_INHERITANCE_CHAIN (chain) = vbase_types;
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
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
780 Returns list of virtual base classes in a FIELD_DECL chain. */
781
782 tree
783 layout_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
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
799 VAR_SIZE. And so far, we've been successful. */
800 #if 0
801 register tree var_size = 0;
802 #endif
803
804 register unsigned const_size = 0;
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 {
812 int inc, desired_align, int_vbase_size;
813 register tree base_binfo = TREE_VEC_ELT (binfos, i);
814 register tree basetype = BINFO_TYPE (base_binfo);
815 tree decl, offset;
816
817 if (TYPE_SIZE (basetype) == 0)
818 {
819 #if 0
820 /* This error is now reported in xref_tag, thus giving better
821 location information. */
822 error_with_aggr_type (base_binfo,
823 "base class `%s' has incomplete type");
824
825 TREE_VIA_PUBLIC (base_binfo) = 1;
826 TREE_VIA_PROTECTED (base_binfo) = 0;
827 TREE_VIA_VIRTUAL (base_binfo) = 0;
828
829 /* Should handle this better so that
830
831 class A;
832 class B: private A { virtual void F(); };
833
834 does not dump core when compiled. */
835 my_friendly_abort (121);
836 #endif
837 continue;
838 }
839
840 /* All basetypes are recorded in the association list of the
841 derived type. */
842
843 if (TREE_VIA_VIRTUAL (base_binfo))
844 {
845 int j;
846 char *name = (char *)alloca (TYPE_NAME_LENGTH (basetype)
847 + sizeof (VBASE_NAME) + 1);
848
849 /* The offset for a virtual base class is only used in computing
850 virtual function tables and for initializing virtual base
851 pointers. It is built once `get_vbase_types' is called. */
852
853 /* If this basetype can come from another vbase pointer
854 without an additional indirection, we will share
855 that pointer. If an indirection is involved, we
856 make our own pointer. */
857 for (j = 0; j < n_baseclasses; j++)
858 {
859 tree other_base_binfo = TREE_VEC_ELT (binfos, j);
860 if (! TREE_VIA_VIRTUAL (other_base_binfo)
861 && binfo_member (basetype,
862 CLASSTYPE_VBASECLASSES (BINFO_TYPE (other_base_binfo))))
863 goto got_it;
864 }
865 sprintf (name, VBASE_NAME_FORMAT, TYPE_NAME_STRING (basetype));
866 decl = build_lang_field_decl (FIELD_DECL, get_identifier (name),
867 build_pointer_type (basetype));
868 /* If you change any of the below, take a look at all the
869 other VFIELD_BASEs and VTABLE_BASEs in the code, and change
870 them too. */
871 DECL_ASSEMBLER_NAME (decl) = get_identifier (VTABLE_BASE);
872 DECL_VIRTUAL_P (decl) = 1;
873 DECL_ARTIFICIAL (decl) = 1;
874 DECL_FIELD_CONTEXT (decl) = rec;
875 DECL_CLASS_CONTEXT (decl) = rec;
876 DECL_FCONTEXT (decl) = basetype;
877 DECL_SAVED_INSNS (decl) = NULL_RTX;
878 DECL_FIELD_SIZE (decl) = 0;
879 DECL_ALIGN (decl) = TYPE_ALIGN (ptr_type_node);
880 TREE_CHAIN (decl) = vbase_decls;
881 BINFO_VPTR_FIELD (base_binfo) = decl;
882 vbase_decls = decl;
883
884 got_it:
885 /* The space this decl occupies has already been accounted for. */
886 continue;
887 }
888
889 /* Effective C++ rule 14. We only need to check TYPE_VIRTUAL_P
890 here because the case of virtual functions but non-virtual
891 dtor is handled in finish_struct_1. */
892 if (warn_ecpp && ! TYPE_VIRTUAL_P (basetype)
893 && TYPE_HAS_DESTRUCTOR (basetype))
894 cp_warning ("base class `%#T' has a non-virtual destructor", basetype);
895
896 if (const_size == 0)
897 offset = integer_zero_node;
898 else
899 {
900 /* Give each base type the alignment it wants. */
901 const_size = CEIL (const_size, TYPE_ALIGN (basetype))
902 * TYPE_ALIGN (basetype);
903 offset = size_int ((const_size + BITS_PER_UNIT - 1) / BITS_PER_UNIT);
904 }
905 BINFO_OFFSET (base_binfo) = offset;
906 if (CLASSTYPE_VSIZE (basetype))
907 {
908 BINFO_VTABLE (base_binfo) = TYPE_BINFO_VTABLE (basetype);
909 BINFO_VIRTUALS (base_binfo) = TYPE_BINFO_VIRTUALS (basetype);
910 }
911 TREE_CHAIN (base_binfo) = TYPE_BINFO (rec);
912 TYPE_BINFO (rec) = base_binfo;
913
914 /* Add only the amount of storage not present in
915 the virtual baseclasses. */
916
917 int_vbase_size = TREE_INT_CST_LOW (CLASSTYPE_VBASE_SIZE (basetype));
918 if (TREE_INT_CST_LOW (TYPE_SIZE (basetype)) > int_vbase_size)
919 {
920 inc = MAX (record_align,
921 (TREE_INT_CST_LOW (TYPE_SIZE (basetype))
922 - int_vbase_size));
923
924 /* Record must have at least as much alignment as any field. */
925 desired_align = TYPE_ALIGN (basetype);
926 record_align = MAX (record_align, desired_align);
927
928 const_size += inc;
929 }
930 }
931
932 if (const_size)
933 CLASSTYPE_SIZE (rec) = size_int (const_size);
934 else
935 CLASSTYPE_SIZE (rec) = integer_zero_node;
936 CLASSTYPE_ALIGN (rec) = record_align;
937
938 return vbase_decls;
939 }
940 \f
941 /* Hashing of lists so that we don't make duplicates.
942 The entry point is `list_hash_canon'. */
943
944 /* Each hash table slot is a bucket containing a chain
945 of these structures. */
946
947 struct list_hash
948 {
949 struct list_hash *next; /* Next structure in the bucket. */
950 int hashcode; /* Hash code of this list. */
951 tree list; /* The list recorded here. */
952 };
953
954 /* Now here is the hash table. When recording a list, it is added
955 to the slot whose index is the hash code mod the table size.
956 Note that the hash table is used for several kinds of lists.
957 While all these live in the same table, they are completely independent,
958 and the hash code is computed differently for each of these. */
959
960 #define TYPE_HASH_SIZE 59
961 static struct list_hash *list_hash_table[TYPE_HASH_SIZE];
962
963 /* Compute a hash code for a list (chain of TREE_LIST nodes
964 with goodies in the TREE_PURPOSE, TREE_VALUE, and bits of the
965 TREE_COMMON slots), by adding the hash codes of the individual entries. */
966
967 static int
968 list_hash (purpose, value, chain)
969 tree purpose, value, chain;
970 {
971 register int hashcode = 0;
972
973 if (chain)
974 hashcode += TYPE_HASH (chain);
975
976 if (value)
977 hashcode += TYPE_HASH (value);
978 else
979 hashcode += 1007;
980 if (purpose)
981 hashcode += TYPE_HASH (purpose);
982 else
983 hashcode += 1009;
984 return hashcode;
985 }
986
987 /* Look in the type hash table for a type isomorphic to TYPE.
988 If one is found, return it. Otherwise return 0. */
989
990 static tree
991 list_hash_lookup (hashcode, via_public, via_protected, via_virtual,
992 purpose, value, chain)
993 int hashcode, via_public, via_virtual, via_protected;
994 tree purpose, value, chain;
995 {
996 register struct list_hash *h;
997
998 for (h = list_hash_table[hashcode % TYPE_HASH_SIZE]; h; h = h->next)
999 if (h->hashcode == hashcode
1000 && TREE_VIA_VIRTUAL (h->list) == via_virtual
1001 && TREE_VIA_PUBLIC (h->list) == via_public
1002 && TREE_VIA_PROTECTED (h->list) == via_protected
1003 && TREE_PURPOSE (h->list) == purpose
1004 && TREE_VALUE (h->list) == value
1005 && TREE_CHAIN (h->list) == chain)
1006 return h->list;
1007 return 0;
1008 }
1009
1010 /* Add an entry to the list-hash-table
1011 for a list TYPE whose hash code is HASHCODE. */
1012
1013 static void
1014 list_hash_add (hashcode, list)
1015 int hashcode;
1016 tree list;
1017 {
1018 register struct list_hash *h;
1019
1020 h = (struct list_hash *) obstack_alloc (&class_obstack, sizeof (struct list_hash));
1021 h->hashcode = hashcode;
1022 h->list = list;
1023 h->next = list_hash_table[hashcode % TYPE_HASH_SIZE];
1024 list_hash_table[hashcode % TYPE_HASH_SIZE] = h;
1025 }
1026
1027 /* Given TYPE, and HASHCODE its hash code, return the canonical
1028 object for an identical list if one already exists.
1029 Otherwise, return TYPE, and record it as the canonical object
1030 if it is a permanent object.
1031
1032 To use this function, first create a list of the sort you want.
1033 Then compute its hash code from the fields of the list that
1034 make it different from other similar lists.
1035 Then call this function and use the value.
1036 This function frees the list you pass in if it is a duplicate. */
1037
1038 /* Set to 1 to debug without canonicalization. Never set by program. */
1039
1040 static int debug_no_list_hash = 0;
1041
1042 tree
1043 hash_tree_cons (via_public, via_virtual, via_protected, purpose, value, chain)
1044 int via_public, via_virtual, via_protected;
1045 tree purpose, value, chain;
1046 {
1047 struct obstack *ambient_obstack = current_obstack;
1048 tree t;
1049 int hashcode;
1050
1051 if (! debug_no_list_hash)
1052 {
1053 hashcode = list_hash (purpose, value, chain);
1054 t = list_hash_lookup (hashcode, via_public, via_protected, via_virtual,
1055 purpose, value, chain);
1056 if (t)
1057 return t;
1058 }
1059
1060 current_obstack = &class_obstack;
1061
1062 t = tree_cons (purpose, value, chain);
1063 TREE_VIA_PUBLIC (t) = via_public;
1064 TREE_VIA_PROTECTED (t) = via_protected;
1065 TREE_VIA_VIRTUAL (t) = via_virtual;
1066
1067 /* If this is a new list, record it for later reuse. */
1068 if (! debug_no_list_hash)
1069 list_hash_add (hashcode, t);
1070
1071 current_obstack = ambient_obstack;
1072 return t;
1073 }
1074
1075 /* Constructor for hashed lists. */
1076
1077 tree
1078 hash_tree_chain (value, chain)
1079 tree value, chain;
1080 {
1081 return hash_tree_cons (0, 0, 0, NULL_TREE, value, chain);
1082 }
1083
1084 /* Similar, but used for concatenating two lists. */
1085
1086 tree
1087 hash_chainon (list1, list2)
1088 tree list1, list2;
1089 {
1090 if (list2 == 0)
1091 return list1;
1092 if (list1 == 0)
1093 return list2;
1094 if (TREE_CHAIN (list1) == NULL_TREE)
1095 return hash_tree_chain (TREE_VALUE (list1), list2);
1096 return hash_tree_chain (TREE_VALUE (list1),
1097 hash_chainon (TREE_CHAIN (list1), list2));
1098 }
1099
1100 static tree
1101 get_identifier_list (value)
1102 tree value;
1103 {
1104 tree list = IDENTIFIER_AS_LIST (value);
1105 if (list != NULL_TREE
1106 && (TREE_CODE (list) != TREE_LIST
1107 || TREE_VALUE (list) != value))
1108 list = NULL_TREE;
1109 else if (IDENTIFIER_HAS_TYPE_VALUE (value)
1110 && TREE_CODE (IDENTIFIER_TYPE_VALUE (value)) == RECORD_TYPE
1111 && IDENTIFIER_TYPE_VALUE (value)
1112 == TYPE_MAIN_VARIANT (IDENTIFIER_TYPE_VALUE (value)))
1113 {
1114 tree type = IDENTIFIER_TYPE_VALUE (value);
1115
1116 if (TYPE_PTRMEMFUNC_P (type))
1117 list = NULL_TREE;
1118 else if (type == current_class_type)
1119 /* Don't mess up the constructor name. */
1120 list = tree_cons (NULL_TREE, value, NULL_TREE);
1121 else
1122 {
1123 if (! CLASSTYPE_ID_AS_LIST (type))
1124 CLASSTYPE_ID_AS_LIST (type)
1125 = perm_tree_cons (NULL_TREE, TYPE_IDENTIFIER (type), NULL_TREE);
1126 list = CLASSTYPE_ID_AS_LIST (type);
1127 }
1128 }
1129 return list;
1130 }
1131
1132 tree
1133 get_decl_list (value)
1134 tree value;
1135 {
1136 tree list = NULL_TREE;
1137
1138 if (TREE_CODE (value) == IDENTIFIER_NODE)
1139 list = get_identifier_list (value);
1140 else if (TREE_CODE (value) == RECORD_TYPE
1141 && TYPE_LANG_SPECIFIC (value)
1142 && value == TYPE_MAIN_VARIANT (value))
1143 list = CLASSTYPE_AS_LIST (value);
1144
1145 if (list != NULL_TREE)
1146 {
1147 my_friendly_assert (TREE_CHAIN (list) == NULL_TREE, 301);
1148 return list;
1149 }
1150
1151 return build_decl_list (NULL_TREE, value);
1152 }
1153 \f
1154 /* Build an association between TYPE and some parameters:
1155
1156 OFFSET is the offset added to `this' to convert it to a pointer
1157 of type `TYPE *'
1158
1159 BINFO is the base binfo to use, if we are deriving from one. This
1160 is necessary, as we want specialized parent binfos from base
1161 classes, so that the VTABLE_NAMEs of bases are for the most derived
1162 type, instead of of the simple type.
1163
1164 VTABLE is the virtual function table with which to initialize
1165 sub-objects of type TYPE.
1166
1167 VIRTUALS are the virtual functions sitting in VTABLE.
1168
1169 CHAIN are more associations we must retain. */
1170
1171 tree
1172 make_binfo (offset, binfo, vtable, virtuals, chain)
1173 tree offset, binfo;
1174 tree vtable, virtuals;
1175 tree chain;
1176 {
1177 tree new_binfo = make_tree_vec (6);
1178 tree type;
1179
1180 if (TREE_CODE (binfo) == TREE_VEC)
1181 type = BINFO_TYPE (binfo);
1182 else
1183 {
1184 type = binfo;
1185 binfo = TYPE_BINFO (binfo);
1186 }
1187
1188 TREE_CHAIN (new_binfo) = chain;
1189 if (chain)
1190 TREE_USED (new_binfo) = TREE_USED (chain);
1191
1192 TREE_TYPE (new_binfo) = TYPE_MAIN_VARIANT (type);
1193 BINFO_OFFSET (new_binfo) = offset;
1194 BINFO_VTABLE (new_binfo) = vtable;
1195 BINFO_VIRTUALS (new_binfo) = virtuals;
1196 BINFO_VPTR_FIELD (new_binfo) = NULL_TREE;
1197
1198 if (binfo && BINFO_BASETYPES (binfo) != NULL_TREE)
1199 BINFO_BASETYPES (new_binfo) = copy_node (BINFO_BASETYPES (binfo));
1200 return new_binfo;
1201 }
1202
1203 /* Return the binfo value for ELEM in TYPE. */
1204
1205 tree
1206 binfo_value (elem, type)
1207 tree elem;
1208 tree type;
1209 {
1210 if (get_base_distance (elem, type, 0, (tree *)0) == -2)
1211 compiler_error ("base class `%s' ambiguous in binfo_value",
1212 TYPE_NAME_STRING (elem));
1213 if (elem == type)
1214 return TYPE_BINFO (type);
1215 if (TREE_CODE (elem) == RECORD_TYPE && TYPE_BINFO (elem) == type)
1216 return type;
1217 return get_binfo (elem, type, 0);
1218 }
1219
1220 tree
1221 reverse_path (path)
1222 tree path;
1223 {
1224 register tree prev = 0, tmp, next;
1225 for (tmp = path; tmp; tmp = next)
1226 {
1227 next = BINFO_INHERITANCE_CHAIN (tmp);
1228 BINFO_INHERITANCE_CHAIN (tmp) = prev;
1229 prev = tmp;
1230 }
1231 return prev;
1232 }
1233
1234 void
1235 debug_binfo (elem)
1236 tree elem;
1237 {
1238 unsigned HOST_WIDE_INT n;
1239 tree virtuals;
1240
1241 fprintf (stderr, "type \"%s\"; offset = %d\n",
1242 TYPE_NAME_STRING (BINFO_TYPE (elem)),
1243 TREE_INT_CST_LOW (BINFO_OFFSET (elem)));
1244 fprintf (stderr, "vtable type:\n");
1245 debug_tree (BINFO_TYPE (elem));
1246 if (BINFO_VTABLE (elem))
1247 fprintf (stderr, "vtable decl \"%s\"\n", IDENTIFIER_POINTER (DECL_NAME (BINFO_VTABLE (elem))));
1248 else
1249 fprintf (stderr, "no vtable decl yet\n");
1250 fprintf (stderr, "virtuals:\n");
1251 virtuals = BINFO_VIRTUALS (elem);
1252
1253 n = skip_rtti_stuff (&virtuals);
1254
1255 while (virtuals)
1256 {
1257 tree fndecl = TREE_OPERAND (FNADDR_FROM_VTABLE_ENTRY (TREE_VALUE (virtuals)), 0);
1258 fprintf (stderr, "%s [%d =? %d]\n",
1259 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (fndecl)),
1260 n, TREE_INT_CST_LOW (DECL_VINDEX (fndecl)));
1261 ++n;
1262 virtuals = TREE_CHAIN (virtuals);
1263 }
1264 }
1265
1266 /* Return the length of a chain of nodes chained through DECL_CHAIN.
1267 We expect a null pointer to mark the end of the chain.
1268 This is the Lisp primitive `length'. */
1269
1270 int
1271 decl_list_length (t)
1272 tree t;
1273 {
1274 register tree tail;
1275 register int len = 0;
1276
1277 my_friendly_assert (TREE_CODE (t) == FUNCTION_DECL
1278 || TREE_CODE (t) == TEMPLATE_DECL, 300);
1279 for (tail = t; tail; tail = DECL_CHAIN (tail))
1280 len++;
1281
1282 return len;
1283 }
1284
1285 int
1286 count_functions (t)
1287 tree t;
1288 {
1289 if (TREE_CODE (t) == FUNCTION_DECL)
1290 return 1;
1291 else if (TREE_CODE (t) == TREE_LIST)
1292 return decl_list_length (TREE_VALUE (t));
1293
1294 my_friendly_abort (359);
1295 return 0;
1296 }
1297
1298 int
1299 is_overloaded_fn (x)
1300 tree x;
1301 {
1302 if (TREE_CODE (x) == FUNCTION_DECL
1303 || TREE_CODE (x) == TEMPLATE_ID_EXPR
1304 || DECL_FUNCTION_TEMPLATE_P (x))
1305 return 1;
1306
1307 if (TREE_CODE (x) == TREE_LIST
1308 && (TREE_CODE (TREE_VALUE (x)) == FUNCTION_DECL
1309 || DECL_FUNCTION_TEMPLATE_P (TREE_VALUE (x))))
1310 return 1;
1311
1312 return 0;
1313 }
1314
1315 int
1316 really_overloaded_fn (x)
1317 tree x;
1318 {
1319 return TREE_CODE (x) != FUNCTION_DECL
1320 && is_overloaded_fn (x);
1321 }
1322
1323 tree
1324 get_first_fn (from)
1325 tree from;
1326 {
1327 if (TREE_CODE (from) == FUNCTION_DECL
1328 || TREE_CODE (from) == TEMPLATE_ID_EXPR
1329 || DECL_FUNCTION_TEMPLATE_P (from))
1330 return from;
1331
1332 my_friendly_assert (TREE_CODE (from) == TREE_LIST, 9);
1333
1334 return TREE_VALUE (from);
1335 }
1336
1337 int
1338 is_aggr_type_2 (t1, t2)
1339 tree t1, t2;
1340 {
1341 if (TREE_CODE (t1) != TREE_CODE (t2))
1342 return 0;
1343 return IS_AGGR_TYPE (t1) && IS_AGGR_TYPE (t2);
1344 }
1345 \f
1346 #define PRINT_RING_SIZE 4
1347
1348 char *
1349 lang_printable_name (decl, v)
1350 tree decl;
1351 int v;
1352 {
1353 static tree decl_ring[PRINT_RING_SIZE];
1354 static char *print_ring[PRINT_RING_SIZE];
1355 static int ring_counter;
1356 int i;
1357
1358 /* Only cache functions. */
1359 if (v < 2
1360 || TREE_CODE (decl) != FUNCTION_DECL
1361 || DECL_LANG_SPECIFIC (decl) == 0)
1362 return lang_decl_name (decl, v);
1363
1364 /* See if this print name is lying around. */
1365 for (i = 0; i < PRINT_RING_SIZE; i++)
1366 if (decl_ring[i] == decl)
1367 /* yes, so return it. */
1368 return print_ring[i];
1369
1370 if (++ring_counter == PRINT_RING_SIZE)
1371 ring_counter = 0;
1372
1373 if (current_function_decl != NULL_TREE)
1374 {
1375 if (decl_ring[ring_counter] == current_function_decl)
1376 ring_counter += 1;
1377 if (ring_counter == PRINT_RING_SIZE)
1378 ring_counter = 0;
1379 if (decl_ring[ring_counter] == current_function_decl)
1380 my_friendly_abort (106);
1381 }
1382
1383 if (print_ring[ring_counter])
1384 free (print_ring[ring_counter]);
1385
1386 print_ring[ring_counter] = xstrdup (lang_decl_name (decl, v));
1387 decl_ring[ring_counter] = decl;
1388 return print_ring[ring_counter];
1389 }
1390 \f
1391 /* Build the FUNCTION_TYPE or METHOD_TYPE which may throw exceptions
1392 listed in RAISES. */
1393
1394 tree
1395 build_exception_variant (type, raises)
1396 tree type;
1397 tree raises;
1398 {
1399 tree v = TYPE_MAIN_VARIANT (type);
1400 int constp = TYPE_READONLY (type);
1401 int volatilep = TYPE_VOLATILE (type);
1402
1403 for (; v; v = TYPE_NEXT_VARIANT (v))
1404 {
1405 if (TYPE_READONLY (v) != constp
1406 || TYPE_VOLATILE (v) != volatilep)
1407 continue;
1408
1409 /* @@ This should do set equality, not exact match. */
1410 if (simple_cst_list_equal (TYPE_RAISES_EXCEPTIONS (v), raises))
1411 /* List of exceptions raised matches previously found list.
1412
1413 @@ Nice to free up storage used in consing up the
1414 @@ list of exceptions raised. */
1415 return v;
1416 }
1417
1418 /* Need to build a new variant. */
1419 v = build_type_copy (type);
1420
1421 if (raises && ! TREE_PERMANENT (raises))
1422 {
1423 push_obstacks_nochange ();
1424 end_temporary_allocation ();
1425 raises = copy_list (raises);
1426 pop_obstacks ();
1427 }
1428
1429 TYPE_RAISES_EXCEPTIONS (v) = raises;
1430 return v;
1431 }
1432
1433 /* Given a TEMPLATE_TEMPLATE_PARM node T, create a new one together with its
1434 lang_specific field and its corresponding TEMPLATE_DECL node */
1435
1436 tree
1437 copy_template_template_parm (t)
1438 tree t;
1439 {
1440 tree template = TYPE_NAME (t);
1441 tree t2 = make_lang_type (TEMPLATE_TEMPLATE_PARM);
1442 template = copy_node (template);
1443 copy_lang_decl (template);
1444 TREE_TYPE (template) = t2;
1445 TYPE_NAME (t2) = template;
1446 TYPE_STUB_DECL (t2) = template;
1447
1448 /* No need to copy these */
1449 TYPE_FIELDS (t2) = TYPE_FIELDS (t);
1450 CLASSTYPE_TEMPLATE_INFO (t2) = CLASSTYPE_TEMPLATE_INFO (t);
1451 return t2;
1452 }
1453
1454 /* Subroutine of copy_to_permanent
1455
1456 Assuming T is a node build bottom-up, make it all exist on
1457 permanent obstack, if it is not permanent already. */
1458
1459 tree
1460 mapcar (t, func)
1461 tree t;
1462 tree (*func) PROTO((tree));
1463 {
1464 tree tmp;
1465
1466 if (t == NULL_TREE)
1467 return t;
1468
1469 if (tmp = func (t), tmp != NULL_TREE)
1470 return tmp;
1471
1472 switch (TREE_CODE (t))
1473 {
1474 case ERROR_MARK:
1475 return error_mark_node;
1476
1477 case VAR_DECL:
1478 case FUNCTION_DECL:
1479 case CONST_DECL:
1480 /* Rather than aborting, return error_mark_node. This allows us
1481 to report a sensible error message on code like this:
1482
1483 void g() { int i; f<i>(7); } */
1484 return error_mark_node;
1485
1486 case PARM_DECL:
1487 {
1488 tree chain = TREE_CHAIN (t);
1489 t = copy_node (t);
1490 TREE_CHAIN (t) = mapcar (chain, func);
1491 TREE_TYPE (t) = mapcar (TREE_TYPE (t), func);
1492 DECL_INITIAL (t) = mapcar (DECL_INITIAL (t), func);
1493 DECL_SIZE (t) = mapcar (DECL_SIZE (t), func);
1494 return t;
1495 }
1496
1497 case TREE_LIST:
1498 {
1499 tree chain = TREE_CHAIN (t);
1500 t = copy_node (t);
1501 TREE_PURPOSE (t) = mapcar (TREE_PURPOSE (t), func);
1502 TREE_VALUE (t) = mapcar (TREE_VALUE (t), func);
1503 TREE_CHAIN (t) = mapcar (chain, func);
1504 return t;
1505 }
1506
1507 case TREE_VEC:
1508 {
1509 int len = TREE_VEC_LENGTH (t);
1510
1511 t = copy_node (t);
1512 while (len--)
1513 TREE_VEC_ELT (t, len) = mapcar (TREE_VEC_ELT (t, len), func);
1514 return t;
1515 }
1516
1517 case INTEGER_CST:
1518 case REAL_CST:
1519 case STRING_CST:
1520 return copy_node (t);
1521
1522 case COND_EXPR:
1523 case TARGET_EXPR:
1524 case AGGR_INIT_EXPR:
1525 t = copy_node (t);
1526 TREE_OPERAND (t, 0) = mapcar (TREE_OPERAND (t, 0), func);
1527 TREE_OPERAND (t, 1) = mapcar (TREE_OPERAND (t, 1), func);
1528 TREE_OPERAND (t, 2) = mapcar (TREE_OPERAND (t, 2), func);
1529 return t;
1530
1531 case SAVE_EXPR:
1532 t = copy_node (t);
1533 TREE_OPERAND (t, 0) = mapcar (TREE_OPERAND (t, 0), func);
1534 return t;
1535
1536 case MODIFY_EXPR:
1537 case PLUS_EXPR:
1538 case MINUS_EXPR:
1539 case MULT_EXPR:
1540 case TRUNC_DIV_EXPR:
1541 case TRUNC_MOD_EXPR:
1542 case MIN_EXPR:
1543 case MAX_EXPR:
1544 case LSHIFT_EXPR:
1545 case RSHIFT_EXPR:
1546 case BIT_IOR_EXPR:
1547 case BIT_XOR_EXPR:
1548 case BIT_AND_EXPR:
1549 case BIT_ANDTC_EXPR:
1550 case TRUTH_ANDIF_EXPR:
1551 case TRUTH_ORIF_EXPR:
1552 case LT_EXPR:
1553 case LE_EXPR:
1554 case GT_EXPR:
1555 case GE_EXPR:
1556 case EQ_EXPR:
1557 case NE_EXPR:
1558 case CEIL_DIV_EXPR:
1559 case FLOOR_DIV_EXPR:
1560 case ROUND_DIV_EXPR:
1561 case CEIL_MOD_EXPR:
1562 case FLOOR_MOD_EXPR:
1563 case ROUND_MOD_EXPR:
1564 case COMPOUND_EXPR:
1565 case PREDECREMENT_EXPR:
1566 case PREINCREMENT_EXPR:
1567 case POSTDECREMENT_EXPR:
1568 case POSTINCREMENT_EXPR:
1569 case ARRAY_REF:
1570 case SCOPE_REF:
1571 case TRY_CATCH_EXPR:
1572 case WITH_CLEANUP_EXPR:
1573 t = copy_node (t);
1574 TREE_OPERAND (t, 0) = mapcar (TREE_OPERAND (t, 0), func);
1575 TREE_OPERAND (t, 1) = mapcar (TREE_OPERAND (t, 1), func);
1576 return t;
1577
1578 case CALL_EXPR:
1579 t = copy_node (t);
1580 TREE_TYPE (t) = mapcar (TREE_TYPE (t), func);
1581 TREE_OPERAND (t, 0) = mapcar (TREE_OPERAND (t, 0), func);
1582 TREE_OPERAND (t, 1) = mapcar (TREE_OPERAND (t, 1), func);
1583
1584 /* tree.def says that operand two is RTL, but
1585 build_call_declarator puts trees in there. */
1586 if (TREE_OPERAND (t, 2)
1587 && TREE_CODE (TREE_OPERAND (t, 2)) == TREE_LIST)
1588 TREE_OPERAND (t, 2) = mapcar (TREE_OPERAND (t, 2), func);
1589 else
1590 TREE_OPERAND (t, 2) = NULL_TREE;
1591 return t;
1592
1593 case CONVERT_EXPR:
1594 case ADDR_EXPR:
1595 case INDIRECT_REF:
1596 case NEGATE_EXPR:
1597 case BIT_NOT_EXPR:
1598 case TRUTH_NOT_EXPR:
1599 case NOP_EXPR:
1600 case COMPONENT_REF:
1601 case CLEANUP_POINT_EXPR:
1602 t = copy_node (t);
1603 TREE_OPERAND (t, 0) = mapcar (TREE_OPERAND (t, 0), func);
1604 return t;
1605
1606 case POINTER_TYPE:
1607 tmp = build_pointer_type (mapcar (TREE_TYPE (t), func));
1608 return cp_build_type_variant (tmp, TYPE_READONLY (t), TYPE_VOLATILE (t));
1609 case REFERENCE_TYPE:
1610 tmp = build_reference_type (mapcar (TREE_TYPE (t), func));
1611 return cp_build_type_variant (tmp, TYPE_READONLY (t), TYPE_VOLATILE (t));
1612 case FUNCTION_TYPE:
1613 tmp = build_function_type (mapcar (TREE_TYPE (t), func),
1614 mapcar (TYPE_ARG_TYPES (t), func));
1615 return cp_build_type_variant (tmp, TYPE_READONLY (t), TYPE_VOLATILE (t));
1616 case ARRAY_TYPE:
1617 tmp = build_cplus_array_type (mapcar (TREE_TYPE (t), func),
1618 mapcar (TYPE_DOMAIN (t), func));
1619 return cp_build_type_variant (tmp, TYPE_READONLY (t), TYPE_VOLATILE (t));
1620 case INTEGER_TYPE:
1621 tmp = build_index_type (mapcar (TYPE_MAX_VALUE (t), func));
1622 return cp_build_type_variant (tmp, TYPE_READONLY (t), TYPE_VOLATILE (t));
1623 case OFFSET_TYPE:
1624 tmp = build_offset_type (mapcar (TYPE_OFFSET_BASETYPE (t), func),
1625 mapcar (TREE_TYPE (t), func));
1626 return cp_build_type_variant (tmp, TYPE_READONLY (t), TYPE_VOLATILE (t));
1627 case METHOD_TYPE:
1628 tmp = build_cplus_method_type
1629 (mapcar (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t))), func),
1630 mapcar (TREE_TYPE (t), func),
1631 mapcar (TREE_CHAIN (TYPE_ARG_TYPES (t)), func));
1632 return cp_build_type_variant (tmp, TYPE_READONLY (t), TYPE_VOLATILE (t));
1633
1634 case COMPLEX_CST:
1635 t = copy_node (t);
1636 TREE_REALPART (t) = mapcar (TREE_REALPART (t), func);
1637 TREE_IMAGPART (t) = mapcar (TREE_REALPART (t), func);
1638 return t;
1639
1640 case CONSTRUCTOR:
1641 t = copy_node (t);
1642 CONSTRUCTOR_ELTS (t) = mapcar (CONSTRUCTOR_ELTS (t), func);
1643 return t;
1644
1645 case TEMPLATE_TEMPLATE_PARM:
1646 return copy_template_template_parm (t);
1647
1648 case RECORD_TYPE:
1649 if (TYPE_PTRMEMFUNC_P (t))
1650 return build_ptrmemfunc_type
1651 (mapcar (TYPE_PTRMEMFUNC_FN_TYPE (t), func));
1652 /* else fall through */
1653
1654 /* This list is incomplete, but should suffice for now.
1655 It is very important that `sorry' not call
1656 `report_error_function'. That could cause an infinite loop. */
1657 default:
1658 sorry ("initializer contains unrecognized tree code");
1659 return error_mark_node;
1660
1661 }
1662 my_friendly_abort (107);
1663 /* NOTREACHED */
1664 return NULL_TREE;
1665 }
1666
1667 static tree
1668 perm_manip (t)
1669 tree t;
1670 {
1671 if (TREE_PERMANENT (t))
1672 return t;
1673 /* Support `void f () { extern int i; A<&i> a; }' */
1674 if ((TREE_CODE (t) == VAR_DECL || TREE_CODE (t) == FUNCTION_DECL)
1675 && TREE_PUBLIC (t))
1676 return copy_node (t);
1677 return NULL_TREE;
1678 }
1679
1680 /* Assuming T is a node built bottom-up, make it all exist on
1681 permanent obstack, if it is not permanent already. */
1682
1683 tree
1684 copy_to_permanent (t)
1685 tree t;
1686 {
1687 register struct obstack *ambient_obstack = current_obstack;
1688 register struct obstack *ambient_saveable_obstack = saveable_obstack;
1689 register struct obstack *ambient_expression_obstack = expression_obstack;
1690
1691 if (t == NULL_TREE || TREE_PERMANENT (t))
1692 return t;
1693
1694 saveable_obstack = &permanent_obstack;
1695 current_obstack = saveable_obstack;
1696 expression_obstack = saveable_obstack;
1697
1698 t = mapcar (t, perm_manip);
1699
1700 current_obstack = ambient_obstack;
1701 saveable_obstack = ambient_saveable_obstack;
1702 expression_obstack = ambient_expression_obstack;
1703
1704 return t;
1705 }
1706
1707 #ifdef GATHER_STATISTICS
1708 extern int depth_reached;
1709 #endif
1710
1711 void
1712 print_lang_statistics ()
1713 {
1714 extern struct obstack decl_obstack;
1715 print_obstack_statistics ("class_obstack", &class_obstack);
1716 print_obstack_statistics ("decl_obstack", &decl_obstack);
1717 print_search_statistics ();
1718 print_class_statistics ();
1719 #ifdef GATHER_STATISTICS
1720 fprintf (stderr, "maximum template instantiation depth reached: %d\n",
1721 depth_reached);
1722 #endif
1723 }
1724
1725 /* This is used by the `assert' macro. It is provided in libgcc.a,
1726 which `cc' doesn't know how to link. Note that the C++ front-end
1727 no longer actually uses the `assert' macro (instead, it calls
1728 my_friendly_assert). But all of the back-end files still need this. */
1729
1730 void
1731 __eprintf (string, expression, line, filename)
1732 #ifdef __STDC__
1733 const char *string;
1734 const char *expression;
1735 unsigned line;
1736 const char *filename;
1737 #else
1738 char *string;
1739 char *expression;
1740 unsigned line;
1741 char *filename;
1742 #endif
1743 {
1744 fprintf (stderr, string, expression, line, filename);
1745 fflush (stderr);
1746 abort ();
1747 }
1748
1749 /* Return, as an INTEGER_CST node, the number of elements for TYPE
1750 (which is an ARRAY_TYPE). This counts only elements of the top
1751 array. */
1752
1753 tree
1754 array_type_nelts_top (type)
1755 tree type;
1756 {
1757 return fold (build (PLUS_EXPR, sizetype,
1758 array_type_nelts (type),
1759 integer_one_node));
1760 }
1761
1762 /* Return, as an INTEGER_CST node, the number of elements for TYPE
1763 (which is an ARRAY_TYPE). This one is a recursive count of all
1764 ARRAY_TYPEs that are clumped together. */
1765
1766 tree
1767 array_type_nelts_total (type)
1768 tree type;
1769 {
1770 tree sz = array_type_nelts_top (type);
1771 type = TREE_TYPE (type);
1772 while (TREE_CODE (type) == ARRAY_TYPE)
1773 {
1774 tree n = array_type_nelts_top (type);
1775 sz = fold (build (MULT_EXPR, sizetype, sz, n));
1776 type = TREE_TYPE (type);
1777 }
1778 return sz;
1779 }
1780
1781 static
1782 tree
1783 bot_manip (t)
1784 tree t;
1785 {
1786 if (TREE_CODE (t) != TREE_LIST && ! TREE_SIDE_EFFECTS (t))
1787 return t;
1788 else if (TREE_CODE (t) == TARGET_EXPR)
1789 {
1790 if (TREE_CODE (TREE_OPERAND (t, 1)) == AGGR_INIT_EXPR)
1791 {
1792 mark_used (TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (t, 1), 0), 0));
1793 return build_cplus_new
1794 (TREE_TYPE (t), break_out_target_exprs (TREE_OPERAND (t, 1)));
1795 }
1796 t = copy_node (t);
1797 TREE_OPERAND (t, 0) = build (VAR_DECL, TREE_TYPE (t));
1798 layout_decl (TREE_OPERAND (t, 0), 0);
1799 return t;
1800 }
1801 else if (TREE_CODE (t) == CALL_EXPR)
1802 mark_used (TREE_OPERAND (TREE_OPERAND (t, 0), 0));
1803
1804 return NULL_TREE;
1805 }
1806
1807 /* Actually, we'll just clean out the target exprs for the moment. */
1808
1809 tree
1810 break_out_target_exprs (t)
1811 tree t;
1812 {
1813 return mapcar (t, bot_manip);
1814 }
1815
1816 /* Obstack used for allocating nodes in template function and variable
1817 definitions. */
1818
1819 /* Similar to `build_nt', except we build
1820 on the permanent_obstack, regardless. */
1821
1822 tree
1823 build_min_nt VPROTO((enum tree_code code, ...))
1824 {
1825 #ifndef __STDC__
1826 enum tree_code code;
1827 #endif
1828 register struct obstack *ambient_obstack = expression_obstack;
1829 va_list p;
1830 register tree t;
1831 register int length;
1832 register int i;
1833
1834 VA_START (p, code);
1835
1836 #ifndef __STDC__
1837 code = va_arg (p, enum tree_code);
1838 #endif
1839
1840 expression_obstack = &permanent_obstack;
1841
1842 t = make_node (code);
1843 length = tree_code_length[(int) code];
1844 TREE_COMPLEXITY (t) = lineno;
1845
1846 for (i = 0; i < length; i++)
1847 {
1848 tree x = va_arg (p, tree);
1849 TREE_OPERAND (t, i) = copy_to_permanent (x);
1850 }
1851
1852 va_end (p);
1853 expression_obstack = ambient_obstack;
1854 return t;
1855 }
1856
1857 /* Similar to `build', except we build
1858 on the permanent_obstack, regardless. */
1859
1860 tree
1861 build_min VPROTO((enum tree_code code, tree tt, ...))
1862 {
1863 #ifndef __STDC__
1864 enum tree_code code;
1865 tree tt;
1866 #endif
1867 register struct obstack *ambient_obstack = expression_obstack;
1868 va_list p;
1869 register tree t;
1870 register int length;
1871 register int i;
1872
1873 VA_START (p, tt);
1874
1875 #ifndef __STDC__
1876 code = va_arg (p, enum tree_code);
1877 tt = va_arg (p, tree);
1878 #endif
1879
1880 expression_obstack = &permanent_obstack;
1881
1882 t = make_node (code);
1883 length = tree_code_length[(int) code];
1884 TREE_TYPE (t) = tt;
1885 TREE_COMPLEXITY (t) = lineno;
1886
1887 for (i = 0; i < length; i++)
1888 {
1889 tree x = va_arg (p, tree);
1890 TREE_OPERAND (t, i) = copy_to_permanent (x);
1891 }
1892
1893 va_end (p);
1894 expression_obstack = ambient_obstack;
1895 return t;
1896 }
1897
1898 /* Same as `tree_cons' but make a permanent object. */
1899
1900 tree
1901 min_tree_cons (purpose, value, chain)
1902 tree purpose, value, chain;
1903 {
1904 register tree node;
1905 register struct obstack *ambient_obstack = current_obstack;
1906 current_obstack = &permanent_obstack;
1907
1908 node = tree_cons (copy_to_permanent (purpose),
1909 copy_to_permanent (value), chain);
1910 current_obstack = ambient_obstack;
1911 return node;
1912 }
1913
1914 tree
1915 get_type_decl (t)
1916 tree t;
1917 {
1918 if (TREE_CODE (t) == TYPE_DECL)
1919 return t;
1920 if (TREE_CODE_CLASS (TREE_CODE (t)) == 't')
1921 return TYPE_STUB_DECL (t);
1922
1923 my_friendly_abort (42);
1924 }
1925
1926 int
1927 can_free (obstack, t)
1928 struct obstack *obstack;
1929 tree t;
1930 {
1931 int size;
1932
1933 if (TREE_CODE (t) == TREE_VEC)
1934 size = (TREE_VEC_LENGTH (t)-1) * sizeof (tree) + sizeof (struct tree_vec);
1935 else
1936 my_friendly_abort (42);
1937
1938 #define ROUND(x) ((x + obstack_alignment_mask (obstack)) \
1939 & ~ obstack_alignment_mask (obstack))
1940 if ((char *)t + ROUND (size) == obstack_next_free (obstack))
1941 return 1;
1942 #undef ROUND
1943
1944 return 0;
1945 }
1946
1947 /* Return first vector element whose BINFO_TYPE is ELEM.
1948 Return 0 if ELEM is not in VEC. VEC may be NULL_TREE. */
1949
1950 tree
1951 vec_binfo_member (elem, vec)
1952 tree elem, vec;
1953 {
1954 int i;
1955
1956 if (vec)
1957 for (i = 0; i < TREE_VEC_LENGTH (vec); ++i)
1958 if (comptypes (elem, BINFO_TYPE (TREE_VEC_ELT (vec, i)), 1))
1959 return TREE_VEC_ELT (vec, i);
1960
1961 return NULL_TREE;
1962 }
1963
1964 /* Kludge around the fact that DECL_CONTEXT for virtual functions returns
1965 the wrong thing for decl_function_context. Hopefully the uses in the
1966 backend won't matter, since we don't need a static chain for local class
1967 methods. FIXME! */
1968
1969 tree
1970 hack_decl_function_context (decl)
1971 tree decl;
1972 {
1973 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FUNCTION_MEMBER_P (decl))
1974 return decl_function_context (TYPE_MAIN_DECL (DECL_CLASS_CONTEXT (decl)));
1975 return decl_function_context (decl);
1976 }
1977
1978 /* Return truthvalue of whether T1 is the same tree structure as T2.
1979 Return 1 if they are the same.
1980 Return 0 if they are understandably different.
1981 Return -1 if either contains tree structure not understood by
1982 this function. */
1983
1984 int
1985 cp_tree_equal (t1, t2)
1986 tree t1, t2;
1987 {
1988 register enum tree_code code1, code2;
1989 int cmp;
1990
1991 if (t1 == t2)
1992 return 1;
1993 if (t1 == 0 || t2 == 0)
1994 return 0;
1995
1996 code1 = TREE_CODE (t1);
1997 code2 = TREE_CODE (t2);
1998
1999 if (code1 == NOP_EXPR || code1 == CONVERT_EXPR || code1 == NON_LVALUE_EXPR)
2000 if (code2 == NOP_EXPR || code2 == CONVERT_EXPR || code2 == NON_LVALUE_EXPR)
2001 return cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
2002 else
2003 return cp_tree_equal (TREE_OPERAND (t1, 0), t2);
2004 else if (code2 == NOP_EXPR || code2 == CONVERT_EXPR
2005 || code2 == NON_LVALUE_EXPR)
2006 return cp_tree_equal (t1, TREE_OPERAND (t2, 0));
2007
2008 if (code1 != code2)
2009 return 0;
2010
2011 switch (code1)
2012 {
2013 case INTEGER_CST:
2014 return TREE_INT_CST_LOW (t1) == TREE_INT_CST_LOW (t2)
2015 && TREE_INT_CST_HIGH (t1) == TREE_INT_CST_HIGH (t2);
2016
2017 case REAL_CST:
2018 return REAL_VALUES_EQUAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
2019
2020 case STRING_CST:
2021 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
2022 && !bcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
2023 TREE_STRING_LENGTH (t1));
2024
2025 case CONSTRUCTOR:
2026 abort ();
2027
2028 case SAVE_EXPR:
2029 return cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
2030
2031 case CALL_EXPR:
2032 cmp = cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
2033 if (cmp <= 0)
2034 return cmp;
2035 return simple_cst_list_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
2036
2037 case TARGET_EXPR:
2038 /* Special case: if either target is an unallocated VAR_DECL,
2039 it means that it's going to be unified with whatever the
2040 TARGET_EXPR is really supposed to initialize, so treat it
2041 as being equivalent to anything. */
2042 if ((TREE_CODE (TREE_OPERAND (t1, 0)) == VAR_DECL
2043 && DECL_NAME (TREE_OPERAND (t1, 0)) == NULL_TREE
2044 && DECL_RTL (TREE_OPERAND (t1, 0)) == 0)
2045 || (TREE_CODE (TREE_OPERAND (t2, 0)) == VAR_DECL
2046 && DECL_NAME (TREE_OPERAND (t2, 0)) == NULL_TREE
2047 && DECL_RTL (TREE_OPERAND (t2, 0)) == 0))
2048 cmp = 1;
2049 else
2050 cmp = cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
2051 if (cmp <= 0)
2052 return cmp;
2053 return cp_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
2054
2055 case WITH_CLEANUP_EXPR:
2056 cmp = cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
2057 if (cmp <= 0)
2058 return cmp;
2059 return cp_tree_equal (TREE_OPERAND (t1, 2), TREE_OPERAND (t1, 2));
2060
2061 case COMPONENT_REF:
2062 if (TREE_OPERAND (t1, 1) == TREE_OPERAND (t2, 1))
2063 return cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
2064 return 0;
2065
2066 case VAR_DECL:
2067 case PARM_DECL:
2068 case CONST_DECL:
2069 case FUNCTION_DECL:
2070 return 0;
2071
2072 case TEMPLATE_CONST_PARM:
2073 return TEMPLATE_CONST_IDX (t1) == TEMPLATE_CONST_IDX (t2)
2074 && TEMPLATE_CONST_LEVEL (t1) == TEMPLATE_CONST_LEVEL (t2);
2075
2076 case SIZEOF_EXPR:
2077 case ALIGNOF_EXPR:
2078 if (TREE_CODE (TREE_OPERAND (t1, 0)) != TREE_CODE (TREE_OPERAND (t2, 0)))
2079 return 0;
2080 if (TREE_CODE_CLASS (TREE_CODE (TREE_OPERAND (t1, 0))) == 't')
2081 return comptypes (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0), 1);
2082 break;
2083
2084 default:
2085 break;
2086 }
2087
2088 switch (TREE_CODE_CLASS (code1))
2089 {
2090 int i;
2091 case '1':
2092 case '2':
2093 case '<':
2094 case 'e':
2095 case 'r':
2096 case 's':
2097 cmp = 1;
2098 for (i=0; i<tree_code_length[(int) code1]; ++i)
2099 {
2100 cmp = cp_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i));
2101 if (cmp <= 0)
2102 return cmp;
2103 }
2104 return cmp;
2105 }
2106
2107 return -1;
2108 }
2109
2110 /* Similar to make_tree_vec, but build on a temporary obstack. */
2111
2112 tree
2113 make_temp_vec (len)
2114 int len;
2115 {
2116 register tree node;
2117 register struct obstack *ambient_obstack = current_obstack;
2118 current_obstack = expression_obstack;
2119 node = make_tree_vec (len);
2120 current_obstack = ambient_obstack;
2121 return node;
2122 }
2123
2124 void
2125 push_expression_obstack ()
2126 {
2127 push_obstacks_nochange ();
2128 current_obstack = expression_obstack;
2129 }
2130
2131 /* The type of ARG when used as an lvalue. */
2132
2133 tree
2134 lvalue_type (arg)
2135 tree arg;
2136 {
2137 return cp_build_type_variant
2138 (TREE_TYPE (arg), TREE_READONLY (arg), TREE_THIS_VOLATILE (arg));
2139 }
2140
2141 /* The type of ARG for printing error messages; denote lvalues with
2142 reference types. */
2143
2144 tree
2145 error_type (arg)
2146 tree arg;
2147 {
2148 tree type = TREE_TYPE (arg);
2149 if (TREE_CODE (type) == ARRAY_TYPE)
2150 ;
2151 else if (real_lvalue_p (arg))
2152 type = build_reference_type (lvalue_type (arg));
2153 else if (IS_AGGR_TYPE (type))
2154 type = lvalue_type (arg);
2155
2156 return type;
2157 }
2158
2159 /* Does FUNCTION use a variable-length argument list? */
2160
2161 int
2162 varargs_function_p (function)
2163 tree function;
2164 {
2165 tree parm = TYPE_ARG_TYPES (TREE_TYPE (function));
2166 for (; parm; parm = TREE_CHAIN (parm))
2167 if (TREE_VALUE (parm) == void_type_node)
2168 return 0;
2169 return 1;
2170 }