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