]>
Commit | Line | Data |
---|---|---|
8d08fdba MS |
1 | /* Handle initialization things in C++. |
2 | Copyright (C) 1987, 1989, 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 "rtl.h" | |
27 | #include "cp-tree.h" | |
28 | #include "flags.h" | |
29 | ||
30 | #undef NULL | |
31 | #define NULL 0 | |
32 | ||
33 | /* In C++, structures with well-defined constructors are initialized by | |
34 | those constructors, unasked. CURRENT_BASE_INIT_LIST | |
35 | holds a list of stmts for a BASE_INIT term in the grammar. | |
36 | This list has one element for each base class which must be | |
37 | initialized. The list elements are [basename, init], with | |
38 | type basetype. This allows the possibly anachronistic form | |
39 | (assuming d : a, b, c) "d (int a) : c(a+5), b (a-4), a (a+3)" | |
40 | where each successive term can be handed down the constructor | |
41 | line. Perhaps this was not intended. */ | |
42 | tree current_base_init_list, current_member_init_list; | |
43 | ||
44 | void emit_base_init (); | |
45 | void check_base_init (); | |
46 | static void expand_aggr_vbase_init (); | |
47 | void expand_member_init (); | |
48 | void expand_aggr_init (); | |
49 | ||
50 | static void expand_aggr_init_1 (); | |
51 | static void expand_recursive_init_1 (); | |
52 | static void expand_recursive_init (); | |
7177d104 | 53 | static void expand_virtual_init PROTO((tree, tree)); |
8d08fdba | 54 | tree expand_vec_init (); |
8d08fdba MS |
55 | |
56 | static void add_friend (), add_friends (); | |
57 | ||
58 | /* Cache _builtin_new and _builtin_delete exprs. */ | |
a28e3c7f | 59 | static tree BIN, BID, BIVN, BIVD; |
8d08fdba MS |
60 | |
61 | /* Cache the identifier nodes for the two magic field of a new cookie. */ | |
62 | static tree nc_nelts_field_id; | |
8926095f | 63 | #if 0 |
8d08fdba | 64 | static tree nc_ptr_2comp_field_id; |
8926095f | 65 | #endif |
8d08fdba MS |
66 | |
67 | static tree minus_one; | |
68 | ||
69 | /* Set up local variable for this file. MUST BE CALLED AFTER | |
70 | INIT_DECL_PROCESSING. */ | |
71 | ||
72 | tree BI_header_type, BI_header_size; | |
73 | ||
74 | void init_init_processing () | |
75 | { | |
76 | tree fields[1]; | |
77 | ||
78 | /* Define implicit `operator new' and `operator delete' functions. */ | |
79 | BIN = default_conversion (get_first_fn (IDENTIFIER_GLOBAL_VALUE (ansi_opname[(int) NEW_EXPR]))); | |
80 | TREE_USED (TREE_OPERAND (BIN, 0)) = 0; | |
81 | BID = default_conversion (get_first_fn (IDENTIFIER_GLOBAL_VALUE (ansi_opname[(int) DELETE_EXPR]))); | |
82 | TREE_USED (TREE_OPERAND (BID, 0)) = 0; | |
a28e3c7f MS |
83 | BIVN = default_conversion (get_first_fn (IDENTIFIER_GLOBAL_VALUE (ansi_opname[(int) VEC_NEW_EXPR]))); |
84 | TREE_USED (TREE_OPERAND (BIVN, 0)) = 0; | |
85 | BIVD = default_conversion (get_first_fn (IDENTIFIER_GLOBAL_VALUE (ansi_opname[(int) VEC_DELETE_EXPR]))); | |
86 | TREE_USED (TREE_OPERAND (BIVD, 0)) = 0; | |
8d08fdba MS |
87 | minus_one = build_int_2 (-1, -1); |
88 | ||
89 | /* Define the structure that holds header information for | |
90 | arrays allocated via operator new. */ | |
91 | BI_header_type = make_lang_type (RECORD_TYPE); | |
92 | nc_nelts_field_id = get_identifier ("nelts"); | |
93 | fields[0] = build_lang_field_decl (FIELD_DECL, nc_nelts_field_id, sizetype); | |
94 | finish_builtin_type (BI_header_type, "__new_cookie", fields, | |
95 | 0, double_type_node); | |
96 | BI_header_size = size_in_bytes (BI_header_type); | |
97 | } | |
98 | ||
99 | /* Subroutine of emit_base_init. For BINFO, initialize all the | |
100 | virtual function table pointers, except those that come from | |
101 | virtual base classes. Initialize binfo's vtable pointer, if | |
102 | INIT_SELF is true. CAN_ELIDE is true when we know that all virtual | |
103 | function table pointers in all bases have been initialized already, | |
7177d104 MS |
104 | probably because their constructors have just be run. ADDR is the |
105 | pointer to the object whos vtables we are going to initialize. | |
106 | ||
107 | REAL_BINFO is usually the same as BINFO, except when addr is not of | |
108 | pointer to the type of the real derived type that we want to | |
109 | initialize for. This is the case when addr is a pointer to a sub | |
110 | object of a complete object, and we only want to do part of the | |
111 | complete object's initiailzation of vtable pointers. This is done | |
112 | for all virtual table pointers in virtual base classes. REAL_BINFO | |
113 | is used to find the BINFO_VTABLE that we initialize with. BINFO is | |
114 | used for conversions of addr to subobjects. | |
115 | ||
116 | BINFO_TYPE (real_binfo) must be BINFO_TYPE (binfo). | |
117 | ||
118 | Relies upon binfo being inside TYPE_BINFO (TREE_TYPE (TREE_TYPE | |
119 | (addr))). */ | |
8d08fdba | 120 | void |
7177d104 MS |
121 | expand_direct_vtbls_init (real_binfo, binfo, init_self, can_elide, addr) |
122 | tree real_binfo, binfo, addr; | |
8d08fdba MS |
123 | int init_self, can_elide; |
124 | { | |
7177d104 | 125 | tree real_binfos = BINFO_BASETYPES (real_binfo); |
8d08fdba | 126 | tree binfos = BINFO_BASETYPES (binfo); |
7177d104 | 127 | int i, n_baselinks = real_binfos ? TREE_VEC_LENGTH (real_binfos) : 0; |
8d08fdba MS |
128 | |
129 | for (i = 0; i < n_baselinks; i++) | |
130 | { | |
7177d104 | 131 | tree real_base_binfo = TREE_VEC_ELT (real_binfos, i); |
8d08fdba MS |
132 | tree base_binfo = TREE_VEC_ELT (binfos, i); |
133 | int is_not_base_vtable = | |
7177d104 MS |
134 | i != CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (real_binfo)); |
135 | if (! TREE_VIA_VIRTUAL (real_base_binfo)) | |
136 | expand_direct_vtbls_init (real_base_binfo, base_binfo, | |
137 | is_not_base_vtable, can_elide, addr); | |
8d08fdba MS |
138 | } |
139 | #if 0 | |
140 | /* Before turning this on, make sure it is correct. */ | |
141 | if (can_elide && ! BINFO_MODIFIED (binfo)) | |
142 | return; | |
143 | #endif | |
144 | /* Should we use something besides CLASSTYPE_VFIELDS? */ | |
7177d104 | 145 | if (init_self && CLASSTYPE_VFIELDS (BINFO_TYPE (real_binfo))) |
8d08fdba | 146 | { |
7177d104 MS |
147 | tree base_ptr = convert_pointer_to_real (binfo, addr); |
148 | expand_virtual_init (real_binfo, base_ptr); | |
8d08fdba MS |
149 | } |
150 | } | |
151 | \f | |
152 | /* 348 - 351 */ | |
153 | /* Subroutine of emit_base_init. */ | |
154 | static void | |
155 | perform_member_init (member, name, init, explicit) | |
156 | tree member, name, init; | |
157 | int explicit; | |
158 | { | |
159 | tree decl; | |
160 | tree type = TREE_TYPE (member); | |
161 | ||
162 | if (TYPE_NEEDS_CONSTRUCTING (type) | |
163 | || (init && TYPE_HAS_CONSTRUCTOR (type))) | |
164 | { | |
165 | /* Since `init' is already a TREE_LIST on the current_member_init_list, | |
166 | only build it into one if we aren't already a list. */ | |
167 | if (init != NULL_TREE && TREE_CODE (init) != TREE_LIST) | |
168 | init = build_tree_list (NULL_TREE, init); | |
169 | ||
170 | decl = build_component_ref (C_C_D, name, 0, explicit); | |
171 | ||
172 | if (explicit | |
173 | && TREE_CODE (type) == ARRAY_TYPE | |
174 | && init != NULL_TREE | |
175 | && TREE_CHAIN (init) == NULL_TREE | |
176 | && TREE_CODE (TREE_TYPE (TREE_VALUE (init))) == ARRAY_TYPE) | |
177 | { | |
178 | /* Initialization of one array from another. */ | |
179 | expand_vec_init (TREE_OPERAND (decl, 1), decl, | |
180 | array_type_nelts (type), TREE_VALUE (init), 1); | |
181 | } | |
182 | else | |
183 | expand_aggr_init (decl, init, 0); | |
184 | } | |
185 | else | |
186 | { | |
187 | if (init == NULL_TREE) | |
188 | { | |
189 | if (explicit) | |
190 | { | |
191 | cp_error ("incomplete initializer for member `%D' of class `%T' which has no constructor", | |
192 | member, current_class_type); | |
193 | init = error_mark_node; | |
194 | } | |
195 | /* member traversal: note it leaves init NULL */ | |
196 | else if (TREE_CODE (TREE_TYPE (member)) == REFERENCE_TYPE) | |
197 | cp_pedwarn ("uninitialized reference member `%D'", member); | |
198 | } | |
199 | else if (TREE_CODE (init) == TREE_LIST) | |
200 | { | |
201 | /* There was an explicit member initialization. Do some | |
202 | work in that case. */ | |
203 | if (TREE_CHAIN (init)) | |
204 | { | |
205 | warning ("initializer list treated as compound expression"); | |
206 | init = build_compound_expr (init); | |
207 | } | |
208 | else | |
209 | init = TREE_VALUE (init); | |
210 | } | |
211 | ||
212 | /* We only build this with a null init if we got it from the | |
213 | current_member_init_list. */ | |
214 | if (init || explicit) | |
215 | { | |
216 | decl = build_component_ref (C_C_D, name, 0, explicit); | |
217 | expand_expr_stmt (build_modify_expr (decl, INIT_EXPR, init)); | |
218 | } | |
219 | } | |
8d2733ca MS |
220 | if (flag_handle_exceptions && TYPE_NEEDS_DESTRUCTOR (type)) |
221 | cp_warning ("caution, member `%D' may not be destroyed in the presense of an exception during construction", member); | |
8d08fdba MS |
222 | } |
223 | ||
224 | /* Subroutine of emit_member_init. */ | |
225 | static tree | |
226 | sort_member_init (t) | |
227 | tree t; | |
228 | { | |
229 | tree x, member, name, field, init; | |
230 | tree init_list = NULL_TREE; | |
231 | tree fields_to_unmark = NULL_TREE; | |
232 | int found; | |
233 | ||
234 | for (member = TYPE_FIELDS (t); member ; member = TREE_CHAIN (member)) | |
235 | { | |
236 | found = 0; | |
237 | for (x = current_member_init_list ; x ; x = TREE_CHAIN (x)) | |
238 | { | |
239 | /* If we cleared this out, then pay no attention to it. */ | |
240 | if (TREE_PURPOSE (x) == NULL_TREE) | |
241 | continue; | |
242 | name = TREE_PURPOSE (x); | |
243 | ||
244 | #if 0 | |
245 | field = (TREE_CODE (name) == COMPONENT_REF | |
246 | ? TREE_OPERAND (name, 1) : IDENTIFIER_CLASS_VALUE (name)); | |
247 | #else | |
248 | /* Let's find out when this happens. */ | |
249 | my_friendly_assert (TREE_CODE (name) != COMPONENT_REF, 348); | |
250 | field = IDENTIFIER_CLASS_VALUE (name); | |
251 | #endif | |
252 | ||
253 | /* If one member shadows another, get the outermost one. */ | |
254 | if (TREE_CODE (field) == TREE_LIST) | |
255 | field = TREE_VALUE (field); | |
256 | ||
257 | if (field == member) | |
258 | { | |
259 | /* See if we already found an initializer for this field. */ | |
260 | if (found) | |
261 | { | |
262 | if (DECL_NAME (field)) | |
263 | cp_error ("multiple initializations given for member `%D'", | |
264 | field); | |
265 | continue; | |
266 | } | |
267 | ||
268 | init_list = chainon (init_list, | |
269 | build_tree_list (name, TREE_VALUE (x))); | |
270 | /* Make sure we won't try to work on this init again. */ | |
271 | TREE_PURPOSE (x) = NULL_TREE; | |
272 | found = 1; | |
273 | break; | |
274 | } | |
275 | } | |
276 | ||
277 | /* If we didn't find MEMBER in the list, create a dummy entry | |
278 | so the two lists (INIT_LIST and the list of members) will be | |
279 | symmetrical. */ | |
280 | if (! found) | |
281 | init_list = chainon (init_list, build_tree_list (NULL_TREE, NULL_TREE)); | |
282 | } | |
283 | ||
284 | for (x = current_member_init_list ; x ; x = TREE_CHAIN (x)) | |
285 | { | |
286 | if (TREE_PURPOSE (x)) | |
287 | { | |
288 | name = TREE_PURPOSE (x); | |
289 | init = TREE_VALUE (x); | |
290 | /* XXX: this may need the COMPONENT_REF operand 0 check if | |
291 | it turns out we actually get them. */ | |
292 | field = IDENTIFIER_CLASS_VALUE (name); | |
293 | ||
294 | /* If one member shadows another, get the outermost one. */ | |
295 | if (TREE_CODE (field) == TREE_LIST) | |
296 | { | |
297 | field = TREE_VALUE (field); | |
298 | if (decl_type_context (field) != current_class_type) | |
299 | cp_error ("field `%D' not in immediate context", field); | |
300 | } | |
301 | ||
302 | #if 0 | |
303 | /* It turns out if you have an anonymous union in the | |
304 | class, a member from it can end up not being on the | |
305 | list of fields (rather, the type is), and therefore | |
306 | won't be seen by the for loop above. */ | |
307 | ||
308 | /* The code in this for loop is derived from a general loop | |
309 | which had this check in it. Theoretically, we've hit | |
310 | every initialization for the list of members in T, so | |
311 | we shouldn't have anything but these left in this list. */ | |
312 | my_friendly_assert (DECL_FIELD_CONTEXT (field) != t, 351); | |
313 | #endif | |
314 | ||
315 | if (TREE_HAS_CONSTRUCTOR (field)) | |
316 | { | |
317 | if (DECL_NAME (field)) | |
318 | error ("multiple initializations given for member `%s'", | |
319 | IDENTIFIER_POINTER (DECL_NAME (field))); | |
320 | continue; | |
321 | } | |
322 | ||
323 | TREE_HAS_CONSTRUCTOR (field) = 1; | |
324 | fields_to_unmark = tree_cons (NULL_TREE, field, fields_to_unmark); | |
325 | ||
326 | perform_member_init (field, name, init, 1); | |
327 | TREE_PURPOSE (x) = NULL_TREE; | |
328 | } | |
329 | } | |
330 | ||
331 | /* Unmark fields which are initialized for the base class. */ | |
332 | while (fields_to_unmark) | |
333 | { | |
334 | TREE_HAS_CONSTRUCTOR (TREE_VALUE (fields_to_unmark)) = 0; | |
335 | /* XXX is this a memory leak? */ | |
336 | fields_to_unmark = TREE_CHAIN (fields_to_unmark); | |
337 | } | |
338 | ||
339 | return init_list; | |
340 | } | |
341 | ||
342 | /* Perform whatever initializations have yet to be done on the base | |
343 | class of the class variable. These actions are in the global | |
344 | variable CURRENT_BASE_INIT_LIST. Such an action could be | |
345 | NULL_TREE, meaning that the user has explicitly called the base | |
346 | class constructor with no arguments. | |
347 | ||
348 | If there is a need for a call to a constructor, we must surround | |
349 | that call with a pushlevel/poplevel pair, since we are technically | |
350 | at the PARM level of scope. | |
351 | ||
352 | Argument IMMEDIATELY, if zero, forces a new sequence to be | |
353 | generated to contain these new insns, so it can be emitted later. | |
354 | This sequence is saved in the global variable BASE_INIT_INSNS. | |
355 | Otherwise, the insns are emitted into the current sequence. | |
356 | ||
357 | Note that emit_base_init does *not* initialize virtual base | |
358 | classes. That is done specially, elsewhere. */ | |
359 | ||
360 | void | |
361 | emit_base_init (t, immediately) | |
362 | tree t; | |
363 | int immediately; | |
364 | { | |
365 | extern tree in_charge_identifier; | |
366 | ||
8926095f MS |
367 | tree member, vbases; |
368 | tree init_list; | |
8d08fdba MS |
369 | int pass, start; |
370 | tree t_binfo = TYPE_BINFO (t); | |
371 | tree binfos = BINFO_BASETYPES (t_binfo); | |
372 | int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0; | |
8d08fdba MS |
373 | int have_init_list = 0, from_init_list; |
374 | ||
375 | if (! immediately) | |
376 | { | |
377 | do_pending_stack_adjust (); | |
378 | start_sequence (); | |
379 | } | |
380 | ||
381 | if (write_symbols == NO_DEBUG) | |
382 | /* As a matter of principle, `start_sequence' should do this. */ | |
383 | emit_note (0, -1); | |
384 | else | |
385 | /* Always emit a line number note so we can step into constructors. */ | |
386 | emit_line_note_force (DECL_SOURCE_FILE (current_function_decl), | |
387 | DECL_SOURCE_LINE (current_function_decl)); | |
388 | ||
8d08fdba MS |
389 | start = ! TYPE_USES_VIRTUAL_BASECLASSES (t); |
390 | for (pass = start; pass < 2; pass++) | |
391 | { | |
392 | tree vbase_init_list = NULL_TREE; | |
393 | ||
394 | for (init_list = current_base_init_list; init_list; | |
395 | init_list = TREE_CHAIN (init_list)) | |
396 | { | |
397 | tree basename = TREE_PURPOSE (init_list); | |
398 | tree binfo; | |
399 | tree init = TREE_VALUE (init_list); | |
400 | ||
401 | if (basename == NULL_TREE) | |
402 | { | |
403 | /* Initializer for single base class. Must not | |
404 | use multiple inheritance or this is ambiguous. */ | |
405 | switch (n_baseclasses) | |
406 | { | |
407 | case 0: | |
408 | error ("type `%s' does not have a base class to initialize", | |
409 | IDENTIFIER_POINTER (current_class_name)); | |
410 | return; | |
411 | case 1: | |
412 | break; | |
413 | default: | |
414 | error ("unnamed initializer ambiguous for type `%s' which uses multiple inheritance", IDENTIFIER_POINTER (current_class_name)); | |
415 | return; | |
416 | } | |
417 | binfo = TREE_VEC_ELT (binfos, 0); | |
418 | } | |
419 | else if (is_aggr_typedef (basename, 1)) | |
420 | { | |
421 | binfo = binfo_or_else (IDENTIFIER_TYPE_VALUE (basename), t); | |
422 | if (binfo == NULL_TREE) | |
423 | continue; | |
424 | ||
425 | /* Virtual base classes are special cases. Their initializers | |
426 | are recorded with this constructor, and they are used when | |
427 | this constructor is the top-level constructor called. */ | |
428 | if (! TREE_VIA_VIRTUAL (binfo)) | |
429 | { | |
430 | /* Otherwise, if it is not an immediate base class, complain. */ | |
431 | for (i = n_baseclasses-1; i >= 0; i--) | |
432 | if (BINFO_TYPE (binfo) == BINFO_TYPE (TREE_VEC_ELT (binfos, i))) | |
433 | break; | |
434 | if (i < 0) | |
435 | { | |
436 | error ("type `%s' is not an immediate base class of type `%s'", | |
437 | IDENTIFIER_POINTER (basename), | |
438 | IDENTIFIER_POINTER (current_class_name)); | |
439 | continue; | |
440 | } | |
441 | } | |
442 | } | |
443 | else | |
444 | continue; | |
445 | ||
446 | /* The base initialization list goes up to the first | |
447 | base class which can actually use it. */ | |
448 | ||
449 | if (pass == start) | |
450 | { | |
451 | char *msgp = (! TYPE_HAS_CONSTRUCTOR (BINFO_TYPE (binfo))) | |
452 | ? "cannot pass initialization up to class `%s'" : 0; | |
453 | ||
454 | while (! TYPE_HAS_CONSTRUCTOR (BINFO_TYPE (binfo)) | |
455 | && BINFO_BASETYPES (binfo) != NULL_TREE | |
456 | && TREE_VEC_LENGTH (BINFO_BASETYPES (binfo)) == 1) | |
457 | { | |
458 | /* ?? This should be fixed in RENO by forcing | |
459 | default constructors to exist. */ | |
460 | SET_BINFO_BASEINIT_MARKED (binfo); | |
461 | binfo = BINFO_BASETYPE (binfo, 0); | |
462 | } | |
463 | ||
464 | /* We used to give an error if this wasn't true, saying that | |
465 | there's no constructor for the initialization of basename. | |
466 | This turned out to be incorrect---it should use the | |
467 | default constructor, since a user could try to initialize | |
468 | the class in a derived class's base initializer list. */ | |
469 | if (TYPE_HAS_CONSTRUCTOR (BINFO_TYPE (binfo))) | |
470 | { | |
471 | if (msgp) | |
472 | { | |
473 | if (pedantic) | |
474 | error_with_aggr_type (binfo, msgp); | |
475 | else | |
476 | msgp = NULL; | |
477 | } | |
478 | } | |
479 | ||
480 | if (BINFO_BASEINIT_MARKED (binfo)) | |
481 | { | |
482 | msgp = "class `%s' initializer already specified"; | |
483 | error (msgp, IDENTIFIER_POINTER (basename)); | |
484 | } | |
485 | ||
486 | if (msgp) | |
487 | continue; | |
488 | ||
489 | SET_BINFO_BASEINIT_MARKED (binfo); | |
490 | if (TREE_VIA_VIRTUAL (binfo)) | |
491 | { | |
492 | vbase_init_list = tree_cons (init, BINFO_TYPE (binfo), | |
493 | vbase_init_list); | |
494 | continue; | |
495 | } | |
496 | if (pass == 0) | |
497 | continue; | |
498 | } | |
499 | else if (TREE_VIA_VIRTUAL (binfo)) | |
500 | continue; | |
501 | ||
502 | member = convert_pointer_to (binfo, current_class_decl); | |
503 | expand_aggr_init_1 (t_binfo, 0, | |
504 | build_indirect_ref (member, NULL_PTR), init, | |
505 | BINFO_OFFSET_ZEROP (binfo), LOOKUP_COMPLAIN); | |
8d08fdba MS |
506 | } |
507 | ||
508 | if (pass == 0) | |
509 | { | |
510 | tree first_arg = TREE_CHAIN (DECL_ARGUMENTS (current_function_decl)); | |
511 | tree vbases; | |
512 | ||
513 | if (DECL_NAME (current_function_decl) == NULL_TREE | |
514 | && TREE_CHAIN (first_arg) != NULL_TREE) | |
515 | { | |
516 | /* If there are virtual baseclasses without initialization | |
517 | specified, and this is a default X(X&) constructor, | |
518 | build the initialization list so that each virtual baseclass | |
519 | of the new object is initialized from the virtual baseclass | |
520 | of the incoming arg. */ | |
521 | tree init_arg = build_unary_op (ADDR_EXPR, TREE_CHAIN (first_arg), 0); | |
522 | for (vbases = CLASSTYPE_VBASECLASSES (t); | |
523 | vbases; vbases = TREE_CHAIN (vbases)) | |
524 | { | |
525 | if (BINFO_BASEINIT_MARKED (vbases) == 0) | |
526 | { | |
527 | member = convert_pointer_to (vbases, init_arg); | |
528 | if (member == init_arg) | |
529 | member = TREE_CHAIN (first_arg); | |
530 | else | |
531 | TREE_TYPE (member) = build_reference_type (BINFO_TYPE (vbases)); | |
532 | vbase_init_list = tree_cons (convert_from_reference (member), | |
533 | vbases, vbase_init_list); | |
534 | SET_BINFO_BASEINIT_MARKED (vbases); | |
535 | } | |
536 | } | |
537 | } | |
538 | expand_start_cond (first_arg, 0); | |
539 | expand_aggr_vbase_init (t_binfo, C_C_D, current_class_decl, | |
540 | vbase_init_list); | |
541 | expand_end_cond (); | |
542 | } | |
543 | } | |
544 | current_base_init_list = NULL_TREE; | |
545 | ||
546 | /* Now, perform default initialization of all base classes which | |
547 | have not yet been initialized, and unmark baseclasses which | |
548 | have been initialized. */ | |
549 | for (i = 0; i < n_baseclasses; i++) | |
550 | { | |
551 | tree base = current_class_decl; | |
552 | tree base_binfo = TREE_VEC_ELT (binfos, i); | |
553 | ||
554 | if (TYPE_NEEDS_CONSTRUCTING (BINFO_TYPE (base_binfo))) | |
555 | { | |
556 | if (! TREE_VIA_VIRTUAL (base_binfo) | |
557 | && ! BINFO_BASEINIT_MARKED (base_binfo)) | |
558 | { | |
559 | tree ref; | |
560 | ||
561 | if (BINFO_OFFSET_ZEROP (base_binfo)) | |
562 | base = build1 (NOP_EXPR, | |
563 | TYPE_POINTER_TO (BINFO_TYPE (base_binfo)), | |
564 | current_class_decl); | |
565 | else | |
566 | base = build (PLUS_EXPR, | |
567 | TYPE_POINTER_TO (BINFO_TYPE (base_binfo)), | |
568 | current_class_decl, BINFO_OFFSET (base_binfo)); | |
569 | ||
570 | ref = build_indirect_ref (base, NULL_PTR); | |
571 | expand_aggr_init_1 (t_binfo, 0, ref, NULL_TREE, | |
572 | BINFO_OFFSET_ZEROP (base_binfo), | |
573 | LOOKUP_COMPLAIN); | |
8d08fdba MS |
574 | } |
575 | } | |
576 | CLEAR_BINFO_BASEINIT_MARKED (base_binfo); | |
577 | ||
578 | if (! TYPE_USES_VIRTUAL_BASECLASSES (t)) | |
579 | { | |
580 | while (! TYPE_HAS_CONSTRUCTOR (BINFO_TYPE (base_binfo)) | |
581 | && BINFO_BASETYPES (base_binfo) != NULL_TREE | |
582 | && TREE_VEC_LENGTH (BINFO_BASETYPES (base_binfo)) == 1) | |
583 | { | |
584 | /* ?? This should be fixed in RENO by forcing | |
585 | default constructors to exist. It is needed for symmetry | |
586 | with code above. */ | |
587 | base_binfo = BINFO_BASETYPE (base_binfo, 0); | |
588 | CLEAR_BINFO_BASEINIT_MARKED (base_binfo); | |
589 | } | |
590 | } | |
591 | } | |
592 | ||
593 | /* Initialize all the virtual function table fields that | |
594 | do come from virtual base classes. */ | |
595 | if (TYPE_USES_VIRTUAL_BASECLASSES (t)) | |
7177d104 | 596 | expand_indirect_vtbls_init (t_binfo, C_C_D, current_class_decl, 0); |
8d08fdba MS |
597 | for (vbases = CLASSTYPE_VBASECLASSES (t); vbases; vbases = TREE_CHAIN (vbases)) |
598 | CLEAR_BINFO_BASEINIT_MARKED (vbases); | |
599 | ||
600 | /* Initialize all the virtual function table fields that | |
601 | do not come from virtual base classes. */ | |
7177d104 | 602 | expand_direct_vtbls_init (t_binfo, t_binfo, 1, 1, current_class_decl); |
8d08fdba MS |
603 | |
604 | if (current_member_init_list) | |
605 | { | |
606 | init_list = sort_member_init (t); | |
607 | have_init_list = 1; | |
608 | } | |
609 | ||
610 | for (member = TYPE_FIELDS (t); member; member = TREE_CHAIN (member)) | |
611 | { | |
612 | tree init, name; | |
613 | from_init_list = 0; | |
614 | ||
615 | /* See if we had a user-specified member initialization. */ | |
616 | if (have_init_list) | |
617 | { | |
618 | if (TREE_PURPOSE (init_list)) | |
619 | { | |
620 | name = TREE_PURPOSE (init_list); | |
621 | init = TREE_VALUE (init_list); | |
622 | from_init_list = 1; | |
623 | ||
624 | if (TREE_STATIC (member)) | |
625 | { | |
626 | error_with_aggr_type (DECL_FIELD_CONTEXT (member), | |
627 | "field `%s::%s' is static; only point of initialization is its declaration", | |
628 | IDENTIFIER_POINTER (TREE_PURPOSE (init_list))); | |
629 | continue; | |
630 | } | |
631 | ||
632 | /* Also see if it's ever a COMPONENT_REF here. If it is, we | |
633 | need to do `expand_assignment (name, init, 0, 0);' and | |
634 | a continue. */ | |
635 | my_friendly_assert (TREE_CODE (name) != COMPONENT_REF, 349); | |
636 | } | |
637 | ||
638 | init_list = TREE_CHAIN (init_list); | |
639 | } | |
640 | ||
641 | if (! from_init_list) | |
642 | { | |
643 | /* member could be, for example, a CONST_DECL for an enumerated | |
644 | tag; we don't want to try to initialize that, since it already | |
645 | has a value. */ | |
a4443a08 | 646 | if (TREE_CODE (member) != FIELD_DECL || !DECL_NAME (member)) |
8d08fdba MS |
647 | continue; |
648 | ||
649 | name = DECL_NAME (member); | |
650 | init = DECL_INITIAL (member); | |
651 | } | |
652 | ||
653 | perform_member_init (member, name, init, from_init_list); | |
654 | } | |
655 | ||
656 | current_member_init_list = NULL_TREE; | |
657 | ||
658 | /* It is possible for the initializers to need cleanups. | |
659 | Expand those cleanups now that all the initialization | |
660 | has been done. */ | |
661 | expand_cleanups_to (NULL_TREE); | |
662 | ||
663 | if (! immediately) | |
664 | { | |
665 | extern rtx base_init_insns; | |
666 | ||
667 | do_pending_stack_adjust (); | |
668 | my_friendly_assert (base_init_insns == 0, 207); | |
669 | base_init_insns = get_insns (); | |
670 | end_sequence (); | |
671 | } | |
672 | ||
673 | /* All the implicit try blocks we built up will be zapped | |
674 | when we come to a real binding contour boundary. */ | |
675 | } | |
676 | ||
677 | /* Check that all fields are properly initialized after | |
678 | an assignment to `this'. */ | |
679 | void | |
680 | check_base_init (t) | |
681 | tree t; | |
682 | { | |
683 | tree member; | |
684 | for (member = TYPE_FIELDS (t); member; member = TREE_CHAIN (member)) | |
685 | if (DECL_NAME (member) && TREE_USED (member)) | |
686 | cp_error ("field `%D' used before initialized (after assignment to `this')", | |
687 | member); | |
688 | } | |
689 | ||
690 | /* This code sets up the virtual function tables appropriate for | |
691 | the pointer DECL. It is a one-ply initialization. | |
692 | ||
693 | BINFO is the exact type that DECL is supposed to be. In | |
694 | multiple inheritance, this might mean "C's A" if C : A, B. */ | |
8926095f | 695 | static void |
7177d104 MS |
696 | expand_virtual_init (binfo, decl) |
697 | tree binfo, decl; | |
8d08fdba | 698 | { |
7177d104 | 699 | tree type = BINFO_TYPE (binfo); |
8d08fdba MS |
700 | tree vtbl, vtbl_ptr; |
701 | tree vtype, vtype_binfo; | |
702 | ||
7177d104 MS |
703 | /* This code is crusty. Should be simple, like: |
704 | vtbl = BINFO_VTABLE (binfo); | |
705 | */ | |
8d08fdba MS |
706 | vtype = DECL_CONTEXT (CLASSTYPE_VFIELD (type)); |
707 | vtype_binfo = get_binfo (vtype, TREE_TYPE (TREE_TYPE (decl)), 0); | |
7177d104 MS |
708 | vtbl = BINFO_VTABLE (binfo_value (DECL_FIELD_CONTEXT (CLASSTYPE_VFIELD (type)), binfo)); |
709 | if (!flag_vtable_thunks) | |
710 | assemble_external (vtbl); | |
711 | TREE_USED (vtbl) = 1; | |
712 | vtbl = build1 (ADDR_EXPR, TYPE_POINTER_TO (TREE_TYPE (vtbl)), vtbl); | |
8d08fdba MS |
713 | decl = convert_pointer_to_real (vtype_binfo, decl); |
714 | vtbl_ptr = build_vfield_ref (build_indirect_ref (decl, NULL_PTR), vtype); | |
715 | if (vtbl_ptr == error_mark_node) | |
8926095f | 716 | return; |
8d08fdba MS |
717 | |
718 | /* Have to convert VTBL since array sizes may be different. */ | |
7177d104 MS |
719 | vtbl = convert_force (TREE_TYPE (vtbl_ptr), vtbl); |
720 | expand_expr_stmt (build_modify_expr (vtbl_ptr, NOP_EXPR, vtbl)); | |
8d08fdba MS |
721 | } |
722 | ||
723 | /* Subroutine of `expand_aggr_vbase_init'. | |
724 | BINFO is the binfo of the type that is being initialized. | |
725 | INIT_LIST is the list of initializers for the virtual baseclass. */ | |
726 | static void | |
727 | expand_aggr_vbase_init_1 (binfo, exp, addr, init_list) | |
728 | tree binfo, exp, addr, init_list; | |
729 | { | |
730 | tree init = value_member (BINFO_TYPE (binfo), init_list); | |
731 | tree ref = build_indirect_ref (addr, NULL_PTR); | |
732 | if (init) | |
733 | init = TREE_PURPOSE (init); | |
734 | /* Call constructors, but don't set up vtables. */ | |
735 | expand_aggr_init_1 (binfo, exp, ref, init, 0, | |
736 | LOOKUP_COMPLAIN|LOOKUP_SPECULATIVELY); | |
737 | CLEAR_BINFO_VBASE_INIT_MARKED (binfo); | |
738 | } | |
739 | ||
740 | /* Initialize this object's virtual base class pointers. This must be | |
741 | done only at the top-level of the object being constructed. | |
742 | ||
743 | INIT_LIST is list of initialization for constructor to perform. */ | |
744 | static void | |
745 | expand_aggr_vbase_init (binfo, exp, addr, init_list) | |
746 | tree binfo; | |
747 | tree exp; | |
748 | tree addr; | |
749 | tree init_list; | |
750 | { | |
751 | tree type = BINFO_TYPE (binfo); | |
752 | ||
753 | if (TYPE_USES_VIRTUAL_BASECLASSES (type)) | |
754 | { | |
755 | tree result = init_vbase_pointers (type, addr); | |
756 | tree vbases; | |
757 | ||
758 | if (result) | |
759 | expand_expr_stmt (build_compound_expr (result)); | |
760 | ||
761 | /* Mark everything as having an initializer | |
762 | (either explicit or default). */ | |
763 | for (vbases = CLASSTYPE_VBASECLASSES (type); | |
764 | vbases; vbases = TREE_CHAIN (vbases)) | |
765 | SET_BINFO_VBASE_INIT_MARKED (vbases); | |
766 | ||
767 | /* First, initialize baseclasses which could be baseclasses | |
768 | for other virtual baseclasses. */ | |
769 | for (vbases = CLASSTYPE_VBASECLASSES (type); | |
770 | vbases; vbases = TREE_CHAIN (vbases)) | |
771 | /* Don't initialize twice. */ | |
772 | if (BINFO_VBASE_INIT_MARKED (vbases)) | |
773 | { | |
774 | tree tmp = result; | |
775 | ||
776 | while (BINFO_TYPE (vbases) != BINFO_TYPE (TREE_PURPOSE (tmp))) | |
777 | tmp = TREE_CHAIN (tmp); | |
778 | expand_aggr_vbase_init_1 (vbases, exp, | |
779 | TREE_OPERAND (TREE_VALUE (tmp), 0), | |
780 | init_list); | |
781 | } | |
782 | ||
783 | /* Now initialize the baseclasses which don't have virtual baseclasses. */ | |
784 | for (; result; result = TREE_CHAIN (result)) | |
785 | /* Don't initialize twice. */ | |
786 | if (BINFO_VBASE_INIT_MARKED (TREE_PURPOSE (result))) | |
787 | { | |
788 | my_friendly_abort (47); | |
789 | expand_aggr_vbase_init_1 (TREE_PURPOSE (result), exp, | |
790 | TREE_OPERAND (TREE_VALUE (result), 0), | |
791 | init_list); | |
792 | } | |
793 | } | |
794 | } | |
795 | ||
796 | /* Subroutine to perform parser actions for member initialization. | |
797 | S_ID is the scoped identifier. | |
798 | NAME is the name of the member. | |
799 | INIT is the initializer, or `void_type_node' if none. */ | |
800 | void | |
801 | do_member_init (s_id, name, init) | |
802 | tree s_id, name, init; | |
803 | { | |
804 | tree binfo, base; | |
805 | ||
806 | if (current_class_type == NULL_TREE | |
807 | || ! is_aggr_typedef (s_id, 1)) | |
808 | return; | |
809 | binfo = get_binfo (IDENTIFIER_TYPE_VALUE (s_id), | |
810 | current_class_type, 1); | |
811 | if (binfo == error_mark_node) | |
812 | return; | |
813 | if (binfo == 0) | |
814 | { | |
815 | error_not_base_type (IDENTIFIER_TYPE_VALUE (s_id), current_class_type); | |
816 | return; | |
817 | } | |
818 | ||
819 | base = convert_pointer_to (binfo, current_class_decl); | |
820 | expand_member_init (build_indirect_ref (base, NULL_PTR), name, init); | |
821 | } | |
822 | ||
823 | /* Function to give error message if member initialization specification | |
824 | is erroneous. FIELD is the member we decided to initialize. | |
825 | TYPE is the type for which the initialization is being performed. | |
826 | FIELD must be a member of TYPE, or the base type from which FIELD | |
827 | comes must not need a constructor. | |
828 | ||
829 | MEMBER_NAME is the name of the member. */ | |
830 | ||
831 | static int | |
832 | member_init_ok_or_else (field, type, member_name) | |
833 | tree field; | |
834 | tree type; | |
835 | char *member_name; | |
836 | { | |
837 | if (field == error_mark_node) | |
838 | return 0; | |
839 | if (field == NULL_TREE) | |
840 | { | |
841 | cp_error ("class `%T' does not have any field named `%s'", type, | |
842 | member_name); | |
843 | return 0; | |
844 | } | |
845 | if (DECL_CONTEXT (field) != type | |
846 | && TYPE_NEEDS_CONSTRUCTING (DECL_CONTEXT (field))) | |
847 | { | |
848 | cp_error ("member `%D' comes from base class needing constructor", | |
849 | field); | |
850 | return 0; | |
851 | } | |
852 | return 1; | |
853 | } | |
854 | ||
855 | /* If NAME is a viable field name for the aggregate DECL, | |
856 | and PARMS is a viable parameter list, then expand an _EXPR | |
857 | which describes this initialization. | |
858 | ||
859 | Note that we do not need to chase through the class's base classes | |
860 | to look for NAME, because if it's in that list, it will be handled | |
861 | by the constructor for that base class. | |
862 | ||
863 | We do not yet have a fixed-point finder to instantiate types | |
864 | being fed to overloaded constructors. If there is a unique | |
865 | constructor, then argument types can be got from that one. | |
866 | ||
867 | If INIT is non-NULL, then it the initialization should | |
868 | be placed in `current_base_init_list', where it will be processed | |
869 | by `emit_base_init'. */ | |
870 | void | |
871 | expand_member_init (exp, name, init) | |
872 | tree exp, name, init; | |
873 | { | |
874 | extern tree ptr_type_node; /* should be in tree.h */ | |
875 | ||
876 | tree basetype = NULL_TREE, field; | |
877 | tree parm; | |
878 | tree rval, type; | |
879 | tree actual_name; | |
880 | ||
881 | if (exp == NULL_TREE) | |
882 | return; /* complain about this later */ | |
883 | ||
884 | type = TYPE_MAIN_VARIANT (TREE_TYPE (exp)); | |
885 | ||
886 | if (name == NULL_TREE && IS_AGGR_TYPE (type)) | |
887 | switch (CLASSTYPE_N_BASECLASSES (type)) | |
888 | { | |
889 | case 0: | |
890 | error ("base class initializer specified, but no base class to initialize"); | |
891 | return; | |
892 | case 1: | |
893 | basetype = TYPE_BINFO_BASETYPE (type, 0); | |
894 | break; | |
895 | default: | |
896 | error ("initializer for unnamed base class ambiguous"); | |
897 | cp_error ("(type `%T' uses multiple inheritance)", type); | |
898 | return; | |
899 | } | |
900 | ||
901 | if (init) | |
902 | { | |
903 | /* The grammar should not allow fields which have names | |
904 | that are TYPENAMEs. Therefore, if the field has | |
905 | a non-NULL TREE_TYPE, we may assume that this is an | |
906 | attempt to initialize a base class member of the current | |
907 | type. Otherwise, it is an attempt to initialize a | |
908 | member field. */ | |
909 | ||
910 | if (init == void_type_node) | |
911 | init = NULL_TREE; | |
912 | ||
913 | if (name == NULL_TREE || IDENTIFIER_HAS_TYPE_VALUE (name)) | |
914 | { | |
915 | tree base_init; | |
916 | ||
917 | if (name == NULL_TREE) | |
918 | { | |
919 | /* | |
920 | if (basetype) | |
921 | name = TYPE_IDENTIFIER (basetype); | |
922 | else | |
923 | { | |
924 | error ("no base class to initialize"); | |
925 | return; | |
926 | } | |
927 | */ | |
928 | } | |
929 | else | |
930 | { | |
931 | basetype = IDENTIFIER_TYPE_VALUE (name); | |
932 | if (basetype != type | |
933 | && ! binfo_member (basetype, TYPE_BINFO (type)) | |
934 | && ! binfo_member (basetype, CLASSTYPE_VBASECLASSES (type))) | |
935 | { | |
936 | if (IDENTIFIER_CLASS_VALUE (name)) | |
937 | goto try_member; | |
938 | if (TYPE_USES_VIRTUAL_BASECLASSES (type)) | |
939 | error ("type `%s' is not an immediate or virtual basetype for `%s'", | |
940 | IDENTIFIER_POINTER (name), | |
941 | TYPE_NAME_STRING (type)); | |
942 | else | |
943 | error ("type `%s' is not an immediate basetype for `%s'", | |
944 | IDENTIFIER_POINTER (name), | |
945 | TYPE_NAME_STRING (type)); | |
946 | return; | |
947 | } | |
948 | } | |
949 | ||
950 | if (purpose_member (name, current_base_init_list)) | |
951 | { | |
952 | error ("base class `%s' already initialized", | |
953 | IDENTIFIER_POINTER (name)); | |
954 | return; | |
955 | } | |
956 | ||
957 | base_init = build_tree_list (name, init); | |
958 | TREE_TYPE (base_init) = basetype; | |
959 | current_base_init_list = chainon (current_base_init_list, base_init); | |
960 | } | |
961 | else | |
962 | { | |
963 | tree member_init; | |
964 | ||
965 | try_member: | |
966 | field = lookup_field (type, name, 1, 0); | |
967 | ||
968 | if (! member_init_ok_or_else (field, type, IDENTIFIER_POINTER (name))) | |
969 | return; | |
970 | ||
971 | if (purpose_member (name, current_member_init_list)) | |
972 | { | |
973 | error ("field `%s' already initialized", IDENTIFIER_POINTER (name)); | |
974 | return; | |
975 | } | |
976 | ||
977 | member_init = build_tree_list (name, init); | |
978 | TREE_TYPE (member_init) = TREE_TYPE (field); | |
979 | current_member_init_list = chainon (current_member_init_list, member_init); | |
980 | } | |
981 | return; | |
982 | } | |
983 | else if (name == NULL_TREE) | |
984 | { | |
985 | compiler_error ("expand_member_init: name == NULL_TREE"); | |
986 | return; | |
987 | } | |
988 | ||
989 | basetype = type; | |
990 | field = lookup_field (basetype, name, 0, 0); | |
991 | ||
992 | if (! member_init_ok_or_else (field, basetype, IDENTIFIER_POINTER (name))) | |
993 | return; | |
994 | ||
995 | /* now see if there is a constructor for this type | |
996 | which will take these args. */ | |
997 | ||
998 | if (TYPE_HAS_CONSTRUCTOR (TREE_TYPE (field))) | |
999 | { | |
1000 | tree parmtypes, fndecl; | |
1001 | ||
1002 | if (TREE_CODE (exp) == VAR_DECL || TREE_CODE (exp) == PARM_DECL) | |
1003 | { | |
1004 | /* just know that we've seen something for this node */ | |
1005 | DECL_INITIAL (exp) = error_mark_node; | |
1006 | TREE_USED (exp) = 1; | |
1007 | } | |
1008 | type = TYPE_MAIN_VARIANT (TREE_TYPE (field)); | |
1009 | actual_name = TYPE_IDENTIFIER (type); | |
1010 | parm = build_component_ref (exp, name, 0, 0); | |
1011 | ||
1012 | /* Now get to the constructor. */ | |
1013 | fndecl = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type), 0); | |
1014 | /* Get past destructor, if any. */ | |
1015 | if (TYPE_HAS_DESTRUCTOR (type)) | |
1016 | fndecl = DECL_CHAIN (fndecl); | |
1017 | ||
1018 | if (fndecl) | |
1019 | my_friendly_assert (TREE_CODE (fndecl) == FUNCTION_DECL, 209); | |
1020 | ||
1021 | /* If the field is unique, we can use the parameter | |
1022 | types to guide possible type instantiation. */ | |
1023 | if (DECL_CHAIN (fndecl) == NULL_TREE) | |
1024 | { | |
1025 | /* There was a confusion here between | |
1026 | FIELD and FNDECL. The following code | |
1027 | should be correct, but abort is here | |
1028 | to make sure. */ | |
1029 | my_friendly_abort (48); | |
1030 | parmtypes = FUNCTION_ARG_CHAIN (fndecl); | |
1031 | } | |
1032 | else | |
1033 | { | |
1034 | parmtypes = NULL_TREE; | |
1035 | fndecl = NULL_TREE; | |
1036 | } | |
1037 | ||
1038 | init = convert_arguments (parm, parmtypes, NULL_TREE, fndecl, LOOKUP_NORMAL); | |
1039 | if (init == NULL_TREE || TREE_TYPE (init) != error_mark_node) | |
1040 | rval = build_method_call (NULL_TREE, actual_name, init, NULL_TREE, LOOKUP_NORMAL); | |
1041 | else | |
1042 | return; | |
1043 | ||
1044 | if (rval != error_mark_node) | |
1045 | { | |
1046 | /* Now, fill in the first parm with our guy */ | |
1047 | TREE_VALUE (TREE_OPERAND (rval, 1)) | |
1048 | = build_unary_op (ADDR_EXPR, parm, 0); | |
1049 | TREE_TYPE (rval) = ptr_type_node; | |
1050 | TREE_SIDE_EFFECTS (rval) = 1; | |
1051 | } | |
1052 | } | |
1053 | else if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (field))) | |
1054 | { | |
1055 | parm = build_component_ref (exp, name, 0, 0); | |
1056 | expand_aggr_init (parm, NULL_TREE, 0); | |
1057 | rval = error_mark_node; | |
1058 | } | |
1059 | ||
1060 | /* Now initialize the member. It does not have to | |
1061 | be of aggregate type to receive initialization. */ | |
1062 | if (rval != error_mark_node) | |
1063 | expand_expr_stmt (rval); | |
1064 | } | |
1065 | ||
1066 | /* This is like `expand_member_init', only it stores one aggregate | |
1067 | value into another. | |
1068 | ||
1069 | INIT comes in two flavors: it is either a value which | |
1070 | is to be stored in EXP, or it is a parameter list | |
1071 | to go to a constructor, which will operate on EXP. | |
1072 | If `init' is a CONSTRUCTOR, then we emit a warning message, | |
1073 | explaining that such initializations are illegal. | |
1074 | ||
1075 | ALIAS_THIS is nonzero iff we are initializing something which is | |
1076 | essentially an alias for C_C_D. In this case, the base constructor | |
1077 | may move it on us, and we must keep track of such deviations. | |
1078 | ||
1079 | If INIT resolves to a CALL_EXPR which happens to return | |
1080 | something of the type we are looking for, then we know | |
1081 | that we can safely use that call to perform the | |
1082 | initialization. | |
1083 | ||
1084 | The virtual function table pointer cannot be set up here, because | |
1085 | we do not really know its type. | |
1086 | ||
1087 | Virtual baseclass pointers are also set up here. | |
1088 | ||
1089 | This never calls operator=(). | |
1090 | ||
1091 | When initializing, nothing is CONST. | |
1092 | ||
1093 | A default copy constructor may have to be used to perform the | |
1094 | initialization. | |
1095 | ||
1096 | A constructor or a conversion operator may have to be used to | |
1097 | perform the initialization, but not both, as it would be ambiguous. | |
1098 | */ | |
1099 | ||
1100 | void | |
1101 | expand_aggr_init (exp, init, alias_this) | |
1102 | tree exp, init; | |
1103 | int alias_this; | |
1104 | { | |
1105 | tree type = TREE_TYPE (exp); | |
1106 | int was_const = TREE_READONLY (exp); | |
1107 | ||
1108 | if (init == error_mark_node) | |
1109 | return; | |
1110 | ||
1111 | TREE_READONLY (exp) = 0; | |
1112 | ||
1113 | if (TREE_CODE (type) == ARRAY_TYPE) | |
1114 | { | |
1115 | /* Must arrange to initialize each element of EXP | |
1116 | from elements of INIT. */ | |
1117 | int was_const_elts = TYPE_READONLY (TREE_TYPE (type)); | |
1118 | tree itype = init ? TREE_TYPE (init) : NULL_TREE; | |
1119 | if (was_const_elts) | |
1120 | { | |
1121 | tree atype = build_cplus_array_type (TYPE_MAIN_VARIANT (TREE_TYPE (type)), | |
1122 | TYPE_DOMAIN (type)); | |
1123 | if (init && (TREE_TYPE (exp) == TREE_TYPE (init))) | |
1124 | TREE_TYPE (init) = atype; | |
1125 | TREE_TYPE (exp) = atype; | |
1126 | } | |
1127 | if (init && TREE_TYPE (init) == NULL_TREE) | |
1128 | { | |
1129 | /* Handle bad initializers like: | |
1130 | class COMPLEX { | |
1131 | public: | |
1132 | double re, im; | |
1133 | COMPLEX(double r = 0.0, double i = 0.0) {re = r; im = i;}; | |
1134 | ~COMPLEX() {}; | |
1135 | }; | |
1136 | ||
1137 | int main(int argc, char **argv) { | |
1138 | COMPLEX zees(1.0, 0.0)[10]; | |
1139 | } | |
1140 | */ | |
1141 | error ("bad array initializer"); | |
1142 | return; | |
1143 | } | |
1144 | expand_vec_init (exp, exp, array_type_nelts (type), init, | |
1145 | init && comptypes (TREE_TYPE (init), TREE_TYPE (exp), 1)); | |
1146 | TREE_READONLY (exp) = was_const; | |
1147 | TREE_TYPE (exp) = type; | |
1148 | if (init) TREE_TYPE (init) = itype; | |
1149 | return; | |
1150 | } | |
1151 | ||
1152 | if (TREE_CODE (exp) == VAR_DECL || TREE_CODE (exp) == PARM_DECL) | |
1153 | /* just know that we've seen something for this node */ | |
1154 | TREE_USED (exp) = 1; | |
1155 | ||
1156 | #if 0 | |
1157 | /* If initializing from a GNU C CONSTRUCTOR, consider the elts in the | |
1158 | constructor as parameters to an implicit GNU C++ constructor. */ | |
1159 | if (init && TREE_CODE (init) == CONSTRUCTOR | |
1160 | && TYPE_HAS_CONSTRUCTOR (type) | |
1161 | && TREE_TYPE (init) == type) | |
1162 | init = CONSTRUCTOR_ELTS (init); | |
1163 | #endif | |
1164 | expand_aggr_init_1 (TYPE_BINFO (type), exp, exp, | |
1165 | init, alias_this, LOOKUP_NORMAL); | |
1166 | TREE_READONLY (exp) = was_const; | |
1167 | } | |
1168 | ||
1169 | static void | |
1170 | expand_default_init (binfo, true_exp, exp, type, init, alias_this, flags) | |
1171 | tree binfo; | |
1172 | tree true_exp, exp; | |
1173 | tree type; | |
1174 | tree init; | |
1175 | int alias_this; | |
1176 | int flags; | |
1177 | { | |
1178 | /* It fails because there may not be a constructor which takes | |
1179 | its own type as the first (or only parameter), but which does | |
1180 | take other types via a conversion. So, if the thing initializing | |
1181 | the expression is a unit element of type X, first try X(X&), | |
1182 | followed by initialization by X. If neither of these work | |
1183 | out, then look hard. */ | |
1184 | tree rval; | |
1185 | tree parms; | |
1186 | int xxref_init_possible; | |
1187 | ||
1188 | if (init == NULL_TREE || TREE_CODE (init) == TREE_LIST) | |
1189 | { | |
1190 | parms = init; | |
1191 | if (parms) init = TREE_VALUE (parms); | |
1192 | } | |
1193 | else if (TREE_CODE (init) == INDIRECT_REF && TREE_HAS_CONSTRUCTOR (init)) | |
1194 | { | |
1195 | rval = convert_for_initialization (exp, type, init, 0, 0, 0, 0); | |
1196 | expand_expr_stmt (rval); | |
1197 | return; | |
1198 | } | |
1199 | else | |
1200 | parms = build_tree_list (NULL_TREE, init); | |
1201 | ||
1202 | if (TYPE_HAS_INIT_REF (type) | |
1203 | || init == NULL_TREE | |
1204 | || TREE_CHAIN (parms) != NULL_TREE) | |
1205 | xxref_init_possible = 0; | |
1206 | else | |
1207 | { | |
1208 | xxref_init_possible = LOOKUP_SPECULATIVELY; | |
1209 | flags &= ~LOOKUP_COMPLAIN; | |
1210 | } | |
1211 | ||
1212 | if (TYPE_USES_VIRTUAL_BASECLASSES (type)) | |
1213 | { | |
1214 | if (true_exp == exp) | |
1215 | parms = tree_cons (NULL_TREE, integer_one_node, parms); | |
1216 | else | |
1217 | parms = tree_cons (NULL_TREE, integer_zero_node, parms); | |
1218 | flags |= LOOKUP_HAS_IN_CHARGE; | |
1219 | } | |
1220 | ||
1221 | rval = build_method_call (exp, constructor_name_full (type), | |
1222 | parms, binfo, flags|xxref_init_possible); | |
1223 | if (rval == NULL_TREE && xxref_init_possible) | |
1224 | { | |
1225 | /* It is an error to implement a default copy constructor if | |
1226 | (see ARM 12.8 for details) ... one case being if another | |
1227 | copy constructor already exists. */ | |
1228 | tree init_type = TREE_TYPE (init); | |
1229 | if (TREE_CODE (init_type) == REFERENCE_TYPE) | |
1230 | init_type = TREE_TYPE (init_type); | |
1231 | if (TYPE_MAIN_VARIANT (init_type) == TYPE_MAIN_VARIANT (type) | |
1232 | || (IS_AGGR_TYPE (init_type) | |
1233 | && UNIQUELY_DERIVED_FROM_P (type, init_type))) | |
1234 | { | |
1235 | if (type == BINFO_TYPE (binfo) | |
1236 | && TYPE_USES_VIRTUAL_BASECLASSES (type)) | |
1237 | { | |
1238 | tree addr = build_unary_op (ADDR_EXPR, exp, 0); | |
1239 | expand_aggr_vbase_init (binfo, exp, addr, NULL_TREE); | |
1240 | ||
7177d104 | 1241 | expand_indirect_vtbls_init (binfo, exp, addr, 1); |
8d08fdba MS |
1242 | } |
1243 | expand_expr_stmt (build_modify_expr (exp, INIT_EXPR, init)); | |
1244 | return; | |
1245 | } | |
1246 | else | |
1247 | rval = build_method_call (exp, constructor_name_full (type), parms, | |
1248 | binfo, flags); | |
1249 | } | |
1250 | ||
1251 | /* Private, protected, or otherwise unavailable. */ | |
1252 | if (rval == error_mark_node && (flags&LOOKUP_COMPLAIN)) | |
1253 | cp_error ("in base initialization for class `%T'", binfo); | |
1254 | /* A valid initialization using constructor. */ | |
1255 | else if (rval != error_mark_node && rval != NULL_TREE) | |
1256 | { | |
1257 | /* p. 222: if the base class assigns to `this', then that | |
1258 | value is used in the derived class. */ | |
1259 | if ((flag_this_is_variable & 1) && alias_this) | |
1260 | { | |
1261 | TREE_TYPE (rval) = TREE_TYPE (current_class_decl); | |
1262 | expand_assignment (current_class_decl, rval, 0, 0); | |
1263 | } | |
1264 | else | |
1265 | expand_expr_stmt (rval); | |
1266 | } | |
1267 | else if (parms && TREE_CHAIN (parms) == NULL_TREE) | |
1268 | { | |
1269 | /* If we are initializing one aggregate value | |
1270 | from another, and though there are constructors, | |
1271 | and none accept the initializer, just do a bitwise | |
1272 | copy. | |
1273 | ||
1274 | The above sounds wrong, ``If a class has any copy | |
1275 | constructor defined, the default copy constructor will | |
1276 | not be generated.'' 12.8 Copying Class Objects (mrs) | |
1277 | ||
1278 | @@ This should reject initializer which a constructor | |
1279 | @@ rejected on access gounds, but there is | |
1280 | @@ no way right now to recognize that case with | |
1281 | @@ just `error_mark_node'. */ | |
1282 | tree itype; | |
1283 | init = TREE_VALUE (parms); | |
1284 | itype = TREE_TYPE (init); | |
1285 | if (TREE_CODE (itype) == REFERENCE_TYPE) | |
1286 | { | |
1287 | init = convert_from_reference (init); | |
1288 | itype = TREE_TYPE (init); | |
1289 | } | |
1290 | itype = TYPE_MAIN_VARIANT (itype); | |
1291 | ||
1292 | /* This is currently how the default X(X&) constructor | |
1293 | is implemented. */ | |
1294 | if (comptypes (TYPE_MAIN_VARIANT (type), itype, 0)) | |
1295 | { | |
1296 | #if 0 | |
1297 | warning ("bitwise copy in initialization of type `%s'", | |
1298 | TYPE_NAME_STRING (type)); | |
1299 | #endif | |
1300 | rval = build (INIT_EXPR, type, exp, init); | |
1301 | expand_expr_stmt (rval); | |
1302 | } | |
1303 | else | |
1304 | { | |
1305 | cp_error ("in base initialization for class `%T',", binfo); | |
1306 | cp_error ("invalid initializer to constructor for type `%T'", type); | |
1307 | return; | |
1308 | } | |
1309 | } | |
1310 | else | |
1311 | { | |
1312 | if (init == NULL_TREE) | |
1313 | my_friendly_assert (parms == NULL_TREE, 210); | |
1314 | if (parms == NULL_TREE && TREE_VIA_VIRTUAL (binfo)) | |
1315 | cp_error ("virtual baseclass `%T' does not have default initializer", binfo); | |
1316 | else | |
1317 | { | |
1318 | cp_error ("in base initialization for class `%T',", binfo); | |
1319 | /* This will make an error message for us. */ | |
1320 | build_method_call (exp, constructor_name_full (type), parms, binfo, | |
1321 | (TYPE_USES_VIRTUAL_BASECLASSES (type) | |
1322 | ? LOOKUP_NORMAL|LOOKUP_HAS_IN_CHARGE | |
1323 | : LOOKUP_NORMAL)); | |
1324 | } | |
1325 | return; | |
1326 | } | |
1327 | /* Constructor has been called, but vtables may be for TYPE | |
1328 | rather than for FOR_TYPE. */ | |
1329 | } | |
1330 | ||
1331 | /* This function is responsible for initializing EXP with INIT | |
1332 | (if any). | |
1333 | ||
1334 | BINFO is the binfo of the type for who we are performing the | |
1335 | initialization. For example, if W is a virtual base class of A and B, | |
1336 | and C : A, B. | |
1337 | If we are initializing B, then W must contain B's W vtable, whereas | |
1338 | were we initializing C, W must contain C's W vtable. | |
1339 | ||
1340 | TRUE_EXP is nonzero if it is the true expression being initialized. | |
1341 | In this case, it may be EXP, or may just contain EXP. The reason we | |
1342 | need this is because if EXP is a base element of TRUE_EXP, we | |
1343 | don't necessarily know by looking at EXP where its virtual | |
1344 | baseclass fields should really be pointing. But we do know | |
1345 | from TRUE_EXP. In constructors, we don't know anything about | |
1346 | the value being initialized. | |
1347 | ||
1348 | ALIAS_THIS serves the same purpose it serves for expand_aggr_init. | |
1349 | ||
1350 | FLAGS is just passes to `build_method_call'. See that function for | |
1351 | its description. */ | |
1352 | ||
1353 | static void | |
1354 | expand_aggr_init_1 (binfo, true_exp, exp, init, alias_this, flags) | |
1355 | tree binfo; | |
1356 | tree true_exp, exp; | |
1357 | tree init; | |
1358 | int alias_this; | |
1359 | int flags; | |
1360 | { | |
1361 | tree type = TREE_TYPE (exp); | |
1362 | tree init_type = NULL_TREE; | |
8d08fdba MS |
1363 | |
1364 | my_friendly_assert (init != error_mark_node && type != error_mark_node, 211); | |
1365 | ||
1366 | /* Use a function returning the desired type to initialize EXP for us. | |
1367 | If the function is a constructor, and its first argument is | |
1368 | NULL_TREE, know that it was meant for us--just slide exp on | |
1369 | in and expand the constructor. Constructors now come | |
1370 | as TARGET_EXPRs. */ | |
1371 | if (init) | |
1372 | { | |
1373 | tree init_list = NULL_TREE; | |
1374 | ||
1375 | if (TREE_CODE (init) == TREE_LIST) | |
1376 | { | |
1377 | init_list = init; | |
1378 | if (TREE_CHAIN (init) == NULL_TREE) | |
1379 | init = TREE_VALUE (init); | |
1380 | } | |
1381 | ||
1382 | init_type = TREE_TYPE (init); | |
1383 | ||
1384 | if (TREE_CODE (init) != TREE_LIST) | |
1385 | { | |
1386 | if (TREE_CODE (init_type) == ERROR_MARK) | |
1387 | return; | |
1388 | ||
1389 | #if 0 | |
1390 | /* These lines are found troublesome 5/11/89. */ | |
1391 | if (TREE_CODE (init_type) == REFERENCE_TYPE) | |
1392 | init_type = TREE_TYPE (init_type); | |
1393 | #endif | |
1394 | ||
1395 | /* This happens when we use C++'s functional cast notation. | |
1396 | If the types match, then just use the TARGET_EXPR | |
1397 | directly. Otherwise, we need to create the initializer | |
1398 | separately from the object being initialized. */ | |
1399 | if (TREE_CODE (init) == TARGET_EXPR) | |
1400 | { | |
1401 | if (init_type == type) | |
1402 | { | |
1403 | if (TREE_CODE (exp) == VAR_DECL | |
1404 | || TREE_CODE (exp) == RESULT_DECL) | |
1405 | /* Unify the initialization targets. */ | |
1406 | DECL_RTL (TREE_OPERAND (init, 0)) = DECL_RTL (exp); | |
1407 | else | |
1408 | DECL_RTL (TREE_OPERAND (init, 0)) = expand_expr (exp, NULL_RTX, 0, 0); | |
1409 | ||
1410 | expand_expr_stmt (init); | |
1411 | return; | |
1412 | } | |
1413 | else | |
1414 | { | |
1415 | init = TREE_OPERAND (init, 1); | |
1416 | init = build (CALL_EXPR, init_type, | |
1417 | TREE_OPERAND (init, 0), TREE_OPERAND (init, 1), 0); | |
1418 | TREE_SIDE_EFFECTS (init) = 1; | |
1419 | #if 0 | |
1420 | TREE_RAISES (init) = ?? | |
1421 | #endif | |
1422 | if (init_list) | |
1423 | TREE_VALUE (init_list) = init; | |
1424 | } | |
1425 | } | |
1426 | ||
1427 | if (init_type == type && TREE_CODE (init) == CALL_EXPR | |
1428 | #if 0 | |
1429 | /* It is legal to directly initialize from a CALL_EXPR | |
1430 | without going through X(X&), apparently. */ | |
1431 | && ! TYPE_GETS_INIT_REF (type) | |
1432 | #endif | |
1433 | ) | |
1434 | { | |
1435 | /* A CALL_EXPR is a legitimate form of initialization, so | |
1436 | we should not print this warning message. */ | |
1437 | #if 0 | |
1438 | /* Should have gone away due to 5/11/89 change. */ | |
1439 | if (TREE_CODE (TREE_TYPE (init)) == REFERENCE_TYPE) | |
1440 | init = convert_from_reference (init); | |
1441 | #endif | |
1442 | expand_assignment (exp, init, 0, 0); | |
1443 | if (exp == DECL_RESULT (current_function_decl)) | |
1444 | { | |
1445 | /* Failing this assertion means that the return value | |
1446 | from receives multiple initializations. */ | |
1447 | my_friendly_assert (DECL_INITIAL (exp) == NULL_TREE | |
1448 | || DECL_INITIAL (exp) == error_mark_node, | |
1449 | 212); | |
1450 | DECL_INITIAL (exp) = init; | |
1451 | } | |
1452 | return; | |
1453 | } | |
1454 | else if (init_type == type | |
1455 | && TREE_CODE (init) == COND_EXPR) | |
1456 | { | |
1457 | /* Push value to be initialized into the cond, where possible. | |
1458 | Avoid spurious warning messages when initializing the | |
1459 | result of this function. */ | |
1460 | TREE_OPERAND (init, 1) | |
1461 | = build_modify_expr (exp, INIT_EXPR, TREE_OPERAND (init, 1)); | |
1462 | if (exp == DECL_RESULT (current_function_decl)) | |
1463 | DECL_INITIAL (exp) = NULL_TREE; | |
1464 | TREE_OPERAND (init, 2) | |
1465 | = build_modify_expr (exp, INIT_EXPR, TREE_OPERAND (init, 2)); | |
1466 | if (exp == DECL_RESULT (current_function_decl)) | |
1467 | DECL_INITIAL (exp) = init; | |
1468 | TREE_SIDE_EFFECTS (init) = 1; | |
1469 | expand_expr (init, const0_rtx, VOIDmode, 0); | |
1470 | free_temp_slots (); | |
1471 | return; | |
1472 | } | |
1473 | } | |
1474 | ||
1475 | /* We did not know what we were initializing before. Now we do. */ | |
1476 | if (TREE_CODE (init) == TARGET_EXPR) | |
1477 | { | |
1478 | tree tmp = TREE_OPERAND (TREE_OPERAND (init, 1), 1); | |
1479 | ||
1480 | if (TREE_CODE (TREE_VALUE (tmp)) == NOP_EXPR | |
1481 | && TREE_OPERAND (TREE_VALUE (tmp), 0) == integer_zero_node) | |
1482 | { | |
1483 | /* In order for this to work for RESULT_DECLs, if their | |
1484 | type has a constructor, then they must be BLKmode | |
1485 | so that they will be meaningfully addressable. */ | |
1486 | tree arg = build_unary_op (ADDR_EXPR, exp, 0); | |
1487 | init = TREE_OPERAND (init, 1); | |
1488 | init = build (CALL_EXPR, build_pointer_type (TREE_TYPE (init)), | |
1489 | TREE_OPERAND (init, 0), TREE_OPERAND (init, 1), 0); | |
1490 | TREE_SIDE_EFFECTS (init) = 1; | |
1491 | #if 0 | |
1492 | TREE_RAISES (init) = ?? | |
1493 | #endif | |
1494 | TREE_VALUE (TREE_OPERAND (init, 1)) | |
1495 | = convert_pointer_to (TREE_TYPE (TREE_TYPE (TREE_VALUE (tmp))), arg); | |
1496 | ||
1497 | if (alias_this) | |
1498 | { | |
1499 | expand_assignment (current_function_decl, init, 0, 0); | |
1500 | return; | |
1501 | } | |
1502 | if (exp == DECL_RESULT (current_function_decl)) | |
1503 | { | |
1504 | if (DECL_INITIAL (DECL_RESULT (current_function_decl))) | |
1505 | fatal ("return value from function receives multiple initializations"); | |
1506 | DECL_INITIAL (exp) = init; | |
1507 | } | |
1508 | expand_expr_stmt (init); | |
1509 | return; | |
1510 | } | |
1511 | } | |
1512 | ||
1513 | if (TREE_CODE (exp) == VAR_DECL | |
1514 | && TREE_CODE (init) == CONSTRUCTOR | |
1515 | && TREE_HAS_CONSTRUCTOR (init)) | |
1516 | { | |
1517 | tree t = store_init_value (exp, init); | |
1518 | if (!t) | |
1519 | { | |
1520 | expand_decl_init (exp); | |
1521 | return; | |
1522 | } | |
1523 | t = build (INIT_EXPR, type, exp, init); | |
1524 | TREE_SIDE_EFFECTS (t) = 1; | |
1525 | expand_expr_stmt (t); | |
1526 | return; | |
1527 | } | |
1528 | ||
1529 | /* Handle this case: when calling a constructor: xyzzy foo(bar); | |
1530 | which really means: xyzzy foo = bar; Ugh! | |
1531 | ||
1532 | More useful for this case: xyzzy *foo = new xyzzy (bar); */ | |
1533 | ||
1534 | if (! TYPE_NEEDS_CONSTRUCTING (type) && ! IS_AGGR_TYPE (type)) | |
1535 | { | |
1536 | if (init_list && TREE_CHAIN (init_list)) | |
1537 | { | |
1538 | warning ("initializer list being treated as compound expression"); | |
1539 | init = convert (type, build_compound_expr (init_list)); | |
1540 | if (init == error_mark_node) | |
1541 | return; | |
1542 | } | |
1543 | ||
1544 | expand_assignment (exp, init, 0, 0); | |
1545 | ||
1546 | return; | |
1547 | } | |
1548 | /* See whether we can go through a type conversion operator. | |
1549 | This wins over going through a non-existent constructor. If | |
1550 | there is a constructor, it is ambiguous. */ | |
1551 | if (TREE_CODE (init) != TREE_LIST) | |
1552 | { | |
1553 | tree ttype = TREE_CODE (init_type) == REFERENCE_TYPE | |
1554 | ? TREE_TYPE (init_type) : init_type; | |
1555 | ||
1556 | if (ttype != type && IS_AGGR_TYPE (ttype)) | |
1557 | { | |
1558 | tree rval = build_type_conversion (CONVERT_EXPR, type, init, 0); | |
1559 | ||
1560 | if (rval) | |
1561 | { | |
1562 | /* See if there is a constructor for``type'' that takes a | |
1563 | ``ttype''-typed object. */ | |
1564 | tree parms = build_tree_list (NULL_TREE, init); | |
1565 | tree as_cons = NULL_TREE; | |
1566 | if (TYPE_HAS_CONSTRUCTOR (type)) | |
1567 | as_cons = build_method_call (exp, constructor_name_full (type), | |
1568 | parms, binfo, | |
1569 | LOOKUP_SPECULATIVELY|LOOKUP_NO_CONVERSION); | |
1570 | if (as_cons != NULL_TREE && as_cons != error_mark_node) | |
1571 | /* ANSI C++ June 5 1992 WP 12.3.2.6.1 */ | |
1572 | cp_error ("ambiguity between conversion to `%T' and constructor", | |
1573 | type); | |
1574 | else | |
1575 | expand_assignment (exp, rval, 0, 0); | |
1576 | return; | |
1577 | } | |
1578 | } | |
1579 | } | |
1580 | } | |
1581 | ||
1582 | /* Handle default copy constructors here, does not matter if there is | |
1583 | a constructor or not. */ | |
1584 | if (type == init_type && IS_AGGR_TYPE (type) | |
1585 | && init && TREE_CODE (init) != TREE_LIST) | |
1586 | expand_default_init (binfo, true_exp, exp, type, init, alias_this, flags); | |
1587 | /* Not sure why this is here... */ | |
1588 | else if (TYPE_HAS_CONSTRUCTOR (type)) | |
1589 | expand_default_init (binfo, true_exp, exp, type, init, alias_this, flags); | |
1590 | else if (TREE_CODE (type) == ARRAY_TYPE) | |
1591 | { | |
1592 | if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (type))) | |
1593 | expand_vec_init (exp, exp, array_type_nelts (type), init, 0); | |
1594 | else if (TYPE_VIRTUAL_P (TREE_TYPE (type))) | |
1595 | sorry ("arrays of objects with virtual functions but no constructors"); | |
1596 | } | |
1597 | else | |
1598 | expand_recursive_init (binfo, true_exp, exp, init, | |
1599 | CLASSTYPE_BASE_INIT_LIST (type), alias_this); | |
1600 | } | |
1601 | ||
1602 | /* A pointer which holds the initializer. First call to | |
1603 | expand_aggr_init gets this value pointed to, and sets it to init_null. */ | |
1604 | static tree *init_ptr, init_null; | |
1605 | ||
1606 | /* Subroutine of expand_recursive_init: | |
1607 | ||
1608 | ADDR is the address of the expression being initialized. | |
1609 | INIT_LIST is the cons-list of initializations to be performed. | |
1610 | ALIAS_THIS is its same, lovable self. */ | |
1611 | static void | |
1612 | expand_recursive_init_1 (binfo, true_exp, addr, init_list, alias_this) | |
1613 | tree binfo, true_exp, addr; | |
1614 | tree init_list; | |
1615 | int alias_this; | |
1616 | { | |
1617 | while (init_list) | |
1618 | { | |
1619 | if (TREE_PURPOSE (init_list)) | |
1620 | { | |
1621 | if (TREE_CODE (TREE_PURPOSE (init_list)) == FIELD_DECL) | |
1622 | { | |
1623 | tree member = TREE_PURPOSE (init_list); | |
1624 | tree subexp = build_indirect_ref (convert_pointer_to (TREE_VALUE (init_list), addr), NULL_PTR); | |
1625 | tree member_base = build (COMPONENT_REF, TREE_TYPE (member), subexp, member); | |
1626 | if (IS_AGGR_TYPE (TREE_TYPE (member))) | |
1627 | expand_aggr_init (member_base, DECL_INITIAL (member), 0); | |
1628 | else if (TREE_CODE (TREE_TYPE (member)) == ARRAY_TYPE | |
1629 | && TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (member))) | |
1630 | { | |
1631 | member_base = save_expr (default_conversion (member_base)); | |
1632 | expand_vec_init (member, member_base, | |
1633 | array_type_nelts (TREE_TYPE (member)), | |
1634 | DECL_INITIAL (member), 0); | |
1635 | } | |
1636 | else | |
1637 | expand_expr_stmt (build_modify_expr (member_base, INIT_EXPR, DECL_INITIAL (member))); | |
1638 | } | |
1639 | else if (TREE_CODE (TREE_PURPOSE (init_list)) == TREE_LIST) | |
1640 | { | |
1641 | expand_recursive_init_1 (binfo, true_exp, addr, TREE_PURPOSE (init_list), alias_this); | |
1642 | expand_recursive_init_1 (binfo, true_exp, addr, TREE_VALUE (init_list), alias_this); | |
1643 | } | |
1644 | else if (TREE_CODE (TREE_PURPOSE (init_list)) == ERROR_MARK) | |
1645 | { | |
1646 | /* Only initialize the virtual function tables if we | |
1647 | are initializing the ultimate users of those vtables. */ | |
1648 | if (TREE_VALUE (init_list)) | |
1649 | { | |
7177d104 | 1650 | /* We have to ensure that the first argment to |
8926095f | 1651 | expand_virtual_init is in binfo's hierarchy. */ |
7177d104 MS |
1652 | /* Is it the case that this is exactly the right binfo? */ |
1653 | /* If it is ok, then fixup expand_virtual_init, to make | |
1654 | it much simpler. */ | |
1655 | expand_virtual_init (get_binfo (TREE_VALUE (init_list), binfo, 0), | |
8926095f | 1656 | addr); |
8d08fdba MS |
1657 | if (TREE_VALUE (init_list) == binfo |
1658 | && TYPE_USES_VIRTUAL_BASECLASSES (BINFO_TYPE (binfo))) | |
7177d104 | 1659 | expand_indirect_vtbls_init (binfo, true_exp, addr, 1); |
8d08fdba MS |
1660 | } |
1661 | } | |
1662 | else | |
1663 | my_friendly_abort (49); | |
1664 | } | |
1665 | else if (TREE_VALUE (init_list) | |
1666 | && TREE_CODE (TREE_VALUE (init_list)) == TREE_VEC) | |
1667 | { | |
1668 | tree subexp = build_indirect_ref (convert_pointer_to (TREE_VALUE (init_list), addr), NULL_PTR); | |
1669 | expand_aggr_init_1 (binfo, true_exp, subexp, *init_ptr, | |
1670 | alias_this && BINFO_OFFSET_ZEROP (TREE_VALUE (init_list)), | |
1671 | LOOKUP_COMPLAIN); | |
1672 | ||
1673 | /* INIT_PTR is used up. */ | |
1674 | init_ptr = &init_null; | |
1675 | } | |
1676 | else | |
1677 | my_friendly_abort (50); | |
1678 | init_list = TREE_CHAIN (init_list); | |
1679 | } | |
1680 | } | |
1681 | ||
1682 | /* Initialize EXP with INIT. Type EXP does not have a constructor, | |
1683 | but it has a baseclass with a constructor or a virtual function | |
1684 | table which needs initializing. | |
1685 | ||
1686 | INIT_LIST is a cons-list describing what parts of EXP actually | |
1687 | need to be initialized. INIT is given to the *unique*, first | |
1688 | constructor within INIT_LIST. If there are multiple first | |
1689 | constructors, such as with multiple inheritance, INIT must | |
1690 | be zero or an ambiguity error is reported. | |
1691 | ||
1692 | ALIAS_THIS is passed from `expand_aggr_init'. See comments | |
1693 | there. */ | |
1694 | ||
1695 | static void | |
1696 | expand_recursive_init (binfo, true_exp, exp, init, init_list, alias_this) | |
1697 | tree binfo, true_exp, exp, init; | |
1698 | tree init_list; | |
1699 | int alias_this; | |
1700 | { | |
1701 | tree *old_init_ptr = init_ptr; | |
1702 | tree addr = build_unary_op (ADDR_EXPR, exp, 0); | |
1703 | init_ptr = &init; | |
1704 | ||
1705 | if (true_exp == exp && TYPE_USES_VIRTUAL_BASECLASSES (BINFO_TYPE (binfo))) | |
1706 | { | |
1707 | expand_aggr_vbase_init (binfo, exp, addr, init_list); | |
7177d104 | 1708 | expand_indirect_vtbls_init (binfo, true_exp, addr, 1); |
8d08fdba MS |
1709 | } |
1710 | expand_recursive_init_1 (binfo, true_exp, addr, init_list, alias_this); | |
1711 | ||
1712 | if (*init_ptr) | |
1713 | { | |
1714 | tree type = TREE_TYPE (exp); | |
1715 | ||
1716 | if (TREE_CODE (type) == REFERENCE_TYPE) | |
1717 | type = TREE_TYPE (type); | |
1718 | if (IS_AGGR_TYPE (type)) | |
1719 | cp_error ("unexpected argument to constructor `%T'", type); | |
1720 | else | |
1721 | error ("unexpected argument to constructor"); | |
1722 | } | |
1723 | init_ptr = old_init_ptr; | |
1724 | } | |
1725 | ||
1726 | /* Report an error if NAME is not the name of a user-defined, | |
1727 | aggregate type. If OR_ELSE is nonzero, give an error message. */ | |
1728 | int | |
1729 | is_aggr_typedef (name, or_else) | |
1730 | tree name; | |
1731 | int or_else; | |
1732 | { | |
1733 | tree type; | |
1734 | ||
1735 | if (name == error_mark_node) | |
1736 | return 0; | |
1737 | ||
1738 | if (IDENTIFIER_HAS_TYPE_VALUE (name)) | |
1739 | type = IDENTIFIER_TYPE_VALUE (name); | |
8d08fdba MS |
1740 | else |
1741 | { | |
1742 | if (or_else) | |
a28e3c7f | 1743 | cp_error ("`%T' is not an aggregate typedef", name); |
8d08fdba MS |
1744 | return 0; |
1745 | } | |
1746 | ||
1747 | if (! IS_AGGR_TYPE (type) | |
1748 | && TREE_CODE (type) != TEMPLATE_TYPE_PARM) | |
1749 | { | |
1750 | if (or_else) | |
a28e3c7f | 1751 | cp_error ("`%T' is not an aggregate type", type); |
8d08fdba MS |
1752 | return 0; |
1753 | } | |
1754 | return 1; | |
1755 | } | |
1756 | ||
1757 | /* Like is_aggr_typedef, but returns typedef if successful. */ | |
1758 | tree | |
1759 | get_aggr_from_typedef (name, or_else) | |
1760 | tree name; | |
1761 | int or_else; | |
1762 | { | |
1763 | tree type; | |
1764 | ||
1765 | if (name == error_mark_node) | |
1766 | return NULL_TREE; | |
1767 | ||
1768 | if (IDENTIFIER_HAS_TYPE_VALUE (name)) | |
1769 | type = IDENTIFIER_TYPE_VALUE (name); | |
8d08fdba MS |
1770 | else |
1771 | { | |
1772 | if (or_else) | |
1773 | cp_error ("`%T' fails to be an aggregate typedef", name); | |
1774 | return NULL_TREE; | |
1775 | } | |
1776 | ||
1777 | if (! IS_AGGR_TYPE (type) | |
1778 | && TREE_CODE (type) != TEMPLATE_TYPE_PARM) | |
1779 | { | |
1780 | if (or_else) | |
1781 | cp_error ("type `%T' is of non-aggregate type", type); | |
1782 | return NULL_TREE; | |
1783 | } | |
1784 | return type; | |
1785 | } | |
1786 | ||
1787 | tree | |
1788 | get_type_value (name) | |
1789 | tree name; | |
1790 | { | |
8d08fdba MS |
1791 | if (name == error_mark_node) |
1792 | return NULL_TREE; | |
1793 | ||
1794 | if (IDENTIFIER_HAS_TYPE_VALUE (name)) | |
1795 | return IDENTIFIER_TYPE_VALUE (name); | |
8d08fdba MS |
1796 | else |
1797 | return NULL_TREE; | |
1798 | } | |
1799 | ||
1800 | \f | |
51c184be | 1801 | /* This code could just as well go in `class.c', but is placed here for |
8d08fdba MS |
1802 | modularity. */ |
1803 | ||
1804 | /* For an expression of the form CNAME :: NAME (PARMLIST), build | |
1805 | the appropriate function call. */ | |
1806 | tree | |
1807 | build_member_call (cname, name, parmlist) | |
1808 | tree cname, name, parmlist; | |
1809 | { | |
1810 | tree type, t; | |
1811 | tree method_name = name; | |
1812 | int dtor = 0; | |
1813 | int dont_use_this = 0; | |
1814 | tree basetype_path, decl; | |
1815 | ||
1816 | if (TREE_CODE (method_name) == BIT_NOT_EXPR) | |
1817 | { | |
1818 | method_name = TREE_OPERAND (method_name, 0); | |
1819 | dtor = 1; | |
1820 | } | |
1821 | ||
1822 | if (TREE_CODE (cname) == SCOPE_REF) | |
1823 | cname = resolve_scope_to_name (NULL_TREE, cname); | |
1824 | ||
1825 | if (cname == NULL_TREE || ! (type = get_aggr_from_typedef (cname, 1))) | |
1826 | return error_mark_node; | |
1827 | ||
1828 | /* An operator we did not like. */ | |
1829 | if (name == NULL_TREE) | |
1830 | return error_mark_node; | |
1831 | ||
1832 | if (dtor) | |
1833 | { | |
1834 | #if 0 | |
1835 | /* Everything can explicitly call a destructor; see 12.4 */ | |
1836 | if (! TYPE_HAS_DESTRUCTOR (type)) | |
1837 | cp_error ("type `%#T' does not have a destructor", type); | |
1838 | else | |
1839 | #endif | |
1840 | cp_error ("cannot call destructor `%T::~%T' without object", type, | |
1841 | method_name); | |
1842 | return error_mark_node; | |
1843 | } | |
1844 | ||
1845 | /* No object? Then just fake one up, and let build_method_call | |
1846 | figure out what to do. */ | |
1847 | if (current_class_type == 0 | |
1848 | || get_base_distance (type, current_class_type, 0, &basetype_path) == -1) | |
1849 | dont_use_this = 1; | |
1850 | ||
1851 | if (dont_use_this) | |
1852 | { | |
39211cd5 | 1853 | basetype_path = TYPE_BINFO (type); |
8d08fdba MS |
1854 | decl = build1 (NOP_EXPR, TYPE_POINTER_TO (type), error_mark_node); |
1855 | } | |
1856 | else if (current_class_decl == 0) | |
1857 | { | |
1858 | dont_use_this = 1; | |
1859 | decl = build1 (NOP_EXPR, TYPE_POINTER_TO (type), error_mark_node); | |
1860 | } | |
1861 | else | |
1862 | { | |
1863 | tree olddecl = current_class_decl; | |
1864 | tree oldtype = TREE_TYPE (TREE_TYPE (olddecl)); | |
1865 | if (oldtype != type) | |
1866 | { | |
1867 | tree newtype = build_type_variant (type, TYPE_READONLY (oldtype), | |
1868 | TYPE_VOLATILE (oldtype)); | |
1869 | decl = convert_force (TYPE_POINTER_TO (newtype), olddecl); | |
1870 | } | |
1871 | else | |
1872 | decl = olddecl; | |
1873 | } | |
1874 | ||
1875 | decl = build_indirect_ref (decl, NULL_PTR); | |
1876 | ||
39211cd5 | 1877 | if (t = lookup_fnfields (basetype_path, method_name, 0)) |
8d08fdba MS |
1878 | return build_method_call (decl, method_name, parmlist, basetype_path, |
1879 | LOOKUP_NORMAL|LOOKUP_NONVIRTUAL); | |
1880 | if (TREE_CODE (name) == IDENTIFIER_NODE | |
8926095f | 1881 | && ((t = lookup_field (TYPE_BINFO (type), name, 1, 0)))) |
8d08fdba MS |
1882 | { |
1883 | if (t == error_mark_node) | |
1884 | return error_mark_node; | |
1885 | if (TREE_CODE (t) == FIELD_DECL) | |
1886 | { | |
1887 | if (dont_use_this) | |
1888 | { | |
1889 | cp_error ("invalid use of non-static field `%D'", t); | |
1890 | return error_mark_node; | |
1891 | } | |
1892 | decl = build (COMPONENT_REF, TREE_TYPE (t), decl, t); | |
1893 | } | |
1894 | else if (TREE_CODE (t) == VAR_DECL) | |
1895 | decl = t; | |
1896 | else | |
1897 | { | |
1898 | cp_error ("invalid use of member `%D'", t); | |
1899 | return error_mark_node; | |
1900 | } | |
1901 | if (TYPE_LANG_SPECIFIC (TREE_TYPE (decl)) | |
1902 | && TYPE_OVERLOADS_CALL_EXPR (TREE_TYPE (decl))) | |
1903 | return build_opfncall (CALL_EXPR, LOOKUP_NORMAL, decl, parmlist, NULL_TREE); | |
1904 | return build_function_call (decl, parmlist); | |
1905 | } | |
1906 | else | |
1907 | { | |
1908 | cp_error ("no method `%T::%D'", type, name); | |
1909 | return error_mark_node; | |
1910 | } | |
1911 | } | |
1912 | ||
1913 | /* Build a reference to a member of an aggregate. This is not a | |
1914 | C++ `&', but really something which can have its address taken, | |
1915 | and then act as a pointer to member, for example CNAME :: FIELD | |
1916 | can have its address taken by saying & CNAME :: FIELD. | |
1917 | ||
1918 | @@ Prints out lousy diagnostics for operator <typename> | |
1919 | @@ fields. | |
1920 | ||
51c184be | 1921 | @@ This function should be rewritten and placed in search.c. */ |
8d08fdba MS |
1922 | tree |
1923 | build_offset_ref (cname, name) | |
1924 | tree cname, name; | |
1925 | { | |
1926 | tree decl, type, fnfields, fields, t = error_mark_node; | |
1927 | tree basetypes = NULL_TREE; | |
1928 | int dtor = 0; | |
1929 | ||
1930 | if (TREE_CODE (cname) == SCOPE_REF) | |
1931 | cname = resolve_scope_to_name (NULL_TREE, cname); | |
1932 | ||
1933 | if (cname == NULL_TREE || ! is_aggr_typedef (cname, 1)) | |
1934 | return error_mark_node; | |
1935 | ||
1936 | type = IDENTIFIER_TYPE_VALUE (cname); | |
1937 | ||
1938 | if (TREE_CODE (name) == BIT_NOT_EXPR) | |
1939 | { | |
1940 | dtor = 1; | |
1941 | name = TREE_OPERAND (name, 0); | |
1942 | } | |
1943 | ||
1944 | if (TYPE_SIZE (type) == 0) | |
1945 | { | |
1946 | t = IDENTIFIER_CLASS_VALUE (name); | |
1947 | if (t == 0) | |
1948 | { | |
1949 | cp_error ("incomplete type `%T' does not have member `%D'", type, | |
1950 | name); | |
1951 | return error_mark_node; | |
1952 | } | |
51c184be MS |
1953 | if (TREE_CODE (t) == TYPE_DECL || TREE_CODE (t) == VAR_DECL |
1954 | || TREE_CODE (t) == CONST_DECL) | |
8d08fdba MS |
1955 | { |
1956 | TREE_USED (t) = 1; | |
1957 | return t; | |
1958 | } | |
1959 | if (TREE_CODE (t) == FIELD_DECL) | |
1960 | sorry ("use of member in incomplete aggregate type"); | |
1961 | else if (TREE_CODE (t) == FUNCTION_DECL) | |
1962 | sorry ("use of member function in incomplete aggregate type"); | |
1963 | else | |
1964 | my_friendly_abort (52); | |
1965 | return error_mark_node; | |
1966 | } | |
1967 | ||
39211cd5 | 1968 | #if 0 |
8d08fdba MS |
1969 | if (TREE_CODE (name) == TYPE_EXPR) |
1970 | /* Pass a TYPE_DECL to build_component_type_expr. */ | |
1971 | return build_component_type_expr (TYPE_NAME (TREE_TYPE (cname)), | |
1972 | name, NULL_TREE, 1); | |
39211cd5 | 1973 | #endif |
8d08fdba MS |
1974 | |
1975 | fnfields = lookup_fnfields (TYPE_BINFO (type), name, 1); | |
1976 | fields = lookup_field (type, name, 0, 0); | |
1977 | ||
1978 | if (fields == error_mark_node || fnfields == error_mark_node) | |
1979 | return error_mark_node; | |
1980 | ||
1981 | if (current_class_type == 0 | |
1982 | || get_base_distance (type, current_class_type, 0, &basetypes) == -1) | |
1983 | { | |
1984 | basetypes = TYPE_BINFO (type); | |
1985 | decl = build1 (NOP_EXPR, | |
1986 | IDENTIFIER_TYPE_VALUE (cname), | |
1987 | error_mark_node); | |
1988 | } | |
1989 | else if (current_class_decl == 0) | |
1990 | decl = build1 (NOP_EXPR, IDENTIFIER_TYPE_VALUE (cname), | |
1991 | error_mark_node); | |
1992 | else | |
1993 | decl = C_C_D; | |
1994 | ||
1995 | /* A lot of this logic is now handled in lookup_field and | |
1996 | lookup_fnfield. */ | |
1997 | if (fnfields) | |
1998 | { | |
1999 | basetypes = TREE_PURPOSE (fnfields); | |
2000 | ||
2001 | /* Go from the TREE_BASELINK to the member function info. */ | |
2002 | t = TREE_VALUE (fnfields); | |
2003 | ||
2004 | if (fields) | |
2005 | { | |
2006 | if (DECL_FIELD_CONTEXT (fields) == DECL_FIELD_CONTEXT (t)) | |
2007 | { | |
2008 | error ("ambiguous member reference: member `%s' defined as both field and function", | |
2009 | IDENTIFIER_POINTER (name)); | |
2010 | return error_mark_node; | |
2011 | } | |
2012 | if (UNIQUELY_DERIVED_FROM_P (DECL_FIELD_CONTEXT (fields), DECL_FIELD_CONTEXT (t))) | |
2013 | ; | |
2014 | else if (UNIQUELY_DERIVED_FROM_P (DECL_FIELD_CONTEXT (t), DECL_FIELD_CONTEXT (fields))) | |
2015 | t = fields; | |
2016 | else | |
2017 | { | |
2018 | error ("ambiguous member reference: member `%s' derives from distinct classes in multiple inheritance lattice"); | |
2019 | return error_mark_node; | |
2020 | } | |
2021 | } | |
2022 | ||
2023 | if (t == TREE_VALUE (fnfields)) | |
2024 | { | |
2025 | extern int flag_save_memoized_contexts; | |
2026 | ||
2027 | /* This does not handle access checking yet. */ | |
2028 | if (DECL_CHAIN (t) == NULL_TREE || dtor) | |
2029 | { | |
2030 | enum access_type access; | |
2031 | ||
2032 | /* unique functions are handled easily. */ | |
2033 | unique: | |
2034 | access = compute_access (basetypes, t); | |
2035 | if (access == access_protected) | |
2036 | { | |
2037 | cp_error_at ("member function `%#D' is protected", t); | |
2038 | error ("in this context"); | |
2039 | return error_mark_node; | |
2040 | } | |
2041 | if (access == access_private) | |
2042 | { | |
2043 | cp_error_at ("member function `%#D' is private", t); | |
2044 | error ("in this context"); | |
2045 | return error_mark_node; | |
2046 | } | |
2047 | assemble_external (t); | |
2048 | return build (OFFSET_REF, TREE_TYPE (t), decl, t); | |
2049 | } | |
2050 | ||
2051 | /* overloaded functions may need more work. */ | |
2052 | if (cname == name) | |
2053 | { | |
2054 | if (TYPE_HAS_DESTRUCTOR (type) | |
2055 | && DECL_CHAIN (DECL_CHAIN (t)) == NULL_TREE) | |
2056 | { | |
2057 | t = DECL_CHAIN (t); | |
2058 | goto unique; | |
2059 | } | |
2060 | } | |
2061 | /* FNFIELDS is most likely allocated on the search_obstack, | |
2062 | which will go away after this class scope. If we need | |
2063 | to save this value for later (either for memoization | |
2064 | or for use as an initializer for a static variable), then | |
2065 | do so here. | |
2066 | ||
2067 | ??? The smart thing to do for the case of saving initializers | |
2068 | is to resolve them before we're done with this scope. */ | |
2069 | if (!TREE_PERMANENT (fnfields) | |
2070 | && ((flag_save_memoized_contexts && global_bindings_p ()) | |
2071 | || ! allocation_temporary_p ())) | |
2072 | fnfields = copy_list (fnfields); | |
2073 | t = build_tree_list (error_mark_node, fnfields); | |
2074 | TREE_TYPE (t) = build_offset_type (type, unknown_type_node); | |
2075 | return t; | |
2076 | } | |
2077 | } | |
2078 | ||
2079 | /* Now that we know we are looking for a field, see if we | |
2080 | have access to that field. Lookup_field will give us the | |
2081 | error message. */ | |
2082 | ||
2083 | t = lookup_field (basetypes, name, 1, 0); | |
2084 | ||
2085 | if (t == error_mark_node) | |
2086 | return error_mark_node; | |
2087 | ||
2088 | if (t == NULL_TREE) | |
2089 | { | |
a4443a08 | 2090 | cp_error ("`%D' is not a member of type `%T'", name, type); |
8d08fdba MS |
2091 | return error_mark_node; |
2092 | } | |
2093 | ||
2094 | if (TREE_CODE (t) == TYPE_DECL) | |
2095 | { | |
51c184be MS |
2096 | TREE_USED (t) = 1; |
2097 | return t; | |
8d08fdba MS |
2098 | } |
2099 | /* static class members and class-specific enum | |
2100 | values can be returned without further ado. */ | |
2101 | if (TREE_CODE (t) == VAR_DECL || TREE_CODE (t) == CONST_DECL) | |
2102 | { | |
2103 | assemble_external (t); | |
2104 | TREE_USED (t) = 1; | |
2105 | return t; | |
2106 | } | |
2107 | ||
2108 | /* static class functions too. */ | |
2109 | if (TREE_CODE (t) == FUNCTION_DECL && TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE) | |
2110 | my_friendly_abort (53); | |
2111 | ||
2112 | /* In member functions, the form `cname::name' is no longer | |
2113 | equivalent to `this->cname::name'. */ | |
2114 | return build (OFFSET_REF, build_offset_type (type, TREE_TYPE (t)), decl, t); | |
2115 | } | |
2116 | ||
2117 | /* Given an object EXP and a member function reference MEMBER, | |
2118 | return the address of the actual member function. */ | |
2119 | tree | |
2120 | get_member_function (exp_addr_ptr, exp, member) | |
2121 | tree *exp_addr_ptr; | |
2122 | tree exp, member; | |
2123 | { | |
2124 | tree ctype = TREE_TYPE (exp); | |
2125 | tree function = save_expr (build_unary_op (ADDR_EXPR, member, 0)); | |
2126 | ||
2127 | if (TYPE_VIRTUAL_P (ctype) | |
2128 | || (flag_all_virtual == 1 && TYPE_OVERLOADS_METHOD_CALL_EXPR (ctype))) | |
2129 | { | |
2130 | tree e0, e1, e3; | |
2131 | tree exp_addr; | |
2132 | ||
2133 | /* Save away the unadulterated `this' pointer. */ | |
2134 | exp_addr = save_expr (*exp_addr_ptr); | |
2135 | ||
2136 | /* Cast function to signed integer. */ | |
2137 | e0 = build1 (NOP_EXPR, integer_type_node, function); | |
2138 | ||
8d08fdba MS |
2139 | /* There is a hack here that takes advantage of |
2140 | twos complement arithmetic, and the fact that | |
2141 | there are more than one UNITS to the WORD. | |
2142 | If the high bit is set for the `function', | |
2143 | then we pretend it is a virtual function, | |
2144 | and the array indexing will knock this bit | |
2145 | out the top, leaving a valid index. */ | |
2146 | if (UNITS_PER_WORD <= 1) | |
2147 | my_friendly_abort (54); | |
2148 | ||
2149 | e1 = build (GT_EXPR, integer_type_node, e0, integer_zero_node); | |
2150 | e1 = build_compound_expr (tree_cons (NULL_TREE, exp_addr, | |
2151 | build_tree_list (NULL_TREE, e1))); | |
2152 | e1 = save_expr (e1); | |
8d08fdba MS |
2153 | |
2154 | if (TREE_SIDE_EFFECTS (*exp_addr_ptr)) | |
2155 | { | |
2156 | exp = build_indirect_ref (exp_addr, NULL_PTR); | |
2157 | *exp_addr_ptr = exp_addr; | |
2158 | } | |
2159 | ||
2160 | /* This is really hairy: if the function pointer is a pointer | |
2161 | to a non-virtual member function, then we can't go mucking | |
2162 | with the `this' pointer (any more than we already have to | |
2163 | this point). If it is a pointer to a virtual member function, | |
2164 | then we have to adjust the `this' pointer according to | |
2165 | what the virtual function table tells us. */ | |
2166 | ||
2167 | e3 = build_vfn_ref (exp_addr_ptr, exp, e0); | |
2168 | my_friendly_assert (e3 != error_mark_node, 213); | |
2169 | ||
2170 | /* Change this pointer type from `void *' to the | |
2171 | type it is really supposed to be. */ | |
2172 | TREE_TYPE (e3) = TREE_TYPE (function); | |
2173 | ||
2174 | /* If non-virtual, use what we had originally. Otherwise, | |
2175 | use the value we get from the virtual function table. */ | |
2176 | *exp_addr_ptr = build_conditional_expr (e1, exp_addr, *exp_addr_ptr); | |
2177 | ||
2178 | function = build_conditional_expr (e1, function, e3); | |
2179 | } | |
2180 | return build_indirect_ref (function, NULL_PTR); | |
2181 | } | |
2182 | ||
2183 | /* If a OFFSET_REF made it through to here, then it did | |
2184 | not have its address taken. */ | |
2185 | ||
2186 | tree | |
2187 | resolve_offset_ref (exp) | |
2188 | tree exp; | |
2189 | { | |
2190 | tree type = TREE_TYPE (exp); | |
2191 | tree base = NULL_TREE; | |
2192 | tree member; | |
2193 | tree basetype, addr; | |
2194 | ||
2195 | if (TREE_CODE (exp) == TREE_LIST) | |
2196 | return build_unary_op (ADDR_EXPR, exp, 0); | |
2197 | ||
2198 | if (TREE_CODE (exp) != OFFSET_REF) | |
2199 | { | |
2200 | my_friendly_assert (TREE_CODE (type) == OFFSET_TYPE, 214); | |
2201 | if (TYPE_OFFSET_BASETYPE (type) != current_class_type) | |
2202 | { | |
2203 | error ("object missing in use of pointer-to-member construct"); | |
2204 | return error_mark_node; | |
2205 | } | |
2206 | member = exp; | |
2207 | type = TREE_TYPE (type); | |
2208 | base = C_C_D; | |
2209 | } | |
2210 | else | |
2211 | { | |
2212 | member = TREE_OPERAND (exp, 1); | |
2213 | base = TREE_OPERAND (exp, 0); | |
2214 | } | |
2215 | ||
2216 | if ((TREE_CODE (member) == VAR_DECL | |
2217 | && ! TYPE_PTRMEMFUNC_P (TREE_TYPE (member))) | |
2218 | || TREE_CODE (TREE_TYPE (member)) == FUNCTION_TYPE) | |
2219 | { | |
2220 | /* These were static members. */ | |
2221 | if (mark_addressable (member) == 0) | |
2222 | return error_mark_node; | |
2223 | return member; | |
2224 | } | |
2225 | ||
2226 | /* Syntax error can cause a member which should | |
2227 | have been seen as static to be grok'd as non-static. */ | |
2228 | if (TREE_CODE (member) == FIELD_DECL && C_C_D == NULL_TREE) | |
2229 | { | |
2230 | if (TREE_ADDRESSABLE (member) == 0) | |
2231 | { | |
2232 | cp_error_at ("member `%D' is non-static in static member function context", member); | |
2233 | error ("at this point in file"); | |
2234 | TREE_ADDRESSABLE (member) = 1; | |
2235 | } | |
2236 | return error_mark_node; | |
2237 | } | |
2238 | ||
2239 | /* The first case is really just a reference to a member of `this'. */ | |
2240 | if (TREE_CODE (member) == FIELD_DECL | |
2241 | && (base == C_C_D | |
2242 | || (TREE_CODE (base) == NOP_EXPR | |
2243 | && TREE_OPERAND (base, 0) == error_mark_node))) | |
2244 | { | |
2245 | tree basetype_path; | |
2246 | enum access_type access; | |
2247 | ||
2248 | if (TREE_CODE (exp) == OFFSET_REF && TREE_CODE (type) == OFFSET_TYPE) | |
39211cd5 | 2249 | basetype = TYPE_OFFSET_BASETYPE (type); |
8d08fdba | 2250 | else |
39211cd5 MS |
2251 | basetype = DECL_CONTEXT (member); |
2252 | ||
2253 | base = current_class_decl; | |
8d08fdba MS |
2254 | |
2255 | if (get_base_distance (basetype, TREE_TYPE (TREE_TYPE (base)), 0, &basetype_path) < 0) | |
2256 | { | |
2257 | error_not_base_type (basetype, TREE_TYPE (TREE_TYPE (base))); | |
2258 | return error_mark_node; | |
2259 | } | |
2260 | addr = convert_pointer_to (basetype, base); | |
2261 | access = compute_access (basetype_path, member); | |
2262 | if (access == access_public) | |
2263 | return build (COMPONENT_REF, TREE_TYPE (member), | |
2264 | build_indirect_ref (addr, NULL_PTR), member); | |
2265 | if (access == access_protected) | |
2266 | { | |
2267 | cp_error_at ("member `%D' is protected", member); | |
2268 | error ("in this context"); | |
2269 | return error_mark_node; | |
2270 | } | |
2271 | if (access == access_private) | |
2272 | { | |
2273 | cp_error_at ("member `%D' is private", member); | |
2274 | error ("in this context"); | |
2275 | return error_mark_node; | |
2276 | } | |
2277 | my_friendly_abort (55); | |
2278 | } | |
2279 | ||
2280 | /* If this is a reference to a member function, then return | |
2281 | the address of the member function (which may involve going | |
2282 | through the object's vtable), otherwise, return an expression | |
2283 | for the dereferenced pointer-to-member construct. */ | |
2284 | addr = build_unary_op (ADDR_EXPR, base, 0); | |
2285 | ||
2286 | if (TREE_CODE (TREE_TYPE (member)) == METHOD_TYPE) | |
2287 | { | |
2288 | basetype = DECL_CLASS_CONTEXT (member); | |
2289 | addr = convert_pointer_to (basetype, addr); | |
2290 | return build_unary_op (ADDR_EXPR, get_member_function (&addr, build_indirect_ref (addr, NULL_PTR), member), 0); | |
2291 | } | |
2292 | else if (TREE_CODE (TREE_TYPE (member)) == OFFSET_TYPE) | |
2293 | { | |
2294 | basetype = TYPE_OFFSET_BASETYPE (TREE_TYPE (member)); | |
2295 | addr = convert_pointer_to (basetype, addr); | |
2296 | member = convert (ptr_type_node, build_unary_op (ADDR_EXPR, member, 0)); | |
2297 | return build1 (INDIRECT_REF, type, | |
2298 | build (PLUS_EXPR, ptr_type_node, addr, member)); | |
2299 | } | |
2300 | else if (TYPE_PTRMEMFUNC_P (TREE_TYPE (member))) | |
2301 | { | |
2302 | return get_member_function_from_ptrfunc (&addr, base, member); | |
2303 | } | |
2304 | my_friendly_abort (56); | |
2305 | /* NOTREACHED */ | |
2306 | return NULL_TREE; | |
2307 | } | |
2308 | ||
2309 | /* Return either DECL or its known constant value (if it has one). */ | |
2310 | ||
2311 | tree | |
2312 | decl_constant_value (decl) | |
2313 | tree decl; | |
2314 | { | |
2315 | if (! TREE_THIS_VOLATILE (decl) | |
2316 | #if 0 | |
2317 | /* These may be necessary for C, but they break C++. */ | |
2318 | ! TREE_PUBLIC (decl) | |
2319 | /* Don't change a variable array bound or initial value to a constant | |
2320 | in a place where a variable is invalid. */ | |
2321 | && ! pedantic | |
2322 | #endif /* 0 */ | |
2323 | && DECL_INITIAL (decl) != 0 | |
2324 | && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK | |
2325 | /* This is invalid if initial value is not constant. | |
2326 | If it has either a function call, a memory reference, | |
2327 | or a variable, then re-evaluating it could give different results. */ | |
2328 | && TREE_CONSTANT (DECL_INITIAL (decl)) | |
2329 | /* Check for cases where this is sub-optimal, even though valid. */ | |
2330 | && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR | |
2331 | #if 0 | |
2332 | /* We must allow this to work outside of functions so that | |
2333 | static constants can be used for array sizes. */ | |
2334 | && current_function_decl != 0 | |
2335 | && DECL_MODE (decl) != BLKmode | |
2336 | #endif | |
2337 | ) | |
2338 | return DECL_INITIAL (decl); | |
2339 | return decl; | |
2340 | } | |
2341 | \f | |
2342 | /* Friend handling routines. */ | |
2343 | /* Friend data structures: | |
2344 | ||
2345 | Lists of friend functions come from TYPE_DECL nodes. Since all | |
2346 | aggregate types are automatically typedef'd, these nodes are guaranteed | |
2347 | to exist. | |
2348 | ||
2349 | The TREE_PURPOSE of a friend list is the name of the friend, | |
2350 | and its TREE_VALUE is another list. | |
2351 | ||
2352 | For each element of that list, either the TREE_VALUE or the TREE_PURPOSE | |
2353 | will be filled in, but not both. The TREE_VALUE of that list is an | |
2354 | individual function which is a friend. The TREE_PURPOSE of that list | |
2355 | indicates a type in which all functions by that name are friends. | |
2356 | ||
2357 | Lists of friend classes come from _TYPE nodes. Love that consistency | |
2358 | thang. */ | |
2359 | ||
2360 | int | |
2361 | is_friend_type (type1, type2) | |
2362 | tree type1, type2; | |
2363 | { | |
2364 | return is_friend (type1, type2); | |
2365 | } | |
2366 | ||
2367 | int | |
2368 | is_friend (type, supplicant) | |
2369 | tree type, supplicant; | |
2370 | { | |
2371 | int declp; | |
2372 | register tree list; | |
2373 | ||
2374 | if (supplicant == NULL_TREE || type == NULL_TREE) | |
2375 | return 0; | |
2376 | ||
2377 | declp = (TREE_CODE_CLASS (TREE_CODE (supplicant)) == 'd'); | |
2378 | ||
2379 | if (declp) | |
2380 | /* It's a function decl. */ | |
2381 | { | |
2382 | tree list = DECL_FRIENDLIST (TYPE_NAME (type)); | |
2383 | tree name = DECL_NAME (supplicant); | |
2384 | tree ctype = DECL_CLASS_CONTEXT (supplicant); | |
2385 | for (; list ; list = TREE_CHAIN (list)) | |
2386 | { | |
2387 | if (name == TREE_PURPOSE (list)) | |
2388 | { | |
2389 | tree friends = TREE_VALUE (list); | |
2390 | name = DECL_ASSEMBLER_NAME (supplicant); | |
2391 | for (; friends ; friends = TREE_CHAIN (friends)) | |
2392 | { | |
2393 | if (ctype == TREE_PURPOSE (friends)) | |
2394 | return 1; | |
2395 | if (name == DECL_ASSEMBLER_NAME (TREE_VALUE (friends))) | |
2396 | return 1; | |
2397 | } | |
2398 | break; | |
2399 | } | |
2400 | } | |
2401 | } | |
2402 | else | |
2403 | /* It's a type. */ | |
2404 | { | |
2405 | if (type == supplicant) | |
2406 | return 1; | |
2407 | ||
2408 | list = CLASSTYPE_FRIEND_CLASSES (TREE_TYPE (TYPE_NAME (type))); | |
2409 | for (; list ; list = TREE_CHAIN (list)) | |
2410 | if (supplicant == TREE_VALUE (list)) | |
2411 | return 1; | |
2412 | } | |
2413 | ||
2414 | { | |
2415 | tree context = declp ? DECL_CLASS_CONTEXT (supplicant) | |
2416 | : DECL_CONTEXT (TYPE_NAME (supplicant)); | |
2417 | ||
2418 | if (context) | |
2419 | return is_friend (type, context); | |
2420 | } | |
2421 | ||
2422 | return 0; | |
2423 | } | |
2424 | ||
2425 | /* Add a new friend to the friends of the aggregate type TYPE. | |
2426 | DECL is the FUNCTION_DECL of the friend being added. */ | |
2427 | static void | |
2428 | add_friend (type, decl) | |
2429 | tree type, decl; | |
2430 | { | |
2431 | tree typedecl = TYPE_NAME (type); | |
2432 | tree list = DECL_FRIENDLIST (typedecl); | |
2433 | tree name = DECL_NAME (decl); | |
2434 | ||
2435 | while (list) | |
2436 | { | |
2437 | if (name == TREE_PURPOSE (list)) | |
2438 | { | |
2439 | tree friends = TREE_VALUE (list); | |
2440 | for (; friends ; friends = TREE_CHAIN (friends)) | |
2441 | { | |
2442 | if (decl == TREE_VALUE (friends)) | |
2443 | { | |
8926095f MS |
2444 | cp_pedwarn ("`%D' is already a friend of class `%T'", |
2445 | decl, type); | |
2446 | cp_pedwarn_at ("previous friend declaration of `%D'", | |
2447 | TREE_VALUE (friends)); | |
8d08fdba MS |
2448 | return; |
2449 | } | |
2450 | } | |
2451 | TREE_VALUE (list) = tree_cons (error_mark_node, decl, | |
2452 | TREE_VALUE (list)); | |
2453 | return; | |
2454 | } | |
2455 | list = TREE_CHAIN (list); | |
2456 | } | |
2457 | DECL_FRIENDLIST (typedecl) | |
2458 | = tree_cons (DECL_NAME (decl), build_tree_list (error_mark_node, decl), | |
2459 | DECL_FRIENDLIST (typedecl)); | |
2460 | if (DECL_NAME (decl) == ansi_opname[(int) MODIFY_EXPR]) | |
2461 | { | |
2462 | tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (decl)); | |
2463 | TYPE_HAS_ASSIGNMENT (TREE_TYPE (typedecl)) = 1; | |
2464 | if (parmtypes && TREE_CHAIN (parmtypes)) | |
2465 | { | |
2466 | tree parmtype = TREE_VALUE (TREE_CHAIN (parmtypes)); | |
2467 | if (TREE_CODE (parmtype) == REFERENCE_TYPE | |
2468 | && TREE_TYPE (parmtypes) == TREE_TYPE (typedecl)) | |
2469 | TYPE_HAS_ASSIGN_REF (TREE_TYPE (typedecl)) = 1; | |
2470 | } | |
2471 | } | |
2472 | } | |
2473 | ||
2474 | /* Declare that every member function NAME in FRIEND_TYPE | |
2475 | (which may be NULL_TREE) is a friend of type TYPE. */ | |
2476 | static void | |
2477 | add_friends (type, name, friend_type) | |
2478 | tree type, name, friend_type; | |
2479 | { | |
2480 | tree typedecl = TYPE_NAME (type); | |
2481 | tree list = DECL_FRIENDLIST (typedecl); | |
2482 | ||
2483 | while (list) | |
2484 | { | |
2485 | if (name == TREE_PURPOSE (list)) | |
2486 | { | |
2487 | tree friends = TREE_VALUE (list); | |
2488 | while (friends && TREE_PURPOSE (friends) != friend_type) | |
2489 | friends = TREE_CHAIN (friends); | |
2490 | if (friends) | |
2491 | if (friend_type) | |
2492 | warning ("method `%s::%s' is already a friend of class", | |
2493 | TYPE_NAME_STRING (friend_type), | |
2494 | IDENTIFIER_POINTER (name)); | |
2495 | else | |
2496 | warning ("function `%s' is already a friend of class `%s'", | |
2497 | IDENTIFIER_POINTER (name), | |
2498 | IDENTIFIER_POINTER (DECL_NAME (typedecl))); | |
2499 | else | |
2500 | TREE_VALUE (list) = tree_cons (friend_type, NULL_TREE, | |
2501 | TREE_VALUE (list)); | |
2502 | return; | |
2503 | } | |
2504 | list = TREE_CHAIN (list); | |
2505 | } | |
2506 | DECL_FRIENDLIST (typedecl) = | |
2507 | tree_cons (name, | |
2508 | build_tree_list (friend_type, NULL_TREE), | |
2509 | DECL_FRIENDLIST (typedecl)); | |
2510 | if (! strncmp (IDENTIFIER_POINTER (name), | |
2511 | IDENTIFIER_POINTER (ansi_opname[(int) MODIFY_EXPR]), | |
2512 | strlen (IDENTIFIER_POINTER (ansi_opname[(int) MODIFY_EXPR])))) | |
2513 | { | |
2514 | TYPE_HAS_ASSIGNMENT (TREE_TYPE (typedecl)) = 1; | |
2515 | sorry ("declaring \"friend operator =\" will not find \"operator = (X&)\" if it exists"); | |
2516 | } | |
2517 | } | |
2518 | ||
2519 | /* Set up a cross reference so that type TYPE will make member function | |
2520 | CTYPE::DECL a friend when CTYPE is finally defined. For more than | |
2521 | one, set up a cross reference so that functions with the name DECL | |
2522 | and type CTYPE know that they are friends of TYPE. */ | |
2523 | static void | |
2524 | xref_friend (type, decl, ctype) | |
2525 | tree type, decl, ctype; | |
2526 | { | |
8d08fdba MS |
2527 | tree friend_decl = TYPE_NAME (ctype); |
2528 | #if 0 | |
8926095f | 2529 | tree typedecl = TYPE_NAME (type); |
8d08fdba MS |
2530 | tree t = tree_cons (NULL_TREE, ctype, DECL_UNDEFINED_FRIENDS (typedecl)); |
2531 | ||
2532 | DECL_UNDEFINED_FRIENDS (typedecl) = t; | |
2533 | #else | |
2534 | tree t = 0; | |
2535 | #endif | |
2536 | SET_DECL_WAITING_FRIENDS (friend_decl, | |
2537 | tree_cons (type, t, | |
2538 | DECL_WAITING_FRIENDS (friend_decl))); | |
2539 | TREE_TYPE (DECL_WAITING_FRIENDS (friend_decl)) = decl; | |
2540 | } | |
2541 | ||
2542 | /* Make FRIEND_TYPE a friend class to TYPE. If FRIEND_TYPE has already | |
2543 | been defined, we make all of its member functions friends of | |
2544 | TYPE. If not, we make it a pending friend, which can later be added | |
2545 | when its definition is seen. If a type is defined, then its TYPE_DECL's | |
2546 | DECL_UNDEFINED_FRIENDS contains a (possibly empty) list of friend | |
2547 | classes that are not defined. If a type has not yet been defined, | |
2548 | then the DECL_WAITING_FRIENDS contains a list of types | |
2549 | waiting to make it their friend. Note that these two can both | |
2550 | be in use at the same time! */ | |
2551 | void | |
2552 | make_friend_class (type, friend_type) | |
2553 | tree type, friend_type; | |
2554 | { | |
2555 | tree classes; | |
2556 | ||
2557 | if (IS_SIGNATURE (type)) | |
2558 | { | |
2559 | error ("`friend' declaration in signature definition"); | |
2560 | return; | |
2561 | } | |
2562 | if (IS_SIGNATURE (friend_type)) | |
2563 | { | |
2564 | error ("signature type `%s' declared `friend'", | |
2565 | IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (friend_type)))); | |
2566 | return; | |
2567 | } | |
2568 | if (type == friend_type) | |
2569 | { | |
2570 | warning ("class `%s' is implicitly friends with itself", | |
2571 | TYPE_NAME_STRING (type)); | |
2572 | return; | |
2573 | } | |
2574 | ||
2575 | GNU_xref_hier (TYPE_NAME_STRING (type), | |
2576 | TYPE_NAME_STRING (friend_type), 0, 0, 1); | |
2577 | ||
2578 | classes = CLASSTYPE_FRIEND_CLASSES (type); | |
2579 | while (classes && TREE_VALUE (classes) != friend_type) | |
2580 | classes = TREE_CHAIN (classes); | |
2581 | if (classes) | |
2582 | warning ("class `%s' is already friends with class `%s'", | |
2583 | TYPE_NAME_STRING (TREE_VALUE (classes)), TYPE_NAME_STRING (type)); | |
2584 | else | |
2585 | { | |
2586 | CLASSTYPE_FRIEND_CLASSES (type) | |
2587 | = tree_cons (NULL_TREE, friend_type, CLASSTYPE_FRIEND_CLASSES (type)); | |
2588 | } | |
2589 | } | |
2590 | ||
2591 | /* Main friend processor. This is large, and for modularity purposes, | |
2592 | has been removed from grokdeclarator. It returns `void_type_node' | |
2593 | to indicate that something happened, though a FIELD_DECL is | |
2594 | not returned. | |
2595 | ||
2596 | CTYPE is the class this friend belongs to. | |
2597 | ||
2598 | DECLARATOR is the name of the friend. | |
2599 | ||
2600 | DECL is the FUNCTION_DECL that the friend is. | |
2601 | ||
2602 | In case we are parsing a friend which is part of an inline | |
2603 | definition, we will need to store PARM_DECL chain that comes | |
2604 | with it into the DECL_ARGUMENTS slot of the FUNCTION_DECL. | |
2605 | ||
2606 | FLAGS is just used for `grokclassfn'. | |
2607 | ||
2608 | QUALS say what special qualifies should apply to the object | |
2609 | pointed to by `this'. */ | |
2610 | tree | |
2611 | do_friend (ctype, declarator, decl, parmdecls, flags, quals) | |
2612 | tree ctype, declarator, decl, parmdecls; | |
2613 | enum overload_flags flags; | |
2614 | tree quals; | |
2615 | { | |
2616 | /* first, lets find out if what we are making a friend needs overloading */ | |
2617 | tree previous_decl; | |
2618 | int was_c_linkage = 0; | |
2619 | ||
2620 | /* Every decl that gets here is a friend of something. */ | |
2621 | DECL_FRIEND_P (decl) = 1; | |
2622 | ||
2623 | /* If we find something in scope, let see if it has extern "C" linkage. */ | |
2624 | /* This code is pretty general and should be ripped out and reused | |
2625 | as a separate function. */ | |
2626 | if (DECL_NAME (decl)) | |
2627 | { | |
2628 | previous_decl = lookup_name (DECL_NAME (decl), 0); | |
2629 | if (previous_decl && TREE_CODE (previous_decl) == TREE_LIST) | |
2630 | { | |
2631 | do | |
2632 | { | |
2633 | if (TREE_TYPE (TREE_VALUE (previous_decl)) == TREE_TYPE (decl)) | |
2634 | { | |
2635 | previous_decl = TREE_VALUE (previous_decl); | |
2636 | break; | |
2637 | } | |
2638 | previous_decl = TREE_CHAIN (previous_decl); | |
2639 | } | |
2640 | while (previous_decl); | |
2641 | } | |
2642 | ||
2643 | /* It had extern "C" linkage, so don't overload this. */ | |
2644 | if (previous_decl && TREE_CODE (previous_decl) == FUNCTION_DECL | |
2645 | && TREE_TYPE (decl) == TREE_TYPE (previous_decl) | |
2646 | && DECL_LANGUAGE (previous_decl) == lang_c) | |
2647 | was_c_linkage = 1; | |
2648 | } | |
2649 | ||
2650 | if (ctype) | |
2651 | { | |
2652 | tree cname = TYPE_NAME (ctype); | |
2653 | if (TREE_CODE (cname) == TYPE_DECL) | |
2654 | cname = DECL_NAME (cname); | |
2655 | ||
2656 | /* A method friend. */ | |
2657 | if (TREE_CODE (decl) == FUNCTION_DECL) | |
2658 | { | |
2659 | if (flags == NO_SPECIAL && ctype && declarator == cname) | |
2660 | DECL_CONSTRUCTOR_P (decl) = 1; | |
2661 | ||
2662 | /* This will set up DECL_ARGUMENTS for us. */ | |
2663 | grokclassfn (ctype, cname, decl, flags, quals); | |
2664 | if (TYPE_SIZE (ctype) != 0) | |
2665 | check_classfn (ctype, cname, decl); | |
2666 | ||
2667 | if (TREE_TYPE (decl) != error_mark_node) | |
2668 | { | |
2669 | if (TYPE_SIZE (ctype)) | |
2670 | { | |
2671 | /* We don't call pushdecl here yet, or ever on this | |
2672 | actual FUNCTION_DECL. We must preserve its TREE_CHAIN | |
2673 | until the end. */ | |
2674 | make_decl_rtl (decl, NULL_PTR, 1); | |
2675 | add_friend (current_class_type, decl); | |
2676 | } | |
2677 | else | |
2678 | { | |
2679 | register char *classname | |
2680 | = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (ctype))); | |
2681 | ||
2682 | error ("member declared as friend before type `%s' defined", | |
2683 | classname); | |
2684 | } | |
2685 | } | |
2686 | } | |
2687 | else | |
2688 | { | |
2689 | /* Possibly a bunch of method friends. */ | |
2690 | ||
2691 | /* Get the class they belong to. */ | |
2692 | tree ctype = IDENTIFIER_TYPE_VALUE (cname); | |
2693 | ||
2694 | /* This class is defined, use its methods now. */ | |
2695 | if (TYPE_SIZE (ctype)) | |
2696 | { | |
2697 | tree fields = lookup_fnfields (TYPE_BINFO (ctype), declarator, 0); | |
2698 | if (fields) | |
2699 | add_friends (current_class_type, declarator, ctype); | |
2700 | else | |
2701 | error ("method `%s' is not a member of class `%s'", | |
2702 | IDENTIFIER_POINTER (declarator), | |
2703 | IDENTIFIER_POINTER (cname)); | |
2704 | } | |
2705 | else | |
2706 | /* Note: DECLARATOR actually has more than one; in this | |
2707 | case, we're making sure that fns with the name DECLARATOR | |
2708 | and type CTYPE know they are friends of the current | |
2709 | class type. */ | |
2710 | xref_friend (current_class_type, declarator, ctype); | |
2711 | decl = void_type_node; | |
2712 | } | |
2713 | } | |
2714 | /* never overload C functions */ | |
2715 | else if (TREE_CODE (decl) == FUNCTION_DECL | |
2716 | && ((IDENTIFIER_LENGTH (declarator) == 4 | |
2717 | && IDENTIFIER_POINTER (declarator)[0] == 'm' | |
2718 | && ! strcmp (IDENTIFIER_POINTER (declarator), "main")) | |
2719 | || (IDENTIFIER_LENGTH (declarator) > 10 | |
2720 | && IDENTIFIER_POINTER (declarator)[0] == '_' | |
2721 | && IDENTIFIER_POINTER (declarator)[1] == '_' | |
2722 | && strncmp (IDENTIFIER_POINTER (declarator)+2, | |
2723 | "builtin_", 8) == 0) | |
2724 | || was_c_linkage)) | |
2725 | { | |
2726 | /* raw "main", and builtin functions never gets overloaded, | |
2727 | but they can become friends. */ | |
2728 | TREE_PUBLIC (decl) = 1; | |
2729 | add_friend (current_class_type, decl); | |
2730 | DECL_FRIEND_P (decl) = 1; | |
8d08fdba MS |
2731 | decl = void_type_node; |
2732 | } | |
2733 | /* A global friend. | |
2734 | @@ or possibly a friend from a base class ?!? */ | |
2735 | else if (TREE_CODE (decl) == FUNCTION_DECL) | |
2736 | { | |
2737 | /* Friends must all go through the overload machinery, | |
2738 | even though they may not technically be overloaded. | |
2739 | ||
2740 | Note that because classes all wind up being top-level | |
2741 | in their scope, their friend wind up in top-level scope as well. */ | |
2742 | DECL_ASSEMBLER_NAME (decl) | |
2743 | = build_decl_overload (declarator, TYPE_ARG_TYPES (TREE_TYPE (decl)), | |
2744 | TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE); | |
2745 | DECL_ARGUMENTS (decl) = parmdecls; | |
2746 | DECL_CLASS_CONTEXT (decl) = current_class_type; | |
2747 | ||
2748 | /* We can call pushdecl here, because the TREE_CHAIN of this | |
2749 | FUNCTION_DECL is not needed for other purposes. */ | |
a4443a08 | 2750 | decl = pushdecl (decl); |
8d08fdba MS |
2751 | |
2752 | make_decl_rtl (decl, NULL_PTR, 1); | |
2753 | add_friend (current_class_type, decl); | |
2754 | ||
8d08fdba | 2755 | DECL_FRIEND_P (decl) = 1; |
7177d104 | 2756 | #if 0 |
8d08fdba | 2757 | TREE_OVERLOADED (declarator) = 1; |
7177d104 | 2758 | #endif |
8d08fdba MS |
2759 | } |
2760 | else | |
2761 | { | |
2762 | /* @@ Should be able to ingest later definitions of this function | |
2763 | before use. */ | |
700f8a87 | 2764 | tree decl = lookup_name_nonclass (declarator); |
8d08fdba MS |
2765 | if (decl == NULL_TREE) |
2766 | { | |
2767 | warning ("implicitly declaring `%s' as struct", | |
2768 | IDENTIFIER_POINTER (declarator)); | |
2769 | decl = xref_tag (record_type_node, declarator, NULL_TREE, 1); | |
2770 | decl = TYPE_NAME (decl); | |
2771 | } | |
2772 | ||
2773 | /* Allow abbreviated declarations of overloaded functions, | |
2774 | but not if those functions are really class names. */ | |
2775 | if (TREE_CODE (decl) == TREE_LIST && TREE_TYPE (TREE_PURPOSE (decl))) | |
2776 | { | |
2777 | warning ("`friend %s' archaic, use `friend class %s' instead", | |
2778 | IDENTIFIER_POINTER (declarator), | |
2779 | IDENTIFIER_POINTER (declarator)); | |
2780 | decl = TREE_TYPE (TREE_PURPOSE (decl)); | |
2781 | } | |
2782 | ||
2783 | if (TREE_CODE (decl) == TREE_LIST) | |
2784 | add_friends (current_class_type, TREE_PURPOSE (decl), NULL_TREE); | |
2785 | else | |
2786 | make_friend_class (current_class_type, TREE_TYPE (decl)); | |
2787 | decl = void_type_node; | |
2788 | } | |
2789 | return decl; | |
2790 | } | |
2791 | ||
2792 | /* TYPE has now been defined. It may, however, have a number of things | |
2793 | waiting make make it their friend. We resolve these references | |
2794 | here. */ | |
2795 | void | |
2796 | embrace_waiting_friends (type) | |
2797 | tree type; | |
2798 | { | |
2799 | tree decl = TYPE_NAME (type); | |
2800 | tree waiters; | |
2801 | ||
2802 | if (TREE_CODE (decl) != TYPE_DECL) | |
2803 | return; | |
2804 | ||
2805 | for (waiters = DECL_WAITING_FRIENDS (decl); waiters; | |
2806 | waiters = TREE_CHAIN (waiters)) | |
2807 | { | |
2808 | tree waiter = TREE_PURPOSE (waiters); | |
2809 | #if 0 | |
2810 | tree waiter_prev = TREE_VALUE (waiters); | |
2811 | #endif | |
2812 | tree decl = TREE_TYPE (waiters); | |
2813 | tree name = decl ? (TREE_CODE (decl) == IDENTIFIER_NODE | |
2814 | ? decl : DECL_NAME (decl)) : NULL_TREE; | |
2815 | if (name) | |
2816 | { | |
2817 | /* @@ There may be work to be done since we have not verified | |
2818 | @@ consistency between original and friend declarations | |
2819 | @@ of the functions waiting to become friends. */ | |
2820 | tree field = lookup_fnfields (TYPE_BINFO (type), name, 0); | |
2821 | if (field) | |
2822 | if (decl == name) | |
2823 | add_friends (waiter, name, type); | |
2824 | else | |
2825 | add_friend (waiter, decl); | |
2826 | else | |
2827 | error_with_file_and_line (DECL_SOURCE_FILE (TYPE_NAME (waiter)), | |
2828 | DECL_SOURCE_LINE (TYPE_NAME (waiter)), | |
2829 | "no method `%s' defined in class `%s' to be friend", | |
2830 | IDENTIFIER_POINTER (DECL_NAME (TREE_TYPE (waiters))), | |
2831 | TYPE_NAME_STRING (type)); | |
2832 | } | |
2833 | else | |
2834 | make_friend_class (type, waiter); | |
2835 | ||
2836 | #if 0 | |
2837 | if (TREE_CHAIN (waiter_prev)) | |
2838 | TREE_CHAIN (waiter_prev) = TREE_CHAIN (TREE_CHAIN (waiter_prev)); | |
2839 | else | |
2840 | DECL_UNDEFINED_FRIENDS (TYPE_NAME (waiter)) = NULL_TREE; | |
2841 | #endif | |
2842 | } | |
2843 | } | |
2844 | \f | |
2845 | /* Common subroutines of build_new and build_vec_delete. */ | |
2846 | ||
2847 | /* Common interface for calling "builtin" functions that are not | |
2848 | really builtin. */ | |
2849 | ||
2850 | tree | |
2851 | build_builtin_call (type, node, arglist) | |
2852 | tree type; | |
2853 | tree node; | |
2854 | tree arglist; | |
2855 | { | |
2856 | tree rval = build (CALL_EXPR, type, node, arglist, 0); | |
2857 | TREE_SIDE_EFFECTS (rval) = 1; | |
2858 | assemble_external (TREE_OPERAND (node, 0)); | |
2859 | TREE_USED (TREE_OPERAND (node, 0)) = 1; | |
2860 | return rval; | |
2861 | } | |
2862 | \f | |
2863 | /* Generate a C++ "new" expression. DECL is either a TREE_LIST | |
2864 | (which needs to go through some sort of groktypename) or it | |
2865 | is the name of the class we are newing. INIT is an initialization value. | |
2866 | It is either an EXPRLIST, an EXPR_NO_COMMAS, or something in braces. | |
2867 | If INIT is void_type_node, it means do *not* call a constructor | |
2868 | for this instance. | |
2869 | ||
2870 | For types with constructors, the data returned is initialized | |
2871 | by the appropriate constructor. | |
2872 | ||
2873 | Whether the type has a constructor or not, if it has a pointer | |
2874 | to a virtual function table, then that pointer is set up | |
2875 | here. | |
2876 | ||
2877 | Unless I am mistaken, a call to new () will return initialized | |
2878 | data regardless of whether the constructor itself is private or | |
8926095f | 2879 | not. NOPE; new fails if the constructor is private (jcm). |
8d08fdba MS |
2880 | |
2881 | Note that build_new does nothing to assure that any special | |
2882 | alignment requirements of the type are met. Rather, it leaves | |
2883 | it up to malloc to do the right thing. Otherwise, folding to | |
2884 | the right alignment cal cause problems if the user tries to later | |
2885 | free the memory returned by `new'. | |
2886 | ||
2887 | PLACEMENT is the `placement' list for user-defined operator new (). */ | |
2888 | ||
2889 | tree | |
2890 | build_new (placement, decl, init, use_global_new) | |
2891 | tree placement; | |
2892 | tree decl, init; | |
2893 | int use_global_new; | |
2894 | { | |
2895 | tree type, true_type, size, rval; | |
8926095f MS |
2896 | tree nelts; |
2897 | int has_array = 0; | |
a28e3c7f | 2898 | enum tree_code code = NEW_EXPR; |
8d08fdba MS |
2899 | |
2900 | tree pending_sizes = NULL_TREE; | |
2901 | ||
2902 | if (decl == error_mark_node) | |
2903 | return error_mark_node; | |
2904 | ||
2905 | if (TREE_CODE (decl) == TREE_LIST) | |
2906 | { | |
2907 | tree absdcl = TREE_VALUE (decl); | |
2908 | tree last_absdcl = NULL_TREE; | |
2909 | int old_immediate_size_expand; | |
2910 | ||
2911 | if (current_function_decl | |
2912 | && DECL_CONSTRUCTOR_P (current_function_decl)) | |
2913 | { | |
2914 | old_immediate_size_expand = immediate_size_expand; | |
2915 | immediate_size_expand = 0; | |
2916 | } | |
2917 | ||
2918 | nelts = integer_one_node; | |
2919 | ||
2920 | if (absdcl && TREE_CODE (absdcl) == CALL_EXPR) | |
8926095f | 2921 | my_friendly_abort (215); |
8d08fdba MS |
2922 | while (absdcl && TREE_CODE (absdcl) == INDIRECT_REF) |
2923 | { | |
2924 | last_absdcl = absdcl; | |
2925 | absdcl = TREE_OPERAND (absdcl, 0); | |
2926 | } | |
2927 | ||
2928 | if (absdcl && TREE_CODE (absdcl) == ARRAY_REF) | |
2929 | { | |
2930 | /* probably meant to be a vec new */ | |
2931 | tree this_nelts; | |
2932 | ||
51c184be MS |
2933 | while (TREE_OPERAND (absdcl, 0) |
2934 | && TREE_CODE (TREE_OPERAND (absdcl, 0)) == ARRAY_REF) | |
2935 | { | |
2936 | last_absdcl = absdcl; | |
2937 | absdcl = TREE_OPERAND (absdcl, 0); | |
2938 | } | |
2939 | ||
8d08fdba MS |
2940 | has_array = 1; |
2941 | this_nelts = TREE_OPERAND (absdcl, 1); | |
2942 | if (this_nelts != error_mark_node) | |
2943 | { | |
2944 | if (this_nelts == NULL_TREE) | |
2945 | error ("new of array type fails to specify size"); | |
2946 | else | |
2947 | { | |
8926095f | 2948 | this_nelts = save_expr (convert (sizetype, this_nelts)); |
8d08fdba MS |
2949 | absdcl = TREE_OPERAND (absdcl, 0); |
2950 | if (this_nelts == integer_zero_node) | |
2951 | { | |
2952 | warning ("zero size array reserves no space"); | |
2953 | nelts = integer_zero_node; | |
2954 | } | |
2955 | else | |
2956 | nelts = build_binary_op (MULT_EXPR, nelts, this_nelts, 1); | |
2957 | } | |
2958 | } | |
2959 | else | |
2960 | nelts = integer_zero_node; | |
2961 | } | |
2962 | ||
2963 | if (last_absdcl) | |
2964 | TREE_OPERAND (last_absdcl, 0) = absdcl; | |
2965 | else | |
2966 | TREE_VALUE (decl) = absdcl; | |
2967 | ||
2968 | type = true_type = groktypename (decl); | |
8926095f | 2969 | if (! type || type == error_mark_node) |
8d08fdba MS |
2970 | { |
2971 | immediate_size_expand = old_immediate_size_expand; | |
2972 | return error_mark_node; | |
2973 | } | |
2974 | ||
8d08fdba MS |
2975 | if (current_function_decl |
2976 | && DECL_CONSTRUCTOR_P (current_function_decl)) | |
2977 | { | |
2978 | pending_sizes = get_pending_sizes (); | |
2979 | immediate_size_expand = old_immediate_size_expand; | |
2980 | } | |
2981 | } | |
2982 | else if (TREE_CODE (decl) == IDENTIFIER_NODE) | |
2983 | { | |
2984 | if (IDENTIFIER_HAS_TYPE_VALUE (decl)) | |
2985 | { | |
2986 | /* An aggregate type. */ | |
2987 | type = IDENTIFIER_TYPE_VALUE (decl); | |
2988 | decl = TYPE_NAME (type); | |
2989 | } | |
2990 | else | |
2991 | { | |
2992 | /* A builtin type. */ | |
2993 | decl = lookup_name (decl, 1); | |
2994 | my_friendly_assert (TREE_CODE (decl) == TYPE_DECL, 215); | |
2995 | type = TREE_TYPE (decl); | |
2996 | } | |
2997 | true_type = type; | |
2998 | } | |
2999 | else if (TREE_CODE (decl) == TYPE_DECL) | |
3000 | { | |
3001 | type = TREE_TYPE (decl); | |
3002 | true_type = type; | |
3003 | } | |
3004 | else | |
3005 | { | |
3006 | type = decl; | |
3007 | true_type = type; | |
3008 | decl = TYPE_NAME (type); | |
3009 | } | |
3010 | ||
8926095f MS |
3011 | /* ``A reference cannot be created by the new operator. A reference |
3012 | is not an object (8.2.2, 8.4.3), so a pointer to it could not be | |
3013 | returned by new.'' ARM 5.3.3 */ | |
3014 | if (TREE_CODE (type) == REFERENCE_TYPE) | |
8d08fdba | 3015 | { |
8926095f MS |
3016 | error ("new cannot be applied to a reference type"); |
3017 | type = true_type = TREE_TYPE (type); | |
8d08fdba MS |
3018 | } |
3019 | ||
8926095f MS |
3020 | /* When the object being created is an array, the new-expression yields a |
3021 | pointer to the initial element (if any) of the array. For example, | |
3022 | both new int and new int[10] return an int*. 5.3.4. */ | |
3023 | if (TREE_CODE (type) == ARRAY_TYPE && has_array == 0) | |
8d08fdba | 3024 | { |
8926095f MS |
3025 | nelts = array_type_nelts_top (type); |
3026 | has_array = 1; | |
3027 | type = true_type = TREE_TYPE (type); | |
8d08fdba MS |
3028 | } |
3029 | ||
8926095f | 3030 | if (TYPE_READONLY (type) || TYPE_VOLATILE (type)) |
8d08fdba | 3031 | { |
8926095f MS |
3032 | pedwarn ("const and volatile types cannot be created with operator new"); |
3033 | type = true_type = TYPE_MAIN_VARIANT (type); | |
8d08fdba | 3034 | } |
8926095f | 3035 | |
8d08fdba MS |
3036 | /* If our base type is an array, then make sure we know how many elements |
3037 | it has. */ | |
3038 | while (TREE_CODE (true_type) == ARRAY_TYPE) | |
3039 | { | |
3040 | tree this_nelts = array_type_nelts_top (true_type); | |
8926095f | 3041 | nelts = build_binary_op (MULT_EXPR, nelts, this_nelts, 1); |
8d08fdba MS |
3042 | true_type = TREE_TYPE (true_type); |
3043 | } | |
3044 | if (has_array) | |
3045 | size = fold (build_binary_op (MULT_EXPR, size_in_bytes (true_type), | |
3046 | nelts, 1)); | |
3047 | else | |
3048 | size = size_in_bytes (type); | |
3049 | ||
8926095f MS |
3050 | if (TYPE_SIZE (true_type) == 0) |
3051 | { | |
3052 | if (true_type == void_type_node) | |
3053 | error ("invalid type for new: `void'"); | |
3054 | else | |
3055 | incomplete_type_error (0, true_type); | |
3056 | return error_mark_node; | |
3057 | } | |
3058 | ||
3059 | if (TYPE_LANG_SPECIFIC (true_type) | |
3060 | && CLASSTYPE_ABSTRACT_VIRTUALS (true_type)) | |
3061 | { | |
3062 | abstract_virtuals_error (NULL_TREE, true_type); | |
3063 | return error_mark_node; | |
3064 | } | |
3065 | ||
3066 | if (TYPE_LANG_SPECIFIC (true_type) && IS_SIGNATURE (true_type)) | |
3067 | { | |
3068 | signature_error (NULL_TREE, true_type); | |
3069 | return error_mark_node; | |
3070 | } | |
8d08fdba MS |
3071 | |
3072 | /* Get a little extra space to store a couple of things before the new'ed | |
3073 | array. */ | |
a28e3c7f | 3074 | if (has_array && TYPE_VEC_NEW_USES_COOKIE (true_type)) |
8d08fdba MS |
3075 | { |
3076 | tree extra = BI_header_size; | |
3077 | ||
3078 | size = size_binop (PLUS_EXPR, size, extra); | |
3079 | } | |
3080 | ||
a28e3c7f MS |
3081 | if (has_array) |
3082 | code = VEC_NEW_EXPR; | |
3083 | ||
8d08fdba | 3084 | /* Allocate the object. */ |
a28e3c7f MS |
3085 | if (! use_global_new && TYPE_LANG_SPECIFIC (true_type) |
3086 | && (TYPE_GETS_NEW (true_type) & (1 << has_array))) | |
3087 | rval = build_opfncall (code, LOOKUP_NORMAL, | |
8d08fdba MS |
3088 | TYPE_POINTER_TO (true_type), size, placement); |
3089 | else if (placement) | |
3090 | { | |
a28e3c7f | 3091 | rval = build_opfncall (code, LOOKUP_GLOBAL|LOOKUP_COMPLAIN, |
8d08fdba MS |
3092 | ptr_type_node, size, placement); |
3093 | rval = convert (TYPE_POINTER_TO (true_type), rval); | |
3094 | } | |
a28e3c7f | 3095 | else if (! has_array && flag_this_is_variable > 0 |
8d08fdba MS |
3096 | && TYPE_HAS_CONSTRUCTOR (true_type) && init != void_type_node) |
3097 | { | |
3098 | if (init == NULL_TREE || TREE_CODE (init) == TREE_LIST) | |
3099 | rval = NULL_TREE; | |
3100 | else | |
3101 | { | |
3102 | error ("constructors take parameter lists"); | |
3103 | return error_mark_node; | |
3104 | } | |
3105 | } | |
3106 | else | |
3107 | { | |
3108 | rval = build_builtin_call (build_pointer_type (true_type), | |
a28e3c7f MS |
3109 | has_array ? BIVN : BIN, |
3110 | build_tree_list (NULL_TREE, size)); | |
8d08fdba MS |
3111 | #if 0 |
3112 | /* See comment above as to why this is disabled. */ | |
3113 | if (alignment) | |
3114 | { | |
3115 | rval = build (PLUS_EXPR, TYPE_POINTER_TO (true_type), rval, | |
3116 | alignment); | |
3117 | rval = build (BIT_AND_EXPR, TYPE_POINTER_TO (true_type), | |
3118 | rval, build1 (BIT_NOT_EXPR, integer_type_node, | |
3119 | alignment)); | |
3120 | } | |
3121 | #endif | |
3122 | TREE_CALLS_NEW (rval) = 1; | |
8d08fdba MS |
3123 | } |
3124 | ||
3125 | /* if rval is NULL_TREE I don't have to allocate it, but are we totally | |
3126 | sure we have some extra bytes in that case for the BI_header_size | |
3127 | cookies? And how does that interact with the code below? (mrs) */ | |
3128 | /* Finish up some magic for new'ed arrays */ | |
a28e3c7f | 3129 | if (has_array && TYPE_VEC_NEW_USES_COOKIE (true_type) && rval != NULL_TREE) |
8d08fdba MS |
3130 | { |
3131 | tree extra = BI_header_size; | |
3132 | tree cookie, exp1; | |
3133 | rval = convert (ptr_type_node, rval); /* convert to void * first */ | |
3134 | rval = convert (string_type_node, rval); /* lets not add void* and ints */ | |
3135 | rval = save_expr (build_binary_op (PLUS_EXPR, rval, extra, 1)); | |
3136 | /* Store header info. */ | |
3137 | cookie = build_indirect_ref (build (MINUS_EXPR, TYPE_POINTER_TO (BI_header_type), | |
3138 | rval, extra), NULL_PTR); | |
3139 | exp1 = build (MODIFY_EXPR, void_type_node, | |
3140 | build_component_ref (cookie, nc_nelts_field_id, 0, 0), | |
3141 | nelts); | |
3142 | TREE_SIDE_EFFECTS (exp1) = 1; | |
3143 | rval = convert (build_pointer_type (true_type), rval); | |
3144 | TREE_CALLS_NEW (rval) = 1; | |
3145 | TREE_SIDE_EFFECTS (rval) = 1; | |
3146 | rval = build_compound_expr (tree_cons (NULL_TREE, exp1, | |
3147 | build_tree_list (NULL_TREE, rval))); | |
3148 | } | |
3149 | ||
3150 | /* We've figured out where the allocation is to go. | |
3151 | If we're not eliding constructors, then if a constructor | |
3152 | is defined, we must go through it. */ | |
3153 | if (!has_array && (rval == NULL_TREE || !flag_elide_constructors) | |
3154 | && TYPE_HAS_CONSTRUCTOR (true_type) && init != void_type_node) | |
3155 | { | |
3156 | tree newrval; | |
3157 | /* Constructors are never virtual. If it has an initialization, we | |
3158 | need to complain if we aren't allowed to use the ctor that took | |
3159 | that argument. */ | |
3160 | int flags = LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_COMPLAIN; | |
3161 | ||
3162 | /* If a copy constructor might work, set things up so that we can | |
3163 | try that after this. We deliberately don't clear LOOKUP_COMPLAIN | |
3164 | any more, since that would make it impossible to rationally use | |
3165 | the access of a constructor that matches perfectly. */ | |
3166 | #if 0 | |
3167 | if (rval != NULL_TREE) | |
3168 | flags |= LOOKUP_SPECULATIVELY; | |
3169 | #endif | |
3170 | ||
3171 | if (rval && TYPE_USES_VIRTUAL_BASECLASSES (true_type)) | |
3172 | { | |
3173 | init = tree_cons (NULL_TREE, integer_one_node, init); | |
3174 | flags |= LOOKUP_HAS_IN_CHARGE; | |
3175 | } | |
3176 | ||
3177 | { | |
3178 | tree tmp = rval; | |
3179 | ||
3180 | if (TREE_CODE (TREE_TYPE (tmp)) == POINTER_TYPE) | |
3181 | tmp = build_indirect_ref (tmp, NULL_PTR); | |
3182 | ||
3183 | newrval = build_method_call (tmp, constructor_name_full (true_type), | |
3184 | init, NULL_TREE, flags); | |
3185 | } | |
3186 | ||
3187 | if (newrval) | |
3188 | { | |
3189 | rval = newrval; | |
3190 | TREE_HAS_CONSTRUCTOR (rval) = 1; | |
3191 | } | |
3192 | else | |
3193 | rval = error_mark_node; | |
3194 | goto done; | |
3195 | } | |
3196 | ||
3197 | if (rval == error_mark_node) | |
3198 | return error_mark_node; | |
3199 | rval = save_expr (rval); | |
3200 | TREE_HAS_CONSTRUCTOR (rval) = 1; | |
3201 | ||
3202 | /* Don't call any constructors or do any initialization. */ | |
3203 | if (init == void_type_node) | |
3204 | goto done; | |
3205 | ||
8926095f | 3206 | if (TYPE_NEEDS_CONSTRUCTING (type) || init) |
8d08fdba MS |
3207 | { |
3208 | if (! TYPE_NEEDS_CONSTRUCTING (type) && ! IS_AGGR_TYPE (type)) | |
3209 | { | |
3210 | /* New 2.0 interpretation: `new int (10)' means | |
3211 | allocate an int, and initialize it with 10. */ | |
3212 | ||
3213 | init = build_c_cast (type, init); | |
3214 | rval = build (COMPOUND_EXPR, TREE_TYPE (rval), | |
3215 | build_modify_expr (build_indirect_ref (rval, NULL_PTR), | |
3216 | NOP_EXPR, init), | |
3217 | rval); | |
3218 | TREE_SIDE_EFFECTS (rval) = 1; | |
3219 | TREE_CALLS_NEW (rval) = 1; | |
3220 | } | |
3221 | else if (current_function_decl == NULL_TREE) | |
3222 | { | |
3223 | extern tree static_aggregates; | |
3224 | ||
3225 | /* In case of static initialization, SAVE_EXPR is good enough. */ | |
3226 | init = copy_to_permanent (init); | |
3227 | rval = copy_to_permanent (rval); | |
3228 | static_aggregates = perm_tree_cons (init, rval, static_aggregates); | |
3229 | } | |
3230 | else | |
3231 | { | |
3232 | /* Have to wrap this in RTL_EXPR for two cases: | |
3233 | in base or member initialization and if we | |
3234 | are a branch of a ?: operator. Since we | |
3235 | can't easily know the latter, just do it always. */ | |
3236 | tree xval = make_node (RTL_EXPR); | |
3237 | ||
3238 | TREE_TYPE (xval) = TREE_TYPE (rval); | |
3239 | do_pending_stack_adjust (); | |
3240 | start_sequence_for_rtl_expr (xval); | |
3241 | ||
3242 | /* As a matter of principle, `start_sequence' should do this. */ | |
3243 | emit_note (0, -1); | |
3244 | ||
3245 | if (has_array) | |
3246 | rval = expand_vec_init (decl, rval, | |
3247 | build_binary_op (MINUS_EXPR, nelts, integer_one_node, 1), | |
3248 | init, 0); | |
3249 | else | |
3250 | expand_aggr_init (build_indirect_ref (rval, NULL_PTR), init, 0); | |
3251 | ||
3252 | do_pending_stack_adjust (); | |
3253 | ||
3254 | TREE_SIDE_EFFECTS (xval) = 1; | |
3255 | TREE_CALLS_NEW (xval) = 1; | |
3256 | RTL_EXPR_SEQUENCE (xval) = get_insns (); | |
3257 | end_sequence (); | |
3258 | ||
3259 | if (TREE_CODE (rval) == SAVE_EXPR) | |
3260 | { | |
3261 | /* Errors may cause this to not get evaluated. */ | |
3262 | if (SAVE_EXPR_RTL (rval) == 0) | |
3263 | SAVE_EXPR_RTL (rval) = const0_rtx; | |
3264 | RTL_EXPR_RTL (xval) = SAVE_EXPR_RTL (rval); | |
3265 | } | |
3266 | else | |
3267 | { | |
3268 | my_friendly_assert (TREE_CODE (rval) == VAR_DECL, 217); | |
3269 | RTL_EXPR_RTL (xval) = DECL_RTL (rval); | |
3270 | } | |
3271 | rval = xval; | |
3272 | } | |
3273 | } | |
3274 | done: | |
51c184be MS |
3275 | if (rval && TREE_TYPE (rval) != build_pointer_type (type)) |
3276 | { | |
3277 | /* The type of new int [3][3] is not int *, but int [3] * */ | |
3278 | rval = build_c_cast (build_pointer_type (type), rval); | |
3279 | } | |
3280 | ||
8d08fdba MS |
3281 | if (pending_sizes) |
3282 | rval = build_compound_expr (chainon (pending_sizes, | |
3283 | build_tree_list (NULL_TREE, rval))); | |
3284 | ||
3285 | if (flag_gc) | |
3286 | { | |
3287 | extern tree gc_visible; | |
3288 | tree objbits; | |
3289 | tree update_expr; | |
3290 | ||
3291 | rval = save_expr (rval); | |
3292 | /* We don't need a `headof' operation to do this because | |
3293 | we know where the object starts. */ | |
3294 | objbits = build1 (INDIRECT_REF, unsigned_type_node, | |
3295 | build (MINUS_EXPR, ptr_type_node, | |
3296 | rval, c_sizeof_nowarn (unsigned_type_node))); | |
3297 | update_expr = build_modify_expr (objbits, BIT_IOR_EXPR, gc_visible); | |
3298 | rval = build_compound_expr (tree_cons (NULL_TREE, rval, | |
3299 | tree_cons (NULL_TREE, update_expr, | |
3300 | build_tree_list (NULL_TREE, rval)))); | |
3301 | } | |
3302 | ||
3303 | return save_expr (rval); | |
3304 | } | |
3305 | \f | |
3306 | /* `expand_vec_init' performs initialization of a vector of aggregate | |
3307 | types. | |
3308 | ||
3309 | DECL is passed only for error reporting, and provides line number | |
3310 | and source file name information. | |
3311 | BASE is the space where the vector will be. | |
3312 | MAXINDEX is the maximum index of the array (one less than the | |
3313 | number of elements). | |
3314 | INIT is the (possibly NULL) initializer. | |
3315 | ||
3316 | FROM_ARRAY is 0 if we should init everything with INIT | |
3317 | (i.e., every element initialized from INIT). | |
3318 | FROM_ARRAY is 1 if we should index into INIT in parallel | |
3319 | with initialization of DECL. | |
3320 | FROM_ARRAY is 2 if we should index into INIT in parallel, | |
3321 | but use assignment instead of initialization. */ | |
3322 | ||
3323 | tree | |
3324 | expand_vec_init (decl, base, maxindex, init, from_array) | |
3325 | tree decl, base, maxindex, init; | |
3326 | int from_array; | |
3327 | { | |
3328 | tree rval; | |
3329 | tree iterator, base2 = NULL_TREE; | |
3330 | tree type = TREE_TYPE (TREE_TYPE (base)); | |
3331 | tree size; | |
3332 | ||
3333 | maxindex = convert (integer_type_node, maxindex); | |
3334 | if (maxindex == error_mark_node) | |
3335 | return error_mark_node; | |
3336 | ||
3337 | if (current_function_decl == NULL_TREE) | |
3338 | { | |
3339 | rval = make_tree_vec (3); | |
3340 | TREE_VEC_ELT (rval, 0) = base; | |
3341 | TREE_VEC_ELT (rval, 1) = maxindex; | |
3342 | TREE_VEC_ELT (rval, 2) = init; | |
3343 | return rval; | |
3344 | } | |
3345 | ||
3346 | size = size_in_bytes (type); | |
3347 | ||
3348 | /* Set to zero in case size is <= 0. Optimizer will delete this if | |
3349 | it is not needed. */ | |
3350 | rval = get_temp_regvar (TYPE_POINTER_TO (type), | |
3351 | convert (TYPE_POINTER_TO (type), null_pointer_node)); | |
3352 | base = default_conversion (base); | |
3353 | base = convert (TYPE_POINTER_TO (type), base); | |
3354 | expand_assignment (rval, base, 0, 0); | |
3355 | base = get_temp_regvar (TYPE_POINTER_TO (type), base); | |
3356 | ||
3357 | if (init != NULL_TREE | |
3358 | && TREE_CODE (init) == CONSTRUCTOR | |
3359 | && TREE_TYPE (init) == TREE_TYPE (decl)) | |
3360 | { | |
3361 | /* Initialization of array from {...}. */ | |
3362 | tree elts = CONSTRUCTOR_ELTS (init); | |
3363 | tree baseref = build1 (INDIRECT_REF, type, base); | |
3364 | tree baseinc = build (PLUS_EXPR, TYPE_POINTER_TO (type), base, size); | |
3365 | int host_i = TREE_INT_CST_LOW (maxindex); | |
3366 | ||
3367 | if (IS_AGGR_TYPE (type)) | |
3368 | { | |
3369 | while (elts) | |
3370 | { | |
3371 | host_i -= 1; | |
3372 | expand_aggr_init (baseref, TREE_VALUE (elts), 0); | |
3373 | ||
3374 | expand_assignment (base, baseinc, 0, 0); | |
3375 | elts = TREE_CHAIN (elts); | |
3376 | } | |
3377 | /* Initialize any elements by default if possible. */ | |
3378 | if (host_i >= 0) | |
3379 | { | |
3380 | if (TYPE_NEEDS_CONSTRUCTING (type) == 0) | |
3381 | { | |
3382 | if (obey_regdecls) | |
3383 | use_variable (DECL_RTL (base)); | |
3384 | goto done_init; | |
3385 | } | |
3386 | ||
3387 | iterator = get_temp_regvar (integer_type_node, | |
3388 | build_int_2 (host_i, 0)); | |
3389 | init = NULL_TREE; | |
3390 | goto init_by_default; | |
3391 | } | |
3392 | } | |
3393 | else | |
3394 | while (elts) | |
3395 | { | |
3396 | expand_assignment (baseref, TREE_VALUE (elts), 0, 0); | |
3397 | ||
3398 | expand_assignment (base, baseinc, 0, 0); | |
3399 | elts = TREE_CHAIN (elts); | |
3400 | } | |
3401 | ||
3402 | if (obey_regdecls) | |
3403 | use_variable (DECL_RTL (base)); | |
3404 | } | |
3405 | else | |
3406 | { | |
3407 | tree itype; | |
3408 | ||
3409 | iterator = get_temp_regvar (integer_type_node, maxindex); | |
3410 | ||
3411 | init_by_default: | |
3412 | ||
3413 | /* If initializing one array from another, | |
3414 | initialize element by element. */ | |
3415 | if (from_array) | |
3416 | { | |
3417 | /* We rely upon the below calls the do argument checking */ | |
3418 | if (decl == NULL_TREE) | |
3419 | { | |
3420 | sorry ("initialization of array from dissimilar array type"); | |
3421 | return error_mark_node; | |
3422 | } | |
3423 | if (init) | |
3424 | { | |
3425 | base2 = default_conversion (init); | |
3426 | itype = TREE_TYPE (base2); | |
3427 | base2 = get_temp_regvar (itype, base2); | |
3428 | itype = TREE_TYPE (itype); | |
3429 | } | |
3430 | else if (TYPE_LANG_SPECIFIC (type) | |
3431 | && TYPE_NEEDS_CONSTRUCTING (type) | |
3432 | && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (type)) | |
3433 | { | |
3434 | error ("initializer ends prematurely"); | |
3435 | return error_mark_node; | |
3436 | } | |
3437 | } | |
3438 | ||
3439 | expand_start_cond (build (GE_EXPR, integer_type_node, | |
3440 | iterator, integer_zero_node), 0); | |
3441 | expand_start_loop_continue_elsewhere (1); | |
3442 | ||
3443 | if (from_array) | |
3444 | { | |
3445 | tree to = build1 (INDIRECT_REF, type, base); | |
3446 | tree from; | |
3447 | ||
3448 | if (base2) | |
3449 | from = build1 (INDIRECT_REF, itype, base2); | |
3450 | else | |
3451 | from = NULL_TREE; | |
3452 | ||
3453 | if (from_array == 2) | |
3454 | expand_expr_stmt (build_modify_expr (to, NOP_EXPR, from)); | |
3455 | else if (TYPE_NEEDS_CONSTRUCTING (type)) | |
3456 | expand_aggr_init (to, from, 0); | |
3457 | else if (from) | |
3458 | expand_assignment (to, from, 0, 0); | |
3459 | else | |
3460 | my_friendly_abort (57); | |
3461 | } | |
3462 | else if (TREE_CODE (type) == ARRAY_TYPE) | |
3463 | { | |
3464 | if (init != 0) | |
3465 | sorry ("cannot initialize multi-dimensional array with initializer"); | |
3466 | expand_vec_init (decl, build1 (NOP_EXPR, TYPE_POINTER_TO (TREE_TYPE (type)), base), | |
3467 | array_type_nelts (type), 0, 0); | |
3468 | } | |
3469 | else | |
3470 | expand_aggr_init (build1 (INDIRECT_REF, type, base), init, 0); | |
3471 | ||
3472 | expand_assignment (base, | |
3473 | build (PLUS_EXPR, TYPE_POINTER_TO (type), base, size), | |
3474 | 0, 0); | |
3475 | if (base2) | |
3476 | expand_assignment (base2, | |
3477 | build (PLUS_EXPR, TYPE_POINTER_TO (type), base2, size), 0, 0); | |
3478 | expand_loop_continue_here (); | |
3479 | expand_exit_loop_if_false (0, build (NE_EXPR, integer_type_node, | |
3480 | build (PREDECREMENT_EXPR, integer_type_node, iterator, integer_one_node), minus_one)); | |
3481 | ||
3482 | if (obey_regdecls) | |
3483 | { | |
3484 | use_variable (DECL_RTL (base)); | |
3485 | if (base2) | |
3486 | use_variable (DECL_RTL (base2)); | |
3487 | } | |
3488 | expand_end_loop (); | |
3489 | expand_end_cond (); | |
3490 | if (obey_regdecls) | |
3491 | use_variable (DECL_RTL (iterator)); | |
3492 | } | |
3493 | done_init: | |
3494 | ||
3495 | if (obey_regdecls) | |
3496 | use_variable (DECL_RTL (rval)); | |
3497 | return rval; | |
3498 | } | |
3499 | ||
3500 | /* Free up storage of type TYPE, at address ADDR. | |
3501 | ||
3502 | TYPE is a POINTER_TYPE and can be ptr_type_node for no special type | |
3503 | of pointer. | |
3504 | ||
3505 | VIRTUAL_SIZE is the amount of storage that was allocated, and is | |
3506 | used as the second argument to operator delete. It can include | |
3507 | things like padding and magic size cookies. It has virtual in it, | |
3508 | because if you have a base pointer and you delete through a virtual | |
3509 | destructor, it should be the size of the dynamic object, not the | |
3510 | static object, see Free Store 12.5 ANSI C++ WP. | |
3511 | ||
3512 | This does not call any destructors. */ | |
3513 | tree | |
a28e3c7f | 3514 | build_x_delete (type, addr, which_delete, virtual_size) |
8d08fdba | 3515 | tree type, addr; |
a28e3c7f | 3516 | int which_delete; |
8d08fdba MS |
3517 | tree virtual_size; |
3518 | { | |
a28e3c7f MS |
3519 | int use_global_delete = which_delete & 1; |
3520 | int use_vec_delete = !!(which_delete & 2); | |
8d08fdba | 3521 | tree rval; |
a28e3c7f | 3522 | enum tree_code code = use_vec_delete ? VEC_DELETE_EXPR : DELETE_EXPR; |
8d08fdba | 3523 | |
a28e3c7f MS |
3524 | if (! use_global_delete && TYPE_LANG_SPECIFIC (TREE_TYPE (type)) |
3525 | && (TYPE_GETS_DELETE (TREE_TYPE (type)) & (1 << use_vec_delete))) | |
3526 | rval = build_opfncall (code, LOOKUP_NORMAL, addr, virtual_size, NULL_TREE); | |
8d08fdba | 3527 | else |
a28e3c7f | 3528 | rval = build_builtin_call (void_type_node, use_vec_delete ? BIVD : BID, |
8d08fdba MS |
3529 | build_tree_list (NULL_TREE, addr)); |
3530 | return rval; | |
3531 | } | |
3532 | ||
3533 | /* Generate a call to a destructor. TYPE is the type to cast ADDR to. | |
3534 | ADDR is an expression which yields the store to be destroyed. | |
3535 | AUTO_DELETE is nonzero if a call to DELETE should be made or not. | |
3536 | If in the program, (AUTO_DELETE & 2) is non-zero, we tear down the | |
3537 | virtual baseclasses. | |
3538 | If in the program, (AUTO_DELETE & 1) is non-zero, then we deallocate. | |
3539 | ||
3540 | FLAGS is the logical disjunction of zero or more LOOKUP_ | |
3541 | flags. See cp-tree.h for more info. | |
3542 | ||
3543 | This function does not delete an object's virtual base classes. */ | |
3544 | tree | |
3545 | build_delete (type, addr, auto_delete, flags, use_global_delete) | |
3546 | tree type, addr; | |
3547 | tree auto_delete; | |
3548 | int flags; | |
3549 | int use_global_delete; | |
3550 | { | |
3551 | tree function, parms; | |
3552 | tree member; | |
3553 | tree expr; | |
3554 | tree ref; | |
3555 | int ptr; | |
3556 | ||
3557 | if (addr == error_mark_node) | |
3558 | return error_mark_node; | |
3559 | ||
3560 | /* Can happen when CURRENT_EXCEPTION_OBJECT gets its type | |
3561 | set to `error_mark_node' before it gets properly cleaned up. */ | |
3562 | if (type == error_mark_node) | |
3563 | return error_mark_node; | |
3564 | ||
3565 | type = TYPE_MAIN_VARIANT (type); | |
3566 | ||
3567 | if (TREE_CODE (type) == POINTER_TYPE) | |
3568 | { | |
3569 | type = TREE_TYPE (type); | |
3570 | if (TYPE_SIZE (type) == 0) | |
3571 | { | |
3572 | incomplete_type_error (0, type); | |
3573 | return error_mark_node; | |
3574 | } | |
3575 | if (TREE_CODE (type) == ARRAY_TYPE) | |
3576 | goto handle_array; | |
3577 | if (! IS_AGGR_TYPE (type)) | |
3578 | { | |
3579 | /* Call the builtin operator delete. */ | |
3580 | return build_builtin_call (void_type_node, BID, | |
3581 | build_tree_list (NULL_TREE, addr)); | |
3582 | } | |
3583 | if (TREE_SIDE_EFFECTS (addr)) | |
3584 | addr = save_expr (addr); | |
3585 | ref = build_indirect_ref (addr, NULL_PTR); | |
3586 | ptr = 1; | |
3587 | } | |
3588 | else if (TREE_CODE (type) == ARRAY_TYPE) | |
3589 | { | |
3590 | handle_array: | |
3591 | if (TREE_SIDE_EFFECTS (addr)) | |
3592 | addr = save_expr (addr); | |
3593 | return build_vec_delete (addr, array_type_nelts (type), | |
3594 | c_sizeof_nowarn (TREE_TYPE (type)), | |
a28e3c7f MS |
3595 | auto_delete, integer_two_node, |
3596 | use_global_delete); | |
8d08fdba MS |
3597 | } |
3598 | else | |
3599 | { | |
3600 | /* Don't check PROTECT here; leave that decision to the | |
3601 | destructor. If the destructor is accessible, call it, | |
3602 | else report error. */ | |
3603 | addr = build_unary_op (ADDR_EXPR, addr, 0); | |
3604 | if (TREE_SIDE_EFFECTS (addr)) | |
3605 | addr = save_expr (addr); | |
3606 | ||
3607 | if (TREE_CONSTANT (addr)) | |
3608 | addr = convert_pointer_to (type, addr); | |
3609 | else | |
3610 | addr = convert_force (build_pointer_type (type), addr); | |
3611 | ||
3612 | if (TREE_CODE (addr) == NOP_EXPR | |
3613 | && TREE_OPERAND (addr, 0) == current_class_decl) | |
3614 | ref = C_C_D; | |
3615 | else | |
3616 | ref = build_indirect_ref (addr, NULL_PTR); | |
3617 | ptr = 0; | |
3618 | } | |
3619 | ||
3620 | my_friendly_assert (IS_AGGR_TYPE (type), 220); | |
3621 | ||
3622 | if (! TYPE_NEEDS_DESTRUCTOR (type)) | |
3623 | { | |
8d08fdba MS |
3624 | if (auto_delete == integer_zero_node) |
3625 | return void_zero_node; | |
3626 | ||
3627 | /* Pass the size of the object down to the operator delete() in | |
3628 | addition to the ADDR. */ | |
a28e3c7f | 3629 | if (TYPE_GETS_REG_DELETE (type) && !use_global_delete) |
8d08fdba | 3630 | { |
8d08fdba MS |
3631 | tree virtual_size = c_sizeof_nowarn (type); |
3632 | return build_opfncall (DELETE_EXPR, LOOKUP_NORMAL, addr, | |
3633 | virtual_size, NULL_TREE); | |
3634 | } | |
3635 | ||
3636 | /* Call the builtin operator delete. */ | |
3637 | return build_builtin_call (void_type_node, BID, | |
3638 | build_tree_list (NULL_TREE, addr)); | |
3639 | } | |
3640 | parms = build_tree_list (NULL_TREE, addr); | |
3641 | ||
3642 | /* Below, we will reverse the order in which these calls are made. | |
3643 | If we have a destructor, then that destructor will take care | |
3644 | of the base classes; otherwise, we must do that here. */ | |
3645 | if (TYPE_HAS_DESTRUCTOR (type)) | |
3646 | { | |
3647 | tree dtor = DECL_MAIN_VARIANT (TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type), 0)); | |
3648 | tree basetypes = TYPE_BINFO (type); | |
700f8a87 MS |
3649 | tree passed_auto_delete; |
3650 | tree do_delete = NULL_TREE; | |
3651 | ||
3652 | if (use_global_delete) | |
3653 | { | |
3654 | tree cond = fold (build (BIT_AND_EXPR, integer_type_node, | |
3655 | auto_delete, integer_one_node)); | |
3656 | tree call = build_builtin_call | |
3657 | (void_type_node, BID, build_tree_list (NULL_TREE, addr)); | |
3658 | ||
3659 | cond = fold (build (COND_EXPR, void_type_node, cond, | |
3660 | call, void_zero_node)); | |
3661 | if (cond != void_zero_node) | |
3662 | do_delete = cond; | |
3663 | ||
3664 | passed_auto_delete = fold (build (BIT_AND_EXPR, integer_type_node, | |
3665 | auto_delete, integer_two_node)); | |
3666 | } | |
3667 | else | |
3668 | passed_auto_delete = auto_delete; | |
8d08fdba MS |
3669 | |
3670 | if (flags & LOOKUP_PROTECT) | |
3671 | { | |
3672 | enum access_type access = compute_access (basetypes, dtor); | |
3673 | ||
3674 | if (access == access_private) | |
3675 | { | |
3676 | if (flags & LOOKUP_COMPLAIN) | |
3677 | cp_error ("destructor for type `%T' is private in this scope", type); | |
3678 | return error_mark_node; | |
3679 | } | |
3680 | else if (access == access_protected) | |
3681 | { | |
3682 | if (flags & LOOKUP_COMPLAIN) | |
3683 | cp_error ("destructor for type `%T' is protected in this scope", type); | |
3684 | return error_mark_node; | |
3685 | } | |
3686 | } | |
3687 | ||
3688 | /* Once we are in a destructor, try not going through | |
3689 | the virtual function table to find the next destructor. */ | |
3690 | if (DECL_VINDEX (dtor) | |
3691 | && ! (flags & LOOKUP_NONVIRTUAL) | |
3692 | && TREE_CODE (auto_delete) != PARM_DECL | |
3693 | && (ptr == 1 || ! resolves_to_fixed_type_p (ref, 0))) | |
3694 | { | |
3695 | tree binfo, basetype; | |
3696 | /* The code below is probably all broken. See call.c for the | |
3697 | complete right way to do this. this offsets may not be right | |
3698 | in the below. (mrs) */ | |
3699 | /* This destructor must be called via virtual function table. */ | |
3700 | dtor = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (DECL_CONTEXT (dtor)), 0); | |
3701 | basetype = DECL_CLASS_CONTEXT (dtor); | |
3702 | binfo = get_binfo (basetype, | |
3703 | TREE_TYPE (TREE_TYPE (TREE_VALUE (parms))), | |
3704 | 0); | |
3705 | expr = convert_pointer_to_real (binfo, TREE_VALUE (parms)); | |
3706 | if (expr != TREE_VALUE (parms)) | |
3707 | { | |
3708 | expr = fold (expr); | |
3709 | ref = build_indirect_ref (expr, NULL_PTR); | |
3710 | TREE_VALUE (parms) = expr; | |
3711 | } | |
3712 | function = build_vfn_ref (&TREE_VALUE (parms), ref, DECL_VINDEX (dtor)); | |
3713 | if (function == error_mark_node) | |
3714 | return error_mark_node; | |
3715 | TREE_TYPE (function) = build_pointer_type (TREE_TYPE (dtor)); | |
700f8a87 | 3716 | TREE_CHAIN (parms) = build_tree_list (NULL_TREE, passed_auto_delete); |
8d08fdba | 3717 | expr = build_function_call (function, parms); |
700f8a87 MS |
3718 | if (do_delete) |
3719 | expr = build (COMPOUND_EXPR, void_type_node, expr, do_delete); | |
8d08fdba MS |
3720 | if (ptr && (flags & LOOKUP_DESTRUCTOR) == 0) |
3721 | { | |
3722 | /* Handle the case where a virtual destructor is | |
3723 | being called on an item that is 0. | |
3724 | ||
3725 | @@ Does this really need to be done? */ | |
3726 | tree ifexp = build_binary_op(NE_EXPR, addr, integer_zero_node,1); | |
3727 | #if 0 | |
3728 | if (TREE_CODE (ref) == VAR_DECL | |
3729 | || TREE_CODE (ref) == COMPONENT_REF) | |
3730 | warning ("losing in build_delete"); | |
3731 | #endif | |
3732 | expr = build (COND_EXPR, void_type_node, | |
3733 | ifexp, expr, void_zero_node); | |
3734 | } | |
3735 | } | |
3736 | else | |
3737 | { | |
3738 | tree ifexp; | |
3739 | ||
3740 | if ((flags & LOOKUP_DESTRUCTOR) | |
3741 | || TREE_CODE (ref) == VAR_DECL | |
3742 | || TREE_CODE (ref) == PARM_DECL | |
3743 | || TREE_CODE (ref) == COMPONENT_REF | |
3744 | || TREE_CODE (ref) == ARRAY_REF) | |
3745 | /* These can't be 0. */ | |
3746 | ifexp = integer_one_node; | |
3747 | else | |
3748 | /* Handle the case where a non-virtual destructor is | |
3749 | being called on an item that is 0. */ | |
3750 | ifexp = build_binary_op (NE_EXPR, addr, integer_zero_node, 1); | |
3751 | ||
3752 | /* Used to mean that this destructor was known to be empty, | |
3753 | but that's now obsolete. */ | |
3754 | my_friendly_assert (DECL_INITIAL (dtor) != void_type_node, 221); | |
3755 | ||
700f8a87 | 3756 | TREE_CHAIN (parms) = build_tree_list (NULL_TREE, passed_auto_delete); |
8d08fdba | 3757 | expr = build_function_call (dtor, parms); |
700f8a87 MS |
3758 | if (do_delete) |
3759 | expr = build (COMPOUND_EXPR, void_type_node, expr, do_delete); | |
8d08fdba MS |
3760 | |
3761 | if (ifexp != integer_one_node) | |
3762 | expr = build (COND_EXPR, void_type_node, | |
3763 | ifexp, expr, void_zero_node); | |
3764 | } | |
3765 | return expr; | |
3766 | } | |
3767 | else | |
3768 | { | |
3769 | /* This can get visibilities wrong. */ | |
3770 | tree binfos = BINFO_BASETYPES (TYPE_BINFO (type)); | |
3771 | int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0; | |
3772 | tree base_binfo = n_baseclasses > 0 ? TREE_VEC_ELT (binfos, 0) : NULL_TREE; | |
3773 | tree exprstmt = NULL_TREE; | |
3774 | tree parent_auto_delete = auto_delete; | |
3775 | tree cond; | |
3776 | ||
3777 | /* If this type does not have a destructor, but does have | |
3778 | operator delete, call the parent parent destructor (if any), | |
3779 | but let this node do the deleting. Otherwise, it is ok | |
3780 | to let the parent destructor do the deleting. */ | |
a28e3c7f | 3781 | if (TYPE_GETS_REG_DELETE (type) && !use_global_delete) |
8d08fdba MS |
3782 | { |
3783 | parent_auto_delete = integer_zero_node; | |
3784 | if (auto_delete == integer_zero_node) | |
3785 | cond = NULL_TREE; | |
3786 | else | |
3787 | { | |
3788 | tree virtual_size; | |
3789 | ||
3790 | /* This is probably wrong. It should be the size of the | |
3791 | virtual object being deleted. */ | |
3792 | virtual_size = c_sizeof_nowarn (type); | |
3793 | ||
3794 | expr = build_opfncall (DELETE_EXPR, LOOKUP_NORMAL, addr, | |
3795 | virtual_size, NULL_TREE); | |
3796 | if (expr == error_mark_node) | |
3797 | return error_mark_node; | |
3798 | if (auto_delete != integer_one_node) | |
3799 | cond = build (COND_EXPR, void_type_node, | |
3800 | build (BIT_AND_EXPR, integer_type_node, | |
3801 | auto_delete, integer_one_node), | |
3802 | expr, void_zero_node); | |
3803 | else | |
3804 | cond = expr; | |
3805 | } | |
3806 | } | |
3807 | else if (base_binfo == NULL_TREE | |
3808 | || (TREE_VIA_VIRTUAL (base_binfo) == 0 | |
3809 | && ! TYPE_NEEDS_DESTRUCTOR (BINFO_TYPE (base_binfo)))) | |
3810 | { | |
3811 | tree virtual_size; | |
3812 | ||
3813 | /* This is probably wrong. It should be the size of the virtual | |
3814 | object being deleted. */ | |
3815 | virtual_size = c_sizeof_nowarn (type); | |
3816 | ||
3817 | cond = build (COND_EXPR, void_type_node, | |
3818 | build (BIT_AND_EXPR, integer_type_node, auto_delete, integer_one_node), | |
3819 | build_builtin_call (void_type_node, BID, | |
3820 | build_tree_list (NULL_TREE, addr)), | |
3821 | void_zero_node); | |
3822 | } | |
3823 | else | |
3824 | cond = NULL_TREE; | |
3825 | ||
3826 | if (cond) | |
3827 | exprstmt = build_tree_list (NULL_TREE, cond); | |
3828 | ||
3829 | if (base_binfo | |
3830 | && ! TREE_VIA_VIRTUAL (base_binfo) | |
3831 | && TYPE_NEEDS_DESTRUCTOR (BINFO_TYPE (base_binfo))) | |
3832 | { | |
3833 | tree this_auto_delete; | |
3834 | ||
3835 | if (BINFO_OFFSET_ZEROP (base_binfo)) | |
3836 | this_auto_delete = parent_auto_delete; | |
3837 | else | |
3838 | this_auto_delete = integer_zero_node; | |
3839 | ||
3840 | expr = build_delete (TYPE_POINTER_TO (BINFO_TYPE (base_binfo)), addr, | |
3841 | this_auto_delete, flags, 0); | |
3842 | exprstmt = tree_cons (NULL_TREE, expr, exprstmt); | |
3843 | } | |
3844 | ||
3845 | /* Take care of the remaining baseclasses. */ | |
3846 | for (i = 1; i < n_baseclasses; i++) | |
3847 | { | |
3848 | base_binfo = TREE_VEC_ELT (binfos, i); | |
3849 | if (! TYPE_NEEDS_DESTRUCTOR (BINFO_TYPE (base_binfo)) | |
3850 | || TREE_VIA_VIRTUAL (base_binfo)) | |
3851 | continue; | |
3852 | ||
3853 | /* May be zero offset if other baseclasses are virtual. */ | |
3854 | expr = fold (build (PLUS_EXPR, TYPE_POINTER_TO (BINFO_TYPE (base_binfo)), | |
3855 | addr, BINFO_OFFSET (base_binfo))); | |
3856 | ||
3857 | expr = build_delete (TYPE_POINTER_TO (BINFO_TYPE (base_binfo)), expr, | |
3858 | integer_zero_node, | |
3859 | flags, 0); | |
3860 | ||
3861 | exprstmt = tree_cons (NULL_TREE, expr, exprstmt); | |
3862 | } | |
3863 | ||
3864 | for (member = TYPE_FIELDS (type); member; member = TREE_CHAIN (member)) | |
3865 | { | |
3866 | if (TREE_CODE (member) != FIELD_DECL) | |
3867 | continue; | |
3868 | if (TYPE_NEEDS_DESTRUCTOR (TREE_TYPE (member))) | |
3869 | { | |
3870 | tree this_member = build_component_ref (ref, DECL_NAME (member), 0, 0); | |
3871 | tree this_type = TREE_TYPE (member); | |
3872 | expr = build_delete (this_type, this_member, integer_two_node, flags, 0); | |
3873 | exprstmt = tree_cons (NULL_TREE, expr, exprstmt); | |
3874 | } | |
3875 | } | |
3876 | ||
3877 | if (exprstmt) | |
3878 | return build_compound_expr (exprstmt); | |
3879 | /* Virtual base classes make this function do nothing. */ | |
3880 | return void_zero_node; | |
3881 | } | |
3882 | } | |
3883 | ||
3884 | /* For type TYPE, delete the virtual baseclass objects of DECL. */ | |
3885 | ||
3886 | tree | |
3887 | build_vbase_delete (type, decl) | |
3888 | tree type, decl; | |
3889 | { | |
3890 | tree vbases = CLASSTYPE_VBASECLASSES (type); | |
3891 | tree result = NULL_TREE; | |
3892 | tree addr = build_unary_op (ADDR_EXPR, decl, 0); | |
3893 | ||
3894 | my_friendly_assert (addr != error_mark_node, 222); | |
3895 | ||
3896 | while (vbases) | |
3897 | { | |
3898 | tree this_addr = convert_force (TYPE_POINTER_TO (BINFO_TYPE (vbases)), | |
3899 | addr); | |
3900 | result = tree_cons (NULL_TREE, | |
3901 | build_delete (TREE_TYPE (this_addr), this_addr, | |
3902 | integer_zero_node, | |
3903 | LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 0), | |
3904 | result); | |
3905 | vbases = TREE_CHAIN (vbases); | |
3906 | } | |
3907 | return build_compound_expr (nreverse (result)); | |
3908 | } | |
3909 | ||
3910 | /* Build a C++ vector delete expression. | |
3911 | MAXINDEX is the number of elements to be deleted. | |
3912 | ELT_SIZE is the nominal size of each element in the vector. | |
3913 | BASE is the expression that should yield the store to be deleted. | |
8d08fdba MS |
3914 | This function expands (or synthesizes) these calls itself. |
3915 | AUTO_DELETE_VEC says whether the container (vector) should be deallocated. | |
3916 | AUTO_DELETE say whether each item in the container should be deallocated. | |
3917 | ||
3918 | This also calls delete for virtual baseclasses of elements of the vector. | |
3919 | ||
3920 | Update: MAXINDEX is no longer needed. The size can be extracted from the | |
3921 | start of the vector for pointers, and from the type for arrays. We still | |
3922 | use MAXINDEX for arrays because it happens to already have one of the | |
3923 | values we'd have to extract. (We could use MAXINDEX with pointers to | |
3924 | confirm the size, and trap if the numbers differ; not clear that it'd | |
3925 | be worth bothering.) */ | |
3926 | tree | |
a28e3c7f MS |
3927 | build_vec_delete (base, maxindex, elt_size, auto_delete_vec, auto_delete, |
3928 | use_global_delete) | |
8d08fdba | 3929 | tree base, maxindex, elt_size; |
8d08fdba | 3930 | tree auto_delete_vec, auto_delete; |
a28e3c7f | 3931 | int use_global_delete; |
8d08fdba MS |
3932 | { |
3933 | tree ptype = TREE_TYPE (base); | |
3934 | tree type; | |
3935 | tree virtual_size; | |
3936 | /* Temporary variables used by the loop. */ | |
3937 | tree tbase, size_exp, tbase_init; | |
3938 | ||
3939 | /* This is the body of the loop that implements the deletion of a | |
3940 | single element, and moves temp variables to next elements. */ | |
3941 | tree body; | |
3942 | ||
3943 | /* This is the LOOP_EXPR that governs the deletion of the elements. */ | |
3944 | tree loop; | |
3945 | ||
3946 | /* This is the thing that governs what to do after the loop has run. */ | |
3947 | tree deallocate_expr = 0; | |
3948 | ||
3949 | /* This is the BIND_EXPR which holds the outermost iterator of the | |
3950 | loop. It is convenient to set this variable up and test it before | |
3951 | executing any other code in the loop. | |
3952 | This is also the containing expression returned by this function. */ | |
3953 | tree controller = NULL_TREE; | |
3954 | ||
3955 | /* This is the BLOCK to record the symbol binding for debugging. */ | |
3956 | tree block; | |
3957 | ||
3958 | base = stabilize_reference (base); | |
3959 | ||
3960 | /* Since we can use base many times, save_expr it. */ | |
3961 | if (TREE_SIDE_EFFECTS (base)) | |
3962 | base = save_expr (base); | |
3963 | ||
3964 | if (TREE_CODE (ptype) == POINTER_TYPE) | |
3965 | { | |
3966 | /* Step back one from start of vector, and read dimension. */ | |
3967 | tree cookie_addr = build (MINUS_EXPR, TYPE_POINTER_TO (BI_header_type), | |
3968 | base, BI_header_size); | |
3969 | tree cookie = build_indirect_ref (cookie_addr, NULL_PTR); | |
3970 | maxindex = build_component_ref (cookie, nc_nelts_field_id, 0, 0); | |
3971 | do | |
3972 | ptype = TREE_TYPE (ptype); | |
3973 | while (TREE_CODE (ptype) == ARRAY_TYPE); | |
3974 | } | |
3975 | else if (TREE_CODE (ptype) == ARRAY_TYPE) | |
3976 | { | |
3977 | /* get the total number of things in the array, maxindex is a bad name */ | |
3978 | maxindex = array_type_nelts_total (ptype); | |
3979 | while (TREE_CODE (ptype) == ARRAY_TYPE) | |
3980 | ptype = TREE_TYPE (ptype); | |
3981 | base = build_unary_op (ADDR_EXPR, base, 1); | |
3982 | } | |
3983 | else | |
3984 | { | |
3985 | error ("type to vector delete is neither pointer or array type"); | |
3986 | return error_mark_node; | |
3987 | } | |
3988 | type = ptype; | |
3989 | ptype = TYPE_POINTER_TO (type); | |
3990 | ||
3991 | size_exp = size_in_bytes (type); | |
3992 | ||
3993 | if (! IS_AGGR_TYPE (type) || ! TYPE_NEEDS_DESTRUCTOR (type)) | |
3994 | { | |
3995 | loop = integer_zero_node; | |
3996 | goto no_destructor; | |
3997 | } | |
3998 | ||
3999 | /* The below is short by BI_header_size */ | |
4000 | virtual_size = fold (size_binop (MULT_EXPR, size_exp, maxindex)); | |
4001 | ||
4002 | tbase = build_decl (VAR_DECL, NULL_TREE, ptype); | |
4003 | tbase_init = build_modify_expr (tbase, NOP_EXPR, | |
4004 | fold (build (PLUS_EXPR, ptype, | |
4005 | base, | |
4006 | virtual_size))); | |
4007 | DECL_REGISTER (tbase) = 1; | |
4008 | controller = build (BIND_EXPR, void_type_node, tbase, 0, 0); | |
4009 | TREE_SIDE_EFFECTS (controller) = 1; | |
4010 | block = build_block (tbase, 0, 0, 0, 0); | |
4011 | add_block_current_level (block); | |
4012 | ||
4013 | if (auto_delete != integer_zero_node | |
4014 | && auto_delete != integer_two_node) | |
4015 | { | |
4016 | tree base_tbd = convert (ptype, | |
4017 | build_binary_op (MINUS_EXPR, | |
4018 | convert (ptr_type_node, base), | |
4019 | BI_header_size, | |
4020 | 1)); | |
4021 | /* This is the real size */ | |
4022 | virtual_size = size_binop (PLUS_EXPR, virtual_size, BI_header_size); | |
4023 | body = build_tree_list (NULL_TREE, | |
a28e3c7f MS |
4024 | build_x_delete (ptype, base_tbd, |
4025 | 2 | use_global_delete, | |
8d08fdba MS |
4026 | virtual_size)); |
4027 | body = build (COND_EXPR, void_type_node, | |
4028 | build (BIT_AND_EXPR, integer_type_node, | |
4029 | auto_delete, integer_one_node), | |
4030 | body, integer_zero_node); | |
4031 | } | |
4032 | else | |
4033 | body = NULL_TREE; | |
4034 | ||
4035 | body = tree_cons (NULL_TREE, | |
4036 | build_delete (ptype, tbase, auto_delete, | |
8926095f | 4037 | LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 1), |
8d08fdba MS |
4038 | body); |
4039 | ||
4040 | body = tree_cons (NULL_TREE, | |
4041 | build_modify_expr (tbase, NOP_EXPR, build (MINUS_EXPR, ptype, tbase, size_exp)), | |
4042 | body); | |
4043 | ||
4044 | body = tree_cons (NULL_TREE, | |
4045 | build (EXIT_EXPR, void_type_node, | |
4046 | build (EQ_EXPR, integer_type_node, base, tbase)), | |
4047 | body); | |
4048 | ||
4049 | loop = build (LOOP_EXPR, void_type_node, build_compound_expr (body)); | |
4050 | ||
4051 | loop = tree_cons (NULL_TREE, tbase_init, | |
4052 | tree_cons (NULL_TREE, loop, NULL_TREE)); | |
4053 | loop = build_compound_expr (loop); | |
4054 | ||
4055 | no_destructor: | |
4056 | /* If the delete flag is one, or anything else with the low bit set, | |
4057 | delete the storage. */ | |
4058 | if (auto_delete_vec == integer_zero_node | |
4059 | || auto_delete_vec == integer_two_node) | |
4060 | deallocate_expr = integer_zero_node; | |
4061 | else | |
4062 | { | |
4063 | tree base_tbd; | |
4064 | ||
4065 | /* The below is short by BI_header_size */ | |
4066 | virtual_size = fold (size_binop (MULT_EXPR, size_exp, maxindex)); | |
4067 | ||
a28e3c7f | 4068 | if (! TYPE_VEC_NEW_USES_COOKIE (type)) |
8d08fdba MS |
4069 | /* no header */ |
4070 | base_tbd = base; | |
4071 | else | |
4072 | { | |
4073 | base_tbd = convert (ptype, | |
4074 | build_binary_op (MINUS_EXPR, | |
4075 | convert (string_type_node, base), | |
4076 | BI_header_size, | |
4077 | 1)); | |
4078 | /* True size with header. */ | |
4079 | virtual_size = size_binop (PLUS_EXPR, virtual_size, BI_header_size); | |
4080 | } | |
a28e3c7f MS |
4081 | deallocate_expr = build_x_delete (ptype, base_tbd, |
4082 | 2 | use_global_delete, | |
8d08fdba MS |
4083 | virtual_size); |
4084 | if (auto_delete_vec != integer_one_node) | |
4085 | deallocate_expr = build (COND_EXPR, void_type_node, | |
4086 | build (BIT_AND_EXPR, integer_type_node, | |
4087 | auto_delete_vec, integer_one_node), | |
4088 | deallocate_expr, integer_zero_node); | |
4089 | } | |
4090 | ||
4091 | if (loop && deallocate_expr != integer_zero_node) | |
4092 | { | |
4093 | body = tree_cons (NULL_TREE, loop, | |
4094 | tree_cons (NULL_TREE, deallocate_expr, NULL_TREE)); | |
4095 | body = build_compound_expr (body); | |
4096 | } | |
4097 | else | |
4098 | body = loop; | |
4099 | ||
4100 | /* Outermost wrapper: If pointer is null, punt. */ | |
4101 | body = build (COND_EXPR, void_type_node, | |
4102 | build (NE_EXPR, integer_type_node, base, integer_zero_node), | |
4103 | body, integer_zero_node); | |
4104 | body = build1 (NOP_EXPR, void_type_node, body); | |
4105 | ||
4106 | if (controller) | |
4107 | { | |
4108 | TREE_OPERAND (controller, 1) = body; | |
4109 | return controller; | |
4110 | } | |
4111 | else | |
4112 | return convert (void_type_node, body); | |
4113 | } |