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