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