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