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