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