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