]> gcc.gnu.org Git - gcc.git/blame - gcc/cp/class.c
Don't warn about unused static const variables.
[gcc.git] / gcc / cp / class.c
CommitLineData
8d08fdba
MS
1/* Functions related to building classes and their related objects.
2 Copyright (C) 1987, 1992, 1993, 1994 Free Software Foundation, Inc.
3 Contributed by Michael Tiemann (tiemann@cygnus.com)
4
5This file is part of GNU CC.
6
7GNU CC is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2, or (at your option)
10any later version.
11
12GNU CC is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with GNU CC; see the file COPYING. If not, write to
19the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
20
21
22/* High-level class interface. */
23
24#include "config.h"
25#include "tree.h"
26#include <stdio.h>
27#include "cp-tree.h"
28#include "flags.h"
29
30#include "obstack.h"
31#define obstack_chunk_alloc xmalloc
32#define obstack_chunk_free free
33
34extern struct obstack permanent_obstack;
35
36/* This is how we tell when two virtual member functions are really the
37 same. */
38#define SAME_FN(FN1DECL, FN2DECL) (DECL_ASSEMBLER_NAME (FN1DECL) == DECL_ASSEMBLER_NAME (FN2DECL))
39
40extern void set_class_shadows PROTO ((tree));
41
42/* Way of stacking class types. */
43static tree *current_class_base, *current_class_stack;
44static int current_class_stacksize;
45int current_class_depth;
46
47struct class_level
48{
49 /* The previous class level. */
50 struct class_level *level_chain;
51
52 /* The class instance variable, as a PARM_DECL. */
53 tree decl;
54 /* The class instance variable, as an object. */
55 tree object;
56 /* The virtual function table pointer
57 for the class instance variable. */
58 tree vtable_decl;
59
60 /* Name of the current class. */
61 tree name;
62 /* Type of the current class. */
63 tree type;
64
65 /* Flags for this class level. */
66 int this_is_variable;
67 int memoized_lookups;
68 int save_memoized;
69 int unused;
70};
71
72tree current_class_decl, C_C_D; /* PARM_DECL: the class instance variable */
73tree current_vtable_decl;
74
75/* The following two can be derived from the previous one */
76tree current_class_name; /* IDENTIFIER_NODE: name of current class */
77tree current_class_type; /* _TYPE: the type of the current class */
78tree previous_class_type; /* _TYPE: the previous type that was a class */
79tree previous_class_values; /* TREE_LIST: copy of the class_shadowed list
80 when leaving an outermost class scope. */
81static tree get_vfield_name PROTO((tree));
82tree the_null_vtable_entry;
83
84/* Way of stacking language names. */
85tree *current_lang_base, *current_lang_stack;
51c184be 86int current_lang_stacksize;
8d08fdba
MS
87
88/* Names of languages we recognize. */
89tree lang_name_c, lang_name_cplusplus;
90tree current_lang_name;
91
92/* When layout out an aggregate type, the size of the
93 basetypes (virtual and non-virtual) is passed to layout_record
94 via this node. */
95static tree base_layout_decl;
96
51c184be 97/* Variables shared between class.c and call.c. */
8d08fdba
MS
98
99int n_vtables = 0;
100int n_vtable_entries = 0;
101int n_vtable_searches = 0;
102int n_vtable_elems = 0;
103int n_convert_harshness = 0;
104int n_compute_conversion_costs = 0;
105int n_build_method_call = 0;
106int n_inner_fields_searched = 0;
107
108/* Virtual baseclass things. */
109tree
110build_vbase_pointer (exp, type)
111 tree exp, type;
112{
113 char *name;
114
115 name = (char *) alloca (TYPE_NAME_LENGTH (type) + sizeof (VBASE_NAME) + 1);
116 sprintf (name, VBASE_NAME_FORMAT, TYPE_NAME_STRING (type));
117 return build_component_ref (exp, get_identifier (name), 0, 0);
118}
119
120/* Is the type of the EXPR, the complete type of the object?
121 If we are going to be wrong, we must be conservative, and return 0. */
122int
123complete_type_p (expr)
124 tree expr;
125{
126 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
127 while (1)
128 {
129 switch (TREE_CODE (expr))
130 {
131 case SAVE_EXPR:
132 case INDIRECT_REF:
133 case ADDR_EXPR:
134 case NOP_EXPR:
135 case CONVERT_EXPR:
136 expr = TREE_OPERAND (expr, 0);
137 continue;
138
139 case CALL_EXPR:
140 if (! TREE_HAS_CONSTRUCTOR (expr))
141 break;
142 /* fall through... */
143 case VAR_DECL:
144 case FIELD_DECL:
145 if (TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE
146 && IS_AGGR_TYPE (TREE_TYPE (TREE_TYPE (expr)))
147 && TYPE_MAIN_VARIANT (TREE_TYPE (expr)) == type)
148 return 1;
149 /* fall through... */
150 case TARGET_EXPR:
151 case PARM_DECL:
152 if (IS_AGGR_TYPE (TREE_TYPE (expr))
153 && TYPE_MAIN_VARIANT (TREE_TYPE (expr)) == type)
154 return 1;
155 /* fall through... */
156 case PLUS_EXPR:
157 default:
158 break;
159 }
160 break;
161 }
162 return 0;
163}
164
165/* Build multi-level access to EXPR using hierarchy path PATH.
166 CODE is PLUS_EXPR if we are going with the grain,
167 and MINUS_EXPR if we are not (in which case, we cannot traverse
168 virtual baseclass links).
169
170 TYPE is the type we want this path to have on exit.
171
172 ALIAS_THIS is non-zero if EXPR in an expression involving `this'. */
173tree
174build_vbase_path (code, type, expr, path, alias_this)
175 enum tree_code code;
176 tree type, expr, path;
177 int alias_this;
178{
179 register int changed = 0;
180 tree last = NULL_TREE, last_virtual = NULL_TREE;
181 int nonnull = 0;
182 int fixed_type_p = resolves_to_fixed_type_p (expr, &nonnull);
183 tree null_expr = 0, nonnull_expr;
184 tree basetype;
185 tree offset = integer_zero_node;
186
187 /* We need additional logic to convert back to the unconverted type
188 (the static type of the complete object), and then convert back
189 to the type we want. Until that is done, or until we can
190 recognize when that is, we cannot do the short cut logic. (mrs) */
191 /* Do this, until we can undo any previous convertions. See net35.C
192 for a testcase. */
193 fixed_type_p = complete_type_p (expr);
194
195 if (!fixed_type_p && TREE_SIDE_EFFECTS (expr))
196 expr = save_expr (expr);
197 nonnull_expr = expr;
198
199 if (BINFO_INHERITANCE_CHAIN (path))
200 {
201 tree reverse_path = NULL_TREE;
202
203 while (path)
204 {
205 tree r = copy_node (path);
206 BINFO_INHERITANCE_CHAIN (r) = reverse_path;
207 reverse_path = r;
208 path = BINFO_INHERITANCE_CHAIN (path);
209 }
210 path = reverse_path;
211 }
212
213 basetype = BINFO_TYPE (path);
214
215 while (path)
216 {
217 if (TREE_VIA_VIRTUAL (path))
218 {
219 last_virtual = BINFO_TYPE (path);
220 if (code == PLUS_EXPR)
221 {
222 changed = ! fixed_type_p;
223
224 if (changed)
225 {
226 extern int flag_assume_nonnull_objects;
227 tree ind;
228
229 /* We already check for ambiguous things in the caller, just
230 find a path. */
231 if (last)
232 {
233 tree binfo = get_binfo (last, TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (nonnull_expr))), 0);
234 nonnull_expr = convert_pointer_to_real (binfo, nonnull_expr);
235 }
236 ind = build_indirect_ref (nonnull_expr, NULL_PTR);
237 nonnull_expr = build_vbase_pointer (ind, last_virtual);
238 if (nonnull == 0 && !flag_assume_nonnull_objects
239 && null_expr == NULL_TREE)
240 {
241 null_expr = build1 (NOP_EXPR, TYPE_POINTER_TO (last_virtual), integer_zero_node);
242 expr = build (COND_EXPR, TYPE_POINTER_TO (last_virtual),
243 build (EQ_EXPR, integer_type_node, expr,
244 integer_zero_node),
245 null_expr, nonnull_expr);
246 }
247 }
248 /* else we'll figure out the offset below. */
249
250 /* Happens in the case of parse errors. */
251 if (nonnull_expr == error_mark_node)
252 return error_mark_node;
253 }
254 else
255 {
256 cp_error ("cannot cast up from virtual baseclass `%T'",
257 last_virtual);
258 return error_mark_node;
259 }
260 }
261 last = path;
262 path = BINFO_INHERITANCE_CHAIN (path);
263 }
264 /* LAST is now the last basetype assoc on the path. */
265
266 /* A pointer to a virtual base member of a non-null object
267 is non-null. Therefore, we only need to test for zeroness once.
268 Make EXPR the canonical expression to deal with here. */
269 if (null_expr)
270 {
271 TREE_OPERAND (expr, 2) = nonnull_expr;
272 TREE_TYPE (TREE_OPERAND (expr, 1)) = TREE_TYPE (nonnull_expr);
273 }
274 else
275 expr = nonnull_expr;
276
277 /* If we go through any virtual base pointers, make sure that
278 casts to BASETYPE from the last virtual base class use
279 the right value for BASETYPE. */
280 if (changed)
281 {
282 tree intype = TREE_TYPE (TREE_TYPE (expr));
283 if (TYPE_MAIN_VARIANT (intype) == BINFO_TYPE (last))
284 basetype = intype;
285 else
286 {
287 tree binfo = get_binfo (last, TYPE_MAIN_VARIANT (intype), 0);
288 basetype = last;
289 offset = BINFO_OFFSET (binfo);
290 }
291 }
292 else
293 {
294 if (last_virtual)
295 {
296 offset = BINFO_OFFSET (binfo_member (last_virtual,
297 CLASSTYPE_VBASECLASSES (basetype)));
298 offset = size_binop (PLUS_EXPR, offset, BINFO_OFFSET (last));
299 }
300 else
301 offset = BINFO_OFFSET (last);
302 }
303
304 if (TREE_INT_CST_LOW (offset))
305 {
306 /* For multiple inheritance: if `this' can be set by any
307 function, then it could be 0 on entry to any function.
308 Preserve such zeroness here. Otherwise, only in the
309 case of constructors need we worry, and in those cases,
310 it will be zero, or initialized to some legal value to
311 which we may add. */
312 if (nonnull == 0 && (alias_this == 0 || flag_this_is_variable > 0))
313 {
314 if (null_expr)
315 TREE_TYPE (null_expr) = type;
316 else
317 null_expr = build1 (NOP_EXPR, type, integer_zero_node);
318 if (TREE_SIDE_EFFECTS (expr))
319 expr = save_expr (expr);
320
321 return build (COND_EXPR, type,
322 build (EQ_EXPR, integer_type_node, expr, integer_zero_node),
323 null_expr,
324 build (code, type, expr, offset));
325 }
326 else return build (code, type, expr, offset);
327 }
328
329 /* Cannot change the TREE_TYPE of a NOP_EXPR here, since it may
330 be used multiple times in initialization of multiple inheritance. */
331 if (null_expr)
332 {
333 TREE_TYPE (expr) = type;
334 return expr;
335 }
336 else
337 return build1 (NOP_EXPR, type, expr);
338}
339
340/* Virtual function things. */
341
342/* Virtual functions to be dealt with after laying out our
343 base classes. Usually this is used only when classes have virtual
344 baseclasses, but it can happen also when classes have non-virtual
345 baseclasses if the derived class overrides baseclass functions
346 at different offsets. */
347static tree pending_hard_virtuals;
348static int doing_hard_virtuals;
349
350/* XXX This is set but never used. (bpk) */
351#if 0
352/* Temporary binfo list to memoize lookups of the left-most non-virtual
353 baseclass B in a lattice topped by T. B can appear multiple times
354 in the lattice.
355 TREE_PURPOSE is B's TYPE_MAIN_VARIANT.
356 TREE_VALUE is the path by which B is reached from T.
357 TREE_TYPE is B's real type.
358
359 If TREE_TYPE is NULL_TREE, it means that B was reached via
360 a virtual baseclass.
361 N.B.: This list consists of nodes on the temporary obstack. */
362static tree leftmost_baseclasses;
363#endif
364
365/* Build an entry in the virtual function table.
366 DELTA is the offset for the `this' pointer.
367 PFN is an ADDR_EXPR containing a pointer to the virtual function.
368 Note that the index (DELTA2) in the virtual function table
369 is always 0. */
370tree
371build_vtable_entry (delta, pfn)
372 tree delta, pfn;
373{
8926095f
MS
374 extern tree abort_fndecl;
375 if (TREE_CODE (pfn) == ADDR_EXPR)
376 {
377 tree fndecl = TREE_OPERAND (pfn, 0);
378 if (TREE_CODE(fndecl) == FUNCTION_DECL
379 && DECL_ABSTRACT_VIRTUAL_P(fndecl))
380 {
381 tree d = copy_node (fndecl);
382 DECL_RTL (d) = DECL_RTL (abort_fndecl);
383 TREE_OPERAND (pfn, 0) = d;
384 }
385 }
8d08fdba 386
8926095f
MS
387 if (flag_vtable_thunks)
388 {
389 HOST_WIDE_INT idelta = TREE_INT_CST_LOW (delta);
390 extern tree make_thunk ();
391 if (idelta)
392 {
393 pfn = build1 (ADDR_EXPR, ptr_type_node,
394 make_thunk (pfn, idelta));
395 TREE_READONLY (pfn) = 1;
396 TREE_CONSTANT (pfn) = 1;
397 }
398#ifdef GATHER_STATISTICS
399 n_vtable_entries += 1;
400#endif
401 return pfn;
402 }
403 else
404 {
405 extern int flag_huge_objects;
406 tree elems = tree_cons (NULL_TREE, delta,
407 tree_cons (NULL_TREE, integer_zero_node,
408 build_tree_list (NULL_TREE, pfn)));
409 tree entry = build (CONSTRUCTOR, vtable_entry_type, NULL_TREE, elems);
410
411 /* DELTA is constructed by `size_int', which means it may be an
412 unsigned quantity on some platforms. Therefore, we cannot use
413 `int_fits_type_p', because when DELTA is really negative,
414 `force_fit_type' will make it look like a very large number. */
415
416 if ((TREE_INT_CST_LOW (TYPE_MAX_VALUE (delta_type_node))
417 < TREE_INT_CST_LOW (delta))
418 || (TREE_INT_CST_LOW (delta)
419 < TREE_INT_CST_LOW (TYPE_MIN_VALUE (delta_type_node))))
420 if (flag_huge_objects)
421 sorry ("object size exceeds built-in limit for virtual function table implementation");
422 else
423 sorry ("object size exceeds normal limit for virtual function table implementation, recompile all source and use -fhuge-objects");
424
425 TREE_CONSTANT (entry) = 1;
426 TREE_STATIC (entry) = 1;
427 TREE_READONLY (entry) = 1;
8d08fdba
MS
428
429#ifdef GATHER_STATISTICS
8926095f 430 n_vtable_entries += 1;
8d08fdba
MS
431#endif
432
8926095f
MS
433 return entry;
434 }
8d08fdba
MS
435}
436
437/* Given an object INSTANCE, return an expression which yields the
438 virtual function corresponding to INDEX. There are many special
439 cases for INSTANCE which we take care of here, mainly to avoid
440 creating extra tree nodes when we don't have to. */
441tree
442build_vfn_ref (ptr_to_instptr, instance, idx)
443 tree *ptr_to_instptr, instance;
444 tree idx;
445{
446 extern int building_cleanup;
447 tree vtbl, aref;
448 tree basetype = TREE_TYPE (instance);
449
450 if (TREE_CODE (basetype) == REFERENCE_TYPE)
451 basetype = TREE_TYPE (basetype);
452
453 if (instance == C_C_D)
454 {
455 if (current_vtable_decl == NULL_TREE
456 || current_vtable_decl == error_mark_node
457 || !UNIQUELY_DERIVED_FROM_P (DECL_FCONTEXT (CLASSTYPE_VFIELD (current_class_type)), basetype))
458 vtbl = build_indirect_ref (build_vfield_ref (instance, basetype), NULL_PTR);
459 else
460 vtbl = current_vtable_decl;
461 }
462 else
463 {
464 if (optimize)
465 {
466 /* Try to figure out what a reference refers to, and
467 access its virtual function table directly. */
468 tree ref = NULL_TREE;
469
470 if (TREE_CODE (instance) == INDIRECT_REF
471 && TREE_CODE (TREE_TYPE (TREE_OPERAND (instance, 0))) == REFERENCE_TYPE)
472 ref = TREE_OPERAND (instance, 0);
473 else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
474 ref = instance;
475
476 if (ref && TREE_CODE (ref) == VAR_DECL
477 && DECL_INITIAL (ref))
478 {
479 tree init = DECL_INITIAL (ref);
480
481 while (TREE_CODE (init) == NOP_EXPR
482 || TREE_CODE (init) == NON_LVALUE_EXPR)
483 init = TREE_OPERAND (init, 0);
484 if (TREE_CODE (init) == ADDR_EXPR)
485 {
486 init = TREE_OPERAND (init, 0);
487 if (IS_AGGR_TYPE (TREE_TYPE (init))
488 && (TREE_CODE (init) == PARM_DECL
489 || TREE_CODE (init) == VAR_DECL))
490 instance = init;
491 }
492 }
493 }
494
495 if (IS_AGGR_TYPE (TREE_TYPE (instance))
496 && !IS_SIGNATURE_POINTER (TREE_TYPE (instance))
497 && !IS_SIGNATURE_REFERENCE (TREE_TYPE (instance))
498 && (TREE_CODE (instance) == RESULT_DECL
499 || TREE_CODE (instance) == PARM_DECL
500 || TREE_CODE (instance) == VAR_DECL))
501 vtbl = TYPE_BINFO_VTABLE (basetype);
502 else
503 vtbl = build_indirect_ref (build_vfield_ref (instance, basetype),
504 NULL_PTR);
505 }
8926095f 506 if (!flag_vtable_thunks)
51c184be 507 assemble_external (vtbl);
8d08fdba
MS
508 aref = build_array_ref (vtbl, idx);
509
510 /* Save the intermediate result in a SAVE_EXPR so we don't have to
511 compute each component of the virtual function pointer twice. */
512 if (!building_cleanup && TREE_CODE (aref) == INDIRECT_REF)
513 TREE_OPERAND (aref, 0) = save_expr (TREE_OPERAND (aref, 0));
514
8926095f
MS
515 if (flag_vtable_thunks)
516 return aref;
517 else
518 {
519 *ptr_to_instptr
520 = build (PLUS_EXPR, TREE_TYPE (*ptr_to_instptr),
521 *ptr_to_instptr,
522 convert (ptrdiff_type_node,
523 build_component_ref (aref, delta_identifier, 0, 0)));
524 return build_component_ref (aref, pfn_identifier, 0, 0);
525 }
8d08fdba
MS
526}
527
528/* Set TREE_PUBLIC and/or TREE_EXTERN on the vtable DECL,
529 based on TYPE and other static flags.
530
531 Note that anything public is tagged TREE_PUBLIC, whether
532 it's public in this file or in another one. */
533
534static void
535import_export_vtable (decl, type)
536 tree decl, type;
537{
538 if (write_virtuals >= 2)
539 {
540 if (CLASSTYPE_INTERFACE_KNOWN (type))
541 {
542 TREE_PUBLIC (decl) = 1;
543 DECL_EXTERNAL (decl) = ! CLASSTYPE_VTABLE_NEEDS_WRITING (type);
544 }
545 }
546 else if (write_virtuals != 0)
547 {
548 TREE_PUBLIC (decl) = 1;
549 if (write_virtuals < 0)
550 DECL_EXTERNAL (decl) = 1;
551 }
552}
553
554/* Return the name of the virtual function table (as an IDENTIFIER_NODE)
555 for the given TYPE. */
556static tree
557get_vtable_name (type)
558 tree type;
559{
560 tree type_id = build_typename_overload (type);
561 char *buf = (char *)alloca (sizeof (VTABLE_NAME_FORMAT)
562 + IDENTIFIER_LENGTH (type_id) + 2);
563 char *ptr = IDENTIFIER_POINTER (type_id);
564 int i;
565 for (i = 0; ptr[i] == OPERATOR_TYPENAME_FORMAT[i]; i++) ;
566#if 0
567 /* We don't take off the numbers; prepare_fresh_vtable uses the
568 DECL_ASSEMBLER_NAME for the type, which includes the number
569 in `3foo'. If we were to pull them off here, we'd end up with
570 something like `_vt.foo.3bar', instead of a uniform definition. */
571 while (ptr[i] >= '0' && ptr[i] <= '9')
572 i += 1;
573#endif
574 sprintf (buf, VTABLE_NAME_FORMAT, ptr+i);
575 return get_identifier (buf);
576}
577
578/* Build a virtual function for type TYPE.
579 If BINFO is non-NULL, build the vtable starting with the initial
580 approximation that it is the same as the one which is the head of
581 the association list. */
582static tree
583build_vtable (binfo, type)
584 tree binfo, type;
585{
586 tree name = get_vtable_name (type);
587 tree virtuals, decl;
588
589 if (binfo)
590 {
591 virtuals = copy_list (BINFO_VIRTUALS (binfo));
592 decl = build_decl (VAR_DECL, name, TREE_TYPE (BINFO_VTABLE (binfo)));
593 }
594 else
595 {
596 virtuals = NULL_TREE;
597 decl = build_decl (VAR_DECL, name, void_type_node);
598 }
599
600#ifdef GATHER_STATISTICS
601 n_vtables += 1;
602 n_vtable_elems += list_length (virtuals);
603#endif
604
605 /* Set TREE_PUBLIC and TREE_EXTERN as appropriate. */
606 import_export_vtable (decl, type);
607
608 IDENTIFIER_GLOBAL_VALUE (name) = decl = pushdecl_top_level (decl);
609 /* Initialize the association list for this type, based
610 on our first approximation. */
611 TYPE_BINFO_VTABLE (type) = decl;
612 TYPE_BINFO_VIRTUALS (type) = virtuals;
613
614 TREE_STATIC (decl) = 1;
615#ifndef WRITABLE_VTABLES
616 /* Make them READONLY by default. (mrs) */
617 TREE_READONLY (decl) = 1;
618#endif
619 /* At one time the vtable info was grabbed 2 words at a time. This
620 fails on sparc unless you have 8-byte alignment. (tiemann) */
621 DECL_ALIGN (decl) = MAX (TYPE_ALIGN (double_type_node),
622 DECL_ALIGN (decl));
623
624 /* Why is this conditional? (mrs) */
625 if (binfo && write_virtuals >= 0)
626 DECL_VIRTUAL_P (decl) = 1;
627#if 0
628 /* Remember which class this vtable is really for. */
629 if (binfo)
630 DECL_VPARENT (decl) = BINFO_TYPE (binfo);
631 else
632 DECL_VPARENT (decl) = type;
633#endif
634 DECL_CONTEXT (decl) = type;
635
636 binfo = TYPE_BINFO (type);
637 SET_BINFO_VTABLE_PATH_MARKED (binfo);
638 SET_BINFO_NEW_VTABLE_MARKED (binfo);
639 return decl;
640}
641
642/* Given a base type PARENT, and a derived type TYPE, build
643 a name which distinguishes exactly the PARENT member of TYPE's type.
644
645 FORMAT is a string which controls how sprintf formats the name
646 we have generated.
647
648 For example, given
649
650 class A; class B; class C : A, B;
651
652 it is possible to distinguish "A" from "C's A". And given
653
654 class L;
655 class A : L; class B : L; class C : A, B;
656
657 it is possible to distinguish "L" from "A's L", and also from
658 "C's L from A".
659
660 Make sure to use the DECL_ASSEMBLER_NAME of the TYPE_NAME of the
661 type, as template have DECL_NAMEs like: X<int>, whereas the
662 DECL_ASSEMBLER_NAME is set to be something the assembler can handle.
663 */
664static tree
665build_type_pathname (format, parent, type)
666 char *format;
667 tree parent, type;
668{
669 extern struct obstack temporary_obstack;
670 char *first, *base, *name;
671 int i;
672 tree id;
673
674 parent = TYPE_MAIN_VARIANT (parent);
675
676 /* Remember where to cut the obstack to. */
677 first = obstack_base (&temporary_obstack);
678
679 /* Put on TYPE+PARENT. */
680 obstack_grow (&temporary_obstack,
681 TYPE_ASSEMBLER_NAME_STRING (type),
682 TYPE_ASSEMBLER_NAME_LENGTH (type));
683#ifdef JOINER
684 obstack_1grow (&temporary_obstack, JOINER);
685#else
686 obstack_1grow (&temporary_obstack, '_');
687#endif
688 obstack_grow0 (&temporary_obstack,
689 TYPE_ASSEMBLER_NAME_STRING (parent),
690 TYPE_ASSEMBLER_NAME_LENGTH (parent));
691 i = obstack_object_size (&temporary_obstack);
692 base = obstack_base (&temporary_obstack);
693 obstack_finish (&temporary_obstack);
694
695 /* Put on FORMAT+TYPE+PARENT. */
696 obstack_blank (&temporary_obstack, strlen (format) + i + 1);
697 name = obstack_base (&temporary_obstack);
698 sprintf (name, format, base);
699 id = get_identifier (name);
700 obstack_free (&temporary_obstack, first);
701
702 return id;
703}
704
705/* Give TYPE a new virtual function table which is initialized
706 with a skeleton-copy of its original initialization. The only
707 entry that changes is the `delta' entry, so we can really
708 share a lot of structure.
709
710 FOR_TYPE is the derived type which caused this table to
711 be needed.
712
713 BINFO is the type association which provided TYPE for FOR_TYPE.
714
715 The way we update BASE_BINFO's vtable information is just to change the
716 association information in FOR_TYPE's association list. */
717static void
718prepare_fresh_vtable (binfo, base_binfo, for_type)
719 tree binfo, base_binfo, for_type;
720{
721 tree basetype = BINFO_TYPE (binfo);
722 tree orig_decl = BINFO_VTABLE (binfo);
723 tree name = build_type_pathname (VTABLE_NAME_FORMAT, basetype, for_type);
724 tree new_decl = build_decl (VAR_DECL, name, TREE_TYPE (orig_decl));
725 tree path;
726 int result;
727
728 /* Remember which class this vtable is really for. */
729#if 0
730 DECL_VPARENT (new_decl) = BINFO_TYPE (base_binfo);
731#endif
732 DECL_CONTEXT (new_decl) = for_type;
733
734 TREE_STATIC (new_decl) = 1;
735 BINFO_VTABLE (binfo) = pushdecl_top_level (new_decl);
736 DECL_VIRTUAL_P (new_decl) = 1;
737#ifndef WRITABLE_VTABLES
738 /* Make them READONLY by default. (mrs) */
739 TREE_READONLY (new_decl) = 1;
740#endif
741 DECL_ALIGN (new_decl) = DECL_ALIGN (orig_decl);
742
743 /* Make fresh virtual list, so we can smash it later. */
744 BINFO_VIRTUALS (binfo) = copy_list (BINFO_VIRTUALS (binfo));
745 /* Install the value for `headof' if that's what we're doing. */
746 if (flag_dossier)
747 TREE_VALUE (TREE_CHAIN (BINFO_VIRTUALS (binfo)))
748 = build_vtable_entry (size_binop (MINUS_EXPR, integer_zero_node, BINFO_OFFSET (binfo)),
749 FNADDR_FROM_VTABLE_ENTRY (TREE_VALUE (TREE_CHAIN (BINFO_VIRTUALS (binfo)))));
750
751#ifdef GATHER_STATISTICS
752 n_vtables += 1;
753 n_vtable_elems += list_length (BINFO_VIRTUALS (binfo));
754#endif
755
756 /* Set TREE_PUBLIC and TREE_EXTERN as appropriate. */
757 import_export_vtable (new_decl, for_type);
758
759 if (TREE_VIA_VIRTUAL (binfo))
760 my_friendly_assert (binfo == binfo_member (BINFO_TYPE (binfo),
761 CLASSTYPE_VBASECLASSES (current_class_type)),
762 170);
763 SET_BINFO_NEW_VTABLE_MARKED (binfo);
764 SET_BINFO_VTABLE_PATH_MARKED (binfo);
765
766 /* Mark all types between FOR_TYPE and TYPE as having been
767 touched, so that if we change virtual function table entries,
768 new vtables will be initialized. We may reach the virtual
769 baseclass via ambiguous intervening baseclasses. This
770 loop makes sure we get through to the actual baseclass we marked.
771
772 Also, update the vtable entries to reflect the overrides
773 of the top-most class (short of the top type). */
774
775 do
776 {
777 result = get_base_distance (basetype, for_type, 0, &path);
778 for_type = path;
779 while (path)
780 {
781 tree path_binfo = path;
782 tree path_type = BINFO_TYPE (path);
783
784 if (TREE_VIA_VIRTUAL (path))
785 path_binfo = binfo_member (path_type,
786 CLASSTYPE_VBASECLASSES (current_class_type));
787
788 SET_BINFO_VTABLE_PATH_MARKED (path_binfo);
789 if (BINFO_INHERITANCE_CHAIN (path)
790 && CLASSTYPE_VFIELD (path_type) != NULL_TREE
791 && (DECL_NAME (CLASSTYPE_VFIELD (BINFO_TYPE (binfo)))
792 == DECL_NAME (CLASSTYPE_VFIELD (path_type)))
793 /* This is the baseclass just before the original FOR_TYPE. */
794 && BINFO_INHERITANCE_CHAIN (BINFO_INHERITANCE_CHAIN (path)) == NULL_TREE)
795 {
796 tree old_virtuals = TREE_CHAIN (BINFO_VIRTUALS (binfo));
797 tree new_virtuals = TREE_CHAIN (BINFO_VIRTUALS (path_binfo));
798 if (flag_dossier)
799 {
800 old_virtuals = TREE_CHAIN (old_virtuals);
801 new_virtuals = TREE_CHAIN (new_virtuals);
802 }
803 while (old_virtuals)
804 {
805 TREE_VALUE (old_virtuals) = TREE_VALUE (new_virtuals);
806 old_virtuals = TREE_CHAIN (old_virtuals);
807 new_virtuals = TREE_CHAIN (new_virtuals);
808 }
809 }
810 path = BINFO_INHERITANCE_CHAIN (path);
811 }
812 }
813 while (result == -2);
814}
815
816/* Access the virtual function table entry that logically
817 contains BASE_FNDECL. VIRTUALS is the virtual function table's
51c184be
MS
818 initializer. We can run off the end, when dealing with virtual
819 destructors in MI situations, return NULL_TREE in that case. */
8d08fdba
MS
820static tree
821get_vtable_entry (virtuals, base_fndecl)
822 tree virtuals, base_fndecl;
823{
824 unsigned HOST_WIDE_INT i = (HOST_BITS_PER_WIDE_INT >= BITS_PER_WORD
825#ifdef VTABLE_USES_MASK
826 && 0
827#endif
828 ? (TREE_INT_CST_LOW (DECL_VINDEX (base_fndecl))
829 & (((unsigned HOST_WIDE_INT)1<<(BITS_PER_WORD-1))-1))
830 : TREE_INT_CST_LOW (DECL_VINDEX (base_fndecl)));
831
832#ifdef GATHER_STATISTICS
833 n_vtable_searches += i;
834#endif
835
51c184be 836 while (i > 0 && virtuals)
8d08fdba
MS
837 {
838 virtuals = TREE_CHAIN (virtuals);
839 i -= 1;
840 }
841 return virtuals;
842}
843
844/* Put new entry ENTRY into virtual function table initializer
845 VIRTUALS.
846
847 Also update DECL_VINDEX (FNDECL). */
848
849static void
850modify_vtable_entry (old_entry_in_list, new_entry, fndecl)
851 tree old_entry_in_list, new_entry, fndecl;
852{
853 tree base_pfn = FNADDR_FROM_VTABLE_ENTRY (TREE_VALUE (old_entry_in_list));
854 tree vindex;
855
856#ifdef NOTQUITE
857 cp_warning ("replaced %D with %D", DECL_ASSEMBLER_NAME (TREE_OPERAND (base_pfn, 0)), DECL_ASSEMBLER_NAME (fndecl));
858#endif
859 /* We can't put in the really right offset information
860 here, since we have not yet laid out the class to
861 take into account virtual base classes. */
862 TREE_VALUE (old_entry_in_list) = new_entry;
863 vindex = DECL_VINDEX (TREE_OPERAND (base_pfn, 0));
864 if (TREE_CODE (DECL_VINDEX (fndecl)) != INTEGER_CST)
865 DECL_VINDEX (fndecl) = vindex;
866 else
867 {
8926095f
MS
868 if (! tree_int_cst_equal (DECL_VINDEX (fndecl), vindex)
869 && ! doing_hard_virtuals)
8d08fdba 870 {
8926095f
MS
871 pending_hard_virtuals
872 = tree_cons (fndecl, FNADDR_FROM_VTABLE_ENTRY (new_entry),
873 pending_hard_virtuals);
874 TREE_TYPE (pending_hard_virtuals) = TREE_OPERAND (base_pfn, 0);
875 return;
8d08fdba
MS
876 }
877 }
878}
879
880/* Check to ensure that the virtual function table slot in VFIELD,
881 found by DECL_VINDEX of the BASE_FNDECL is in fact from a parent
882 virtual function table that is the same parent as for the
883 BASE_FNDECL given to us. */
884
885static int
886related_vslot (base_fndecl, vfields, type)
887 tree base_fndecl, vfields, type;
888{
889 tree base_context = TYPE_MAIN_VARIANT (DECL_CONTEXT (base_fndecl));
890 tree base;
891 tree path;
892 int distance;
893
894 if (TREE_CODE (vfields) != TREE_LIST)
895 abort ();
896 base = VF_NORMAL_VALUE (vfields);
897 if (base == NULL_TREE)
898 base = VF_BASETYPE_VALUE (vfields);
899
900 /* The simple right way to do this is to ensure that the context of
901 the base virtual function is found along the leftmost path
902 between the most derived type associated with the vfield and the
903 current type. */
904 distance = get_base_distance (base, type, 0, &path);
905 if (distance == -1)
906 abort ();
907 while (path)
908 {
909 if (BINFO_TYPE (path) == base_context)
910 return 1;
911 path = BINFO_INHERITANCE_CHAIN (path);
912 }
913
914 /* given:
915 Rr
916 / \
917 Mm Hh
918 \ /
919 P
920
921 make sure we fill in P's vtable for H with overrides of r,
922 but be cautious of virtual base classes. */
923 /* Combine the two below after debugging. */
924 if (get_base_distance (base_context, base, 0, &path) != -1)
925 {
926 while (path)
927 {
928 if (TREE_VIA_VIRTUAL (path))
929 return 0;
930 path = BINFO_INHERITANCE_CHAIN (path);
931 }
932 /* Make sure that:
933
934 RRB
935 |
936 RL RR
937 \ /
938 L R
939 \ /
940 C
941
942 returns 0. VF_BASETYPE_VALUE is RL, base_context is RRB, type is C,
943 and the vfield we are checking is R. */
944 if (VF_BASETYPE_VALUE (vfields)
945 && get_base_distance (base_context, VF_BASETYPE_VALUE (vfields), 0, &path) == -1
946 && get_base_distance (VF_BASETYPE_VALUE (vfields), base_context, 0, &path) == -1)
947 return 0;
948 return 1;
949 }
950 return 0;
951}
952
953static void modify_vtable_entries ();
954
955/* Access the virtual function table entry i. VIRTUALS is the virtual
956 function table's initializer. */
957static tree
958get_vtable_entry_n (virtuals, i)
959 tree virtuals;
960 unsigned HOST_WIDE_INT i;
961{
962 while (i > 0)
963 {
964 virtuals = TREE_CHAIN (virtuals);
965 i -= 1;
966 }
967 return virtuals;
968}
969
970#if 0
971/* Find the vfield (in the CLASSTYPE_VFIELDS chain) of the given binfo. */
972static tree
973find_associated_vfield (binfo, t)
974 tree t, binfo;
975{
976 tree vfields;
977 tree save_vfields = 0;
978 for (vfields = CLASSTYPE_VFIELDS (t); vfields; vfields = TREE_CHAIN (vfields))
979 {
980 if (VF_BINFO_VALUE (vfields) == binfo)
981 return vfields;
982 }
983 for (vfields = CLASSTYPE_VFIELDS (t); vfields; vfields = TREE_CHAIN (vfields))
984 {
985 tree path;
986 get_base_distance (VF_BASETYPE_VALUE (vfields), t, 0, &path);
987 while (path)
988 {
989 if (path == binfo)
990 return vfields;
991 path = BINFO_INHERITANCE_CHAIN (path);
992 }
993 }
994 /* This is from a virtual base class's vtable, hopefully. */
995 return 0;
996}
997#endif
998
999
1000/* Returns != 0 is the BINFO is the normal one for the main vfield, 0
1001 otherwise. We don't have to worry about the finding BINFO in
1002 CLASSTYPE_VBASECLASSES, if it is virtual, as we never inherit
1003 vtables from virtual base classes. */
1004static int
1005is_normal (binfo, t)
1006 tree t, binfo;
1007{
8d08fdba
MS
1008 int i = CLASSTYPE_VFIELD_PARENT (t);
1009 if (i != -1)
1010 {
1011 tree base_binfo = TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO (t)), i);
1012 if (base_binfo == binfo)
1013 return 1;
1014 }
1015 return 0;
1016}
1017
1018/* Modify virtual function tables in lattice topped by T to place
1019 FNDECL in tables which previously held BASE_FNDECL. This marches
1020 through the vtables directly, looking for exact mactes to
1021 modify. */
1022static void
1023modify_other_vtable_entries (t, binfo, fndecl, base_fndecl, pfn)
1024 tree t, binfo;
1025 tree fndecl, base_fndecl, pfn;
1026{
8926095f 1027 tree virtuals;
8d08fdba
MS
1028 tree binfos;
1029 int i, n_baselinks;
1030 unsigned HOST_WIDE_INT n;
8926095f
MS
1031#if 0
1032 tree vfields;
1033#endif
8d08fdba
MS
1034
1035 virtuals = BINFO_VIRTUALS (binfo);
1036 n = 0;
1037 while (virtuals)
1038 {
1039 tree current_fndecl = TREE_VALUE (virtuals);
1040 tree *binfo2_ptr;
1041 current_fndecl = FNADDR_FROM_VTABLE_ENTRY (current_fndecl);
1042 current_fndecl = TREE_OPERAND (current_fndecl, 0);
1043 if (current_fndecl && SAME_FN (current_fndecl, base_fndecl))
1044 {
1045 /* Most of the below was copied from
1046 modify_vtable_entries (t, fndecl, base_fndecl, pfn); */
1047 tree base_offset, offset;
1048 tree context = DECL_CLASS_CONTEXT (fndecl);
1049 tree vfield = CLASSTYPE_VFIELD (t);
1050 int normal = 1;
1051 tree binfo2, this_offset;
1052 tree base, path;
1053
1054 offset = integer_zero_node;
1055 if (context != t && TYPE_USES_COMPLEX_INHERITANCE (t))
1056 {
1057 offset = virtual_offset (context, CLASSTYPE_VBASECLASSES (t), offset);
1058 if (offset == NULL_TREE)
1059 {
1060 tree binfo = get_binfo (context, t, 0);
1061 offset = BINFO_OFFSET (binfo);
1062 }
1063 }
1064
1065 /* Get the path starting from the deepest base class CONTEXT
1066 of T (i.e., first defn of BASE_FNDECL). */
1067 get_base_distance (binfo, t, 0, &path);
1068 binfo2_ptr = 0;
1069
1070 /* Get our best approximation of what to use for constructing
1071 the virtual function table for T. */
1072 do
1073 {
1074 /* Walk from base toward derived, stopping at the
1075 most derived baseclass that matters. That baseclass
1076 is exactly the one which provides the vtable along
1077 the VFIELD spine, but no more. */
1078 if (TREE_VIA_VIRTUAL (path))
1079 {
1080 base = path;
1081 binfo2 = binfo_member (BINFO_TYPE (base), CLASSTYPE_VBASECLASSES (t));
1082 /* This should never have TREE_USED set. */
1083 binfo2_ptr = 0;
1084 break;
1085 }
1086 if (BINFO_INHERITANCE_CHAIN (path) == NULL_TREE
1087 || (BINFO_TYPE (BINFO_BASETYPE (BINFO_INHERITANCE_CHAIN (path), 0))
1088 != BINFO_TYPE (path))
1089 || BINFO_INHERITANCE_CHAIN (BINFO_INHERITANCE_CHAIN (path)) == NULL_TREE)
1090 {
1091 base = path;
1092 binfo2 = base;
1093 binfo2_ptr = 0;
1094 break;
1095 }
1096 path = BINFO_INHERITANCE_CHAIN (path);
1097 binfo2_ptr = &BINFO_INHERITANCE_CHAIN (path);
1098 }
1099 while (1);
1100
1101 /* Find the right offset for the this pointer based on the base
1102 class we just found. */
1103 base_offset = BINFO_OFFSET (binfo2);
1104 this_offset = size_binop (MINUS_EXPR, offset, base_offset);
1105
1106 /* Make sure we can modify the derived association with immunity. */
1107 if (TREE_USED (binfo2)) {
1108 my_friendly_assert (*binfo2_ptr == binfo2, 999);
1109 *binfo2_ptr = copy_binfo (binfo2);
1110 }
1111
1112#if 0
1113 vfields = find_associated_vfield (binfo2, t);
1114
1115 /* We call this case NORMAL iff this virtual function table
1116 pointer field has its storage reserved in this class.
1117 This is normally the case without virtual baseclasses
1118 or off-center multiple baseclasses. */
1119 normal = (vfields && vfield != NULL_TREE
1120 && VF_BASETYPE_VALUE (vfields) == DECL_FCONTEXT (vfield)
1121 && (VF_BINFO_VALUE (vfields) == NULL_TREE
1122 || ! TREE_VIA_VIRTUAL (VF_BINFO_VALUE (vfields))));
1123
1124 if (normal && VF_BINFO_VALUE (vfields))
1125 /* Everything looks normal so far...check that we are really
1126 working from VFIELD's basetype, and not some other appearance
1127 of that basetype in the lattice. */
1128 normal = (VF_BINFO_VALUE (vfields)
1129 == get_binfo (VF_BASETYPE_VALUE (vfields), t, 0));
1130#else
1131 normal = is_normal (binfo2, t);
1132#endif
1133
1134 if (normal)
1135 {
1136 /* In this case, it is *type*'s vtable we are modifying.
1137 We start with the approximation that it's vtable is that
1138 of the immediate base class. */
1139 binfo2 = TYPE_BINFO (t);
1140 if (! BINFO_NEW_VTABLE_MARKED (binfo2))
1141 build_vtable (TYPE_BINFO (DECL_CONTEXT (vfield)), t);
1142 }
1143 else
1144 {
1145 /* This is our very own copy of `basetype' to play with.
1146 Later, we will fill in all the virtual functions
1147 that override the virtual functions in these base classes
1148 which are not defined by the current type. */
1149 if (! BINFO_NEW_VTABLE_MARKED (binfo2))
1150 prepare_fresh_vtable (binfo2, base, t);
1151 }
1152
1153#ifdef NOTQUITE
1154 cp_warning ("in %D", DECL_NAME (BINFO_VTABLE (binfo2)));
1155#endif
1156 modify_vtable_entry (get_vtable_entry_n (BINFO_VIRTUALS (binfo2), n),
1157 build_vtable_entry (this_offset, pfn),
1158 fndecl);
1159 } else if (current_fndecl && DECL_NAME (current_fndecl) == DECL_NAME (base_fndecl))
1160 {
1161#ifdef NOTQUITE
1162 cp_warning ("%D not replaced (looking for %D) in %D", DECL_ASSEMBLER_NAME (current_fndecl), DECL_ASSEMBLER_NAME (base_fndecl), DECL_NAME (BINFO_VTABLE (binfo)));
1163#endif
1164 }
1165 ++n;
1166 virtuals = TREE_CHAIN (virtuals);
1167 }
1168 binfos = BINFO_BASETYPES (binfo);
1169 n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
1170 for (i = 0; i < n_baselinks; i++)
1171 {
1172 tree base_binfo = TREE_VEC_ELT (binfos, i);
1173 /* Don't modify virtual bases, as we share them down this way. */
1174 /* We hope that other places will get things down this direction. */
1175 if (TREE_VIA_VIRTUAL (base_binfo))
1176 continue;
1177 modify_other_vtable_entries (t, base_binfo, fndecl, base_fndecl, pfn);
1178 }
1179}
1180
1181/* Modify virtual function tables in lattice topped by T to
1182 place FNDECL in tables which previously held BASE_FNDECL.
1183 PFN is just FNDECL wrapped in an ADDR_EXPR, so that it
1184 is suitable for placement directly into an initializer.
1185
1186 All distinct virtual function tables that this type uses
1187 must be updated. */
1188static void
1189modify_vtable_entries (t, fndecl, base_fndecl, pfn)
1190 tree t;
1191 tree fndecl, base_fndecl, pfn;
1192{
1193 tree base_offset, offset;
1194 tree base_context = DECL_CONTEXT (base_fndecl);
1195 tree context = DECL_CLASS_CONTEXT (fndecl);
1196 tree vfield = CLASSTYPE_VFIELD (t);
1197 tree vfields, vbases;
1198 tree saved_pfn;
1199
1200#ifdef NOTQUITE
1201 cp_warning ("modifing all %D into %D for %T", base_fndecl, fndecl, t);
1202 if (DECL_CONTEXT (fndecl) != DECL_CONTEXT (base_fndecl))
1203 {
1204 cp_warning ("switching contexts from %T to %T for %x",
1205 DECL_CONTEXT (fndecl),
1206 DECL_CONTEXT (base_fndecl), fndecl);
1207 cp_warning ("base was %D, new is %D", DECL_ASSEMBLER_NAME (base_fndecl),
1208 DECL_ASSEMBLER_NAME(fndecl));
1209 }
1210#endif
8926095f
MS
1211#if 0
1212 /* this is wrong, see p4736a.C testcase */
8d08fdba 1213 DECL_CONTEXT (fndecl) = DECL_CONTEXT (base_fndecl);
8926095f 1214#endif
8d08fdba
MS
1215
1216 offset = integer_zero_node;
1217 if (context != t && TYPE_USES_COMPLEX_INHERITANCE (t))
1218 {
1219 offset = virtual_offset (context, CLASSTYPE_VBASECLASSES (t), offset);
1220 if (offset == NULL_TREE)
1221 {
1222 tree binfo = get_binfo (context, t, 0);
1223 offset = BINFO_OFFSET (binfo);
1224 }
1225 }
1226
1227 /* For each layer of base class (i.e., the first base class, and each
1228 virtual base class from that one), modify the virtual function table
1229 of the derived class to contain the new virtual function.
1230 A class has as many vfields as it has virtual base classes (total). */
1231 for (vfields = CLASSTYPE_VFIELDS (t); vfields; vfields = TREE_CHAIN (vfields))
1232 {
1233 int normal = 1;
1234 tree binfo, this_offset;
1235 tree base, path;
1236
1237 if (!related_vslot (base_fndecl, vfields, t))
1238 continue;
1239
1240 /* Find the right base class for this derived class, call it BASE. */
1241 base = VF_BASETYPE_VALUE (vfields);
1242
1243 /* Get the path starting from the deepest base class CONTEXT
1244 of T (i.e., first defn of BASE_FNDECL). */
1245 get_base_distance (DECL_CONTEXT (base_fndecl), t, 0, &path);
1246
1247 /* Get our best approximation of what to use for constructing
1248 the virtual function table for T. */
1249 do
1250 {
1251 /* Walk from base toward derived, stopping at the
1252 most derived baseclass that matters. That baseclass
1253 is exactly the one which provides the vtable along
1254 the VFIELD spine, but no more. */
1255 if (TREE_VIA_VIRTUAL (path))
1256 {
1257 base = path;
1258 binfo = binfo_member (BINFO_TYPE (base), CLASSTYPE_VBASECLASSES (t));
1259 break;
1260 }
1261 if (BINFO_INHERITANCE_CHAIN (path) == NULL_TREE
1262 || (BINFO_TYPE (BINFO_BASETYPE (BINFO_INHERITANCE_CHAIN (path), 0))
1263 != BINFO_TYPE (path))
1264 || BINFO_INHERITANCE_CHAIN (BINFO_INHERITANCE_CHAIN (path)) == NULL_TREE)
1265 {
1266 base = path;
1267 binfo = base;
1268 break;
1269 }
1270 path = BINFO_INHERITANCE_CHAIN (path);
1271 }
1272 while (1);
1273
1274 /* Find the right offset for the this pointer based on the base
1275 class we just found. */
1276 base_offset = BINFO_OFFSET (binfo);
1277 this_offset = size_binop (MINUS_EXPR, offset, base_offset);
1278
1279 /* Make sure we can modify the derived association with immunity. */
1280 if (TREE_USED (TYPE_BINFO (t)))
1281 TYPE_BINFO (t) = copy_binfo (TYPE_BINFO (t));
1282
1283 /* We call this case NORMAL iff this virtual function table
1284 pointer field has its storage reserved in this class.
1285 This is normally the case without virtual baseclasses
1286 or off-center multiple baseclasses. */
1287 normal = (vfield != NULL_TREE
1288 && VF_BASETYPE_VALUE (vfields) == DECL_FCONTEXT (vfield)
1289 && (VF_BINFO_VALUE (vfields) == NULL_TREE
1290 || ! TREE_VIA_VIRTUAL (VF_BINFO_VALUE (vfields))));
1291
1292 if (normal && VF_BINFO_VALUE (vfields))
1293 /* Everything looks normal so far...check that we are really
1294 working from VFIELD's basetype, and not some other appearance
1295 of that basetype in the lattice. */
1296 normal = (VF_BINFO_VALUE (vfields)
1297 == get_binfo (VF_BASETYPE_VALUE (vfields), t, 0));
1298
1299 if (normal)
1300 {
1301 /* In this case, it is *type*'s vtable we are modifying.
1302 We start with the approximation that it's vtable is that
1303 of the immediate base class. */
1304 base_context = t;
1305 binfo = TYPE_BINFO (t);
1306 if (! BINFO_NEW_VTABLE_MARKED (binfo))
1307 build_vtable (TYPE_BINFO (DECL_CONTEXT (vfield)), t);
1308 }
1309 else
1310 {
1311 /* This is our very own copy of `basetype' to play with.
1312 Later, we will fill in all the virtual functions
1313 that override the virtual functions in these base classes
1314 which are not defined by the current type. */
1315 if (! BINFO_NEW_VTABLE_MARKED (binfo))
1316 prepare_fresh_vtable (binfo, base, t);
1317 }
1318
51c184be
MS
1319 saved_pfn = get_vtable_entry (BINFO_VIRTUALS (binfo), base_fndecl);
1320 if (saved_pfn)
1321 saved_pfn = TREE_OPERAND (FNADDR_FROM_VTABLE_ENTRY (TREE_VALUE (saved_pfn)), 0);
8d08fdba
MS
1322#ifdef NOTQUITE
1323 cp_warning ("in %D", DECL_NAME (BINFO_VTABLE (binfo)));
1324#endif
1325 /* The this_offset can be wrong, if we try and modify an entry
1326 that had been modified once before. */
51c184be 1327 if (saved_pfn && ! SAME_FN (saved_pfn, fndecl))
8d08fdba
MS
1328 {
1329 modify_vtable_entry (get_vtable_entry (BINFO_VIRTUALS (binfo), base_fndecl),
1330 build_vtable_entry (this_offset, pfn),
1331 fndecl);
1332 modify_other_vtable_entries (t, TYPE_BINFO (t), fndecl, saved_pfn, pfn);
1333 }
1334 }
1335 for (vbases = CLASSTYPE_VBASECLASSES (t); vbases; vbases = TREE_CHAIN (vbases))
1336 {
1337 tree this_offset;
1338 tree base, path;
1339
1340 if (! BINFO_VTABLE (vbases))
1341 /* There are only two ways that a type can fail to have
1342 virtual functions: neither it nor any of its base
1343 types define virtual functions (in which case
1344 no updating need be done), or virtual functions
1345 accessible to it come from virtual base classes
1346 (in which case we have or will get them modified
1347 in other passes of this loop). */
1348 continue;
1349
1350 base = BINFO_TYPE (vbases);
1351 path = NULL_TREE;
1352
1353 if (base != base_context
1354 && get_base_distance (base_context, base, 0, &path) == -1)
1355 continue;
1356
1357 if (path)
1358 this_offset = size_binop (MINUS_EXPR, offset, BINFO_OFFSET (path));
1359 else
1360 this_offset = offset;
1361
1362 /* Doesn't matter if not actually from this virtual base class,
1363 but shouldn't come from deeper virtual baseclasses. The enclosing
1364 loop should take care of such baseclasses. */
1365 while (path)
1366 {
1367 if (TREE_VIA_VIRTUAL (path))
1368 goto skip;
1369 path = BINFO_INHERITANCE_CHAIN (path);
1370 }
1371
1372 base_offset = BINFO_OFFSET (vbases);
1373 this_offset = size_binop (MINUS_EXPR, this_offset, base_offset);
1374
1375 /* Make sure we can modify the derived association with immunity. */
1376 if (TREE_USED (TYPE_BINFO (t)))
1377 TYPE_BINFO (t) = copy_binfo (TYPE_BINFO (t));
1378
1379 /* This is our very own copy of `basetype' to play with. */
1380 if (! BINFO_NEW_VTABLE_MARKED (vbases))
1381 {
1382 tree context_binfo = binfo_value (base_context, base);
1383 prepare_fresh_vtable (vbases, context_binfo, t);
1384 }
1385 saved_pfn = TREE_OPERAND (FNADDR_FROM_VTABLE_ENTRY (TREE_VALUE (get_vtable_entry (BINFO_VIRTUALS (vbases), base_fndecl))), 0);
1386#ifdef NOTQUITE
1387 cp_warning ("in %D", DECL_NAME (BINFO_VTABLE (vbases)));
1388#endif
1389 /* The this_offset can be wrong, if we try and modify an entry
1390 that had been modified once before. */
1391 if (! SAME_FN (saved_pfn, fndecl))
1392 {
1393 modify_vtable_entry (get_vtable_entry (BINFO_VIRTUALS (vbases),
1394 base_fndecl),
1395 build_vtable_entry (this_offset, pfn),
1396 fndecl);
1397 modify_other_vtable_entries (t, TYPE_BINFO (t), fndecl, saved_pfn, pfn);
1398 }
1399 skip: {}
1400 }
1401}
1402
1403static tree
1404add_virtual_function (pending_virtuals, has_virtual, x, t)
1405 tree pending_virtuals;
1406 int *has_virtual;
1407 tree x;
1408 tree t; /* Structure type. */
1409{
1410 int debug_vbase = 1;
1411
1412 /* FUNCTION_TYPEs and OFFSET_TYPEs no longer freely
1413 convert to void *. Make such a conversion here. */
1414 tree vfn = build1 (ADDR_EXPR, ptr_type_node, x);
1415 TREE_CONSTANT (vfn) = 1;
1416
1417 /* current_class_type may be NULL_TREE in case of error. */
8926095f 1418 if (current_class_type && !flag_vtable_thunks)
8d08fdba
MS
1419 TREE_ADDRESSABLE (x) = CLASSTYPE_VTABLE_NEEDS_WRITING (current_class_type);
1420
1421 /* If the virtual function is a redefinition of a prior one,
1422 figure out in which base class the new definition goes,
1423 and if necessary, make a fresh virtual function table
1424 to hold that entry. */
1425 if (DECL_VINDEX (x) == error_mark_node)
1426 {
8926095f 1427 tree entry;
8d08fdba
MS
1428
1429 if (flag_dossier && *has_virtual == 0)
1430 {
1431 /* CLASSTYPE_DOSSIER is only used as a Boolean (NULL or not). */
1432 CLASSTYPE_DOSSIER (t) = integer_one_node;
1433 *has_virtual = 1;
1434 }
1435
1436 /* Build a new INT_CST for this DECL_VINDEX. */
1437#ifdef VTABLE_USES_MASK
1438 SET_DECL_VINDEX (x, build_int_2 (++(*has_virtual), 0));
1439#else
1440 {
1441 static tree index_table[256];
1442 tree index;
1443 int i = ++(*has_virtual);
1444
1445 if (i >= 256 || index_table[i] == 0)
1446 {
1447 index = build_int_2 (i, 0);
1448 if (i < 256)
1449 index_table[i] = index;
1450 }
1451 else
1452 index = index_table[i];
1453
1454 DECL_VINDEX (x) = index;
1455 }
1456#endif
8926095f 1457 entry = build_vtable_entry (integer_zero_node, vfn);
8d08fdba
MS
1458 pending_virtuals = tree_cons (DECL_VINDEX (x), entry, pending_virtuals);
1459 }
1460 /* Happens if declared twice in class or we're not in a class definition.
1461 We will give error later or we've already given it. */
1462 else if (TREE_CODE (DECL_VINDEX (x)) == INTEGER_CST
1463 || current_class_type == NULL_TREE)
1464 return pending_virtuals;
1465 else if (debug_vbase && TYPE_USES_VIRTUAL_BASECLASSES (current_class_type))
1466 {
1467 /* Need an entry in some other virtual function table.
1468 Deal with this after we have laid out our virtual base classes. */
1469 pending_hard_virtuals = temp_tree_cons (x, vfn, pending_hard_virtuals);
1470 }
1471 else
1472 {
1473 /* Need an entry in some other virtual function table.
1474 We can do this now. */
1475 tree base_fndecl_list = DECL_VINDEX (x), base_fndecls, prev = 0;
1476 tree vtable_context = DECL_FCONTEXT (CLASSTYPE_VFIELD (current_class_type));
1477 tree true_base_fndecl = 0;
1478
1479 /* First assign DECL_VINDEX from the base vfn with which
1480 we share our vtable. */
1481 base_fndecls = base_fndecl_list;
1482 while (base_fndecls)
1483 {
1484 if (TREE_CHAIN (base_fndecls) == NULL_TREE
1485 || DECL_FCONTEXT (CLASSTYPE_VFIELD (DECL_CLASS_CONTEXT (TREE_VALUE (base_fndecls)))) == vtable_context)
1486 {
1487 true_base_fndecl = TREE_VALUE (base_fndecls);
1488 modify_vtable_entries (current_class_type, x,
1489 true_base_fndecl, vfn);
1490 if (prev)
1491 TREE_CHAIN (prev) = TREE_CHAIN (base_fndecls);
1492 else
1493 base_fndecl_list = prev;
1494 break;
1495 }
1496 prev = base_fndecls;
1497 base_fndecls = TREE_CHAIN (base_fndecls);
1498 }
1499
1500 /* Now fill in the rest of the vtables. */
1501 base_fndecls = base_fndecl_list;
1502 while (base_fndecls)
1503 {
1504 /* If we haven't found one we like, first one wins. */
1505 if (true_base_fndecl == 0)
1506 true_base_fndecl = TREE_VALUE (base_fndecls);
1507
1508 modify_vtable_entries (current_class_type, x,
1509 TREE_VALUE (base_fndecls), vfn);
1510 base_fndecls = TREE_CHAIN (base_fndecls);
1511 }
1512
1513 DECL_CONTEXT (x) = DECL_CONTEXT (true_base_fndecl);
1514 }
1515 return pending_virtuals;
1516}
1517\f
1518/* Obstack on which to build the vector of class methods. */
1519struct obstack class_obstack;
1520extern struct obstack *current_obstack;
1521
1522/* Add method METHOD to class TYPE. This is used when a method
1523 has been defined which did not initially appear in the class definition,
1524 and helps cut down on spurious error messages.
1525
1526 FIELDS is the entry in the METHOD_VEC vector entry of the class type where
1527 the method should be added. */
1528void
1529add_method (type, fields, method)
1530 tree type, *fields, method;
1531{
1532 /* We must make a copy of METHOD here, since we must be sure that
1533 we have exclusive title to this method's DECL_CHAIN. */
1534 tree decl;
1535
1536 push_obstacks (&permanent_obstack, &permanent_obstack);
1537 {
1538 decl = copy_node (method);
1539 if (DECL_RTL (decl) == 0
1540 && (!processing_template_decl
1541 || !uses_template_parms (decl)))
1542 {
1543 make_function_rtl (decl);
1544 DECL_RTL (method) = DECL_RTL (decl);
1545 }
1546 }
1547
1548 if (fields && *fields)
1549 {
1550 /* Take care not to hide destructor. */
1551 DECL_CHAIN (decl) = DECL_CHAIN (*fields);
1552 DECL_CHAIN (*fields) = decl;
1553 }
1554 else if (CLASSTYPE_METHOD_VEC (type) == 0)
1555 {
1556 tree method_vec = make_node (TREE_VEC);
1557 if (TYPE_IDENTIFIER (type) == DECL_NAME (decl))
1558 {
1559 TREE_VEC_ELT (method_vec, 0) = decl;
1560 TREE_VEC_LENGTH (method_vec) = 1;
1561 }
1562 else
1563 {
1564 /* ??? Is it possible for there to have been enough room in the
1565 current chunk for the tree_vec structure but not a tree_vec
1566 plus a tree*? Will this work in that case? */
1567 obstack_free (current_obstack, method_vec);
1568 obstack_blank (current_obstack, sizeof (struct tree_vec) + sizeof (tree *));
1569 TREE_VEC_ELT (method_vec, 1) = decl;
1570 TREE_VEC_LENGTH (method_vec) = 2;
1571 obstack_finish (current_obstack);
1572 }
1573 CLASSTYPE_METHOD_VEC (type) = method_vec;
1574 }
1575 else
1576 {
1577 tree method_vec = CLASSTYPE_METHOD_VEC (type);
1578 int len = TREE_VEC_LENGTH (method_vec);
1579
1580 /* Adding a new ctor or dtor. This is easy because our
1581 METHOD_VEC always has a slot for such entries. */
1582 if (TYPE_IDENTIFIER (type) == DECL_NAME (decl))
1583 {
1584 /* TREE_VEC_ELT (method_vec, 0) = decl; */
1585 if (decl != TREE_VEC_ELT (method_vec, 0))
1586 {
1587 DECL_CHAIN (decl) = TREE_VEC_ELT (method_vec, 0);
1588 TREE_VEC_ELT (method_vec, 0) = decl;
1589 }
1590 }
1591 else
1592 {
1593 /* This is trickier. We try to extend the TREE_VEC in-place,
1594 but if that does not work, we copy all its data to a new
1595 TREE_VEC that's large enough. */
1596 struct obstack *ob = &class_obstack;
1597 tree *end = (tree *)obstack_next_free (ob);
1598
1599 if (end != TREE_VEC_END (method_vec))
1600 {
1601 ob = current_obstack;
1602 TREE_VEC_LENGTH (method_vec) += 1;
1603 TREE_VEC_ELT (method_vec, len) = NULL_TREE;
1604 method_vec = copy_node (method_vec);
1605 TREE_VEC_LENGTH (method_vec) -= 1;
1606 }
1607 else
1608 {
1609 tree tmp_vec = (tree) obstack_base (ob);
1610 if (obstack_room (ob) < sizeof (tree))
1611 {
1612 obstack_blank (ob, sizeof (struct tree_common)
1613 + tree_code_length[(int) TREE_VEC]
1614 * sizeof (char *)
1615 + len * sizeof (tree));
1616 tmp_vec = (tree) obstack_base (ob);
1617 bcopy (method_vec, tmp_vec,
1618 (sizeof (struct tree_common)
1619 + tree_code_length[(int) TREE_VEC] * sizeof (char *)
1620 + (len-1) * sizeof (tree)));
1621 method_vec = tmp_vec;
1622 }
1623 else
1624 obstack_blank (ob, sizeof (tree));
1625 }
1626
1627 obstack_finish (ob);
1628 TREE_VEC_ELT (method_vec, len) = decl;
1629 TREE_VEC_LENGTH (method_vec) = len + 1;
1630 CLASSTYPE_METHOD_VEC (type) = method_vec;
1631
1632 if (TYPE_BINFO_BASETYPES (type) && CLASSTYPE_BASELINK_VEC (type))
1633 {
1634 /* ??? May be better to know whether these can be extended? */
1635 tree baselink_vec = CLASSTYPE_BASELINK_VEC (type);
1636
1637 TREE_VEC_LENGTH (baselink_vec) += 1;
1638 CLASSTYPE_BASELINK_VEC (type) = copy_node (baselink_vec);
1639 TREE_VEC_LENGTH (baselink_vec) -= 1;
1640
1641 TREE_VEC_ELT (CLASSTYPE_BASELINK_VEC (type), len) = 0;
1642 }
1643 }
1644 }
1645 DECL_CONTEXT (decl) = type;
1646 DECL_CLASS_CONTEXT (decl) = type;
1647
1648 pop_obstacks ();
1649}
1650
1651/* Subroutines of finish_struct. */
1652
1653/* Look through the list of fields for this struct, deleting
1654 duplicates as we go. This must be recursive to handle
1655 anonymous unions.
1656
1657 FIELD is the field which may not appear anywhere in FIELDS.
1658 FIELD_PTR, if non-null, is the starting point at which
1659 chained deletions may take place.
1660 The value returned is the first acceptable entry found
1661 in FIELDS.
1662
1663 Note that anonymous fields which are not of UNION_TYPE are
1664 not duplicates, they are just anonymous fields. This happens
1665 when we have unnamed bitfields, for example. */
1666static tree
1667delete_duplicate_fields_1 (field, field_ptr, fields)
1668 tree field, *field_ptr, fields;
1669{
1670 tree x;
1671 tree prev = field_ptr ? *field_ptr : 0;
1672 if (DECL_NAME (field) == 0)
1673 {
1674 if (TREE_CODE (TREE_TYPE (field)) != UNION_TYPE)
1675 return fields;
1676
1677 for (x = TYPE_FIELDS (TREE_TYPE (field)); x; x = TREE_CHAIN (x))
1678 fields = delete_duplicate_fields_1 (x, field_ptr, fields);
1679 if (prev)
1680 TREE_CHAIN (prev) = fields;
1681 return fields;
1682 }
1683 else
1684 {
1685 for (x = fields; x; prev = x, x = TREE_CHAIN (x))
1686 {
1687 if (DECL_NAME (x) == 0)
1688 {
1689 if (TREE_CODE (TREE_TYPE (x)) != UNION_TYPE)
1690 continue;
1691 TYPE_FIELDS (TREE_TYPE (x))
1692 = delete_duplicate_fields_1 (field, (tree *)0, TYPE_FIELDS (TREE_TYPE (x)));
1693 if (TYPE_FIELDS (TREE_TYPE (x)) == 0)
1694 {
1695 if (prev == 0)
1696 fields = TREE_CHAIN (fields);
1697 else
1698 TREE_CHAIN (prev) = TREE_CHAIN (x);
1699 }
1700 }
1701 else
1702 {
1703 if (DECL_NAME (field) == DECL_NAME (x))
1704 {
1705 if (TREE_CODE (field) == CONST_DECL
1706 && TREE_CODE (x) == CONST_DECL)
1707 cp_error_at ("duplicate enum value `%D'", x);
1708 else if (TREE_CODE (field) == CONST_DECL
1709 || TREE_CODE (x) == CONST_DECL)
1710 cp_error_at ("duplicate field `%D' (as enum and non-enum)",
1711 x);
1712 else if (TREE_CODE (field) == TYPE_DECL
1713 && TREE_CODE (x) == TYPE_DECL)
1714 cp_error_at ("duplicate class scope type `%D'", x);
1715 else if (TREE_CODE (field) == TYPE_DECL
1716 || TREE_CODE (x) == TYPE_DECL)
1717 cp_error_at ("duplicate field `%D' (as type and non-type)",
1718 x);
1719 else
1720 cp_error_at ("duplicate member `%D'", x);
1721 if (prev == 0)
1722 fields = TREE_CHAIN (fields);
1723 else
1724 TREE_CHAIN (prev) = TREE_CHAIN (x);
1725 }
1726 }
1727 }
1728 }
1729 return fields;
1730}
1731
1732static void
1733delete_duplicate_fields (fields)
1734 tree fields;
1735{
1736 tree x;
1737 for (x = fields; x && TREE_CHAIN (x); x = TREE_CHAIN (x))
1738 TREE_CHAIN (x) = delete_duplicate_fields_1 (x, &x, TREE_CHAIN (x));
1739}
1740
1741/* Change the access of FDECL to ACCESS in T.
1742 Return 1 if change was legit, otherwise return 0. */
1743static int
1744alter_access (t, fdecl, access)
1745 tree t;
1746 tree fdecl;
1747 enum access_type access;
1748{
1749 tree elem = purpose_member (t, DECL_ACCESS (fdecl));
1750 if (elem && TREE_VALUE (elem) != (tree)access)
1751 {
1752 if (TREE_CODE (TREE_TYPE (fdecl)) == FUNCTION_DECL)
1753 {
1754 cp_error_at ("conflicting access specifications for method `%D', ignored", TREE_TYPE (fdecl));
1755 }
1756 else
1757 error ("conflicting access specifications for field `%s', ignored",
1758 IDENTIFIER_POINTER (DECL_NAME (fdecl)));
1759 }
1760 else if (TREE_PRIVATE (fdecl) && access != access_private)
1761 cp_error_at ("cannot make private `%D' non-private", fdecl);
1762 else if (TREE_PROTECTED (fdecl))
1763 {
1764 if (access == access_public)
1765 cp_error_at ("cannot make protected `%D' public", fdecl);
1766 goto alter;
1767 }
1768 /* ARM 11.3: an access declaration may not be used to restrict access
1769 to a member that is accessible in the base class. */
1770 else if (TREE_PUBLIC (fdecl)
1771 && (access == access_private
1772 || access == access_protected))
1773 cp_error_at ("cannot reduce access of public member `%D'", fdecl);
1774 else if (elem == NULL_TREE)
1775 {
1776 alter:
1777 DECL_ACCESS (fdecl) = tree_cons (t, (tree)access,
1778 DECL_ACCESS (fdecl));
1779 return 1;
1780 }
1781 return 0;
1782}
1783
51c184be
MS
1784/* Return the offset to the main vtable for a given base BINFO. */
1785tree
8d08fdba
MS
1786get_vfield_offset (binfo)
1787 tree binfo;
1788{
1789 return size_binop (PLUS_EXPR,
51c184be
MS
1790 size_binop (FLOOR_DIV_EXPR,
1791 DECL_FIELD_BITPOS (CLASSTYPE_VFIELD (BINFO_TYPE (binfo))),
1792 size_int (BITS_PER_UNIT)),
8d08fdba
MS
1793 BINFO_OFFSET (binfo));
1794}
1795
1796/* If FOR_TYPE needs to reinitialize virtual function table pointers
1797 for TYPE's sub-objects, add such reinitializations to BASE_INIT_LIST.
1798 Returns BASE_INIT_LIST appropriately modified. */
1799
1800static tree
1801maybe_fixup_vptrs (for_type, binfo, base_init_list)
1802 tree for_type, binfo, base_init_list;
1803{
1804 /* Now reinitialize any slots that don't fall under our virtual
1805 function table pointer. */
1806 tree vfields = CLASSTYPE_VFIELDS (BINFO_TYPE (binfo));
1807 while (vfields)
1808 {
1809 tree basetype = VF_NORMAL_VALUE (vfields)
1810 ? TYPE_MAIN_VARIANT (VF_NORMAL_VALUE (vfields))
1811 : VF_BASETYPE_VALUE (vfields);
1812
1813 tree base_binfo = get_binfo (basetype, for_type, 0);
1814 /* Punt until this is implemented. */
1815 if (1 /* BINFO_MODIFIED (base_binfo) */)
1816 {
1817 tree base_offset = get_vfield_offset (base_binfo);
1818 if (! tree_int_cst_equal (base_offset, get_vfield_offset (TYPE_BINFO (for_type)))
1819 && ! tree_int_cst_equal (base_offset, get_vfield_offset (binfo)))
1820 base_init_list = tree_cons (error_mark_node, base_binfo,
1821 base_init_list);
1822 }
1823 vfields = TREE_CHAIN (vfields);
1824 }
1825 return base_init_list;
1826}
1827
1828/* If TYPE does not have a constructor, then the compiler must
1829 manually deal with all of the initialization this type requires.
1830
1831 If a base initializer exists only to fill in the virtual function
1832 table pointer, then we mark that fact with the TREE_VIRTUAL bit.
1833 This way, we avoid multiple initializations of the same field by
1834 each virtual function table up the class hierarchy.
1835
1836 Virtual base class pointers are not initialized here. They are
1837 initialized only at the "top level" of object creation. If we
1838 initialized them here, we would have to skip a lot of work. */
1839
1840static void
1841build_class_init_list (type)
1842 tree type;
1843{
1844 tree base_init_list = NULL_TREE;
1845 tree member_init_list = NULL_TREE;
1846
1847 /* Since we build member_init_list and base_init_list using
1848 tree_cons, backwards fields the all through work. */
1849 tree x;
1850 tree binfos = BINFO_BASETYPES (TYPE_BINFO (type));
1851 int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
1852
1853 for (x = TYPE_FIELDS (type); x; x = TREE_CHAIN (x))
1854 {
1855 if (TREE_CODE (x) != FIELD_DECL)
1856 continue;
1857
1858 if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (x))
1859 || DECL_INITIAL (x) != NULL_TREE)
1860 member_init_list = tree_cons (x, type, member_init_list);
1861 }
1862 member_init_list = nreverse (member_init_list);
1863
1864 /* We will end up doing this last. Need special marker
1865 to avoid infinite regress. */
1866 if (TYPE_VIRTUAL_P (type))
1867 {
1868 base_init_list = build_tree_list (error_mark_node, TYPE_BINFO (type));
1869 if (CLASSTYPE_NEEDS_VIRTUAL_REINIT (type) == 0)
1870 TREE_VALUE (base_init_list) = NULL_TREE;
1871 TREE_ADDRESSABLE (base_init_list) = 1;
1872 }
1873
1874 /* Each base class which needs to have initialization
1875 of some kind gets to make such requests known here. */
1876 for (i = n_baseclasses-1; i >= 0; i--)
1877 {
1878 tree base_binfo = TREE_VEC_ELT (binfos, i);
1879 tree blist;
1880
1881 /* Don't initialize virtual baseclasses this way. */
1882 if (TREE_VIA_VIRTUAL (base_binfo))
1883 continue;
1884
1885 if (TYPE_HAS_CONSTRUCTOR (BINFO_TYPE (base_binfo)))
1886 {
1887 /* ...and the last shall come first... */
1888 base_init_list = maybe_fixup_vptrs (type, base_binfo, base_init_list);
1889 base_init_list = tree_cons (NULL_TREE, base_binfo, base_init_list);
1890 continue;
1891 }
1892
1893 if ((blist = CLASSTYPE_BASE_INIT_LIST (BINFO_TYPE (base_binfo))) == NULL_TREE)
1894 /* Nothing to initialize. */
1895 continue;
1896
1897 /* ...ditto... */
1898 base_init_list = maybe_fixup_vptrs (type, base_binfo, base_init_list);
1899
1900 /* This is normally true for single inheritance.
1901 The win is we can shrink the chain of initializations
1902 to be done by only converting to the actual type
1903 we are interested in. */
1904 if (TREE_VALUE (blist)
1905 && TREE_CODE (TREE_VALUE (blist)) == TREE_VEC
1906 && tree_int_cst_equal (BINFO_OFFSET (base_binfo),
1907 BINFO_OFFSET (TREE_VALUE (blist))))
1908 {
1909 if (base_init_list)
1910 {
1911 /* Does it do more than just fill in a
1912 virtual function table pointer? */
1913 if (! TREE_ADDRESSABLE (blist))
1914 base_init_list = build_tree_list (blist, base_init_list);
1915 /* Can we get by just with the virtual function table
1916 pointer that it fills in? */
1917 else if (TREE_ADDRESSABLE (base_init_list)
1918 && TREE_VALUE (base_init_list) == 0)
1919 base_init_list = blist;
1920 /* Maybe, but it is not obvious as the previous case. */
1921 else if (! CLASSTYPE_NEEDS_VIRTUAL_REINIT (type))
1922 {
1923 tree last = tree_last (base_init_list);
1924 while (TREE_VALUE (last)
1925 && TREE_CODE (TREE_VALUE (last)) == TREE_LIST)
1926 last = tree_last (TREE_VALUE (last));
1927 if (TREE_VALUE (last) == 0)
1928 base_init_list = build_tree_list (blist, base_init_list);
1929 }
1930 }
1931 else
1932 base_init_list = blist;
1933 }
1934 else
1935 {
1936 /* The function expand_aggr_init knows how to do the
1937 initialization of `basetype' without getting
1938 an explicit `blist'. */
1939 if (base_init_list)
1940 base_init_list = tree_cons (NULL_TREE, base_binfo, base_init_list);
1941 else
1942 base_init_list = CLASSTYPE_BINFO_AS_LIST (BINFO_TYPE (base_binfo));
1943 }
1944 }
1945
1946 if (base_init_list)
1947 if (member_init_list)
1948 CLASSTYPE_BASE_INIT_LIST (type) = build_tree_list (base_init_list, member_init_list);
1949 else
1950 CLASSTYPE_BASE_INIT_LIST (type) = base_init_list;
1951 else if (member_init_list)
1952 CLASSTYPE_BASE_INIT_LIST (type) = member_init_list;
1953}
1954\f
1955struct base_info
1956{
1957 int has_virtual;
1958 int max_has_virtual;
1959 int n_ancestors;
1960 tree vfield;
1961 tree vfields;
1962 char cant_have_default_ctor;
1963 char cant_have_const_ctor;
1964 char cant_synth_copy_ctor;
1965 char cant_synth_asn_ref;
1966 char no_const_asn_ref;
1967 char needs_virtual_dtor;
1968};
1969
1970/* Record information about type T derived from its base classes.
1971 Store most of that information in T itself, and place the
1972 remaining information in the struct BASE_INFO.
1973
1974 Propagate basetype offsets throughout the lattice. Note that the
1975 lattice topped by T is really a pair: it's a DAG that gives the
1976 structure of the derivation hierarchy, and it's a list of the
1977 virtual baseclasses that appear anywhere in the DAG. When a vbase
1978 type appears in the DAG, it's offset is 0, and it's children start
1979 their offsets from that point. When a vbase type appears in the list,
1980 its offset is the offset it has in the hierarchy, and its children's
1981 offsets include that offset in theirs.
1982
1983 Returns the index of the first base class to have virtual functions,
1984 or -1 if no such base class.
1985
1986 Note that at this point TYPE_BINFO (t) != t_binfo. */
1987
1988static int
1989finish_base_struct (t, b, t_binfo)
1990 tree t;
1991 struct base_info *b;
1992 tree t_binfo;
1993{
1994 tree binfos = BINFO_BASETYPES (t_binfo);
1995 int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
1996 int first_vfn_base_index = -1;
1997 bzero (b, sizeof (struct base_info));
1998
1999 for (i = 0; i < n_baseclasses; i++)
2000 {
2001 tree base_binfo = TREE_VEC_ELT (binfos, i);
2002 tree basetype = BINFO_TYPE (base_binfo);
2003
2004 /* If the type of basetype is incomplete, then
2005 we already complained about that fact
2006 (and we should have fixed it up as well). */
2007 if (TYPE_SIZE (basetype) == 0)
2008 {
2009 int j;
2010 /* The base type is of incomplete type. It is
2011 probably best to pretend that it does not
2012 exist. */
2013 if (i == n_baseclasses-1)
2014 TREE_VEC_ELT (binfos, i) = NULL_TREE;
2015 TREE_VEC_LENGTH (binfos) -= 1;
2016 n_baseclasses -= 1;
2017 for (j = i; j+1 < n_baseclasses; j++)
2018 TREE_VEC_ELT (binfos, j) = TREE_VEC_ELT (binfos, j+1);
2019 }
2020
2021 if (TYPE_HAS_INIT_REF (basetype)
2022 && !TYPE_HAS_CONST_INIT_REF (basetype))
2023 b->cant_have_const_ctor = 1;
2024 if (! TYPE_HAS_INIT_REF (basetype)
2025 || (TYPE_HAS_NONPUBLIC_CTOR (basetype) == 2
2026 && ! is_friend_type (t, basetype)))
2027 b->cant_synth_copy_ctor = 1;
2028
2029 if (TYPE_HAS_CONSTRUCTOR (basetype)
2030 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype))
2031 {
2032 b->cant_have_default_ctor = 1;
2033 if (! TYPE_HAS_CONSTRUCTOR (t))
2034 {
2035 cp_pedwarn ("base `%T' with only non-default constructor",
2036 basetype);
2037 cp_pedwarn ("in class without a constructor");
2038 }
2039 }
2040
2041 if (TYPE_HAS_ASSIGN_REF (basetype)
2042 && !TYPE_HAS_CONST_ASSIGN_REF (basetype))
2043 b->no_const_asn_ref = 1;
2044 if (! TYPE_HAS_ASSIGN_REF (basetype)
2045 || (TYPE_HAS_NONPUBLIC_ASSIGN_REF (basetype) == 2
2046 && ! is_friend_type (t, basetype)))
2047 b->cant_synth_asn_ref = 1;
2048
2049 b->n_ancestors += CLASSTYPE_N_SUPERCLASSES (basetype);
2050 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (basetype);
2051 TYPE_NEEDS_DESTRUCTOR (t) |= TYPE_NEEDS_DESTRUCTOR (basetype);
2052 TYPE_HAS_COMPLEX_ASSIGN_REF (t) |= TYPE_HAS_COMPLEX_ASSIGN_REF (basetype);
2053 TYPE_HAS_COMPLEX_INIT_REF (t) |= (TYPE_HAS_COMPLEX_INIT_REF (basetype)
2054 || TYPE_NEEDS_CONSTRUCTING (basetype));
2055
2056 TYPE_OVERLOADS_CALL_EXPR (t) |= TYPE_OVERLOADS_CALL_EXPR (basetype);
2057 TYPE_OVERLOADS_ARRAY_REF (t) |= TYPE_OVERLOADS_ARRAY_REF (basetype);
2058 TYPE_OVERLOADS_ARROW (t) |= TYPE_OVERLOADS_ARROW (basetype);
2059
2060 if (! TREE_VIA_VIRTUAL (base_binfo)
2061#if 0
2062 /* This cannot be done, as prepare_fresh_vtable wants to modify
2063 binfos associated with vfields anywhere in the hierarchy, not
2064 just immediate base classes. Due to unsharing, the compiler
2065 might consume 3% more memory on a real program.
2066 */
2067 && ! BINFO_OFFSET_ZEROP (base_binfo)
2068#endif
2069 && BINFO_BASETYPES (base_binfo))
2070 {
2071 tree base_binfos = BINFO_BASETYPES (base_binfo);
2072 tree chain = NULL_TREE;
2073 int j;
2074
2075 /* Now unshare the structure beneath BASE_BINFO. */
2076 for (j = TREE_VEC_LENGTH (base_binfos)-1;
2077 j >= 0; j--)
2078 {
2079 tree base_base_binfo = TREE_VEC_ELT (base_binfos, j);
2080 if (! TREE_VIA_VIRTUAL (base_base_binfo))
2081 TREE_VEC_ELT (base_binfos, j)
2082 = make_binfo (BINFO_OFFSET (base_base_binfo),
8926095f 2083 base_base_binfo,
8d08fdba
MS
2084 BINFO_VTABLE (base_base_binfo),
2085 BINFO_VIRTUALS (base_base_binfo),
2086 chain);
2087 chain = TREE_VEC_ELT (base_binfos, j);
2088 TREE_VIA_PUBLIC (chain) = TREE_VIA_PUBLIC (base_base_binfo);
2089 TREE_VIA_PROTECTED (chain) = TREE_VIA_PROTECTED (base_base_binfo);
2090 }
2091
2092 /* Completely unshare potentially shared data, and
2093 update what is ours. */
2094 propagate_binfo_offsets (base_binfo, BINFO_OFFSET (base_binfo));
2095 }
2096
2097 if (! TREE_VIA_VIRTUAL (base_binfo))
2098 CLASSTYPE_N_SUPERCLASSES (t) += 1;
2099
2100 if (TYPE_VIRTUAL_P (basetype))
2101 {
2102 /* If there's going to be a destructor needed, make
2103 sure it will be virtual. */
2104 b->needs_virtual_dtor = 1;
2105
2106 /* Don't borrow virtuals from virtual baseclasses. */
2107 if (TREE_VIA_VIRTUAL (base_binfo))
2108 continue;
2109
2110 if (first_vfn_base_index < 0)
2111 {
2112 tree vfields;
2113 first_vfn_base_index = i;
2114
2115 b->has_virtual = CLASSTYPE_VSIZE (basetype);
2116 b->vfield = CLASSTYPE_VFIELD (basetype);
2117 b->vfields = copy_list (CLASSTYPE_VFIELDS (basetype));
2118 vfields = b->vfields;
2119 while (vfields)
2120 {
2121 if (VF_BINFO_VALUE (vfields) == NULL_TREE
2122 || ! TREE_VIA_VIRTUAL (VF_BINFO_VALUE (vfields)))
2123 {
2124 tree value = VF_BASETYPE_VALUE (vfields);
2125 if (DECL_NAME (CLASSTYPE_VFIELD (value))
2126 == DECL_NAME (CLASSTYPE_VFIELD (basetype)))
2127 VF_NORMAL_VALUE (b->vfields) = basetype;
2128 else
2129 VF_NORMAL_VALUE (b->vfields) = VF_NORMAL_VALUE (vfields);
2130 }
2131 vfields = TREE_CHAIN (vfields);
2132 }
2133 CLASSTYPE_VFIELD (t) = b->vfield;
2134 }
2135 else
2136 {
2137 /* Only add unique vfields, and flatten them out as we go. */
2138 tree vfields = CLASSTYPE_VFIELDS (basetype);
2139 while (vfields)
2140 {
2141 if (VF_BINFO_VALUE (vfields) == NULL_TREE
2142 || ! TREE_VIA_VIRTUAL (VF_BINFO_VALUE (vfields)))
2143 {
2144 tree value = VF_BASETYPE_VALUE (vfields);
2145 b->vfields = tree_cons (base_binfo, value, b->vfields);
2146 if (DECL_NAME (CLASSTYPE_VFIELD (value))
2147 == DECL_NAME (CLASSTYPE_VFIELD (basetype)))
2148 VF_NORMAL_VALUE (b->vfields) = basetype;
2149 else
2150 VF_NORMAL_VALUE (b->vfields) = VF_NORMAL_VALUE (vfields);
2151 }
2152 vfields = TREE_CHAIN (vfields);
2153 }
2154
2155 if (b->has_virtual == 0)
2156 {
2157 first_vfn_base_index = i;
2158 b->has_virtual = CLASSTYPE_VSIZE (basetype);
2159 b->vfield = CLASSTYPE_VFIELD (basetype);
2160 CLASSTYPE_VFIELD (t) = b->vfield;
2161 /* When we install the first one, set the VF_NORMAL_VALUE
2162 to be the current class, as this it is the most derived
2163 class. Hopefully, this is not set to something else
2164 later. (mrs) */
2165 vfields = b->vfields;
2166 while (vfields)
2167 {
2168 if (DECL_NAME (CLASSTYPE_VFIELD (t))
2169 == DECL_NAME (CLASSTYPE_VFIELD (basetype)))
2170 {
2171 VF_NORMAL_VALUE (vfields) = t;
2172 /* There should only be one of them! And it should
2173 always be found, if we get into here. (mrs) */
2174 break;
2175 }
2176 vfields = TREE_CHAIN (vfields);
2177 }
2178 }
2179 }
2180 }
2181 }
2182
2183 /* Must come after offsets are fixed for all bases. */
2184 for (i = 0; i < n_baseclasses; i++)
2185 {
2186 tree base_binfo = TREE_VEC_ELT (binfos, i);
2187 tree basetype = BINFO_TYPE (base_binfo);
2188
2189 if (get_base_distance (basetype, t_binfo, 0, (tree*)0) == -2)
2190 {
2191 cp_warning ("direct base `%T' inaccessible in `%T' due to ambiguity",
2192 basetype, t);
2193 b->cant_synth_asn_ref = 1;
2194 b->cant_synth_copy_ctor = 1;
2195 }
2196 }
51c184be
MS
2197 {
2198 tree v = get_vbase_types (t_binfo);
2199
2200 for (; v; v = TREE_CHAIN (v))
2201 {
2202 tree basetype = BINFO_TYPE (v);
2203 if (get_base_distance (basetype, t_binfo, 0, (tree*)0) == -2)
2204 {
2205 if (extra_warnings)
2206 cp_warning ("virtual base `%T' inaccessible in `%T' due to ambiguity",
2207 basetype, t);
2208 b->cant_synth_asn_ref = 1;
2209 b->cant_synth_copy_ctor = 1;
2210 }
2211 }
2212 }
8d08fdba
MS
2213
2214 {
2215 tree vfields;
2216 /* Find the base class with the largest number of virtual functions. */
2217 for (vfields = b->vfields; vfields; vfields = TREE_CHAIN (vfields))
2218 {
2219 if (CLASSTYPE_VSIZE (VF_BASETYPE_VALUE (vfields)) > b->max_has_virtual)
2220 b->max_has_virtual = CLASSTYPE_VSIZE (VF_BASETYPE_VALUE (vfields));
2221 if (VF_DERIVED_VALUE (vfields)
2222 && CLASSTYPE_VSIZE (VF_DERIVED_VALUE (vfields)) > b->max_has_virtual)
2223 b->max_has_virtual = CLASSTYPE_VSIZE (VF_DERIVED_VALUE (vfields));
2224 }
2225 }
2226
2227 if (b->vfield == 0)
2228 /* If all virtual functions come only from virtual baseclasses. */
2229 return -1;
2230 return first_vfn_base_index;
2231}
2232
2233static int
2234typecode_p (type, code)
2235 tree type;
2236 enum tree_code code;
2237{
2238 return (TREE_CODE (type) == code
2239 || (TREE_CODE (type) == REFERENCE_TYPE
2240 && TREE_CODE (TREE_TYPE (type)) == code));
2241}
2242\f
2243/* Set memoizing fields and bits of T (and its variants) for later use.
2244 MAX_HAS_VIRTUAL is the largest size of any T's virtual function tables. */
2245static void
2246finish_struct_bits (t, max_has_virtual)
2247 tree t;
2248 int max_has_virtual;
2249{
2250 int i, n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
2251 tree method_vec = CLASSTYPE_METHOD_VEC (t);
2252
2253 /* Fix up variants (if any). */
2254 tree variants = TYPE_NEXT_VARIANT (t);
2255 while (variants)
2256 {
2257 /* These fields are in the _TYPE part of the node, not in
2258 the TYPE_LANG_SPECIFIC component, so they are not shared. */
2259 TYPE_HAS_CONSTRUCTOR (variants) = TYPE_HAS_CONSTRUCTOR (t);
2260 TYPE_HAS_DESTRUCTOR (variants) = TYPE_HAS_DESTRUCTOR (t);
2261 TYPE_NEEDS_CONSTRUCTING (variants) = TYPE_NEEDS_CONSTRUCTING (t);
2262 TYPE_NEEDS_DESTRUCTOR (variants) = TYPE_NEEDS_DESTRUCTOR (t);
2263
2264 TYPE_USES_COMPLEX_INHERITANCE (variants) = TYPE_USES_COMPLEX_INHERITANCE (t);
2265 TYPE_VIRTUAL_P (variants) = TYPE_VIRTUAL_P (t);
2266 TYPE_USES_VIRTUAL_BASECLASSES (variants) = TYPE_USES_VIRTUAL_BASECLASSES (t);
2267 /* Copy whatever these are holding today. */
2268 TYPE_MIN_VALUE (variants) = TYPE_MIN_VALUE (t);
2269 TYPE_MAX_VALUE (variants) = TYPE_MAX_VALUE (t);
2270 variants = TYPE_NEXT_VARIANT (variants);
2271 }
2272
2273 if (n_baseclasses && max_has_virtual)
2274 {
2275 /* Done by `finish_struct' for classes without baseclasses. */
2276 int has_abstract_virtuals = CLASSTYPE_ABSTRACT_VIRTUALS (t) != 0;
2277 tree binfos = TYPE_BINFO_BASETYPES (t);
2278 for (i = n_baseclasses-1; i >= 0; i--)
2279 {
2280 has_abstract_virtuals
2281 |= (CLASSTYPE_ABSTRACT_VIRTUALS (BINFO_TYPE (TREE_VEC_ELT (binfos, i))) != 0);
2282 if (has_abstract_virtuals)
2283 break;
2284 }
2285 if (has_abstract_virtuals)
2286 CLASSTYPE_ABSTRACT_VIRTUALS (t) = get_abstract_virtuals (t);
2287 }
2288
2289 if (n_baseclasses)
2290 {
2291 /* Notice whether this class has type conversion functions defined. */
2292 tree binfo = TYPE_BINFO (t);
2293 tree binfos = BINFO_BASETYPES (binfo);
2294 tree basetype;
2295
2296 for (i = n_baseclasses-1; i >= 0; i--)
2297 {
2298 basetype = BINFO_TYPE (TREE_VEC_ELT (binfos, i));
2299
2300 if (TYPE_HAS_CONVERSION (basetype))
2301 {
2302 TYPE_HAS_CONVERSION (t) = 1;
2303 TYPE_HAS_INT_CONVERSION (t) |= TYPE_HAS_INT_CONVERSION (basetype);
2304 TYPE_HAS_REAL_CONVERSION (t) |= TYPE_HAS_REAL_CONVERSION (basetype);
2305 }
2306 if (CLASSTYPE_MAX_DEPTH (basetype) >= CLASSTYPE_MAX_DEPTH (t))
2307 CLASSTYPE_MAX_DEPTH (t) = CLASSTYPE_MAX_DEPTH (basetype) + 1;
2308 }
2309 }
2310
2311 /* Need to test METHOD_VEC here in case all methods
2312 (conversions and otherwise) are inherited. */
2313 if (TYPE_HAS_CONVERSION (t) && method_vec != NULL_TREE)
2314 {
2315 tree first_conversions[last_conversion_type];
2316 tree last_conversions[last_conversion_type];
2317 enum conversion_type conv_index;
2318 tree *tmp;
2319 int i;
2320
2321 bzero (first_conversions, sizeof (first_conversions));
2322 bzero (last_conversions, sizeof (last_conversions));
2323 for (tmp = &TREE_VEC_ELT (method_vec, 1);
2324 tmp != TREE_VEC_END (method_vec); tmp += 1)
2325 {
2326 /* ??? This should compare DECL_NAME (*tmp) == ansi_opname[TYPE_EXPR]. */
2327 if (IDENTIFIER_TYPENAME_P (DECL_ASSEMBLER_NAME (*tmp)))
2328 {
2329 tree fntype = TREE_TYPE (*tmp);
2330 tree return_type = TREE_TYPE (fntype);
2331 my_friendly_assert (TREE_CODE (fntype) == METHOD_TYPE, 171);
2332
2333 if (typecode_p (return_type, POINTER_TYPE))
2334 {
2335 if (TYPE_READONLY (TREE_TYPE (return_type)))
2336 conv_index = constptr_conv;
2337 else
2338 conv_index = ptr_conv;
2339 }
2340 else if (typecode_p (return_type, INTEGER_TYPE))
2341 {
2342 TYPE_HAS_INT_CONVERSION (t) = 1;
2343 conv_index = int_conv;
2344 }
2345 else if (typecode_p (return_type, REAL_TYPE))
2346 {
2347 TYPE_HAS_REAL_CONVERSION (t) = 1;
2348 conv_index = real_conv;
2349 }
2350 else
2351 continue;
2352
2353 if (first_conversions[(int) conv_index] == NULL_TREE)
2354 first_conversions[(int) conv_index] = *tmp;
2355 last_conversions[(int) conv_index] = *tmp;
2356 }
2357 }
2358
2359 for (i = 0; i < (int) last_conversion_type; i++)
2360 if (first_conversions[i] != last_conversions[i])
2361 CLASSTYPE_CONVERSION (t, i) = error_mark_node;
2362 else
2363 CLASSTYPE_CONVERSION (t, i) = first_conversions[i];
2364 }
2365
2366 /* If this type has constructors, force its mode to be BLKmode,
2367 and force its TREE_ADDRESSABLE bit to be nonzero. */
2368 if (TYPE_NEEDS_CONSTRUCTING (t) || TYPE_NEEDS_DESTRUCTOR (t))
2369 {
2370 tree variants = t;
2371
2372 if (TREE_CODE (TYPE_NAME (t)) == TYPE_DECL)
2373 DECL_MODE (TYPE_NAME (t)) = BLKmode;
2374 while (variants)
2375 {
2376 TYPE_MODE (variants) = BLKmode;
2377 TREE_ADDRESSABLE (variants) = 1;
2378 variants = TYPE_NEXT_VARIANT (variants);
2379 }
2380 }
2381}
2382
2383/* Warn about duplicate methods in fn_fields. Also compact method
2384 lists so that lookup can be made faster.
2385
2386 Algorithm: Outer loop builds lists by method name. Inner loop
2387 checks for redundant method names within a list.
2388
2389 Data Structure: List of method lists. The outer list is a
2390 TREE_LIST, whose TREE_PURPOSE field is the field name and the
2391 TREE_VALUE is the TREE_CHAIN of the FUNCTION_DECLs. Friends are
2392 chained in the same way as member functions, but they live in the
2393 TREE_TYPE field of the outer list. That allows them to be quickly
2394 deleted, and requires no extra storage.
2395
2396 If there are any constructors/destructors, they are moved to the
2397 front of the list. This makes pushclass more efficient.
2398
2399 We also link each field which has shares a name with its baseclass
2400 to the head of the list of fields for that base class. This allows
2401 us to reduce search time in places like `build_method_call' to
2402 consider only reasonably likely functions. */
2403
2404static tree
2405finish_struct_methods (t, fn_fields, nonprivate_method)
2406 tree t;
2407 tree fn_fields;
2408 int nonprivate_method;
2409{
2410 tree method_vec;
2411 tree name = constructor_name (t);
2412 int i, n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
2413
2414 /* Now prepare to gather fn_fields into vector. */
2415 struct obstack *ambient_obstack = current_obstack;
2416 current_obstack = &class_obstack;
2417 method_vec = make_node (TREE_VEC);
2418 /* Room has been saved for constructors and destructors. */
2419 current_obstack = ambient_obstack;
2420 /* Now make this a live vector. */
2421 obstack_free (&class_obstack, method_vec);
2422 obstack_blank (&class_obstack, sizeof (struct tree_vec));
2423
2424 while (fn_fields)
2425 {
2426 /* NEXT Pointer, TEST Pointer, and BASE Pointer. */
2427 tree nextp, *testp;
2428 tree fn_name = DECL_NAME (fn_fields);
2429 if (fn_name == NULL_TREE)
2430 fn_name = name;
2431
2432 nextp = TREE_CHAIN (fn_fields);
2433 TREE_CHAIN (fn_fields) = NULL_TREE;
2434
2435 /* Clear out this flag.
2436
2437 @@ Doug may figure out how to break
2438 @@ this with nested classes and friends. */
2439 DECL_IN_AGGR_P (fn_fields) = 0;
2440
2441 /* Note here that a copy ctor is private, so we don't dare generate
2442 a default copy constructor for a class that has a member
2443 of this type without making sure they have access to it. */
2444 if (fn_name == name)
2445 {
2446 tree parmtypes = FUNCTION_ARG_CHAIN (fn_fields);
2447 tree parmtype = parmtypes ? TREE_VALUE (parmtypes) : void_type_node;
2448
2449 if (TREE_CODE (parmtype) == REFERENCE_TYPE
2450 && TYPE_MAIN_VARIANT (TREE_TYPE (parmtype)) == t)
2451 {
2452 if (TREE_CHAIN (parmtypes) == NULL_TREE
2453 || TREE_CHAIN (parmtypes) == void_list_node
2454 || TREE_PURPOSE (TREE_CHAIN (parmtypes)))
2455 {
2456 if (TREE_PROTECTED (fn_fields))
2457 TYPE_HAS_NONPUBLIC_CTOR (t) = 1;
2458 else if (TREE_PRIVATE (fn_fields))
2459 TYPE_HAS_NONPUBLIC_CTOR (t) = 2;
2460 }
2461 }
2462 }
2463 else if (fn_name == ansi_opname[(int) MODIFY_EXPR])
2464 {
2465 tree parmtype = TREE_VALUE (FUNCTION_ARG_CHAIN (fn_fields));
2466
2467 if (TREE_CODE (parmtype) == REFERENCE_TYPE
2468 && TYPE_MAIN_VARIANT (TREE_TYPE (parmtype)) == t)
2469 {
2470 if (TREE_PROTECTED (fn_fields))
2471 TYPE_HAS_NONPUBLIC_ASSIGN_REF (t) = 1;
2472 else if (TREE_PRIVATE (fn_fields))
2473 TYPE_HAS_NONPUBLIC_ASSIGN_REF (t) = 2;
2474 }
2475 }
2476
2477 /* Constructors are handled easily in search routines.
2478 Besides, we know we won't find any, so do not bother looking. */
2479 if (fn_name == name && TREE_VEC_ELT (method_vec, 0) == 0)
2480 TREE_VEC_ELT (method_vec, 0) = fn_fields;
2481 else
2482 {
2483 testp = &TREE_VEC_ELT (method_vec, 0);
2484 if (*testp == NULL_TREE)
2485 testp++;
2486 while (((HOST_WIDE_INT) testp
2487 < (HOST_WIDE_INT) obstack_next_free (&class_obstack))
2488 && DECL_NAME (*testp) != fn_name)
2489 testp++;
2490 if ((HOST_WIDE_INT) testp
2491 < (HOST_WIDE_INT) obstack_next_free (&class_obstack))
2492 {
2493 tree x, prev_x;
2494
2495 for (x = *testp; x; x = DECL_CHAIN (x))
2496 {
2497 if (DECL_NAME (fn_fields) == ansi_opname[(int) DELETE_EXPR])
2498 {
2499 /* ANSI C++ June 5 1992 WP 12.5.5.1 */
2500 cp_error_at ("`%D' overloaded", fn_fields);
2501 cp_error_at ("previous declaration as `%D' here", x);
2502 }
2503 if (DECL_ASSEMBLER_NAME (fn_fields)==DECL_ASSEMBLER_NAME (x))
2504 {
2505 /* We complain about multiple destructors on sight,
2506 so we do not repeat the warning here. Friend-friend
2507 ambiguities are warned about outside this loop. */
2508 if (!DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (fn_fields)))
2509 cp_error_at ("ambiguous method `%#D' in structure",
2510 fn_fields);
2511 break;
2512 }
2513 prev_x = x;
2514 }
2515 if (x == 0)
2516 {
2517 if (*testp)
2518 DECL_CHAIN (prev_x) = fn_fields;
2519 else
2520 *testp = fn_fields;
2521 }
2522 }
2523 else
2524 {
2525 obstack_ptr_grow (&class_obstack, fn_fields);
2526 method_vec = (tree)obstack_base (&class_obstack);
2527 }
2528 }
2529 fn_fields = nextp;
2530 }
2531
2532 TREE_VEC_LENGTH (method_vec) = (tree *)obstack_next_free (&class_obstack)
2533 - (&TREE_VEC_ELT (method_vec, 0));
2534 obstack_finish (&class_obstack);
2535 CLASSTYPE_METHOD_VEC (t) = method_vec;
2536
2537 if (nonprivate_method == 0
2538 && CLASSTYPE_FRIEND_CLASSES (t) == NULL_TREE
2539 && DECL_FRIENDLIST (TYPE_NAME (t)) == NULL_TREE)
2540 {
2541 tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
2542 for (i = 0; i < n_baseclasses; i++)
2543 if (TREE_VIA_PUBLIC (TREE_VEC_ELT (binfos, i))
2544 || TREE_VIA_PROTECTED (TREE_VEC_ELT (binfos, i)))
2545 {
2546 nonprivate_method = 1;
2547 break;
2548 }
2549 if (nonprivate_method == 0)
2550 cp_warning ("all member functions in class `%T' are private", t);
2551 }
2552
2553 /* If there are constructors (and destructors), they are at the
2554 front. Place destructors at very front. Also warn if all
2555 constructors and/or destructors are private (in which case this
2556 class is effectively unusable. */
2557 if (TYPE_HAS_DESTRUCTOR (t))
2558 {
2559 tree dtor, prev;
2560
2561 for (dtor = TREE_VEC_ELT (method_vec, 0);
2562 dtor;
2563 prev = dtor, dtor = DECL_CHAIN (dtor))
2564 {
2565 if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (dtor)))
2566 {
2567 if (TREE_PRIVATE (dtor)
2568 && CLASSTYPE_FRIEND_CLASSES (t) == NULL_TREE
2569 && DECL_FRIENDLIST (TYPE_NAME (t)) == NULL_TREE
2570 && warn_ctor_dtor_privacy)
2571 warning ("class `%s' only defines a private destructor and has no friends",
2572 TYPE_NAME_STRING (t));
2573 break;
2574 }
2575 }
2576
2577 /* Wild parse errors can cause this to happen. */
2578 if (dtor == NULL_TREE)
2579 TYPE_HAS_DESTRUCTOR (t) = 0;
2580 else if (dtor != TREE_VEC_ELT (method_vec, 0))
2581 {
2582 DECL_CHAIN (prev) = DECL_CHAIN (dtor);
2583 DECL_CHAIN (dtor) = TREE_VEC_ELT (method_vec, 0);
2584 TREE_VEC_ELT (method_vec, 0) = dtor;
2585 }
2586 }
2587
2588 /* Now for each member function (except for constructors and
2589 destructors), compute where member functions of the same
2590 name reside in base classes. */
2591 if (n_baseclasses != 0
2592 && TREE_VEC_LENGTH (method_vec) > 1)
2593 {
2594 int len = TREE_VEC_LENGTH (method_vec);
2595 tree baselink_vec = make_tree_vec (len);
2596 int any_links = 0;
2597 tree baselink_binfo = build_tree_list (NULL_TREE, TYPE_BINFO (t));
2598
2599 for (i = 1; i < len; i++)
2600 {
2601 TREE_VEC_ELT (baselink_vec, i)
2602 = get_baselinks (baselink_binfo, t, DECL_NAME (TREE_VEC_ELT (method_vec, i)));
2603 if (TREE_VEC_ELT (baselink_vec, i) != 0)
2604 any_links = 1;
2605 }
2606 if (any_links != 0)
2607 CLASSTYPE_BASELINK_VEC (t) = baselink_vec;
2608 else
2609 obstack_free (current_obstack, baselink_vec);
2610 }
2611
2612 /* Now add the methods to the TYPE_METHODS of T, arranged in a chain. */
2613 {
2614 tree x, last_x = NULL_TREE;
2615 int limit = TREE_VEC_LENGTH (method_vec);
2616
2617 for (i = 1; i < limit; i++)
2618 {
2619 for (x = TREE_VEC_ELT (method_vec, i); x; x = DECL_CHAIN (x))
2620 {
2621 if (last_x != NULL_TREE)
2622 TREE_CHAIN (last_x) = x;
2623 last_x = x;
2624 }
2625 }
2626
2627 /* Put ctors and dtors at the front of the list. */
2628 x = TREE_VEC_ELT (method_vec, 0);
2629 if (x)
2630 {
2631 while (DECL_CHAIN (x))
2632 {
2633 /* Let's avoid being circular about this. */
2634 if (x == DECL_CHAIN (x))
2635 break;
2636 TREE_CHAIN (x) = DECL_CHAIN (x);
2637 x = DECL_CHAIN (x);
2638 }
2639 if (TREE_VEC_LENGTH (method_vec) > 1)
2640 TREE_CHAIN (x) = TREE_VEC_ELT (method_vec, 1);
2641 else
2642 TREE_CHAIN (x) = NULL_TREE;
2643 }
2644 }
2645
2646#if 0
2647 TYPE_METHODS (t) = TREE_VEC_ELT (method_vec, 0)
2648 ? TREE_VEC_ELT (method_vec, 0) : TREE_VEC_ELT (method_vec, 1);
2649#else
2650 TYPE_METHODS (t) = method_vec;
2651#endif
2652
2653 return method_vec;
2654}
2655
2656/* Emit error when a duplicate definition of a type is seen. Patch up. */
2657
2658void
2659duplicate_tag_error (t)
2660 tree t;
2661{
2662 cp_error ("redefinition of `%#T'", t);
2663
2664 /* Pretend we haven't defined this type. */
2665
2666 /* All of the component_decl's were TREE_CHAINed together in the parser.
2667 finish_struct_methods walks these chains and assembles all methods with
2668 the same base name into DECL_CHAINs. Now we don't need the parser chains
2669 anymore, so we unravel them.
2670 */
2671 /*
2672 * This used to be in finish_struct, but it turns out that the
2673 * TREE_CHAIN is used by dbxout_type_methods and perhaps some other things...
2674 */
2675 if (CLASSTYPE_METHOD_VEC(t))
2676 {
2677 tree tv = CLASSTYPE_METHOD_VEC(t);
2678 int i, len = TREE_VEC_LENGTH (tv);
2679 for (i = 0; i < len; i++)
2680 {
2681 tree unchain = TREE_VEC_ELT (tv, i);
2682 while (unchain != NULL_TREE)
2683 {
2684 TREE_CHAIN (unchain) = NULL_TREE;
2685 unchain = DECL_CHAIN(unchain);
2686 }
2687 }
2688 }
2689
2690 if (TYPE_LANG_SPECIFIC (t))
2691 {
2692 tree as_list = CLASSTYPE_AS_LIST (t);
2693 tree binfo = TYPE_BINFO (t);
2694 tree binfo_as_list = CLASSTYPE_BINFO_AS_LIST (t);
2695 int interface_only = CLASSTYPE_INTERFACE_ONLY (t);
2696 int interface_unknown = CLASSTYPE_INTERFACE_UNKNOWN (t);
2697
2698 bzero (TYPE_LANG_SPECIFIC (t), sizeof (struct lang_type));
2699 BINFO_BASETYPES(binfo) = NULL_TREE;
2700
2701 CLASSTYPE_AS_LIST (t) = as_list;
2702 TYPE_BINFO (t) = binfo;
2703 CLASSTYPE_BINFO_AS_LIST (t) = binfo_as_list;
2704 CLASSTYPE_INTERFACE_ONLY (t) = interface_only;
2705 SET_CLASSTYPE_INTERFACE_UNKNOWN_X (t, interface_unknown);
2706 CLASSTYPE_VBASE_SIZE (t) = integer_zero_node;
2707 TYPE_REDEFINED (t) = 1;
2708 }
2709 TYPE_SIZE (t) = NULL_TREE;
2710 TYPE_MODE (t) = VOIDmode;
2711 TYPE_FIELDS (t) = NULL_TREE;
2712 TYPE_METHODS (t) = NULL_TREE;
2713 TYPE_VFIELD (t) = NULL_TREE;
2714 TYPE_CONTEXT (t) = NULL_TREE;
2715}
2716
2717/* Create a RECORD_TYPE or UNION_TYPE node for a C struct or union declaration
2718 (or C++ class declaration).
2719
2720 For C++, we must handle the building of derived classes.
2721 Also, C++ allows static class members. The way that this is
2722 handled is to keep the field name where it is (as the DECL_NAME
2723 of the field), and place the overloaded decl in the DECL_FIELD_BITPOS
2724 of the field. layout_record and layout_union will know about this.
2725
2726 More C++ hair: inline functions have text in their
2727 DECL_PENDING_INLINE_INFO nodes which must somehow be parsed into
2728 meaningful tree structure. After the struct has been laid out, set
2729 things up so that this can happen.
2730
2731 And still more: virtual functions. In the case of single inheritance,
2732 when a new virtual function is seen which redefines a virtual function
2733 from the base class, the new virtual function is placed into
2734 the virtual function table at exactly the same address that
2735 it had in the base class. When this is extended to multiple
2736 inheritance, the same thing happens, except that multiple virtual
2737 function tables must be maintained. The first virtual function
2738 table is treated in exactly the same way as in the case of single
2739 inheritance. Additional virtual function tables have different
2740 DELTAs, which tell how to adjust `this' to point to the right thing.
2741
2742 LIST_OF_FIELDLISTS is just that. The elements of the list are
2743 TREE_LIST elements, whose TREE_PURPOSE field tells what access
2744 the list has, and the TREE_VALUE slot gives the actual fields.
2745
2746 If flag_all_virtual == 1, then we lay all functions into
2747 the virtual function table, as though they were declared
2748 virtual. Constructors do not lay down in the virtual function table.
2749
2750 If flag_all_virtual == 2, then we lay all functions into
2751 the virtual function table, such that virtual functions
2752 occupy a space by themselves, and then all functions
2753 of the class occupy a space by themselves. This is illustrated
2754 in the following diagram:
2755
2756 class A; class B : A;
2757
2758 Class A's vtbl: Class B's vtbl:
2759 --------------------------------------------------------------------
2760 | A's virtual functions| | B's virtual functions |
2761 | | | (may inherit some from A). |
2762 --------------------------------------------------------------------
2763 | All of A's functions | | All of A's functions |
2764 | (such as a->A::f). | | (such as b->A::f) |
2765 --------------------------------------------------------------------
2766 | B's new virtual functions |
2767 | (not defined in A.) |
2768 -------------------------------
2769 | All of B's functions |
2770 | (such as b->B::f) |
2771 -------------------------------
2772
2773 this allows the program to make references to any function, virtual
2774 or otherwise in a type-consistent manner. */
2775
2776tree
2777finish_struct (t, list_of_fieldlists, warn_anon)
2778 tree t;
2779 tree list_of_fieldlists;
2780 int warn_anon;
2781{
2782 extern int interface_only, interface_unknown;
2783 extern tree EHS_type;
2784
2785 int old;
2786 int round_up_size = 1;
2787
2788 enum tree_code code = TREE_CODE (t);
2789 register tree x, last_x, method_vec;
2790 int needs_virtual_dtor;
51c184be
MS
2791 tree name = TYPE_NAME (t), fields, fn_fields, *tail;
2792 tree *tail_user_methods = &CLASSTYPE_METHODS (t);
8d08fdba
MS
2793 enum access_type access;
2794 int all_virtual;
2795 int has_virtual;
2796 int max_has_virtual;
2797 tree pending_virtuals = NULL_TREE;
2798 tree abstract_virtuals = NULL_TREE;
2799 tree vfield;
2800 tree vfields;
2801 int cant_have_default_ctor;
2802 int cant_have_const_ctor;
2803 int cant_synth_copy_ctor;
2804 int cant_synth_asn_ref;
2805 int no_const_asn_ref;
2806
2807 /* The index of the first base class which has virtual
2808 functions. Only applied to non-virtual baseclasses. */
2809 int first_vfn_base_index;
2810
2811 int n_baseclasses;
2812 int any_default_members = 0;
2813 int const_sans_init = 0;
2814 int ref_sans_init = 0;
8d08fdba
MS
2815 int nonprivate_method = 0;
2816 tree t_binfo = TYPE_BINFO (t);
2817 tree access_decls = 0;
2818
2819 if (TREE_CODE (name) == TYPE_DECL)
2820 {
2821#if 0 /* Maybe later. -jason */
2822 struct tinst_level *til = tinst_for_decl();
2823
2824 if (til)
2825 {
2826 DECL_SOURCE_FILE (name) = til->file;
2827 if (DECL_SOURCE_LINE (name))
2828 DECL_SOURCE_LINE (name) = til->line;
2829 }
2830 else
2831#endif
2832 {
2833 extern int lineno;
2834
2835 DECL_SOURCE_FILE (name) = input_filename;
2836 /* For TYPE_DECL that are not typedefs (those marked with a line
2837 number of zero, we don't want to mark them as real typedefs.
2838 If this fails one needs to make sure real typedefs have a
2839 previous line number, even if it is wrong, that way the below
2840 will fill in the right line number. (mrs) */
2841 if (DECL_SOURCE_LINE (name))
2842 DECL_SOURCE_LINE (name) = lineno;
2843 }
2844 name = DECL_NAME (name);
2845 }
2846
2847 if (warn_anon && code != UNION_TYPE && ANON_AGGRNAME_P (name))
2848 warning ("anonymous class type not used to declare any objects");
2849
2850#if 0
2851 /* This is set here, but it's never actually used anywhere. (bpk) */
2852 leftmost_baseclasses = NULL_TREE;
2853#endif
2854 if (TYPE_SIZE (t))
2855 {
2856 if (IS_AGGR_TYPE (t))
2857 cp_error ("redefinition of `%#T'", t);
2858 else
2859 my_friendly_abort (172);
2860 popclass (0);
2861 return t;
2862 }
2863
2864 /* Append the fields we need for constructing signature tables. */
2865 if (IS_SIGNATURE (t))
2866 append_signature_fields (list_of_fieldlists);
2867
2868 GNU_xref_decl (current_function_decl, t);
2869
2870 /* If this type was previously laid out as a forward reference,
2871 make sure we lay it out again. */
2872
2873 TYPE_SIZE (t) = 0;
2874 CLASSTYPE_GOT_SEMICOLON (t) = 0;
2875
2876 /* A signature type will contain the fields of the signature table.
2877 Therefore, it's not only an interface. */
2878 if (IS_SIGNATURE (t))
2879 {
2880 CLASSTYPE_INTERFACE_ONLY (t) = 0;
2881 SET_CLASSTYPE_INTERFACE_KNOWN (t);
2882 }
2883 else
2884 {
2885 CLASSTYPE_INTERFACE_ONLY (t) = interface_only;
2886 SET_CLASSTYPE_INTERFACE_UNKNOWN_X (t, interface_unknown);
2887 }
2888
2889 if (flag_dossier)
2890 build_t_desc (t, 0);
2891
2892 TYPE_BINFO (t) = NULL_TREE;
2893
2894 old = suspend_momentary ();
2895
2896 /* Install struct as DECL_FIELD_CONTEXT of each field decl.
2897 Also process specified field sizes.
2898 Set DECL_FIELD_SIZE to the specified size, or 0 if none specified.
2899 The specified size is found in the DECL_INITIAL.
2900 Store 0 there, except for ": 0" fields (so we can find them
2901 and delete them, below). */
2902
2903 if (t_binfo && BINFO_BASETYPES (t_binfo))
2904 n_baseclasses = TREE_VEC_LENGTH (BINFO_BASETYPES (t_binfo));
2905 else
2906 n_baseclasses = 0;
2907
2908 if (n_baseclasses > 0)
2909 {
2910 struct base_info base_info;
2911
2912 /* If using multiple inheritance, this may cause variants of our
2913 basetypes to be used (instead of their canonical forms). */
2914 fields = layout_basetypes (t, BINFO_BASETYPES (t_binfo));
2915 last_x = tree_last (fields);
2916
2917 first_vfn_base_index = finish_base_struct (t, &base_info, t_binfo);
2918 /* Remember where we got our vfield from */
2919 CLASSTYPE_VFIELD_PARENT (t) = first_vfn_base_index;
2920 has_virtual = base_info.has_virtual;
2921 max_has_virtual = base_info.max_has_virtual;
2922 CLASSTYPE_N_SUPERCLASSES (t) += base_info.n_ancestors;
2923 vfield = base_info.vfield;
2924 vfields = base_info.vfields;
2925 cant_have_default_ctor = base_info.cant_have_default_ctor;
2926 cant_have_const_ctor = base_info.cant_have_const_ctor;
2927 cant_synth_copy_ctor = base_info.cant_synth_copy_ctor;
2928 cant_synth_asn_ref = base_info.cant_synth_asn_ref;
2929 no_const_asn_ref = base_info.no_const_asn_ref;
2930 needs_virtual_dtor = base_info.needs_virtual_dtor;
2931 n_baseclasses = TREE_VEC_LENGTH (BINFO_BASETYPES (t_binfo));
2932 }
2933 else
2934 {
2935 first_vfn_base_index = -1;
2936 has_virtual = 0;
2937 max_has_virtual = has_virtual;
2938 vfield = NULL_TREE;
2939 vfields = NULL_TREE;
2940 fields = NULL_TREE;
2941 last_x = NULL_TREE;
2942 cant_have_default_ctor = 0;
2943 cant_have_const_ctor = 0;
2944 cant_synth_copy_ctor = 0;
2945 cant_synth_asn_ref = 0;
2946 no_const_asn_ref = 0;
2947 needs_virtual_dtor = 0;
2948 }
2949
2950 if (write_virtuals == 3 && CLASSTYPE_INTERFACE_KNOWN (t)
2951 && current_lang_name == lang_name_cplusplus && ! IS_SIGNATURE (t))
2952 {
2953 CLASSTYPE_INTERFACE_ONLY (t) = interface_only;
2954 CLASSTYPE_VTABLE_NEEDS_WRITING (t) = ! interface_only;
2955 }
2956 else if (IS_SIGNATURE (t))
2957 CLASSTYPE_VTABLE_NEEDS_WRITING (t) = 0;
2958
2959 /* The three of these are approximations which may later be
2960 modified. Needed at this point to make add_virtual_function
2961 and modify_vtable_entries work. */
2962 TREE_CHAIN (t_binfo) = TYPE_BINFO (t);
2963 TYPE_BINFO (t) = t_binfo;
2964 CLASSTYPE_VFIELDS (t) = vfields;
2965 CLASSTYPE_VFIELD (t) = vfield;
2966
51c184be 2967 tail = &fn_fields;
8d08fdba
MS
2968 if (last_x && list_of_fieldlists)
2969 TREE_CHAIN (last_x) = TREE_VALUE (list_of_fieldlists);
2970
2971 if (IS_SIGNATURE (t))
2972 all_virtual = 0;
2973 else if (flag_all_virtual == 1 && TYPE_OVERLOADS_METHOD_CALL_EXPR (t))
2974 all_virtual = 1;
2975 else
2976 all_virtual = 0;
2977
2978 /* For signatures, we made all methods `public' in the parser and
2979 reported an error if a access specifier was used. */
2980 if (CLASSTYPE_DECLARED_CLASS (t) == 0)
2981 {
2982 nonprivate_method = 1;
2983 if (list_of_fieldlists
2984 && TREE_PURPOSE (list_of_fieldlists) == (tree)access_default)
2985 TREE_PURPOSE (list_of_fieldlists) = (tree)access_public;
2986 }
2987 else if (list_of_fieldlists
2988 && TREE_PURPOSE (list_of_fieldlists) == (tree)access_default)
2989 TREE_PURPOSE (list_of_fieldlists) = (tree)access_private;
2990
2991 while (list_of_fieldlists)
2992 {
2993 access = (enum access_type)TREE_PURPOSE (list_of_fieldlists);
2994
2995 for (x = TREE_VALUE (list_of_fieldlists); x; x = TREE_CHAIN (x))
2996 {
2997 TREE_PRIVATE (x) = access == access_private;
2998 TREE_PROTECTED (x) = access == access_protected;
2999 GNU_xref_member (current_class_name, x);
3000
3001 if (TREE_CODE (x) == TYPE_DECL)
3002 {
3003 /* Make sure we set this up. In find_scoped_type, it explicitly
3004 looks for a TYPE_DECL in the TYPE_FIELDS list. If we don't
3005 do this here, we'll miss including this TYPE_DECL in the
3006 list. */
3007 if (! fields)
3008 fields = x;
3009 last_x = x;
3010 DECL_CONTEXT (x) = t;
3011 continue;
3012 }
3013
3014
3015 if (TREE_CODE (x) == FUNCTION_DECL)
3016 {
3017 nonprivate_method |= ! TREE_PRIVATE (x);
3018
3019 /* If this was an evil function, don't keep it in class. */
3020 if (IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (x)))
3021 continue;
3022
3023 if (last_x)
3024 TREE_CHAIN (last_x) = TREE_CHAIN (x);
51c184be
MS
3025 /* Link x onto end of fn_fields and CLASSTYPE_METHODS. */
3026 *tail = x;
3027 tail = &TREE_CHAIN (x);
3028 *tail_user_methods = x;
3029 tail_user_methods = &DECL_NEXT_METHOD (x);
8d08fdba
MS
3030
3031#if 0
3032 /* ??? What if we have duplicate declarations
3033 in T's definition? */
3034 if (DECL_CLASS_CONTEXT (x))
3035 continue;
3036#endif
3037 DECL_CLASS_CONTEXT (x) = t;
3038
3039 DECL_FIELD_SIZE (x) = 0;
3040
3041 /* The name of the field is the original field name
3042 Save this in auxiliary field for later overloading. */
3043 if (DECL_VINDEX (x)
3044 || (all_virtual == 1 && ! DECL_CONSTRUCTOR_P (x)))
3045 {
8926095f
MS
3046 pending_virtuals = add_virtual_function (pending_virtuals,
3047 &has_virtual, x, t);
3048 if (DECL_ABSTRACT_VIRTUAL_P (x))
3049 abstract_virtuals = tree_cons (NULL_TREE, x, abstract_virtuals);
8d08fdba
MS
3050 }
3051 continue;
3052 }
3053
3054 /* Handle access declarations. */
3055 if (DECL_NAME (x) && TREE_CODE (DECL_NAME (x)) == SCOPE_REF)
3056 {
3057 tree fdecl = TREE_OPERAND (DECL_NAME (x), 1);
3058
3059 if (last_x)
3060 TREE_CHAIN (last_x) = TREE_CHAIN (x);
3061 access_decls = tree_cons ((tree) access, fdecl, access_decls);
3062 continue;
3063 }
3064
3065 /* If we've gotten this far, it's a data member, possibly static,
3066 or an enumerator. */
3067
3068 DECL_FIELD_CONTEXT (x) = t;
3069
3070 /* ``A local class cannot have static data members.'' ARM 9.4 */
3071 if (current_function_decl && TREE_STATIC (x))
3072 cp_error_at ("field `%D' in local class cannot be static", x);
3073
3074 /* Perform error checking that did not get done in
3075 grokdeclarator. */
3076 if (TREE_CODE (TREE_TYPE (x)) == FUNCTION_TYPE)
3077 {
3078 cp_error_at ("field `%D' invalidly declared function type",
3079 x);
3080 TREE_TYPE (x) = build_pointer_type (TREE_TYPE (x));
3081 }
3082 else if (TREE_CODE (TREE_TYPE (x)) == METHOD_TYPE)
3083 {
3084 cp_error_at ("field `%D' invalidly declared method type", x);
3085 TREE_TYPE (x) = build_pointer_type (TREE_TYPE (x));
3086 }
3087 else if (TREE_CODE (TREE_TYPE (x)) == OFFSET_TYPE)
3088 {
3089 cp_error_at ("field `%D' invalidly declared offset type", x);
3090 TREE_TYPE (x) = build_pointer_type (TREE_TYPE (x));
3091 }
3092
3093 if (TREE_TYPE (x) == error_mark_node)
3094 continue;
3095
3096 if (! fields)
3097 fields = x;
3098 last_x = x;
3099
3100 DECL_FIELD_SIZE (x) = 0;
3101
3102 /* When this goes into scope, it will be a non-local reference. */
3103 DECL_NONLOCAL (x) = 1;
3104
3105 if (TREE_CODE (x) == CONST_DECL)
3106 continue;
3107
3108 if (TREE_CODE (x) == VAR_DECL)
3109 {
3110 if (TREE_CODE (t) == UNION_TYPE)
3111 /* Unions cannot have static members. */
3112 cp_error_at ("field `%D' declared static in union", x);
3113
3114 continue;
3115 }
3116
3117 /* Now it can only be a FIELD_DECL. */
3118
3119 /* If this is of reference type, check if it needs an init.
3120 Also do a little ANSI jig if necessary. */
3121 if (TREE_CODE (TREE_TYPE (x)) == REFERENCE_TYPE)
3122 {
3123 if (DECL_INITIAL (x) == NULL_TREE)
3124 ref_sans_init = 1;
3125
3126 /* ARM $12.6.2: [A member initializer list] is the only
3127 way to initialize a nonstatic const and reference
3128 [member]. */
3129 cant_synth_asn_ref = 1;
3130 cant_have_default_ctor = 1;
3131 TYPE_HAS_COMPLEX_INIT_REF (t) = 1;
3132
3133 if (! TYPE_HAS_CONSTRUCTOR (t))
3134 {
3135 if (DECL_NAME (x))
3136 cp_pedwarn_at ("non-static reference `%#D' in class without a constructor", x);
3137 else
3138 cp_pedwarn_at ("non-static reference in class without a constructor", x);
3139 }
3140 }
3141
3142 /* If any field is const, the structure type is pseudo-const. */
3143 if (TREE_READONLY (x))
3144 {
3145 C_TYPE_FIELDS_READONLY (t) = 1;
3146 if (DECL_INITIAL (x) == NULL_TREE)
3147 const_sans_init = 1;
3148
3149 /* ARM $12.6.2: [A member initializer list] is the only
3150 way to initialize a nonstatic const and reference
3151 [member]. */
3152 cant_synth_asn_ref = 1;
3153 cant_have_default_ctor = 1;
3154 TYPE_HAS_COMPLEX_INIT_REF (t) = 1;
3155
3156 if (! TYPE_HAS_CONSTRUCTOR (t) && !IS_SIGNATURE (t))
3157 {
3158 if (DECL_NAME (x))
3159 cp_pedwarn_at ("non-static const member `%#D' in class without a constructor", x);
3160 else
3161 cp_pedwarn_at ("non-static const member in class without a constructor", x);
3162 }
3163 }
3164 else
3165 {
3166 /* A field that is pseudo-const makes the structure
3167 likewise. */
3168 tree t1 = TREE_TYPE (x);
3169 while (TREE_CODE (t1) == ARRAY_TYPE)
3170 t1 = TREE_TYPE (t1);
3171 if (IS_AGGR_TYPE (t1))
3172 {
3173 if (C_TYPE_FIELDS_READONLY (t1))
3174 C_TYPE_FIELDS_READONLY (t) = 1;
3175 if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (t1))
3176 const_sans_init = 1;
3177 }
3178 }
3179
3180 /* We set DECL_BIT_FIELD tentatively in grokbitfield.
3181 If the type and width are valid, we'll keep it set.
3182 Otherwise, the flag is cleared. */
3183 if (DECL_BIT_FIELD (x))
3184 {
3185 DECL_BIT_FIELD (x) = 0;
3186 /* Invalid bit-field size done by grokfield. */
3187 /* Detect invalid bit-field type. */
3188 if (DECL_INITIAL (x)
3189 && TREE_CODE (TREE_TYPE (x)) != INTEGER_TYPE
3190 && TREE_CODE (TREE_TYPE (x)) != ENUMERAL_TYPE)
3191 {
3192 cp_error_at ("bit-field `%D' has invalid type", x);
3193 DECL_INITIAL (x) = NULL;
3194 }
3195
3196 /* Detect and ignore out of range field width. */
3197 if (DECL_INITIAL (x))
3198 {
3199 register int width = TREE_INT_CST_LOW (DECL_INITIAL (x));
3200
3201 if (width < 0)
3202 {
3203 DECL_INITIAL (x) = NULL;
3204 cp_error_at ("negative width in bit-field `%D'", x);
3205 }
3206 else if (width == 0 && DECL_NAME (x) != 0)
3207 {
3208 DECL_INITIAL (x) = NULL;
3209 cp_error_at ("zero width for bit-field `%D'", x);
3210 }
3211 else if ((unsigned)width > TYPE_PRECISION (TREE_TYPE (x)))
3212 {
3213 DECL_INITIAL (x) = NULL;
3214 cp_error_at ("width of `%D' exceeds its type", x);
3215 }
3216 }
3217
3218 /* Process valid field width. */
3219 if (DECL_INITIAL (x))
3220 {
3221 register int width = TREE_INT_CST_LOW (DECL_INITIAL (x));
3222
3223 if (width == 0)
3224 {
3225#ifdef EMPTY_FIELD_BOUNDARY
3226 /* field size 0 => mark following field as "aligned" */
3227 if (TREE_CHAIN (x))
3228 DECL_ALIGN (TREE_CHAIN (x))
3229 = MAX (DECL_ALIGN (TREE_CHAIN (x)), EMPTY_FIELD_BOUNDARY);
3230 /* field of size 0 at the end => round up the size. */
3231 else
3232 round_up_size = EMPTY_FIELD_BOUNDARY;
3233#endif
3234#ifdef PCC_BITFIELD_TYPE_MATTERS
3235 DECL_ALIGN (x) = MAX (DECL_ALIGN (x),
3236 TYPE_ALIGN (TREE_TYPE (x)));
3237#endif
3238 }
3239 else
3240 {
3241 DECL_INITIAL (x) = NULL_TREE;
3242 DECL_FIELD_SIZE (x) = width;
3243 DECL_BIT_FIELD (x) = 1;
3244 /* Traditionally a bit field is unsigned
3245 even if declared signed. */
3246 if (flag_traditional
3247 && TREE_CODE (TREE_TYPE (x)) == INTEGER_TYPE)
3248 TREE_TYPE (x) = unsigned_type_node;
3249 }
3250 }
3251 else
3252 /* Non-bit-fields are aligned for their type. */
3253 DECL_ALIGN (x) = MAX (DECL_ALIGN (x), TYPE_ALIGN (TREE_TYPE (x)));
3254 }
3255 else
3256 {
3257 tree type = TREE_TYPE (x);
3258
3259 if (TREE_CODE (type) == ARRAY_TYPE)
3260 type = TREE_TYPE (type);
3261
3262 if (TYPE_LANG_SPECIFIC (type) && ! ANON_UNION_P (x))
3263 {
3264 /* Never let anything with uninheritable virtuals
3265 make it through without complaint. */
3266 if (CLASSTYPE_ABSTRACT_VIRTUALS (type))
3267 abstract_virtuals_error (x, type);
3268
3269 /* Don't let signatures make it through either. */
3270 if (IS_SIGNATURE (type))
3271 signature_error (x, type);
3272
3273 if (code == UNION_TYPE)
3274 {
3275 char * fie = 0;
3276 if (TYPE_NEEDS_CONSTRUCTING (type))
3277 fie = "constructor";
3278 else if (TYPE_NEEDS_DESTRUCTOR (type))
3279 fie = "destructor";
3280 else if (TYPE_HAS_REAL_ASSIGNMENT (type))
3281 fie = "assignment operator";
3282 if (fie)
3283 cp_error_at ("member `%#D' with %s not allowed in union", x,
3284 fie);
3285 }
3286 else
3287 {
3288 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (type);
3289 TYPE_NEEDS_DESTRUCTOR (t) |= TYPE_NEEDS_DESTRUCTOR (type);
3290 TYPE_HAS_COMPLEX_ASSIGN_REF (t) |= TYPE_HAS_COMPLEX_ASSIGN_REF (type);
3291 TYPE_HAS_COMPLEX_INIT_REF (t)
3292 |= (TYPE_HAS_COMPLEX_INIT_REF (type)
3293 || TYPE_NEEDS_CONSTRUCTING (type));
3294 }
3295
3296 if (! TYPE_HAS_INIT_REF (type)
3297 || (TYPE_HAS_NONPUBLIC_CTOR (type)
3298 && ! is_friend (t, type)))
3299 cant_synth_copy_ctor = 1;
3300 else if (!TYPE_HAS_CONST_INIT_REF (type))
3301 cant_have_const_ctor = 1;
3302
3303 if (! TYPE_HAS_ASSIGN_REF (type)
3304 || (TYPE_HAS_NONPUBLIC_ASSIGN_REF (type)
3305 && ! is_friend (t, type)))
3306 cant_synth_asn_ref = 1;
3307 else if (!TYPE_HAS_CONST_ASSIGN_REF (type))
3308 no_const_asn_ref = 1;
3309
3310 if (TYPE_HAS_CONSTRUCTOR (type)
3311 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
3312 {
3313 cant_have_default_ctor = 1;
3314 if (! TYPE_HAS_CONSTRUCTOR (t))
3315 {
3316 if (DECL_NAME (x))
3317 cp_pedwarn_at ("member `%#D' with only non-default constructor", x);
3318 else
3319 cp_pedwarn_at ("member with only non-default constructor", x);
3320 cp_pedwarn_at ("in class without a constructor",
3321 x);
3322 }
3323 }
3324 }
3325 if (DECL_INITIAL (x) != NULL_TREE)
3326 {
3327 /* `build_class_init_list' does not recognize
3328 non-FIELD_DECLs. */
3329 if (code == UNION_TYPE && any_default_members != 0)
3330 cp_error_at ("multiple fields in union `%T' initialized");
3331 any_default_members = 1;
3332 }
3333 }
3334 }
3335 list_of_fieldlists = TREE_CHAIN (list_of_fieldlists);
3336 /* link the tail while we have it! */
3337 if (last_x)
3338 {
3339 TREE_CHAIN (last_x) = NULL_TREE;
3340
3341 if (list_of_fieldlists
3342 && TREE_VALUE (list_of_fieldlists)
3343 && TREE_CODE (TREE_VALUE (list_of_fieldlists)) != FUNCTION_DECL)
3344 TREE_CHAIN (last_x) = TREE_VALUE (list_of_fieldlists);
3345 }
3346 }
3347
8d08fdba
MS
3348 /* If this type has any constant members which did not come
3349 with their own initialization, mark that fact here. It is
3350 not an error here, since such types can be saved either by their
3351 constructors, or by fortuitous initialization. */
3352 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t) = const_sans_init;
3353 CLASSTYPE_REF_FIELDS_NEED_INIT (t) = ref_sans_init;
3354 CLASSTYPE_ABSTRACT_VIRTUALS (t) = abstract_virtuals;
3355
3356 if (TYPE_NEEDS_DESTRUCTOR (t) && !TYPE_HAS_DESTRUCTOR (t)
3357 && !IS_SIGNATURE (t))
3358 {
3359 /* Here we must cons up a destructor on the fly. */
3360 tree dtor = cons_up_default_function (t, name, fields,
3361 needs_virtual_dtor != 0);
3362
3363 /* If we couldn't make it work, then pretend we didn't need it. */
3364 if (dtor == void_type_node)
3365 TYPE_NEEDS_DESTRUCTOR (t) = 0;
3366 else
3367 {
51c184be
MS
3368 /* Link dtor onto end of fn_fields. */
3369 *tail = dtor;
3370 tail = &TREE_CHAIN (dtor);
8d08fdba
MS
3371
3372 if (DECL_VINDEX (dtor) == NULL_TREE
3373 && ! CLASSTYPE_DECLARED_EXCEPTION (t)
3374 && (needs_virtual_dtor
3375 || pending_virtuals != NULL_TREE
3376 || pending_hard_virtuals != NULL_TREE))
3377 DECL_VINDEX (dtor) = error_mark_node;
3378 if (DECL_VINDEX (dtor))
3379 pending_virtuals = add_virtual_function (pending_virtuals,
3380 &has_virtual, dtor, NULL_TREE);
3381 nonprivate_method = 1;
3382 }
3383 }
3384
51c184be
MS
3385 *tail = NULL_TREE;
3386 *tail_user_methods = NULL_TREE;
3387
8d08fdba
MS
3388 TYPE_NEEDS_DESTRUCTOR (t) |= TYPE_HAS_DESTRUCTOR (t);
3389
3390 /* Synthesize any needed methods. Note that methods will be synthesized
3391 for anonymous unions; grok_x_components undoes that. */
3392
3393 if (! fn_fields)
3394 nonprivate_method = 1;
3395
3396 TYPE_HAS_COMPLEX_INIT_REF (t)
3397 |= (TYPE_HAS_INIT_REF (t) || TYPE_USES_VIRTUAL_BASECLASSES (t)
3398 || has_virtual || any_default_members || first_vfn_base_index >= 0);
3399 TYPE_NEEDS_CONSTRUCTING (t)
3400 |= (TYPE_HAS_CONSTRUCTOR (t) || TYPE_USES_VIRTUAL_BASECLASSES (t)
3401 || has_virtual || any_default_members || first_vfn_base_index >= 0);
3402
3403 /* ARM $12.1: A default constructor will be generated for a class X
3404 only if no constructor has been declared for class X. So we
3405 check TYPE_HAS_CONSTRUCTOR also, to make sure we don't generate
3406 one if they declared a constructor in this class. */
3407 if (! TYPE_HAS_CONSTRUCTOR (t) && ! cant_have_default_ctor)
3408 {
3409 tree default_fn = cons_up_default_function (t, name, fields, 2);
3410 TREE_CHAIN (default_fn) = fn_fields;
3411 fn_fields = default_fn;
3412 }
3413
3414 /* Create default copy constructor, if needed. Don't do it for
3415 the exception handler. */
3416 if (! TYPE_HAS_INIT_REF (t) && ! cant_synth_copy_ctor && t != EHS_type)
3417 {
3418 /* ARM 12.18: You get either X(X&) or X(const X&), but
3419 not both. --Chip */
3420 tree default_fn =
3421 cons_up_default_function (t, name, fields,
3422 cant_have_const_ctor ? 4 : 3);
3423 TREE_CHAIN (default_fn) = fn_fields;
3424 fn_fields = default_fn;
3425 }
3426
3427 TYPE_HAS_REAL_ASSIGNMENT (t) |= TYPE_HAS_ASSIGNMENT (t);
3428 TYPE_HAS_REAL_ASSIGN_REF (t) |= TYPE_HAS_ASSIGN_REF (t);
3429 TYPE_HAS_COMPLEX_ASSIGN_REF (t)
3430 |= (TYPE_HAS_ASSIGN_REF (t) || TYPE_USES_VIRTUAL_BASECLASSES (t)
3431 || has_virtual || first_vfn_base_index >= 0);
3432
3433 if (! TYPE_HAS_ASSIGN_REF (t) && ! cant_synth_asn_ref)
3434 {
3435 tree default_fn =
3436 cons_up_default_function (t, name, fields,
3437 no_const_asn_ref ? 6 : 5);
3438 TREE_CHAIN (default_fn) = fn_fields;
3439 fn_fields = default_fn;
3440 }
3441
3442 if (fn_fields)
3443 {
3444 method_vec = finish_struct_methods (t, fn_fields, nonprivate_method);
3445
3446 if (TYPE_HAS_CONSTRUCTOR (t)
3447 && ! CLASSTYPE_DECLARED_EXCEPTION (t)
3448 && CLASSTYPE_FRIEND_CLASSES (t) == NULL_TREE
3449 && DECL_FRIENDLIST (TYPE_NAME (t)) == NULL_TREE)
3450 {
3451 int nonprivate_ctor = 0;
3452 tree ctor;
3453
3454 for (ctor = TREE_VEC_ELT (method_vec, 0);
3455 ctor;
3456 ctor = DECL_CHAIN (ctor))
3457 if (! TREE_PRIVATE (ctor))
3458 {
3459 nonprivate_ctor = 1;
3460 break;
3461 }
3462
3463 if (nonprivate_ctor == 0 && warn_ctor_dtor_privacy)
3464 cp_warning ("`%#T' only defines private constructors and has no friends",
3465 t);
3466 }
3467 }
3468 else
3469 {
3470 method_vec = 0;
3471
3472 /* Just in case these got accidentally
3473 filled in by syntax errors. */
3474 TYPE_HAS_CONSTRUCTOR (t) = 0;
3475 TYPE_HAS_DESTRUCTOR (t) = 0;
3476 }
3477
3478 {
3479 int n_methods = TREE_VEC_LENGTH (method_vec);
3480
3481 for (access_decls = nreverse (access_decls); access_decls;
3482 access_decls = TREE_CHAIN (access_decls))
3483 {
3484 tree fdecl = TREE_VALUE (access_decls);
3485 tree flist = NULL_TREE;
3486 tree name;
3487 enum access_type access = (enum access_type)TREE_PURPOSE(access_decls);
3488 int i = 0;
3489 tree tmp;
3490
3491 if (TREE_CODE (fdecl) == TREE_LIST)
3492 {
3493 flist = fdecl;
3494 fdecl = TREE_VALUE (flist);
3495 }
3496
3497 name = DECL_NAME (fdecl);
3498
3499 for (; i < n_methods; i++)
3500 if (DECL_NAME (TREE_VEC_ELT (method_vec, i)) == name)
3501 {
3502 cp_error ("cannot adjust access to `%#D' in `%#T'", fdecl, t);
3503 cp_error_at ("because of local method `%#D' with same name",
3504 TREE_VEC_ELT (method_vec, i));
3505 fdecl = 0;
3506 break;
3507 }
3508
3509 if (! fdecl)
3510 continue;
3511
3512 for (tmp = fields; tmp; tmp = TREE_CHAIN (tmp))
3513 if (DECL_NAME (tmp) == name)
3514 {
3515 cp_error ("cannot adjust access to `%#D' in `%#T'", fdecl, t);
3516 cp_error_at ("because of local field `%#D' with same name", tmp);
3517 fdecl = 0;
3518 break;
3519 }
3520
3521 if (!fdecl)
3522 continue;
3523
3524 /* Make type T see field decl FDECL with access ACCESS.*/
3525 if (flist)
3526 {
3527 fdecl = TREE_VALUE (flist);
3528 while (fdecl)
3529 {
3530 if (alter_access (t, fdecl, access) == 0)
3531 break;
3532 fdecl = DECL_CHAIN (fdecl);
3533 }
3534 }
3535 else
3536 alter_access (t, fdecl, access);
3537 }
3538
3539 }
3540
3541 if (vfield == NULL_TREE && has_virtual)
3542 {
3543 /* We build this decl with ptr_type_node, and
3544 change the type when we know what it should be. */
3545 vfield = build_lang_field_decl (FIELD_DECL, get_vfield_name (t),
3546 ptr_type_node);
3547 /* If you change any of the below, take a look at all the
3548 other VFIELD_BASEs and VTABLE_BASEs in the code, and change
3549 them too. */
3550 DECL_ASSEMBLER_NAME (vfield) = get_identifier (VFIELD_BASE);
3551 CLASSTYPE_VFIELD (t) = vfield;
3552 DECL_VIRTUAL_P (vfield) = 1;
3553 DECL_FIELD_CONTEXT (vfield) = t;
3554 DECL_CLASS_CONTEXT (vfield) = t;
3555 DECL_FCONTEXT (vfield) = t;
3556 DECL_FIELD_SIZE (vfield) = 0;
3557 DECL_ALIGN (vfield) = TYPE_ALIGN (ptr_type_node);
3558 if (CLASSTYPE_DOSSIER (t))
3559 {
3560 /* vfield is always first entry in structure. */
3561 TREE_CHAIN (vfield) = fields;
3562 fields = vfield;
3563 }
3564 else if (last_x)
3565 {
3566 my_friendly_assert (TREE_CHAIN (last_x) == 0, 175);
3567 TREE_CHAIN (last_x) = vfield;
3568 last_x = vfield;
3569 }
3570 else
3571 fields = vfield;
3572 vfields = chainon (vfields, CLASSTYPE_AS_LIST (t));
3573 }
3574
3575 /* Now DECL_INITIAL is null on all members except for zero-width bit-fields.
3576 And they have already done their work.
3577
3578 C++: maybe we will support default field initialization some day... */
3579
3580 /* Delete all zero-width bit-fields from the front of the fieldlist */
3581 while (fields && DECL_BIT_FIELD (fields)
3582 && DECL_INITIAL (fields))
3583 fields = TREE_CHAIN (fields);
3584 /* Delete all such fields from the rest of the fields. */
3585 for (x = fields; x;)
3586 {
3587 if (TREE_CHAIN (x) && DECL_BIT_FIELD (TREE_CHAIN (x))
3588 && DECL_INITIAL (TREE_CHAIN (x)))
3589 TREE_CHAIN (x) = TREE_CHAIN (TREE_CHAIN (x));
3590 else
3591 x = TREE_CHAIN (x);
3592 }
3593 /* Delete all duplicate fields from the fields */
3594 delete_duplicate_fields (fields);
3595
3596 /* Now we have the final fieldlist for the data fields. Record it,
3597 then lay out the structure or union (including the fields). */
3598
3599 TYPE_FIELDS (t) = fields;
3600
3601 /* If there's a :0 field at the end, round the size to the
3602 EMPTY_FIELD_BOUNDARY. */
3603 TYPE_ALIGN (t) = round_up_size;
3604
3605 /* Pass layout information about base classes to layout_type, if any. */
3606
3607 {
3608 tree field;
3609 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
3610 {
3611 if (TREE_STATIC (field))
3612 continue;
3613 if (TREE_CODE (field) != FIELD_DECL)
3614 continue;
3615
3616 /* If this field is an anonymous union,
3617 give each union-member the same position as the union has.
3618
3619 ??? This is a real kludge because it makes the structure
3620 of the types look strange. This feature is only used by
3621 C++, which should have build_component_ref build two
3622 COMPONENT_REF operations, one for the union and one for
3623 the inner field. We set the offset of this field to zero
3624 so that either the old or the correct method will work.
3625 Setting DECL_FIELD_CONTEXT is wrong unless the inner fields are
3626 moved into the type of this field, but nothing seems to break
3627 by doing this. */
3628
3629 if (DECL_NAME (field) == 0
3630 && TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
3631 {
3632 tree uelt = TYPE_FIELDS (TREE_TYPE (field));
3633 for (; uelt; uelt = TREE_CHAIN (uelt))
3634 {
3635 DECL_FIELD_CONTEXT (uelt) = DECL_FIELD_CONTEXT (field);
3636 DECL_FIELD_BITPOS (uelt) = DECL_FIELD_BITPOS (field);
3637 }
3638
3639 DECL_FIELD_BITPOS (field) = integer_zero_node;
3640 }
3641 }
3642 }
3643
3644 if (n_baseclasses)
3645 {
3646 tree pseudo_basetype = TREE_TYPE (base_layout_decl);
3647
3648 TREE_CHAIN (base_layout_decl) = TYPE_FIELDS (t);
3649 TYPE_FIELDS (t) = base_layout_decl;
3650
3651 TYPE_SIZE (pseudo_basetype) = CLASSTYPE_SIZE (t);
3652 TYPE_MODE (pseudo_basetype) = TYPE_MODE (t);
3653 TYPE_ALIGN (pseudo_basetype) = CLASSTYPE_ALIGN (t);
3654 DECL_ALIGN (base_layout_decl) = TYPE_ALIGN (pseudo_basetype);
3655 /* Don't re-use old size. */
3656 DECL_SIZE (base_layout_decl) = 0;
3657 }
3658
3659 layout_type (t);
3660
3661 {
3662 tree field;
3663 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
3664 {
3665 if (TREE_STATIC (field))
3666 continue;
3667 if (TREE_CODE (field) != FIELD_DECL)
3668 continue;
3669
3670 /* If this field is an anonymous union,
3671 give each union-member the same position as the union has.
3672
3673 ??? This is a real kludge because it makes the structure
3674 of the types look strange. This feature is only used by
3675 C++, which should have build_component_ref build two
3676 COMPONENT_REF operations, one for the union and one for
3677 the inner field. We set the offset of this field to zero
3678 so that either the old or the correct method will work.
3679 Setting DECL_FIELD_CONTEXT is wrong unless the inner fields are
3680 moved into the type of this field, but nothing seems to break
3681 by doing this. */
3682
3683 if (DECL_NAME (field) == 0
3684 && TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
3685 {
3686 tree uelt = TYPE_FIELDS (TREE_TYPE (field));
3687 for (; uelt; uelt = TREE_CHAIN (uelt))
3688 {
3689 DECL_FIELD_CONTEXT (uelt) = DECL_FIELD_CONTEXT (field);
3690 DECL_FIELD_BITPOS (uelt) = DECL_FIELD_BITPOS (field);
3691 }
3692
3693 DECL_FIELD_BITPOS (field) = integer_zero_node;
3694 }
3695 }
3696 }
3697
3698 if (n_baseclasses)
3699 TYPE_FIELDS (t) = TREE_CHAIN (TYPE_FIELDS (t));
3700
3701 /* C++: do not let empty structures exist. */
3702 if (integer_zerop (TYPE_SIZE (t)))
3703 TYPE_SIZE (t) = TYPE_SIZE (char_type_node);
3704
3705 /* Set the TYPE_DECL for this type to contain the right
3706 value for DECL_OFFSET, so that we can use it as part
3707 of a COMPONENT_REF for multiple inheritance. */
3708
3709 if (TREE_CODE (TYPE_NAME (t)) == TYPE_DECL)
3710 layout_decl (TYPE_NAME (t), 0);
3711
3712 /* Now fix up any virtual base class types that we
3713 left lying around. We must get these done
3714 before we try to lay out the virtual function table. */
3715 doing_hard_virtuals = 1;
3716 pending_hard_virtuals = nreverse (pending_hard_virtuals);
3717
3718 if (TYPE_USES_VIRTUAL_BASECLASSES (t))
3719 {
3720 tree vbases;
3721
3722 max_has_virtual = layout_vbasetypes (t, max_has_virtual);
3723 vbases = CLASSTYPE_VBASECLASSES (t);
3724 CLASSTYPE_N_VBASECLASSES (t) = list_length (vbases);
3725
3726 while (vbases)
3727 {
3728 /* Update dossier info with offsets for virtual baseclasses. */
3729 if (flag_dossier && ! BINFO_NEW_VTABLE_MARKED (vbases))
3730 prepare_fresh_vtable (vbases, vbases, t);
3731
3732 vbases = TREE_CHAIN (vbases);
3733 }
3734 }
3735
3736#ifdef NOTQUITE
3737 cp_warning ("Doing hard virtuals for %T...", t);
3738#endif
3739 while (pending_hard_virtuals)
3740 {
3741 /* Need an entry in some other virtual function table. */
3742 if (TREE_TYPE (pending_hard_virtuals))
3743 {
3744 /* This is how we modify entries when a vfn's index changes
3745 between derived and base type. */
3746 modify_vtable_entries (t, TREE_PURPOSE (pending_hard_virtuals),
3747 TREE_TYPE (pending_hard_virtuals),
3748 TREE_VALUE (pending_hard_virtuals));
3749 }
3750 else
3751 {
3752 /* This is how we modify entries when a vfn comes from
3753 a virtual baseclass. */
3754 tree base_fndecls = DECL_VINDEX (TREE_PURPOSE (pending_hard_virtuals));
3755 /* Only do this, if it was missed before. */
3756 if (TREE_CODE (base_fndecls) != INTEGER_CST)
3757 {
3758 my_friendly_assert (base_fndecls != error_mark_node, 176);
3759 while (base_fndecls)
3760 {
3761 modify_vtable_entries (t, TREE_PURPOSE (pending_hard_virtuals),
3762 TREE_VALUE (base_fndecls),
3763 TREE_VALUE (pending_hard_virtuals));
3764 modify_other_vtable_entries (t, TYPE_BINFO (t),
3765 TREE_PURPOSE (pending_hard_virtuals),
3766 TREE_VALUE (base_fndecls),
3767 TREE_VALUE (pending_hard_virtuals));
3768 base_fndecls = TREE_CHAIN (base_fndecls);
3769 }
3770 } else {
3771#ifdef NOTQUITE
3772 cp_warning ("missed bases for `%D'", TREE_PURPOSE (pending_hard_virtuals));
3773#endif
3774 }
3775 }
3776 pending_hard_virtuals = TREE_CHAIN (pending_hard_virtuals);
3777 }
3778
3779 if (TYPE_USES_VIRTUAL_BASECLASSES (t))
3780 {
3781 tree vbases;
3782
3783 vbases = CLASSTYPE_VBASECLASSES (t);
3784 CLASSTYPE_N_VBASECLASSES (t) = list_length (vbases);
3785
3786 /* This loop makes all the entries in the virtual function tables
3787 of interest contain the "latest" version of the functions
3788 we have defined. */
3789
3790 while (vbases)
3791 {
3792 tree virtuals = BINFO_VIRTUALS (vbases);
3793
3794 if (virtuals)
3795 {
3796 /* Get past the `null' vtable entry... */
3797 virtuals = TREE_CHAIN (virtuals);
3798 /* and the `dossier' vtable entry if we're doing dossiers. */
3799 if (flag_dossier)
3800 virtuals = TREE_CHAIN (virtuals);
3801 }
3802
3803 while (virtuals != NULL_TREE)
3804 {
3805 tree pfn = FNADDR_FROM_VTABLE_ENTRY (TREE_VALUE (virtuals));
3806 tree base_fndecl = TREE_OPERAND (pfn, 0);
3807 tree decl = get_first_matching_virtual (TYPE_BINFO (t), base_fndecl,
3808 DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (base_fndecl)));
3809 tree context = DECL_CLASS_CONTEXT (decl);
3810 if (! SAME_FN (decl, base_fndecl))
3811 {
3812 tree base_context = DECL_CLASS_CONTEXT (base_fndecl);
8926095f 3813 tree binfo = NULL_TREE;
8d08fdba 3814#if 0
8926095f 3815 tree these_virtuals;
8d08fdba
MS
3816 unsigned HOST_WIDE_INT i
3817 = (TREE_INT_CST_LOW (DECL_VINDEX (base_fndecl))
3818 & (((unsigned HOST_WIDE_INT)1<<(BITS_PER_WORD-1))-1));
3819#endif
3820
3821 if (TYPE_USES_VIRTUAL_BASECLASSES (context))
3822 binfo = virtual_member (base_context,
3823 CLASSTYPE_VBASECLASSES (context));
3824 if (binfo == NULL_TREE)
3825 binfo = binfo_value (base_context, context);
3826 if (binfo != NULL_TREE)
3827 {
3828#if 1
3829 pfn = FNADDR_FROM_VTABLE_ENTRY (TREE_VALUE (get_vtable_entry (BINFO_VIRTUALS (binfo), base_fndecl)));
3830#else
3831 these_virtuals = BINFO_VIRTUALS (binfo);
3832
3833 while (i-- > 0)
3834 these_virtuals = TREE_CHAIN (these_virtuals);
3835 pfn = FNADDR_FROM_VTABLE_ENTRY (TREE_VALUE (these_virtuals));
3836#endif
3837 pfn = build1 (ADDR_EXPR, ptr_type_node, decl);
3838 TREE_CONSTANT (pfn) = 1;
3839 modify_vtable_entries (t, decl, base_fndecl, pfn);
3840 }
3841 }
3842 virtuals = TREE_CHAIN (virtuals);
3843 }
3844
3845 vbases = TREE_CHAIN (vbases);
3846 }
3847 }
3848 doing_hard_virtuals = 0;
3849
3850 /* Under our model of GC, every C++ class gets its own virtual
3851 function table, at least virtually. */
3852 if (pending_virtuals || CLASSTYPE_DOSSIER (t))
3853 {
3854 pending_virtuals = nreverse (pending_virtuals);
3855 /* We must enter these virtuals into the table. */
3856 if (first_vfn_base_index < 0)
3857 {
3858 if (flag_dossier)
3859 pending_virtuals = tree_cons (NULL_TREE,
3860 build_vtable_entry (integer_zero_node,
3861 build_t_desc (t, 0)),
3862 pending_virtuals);
3863 pending_virtuals = tree_cons (NULL_TREE, the_null_vtable_entry,
3864 pending_virtuals);
3865 build_vtable (NULL_TREE, t);
3866 }
3867 else
3868 {
3869 /* Here we know enough to change the type of our virtual
3870 function table, but we will wait until later this function. */
3871
3872 if (! BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (t)))
3873 build_vtable (TREE_VEC_ELT (TYPE_BINFO_BASETYPES (t), first_vfn_base_index), t);
3874
3875 /* Update the dossier pointer for this class. */
3876 if (flag_dossier)
3877 TREE_VALUE (TREE_CHAIN (TYPE_BINFO_VIRTUALS (t)))
3878 = build_vtable_entry (integer_zero_node, build_t_desc (t, 0));
3879 }
3880
3881 /* If this type has basetypes with constructors, then those
3882 constructors might clobber the virtual function table. But
3883 they don't if the derived class shares the exact vtable of the base
3884 class. */
3885
3886 CLASSTYPE_NEEDS_VIRTUAL_REINIT (t) = 1;
3887 }
3888 else if (first_vfn_base_index >= 0)
3889 {
3890 tree binfo = TREE_VEC_ELT (TYPE_BINFO_BASETYPES (t), first_vfn_base_index);
3891#if 0
3892 /* For testing. */
3893 tree binfo1 = get_binfo (DECL_FIELD_CONTEXT (vfield), t, 0);
3894 if (binfo != binfo1)
3895 warning ("binfos are different in vtable creation");
3896#endif
3897
3898 /* This class contributes nothing new to the virtual function
3899 table. However, it may have declared functions which
3900 went into the virtual function table "inherited" from the
3901 base class. If so, we grab a copy of those updated functions,
3902 and pretend they are ours. */
3903
3904 /* See if we should steal the virtual info from base class. */
3905 if (TYPE_BINFO_VTABLE (t) == NULL_TREE)
3906 TYPE_BINFO_VTABLE (t) = BINFO_VTABLE (binfo);
3907 if (TYPE_BINFO_VIRTUALS (t) == NULL_TREE)
3908 TYPE_BINFO_VIRTUALS (t) = BINFO_VIRTUALS (binfo);
3909 if (TYPE_BINFO_VTABLE (t) != BINFO_VTABLE (binfo))
3910 CLASSTYPE_NEEDS_VIRTUAL_REINIT (t) = 1;
3911 }
3912
3913 if (has_virtual > max_has_virtual)
3914 max_has_virtual = has_virtual;
3915 if (max_has_virtual || first_vfn_base_index >= 0)
3916 {
3917#ifdef VTABLE_USES_MASK
3918 if (max_has_virtual >= VINDEX_MAX)
3919 {
3920 cp_error ("too many virtual functions for `%#T' (VINDEX_MAX < %d)",
3921 t, has_virtual);
3922 }
3923#endif
3924 TYPE_VIRTUAL_P (t) = 1;
3925 CLASSTYPE_VSIZE (t) = has_virtual;
3926 if (first_vfn_base_index >= 0)
3927 {
3928 if (pending_virtuals)
3929 TYPE_BINFO_VIRTUALS (t) = chainon (TYPE_BINFO_VIRTUALS (t),
3930 pending_virtuals);
3931 }
3932 else if (has_virtual)
3933 {
3934 TYPE_BINFO_VIRTUALS (t) = pending_virtuals;
3935 if (write_virtuals >= 0)
3936 DECL_VIRTUAL_P (TYPE_BINFO_VTABLE (t)) = 1;
3937 }
3938 }
3939
3940 /* Now lay out the virtual function table. */
3941 if (has_virtual)
3942 {
3943 tree atype, itype;
3944
3945 if (TREE_TYPE (vfield) == ptr_type_node)
3946 {
3947 /* We must create a pointer to this table because
3948 the one inherited from base class does not exist.
3949 We will fill in the type when we know what it
3950 should really be. Use `size_int' so values are memoized
3951 in common cases. */
3952 itype = build_index_type (size_int (has_virtual));
3953 atype = build_array_type (vtable_entry_type, itype);
3954 layout_type (atype);
3955 TREE_TYPE (vfield) = build_pointer_type (atype);
3956 }
3957 else
3958 {
3959 atype = TREE_TYPE (TREE_TYPE (vfield));
3960
3961 if (has_virtual != TREE_INT_CST_LOW (TYPE_MAX_VALUE (TYPE_DOMAIN (atype))))
3962 {
3963 /* We must extend (or create) the boundaries on this array,
3964 because we picked up virtual functions from multiple
3965 base classes. */
3966 itype = build_index_type (size_int (has_virtual));
3967 atype = build_array_type (vtable_entry_type, itype);
3968 layout_type (atype);
3969 vfield = copy_node (vfield);
3970 TREE_TYPE (vfield) = build_pointer_type (atype);
3971 }
3972 }
3973
3974 CLASSTYPE_VFIELD (t) = vfield;
3975 if (TREE_TYPE (TYPE_BINFO_VTABLE (t)) != atype)
3976 {
3977 TREE_TYPE (TYPE_BINFO_VTABLE (t)) = atype;
3978 layout_decl (TYPE_BINFO_VTABLE (t), 0);
3979 /* At one time the vtable info was grabbed 2 words at a time. This
3980 fails on sparc unless you have 8-byte alignment. (tiemann) */
3981 DECL_ALIGN (TYPE_BINFO_VTABLE (t))
3982 = MAX (TYPE_ALIGN (double_type_node),
3983 DECL_ALIGN (TYPE_BINFO_VTABLE (t)));
3984 }
3985 }
3986 else if (first_vfn_base_index >= 0)
3987 CLASSTYPE_VFIELD (t) = vfield;
3988 CLASSTYPE_VFIELDS (t) = vfields;
3989
3990 finish_struct_bits (t, max_has_virtual);
3991
3992 /* Promote each bit-field's type to int if it is narrower than that.
3993 There's more: complete the rtl for any static member objects which
3994 is of the same type we're working on. */
3995 for (x = fields; x; x = TREE_CHAIN (x))
3996 {
3997 if (DECL_BIT_FIELD (x)
3998 && (C_PROMOTING_INTEGER_TYPE_P (TREE_TYPE (x))
3999 || DECL_FIELD_SIZE (x) < TYPE_PRECISION (integer_type_node)))
4000 {
4001 tree type = TREE_TYPE (x);
4002
4003 /* Preserve unsignedness if traditional or if not really getting
4004 any wider. */
4005 if (TREE_UNSIGNED (type)
4006 && (flag_traditional
4007 ||
4008 (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)
4009 && DECL_FIELD_SIZE (x) == TYPE_PRECISION (integer_type_node))))
4010 TREE_TYPE (x) = unsigned_type_node;
4011 else
4012 TREE_TYPE (x) = integer_type_node;
4013 }
4014
4015 if (TREE_CODE (x) == VAR_DECL && TREE_STATIC (x)
4016 && TREE_TYPE (x) == t)
4017 {
4018 DECL_MODE (x) = TYPE_MODE (t);
4019 make_decl_rtl (x, NULL, 0);
4020 }
4021 }
4022
4023 /* Now add the tags, if any, to the list of TYPE_DECLs
4024 defined for this type. */
4025 if (CLASSTYPE_TAGS (t))
4026 {
4027 x = CLASSTYPE_TAGS (t);
4028 last_x = tree_last (TYPE_FIELDS (t));
4029 while (x)
4030 {
4031 tree tag = build_lang_decl (TYPE_DECL, TREE_PURPOSE (x), TREE_VALUE (x));
4032#ifdef DWARF_DEBUGGING_INFO
4033 if (write_symbols == DWARF_DEBUG)
4034 {
4035 /* Notify dwarfout.c that this TYPE_DECL node represent a
4036 gratuitous typedef. */
4037 DECL_IGNORED_P (tag) = 1;
4038 }
4039#endif /* DWARF_DEBUGGING_INFO */
4040 DECL_CONTEXT (tag) = t;
4041 DECL_CLASS_CONTEXT (tag) = t;
4042 x = TREE_CHAIN (x);
4043 last_x = chainon (last_x, tag);
4044 }
4045 if (TYPE_FIELDS (t) == 0)
4046 TYPE_FIELDS (t) = last_x;
4047 CLASSTYPE_LOCAL_TYPEDECLS (t) = 1;
4048 }
4049
4050 if (TYPE_HAS_CONSTRUCTOR (t))
4051 {
4052 tree vfields = CLASSTYPE_VFIELDS (t);
4053
4054 while (vfields)
4055 {
4056 /* Mark the fact that constructor for T
4057 could affect anybody inheriting from T
4058 who wants to initialize vtables for VFIELDS's type. */
4059 if (VF_DERIVED_VALUE (vfields))
4060 TREE_ADDRESSABLE (vfields) = 1;
4061 vfields = TREE_CHAIN (vfields);
4062 }
4063 if (any_default_members != 0)
4064 build_class_init_list (t);
4065 }
4066 else if (TYPE_NEEDS_CONSTRUCTING (t))
4067 build_class_init_list (t);
4068
4069 if (current_lang_name == lang_name_cplusplus)
4070 {
4071 if (! CLASSTYPE_DECLARED_EXCEPTION (t)
4072 && ! IS_SIGNATURE (t))
4073 embrace_waiting_friends (t);
4074
4075 /* Write out inline function definitions. */
4076 do_inline_function_hair (t, CLASSTYPE_INLINE_FRIENDS (t));
4077 CLASSTYPE_INLINE_FRIENDS (t) = 0;
4078 }
4079
4080 if (CLASSTYPE_VSIZE (t) != 0)
4081 {
4082 if ((flag_this_is_variable & 1) == 0)
4083 {
4084 tree vtbl_ptr = build_decl (VAR_DECL, get_identifier (VPTR_NAME),
4085 TREE_TYPE (vfield));
4086 DECL_REGISTER (vtbl_ptr) = 1;
4087 CLASSTYPE_VTBL_PTR (t) = vtbl_ptr;
4088 }
4089 if (DECL_FIELD_CONTEXT (vfield) != t)
4090 {
4091 tree binfo = get_binfo (DECL_FIELD_CONTEXT (vfield), t, 0);
4092 tree offset = BINFO_OFFSET (binfo);
4093
4094 vfield = copy_node (vfield);
4095 copy_lang_decl (vfield);
4096
4097 if (! integer_zerop (offset))
4098 offset = size_binop (MULT_EXPR, offset, size_int (BITS_PER_UNIT));
4099 DECL_FIELD_CONTEXT (vfield) = t;
4100 DECL_CLASS_CONTEXT (vfield) = t;
4101 DECL_FIELD_BITPOS (vfield)
4102 = size_binop (PLUS_EXPR, offset, DECL_FIELD_BITPOS (vfield));
4103 CLASSTYPE_VFIELD (t) = vfield;
4104 }
4105
4106 /* In addition to this one, all the other vfields should be listed. */
4107 /* Before that can be done, we have to have FIELD_DECLs for them, and
4108 a place to find them. */
4109 TYPE_NONCOPIED_PARTS (t) = build_tree_list (default_conversion (TYPE_BINFO_VTABLE (t)), vfield);
4110
4111 if (warn_nonvdtor && TYPE_HAS_DESTRUCTOR (t)
4112 && DECL_VINDEX (TREE_VEC_ELT (method_vec, 0)) == NULL_TREE)
4113 cp_warning ("`%#T' has virtual functions but non-virtual destructor",
4114 t);
4115 }
4116
4117 /* Make the rtl for any new vtables we have created, and unmark
4118 the base types we marked. */
4119 unmark_finished_struct (t);
4120 TYPE_BEING_DEFINED (t) = 0;
4121
4122 if (flag_dossier && CLASSTYPE_VTABLE_NEEDS_WRITING (t))
4123 {
4124 tree variants;
4125 tree tdecl;
4126
4127 /* Now instantiate its type descriptors. */
4128 tdecl = TREE_OPERAND (build_t_desc (t, 1), 0);
4129 variants = TYPE_POINTER_TO (t);
4130 build_type_variant (variants, 1, 0);
4131 while (variants)
4132 {
4133 build_t_desc (variants, 1);
4134 variants = TYPE_NEXT_VARIANT (variants);
4135 }
4136 variants = build_reference_type (t);
4137 build_type_variant (variants, 1, 0);
4138 while (variants)
4139 {
4140 build_t_desc (variants, 1);
4141 variants = TYPE_NEXT_VARIANT (variants);
4142 }
4143#if 0
4144 DECL_VPARENT (tdecl) = t;
4145#endif
4146 DECL_CONTEXT (tdecl) = t;
4147 }
4148 /* Still need to instantiate this C struct's type descriptor. */
4149 else if (flag_dossier && ! CLASSTYPE_DOSSIER (t))
4150 build_t_desc (t, 1);
4151
4152 if (TYPE_NAME (t) && TYPE_IDENTIFIER (t))
4153 undo_template_name_overload (TYPE_IDENTIFIER (t), 1);
4154 if (current_class_type)
4155 popclass (0);
4156 else
4157 error ("trying to finish struct, but kicked out due to previous parse errors.");
4158
4159 hack_incomplete_structures (t);
4160
4161 resume_momentary (old);
4162
4163 if (flag_cadillac)
4164 cadillac_finish_struct (t);
4165
4166#if 0
4167 /* This has to be done after we have sorted out what to do with
4168 the enclosing type. */
4169 if (write_symbols != DWARF_DEBUG)
4170 {
4171 /* Be smarter about nested classes here. If a type is nested,
4172 only output it if we would output the enclosing type. */
4173 if (DECL_CONTEXT (TYPE_NAME (t))
4174 && TREE_CODE_CLASS (TREE_CODE (DECL_CONTEXT (TYPE_NAME (t)))) == 't')
4175 DECL_IGNORED_P (TYPE_NAME (t)) = TREE_ASM_WRITTEN (TYPE_NAME (t));
4176 }
4177#endif
4178
4179 if (write_symbols != DWARF_DEBUG)
4180 {
4181 /* If the type has methods, we want to think about cutting down
4182 the amount of symbol table stuff we output. The value stored in
4183 the TYPE_DECL's DECL_IGNORED_P slot is a first approximation.
4184 For example, if a member function is seen and we decide to
4185 write out that member function, then we can change the value
4186 of the DECL_IGNORED_P slot, and the type will be output when
4187 that member function's debug info is written out. */
4188 if (CLASSTYPE_METHOD_VEC (t))
4189 {
4190 extern tree pending_vtables;
4191
4192 /* Don't output full info about any type
4193 which does not have its implementation defined here. */
4194 if (TYPE_VIRTUAL_P (t) && write_virtuals == 2)
4195 DECL_IGNORED_P (TYPE_NAME (t))
4196 = (value_member (TYPE_IDENTIFIER (t), pending_vtables) == 0);
4197 else if (CLASSTYPE_INTERFACE_ONLY (t))
4198 DECL_IGNORED_P (TYPE_NAME (t)) = 1;
4199 else if (CLASSTYPE_INTERFACE_UNKNOWN (t))
4200 /* Only a first approximation! */
4201 DECL_IGNORED_P (TYPE_NAME (t)) = 1;
4202 }
4203 else if (CLASSTYPE_INTERFACE_ONLY (t))
4204 DECL_IGNORED_P (TYPE_NAME (t)) = 1;
4205 }
4206
4207 /* Finish debugging output for this type. */
4208 rest_of_type_compilation (t, global_bindings_p ());
4209
4210 return t;
4211}
4212\f
4213/* Return non-zero if the effective type of INSTANCE is static.
4214 Used to determine whether the virtual function table is needed
4215 or not.
4216
4217 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
4218 of our knowledge of its type. */
4219int
4220resolves_to_fixed_type_p (instance, nonnull)
4221 tree instance;
4222 int *nonnull;
4223{
4224 switch (TREE_CODE (instance))
4225 {
4226 case INDIRECT_REF:
4227 /* Check that we are not going through a cast of some sort. */
4228 if (TREE_TYPE (instance)
4229 == TREE_TYPE (TREE_TYPE (TREE_OPERAND (instance, 0))))
4230 instance = TREE_OPERAND (instance, 0);
4231 /* fall through... */
4232 case CALL_EXPR:
4233 /* This is a call to a constructor, hence it's never zero. */
4234 if (TREE_HAS_CONSTRUCTOR (instance))
4235 {
4236 if (nonnull)
4237 *nonnull = 1;
4238 return 1;
4239 }
4240 return 0;
4241
4242 case SAVE_EXPR:
4243 /* This is a call to a constructor, hence it's never zero. */
4244 if (TREE_HAS_CONSTRUCTOR (instance))
4245 {
4246 if (nonnull)
4247 *nonnull = 1;
4248 return 1;
4249 }
4250 return resolves_to_fixed_type_p (TREE_OPERAND (instance, 0), nonnull);
4251
4252 case RTL_EXPR:
4253 /* This is a call to `new', hence it's never zero. */
4254 if (TREE_CALLS_NEW (instance))
4255 {
4256 if (nonnull)
4257 *nonnull = 1;
4258 return 1;
4259 }
4260 return 0;
4261
4262 case PLUS_EXPR:
4263 case MINUS_EXPR:
4264 if (TREE_CODE (TREE_OPERAND (instance, 1)) == INTEGER_CST)
4265 /* Propagate nonnull. */
4266 resolves_to_fixed_type_p (TREE_OPERAND (instance, 0), nonnull);
4267 if (TREE_CODE (TREE_OPERAND (instance, 0)) == ADDR_EXPR)
4268 return resolves_to_fixed_type_p (TREE_OPERAND (instance, 0), nonnull);
4269 return 0;
4270
4271 case NOP_EXPR:
4272 case CONVERT_EXPR:
4273 return resolves_to_fixed_type_p (TREE_OPERAND (instance, 0), nonnull);
4274
4275 case ADDR_EXPR:
4276 if (nonnull)
4277 *nonnull = 1;
4278 return resolves_to_fixed_type_p (TREE_OPERAND (instance, 0), nonnull);
4279
4280 case COMPONENT_REF:
4281 return resolves_to_fixed_type_p (TREE_OPERAND (instance, 1), nonnull);
4282
4283 case WITH_CLEANUP_EXPR:
4284 if (TREE_CODE (TREE_OPERAND (instance, 0)) == ADDR_EXPR)
4285 return resolves_to_fixed_type_p (TREE_OPERAND (instance, 0), nonnull);
4286 /* fall through... */
4287 case VAR_DECL:
4288 case FIELD_DECL:
4289 if (TREE_CODE (TREE_TYPE (instance)) == ARRAY_TYPE
4290 && IS_AGGR_TYPE (TREE_TYPE (TREE_TYPE (instance))))
4291 {
4292 if (nonnull)
4293 *nonnull = 1;
4294 return 1;
4295 }
4296 /* fall through... */
4297 case TARGET_EXPR:
4298 case PARM_DECL:
4299 if (IS_AGGR_TYPE (TREE_TYPE (instance)))
4300 {
4301 if (nonnull)
4302 *nonnull = 1;
4303 return 1;
4304 }
4305 else if (nonnull)
4306 {
4307 if (instance == current_class_decl
4308 && flag_this_is_variable <= 0)
4309 {
4310 /* Some people still use `this = 0' inside destructors. */
4311 *nonnull = ! DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (current_function_decl));
4312 /* In a constructor, we know our type. */
4313 if (flag_this_is_variable < 0)
4314 return 1;
4315 }
4316 else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
4317 /* Reference variables should be references to objects. */
4318 *nonnull = 1;
4319 }
4320 return 0;
4321
4322 default:
4323 return 0;
4324 }
4325}
4326\f
4327void
4328init_class_processing ()
4329{
4330 current_class_depth = 0;
4331 current_class_stacksize = 10;
4332 current_class_base = (tree *)xmalloc(current_class_stacksize * sizeof (tree));
4333 current_class_stack = current_class_base;
4334
4335 current_lang_stacksize = 10;
4336 current_lang_base = (tree *)xmalloc(current_lang_stacksize * sizeof (tree));
4337 current_lang_stack = current_lang_base;
4338
4339 /* Keep these values lying around. */
4340 the_null_vtable_entry = build_vtable_entry (integer_zero_node, integer_zero_node);
4341 base_layout_decl = build_lang_field_decl (FIELD_DECL, NULL_TREE, error_mark_node);
4342 TREE_TYPE (base_layout_decl) = make_node (RECORD_TYPE);
4343
4344 gcc_obstack_init (&class_obstack);
4345}
4346
4347/* Set current scope to NAME. CODE tells us if this is a
4348 STRUCT, UNION, or ENUM environment.
4349
4350 NAME may end up being NULL_TREE if this is an anonymous or
4351 late-bound struct (as in "struct { ... } foo;") */
4352
4353/* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE to
4354 appropriate values, found by looking up the type definition of
4355 NAME (as a CODE).
4356
4357 If MODIFY is 1, we set IDENTIFIER_CLASS_VALUE's of names
4358 which can be seen locally to the class. They are shadowed by
4359 any subsequent local declaration (including parameter names).
4360
4361 If MODIFY is 2, we set IDENTIFIER_CLASS_VALUE's of names
4362 which have static meaning (i.e., static members, static
4363 member functions, enum declarations, etc).
4364
4365 If MODIFY is 3, we set IDENTIFIER_CLASS_VALUE of names
4366 which can be seen locally to the class (as in 1), but
4367 know that we are doing this for declaration purposes
4368 (i.e. friend foo::bar (int)).
4369
4370 So that we may avoid calls to lookup_name, we cache the _TYPE
4371 nodes of local TYPE_DECLs in the TREE_TYPE field of the name.
4372
4373 For multiple inheritance, we perform a two-pass depth-first search
4374 of the type lattice. The first pass performs a pre-order search,
4375 marking types after the type has had its fields installed in
4376 the appropriate IDENTIFIER_CLASS_VALUE slot. The second pass merely
4377 unmarks the marked types. If a field or member function name
4378 appears in an ambiguous way, the IDENTIFIER_CLASS_VALUE of
4379 that name becomes `error_mark_node'. */
4380
4381void
4382pushclass (type, modify)
4383 tree type;
4384 int modify;
4385{
4386 push_memoized_context (type, modify);
4387
4388 current_class_depth++;
4389 *current_class_stack++ = current_class_name;
4390 *current_class_stack++ = current_class_type;
4391 if (current_class_stack >= current_class_base + current_class_stacksize)
4392 {
4393 current_class_base =
4394 (tree *)xrealloc (current_class_base,
4395 sizeof (tree) * (current_class_stacksize + 10));
4396 current_class_stack = current_class_base + current_class_stacksize;
4397 current_class_stacksize += 10;
4398 }
4399
4400 current_class_name = TYPE_NAME (type);
4401 if (TREE_CODE (current_class_name) == TYPE_DECL)
4402 current_class_name = DECL_NAME (current_class_name);
4403 current_class_type = type;
4404
4405 if (previous_class_type != NULL_TREE
4406 && (type != previous_class_type || TYPE_SIZE (previous_class_type) == NULL_TREE)
4407 && current_class_depth == 1)
4408 {
4409 /* Forcibly remove any old class remnants. */
4410 popclass (-1);
4411 previous_class_type = NULL_TREE;
4412 }
4413
4414 pushlevel_class ();
4415
4416 if (modify)
4417 {
4418 tree tags;
4419 tree this_fndecl = current_function_decl;
4420
4421 if (current_function_decl
4422 && DECL_CONTEXT (current_function_decl)
4423 && TREE_CODE (DECL_CONTEXT (current_function_decl)) == FUNCTION_DECL)
4424 current_function_decl = DECL_CONTEXT (current_function_decl);
4425 else
4426 current_function_decl = NULL_TREE;
4427
4428 if (TREE_CODE (type) == UNINSTANTIATED_P_TYPE)
4429 {
4430 declare_uninstantiated_type_level ();
4431 overload_template_name (current_class_name, 0);
4432 }
4433 else if (type != previous_class_type || current_class_depth > 1)
4434 {
4435 build_mi_matrix (type);
4436 push_class_decls (type);
4437 free_mi_matrix ();
4438 if (current_class_depth == 1)
4439 previous_class_type = type;
4440 }
4441 else
4442 {
4443 tree item;
4444
4445 /* Hooray, our cacheing was successful, let's just install the
4446 cached class_shadowed list, and walk through it to get the
4447 IDENTIFIER_TYPE_VALUEs correct. */
4448 set_class_shadows (previous_class_values);
4449 for (item = previous_class_values; item; item = TREE_CHAIN (item))
4450 {
4451 tree id = TREE_PURPOSE (item);
4452 tree decl = IDENTIFIER_CLASS_VALUE (id);
4453
4454 if (TREE_CODE (decl) == TYPE_DECL)
4455 set_identifier_type_value (id, TREE_TYPE (decl));
4456 }
4457 unuse_fields (type);
4458 }
4459
4460 for (tags = CLASSTYPE_TAGS (type); tags; tags = TREE_CHAIN (tags))
4461 {
4462 TREE_NONLOCAL_FLAG (TREE_VALUE (tags)) = 1;
4463 if (! TREE_PURPOSE (tags))
4464 continue;
4465 pushtag (TREE_PURPOSE (tags), TREE_VALUE (tags), 0);
4466 }
4467
4468 current_function_decl = this_fndecl;
4469 }
4470
4471 if (flag_cadillac)
4472 cadillac_push_class (type);
4473}
4474
4475/* Get out of the current class scope. If we were in a class scope
4476 previously, that is the one popped to. The flag MODIFY tells
4477 whether the current scope declarations needs to be modified
4478 as a result of popping to the previous scope. */
4479void
4480popclass (modify)
4481 int modify;
4482{
4483 if (flag_cadillac)
4484 cadillac_pop_class ();
4485
4486 if (modify < 0)
4487 {
4488 /* Back this old class out completely. */
4489 tree tags = CLASSTYPE_TAGS (previous_class_type);
4490 tree t;
4491
4492 /* This code can be seen as a cache miss. When we've cached a
4493 class' scope's bindings and we can't use them, we need to reset
4494 them. This is it! */
4495 for (t = previous_class_values; t; t = TREE_CHAIN(t))
4496 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (t)) = NULL_TREE;
4497 while (tags)
4498 {
4499 TREE_NONLOCAL_FLAG (TREE_VALUE (tags)) = 0;
4500 tags = TREE_CHAIN (tags);
4501 }
4502 goto ret;
4503 }
4504
4505 if (modify)
4506 {
4507 /* Just remove from this class what didn't make
4508 it into IDENTIFIER_CLASS_VALUE. */
4509 tree tags = CLASSTYPE_TAGS (current_class_type);
4510
4511 while (tags)
4512 {
4513 TREE_NONLOCAL_FLAG (TREE_VALUE (tags)) = 0;
4514 tags = TREE_CHAIN (tags);
4515 }
4516 }
4517 if (TREE_CODE (current_class_type) == UNINSTANTIATED_P_TYPE)
4518 undo_template_name_overload (current_class_name, 0);
4519
4520 poplevel_class ();
4521
4522 /* Since poplevel_class does the popping of class decls nowadays,
4523 this really only frees the obstack used for these decls.
4524 That's why it had to be moved down here. */
4525 if (modify)
4526 pop_class_decls (current_class_type);
4527
4528 current_class_depth--;
4529 current_class_type = *--current_class_stack;
4530 current_class_name = *--current_class_stack;
4531
4532 if (current_class_type)
4533 {
4534 if (CLASSTYPE_VTBL_PTR (current_class_type))
4535 {
4536 current_vtable_decl = lookup_name (DECL_NAME (CLASSTYPE_VTBL_PTR (current_class_type)), 0);
4537 if (current_vtable_decl)
4538 current_vtable_decl = build_indirect_ref (current_vtable_decl,
4539 NULL_PTR);
4540 }
4541 current_class_decl = lookup_name (this_identifier, 0);
4542 if (current_class_decl)
4543 {
4544 if (TREE_CODE (TREE_TYPE (current_class_decl)) == POINTER_TYPE)
4545 {
4546 tree temp;
4547 /* Can't call build_indirect_ref here, because it has special
4548 logic to return C_C_D given this argument. */
4549 C_C_D = build1 (INDIRECT_REF, current_class_type, current_class_decl);
4550 temp = TREE_TYPE (TREE_TYPE (current_class_decl));
4551 TREE_READONLY (C_C_D) = TYPE_READONLY (temp);
4552 TREE_SIDE_EFFECTS (C_C_D) = TYPE_VOLATILE (temp);
4553 TREE_THIS_VOLATILE (C_C_D) = TYPE_VOLATILE (temp);
4554 }
4555 else
4556 C_C_D = current_class_decl;
4557 }
4558 else
4559 C_C_D = NULL_TREE;
4560 }
4561 else
4562 {
4563 current_class_decl = NULL_TREE;
4564 current_vtable_decl = NULL_TREE;
4565 C_C_D = NULL_TREE;
4566 }
4567
4568 pop_memoized_context (modify);
4569
4570 ret:
4571 ;
4572}
4573
4574/* When entering a class scope, all enclosing class scopes' names with
4575 static meaning (static variables, static functions, types and enumerators)
4576 have to be visible. This recursive function calls pushclass for all
4577 enclosing class contexts until global or a local scope is reached.
4578 TYPE is the enclosed class and MODIFY is equivalent with the pushclass
4579 formal of the same name. */
4580
4581void
4582push_nested_class (type, modify)
4583 tree type;
4584 int modify;
4585{
4586 tree context = DECL_CONTEXT (TYPE_NAME (type));
4587
4588 if (context && TREE_CODE (context) == RECORD_TYPE)
4589 push_nested_class (context, 2);
4590 pushclass (type, modify);
4591}
4592
4593/* Undoes a push_nested_class call. MODIFY is passed on to popclass. */
4594
4595void
4596pop_nested_class (modify)
4597 int modify;
4598{
4599 tree context = DECL_CONTEXT (TYPE_NAME (current_class_type));
4600
4601 popclass (modify);
4602 if (context && TREE_CODE (context) == RECORD_TYPE)
4603 pop_nested_class (modify);
4604}
4605
4606/* Set global variables CURRENT_LANG_NAME to appropriate value
4607 so that behavior of name-mangling machinery is correct. */
4608
4609void
4610push_lang_context (name)
4611 tree name;
4612{
4613 *current_lang_stack++ = current_lang_name;
4614 if (current_lang_stack >= current_lang_base + current_lang_stacksize)
4615 {
4616 current_lang_base =
4617 (tree *)xrealloc (current_lang_base,
4618 sizeof (tree) * (current_lang_stacksize + 10));
4619 current_lang_stack = current_lang_base + current_lang_stacksize;
4620 current_lang_stacksize += 10;
4621 }
4622
4623 if (name == lang_name_cplusplus)
4624 {
4625 strict_prototype = strict_prototypes_lang_cplusplus;
4626 current_lang_name = name;
4627 }
4628 else if (name == lang_name_c)
4629 {
4630 strict_prototype = strict_prototypes_lang_c;
4631 current_lang_name = name;
4632 }
4633 else
4634 error ("language string `\"%s\"' not recognized", IDENTIFIER_POINTER (name));
4635
4636 if (flag_cadillac)
4637 cadillac_push_lang (name);
4638}
4639
4640/* Get out of the current language scope. */
4641void
4642pop_lang_context ()
4643{
4644 if (flag_cadillac)
4645 cadillac_pop_lang ();
4646
4647 current_lang_name = *--current_lang_stack;
4648 if (current_lang_name == lang_name_cplusplus)
4649 strict_prototype = strict_prototypes_lang_cplusplus;
4650 else if (current_lang_name == lang_name_c)
4651 strict_prototype = strict_prototypes_lang_c;
4652}
4653
4654int
4655root_lang_context_p ()
4656{
4657 return current_lang_stack == current_lang_base;
4658}
4659\f
4660/* Type instantiation routines. */
4661
4662/* This function will instantiate the type of the expression given
4663 in RHS to match the type of LHSTYPE. If LHSTYPE is NULL_TREE,
4664 or other errors exist, the TREE_TYPE of RHS will be ERROR_MARK_NODE.
4665
4666 This function is used in build_modify_expr, convert_arguments,
4667 build_c_cast, and compute_conversion_costs. */
4668tree
4669instantiate_type (lhstype, rhs, complain)
4670 tree lhstype, rhs;
4671 int complain;
4672{
4673 if (TREE_CODE (lhstype) == UNKNOWN_TYPE)
4674 {
4675 if (complain)
4676 error ("not enough type information");
4677 return error_mark_node;
4678 }
4679
4680 if (TREE_TYPE (rhs) != NULL_TREE && ! (type_unknown_p (rhs)))
4681 return rhs;
4682
4683 /* This should really only be used when attempting to distinguish
4684 what sort of a pointer to function we have. For now, any
4685 arithmetic operation which is not supported on pointers
4686 is rejected as an error. */
4687
4688 switch (TREE_CODE (rhs))
4689 {
4690 case TYPE_EXPR:
4691 case CONVERT_EXPR:
4692 case SAVE_EXPR:
4693 case CONSTRUCTOR:
4694 case BUFFER_REF:
4695 my_friendly_abort (177);
4696 return error_mark_node;
4697
4698 case INDIRECT_REF:
4699 case ARRAY_REF:
4700 TREE_TYPE (rhs) = lhstype;
4701 lhstype = build_pointer_type (lhstype);
4702 TREE_OPERAND (rhs, 0)
4703 = instantiate_type (lhstype, TREE_OPERAND (rhs, 0), complain);
4704 if (TREE_OPERAND (rhs, 0) == error_mark_node)
4705 return error_mark_node;
4706
4707 return rhs;
4708
4709 case NOP_EXPR:
4710 rhs = copy_node (TREE_OPERAND (rhs, 0));
4711 TREE_TYPE (rhs) = unknown_type_node;
4712 return instantiate_type (lhstype, rhs, complain);
4713
4714 case COMPONENT_REF:
4715 {
4716 tree field = TREE_OPERAND (rhs, 1);
4717 if (TREE_CODE (field) == TREE_LIST)
4718 {
4719 tree function = instantiate_type (lhstype, field, complain);
4720 if (function == error_mark_node)
4721 return error_mark_node;
4722 my_friendly_assert (TREE_CODE (function) == FUNCTION_DECL, 185);
4723 if (DECL_VINDEX (function))
4724 {
4725 tree base = TREE_OPERAND (rhs, 0);
4726 tree base_ptr = build_unary_op (ADDR_EXPR, base, 0);
4727 if (base_ptr == error_mark_node)
4728 return error_mark_node;
4729 base_ptr = convert_pointer_to (DECL_CONTEXT (function), base_ptr);
4730 if (base_ptr == error_mark_node)
4731 return error_mark_node;
4732 return build_vfn_ref (&base_ptr, base, DECL_VINDEX (function));
4733 }
4734 return function;
4735 }
4736
4737 my_friendly_assert (TREE_CODE (field) == FIELD_DECL, 178);
4738 my_friendly_assert (!(TREE_CODE (TREE_TYPE (field)) == FUNCTION_TYPE
4739 || TREE_CODE (TREE_TYPE (field)) == METHOD_TYPE),
4740 179);
4741
4742 TREE_TYPE (rhs) = lhstype;
4743 /* First look for an exact match */
4744
4745 while (field && TREE_TYPE (field) != lhstype)
4746 field = TREE_CHAIN (field);
4747 if (field)
4748 {
4749 TREE_OPERAND (rhs, 1) = field;
4750 return rhs;
4751 }
4752
4753 /* No exact match found, look for a compatible function. */
4754 field = TREE_OPERAND (rhs, 1);
4755 while (field && ! comptypes (lhstype, TREE_TYPE (field), 0))
4756 field = TREE_CHAIN (field);
4757 if (field)
4758 {
4759 TREE_OPERAND (rhs, 1) = field;
4760 field = TREE_CHAIN (field);
4761 while (field && ! comptypes (lhstype, TREE_TYPE (field), 0))
4762 field = TREE_CHAIN (field);
4763 if (field)
4764 {
4765 if (complain)
4766 error ("ambiguous overload for COMPONENT_REF requested");
4767 return error_mark_node;
4768 }
4769 }
4770 else
4771 {
4772 if (complain)
4773 error ("no appropriate overload exists for COMPONENT_REF");
4774 return error_mark_node;
4775 }
4776 return rhs;
4777 }
4778
4779 case TREE_LIST:
4780 {
4781 tree elem, baselink, name;
4782 int globals = overloaded_globals_p (rhs);
4783
4784#if 0 /* obsolete */
4785 /* If there's only one function we know about, return that. */
4786 if (globals > 0 && TREE_CHAIN (rhs) == NULL_TREE)
4787 return TREE_VALUE (rhs);
4788#endif
4789
4790 /* First look for an exact match. Search either overloaded
4791 functions or member functions. May have to undo what
4792 `default_conversion' might do to lhstype. */
4793
4794 if (TREE_CODE (lhstype) == POINTER_TYPE)
4795 if (TREE_CODE (TREE_TYPE (lhstype)) == FUNCTION_TYPE
4796 || TREE_CODE (TREE_TYPE (lhstype)) == METHOD_TYPE)
4797 lhstype = TREE_TYPE (lhstype);
4798 else
4799 {
4800 if (complain)
4801 error ("invalid type combination for overload");
4802 return error_mark_node;
4803 }
4804
4805 if (TREE_CODE (lhstype) != FUNCTION_TYPE && globals > 0)
4806 {
4807 if (complain)
4808 cp_error ("cannot resolve overloaded function `%D' based on non-function type",
4809 TREE_PURPOSE (rhs));
4810 return error_mark_node;
4811 }
4812
4813 if (globals > 0)
4814 {
4815 elem = get_first_fn (rhs);
4816 while (elem)
4817 if (TREE_TYPE (elem) != lhstype)
4818 elem = DECL_CHAIN (elem);
4819 else
4820 return elem;
4821 /* No exact match found, look for a compatible function. */
4822 elem = get_first_fn (rhs);
4823 while (elem && ! comp_target_types (lhstype, TREE_TYPE (elem), 1))
4824 elem = DECL_CHAIN (elem);
4825 if (elem)
4826 {
4827 tree save_elem = elem;
4828 elem = DECL_CHAIN (elem);
4829 while (elem && ! comp_target_types (lhstype, TREE_TYPE (elem),
4830 0))
4831 elem = DECL_CHAIN (elem);
4832 if (elem)
4833 {
4834 if (complain)
4835 {
4836 cp_error ("cannot resolve overload to target type `%#T';", lhstype);
4837 cp_error_at ("ambiguity between `%#D'", save_elem);
4838 cp_error_at ("and `%#D', at least", elem);
4839 }
4840 return error_mark_node;
4841 }
4842 if (TREE_CODE (save_elem) == TEMPLATE_DECL)
4843 {
4844 int ntparms = TREE_VEC_LENGTH
4845 (DECL_TEMPLATE_PARMS (save_elem));
4846 tree *targs = (tree *) alloca (sizeof (tree) * ntparms);
4847 int i, dummy;
4848 i = type_unification
4849 (DECL_TEMPLATE_PARMS (save_elem), targs,
4850 TYPE_ARG_TYPES (TREE_TYPE (save_elem)),
4851 TYPE_ARG_TYPES (lhstype), &dummy, 0);
4852 save_elem = instantiate_template (save_elem, targs);
4853 }
4854 return save_elem;
4855 }
4856 if (complain)
4857 {
4858 cp_error ("cannot resolve overload to target type `%#T';",
4859 lhstype);
4860 cp_error ("no suitable overload of function `%D' exists",
4861 TREE_PURPOSE (rhs));
4862 }
4863 return error_mark_node;
4864 }
4865
4866 if (TREE_NONLOCAL_FLAG (rhs))
4867 {
4868 /* Got to get it as a baselink. */
4869 rhs = lookup_fnfields (TYPE_BINFO (current_class_type),
4870 TREE_PURPOSE (rhs), 0);
4871 }
4872 else
4873 {
4874 my_friendly_assert (TREE_CHAIN (rhs) == NULL_TREE, 181);
4875 if (TREE_CODE (TREE_VALUE (rhs)) == TREE_LIST)
4876 rhs = TREE_VALUE (rhs);
4877 my_friendly_assert (TREE_CODE (TREE_VALUE (rhs)) == FUNCTION_DECL,
4878 182);
4879 }
4880
4881 for (baselink = rhs; baselink;
4882 baselink = next_baselink (baselink))
4883 {
4884 elem = TREE_VALUE (baselink);
4885 while (elem)
4886 if (TREE_TYPE (elem) != lhstype)
4887 elem = TREE_CHAIN (elem);
4888 else
4889 return elem;
4890 }
4891
4892 /* No exact match found, look for a compatible method. */
4893 for (baselink = rhs; baselink;
4894 baselink = next_baselink (baselink))
4895 {
4896 elem = TREE_VALUE (baselink);
4897 while (elem && ! comp_target_types (lhstype, TREE_TYPE (elem), 1))
4898 elem = TREE_CHAIN (elem);
4899 if (elem)
4900 {
4901 tree save_elem = elem;
4902 elem = TREE_CHAIN (elem);
4903 while (elem && ! comp_target_types (lhstype, TREE_TYPE (elem), 0))
4904 elem = TREE_CHAIN (elem);
4905 if (elem)
4906 {
4907 if (complain)
4908 error ("ambiguous overload for overloaded method requested");
4909 return error_mark_node;
4910 }
4911 return save_elem;
4912 }
4913 name = DECL_NAME (TREE_VALUE (rhs));
4914 if (TREE_CODE (lhstype) == FUNCTION_TYPE && globals < 0)
4915 {
4916 /* Try to instantiate from non-member functions. */
4917 rhs = IDENTIFIER_GLOBAL_VALUE (name);
4918 if (rhs && TREE_CODE (rhs) == TREE_LIST)
4919 {
4920 /* This code seems to be missing a `return'. */
4921 my_friendly_abort (4);
4922 instantiate_type (lhstype, rhs, complain);
4923 }
4924 }
4925 }
4926 if (complain)
4927 error ("no static member functions named `%s'",
4928 IDENTIFIER_POINTER (name));
4929 return error_mark_node;
4930 }
4931
4932 case CALL_EXPR:
4933 /* This is too hard for now. */
4934 my_friendly_abort (183);
4935 return error_mark_node;
4936
4937 case PLUS_EXPR:
4938 case MINUS_EXPR:
4939 case COMPOUND_EXPR:
4940 TREE_OPERAND (rhs, 0) = instantiate_type (lhstype, TREE_OPERAND (rhs, 0), complain);
4941 if (TREE_OPERAND (rhs, 0) == error_mark_node)
4942 return error_mark_node;
4943 TREE_OPERAND (rhs, 1) = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), complain);
4944 if (TREE_OPERAND (rhs, 1) == error_mark_node)
4945 return error_mark_node;
4946
4947 TREE_TYPE (rhs) = lhstype;
4948 return rhs;
4949
4950 case MULT_EXPR:
4951 case TRUNC_DIV_EXPR:
4952 case FLOOR_DIV_EXPR:
4953 case CEIL_DIV_EXPR:
4954 case ROUND_DIV_EXPR:
4955 case RDIV_EXPR:
4956 case TRUNC_MOD_EXPR:
4957 case FLOOR_MOD_EXPR:
4958 case CEIL_MOD_EXPR:
4959 case ROUND_MOD_EXPR:
4960 case FIX_ROUND_EXPR:
4961 case FIX_FLOOR_EXPR:
4962 case FIX_CEIL_EXPR:
4963 case FIX_TRUNC_EXPR:
4964 case FLOAT_EXPR:
4965 case NEGATE_EXPR:
4966 case ABS_EXPR:
4967 case MAX_EXPR:
4968 case MIN_EXPR:
4969 case FFS_EXPR:
4970
4971 case BIT_AND_EXPR:
4972 case BIT_IOR_EXPR:
4973 case BIT_XOR_EXPR:
4974 case LSHIFT_EXPR:
4975 case RSHIFT_EXPR:
4976 case LROTATE_EXPR:
4977 case RROTATE_EXPR:
4978
4979 case PREINCREMENT_EXPR:
4980 case PREDECREMENT_EXPR:
4981 case POSTINCREMENT_EXPR:
4982 case POSTDECREMENT_EXPR:
4983 if (complain)
4984 error ("illegal operation on uninstantiated type");
4985 return error_mark_node;
4986
4987 case TRUTH_AND_EXPR:
4988 case TRUTH_OR_EXPR:
4989 case TRUTH_XOR_EXPR:
4990 case LT_EXPR:
4991 case LE_EXPR:
4992 case GT_EXPR:
4993 case GE_EXPR:
4994 case EQ_EXPR:
4995 case NE_EXPR:
4996 case TRUTH_ANDIF_EXPR:
4997 case TRUTH_ORIF_EXPR:
4998 case TRUTH_NOT_EXPR:
4999 if (complain)
5000 error ("not enough type information");
5001 return error_mark_node;
5002
5003 case COND_EXPR:
5004 if (type_unknown_p (TREE_OPERAND (rhs, 0)))
5005 {
5006 if (complain)
5007 error ("not enough type information");
5008 return error_mark_node;
5009 }
5010 TREE_OPERAND (rhs, 1) = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), complain);
5011 if (TREE_OPERAND (rhs, 1) == error_mark_node)
5012 return error_mark_node;
5013 TREE_OPERAND (rhs, 2) = instantiate_type (lhstype, TREE_OPERAND (rhs, 2), complain);
5014 if (TREE_OPERAND (rhs, 2) == error_mark_node)
5015 return error_mark_node;
5016
5017 TREE_TYPE (rhs) = lhstype;
5018 return rhs;
5019
5020 case MODIFY_EXPR:
5021 TREE_OPERAND (rhs, 1) = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), complain);
5022 if (TREE_OPERAND (rhs, 1) == error_mark_node)
5023 return error_mark_node;
5024
5025 TREE_TYPE (rhs) = lhstype;
5026 return rhs;
5027
5028 case ADDR_EXPR:
5029 if (TREE_CODE (lhstype) != POINTER_TYPE)
5030 {
5031 if (complain)
5032 error ("type for resolving address of overloaded function must be pointer type");
5033 return error_mark_node;
5034 }
5035 TREE_TYPE (rhs) = lhstype;
5036 lhstype = TREE_TYPE (lhstype);
5037 TREE_OPERAND (rhs, 0) = instantiate_type (lhstype, TREE_OPERAND (rhs, 0), complain);
5038 if (TREE_OPERAND (rhs, 0) == error_mark_node)
5039 return error_mark_node;
5040
5041 mark_addressable (TREE_OPERAND (rhs, 0));
5042 return rhs;
5043
5044 case ENTRY_VALUE_EXPR:
5045 my_friendly_abort (184);
5046 return error_mark_node;
5047
5048 case ERROR_MARK:
5049 return error_mark_node;
5050
5051 default:
5052 my_friendly_abort (185);
5053 return error_mark_node;
5054 }
5055}
5056\f
5057/* Return the name of the virtual function pointer field
5058 (as an IDENTIFIER_NODE) for the given TYPE. Note that
5059 this may have to look back through base types to find the
5060 ultimate field name. (For single inheritance, these could
5061 all be the same name. Who knows for multiple inheritance). */
5062static tree
5063get_vfield_name (type)
5064 tree type;
5065{
5066 tree binfo = TYPE_BINFO (type);
5067 char *buf;
5068
5069 while (BINFO_BASETYPES (binfo)
5070 && TYPE_VIRTUAL_P (BINFO_TYPE (BINFO_BASETYPE (binfo, 0)))
5071 && ! TREE_VIA_VIRTUAL (BINFO_BASETYPE (binfo, 0)))
5072 binfo = BINFO_BASETYPE (binfo, 0);
5073
5074 type = BINFO_TYPE (binfo);
5075 buf = (char *)alloca (sizeof (VFIELD_NAME_FORMAT)
5076 + TYPE_NAME_LENGTH (type) + 2);
5077 sprintf (buf, VFIELD_NAME_FORMAT, TYPE_NAME_STRING (type));
5078 return get_identifier (buf);
5079}
5080
5081void
5082print_class_statistics ()
5083{
5084#ifdef GATHER_STATISTICS
5085 fprintf (stderr, "convert_harshness = %d\n", n_convert_harshness);
5086 fprintf (stderr, "compute_conversion_costs = %d\n", n_compute_conversion_costs);
5087 fprintf (stderr, "build_method_call = %d (inner = %d)\n",
5088 n_build_method_call, n_inner_fields_searched);
5089 if (n_vtables)
5090 {
5091 fprintf (stderr, "vtables = %d; vtable searches = %d\n",
5092 n_vtables, n_vtable_searches);
5093 fprintf (stderr, "vtable entries = %d; vtable elems = %d\n",
5094 n_vtable_entries, n_vtable_elems);
5095 }
5096#endif
5097}
5098
5099/* Push an obstack which is sufficiently long-lived to hold such class
5100 decls that may be cached in the previous_class_values list. For now, let's
5101 use the permanent obstack, later we may create a dedicated obstack just
5102 for this purpose. The effect is undone by pop_obstacks. */
5103void
5104maybe_push_cache_obstack ()
5105{
5106 push_obstacks_nochange ();
5107 if (current_class_depth == 1)
5108 current_obstack = &permanent_obstack;
5109}
This page took 0.495866 seconds and 5 git commands to generate.