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