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