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