7b9ab1d52e2704add1beffbf7a9ab18995c453b2
[gcc.git] / gcc / cp / init.c
1 /* Handle initialization things in C++.
2 Copyright (C) 1987, 89, 92-98, 1999, 2000 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 /* High-level class interface. */
23
24 #include "config.h"
25 #include "system.h"
26 #include "tree.h"
27 #include "rtl.h"
28 #include "cp-tree.h"
29 #include "flags.h"
30 #include "output.h"
31 #include "except.h"
32 #include "expr.h"
33 #include "toplev.h"
34 #include "ggc.h"
35
36 static void expand_aggr_vbase_init_1 PARAMS ((tree, tree, tree, tree));
37 static void construct_virtual_bases PARAMS ((tree, tree, tree, tree, tree));
38 static void expand_aggr_init_1 PARAMS ((tree, tree, tree, tree, int));
39 static void expand_default_init PARAMS ((tree, tree, tree, tree, int));
40 static tree build_vec_delete_1 PARAMS ((tree, tree, tree, tree, int));
41 static void perform_member_init PARAMS ((tree, tree, tree, int));
42 static void sort_base_init PARAMS ((tree, tree *, tree *));
43 static tree build_builtin_delete_call PARAMS ((tree));
44 static int member_init_ok_or_else PARAMS ((tree, tree, const char *));
45 static void expand_virtual_init PARAMS ((tree, tree));
46 static tree sort_member_init PARAMS ((tree));
47 static tree initializing_context PARAMS ((tree));
48 static tree build_java_class_ref PARAMS ((tree));
49 static void expand_cleanup_for_base PARAMS ((tree, tree));
50 static tree get_temp_regvar PARAMS ((tree, tree));
51 static tree dfs_initialize_vtbl_ptrs PARAMS ((tree, void *));
52
53 /* Set up local variable for this file. MUST BE CALLED AFTER
54 INIT_DECL_PROCESSING. */
55
56 static tree BI_header_type, BI_header_size;
57
58 void init_init_processing ()
59 {
60 tree fields[1];
61
62 minus_one_node = build_int_2 (-1, -1);
63
64 /* Define the structure that holds header information for
65 arrays allocated via operator new. */
66 BI_header_type = make_aggr_type (RECORD_TYPE);
67 nelts_identifier = get_identifier ("nelts");
68 fields[0] = build_lang_decl (FIELD_DECL, nelts_identifier, sizetype);
69
70 /* Use the biggest alignment supported by the target to prevent operator
71 new from returning misaligned pointers. */
72 TYPE_ALIGN (BI_header_type) = BIGGEST_ALIGNMENT;
73 finish_builtin_type (BI_header_type, "__new_cookie", fields,
74 0, BI_header_type);
75 BI_header_size = size_in_bytes (BI_header_type);
76
77 ggc_add_tree_root (&BI_header_type, 1);
78 ggc_add_tree_root (&BI_header_size, 1);
79 }
80
81 /* Called from initialize_vtbl_ptrs via dfs_walk. */
82
83 static tree
84 dfs_initialize_vtbl_ptrs (binfo, data)
85 tree binfo;
86 void *data;
87 {
88 if (!BINFO_PRIMARY_MARKED_P (binfo)
89 && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo)))
90 {
91 tree base_ptr = TREE_VALUE ((tree) data);
92
93 if (TREE_VIA_VIRTUAL (binfo))
94 base_ptr = convert_pointer_to_vbase (BINFO_TYPE (binfo),
95 base_ptr);
96 else
97 base_ptr
98 = build_vbase_path (PLUS_EXPR,
99 build_pointer_type (BINFO_TYPE (binfo)),
100 base_ptr,
101 binfo,
102 /*nonnull=*/1);
103
104 expand_virtual_init (binfo, base_ptr);
105 }
106
107 SET_BINFO_MARKED (binfo);
108
109 return NULL_TREE;
110 }
111
112 /* Initialize all the vtable pointers for the hierarchy dominated by
113 TYPE. */
114
115 void
116 initialize_vtbl_ptrs (type, addr)
117 tree type;
118 tree addr;
119 {
120 tree list = build_tree_list (type, addr);
121
122 /* Walk through the hierarchy, initializing the vptr in each base
123 class. We do these in pre-order because under the new ABI we
124 can't find the virtual bases for a class until we've initialized
125 the vtbl for that class. */
126 dfs_walk_real (TYPE_BINFO (type), dfs_initialize_vtbl_ptrs,
127 NULL, dfs_unmarked_real_bases_queue_p, list);
128 dfs_walk (TYPE_BINFO (type), dfs_unmark,
129 dfs_marked_real_bases_queue_p, type);
130 if (TYPE_USES_VIRTUAL_BASECLASSES (type))
131 expand_indirect_vtbls_init (TYPE_BINFO (type), addr);
132 }
133
134 \f
135 /* 348 - 351 */
136 /* Subroutine of emit_base_init. */
137
138 static void
139 perform_member_init (member, name, init, explicit)
140 tree member, name, init;
141 int explicit;
142 {
143 tree decl;
144 tree type = TREE_TYPE (member);
145
146 decl = build_component_ref (current_class_ref, name, NULL_TREE, explicit);
147
148 if (decl == error_mark_node)
149 return;
150
151 /* Deal with this here, as we will get confused if we try to call the
152 assignment op for an anonymous union. This can happen in a
153 synthesized copy constructor. */
154 if (ANON_AGGR_TYPE_P (type))
155 {
156 init = build (INIT_EXPR, type, decl, TREE_VALUE (init));
157 finish_expr_stmt (init);
158 }
159 else if (TYPE_NEEDS_CONSTRUCTING (type)
160 || (init && TYPE_HAS_CONSTRUCTOR (type)))
161 {
162 /* Since `init' is already a TREE_LIST on the current_member_init_list,
163 only build it into one if we aren't already a list. */
164 if (init != NULL_TREE && TREE_CODE (init) != TREE_LIST)
165 init = build_tree_list (NULL_TREE, init);
166
167 if (explicit
168 && TREE_CODE (type) == ARRAY_TYPE
169 && init != NULL_TREE
170 && TREE_CHAIN (init) == NULL_TREE
171 && TREE_CODE (TREE_TYPE (TREE_VALUE (init))) == ARRAY_TYPE)
172 {
173 /* Initialization of one array from another. */
174 finish_expr_stmt
175 (build_vec_init (TREE_OPERAND (decl, 1), decl,
176 array_type_nelts (type), TREE_VALUE (init), 1));
177 }
178 else
179 finish_expr_stmt (build_aggr_init (decl, init, 0));
180 }
181 else
182 {
183 if (init == NULL_TREE)
184 {
185 if (explicit)
186 {
187 /* default-initialization. */
188 if (AGGREGATE_TYPE_P (type))
189 {
190 /* This is a default initialization of an aggregate,
191 but not one of non-POD class type. We cleverly
192 notice that the initialization rules in such a
193 case are the same as for initialization with an
194 empty brace-initialization list. We don't want
195 to call build_modify_expr as that will go looking
196 for constructors and such. */
197 tree e = build (CONSTRUCTOR, type, NULL_TREE, NULL_TREE);
198 TREE_SIDE_EFFECTS (e) = 1;
199 finish_expr_stmt (build (INIT_EXPR, type, decl, e));
200 }
201 else if (TREE_CODE (type) == REFERENCE_TYPE)
202 cp_error ("default-initialization of `%#D', which has reference type",
203 member);
204 else
205 init = integer_zero_node;
206 }
207 /* member traversal: note it leaves init NULL */
208 else if (TREE_CODE (TREE_TYPE (member)) == REFERENCE_TYPE)
209 cp_pedwarn ("uninitialized reference member `%D'", member);
210 }
211 else if (TREE_CODE (init) == TREE_LIST)
212 {
213 /* There was an explicit member initialization. Do some
214 work in that case. */
215 if (TREE_CHAIN (init))
216 {
217 warning ("initializer list treated as compound expression");
218 init = build_compound_expr (init);
219 }
220 else
221 init = TREE_VALUE (init);
222 }
223
224 if (init)
225 finish_expr_stmt (build_modify_expr (decl, INIT_EXPR, init));
226 }
227
228 if (TYPE_NEEDS_DESTRUCTOR (type))
229 {
230 tree expr;
231
232 expr = build_component_ref (current_class_ref, name, NULL_TREE,
233 explicit);
234 expr = build_delete (type, expr, integer_zero_node,
235 LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR, 0);
236
237 if (expr != error_mark_node)
238 finish_subobject (expr);
239 }
240 }
241
242 extern int warn_reorder;
243
244 /* Subroutine of emit_member_init. */
245
246 static tree
247 sort_member_init (t)
248 tree t;
249 {
250 tree x, member, name, field;
251 tree init_list = NULL_TREE;
252 int last_pos = 0;
253 tree last_field = NULL_TREE;
254
255 for (member = TYPE_FIELDS (t); member ; member = TREE_CHAIN (member))
256 {
257 int pos;
258
259 /* member could be, for example, a CONST_DECL for an enumerated
260 tag; we don't want to try to initialize that, since it already
261 has a value. */
262 if (TREE_CODE (member) != FIELD_DECL || !DECL_NAME (member))
263 continue;
264
265 for (x = current_member_init_list, pos = 0; x; x = TREE_CHAIN (x), ++pos)
266 {
267 /* If we cleared this out, then pay no attention to it. */
268 if (TREE_PURPOSE (x) == NULL_TREE)
269 continue;
270 name = TREE_PURPOSE (x);
271
272 if (TREE_CODE (name) == IDENTIFIER_NODE)
273 field = IDENTIFIER_CLASS_VALUE (name);
274 else
275 {
276 my_friendly_assert (TREE_CODE (name) == FIELD_DECL, 348);
277 field = name;
278 }
279
280 /* If one member shadows another, get the outermost one. */
281 if (TREE_CODE (field) == TREE_LIST)
282 field = TREE_VALUE (field);
283
284 if (field == member)
285 {
286 if (warn_reorder)
287 {
288 if (pos < last_pos)
289 {
290 cp_warning_at ("member initializers for `%#D'", last_field);
291 cp_warning_at (" and `%#D'", field);
292 warning (" will be re-ordered to match declaration order");
293 }
294 last_pos = pos;
295 last_field = field;
296 }
297
298 /* Make sure we won't try to work on this init again. */
299 TREE_PURPOSE (x) = NULL_TREE;
300 x = build_tree_list (name, TREE_VALUE (x));
301 goto got_it;
302 }
303 }
304
305 /* If we didn't find MEMBER in the list, create a dummy entry
306 so the two lists (INIT_LIST and the list of members) will be
307 symmetrical. */
308 x = build_tree_list (NULL_TREE, NULL_TREE);
309 got_it:
310 init_list = chainon (init_list, x);
311 }
312
313 /* Initializers for base members go at the end. */
314 for (x = current_member_init_list ; x ; x = TREE_CHAIN (x))
315 {
316 name = TREE_PURPOSE (x);
317 if (name)
318 {
319 if (purpose_member (name, init_list))
320 {
321 cp_error ("multiple initializations given for member `%D'",
322 IDENTIFIER_CLASS_VALUE (name));
323 continue;
324 }
325
326 init_list = chainon (init_list,
327 build_tree_list (name, TREE_VALUE (x)));
328 TREE_PURPOSE (x) = NULL_TREE;
329 }
330 }
331
332 return init_list;
333 }
334
335 static void
336 sort_base_init (t, rbase_ptr, vbase_ptr)
337 tree t, *rbase_ptr, *vbase_ptr;
338 {
339 tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
340 int n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
341
342 int i;
343 tree x;
344 tree last;
345
346 /* For warn_reorder. */
347 int last_pos = 0;
348 tree last_base = NULL_TREE;
349
350 tree rbases = NULL_TREE;
351 tree vbases = NULL_TREE;
352
353 /* First walk through and splice out vbase and invalid initializers.
354 Also replace names with binfos. */
355
356 last = tree_cons (NULL_TREE, NULL_TREE, current_base_init_list);
357 for (x = TREE_CHAIN (last); x; x = TREE_CHAIN (x))
358 {
359 tree basetype = TREE_PURPOSE (x);
360 tree binfo = NULL_TREE;
361
362 if (basetype == NULL_TREE)
363 {
364 /* Initializer for single base class. Must not
365 use multiple inheritance or this is ambiguous. */
366 switch (n_baseclasses)
367 {
368 case 0:
369 cp_error ("`%T' does not have a base class to initialize",
370 current_class_type);
371 return;
372 case 1:
373 break;
374 default:
375 cp_error ("unnamed initializer ambiguous for `%T' which uses multiple inheritance",
376 current_class_type);
377 return;
378 }
379 binfo = TREE_VEC_ELT (binfos, 0);
380 }
381 else if (is_aggr_type (basetype, 1))
382 {
383 binfo = binfo_or_else (basetype, t);
384 if (binfo == NULL_TREE)
385 continue;
386
387 /* Virtual base classes are special cases. Their initializers
388 are recorded with this constructor, and they are used when
389 this constructor is the top-level constructor called. */
390 if (TREE_VIA_VIRTUAL (binfo))
391 {
392 tree v = BINFO_FOR_VBASE (BINFO_TYPE (binfo), t);
393 vbases = tree_cons (v, TREE_VALUE (x), vbases);
394 continue;
395 }
396 else
397 {
398 /* Otherwise, if it is not an immediate base class, complain. */
399 for (i = n_baseclasses-1; i >= 0; i--)
400 if (BINFO_TYPE (binfo) == BINFO_TYPE (TREE_VEC_ELT (binfos, i)))
401 break;
402 if (i < 0)
403 {
404 cp_error ("`%T' is not an immediate base class of `%T'",
405 basetype, current_class_type);
406 continue;
407 }
408 }
409 }
410 else
411 my_friendly_abort (365);
412
413 TREE_PURPOSE (x) = binfo;
414 TREE_CHAIN (last) = x;
415 last = x;
416 }
417 TREE_CHAIN (last) = NULL_TREE;
418
419 /* Now walk through our regular bases and make sure they're initialized. */
420
421 for (i = 0; i < n_baseclasses; ++i)
422 {
423 tree base_binfo = TREE_VEC_ELT (binfos, i);
424 int pos;
425
426 if (TREE_VIA_VIRTUAL (base_binfo))
427 continue;
428
429 for (x = current_base_init_list, pos = 0; x; x = TREE_CHAIN (x), ++pos)
430 {
431 tree binfo = TREE_PURPOSE (x);
432
433 if (binfo == NULL_TREE)
434 continue;
435
436 if (binfo == base_binfo)
437 {
438 if (warn_reorder)
439 {
440 if (pos < last_pos)
441 {
442 cp_warning_at ("base initializers for `%#T'", last_base);
443 cp_warning_at (" and `%#T'", BINFO_TYPE (binfo));
444 warning (" will be re-ordered to match inheritance order");
445 }
446 last_pos = pos;
447 last_base = BINFO_TYPE (binfo);
448 }
449
450 /* Make sure we won't try to work on this init again. */
451 TREE_PURPOSE (x) = NULL_TREE;
452 x = build_tree_list (binfo, TREE_VALUE (x));
453 goto got_it;
454 }
455 }
456
457 /* If we didn't find BASE_BINFO in the list, create a dummy entry
458 so the two lists (RBASES and the list of bases) will be
459 symmetrical. */
460 x = build_tree_list (NULL_TREE, NULL_TREE);
461 got_it:
462 rbases = chainon (rbases, x);
463 }
464
465 *rbase_ptr = rbases;
466 *vbase_ptr = vbases;
467 }
468
469 /* Perform whatever initializations have yet to be done on the base
470 class of the class variable. These actions are in the global
471 variable CURRENT_BASE_INIT_LIST. Such an action could be
472 NULL_TREE, meaning that the user has explicitly called the base
473 class constructor with no arguments.
474
475 If there is a need for a call to a constructor, we must surround
476 that call with a pushlevel/poplevel pair, since we are technically
477 at the PARM level of scope.
478
479 Argument IMMEDIATELY, if zero, forces a new sequence to be
480 generated to contain these new insns, so it can be emitted later.
481 This sequence is saved in the global variable BASE_INIT_EXPR.
482 Otherwise, the insns are emitted into the current sequence.
483
484 Note that emit_base_init does *not* initialize virtual base
485 classes. That is done specially, elsewhere. */
486
487 tree
488 emit_base_init (t)
489 tree t;
490 {
491 tree member;
492 tree mem_init_list;
493 tree rbase_init_list, vbase_init_list;
494 tree t_binfo = TYPE_BINFO (t);
495 tree binfos = BINFO_BASETYPES (t_binfo);
496 int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
497 tree stmt_expr;
498 tree compound_stmt;
499
500 mem_init_list = sort_member_init (t);
501 current_member_init_list = NULL_TREE;
502
503 sort_base_init (t, &rbase_init_list, &vbase_init_list);
504 current_base_init_list = NULL_TREE;
505
506 begin_init_stmts (&stmt_expr, &compound_stmt);
507
508 /* First, initialize the virtual base classes, if we are
509 constructing the most-derived object. */
510 if (TYPE_USES_VIRTUAL_BASECLASSES (t))
511 {
512 tree first_arg = TREE_CHAIN (DECL_ARGUMENTS (current_function_decl));
513 construct_virtual_bases (t, current_class_ref, current_class_ptr,
514 vbase_init_list, first_arg);
515 }
516
517 /* Now, perform initialization of non-virtual base classes. */
518 for (i = 0; i < n_baseclasses; i++)
519 {
520 tree base_binfo = TREE_VEC_ELT (binfos, i);
521 tree init = void_list_node;
522
523 if (TREE_VIA_VIRTUAL (base_binfo))
524 continue;
525
526 my_friendly_assert (BINFO_INHERITANCE_CHAIN (base_binfo) == t_binfo,
527 999);
528
529 if (TREE_PURPOSE (rbase_init_list))
530 init = TREE_VALUE (rbase_init_list);
531 else if (TYPE_NEEDS_CONSTRUCTING (BINFO_TYPE (base_binfo)))
532 {
533 init = NULL_TREE;
534 if (extra_warnings && copy_args_p (current_function_decl))
535 cp_warning ("base class `%#T' should be explicitly initialized in the copy constructor",
536 BINFO_TYPE (base_binfo));
537 }
538
539 if (init != void_list_node)
540 {
541 member = convert_pointer_to_real (base_binfo, current_class_ptr);
542 expand_aggr_init_1 (base_binfo, NULL_TREE,
543 build_indirect_ref (member, NULL_PTR), init,
544 LOOKUP_NORMAL);
545 }
546
547 expand_cleanup_for_base (base_binfo, NULL_TREE);
548 rbase_init_list = TREE_CHAIN (rbase_init_list);
549 }
550
551 /* Initialize the vtable pointers for the class. */
552 initialize_vtbl_ptrs (t, current_class_ptr);
553
554 for (member = TYPE_FIELDS (t); member; member = TREE_CHAIN (member))
555 {
556 tree init, name;
557 int from_init_list;
558
559 /* member could be, for example, a CONST_DECL for an enumerated
560 tag; we don't want to try to initialize that, since it already
561 has a value. */
562 if (TREE_CODE (member) != FIELD_DECL || !DECL_NAME (member))
563 continue;
564
565 /* See if we had a user-specified member initialization. */
566 if (TREE_PURPOSE (mem_init_list))
567 {
568 name = TREE_PURPOSE (mem_init_list);
569 init = TREE_VALUE (mem_init_list);
570 from_init_list = 1;
571
572 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE
573 || TREE_CODE (name) == FIELD_DECL, 349);
574 }
575 else
576 {
577 name = DECL_NAME (member);
578 init = DECL_INITIAL (member);
579
580 from_init_list = 0;
581
582 /* Effective C++ rule 12. */
583 if (warn_ecpp && init == NULL_TREE
584 && !DECL_ARTIFICIAL (member)
585 && TREE_CODE (TREE_TYPE (member)) != ARRAY_TYPE)
586 cp_warning ("`%D' should be initialized in the member initialization list", member);
587 }
588
589 perform_member_init (member, name, init, from_init_list);
590 mem_init_list = TREE_CHAIN (mem_init_list);
591 }
592
593 /* Now initialize any members from our bases. */
594 while (mem_init_list)
595 {
596 tree name, init, field;
597
598 if (TREE_PURPOSE (mem_init_list))
599 {
600 name = TREE_PURPOSE (mem_init_list);
601 init = TREE_VALUE (mem_init_list);
602
603 if (TREE_CODE (name) == IDENTIFIER_NODE)
604 field = IDENTIFIER_CLASS_VALUE (name);
605 else
606 field = name;
607
608 /* If one member shadows another, get the outermost one. */
609 if (TREE_CODE (field) == TREE_LIST)
610 {
611 field = TREE_VALUE (field);
612 if (decl_type_context (field) != current_class_type)
613 cp_error ("field `%D' not in immediate context", field);
614 }
615
616 perform_member_init (field, name, init, 1);
617 }
618 mem_init_list = TREE_CHAIN (mem_init_list);
619 }
620
621 /* All the implicit try blocks we built up will be zapped
622 when we come to a real binding contour boundary. */
623 return finish_init_stmts (stmt_expr, compound_stmt);
624 }
625
626 /* Check that all fields are properly initialized after
627 an assignment to `this'. Called only when such an assignment
628 is actually noted. */
629
630 void
631 check_base_init (t)
632 tree t;
633 {
634 tree member;
635 for (member = TYPE_FIELDS (t); member; member = TREE_CHAIN (member))
636 if (DECL_NAME (member) && TREE_USED (member))
637 cp_error ("field `%D' used before initialized (after assignment to `this')",
638 member);
639 }
640
641 /* This code sets up the virtual function tables appropriate for
642 the pointer DECL. It is a one-ply initialization.
643
644 BINFO is the exact type that DECL is supposed to be. In
645 multiple inheritance, this might mean "C's A" if C : A, B. */
646
647 static void
648 expand_virtual_init (binfo, decl)
649 tree binfo, decl;
650 {
651 tree type = BINFO_TYPE (binfo);
652 tree vtbl, vtbl_ptr;
653 tree vtype, vtype_binfo;
654
655 /* Compute the location of the vtable. */
656 vtype = DECL_CONTEXT (TYPE_VFIELD (type));
657 vtype_binfo = get_binfo (vtype, TREE_TYPE (TREE_TYPE (decl)), 0);
658 vtbl = BINFO_VTABLE (binfo_value (DECL_FIELD_CONTEXT (TYPE_VFIELD (type)), binfo));
659
660 if (TREE_CODE (vtbl) == VAR_DECL)
661 {
662 assemble_external (vtbl);
663 TREE_USED (vtbl) = 1;
664 vtbl = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (vtbl)), vtbl);
665 }
666 else
667 /* Under the new ABI, secondary vtables are stored with the
668 primary vtable. So, the BINFO_VTABLE may be an expression for
669 computing the secondary vtable, rather than the secondary
670 vtable itself. */
671 my_friendly_assert (merge_primary_and_secondary_vtables_p (),
672 20000220);
673
674 /* Under the new ABI, we need to point into the middle of the
675 vtable. */
676 if (vbase_offsets_in_vtable_p ())
677 vtbl = build (PLUS_EXPR, TREE_TYPE (vtbl), vtbl,
678 size_extra_vtbl_entries (binfo));
679
680 /* Compute the location of the vtpr. */
681 decl = convert_pointer_to_real (vtype_binfo, decl);
682 vtbl_ptr = build_vfield_ref (build_indirect_ref (decl, NULL_PTR), vtype);
683 if (vtbl_ptr == error_mark_node)
684 return;
685
686 /* Assign the vtable to the vptr. */
687 vtbl = convert_force (TREE_TYPE (vtbl_ptr), vtbl, 0);
688 finish_expr_stmt (build_modify_expr (vtbl_ptr, NOP_EXPR, vtbl));
689 }
690
691 /* If an exception is thrown in a constructor, those base classes already
692 constructed must be destroyed. This function creates the cleanup
693 for BINFO, which has just been constructed. If FLAG is non-NULL,
694 it is a DECL which is non-zero when this base needs to be
695 destroyed. */
696
697 static void
698 expand_cleanup_for_base (binfo, flag)
699 tree binfo;
700 tree flag;
701 {
702 tree expr;
703
704 if (!TYPE_NEEDS_DESTRUCTOR (BINFO_TYPE (binfo)))
705 return;
706
707 /* Call the destructor. */
708 expr = (build_scoped_method_call
709 (current_class_ref, binfo, dtor_identifier,
710 build_tree_list (NULL_TREE, integer_zero_node)));
711 if (flag)
712 expr = fold (build (COND_EXPR, void_type_node,
713 truthvalue_conversion (flag),
714 expr, integer_zero_node));
715
716 finish_subobject (expr);
717 }
718
719 /* Subroutine of `expand_aggr_vbase_init'.
720 BINFO is the binfo of the type that is being initialized.
721 INIT_LIST is the list of initializers for the virtual baseclass. */
722
723 static void
724 expand_aggr_vbase_init_1 (binfo, exp, addr, init_list)
725 tree binfo, exp, addr, init_list;
726 {
727 tree init = purpose_member (binfo, init_list);
728 tree ref = build_indirect_ref (addr, NULL_PTR);
729
730 if (init)
731 init = TREE_VALUE (init);
732 /* Call constructors, but don't set up vtables. */
733 expand_aggr_init_1 (binfo, exp, ref, init, LOOKUP_COMPLAIN);
734 }
735
736 /* Construct the virtual base-classes of THIS_REF (whose address is
737 THIS_PTR). The object has the indicated TYPE. The construction
738 actually takes place only if FLAG is non-zero. INIT_LIST is list
739 of initializations for constructors to perform. */
740
741 static void
742 construct_virtual_bases (type, this_ref, this_ptr, init_list, flag)
743 tree type;
744 tree this_ref;
745 tree this_ptr;
746 tree init_list;
747 tree flag;
748 {
749 tree vbases;
750
751 /* If there are no virtual baseclasses, we shouldn't even be here. */
752 my_friendly_assert (TYPE_USES_VIRTUAL_BASECLASSES (type), 19990621);
753
754 /* First set the pointers in our object that tell us where to find
755 our virtual baseclasses. */
756 if (!vbase_offsets_in_vtable_p ())
757 {
758 tree if_stmt;
759 tree result;
760
761 if_stmt = begin_if_stmt ();
762 finish_if_stmt_cond (flag, if_stmt);
763 result = init_vbase_pointers (type, this_ptr);
764 if (result)
765 finish_expr_stmt (build_compound_expr (result));
766 finish_then_clause (if_stmt);
767 finish_if_stmt ();
768 }
769
770 /* Now, run through the baseclasses, initializing each. */
771 for (vbases = CLASSTYPE_VBASECLASSES (type); vbases;
772 vbases = TREE_CHAIN (vbases))
773 {
774 tree inner_if_stmt;
775 tree compound_stmt;
776 tree exp;
777
778 /* If there are virtual base classes with destructors, we need to
779 emit cleanups to destroy them if an exception is thrown during
780 the construction process. These exception regions (i.e., the
781 period during which the cleanups must occur) begin from the time
782 the construction is complete to the end of the function. If we
783 create a conditional block in which to initialize the
784 base-classes, then the cleanup region for the virtual base begins
785 inside a block, and ends outside of that block. This situation
786 confuses the sjlj exception-handling code. Therefore, we do not
787 create a single conditional block, but one for each
788 initialization. (That way the cleanup regions always begin
789 in the outer block.) We trust the back-end to figure out
790 that the FLAG will not change across initializations, and
791 avoid doing multiple tests. */
792 inner_if_stmt = begin_if_stmt ();
793 finish_if_stmt_cond (flag, inner_if_stmt);
794 compound_stmt = begin_compound_stmt (/*has_no_scope=*/1);
795
796 /* Compute the location of the virtual base. If we're
797 constructing virtual bases, then we must be the most derived
798 class. Therefore, we don't have to look up the virtual base;
799 we already know where it is. */
800 exp = build (PLUS_EXPR,
801 TREE_TYPE (this_ptr),
802 this_ptr,
803 BINFO_OFFSET (vbases));
804 exp = build1 (NOP_EXPR,
805 build_pointer_type (BINFO_TYPE (vbases)),
806 exp);
807
808 expand_aggr_vbase_init_1 (vbases, this_ref, exp, init_list);
809 finish_compound_stmt (/*has_no_scope=*/1, compound_stmt);
810 finish_then_clause (inner_if_stmt);
811 finish_if_stmt ();
812
813 expand_cleanup_for_base (vbases, flag);
814 }
815 }
816
817 /* Find the context in which this FIELD can be initialized. */
818
819 static tree
820 initializing_context (field)
821 tree field;
822 {
823 tree t = DECL_CONTEXT (field);
824
825 /* Anonymous union members can be initialized in the first enclosing
826 non-anonymous union context. */
827 while (t && ANON_AGGR_TYPE_P (t))
828 t = TYPE_CONTEXT (t);
829 return t;
830 }
831
832 /* Function to give error message if member initialization specification
833 is erroneous. FIELD is the member we decided to initialize.
834 TYPE is the type for which the initialization is being performed.
835 FIELD must be a member of TYPE.
836
837 MEMBER_NAME is the name of the member. */
838
839 static int
840 member_init_ok_or_else (field, type, member_name)
841 tree field;
842 tree type;
843 const char *member_name;
844 {
845 if (field == error_mark_node)
846 return 0;
847 if (field == NULL_TREE || initializing_context (field) != type)
848 {
849 cp_error ("class `%T' does not have any field named `%s'", type,
850 member_name);
851 return 0;
852 }
853 if (TREE_STATIC (field))
854 {
855 cp_error ("field `%#D' is static; only point of initialization is its declaration",
856 field);
857 return 0;
858 }
859
860 return 1;
861 }
862
863 /* If NAME is a viable field name for the aggregate DECL,
864 and PARMS is a viable parameter list, then expand an _EXPR
865 which describes this initialization.
866
867 Note that we do not need to chase through the class's base classes
868 to look for NAME, because if it's in that list, it will be handled
869 by the constructor for that base class.
870
871 We do not yet have a fixed-point finder to instantiate types
872 being fed to overloaded constructors. If there is a unique
873 constructor, then argument types can be got from that one.
874
875 If INIT is non-NULL, then it the initialization should
876 be placed in `current_base_init_list', where it will be processed
877 by `emit_base_init'. */
878
879 void
880 expand_member_init (exp, name, init)
881 tree exp, name, init;
882 {
883 tree basetype = NULL_TREE, field;
884 tree type;
885
886 if (exp == NULL_TREE)
887 return; /* complain about this later */
888
889 type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
890
891 if (name && TREE_CODE (name) == TYPE_DECL)
892 {
893 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (name));
894 name = DECL_NAME (name);
895 }
896
897 if (name == NULL_TREE && IS_AGGR_TYPE (type))
898 switch (CLASSTYPE_N_BASECLASSES (type))
899 {
900 case 0:
901 error ("base class initializer specified, but no base class to initialize");
902 return;
903 case 1:
904 basetype = TYPE_BINFO_BASETYPE (type, 0);
905 break;
906 default:
907 error ("initializer for unnamed base class ambiguous");
908 cp_error ("(type `%T' uses multiple inheritance)", type);
909 return;
910 }
911
912 my_friendly_assert (init != NULL_TREE, 0);
913
914 /* The grammar should not allow fields which have names that are
915 TYPENAMEs. Therefore, if the field has a non-NULL TREE_TYPE, we
916 may assume that this is an attempt to initialize a base class
917 member of the current type. Otherwise, it is an attempt to
918 initialize a member field. */
919
920 if (init == void_type_node)
921 init = NULL_TREE;
922
923 if (name == NULL_TREE || basetype)
924 {
925 tree base_init;
926
927 if (name == NULL_TREE)
928 {
929 #if 0
930 if (basetype)
931 name = TYPE_IDENTIFIER (basetype);
932 else
933 {
934 error ("no base class to initialize");
935 return;
936 }
937 #endif
938 }
939 else if (basetype != type
940 && ! current_template_parms
941 && ! vec_binfo_member (basetype,
942 TYPE_BINFO_BASETYPES (type))
943 && ! BINFO_FOR_VBASE (basetype, type))
944 {
945 if (IDENTIFIER_CLASS_VALUE (name))
946 goto try_member;
947 if (TYPE_USES_VIRTUAL_BASECLASSES (type))
948 cp_error ("type `%T' is not an immediate or virtual basetype for `%T'",
949 basetype, type);
950 else
951 cp_error ("type `%T' is not an immediate basetype for `%T'",
952 basetype, type);
953 return;
954 }
955
956 if (purpose_member (basetype, current_base_init_list))
957 {
958 cp_error ("base class `%T' already initialized", basetype);
959 return;
960 }
961
962 if (warn_reorder && current_member_init_list)
963 {
964 cp_warning ("base initializer for `%T'", basetype);
965 warning (" will be re-ordered to precede member initializations");
966 }
967
968 base_init = build_tree_list (basetype, init);
969 current_base_init_list = chainon (current_base_init_list, base_init);
970 }
971 else
972 {
973 tree member_init;
974
975 try_member:
976 field = lookup_field (type, name, 1, 0);
977
978 if (! member_init_ok_or_else (field, type, IDENTIFIER_POINTER (name)))
979 return;
980
981 if (purpose_member (name, current_member_init_list))
982 {
983 cp_error ("field `%D' already initialized", field);
984 return;
985 }
986
987 member_init = build_tree_list (name, init);
988 current_member_init_list = chainon (current_member_init_list, member_init);
989 }
990 }
991
992 /* We are about to generate some complex initialization code.
993 Conceptually, it is all a single expression. However, we may want
994 to include conditionals, loops, and other such statement-level
995 constructs. Therefore, we build the initialization code inside a
996 statement-expression. This function starts such an expression.
997 STMT_EXPR_P and COMPOUND_STMT_P are filled in by this function;
998 pass them back to finish_init_stmts when the expression is
999 complete. */
1000
1001 void
1002 begin_init_stmts (stmt_expr_p, compound_stmt_p)
1003 tree *stmt_expr_p;
1004 tree *compound_stmt_p;
1005 {
1006 *stmt_expr_p = begin_stmt_expr ();
1007 *compound_stmt_p = begin_compound_stmt (/*has_no_scope=*/1);
1008 }
1009
1010 /* Finish out the statement-expression begun by the previous call to
1011 begin_init_stmts. Returns the statement-expression itself. */
1012
1013 tree
1014 finish_init_stmts (stmt_expr, compound_stmt)
1015 tree stmt_expr;
1016 tree compound_stmt;
1017 {
1018 finish_compound_stmt (/*has_no_scope=*/1, compound_stmt);
1019 stmt_expr = finish_stmt_expr (stmt_expr);
1020
1021 /* To avoid spurious warnings about unused values, we set
1022 TREE_USED. */
1023 if (stmt_expr)
1024 TREE_USED (stmt_expr) = 1;
1025
1026 return stmt_expr;
1027 }
1028
1029 /* This is like `expand_member_init', only it stores one aggregate
1030 value into another.
1031
1032 INIT comes in two flavors: it is either a value which
1033 is to be stored in EXP, or it is a parameter list
1034 to go to a constructor, which will operate on EXP.
1035 If INIT is not a parameter list for a constructor, then set
1036 LOOKUP_ONLYCONVERTING.
1037 If FLAGS is LOOKUP_ONLYCONVERTING then it is the = init form of
1038 the initializer, if FLAGS is 0, then it is the (init) form.
1039 If `init' is a CONSTRUCTOR, then we emit a warning message,
1040 explaining that such initializations are invalid.
1041
1042 If INIT resolves to a CALL_EXPR which happens to return
1043 something of the type we are looking for, then we know
1044 that we can safely use that call to perform the
1045 initialization.
1046
1047 The virtual function table pointer cannot be set up here, because
1048 we do not really know its type.
1049
1050 Virtual baseclass pointers are also set up here.
1051
1052 This never calls operator=().
1053
1054 When initializing, nothing is CONST.
1055
1056 A default copy constructor may have to be used to perform the
1057 initialization.
1058
1059 A constructor or a conversion operator may have to be used to
1060 perform the initialization, but not both, as it would be ambiguous. */
1061
1062 tree
1063 build_aggr_init (exp, init, flags)
1064 tree exp, init;
1065 int flags;
1066 {
1067 tree stmt_expr;
1068 tree compound_stmt;
1069 int destroy_temps;
1070 tree type = TREE_TYPE (exp);
1071 int was_const = TREE_READONLY (exp);
1072 int was_volatile = TREE_THIS_VOLATILE (exp);
1073
1074 if (init == error_mark_node)
1075 return error_mark_node;
1076
1077 TREE_READONLY (exp) = 0;
1078 TREE_THIS_VOLATILE (exp) = 0;
1079
1080 if (init && TREE_CODE (init) != TREE_LIST)
1081 flags |= LOOKUP_ONLYCONVERTING;
1082
1083 if (TREE_CODE (type) == ARRAY_TYPE)
1084 {
1085 /* Must arrange to initialize each element of EXP
1086 from elements of INIT. */
1087 tree itype = init ? TREE_TYPE (init) : NULL_TREE;
1088 if (CP_TYPE_QUALS (type) != TYPE_UNQUALIFIED)
1089 {
1090 TREE_TYPE (exp) = TYPE_MAIN_VARIANT (type);
1091 if (init)
1092 TREE_TYPE (init) = TYPE_MAIN_VARIANT (itype);
1093 }
1094 if (init && TREE_TYPE (init) == NULL_TREE)
1095 {
1096 /* Handle bad initializers like:
1097 class COMPLEX {
1098 public:
1099 double re, im;
1100 COMPLEX(double r = 0.0, double i = 0.0) {re = r; im = i;};
1101 ~COMPLEX() {};
1102 };
1103
1104 int main(int argc, char **argv) {
1105 COMPLEX zees(1.0, 0.0)[10];
1106 }
1107 */
1108 error ("bad array initializer");
1109 return error_mark_node;
1110 }
1111 stmt_expr = build_vec_init (exp, exp, array_type_nelts (type), init,
1112 init && same_type_p (TREE_TYPE (init),
1113 TREE_TYPE (exp)));
1114 TREE_READONLY (exp) = was_const;
1115 TREE_THIS_VOLATILE (exp) = was_volatile;
1116 TREE_TYPE (exp) = type;
1117 if (init)
1118 TREE_TYPE (init) = itype;
1119 return stmt_expr;
1120 }
1121
1122 if (TREE_CODE (exp) == VAR_DECL || TREE_CODE (exp) == PARM_DECL)
1123 /* just know that we've seen something for this node */
1124 TREE_USED (exp) = 1;
1125
1126 TREE_TYPE (exp) = TYPE_MAIN_VARIANT (type);
1127 begin_init_stmts (&stmt_expr, &compound_stmt);
1128 destroy_temps = stmts_are_full_exprs_p;
1129 stmts_are_full_exprs_p = 0;
1130 expand_aggr_init_1 (TYPE_BINFO (type), exp, exp,
1131 init, LOOKUP_NORMAL|flags);
1132 stmt_expr = finish_init_stmts (stmt_expr, compound_stmt);
1133 stmts_are_full_exprs_p = destroy_temps;
1134 TREE_TYPE (exp) = type;
1135 TREE_READONLY (exp) = was_const;
1136 TREE_THIS_VOLATILE (exp) = was_volatile;
1137
1138 return stmt_expr;
1139 }
1140
1141 static void
1142 expand_default_init (binfo, true_exp, exp, init, flags)
1143 tree binfo;
1144 tree true_exp, exp;
1145 tree init;
1146 int flags;
1147 {
1148 tree type = TREE_TYPE (exp);
1149
1150 /* It fails because there may not be a constructor which takes
1151 its own type as the first (or only parameter), but which does
1152 take other types via a conversion. So, if the thing initializing
1153 the expression is a unit element of type X, first try X(X&),
1154 followed by initialization by X. If neither of these work
1155 out, then look hard. */
1156 tree rval;
1157 tree parms;
1158
1159 if (init && TREE_CODE (init) != TREE_LIST
1160 && (flags & LOOKUP_ONLYCONVERTING))
1161 {
1162 /* Base subobjects should only get direct-initialization. */
1163 if (true_exp != exp)
1164 abort ();
1165
1166 if (flags & DIRECT_BIND)
1167 /* Do nothing. We hit this in two cases: Reference initialization,
1168 where we aren't initializing a real variable, so we don't want
1169 to run a new constructor; and catching an exception, where we
1170 have already built up the constructor call so we could wrap it
1171 in an exception region. */;
1172 else if (TREE_CODE (init) == CONSTRUCTOR)
1173 /* A brace-enclosed initializer has whatever type is
1174 required. There's no need to convert it. */
1175 ;
1176 else
1177 init = ocp_convert (type, init, CONV_IMPLICIT|CONV_FORCE_TEMP, flags);
1178
1179 if (TREE_CODE (init) == TRY_CATCH_EXPR)
1180 /* We need to protect the initialization of a catch parm
1181 with a call to terminate(), which shows up as a TRY_CATCH_EXPR
1182 around the TARGET_EXPR for the copy constructor. See
1183 expand_start_catch_block. */
1184 TREE_OPERAND (init, 0) = build (INIT_EXPR, TREE_TYPE (exp), exp,
1185 TREE_OPERAND (init, 0));
1186 else
1187 init = build (INIT_EXPR, TREE_TYPE (exp), exp, init);
1188 TREE_SIDE_EFFECTS (init) = 1;
1189 finish_expr_stmt (init);
1190 return;
1191 }
1192
1193 if (init == NULL_TREE
1194 || (TREE_CODE (init) == TREE_LIST && ! TREE_TYPE (init)))
1195 {
1196 parms = init;
1197 if (parms)
1198 init = TREE_VALUE (parms);
1199 }
1200 else
1201 parms = build_tree_list (NULL_TREE, init);
1202
1203 if (TYPE_USES_VIRTUAL_BASECLASSES (type))
1204 {
1205 if (true_exp == exp)
1206 parms = tree_cons (NULL_TREE, integer_one_node, parms);
1207 else
1208 parms = tree_cons (NULL_TREE, integer_zero_node, parms);
1209 flags |= LOOKUP_HAS_IN_CHARGE;
1210 }
1211
1212 rval = build_method_call (exp, ctor_identifier,
1213 parms, binfo, flags);
1214 if (TREE_SIDE_EFFECTS (rval))
1215 finish_expr_stmt (rval);
1216 }
1217
1218 /* This function is responsible for initializing EXP with INIT
1219 (if any).
1220
1221 BINFO is the binfo of the type for who we are performing the
1222 initialization. For example, if W is a virtual base class of A and B,
1223 and C : A, B.
1224 If we are initializing B, then W must contain B's W vtable, whereas
1225 were we initializing C, W must contain C's W vtable.
1226
1227 TRUE_EXP is nonzero if it is the true expression being initialized.
1228 In this case, it may be EXP, or may just contain EXP. The reason we
1229 need this is because if EXP is a base element of TRUE_EXP, we
1230 don't necessarily know by looking at EXP where its virtual
1231 baseclass fields should really be pointing. But we do know
1232 from TRUE_EXP. In constructors, we don't know anything about
1233 the value being initialized.
1234
1235 FLAGS is just passes to `build_method_call'. See that function for
1236 its description. */
1237
1238 static void
1239 expand_aggr_init_1 (binfo, true_exp, exp, init, flags)
1240 tree binfo;
1241 tree true_exp, exp;
1242 tree init;
1243 int flags;
1244 {
1245 tree type = TREE_TYPE (exp);
1246
1247 my_friendly_assert (init != error_mark_node && type != error_mark_node, 211);
1248
1249 /* Use a function returning the desired type to initialize EXP for us.
1250 If the function is a constructor, and its first argument is
1251 NULL_TREE, know that it was meant for us--just slide exp on
1252 in and expand the constructor. Constructors now come
1253 as TARGET_EXPRs. */
1254
1255 if (init && TREE_CODE (exp) == VAR_DECL
1256 && TREE_CODE (init) == CONSTRUCTOR
1257 && TREE_HAS_CONSTRUCTOR (init))
1258 {
1259 /* If store_init_value returns NULL_TREE, the INIT has been
1260 record in the DECL_INITIAL for EXP. That means there's
1261 nothing more we have to do. */
1262 if (!store_init_value (exp, init))
1263 {
1264 if (!building_stmt_tree ())
1265 expand_decl_init (exp);
1266 }
1267 else
1268 finish_expr_stmt (build (INIT_EXPR, type, exp, init));
1269 return;
1270 }
1271
1272 /* We know that expand_default_init can handle everything we want
1273 at this point. */
1274 expand_default_init (binfo, true_exp, exp, init, flags);
1275 }
1276
1277 /* Report an error if NAME is not the name of a user-defined,
1278 aggregate type. If OR_ELSE is nonzero, give an error message. */
1279
1280 int
1281 is_aggr_typedef (name, or_else)
1282 tree name;
1283 int or_else;
1284 {
1285 tree type;
1286
1287 if (name == error_mark_node)
1288 return 0;
1289
1290 if (IDENTIFIER_HAS_TYPE_VALUE (name))
1291 type = IDENTIFIER_TYPE_VALUE (name);
1292 else
1293 {
1294 if (or_else)
1295 cp_error ("`%T' is not an aggregate typedef", name);
1296 return 0;
1297 }
1298
1299 if (! IS_AGGR_TYPE (type)
1300 && TREE_CODE (type) != TEMPLATE_TYPE_PARM
1301 && TREE_CODE (type) != TEMPLATE_TEMPLATE_PARM)
1302 {
1303 if (or_else)
1304 cp_error ("`%T' is not an aggregate type", type);
1305 return 0;
1306 }
1307 return 1;
1308 }
1309
1310 /* Report an error if TYPE is not a user-defined, aggregate type. If
1311 OR_ELSE is nonzero, give an error message. */
1312
1313 int
1314 is_aggr_type (type, or_else)
1315 tree type;
1316 int or_else;
1317 {
1318 if (type == error_mark_node)
1319 return 0;
1320
1321 if (! IS_AGGR_TYPE (type)
1322 && TREE_CODE (type) != TEMPLATE_TYPE_PARM
1323 && TREE_CODE (type) != TEMPLATE_TEMPLATE_PARM)
1324 {
1325 if (or_else)
1326 cp_error ("`%T' is not an aggregate type", type);
1327 return 0;
1328 }
1329 return 1;
1330 }
1331
1332 /* Like is_aggr_typedef, but returns typedef if successful. */
1333
1334 tree
1335 get_aggr_from_typedef (name, or_else)
1336 tree name;
1337 int or_else;
1338 {
1339 tree type;
1340
1341 if (name == error_mark_node)
1342 return NULL_TREE;
1343
1344 if (IDENTIFIER_HAS_TYPE_VALUE (name))
1345 type = IDENTIFIER_TYPE_VALUE (name);
1346 else
1347 {
1348 if (or_else)
1349 cp_error ("`%T' fails to be an aggregate typedef", name);
1350 return NULL_TREE;
1351 }
1352
1353 if (! IS_AGGR_TYPE (type)
1354 && TREE_CODE (type) != TEMPLATE_TYPE_PARM
1355 && TREE_CODE (type) != TEMPLATE_TEMPLATE_PARM)
1356 {
1357 if (or_else)
1358 cp_error ("type `%T' is of non-aggregate type", type);
1359 return NULL_TREE;
1360 }
1361 return type;
1362 }
1363
1364 tree
1365 get_type_value (name)
1366 tree name;
1367 {
1368 if (name == error_mark_node)
1369 return NULL_TREE;
1370
1371 if (IDENTIFIER_HAS_TYPE_VALUE (name))
1372 return IDENTIFIER_TYPE_VALUE (name);
1373 else
1374 return NULL_TREE;
1375 }
1376
1377 \f
1378 /* This code could just as well go in `class.c', but is placed here for
1379 modularity. */
1380
1381 /* For an expression of the form TYPE :: NAME (PARMLIST), build
1382 the appropriate function call. */
1383
1384 tree
1385 build_member_call (type, name, parmlist)
1386 tree type, name, parmlist;
1387 {
1388 tree t;
1389 tree method_name;
1390 int dtor = 0;
1391 tree basetype_path, decl;
1392
1393 if (TREE_CODE (name) == TEMPLATE_ID_EXPR
1394 && TREE_CODE (type) == NAMESPACE_DECL)
1395 {
1396 /* 'name' already refers to the decls from the namespace, since we
1397 hit do_identifier for template_ids. */
1398 method_name = TREE_OPERAND (name, 0);
1399 /* FIXME: Since we don't do independent names right yet, the
1400 name might also be a LOOKUP_EXPR. Once we resolve this to a
1401 real decl earlier, this can go. This may happen during
1402 tsubst'ing. */
1403 if (TREE_CODE (method_name) == LOOKUP_EXPR)
1404 {
1405 method_name = lookup_namespace_name
1406 (type, TREE_OPERAND (method_name, 0));
1407 TREE_OPERAND (name, 0) = method_name;
1408 }
1409 my_friendly_assert (is_overloaded_fn (method_name), 980519);
1410 return build_x_function_call (name, parmlist, current_class_ref);
1411 }
1412
1413 if (type == std_node)
1414 return build_x_function_call (do_scoped_id (name, 0), parmlist,
1415 current_class_ref);
1416 if (TREE_CODE (type) == NAMESPACE_DECL)
1417 return build_x_function_call (lookup_namespace_name (type, name),
1418 parmlist, current_class_ref);
1419
1420 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
1421 {
1422 method_name = TREE_OPERAND (name, 0);
1423 if (TREE_CODE (method_name) == COMPONENT_REF)
1424 method_name = TREE_OPERAND (method_name, 1);
1425 if (is_overloaded_fn (method_name))
1426 method_name = DECL_NAME (OVL_CURRENT (method_name));
1427 TREE_OPERAND (name, 0) = method_name;
1428 }
1429 else
1430 method_name = name;
1431
1432 if (TREE_CODE (method_name) == BIT_NOT_EXPR)
1433 {
1434 method_name = TREE_OPERAND (method_name, 0);
1435 dtor = 1;
1436 }
1437
1438 /* This shouldn't be here, and build_member_call shouldn't appear in
1439 parse.y! (mrs) */
1440 if (type && TREE_CODE (type) == IDENTIFIER_NODE
1441 && get_aggr_from_typedef (type, 0) == 0)
1442 {
1443 tree ns = lookup_name (type, 0);
1444 if (ns && TREE_CODE (ns) == NAMESPACE_DECL)
1445 {
1446 return build_x_function_call (build_offset_ref (type, name), parmlist, current_class_ref);
1447 }
1448 }
1449
1450 if (type == NULL_TREE || ! is_aggr_type (type, 1))
1451 return error_mark_node;
1452
1453 /* An operator we did not like. */
1454 if (name == NULL_TREE)
1455 return error_mark_node;
1456
1457 if (dtor)
1458 {
1459 cp_error ("cannot call destructor `%T::~%T' without object", type,
1460 method_name);
1461 return error_mark_node;
1462 }
1463
1464 decl = maybe_dummy_object (type, &basetype_path);
1465
1466 /* Convert 'this' to the specified type to disambiguate conversion
1467 to the function's context. Apparently Standard C++ says that we
1468 shouldn't do this. */
1469 if (decl == current_class_ref
1470 && ! pedantic
1471 && ACCESSIBLY_UNIQUELY_DERIVED_P (type, current_class_type))
1472 {
1473 tree olddecl = current_class_ptr;
1474 tree oldtype = TREE_TYPE (TREE_TYPE (olddecl));
1475 if (oldtype != type)
1476 {
1477 tree newtype = build_qualified_type (type, TYPE_QUALS (oldtype));
1478 decl = convert_force (build_pointer_type (newtype), olddecl, 0);
1479 decl = build_indirect_ref (decl, NULL_PTR);
1480 }
1481 }
1482
1483 if (method_name == constructor_name (type)
1484 || method_name == constructor_name_full (type))
1485 return build_functional_cast (type, parmlist);
1486 if (lookup_fnfields (basetype_path, method_name, 0))
1487 return build_method_call (decl,
1488 TREE_CODE (name) == TEMPLATE_ID_EXPR
1489 ? name : method_name,
1490 parmlist, basetype_path,
1491 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL);
1492 if (TREE_CODE (name) == IDENTIFIER_NODE
1493 && ((t = lookup_field (TYPE_BINFO (type), name, 1, 0))))
1494 {
1495 if (t == error_mark_node)
1496 return error_mark_node;
1497 if (TREE_CODE (t) == FIELD_DECL)
1498 {
1499 if (is_dummy_object (decl))
1500 {
1501 cp_error ("invalid use of non-static field `%D'", t);
1502 return error_mark_node;
1503 }
1504 decl = build (COMPONENT_REF, TREE_TYPE (t), decl, t);
1505 }
1506 else if (TREE_CODE (t) == VAR_DECL)
1507 decl = t;
1508 else
1509 {
1510 cp_error ("invalid use of member `%D'", t);
1511 return error_mark_node;
1512 }
1513 if (TYPE_LANG_SPECIFIC (TREE_TYPE (decl)))
1514 return build_opfncall (CALL_EXPR, LOOKUP_NORMAL, decl,
1515 parmlist, NULL_TREE);
1516 return build_function_call (decl, parmlist);
1517 }
1518 else
1519 {
1520 cp_error ("no method `%T::%D'", type, name);
1521 return error_mark_node;
1522 }
1523 }
1524
1525 /* Build a reference to a member of an aggregate. This is not a
1526 C++ `&', but really something which can have its address taken,
1527 and then act as a pointer to member, for example TYPE :: FIELD
1528 can have its address taken by saying & TYPE :: FIELD.
1529
1530 @@ Prints out lousy diagnostics for operator <typename>
1531 @@ fields.
1532
1533 @@ This function should be rewritten and placed in search.c. */
1534
1535 tree
1536 build_offset_ref (type, name)
1537 tree type, name;
1538 {
1539 tree decl, t = error_mark_node;
1540 tree member;
1541 tree basebinfo = NULL_TREE;
1542 tree orig_name = name;
1543
1544 /* class templates can come in as TEMPLATE_DECLs here. */
1545 if (TREE_CODE (name) == TEMPLATE_DECL)
1546 return name;
1547
1548 if (type == std_node)
1549 return do_scoped_id (name, 0);
1550
1551 if (processing_template_decl || uses_template_parms (type))
1552 return build_min_nt (SCOPE_REF, type, name);
1553
1554 /* Handle namespace names fully here. */
1555 if (TREE_CODE (type) == NAMESPACE_DECL)
1556 {
1557 t = lookup_namespace_name (type, name);
1558 if (t != error_mark_node && ! type_unknown_p (t))
1559 {
1560 mark_used (t);
1561 t = convert_from_reference (t);
1562 }
1563 return t;
1564 }
1565
1566 if (type == NULL_TREE || ! is_aggr_type (type, 1))
1567 return error_mark_node;
1568
1569 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
1570 {
1571 /* If the NAME is a TEMPLATE_ID_EXPR, we are looking at
1572 something like `a.template f<int>' or the like. For the most
1573 part, we treat this just like a.f. We do remember, however,
1574 the template-id that was used. */
1575 name = TREE_OPERAND (orig_name, 0);
1576
1577 if (TREE_CODE (name) == LOOKUP_EXPR)
1578 /* This can happen during tsubst'ing. */
1579 name = TREE_OPERAND (name, 0);
1580
1581 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 0);
1582 }
1583
1584 if (TREE_CODE (name) == BIT_NOT_EXPR)
1585 {
1586 if (! check_dtor_name (type, name))
1587 cp_error ("qualified type `%T' does not match destructor name `~%T'",
1588 type, TREE_OPERAND (name, 0));
1589 name = dtor_identifier;
1590 }
1591 #if 0
1592 /* I think this is wrong, but the draft is unclear. --jason 6/15/98 */
1593 else if (name == constructor_name_full (type)
1594 || name == constructor_name (type))
1595 name = ctor_identifier;
1596 #endif
1597
1598 if (TYPE_SIZE (complete_type (type)) == 0
1599 && !TYPE_BEING_DEFINED (type))
1600 {
1601 cp_error ("incomplete type `%T' does not have member `%D'", type,
1602 name);
1603 return error_mark_node;
1604 }
1605
1606 decl = maybe_dummy_object (type, &basebinfo);
1607
1608 member = lookup_member (basebinfo, name, 1, 0);
1609
1610 if (member == error_mark_node)
1611 return error_mark_node;
1612
1613 /* A lot of this logic is now handled in lookup_field and
1614 lookup_fnfield. */
1615 if (member && BASELINK_P (member))
1616 {
1617 /* Go from the TREE_BASELINK to the member function info. */
1618 tree fnfields = member;
1619 t = TREE_VALUE (fnfields);
1620
1621 if (TREE_CODE (orig_name) == TEMPLATE_ID_EXPR)
1622 {
1623 /* The FNFIELDS are going to contain functions that aren't
1624 necessarily templates, and templates that don't
1625 necessarily match the explicit template parameters. We
1626 save all the functions, and the explicit parameters, and
1627 then figure out exactly what to instantiate with what
1628 arguments in instantiate_type. */
1629
1630 if (TREE_CODE (t) != OVERLOAD)
1631 /* The code in instantiate_type which will process this
1632 expects to encounter OVERLOADs, not raw functions. */
1633 t = ovl_cons (t, NULL_TREE);
1634
1635 return build (OFFSET_REF,
1636 unknown_type_node,
1637 decl,
1638 build (TEMPLATE_ID_EXPR,
1639 TREE_TYPE (t),
1640 t,
1641 TREE_OPERAND (orig_name, 1)));
1642 }
1643
1644 if (!really_overloaded_fn (t))
1645 {
1646 /* Get rid of a potential OVERLOAD around it */
1647 t = OVL_CURRENT (t);
1648
1649 /* unique functions are handled easily. */
1650 basebinfo = TREE_PURPOSE (fnfields);
1651 if (!enforce_access (basebinfo, t))
1652 return error_mark_node;
1653 mark_used (t);
1654 if (DECL_STATIC_FUNCTION_P (t))
1655 return t;
1656 return build (OFFSET_REF, TREE_TYPE (t), decl, t);
1657 }
1658
1659 TREE_TYPE (fnfields) = unknown_type_node;
1660 return build (OFFSET_REF, unknown_type_node, decl, fnfields);
1661 }
1662
1663 t = member;
1664
1665 if (t == NULL_TREE)
1666 {
1667 cp_error ("`%D' is not a member of type `%T'", name, type);
1668 return error_mark_node;
1669 }
1670
1671 if (TREE_CODE (t) == TYPE_DECL)
1672 {
1673 TREE_USED (t) = 1;
1674 return t;
1675 }
1676 /* static class members and class-specific enum
1677 values can be returned without further ado. */
1678 if (TREE_CODE (t) == VAR_DECL || TREE_CODE (t) == CONST_DECL)
1679 {
1680 mark_used (t);
1681 return convert_from_reference (t);
1682 }
1683
1684 if (TREE_CODE (t) == FIELD_DECL && DECL_C_BIT_FIELD (t))
1685 {
1686 cp_error ("illegal pointer to bit field `%D'", t);
1687 return error_mark_node;
1688 }
1689
1690 /* static class functions too. */
1691 if (TREE_CODE (t) == FUNCTION_DECL
1692 && TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE)
1693 my_friendly_abort (53);
1694
1695 /* In member functions, the form `type::name' is no longer
1696 equivalent to `this->type::name', at least not until
1697 resolve_offset_ref. */
1698 return build (OFFSET_REF, build_offset_type (type, TREE_TYPE (t)), decl, t);
1699 }
1700
1701 /* If a OFFSET_REF made it through to here, then it did
1702 not have its address taken. */
1703
1704 tree
1705 resolve_offset_ref (exp)
1706 tree exp;
1707 {
1708 tree type = TREE_TYPE (exp);
1709 tree base = NULL_TREE;
1710 tree member;
1711 tree basetype, addr;
1712
1713 if (TREE_CODE (exp) == OFFSET_REF)
1714 {
1715 member = TREE_OPERAND (exp, 1);
1716 base = TREE_OPERAND (exp, 0);
1717 }
1718 else
1719 {
1720 my_friendly_assert (TREE_CODE (type) == OFFSET_TYPE, 214);
1721 if (TYPE_OFFSET_BASETYPE (type) != current_class_type)
1722 {
1723 error ("object missing in use of pointer-to-member construct");
1724 return error_mark_node;
1725 }
1726 member = exp;
1727 type = TREE_TYPE (type);
1728 base = current_class_ref;
1729 }
1730
1731 if (BASELINK_P (member))
1732 {
1733 if (! flag_ms_extensions)
1734 cp_pedwarn ("assuming & on overloaded member function");
1735 return build_unary_op (ADDR_EXPR, exp, 0);
1736 }
1737
1738 if (TREE_CODE (TREE_TYPE (member)) == METHOD_TYPE)
1739 {
1740 if (! flag_ms_extensions)
1741 cp_pedwarn ("assuming & on `%E'", member);
1742 return build_unary_op (ADDR_EXPR, exp, 0);
1743 }
1744
1745 if ((TREE_CODE (member) == VAR_DECL
1746 && ! TYPE_PTRMEMFUNC_P (TREE_TYPE (member))
1747 && ! TYPE_PTRMEM_P (TREE_TYPE (member)))
1748 || TREE_CODE (TREE_TYPE (member)) == FUNCTION_TYPE)
1749 {
1750 /* These were static members. */
1751 if (mark_addressable (member) == 0)
1752 return error_mark_node;
1753 return member;
1754 }
1755
1756 if (TREE_CODE (TREE_TYPE (member)) == POINTER_TYPE
1757 && TREE_CODE (TREE_TYPE (TREE_TYPE (member))) == METHOD_TYPE)
1758 return member;
1759
1760 /* Syntax error can cause a member which should
1761 have been seen as static to be grok'd as non-static. */
1762 if (TREE_CODE (member) == FIELD_DECL && current_class_ref == NULL_TREE)
1763 {
1764 if (TREE_ADDRESSABLE (member) == 0)
1765 {
1766 cp_error_at ("member `%D' is non-static but referenced as a static member",
1767 member);
1768 error ("at this point in file");
1769 TREE_ADDRESSABLE (member) = 1;
1770 }
1771 return error_mark_node;
1772 }
1773
1774 /* The first case is really just a reference to a member of `this'. */
1775 if (TREE_CODE (member) == FIELD_DECL
1776 && (base == current_class_ref || is_dummy_object (base)))
1777 {
1778 tree basetype_path;
1779 tree expr;
1780
1781 if (TREE_CODE (exp) == OFFSET_REF && TREE_CODE (type) == OFFSET_TYPE)
1782 basetype = TYPE_OFFSET_BASETYPE (type);
1783 else
1784 basetype = DECL_CONTEXT (member);
1785
1786 base = current_class_ptr;
1787
1788 if (get_base_distance (basetype, TREE_TYPE (TREE_TYPE (base)), 0, &basetype_path) < 0)
1789 {
1790 error_not_base_type (basetype, TREE_TYPE (TREE_TYPE (base)));
1791 return error_mark_node;
1792 }
1793 /* Kludge: we need to use basetype_path now, because
1794 convert_pointer_to will bash it. */
1795 enforce_access (basetype_path, member);
1796 addr = convert_pointer_to (basetype, base);
1797
1798 /* Even in the case of illegal access, we form the
1799 COMPONENT_REF; that will allow better error recovery than
1800 just feeding back error_mark_node. */
1801 expr = build (COMPONENT_REF, TREE_TYPE (member),
1802 build_indirect_ref (addr, NULL_PTR), member);
1803 return convert_from_reference (expr);
1804 }
1805
1806 /* Ensure that we have an object. */
1807 if (is_dummy_object (base))
1808 addr = error_mark_node;
1809 else
1810 /* If this is a reference to a member function, then return the
1811 address of the member function (which may involve going
1812 through the object's vtable), otherwise, return an expression
1813 for the dereferenced pointer-to-member construct. */
1814 addr = build_unary_op (ADDR_EXPR, base, 0);
1815
1816 if (TYPE_PTRMEM_P (TREE_TYPE (member)))
1817 {
1818 if (addr == error_mark_node)
1819 {
1820 cp_error ("object missing in `%E'", exp);
1821 return error_mark_node;
1822 }
1823
1824 basetype = TYPE_OFFSET_BASETYPE (TREE_TYPE (TREE_TYPE (member)));
1825 addr = convert_pointer_to (basetype, addr);
1826 member = cp_convert (ptrdiff_type_node, member);
1827
1828 /* Pointer to data members are offset by one, so that a null
1829 pointer with a real value of 0 is distinguishable from an
1830 offset of the first member of a structure. */
1831 member = build_binary_op (MINUS_EXPR, member,
1832 cp_convert (ptrdiff_type_node, integer_one_node));
1833
1834 return build1 (INDIRECT_REF, type,
1835 build (PLUS_EXPR, build_pointer_type (type),
1836 addr, member));
1837 }
1838 else if (TYPE_PTRMEMFUNC_P (TREE_TYPE (member)))
1839 {
1840 return get_member_function_from_ptrfunc (&addr, member);
1841 }
1842 my_friendly_abort (56);
1843 /* NOTREACHED */
1844 return NULL_TREE;
1845 }
1846
1847 /* Return either DECL or its known constant value (if it has one). */
1848
1849 tree
1850 decl_constant_value (decl)
1851 tree decl;
1852 {
1853 if (! TREE_THIS_VOLATILE (decl)
1854 && DECL_INITIAL (decl)
1855 && DECL_INITIAL (decl) != error_mark_node
1856 /* This is invalid if initial value is not constant.
1857 If it has either a function call, a memory reference,
1858 or a variable, then re-evaluating it could give different results. */
1859 && TREE_CONSTANT (DECL_INITIAL (decl))
1860 /* Check for cases where this is sub-optimal, even though valid. */
1861 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
1862 return DECL_INITIAL (decl);
1863 return decl;
1864 }
1865 \f
1866 /* Common subroutines of build_new and build_vec_delete. */
1867
1868 /* Call the global __builtin_delete to delete ADDR. */
1869
1870 static tree
1871 build_builtin_delete_call (addr)
1872 tree addr;
1873 {
1874 mark_used (global_delete_fndecl);
1875 return build_call (global_delete_fndecl,
1876 void_type_node, build_tree_list (NULL_TREE, addr));
1877 }
1878 \f
1879 /* Generate a C++ "new" expression. DECL is either a TREE_LIST
1880 (which needs to go through some sort of groktypename) or it
1881 is the name of the class we are newing. INIT is an initialization value.
1882 It is either an EXPRLIST, an EXPR_NO_COMMAS, or something in braces.
1883 If INIT is void_type_node, it means do *not* call a constructor
1884 for this instance.
1885
1886 For types with constructors, the data returned is initialized
1887 by the appropriate constructor.
1888
1889 Whether the type has a constructor or not, if it has a pointer
1890 to a virtual function table, then that pointer is set up
1891 here.
1892
1893 Unless I am mistaken, a call to new () will return initialized
1894 data regardless of whether the constructor itself is private or
1895 not. NOPE; new fails if the constructor is private (jcm).
1896
1897 Note that build_new does nothing to assure that any special
1898 alignment requirements of the type are met. Rather, it leaves
1899 it up to malloc to do the right thing. Otherwise, folding to
1900 the right alignment cal cause problems if the user tries to later
1901 free the memory returned by `new'.
1902
1903 PLACEMENT is the `placement' list for user-defined operator new (). */
1904
1905 extern int flag_check_new;
1906
1907 tree
1908 build_new (placement, decl, init, use_global_new)
1909 tree placement;
1910 tree decl, init;
1911 int use_global_new;
1912 {
1913 tree type, rval;
1914 tree nelts = NULL_TREE, t;
1915 int has_array = 0;
1916
1917 if (decl == error_mark_node)
1918 return error_mark_node;
1919
1920 if (TREE_CODE (decl) == TREE_LIST)
1921 {
1922 tree absdcl = TREE_VALUE (decl);
1923 tree last_absdcl = NULL_TREE;
1924
1925 if (current_function_decl
1926 && DECL_CONSTRUCTOR_P (current_function_decl))
1927 my_friendly_assert (immediate_size_expand == 0, 19990926);
1928
1929 nelts = integer_one_node;
1930
1931 if (absdcl && TREE_CODE (absdcl) == CALL_EXPR)
1932 my_friendly_abort (215);
1933 while (absdcl && TREE_CODE (absdcl) == INDIRECT_REF)
1934 {
1935 last_absdcl = absdcl;
1936 absdcl = TREE_OPERAND (absdcl, 0);
1937 }
1938
1939 if (absdcl && TREE_CODE (absdcl) == ARRAY_REF)
1940 {
1941 /* probably meant to be a vec new */
1942 tree this_nelts;
1943
1944 while (TREE_OPERAND (absdcl, 0)
1945 && TREE_CODE (TREE_OPERAND (absdcl, 0)) == ARRAY_REF)
1946 {
1947 last_absdcl = absdcl;
1948 absdcl = TREE_OPERAND (absdcl, 0);
1949 }
1950
1951 has_array = 1;
1952 this_nelts = TREE_OPERAND (absdcl, 1);
1953 if (this_nelts != error_mark_node)
1954 {
1955 if (this_nelts == NULL_TREE)
1956 error ("new of array type fails to specify size");
1957 else if (processing_template_decl)
1958 {
1959 nelts = this_nelts;
1960 absdcl = TREE_OPERAND (absdcl, 0);
1961 }
1962 else
1963 {
1964 int flags = pedantic ? WANT_INT : (WANT_INT | WANT_ENUM);
1965 if (build_expr_type_conversion (flags, this_nelts, 0)
1966 == NULL_TREE)
1967 pedwarn ("size in array new must have integral type");
1968
1969 this_nelts = save_expr (cp_convert (sizetype, this_nelts));
1970 absdcl = TREE_OPERAND (absdcl, 0);
1971 if (this_nelts == integer_zero_node)
1972 {
1973 warning ("zero size array reserves no space");
1974 nelts = integer_zero_node;
1975 }
1976 else
1977 nelts = build_binary_op (MULT_EXPR, nelts, this_nelts);
1978 }
1979 }
1980 else
1981 nelts = integer_zero_node;
1982 }
1983
1984 if (last_absdcl)
1985 TREE_OPERAND (last_absdcl, 0) = absdcl;
1986 else
1987 TREE_VALUE (decl) = absdcl;
1988
1989 type = groktypename (decl);
1990 if (! type || type == error_mark_node)
1991 return error_mark_node;
1992 }
1993 else if (TREE_CODE (decl) == IDENTIFIER_NODE)
1994 {
1995 if (IDENTIFIER_HAS_TYPE_VALUE (decl))
1996 {
1997 /* An aggregate type. */
1998 type = IDENTIFIER_TYPE_VALUE (decl);
1999 decl = TYPE_MAIN_DECL (type);
2000 }
2001 else
2002 {
2003 /* A builtin type. */
2004 decl = lookup_name (decl, 1);
2005 my_friendly_assert (TREE_CODE (decl) == TYPE_DECL, 215);
2006 type = TREE_TYPE (decl);
2007 }
2008 }
2009 else if (TREE_CODE (decl) == TYPE_DECL)
2010 {
2011 type = TREE_TYPE (decl);
2012 }
2013 else
2014 {
2015 type = decl;
2016 decl = TYPE_MAIN_DECL (type);
2017 }
2018
2019 if (processing_template_decl)
2020 {
2021 if (has_array)
2022 t = tree_cons (tree_cons (NULL_TREE, type, NULL_TREE),
2023 build_min_nt (ARRAY_REF, NULL_TREE, nelts),
2024 NULL_TREE);
2025 else
2026 t = type;
2027
2028 rval = build_min_nt (NEW_EXPR, placement, t, init);
2029 NEW_EXPR_USE_GLOBAL (rval) = use_global_new;
2030 return rval;
2031 }
2032
2033 /* ``A reference cannot be created by the new operator. A reference
2034 is not an object (8.2.2, 8.4.3), so a pointer to it could not be
2035 returned by new.'' ARM 5.3.3 */
2036 if (TREE_CODE (type) == REFERENCE_TYPE)
2037 {
2038 error ("new cannot be applied to a reference type");
2039 type = TREE_TYPE (type);
2040 }
2041
2042 if (TREE_CODE (type) == FUNCTION_TYPE)
2043 {
2044 error ("new cannot be applied to a function type");
2045 return error_mark_node;
2046 }
2047
2048 /* When the object being created is an array, the new-expression yields a
2049 pointer to the initial element (if any) of the array. For example,
2050 both new int and new int[10] return an int*. 5.3.4. */
2051 if (TREE_CODE (type) == ARRAY_TYPE && has_array == 0)
2052 {
2053 nelts = array_type_nelts_top (type);
2054 has_array = 1;
2055 type = TREE_TYPE (type);
2056 }
2057
2058 if (has_array)
2059 t = build_nt (ARRAY_REF, type, nelts);
2060 else
2061 t = type;
2062
2063 rval = build (NEW_EXPR, build_pointer_type (type), placement, t, init);
2064 NEW_EXPR_USE_GLOBAL (rval) = use_global_new;
2065 TREE_SIDE_EFFECTS (rval) = 1;
2066 rval = build_new_1 (rval);
2067 if (rval == error_mark_node)
2068 return error_mark_node;
2069
2070 /* Wrap it in a NOP_EXPR so warn_if_unused_value doesn't complain. */
2071 rval = build1 (NOP_EXPR, TREE_TYPE (rval), rval);
2072 TREE_NO_UNUSED_WARNING (rval) = 1;
2073
2074 return rval;
2075 }
2076
2077 /* Given a Java class, return a decl for the corresponding java.lang.Class. */
2078
2079 static tree
2080 build_java_class_ref (type)
2081 tree type;
2082 {
2083 tree name, class_decl;
2084 static tree CL_prefix = NULL_TREE;
2085 if (CL_prefix == NULL_TREE)
2086 CL_prefix = get_identifier("_CL_");
2087 if (jclass_node == NULL_TREE)
2088 {
2089 jclass_node = IDENTIFIER_GLOBAL_VALUE (get_identifier("jclass"));
2090 if (jclass_node == NULL_TREE)
2091 fatal("call to Java constructor, while `jclass' undefined");
2092 jclass_node = TREE_TYPE (jclass_node);
2093 }
2094 name = build_overload_with_type (CL_prefix, type);
2095 class_decl = IDENTIFIER_GLOBAL_VALUE (name);
2096 if (class_decl == NULL_TREE)
2097 {
2098 class_decl = build_decl (VAR_DECL, name, TREE_TYPE (jclass_node));
2099 TREE_STATIC (class_decl) = 1;
2100 DECL_EXTERNAL (class_decl) = 1;
2101 TREE_PUBLIC (class_decl) = 1;
2102 DECL_ARTIFICIAL (class_decl) = 1;
2103 DECL_IGNORED_P (class_decl) = 1;
2104 pushdecl_top_level (class_decl);
2105 make_decl_rtl (class_decl, NULL_PTR, 1);
2106 }
2107 return class_decl;
2108 }
2109
2110 /* Called from cplus_expand_expr when expanding a NEW_EXPR. The return
2111 value is immediately handed to expand_expr. */
2112
2113 tree
2114 build_new_1 (exp)
2115 tree exp;
2116 {
2117 tree placement, init;
2118 tree type, true_type, size, rval;
2119 tree nelts = NULL_TREE;
2120 tree alloc_expr, alloc_node = NULL_TREE;
2121 int has_array = 0;
2122 enum tree_code code = NEW_EXPR;
2123 int use_cookie, nothrow, check_new;
2124 int use_global_new;
2125 int use_java_new = 0;
2126
2127 placement = TREE_OPERAND (exp, 0);
2128 type = TREE_OPERAND (exp, 1);
2129 init = TREE_OPERAND (exp, 2);
2130 use_global_new = NEW_EXPR_USE_GLOBAL (exp);
2131
2132 if (TREE_CODE (type) == ARRAY_REF)
2133 {
2134 has_array = 1;
2135 nelts = TREE_OPERAND (type, 1);
2136 type = TREE_OPERAND (type, 0);
2137 }
2138 true_type = type;
2139
2140 if (CP_TYPE_QUALS (type))
2141 type = TYPE_MAIN_VARIANT (type);
2142
2143 /* If our base type is an array, then make sure we know how many elements
2144 it has. */
2145 while (TREE_CODE (true_type) == ARRAY_TYPE)
2146 {
2147 tree this_nelts = array_type_nelts_top (true_type);
2148 nelts = build_binary_op (MULT_EXPR, nelts, this_nelts);
2149 true_type = TREE_TYPE (true_type);
2150 }
2151
2152 if (!complete_type_or_else (true_type, exp))
2153 return error_mark_node;
2154
2155 if (has_array)
2156 size = fold (build_binary_op (MULT_EXPR, size_in_bytes (true_type),
2157 nelts));
2158 else
2159 size = size_in_bytes (type);
2160
2161 if (TREE_CODE (true_type) == VOID_TYPE)
2162 {
2163 error ("invalid type `void' for new");
2164 return error_mark_node;
2165 }
2166
2167 if (abstract_virtuals_error (NULL_TREE, true_type))
2168 return error_mark_node;
2169
2170 /* When we allocate an array, and the corresponding deallocation
2171 function takes a second argument of type size_t, and that's the
2172 "usual deallocation function", we allocate some extra space at
2173 the beginning of the array to store the size of the array.
2174
2175 Well, that's what we should do. For backwards compatibility, we
2176 have to do this whenever there's a two-argument array-delete
2177 operator.
2178
2179 FIXME: For -fnew-abi, we don't have to maintain backwards
2180 compatibility and we should fix this. */
2181 use_cookie = (has_array && TYPE_VEC_NEW_USES_COOKIE (true_type)
2182 && ! (placement && ! TREE_CHAIN (placement)
2183 && TREE_TYPE (TREE_VALUE (placement)) == ptr_type_node));
2184
2185 if (use_cookie)
2186 size = size_binop (PLUS_EXPR, size, BI_header_size);
2187
2188 if (has_array)
2189 {
2190 code = VEC_NEW_EXPR;
2191
2192 if (init && pedantic)
2193 cp_pedwarn ("initialization in array new");
2194 }
2195
2196 /* Allocate the object. */
2197
2198 if (! placement && TYPE_FOR_JAVA (true_type))
2199 {
2200 tree class_addr, alloc_decl;
2201 tree class_decl = build_java_class_ref (true_type);
2202 tree class_size = size_in_bytes (true_type);
2203 static char alloc_name[] = "_Jv_AllocObject";
2204 use_java_new = 1;
2205 alloc_decl = IDENTIFIER_GLOBAL_VALUE (get_identifier (alloc_name));
2206 if (alloc_decl == NULL_TREE)
2207 fatal("call to Java constructor, while `%s' undefined", alloc_name);
2208 class_addr = build1 (ADDR_EXPR, jclass_node, class_decl);
2209 rval = build_function_call (alloc_decl,
2210 tree_cons (NULL_TREE, class_addr,
2211 build_tree_list (NULL_TREE,
2212 class_size)));
2213 rval = cp_convert (build_pointer_type (true_type), rval);
2214 }
2215 else
2216 {
2217 rval = build_op_new_call
2218 (code, true_type, tree_cons (NULL_TREE, size, placement),
2219 LOOKUP_NORMAL | (use_global_new * LOOKUP_GLOBAL));
2220 rval = cp_convert (build_pointer_type (true_type), rval);
2221 }
2222
2223 /* unless an allocation function is declared with an empty excep-
2224 tion-specification (_except.spec_), throw(), it indicates failure to
2225 allocate storage by throwing a bad_alloc exception (clause _except_,
2226 _lib.bad.alloc_); it returns a non-null pointer otherwise If the allo-
2227 cation function is declared with an empty exception-specification,
2228 throw(), it returns null to indicate failure to allocate storage and a
2229 non-null pointer otherwise.
2230
2231 So check for a null exception spec on the op new we just called. */
2232
2233 nothrow = 0;
2234 if (rval)
2235 {
2236 /* The CALL_EXPR. */
2237 tree t = TREE_OPERAND (rval, 0);
2238 /* The function. */
2239 t = TREE_OPERAND (TREE_OPERAND (t, 0), 0);
2240 nothrow = TYPE_NOTHROW_P (TREE_TYPE (t));
2241 }
2242 check_new = (flag_check_new || nothrow) && ! use_java_new;
2243
2244 if ((check_new || flag_exceptions) && rval)
2245 {
2246 alloc_expr = get_target_expr (rval);
2247 alloc_node = rval = TREE_OPERAND (alloc_expr, 0);
2248 }
2249 else
2250 alloc_expr = NULL_TREE;
2251
2252 /* if rval is NULL_TREE I don't have to allocate it, but are we totally
2253 sure we have some extra bytes in that case for the BI_header_size
2254 cookies? And how does that interact with the code below? (mrs) */
2255 /* Finish up some magic for new'ed arrays */
2256 if (use_cookie && rval != NULL_TREE)
2257 {
2258 tree extra = BI_header_size;
2259 tree cookie, exp1;
2260 rval = convert (string_type_node, rval); /* for ptr arithmetic */
2261 rval = save_expr (build_binary_op (PLUS_EXPR, rval, extra));
2262 /* Store header info. */
2263 cookie = build_indirect_ref (build (MINUS_EXPR,
2264 build_pointer_type (BI_header_type),
2265 rval, extra), NULL_PTR);
2266 exp1 = build (MODIFY_EXPR, void_type_node,
2267 build_component_ref (cookie, nelts_identifier,
2268 NULL_TREE, 0),
2269 nelts);
2270 rval = cp_convert (build_pointer_type (true_type), rval);
2271 rval = build_compound_expr
2272 (tree_cons (NULL_TREE, exp1,
2273 build_tree_list (NULL_TREE, rval)));
2274 }
2275
2276 if (rval == error_mark_node)
2277 return error_mark_node;
2278
2279 /* Don't call any constructors or do any initialization. */
2280 if (init == void_type_node)
2281 goto done;
2282
2283 if (TYPE_NEEDS_CONSTRUCTING (type) || init)
2284 {
2285 if (! TYPE_NEEDS_CONSTRUCTING (type)
2286 && ! IS_AGGR_TYPE (type) && ! has_array)
2287 {
2288 /* We are processing something like `new int (10)', which
2289 means allocate an int, and initialize it with 10. */
2290 tree deref;
2291 tree deref_type;
2292
2293 /* At present RVAL is a temporary variable, created to hold
2294 the value from the call to `operator new'. We transform
2295 it to (*RVAL = INIT, RVAL). */
2296 rval = save_expr (rval);
2297 deref = build_indirect_ref (rval, NULL_PTR);
2298
2299 /* Even for something like `new const int (10)' we must
2300 allow the expression to be non-const while we do the
2301 initialization. */
2302 deref_type = TREE_TYPE (deref);
2303 if (CP_TYPE_CONST_P (deref_type))
2304 TREE_TYPE (deref)
2305 = cp_build_qualified_type (deref_type,
2306 CP_TYPE_QUALS (deref_type)
2307 & ~TYPE_QUAL_CONST);
2308 TREE_READONLY (deref) = 0;
2309
2310 if (TREE_CHAIN (init) != NULL_TREE)
2311 pedwarn ("initializer list being treated as compound expression");
2312 else if (TREE_CODE (init) == CONSTRUCTOR)
2313 {
2314 pedwarn ("initializer list appears where operand should be used");
2315 init = TREE_OPERAND (init, 1);
2316 }
2317 init = build_compound_expr (init);
2318
2319 init = convert_for_initialization (deref, type, init, LOOKUP_NORMAL,
2320 "new", NULL_TREE, 0);
2321 rval = build (COMPOUND_EXPR, TREE_TYPE (rval),
2322 build_modify_expr (deref, NOP_EXPR, init),
2323 rval);
2324 TREE_NO_UNUSED_WARNING (rval) = 1;
2325 TREE_SIDE_EFFECTS (rval) = 1;
2326 }
2327 else if (! has_array)
2328 {
2329 tree newrval;
2330 /* Constructors are never virtual. If it has an initialization, we
2331 need to complain if we aren't allowed to use the ctor that took
2332 that argument. */
2333 int flags = LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_COMPLAIN;
2334
2335 if (rval && TYPE_USES_VIRTUAL_BASECLASSES (true_type))
2336 {
2337 init = tree_cons (NULL_TREE, integer_one_node, init);
2338 flags |= LOOKUP_HAS_IN_CHARGE;
2339 }
2340
2341 if (use_java_new)
2342 rval = save_expr (rval);
2343 newrval = rval;
2344
2345 if (newrval && TREE_CODE (TREE_TYPE (newrval)) == POINTER_TYPE)
2346 newrval = build_indirect_ref (newrval, NULL_PTR);
2347
2348 newrval = build_method_call (newrval, ctor_identifier,
2349 init, TYPE_BINFO (true_type), flags);
2350
2351 if (newrval == NULL_TREE || newrval == error_mark_node)
2352 return error_mark_node;
2353
2354 /* Java constructors compiled by jc1 do not return this. */
2355 if (use_java_new)
2356 newrval = build (COMPOUND_EXPR, TREE_TYPE (newrval),
2357 newrval, rval);
2358 rval = newrval;
2359 TREE_HAS_CONSTRUCTOR (rval) = 1;
2360 }
2361 else
2362 rval = (build_vec_init
2363 (NULL_TREE,
2364 save_expr (rval),
2365 build_binary_op (MINUS_EXPR, nelts, integer_one_node),
2366 init,
2367 /*from_array=*/0));
2368
2369 /* If any part of the object initialization terminates by throwing an
2370 exception and a suitable deallocation function can be found, the
2371 deallocation function is called to free the memory in which the
2372 object was being constructed, after which the exception continues
2373 to propagate in the context of the new-expression. If no
2374 unambiguous matching deallocation function can be found,
2375 propagating the exception does not cause the object's memory to be
2376 freed. */
2377 if (flag_exceptions && alloc_expr && ! use_java_new)
2378 {
2379 enum tree_code dcode = has_array ? VEC_DELETE_EXPR : DELETE_EXPR;
2380 tree cleanup, fn = NULL_TREE;
2381 int flags = LOOKUP_NORMAL | (use_global_new * LOOKUP_GLOBAL);
2382
2383 /* The Standard is unclear here, but the right thing to do
2384 is to use the same method for finding deallocation
2385 functions that we use for finding allocation functions. */
2386 flags |= LOOKUP_SPECULATIVELY;
2387
2388 /* We expect alloc_expr to look like a TARGET_EXPR around
2389 a NOP_EXPR around the CALL_EXPR we want. */
2390 fn = TREE_OPERAND (alloc_expr, 1);
2391 fn = TREE_OPERAND (fn, 0);
2392
2393 cleanup = build_op_delete_call (dcode, alloc_node, size, flags, fn);
2394
2395 /* Ack! First we allocate the memory. Then we set our sentry
2396 variable to true, and expand a cleanup that deletes the memory
2397 if sentry is true. Then we run the constructor and store the
2398 returned pointer in buf. Then we clear sentry and return buf. */
2399
2400 if (cleanup)
2401 {
2402 tree end, sentry, begin, buf, t = TREE_TYPE (rval);
2403
2404 begin = get_target_expr (boolean_true_node);
2405 sentry = TREE_OPERAND (begin, 0);
2406
2407 TREE_OPERAND (begin, 2)
2408 = build (COND_EXPR, void_type_node, sentry,
2409 cleanup, void_zero_node);
2410
2411 rval = get_target_expr (rval);
2412
2413 end = build (MODIFY_EXPR, TREE_TYPE (sentry),
2414 sentry, boolean_false_node);
2415
2416 buf = TREE_OPERAND (rval, 0);
2417
2418 rval = build (COMPOUND_EXPR, t, begin,
2419 build (COMPOUND_EXPR, t, rval,
2420 build (COMPOUND_EXPR, t, end, buf)));
2421 }
2422 }
2423 }
2424 else if (CP_TYPE_CONST_P (true_type))
2425 cp_error ("uninitialized const in `new' of `%#T'", true_type);
2426
2427 done:
2428
2429 if (alloc_expr && rval == alloc_node)
2430 {
2431 rval = TREE_OPERAND (alloc_expr, 1);
2432 alloc_expr = NULL_TREE;
2433 }
2434
2435 if (check_new && alloc_expr)
2436 {
2437 /* Did we modify the storage? */
2438 tree ifexp = build_binary_op (NE_EXPR, alloc_node,
2439 integer_zero_node);
2440 rval = build_conditional_expr (ifexp, rval, alloc_node);
2441 }
2442
2443 if (alloc_expr)
2444 rval = build (COMPOUND_EXPR, TREE_TYPE (rval), alloc_expr, rval);
2445
2446 if (rval && TREE_TYPE (rval) != build_pointer_type (type))
2447 {
2448 /* The type of new int [3][3] is not int *, but int [3] * */
2449 rval = build_c_cast (build_pointer_type (type), rval);
2450 }
2451
2452 return rval;
2453 }
2454 \f
2455 static tree
2456 build_vec_delete_1 (base, maxindex, type, auto_delete_vec, use_global_delete)
2457 tree base, maxindex, type;
2458 tree auto_delete_vec;
2459 int use_global_delete;
2460 {
2461 tree virtual_size;
2462 tree ptype = build_pointer_type (type = complete_type (type));
2463 tree size_exp = size_in_bytes (type);
2464
2465 /* Temporary variables used by the loop. */
2466 tree tbase, tbase_init;
2467
2468 /* This is the body of the loop that implements the deletion of a
2469 single element, and moves temp variables to next elements. */
2470 tree body;
2471
2472 /* This is the LOOP_EXPR that governs the deletion of the elements. */
2473 tree loop;
2474
2475 /* This is the thing that governs what to do after the loop has run. */
2476 tree deallocate_expr = 0;
2477
2478 /* This is the BIND_EXPR which holds the outermost iterator of the
2479 loop. It is convenient to set this variable up and test it before
2480 executing any other code in the loop.
2481 This is also the containing expression returned by this function. */
2482 tree controller = NULL_TREE;
2483
2484 if (! IS_AGGR_TYPE (type) || ! TYPE_NEEDS_DESTRUCTOR (type))
2485 {
2486 loop = integer_zero_node;
2487 goto no_destructor;
2488 }
2489
2490 /* The below is short by BI_header_size */
2491 virtual_size = fold (size_binop (MULT_EXPR, size_exp, maxindex));
2492
2493 tbase = create_temporary_var (ptype);
2494 tbase_init = build_modify_expr (tbase, NOP_EXPR,
2495 fold (build (PLUS_EXPR, ptype,
2496 base,
2497 virtual_size)));
2498 DECL_REGISTER (tbase) = 1;
2499 controller = build (BIND_EXPR, void_type_node, tbase, NULL_TREE, NULL_TREE);
2500 TREE_SIDE_EFFECTS (controller) = 1;
2501
2502 body = NULL_TREE;
2503
2504 body = tree_cons (NULL_TREE,
2505 build_delete (ptype, tbase, integer_two_node,
2506 LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 1),
2507 body);
2508
2509 body = tree_cons (NULL_TREE,
2510 build_modify_expr (tbase, NOP_EXPR, build (MINUS_EXPR, ptype, tbase, size_exp)),
2511 body);
2512
2513 body = tree_cons (NULL_TREE,
2514 build (EXIT_EXPR, void_type_node,
2515 build (EQ_EXPR, boolean_type_node, base, tbase)),
2516 body);
2517
2518 loop = build (LOOP_EXPR, void_type_node, build_compound_expr (body));
2519
2520 loop = tree_cons (NULL_TREE, tbase_init,
2521 tree_cons (NULL_TREE, loop, NULL_TREE));
2522 loop = build_compound_expr (loop);
2523
2524 no_destructor:
2525 /* If the delete flag is one, or anything else with the low bit set,
2526 delete the storage. */
2527 if (auto_delete_vec == integer_zero_node)
2528 deallocate_expr = integer_zero_node;
2529 else
2530 {
2531 tree base_tbd;
2532
2533 /* The below is short by BI_header_size */
2534 virtual_size = fold (size_binop (MULT_EXPR, size_exp, maxindex));
2535
2536 if (! TYPE_VEC_NEW_USES_COOKIE (type))
2537 /* no header */
2538 base_tbd = base;
2539 else
2540 {
2541 base_tbd = cp_convert (ptype,
2542 build_binary_op (MINUS_EXPR,
2543 cp_convert (string_type_node, base),
2544 BI_header_size));
2545 /* True size with header. */
2546 virtual_size = size_binop (PLUS_EXPR, virtual_size, BI_header_size);
2547 }
2548 deallocate_expr = build_x_delete (base_tbd,
2549 2 | use_global_delete,
2550 virtual_size);
2551 deallocate_expr = fold (build (COND_EXPR, void_type_node,
2552 fold (build (BIT_AND_EXPR,
2553 integer_type_node,
2554 auto_delete_vec,
2555 integer_one_node)),
2556 deallocate_expr, integer_zero_node));
2557 }
2558
2559 if (loop && deallocate_expr != integer_zero_node)
2560 {
2561 body = tree_cons (NULL_TREE, loop,
2562 tree_cons (NULL_TREE, deallocate_expr, NULL_TREE));
2563 body = build_compound_expr (body);
2564 }
2565 else
2566 body = loop;
2567
2568 /* Outermost wrapper: If pointer is null, punt. */
2569 body = fold (build (COND_EXPR, void_type_node,
2570 fold (build (NE_EXPR, boolean_type_node, base,
2571 integer_zero_node)),
2572 body, integer_zero_node));
2573 body = build1 (NOP_EXPR, void_type_node, body);
2574
2575 if (controller)
2576 {
2577 TREE_OPERAND (controller, 1) = body;
2578 return controller;
2579 }
2580 else
2581 return cp_convert (void_type_node, body);
2582 }
2583
2584 tree
2585 create_temporary_var (type)
2586 tree type;
2587 {
2588 tree decl;
2589
2590 decl = build_decl (VAR_DECL, NULL_TREE, type);
2591 TREE_USED (decl) = 1;
2592 DECL_ARTIFICIAL (decl) = 1;
2593 DECL_SOURCE_FILE (decl) = input_filename;
2594 DECL_SOURCE_LINE (decl) = lineno;
2595 DECL_IGNORED_P (decl) = 1;
2596 DECL_CONTEXT (decl) = current_function_decl;
2597
2598 return decl;
2599 }
2600
2601 /* Create a new temporary variable of the indicated TYPE, initialized
2602 to INIT.
2603
2604 It is not entered into current_binding_level, because that breaks
2605 things when it comes time to do final cleanups (which take place
2606 "outside" the binding contour of the function). */
2607
2608 static tree
2609 get_temp_regvar (type, init)
2610 tree type, init;
2611 {
2612 tree decl;
2613
2614 decl = create_temporary_var (type);
2615 if (building_stmt_tree ())
2616 add_decl_stmt (decl);
2617 if (!building_stmt_tree ())
2618 DECL_RTL (decl) = assign_temp (type, 2, 0, 1);
2619 finish_expr_stmt (build_modify_expr (decl, INIT_EXPR, init));
2620
2621 return decl;
2622 }
2623
2624 /* `build_vec_init' returns tree structure that performs
2625 initialization of a vector of aggregate types.
2626
2627 DECL is passed only for error reporting, and provides line number
2628 and source file name information.
2629 BASE is the space where the vector will be. For a vector of Ts,
2630 the type of BASE is `T*'.
2631 MAXINDEX is the maximum index of the array (one less than the
2632 number of elements).
2633 INIT is the (possibly NULL) initializer.
2634
2635 FROM_ARRAY is 0 if we should init everything with INIT
2636 (i.e., every element initialized from INIT).
2637 FROM_ARRAY is 1 if we should index into INIT in parallel
2638 with initialization of DECL.
2639 FROM_ARRAY is 2 if we should index into INIT in parallel,
2640 but use assignment instead of initialization. */
2641
2642 tree
2643 build_vec_init (decl, base, maxindex, init, from_array)
2644 tree decl, base, maxindex, init;
2645 int from_array;
2646 {
2647 tree rval;
2648 tree base2 = NULL_TREE;
2649 tree size;
2650 tree itype = NULL_TREE;
2651 tree iterator;
2652 /* The type of an element in the array. */
2653 tree type;
2654 /* The type of a pointer to an element in the array. */
2655 tree ptype;
2656 tree stmt_expr;
2657 tree compound_stmt;
2658 int destroy_temps;
2659 tree try_block = NULL_TREE;
2660 tree try_body = NULL_TREE;
2661 int num_initialized_elts = 0;
2662
2663 maxindex = cp_convert (ptrdiff_type_node, maxindex);
2664 if (maxindex == error_mark_node)
2665 return error_mark_node;
2666
2667 type = TREE_TYPE (TREE_TYPE (base));
2668 ptype = build_pointer_type (type);
2669 size = size_in_bytes (type);
2670
2671 /* The code we are generating looks like:
2672
2673 T* t1 = (T*) base;
2674 T* rval = base;
2675 ptrdiff_t iterator = maxindex;
2676 try {
2677 ... initializations from CONSTRUCTOR ...
2678 if (iterator != -1) {
2679 do {
2680 ... initialize *base ...
2681 ++base;
2682 } while (--iterator != -1);
2683 }
2684 } catch (...) {
2685 ... destroy elements that were constructed ...
2686 }
2687
2688 We can omit the try and catch blocks if we know that the
2689 initialization will never throw an exception, or if the array
2690 elements do not have destructors. If we have a CONSTRUCTOR to
2691 give us initialization information, we emit code to initialize
2692 each of the elements before the loop in the try block, and then
2693 iterate over fewer elements. We can omit the loop completely if
2694 the elements of the array do not have constructors.
2695
2696 We actually wrap the entire body of the above in a STMT_EXPR, for
2697 tidiness.
2698
2699 When copying from array to another, when the array elements have
2700 only trivial copy constructors, we should use __builtin_memcpy
2701 rather than generating a loop. That way, we could take advantage
2702 of whatever cleverness the back-end has for dealing with copies
2703 of blocks of memory. */
2704
2705 begin_init_stmts (&stmt_expr, &compound_stmt);
2706 destroy_temps = stmts_are_full_exprs_p;
2707 stmts_are_full_exprs_p = 0;
2708 rval = get_temp_regvar (ptype,
2709 cp_convert (ptype, default_conversion (base)));
2710 base = get_temp_regvar (ptype, rval);
2711 iterator = get_temp_regvar (ptrdiff_type_node, maxindex);
2712
2713 /* Protect the entire array initialization so that we can destroy
2714 the partially constructed array if an exception is thrown. */
2715 if (flag_exceptions && TYPE_NEEDS_DESTRUCTOR (type))
2716 {
2717 try_block = begin_try_block ();
2718 try_body = begin_compound_stmt (/*has_no_scope=*/1);
2719 }
2720
2721 if (init != NULL_TREE && TREE_CODE (init) == CONSTRUCTOR
2722 && (!decl || same_type_p (TREE_TYPE (init), TREE_TYPE (decl))))
2723 {
2724 /* Do non-default initialization resulting from brace-enclosed
2725 initializers. */
2726
2727 tree elts;
2728 from_array = 0;
2729
2730 for (elts = CONSTRUCTOR_ELTS (init); elts; elts = TREE_CHAIN (elts))
2731 {
2732 tree elt = TREE_VALUE (elts);
2733 tree baseref = build1 (INDIRECT_REF, type, base);
2734
2735 num_initialized_elts++;
2736
2737 if (IS_AGGR_TYPE (type) || TREE_CODE (type) == ARRAY_TYPE)
2738 finish_expr_stmt (build_aggr_init (baseref, elt, 0));
2739 else
2740 finish_expr_stmt (build_modify_expr (baseref, NOP_EXPR,
2741 elt));
2742
2743 finish_expr_stmt (build_modify_expr
2744 (base,
2745 NOP_EXPR,
2746 build (PLUS_EXPR, build_pointer_type (type),
2747 base, size)));
2748 finish_expr_stmt (build_modify_expr
2749 (iterator,
2750 NOP_EXPR,
2751 build (MINUS_EXPR, ptrdiff_type_node,
2752 iterator, integer_one_node)));
2753 }
2754
2755 /* Clear out INIT so that we don't get confused below. */
2756 init = NULL_TREE;
2757 }
2758 else if (from_array)
2759 {
2760 /* If initializing one array from another, initialize element by
2761 element. We rely upon the below calls the do argument
2762 checking. */
2763 if (decl == NULL_TREE)
2764 {
2765 sorry ("initialization of array from dissimilar array type");
2766 return error_mark_node;
2767 }
2768 if (init)
2769 {
2770 base2 = default_conversion (init);
2771 itype = TREE_TYPE (base2);
2772 base2 = get_temp_regvar (itype, base2);
2773 itype = TREE_TYPE (itype);
2774 }
2775 else if (TYPE_LANG_SPECIFIC (type)
2776 && TYPE_NEEDS_CONSTRUCTING (type)
2777 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
2778 {
2779 error ("initializer ends prematurely");
2780 return error_mark_node;
2781 }
2782 }
2783
2784 /* Now, default-initialize any remaining elements. We don't need to
2785 do that if a) the type does not need constructing, or b) we've
2786 already initialized all the elements.
2787
2788 We do need to keep going if we're copying an array. */
2789
2790 if (from_array
2791 || (TYPE_NEEDS_CONSTRUCTING (type)
2792 && !(TREE_CODE (maxindex) == INTEGER_CST
2793 && num_initialized_elts == TREE_INT_CST_LOW (maxindex) + 1)))
2794 {
2795 /* If the ITERATOR is equal to -1, then we don't have to loop;
2796 we've already initialized all the elements. */
2797 tree if_stmt;
2798 tree do_stmt;
2799 tree do_body;
2800 tree elt_init;
2801
2802 if_stmt = begin_if_stmt ();
2803 finish_if_stmt_cond (build (NE_EXPR, boolean_type_node,
2804 iterator, minus_one_node),
2805 if_stmt);
2806
2807 /* Otherwise, loop through the elements. */
2808 do_stmt = begin_do_stmt ();
2809 do_body = begin_compound_stmt (/*has_no_scope=*/1);
2810
2811 /* When we're not building a statement-tree, things are a little
2812 complicated. If, when we recursively call build_aggr_init,
2813 an expression containing a TARGET_EXPR is expanded, then it
2814 may get a cleanup. Then, the result of that expression is
2815 passed to finish_expr_stmt, which will call
2816 expand_start_target_temps/expand_end_target_temps. However,
2817 the latter call will not cause the cleanup to run because
2818 that block will still be on the block stack. So, we call
2819 expand_start_target_temps here manually; the corresponding
2820 call to expand_end_target_temps below will cause the cleanup
2821 to be performed. */
2822 if (!building_stmt_tree ())
2823 expand_start_target_temps ();
2824
2825 if (from_array)
2826 {
2827 tree to = build1 (INDIRECT_REF, type, base);
2828 tree from;
2829
2830 if (base2)
2831 from = build1 (INDIRECT_REF, itype, base2);
2832 else
2833 from = NULL_TREE;
2834
2835 if (from_array == 2)
2836 elt_init = build_modify_expr (to, NOP_EXPR, from);
2837 else if (TYPE_NEEDS_CONSTRUCTING (type))
2838 elt_init = build_aggr_init (to, from, 0);
2839 else if (from)
2840 elt_init = build_modify_expr (to, NOP_EXPR, from);
2841 else
2842 my_friendly_abort (57);
2843 }
2844 else if (TREE_CODE (type) == ARRAY_TYPE)
2845 {
2846 if (init != 0)
2847 sorry ("cannot initialize multi-dimensional array with initializer");
2848 elt_init = (build_vec_init
2849 (decl,
2850 build1 (NOP_EXPR,
2851 build_pointer_type (TREE_TYPE (type)),
2852 base),
2853 array_type_nelts (type), 0, 0));
2854 }
2855 else
2856 elt_init = build_aggr_init (build1 (INDIRECT_REF, type, base),
2857 init, 0);
2858
2859 /* The initialization of each array element is a
2860 full-expression. */
2861 if (!building_stmt_tree ())
2862 {
2863 finish_expr_stmt (elt_init);
2864 expand_end_target_temps ();
2865 }
2866 else
2867 {
2868 stmts_are_full_exprs_p = 1;
2869 finish_expr_stmt (elt_init);
2870 stmts_are_full_exprs_p = 0;
2871 }
2872
2873 finish_expr_stmt (build_modify_expr
2874 (base,
2875 NOP_EXPR,
2876 build (PLUS_EXPR, build_pointer_type (type),
2877 base, size)));
2878 if (base2)
2879 finish_expr_stmt (build_modify_expr
2880 (base2,
2881 NOP_EXPR,
2882 build (PLUS_EXPR, build_pointer_type (type),
2883 base2, size)));
2884
2885 finish_compound_stmt (/*has_no_scope=*/1, do_body);
2886 finish_do_body (do_stmt);
2887 finish_do_stmt (build (NE_EXPR, boolean_type_node,
2888 build (PREDECREMENT_EXPR,
2889 ptrdiff_type_node,
2890 iterator,
2891 integer_one_node),
2892 minus_one_node),
2893 do_stmt);
2894
2895 finish_then_clause (if_stmt);
2896 finish_if_stmt ();
2897 }
2898
2899 /* Make sure to cleanup any partially constructed elements. */
2900 if (flag_exceptions && TYPE_NEEDS_DESTRUCTOR (type))
2901 {
2902 tree e;
2903
2904 finish_compound_stmt (/*has_no_scope=*/1, try_body);
2905 finish_cleanup_try_block (try_block);
2906 e = build_vec_delete_1 (rval,
2907 build_binary_op (MINUS_EXPR, maxindex,
2908 iterator),
2909 type,
2910 /*auto_delete_vec=*/integer_zero_node,
2911 /*use_global_delete=*/0);
2912 finish_cleanup (e, try_block);
2913 }
2914
2915 /* The value of the array initialization is the address of the
2916 first element in the array. */
2917 finish_expr_stmt (rval);
2918
2919 stmt_expr = finish_init_stmts (stmt_expr, compound_stmt);
2920 stmts_are_full_exprs_p = destroy_temps;
2921 return stmt_expr;
2922 }
2923
2924 /* Free up storage of type TYPE, at address ADDR.
2925
2926 TYPE is a POINTER_TYPE and can be ptr_type_node for no special type
2927 of pointer.
2928
2929 VIRTUAL_SIZE is the amount of storage that was allocated, and is
2930 used as the second argument to operator delete. It can include
2931 things like padding and magic size cookies. It has virtual in it,
2932 because if you have a base pointer and you delete through a virtual
2933 destructor, it should be the size of the dynamic object, not the
2934 static object, see Free Store 12.5 ISO C++.
2935
2936 This does not call any destructors. */
2937
2938 tree
2939 build_x_delete (addr, which_delete, virtual_size)
2940 tree addr;
2941 int which_delete;
2942 tree virtual_size;
2943 {
2944 int use_global_delete = which_delete & 1;
2945 int use_vec_delete = !!(which_delete & 2);
2946 enum tree_code code = use_vec_delete ? VEC_DELETE_EXPR : DELETE_EXPR;
2947 int flags = LOOKUP_NORMAL | (use_global_delete * LOOKUP_GLOBAL);
2948
2949 return build_op_delete_call (code, addr, virtual_size, flags, NULL_TREE);
2950 }
2951
2952 /* Generate a call to a destructor. TYPE is the type to cast ADDR to.
2953 ADDR is an expression which yields the store to be destroyed.
2954 AUTO_DELETE is nonzero if a call to DELETE should be made or not.
2955 If in the program, (AUTO_DELETE & 2) is non-zero, we tear down the
2956 virtual baseclasses.
2957 If in the program, (AUTO_DELETE & 1) is non-zero, then we deallocate.
2958
2959 FLAGS is the logical disjunction of zero or more LOOKUP_
2960 flags. See cp-tree.h for more info.
2961
2962 This function does not delete an object's virtual base classes. */
2963
2964 tree
2965 build_delete (type, addr, auto_delete, flags, use_global_delete)
2966 tree type, addr;
2967 tree auto_delete;
2968 int flags;
2969 int use_global_delete;
2970 {
2971 tree member;
2972 tree expr;
2973 tree ref;
2974
2975 if (addr == error_mark_node)
2976 return error_mark_node;
2977
2978 /* Can happen when CURRENT_EXCEPTION_OBJECT gets its type
2979 set to `error_mark_node' before it gets properly cleaned up. */
2980 if (type == error_mark_node)
2981 return error_mark_node;
2982
2983 type = TYPE_MAIN_VARIANT (type);
2984
2985 if (TREE_CODE (type) == POINTER_TYPE)
2986 {
2987 type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
2988 if (type != void_type_node && !complete_type_or_else (type, addr))
2989 return error_mark_node;
2990 if (TREE_CODE (type) == ARRAY_TYPE)
2991 goto handle_array;
2992 if (! IS_AGGR_TYPE (type))
2993 {
2994 /* Call the builtin operator delete. */
2995 return build_builtin_delete_call (addr);
2996 }
2997 if (TREE_SIDE_EFFECTS (addr))
2998 addr = save_expr (addr);
2999
3000 /* throw away const and volatile on target type of addr */
3001 addr = convert_force (build_pointer_type (type), addr, 0);
3002 ref = build_indirect_ref (addr, NULL_PTR);
3003 }
3004 else if (TREE_CODE (type) == ARRAY_TYPE)
3005 {
3006 handle_array:
3007 if (TREE_SIDE_EFFECTS (addr))
3008 addr = save_expr (addr);
3009 if (TYPE_DOMAIN (type) == NULL_TREE)
3010 {
3011 error ("unknown array size in delete");
3012 return error_mark_node;
3013 }
3014 return build_vec_delete (addr, array_type_nelts (type),
3015 auto_delete, use_global_delete);
3016 }
3017 else
3018 {
3019 /* Don't check PROTECT here; leave that decision to the
3020 destructor. If the destructor is accessible, call it,
3021 else report error. */
3022 addr = build_unary_op (ADDR_EXPR, addr, 0);
3023 if (TREE_SIDE_EFFECTS (addr))
3024 addr = save_expr (addr);
3025
3026 if (TREE_CONSTANT (addr))
3027 addr = convert_pointer_to (type, addr);
3028 else
3029 addr = convert_force (build_pointer_type (type), addr, 0);
3030
3031 ref = build_indirect_ref (addr, NULL_PTR);
3032 }
3033
3034 my_friendly_assert (IS_AGGR_TYPE (type), 220);
3035
3036 if (! TYPE_NEEDS_DESTRUCTOR (type))
3037 {
3038 if (auto_delete == integer_zero_node)
3039 return void_zero_node;
3040
3041 return build_op_delete_call
3042 (DELETE_EXPR, addr, c_sizeof_nowarn (type),
3043 LOOKUP_NORMAL | (use_global_delete * LOOKUP_GLOBAL),
3044 NULL_TREE);
3045 }
3046
3047 /* Below, we will reverse the order in which these calls are made.
3048 If we have a destructor, then that destructor will take care
3049 of the base classes; otherwise, we must do that here. */
3050 if (TYPE_HAS_DESTRUCTOR (type))
3051 {
3052 tree passed_auto_delete;
3053 tree do_delete = NULL_TREE;
3054 tree ifexp;
3055
3056 if (use_global_delete)
3057 {
3058 tree cond = fold (build (BIT_AND_EXPR, integer_type_node,
3059 auto_delete, integer_one_node));
3060 tree call = build_builtin_delete_call (addr);
3061
3062 cond = fold (build (COND_EXPR, void_type_node, cond,
3063 call, void_zero_node));
3064 if (cond != void_zero_node)
3065 do_delete = cond;
3066
3067 passed_auto_delete = fold (build (BIT_AND_EXPR, integer_type_node,
3068 auto_delete, integer_two_node));
3069 }
3070 else
3071 passed_auto_delete = auto_delete;
3072
3073 expr = build_method_call
3074 (ref, dtor_identifier, build_tree_list (NULL_TREE, passed_auto_delete),
3075 NULL_TREE, flags);
3076
3077 if (do_delete)
3078 expr = build (COMPOUND_EXPR, void_type_node, expr, do_delete);
3079
3080 if (flags & LOOKUP_DESTRUCTOR)
3081 /* Explicit destructor call; don't check for null pointer. */
3082 ifexp = integer_one_node;
3083 else
3084 /* Handle deleting a null pointer. */
3085 ifexp = fold (build_binary_op (NE_EXPR, addr, integer_zero_node));
3086
3087 if (ifexp != integer_one_node)
3088 expr = build (COND_EXPR, void_type_node,
3089 ifexp, expr, void_zero_node);
3090
3091 return expr;
3092 }
3093 else
3094 {
3095 /* We only get here from finish_function for a destructor. */
3096 tree binfos = BINFO_BASETYPES (TYPE_BINFO (type));
3097 int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
3098 tree base_binfo = n_baseclasses > 0 ? TREE_VEC_ELT (binfos, 0) : NULL_TREE;
3099 tree exprstmt = NULL_TREE;
3100 tree parent_auto_delete = auto_delete;
3101 tree cond;
3102
3103 /* Set this again before we call anything, as we might get called
3104 recursively. */
3105 TYPE_HAS_DESTRUCTOR (type) = 1;
3106
3107 /* If we have member delete or vbases, we call delete in
3108 finish_function. */
3109 if (auto_delete == integer_zero_node)
3110 cond = NULL_TREE;
3111 else if (base_binfo == NULL_TREE
3112 || ! TYPE_NEEDS_DESTRUCTOR (BINFO_TYPE (base_binfo)))
3113 {
3114 cond = build (COND_EXPR, void_type_node,
3115 build (BIT_AND_EXPR, integer_type_node, auto_delete, integer_one_node),
3116 build_builtin_delete_call (addr),
3117 void_zero_node);
3118 }
3119 else
3120 cond = NULL_TREE;
3121
3122 if (cond)
3123 exprstmt = build_tree_list (NULL_TREE, cond);
3124
3125 if (base_binfo
3126 && ! TREE_VIA_VIRTUAL (base_binfo)
3127 && TYPE_NEEDS_DESTRUCTOR (BINFO_TYPE (base_binfo)))
3128 {
3129 tree this_auto_delete;
3130
3131 if (BINFO_OFFSET_ZEROP (base_binfo))
3132 this_auto_delete = parent_auto_delete;
3133 else
3134 this_auto_delete = integer_zero_node;
3135
3136 expr = build_scoped_method_call
3137 (ref, base_binfo, dtor_identifier,
3138 build_tree_list (NULL_TREE, this_auto_delete));
3139 exprstmt = tree_cons (NULL_TREE, expr, exprstmt);
3140 }
3141
3142 /* Take care of the remaining baseclasses. */
3143 for (i = 1; i < n_baseclasses; i++)
3144 {
3145 base_binfo = TREE_VEC_ELT (binfos, i);
3146 if (! TYPE_NEEDS_DESTRUCTOR (BINFO_TYPE (base_binfo))
3147 || TREE_VIA_VIRTUAL (base_binfo))
3148 continue;
3149
3150 expr = build_scoped_method_call
3151 (ref, base_binfo, dtor_identifier,
3152 build_tree_list (NULL_TREE, integer_zero_node));
3153
3154 exprstmt = tree_cons (NULL_TREE, expr, exprstmt);
3155 }
3156
3157 for (member = TYPE_FIELDS (type); member; member = TREE_CHAIN (member))
3158 {
3159 if (TREE_CODE (member) != FIELD_DECL)
3160 continue;
3161 if (TYPE_NEEDS_DESTRUCTOR (TREE_TYPE (member)))
3162 {
3163 tree this_member = build_component_ref (ref, DECL_NAME (member), NULL_TREE, 0);
3164 tree this_type = TREE_TYPE (member);
3165 expr = build_delete (this_type, this_member, integer_two_node, flags, 0);
3166 exprstmt = tree_cons (NULL_TREE, expr, exprstmt);
3167 }
3168 }
3169
3170 if (exprstmt)
3171 return build_compound_expr (exprstmt);
3172 /* Virtual base classes make this function do nothing. */
3173 return void_zero_node;
3174 }
3175 }
3176
3177 /* For type TYPE, delete the virtual baseclass objects of DECL. */
3178
3179 tree
3180 build_vbase_delete (type, decl)
3181 tree type, decl;
3182 {
3183 tree vbases = CLASSTYPE_VBASECLASSES (type);
3184 tree result = NULL_TREE;
3185 tree addr = build_unary_op (ADDR_EXPR, decl, 0);
3186
3187 my_friendly_assert (addr != error_mark_node, 222);
3188
3189 while (vbases)
3190 {
3191 tree this_addr = convert_force (build_pointer_type (BINFO_TYPE (vbases)),
3192 addr, 0);
3193 result = tree_cons (NULL_TREE,
3194 build_delete (TREE_TYPE (this_addr), this_addr,
3195 integer_zero_node,
3196 LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 0),
3197 result);
3198 vbases = TREE_CHAIN (vbases);
3199 }
3200 return build_compound_expr (nreverse (result));
3201 }
3202
3203 /* Build a C++ vector delete expression.
3204 MAXINDEX is the number of elements to be deleted.
3205 ELT_SIZE is the nominal size of each element in the vector.
3206 BASE is the expression that should yield the store to be deleted.
3207 This function expands (or synthesizes) these calls itself.
3208 AUTO_DELETE_VEC says whether the container (vector) should be deallocated.
3209
3210 This also calls delete for virtual baseclasses of elements of the vector.
3211
3212 Update: MAXINDEX is no longer needed. The size can be extracted from the
3213 start of the vector for pointers, and from the type for arrays. We still
3214 use MAXINDEX for arrays because it happens to already have one of the
3215 values we'd have to extract. (We could use MAXINDEX with pointers to
3216 confirm the size, and trap if the numbers differ; not clear that it'd
3217 be worth bothering.) */
3218
3219 tree
3220 build_vec_delete (base, maxindex, auto_delete_vec, use_global_delete)
3221 tree base, maxindex;
3222 tree auto_delete_vec;
3223 int use_global_delete;
3224 {
3225 tree type;
3226
3227 if (TREE_CODE (base) == OFFSET_REF)
3228 base = resolve_offset_ref (base);
3229
3230 type = TREE_TYPE (base);
3231
3232 base = stabilize_reference (base);
3233
3234 /* Since we can use base many times, save_expr it. */
3235 if (TREE_SIDE_EFFECTS (base))
3236 base = save_expr (base);
3237
3238 if (TREE_CODE (type) == POINTER_TYPE)
3239 {
3240 /* Step back one from start of vector, and read dimension. */
3241 tree cookie_addr = build (MINUS_EXPR, build_pointer_type (BI_header_type),
3242 base, BI_header_size);
3243 tree cookie = build_indirect_ref (cookie_addr, NULL_PTR);
3244 maxindex = build_component_ref (cookie, nelts_identifier, NULL_TREE, 0);
3245 do
3246 type = TREE_TYPE (type);
3247 while (TREE_CODE (type) == ARRAY_TYPE);
3248 }
3249 else if (TREE_CODE (type) == ARRAY_TYPE)
3250 {
3251 /* get the total number of things in the array, maxindex is a bad name */
3252 maxindex = array_type_nelts_total (type);
3253 while (TREE_CODE (type) == ARRAY_TYPE)
3254 type = TREE_TYPE (type);
3255 base = build_unary_op (ADDR_EXPR, base, 1);
3256 }
3257 else
3258 {
3259 if (base != error_mark_node)
3260 error ("type to vector delete is neither pointer or array type");
3261 return error_mark_node;
3262 }
3263
3264 return build_vec_delete_1 (base, maxindex, type, auto_delete_vec,
3265 use_global_delete);
3266 }