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