No empty .Rs/.Re
[netbsd-mini2440.git] / gnu / usr.bin / g++ / cc1plus / cplus-tree.c
blob9387d985e03d1a20cdef3d82ec4ecc00784a2b78
1 /* Language-depednent node constructors for parse phase of GNU compiler.
2 Copyright (C) 1987, 1988 Free Software Foundation, Inc.
3 Hacked by Michael Tiemann (tiemann@mcc.com)
5 This file is part of GNU CC.
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 1, or (at your option)
10 any later version.
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.
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, 675 Mass Ave, Cambridge, MA 02139, USA. */
21 #include "config.h"
22 #include <stdio.h>
23 #include "obstack.h"
24 #include "tree.h"
25 #include "cplus-tree.h"
26 #include "flags.h"
27 #include "assert.h"
29 #define MAX(x,y) ((x) > (y) ? (x) : (y))
30 #define MIN(x,y) ((x) < (y) ? (x) : (y))
31 #define CEIL(x,y) (((x) + (y) - 1) / (y))
33 /* Return nonzero if REF is an lvalue valid for this language.
34 Lvalues can be assigned, unless they have TREE_READONLY.
35 Lvalues can have their address taken, unless they have TREE_REGDECL. */
37 int
38 lvalue_p (ref)
39 tree ref;
41 register enum tree_code code = TREE_CODE (ref);
43 if (language_lvalue_valid (ref))
44 switch (code)
46 case COMPONENT_REF:
47 return lvalue_p (TREE_OPERAND (ref, 0));
49 case STRING_CST:
50 return 1;
52 case VAR_DECL:
53 if (TREE_READONLY (ref) && ! TREE_STATIC (ref)
54 && DECL_LANG_SPECIFIC (ref)
55 && DECL_IN_AGGR_P (ref))
56 return 0;
57 case INDIRECT_REF:
58 case ARRAY_REF:
59 case PARM_DECL:
60 case RESULT_DECL:
61 case ERROR_MARK:
62 if (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
63 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE)
64 return 1;
65 break;
67 case NEW_EXPR:
68 return 1;
70 case CALL_EXPR:
71 if (TREE_CODE (TREE_TYPE (ref)) == REFERENCE_TYPE
72 /* unary_complex_lvalue knows how to deal with this case. */
73 || TREE_ADDRESSABLE (TREE_TYPE (ref)))
74 return 1;
75 break;
77 /* A currently unresolved scope ref. */
78 case SCOPE_REF:
79 abort ();
80 case OFFSET_REF:
81 if (TREE_CODE (TREE_OPERAND (ref, 1)) == FUNCTION_DECL)
82 return 1;
83 if (TREE_CODE (TREE_OPERAND (ref, 1)) == VAR_DECL)
84 if (TREE_READONLY (ref) && ! TREE_STATIC (ref)
85 && DECL_LANG_SPECIFIC (ref)
86 && DECL_IN_AGGR_P (ref))
87 return 0;
88 else
89 return 1;
90 break;
92 return 0;
95 /* Return nonzero if REF is an lvalue valid for this language;
96 otherwise, print an error message and return zero. */
98 int
99 lvalue_or_else (ref, string)
100 tree ref;
101 char *string;
103 int win = lvalue_p (ref);
104 if (! win)
105 error ("invalid lvalue in %s", string);
106 return win;
109 /* INIT is a CALL_EXPR which needs info about its target.
110 TYPE is the type that this initialization should appear to have.
112 Build an encapsultation of the initialization to perfom
113 and return it so that it can be processed by language-independent
114 and language-specific expression expanders. */
115 tree
116 build_cplus_new (type, init)
117 tree type;
118 tree init;
120 tree slot = build (VAR_DECL, type);
121 tree rval = build (CPLUS_NEW_EXPR, type,
122 TREE_OPERAND (init, 0), TREE_OPERAND (init, 1), slot);
123 TREE_ADDRESSABLE (rval) = 1;
124 rval = build (NEW_EXPR, type, slot, rval, 0);
125 TREE_ADDRESSABLE (rval) = 1;
126 return rval;
129 extern struct obstack *current_obstack;
130 extern struct obstack permanent_obstack, class_obstack;
131 extern struct obstack *saveable_obstack;
133 /* Return a type like TYPE except that its CLASSTYPE_OFFSET
134 is OFFSET.
136 Such variant types already made are recorded so that duplicates
137 are not made.
139 A variant types should never be used as the type of an expression.
140 Use TYPE_MAIN_VARIANT to find the main variant. */
142 tree
143 build_classtype_variant (type, offset, virtualp)
144 tree type;
145 tree offset;
146 int virtualp;
148 register tree t, m = CLASSTYPE_MAIN_VARIANT (type);
149 register struct obstack *ambient_obstack = current_obstack;
150 register struct obstack *ambient_saveable_obstack = saveable_obstack;
151 register int lo = TREE_INT_CST_LOW (offset);
152 register int hi = TREE_INT_CST_HIGH (offset);
153 /* First search the chain variants for one that is what we want. */
155 if (hi == 0 && lo == 0)
156 offset = integer_zero_node;
158 for (t = m; t; t = CLASSTYPE_NEXT_VARIANT (t))
159 if (virtualp == TREE_VIA_VIRTUAL (t)
160 && lo == TREE_INT_CST_LOW (CLASSTYPE_OFFSET (t))
161 && hi == TREE_INT_CST_HIGH (CLASSTYPE_OFFSET (t)))
162 return t;
164 /* We need a new one. */
165 if (TREE_PERMANENT (type))
166 saveable_obstack = &permanent_obstack;
167 current_obstack = saveable_obstack;
169 t = copy_node (type);
170 copy_type_lang_specific (t);
171 CLASSTYPE_AS_LIST (t) = build_tree_list (NULL_TREE, t);
173 TYPE_POINTER_TO (t) = 0;
174 TYPE_REFERENCE_TO (t) = 0;
175 CLASSTYPE_OFFSET (t) = offset;
176 TREE_VIA_VIRTUAL (t) = virtualp;
178 /* Always promise to have TYPE_POINTER_TO filled in. */
179 build_pointer_type (t);
181 /* Add this type to the chain of variants of TYPE. */
182 CLASSTYPE_NEXT_VARIANT (t) = CLASSTYPE_NEXT_VARIANT (m);
183 CLASSTYPE_NEXT_VARIANT (m) = t;
185 current_obstack = ambient_obstack;
186 saveable_obstack = ambient_saveable_obstack;
187 return t;
190 /* Here is how primitive or already-canonicalized types' hash
191 codes are made. MUST BE CONSISTENT WITH tree.c !!! */
192 #define TYPE_HASH(TYPE) TREE_UID (TYPE)
194 /* Construct, lay out and return the type of methods belonging to class
195 BASETYPE and whose arguments and values are described by TYPE.
196 If that type exists already, reuse it.
197 TYPE must be a FUNCTION_TYPE node. */
199 tree
200 build_cplus_method_type (basetype, rettype, argtypes)
201 tree basetype, rettype, argtypes;
203 register tree t;
204 tree ptype = build_pointer_type (basetype);
205 int hashcode;
207 /* Make a node of the sort we want. */
208 t = make_node (METHOD_TYPE);
210 TYPE_METHOD_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
211 TREE_TYPE (t) = rettype;
212 ptype = build_type_variant (ptype, !flag_this_is_variable, 0);
214 /* The actual arglist for this function includes a "hidden" argument
215 which is "this". Put it into the list of argument types. */
217 TYPE_ARG_TYPES (t) = tree_cons (NULL, ptype, argtypes);
219 /* If we already have such a type, use the old one and free this one.
220 Note that it also frees up the above cons cell if found. */
221 hashcode = TYPE_HASH (basetype) + TYPE_HASH (rettype) + type_hash_list (argtypes);
222 t = type_hash_canon (hashcode, t);
224 if (TYPE_SIZE (t) == 0)
225 layout_type (t);
227 return t;
230 tree
231 build_cplus_array_type (elt_type, index_type)
232 tree elt_type;
233 tree index_type;
235 register struct obstack *ambient_obstack = current_obstack;
236 register struct obstack *ambient_saveable_obstack = saveable_obstack;
237 tree t;
239 /* We need a new one. If ELT_TYPE is permanent, make this permanent too. */
240 if (TREE_PERMANENT (elt_type))
242 current_obstack = &permanent_obstack;
243 saveable_obstack = &permanent_obstack;
246 t = build_array_type (elt_type, index_type);
248 /* Push these needs up so that initialization takes place
249 more easily. */
250 TYPE_NEEDS_CONSTRUCTING (t) = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (elt_type));
251 TYPE_NEEDS_DESTRUCTOR (t) = TYPE_NEEDS_DESTRUCTOR (TYPE_MAIN_VARIANT (elt_type));
252 current_obstack = ambient_obstack;
253 saveable_obstack = ambient_saveable_obstack;
254 return t;
257 /* This is temporary until later. */
258 /* Construct, lay out and return the type of objects which are of type TYPE
259 as members of type BASETYPE. If that type exists already, reuse it. */
260 tree
261 build_member_type (basetype, type)
262 tree basetype, type;
264 register tree t;
265 int hashcode;
267 assert (TREE_CODE (type) != FUNCTION_TYPE);
269 /* Make a node of the sort we want. */
270 t = make_node (OFFSET_TYPE);
271 TYPE_OFFSET_BASETYPE (t) = basetype;
272 TREE_TYPE (t) = type;
273 hashcode = TREE_UID (basetype) + TREE_UID (type);
274 t = type_hash_canon (hashcode, t);
276 return t;
279 /* Compute the actual offsets that our virtual base classes
280 will have *for this type*. This must be performed after
281 the fields are laid out, since virtual baseclasses must
282 lay down at the end of the record.
284 Returns the maximum number of virtual functions any of the virtual
285 baseclasses provide. */
287 layout_vbasetypes (rec, max)
288 tree rec;
289 int max;
291 /* Get all the virtual base types that this type uses.
292 The TREE_VALUE slot holds the virtual baseclass type. */
293 tree vbase_types = get_vbase_types (rec);
295 #ifdef STRUCTURE_SIZE_BOUNDARY
296 int record_align = MAX (STRUCTURE_SIZE_BOUNDARY, TYPE_ALIGN (rec));
297 #else
298 int record_align = MAX (BITS_PER_UNIT, TYPE_ALIGN (rec));
299 #endif
301 /* Record size so far is CONST_SIZE + VAR_SIZE * SIZE_UNIT bits,
302 where CONST_SIZE is an integer
303 and VAR_SIZE is a tree expression.
304 If VAR_SIZE is null, the size is just CONST_SIZE.
305 Naturally we try to avoid using VAR_SIZE. */
306 register int const_size = 0;
307 register tree var_size = 0;
308 register int size_unit = BITS_PER_UNIT;
309 int nonvirtual_const_size;
310 tree nonvirtual_var_size;
312 CLASSTYPE_VBASECLASSES (rec) = vbase_types;
314 if (TREE_CODE (TYPE_SIZE (rec)) == INTEGER_CST)
315 const_size = TREE_INT_CST_LOW (TYPE_SIZE (rec)) * TYPE_SIZE_UNIT (rec);
316 else
318 var_size = TYPE_SIZE (rec);
319 size_unit = record_align;
322 nonvirtual_const_size = const_size;
323 nonvirtual_var_size = var_size;
325 while (vbase_types)
327 int inc;
328 tree basetype = ASSOC_TYPE (vbase_types);
329 tree offset;
331 if (const_size == 0)
332 offset = integer_zero_node;
333 else
334 offset = convert_units (build_int (const_size), 1, BITS_PER_UNIT);
336 if (CLASSTYPE_VSIZE (basetype) > max)
337 max = CLASSTYPE_VSIZE (basetype);
338 ASSOC_OFFSET (vbase_types) = offset;
340 inc = MAX (record_align,
341 (TREE_INT_CST_LOW (TYPE_SIZE (basetype))
342 - TREE_INT_CST_LOW (CLASSTYPE_VBASE_SIZE (basetype)))
343 * TYPE_SIZE_UNIT (basetype));
345 const_size += inc;
346 vbase_types = TREE_CHAIN (vbase_types);
349 if (const_size - nonvirtual_const_size)
351 CLASSTYPE_VBASE_SIZE (rec) = convert_units (build_int (const_size - nonvirtual_const_size),
352 1, BITS_PER_UNIT);
353 TYPE_SIZE (rec) = convert_units (build_int (const_size), 1, BITS_PER_UNIT);
355 else
356 CLASSTYPE_VBASE_SIZE (rec) = integer_zero_node;
357 return max;
360 #if 0
361 /* This function should never be needed. */
362 void
363 fixup_vbase_offsets (type)
364 tree type;
366 tree virtuals = TREE_CHAIN (CLASS_ASSOC_VIRTUALS (type));
368 while (virtuals)
370 tree pfn = FNADDR_FROM_VTABLE_ENTRY (TREE_VALUE (virtuals));
371 tree decl = TREE_OPERAND (pfn, 0);
372 tree vcontext = get_base_type (DECL_VCONTEXT (decl), DECL_CONTEXT (decl), 0);
373 if (vcontext != NULL_TREE && TREE_VIA_VIRTUAL (vcontext))
375 tree offset;
376 tree vbase_offset_info;
377 tree parent_type;
379 if (DECL_CONTEXT (decl) == TYPE_MAIN_VARIANT (type))
380 parent_type = type;
381 else
383 parent_type = get_base_type (DECL_CONTEXT (decl), type, 0);
384 if (parent_type == 0)
385 parent_type = type;
387 vbase_offset_info = value_member (vcontext,
388 CLASSTYPE_VBASECLASSES (parent_type));
389 offset = genop (MINUS_EXPR, CLASSTYPE_OFFSET (parent_type),
390 TREE_PURPOSE (vbase_offset_info));
391 TREE_VALUE (virtuals) = build_vtable_entry (offset, pfn);
393 virtuals = TREE_CHAIN (virtuals);
396 #endif
398 /* Lay out the base types of a record type, REC.
399 Tentatively set the size and alignment of REC
400 according to the base types alone.
402 Returns list of virtual base classes in a FIELD_DECL chain. */
403 tree
404 layout_basetypes (rec)
405 tree rec;
407 /* Chain to hold all the new FIELD_DECLs which point at virtual
408 base classes. */
409 tree vbase_decls = NULL_TREE;
411 #ifdef STRUCTURE_SIZE_BOUNDARY
412 int record_align = MAX (STRUCTURE_SIZE_BOUNDARY, TYPE_ALIGN (rec));
413 #else
414 int record_align = MAX (BITS_PER_UNIT, TYPE_ALIGN (rec));
415 #endif
417 /* Record size so far is CONST_SIZE + VAR_SIZE * SIZE_UNIT bits,
418 where CONST_SIZE is an integer
419 and VAR_SIZE is a tree expression.
420 If VAR_SIZE is null, the size is just CONST_SIZE.
421 Naturally we try to avoid using VAR_SIZE. */
422 register int const_size = 0;
423 register tree var_size = 0;
424 register int size_unit = BITS_PER_UNIT;
425 int i, n_baseclasses = CLASSTYPE_N_BASECLASSES (rec);
427 /* Handle basetypes almost like fields, but record their
428 offsets differently. */
430 for (i = 1; i <= n_baseclasses; i++)
432 int inc, desired_align;
433 register tree basetype = CLASSTYPE_BASECLASS (rec, i);
434 tree decl;
436 if (TYPE_SIZE (basetype) == 0)
438 error_with_aggr_type (basetype, "base class `%s' has incomplete type");
439 SET_CLASSTYPE_VIAS (rec, i, 1, 0);
440 continue;
443 /* All basetypes are recorded in the association list of the
444 derived type. */
446 if (CLASSTYPE_VIA_VIRTUAL (rec, i))
448 int j;
449 char *name = (char *)alloca (TYPE_NAME_LENGTH (basetype)
450 + sizeof (VBASE_NAME) + 1);
451 sprintf (name, VBASE_NAME_FORMAT, TYPE_NAME_STRING (basetype));
453 /* The offset for a virtual base class is only
454 used in computing virtual function tables and
455 for initializing virtual base pointers. The assoc
456 for this base type is built once `get_vbase_types'
457 is called. */
458 CLASSTYPE_BASECLASS (rec, i) = basetype
459 = build_classtype_variant (basetype, integer_zero_node, 1);
461 /* If this basetype can come from another vbase pointer
462 without an additional indirection, we will share
463 that pointer. If an indirection is involved, we
464 make our own pointer. */
465 for (j = 1; j <= n_baseclasses; j++)
466 if (! CLASSTYPE_VIA_VIRTUAL (rec, j)
467 && TYPE_USES_VIRTUAL_BASECLASSES (CLASSTYPE_BASECLASS (rec, j))
468 && value_member (TYPE_MAIN_VARIANT (basetype),
469 CLASSTYPE_VBASECLASSES (CLASSTYPE_BASECLASS (rec, j))))
471 goto got_it;
474 decl = build_lang_decl (FIELD_DECL, get_identifier (name),
475 build_pointer_type (basetype));
476 DECL_FIELD_CONTEXT (decl) = rec;
477 SET_DECL_FCONTEXT (decl, TYPE_MAIN_VARIANT (basetype));
478 DECL_VBASE_P (decl) = 1;
479 TREE_CHAIN (decl) = vbase_decls;
480 vbase_decls = decl;
482 got_it:
483 /* The space this decl occupies has already been accounted for. */
484 continue;
486 else
488 tree class_offset;
489 tree assoc;
491 if (const_size == 0)
492 class_offset = integer_zero_node;
493 else
495 /* Give each base type the alignment it wants. */
496 const_size = CEIL (const_size, TYPE_ALIGN (basetype))
497 * TYPE_ALIGN (basetype);
498 class_offset = convert_units (build_int (const_size), 1, BITS_PER_UNIT);
499 CLASSTYPE_BASECLASS (rec, i) = basetype
500 = build_classtype_variant (basetype, class_offset, 0);
503 if (CLASSTYPE_VSIZE (basetype))
504 assoc = make_assoc (class_offset, basetype,
505 CLASS_ASSOC_VTABLE (basetype),
506 CLASS_ASSOC_VIRTUALS (basetype),
507 CLASSTYPE_ASSOC (rec));
508 else
509 assoc = make_assoc (class_offset, basetype, 0, 0,
510 CLASSTYPE_ASSOC (rec));
511 CLASSTYPE_ASSOC (rec) = assoc;
512 if (const_size != 0)
514 TYPE_NAME (basetype) = copy_node (TYPE_NAME (basetype));
515 TREE_TYPE (TYPE_NAME (basetype)) = basetype;
516 DECL_OFFSET (TYPE_NAME (basetype)) = const_size;
520 /* Add only the amount of storage not present in
521 the virtual baseclasses. */
522 inc = MAX (record_align,
523 (TREE_INT_CST_LOW (TYPE_SIZE (basetype))
524 - TREE_INT_CST_LOW (CLASSTYPE_VBASE_SIZE (basetype)))
525 * TYPE_SIZE_UNIT (basetype));
527 /* Record must have at least as much alignment as any field. */
528 desired_align = TYPE_ALIGN (basetype);
529 record_align = MAX (record_align, desired_align);
531 const_size += inc;
534 if (const_size)
535 CLASSTYPE_SIZE (rec) = build_int_2 (const_size, 0);
536 else
537 CLASSTYPE_SIZE (rec) = integer_zero_node;
538 CLASSTYPE_ALIGN (rec) = record_align;
540 return vbase_decls;
543 /* Hashing of lists so that we don't make duplicates.
544 The entry point is `list_hash_canon'. */
546 /* Each hash table slot is a bucket containing a chain
547 of these structures. */
549 struct list_hash
551 struct list_hash *next; /* Next structure in the bucket. */
552 int hashcode; /* Hash code of this list. */
553 tree list; /* The list recorded here. */
556 /* Now here is the hash table. When recording a list, it is added
557 to the slot whose index is the hash code mod the table size.
558 Note that the hash table is used for several kinds of lists.
559 While all these live in the same table, they are completely independent,
560 and the hash code is computed differently for each of these. */
562 #define TYPE_HASH_SIZE 59
563 struct list_hash *list_hash_table[TYPE_HASH_SIZE];
565 /* Here is how primitive or already-canonicalized lists' hash
566 codes are made. */
567 #define TYPE_HASH(TYPE) TREE_UID (TYPE)
569 /* Compute a hash code for a list (chain of TREE_LIST nodes
570 with goodies in the TREE_PURPOSE, TREE_VALUE, and bits of the
571 TREE_COMMON slots), by adding the hash codes of the individual entries. */
574 list_hash (list)
575 tree list;
577 register int hashcode = 0;
579 if (TREE_CHAIN (list))
580 hashcode = TYPE_HASH (TREE_CHAIN (list));
581 else
582 hashcode = 0;
583 if (TREE_VALUE (list))
584 hashcode += TYPE_HASH (TREE_VALUE (list));
585 else
586 hashcode += 1007;
587 if (TREE_PURPOSE (list))
588 hashcode += TYPE_HASH (TREE_PURPOSE (list));
589 else
590 hashcode += 1009;
591 return hashcode;
594 /* Look in the type hash table for a type isomorphic to TYPE.
595 If one is found, return it. Otherwise return 0. */
597 tree
598 list_hash_lookup (hashcode, list)
599 int hashcode;
600 tree list;
602 register struct list_hash *h;
603 for (h = list_hash_table[hashcode % TYPE_HASH_SIZE]; h; h = h->next)
604 if (h->hashcode == hashcode
605 && TREE_VIA_VIRTUAL (h->list) == TREE_VIA_VIRTUAL (list)
606 && TREE_VIA_PUBLIC (h->list) == TREE_VIA_PUBLIC (list)
607 && TREE_PURPOSE (h->list) == TREE_PURPOSE (list)
608 && TREE_VALUE (h->list) == TREE_VALUE (list))
610 assert (TREE_TYPE (h->list) == TREE_TYPE (list));
611 assert (TREE_CHAIN (h->list) == TREE_CHAIN (list));
612 return h->list;
614 return 0;
617 /* Add an entry to the list-hash-table
618 for a list TYPE whose hash code is HASHCODE. */
620 void
621 list_hash_add (hashcode, list)
622 int hashcode;
623 tree list;
625 register struct list_hash *h;
627 h = (struct list_hash *) obstack_alloc (&class_obstack, sizeof (struct list_hash));
628 h->hashcode = hashcode;
629 h->list = list;
630 h->next = list_hash_table[hashcode % TYPE_HASH_SIZE];
631 list_hash_table[hashcode % TYPE_HASH_SIZE] = h;
634 /* Given TYPE, and HASHCODE its hash code, return the canonical
635 object for an identical list if one already exists.
636 Otherwise, return TYPE, and record it as the canonical object
637 if it is a permanent object.
639 To use this function, first create a list of the sort you want.
640 Then compute its hash code from the fields of the list that
641 make it different from other similar lists.
642 Then call this function and use the value.
643 This function frees the list you pass in if it is a duplicate. */
645 /* Set to 1 to debug without canonicalization. Never set by program. */
646 int debug_no_list_hash = 0;
648 tree
649 list_hash_canon (hashcode, list)
650 int hashcode;
651 tree list;
653 tree t1;
655 if (debug_no_list_hash)
656 return list;
658 t1 = list_hash_lookup (hashcode, list);
659 if (t1 != 0)
661 obstack_free (&class_obstack, list);
662 return t1;
665 /* If this is a new list, record it for later reuse. */
666 list_hash_add (hashcode, list);
668 return list;
671 tree
672 hash_tree_cons (via_public, via_virtual, purpose, value, chain)
673 int via_public, via_virtual;
674 tree purpose, value, chain;
676 struct obstack *ambient_obstack = current_obstack;
677 tree t;
678 int hashcode;
680 current_obstack = &class_obstack;
681 t = tree_cons (purpose, value, chain);
682 TREE_VIA_PUBLIC (t) = via_public;
683 TREE_VIA_VIRTUAL (t) = via_virtual;
684 hashcode = list_hash (t);
685 t = list_hash_canon (hashcode, t);
686 current_obstack = ambient_obstack;
687 return t;
690 /* Constructor for hashed lists. */
691 tree
692 hash_tree_chain (value, chain)
693 tree value, chain;
695 struct obstack *ambient_obstack = current_obstack;
696 tree t;
697 int hashcode;
699 current_obstack = &class_obstack;
700 t = tree_cons (NULL_TREE, value, chain);
701 hashcode = list_hash (t);
702 t = list_hash_canon (hashcode, t);
703 current_obstack = ambient_obstack;
704 return t;
707 /* Similar, but used for concatenating two lists. */
708 tree
709 hash_chainon (list1, list2)
710 tree list1, list2;
712 if (list2 == 0)
713 return list1;
714 if (list1 == 0)
715 return list2;
716 if (TREE_CHAIN (list1) == NULL_TREE)
717 return hash_tree_chain (TREE_VALUE (list1), list2);
718 return hash_tree_chain (TREE_VALUE (list1),
719 hash_chainon (TREE_CHAIN (list1), list2));
722 tree
723 build_decl_list_1 (value)
724 tree value;
726 tree list = NULL_TREE;
728 if (TREE_CODE (value) == IDENTIFIER_NODE)
730 list = IDENTIFIER_AS_LIST (value);
731 if (list != NULL_TREE
732 && (TREE_CODE (list) != TREE_LIST
733 || TREE_VALUE (list) != value))
734 list = NULL_TREE;
735 else if (TREE_TYPE (value) != NULL_TREE
736 && TREE_CODE (TREE_TYPE (TREE_TYPE (value))) == RECORD_TYPE)
738 tree type = TREE_TYPE (TREE_TYPE (value));
739 if (CLASSTYPE_AS_ID_LIST (type) == NULL_TREE)
740 CLASSTYPE_AS_ID_LIST (type) = perm_tree_cons (NULL_TREE, value, NULL_TREE);
741 list = CLASSTYPE_AS_ID_LIST (type);
744 else if (TREE_CODE (value) == RECORD_TYPE
745 && TYPE_LANG_SPECIFIC (value))
746 list = CLASSTYPE_AS_LIST (value);
748 if (list != NULL_TREE)
750 assert (TREE_CHAIN (list) == NULL_TREE);
751 return list;
754 return build_decl_list (NULL_TREE, value);
757 /* Look in the type hash table for a type isomorphic to
758 `build_tree_list (NULL_TREE, VALUE)'.
759 If one is found, return it. Otherwise return 0. */
761 tree
762 list_hash_lookup_or_cons (value)
763 tree value;
765 register int hashcode = TYPE_HASH (value);
766 register struct list_hash *h;
767 struct obstack *ambient_obstack;
768 tree list = NULL_TREE;
770 if (TREE_CODE (value) == IDENTIFIER_NODE)
772 list = IDENTIFIER_AS_LIST (value);
773 if (list != NULL_TREE
774 && (TREE_CODE (list) != TREE_LIST
775 || TREE_VALUE (list) != value))
776 list = NULL_TREE;
777 else if (TREE_TYPE (value) != NULL_TREE
778 && TREE_CODE (TREE_TYPE (TREE_TYPE (value))) == RECORD_TYPE)
780 tree type = TREE_TYPE (TREE_TYPE (value));
781 if (CLASSTYPE_AS_ID_LIST (type) == NULL_TREE)
782 CLASSTYPE_AS_ID_LIST (type) = perm_tree_cons (NULL_TREE, value, NULL_TREE);
783 list = CLASSTYPE_AS_ID_LIST (type);
786 else if (TREE_CODE (value) == TYPE_DECL
787 && TREE_CODE (TREE_TYPE (value)) == RECORD_TYPE
788 && TYPE_LANG_SPECIFIC (TREE_TYPE (value)))
789 list = CLASSTYPE_AS_ID_LIST (TREE_TYPE (value));
790 else if (TREE_CODE (value) == RECORD_TYPE
791 && TYPE_LANG_SPECIFIC (value))
792 list = CLASSTYPE_AS_LIST (value);
794 if (list != NULL_TREE)
796 assert (TREE_CHAIN (list) == NULL_TREE);
797 return list;
800 if (debug_no_list_hash)
801 return hash_tree_chain (value, NULL_TREE);
803 for (h = list_hash_table[hashcode % TYPE_HASH_SIZE]; h; h = h->next)
804 if (h->hashcode == hashcode
805 && TREE_VIA_VIRTUAL (h->list) == 0
806 && TREE_VIA_PUBLIC (h->list) == 0
807 && TREE_PURPOSE (h->list) == 0
808 && TREE_VALUE (h->list) == value)
810 assert (TREE_TYPE (h->list) == 0);
811 assert (TREE_CHAIN (h->list) == 0);
812 return h->list;
815 ambient_obstack = current_obstack;
816 current_obstack = &class_obstack;
817 list = build_tree_list (NULL_TREE, value);
818 list_hash_add (hashcode, list);
819 current_obstack = ambient_obstack;
820 return list;
823 /* Build an association between TYPE and some parameters:
825 OFFSET is the offset added to `this' to convert it to a pointer
826 of type `TYPE *'
828 VTABLE is the virtual function table with which to initialize
829 sub-objects of type TYPE.
831 VIRTUALS are the virtual functions sitting in VTABLE.
833 CHAIN are more associations we must retain. */
835 tree
836 make_assoc (offset, type, vtable, virtuals, chain)
837 tree offset, type;
838 tree vtable, virtuals;
839 tree chain;
841 tree assoc = make_tree_vec (4);
843 TREE_TYPE (assoc) = type;
844 TREE_CHAIN (assoc) = chain;
845 if (chain)
846 TREE_USED (assoc) = TREE_USED (chain);
848 /* n.b.: TREE_VEC_ELT (assoc, 0) <=> TREE_VALUE (assoc). */
849 TREE_VEC_ELT (assoc, 0) = TYPE_MAIN_VARIANT (type);
850 TREE_VEC_ELT (assoc, 1) = offset;
851 TREE_VEC_ELT (assoc, 2) = vtable;
852 TREE_VEC_ELT (assoc, 3) = virtuals;
853 return assoc;
856 tree
857 copy_assoc (list)
858 tree list;
860 tree assoc = copy_list (list);
861 tree rval = assoc;
862 while (assoc)
864 TREE_USED (assoc) = 0;
865 assoc = TREE_CHAIN (assoc);
867 return rval;
870 tree
871 assoc_value (elem, type)
872 tree elem;
873 tree type;
875 tree assoc = CLASSTYPE_ASSOC (type);
876 tree rval = NULL_TREE;
878 /* Dispose quickly of degenerate case. */
879 if (elem == type)
880 return assoc;
882 while (assoc)
884 if (elem == ASSOC_VALUE (assoc))
885 /* If we find it on the main spine, then
886 there can be no ambiguity. */
887 return assoc;
889 if (ASSOC_VALUE (assoc) != type)
891 tree nval = assoc_value (elem, ASSOC_TYPE (assoc));
893 if (nval)
894 if (rval && ASSOC_TYPE (rval) != ASSOC_TYPE (nval))
895 /* If we find it underneath, we must make sure that
896 there are no two ways to do it. */
897 compiler_error ("base class `%s' ambiguous in assoc_value",
898 TYPE_NAME_STRING (elem));
899 else
900 rval = nval;
902 assoc = TREE_CHAIN (assoc);
904 return rval;
907 tree
908 virtual_member (elem, list)
909 tree elem;
910 tree list;
912 tree t;
913 tree rval, nval;
915 for (t = list; t; t = TREE_CHAIN (t))
916 if (elem == TREE_VALUE (t))
917 return t;
918 rval = 0;
919 for (t = list; t; t = TREE_CHAIN (t))
921 int i;
922 for (i = CLASSTYPE_N_BASECLASSES (TREE_TYPE (t)); i > 0; i--)
924 nval = assoc_value (elem, CLASSTYPE_BASECLASS (TREE_TYPE (t), i));
925 if (nval)
927 if (rval && TREE_TYPE (nval) != TREE_TYPE (rval))
928 abort ();
929 rval = nval;
933 return rval;
936 void
937 debug_dump_assoc (elem)
938 tree elem;
940 int i;
941 tree virtuals;
943 fprintf (stderr, "type \"%s\"; offset = %d\n",
944 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (ASSOC_VALUE (elem)))),
945 TREE_INT_CST_LOW (ASSOC_OFFSET (elem)));
946 fprintf (stderr, "vtable type:\n");
947 dump_tree (stderr, ASSOC_TYPE (elem));
948 if (ASSOC_VTABLE (elem))
949 fprintf (stderr, "vtable decl \"%s\"\n", IDENTIFIER_POINTER (DECL_NAME (ASSOC_VTABLE (elem))));
950 else
951 fprintf (stderr, "no vtable decl yet\n");
952 fprintf (stderr, "virtuals:\n");
953 virtuals = ASSOC_VIRTUALS (elem);
954 if (virtuals != 0)
955 virtuals = TREE_CHAIN (virtuals);
956 i = 1;
957 while (virtuals)
959 tree fndecl = TREE_OPERAND (FNADDR_FROM_VTABLE_ENTRY (TREE_VALUE (virtuals)), 0);
960 fprintf (stderr, "%s [%d =? %d]\n",
961 IDENTIFIER_POINTER (DECL_NAME (fndecl)),
962 i, TREE_INT_CST_LOW (DECL_VINDEX (fndecl)));
963 virtuals = TREE_CHAIN (virtuals);
964 i += 1;
968 char *
969 lang_printable_name (decl)
970 tree decl;
972 if (TREE_CODE (decl) != FUNCTION_DECL
973 || DECL_LANG_SPECIFIC (decl) == 0)
975 if (DECL_NAME (decl))
977 if (THIS_NAME_P (DECL_NAME (decl)))
978 return "this";
979 return IDENTIFIER_POINTER (DECL_NAME (decl));
981 return "((anonymous))";
983 if (DECL_PRINT_NAME (decl) == 0)
985 int print_ret_type_p
986 = (!DECL_CONSTRUCTOR_P (decl)
987 && !DESTRUCTOR_NAME_P (DECL_NAME (decl)));
988 int temp = allocation_temporary_p ();
989 char *buf = (char *)alloca (8192);
990 char *name = (char *)fndecl_as_string (buf, 0, decl, print_ret_type_p);
991 end_temporary_allocation ();
992 DECL_PRINT_NAME (decl) = oballoc (strlen (name) + 1);
993 strcpy (DECL_PRINT_NAME (decl), name);
994 if (temp)
995 resume_temporary_allocation ();
997 else if (DECL_NAME (decl) == 0)
998 DECL_PRINT_NAME (decl) = "((anonymous))";
1000 return DECL_PRINT_NAME (decl);
1003 /* Return truthvalue about whether debugger should
1004 output full info about this type or not.
1006 Current strategy is to permit types which define
1007 no member functions to be output normally. For
1008 those which do define member functions, if no
1009 member functions have yet been output, then don't
1010 output the definition of the type. If member functions
1011 for the type are later seen, a full definition of the
1012 type will eventually be output. */
1014 lang_output_debug_info (type)
1015 tree type;
1017 extern tree pending_vtables;
1019 if (! IS_AGGR_TYPE (type))
1020 return 1;
1021 if (TYPE_LANG_SPECIFIC (type) == 0)
1022 return 1;
1023 if (CLASSTYPE_METHOD_VEC (type) == 0)
1024 return 1;
1026 if (flag_minimal_debug)
1028 /* Don't output full info about any type
1029 which does not have its implementation defined here. */
1030 if (TYPE_VIRTUAL_P (type) && write_virtuals == 2)
1031 return value_member (DECL_NAME (TYPE_NAME (type)), pending_vtables) != 0;
1032 if (CLASSTYPE_INTERFACE_ONLY (type))
1033 return 0;
1035 return CLASSTYPE_ASM_WRITTEN (type);
1037 else
1038 /* Can't work until GDB is modified. */
1039 return 1;
1042 /* Comparison function for sorting identifiers in RAISES lists.
1043 Note that because IDENTIFIER_NODEs are unique, we can sort
1044 them by address, saving an indirection. */
1045 static int
1046 id_cmp (p1, p2)
1047 int *p1, *p2;
1049 return *p1 - *p2;
1052 /* Build the FUNCTION_TYPE or METHOD_TYPE which may raise exceptions
1053 listed in RAISES. */
1054 tree
1055 build_exception_variant (ctype, type, raises)
1056 tree ctype, type;
1057 tree raises;
1059 int i;
1060 tree v = TYPE_MAIN_VARIANT (type);
1061 tree t, t2, cname;
1062 tree *a = (tree *)alloca ((list_length (raises)+1) * sizeof (tree));
1063 int constp = TREE_READONLY (type);
1064 int volatilep = TREE_VOLATILE (type);
1066 if (raises && TREE_CHAIN (raises))
1068 for (i = 0, t = raises; t; t = TREE_CHAIN (t), i++)
1069 a[i] = t;
1070 /* NULL terminator for list. */
1071 a[i] = NULL_TREE;
1072 qsort (a, i, sizeof (tree), id_cmp);
1073 while (i--)
1074 TREE_CHAIN (a[i]) = a[i+1];
1075 raises = a[0];
1077 else if (raises)
1078 /* do nothing. */;
1079 else
1080 return build_type_variant (v, constp, volatilep);
1082 if (ctype)
1084 cname = TYPE_NAME (ctype);
1085 if (TREE_CODE (cname) == TYPE_DECL)
1086 cname = DECL_NAME (cname);
1088 else
1089 cname = NULL_TREE;
1091 for (t = raises; t; t = TREE_CHAIN (t))
1093 /* See that all the exceptions we are thinking about
1094 raising have been declared. */
1095 tree this_cname = lookup_exception_cname (ctype, cname, t);
1096 tree decl = lookup_exception_object (this_cname, TREE_VALUE (t), 1);
1098 if (decl == NULL_TREE)
1099 decl = lookup_exception_object (this_cname, TREE_VALUE (t), 0);
1100 /* Place canonical exception decl into TREE_TYPE of RAISES list. */
1101 TREE_TYPE (t) = decl;
1104 for (v = TYPE_NEXT_VARIANT (v); v; v = TYPE_NEXT_VARIANT (v))
1106 if (TREE_READONLY (v) != constp
1107 || TREE_VOLATILE (v) != volatilep)
1108 continue;
1110 t = raises;
1111 t2 = TYPE_RAISES_EXCEPTIONS (v);
1112 while (t && t2)
1114 if (TREE_TYPE (t) == TREE_TYPE (t2))
1116 t = TREE_CHAIN (t);
1117 t2 = TREE_CHAIN (t2);
1119 else break;
1121 if (t || t2)
1122 continue;
1123 /* List of exceptions raised matches previously found list.
1125 @@ Nice to free up storage used in consing up the
1126 @@ list of exceptions raised. */
1127 return v;
1130 /* Need to build a new variant. */
1131 v = copy_node (type);
1132 TYPE_NEXT_VARIANT (v) = TYPE_NEXT_VARIANT (type);
1133 TYPE_NEXT_VARIANT (type) = v;
1134 if (raises && ! TREE_PERMANENT (raises))
1136 int temporary = allocation_temporary_p ();
1137 if (temporary)
1138 end_temporary_allocation ();
1139 raises = copy_list (raises);
1140 if (temporary)
1141 resume_temporary_allocation ();
1143 TYPE_RAISES_EXCEPTIONS (v) = raises;
1144 return v;
1147 /* Subroutine of make_permanent_node.
1149 Assuming T is a node build bottom-up, make it all exist on
1150 permanent obstack, if it is not permanent already. */
1151 static tree
1152 make_deep_copy (t)
1153 tree t;
1155 enum tree_code code;
1157 if (t == NULL_TREE || TREE_PERMANENT (t))
1158 return t;
1160 switch (code = TREE_CODE (t))
1162 case ERROR_MARK:
1163 return error_mark_node;
1165 case VAR_DECL:
1166 case PARM_DECL:
1167 case FUNCTION_DECL:
1168 case CONST_DECL:
1169 break;
1171 case TREE_LIST:
1173 tree chain = TREE_CHAIN (t);
1174 t = copy_node (t);
1175 TREE_PURPOSE (t) = make_deep_copy (TREE_PURPOSE (t));
1176 TREE_VALUE (t) = make_deep_copy (TREE_VALUE (t));
1177 TREE_CHAIN (t) = make_deep_copy (chain);
1178 return t;
1181 case TREE_VEC:
1183 int len = TREE_VEC_LENGTH (t);
1185 t = copy_node (t);
1186 while (len--)
1187 TREE_VEC_ELT (t, len) = make_deep_copy (TREE_VEC_ELT (t, len));
1188 return t;
1191 case INTEGER_CST:
1192 case REAL_CST:
1193 case STRING_CST:
1194 return copy_node (t);
1196 case COND_EXPR:
1197 case NEW_EXPR:
1198 t = copy_node (t);
1199 TREE_OPERAND (t, 0) = make_deep_copy (TREE_OPERAND (t, 0));
1200 TREE_OPERAND (t, 1) = make_deep_copy (TREE_OPERAND (t, 1));
1201 TREE_OPERAND (t, 2) = make_deep_copy (TREE_OPERAND (t, 2));
1202 return t;
1204 case SAVE_EXPR:
1205 t = copy_node (t);
1206 TREE_OPERAND (t, 0) = make_deep_copy (TREE_OPERAND (t, 0));
1207 return t;
1209 case MODIFY_EXPR:
1210 case PLUS_EXPR:
1211 case MINUS_EXPR:
1212 case MULT_EXPR:
1213 case TRUNC_DIV_EXPR:
1214 case TRUNC_MOD_EXPR:
1215 case MIN_EXPR:
1216 case MAX_EXPR:
1217 case LSHIFT_EXPR:
1218 case RSHIFT_EXPR:
1219 case BIT_IOR_EXPR:
1220 case BIT_XOR_EXPR:
1221 case BIT_AND_EXPR:
1222 case BIT_ANDTC_EXPR:
1223 case TRUTH_ANDIF_EXPR:
1224 case TRUTH_ORIF_EXPR:
1225 case LT_EXPR:
1226 case LE_EXPR:
1227 case GT_EXPR:
1228 case GE_EXPR:
1229 case EQ_EXPR:
1230 case NE_EXPR:
1231 case CEIL_DIV_EXPR:
1232 case FLOOR_DIV_EXPR:
1233 case ROUND_DIV_EXPR:
1234 case CEIL_MOD_EXPR:
1235 case FLOOR_MOD_EXPR:
1236 case ROUND_MOD_EXPR:
1237 case COMPOUND_EXPR:
1238 case PREDECREMENT_EXPR:
1239 case PREINCREMENT_EXPR:
1240 case POSTDECREMENT_EXPR:
1241 case POSTINCREMENT_EXPR:
1242 case CALL_EXPR:
1243 t = copy_node (t);
1244 TREE_OPERAND (t, 0) = make_deep_copy (TREE_OPERAND (t, 0));
1245 TREE_OPERAND (t, 1) = make_deep_copy (TREE_OPERAND (t, 1));
1246 return t;
1248 case CONVERT_EXPR:
1249 case ADDR_EXPR:
1250 case INDIRECT_REF:
1251 case NEGATE_EXPR:
1252 case BIT_NOT_EXPR:
1253 case TRUTH_NOT_EXPR:
1254 case NOP_EXPR:
1255 case COMPONENT_REF:
1256 t = copy_node (t);
1257 TREE_OPERAND (t, 0) = make_deep_copy (TREE_OPERAND (t, 0));
1258 return t;
1260 /* This list is incomplete, but should suffice for now.
1261 It is very important that `sorry' does not call
1262 `report_error_function'. That could cause an infinite loop. */
1263 default:
1264 sorry ("initializer contains unrecognized tree code");
1265 return error_mark_node;
1268 abort ();
1271 /* Assuming T is a node built bottom-up, make it all exist on
1272 permanent obstack, if it is not permanent already. */
1273 tree
1274 copy_to_permanent (t)
1275 tree t;
1277 register struct obstack *ambient_obstack = current_obstack;
1278 register struct obstack *ambient_saveable_obstack = saveable_obstack;
1280 if (t == NULL_TREE || TREE_PERMANENT (t))
1281 return t;
1283 saveable_obstack = &permanent_obstack;
1284 current_obstack = saveable_obstack;
1286 t = make_deep_copy (t);
1288 current_obstack = ambient_obstack;
1289 saveable_obstack = ambient_saveable_obstack;
1291 return t;
1295 lang_simple_cst_equal (t1, t2)
1296 tree t1, t2;
1298 register enum tree_code code1, code2;
1299 int cmp;
1301 if (t1 == t2)
1302 return 1;
1303 if (t1 == 0 || t2 == 0)
1304 return 0;
1306 code1 = TREE_CODE (t1);
1307 code2 = TREE_CODE (t2);
1309 switch (code1)
1311 case CPLUS_NEW_EXPR:
1312 cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
1313 if (cmp <= 0)
1314 return cmp;
1315 return simple_cst_list_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
1317 default:
1318 return -1;