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