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