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