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