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