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