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