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