]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/cp/init.c
(instantiate_type): Change error message text.
[thirdparty/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
6060a796 188 expand_aggr_init (decl, init, 0, 0);
8d08fdba
MS
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. */
6060a796 737 vtbl = convert_force (TREE_TYPE (vtbl_ptr), vtbl, 0);
7177d104 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);
6060a796 1074 expand_aggr_init (parm, NULL_TREE, 0, 0);
8d08fdba
MS
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.
6060a796
MS
1090 If FLAGS is LOOKUP_ONLYCONVERTING then it is the = init form of
1091 the initializer, if FLAGS is 0, then it is the (init) form.
8d08fdba
MS
1092 If `init' is a CONSTRUCTOR, then we emit a warning message,
1093 explaining that such initializations are illegal.
1094
1095 ALIAS_THIS is nonzero iff we are initializing something which is
1096 essentially an alias for C_C_D. In this case, the base constructor
1097 may move it on us, and we must keep track of such deviations.
1098
1099 If INIT resolves to a CALL_EXPR which happens to return
1100 something of the type we are looking for, then we know
1101 that we can safely use that call to perform the
1102 initialization.
1103
1104 The virtual function table pointer cannot be set up here, because
1105 we do not really know its type.
1106
1107 Virtual baseclass pointers are also set up here.
1108
1109 This never calls operator=().
1110
1111 When initializing, nothing is CONST.
1112
1113 A default copy constructor may have to be used to perform the
1114 initialization.
1115
1116 A constructor or a conversion operator may have to be used to
1117 perform the initialization, but not both, as it would be ambiguous.
1118 */
1119
1120void
6060a796 1121expand_aggr_init (exp, init, alias_this, flags)
8d08fdba
MS
1122 tree exp, init;
1123 int alias_this;
6060a796 1124 int flags;
8d08fdba
MS
1125{
1126 tree type = TREE_TYPE (exp);
1127 int was_const = TREE_READONLY (exp);
1128
1129 if (init == error_mark_node)
1130 return;
1131
1132 TREE_READONLY (exp) = 0;
1133
1134 if (TREE_CODE (type) == ARRAY_TYPE)
1135 {
1136 /* Must arrange to initialize each element of EXP
1137 from elements of INIT. */
1138 int was_const_elts = TYPE_READONLY (TREE_TYPE (type));
1139 tree itype = init ? TREE_TYPE (init) : NULL_TREE;
1140 if (was_const_elts)
f376e137
MS
1141 {
1142 TREE_TYPE (exp) = TYPE_MAIN_VARIANT (type);
1143 if (init)
1144 TREE_TYPE (init) = TYPE_MAIN_VARIANT (itype);
1145 }
8d08fdba
MS
1146 if (init && TREE_TYPE (init) == NULL_TREE)
1147 {
1148 /* Handle bad initializers like:
1149 class COMPLEX {
1150 public:
1151 double re, im;
1152 COMPLEX(double r = 0.0, double i = 0.0) {re = r; im = i;};
1153 ~COMPLEX() {};
1154 };
1155
1156 int main(int argc, char **argv) {
1157 COMPLEX zees(1.0, 0.0)[10];
1158 }
1159 */
1160 error ("bad array initializer");
1161 return;
1162 }
1163 expand_vec_init (exp, exp, array_type_nelts (type), init,
1164 init && comptypes (TREE_TYPE (init), TREE_TYPE (exp), 1));
1165 TREE_READONLY (exp) = was_const;
1166 TREE_TYPE (exp) = type;
f376e137
MS
1167 if (init)
1168 TREE_TYPE (init) = itype;
8d08fdba
MS
1169 return;
1170 }
1171
1172 if (TREE_CODE (exp) == VAR_DECL || TREE_CODE (exp) == PARM_DECL)
1173 /* just know that we've seen something for this node */
1174 TREE_USED (exp) = 1;
1175
1176#if 0
1177 /* If initializing from a GNU C CONSTRUCTOR, consider the elts in the
1178 constructor as parameters to an implicit GNU C++ constructor. */
1179 if (init && TREE_CODE (init) == CONSTRUCTOR
1180 && TYPE_HAS_CONSTRUCTOR (type)
1181 && TREE_TYPE (init) == type)
1182 init = CONSTRUCTOR_ELTS (init);
1183#endif
1184 expand_aggr_init_1 (TYPE_BINFO (type), exp, exp,
6060a796 1185 init, alias_this, LOOKUP_NORMAL|flags);
8d08fdba
MS
1186 TREE_READONLY (exp) = was_const;
1187}
1188
1189static void
1190expand_default_init (binfo, true_exp, exp, type, init, alias_this, flags)
1191 tree binfo;
1192 tree true_exp, exp;
1193 tree type;
1194 tree init;
1195 int alias_this;
1196 int flags;
1197{
1198 /* It fails because there may not be a constructor which takes
1199 its own type as the first (or only parameter), but which does
1200 take other types via a conversion. So, if the thing initializing
1201 the expression is a unit element of type X, first try X(X&),
1202 followed by initialization by X. If neither of these work
1203 out, then look hard. */
1204 tree rval;
1205 tree parms;
8d08fdba
MS
1206
1207 if (init == NULL_TREE || TREE_CODE (init) == TREE_LIST)
1208 {
1209 parms = init;
db5ae43f
MS
1210 if (parms)
1211 init = TREE_VALUE (parms);
8d08fdba
MS
1212 }
1213 else if (TREE_CODE (init) == INDIRECT_REF && TREE_HAS_CONSTRUCTOR (init))
1214 {
1215 rval = convert_for_initialization (exp, type, init, 0, 0, 0, 0);
f376e137 1216 TREE_USED (rval) = 1;
8d08fdba
MS
1217 expand_expr_stmt (rval);
1218 return;
1219 }
1220 else
1221 parms = build_tree_list (NULL_TREE, init);
1222
8d08fdba
MS
1223 if (TYPE_USES_VIRTUAL_BASECLASSES (type))
1224 {
1225 if (true_exp == exp)
1226 parms = tree_cons (NULL_TREE, integer_one_node, parms);
1227 else
1228 parms = tree_cons (NULL_TREE, integer_zero_node, parms);
1229 flags |= LOOKUP_HAS_IN_CHARGE;
1230 }
1231
db5ae43f
MS
1232 if (init && TREE_CHAIN (parms) == NULL_TREE
1233 && TYPE_HAS_CONSTRUCTOR (type)
1234 && ! TYPE_NEEDS_CONSTRUCTING (type)
1235 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (TREE_TYPE (init)))
8d08fdba 1236 {
db5ae43f
MS
1237 rval = build (INIT_EXPR, type, exp, init);
1238 TREE_SIDE_EFFECTS (rval) = 1;
1239 expand_expr_stmt (rval);
8d08fdba 1240 }
db5ae43f 1241 else
8d08fdba 1242 {
db5ae43f
MS
1243 rval = build_method_call (exp, constructor_name_full (type),
1244 parms, binfo, flags);
8d08fdba 1245
db5ae43f
MS
1246 /* Private, protected, or otherwise unavailable. */
1247 if (rval == error_mark_node)
8d08fdba 1248 {
db5ae43f
MS
1249 if (flags & LOOKUP_COMPLAIN)
1250 cp_error ("in base initialization for %sclass `%T'",
1251 TREE_VIA_VIRTUAL (binfo) ? "virtual base " : "",
1252 binfo);
8d08fdba 1253 }
db5ae43f
MS
1254 else if (rval == NULL_TREE)
1255 my_friendly_abort (361);
8d08fdba
MS
1256 else
1257 {
db5ae43f
MS
1258 /* p. 222: if the base class assigns to `this', then that
1259 value is used in the derived class. */
1260 if ((flag_this_is_variable & 1) && alias_this)
1261 {
1262 TREE_TYPE (rval) = TREE_TYPE (current_class_decl);
1263 expand_assignment (current_class_decl, rval, 0, 0);
1264 }
1265 else
1266 expand_expr_stmt (rval);
8d08fdba 1267 }
8d08fdba 1268 }
8d08fdba
MS
1269}
1270
1271/* This function is responsible for initializing EXP with INIT
1272 (if any).
1273
1274 BINFO is the binfo of the type for who we are performing the
1275 initialization. For example, if W is a virtual base class of A and B,
1276 and C : A, B.
1277 If we are initializing B, then W must contain B's W vtable, whereas
1278 were we initializing C, W must contain C's W vtable.
1279
1280 TRUE_EXP is nonzero if it is the true expression being initialized.
1281 In this case, it may be EXP, or may just contain EXP. The reason we
1282 need this is because if EXP is a base element of TRUE_EXP, we
1283 don't necessarily know by looking at EXP where its virtual
1284 baseclass fields should really be pointing. But we do know
1285 from TRUE_EXP. In constructors, we don't know anything about
1286 the value being initialized.
1287
1288 ALIAS_THIS serves the same purpose it serves for expand_aggr_init.
1289
1290 FLAGS is just passes to `build_method_call'. See that function for
1291 its description. */
1292
1293static void
1294expand_aggr_init_1 (binfo, true_exp, exp, init, alias_this, flags)
1295 tree binfo;
1296 tree true_exp, exp;
1297 tree init;
1298 int alias_this;
1299 int flags;
1300{
1301 tree type = TREE_TYPE (exp);
1302 tree init_type = NULL_TREE;
8d08fdba
MS
1303
1304 my_friendly_assert (init != error_mark_node && type != error_mark_node, 211);
1305
1306 /* Use a function returning the desired type to initialize EXP for us.
1307 If the function is a constructor, and its first argument is
1308 NULL_TREE, know that it was meant for us--just slide exp on
1309 in and expand the constructor. Constructors now come
1310 as TARGET_EXPRs. */
1311 if (init)
1312 {
1313 tree init_list = NULL_TREE;
1314
1315 if (TREE_CODE (init) == TREE_LIST)
1316 {
1317 init_list = init;
1318 if (TREE_CHAIN (init) == NULL_TREE)
1319 init = TREE_VALUE (init);
1320 }
1321
1322 init_type = TREE_TYPE (init);
1323
1324 if (TREE_CODE (init) != TREE_LIST)
1325 {
1326 if (TREE_CODE (init_type) == ERROR_MARK)
1327 return;
1328
1329#if 0
1330 /* These lines are found troublesome 5/11/89. */
1331 if (TREE_CODE (init_type) == REFERENCE_TYPE)
1332 init_type = TREE_TYPE (init_type);
1333#endif
1334
1335 /* This happens when we use C++'s functional cast notation.
1336 If the types match, then just use the TARGET_EXPR
1337 directly. Otherwise, we need to create the initializer
1338 separately from the object being initialized. */
1339 if (TREE_CODE (init) == TARGET_EXPR)
1340 {
a292b002 1341 if (TYPE_MAIN_VARIANT (init_type) == TYPE_MAIN_VARIANT (type))
8d08fdba
MS
1342 {
1343 if (TREE_CODE (exp) == VAR_DECL
1344 || TREE_CODE (exp) == RESULT_DECL)
1345 /* Unify the initialization targets. */
1346 DECL_RTL (TREE_OPERAND (init, 0)) = DECL_RTL (exp);
1347 else
1348 DECL_RTL (TREE_OPERAND (init, 0)) = expand_expr (exp, NULL_RTX, 0, 0);
1349
1350 expand_expr_stmt (init);
1351 return;
1352 }
1353 else
1354 {
1355 init = TREE_OPERAND (init, 1);
1356 init = build (CALL_EXPR, init_type,
1357 TREE_OPERAND (init, 0), TREE_OPERAND (init, 1), 0);
1358 TREE_SIDE_EFFECTS (init) = 1;
8d08fdba
MS
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;
8d08fdba
MS
1428 TREE_VALUE (TREE_OPERAND (init, 1))
1429 = convert_pointer_to (TREE_TYPE (TREE_TYPE (TREE_VALUE (tmp))), arg);
1430
1431 if (alias_this)
1432 {
1433 expand_assignment (current_function_decl, init, 0, 0);
1434 return;
1435 }
1436 if (exp == DECL_RESULT (current_function_decl))
1437 {
1438 if (DECL_INITIAL (DECL_RESULT (current_function_decl)))
1439 fatal ("return value from function receives multiple initializations");
1440 DECL_INITIAL (exp) = init;
1441 }
1442 expand_expr_stmt (init);
1443 return;
1444 }
1445 }
1446
1447 if (TREE_CODE (exp) == VAR_DECL
1448 && TREE_CODE (init) == CONSTRUCTOR
1449 && TREE_HAS_CONSTRUCTOR (init))
1450 {
1451 tree t = store_init_value (exp, init);
1452 if (!t)
1453 {
1454 expand_decl_init (exp);
1455 return;
1456 }
1457 t = build (INIT_EXPR, type, exp, init);
1458 TREE_SIDE_EFFECTS (t) = 1;
1459 expand_expr_stmt (t);
1460 return;
1461 }
1462
1463 /* Handle this case: when calling a constructor: xyzzy foo(bar);
1464 which really means: xyzzy foo = bar; Ugh!
1465
1466 More useful for this case: xyzzy *foo = new xyzzy (bar); */
1467
1468 if (! TYPE_NEEDS_CONSTRUCTING (type) && ! IS_AGGR_TYPE (type))
1469 {
1470 if (init_list && TREE_CHAIN (init_list))
1471 {
1472 warning ("initializer list being treated as compound expression");
1473 init = convert (type, build_compound_expr (init_list));
1474 if (init == error_mark_node)
1475 return;
1476 }
1477
1478 expand_assignment (exp, init, 0, 0);
1479
1480 return;
1481 }
1482 /* See whether we can go through a type conversion operator.
1483 This wins over going through a non-existent constructor. If
1484 there is a constructor, it is ambiguous. */
1485 if (TREE_CODE (init) != TREE_LIST)
1486 {
1487 tree ttype = TREE_CODE (init_type) == REFERENCE_TYPE
1488 ? TREE_TYPE (init_type) : init_type;
1489
1490 if (ttype != type && IS_AGGR_TYPE (ttype))
1491 {
1492 tree rval = build_type_conversion (CONVERT_EXPR, type, init, 0);
1493
1494 if (rval)
1495 {
1496 /* See if there is a constructor for``type'' that takes a
1497 ``ttype''-typed object. */
1498 tree parms = build_tree_list (NULL_TREE, init);
1499 tree as_cons = NULL_TREE;
1500 if (TYPE_HAS_CONSTRUCTOR (type))
1501 as_cons = build_method_call (exp, constructor_name_full (type),
1502 parms, binfo,
1503 LOOKUP_SPECULATIVELY|LOOKUP_NO_CONVERSION);
1504 if (as_cons != NULL_TREE && as_cons != error_mark_node)
1505 /* ANSI C++ June 5 1992 WP 12.3.2.6.1 */
1506 cp_error ("ambiguity between conversion to `%T' and constructor",
1507 type);
1508 else
1509 expand_assignment (exp, rval, 0, 0);
1510 return;
1511 }
1512 }
1513 }
1514 }
1515
1516 /* Handle default copy constructors here, does not matter if there is
1517 a constructor or not. */
1518 if (type == init_type && IS_AGGR_TYPE (type)
1519 && init && TREE_CODE (init) != TREE_LIST)
1520 expand_default_init (binfo, true_exp, exp, type, init, alias_this, flags);
1521 /* Not sure why this is here... */
1522 else if (TYPE_HAS_CONSTRUCTOR (type))
1523 expand_default_init (binfo, true_exp, exp, type, init, alias_this, flags);
1524 else if (TREE_CODE (type) == ARRAY_TYPE)
1525 {
1526 if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (type)))
1527 expand_vec_init (exp, exp, array_type_nelts (type), init, 0);
1528 else if (TYPE_VIRTUAL_P (TREE_TYPE (type)))
1529 sorry ("arrays of objects with virtual functions but no constructors");
1530 }
1531 else
1532 expand_recursive_init (binfo, true_exp, exp, init,
1533 CLASSTYPE_BASE_INIT_LIST (type), alias_this);
1534}
1535
1536/* A pointer which holds the initializer. First call to
1537 expand_aggr_init gets this value pointed to, and sets it to init_null. */
1538static tree *init_ptr, init_null;
1539
1540/* Subroutine of expand_recursive_init:
1541
1542 ADDR is the address of the expression being initialized.
1543 INIT_LIST is the cons-list of initializations to be performed.
1544 ALIAS_THIS is its same, lovable self. */
1545static void
1546expand_recursive_init_1 (binfo, true_exp, addr, init_list, alias_this)
1547 tree binfo, true_exp, addr;
1548 tree init_list;
1549 int alias_this;
1550{
1551 while (init_list)
1552 {
1553 if (TREE_PURPOSE (init_list))
1554 {
1555 if (TREE_CODE (TREE_PURPOSE (init_list)) == FIELD_DECL)
1556 {
1557 tree member = TREE_PURPOSE (init_list);
1558 tree subexp = build_indirect_ref (convert_pointer_to (TREE_VALUE (init_list), addr), NULL_PTR);
1559 tree member_base = build (COMPONENT_REF, TREE_TYPE (member), subexp, member);
1560 if (IS_AGGR_TYPE (TREE_TYPE (member)))
6060a796 1561 expand_aggr_init (member_base, DECL_INITIAL (member), 0, 0);
8d08fdba
MS
1562 else if (TREE_CODE (TREE_TYPE (member)) == ARRAY_TYPE
1563 && TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (member)))
1564 {
1565 member_base = save_expr (default_conversion (member_base));
1566 expand_vec_init (member, member_base,
1567 array_type_nelts (TREE_TYPE (member)),
1568 DECL_INITIAL (member), 0);
1569 }
1570 else
1571 expand_expr_stmt (build_modify_expr (member_base, INIT_EXPR, DECL_INITIAL (member)));
1572 }
1573 else if (TREE_CODE (TREE_PURPOSE (init_list)) == TREE_LIST)
1574 {
1575 expand_recursive_init_1 (binfo, true_exp, addr, TREE_PURPOSE (init_list), alias_this);
1576 expand_recursive_init_1 (binfo, true_exp, addr, TREE_VALUE (init_list), alias_this);
1577 }
1578 else if (TREE_CODE (TREE_PURPOSE (init_list)) == ERROR_MARK)
1579 {
1580 /* Only initialize the virtual function tables if we
1581 are initializing the ultimate users of those vtables. */
1582 if (TREE_VALUE (init_list))
1583 {
7177d104 1584 /* We have to ensure that the first argment to
8926095f 1585 expand_virtual_init is in binfo's hierarchy. */
7177d104
MS
1586 /* Is it the case that this is exactly the right binfo? */
1587 /* If it is ok, then fixup expand_virtual_init, to make
1588 it much simpler. */
1589 expand_virtual_init (get_binfo (TREE_VALUE (init_list), binfo, 0),
8926095f 1590 addr);
8d08fdba
MS
1591 if (TREE_VALUE (init_list) == binfo
1592 && TYPE_USES_VIRTUAL_BASECLASSES (BINFO_TYPE (binfo)))
7177d104 1593 expand_indirect_vtbls_init (binfo, true_exp, addr, 1);
8d08fdba
MS
1594 }
1595 }
1596 else
1597 my_friendly_abort (49);
1598 }
1599 else if (TREE_VALUE (init_list)
1600 && TREE_CODE (TREE_VALUE (init_list)) == TREE_VEC)
1601 {
1602 tree subexp = build_indirect_ref (convert_pointer_to (TREE_VALUE (init_list), addr), NULL_PTR);
1603 expand_aggr_init_1 (binfo, true_exp, subexp, *init_ptr,
1604 alias_this && BINFO_OFFSET_ZEROP (TREE_VALUE (init_list)),
1605 LOOKUP_COMPLAIN);
1606
1607 /* INIT_PTR is used up. */
1608 init_ptr = &init_null;
1609 }
1610 else
1611 my_friendly_abort (50);
1612 init_list = TREE_CHAIN (init_list);
1613 }
1614}
1615
1616/* Initialize EXP with INIT. Type EXP does not have a constructor,
1617 but it has a baseclass with a constructor or a virtual function
1618 table which needs initializing.
1619
1620 INIT_LIST is a cons-list describing what parts of EXP actually
1621 need to be initialized. INIT is given to the *unique*, first
1622 constructor within INIT_LIST. If there are multiple first
1623 constructors, such as with multiple inheritance, INIT must
1624 be zero or an ambiguity error is reported.
1625
1626 ALIAS_THIS is passed from `expand_aggr_init'. See comments
1627 there. */
1628
1629static void
1630expand_recursive_init (binfo, true_exp, exp, init, init_list, alias_this)
1631 tree binfo, true_exp, exp, init;
1632 tree init_list;
1633 int alias_this;
1634{
1635 tree *old_init_ptr = init_ptr;
1636 tree addr = build_unary_op (ADDR_EXPR, exp, 0);
1637 init_ptr = &init;
1638
1639 if (true_exp == exp && TYPE_USES_VIRTUAL_BASECLASSES (BINFO_TYPE (binfo)))
1640 {
1641 expand_aggr_vbase_init (binfo, exp, addr, init_list);
7177d104 1642 expand_indirect_vtbls_init (binfo, true_exp, addr, 1);
8d08fdba
MS
1643 }
1644 expand_recursive_init_1 (binfo, true_exp, addr, init_list, alias_this);
1645
1646 if (*init_ptr)
1647 {
1648 tree type = TREE_TYPE (exp);
1649
1650 if (TREE_CODE (type) == REFERENCE_TYPE)
1651 type = TREE_TYPE (type);
1652 if (IS_AGGR_TYPE (type))
1653 cp_error ("unexpected argument to constructor `%T'", type);
1654 else
1655 error ("unexpected argument to constructor");
1656 }
1657 init_ptr = old_init_ptr;
1658}
1659
1660/* Report an error if NAME is not the name of a user-defined,
1661 aggregate type. If OR_ELSE is nonzero, give an error message. */
1662int
1663is_aggr_typedef (name, or_else)
1664 tree name;
1665 int or_else;
1666{
1667 tree type;
1668
1669 if (name == error_mark_node)
1670 return 0;
1671
1672 if (IDENTIFIER_HAS_TYPE_VALUE (name))
1673 type = IDENTIFIER_TYPE_VALUE (name);
8d08fdba
MS
1674 else
1675 {
1676 if (or_else)
a28e3c7f 1677 cp_error ("`%T' is not an aggregate typedef", name);
8d08fdba
MS
1678 return 0;
1679 }
1680
1681 if (! IS_AGGR_TYPE (type)
1682 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
1683 {
1684 if (or_else)
a28e3c7f 1685 cp_error ("`%T' is not an aggregate type", type);
8d08fdba
MS
1686 return 0;
1687 }
1688 return 1;
1689}
1690
1691/* Like is_aggr_typedef, but returns typedef if successful. */
1692tree
1693get_aggr_from_typedef (name, or_else)
1694 tree name;
1695 int or_else;
1696{
1697 tree type;
1698
1699 if (name == error_mark_node)
1700 return NULL_TREE;
1701
1702 if (IDENTIFIER_HAS_TYPE_VALUE (name))
1703 type = IDENTIFIER_TYPE_VALUE (name);
8d08fdba
MS
1704 else
1705 {
1706 if (or_else)
1707 cp_error ("`%T' fails to be an aggregate typedef", name);
1708 return NULL_TREE;
1709 }
1710
1711 if (! IS_AGGR_TYPE (type)
1712 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
1713 {
1714 if (or_else)
1715 cp_error ("type `%T' is of non-aggregate type", type);
1716 return NULL_TREE;
1717 }
1718 return type;
1719}
1720
1721tree
1722get_type_value (name)
1723 tree name;
1724{
8d08fdba
MS
1725 if (name == error_mark_node)
1726 return NULL_TREE;
1727
1728 if (IDENTIFIER_HAS_TYPE_VALUE (name))
1729 return IDENTIFIER_TYPE_VALUE (name);
8d08fdba
MS
1730 else
1731 return NULL_TREE;
1732}
1733
1734\f
51c184be 1735/* This code could just as well go in `class.c', but is placed here for
8d08fdba
MS
1736 modularity. */
1737
1738/* For an expression of the form CNAME :: NAME (PARMLIST), build
1739 the appropriate function call. */
1740tree
1741build_member_call (cname, name, parmlist)
1742 tree cname, name, parmlist;
1743{
1744 tree type, t;
1745 tree method_name = name;
1746 int dtor = 0;
1747 int dont_use_this = 0;
1748 tree basetype_path, decl;
1749
1750 if (TREE_CODE (method_name) == BIT_NOT_EXPR)
1751 {
1752 method_name = TREE_OPERAND (method_name, 0);
1753 dtor = 1;
1754 }
1755
1756 if (TREE_CODE (cname) == SCOPE_REF)
1757 cname = resolve_scope_to_name (NULL_TREE, cname);
1758
1759 if (cname == NULL_TREE || ! (type = get_aggr_from_typedef (cname, 1)))
1760 return error_mark_node;
1761
1762 /* An operator we did not like. */
1763 if (name == NULL_TREE)
1764 return error_mark_node;
1765
1766 if (dtor)
1767 {
1768#if 0
1769 /* Everything can explicitly call a destructor; see 12.4 */
1770 if (! TYPE_HAS_DESTRUCTOR (type))
1771 cp_error ("type `%#T' does not have a destructor", type);
1772 else
1773#endif
1774 cp_error ("cannot call destructor `%T::~%T' without object", type,
1775 method_name);
1776 return error_mark_node;
1777 }
1778
1779 /* No object? Then just fake one up, and let build_method_call
1780 figure out what to do. */
1781 if (current_class_type == 0
1782 || get_base_distance (type, current_class_type, 0, &basetype_path) == -1)
1783 dont_use_this = 1;
1784
1785 if (dont_use_this)
1786 {
39211cd5 1787 basetype_path = TYPE_BINFO (type);
8d08fdba
MS
1788 decl = build1 (NOP_EXPR, TYPE_POINTER_TO (type), error_mark_node);
1789 }
1790 else if (current_class_decl == 0)
1791 {
1792 dont_use_this = 1;
1793 decl = build1 (NOP_EXPR, TYPE_POINTER_TO (type), error_mark_node);
1794 }
1795 else
1796 {
1797 tree olddecl = current_class_decl;
1798 tree oldtype = TREE_TYPE (TREE_TYPE (olddecl));
1799 if (oldtype != type)
1800 {
1801 tree newtype = build_type_variant (type, TYPE_READONLY (oldtype),
1802 TYPE_VOLATILE (oldtype));
6060a796 1803 decl = convert_force (build_pointer_type (newtype), olddecl, 0);
8d08fdba
MS
1804 }
1805 else
1806 decl = olddecl;
1807 }
1808
1809 decl = build_indirect_ref (decl, NULL_PTR);
1810
39211cd5 1811 if (t = lookup_fnfields (basetype_path, method_name, 0))
8d08fdba
MS
1812 return build_method_call (decl, method_name, parmlist, basetype_path,
1813 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL);
1814 if (TREE_CODE (name) == IDENTIFIER_NODE
8926095f 1815 && ((t = lookup_field (TYPE_BINFO (type), name, 1, 0))))
8d08fdba
MS
1816 {
1817 if (t == error_mark_node)
1818 return error_mark_node;
1819 if (TREE_CODE (t) == FIELD_DECL)
1820 {
1821 if (dont_use_this)
1822 {
1823 cp_error ("invalid use of non-static field `%D'", t);
1824 return error_mark_node;
1825 }
1826 decl = build (COMPONENT_REF, TREE_TYPE (t), decl, t);
1827 }
1828 else if (TREE_CODE (t) == VAR_DECL)
1829 decl = t;
1830 else
1831 {
1832 cp_error ("invalid use of member `%D'", t);
1833 return error_mark_node;
1834 }
1835 if (TYPE_LANG_SPECIFIC (TREE_TYPE (decl))
1836 && TYPE_OVERLOADS_CALL_EXPR (TREE_TYPE (decl)))
1837 return build_opfncall (CALL_EXPR, LOOKUP_NORMAL, decl, parmlist, NULL_TREE);
1838 return build_function_call (decl, parmlist);
1839 }
1840 else
1841 {
1842 cp_error ("no method `%T::%D'", type, name);
1843 return error_mark_node;
1844 }
1845}
1846
1847/* Build a reference to a member of an aggregate. This is not a
1848 C++ `&', but really something which can have its address taken,
1849 and then act as a pointer to member, for example CNAME :: FIELD
1850 can have its address taken by saying & CNAME :: FIELD.
1851
1852 @@ Prints out lousy diagnostics for operator <typename>
1853 @@ fields.
1854
51c184be 1855 @@ This function should be rewritten and placed in search.c. */
8d08fdba
MS
1856tree
1857build_offset_ref (cname, name)
1858 tree cname, name;
1859{
1860 tree decl, type, fnfields, fields, t = error_mark_node;
1861 tree basetypes = NULL_TREE;
1862 int dtor = 0;
1863
1864 if (TREE_CODE (cname) == SCOPE_REF)
1865 cname = resolve_scope_to_name (NULL_TREE, cname);
1866
1867 if (cname == NULL_TREE || ! is_aggr_typedef (cname, 1))
1868 return error_mark_node;
1869
1870 type = IDENTIFIER_TYPE_VALUE (cname);
1871
1872 if (TREE_CODE (name) == BIT_NOT_EXPR)
1873 {
1874 dtor = 1;
1875 name = TREE_OPERAND (name, 0);
1876 }
1877
1878 if (TYPE_SIZE (type) == 0)
1879 {
1880 t = IDENTIFIER_CLASS_VALUE (name);
1881 if (t == 0)
1882 {
1883 cp_error ("incomplete type `%T' does not have member `%D'", type,
1884 name);
1885 return error_mark_node;
1886 }
51c184be
MS
1887 if (TREE_CODE (t) == TYPE_DECL || TREE_CODE (t) == VAR_DECL
1888 || TREE_CODE (t) == CONST_DECL)
8d08fdba
MS
1889 {
1890 TREE_USED (t) = 1;
1891 return t;
1892 }
1893 if (TREE_CODE (t) == FIELD_DECL)
1894 sorry ("use of member in incomplete aggregate type");
1895 else if (TREE_CODE (t) == FUNCTION_DECL)
1896 sorry ("use of member function in incomplete aggregate type");
1897 else
1898 my_friendly_abort (52);
1899 return error_mark_node;
1900 }
1901
39211cd5 1902#if 0
8d08fdba
MS
1903 if (TREE_CODE (name) == TYPE_EXPR)
1904 /* Pass a TYPE_DECL to build_component_type_expr. */
1905 return build_component_type_expr (TYPE_NAME (TREE_TYPE (cname)),
1906 name, NULL_TREE, 1);
39211cd5 1907#endif
8d08fdba 1908
8d08fdba
MS
1909 if (current_class_type == 0
1910 || get_base_distance (type, current_class_type, 0, &basetypes) == -1)
1911 {
1912 basetypes = TYPE_BINFO (type);
1913 decl = build1 (NOP_EXPR,
1914 IDENTIFIER_TYPE_VALUE (cname),
1915 error_mark_node);
1916 }
1917 else if (current_class_decl == 0)
1918 decl = build1 (NOP_EXPR, IDENTIFIER_TYPE_VALUE (cname),
1919 error_mark_node);
1920 else
1921 decl = C_C_D;
1922
00595019
MS
1923 fnfields = lookup_fnfields (basetypes, name, 1);
1924 fields = lookup_field (basetypes, name, 0, 0);
1925
1926 if (fields == error_mark_node || fnfields == error_mark_node)
1927 return error_mark_node;
1928
8d08fdba
MS
1929 /* A lot of this logic is now handled in lookup_field and
1930 lookup_fnfield. */
1931 if (fnfields)
1932 {
1933 basetypes = TREE_PURPOSE (fnfields);
1934
1935 /* Go from the TREE_BASELINK to the member function info. */
1936 t = TREE_VALUE (fnfields);
1937
1938 if (fields)
1939 {
1940 if (DECL_FIELD_CONTEXT (fields) == DECL_FIELD_CONTEXT (t))
1941 {
1942 error ("ambiguous member reference: member `%s' defined as both field and function",
1943 IDENTIFIER_POINTER (name));
1944 return error_mark_node;
1945 }
1946 if (UNIQUELY_DERIVED_FROM_P (DECL_FIELD_CONTEXT (fields), DECL_FIELD_CONTEXT (t)))
1947 ;
1948 else if (UNIQUELY_DERIVED_FROM_P (DECL_FIELD_CONTEXT (t), DECL_FIELD_CONTEXT (fields)))
1949 t = fields;
1950 else
1951 {
1952 error ("ambiguous member reference: member `%s' derives from distinct classes in multiple inheritance lattice");
1953 return error_mark_node;
1954 }
1955 }
1956
1957 if (t == TREE_VALUE (fnfields))
1958 {
1959 extern int flag_save_memoized_contexts;
1960
8d08fdba
MS
1961 if (DECL_CHAIN (t) == NULL_TREE || dtor)
1962 {
1963 enum access_type access;
1964
1965 /* unique functions are handled easily. */
1966 unique:
1967 access = compute_access (basetypes, t);
1968 if (access == access_protected)
1969 {
1970 cp_error_at ("member function `%#D' is protected", t);
1971 error ("in this context");
1972 return error_mark_node;
1973 }
1974 if (access == access_private)
1975 {
1976 cp_error_at ("member function `%#D' is private", t);
1977 error ("in this context");
1978 return error_mark_node;
1979 }
1980 assemble_external (t);
1981 return build (OFFSET_REF, TREE_TYPE (t), decl, t);
1982 }
1983
1984 /* overloaded functions may need more work. */
1985 if (cname == name)
1986 {
1987 if (TYPE_HAS_DESTRUCTOR (type)
1988 && DECL_CHAIN (DECL_CHAIN (t)) == NULL_TREE)
1989 {
1990 t = DECL_CHAIN (t);
1991 goto unique;
1992 }
1993 }
1994 /* FNFIELDS is most likely allocated on the search_obstack,
1995 which will go away after this class scope. If we need
1996 to save this value for later (either for memoization
1997 or for use as an initializer for a static variable), then
1998 do so here.
1999
2000 ??? The smart thing to do for the case of saving initializers
2001 is to resolve them before we're done with this scope. */
2002 if (!TREE_PERMANENT (fnfields)
2003 && ((flag_save_memoized_contexts && global_bindings_p ())
2004 || ! allocation_temporary_p ()))
2005 fnfields = copy_list (fnfields);
2006 t = build_tree_list (error_mark_node, fnfields);
2007 TREE_TYPE (t) = build_offset_type (type, unknown_type_node);
2008 return t;
2009 }
2010 }
2011
2012 /* Now that we know we are looking for a field, see if we
2013 have access to that field. Lookup_field will give us the
2014 error message. */
2015
2016 t = lookup_field (basetypes, name, 1, 0);
2017
2018 if (t == error_mark_node)
2019 return error_mark_node;
2020
2021 if (t == NULL_TREE)
2022 {
a4443a08 2023 cp_error ("`%D' is not a member of type `%T'", name, type);
8d08fdba
MS
2024 return error_mark_node;
2025 }
2026
2027 if (TREE_CODE (t) == TYPE_DECL)
2028 {
51c184be
MS
2029 TREE_USED (t) = 1;
2030 return t;
8d08fdba
MS
2031 }
2032 /* static class members and class-specific enum
2033 values can be returned without further ado. */
2034 if (TREE_CODE (t) == VAR_DECL || TREE_CODE (t) == CONST_DECL)
2035 {
2036 assemble_external (t);
2037 TREE_USED (t) = 1;
2038 return t;
2039 }
2040
2041 /* static class functions too. */
2042 if (TREE_CODE (t) == FUNCTION_DECL && TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE)
2043 my_friendly_abort (53);
2044
2045 /* In member functions, the form `cname::name' is no longer
2046 equivalent to `this->cname::name'. */
2047 return build (OFFSET_REF, build_offset_type (type, TREE_TYPE (t)), decl, t);
2048}
2049
2050/* Given an object EXP and a member function reference MEMBER,
2051 return the address of the actual member function. */
2052tree
2053get_member_function (exp_addr_ptr, exp, member)
2054 tree *exp_addr_ptr;
2055 tree exp, member;
2056{
2057 tree ctype = TREE_TYPE (exp);
2058 tree function = save_expr (build_unary_op (ADDR_EXPR, member, 0));
2059
2060 if (TYPE_VIRTUAL_P (ctype)
2061 || (flag_all_virtual == 1 && TYPE_OVERLOADS_METHOD_CALL_EXPR (ctype)))
2062 {
2063 tree e0, e1, e3;
2064 tree exp_addr;
2065
2066 /* Save away the unadulterated `this' pointer. */
2067 exp_addr = save_expr (*exp_addr_ptr);
2068
2069 /* Cast function to signed integer. */
2070 e0 = build1 (NOP_EXPR, integer_type_node, function);
2071
8d08fdba
MS
2072 /* There is a hack here that takes advantage of
2073 twos complement arithmetic, and the fact that
2074 there are more than one UNITS to the WORD.
2075 If the high bit is set for the `function',
2076 then we pretend it is a virtual function,
2077 and the array indexing will knock this bit
2078 out the top, leaving a valid index. */
2079 if (UNITS_PER_WORD <= 1)
2080 my_friendly_abort (54);
2081
2082 e1 = build (GT_EXPR, integer_type_node, e0, integer_zero_node);
2083 e1 = build_compound_expr (tree_cons (NULL_TREE, exp_addr,
2084 build_tree_list (NULL_TREE, e1)));
2085 e1 = save_expr (e1);
8d08fdba
MS
2086
2087 if (TREE_SIDE_EFFECTS (*exp_addr_ptr))
2088 {
2089 exp = build_indirect_ref (exp_addr, NULL_PTR);
2090 *exp_addr_ptr = exp_addr;
2091 }
2092
2093 /* This is really hairy: if the function pointer is a pointer
2094 to a non-virtual member function, then we can't go mucking
2095 with the `this' pointer (any more than we already have to
2096 this point). If it is a pointer to a virtual member function,
2097 then we have to adjust the `this' pointer according to
2098 what the virtual function table tells us. */
2099
2100 e3 = build_vfn_ref (exp_addr_ptr, exp, e0);
2101 my_friendly_assert (e3 != error_mark_node, 213);
2102
2103 /* Change this pointer type from `void *' to the
2104 type it is really supposed to be. */
2105 TREE_TYPE (e3) = TREE_TYPE (function);
2106
2107 /* If non-virtual, use what we had originally. Otherwise,
2108 use the value we get from the virtual function table. */
2109 *exp_addr_ptr = build_conditional_expr (e1, exp_addr, *exp_addr_ptr);
2110
2111 function = build_conditional_expr (e1, function, e3);
2112 }
2113 return build_indirect_ref (function, NULL_PTR);
2114}
2115
2116/* If a OFFSET_REF made it through to here, then it did
2117 not have its address taken. */
2118
2119tree
2120resolve_offset_ref (exp)
2121 tree exp;
2122{
2123 tree type = TREE_TYPE (exp);
2124 tree base = NULL_TREE;
2125 tree member;
2126 tree basetype, addr;
2127
2128 if (TREE_CODE (exp) == TREE_LIST)
2129 return build_unary_op (ADDR_EXPR, exp, 0);
2130
2131 if (TREE_CODE (exp) != OFFSET_REF)
2132 {
2133 my_friendly_assert (TREE_CODE (type) == OFFSET_TYPE, 214);
2134 if (TYPE_OFFSET_BASETYPE (type) != current_class_type)
2135 {
2136 error ("object missing in use of pointer-to-member construct");
2137 return error_mark_node;
2138 }
2139 member = exp;
2140 type = TREE_TYPE (type);
2141 base = C_C_D;
2142 }
2143 else
2144 {
2145 member = TREE_OPERAND (exp, 1);
2146 base = TREE_OPERAND (exp, 0);
2147 }
2148
2149 if ((TREE_CODE (member) == VAR_DECL
2150 && ! TYPE_PTRMEMFUNC_P (TREE_TYPE (member)))
2151 || TREE_CODE (TREE_TYPE (member)) == FUNCTION_TYPE)
2152 {
2153 /* These were static members. */
2154 if (mark_addressable (member) == 0)
2155 return error_mark_node;
2156 return member;
2157 }
2158
2159 /* Syntax error can cause a member which should
2160 have been seen as static to be grok'd as non-static. */
2161 if (TREE_CODE (member) == FIELD_DECL && C_C_D == NULL_TREE)
2162 {
2163 if (TREE_ADDRESSABLE (member) == 0)
2164 {
2165 cp_error_at ("member `%D' is non-static in static member function context", member);
2166 error ("at this point in file");
2167 TREE_ADDRESSABLE (member) = 1;
2168 }
2169 return error_mark_node;
2170 }
2171
2172 /* The first case is really just a reference to a member of `this'. */
2173 if (TREE_CODE (member) == FIELD_DECL
2174 && (base == C_C_D
2175 || (TREE_CODE (base) == NOP_EXPR
2176 && TREE_OPERAND (base, 0) == error_mark_node)))
2177 {
2178 tree basetype_path;
2179 enum access_type access;
2180
2181 if (TREE_CODE (exp) == OFFSET_REF && TREE_CODE (type) == OFFSET_TYPE)
39211cd5 2182 basetype = TYPE_OFFSET_BASETYPE (type);
8d08fdba 2183 else
39211cd5
MS
2184 basetype = DECL_CONTEXT (member);
2185
2186 base = current_class_decl;
8d08fdba
MS
2187
2188 if (get_base_distance (basetype, TREE_TYPE (TREE_TYPE (base)), 0, &basetype_path) < 0)
2189 {
2190 error_not_base_type (basetype, TREE_TYPE (TREE_TYPE (base)));
2191 return error_mark_node;
2192 }
2193 addr = convert_pointer_to (basetype, base);
2194 access = compute_access (basetype_path, member);
2195 if (access == access_public)
2196 return build (COMPONENT_REF, TREE_TYPE (member),
2197 build_indirect_ref (addr, NULL_PTR), member);
2198 if (access == access_protected)
2199 {
2200 cp_error_at ("member `%D' is protected", member);
2201 error ("in this context");
2202 return error_mark_node;
2203 }
2204 if (access == access_private)
2205 {
2206 cp_error_at ("member `%D' is private", member);
2207 error ("in this context");
2208 return error_mark_node;
2209 }
2210 my_friendly_abort (55);
2211 }
2212
2213 /* If this is a reference to a member function, then return
2214 the address of the member function (which may involve going
2215 through the object's vtable), otherwise, return an expression
2216 for the dereferenced pointer-to-member construct. */
2217 addr = build_unary_op (ADDR_EXPR, base, 0);
2218
2219 if (TREE_CODE (TREE_TYPE (member)) == METHOD_TYPE)
2220 {
2221 basetype = DECL_CLASS_CONTEXT (member);
2222 addr = convert_pointer_to (basetype, addr);
2223 return build_unary_op (ADDR_EXPR, get_member_function (&addr, build_indirect_ref (addr, NULL_PTR), member), 0);
2224 }
2225 else if (TREE_CODE (TREE_TYPE (member)) == OFFSET_TYPE)
2226 {
2227 basetype = TYPE_OFFSET_BASETYPE (TREE_TYPE (member));
2228 addr = convert_pointer_to (basetype, addr);
fef71f9d
JM
2229 member = convert (ptrdiff_type_node,
2230 build_unary_op (ADDR_EXPR, member, 0));
8d08fdba 2231 return build1 (INDIRECT_REF, type,
fef71f9d
JM
2232 build (PLUS_EXPR, build_pointer_type (type),
2233 addr, member));
8d08fdba
MS
2234 }
2235 else if (TYPE_PTRMEMFUNC_P (TREE_TYPE (member)))
2236 {
2237 return get_member_function_from_ptrfunc (&addr, base, member);
2238 }
2239 my_friendly_abort (56);
2240 /* NOTREACHED */
2241 return NULL_TREE;
2242}
2243
2244/* Return either DECL or its known constant value (if it has one). */
2245
2246tree
2247decl_constant_value (decl)
2248 tree decl;
2249{
2250 if (! TREE_THIS_VOLATILE (decl)
2251#if 0
2252 /* These may be necessary for C, but they break C++. */
2253 ! TREE_PUBLIC (decl)
2254 /* Don't change a variable array bound or initial value to a constant
2255 in a place where a variable is invalid. */
2256 && ! pedantic
2257#endif /* 0 */
2258 && DECL_INITIAL (decl) != 0
2259 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
2260 /* This is invalid if initial value is not constant.
2261 If it has either a function call, a memory reference,
2262 or a variable, then re-evaluating it could give different results. */
2263 && TREE_CONSTANT (DECL_INITIAL (decl))
2264 /* Check for cases where this is sub-optimal, even though valid. */
2265 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR
2266#if 0
2267 /* We must allow this to work outside of functions so that
2268 static constants can be used for array sizes. */
2269 && current_function_decl != 0
2270 && DECL_MODE (decl) != BLKmode
2271#endif
2272 )
2273 return DECL_INITIAL (decl);
2274 return decl;
2275}
2276\f
2277/* Friend handling routines. */
2278/* Friend data structures:
2279
2280 Lists of friend functions come from TYPE_DECL nodes. Since all
2281 aggregate types are automatically typedef'd, these nodes are guaranteed
2282 to exist.
2283
2284 The TREE_PURPOSE of a friend list is the name of the friend,
2285 and its TREE_VALUE is another list.
2286
2287 For each element of that list, either the TREE_VALUE or the TREE_PURPOSE
2288 will be filled in, but not both. The TREE_VALUE of that list is an
2289 individual function which is a friend. The TREE_PURPOSE of that list
2290 indicates a type in which all functions by that name are friends.
2291
2292 Lists of friend classes come from _TYPE nodes. Love that consistency
2293 thang. */
2294
2295int
2296is_friend_type (type1, type2)
2297 tree type1, type2;
2298{
2299 return is_friend (type1, type2);
2300}
2301
2302int
2303is_friend (type, supplicant)
2304 tree type, supplicant;
2305{
2306 int declp;
2307 register tree list;
2308
2309 if (supplicant == NULL_TREE || type == NULL_TREE)
2310 return 0;
2311
2312 declp = (TREE_CODE_CLASS (TREE_CODE (supplicant)) == 'd');
2313
2314 if (declp)
2315 /* It's a function decl. */
2316 {
2317 tree list = DECL_FRIENDLIST (TYPE_NAME (type));
2318 tree name = DECL_NAME (supplicant);
2319 tree ctype = DECL_CLASS_CONTEXT (supplicant);
2320 for (; list ; list = TREE_CHAIN (list))
2321 {
2322 if (name == TREE_PURPOSE (list))
2323 {
2324 tree friends = TREE_VALUE (list);
2325 name = DECL_ASSEMBLER_NAME (supplicant);
2326 for (; friends ; friends = TREE_CHAIN (friends))
2327 {
2328 if (ctype == TREE_PURPOSE (friends))
2329 return 1;
2330 if (name == DECL_ASSEMBLER_NAME (TREE_VALUE (friends)))
2331 return 1;
2332 }
2333 break;
2334 }
2335 }
2336 }
2337 else
2338 /* It's a type. */
2339 {
2340 if (type == supplicant)
2341 return 1;
2342
2343 list = CLASSTYPE_FRIEND_CLASSES (TREE_TYPE (TYPE_NAME (type)));
2344 for (; list ; list = TREE_CHAIN (list))
2345 if (supplicant == TREE_VALUE (list))
2346 return 1;
2347 }
2348
2349 {
2350 tree context = declp ? DECL_CLASS_CONTEXT (supplicant)
2351 : DECL_CONTEXT (TYPE_NAME (supplicant));
2352
2353 if (context)
2354 return is_friend (type, context);
2355 }
2356
2357 return 0;
2358}
2359
2360/* Add a new friend to the friends of the aggregate type TYPE.
2361 DECL is the FUNCTION_DECL of the friend being added. */
2362static void
2363add_friend (type, decl)
2364 tree type, decl;
2365{
2366 tree typedecl = TYPE_NAME (type);
2367 tree list = DECL_FRIENDLIST (typedecl);
2368 tree name = DECL_NAME (decl);
2369
2370 while (list)
2371 {
2372 if (name == TREE_PURPOSE (list))
2373 {
2374 tree friends = TREE_VALUE (list);
2375 for (; friends ; friends = TREE_CHAIN (friends))
2376 {
2377 if (decl == TREE_VALUE (friends))
2378 {
e1cd6e56 2379 cp_warning ("`%D' is already a friend of class `%T'",
8926095f 2380 decl, type);
e1cd6e56 2381 cp_warning_at ("previous friend declaration of `%D'",
8926095f 2382 TREE_VALUE (friends));
8d08fdba
MS
2383 return;
2384 }
2385 }
2386 TREE_VALUE (list) = tree_cons (error_mark_node, decl,
2387 TREE_VALUE (list));
2388 return;
2389 }
2390 list = TREE_CHAIN (list);
2391 }
2392 DECL_FRIENDLIST (typedecl)
2393 = tree_cons (DECL_NAME (decl), build_tree_list (error_mark_node, decl),
2394 DECL_FRIENDLIST (typedecl));
2395 if (DECL_NAME (decl) == ansi_opname[(int) MODIFY_EXPR])
2396 {
2397 tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
2398 TYPE_HAS_ASSIGNMENT (TREE_TYPE (typedecl)) = 1;
2399 if (parmtypes && TREE_CHAIN (parmtypes))
2400 {
2401 tree parmtype = TREE_VALUE (TREE_CHAIN (parmtypes));
2402 if (TREE_CODE (parmtype) == REFERENCE_TYPE
2403 && TREE_TYPE (parmtypes) == TREE_TYPE (typedecl))
2404 TYPE_HAS_ASSIGN_REF (TREE_TYPE (typedecl)) = 1;
2405 }
2406 }
2407}
2408
2409/* Declare that every member function NAME in FRIEND_TYPE
2410 (which may be NULL_TREE) is a friend of type TYPE. */
2411static void
2412add_friends (type, name, friend_type)
2413 tree type, name, friend_type;
2414{
2415 tree typedecl = TYPE_NAME (type);
2416 tree list = DECL_FRIENDLIST (typedecl);
2417
2418 while (list)
2419 {
2420 if (name == TREE_PURPOSE (list))
2421 {
2422 tree friends = TREE_VALUE (list);
2423 while (friends && TREE_PURPOSE (friends) != friend_type)
2424 friends = TREE_CHAIN (friends);
2425 if (friends)
2426 if (friend_type)
2427 warning ("method `%s::%s' is already a friend of class",
2428 TYPE_NAME_STRING (friend_type),
2429 IDENTIFIER_POINTER (name));
2430 else
2431 warning ("function `%s' is already a friend of class `%s'",
2432 IDENTIFIER_POINTER (name),
2433 IDENTIFIER_POINTER (DECL_NAME (typedecl)));
2434 else
2435 TREE_VALUE (list) = tree_cons (friend_type, NULL_TREE,
2436 TREE_VALUE (list));
2437 return;
2438 }
2439 list = TREE_CHAIN (list);
2440 }
2441 DECL_FRIENDLIST (typedecl) =
2442 tree_cons (name,
2443 build_tree_list (friend_type, NULL_TREE),
2444 DECL_FRIENDLIST (typedecl));
2445 if (! strncmp (IDENTIFIER_POINTER (name),
2446 IDENTIFIER_POINTER (ansi_opname[(int) MODIFY_EXPR]),
2447 strlen (IDENTIFIER_POINTER (ansi_opname[(int) MODIFY_EXPR]))))
2448 {
2449 TYPE_HAS_ASSIGNMENT (TREE_TYPE (typedecl)) = 1;
2450 sorry ("declaring \"friend operator =\" will not find \"operator = (X&)\" if it exists");
2451 }
2452}
2453
2454/* Set up a cross reference so that type TYPE will make member function
2455 CTYPE::DECL a friend when CTYPE is finally defined. For more than
2456 one, set up a cross reference so that functions with the name DECL
2457 and type CTYPE know that they are friends of TYPE. */
2458static void
2459xref_friend (type, decl, ctype)
2460 tree type, decl, ctype;
2461{
8d08fdba
MS
2462 tree friend_decl = TYPE_NAME (ctype);
2463#if 0
8926095f 2464 tree typedecl = TYPE_NAME (type);
8d08fdba
MS
2465 tree t = tree_cons (NULL_TREE, ctype, DECL_UNDEFINED_FRIENDS (typedecl));
2466
2467 DECL_UNDEFINED_FRIENDS (typedecl) = t;
2468#else
2469 tree t = 0;
2470#endif
2471 SET_DECL_WAITING_FRIENDS (friend_decl,
2472 tree_cons (type, t,
2473 DECL_WAITING_FRIENDS (friend_decl)));
2474 TREE_TYPE (DECL_WAITING_FRIENDS (friend_decl)) = decl;
2475}
2476
2477/* Make FRIEND_TYPE a friend class to TYPE. If FRIEND_TYPE has already
2478 been defined, we make all of its member functions friends of
2479 TYPE. If not, we make it a pending friend, which can later be added
2480 when its definition is seen. If a type is defined, then its TYPE_DECL's
2481 DECL_UNDEFINED_FRIENDS contains a (possibly empty) list of friend
2482 classes that are not defined. If a type has not yet been defined,
2483 then the DECL_WAITING_FRIENDS contains a list of types
2484 waiting to make it their friend. Note that these two can both
2485 be in use at the same time! */
2486void
2487make_friend_class (type, friend_type)
2488 tree type, friend_type;
2489{
2490 tree classes;
2491
2492 if (IS_SIGNATURE (type))
2493 {
2494 error ("`friend' declaration in signature definition");
2495 return;
2496 }
2497 if (IS_SIGNATURE (friend_type))
2498 {
2499 error ("signature type `%s' declared `friend'",
2500 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (friend_type))));
2501 return;
2502 }
2503 if (type == friend_type)
2504 {
2505 warning ("class `%s' is implicitly friends with itself",
2506 TYPE_NAME_STRING (type));
2507 return;
2508 }
2509
2510 GNU_xref_hier (TYPE_NAME_STRING (type),
2511 TYPE_NAME_STRING (friend_type), 0, 0, 1);
2512
2513 classes = CLASSTYPE_FRIEND_CLASSES (type);
2514 while (classes && TREE_VALUE (classes) != friend_type)
2515 classes = TREE_CHAIN (classes);
2516 if (classes)
2517 warning ("class `%s' is already friends with class `%s'",
2518 TYPE_NAME_STRING (TREE_VALUE (classes)), TYPE_NAME_STRING (type));
2519 else
2520 {
2521 CLASSTYPE_FRIEND_CLASSES (type)
2522 = tree_cons (NULL_TREE, friend_type, CLASSTYPE_FRIEND_CLASSES (type));
2523 }
2524}
2525
2526/* Main friend processor. This is large, and for modularity purposes,
2527 has been removed from grokdeclarator. It returns `void_type_node'
2528 to indicate that something happened, though a FIELD_DECL is
2529 not returned.
2530
2531 CTYPE is the class this friend belongs to.
2532
2533 DECLARATOR is the name of the friend.
2534
2535 DECL is the FUNCTION_DECL that the friend is.
2536
2537 In case we are parsing a friend which is part of an inline
2538 definition, we will need to store PARM_DECL chain that comes
2539 with it into the DECL_ARGUMENTS slot of the FUNCTION_DECL.
2540
2541 FLAGS is just used for `grokclassfn'.
2542
2543 QUALS say what special qualifies should apply to the object
2544 pointed to by `this'. */
2545tree
2546do_friend (ctype, declarator, decl, parmdecls, flags, quals)
2547 tree ctype, declarator, decl, parmdecls;
2548 enum overload_flags flags;
2549 tree quals;
2550{
8d08fdba
MS
2551 /* Every decl that gets here is a friend of something. */
2552 DECL_FRIEND_P (decl) = 1;
2553
8d08fdba
MS
2554 if (ctype)
2555 {
2556 tree cname = TYPE_NAME (ctype);
2557 if (TREE_CODE (cname) == TYPE_DECL)
2558 cname = DECL_NAME (cname);
2559
2560 /* A method friend. */
2561 if (TREE_CODE (decl) == FUNCTION_DECL)
2562 {
2563 if (flags == NO_SPECIAL && ctype && declarator == cname)
2564 DECL_CONSTRUCTOR_P (decl) = 1;
2565
2566 /* This will set up DECL_ARGUMENTS for us. */
2567 grokclassfn (ctype, cname, decl, flags, quals);
2568 if (TYPE_SIZE (ctype) != 0)
2569 check_classfn (ctype, cname, decl);
2570
2571 if (TREE_TYPE (decl) != error_mark_node)
2572 {
2573 if (TYPE_SIZE (ctype))
2574 {
2575 /* We don't call pushdecl here yet, or ever on this
2576 actual FUNCTION_DECL. We must preserve its TREE_CHAIN
2577 until the end. */
2578 make_decl_rtl (decl, NULL_PTR, 1);
2579 add_friend (current_class_type, decl);
2580 }
2581 else
2582 {
2583 register char *classname
2584 = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (ctype)));
2585
2586 error ("member declared as friend before type `%s' defined",
2587 classname);
2588 }
2589 }
2590 }
2591 else
2592 {
2593 /* Possibly a bunch of method friends. */
2594
2595 /* Get the class they belong to. */
2596 tree ctype = IDENTIFIER_TYPE_VALUE (cname);
2597
2598 /* This class is defined, use its methods now. */
2599 if (TYPE_SIZE (ctype))
2600 {
2601 tree fields = lookup_fnfields (TYPE_BINFO (ctype), declarator, 0);
2602 if (fields)
2603 add_friends (current_class_type, declarator, ctype);
2604 else
2605 error ("method `%s' is not a member of class `%s'",
2606 IDENTIFIER_POINTER (declarator),
2607 IDENTIFIER_POINTER (cname));
2608 }
2609 else
2610 /* Note: DECLARATOR actually has more than one; in this
2611 case, we're making sure that fns with the name DECLARATOR
2612 and type CTYPE know they are friends of the current
2613 class type. */
2614 xref_friend (current_class_type, declarator, ctype);
2615 decl = void_type_node;
2616 }
2617 }
8d08fdba
MS
2618 else if (TREE_CODE (decl) == FUNCTION_DECL
2619 && ((IDENTIFIER_LENGTH (declarator) == 4
2620 && IDENTIFIER_POINTER (declarator)[0] == 'm'
2621 && ! strcmp (IDENTIFIER_POINTER (declarator), "main"))
2622 || (IDENTIFIER_LENGTH (declarator) > 10
2623 && IDENTIFIER_POINTER (declarator)[0] == '_'
2624 && IDENTIFIER_POINTER (declarator)[1] == '_'
2625 && strncmp (IDENTIFIER_POINTER (declarator)+2,
a3203465 2626 "builtin_", 8) == 0)))
8d08fdba
MS
2627 {
2628 /* raw "main", and builtin functions never gets overloaded,
2629 but they can become friends. */
8d08fdba
MS
2630 add_friend (current_class_type, decl);
2631 DECL_FRIEND_P (decl) = 1;
8d08fdba
MS
2632 decl = void_type_node;
2633 }
2634 /* A global friend.
2635 @@ or possibly a friend from a base class ?!? */
2636 else if (TREE_CODE (decl) == FUNCTION_DECL)
2637 {
2638 /* Friends must all go through the overload machinery,
2639 even though they may not technically be overloaded.
2640
2641 Note that because classes all wind up being top-level
2642 in their scope, their friend wind up in top-level scope as well. */
2643 DECL_ASSEMBLER_NAME (decl)
2644 = build_decl_overload (declarator, TYPE_ARG_TYPES (TREE_TYPE (decl)),
2645 TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE);
2646 DECL_ARGUMENTS (decl) = parmdecls;
2647 DECL_CLASS_CONTEXT (decl) = current_class_type;
2648
2649 /* We can call pushdecl here, because the TREE_CHAIN of this
2650 FUNCTION_DECL is not needed for other purposes. */
a4443a08 2651 decl = pushdecl (decl);
8d08fdba
MS
2652
2653 make_decl_rtl (decl, NULL_PTR, 1);
2654 add_friend (current_class_type, decl);
2655
8d08fdba 2656 DECL_FRIEND_P (decl) = 1;
7177d104 2657#if 0
8d08fdba 2658 TREE_OVERLOADED (declarator) = 1;
7177d104 2659#endif
8d08fdba
MS
2660 }
2661 else
2662 {
2663 /* @@ Should be able to ingest later definitions of this function
2664 before use. */
700f8a87 2665 tree decl = lookup_name_nonclass (declarator);
8d08fdba
MS
2666 if (decl == NULL_TREE)
2667 {
2668 warning ("implicitly declaring `%s' as struct",
2669 IDENTIFIER_POINTER (declarator));
2670 decl = xref_tag (record_type_node, declarator, NULL_TREE, 1);
2671 decl = TYPE_NAME (decl);
2672 }
2673
2674 /* Allow abbreviated declarations of overloaded functions,
2675 but not if those functions are really class names. */
2676 if (TREE_CODE (decl) == TREE_LIST && TREE_TYPE (TREE_PURPOSE (decl)))
2677 {
2678 warning ("`friend %s' archaic, use `friend class %s' instead",
2679 IDENTIFIER_POINTER (declarator),
2680 IDENTIFIER_POINTER (declarator));
2681 decl = TREE_TYPE (TREE_PURPOSE (decl));
2682 }
2683
2684 if (TREE_CODE (decl) == TREE_LIST)
2685 add_friends (current_class_type, TREE_PURPOSE (decl), NULL_TREE);
2686 else
2687 make_friend_class (current_class_type, TREE_TYPE (decl));
2688 decl = void_type_node;
2689 }
2690 return decl;
2691}
2692
2693/* TYPE has now been defined. It may, however, have a number of things
2694 waiting make make it their friend. We resolve these references
2695 here. */
2696void
2697embrace_waiting_friends (type)
2698 tree type;
2699{
2700 tree decl = TYPE_NAME (type);
2701 tree waiters;
2702
2703 if (TREE_CODE (decl) != TYPE_DECL)
2704 return;
2705
2706 for (waiters = DECL_WAITING_FRIENDS (decl); waiters;
2707 waiters = TREE_CHAIN (waiters))
2708 {
2709 tree waiter = TREE_PURPOSE (waiters);
2710#if 0
2711 tree waiter_prev = TREE_VALUE (waiters);
2712#endif
2713 tree decl = TREE_TYPE (waiters);
2714 tree name = decl ? (TREE_CODE (decl) == IDENTIFIER_NODE
2715 ? decl : DECL_NAME (decl)) : NULL_TREE;
2716 if (name)
2717 {
2718 /* @@ There may be work to be done since we have not verified
2719 @@ consistency between original and friend declarations
2720 @@ of the functions waiting to become friends. */
2721 tree field = lookup_fnfields (TYPE_BINFO (type), name, 0);
2722 if (field)
2723 if (decl == name)
2724 add_friends (waiter, name, type);
2725 else
2726 add_friend (waiter, decl);
2727 else
2728 error_with_file_and_line (DECL_SOURCE_FILE (TYPE_NAME (waiter)),
2729 DECL_SOURCE_LINE (TYPE_NAME (waiter)),
2730 "no method `%s' defined in class `%s' to be friend",
2731 IDENTIFIER_POINTER (DECL_NAME (TREE_TYPE (waiters))),
2732 TYPE_NAME_STRING (type));
2733 }
2734 else
2735 make_friend_class (type, waiter);
2736
2737#if 0
2738 if (TREE_CHAIN (waiter_prev))
2739 TREE_CHAIN (waiter_prev) = TREE_CHAIN (TREE_CHAIN (waiter_prev));
2740 else
2741 DECL_UNDEFINED_FRIENDS (TYPE_NAME (waiter)) = NULL_TREE;
2742#endif
2743 }
2744}
2745\f
2746/* Common subroutines of build_new and build_vec_delete. */
2747
2748/* Common interface for calling "builtin" functions that are not
2749 really builtin. */
2750
2751tree
2752build_builtin_call (type, node, arglist)
2753 tree type;
2754 tree node;
2755 tree arglist;
2756{
2757 tree rval = build (CALL_EXPR, type, node, arglist, 0);
2758 TREE_SIDE_EFFECTS (rval) = 1;
2759 assemble_external (TREE_OPERAND (node, 0));
2760 TREE_USED (TREE_OPERAND (node, 0)) = 1;
2761 return rval;
2762}
2763\f
2764/* Generate a C++ "new" expression. DECL is either a TREE_LIST
2765 (which needs to go through some sort of groktypename) or it
2766 is the name of the class we are newing. INIT is an initialization value.
2767 It is either an EXPRLIST, an EXPR_NO_COMMAS, or something in braces.
2768 If INIT is void_type_node, it means do *not* call a constructor
2769 for this instance.
2770
2771 For types with constructors, the data returned is initialized
2772 by the appropriate constructor.
2773
2774 Whether the type has a constructor or not, if it has a pointer
2775 to a virtual function table, then that pointer is set up
2776 here.
2777
2778 Unless I am mistaken, a call to new () will return initialized
2779 data regardless of whether the constructor itself is private or
8926095f 2780 not. NOPE; new fails if the constructor is private (jcm).
8d08fdba
MS
2781
2782 Note that build_new does nothing to assure that any special
2783 alignment requirements of the type are met. Rather, it leaves
2784 it up to malloc to do the right thing. Otherwise, folding to
2785 the right alignment cal cause problems if the user tries to later
2786 free the memory returned by `new'.
2787
2788 PLACEMENT is the `placement' list for user-defined operator new (). */
2789
2790tree
2791build_new (placement, decl, init, use_global_new)
2792 tree placement;
2793 tree decl, init;
2794 int use_global_new;
2795{
2796 tree type, true_type, size, rval;
8926095f
MS
2797 tree nelts;
2798 int has_array = 0;
a28e3c7f 2799 enum tree_code code = NEW_EXPR;
8d08fdba
MS
2800
2801 tree pending_sizes = NULL_TREE;
2802
2803 if (decl == error_mark_node)
2804 return error_mark_node;
2805
2806 if (TREE_CODE (decl) == TREE_LIST)
2807 {
2808 tree absdcl = TREE_VALUE (decl);
2809 tree last_absdcl = NULL_TREE;
2810 int old_immediate_size_expand;
2811
2812 if (current_function_decl
2813 && DECL_CONSTRUCTOR_P (current_function_decl))
2814 {
2815 old_immediate_size_expand = immediate_size_expand;
2816 immediate_size_expand = 0;
2817 }
2818
2819 nelts = integer_one_node;
2820
2821 if (absdcl && TREE_CODE (absdcl) == CALL_EXPR)
8926095f 2822 my_friendly_abort (215);
8d08fdba
MS
2823 while (absdcl && TREE_CODE (absdcl) == INDIRECT_REF)
2824 {
2825 last_absdcl = absdcl;
2826 absdcl = TREE_OPERAND (absdcl, 0);
2827 }
2828
2829 if (absdcl && TREE_CODE (absdcl) == ARRAY_REF)
2830 {
2831 /* probably meant to be a vec new */
2832 tree this_nelts;
2833
51c184be
MS
2834 while (TREE_OPERAND (absdcl, 0)
2835 && TREE_CODE (TREE_OPERAND (absdcl, 0)) == ARRAY_REF)
2836 {
2837 last_absdcl = absdcl;
2838 absdcl = TREE_OPERAND (absdcl, 0);
2839 }
2840
8d08fdba
MS
2841 has_array = 1;
2842 this_nelts = TREE_OPERAND (absdcl, 1);
2843 if (this_nelts != error_mark_node)
2844 {
2845 if (this_nelts == NULL_TREE)
2846 error ("new of array type fails to specify size");
2847 else
2848 {
8926095f 2849 this_nelts = save_expr (convert (sizetype, this_nelts));
8d08fdba
MS
2850 absdcl = TREE_OPERAND (absdcl, 0);
2851 if (this_nelts == integer_zero_node)
2852 {
2853 warning ("zero size array reserves no space");
2854 nelts = integer_zero_node;
2855 }
2856 else
2857 nelts = build_binary_op (MULT_EXPR, nelts, this_nelts, 1);
2858 }
2859 }
2860 else
2861 nelts = integer_zero_node;
2862 }
2863
2864 if (last_absdcl)
2865 TREE_OPERAND (last_absdcl, 0) = absdcl;
2866 else
2867 TREE_VALUE (decl) = absdcl;
2868
2869 type = true_type = groktypename (decl);
8926095f 2870 if (! type || type == error_mark_node)
8d08fdba
MS
2871 {
2872 immediate_size_expand = old_immediate_size_expand;
2873 return error_mark_node;
2874 }
2875
8d08fdba
MS
2876 if (current_function_decl
2877 && DECL_CONSTRUCTOR_P (current_function_decl))
2878 {
2879 pending_sizes = get_pending_sizes ();
2880 immediate_size_expand = old_immediate_size_expand;
2881 }
2882 }
2883 else if (TREE_CODE (decl) == IDENTIFIER_NODE)
2884 {
2885 if (IDENTIFIER_HAS_TYPE_VALUE (decl))
2886 {
2887 /* An aggregate type. */
2888 type = IDENTIFIER_TYPE_VALUE (decl);
2889 decl = TYPE_NAME (type);
2890 }
2891 else
2892 {
2893 /* A builtin type. */
2894 decl = lookup_name (decl, 1);
2895 my_friendly_assert (TREE_CODE (decl) == TYPE_DECL, 215);
2896 type = TREE_TYPE (decl);
2897 }
2898 true_type = type;
2899 }
2900 else if (TREE_CODE (decl) == TYPE_DECL)
2901 {
2902 type = TREE_TYPE (decl);
2903 true_type = type;
2904 }
2905 else
2906 {
2907 type = decl;
2908 true_type = type;
2909 decl = TYPE_NAME (type);
2910 }
2911
8926095f
MS
2912 /* ``A reference cannot be created by the new operator. A reference
2913 is not an object (8.2.2, 8.4.3), so a pointer to it could not be
2914 returned by new.'' ARM 5.3.3 */
2915 if (TREE_CODE (type) == REFERENCE_TYPE)
8d08fdba 2916 {
8926095f
MS
2917 error ("new cannot be applied to a reference type");
2918 type = true_type = TREE_TYPE (type);
8d08fdba
MS
2919 }
2920
8926095f
MS
2921 /* When the object being created is an array, the new-expression yields a
2922 pointer to the initial element (if any) of the array. For example,
2923 both new int and new int[10] return an int*. 5.3.4. */
2924 if (TREE_CODE (type) == ARRAY_TYPE && has_array == 0)
8d08fdba 2925 {
8926095f
MS
2926 nelts = array_type_nelts_top (type);
2927 has_array = 1;
2928 type = true_type = TREE_TYPE (type);
8d08fdba
MS
2929 }
2930
8926095f 2931 if (TYPE_READONLY (type) || TYPE_VOLATILE (type))
8d08fdba 2932 {
8926095f
MS
2933 pedwarn ("const and volatile types cannot be created with operator new");
2934 type = true_type = TYPE_MAIN_VARIANT (type);
8d08fdba 2935 }
8926095f 2936
8d08fdba
MS
2937 /* If our base type is an array, then make sure we know how many elements
2938 it has. */
2939 while (TREE_CODE (true_type) == ARRAY_TYPE)
2940 {
2941 tree this_nelts = array_type_nelts_top (true_type);
8926095f 2942 nelts = build_binary_op (MULT_EXPR, nelts, this_nelts, 1);
8d08fdba
MS
2943 true_type = TREE_TYPE (true_type);
2944 }
2945 if (has_array)
2946 size = fold (build_binary_op (MULT_EXPR, size_in_bytes (true_type),
2947 nelts, 1));
2948 else
2949 size = size_in_bytes (type);
2950
e1cd6e56
MS
2951 if (true_type == void_type_node)
2952 {
2953 error ("invalid type for new: `void'");
2954 return error_mark_node;
2955 }
2956
8926095f
MS
2957 if (TYPE_SIZE (true_type) == 0)
2958 {
e1cd6e56 2959 incomplete_type_error (0, true_type);
8926095f
MS
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
6060a796 3117 init = build_c_cast (type, init, 1);
8d08fdba
MS
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
6060a796 3154 expand_aggr_init (build_indirect_ref (rval, NULL_PTR), init, 0, 0);
8d08fdba
MS
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] * */
6060a796 3182 rval = build_c_cast (build_pointer_type (type), rval, 0);
51c184be
MS
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;
6060a796 3276 expand_aggr_init (baseref, TREE_VALUE (elts), 0, 0);
8d08fdba
MS
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))
6060a796 3360 expand_aggr_init (to, from, 0, 0);
8d08fdba
MS
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
6060a796 3374 expand_aggr_init (build1 (INDIRECT_REF, type, base), init, 0, 0);
8d08fdba
MS
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 */
6060a796 3491 addr = convert_force (build_pointer_type (type), addr, 0);
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
6060a796 3517 addr = convert_force (build_pointer_type (type), addr, 0);
8d08fdba
MS
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)),
6060a796 3806 addr, 0);
8d08fdba
MS
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}