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